00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <opus.h>
00023 #include <opus_multistream.h>
00024
00025 #include "libavutil/avassert.h"
00026 #include "libavutil/intreadwrite.h"
00027 #include "avcodec.h"
00028 #include "internal.h"
00029 #include "vorbis.h"
00030 #include "mathops.h"
00031 #include "libopus.h"
00032
00033 struct libopus_context {
00034 OpusMSDecoder *dec;
00035 AVFrame frame;
00036 int pre_skip;
00037 #ifndef OPUS_SET_GAIN
00038 union { int i; double d; } gain;
00039 #endif
00040 };
00041
00042 #define OPUS_HEAD_SIZE 19
00043
00044 static av_cold int libopus_decode_init(AVCodecContext *avc)
00045 {
00046 struct libopus_context *opus = avc->priv_data;
00047 int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled;
00048 uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
00049
00050 avc->sample_rate = 48000;
00051 avc->sample_fmt = avc->request_sample_fmt == AV_SAMPLE_FMT_FLT ?
00052 AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S16;
00053 avc->channel_layout = avc->channels > 8 ? 0 :
00054 ff_vorbis_channel_layouts[avc->channels - 1];
00055
00056 if (avc->extradata_size >= OPUS_HEAD_SIZE) {
00057 opus->pre_skip = AV_RL16(avc->extradata + 10);
00058 gain_db = sign_extend(AV_RL16(avc->extradata + 16), 16);
00059 channel_map = AV_RL8 (avc->extradata + 18);
00060 }
00061 if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + avc->channels) {
00062 nb_streams = avc->extradata[OPUS_HEAD_SIZE + 0];
00063 nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
00064 if (nb_streams + nb_coupled != avc->channels)
00065 av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
00066 mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
00067 } else {
00068 if (avc->channels > 2 || channel_map) {
00069 av_log(avc, AV_LOG_ERROR,
00070 "No channel mapping for %d channels.\n", avc->channels);
00071 return AVERROR(EINVAL);
00072 }
00073 nb_streams = 1;
00074 nb_coupled = avc->channels > 1;
00075 mapping = mapping_arr;
00076 }
00077
00078 if (avc->channels > 2 && avc->channels <= 8) {
00079 const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[avc->channels - 1];
00080 int ch;
00081
00082
00083 for (ch = 0; ch < avc->channels; ch++)
00084 mapping_arr[ch] = mapping[vorbis_offset[ch]];
00085 mapping = mapping_arr;
00086 }
00087
00088 opus->dec = opus_multistream_decoder_create(avc->sample_rate, avc->channels,
00089 nb_streams, nb_coupled,
00090 mapping, &ret);
00091 if (!opus->dec) {
00092 av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
00093 opus_strerror(ret));
00094 return ff_opus_error_to_averror(ret);
00095 }
00096
00097 #ifdef OPUS_SET_GAIN
00098 ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
00099 if (ret != OPUS_OK)
00100 av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
00101 opus_strerror(ret));
00102 #else
00103 {
00104 double gain_lin = pow(10, gain_db / (20.0 * 256));
00105 if (avc->sample_fmt == AV_SAMPLE_FMT_FLT)
00106 opus->gain.d = gain_lin;
00107 else
00108 opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX);
00109 }
00110 #endif
00111
00112 avc->internal->skip_samples = opus->pre_skip;
00113 avc->delay = 3840;
00114 avcodec_get_frame_defaults(&opus->frame);
00115 avc->coded_frame = &opus->frame;
00116 return 0;
00117 }
00118
00119 static av_cold int libopus_decode_close(AVCodecContext *avc)
00120 {
00121 struct libopus_context *opus = avc->priv_data;
00122
00123 opus_multistream_decoder_destroy(opus->dec);
00124 return 0;
00125 }
00126
00127 #define MAX_FRAME_SIZE (960 * 6)
00128
00129 static int libopus_decode(AVCodecContext *avc, void *frame,
00130 int *got_frame_ptr, AVPacket *pkt)
00131 {
00132 struct libopus_context *opus = avc->priv_data;
00133 int ret, nb_samples;
00134
00135 opus->frame.nb_samples = MAX_FRAME_SIZE;
00136 ret = ff_get_buffer(avc, &opus->frame);
00137 if (ret < 0) {
00138 av_log(avc, AV_LOG_ERROR, "get_buffer() failed\n");
00139 return ret;
00140 }
00141
00142 if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
00143 nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
00144 (opus_int16 *)opus->frame.data[0],
00145 opus->frame.nb_samples, 0);
00146 else
00147 nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
00148 (float *)opus->frame.data[0],
00149 opus->frame.nb_samples, 0);
00150
00151 if (nb_samples < 0) {
00152 av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
00153 opus_strerror(nb_samples));
00154 return ff_opus_error_to_averror(nb_samples);
00155 }
00156
00157 #ifndef OPUS_SET_GAIN
00158 {
00159 int i = avc->channels * nb_samples;
00160 if (avc->sample_fmt == AV_SAMPLE_FMT_FLT) {
00161 float *pcm = (float *)opus->frame.data[0];
00162 for (; i > 0; i--, pcm++)
00163 *pcm = av_clipf(*pcm * opus->gain.d, -1, 1);
00164 } else {
00165 int16_t *pcm = (int16_t *)opus->frame.data[0];
00166 for (; i > 0; i--, pcm++)
00167 *pcm = av_clip_int16(((int64_t)opus->gain.i * *pcm) >> 16);
00168 }
00169 }
00170 #endif
00171
00172 opus->frame.nb_samples = nb_samples;
00173 *(AVFrame *)frame = opus->frame;
00174 *got_frame_ptr = 1;
00175 return pkt->size;
00176 }
00177
00178 static void libopus_flush(AVCodecContext *avc)
00179 {
00180 struct libopus_context *opus = avc->priv_data;
00181
00182 opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
00183
00184
00185 avc->internal->skip_samples = opus->pre_skip;
00186 }
00187
00188 AVCodec ff_libopus_decoder = {
00189 .name = "libopus",
00190 .type = AVMEDIA_TYPE_AUDIO,
00191 .id = AV_CODEC_ID_OPUS,
00192 .priv_data_size = sizeof(struct libopus_context),
00193 .init = libopus_decode_init,
00194 .close = libopus_decode_close,
00195 .decode = libopus_decode,
00196 .flush = libopus_flush,
00197 .capabilities = CODEC_CAP_DR1,
00198 .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
00199 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
00200 AV_SAMPLE_FMT_S16,
00201 AV_SAMPLE_FMT_NONE },
00202 };