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"
33 typedef struct DCADecContext {
35 struct dcadec_context *ctx;
42 static void my_log_cb(int level, const char *file, int line,
43 const char *message, void *cbarg)
48 case DCADEC_LOG_ERROR:
49 av_level = AV_LOG_ERROR;
51 case DCADEC_LOG_WARNING:
52 av_level = AV_LOG_WARNING;
55 av_level = AV_LOG_INFO;
57 case DCADEC_LOG_VERBOSE:
58 av_level = AV_LOG_VERBOSE;
60 case DCADEC_LOG_DEBUG:
62 av_level = AV_LOG_DEBUG;
66 av_log(cbarg, av_level, "%s\n", message);
69 static int dcadec_decode_frame(AVCodecContext *avctx, void *data,
70 int *got_frame_ptr, AVPacket *avpkt)
72 DCADecContext *s = avctx->priv_data;
73 AVFrame *frame = data;
74 struct dcadec_exss_info *exss;
76 int **samples, nsamples, channel_mask, sample_rate, bits_per_sample, profile;
78 uint8_t *input = avpkt->data;
79 int input_size = avpkt->size;
81 /* convert bytestream syntax to RAW BE format if required */
83 av_log(avctx, AV_LOG_ERROR, "Input size too small\n");
84 return AVERROR_INVALIDDATA;
87 if (mrk != DCA_SYNCWORD_CORE_BE && mrk != DCA_SYNCWORD_SUBSTREAM) {
88 s->buffer = av_fast_realloc(s->buffer, &s->buffer_size, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
90 return AVERROR(ENOMEM);
92 for (i = 0, ret = AVERROR_INVALIDDATA; i < input_size - 3 && ret < 0; i++)
93 ret = avpriv_dca_convert_bitstream(input + i, input_size - i, s->buffer, s->buffer_size);
102 if ((ret = dcadec_context_parse(s->ctx, input, input_size)) < 0) {
103 av_log(avctx, AV_LOG_ERROR, "dcadec_context_parse() failed: %d (%s)\n", -ret, dcadec_strerror(ret));
104 return AVERROR_EXTERNAL;
106 if ((ret = dcadec_context_filter(s->ctx, &samples, &nsamples, &channel_mask,
107 &sample_rate, &bits_per_sample, &profile)) < 0) {
108 av_log(avctx, AV_LOG_ERROR, "dcadec_context_filter() failed: %d (%s)\n", -ret, dcadec_strerror(ret));
109 return AVERROR_EXTERNAL;
112 avctx->channels = av_get_channel_layout_nb_channels(channel_mask);
113 avctx->channel_layout = channel_mask;
114 avctx->sample_rate = sample_rate;
116 if (bits_per_sample == 16)
117 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
118 else if (bits_per_sample > 16 && bits_per_sample <= 24)
119 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
121 av_log(avctx, AV_LOG_ERROR, "Unsupported number of bits per sample: %d\n",
123 return AVERROR(ENOSYS);
126 avctx->bits_per_raw_sample = bits_per_sample;
129 case DCADEC_PROFILE_DS:
130 avctx->profile = FF_PROFILE_DTS;
132 case DCADEC_PROFILE_DS_96_24:
133 avctx->profile = FF_PROFILE_DTS_96_24;
135 case DCADEC_PROFILE_DS_ES:
136 avctx->profile = FF_PROFILE_DTS_ES;
138 case DCADEC_PROFILE_HD_HRA:
139 avctx->profile = FF_PROFILE_DTS_HD_HRA;
141 case DCADEC_PROFILE_HD_MA:
142 avctx->profile = FF_PROFILE_DTS_HD_MA;
144 case DCADEC_PROFILE_EXPRESS:
145 avctx->profile = FF_PROFILE_DTS_EXPRESS;
147 case DCADEC_PROFILE_UNKNOWN:
149 avctx->profile = FF_PROFILE_UNKNOWN;
153 /* bitrate is only meaningful if there are no HD extensions, as they distort the bitrate */
154 if (profile == DCADEC_PROFILE_DS || profile == DCADEC_PROFILE_DS_96_24 || profile == DCADEC_PROFILE_DS_ES) {
155 struct dcadec_core_info *info = dcadec_context_get_core_info(s->ctx);
156 avctx->bit_rate = info->bit_rate;
157 dcadec_context_free_core_info(info);
161 if (exss = dcadec_context_get_exss_info(s->ctx)) {
162 enum AVMatrixEncoding matrix_encoding = AV_MATRIX_ENCODING_NONE;
164 switch(exss->matrix_encoding) {
165 case DCADEC_MATRIX_ENCODING_SURROUND:
166 matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
168 case DCADEC_MATRIX_ENCODING_HEADPHONE:
169 matrix_encoding = AV_MATRIX_ENCODING_DOLBYHEADPHONE;
172 dcadec_context_free_exss_info(exss);
174 if (matrix_encoding != AV_MATRIX_ENCODING_NONE &&
175 (ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
179 frame->nb_samples = nsamples;
180 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
183 for (i = 0; i < avctx->channels; i++) {
184 if (frame->format == AV_SAMPLE_FMT_S16P) {
185 int16_t *plane = (int16_t *)frame->extended_data[i];
186 for (k = 0; k < nsamples; k++)
187 plane[k] = samples[i][k];
189 int32_t *plane = (int32_t *)frame->extended_data[i];
190 int shift = 32 - bits_per_sample;
191 for (k = 0; k < nsamples; k++)
192 plane[k] = samples[i][k] << shift;
201 static av_cold void dcadec_flush(AVCodecContext *avctx)
203 DCADecContext *s = avctx->priv_data;
204 dcadec_context_clear(s->ctx);
207 static av_cold int dcadec_close(AVCodecContext *avctx)
209 DCADecContext *s = avctx->priv_data;
211 dcadec_context_destroy(s->ctx);
214 av_freep(&s->buffer);
219 static av_cold int dcadec_init(AVCodecContext *avctx)
221 DCADecContext *s = avctx->priv_data;
224 /* Affects only lossy DTS profiles. DTS-HD MA is always bitexact */
225 if (avctx->flags & AV_CODEC_FLAG_BITEXACT)
226 flags |= DCADEC_FLAG_CORE_BIT_EXACT;
228 if (avctx->err_recognition & AV_EF_EXPLODE)
229 flags |= DCADEC_FLAG_STRICT;
231 if (avctx->request_channel_layout) {
232 switch (avctx->request_channel_layout) {
233 case AV_CH_LAYOUT_STEREO:
234 case AV_CH_LAYOUT_STEREO_DOWNMIX:
235 flags |= DCADEC_FLAG_KEEP_DMIX_2CH;
237 case AV_CH_LAYOUT_5POINT1:
238 flags |= DCADEC_FLAG_KEEP_DMIX_6CH;
240 case AV_CH_LAYOUT_NATIVE:
241 flags |= DCADEC_FLAG_NATIVE_LAYOUT;
244 av_log(avctx, AV_LOG_WARNING, "Invalid request_channel_layout\n");
250 flags |= DCADEC_FLAG_CORE_ONLY;
252 switch (s->lfe_filter) {
253 #if DCADEC_API_VERSION >= DCADEC_VERSION_CODE(0, 1, 0)
255 flags |= DCADEC_FLAG_CORE_LFE_IIR;
259 flags |= DCADEC_FLAG_CORE_LFE_FIR;
263 s->ctx = dcadec_context_create(flags);
265 return AVERROR(ENOMEM);
267 dcadec_context_set_log_cb(s->ctx, my_log_cb, avctx);
269 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
270 avctx->bits_per_raw_sample = 24;
275 #define OFFSET(x) offsetof(DCADecContext, x)
276 #define PARAM AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
278 static const AVOption dcadec_options[] = {
279 { "lfe_filter", "Lossy LFE channel interpolation filter", OFFSET(lfe_filter), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, PARAM, "lfe_filter" },
280 { "default", "Library default", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, PARAM, "lfe_filter" },
281 { "iir", "IIR filter", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, PARAM, "lfe_filter" },
282 { "fir", "FIR filter", 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, PARAM, "lfe_filter" },
283 { "core_only", "Decode core only without extensions", OFFSET(core_only), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, PARAM },
287 static const AVClass dcadec_class = {
288 .class_name = "libdcadec decoder",
289 .item_name = av_default_item_name,
290 .option = dcadec_options,
291 .version = LIBAVUTIL_VERSION_INT,
292 .category = AV_CLASS_CATEGORY_DECODER,
295 static const AVProfile profiles[] = {
296 { FF_PROFILE_DTS, "DTS" },
297 { FF_PROFILE_DTS_ES, "DTS-ES" },
298 { FF_PROFILE_DTS_96_24, "DTS 96/24" },
299 { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
300 { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
301 { FF_PROFILE_DTS_EXPRESS, "DTS Express" },
302 { FF_PROFILE_UNKNOWN },
305 AVCodec ff_libdcadec_decoder = {
307 .long_name = NULL_IF_CONFIG_SMALL("dcadec DCA decoder"),
308 .type = AVMEDIA_TYPE_AUDIO,
309 .id = AV_CODEC_ID_DTS,
310 .priv_data_size = sizeof(DCADecContext),
312 .decode = dcadec_decode_frame,
313 .close = dcadec_close,
314 .flush = dcadec_flush,
315 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
316 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_S16P,
317 AV_SAMPLE_FMT_NONE },
318 .priv_class = &dcadec_class,
319 .profiles = NULL_IF_CONFIG_SMALL(profiles),