]> git.sesse.net Git - ffmpeg/blob - libavcodec/dca_core.c
avcodec/dca: fix sync word search error condition
[ffmpeg] / libavcodec / dca_core.c
1 /*
2  * Copyright (C) 2016 foo86
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "dcadec.h"
22 #include "dcadata.h"
23 #include "dcahuff.h"
24 #include "dcamath.h"
25 #include "dca_syncwords.h"
26
27 #if ARCH_ARM
28 #include "arm/dca.h"
29 #endif
30
31 enum HeaderType {
32     HEADER_CORE,
33     HEADER_XCH,
34     HEADER_XXCH
35 };
36
37 enum AudioMode {
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
48
49     AMODE_COUNT
50 };
51
52 enum ExtAudioType {
53     EXT_AUDIO_XCH   = 0,
54     EXT_AUDIO_X96   = 2,
55     EXT_AUDIO_XXCH  = 6
56 };
57
58 enum LFEFlag {
59     LFE_FLAG_NONE,
60     LFE_FLAG_128,
61     LFE_FLAG_64,
62     LFE_FLAG_INVALID
63 };
64
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 }
76 };
77
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
89 };
90
91 static const uint8_t block_code_nbits[7] = {
92     7, 10, 12, 13, 15, 17, 19
93 };
94
95 static const uint8_t quant_index_sel_nbits[DCA_CODE_BOOKS] = {
96     1, 2, 2, 2, 2, 3, 3, 3, 3, 3
97 };
98
99 static const uint8_t quant_index_group_size[DCA_CODE_BOOKS] = {
100     1, 3, 3, 3, 3, 7, 7, 7, 7, 7
101 };
102
103 static int dca_get_vlc(GetBitContext *s, DCAVLC *v, int i)
104 {
105     return get_vlc2(s, v->vlc[i].table, v->vlc[i].bits, v->max_depth) + v->offset;
106 }
107
108 static void get_array(GetBitContext *s, int32_t *array, int size, int n)
109 {
110     int i;
111
112     for (i = 0; i < size; i++)
113         array[i] = get_sbits(s, n);
114 }
115
116 // 5.3.1 - Bit stream header
117 static int parse_frame_header(DCACoreDecoder *s)
118 {
119     int normal_frame, pcmr_index;
120
121     // Frame type
122     normal_frame = get_bits1(&s->gb);
123
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;
128     }
129
130     // CRC present flag
131     s->crc_present = get_bits1(&s->gb);
132
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;
138     }
139
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;
145     }
146
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;
152     }
153
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;
159     }
160
161     // Transmission bit rate
162     s->bit_rate = ff_dca_bit_rates[get_bits(&s->gb, 5)];
163
164     // Reserved field
165     skip_bits1(&s->gb);
166
167     // Embedded dynamic range flag
168     s->drc_present = get_bits1(&s->gb);
169
170     // Embedded time stamp flag
171     s->ts_present = get_bits1(&s->gb);
172
173     // Auxiliary data flag
174     s->aux_present = get_bits1(&s->gb);
175
176     // HDCD mastering flag
177     skip_bits1(&s->gb);
178
179     // Extension audio descriptor flag
180     s->ext_audio_type = get_bits(&s->gb, 3);
181
182     // Extended coding flag
183     s->ext_audio_present = get_bits1(&s->gb);
184
185     // Audio sync word insertion flag
186     s->sync_ssf = get_bits1(&s->gb);
187
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;
193     }
194
195     // Predictor history flag switch
196     s->predictor_history = get_bits1(&s->gb);
197
198     // Header CRC check bytes
199     if (s->crc_present)
200         skip_bits(&s->gb, 16);
201
202     // Multirate interpolator switch
203     s->filter_perfect = get_bits1(&s->gb);
204
205     // Encoder software revision
206     skip_bits(&s->gb, 4);
207
208     // Copy history
209     skip_bits(&s->gb, 2);
210
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;
216     }
217     s->es_format = pcmr_index & 1;
218
219     // Front sum/difference flag
220     s->sumdiff_front = get_bits1(&s->gb);
221
222     // Surround sum/difference flag
223     s->sumdiff_surround = get_bits1(&s->gb);
224
225     // Dialog normalization / unspecified
226     skip_bits(&s->gb, 4);
227
228     return 0;
229 }
230
231 // 5.3.2 - Primary audio coding header
232 static int parse_coding_header(DCACoreDecoder *s, enum HeaderType header, int xch_base)
233 {
234     int n, ch, nchannels, header_size = 0, header_pos = get_bits_count(&s->gb);
235     unsigned int mask, index;
236
237     if (get_bits_left(&s->gb) < 0)
238         return AVERROR_INVALIDDATA;
239
240     switch (header) {
241     case HEADER_CORE:
242         // Number of subframes
243         s->nsubframes = get_bits(&s->gb, 4) + 1;
244
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;
250         }
251         av_assert1(s->nchannels <= DCA_CHANNELS - 2);
252
253         s->ch_mask = audio_mode_ch_mask[s->audio_mode];
254
255         // Add LFE channel if present
256         if (s->lfe_present)
257             s->ch_mask |= DCA_SPEAKER_MASK_LFE1;
258         break;
259
260     case HEADER_XCH:
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;
264         break;
265
266     case HEADER_XXCH:
267         // Channel set header length
268         header_size = get_bits(&s->gb, 7) + 1;
269
270         // Check CRC
271         if (s->xxch_crc_present
272             && (s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
273             && ff_dca_check_crc(&s->gb, header_pos, header_pos + header_size * 8)) {
274             av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH channel set header checksum\n");
275             return AVERROR_INVALIDDATA;
276         }
277
278         // Number of channels in a channel set
279         nchannels = get_bits(&s->gb, 3) + 1;
280         if (nchannels > DCA_XXCH_CHANNELS_MAX) {
281             avpriv_request_sample(s->avctx, "%d XXCH channels", nchannels);
282             return AVERROR_PATCHWELCOME;
283         }
284         s->nchannels = ff_dca_channels[s->audio_mode] + nchannels;
285         av_assert1(s->nchannels <= DCA_CHANNELS);
286
287         // Loudspeaker layout mask
288         mask = get_bits_long(&s->gb, s->xxch_mask_nbits - DCA_SPEAKER_Cs);
289         s->xxch_spkr_mask = mask << DCA_SPEAKER_Cs;
290
291         if (av_popcount(s->xxch_spkr_mask) != nchannels) {
292             av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH speaker layout mask (%#x)\n", s->xxch_spkr_mask);
293             return AVERROR_INVALIDDATA;
294         }
295
296         if (s->xxch_core_mask & s->xxch_spkr_mask) {
297             av_log(s->avctx, AV_LOG_ERROR, "XXCH speaker layout mask (%#x) overlaps with core (%#x)\n", s->xxch_spkr_mask, s->xxch_core_mask);
298             return AVERROR_INVALIDDATA;
299         }
300
301         // Combine core and XXCH masks together
302         s->ch_mask = s->xxch_core_mask | s->xxch_spkr_mask;
303
304         // Downmix coefficients present in stream
305         if (get_bits1(&s->gb)) {
306             int *coeff_ptr = s->xxch_dmix_coeff;
307
308             // Downmix already performed by encoder
309             s->xxch_dmix_embedded = get_bits1(&s->gb);
310
311             // Downmix scale factor
312             index = get_bits(&s->gb, 6) * 4 - FF_DCA_DMIXTABLE_OFFSET - 3;
313             if (index >= FF_DCA_INV_DMIXTABLE_SIZE) {
314                 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix scale index (%d)\n", index);
315                 return AVERROR_INVALIDDATA;
316             }
317             s->xxch_dmix_scale_inv = ff_dca_inv_dmixtable[index];
318
319             // Downmix channel mapping mask
320             for (ch = 0; ch < nchannels; ch++) {
321                 mask = get_bits_long(&s->gb, s->xxch_mask_nbits);
322                 if ((mask & s->xxch_core_mask) != mask) {
323                     av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix channel mapping mask (%#x)\n", mask);
324                     return AVERROR_INVALIDDATA;
325                 }
326                 s->xxch_dmix_mask[ch] = mask;
327             }
328
329             // Downmix coefficients
330             for (ch = 0; ch < nchannels; ch++) {
331                 for (n = 0; n < s->xxch_mask_nbits; n++) {
332                     if (s->xxch_dmix_mask[ch] & (1U << n)) {
333                         int code = get_bits(&s->gb, 7);
334                         int sign = (code >> 6) - 1;
335                         if (code &= 63) {
336                             index = code * 4 - 3;
337                             if (index >= FF_DCA_DMIXTABLE_SIZE) {
338                                 av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix coefficient index (%d)\n", index);
339                                 return AVERROR_INVALIDDATA;
340                             }
341                             *coeff_ptr++ = (ff_dca_dmixtable[index] ^ sign) - sign;
342                         } else {
343                             *coeff_ptr++ = 0;
344                         }
345                     }
346                 }
347             }
348         } else {
349             s->xxch_dmix_embedded = 0;
350         }
351
352         break;
353     }
354
355     // Subband activity count
356     for (ch = xch_base; ch < s->nchannels; ch++) {
357         s->nsubbands[ch] = get_bits(&s->gb, 5) + 2;
358         if (s->nsubbands[ch] > DCA_SUBBANDS) {
359             av_log(s->avctx, AV_LOG_ERROR, "Invalid subband activity count\n");
360             return AVERROR_INVALIDDATA;
361         }
362     }
363
364     // High frequency VQ start subband
365     for (ch = xch_base; ch < s->nchannels; ch++)
366         s->subband_vq_start[ch] = get_bits(&s->gb, 5) + 1;
367
368     // Joint intensity coding index
369     for (ch = xch_base; ch < s->nchannels; ch++) {
370         if ((n = get_bits(&s->gb, 3)) && header == HEADER_XXCH)
371             n += xch_base - 1;
372         if (n > s->nchannels) {
373             av_log(s->avctx, AV_LOG_ERROR, "Invalid joint intensity coding index\n");
374             return AVERROR_INVALIDDATA;
375         }
376         s->joint_intensity_index[ch] = n;
377     }
378
379     // Transient mode code book
380     for (ch = xch_base; ch < s->nchannels; ch++)
381         s->transition_mode_sel[ch] = get_bits(&s->gb, 2);
382
383     // Scale factor code book
384     for (ch = xch_base; ch < s->nchannels; ch++) {
385         s->scale_factor_sel[ch] = get_bits(&s->gb, 3);
386         if (s->scale_factor_sel[ch] == 7) {
387             av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor code book\n");
388             return AVERROR_INVALIDDATA;
389         }
390     }
391
392     // Bit allocation quantizer select
393     for (ch = xch_base; ch < s->nchannels; ch++) {
394         s->bit_allocation_sel[ch] = get_bits(&s->gb, 3);
395         if (s->bit_allocation_sel[ch] == 7) {
396             av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation quantizer select\n");
397             return AVERROR_INVALIDDATA;
398         }
399     }
400
401     // Quantization index codebook select
402     for (n = 0; n < DCA_CODE_BOOKS; n++)
403         for (ch = xch_base; ch < s->nchannels; ch++)
404             s->quant_index_sel[ch][n] = get_bits(&s->gb, quant_index_sel_nbits[n]);
405
406     // Scale factor adjustment index
407     for (n = 0; n < DCA_CODE_BOOKS; n++)
408         for (ch = xch_base; ch < s->nchannels; ch++)
409             if (s->quant_index_sel[ch][n] < quant_index_group_size[n])
410                 s->scale_factor_adj[ch][n] = ff_dca_scale_factor_adj[get_bits(&s->gb, 2)];
411
412     if (header == HEADER_XXCH) {
413         // Reserved
414         // Byte align
415         // CRC16 of channel set header
416         if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
417             av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set header\n");
418             return AVERROR_INVALIDDATA;
419         }
420     } else {
421         // Audio header CRC check word
422         if (s->crc_present)
423             skip_bits(&s->gb, 16);
424     }
425
426     return 0;
427 }
428
429 static inline int parse_scale(DCACoreDecoder *s, int *scale_index, int sel)
430 {
431     const uint32_t *scale_table;
432     unsigned int scale_size;
433
434     // Select the root square table
435     if (sel > 5) {
436         scale_table = ff_dca_scale_factor_quant7;
437         scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7);
438     } else {
439         scale_table = ff_dca_scale_factor_quant6;
440         scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6);
441     }
442
443     // If Huffman code was used, the difference of scales was encoded
444     if (sel < 5)
445         *scale_index += dca_get_vlc(&s->gb, &ff_dca_vlc_scale_factor, sel);
446     else
447         *scale_index = get_bits(&s->gb, sel + 1);
448
449     // Look up scale factor from the root square table
450     if ((unsigned int)*scale_index >= scale_size) {
451         av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor index\n");
452         return AVERROR_INVALIDDATA;
453     }
454
455     return scale_table[*scale_index];
456 }
457
458 static inline int parse_joint_scale(DCACoreDecoder *s, int sel)
459 {
460     int scale_index;
461
462     // Absolute value was encoded even when Huffman code was used
463     if (sel < 5)
464         scale_index = dca_get_vlc(&s->gb, &ff_dca_vlc_scale_factor, sel);
465     else
466         scale_index = get_bits(&s->gb, sel + 1);
467
468     // Bias by 64
469     scale_index += 64;
470
471     // Look up joint scale factor
472     if ((unsigned int)scale_index >= FF_ARRAY_ELEMS(ff_dca_joint_scale_factors)) {
473         av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor index\n");
474         return AVERROR_INVALIDDATA;
475     }
476
477     return ff_dca_joint_scale_factors[scale_index];
478 }
479
480 // 5.4.1 - Primary audio coding side information
481 static int parse_subframe_header(DCACoreDecoder *s, int sf,
482                                  enum HeaderType header, int xch_base)
483 {
484     int ch, band, ret;
485
486     if (get_bits_left(&s->gb) < 0)
487         return AVERROR_INVALIDDATA;
488
489     if (header == HEADER_CORE) {
490         // Subsubframe count
491         s->nsubsubframes[sf] = get_bits(&s->gb, 2) + 1;
492
493         // Partial subsubframe sample count
494         skip_bits(&s->gb, 3);
495     }
496
497     // Prediction mode
498     for (ch = xch_base; ch < s->nchannels; ch++)
499         for (band = 0; band < s->nsubbands[ch]; band++)
500             s->prediction_mode[ch][band] = get_bits1(&s->gb);
501
502     // Prediction coefficients VQ address
503     for (ch = xch_base; ch < s->nchannels; ch++)
504         for (band = 0; band < s->nsubbands[ch]; band++)
505             if (s->prediction_mode[ch][band])
506                 s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12);
507
508     // Bit allocation index
509     for (ch = xch_base; ch < s->nchannels; ch++) {
510         int sel = s->bit_allocation_sel[ch];
511
512         for (band = 0; band < s->subband_vq_start[ch]; band++) {
513             int abits;
514
515             if (sel < 5)
516                 abits = dca_get_vlc(&s->gb, &ff_dca_vlc_bit_allocation, sel);
517             else
518                 abits = get_bits(&s->gb, sel - 1);
519
520             if (abits > DCA_ABITS_MAX) {
521                 av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation index\n");
522                 return AVERROR_INVALIDDATA;
523             }
524
525             s->bit_allocation[ch][band] = abits;
526         }
527     }
528
529     // Transition mode
530     for (ch = xch_base; ch < s->nchannels; ch++) {
531         // Clear transition mode for all subbands
532         memset(s->transition_mode[sf][ch], 0, sizeof(s->transition_mode[0][0]));
533
534         // Transient possible only if more than one subsubframe
535         if (s->nsubsubframes[sf] > 1) {
536             int sel = s->transition_mode_sel[ch];
537             for (band = 0; band < s->subband_vq_start[ch]; band++)
538                 if (s->bit_allocation[ch][band])
539                     s->transition_mode[sf][ch][band] = dca_get_vlc(&s->gb, &ff_dca_vlc_transition_mode, sel);
540         }
541     }
542
543     // Scale factors
544     for (ch = xch_base; ch < s->nchannels; ch++) {
545         int sel = s->scale_factor_sel[ch];
546         int scale_index = 0;
547
548         // Extract scales for subbands up to VQ
549         for (band = 0; band < s->subband_vq_start[ch]; band++) {
550             if (s->bit_allocation[ch][band]) {
551                 if ((ret = parse_scale(s, &scale_index, sel)) < 0)
552                     return ret;
553                 s->scale_factors[ch][band][0] = ret;
554                 if (s->transition_mode[sf][ch][band]) {
555                     if ((ret = parse_scale(s, &scale_index, sel)) < 0)
556                         return ret;
557                     s->scale_factors[ch][band][1] = ret;
558                 }
559             } else {
560                 s->scale_factors[ch][band][0] = 0;
561             }
562         }
563
564         // High frequency VQ subbands
565         for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++) {
566             if ((ret = parse_scale(s, &scale_index, sel)) < 0)
567                 return ret;
568             s->scale_factors[ch][band][0] = ret;
569         }
570     }
571
572     // Joint subband codebook select
573     for (ch = xch_base; ch < s->nchannels; ch++) {
574         if (s->joint_intensity_index[ch]) {
575             s->joint_scale_sel[ch] = get_bits(&s->gb, 3);
576             if (s->joint_scale_sel[ch] == 7) {
577                 av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor code book\n");
578                 return AVERROR_INVALIDDATA;
579             }
580         }
581     }
582
583     // Scale factors for joint subband coding
584     for (ch = xch_base; ch < s->nchannels; ch++) {
585         int src_ch = s->joint_intensity_index[ch] - 1;
586         if (src_ch >= 0) {
587             int sel = s->joint_scale_sel[ch];
588             for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) {
589                 if ((ret = parse_joint_scale(s, sel)) < 0)
590                     return ret;
591                 s->joint_scale_factors[ch][band] = ret;
592             }
593         }
594     }
595
596     // Dynamic range coefficient
597     if (s->drc_present && header == HEADER_CORE)
598         skip_bits(&s->gb, 8);
599
600     // Side information CRC check word
601     if (s->crc_present)
602         skip_bits(&s->gb, 16);
603
604     return 0;
605 }
606
607 #ifndef decode_blockcodes
608 static inline int decode_blockcodes(int code1, int code2, int levels, int32_t *audio)
609 {
610     int offset = (levels - 1) / 2;
611     int n, div;
612
613     for (n = 0; n < DCA_SUBBAND_SAMPLES / 2; n++) {
614         div = FASTDIV(code1, levels);
615         audio[n] = code1 - div * levels - offset;
616         code1 = div;
617     }
618     for (; n < DCA_SUBBAND_SAMPLES; n++) {
619         div = FASTDIV(code2, levels);
620         audio[n] = code2 - div * levels - offset;
621         code2 = div;
622     }
623
624     return code1 | code2;
625 }
626 #endif
627
628 static inline int parse_block_codes(DCACoreDecoder *s, int32_t *audio, int abits)
629 {
630     // Extract block code indices from the bit stream
631     int code1 = get_bits(&s->gb, block_code_nbits[abits - 1]);
632     int code2 = get_bits(&s->gb, block_code_nbits[abits - 1]);
633     int levels = ff_dca_quant_levels[abits];
634
635     // Look up samples from the block code book
636     if (decode_blockcodes(code1, code2, levels, audio)) {
637         av_log(s->avctx, AV_LOG_ERROR, "Failed to decode block code(s)\n");
638         return AVERROR_INVALIDDATA;
639     }
640
641     return 0;
642 }
643
644 static inline int parse_huffman_codes(DCACoreDecoder *s, int32_t *audio, int abits, int sel)
645 {
646     int i;
647
648     // Extract Huffman codes from the bit stream
649     for (i = 0; i < DCA_SUBBAND_SAMPLES; i++)
650         audio[i] = dca_get_vlc(&s->gb, &ff_dca_vlc_quant_index[abits - 1], sel);
651
652     return 1;
653 }
654
655 static inline int extract_audio(DCACoreDecoder *s, int32_t *audio, int abits, int ch)
656 {
657     av_assert1(abits >= 0 && abits <= DCA_ABITS_MAX);
658
659     if (abits == 0) {
660         // No bits allocated
661         memset(audio, 0, DCA_SUBBAND_SAMPLES * sizeof(*audio));
662         return 0;
663     }
664
665     if (abits <= DCA_CODE_BOOKS) {
666         int sel = s->quant_index_sel[ch][abits - 1];
667         if (sel < quant_index_group_size[abits - 1]) {
668             // Huffman codes
669             return parse_huffman_codes(s, audio, abits, sel);
670         }
671         if (abits <= 7) {
672             // Block codes
673             return parse_block_codes(s, audio, abits);
674         }
675     }
676
677     // No further encoding
678     get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3);
679     return 0;
680 }
681
682 static inline void dequantize(int32_t *output, const int32_t *input,
683                               int32_t step_size, int32_t scale, int residual)
684 {
685     // Account for quantizer step size
686     int64_t step_scale = (int64_t)step_size * scale;
687     int n, shift = 0;
688
689     // Limit scale factor resolution to 22 bits
690     if (step_scale > (1 << 23)) {
691         shift = av_log2(step_scale >> 23) + 1;
692         step_scale >>= shift;
693     }
694
695     // Scale the samples
696     if (residual) {
697         for (n = 0; n < DCA_SUBBAND_SAMPLES; n++)
698             output[n] += clip23(norm__(input[n] * step_scale, 22 - shift));
699     } else {
700         for (n = 0; n < DCA_SUBBAND_SAMPLES; n++)
701             output[n]  = clip23(norm__(input[n] * step_scale, 22 - shift));
702     }
703 }
704
705 static inline void inverse_adpcm(int32_t **subband_samples,
706                                  const int16_t *vq_index,
707                                  const int8_t *prediction_mode,
708                                  int sb_start, int sb_end,
709                                  int ofs, int len)
710 {
711     int i, j, k;
712
713     for (i = sb_start; i < sb_end; i++) {
714         if (prediction_mode[i]) {
715             const int16_t *coeff = ff_dca_adpcm_vb[vq_index[i]];
716             int32_t *ptr = subband_samples[i] + ofs;
717             for (j = 0; j < len; j++) {
718                 int64_t err = 0;
719                 for (k = 0; k < DCA_ADPCM_COEFFS; k++)
720                     err += (int64_t)ptr[j - k - 1] * coeff[k];
721                 ptr[j] = clip23(ptr[j] + clip23(norm13(err)));
722             }
723         }
724     }
725 }
726
727 // 5.5 - Primary audio data arrays
728 static int parse_subframe_audio(DCACoreDecoder *s, int sf, enum HeaderType header,
729                                 int xch_base, int *sub_pos, int *lfe_pos)
730 {
731     int32_t audio[16], scale;
732     int n, ssf, ofs, ch, band;
733
734     // Check number of subband samples in this subframe
735     int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES;
736     if (*sub_pos + nsamples > s->npcmblocks) {
737         av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
738         return AVERROR_INVALIDDATA;
739     }
740
741     if (get_bits_left(&s->gb) < 0)
742         return AVERROR_INVALIDDATA;
743
744     // VQ encoded subbands
745     for (ch = xch_base; ch < s->nchannels; ch++) {
746         int32_t vq_index[DCA_SUBBANDS];
747
748         for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++)
749             // Extract the VQ address from the bit stream
750             vq_index[band] = get_bits(&s->gb, 10);
751
752         if (s->subband_vq_start[ch] < s->nsubbands[ch]) {
753             s->dcadsp->decode_hf(s->subband_samples[ch], vq_index,
754                                  ff_dca_high_freq_vq, s->scale_factors[ch],
755                                  s->subband_vq_start[ch], s->nsubbands[ch],
756                                  *sub_pos, nsamples);
757         }
758     }
759
760     // Low frequency effect data
761     if (s->lfe_present && header == HEADER_CORE) {
762         unsigned int index;
763
764         // Determine number of LFE samples in this subframe
765         int nlfesamples = 2 * s->lfe_present * s->nsubsubframes[sf];
766         av_assert1((unsigned int)nlfesamples <= FF_ARRAY_ELEMS(audio));
767
768         // Extract LFE samples from the bit stream
769         get_array(&s->gb, audio, nlfesamples, 8);
770
771         // Extract scale factor index from the bit stream
772         index = get_bits(&s->gb, 8);
773         if (index >= FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7)) {
774             av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE scale factor index\n");
775             return AVERROR_INVALIDDATA;
776         }
777
778         // Look up the 7-bit root square quantization table
779         scale = ff_dca_scale_factor_quant7[index];
780
781         // Account for quantizer step size which is 0.035
782         scale = mul23(4697620 /* 0.035 * (1 << 27) */, scale);
783
784         // Scale and take the LFE samples
785         for (n = 0, ofs = *lfe_pos; n < nlfesamples; n++, ofs++)
786             s->lfe_samples[ofs] = clip23(audio[n] * scale >> 4);
787
788         // Advance LFE sample pointer for the next subframe
789         *lfe_pos = ofs;
790     }
791
792     // Audio data
793     for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
794         for (ch = xch_base; ch < s->nchannels; ch++) {
795             if (get_bits_left(&s->gb) < 0)
796                 return AVERROR_INVALIDDATA;
797
798             // Not high frequency VQ subbands
799             for (band = 0; band < s->subband_vq_start[ch]; band++) {
800                 int ret, trans_ssf, abits = s->bit_allocation[ch][band];
801                 int32_t step_size;
802
803                 // Extract bits from the bit stream
804                 if ((ret = extract_audio(s, audio, abits, ch)) < 0)
805                     return ret;
806
807                 // Select quantization step size table and look up
808                 // quantization step size
809                 if (s->bit_rate == 3)
810                     step_size = ff_dca_lossless_quant[abits];
811                 else
812                     step_size = ff_dca_lossy_quant[abits];
813
814                 // Identify transient location
815                 trans_ssf = s->transition_mode[sf][ch][band];
816
817                 // Determine proper scale factor
818                 if (trans_ssf == 0 || ssf < trans_ssf)
819                     scale = s->scale_factors[ch][band][0];
820                 else
821                     scale = s->scale_factors[ch][band][1];
822
823                 // Adjust scale factor when SEL indicates Huffman code
824                 if (ret > 0) {
825                     int64_t adj = s->scale_factor_adj[ch][abits - 1];
826                     scale = clip23(adj * scale >> 22);
827                 }
828
829                 dequantize(s->subband_samples[ch][band] + ofs,
830                            audio, step_size, scale, 0);
831             }
832         }
833
834         // DSYNC
835         if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
836             av_log(s->avctx, AV_LOG_ERROR, "DSYNC check failed\n");
837             return AVERROR_INVALIDDATA;
838         }
839
840         ofs += DCA_SUBBAND_SAMPLES;
841     }
842
843     // Inverse ADPCM
844     for (ch = xch_base; ch < s->nchannels; ch++) {
845         inverse_adpcm(s->subband_samples[ch], s->prediction_vq_index[ch],
846                       s->prediction_mode[ch], 0, s->nsubbands[ch],
847                       *sub_pos, nsamples);
848     }
849
850     // Joint subband coding
851     for (ch = xch_base; ch < s->nchannels; ch++) {
852         int src_ch = s->joint_intensity_index[ch] - 1;
853         if (src_ch >= 0) {
854             s->dcadsp->decode_joint(s->subband_samples[ch], s->subband_samples[src_ch],
855                                     s->joint_scale_factors[ch], s->nsubbands[ch],
856                                     s->nsubbands[src_ch], *sub_pos, nsamples);
857         }
858     }
859
860     // Advance subband sample pointer for the next subframe
861     *sub_pos = ofs;
862     return 0;
863 }
864
865 static void erase_adpcm_history(DCACoreDecoder *s)
866 {
867     int ch, band;
868
869     // Erase ADPCM history from previous frame if
870     // predictor history switch was disabled
871     for (ch = 0; ch < DCA_CHANNELS; ch++)
872         for (band = 0; band < DCA_SUBBANDS; band++)
873             AV_ZERO128(s->subband_samples[ch][band] - DCA_ADPCM_COEFFS);
874
875     emms_c();
876 }
877
878 static int alloc_sample_buffer(DCACoreDecoder *s)
879 {
880     int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks;
881     int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS;
882     int nlfesamples = DCA_LFE_HISTORY + s->npcmblocks / 2;
883     unsigned int size = s->subband_size;
884     int ch, band;
885
886     // Reallocate subband sample buffer
887     av_fast_mallocz(&s->subband_buffer, &s->subband_size,
888                     (nframesamples + nlfesamples) * sizeof(int32_t));
889     if (!s->subband_buffer)
890         return AVERROR(ENOMEM);
891
892     if (size != s->subband_size) {
893         for (ch = 0; ch < DCA_CHANNELS; ch++)
894             for (band = 0; band < DCA_SUBBANDS; band++)
895                 s->subband_samples[ch][band] = s->subband_buffer +
896                     (ch * DCA_SUBBANDS + band) * nchsamples + DCA_ADPCM_COEFFS;
897         s->lfe_samples = s->subband_buffer + nframesamples;
898     }
899
900     if (!s->predictor_history)
901         erase_adpcm_history(s);
902
903     return 0;
904 }
905
906 static int parse_frame_data(DCACoreDecoder *s, enum HeaderType header, int xch_base)
907 {
908     int sf, ch, ret, band, sub_pos, lfe_pos;
909
910     if ((ret = parse_coding_header(s, header, xch_base)) < 0)
911         return ret;
912
913     for (sf = 0, sub_pos = 0, lfe_pos = DCA_LFE_HISTORY; sf < s->nsubframes; sf++) {
914         if ((ret = parse_subframe_header(s, sf, header, xch_base)) < 0)
915             return ret;
916         if ((ret = parse_subframe_audio(s, sf, header, xch_base, &sub_pos, &lfe_pos)) < 0)
917             return ret;
918     }
919
920     for (ch = xch_base; ch < s->nchannels; ch++) {
921         // Determine number of active subbands for this channel
922         int nsubbands = s->nsubbands[ch];
923         if (s->joint_intensity_index[ch])
924             nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]);
925
926         // Update history for ADPCM
927         for (band = 0; band < nsubbands; band++) {
928             int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS;
929             AV_COPY128(samples, samples + s->npcmblocks);
930         }
931
932         // Clear inactive subbands
933         for (; band < DCA_SUBBANDS; band++) {
934             int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS;
935             memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t));
936         }
937     }
938
939     emms_c();
940
941     return 0;
942 }
943
944 static int parse_xch_frame(DCACoreDecoder *s)
945 {
946     int ret;
947
948     if (s->ch_mask & DCA_SPEAKER_MASK_Cs) {
949         av_log(s->avctx, AV_LOG_ERROR, "XCH with Cs speaker already present\n");
950         return AVERROR_INVALIDDATA;
951     }
952
953     if ((ret = parse_frame_data(s, HEADER_XCH, s->nchannels)) < 0)
954         return ret;
955
956     // Seek to the end of core frame, don't trust XCH frame size
957     if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
958         av_log(s->avctx, AV_LOG_ERROR, "Read past end of XCH frame\n");
959         return AVERROR_INVALIDDATA;
960     }
961
962     return 0;
963 }
964
965 static int parse_xxch_frame(DCACoreDecoder *s)
966 {
967     int xxch_nchsets, xxch_frame_size;
968     int ret, mask, header_size, header_pos = get_bits_count(&s->gb);
969
970     // XXCH sync word
971     if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XXCH) {
972         av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH sync word\n");
973         return AVERROR_INVALIDDATA;
974     }
975
976     // XXCH frame header length
977     header_size = get_bits(&s->gb, 6) + 1;
978
979     // Check XXCH frame header CRC
980     if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
981         && ff_dca_check_crc(&s->gb, header_pos + 32, header_pos + header_size * 8)) {
982         av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH frame header checksum\n");
983         return AVERROR_INVALIDDATA;
984     }
985
986     // CRC presence flag for channel set header
987     s->xxch_crc_present = get_bits1(&s->gb);
988
989     // Number of bits for loudspeaker mask
990     s->xxch_mask_nbits = get_bits(&s->gb, 5) + 1;
991     if (s->xxch_mask_nbits <= DCA_SPEAKER_Cs) {
992         av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XXCH speaker mask (%d)\n", s->xxch_mask_nbits);
993         return AVERROR_INVALIDDATA;
994     }
995
996     // Number of channel sets
997     xxch_nchsets = get_bits(&s->gb, 2) + 1;
998     if (xxch_nchsets > 1) {
999         avpriv_request_sample(s->avctx, "%d XXCH channel sets", xxch_nchsets);
1000         return AVERROR_PATCHWELCOME;
1001     }
1002
1003     // Channel set 0 data byte size
1004     xxch_frame_size = get_bits(&s->gb, 14) + 1;
1005
1006     // Core loudspeaker activity mask
1007     s->xxch_core_mask = get_bits_long(&s->gb, s->xxch_mask_nbits);
1008
1009     // Validate the core mask
1010     mask = s->ch_mask;
1011
1012     if ((mask & DCA_SPEAKER_MASK_Ls) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss))
1013         mask = (mask & ~DCA_SPEAKER_MASK_Ls) | DCA_SPEAKER_MASK_Lss;
1014
1015     if ((mask & DCA_SPEAKER_MASK_Rs) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss))
1016         mask = (mask & ~DCA_SPEAKER_MASK_Rs) | DCA_SPEAKER_MASK_Rss;
1017
1018     if (mask != s->xxch_core_mask) {
1019         av_log(s->avctx, AV_LOG_ERROR, "XXCH core speaker activity mask (%#x) disagrees with core (%#x)\n", s->xxch_core_mask, mask);
1020         return AVERROR_INVALIDDATA;
1021     }
1022
1023     // Reserved
1024     // Byte align
1025     // CRC16 of XXCH frame header
1026     if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1027         av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH frame header\n");
1028         return AVERROR_INVALIDDATA;
1029     }
1030
1031     // Parse XXCH channel set 0
1032     if ((ret = parse_frame_data(s, HEADER_XXCH, s->nchannels)) < 0)
1033         return ret;
1034
1035     if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8 + xxch_frame_size * 8)) {
1036         av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set\n");
1037         return AVERROR_INVALIDDATA;
1038     }
1039
1040     return 0;
1041 }
1042
1043 static int parse_xbr_subframe(DCACoreDecoder *s, int xbr_base_ch, int xbr_nchannels,
1044                               int *xbr_nsubbands, int xbr_transition_mode, int sf, int *sub_pos)
1045 {
1046     int     xbr_nabits[DCA_CHANNELS];
1047     int     xbr_bit_allocation[DCA_CHANNELS][DCA_SUBBANDS];
1048     int     xbr_scale_nbits[DCA_CHANNELS];
1049     int32_t xbr_scale_factors[DCA_CHANNELS][DCA_SUBBANDS][2];
1050     int     ssf, ch, band, ofs;
1051
1052     // Check number of subband samples in this subframe
1053     if (*sub_pos + s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES > s->npcmblocks) {
1054         av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
1055         return AVERROR_INVALIDDATA;
1056     }
1057
1058     if (get_bits_left(&s->gb) < 0)
1059         return AVERROR_INVALIDDATA;
1060
1061     // Number of bits for XBR bit allocation index
1062     for (ch = xbr_base_ch; ch < xbr_nchannels; ch++)
1063         xbr_nabits[ch] = get_bits(&s->gb, 2) + 2;
1064
1065     // XBR bit allocation index
1066     for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1067         for (band = 0; band < xbr_nsubbands[ch]; band++) {
1068             xbr_bit_allocation[ch][band] = get_bits(&s->gb, xbr_nabits[ch]);
1069             if (xbr_bit_allocation[ch][band] > DCA_ABITS_MAX) {
1070                 av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR bit allocation index\n");
1071                 return AVERROR_INVALIDDATA;
1072             }
1073         }
1074     }
1075
1076     // Number of bits for scale indices
1077     for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1078         xbr_scale_nbits[ch] = get_bits(&s->gb, 3);
1079         if (!xbr_scale_nbits[ch]) {
1080             av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XBR scale factor index\n");
1081             return AVERROR_INVALIDDATA;
1082         }
1083     }
1084
1085     // XBR scale factors
1086     for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1087         const uint32_t *scale_table;
1088         int scale_size;
1089
1090         // Select the root square table
1091         if (s->scale_factor_sel[ch] > 5) {
1092             scale_table = ff_dca_scale_factor_quant7;
1093             scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7);
1094         } else {
1095             scale_table = ff_dca_scale_factor_quant6;
1096             scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6);
1097         }
1098
1099         // Parse scale factor indices and look up scale factors from the root
1100         // square table
1101         for (band = 0; band < xbr_nsubbands[ch]; band++) {
1102             if (xbr_bit_allocation[ch][band]) {
1103                 int scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
1104                 if (scale_index >= scale_size) {
1105                     av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
1106                     return AVERROR_INVALIDDATA;
1107                 }
1108                 xbr_scale_factors[ch][band][0] = scale_table[scale_index];
1109                 if (xbr_transition_mode && s->transition_mode[sf][ch][band]) {
1110                     scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
1111                     if (scale_index >= scale_size) {
1112                         av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
1113                         return AVERROR_INVALIDDATA;
1114                     }
1115                     xbr_scale_factors[ch][band][1] = scale_table[scale_index];
1116                 }
1117             }
1118         }
1119     }
1120
1121     // Audio data
1122     for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
1123         for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
1124             if (get_bits_left(&s->gb) < 0)
1125                 return AVERROR_INVALIDDATA;
1126
1127             for (band = 0; band < xbr_nsubbands[ch]; band++) {
1128                 int ret, trans_ssf, abits = xbr_bit_allocation[ch][band];
1129                 int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale;
1130
1131                 // Extract bits from the bit stream
1132                 if (abits > 7) {
1133                     // No further encoding
1134                     get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3);
1135                 } else if (abits > 0) {
1136                     // Block codes
1137                     if ((ret = parse_block_codes(s, audio, abits)) < 0)
1138                         return ret;
1139                 } else {
1140                     // No bits allocated
1141                     continue;
1142                 }
1143
1144                 // Look up quantization step size
1145                 step_size = ff_dca_lossless_quant[abits];
1146
1147                 // Identify transient location
1148                 if (xbr_transition_mode)
1149                     trans_ssf = s->transition_mode[sf][ch][band];
1150                 else
1151                     trans_ssf = 0;
1152
1153                 // Determine proper scale factor
1154                 if (trans_ssf == 0 || ssf < trans_ssf)
1155                     scale = xbr_scale_factors[ch][band][0];
1156                 else
1157                     scale = xbr_scale_factors[ch][band][1];
1158
1159                 dequantize(s->subband_samples[ch][band] + ofs,
1160                            audio, step_size, scale, 1);
1161             }
1162         }
1163
1164         // DSYNC
1165         if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
1166             av_log(s->avctx, AV_LOG_ERROR, "XBR-DSYNC check failed\n");
1167             return AVERROR_INVALIDDATA;
1168         }
1169
1170         ofs += DCA_SUBBAND_SAMPLES;
1171     }
1172
1173     // Advance subband sample pointer for the next subframe
1174     *sub_pos = ofs;
1175     return 0;
1176 }
1177
1178 static int parse_xbr_frame(DCACoreDecoder *s)
1179 {
1180     int     xbr_frame_size[DCA_EXSS_CHSETS_MAX];
1181     int     xbr_nchannels[DCA_EXSS_CHSETS_MAX];
1182     int     xbr_nsubbands[DCA_EXSS_CHSETS_MAX * DCA_EXSS_CHANNELS_MAX];
1183     int     xbr_nchsets, xbr_transition_mode, xbr_band_nbits, xbr_base_ch;
1184     int     i, ch1, ch2, ret, header_size, header_pos = get_bits_count(&s->gb);
1185
1186     // XBR sync word
1187     if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XBR) {
1188         av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR sync word\n");
1189         return AVERROR_INVALIDDATA;
1190     }
1191
1192     // XBR frame header length
1193     header_size = get_bits(&s->gb, 6) + 1;
1194
1195     // Check XBR frame header CRC
1196     if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
1197         && ff_dca_check_crc(&s->gb, header_pos + 32, header_pos + header_size * 8)) {
1198         av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR frame header checksum\n");
1199         return AVERROR_INVALIDDATA;
1200     }
1201
1202     // Number of channel sets
1203     xbr_nchsets = get_bits(&s->gb, 2) + 1;
1204
1205     // Channel set data byte size
1206     for (i = 0; i < xbr_nchsets; i++)
1207         xbr_frame_size[i] = get_bits(&s->gb, 14) + 1;
1208
1209     // Transition mode flag
1210     xbr_transition_mode = get_bits1(&s->gb);
1211
1212     // Channel set headers
1213     for (i = 0, ch2 = 0; i < xbr_nchsets; i++) {
1214         xbr_nchannels[i] = get_bits(&s->gb, 3) + 1;
1215         xbr_band_nbits = get_bits(&s->gb, 2) + 5;
1216         for (ch1 = 0; ch1 < xbr_nchannels[i]; ch1++, ch2++) {
1217             xbr_nsubbands[ch2] = get_bits(&s->gb, xbr_band_nbits) + 1;
1218             if (xbr_nsubbands[ch2] > DCA_SUBBANDS) {
1219                 av_log(s->avctx, AV_LOG_ERROR, "Invalid number of active XBR subbands (%d)\n", xbr_nsubbands[ch2]);
1220                 return AVERROR_INVALIDDATA;
1221             }
1222         }
1223     }
1224
1225     // Reserved
1226     // Byte align
1227     // CRC16 of XBR frame header
1228     if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1229         av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR frame header\n");
1230         return AVERROR_INVALIDDATA;
1231     }
1232
1233     // Channel set data
1234     for (i = 0, xbr_base_ch = 0; i < xbr_nchsets; i++) {
1235         header_pos = get_bits_count(&s->gb);
1236
1237         if (xbr_base_ch + xbr_nchannels[i] <= s->nchannels) {
1238             int sf, sub_pos;
1239
1240             for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) {
1241                 if ((ret = parse_xbr_subframe(s, xbr_base_ch,
1242                                               xbr_base_ch + xbr_nchannels[i],
1243                                               xbr_nsubbands, xbr_transition_mode,
1244                                               sf, &sub_pos)) < 0)
1245                     return ret;
1246             }
1247         }
1248
1249         xbr_base_ch += xbr_nchannels[i];
1250
1251         if (ff_dca_seek_bits(&s->gb, header_pos + xbr_frame_size[i] * 8)) {
1252             av_log(s->avctx, AV_LOG_ERROR, "Read past end of XBR channel set\n");
1253             return AVERROR_INVALIDDATA;
1254         }
1255     }
1256
1257     return 0;
1258 }
1259
1260 // Modified ISO/IEC 9899 linear congruential generator
1261 // Returns pseudorandom integer in range [-2^30, 2^30 - 1]
1262 static int rand_x96(DCACoreDecoder *s)
1263 {
1264     s->x96_rand = 1103515245U * s->x96_rand + 12345U;
1265     return (s->x96_rand & 0x7fffffff) - 0x40000000;
1266 }
1267
1268 static int parse_x96_subframe_audio(DCACoreDecoder *s, int sf, int xch_base, int *sub_pos)
1269 {
1270     int n, ssf, ch, band, ofs;
1271
1272     // Check number of subband samples in this subframe
1273     int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES;
1274     if (*sub_pos + nsamples > s->npcmblocks) {
1275         av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
1276         return AVERROR_INVALIDDATA;
1277     }
1278
1279     if (get_bits_left(&s->gb) < 0)
1280         return AVERROR_INVALIDDATA;
1281
1282     // VQ encoded or unallocated subbands
1283     for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1284         for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1285             // Get the sample pointer and scale factor
1286             int32_t *samples = s->x96_subband_samples[ch][band] + *sub_pos;
1287             int32_t scale    = s->scale_factors[ch][band >> 1][band & 1];
1288
1289             switch (s->bit_allocation[ch][band]) {
1290             case 0: // No bits allocated for subband
1291                 if (scale <= 1)
1292                     memset(samples, 0, nsamples * sizeof(int32_t));
1293                 else for (n = 0; n < nsamples; n++)
1294                     // Generate scaled random samples
1295                     samples[n] = mul31(rand_x96(s), scale);
1296                 break;
1297
1298             case 1: // VQ encoded subband
1299                 for (ssf = 0; ssf < (s->nsubsubframes[sf] + 1) / 2; ssf++) {
1300                     // Extract the VQ address from the bit stream and look up
1301                     // the VQ code book for up to 16 subband samples
1302                     const int8_t *vq_samples = ff_dca_high_freq_vq[get_bits(&s->gb, 10)];
1303                     // Scale and take the samples
1304                     for (n = 0; n < FFMIN(nsamples - ssf * 16, 16); n++)
1305                         *samples++ = clip23(vq_samples[n] * scale + (1 << 3) >> 4);
1306                 }
1307                 break;
1308             }
1309         }
1310     }
1311
1312     // Audio data
1313     for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
1314         for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1315             if (get_bits_left(&s->gb) < 0)
1316                 return AVERROR_INVALIDDATA;
1317
1318             for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1319                 int ret, abits = s->bit_allocation[ch][band] - 1;
1320                 int32_t audio[DCA_SUBBAND_SAMPLES], step_size, scale;
1321
1322                 // Not VQ encoded or unallocated subbands
1323                 if (abits < 1)
1324                     continue;
1325
1326                 // Extract bits from the bit stream
1327                 if ((ret = extract_audio(s, audio, abits, ch)) < 0)
1328                     return ret;
1329
1330                 // Select quantization step size table and look up quantization
1331                 // step size
1332                 if (s->bit_rate == 3)
1333                     step_size = ff_dca_lossless_quant[abits];
1334                 else
1335                     step_size = ff_dca_lossy_quant[abits];
1336
1337                 // Get the scale factor
1338                 scale = s->scale_factors[ch][band >> 1][band & 1];
1339
1340                 dequantize(s->x96_subband_samples[ch][band] + ofs,
1341                            audio, step_size, scale, 0);
1342             }
1343         }
1344
1345         // DSYNC
1346         if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
1347             av_log(s->avctx, AV_LOG_ERROR, "X96-DSYNC check failed\n");
1348             return AVERROR_INVALIDDATA;
1349         }
1350
1351         ofs += DCA_SUBBAND_SAMPLES;
1352     }
1353
1354     // Inverse ADPCM
1355     for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1356         inverse_adpcm(s->x96_subband_samples[ch], s->prediction_vq_index[ch],
1357                       s->prediction_mode[ch], s->x96_subband_start, s->nsubbands[ch],
1358                       *sub_pos, nsamples);
1359     }
1360
1361     // Joint subband coding
1362     for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1363         int src_ch = s->joint_intensity_index[ch] - 1;
1364         if (src_ch >= 0) {
1365             s->dcadsp->decode_joint(s->x96_subband_samples[ch], s->x96_subband_samples[src_ch],
1366                                     s->joint_scale_factors[ch], s->nsubbands[ch],
1367                                     s->nsubbands[src_ch], *sub_pos, nsamples);
1368         }
1369     }
1370
1371     // Advance subband sample pointer for the next subframe
1372     *sub_pos = ofs;
1373     return 0;
1374 }
1375
1376 static void erase_x96_adpcm_history(DCACoreDecoder *s)
1377 {
1378     int ch, band;
1379
1380     // Erase ADPCM history from previous frame if
1381     // predictor history switch was disabled
1382     for (ch = 0; ch < DCA_CHANNELS; ch++)
1383         for (band = 0; band < DCA_SUBBANDS_X96; band++)
1384             AV_ZERO128(s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS);
1385
1386     emms_c();
1387 }
1388
1389 static int alloc_x96_sample_buffer(DCACoreDecoder *s)
1390 {
1391     int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks;
1392     int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS_X96;
1393     unsigned int size = s->x96_subband_size;
1394     int ch, band;
1395
1396     // Reallocate subband sample buffer
1397     av_fast_mallocz(&s->x96_subband_buffer, &s->x96_subband_size,
1398                     nframesamples * sizeof(int32_t));
1399     if (!s->x96_subband_buffer)
1400         return AVERROR(ENOMEM);
1401
1402     if (size != s->x96_subband_size) {
1403         for (ch = 0; ch < DCA_CHANNELS; ch++)
1404             for (band = 0; band < DCA_SUBBANDS_X96; band++)
1405                 s->x96_subband_samples[ch][band] = s->x96_subband_buffer +
1406                     (ch * DCA_SUBBANDS_X96 + band) * nchsamples + DCA_ADPCM_COEFFS;
1407     }
1408
1409     if (!s->predictor_history)
1410         erase_x96_adpcm_history(s);
1411
1412     return 0;
1413 }
1414
1415 static int parse_x96_subframe_header(DCACoreDecoder *s, int xch_base)
1416 {
1417     int ch, band, ret;
1418
1419     if (get_bits_left(&s->gb) < 0)
1420         return AVERROR_INVALIDDATA;
1421
1422     // Prediction mode
1423     for (ch = xch_base; ch < s->x96_nchannels; ch++)
1424         for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++)
1425             s->prediction_mode[ch][band] = get_bits1(&s->gb);
1426
1427     // Prediction coefficients VQ address
1428     for (ch = xch_base; ch < s->x96_nchannels; ch++)
1429         for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++)
1430             if (s->prediction_mode[ch][band])
1431                 s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12);
1432
1433     // Bit allocation index
1434     for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1435         int sel = s->bit_allocation_sel[ch];
1436         int abits = 0;
1437
1438         for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1439             // If Huffman code was used, the difference of abits was encoded
1440             if (sel < 7)
1441                 abits += dca_get_vlc(&s->gb, &ff_dca_vlc_quant_index[5 + 2 * s->x96_high_res], sel);
1442             else
1443                 abits = get_bits(&s->gb, 3 + s->x96_high_res);
1444
1445             if (abits < 0 || abits > 7 + 8 * s->x96_high_res) {
1446                 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 bit allocation index\n");
1447                 return AVERROR_INVALIDDATA;
1448             }
1449
1450             s->bit_allocation[ch][band] = abits;
1451         }
1452     }
1453
1454     // Scale factors
1455     for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1456         int sel = s->scale_factor_sel[ch];
1457         int scale_index = 0;
1458
1459         // Extract scales for subbands which are transmitted even for
1460         // unallocated subbands
1461         for (band = s->x96_subband_start; band < s->nsubbands[ch]; band++) {
1462             if ((ret = parse_scale(s, &scale_index, sel)) < 0)
1463                 return ret;
1464             s->scale_factors[ch][band >> 1][band & 1] = ret;
1465         }
1466     }
1467
1468     // Joint subband codebook select
1469     for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1470         if (s->joint_intensity_index[ch]) {
1471             s->joint_scale_sel[ch] = get_bits(&s->gb, 3);
1472             if (s->joint_scale_sel[ch] == 7) {
1473                 av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint scale factor code book\n");
1474                 return AVERROR_INVALIDDATA;
1475             }
1476         }
1477     }
1478
1479     // Scale factors for joint subband coding
1480     for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1481         int src_ch = s->joint_intensity_index[ch] - 1;
1482         if (src_ch >= 0) {
1483             int sel = s->joint_scale_sel[ch];
1484             for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) {
1485                 if ((ret = parse_joint_scale(s, sel)) < 0)
1486                     return ret;
1487                 s->joint_scale_factors[ch][band] = ret;
1488             }
1489         }
1490     }
1491
1492     // Side information CRC check word
1493     if (s->crc_present)
1494         skip_bits(&s->gb, 16);
1495
1496     return 0;
1497 }
1498
1499 static int parse_x96_coding_header(DCACoreDecoder *s, int exss, int xch_base)
1500 {
1501     int n, ch, header_size = 0, header_pos = get_bits_count(&s->gb);
1502
1503     if (get_bits_left(&s->gb) < 0)
1504         return AVERROR_INVALIDDATA;
1505
1506     if (exss) {
1507         // Channel set header length
1508         header_size = get_bits(&s->gb, 7) + 1;
1509
1510         // Check CRC
1511         if (s->x96_crc_present
1512             && (s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
1513             && ff_dca_check_crc(&s->gb, header_pos, header_pos + header_size * 8)) {
1514             av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 channel set header checksum\n");
1515             return AVERROR_INVALIDDATA;
1516         }
1517     }
1518
1519     // High resolution flag
1520     s->x96_high_res = get_bits1(&s->gb);
1521
1522     // First encoded subband
1523     if (s->x96_rev_no < 8) {
1524         s->x96_subband_start = get_bits(&s->gb, 5);
1525         if (s->x96_subband_start > 27) {
1526             av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband start index (%d)\n", s->x96_subband_start);
1527             return AVERROR_INVALIDDATA;
1528         }
1529     } else {
1530         s->x96_subband_start = DCA_SUBBANDS;
1531     }
1532
1533     // Subband activity count
1534     for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1535         s->nsubbands[ch] = get_bits(&s->gb, 6) + 1;
1536         if (s->nsubbands[ch] < DCA_SUBBANDS) {
1537             av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 subband activity count (%d)\n", s->nsubbands[ch]);
1538             return AVERROR_INVALIDDATA;
1539         }
1540     }
1541
1542     // Joint intensity coding index
1543     for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1544         if ((n = get_bits(&s->gb, 3)) && xch_base)
1545             n += xch_base - 1;
1546         if (n > s->x96_nchannels) {
1547             av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 joint intensity coding index\n");
1548             return AVERROR_INVALIDDATA;
1549         }
1550         s->joint_intensity_index[ch] = n;
1551     }
1552
1553     // Scale factor code book
1554     for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1555         s->scale_factor_sel[ch] = get_bits(&s->gb, 3);
1556         if (s->scale_factor_sel[ch] >= 6) {
1557             av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 scale factor code book\n");
1558             return AVERROR_INVALIDDATA;
1559         }
1560     }
1561
1562     // Bit allocation quantizer select
1563     for (ch = xch_base; ch < s->x96_nchannels; ch++)
1564         s->bit_allocation_sel[ch] = get_bits(&s->gb, 3);
1565
1566     // Quantization index codebook select
1567     for (n = 0; n < 6 + 4 * s->x96_high_res; n++)
1568         for (ch = xch_base; ch < s->x96_nchannels; ch++)
1569             s->quant_index_sel[ch][n] = get_bits(&s->gb, quant_index_sel_nbits[n]);
1570
1571     if (exss) {
1572         // Reserved
1573         // Byte align
1574         // CRC16 of channel set header
1575         if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1576             av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set header\n");
1577             return AVERROR_INVALIDDATA;
1578         }
1579     } else {
1580         if (s->crc_present)
1581             skip_bits(&s->gb, 16);
1582     }
1583
1584     return 0;
1585 }
1586
1587 static int parse_x96_frame_data(DCACoreDecoder *s, int exss, int xch_base)
1588 {
1589     int sf, ch, ret, band, sub_pos;
1590
1591     if ((ret = parse_x96_coding_header(s, exss, xch_base)) < 0)
1592         return ret;
1593
1594     for (sf = 0, sub_pos = 0; sf < s->nsubframes; sf++) {
1595         if ((ret = parse_x96_subframe_header(s, xch_base)) < 0)
1596             return ret;
1597         if ((ret = parse_x96_subframe_audio(s, sf, xch_base, &sub_pos)) < 0)
1598             return ret;
1599     }
1600
1601     for (ch = xch_base; ch < s->x96_nchannels; ch++) {
1602         // Determine number of active subbands for this channel
1603         int nsubbands = s->nsubbands[ch];
1604         if (s->joint_intensity_index[ch])
1605             nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]);
1606
1607         // Update history for ADPCM and clear inactive subbands
1608         for (band = 0; band < DCA_SUBBANDS_X96; band++) {
1609             int32_t *samples = s->x96_subband_samples[ch][band] - DCA_ADPCM_COEFFS;
1610             if (band >= s->x96_subband_start && band < nsubbands)
1611                 AV_COPY128(samples, samples + s->npcmblocks);
1612             else
1613                 memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t));
1614         }
1615     }
1616
1617     emms_c();
1618
1619     return 0;
1620 }
1621
1622 static int parse_x96_frame(DCACoreDecoder *s)
1623 {
1624     int ret;
1625
1626     // Revision number
1627     s->x96_rev_no = get_bits(&s->gb, 4);
1628     if (s->x96_rev_no < 1 || s->x96_rev_no > 8) {
1629         av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no);
1630         return AVERROR_INVALIDDATA;
1631     }
1632
1633     s->x96_crc_present = 0;
1634     s->x96_nchannels = s->nchannels;
1635
1636     if ((ret = alloc_x96_sample_buffer(s)) < 0)
1637         return ret;
1638
1639     if ((ret = parse_x96_frame_data(s, 0, 0)) < 0)
1640         return ret;
1641
1642     // Seek to the end of core frame
1643     if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1644         av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame\n");
1645         return AVERROR_INVALIDDATA;
1646     }
1647
1648     return 0;
1649 }
1650
1651 static int parse_x96_frame_exss(DCACoreDecoder *s)
1652 {
1653     int     x96_frame_size[DCA_EXSS_CHSETS_MAX];
1654     int     x96_nchannels[DCA_EXSS_CHSETS_MAX];
1655     int     x96_nchsets, x96_base_ch;
1656     int     i, ret, header_size, header_pos = get_bits_count(&s->gb);
1657
1658     // X96 sync word
1659     if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_X96) {
1660         av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 sync word\n");
1661         return AVERROR_INVALIDDATA;
1662     }
1663
1664     // X96 frame header length
1665     header_size = get_bits(&s->gb, 6) + 1;
1666
1667     // Check X96 frame header CRC
1668     if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
1669         && ff_dca_check_crc(&s->gb, header_pos + 32, header_pos + header_size * 8)) {
1670         av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 frame header checksum\n");
1671         return AVERROR_INVALIDDATA;
1672     }
1673
1674     // Revision number
1675     s->x96_rev_no = get_bits(&s->gb, 4);
1676     if (s->x96_rev_no < 1 || s->x96_rev_no > 8) {
1677         av_log(s->avctx, AV_LOG_ERROR, "Invalid X96 revision (%d)\n", s->x96_rev_no);
1678         return AVERROR_INVALIDDATA;
1679     }
1680
1681     // CRC presence flag for channel set header
1682     s->x96_crc_present = get_bits1(&s->gb);
1683
1684     // Number of channel sets
1685     x96_nchsets = get_bits(&s->gb, 2) + 1;
1686
1687     // Channel set data byte size
1688     for (i = 0; i < x96_nchsets; i++)
1689         x96_frame_size[i] = get_bits(&s->gb, 12) + 1;
1690
1691     // Number of channels in channel set
1692     for (i = 0; i < x96_nchsets; i++)
1693         x96_nchannels[i] = get_bits(&s->gb, 3) + 1;
1694
1695     // Reserved
1696     // Byte align
1697     // CRC16 of X96 frame header
1698     if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
1699         av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 frame header\n");
1700         return AVERROR_INVALIDDATA;
1701     }
1702
1703     if ((ret = alloc_x96_sample_buffer(s)) < 0)
1704         return ret;
1705
1706     // Channel set data
1707     s->x96_nchannels = 0;
1708     for (i = 0, x96_base_ch = 0; i < x96_nchsets; i++) {
1709         header_pos = get_bits_count(&s->gb);
1710
1711         if (x96_base_ch + x96_nchannels[i] <= s->nchannels) {
1712             s->x96_nchannels = x96_base_ch + x96_nchannels[i];
1713             if ((ret = parse_x96_frame_data(s, 1, x96_base_ch)) < 0)
1714                 return ret;
1715         }
1716
1717         x96_base_ch += x96_nchannels[i];
1718
1719         if (ff_dca_seek_bits(&s->gb, header_pos + x96_frame_size[i] * 8)) {
1720             av_log(s->avctx, AV_LOG_ERROR, "Read past end of X96 channel set\n");
1721             return AVERROR_INVALIDDATA;
1722         }
1723     }
1724
1725     return 0;
1726 }
1727
1728 static int parse_aux_data(DCACoreDecoder *s)
1729 {
1730     int aux_pos;
1731
1732     if (get_bits_left(&s->gb) < 0)
1733         return AVERROR_INVALIDDATA;
1734
1735     // Auxiliary data byte count (can't be trusted)
1736     skip_bits(&s->gb, 6);
1737
1738     // 4-byte align
1739     skip_bits_long(&s->gb, -get_bits_count(&s->gb) & 31);
1740
1741     // Auxiliary data sync word
1742     if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_REV1AUX) {
1743         av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data sync word\n");
1744         return AVERROR_INVALIDDATA;
1745     }
1746
1747     aux_pos = get_bits_count(&s->gb);
1748
1749     // Auxiliary decode time stamp flag
1750     if (get_bits1(&s->gb))
1751         skip_bits_long(&s->gb, 47);
1752
1753     // Auxiliary dynamic downmix flag
1754     if (s->prim_dmix_embedded = get_bits1(&s->gb)) {
1755         int i, m, n;
1756
1757         // Auxiliary primary channel downmix type
1758         s->prim_dmix_type = get_bits(&s->gb, 3);
1759         if (s->prim_dmix_type >= DCA_DMIX_TYPE_COUNT) {
1760             av_log(s->avctx, AV_LOG_ERROR, "Invalid primary channel set downmix type\n");
1761             return AVERROR_INVALIDDATA;
1762         }
1763
1764         // Size of downmix coefficients matrix
1765         m = ff_dca_dmix_primary_nch[s->prim_dmix_type];
1766         n = ff_dca_channels[s->audio_mode] + !!s->lfe_present;
1767
1768         // Dynamic downmix code coefficients
1769         for (i = 0; i < m * n; i++) {
1770             int code = get_bits(&s->gb, 9);
1771             int sign = (code >> 8) - 1;
1772             unsigned int index = code & 0xff;
1773             if (index >= FF_DCA_DMIXTABLE_SIZE) {
1774                 av_log(s->avctx, AV_LOG_ERROR, "Invalid downmix coefficient index\n");
1775                 return AVERROR_INVALIDDATA;
1776             }
1777             s->prim_dmix_coeff[i] = (ff_dca_dmixtable[index] ^ sign) - sign;
1778         }
1779     }
1780
1781     // Byte align
1782     skip_bits(&s->gb, -get_bits_count(&s->gb) & 7);
1783
1784     // CRC16 of auxiliary data
1785     skip_bits(&s->gb, 16);
1786
1787     // Check CRC
1788     if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
1789         && ff_dca_check_crc(&s->gb, aux_pos, get_bits_count(&s->gb))) {
1790         av_log(s->avctx, AV_LOG_ERROR, "Invalid auxiliary data checksum\n");
1791         return AVERROR_INVALIDDATA;
1792     }
1793
1794     return 0;
1795 }
1796
1797 static int parse_optional_info(DCACoreDecoder *s)
1798 {
1799     DCAContext *dca = s->avctx->priv_data;
1800     int ret = -1;
1801
1802     // Time code stamp
1803     if (s->ts_present)
1804         skip_bits_long(&s->gb, 32);
1805
1806     // Auxiliary data
1807     if (s->aux_present && (ret = parse_aux_data(s)) < 0
1808         && (s->avctx->err_recognition & AV_EF_EXPLODE))
1809         return ret;
1810
1811     if (ret < 0)
1812         s->prim_dmix_embedded = 0;
1813
1814     // Core extensions
1815     if (s->ext_audio_present && !dca->core_only) {
1816         int sync_pos = FFMIN(s->frame_size / 4, s->gb.size_in_bits / 32) - 1;
1817         int last_pos = get_bits_count(&s->gb) / 32;
1818         int size, dist;
1819
1820         // Search for extension sync words aligned on 4-byte boundary. Search
1821         // must be done backwards from the end of core frame to work around
1822         // sync word aliasing issues.
1823         switch (s->ext_audio_type) {
1824         case EXT_AUDIO_XCH:
1825             if (dca->request_channel_layout)
1826                 break;
1827
1828             // The distance between XCH sync word and end of the core frame
1829             // must be equal to XCH frame size. Off by one error is allowed for
1830             // compatibility with legacy bitstreams. Minimum XCH frame size is
1831             // 96 bytes. AMODE and PCHS are further checked to reduce
1832             // probability of alias sync detection.
1833             for (; sync_pos >= last_pos; sync_pos--) {
1834                 if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_XCH) {
1835                     s->gb.index = (sync_pos + 1) * 32;
1836                     size = get_bits(&s->gb, 10) + 1;
1837                     dist = s->frame_size - sync_pos * 4;
1838                     if (size >= 96
1839                         && (size == dist || size - 1 == dist)
1840                         && get_bits(&s->gb, 7) == 0x08) {
1841                         s->xch_pos = get_bits_count(&s->gb);
1842                         break;
1843                     }
1844                 }
1845             }
1846
1847             if (!s->xch_pos) {
1848                 av_log(s->avctx, AV_LOG_ERROR, "XCH sync word not found\n");
1849                 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1850                     return AVERROR_INVALIDDATA;
1851             }
1852             break;
1853
1854         case EXT_AUDIO_X96:
1855             // The distance between X96 sync word and end of the core frame
1856             // must be equal to X96 frame size. Minimum X96 frame size is 96
1857             // bytes.
1858             for (; sync_pos >= last_pos; sync_pos--) {
1859                 if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_X96) {
1860                     s->gb.index = (sync_pos + 1) * 32;
1861                     size = get_bits(&s->gb, 12) + 1;
1862                     dist = s->frame_size - sync_pos * 4;
1863                     if (size >= 96 && size == dist) {
1864                         s->x96_pos = get_bits_count(&s->gb);
1865                         break;
1866                     }
1867                 }
1868             }
1869
1870             if (!s->x96_pos) {
1871                 av_log(s->avctx, AV_LOG_ERROR, "X96 sync word not found\n");
1872                 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1873                     return AVERROR_INVALIDDATA;
1874             }
1875             break;
1876
1877         case EXT_AUDIO_XXCH:
1878             if (dca->request_channel_layout)
1879                 break;
1880
1881             // XXCH frame header CRC must be valid. Minimum XXCH frame header
1882             // size is 11 bytes.
1883             for (; sync_pos >= last_pos; sync_pos--) {
1884                 if (AV_RB32(s->gb.buffer + sync_pos * 4) == DCA_SYNCWORD_XXCH) {
1885                     s->gb.index = (sync_pos + 1) * 32;
1886                     size = get_bits(&s->gb, 6) + 1;
1887                     if (size >= 11 &&
1888                         !ff_dca_check_crc(&s->gb, (sync_pos + 1) * 32,
1889                                           sync_pos * 32 + size * 8)) {
1890                         s->xxch_pos = sync_pos * 32;
1891                         break;
1892                     }
1893                 }
1894             }
1895
1896             if (!s->xxch_pos) {
1897                 av_log(s->avctx, AV_LOG_ERROR, "XXCH sync word not found\n");
1898                 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1899                     return AVERROR_INVALIDDATA;
1900             }
1901             break;
1902         }
1903     }
1904
1905     return 0;
1906 }
1907
1908 int ff_dca_core_parse(DCACoreDecoder *s, uint8_t *data, int size)
1909 {
1910     int ret;
1911
1912     s->ext_audio_mask = 0;
1913     s->xch_pos = s->xxch_pos = s->x96_pos = 0;
1914
1915     if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
1916         return ret;
1917
1918     skip_bits_long(&s->gb, 32);
1919     if ((ret = parse_frame_header(s)) < 0)
1920         return ret;
1921     if ((ret = alloc_sample_buffer(s)) < 0)
1922         return ret;
1923     if ((ret = parse_frame_data(s, HEADER_CORE, 0)) < 0)
1924         return ret;
1925     if ((ret = parse_optional_info(s)) < 0)
1926         return ret;
1927
1928     // Workaround for DTS in WAV
1929     if (s->frame_size > size && s->frame_size < size + 4) {
1930         av_log(s->avctx, AV_LOG_DEBUG, "Working around excessive core frame size (%d > %d)\n", s->frame_size, size);
1931         s->frame_size = size;
1932     }
1933
1934     if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
1935         av_log(s->avctx, AV_LOG_ERROR, "Read past end of core frame\n");
1936         if (s->avctx->err_recognition & AV_EF_EXPLODE)
1937             return AVERROR_INVALIDDATA;
1938     }
1939
1940     return 0;
1941 }
1942
1943 int ff_dca_core_parse_exss(DCACoreDecoder *s, uint8_t *data, DCAExssAsset *asset)
1944 {
1945     AVCodecContext *avctx = s->avctx;
1946     DCAContext *dca = avctx->priv_data;
1947     GetBitContext gb = s->gb;
1948     int exss_mask = asset ? asset->extension_mask : 0;
1949     int ret = 0, ext = 0;
1950
1951     // Parse (X)XCH unless downmixing
1952     if (!dca->request_channel_layout) {
1953         if (exss_mask & DCA_EXSS_XXCH) {
1954             if ((ret = init_get_bits8(&s->gb, data + asset->xxch_offset, asset->xxch_size)) < 0)
1955                 return ret;
1956             ret = parse_xxch_frame(s);
1957             ext = DCA_EXSS_XXCH;
1958         } else if (s->xxch_pos) {
1959             s->gb.index = s->xxch_pos;
1960             ret = parse_xxch_frame(s);
1961             ext = DCA_CSS_XXCH;
1962         } else if (s->xch_pos) {
1963             s->gb.index = s->xch_pos;
1964             ret = parse_xch_frame(s);
1965             ext = DCA_CSS_XCH;
1966         }
1967
1968         // Revert to primary channel set in case (X)XCH parsing fails
1969         if (ret < 0) {
1970             if (avctx->err_recognition & AV_EF_EXPLODE)
1971                 return ret;
1972             s->nchannels = ff_dca_channels[s->audio_mode];
1973             s->ch_mask = audio_mode_ch_mask[s->audio_mode];
1974             if (s->lfe_present)
1975                 s->ch_mask |= DCA_SPEAKER_MASK_LFE1;
1976         } else {
1977             s->ext_audio_mask |= ext;
1978         }
1979     }
1980
1981     // Parse XBR
1982     if (exss_mask & DCA_EXSS_XBR) {
1983         if ((ret = init_get_bits8(&s->gb, data + asset->xbr_offset, asset->xbr_size)) < 0)
1984             return ret;
1985         if ((ret = parse_xbr_frame(s)) < 0) {
1986             if (avctx->err_recognition & AV_EF_EXPLODE)
1987                 return ret;
1988         } else {
1989             s->ext_audio_mask |= DCA_EXSS_XBR;
1990         }
1991     }
1992
1993     // Parse X96 unless decoding XLL
1994     if (!(dca->packet & DCA_PACKET_XLL)) {
1995         if (exss_mask & DCA_EXSS_X96) {
1996             if ((ret = init_get_bits8(&s->gb, data + asset->x96_offset, asset->x96_size)) < 0)
1997                 return ret;
1998             if ((ret = parse_x96_frame_exss(s)) < 0) {
1999                 if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
2000                     return ret;
2001             } else {
2002                 s->ext_audio_mask |= DCA_EXSS_X96;
2003             }
2004         } else if (s->x96_pos) {
2005             s->gb = gb;
2006             s->gb.index = s->x96_pos;
2007             if ((ret = parse_x96_frame(s)) < 0) {
2008                 if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
2009                     return ret;
2010             } else {
2011                 s->ext_audio_mask |= DCA_CSS_X96;
2012             }
2013         }
2014     }
2015
2016     return 0;
2017 }
2018
2019 static int map_prm_ch_to_spkr(DCACoreDecoder *s, int ch)
2020 {
2021     int pos, spkr;
2022
2023     // Try to map this channel to core first
2024     pos = ff_dca_channels[s->audio_mode];
2025     if (ch < pos) {
2026         spkr = prm_ch_to_spkr_map[s->audio_mode][ch];
2027         if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) {
2028             if (s->xxch_core_mask & (1U << spkr))
2029                 return spkr;
2030             if (spkr == DCA_SPEAKER_Ls && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss))
2031                 return DCA_SPEAKER_Lss;
2032             if (spkr == DCA_SPEAKER_Rs && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss))
2033                 return DCA_SPEAKER_Rss;
2034             return -1;
2035         }
2036         return spkr;
2037     }
2038
2039     // Then XCH
2040     if ((s->ext_audio_mask & DCA_CSS_XCH) && ch == pos)
2041         return DCA_SPEAKER_Cs;
2042
2043     // Then XXCH
2044     if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH)) {
2045         for (spkr = DCA_SPEAKER_Cs; spkr < s->xxch_mask_nbits; spkr++)
2046             if (s->xxch_spkr_mask & (1U << spkr))
2047                 if (pos++ == ch)
2048                     return spkr;
2049     }
2050
2051     // No mapping
2052     return -1;
2053 }
2054
2055 static void erase_dsp_history(DCACoreDecoder *s)
2056 {
2057     memset(s->dcadsp_data, 0, sizeof(s->dcadsp_data));
2058     s->output_history_lfe_fixed = 0;
2059     s->output_history_lfe_float = 0;
2060 }
2061
2062 static void set_filter_mode(DCACoreDecoder *s, int mode)
2063 {
2064     if (s->filter_mode != mode) {
2065         erase_dsp_history(s);
2066         s->filter_mode = mode;
2067     }
2068 }
2069
2070 int ff_dca_core_filter_fixed(DCACoreDecoder *s, int x96_synth)
2071 {
2072     int n, ch, spkr, nsamples, x96_nchannels = 0;
2073     const int32_t *filter_coeff;
2074     int32_t *ptr;
2075
2076     // Externally set x96_synth flag implies that X96 synthesis should be
2077     // enabled, yet actual X96 subband data should be discarded. This is a
2078     // special case for lossless residual decoder that ignores X96 data if
2079     // present.
2080     if (!x96_synth && (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96))) {
2081         x96_nchannels = s->x96_nchannels;
2082         x96_synth = 1;
2083     }
2084     if (x96_synth < 0)
2085         x96_synth = 0;
2086
2087     s->output_rate = s->sample_rate << x96_synth;
2088     s->npcmsamples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth;
2089
2090     // Reallocate PCM output buffer
2091     av_fast_malloc(&s->output_buffer, &s->output_size,
2092                    nsamples * av_popcount(s->ch_mask) * sizeof(int32_t));
2093     if (!s->output_buffer)
2094         return AVERROR(ENOMEM);
2095
2096     ptr = (int32_t *)s->output_buffer;
2097     for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) {
2098         if (s->ch_mask & (1U << spkr)) {
2099             s->output_samples[spkr] = ptr;
2100             ptr += nsamples;
2101         } else {
2102             s->output_samples[spkr] = NULL;
2103         }
2104     }
2105
2106     // Handle change of filtering mode
2107     set_filter_mode(s, x96_synth | DCA_FILTER_MODE_FIXED);
2108
2109     // Select filter
2110     if (x96_synth)
2111         filter_coeff = ff_dca_fir_64bands_fixed;
2112     else if (s->filter_perfect)
2113         filter_coeff = ff_dca_fir_32bands_perfect_fixed;
2114     else
2115         filter_coeff = ff_dca_fir_32bands_nonperfect_fixed;
2116
2117     // Filter primary channels
2118     for (ch = 0; ch < s->nchannels; ch++) {
2119         // Map this primary channel to speaker
2120         spkr = map_prm_ch_to_spkr(s, ch);
2121         if (spkr < 0)
2122             return AVERROR(EINVAL);
2123
2124         // Filter bank reconstruction
2125         s->dcadsp->sub_qmf_fixed[x96_synth](
2126             &s->synth,
2127             &s->dcadct,
2128             s->output_samples[spkr],
2129             s->subband_samples[ch],
2130             ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL,
2131             s->dcadsp_data[ch].u.fix.hist1,
2132             &s->dcadsp_data[ch].offset,
2133             s->dcadsp_data[ch].u.fix.hist2,
2134             filter_coeff,
2135             s->npcmblocks);
2136     }
2137
2138     // Filter LFE channel
2139     if (s->lfe_present) {
2140         int32_t *samples = s->output_samples[DCA_SPEAKER_LFE1];
2141         int nlfesamples = s->npcmblocks >> 1;
2142
2143         // Check LFF
2144         if (s->lfe_present == LFE_FLAG_128) {
2145             av_log(s->avctx, AV_LOG_ERROR, "Fixed point mode doesn't support LFF=1\n");
2146             return AVERROR(EINVAL);
2147         }
2148
2149         // Offset intermediate buffer for X96
2150         if (x96_synth)
2151             samples += nsamples / 2;
2152
2153         // Interpolate LFE channel
2154         s->dcadsp->lfe_fir_fixed(samples, s->lfe_samples + DCA_LFE_HISTORY,
2155                                  ff_dca_lfe_fir_64_fixed, s->npcmblocks);
2156
2157         if (x96_synth) {
2158             // Filter 96 kHz oversampled LFE PCM to attenuate high frequency
2159             // (47.6 - 48.0 kHz) components of interpolation image
2160             s->dcadsp->lfe_x96_fixed(s->output_samples[DCA_SPEAKER_LFE1],
2161                                      samples, &s->output_history_lfe_fixed,
2162                                      nsamples / 2);
2163
2164         }
2165
2166         // Update LFE history
2167         for (n = DCA_LFE_HISTORY - 1; n >= 0; n--)
2168             s->lfe_samples[n] = s->lfe_samples[nlfesamples + n];
2169     }
2170
2171     return 0;
2172 }
2173
2174 static int filter_frame_fixed(DCACoreDecoder *s, AVFrame *frame)
2175 {
2176     AVCodecContext *avctx = s->avctx;
2177     DCAContext *dca = avctx->priv_data;
2178     int i, n, ch, ret, spkr, nsamples;
2179
2180     // Don't filter twice when falling back from XLL
2181     if (!(dca->packet & DCA_PACKET_XLL) && (ret = ff_dca_core_filter_fixed(s, 0)) < 0)
2182         return ret;
2183
2184     avctx->sample_rate = s->output_rate;
2185     avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
2186     avctx->bits_per_raw_sample = 24;
2187
2188     frame->nb_samples = nsamples = s->npcmsamples;
2189     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2190         return ret;
2191
2192     // Undo embedded XCH downmix
2193     if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
2194         && s->audio_mode >= AMODE_2F2R) {
2195         s->dcadsp->dmix_sub_xch(s->output_samples[DCA_SPEAKER_Ls],
2196                                 s->output_samples[DCA_SPEAKER_Rs],
2197                                 s->output_samples[DCA_SPEAKER_Cs],
2198                                 nsamples);
2199
2200     }
2201
2202     // Undo embedded XXCH downmix
2203     if ((s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH))
2204         && s->xxch_dmix_embedded) {
2205         int scale_inv   = s->xxch_dmix_scale_inv;
2206         int *coeff_ptr  = s->xxch_dmix_coeff;
2207         int xch_base    = ff_dca_channels[s->audio_mode];
2208         av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX);
2209
2210         // Undo embedded core downmix pre-scaling
2211         for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2212             if (s->xxch_core_mask & (1U << spkr)) {
2213                 s->dcadsp->dmix_scale_inv(s->output_samples[spkr],
2214                                           scale_inv, nsamples);
2215             }
2216         }
2217
2218         // Undo downmix
2219         for (ch = xch_base; ch < s->nchannels; ch++) {
2220             int src_spkr = map_prm_ch_to_spkr(s, ch);
2221             if (src_spkr < 0)
2222                 return AVERROR(EINVAL);
2223             for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2224                 if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) {
2225                     int coeff = mul16(*coeff_ptr++, scale_inv);
2226                     if (coeff) {
2227                         s->dcadsp->dmix_sub(s->output_samples[spkr    ],
2228                                             s->output_samples[src_spkr],
2229                                             coeff, nsamples);
2230                     }
2231                 }
2232             }
2233         }
2234     }
2235
2236     if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) {
2237         // Front sum/difference decoding
2238         if ((s->sumdiff_front && s->audio_mode > AMODE_MONO)
2239             || s->audio_mode == AMODE_STEREO_SUMDIFF) {
2240             s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_L],
2241                                             s->output_samples[DCA_SPEAKER_R],
2242                                             nsamples);
2243         }
2244
2245         // Surround sum/difference decoding
2246         if (s->sumdiff_surround && s->audio_mode >= AMODE_2F2R) {
2247             s->fixed_dsp->butterflies_fixed(s->output_samples[DCA_SPEAKER_Ls],
2248                                             s->output_samples[DCA_SPEAKER_Rs],
2249                                             nsamples);
2250         }
2251     }
2252
2253     // Downmix primary channel set to stereo
2254     if (s->request_mask != s->ch_mask) {
2255         ff_dca_downmix_to_stereo_fixed(s->dcadsp,
2256                                        s->output_samples,
2257                                        s->prim_dmix_coeff,
2258                                        nsamples, s->ch_mask);
2259     }
2260
2261     for (i = 0; i < avctx->channels; i++) {
2262         int32_t *samples = s->output_samples[s->ch_remap[i]];
2263         int32_t *plane = (int32_t *)frame->extended_data[i];
2264         for (n = 0; n < nsamples; n++)
2265             plane[n] = clip23(samples[n]) * (1 << 8);
2266     }
2267
2268     return 0;
2269 }
2270
2271 static int filter_frame_float(DCACoreDecoder *s, AVFrame *frame)
2272 {
2273     AVCodecContext *avctx = s->avctx;
2274     int x96_nchannels = 0, x96_synth = 0;
2275     int i, n, ch, ret, spkr, nsamples, nchannels;
2276     float *output_samples[DCA_SPEAKER_COUNT] = { NULL }, *ptr;
2277     const float *filter_coeff;
2278
2279     if (s->ext_audio_mask & (DCA_CSS_X96 | DCA_EXSS_X96)) {
2280         x96_nchannels = s->x96_nchannels;
2281         x96_synth = 1;
2282     }
2283
2284     avctx->sample_rate = s->sample_rate << x96_synth;
2285     avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
2286     avctx->bits_per_raw_sample = 0;
2287
2288     frame->nb_samples = nsamples = (s->npcmblocks * DCA_PCMBLOCK_SAMPLES) << x96_synth;
2289     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
2290         return ret;
2291
2292     // Build reverse speaker to channel mapping
2293     for (i = 0; i < avctx->channels; i++)
2294         output_samples[s->ch_remap[i]] = (float *)frame->extended_data[i];
2295
2296     // Allocate space for extra channels
2297     nchannels = av_popcount(s->ch_mask) - avctx->channels;
2298     if (nchannels > 0) {
2299         av_fast_malloc(&s->output_buffer, &s->output_size,
2300                        nsamples * nchannels * sizeof(float));
2301         if (!s->output_buffer)
2302             return AVERROR(ENOMEM);
2303
2304         ptr = (float *)s->output_buffer;
2305         for (spkr = 0; spkr < DCA_SPEAKER_COUNT; spkr++) {
2306             if (!(s->ch_mask & (1U << spkr)))
2307                 continue;
2308             if (output_samples[spkr])
2309                 continue;
2310             output_samples[spkr] = ptr;
2311             ptr += nsamples;
2312         }
2313     }
2314
2315     // Handle change of filtering mode
2316     set_filter_mode(s, x96_synth);
2317
2318     // Select filter
2319     if (x96_synth)
2320         filter_coeff = ff_dca_fir_64bands;
2321     else if (s->filter_perfect)
2322         filter_coeff = ff_dca_fir_32bands_perfect;
2323     else
2324         filter_coeff = ff_dca_fir_32bands_nonperfect;
2325
2326     // Filter primary channels
2327     for (ch = 0; ch < s->nchannels; ch++) {
2328         // Map this primary channel to speaker
2329         spkr = map_prm_ch_to_spkr(s, ch);
2330         if (spkr < 0)
2331             return AVERROR(EINVAL);
2332
2333         // Filter bank reconstruction
2334         s->dcadsp->sub_qmf_float[x96_synth](
2335             &s->synth,
2336             &s->imdct[x96_synth],
2337             output_samples[spkr],
2338             s->subband_samples[ch],
2339             ch < x96_nchannels ? s->x96_subband_samples[ch] : NULL,
2340             s->dcadsp_data[ch].u.flt.hist1,
2341             &s->dcadsp_data[ch].offset,
2342             s->dcadsp_data[ch].u.flt.hist2,
2343             filter_coeff,
2344             s->npcmblocks,
2345             1.0f / (1 << (17 - x96_synth)));
2346     }
2347
2348     // Filter LFE channel
2349     if (s->lfe_present) {
2350         int dec_select = (s->lfe_present == LFE_FLAG_128);
2351         float *samples = output_samples[DCA_SPEAKER_LFE1];
2352         int nlfesamples = s->npcmblocks >> (dec_select + 1);
2353
2354         // Offset intermediate buffer for X96
2355         if (x96_synth)
2356             samples += nsamples / 2;
2357
2358         // Select filter
2359         if (dec_select)
2360             filter_coeff = ff_dca_lfe_fir_128;
2361         else
2362             filter_coeff = ff_dca_lfe_fir_64;
2363
2364         // Interpolate LFE channel
2365         s->dcadsp->lfe_fir_float[dec_select](
2366             samples, s->lfe_samples + DCA_LFE_HISTORY,
2367             filter_coeff, s->npcmblocks);
2368
2369         if (x96_synth) {
2370             // Filter 96 kHz oversampled LFE PCM to attenuate high frequency
2371             // (47.6 - 48.0 kHz) components of interpolation image
2372             s->dcadsp->lfe_x96_float(output_samples[DCA_SPEAKER_LFE1],
2373                                      samples, &s->output_history_lfe_float,
2374                                      nsamples / 2);
2375         }
2376
2377         // Update LFE history
2378         for (n = DCA_LFE_HISTORY - 1; n >= 0; n--)
2379             s->lfe_samples[n] = s->lfe_samples[nlfesamples + n];
2380     }
2381
2382     // Undo embedded XCH downmix
2383     if (s->es_format && (s->ext_audio_mask & DCA_CSS_XCH)
2384         && s->audio_mode >= AMODE_2F2R) {
2385         s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Ls],
2386                                          output_samples[DCA_SPEAKER_Cs],
2387                                          -M_SQRT1_2, nsamples);
2388         s->float_dsp->vector_fmac_scalar(output_samples[DCA_SPEAKER_Rs],
2389                                          output_samples[DCA_SPEAKER_Cs],
2390                                          -M_SQRT1_2, nsamples);
2391     }
2392
2393     // Undo embedded XXCH downmix
2394     if ((s->ext_audio_mask & (DCA_CSS_XXCH | DCA_EXSS_XXCH))
2395         && s->xxch_dmix_embedded) {
2396         float scale_inv = s->xxch_dmix_scale_inv * (1.0f / (1 << 16));
2397         int *coeff_ptr  = s->xxch_dmix_coeff;
2398         int xch_base    = ff_dca_channels[s->audio_mode];
2399         av_assert1(s->nchannels - xch_base <= DCA_XXCH_CHANNELS_MAX);
2400
2401         // Undo downmix
2402         for (ch = xch_base; ch < s->nchannels; ch++) {
2403             int src_spkr = map_prm_ch_to_spkr(s, ch);
2404             if (src_spkr < 0)
2405                 return AVERROR(EINVAL);
2406             for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2407                 if (s->xxch_dmix_mask[ch - xch_base] & (1U << spkr)) {
2408                     int coeff = *coeff_ptr++;
2409                     if (coeff) {
2410                         s->float_dsp->vector_fmac_scalar(output_samples[    spkr],
2411                                                          output_samples[src_spkr],
2412                                                          coeff * (-1.0f / (1 << 15)),
2413                                                          nsamples);
2414                     }
2415                 }
2416             }
2417         }
2418
2419         // Undo embedded core downmix pre-scaling
2420         for (spkr = 0; spkr < s->xxch_mask_nbits; spkr++) {
2421             if (s->xxch_core_mask & (1U << spkr)) {
2422                 s->float_dsp->vector_fmul_scalar(output_samples[spkr],
2423                                                  output_samples[spkr],
2424                                                  scale_inv, nsamples);
2425             }
2426         }
2427     }
2428
2429     if (!(s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH | DCA_EXSS_XXCH))) {
2430         // Front sum/difference decoding
2431         if ((s->sumdiff_front && s->audio_mode > AMODE_MONO)
2432             || s->audio_mode == AMODE_STEREO_SUMDIFF) {
2433             s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_L],
2434                                             output_samples[DCA_SPEAKER_R],
2435                                             nsamples);
2436         }
2437
2438         // Surround sum/difference decoding
2439         if (s->sumdiff_surround && s->audio_mode >= AMODE_2F2R) {
2440             s->float_dsp->butterflies_float(output_samples[DCA_SPEAKER_Ls],
2441                                             output_samples[DCA_SPEAKER_Rs],
2442                                             nsamples);
2443         }
2444     }
2445
2446     // Downmix primary channel set to stereo
2447     if (s->request_mask != s->ch_mask) {
2448         ff_dca_downmix_to_stereo_float(s->float_dsp, output_samples,
2449                                        s->prim_dmix_coeff,
2450                                        nsamples, s->ch_mask);
2451     }
2452
2453     return 0;
2454 }
2455
2456 int ff_dca_core_filter_frame(DCACoreDecoder *s, AVFrame *frame)
2457 {
2458     AVCodecContext *avctx = s->avctx;
2459     DCAContext *dca = avctx->priv_data;
2460     DCAExssAsset *asset = &dca->exss.assets[0];
2461     enum AVMatrixEncoding matrix_encoding;
2462     int ret;
2463
2464     // Handle downmixing to stereo request
2465     if (dca->request_channel_layout == DCA_SPEAKER_LAYOUT_STEREO
2466         && s->audio_mode > AMODE_MONO && s->prim_dmix_embedded
2467         && (s->prim_dmix_type == DCA_DMIX_TYPE_LoRo ||
2468             s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
2469         s->request_mask = DCA_SPEAKER_LAYOUT_STEREO;
2470     else
2471         s->request_mask = s->ch_mask;
2472     if (!ff_dca_set_channel_layout(avctx, s->ch_remap, s->request_mask))
2473         return AVERROR(EINVAL);
2474
2475     // Force fixed point mode when falling back from XLL
2476     if ((avctx->flags & AV_CODEC_FLAG_BITEXACT) || ((dca->packet & DCA_PACKET_EXSS)
2477                                                     && (asset->extension_mask & DCA_EXSS_XLL)))
2478         ret = filter_frame_fixed(s, frame);
2479     else
2480         ret = filter_frame_float(s, frame);
2481     if (ret < 0)
2482         return ret;
2483
2484     // Set profile, bit rate, etc
2485     if (s->ext_audio_mask & DCA_EXSS_MASK)
2486         avctx->profile = FF_PROFILE_DTS_HD_HRA;
2487     else if (s->ext_audio_mask & (DCA_CSS_XXCH | DCA_CSS_XCH))
2488         avctx->profile = FF_PROFILE_DTS_ES;
2489     else if (s->ext_audio_mask & DCA_CSS_X96)
2490         avctx->profile = FF_PROFILE_DTS_96_24;
2491     else
2492         avctx->profile = FF_PROFILE_DTS;
2493
2494     if (s->bit_rate > 3 && !(s->ext_audio_mask & DCA_EXSS_MASK))
2495         avctx->bit_rate = s->bit_rate;
2496     else
2497         avctx->bit_rate = 0;
2498
2499     if (s->audio_mode == AMODE_STEREO_TOTAL || (s->request_mask != s->ch_mask &&
2500                                                 s->prim_dmix_type == DCA_DMIX_TYPE_LtRt))
2501         matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
2502     else
2503         matrix_encoding = AV_MATRIX_ENCODING_NONE;
2504     if ((ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
2505         return ret;
2506
2507     return 0;
2508 }
2509
2510 av_cold void ff_dca_core_flush(DCACoreDecoder *s)
2511 {
2512     if (s->subband_buffer) {
2513         erase_adpcm_history(s);
2514         memset(s->lfe_samples, 0, DCA_LFE_HISTORY * sizeof(int32_t));
2515     }
2516
2517     if (s->x96_subband_buffer)
2518         erase_x96_adpcm_history(s);
2519
2520     erase_dsp_history(s);
2521 }
2522
2523 av_cold int ff_dca_core_init(DCACoreDecoder *s)
2524 {
2525     if (!(s->float_dsp = avpriv_float_dsp_alloc(0)))
2526         return -1;
2527     if (!(s->fixed_dsp = avpriv_alloc_fixed_dsp(0)))
2528         return -1;
2529
2530     ff_dcadct_init(&s->dcadct);
2531     if (ff_mdct_init(&s->imdct[0], 6, 1, 1.0) < 0)
2532         return -1;
2533     if (ff_mdct_init(&s->imdct[1], 7, 1, 1.0) < 0)
2534         return -1;
2535     ff_synth_filter_init(&s->synth);
2536
2537     s->x96_rand = 1;
2538     return 0;
2539 }
2540
2541 av_cold void ff_dca_core_close(DCACoreDecoder *s)
2542 {
2543     av_freep(&s->float_dsp);
2544     av_freep(&s->fixed_dsp);
2545
2546     ff_mdct_end(&s->imdct[0]);
2547     ff_mdct_end(&s->imdct[1]);
2548
2549     av_freep(&s->subband_buffer);
2550     s->subband_size = 0;
2551
2552     av_freep(&s->x96_subband_buffer);
2553     s->x96_subband_size = 0;
2554
2555     av_freep(&s->output_buffer);
2556     s->output_size = 0;
2557 }