Coverage Report

Created: 2025-08-28 07:12

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