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.

174 lines
5.9KB

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