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
26 #include "dca_syncwords.h"
38 static const int8_t prm_ch_to_spkr_map[DCA_AMODE_COUNT][5] = {
39 { DCA_SPEAKER_C, -1, -1, -1, -1 },
40 { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
41 { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
42 { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
43 { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
44 { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R , -1, -1 },
45 { DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Cs, -1, -1 },
46 { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R , DCA_SPEAKER_Cs, -1 },
47 { DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Ls, DCA_SPEAKER_Rs, -1 },
48 { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Ls, DCA_SPEAKER_Rs }
51 static const uint8_t audio_mode_ch_mask[DCA_AMODE_COUNT] = {
52 DCA_SPEAKER_LAYOUT_MONO,
53 DCA_SPEAKER_LAYOUT_STEREO,
54 DCA_SPEAKER_LAYOUT_STEREO,
55 DCA_SPEAKER_LAYOUT_STEREO,
56 DCA_SPEAKER_LAYOUT_STEREO,
57 DCA_SPEAKER_LAYOUT_3_0,
58 DCA_SPEAKER_LAYOUT_2_1,
59 DCA_SPEAKER_LAYOUT_3_1,
60 DCA_SPEAKER_LAYOUT_2_2,
61 DCA_SPEAKER_LAYOUT_5POINT0
64 static const uint8_t block_code_nbits[7] = {
65 7, 10, 12, 13, 15, 17, 19
68 static int dca_get_vlc(GetBitContext *s, DCAVLC *v, int i)
70 return get_vlc2(s, v->vlc[i].table, v->vlc[i].bits, v->max_depth) + v->offset;
73 static void get_array(GetBitContext *s, int32_t *array, int size, int n)
77 for (i = 0; i < size; i++)
78 array[i] = get_sbits(s, n);
81 // 5.3.1 - Bit stream header
82 static int parse_frame_header(DCACoreDecoder *s)
84 DCACoreFrameHeader h = { 0 };
85 int err = ff_dca_parse_core_frame_header(&h, &s->gb);
89 case DCA_PARSE_ERROR_DEFICIT_SAMPLES:
90 av_log(s->avctx, AV_LOG_ERROR, "Deficit samples are not supported\n");
91 return h.normal_frame ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME;
93 case DCA_PARSE_ERROR_PCM_BLOCKS:
94 av_log(s->avctx, AV_LOG_ERROR, "Unsupported number of PCM sample blocks (%d)\n", h.npcmblocks);
95 return (h.npcmblocks < 6 || h.normal_frame) ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME;
97 case DCA_PARSE_ERROR_FRAME_SIZE:
98 av_log(s->avctx, AV_LOG_ERROR, "Invalid core frame size (%d bytes)\n", h.frame_size);
99 return AVERROR_INVALIDDATA;
101 case DCA_PARSE_ERROR_AMODE:
102 av_log(s->avctx, AV_LOG_ERROR, "Unsupported audio channel arrangement (%d)\n", h.audio_mode);
103 return AVERROR_PATCHWELCOME;
105 case DCA_PARSE_ERROR_SAMPLE_RATE:
106 av_log(s->avctx, AV_LOG_ERROR, "Invalid core audio sampling frequency\n");
107 return AVERROR_INVALIDDATA;
109 case DCA_PARSE_ERROR_RESERVED_BIT:
110 av_log(s->avctx, AV_LOG_ERROR, "Reserved bit set\n");
111 return AVERROR_INVALIDDATA;
113 case DCA_PARSE_ERROR_LFE_FLAG:
114 av_log(s->avctx, AV_LOG_ERROR, "Invalid low frequency effects flag\n");
115 return AVERROR_INVALIDDATA;
117 case DCA_PARSE_ERROR_PCM_RES:
118 av_log(s->avctx, AV_LOG_ERROR, "Invalid source PCM resolution\n");
119 return AVERROR_INVALIDDATA;
122 av_log(s->avctx, AV_LOG_ERROR, "Unknown core frame header error\n");
123 return AVERROR_INVALIDDATA;
127 s->crc_present = h.crc_present;
128 s->npcmblocks = h.npcmblocks;
129 s->frame_size = h.frame_size;
130 s->audio_mode = h.audio_mode;
131 s->sample_rate = avpriv_dca_sample_rates[h.sr_code];
132 s->bit_rate = ff_dca_bit_rates[h.br_code];
133 s->drc_present = h.drc_present;
134 s->ts_present = h.ts_present;
135 s->aux_present = h.aux_present;
136 s->ext_audio_type = h.ext_audio_type;
137 s->ext_audio_present = h.ext_audio_present;
138 s->sync_ssf = h.sync_ssf;
139 s->lfe_present = h.lfe_present;
140 s->predictor_history = h.predictor_history;
141 s->filter_perfect = h.filter_perfect;
142 s->source_pcm_res = ff_dca_bits_per_sample[h.pcmr_code];
143 s->es_format = h.pcmr_code & 1;
144 s->sumdiff_front = h.sumdiff_front;
145 s->sumdiff_surround = h.sumdiff_surround;
150 // 5.3.2 - Primary audio coding header
151 static int parse_coding_header(DCACoreDecoder *s, enum HeaderType header, int xch_base)
153 int n, ch, nchannels, header_size = 0, header_pos = get_bits_count(&s->gb);
154 unsigned int mask, index;
156 if (get_bits_left(&s->gb) < 0)
157 return AVERROR_INVALIDDATA;
161 // Number of subframes
162 s->nsubframes = get_bits(&s->gb, 4) + 1;
164 // Number of primary audio channels
165 s->nchannels = get_bits(&s->gb, 3) + 1;
166 if (s->nchannels != ff_dca_channels[s->audio_mode]) {
167 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);
168 return AVERROR_INVALIDDATA;
170 av_assert1(s->nchannels <= DCA_CHANNELS - 2);
172 s->ch_mask = audio_mode_ch_mask[s->audio_mode];
174 // Add LFE channel if present
176 s->ch_mask |= DCA_SPEAKER_MASK_LFE1;
180 s->nchannels = ff_dca_channels[s->audio_mode] + 1;
181 av_assert1(s->nchannels <= DCA_CHANNELS - 1);
182 s->ch_mask |= DCA_SPEAKER_MASK_Cs;
186 // Channel set header length
187 header_size = get_bits(&s->gb, 7) + 1;
190 if (s->xxch_crc_present
191 && ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) {
192 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH channel set header checksum\n");
193 return AVERROR_INVALIDDATA;
196 // Number of channels in a channel set
197 nchannels = get_bits(&s->gb, 3) + 1;
198 if (nchannels > DCA_XXCH_CHANNELS_MAX) {
199 avpriv_request_sample(s->avctx, "%d XXCH channels", nchannels);
200 return AVERROR_PATCHWELCOME;
202 s->nchannels = ff_dca_channels[s->audio_mode] + nchannels;
203 av_assert1(s->nchannels <= DCA_CHANNELS);
205 // Loudspeaker layout mask
206 mask = get_bits_long(&s->gb, s->xxch_mask_nbits - DCA_SPEAKER_Cs);
207 s->xxch_spkr_mask = mask << DCA_SPEAKER_Cs;
209 if (av_popcount(s->xxch_spkr_mask) != nchannels) {
210 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH speaker layout mask (%#x)\n", s->xxch_spkr_mask);
211 return AVERROR_INVALIDDATA;
214 if (s->xxch_core_mask & s->xxch_spkr_mask) {
215 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);
216 return AVERROR_INVALIDDATA;
219 // Combine core and XXCH masks together
220 s->ch_mask = s->xxch_core_mask | s->xxch_spkr_mask;
222 // Downmix coefficients present in stream
223 if (get_bits1(&s->gb)) {
224 int *coeff_ptr = s->xxch_dmix_coeff;
226 // Downmix already performed by encoder
227 s->xxch_dmix_embedded = get_bits1(&s->gb);
229 // Downmix scale factor
230 index = get_bits(&s->gb, 6) * 4 - FF_DCA_DMIXTABLE_OFFSET - 3;
231 if (index >= FF_DCA_INV_DMIXTABLE_SIZE) {
232 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix scale index (%d)\n", index);
233 return AVERROR_INVALIDDATA;
235 s->xxch_dmix_scale_inv = ff_dca_inv_dmixtable[index];
237 // Downmix channel mapping mask
238 for (ch = 0; ch < nchannels; ch++) {
239 mask = get_bits_long(&s->gb, s->xxch_mask_nbits);
240 if ((mask & s->xxch_core_mask) != mask) {
241 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix channel mapping mask (%#x)\n", mask);
242 return AVERROR_INVALIDDATA;
244 s->xxch_dmix_mask[ch] = mask;
247 // Downmix coefficients
248 for (ch = 0; ch < nchannels; ch++) {
249 for (n = 0; n < s->xxch_mask_nbits; n++) {
250 if (s->xxch_dmix_mask[ch] & (1U << n)) {
251 int code = get_bits(&s->gb, 7);
252 int sign = (code >> 6) - 1;
254 index = code * 4 - 3;
255 if (index >= FF_DCA_DMIXTABLE_SIZE) {
256 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix coefficient index (%d)\n", index);
257 return AVERROR_INVALIDDATA;
259 *coeff_ptr++ = (ff_dca_dmixtable[index] ^ sign) - sign;
267 s->xxch_dmix_embedded = 0;
273 // Subband activity count
274 for (ch = xch_base; ch < s->nchannels; ch++) {
275 s->nsubbands[ch] = get_bits(&s->gb, 5) + 2;
276 if (s->nsubbands[ch] > DCA_SUBBANDS) {
277 av_log(s->avctx, AV_LOG_ERROR, "Invalid subband activity count\n");
278 return AVERROR_INVALIDDATA;
282 // High frequency VQ start subband
283 for (ch = xch_base; ch < s->nchannels; ch++)
284 s->subband_vq_start[ch] = get_bits(&s->gb, 5) + 1;
286 // Joint intensity coding index
287 for (ch = xch_base; ch < s->nchannels; ch++) {
288 if ((n = get_bits(&s->gb, 3)) && header == HEADER_XXCH)
290 if (n > s->nchannels) {
291 av_log(s->avctx, AV_LOG_ERROR, "Invalid joint intensity coding index\n");
292 return AVERROR_INVALIDDATA;
294 s->joint_intensity_index[ch] = n;
297 // Transient mode code book
298 for (ch = xch_base; ch < s->nchannels; ch++)
299 s->transition_mode_sel[ch] = get_bits(&s->gb, 2);
301 // Scale factor code book
302 for (ch = xch_base; ch < s->nchannels; ch++) {
303 s->scale_factor_sel[ch] = get_bits(&s->gb, 3);
304 if (s->scale_factor_sel[ch] == 7) {
305 av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor code book\n");
306 return AVERROR_INVALIDDATA;
310 // Bit allocation quantizer select
311 for (ch = xch_base; ch < s->nchannels; ch++) {
312 s->bit_allocation_sel[ch] = get_bits(&s->gb, 3);
313 if (s->bit_allocation_sel[ch] == 7) {
314 av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation quantizer select\n");
315 return AVERROR_INVALIDDATA;
319 // Quantization index codebook select
320 for (n = 0; n < DCA_CODE_BOOKS; n++)
321 for (ch = xch_base; ch < s->nchannels; ch++)
322 s->quant_index_sel[ch][n] = get_bits(&s->gb, ff_dca_quant_index_sel_nbits[n]);
324 // Scale factor adjustment index
325 for (n = 0; n < DCA_CODE_BOOKS; n++)
326 for (ch = xch_base; ch < s->nchannels; ch++)
327 if (s->quant_index_sel[ch][n] < ff_dca_quant_index_group_size[n])
328 s->scale_factor_adj[ch][n] = ff_dca_scale_factor_adj[get_bits(&s->gb, 2)];
330 if (header == HEADER_XXCH) {
333 // CRC16 of channel set header
334 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
335 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set header\n");
336 return AVERROR_INVALIDDATA;
339 // Audio header CRC check word
341 skip_bits(&s->gb, 16);
347 static inline int parse_scale(DCACoreDecoder *s, int *scale_index, int sel)
349 const uint32_t *scale_table;
350 unsigned int scale_size;
352 // Select the root square table
354 scale_table = ff_dca_scale_factor_quant7;
355 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7);
357 scale_table = ff_dca_scale_factor_quant6;
358 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6);
361 // If Huffman code was used, the difference of scales was encoded
363 *scale_index += dca_get_vlc(&s->gb, &ff_dca_vlc_scale_factor, sel);
365 *scale_index = get_bits(&s->gb, sel + 1);
367 // Look up scale factor from the root square table
368 if ((unsigned int)*scale_index >= scale_size) {
369 av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor index\n");
370 return AVERROR_INVALIDDATA;
373 return scale_table[*scale_index];
376 static inline int parse_joint_scale(DCACoreDecoder *s, int sel)
380 // Absolute value was encoded even when Huffman code was used
382 scale_index = dca_get_vlc(&s->gb, &ff_dca_vlc_scale_factor, sel);
384 scale_index = get_bits(&s->gb, sel + 1);
389 // Look up joint scale factor
390 if ((unsigned int)scale_index >= FF_ARRAY_ELEMS(ff_dca_joint_scale_factors)) {
391 av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor index\n");
392 return AVERROR_INVALIDDATA;
395 return ff_dca_joint_scale_factors[scale_index];
398 // 5.4.1 - Primary audio coding side information
399 static int parse_subframe_header(DCACoreDecoder *s, int sf,
400 enum HeaderType header, int xch_base)
404 if (get_bits_left(&s->gb) < 0)
405 return AVERROR_INVALIDDATA;
407 if (header == HEADER_CORE) {
409 s->nsubsubframes[sf] = get_bits(&s->gb, 2) + 1;
411 // Partial subsubframe sample count
412 skip_bits(&s->gb, 3);
416 for (ch = xch_base; ch < s->nchannels; ch++)
417 for (band = 0; band < s->nsubbands[ch]; band++)
418 s->prediction_mode[ch][band] = get_bits1(&s->gb);
420 // Prediction coefficients VQ address
421 for (ch = xch_base; ch < s->nchannels; ch++)
422 for (band = 0; band < s->nsubbands[ch]; band++)
423 if (s->prediction_mode[ch][band])
424 s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12);
426 // Bit allocation index
427 for (ch = xch_base; ch < s->nchannels; ch++) {
428 int sel = s->bit_allocation_sel[ch];
430 for (band = 0; band < s->subband_vq_start[ch]; band++) {
434 abits = dca_get_vlc(&s->gb, &ff_dca_vlc_bit_allocation, sel);
436 abits = get_bits(&s->gb, sel - 1);
438 if (abits > DCA_ABITS_MAX) {
439 av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation index\n");
440 return AVERROR_INVALIDDATA;
443 s->bit_allocation[ch][band] = abits;
448 for (ch = xch_base; ch < s->nchannels; ch++) {
449 // Clear transition mode for all subbands
450 memset(s->transition_mode[sf][ch], 0, sizeof(s->transition_mode[0][0]));
452 // Transient possible only if more than one subsubframe
453 if (s->nsubsubframes[sf] > 1) {
454 int sel = s->transition_mode_sel[ch];
455 for (band = 0; band < s->subband_vq_start[ch]; band++)
456 if (s->bit_allocation[ch][band])
457 s->transition_mode[sf][ch][band] = dca_get_vlc(&s->gb, &ff_dca_vlc_transition_mode, sel);
462 for (ch = xch_base; ch < s->nchannels; ch++) {
463 int sel = s->scale_factor_sel[ch];
466 // Extract scales for subbands up to VQ
467 for (band = 0; band < s->subband_vq_start[ch]; band++) {
468 if (s->bit_allocation[ch][band]) {
469 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
471 s->scale_factors[ch][band][0] = ret;
472 if (s->transition_mode[sf][ch][band]) {
473 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
475 s->scale_factors[ch][band][1] = ret;
478 s->scale_factors[ch][band][0] = 0;
482 // High frequency VQ subbands
483 for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++) {
484 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
486 s->scale_factors[ch][band][0] = ret;
490 // Joint subband codebook select
491 for (ch = xch_base; ch < s->nchannels; ch++) {
492 if (s->joint_intensity_index[ch]) {
493 s->joint_scale_sel[ch] = get_bits(&s->gb, 3);
494 if (s->joint_scale_sel[ch] == 7) {
495 av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor code book\n");
496 return AVERROR_INVALIDDATA;
501 // Scale factors for joint subband coding
502 for (ch = xch_base; ch < s->nchannels; ch++) {
503 int src_ch = s->joint_intensity_index[ch] - 1;
505 int sel = s->joint_scale_sel[ch];
506 for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) {
507 if ((ret = parse_joint_scale(s, sel)) < 0)
509 s->joint_scale_factors[ch][band] = ret;
514 // Dynamic range coefficient
515 if (s->drc_present && header == HEADER_CORE)
516 skip_bits(&s->gb, 8);
518 // Side information CRC check word
520 skip_bits(&s->gb, 16);
525 #ifndef decode_blockcodes
526 static inline int decode_blockcodes(int code1, int code2, int levels, int32_t *audio)
528 int offset = (levels - 1) / 2;
531 for (n = 0; n < DCA_SUBBAND_SAMPLES / 2; n++) {
532 div = FASTDIV(code1, levels);
533 audio[n] = code1 - div * levels - offset;
536 for (; n < DCA_SUBBAND_SAMPLES; n++) {
537 div = FASTDIV(code2, levels);
538 audio[n] = code2 - div * levels - offset;
542 return code1 | code2;
546 static inline int parse_block_codes(DCACoreDecoder *s, int32_t *audio, int abits)
548 // Extract block code indices from the bit stream
549 int code1 = get_bits(&s->gb, block_code_nbits[abits - 1]);
550 int code2 = get_bits(&s->gb, block_code_nbits[abits - 1]);
551 int levels = ff_dca_quant_levels[abits];
553 // Look up samples from the block code book
554 if (decode_blockcodes(code1, code2, levels, audio)) {
555 av_log(s->avctx, AV_LOG_ERROR, "Failed to decode block code(s)\n");
556 return AVERROR_INVALIDDATA;
562 static inline int parse_huffman_codes(DCACoreDecoder *s, int32_t *audio, int abits, int sel)
566 // Extract Huffman codes from the bit stream
567 for (i = 0; i < DCA_SUBBAND_SAMPLES; i++)
568 audio[i] = dca_get_vlc(&s->gb, &ff_dca_vlc_quant_index[abits - 1], sel);
573 static inline int extract_audio(DCACoreDecoder *s, int32_t *audio, int abits, int ch)
575 av_assert1(abits >= 0 && abits <= DCA_ABITS_MAX);
579 memset(audio, 0, DCA_SUBBAND_SAMPLES * sizeof(*audio));
583 if (abits <= DCA_CODE_BOOKS) {
584 int sel = s->quant_index_sel[ch][abits - 1];
585 if (sel < ff_dca_quant_index_group_size[abits - 1]) {
587 return parse_huffman_codes(s, audio, abits, sel);
591 return parse_block_codes(s, audio, abits);
595 // No further encoding
596 get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3);
600 static inline void inverse_adpcm(int32_t **subband_samples,
601 const int16_t *vq_index,
602 const int8_t *prediction_mode,
603 int sb_start, int sb_end,
608 for (i = sb_start; i < sb_end; i++) {
609 if (prediction_mode[i]) {
610 const int pred_id = vq_index[i];
611 int32_t *ptr = subband_samples[i] + ofs;
612 for (j = 0; j < len; j++) {
613 int32_t x = ff_dcaadpcm_predict(pred_id, ptr + j - DCA_ADPCM_COEFFS);
614 ptr[j] = clip23(ptr[j] + x);
620 // 5.5 - Primary audio data arrays
621 static int parse_subframe_audio(DCACoreDecoder *s, int sf, enum HeaderType header,
622 int xch_base, int *sub_pos, int *lfe_pos)
624 int32_t audio[16], scale;
625 int n, ssf, ofs, ch, band;
627 // Check number of subband samples in this subframe
628 int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES;
629 if (*sub_pos + nsamples > s->npcmblocks) {
630 av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
631 return AVERROR_INVALIDDATA;
634 if (get_bits_left(&s->gb) < 0)
635 return AVERROR_INVALIDDATA;
637 // VQ encoded subbands
638 for (ch = xch_base; ch < s->nchannels; ch++) {
639 int32_t vq_index[DCA_SUBBANDS];
641 for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++)
642 // Extract the VQ address from the bit stream
643 vq_index[band] = get_bits(&s->gb, 10);
645 if (s->subband_vq_start[ch] < s->nsubbands[ch]) {
646 s->dcadsp->decode_hf(s->subband_samples[ch], vq_index,
647 ff_dca_high_freq_vq, s->scale_factors[ch],
648 s->subband_vq_start[ch], s->nsubbands[ch],
653 // Low frequency effect data
654 if (s->lfe_present && header == HEADER_CORE) {
657 // Determine number of LFE samples in this subframe
658 int nlfesamples = 2 * s->lfe_present * s->nsubsubframes[sf];
659 av_assert1((unsigned int)nlfesamples <= FF_ARRAY_ELEMS(audio));
661 // Extract LFE samples from the bit stream
662 get_array(&s->gb, audio, nlfesamples, 8);
664 // Extract scale factor index from the bit stream
665 index = get_bits(&s->gb, 8);
666 if (index >= FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7)) {
667 av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE scale factor index\n");
668 return AVERROR_INVALIDDATA;
671 // Look up the 7-bit root square quantization table
672 scale = ff_dca_scale_factor_quant7[index];
674 // Account for quantizer step size which is 0.035
675 scale = mul23(4697620 /* 0.035 * (1 << 27) */, scale);
677 // Scale and take the LFE samples
678 for (n = 0, ofs = *lfe_pos; n < nlfesamples; n++, ofs++)
679 s->lfe_samples[ofs] = clip23(audio[n] * scale >> 4);
681 // Advance LFE sample pointer for the next subframe
686 for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
687 for (ch = xch_base; ch < s->nchannels; ch++) {
688 if (get_bits_left(&s->gb) < 0)
689 return AVERROR_INVALIDDATA;
691 // Not high frequency VQ subbands
692 for (band = 0; band < s->subband_vq_start[ch]; band++) {
693 int ret, trans_ssf, abits = s->bit_allocation[ch][band];
696 // Extract bits from the bit stream
697 if ((ret = extract_audio(s, audio, abits, ch)) < 0)
700 // Select quantization step size table and look up
701 // quantization step size
702 if (s->bit_rate == 3)
703 step_size = ff_dca_lossless_quant[abits];
705 step_size = ff_dca_lossy_quant[abits];
707 // Identify transient location
708 trans_ssf = s->transition_mode[sf][ch][band];
710 // Determine proper scale factor
711 if (trans_ssf == 0 || ssf < trans_ssf)
712 scale = s->scale_factors[ch][band][0];
714 scale = s->scale_factors[ch][band][1];
716 // Adjust scale factor when SEL indicates Huffman code
718 int64_t adj = s->scale_factor_adj[ch][abits - 1];
719 scale = clip23(adj * scale >> 22);
722 ff_dca_core_dequantize(s->subband_samples[ch][band] + ofs,
723 audio, step_size, scale, 0, DCA_SUBBAND_SAMPLES);
728 if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
729 av_log(s->avctx, AV_LOG_ERROR, "DSYNC check failed\n");
730 return AVERROR_INVALIDDATA;
733 ofs += DCA_SUBBAND_SAMPLES;
737 for (ch = xch_base; ch < s->nchannels; ch++) {
738 inverse_adpcm(s->subband_samples[ch], s->prediction_vq_index[ch],
739 s->prediction_mode[ch], 0, s->nsubbands[ch],
743 // Joint subband coding
744 for (ch = xch_base; ch < s->nchannels; ch++) {
745 int src_ch = s->joint_intensity_index[ch] - 1;
747 s->dcadsp->decode_joint(s->subband_samples[ch], s->subband_samples[src_ch],
748 s->joint_scale_factors[ch], s->nsubbands[ch],
749 s->nsubbands[src_ch], *sub_pos, nsamples);
753 // Advance subband sample pointer for the next subframe
758 static void erase_adpcm_history(DCACoreDecoder *s)
762 // Erase ADPCM history from previous frame if
763 // predictor history switch was disabled
764 for (ch = 0; ch < DCA_CHANNELS; ch++)
765 for (band = 0; band < DCA_SUBBANDS; band++)
766 AV_ZERO128(s->subband_samples[ch][band] - DCA_ADPCM_COEFFS);
771 static int alloc_sample_buffer(DCACoreDecoder *s)
773 int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks;
774 int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS;
775 int nlfesamples = DCA_LFE_HISTORY + s->npcmblocks / 2;
776 unsigned int size = s->subband_size;
779 // Reallocate subband sample buffer
780 av_fast_mallocz(&s->subband_buffer, &s->subband_size,
781 (nframesamples + nlfesamples) * sizeof(int32_t));
782 if (!s->subband_buffer)
783 return AVERROR(ENOMEM);
785 if (size != s->subband_size) {
786 for (ch = 0; ch < DCA_CHANNELS; ch++)
787 for (band = 0; band < DCA_SUBBANDS; band++)
788 s->subband_samples[ch][band] = s->subband_buffer +
789 (ch * DCA_SUBBANDS + band) * nchsamples + DCA_ADPCM_COEFFS;
790 s->lfe_samples = s->subband_buffer + nframesamples;
793 if (!s->predictor_history)
794 erase_adpcm_history(s);
799 static int parse_frame_data(DCACoreDecoder *s, enum HeaderType header, int xch_base)
801 int sf, ch, ret, band, sub_pos, lfe_pos;
803 if ((ret = parse_coding_header(s, header, xch_base)) < 0)
806 for (sf = 0, sub_pos = 0, lfe_pos = DCA_LFE_HISTORY; sf < s->nsubframes; sf++) {
807 if ((ret = parse_subframe_header(s, sf, header, xch_base)) < 0)
809 if ((ret = parse_subframe_audio(s, sf, header, xch_base, &sub_pos, &lfe_pos)) < 0)
813 for (ch = xch_base; ch < s->nchannels; ch++) {
814 // Determine number of active subbands for this channel
815 int nsubbands = s->nsubbands[ch];
816 if (s->joint_intensity_index[ch])
817 nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]);
819 // Update history for ADPCM
820 for (band = 0; band < nsubbands; band++) {
821 int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS;
822 AV_COPY128(samples, samples + s->npcmblocks);
825 // Clear inactive subbands
826 for (; band < DCA_SUBBANDS; band++) {
827 int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS;
828 memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t));
837 static int parse_xch_frame(DCACoreDecoder *s)
841 if (s->ch_mask & DCA_SPEAKER_MASK_Cs) {
842 av_log(s->avctx, AV_LOG_ERROR, "XCH with Cs speaker already present\n");
843 return AVERROR_INVALIDDATA;
846 if ((ret = parse_frame_data(s, HEADER_XCH, s->nchannels)) < 0)
849 // Seek to the end of core frame, don't trust XCH frame size
850 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
851 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XCH frame\n");
852 return AVERROR_INVALIDDATA;
858 static int parse_xxch_frame(DCACoreDecoder *s)
860 int xxch_nchsets, xxch_frame_size;
861 int ret, mask, header_size, header_pos = get_bits_count(&s->gb);
864 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XXCH) {
865 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH sync word\n");
866 return AVERROR_INVALIDDATA;
869 // XXCH frame header length
870 header_size = get_bits(&s->gb, 6) + 1;
872 // Check XXCH frame header CRC
873 if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) {
874 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH frame header checksum\n");
875 return AVERROR_INVALIDDATA;
878 // CRC presence flag for channel set header
879 s->xxch_crc_present = get_bits1(&s->gb);
881 // Number of bits for loudspeaker mask
882 s->xxch_mask_nbits = get_bits(&s->gb, 5) + 1;
883 if (s->xxch_mask_nbits <= DCA_SPEAKER_Cs) {
884 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XXCH speaker mask (%d)\n", s->xxch_mask_nbits);
885 return AVERROR_INVALIDDATA;
888 // Number of channel sets
889 xxch_nchsets = get_bits(&s->gb, 2) + 1;
890 if (xxch_nchsets > 1) {
891 avpriv_request_sample(s->avctx, "%d XXCH channel sets", xxch_nchsets);
892 return AVERROR_PATCHWELCOME;
895 // Channel set 0 data byte size
896 xxch_frame_size = get_bits(&s->gb, 14) + 1;
898 // Core loudspeaker activity mask
899 s->xxch_core_mask = get_bits_long(&s->gb, s->xxch_mask_nbits);
901 // Validate the core mask
904 if ((mask & DCA_SPEAKER_MASK_Ls) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss))
905 mask = (mask & ~DCA_SPEAKER_MASK_Ls) | DCA_SPEAKER_MASK_Lss;
907 if ((mask & DCA_SPEAKER_MASK_Rs) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss))
908 mask = (mask & ~DCA_SPEAKER_MASK_Rs) | DCA_SPEAKER_MASK_Rss;
910 if (mask != s->xxch_core_mask) {
911 av_log(s->avctx, AV_LOG_ERROR, "XXCH core speaker activity mask (%#x) disagrees with core (%#x)\n", s->xxch_core_mask, mask);
912 return AVERROR_INVALIDDATA;
917 // CRC16 of XXCH frame header
918 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
919 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH frame header\n");
920 return AVERROR_INVALIDDATA;
923 // Parse XXCH channel set 0
924 if ((ret = parse_frame_data(s, HEADER_XXCH, s->nchannels)) < 0)
927 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8 + xxch_frame_size * 8)) {
928 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set\n");
929 return AVERROR_INVALIDDATA;
935 static int parse_xbr_subframe(DCACoreDecoder *s, int xbr_base_ch, int xbr_nchannels,
936 int *xbr_nsubbands, int xbr_transition_mode, int sf, int *sub_pos)
938 int xbr_nabits[DCA_CHANNELS];
939 int xbr_bit_allocation[DCA_CHANNELS][DCA_SUBBANDS];
940 int xbr_scale_nbits[DCA_CHANNELS];
941 int32_t xbr_scale_factors[DCA_CHANNELS][DCA_SUBBANDS][2];
942 int ssf, ch, band, ofs;
944 // Check number of subband samples in this subframe
945 if (*sub_pos + s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES > s->npcmblocks) {
946 av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
947 return AVERROR_INVALIDDATA;
950 if (get_bits_left(&s->gb) < 0)
951 return AVERROR_INVALIDDATA;
953 // Number of bits for XBR bit allocation index
954 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++)
955 xbr_nabits[ch] = get_bits(&s->gb, 2) + 2;
957 // XBR bit allocation index
958 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
959 for (band = 0; band < xbr_nsubbands[ch]; band++) {
960 xbr_bit_allocation[ch][band] = get_bits(&s->gb, xbr_nabits[ch]);
961 if (xbr_bit_allocation[ch][band] > DCA_ABITS_MAX) {
962 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR bit allocation index\n");
963 return AVERROR_INVALIDDATA;
968 // Number of bits for scale indices
969 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
970 xbr_scale_nbits[ch] = get_bits(&s->gb, 3);
971 if (!xbr_scale_nbits[ch]) {
972 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XBR scale factor index\n");
973 return AVERROR_INVALIDDATA;
978 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
979 const uint32_t *scale_table;
982 // Select the root square table
983 if (s->scale_factor_sel[ch] > 5) {
984 scale_table = ff_dca_scale_factor_quant7;
985 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7);
987 scale_table = ff_dca_scale_factor_quant6;
988 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6);
991 // Parse scale factor indices and look up scale factors from the root
993 for (band = 0; band < xbr_nsubbands[ch]; band++) {
994 if (xbr_bit_allocation[ch][band]) {
995 int scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
996 if (scale_index >= scale_size) {
997 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
998 return AVERROR_INVALIDDATA;
1000 xbr_scale_factors[ch][band][0] = scale_table[scale_index];
1001 if (xbr_transition_mode && s->transition_mode[sf][ch][band]) {
1002 scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
1003 if (scale_index >= scale_size) {
1004 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
1005 return AVERROR_INVALIDDATA;
1007 xbr_scale_factors[ch][band][1] = scale_table[scale_index];
1014 for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
1015 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1016 if (get_bits_left(&s->gb) < 0)
1017 return AVERROR_INVALIDDATA;
1019 for (band = 0; band < xbr_nsubbands[ch]; band++) {
1020 int ret, trans_ssf, abits = xbr_bit_allocation[ch][band];
1021 int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale;
1023 // Extract bits from the bit stream
1025 // No further encoding
1026 get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3);
1027 } else if (abits > 0) {
1029 if ((ret = parse_block_codes(s, audio, abits)) < 0)
1032 // No bits allocated
1036 // Look up quantization step size
1037 step_size = ff_dca_lossless_quant[abits];
1039 // Identify transient location
1040 if (xbr_transition_mode)
1041 trans_ssf = s->transition_mode[sf][ch][band];
1045 // Determine proper scale factor
1046 if (trans_ssf == 0 || ssf < trans_ssf)
1047 scale = xbr_scale_factors[ch][band][0];
1049 scale = xbr_scale_factors[ch][band][1];
1051 ff_dca_core_dequantize(s->subband_samples[ch][band] + ofs,
1052 audio, step_size, scale, 1, DCA_SUBBAND_SAMPLES);
1057 if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
1058 av_log(s->avctx, AV_LOG_ERROR, "XBR-DSYNC check failed\n");
1059 return AVERROR_INVALIDDATA;
1062 ofs += DCA_SUBBAND_SAMPLES;
1065 // Advance subband sample pointer for the next subframe
1070 static int parse_xbr_frame(DCACoreDecoder *s)
1072 int xbr_frame_size[DCA_EXSS_CHSETS_MAX];
1073 int xbr_nchannels[DCA_EXSS_CHSETS_MAX];
1074 int xbr_nsubbands[DCA_EXSS_CHSETS_MAX * DCA_EXSS_CHANNELS_MAX];
1075 int xbr_nchsets, xbr_transition_mode, xbr_band_nbits, xbr_base_ch;
1076 int i, ch1, ch2, ret, header_size, header_pos = get_bits_count(&s->gb);
1079 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XBR) {
1080 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR sync word\n");
1081 return AVERROR_INVALIDDATA;
1084 // XBR frame header length
1085 header_size = get_bits(&s->gb, 6) + 1;
1087 // Check XBR frame header CRC
1088 if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) {
1089 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR frame header checksum\n");
1090 return AVERROR_INVALIDDATA;
1093 // Number of channel sets
1094 xbr_nchsets = get_bits(&s->gb, 2) + 1;
1096 // Channel set data byte size
1097 for (i = 0; i < xbr_nchsets; i++)
1098 xbr_frame_size[i] = get_bits(&s->gb, 14) + 1;
1100 // Transition mode flag
1101 xbr_transition_mode = get_bits1(&s->gb);
1103 // Channel set headers
1104 for (i = 0, ch2 = 0; i < xbr_nchsets; i++) {
1105 xbr_nchannels[i] = get_bits(&s->gb, 3) + 1;
1106 xbr_band_nbits = get_bits(&s->gb, 2) + 5;
1107 for (ch1 = 0; ch1 < xbr_nchannels[i]; ch1++, ch2++) {
1108 xbr_nsubbands[ch2] = get_bits(&s->gb, xbr_band_nbits) + 1;
1109 if (xbr_nsubbands[ch2] > DCA_SUBBANDS) {
1110 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of active XBR subbands (%d)\n", xbr_nsubbands[ch2]);
1111 return AVERROR_INVALIDDATA;
1118 // CRC16 of XBR frame header
1119 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1120 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR frame header\n");
1121 return AVERROR_INVALIDDATA;
1125 for (i = 0, xbr_base_ch = 0; i < xbr_nchsets; i++) {
1126 header_pos = get_bits_count(&s->gb);
1128 if (xbr_base_ch + xbr_nchannels[i] <= s->nchannels) {
1131 for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) {
1132 if ((ret = parse_xbr_subframe(s, xbr_base_ch,
1133 xbr_base_ch + xbr_nchannels[i],
1134 xbr_nsubbands, xbr_transition_mode,
1140 xbr_base_ch += xbr_nchannels[i];
1142 if (ff_dca_seek_bits(&s->gb, header_pos + xbr_frame_size[i] * 8)) {
1143 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR channel set\n");
1144 return AVERROR_INVALIDDATA;
1151 // Modified ISO/IEC 9899 linear congruential generator
1152 // Returns pseudorandom integer in range [-2^30, 2^30 - 1]
1153 static int rand_x96(DCACoreDecoder *s)
1155 s->x96_rand = 1103515245U * s->x96_rand + 12345U;
1156 return (s->x96_rand & 0x7fffffff) - 0x40000000;
1159 static int parse_x96_subframe_audio(DCACoreDecoder *s, int sf, int xch_base, int *sub_pos)
1161 int n, ssf, ch, band, ofs;
1163 // Check number of subband samples in this subframe
1164 int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES;
1165 if (*sub_pos + nsamples > s->npcmblocks) {
1166 av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
1167 return AVERROR_INVALIDDATA;
1170 if (get_bits_left(&s->gb) < 0)
1171 return AVERROR_INVALIDDATA;
1173 // VQ encoded or unallocated subbands
1174 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1175 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1176 // Get the sample pointer and scale factor
1177 int32_t *samples = s->x96_subband_samples[ch][band] + *sub_pos;
1178 int32_t scale = s->scale_factors[ch][band >> 1][band & 1];
1180 switch (s->bit_allocation[ch][band]) {
1181 case 0: // No bits allocated for subband
1183 memset(samples, 0, nsamples * sizeof(int32_t));
1184 else for (n = 0; n < nsamples; n++)
1185 // Generate scaled random samples
1186 samples[n] = mul31(rand_x96(s), scale);
1189 case 1: // VQ encoded subband
1190 for (ssf = 0; ssf < (s->nsubsubframes[sf] + 1) / 2; ssf++) {
1191 // Extract the VQ address from the bit stream and look up
1192 // the VQ code book for up to 16 subband samples
1193 const int8_t *vq_samples = ff_dca_high_freq_vq[get_bits(&s->gb, 10)];
1194 // Scale and take the samples
1195 for (n = 0; n < FFMIN(nsamples - ssf * 16, 16); n++)
1196 *samples++ = clip23(vq_samples[n] * scale + (1 << 3) >> 4);
1204 for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
1205 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1206 if (get_bits_left(&s->gb) < 0)
1207 return AVERROR_INVALIDDATA;
1209 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1210 int ret, abits = s->bit_allocation[ch][band] - 1;
1211 int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale;
1213 // Not VQ encoded or unallocated subbands
1217 // Extract bits from the bit stream
1218 if ((ret = extract_audio(s, audio, abits, ch)) < 0)
1221 // Select quantization step size table and look up quantization
1223 if (s->bit_rate == 3)
1224 step_size = ff_dca_lossless_quant[abits];
1226 step_size = ff_dca_lossy_quant[abits];
1228 // Get the scale factor
1229 scale = s->scale_factors[ch][band >> 1][band & 1];
1231 ff_dca_core_dequantize(s->x96_subband_samples[ch][band] + ofs,
1232 audio, step_size, scale, 0, DCA_SUBBAND_SAMPLES);
1237 if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
1238 av_log(s->avctx, AV_LOG_ERROR, "X96-DSYNC check failed\n");
1239 return AVERROR_INVALIDDATA;
1242 ofs += DCA_SUBBAND_SAMPLES;
1246 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1247 inverse_adpcm(s->x96_subband_samples[ch], s->prediction_vq_index[ch],
1248 s->prediction_mode[ch], s->x96_subband_start, s->nsubbands[ch],
1249 *sub_pos, nsamples);
1252 // Joint subband coding
1253 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1254 int src_ch = s->joint_intensity_index[ch] - 1;
1256 s->dcadsp->decode_joint(s->x96_subband_samples[ch], s->x96_subband_samples[src_ch],
1257 s->joint_scale_factors[ch], s->nsubbands[ch],
1258 s->nsubbands[src_ch], *sub_pos, nsamples);
1262 // Advance subband sample pointer for the next subframe
1267 static void erase_x96_adpcm_history(DCACoreDecoder *s)
1271 // Erase ADPCM history from previous frame if
1272 // predictor history switch was disabled
1273 for (ch = 0; ch < DCA_CHANNELS; ch++)
1274 for (band = 0; band < DCA_SUBBANDS_X96; band++)
1275 AV_ZERO128(s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS);
1280 static int alloc_x96_sample_buffer(DCACoreDecoder *s)
1282 int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks;
1283 int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS_X96;
1284 unsigned int size = s->x96_subband_size;
1287 // Reallocate subband sample buffer
1288 av_fast_mallocz(&s->x96_subband_buffer, &s->x96_subband_size,
1289 nframesamples * sizeof(int32_t));
1290 if (!s->x96_subband_buffer)
1291 return AVERROR(ENOMEM);
1293 if (size != s->x96_subband_size) {
1294 for (ch = 0; ch < DCA_CHANNELS; ch++)
1295 for (band = 0; band < DCA_SUBBANDS_X96; band++)
1296 s->x96_subband_samples[ch][band] = s->x96_subband_buffer +
1297 (ch * DCA_SUBBANDS_X96 + band) * nchsamples + DCA_ADPCM_COEFFS;
1300 if (!s->predictor_history)
1301 erase_x96_adpcm_history(s);
1306 static int parse_x96_subframe_header(DCACoreDecoder *s, int xch_base)
1310 if (get_bits_left(&s->gb) < 0)
1311 return AVERROR_INVALIDDATA;
1314 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1315 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++)
1316 s->prediction_mode[ch][band] = get_bits1(&s->gb);
1318 // Prediction coefficients VQ address
1319 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1320 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++)
1321 if (s->prediction_mode[ch][band])
1322 s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12);
1324 // Bit allocation index
1325 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1326 int sel = s->bit_allocation_sel[ch];
1329 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1330 // If Huffman code was used, the difference of abits was encoded
1332 abits += dca_get_vlc(&s->gb, &ff_dca_vlc_quant_index[5 + 2 * s->x96_high_res], sel);
1334 abits = get_bits(&s->gb, 3 + s->x96_high_res);
1336 if (abits < 0 || abits > 7 + 8 * s->x96_high_res) {
1337 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 bit allocation index\n");
1338 return AVERROR_INVALIDDATA;
1341 s->bit_allocation[ch][band] = abits;
1346 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1347 int sel = s->scale_factor_sel[ch];
1348 int scale_index = 0;
1350 // Extract scales for subbands which are transmitted even for
1351 // unallocated subbands
1352 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1353 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
1355 s->scale_factors[ch][band >> 1][band & 1] = ret;
1359 // Joint subband codebook select
1360 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1361 if (s->joint_intensity_index[ch]) {
1362 s->joint_scale_sel[ch] = get_bits(&s->gb, 3);
1363 if (s->joint_scale_sel[ch] == 7) {
1364 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint scale factor code book\n");
1365 return AVERROR_INVALIDDATA;
1370 // Scale factors for joint subband coding
1371 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1372 int src_ch = s->joint_intensity_index[ch] - 1;
1374 int sel = s->joint_scale_sel[ch];
1375 for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) {
1376 if ((ret = parse_joint_scale(s, sel)) < 0)
1378 s->joint_scale_factors[ch][band] = ret;
1383 // Side information CRC check word
1385 skip_bits(&s->gb, 16);
1390 static int parse_x96_coding_header(DCACoreDecoder *s, int exss, int xch_base)
1392 int n, ch, header_size = 0, header_pos = get_bits_count(&s->gb);
1394 if (get_bits_left(&s->gb) < 0)
1395 return AVERROR_INVALIDDATA;
1398 // Channel set header length
1399 header_size = get_bits(&s->gb, 7) + 1;
1402 if (s->x96_crc_present
1403 && ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) {
1404 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 channel set header checksum\n");
1405 return AVERROR_INVALIDDATA;
1409 // High resolution flag
1410 s->x96_high_res = get_bits1(&s->gb);
1412 // First encoded subband
1413 if (s->x96_rev_no < 8) {
1414 s->x96_subband_start = get_bits(&s->gb, 5);
1415 if (s->x96_subband_start > 27) {
1416 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband start index (%d)\n", s->x96_subband_start);
1417 return AVERROR_INVALIDDATA;
1420 s->x96_subband_start = DCA_SUBBANDS;
1423 // Subband activity count
1424 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1425 s->nsubbands[ch] = get_bits(&s->gb, 6) + 1;
1426 if (s->nsubbands[ch] < DCA_SUBBANDS) {
1427 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband activity count (%d)\n", s->nsubbands[ch]);
1428 return AVERROR_INVALIDDATA;
1432 // Joint intensity coding index
1433 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1434 if ((n = get_bits(&s->gb, 3)) && xch_base)
1436 if (n > s->x96_nchannels) {
1437 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint intensity coding index\n");
1438 return AVERROR_INVALIDDATA;
1440 s->joint_intensity_index[ch] = n;
1443 // Scale factor code book
1444 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1445 s->scale_factor_sel[ch] = get_bits(&s->gb, 3);
1446 if (s->scale_factor_sel[ch] >= 6) {
1447 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 scale factor code book\n");
1448 return AVERROR_INVALIDDATA;
1452 // Bit allocation quantizer select
1453 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1454 s->bit_allocation_sel[ch] = get_bits(&s->gb, 3);
1456 // Quantization index codebook select
1457 for (n = 0; n < 6 + 4 * s->x96_high_res; n++)
1458 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1459 s->quant_index_sel[ch][n] = get_bits(&s->gb, ff_dca_quant_index_sel_nbits[n]);
1464 // CRC16 of channel set header
1465 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1466 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set header\n");
1467 return AVERROR_INVALIDDATA;
1471 skip_bits(&s->gb, 16);
1477 static int parse_x96_frame_data(DCACoreDecoder *s, int exss, int xch_base)
1479 int sf, ch, ret, band, sub_pos;
1481 if ((ret = parse_x96_coding_header(s, exss, xch_base)) < 0)
1484 for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) {
1485 if ((ret = parse_x96_subframe_header(s, xch_base)) < 0)
1487 if ((ret = parse_x96_subframe_audio(s, sf, xch_base, &sub_pos)) < 0)
1491 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1492 // Determine number of active subbands for this channel
1493 int nsubbands = s->nsubbands[ch];
1494 if (s->joint_intensity_index[ch])
1495 nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]);
1497 // Update history for ADPCM and clear inactive subbands
1498 for (band = 0; band < DCA_SUBBANDS_X96; band++) {
1499 int32_t *samples = s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS;
1500 if (band >= s->x96_subband_start && band < nsubbands)
1501 AV_COPY128(samples, samples + s->npcmblocks);
1503 memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t));
1512 static int parse_x96_frame(DCACoreDecoder *s)
1517 s->x96_rev_no = get_bits(&s->gb, 4);
1518 if (s->x96_rev_no < 1 || s->x96_rev_no > 8) {
1519 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no);
1520 return AVERROR_INVALIDDATA;
1523 s->x96_crc_present = 0;
1524 s->x96_nchannels = s->nchannels;
1526 if ((ret = alloc_x96_sample_buffer(s)) < 0)
1529 if ((ret = parse_x96_frame_data(s, 0, 0)) < 0)
1532 // Seek to the end of core frame
1533 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1534 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame\n");
1535 return AVERROR_INVALIDDATA;
1541 static int parse_x96_frame_exss(DCACoreDecoder *s)
1543 int x96_frame_size[DCA_EXSS_CHSETS_MAX];
1544 int x96_nchannels[DCA_EXSS_CHSETS_MAX];
1545 int x96_nchsets, x96_base_ch;
1546 int i, ret, header_size, header_pos = get_bits_count(&s->gb);
1549 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_X96) {
1550 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 sync word\n");
1551 return AVERROR_INVALIDDATA;
1554 // X96 frame header length
1555 header_size = get_bits(&s->gb, 6) + 1;
1557 // Check X96 frame header CRC
1558 if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) {
1559 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 frame header checksum\n");
1560 return AVERROR_INVALIDDATA;
1564 s->x96_rev_no = get_bits(&s->gb, 4);
1565 if (s->x96_rev_no < 1 || s->x96_rev_no > 8) {
1566 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no);
1567 return AVERROR_INVALIDDATA;
1570 // CRC presence flag for channel set header
1571 s->x96_crc_present = get_bits1(&s->gb);
1573 // Number of channel sets
1574 x96_nchsets = get_bits(&s->gb, 2) + 1;
1576 // Channel set data byte size
1577 for (i = 0; i < x96_nchsets; i++)
1578 x96_frame_size[i] = get_bits(&s->gb, 12) + 1;
1580 // Number of channels in channel set
1581 for (i = 0; i < x96_nchsets; i++)
1582 x96_nchannels[i] = get_bits(&s->gb, 3) + 1;
1586 // CRC16 of X96 frame header
1587 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1588 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame header\n");
1589 return AVERROR_INVALIDDATA;
1592 if ((ret = alloc_x96_sample_buffer(s)) < 0)
1596 s->x96_nchannels = 0;
1597 for (i = 0, x96_base_ch = 0; i < x96_nchsets; i++) {
1598 header_pos = get_bits_count(&s->gb);
1600 if (x96_base_ch + x96_nchannels[i] <= s->nchannels) {
1601 s->x96_nchannels = x96_base_ch + x96_nchannels[i];
1602 if ((ret = parse_x96_frame_data(s, 1, x96_base_ch)) < 0)
1606 x96_base_ch += x96_nchannels[i];
1608 if (ff_dca_seek_bits(&s->gb, header_pos + x96_frame_size[i] * 8)) {
1609 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set\n");
1610 return AVERROR_INVALIDDATA;
1617 static int parse_aux_data(DCACoreDecoder *s)
1621 if (get_bits_left(&s->gb) < 0)
1622 return AVERROR_INVALIDDATA;
1624 // Auxiliary data byte count (can't be trusted)
1625 skip_bits(&s->gb, 6);
1628 skip_bits_long(&s->gb, -get_bits_count(&s->gb) & 31);
1630 // Auxiliary data sync word
1631 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_REV1AUX) {
1632 av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data sync word\n");
1633 return AVERROR_INVALIDDATA;
1636 aux_pos = get_bits_count(&s->gb);
1638 // Auxiliary decode time stamp flag
1639 if (get_bits1(&s->gb))
1640 skip_bits_long(&s->gb, 47);
1642 // Auxiliary dynamic downmix flag
1643 if (s->prim_dmix_embedded = get_bits1(&s->gb)) {
1646 // Auxiliary primary channel downmix type
1647 s->prim_dmix_type = get_bits(&s->gb, 3);
1648 if (s->prim_dmix_type >= DCA_DMIX_TYPE_COUNT) {
1649 av_log(s->avctx, AV_LOG_ERROR, "Invalid primary channel set downmix type\n");
1650 return AVERROR_INVALIDDATA;
1653 // Size of downmix coefficients matrix
1654 m = ff_dca_dmix_primary_nch[s->prim_dmix_type];
1655 n = ff_dca_channels[s->audio_mode] + !!s->lfe_present;
1657 // Dynamic downmix code coefficients
1658 for (i = 0; i < m * n; i++) {
1659 int code = get_bits(&s->gb, 9);
1660 int sign = (code >> 8) - 1;
1661 unsigned int index = code & 0xff;
1662 if (index >= FF_DCA_DMIXTABLE_SIZE) {
1663 av_log(s->avctx, AV_LOG_ERROR, "Invalid downmix coefficient index\n");
1664 return AVERROR_INVALIDDATA;
1666 s->prim_dmix_coeff[i] = (ff_dca_dmixtable[index] ^ sign) - sign;
1671 skip_bits(&s->gb, -get_bits_count(&s->gb) & 7);
1673 // CRC16 of auxiliary data
1674 skip_bits(&s->gb, 16);
1677 if (ff_dca_check_crc(s->avctx, &s->gb, aux_pos, get_bits_count(&s->gb))) {
1678 av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data checksum\n");
1679 return AVERROR_INVALIDDATA;
1685 static int parse_optional_info(DCACoreDecoder *s)
1687 DCAContext *dca = s->avctx->priv_data;
1692 skip_bits_long(&s->gb, 32);
1695 if (s->aux_present && (ret = parse_aux_data(s)) < 0
1696 && (s->avctx->err_recognition & AV_EF_EXPLODE))
1700 s->prim_dmix_embedded = 0;
1703 if (s->ext_audio_present && !dca->core_only) {
1704 int sync_pos = FFMIN(s->frame_size / 4, s->gb.size_in_bits / 32) - 1;
1705 int last_pos = get_bits_count(&s->gb) / 32;
1707 uint32_t w1, w2 = 0;
1709 // Search for extension sync words aligned on 4-byte boundary. Search
1710 // must be done backwards from the end of core frame to work around
1711 // sync word aliasing issues.
1712 switch (s->ext_audio_type) {
1713 case DCA_EXT_AUDIO_XCH:
1714 if (dca->request_channel_layout)
1717 // The distance between XCH sync word and end of the core frame
1718 // must be equal to XCH frame size. Off by one error is allowed for
1719 // compatibility with legacy bitstreams. Minimum XCH frame size is
1720 // 96 bytes. AMODE and PCHS are further checked to reduce
1721 // probability of alias sync detection.
1722 for (; sync_pos >= last_pos; sync_pos--, w2 = w1) {
1723 w1 = AV_RB32(s->gb.buffer + sync_pos * 4);
1724 if (w1 == DCA_SYNCWORD_XCH) {
1725 size = (w2 >> 22) + 1;
1726 dist = s->frame_size - sync_pos * 4;
1728 && (size == dist || size - 1 == dist)
1729 && (w2 >> 15 & 0x7f) == 0x08) {
1730 s->xch_pos = sync_pos * 32 + 49;
1737 av_log(s->avctx, AV_LOG_ERROR, "XCH sync word not found\n");
1738 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1739 return AVERROR_INVALIDDATA;
1743 case DCA_EXT_AUDIO_X96:
1744 // The distance between X96 sync word and end of the core frame
1745 // must be equal to X96 frame size. Minimum X96 frame size is 96
1747 for (; sync_pos >= last_pos; sync_pos--, w2 = w1) {
1748 w1 = AV_RB32(s->gb.buffer + sync_pos * 4);
1749 if (w1 == DCA_SYNCWORD_X96) {
1750 size = (w2 >> 20) + 1;
1751 dist = s->frame_size - sync_pos * 4;
1752 if (size >= 96 && size == dist) {
1753 s->x96_pos = sync_pos * 32 + 44;
1760 av_log(s->avctx, AV_LOG_ERROR, "X96 sync word not found\n");
1761 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1762 return AVERROR_INVALIDDATA;
1766 case DCA_EXT_AUDIO_XXCH:
1767 if (dca->request_channel_layout)
1770 // XXCH frame header CRC must be valid. Minimum XXCH frame header
1771 // size is 11 bytes.
1772 for (; sync_pos >= last_pos; sync_pos--, w2 = w1) {
1773 w1 = AV_RB32(s->gb.buffer + sync_pos * 4);
1774 if (w1 == DCA_SYNCWORD_XXCH) {
1775 size = (w2 >> 26) + 1;
1776 dist = s->gb.size_in_bits / 8 - sync_pos * 4;
1777 if (size >= 11 && size <= dist &&
1778 !av_crc(dca->crctab, 0xffff, s->gb.buffer +
1779 (sync_pos + 1) * 4, size - 4)) {
1780 s->xxch_pos = sync_pos * 32;
1787 av_log(s->avctx, AV_LOG_ERROR, "XXCH sync word not found\n");
1788 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1789 return AVERROR_INVALIDDATA;
1798 int ff_dca_core_parse(DCACoreDecoder *s, uint8_t *data, int size)
1802 s->ext_audio_mask = 0;
1803 s->xch_pos = s->xxch_pos = s->x96_pos = 0;
1805 if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
1809 if ((ret = parse_frame_header(s)) < 0)
1811 if ((ret = alloc_sample_buffer(s)) < 0)
1813 if ((ret = parse_frame_data(s, HEADER_CORE, 0)) < 0)
1815 if ((ret = parse_optional_info(s)) < 0)
1818 // Workaround for DTS in WAV
1819 if (s->frame_size > size)
1820 s->frame_size = size;
1822 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1823 av_log(s->avctx, AV_LOG_ERROR, "Read past end of core frame\n");
1824 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1825 return AVERROR_INVALIDDATA;
1831 int ff_dca_core_parse_exss(DCACoreDecoder *s, uint8_t *data, DCAExssAsset *asset)
1833 AVCodecContext *avctx = s->avctx;
1834 DCAContext *dca = avctx->priv_data;
1835 int exss_mask = asset ? asset->extension_mask : 0;
1836 int ret = 0, ext = 0;
1838 // Parse (X)XCH unless downmixing
1839 if (!dca->request_channel_layout) {
1840 if (exss_mask & DCA_EXSS_XXCH) {
1841 if ((ret = init_get_bits8(&s->gb, data + asset->xxch_offset, asset->xxch_size)) < 0)
1843 ret = parse_xxch_frame(s);
1844 ext = DCA_EXSS_XXCH;
1845 } else if (s->xxch_pos) {
1847 skip_bits_long(&s->gb, s->xxch_pos);
1848 ret = parse_xxch_frame(s);
1850 } else if (s->xch_pos) {
1852 skip_bits_long(&s->gb, s->xch_pos);
1853 ret = parse_xch_frame(s);
1857 // Revert to primary channel set in case (X)XCH parsing fails
1859 if (avctx->err_recognition & AV_EF_EXPLODE)
1861 s->nchannels = ff_dca_channels[s->audio_mode];
1862 s->ch_mask = audio_mode_ch_mask[s->audio_mode];
1864 s->ch_mask |= DCA_SPEAKER_MASK_LFE1;
1866 s->ext_audio_mask |= ext;
1871 if (exss_mask & DCA_EXSS_XBR) {
1872 if ((ret = init_get_bits8(&s->gb, data + asset->xbr_offset, asset->xbr_size)) < 0)
1874 if ((ret = parse_xbr_frame(s)) < 0) {
1875 if (avctx->err_recognition & AV_EF_EXPLODE)
1878 s->ext_audio_mask |= DCA_EXSS_XBR;
1882 // Parse X96 unless decoding XLL
1883 if (!(dca->packet & DCA_PACKET_XLL)) {
1884 if (exss_mask & DCA_EXSS_X96) {
1885 if ((ret = init_get_bits8(&s->gb, data + asset->x96_offset, asset->x96_size)) < 0)
1887 if ((ret = parse_x96_frame_exss(s)) < 0) {
1888 if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
1891 s->ext_audio_mask |= DCA_EXSS_X96;
1893 } else if (s->x96_pos) {
1895 skip_bits_long(&s->gb, s->x96_pos);
1896 if ((ret = parse_x96_frame(s)) < 0) {
1897 if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
1900 s->ext_audio_mask |= DCA_CSS_X96;
1908 static int map_prm_ch_to_spkr(DCACoreDecoder *s, int ch)
1912 // Try to map this channel to core first
1913 pos = ff_dca_channels[s->audio_mode];
1915 spkr = prm_ch_to_spkr_map[s->audio_mode][ch];
1916 if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) {
1917 if (s->xxch_core_mask & (1U << spkr))
1919 if (spkr == DCA_SPEAKER_Ls && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss))
1920 return DCA_SPEAKER_Lss;
1921 if (spkr == DCA_SPEAKER_Rs && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss))
1922 return DCA_SPEAKER_Rss;
1929 if ((s->ext_audio_mask & DCA_CSS_XCH) && ch == pos)
1930 return DCA_SPEAKER_Cs;
1933 if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) {
1934 for (spkr = DCA_SPEAKER_Cs; spkr < s->xxch_mask_nbits; spkr++)
1935 if (s->xxch_spkr_mask & (1U << spkr))
1944 static void erase_dsp_history(DCACoreDecoder *s)
1946 memset(s->dcadsp_data, 0, sizeof(s->dcadsp_data));
1947 s->output_history_lfe_fixed = 0;
1948 s->output_history_lfe_float = 0;
1951 static void set_filter_mode(DCACoreDecoder *s, int mode)
1953 if (s->filter_mode != mode) {
1954 erase_dsp_history(s);
1955 s->filter_mode = mode;
1959 int ff_dca_core_filter_fixed(DCACoreDecoder *s, int x96_synth)
1961 int n, ch, spkr, nsamples, x96_nchannels = 0;
1962 const int32_t *filter_coeff;
1965 // Externally set x96_synth flag implies that X96 synthesis should be
1966 // enabled, yet actual X96 subband data should be discarded. This is a
1967 // special case for lossless residual decoder that ignores X96 data if
1969 if (!x96_synth && (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96))) {
1970 x96_nchannels = s->x96_nchannels;
1976 s->output_rate = s->sample_rate << x96_synth;
1977 s->npcmsamples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth;
1979 // Reallocate PCM output buffer
1980 av_fast_malloc(&s->output_buffer, &s->output_size,
1981 nsamples * av_popcount(s->ch_mask) * sizeof(int32_t));
1982 if (!s->output_buffer)
1983 return AVERROR(ENOMEM);
1985 ptr = (int32_t *)s->output_buffer;
1986 for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) {
1987 if (s->ch_mask & (1U << spkr)) {
1988 s->output_samples[spkr] = ptr;
1991 s->output_samples[spkr] = NULL;
1995 // Handle change of filtering mode
1996 set_filter_mode(s, x96_synth | DCA_FILTER_MODE_FIXED);
2000 filter_coeff = ff_dca_fir_64bands_fixed;
2001 else if (s->filter_perfect)
2002 filter_coeff = ff_dca_fir_32bands_perfect_fixed;
2004 filter_coeff = ff_dca_fir_32bands_nonperfect_fixed;
2006 // Filter primary channels
2007 for (ch = 0; ch < s->nchannels; ch++) {
2008 // Map this primary channel to speaker
2009 spkr = map_prm_ch_to_spkr(s, ch);
2011 return AVERROR(EINVAL);
2013 // Filter bank reconstruction
2014 s->dcadsp->sub_qmf_fixed[x96_synth](
2017 s->output_samples[spkr],
2018 s->subband_samples[ch],
2019 ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL,
2020 s->dcadsp_data[ch].u.fix.hist1,
2021 &s->dcadsp_data[ch].offset,
2022 s->dcadsp_data[ch].u.fix.hist2,
2027 // Filter LFE channel
2028 if (s->lfe_present) {
2029 int32_t *samples = s->output_samples[DCA_SPEAKER_LFE1];
2030 int nlfesamples = s->npcmblocks >> 1;
2033 if (s->lfe_present == DCA_LFE_FLAG_128) {
2034 av_log(s->avctx, AV_LOG_ERROR, "Fixed point mode doesn't support LFF=1\n");
2035 return AVERROR(EINVAL);
2038 // Offset intermediate buffer for X96
2040 samples += nsamples / 2;
2042 // Interpolate LFE channel
2043 s->dcadsp->lfe_fir_fixed(samples, s->lfe_samples + DCA_LFE_HISTORY,
2044 ff_dca_lfe_fir_64_fixed, s->npcmblocks);
2047 // Filter 96 kHz oversampled LFE PCM to attenuate high frequency
2048 // (47.6 - 48.0 kHz) components of interpolation image
2049 s->dcadsp->lfe_x96_fixed(s->output_samples[DCA_SPEAKER_LFE1],
2050 samples, &s->output_history_lfe_fixed,
2055 // Update LFE history
2056 for (n = DCA_LFE_HISTORY - 1; n >= 0; n--)
2057 s->lfe_samples[n] = s->lfe_samples[nlfesamples + n];
2063 static int filter_frame_fixed(DCACoreDecoder *s, AVFrame *frame)
2065 AVCodecContext *avctx = s->avctx;
2066 DCAContext *dca = avctx->priv_data;
2067 int i, n, ch, ret, spkr, nsamples;
2069 // Don't filter twice when falling back from XLL
2070 if (!(dca->packet & DCA_PACKET_XLL) && (ret = ff_dca_core_filter_fixed(s, 0)) < 0)
2073 avctx->sample_rate = s->output_rate;
2074 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
2075 avctx->bits_per_raw_sample = 24;
2077 frame->nb_samples = nsamples = s->npcmsamples;
2078 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2081 // Undo embedded XCH downmix
2082 if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
2083 && s->audio_mode >= DCA_AMODE_2F2R) {
2084 s->dcadsp->dmix_sub_xch(s->output_samples[DCA_SPEAKER_Ls],
2085 s->output_samples[DCA_SPEAKER_Rs],
2086 s->output_samples[DCA_SPEAKER_Cs],
2091 // Undo embedded XXCH downmix
2092 if ((s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH))
2093 && s->xxch_dmix_embedded) {
2094 int scale_inv = s->xxch_dmix_scale_inv;
2095 int *coeff_ptr = s->xxch_dmix_coeff;
2096 int xch_base = ff_dca_channels[s->audio_mode];
2097 av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX);
2099 // Undo embedded core downmix pre-scaling
2100 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2101 if (s->xxch_core_mask & (1U << spkr)) {
2102 s->dcadsp->dmix_scale_inv(s->output_samples[spkr],
2103 scale_inv, nsamples);
2108 for (ch = xch_base; ch < s->nchannels; ch++) {
2109 int src_spkr = map_prm_ch_to_spkr(s, ch);
2111 return AVERROR(EINVAL);
2112 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2113 if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) {
2114 int coeff = mul16(*coeff_ptr++, scale_inv);
2116 s->dcadsp->dmix_sub(s->output_samples[spkr ],
2117 s->output_samples[src_spkr],
2125 if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) {
2126 // Front sum/difference decoding
2127 if ((s->sumdiff_front && s->audio_mode > DCA_AMODE_MONO)
2128 || s->audio_mode == DCA_AMODE_STEREO_SUMDIFF) {
2129 s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_L],
2130 s->output_samples[DCA_SPEAKER_R],
2134 // Surround sum/difference decoding
2135 if (s->sumdiff_surround && s->audio_mode >= DCA_AMODE_2F2R) {
2136 s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_Ls],
2137 s->output_samples[DCA_SPEAKER_Rs],
2142 // Downmix primary channel set to stereo
2143 if (s->request_mask != s->ch_mask) {
2144 ff_dca_downmix_to_stereo_fixed(s->dcadsp,
2147 nsamples, s->ch_mask);
2150 for (i = 0; i < avctx->channels; i++) {
2151 int32_t *samples = s->output_samples[s->ch_remap[i]];
2152 int32_t *plane = (int32_t *)frame->extended_data[i];
2153 for (n = 0; n < nsamples; n++)
2154 plane[n] = clip23(samples[n]) * (1 << 8);
2160 static int filter_frame_float(DCACoreDecoder *s, AVFrame *frame)
2162 AVCodecContext *avctx = s->avctx;
2163 int x96_nchannels = 0, x96_synth = 0;
2164 int i, n, ch, ret, spkr, nsamples, nchannels;
2165 float *output_samples[DCA_SPEAKER_COUNT] = { NULL }, *ptr;
2166 const float *filter_coeff;
2168 if (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96)) {
2169 x96_nchannels = s->x96_nchannels;
2173 avctx->sample_rate = s->sample_rate << x96_synth;
2174 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2175 avctx->bits_per_raw_sample = 0;
2177 frame->nb_samples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth;
2178 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2181 // Build reverse speaker to channel mapping
2182 for (i = 0; i < avctx->channels; i++)
2183 output_samples[s->ch_remap[i]] = (float *)frame->extended_data[i];
2185 // Allocate space for extra channels
2186 nchannels = av_popcount(s->ch_mask) - avctx->channels;
2187 if (nchannels > 0) {
2188 av_fast_malloc(&s->output_buffer, &s->output_size,
2189 nsamples * nchannels * sizeof(float));
2190 if (!s->output_buffer)
2191 return AVERROR(ENOMEM);
2193 ptr = (float *)s->output_buffer;
2194 for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) {
2195 if (!(s->ch_mask & (1U << spkr)))
2197 if (output_samples[spkr])
2199 output_samples[spkr] = ptr;
2204 // Handle change of filtering mode
2205 set_filter_mode(s, x96_synth);
2209 filter_coeff = ff_dca_fir_64bands;
2210 else if (s->filter_perfect)
2211 filter_coeff = ff_dca_fir_32bands_perfect;
2213 filter_coeff = ff_dca_fir_32bands_nonperfect;
2215 // Filter primary channels
2216 for (ch = 0; ch < s->nchannels; ch++) {
2217 // Map this primary channel to speaker
2218 spkr = map_prm_ch_to_spkr(s, ch);
2220 return AVERROR(EINVAL);
2222 // Filter bank reconstruction
2223 s->dcadsp->sub_qmf_float[x96_synth](
2225 &s->imdct[x96_synth],
2226 output_samples[spkr],
2227 s->subband_samples[ch],
2228 ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL,
2229 s->dcadsp_data[ch].u.flt.hist1,
2230 &s->dcadsp_data[ch].offset,
2231 s->dcadsp_data[ch].u.flt.hist2,
2234 1.0f / (1 << (17 - x96_synth)));
2237 // Filter LFE channel
2238 if (s->lfe_present) {
2239 int dec_select = (s->lfe_present == DCA_LFE_FLAG_128);
2240 float *samples = output_samples[DCA_SPEAKER_LFE1];
2241 int nlfesamples = s->npcmblocks >> (dec_select + 1);
2243 // Offset intermediate buffer for X96
2245 samples += nsamples / 2;
2249 filter_coeff = ff_dca_lfe_fir_128;
2251 filter_coeff = ff_dca_lfe_fir_64;
2253 // Interpolate LFE channel
2254 s->dcadsp->lfe_fir_float[dec_select](
2255 samples, s->lfe_samples + DCA_LFE_HISTORY,
2256 filter_coeff, s->npcmblocks);
2259 // Filter 96 kHz oversampled LFE PCM to attenuate high frequency
2260 // (47.6 - 48.0 kHz) components of interpolation image
2261 s->dcadsp->lfe_x96_float(output_samples[DCA_SPEAKER_LFE1],
2262 samples, &s->output_history_lfe_float,
2266 // Update LFE history
2267 for (n = DCA_LFE_HISTORY - 1; n >= 0; n--)
2268 s->lfe_samples[n] = s->lfe_samples[nlfesamples + n];
2271 // Undo embedded XCH downmix
2272 if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
2273 && s->audio_mode >= DCA_AMODE_2F2R) {
2274 s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Ls],
2275 output_samples[DCA_SPEAKER_Cs],
2276 -M_SQRT1_2, nsamples);
2277 s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Rs],
2278 output_samples[DCA_SPEAKER_Cs],
2279 -M_SQRT1_2, nsamples);
2282 // Undo embedded XXCH downmix
2283 if ((s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH))
2284 && s->xxch_dmix_embedded) {
2285 float scale_inv = s->xxch_dmix_scale_inv * (1.0f / (1 << 16));
2286 int *coeff_ptr = s->xxch_dmix_coeff;
2287 int xch_base = ff_dca_channels[s->audio_mode];
2288 av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX);
2291 for (ch = xch_base; ch < s->nchannels; ch++) {
2292 int src_spkr = map_prm_ch_to_spkr(s, ch);
2294 return AVERROR(EINVAL);
2295 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2296 if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) {
2297 int coeff = *coeff_ptr++;
2299 s->float_dsp->vector_fmac_scalar(output_samples[ spkr],
2300 output_samples[src_spkr],
2301 coeff * (-1.0f / (1 << 15)),
2308 // Undo embedded core downmix pre-scaling
2309 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2310 if (s->xxch_core_mask & (1U << spkr)) {
2311 s->float_dsp->vector_fmul_scalar(output_samples[spkr],
2312 output_samples[spkr],
2313 scale_inv, nsamples);
2318 if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) {
2319 // Front sum/difference decoding
2320 if ((s->sumdiff_front && s->audio_mode > DCA_AMODE_MONO)
2321 || s->audio_mode == DCA_AMODE_STEREO_SUMDIFF) {
2322 s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_L],
2323 output_samples[DCA_SPEAKER_R],
2327 // Surround sum/difference decoding
2328 if (s->sumdiff_surround && s->audio_mode >= DCA_AMODE_2F2R) {
2329 s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_Ls],
2330 output_samples[DCA_SPEAKER_Rs],
2335 // Downmix primary channel set to stereo
2336 if (s->request_mask != s->ch_mask) {
2337 ff_dca_downmix_to_stereo_float(s->float_dsp, output_samples,
2339 nsamples, s->ch_mask);
2345 int ff_dca_core_filter_frame(DCACoreDecoder *s, AVFrame *frame)
2347 AVCodecContext *avctx = s->avctx;
2348 DCAContext *dca = avctx->priv_data;
2349 DCAExssAsset *asset = &dca->exss.assets[0];
2350 enum AVMatrixEncoding matrix_encoding;
2353 // Handle downmixing to stereo request
2354 if (dca->request_channel_layout == DCA_SPEAKER_LAYOUT_STEREO
2355 && s->audio_mode > DCA_AMODE_MONO && s->prim_dmix_embedded
2356 && (s->prim_dmix_type == DCA_DMIX_TYPE_LoRo ||
2357 s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
2358 s->request_mask = DCA_SPEAKER_LAYOUT_STEREO;
2360 s->request_mask = s->ch_mask;
2361 if (!ff_dca_set_channel_layout(avctx, s->ch_remap, s->request_mask))
2362 return AVERROR(EINVAL);
2364 // Force fixed point mode when falling back from XLL
2365 if ((avctx->flags & AV_CODEC_FLAG_BITEXACT) || ((dca->packet & DCA_PACKET_EXSS)
2366 && (asset->extension_mask & DCA_EXSS_XLL)))
2367 ret = filter_frame_fixed(s, frame);
2369 ret = filter_frame_float(s, frame);
2373 // Set profile, bit rate, etc
2374 if (s->ext_audio_mask & DCA_EXSS_MASK)
2375 avctx->profile = FF_PROFILE_DTS_HD_HRA;
2376 else if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH))
2377 avctx->profile = FF_PROFILE_DTS_ES;
2378 else if (s->ext_audio_mask & DCA_CSS_X96)
2379 avctx->profile = FF_PROFILE_DTS_96_24;
2381 avctx->profile = FF_PROFILE_DTS;
2383 if (s->bit_rate > 3 && !(s->ext_audio_mask & DCA_EXSS_MASK))
2384 avctx->bit_rate = s->bit_rate;
2386 avctx->bit_rate = 0;
2388 if (s->audio_mode == DCA_AMODE_STEREO_TOTAL || (s->request_mask != s->ch_mask &&
2389 s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
2390 matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
2392 matrix_encoding = AV_MATRIX_ENCODING_NONE;
2393 if ((ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
2399 av_cold void ff_dca_core_flush(DCACoreDecoder *s)
2401 if (s->subband_buffer) {
2402 erase_adpcm_history(s);
2403 memset(s->lfe_samples, 0, DCA_LFE_HISTORY * sizeof(int32_t));
2406 if (s->x96_subband_buffer)
2407 erase_x96_adpcm_history(s);
2409 erase_dsp_history(s);
2412 av_cold int ff_dca_core_init(DCACoreDecoder *s)
2414 if (!(s->float_dsp = avpriv_float_dsp_alloc(0)))
2416 if (!(s->fixed_dsp = avpriv_alloc_fixed_dsp(0)))
2419 ff_dcadct_init(&s->dcadct);
2420 if (ff_mdct_init(&s->imdct[0], 6, 1, 1.0) < 0)
2422 if (ff_mdct_init(&s->imdct[1], 7, 1, 1.0) < 0)
2424 ff_synth_filter_init(&s->synth);
2430 av_cold void ff_dca_core_close(DCACoreDecoder *s)
2432 av_freep(&s->float_dsp);
2433 av_freep(&s->fixed_dsp);
2435 ff_mdct_end(&s->imdct[0]);
2436 ff_mdct_end(&s->imdct[1]);
2438 av_freep(&s->subband_buffer);
2439 s->subband_size = 0;
2441 av_freep(&s->x96_subband_buffer);
2442 s->x96_subband_size = 0;
2444 av_freep(&s->output_buffer);