OSDN Git Service

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