2 * Copyright (C) 2016 foo86
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "dca_syncwords.h"
38 AMODE_MONO, // Mode 0: A (mono)
39 AMODE_MONO_DUAL, // Mode 1: A + B (dual mono)
40 AMODE_STEREO, // Mode 2: L + R (stereo)
41 AMODE_STEREO_SUMDIFF, // Mode 3: (L+R) + (L-R) (sum-diff)
42 AMODE_STEREO_TOTAL, // Mode 4: LT + RT (left and right total)
43 AMODE_3F, // Mode 5: C + L + R
44 AMODE_2F1R, // Mode 6: L + R + S
45 AMODE_3F1R, // Mode 7: C + L + R + S
46 AMODE_2F2R, // Mode 8: L + R + SL + SR
47 AMODE_3F2R, // Mode 9: C + L + R + SL + SR
65 static const int8_t prm_ch_to_spkr_map[AMODE_COUNT][5] = {
66 { DCA_SPEAKER_C, -1, -1, -1, -1 },
67 { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
68 { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
69 { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
70 { DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
71 { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R , -1, -1 },
72 { DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Cs, -1, -1 },
73 { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R , DCA_SPEAKER_Cs, -1 },
74 { DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Ls, DCA_SPEAKER_Rs, -1 },
75 { DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Ls, DCA_SPEAKER_Rs }
78 static const uint8_t audio_mode_ch_mask[AMODE_COUNT] = {
79 DCA_SPEAKER_LAYOUT_MONO,
80 DCA_SPEAKER_LAYOUT_STEREO,
81 DCA_SPEAKER_LAYOUT_STEREO,
82 DCA_SPEAKER_LAYOUT_STEREO,
83 DCA_SPEAKER_LAYOUT_STEREO,
84 DCA_SPEAKER_LAYOUT_3_0,
85 DCA_SPEAKER_LAYOUT_2_1,
86 DCA_SPEAKER_LAYOUT_3_1,
87 DCA_SPEAKER_LAYOUT_2_2,
88 DCA_SPEAKER_LAYOUT_5POINT0
91 static const uint8_t block_code_nbits[7] = {
92 7, 10, 12, 13, 15, 17, 19
95 static const uint8_t quant_index_sel_nbits[DCA_CODE_BOOKS] = {
96 1, 2, 2, 2, 2, 3, 3, 3, 3, 3
99 static const uint8_t quant_index_group_size[DCA_CODE_BOOKS] = {
100 1, 3, 3, 3, 3, 7, 7, 7, 7, 7
103 static int dca_get_vlc(GetBitContext *s, DCAVLC *v, int i)
105 return get_vlc2(s, v->vlc[i].table, v->vlc[i].bits, v->max_depth) + v->offset;
108 static void get_array(GetBitContext *s, int32_t *array, int size, int n)
112 for (i = 0; i < size; i++)
113 array[i] = get_sbits(s, n);
116 // 5.3.1 - Bit stream header
117 static int parse_frame_header(DCACoreDecoder *s)
119 int normal_frame, pcmr_index;
122 normal_frame = get_bits1(&s->gb);
124 // Deficit sample count
125 if (get_bits(&s->gb, 5) != DCA_PCMBLOCK_SAMPLES - 1) {
126 av_log(s->avctx, AV_LOG_ERROR, "Deficit samples are not supported\n");
127 return normal_frame ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME;
131 s->crc_present = get_bits1(&s->gb);
133 // Number of PCM sample blocks
134 s->npcmblocks = get_bits(&s->gb, 7) + 1;
135 if (s->npcmblocks & (DCA_SUBBAND_SAMPLES - 1)) {
136 av_log(s->avctx, AV_LOG_ERROR, "Unsupported number of PCM sample blocks (%d)\n", s->npcmblocks);
137 return (s->npcmblocks < 6 || normal_frame) ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME;
140 // Primary frame byte size
141 s->frame_size = get_bits(&s->gb, 14) + 1;
142 if (s->frame_size < 96) {
143 av_log(s->avctx, AV_LOG_ERROR, "Invalid core frame size (%d bytes)\n", s->frame_size);
144 return AVERROR_INVALIDDATA;
147 // Audio channel arrangement
148 s->audio_mode = get_bits(&s->gb, 6);
149 if (s->audio_mode >= AMODE_COUNT) {
150 av_log(s->avctx, AV_LOG_ERROR, "Unsupported audio channel arrangement (%d)\n", s->audio_mode);
151 return AVERROR_PATCHWELCOME;
154 // Core audio sampling frequency
155 s->sample_rate = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
156 if (!s->sample_rate) {
157 av_log(s->avctx, AV_LOG_ERROR, "Invalid core audio sampling frequency\n");
158 return AVERROR_INVALIDDATA;
161 // Transmission bit rate
162 s->bit_rate = ff_dca_bit_rates[get_bits(&s->gb, 5)];
167 // Embedded dynamic range flag
168 s->drc_present = get_bits1(&s->gb);
170 // Embedded time stamp flag
171 s->ts_present = get_bits1(&s->gb);
173 // Auxiliary data flag
174 s->aux_present = get_bits1(&s->gb);
176 // HDCD mastering flag
179 // Extension audio descriptor flag
180 s->ext_audio_type = get_bits(&s->gb, 3);
182 // Extended coding flag
183 s->ext_audio_present = get_bits1(&s->gb);
185 // Audio sync word insertion flag
186 s->sync_ssf = get_bits1(&s->gb);
188 // Low frequency effects flag
189 s->lfe_present = get_bits(&s->gb, 2);
190 if (s->lfe_present == LFE_FLAG_INVALID) {
191 av_log(s->avctx, AV_LOG_ERROR, "Invalid low frequency effects flag\n");
192 return AVERROR_INVALIDDATA;
195 // Predictor history flag switch
196 s->predictor_history = get_bits1(&s->gb);
198 // Header CRC check bytes
200 skip_bits(&s->gb, 16);
202 // Multirate interpolator switch
203 s->filter_perfect = get_bits1(&s->gb);
205 // Encoder software revision
206 skip_bits(&s->gb, 4);
209 skip_bits(&s->gb, 2);
211 // Source PCM resolution
212 s->source_pcm_res = ff_dca_bits_per_sample[pcmr_index = get_bits(&s->gb, 3)];
213 if (!s->source_pcm_res) {
214 av_log(s->avctx, AV_LOG_ERROR, "Invalid source PCM resolution\n");
215 return AVERROR_INVALIDDATA;
217 s->es_format = pcmr_index & 1;
219 // Front sum/difference flag
220 s->sumdiff_front = get_bits1(&s->gb);
222 // Surround sum/difference flag
223 s->sumdiff_surround = get_bits1(&s->gb);
225 // Dialog normalization / unspecified
226 skip_bits(&s->gb, 4);
231 // 5.3.2 - Primary audio coding header
232 static int parse_coding_header(DCACoreDecoder *s, enum HeaderType header, int xch_base)
234 int n, ch, nchannels, header_size = 0, header_pos = get_bits_count(&s->gb);
235 unsigned int mask, index;
237 if (get_bits_left(&s->gb) < 0)
238 return AVERROR_INVALIDDATA;
242 // Number of subframes
243 s->nsubframes = get_bits(&s->gb, 4) + 1;
245 // Number of primary audio channels
246 s->nchannels = get_bits(&s->gb, 3) + 1;
247 if (s->nchannels != ff_dca_channels[s->audio_mode]) {
248 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of primary audio channels (%d) for audio channel arrangement (%d)\n", s->nchannels, s->audio_mode);
249 return AVERROR_INVALIDDATA;
251 av_assert1(s->nchannels <= DCA_CHANNELS - 2);
253 s->ch_mask = audio_mode_ch_mask[s->audio_mode];
255 // Add LFE channel if present
257 s->ch_mask |= DCA_SPEAKER_MASK_LFE1;
261 s->nchannels = ff_dca_channels[s->audio_mode] + 1;
262 av_assert1(s->nchannels <= DCA_CHANNELS - 1);
263 s->ch_mask |= DCA_SPEAKER_MASK_Cs;
267 // Channel set header length
268 header_size = get_bits(&s->gb, 7) + 1;
271 if (s->xxch_crc_present
272 && ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) {
273 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH channel set header checksum\n");
274 return AVERROR_INVALIDDATA;
277 // Number of channels in a channel set
278 nchannels = get_bits(&s->gb, 3) + 1;
279 if (nchannels > DCA_XXCH_CHANNELS_MAX) {
280 avpriv_request_sample(s->avctx, "%d XXCH channels", nchannels);
281 return AVERROR_PATCHWELCOME;
283 s->nchannels = ff_dca_channels[s->audio_mode] + nchannels;
284 av_assert1(s->nchannels <= DCA_CHANNELS);
286 // Loudspeaker layout mask
287 mask = get_bits_long(&s->gb, s->xxch_mask_nbits - DCA_SPEAKER_Cs);
288 s->xxch_spkr_mask = mask << DCA_SPEAKER_Cs;
290 if (av_popcount(s->xxch_spkr_mask) != nchannels) {
291 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH speaker layout mask (%#x)\n", s->xxch_spkr_mask);
292 return AVERROR_INVALIDDATA;
295 if (s->xxch_core_mask & s->xxch_spkr_mask) {
296 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);
297 return AVERROR_INVALIDDATA;
300 // Combine core and XXCH masks together
301 s->ch_mask = s->xxch_core_mask | s->xxch_spkr_mask;
303 // Downmix coefficients present in stream
304 if (get_bits1(&s->gb)) {
305 int *coeff_ptr = s->xxch_dmix_coeff;
307 // Downmix already performed by encoder
308 s->xxch_dmix_embedded = get_bits1(&s->gb);
310 // Downmix scale factor
311 index = get_bits(&s->gb, 6) * 4 - FF_DCA_DMIXTABLE_OFFSET - 3;
312 if (index >= FF_DCA_INV_DMIXTABLE_SIZE) {
313 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix scale index (%d)\n", index);
314 return AVERROR_INVALIDDATA;
316 s->xxch_dmix_scale_inv = ff_dca_inv_dmixtable[index];
318 // Downmix channel mapping mask
319 for (ch = 0; ch < nchannels; ch++) {
320 mask = get_bits_long(&s->gb, s->xxch_mask_nbits);
321 if ((mask & s->xxch_core_mask) != mask) {
322 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix channel mapping mask (%#x)\n", mask);
323 return AVERROR_INVALIDDATA;
325 s->xxch_dmix_mask[ch] = mask;
328 // Downmix coefficients
329 for (ch = 0; ch < nchannels; ch++) {
330 for (n = 0; n < s->xxch_mask_nbits; n++) {
331 if (s->xxch_dmix_mask[ch] & (1U << n)) {
332 int code = get_bits(&s->gb, 7);
333 int sign = (code >> 6) - 1;
335 index = code * 4 - 3;
336 if (index >= FF_DCA_DMIXTABLE_SIZE) {
337 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix coefficient index (%d)\n", index);
338 return AVERROR_INVALIDDATA;
340 *coeff_ptr++ = (ff_dca_dmixtable[index] ^ sign) - sign;
348 s->xxch_dmix_embedded = 0;
354 // Subband activity count
355 for (ch = xch_base; ch < s->nchannels; ch++) {
356 s->nsubbands[ch] = get_bits(&s->gb, 5) + 2;
357 if (s->nsubbands[ch] > DCA_SUBBANDS) {
358 av_log(s->avctx, AV_LOG_ERROR, "Invalid subband activity count\n");
359 return AVERROR_INVALIDDATA;
363 // High frequency VQ start subband
364 for (ch = xch_base; ch < s->nchannels; ch++)
365 s->subband_vq_start[ch] = get_bits(&s->gb, 5) + 1;
367 // Joint intensity coding index
368 for (ch = xch_base; ch < s->nchannels; ch++) {
369 if ((n = get_bits(&s->gb, 3)) && header == HEADER_XXCH)
371 if (n > s->nchannels) {
372 av_log(s->avctx, AV_LOG_ERROR, "Invalid joint intensity coding index\n");
373 return AVERROR_INVALIDDATA;
375 s->joint_intensity_index[ch] = n;
378 // Transient mode code book
379 for (ch = xch_base; ch < s->nchannels; ch++)
380 s->transition_mode_sel[ch] = get_bits(&s->gb, 2);
382 // Scale factor code book
383 for (ch = xch_base; ch < s->nchannels; ch++) {
384 s->scale_factor_sel[ch] = get_bits(&s->gb, 3);
385 if (s->scale_factor_sel[ch] == 7) {
386 av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor code book\n");
387 return AVERROR_INVALIDDATA;
391 // Bit allocation quantizer select
392 for (ch = xch_base; ch < s->nchannels; ch++) {
393 s->bit_allocation_sel[ch] = get_bits(&s->gb, 3);
394 if (s->bit_allocation_sel[ch] == 7) {
395 av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation quantizer select\n");
396 return AVERROR_INVALIDDATA;
400 // Quantization index codebook select
401 for (n = 0; n < DCA_CODE_BOOKS; n++)
402 for (ch = xch_base; ch < s->nchannels; ch++)
403 s->quant_index_sel[ch][n] = get_bits(&s->gb, quant_index_sel_nbits[n]);
405 // Scale factor adjustment index
406 for (n = 0; n < DCA_CODE_BOOKS; n++)
407 for (ch = xch_base; ch < s->nchannels; ch++)
408 if (s->quant_index_sel[ch][n] < quant_index_group_size[n])
409 s->scale_factor_adj[ch][n] = ff_dca_scale_factor_adj[get_bits(&s->gb, 2)];
411 if (header == HEADER_XXCH) {
414 // CRC16 of channel set header
415 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
416 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set header\n");
417 return AVERROR_INVALIDDATA;
420 // Audio header CRC check word
422 skip_bits(&s->gb, 16);
428 static inline int parse_scale(DCACoreDecoder *s, int *scale_index, int sel)
430 const uint32_t *scale_table;
431 unsigned int scale_size;
433 // Select the root square table
435 scale_table = ff_dca_scale_factor_quant7;
436 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7);
438 scale_table = ff_dca_scale_factor_quant6;
439 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6);
442 // If Huffman code was used, the difference of scales was encoded
444 *scale_index += dca_get_vlc(&s->gb, &ff_dca_vlc_scale_factor, sel);
446 *scale_index = get_bits(&s->gb, sel + 1);
448 // Look up scale factor from the root square table
449 if ((unsigned int)*scale_index >= scale_size) {
450 av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor index\n");
451 return AVERROR_INVALIDDATA;
454 return scale_table[*scale_index];
457 static inline int parse_joint_scale(DCACoreDecoder *s, int sel)
461 // Absolute value was encoded even when Huffman code was used
463 scale_index = dca_get_vlc(&s->gb, &ff_dca_vlc_scale_factor, sel);
465 scale_index = get_bits(&s->gb, sel + 1);
470 // Look up joint scale factor
471 if ((unsigned int)scale_index >= FF_ARRAY_ELEMS(ff_dca_joint_scale_factors)) {
472 av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor index\n");
473 return AVERROR_INVALIDDATA;
476 return ff_dca_joint_scale_factors[scale_index];
479 // 5.4.1 - Primary audio coding side information
480 static int parse_subframe_header(DCACoreDecoder *s, int sf,
481 enum HeaderType header, int xch_base)
485 if (get_bits_left(&s->gb) < 0)
486 return AVERROR_INVALIDDATA;
488 if (header == HEADER_CORE) {
490 s->nsubsubframes[sf] = get_bits(&s->gb, 2) + 1;
492 // Partial subsubframe sample count
493 skip_bits(&s->gb, 3);
497 for (ch = xch_base; ch < s->nchannels; ch++)
498 for (band = 0; band < s->nsubbands[ch]; band++)
499 s->prediction_mode[ch][band] = get_bits1(&s->gb);
501 // Prediction coefficients VQ address
502 for (ch = xch_base; ch < s->nchannels; ch++)
503 for (band = 0; band < s->nsubbands[ch]; band++)
504 if (s->prediction_mode[ch][band])
505 s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12);
507 // Bit allocation index
508 for (ch = xch_base; ch < s->nchannels; ch++) {
509 int sel = s->bit_allocation_sel[ch];
511 for (band = 0; band < s->subband_vq_start[ch]; band++) {
515 abits = dca_get_vlc(&s->gb, &ff_dca_vlc_bit_allocation, sel);
517 abits = get_bits(&s->gb, sel - 1);
519 if (abits > DCA_ABITS_MAX) {
520 av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation index\n");
521 return AVERROR_INVALIDDATA;
524 s->bit_allocation[ch][band] = abits;
529 for (ch = xch_base; ch < s->nchannels; ch++) {
530 // Clear transition mode for all subbands
531 memset(s->transition_mode[sf][ch], 0, sizeof(s->transition_mode[0][0]));
533 // Transient possible only if more than one subsubframe
534 if (s->nsubsubframes[sf] > 1) {
535 int sel = s->transition_mode_sel[ch];
536 for (band = 0; band < s->subband_vq_start[ch]; band++)
537 if (s->bit_allocation[ch][band])
538 s->transition_mode[sf][ch][band] = dca_get_vlc(&s->gb, &ff_dca_vlc_transition_mode, sel);
543 for (ch = xch_base; ch < s->nchannels; ch++) {
544 int sel = s->scale_factor_sel[ch];
547 // Extract scales for subbands up to VQ
548 for (band = 0; band < s->subband_vq_start[ch]; band++) {
549 if (s->bit_allocation[ch][band]) {
550 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
552 s->scale_factors[ch][band][0] = ret;
553 if (s->transition_mode[sf][ch][band]) {
554 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
556 s->scale_factors[ch][band][1] = ret;
559 s->scale_factors[ch][band][0] = 0;
563 // High frequency VQ subbands
564 for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++) {
565 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
567 s->scale_factors[ch][band][0] = ret;
571 // Joint subband codebook select
572 for (ch = xch_base; ch < s->nchannels; ch++) {
573 if (s->joint_intensity_index[ch]) {
574 s->joint_scale_sel[ch] = get_bits(&s->gb, 3);
575 if (s->joint_scale_sel[ch] == 7) {
576 av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor code book\n");
577 return AVERROR_INVALIDDATA;
582 // Scale factors for joint subband coding
583 for (ch = xch_base; ch < s->nchannels; ch++) {
584 int src_ch = s->joint_intensity_index[ch] - 1;
586 int sel = s->joint_scale_sel[ch];
587 for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) {
588 if ((ret = parse_joint_scale(s, sel)) < 0)
590 s->joint_scale_factors[ch][band] = ret;
595 // Dynamic range coefficient
596 if (s->drc_present && header == HEADER_CORE)
597 skip_bits(&s->gb, 8);
599 // Side information CRC check word
601 skip_bits(&s->gb, 16);
606 #ifndef decode_blockcodes
607 static inline int decode_blockcodes(int code1, int code2, int levels, int32_t *audio)
609 int offset = (levels - 1) / 2;
612 for (n = 0; n < DCA_SUBBAND_SAMPLES / 2; n++) {
613 div = FASTDIV(code1, levels);
614 audio[n] = code1 - div * levels - offset;
617 for (; n < DCA_SUBBAND_SAMPLES; n++) {
618 div = FASTDIV(code2, levels);
619 audio[n] = code2 - div * levels - offset;
623 return code1 | code2;
627 static inline int parse_block_codes(DCACoreDecoder *s, int32_t *audio, int abits)
629 // Extract block code indices from the bit stream
630 int code1 = get_bits(&s->gb, block_code_nbits[abits - 1]);
631 int code2 = get_bits(&s->gb, block_code_nbits[abits - 1]);
632 int levels = ff_dca_quant_levels[abits];
634 // Look up samples from the block code book
635 if (decode_blockcodes(code1, code2, levels, audio)) {
636 av_log(s->avctx, AV_LOG_ERROR, "Failed to decode block code(s)\n");
637 return AVERROR_INVALIDDATA;
643 static inline int parse_huffman_codes(DCACoreDecoder *s, int32_t *audio, int abits, int sel)
647 // Extract Huffman codes from the bit stream
648 for (i = 0; i < DCA_SUBBAND_SAMPLES; i++)
649 audio[i] = dca_get_vlc(&s->gb, &ff_dca_vlc_quant_index[abits - 1], sel);
654 static inline int extract_audio(DCACoreDecoder *s, int32_t *audio, int abits, int ch)
656 av_assert1(abits >= 0 && abits <= DCA_ABITS_MAX);
660 memset(audio, 0, DCA_SUBBAND_SAMPLES * sizeof(*audio));
664 if (abits <= DCA_CODE_BOOKS) {
665 int sel = s->quant_index_sel[ch][abits - 1];
666 if (sel < quant_index_group_size[abits - 1]) {
668 return parse_huffman_codes(s, audio, abits, sel);
672 return parse_block_codes(s, audio, abits);
676 // No further encoding
677 get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3);
681 static inline void dequantize(int32_t *output, const int32_t *input,
682 int32_t step_size, int32_t scale, int residual)
684 // Account for quantizer step size
685 int64_t step_scale = (int64_t)step_size * scale;
688 // Limit scale factor resolution to 22 bits
689 if (step_scale > (1 << 23)) {
690 shift = av_log2(step_scale >> 23) + 1;
691 step_scale >>= shift;
696 for (n = 0; n < DCA_SUBBAND_SAMPLES; n++)
697 output[n] += clip23(norm__(input[n] * step_scale, 22 - shift));
699 for (n = 0; n < DCA_SUBBAND_SAMPLES; n++)
700 output[n] = clip23(norm__(input[n] * step_scale, 22 - shift));
704 static inline void inverse_adpcm(int32_t **subband_samples,
705 const int16_t *vq_index,
706 const int8_t *prediction_mode,
707 int sb_start, int sb_end,
712 for (i = sb_start; i < sb_end; i++) {
713 if (prediction_mode[i]) {
714 const int16_t *coeff = ff_dca_adpcm_vb[vq_index[i]];
715 int32_t *ptr = subband_samples[i] + ofs;
716 for (j = 0; j < len; j++) {
718 for (k = 0; k < DCA_ADPCM_COEFFS; k++)
719 err += (int64_t)ptr[j - k - 1] * coeff[k];
720 ptr[j] = clip23(ptr[j] + clip23(norm13(err)));
726 // 5.5 - Primary audio data arrays
727 static int parse_subframe_audio(DCACoreDecoder *s, int sf, enum HeaderType header,
728 int xch_base, int *sub_pos, int *lfe_pos)
730 int32_t audio[16], scale;
731 int n, ssf, ofs, ch, band;
733 // Check number of subband samples in this subframe
734 int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES;
735 if (*sub_pos + nsamples > s->npcmblocks) {
736 av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
737 return AVERROR_INVALIDDATA;
740 if (get_bits_left(&s->gb) < 0)
741 return AVERROR_INVALIDDATA;
743 // VQ encoded subbands
744 for (ch = xch_base; ch < s->nchannels; ch++) {
745 int32_t vq_index[DCA_SUBBANDS];
747 for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++)
748 // Extract the VQ address from the bit stream
749 vq_index[band] = get_bits(&s->gb, 10);
751 if (s->subband_vq_start[ch] < s->nsubbands[ch]) {
752 s->dcadsp->decode_hf(s->subband_samples[ch], vq_index,
753 ff_dca_high_freq_vq, s->scale_factors[ch],
754 s->subband_vq_start[ch], s->nsubbands[ch],
759 // Low frequency effect data
760 if (s->lfe_present && header == HEADER_CORE) {
763 // Determine number of LFE samples in this subframe
764 int nlfesamples = 2 * s->lfe_present * s->nsubsubframes[sf];
765 av_assert1((unsigned int)nlfesamples <= FF_ARRAY_ELEMS(audio));
767 // Extract LFE samples from the bit stream
768 get_array(&s->gb, audio, nlfesamples, 8);
770 // Extract scale factor index from the bit stream
771 index = get_bits(&s->gb, 8);
772 if (index >= FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7)) {
773 av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE scale factor index\n");
774 return AVERROR_INVALIDDATA;
777 // Look up the 7-bit root square quantization table
778 scale = ff_dca_scale_factor_quant7[index];
780 // Account for quantizer step size which is 0.035
781 scale = mul23(4697620 /* 0.035 * (1 << 27) */, scale);
783 // Scale and take the LFE samples
784 for (n = 0, ofs = *lfe_pos; n < nlfesamples; n++, ofs++)
785 s->lfe_samples[ofs] = clip23(audio[n] * scale >> 4);
787 // Advance LFE sample pointer for the next subframe
792 for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
793 for (ch = xch_base; ch < s->nchannels; ch++) {
794 if (get_bits_left(&s->gb) < 0)
795 return AVERROR_INVALIDDATA;
797 // Not high frequency VQ subbands
798 for (band = 0; band < s->subband_vq_start[ch]; band++) {
799 int ret, trans_ssf, abits = s->bit_allocation[ch][band];
802 // Extract bits from the bit stream
803 if ((ret = extract_audio(s, audio, abits, ch)) < 0)
806 // Select quantization step size table and look up
807 // quantization step size
808 if (s->bit_rate == 3)
809 step_size = ff_dca_lossless_quant[abits];
811 step_size = ff_dca_lossy_quant[abits];
813 // Identify transient location
814 trans_ssf = s->transition_mode[sf][ch][band];
816 // Determine proper scale factor
817 if (trans_ssf == 0 || ssf < trans_ssf)
818 scale = s->scale_factors[ch][band][0];
820 scale = s->scale_factors[ch][band][1];
822 // Adjust scale factor when SEL indicates Huffman code
824 int64_t adj = s->scale_factor_adj[ch][abits - 1];
825 scale = clip23(adj * scale >> 22);
828 dequantize(s->subband_samples[ch][band] + ofs,
829 audio, step_size, scale, 0);
834 if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
835 av_log(s->avctx, AV_LOG_ERROR, "DSYNC check failed\n");
836 return AVERROR_INVALIDDATA;
839 ofs += DCA_SUBBAND_SAMPLES;
843 for (ch = xch_base; ch < s->nchannels; ch++) {
844 inverse_adpcm(s->subband_samples[ch], s->prediction_vq_index[ch],
845 s->prediction_mode[ch], 0, s->nsubbands[ch],
849 // Joint subband coding
850 for (ch = xch_base; ch < s->nchannels; ch++) {
851 int src_ch = s->joint_intensity_index[ch] - 1;
853 s->dcadsp->decode_joint(s->subband_samples[ch], s->subband_samples[src_ch],
854 s->joint_scale_factors[ch], s->nsubbands[ch],
855 s->nsubbands[src_ch], *sub_pos, nsamples);
859 // Advance subband sample pointer for the next subframe
864 static void erase_adpcm_history(DCACoreDecoder *s)
868 // Erase ADPCM history from previous frame if
869 // predictor history switch was disabled
870 for (ch = 0; ch < DCA_CHANNELS; ch++)
871 for (band = 0; band < DCA_SUBBANDS; band++)
872 AV_ZERO128(s->subband_samples[ch][band] - DCA_ADPCM_COEFFS);
877 static int alloc_sample_buffer(DCACoreDecoder *s)
879 int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks;
880 int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS;
881 int nlfesamples = DCA_LFE_HISTORY + s->npcmblocks / 2;
882 unsigned int size = s->subband_size;
885 // Reallocate subband sample buffer
886 av_fast_mallocz(&s->subband_buffer, &s->subband_size,
887 (nframesamples + nlfesamples) * sizeof(int32_t));
888 if (!s->subband_buffer)
889 return AVERROR(ENOMEM);
891 if (size != s->subband_size) {
892 for (ch = 0; ch < DCA_CHANNELS; ch++)
893 for (band = 0; band < DCA_SUBBANDS; band++)
894 s->subband_samples[ch][band] = s->subband_buffer +
895 (ch * DCA_SUBBANDS + band) * nchsamples + DCA_ADPCM_COEFFS;
896 s->lfe_samples = s->subband_buffer + nframesamples;
899 if (!s->predictor_history)
900 erase_adpcm_history(s);
905 static int parse_frame_data(DCACoreDecoder *s, enum HeaderType header, int xch_base)
907 int sf, ch, ret, band, sub_pos, lfe_pos;
909 if ((ret = parse_coding_header(s, header, xch_base)) < 0)
912 for (sf = 0, sub_pos = 0, lfe_pos = DCA_LFE_HISTORY; sf < s->nsubframes; sf++) {
913 if ((ret = parse_subframe_header(s, sf, header, xch_base)) < 0)
915 if ((ret = parse_subframe_audio(s, sf, header, xch_base, &sub_pos, &lfe_pos)) < 0)
919 for (ch = xch_base; ch < s->nchannels; ch++) {
920 // Determine number of active subbands for this channel
921 int nsubbands = s->nsubbands[ch];
922 if (s->joint_intensity_index[ch])
923 nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]);
925 // Update history for ADPCM
926 for (band = 0; band < nsubbands; band++) {
927 int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS;
928 AV_COPY128(samples, samples + s->npcmblocks);
931 // Clear inactive subbands
932 for (; band < DCA_SUBBANDS; band++) {
933 int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS;
934 memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t));
943 static int parse_xch_frame(DCACoreDecoder *s)
947 if (s->ch_mask & DCA_SPEAKER_MASK_Cs) {
948 av_log(s->avctx, AV_LOG_ERROR, "XCH with Cs speaker already present\n");
949 return AVERROR_INVALIDDATA;
952 if ((ret = parse_frame_data(s, HEADER_XCH, s->nchannels)) < 0)
955 // Seek to the end of core frame, don't trust XCH frame size
956 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
957 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XCH frame\n");
958 return AVERROR_INVALIDDATA;
964 static int parse_xxch_frame(DCACoreDecoder *s)
966 int xxch_nchsets, xxch_frame_size;
967 int ret, mask, header_size, header_pos = get_bits_count(&s->gb);
970 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XXCH) {
971 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH sync word\n");
972 return AVERROR_INVALIDDATA;
975 // XXCH frame header length
976 header_size = get_bits(&s->gb, 6) + 1;
978 // Check XXCH frame header CRC
979 if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) {
980 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH frame header checksum\n");
981 return AVERROR_INVALIDDATA;
984 // CRC presence flag for channel set header
985 s->xxch_crc_present = get_bits1(&s->gb);
987 // Number of bits for loudspeaker mask
988 s->xxch_mask_nbits = get_bits(&s->gb, 5) + 1;
989 if (s->xxch_mask_nbits <= DCA_SPEAKER_Cs) {
990 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XXCH speaker mask (%d)\n", s->xxch_mask_nbits);
991 return AVERROR_INVALIDDATA;
994 // Number of channel sets
995 xxch_nchsets = get_bits(&s->gb, 2) + 1;
996 if (xxch_nchsets > 1) {
997 avpriv_request_sample(s->avctx, "%d XXCH channel sets", xxch_nchsets);
998 return AVERROR_PATCHWELCOME;
1001 // Channel set 0 data byte size
1002 xxch_frame_size = get_bits(&s->gb, 14) + 1;
1004 // Core loudspeaker activity mask
1005 s->xxch_core_mask = get_bits_long(&s->gb, s->xxch_mask_nbits);
1007 // Validate the core mask
1010 if ((mask & DCA_SPEAKER_MASK_Ls) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss))
1011 mask = (mask & ~DCA_SPEAKER_MASK_Ls) | DCA_SPEAKER_MASK_Lss;
1013 if ((mask & DCA_SPEAKER_MASK_Rs) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss))
1014 mask = (mask & ~DCA_SPEAKER_MASK_Rs) | DCA_SPEAKER_MASK_Rss;
1016 if (mask != s->xxch_core_mask) {
1017 av_log(s->avctx, AV_LOG_ERROR, "XXCH core speaker activity mask (%#x) disagrees with core (%#x)\n", s->xxch_core_mask, mask);
1018 return AVERROR_INVALIDDATA;
1023 // CRC16 of XXCH frame header
1024 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1025 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH frame header\n");
1026 return AVERROR_INVALIDDATA;
1029 // Parse XXCH channel set 0
1030 if ((ret = parse_frame_data(s, HEADER_XXCH, s->nchannels)) < 0)
1033 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8 + xxch_frame_size * 8)) {
1034 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set\n");
1035 return AVERROR_INVALIDDATA;
1041 static int parse_xbr_subframe(DCACoreDecoder *s, int xbr_base_ch, int xbr_nchannels,
1042 int *xbr_nsubbands, int xbr_transition_mode, int sf, int *sub_pos)
1044 int xbr_nabits[DCA_CHANNELS];
1045 int xbr_bit_allocation[DCA_CHANNELS][DCA_SUBBANDS];
1046 int xbr_scale_nbits[DCA_CHANNELS];
1047 int32_t xbr_scale_factors[DCA_CHANNELS][DCA_SUBBANDS][2];
1048 int ssf, ch, band, ofs;
1050 // Check number of subband samples in this subframe
1051 if (*sub_pos + s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES > s->npcmblocks) {
1052 av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
1053 return AVERROR_INVALIDDATA;
1056 if (get_bits_left(&s->gb) < 0)
1057 return AVERROR_INVALIDDATA;
1059 // Number of bits for XBR bit allocation index
1060 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++)
1061 xbr_nabits[ch] = get_bits(&s->gb, 2) + 2;
1063 // XBR bit allocation index
1064 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1065 for (band = 0; band < xbr_nsubbands[ch]; band++) {
1066 xbr_bit_allocation[ch][band] = get_bits(&s->gb, xbr_nabits[ch]);
1067 if (xbr_bit_allocation[ch][band] > DCA_ABITS_MAX) {
1068 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR bit allocation index\n");
1069 return AVERROR_INVALIDDATA;
1074 // Number of bits for scale indices
1075 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1076 xbr_scale_nbits[ch] = get_bits(&s->gb, 3);
1077 if (!xbr_scale_nbits[ch]) {
1078 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XBR scale factor index\n");
1079 return AVERROR_INVALIDDATA;
1083 // XBR scale factors
1084 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1085 const uint32_t *scale_table;
1088 // Select the root square table
1089 if (s->scale_factor_sel[ch] > 5) {
1090 scale_table = ff_dca_scale_factor_quant7;
1091 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7);
1093 scale_table = ff_dca_scale_factor_quant6;
1094 scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6);
1097 // Parse scale factor indices and look up scale factors from the root
1099 for (band = 0; band < xbr_nsubbands[ch]; band++) {
1100 if (xbr_bit_allocation[ch][band]) {
1101 int scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
1102 if (scale_index >= scale_size) {
1103 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
1104 return AVERROR_INVALIDDATA;
1106 xbr_scale_factors[ch][band][0] = scale_table[scale_index];
1107 if (xbr_transition_mode && s->transition_mode[sf][ch][band]) {
1108 scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
1109 if (scale_index >= scale_size) {
1110 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
1111 return AVERROR_INVALIDDATA;
1113 xbr_scale_factors[ch][band][1] = scale_table[scale_index];
1120 for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
1121 for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1122 if (get_bits_left(&s->gb) < 0)
1123 return AVERROR_INVALIDDATA;
1125 for (band = 0; band < xbr_nsubbands[ch]; band++) {
1126 int ret, trans_ssf, abits = xbr_bit_allocation[ch][band];
1127 int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale;
1129 // Extract bits from the bit stream
1131 // No further encoding
1132 get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3);
1133 } else if (abits > 0) {
1135 if ((ret = parse_block_codes(s, audio, abits)) < 0)
1138 // No bits allocated
1142 // Look up quantization step size
1143 step_size = ff_dca_lossless_quant[abits];
1145 // Identify transient location
1146 if (xbr_transition_mode)
1147 trans_ssf = s->transition_mode[sf][ch][band];
1151 // Determine proper scale factor
1152 if (trans_ssf == 0 || ssf < trans_ssf)
1153 scale = xbr_scale_factors[ch][band][0];
1155 scale = xbr_scale_factors[ch][band][1];
1157 dequantize(s->subband_samples[ch][band] + ofs,
1158 audio, step_size, scale, 1);
1163 if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
1164 av_log(s->avctx, AV_LOG_ERROR, "XBR-DSYNC check failed\n");
1165 return AVERROR_INVALIDDATA;
1168 ofs += DCA_SUBBAND_SAMPLES;
1171 // Advance subband sample pointer for the next subframe
1176 static int parse_xbr_frame(DCACoreDecoder *s)
1178 int xbr_frame_size[DCA_EXSS_CHSETS_MAX];
1179 int xbr_nchannels[DCA_EXSS_CHSETS_MAX];
1180 int xbr_nsubbands[DCA_EXSS_CHSETS_MAX * DCA_EXSS_CHANNELS_MAX];
1181 int xbr_nchsets, xbr_transition_mode, xbr_band_nbits, xbr_base_ch;
1182 int i, ch1, ch2, ret, header_size, header_pos = get_bits_count(&s->gb);
1185 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XBR) {
1186 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR sync word\n");
1187 return AVERROR_INVALIDDATA;
1190 // XBR frame header length
1191 header_size = get_bits(&s->gb, 6) + 1;
1193 // Check XBR frame header CRC
1194 if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) {
1195 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR frame header checksum\n");
1196 return AVERROR_INVALIDDATA;
1199 // Number of channel sets
1200 xbr_nchsets = get_bits(&s->gb, 2) + 1;
1202 // Channel set data byte size
1203 for (i = 0; i < xbr_nchsets; i++)
1204 xbr_frame_size[i] = get_bits(&s->gb, 14) + 1;
1206 // Transition mode flag
1207 xbr_transition_mode = get_bits1(&s->gb);
1209 // Channel set headers
1210 for (i = 0, ch2 = 0; i < xbr_nchsets; i++) {
1211 xbr_nchannels[i] = get_bits(&s->gb, 3) + 1;
1212 xbr_band_nbits = get_bits(&s->gb, 2) + 5;
1213 for (ch1 = 0; ch1 < xbr_nchannels[i]; ch1++, ch2++) {
1214 xbr_nsubbands[ch2] = get_bits(&s->gb, xbr_band_nbits) + 1;
1215 if (xbr_nsubbands[ch2] > DCA_SUBBANDS) {
1216 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of active XBR subbands (%d)\n", xbr_nsubbands[ch2]);
1217 return AVERROR_INVALIDDATA;
1224 // CRC16 of XBR frame header
1225 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1226 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR frame header\n");
1227 return AVERROR_INVALIDDATA;
1231 for (i = 0, xbr_base_ch = 0; i < xbr_nchsets; i++) {
1232 header_pos = get_bits_count(&s->gb);
1234 if (xbr_base_ch + xbr_nchannels[i] <= s->nchannels) {
1237 for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) {
1238 if ((ret = parse_xbr_subframe(s, xbr_base_ch,
1239 xbr_base_ch + xbr_nchannels[i],
1240 xbr_nsubbands, xbr_transition_mode,
1246 xbr_base_ch += xbr_nchannels[i];
1248 if (ff_dca_seek_bits(&s->gb, header_pos + xbr_frame_size[i] * 8)) {
1249 av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR channel set\n");
1250 return AVERROR_INVALIDDATA;
1257 // Modified ISO/IEC 9899 linear congruential generator
1258 // Returns pseudorandom integer in range [-2^30, 2^30 - 1]
1259 static int rand_x96(DCACoreDecoder *s)
1261 s->x96_rand = 1103515245U * s->x96_rand + 12345U;
1262 return (s->x96_rand & 0x7fffffff) - 0x40000000;
1265 static int parse_x96_subframe_audio(DCACoreDecoder *s, int sf, int xch_base, int *sub_pos)
1267 int n, ssf, ch, band, ofs;
1269 // Check number of subband samples in this subframe
1270 int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES;
1271 if (*sub_pos + nsamples > s->npcmblocks) {
1272 av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
1273 return AVERROR_INVALIDDATA;
1276 if (get_bits_left(&s->gb) < 0)
1277 return AVERROR_INVALIDDATA;
1279 // VQ encoded or unallocated subbands
1280 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1281 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1282 // Get the sample pointer and scale factor
1283 int32_t *samples = s->x96_subband_samples[ch][band] + *sub_pos;
1284 int32_t scale = s->scale_factors[ch][band >> 1][band & 1];
1286 switch (s->bit_allocation[ch][band]) {
1287 case 0: // No bits allocated for subband
1289 memset(samples, 0, nsamples * sizeof(int32_t));
1290 else for (n = 0; n < nsamples; n++)
1291 // Generate scaled random samples
1292 samples[n] = mul31(rand_x96(s), scale);
1295 case 1: // VQ encoded subband
1296 for (ssf = 0; ssf < (s->nsubsubframes[sf] + 1) / 2; ssf++) {
1297 // Extract the VQ address from the bit stream and look up
1298 // the VQ code book for up to 16 subband samples
1299 const int8_t *vq_samples = ff_dca_high_freq_vq[get_bits(&s->gb, 10)];
1300 // Scale and take the samples
1301 for (n = 0; n < FFMIN(nsamples - ssf * 16, 16); n++)
1302 *samples++ = clip23(vq_samples[n] * scale + (1 << 3) >> 4);
1310 for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
1311 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1312 if (get_bits_left(&s->gb) < 0)
1313 return AVERROR_INVALIDDATA;
1315 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1316 int ret, abits = s->bit_allocation[ch][band] - 1;
1317 int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale;
1319 // Not VQ encoded or unallocated subbands
1323 // Extract bits from the bit stream
1324 if ((ret = extract_audio(s, audio, abits, ch)) < 0)
1327 // Select quantization step size table and look up quantization
1329 if (s->bit_rate == 3)
1330 step_size = ff_dca_lossless_quant[abits];
1332 step_size = ff_dca_lossy_quant[abits];
1334 // Get the scale factor
1335 scale = s->scale_factors[ch][band >> 1][band & 1];
1337 dequantize(s->x96_subband_samples[ch][band] + ofs,
1338 audio, step_size, scale, 0);
1343 if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
1344 av_log(s->avctx, AV_LOG_ERROR, "X96-DSYNC check failed\n");
1345 return AVERROR_INVALIDDATA;
1348 ofs += DCA_SUBBAND_SAMPLES;
1352 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1353 inverse_adpcm(s->x96_subband_samples[ch], s->prediction_vq_index[ch],
1354 s->prediction_mode[ch], s->x96_subband_start, s->nsubbands[ch],
1355 *sub_pos, nsamples);
1358 // Joint subband coding
1359 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1360 int src_ch = s->joint_intensity_index[ch] - 1;
1362 s->dcadsp->decode_joint(s->x96_subband_samples[ch], s->x96_subband_samples[src_ch],
1363 s->joint_scale_factors[ch], s->nsubbands[ch],
1364 s->nsubbands[src_ch], *sub_pos, nsamples);
1368 // Advance subband sample pointer for the next subframe
1373 static void erase_x96_adpcm_history(DCACoreDecoder *s)
1377 // Erase ADPCM history from previous frame if
1378 // predictor history switch was disabled
1379 for (ch = 0; ch < DCA_CHANNELS; ch++)
1380 for (band = 0; band < DCA_SUBBANDS_X96; band++)
1381 AV_ZERO128(s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS);
1386 static int alloc_x96_sample_buffer(DCACoreDecoder *s)
1388 int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks;
1389 int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS_X96;
1390 unsigned int size = s->x96_subband_size;
1393 // Reallocate subband sample buffer
1394 av_fast_mallocz(&s->x96_subband_buffer, &s->x96_subband_size,
1395 nframesamples * sizeof(int32_t));
1396 if (!s->x96_subband_buffer)
1397 return AVERROR(ENOMEM);
1399 if (size != s->x96_subband_size) {
1400 for (ch = 0; ch < DCA_CHANNELS; ch++)
1401 for (band = 0; band < DCA_SUBBANDS_X96; band++)
1402 s->x96_subband_samples[ch][band] = s->x96_subband_buffer +
1403 (ch * DCA_SUBBANDS_X96 + band) * nchsamples + DCA_ADPCM_COEFFS;
1406 if (!s->predictor_history)
1407 erase_x96_adpcm_history(s);
1412 static int parse_x96_subframe_header(DCACoreDecoder *s, int xch_base)
1416 if (get_bits_left(&s->gb) < 0)
1417 return AVERROR_INVALIDDATA;
1420 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1421 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++)
1422 s->prediction_mode[ch][band] = get_bits1(&s->gb);
1424 // Prediction coefficients VQ address
1425 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1426 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++)
1427 if (s->prediction_mode[ch][band])
1428 s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12);
1430 // Bit allocation index
1431 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1432 int sel = s->bit_allocation_sel[ch];
1435 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1436 // If Huffman code was used, the difference of abits was encoded
1438 abits += dca_get_vlc(&s->gb, &ff_dca_vlc_quant_index[5 + 2 * s->x96_high_res], sel);
1440 abits = get_bits(&s->gb, 3 + s->x96_high_res);
1442 if (abits < 0 || abits > 7 + 8 * s->x96_high_res) {
1443 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 bit allocation index\n");
1444 return AVERROR_INVALIDDATA;
1447 s->bit_allocation[ch][band] = abits;
1452 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1453 int sel = s->scale_factor_sel[ch];
1454 int scale_index = 0;
1456 // Extract scales for subbands which are transmitted even for
1457 // unallocated subbands
1458 for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1459 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
1461 s->scale_factors[ch][band >> 1][band & 1] = ret;
1465 // Joint subband codebook select
1466 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1467 if (s->joint_intensity_index[ch]) {
1468 s->joint_scale_sel[ch] = get_bits(&s->gb, 3);
1469 if (s->joint_scale_sel[ch] == 7) {
1470 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint scale factor code book\n");
1471 return AVERROR_INVALIDDATA;
1476 // Scale factors for joint subband coding
1477 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1478 int src_ch = s->joint_intensity_index[ch] - 1;
1480 int sel = s->joint_scale_sel[ch];
1481 for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) {
1482 if ((ret = parse_joint_scale(s, sel)) < 0)
1484 s->joint_scale_factors[ch][band] = ret;
1489 // Side information CRC check word
1491 skip_bits(&s->gb, 16);
1496 static int parse_x96_coding_header(DCACoreDecoder *s, int exss, int xch_base)
1498 int n, ch, header_size = 0, header_pos = get_bits_count(&s->gb);
1500 if (get_bits_left(&s->gb) < 0)
1501 return AVERROR_INVALIDDATA;
1504 // Channel set header length
1505 header_size = get_bits(&s->gb, 7) + 1;
1508 if (s->x96_crc_present
1509 && ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) {
1510 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 channel set header checksum\n");
1511 return AVERROR_INVALIDDATA;
1515 // High resolution flag
1516 s->x96_high_res = get_bits1(&s->gb);
1518 // First encoded subband
1519 if (s->x96_rev_no < 8) {
1520 s->x96_subband_start = get_bits(&s->gb, 5);
1521 if (s->x96_subband_start > 27) {
1522 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband start index (%d)\n", s->x96_subband_start);
1523 return AVERROR_INVALIDDATA;
1526 s->x96_subband_start = DCA_SUBBANDS;
1529 // Subband activity count
1530 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1531 s->nsubbands[ch] = get_bits(&s->gb, 6) + 1;
1532 if (s->nsubbands[ch] < DCA_SUBBANDS) {
1533 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband activity count (%d)\n", s->nsubbands[ch]);
1534 return AVERROR_INVALIDDATA;
1538 // Joint intensity coding index
1539 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1540 if ((n = get_bits(&s->gb, 3)) && xch_base)
1542 if (n > s->x96_nchannels) {
1543 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint intensity coding index\n");
1544 return AVERROR_INVALIDDATA;
1546 s->joint_intensity_index[ch] = n;
1549 // Scale factor code book
1550 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1551 s->scale_factor_sel[ch] = get_bits(&s->gb, 3);
1552 if (s->scale_factor_sel[ch] >= 6) {
1553 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 scale factor code book\n");
1554 return AVERROR_INVALIDDATA;
1558 // Bit allocation quantizer select
1559 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1560 s->bit_allocation_sel[ch] = get_bits(&s->gb, 3);
1562 // Quantization index codebook select
1563 for (n = 0; n < 6 + 4 * s->x96_high_res; n++)
1564 for (ch = xch_base; ch < s->x96_nchannels; ch++)
1565 s->quant_index_sel[ch][n] = get_bits(&s->gb, quant_index_sel_nbits[n]);
1570 // CRC16 of channel set header
1571 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1572 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set header\n");
1573 return AVERROR_INVALIDDATA;
1577 skip_bits(&s->gb, 16);
1583 static int parse_x96_frame_data(DCACoreDecoder *s, int exss, int xch_base)
1585 int sf, ch, ret, band, sub_pos;
1587 if ((ret = parse_x96_coding_header(s, exss, xch_base)) < 0)
1590 for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) {
1591 if ((ret = parse_x96_subframe_header(s, xch_base)) < 0)
1593 if ((ret = parse_x96_subframe_audio(s, sf, xch_base, &sub_pos)) < 0)
1597 for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1598 // Determine number of active subbands for this channel
1599 int nsubbands = s->nsubbands[ch];
1600 if (s->joint_intensity_index[ch])
1601 nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]);
1603 // Update history for ADPCM and clear inactive subbands
1604 for (band = 0; band < DCA_SUBBANDS_X96; band++) {
1605 int32_t *samples = s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS;
1606 if (band >= s->x96_subband_start && band < nsubbands)
1607 AV_COPY128(samples, samples + s->npcmblocks);
1609 memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t));
1618 static int parse_x96_frame(DCACoreDecoder *s)
1623 s->x96_rev_no = get_bits(&s->gb, 4);
1624 if (s->x96_rev_no < 1 || s->x96_rev_no > 8) {
1625 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no);
1626 return AVERROR_INVALIDDATA;
1629 s->x96_crc_present = 0;
1630 s->x96_nchannels = s->nchannels;
1632 if ((ret = alloc_x96_sample_buffer(s)) < 0)
1635 if ((ret = parse_x96_frame_data(s, 0, 0)) < 0)
1638 // Seek to the end of core frame
1639 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1640 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame\n");
1641 return AVERROR_INVALIDDATA;
1647 static int parse_x96_frame_exss(DCACoreDecoder *s)
1649 int x96_frame_size[DCA_EXSS_CHSETS_MAX];
1650 int x96_nchannels[DCA_EXSS_CHSETS_MAX];
1651 int x96_nchsets, x96_base_ch;
1652 int i, ret, header_size, header_pos = get_bits_count(&s->gb);
1655 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_X96) {
1656 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 sync word\n");
1657 return AVERROR_INVALIDDATA;
1660 // X96 frame header length
1661 header_size = get_bits(&s->gb, 6) + 1;
1663 // Check X96 frame header CRC
1664 if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) {
1665 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 frame header checksum\n");
1666 return AVERROR_INVALIDDATA;
1670 s->x96_rev_no = get_bits(&s->gb, 4);
1671 if (s->x96_rev_no < 1 || s->x96_rev_no > 8) {
1672 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no);
1673 return AVERROR_INVALIDDATA;
1676 // CRC presence flag for channel set header
1677 s->x96_crc_present = get_bits1(&s->gb);
1679 // Number of channel sets
1680 x96_nchsets = get_bits(&s->gb, 2) + 1;
1682 // Channel set data byte size
1683 for (i = 0; i < x96_nchsets; i++)
1684 x96_frame_size[i] = get_bits(&s->gb, 12) + 1;
1686 // Number of channels in channel set
1687 for (i = 0; i < x96_nchsets; i++)
1688 x96_nchannels[i] = get_bits(&s->gb, 3) + 1;
1692 // CRC16 of X96 frame header
1693 if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1694 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame header\n");
1695 return AVERROR_INVALIDDATA;
1698 if ((ret = alloc_x96_sample_buffer(s)) < 0)
1702 s->x96_nchannels = 0;
1703 for (i = 0, x96_base_ch = 0; i < x96_nchsets; i++) {
1704 header_pos = get_bits_count(&s->gb);
1706 if (x96_base_ch + x96_nchannels[i] <= s->nchannels) {
1707 s->x96_nchannels = x96_base_ch + x96_nchannels[i];
1708 if ((ret = parse_x96_frame_data(s, 1, x96_base_ch)) < 0)
1712 x96_base_ch += x96_nchannels[i];
1714 if (ff_dca_seek_bits(&s->gb, header_pos + x96_frame_size[i] * 8)) {
1715 av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set\n");
1716 return AVERROR_INVALIDDATA;
1723 static int parse_aux_data(DCACoreDecoder *s)
1727 if (get_bits_left(&s->gb) < 0)
1728 return AVERROR_INVALIDDATA;
1730 // Auxiliary data byte count (can't be trusted)
1731 skip_bits(&s->gb, 6);
1734 skip_bits_long(&s->gb, -get_bits_count(&s->gb) & 31);
1736 // Auxiliary data sync word
1737 if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_REV1AUX) {
1738 av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data sync word\n");
1739 return AVERROR_INVALIDDATA;
1742 aux_pos = get_bits_count(&s->gb);
1744 // Auxiliary decode time stamp flag
1745 if (get_bits1(&s->gb))
1746 skip_bits_long(&s->gb, 47);
1748 // Auxiliary dynamic downmix flag
1749 if (s->prim_dmix_embedded = get_bits1(&s->gb)) {
1752 // Auxiliary primary channel downmix type
1753 s->prim_dmix_type = get_bits(&s->gb, 3);
1754 if (s->prim_dmix_type >= DCA_DMIX_TYPE_COUNT) {
1755 av_log(s->avctx, AV_LOG_ERROR, "Invalid primary channel set downmix type\n");
1756 return AVERROR_INVALIDDATA;
1759 // Size of downmix coefficients matrix
1760 m = ff_dca_dmix_primary_nch[s->prim_dmix_type];
1761 n = ff_dca_channels[s->audio_mode] + !!s->lfe_present;
1763 // Dynamic downmix code coefficients
1764 for (i = 0; i < m * n; i++) {
1765 int code = get_bits(&s->gb, 9);
1766 int sign = (code >> 8) - 1;
1767 unsigned int index = code & 0xff;
1768 if (index >= FF_DCA_DMIXTABLE_SIZE) {
1769 av_log(s->avctx, AV_LOG_ERROR, "Invalid downmix coefficient index\n");
1770 return AVERROR_INVALIDDATA;
1772 s->prim_dmix_coeff[i] = (ff_dca_dmixtable[index] ^ sign) - sign;
1777 skip_bits(&s->gb, -get_bits_count(&s->gb) & 7);
1779 // CRC16 of auxiliary data
1780 skip_bits(&s->gb, 16);
1783 if (ff_dca_check_crc(s->avctx, &s->gb, aux_pos, get_bits_count(&s->gb))) {
1784 av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data checksum\n");
1785 return AVERROR_INVALIDDATA;
1791 static int parse_optional_info(DCACoreDecoder *s)
1793 DCAContext *dca = s->avctx->priv_data;
1798 skip_bits_long(&s->gb, 32);
1801 if (s->aux_present && (ret = parse_aux_data(s)) < 0
1802 && (s->avctx->err_recognition & AV_EF_EXPLODE))
1806 s->prim_dmix_embedded = 0;
1809 if (s->ext_audio_present && !dca->core_only) {
1810 int sync_pos = FFMIN(s->frame_size / 4, s->gb.size_in_bits / 32) - 1;
1811 int last_pos = get_bits_count(&s->gb) / 32;
1814 // Search for extension sync words aligned on 4-byte boundary. Search
1815 // must be done backwards from the end of core frame to work around
1816 // sync word aliasing issues.
1817 switch (s->ext_audio_type) {
1819 if (dca->request_channel_layout)
1822 // The distance between XCH sync word and end of the core frame
1823 // must be equal to XCH frame size. Off by one error is allowed for
1824 // compatibility with legacy bitstreams. Minimum XCH frame size is
1825 // 96 bytes. AMODE and PCHS are further checked to reduce
1826 // probability of alias sync detection.
1827 for (; sync_pos >= last_pos; sync_pos--) {
1828 if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_XCH) {
1829 s->gb.index = (sync_pos + 1) * 32;
1830 size = get_bits(&s->gb, 10) + 1;
1831 dist = s->frame_size - sync_pos * 4;
1833 && (size == dist || size - 1 == dist)
1834 && get_bits(&s->gb, 7) == 0x08) {
1835 s->xch_pos = get_bits_count(&s->gb);
1842 av_log(s->avctx, AV_LOG_ERROR, "XCH sync word not found\n");
1843 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1844 return AVERROR_INVALIDDATA;
1849 // The distance between X96 sync word and end of the core frame
1850 // must be equal to X96 frame size. Minimum X96 frame size is 96
1852 for (; sync_pos >= last_pos; sync_pos--) {
1853 if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_X96) {
1854 s->gb.index = (sync_pos + 1) * 32;
1855 size = get_bits(&s->gb, 12) + 1;
1856 dist = s->frame_size - sync_pos * 4;
1857 if (size >= 96 && size == dist) {
1858 s->x96_pos = get_bits_count(&s->gb);
1865 av_log(s->avctx, AV_LOG_ERROR, "X96 sync word not found\n");
1866 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1867 return AVERROR_INVALIDDATA;
1871 case EXT_AUDIO_XXCH:
1872 if (dca->request_channel_layout)
1875 // XXCH frame header CRC must be valid. Minimum XXCH frame header
1876 // size is 11 bytes.
1877 for (; sync_pos >= last_pos; sync_pos--) {
1878 if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_XXCH) {
1879 s->gb.index = (sync_pos + 1) * 32;
1880 size = get_bits(&s->gb, 6) + 1;
1881 dist = s->gb.size_in_bits / 8 - sync_pos * 4;
1882 if (size >= 11 && size <= dist &&
1883 !av_crc(dca->crctab, 0xffff, s->gb.buffer +
1884 (sync_pos + 1) * 4, size - 4)) {
1885 s->xxch_pos = sync_pos * 32;
1892 av_log(s->avctx, AV_LOG_ERROR, "XXCH sync word not found\n");
1893 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1894 return AVERROR_INVALIDDATA;
1903 int ff_dca_core_parse(DCACoreDecoder *s, uint8_t *data, int size)
1907 s->ext_audio_mask = 0;
1908 s->xch_pos = s->xxch_pos = s->x96_pos = 0;
1910 if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
1913 skip_bits_long(&s->gb, 32);
1914 if ((ret = parse_frame_header(s)) < 0)
1916 if ((ret = alloc_sample_buffer(s)) < 0)
1918 if ((ret = parse_frame_data(s, HEADER_CORE, 0)) < 0)
1920 if ((ret = parse_optional_info(s)) < 0)
1923 // Workaround for DTS in WAV
1924 if (s->frame_size > size && s->frame_size < size + 4) {
1925 av_log(s->avctx, AV_LOG_DEBUG, "Working around excessive core frame size (%d > %d)\n", s->frame_size, size);
1926 s->frame_size = size;
1929 if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1930 av_log(s->avctx, AV_LOG_ERROR, "Read past end of core frame\n");
1931 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1932 return AVERROR_INVALIDDATA;
1938 int ff_dca_core_parse_exss(DCACoreDecoder *s, uint8_t *data, DCAExssAsset *asset)
1940 AVCodecContext *avctx = s->avctx;
1941 DCAContext *dca = avctx->priv_data;
1942 GetBitContext gb = s->gb;
1943 int exss_mask = asset ? asset->extension_mask : 0;
1944 int ret = 0, ext = 0;
1946 // Parse (X)XCH unless downmixing
1947 if (!dca->request_channel_layout) {
1948 if (exss_mask & DCA_EXSS_XXCH) {
1949 if ((ret = init_get_bits8(&s->gb, data + asset->xxch_offset, asset->xxch_size)) < 0)
1951 ret = parse_xxch_frame(s);
1952 ext = DCA_EXSS_XXCH;
1953 } else if (s->xxch_pos) {
1954 s->gb.index = s->xxch_pos;
1955 ret = parse_xxch_frame(s);
1957 } else if (s->xch_pos) {
1958 s->gb.index = s->xch_pos;
1959 ret = parse_xch_frame(s);
1963 // Revert to primary channel set in case (X)XCH parsing fails
1965 if (avctx->err_recognition & AV_EF_EXPLODE)
1967 s->nchannels = ff_dca_channels[s->audio_mode];
1968 s->ch_mask = audio_mode_ch_mask[s->audio_mode];
1970 s->ch_mask |= DCA_SPEAKER_MASK_LFE1;
1972 s->ext_audio_mask |= ext;
1977 if (exss_mask & DCA_EXSS_XBR) {
1978 if ((ret = init_get_bits8(&s->gb, data + asset->xbr_offset, asset->xbr_size)) < 0)
1980 if ((ret = parse_xbr_frame(s)) < 0) {
1981 if (avctx->err_recognition & AV_EF_EXPLODE)
1984 s->ext_audio_mask |= DCA_EXSS_XBR;
1988 // Parse X96 unless decoding XLL
1989 if (!(dca->packet & DCA_PACKET_XLL)) {
1990 if (exss_mask & DCA_EXSS_X96) {
1991 if ((ret = init_get_bits8(&s->gb, data + asset->x96_offset, asset->x96_size)) < 0)
1993 if ((ret = parse_x96_frame_exss(s)) < 0) {
1994 if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
1997 s->ext_audio_mask |= DCA_EXSS_X96;
1999 } else if (s->x96_pos) {
2001 s->gb.index = s->x96_pos;
2002 if ((ret = parse_x96_frame(s)) < 0) {
2003 if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
2006 s->ext_audio_mask |= DCA_CSS_X96;
2014 static int map_prm_ch_to_spkr(DCACoreDecoder *s, int ch)
2018 // Try to map this channel to core first
2019 pos = ff_dca_channels[s->audio_mode];
2021 spkr = prm_ch_to_spkr_map[s->audio_mode][ch];
2022 if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) {
2023 if (s->xxch_core_mask & (1U << spkr))
2025 if (spkr == DCA_SPEAKER_Ls && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss))
2026 return DCA_SPEAKER_Lss;
2027 if (spkr == DCA_SPEAKER_Rs && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss))
2028 return DCA_SPEAKER_Rss;
2035 if ((s->ext_audio_mask & DCA_CSS_XCH) && ch == pos)
2036 return DCA_SPEAKER_Cs;
2039 if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) {
2040 for (spkr = DCA_SPEAKER_Cs; spkr < s->xxch_mask_nbits; spkr++)
2041 if (s->xxch_spkr_mask & (1U << spkr))
2050 static void erase_dsp_history(DCACoreDecoder *s)
2052 memset(s->dcadsp_data, 0, sizeof(s->dcadsp_data));
2053 s->output_history_lfe_fixed = 0;
2054 s->output_history_lfe_float = 0;
2057 static void set_filter_mode(DCACoreDecoder *s, int mode)
2059 if (s->filter_mode != mode) {
2060 erase_dsp_history(s);
2061 s->filter_mode = mode;
2065 int ff_dca_core_filter_fixed(DCACoreDecoder *s, int x96_synth)
2067 int n, ch, spkr, nsamples, x96_nchannels = 0;
2068 const int32_t *filter_coeff;
2071 // Externally set x96_synth flag implies that X96 synthesis should be
2072 // enabled, yet actual X96 subband data should be discarded. This is a
2073 // special case for lossless residual decoder that ignores X96 data if
2075 if (!x96_synth && (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96))) {
2076 x96_nchannels = s->x96_nchannels;
2082 s->output_rate = s->sample_rate << x96_synth;
2083 s->npcmsamples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth;
2085 // Reallocate PCM output buffer
2086 av_fast_malloc(&s->output_buffer, &s->output_size,
2087 nsamples * av_popcount(s->ch_mask) * sizeof(int32_t));
2088 if (!s->output_buffer)
2089 return AVERROR(ENOMEM);
2091 ptr = (int32_t *)s->output_buffer;
2092 for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) {
2093 if (s->ch_mask & (1U << spkr)) {
2094 s->output_samples[spkr] = ptr;
2097 s->output_samples[spkr] = NULL;
2101 // Handle change of filtering mode
2102 set_filter_mode(s, x96_synth | DCA_FILTER_MODE_FIXED);
2106 filter_coeff = ff_dca_fir_64bands_fixed;
2107 else if (s->filter_perfect)
2108 filter_coeff = ff_dca_fir_32bands_perfect_fixed;
2110 filter_coeff = ff_dca_fir_32bands_nonperfect_fixed;
2112 // Filter primary channels
2113 for (ch = 0; ch < s->nchannels; ch++) {
2114 // Map this primary channel to speaker
2115 spkr = map_prm_ch_to_spkr(s, ch);
2117 return AVERROR(EINVAL);
2119 // Filter bank reconstruction
2120 s->dcadsp->sub_qmf_fixed[x96_synth](
2123 s->output_samples[spkr],
2124 s->subband_samples[ch],
2125 ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL,
2126 s->dcadsp_data[ch].u.fix.hist1,
2127 &s->dcadsp_data[ch].offset,
2128 s->dcadsp_data[ch].u.fix.hist2,
2133 // Filter LFE channel
2134 if (s->lfe_present) {
2135 int32_t *samples = s->output_samples[DCA_SPEAKER_LFE1];
2136 int nlfesamples = s->npcmblocks >> 1;
2139 if (s->lfe_present == LFE_FLAG_128) {
2140 av_log(s->avctx, AV_LOG_ERROR, "Fixed point mode doesn't support LFF=1\n");
2141 return AVERROR(EINVAL);
2144 // Offset intermediate buffer for X96
2146 samples += nsamples / 2;
2148 // Interpolate LFE channel
2149 s->dcadsp->lfe_fir_fixed(samples, s->lfe_samples + DCA_LFE_HISTORY,
2150 ff_dca_lfe_fir_64_fixed, s->npcmblocks);
2153 // Filter 96 kHz oversampled LFE PCM to attenuate high frequency
2154 // (47.6 - 48.0 kHz) components of interpolation image
2155 s->dcadsp->lfe_x96_fixed(s->output_samples[DCA_SPEAKER_LFE1],
2156 samples, &s->output_history_lfe_fixed,
2161 // Update LFE history
2162 for (n = DCA_LFE_HISTORY - 1; n >= 0; n--)
2163 s->lfe_samples[n] = s->lfe_samples[nlfesamples + n];
2169 static int filter_frame_fixed(DCACoreDecoder *s, AVFrame *frame)
2171 AVCodecContext *avctx = s->avctx;
2172 DCAContext *dca = avctx->priv_data;
2173 int i, n, ch, ret, spkr, nsamples;
2175 // Don't filter twice when falling back from XLL
2176 if (!(dca->packet & DCA_PACKET_XLL) && (ret = ff_dca_core_filter_fixed(s, 0)) < 0)
2179 avctx->sample_rate = s->output_rate;
2180 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
2181 avctx->bits_per_raw_sample = 24;
2183 frame->nb_samples = nsamples = s->npcmsamples;
2184 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2187 // Undo embedded XCH downmix
2188 if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
2189 && s->audio_mode >= AMODE_2F2R) {
2190 s->dcadsp->dmix_sub_xch(s->output_samples[DCA_SPEAKER_Ls],
2191 s->output_samples[DCA_SPEAKER_Rs],
2192 s->output_samples[DCA_SPEAKER_Cs],
2197 // Undo embedded XXCH downmix
2198 if ((s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH))
2199 && s->xxch_dmix_embedded) {
2200 int scale_inv = s->xxch_dmix_scale_inv;
2201 int *coeff_ptr = s->xxch_dmix_coeff;
2202 int xch_base = ff_dca_channels[s->audio_mode];
2203 av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX);
2205 // Undo embedded core downmix pre-scaling
2206 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2207 if (s->xxch_core_mask & (1U << spkr)) {
2208 s->dcadsp->dmix_scale_inv(s->output_samples[spkr],
2209 scale_inv, nsamples);
2214 for (ch = xch_base; ch < s->nchannels; ch++) {
2215 int src_spkr = map_prm_ch_to_spkr(s, ch);
2217 return AVERROR(EINVAL);
2218 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2219 if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) {
2220 int coeff = mul16(*coeff_ptr++, scale_inv);
2222 s->dcadsp->dmix_sub(s->output_samples[spkr ],
2223 s->output_samples[src_spkr],
2231 if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) {
2232 // Front sum/difference decoding
2233 if ((s->sumdiff_front && s->audio_mode > AMODE_MONO)
2234 || s->audio_mode == AMODE_STEREO_SUMDIFF) {
2235 s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_L],
2236 s->output_samples[DCA_SPEAKER_R],
2240 // Surround sum/difference decoding
2241 if (s->sumdiff_surround && s->audio_mode >= AMODE_2F2R) {
2242 s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_Ls],
2243 s->output_samples[DCA_SPEAKER_Rs],
2248 // Downmix primary channel set to stereo
2249 if (s->request_mask != s->ch_mask) {
2250 ff_dca_downmix_to_stereo_fixed(s->dcadsp,
2253 nsamples, s->ch_mask);
2256 for (i = 0; i < avctx->channels; i++) {
2257 int32_t *samples = s->output_samples[s->ch_remap[i]];
2258 int32_t *plane = (int32_t *)frame->extended_data[i];
2259 for (n = 0; n < nsamples; n++)
2260 plane[n] = clip23(samples[n]) * (1 << 8);
2266 static int filter_frame_float(DCACoreDecoder *s, AVFrame *frame)
2268 AVCodecContext *avctx = s->avctx;
2269 int x96_nchannels = 0, x96_synth = 0;
2270 int i, n, ch, ret, spkr, nsamples, nchannels;
2271 float *output_samples[DCA_SPEAKER_COUNT] = { NULL }, *ptr;
2272 const float *filter_coeff;
2274 if (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96)) {
2275 x96_nchannels = s->x96_nchannels;
2279 avctx->sample_rate = s->sample_rate << x96_synth;
2280 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2281 avctx->bits_per_raw_sample = 0;
2283 frame->nb_samples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth;
2284 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2287 // Build reverse speaker to channel mapping
2288 for (i = 0; i < avctx->channels; i++)
2289 output_samples[s->ch_remap[i]] = (float *)frame->extended_data[i];
2291 // Allocate space for extra channels
2292 nchannels = av_popcount(s->ch_mask) - avctx->channels;
2293 if (nchannels > 0) {
2294 av_fast_malloc(&s->output_buffer, &s->output_size,
2295 nsamples * nchannels * sizeof(float));
2296 if (!s->output_buffer)
2297 return AVERROR(ENOMEM);
2299 ptr = (float *)s->output_buffer;
2300 for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) {
2301 if (!(s->ch_mask & (1U << spkr)))
2303 if (output_samples[spkr])
2305 output_samples[spkr] = ptr;
2310 // Handle change of filtering mode
2311 set_filter_mode(s, x96_synth);
2315 filter_coeff = ff_dca_fir_64bands;
2316 else if (s->filter_perfect)
2317 filter_coeff = ff_dca_fir_32bands_perfect;
2319 filter_coeff = ff_dca_fir_32bands_nonperfect;
2321 // Filter primary channels
2322 for (ch = 0; ch < s->nchannels; ch++) {
2323 // Map this primary channel to speaker
2324 spkr = map_prm_ch_to_spkr(s, ch);
2326 return AVERROR(EINVAL);
2328 // Filter bank reconstruction
2329 s->dcadsp->sub_qmf_float[x96_synth](
2331 &s->imdct[x96_synth],
2332 output_samples[spkr],
2333 s->subband_samples[ch],
2334 ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL,
2335 s->dcadsp_data[ch].u.flt.hist1,
2336 &s->dcadsp_data[ch].offset,
2337 s->dcadsp_data[ch].u.flt.hist2,
2340 1.0f / (1 << (17 - x96_synth)));
2343 // Filter LFE channel
2344 if (s->lfe_present) {
2345 int dec_select = (s->lfe_present == LFE_FLAG_128);
2346 float *samples = output_samples[DCA_SPEAKER_LFE1];
2347 int nlfesamples = s->npcmblocks >> (dec_select + 1);
2349 // Offset intermediate buffer for X96
2351 samples += nsamples / 2;
2355 filter_coeff = ff_dca_lfe_fir_128;
2357 filter_coeff = ff_dca_lfe_fir_64;
2359 // Interpolate LFE channel
2360 s->dcadsp->lfe_fir_float[dec_select](
2361 samples, s->lfe_samples + DCA_LFE_HISTORY,
2362 filter_coeff, s->npcmblocks);
2365 // Filter 96 kHz oversampled LFE PCM to attenuate high frequency
2366 // (47.6 - 48.0 kHz) components of interpolation image
2367 s->dcadsp->lfe_x96_float(output_samples[DCA_SPEAKER_LFE1],
2368 samples, &s->output_history_lfe_float,
2372 // Update LFE history
2373 for (n = DCA_LFE_HISTORY - 1; n >= 0; n--)
2374 s->lfe_samples[n] = s->lfe_samples[nlfesamples + n];
2377 // Undo embedded XCH downmix
2378 if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
2379 && s->audio_mode >= AMODE_2F2R) {
2380 s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Ls],
2381 output_samples[DCA_SPEAKER_Cs],
2382 -M_SQRT1_2, nsamples);
2383 s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Rs],
2384 output_samples[DCA_SPEAKER_Cs],
2385 -M_SQRT1_2, nsamples);
2388 // Undo embedded XXCH downmix
2389 if ((s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH))
2390 && s->xxch_dmix_embedded) {
2391 float scale_inv = s->xxch_dmix_scale_inv * (1.0f / (1 << 16));
2392 int *coeff_ptr = s->xxch_dmix_coeff;
2393 int xch_base = ff_dca_channels[s->audio_mode];
2394 av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX);
2397 for (ch = xch_base; ch < s->nchannels; ch++) {
2398 int src_spkr = map_prm_ch_to_spkr(s, ch);
2400 return AVERROR(EINVAL);
2401 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2402 if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) {
2403 int coeff = *coeff_ptr++;
2405 s->float_dsp->vector_fmac_scalar(output_samples[ spkr],
2406 output_samples[src_spkr],
2407 coeff * (-1.0f / (1 << 15)),
2414 // Undo embedded core downmix pre-scaling
2415 for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2416 if (s->xxch_core_mask & (1U << spkr)) {
2417 s->float_dsp->vector_fmul_scalar(output_samples[spkr],
2418 output_samples[spkr],
2419 scale_inv, nsamples);
2424 if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) {
2425 // Front sum/difference decoding
2426 if ((s->sumdiff_front && s->audio_mode > AMODE_MONO)
2427 || s->audio_mode == AMODE_STEREO_SUMDIFF) {
2428 s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_L],
2429 output_samples[DCA_SPEAKER_R],
2433 // Surround sum/difference decoding
2434 if (s->sumdiff_surround && s->audio_mode >= AMODE_2F2R) {
2435 s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_Ls],
2436 output_samples[DCA_SPEAKER_Rs],
2441 // Downmix primary channel set to stereo
2442 if (s->request_mask != s->ch_mask) {
2443 ff_dca_downmix_to_stereo_float(s->float_dsp, output_samples,
2445 nsamples, s->ch_mask);
2451 int ff_dca_core_filter_frame(DCACoreDecoder *s, AVFrame *frame)
2453 AVCodecContext *avctx = s->avctx;
2454 DCAContext *dca = avctx->priv_data;
2455 DCAExssAsset *asset = &dca->exss.assets[0];
2456 enum AVMatrixEncoding matrix_encoding;
2459 // Handle downmixing to stereo request
2460 if (dca->request_channel_layout == DCA_SPEAKER_LAYOUT_STEREO
2461 && s->audio_mode > AMODE_MONO && s->prim_dmix_embedded
2462 && (s->prim_dmix_type == DCA_DMIX_TYPE_LoRo ||
2463 s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
2464 s->request_mask = DCA_SPEAKER_LAYOUT_STEREO;
2466 s->request_mask = s->ch_mask;
2467 if (!ff_dca_set_channel_layout(avctx, s->ch_remap, s->request_mask))
2468 return AVERROR(EINVAL);
2470 // Force fixed point mode when falling back from XLL
2471 if ((avctx->flags & AV_CODEC_FLAG_BITEXACT) || ((dca->packet & DCA_PACKET_EXSS)
2472 && (asset->extension_mask & DCA_EXSS_XLL)))
2473 ret = filter_frame_fixed(s, frame);
2475 ret = filter_frame_float(s, frame);
2479 // Set profile, bit rate, etc
2480 if (s->ext_audio_mask & DCA_EXSS_MASK)
2481 avctx->profile = FF_PROFILE_DTS_HD_HRA;
2482 else if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH))
2483 avctx->profile = FF_PROFILE_DTS_ES;
2484 else if (s->ext_audio_mask & DCA_CSS_X96)
2485 avctx->profile = FF_PROFILE_DTS_96_24;
2487 avctx->profile = FF_PROFILE_DTS;
2489 if (s->bit_rate > 3 && !(s->ext_audio_mask & DCA_EXSS_MASK))
2490 avctx->bit_rate = s->bit_rate;
2492 avctx->bit_rate = 0;
2494 if (s->audio_mode == AMODE_STEREO_TOTAL || (s->request_mask != s->ch_mask &&
2495 s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
2496 matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
2498 matrix_encoding = AV_MATRIX_ENCODING_NONE;
2499 if ((ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
2505 av_cold void ff_dca_core_flush(DCACoreDecoder *s)
2507 if (s->subband_buffer) {
2508 erase_adpcm_history(s);
2509 memset(s->lfe_samples, 0, DCA_LFE_HISTORY * sizeof(int32_t));
2512 if (s->x96_subband_buffer)
2513 erase_x96_adpcm_history(s);
2515 erase_dsp_history(s);
2518 av_cold int ff_dca_core_init(DCACoreDecoder *s)
2520 if (!(s->float_dsp = avpriv_float_dsp_alloc(0)))
2522 if (!(s->fixed_dsp = avpriv_alloc_fixed_dsp(0)))
2525 ff_dcadct_init(&s->dcadct);
2526 if (ff_mdct_init(&s->imdct[0], 6, 1, 1.0) < 0)
2528 if (ff_mdct_init(&s->imdct[1], 7, 1, 1.0) < 0)
2530 ff_synth_filter_init(&s->synth);
2536 av_cold void ff_dca_core_close(DCACoreDecoder *s)
2538 av_freep(&s->float_dsp);
2539 av_freep(&s->fixed_dsp);
2541 ff_mdct_end(&s->imdct[0]);
2542 ff_mdct_end(&s->imdct[1]);
2544 av_freep(&s->subband_buffer);
2545 s->subband_size = 0;
2547 av_freep(&s->x96_subband_buffer);
2548 s->x96_subband_size = 0;
2550 av_freep(&s->output_buffer);