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.

169 lines
5.8KB

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