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.

190 lines
6.4KB

  1. /*
  2. * Opus decoder using libopus
  3. * Copyright (c) 2012 Nicolas George
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "vorbis.h"
  28. #include "mathops.h"
  29. struct libopus_context {
  30. OpusMSDecoder *dec;
  31. AVFrame frame;
  32. };
  33. static int opus_error_to_averror(int err)
  34. {
  35. switch (err) {
  36. case OPUS_BAD_ARG:
  37. return AVERROR(EINVAL);
  38. case OPUS_BUFFER_TOO_SMALL:
  39. return AVERROR_UNKNOWN;
  40. case OPUS_INTERNAL_ERROR:
  41. return AVERROR(EFAULT);
  42. case OPUS_INVALID_PACKET:
  43. return AVERROR_INVALIDDATA;
  44. case OPUS_UNIMPLEMENTED:
  45. return AVERROR(ENOSYS);
  46. case OPUS_INVALID_STATE:
  47. return AVERROR_UNKNOWN;
  48. case OPUS_ALLOC_FAIL:
  49. return AVERROR(ENOMEM);
  50. default:
  51. return AVERROR(EINVAL);
  52. }
  53. }
  54. #define OPUS_HEAD_SIZE 19
  55. static av_cold int libopus_decode_init(AVCodecContext *avc)
  56. {
  57. struct libopus_context *opus = avc->priv_data;
  58. int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled;
  59. uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
  60. avc->sample_rate = 48000;
  61. avc->sample_fmt = avc->request_sample_fmt == AV_SAMPLE_FMT_FLT ?
  62. AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S16;
  63. avc->channel_layout = avc->channels > 8 ? 0 :
  64. ff_vorbis_channel_layouts[avc->channels - 1];
  65. if (avc->extradata_size >= OPUS_HEAD_SIZE) {
  66. gain_db = sign_extend(AV_RL16(avc->extradata + 16), 16);
  67. channel_map = AV_RL8 (avc->extradata + 18);
  68. }
  69. if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + avc->channels) {
  70. nb_streams = avc->extradata[OPUS_HEAD_SIZE + 0];
  71. nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
  72. if (nb_streams + nb_coupled != avc->channels)
  73. av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
  74. mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
  75. } else {
  76. if (avc->channels > 2 || channel_map) {
  77. av_log(avc, AV_LOG_ERROR,
  78. "No channel mapping for %d channels.\n", avc->channels);
  79. return AVERROR(EINVAL);
  80. }
  81. nb_streams = 1;
  82. nb_coupled = avc->channels > 1;
  83. mapping = mapping_arr;
  84. }
  85. if (avc->channels > 2 && avc->channels <= 8) {
  86. const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[avc->channels - 1];
  87. int ch;
  88. /* Remap channels from vorbis order to libav order */
  89. for (ch = 0; ch < avc->channels; ch++)
  90. mapping_arr[ch] = mapping[vorbis_offset[ch]];
  91. mapping = mapping_arr;
  92. }
  93. opus->dec = opus_multistream_decoder_create(avc->sample_rate, avc->channels,
  94. nb_streams, nb_coupled,
  95. mapping, &ret);
  96. if (!opus->dec) {
  97. av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
  98. opus_strerror(ret));
  99. return opus_error_to_averror(ret);
  100. }
  101. ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
  102. if (ret != OPUS_OK)
  103. av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
  104. opus_strerror(ret));
  105. avc->delay = 3840; /* Decoder delay (in samples) at 48kHz */
  106. avcodec_get_frame_defaults(&opus->frame);
  107. avc->coded_frame = &opus->frame;
  108. return 0;
  109. }
  110. static av_cold int libopus_decode_close(AVCodecContext *avc)
  111. {
  112. struct libopus_context *opus = avc->priv_data;
  113. opus_multistream_decoder_destroy(opus->dec);
  114. return 0;
  115. }
  116. #define MAX_FRAME_SIZE (960 * 6)
  117. static int libopus_decode(AVCodecContext *avc, void *frame,
  118. int *got_frame_ptr, AVPacket *pkt)
  119. {
  120. struct libopus_context *opus = avc->priv_data;
  121. int ret, nb_samples;
  122. opus->frame.nb_samples = MAX_FRAME_SIZE;
  123. ret = avc->get_buffer(avc, &opus->frame);
  124. if (ret < 0) {
  125. av_log(avc, AV_LOG_ERROR, "get_buffer() failed\n");
  126. return ret;
  127. }
  128. if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
  129. nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
  130. (opus_int16 *)opus->frame.data[0],
  131. opus->frame.nb_samples, 0);
  132. else
  133. nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
  134. (float *)opus->frame.data[0],
  135. opus->frame.nb_samples, 0);
  136. if (nb_samples < 0) {
  137. av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
  138. opus_strerror(nb_samples));
  139. return opus_error_to_averror(nb_samples);
  140. }
  141. opus->frame.nb_samples = nb_samples;
  142. *(AVFrame *)frame = opus->frame;
  143. *got_frame_ptr = 1;
  144. return pkt->size;
  145. }
  146. static void libopus_flush(AVCodecContext *avc)
  147. {
  148. struct libopus_context *opus = avc->priv_data;
  149. opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
  150. }
  151. AVCodec ff_libopus_decoder = {
  152. .name = "libopus",
  153. .type = AVMEDIA_TYPE_AUDIO,
  154. .id = AV_CODEC_ID_OPUS,
  155. .priv_data_size = sizeof(struct libopus_context),
  156. .init = libopus_decode_init,
  157. .close = libopus_decode_close,
  158. .decode = libopus_decode,
  159. .flush = libopus_flush,
  160. .capabilities = CODEC_CAP_DR1,
  161. .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
  162. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
  163. AV_SAMPLE_FMT_S16,
  164. AV_SAMPLE_FMT_NONE },
  165. };