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 #include "avcodec.h"
00025 #include "internal.h"
00026 #include "vorbis.h"
00027 #include "libavutil/avassert.h"
00028 #include "libavutil/intreadwrite.h"
00029
00030 struct libopus_context {
00031 OpusMSDecoder *dec;
00032 AVFrame frame;
00033 int pre_skip;
00034 #ifndef OPUS_SET_GAIN
00035 union { int i; double d; } gain;
00036 #endif
00037 };
00038
00039 static int ff_opus_error_to_averror(int err)
00040 {
00041 switch (err) {
00042 case OPUS_BAD_ARG: return AVERROR(EINVAL);
00043 case OPUS_BUFFER_TOO_SMALL: return AVERROR_BUFFER_TOO_SMALL;
00044 case OPUS_INTERNAL_ERROR: return AVERROR(EFAULT);
00045 case OPUS_INVALID_PACKET: return AVERROR_INVALIDDATA;
00046 case OPUS_UNIMPLEMENTED: return AVERROR(ENOSYS);
00047 case OPUS_INVALID_STATE: return AVERROR_EXTERNAL;
00048 case OPUS_ALLOC_FAIL: return AVERROR(ENOMEM);
00049 default: return AVERROR(EINVAL);
00050 }
00051 }
00052
00053 static inline void reorder(uint8_t *data, unsigned channels, unsigned bps,
00054 unsigned samples, const uint8_t *map)
00055 {
00056 uint8_t tmp[8 * 4];
00057 unsigned i;
00058
00059 av_assert1(channels * bps <= sizeof(tmp));
00060 for (; samples > 0; samples--) {
00061 for (i = 0; i < channels; i++)
00062 memcpy(tmp + bps * i, data + bps * map[i], bps);
00063 memcpy(data, tmp, bps * channels);
00064 data += bps * channels;
00065 }
00066 }
00067
00068 #define OPUS_HEAD_SIZE 19
00069
00070 static av_cold int libopus_dec_init(AVCodecContext *avc)
00071 {
00072 struct libopus_context *opus = avc->priv_data;
00073 int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled;
00074 uint8_t mapping_stereo[] = { 0, 1 }, *mapping;
00075
00076 avc->sample_rate = 48000;
00077 avc->sample_fmt = avc->request_sample_fmt == AV_SAMPLE_FMT_FLT ?
00078 AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S16;
00079 avc->channel_layout = avc->channels > 8 ? 0 :
00080 ff_vorbis_channel_layouts[avc->channels - 1];
00081
00082 if (avc->extradata_size >= OPUS_HEAD_SIZE) {
00083 opus->pre_skip = AV_RL16(avc->extradata + 10);
00084 gain_db = AV_RL16(avc->extradata + 16);
00085 channel_map = AV_RL8 (avc->extradata + 18);
00086 gain_db -= (gain_db & 0x8000) << 1;
00087 }
00088 if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + avc->channels) {
00089 nb_streams = avc->extradata[OPUS_HEAD_SIZE + 0];
00090 nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
00091 if (nb_streams + nb_coupled != avc->channels)
00092 av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
00093 mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
00094 } else {
00095 if (avc->channels > 2 || channel_map) {
00096 av_log(avc, AV_LOG_ERROR,
00097 "No channel mapping for %d channels.\n", avc->channels);
00098 return AVERROR(EINVAL);
00099 }
00100 nb_streams = 1;
00101 nb_coupled = avc->channels > 1;
00102 mapping = mapping_stereo;
00103 }
00104
00105 opus->dec = opus_multistream_decoder_create(
00106 avc->sample_rate, avc->channels,
00107 nb_streams, nb_coupled, mapping, &ret);
00108 if (!opus->dec) {
00109 av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
00110 opus_strerror(ret));
00111 return ff_opus_error_to_averror(ret);
00112 }
00113
00114 #ifdef OPUS_SET_GAIN
00115 ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
00116 if (ret != OPUS_OK)
00117 av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
00118 opus_strerror(ret));
00119 #else
00120 {
00121 double gain_lin = pow(10, gain_db / (20.0 * 256));
00122 if (avc->sample_fmt == AV_SAMPLE_FMT_FLT)
00123 opus->gain.d = gain_lin;
00124 else
00125 opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX);
00126 }
00127 #endif
00128
00129 avc->internal->skip_samples = opus->pre_skip;
00130 avcodec_get_frame_defaults(&opus->frame);
00131 avc->coded_frame = &opus->frame;
00132 return 0;
00133 }
00134
00135 static av_cold int libopus_dec_close(AVCodecContext *avc)
00136 {
00137 struct libopus_context *opus = avc->priv_data;
00138
00139 opus_multistream_decoder_destroy(opus->dec);
00140 return 0;
00141 }
00142
00143 #define MAX_FRAME_SIZE (960*6)
00144
00145 static int libopus_dec_decode(AVCodecContext *avc, void *frame,
00146 int *got_frame_ptr, AVPacket *pkt)
00147 {
00148 struct libopus_context *opus = avc->priv_data;
00149 int ret, nb_samples;
00150
00151 opus->frame.nb_samples = MAX_FRAME_SIZE;
00152 ret = avc->get_buffer(avc, &opus->frame);
00153 if (ret < 0) {
00154 av_log(avc, AV_LOG_ERROR, "get_buffer() failed\n");
00155 return ret;
00156 }
00157
00158 nb_samples = avc->sample_fmt == AV_SAMPLE_FMT_S16 ?
00159 opus_multistream_decode (opus->dec, pkt->data, pkt->size,
00160 (void *)opus->frame.data[0],
00161 opus->frame.nb_samples, 0) :
00162 opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
00163 (void *)opus->frame.data[0],
00164 opus->frame.nb_samples, 0);
00165 if (nb_samples < 0) {
00166 av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
00167 opus_strerror(nb_samples));
00168 return ff_opus_error_to_averror(nb_samples);
00169 }
00170
00171 if (avc->channels > 3 && avc->channels <= 8) {
00172 const uint8_t *m = ff_vorbis_channel_layout_offsets[avc->channels - 1];
00173 if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
00174 reorder(opus->frame.data[0], avc->channels, 2, nb_samples, m);
00175 else
00176 reorder(opus->frame.data[0], avc->channels, 4, nb_samples, m);
00177 }
00178
00179 #ifndef OPUS_SET_GAIN
00180 {
00181 int i = avc->channels * nb_samples;
00182 if (avc->sample_fmt == AV_SAMPLE_FMT_FLT) {
00183 float *pcm = (float *)opus->frame.data[0];
00184 for (; i > 0; i--, pcm++)
00185 *pcm = av_clipf(*pcm * opus->gain.d, -1, 1);
00186 } else {
00187 int16_t *pcm = (int16_t *)opus->frame.data[0];
00188 for (; i > 0; i--, pcm++)
00189 *pcm = av_clip_int16(((int64_t)opus->gain.i * *pcm) >> 16);
00190 }
00191 }
00192 #endif
00193
00194 opus->frame.nb_samples = nb_samples;
00195 *(AVFrame *)frame = opus->frame;
00196 *got_frame_ptr = 1;
00197 return pkt->size;
00198 }
00199
00200 static void libopus_dec_flush(AVCodecContext *avc)
00201 {
00202 struct libopus_context *opus = avc->priv_data;
00203
00204 opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
00205
00206
00207 avc->internal->skip_samples = opus->pre_skip;
00208 }
00209
00210 AVCodec ff_libopus_decoder = {
00211 .name = "libopus",
00212 .type = AVMEDIA_TYPE_AUDIO,
00213 .id = AV_CODEC_ID_OPUS,
00214 .priv_data_size = sizeof(struct libopus_context),
00215 .init = libopus_dec_init,
00216 .close = libopus_dec_close,
00217 .decode = libopus_dec_decode,
00218 .flush = libopus_dec_flush,
00219 .capabilities = CODEC_CAP_DR1,
00220 .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
00221 };