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
8 * This file is part of Libav.
10 * Libav is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * Libav is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with Libav; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/common.h"
31 #include "libavutil/float_dsp.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/mathematics.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/samplefmt.h"
44 #include "fmtconvert.h"
49 #include "synth_filter.h"
69 /* -1 are reserved or unknown */
70 static const int dca_ext_audio_descr_mask[] = {
74 DCA_EXT_XCH | DCA_EXT_X96,
81 /* Tables for mapping dts channel configurations to libavcodec multichannel api.
82 * Some compromises have been made for special configurations. Most configurations
83 * are never used so complete accuracy is not needed.
85 * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
86 * S -> side, when both rear and back are configured move one of them to the side channel
88 * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
90 static const uint64_t dca_core_channel_layout[] = {
91 AV_CH_FRONT_CENTER, ///< 1, A
92 AV_CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
93 AV_CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
94 AV_CH_LAYOUT_STEREO, ///< 2, (L + R) + (L - R) (sum-difference)
95 AV_CH_LAYOUT_STEREO, ///< 2, LT + RT (left and right total)
96 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER, ///< 3, C + L + R
97 AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER, ///< 3, L + R + S
98 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 4, C + L + R + S
99 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 4, L + R + SL + SR
101 AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT |
102 AV_CH_SIDE_RIGHT, ///< 5, C + L + R + SL + SR
104 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
105 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER, ///< 6, CL + CR + L + R + SL + SR
107 AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT |
108 AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 6, C + L + R + LR + RR + OV
110 AV_CH_FRONT_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
111 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_BACK_CENTER |
112 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 6, CF + CR + LF + RF + LR + RR
114 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
115 AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
116 AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR
118 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
119 AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
120 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2 + SR1 + SR2
122 AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
123 AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
124 AV_CH_SIDE_LEFT | AV_CH_BACK_CENTER | AV_CH_SIDE_RIGHT, ///< 8, CL + C + CR + L + R + SL + S + SR
127 #define DCA_DOLBY 101 /* FIXME */
129 #define DCA_CHANNEL_BITS 6
130 #define DCA_CHANNEL_MASK 0x3F
134 #define HEADER_SIZE 14
136 #define DCA_NSYNCAUX 0x9A1105A0
138 /** Bit allocation */
139 typedef struct BitAlloc {
140 int offset; ///< code values offset
141 int maxbits[8]; ///< max bits in VLC
142 int wrap; ///< wrap for get_vlc2()
143 VLC vlc[8]; ///< actual codes
146 static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
147 static BitAlloc dca_tmode; ///< transition mode VLCs
148 static BitAlloc dca_scalefactor; ///< scalefactor VLCs
149 static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
151 static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba,
154 return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
158 static av_cold void dca_init_vlcs(void)
160 static int vlcs_initialized = 0;
162 static VLC_TYPE dca_table[23622][2];
164 if (vlcs_initialized)
167 dca_bitalloc_index.offset = 1;
168 dca_bitalloc_index.wrap = 2;
169 for (i = 0; i < 5; i++) {
170 dca_bitalloc_index.vlc[i].table = &dca_table[ff_dca_vlc_offs[i]];
171 dca_bitalloc_index.vlc[i].table_allocated = ff_dca_vlc_offs[i + 1] - ff_dca_vlc_offs[i];
172 init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
173 bitalloc_12_bits[i], 1, 1,
174 bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
176 dca_scalefactor.offset = -64;
177 dca_scalefactor.wrap = 2;
178 for (i = 0; i < 5; i++) {
179 dca_scalefactor.vlc[i].table = &dca_table[ff_dca_vlc_offs[i + 5]];
180 dca_scalefactor.vlc[i].table_allocated = ff_dca_vlc_offs[i + 6] - ff_dca_vlc_offs[i + 5];
181 init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
182 scales_bits[i], 1, 1,
183 scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
185 dca_tmode.offset = 0;
187 for (i = 0; i < 4; i++) {
188 dca_tmode.vlc[i].table = &dca_table[ff_dca_vlc_offs[i + 10]];
189 dca_tmode.vlc[i].table_allocated = ff_dca_vlc_offs[i + 11] - ff_dca_vlc_offs[i + 10];
190 init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
192 tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
195 for (i = 0; i < 10; i++)
196 for (j = 0; j < 7; j++) {
197 if (!bitalloc_codes[i][j])
199 dca_smpl_bitalloc[i + 1].offset = bitalloc_offsets[i];
200 dca_smpl_bitalloc[i + 1].wrap = 1 + (j > 4);
201 dca_smpl_bitalloc[i + 1].vlc[j].table = &dca_table[ff_dca_vlc_offs[c]];
202 dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = ff_dca_vlc_offs[c + 1] - ff_dca_vlc_offs[c];
204 init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
206 bitalloc_bits[i][j], 1, 1,
207 bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
210 vlcs_initialized = 1;
213 static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
216 *dst++ = get_bits(gb, bits);
219 static int dca_parse_audio_coding_header(DCAContext *s, int base_channel)
222 static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
223 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
224 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
226 s->total_channels = get_bits(&s->gb, 3) + 1 + base_channel;
227 s->prim_channels = s->total_channels;
229 if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
230 s->prim_channels = DCA_PRIM_CHANNELS_MAX;
232 for (i = base_channel; i < s->prim_channels; i++) {
233 s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
234 if (s->subband_activity[i] > DCA_SUBBANDS)
235 s->subband_activity[i] = DCA_SUBBANDS;
237 for (i = base_channel; i < s->prim_channels; i++) {
238 s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
239 if (s->vq_start_subband[i] > DCA_SUBBANDS)
240 s->vq_start_subband[i] = DCA_SUBBANDS;
242 get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
243 get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
244 get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
245 get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
247 /* Get codebooks quantization indexes */
249 memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
250 for (j = 1; j < 11; j++)
251 for (i = base_channel; i < s->prim_channels; i++)
252 s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
254 /* Get scale factor adjustment */
255 for (j = 0; j < 11; j++)
256 for (i = base_channel; i < s->prim_channels; i++)
257 s->scalefactor_adj[i][j] = 1;
259 for (j = 1; j < 11; j++)
260 for (i = base_channel; i < s->prim_channels; i++)
261 if (s->quant_index_huffman[i][j] < thr[j])
262 s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
264 if (s->crc_present) {
265 /* Audio header CRC check */
266 get_bits(&s->gb, 16);
269 s->current_subframe = 0;
270 s->current_subsubframe = 0;
275 static int dca_parse_frame_header(DCAContext *s)
277 init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
280 skip_bits_long(&s->gb, 32);
283 s->frame_type = get_bits(&s->gb, 1);
284 s->samples_deficit = get_bits(&s->gb, 5) + 1;
285 s->crc_present = get_bits(&s->gb, 1);
286 s->sample_blocks = get_bits(&s->gb, 7) + 1;
287 s->frame_size = get_bits(&s->gb, 14) + 1;
288 if (s->frame_size < 95)
289 return AVERROR_INVALIDDATA;
290 s->amode = get_bits(&s->gb, 6);
291 s->sample_rate = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
293 return AVERROR_INVALIDDATA;
294 s->bit_rate_index = get_bits(&s->gb, 5);
295 s->bit_rate = ff_dca_bit_rates[s->bit_rate_index];
297 return AVERROR_INVALIDDATA;
299 skip_bits1(&s->gb); // always 0 (reserved, cf. ETSI TS 102 114 V1.4.1)
300 s->dynrange = get_bits(&s->gb, 1);
301 s->timestamp = get_bits(&s->gb, 1);
302 s->aux_data = get_bits(&s->gb, 1);
303 s->hdcd = get_bits(&s->gb, 1);
304 s->ext_descr = get_bits(&s->gb, 3);
305 s->ext_coding = get_bits(&s->gb, 1);
306 s->aspf = get_bits(&s->gb, 1);
307 s->lfe = get_bits(&s->gb, 2);
308 s->predictor_history = get_bits(&s->gb, 1);
311 av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE value: %d\n", s->lfe);
312 return AVERROR_INVALIDDATA;
315 /* TODO: check CRC */
317 s->header_crc = get_bits(&s->gb, 16);
319 s->multirate_inter = get_bits(&s->gb, 1);
320 s->version = get_bits(&s->gb, 4);
321 s->copy_history = get_bits(&s->gb, 2);
322 s->source_pcm_res = get_bits(&s->gb, 3);
323 s->front_sum = get_bits(&s->gb, 1);
324 s->surround_sum = get_bits(&s->gb, 1);
325 s->dialog_norm = get_bits(&s->gb, 4);
327 /* FIXME: channels mixing levels */
328 s->output = s->amode;
330 s->output |= DCA_LFE;
332 /* Primary audio coding header */
333 s->subframes = get_bits(&s->gb, 4) + 1;
335 return dca_parse_audio_coding_header(s, 0);
338 static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
341 /* huffman encoded */
342 value += get_bitalloc(gb, &dca_scalefactor, level);
343 value = av_clip(value, 0, (1 << log2range) - 1);
344 } else if (level < 8) {
345 if (level + 1 > log2range) {
346 skip_bits(gb, level + 1 - log2range);
347 value = get_bits(gb, log2range);
349 value = get_bits(gb, level + 1);
355 static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
357 /* Primary audio coding side information */
360 if (get_bits_left(&s->gb) < 0)
361 return AVERROR_INVALIDDATA;
364 s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
365 s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
368 for (j = base_channel; j < s->prim_channels; j++) {
369 for (k = 0; k < s->subband_activity[j]; k++)
370 s->prediction_mode[j][k] = get_bits(&s->gb, 1);
373 /* Get prediction codebook */
374 for (j = base_channel; j < s->prim_channels; j++) {
375 for (k = 0; k < s->subband_activity[j]; k++) {
376 if (s->prediction_mode[j][k] > 0) {
377 /* (Prediction coefficient VQ address) */
378 s->prediction_vq[j][k] = get_bits(&s->gb, 12);
383 /* Bit allocation index */
384 for (j = base_channel; j < s->prim_channels; j++) {
385 for (k = 0; k < s->vq_start_subband[j]; k++) {
386 if (s->bitalloc_huffman[j] == 6)
387 s->bitalloc[j][k] = get_bits(&s->gb, 5);
388 else if (s->bitalloc_huffman[j] == 5)
389 s->bitalloc[j][k] = get_bits(&s->gb, 4);
390 else if (s->bitalloc_huffman[j] == 7) {
391 av_log(s->avctx, AV_LOG_ERROR,
392 "Invalid bit allocation index\n");
393 return AVERROR_INVALIDDATA;
396 get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
399 if (s->bitalloc[j][k] > 26) {
400 av_dlog(s->avctx, "bitalloc index [%i][%i] too big (%i)\n",
401 j, k, s->bitalloc[j][k]);
402 return AVERROR_INVALIDDATA;
407 /* Transition mode */
408 for (j = base_channel; j < s->prim_channels; j++) {
409 for (k = 0; k < s->subband_activity[j]; k++) {
410 s->transition_mode[j][k] = 0;
411 if (s->subsubframes[s->current_subframe] > 1 &&
412 k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
413 s->transition_mode[j][k] =
414 get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
419 if (get_bits_left(&s->gb) < 0)
420 return AVERROR_INVALIDDATA;
422 for (j = base_channel; j < s->prim_channels; j++) {
423 const uint32_t *scale_table;
424 int scale_sum, log_size;
426 memset(s->scale_factor[j], 0,
427 s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
429 if (s->scalefactor_huffman[j] == 6) {
430 scale_table = ff_dca_scale_factor_quant7;
433 scale_table = ff_dca_scale_factor_quant6;
437 /* When huffman coded, only the difference is encoded */
440 for (k = 0; k < s->subband_activity[j]; k++) {
441 if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
442 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
443 s->scale_factor[j][k][0] = scale_table[scale_sum];
446 if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
447 /* Get second scale factor */
448 scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
449 s->scale_factor[j][k][1] = scale_table[scale_sum];
454 /* Joint subband scale factor codebook select */
455 for (j = base_channel; j < s->prim_channels; j++) {
456 /* Transmitted only if joint subband coding enabled */
457 if (s->joint_intensity[j] > 0)
458 s->joint_huff[j] = get_bits(&s->gb, 3);
461 if (get_bits_left(&s->gb) < 0)
462 return AVERROR_INVALIDDATA;
464 /* Scale factors for joint subband coding */
465 for (j = base_channel; j < s->prim_channels; j++) {
468 /* Transmitted only if joint subband coding enabled */
469 if (s->joint_intensity[j] > 0) {
471 source_channel = s->joint_intensity[j] - 1;
473 /* When huffman coded, only the difference is encoded
474 * (is this valid as well for joint scales ???) */
476 for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
477 scale = get_scale(&s->gb, s->joint_huff[j], 64 /* bias */, 7);
478 s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
481 if (!(s->debug_flag & 0x02)) {
482 av_log(s->avctx, AV_LOG_DEBUG,
483 "Joint stereo coding not supported\n");
484 s->debug_flag |= 0x02;
489 /* Dynamic range coefficient */
490 if (!base_channel && s->dynrange)
491 s->dynrange_coef = get_bits(&s->gb, 8);
493 /* Side information CRC check word */
494 if (s->crc_present) {
495 get_bits(&s->gb, 16);
499 * Primary audio data arrays
502 /* VQ encoded high frequency subbands */
503 for (j = base_channel; j < s->prim_channels; j++)
504 for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
505 /* 1 vector -> 32 samples */
506 s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
508 /* Low frequency effect data */
509 if (!base_channel && s->lfe) {
511 int lfe_samples = 2 * s->lfe * (4 + block_index);
512 int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
515 for (j = lfe_samples; j < lfe_end_sample; j++) {
516 /* Signed 8 bits int */
517 s->lfe_data[j] = get_sbits(&s->gb, 8);
520 /* Scale factor index */
521 skip_bits(&s->gb, 1);
522 s->lfe_scale_factor = ff_dca_scale_factor_quant7[get_bits(&s->gb, 7)];
524 /* Quantization step size * scale factor */
525 lfe_scale = 0.035 * s->lfe_scale_factor;
527 for (j = lfe_samples; j < lfe_end_sample; j++)
528 s->lfe_data[j] *= lfe_scale;
534 static void qmf_32_subbands(DCAContext *s, int chans,
535 float samples_in[32][8], float *samples_out,
538 const float *prCoeff;
540 int sb_act = s->subband_activity[chans];
542 scale *= sqrt(1 / 8.0);
545 if (!s->multirate_inter) /* Non-perfect reconstruction */
546 prCoeff = ff_dca_fir_32bands_nonperfect;
547 else /* Perfect reconstruction */
548 prCoeff = ff_dca_fir_32bands_perfect;
550 s->dcadsp.qmf_32_subbands(samples_in, sb_act, &s->synth, &s->imdct,
551 s->subband_fir_hist[chans],
552 &s->hist_index[chans],
553 s->subband_fir_noidea[chans], prCoeff,
554 samples_out, s->raXin, scale);
557 static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
558 int num_deci_sample, float *samples_in,
561 /* samples_in: An array holding decimated samples.
562 * Samples in current subframe starts from samples_in[0],
563 * while samples_in[-1], samples_in[-2], ..., stores samples
564 * from last subframe as history.
566 * samples_out: An array holding interpolated samples
570 const float *prCoeff;
573 /* Select decimation filter */
574 if (decimation_select == 1) {
576 prCoeff = ff_dca_lfe_fir_128;
579 prCoeff = ff_dca_lfe_fir_64;
582 for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
583 s->dcadsp.lfe_fir[idx](samples_out, samples_in, prCoeff);
585 samples_out += 2 * 32 * (1 + idx);
589 /* downmixing routines */
590 #define MIX_REAR1(samples, s1, rs, coef) \
591 samples[0][i] += samples[s1][i] * coef[rs][0]; \
592 samples[1][i] += samples[s1][i] * coef[rs][1];
594 #define MIX_REAR2(samples, s1, s2, rs, coef) \
595 samples[0][i] += samples[s1][i] * coef[rs][0] + samples[s2][i] * coef[rs + 1][0]; \
596 samples[1][i] += samples[s1][i] * coef[rs][1] + samples[s2][i] * coef[rs + 1][1];
598 #define MIX_FRONT3(samples, coef) \
602 samples[0][i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
603 samples[1][i] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
605 #define DOWNMIX_TO_STEREO(op1, op2) \
606 for (i = 0; i < 256; i++) { \
611 static void dca_downmix(float **samples, int srcfmt, int lfe_present,
612 float coef[DCA_PRIM_CHANNELS_MAX + 1][2],
613 const int8_t *channel_mapping)
615 int c, l, r, sl, sr, s;
622 av_log(NULL, 0, "Not implemented!\n");
626 case DCA_STEREO_TOTAL:
627 case DCA_STEREO_SUMDIFF:
630 c = channel_mapping[0];
631 l = channel_mapping[1];
632 r = channel_mapping[2];
633 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
636 s = channel_mapping[2];
637 DOWNMIX_TO_STEREO(MIX_REAR1(samples, s, 2, coef), );
640 c = channel_mapping[0];
641 l = channel_mapping[1];
642 r = channel_mapping[2];
643 s = channel_mapping[3];
644 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
645 MIX_REAR1(samples, s, 3, coef));
648 sl = channel_mapping[2];
649 sr = channel_mapping[3];
650 DOWNMIX_TO_STEREO(MIX_REAR2(samples, sl, sr, 2, coef), );
653 c = channel_mapping[0];
654 l = channel_mapping[1];
655 r = channel_mapping[2];
656 sl = channel_mapping[3];
657 sr = channel_mapping[4];
658 DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
659 MIX_REAR2(samples, sl, sr, 3, coef));
663 int lf_buf = ff_dca_lfe_index[srcfmt];
664 int lf_idx = ff_dca_channels[srcfmt];
665 for (i = 0; i < 256; i++) {
666 samples[0][i] += samples[lf_buf][i] * coef[lf_idx][0];
667 samples[1][i] += samples[lf_buf][i] * coef[lf_idx][1];
672 #ifndef decode_blockcodes
673 /* Very compact version of the block code decoder that does not use table
674 * look-up but is slightly slower */
675 static int decode_blockcode(int code, int levels, int32_t *values)
678 int offset = (levels - 1) >> 1;
680 for (i = 0; i < 4; i++) {
681 int div = FASTDIV(code, levels);
682 values[i] = code - offset - div * levels;
689 static int decode_blockcodes(int code1, int code2, int levels, int32_t *values)
691 return decode_blockcode(code1, levels, values) |
692 decode_blockcode(code2, levels, values + 4);
696 static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
697 static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
699 static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
702 int subsubframe = s->current_subsubframe;
704 const float *quant_step_table;
707 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
708 LOCAL_ALIGNED_16(int32_t, block, [8 * DCA_SUBBANDS]);
714 /* Select quantization step size table */
715 if (s->bit_rate_index == 0x1f)
716 quant_step_table = ff_dca_lossless_quant_d;
718 quant_step_table = ff_dca_lossy_quant_d;
720 for (k = base_channel; k < s->prim_channels; k++) {
721 float rscale[DCA_SUBBANDS];
723 if (get_bits_left(&s->gb) < 0)
724 return AVERROR_INVALIDDATA;
726 for (l = 0; l < s->vq_start_subband[k]; l++) {
729 /* Select the mid-tread linear quantizer */
730 int abits = s->bitalloc[k][l];
732 float quant_step_size = quant_step_table[abits];
735 * Determine quantization index code book and its type
738 /* Select quantization index code book */
739 int sel = s->quant_index_huffman[k][abits];
742 * Extract bits from the bit stream
746 memset(block + 8 * l, 0, 8 * sizeof(block[0]));
748 /* Deal with transients */
749 int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
750 rscale[l] = quant_step_size * s->scale_factor[k][l][sfi] *
751 s->scalefactor_adj[k][sel];
753 if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
756 int block_code1, block_code2, size, levels, err;
758 size = abits_sizes[abits - 1];
759 levels = abits_levels[abits - 1];
761 block_code1 = get_bits(&s->gb, size);
762 block_code2 = get_bits(&s->gb, size);
763 err = decode_blockcodes(block_code1, block_code2,
764 levels, block + 8 * l);
766 av_log(s->avctx, AV_LOG_ERROR,
767 "ERROR: block code look-up failed\n");
768 return AVERROR_INVALIDDATA;
772 for (m = 0; m < 8; m++)
773 block[8 * l + m] = get_sbits(&s->gb, abits - 3);
777 for (m = 0; m < 8; m++)
778 block[8 * l + m] = get_bitalloc(&s->gb,
779 &dca_smpl_bitalloc[abits], sel);
784 s->fmt_conv.int32_to_float_fmul_array8(&s->fmt_conv, subband_samples[k][0],
785 block, rscale, 8 * s->vq_start_subband[k]);
787 for (l = 0; l < s->vq_start_subband[k]; l++) {
790 * Inverse ADPCM if in prediction mode
792 if (s->prediction_mode[k][l]) {
794 if (s->predictor_history)
795 subband_samples[k][l][0] += (ff_dca_adpcm_vb[s->prediction_vq[k][l]][0] *
796 s->subband_samples_hist[k][l][3] +
797 ff_dca_adpcm_vb[s->prediction_vq[k][l]][1] *
798 s->subband_samples_hist[k][l][2] +
799 ff_dca_adpcm_vb[s->prediction_vq[k][l]][2] *
800 s->subband_samples_hist[k][l][1] +
801 ff_dca_adpcm_vb[s->prediction_vq[k][l]][3] *
802 s->subband_samples_hist[k][l][0]) *
804 for (m = 1; m < 8; m++) {
805 float sum = ff_dca_adpcm_vb[s->prediction_vq[k][l]][0] *
806 subband_samples[k][l][m - 1];
807 for (n = 2; n <= 4; n++)
809 sum += ff_dca_adpcm_vb[s->prediction_vq[k][l]][n - 1] *
810 subband_samples[k][l][m - n];
811 else if (s->predictor_history)
812 sum += ff_dca_adpcm_vb[s->prediction_vq[k][l]][n - 1] *
813 s->subband_samples_hist[k][l][m - n + 4];
814 subband_samples[k][l][m] += sum * 1.0f / 8192;
820 * Decode VQ encoded high frequencies
822 if (s->subband_activity[k] > s->vq_start_subband[k]) {
823 if (!s->debug_flag & 0x01) {
824 av_log(s->avctx, AV_LOG_DEBUG,
825 "Stream with high frequencies VQ coding\n");
826 s->debug_flag |= 0x01;
828 s->dcadsp.decode_hf(subband_samples[k], s->high_freq_vq[k],
829 ff_dca_high_freq_vq, subsubframe * 8,
830 s->scale_factor[k], s->vq_start_subband[k],
831 s->subband_activity[k]);
835 /* Check for DSYNC after subsubframe */
836 if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
837 if (get_bits(&s->gb, 16) != 0xFFFF) {
838 av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
839 return AVERROR_INVALIDDATA;
843 /* Backup predictor history for adpcm */
844 for (k = base_channel; k < s->prim_channels; k++)
845 for (l = 0; l < s->vq_start_subband[k]; l++)
846 AV_COPY128(s->subband_samples_hist[k][l], &subband_samples[k][l][4]);
851 static int dca_filter_channels(DCAContext *s, int block_index)
853 float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
856 /* 32 subbands QMF */
857 for (k = 0; k < s->prim_channels; k++) {
858 if (s->channel_order_tab[k] >= 0)
859 qmf_32_subbands(s, k, subband_samples[k],
860 s->samples_chanptr[s->channel_order_tab[k]],
861 M_SQRT1_2 / 32768.0);
864 /* Generate LFE samples for this subsubframe FIXME!!! */
866 lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
867 s->lfe_data + 2 * s->lfe * (block_index + 4),
868 s->samples_chanptr[ff_dca_lfe_index[s->amode]]);
869 /* Outputs 20bits pcm samples */
872 /* Downmixing to Stereo */
873 if (s->prim_channels + !!s->lfe > 2 &&
874 s->avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
875 dca_downmix(s->samples_chanptr, s->amode, !!s->lfe, s->downmix_coef,
876 s->channel_order_tab);
882 static int dca_subframe_footer(DCAContext *s, int base_channel)
884 int in, out, aux_data_count, aux_data_end, reserved;
888 * Unpack optional information
891 /* presumably optional information only appears in the core? */
894 skip_bits_long(&s->gb, 32);
897 aux_data_count = get_bits(&s->gb, 6);
900 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
902 aux_data_end = 8 * aux_data_count + get_bits_count(&s->gb);
904 if ((nsyncaux = get_bits_long(&s->gb, 32)) != DCA_NSYNCAUX) {
905 av_log(s->avctx, AV_LOG_ERROR, "nSYNCAUX mismatch %#"PRIx32"\n",
907 return AVERROR_INVALIDDATA;
910 if (get_bits1(&s->gb)) { // bAUXTimeStampFlag
911 avpriv_request_sample(s->avctx,
912 "Auxiliary Decode Time Stamp Flag");
914 skip_bits(&s->gb, (-get_bits_count(&s->gb)) & 4);
915 // 44 bits: nMSByte (8), nMarker (4), nLSByte (28), nMarker (4)
916 skip_bits_long(&s->gb, 44);
919 if ((s->core_downmix = get_bits1(&s->gb))) {
920 int am = get_bits(&s->gb, 3);
923 s->core_downmix_amode = DCA_MONO;
926 s->core_downmix_amode = DCA_STEREO;
929 s->core_downmix_amode = DCA_STEREO_TOTAL;
932 s->core_downmix_amode = DCA_3F;
935 s->core_downmix_amode = DCA_2F1R;
938 s->core_downmix_amode = DCA_2F2R;
941 s->core_downmix_amode = DCA_3F1R;
944 av_log(s->avctx, AV_LOG_ERROR,
945 "Invalid mode %d for embedded downmix coefficients\n",
947 return AVERROR_INVALIDDATA;
949 for (out = 0; out < ff_dca_channels[s->core_downmix_amode]; out++) {
950 for (in = 0; in < s->prim_channels + !!s->lfe; in++) {
951 uint16_t tmp = get_bits(&s->gb, 9);
952 if ((tmp & 0xFF) > 241) {
953 av_log(s->avctx, AV_LOG_ERROR,
954 "Invalid downmix coefficient code %"PRIu16"\n",
956 return AVERROR_INVALIDDATA;
958 s->core_downmix_codes[in][out] = tmp;
963 align_get_bits(&s->gb); // byte align
964 skip_bits(&s->gb, 16); // nAUXCRC16
966 // additional data (reserved, cf. ETSI TS 102 114 V1.4.1)
967 if ((reserved = (aux_data_end - get_bits_count(&s->gb))) < 0) {
968 av_log(s->avctx, AV_LOG_ERROR,
969 "Overread auxiliary data by %d bits\n", -reserved);
970 return AVERROR_INVALIDDATA;
971 } else if (reserved) {
972 avpriv_request_sample(s->avctx,
973 "Core auxiliary data reserved content");
974 skip_bits_long(&s->gb, reserved);
978 if (s->crc_present && s->dynrange)
979 get_bits(&s->gb, 16);
986 * Decode a dca frame block
988 * @param s pointer to the DCAContext
991 static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
996 if (s->current_subframe >= s->subframes) {
997 av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
998 s->current_subframe, s->subframes);
999 return AVERROR_INVALIDDATA;
1002 if (!s->current_subsubframe) {
1003 /* Read subframe header */
1004 if ((ret = dca_subframe_header(s, base_channel, block_index)))
1008 /* Read subsubframe */
1009 if ((ret = dca_subsubframe(s, base_channel, block_index)))
1013 s->current_subsubframe++;
1014 if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
1015 s->current_subsubframe = 0;
1016 s->current_subframe++;
1018 if (s->current_subframe >= s->subframes) {
1019 /* Read subframe footer */
1020 if ((ret = dca_subframe_footer(s, base_channel)))
1027 static float dca_dmix_code(unsigned code)
1029 int sign = (code >> 8) - 1;
1031 return ((ff_dca_dmixtable[code] ^ sign) - sign) * (1.0 / (1U << 15));
1035 * Main frame decoding function
1036 * FIXME add arguments
1038 static int dca_decode_frame(AVCodecContext *avctx, void *data,
1039 int *got_frame_ptr, AVPacket *avpkt)
1041 AVFrame *frame = data;
1042 const uint8_t *buf = avpkt->data;
1043 int buf_size = avpkt->size;
1046 int num_core_channels = 0;
1048 float **samples_flt;
1049 DCAContext *s = avctx->priv_data;
1050 int channels, full_channels;
1055 s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
1056 DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
1057 if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
1058 av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
1059 return AVERROR_INVALIDDATA;
1062 if ((ret = dca_parse_frame_header(s)) < 0) {
1063 // seems like the frame is corrupt, try with the next one
1066 // set AVCodec values with parsed data
1067 avctx->sample_rate = s->sample_rate;
1068 avctx->bit_rate = s->bit_rate;
1070 s->profile = FF_PROFILE_DTS;
1072 for (i = 0; i < (s->sample_blocks / 8); i++) {
1073 if ((ret = dca_decode_block(s, 0, i))) {
1074 av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
1079 /* record number of core channels incase less than max channels are requested */
1080 num_core_channels = s->prim_channels;
1083 s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
1085 s->core_ext_mask = 0;
1087 core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
1089 /* only scan for extensions if ext_descr was unknown or indicated a
1090 * supported XCh extension */
1091 if (s->core_ext_mask < 0 || s->core_ext_mask & DCA_EXT_XCH) {
1092 /* if ext_descr was unknown, clear s->core_ext_mask so that the
1093 * extensions scan can fill it up */
1094 s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
1096 /* extensions start at 32-bit boundaries into bitstream */
1097 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1099 while (core_ss_end - get_bits_count(&s->gb) >= 32) {
1100 uint32_t bits = get_bits_long(&s->gb, 32);
1104 int ext_amode, xch_fsize;
1106 s->xch_base_channel = s->prim_channels;
1108 /* validate sync word using XCHFSIZE field */
1109 xch_fsize = show_bits(&s->gb, 10);
1110 if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
1111 (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
1114 /* skip length-to-end-of-frame field for the moment */
1115 skip_bits(&s->gb, 10);
1117 s->core_ext_mask |= DCA_EXT_XCH;
1119 /* extension amode(number of channels in extension) should be 1 */
1120 /* AFAIK XCh is not used for more channels */
1121 if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
1122 av_log(avctx, AV_LOG_ERROR,
1123 "XCh extension amode %d not supported!\n",
1128 /* much like core primary audio coding header */
1129 dca_parse_audio_coding_header(s, s->xch_base_channel);
1131 for (i = 0; i < (s->sample_blocks / 8); i++)
1132 if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
1133 av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
1141 /* XXCh: extended channels */
1142 /* usually found either in core or HD part in DTS-HD HRA streams,
1143 * but not in DTS-ES which contains XCh extensions instead */
1144 s->core_ext_mask |= DCA_EXT_XXCH;
1148 int fsize96 = show_bits(&s->gb, 12) + 1;
1149 if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
1152 av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
1153 get_bits_count(&s->gb));
1154 skip_bits(&s->gb, 12);
1155 av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
1156 av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
1158 s->core_ext_mask |= DCA_EXT_X96;
1163 skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
1166 /* no supported extensions, skip the rest of the core substream */
1167 skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
1170 if (s->core_ext_mask & DCA_EXT_X96)
1171 s->profile = FF_PROFILE_DTS_96_24;
1172 else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
1173 s->profile = FF_PROFILE_DTS_ES;
1175 /* check for ExSS (HD part) */
1176 if (s->dca_buffer_size - s->frame_size > 32 &&
1177 get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
1178 ff_dca_exss_parse_header(s);
1180 avctx->profile = s->profile;
1182 full_channels = channels = s->prim_channels + !!s->lfe;
1184 if (s->amode < 16) {
1185 avctx->channel_layout = dca_core_channel_layout[s->amode];
1187 if (s->prim_channels + !!s->lfe > 2 &&
1188 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1190 * Neither the core's auxiliary data nor our default tables contain
1191 * downmix coefficients for the additional channel coded in the XCh
1192 * extension, so when we're doing a Stereo downmix, don't decode it.
1197 #if FF_API_REQUEST_CHANNELS
1198 FF_DISABLE_DEPRECATION_WARNINGS
1199 if (s->xch_present && !s->xch_disable &&
1200 (!avctx->request_channels ||
1201 avctx->request_channels > num_core_channels + !!s->lfe)) {
1202 FF_ENABLE_DEPRECATION_WARNINGS
1204 if (s->xch_present && !s->xch_disable) {
1206 avctx->channel_layout |= AV_CH_BACK_CENTER;
1208 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
1209 s->channel_order_tab = ff_dca_channel_reorder_lfe_xch[s->amode];
1211 s->channel_order_tab = ff_dca_channel_reorder_nolfe_xch[s->amode];
1214 channels = num_core_channels + !!s->lfe;
1215 s->xch_present = 0; /* disable further xch processing */
1217 avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
1218 s->channel_order_tab = ff_dca_channel_reorder_lfe[s->amode];
1220 s->channel_order_tab = ff_dca_channel_reorder_nolfe[s->amode];
1223 if (channels > !!s->lfe &&
1224 s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
1225 return AVERROR_INVALIDDATA;
1227 if (num_core_channels + !!s->lfe > 2 &&
1228 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO) {
1230 s->output = s->prim_channels == 2 ? s->amode : DCA_STEREO;
1231 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
1233 /* Stereo downmix coefficients
1235 * The decoder can only downmix to 2-channel, so we need to ensure
1236 * embedded downmix coefficients are actually targeting 2-channel.
1238 if (s->core_downmix && (s->core_downmix_amode == DCA_STEREO ||
1239 s->core_downmix_amode == DCA_STEREO_TOTAL)) {
1240 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1241 /* Range checked earlier */
1242 s->downmix_coef[i][0] = dca_dmix_code(s->core_downmix_codes[i][0]);
1243 s->downmix_coef[i][1] = dca_dmix_code(s->core_downmix_codes[i][1]);
1245 s->output = s->core_downmix_amode;
1247 int am = s->amode & DCA_CHANNEL_MASK;
1248 if (am >= FF_ARRAY_ELEMS(ff_dca_default_coeffs)) {
1249 av_log(s->avctx, AV_LOG_ERROR,
1250 "Invalid channel mode %d\n", am);
1251 return AVERROR_INVALIDDATA;
1253 if (num_core_channels + !!s->lfe >
1254 FF_ARRAY_ELEMS(ff_dca_default_coeffs[0])) {
1255 avpriv_request_sample(s->avctx, "Downmixing %d channels",
1256 s->prim_channels + !!s->lfe);
1257 return AVERROR_PATCHWELCOME;
1259 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1260 s->downmix_coef[i][0] = ff_dca_default_coeffs[am][i][0];
1261 s->downmix_coef[i][1] = ff_dca_default_coeffs[am][i][1];
1264 av_dlog(s->avctx, "Stereo downmix coeffs:\n");
1265 for (i = 0; i < num_core_channels + !!s->lfe; i++) {
1266 av_dlog(s->avctx, "L, input channel %d = %f\n", i,
1267 s->downmix_coef[i][0]);
1268 av_dlog(s->avctx, "R, input channel %d = %f\n", i,
1269 s->downmix_coef[i][1]);
1271 av_dlog(s->avctx, "\n");
1274 av_log(avctx, AV_LOG_ERROR, "Non standard configuration %d !\n", s->amode);
1275 return AVERROR_INVALIDDATA;
1277 avctx->channels = channels;
1279 /* get output buffer */
1280 frame->nb_samples = 256 * (s->sample_blocks / 8);
1281 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
1282 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1285 samples_flt = (float **) frame->extended_data;
1287 /* allocate buffer for extra channels if downmixing */
1288 if (avctx->channels < full_channels) {
1289 ret = av_samples_get_buffer_size(NULL, full_channels - channels,
1291 avctx->sample_fmt, 0);
1295 av_fast_malloc(&s->extra_channels_buffer,
1296 &s->extra_channels_buffer_size, ret);
1297 if (!s->extra_channels_buffer)
1298 return AVERROR(ENOMEM);
1300 ret = av_samples_fill_arrays((uint8_t **) s->extra_channels, NULL,
1301 s->extra_channels_buffer,
1302 full_channels - channels,
1303 frame->nb_samples, avctx->sample_fmt, 0);
1308 /* filter to get final output */
1309 for (i = 0; i < (s->sample_blocks / 8); i++) {
1312 for (ch = 0; ch < channels; ch++)
1313 s->samples_chanptr[ch] = samples_flt[ch] + i * 256;
1314 for (; ch < full_channels; ch++)
1315 s->samples_chanptr[ch] = s->extra_channels[ch - channels] + i * 256;
1317 dca_filter_channels(s, i);
1319 /* If this was marked as a DTS-ES stream we need to subtract back- */
1320 /* channel from SL & SR to remove matrixed back-channel signal */
1321 if ((s->source_pcm_res & 1) && s->xch_present) {
1322 float *back_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel]];
1323 float *lt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 2]];
1324 float *rt_chan = s->samples_chanptr[s->channel_order_tab[s->xch_base_channel - 1]];
1325 s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
1326 s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
1330 /* update lfe history */
1331 lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
1332 for (i = 0; i < 2 * s->lfe * 4; i++)
1333 s->lfe_data[i] = s->lfe_data[i + lfe_samples];
1337 * DCA_STEREO_TOTAL (Lt/Rt) is equivalent to Dolby Surround */
1338 ret = ff_side_data_update_matrix_encoding(frame,
1339 (s->output & ~DCA_LFE) == DCA_STEREO_TOTAL ?
1340 AV_MATRIX_ENCODING_DOLBY : AV_MATRIX_ENCODING_NONE);
1350 * DCA initialization
1352 * @param avctx pointer to the AVCodecContext
1355 static av_cold int dca_decode_init(AVCodecContext *avctx)
1357 DCAContext *s = avctx->priv_data;
1362 avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
1363 ff_mdct_init(&s->imdct, 6, 1, 1.0);
1364 ff_synth_filter_init(&s->synth);
1365 ff_dcadsp_init(&s->dcadsp);
1366 ff_fmt_convert_init(&s->fmt_conv, avctx);
1368 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
1370 /* allow downmixing to stereo */
1371 #if FF_API_REQUEST_CHANNELS
1372 FF_DISABLE_DEPRECATION_WARNINGS
1373 if (avctx->request_channels == 2)
1374 avctx->request_channel_layout = AV_CH_LAYOUT_STEREO;
1375 FF_ENABLE_DEPRECATION_WARNINGS
1377 if (avctx->channels > 2 &&
1378 avctx->request_channel_layout == AV_CH_LAYOUT_STEREO)
1379 avctx->channels = 2;
1384 static av_cold int dca_decode_end(AVCodecContext *avctx)
1386 DCAContext *s = avctx->priv_data;
1387 ff_mdct_end(&s->imdct);
1388 av_freep(&s->extra_channels_buffer);
1392 static const AVProfile profiles[] = {
1393 { FF_PROFILE_DTS, "DTS" },
1394 { FF_PROFILE_DTS_ES, "DTS-ES" },
1395 { FF_PROFILE_DTS_96_24, "DTS 96/24" },
1396 { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
1397 { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
1398 { FF_PROFILE_UNKNOWN },
1401 static const AVOption options[] = {
1402 { "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 },
1406 static const AVClass dca_decoder_class = {
1407 .class_name = "DCA decoder",
1408 .item_name = av_default_item_name,
1410 .version = LIBAVUTIL_VERSION_INT,
1413 AVCodec ff_dca_decoder = {
1415 .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
1416 .type = AVMEDIA_TYPE_AUDIO,
1417 .id = AV_CODEC_ID_DTS,
1418 .priv_data_size = sizeof(DCAContext),
1419 .init = dca_decode_init,
1420 .decode = dca_decode_frame,
1421 .close = dca_decode_end,
1422 .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
1423 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1424 AV_SAMPLE_FMT_NONE },
1425 .profiles = NULL_IF_CONFIG_SMALL(profiles),
1426 .priv_class = &dca_decoder_class,