You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
7.9KB

  1. /*
  2. * Opus decoder using libopus
  3. * Copyright (c) 2012 Nicolas George
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <opus.h>
  22. #include <opus_multistream.h>
  23. #include "libavutil/internal.h"
  24. #include "libavutil/intreadwrite.h"
  25. #include "libavutil/ffmath.h"
  26. #include "avcodec.h"
  27. #include "internal.h"
  28. #include "vorbis.h"
  29. #include "mathops.h"
  30. #include "libopus.h"
  31. struct libopus_context {
  32. OpusMSDecoder *dec;
  33. int pre_skip;
  34. #ifndef OPUS_SET_GAIN
  35. union { int i; double d; } gain;
  36. #endif
  37. };
  38. #define OPUS_HEAD_SIZE 19
  39. static av_cold int libopus_decode_init(AVCodecContext *avc)
  40. {
  41. struct libopus_context *opus = avc->priv_data;
  42. int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled;
  43. uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
  44. avc->channels = avc->extradata_size >= 10 ? avc->extradata[9] : (avc->channels == 1) ? 1 : 2;
  45. if (avc->channels <= 0) {
  46. av_log(avc, AV_LOG_WARNING,
  47. "Invalid number of channels %d, defaulting to stereo\n", avc->channels);
  48. avc->channels = 2;
  49. }
  50. avc->sample_rate = 48000;
  51. avc->sample_fmt = avc->request_sample_fmt == AV_SAMPLE_FMT_FLT ?
  52. AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S16;
  53. if (avc->extradata_size >= OPUS_HEAD_SIZE) {
  54. opus->pre_skip = AV_RL16(avc->extradata + 10);
  55. gain_db = sign_extend(AV_RL16(avc->extradata + 16), 16);
  56. channel_map = AV_RL8 (avc->extradata + 18);
  57. }
  58. if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + avc->channels) {
  59. nb_streams = avc->extradata[OPUS_HEAD_SIZE + 0];
  60. nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
  61. if (nb_streams + nb_coupled != avc->channels)
  62. av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
  63. mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
  64. } else {
  65. if (avc->channels > 2 || channel_map) {
  66. av_log(avc, AV_LOG_ERROR,
  67. "No channel mapping for %d channels.\n", avc->channels);
  68. return AVERROR(EINVAL);
  69. }
  70. nb_streams = 1;
  71. nb_coupled = avc->channels > 1;
  72. mapping = mapping_arr;
  73. }
  74. if (channel_map == 1) {
  75. avc->channel_layout = avc->channels > 8 ? 0 :
  76. ff_vorbis_channel_layouts[avc->channels - 1];
  77. if (avc->channels > 2 && avc->channels <= 8) {
  78. const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[avc->channels - 1];
  79. int ch;
  80. /* Remap channels from Vorbis order to ffmpeg order */
  81. for (ch = 0; ch < avc->channels; ch++)
  82. mapping_arr[ch] = mapping[vorbis_offset[ch]];
  83. mapping = mapping_arr;
  84. }
  85. } else if (channel_map == 2) {
  86. int ambisonic_order = ff_sqrt(avc->channels) - 1;
  87. if (avc->channels != (ambisonic_order + 1) * (ambisonic_order + 1) &&
  88. avc->channels != (ambisonic_order + 1) * (ambisonic_order + 1) + 2) {
  89. av_log(avc, AV_LOG_ERROR,
  90. "Channel mapping 2 is only specified for channel counts"
  91. " which can be written as (n + 1)^2 or (n + 2)^2 + 2"
  92. " for nonnegative integer n\n");
  93. return AVERROR_INVALIDDATA;
  94. }
  95. if (avc->channels > 227) {
  96. av_log(avc, AV_LOG_ERROR, "Too many channels\n");
  97. return AVERROR_INVALIDDATA;
  98. }
  99. avc->channel_layout = 0;
  100. } else {
  101. avc->channel_layout = 0;
  102. }
  103. opus->dec = opus_multistream_decoder_create(avc->sample_rate, avc->channels,
  104. nb_streams, nb_coupled,
  105. mapping, &ret);
  106. if (!opus->dec) {
  107. av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
  108. opus_strerror(ret));
  109. return ff_opus_error_to_averror(ret);
  110. }
  111. #ifdef OPUS_SET_GAIN
  112. ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
  113. if (ret != OPUS_OK)
  114. av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
  115. opus_strerror(ret));
  116. #else
  117. {
  118. double gain_lin = ff_exp10(gain_db / (20.0 * 256));
  119. if (avc->sample_fmt == AV_SAMPLE_FMT_FLT)
  120. opus->gain.d = gain_lin;
  121. else
  122. opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX);
  123. }
  124. #endif
  125. /* Decoder delay (in samples) at 48kHz */
  126. avc->delay = avc->internal->skip_samples = opus->pre_skip;
  127. return 0;
  128. }
  129. static av_cold int libopus_decode_close(AVCodecContext *avc)
  130. {
  131. struct libopus_context *opus = avc->priv_data;
  132. opus_multistream_decoder_destroy(opus->dec);
  133. return 0;
  134. }
  135. #define MAX_FRAME_SIZE (960 * 6)
  136. static int libopus_decode(AVCodecContext *avc, void *data,
  137. int *got_frame_ptr, AVPacket *pkt)
  138. {
  139. struct libopus_context *opus = avc->priv_data;
  140. AVFrame *frame = data;
  141. int ret, nb_samples;
  142. frame->nb_samples = MAX_FRAME_SIZE;
  143. if ((ret = ff_get_buffer(avc, frame, 0)) < 0)
  144. return ret;
  145. if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
  146. nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
  147. (opus_int16 *)frame->data[0],
  148. frame->nb_samples, 0);
  149. else
  150. nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
  151. (float *)frame->data[0],
  152. frame->nb_samples, 0);
  153. if (nb_samples < 0) {
  154. av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
  155. opus_strerror(nb_samples));
  156. return ff_opus_error_to_averror(nb_samples);
  157. }
  158. #ifndef OPUS_SET_GAIN
  159. {
  160. int i = avc->channels * nb_samples;
  161. if (avc->sample_fmt == AV_SAMPLE_FMT_FLT) {
  162. float *pcm = (float *)frame->data[0];
  163. for (; i > 0; i--, pcm++)
  164. *pcm = av_clipf(*pcm * opus->gain.d, -1, 1);
  165. } else {
  166. int16_t *pcm = (int16_t *)frame->data[0];
  167. for (; i > 0; i--, pcm++)
  168. *pcm = av_clip_int16(((int64_t)opus->gain.i * *pcm) >> 16);
  169. }
  170. }
  171. #endif
  172. frame->nb_samples = nb_samples;
  173. *got_frame_ptr = 1;
  174. return pkt->size;
  175. }
  176. static void libopus_flush(AVCodecContext *avc)
  177. {
  178. struct libopus_context *opus = avc->priv_data;
  179. opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
  180. /* The stream can have been extracted by a tool that is not Opus-aware.
  181. Therefore, any packet can become the first of the stream. */
  182. avc->internal->skip_samples = opus->pre_skip;
  183. }
  184. AVCodec ff_libopus_decoder = {
  185. .name = "libopus",
  186. .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
  187. .type = AVMEDIA_TYPE_AUDIO,
  188. .id = AV_CODEC_ID_OPUS,
  189. .priv_data_size = sizeof(struct libopus_context),
  190. .init = libopus_decode_init,
  191. .close = libopus_decode_close,
  192. .decode = libopus_decode,
  193. .flush = libopus_flush,
  194. .capabilities = AV_CODEC_CAP_DR1,
  195. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
  196. AV_SAMPLE_FMT_S16,
  197. AV_SAMPLE_FMT_NONE },
  198. .wrapper_name = "libopus",
  199. };