libopusdec.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 "libavutil/opt.h"
  27. #include "avcodec.h"
  28. #include "internal.h"
  29. #include "vorbis.h"
  30. #include "mathops.h"
  31. #include "libopus.h"
  32. struct libopus_context {
  33. AVClass *class;
  34. OpusMSDecoder *dec;
  35. int pre_skip;
  36. #ifndef OPUS_SET_GAIN
  37. union { int i; double d; } gain;
  38. #endif
  39. #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
  40. int apply_phase_inv;
  41. #endif
  42. };
  43. #define OPUS_HEAD_SIZE 19
  44. static av_cold int libopus_decode_init(AVCodecContext *avc)
  45. {
  46. struct libopus_context *opus = avc->priv_data;
  47. int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled;
  48. uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
  49. avc->channels = avc->extradata_size >= 10 ? avc->extradata[9] : (avc->channels == 1) ? 1 : 2;
  50. if (avc->channels <= 0) {
  51. av_log(avc, AV_LOG_WARNING,
  52. "Invalid number of channels %d, defaulting to stereo\n", avc->channels);
  53. avc->channels = 2;
  54. }
  55. avc->sample_rate = 48000;
  56. avc->sample_fmt = avc->request_sample_fmt == AV_SAMPLE_FMT_FLT ?
  57. AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S16;
  58. if (avc->extradata_size >= OPUS_HEAD_SIZE) {
  59. opus->pre_skip = AV_RL16(avc->extradata + 10);
  60. gain_db = sign_extend(AV_RL16(avc->extradata + 16), 16);
  61. channel_map = AV_RL8 (avc->extradata + 18);
  62. }
  63. if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + avc->channels) {
  64. nb_streams = avc->extradata[OPUS_HEAD_SIZE + 0];
  65. nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
  66. if (nb_streams + nb_coupled != avc->channels)
  67. av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
  68. mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
  69. } else {
  70. if (avc->channels > 2 || channel_map) {
  71. av_log(avc, AV_LOG_ERROR,
  72. "No channel mapping for %d channels.\n", avc->channels);
  73. return AVERROR(EINVAL);
  74. }
  75. nb_streams = 1;
  76. nb_coupled = avc->channels > 1;
  77. mapping = mapping_arr;
  78. }
  79. if (channel_map == 1) {
  80. avc->channel_layout = avc->channels > 8 ? 0 :
  81. ff_vorbis_channel_layouts[avc->channels - 1];
  82. if (avc->channels > 2 && avc->channels <= 8) {
  83. const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[avc->channels - 1];
  84. int ch;
  85. /* Remap channels from Vorbis order to ffmpeg order */
  86. for (ch = 0; ch < avc->channels; ch++)
  87. mapping_arr[ch] = mapping[vorbis_offset[ch]];
  88. mapping = mapping_arr;
  89. }
  90. } else if (channel_map == 2) {
  91. int ambisonic_order = ff_sqrt(avc->channels) - 1;
  92. if (avc->channels != (ambisonic_order + 1) * (ambisonic_order + 1) &&
  93. avc->channels != (ambisonic_order + 1) * (ambisonic_order + 1) + 2) {
  94. av_log(avc, AV_LOG_ERROR,
  95. "Channel mapping 2 is only specified for channel counts"
  96. " which can be written as (n + 1)^2 or (n + 2)^2 + 2"
  97. " for nonnegative integer n\n");
  98. return AVERROR_INVALIDDATA;
  99. }
  100. if (avc->channels > 227) {
  101. av_log(avc, AV_LOG_ERROR, "Too many channels\n");
  102. return AVERROR_INVALIDDATA;
  103. }
  104. avc->channel_layout = 0;
  105. } else {
  106. avc->channel_layout = 0;
  107. }
  108. opus->dec = opus_multistream_decoder_create(avc->sample_rate, avc->channels,
  109. nb_streams, nb_coupled,
  110. mapping, &ret);
  111. if (!opus->dec) {
  112. av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
  113. opus_strerror(ret));
  114. return ff_opus_error_to_averror(ret);
  115. }
  116. #ifdef OPUS_SET_GAIN
  117. ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
  118. if (ret != OPUS_OK)
  119. av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
  120. opus_strerror(ret));
  121. #else
  122. {
  123. double gain_lin = ff_exp10(gain_db / (20.0 * 256));
  124. if (avc->sample_fmt == AV_SAMPLE_FMT_FLT)
  125. opus->gain.d = gain_lin;
  126. else
  127. opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX);
  128. }
  129. #endif
  130. #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
  131. ret = opus_multistream_decoder_ctl(opus->dec,
  132. OPUS_SET_PHASE_INVERSION_DISABLED(!opus->apply_phase_inv));
  133. if (ret != OPUS_OK)
  134. av_log(avc, AV_LOG_WARNING,
  135. "Unable to set phase inversion: %s\n",
  136. opus_strerror(ret));
  137. #endif
  138. /* Decoder delay (in samples) at 48kHz */
  139. avc->delay = avc->internal->skip_samples = opus->pre_skip;
  140. return 0;
  141. }
  142. static av_cold int libopus_decode_close(AVCodecContext *avc)
  143. {
  144. struct libopus_context *opus = avc->priv_data;
  145. if (opus->dec) {
  146. opus_multistream_decoder_destroy(opus->dec);
  147. opus->dec = NULL;
  148. }
  149. return 0;
  150. }
  151. #define MAX_FRAME_SIZE (960 * 6)
  152. static int libopus_decode(AVCodecContext *avc, void *data,
  153. int *got_frame_ptr, AVPacket *pkt)
  154. {
  155. struct libopus_context *opus = avc->priv_data;
  156. AVFrame *frame = data;
  157. int ret, nb_samples;
  158. frame->nb_samples = MAX_FRAME_SIZE;
  159. if ((ret = ff_get_buffer(avc, frame, 0)) < 0)
  160. return ret;
  161. if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
  162. nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
  163. (opus_int16 *)frame->data[0],
  164. frame->nb_samples, 0);
  165. else
  166. nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
  167. (float *)frame->data[0],
  168. frame->nb_samples, 0);
  169. if (nb_samples < 0) {
  170. av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
  171. opus_strerror(nb_samples));
  172. return ff_opus_error_to_averror(nb_samples);
  173. }
  174. #ifndef OPUS_SET_GAIN
  175. {
  176. int i = avc->channels * nb_samples;
  177. if (avc->sample_fmt == AV_SAMPLE_FMT_FLT) {
  178. float *pcm = (float *)frame->data[0];
  179. for (; i > 0; i--, pcm++)
  180. *pcm = av_clipf(*pcm * opus->gain.d, -1, 1);
  181. } else {
  182. int16_t *pcm = (int16_t *)frame->data[0];
  183. for (; i > 0; i--, pcm++)
  184. *pcm = av_clip_int16(((int64_t)opus->gain.i * *pcm) >> 16);
  185. }
  186. }
  187. #endif
  188. frame->nb_samples = nb_samples;
  189. *got_frame_ptr = 1;
  190. return pkt->size;
  191. }
  192. static void libopus_flush(AVCodecContext *avc)
  193. {
  194. struct libopus_context *opus = avc->priv_data;
  195. opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
  196. /* The stream can have been extracted by a tool that is not Opus-aware.
  197. Therefore, any packet can become the first of the stream. */
  198. avc->internal->skip_samples = opus->pre_skip;
  199. }
  200. #define OFFSET(x) offsetof(struct libopus_context, x)
  201. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  202. static const AVOption libopusdec_options[] = {
  203. #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
  204. { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
  205. #endif
  206. { NULL },
  207. };
  208. static const AVClass libopusdec_class = {
  209. .class_name = "libopusdec",
  210. .item_name = av_default_item_name,
  211. .option = libopusdec_options,
  212. .version = LIBAVUTIL_VERSION_INT,
  213. };
  214. AVCodec ff_libopus_decoder = {
  215. .name = "libopus",
  216. .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
  217. .type = AVMEDIA_TYPE_AUDIO,
  218. .id = AV_CODEC_ID_OPUS,
  219. .priv_data_size = sizeof(struct libopus_context),
  220. .init = libopus_decode_init,
  221. .close = libopus_decode_close,
  222. .decode = libopus_decode,
  223. .flush = libopus_flush,
  224. .capabilities = AV_CODEC_CAP_DR1,
  225. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  226. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
  227. AV_SAMPLE_FMT_S16,
  228. AV_SAMPLE_FMT_NONE },
  229. .priv_class = &libopusdec_class,
  230. .wrapper_name = "libopus",
  231. };