GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/hcadec.c Lines: 0 248 0.0 %
Date: 2020-11-28 20:53:16 Branches: 0 145 0.0 %

Line Branch Exec 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/intreadwrite.h"
22
#include "libavutil/tx.h"
23
24
#include "avcodec.h"
25
#include "get_bits.h"
26
#include "internal.h"
27
#include "hca_data.h"
28
29
typedef struct ChannelContext {
30
    float    base[128];
31
    DECLARE_ALIGNED(32, float, imdct_in)[128];
32
    DECLARE_ALIGNED(32, float, imdct_out)[128];
33
    DECLARE_ALIGNED(32, float, imdct_prev)[128];
34
    int8_t   scale_factors[128];
35
    uint8_t  scale[128];
36
    int8_t   intensity[8];
37
    int8_t  *hfr_scale;
38
    unsigned count;
39
    int      chan_type;
40
} ChannelContext;
41
42
typedef struct HCAContext {
43
    GetBitContext gb;
44
45
    const AVCRC *crc_table;
46
47
    ChannelContext ch[16];
48
49
    uint8_t ath[128];
50
51
    int     ath_type;
52
    unsigned hfr_group_count;
53
    uint8_t track_count;
54
    uint8_t channel_config;
55
    uint8_t total_band_count;
56
    uint8_t base_band_count;
57
    uint8_t stereo_band_count;
58
    uint8_t bands_per_hfr_group;
59
60
    av_tx_fn           tx_fn;
61
    AVTXContext       *tx_ctx;
62
    AVFloatDSPContext *fdsp;
63
} HCAContext;
64
65
static void ath_init1(uint8_t *ath, int sample_rate)
66
{
67
    unsigned int index;
68
    unsigned int acc = 0;
69
70
    for (int i = 0; i < 128; i++) {
71
        acc += sample_rate;
72
        index = acc >> 13;
73
74
        if (index >= 654) {
75
            memset(ath+i, 0xFF, (128 - i));
76
            break;
77
        }
78
79
        ath[i] = ath_base_curve[index];
80
    }
81
}
82
83
static int ath_init(uint8_t *ath, int type, int sample_rate)
84
{
85
    switch (type) {
86
    case 0:
87
        /* nothing to do */
88
        break;
89
    case 1:
90
        ath_init1(ath, sample_rate);
91
        break;
92
    default:
93
        return AVERROR_INVALIDDATA;
94
    }
95
96
    return 0;
97
}
98
99
static inline unsigned ceil2(unsigned a, unsigned b)
100
{
101
    return (b > 0) ? (a / b + ((a % b) ? 1 : 0)) : 0;
102
}
103
104
static av_cold int decode_init(AVCodecContext *avctx)
105
{
106
    HCAContext *c = avctx->priv_data;
107
    GetBitContext *gb = &c->gb;
108
    int8_t r[16] = { 0 };
109
    float scale = 1.f / 8.f;
110
    unsigned b, chunk;
111
    int version, ret;
112
113
    avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
114
    c->crc_table = av_crc_get_table(AV_CRC_16_ANSI);
115
116
    if (avctx->channels <= 0 || avctx->channels > 16)
117
        return AVERROR(EINVAL);
118
119
    ret = init_get_bits8(gb, avctx->extradata, avctx->extradata_size);
120
    if (ret < 0)
121
        return ret;
122
    skip_bits_long(gb, 32);
123
    version = get_bits(gb, 16);
124
    skip_bits_long(gb, 16);
125
126
    c->ath_type = version >= 0x200 ? 0 : 1;
127
128
    if (get_bits_long(gb, 32) != MKBETAG('f', 'm', 't', 0))
129
        return AVERROR_INVALIDDATA;
130
    skip_bits_long(gb, 32);
131
    skip_bits_long(gb, 32);
132
    skip_bits_long(gb, 32);
133
134
    chunk = get_bits_long(gb, 32);
135
    if (chunk == MKBETAG('c', 'o', 'm', 'p')) {
136
        skip_bits_long(gb, 16);
137
        skip_bits_long(gb, 8);
138
        skip_bits_long(gb, 8);
139
        c->track_count = get_bits(gb, 8);
140
        c->channel_config = get_bits(gb, 8);
141
        c->total_band_count = get_bits(gb, 8);
142
        c->base_band_count = get_bits(gb, 8);
143
        c->stereo_band_count = get_bits(gb, 8);
144
        c->bands_per_hfr_group = get_bits(gb, 8);
145
    } else if (chunk == MKBETAG('d', 'e', 'c', 0)) {
146
        skip_bits_long(gb, 16);
147
        skip_bits_long(gb, 8);
148
        skip_bits_long(gb, 8);
149
        c->total_band_count = get_bits(gb, 8) + 1;
150
        c->base_band_count = get_bits(gb, 8) + 1;
151
        c->track_count = get_bits(gb, 4);
152
        c->channel_config = get_bits(gb, 4);
153
        if (!get_bits(gb, 8))
154
            c->base_band_count = c->total_band_count;
155
        c->stereo_band_count = c->total_band_count - c->base_band_count;
156
        c->bands_per_hfr_group = 0;
157
    } else
158
        return AVERROR_INVALIDDATA;
159
160
    if (c->total_band_count > FF_ARRAY_ELEMS(c->ch->imdct_in))
161
        return AVERROR_INVALIDDATA;
162
163
164
    while (get_bits_left(gb) >= 32) {
165
        chunk = get_bits_long(gb, 32);
166
        if (chunk == MKBETAG('v', 'b', 'r', 0)) {
167
            skip_bits_long(gb, 16);
168
            skip_bits_long(gb, 16);
169
        } else if (chunk == MKBETAG('a', 't', 'h', 0)) {
170
            c->ath_type = get_bits(gb, 16);
171
        } else if (chunk == MKBETAG('r', 'v', 'a', 0)) {
172
            skip_bits_long(gb, 32);
173
        } else if (chunk == MKBETAG('c', 'o', 'm', 'm')) {
174
            skip_bits_long(gb, get_bits(gb, 8) * 8);
175
        } else if (chunk == MKBETAG('c', 'i', 'p', 'h')) {
176
            skip_bits_long(gb, 16);
177
        } else if (chunk == MKBETAG('l', 'o', 'o', 'p')) {
178
            skip_bits_long(gb, 32);
179
            skip_bits_long(gb, 32);
180
            skip_bits_long(gb, 16);
181
            skip_bits_long(gb, 16);
182
        } else if (chunk == MKBETAG('p', 'a', 'd', 0)) {
183
            break;
184
        } else {
185
            break;
186
        }
187
    }
188
189
    ret = ath_init(c->ath, c->ath_type, avctx->sample_rate);
190
    if (ret < 0)
191
        return ret;
192
193
    if (!c->track_count)
194
        c->track_count = 1;
195
196
    b = avctx->channels / c->track_count;
197
    if (c->stereo_band_count && b > 1) {
198
        int8_t *x = r;
199
200
        for (int i = 0; i < c->track_count; i++, x+=b) {
201
            switch (b) {
202
            case 2:
203
            case 3:
204
                x[0] = 1;
205
                x[1] = 2;
206
                break;
207
            case 4:
208
                x[0]=1; x[1] = 2;
209
                if (c->channel_config == 0) {
210
                    x[2]=1;
211
                    x[3]=2;
212
                }
213
                break;
214
            case 5:
215
                x[0]=1; x[1] = 2;
216
                if (c->channel_config <= 2) {
217
                    x[3]=1;
218
                    x[4]=2;
219
                }
220
                break;
221
            case 6:
222
            case 7:
223
                x[0] = 1; x[1] = 2; x[4] = 1; x[5] = 2;
224
                break;
225
            case 8:
226
                x[0] = 1; x[1] = 2; x[4] = 1; x[5] = 2; x[6] = 1; x[7] = 2;
227
                break;
228
            }
229
        }
230
    }
231
232
    if (c->total_band_count < c->base_band_count)
233
        return AVERROR_INVALIDDATA;
234
235
    c->hfr_group_count = ceil2(c->total_band_count - (c->base_band_count + c->stereo_band_count),
236
                               c->bands_per_hfr_group);
237
238
    if (c->base_band_count + c->stereo_band_count + (unsigned long)c->hfr_group_count > 128ULL)
239
        return AVERROR_INVALIDDATA;
240
241
    for (int i = 0; i < avctx->channels; i++) {
242
        c->ch[i].chan_type = r[i];
243
        c->ch[i].count     = c->base_band_count + ((r[i] != 2) ? c->stereo_band_count : 0);
244
        c->ch[i].hfr_scale = &c->ch[i].scale_factors[c->base_band_count + c->stereo_band_count];
245
        if (c->ch[i].count > 128)
246
            return AVERROR_INVALIDDATA;
247
    }
248
249
    c->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
250
    if (!c->fdsp)
251
        return AVERROR(ENOMEM);
252
253
    return av_tx_init(&c->tx_ctx, &c->tx_fn, AV_TX_FLOAT_MDCT, 1, 128, &scale, 0);
254
}
255
256
static void run_imdct(HCAContext *c, ChannelContext *ch, int index, float *out)
257
{
258
    c->tx_fn(c->tx_ctx, ch->imdct_out, ch->imdct_in, sizeof(float));
259
260
    c->fdsp->vector_fmul_window(out, ch->imdct_prev + (128 >> 1),
261
                                ch->imdct_out, window, 128 >> 1);
262
263
    memcpy(ch->imdct_prev, ch->imdct_out, 128 * sizeof(float));
264
}
265
266
static void apply_intensity_stereo(HCAContext *s, ChannelContext *ch1, ChannelContext *ch2,
267
                                   int index, unsigned band_count, unsigned base_band_count,
268
                                   unsigned stereo_band_count)
269
{
270
    float ratio_l = intensity_ratio_table[ch2->intensity[index]];
271
    float ratio_r = ratio_l - 2.0f;
272
    float *c1 = &ch1->imdct_in[base_band_count];
273
    float *c2 = &ch2->imdct_in[base_band_count];
274
275
    if (ch1->chan_type != 1 || !stereo_band_count)
276
        return;
277
278
    for (int i = 0; i < band_count; i++) {
279
        *(c2++)  = *c1 * ratio_r;
280
        *(c1++) *= ratio_l;
281
    }
282
}
283
284
static void reconstruct_hfr(HCAContext *s, ChannelContext *ch,
285
                            unsigned hfr_group_count,
286
                            unsigned bands_per_hfr_group,
287
                            unsigned start_band, unsigned total_band_count)
288
{
289
    if (ch->chan_type == 2 || !bands_per_hfr_group)
290
        return;
291
292
    for (int i = 0, k = start_band, l = start_band - 1; i < hfr_group_count; i++){
293
        for (int j = 0; j < bands_per_hfr_group && k < total_band_count && l >= 0; j++, k++, l--){
294
            ch->imdct_in[k] = scale_conversion_table[ scale_conv_bias +
295
                av_clip_intp2(ch->hfr_scale[i] - ch->scale_factors[l], 6) ] * ch->imdct_in[l];
296
        }
297
    }
298
299
    ch->imdct_in[127] = 0;
300
}
301
302
static void dequantize_coefficients(HCAContext *c, ChannelContext *ch)
303
{
304
    GetBitContext *gb = &c->gb;
305
306
    for (int i = 0; i < ch->count; i++) {
307
        unsigned scale = ch->scale[i];
308
        int nb_bits = max_bits_table[scale];
309
        int value = get_bitsz(gb, nb_bits);
310
        float factor;
311
312
        if (scale > 7) {
313
            value = (1 - ((value & 1) << 1)) * (value >> 1);
314
            if (!value)
315
                skip_bits_long(gb, -1);
316
            factor = value;
317
        } else {
318
            value += scale << 4;
319
            skip_bits_long(gb, quant_spectrum_bits[value] - nb_bits);
320
            factor = quant_spectrum_value[value];
321
        }
322
        ch->imdct_in[i] = factor * ch->base[i];
323
    }
324
325
    memset(ch->imdct_in + ch->count, 0,  sizeof(ch->imdct_in) - ch->count * sizeof(ch->imdct_in[0]));
326
}
327
328
static void unpack(HCAContext *c, ChannelContext *ch,
329
                   unsigned hfr_group_count,
330
                   int packed_noise_level,
331
                   const uint8_t *ath)
332
{
333
    GetBitContext *gb = &c->gb;
334
    int delta_bits = get_bits(gb, 3);
335
336
    if (delta_bits > 5) {
337
        for (int i = 0; i < ch->count; i++)
338
            ch->scale_factors[i] = get_bits(gb, 6);
339
    } else if (delta_bits) {
340
        int factor = get_bits(gb, 6);
341
        int max_value = (1 << delta_bits) - 1;
342
        int half_max = max_value >> 1;
343
344
        ch->scale_factors[0] = factor;
345
        for (int i = 1; i < ch->count; i++){
346
            int delta = get_bits(gb, delta_bits);
347
348
            if (delta == max_value) {
349
                factor = get_bits(gb, 6);
350
            } else {
351
                factor += delta - half_max;
352
            }
353
            factor = av_clip_uintp2(factor, 6);
354
355
            ch->scale_factors[i] = factor;
356
        }
357
    } else {
358
        memset(ch->scale_factors, 0, 128);
359
    }
360
361
    if (ch->chan_type == 2){
362
        ch->intensity[0] = get_bits(gb, 4);
363
        if (ch->intensity[0] < 15) {
364
            for (int i = 1; i < 8; i++)
365
                ch->intensity[i] = get_bits(gb, 4);
366
        }
367
    } else {
368
        for (int i = 0; i < hfr_group_count; i++)
369
            ch->hfr_scale[i] = get_bits(gb, 6);
370
    }
371
372
    for (int i = 0; i < ch->count; i++) {
373
        int scale = ch->scale_factors[i];
374
375
        if (scale) {
376
            scale = c->ath[i] + ((packed_noise_level + i) >> 8) - ((scale * 5) >> 1) + 2;
377
            scale = scale_table[av_clip(scale, 0, 58)];
378
        }
379
        ch->scale[i] = scale;
380
    }
381
382
    memset(ch->scale + ch->count, 0, sizeof(ch->scale) - ch->count);
383
384
    for (int i = 0; i < ch->count; i++)
385
        ch->base[i] = dequantizer_scaling_table[ch->scale_factors[i]] * quant_step_size[ch->scale[i]];
386
}
387
388
static int decode_frame(AVCodecContext *avctx, void *data,
389
                        int *got_frame_ptr, AVPacket *avpkt)
390
{
391
    AVFrame *frame = data;
392
    HCAContext *c = avctx->priv_data;
393
    int ch, ret, packed_noise_level;
394
    GetBitContext *gb = &c->gb;
395
    float **samples;
396
397
    if (avctx->err_recognition & AV_EF_CRCCHECK) {
398
        if (av_crc(c->crc_table, 0, avpkt->data, avpkt->size))
399
            return AVERROR_INVALIDDATA;
400
    }
401
402
    if ((ret = init_get_bits8(gb, avpkt->data, avpkt->size)) < 0)
403
        return ret;
404
405
    if (get_bits(gb, 16) != 0xFFFF)
406
        return AVERROR_INVALIDDATA;
407
408
    frame->nb_samples = 1024;
409
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
410
        return ret;
411
    samples = (float **)frame->extended_data;
412
413
    packed_noise_level = (get_bits(gb, 9) << 8) - get_bits(gb, 7);
414
415
    for (ch = 0; ch < avctx->channels; ch++)
416
        unpack(c, &c->ch[ch], c->hfr_group_count, packed_noise_level, c->ath);
417
418
    for (int i = 0; i < 8; i++) {
419
        for (ch = 0; ch < avctx->channels; ch++)
420
            dequantize_coefficients(c, &c->ch[ch]);
421
        for (ch = 0; ch < avctx->channels; ch++)
422
            reconstruct_hfr(c, &c->ch[ch], c->hfr_group_count, c->bands_per_hfr_group,
423
                            c->stereo_band_count + c->base_band_count, c->total_band_count);
424
        for (ch = 0; ch < avctx->channels - 1; ch++)
425
            apply_intensity_stereo(c, &c->ch[ch], &c->ch[ch+1], i,
426
                                   c->total_band_count - c->base_band_count,
427
                                   c->base_band_count, c->stereo_band_count);
428
        for (ch = 0; ch < avctx->channels; ch++)
429
            run_imdct(c, &c->ch[ch], i, samples[ch] + i * 128);
430
    }
431
432
    *got_frame_ptr = 1;
433
434
    return avpkt->size;
435
}
436
437
static av_cold int decode_close(AVCodecContext *avctx)
438
{
439
    HCAContext *c = avctx->priv_data;
440
441
    av_freep(&c->fdsp);
442
    av_tx_uninit(&c->tx_ctx);
443
444
    return 0;
445
}
446
447
AVCodec ff_hca_decoder = {
448
    .name           = "hca",
449
    .long_name      = NULL_IF_CONFIG_SMALL("CRI HCA"),
450
    .type           = AVMEDIA_TYPE_AUDIO,
451
    .id             = AV_CODEC_ID_HCA,
452
    .priv_data_size = sizeof(HCAContext),
453
    .init           = decode_init,
454
    .decode         = decode_frame,
455
    .close          = decode_close,
456
    .capabilities   = AV_CODEC_CAP_DR1,
457
    .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
458
                                                      AV_SAMPLE_FMT_NONE },
459
};