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