2 * DCA compatible decoder
3 * Copyright (C) 2004 Gildas Bazin
4 * Copyright (C) 2004 Benjamin Zores
5 * Copyright (C) 2006 Benjamin Larsson
6 * Copyright (C) 2007 Konstantin Shishkov
7 * Copyright (C) 2012 Paul B Mahol
8 * Copyright (C) 2014 Niels Möller
10 * This file is part of FFmpeg.
12 * FFmpeg is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * FFmpeg is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with FFmpeg; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 #include "libavutil/attributes.h"
32 #include "libavutil/channel_layout.h"
33 #include "libavutil/common.h"
34 #include "libavutil/float_dsp.h"
35 #include "libavutil/internal.h"
36 #include "libavutil/intreadwrite.h"
37 #include "libavutil/mathematics.h"
38 #include "libavutil/opt.h"
39 #include "libavutil/samplefmt.h"
43 #include "dca_syncwords.h"
48 #include "fmtconvert.h"
52 #include "synth_filter.h"
73 enum DCAXxchSpeakerMask {
74 DCA_XXCH_FRONT_CENTER = 0x0000001,
75 DCA_XXCH_FRONT_LEFT = 0x0000002,
76 DCA_XXCH_FRONT_RIGHT = 0x0000004,
77 DCA_XXCH_SIDE_REAR_LEFT = 0x0000008,
78 DCA_XXCH_SIDE_REAR_RIGHT = 0x0000010,
79 DCA_XXCH_LFE1 = 0x0000020,
80 DCA_XXCH_REAR_CENTER = 0x0000040,
81 DCA_XXCH_SURROUND_REAR_LEFT = 0x0000080,
82 DCA_XXCH_SURROUND_REAR_RIGHT = 0x0000100,
83 DCA_XXCH_SIDE_SURROUND_LEFT = 0x0000200,
84 DCA_XXCH_SIDE_SURROUND_RIGHT = 0x0000400,
85 DCA_XXCH_FRONT_CENTER_LEFT = 0x0000800,
86 DCA_XXCH_FRONT_CENTER_RIGHT = 0x0001000,
87 DCA_XXCH_FRONT_HIGH_LEFT = 0x0002000,
88 DCA_XXCH_FRONT_HIGH_CENTER = 0x0004000,
89 DCA_XXCH_FRONT_HIGH_RIGHT = 0x0008000,
90 DCA_XXCH_LFE2 = 0x0010000,
91 DCA_XXCH_SIDE_FRONT_LEFT = 0x0020000,
92 DCA_XXCH_SIDE_FRONT_RIGHT = 0x0040000,
93 DCA_XXCH_OVERHEAD = 0x0080000,
94 DCA_XXCH_SIDE_HIGH_LEFT = 0x0100000,
95 DCA_XXCH_SIDE_HIGH_RIGHT = 0x0200000,
96 DCA_XXCH_REAR_HIGH_CENTER = 0x0400000,
97 DCA_XXCH_REAR_HIGH_LEFT = 0x0800000,
98 DCA_XXCH_REAR_HIGH_RIGHT = 0x1000000,
99 DCA_XXCH_REAR_LOW_CENTER = 0x2000000,
100 DCA_XXCH_REAR_LOW_LEFT = 0x4000000,
101 DCA_XXCH_REAR_LOW_RIGHT = 0x8000000,
104 #define DCA_DOLBY 101 /* FIXME */
106 #define DCA_CHANNEL_BITS 6
107 #define DCA_CHANNEL_MASK 0x3F
111 #define HEADER_SIZE 14
113 #define DCA_NSYNCAUX 0x9A1105A0
116 /** Bit allocation */
117 typedef struct BitAlloc {
118 int offset; ///< code values offset
119 int maxbits[8]; ///< max bits in VLC
120 int wrap; ///< wrap for get_vlc2()
121 VLC vlc[8]; ///< actual codes
124 static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
125 static BitAlloc dca_tmode; ///< transition mode VLCs
126 static BitAlloc dca_scalefactor; ///< scalefactor VLCs
127 static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
129 static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba,
132 return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
136 static float dca_dmix_code(unsigned code);
138 static av_cold void dca_init_vlcs(void)
140 static int vlcs_initialized = 0;
142 static VLC_TYPE dca_table[23622][2];
144 if (vlcs_initialized)
147 dca_bitalloc_index.offset = 1;
148 dca_bitalloc_index.wrap = 2;
149 for (i = 0; i < 5; i++) {
150 dca_bitalloc_index.vlc[i].table = &dca_table[ff_dca_vlc_offs[i]];
151 dca_bitalloc_index.vlc[i].table_allocated = ff_dca_vlc_offs[i + 1] - ff_dca_vlc_offs[i];
152 init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
153 bitalloc_12_bits[i], 1, 1,
154 bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
156 dca_scalefactor.offset = -64;
157 dca_scalefactor.wrap = 2;
158 for (i = 0; i < 5; i++) {
159 dca_scalefactor.vlc[i].table = &dca_table[ff_dca_vlc_offs[i + 5]];
160 dca_scalefactor.vlc[i].table_allocated = ff_dca_vlc_offs[i + 6] - ff_dca_vlc_offs[i + 5];
161 init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
162 scales_bits[i], 1, 1,
163 scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
165 dca_tmode.offset = 0;
167 for (i = 0; i < 4; i++) {
168 dca_tmode.vlc[i].table = &dca_table[ff_dca_vlc_offs[i + 10]];
169 dca_tmode.vlc[i].table_allocated = ff_dca_vlc_offs[i + 11] - ff_dca_vlc_offs[i + 10];
170 init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
172 tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
175 for (i = 0; i < 10; i++)
176 for (j = 0; j < 7; j++) {
177 if (!bitalloc_codes[i][j])
179 dca_smpl_bitalloc[i + 1].offset = bitalloc_offsets[i];
180 dca_smpl_bitalloc[i + 1].wrap = 1 + (j > 4);
181 dca_smpl_bitalloc[i + 1].vlc[j].table = &dca_table[ff_dca_vlc_offs[c]];
182 dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = ff_dca_vlc_offs[c + 1] - ff_dca_vlc_offs[c];
184 init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
186 bitalloc_bits[i][j], 1, 1,
187 bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
190 vlcs_initialized = 1;
193 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
196 *dst++ = get_bits(gb, bits);
199 static inline int dca_xxch2index(DCAContext *s, int xxch_ch)
203 /* locate channel set containing the channel */
204 for (i = -1, base = 0, mask = (s->xxch_core_spkmask & ~DCA_XXCH_LFE1);
205 i <= s->xxch_chset && !(mask & xxch_ch); mask = s->xxch_spk_masks[++i])
206 base += av_popcount(mask);
208 return base + av_popcount(mask & (xxch_ch - 1));
211 static int dca_parse_audio_coding_header(DCAContext *s, int base_channel,
215 static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
216 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
217 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
218 int hdr_pos = 0, hdr_size = 0;
220 int this_chans, acc_mask;
221 int embedded_downmix;
225 /* xxch has arbitrary sized audio coding headers */
227 hdr_pos = get_bits_count(&s->gb);
228 hdr_size = get_bits(&s->gb, 7) + 1;
231 nchans = get_bits(&s->gb, 3) + 1;
232 if (xxch && nchans >= 3) {
233 av_log(s->avctx, AV_LOG_ERROR, "nchans %d is too large\n", nchans);
234 return AVERROR_INVALIDDATA;
235 } else if (nchans + base_channel > DCA_PRIM_CHANNELS_MAX) {
236 av_log(s->avctx, AV_LOG_ERROR, "channel sum %d + %d is too large\n", nchans, base_channel);
237 return AVERROR_INVALIDDATA;
240 s->total_channels = nchans + base_channel;
241 s->prim_channels = s->total_channels;
243 /* obtain speaker layout mask & downmix coefficients for XXCH */
245 acc_mask = s->xxch_core_spkmask;
247 this_chans = get_bits(&s->gb, s->xxch_nbits_spk_mask - 6) << 6;
248 s->xxch_spk_masks[s->xxch_chset] = this_chans;
249 s->xxch_chset_nch[s->xxch_chset] = nchans;
251 for (i = 0; i <= s->xxch_chset; i++)
252 acc_mask |= s->xxch_spk_masks[i];
254 /* check for downmixing information */
255 if (get_bits1(&s->gb)) {
256 embedded_downmix = get_bits1(&s->gb);
257 coeff = get_bits(&s->gb, 6);
259 if (coeff<1 || coeff>61) {
260 av_log(s->avctx, AV_LOG_ERROR, "6bit coeff %d is out of range\n", coeff);
261 return AVERROR_INVALIDDATA;
264 scale_factor = -1.0f / dca_dmix_code((coeff<<2)-3);
266 s->xxch_dmix_sf[s->xxch_chset] = scale_factor;
268 for (i = base_channel; i < s->prim_channels; i++) {
269 mask[i] = get_bits(&s->gb, s->xxch_nbits_spk_mask);
272 for (j = base_channel; j < s->prim_channels; j++) {
273 memset(s->xxch_dmix_coeff[j], 0, sizeof(s->xxch_dmix_coeff[0]));
274 s->xxch_dmix_embedded |= (embedded_downmix << j);
275 for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
276 if (mask[j] & (1 << i)) {
277 if ((1 << i) == DCA_XXCH_LFE1) {
278 av_log(s->avctx, AV_LOG_WARNING,
279 "DCA-XXCH: dmix to LFE1 not supported.\n");
283 coeff = get_bits(&s->gb, 7);
284 ichan = dca_xxch2index(s, 1 << i);
285 if ((coeff&63)<1 || (coeff&63)>61) {
286 av_log(s->avctx, AV_LOG_ERROR, "7bit coeff %d is out of range\n", coeff);
287 return AVERROR_INVALIDDATA;
289 s->xxch_dmix_coeff[j][ichan] = dca_dmix_code((coeff<<2)-3);
296 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
297 s->prim_channels = DCA_PRIM_CHANNELS_MAX;
299 for (i = base_channel; i < s->prim_channels; i++) {
300 s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
301 if (s->subband_activity[i] > DCA_SUBBANDS)
302 s->subband_activity[i] = DCA_SUBBANDS;
304 for (i = base_channel; i < s->prim_channels; i++) {
305 s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
306 if (s->vq_start_subband[i] > DCA_SUBBANDS)
307 s->vq_start_subband[i] = DCA_SUBBANDS;
309 get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
310 get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
311 get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
312 get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
314 /* Get codebooks quantization indexes */
316 memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
317 for (j = 1; j < 11; j++)
318 for (i = base_channel; i < s->prim_channels; i++)
319 s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
321 /* Get scale factor adjustment */
322 for (j = 0; j < 11; j++)
323 for (i = base_channel; i < s->prim_channels; i++)
324 s->scalefactor_adj[i][j] = 1;
326 for (j = 1; j < 11; j++)
327 for (i = base_channel; i < s->prim_channels; i++)
328 if (s->quant_index_huffman[i][j] < thr[j])
329 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
332 if (s->crc_present) {
333 /* Audio header CRC check */
334 get_bits(&s->gb, 16);
337 /* Skip to the end of the header, also ignore CRC if present */
338 i = get_bits_count(&s->gb);
339 if (hdr_pos + 8 * hdr_size > i)
340 skip_bits_long(&s->gb, hdr_pos + 8 * hdr_size - i);
343 s->current_subframe = 0;
344 s->current_subsubframe = 0;
349 static int dca_parse_frame_header(DCAContext *s)
351 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
354 skip_bits_long(&s->gb, 32);
357 s->frame_type = get_bits(&s->gb, 1);
358 s->samples_deficit = get_bits(&s->gb, 5) + 1;
359 s->crc_present = get_bits(&s->gb, 1);
360 s->sample_blocks = get_bits(&s->gb, 7) + 1;
361 s->frame_size = get_bits(&s->gb, 14) + 1;
362 if (s->frame_size < 95)
363 return AVERROR_INVALIDDATA;
364 s->amode = get_bits(&s->gb, 6);
365 s->sample_rate = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
367 return AVERROR_INVALIDDATA;
368 s->bit_rate_index = get_bits(&s->gb, 5);
369 s->bit_rate = ff_dca_bit_rates[s->bit_rate_index];
371 return AVERROR_INVALIDDATA;
373 skip_bits1(&s->gb); // always 0 (reserved, cf. ETSI TS 102 114 V1.4.1)
374 s->dynrange = get_bits(&s->gb, 1);
375 s->timestamp = get_bits(&s->gb, 1);
376 s->aux_data = get_bits(&s->gb, 1);
377 s->hdcd = get_bits(&s->gb, 1);
378 s->ext_descr = get_bits(&s->gb, 3);
379 s->ext_coding = get_bits(&s->gb, 1);
380 s->aspf = get_bits(&s->gb, 1);
381 s->lfe = get_bits(&s->gb, 2);
382 s->predictor_history = get_bits(&s->gb, 1);
386 av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE value: %d\n", s->lfe);
387 return AVERROR_INVALIDDATA;
390 /* TODO: check CRC */
392 s->header_crc = get_bits(&s->gb, 16);
394 s->multirate_inter = get_bits(&s->gb, 1);
395 s->version = get_bits(&s->gb, 4);
396 s->copy_history = get_bits(&s->gb, 2);
397 s->source_pcm_res = get_bits(&s->gb, 3);
398 s->front_sum = get_bits(&s->gb, 1);
399 s->surround_sum = get_bits(&s->gb, 1);
400 s->dialog_norm = get_bits(&s->gb, 4);
402 /* FIXME: channels mixing levels */
403 s->output = s->amode;
405 s->output |= DCA_LFE;
407 /* Primary audio coding header */
408 s->subframes = get_bits(&s->gb, 4) + 1;
410 return dca_parse_audio_coding_header(s, 0, 0);
413 static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
416 /* huffman encoded */
417 value += get_bitalloc(gb, &dca_scalefactor, level);
418 value = av_clip(value, 0, (1 << log2range) - 1);
419 } else if (level < 8) {
420 if (level + 1 > log2range) {
421 skip_bits(gb, level + 1 - log2range);
422 value = get_bits(gb, log2range);
424 value = get_bits(gb, level + 1);
430 static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
432 /* Primary audio coding side information */
435 if (get_bits_left(&s->gb) < 0)
436 return AVERROR_INVALIDDATA;
439 s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
440 if (block_index + s->subsubframes[s->current_subframe] > s->sample_blocks/8) {
441 s->subsubframes[s->current_subframe] = 1;
442 return AVERROR_INVALIDDATA;
444 s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
447 for (j = base_channel; j < s->prim_channels; j++) {
448 for (k = 0; k < s->subband_activity[j]; k++)
449 s->prediction_mode[j][k] = get_bits(&s->gb, 1);
452 /* Get prediction codebook */
453 for (j = base_channel; j < s->prim_channels; j++) {
454 for (k = 0; k < s->subband_activity[j]; k++) {
455 if (s->prediction_mode[j][k] > 0) {
456 /* (Prediction coefficient VQ address) */
457 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
462 /* Bit allocation index */
463 for (j = base_channel; j < s->prim_channels; j++) {
464 for (k = 0; k < s->vq_start_subband[j]; k++) {
465 if (s->bitalloc_huffman[j] == 6)
466 s->bitalloc[j][k] = get_bits(&s->gb, 5);
467 else if (s->bitalloc_huffman[j] == 5)
468 s->bitalloc[j][k] = get_bits(&s->gb, 4);
469 else if (s->bitalloc_huffman[j] == 7) {
470 av_log(s->avctx, AV_LOG_ERROR,
471 "Invalid bit allocation index\n");
472 return AVERROR_INVALIDDATA;
475 get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
478 if (s->bitalloc[j][k] > 26) {
479 ff_dlog(s->avctx, "bitalloc index [%i][%i] too big (%i)\n",
480 j, k, s->bitalloc[j][k]);
481 return AVERROR_INVALIDDATA;
486 /* Transition mode */
487 for (j = base_channel; j < s->prim_channels; j++) {
488 for (k = 0; k < s->subband_activity[j]; k++) {
489 s->transition_mode[j][k] = 0;
490 if (s->subsubframes[s->current_subframe] > 1 &&
491 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
492 s->transition_mode[j][k] =
493 get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
498 if (get_bits_left(&s->gb) < 0)
499 return AVERROR_INVALIDDATA;
501 for (j = base_channel; j < s->prim_channels; j++) {
502 const uint32_t *scale_table;
503 int scale_sum, log_size;
505 memset(s->scale_factor[j], 0,
506 s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
508 if (s->scalefactor_huffman[j] == 6) {
509 scale_table = ff_dca_scale_factor_quant7;
512 scale_table = ff_dca_scale_factor_quant6;
516 /* When huffman coded, only the difference is encoded */
519 for (k = 0; k < s->subband_activity[j]; k++) {
520 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
521 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
522 s->scale_factor[j][k][0] = scale_table[scale_sum];
525 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
526 /* Get second scale factor */
527 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
528 s->scale_factor[j][k][1] = scale_table[scale_sum];
533 /* Joint subband scale factor codebook select */
534 for (j = base_channel; j < s->prim_channels; j++) {
535 /* Transmitted only if joint subband coding enabled */
536 if (s->joint_intensity[j] > 0)
537 s->joint_huff[j] = get_bits(&s->gb, 3);
540 if (get_bits_left(&s->gb) < 0)
541 return AVERROR_INVALIDDATA;
543 /* Scale factors for joint subband coding */
544 for (j = base_channel; j < s->prim_channels; j++) {
547 /* Transmitted only if joint subband coding enabled */
548 if (s->joint_intensity[j] > 0) {
550 source_channel = s->joint_intensity[j] - 1;
552 /* When huffman coded, only the difference is encoded
553 * (is this valid as well for joint scales ???) */
555 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
556 scale = get_scale(&s->gb, s->joint_huff[j], 64 /* bias */, 7);
557 s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
560 if (!(s->debug_flag & 0x02)) {
561 av_log(s->avctx, AV_LOG_DEBUG,
562 "Joint stereo coding not supported\n");
563 s->debug_flag |= 0x02;
568 /* Dynamic range coefficient */
569 if (!base_channel && s->dynrange)
570 s->dynrange_coef = get_bits(&s->gb, 8);
572 /* Side information CRC check word */
573 if (s->crc_present) {
574 get_bits(&s->gb, 16);
578 * Primary audio data arrays
581 /* VQ encoded high frequency subbands */
582 for (j = base_channel; j < s->prim_channels; j++)
583 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
584 /* 1 vector -> 32 samples */
585 s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
587 /* Low frequency effect data */
588 if (!base_channel && s->lfe) {
591 int lfe_samples = 2 * s->lfe * (4 + block_index);
592 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
595 for (j = lfe_samples; j < lfe_end_sample; j++) {
596 /* Signed 8 bits int */
597 s->lfe_data[j] = get_sbits(&s->gb, 8);
600 /* Scale factor index */
601 quant7 = get_bits(&s->gb, 8);
603 avpriv_request_sample(s->avctx, "LFEScaleIndex larger than 127");
604 return AVERROR_INVALIDDATA;
606 s->lfe_scale_factor = ff_dca_scale_factor_quant7[quant7];
608 /* Quantization step size * scale factor */
609 lfe_scale = 0.035 * s->lfe_scale_factor;
611 for (j = lfe_samples; j < lfe_end_sample; j++)
612 s->lfe_data[j] *= lfe_scale;
618 static void qmf_32_subbands(DCAContext *s, int chans,
619 float samples_in[32][8], float *samples_out,
622 const float *prCoeff;
624 int sb_act = s->subband_activity[chans];
626 scale *= sqrt(1 / 8.0);
629 if (!s->multirate_inter) /* Non-perfect reconstruction */
630 prCoeff = ff_dca_fir_32bands_nonperfect;
631 else /* Perfect reconstruction */
632 prCoeff = ff_dca_fir_32bands_perfect;
634 s->dcadsp.qmf_32_subbands(samples_in, sb_act, &s->synth, &s->imdct,
635 s->subband_fir_hist[chans],
636 &s->hist_index[chans],
637 s->subband_fir_noidea[chans], prCoeff,
638 samples_out, s->raXin, scale);
641 static QMF64_table *qmf64_precompute(void)
644 QMF64_table *table = av_malloc(sizeof(*table));
648 for (i = 0; i < 32; i++)
649 for (j = 0; j < 32; j++)
650 table->dct4_coeff[i][j] = cos((2 * i + 1) * (2 * j + 1) * M_PI / 128);
651 for (i = 0; i < 32; i++)
652 for (j = 0; j < 32; j++)
653 table->dct2_coeff[i][j] = cos((2 * i + 1) * j * M_PI / 64);
655 /* FIXME: Is the factor 0.125 = 1/8 right? */
656 for (i = 0; i < 32; i++)
657 table->rcos[i] = 0.125 / cos((2 * i + 1) * M_PI / 256);
658 for (i = 0; i < 32; i++)
659 table->rsin[i] = -0.125 / sin((2 * i + 1) * M_PI / 256);
664 /* FIXME: Totally unoptimized. Based on the reference code and
665 * http://multimedia.cx/mirror/dca-transform.pdf, with guessed tweaks
666 * for doubling the size. */
667 static void qmf_64_subbands(DCAContext *s, int chans, float samples_in[64][8],
668 float *samples_out, float scale)
672 float *raX = s->subband_fir_hist[chans];
673 float *raZ = s->subband_fir_noidea[chans];
674 unsigned i, j, k, subindex;
676 for (i = s->subband_activity[chans]; i < 64; i++)
678 for (subindex = 0; subindex < 8; subindex++) {
679 for (i = 0; i < s->subband_activity[chans]; i++)
680 raXin[i] = samples_in[i][subindex];
682 for (k = 0; k < 32; k++) {
684 for (i = 0; i < 32; i++)
685 A[k] += (raXin[2 * i] + raXin[2 * i + 1]) * s->qmf64_table->dct4_coeff[k][i];
687 for (k = 0; k < 32; k++) {
688 B[k] = raXin[0] * s->qmf64_table->dct2_coeff[k][0];
689 for (i = 1; i < 32; i++)
690 B[k] += (raXin[2 * i] + raXin[2 * i - 1]) * s->qmf64_table->dct2_coeff[k][i];
692 for (k = 0; k < 32; k++) {
693 raX[k] = s->qmf64_table->rcos[k] * (A[k] + B[k]);
694 raX[63 - k] = s->qmf64_table->rsin[k] * (A[k] - B[k]);
697 for (i = 0; i < 64; i++) {
699 for (j = 0; j < 1024; j += 128)
700 out += ff_dca_fir_64bands[j + i] * (raX[j + i] - raX[j + 63 - i]);
701 *samples_out++ = out * scale;
704 for (i = 0; i < 64; i++) {
706 for (j = 0; j < 1024; j += 128)
707 hist += ff_dca_fir_64bands[64 + j + i] * (-raX[i + j] - raX[j + 63 - i]);
712 /* FIXME: Make buffer circular, to avoid this move. */
713 memmove(raX + 64, raX, (1024 - 64) * sizeof(*raX));
717 static void lfe_interpolation_fir(DCAContext *s, const float *samples_in,
720 /* samples_in: An array holding decimated samples.
721 * Samples in current subframe starts from samples_in[0],
722 * while samples_in[-1], samples_in[-2], ..., stores samples
723 * from last subframe as history.
725 * samples_out: An array holding interpolated samples
729 const float *prCoeff;
732 /* Select decimation filter */
735 prCoeff = ff_dca_lfe_fir_128;
738 if (s->exss_ext_mask & DCA_EXT_EXSS_XLL)
739 prCoeff = ff_dca_lfe_xll_fir_64;
741 prCoeff = ff_dca_lfe_fir_64;
744 for (deciindex = 0; deciindex < 2 * s->lfe; deciindex++) {
745 s->dcadsp.lfe_fir[idx](samples_out, samples_in, prCoeff);
747 samples_out += 2 * 32 * (1 + idx);
751 /* downmixing routines */
752 #define MIX_REAR1(samples, s1, rs, coef) \
753 samples[0][i] += samples[s1][i] * coef[rs][0]; \
754 samples[1][i] += samples[s1][i] * coef[rs][1];
756 #define MIX_REAR2(samples, s1, s2, rs, coef) \
757 samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \
758 samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1];
760 #define MIX_FRONT3(samples, coef) \
764 samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
765 samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
767 #define DOWNMIX_TO_STEREO(op1, op2) \
768 for (i = 0; i < 256; i++) { \
773 static void dca_downmix(float **samples, int srcfmt, int lfe_present,
774 float coef[DCA_PRIM_CHANNELS_MAX + 1][2],
775 const int8_t *channel_mapping)
777 int c, l, r, sl, sr, s;
784 av_log(NULL, AV_LOG_ERROR, "Not implemented!\n");
788 case DCA_STEREO_TOTAL:
789 case DCA_STEREO_SUMDIFF:
792 c = channel_mapping[0];
793 l = channel_mapping[1];
794 r = channel_mapping[2];
795 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
798 s = channel_mapping[2];
799 DOWNMIX_TO_STEREO(MIX_REAR1(samples, s, 2, coef), );
802 c = channel_mapping[0];
803 l = channel_mapping[1];
804 r = channel_mapping[2];
805 s = channel_mapping[3];
806 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
807 MIX_REAR1(samples, s, 3, coef));
810 sl = channel_mapping[2];
811 sr = channel_mapping[3];
812 DOWNMIX_TO_STEREO(MIX_REAR2(samples, sl, sr, 2, coef), );
815 c = channel_mapping[0];
816 l = channel_mapping[1];
817 r = channel_mapping[2];
818 sl = channel_mapping[3];
819 sr = channel_mapping[4];
820 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
821 MIX_REAR2(samples, sl, sr, 3, coef));
825 int lf_buf = ff_dca_lfe_index[srcfmt];
826 int lf_idx = ff_dca_channels[srcfmt];
827 for (i = 0; i < 256; i++) {
828 samples[0][i] += samples[lf_buf][i] * coef[lf_idx][0];
829 samples[1][i] += samples[lf_buf][i] * coef[lf_idx][1];
834 #ifndef decode_blockcodes
835 /* Very compact version of the block code decoder that does not use table
836 * look-up but is slightly slower */
837 static int decode_blockcode(int code, int levels, int32_t *values)
840 int offset = (levels - 1) >> 1;
842 for (i = 0; i < 4; i++) {
843 int div = FASTDIV(code, levels);
844 values[i] = code - offset - div * levels;
851 static int decode_blockcodes(int code1, int code2, int levels, int32_t *values)
853 return decode_blockcode(code1, levels, values) |
854 decode_blockcode(code2, levels, values + 4);
858 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
859 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
861 static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
864 int subsubframe = s->current_subsubframe;
866 const float *quant_step_table;
869 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
870 LOCAL_ALIGNED_16(int32_t, block, [8 * DCA_SUBBANDS]);
876 /* Select quantization step size table */
877 if (s->bit_rate_index == 0x1f)
878 quant_step_table = ff_dca_lossless_quant_d;
880 quant_step_table = ff_dca_lossy_quant_d;
882 for (k = base_channel; k < s->prim_channels; k++) {
883 float rscale[DCA_SUBBANDS];
885 if (get_bits_left(&s->gb) < 0)
886 return AVERROR_INVALIDDATA;
888 for (l = 0; l < s->vq_start_subband[k]; l++) {
891 /* Select the mid-tread linear quantizer */
892 int abits = s->bitalloc[k][l];
894 float quant_step_size = quant_step_table[abits];
897 * Determine quantization index code book and its type
900 /* Select quantization index code book */
901 int sel = s->quant_index_huffman[k][abits];
904 * Extract bits from the bit stream
908 memset(block + 8 * l, 0, 8 * sizeof(block[0]));
910 /* Deal with transients */
911 int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
912 rscale[l] = quant_step_size * s->scale_factor[k][l][sfi] *
913 s->scalefactor_adj[k][sel];
915 if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
918 int block_code1, block_code2, size, levels, err;
920 size = abits_sizes[abits - 1];
921 levels = abits_levels[abits - 1];
923 block_code1 = get_bits(&s->gb, size);
924 block_code2 = get_bits(&s->gb, size);
925 err = decode_blockcodes(block_code1, block_code2,
926 levels, block + 8 * l);
928 av_log(s->avctx, AV_LOG_ERROR,
929 "ERROR: block code look-up failed\n");
930 return AVERROR_INVALIDDATA;
934 for (m = 0; m < 8; m++)
935 block[8 * l + m] = get_sbits(&s->gb, abits - 3);
939 for (m = 0; m < 8; m++)
940 block[8 * l + m] = get_bitalloc(&s->gb,
941 &dca_smpl_bitalloc[abits], sel);
946 s->fmt_conv.int32_to_float_fmul_array8(&s->fmt_conv, subband_samples[k][0],
947 block, rscale, 8 * s->vq_start_subband[k]);
949 for (l = 0; l < s->vq_start_subband[k]; l++) {
952 * Inverse ADPCM if in prediction mode
954 if (s->prediction_mode[k][l]) {
956 if (s->predictor_history)
957 subband_samples[k][l][0] += (ff_dca_adpcm_vb[s->prediction_vq[k][l]][0] *
958 s->subband_samples_hist[k][l][3] +
959 ff_dca_adpcm_vb[s->prediction_vq[k][l]][1] *
960 s->subband_samples_hist[k][l][2] +
961 ff_dca_adpcm_vb[s->prediction_vq[k][l]][2] *
962 s->subband_samples_hist[k][l][1] +
963 ff_dca_adpcm_vb[s->prediction_vq[k][l]][3] *
964 s->subband_samples_hist[k][l][0]) *
966 for (m = 1; m < 8; m++) {
967 float sum = ff_dca_adpcm_vb[s->prediction_vq[k][l]][0] *
968 subband_samples[k][l][m - 1];
969 for (n = 2; n <= 4; n++)
971 sum += ff_dca_adpcm_vb[s->prediction_vq[k][l]][n - 1] *
972 subband_samples[k][l][m - n];
973 else if (s->predictor_history)
974 sum += ff_dca_adpcm_vb[s->prediction_vq[k][l]][n - 1] *
975 s->subband_samples_hist[k][l][m - n + 4];
976 subband_samples[k][l][m] += sum * (1.0f / 8192);
982 * Decode VQ encoded high frequencies
984 if (s->subband_activity[k] > s->vq_start_subband[k]) {
985 if (!(s->debug_flag & 0x01)) {
986 av_log(s->avctx, AV_LOG_DEBUG,
987 "Stream with high frequencies VQ coding\n");
988 s->debug_flag |= 0x01;
990 s->dcadsp.decode_hf(subband_samples[k], s->high_freq_vq[k],
991 ff_dca_high_freq_vq, subsubframe * 8,
992 s->scale_factor[k], s->vq_start_subband[k],
993 s->subband_activity[k]);
997 /* Check for DSYNC after subsubframe */
998 if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
999 if (get_bits(&s->gb, 16) != 0xFFFF) {
1000 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1001 return AVERROR_INVALIDDATA;
1005 /* Backup predictor history for adpcm */
1006 for (k = base_channel; k < s->prim_channels; k++)
1007 for (l = 0; l < s->vq_start_subband[k]; l++)
1008 AV_COPY128(s->subband_samples_hist[k][l], &subband_samples[k][l][4]);
1013 static int dca_filter_channels(DCAContext *s, int block_index, int upsample)
1015 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1019 if (!s->qmf64_table) {
1020 s->qmf64_table = qmf64_precompute();
1021 if (!s->qmf64_table)
1022 return AVERROR(ENOMEM);
1025 /* 64 subbands QMF */
1026 for (k = 0; k < s->prim_channels; k++) {
1027 if (s->channel_order_tab[k] >= 0)
1028 qmf_64_subbands(s, k, subband_samples[k],
1029 s->samples_chanptr[s->channel_order_tab[k]],
1030 /* Upsampling needs a factor 2 here. */
1034 /* 32 subbands QMF */
1035 for (k = 0; k < s->prim_channels; k++) {
1036 if (s->channel_order_tab[k] >= 0)
1037 qmf_32_subbands(s, k, subband_samples[k],
1038 s->samples_chanptr[s->channel_order_tab[k]],
1039 M_SQRT1_2 / 32768.0);
1043 /* Generate LFE samples for this subsubframe FIXME!!! */
1045 float *samples = s->samples_chanptr[s->lfe_index];
1046 lfe_interpolation_fir(s,
1047 s->lfe_data + 2 * s->lfe * (block_index + 4),
1051 /* Should apply the filter in Table 6-11 when upsampling. For
1052 * now, just duplicate. */
1053 for (i = 255; i > 0; i--) {
1055 samples[2 * i + 1] = samples[i];
1057 samples[1] = samples[0];
1061 /* FIXME: This downmixing is probably broken with upsample.
1062 * Probably totally broken also with XLL in general. */
1063 /* Downmixing to Stereo */
1064 if (s->prim_channels + !!s->lfe > 2 &&
1065 s->avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1066 dca_downmix(s->samples_chanptr, s->amode, !!s->lfe, s->downmix_coef,
1067 s->channel_order_tab);
1073 static int dca_subframe_footer(DCAContext *s, int base_channel)
1075 int in, out, aux_data_count, aux_data_end, reserved;
1079 * Unpack optional information
1082 /* presumably optional information only appears in the core? */
1083 if (!base_channel) {
1085 skip_bits_long(&s->gb, 32);
1088 aux_data_count = get_bits(&s->gb, 6);
1091 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1093 aux_data_end = 8 * aux_data_count + get_bits_count(&s->gb);
1095 if ((nsyncaux = get_bits_long(&s->gb, 32)) != DCA_NSYNCAUX) {
1096 av_log(s->avctx, AV_LOG_ERROR, "nSYNCAUX mismatch %#"PRIx32"\n",
1098 return AVERROR_INVALIDDATA;
1101 if (get_bits1(&s->gb)) { // bAUXTimeStampFlag
1102 avpriv_request_sample(s->avctx,
1103 "Auxiliary Decode Time Stamp Flag");
1105 skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 4);
1106 // 44 bits: nMSByte (8), nMarker (4), nLSByte (28), nMarker (4)
1107 skip_bits_long(&s->gb, 44);
1110 if ((s->core_downmix = get_bits1(&s->gb))) {
1111 int am = get_bits(&s->gb, 3);
1114 s->core_downmix_amode = DCA_MONO;
1117 s->core_downmix_amode = DCA_STEREO;
1120 s->core_downmix_amode = DCA_STEREO_TOTAL;
1123 s->core_downmix_amode = DCA_3F;
1126 s->core_downmix_amode = DCA_2F1R;
1129 s->core_downmix_amode = DCA_2F2R;
1132 s->core_downmix_amode = DCA_3F1R;
1135 av_log(s->avctx, AV_LOG_ERROR,
1136 "Invalid mode %d for embedded downmix coefficients\n",
1138 return AVERROR_INVALIDDATA;
1140 for (out = 0; out < ff_dca_channels[s->core_downmix_amode]; out++) {
1141 for (in = 0; in < s->prim_channels + !!s->lfe; in++) {
1142 uint16_t tmp = get_bits(&s->gb, 9);
1143 if ((tmp & 0xFF) > 241) {
1144 av_log(s->avctx, AV_LOG_ERROR,
1145 "Invalid downmix coefficient code %"PRIu16"\n",
1147 return AVERROR_INVALIDDATA;
1149 s->core_downmix_codes[in][out] = tmp;
1154 align_get_bits(&s->gb); // byte align
1155 skip_bits(&s->gb, 16); // nAUXCRC16
1157 // additional data (reserved, cf. ETSI TS 102 114 V1.4.1)
1158 if ((reserved = (aux_data_end - get_bits_count(&s->gb))) < 0) {
1159 av_log(s->avctx, AV_LOG_ERROR,
1160 "Overread auxiliary data by %d bits\n", -reserved);
1161 return AVERROR_INVALIDDATA;
1162 } else if (reserved) {
1163 avpriv_request_sample(s->avctx,
1164 "Core auxiliary data reserved content");
1165 skip_bits_long(&s->gb, reserved);
1169 if (s->crc_present && s->dynrange)
1170 get_bits(&s->gb, 16);
1177 * Decode a dca frame block
1179 * @param s pointer to the DCAContext
1182 static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
1187 if (s->current_subframe >= s->subframes) {
1188 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1189 s->current_subframe, s->subframes);
1190 return AVERROR_INVALIDDATA;
1193 if (!s->current_subsubframe) {
1194 /* Read subframe header */
1195 if ((ret = dca_subframe_header(s, base_channel, block_index)))
1199 /* Read subsubframe */
1200 if ((ret = dca_subsubframe(s, base_channel, block_index)))
1204 s->current_subsubframe++;
1205 if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
1206 s->current_subsubframe = 0;
1207 s->current_subframe++;
1209 if (s->current_subframe >= s->subframes) {
1210 /* Read subframe footer */
1211 if ((ret = dca_subframe_footer(s, base_channel)))
1218 int ff_dca_xbr_parse_frame(DCAContext *s)
1220 int scale_table_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS][2];
1221 int active_bands[DCA_CHSETS_MAX][DCA_CHSET_CHANS_MAX];
1222 int abits_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS];
1223 int anctemp[DCA_CHSET_CHANS_MAX];
1224 int chset_fsize[DCA_CHSETS_MAX];
1225 int n_xbr_ch[DCA_CHSETS_MAX];
1226 int hdr_size, num_chsets, xbr_tmode, hdr_pos;
1227 int i, j, k, l, chset, chan_base;
1229 av_log(s->avctx, AV_LOG_DEBUG, "DTS-XBR: decoding XBR extension\n");
1231 /* get bit position of sync header */
1232 hdr_pos = get_bits_count(&s->gb) - 32;
1234 hdr_size = get_bits(&s->gb, 6) + 1;
1235 num_chsets = get_bits(&s->gb, 2) + 1;
1237 for(i = 0; i < num_chsets; i++)
1238 chset_fsize[i] = get_bits(&s->gb, 14) + 1;
1240 xbr_tmode = get_bits1(&s->gb);
1242 for(i = 0; i < num_chsets; i++) {
1243 n_xbr_ch[i] = get_bits(&s->gb, 3) + 1;
1244 k = get_bits(&s->gb, 2) + 5;
1245 for(j = 0; j < n_xbr_ch[i]; j++) {
1246 active_bands[i][j] = get_bits(&s->gb, k) + 1;
1247 if (active_bands[i][j] > DCA_SUBBANDS) {
1248 av_log(s->avctx, AV_LOG_ERROR, "too many active subbands (%d)\n", active_bands[i][j]);
1249 return AVERROR_INVALIDDATA;
1254 /* skip to the end of the header */
1255 i = get_bits_count(&s->gb);
1256 if(hdr_pos + hdr_size * 8 > i)
1257 skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1259 /* loop over the channel data sets */
1260 /* only decode as many channels as we've decoded base data for */
1261 for(chset = 0, chan_base = 0;
1262 chset < num_chsets && chan_base + n_xbr_ch[chset] <= s->prim_channels;
1263 chan_base += n_xbr_ch[chset++]) {
1264 int start_posn = get_bits_count(&s->gb);
1265 int subsubframe = 0;
1268 /* loop over subframes */
1269 for (k = 0; k < (s->sample_blocks / 8); k++) {
1270 /* parse header if we're on first subsubframe of a block */
1271 if(subsubframe == 0) {
1272 /* Parse subframe header */
1273 for(i = 0; i < n_xbr_ch[chset]; i++) {
1274 anctemp[i] = get_bits(&s->gb, 2) + 2;
1277 for(i = 0; i < n_xbr_ch[chset]; i++) {
1278 get_array(&s->gb, abits_high[i], active_bands[chset][i], anctemp[i]);
1281 for(i = 0; i < n_xbr_ch[chset]; i++) {
1282 anctemp[i] = get_bits(&s->gb, 3);
1283 if(anctemp[i] < 1) {
1284 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: SYNC ERROR\n");
1285 return AVERROR_INVALIDDATA;
1289 /* generate scale factors */
1290 for(i = 0; i < n_xbr_ch[chset]; i++) {
1291 const uint32_t *scale_table;
1293 int scale_table_size;
1295 if (s->scalefactor_huffman[chan_base+i] == 6) {
1296 scale_table = ff_dca_scale_factor_quant7;
1297 scale_table_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7);
1299 scale_table = ff_dca_scale_factor_quant6;
1300 scale_table_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6);
1305 for(j = 0; j < active_bands[chset][i]; j++) {
1306 if(abits_high[i][j] > 0) {
1307 int index = get_bits(&s->gb, nbits);
1308 if (index >= scale_table_size) {
1309 av_log(s->avctx, AV_LOG_ERROR, "scale table index %d invalid\n", index);
1310 return AVERROR_INVALIDDATA;
1312 scale_table_high[i][j][0] = scale_table[index];
1314 if(xbr_tmode && s->transition_mode[i][j]) {
1315 int index = get_bits(&s->gb, nbits);
1316 if (index >= scale_table_size) {
1317 av_log(s->avctx, AV_LOG_ERROR, "scale table index %d invalid\n", index);
1318 return AVERROR_INVALIDDATA;
1320 scale_table_high[i][j][1] = scale_table[index];
1327 /* decode audio array for this block */
1328 for(i = 0; i < n_xbr_ch[chset]; i++) {
1329 for(j = 0; j < active_bands[chset][i]; j++) {
1330 const int xbr_abits = abits_high[i][j];
1331 const float quant_step_size = ff_dca_lossless_quant_d[xbr_abits];
1332 const int sfi = xbr_tmode && s->transition_mode[i][j] && subsubframe >= s->transition_mode[i][j];
1333 const float rscale = quant_step_size * scale_table_high[i][j][sfi];
1334 float *subband_samples = s->subband_samples[k][chan_base+i][j];
1341 get_array(&s->gb, block, 8, xbr_abits - 3);
1343 int block_code1, block_code2, size, levels, err;
1345 size = abits_sizes[xbr_abits - 1];
1346 levels = abits_levels[xbr_abits - 1];
1348 block_code1 = get_bits(&s->gb, size);
1349 block_code2 = get_bits(&s->gb, size);
1350 err = decode_blockcodes(block_code1, block_code2,
1353 av_log(s->avctx, AV_LOG_ERROR,
1354 "ERROR: DTS-XBR: block code look-up failed\n");
1355 return AVERROR_INVALIDDATA;
1359 /* scale & sum into subband */
1360 for(l = 0; l < 8; l++)
1361 subband_samples[l] += (float)block[l] * rscale;
1365 /* check DSYNC marker */
1366 if(s->aspf || subsubframe == s->subsubframes[subframe] - 1) {
1367 if(get_bits(&s->gb, 16) != 0xffff) {
1368 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: Didn't get subframe DSYNC\n");
1369 return AVERROR_INVALIDDATA;
1373 /* advance sub-sub-frame index */
1374 if(++subsubframe >= s->subsubframes[subframe]) {
1380 /* skip to next channel set */
1381 i = get_bits_count(&s->gb);
1382 if(start_posn + chset_fsize[chset] * 8 != i) {
1383 j = start_posn + chset_fsize[chset] * 8 - i;
1385 av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: end of channel set,"
1386 " skipping further than expected (%d bits)\n", j);
1387 skip_bits_long(&s->gb, j);
1395 /* parse initial header for XXCH and dump details */
1396 int ff_dca_xxch_decode_frame(DCAContext *s)
1398 int hdr_size, spkmsk_bits, num_chsets, core_spk, hdr_pos;
1399 int i, chset, base_channel, chstart, fsize[8];
1401 /* assume header word has already been parsed */
1402 hdr_pos = get_bits_count(&s->gb) - 32;
1403 hdr_size = get_bits(&s->gb, 6) + 1;
1404 /*chhdr_crc =*/ skip_bits1(&s->gb);
1405 spkmsk_bits = get_bits(&s->gb, 5) + 1;
1406 num_chsets = get_bits(&s->gb, 2) + 1;
1408 for (i = 0; i < num_chsets; i++)
1409 fsize[i] = get_bits(&s->gb, 14) + 1;
1411 core_spk = get_bits(&s->gb, spkmsk_bits);
1412 s->xxch_core_spkmask = core_spk;
1413 s->xxch_nbits_spk_mask = spkmsk_bits;
1414 s->xxch_dmix_embedded = 0;
1416 /* skip to the end of the header */
1417 i = get_bits_count(&s->gb);
1418 if (hdr_pos + hdr_size * 8 > i)
1419 skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1421 for (chset = 0; chset < num_chsets; chset++) {
1422 chstart = get_bits_count(&s->gb);
1423 base_channel = s->prim_channels;
1424 s->xxch_chset = chset;
1426 /* XXCH and Core headers differ, see 6.4.2 "XXCH Channel Set Header" vs.
1427 5.3.2 "Primary Audio Coding Header", DTS Spec 1.3.1 */
1428 dca_parse_audio_coding_header(s, base_channel, 1);
1430 /* decode channel data */
1431 for (i = 0; i < (s->sample_blocks / 8); i++) {
1432 if (dca_decode_block(s, base_channel, i)) {
1433 av_log(s->avctx, AV_LOG_ERROR,
1434 "Error decoding DTS-XXCH extension\n");
1439 /* skip to end of this section */
1440 i = get_bits_count(&s->gb);
1441 if (chstart + fsize[chset] * 8 > i)
1442 skip_bits_long(&s->gb, chstart + fsize[chset] * 8 - i);
1444 s->xxch_chset = num_chsets;
1449 static float dca_dmix_code(unsigned code)
1451 int sign = (code >> 8) - 1;
1453 return ((ff_dca_dmixtable[code] ^ sign) - sign) * (1.0 / (1 << 15));
1457 * Main frame decoding function
1458 * FIXME add arguments
1460 static int dca_decode_frame(AVCodecContext *avctx, void *data,
1461 int *got_frame_ptr, AVPacket *avpkt)
1463 AVFrame *frame = data;
1464 const uint8_t *buf = avpkt->data;
1465 int buf_size = avpkt->size;
1469 int num_core_channels = 0;
1471 float **samples_flt;
1474 DCAContext *s = avctx->priv_data;
1476 int channels, full_channels;
1487 s->exss_ext_mask = 0;
1490 s->dca_buffer_size = AVERROR_INVALIDDATA;
1491 for (i = 0; i < buf_size - 3 && s->dca_buffer_size == AVERROR_INVALIDDATA; i++)
1492 s->dca_buffer_size = avpriv_dca_convert_bitstream(buf + i, buf_size - i, s->dca_buffer,
1493 DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
1495 if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
1496 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
1497 return AVERROR_INVALIDDATA;
1500 if ((ret = dca_parse_frame_header(s)) < 0) {
1501 // seems like the frame is corrupt, try with the next one
1504 // set AVCodec values with parsed data
1505 avctx->sample_rate = s->sample_rate;
1507 s->profile = FF_PROFILE_DTS;
1509 for (i = 0; i < (s->sample_blocks / 8); i++) {
1510 if ((ret = dca_decode_block(s, 0, i))) {
1511 av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
1516 /* record number of core channels incase less than max channels are requested */
1517 num_core_channels = s->prim_channels;
1519 if (s->prim_channels + !!s->lfe > 2 &&
1520 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1521 /* Stereo downmix coefficients
1523 * The decoder can only downmix to 2-channel, so we need to ensure
1524 * embedded downmix coefficients are actually targeting 2-channel.
1526 if (s->core_downmix && (s->core_downmix_amode == DCA_STEREO ||
1527 s->core_downmix_amode == DCA_STEREO_TOTAL)) {
1528 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1529 /* Range checked earlier */
1530 s->downmix_coef[i][0] = dca_dmix_code(s->core_downmix_codes[i][0]);
1531 s->downmix_coef[i][1] = dca_dmix_code(s->core_downmix_codes[i][1]);
1533 s->output = s->core_downmix_amode;
1535 int am = s->amode & DCA_CHANNEL_MASK;
1536 if (am >= FF_ARRAY_ELEMS(ff_dca_default_coeffs)) {
1537 av_log(s->avctx, AV_LOG_ERROR,
1538 "Invalid channel mode %d\n", am);
1539 return AVERROR_INVALIDDATA;
1541 if (num_core_channels + !!s->lfe >
1542 FF_ARRAY_ELEMS(ff_dca_default_coeffs[0])) {
1543 avpriv_request_sample(s->avctx, "Downmixing %d channels",
1544 s->prim_channels + !!s->lfe);
1545 return AVERROR_PATCHWELCOME;
1547 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1548 s->downmix_coef[i][0] = ff_dca_default_coeffs[am][i][0];
1549 s->downmix_coef[i][1] = ff_dca_default_coeffs[am][i][1];
1552 ff_dlog(s->avctx, "Stereo downmix coeffs:\n");
1553 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1554 ff_dlog(s->avctx, "L, input channel %d = %f\n", i,
1555 s->downmix_coef[i][0]);
1556 ff_dlog(s->avctx, "R, input channel %d = %f\n", i,
1557 s->downmix_coef[i][1]);
1559 ff_dlog(s->avctx, "\n");
1563 s->core_ext_mask = ff_dca_ext_audio_descr_mask[s->ext_descr];
1565 s->core_ext_mask = 0;
1567 core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
1569 /* only scan for extensions if ext_descr was unknown or indicated a
1570 * supported XCh extension */
1571 if (s->core_ext_mask < 0 || s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH)) {
1572 /* if ext_descr was unknown, clear s->core_ext_mask so that the
1573 * extensions scan can fill it up */
1574 s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
1576 /* extensions start at 32-bit boundaries into bitstream */
1577 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1579 while (core_ss_end - get_bits_count(&s->gb) >= 32) {
1580 uint32_t bits = get_bits_long(&s->gb, 32);
1583 case DCA_SYNCWORD_XCH: {
1584 int ext_amode, xch_fsize;
1586 s->xch_base_channel = s->prim_channels;
1588 /* validate sync word using XCHFSIZE field */
1589 xch_fsize = show_bits(&s->gb, 10);
1590 if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
1591 (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
1594 /* skip length-to-end-of-frame field for the moment */
1595 skip_bits(&s->gb, 10);
1597 s->core_ext_mask |= DCA_EXT_XCH;
1599 /* extension amode(number of channels in extension) should be 1 */
1600 /* AFAIK XCh is not used for more channels */
1601 if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
1602 av_log(avctx, AV_LOG_ERROR,
1603 "XCh extension amode %d not supported!\n",
1608 if (s->xch_base_channel < 2) {
1609 avpriv_request_sample(avctx, "XCh with fewer than 2 base channels");
1613 /* much like core primary audio coding header */
1614 dca_parse_audio_coding_header(s, s->xch_base_channel, 0);
1616 for (i = 0; i < (s->sample_blocks / 8); i++)
1617 if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
1618 av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
1625 case DCA_SYNCWORD_XXCH:
1626 /* XXCh: extended channels */
1627 /* usually found either in core or HD part in DTS-HD HRA streams,
1628 * but not in DTS-ES which contains XCh extensions instead */
1629 s->core_ext_mask |= DCA_EXT_XXCH;
1630 ff_dca_xxch_decode_frame(s);
1634 int fsize96 = show_bits(&s->gb, 12) + 1;
1635 if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
1638 av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
1639 get_bits_count(&s->gb));
1640 skip_bits(&s->gb, 12);
1641 av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
1642 av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
1644 s->core_ext_mask |= DCA_EXT_X96;
1649 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1652 /* no supported extensions, skip the rest of the core substream */
1653 skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
1656 if (s->core_ext_mask & DCA_EXT_X96)
1657 s->profile = FF_PROFILE_DTS_96_24;
1658 else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
1659 s->profile = FF_PROFILE_DTS_ES;
1661 /* check for ExSS (HD part) */
1662 if (s->dca_buffer_size - s->frame_size > 32 &&
1663 get_bits_long(&s->gb, 32) == DCA_SYNCWORD_SUBSTREAM)
1664 ff_dca_exss_parse_header(s);
1666 avctx->profile = s->profile;
1668 full_channels = channels = s->prim_channels + !!s->lfe;
1670 /* If we have XXCH then the channel layout is managed differently */
1671 /* note that XLL will also have another way to do things */
1672 if (!(s->core_ext_mask & DCA_EXT_XXCH)
1673 || (s->core_ext_mask & DCA_EXT_XXCH && avctx->request_channels > 0
1674 && avctx->request_channels
1675 < num_core_channels + !!s->lfe + s->xxch_chset_nch[0]))
1676 { /* xxx should also do MA extensions */
1677 if (s->amode < 16) {
1678 avctx->channel_layout = ff_dca_core_channel_layout[s->amode];
1680 if (s->prim_channels + !!s->lfe > 2 &&
1681 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1683 * Neither the core's auxiliary data nor our default tables contain
1684 * downmix coefficients for the additional channel coded in the XCh
1685 * extension, so when we're doing a Stereo downmix, don't decode it.
1690 #if FF_API_REQUEST_CHANNELS
1691 FF_DISABLE_DEPRECATION_WARNINGS
1692 if (s->xch_present && !s->xch_disable &&
1693 (!avctx->request_channels ||
1694 avctx->request_channels > num_core_channels + !!s->lfe)) {
1695 FF_ENABLE_DEPRECATION_WARNINGS
1697 if (s->xch_present && !s->xch_disable) {
1699 if (avctx->channel_layout & AV_CH_BACK_CENTER) {
1700 avpriv_request_sample(avctx, "XCh with Back center channel");
1701 return AVERROR_INVALIDDATA;
1703 avctx->channel_layout |= AV_CH_BACK_CENTER;
1705 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
1706 s->channel_order_tab = ff_dca_channel_reorder_lfe_xch[s->amode];
1708 s->channel_order_tab = ff_dca_channel_reorder_nolfe_xch[s->amode];
1710 if (s->channel_order_tab[s->xch_base_channel] < 0)
1711 return AVERROR_INVALIDDATA;
1713 channels = num_core_channels + !!s->lfe;
1714 s->xch_present = 0; /* disable further xch processing */
1716 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
1717 s->channel_order_tab = ff_dca_channel_reorder_lfe[s->amode];
1719 s->channel_order_tab = ff_dca_channel_reorder_nolfe[s->amode];
1722 if (channels > !!s->lfe &&
1723 s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
1724 return AVERROR_INVALIDDATA;
1726 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != channels) {
1727 av_log(avctx, AV_LOG_ERROR, "Number of channels %d mismatches layout %d\n", channels, av_get_channel_layout_nb_channels(avctx->channel_layout));
1728 return AVERROR_INVALIDDATA;
1731 if (num_core_channels + !!s->lfe > 2 &&
1732 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1734 s->output = s->prim_channels == 2 ? s->amode : DCA_STEREO;
1735 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
1737 else if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
1738 static const int8_t dca_channel_order_native[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
1739 s->channel_order_tab = dca_channel_order_native;
1741 s->lfe_index = ff_dca_lfe_index[s->amode];
1743 av_log(avctx, AV_LOG_ERROR,
1744 "Non standard configuration %d !\n", s->amode);
1745 return AVERROR_INVALIDDATA;
1748 s->xxch_dmix_embedded = 0;
1750 /* we only get here if an XXCH channel set can be added to the mix */
1751 channel_mask = s->xxch_core_spkmask;
1753 if (avctx->request_channels > 0
1754 && avctx->request_channels < s->prim_channels) {
1755 channels = num_core_channels + !!s->lfe;
1756 for (i = 0; i < s->xxch_chset && channels + s->xxch_chset_nch[i]
1757 <= avctx->request_channels; i++) {
1758 channels += s->xxch_chset_nch[i];
1759 channel_mask |= s->xxch_spk_masks[i];
1762 channels = s->prim_channels + !!s->lfe;
1763 for (i = 0; i < s->xxch_chset; i++) {
1764 channel_mask |= s->xxch_spk_masks[i];
1768 /* Given the DTS spec'ed channel mask, generate an avcodec version */
1770 for (i = 0; i < s->xxch_nbits_spk_mask; ++i) {
1771 if (channel_mask & (1 << i)) {
1772 channel_layout |= ff_dca_map_xxch_to_native[i];
1776 /* make sure that we have managed to get equivalent dts/avcodec channel
1777 * masks in some sense -- unfortunately some channels could overlap */
1778 if (av_popcount(channel_mask) != av_popcount(channel_layout)) {
1779 av_log(avctx, AV_LOG_DEBUG,
1780 "DTS-XXCH: Inconsistent avcodec/dts channel layouts\n");
1781 return AVERROR_INVALIDDATA;
1784 avctx->channel_layout = channel_layout;
1786 if (!(avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE)) {
1787 /* Estimate DTS --> avcodec ordering table */
1788 for (chset = -1, j = 0; chset < s->xxch_chset; ++chset) {
1789 mask = chset >= 0 ? s->xxch_spk_masks[chset]
1790 : s->xxch_core_spkmask;
1791 for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
1792 if (mask & ~(DCA_XXCH_LFE1 | DCA_XXCH_LFE2) & (1 << i)) {
1793 lavc = ff_dca_map_xxch_to_native[i];
1794 posn = av_popcount(channel_layout & (lavc - 1));
1795 s->xxch_order_tab[j++] = posn;
1801 s->lfe_index = av_popcount(channel_layout & (AV_CH_LOW_FREQUENCY-1));
1802 } else { /* native ordering */
1803 for (i = 0; i < channels; i++)
1804 s->xxch_order_tab[i] = i;
1806 s->lfe_index = channels - 1;
1809 s->channel_order_tab = s->xxch_order_tab;
1812 /* get output buffer */
1813 frame->nb_samples = 256 * (s->sample_blocks / 8);
1814 if (s->exss_ext_mask & DCA_EXT_EXSS_XLL) {
1815 int xll_nb_samples = s->xll_segments * s->xll_smpl_in_seg;
1816 /* Check for invalid/unsupported conditions first */
1817 if (s->xll_residual_channels > channels) {
1818 av_log(s->avctx, AV_LOG_WARNING,
1819 "DCA: too many residual channels (%d, core channels %d). Disabling XLL\n",
1820 s->xll_residual_channels, channels);
1821 s->exss_ext_mask &= ~DCA_EXT_EXSS_XLL;
1822 } else if (xll_nb_samples != frame->nb_samples &&
1823 2 * frame->nb_samples != xll_nb_samples) {
1824 av_log(s->avctx, AV_LOG_WARNING,
1825 "DCA: unsupported upsampling (%d XLL samples, %d core samples). Disabling XLL\n",
1826 xll_nb_samples, frame->nb_samples);
1827 s->exss_ext_mask &= ~DCA_EXT_EXSS_XLL;
1829 if (2 * frame->nb_samples == xll_nb_samples) {
1830 av_log(s->avctx, AV_LOG_INFO,
1831 "XLL: upsampling core channels by a factor of 2\n");
1834 frame->nb_samples = xll_nb_samples;
1835 // FIXME: Is it good enough to copy from the first channel set?
1836 avctx->sample_rate = s->xll_chsets[0].sampling_frequency;
1838 /* If downmixing to stereo, don't decode additional channels.
1839 * FIXME: Using the xch_disable flag for this doesn't seem right. */
1840 if (!s->xch_disable)
1841 channels = s->xll_channels;
1845 if (avctx->channels != channels) {
1846 if (avctx->channels)
1847 av_log(avctx, AV_LOG_INFO, "Number of channels changed in DCA decoder (%d -> %d)\n", avctx->channels, channels);
1848 avctx->channels = channels;
1851 /* FIXME: This is an ugly hack, to just revert to the default
1852 * layout if we have additional channels. Need to convert the XLL
1853 * channel masks to ffmpeg channel_layout mask. */
1854 if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels)
1855 avctx->channel_layout = 0;
1857 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1859 samples_flt = (float **) frame->extended_data;
1861 /* allocate buffer for extra channels if downmixing */
1862 if (avctx->channels < full_channels) {
1863 ret = av_samples_get_buffer_size(NULL, full_channels - channels,
1865 avctx->sample_fmt, 0);
1869 av_fast_malloc(&s->extra_channels_buffer,
1870 &s->extra_channels_buffer_size, ret);
1871 if (!s->extra_channels_buffer)
1872 return AVERROR(ENOMEM);
1874 ret = av_samples_fill_arrays((uint8_t **) s->extra_channels, NULL,
1875 s->extra_channels_buffer,
1876 full_channels - channels,
1877 frame->nb_samples, avctx->sample_fmt, 0);
1882 /* filter to get final output */
1883 for (i = 0; i < (s->sample_blocks / 8); i++) {
1885 unsigned block = upsample ? 512 : 256;
1886 for (ch = 0; ch < channels; ch++)
1887 s->samples_chanptr[ch] = samples_flt[ch] + i * block;
1888 for (; ch < full_channels; ch++)
1889 s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * block;
1891 dca_filter_channels(s, i, upsample);
1893 /* If this was marked as a DTS-ES stream we need to subtract back- */
1894 /* channel from SL & SR to remove matrixed back-channel signal */
1895 if ((s->source_pcm_res & 1) && s->xch_present) {
1896 float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
1897 float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
1898 float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
1899 s->fdsp->vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
1900 s->fdsp->vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
1903 /* If stream contains XXCH, we might need to undo an embedded downmix */
1904 if (s->xxch_dmix_embedded) {
1905 /* Loop over channel sets in turn */
1906 ch = num_core_channels;
1907 for (chset = 0; chset < s->xxch_chset; chset++) {
1908 endch = ch + s->xxch_chset_nch[chset];
1909 mask = s->xxch_dmix_embedded;
1912 for (j = ch; j < endch; j++) {
1913 if (mask & (1 << j)) { /* this channel has been mixed-out */
1914 src_chan = s->samples_chanptr[s->channel_order_tab[j]];
1915 for (k = 0; k < endch; k++) {
1916 achan = s->channel_order_tab[k];
1917 scale = s->xxch_dmix_coeff[j][k];
1919 dst_chan = s->samples_chanptr[achan];
1920 s->fdsp->vector_fmac_scalar(dst_chan, src_chan,
1927 /* if a downmix has been embedded then undo the pre-scaling */
1928 if ((mask & (1 << ch)) && s->xxch_dmix_sf[chset] != 1.0f) {
1929 scale = s->xxch_dmix_sf[chset];
1931 for (j = 0; j < ch; j++) {
1932 src_chan = s->samples_chanptr[s->channel_order_tab[j]];
1933 for (k = 0; k < 256; k++)
1934 src_chan[k] *= scale;
1937 /* LFE channel is always part of core, scale if it exists */
1939 src_chan = s->samples_chanptr[s->lfe_index];
1940 for (k = 0; k < 256; k++)
1941 src_chan[k] *= scale;
1951 /* update lfe history */
1952 lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
1953 for (i = 0; i < 2 * s->lfe * 4; i++)
1954 s->lfe_data[i] = s->lfe_data[i + lfe_samples];
1956 if (s->exss_ext_mask & DCA_EXT_EXSS_XLL) {
1957 ret = ff_dca_xll_decode_audio(s, frame);
1963 * DCA_STEREO_TOTAL (Lt/Rt) is equivalent to Dolby Surround */
1964 ret = ff_side_data_update_matrix_encoding(frame,
1965 (s->output & ~DCA_LFE) == DCA_STEREO_TOTAL ?
1966 AV_MATRIX_ENCODING_DOLBY : AV_MATRIX_ENCODING_NONE);
1970 if ( avctx->profile != FF_PROFILE_DTS_HD_MA
1971 && avctx->profile != FF_PROFILE_DTS_HD_HRA)
1972 avctx->bit_rate = s->bit_rate;
1979 * DCA initialization
1981 * @param avctx pointer to the AVCodecContext
1984 static av_cold int dca_decode_init(AVCodecContext *avctx)
1986 DCAContext *s = avctx->priv_data;
1991 s->fdsp = avpriv_float_dsp_alloc(avctx->flags & CODEC_FLAG_BITEXACT);
1993 return AVERROR(ENOMEM);
1995 ff_mdct_init(&s->imdct, 6, 1, 1.0);
1996 ff_synth_filter_init(&s->synth);
1997 ff_dcadsp_init(&s->dcadsp);
1998 ff_fmt_convert_init(&s->fmt_conv, avctx);
2000 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2002 /* allow downmixing to stereo */
2003 #if FF_API_REQUEST_CHANNELS
2004 FF_DISABLE_DEPRECATION_WARNINGS
2005 if (avctx->request_channels == 2)
2006 avctx->request_channel_layout = AV_CH_LAYOUT_STEREO;
2007 FF_ENABLE_DEPRECATION_WARNINGS
2009 if (avctx->channels > 2 &&
2010 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO)
2011 avctx->channels = 2;
2016 static av_cold int dca_decode_end(AVCodecContext *avctx)
2018 DCAContext *s = avctx->priv_data;
2019 ff_mdct_end(&s->imdct);
2020 av_freep(&s->extra_channels_buffer);
2022 av_freep(&s->xll_sample_buf);
2023 av_freep(&s->qmf64_table);
2027 static const AVProfile profiles[] = {
2028 { FF_PROFILE_DTS, "DTS" },
2029 { FF_PROFILE_DTS_ES, "DTS-ES" },
2030 { FF_PROFILE_DTS_96_24, "DTS 96/24" },
2031 { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
2032 { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
2033 { FF_PROFILE_UNKNOWN },
2036 static const AVOption options[] = {
2037 { "disable_xch", "disable decoding of the XCh extension", offsetof(DCAContext, xch_disable), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM },
2038 { "disable_xll", "disable decoding of the XLL extension", offsetof(DCAContext, xll_disable), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM },
2042 static const AVClass dca_decoder_class = {
2043 .class_name = "DCA decoder",
2044 .item_name = av_default_item_name,
2046 .version = LIBAVUTIL_VERSION_INT,
2047 .category = AV_CLASS_CATEGORY_DECODER,
2050 AVCodec ff_dca_decoder = {
2052 .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
2053 .type = AVMEDIA_TYPE_AUDIO,
2054 .id = AV_CODEC_ID_DTS,
2055 .priv_data_size = sizeof(DCAContext),
2056 .init = dca_decode_init,
2057 .decode = dca_decode_frame,
2058 .close = dca_decode_end,
2059 .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2060 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
2061 AV_SAMPLE_FMT_NONE },
2062 .profiles = NULL_IF_CONFIG_SMALL(profiles),
2063 .priv_class = &dca_decoder_class,