2 * Copyright (C) 2016 foo86
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "dca_syncwords.h"
38 AMODE_MONO, // Mode 0: A (mono)
39 AMODE_MONO_DUAL, // Mode 1: A + B (dual mono)
40 AMODE_STEREO, // Mode 2: L + R (stereo)
41 AMODE_STEREO_SUMDIFF, // Mode 3: (L+R) + (L-R) (sum-diff)
42 AMODE_STEREO_TOTAL, // Mode 4: LT + RT (left and right total)
43 AMODE_3F, // Mode 5: C + L + R
44 AMODE_2F1R, // Mode 6: L + R + S
45 AMODE_3F1R, // Mode 7: C + L + R + S
46 AMODE_2F2R, // Mode 8: L + R + SL + SR
47 AMODE_3F2R, // Mode 9: C + L + R + SL + SR
65 static const int8_t prm_ch_to_spkr_map[AMODE_COUNT][5] = {
66 { DCA_SPEAKER_C, -1, -1, -1, -1 },
67 { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
68 { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
69 { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
70 { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
71 { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R , -1, -1 },
72 { DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Cs, -1, -1 },
73 { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R , DCA_SPEAKER_Cs, -1 },
74 { DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Ls, DCA_SPEAKER_Rs, -1 },
75 { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Ls, DCA_SPEAKER_Rs }
78 static const uint8_t audio_mode_ch_mask[AMODE_COUNT] = {
79 DCA_SPEAKER_LAYOUT_MONO,
80 DCA_SPEAKER_LAYOUT_STEREO,
81 DCA_SPEAKER_LAYOUT_STEREO,
82 DCA_SPEAKER_LAYOUT_STEREO,
83 DCA_SPEAKER_LAYOUT_STEREO,
84 DCA_SPEAKER_LAYOUT_3_0,
85 DCA_SPEAKER_LAYOUT_2_1,
86 DCA_SPEAKER_LAYOUT_3_1,
87 DCA_SPEAKER_LAYOUT_2_2,
88 DCA_SPEAKER_LAYOUT_5POINT0
91 static const uint8_t block_code_nbits[7] = {
92 7, 10, 12, 13, 15, 17, 19
95 static int dca_get_vlc(GetBitContext *s, DCAVLC *v, int i)
97 return get_vlc2(s, v->vlc[i].table, v->vlc[i].bits, v->max_depth) + v->offset;
100 static void get_array(GetBitContext *s, int32_t *array, int size, int n)
104 for (i = 0; i < size; i++)
105 array[i] = get_sbits(s, n);
108 // 5.3.1 - Bit stream header
109 static int parse_frame_header(DCACoreDecoder *s)
111 int normal_frame, pcmr_index;
114 normal_frame = get_bits1(&s->gb);
116 // Deficit sample count
117 if (get_bits(&s->gb, 5) != DCA_PCMBLOCK_SAMPLES - 1) {
118 av_log(s->avctx, AV_LOG_ERROR, "Deficit samples are not supported\n");
119 return normal_frame ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME;
123 s->crc_present = get_bits1(&s->gb);
125 // Number of PCM sample blocks
126 s->npcmblocks = get_bits(&s->gb, 7) + 1;
127 if (s->npcmblocks & (DCA_SUBBAND_SAMPLES - 1)) {
128 av_log(s->avctx, AV_LOG_ERROR, "Unsupported number of PCM sample blocks (%d)\n", s->npcmblocks);
129 return (s->npcmblocks < 6 || normal_frame) ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME;
132 // Primary frame byte size
133 s->frame_size = get_bits(&s->gb, 14) + 1;
134 if (s->frame_size < 96) {
135 av_log(s->avctx, AV_LOG_ERROR, "Invalid core frame size (%d bytes)\n", s->frame_size);
136 return AVERROR_INVALIDDATA;
139 // Audio channel arrangement
140 s->audio_mode = get_bits(&s->gb, 6);
141 if (s->audio_mode >= AMODE_COUNT) {
142 av_log(s->avctx, AV_LOG_ERROR, "Unsupported audio channel arrangement (%d)\n", s->audio_mode);
143 return AVERROR_PATCHWELCOME;
146 // Core audio sampling frequency
147 s->sample_rate = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
148 if (!s->sample_rate) {
149 av_log(s->avctx, AV_LOG_ERROR, "Invalid core audio sampling frequency\n");
150 return AVERROR_INVALIDDATA;
153 // Transmission bit rate
154 s->bit_rate = ff_dca_bit_rates[get_bits(&s->gb, 5)];
159 // Embedded dynamic range flag
160 s->drc_present = get_bits1(&s->gb);
162 // Embedded time stamp flag
163 s->ts_present = get_bits1(&s->gb);
165 // Auxiliary data flag
166 s->aux_present = get_bits1(&s->gb);
168 // HDCD mastering flag
171 // Extension audio descriptor flag
172 s->ext_audio_type = get_bits(&s->gb, 3);
174 // Extended coding flag
175 s->ext_audio_present = get_bits1(&s->gb);
177 // Audio sync word insertion flag
178 s->sync_ssf = get_bits1(&s->gb);
180 // Low frequency effects flag
181 s->lfe_present = get_bits(&s->gb, 2);
182 if (s->lfe_present == LFE_FLAG_INVALID) {
183 av_log(s->avctx, AV_LOG_ERROR, "Invalid low frequency effects flag\n");
184 return AVERROR_INVALIDDATA;
187 // Predictor history flag switch
188 s->predictor_history = get_bits1(&s->gb);
190 // Header CRC check bytes
192 skip_bits(&s->gb, 16);
194 // Multirate interpolator switch
195 s->filter_perfect = get_bits1(&s->gb);
197 // Encoder software revision
198 skip_bits(&s->gb, 4);
201 skip_bits(&s->gb, 2);
203 // Source PCM resolution
204 s->source_pcm_res = ff_dca_bits_per_sample[pcmr_index = get_bits(&s->gb, 3)];
205 if (!s->source_pcm_res) {
206 av_log(s->avctx, AV_LOG_ERROR, "Invalid source PCM resolution\n");
207 return AVERROR_INVALIDDATA;
209 s->es_format = pcmr_index & 1;
211 // Front sum/difference flag
212 s->sumdiff_front = get_bits1(&s->gb);
214 // Surround sum/difference flag
215 s->sumdiff_surround = get_bits1(&s->gb);
217 // Dialog normalization / unspecified
218 skip_bits(&s->gb, 4);
223 // 5.3.2 - Primary audio coding header
224 static int parse_coding_header(DCACoreDecoder *s, enum HeaderType header, int xch_base)
226 int n, ch, nchannels, header_size = 0, header_pos = get_bits_count(&s->gb);
227 unsigned int mask, index;
229 if (get_bits_left(&s->gb) < 0)
230 return AVERROR_INVALIDDATA;
234 // Number of subframes
235 s->nsubframes = get_bits(&s->gb, 4) + 1;
237 // Number of primary audio channels
238 s->nchannels = get_bits(&s->gb, 3) + 1;
239 if (s->nchannels != ff_dca_channels[s->audio_mode]) {
240 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);
241 return AVERROR_INVALIDDATA;
243 av_assert1(s->nchannels <= DCA_CHANNELS - 2);
245 s->ch_mask = audio_mode_ch_mask[s->audio_mode];
247 // Add LFE channel if present
249 s->ch_mask |= DCA_SPEAKER_MASK_LFE1;
253 s->nchannels = ff_dca_channels[s->audio_mode] + 1;
254 av_assert1(s->nchannels <= DCA_CHANNELS - 1);
255 s->ch_mask |= DCA_SPEAKER_MASK_Cs;
259 // Channel set header length
260 header_size = get_bits(&s->gb, 7) + 1;
263 if (s->xxch_crc_present
264 && ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) {
265 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH channel set header checksum\n");
266 return AVERROR_INVALIDDATA;
269 // Number of channels in a channel set
270 nchannels = get_bits(&s->gb, 3) + 1;
271 if (nchannels > DCA_XXCH_CHANNELS_MAX) {
272 avpriv_request_sample(s->avctx, "%d XXCH channels", nchannels);
273 return AVERROR_PATCHWELCOME;
275 s->nchannels = ff_dca_channels[s->audio_mode] + nchannels;
276 av_assert1(s->nchannels <= DCA_CHANNELS);
278 // Loudspeaker layout mask
279 mask = get_bits_long(&s->gb, s->xxch_mask_nbits - DCA_SPEAKER_Cs);
280 s->xxch_spkr_mask = mask << DCA_SPEAKER_Cs;
282 if (av_popcount(s->xxch_spkr_mask) != nchannels) {
283 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH speaker layout mask (%#x)\n", s->xxch_spkr_mask);
284 return AVERROR_INVALIDDATA;
287 if (s->xxch_core_mask & s->xxch_spkr_mask) {
288 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);
289 return AVERROR_INVALIDDATA;
292 // Combine core and XXCH masks together
293 s->ch_mask = s->xxch_core_mask | s->xxch_spkr_mask;
295 // Downmix coefficients present in stream
296 if (get_bits1(&s->gb)) {
297 int *coeff_ptr = s->xxch_dmix_coeff;
299 // Downmix already performed by encoder
300 s->xxch_dmix_embedded = get_bits1(&s->gb);
302 // Downmix scale factor
303 index = get_bits(&s->gb, 6) * 4 - FF_DCA_DMIXTABLE_OFFSET - 3;
304 if (index >= FF_DCA_INV_DMIXTABLE_SIZE) {
305 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix scale index (%d)\n", index);
306 return AVERROR_INVALIDDATA;
308 s->xxch_dmix_scale_inv = ff_dca_inv_dmixtable[index];
310 // Downmix channel mapping mask
311 for (ch = 0; ch < nchannels; ch++) {
312 mask = get_bits_long(&s->gb, s->xxch_mask_nbits);
313 if ((mask & s->xxch_core_mask) != mask) {
314 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix channel mapping mask (%#x)\n", mask);
315 return AVERROR_INVALIDDATA;
317 s->xxch_dmix_mask[ch] = mask;
320 // Downmix coefficients
321 for (ch = 0; ch < nchannels; ch++) {
322 for (n = 0; n < s->xxch_mask_nbits; n++) {
323 if (s->xxch_dmix_mask[ch] & (1U << n)) {
324 int code = get_bits(&s->gb, 7);
325 int sign = (code >> 6) - 1;
327 index = code * 4 - 3;
328 if (index >= FF_DCA_DMIXTABLE_SIZE) {
329 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix coefficient index (%d)\n", index);
330 return AVERROR_INVALIDDATA;
332 *coeff_ptr++ = (ff_dca_dmixtable[index] ^ sign) - sign;
340 s->xxch_dmix_embedded = 0;
346 // Subband activity count
347 for (ch = xch_base; ch < s->nchannels; ch++) {
348 s->nsubbands[ch] = get_bits(&s->gb, 5) + 2;
349 if (s->nsubbands[ch] > DCA_SUBBANDS) {
350 av_log(s->avctx, AV_LOG_ERROR, "Invalid subband activity count\n");
351 return AVERROR_INVALIDDATA;
355 // High frequency VQ start subband
356 for (ch = xch_base; ch < s->nchannels; ch++)
357 s->subband_vq_start[ch] = get_bits(&s->gb, 5) + 1;
359 // Joint intensity coding index
360 for (ch = xch_base; ch < s->nchannels; ch++) {
361 if ((n = get_bits(&s->gb, 3)) && header == HEADER_XXCH)
363 if (n > s->nchannels) {
364 av_log(s->avctx, AV_LOG_ERROR, "Invalid joint intensity coding index\n");
365 return AVERROR_INVALIDDATA;
367 s->joint_intensity_index[ch] = n;
370 // Transient mode code book
371 for (ch = xch_base; ch < s->nchannels; ch++)
372 s->transition_mode_sel[ch] = get_bits(&s->gb, 2);
374 // Scale factor code book
375 for (ch = xch_base; ch < s->nchannels; ch++) {
376 s->scale_factor_sel[ch] = get_bits(&s->gb, 3);
377 if (s->scale_factor_sel[ch] == 7) {
378 av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor code book\n");
379 return AVERROR_INVALIDDATA;
383 // Bit allocation quantizer select
384 for (ch = xch_base; ch < s->nchannels; ch++) {
385 s->bit_allocation_sel[ch] = get_bits(&s->gb, 3);
386 if (s->bit_allocation_sel[ch] == 7) {
387 av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation quantizer select\n");
388 return AVERROR_INVALIDDATA;
392 // Quantization index codebook select
393 for (n = 0; n < DCA_CODE_BOOKS; n++)
394 for (ch = xch_base; ch < s->nchannels; ch++)
395 s->quant_index_sel[ch][n] = get_bits(&s->gb, ff_dca_quant_index_sel_nbits[n]);
397 // Scale factor adjustment index
398 for (n = 0; n < DCA_CODE_BOOKS; n++)
399 for (ch = xch_base; ch < s->nchannels; ch++)
400 if (s->quant_index_sel[ch][n] < ff_dca_quant_index_group_size[n])
401 s->scale_factor_adj[ch][n] = ff_dca_scale_factor_adj[get_bits(&s->gb, 2)];
403 if (header == HEADER_XXCH) {
406 // CRC16 of channel set header
407 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
408 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set header\n");
409 return AVERROR_INVALIDDATA;
412 // Audio header CRC check word
414 skip_bits(&s->gb, 16);
420 static inline int parse_scale(DCACoreDecoder *s, int *scale_index, int sel)
422 const uint32_t *scale_table;
423 unsigned int scale_size;
425 // Select the root square table
427 scale_table = ff_dca_scale_factor_quant7;
428 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7);
430 scale_table = ff_dca_scale_factor_quant6;
431 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6);
434 // If Huffman code was used, the difference of scales was encoded
436 *scale_index += dca_get_vlc(&s->gb, &ff_dca_vlc_scale_factor, sel);
438 *scale_index = get_bits(&s->gb, sel + 1);
440 // Look up scale factor from the root square table
441 if ((unsigned int)*scale_index >= scale_size) {
442 av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor index\n");
443 return AVERROR_INVALIDDATA;
446 return scale_table[*scale_index];
449 static inline int parse_joint_scale(DCACoreDecoder *s, int sel)
453 // Absolute value was encoded even when Huffman code was used
455 scale_index = dca_get_vlc(&s->gb, &ff_dca_vlc_scale_factor, sel);
457 scale_index = get_bits(&s->gb, sel + 1);
462 // Look up joint scale factor
463 if ((unsigned int)scale_index >= FF_ARRAY_ELEMS(ff_dca_joint_scale_factors)) {
464 av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor index\n");
465 return AVERROR_INVALIDDATA;
468 return ff_dca_joint_scale_factors[scale_index];
471 // 5.4.1 - Primary audio coding side information
472 static int parse_subframe_header(DCACoreDecoder *s, int sf,
473 enum HeaderType header, int xch_base)
477 if (get_bits_left(&s->gb) < 0)
478 return AVERROR_INVALIDDATA;
480 if (header == HEADER_CORE) {
482 s->nsubsubframes[sf] = get_bits(&s->gb, 2) + 1;
484 // Partial subsubframe sample count
485 skip_bits(&s->gb, 3);
489 for (ch = xch_base; ch < s->nchannels; ch++)
490 for (band = 0; band < s->nsubbands[ch]; band++)
491 s->prediction_mode[ch][band] = get_bits1(&s->gb);
493 // Prediction coefficients VQ address
494 for (ch = xch_base; ch < s->nchannels; ch++)
495 for (band = 0; band < s->nsubbands[ch]; band++)
496 if (s->prediction_mode[ch][band])
497 s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12);
499 // Bit allocation index
500 for (ch = xch_base; ch < s->nchannels; ch++) {
501 int sel = s->bit_allocation_sel[ch];
503 for (band = 0; band < s->subband_vq_start[ch]; band++) {
507 abits = dca_get_vlc(&s->gb, &ff_dca_vlc_bit_allocation, sel);
509 abits = get_bits(&s->gb, sel - 1);
511 if (abits > DCA_ABITS_MAX) {
512 av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation index\n");
513 return AVERROR_INVALIDDATA;
516 s->bit_allocation[ch][band] = abits;
521 for (ch = xch_base; ch < s->nchannels; ch++) {
522 // Clear transition mode for all subbands
523 memset(s->transition_mode[sf][ch], 0, sizeof(s->transition_mode[0][0]));
525 // Transient possible only if more than one subsubframe
526 if (s->nsubsubframes[sf] > 1) {
527 int sel = s->transition_mode_sel[ch];
528 for (band = 0; band < s->subband_vq_start[ch]; band++)
529 if (s->bit_allocation[ch][band])
530 s->transition_mode[sf][ch][band] = dca_get_vlc(&s->gb, &ff_dca_vlc_transition_mode, sel);
535 for (ch = xch_base; ch < s->nchannels; ch++) {
536 int sel = s->scale_factor_sel[ch];
539 // Extract scales for subbands up to VQ
540 for (band = 0; band < s->subband_vq_start[ch]; band++) {
541 if (s->bit_allocation[ch][band]) {
542 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
544 s->scale_factors[ch][band][0] = ret;
545 if (s->transition_mode[sf][ch][band]) {
546 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
548 s->scale_factors[ch][band][1] = ret;
551 s->scale_factors[ch][band][0] = 0;
555 // High frequency VQ subbands
556 for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++) {
557 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
559 s->scale_factors[ch][band][0] = ret;
563 // Joint subband codebook select
564 for (ch = xch_base; ch < s->nchannels; ch++) {
565 if (s->joint_intensity_index[ch]) {
566 s->joint_scale_sel[ch] = get_bits(&s->gb, 3);
567 if (s->joint_scale_sel[ch] == 7) {
568 av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor code book\n");
569 return AVERROR_INVALIDDATA;
574 // Scale factors for joint subband coding
575 for (ch = xch_base; ch < s->nchannels; ch++) {
576 int src_ch = s->joint_intensity_index[ch] - 1;
578 int sel = s->joint_scale_sel[ch];
579 for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) {
580 if ((ret = parse_joint_scale(s, sel)) < 0)
582 s->joint_scale_factors[ch][band] = ret;
587 // Dynamic range coefficient
588 if (s->drc_present && header == HEADER_CORE)
589 skip_bits(&s->gb, 8);
591 // Side information CRC check word
593 skip_bits(&s->gb, 16);
598 #ifndef decode_blockcodes
599 static inline int decode_blockcodes(int code1, int code2, int levels, int32_t *audio)
601 int offset = (levels - 1) / 2;
604 for (n = 0; n < DCA_SUBBAND_SAMPLES / 2; n++) {
605 div = FASTDIV(code1, levels);
606 audio[n] = code1 - div * levels - offset;
609 for (; n < DCA_SUBBAND_SAMPLES; n++) {
610 div = FASTDIV(code2, levels);
611 audio[n] = code2 - div * levels - offset;
615 return code1 | code2;
619 static inline int parse_block_codes(DCACoreDecoder *s, int32_t *audio, int abits)
621 // Extract block code indices from the bit stream
622 int code1 = get_bits(&s->gb, block_code_nbits[abits - 1]);
623 int code2 = get_bits(&s->gb, block_code_nbits[abits - 1]);
624 int levels = ff_dca_quant_levels[abits];
626 // Look up samples from the block code book
627 if (decode_blockcodes(code1, code2, levels, audio)) {
628 av_log(s->avctx, AV_LOG_ERROR, "Failed to decode block code(s)\n");
629 return AVERROR_INVALIDDATA;
635 static inline int parse_huffman_codes(DCACoreDecoder *s, int32_t *audio, int abits, int sel)
639 // Extract Huffman codes from the bit stream
640 for (i = 0; i < DCA_SUBBAND_SAMPLES; i++)
641 audio[i] = dca_get_vlc(&s->gb, &ff_dca_vlc_quant_index[abits - 1], sel);
646 static inline int extract_audio(DCACoreDecoder *s, int32_t *audio, int abits, int ch)
648 av_assert1(abits >= 0 && abits <= DCA_ABITS_MAX);
652 memset(audio, 0, DCA_SUBBAND_SAMPLES * sizeof(*audio));
656 if (abits <= DCA_CODE_BOOKS) {
657 int sel = s->quant_index_sel[ch][abits - 1];
658 if (sel < ff_dca_quant_index_group_size[abits - 1]) {
660 return parse_huffman_codes(s, audio, abits, sel);
664 return parse_block_codes(s, audio, abits);
668 // No further encoding
669 get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3);
673 static inline void dequantize(int32_t *output, const int32_t *input,
674 int32_t step_size, int32_t scale, int residual)
676 // Account for quantizer step size
677 int64_t step_scale = (int64_t)step_size * scale;
680 // Limit scale factor resolution to 22 bits
681 if (step_scale > (1 << 23)) {
682 shift = av_log2(step_scale >> 23) + 1;
683 step_scale >>= shift;
688 for (n = 0; n < DCA_SUBBAND_SAMPLES; n++)
689 output[n] += clip23(norm__(input[n] * step_scale, 22 - shift));
691 for (n = 0; n < DCA_SUBBAND_SAMPLES; n++)
692 output[n] = clip23(norm__(input[n] * step_scale, 22 - shift));
696 static inline void inverse_adpcm(int32_t **subband_samples,
697 const int16_t *vq_index,
698 const int8_t *prediction_mode,
699 int sb_start, int sb_end,
704 for (i = sb_start; i < sb_end; i++) {
705 if (prediction_mode[i]) {
706 const int16_t *coeff = ff_dca_adpcm_vb[vq_index[i]];
707 int32_t *ptr = subband_samples[i] + ofs;
708 for (j = 0; j < len; j++) {
710 for (k = 0; k < DCA_ADPCM_COEFFS; k++)
711 err += (int64_t)ptr[j - k - 1] * coeff[k];
712 ptr[j] = clip23(ptr[j] + clip23(norm13(err)));
718 // 5.5 - Primary audio data arrays
719 static int parse_subframe_audio(DCACoreDecoder *s, int sf, enum HeaderType header,
720 int xch_base, int *sub_pos, int *lfe_pos)
722 int32_t audio[16], scale;
723 int n, ssf, ofs, ch, band;
725 // Check number of subband samples in this subframe
726 int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES;
727 if (*sub_pos + nsamples > s->npcmblocks) {
728 av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
729 return AVERROR_INVALIDDATA;
732 if (get_bits_left(&s->gb) < 0)
733 return AVERROR_INVALIDDATA;
735 // VQ encoded subbands
736 for (ch = xch_base; ch < s->nchannels; ch++) {
737 int32_t vq_index[DCA_SUBBANDS];
739 for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++)
740 // Extract the VQ address from the bit stream
741 vq_index[band] = get_bits(&s->gb, 10);
743 if (s->subband_vq_start[ch] < s->nsubbands[ch]) {
744 s->dcadsp->decode_hf(s->subband_samples[ch], vq_index,
745 ff_dca_high_freq_vq, s->scale_factors[ch],
746 s->subband_vq_start[ch], s->nsubbands[ch],
751 // Low frequency effect data
752 if (s->lfe_present && header == HEADER_CORE) {
755 // Determine number of LFE samples in this subframe
756 int nlfesamples = 2 * s->lfe_present * s->nsubsubframes[sf];
757 av_assert1((unsigned int)nlfesamples <= FF_ARRAY_ELEMS(audio));
759 // Extract LFE samples from the bit stream
760 get_array(&s->gb, audio, nlfesamples, 8);
762 // Extract scale factor index from the bit stream
763 index = get_bits(&s->gb, 8);
764 if (index >= FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7)) {
765 av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE scale factor index\n");
766 return AVERROR_INVALIDDATA;
769 // Look up the 7-bit root square quantization table
770 scale = ff_dca_scale_factor_quant7[index];
772 // Account for quantizer step size which is 0.035
773 scale = mul23(4697620 /* 0.035 * (1 << 27) */, scale);
775 // Scale and take the LFE samples
776 for (n = 0, ofs = *lfe_pos; n < nlfesamples; n++, ofs++)
777 s->lfe_samples[ofs] = clip23(audio[n] * scale >> 4);
779 // Advance LFE sample pointer for the next subframe
784 for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
785 for (ch = xch_base; ch < s->nchannels; ch++) {
786 if (get_bits_left(&s->gb) < 0)
787 return AVERROR_INVALIDDATA;
789 // Not high frequency VQ subbands
790 for (band = 0; band < s->subband_vq_start[ch]; band++) {
791 int ret, trans_ssf, abits = s->bit_allocation[ch][band];
794 // Extract bits from the bit stream
795 if ((ret = extract_audio(s, audio, abits, ch)) < 0)
798 // Select quantization step size table and look up
799 // quantization step size
800 if (s->bit_rate == 3)
801 step_size = ff_dca_lossless_quant[abits];
803 step_size = ff_dca_lossy_quant[abits];
805 // Identify transient location
806 trans_ssf = s->transition_mode[sf][ch][band];
808 // Determine proper scale factor
809 if (trans_ssf == 0 || ssf < trans_ssf)
810 scale = s->scale_factors[ch][band][0];
812 scale = s->scale_factors[ch][band][1];
814 // Adjust scale factor when SEL indicates Huffman code
816 int64_t adj = s->scale_factor_adj[ch][abits - 1];
817 scale = clip23(adj * scale >> 22);
820 dequantize(s->subband_samples[ch][band] + ofs,
821 audio, step_size, scale, 0);
826 if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
827 av_log(s->avctx, AV_LOG_ERROR, "DSYNC check failed\n");
828 return AVERROR_INVALIDDATA;
831 ofs += DCA_SUBBAND_SAMPLES;
835 for (ch = xch_base; ch < s->nchannels; ch++) {
836 inverse_adpcm(s->subband_samples[ch], s->prediction_vq_index[ch],
837 s->prediction_mode[ch], 0, s->nsubbands[ch],
841 // Joint subband coding
842 for (ch = xch_base; ch < s->nchannels; ch++) {
843 int src_ch = s->joint_intensity_index[ch] - 1;
845 s->dcadsp->decode_joint(s->subband_samples[ch], s->subband_samples[src_ch],
846 s->joint_scale_factors[ch], s->nsubbands[ch],
847 s->nsubbands[src_ch], *sub_pos, nsamples);
851 // Advance subband sample pointer for the next subframe
856 static void erase_adpcm_history(DCACoreDecoder *s)
860 // Erase ADPCM history from previous frame if
861 // predictor history switch was disabled
862 for (ch = 0; ch < DCA_CHANNELS; ch++)
863 for (band = 0; band < DCA_SUBBANDS; band++)
864 AV_ZERO128(s->subband_samples[ch][band] - DCA_ADPCM_COEFFS);
869 static int alloc_sample_buffer(DCACoreDecoder *s)
871 int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks;
872 int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS;
873 int nlfesamples = DCA_LFE_HISTORY + s->npcmblocks / 2;
874 unsigned int size = s->subband_size;
877 // Reallocate subband sample buffer
878 av_fast_mallocz(&s->subband_buffer, &s->subband_size,
879 (nframesamples + nlfesamples) * sizeof(int32_t));
880 if (!s->subband_buffer)
881 return AVERROR(ENOMEM);
883 if (size != s->subband_size) {
884 for (ch = 0; ch < DCA_CHANNELS; ch++)
885 for (band = 0; band < DCA_SUBBANDS; band++)
886 s->subband_samples[ch][band] = s->subband_buffer +
887 (ch * DCA_SUBBANDS + band) * nchsamples + DCA_ADPCM_COEFFS;
888 s->lfe_samples = s->subband_buffer + nframesamples;
891 if (!s->predictor_history)
892 erase_adpcm_history(s);
897 static int parse_frame_data(DCACoreDecoder *s, enum HeaderType header, int xch_base)
899 int sf, ch, ret, band, sub_pos, lfe_pos;
901 if ((ret = parse_coding_header(s, header, xch_base)) < 0)
904 for (sf = 0, sub_pos = 0, lfe_pos = DCA_LFE_HISTORY; sf < s->nsubframes; sf++) {
905 if ((ret = parse_subframe_header(s, sf, header, xch_base)) < 0)
907 if ((ret = parse_subframe_audio(s, sf, header, xch_base, &sub_pos, &lfe_pos)) < 0)
911 for (ch = xch_base; ch < s->nchannels; ch++) {
912 // Determine number of active subbands for this channel
913 int nsubbands = s->nsubbands[ch];
914 if (s->joint_intensity_index[ch])
915 nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]);
917 // Update history for ADPCM
918 for (band = 0; band < nsubbands; band++) {
919 int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS;
920 AV_COPY128(samples, samples + s->npcmblocks);
923 // Clear inactive subbands
924 for (; band < DCA_SUBBANDS; band++) {
925 int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS;
926 memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t));
935 static int parse_xch_frame(DCACoreDecoder *s)
939 if (s->ch_mask & DCA_SPEAKER_MASK_Cs) {
940 av_log(s->avctx, AV_LOG_ERROR, "XCH with Cs speaker already present\n");
941 return AVERROR_INVALIDDATA;
944 if ((ret = parse_frame_data(s, HEADER_XCH, s->nchannels)) < 0)
947 // Seek to the end of core frame, don't trust XCH frame size
948 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
949 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XCH frame\n");
950 return AVERROR_INVALIDDATA;
956 static int parse_xxch_frame(DCACoreDecoder *s)
958 int xxch_nchsets, xxch_frame_size;
959 int ret, mask, header_size, header_pos = get_bits_count(&s->gb);
962 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XXCH) {
963 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH sync word\n");
964 return AVERROR_INVALIDDATA;
967 // XXCH frame header length
968 header_size = get_bits(&s->gb, 6) + 1;
970 // Check XXCH frame header CRC
971 if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) {
972 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH frame header checksum\n");
973 return AVERROR_INVALIDDATA;
976 // CRC presence flag for channel set header
977 s->xxch_crc_present = get_bits1(&s->gb);
979 // Number of bits for loudspeaker mask
980 s->xxch_mask_nbits = get_bits(&s->gb, 5) + 1;
981 if (s->xxch_mask_nbits <= DCA_SPEAKER_Cs) {
982 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XXCH speaker mask (%d)\n", s->xxch_mask_nbits);
983 return AVERROR_INVALIDDATA;
986 // Number of channel sets
987 xxch_nchsets = get_bits(&s->gb, 2) + 1;
988 if (xxch_nchsets > 1) {
989 avpriv_request_sample(s->avctx, "%d XXCH channel sets", xxch_nchsets);
990 return AVERROR_PATCHWELCOME;
993 // Channel set 0 data byte size
994 xxch_frame_size = get_bits(&s->gb, 14) + 1;
996 // Core loudspeaker activity mask
997 s->xxch_core_mask = get_bits_long(&s->gb, s->xxch_mask_nbits);
999 // Validate the core mask
1002 if ((mask & DCA_SPEAKER_MASK_Ls) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss))
1003 mask = (mask & ~DCA_SPEAKER_MASK_Ls) | DCA_SPEAKER_MASK_Lss;
1005 if ((mask & DCA_SPEAKER_MASK_Rs) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss))
1006 mask = (mask & ~DCA_SPEAKER_MASK_Rs) | DCA_SPEAKER_MASK_Rss;
1008 if (mask != s->xxch_core_mask) {
1009 av_log(s->avctx, AV_LOG_ERROR, "XXCH core speaker activity mask (%#x) disagrees with core (%#x)\n", s->xxch_core_mask, mask);
1010 return AVERROR_INVALIDDATA;
1015 // CRC16 of XXCH frame header
1016 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1017 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH frame header\n");
1018 return AVERROR_INVALIDDATA;
1021 // Parse XXCH channel set 0
1022 if ((ret = parse_frame_data(s, HEADER_XXCH, s->nchannels)) < 0)
1025 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8 + xxch_frame_size * 8)) {
1026 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set\n");
1027 return AVERROR_INVALIDDATA;
1033 static int parse_xbr_subframe(DCACoreDecoder *s, int xbr_base_ch, int xbr_nchannels,
1034 int *xbr_nsubbands, int xbr_transition_mode, int sf, int *sub_pos)
1036 int xbr_nabits[DCA_CHANNELS];
1037 int xbr_bit_allocation[DCA_CHANNELS][DCA_SUBBANDS];
1038 int xbr_scale_nbits[DCA_CHANNELS];
1039 int32_t xbr_scale_factors[DCA_CHANNELS][DCA_SUBBANDS][2];
1040 int ssf, ch, band, ofs;
1042 // Check number of subband samples in this subframe
1043 if (*sub_pos + s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES > s->npcmblocks) {
1044 av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
1045 return AVERROR_INVALIDDATA;
1048 if (get_bits_left(&s->gb) < 0)
1049 return AVERROR_INVALIDDATA;
1051 // Number of bits for XBR bit allocation index
1052 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++)
1053 xbr_nabits[ch] = get_bits(&s->gb, 2) + 2;
1055 // XBR bit allocation index
1056 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1057 for (band = 0; band < xbr_nsubbands[ch]; band++) {
1058 xbr_bit_allocation[ch][band] = get_bits(&s->gb, xbr_nabits[ch]);
1059 if (xbr_bit_allocation[ch][band] > DCA_ABITS_MAX) {
1060 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR bit allocation index\n");
1061 return AVERROR_INVALIDDATA;
1066 // Number of bits for scale indices
1067 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1068 xbr_scale_nbits[ch] = get_bits(&s->gb, 3);
1069 if (!xbr_scale_nbits[ch]) {
1070 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XBR scale factor index\n");
1071 return AVERROR_INVALIDDATA;
1075 // XBR scale factors
1076 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1077 const uint32_t *scale_table;
1080 // Select the root square table
1081 if (s->scale_factor_sel[ch] > 5) {
1082 scale_table = ff_dca_scale_factor_quant7;
1083 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7);
1085 scale_table = ff_dca_scale_factor_quant6;
1086 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6);
1089 // Parse scale factor indices and look up scale factors from the root
1091 for (band = 0; band < xbr_nsubbands[ch]; band++) {
1092 if (xbr_bit_allocation[ch][band]) {
1093 int scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
1094 if (scale_index >= scale_size) {
1095 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
1096 return AVERROR_INVALIDDATA;
1098 xbr_scale_factors[ch][band][0] = scale_table[scale_index];
1099 if (xbr_transition_mode && s->transition_mode[sf][ch][band]) {
1100 scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
1101 if (scale_index >= scale_size) {
1102 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
1103 return AVERROR_INVALIDDATA;
1105 xbr_scale_factors[ch][band][1] = scale_table[scale_index];
1112 for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
1113 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1114 if (get_bits_left(&s->gb) < 0)
1115 return AVERROR_INVALIDDATA;
1117 for (band = 0; band < xbr_nsubbands[ch]; band++) {
1118 int ret, trans_ssf, abits = xbr_bit_allocation[ch][band];
1119 int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale;
1121 // Extract bits from the bit stream
1123 // No further encoding
1124 get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3);
1125 } else if (abits > 0) {
1127 if ((ret = parse_block_codes(s, audio, abits)) < 0)
1130 // No bits allocated
1134 // Look up quantization step size
1135 step_size = ff_dca_lossless_quant[abits];
1137 // Identify transient location
1138 if (xbr_transition_mode)
1139 trans_ssf = s->transition_mode[sf][ch][band];
1143 // Determine proper scale factor
1144 if (trans_ssf == 0 || ssf < trans_ssf)
1145 scale = xbr_scale_factors[ch][band][0];
1147 scale = xbr_scale_factors[ch][band][1];
1149 dequantize(s->subband_samples[ch][band] + ofs,
1150 audio, step_size, scale, 1);
1155 if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
1156 av_log(s->avctx, AV_LOG_ERROR, "XBR-DSYNC check failed\n");
1157 return AVERROR_INVALIDDATA;
1160 ofs += DCA_SUBBAND_SAMPLES;
1163 // Advance subband sample pointer for the next subframe
1168 static int parse_xbr_frame(DCACoreDecoder *s)
1170 int xbr_frame_size[DCA_EXSS_CHSETS_MAX];
1171 int xbr_nchannels[DCA_EXSS_CHSETS_MAX];
1172 int xbr_nsubbands[DCA_EXSS_CHSETS_MAX * DCA_EXSS_CHANNELS_MAX];
1173 int xbr_nchsets, xbr_transition_mode, xbr_band_nbits, xbr_base_ch;
1174 int i, ch1, ch2, ret, header_size, header_pos = get_bits_count(&s->gb);
1177 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XBR) {
1178 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR sync word\n");
1179 return AVERROR_INVALIDDATA;
1182 // XBR frame header length
1183 header_size = get_bits(&s->gb, 6) + 1;
1185 // Check XBR frame header CRC
1186 if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) {
1187 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR frame header checksum\n");
1188 return AVERROR_INVALIDDATA;
1191 // Number of channel sets
1192 xbr_nchsets = get_bits(&s->gb, 2) + 1;
1194 // Channel set data byte size
1195 for (i = 0; i < xbr_nchsets; i++)
1196 xbr_frame_size[i] = get_bits(&s->gb, 14) + 1;
1198 // Transition mode flag
1199 xbr_transition_mode = get_bits1(&s->gb);
1201 // Channel set headers
1202 for (i = 0, ch2 = 0; i < xbr_nchsets; i++) {
1203 xbr_nchannels[i] = get_bits(&s->gb, 3) + 1;
1204 xbr_band_nbits = get_bits(&s->gb, 2) + 5;
1205 for (ch1 = 0; ch1 < xbr_nchannels[i]; ch1++, ch2++) {
1206 xbr_nsubbands[ch2] = get_bits(&s->gb, xbr_band_nbits) + 1;
1207 if (xbr_nsubbands[ch2] > DCA_SUBBANDS) {
1208 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of active XBR subbands (%d)\n", xbr_nsubbands[ch2]);
1209 return AVERROR_INVALIDDATA;
1216 // CRC16 of XBR frame header
1217 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1218 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR frame header\n");
1219 return AVERROR_INVALIDDATA;
1223 for (i = 0, xbr_base_ch = 0; i < xbr_nchsets; i++) {
1224 header_pos = get_bits_count(&s->gb);
1226 if (xbr_base_ch + xbr_nchannels[i] <= s->nchannels) {
1229 for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) {
1230 if ((ret = parse_xbr_subframe(s, xbr_base_ch,
1231 xbr_base_ch + xbr_nchannels[i],
1232 xbr_nsubbands, xbr_transition_mode,
1238 xbr_base_ch += xbr_nchannels[i];
1240 if (ff_dca_seek_bits(&s->gb, header_pos + xbr_frame_size[i] * 8)) {
1241 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR channel set\n");
1242 return AVERROR_INVALIDDATA;
1249 // Modified ISO/IEC 9899 linear congruential generator
1250 // Returns pseudorandom integer in range [-2^30, 2^30 - 1]
1251 static int rand_x96(DCACoreDecoder *s)
1253 s->x96_rand = 1103515245U * s->x96_rand + 12345U;
1254 return (s->x96_rand & 0x7fffffff) - 0x40000000;
1257 static int parse_x96_subframe_audio(DCACoreDecoder *s, int sf, int xch_base, int *sub_pos)
1259 int n, ssf, ch, band, ofs;
1261 // Check number of subband samples in this subframe
1262 int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES;
1263 if (*sub_pos + nsamples > s->npcmblocks) {
1264 av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
1265 return AVERROR_INVALIDDATA;
1268 if (get_bits_left(&s->gb) < 0)
1269 return AVERROR_INVALIDDATA;
1271 // VQ encoded or unallocated subbands
1272 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1273 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1274 // Get the sample pointer and scale factor
1275 int32_t *samples = s->x96_subband_samples[ch][band] + *sub_pos;
1276 int32_t scale = s->scale_factors[ch][band >> 1][band & 1];
1278 switch (s->bit_allocation[ch][band]) {
1279 case 0: // No bits allocated for subband
1281 memset(samples, 0, nsamples * sizeof(int32_t));
1282 else for (n = 0; n < nsamples; n++)
1283 // Generate scaled random samples
1284 samples[n] = mul31(rand_x96(s), scale);
1287 case 1: // VQ encoded subband
1288 for (ssf = 0; ssf < (s->nsubsubframes[sf] + 1) / 2; ssf++) {
1289 // Extract the VQ address from the bit stream and look up
1290 // the VQ code book for up to 16 subband samples
1291 const int8_t *vq_samples = ff_dca_high_freq_vq[get_bits(&s->gb, 10)];
1292 // Scale and take the samples
1293 for (n = 0; n < FFMIN(nsamples - ssf * 16, 16); n++)
1294 *samples++ = clip23(vq_samples[n] * scale + (1 << 3) >> 4);
1302 for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
1303 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1304 if (get_bits_left(&s->gb) < 0)
1305 return AVERROR_INVALIDDATA;
1307 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1308 int ret, abits = s->bit_allocation[ch][band] - 1;
1309 int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale;
1311 // Not VQ encoded or unallocated subbands
1315 // Extract bits from the bit stream
1316 if ((ret = extract_audio(s, audio, abits, ch)) < 0)
1319 // Select quantization step size table and look up quantization
1321 if (s->bit_rate == 3)
1322 step_size = ff_dca_lossless_quant[abits];
1324 step_size = ff_dca_lossy_quant[abits];
1326 // Get the scale factor
1327 scale = s->scale_factors[ch][band >> 1][band & 1];
1329 dequantize(s->x96_subband_samples[ch][band] + ofs,
1330 audio, step_size, scale, 0);
1335 if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
1336 av_log(s->avctx, AV_LOG_ERROR, "X96-DSYNC check failed\n");
1337 return AVERROR_INVALIDDATA;
1340 ofs += DCA_SUBBAND_SAMPLES;
1344 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1345 inverse_adpcm(s->x96_subband_samples[ch], s->prediction_vq_index[ch],
1346 s->prediction_mode[ch], s->x96_subband_start, s->nsubbands[ch],
1347 *sub_pos, nsamples);
1350 // Joint subband coding
1351 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1352 int src_ch = s->joint_intensity_index[ch] - 1;
1354 s->dcadsp->decode_joint(s->x96_subband_samples[ch], s->x96_subband_samples[src_ch],
1355 s->joint_scale_factors[ch], s->nsubbands[ch],
1356 s->nsubbands[src_ch], *sub_pos, nsamples);
1360 // Advance subband sample pointer for the next subframe
1365 static void erase_x96_adpcm_history(DCACoreDecoder *s)
1369 // Erase ADPCM history from previous frame if
1370 // predictor history switch was disabled
1371 for (ch = 0; ch < DCA_CHANNELS; ch++)
1372 for (band = 0; band < DCA_SUBBANDS_X96; band++)
1373 AV_ZERO128(s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS);
1378 static int alloc_x96_sample_buffer(DCACoreDecoder *s)
1380 int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks;
1381 int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS_X96;
1382 unsigned int size = s->x96_subband_size;
1385 // Reallocate subband sample buffer
1386 av_fast_mallocz(&s->x96_subband_buffer, &s->x96_subband_size,
1387 nframesamples * sizeof(int32_t));
1388 if (!s->x96_subband_buffer)
1389 return AVERROR(ENOMEM);
1391 if (size != s->x96_subband_size) {
1392 for (ch = 0; ch < DCA_CHANNELS; ch++)
1393 for (band = 0; band < DCA_SUBBANDS_X96; band++)
1394 s->x96_subband_samples[ch][band] = s->x96_subband_buffer +
1395 (ch * DCA_SUBBANDS_X96 + band) * nchsamples + DCA_ADPCM_COEFFS;
1398 if (!s->predictor_history)
1399 erase_x96_adpcm_history(s);
1404 static int parse_x96_subframe_header(DCACoreDecoder *s, int xch_base)
1408 if (get_bits_left(&s->gb) < 0)
1409 return AVERROR_INVALIDDATA;
1412 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1413 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++)
1414 s->prediction_mode[ch][band] = get_bits1(&s->gb);
1416 // Prediction coefficients VQ address
1417 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1418 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++)
1419 if (s->prediction_mode[ch][band])
1420 s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12);
1422 // Bit allocation index
1423 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1424 int sel = s->bit_allocation_sel[ch];
1427 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1428 // If Huffman code was used, the difference of abits was encoded
1430 abits += dca_get_vlc(&s->gb, &ff_dca_vlc_quant_index[5 + 2 * s->x96_high_res], sel);
1432 abits = get_bits(&s->gb, 3 + s->x96_high_res);
1434 if (abits < 0 || abits > 7 + 8 * s->x96_high_res) {
1435 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 bit allocation index\n");
1436 return AVERROR_INVALIDDATA;
1439 s->bit_allocation[ch][band] = abits;
1444 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1445 int sel = s->scale_factor_sel[ch];
1446 int scale_index = 0;
1448 // Extract scales for subbands which are transmitted even for
1449 // unallocated subbands
1450 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1451 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
1453 s->scale_factors[ch][band >> 1][band & 1] = ret;
1457 // Joint subband codebook select
1458 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1459 if (s->joint_intensity_index[ch]) {
1460 s->joint_scale_sel[ch] = get_bits(&s->gb, 3);
1461 if (s->joint_scale_sel[ch] == 7) {
1462 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint scale factor code book\n");
1463 return AVERROR_INVALIDDATA;
1468 // Scale factors for joint subband coding
1469 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1470 int src_ch = s->joint_intensity_index[ch] - 1;
1472 int sel = s->joint_scale_sel[ch];
1473 for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) {
1474 if ((ret = parse_joint_scale(s, sel)) < 0)
1476 s->joint_scale_factors[ch][band] = ret;
1481 // Side information CRC check word
1483 skip_bits(&s->gb, 16);
1488 static int parse_x96_coding_header(DCACoreDecoder *s, int exss, int xch_base)
1490 int n, ch, header_size = 0, header_pos = get_bits_count(&s->gb);
1492 if (get_bits_left(&s->gb) < 0)
1493 return AVERROR_INVALIDDATA;
1496 // Channel set header length
1497 header_size = get_bits(&s->gb, 7) + 1;
1500 if (s->x96_crc_present
1501 && ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) {
1502 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 channel set header checksum\n");
1503 return AVERROR_INVALIDDATA;
1507 // High resolution flag
1508 s->x96_high_res = get_bits1(&s->gb);
1510 // First encoded subband
1511 if (s->x96_rev_no < 8) {
1512 s->x96_subband_start = get_bits(&s->gb, 5);
1513 if (s->x96_subband_start > 27) {
1514 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband start index (%d)\n", s->x96_subband_start);
1515 return AVERROR_INVALIDDATA;
1518 s->x96_subband_start = DCA_SUBBANDS;
1521 // Subband activity count
1522 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1523 s->nsubbands[ch] = get_bits(&s->gb, 6) + 1;
1524 if (s->nsubbands[ch] < DCA_SUBBANDS) {
1525 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband activity count (%d)\n", s->nsubbands[ch]);
1526 return AVERROR_INVALIDDATA;
1530 // Joint intensity coding index
1531 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1532 if ((n = get_bits(&s->gb, 3)) && xch_base)
1534 if (n > s->x96_nchannels) {
1535 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint intensity coding index\n");
1536 return AVERROR_INVALIDDATA;
1538 s->joint_intensity_index[ch] = n;
1541 // Scale factor code book
1542 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1543 s->scale_factor_sel[ch] = get_bits(&s->gb, 3);
1544 if (s->scale_factor_sel[ch] >= 6) {
1545 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 scale factor code book\n");
1546 return AVERROR_INVALIDDATA;
1550 // Bit allocation quantizer select
1551 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1552 s->bit_allocation_sel[ch] = get_bits(&s->gb, 3);
1554 // Quantization index codebook select
1555 for (n = 0; n < 6 + 4 * s->x96_high_res; n++)
1556 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1557 s->quant_index_sel[ch][n] = get_bits(&s->gb, ff_dca_quant_index_sel_nbits[n]);
1562 // CRC16 of channel set header
1563 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1564 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set header\n");
1565 return AVERROR_INVALIDDATA;
1569 skip_bits(&s->gb, 16);
1575 static int parse_x96_frame_data(DCACoreDecoder *s, int exss, int xch_base)
1577 int sf, ch, ret, band, sub_pos;
1579 if ((ret = parse_x96_coding_header(s, exss, xch_base)) < 0)
1582 for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) {
1583 if ((ret = parse_x96_subframe_header(s, xch_base)) < 0)
1585 if ((ret = parse_x96_subframe_audio(s, sf, xch_base, &sub_pos)) < 0)
1589 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1590 // Determine number of active subbands for this channel
1591 int nsubbands = s->nsubbands[ch];
1592 if (s->joint_intensity_index[ch])
1593 nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]);
1595 // Update history for ADPCM and clear inactive subbands
1596 for (band = 0; band < DCA_SUBBANDS_X96; band++) {
1597 int32_t *samples = s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS;
1598 if (band >= s->x96_subband_start && band < nsubbands)
1599 AV_COPY128(samples, samples + s->npcmblocks);
1601 memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t));
1610 static int parse_x96_frame(DCACoreDecoder *s)
1615 s->x96_rev_no = get_bits(&s->gb, 4);
1616 if (s->x96_rev_no < 1 || s->x96_rev_no > 8) {
1617 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no);
1618 return AVERROR_INVALIDDATA;
1621 s->x96_crc_present = 0;
1622 s->x96_nchannels = s->nchannels;
1624 if ((ret = alloc_x96_sample_buffer(s)) < 0)
1627 if ((ret = parse_x96_frame_data(s, 0, 0)) < 0)
1630 // Seek to the end of core frame
1631 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1632 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame\n");
1633 return AVERROR_INVALIDDATA;
1639 static int parse_x96_frame_exss(DCACoreDecoder *s)
1641 int x96_frame_size[DCA_EXSS_CHSETS_MAX];
1642 int x96_nchannels[DCA_EXSS_CHSETS_MAX];
1643 int x96_nchsets, x96_base_ch;
1644 int i, ret, header_size, header_pos = get_bits_count(&s->gb);
1647 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_X96) {
1648 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 sync word\n");
1649 return AVERROR_INVALIDDATA;
1652 // X96 frame header length
1653 header_size = get_bits(&s->gb, 6) + 1;
1655 // Check X96 frame header CRC
1656 if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) {
1657 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 frame header checksum\n");
1658 return AVERROR_INVALIDDATA;
1662 s->x96_rev_no = get_bits(&s->gb, 4);
1663 if (s->x96_rev_no < 1 || s->x96_rev_no > 8) {
1664 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no);
1665 return AVERROR_INVALIDDATA;
1668 // CRC presence flag for channel set header
1669 s->x96_crc_present = get_bits1(&s->gb);
1671 // Number of channel sets
1672 x96_nchsets = get_bits(&s->gb, 2) + 1;
1674 // Channel set data byte size
1675 for (i = 0; i < x96_nchsets; i++)
1676 x96_frame_size[i] = get_bits(&s->gb, 12) + 1;
1678 // Number of channels in channel set
1679 for (i = 0; i < x96_nchsets; i++)
1680 x96_nchannels[i] = get_bits(&s->gb, 3) + 1;
1684 // CRC16 of X96 frame header
1685 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1686 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame header\n");
1687 return AVERROR_INVALIDDATA;
1690 if ((ret = alloc_x96_sample_buffer(s)) < 0)
1694 s->x96_nchannels = 0;
1695 for (i = 0, x96_base_ch = 0; i < x96_nchsets; i++) {
1696 header_pos = get_bits_count(&s->gb);
1698 if (x96_base_ch + x96_nchannels[i] <= s->nchannels) {
1699 s->x96_nchannels = x96_base_ch + x96_nchannels[i];
1700 if ((ret = parse_x96_frame_data(s, 1, x96_base_ch)) < 0)
1704 x96_base_ch += x96_nchannels[i];
1706 if (ff_dca_seek_bits(&s->gb, header_pos + x96_frame_size[i] * 8)) {
1707 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set\n");
1708 return AVERROR_INVALIDDATA;
1715 static int parse_aux_data(DCACoreDecoder *s)
1719 if (get_bits_left(&s->gb) < 0)
1720 return AVERROR_INVALIDDATA;
1722 // Auxiliary data byte count (can't be trusted)
1723 skip_bits(&s->gb, 6);
1726 skip_bits_long(&s->gb, -get_bits_count(&s->gb) & 31);
1728 // Auxiliary data sync word
1729 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_REV1AUX) {
1730 av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data sync word\n");
1731 return AVERROR_INVALIDDATA;
1734 aux_pos = get_bits_count(&s->gb);
1736 // Auxiliary decode time stamp flag
1737 if (get_bits1(&s->gb))
1738 skip_bits_long(&s->gb, 47);
1740 // Auxiliary dynamic downmix flag
1741 if (s->prim_dmix_embedded = get_bits1(&s->gb)) {
1744 // Auxiliary primary channel downmix type
1745 s->prim_dmix_type = get_bits(&s->gb, 3);
1746 if (s->prim_dmix_type >= DCA_DMIX_TYPE_COUNT) {
1747 av_log(s->avctx, AV_LOG_ERROR, "Invalid primary channel set downmix type\n");
1748 return AVERROR_INVALIDDATA;
1751 // Size of downmix coefficients matrix
1752 m = ff_dca_dmix_primary_nch[s->prim_dmix_type];
1753 n = ff_dca_channels[s->audio_mode] + !!s->lfe_present;
1755 // Dynamic downmix code coefficients
1756 for (i = 0; i < m * n; i++) {
1757 int code = get_bits(&s->gb, 9);
1758 int sign = (code >> 8) - 1;
1759 unsigned int index = code & 0xff;
1760 if (index >= FF_DCA_DMIXTABLE_SIZE) {
1761 av_log(s->avctx, AV_LOG_ERROR, "Invalid downmix coefficient index\n");
1762 return AVERROR_INVALIDDATA;
1764 s->prim_dmix_coeff[i] = (ff_dca_dmixtable[index] ^ sign) - sign;
1769 skip_bits(&s->gb, -get_bits_count(&s->gb) & 7);
1771 // CRC16 of auxiliary data
1772 skip_bits(&s->gb, 16);
1775 if (ff_dca_check_crc(s->avctx, &s->gb, aux_pos, get_bits_count(&s->gb))) {
1776 av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data checksum\n");
1777 return AVERROR_INVALIDDATA;
1783 static int parse_optional_info(DCACoreDecoder *s)
1785 DCAContext *dca = s->avctx->priv_data;
1790 skip_bits_long(&s->gb, 32);
1793 if (s->aux_present && (ret = parse_aux_data(s)) < 0
1794 && (s->avctx->err_recognition & AV_EF_EXPLODE))
1798 s->prim_dmix_embedded = 0;
1801 if (s->ext_audio_present && !dca->core_only) {
1802 int sync_pos = FFMIN(s->frame_size / 4, s->gb.size_in_bits / 32) - 1;
1803 int last_pos = get_bits_count(&s->gb) / 32;
1806 // Search for extension sync words aligned on 4-byte boundary. Search
1807 // must be done backwards from the end of core frame to work around
1808 // sync word aliasing issues.
1809 switch (s->ext_audio_type) {
1811 if (dca->request_channel_layout)
1814 // The distance between XCH sync word and end of the core frame
1815 // must be equal to XCH frame size. Off by one error is allowed for
1816 // compatibility with legacy bitstreams. Minimum XCH frame size is
1817 // 96 bytes. AMODE and PCHS are further checked to reduce
1818 // probability of alias sync detection.
1819 for (; sync_pos >= last_pos; sync_pos--) {
1820 if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_XCH) {
1821 s->gb.index = (sync_pos + 1) * 32;
1822 size = get_bits(&s->gb, 10) + 1;
1823 dist = s->frame_size - sync_pos * 4;
1825 && (size == dist || size - 1 == dist)
1826 && get_bits(&s->gb, 7) == 0x08) {
1827 s->xch_pos = get_bits_count(&s->gb);
1834 av_log(s->avctx, AV_LOG_ERROR, "XCH sync word not found\n");
1835 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1836 return AVERROR_INVALIDDATA;
1841 // The distance between X96 sync word and end of the core frame
1842 // must be equal to X96 frame size. Minimum X96 frame size is 96
1844 for (; sync_pos >= last_pos; sync_pos--) {
1845 if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_X96) {
1846 s->gb.index = (sync_pos + 1) * 32;
1847 size = get_bits(&s->gb, 12) + 1;
1848 dist = s->frame_size - sync_pos * 4;
1849 if (size >= 96 && size == dist) {
1850 s->x96_pos = get_bits_count(&s->gb);
1857 av_log(s->avctx, AV_LOG_ERROR, "X96 sync word not found\n");
1858 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1859 return AVERROR_INVALIDDATA;
1863 case EXT_AUDIO_XXCH:
1864 if (dca->request_channel_layout)
1867 // XXCH frame header CRC must be valid. Minimum XXCH frame header
1868 // size is 11 bytes.
1869 for (; sync_pos >= last_pos; sync_pos--) {
1870 if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_XXCH) {
1871 s->gb.index = (sync_pos + 1) * 32;
1872 size = get_bits(&s->gb, 6) + 1;
1873 dist = s->gb.size_in_bits / 8 - sync_pos * 4;
1874 if (size >= 11 && size <= dist &&
1875 !av_crc(dca->crctab, 0xffff, s->gb.buffer +
1876 (sync_pos + 1) * 4, size - 4)) {
1877 s->xxch_pos = sync_pos * 32;
1884 av_log(s->avctx, AV_LOG_ERROR, "XXCH sync word not found\n");
1885 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1886 return AVERROR_INVALIDDATA;
1895 int ff_dca_core_parse(DCACoreDecoder *s, uint8_t *data, int size)
1899 s->ext_audio_mask = 0;
1900 s->xch_pos = s->xxch_pos = s->x96_pos = 0;
1902 if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
1905 skip_bits_long(&s->gb, 32);
1906 if ((ret = parse_frame_header(s)) < 0)
1908 if ((ret = alloc_sample_buffer(s)) < 0)
1910 if ((ret = parse_frame_data(s, HEADER_CORE, 0)) < 0)
1912 if ((ret = parse_optional_info(s)) < 0)
1915 // Workaround for DTS in WAV
1916 if (s->frame_size > size && s->frame_size < size + 4)
1917 s->frame_size = size;
1919 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1920 av_log(s->avctx, AV_LOG_ERROR, "Read past end of core frame\n");
1921 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1922 return AVERROR_INVALIDDATA;
1928 int ff_dca_core_parse_exss(DCACoreDecoder *s, uint8_t *data, DCAExssAsset *asset)
1930 AVCodecContext *avctx = s->avctx;
1931 DCAContext *dca = avctx->priv_data;
1932 GetBitContext gb = s->gb;
1933 int exss_mask = asset ? asset->extension_mask : 0;
1934 int ret = 0, ext = 0;
1936 // Parse (X)XCH unless downmixing
1937 if (!dca->request_channel_layout) {
1938 if (exss_mask & DCA_EXSS_XXCH) {
1939 if ((ret = init_get_bits8(&s->gb, data + asset->xxch_offset, asset->xxch_size)) < 0)
1941 ret = parse_xxch_frame(s);
1942 ext = DCA_EXSS_XXCH;
1943 } else if (s->xxch_pos) {
1944 s->gb.index = s->xxch_pos;
1945 ret = parse_xxch_frame(s);
1947 } else if (s->xch_pos) {
1948 s->gb.index = s->xch_pos;
1949 ret = parse_xch_frame(s);
1953 // Revert to primary channel set in case (X)XCH parsing fails
1955 if (avctx->err_recognition & AV_EF_EXPLODE)
1957 s->nchannels = ff_dca_channels[s->audio_mode];
1958 s->ch_mask = audio_mode_ch_mask[s->audio_mode];
1960 s->ch_mask |= DCA_SPEAKER_MASK_LFE1;
1962 s->ext_audio_mask |= ext;
1967 if (exss_mask & DCA_EXSS_XBR) {
1968 if ((ret = init_get_bits8(&s->gb, data + asset->xbr_offset, asset->xbr_size)) < 0)
1970 if ((ret = parse_xbr_frame(s)) < 0) {
1971 if (avctx->err_recognition & AV_EF_EXPLODE)
1974 s->ext_audio_mask |= DCA_EXSS_XBR;
1978 // Parse X96 unless decoding XLL
1979 if (!(dca->packet & DCA_PACKET_XLL)) {
1980 if (exss_mask & DCA_EXSS_X96) {
1981 if ((ret = init_get_bits8(&s->gb, data + asset->x96_offset, asset->x96_size)) < 0)
1983 if ((ret = parse_x96_frame_exss(s)) < 0) {
1984 if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
1987 s->ext_audio_mask |= DCA_EXSS_X96;
1989 } else if (s->x96_pos) {
1991 s->gb.index = s->x96_pos;
1992 if ((ret = parse_x96_frame(s)) < 0) {
1993 if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
1996 s->ext_audio_mask |= DCA_CSS_X96;
2004 static int map_prm_ch_to_spkr(DCACoreDecoder *s, int ch)
2008 // Try to map this channel to core first
2009 pos = ff_dca_channels[s->audio_mode];
2011 spkr = prm_ch_to_spkr_map[s->audio_mode][ch];
2012 if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) {
2013 if (s->xxch_core_mask & (1U << spkr))
2015 if (spkr == DCA_SPEAKER_Ls && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss))
2016 return DCA_SPEAKER_Lss;
2017 if (spkr == DCA_SPEAKER_Rs && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss))
2018 return DCA_SPEAKER_Rss;
2025 if ((s->ext_audio_mask & DCA_CSS_XCH) && ch == pos)
2026 return DCA_SPEAKER_Cs;
2029 if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) {
2030 for (spkr = DCA_SPEAKER_Cs; spkr < s->xxch_mask_nbits; spkr++)
2031 if (s->xxch_spkr_mask & (1U << spkr))
2040 static void erase_dsp_history(DCACoreDecoder *s)
2042 memset(s->dcadsp_data, 0, sizeof(s->dcadsp_data));
2043 s->output_history_lfe_fixed = 0;
2044 s->output_history_lfe_float = 0;
2047 static void set_filter_mode(DCACoreDecoder *s, int mode)
2049 if (s->filter_mode != mode) {
2050 erase_dsp_history(s);
2051 s->filter_mode = mode;
2055 int ff_dca_core_filter_fixed(DCACoreDecoder *s, int x96_synth)
2057 int n, ch, spkr, nsamples, x96_nchannels = 0;
2058 const int32_t *filter_coeff;
2061 // Externally set x96_synth flag implies that X96 synthesis should be
2062 // enabled, yet actual X96 subband data should be discarded. This is a
2063 // special case for lossless residual decoder that ignores X96 data if
2065 if (!x96_synth && (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96))) {
2066 x96_nchannels = s->x96_nchannels;
2072 s->output_rate = s->sample_rate << x96_synth;
2073 s->npcmsamples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth;
2075 // Reallocate PCM output buffer
2076 av_fast_malloc(&s->output_buffer, &s->output_size,
2077 nsamples * av_popcount(s->ch_mask) * sizeof(int32_t));
2078 if (!s->output_buffer)
2079 return AVERROR(ENOMEM);
2081 ptr = (int32_t *)s->output_buffer;
2082 for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) {
2083 if (s->ch_mask & (1U << spkr)) {
2084 s->output_samples[spkr] = ptr;
2087 s->output_samples[spkr] = NULL;
2091 // Handle change of filtering mode
2092 set_filter_mode(s, x96_synth | DCA_FILTER_MODE_FIXED);
2096 filter_coeff = ff_dca_fir_64bands_fixed;
2097 else if (s->filter_perfect)
2098 filter_coeff = ff_dca_fir_32bands_perfect_fixed;
2100 filter_coeff = ff_dca_fir_32bands_nonperfect_fixed;
2102 // Filter primary channels
2103 for (ch = 0; ch < s->nchannels; ch++) {
2104 // Map this primary channel to speaker
2105 spkr = map_prm_ch_to_spkr(s, ch);
2107 return AVERROR(EINVAL);
2109 // Filter bank reconstruction
2110 s->dcadsp->sub_qmf_fixed[x96_synth](
2113 s->output_samples[spkr],
2114 s->subband_samples[ch],
2115 ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL,
2116 s->dcadsp_data[ch].u.fix.hist1,
2117 &s->dcadsp_data[ch].offset,
2118 s->dcadsp_data[ch].u.fix.hist2,
2123 // Filter LFE channel
2124 if (s->lfe_present) {
2125 int32_t *samples = s->output_samples[DCA_SPEAKER_LFE1];
2126 int nlfesamples = s->npcmblocks >> 1;
2129 if (s->lfe_present == LFE_FLAG_128) {
2130 av_log(s->avctx, AV_LOG_ERROR, "Fixed point mode doesn't support LFF=1\n");
2131 return AVERROR(EINVAL);
2134 // Offset intermediate buffer for X96
2136 samples += nsamples / 2;
2138 // Interpolate LFE channel
2139 s->dcadsp->lfe_fir_fixed(samples, s->lfe_samples + DCA_LFE_HISTORY,
2140 ff_dca_lfe_fir_64_fixed, s->npcmblocks);
2143 // Filter 96 kHz oversampled LFE PCM to attenuate high frequency
2144 // (47.6 - 48.0 kHz) components of interpolation image
2145 s->dcadsp->lfe_x96_fixed(s->output_samples[DCA_SPEAKER_LFE1],
2146 samples, &s->output_history_lfe_fixed,
2151 // Update LFE history
2152 for (n = DCA_LFE_HISTORY - 1; n >= 0; n--)
2153 s->lfe_samples[n] = s->lfe_samples[nlfesamples + n];
2159 static int filter_frame_fixed(DCACoreDecoder *s, AVFrame *frame)
2161 AVCodecContext *avctx = s->avctx;
2162 DCAContext *dca = avctx->priv_data;
2163 int i, n, ch, ret, spkr, nsamples;
2165 // Don't filter twice when falling back from XLL
2166 if (!(dca->packet & DCA_PACKET_XLL) && (ret = ff_dca_core_filter_fixed(s, 0)) < 0)
2169 avctx->sample_rate = s->output_rate;
2170 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
2171 avctx->bits_per_raw_sample = 24;
2173 frame->nb_samples = nsamples = s->npcmsamples;
2174 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2177 // Undo embedded XCH downmix
2178 if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
2179 && s->audio_mode >= AMODE_2F2R) {
2180 s->dcadsp->dmix_sub_xch(s->output_samples[DCA_SPEAKER_Ls],
2181 s->output_samples[DCA_SPEAKER_Rs],
2182 s->output_samples[DCA_SPEAKER_Cs],
2187 // Undo embedded XXCH downmix
2188 if ((s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH))
2189 && s->xxch_dmix_embedded) {
2190 int scale_inv = s->xxch_dmix_scale_inv;
2191 int *coeff_ptr = s->xxch_dmix_coeff;
2192 int xch_base = ff_dca_channels[s->audio_mode];
2193 av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX);
2195 // Undo embedded core downmix pre-scaling
2196 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2197 if (s->xxch_core_mask & (1U << spkr)) {
2198 s->dcadsp->dmix_scale_inv(s->output_samples[spkr],
2199 scale_inv, nsamples);
2204 for (ch = xch_base; ch < s->nchannels; ch++) {
2205 int src_spkr = map_prm_ch_to_spkr(s, ch);
2207 return AVERROR(EINVAL);
2208 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2209 if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) {
2210 int coeff = mul16(*coeff_ptr++, scale_inv);
2212 s->dcadsp->dmix_sub(s->output_samples[spkr ],
2213 s->output_samples[src_spkr],
2221 if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) {
2222 // Front sum/difference decoding
2223 if ((s->sumdiff_front && s->audio_mode > AMODE_MONO)
2224 || s->audio_mode == AMODE_STEREO_SUMDIFF) {
2225 s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_L],
2226 s->output_samples[DCA_SPEAKER_R],
2230 // Surround sum/difference decoding
2231 if (s->sumdiff_surround && s->audio_mode >= AMODE_2F2R) {
2232 s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_Ls],
2233 s->output_samples[DCA_SPEAKER_Rs],
2238 // Downmix primary channel set to stereo
2239 if (s->request_mask != s->ch_mask) {
2240 ff_dca_downmix_to_stereo_fixed(s->dcadsp,
2243 nsamples, s->ch_mask);
2246 for (i = 0; i < avctx->channels; i++) {
2247 int32_t *samples = s->output_samples[s->ch_remap[i]];
2248 int32_t *plane = (int32_t *)frame->extended_data[i];
2249 for (n = 0; n < nsamples; n++)
2250 plane[n] = clip23(samples[n]) * (1 << 8);
2256 static int filter_frame_float(DCACoreDecoder *s, AVFrame *frame)
2258 AVCodecContext *avctx = s->avctx;
2259 int x96_nchannels = 0, x96_synth = 0;
2260 int i, n, ch, ret, spkr, nsamples, nchannels;
2261 float *output_samples[DCA_SPEAKER_COUNT] = { NULL }, *ptr;
2262 const float *filter_coeff;
2264 if (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96)) {
2265 x96_nchannels = s->x96_nchannels;
2269 avctx->sample_rate = s->sample_rate << x96_synth;
2270 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2271 avctx->bits_per_raw_sample = 0;
2273 frame->nb_samples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth;
2274 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2277 // Build reverse speaker to channel mapping
2278 for (i = 0; i < avctx->channels; i++)
2279 output_samples[s->ch_remap[i]] = (float *)frame->extended_data[i];
2281 // Allocate space for extra channels
2282 nchannels = av_popcount(s->ch_mask) - avctx->channels;
2283 if (nchannels > 0) {
2284 av_fast_malloc(&s->output_buffer, &s->output_size,
2285 nsamples * nchannels * sizeof(float));
2286 if (!s->output_buffer)
2287 return AVERROR(ENOMEM);
2289 ptr = (float *)s->output_buffer;
2290 for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) {
2291 if (!(s->ch_mask & (1U << spkr)))
2293 if (output_samples[spkr])
2295 output_samples[spkr] = ptr;
2300 // Handle change of filtering mode
2301 set_filter_mode(s, x96_synth);
2305 filter_coeff = ff_dca_fir_64bands;
2306 else if (s->filter_perfect)
2307 filter_coeff = ff_dca_fir_32bands_perfect;
2309 filter_coeff = ff_dca_fir_32bands_nonperfect;
2311 // Filter primary channels
2312 for (ch = 0; ch < s->nchannels; ch++) {
2313 // Map this primary channel to speaker
2314 spkr = map_prm_ch_to_spkr(s, ch);
2316 return AVERROR(EINVAL);
2318 // Filter bank reconstruction
2319 s->dcadsp->sub_qmf_float[x96_synth](
2321 &s->imdct[x96_synth],
2322 output_samples[spkr],
2323 s->subband_samples[ch],
2324 ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL,
2325 s->dcadsp_data[ch].u.flt.hist1,
2326 &s->dcadsp_data[ch].offset,
2327 s->dcadsp_data[ch].u.flt.hist2,
2330 1.0f / (1 << (17 - x96_synth)));
2333 // Filter LFE channel
2334 if (s->lfe_present) {
2335 int dec_select = (s->lfe_present == LFE_FLAG_128);
2336 float *samples = output_samples[DCA_SPEAKER_LFE1];
2337 int nlfesamples = s->npcmblocks >> (dec_select + 1);
2339 // Offset intermediate buffer for X96
2341 samples += nsamples / 2;
2345 filter_coeff = ff_dca_lfe_fir_128;
2347 filter_coeff = ff_dca_lfe_fir_64;
2349 // Interpolate LFE channel
2350 s->dcadsp->lfe_fir_float[dec_select](
2351 samples, s->lfe_samples + DCA_LFE_HISTORY,
2352 filter_coeff, s->npcmblocks);
2355 // Filter 96 kHz oversampled LFE PCM to attenuate high frequency
2356 // (47.6 - 48.0 kHz) components of interpolation image
2357 s->dcadsp->lfe_x96_float(output_samples[DCA_SPEAKER_LFE1],
2358 samples, &s->output_history_lfe_float,
2362 // Update LFE history
2363 for (n = DCA_LFE_HISTORY - 1; n >= 0; n--)
2364 s->lfe_samples[n] = s->lfe_samples[nlfesamples + n];
2367 // Undo embedded XCH downmix
2368 if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
2369 && s->audio_mode >= AMODE_2F2R) {
2370 s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Ls],
2371 output_samples[DCA_SPEAKER_Cs],
2372 -M_SQRT1_2, nsamples);
2373 s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Rs],
2374 output_samples[DCA_SPEAKER_Cs],
2375 -M_SQRT1_2, nsamples);
2378 // Undo embedded XXCH downmix
2379 if ((s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH))
2380 && s->xxch_dmix_embedded) {
2381 float scale_inv = s->xxch_dmix_scale_inv * (1.0f / (1 << 16));
2382 int *coeff_ptr = s->xxch_dmix_coeff;
2383 int xch_base = ff_dca_channels[s->audio_mode];
2384 av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX);
2387 for (ch = xch_base; ch < s->nchannels; ch++) {
2388 int src_spkr = map_prm_ch_to_spkr(s, ch);
2390 return AVERROR(EINVAL);
2391 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2392 if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) {
2393 int coeff = *coeff_ptr++;
2395 s->float_dsp->vector_fmac_scalar(output_samples[ spkr],
2396 output_samples[src_spkr],
2397 coeff * (-1.0f / (1 << 15)),
2404 // Undo embedded core downmix pre-scaling
2405 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2406 if (s->xxch_core_mask & (1U << spkr)) {
2407 s->float_dsp->vector_fmul_scalar(output_samples[spkr],
2408 output_samples[spkr],
2409 scale_inv, nsamples);
2414 if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) {
2415 // Front sum/difference decoding
2416 if ((s->sumdiff_front && s->audio_mode > AMODE_MONO)
2417 || s->audio_mode == AMODE_STEREO_SUMDIFF) {
2418 s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_L],
2419 output_samples[DCA_SPEAKER_R],
2423 // Surround sum/difference decoding
2424 if (s->sumdiff_surround && s->audio_mode >= AMODE_2F2R) {
2425 s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_Ls],
2426 output_samples[DCA_SPEAKER_Rs],
2431 // Downmix primary channel set to stereo
2432 if (s->request_mask != s->ch_mask) {
2433 ff_dca_downmix_to_stereo_float(s->float_dsp, output_samples,
2435 nsamples, s->ch_mask);
2441 int ff_dca_core_filter_frame(DCACoreDecoder *s, AVFrame *frame)
2443 AVCodecContext *avctx = s->avctx;
2444 DCAContext *dca = avctx->priv_data;
2445 DCAExssAsset *asset = &dca->exss.assets[0];
2446 enum AVMatrixEncoding matrix_encoding;
2449 // Handle downmixing to stereo request
2450 if (dca->request_channel_layout == DCA_SPEAKER_LAYOUT_STEREO
2451 && s->audio_mode > AMODE_MONO && s->prim_dmix_embedded
2452 && (s->prim_dmix_type == DCA_DMIX_TYPE_LoRo ||
2453 s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
2454 s->request_mask = DCA_SPEAKER_LAYOUT_STEREO;
2456 s->request_mask = s->ch_mask;
2457 if (!ff_dca_set_channel_layout(avctx, s->ch_remap, s->request_mask))
2458 return AVERROR(EINVAL);
2460 // Force fixed point mode when falling back from XLL
2461 if ((avctx->flags & AV_CODEC_FLAG_BITEXACT) || ((dca->packet & DCA_PACKET_EXSS)
2462 && (asset->extension_mask & DCA_EXSS_XLL)))
2463 ret = filter_frame_fixed(s, frame);
2465 ret = filter_frame_float(s, frame);
2469 // Set profile, bit rate, etc
2470 if (s->ext_audio_mask & DCA_EXSS_MASK)
2471 avctx->profile = FF_PROFILE_DTS_HD_HRA;
2472 else if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH))
2473 avctx->profile = FF_PROFILE_DTS_ES;
2474 else if (s->ext_audio_mask & DCA_CSS_X96)
2475 avctx->profile = FF_PROFILE_DTS_96_24;
2477 avctx->profile = FF_PROFILE_DTS;
2479 if (s->bit_rate > 3 && !(s->ext_audio_mask & DCA_EXSS_MASK))
2480 avctx->bit_rate = s->bit_rate;
2482 avctx->bit_rate = 0;
2484 if (s->audio_mode == AMODE_STEREO_TOTAL || (s->request_mask != s->ch_mask &&
2485 s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
2486 matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
2488 matrix_encoding = AV_MATRIX_ENCODING_NONE;
2489 if ((ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
2495 av_cold void ff_dca_core_flush(DCACoreDecoder *s)
2497 if (s->subband_buffer) {
2498 erase_adpcm_history(s);
2499 memset(s->lfe_samples, 0, DCA_LFE_HISTORY * sizeof(int32_t));
2502 if (s->x96_subband_buffer)
2503 erase_x96_adpcm_history(s);
2505 erase_dsp_history(s);
2508 av_cold int ff_dca_core_init(DCACoreDecoder *s)
2510 if (!(s->float_dsp = avpriv_float_dsp_alloc(0)))
2512 if (!(s->fixed_dsp = avpriv_alloc_fixed_dsp(0)))
2515 ff_dcadct_init(&s->dcadct);
2516 if (ff_mdct_init(&s->imdct[0], 6, 1, 1.0) < 0)
2518 if (ff_mdct_init(&s->imdct[1], 7, 1, 1.0) < 0)
2520 ff_synth_filter_init(&s->synth);
2526 av_cold void ff_dca_core_close(DCACoreDecoder *s)
2528 av_freep(&s->float_dsp);
2529 av_freep(&s->fixed_dsp);
2531 ff_mdct_end(&s->imdct[0]);
2532 ff_mdct_end(&s->imdct[1]);
2534 av_freep(&s->subband_buffer);
2535 s->subband_size = 0;
2537 av_freep(&s->x96_subband_buffer);
2538 s->x96_subband_size = 0;
2540 av_freep(&s->output_buffer);