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.

261 lines
9.0KB

  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. opus_multistream_decoder_destroy(opus->dec);
  146. return 0;
  147. }
  148. #define MAX_FRAME_SIZE (960 * 6)
  149. static int libopus_decode(AVCodecContext *avc, void *data,
  150. int *got_frame_ptr, AVPacket *pkt)
  151. {
  152. struct libopus_context *opus = avc->priv_data;
  153. AVFrame *frame = data;
  154. int ret, nb_samples;
  155. frame->nb_samples = MAX_FRAME_SIZE;
  156. if ((ret = ff_get_buffer(avc, frame, 0)) < 0)
  157. return ret;
  158. if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
  159. nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
  160. (opus_int16 *)frame->data[0],
  161. frame->nb_samples, 0);
  162. else
  163. nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
  164. (float *)frame->data[0],
  165. frame->nb_samples, 0);
  166. if (nb_samples < 0) {
  167. av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
  168. opus_strerror(nb_samples));
  169. return ff_opus_error_to_averror(nb_samples);
  170. }
  171. #ifndef OPUS_SET_GAIN
  172. {
  173. int i = avc->channels * nb_samples;
  174. if (avc->sample_fmt == AV_SAMPLE_FMT_FLT) {
  175. float *pcm = (float *)frame->data[0];
  176. for (; i > 0; i--, pcm++)
  177. *pcm = av_clipf(*pcm * opus->gain.d, -1, 1);
  178. } else {
  179. int16_t *pcm = (int16_t *)frame->data[0];
  180. for (; i > 0; i--, pcm++)
  181. *pcm = av_clip_int16(((int64_t)opus->gain.i * *pcm) >> 16);
  182. }
  183. }
  184. #endif
  185. frame->nb_samples = nb_samples;
  186. *got_frame_ptr = 1;
  187. return pkt->size;
  188. }
  189. static void libopus_flush(AVCodecContext *avc)
  190. {
  191. struct libopus_context *opus = avc->priv_data;
  192. opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
  193. /* The stream can have been extracted by a tool that is not Opus-aware.
  194. Therefore, any packet can become the first of the stream. */
  195. avc->internal->skip_samples = opus->pre_skip;
  196. }
  197. #define OFFSET(x) offsetof(struct libopus_context, x)
  198. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  199. static const AVOption libopusdec_options[] = {
  200. #ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
  201. { "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
  202. #endif
  203. { NULL },
  204. };
  205. static const AVClass libopusdec_class = {
  206. .class_name = "libopusdec",
  207. .item_name = av_default_item_name,
  208. .option = libopusdec_options,
  209. .version = LIBAVUTIL_VERSION_INT,
  210. };
  211. AVCodec ff_libopus_decoder = {
  212. .name = "libopus",
  213. .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
  214. .type = AVMEDIA_TYPE_AUDIO,
  215. .id = AV_CODEC_ID_OPUS,
  216. .priv_data_size = sizeof(struct libopus_context),
  217. .init = libopus_decode_init,
  218. .close = libopus_decode_close,
  219. .decode = libopus_decode,
  220. .flush = libopus_flush,
  221. .capabilities = AV_CODEC_CAP_DR1,
  222. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
  223. AV_SAMPLE_FMT_S16,
  224. AV_SAMPLE_FMT_NONE },
  225. .priv_class = &libopusdec_class,
  226. .wrapper_name = "libopus",
  227. };