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.

168 lines
5.7KB

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