Coverage Report

Created: 2025-12-31 07:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/bonk.c
Line
Count
Source
1
/*
2
 * Bonk audio decoder
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/intreadwrite.h"
22
#include "libavutil/mem.h"
23
#include "avcodec.h"
24
#include "codec_internal.h"
25
#include "decode.h"
26
#define BITSTREAM_READER_LE
27
#include "get_bits.h"
28
29
typedef struct BitCount {
30
    uint8_t bit;
31
    unsigned count;
32
} BitCount;
33
34
typedef struct BonkContext {
35
    GetBitContext gb;
36
    int skip;
37
38
    uint8_t *bitstream;
39
    int64_t max_framesize;
40
    int bitstream_size;
41
    int bitstream_index;
42
43
    uint64_t nb_samples;
44
    int lossless;
45
    int mid_side;
46
    int n_taps;
47
    int down_sampling;
48
    int samples_per_packet;
49
50
    int state[2][2048], k[2048];
51
    int *samples[2];
52
    int *input_samples;
53
    uint8_t quant[2048];
54
    BitCount *bits;
55
} BonkContext;
56
57
static av_cold int bonk_close(AVCodecContext *avctx)
58
237
{
59
237
    BonkContext *s = avctx->priv_data;
60
61
237
    av_freep(&s->bitstream);
62
237
    av_freep(&s->input_samples);
63
237
    av_freep(&s->samples[0]);
64
237
    av_freep(&s->samples[1]);
65
237
    av_freep(&s->bits);
66
237
    s->bitstream_size = 0;
67
68
237
    return 0;
69
237
}
70
71
static av_cold int bonk_init(AVCodecContext *avctx)
72
237
{
73
237
    BonkContext *s = avctx->priv_data;
74
75
237
    avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
76
237
    if (avctx->extradata_size < 17)
77
0
        return AVERROR(EINVAL);
78
79
237
    if (avctx->extradata[0]) {
80
8
        av_log(avctx, AV_LOG_ERROR, "Unsupported version.\n");
81
8
        return AVERROR_INVALIDDATA;
82
8
    }
83
84
229
    if (avctx->ch_layout.nb_channels < 1 || avctx->ch_layout.nb_channels > 2)
85
3
        return AVERROR_INVALIDDATA;
86
87
226
    s->nb_samples = AV_RL32(avctx->extradata + 1) / avctx->ch_layout.nb_channels;
88
226
    if (!s->nb_samples)
89
152
        s->nb_samples = UINT64_MAX;
90
226
    s->lossless = avctx->extradata[10] != 0;
91
226
    s->mid_side = avctx->extradata[11] != 0;
92
226
    s->n_taps = AV_RL16(avctx->extradata + 12);
93
226
    if (!s->n_taps || s->n_taps > 2048)
94
1
        return AVERROR(EINVAL);
95
96
225
    s->down_sampling = avctx->extradata[14];
97
225
    if (!s->down_sampling)
98
0
        return AVERROR(EINVAL);
99
100
225
    s->samples_per_packet = AV_RL16(avctx->extradata + 15);
101
225
    if (!s->samples_per_packet)
102
0
        return AVERROR(EINVAL);
103
104
225
    if (s->down_sampling * s->samples_per_packet < s->n_taps)
105
0
        return AVERROR_INVALIDDATA;
106
107
225
    s->max_framesize = s->samples_per_packet * avctx->ch_layout.nb_channels * s->down_sampling * 16LL;
108
225
    if (s->max_framesize > (INT32_MAX - AV_INPUT_BUFFER_PADDING_SIZE) / 8)
109
0
        return AVERROR_INVALIDDATA;
110
111
225
    s->bitstream = av_calloc(s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE, sizeof(*s->bitstream));
112
225
    if (!s->bitstream)
113
0
        return AVERROR(ENOMEM);
114
115
225
    s->input_samples = av_calloc(s->samples_per_packet, sizeof(*s->input_samples));
116
225
    if (!s->input_samples)
117
0
        return AVERROR(ENOMEM);
118
119
225
    s->samples[0] = av_calloc(s->samples_per_packet * s->down_sampling, sizeof(*s->samples[0]));
120
225
    s->samples[1] = av_calloc(s->samples_per_packet * s->down_sampling, sizeof(*s->samples[0]));
121
225
    if (!s->samples[0] || !s->samples[1])
122
0
        return AVERROR(ENOMEM);
123
124
225
    s->bits = av_calloc(s->max_framesize * 8, sizeof(*s->bits));
125
225
    if (!s->bits)
126
0
        return AVERROR(ENOMEM);
127
128
115k
    for (int i = 0; i < 512; i++) {
129
115k
        s->quant[i] = sqrt(i + 1);
130
115k
    }
131
132
225
    return 0;
133
225
}
134
135
static unsigned read_uint_max(BonkContext *s, uint32_t max)
136
475k
{
137
475k
    unsigned value = 0;
138
139
475k
    if (max == 0)
140
341k
        return 0;
141
142
133k
    av_assert0(max >> 31 == 0);
143
144
1.10M
    for (unsigned i = 1; i <= max - value; i+=i)
145
974k
        if (get_bits1(&s->gb))
146
479k
            value += i;
147
148
133k
    return value;
149
133k
}
150
151
static int intlist_read(BonkContext *s, int *buf, int entries, int base_2_part)
152
148k
{
153
148k
    int i, low_bits = 0, x = 0, max_x;
154
148k
    int n_zeros = 0, step = 256, dominant = 0;
155
148k
    int pos = 0, level = 0;
156
148k
    BitCount *bits = s->bits;
157
148k
    int passes = 1;
158
159
148k
    memset(buf, 0, entries * sizeof(*buf));
160
148k
    if (base_2_part) {
161
75.2k
        low_bits = get_bits(&s->gb, 4);
162
163
75.2k
        if (low_bits)
164
1.25M
            for (i = 0; i < entries; i++)
165
1.23M
                buf[i] = get_bits(&s->gb, low_bits);
166
75.2k
    }
167
168
1.43M
    while (n_zeros < entries) {
169
1.28M
        int steplet = step >> 8;
170
171
1.28M
        if (get_bits_left(&s->gb) <= 0)
172
252
            return AVERROR_INVALIDDATA;
173
174
1.28M
        if (!get_bits1(&s->gb)) {
175
808k
            av_assert0(steplet >= 0);
176
177
808k
            if (steplet > 0) {
178
808k
                bits[x  ].bit   = dominant;
179
808k
                bits[x++].count = steplet;
180
808k
            }
181
182
808k
            if (!dominant)
183
346k
                n_zeros += steplet;
184
185
808k
            if (step > INT32_MAX*8LL/9 + 1)
186
638
                return AVERROR_INVALIDDATA;
187
807k
            step += step / 8;
188
807k
        } else if (steplet > 0) {
189
475k
            int actual_run = read_uint_max(s, steplet - 1);
190
191
475k
            av_assert0(actual_run >= 0);
192
193
475k
            if (actual_run > 0) {
194
111k
                bits[x  ].bit   = dominant;
195
111k
                bits[x++].count = actual_run;
196
111k
            }
197
198
475k
            bits[x  ].bit   = !dominant;
199
475k
            bits[x++].count = 1;
200
201
475k
            if (!dominant)
202
198k
                n_zeros += actual_run;
203
276k
            else
204
276k
                n_zeros++;
205
206
475k
            step -= step / 8;
207
475k
        }
208
209
1.28M
        if (step < 256) {
210
130k
            step = 65536 / step;
211
130k
            dominant = !dominant;
212
130k
        }
213
1.28M
    }
214
215
147k
    max_x = x;
216
147k
    x = 0;
217
147k
    n_zeros = 0;
218
16.6M
    for (i = 0; n_zeros < entries; i++) {
219
16.4M
        if (x >= max_x)
220
0
            return AVERROR_INVALIDDATA;
221
222
16.4M
        if (pos >= entries) {
223
168k
            pos = 0;
224
168k
            level += passes << low_bits;
225
168k
            passes = 1;
226
168k
            if (bits[x].bit && bits[x].count > entries - n_zeros)
227
63.2k
                passes =  bits[x].count / (entries - n_zeros);
228
168k
        }
229
230
16.4M
        if (level > 1 << 16)
231
700
            return AVERROR_INVALIDDATA;
232
233
16.4M
        if (buf[pos] >= level) {
234
5.65M
            if (bits[x].bit)
235
3.37M
                buf[pos] += passes << low_bits;
236
2.27M
            else
237
2.27M
                n_zeros++;
238
239
5.65M
            av_assert1(bits[x].count >= passes);
240
5.65M
            bits[x].count -= passes;
241
5.65M
            x += bits[x].count == 0;
242
5.65M
        }
243
244
16.4M
        pos++;
245
16.4M
    }
246
247
2.35M
    for (i = 0; i < entries; i++) {
248
2.21M
        if (buf[i] && get_bits1(&s->gb)) {
249
229k
            buf[i] = -buf[i];
250
229k
        }
251
2.21M
    }
252
253
146k
    return 0;
254
147k
}
255
256
static inline int shift_down(int a, int b)
257
2.97G
{
258
2.97G
    return (a >> b) + (a < 0);
259
2.97G
}
260
261
static inline int shift(int a, int b)
262
6.23M
{
263
6.23M
    return a + (1 << b - 1) >> b;
264
6.23M
}
265
266
2.97G
#define LATTICE_SHIFT 10
267
17.3M
#define SAMPLE_SHIFT   4
268
17.4M
#define SAMPLE_FACTOR (1 << SAMPLE_SHIFT)
269
270
static int predictor_calc_error(int *k, int *state, int order, int error)
271
8.67M
{
272
8.67M
    int i, x = error - (unsigned)shift_down(k[order-1] * (unsigned)state[order-1], LATTICE_SHIFT);
273
8.67M
    int *k_ptr = &(k[order-2]),
274
8.67M
        *state_ptr = &(state[order-2]);
275
276
1.42G
    for (i = order-2; i >= 0; i--, k_ptr--, state_ptr--) {
277
1.41G
        unsigned k_value = *k_ptr, state_value = *state_ptr;
278
279
1.41G
        x -= (unsigned) shift_down(k_value * (unsigned)state_value, LATTICE_SHIFT);
280
1.41G
        state_ptr[1] = state_value + shift_down(k_value * x, LATTICE_SHIFT);
281
1.41G
    }
282
283
    // don't drift too far, to avoid overflows
284
8.67M
    x = av_clip(x, -(SAMPLE_FACTOR << 16), SAMPLE_FACTOR << 16);
285
286
8.67M
    state[0] = x;
287
288
8.67M
    return x;
289
8.67M
}
290
291
static void predictor_init_state(int *k, unsigned *state, int order)
292
75.2k
{
293
323k
    for (int i = order - 2; i >= 0; i--) {
294
247k
        unsigned x = state[i];
295
296
68.6M
        for (int j = 0, p = i + 1; p < order; j++, p++) {
297
68.3M
            int tmp = x + shift_down(k[j] * state[p], LATTICE_SHIFT);
298
299
68.3M
            state[p] += shift_down(k[j] * x, LATTICE_SHIFT);
300
68.3M
            x = tmp;
301
68.3M
        }
302
247k
    }
303
75.2k
}
304
305
static int bonk_decode(AVCodecContext *avctx, AVFrame *frame,
306
                       int *got_frame_ptr, AVPacket *pkt)
307
279k
{
308
279k
    BonkContext *s = avctx->priv_data;
309
279k
    GetBitContext *gb = &s->gb;
310
279k
    const uint8_t *buf;
311
279k
    int quant, n, buf_size, input_buf_size;
312
279k
    int ret = AVERROR_INVALIDDATA;
313
314
279k
    if ((!pkt->size && !s->bitstream_size) || s->nb_samples == 0) {
315
3.92k
        *got_frame_ptr = 0;
316
3.92k
        return pkt->size;
317
3.92k
    }
318
319
275k
    buf_size = FFMIN(pkt->size, s->max_framesize - s->bitstream_size);
320
275k
    input_buf_size = buf_size;
321
275k
    if (s->bitstream_index + s->bitstream_size + buf_size + AV_INPUT_BUFFER_PADDING_SIZE > s->max_framesize) {
322
136k
        memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
323
136k
        s->bitstream_index = 0;
324
136k
    }
325
275k
    if (pkt->data)
326
271k
        memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], pkt->data, buf_size);
327
275k
    buf                = &s->bitstream[s->bitstream_index];
328
275k
    buf_size          += s->bitstream_size;
329
275k
    s->bitstream_size  = buf_size;
330
275k
    if (buf_size < s->max_framesize && pkt->data) {
331
202k
        *got_frame_ptr = 0;
332
202k
        return input_buf_size;
333
202k
    }
334
335
73.0k
    frame->nb_samples = FFMIN(s->samples_per_packet * s->down_sampling, s->nb_samples);
336
73.0k
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
337
7
        goto fail;
338
339
73.0k
    if ((ret = init_get_bits8(gb, buf, buf_size)) < 0)
340
0
        goto fail;
341
342
73.0k
    skip_bits(gb, s->skip);
343
73.0k
    if ((ret = intlist_read(s, s->k, s->n_taps, 0)) < 0)
344
520
        goto fail;
345
346
392k
    for (int i = 0; i < s->n_taps; i++)
347
320k
        s->k[i] *= s->quant[i];
348
72.5k
    quant = s->lossless ? 1 : get_bits(&s->gb, 16) * SAMPLE_FACTOR;
349
350
146k
    for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
351
75.2k
        const int samples_per_packet = s->samples_per_packet;
352
75.2k
        const int down_sampling = s->down_sampling;
353
75.2k
        const int offset = samples_per_packet * down_sampling - 1;
354
75.2k
        int *state = s->state[ch];
355
75.2k
        int *sample = s->samples[ch];
356
357
75.2k
        predictor_init_state(s->k, state, s->n_taps);
358
75.2k
        if ((ret = intlist_read(s, s->input_samples, samples_per_packet, 1)) < 0)
359
1.07k
            goto fail;
360
361
1.96M
        for (int i = 0; i < samples_per_packet; i++) {
362
8.67M
            for (int j = 0; j < s->down_sampling - 1; j++) {
363
6.78M
                sample[0] = predictor_calc_error(s->k, state, s->n_taps, 0);
364
6.78M
                sample++;
365
6.78M
            }
366
367
1.89M
            sample[0] = predictor_calc_error(s->k, state, s->n_taps, s->input_samples[i] * (unsigned)quant);
368
1.89M
            sample++;
369
1.89M
        }
370
371
74.2k
        sample = s->samples[ch];
372
368k
        for (int i = 0; i < s->n_taps; i++)
373
294k
            state[i] = sample[offset - i];
374
74.2k
    }
375
376
71.4k
    if (s->mid_side && avctx->ch_layout.nb_channels == 2) {
377
71.2k
        for (int i = 0; i < frame->nb_samples; i++) {
378
68.7k
            s->samples[1][i] += shift(s->samples[0][i], 1);
379
68.7k
            s->samples[0][i] -= s->samples[1][i];
380
68.7k
        }
381
2.47k
    }
382
383
71.4k
    if (!s->lossless) {
384
22.9k
        for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
385
12.5k
            int *samples = s->samples[ch];
386
6.18M
            for (int i = 0; i < frame->nb_samples; i++)
387
6.17M
                samples[i] = shift(samples[i], 4);
388
12.5k
        }
389
10.3k
    }
390
391
145k
    for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
392
74.0k
        int16_t *osamples = (int16_t *)frame->extended_data[ch];
393
74.0k
        int *samples = s->samples[ch];
394
8.64M
        for (int i = 0; i < frame->nb_samples; i++)
395
8.57M
            osamples[i] = av_clip_int16(samples[i]);
396
74.0k
    }
397
398
71.4k
    s->nb_samples -= frame->nb_samples;
399
400
71.4k
    s->skip = get_bits_count(gb) - 8 * (get_bits_count(gb) / 8);
401
71.4k
    n = get_bits_count(gb) / 8;
402
403
71.4k
    if (n > buf_size) {
404
1.61k
fail:
405
1.61k
        s->bitstream_size = 0;
406
1.61k
        s->bitstream_index = 0;
407
1.61k
        return AVERROR_INVALIDDATA;
408
16
    }
409
410
71.4k
    *got_frame_ptr = 1;
411
412
71.4k
    if (s->bitstream_size) {
413
71.4k
        s->bitstream_index += n;
414
71.4k
        s->bitstream_size  -= n;
415
71.4k
        return input_buf_size;
416
71.4k
    }
417
0
    return n;
418
71.4k
}
419
420
const FFCodec ff_bonk_decoder = {
421
    .p.name           = "bonk",
422
    CODEC_LONG_NAME("Bonk audio"),
423
    .p.type           = AVMEDIA_TYPE_AUDIO,
424
    .p.id             = AV_CODEC_ID_BONK,
425
    .priv_data_size   = sizeof(BonkContext),
426
    .init             = bonk_init,
427
    FF_CODEC_DECODE_CB(bonk_decode),
428
    .close            = bonk_close,
429
    .p.capabilities   = AV_CODEC_CAP_DELAY |
430
                        AV_CODEC_CAP_DR1,
431
    .caps_internal    = FF_CODEC_CAP_INIT_CLEANUP,
432
    CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16P),
433
};