Coverage Report

Created: 2026-07-25 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/hcadec.c
Line
Count
Source
1
/*
2
 * This file is part of FFmpeg.
3
 *
4
 * FFmpeg is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * FFmpeg is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with FFmpeg; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
 */
18
19
#include "libavutil/crc.h"
20
#include "libavutil/float_dsp.h"
21
#include "libavutil/mem.h"
22
#include "libavutil/mem_internal.h"
23
#include "libavutil/tx.h"
24
25
#include "avcodec.h"
26
#include "bytestream.h"
27
#include "codec_internal.h"
28
#include "decode.h"
29
#include "get_bits.h"
30
#include "hca_data.h"
31
32
91.0k
#define HCA_MASK 0x7f7f7f7f
33
1.70M
#define MAX_CHANNELS 16
34
35
typedef struct ChannelContext {
36
    DECLARE_ALIGNED(32, float, base)[128];
37
    DECLARE_ALIGNED(32, float, factors)[128];
38
    DECLARE_ALIGNED(32, float, imdct_in)[128];
39
    DECLARE_ALIGNED(32, float, imdct_out)[128];
40
    DECLARE_ALIGNED(32, float, imdct_prev)[128];
41
    int8_t   scale_factors[128];
42
    uint8_t  scale[128];
43
    int8_t   intensity[8];
44
    int8_t  *hfr_scale;
45
    unsigned count;
46
    int      chan_type;
47
} ChannelContext;
48
49
typedef struct HCAContext {
50
    const AVCRC *crc_table;
51
52
    ChannelContext ch[MAX_CHANNELS];
53
54
    uint8_t ath[128];
55
    uint8_t cipher[256];
56
    uint64_t key;
57
    uint16_t subkey;
58
59
    int     ath_type;
60
    int     ciph_type;
61
    unsigned hfr_group_count;
62
    uint8_t track_count;
63
    uint8_t channel_config;
64
    uint8_t total_band_count;
65
    uint8_t base_band_count;
66
    uint8_t stereo_band_count;
67
    uint8_t bands_per_hfr_group;
68
69
    // Set during init() and freed on close(). Untouched on init_flush()
70
    av_tx_fn           tx_fn;
71
    AVTXContext       *tx_ctx;
72
    AVFloatDSPContext *fdsp;
73
} HCAContext;
74
75
static void cipher_init56_create_table(uint8_t *r, uint8_t key)
76
73.0k
{
77
73.0k
    const int mul = ((key & 1) << 3) | 5;
78
73.0k
    const int add = (key & 0xE) | 1;
79
80
73.0k
    key >>= 4;
81
1.24M
    for (int i = 0; i < 16; i++) {
82
1.16M
        key = (key * mul + add) & 0xF;
83
1.16M
        r[i] = key;
84
1.16M
    }
85
73.0k
}
86
87
static void cipher_init56(uint8_t *cipher, uint64_t keycode)
88
4.29k
{
89
4.29k
    uint8_t base[256], base_r[16], base_c[16], kc[8], seed[16];
90
91
    /* 56bit keycode encryption (given as a uint64_t number, but upper 8b aren't used) */
92
    /* keycode = keycode - 1 */
93
4.29k
    if (keycode != 0)
94
4.10k
        keycode--;
95
96
    /* init keycode table */
97
34.3k
    for (int r = 0; r < (8-1); r++) {
98
30.0k
        kc[r] = keycode & 0xFF;
99
30.0k
        keycode = keycode >> 8;
100
30.0k
    }
101
102
    /* init seed table */
103
4.29k
    seed[ 0] = kc[1];
104
4.29k
    seed[ 1] = kc[1] ^ kc[6];
105
4.29k
    seed[ 2] = kc[2] ^ kc[3];
106
4.29k
    seed[ 3] = kc[2];
107
4.29k
    seed[ 4] = kc[2] ^ kc[1];
108
4.29k
    seed[ 5] = kc[3] ^ kc[4];
109
4.29k
    seed[ 6] = kc[3];
110
4.29k
    seed[ 7] = kc[3] ^ kc[2];
111
4.29k
    seed[ 8] = kc[4] ^ kc[5];
112
4.29k
    seed[ 9] = kc[4];
113
4.29k
    seed[10] = kc[4] ^ kc[3];
114
4.29k
    seed[11] = kc[5] ^ kc[6];
115
4.29k
    seed[12] = kc[5];
116
4.29k
    seed[13] = kc[5] ^ kc[4];
117
4.29k
    seed[14] = kc[6] ^ kc[1];
118
4.29k
    seed[15] = kc[6];
119
120
    /* init base table */
121
4.29k
    cipher_init56_create_table(base_r, kc[0]);
122
73.0k
    for (int r = 0; r < 16; r++) {
123
68.7k
        uint8_t nb;
124
68.7k
        cipher_init56_create_table(base_c, seed[r]);
125
68.7k
        nb = base_r[r] << 4;
126
1.16M
        for (int c = 0; c < 16; c++)
127
1.09M
            base[r*16 + c] = nb | base_c[c]; /* combine nibbles */
128
68.7k
    }
129
130
    /* final shuffle table */
131
4.29k
    {
132
4.29k
        unsigned x = 0;
133
4.29k
        unsigned pos = 1;
134
135
1.10M
        for (int i = 0; i < 256; i++) {
136
1.09M
            x = (x + 17) & 0xFF;
137
1.09M
            if (base[x] != 0 && base[x] != 0xFF)
138
1.09M
                cipher[pos++] = base[x];
139
1.09M
        }
140
4.29k
        cipher[0] = 0;
141
4.29k
        cipher[0xFF] = 0xFF;
142
4.29k
    }
143
4.29k
}
144
145
static void cipher_init(uint8_t *cipher, int type, uint64_t keycode, uint16_t subkey)
146
16.3k
{
147
16.3k
    switch (type) {
148
4.52k
    case 56:
149
4.52k
        if (keycode) {
150
4.29k
            if (subkey)
151
3.84k
                keycode = keycode * (((uint64_t)subkey<<16u)|((uint16_t)~subkey+2u));
152
4.29k
            cipher_init56(cipher, keycode);
153
4.29k
        }
154
4.52k
        break;
155
10.8k
    case 0:
156
2.79M
        for (int i = 0; i < 256; i++)
157
2.78M
            cipher[i] = i;
158
10.8k
        break;
159
16.3k
    }
160
16.3k
}
161
162
static void ath_init1(uint8_t *ath, int sample_rate)
163
3.17k
{
164
3.17k
    unsigned int index;
165
3.17k
    unsigned int acc = 0;
166
167
137k
    for (int i = 0; i < 128; i++) {
168
136k
        acc += sample_rate;
169
136k
        index = acc >> 13;
170
171
136k
        if (index >= 654) {
172
2.16k
            memset(ath+i, 0xFF, (128 - i));
173
2.16k
            break;
174
2.16k
        }
175
176
134k
        ath[i] = ath_base_curve[index];
177
134k
    }
178
3.17k
}
179
180
static int ath_init(uint8_t *ath, int type, int sample_rate)
181
16.3k
{
182
16.3k
    switch (type) {
183
12.6k
    case 0:
184
        /* nothing to do */
185
12.6k
        break;
186
3.17k
    case 1:
187
3.17k
        ath_init1(ath, sample_rate);
188
3.17k
        break;
189
504
    default:
190
504
        return AVERROR_INVALIDDATA;
191
16.3k
    }
192
193
15.8k
    return 0;
194
16.3k
}
195
196
static inline unsigned ceil2(unsigned a, unsigned b)
197
13.1k
{
198
13.1k
    return (b > 0) ? (a / b + ((a % b) ? 1 : 0)) : 0;
199
13.1k
}
200
201
static av_cold void init_flush(AVCodecContext *avctx)
202
17.9k
{
203
17.9k
    HCAContext *c = avctx->priv_data;
204
205
17.9k
    memset(c, 0, offsetof(HCAContext, tx_fn));
206
17.9k
}
207
208
static int init_hca(AVCodecContext *avctx, const uint8_t *extradata,
209
                    const int extradata_size)
210
17.9k
{
211
17.9k
    HCAContext *c = avctx->priv_data;
212
17.9k
    GetByteContext gb0, *const gb = &gb0;
213
17.9k
    int8_t r[16] = { 0 };
214
17.9k
    unsigned b, chunk;
215
17.9k
    int version, ret;
216
17.9k
    unsigned hfr_group_count;
217
218
17.9k
    init_flush(avctx);
219
220
17.9k
    if (extradata_size < 36)
221
290
        return AVERROR_INVALIDDATA;
222
223
17.6k
    bytestream2_init(gb, extradata, extradata_size);
224
225
17.6k
    bytestream2_skipu(gb, 4);
226
17.6k
    version = bytestream2_get_be16(gb);
227
17.6k
    bytestream2_skipu(gb, 2);
228
229
17.6k
    c->ath_type = version >= 0x200 ? 0 : 1;
230
231
17.6k
    if ((bytestream2_get_be32u(gb) & HCA_MASK) != MKBETAG('f', 'm', 't', 0))
232
486
        return AVERROR_INVALIDDATA;
233
17.1k
    bytestream2_skipu(gb, 4);
234
17.1k
    bytestream2_skipu(gb, 4);
235
17.1k
    bytestream2_skipu(gb, 4);
236
237
17.1k
    chunk = bytestream2_get_be32u(gb) & HCA_MASK;
238
17.1k
    if (chunk == MKBETAG('c', 'o', 'm', 'p')) {
239
6.63k
        bytestream2_skipu(gb, 2);
240
6.63k
        bytestream2_skipu(gb, 1);
241
6.63k
        bytestream2_skipu(gb, 1);
242
6.63k
        c->track_count         = bytestream2_get_byteu(gb);
243
6.63k
        c->channel_config      = bytestream2_get_byteu(gb);
244
6.63k
        c->total_band_count    = bytestream2_get_byteu(gb);
245
6.63k
        c->base_band_count     = bytestream2_get_byteu(gb);
246
6.63k
        c->stereo_band_count   = bytestream2_get_byte (gb);
247
6.63k
        c->bands_per_hfr_group = bytestream2_get_byte (gb);
248
6.63k
        bytestream2_skipu(gb, 2);
249
10.5k
    } else if (chunk == MKBETAG('d', 'e', 'c', 0)) {
250
9.89k
        bytestream2_skipu(gb, 2);
251
9.89k
        bytestream2_skipu(gb, 1);
252
9.89k
        bytestream2_skipu(gb, 1);
253
9.89k
        c->total_band_count = bytestream2_get_byteu(gb) + 1;
254
9.89k
        c->base_band_count  = bytestream2_get_byteu(gb) + 1;
255
9.89k
        c->track_count      = bytestream2_peek_byteu(gb) >> 4;
256
9.89k
        c->channel_config   = bytestream2_get_byteu(gb) & 0xF;
257
9.89k
        if (!bytestream2_get_byteu(gb))
258
1.11k
            c->base_band_count = c->total_band_count;
259
9.89k
        c->stereo_band_count = c->total_band_count - c->base_band_count;
260
9.89k
        c->bands_per_hfr_group = 0;
261
9.89k
    } else
262
642
        return AVERROR_INVALIDDATA;
263
264
16.5k
    if (c->total_band_count > FF_ARRAY_ELEMS(c->ch->imdct_in))
265
223
        return AVERROR_INVALIDDATA;
266
267
65.1k
    while (bytestream2_get_bytes_left(gb) >= 4) {
268
56.1k
        chunk = bytestream2_get_be32u(gb) & HCA_MASK;
269
56.1k
        if (chunk == MKBETAG('v', 'b', 'r', 0)) {
270
7.37k
            bytestream2_skip(gb, 2 + 2);
271
48.7k
        } else if (chunk == MKBETAG('a', 't', 'h', 0)) {
272
10.7k
            c->ath_type = bytestream2_get_be16(gb);
273
38.0k
        } else if (chunk == MKBETAG('r', 'v', 'a', 0)) {
274
5.82k
            bytestream2_skip(gb, 4);
275
32.2k
        } else if (chunk == MKBETAG('c', 'o', 'm', 'm')) {
276
659
            bytestream2_skip(gb, bytestream2_get_byte(gb) * 8);
277
31.5k
        } else if (chunk == MKBETAG('c', 'i', 'p', 'h')) {
278
23.5k
            c->ciph_type = bytestream2_get_be16(gb);
279
23.5k
        } else if (chunk == MKBETAG('l', 'o', 'o', 'p')) {
280
721
            bytestream2_skip(gb, 4 + 4 + 2 + 2);
281
7.28k
        } else if (chunk == MKBETAG('p', 'a', 'd', 0)) {
282
0
            break;
283
7.28k
        } else {
284
7.28k
            break;
285
7.28k
        }
286
56.1k
    }
287
288
16.3k
    if (bytestream2_get_bytes_left(gb) >= 10) {
289
5.39k
        bytestream2_skip(gb, bytestream2_get_bytes_left(gb) - 10);
290
5.39k
        c->key = bytestream2_get_be64u(gb);
291
5.39k
        c->subkey = bytestream2_get_be16u(gb);
292
5.39k
    }
293
294
16.3k
    cipher_init(c->cipher, c->ciph_type, c->key, c->subkey);
295
296
16.3k
    ret = ath_init(c->ath, c->ath_type, avctx->sample_rate);
297
16.3k
    if (ret < 0)
298
504
        return ret;
299
300
15.8k
    if (!c->track_count)
301
8.57k
        c->track_count = 1;
302
303
15.8k
    b = avctx->ch_layout.nb_channels / c->track_count;
304
15.8k
    if (c->stereo_band_count && b > 1) {
305
7.99k
        int8_t *x = r;
306
307
18.4k
        for (int i = 0; i < c->track_count; i++, x+=b) {
308
10.4k
            switch (b) {
309
819
            case 2:
310
2.54k
            case 3:
311
2.54k
                x[0] = 1;
312
2.54k
                x[1] = 2;
313
2.54k
                break;
314
696
            case 4:
315
696
                x[0]=1; x[1] = 2;
316
696
                if (c->channel_config == 0) {
317
219
                    x[2]=1;
318
219
                    x[3]=2;
319
219
                }
320
696
                break;
321
1.07k
            case 5:
322
1.07k
                x[0]=1; x[1] = 2;
323
1.07k
                if (c->channel_config <= 2) {
324
321
                    x[3]=1;
325
321
                    x[4]=2;
326
321
                }
327
1.07k
                break;
328
468
            case 6:
329
758
            case 7:
330
758
                x[0] = 1; x[1] = 2; x[4] = 1; x[5] = 2;
331
758
                break;
332
207
            case 8:
333
207
                x[0] = 1; x[1] = 2; x[4] = 1; x[5] = 2; x[6] = 1; x[7] = 2;
334
207
                break;
335
10.4k
            }
336
10.4k
        }
337
7.99k
    }
338
339
15.8k
    if (c->total_band_count < c->base_band_count)
340
2.70k
        return AVERROR_INVALIDDATA;
341
342
13.1k
    hfr_group_count = ceil2(c->total_band_count - (c->base_band_count + c->stereo_band_count),
343
13.1k
                               c->bands_per_hfr_group);
344
345
13.1k
    if (c->base_band_count + c->stereo_band_count + (uint64_t)hfr_group_count > 128ULL)
346
549
        return AVERROR_INVALIDDATA;
347
12.5k
    c->hfr_group_count = hfr_group_count;
348
349
122k
    for (int i = 0; i < avctx->ch_layout.nb_channels; i++) {
350
109k
        c->ch[i].chan_type = r[i];
351
109k
        c->ch[i].count     = c->base_band_count + ((r[i] != 2) ? c->stereo_band_count : 0);
352
109k
        c->ch[i].hfr_scale = &c->ch[i].scale_factors[c->base_band_count + c->stereo_band_count];
353
109k
        if (c->ch[i].count > 128)
354
0
            return AVERROR_INVALIDDATA;
355
109k
    }
356
357
    // Done last to signal init() finished
358
12.5k
    c->crc_table = av_crc_get_table(AV_CRC_16_ANSI);
359
360
12.5k
    return 0;
361
12.5k
}
362
363
static av_cold int decode_init(AVCodecContext *avctx)
364
1.26k
{
365
1.26k
    HCAContext *c = avctx->priv_data;
366
1.26k
    float scale = 1.f / 8.f;
367
1.26k
    int ret;
368
369
1.26k
    avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
370
371
1.26k
    if (avctx->ch_layout.nb_channels <= 0 || avctx->ch_layout.nb_channels > FF_ARRAY_ELEMS(c->ch))
372
39
        return AVERROR(EINVAL);
373
374
1.23k
    c->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
375
1.23k
    if (!c->fdsp)
376
0
        return AVERROR(ENOMEM);
377
378
1.23k
    ret = av_tx_init(&c->tx_ctx, &c->tx_fn, AV_TX_FLOAT_MDCT, 1, 128, &scale, 0);
379
1.23k
    if (ret < 0)
380
0
        return ret;
381
382
1.23k
    if (avctx->extradata_size != 0 && avctx->extradata_size < 36)
383
6
        return AVERROR_INVALIDDATA;
384
385
1.22k
    if (!avctx->extradata_size)
386
1.00k
        return 0;
387
388
222
    return init_hca(avctx, avctx->extradata, avctx->extradata_size);
389
1.22k
}
390
391
static void run_imdct(HCAContext *c, ChannelContext *ch, int index, float *out)
392
1.41M
{
393
1.41M
    c->tx_fn(c->tx_ctx, ch->imdct_out, ch->imdct_in, sizeof(float));
394
395
1.41M
    c->fdsp->vector_fmul_window(out, ch->imdct_prev + (128 >> 1),
396
1.41M
                                ch->imdct_out, window, 128 >> 1);
397
398
1.41M
    memcpy(ch->imdct_prev, ch->imdct_out, 128 * sizeof(float));
399
1.41M
}
400
401
static void apply_intensity_stereo(HCAContext *s, ChannelContext *ch1, ChannelContext *ch2,
402
                                   int index, unsigned band_count, unsigned base_band_count,
403
                                   unsigned stereo_band_count)
404
555k
{
405
555k
    float ratio_l = intensity_ratio_table[ch2->intensity[index]];
406
555k
    float ratio_r = ratio_l - 2.0f;
407
555k
    float *c1 = &ch1->imdct_in[base_band_count];
408
555k
    float *c2 = &ch2->imdct_in[base_band_count];
409
410
555k
    if (ch1->chan_type != 1 || !stereo_band_count)
411
456k
        return;
412
413
6.30M
    for (int i = 0; i < band_count; i++) {
414
6.20M
        c2[i]  = c1[i] * ratio_r;
415
6.20M
        c1[i] *= ratio_l;
416
6.20M
    }
417
99.4k
}
418
419
static void reconstruct_hfr(HCAContext *s, ChannelContext *ch,
420
                            unsigned hfr_group_count,
421
                            unsigned bands_per_hfr_group,
422
                            unsigned start_band, unsigned total_band_count)
423
1.41M
{
424
1.41M
    if (ch->chan_type == 2 || !bands_per_hfr_group)
425
563k
        return;
426
427
1.65M
    for (int i = 0, k = start_band, l = start_band - 1; i < hfr_group_count; i++){
428
1.88M
        for (int j = 0; j < bands_per_hfr_group && k < total_band_count && l >= 0; j++, k++, l--){
429
1.07M
            ch->imdct_in[k] = scale_conversion_table[ scale_conv_bias +
430
1.07M
                av_clip_intp2(ch->hfr_scale[i] - ch->scale_factors[l], 6) ] * ch->imdct_in[l];
431
1.07M
        }
432
803k
    }
433
434
852k
    ch->imdct_in[127] = 0;
435
852k
}
436
437
static void dequantize_coefficients(HCAContext *c, ChannelContext *ch,
438
                                    GetBitContext *gb)
439
1.41M
{
440
1.41M
    const float *base = ch->base;
441
1.41M
    float *factors = ch->factors;
442
1.41M
    float *out = ch->imdct_in;
443
444
17.3M
    for (int i = 0; i < ch->count; i++) {
445
15.9M
        unsigned scale = ch->scale[i];
446
15.9M
        int nb_bits = max_bits_table[scale];
447
15.9M
        int value = get_bitsz(gb, nb_bits);
448
15.9M
        float factor;
449
450
15.9M
        if (scale > 7) {
451
757k
            value = (1 - ((value & 1) << 1)) * (value >> 1);
452
757k
            if (!value)
453
582k
                skip_bits_long(gb, -1);
454
757k
            factor = value;
455
15.1M
        } else {
456
15.1M
            value += scale << 4;
457
15.1M
            skip_bits_long(gb, quant_spectrum_bits[value] - nb_bits);
458
15.1M
            factor = quant_spectrum_value[value];
459
15.1M
        }
460
15.9M
        factors[i] = factor;
461
15.9M
    }
462
463
1.41M
    memset(factors + ch->count, 0, 512 - ch->count * sizeof(*factors));
464
1.41M
    c->fdsp->vector_fmul(out, factors, base, 128);
465
1.41M
}
466
467
static void unpack(HCAContext *c, ChannelContext *ch,
468
                   GetBitContext *gb,
469
                   unsigned hfr_group_count,
470
                   int packed_noise_level,
471
                   const uint8_t *ath)
472
176k
{
473
176k
    int delta_bits = get_bits(gb, 3);
474
475
176k
    if (delta_bits > 5) {
476
60.9k
        for (int i = 0; i < ch->count; i++)
477
57.0k
            ch->scale_factors[i] = get_bits(gb, 6);
478
173k
    } else if (delta_bits) {
479
123k
        int factor = get_bits(gb, 6);
480
123k
        int max_value = (1 << delta_bits) - 1;
481
123k
        int half_max = max_value >> 1;
482
483
123k
        ch->scale_factors[0] = factor;
484
478k
        for (int i = 1; i < ch->count; i++){
485
354k
            int delta = get_bits(gb, delta_bits);
486
487
354k
            if (delta == max_value) {
488
29.5k
                factor = get_bits(gb, 6);
489
325k
            } else {
490
325k
                factor += delta - half_max;
491
325k
            }
492
354k
            factor = av_clip_uintp2(factor, 6);
493
494
354k
            ch->scale_factors[i] = factor;
495
354k
        }
496
123k
    } else {
497
49.5k
        memset(ch->scale_factors, 0, 128);
498
49.5k
    }
499
500
176k
    if (ch->chan_type == 2){
501
12.4k
        ch->intensity[0] = get_bits(gb, 4);
502
12.4k
        if (ch->intensity[0] < 15) {
503
95.4k
            for (int i = 1; i < 8; i++)
504
83.5k
                ch->intensity[i] = get_bits(gb, 4);
505
11.9k
        }
506
164k
    } else {
507
264k
        for (int i = 0; i < hfr_group_count; i++)
508
100k
            ch->hfr_scale[i] = get_bits(gb, 6);
509
164k
    }
510
511
2.16M
    for (int i = 0; i < ch->count; i++) {
512
1.99M
        int scale = ch->scale_factors[i];
513
514
1.99M
        if (scale) {
515
259k
            scale = c->ath[i] + ((packed_noise_level + i) >> 8) - ((scale * 5) >> 1) + 2;
516
259k
            scale = scale_table[av_clip(scale, 0, 58)];
517
259k
        }
518
1.99M
        ch->scale[i] = scale;
519
1.99M
    }
520
521
176k
    memset(ch->scale + ch->count, 0, sizeof(ch->scale) - ch->count);
522
523
2.16M
    for (int i = 0; i < ch->count; i++)
524
1.99M
        ch->base[i] = dequantizer_scaling_table[ch->scale_factors[i]] * quant_step_size[ch->scale[i]];
525
176k
}
526
527
static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
528
                        int *got_frame_ptr, AVPacket *avpkt)
529
254k
{
530
254k
    HCAContext *c = avctx->priv_data;
531
254k
    int ch, offset = 0, ret, packed_noise_level;
532
254k
    GetBitContext gb0, *const gb = &gb0;
533
254k
    float **samples;
534
535
254k
    if (avpkt->size <= 8)
536
117k
        return AVERROR_INVALIDDATA;
537
538
137k
    if (AV_RN16(avpkt->data) != 0xFFFF) {
539
26.2k
        if ((AV_RL32(avpkt->data)) != MKTAG('H','C','A',0)) {
540
7.23k
            return AVERROR_INVALIDDATA;
541
19.0k
        } else if (AV_RB16(avpkt->data + 6) <= avpkt->size) {
542
17.7k
            ret = init_hca(avctx, avpkt->data, AV_RB16(avpkt->data + 6));
543
17.7k
            if (ret < 0) {
544
5.37k
                c->crc_table = NULL; // signal that init has not finished
545
5.37k
                return ret;
546
5.37k
            }
547
12.3k
            offset = AV_RB16(avpkt->data + 6);
548
12.3k
            if (offset == avpkt->size)
549
3.22k
                return avpkt->size;
550
12.3k
        } else {
551
1.30k
            return AVERROR_INVALIDDATA;
552
1.30k
        }
553
26.2k
    }
554
555
120k
    if (!c->crc_table)
556
2.48k
        return AVERROR_INVALIDDATA;
557
558
117k
    if (c->key || c->subkey) {
559
99.9k
        uint8_t *data, *cipher = c->cipher;
560
561
99.9k
        if ((ret = av_packet_make_writable(avpkt)) < 0)
562
0
            return ret;
563
99.9k
        data = avpkt->data;
564
4.66M
        for (int n = 0; n < avpkt->size; n++)
565
4.56M
            data[n] = cipher[data[n]];
566
99.9k
    }
567
568
117k
    if (avctx->err_recognition & AV_EF_CRCCHECK) {
569
4.27k
        if (av_crc(c->crc_table, 0, avpkt->data + offset, avpkt->size - offset))
570
3.94k
            return AVERROR_INVALIDDATA;
571
4.27k
    }
572
573
113k
    if ((ret = init_get_bits8(gb, avpkt->data + offset, avpkt->size - offset)) < 0)
574
0
        return ret;
575
576
113k
    if (get_bits(gb, 16) != 0xFFFF)
577
6.24k
        return AVERROR_INVALIDDATA;
578
579
107k
    frame->nb_samples = 1024;
580
107k
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
581
0
        return ret;
582
107k
    samples = (float **)frame->extended_data;
583
584
107k
    packed_noise_level = (get_bits(gb, 9) << 8) - get_bits(gb, 7);
585
586
284k
    for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++)
587
176k
        unpack(c, &c->ch[ch], gb, c->hfr_group_count, packed_noise_level, c->ath);
588
589
967k
    for (int i = 0; i < 8; i++) {
590
2.27M
        for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++)
591
1.41M
            dequantize_coefficients(c, &c->ch[ch], gb);
592
2.27M
        for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++)
593
1.41M
            reconstruct_hfr(c, &c->ch[ch], c->hfr_group_count, c->bands_per_hfr_group,
594
1.41M
                            c->stereo_band_count + c->base_band_count, c->total_band_count);
595
1.41M
        for (ch = 0; ch < avctx->ch_layout.nb_channels - 1; ch++)
596
555k
            apply_intensity_stereo(c, &c->ch[ch], &c->ch[ch+1], i,
597
555k
                                   c->total_band_count - c->base_band_count,
598
555k
                                   c->base_band_count, c->stereo_band_count);
599
2.27M
        for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++)
600
1.41M
            run_imdct(c, &c->ch[ch], i, samples[ch] + i * 128);
601
859k
    }
602
603
107k
    *got_frame_ptr = 1;
604
605
107k
    return avpkt->size;
606
107k
}
607
608
static av_cold int decode_close(AVCodecContext *avctx)
609
1.26k
{
610
1.26k
    HCAContext *c = avctx->priv_data;
611
612
1.26k
    av_freep(&c->fdsp);
613
1.26k
    av_tx_uninit(&c->tx_ctx);
614
615
1.26k
    return 0;
616
1.26k
}
617
618
static av_cold void decode_flush(AVCodecContext *avctx)
619
100k
{
620
100k
    HCAContext *c = avctx->priv_data;
621
622
1.70M
    for (int ch = 0; ch < MAX_CHANNELS; ch++)
623
1.60M
        memset(c->ch[ch].imdct_prev, 0, sizeof(c->ch[ch].imdct_prev));
624
100k
}
625
626
const FFCodec ff_hca_decoder = {
627
    .p.name         = "hca",
628
    CODEC_LONG_NAME("CRI HCA"),
629
    .p.type         = AVMEDIA_TYPE_AUDIO,
630
    .p.id           = AV_CODEC_ID_HCA,
631
    .priv_data_size = sizeof(HCAContext),
632
    .init           = decode_init,
633
    FF_CODEC_DECODE_CB(decode_frame),
634
    .flush          = decode_flush,
635
    .close          = decode_close,
636
    .p.capabilities = AV_CODEC_CAP_DR1,
637
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
638
};