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"
39 AMODE_MONO, // Mode 0: A (mono)
40 AMODE_MONO_DUAL, // Mode 1: A + B (dual mono)
41 AMODE_STEREO, // Mode 2: L + R (stereo)
42 AMODE_STEREO_SUMDIFF, // Mode 3: (L+R) + (L-R) (sum-diff)
43 AMODE_STEREO_TOTAL, // Mode 4: LT + RT (left and right total)
44 AMODE_3F, // Mode 5: C + L + R
45 AMODE_2F1R, // Mode 6: L + R + S
46 AMODE_3F1R, // Mode 7: C + L + R + S
47 AMODE_2F2R, // Mode 8: L + R + SL + SR
48 AMODE_3F2R, // Mode 9: C + L + R + SL + SR
66 static const int8_t prm_ch_to_spkr_map[AMODE_COUNT][5] = {
67 { DCA_SPEAKER_C, -1, -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_L, DCA_SPEAKER_R, -1, -1, -1 },
72 { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R , -1, -1 },
73 { DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Cs, -1, -1 },
74 { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R , DCA_SPEAKER_Cs, -1 },
75 { DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Ls, DCA_SPEAKER_Rs, -1 },
76 { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Ls, DCA_SPEAKER_Rs }
79 static const uint8_t audio_mode_ch_mask[AMODE_COUNT] = {
80 DCA_SPEAKER_LAYOUT_MONO,
81 DCA_SPEAKER_LAYOUT_STEREO,
82 DCA_SPEAKER_LAYOUT_STEREO,
83 DCA_SPEAKER_LAYOUT_STEREO,
84 DCA_SPEAKER_LAYOUT_STEREO,
85 DCA_SPEAKER_LAYOUT_3_0,
86 DCA_SPEAKER_LAYOUT_2_1,
87 DCA_SPEAKER_LAYOUT_3_1,
88 DCA_SPEAKER_LAYOUT_2_2,
89 DCA_SPEAKER_LAYOUT_5POINT0
92 static const uint8_t block_code_nbits[7] = {
93 7, 10, 12, 13, 15, 17, 19
96 static int dca_get_vlc(GetBitContext *s, DCAVLC *v, int i)
98 return get_vlc2(s, v->vlc[i].table, v->vlc[i].bits, v->max_depth) + v->offset;
101 static void get_array(GetBitContext *s, int32_t *array, int size, int n)
105 for (i = 0; i < size; i++)
106 array[i] = get_sbits(s, n);
109 // 5.3.1 - Bit stream header
110 static int parse_frame_header(DCACoreDecoder *s)
112 int normal_frame, pcmr_index;
115 normal_frame = get_bits1(&s->gb);
117 // Deficit sample count
118 if (get_bits(&s->gb, 5) != DCA_PCMBLOCK_SAMPLES - 1) {
119 av_log(s->avctx, AV_LOG_ERROR, "Deficit samples are not supported\n");
120 return normal_frame ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME;
124 s->crc_present = get_bits1(&s->gb);
126 // Number of PCM sample blocks
127 s->npcmblocks = get_bits(&s->gb, 7) + 1;
128 if (s->npcmblocks & (DCA_SUBBAND_SAMPLES - 1)) {
129 av_log(s->avctx, AV_LOG_ERROR, "Unsupported number of PCM sample blocks (%d)\n", s->npcmblocks);
130 return (s->npcmblocks < 6 || normal_frame) ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME;
133 // Primary frame byte size
134 s->frame_size = get_bits(&s->gb, 14) + 1;
135 if (s->frame_size < 96) {
136 av_log(s->avctx, AV_LOG_ERROR, "Invalid core frame size (%d bytes)\n", s->frame_size);
137 return AVERROR_INVALIDDATA;
140 // Audio channel arrangement
141 s->audio_mode = get_bits(&s->gb, 6);
142 if (s->audio_mode >= AMODE_COUNT) {
143 av_log(s->avctx, AV_LOG_ERROR, "Unsupported audio channel arrangement (%d)\n", s->audio_mode);
144 return AVERROR_PATCHWELCOME;
147 // Core audio sampling frequency
148 s->sample_rate = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
149 if (!s->sample_rate) {
150 av_log(s->avctx, AV_LOG_ERROR, "Invalid core audio sampling frequency\n");
151 return AVERROR_INVALIDDATA;
154 // Transmission bit rate
155 s->bit_rate = ff_dca_bit_rates[get_bits(&s->gb, 5)];
160 // Embedded dynamic range flag
161 s->drc_present = get_bits1(&s->gb);
163 // Embedded time stamp flag
164 s->ts_present = get_bits1(&s->gb);
166 // Auxiliary data flag
167 s->aux_present = get_bits1(&s->gb);
169 // HDCD mastering flag
172 // Extension audio descriptor flag
173 s->ext_audio_type = get_bits(&s->gb, 3);
175 // Extended coding flag
176 s->ext_audio_present = get_bits1(&s->gb);
178 // Audio sync word insertion flag
179 s->sync_ssf = get_bits1(&s->gb);
181 // Low frequency effects flag
182 s->lfe_present = get_bits(&s->gb, 2);
183 if (s->lfe_present == LFE_FLAG_INVALID) {
184 av_log(s->avctx, AV_LOG_ERROR, "Invalid low frequency effects flag\n");
185 return AVERROR_INVALIDDATA;
188 // Predictor history flag switch
189 s->predictor_history = get_bits1(&s->gb);
191 // Header CRC check bytes
193 skip_bits(&s->gb, 16);
195 // Multirate interpolator switch
196 s->filter_perfect = get_bits1(&s->gb);
198 // Encoder software revision
199 skip_bits(&s->gb, 4);
202 skip_bits(&s->gb, 2);
204 // Source PCM resolution
205 s->source_pcm_res = ff_dca_bits_per_sample[pcmr_index = get_bits(&s->gb, 3)];
206 if (!s->source_pcm_res) {
207 av_log(s->avctx, AV_LOG_ERROR, "Invalid source PCM resolution\n");
208 return AVERROR_INVALIDDATA;
210 s->es_format = pcmr_index & 1;
212 // Front sum/difference flag
213 s->sumdiff_front = get_bits1(&s->gb);
215 // Surround sum/difference flag
216 s->sumdiff_surround = get_bits1(&s->gb);
218 // Dialog normalization / unspecified
219 skip_bits(&s->gb, 4);
224 // 5.3.2 - Primary audio coding header
225 static int parse_coding_header(DCACoreDecoder *s, enum HeaderType header, int xch_base)
227 int n, ch, nchannels, header_size = 0, header_pos = get_bits_count(&s->gb);
228 unsigned int mask, index;
230 if (get_bits_left(&s->gb) < 0)
231 return AVERROR_INVALIDDATA;
235 // Number of subframes
236 s->nsubframes = get_bits(&s->gb, 4) + 1;
238 // Number of primary audio channels
239 s->nchannels = get_bits(&s->gb, 3) + 1;
240 if (s->nchannels != ff_dca_channels[s->audio_mode]) {
241 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);
242 return AVERROR_INVALIDDATA;
244 av_assert1(s->nchannels <= DCA_CHANNELS - 2);
246 s->ch_mask = audio_mode_ch_mask[s->audio_mode];
248 // Add LFE channel if present
250 s->ch_mask |= DCA_SPEAKER_MASK_LFE1;
254 s->nchannels = ff_dca_channels[s->audio_mode] + 1;
255 av_assert1(s->nchannels <= DCA_CHANNELS - 1);
256 s->ch_mask |= DCA_SPEAKER_MASK_Cs;
260 // Channel set header length
261 header_size = get_bits(&s->gb, 7) + 1;
264 if (s->xxch_crc_present
265 && ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) {
266 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH channel set header checksum\n");
267 return AVERROR_INVALIDDATA;
270 // Number of channels in a channel set
271 nchannels = get_bits(&s->gb, 3) + 1;
272 if (nchannels > DCA_XXCH_CHANNELS_MAX) {
273 avpriv_request_sample(s->avctx, "%d XXCH channels", nchannels);
274 return AVERROR_PATCHWELCOME;
276 s->nchannels = ff_dca_channels[s->audio_mode] + nchannels;
277 av_assert1(s->nchannels <= DCA_CHANNELS);
279 // Loudspeaker layout mask
280 mask = get_bits_long(&s->gb, s->xxch_mask_nbits - DCA_SPEAKER_Cs);
281 s->xxch_spkr_mask = mask << DCA_SPEAKER_Cs;
283 if (av_popcount(s->xxch_spkr_mask) != nchannels) {
284 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH speaker layout mask (%#x)\n", s->xxch_spkr_mask);
285 return AVERROR_INVALIDDATA;
288 if (s->xxch_core_mask & s->xxch_spkr_mask) {
289 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);
290 return AVERROR_INVALIDDATA;
293 // Combine core and XXCH masks together
294 s->ch_mask = s->xxch_core_mask | s->xxch_spkr_mask;
296 // Downmix coefficients present in stream
297 if (get_bits1(&s->gb)) {
298 int *coeff_ptr = s->xxch_dmix_coeff;
300 // Downmix already performed by encoder
301 s->xxch_dmix_embedded = get_bits1(&s->gb);
303 // Downmix scale factor
304 index = get_bits(&s->gb, 6) * 4 - FF_DCA_DMIXTABLE_OFFSET - 3;
305 if (index >= FF_DCA_INV_DMIXTABLE_SIZE) {
306 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix scale index (%d)\n", index);
307 return AVERROR_INVALIDDATA;
309 s->xxch_dmix_scale_inv = ff_dca_inv_dmixtable[index];
311 // Downmix channel mapping mask
312 for (ch = 0; ch < nchannels; ch++) {
313 mask = get_bits_long(&s->gb, s->xxch_mask_nbits);
314 if ((mask & s->xxch_core_mask) != mask) {
315 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix channel mapping mask (%#x)\n", mask);
316 return AVERROR_INVALIDDATA;
318 s->xxch_dmix_mask[ch] = mask;
321 // Downmix coefficients
322 for (ch = 0; ch < nchannels; ch++) {
323 for (n = 0; n < s->xxch_mask_nbits; n++) {
324 if (s->xxch_dmix_mask[ch] & (1U << n)) {
325 int code = get_bits(&s->gb, 7);
326 int sign = (code >> 6) - 1;
328 index = code * 4 - 3;
329 if (index >= FF_DCA_DMIXTABLE_SIZE) {
330 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix coefficient index (%d)\n", index);
331 return AVERROR_INVALIDDATA;
333 *coeff_ptr++ = (ff_dca_dmixtable[index] ^ sign) - sign;
341 s->xxch_dmix_embedded = 0;
347 // Subband activity count
348 for (ch = xch_base; ch < s->nchannels; ch++) {
349 s->nsubbands[ch] = get_bits(&s->gb, 5) + 2;
350 if (s->nsubbands[ch] > DCA_SUBBANDS) {
351 av_log(s->avctx, AV_LOG_ERROR, "Invalid subband activity count\n");
352 return AVERROR_INVALIDDATA;
356 // High frequency VQ start subband
357 for (ch = xch_base; ch < s->nchannels; ch++)
358 s->subband_vq_start[ch] = get_bits(&s->gb, 5) + 1;
360 // Joint intensity coding index
361 for (ch = xch_base; ch < s->nchannels; ch++) {
362 if ((n = get_bits(&s->gb, 3)) && header == HEADER_XXCH)
364 if (n > s->nchannels) {
365 av_log(s->avctx, AV_LOG_ERROR, "Invalid joint intensity coding index\n");
366 return AVERROR_INVALIDDATA;
368 s->joint_intensity_index[ch] = n;
371 // Transient mode code book
372 for (ch = xch_base; ch < s->nchannels; ch++)
373 s->transition_mode_sel[ch] = get_bits(&s->gb, 2);
375 // Scale factor code book
376 for (ch = xch_base; ch < s->nchannels; ch++) {
377 s->scale_factor_sel[ch] = get_bits(&s->gb, 3);
378 if (s->scale_factor_sel[ch] == 7) {
379 av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor code book\n");
380 return AVERROR_INVALIDDATA;
384 // Bit allocation quantizer select
385 for (ch = xch_base; ch < s->nchannels; ch++) {
386 s->bit_allocation_sel[ch] = get_bits(&s->gb, 3);
387 if (s->bit_allocation_sel[ch] == 7) {
388 av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation quantizer select\n");
389 return AVERROR_INVALIDDATA;
393 // Quantization index codebook select
394 for (n = 0; n < DCA_CODE_BOOKS; n++)
395 for (ch = xch_base; ch < s->nchannels; ch++)
396 s->quant_index_sel[ch][n] = get_bits(&s->gb, ff_dca_quant_index_sel_nbits[n]);
398 // Scale factor adjustment index
399 for (n = 0; n < DCA_CODE_BOOKS; n++)
400 for (ch = xch_base; ch < s->nchannels; ch++)
401 if (s->quant_index_sel[ch][n] < ff_dca_quant_index_group_size[n])
402 s->scale_factor_adj[ch][n] = ff_dca_scale_factor_adj[get_bits(&s->gb, 2)];
404 if (header == HEADER_XXCH) {
407 // CRC16 of channel set header
408 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
409 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set header\n");
410 return AVERROR_INVALIDDATA;
413 // Audio header CRC check word
415 skip_bits(&s->gb, 16);
421 static inline int parse_scale(DCACoreDecoder *s, int *scale_index, int sel)
423 const uint32_t *scale_table;
424 unsigned int scale_size;
426 // Select the root square table
428 scale_table = ff_dca_scale_factor_quant7;
429 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7);
431 scale_table = ff_dca_scale_factor_quant6;
432 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6);
435 // If Huffman code was used, the difference of scales was encoded
437 *scale_index += dca_get_vlc(&s->gb, &ff_dca_vlc_scale_factor, sel);
439 *scale_index = get_bits(&s->gb, sel + 1);
441 // Look up scale factor from the root square table
442 if ((unsigned int)*scale_index >= scale_size) {
443 av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor index\n");
444 return AVERROR_INVALIDDATA;
447 return scale_table[*scale_index];
450 static inline int parse_joint_scale(DCACoreDecoder *s, int sel)
454 // Absolute value was encoded even when Huffman code was used
456 scale_index = dca_get_vlc(&s->gb, &ff_dca_vlc_scale_factor, sel);
458 scale_index = get_bits(&s->gb, sel + 1);
463 // Look up joint scale factor
464 if ((unsigned int)scale_index >= FF_ARRAY_ELEMS(ff_dca_joint_scale_factors)) {
465 av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor index\n");
466 return AVERROR_INVALIDDATA;
469 return ff_dca_joint_scale_factors[scale_index];
472 // 5.4.1 - Primary audio coding side information
473 static int parse_subframe_header(DCACoreDecoder *s, int sf,
474 enum HeaderType header, int xch_base)
478 if (get_bits_left(&s->gb) < 0)
479 return AVERROR_INVALIDDATA;
481 if (header == HEADER_CORE) {
483 s->nsubsubframes[sf] = get_bits(&s->gb, 2) + 1;
485 // Partial subsubframe sample count
486 skip_bits(&s->gb, 3);
490 for (ch = xch_base; ch < s->nchannels; ch++)
491 for (band = 0; band < s->nsubbands[ch]; band++)
492 s->prediction_mode[ch][band] = get_bits1(&s->gb);
494 // Prediction coefficients VQ address
495 for (ch = xch_base; ch < s->nchannels; ch++)
496 for (band = 0; band < s->nsubbands[ch]; band++)
497 if (s->prediction_mode[ch][band])
498 s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12);
500 // Bit allocation index
501 for (ch = xch_base; ch < s->nchannels; ch++) {
502 int sel = s->bit_allocation_sel[ch];
504 for (band = 0; band < s->subband_vq_start[ch]; band++) {
508 abits = dca_get_vlc(&s->gb, &ff_dca_vlc_bit_allocation, sel);
510 abits = get_bits(&s->gb, sel - 1);
512 if (abits > DCA_ABITS_MAX) {
513 av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation index\n");
514 return AVERROR_INVALIDDATA;
517 s->bit_allocation[ch][band] = abits;
522 for (ch = xch_base; ch < s->nchannels; ch++) {
523 // Clear transition mode for all subbands
524 memset(s->transition_mode[sf][ch], 0, sizeof(s->transition_mode[0][0]));
526 // Transient possible only if more than one subsubframe
527 if (s->nsubsubframes[sf] > 1) {
528 int sel = s->transition_mode_sel[ch];
529 for (band = 0; band < s->subband_vq_start[ch]; band++)
530 if (s->bit_allocation[ch][band])
531 s->transition_mode[sf][ch][band] = dca_get_vlc(&s->gb, &ff_dca_vlc_transition_mode, sel);
536 for (ch = xch_base; ch < s->nchannels; ch++) {
537 int sel = s->scale_factor_sel[ch];
540 // Extract scales for subbands up to VQ
541 for (band = 0; band < s->subband_vq_start[ch]; band++) {
542 if (s->bit_allocation[ch][band]) {
543 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
545 s->scale_factors[ch][band][0] = ret;
546 if (s->transition_mode[sf][ch][band]) {
547 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
549 s->scale_factors[ch][band][1] = ret;
552 s->scale_factors[ch][band][0] = 0;
556 // High frequency VQ subbands
557 for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++) {
558 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
560 s->scale_factors[ch][band][0] = ret;
564 // Joint subband codebook select
565 for (ch = xch_base; ch < s->nchannels; ch++) {
566 if (s->joint_intensity_index[ch]) {
567 s->joint_scale_sel[ch] = get_bits(&s->gb, 3);
568 if (s->joint_scale_sel[ch] == 7) {
569 av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor code book\n");
570 return AVERROR_INVALIDDATA;
575 // Scale factors for joint subband coding
576 for (ch = xch_base; ch < s->nchannels; ch++) {
577 int src_ch = s->joint_intensity_index[ch] - 1;
579 int sel = s->joint_scale_sel[ch];
580 for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) {
581 if ((ret = parse_joint_scale(s, sel)) < 0)
583 s->joint_scale_factors[ch][band] = ret;
588 // Dynamic range coefficient
589 if (s->drc_present && header == HEADER_CORE)
590 skip_bits(&s->gb, 8);
592 // Side information CRC check word
594 skip_bits(&s->gb, 16);
599 #ifndef decode_blockcodes
600 static inline int decode_blockcodes(int code1, int code2, int levels, int32_t *audio)
602 int offset = (levels - 1) / 2;
605 for (n = 0; n < DCA_SUBBAND_SAMPLES / 2; n++) {
606 div = FASTDIV(code1, levels);
607 audio[n] = code1 - div * levels - offset;
610 for (; n < DCA_SUBBAND_SAMPLES; n++) {
611 div = FASTDIV(code2, levels);
612 audio[n] = code2 - div * levels - offset;
616 return code1 | code2;
620 static inline int parse_block_codes(DCACoreDecoder *s, int32_t *audio, int abits)
622 // Extract block code indices from the bit stream
623 int code1 = get_bits(&s->gb, block_code_nbits[abits - 1]);
624 int code2 = get_bits(&s->gb, block_code_nbits[abits - 1]);
625 int levels = ff_dca_quant_levels[abits];
627 // Look up samples from the block code book
628 if (decode_blockcodes(code1, code2, levels, audio)) {
629 av_log(s->avctx, AV_LOG_ERROR, "Failed to decode block code(s)\n");
630 return AVERROR_INVALIDDATA;
636 static inline int parse_huffman_codes(DCACoreDecoder *s, int32_t *audio, int abits, int sel)
640 // Extract Huffman codes from the bit stream
641 for (i = 0; i < DCA_SUBBAND_SAMPLES; i++)
642 audio[i] = dca_get_vlc(&s->gb, &ff_dca_vlc_quant_index[abits - 1], sel);
647 static inline int extract_audio(DCACoreDecoder *s, int32_t *audio, int abits, int ch)
649 av_assert1(abits >= 0 && abits <= DCA_ABITS_MAX);
653 memset(audio, 0, DCA_SUBBAND_SAMPLES * sizeof(*audio));
657 if (abits <= DCA_CODE_BOOKS) {
658 int sel = s->quant_index_sel[ch][abits - 1];
659 if (sel < ff_dca_quant_index_group_size[abits - 1]) {
661 return parse_huffman_codes(s, audio, abits, sel);
665 return parse_block_codes(s, audio, abits);
669 // No further encoding
670 get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3);
674 static inline void inverse_adpcm(int32_t **subband_samples,
675 const int16_t *vq_index,
676 const int8_t *prediction_mode,
677 int sb_start, int sb_end,
682 for (i = sb_start; i < sb_end; i++) {
683 if (prediction_mode[i]) {
684 const int pred_id = vq_index[i];
685 int32_t *ptr = subband_samples[i] + ofs;
686 for (j = 0; j < len; j++) {
687 int32_t x = ff_dcaadpcm_predict(pred_id, ptr + j - DCA_ADPCM_COEFFS);
688 ptr[j] = clip23(ptr[j] + x);
694 // 5.5 - Primary audio data arrays
695 static int parse_subframe_audio(DCACoreDecoder *s, int sf, enum HeaderType header,
696 int xch_base, int *sub_pos, int *lfe_pos)
698 int32_t audio[16], scale;
699 int n, ssf, ofs, ch, band;
701 // Check number of subband samples in this subframe
702 int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES;
703 if (*sub_pos + nsamples > s->npcmblocks) {
704 av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
705 return AVERROR_INVALIDDATA;
708 if (get_bits_left(&s->gb) < 0)
709 return AVERROR_INVALIDDATA;
711 // VQ encoded subbands
712 for (ch = xch_base; ch < s->nchannels; ch++) {
713 int32_t vq_index[DCA_SUBBANDS];
715 for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++)
716 // Extract the VQ address from the bit stream
717 vq_index[band] = get_bits(&s->gb, 10);
719 if (s->subband_vq_start[ch] < s->nsubbands[ch]) {
720 s->dcadsp->decode_hf(s->subband_samples[ch], vq_index,
721 ff_dca_high_freq_vq, s->scale_factors[ch],
722 s->subband_vq_start[ch], s->nsubbands[ch],
727 // Low frequency effect data
728 if (s->lfe_present && header == HEADER_CORE) {
731 // Determine number of LFE samples in this subframe
732 int nlfesamples = 2 * s->lfe_present * s->nsubsubframes[sf];
733 av_assert1((unsigned int)nlfesamples <= FF_ARRAY_ELEMS(audio));
735 // Extract LFE samples from the bit stream
736 get_array(&s->gb, audio, nlfesamples, 8);
738 // Extract scale factor index from the bit stream
739 index = get_bits(&s->gb, 8);
740 if (index >= FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7)) {
741 av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE scale factor index\n");
742 return AVERROR_INVALIDDATA;
745 // Look up the 7-bit root square quantization table
746 scale = ff_dca_scale_factor_quant7[index];
748 // Account for quantizer step size which is 0.035
749 scale = mul23(4697620 /* 0.035 * (1 << 27) */, scale);
751 // Scale and take the LFE samples
752 for (n = 0, ofs = *lfe_pos; n < nlfesamples; n++, ofs++)
753 s->lfe_samples[ofs] = clip23(audio[n] * scale >> 4);
755 // Advance LFE sample pointer for the next subframe
760 for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
761 for (ch = xch_base; ch < s->nchannels; ch++) {
762 if (get_bits_left(&s->gb) < 0)
763 return AVERROR_INVALIDDATA;
765 // Not high frequency VQ subbands
766 for (band = 0; band < s->subband_vq_start[ch]; band++) {
767 int ret, trans_ssf, abits = s->bit_allocation[ch][band];
770 // Extract bits from the bit stream
771 if ((ret = extract_audio(s, audio, abits, ch)) < 0)
774 // Select quantization step size table and look up
775 // quantization step size
776 if (s->bit_rate == 3)
777 step_size = ff_dca_lossless_quant[abits];
779 step_size = ff_dca_lossy_quant[abits];
781 // Identify transient location
782 trans_ssf = s->transition_mode[sf][ch][band];
784 // Determine proper scale factor
785 if (trans_ssf == 0 || ssf < trans_ssf)
786 scale = s->scale_factors[ch][band][0];
788 scale = s->scale_factors[ch][band][1];
790 // Adjust scale factor when SEL indicates Huffman code
792 int64_t adj = s->scale_factor_adj[ch][abits - 1];
793 scale = clip23(adj * scale >> 22);
796 ff_dca_core_dequantize(s->subband_samples[ch][band] + ofs,
797 audio, step_size, scale, 0, DCA_SUBBAND_SAMPLES);
802 if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
803 av_log(s->avctx, AV_LOG_ERROR, "DSYNC check failed\n");
804 return AVERROR_INVALIDDATA;
807 ofs += DCA_SUBBAND_SAMPLES;
811 for (ch = xch_base; ch < s->nchannels; ch++) {
812 inverse_adpcm(s->subband_samples[ch], s->prediction_vq_index[ch],
813 s->prediction_mode[ch], 0, s->nsubbands[ch],
817 // Joint subband coding
818 for (ch = xch_base; ch < s->nchannels; ch++) {
819 int src_ch = s->joint_intensity_index[ch] - 1;
821 s->dcadsp->decode_joint(s->subband_samples[ch], s->subband_samples[src_ch],
822 s->joint_scale_factors[ch], s->nsubbands[ch],
823 s->nsubbands[src_ch], *sub_pos, nsamples);
827 // Advance subband sample pointer for the next subframe
832 static void erase_adpcm_history(DCACoreDecoder *s)
836 // Erase ADPCM history from previous frame if
837 // predictor history switch was disabled
838 for (ch = 0; ch < DCA_CHANNELS; ch++)
839 for (band = 0; band < DCA_SUBBANDS; band++)
840 AV_ZERO128(s->subband_samples[ch][band] - DCA_ADPCM_COEFFS);
845 static int alloc_sample_buffer(DCACoreDecoder *s)
847 int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks;
848 int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS;
849 int nlfesamples = DCA_LFE_HISTORY + s->npcmblocks / 2;
850 unsigned int size = s->subband_size;
853 // Reallocate subband sample buffer
854 av_fast_mallocz(&s->subband_buffer, &s->subband_size,
855 (nframesamples + nlfesamples) * sizeof(int32_t));
856 if (!s->subband_buffer)
857 return AVERROR(ENOMEM);
859 if (size != s->subband_size) {
860 for (ch = 0; ch < DCA_CHANNELS; ch++)
861 for (band = 0; band < DCA_SUBBANDS; band++)
862 s->subband_samples[ch][band] = s->subband_buffer +
863 (ch * DCA_SUBBANDS + band) * nchsamples + DCA_ADPCM_COEFFS;
864 s->lfe_samples = s->subband_buffer + nframesamples;
867 if (!s->predictor_history)
868 erase_adpcm_history(s);
873 static int parse_frame_data(DCACoreDecoder *s, enum HeaderType header, int xch_base)
875 int sf, ch, ret, band, sub_pos, lfe_pos;
877 if ((ret = parse_coding_header(s, header, xch_base)) < 0)
880 for (sf = 0, sub_pos = 0, lfe_pos = DCA_LFE_HISTORY; sf < s->nsubframes; sf++) {
881 if ((ret = parse_subframe_header(s, sf, header, xch_base)) < 0)
883 if ((ret = parse_subframe_audio(s, sf, header, xch_base, &sub_pos, &lfe_pos)) < 0)
887 for (ch = xch_base; ch < s->nchannels; ch++) {
888 // Determine number of active subbands for this channel
889 int nsubbands = s->nsubbands[ch];
890 if (s->joint_intensity_index[ch])
891 nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]);
893 // Update history for ADPCM
894 for (band = 0; band < nsubbands; band++) {
895 int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS;
896 AV_COPY128(samples, samples + s->npcmblocks);
899 // Clear inactive subbands
900 for (; band < DCA_SUBBANDS; band++) {
901 int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS;
902 memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t));
911 static int parse_xch_frame(DCACoreDecoder *s)
915 if (s->ch_mask & DCA_SPEAKER_MASK_Cs) {
916 av_log(s->avctx, AV_LOG_ERROR, "XCH with Cs speaker already present\n");
917 return AVERROR_INVALIDDATA;
920 if ((ret = parse_frame_data(s, HEADER_XCH, s->nchannels)) < 0)
923 // Seek to the end of core frame, don't trust XCH frame size
924 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
925 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XCH frame\n");
926 return AVERROR_INVALIDDATA;
932 static int parse_xxch_frame(DCACoreDecoder *s)
934 int xxch_nchsets, xxch_frame_size;
935 int ret, mask, header_size, header_pos = get_bits_count(&s->gb);
938 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XXCH) {
939 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH sync word\n");
940 return AVERROR_INVALIDDATA;
943 // XXCH frame header length
944 header_size = get_bits(&s->gb, 6) + 1;
946 // Check XXCH frame header CRC
947 if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) {
948 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH frame header checksum\n");
949 return AVERROR_INVALIDDATA;
952 // CRC presence flag for channel set header
953 s->xxch_crc_present = get_bits1(&s->gb);
955 // Number of bits for loudspeaker mask
956 s->xxch_mask_nbits = get_bits(&s->gb, 5) + 1;
957 if (s->xxch_mask_nbits <= DCA_SPEAKER_Cs) {
958 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XXCH speaker mask (%d)\n", s->xxch_mask_nbits);
959 return AVERROR_INVALIDDATA;
962 // Number of channel sets
963 xxch_nchsets = get_bits(&s->gb, 2) + 1;
964 if (xxch_nchsets > 1) {
965 avpriv_request_sample(s->avctx, "%d XXCH channel sets", xxch_nchsets);
966 return AVERROR_PATCHWELCOME;
969 // Channel set 0 data byte size
970 xxch_frame_size = get_bits(&s->gb, 14) + 1;
972 // Core loudspeaker activity mask
973 s->xxch_core_mask = get_bits_long(&s->gb, s->xxch_mask_nbits);
975 // Validate the core mask
978 if ((mask & DCA_SPEAKER_MASK_Ls) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss))
979 mask = (mask & ~DCA_SPEAKER_MASK_Ls) | DCA_SPEAKER_MASK_Lss;
981 if ((mask & DCA_SPEAKER_MASK_Rs) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss))
982 mask = (mask & ~DCA_SPEAKER_MASK_Rs) | DCA_SPEAKER_MASK_Rss;
984 if (mask != s->xxch_core_mask) {
985 av_log(s->avctx, AV_LOG_ERROR, "XXCH core speaker activity mask (%#x) disagrees with core (%#x)\n", s->xxch_core_mask, mask);
986 return AVERROR_INVALIDDATA;
991 // CRC16 of XXCH frame header
992 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
993 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH frame header\n");
994 return AVERROR_INVALIDDATA;
997 // Parse XXCH channel set 0
998 if ((ret = parse_frame_data(s, HEADER_XXCH, s->nchannels)) < 0)
1001 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8 + xxch_frame_size * 8)) {
1002 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set\n");
1003 return AVERROR_INVALIDDATA;
1009 static int parse_xbr_subframe(DCACoreDecoder *s, int xbr_base_ch, int xbr_nchannels,
1010 int *xbr_nsubbands, int xbr_transition_mode, int sf, int *sub_pos)
1012 int xbr_nabits[DCA_CHANNELS];
1013 int xbr_bit_allocation[DCA_CHANNELS][DCA_SUBBANDS];
1014 int xbr_scale_nbits[DCA_CHANNELS];
1015 int32_t xbr_scale_factors[DCA_CHANNELS][DCA_SUBBANDS][2];
1016 int ssf, ch, band, ofs;
1018 // Check number of subband samples in this subframe
1019 if (*sub_pos + s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES > s->npcmblocks) {
1020 av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
1021 return AVERROR_INVALIDDATA;
1024 if (get_bits_left(&s->gb) < 0)
1025 return AVERROR_INVALIDDATA;
1027 // Number of bits for XBR bit allocation index
1028 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++)
1029 xbr_nabits[ch] = get_bits(&s->gb, 2) + 2;
1031 // XBR bit allocation index
1032 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1033 for (band = 0; band < xbr_nsubbands[ch]; band++) {
1034 xbr_bit_allocation[ch][band] = get_bits(&s->gb, xbr_nabits[ch]);
1035 if (xbr_bit_allocation[ch][band] > DCA_ABITS_MAX) {
1036 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR bit allocation index\n");
1037 return AVERROR_INVALIDDATA;
1042 // Number of bits for scale indices
1043 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1044 xbr_scale_nbits[ch] = get_bits(&s->gb, 3);
1045 if (!xbr_scale_nbits[ch]) {
1046 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XBR scale factor index\n");
1047 return AVERROR_INVALIDDATA;
1051 // XBR scale factors
1052 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1053 const uint32_t *scale_table;
1056 // Select the root square table
1057 if (s->scale_factor_sel[ch] > 5) {
1058 scale_table = ff_dca_scale_factor_quant7;
1059 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7);
1061 scale_table = ff_dca_scale_factor_quant6;
1062 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6);
1065 // Parse scale factor indices and look up scale factors from the root
1067 for (band = 0; band < xbr_nsubbands[ch]; band++) {
1068 if (xbr_bit_allocation[ch][band]) {
1069 int scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
1070 if (scale_index >= scale_size) {
1071 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
1072 return AVERROR_INVALIDDATA;
1074 xbr_scale_factors[ch][band][0] = scale_table[scale_index];
1075 if (xbr_transition_mode && s->transition_mode[sf][ch][band]) {
1076 scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
1077 if (scale_index >= scale_size) {
1078 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
1079 return AVERROR_INVALIDDATA;
1081 xbr_scale_factors[ch][band][1] = scale_table[scale_index];
1088 for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
1089 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1090 if (get_bits_left(&s->gb) < 0)
1091 return AVERROR_INVALIDDATA;
1093 for (band = 0; band < xbr_nsubbands[ch]; band++) {
1094 int ret, trans_ssf, abits = xbr_bit_allocation[ch][band];
1095 int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale;
1097 // Extract bits from the bit stream
1099 // No further encoding
1100 get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3);
1101 } else if (abits > 0) {
1103 if ((ret = parse_block_codes(s, audio, abits)) < 0)
1106 // No bits allocated
1110 // Look up quantization step size
1111 step_size = ff_dca_lossless_quant[abits];
1113 // Identify transient location
1114 if (xbr_transition_mode)
1115 trans_ssf = s->transition_mode[sf][ch][band];
1119 // Determine proper scale factor
1120 if (trans_ssf == 0 || ssf < trans_ssf)
1121 scale = xbr_scale_factors[ch][band][0];
1123 scale = xbr_scale_factors[ch][band][1];
1125 ff_dca_core_dequantize(s->subband_samples[ch][band] + ofs,
1126 audio, step_size, scale, 1, DCA_SUBBAND_SAMPLES);
1131 if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
1132 av_log(s->avctx, AV_LOG_ERROR, "XBR-DSYNC check failed\n");
1133 return AVERROR_INVALIDDATA;
1136 ofs += DCA_SUBBAND_SAMPLES;
1139 // Advance subband sample pointer for the next subframe
1144 static int parse_xbr_frame(DCACoreDecoder *s)
1146 int xbr_frame_size[DCA_EXSS_CHSETS_MAX];
1147 int xbr_nchannels[DCA_EXSS_CHSETS_MAX];
1148 int xbr_nsubbands[DCA_EXSS_CHSETS_MAX * DCA_EXSS_CHANNELS_MAX];
1149 int xbr_nchsets, xbr_transition_mode, xbr_band_nbits, xbr_base_ch;
1150 int i, ch1, ch2, ret, header_size, header_pos = get_bits_count(&s->gb);
1153 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XBR) {
1154 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR sync word\n");
1155 return AVERROR_INVALIDDATA;
1158 // XBR frame header length
1159 header_size = get_bits(&s->gb, 6) + 1;
1161 // Check XBR frame header CRC
1162 if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) {
1163 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR frame header checksum\n");
1164 return AVERROR_INVALIDDATA;
1167 // Number of channel sets
1168 xbr_nchsets = get_bits(&s->gb, 2) + 1;
1170 // Channel set data byte size
1171 for (i = 0; i < xbr_nchsets; i++)
1172 xbr_frame_size[i] = get_bits(&s->gb, 14) + 1;
1174 // Transition mode flag
1175 xbr_transition_mode = get_bits1(&s->gb);
1177 // Channel set headers
1178 for (i = 0, ch2 = 0; i < xbr_nchsets; i++) {
1179 xbr_nchannels[i] = get_bits(&s->gb, 3) + 1;
1180 xbr_band_nbits = get_bits(&s->gb, 2) + 5;
1181 for (ch1 = 0; ch1 < xbr_nchannels[i]; ch1++, ch2++) {
1182 xbr_nsubbands[ch2] = get_bits(&s->gb, xbr_band_nbits) + 1;
1183 if (xbr_nsubbands[ch2] > DCA_SUBBANDS) {
1184 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of active XBR subbands (%d)\n", xbr_nsubbands[ch2]);
1185 return AVERROR_INVALIDDATA;
1192 // CRC16 of XBR frame header
1193 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1194 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR frame header\n");
1195 return AVERROR_INVALIDDATA;
1199 for (i = 0, xbr_base_ch = 0; i < xbr_nchsets; i++) {
1200 header_pos = get_bits_count(&s->gb);
1202 if (xbr_base_ch + xbr_nchannels[i] <= s->nchannels) {
1205 for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) {
1206 if ((ret = parse_xbr_subframe(s, xbr_base_ch,
1207 xbr_base_ch + xbr_nchannels[i],
1208 xbr_nsubbands, xbr_transition_mode,
1214 xbr_base_ch += xbr_nchannels[i];
1216 if (ff_dca_seek_bits(&s->gb, header_pos + xbr_frame_size[i] * 8)) {
1217 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR channel set\n");
1218 return AVERROR_INVALIDDATA;
1225 // Modified ISO/IEC 9899 linear congruential generator
1226 // Returns pseudorandom integer in range [-2^30, 2^30 - 1]
1227 static int rand_x96(DCACoreDecoder *s)
1229 s->x96_rand = 1103515245U * s->x96_rand + 12345U;
1230 return (s->x96_rand & 0x7fffffff) - 0x40000000;
1233 static int parse_x96_subframe_audio(DCACoreDecoder *s, int sf, int xch_base, int *sub_pos)
1235 int n, ssf, ch, band, ofs;
1237 // Check number of subband samples in this subframe
1238 int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES;
1239 if (*sub_pos + nsamples > s->npcmblocks) {
1240 av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
1241 return AVERROR_INVALIDDATA;
1244 if (get_bits_left(&s->gb) < 0)
1245 return AVERROR_INVALIDDATA;
1247 // VQ encoded or unallocated subbands
1248 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1249 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1250 // Get the sample pointer and scale factor
1251 int32_t *samples = s->x96_subband_samples[ch][band] + *sub_pos;
1252 int32_t scale = s->scale_factors[ch][band >> 1][band & 1];
1254 switch (s->bit_allocation[ch][band]) {
1255 case 0: // No bits allocated for subband
1257 memset(samples, 0, nsamples * sizeof(int32_t));
1258 else for (n = 0; n < nsamples; n++)
1259 // Generate scaled random samples
1260 samples[n] = mul31(rand_x96(s), scale);
1263 case 1: // VQ encoded subband
1264 for (ssf = 0; ssf < (s->nsubsubframes[sf] + 1) / 2; ssf++) {
1265 // Extract the VQ address from the bit stream and look up
1266 // the VQ code book for up to 16 subband samples
1267 const int8_t *vq_samples = ff_dca_high_freq_vq[get_bits(&s->gb, 10)];
1268 // Scale and take the samples
1269 for (n = 0; n < FFMIN(nsamples - ssf * 16, 16); n++)
1270 *samples++ = clip23(vq_samples[n] * scale + (1 << 3) >> 4);
1278 for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
1279 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1280 if (get_bits_left(&s->gb) < 0)
1281 return AVERROR_INVALIDDATA;
1283 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1284 int ret, abits = s->bit_allocation[ch][band] - 1;
1285 int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale;
1287 // Not VQ encoded or unallocated subbands
1291 // Extract bits from the bit stream
1292 if ((ret = extract_audio(s, audio, abits, ch)) < 0)
1295 // Select quantization step size table and look up quantization
1297 if (s->bit_rate == 3)
1298 step_size = ff_dca_lossless_quant[abits];
1300 step_size = ff_dca_lossy_quant[abits];
1302 // Get the scale factor
1303 scale = s->scale_factors[ch][band >> 1][band & 1];
1305 ff_dca_core_dequantize(s->x96_subband_samples[ch][band] + ofs,
1306 audio, step_size, scale, 0, DCA_SUBBAND_SAMPLES);
1311 if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
1312 av_log(s->avctx, AV_LOG_ERROR, "X96-DSYNC check failed\n");
1313 return AVERROR_INVALIDDATA;
1316 ofs += DCA_SUBBAND_SAMPLES;
1320 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1321 inverse_adpcm(s->x96_subband_samples[ch], s->prediction_vq_index[ch],
1322 s->prediction_mode[ch], s->x96_subband_start, s->nsubbands[ch],
1323 *sub_pos, nsamples);
1326 // Joint subband coding
1327 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1328 int src_ch = s->joint_intensity_index[ch] - 1;
1330 s->dcadsp->decode_joint(s->x96_subband_samples[ch], s->x96_subband_samples[src_ch],
1331 s->joint_scale_factors[ch], s->nsubbands[ch],
1332 s->nsubbands[src_ch], *sub_pos, nsamples);
1336 // Advance subband sample pointer for the next subframe
1341 static void erase_x96_adpcm_history(DCACoreDecoder *s)
1345 // Erase ADPCM history from previous frame if
1346 // predictor history switch was disabled
1347 for (ch = 0; ch < DCA_CHANNELS; ch++)
1348 for (band = 0; band < DCA_SUBBANDS_X96; band++)
1349 AV_ZERO128(s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS);
1354 static int alloc_x96_sample_buffer(DCACoreDecoder *s)
1356 int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks;
1357 int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS_X96;
1358 unsigned int size = s->x96_subband_size;
1361 // Reallocate subband sample buffer
1362 av_fast_mallocz(&s->x96_subband_buffer, &s->x96_subband_size,
1363 nframesamples * sizeof(int32_t));
1364 if (!s->x96_subband_buffer)
1365 return AVERROR(ENOMEM);
1367 if (size != s->x96_subband_size) {
1368 for (ch = 0; ch < DCA_CHANNELS; ch++)
1369 for (band = 0; band < DCA_SUBBANDS_X96; band++)
1370 s->x96_subband_samples[ch][band] = s->x96_subband_buffer +
1371 (ch * DCA_SUBBANDS_X96 + band) * nchsamples + DCA_ADPCM_COEFFS;
1374 if (!s->predictor_history)
1375 erase_x96_adpcm_history(s);
1380 static int parse_x96_subframe_header(DCACoreDecoder *s, int xch_base)
1384 if (get_bits_left(&s->gb) < 0)
1385 return AVERROR_INVALIDDATA;
1388 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1389 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++)
1390 s->prediction_mode[ch][band] = get_bits1(&s->gb);
1392 // Prediction coefficients VQ address
1393 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1394 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++)
1395 if (s->prediction_mode[ch][band])
1396 s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12);
1398 // Bit allocation index
1399 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1400 int sel = s->bit_allocation_sel[ch];
1403 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1404 // If Huffman code was used, the difference of abits was encoded
1406 abits += dca_get_vlc(&s->gb, &ff_dca_vlc_quant_index[5 + 2 * s->x96_high_res], sel);
1408 abits = get_bits(&s->gb, 3 + s->x96_high_res);
1410 if (abits < 0 || abits > 7 + 8 * s->x96_high_res) {
1411 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 bit allocation index\n");
1412 return AVERROR_INVALIDDATA;
1415 s->bit_allocation[ch][band] = abits;
1420 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1421 int sel = s->scale_factor_sel[ch];
1422 int scale_index = 0;
1424 // Extract scales for subbands which are transmitted even for
1425 // unallocated subbands
1426 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1427 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
1429 s->scale_factors[ch][band >> 1][band & 1] = ret;
1433 // Joint subband codebook select
1434 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1435 if (s->joint_intensity_index[ch]) {
1436 s->joint_scale_sel[ch] = get_bits(&s->gb, 3);
1437 if (s->joint_scale_sel[ch] == 7) {
1438 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint scale factor code book\n");
1439 return AVERROR_INVALIDDATA;
1444 // Scale factors for joint subband coding
1445 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1446 int src_ch = s->joint_intensity_index[ch] - 1;
1448 int sel = s->joint_scale_sel[ch];
1449 for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) {
1450 if ((ret = parse_joint_scale(s, sel)) < 0)
1452 s->joint_scale_factors[ch][band] = ret;
1457 // Side information CRC check word
1459 skip_bits(&s->gb, 16);
1464 static int parse_x96_coding_header(DCACoreDecoder *s, int exss, int xch_base)
1466 int n, ch, header_size = 0, header_pos = get_bits_count(&s->gb);
1468 if (get_bits_left(&s->gb) < 0)
1469 return AVERROR_INVALIDDATA;
1472 // Channel set header length
1473 header_size = get_bits(&s->gb, 7) + 1;
1476 if (s->x96_crc_present
1477 && ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) {
1478 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 channel set header checksum\n");
1479 return AVERROR_INVALIDDATA;
1483 // High resolution flag
1484 s->x96_high_res = get_bits1(&s->gb);
1486 // First encoded subband
1487 if (s->x96_rev_no < 8) {
1488 s->x96_subband_start = get_bits(&s->gb, 5);
1489 if (s->x96_subband_start > 27) {
1490 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband start index (%d)\n", s->x96_subband_start);
1491 return AVERROR_INVALIDDATA;
1494 s->x96_subband_start = DCA_SUBBANDS;
1497 // Subband activity count
1498 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1499 s->nsubbands[ch] = get_bits(&s->gb, 6) + 1;
1500 if (s->nsubbands[ch] < DCA_SUBBANDS) {
1501 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband activity count (%d)\n", s->nsubbands[ch]);
1502 return AVERROR_INVALIDDATA;
1506 // Joint intensity coding index
1507 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1508 if ((n = get_bits(&s->gb, 3)) && xch_base)
1510 if (n > s->x96_nchannels) {
1511 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint intensity coding index\n");
1512 return AVERROR_INVALIDDATA;
1514 s->joint_intensity_index[ch] = n;
1517 // Scale factor code book
1518 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1519 s->scale_factor_sel[ch] = get_bits(&s->gb, 3);
1520 if (s->scale_factor_sel[ch] >= 6) {
1521 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 scale factor code book\n");
1522 return AVERROR_INVALIDDATA;
1526 // Bit allocation quantizer select
1527 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1528 s->bit_allocation_sel[ch] = get_bits(&s->gb, 3);
1530 // Quantization index codebook select
1531 for (n = 0; n < 6 + 4 * s->x96_high_res; n++)
1532 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1533 s->quant_index_sel[ch][n] = get_bits(&s->gb, ff_dca_quant_index_sel_nbits[n]);
1538 // CRC16 of channel set header
1539 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1540 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set header\n");
1541 return AVERROR_INVALIDDATA;
1545 skip_bits(&s->gb, 16);
1551 static int parse_x96_frame_data(DCACoreDecoder *s, int exss, int xch_base)
1553 int sf, ch, ret, band, sub_pos;
1555 if ((ret = parse_x96_coding_header(s, exss, xch_base)) < 0)
1558 for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) {
1559 if ((ret = parse_x96_subframe_header(s, xch_base)) < 0)
1561 if ((ret = parse_x96_subframe_audio(s, sf, xch_base, &sub_pos)) < 0)
1565 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1566 // Determine number of active subbands for this channel
1567 int nsubbands = s->nsubbands[ch];
1568 if (s->joint_intensity_index[ch])
1569 nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]);
1571 // Update history for ADPCM and clear inactive subbands
1572 for (band = 0; band < DCA_SUBBANDS_X96; band++) {
1573 int32_t *samples = s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS;
1574 if (band >= s->x96_subband_start && band < nsubbands)
1575 AV_COPY128(samples, samples + s->npcmblocks);
1577 memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t));
1586 static int parse_x96_frame(DCACoreDecoder *s)
1591 s->x96_rev_no = get_bits(&s->gb, 4);
1592 if (s->x96_rev_no < 1 || s->x96_rev_no > 8) {
1593 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no);
1594 return AVERROR_INVALIDDATA;
1597 s->x96_crc_present = 0;
1598 s->x96_nchannels = s->nchannels;
1600 if ((ret = alloc_x96_sample_buffer(s)) < 0)
1603 if ((ret = parse_x96_frame_data(s, 0, 0)) < 0)
1606 // Seek to the end of core frame
1607 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1608 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame\n");
1609 return AVERROR_INVALIDDATA;
1615 static int parse_x96_frame_exss(DCACoreDecoder *s)
1617 int x96_frame_size[DCA_EXSS_CHSETS_MAX];
1618 int x96_nchannels[DCA_EXSS_CHSETS_MAX];
1619 int x96_nchsets, x96_base_ch;
1620 int i, ret, header_size, header_pos = get_bits_count(&s->gb);
1623 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_X96) {
1624 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 sync word\n");
1625 return AVERROR_INVALIDDATA;
1628 // X96 frame header length
1629 header_size = get_bits(&s->gb, 6) + 1;
1631 // Check X96 frame header CRC
1632 if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) {
1633 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 frame header checksum\n");
1634 return AVERROR_INVALIDDATA;
1638 s->x96_rev_no = get_bits(&s->gb, 4);
1639 if (s->x96_rev_no < 1 || s->x96_rev_no > 8) {
1640 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no);
1641 return AVERROR_INVALIDDATA;
1644 // CRC presence flag for channel set header
1645 s->x96_crc_present = get_bits1(&s->gb);
1647 // Number of channel sets
1648 x96_nchsets = get_bits(&s->gb, 2) + 1;
1650 // Channel set data byte size
1651 for (i = 0; i < x96_nchsets; i++)
1652 x96_frame_size[i] = get_bits(&s->gb, 12) + 1;
1654 // Number of channels in channel set
1655 for (i = 0; i < x96_nchsets; i++)
1656 x96_nchannels[i] = get_bits(&s->gb, 3) + 1;
1660 // CRC16 of X96 frame header
1661 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1662 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame header\n");
1663 return AVERROR_INVALIDDATA;
1666 if ((ret = alloc_x96_sample_buffer(s)) < 0)
1670 s->x96_nchannels = 0;
1671 for (i = 0, x96_base_ch = 0; i < x96_nchsets; i++) {
1672 header_pos = get_bits_count(&s->gb);
1674 if (x96_base_ch + x96_nchannels[i] <= s->nchannels) {
1675 s->x96_nchannels = x96_base_ch + x96_nchannels[i];
1676 if ((ret = parse_x96_frame_data(s, 1, x96_base_ch)) < 0)
1680 x96_base_ch += x96_nchannels[i];
1682 if (ff_dca_seek_bits(&s->gb, header_pos + x96_frame_size[i] * 8)) {
1683 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set\n");
1684 return AVERROR_INVALIDDATA;
1691 static int parse_aux_data(DCACoreDecoder *s)
1695 if (get_bits_left(&s->gb) < 0)
1696 return AVERROR_INVALIDDATA;
1698 // Auxiliary data byte count (can't be trusted)
1699 skip_bits(&s->gb, 6);
1702 skip_bits_long(&s->gb, -get_bits_count(&s->gb) & 31);
1704 // Auxiliary data sync word
1705 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_REV1AUX) {
1706 av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data sync word\n");
1707 return AVERROR_INVALIDDATA;
1710 aux_pos = get_bits_count(&s->gb);
1712 // Auxiliary decode time stamp flag
1713 if (get_bits1(&s->gb))
1714 skip_bits_long(&s->gb, 47);
1716 // Auxiliary dynamic downmix flag
1717 if (s->prim_dmix_embedded = get_bits1(&s->gb)) {
1720 // Auxiliary primary channel downmix type
1721 s->prim_dmix_type = get_bits(&s->gb, 3);
1722 if (s->prim_dmix_type >= DCA_DMIX_TYPE_COUNT) {
1723 av_log(s->avctx, AV_LOG_ERROR, "Invalid primary channel set downmix type\n");
1724 return AVERROR_INVALIDDATA;
1727 // Size of downmix coefficients matrix
1728 m = ff_dca_dmix_primary_nch[s->prim_dmix_type];
1729 n = ff_dca_channels[s->audio_mode] + !!s->lfe_present;
1731 // Dynamic downmix code coefficients
1732 for (i = 0; i < m * n; i++) {
1733 int code = get_bits(&s->gb, 9);
1734 int sign = (code >> 8) - 1;
1735 unsigned int index = code & 0xff;
1736 if (index >= FF_DCA_DMIXTABLE_SIZE) {
1737 av_log(s->avctx, AV_LOG_ERROR, "Invalid downmix coefficient index\n");
1738 return AVERROR_INVALIDDATA;
1740 s->prim_dmix_coeff[i] = (ff_dca_dmixtable[index] ^ sign) - sign;
1745 skip_bits(&s->gb, -get_bits_count(&s->gb) & 7);
1747 // CRC16 of auxiliary data
1748 skip_bits(&s->gb, 16);
1751 if (ff_dca_check_crc(s->avctx, &s->gb, aux_pos, get_bits_count(&s->gb))) {
1752 av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data checksum\n");
1753 return AVERROR_INVALIDDATA;
1759 static int parse_optional_info(DCACoreDecoder *s)
1761 DCAContext *dca = s->avctx->priv_data;
1766 skip_bits_long(&s->gb, 32);
1769 if (s->aux_present && (ret = parse_aux_data(s)) < 0
1770 && (s->avctx->err_recognition & AV_EF_EXPLODE))
1774 s->prim_dmix_embedded = 0;
1777 if (s->ext_audio_present && !dca->core_only) {
1778 int sync_pos = FFMIN(s->frame_size / 4, s->gb.size_in_bits / 32) - 1;
1779 int last_pos = get_bits_count(&s->gb) / 32;
1782 // Search for extension sync words aligned on 4-byte boundary. Search
1783 // must be done backwards from the end of core frame to work around
1784 // sync word aliasing issues.
1785 switch (s->ext_audio_type) {
1787 if (dca->request_channel_layout)
1790 // The distance between XCH sync word and end of the core frame
1791 // must be equal to XCH frame size. Off by one error is allowed for
1792 // compatibility with legacy bitstreams. Minimum XCH frame size is
1793 // 96 bytes. AMODE and PCHS are further checked to reduce
1794 // probability of alias sync detection.
1795 for (; sync_pos >= last_pos; sync_pos--) {
1796 if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_XCH) {
1797 s->gb.index = (sync_pos + 1) * 32;
1798 size = get_bits(&s->gb, 10) + 1;
1799 dist = s->frame_size - sync_pos * 4;
1801 && (size == dist || size - 1 == dist)
1802 && get_bits(&s->gb, 7) == 0x08) {
1803 s->xch_pos = get_bits_count(&s->gb);
1810 av_log(s->avctx, AV_LOG_ERROR, "XCH sync word not found\n");
1811 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1812 return AVERROR_INVALIDDATA;
1817 // The distance between X96 sync word and end of the core frame
1818 // must be equal to X96 frame size. Minimum X96 frame size is 96
1820 for (; sync_pos >= last_pos; sync_pos--) {
1821 if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_X96) {
1822 s->gb.index = (sync_pos + 1) * 32;
1823 size = get_bits(&s->gb, 12) + 1;
1824 dist = s->frame_size - sync_pos * 4;
1825 if (size >= 96 && size == dist) {
1826 s->x96_pos = get_bits_count(&s->gb);
1833 av_log(s->avctx, AV_LOG_ERROR, "X96 sync word not found\n");
1834 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1835 return AVERROR_INVALIDDATA;
1839 case EXT_AUDIO_XXCH:
1840 if (dca->request_channel_layout)
1843 // XXCH frame header CRC must be valid. Minimum XXCH frame header
1844 // size is 11 bytes.
1845 for (; sync_pos >= last_pos; sync_pos--) {
1846 if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_XXCH) {
1847 s->gb.index = (sync_pos + 1) * 32;
1848 size = get_bits(&s->gb, 6) + 1;
1849 dist = s->gb.size_in_bits / 8 - sync_pos * 4;
1850 if (size >= 11 && size <= dist &&
1851 !av_crc(dca->crctab, 0xffff, s->gb.buffer +
1852 (sync_pos + 1) * 4, size - 4)) {
1853 s->xxch_pos = sync_pos * 32;
1860 av_log(s->avctx, AV_LOG_ERROR, "XXCH sync word not found\n");
1861 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1862 return AVERROR_INVALIDDATA;
1871 int ff_dca_core_parse(DCACoreDecoder *s, uint8_t *data, int size)
1875 s->ext_audio_mask = 0;
1876 s->xch_pos = s->xxch_pos = s->x96_pos = 0;
1878 if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
1881 skip_bits_long(&s->gb, 32);
1882 if ((ret = parse_frame_header(s)) < 0)
1884 if ((ret = alloc_sample_buffer(s)) < 0)
1886 if ((ret = parse_frame_data(s, HEADER_CORE, 0)) < 0)
1888 if ((ret = parse_optional_info(s)) < 0)
1891 // Workaround for DTS in WAV
1892 if (s->frame_size > size && s->frame_size < size + 4)
1893 s->frame_size = size;
1895 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1896 av_log(s->avctx, AV_LOG_ERROR, "Read past end of core frame\n");
1897 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1898 return AVERROR_INVALIDDATA;
1904 int ff_dca_core_parse_exss(DCACoreDecoder *s, uint8_t *data, DCAExssAsset *asset)
1906 AVCodecContext *avctx = s->avctx;
1907 DCAContext *dca = avctx->priv_data;
1908 GetBitContext gb = s->gb;
1909 int exss_mask = asset ? asset->extension_mask : 0;
1910 int ret = 0, ext = 0;
1912 // Parse (X)XCH unless downmixing
1913 if (!dca->request_channel_layout) {
1914 if (exss_mask & DCA_EXSS_XXCH) {
1915 if ((ret = init_get_bits8(&s->gb, data + asset->xxch_offset, asset->xxch_size)) < 0)
1917 ret = parse_xxch_frame(s);
1918 ext = DCA_EXSS_XXCH;
1919 } else if (s->xxch_pos) {
1920 s->gb.index = s->xxch_pos;
1921 ret = parse_xxch_frame(s);
1923 } else if (s->xch_pos) {
1924 s->gb.index = s->xch_pos;
1925 ret = parse_xch_frame(s);
1929 // Revert to primary channel set in case (X)XCH parsing fails
1931 if (avctx->err_recognition & AV_EF_EXPLODE)
1933 s->nchannels = ff_dca_channels[s->audio_mode];
1934 s->ch_mask = audio_mode_ch_mask[s->audio_mode];
1936 s->ch_mask |= DCA_SPEAKER_MASK_LFE1;
1938 s->ext_audio_mask |= ext;
1943 if (exss_mask & DCA_EXSS_XBR) {
1944 if ((ret = init_get_bits8(&s->gb, data + asset->xbr_offset, asset->xbr_size)) < 0)
1946 if ((ret = parse_xbr_frame(s)) < 0) {
1947 if (avctx->err_recognition & AV_EF_EXPLODE)
1950 s->ext_audio_mask |= DCA_EXSS_XBR;
1954 // Parse X96 unless decoding XLL
1955 if (!(dca->packet & DCA_PACKET_XLL)) {
1956 if (exss_mask & DCA_EXSS_X96) {
1957 if ((ret = init_get_bits8(&s->gb, data + asset->x96_offset, asset->x96_size)) < 0)
1959 if ((ret = parse_x96_frame_exss(s)) < 0) {
1960 if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
1963 s->ext_audio_mask |= DCA_EXSS_X96;
1965 } else if (s->x96_pos) {
1967 s->gb.index = s->x96_pos;
1968 if ((ret = parse_x96_frame(s)) < 0) {
1969 if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
1972 s->ext_audio_mask |= DCA_CSS_X96;
1980 static int map_prm_ch_to_spkr(DCACoreDecoder *s, int ch)
1984 // Try to map this channel to core first
1985 pos = ff_dca_channels[s->audio_mode];
1987 spkr = prm_ch_to_spkr_map[s->audio_mode][ch];
1988 if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) {
1989 if (s->xxch_core_mask & (1U << spkr))
1991 if (spkr == DCA_SPEAKER_Ls && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss))
1992 return DCA_SPEAKER_Lss;
1993 if (spkr == DCA_SPEAKER_Rs && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss))
1994 return DCA_SPEAKER_Rss;
2001 if ((s->ext_audio_mask & DCA_CSS_XCH) && ch == pos)
2002 return DCA_SPEAKER_Cs;
2005 if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) {
2006 for (spkr = DCA_SPEAKER_Cs; spkr < s->xxch_mask_nbits; spkr++)
2007 if (s->xxch_spkr_mask & (1U << spkr))
2016 static void erase_dsp_history(DCACoreDecoder *s)
2018 memset(s->dcadsp_data, 0, sizeof(s->dcadsp_data));
2019 s->output_history_lfe_fixed = 0;
2020 s->output_history_lfe_float = 0;
2023 static void set_filter_mode(DCACoreDecoder *s, int mode)
2025 if (s->filter_mode != mode) {
2026 erase_dsp_history(s);
2027 s->filter_mode = mode;
2031 int ff_dca_core_filter_fixed(DCACoreDecoder *s, int x96_synth)
2033 int n, ch, spkr, nsamples, x96_nchannels = 0;
2034 const int32_t *filter_coeff;
2037 // Externally set x96_synth flag implies that X96 synthesis should be
2038 // enabled, yet actual X96 subband data should be discarded. This is a
2039 // special case for lossless residual decoder that ignores X96 data if
2041 if (!x96_synth && (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96))) {
2042 x96_nchannels = s->x96_nchannels;
2048 s->output_rate = s->sample_rate << x96_synth;
2049 s->npcmsamples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth;
2051 // Reallocate PCM output buffer
2052 av_fast_malloc(&s->output_buffer, &s->output_size,
2053 nsamples * av_popcount(s->ch_mask) * sizeof(int32_t));
2054 if (!s->output_buffer)
2055 return AVERROR(ENOMEM);
2057 ptr = (int32_t *)s->output_buffer;
2058 for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) {
2059 if (s->ch_mask & (1U << spkr)) {
2060 s->output_samples[spkr] = ptr;
2063 s->output_samples[spkr] = NULL;
2067 // Handle change of filtering mode
2068 set_filter_mode(s, x96_synth | DCA_FILTER_MODE_FIXED);
2072 filter_coeff = ff_dca_fir_64bands_fixed;
2073 else if (s->filter_perfect)
2074 filter_coeff = ff_dca_fir_32bands_perfect_fixed;
2076 filter_coeff = ff_dca_fir_32bands_nonperfect_fixed;
2078 // Filter primary channels
2079 for (ch = 0; ch < s->nchannels; ch++) {
2080 // Map this primary channel to speaker
2081 spkr = map_prm_ch_to_spkr(s, ch);
2083 return AVERROR(EINVAL);
2085 // Filter bank reconstruction
2086 s->dcadsp->sub_qmf_fixed[x96_synth](
2089 s->output_samples[spkr],
2090 s->subband_samples[ch],
2091 ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL,
2092 s->dcadsp_data[ch].u.fix.hist1,
2093 &s->dcadsp_data[ch].offset,
2094 s->dcadsp_data[ch].u.fix.hist2,
2099 // Filter LFE channel
2100 if (s->lfe_present) {
2101 int32_t *samples = s->output_samples[DCA_SPEAKER_LFE1];
2102 int nlfesamples = s->npcmblocks >> 1;
2105 if (s->lfe_present == LFE_FLAG_128) {
2106 av_log(s->avctx, AV_LOG_ERROR, "Fixed point mode doesn't support LFF=1\n");
2107 return AVERROR(EINVAL);
2110 // Offset intermediate buffer for X96
2112 samples += nsamples / 2;
2114 // Interpolate LFE channel
2115 s->dcadsp->lfe_fir_fixed(samples, s->lfe_samples + DCA_LFE_HISTORY,
2116 ff_dca_lfe_fir_64_fixed, s->npcmblocks);
2119 // Filter 96 kHz oversampled LFE PCM to attenuate high frequency
2120 // (47.6 - 48.0 kHz) components of interpolation image
2121 s->dcadsp->lfe_x96_fixed(s->output_samples[DCA_SPEAKER_LFE1],
2122 samples, &s->output_history_lfe_fixed,
2127 // Update LFE history
2128 for (n = DCA_LFE_HISTORY - 1; n >= 0; n--)
2129 s->lfe_samples[n] = s->lfe_samples[nlfesamples + n];
2135 static int filter_frame_fixed(DCACoreDecoder *s, AVFrame *frame)
2137 AVCodecContext *avctx = s->avctx;
2138 DCAContext *dca = avctx->priv_data;
2139 int i, n, ch, ret, spkr, nsamples;
2141 // Don't filter twice when falling back from XLL
2142 if (!(dca->packet & DCA_PACKET_XLL) && (ret = ff_dca_core_filter_fixed(s, 0)) < 0)
2145 avctx->sample_rate = s->output_rate;
2146 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
2147 avctx->bits_per_raw_sample = 24;
2149 frame->nb_samples = nsamples = s->npcmsamples;
2150 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2153 // Undo embedded XCH downmix
2154 if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
2155 && s->audio_mode >= AMODE_2F2R) {
2156 s->dcadsp->dmix_sub_xch(s->output_samples[DCA_SPEAKER_Ls],
2157 s->output_samples[DCA_SPEAKER_Rs],
2158 s->output_samples[DCA_SPEAKER_Cs],
2163 // Undo embedded XXCH downmix
2164 if ((s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH))
2165 && s->xxch_dmix_embedded) {
2166 int scale_inv = s->xxch_dmix_scale_inv;
2167 int *coeff_ptr = s->xxch_dmix_coeff;
2168 int xch_base = ff_dca_channels[s->audio_mode];
2169 av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX);
2171 // Undo embedded core downmix pre-scaling
2172 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2173 if (s->xxch_core_mask & (1U << spkr)) {
2174 s->dcadsp->dmix_scale_inv(s->output_samples[spkr],
2175 scale_inv, nsamples);
2180 for (ch = xch_base; ch < s->nchannels; ch++) {
2181 int src_spkr = map_prm_ch_to_spkr(s, ch);
2183 return AVERROR(EINVAL);
2184 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2185 if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) {
2186 int coeff = mul16(*coeff_ptr++, scale_inv);
2188 s->dcadsp->dmix_sub(s->output_samples[spkr ],
2189 s->output_samples[src_spkr],
2197 if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) {
2198 // Front sum/difference decoding
2199 if ((s->sumdiff_front && s->audio_mode > AMODE_MONO)
2200 || s->audio_mode == AMODE_STEREO_SUMDIFF) {
2201 s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_L],
2202 s->output_samples[DCA_SPEAKER_R],
2206 // Surround sum/difference decoding
2207 if (s->sumdiff_surround && s->audio_mode >= AMODE_2F2R) {
2208 s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_Ls],
2209 s->output_samples[DCA_SPEAKER_Rs],
2214 // Downmix primary channel set to stereo
2215 if (s->request_mask != s->ch_mask) {
2216 ff_dca_downmix_to_stereo_fixed(s->dcadsp,
2219 nsamples, s->ch_mask);
2222 for (i = 0; i < avctx->channels; i++) {
2223 int32_t *samples = s->output_samples[s->ch_remap[i]];
2224 int32_t *plane = (int32_t *)frame->extended_data[i];
2225 for (n = 0; n < nsamples; n++)
2226 plane[n] = clip23(samples[n]) * (1 << 8);
2232 static int filter_frame_float(DCACoreDecoder *s, AVFrame *frame)
2234 AVCodecContext *avctx = s->avctx;
2235 int x96_nchannels = 0, x96_synth = 0;
2236 int i, n, ch, ret, spkr, nsamples, nchannels;
2237 float *output_samples[DCA_SPEAKER_COUNT] = { NULL }, *ptr;
2238 const float *filter_coeff;
2240 if (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96)) {
2241 x96_nchannels = s->x96_nchannels;
2245 avctx->sample_rate = s->sample_rate << x96_synth;
2246 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2247 avctx->bits_per_raw_sample = 0;
2249 frame->nb_samples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth;
2250 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2253 // Build reverse speaker to channel mapping
2254 for (i = 0; i < avctx->channels; i++)
2255 output_samples[s->ch_remap[i]] = (float *)frame->extended_data[i];
2257 // Allocate space for extra channels
2258 nchannels = av_popcount(s->ch_mask) - avctx->channels;
2259 if (nchannels > 0) {
2260 av_fast_malloc(&s->output_buffer, &s->output_size,
2261 nsamples * nchannels * sizeof(float));
2262 if (!s->output_buffer)
2263 return AVERROR(ENOMEM);
2265 ptr = (float *)s->output_buffer;
2266 for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) {
2267 if (!(s->ch_mask & (1U << spkr)))
2269 if (output_samples[spkr])
2271 output_samples[spkr] = ptr;
2276 // Handle change of filtering mode
2277 set_filter_mode(s, x96_synth);
2281 filter_coeff = ff_dca_fir_64bands;
2282 else if (s->filter_perfect)
2283 filter_coeff = ff_dca_fir_32bands_perfect;
2285 filter_coeff = ff_dca_fir_32bands_nonperfect;
2287 // Filter primary channels
2288 for (ch = 0; ch < s->nchannels; ch++) {
2289 // Map this primary channel to speaker
2290 spkr = map_prm_ch_to_spkr(s, ch);
2292 return AVERROR(EINVAL);
2294 // Filter bank reconstruction
2295 s->dcadsp->sub_qmf_float[x96_synth](
2297 &s->imdct[x96_synth],
2298 output_samples[spkr],
2299 s->subband_samples[ch],
2300 ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL,
2301 s->dcadsp_data[ch].u.flt.hist1,
2302 &s->dcadsp_data[ch].offset,
2303 s->dcadsp_data[ch].u.flt.hist2,
2306 1.0f / (1 << (17 - x96_synth)));
2309 // Filter LFE channel
2310 if (s->lfe_present) {
2311 int dec_select = (s->lfe_present == LFE_FLAG_128);
2312 float *samples = output_samples[DCA_SPEAKER_LFE1];
2313 int nlfesamples = s->npcmblocks >> (dec_select + 1);
2315 // Offset intermediate buffer for X96
2317 samples += nsamples / 2;
2321 filter_coeff = ff_dca_lfe_fir_128;
2323 filter_coeff = ff_dca_lfe_fir_64;
2325 // Interpolate LFE channel
2326 s->dcadsp->lfe_fir_float[dec_select](
2327 samples, s->lfe_samples + DCA_LFE_HISTORY,
2328 filter_coeff, s->npcmblocks);
2331 // Filter 96 kHz oversampled LFE PCM to attenuate high frequency
2332 // (47.6 - 48.0 kHz) components of interpolation image
2333 s->dcadsp->lfe_x96_float(output_samples[DCA_SPEAKER_LFE1],
2334 samples, &s->output_history_lfe_float,
2338 // Update LFE history
2339 for (n = DCA_LFE_HISTORY - 1; n >= 0; n--)
2340 s->lfe_samples[n] = s->lfe_samples[nlfesamples + n];
2343 // Undo embedded XCH downmix
2344 if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
2345 && s->audio_mode >= AMODE_2F2R) {
2346 s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Ls],
2347 output_samples[DCA_SPEAKER_Cs],
2348 -M_SQRT1_2, nsamples);
2349 s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Rs],
2350 output_samples[DCA_SPEAKER_Cs],
2351 -M_SQRT1_2, nsamples);
2354 // Undo embedded XXCH downmix
2355 if ((s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH))
2356 && s->xxch_dmix_embedded) {
2357 float scale_inv = s->xxch_dmix_scale_inv * (1.0f / (1 << 16));
2358 int *coeff_ptr = s->xxch_dmix_coeff;
2359 int xch_base = ff_dca_channels[s->audio_mode];
2360 av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX);
2363 for (ch = xch_base; ch < s->nchannels; ch++) {
2364 int src_spkr = map_prm_ch_to_spkr(s, ch);
2366 return AVERROR(EINVAL);
2367 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2368 if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) {
2369 int coeff = *coeff_ptr++;
2371 s->float_dsp->vector_fmac_scalar(output_samples[ spkr],
2372 output_samples[src_spkr],
2373 coeff * (-1.0f / (1 << 15)),
2380 // Undo embedded core downmix pre-scaling
2381 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2382 if (s->xxch_core_mask & (1U << spkr)) {
2383 s->float_dsp->vector_fmul_scalar(output_samples[spkr],
2384 output_samples[spkr],
2385 scale_inv, nsamples);
2390 if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) {
2391 // Front sum/difference decoding
2392 if ((s->sumdiff_front && s->audio_mode > AMODE_MONO)
2393 || s->audio_mode == AMODE_STEREO_SUMDIFF) {
2394 s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_L],
2395 output_samples[DCA_SPEAKER_R],
2399 // Surround sum/difference decoding
2400 if (s->sumdiff_surround && s->audio_mode >= AMODE_2F2R) {
2401 s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_Ls],
2402 output_samples[DCA_SPEAKER_Rs],
2407 // Downmix primary channel set to stereo
2408 if (s->request_mask != s->ch_mask) {
2409 ff_dca_downmix_to_stereo_float(s->float_dsp, output_samples,
2411 nsamples, s->ch_mask);
2417 int ff_dca_core_filter_frame(DCACoreDecoder *s, AVFrame *frame)
2419 AVCodecContext *avctx = s->avctx;
2420 DCAContext *dca = avctx->priv_data;
2421 DCAExssAsset *asset = &dca->exss.assets[0];
2422 enum AVMatrixEncoding matrix_encoding;
2425 // Handle downmixing to stereo request
2426 if (dca->request_channel_layout == DCA_SPEAKER_LAYOUT_STEREO
2427 && s->audio_mode > AMODE_MONO && s->prim_dmix_embedded
2428 && (s->prim_dmix_type == DCA_DMIX_TYPE_LoRo ||
2429 s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
2430 s->request_mask = DCA_SPEAKER_LAYOUT_STEREO;
2432 s->request_mask = s->ch_mask;
2433 if (!ff_dca_set_channel_layout(avctx, s->ch_remap, s->request_mask))
2434 return AVERROR(EINVAL);
2436 // Force fixed point mode when falling back from XLL
2437 if ((avctx->flags & AV_CODEC_FLAG_BITEXACT) || ((dca->packet & DCA_PACKET_EXSS)
2438 && (asset->extension_mask & DCA_EXSS_XLL)))
2439 ret = filter_frame_fixed(s, frame);
2441 ret = filter_frame_float(s, frame);
2445 // Set profile, bit rate, etc
2446 if (s->ext_audio_mask & DCA_EXSS_MASK)
2447 avctx->profile = FF_PROFILE_DTS_HD_HRA;
2448 else if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH))
2449 avctx->profile = FF_PROFILE_DTS_ES;
2450 else if (s->ext_audio_mask & DCA_CSS_X96)
2451 avctx->profile = FF_PROFILE_DTS_96_24;
2453 avctx->profile = FF_PROFILE_DTS;
2455 if (s->bit_rate > 3 && !(s->ext_audio_mask & DCA_EXSS_MASK))
2456 avctx->bit_rate = s->bit_rate;
2458 avctx->bit_rate = 0;
2460 if (s->audio_mode == AMODE_STEREO_TOTAL || (s->request_mask != s->ch_mask &&
2461 s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
2462 matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
2464 matrix_encoding = AV_MATRIX_ENCODING_NONE;
2465 if ((ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
2471 av_cold void ff_dca_core_flush(DCACoreDecoder *s)
2473 if (s->subband_buffer) {
2474 erase_adpcm_history(s);
2475 memset(s->lfe_samples, 0, DCA_LFE_HISTORY * sizeof(int32_t));
2478 if (s->x96_subband_buffer)
2479 erase_x96_adpcm_history(s);
2481 erase_dsp_history(s);
2484 av_cold int ff_dca_core_init(DCACoreDecoder *s)
2486 if (!(s->float_dsp = avpriv_float_dsp_alloc(0)))
2488 if (!(s->fixed_dsp = avpriv_alloc_fixed_dsp(0)))
2491 ff_dcadct_init(&s->dcadct);
2492 if (ff_mdct_init(&s->imdct[0], 6, 1, 1.0) < 0)
2494 if (ff_mdct_init(&s->imdct[1], 7, 1, 1.0) < 0)
2496 ff_synth_filter_init(&s->synth);
2502 av_cold void ff_dca_core_close(DCACoreDecoder *s)
2504 av_freep(&s->float_dsp);
2505 av_freep(&s->fixed_dsp);
2507 ff_mdct_end(&s->imdct[0]);
2508 ff_mdct_end(&s->imdct[1]);
2510 av_freep(&s->subband_buffer);
2511 s->subband_size = 0;
2513 av_freep(&s->x96_subband_buffer);
2514 s->x96_subband_size = 0;
2516 av_freep(&s->output_buffer);