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.

199 lines
6.7KB

  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/avassert.h"
  24. #include "libavutil/intreadwrite.h"
  25. #include "avcodec.h"
  26. #include "internal.h"
  27. #include "vorbis.h"
  28. #include "mathops.h"
  29. #include "libopus.h"
  30. struct libopus_context {
  31. OpusMSDecoder *dec;
  32. int pre_skip;
  33. #ifndef OPUS_SET_GAIN
  34. union { int i; double d; } gain;
  35. #endif
  36. };
  37. #define OPUS_HEAD_SIZE 19
  38. static av_cold int libopus_decode_init(AVCodecContext *avc)
  39. {
  40. struct libopus_context *opus = avc->priv_data;
  41. int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled;
  42. uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
  43. avc->sample_rate = 48000;
  44. avc->sample_fmt = avc->request_sample_fmt == AV_SAMPLE_FMT_FLT ?
  45. AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S16;
  46. avc->channel_layout = avc->channels > 8 ? 0 :
  47. ff_vorbis_channel_layouts[avc->channels - 1];
  48. if (avc->extradata_size >= OPUS_HEAD_SIZE) {
  49. opus->pre_skip = AV_RL16(avc->extradata + 10);
  50. gain_db = sign_extend(AV_RL16(avc->extradata + 16), 16);
  51. channel_map = AV_RL8 (avc->extradata + 18);
  52. }
  53. if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + avc->channels) {
  54. nb_streams = avc->extradata[OPUS_HEAD_SIZE + 0];
  55. nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
  56. if (nb_streams + nb_coupled != avc->channels)
  57. av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
  58. mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
  59. } else {
  60. if (avc->channels > 2 || channel_map) {
  61. av_log(avc, AV_LOG_ERROR,
  62. "No channel mapping for %d channels.\n", avc->channels);
  63. return AVERROR(EINVAL);
  64. }
  65. nb_streams = 1;
  66. nb_coupled = avc->channels > 1;
  67. mapping = mapping_arr;
  68. }
  69. if (avc->channels > 2 && avc->channels <= 8) {
  70. const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[avc->channels - 1];
  71. int ch;
  72. /* Remap channels from vorbis order to ffmpeg order */
  73. for (ch = 0; ch < avc->channels; ch++)
  74. mapping_arr[ch] = mapping[vorbis_offset[ch]];
  75. mapping = mapping_arr;
  76. }
  77. opus->dec = opus_multistream_decoder_create(avc->sample_rate, avc->channels,
  78. nb_streams, nb_coupled,
  79. mapping, &ret);
  80. if (!opus->dec) {
  81. av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
  82. opus_strerror(ret));
  83. return ff_opus_error_to_averror(ret);
  84. }
  85. #ifdef OPUS_SET_GAIN
  86. ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
  87. if (ret != OPUS_OK)
  88. av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
  89. opus_strerror(ret));
  90. #else
  91. {
  92. double gain_lin = pow(10, gain_db / (20.0 * 256));
  93. if (avc->sample_fmt == AV_SAMPLE_FMT_FLT)
  94. opus->gain.d = gain_lin;
  95. else
  96. opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX);
  97. }
  98. #endif
  99. /* Decoder delay (in samples) at 48kHz */
  100. avc->delay = avc->internal->skip_samples = opus->pre_skip;
  101. return 0;
  102. }
  103. static av_cold int libopus_decode_close(AVCodecContext *avc)
  104. {
  105. struct libopus_context *opus = avc->priv_data;
  106. opus_multistream_decoder_destroy(opus->dec);
  107. return 0;
  108. }
  109. #define MAX_FRAME_SIZE (960 * 6)
  110. static int libopus_decode(AVCodecContext *avc, void *data,
  111. int *got_frame_ptr, AVPacket *pkt)
  112. {
  113. struct libopus_context *opus = avc->priv_data;
  114. AVFrame *frame = data;
  115. int ret, nb_samples;
  116. frame->nb_samples = MAX_FRAME_SIZE;
  117. if ((ret = ff_get_buffer(avc, frame, 0)) < 0)
  118. return ret;
  119. if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
  120. nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
  121. (opus_int16 *)frame->data[0],
  122. frame->nb_samples, 0);
  123. else
  124. nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
  125. (float *)frame->data[0],
  126. frame->nb_samples, 0);
  127. if (nb_samples < 0) {
  128. av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
  129. opus_strerror(nb_samples));
  130. return ff_opus_error_to_averror(nb_samples);
  131. }
  132. #ifndef OPUS_SET_GAIN
  133. {
  134. int i = avc->channels * nb_samples;
  135. if (avc->sample_fmt == AV_SAMPLE_FMT_FLT) {
  136. float *pcm = (float *)frame->data[0];
  137. for (; i > 0; i--, pcm++)
  138. *pcm = av_clipf(*pcm * opus->gain.d, -1, 1);
  139. } else {
  140. int16_t *pcm = (int16_t *)frame->data[0];
  141. for (; i > 0; i--, pcm++)
  142. *pcm = av_clip_int16(((int64_t)opus->gain.i * *pcm) >> 16);
  143. }
  144. }
  145. #endif
  146. frame->nb_samples = nb_samples;
  147. *got_frame_ptr = 1;
  148. return pkt->size;
  149. }
  150. static void libopus_flush(AVCodecContext *avc)
  151. {
  152. struct libopus_context *opus = avc->priv_data;
  153. opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
  154. /* The stream can have been extracted by a tool that is not Opus-aware.
  155. Therefore, any packet can become the first of the stream. */
  156. avc->internal->skip_samples = opus->pre_skip;
  157. }
  158. AVCodec ff_libopus_decoder = {
  159. .name = "libopus",
  160. .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
  161. .type = AVMEDIA_TYPE_AUDIO,
  162. .id = AV_CODEC_ID_OPUS,
  163. .priv_data_size = sizeof(struct libopus_context),
  164. .init = libopus_decode_init,
  165. .close = libopus_decode_close,
  166. .decode = libopus_decode,
  167. .flush = libopus_flush,
  168. .capabilities = AV_CODEC_CAP_DR1,
  169. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
  170. AV_SAMPLE_FMT_S16,
  171. AV_SAMPLE_FMT_NONE },
  172. };