2 * libdcadec decoder wrapper
3 * Copyright (C) 2015 Hendrik Leppkes
5 * This file is part of FFmpeg.
7 * FFmpeg 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.
12 * FFmpeg 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <libdcadec/dca_context.h>
24 #include "libavutil/channel_layout.h"
25 #include "libavutil/common.h"
26 #include "libavutil/opt.h"
30 #include "dca_syncwords.h"
34 typedef struct DCADecContext {
36 struct dcadec_context *ctx;
43 static void my_log_cb(int level, const char *file, int line,
44 const char *message, void *cbarg)
49 case DCADEC_LOG_ERROR:
50 av_level = AV_LOG_ERROR;
52 case DCADEC_LOG_WARNING:
53 av_level = AV_LOG_WARNING;
56 av_level = AV_LOG_INFO;
58 case DCADEC_LOG_VERBOSE:
59 av_level = AV_LOG_VERBOSE;
61 case DCADEC_LOG_DEBUG:
63 av_level = AV_LOG_DEBUG;
67 av_log(cbarg, av_level, "%s\n", message);
70 static int dcadec_decode_frame(AVCodecContext *avctx, void *data,
71 int *got_frame_ptr, AVPacket *avpkt)
73 DCADecContext *s = avctx->priv_data;
74 AVFrame *frame = data;
75 struct dcadec_exss_info *exss;
77 int **samples, nsamples, channel_mask, sample_rate, bits_per_sample, profile;
79 uint8_t *input = avpkt->data;
80 int input_size = avpkt->size;
82 /* convert bytestream syntax to RAW BE format if required */
84 av_log(avctx, AV_LOG_ERROR, "Input size too small\n");
85 return AVERROR_INVALIDDATA;
88 if (mrk != DCA_SYNCWORD_CORE_BE && mrk != DCA_SYNCWORD_SUBSTREAM) {
89 s->buffer = av_fast_realloc(s->buffer, &s->buffer_size, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
91 return AVERROR(ENOMEM);
93 for (i = 0, ret = AVERROR_INVALIDDATA; i < input_size - 3 && ret < 0; i++)
94 ret = avpriv_dca_convert_bitstream(input + i, input_size - i, s->buffer, s->buffer_size);
103 if ((ret = dcadec_context_parse(s->ctx, input, input_size)) < 0) {
104 av_log(avctx, AV_LOG_ERROR, "dcadec_context_parse() failed: %d (%s)\n", -ret, dcadec_strerror(ret));
105 return AVERROR_EXTERNAL;
107 if ((ret = dcadec_context_filter(s->ctx, &samples, &nsamples, &channel_mask,
108 &sample_rate, &bits_per_sample, &profile)) < 0) {
109 av_log(avctx, AV_LOG_ERROR, "dcadec_context_filter() failed: %d (%s)\n", -ret, dcadec_strerror(ret));
110 return AVERROR_EXTERNAL;
113 avctx->channels = av_get_channel_layout_nb_channels(channel_mask);
114 avctx->channel_layout = channel_mask;
115 avctx->sample_rate = sample_rate;
117 if (bits_per_sample == 16)
118 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
119 else if (bits_per_sample > 16 && bits_per_sample <= 24)
120 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
122 av_log(avctx, AV_LOG_ERROR, "Unsupported number of bits per sample: %d\n",
124 return AVERROR(ENOSYS);
127 avctx->bits_per_raw_sample = bits_per_sample;
130 case DCADEC_PROFILE_DS:
131 avctx->profile = FF_PROFILE_DTS;
133 case DCADEC_PROFILE_DS_96_24:
134 avctx->profile = FF_PROFILE_DTS_96_24;
136 case DCADEC_PROFILE_DS_ES:
137 avctx->profile = FF_PROFILE_DTS_ES;
139 case DCADEC_PROFILE_HD_HRA:
140 avctx->profile = FF_PROFILE_DTS_HD_HRA;
142 case DCADEC_PROFILE_HD_MA:
143 avctx->profile = FF_PROFILE_DTS_HD_MA;
145 case DCADEC_PROFILE_EXPRESS:
146 avctx->profile = FF_PROFILE_DTS_EXPRESS;
148 case DCADEC_PROFILE_UNKNOWN:
150 avctx->profile = FF_PROFILE_UNKNOWN;
154 /* bitrate is only meaningful if there are no HD extensions, as they distort the bitrate */
155 if (profile == DCADEC_PROFILE_DS || profile == DCADEC_PROFILE_DS_96_24 || profile == DCADEC_PROFILE_DS_ES) {
156 struct dcadec_core_info *info = dcadec_context_get_core_info(s->ctx);
157 avctx->bit_rate = info->bit_rate;
158 dcadec_context_free_core_info(info);
162 if (exss = dcadec_context_get_exss_info(s->ctx)) {
163 enum AVMatrixEncoding matrix_encoding = AV_MATRIX_ENCODING_NONE;
165 switch(exss->matrix_encoding) {
166 case DCADEC_MATRIX_ENCODING_SURROUND:
167 matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
169 case DCADEC_MATRIX_ENCODING_HEADPHONE:
170 matrix_encoding = AV_MATRIX_ENCODING_DOLBYHEADPHONE;
173 dcadec_context_free_exss_info(exss);
175 if (matrix_encoding != AV_MATRIX_ENCODING_NONE &&
176 (ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
180 frame->nb_samples = nsamples;
181 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
184 for (i = 0; i < avctx->channels; i++) {
185 if (frame->format == AV_SAMPLE_FMT_S16P) {
186 int16_t *plane = (int16_t *)frame->extended_data[i];
187 for (k = 0; k < nsamples; k++)
188 plane[k] = samples[i][k];
190 int32_t *plane = (int32_t *)frame->extended_data[i];
191 int shift = 32 - bits_per_sample;
192 for (k = 0; k < nsamples; k++)
193 plane[k] = samples[i][k] << shift;
202 static av_cold void dcadec_flush(AVCodecContext *avctx)
204 DCADecContext *s = avctx->priv_data;
205 dcadec_context_clear(s->ctx);
208 static av_cold int dcadec_close(AVCodecContext *avctx)
210 DCADecContext *s = avctx->priv_data;
212 dcadec_context_destroy(s->ctx);
215 av_freep(&s->buffer);
220 static av_cold int dcadec_init(AVCodecContext *avctx)
222 DCADecContext *s = avctx->priv_data;
225 /* Affects only lossy DTS profiles. DTS-HD MA is always bitexact */
226 if (avctx->flags & AV_CODEC_FLAG_BITEXACT)
227 flags |= DCADEC_FLAG_CORE_BIT_EXACT;
229 if (avctx->err_recognition & AV_EF_EXPLODE)
230 flags |= DCADEC_FLAG_STRICT;
232 if (avctx->request_channel_layout) {
233 switch (avctx->request_channel_layout) {
234 case AV_CH_LAYOUT_STEREO:
235 case AV_CH_LAYOUT_STEREO_DOWNMIX:
236 flags |= DCADEC_FLAG_KEEP_DMIX_2CH;
238 case AV_CH_LAYOUT_5POINT1:
239 flags |= DCADEC_FLAG_KEEP_DMIX_6CH;
241 case AV_CH_LAYOUT_NATIVE:
242 flags |= DCADEC_FLAG_NATIVE_LAYOUT;
245 av_log(avctx, AV_LOG_WARNING, "Invalid request_channel_layout\n");
251 flags |= DCADEC_FLAG_CORE_ONLY;
253 switch (s->lfe_filter) {
254 #if DCADEC_API_VERSION >= DCADEC_VERSION_CODE(0, 1, 0)
256 flags |= DCADEC_FLAG_CORE_LFE_IIR;
260 flags |= DCADEC_FLAG_CORE_LFE_FIR;
264 s->ctx = dcadec_context_create(flags);
266 return AVERROR(ENOMEM);
268 dcadec_context_set_log_cb(s->ctx, my_log_cb, avctx);
270 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
271 avctx->bits_per_raw_sample = 24;
276 #define OFFSET(x) offsetof(DCADecContext, x)
277 #define PARAM AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
279 static const AVOption dcadec_options[] = {
280 { "lfe_filter", "Lossy LFE channel interpolation filter", OFFSET(lfe_filter), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, PARAM, "lfe_filter" },
281 { "default", "Library default", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, PARAM, "lfe_filter" },
282 { "iir", "IIR filter", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, PARAM, "lfe_filter" },
283 { "fir", "FIR filter", 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, PARAM, "lfe_filter" },
284 { "core_only", "Decode core only without extensions", OFFSET(core_only), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, PARAM },
288 static const AVClass dcadec_class = {
289 .class_name = "libdcadec decoder",
290 .item_name = av_default_item_name,
291 .option = dcadec_options,
292 .version = LIBAVUTIL_VERSION_INT,
293 .category = AV_CLASS_CATEGORY_DECODER,
296 AVCodec ff_libdcadec_decoder = {
298 .long_name = NULL_IF_CONFIG_SMALL("dcadec DCA decoder"),
299 .type = AVMEDIA_TYPE_AUDIO,
300 .id = AV_CODEC_ID_DTS,
301 .priv_data_size = sizeof(DCADecContext),
303 .decode = dcadec_decode_frame,
304 .close = dcadec_close,
305 .flush = dcadec_flush,
306 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
307 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_S16P,
308 AV_SAMPLE_FMT_NONE },
309 .priv_class = &dcadec_class,
310 .profiles = NULL_IF_CONFIG_SMALL(ff_dca_profiles),