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