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 static int dca_get_vlc(GetBitContext *s, DCAVLC *v, int i)
105 return get_vlc2(s, v->vlc[i].table, v->vlc[i].bits, v->max_depth) + v->offset;
108 static void get_array(GetBitContext *s, int32_t *array, int size, int n)
112 for (i = 0; i < size; i++)
113 array[i] = get_sbits(s, n);
116 // 5.3.1 - Bit stream header
117 static int parse_frame_header(DCACoreDecoder *s)
119 int normal_frame, pcmr_index;
122 normal_frame = get_bits1(&s->gb);
124 // Deficit sample count
125 if (get_bits(&s->gb, 5) != DCA_PCMBLOCK_SAMPLES - 1) {
126 av_log(s->avctx, AV_LOG_ERROR, "Deficit samples are not supported\n");
127 return normal_frame ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME;
131 s->crc_present = get_bits1(&s->gb);
133 // Number of PCM sample blocks
134 s->npcmblocks = get_bits(&s->gb, 7) + 1;
135 if (s->npcmblocks & (DCA_SUBBAND_SAMPLES - 1)) {
136 av_log(s->avctx, AV_LOG_ERROR, "Unsupported number of PCM sample blocks (%d)\n", s->npcmblocks);
137 return (s->npcmblocks < 6 || normal_frame) ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME;
140 // Primary frame byte size
141 s->frame_size = get_bits(&s->gb, 14) + 1;
142 if (s->frame_size < 96) {
143 av_log(s->avctx, AV_LOG_ERROR, "Invalid core frame size (%d bytes)\n", s->frame_size);
144 return AVERROR_INVALIDDATA;
147 // Audio channel arrangement
148 s->audio_mode = get_bits(&s->gb, 6);
149 if (s->audio_mode >= AMODE_COUNT) {
150 av_log(s->avctx, AV_LOG_ERROR, "Unsupported audio channel arrangement (%d)\n", s->audio_mode);
151 return AVERROR_PATCHWELCOME;
154 // Core audio sampling frequency
155 s->sample_rate = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
156 if (!s->sample_rate) {
157 av_log(s->avctx, AV_LOG_ERROR, "Invalid core audio sampling frequency\n");
158 return AVERROR_INVALIDDATA;
161 // Transmission bit rate
162 s->bit_rate = ff_dca_bit_rates[get_bits(&s->gb, 5)];
167 // Embedded dynamic range flag
168 s->drc_present = get_bits1(&s->gb);
170 // Embedded time stamp flag
171 s->ts_present = get_bits1(&s->gb);
173 // Auxiliary data flag
174 s->aux_present = get_bits1(&s->gb);
176 // HDCD mastering flag
179 // Extension audio descriptor flag
180 s->ext_audio_type = get_bits(&s->gb, 3);
182 // Extended coding flag
183 s->ext_audio_present = get_bits1(&s->gb);
185 // Audio sync word insertion flag
186 s->sync_ssf = get_bits1(&s->gb);
188 // Low frequency effects flag
189 s->lfe_present = get_bits(&s->gb, 2);
190 if (s->lfe_present == LFE_FLAG_INVALID) {
191 av_log(s->avctx, AV_LOG_ERROR, "Invalid low frequency effects flag\n");
192 return AVERROR_INVALIDDATA;
195 // Predictor history flag switch
196 s->predictor_history = get_bits1(&s->gb);
198 // Header CRC check bytes
200 skip_bits(&s->gb, 16);
202 // Multirate interpolator switch
203 s->filter_perfect = get_bits1(&s->gb);
205 // Encoder software revision
206 skip_bits(&s->gb, 4);
209 skip_bits(&s->gb, 2);
211 // Source PCM resolution
212 s->source_pcm_res = ff_dca_bits_per_sample[pcmr_index = get_bits(&s->gb, 3)];
213 if (!s->source_pcm_res) {
214 av_log(s->avctx, AV_LOG_ERROR, "Invalid source PCM resolution\n");
215 return AVERROR_INVALIDDATA;
217 s->es_format = pcmr_index & 1;
219 // Front sum/difference flag
220 s->sumdiff_front = get_bits1(&s->gb);
222 // Surround sum/difference flag
223 s->sumdiff_surround = get_bits1(&s->gb);
225 // Dialog normalization / unspecified
226 skip_bits(&s->gb, 4);
231 // 5.3.2 - Primary audio coding header
232 static int parse_coding_header(DCACoreDecoder *s, enum HeaderType header, int xch_base)
234 int n, ch, nchannels, header_size = 0, header_pos = get_bits_count(&s->gb);
235 unsigned int mask, index;
237 if (get_bits_left(&s->gb) < 0)
238 return AVERROR_INVALIDDATA;
242 // Number of subframes
243 s->nsubframes = get_bits(&s->gb, 4) + 1;
245 // Number of primary audio channels
246 s->nchannels = get_bits(&s->gb, 3) + 1;
247 if (s->nchannels != ff_dca_channels[s->audio_mode]) {
248 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);
249 return AVERROR_INVALIDDATA;
251 av_assert1(s->nchannels <= DCA_CHANNELS - 2);
253 s->ch_mask = audio_mode_ch_mask[s->audio_mode];
255 // Add LFE channel if present
257 s->ch_mask |= DCA_SPEAKER_MASK_LFE1;
261 s->nchannels = ff_dca_channels[s->audio_mode] + 1;
262 av_assert1(s->nchannels <= DCA_CHANNELS - 1);
263 s->ch_mask |= DCA_SPEAKER_MASK_Cs;
267 // Channel set header length
268 header_size = get_bits(&s->gb, 7) + 1;
271 if (s->xxch_crc_present
272 && (s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
273 && ff_dca_check_crc(&s->gb, header_pos, header_pos + header_size * 8)) {
274 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH channel set header checksum\n");
275 return AVERROR_INVALIDDATA;
278 // Number of channels in a channel set
279 nchannels = get_bits(&s->gb, 3) + 1;
280 if (nchannels > DCA_XXCH_CHANNELS_MAX) {
281 avpriv_request_sample(s->avctx, "%d XXCH channels", nchannels);
282 return AVERROR_PATCHWELCOME;
284 s->nchannels = ff_dca_channels[s->audio_mode] + nchannels;
285 av_assert1(s->nchannels <= DCA_CHANNELS);
287 // Loudspeaker layout mask
288 mask = get_bits_long(&s->gb, s->xxch_mask_nbits - DCA_SPEAKER_Cs);
289 s->xxch_spkr_mask = mask << DCA_SPEAKER_Cs;
291 if (av_popcount(s->xxch_spkr_mask) != nchannels) {
292 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH speaker layout mask (%#x)\n", s->xxch_spkr_mask);
293 return AVERROR_INVALIDDATA;
296 if (s->xxch_core_mask & s->xxch_spkr_mask) {
297 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);
298 return AVERROR_INVALIDDATA;
301 // Combine core and XXCH masks together
302 s->ch_mask = s->xxch_core_mask | s->xxch_spkr_mask;
304 // Downmix coefficients present in stream
305 if (get_bits1(&s->gb)) {
306 int *coeff_ptr = s->xxch_dmix_coeff;
308 // Downmix already performed by encoder
309 s->xxch_dmix_embedded = get_bits1(&s->gb);
311 // Downmix scale factor
312 index = get_bits(&s->gb, 6) * 4 - FF_DCA_DMIXTABLE_OFFSET - 3;
313 if (index >= FF_DCA_INV_DMIXTABLE_SIZE) {
314 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix scale index (%d)\n", index);
315 return AVERROR_INVALIDDATA;
317 s->xxch_dmix_scale_inv = ff_dca_inv_dmixtable[index];
319 // Downmix channel mapping mask
320 for (ch = 0; ch < nchannels; ch++) {
321 mask = get_bits_long(&s->gb, s->xxch_mask_nbits);
322 if ((mask & s->xxch_core_mask) != mask) {
323 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix channel mapping mask (%#x)\n", mask);
324 return AVERROR_INVALIDDATA;
326 s->xxch_dmix_mask[ch] = mask;
329 // Downmix coefficients
330 for (ch = 0; ch < nchannels; ch++) {
331 for (n = 0; n < s->xxch_mask_nbits; n++) {
332 if (s->xxch_dmix_mask[ch] & (1U << n)) {
333 int code = get_bits(&s->gb, 7);
334 int sign = (code >> 6) - 1;
336 index = code * 4 - 3;
337 if (index >= FF_DCA_DMIXTABLE_SIZE) {
338 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix coefficient index (%d)\n", index);
339 return AVERROR_INVALIDDATA;
341 *coeff_ptr++ = (ff_dca_dmixtable[index] ^ sign) - sign;
349 s->xxch_dmix_embedded = 0;
355 // Subband activity count
356 for (ch = xch_base; ch < s->nchannels; ch++) {
357 s->nsubbands[ch] = get_bits(&s->gb, 5) + 2;
358 if (s->nsubbands[ch] > DCA_SUBBANDS) {
359 av_log(s->avctx, AV_LOG_ERROR, "Invalid subband activity count\n");
360 return AVERROR_INVALIDDATA;
364 // High frequency VQ start subband
365 for (ch = xch_base; ch < s->nchannels; ch++)
366 s->subband_vq_start[ch] = get_bits(&s->gb, 5) + 1;
368 // Joint intensity coding index
369 for (ch = xch_base; ch < s->nchannels; ch++) {
370 if ((n = get_bits(&s->gb, 3)) && header == HEADER_XXCH)
372 if (n > s->nchannels) {
373 av_log(s->avctx, AV_LOG_ERROR, "Invalid joint intensity coding index\n");
374 return AVERROR_INVALIDDATA;
376 s->joint_intensity_index[ch] = n;
379 // Transient mode code book
380 for (ch = xch_base; ch < s->nchannels; ch++)
381 s->transition_mode_sel[ch] = get_bits(&s->gb, 2);
383 // Scale factor code book
384 for (ch = xch_base; ch < s->nchannels; ch++) {
385 s->scale_factor_sel[ch] = get_bits(&s->gb, 3);
386 if (s->scale_factor_sel[ch] == 7) {
387 av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor code book\n");
388 return AVERROR_INVALIDDATA;
392 // Bit allocation quantizer select
393 for (ch = xch_base; ch < s->nchannels; ch++) {
394 s->bit_allocation_sel[ch] = get_bits(&s->gb, 3);
395 if (s->bit_allocation_sel[ch] == 7) {
396 av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation quantizer select\n");
397 return AVERROR_INVALIDDATA;
401 // Quantization index codebook select
402 for (n = 0; n < DCA_CODE_BOOKS; n++)
403 for (ch = xch_base; ch < s->nchannels; ch++)
404 s->quant_index_sel[ch][n] = get_bits(&s->gb, quant_index_sel_nbits[n]);
406 // Scale factor adjustment index
407 for (n = 0; n < DCA_CODE_BOOKS; n++)
408 for (ch = xch_base; ch < s->nchannels; ch++)
409 if (s->quant_index_sel[ch][n] < quant_index_group_size[n])
410 s->scale_factor_adj[ch][n] = ff_dca_scale_factor_adj[get_bits(&s->gb, 2)];
412 if (header == HEADER_XXCH) {
415 // CRC16 of channel set header
416 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
417 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set header\n");
418 return AVERROR_INVALIDDATA;
421 // Audio header CRC check word
423 skip_bits(&s->gb, 16);
429 static inline int parse_scale(DCACoreDecoder *s, int *scale_index, int sel)
431 const uint32_t *scale_table;
432 unsigned int scale_size;
434 // Select the root square table
436 scale_table = ff_dca_scale_factor_quant7;
437 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7);
439 scale_table = ff_dca_scale_factor_quant6;
440 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6);
443 // If Huffman code was used, the difference of scales was encoded
445 *scale_index += dca_get_vlc(&s->gb, &ff_dca_vlc_scale_factor, sel);
447 *scale_index = get_bits(&s->gb, sel + 1);
449 // Look up scale factor from the root square table
450 if ((unsigned int)*scale_index >= scale_size) {
451 av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor index\n");
452 return AVERROR_INVALIDDATA;
455 return scale_table[*scale_index];
458 static inline int parse_joint_scale(DCACoreDecoder *s, int sel)
462 // Absolute value was encoded even when Huffman code was used
464 scale_index = dca_get_vlc(&s->gb, &ff_dca_vlc_scale_factor, sel);
466 scale_index = get_bits(&s->gb, sel + 1);
471 // Look up joint scale factor
472 if ((unsigned int)scale_index >= FF_ARRAY_ELEMS(ff_dca_joint_scale_factors)) {
473 av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor index\n");
474 return AVERROR_INVALIDDATA;
477 return ff_dca_joint_scale_factors[scale_index];
480 // 5.4.1 - Primary audio coding side information
481 static int parse_subframe_header(DCACoreDecoder *s, int sf,
482 enum HeaderType header, int xch_base)
486 if (get_bits_left(&s->gb) < 0)
487 return AVERROR_INVALIDDATA;
489 if (header == HEADER_CORE) {
491 s->nsubsubframes[sf] = get_bits(&s->gb, 2) + 1;
493 // Partial subsubframe sample count
494 skip_bits(&s->gb, 3);
498 for (ch = xch_base; ch < s->nchannels; ch++)
499 for (band = 0; band < s->nsubbands[ch]; band++)
500 s->prediction_mode[ch][band] = get_bits1(&s->gb);
502 // Prediction coefficients VQ address
503 for (ch = xch_base; ch < s->nchannels; ch++)
504 for (band = 0; band < s->nsubbands[ch]; band++)
505 if (s->prediction_mode[ch][band])
506 s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12);
508 // Bit allocation index
509 for (ch = xch_base; ch < s->nchannels; ch++) {
510 int sel = s->bit_allocation_sel[ch];
512 for (band = 0; band < s->subband_vq_start[ch]; band++) {
516 abits = dca_get_vlc(&s->gb, &ff_dca_vlc_bit_allocation, sel);
518 abits = get_bits(&s->gb, sel - 1);
520 if (abits > DCA_ABITS_MAX) {
521 av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation index\n");
522 return AVERROR_INVALIDDATA;
525 s->bit_allocation[ch][band] = abits;
530 for (ch = xch_base; ch < s->nchannels; ch++) {
531 // Clear transition mode for all subbands
532 memset(s->transition_mode[sf][ch], 0, sizeof(s->transition_mode[0][0]));
534 // Transient possible only if more than one subsubframe
535 if (s->nsubsubframes[sf] > 1) {
536 int sel = s->transition_mode_sel[ch];
537 for (band = 0; band < s->subband_vq_start[ch]; band++)
538 if (s->bit_allocation[ch][band])
539 s->transition_mode[sf][ch][band] = dca_get_vlc(&s->gb, &ff_dca_vlc_transition_mode, sel);
544 for (ch = xch_base; ch < s->nchannels; ch++) {
545 int sel = s->scale_factor_sel[ch];
548 // Extract scales for subbands up to VQ
549 for (band = 0; band < s->subband_vq_start[ch]; band++) {
550 if (s->bit_allocation[ch][band]) {
551 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
553 s->scale_factors[ch][band][0] = ret;
554 if (s->transition_mode[sf][ch][band]) {
555 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
557 s->scale_factors[ch][band][1] = ret;
560 s->scale_factors[ch][band][0] = 0;
564 // High frequency VQ subbands
565 for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++) {
566 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
568 s->scale_factors[ch][band][0] = ret;
572 // Joint subband codebook select
573 for (ch = xch_base; ch < s->nchannels; ch++) {
574 if (s->joint_intensity_index[ch]) {
575 s->joint_scale_sel[ch] = get_bits(&s->gb, 3);
576 if (s->joint_scale_sel[ch] == 7) {
577 av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor code book\n");
578 return AVERROR_INVALIDDATA;
583 // Scale factors for joint subband coding
584 for (ch = xch_base; ch < s->nchannels; ch++) {
585 int src_ch = s->joint_intensity_index[ch] - 1;
587 int sel = s->joint_scale_sel[ch];
588 for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) {
589 if ((ret = parse_joint_scale(s, sel)) < 0)
591 s->joint_scale_factors[ch][band] = ret;
596 // Dynamic range coefficient
597 if (s->drc_present && header == HEADER_CORE)
598 skip_bits(&s->gb, 8);
600 // Side information CRC check word
602 skip_bits(&s->gb, 16);
607 #ifndef decode_blockcodes
608 static inline int decode_blockcodes(int code1, int code2, int levels, int32_t *audio)
610 int offset = (levels - 1) / 2;
613 for (n = 0; n < DCA_SUBBAND_SAMPLES / 2; n++) {
614 div = FASTDIV(code1, levels);
615 audio[n] = code1 - div * levels - offset;
618 for (; n < DCA_SUBBAND_SAMPLES; n++) {
619 div = FASTDIV(code2, levels);
620 audio[n] = code2 - div * levels - offset;
624 return code1 | code2;
628 static inline int parse_block_codes(DCACoreDecoder *s, int32_t *audio, int abits)
630 // Extract block code indices from the bit stream
631 int code1 = get_bits(&s->gb, block_code_nbits[abits - 1]);
632 int code2 = get_bits(&s->gb, block_code_nbits[abits - 1]);
633 int levels = ff_dca_quant_levels[abits];
635 // Look up samples from the block code book
636 if (decode_blockcodes(code1, code2, levels, audio)) {
637 av_log(s->avctx, AV_LOG_ERROR, "Failed to decode block code(s)\n");
638 return AVERROR_INVALIDDATA;
644 static inline int parse_huffman_codes(DCACoreDecoder *s, int32_t *audio, int abits, int sel)
648 // Extract Huffman codes from the bit stream
649 for (i = 0; i < DCA_SUBBAND_SAMPLES; i++)
650 audio[i] = dca_get_vlc(&s->gb, &ff_dca_vlc_quant_index[abits - 1], sel);
655 static inline int extract_audio(DCACoreDecoder *s, int32_t *audio, int abits, int ch)
657 av_assert1(abits >= 0 && abits <= DCA_ABITS_MAX);
661 memset(audio, 0, DCA_SUBBAND_SAMPLES * sizeof(*audio));
665 if (abits <= DCA_CODE_BOOKS) {
666 int sel = s->quant_index_sel[ch][abits - 1];
667 if (sel < quant_index_group_size[abits - 1]) {
669 return parse_huffman_codes(s, audio, abits, sel);
673 return parse_block_codes(s, audio, abits);
677 // No further encoding
678 get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3);
682 static inline void dequantize(int32_t *output, const int32_t *input,
683 int32_t step_size, int32_t scale, int residual)
685 // Account for quantizer step size
686 int64_t step_scale = (int64_t)step_size * scale;
689 // Limit scale factor resolution to 22 bits
690 if (step_scale > (1 << 23)) {
691 shift = av_log2(step_scale >> 23) + 1;
692 step_scale >>= shift;
697 for (n = 0; n < DCA_SUBBAND_SAMPLES; n++)
698 output[n] += clip23(norm__(input[n] * step_scale, 22 - shift));
700 for (n = 0; n < DCA_SUBBAND_SAMPLES; n++)
701 output[n] = clip23(norm__(input[n] * step_scale, 22 - shift));
705 static inline void inverse_adpcm(int32_t **subband_samples,
706 const int16_t *vq_index,
707 const int8_t *prediction_mode,
708 int sb_start, int sb_end,
713 for (i = sb_start; i < sb_end; i++) {
714 if (prediction_mode[i]) {
715 const int16_t *coeff = ff_dca_adpcm_vb[vq_index[i]];
716 int32_t *ptr = subband_samples[i] + ofs;
717 for (j = 0; j < len; j++) {
719 for (k = 0; k < DCA_ADPCM_COEFFS; k++)
720 err += (int64_t)ptr[j - k - 1] * coeff[k];
721 ptr[j] = clip23(ptr[j] + clip23(norm13(err)));
727 // 5.5 - Primary audio data arrays
728 static int parse_subframe_audio(DCACoreDecoder *s, int sf, enum HeaderType header,
729 int xch_base, int *sub_pos, int *lfe_pos)
731 int32_t audio[16], scale;
732 int n, ssf, ofs, ch, band;
734 // Check number of subband samples in this subframe
735 int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES;
736 if (*sub_pos + nsamples > s->npcmblocks) {
737 av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
738 return AVERROR_INVALIDDATA;
741 if (get_bits_left(&s->gb) < 0)
742 return AVERROR_INVALIDDATA;
744 // VQ encoded subbands
745 for (ch = xch_base; ch < s->nchannels; ch++) {
746 int32_t vq_index[DCA_SUBBANDS];
748 for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++)
749 // Extract the VQ address from the bit stream
750 vq_index[band] = get_bits(&s->gb, 10);
752 if (s->subband_vq_start[ch] < s->nsubbands[ch]) {
753 s->dcadsp->decode_hf(s->subband_samples[ch], vq_index,
754 ff_dca_high_freq_vq, s->scale_factors[ch],
755 s->subband_vq_start[ch], s->nsubbands[ch],
760 // Low frequency effect data
761 if (s->lfe_present && header == HEADER_CORE) {
764 // Determine number of LFE samples in this subframe
765 int nlfesamples = 2 * s->lfe_present * s->nsubsubframes[sf];
766 av_assert1((unsigned int)nlfesamples <= FF_ARRAY_ELEMS(audio));
768 // Extract LFE samples from the bit stream
769 get_array(&s->gb, audio, nlfesamples, 8);
771 // Extract scale factor index from the bit stream
772 index = get_bits(&s->gb, 8);
773 if (index >= FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7)) {
774 av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE scale factor index\n");
775 return AVERROR_INVALIDDATA;
778 // Look up the 7-bit root square quantization table
779 scale = ff_dca_scale_factor_quant7[index];
781 // Account for quantizer step size which is 0.035
782 scale = mul23(4697620 /* 0.035 * (1 << 27) */, scale);
784 // Scale and take the LFE samples
785 for (n = 0, ofs = *lfe_pos; n < nlfesamples; n++, ofs++)
786 s->lfe_samples[ofs] = clip23(audio[n] * scale >> 4);
788 // Advance LFE sample pointer for the next subframe
793 for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
794 for (ch = xch_base; ch < s->nchannels; ch++) {
795 if (get_bits_left(&s->gb) < 0)
796 return AVERROR_INVALIDDATA;
798 // Not high frequency VQ subbands
799 for (band = 0; band < s->subband_vq_start[ch]; band++) {
800 int ret, trans_ssf, abits = s->bit_allocation[ch][band];
803 // Extract bits from the bit stream
804 if ((ret = extract_audio(s, audio, abits, ch)) < 0)
807 // Select quantization step size table and look up
808 // quantization step size
809 if (s->bit_rate == 3)
810 step_size = ff_dca_lossless_quant[abits];
812 step_size = ff_dca_lossy_quant[abits];
814 // Identify transient location
815 trans_ssf = s->transition_mode[sf][ch][band];
817 // Determine proper scale factor
818 if (trans_ssf == 0 || ssf < trans_ssf)
819 scale = s->scale_factors[ch][band][0];
821 scale = s->scale_factors[ch][band][1];
823 // Adjust scale factor when SEL indicates Huffman code
825 int64_t adj = s->scale_factor_adj[ch][abits - 1];
826 scale = clip23(adj * scale >> 22);
829 dequantize(s->subband_samples[ch][band] + ofs,
830 audio, step_size, scale, 0);
835 if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
836 av_log(s->avctx, AV_LOG_ERROR, "DSYNC check failed\n");
837 return AVERROR_INVALIDDATA;
840 ofs += DCA_SUBBAND_SAMPLES;
844 for (ch = xch_base; ch < s->nchannels; ch++) {
845 inverse_adpcm(s->subband_samples[ch], s->prediction_vq_index[ch],
846 s->prediction_mode[ch], 0, s->nsubbands[ch],
850 // Joint subband coding
851 for (ch = xch_base; ch < s->nchannels; ch++) {
852 int src_ch = s->joint_intensity_index[ch] - 1;
854 s->dcadsp->decode_joint(s->subband_samples[ch], s->subband_samples[src_ch],
855 s->joint_scale_factors[ch], s->nsubbands[ch],
856 s->nsubbands[src_ch], *sub_pos, nsamples);
860 // Advance subband sample pointer for the next subframe
865 static void erase_adpcm_history(DCACoreDecoder *s)
869 // Erase ADPCM history from previous frame if
870 // predictor history switch was disabled
871 for (ch = 0; ch < DCA_CHANNELS; ch++)
872 for (band = 0; band < DCA_SUBBANDS; band++)
873 AV_ZERO128(s->subband_samples[ch][band] - DCA_ADPCM_COEFFS);
878 static int alloc_sample_buffer(DCACoreDecoder *s)
880 int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks;
881 int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS;
882 int nlfesamples = DCA_LFE_HISTORY + s->npcmblocks / 2;
883 unsigned int size = s->subband_size;
886 // Reallocate subband sample buffer
887 av_fast_mallocz(&s->subband_buffer, &s->subband_size,
888 (nframesamples + nlfesamples) * sizeof(int32_t));
889 if (!s->subband_buffer)
890 return AVERROR(ENOMEM);
892 if (size != s->subband_size) {
893 for (ch = 0; ch < DCA_CHANNELS; ch++)
894 for (band = 0; band < DCA_SUBBANDS; band++)
895 s->subband_samples[ch][band] = s->subband_buffer +
896 (ch * DCA_SUBBANDS + band) * nchsamples + DCA_ADPCM_COEFFS;
897 s->lfe_samples = s->subband_buffer + nframesamples;
900 if (!s->predictor_history)
901 erase_adpcm_history(s);
906 static int parse_frame_data(DCACoreDecoder *s, enum HeaderType header, int xch_base)
908 int sf, ch, ret, band, sub_pos, lfe_pos;
910 if ((ret = parse_coding_header(s, header, xch_base)) < 0)
913 for (sf = 0, sub_pos = 0, lfe_pos = DCA_LFE_HISTORY; sf < s->nsubframes; sf++) {
914 if ((ret = parse_subframe_header(s, sf, header, xch_base)) < 0)
916 if ((ret = parse_subframe_audio(s, sf, header, xch_base, &sub_pos, &lfe_pos)) < 0)
920 for (ch = xch_base; ch < s->nchannels; ch++) {
921 // Determine number of active subbands for this channel
922 int nsubbands = s->nsubbands[ch];
923 if (s->joint_intensity_index[ch])
924 nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]);
926 // Update history for ADPCM
927 for (band = 0; band < nsubbands; band++) {
928 int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS;
929 AV_COPY128(samples, samples + s->npcmblocks);
932 // Clear inactive subbands
933 for (; band < DCA_SUBBANDS; band++) {
934 int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS;
935 memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t));
944 static int parse_xch_frame(DCACoreDecoder *s)
948 if (s->ch_mask & DCA_SPEAKER_MASK_Cs) {
949 av_log(s->avctx, AV_LOG_ERROR, "XCH with Cs speaker already present\n");
950 return AVERROR_INVALIDDATA;
953 if ((ret = parse_frame_data(s, HEADER_XCH, s->nchannels)) < 0)
956 // Seek to the end of core frame, don't trust XCH frame size
957 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
958 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XCH frame\n");
959 return AVERROR_INVALIDDATA;
965 static int parse_xxch_frame(DCACoreDecoder *s)
967 int xxch_nchsets, xxch_frame_size;
968 int ret, mask, header_size, header_pos = get_bits_count(&s->gb);
971 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XXCH) {
972 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH sync word\n");
973 return AVERROR_INVALIDDATA;
976 // XXCH frame header length
977 header_size = get_bits(&s->gb, 6) + 1;
979 // Check XXCH frame header CRC
980 if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
981 && ff_dca_check_crc(&s->gb, header_pos + 32, header_pos + header_size * 8)) {
982 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH frame header checksum\n");
983 return AVERROR_INVALIDDATA;
986 // CRC presence flag for channel set header
987 s->xxch_crc_present = get_bits1(&s->gb);
989 // Number of bits for loudspeaker mask
990 s->xxch_mask_nbits = get_bits(&s->gb, 5) + 1;
991 if (s->xxch_mask_nbits <= DCA_SPEAKER_Cs) {
992 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XXCH speaker mask (%d)\n", s->xxch_mask_nbits);
993 return AVERROR_INVALIDDATA;
996 // Number of channel sets
997 xxch_nchsets = get_bits(&s->gb, 2) + 1;
998 if (xxch_nchsets > 1) {
999 avpriv_request_sample(s->avctx, "%d XXCH channel sets", xxch_nchsets);
1000 return AVERROR_PATCHWELCOME;
1003 // Channel set 0 data byte size
1004 xxch_frame_size = get_bits(&s->gb, 14) + 1;
1006 // Core loudspeaker activity mask
1007 s->xxch_core_mask = get_bits_long(&s->gb, s->xxch_mask_nbits);
1009 // Validate the core mask
1012 if ((mask & DCA_SPEAKER_MASK_Ls) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss))
1013 mask = (mask & ~DCA_SPEAKER_MASK_Ls) | DCA_SPEAKER_MASK_Lss;
1015 if ((mask & DCA_SPEAKER_MASK_Rs) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss))
1016 mask = (mask & ~DCA_SPEAKER_MASK_Rs) | DCA_SPEAKER_MASK_Rss;
1018 if (mask != s->xxch_core_mask) {
1019 av_log(s->avctx, AV_LOG_ERROR, "XXCH core speaker activity mask (%#x) disagrees with core (%#x)\n", s->xxch_core_mask, mask);
1020 return AVERROR_INVALIDDATA;
1025 // CRC16 of XXCH frame header
1026 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1027 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH frame header\n");
1028 return AVERROR_INVALIDDATA;
1031 // Parse XXCH channel set 0
1032 if ((ret = parse_frame_data(s, HEADER_XXCH, s->nchannels)) < 0)
1035 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8 + xxch_frame_size * 8)) {
1036 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set\n");
1037 return AVERROR_INVALIDDATA;
1043 static int parse_xbr_subframe(DCACoreDecoder *s, int xbr_base_ch, int xbr_nchannels,
1044 int *xbr_nsubbands, int xbr_transition_mode, int sf, int *sub_pos)
1046 int xbr_nabits[DCA_CHANNELS];
1047 int xbr_bit_allocation[DCA_CHANNELS][DCA_SUBBANDS];
1048 int xbr_scale_nbits[DCA_CHANNELS];
1049 int32_t xbr_scale_factors[DCA_CHANNELS][DCA_SUBBANDS][2];
1050 int ssf, ch, band, ofs;
1052 // Check number of subband samples in this subframe
1053 if (*sub_pos + s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES > s->npcmblocks) {
1054 av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
1055 return AVERROR_INVALIDDATA;
1058 if (get_bits_left(&s->gb) < 0)
1059 return AVERROR_INVALIDDATA;
1061 // Number of bits for XBR bit allocation index
1062 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++)
1063 xbr_nabits[ch] = get_bits(&s->gb, 2) + 2;
1065 // XBR bit allocation index
1066 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1067 for (band = 0; band < xbr_nsubbands[ch]; band++) {
1068 xbr_bit_allocation[ch][band] = get_bits(&s->gb, xbr_nabits[ch]);
1069 if (xbr_bit_allocation[ch][band] > DCA_ABITS_MAX) {
1070 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR bit allocation index\n");
1071 return AVERROR_INVALIDDATA;
1076 // Number of bits for scale indices
1077 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1078 xbr_scale_nbits[ch] = get_bits(&s->gb, 3);
1079 if (!xbr_scale_nbits[ch]) {
1080 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XBR scale factor index\n");
1081 return AVERROR_INVALIDDATA;
1085 // XBR scale factors
1086 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1087 const uint32_t *scale_table;
1090 // Select the root square table
1091 if (s->scale_factor_sel[ch] > 5) {
1092 scale_table = ff_dca_scale_factor_quant7;
1093 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7);
1095 scale_table = ff_dca_scale_factor_quant6;
1096 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6);
1099 // Parse scale factor indices and look up scale factors from the root
1101 for (band = 0; band < xbr_nsubbands[ch]; band++) {
1102 if (xbr_bit_allocation[ch][band]) {
1103 int scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
1104 if (scale_index >= scale_size) {
1105 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
1106 return AVERROR_INVALIDDATA;
1108 xbr_scale_factors[ch][band][0] = scale_table[scale_index];
1109 if (xbr_transition_mode && s->transition_mode[sf][ch][band]) {
1110 scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
1111 if (scale_index >= scale_size) {
1112 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
1113 return AVERROR_INVALIDDATA;
1115 xbr_scale_factors[ch][band][1] = scale_table[scale_index];
1122 for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
1123 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1124 if (get_bits_left(&s->gb) < 0)
1125 return AVERROR_INVALIDDATA;
1127 for (band = 0; band < xbr_nsubbands[ch]; band++) {
1128 int ret, trans_ssf, abits = xbr_bit_allocation[ch][band];
1129 int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale;
1131 // Extract bits from the bit stream
1133 // No further encoding
1134 get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3);
1135 } else if (abits > 0) {
1137 if ((ret = parse_block_codes(s, audio, abits)) < 0)
1140 // No bits allocated
1144 // Look up quantization step size
1145 step_size = ff_dca_lossless_quant[abits];
1147 // Identify transient location
1148 if (xbr_transition_mode)
1149 trans_ssf = s->transition_mode[sf][ch][band];
1153 // Determine proper scale factor
1154 if (trans_ssf == 0 || ssf < trans_ssf)
1155 scale = xbr_scale_factors[ch][band][0];
1157 scale = xbr_scale_factors[ch][band][1];
1159 dequantize(s->subband_samples[ch][band] + ofs,
1160 audio, step_size, scale, 1);
1165 if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
1166 av_log(s->avctx, AV_LOG_ERROR, "XBR-DSYNC check failed\n");
1167 return AVERROR_INVALIDDATA;
1170 ofs += DCA_SUBBAND_SAMPLES;
1173 // Advance subband sample pointer for the next subframe
1178 static int parse_xbr_frame(DCACoreDecoder *s)
1180 int xbr_frame_size[DCA_EXSS_CHSETS_MAX];
1181 int xbr_nchannels[DCA_EXSS_CHSETS_MAX];
1182 int xbr_nsubbands[DCA_EXSS_CHSETS_MAX * DCA_EXSS_CHANNELS_MAX];
1183 int xbr_nchsets, xbr_transition_mode, xbr_band_nbits, xbr_base_ch;
1184 int i, ch1, ch2, ret, header_size, header_pos = get_bits_count(&s->gb);
1187 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XBR) {
1188 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR sync word\n");
1189 return AVERROR_INVALIDDATA;
1192 // XBR frame header length
1193 header_size = get_bits(&s->gb, 6) + 1;
1195 // Check XBR frame header CRC
1196 if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
1197 && ff_dca_check_crc(&s->gb, header_pos + 32, header_pos + header_size * 8)) {
1198 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR frame header checksum\n");
1199 return AVERROR_INVALIDDATA;
1202 // Number of channel sets
1203 xbr_nchsets = get_bits(&s->gb, 2) + 1;
1205 // Channel set data byte size
1206 for (i = 0; i < xbr_nchsets; i++)
1207 xbr_frame_size[i] = get_bits(&s->gb, 14) + 1;
1209 // Transition mode flag
1210 xbr_transition_mode = get_bits1(&s->gb);
1212 // Channel set headers
1213 for (i = 0, ch2 = 0; i < xbr_nchsets; i++) {
1214 xbr_nchannels[i] = get_bits(&s->gb, 3) + 1;
1215 xbr_band_nbits = get_bits(&s->gb, 2) + 5;
1216 for (ch1 = 0; ch1 < xbr_nchannels[i]; ch1++, ch2++) {
1217 xbr_nsubbands[ch2] = get_bits(&s->gb, xbr_band_nbits) + 1;
1218 if (xbr_nsubbands[ch2] > DCA_SUBBANDS) {
1219 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of active XBR subbands (%d)\n", xbr_nsubbands[ch2]);
1220 return AVERROR_INVALIDDATA;
1227 // CRC16 of XBR frame header
1228 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1229 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR frame header\n");
1230 return AVERROR_INVALIDDATA;
1234 for (i = 0, xbr_base_ch = 0; i < xbr_nchsets; i++) {
1235 header_pos = get_bits_count(&s->gb);
1237 if (xbr_base_ch + xbr_nchannels[i] <= s->nchannels) {
1240 for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) {
1241 if ((ret = parse_xbr_subframe(s, xbr_base_ch,
1242 xbr_base_ch + xbr_nchannels[i],
1243 xbr_nsubbands, xbr_transition_mode,
1249 xbr_base_ch += xbr_nchannels[i];
1251 if (ff_dca_seek_bits(&s->gb, header_pos + xbr_frame_size[i] * 8)) {
1252 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR channel set\n");
1253 return AVERROR_INVALIDDATA;
1260 // Modified ISO/IEC 9899 linear congruential generator
1261 // Returns pseudorandom integer in range [-2^30, 2^30 - 1]
1262 static int rand_x96(DCACoreDecoder *s)
1264 s->x96_rand = 1103515245U * s->x96_rand + 12345U;
1265 return (s->x96_rand & 0x7fffffff) - 0x40000000;
1268 static int parse_x96_subframe_audio(DCACoreDecoder *s, int sf, int xch_base, int *sub_pos)
1270 int n, ssf, ch, band, ofs;
1272 // Check number of subband samples in this subframe
1273 int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES;
1274 if (*sub_pos + nsamples > s->npcmblocks) {
1275 av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
1276 return AVERROR_INVALIDDATA;
1279 if (get_bits_left(&s->gb) < 0)
1280 return AVERROR_INVALIDDATA;
1282 // VQ encoded or unallocated subbands
1283 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1284 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1285 // Get the sample pointer and scale factor
1286 int32_t *samples = s->x96_subband_samples[ch][band] + *sub_pos;
1287 int32_t scale = s->scale_factors[ch][band >> 1][band & 1];
1289 switch (s->bit_allocation[ch][band]) {
1290 case 0: // No bits allocated for subband
1292 memset(samples, 0, nsamples * sizeof(int32_t));
1293 else for (n = 0; n < nsamples; n++)
1294 // Generate scaled random samples
1295 samples[n] = mul31(rand_x96(s), scale);
1298 case 1: // VQ encoded subband
1299 for (ssf = 0; ssf < (s->nsubsubframes[sf] + 1) / 2; ssf++) {
1300 // Extract the VQ address from the bit stream and look up
1301 // the VQ code book for up to 16 subband samples
1302 const int8_t *vq_samples = ff_dca_high_freq_vq[get_bits(&s->gb, 10)];
1303 // Scale and take the samples
1304 for (n = 0; n < FFMIN(nsamples - ssf * 16, 16); n++)
1305 *samples++ = clip23(vq_samples[n] * scale + (1 << 3) >> 4);
1313 for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
1314 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1315 if (get_bits_left(&s->gb) < 0)
1316 return AVERROR_INVALIDDATA;
1318 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1319 int ret, abits = s->bit_allocation[ch][band] - 1;
1320 int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale;
1322 // Not VQ encoded or unallocated subbands
1326 // Extract bits from the bit stream
1327 if ((ret = extract_audio(s, audio, abits, ch)) < 0)
1330 // Select quantization step size table and look up quantization
1332 if (s->bit_rate == 3)
1333 step_size = ff_dca_lossless_quant[abits];
1335 step_size = ff_dca_lossy_quant[abits];
1337 // Get the scale factor
1338 scale = s->scale_factors[ch][band >> 1][band & 1];
1340 dequantize(s->x96_subband_samples[ch][band] + ofs,
1341 audio, step_size, scale, 0);
1346 if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
1347 av_log(s->avctx, AV_LOG_ERROR, "X96-DSYNC check failed\n");
1348 return AVERROR_INVALIDDATA;
1351 ofs += DCA_SUBBAND_SAMPLES;
1355 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1356 inverse_adpcm(s->x96_subband_samples[ch], s->prediction_vq_index[ch],
1357 s->prediction_mode[ch], s->x96_subband_start, s->nsubbands[ch],
1358 *sub_pos, nsamples);
1361 // Joint subband coding
1362 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1363 int src_ch = s->joint_intensity_index[ch] - 1;
1365 s->dcadsp->decode_joint(s->x96_subband_samples[ch], s->x96_subband_samples[src_ch],
1366 s->joint_scale_factors[ch], s->nsubbands[ch],
1367 s->nsubbands[src_ch], *sub_pos, nsamples);
1371 // Advance subband sample pointer for the next subframe
1376 static void erase_x96_adpcm_history(DCACoreDecoder *s)
1380 // Erase ADPCM history from previous frame if
1381 // predictor history switch was disabled
1382 for (ch = 0; ch < DCA_CHANNELS; ch++)
1383 for (band = 0; band < DCA_SUBBANDS_X96; band++)
1384 AV_ZERO128(s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS);
1389 static int alloc_x96_sample_buffer(DCACoreDecoder *s)
1391 int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks;
1392 int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS_X96;
1393 unsigned int size = s->x96_subband_size;
1396 // Reallocate subband sample buffer
1397 av_fast_mallocz(&s->x96_subband_buffer, &s->x96_subband_size,
1398 nframesamples * sizeof(int32_t));
1399 if (!s->x96_subband_buffer)
1400 return AVERROR(ENOMEM);
1402 if (size != s->x96_subband_size) {
1403 for (ch = 0; ch < DCA_CHANNELS; ch++)
1404 for (band = 0; band < DCA_SUBBANDS_X96; band++)
1405 s->x96_subband_samples[ch][band] = s->x96_subband_buffer +
1406 (ch * DCA_SUBBANDS_X96 + band) * nchsamples + DCA_ADPCM_COEFFS;
1409 if (!s->predictor_history)
1410 erase_x96_adpcm_history(s);
1415 static int parse_x96_subframe_header(DCACoreDecoder *s, int xch_base)
1419 if (get_bits_left(&s->gb) < 0)
1420 return AVERROR_INVALIDDATA;
1423 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1424 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++)
1425 s->prediction_mode[ch][band] = get_bits1(&s->gb);
1427 // Prediction coefficients VQ address
1428 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1429 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++)
1430 if (s->prediction_mode[ch][band])
1431 s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12);
1433 // Bit allocation index
1434 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1435 int sel = s->bit_allocation_sel[ch];
1438 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1439 // If Huffman code was used, the difference of abits was encoded
1441 abits += dca_get_vlc(&s->gb, &ff_dca_vlc_quant_index[5 + 2 * s->x96_high_res], sel);
1443 abits = get_bits(&s->gb, 3 + s->x96_high_res);
1445 if (abits < 0 || abits > 7 + 8 * s->x96_high_res) {
1446 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 bit allocation index\n");
1447 return AVERROR_INVALIDDATA;
1450 s->bit_allocation[ch][band] = abits;
1455 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1456 int sel = s->scale_factor_sel[ch];
1457 int scale_index = 0;
1459 // Extract scales for subbands which are transmitted even for
1460 // unallocated subbands
1461 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1462 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
1464 s->scale_factors[ch][band >> 1][band & 1] = ret;
1468 // Joint subband codebook select
1469 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1470 if (s->joint_intensity_index[ch]) {
1471 s->joint_scale_sel[ch] = get_bits(&s->gb, 3);
1472 if (s->joint_scale_sel[ch] == 7) {
1473 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint scale factor code book\n");
1474 return AVERROR_INVALIDDATA;
1479 // Scale factors for joint subband coding
1480 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1481 int src_ch = s->joint_intensity_index[ch] - 1;
1483 int sel = s->joint_scale_sel[ch];
1484 for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) {
1485 if ((ret = parse_joint_scale(s, sel)) < 0)
1487 s->joint_scale_factors[ch][band] = ret;
1492 // Side information CRC check word
1494 skip_bits(&s->gb, 16);
1499 static int parse_x96_coding_header(DCACoreDecoder *s, int exss, int xch_base)
1501 int n, ch, header_size = 0, header_pos = get_bits_count(&s->gb);
1503 if (get_bits_left(&s->gb) < 0)
1504 return AVERROR_INVALIDDATA;
1507 // Channel set header length
1508 header_size = get_bits(&s->gb, 7) + 1;
1511 if (s->x96_crc_present
1512 && (s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
1513 && ff_dca_check_crc(&s->gb, header_pos, header_pos + header_size * 8)) {
1514 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 channel set header checksum\n");
1515 return AVERROR_INVALIDDATA;
1519 // High resolution flag
1520 s->x96_high_res = get_bits1(&s->gb);
1522 // First encoded subband
1523 if (s->x96_rev_no < 8) {
1524 s->x96_subband_start = get_bits(&s->gb, 5);
1525 if (s->x96_subband_start > 27) {
1526 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband start index (%d)\n", s->x96_subband_start);
1527 return AVERROR_INVALIDDATA;
1530 s->x96_subband_start = DCA_SUBBANDS;
1533 // Subband activity count
1534 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1535 s->nsubbands[ch] = get_bits(&s->gb, 6) + 1;
1536 if (s->nsubbands[ch] < DCA_SUBBANDS) {
1537 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband activity count (%d)\n", s->nsubbands[ch]);
1538 return AVERROR_INVALIDDATA;
1542 // Joint intensity coding index
1543 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1544 if ((n = get_bits(&s->gb, 3)) && xch_base)
1546 if (n > s->x96_nchannels) {
1547 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint intensity coding index\n");
1548 return AVERROR_INVALIDDATA;
1550 s->joint_intensity_index[ch] = n;
1553 // Scale factor code book
1554 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1555 s->scale_factor_sel[ch] = get_bits(&s->gb, 3);
1556 if (s->scale_factor_sel[ch] >= 6) {
1557 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 scale factor code book\n");
1558 return AVERROR_INVALIDDATA;
1562 // Bit allocation quantizer select
1563 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1564 s->bit_allocation_sel[ch] = get_bits(&s->gb, 3);
1566 // Quantization index codebook select
1567 for (n = 0; n < 6 + 4 * s->x96_high_res; n++)
1568 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1569 s->quant_index_sel[ch][n] = get_bits(&s->gb, quant_index_sel_nbits[n]);
1574 // CRC16 of channel set header
1575 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1576 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set header\n");
1577 return AVERROR_INVALIDDATA;
1581 skip_bits(&s->gb, 16);
1587 static int parse_x96_frame_data(DCACoreDecoder *s, int exss, int xch_base)
1589 int sf, ch, ret, band, sub_pos;
1591 if ((ret = parse_x96_coding_header(s, exss, xch_base)) < 0)
1594 for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) {
1595 if ((ret = parse_x96_subframe_header(s, xch_base)) < 0)
1597 if ((ret = parse_x96_subframe_audio(s, sf, xch_base, &sub_pos)) < 0)
1601 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1602 // Determine number of active subbands for this channel
1603 int nsubbands = s->nsubbands[ch];
1604 if (s->joint_intensity_index[ch])
1605 nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]);
1607 // Update history for ADPCM and clear inactive subbands
1608 for (band = 0; band < DCA_SUBBANDS_X96; band++) {
1609 int32_t *samples = s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS;
1610 if (band >= s->x96_subband_start && band < nsubbands)
1611 AV_COPY128(samples, samples + s->npcmblocks);
1613 memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t));
1622 static int parse_x96_frame(DCACoreDecoder *s)
1627 s->x96_rev_no = get_bits(&s->gb, 4);
1628 if (s->x96_rev_no < 1 || s->x96_rev_no > 8) {
1629 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no);
1630 return AVERROR_INVALIDDATA;
1633 s->x96_crc_present = 0;
1634 s->x96_nchannels = s->nchannels;
1636 if ((ret = alloc_x96_sample_buffer(s)) < 0)
1639 if ((ret = parse_x96_frame_data(s, 0, 0)) < 0)
1642 // Seek to the end of core frame
1643 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1644 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame\n");
1645 return AVERROR_INVALIDDATA;
1651 static int parse_x96_frame_exss(DCACoreDecoder *s)
1653 int x96_frame_size[DCA_EXSS_CHSETS_MAX];
1654 int x96_nchannels[DCA_EXSS_CHSETS_MAX];
1655 int x96_nchsets, x96_base_ch;
1656 int i, ret, header_size, header_pos = get_bits_count(&s->gb);
1659 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_X96) {
1660 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 sync word\n");
1661 return AVERROR_INVALIDDATA;
1664 // X96 frame header length
1665 header_size = get_bits(&s->gb, 6) + 1;
1667 // Check X96 frame header CRC
1668 if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
1669 && ff_dca_check_crc(&s->gb, header_pos + 32, header_pos + header_size * 8)) {
1670 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 frame header checksum\n");
1671 return AVERROR_INVALIDDATA;
1675 s->x96_rev_no = get_bits(&s->gb, 4);
1676 if (s->x96_rev_no < 1 || s->x96_rev_no > 8) {
1677 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no);
1678 return AVERROR_INVALIDDATA;
1681 // CRC presence flag for channel set header
1682 s->x96_crc_present = get_bits1(&s->gb);
1684 // Number of channel sets
1685 x96_nchsets = get_bits(&s->gb, 2) + 1;
1687 // Channel set data byte size
1688 for (i = 0; i < x96_nchsets; i++)
1689 x96_frame_size[i] = get_bits(&s->gb, 12) + 1;
1691 // Number of channels in channel set
1692 for (i = 0; i < x96_nchsets; i++)
1693 x96_nchannels[i] = get_bits(&s->gb, 3) + 1;
1697 // CRC16 of X96 frame header
1698 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1699 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame header\n");
1700 return AVERROR_INVALIDDATA;
1703 if ((ret = alloc_x96_sample_buffer(s)) < 0)
1707 s->x96_nchannels = 0;
1708 for (i = 0, x96_base_ch = 0; i < x96_nchsets; i++) {
1709 header_pos = get_bits_count(&s->gb);
1711 if (x96_base_ch + x96_nchannels[i] <= s->nchannels) {
1712 s->x96_nchannels = x96_base_ch + x96_nchannels[i];
1713 if ((ret = parse_x96_frame_data(s, 1, x96_base_ch)) < 0)
1717 x96_base_ch += x96_nchannels[i];
1719 if (ff_dca_seek_bits(&s->gb, header_pos + x96_frame_size[i] * 8)) {
1720 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set\n");
1721 return AVERROR_INVALIDDATA;
1728 static int parse_aux_data(DCACoreDecoder *s)
1732 if (get_bits_left(&s->gb) < 0)
1733 return AVERROR_INVALIDDATA;
1735 // Auxiliary data byte count (can't be trusted)
1736 skip_bits(&s->gb, 6);
1739 skip_bits_long(&s->gb, -get_bits_count(&s->gb) & 31);
1741 // Auxiliary data sync word
1742 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_REV1AUX) {
1743 av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data sync word\n");
1744 return AVERROR_INVALIDDATA;
1747 aux_pos = get_bits_count(&s->gb);
1749 // Auxiliary decode time stamp flag
1750 if (get_bits1(&s->gb))
1751 skip_bits_long(&s->gb, 47);
1753 // Auxiliary dynamic downmix flag
1754 if (s->prim_dmix_embedded = get_bits1(&s->gb)) {
1757 // Auxiliary primary channel downmix type
1758 s->prim_dmix_type = get_bits(&s->gb, 3);
1759 if (s->prim_dmix_type >= DCA_DMIX_TYPE_COUNT) {
1760 av_log(s->avctx, AV_LOG_ERROR, "Invalid primary channel set downmix type\n");
1761 return AVERROR_INVALIDDATA;
1764 // Size of downmix coefficients matrix
1765 m = ff_dca_dmix_primary_nch[s->prim_dmix_type];
1766 n = ff_dca_channels[s->audio_mode] + !!s->lfe_present;
1768 // Dynamic downmix code coefficients
1769 for (i = 0; i < m * n; i++) {
1770 int code = get_bits(&s->gb, 9);
1771 int sign = (code >> 8) - 1;
1772 unsigned int index = code & 0xff;
1773 if (index >= FF_DCA_DMIXTABLE_SIZE) {
1774 av_log(s->avctx, AV_LOG_ERROR, "Invalid downmix coefficient index\n");
1775 return AVERROR_INVALIDDATA;
1777 s->prim_dmix_coeff[i] = (ff_dca_dmixtable[index] ^ sign) - sign;
1782 skip_bits(&s->gb, -get_bits_count(&s->gb) & 7);
1784 // CRC16 of auxiliary data
1785 skip_bits(&s->gb, 16);
1788 if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
1789 && ff_dca_check_crc(&s->gb, aux_pos, get_bits_count(&s->gb))) {
1790 av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data checksum\n");
1791 return AVERROR_INVALIDDATA;
1797 static int parse_optional_info(DCACoreDecoder *s)
1799 DCAContext *dca = s->avctx->priv_data;
1804 skip_bits_long(&s->gb, 32);
1807 if (s->aux_present && (ret = parse_aux_data(s)) < 0
1808 && (s->avctx->err_recognition & AV_EF_EXPLODE))
1812 s->prim_dmix_embedded = 0;
1815 if (s->ext_audio_present && !dca->core_only) {
1816 int sync_pos = FFMIN(s->frame_size / 4, s->gb.size_in_bits / 32) - 1;
1817 int last_pos = get_bits_count(&s->gb) / 32;
1820 // Search for extension sync words aligned on 4-byte boundary. Search
1821 // must be done backwards from the end of core frame to work around
1822 // sync word aliasing issues.
1823 switch (s->ext_audio_type) {
1825 if (dca->request_channel_layout)
1828 // The distance between XCH sync word and end of the core frame
1829 // must be equal to XCH frame size. Off by one error is allowed for
1830 // compatibility with legacy bitstreams. Minimum XCH frame size is
1831 // 96 bytes. AMODE and PCHS are further checked to reduce
1832 // probability of alias sync detection.
1833 for (; sync_pos >= last_pos; sync_pos--) {
1834 if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_XCH) {
1835 s->gb.index = (sync_pos + 1) * 32;
1836 size = get_bits(&s->gb, 10) + 1;
1837 dist = s->frame_size - sync_pos * 4;
1839 && (size == dist || size - 1 == dist)
1840 && get_bits(&s->gb, 7) == 0x08) {
1841 s->xch_pos = get_bits_count(&s->gb);
1848 av_log(s->avctx, AV_LOG_ERROR, "XCH sync word not found\n");
1849 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1850 return AVERROR_INVALIDDATA;
1855 // The distance between X96 sync word and end of the core frame
1856 // must be equal to X96 frame size. Minimum X96 frame size is 96
1858 for (; sync_pos >= last_pos; sync_pos--) {
1859 if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_X96) {
1860 s->gb.index = (sync_pos + 1) * 32;
1861 size = get_bits(&s->gb, 12) + 1;
1862 dist = s->frame_size - sync_pos * 4;
1863 if (size >= 96 && size == dist) {
1864 s->x96_pos = get_bits_count(&s->gb);
1871 av_log(s->avctx, AV_LOG_ERROR, "X96 sync word not found\n");
1872 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1873 return AVERROR_INVALIDDATA;
1877 case EXT_AUDIO_XXCH:
1878 if (dca->request_channel_layout)
1881 // XXCH frame header CRC must be valid. Minimum XXCH frame header
1882 // size is 11 bytes.
1883 for (; sync_pos >= last_pos; sync_pos--) {
1884 if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_XXCH) {
1885 s->gb.index = (sync_pos + 1) * 32;
1886 size = get_bits(&s->gb, 6) + 1;
1888 !ff_dca_check_crc(&s->gb, (sync_pos + 1) * 32,
1889 sync_pos * 32 + size * 8)) {
1890 s->xxch_pos = sync_pos * 32;
1897 av_log(s->avctx, AV_LOG_ERROR, "XXCH sync word not found\n");
1898 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1899 return AVERROR_INVALIDDATA;
1908 int ff_dca_core_parse(DCACoreDecoder *s, uint8_t *data, int size)
1912 s->ext_audio_mask = 0;
1913 s->xch_pos = s->xxch_pos = s->x96_pos = 0;
1915 if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
1918 skip_bits_long(&s->gb, 32);
1919 if ((ret = parse_frame_header(s)) < 0)
1921 if ((ret = alloc_sample_buffer(s)) < 0)
1923 if ((ret = parse_frame_data(s, HEADER_CORE, 0)) < 0)
1925 if ((ret = parse_optional_info(s)) < 0)
1928 // Workaround for DTS in WAV
1929 if (s->frame_size > size && s->frame_size < size + 4) {
1930 av_log(s->avctx, AV_LOG_DEBUG, "Working around excessive core frame size (%d > %d)\n", s->frame_size, size);
1931 s->frame_size = size;
1934 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1935 av_log(s->avctx, AV_LOG_ERROR, "Read past end of core frame\n");
1936 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1937 return AVERROR_INVALIDDATA;
1943 int ff_dca_core_parse_exss(DCACoreDecoder *s, uint8_t *data, DCAExssAsset *asset)
1945 AVCodecContext *avctx = s->avctx;
1946 DCAContext *dca = avctx->priv_data;
1947 GetBitContext gb = s->gb;
1948 int exss_mask = asset ? asset->extension_mask : 0;
1949 int ret = 0, ext = 0;
1951 // Parse (X)XCH unless downmixing
1952 if (!dca->request_channel_layout) {
1953 if (exss_mask & DCA_EXSS_XXCH) {
1954 if ((ret = init_get_bits8(&s->gb, data + asset->xxch_offset, asset->xxch_size)) < 0)
1956 ret = parse_xxch_frame(s);
1957 ext = DCA_EXSS_XXCH;
1958 } else if (s->xxch_pos) {
1959 s->gb.index = s->xxch_pos;
1960 ret = parse_xxch_frame(s);
1962 } else if (s->xch_pos) {
1963 s->gb.index = s->xch_pos;
1964 ret = parse_xch_frame(s);
1968 // Revert to primary channel set in case (X)XCH parsing fails
1970 if (avctx->err_recognition & AV_EF_EXPLODE)
1972 s->nchannels = ff_dca_channels[s->audio_mode];
1973 s->ch_mask = audio_mode_ch_mask[s->audio_mode];
1975 s->ch_mask |= DCA_SPEAKER_MASK_LFE1;
1977 s->ext_audio_mask |= ext;
1982 if (exss_mask & DCA_EXSS_XBR) {
1983 if ((ret = init_get_bits8(&s->gb, data + asset->xbr_offset, asset->xbr_size)) < 0)
1985 if ((ret = parse_xbr_frame(s)) < 0) {
1986 if (avctx->err_recognition & AV_EF_EXPLODE)
1989 s->ext_audio_mask |= DCA_EXSS_XBR;
1993 // Parse X96 unless decoding XLL
1994 if (!(dca->packet & DCA_PACKET_XLL)) {
1995 if (exss_mask & DCA_EXSS_X96) {
1996 if ((ret = init_get_bits8(&s->gb, data + asset->x96_offset, asset->x96_size)) < 0)
1998 if ((ret = parse_x96_frame_exss(s)) < 0) {
1999 if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
2002 s->ext_audio_mask |= DCA_EXSS_X96;
2004 } else if (s->x96_pos) {
2006 s->gb.index = s->x96_pos;
2007 if ((ret = parse_x96_frame(s)) < 0) {
2008 if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
2011 s->ext_audio_mask |= DCA_CSS_X96;
2019 static int map_prm_ch_to_spkr(DCACoreDecoder *s, int ch)
2023 // Try to map this channel to core first
2024 pos = ff_dca_channels[s->audio_mode];
2026 spkr = prm_ch_to_spkr_map[s->audio_mode][ch];
2027 if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) {
2028 if (s->xxch_core_mask & (1U << spkr))
2030 if (spkr == DCA_SPEAKER_Ls && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss))
2031 return DCA_SPEAKER_Lss;
2032 if (spkr == DCA_SPEAKER_Rs && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss))
2033 return DCA_SPEAKER_Rss;
2040 if ((s->ext_audio_mask & DCA_CSS_XCH) && ch == pos)
2041 return DCA_SPEAKER_Cs;
2044 if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) {
2045 for (spkr = DCA_SPEAKER_Cs; spkr < s->xxch_mask_nbits; spkr++)
2046 if (s->xxch_spkr_mask & (1U << spkr))
2055 static void erase_dsp_history(DCACoreDecoder *s)
2057 memset(s->dcadsp_data, 0, sizeof(s->dcadsp_data));
2058 s->output_history_lfe_fixed = 0;
2059 s->output_history_lfe_float = 0;
2062 static void set_filter_mode(DCACoreDecoder *s, int mode)
2064 if (s->filter_mode != mode) {
2065 erase_dsp_history(s);
2066 s->filter_mode = mode;
2070 int ff_dca_core_filter_fixed(DCACoreDecoder *s, int x96_synth)
2072 int n, ch, spkr, nsamples, x96_nchannels = 0;
2073 const int32_t *filter_coeff;
2076 // Externally set x96_synth flag implies that X96 synthesis should be
2077 // enabled, yet actual X96 subband data should be discarded. This is a
2078 // special case for lossless residual decoder that ignores X96 data if
2080 if (!x96_synth && (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96))) {
2081 x96_nchannels = s->x96_nchannels;
2087 s->output_rate = s->sample_rate << x96_synth;
2088 s->npcmsamples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth;
2090 // Reallocate PCM output buffer
2091 av_fast_malloc(&s->output_buffer, &s->output_size,
2092 nsamples * av_popcount(s->ch_mask) * sizeof(int32_t));
2093 if (!s->output_buffer)
2094 return AVERROR(ENOMEM);
2096 ptr = (int32_t *)s->output_buffer;
2097 for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) {
2098 if (s->ch_mask & (1U << spkr)) {
2099 s->output_samples[spkr] = ptr;
2102 s->output_samples[spkr] = NULL;
2106 // Handle change of filtering mode
2107 set_filter_mode(s, x96_synth | DCA_FILTER_MODE_FIXED);
2111 filter_coeff = ff_dca_fir_64bands_fixed;
2112 else if (s->filter_perfect)
2113 filter_coeff = ff_dca_fir_32bands_perfect_fixed;
2115 filter_coeff = ff_dca_fir_32bands_nonperfect_fixed;
2117 // Filter primary channels
2118 for (ch = 0; ch < s->nchannels; ch++) {
2119 // Map this primary channel to speaker
2120 spkr = map_prm_ch_to_spkr(s, ch);
2122 return AVERROR(EINVAL);
2124 // Filter bank reconstruction
2125 s->dcadsp->sub_qmf_fixed[x96_synth](
2128 s->output_samples[spkr],
2129 s->subband_samples[ch],
2130 ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL,
2131 s->dcadsp_data[ch].u.fix.hist1,
2132 &s->dcadsp_data[ch].offset,
2133 s->dcadsp_data[ch].u.fix.hist2,
2138 // Filter LFE channel
2139 if (s->lfe_present) {
2140 int32_t *samples = s->output_samples[DCA_SPEAKER_LFE1];
2141 int nlfesamples = s->npcmblocks >> 1;
2144 if (s->lfe_present == LFE_FLAG_128) {
2145 av_log(s->avctx, AV_LOG_ERROR, "Fixed point mode doesn't support LFF=1\n");
2146 return AVERROR(EINVAL);
2149 // Offset intermediate buffer for X96
2151 samples += nsamples / 2;
2153 // Interpolate LFE channel
2154 s->dcadsp->lfe_fir_fixed(samples, s->lfe_samples + DCA_LFE_HISTORY,
2155 ff_dca_lfe_fir_64_fixed, s->npcmblocks);
2158 // Filter 96 kHz oversampled LFE PCM to attenuate high frequency
2159 // (47.6 - 48.0 kHz) components of interpolation image
2160 s->dcadsp->lfe_x96_fixed(s->output_samples[DCA_SPEAKER_LFE1],
2161 samples, &s->output_history_lfe_fixed,
2166 // Update LFE history
2167 for (n = DCA_LFE_HISTORY - 1; n >= 0; n--)
2168 s->lfe_samples[n] = s->lfe_samples[nlfesamples + n];
2174 static int filter_frame_fixed(DCACoreDecoder *s, AVFrame *frame)
2176 AVCodecContext *avctx = s->avctx;
2177 DCAContext *dca = avctx->priv_data;
2178 int i, n, ch, ret, spkr, nsamples;
2180 // Don't filter twice when falling back from XLL
2181 if (!(dca->packet & DCA_PACKET_XLL) && (ret = ff_dca_core_filter_fixed(s, 0)) < 0)
2184 avctx->sample_rate = s->output_rate;
2185 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
2186 avctx->bits_per_raw_sample = 24;
2188 frame->nb_samples = nsamples = s->npcmsamples;
2189 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2192 // Undo embedded XCH downmix
2193 if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
2194 && s->audio_mode >= AMODE_2F2R) {
2195 s->dcadsp->dmix_sub_xch(s->output_samples[DCA_SPEAKER_Ls],
2196 s->output_samples[DCA_SPEAKER_Rs],
2197 s->output_samples[DCA_SPEAKER_Cs],
2202 // Undo embedded XXCH downmix
2203 if ((s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH))
2204 && s->xxch_dmix_embedded) {
2205 int scale_inv = s->xxch_dmix_scale_inv;
2206 int *coeff_ptr = s->xxch_dmix_coeff;
2207 int xch_base = ff_dca_channels[s->audio_mode];
2208 av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX);
2210 // Undo embedded core downmix pre-scaling
2211 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2212 if (s->xxch_core_mask & (1U << spkr)) {
2213 s->dcadsp->dmix_scale_inv(s->output_samples[spkr],
2214 scale_inv, nsamples);
2219 for (ch = xch_base; ch < s->nchannels; ch++) {
2220 int src_spkr = map_prm_ch_to_spkr(s, ch);
2222 return AVERROR(EINVAL);
2223 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2224 if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) {
2225 int coeff = mul16(*coeff_ptr++, scale_inv);
2227 s->dcadsp->dmix_sub(s->output_samples[spkr ],
2228 s->output_samples[src_spkr],
2236 if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) {
2237 // Front sum/difference decoding
2238 if ((s->sumdiff_front && s->audio_mode > AMODE_MONO)
2239 || s->audio_mode == AMODE_STEREO_SUMDIFF) {
2240 s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_L],
2241 s->output_samples[DCA_SPEAKER_R],
2245 // Surround sum/difference decoding
2246 if (s->sumdiff_surround && s->audio_mode >= AMODE_2F2R) {
2247 s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_Ls],
2248 s->output_samples[DCA_SPEAKER_Rs],
2253 // Downmix primary channel set to stereo
2254 if (s->request_mask != s->ch_mask) {
2255 ff_dca_downmix_to_stereo_fixed(s->dcadsp,
2258 nsamples, s->ch_mask);
2261 for (i = 0; i < avctx->channels; i++) {
2262 int32_t *samples = s->output_samples[s->ch_remap[i]];
2263 int32_t *plane = (int32_t *)frame->extended_data[i];
2264 for (n = 0; n < nsamples; n++)
2265 plane[n] = clip23(samples[n]) * (1 << 8);
2271 static int filter_frame_float(DCACoreDecoder *s, AVFrame *frame)
2273 AVCodecContext *avctx = s->avctx;
2274 int x96_nchannels = 0, x96_synth = 0;
2275 int i, n, ch, ret, spkr, nsamples, nchannels;
2276 float *output_samples[DCA_SPEAKER_COUNT] = { NULL }, *ptr;
2277 const float *filter_coeff;
2279 if (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96)) {
2280 x96_nchannels = s->x96_nchannels;
2284 avctx->sample_rate = s->sample_rate << x96_synth;
2285 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2286 avctx->bits_per_raw_sample = 0;
2288 frame->nb_samples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth;
2289 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2292 // Build reverse speaker to channel mapping
2293 for (i = 0; i < avctx->channels; i++)
2294 output_samples[s->ch_remap[i]] = (float *)frame->extended_data[i];
2296 // Allocate space for extra channels
2297 nchannels = av_popcount(s->ch_mask) - avctx->channels;
2298 if (nchannels > 0) {
2299 av_fast_malloc(&s->output_buffer, &s->output_size,
2300 nsamples * nchannels * sizeof(float));
2301 if (!s->output_buffer)
2302 return AVERROR(ENOMEM);
2304 ptr = (float *)s->output_buffer;
2305 for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) {
2306 if (!(s->ch_mask & (1U << spkr)))
2308 if (output_samples[spkr])
2310 output_samples[spkr] = ptr;
2315 // Handle change of filtering mode
2316 set_filter_mode(s, x96_synth);
2320 filter_coeff = ff_dca_fir_64bands;
2321 else if (s->filter_perfect)
2322 filter_coeff = ff_dca_fir_32bands_perfect;
2324 filter_coeff = ff_dca_fir_32bands_nonperfect;
2326 // Filter primary channels
2327 for (ch = 0; ch < s->nchannels; ch++) {
2328 // Map this primary channel to speaker
2329 spkr = map_prm_ch_to_spkr(s, ch);
2331 return AVERROR(EINVAL);
2333 // Filter bank reconstruction
2334 s->dcadsp->sub_qmf_float[x96_synth](
2336 &s->imdct[x96_synth],
2337 output_samples[spkr],
2338 s->subband_samples[ch],
2339 ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL,
2340 s->dcadsp_data[ch].u.flt.hist1,
2341 &s->dcadsp_data[ch].offset,
2342 s->dcadsp_data[ch].u.flt.hist2,
2345 1.0f / (1 << (17 - x96_synth)));
2348 // Filter LFE channel
2349 if (s->lfe_present) {
2350 int dec_select = (s->lfe_present == LFE_FLAG_128);
2351 float *samples = output_samples[DCA_SPEAKER_LFE1];
2352 int nlfesamples = s->npcmblocks >> (dec_select + 1);
2354 // Offset intermediate buffer for X96
2356 samples += nsamples / 2;
2360 filter_coeff = ff_dca_lfe_fir_128;
2362 filter_coeff = ff_dca_lfe_fir_64;
2364 // Interpolate LFE channel
2365 s->dcadsp->lfe_fir_float[dec_select](
2366 samples, s->lfe_samples + DCA_LFE_HISTORY,
2367 filter_coeff, s->npcmblocks);
2370 // Filter 96 kHz oversampled LFE PCM to attenuate high frequency
2371 // (47.6 - 48.0 kHz) components of interpolation image
2372 s->dcadsp->lfe_x96_float(output_samples[DCA_SPEAKER_LFE1],
2373 samples, &s->output_history_lfe_float,
2377 // Update LFE history
2378 for (n = DCA_LFE_HISTORY - 1; n >= 0; n--)
2379 s->lfe_samples[n] = s->lfe_samples[nlfesamples + n];
2382 // Undo embedded XCH downmix
2383 if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
2384 && s->audio_mode >= AMODE_2F2R) {
2385 s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Ls],
2386 output_samples[DCA_SPEAKER_Cs],
2387 -M_SQRT1_2, nsamples);
2388 s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Rs],
2389 output_samples[DCA_SPEAKER_Cs],
2390 -M_SQRT1_2, nsamples);
2393 // Undo embedded XXCH downmix
2394 if ((s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH))
2395 && s->xxch_dmix_embedded) {
2396 float scale_inv = s->xxch_dmix_scale_inv * (1.0f / (1 << 16));
2397 int *coeff_ptr = s->xxch_dmix_coeff;
2398 int xch_base = ff_dca_channels[s->audio_mode];
2399 av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX);
2402 for (ch = xch_base; ch < s->nchannels; ch++) {
2403 int src_spkr = map_prm_ch_to_spkr(s, ch);
2405 return AVERROR(EINVAL);
2406 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2407 if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) {
2408 int coeff = *coeff_ptr++;
2410 s->float_dsp->vector_fmac_scalar(output_samples[ spkr],
2411 output_samples[src_spkr],
2412 coeff * (-1.0f / (1 << 15)),
2419 // Undo embedded core downmix pre-scaling
2420 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2421 if (s->xxch_core_mask & (1U << spkr)) {
2422 s->float_dsp->vector_fmul_scalar(output_samples[spkr],
2423 output_samples[spkr],
2424 scale_inv, nsamples);
2429 if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) {
2430 // Front sum/difference decoding
2431 if ((s->sumdiff_front && s->audio_mode > AMODE_MONO)
2432 || s->audio_mode == AMODE_STEREO_SUMDIFF) {
2433 s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_L],
2434 output_samples[DCA_SPEAKER_R],
2438 // Surround sum/difference decoding
2439 if (s->sumdiff_surround && s->audio_mode >= AMODE_2F2R) {
2440 s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_Ls],
2441 output_samples[DCA_SPEAKER_Rs],
2446 // Downmix primary channel set to stereo
2447 if (s->request_mask != s->ch_mask) {
2448 ff_dca_downmix_to_stereo_float(s->float_dsp, output_samples,
2450 nsamples, s->ch_mask);
2456 int ff_dca_core_filter_frame(DCACoreDecoder *s, AVFrame *frame)
2458 AVCodecContext *avctx = s->avctx;
2459 DCAContext *dca = avctx->priv_data;
2460 DCAExssAsset *asset = &dca->exss.assets[0];
2461 enum AVMatrixEncoding matrix_encoding;
2464 // Handle downmixing to stereo request
2465 if (dca->request_channel_layout == DCA_SPEAKER_LAYOUT_STEREO
2466 && s->audio_mode > AMODE_MONO && s->prim_dmix_embedded
2467 && (s->prim_dmix_type == DCA_DMIX_TYPE_LoRo ||
2468 s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
2469 s->request_mask = DCA_SPEAKER_LAYOUT_STEREO;
2471 s->request_mask = s->ch_mask;
2472 if (!ff_dca_set_channel_layout(avctx, s->ch_remap, s->request_mask))
2473 return AVERROR(EINVAL);
2475 // Force fixed point mode when falling back from XLL
2476 if ((avctx->flags & AV_CODEC_FLAG_BITEXACT) || ((dca->packet & DCA_PACKET_EXSS)
2477 && (asset->extension_mask & DCA_EXSS_XLL)))
2478 ret = filter_frame_fixed(s, frame);
2480 ret = filter_frame_float(s, frame);
2484 // Set profile, bit rate, etc
2485 if (s->ext_audio_mask & DCA_EXSS_MASK)
2486 avctx->profile = FF_PROFILE_DTS_HD_HRA;
2487 else if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH))
2488 avctx->profile = FF_PROFILE_DTS_ES;
2489 else if (s->ext_audio_mask & DCA_CSS_X96)
2490 avctx->profile = FF_PROFILE_DTS_96_24;
2492 avctx->profile = FF_PROFILE_DTS;
2494 if (s->bit_rate > 3 && !(s->ext_audio_mask & DCA_EXSS_MASK))
2495 avctx->bit_rate = s->bit_rate;
2497 avctx->bit_rate = 0;
2499 if (s->audio_mode == AMODE_STEREO_TOTAL || (s->request_mask != s->ch_mask &&
2500 s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
2501 matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
2503 matrix_encoding = AV_MATRIX_ENCODING_NONE;
2504 if ((ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
2510 av_cold void ff_dca_core_flush(DCACoreDecoder *s)
2512 if (s->subband_buffer) {
2513 erase_adpcm_history(s);
2514 memset(s->lfe_samples, 0, DCA_LFE_HISTORY * sizeof(int32_t));
2517 if (s->x96_subband_buffer)
2518 erase_x96_adpcm_history(s);
2520 erase_dsp_history(s);
2523 av_cold int ff_dca_core_init(DCACoreDecoder *s)
2525 if (!(s->float_dsp = avpriv_float_dsp_alloc(0)))
2527 if (!(s->fixed_dsp = avpriv_alloc_fixed_dsp(0)))
2530 ff_dcadct_init(&s->dcadct);
2531 if (ff_mdct_init(&s->imdct[0], 6, 1, 1.0) < 0)
2533 if (ff_mdct_init(&s->imdct[1], 7, 1, 1.0) < 0)
2535 ff_synth_filter_init(&s->synth);
2541 av_cold void ff_dca_core_close(DCACoreDecoder *s)
2543 av_freep(&s->float_dsp);
2544 av_freep(&s->fixed_dsp);
2546 ff_mdct_end(&s->imdct[0]);
2547 ff_mdct_end(&s->imdct[1]);
2549 av_freep(&s->subband_buffer);
2550 s->subband_size = 0;
2552 av_freep(&s->x96_subband_buffer);
2553 s->x96_subband_size = 0;
2555 av_freep(&s->output_buffer);