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.

222 lines
7.6KB

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