2 * Copyright (C) 2016 foo86
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "dca_syncwords.h"
38 AMODE_MONO, // Mode 0: A (mono)
39 AMODE_MONO_DUAL, // Mode 1: A + B (dual mono)
40 AMODE_STEREO, // Mode 2: L + R (stereo)
41 AMODE_STEREO_SUMDIFF, // Mode 3: (L+R) + (L-R) (sum-diff)
42 AMODE_STEREO_TOTAL, // Mode 4: LT + RT (left and right total)
43 AMODE_3F, // Mode 5: C + L + R
44 AMODE_2F1R, // Mode 6: L + R + S
45 AMODE_3F1R, // Mode 7: C + L + R + S
46 AMODE_2F2R, // Mode 8: L + R + SL + SR
47 AMODE_3F2R, // Mode 9: C + L + R + SL + SR
65 static const int8_t prm_ch_to_spkr_map[AMODE_COUNT][5] = {
66 { DCA_SPEAKER_C, -1, -1, -1, -1 },
67 { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
68 { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
69 { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
70 { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
71 { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R , -1, -1 },
72 { DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Cs, -1, -1 },
73 { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R , DCA_SPEAKER_Cs, -1 },
74 { DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Ls, DCA_SPEAKER_Rs, -1 },
75 { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Ls, DCA_SPEAKER_Rs }
78 static const uint8_t audio_mode_ch_mask[AMODE_COUNT] = {
79 DCA_SPEAKER_LAYOUT_MONO,
80 DCA_SPEAKER_LAYOUT_STEREO,
81 DCA_SPEAKER_LAYOUT_STEREO,
82 DCA_SPEAKER_LAYOUT_STEREO,
83 DCA_SPEAKER_LAYOUT_STEREO,
84 DCA_SPEAKER_LAYOUT_3_0,
85 DCA_SPEAKER_LAYOUT_2_1,
86 DCA_SPEAKER_LAYOUT_3_1,
87 DCA_SPEAKER_LAYOUT_2_2,
88 DCA_SPEAKER_LAYOUT_5POINT0
91 static const uint8_t block_code_nbits[7] = {
92 7, 10, 12, 13, 15, 17, 19
95 static const uint8_t quant_index_sel_nbits[DCA_CODE_BOOKS] = {
96 1, 2, 2, 2, 2, 3, 3, 3, 3, 3
99 static const uint8_t quant_index_group_size[DCA_CODE_BOOKS] = {
100 1, 3, 3, 3, 3, 7, 7, 7, 7, 7
103 typedef struct DCAVLC {
104 int offset; ///< Code values offset
105 int max_depth; ///< Parameter for get_vlc2()
106 VLC vlc[7]; ///< Actual codes
109 static DCAVLC vlc_bit_allocation;
110 static DCAVLC vlc_transition_mode;
111 static DCAVLC vlc_scale_factor;
112 static DCAVLC vlc_quant_index[DCA_CODE_BOOKS];
114 static av_cold void dca_init_vlcs(void)
116 static VLC_TYPE dca_table[23622][2];
117 static int vlcs_initialized = 0;
120 if (vlcs_initialized)
123 #define DCA_INIT_VLC(vlc, a, b, c, d) \
125 vlc.table = &dca_table[ff_dca_vlc_offs[k]]; \
126 vlc.table_allocated = ff_dca_vlc_offs[k + 1] - ff_dca_vlc_offs[k]; \
127 init_vlc(&vlc, a, b, c, 1, 1, d, 2, 2, INIT_VLC_USE_NEW_STATIC); \
130 vlc_bit_allocation.offset = 1;
131 vlc_bit_allocation.max_depth = 2;
132 for (i = 0, k = 0; i < 5; i++, k++)
133 DCA_INIT_VLC(vlc_bit_allocation.vlc[i], bitalloc_12_vlc_bits[i], 12,
134 bitalloc_12_bits[i], bitalloc_12_codes[i]);
136 vlc_scale_factor.offset = -64;
137 vlc_scale_factor.max_depth = 2;
138 for (i = 0; i < 5; i++, k++)
139 DCA_INIT_VLC(vlc_scale_factor.vlc[i], SCALES_VLC_BITS, 129,
140 scales_bits[i], scales_codes[i]);
142 vlc_transition_mode.offset = 0;
143 vlc_transition_mode.max_depth = 1;
144 for (i = 0; i < 4; i++, k++)
145 DCA_INIT_VLC(vlc_transition_mode.vlc[i], tmode_vlc_bits[i], 4,
146 tmode_bits[i], tmode_codes[i]);
148 for (i = 0; i < DCA_CODE_BOOKS; i++) {
149 vlc_quant_index[i].offset = bitalloc_offsets[i];
150 vlc_quant_index[i].max_depth = 1 + (i > 4);
151 for (j = 0; j < quant_index_group_size[i]; j++, k++)
152 DCA_INIT_VLC(vlc_quant_index[i].vlc[j], bitalloc_maxbits[i][j],
153 bitalloc_sizes[i], bitalloc_bits[i][j], bitalloc_codes[i][j]);
156 vlcs_initialized = 1;
159 static int dca_get_vlc(GetBitContext *s, DCAVLC *v, int i)
161 return get_vlc2(s, v->vlc[i].table, v->vlc[i].bits, v->max_depth) + v->offset;
164 static void get_array(GetBitContext *s, int32_t *array, int size, int n)
168 for (i = 0; i < size; i++)
169 array[i] = get_sbits(s, n);
172 // 5.3.1 - Bit stream header
173 static int parse_frame_header(DCACoreDecoder *s)
175 int normal_frame, pcmr_index;
178 normal_frame = get_bits1(&s->gb);
180 // Deficit sample count
181 if (get_bits(&s->gb, 5) != DCA_PCMBLOCK_SAMPLES - 1) {
182 av_log(s->avctx, AV_LOG_ERROR, "Deficit samples are not supported\n");
183 return normal_frame ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME;
187 s->crc_present = get_bits1(&s->gb);
189 // Number of PCM sample blocks
190 s->npcmblocks = get_bits(&s->gb, 7) + 1;
191 if (s->npcmblocks & (DCA_SUBBAND_SAMPLES - 1)) {
192 av_log(s->avctx, AV_LOG_ERROR, "Unsupported number of PCM sample blocks (%d)\n", s->npcmblocks);
193 return (s->npcmblocks < 6 || normal_frame) ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME;
196 // Primary frame byte size
197 s->frame_size = get_bits(&s->gb, 14) + 1;
198 if (s->frame_size < 96) {
199 av_log(s->avctx, AV_LOG_ERROR, "Invalid core frame size (%d bytes)\n", s->frame_size);
200 return AVERROR_INVALIDDATA;
203 // Audio channel arrangement
204 s->audio_mode = get_bits(&s->gb, 6);
205 if (s->audio_mode >= AMODE_COUNT) {
206 av_log(s->avctx, AV_LOG_ERROR, "Unsupported audio channel arrangement (%d)\n", s->audio_mode);
207 return AVERROR_PATCHWELCOME;
210 // Core audio sampling frequency
211 s->sample_rate = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
212 if (!s->sample_rate) {
213 av_log(s->avctx, AV_LOG_ERROR, "Invalid core audio sampling frequency\n");
214 return AVERROR_INVALIDDATA;
217 // Transmission bit rate
218 s->bit_rate = ff_dca_bit_rates[get_bits(&s->gb, 5)];
223 // Embedded dynamic range flag
224 s->drc_present = get_bits1(&s->gb);
226 // Embedded time stamp flag
227 s->ts_present = get_bits1(&s->gb);
229 // Auxiliary data flag
230 s->aux_present = get_bits1(&s->gb);
232 // HDCD mastering flag
235 // Extension audio descriptor flag
236 s->ext_audio_type = get_bits(&s->gb, 3);
238 // Extended coding flag
239 s->ext_audio_present = get_bits1(&s->gb);
241 // Audio sync word insertion flag
242 s->sync_ssf = get_bits1(&s->gb);
244 // Low frequency effects flag
245 s->lfe_present = get_bits(&s->gb, 2);
246 if (s->lfe_present == LFE_FLAG_INVALID) {
247 av_log(s->avctx, AV_LOG_ERROR, "Invalid low frequency effects flag\n");
248 return AVERROR_INVALIDDATA;
251 // Predictor history flag switch
252 s->predictor_history = get_bits1(&s->gb);
254 // Header CRC check bytes
256 skip_bits(&s->gb, 16);
258 // Multirate interpolator switch
259 s->filter_perfect = get_bits1(&s->gb);
261 // Encoder software revision
262 skip_bits(&s->gb, 4);
265 skip_bits(&s->gb, 2);
267 // Source PCM resolution
268 s->source_pcm_res = ff_dca_bits_per_sample[pcmr_index = get_bits(&s->gb, 3)];
269 if (!s->source_pcm_res) {
270 av_log(s->avctx, AV_LOG_ERROR, "Invalid source PCM resolution\n");
271 return AVERROR_INVALIDDATA;
273 s->es_format = pcmr_index & 1;
275 // Front sum/difference flag
276 s->sumdiff_front = get_bits1(&s->gb);
278 // Surround sum/difference flag
279 s->sumdiff_surround = get_bits1(&s->gb);
281 // Dialog normalization / unspecified
282 skip_bits(&s->gb, 4);
287 // 5.3.2 - Primary audio coding header
288 static int parse_coding_header(DCACoreDecoder *s, enum HeaderType header, int xch_base)
290 int n, ch, nchannels, header_size = 0, header_pos = get_bits_count(&s->gb);
291 unsigned int mask, index;
293 if (get_bits_left(&s->gb) < 0)
294 return AVERROR_INVALIDDATA;
298 // Number of subframes
299 s->nsubframes = get_bits(&s->gb, 4) + 1;
301 // Number of primary audio channels
302 s->nchannels = get_bits(&s->gb, 3) + 1;
303 if (s->nchannels != ff_dca_channels[s->audio_mode]) {
304 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of primary audio channels (%d) for audio channel arrangement (%d)\n", s->nchannels, s->audio_mode);
305 return AVERROR_INVALIDDATA;
307 av_assert1(s->nchannels <= DCA_CHANNELS - 2);
309 s->ch_mask = audio_mode_ch_mask[s->audio_mode];
311 // Add LFE channel if present
313 s->ch_mask |= DCA_SPEAKER_MASK_LFE1;
317 s->nchannels = ff_dca_channels[s->audio_mode] + 1;
318 av_assert1(s->nchannels <= DCA_CHANNELS - 1);
319 s->ch_mask |= DCA_SPEAKER_MASK_Cs;
323 // Channel set header length
324 header_size = get_bits(&s->gb, 7) + 1;
327 if (s->xxch_crc_present
328 && (s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
329 && ff_dca_check_crc(&s->gb, header_pos, header_pos + header_size * 8)) {
330 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH channel set header checksum\n");
331 return AVERROR_INVALIDDATA;
334 // Number of channels in a channel set
335 nchannels = get_bits(&s->gb, 3) + 1;
336 if (nchannels > DCA_XXCH_CHANNELS_MAX) {
337 avpriv_request_sample(s->avctx, "%d XXCH channels", nchannels);
338 return AVERROR_PATCHWELCOME;
340 s->nchannels = ff_dca_channels[s->audio_mode] + nchannels;
341 av_assert1(s->nchannels <= DCA_CHANNELS);
343 // Loudspeaker layout mask
344 mask = get_bits_long(&s->gb, s->xxch_mask_nbits - DCA_SPEAKER_Cs);
345 s->xxch_spkr_mask = mask << DCA_SPEAKER_Cs;
347 if (av_popcount(s->xxch_spkr_mask) != nchannels) {
348 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH speaker layout mask (%#x)\n", s->xxch_spkr_mask);
349 return AVERROR_INVALIDDATA;
352 if (s->xxch_core_mask & s->xxch_spkr_mask) {
353 av_log(s->avctx, AV_LOG_ERROR, "XXCH speaker layout mask (%#x) overlaps with core (%#x)\n", s->xxch_spkr_mask, s->xxch_core_mask);
354 return AVERROR_INVALIDDATA;
357 // Combine core and XXCH masks together
358 s->ch_mask = s->xxch_core_mask | s->xxch_spkr_mask;
360 // Downmix coefficients present in stream
361 if (get_bits1(&s->gb)) {
362 int *coeff_ptr = s->xxch_dmix_coeff;
364 // Downmix already performed by encoder
365 s->xxch_dmix_embedded = get_bits1(&s->gb);
367 // Downmix scale factor
368 index = get_bits(&s->gb, 6) * 4 - FF_DCA_DMIXTABLE_OFFSET - 3;
369 if (index >= FF_DCA_INV_DMIXTABLE_SIZE) {
370 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix scale index (%d)\n", index);
371 return AVERROR_INVALIDDATA;
373 s->xxch_dmix_scale_inv = ff_dca_inv_dmixtable[index];
375 // Downmix channel mapping mask
376 for (ch = 0; ch < nchannels; ch++) {
377 mask = get_bits_long(&s->gb, s->xxch_mask_nbits);
378 if ((mask & s->xxch_core_mask) != mask) {
379 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix channel mapping mask (%#x)\n", mask);
380 return AVERROR_INVALIDDATA;
382 s->xxch_dmix_mask[ch] = mask;
385 // Downmix coefficients
386 for (ch = 0; ch < nchannels; ch++) {
387 for (n = 0; n < s->xxch_mask_nbits; n++) {
388 if (s->xxch_dmix_mask[ch] & (1U << n)) {
389 int code = get_bits(&s->gb, 7);
390 int sign = (code >> 6) - 1;
392 index = code * 4 - 3;
393 if (index >= FF_DCA_DMIXTABLE_SIZE) {
394 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix coefficient index (%d)\n", index);
395 return AVERROR_INVALIDDATA;
397 *coeff_ptr++ = (ff_dca_dmixtable[index] ^ sign) - sign;
405 s->xxch_dmix_embedded = 0;
411 // Subband activity count
412 for (ch = xch_base; ch < s->nchannels; ch++) {
413 s->nsubbands[ch] = get_bits(&s->gb, 5) + 2;
414 if (s->nsubbands[ch] > DCA_SUBBANDS) {
415 av_log(s->avctx, AV_LOG_ERROR, "Invalid subband activity count\n");
416 return AVERROR_INVALIDDATA;
420 // High frequency VQ start subband
421 for (ch = xch_base; ch < s->nchannels; ch++)
422 s->subband_vq_start[ch] = get_bits(&s->gb, 5) + 1;
424 // Joint intensity coding index
425 for (ch = xch_base; ch < s->nchannels; ch++) {
426 if ((n = get_bits(&s->gb, 3)) && header == HEADER_XXCH)
428 if (n > s->nchannels) {
429 av_log(s->avctx, AV_LOG_ERROR, "Invalid joint intensity coding index\n");
430 return AVERROR_INVALIDDATA;
432 s->joint_intensity_index[ch] = n;
435 // Transient mode code book
436 for (ch = xch_base; ch < s->nchannels; ch++)
437 s->transition_mode_sel[ch] = get_bits(&s->gb, 2);
439 // Scale factor code book
440 for (ch = xch_base; ch < s->nchannels; ch++) {
441 s->scale_factor_sel[ch] = get_bits(&s->gb, 3);
442 if (s->scale_factor_sel[ch] == 7) {
443 av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor code book\n");
444 return AVERROR_INVALIDDATA;
448 // Bit allocation quantizer select
449 for (ch = xch_base; ch < s->nchannels; ch++) {
450 s->bit_allocation_sel[ch] = get_bits(&s->gb, 3);
451 if (s->bit_allocation_sel[ch] == 7) {
452 av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation quantizer select\n");
453 return AVERROR_INVALIDDATA;
457 // Quantization index codebook select
458 for (n = 0; n < DCA_CODE_BOOKS; n++)
459 for (ch = xch_base; ch < s->nchannels; ch++)
460 s->quant_index_sel[ch][n] = get_bits(&s->gb, quant_index_sel_nbits[n]);
462 // Scale factor adjustment index
463 for (n = 0; n < DCA_CODE_BOOKS; n++)
464 for (ch = xch_base; ch < s->nchannels; ch++)
465 if (s->quant_index_sel[ch][n] < quant_index_group_size[n])
466 s->scale_factor_adj[ch][n] = ff_dca_scale_factor_adj[get_bits(&s->gb, 2)];
468 if (header == HEADER_XXCH) {
471 // CRC16 of channel set header
472 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
473 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set header\n");
474 return AVERROR_INVALIDDATA;
477 // Audio header CRC check word
479 skip_bits(&s->gb, 16);
485 static inline int parse_scale(DCACoreDecoder *s, int *scale_index, int sel)
487 const uint32_t *scale_table;
488 unsigned int scale_size;
490 // Select the root square table
492 scale_table = ff_dca_scale_factor_quant7;
493 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7);
495 scale_table = ff_dca_scale_factor_quant6;
496 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6);
499 // If Huffman code was used, the difference of scales was encoded
501 *scale_index += dca_get_vlc(&s->gb, &vlc_scale_factor, sel);
503 *scale_index = get_bits(&s->gb, sel + 1);
505 // Look up scale factor from the root square table
506 if ((unsigned int)*scale_index >= scale_size) {
507 av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor index\n");
508 return AVERROR_INVALIDDATA;
511 return scale_table[*scale_index];
514 static inline int parse_joint_scale(DCACoreDecoder *s, int sel)
518 // Absolute value was encoded even when Huffman code was used
520 scale_index = dca_get_vlc(&s->gb, &vlc_scale_factor, sel);
522 scale_index = get_bits(&s->gb, sel + 1);
527 // Look up joint scale factor
528 if ((unsigned int)scale_index >= FF_ARRAY_ELEMS(ff_dca_joint_scale_factors)) {
529 av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor index\n");
530 return AVERROR_INVALIDDATA;
533 return ff_dca_joint_scale_factors[scale_index];
536 // 5.4.1 - Primary audio coding side information
537 static int parse_subframe_header(DCACoreDecoder *s, int sf,
538 enum HeaderType header, int xch_base)
542 if (get_bits_left(&s->gb) < 0)
543 return AVERROR_INVALIDDATA;
545 if (header == HEADER_CORE) {
547 s->nsubsubframes[sf] = get_bits(&s->gb, 2) + 1;
549 // Partial subsubframe sample count
550 skip_bits(&s->gb, 3);
554 for (ch = xch_base; ch < s->nchannels; ch++)
555 for (band = 0; band < s->nsubbands[ch]; band++)
556 s->prediction_mode[ch][band] = get_bits1(&s->gb);
558 // Prediction coefficients VQ address
559 for (ch = xch_base; ch < s->nchannels; ch++)
560 for (band = 0; band < s->nsubbands[ch]; band++)
561 if (s->prediction_mode[ch][band])
562 s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12);
564 // Bit allocation index
565 for (ch = xch_base; ch < s->nchannels; ch++) {
566 int sel = s->bit_allocation_sel[ch];
568 for (band = 0; band < s->subband_vq_start[ch]; band++) {
572 abits = dca_get_vlc(&s->gb, &vlc_bit_allocation, sel);
574 abits = get_bits(&s->gb, sel - 1);
576 if (abits > DCA_ABITS_MAX) {
577 av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation index\n");
578 return AVERROR_INVALIDDATA;
581 s->bit_allocation[ch][band] = abits;
586 for (ch = xch_base; ch < s->nchannels; ch++) {
587 // Clear transition mode for all subbands
588 memset(s->transition_mode[sf][ch], 0, sizeof(s->transition_mode[0][0]));
590 // Transient possible only if more than one subsubframe
591 if (s->nsubsubframes[sf] > 1) {
592 int sel = s->transition_mode_sel[ch];
593 for (band = 0; band < s->subband_vq_start[ch]; band++)
594 if (s->bit_allocation[ch][band])
595 s->transition_mode[sf][ch][band] = dca_get_vlc(&s->gb, &vlc_transition_mode, sel);
600 for (ch = xch_base; ch < s->nchannels; ch++) {
601 int sel = s->scale_factor_sel[ch];
604 // Extract scales for subbands up to VQ
605 for (band = 0; band < s->subband_vq_start[ch]; band++) {
606 if (s->bit_allocation[ch][band]) {
607 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
609 s->scale_factors[ch][band][0] = ret;
610 if (s->transition_mode[sf][ch][band]) {
611 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
613 s->scale_factors[ch][band][1] = ret;
616 s->scale_factors[ch][band][0] = 0;
620 // High frequency VQ subbands
621 for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++) {
622 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
624 s->scale_factors[ch][band][0] = ret;
628 // Joint subband codebook select
629 for (ch = xch_base; ch < s->nchannels; ch++) {
630 if (s->joint_intensity_index[ch]) {
631 s->joint_scale_sel[ch] = get_bits(&s->gb, 3);
632 if (s->joint_scale_sel[ch] == 7) {
633 av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor code book\n");
634 return AVERROR_INVALIDDATA;
639 // Scale factors for joint subband coding
640 for (ch = xch_base; ch < s->nchannels; ch++) {
641 int src_ch = s->joint_intensity_index[ch] - 1;
643 int sel = s->joint_scale_sel[ch];
644 for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) {
645 if ((ret = parse_joint_scale(s, sel)) < 0)
647 s->joint_scale_factors[ch][band] = ret;
652 // Dynamic range coefficient
653 if (s->drc_present && header == HEADER_CORE)
654 skip_bits(&s->gb, 8);
656 // Side information CRC check word
658 skip_bits(&s->gb, 16);
663 #ifndef decode_blockcodes
664 static inline int decode_blockcodes(int code1, int code2, int levels, int32_t *audio)
666 int offset = (levels - 1) / 2;
669 for (n = 0; n < DCA_SUBBAND_SAMPLES / 2; n++) {
670 div = FASTDIV(code1, levels);
671 audio[n] = code1 - div * levels - offset;
674 for (; n < DCA_SUBBAND_SAMPLES; n++) {
675 div = FASTDIV(code2, levels);
676 audio[n] = code2 - div * levels - offset;
680 return code1 | code2;
684 static inline int parse_block_codes(DCACoreDecoder *s, int32_t *audio, int abits)
686 // Extract block code indices from the bit stream
687 int code1 = get_bits(&s->gb, block_code_nbits[abits - 1]);
688 int code2 = get_bits(&s->gb, block_code_nbits[abits - 1]);
689 int levels = ff_dca_quant_levels[abits];
691 // Look up samples from the block code book
692 if (decode_blockcodes(code1, code2, levels, audio)) {
693 av_log(s->avctx, AV_LOG_ERROR, "Failed to decode block code(s)\n");
694 return AVERROR_INVALIDDATA;
700 static inline int parse_huffman_codes(DCACoreDecoder *s, int32_t *audio, int abits, int sel)
704 // Extract Huffman codes from the bit stream
705 for (i = 0; i < DCA_SUBBAND_SAMPLES; i++)
706 audio[i] = dca_get_vlc(&s->gb, &vlc_quant_index[abits - 1], sel);
711 static inline int extract_audio(DCACoreDecoder *s, int32_t *audio, int abits, int ch)
713 av_assert1(abits >= 0 && abits <= DCA_ABITS_MAX);
717 memset(audio, 0, DCA_SUBBAND_SAMPLES * sizeof(*audio));
721 if (abits <= DCA_CODE_BOOKS) {
722 int sel = s->quant_index_sel[ch][abits - 1];
723 if (sel < quant_index_group_size[abits - 1]) {
725 return parse_huffman_codes(s, audio, abits, sel);
729 return parse_block_codes(s, audio, abits);
733 // No further encoding
734 get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3);
738 static inline void dequantize(int32_t *output, const int32_t *input,
739 int32_t step_size, int32_t scale, int residual)
741 // Account for quantizer step size
742 int64_t step_scale = (int64_t)step_size * scale;
745 // Limit scale factor resolution to 22 bits
746 if (step_scale > (1 << 23)) {
747 shift = av_log2(step_scale >> 23) + 1;
748 step_scale >>= shift;
753 for (n = 0; n < DCA_SUBBAND_SAMPLES; n++)
754 output[n] += clip23(norm__(input[n] * step_scale, 22 - shift));
756 for (n = 0; n < DCA_SUBBAND_SAMPLES; n++)
757 output[n] = clip23(norm__(input[n] * step_scale, 22 - shift));
761 static inline void inverse_adpcm(int32_t **subband_samples,
762 const int16_t *vq_index,
763 const int8_t *prediction_mode,
764 int sb_start, int sb_end,
769 for (i = sb_start; i < sb_end; i++) {
770 if (prediction_mode[i]) {
771 const int16_t *coeff = ff_dca_adpcm_vb[vq_index[i]];
772 int32_t *ptr = subband_samples[i] + ofs;
773 for (j = 0; j < len; j++) {
775 for (k = 0; k < DCA_ADPCM_COEFFS; k++)
776 err += (int64_t)ptr[j - k - 1] * coeff[k];
777 ptr[j] = clip23(ptr[j] + clip23(norm13(err)));
783 // 5.5 - Primary audio data arrays
784 static int parse_subframe_audio(DCACoreDecoder *s, int sf, enum HeaderType header,
785 int xch_base, int *sub_pos, int *lfe_pos)
787 int32_t audio[16], scale;
788 int n, ssf, ofs, ch, band;
790 // Check number of subband samples in this subframe
791 int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES;
792 if (*sub_pos + nsamples > s->npcmblocks) {
793 av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
794 return AVERROR_INVALIDDATA;
797 if (get_bits_left(&s->gb) < 0)
798 return AVERROR_INVALIDDATA;
800 // VQ encoded subbands
801 for (ch = xch_base; ch < s->nchannels; ch++) {
802 int32_t vq_index[DCA_SUBBANDS];
804 for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++)
805 // Extract the VQ address from the bit stream
806 vq_index[band] = get_bits(&s->gb, 10);
808 if (s->subband_vq_start[ch] < s->nsubbands[ch]) {
809 s->dcadsp->decode_hf(s->subband_samples[ch], vq_index,
810 ff_dca_high_freq_vq, s->scale_factors[ch],
811 s->subband_vq_start[ch], s->nsubbands[ch],
816 // Low frequency effect data
817 if (s->lfe_present && header == HEADER_CORE) {
820 // Determine number of LFE samples in this subframe
821 int nlfesamples = 2 * s->lfe_present * s->nsubsubframes[sf];
822 av_assert1((unsigned int)nlfesamples <= FF_ARRAY_ELEMS(audio));
824 // Extract LFE samples from the bit stream
825 get_array(&s->gb, audio, nlfesamples, 8);
827 // Extract scale factor index from the bit stream
828 index = get_bits(&s->gb, 8);
829 if (index >= FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7)) {
830 av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE scale factor index\n");
831 return AVERROR_INVALIDDATA;
834 // Look up the 7-bit root square quantization table
835 scale = ff_dca_scale_factor_quant7[index];
837 // Account for quantizer step size which is 0.035
838 scale = mul23(4697620 /* 0.035 * (1 << 27) */, scale);
840 // Scale and take the LFE samples
841 for (n = 0, ofs = *lfe_pos; n < nlfesamples; n++, ofs++)
842 s->lfe_samples[ofs] = clip23(audio[n] * scale >> 4);
844 // Advance LFE sample pointer for the next subframe
849 for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
850 for (ch = xch_base; ch < s->nchannels; ch++) {
851 if (get_bits_left(&s->gb) < 0)
852 return AVERROR_INVALIDDATA;
854 // Not high frequency VQ subbands
855 for (band = 0; band < s->subband_vq_start[ch]; band++) {
856 int ret, trans_ssf, abits = s->bit_allocation[ch][band];
859 // Extract bits from the bit stream
860 if ((ret = extract_audio(s, audio, abits, ch)) < 0)
863 // Select quantization step size table and look up
864 // quantization step size
865 if (s->bit_rate == 3)
866 step_size = ff_dca_lossless_quant[abits];
868 step_size = ff_dca_lossy_quant[abits];
870 // Identify transient location
871 trans_ssf = s->transition_mode[sf][ch][band];
873 // Determine proper scale factor
874 if (trans_ssf == 0 || ssf < trans_ssf)
875 scale = s->scale_factors[ch][band][0];
877 scale = s->scale_factors[ch][band][1];
879 // Adjust scale factor when SEL indicates Huffman code
881 int64_t adj = s->scale_factor_adj[ch][abits - 1];
882 scale = clip23(adj * scale >> 22);
885 dequantize(s->subband_samples[ch][band] + ofs,
886 audio, step_size, scale, 0);
891 if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
892 av_log(s->avctx, AV_LOG_ERROR, "DSYNC check failed\n");
893 return AVERROR_INVALIDDATA;
896 ofs += DCA_SUBBAND_SAMPLES;
900 for (ch = xch_base; ch < s->nchannels; ch++) {
901 inverse_adpcm(s->subband_samples[ch], s->prediction_vq_index[ch],
902 s->prediction_mode[ch], 0, s->nsubbands[ch],
906 // Joint subband coding
907 for (ch = xch_base; ch < s->nchannels; ch++) {
908 int src_ch = s->joint_intensity_index[ch] - 1;
910 s->dcadsp->decode_joint(s->subband_samples[ch], s->subband_samples[src_ch],
911 s->joint_scale_factors[ch], s->nsubbands[ch],
912 s->nsubbands[src_ch], *sub_pos, nsamples);
916 // Advance subband sample pointer for the next subframe
921 static void erase_adpcm_history(DCACoreDecoder *s)
925 // Erase ADPCM history from previous frame if
926 // predictor history switch was disabled
927 for (ch = 0; ch < DCA_CHANNELS; ch++)
928 for (band = 0; band < DCA_SUBBANDS; band++)
929 AV_ZERO128(s->subband_samples[ch][band] - DCA_ADPCM_COEFFS);
934 static int alloc_sample_buffer(DCACoreDecoder *s)
936 int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks;
937 int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS;
938 int nlfesamples = DCA_LFE_HISTORY + s->npcmblocks / 2;
939 unsigned int size = s->subband_size;
942 // Reallocate subband sample buffer
943 av_fast_mallocz(&s->subband_buffer, &s->subband_size,
944 (nframesamples + nlfesamples) * sizeof(int32_t));
945 if (!s->subband_buffer)
946 return AVERROR(ENOMEM);
948 if (size != s->subband_size) {
949 for (ch = 0; ch < DCA_CHANNELS; ch++)
950 for (band = 0; band < DCA_SUBBANDS; band++)
951 s->subband_samples[ch][band] = s->subband_buffer +
952 (ch * DCA_SUBBANDS + band) * nchsamples + DCA_ADPCM_COEFFS;
953 s->lfe_samples = s->subband_buffer + nframesamples;
956 if (!s->predictor_history)
957 erase_adpcm_history(s);
962 static int parse_frame_data(DCACoreDecoder *s, enum HeaderType header, int xch_base)
964 int sf, ch, ret, band, sub_pos, lfe_pos;
966 if ((ret = parse_coding_header(s, header, xch_base)) < 0)
969 for (sf = 0, sub_pos = 0, lfe_pos = DCA_LFE_HISTORY; sf < s->nsubframes; sf++) {
970 if ((ret = parse_subframe_header(s, sf, header, xch_base)) < 0)
972 if ((ret = parse_subframe_audio(s, sf, header, xch_base, &sub_pos, &lfe_pos)) < 0)
976 for (ch = xch_base; ch < s->nchannels; ch++) {
977 // Determine number of active subbands for this channel
978 int nsubbands = s->nsubbands[ch];
979 if (s->joint_intensity_index[ch])
980 nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]);
982 // Update history for ADPCM
983 for (band = 0; band < nsubbands; band++) {
984 int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS;
985 AV_COPY128(samples, samples + s->npcmblocks);
988 // Clear inactive subbands
989 for (; band < DCA_SUBBANDS; band++) {
990 int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS;
991 memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t));
1000 static int parse_xch_frame(DCACoreDecoder *s)
1004 if (s->ch_mask & DCA_SPEAKER_MASK_Cs) {
1005 av_log(s->avctx, AV_LOG_ERROR, "XCH with Cs speaker already present\n");
1006 return AVERROR_INVALIDDATA;
1009 if ((ret = parse_frame_data(s, HEADER_XCH, s->nchannels)) < 0)
1012 // Seek to the end of core frame, don't trust XCH frame size
1013 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1014 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XCH frame\n");
1015 return AVERROR_INVALIDDATA;
1021 static int parse_xxch_frame(DCACoreDecoder *s)
1023 int xxch_nchsets, xxch_frame_size;
1024 int ret, mask, header_size, header_pos = get_bits_count(&s->gb);
1027 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XXCH) {
1028 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH sync word\n");
1029 return AVERROR_INVALIDDATA;
1032 // XXCH frame header length
1033 header_size = get_bits(&s->gb, 6) + 1;
1035 // Check XXCH frame header CRC
1036 if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
1037 && ff_dca_check_crc(&s->gb, header_pos + 32, header_pos + header_size * 8)) {
1038 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH frame header checksum\n");
1039 return AVERROR_INVALIDDATA;
1042 // CRC presence flag for channel set header
1043 s->xxch_crc_present = get_bits1(&s->gb);
1045 // Number of bits for loudspeaker mask
1046 s->xxch_mask_nbits = get_bits(&s->gb, 5) + 1;
1047 if (s->xxch_mask_nbits <= DCA_SPEAKER_Cs) {
1048 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XXCH speaker mask (%d)\n", s->xxch_mask_nbits);
1049 return AVERROR_INVALIDDATA;
1052 // Number of channel sets
1053 xxch_nchsets = get_bits(&s->gb, 2) + 1;
1054 if (xxch_nchsets > 1) {
1055 avpriv_request_sample(s->avctx, "%d XXCH channel sets", xxch_nchsets);
1056 return AVERROR_PATCHWELCOME;
1059 // Channel set 0 data byte size
1060 xxch_frame_size = get_bits(&s->gb, 14) + 1;
1062 // Core loudspeaker activity mask
1063 s->xxch_core_mask = get_bits_long(&s->gb, s->xxch_mask_nbits);
1065 // Validate the core mask
1068 if ((mask & DCA_SPEAKER_MASK_Ls) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss))
1069 mask = (mask & ~DCA_SPEAKER_MASK_Ls) | DCA_SPEAKER_MASK_Lss;
1071 if ((mask & DCA_SPEAKER_MASK_Rs) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss))
1072 mask = (mask & ~DCA_SPEAKER_MASK_Rs) | DCA_SPEAKER_MASK_Rss;
1074 if (mask != s->xxch_core_mask) {
1075 av_log(s->avctx, AV_LOG_ERROR, "XXCH core speaker activity mask (%#x) disagrees with core (%#x)\n", s->xxch_core_mask, mask);
1076 return AVERROR_INVALIDDATA;
1081 // CRC16 of XXCH frame header
1082 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1083 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH frame header\n");
1084 return AVERROR_INVALIDDATA;
1087 // Parse XXCH channel set 0
1088 if ((ret = parse_frame_data(s, HEADER_XXCH, s->nchannels)) < 0)
1091 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8 + xxch_frame_size * 8)) {
1092 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set\n");
1093 return AVERROR_INVALIDDATA;
1099 static int parse_xbr_subframe(DCACoreDecoder *s, int xbr_base_ch, int xbr_nchannels,
1100 int *xbr_nsubbands, int xbr_transition_mode, int sf, int *sub_pos)
1102 int xbr_nabits[DCA_CHANNELS];
1103 int xbr_bit_allocation[DCA_CHANNELS][DCA_SUBBANDS];
1104 int xbr_scale_nbits[DCA_CHANNELS];
1105 int32_t xbr_scale_factors[DCA_CHANNELS][DCA_SUBBANDS][2];
1106 int ssf, ch, band, ofs;
1108 // Check number of subband samples in this subframe
1109 if (*sub_pos + s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES > s->npcmblocks) {
1110 av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
1111 return AVERROR_INVALIDDATA;
1114 if (get_bits_left(&s->gb) < 0)
1115 return AVERROR_INVALIDDATA;
1117 // Number of bits for XBR bit allocation index
1118 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++)
1119 xbr_nabits[ch] = get_bits(&s->gb, 2) + 2;
1121 // XBR bit allocation index
1122 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1123 for (band = 0; band < xbr_nsubbands[ch]; band++) {
1124 xbr_bit_allocation[ch][band] = get_bits(&s->gb, xbr_nabits[ch]);
1125 if (xbr_bit_allocation[ch][band] > DCA_ABITS_MAX) {
1126 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR bit allocation index\n");
1127 return AVERROR_INVALIDDATA;
1132 // Number of bits for scale indices
1133 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1134 xbr_scale_nbits[ch] = get_bits(&s->gb, 3);
1135 if (!xbr_scale_nbits[ch]) {
1136 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XBR scale factor index\n");
1137 return AVERROR_INVALIDDATA;
1141 // XBR scale factors
1142 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1143 const uint32_t *scale_table;
1146 // Select the root square table
1147 if (s->scale_factor_sel[ch] > 5) {
1148 scale_table = ff_dca_scale_factor_quant7;
1149 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7);
1151 scale_table = ff_dca_scale_factor_quant6;
1152 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6);
1155 // Parse scale factor indices and look up scale factors from the root
1157 for (band = 0; band < xbr_nsubbands[ch]; band++) {
1158 if (xbr_bit_allocation[ch][band]) {
1159 int scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
1160 if (scale_index >= scale_size) {
1161 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
1162 return AVERROR_INVALIDDATA;
1164 xbr_scale_factors[ch][band][0] = scale_table[scale_index];
1165 if (xbr_transition_mode && s->transition_mode[sf][ch][band]) {
1166 scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
1167 if (scale_index >= scale_size) {
1168 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
1169 return AVERROR_INVALIDDATA;
1171 xbr_scale_factors[ch][band][1] = scale_table[scale_index];
1178 for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
1179 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1180 if (get_bits_left(&s->gb) < 0)
1181 return AVERROR_INVALIDDATA;
1183 for (band = 0; band < xbr_nsubbands[ch]; band++) {
1184 int ret, trans_ssf, abits = xbr_bit_allocation[ch][band];
1185 int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale;
1187 // Extract bits from the bit stream
1189 // No further encoding
1190 get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3);
1191 } else if (abits > 0) {
1193 if ((ret = parse_block_codes(s, audio, abits)) < 0)
1196 // No bits allocated
1200 // Look up quantization step size
1201 step_size = ff_dca_lossless_quant[abits];
1203 // Identify transient location
1204 if (xbr_transition_mode)
1205 trans_ssf = s->transition_mode[sf][ch][band];
1209 // Determine proper scale factor
1210 if (trans_ssf == 0 || ssf < trans_ssf)
1211 scale = xbr_scale_factors[ch][band][0];
1213 scale = xbr_scale_factors[ch][band][1];
1215 dequantize(s->subband_samples[ch][band] + ofs,
1216 audio, step_size, scale, 1);
1221 if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
1222 av_log(s->avctx, AV_LOG_ERROR, "XBR-DSYNC check failed\n");
1223 return AVERROR_INVALIDDATA;
1226 ofs += DCA_SUBBAND_SAMPLES;
1229 // Advance subband sample pointer for the next subframe
1234 static int parse_xbr_frame(DCACoreDecoder *s)
1236 int xbr_frame_size[DCA_EXSS_CHSETS_MAX];
1237 int xbr_nchannels[DCA_EXSS_CHSETS_MAX];
1238 int xbr_nsubbands[DCA_EXSS_CHSETS_MAX * DCA_EXSS_CHANNELS_MAX];
1239 int xbr_nchsets, xbr_transition_mode, xbr_band_nbits, xbr_base_ch;
1240 int i, ch1, ch2, ret, header_size, header_pos = get_bits_count(&s->gb);
1243 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XBR) {
1244 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR sync word\n");
1245 return AVERROR_INVALIDDATA;
1248 // XBR frame header length
1249 header_size = get_bits(&s->gb, 6) + 1;
1251 // Check XBR frame header CRC
1252 if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
1253 && ff_dca_check_crc(&s->gb, header_pos + 32, header_pos + header_size * 8)) {
1254 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR frame header checksum\n");
1255 return AVERROR_INVALIDDATA;
1258 // Number of channel sets
1259 xbr_nchsets = get_bits(&s->gb, 2) + 1;
1261 // Channel set data byte size
1262 for (i = 0; i < xbr_nchsets; i++)
1263 xbr_frame_size[i] = get_bits(&s->gb, 14) + 1;
1265 // Transition mode flag
1266 xbr_transition_mode = get_bits1(&s->gb);
1268 // Channel set headers
1269 for (i = 0, ch2 = 0; i < xbr_nchsets; i++) {
1270 xbr_nchannels[i] = get_bits(&s->gb, 3) + 1;
1271 xbr_band_nbits = get_bits(&s->gb, 2) + 5;
1272 for (ch1 = 0; ch1 < xbr_nchannels[i]; ch1++, ch2++) {
1273 xbr_nsubbands[ch2] = get_bits(&s->gb, xbr_band_nbits) + 1;
1274 if (xbr_nsubbands[ch2] > DCA_SUBBANDS) {
1275 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of active XBR subbands (%d)\n", xbr_nsubbands[ch2]);
1276 return AVERROR_INVALIDDATA;
1283 // CRC16 of XBR frame header
1284 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1285 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR frame header\n");
1286 return AVERROR_INVALIDDATA;
1290 for (i = 0, xbr_base_ch = 0; i < xbr_nchsets; i++) {
1291 header_pos = get_bits_count(&s->gb);
1293 if (xbr_base_ch + xbr_nchannels[i] <= s->nchannels) {
1296 for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) {
1297 if ((ret = parse_xbr_subframe(s, xbr_base_ch,
1298 xbr_base_ch + xbr_nchannels[i],
1299 xbr_nsubbands, xbr_transition_mode,
1305 xbr_base_ch += xbr_nchannels[i];
1307 if (ff_dca_seek_bits(&s->gb, header_pos + xbr_frame_size[i] * 8)) {
1308 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR channel set\n");
1309 return AVERROR_INVALIDDATA;
1316 // Modified ISO/IEC 9899 linear congruential generator
1317 // Returns pseudorandom integer in range [-2^30, 2^30 - 1]
1318 static int rand_x96(DCACoreDecoder *s)
1320 s->x96_rand = 1103515245U * s->x96_rand + 12345U;
1321 return (s->x96_rand & 0x7fffffff) - 0x40000000;
1324 static int parse_x96_subframe_audio(DCACoreDecoder *s, int sf, int xch_base, int *sub_pos)
1326 int n, ssf, ch, band, ofs;
1328 // Check number of subband samples in this subframe
1329 int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES;
1330 if (*sub_pos + nsamples > s->npcmblocks) {
1331 av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
1332 return AVERROR_INVALIDDATA;
1335 if (get_bits_left(&s->gb) < 0)
1336 return AVERROR_INVALIDDATA;
1338 // VQ encoded or unallocated subbands
1339 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1340 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1341 // Get the sample pointer and scale factor
1342 int32_t *samples = s->x96_subband_samples[ch][band] + *sub_pos;
1343 int32_t scale = s->scale_factors[ch][band >> 1][band & 1];
1345 switch (s->bit_allocation[ch][band]) {
1346 case 0: // No bits allocated for subband
1348 memset(samples, 0, nsamples * sizeof(int32_t));
1349 else for (n = 0; n < nsamples; n++)
1350 // Generate scaled random samples
1351 samples[n] = mul31(rand_x96(s), scale);
1354 case 1: // VQ encoded subband
1355 for (ssf = 0; ssf < (s->nsubsubframes[sf] + 1) / 2; ssf++) {
1356 // Extract the VQ address from the bit stream and look up
1357 // the VQ code book for up to 16 subband samples
1358 const int8_t *vq_samples = ff_dca_high_freq_vq[get_bits(&s->gb, 10)];
1359 // Scale and take the samples
1360 for (n = 0; n < FFMIN(nsamples - ssf * 16, 16); n++)
1361 *samples++ = clip23(vq_samples[n] * scale + (1 << 3) >> 4);
1369 for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
1370 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1371 if (get_bits_left(&s->gb) < 0)
1372 return AVERROR_INVALIDDATA;
1374 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1375 int ret, abits = s->bit_allocation[ch][band] - 1;
1376 int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale;
1378 // Not VQ encoded or unallocated subbands
1382 // Extract bits from the bit stream
1383 if ((ret = extract_audio(s, audio, abits, ch)) < 0)
1386 // Select quantization step size table and look up quantization
1388 if (s->bit_rate == 3)
1389 step_size = ff_dca_lossless_quant[abits];
1391 step_size = ff_dca_lossy_quant[abits];
1393 // Get the scale factor
1394 scale = s->scale_factors[ch][band >> 1][band & 1];
1396 dequantize(s->x96_subband_samples[ch][band] + ofs,
1397 audio, step_size, scale, 0);
1402 if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
1403 av_log(s->avctx, AV_LOG_ERROR, "X96-DSYNC check failed\n");
1404 return AVERROR_INVALIDDATA;
1407 ofs += DCA_SUBBAND_SAMPLES;
1411 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1412 inverse_adpcm(s->x96_subband_samples[ch], s->prediction_vq_index[ch],
1413 s->prediction_mode[ch], s->x96_subband_start, s->nsubbands[ch],
1414 *sub_pos, nsamples);
1417 // Joint subband coding
1418 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1419 int src_ch = s->joint_intensity_index[ch] - 1;
1421 s->dcadsp->decode_joint(s->x96_subband_samples[ch], s->x96_subband_samples[src_ch],
1422 s->joint_scale_factors[ch], s->nsubbands[ch],
1423 s->nsubbands[src_ch], *sub_pos, nsamples);
1427 // Advance subband sample pointer for the next subframe
1432 static void erase_x96_adpcm_history(DCACoreDecoder *s)
1436 // Erase ADPCM history from previous frame if
1437 // predictor history switch was disabled
1438 for (ch = 0; ch < DCA_CHANNELS; ch++)
1439 for (band = 0; band < DCA_SUBBANDS_X96; band++)
1440 AV_ZERO128(s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS);
1445 static int alloc_x96_sample_buffer(DCACoreDecoder *s)
1447 int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks;
1448 int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS_X96;
1449 unsigned int size = s->x96_subband_size;
1452 // Reallocate subband sample buffer
1453 av_fast_mallocz(&s->x96_subband_buffer, &s->x96_subband_size,
1454 nframesamples * sizeof(int32_t));
1455 if (!s->x96_subband_buffer)
1456 return AVERROR(ENOMEM);
1458 if (size != s->x96_subband_size) {
1459 for (ch = 0; ch < DCA_CHANNELS; ch++)
1460 for (band = 0; band < DCA_SUBBANDS_X96; band++)
1461 s->x96_subband_samples[ch][band] = s->x96_subband_buffer +
1462 (ch * DCA_SUBBANDS_X96 + band) * nchsamples + DCA_ADPCM_COEFFS;
1465 if (!s->predictor_history)
1466 erase_x96_adpcm_history(s);
1471 static int parse_x96_subframe_header(DCACoreDecoder *s, int xch_base)
1475 if (get_bits_left(&s->gb) < 0)
1476 return AVERROR_INVALIDDATA;
1479 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1480 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++)
1481 s->prediction_mode[ch][band] = get_bits1(&s->gb);
1483 // Prediction coefficients VQ address
1484 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1485 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++)
1486 if (s->prediction_mode[ch][band])
1487 s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12);
1489 // Bit allocation index
1490 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1491 int sel = s->bit_allocation_sel[ch];
1494 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1495 // If Huffman code was used, the difference of abits was encoded
1497 abits += dca_get_vlc(&s->gb, &vlc_quant_index[5 + 2 * s->x96_high_res], sel);
1499 abits = get_bits(&s->gb, 3 + s->x96_high_res);
1501 if (abits < 0 || abits > 7 + 8 * s->x96_high_res) {
1502 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 bit allocation index\n");
1503 return AVERROR_INVALIDDATA;
1506 s->bit_allocation[ch][band] = abits;
1511 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1512 int sel = s->scale_factor_sel[ch];
1513 int scale_index = 0;
1515 // Extract scales for subbands which are transmitted even for
1516 // unallocated subbands
1517 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1518 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
1520 s->scale_factors[ch][band >> 1][band & 1] = ret;
1524 // Joint subband codebook select
1525 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1526 if (s->joint_intensity_index[ch]) {
1527 s->joint_scale_sel[ch] = get_bits(&s->gb, 3);
1528 if (s->joint_scale_sel[ch] == 7) {
1529 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint scale factor code book\n");
1530 return AVERROR_INVALIDDATA;
1535 // Scale factors for joint subband coding
1536 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1537 int src_ch = s->joint_intensity_index[ch] - 1;
1539 int sel = s->joint_scale_sel[ch];
1540 for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) {
1541 if ((ret = parse_joint_scale(s, sel)) < 0)
1543 s->joint_scale_factors[ch][band] = ret;
1548 // Side information CRC check word
1550 skip_bits(&s->gb, 16);
1555 static int parse_x96_coding_header(DCACoreDecoder *s, int exss, int xch_base)
1557 int n, ch, header_size = 0, header_pos = get_bits_count(&s->gb);
1559 if (get_bits_left(&s->gb) < 0)
1560 return AVERROR_INVALIDDATA;
1563 // Channel set header length
1564 header_size = get_bits(&s->gb, 7) + 1;
1567 if (s->x96_crc_present
1568 && (s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
1569 && ff_dca_check_crc(&s->gb, header_pos, header_pos + header_size * 8)) {
1570 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 channel set header checksum\n");
1571 return AVERROR_INVALIDDATA;
1575 // High resolution flag
1576 s->x96_high_res = get_bits1(&s->gb);
1578 // First encoded subband
1579 if (s->x96_rev_no < 8) {
1580 s->x96_subband_start = get_bits(&s->gb, 5);
1581 if (s->x96_subband_start > 27) {
1582 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband start index (%d)\n", s->x96_subband_start);
1583 return AVERROR_INVALIDDATA;
1586 s->x96_subband_start = DCA_SUBBANDS;
1589 // Subband activity count
1590 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1591 s->nsubbands[ch] = get_bits(&s->gb, 6) + 1;
1592 if (s->nsubbands[ch] < DCA_SUBBANDS) {
1593 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband activity count (%d)\n", s->nsubbands[ch]);
1594 return AVERROR_INVALIDDATA;
1598 // Joint intensity coding index
1599 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1600 if ((n = get_bits(&s->gb, 3)) && xch_base)
1602 if (n > s->x96_nchannels) {
1603 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint intensity coding index\n");
1604 return AVERROR_INVALIDDATA;
1606 s->joint_intensity_index[ch] = n;
1609 // Scale factor code book
1610 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1611 s->scale_factor_sel[ch] = get_bits(&s->gb, 3);
1612 if (s->scale_factor_sel[ch] >= 6) {
1613 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 scale factor code book\n");
1614 return AVERROR_INVALIDDATA;
1618 // Bit allocation quantizer select
1619 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1620 s->bit_allocation_sel[ch] = get_bits(&s->gb, 3);
1622 // Quantization index codebook select
1623 for (n = 0; n < 6 + 4 * s->x96_high_res; n++)
1624 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1625 s->quant_index_sel[ch][n] = get_bits(&s->gb, quant_index_sel_nbits[n]);
1630 // CRC16 of channel set header
1631 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1632 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set header\n");
1633 return AVERROR_INVALIDDATA;
1637 skip_bits(&s->gb, 16);
1643 static int parse_x96_frame_data(DCACoreDecoder *s, int exss, int xch_base)
1645 int sf, ch, ret, band, sub_pos;
1647 if ((ret = parse_x96_coding_header(s, exss, xch_base)) < 0)
1650 for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) {
1651 if ((ret = parse_x96_subframe_header(s, xch_base)) < 0)
1653 if ((ret = parse_x96_subframe_audio(s, sf, xch_base, &sub_pos)) < 0)
1657 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1658 // Determine number of active subbands for this channel
1659 int nsubbands = s->nsubbands[ch];
1660 if (s->joint_intensity_index[ch])
1661 nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]);
1663 // Update history for ADPCM and clear inactive subbands
1664 for (band = 0; band < DCA_SUBBANDS_X96; band++) {
1665 int32_t *samples = s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS;
1666 if (band >= s->x96_subband_start && band < nsubbands)
1667 AV_COPY128(samples, samples + s->npcmblocks);
1669 memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t));
1678 static int parse_x96_frame(DCACoreDecoder *s)
1683 s->x96_rev_no = get_bits(&s->gb, 4);
1684 if (s->x96_rev_no < 1 || s->x96_rev_no > 8) {
1685 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no);
1686 return AVERROR_INVALIDDATA;
1689 s->x96_crc_present = 0;
1690 s->x96_nchannels = s->nchannels;
1692 if ((ret = alloc_x96_sample_buffer(s)) < 0)
1695 if ((ret = parse_x96_frame_data(s, 0, 0)) < 0)
1698 // Seek to the end of core frame
1699 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1700 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame\n");
1701 return AVERROR_INVALIDDATA;
1707 static int parse_x96_frame_exss(DCACoreDecoder *s)
1709 int x96_frame_size[DCA_EXSS_CHSETS_MAX];
1710 int x96_nchannels[DCA_EXSS_CHSETS_MAX];
1711 int x96_nchsets, x96_base_ch;
1712 int i, ret, header_size, header_pos = get_bits_count(&s->gb);
1715 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_X96) {
1716 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 sync word\n");
1717 return AVERROR_INVALIDDATA;
1720 // X96 frame header length
1721 header_size = get_bits(&s->gb, 6) + 1;
1723 // Check X96 frame header CRC
1724 if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
1725 && ff_dca_check_crc(&s->gb, header_pos + 32, header_pos + header_size * 8)) {
1726 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 frame header checksum\n");
1727 return AVERROR_INVALIDDATA;
1731 s->x96_rev_no = get_bits(&s->gb, 4);
1732 if (s->x96_rev_no < 1 || s->x96_rev_no > 8) {
1733 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no);
1734 return AVERROR_INVALIDDATA;
1737 // CRC presence flag for channel set header
1738 s->x96_crc_present = get_bits1(&s->gb);
1740 // Number of channel sets
1741 x96_nchsets = get_bits(&s->gb, 2) + 1;
1743 // Channel set data byte size
1744 for (i = 0; i < x96_nchsets; i++)
1745 x96_frame_size[i] = get_bits(&s->gb, 12) + 1;
1747 // Number of channels in channel set
1748 for (i = 0; i < x96_nchsets; i++)
1749 x96_nchannels[i] = get_bits(&s->gb, 3) + 1;
1753 // CRC16 of X96 frame header
1754 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1755 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame header\n");
1756 return AVERROR_INVALIDDATA;
1759 if ((ret = alloc_x96_sample_buffer(s)) < 0)
1763 s->x96_nchannels = 0;
1764 for (i = 0, x96_base_ch = 0; i < x96_nchsets; i++) {
1765 header_pos = get_bits_count(&s->gb);
1767 if (x96_base_ch + x96_nchannels[i] <= s->nchannels) {
1768 s->x96_nchannels = x96_base_ch + x96_nchannels[i];
1769 if ((ret = parse_x96_frame_data(s, 1, x96_base_ch)) < 0)
1773 x96_base_ch += x96_nchannels[i];
1775 if (ff_dca_seek_bits(&s->gb, header_pos + x96_frame_size[i] * 8)) {
1776 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set\n");
1777 return AVERROR_INVALIDDATA;
1784 static int parse_aux_data(DCACoreDecoder *s)
1788 if (get_bits_left(&s->gb) < 0)
1789 return AVERROR_INVALIDDATA;
1791 // Auxiliary data byte count (can't be trusted)
1792 skip_bits(&s->gb, 6);
1795 skip_bits_long(&s->gb, -get_bits_count(&s->gb) & 31);
1797 // Auxiliary data sync word
1798 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_REV1AUX) {
1799 av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data sync word\n");
1800 return AVERROR_INVALIDDATA;
1803 aux_pos = get_bits_count(&s->gb);
1805 // Auxiliary decode time stamp flag
1806 if (get_bits1(&s->gb))
1807 skip_bits_long(&s->gb, 47);
1809 // Auxiliary dynamic downmix flag
1810 if (s->prim_dmix_embedded = get_bits1(&s->gb)) {
1813 // Auxiliary primary channel downmix type
1814 s->prim_dmix_type = get_bits(&s->gb, 3);
1815 if (s->prim_dmix_type >= DCA_DMIX_TYPE_COUNT) {
1816 av_log(s->avctx, AV_LOG_ERROR, "Invalid primary channel set downmix type\n");
1817 return AVERROR_INVALIDDATA;
1820 // Size of downmix coefficients matrix
1821 m = ff_dca_dmix_primary_nch[s->prim_dmix_type];
1822 n = ff_dca_channels[s->audio_mode] + !!s->lfe_present;
1824 // Dynamic downmix code coefficients
1825 for (i = 0; i < m * n; i++) {
1826 int code = get_bits(&s->gb, 9);
1827 int sign = (code >> 8) - 1;
1828 unsigned int index = code & 0xff;
1829 if (index >= FF_DCA_DMIXTABLE_SIZE) {
1830 av_log(s->avctx, AV_LOG_ERROR, "Invalid downmix coefficient index\n");
1831 return AVERROR_INVALIDDATA;
1833 s->prim_dmix_coeff[i] = (ff_dca_dmixtable[index] ^ sign) - sign;
1838 skip_bits(&s->gb, -get_bits_count(&s->gb) & 7);
1840 // CRC16 of auxiliary data
1841 skip_bits(&s->gb, 16);
1844 if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
1845 && ff_dca_check_crc(&s->gb, aux_pos, get_bits_count(&s->gb))) {
1846 av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data checksum\n");
1847 return AVERROR_INVALIDDATA;
1853 static int parse_optional_info(DCACoreDecoder *s)
1855 DCAContext *dca = s->avctx->priv_data;
1860 skip_bits_long(&s->gb, 32);
1863 if (s->aux_present && (ret = parse_aux_data(s)) < 0
1864 && (s->avctx->err_recognition & AV_EF_EXPLODE))
1868 s->prim_dmix_embedded = 0;
1871 if (s->ext_audio_present && !dca->core_only) {
1872 int sync_pos = FFMIN(s->frame_size / 4, s->gb.size_in_bits / 32) - 1;
1873 int last_pos = get_bits_count(&s->gb) / 32;
1876 // Search for extension sync words aligned on 4-byte boundary. Search
1877 // must be done backwards from the end of core frame to work around
1878 // sync word aliasing issues.
1879 switch (s->ext_audio_type) {
1881 if (dca->request_channel_layout)
1884 // The distance between XCH sync word and end of the core frame
1885 // must be equal to XCH frame size. Off by one error is allowed for
1886 // compatibility with legacy bitstreams. Minimum XCH frame size is
1887 // 96 bytes. AMODE and PCHS are further checked to reduce
1888 // probability of alias sync detection.
1889 for (; sync_pos >= last_pos; sync_pos--) {
1890 if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_XCH) {
1891 s->gb.index = (sync_pos + 1) * 32;
1892 size = get_bits(&s->gb, 10) + 1;
1893 dist = s->frame_size - sync_pos * 4;
1895 && (size == dist || size - 1 == dist)
1896 && get_bits(&s->gb, 7) == 0x08) {
1897 s->xch_pos = get_bits_count(&s->gb);
1903 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
1904 av_log(s->avctx, AV_LOG_ERROR, "XCH sync word not found\n");
1905 return AVERROR_INVALIDDATA;
1910 // The distance between X96 sync word and end of the core frame
1911 // must be equal to X96 frame size. Minimum X96 frame size is 96
1913 for (; sync_pos >= last_pos; sync_pos--) {
1914 if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_X96) {
1915 s->gb.index = (sync_pos + 1) * 32;
1916 size = get_bits(&s->gb, 12) + 1;
1917 dist = s->frame_size - sync_pos * 4;
1918 if (size >= 96 && size == dist) {
1919 s->x96_pos = get_bits_count(&s->gb);
1925 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
1926 av_log(s->avctx, AV_LOG_ERROR, "X96 sync word not found\n");
1927 return AVERROR_INVALIDDATA;
1931 case EXT_AUDIO_XXCH:
1932 if (dca->request_channel_layout)
1935 // XXCH frame header CRC must be valid. Minimum XXCH frame header
1936 // size is 11 bytes.
1937 for (; sync_pos >= last_pos; sync_pos--) {
1938 if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_XXCH) {
1939 s->gb.index = (sync_pos + 1) * 32;
1940 size = get_bits(&s->gb, 6) + 1;
1942 !ff_dca_check_crc(&s->gb, (sync_pos + 1) * 32,
1943 sync_pos * 32 + size * 8)) {
1944 s->xxch_pos = sync_pos * 32;
1950 if (s->avctx->err_recognition & AV_EF_EXPLODE) {
1951 av_log(s->avctx, AV_LOG_ERROR, "XXCH sync word not found\n");
1952 return AVERROR_INVALIDDATA;
1961 int ff_dca_core_parse(DCACoreDecoder *s, uint8_t *data, int size)
1965 s->ext_audio_mask = 0;
1966 s->xch_pos = s->xxch_pos = s->x96_pos = 0;
1968 if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
1971 skip_bits_long(&s->gb, 32);
1972 if ((ret = parse_frame_header(s)) < 0)
1974 if ((ret = alloc_sample_buffer(s)) < 0)
1976 if ((ret = parse_frame_data(s, HEADER_CORE, 0)) < 0)
1978 if ((ret = parse_optional_info(s)) < 0)
1981 // Workaround for DTS in WAV
1982 if (s->frame_size > size && s->frame_size < size + 4) {
1983 av_log(s->avctx, AV_LOG_DEBUG, "Working around excessive core frame size (%d > %d)\n", s->frame_size, size);
1984 s->frame_size = size;
1987 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1988 av_log(s->avctx, AV_LOG_ERROR, "Read past end of core frame\n");
1989 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1990 return AVERROR_INVALIDDATA;
1996 int ff_dca_core_parse_exss(DCACoreDecoder *s, uint8_t *data, DCAExssAsset *asset)
1998 AVCodecContext *avctx = s->avctx;
1999 DCAContext *dca = avctx->priv_data;
2000 GetBitContext gb = s->gb;
2001 int exss_mask = asset ? asset->extension_mask : 0;
2002 int ret = 0, ext = 0;
2004 // Parse (X)XCH unless downmixing
2005 if (!dca->request_channel_layout) {
2006 if (exss_mask & DCA_EXSS_XXCH) {
2007 if ((ret = init_get_bits8(&s->gb, data + asset->xxch_offset, asset->xxch_size)) < 0)
2009 ret = parse_xxch_frame(s);
2010 ext = DCA_EXSS_XXCH;
2011 } else if (s->xxch_pos) {
2012 s->gb.index = s->xxch_pos;
2013 ret = parse_xxch_frame(s);
2015 } else if (s->xch_pos) {
2016 s->gb.index = s->xch_pos;
2017 ret = parse_xch_frame(s);
2021 // Revert to primary channel set in case (X)XCH parsing fails
2023 if (avctx->err_recognition & AV_EF_EXPLODE)
2025 s->nchannels = ff_dca_channels[s->audio_mode];
2026 s->ch_mask = audio_mode_ch_mask[s->audio_mode];
2028 s->ch_mask |= DCA_SPEAKER_MASK_LFE1;
2030 s->ext_audio_mask |= ext;
2035 if (exss_mask & DCA_EXSS_XBR) {
2036 if ((ret = init_get_bits8(&s->gb, data + asset->xbr_offset, asset->xbr_size)) < 0)
2038 if ((ret = parse_xbr_frame(s)) < 0) {
2039 if (avctx->err_recognition & AV_EF_EXPLODE)
2042 s->ext_audio_mask |= DCA_EXSS_XBR;
2046 // Parse X96 unless decoding XLL
2047 if (!(dca->packet & DCA_PACKET_XLL)) {
2048 if (exss_mask & DCA_EXSS_X96) {
2049 if ((ret = init_get_bits8(&s->gb, data + asset->x96_offset, asset->x96_size)) < 0)
2051 if ((ret = parse_x96_frame_exss(s)) < 0) {
2052 if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
2055 s->ext_audio_mask |= DCA_EXSS_X96;
2057 } else if (s->x96_pos) {
2059 s->gb.index = s->x96_pos;
2060 if ((ret = parse_x96_frame(s)) < 0) {
2061 if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
2064 s->ext_audio_mask |= DCA_CSS_X96;
2072 static int map_prm_ch_to_spkr(DCACoreDecoder *s, int ch)
2076 // Try to map this channel to core first
2077 pos = ff_dca_channels[s->audio_mode];
2079 spkr = prm_ch_to_spkr_map[s->audio_mode][ch];
2080 if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) {
2081 if (s->xxch_core_mask & (1U << spkr))
2083 if (spkr == DCA_SPEAKER_Ls && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss))
2084 return DCA_SPEAKER_Lss;
2085 if (spkr == DCA_SPEAKER_Rs && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss))
2086 return DCA_SPEAKER_Rss;
2093 if ((s->ext_audio_mask & DCA_CSS_XCH) && ch == pos)
2094 return DCA_SPEAKER_Cs;
2097 if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) {
2098 for (spkr = DCA_SPEAKER_Cs; spkr < s->xxch_mask_nbits; spkr++)
2099 if (s->xxch_spkr_mask & (1U << spkr))
2108 static void erase_dsp_history(DCACoreDecoder *s)
2110 memset(s->dcadsp_data, 0, sizeof(s->dcadsp_data));
2111 s->output_history_lfe_fixed = 0;
2112 s->output_history_lfe_float = 0;
2115 static void set_filter_mode(DCACoreDecoder *s, int mode)
2117 if (s->filter_mode != mode) {
2118 erase_dsp_history(s);
2119 s->filter_mode = mode;
2123 int ff_dca_core_filter_fixed(DCACoreDecoder *s, int x96_synth)
2125 int n, ch, spkr, nsamples, x96_nchannels = 0;
2126 const int32_t *filter_coeff;
2129 // Externally set x96_synth flag implies that X96 synthesis should be
2130 // enabled, yet actual X96 subband data should be discarded. This is a
2131 // special case for lossless residual decoder that ignores X96 data if
2133 if (!x96_synth && (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96))) {
2134 x96_nchannels = s->x96_nchannels;
2140 s->output_rate = s->sample_rate << x96_synth;
2141 s->npcmsamples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth;
2143 // Reallocate PCM output buffer
2144 av_fast_malloc(&s->output_buffer, &s->output_size,
2145 nsamples * av_popcount(s->ch_mask) * sizeof(int32_t));
2146 if (!s->output_buffer)
2147 return AVERROR(ENOMEM);
2149 ptr = (int32_t *)s->output_buffer;
2150 for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) {
2151 if (s->ch_mask & (1U << spkr)) {
2152 s->output_samples[spkr] = ptr;
2155 s->output_samples[spkr] = NULL;
2159 // Handle change of filtering mode
2160 set_filter_mode(s, x96_synth | DCA_FILTER_MODE_FIXED);
2164 filter_coeff = ff_dca_fir_64bands_fixed;
2165 else if (s->filter_perfect)
2166 filter_coeff = ff_dca_fir_32bands_perfect_fixed;
2168 filter_coeff = ff_dca_fir_32bands_nonperfect_fixed;
2170 // Filter primary channels
2171 for (ch = 0; ch < s->nchannels; ch++) {
2172 // Map this primary channel to speaker
2173 spkr = map_prm_ch_to_spkr(s, ch);
2175 return AVERROR(EINVAL);
2177 // Filter bank reconstruction
2178 s->dcadsp->sub_qmf_fixed[x96_synth](
2181 s->output_samples[spkr],
2182 s->subband_samples[ch],
2183 ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL,
2184 s->dcadsp_data[ch].u.fix.hist1,
2185 &s->dcadsp_data[ch].offset,
2186 s->dcadsp_data[ch].u.fix.hist2,
2191 // Filter LFE channel
2192 if (s->lfe_present) {
2193 int32_t *samples = s->output_samples[DCA_SPEAKER_LFE1];
2194 int nlfesamples = s->npcmblocks >> 1;
2197 if (s->lfe_present == LFE_FLAG_128) {
2198 av_log(s->avctx, AV_LOG_ERROR, "Fixed point mode doesn't support LFF=1\n");
2199 return AVERROR(EINVAL);
2202 // Offset intermediate buffer for X96
2204 samples += nsamples / 2;
2206 // Interpolate LFE channel
2207 s->dcadsp->lfe_fir_fixed(samples, s->lfe_samples + DCA_LFE_HISTORY,
2208 ff_dca_lfe_fir_64_fixed, s->npcmblocks);
2211 // Filter 96 kHz oversampled LFE PCM to attenuate high frequency
2212 // (47.6 - 48.0 kHz) components of interpolation image
2213 s->dcadsp->lfe_x96_fixed(s->output_samples[DCA_SPEAKER_LFE1],
2214 samples, &s->output_history_lfe_fixed,
2219 // Update LFE history
2220 for (n = DCA_LFE_HISTORY - 1; n >= 0; n--)
2221 s->lfe_samples[n] = s->lfe_samples[nlfesamples + n];
2227 static int filter_frame_fixed(DCACoreDecoder *s, AVFrame *frame)
2229 AVCodecContext *avctx = s->avctx;
2230 DCAContext *dca = avctx->priv_data;
2231 int i, n, ch, ret, spkr, nsamples;
2233 // Don't filter twice when falling back from XLL
2234 if (!(dca->packet & DCA_PACKET_XLL) && (ret = ff_dca_core_filter_fixed(s, 0)) < 0)
2237 avctx->sample_rate = s->output_rate;
2238 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
2239 avctx->bits_per_raw_sample = 24;
2241 frame->nb_samples = nsamples = s->npcmsamples;
2242 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2245 // Undo embedded XCH downmix
2246 if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
2247 && s->audio_mode >= AMODE_2F2R) {
2248 s->dcadsp->dmix_sub_xch(s->output_samples[DCA_SPEAKER_Ls],
2249 s->output_samples[DCA_SPEAKER_Rs],
2250 s->output_samples[DCA_SPEAKER_Cs],
2255 // Undo embedded XXCH downmix
2256 if ((s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH))
2257 && s->xxch_dmix_embedded) {
2258 int scale_inv = s->xxch_dmix_scale_inv;
2259 int *coeff_ptr = s->xxch_dmix_coeff;
2260 int xch_base = ff_dca_channels[s->audio_mode];
2261 av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX);
2263 // Undo embedded core downmix pre-scaling
2264 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2265 if (s->xxch_core_mask & (1U << spkr)) {
2266 s->dcadsp->dmix_scale_inv(s->output_samples[spkr],
2267 scale_inv, nsamples);
2272 for (ch = xch_base; ch < s->nchannels; ch++) {
2273 int src_spkr = map_prm_ch_to_spkr(s, ch);
2275 return AVERROR(EINVAL);
2276 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2277 if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) {
2278 int coeff = mul16(*coeff_ptr++, scale_inv);
2280 s->dcadsp->dmix_sub(s->output_samples[spkr ],
2281 s->output_samples[src_spkr],
2289 if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) {
2290 // Front sum/difference decoding
2291 if ((s->sumdiff_front && s->audio_mode > AMODE_MONO)
2292 || s->audio_mode == AMODE_STEREO_SUMDIFF) {
2293 s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_L],
2294 s->output_samples[DCA_SPEAKER_R],
2298 // Surround sum/difference decoding
2299 if (s->sumdiff_surround && s->audio_mode >= AMODE_2F2R) {
2300 s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_Ls],
2301 s->output_samples[DCA_SPEAKER_Rs],
2306 // Downmix primary channel set to stereo
2307 if (s->request_mask != s->ch_mask) {
2308 ff_dca_downmix_to_stereo_fixed(s->dcadsp,
2311 nsamples, s->ch_mask);
2314 for (i = 0; i < avctx->channels; i++) {
2315 int32_t *samples = s->output_samples[s->ch_remap[i]];
2316 int32_t *plane = (int32_t *)frame->extended_data[i];
2317 for (n = 0; n < nsamples; n++)
2318 plane[n] = clip23(samples[n]) * (1 << 8);
2324 static int filter_frame_float(DCACoreDecoder *s, AVFrame *frame)
2326 AVCodecContext *avctx = s->avctx;
2327 int x96_nchannels = 0, x96_synth = 0;
2328 int i, n, ch, ret, spkr, nsamples, nchannels;
2329 float *output_samples[DCA_SPEAKER_COUNT] = { NULL }, *ptr;
2330 const float *filter_coeff;
2332 if (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96)) {
2333 x96_nchannels = s->x96_nchannels;
2337 avctx->sample_rate = s->sample_rate << x96_synth;
2338 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2339 avctx->bits_per_raw_sample = 0;
2341 frame->nb_samples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth;
2342 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2345 // Build reverse speaker to channel mapping
2346 for (i = 0; i < avctx->channels; i++)
2347 output_samples[s->ch_remap[i]] = (float *)frame->extended_data[i];
2349 // Allocate space for extra channels
2350 nchannels = av_popcount(s->ch_mask) - avctx->channels;
2351 if (nchannels > 0) {
2352 av_fast_malloc(&s->output_buffer, &s->output_size,
2353 nsamples * nchannels * sizeof(float));
2354 if (!s->output_buffer)
2355 return AVERROR(ENOMEM);
2357 ptr = (float *)s->output_buffer;
2358 for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) {
2359 if (!(s->ch_mask & (1U << spkr)))
2361 if (output_samples[spkr])
2363 output_samples[spkr] = ptr;
2368 // Handle change of filtering mode
2369 set_filter_mode(s, x96_synth);
2373 filter_coeff = ff_dca_fir_64bands;
2374 else if (s->filter_perfect)
2375 filter_coeff = ff_dca_fir_32bands_perfect;
2377 filter_coeff = ff_dca_fir_32bands_nonperfect;
2379 // Filter primary channels
2380 for (ch = 0; ch < s->nchannels; ch++) {
2381 // Map this primary channel to speaker
2382 spkr = map_prm_ch_to_spkr(s, ch);
2384 return AVERROR(EINVAL);
2386 // Filter bank reconstruction
2387 s->dcadsp->sub_qmf_float[x96_synth](
2389 &s->imdct[x96_synth],
2390 output_samples[spkr],
2391 s->subband_samples[ch],
2392 ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL,
2393 s->dcadsp_data[ch].u.flt.hist1,
2394 &s->dcadsp_data[ch].offset,
2395 s->dcadsp_data[ch].u.flt.hist2,
2398 1.0f / (1 << (17 - x96_synth)));
2401 // Filter LFE channel
2402 if (s->lfe_present) {
2403 int dec_select = (s->lfe_present == LFE_FLAG_128);
2404 float *samples = output_samples[DCA_SPEAKER_LFE1];
2405 int nlfesamples = s->npcmblocks >> (dec_select + 1);
2407 // Offset intermediate buffer for X96
2409 samples += nsamples / 2;
2413 filter_coeff = ff_dca_lfe_fir_128;
2415 filter_coeff = ff_dca_lfe_fir_64;
2417 // Interpolate LFE channel
2418 s->dcadsp->lfe_fir_float[dec_select](
2419 samples, s->lfe_samples + DCA_LFE_HISTORY,
2420 filter_coeff, s->npcmblocks);
2423 // Filter 96 kHz oversampled LFE PCM to attenuate high frequency
2424 // (47.6 - 48.0 kHz) components of interpolation image
2425 s->dcadsp->lfe_x96_float(output_samples[DCA_SPEAKER_LFE1],
2426 samples, &s->output_history_lfe_float,
2430 // Update LFE history
2431 for (n = DCA_LFE_HISTORY - 1; n >= 0; n--)
2432 s->lfe_samples[n] = s->lfe_samples[nlfesamples + n];
2435 // Undo embedded XCH downmix
2436 if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
2437 && s->audio_mode >= AMODE_2F2R) {
2438 s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Ls],
2439 output_samples[DCA_SPEAKER_Cs],
2440 -M_SQRT1_2, nsamples);
2441 s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Rs],
2442 output_samples[DCA_SPEAKER_Cs],
2443 -M_SQRT1_2, nsamples);
2446 // Undo embedded XXCH downmix
2447 if ((s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH))
2448 && s->xxch_dmix_embedded) {
2449 float scale_inv = s->xxch_dmix_scale_inv * (1.0f / (1 << 16));
2450 int *coeff_ptr = s->xxch_dmix_coeff;
2451 int xch_base = ff_dca_channels[s->audio_mode];
2452 av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX);
2455 for (ch = xch_base; ch < s->nchannels; ch++) {
2456 int src_spkr = map_prm_ch_to_spkr(s, ch);
2458 return AVERROR(EINVAL);
2459 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2460 if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) {
2461 int coeff = *coeff_ptr++;
2463 s->float_dsp->vector_fmac_scalar(output_samples[ spkr],
2464 output_samples[src_spkr],
2465 coeff * (-1.0f / (1 << 15)),
2472 // Undo embedded core downmix pre-scaling
2473 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2474 if (s->xxch_core_mask & (1U << spkr)) {
2475 s->float_dsp->vector_fmul_scalar(output_samples[spkr],
2476 output_samples[spkr],
2477 scale_inv, nsamples);
2482 if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) {
2483 // Front sum/difference decoding
2484 if ((s->sumdiff_front && s->audio_mode > AMODE_MONO)
2485 || s->audio_mode == AMODE_STEREO_SUMDIFF) {
2486 s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_L],
2487 output_samples[DCA_SPEAKER_R],
2491 // Surround sum/difference decoding
2492 if (s->sumdiff_surround && s->audio_mode >= AMODE_2F2R) {
2493 s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_Ls],
2494 output_samples[DCA_SPEAKER_Rs],
2499 // Downmix primary channel set to stereo
2500 if (s->request_mask != s->ch_mask) {
2501 ff_dca_downmix_to_stereo_float(s->float_dsp, output_samples,
2503 nsamples, s->ch_mask);
2509 int ff_dca_core_filter_frame(DCACoreDecoder *s, AVFrame *frame)
2511 AVCodecContext *avctx = s->avctx;
2512 DCAContext *dca = avctx->priv_data;
2513 DCAExssAsset *asset = &dca->exss.assets[0];
2514 enum AVMatrixEncoding matrix_encoding;
2517 // Handle downmixing to stereo request
2518 if (dca->request_channel_layout == DCA_SPEAKER_LAYOUT_STEREO
2519 && s->audio_mode > AMODE_MONO && s->prim_dmix_embedded
2520 && (s->prim_dmix_type == DCA_DMIX_TYPE_LoRo ||
2521 s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
2522 s->request_mask = DCA_SPEAKER_LAYOUT_STEREO;
2524 s->request_mask = s->ch_mask;
2525 if (!ff_dca_set_channel_layout(avctx, s->ch_remap, s->request_mask))
2526 return AVERROR(EINVAL);
2528 // Force fixed point mode when falling back from XLL
2529 if ((avctx->flags & AV_CODEC_FLAG_BITEXACT) || ((dca->packet & DCA_PACKET_EXSS)
2530 && (asset->extension_mask & DCA_EXSS_XLL)))
2531 ret = filter_frame_fixed(s, frame);
2533 ret = filter_frame_float(s, frame);
2537 // Set profile, bit rate, etc
2538 if (s->ext_audio_mask & DCA_EXSS_MASK)
2539 avctx->profile = FF_PROFILE_DTS_HD_HRA;
2540 else if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH))
2541 avctx->profile = FF_PROFILE_DTS_ES;
2542 else if (s->ext_audio_mask & DCA_CSS_X96)
2543 avctx->profile = FF_PROFILE_DTS_96_24;
2545 avctx->profile = FF_PROFILE_DTS;
2547 if (s->bit_rate > 3 && !(s->ext_audio_mask & DCA_EXSS_MASK))
2548 avctx->bit_rate = s->bit_rate;
2550 avctx->bit_rate = 0;
2552 if (s->audio_mode == AMODE_STEREO_TOTAL || (s->request_mask != s->ch_mask &&
2553 s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
2554 matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
2556 matrix_encoding = AV_MATRIX_ENCODING_NONE;
2557 if ((ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
2563 av_cold void ff_dca_core_flush(DCACoreDecoder *s)
2565 if (s->subband_buffer) {
2566 erase_adpcm_history(s);
2567 memset(s->lfe_samples, 0, DCA_LFE_HISTORY * sizeof(int32_t));
2570 if (s->x96_subband_buffer)
2571 erase_x96_adpcm_history(s);
2573 erase_dsp_history(s);
2576 av_cold int ff_dca_core_init(DCACoreDecoder *s)
2580 if (!(s->float_dsp = avpriv_float_dsp_alloc(0)))
2582 if (!(s->fixed_dsp = avpriv_alloc_fixed_dsp(0)))
2585 ff_dcadct_init(&s->dcadct);
2586 if (ff_mdct_init(&s->imdct[0], 6, 1, 1.0) < 0)
2588 if (ff_mdct_init(&s->imdct[1], 7, 1, 1.0) < 0)
2590 ff_synth_filter_init(&s->synth);
2596 av_cold void ff_dca_core_close(DCACoreDecoder *s)
2598 av_freep(&s->float_dsp);
2599 av_freep(&s->fixed_dsp);
2601 ff_mdct_end(&s->imdct[0]);
2602 ff_mdct_end(&s->imdct[1]);
2604 av_freep(&s->subband_buffer);
2605 s->subband_size = 0;
2607 av_freep(&s->x96_subband_buffer);
2608 s->x96_subband_size = 0;
2610 av_freep(&s->output_buffer);