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.

217 lines
7.5KB

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