Coverage Report

Created: 2026-01-16 07:48

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