2 * Copyright (C) 2016 foo86
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/opt.h"
22 #include "libavutil/channel_layout.h"
26 #include "dca_syncwords.h"
29 #define MIN_PACKET_SIZE 16
30 #define MAX_PACKET_SIZE 0x104000
32 int ff_dca_set_channel_layout(AVCodecContext *avctx, int *ch_remap, int dca_mask)
34 static const uint8_t dca2wav_norm[28] = {
35 2, 0, 1, 9, 10, 3, 8, 4, 5, 9, 10, 6, 7, 12,
36 13, 14, 3, 6, 7, 11, 12, 14, 16, 15, 17, 8, 4, 5,
39 static const uint8_t dca2wav_wide[28] = {
40 2, 0, 1, 4, 5, 3, 8, 4, 5, 9, 10, 6, 7, 12,
41 13, 14, 3, 9, 10, 11, 12, 14, 16, 15, 17, 8, 4, 5,
44 int dca_ch, wav_ch, nchannels = 0;
46 if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
47 for (dca_ch = 0; dca_ch < DCA_SPEAKER_COUNT; dca_ch++)
48 if (dca_mask & (1U << dca_ch))
49 ch_remap[nchannels++] = dca_ch;
50 avctx->channel_layout = dca_mask;
54 const uint8_t *dca2wav;
55 if (dca_mask == DCA_SPEAKER_LAYOUT_7POINT0_WIDE ||
56 dca_mask == DCA_SPEAKER_LAYOUT_7POINT1_WIDE)
57 dca2wav = dca2wav_wide;
59 dca2wav = dca2wav_norm;
60 for (dca_ch = 0; dca_ch < 28; dca_ch++) {
61 if (dca_mask & (1 << dca_ch)) {
62 wav_ch = dca2wav[dca_ch];
63 if (!(wav_mask & (1 << wav_ch))) {
64 wav_map[wav_ch] = dca_ch;
65 wav_mask |= 1 << wav_ch;
69 for (wav_ch = 0; wav_ch < 18; wav_ch++)
70 if (wav_mask & (1 << wav_ch))
71 ch_remap[nchannels++] = wav_map[wav_ch];
72 avctx->channel_layout = wav_mask;
75 avctx->channels = nchannels;
79 static uint16_t crc16(const uint8_t *data, int size)
81 static const uint16_t crctab[16] = {
82 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
83 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
86 uint16_t res = 0xffff;
89 for (i = 0; i < size; i++) {
90 res = (res << 4) ^ crctab[(data[i] >> 4) ^ (res >> 12)];
91 res = (res << 4) ^ crctab[(data[i] & 15) ^ (res >> 12)];
97 int ff_dca_check_crc(GetBitContext *s, int p1, int p2)
99 if (((p1 | p2) & 7) || p1 < 0 || p2 > s->size_in_bits || p2 - p1 < 16)
101 if (crc16(s->buffer + p1 / 8, (p2 - p1) / 8))
106 void ff_dca_downmix_to_stereo_fixed(DCADSPContext *dcadsp, int32_t **samples,
107 int *coeff_l, int nsamples, int ch_mask)
109 int pos, spkr, max_spkr = av_log2(ch_mask);
110 int *coeff_r = coeff_l + av_popcount(ch_mask);
112 av_assert0(DCA_HAS_STEREO(ch_mask));
114 // Scale left and right channels
115 pos = (ch_mask & DCA_SPEAKER_MASK_C);
116 dcadsp->dmix_scale(samples[DCA_SPEAKER_L], coeff_l[pos ], nsamples);
117 dcadsp->dmix_scale(samples[DCA_SPEAKER_R], coeff_r[pos + 1], nsamples);
119 // Downmix remaining channels
120 for (spkr = 0; spkr <= max_spkr; spkr++) {
121 if (!(ch_mask & (1U << spkr)))
124 if (*coeff_l && spkr != DCA_SPEAKER_L)
125 dcadsp->dmix_add(samples[DCA_SPEAKER_L], samples[spkr],
128 if (*coeff_r && spkr != DCA_SPEAKER_R)
129 dcadsp->dmix_add(samples[DCA_SPEAKER_R], samples[spkr],
137 void ff_dca_downmix_to_stereo_float(AVFloatDSPContext *fdsp, float **samples,
138 int *coeff_l, int nsamples, int ch_mask)
140 int pos, spkr, max_spkr = av_log2(ch_mask);
141 int *coeff_r = coeff_l + av_popcount(ch_mask);
142 const float scale = 1.0f / (1 << 15);
144 av_assert0(DCA_HAS_STEREO(ch_mask));
146 // Scale left and right channels
147 pos = (ch_mask & DCA_SPEAKER_MASK_C);
148 fdsp->vector_fmul_scalar(samples[DCA_SPEAKER_L], samples[DCA_SPEAKER_L],
149 coeff_l[pos ] * scale, nsamples);
150 fdsp->vector_fmul_scalar(samples[DCA_SPEAKER_R], samples[DCA_SPEAKER_R],
151 coeff_r[pos + 1] * scale, nsamples);
153 // Downmix remaining channels
154 for (spkr = 0; spkr <= max_spkr; spkr++) {
155 if (!(ch_mask & (1U << spkr)))
158 if (*coeff_l && spkr != DCA_SPEAKER_L)
159 fdsp->vector_fmac_scalar(samples[DCA_SPEAKER_L], samples[spkr],
160 *coeff_l * scale, nsamples);
162 if (*coeff_r && spkr != DCA_SPEAKER_R)
163 fdsp->vector_fmac_scalar(samples[DCA_SPEAKER_R], samples[spkr],
164 *coeff_r * scale, nsamples);
171 static int convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst, int max_size)
173 switch (AV_RB32(src)) {
174 case DCA_SYNCWORD_CORE_BE:
175 case DCA_SYNCWORD_SUBSTREAM:
176 memcpy(dst, src, src_size);
178 case DCA_SYNCWORD_CORE_LE:
179 case DCA_SYNCWORD_CORE_14B_BE:
180 case DCA_SYNCWORD_CORE_14B_LE:
181 return avpriv_dca_convert_bitstream(src, src_size, dst, max_size);
183 return AVERROR_INVALIDDATA;
187 static int dcadec_decode_frame(AVCodecContext *avctx, void *data,
188 int *got_frame_ptr, AVPacket *avpkt)
190 DCAContext *s = avctx->priv_data;
191 AVFrame *frame = data;
192 uint8_t *input = avpkt->data;
193 int input_size = avpkt->size;
194 int i, ret, prev_packet = s->packet;
196 if (input_size < MIN_PACKET_SIZE || input_size > MAX_PACKET_SIZE) {
197 av_log(avctx, AV_LOG_ERROR, "Invalid packet size\n");
198 return AVERROR_INVALIDDATA;
201 av_fast_malloc(&s->buffer, &s->buffer_size,
202 FFALIGN(input_size, 4096) + DCA_BUFFER_PADDING_SIZE);
204 return AVERROR(ENOMEM);
206 for (i = 0, ret = AVERROR_INVALIDDATA; i < input_size - MIN_PACKET_SIZE + 1 && ret < 0; i++)
207 ret = convert_bitstream(input + i, input_size - i, s->buffer, s->buffer_size);
217 // Parse backward compatible core sub-stream
218 if (AV_RB32(input) == DCA_SYNCWORD_CORE_BE) {
221 if ((ret = ff_dca_core_parse(&s->core, input, input_size)) < 0) {
222 s->core_residual_valid = 0;
226 s->packet |= DCA_PACKET_CORE;
228 // EXXS data must be aligned on 4-byte boundary
229 frame_size = FFALIGN(s->core.frame_size, 4);
230 if (input_size - 4 > frame_size) {
232 input_size -= frame_size;
237 DCAExssAsset *asset = NULL;
239 // Parse extension sub-stream (EXSS)
240 if (AV_RB32(input) == DCA_SYNCWORD_SUBSTREAM) {
241 if ((ret = ff_dca_exss_parse(&s->exss, input, input_size)) < 0) {
242 if (avctx->err_recognition & AV_EF_EXPLODE)
245 s->packet |= DCA_PACKET_EXSS;
246 asset = &s->exss.assets[0];
250 // Parse XLL component in EXSS
251 if (asset && (asset->extension_mask & DCA_EXSS_XLL)) {
252 if ((ret = ff_dca_xll_parse(&s->xll, input, asset)) < 0) {
253 // Conceal XLL synchronization error
254 if (ret == AVERROR(EAGAIN)
255 && (prev_packet & DCA_PACKET_XLL)
256 && (s->packet & DCA_PACKET_CORE))
257 s->packet |= DCA_PACKET_XLL | DCA_PACKET_RECOVERY;
258 else if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
261 s->packet |= DCA_PACKET_XLL;
265 // Parse core extensions in EXSS or backward compatible core sub-stream
266 if ((s->packet & DCA_PACKET_CORE)
267 && (ret = ff_dca_core_parse_exss(&s->core, input, asset)) < 0)
272 if (s->packet & DCA_PACKET_XLL) {
273 if (s->packet & DCA_PACKET_CORE) {
276 // Enable X96 synthesis if needed
277 if (s->xll.chset[0].freq == 96000 && s->core.sample_rate == 48000)
280 if ((ret = ff_dca_core_filter_fixed(&s->core, x96_synth)) < 0) {
281 s->core_residual_valid = 0;
285 // Force lossy downmixed output on the first core frame filtered.
286 // This prevents audible clicks when seeking and is consistent with
287 // what reference decoder does when there are multiple channel sets.
288 if (!s->core_residual_valid) {
289 if (s->xll.nreschsets > 0 && s->xll.nchsets > 1)
290 s->packet |= DCA_PACKET_RECOVERY;
291 s->core_residual_valid = 1;
295 if ((ret = ff_dca_xll_filter_frame(&s->xll, frame)) < 0) {
296 // Fall back to core unless hard error
297 if (!(s->packet & DCA_PACKET_CORE))
299 if (ret != AVERROR_INVALIDDATA || (avctx->err_recognition & AV_EF_EXPLODE))
301 if ((ret = ff_dca_core_filter_frame(&s->core, frame)) < 0) {
302 s->core_residual_valid = 0;
306 } else if (s->packet & DCA_PACKET_CORE) {
307 if ((ret = ff_dca_core_filter_frame(&s->core, frame)) < 0) {
308 s->core_residual_valid = 0;
311 s->core_residual_valid = !!(s->core.filter_mode & DCA_FILTER_MODE_FIXED);
313 return AVERROR_INVALIDDATA;
321 static av_cold void dcadec_flush(AVCodecContext *avctx)
323 DCAContext *s = avctx->priv_data;
325 ff_dca_core_flush(&s->core);
326 ff_dca_xll_flush(&s->xll);
328 s->core_residual_valid = 0;
331 static av_cold int dcadec_close(AVCodecContext *avctx)
333 DCAContext *s = avctx->priv_data;
335 ff_dca_core_close(&s->core);
336 ff_dca_xll_close(&s->xll);
338 av_freep(&s->buffer);
344 static av_cold int dcadec_init(AVCodecContext *avctx)
346 DCAContext *s = avctx->priv_data;
349 s->core.avctx = avctx;
350 s->exss.avctx = avctx;
351 s->xll.avctx = avctx;
353 if (ff_dca_core_init(&s->core) < 0)
354 return AVERROR(ENOMEM);
356 ff_dcadsp_init(&s->dcadsp);
357 s->core.dcadsp = &s->dcadsp;
358 s->xll.dcadsp = &s->dcadsp;
360 switch (avctx->request_channel_layout & ~AV_CH_LAYOUT_NATIVE) {
362 s->request_channel_layout = 0;
364 case AV_CH_LAYOUT_STEREO:
365 case AV_CH_LAYOUT_STEREO_DOWNMIX:
366 s->request_channel_layout = DCA_SPEAKER_LAYOUT_STEREO;
368 case AV_CH_LAYOUT_5POINT0:
369 s->request_channel_layout = DCA_SPEAKER_LAYOUT_5POINT0;
371 case AV_CH_LAYOUT_5POINT1:
372 s->request_channel_layout = DCA_SPEAKER_LAYOUT_5POINT1;
375 av_log(avctx, AV_LOG_WARNING, "Invalid request_channel_layout\n");
379 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
380 avctx->bits_per_raw_sample = 24;
385 #define OFFSET(x) offsetof(DCAContext, x)
386 #define PARAM AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
388 static const AVOption dcadec_options[] = {
389 { "core_only", "Decode core only without extensions", OFFSET(core_only), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, PARAM },
393 static const AVClass dcadec_class = {
394 .class_name = "DCA decoder",
395 .item_name = av_default_item_name,
396 .option = dcadec_options,
397 .version = LIBAVUTIL_VERSION_INT,
398 .category = AV_CLASS_CATEGORY_DECODER,
401 AVCodec ff_dca_decoder = {
403 .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
404 .type = AVMEDIA_TYPE_AUDIO,
405 .id = AV_CODEC_ID_DTS,
406 .priv_data_size = sizeof(DCAContext),
408 .decode = dcadec_decode_frame,
409 .close = dcadec_close,
410 .flush = dcadec_flush,
411 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
412 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
413 AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE },
414 .priv_class = &dcadec_class,
415 .profiles = NULL_IF_CONFIG_SMALL(ff_dca_profiles),
416 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,