OSDN Git Service

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