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.

246 lines
8.3KB

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