FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/bonk.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 0 229 0.0%
Functions: 0 9 0.0%
Branches: 0 154 0.0%

Line Branch Exec 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 {
59 BonkContext *s = avctx->priv_data;
60
61 av_freep(&s->bitstream);
62 av_freep(&s->input_samples);
63 av_freep(&s->samples[0]);
64 av_freep(&s->samples[1]);
65 av_freep(&s->bits);
66 s->bitstream_size = 0;
67
68 return 0;
69 }
70
71 static av_cold int bonk_init(AVCodecContext *avctx)
72 {
73 BonkContext *s = avctx->priv_data;
74
75 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
76 if (avctx->extradata_size < 17)
77 return AVERROR(EINVAL);
78
79 if (avctx->extradata[0]) {
80 av_log(avctx, AV_LOG_ERROR, "Unsupported version.\n");
81 return AVERROR_INVALIDDATA;
82 }
83
84 if (avctx->ch_layout.nb_channels < 1 || avctx->ch_layout.nb_channels > 2)
85 return AVERROR_INVALIDDATA;
86
87 s->nb_samples = AV_RL32(avctx->extradata + 1) / avctx->ch_layout.nb_channels;
88 if (!s->nb_samples)
89 s->nb_samples = UINT64_MAX;
90 s->lossless = avctx->extradata[10] != 0;
91 s->mid_side = avctx->extradata[11] != 0;
92 s->n_taps = AV_RL16(avctx->extradata + 12);
93 if (!s->n_taps || s->n_taps > 2048)
94 return AVERROR(EINVAL);
95
96 s->down_sampling = avctx->extradata[14];
97 if (!s->down_sampling)
98 return AVERROR(EINVAL);
99
100 s->samples_per_packet = AV_RL16(avctx->extradata + 15);
101 if (!s->samples_per_packet)
102 return AVERROR(EINVAL);
103
104 if (s->down_sampling * s->samples_per_packet < s->n_taps)
105 return AVERROR_INVALIDDATA;
106
107 s->max_framesize = s->samples_per_packet * avctx->ch_layout.nb_channels * s->down_sampling * 16LL;
108 if (s->max_framesize > (INT32_MAX - AV_INPUT_BUFFER_PADDING_SIZE) / 8)
109 return AVERROR_INVALIDDATA;
110
111 s->bitstream = av_calloc(s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE, sizeof(*s->bitstream));
112 if (!s->bitstream)
113 return AVERROR(ENOMEM);
114
115 s->input_samples = av_calloc(s->samples_per_packet, sizeof(*s->input_samples));
116 if (!s->input_samples)
117 return AVERROR(ENOMEM);
118
119 s->samples[0] = av_calloc(s->samples_per_packet * s->down_sampling, sizeof(*s->samples[0]));
120 s->samples[1] = av_calloc(s->samples_per_packet * s->down_sampling, sizeof(*s->samples[0]));
121 if (!s->samples[0] || !s->samples[1])
122 return AVERROR(ENOMEM);
123
124 s->bits = av_calloc(s->max_framesize * 8, sizeof(*s->bits));
125 if (!s->bits)
126 return AVERROR(ENOMEM);
127
128 for (int i = 0; i < 512; i++) {
129 s->quant[i] = sqrt(i + 1);
130 }
131
132 return 0;
133 }
134
135 static unsigned read_uint_max(BonkContext *s, uint32_t max)
136 {
137 unsigned value = 0;
138
139 if (max == 0)
140 return 0;
141
142 av_assert0(max >> 31 == 0);
143
144 for (unsigned i = 1; i <= max - value; i+=i)
145 if (get_bits1(&s->gb))
146 value += i;
147
148 return value;
149 }
150
151 static int intlist_read(BonkContext *s, int *buf, int entries, int base_2_part)
152 {
153 int i, low_bits = 0, x = 0, max_x;
154 int n_zeros = 0, step = 256, dominant = 0;
155 int pos = 0, level = 0;
156 BitCount *bits = s->bits;
157 int passes = 1;
158
159 memset(buf, 0, entries * sizeof(*buf));
160 if (base_2_part) {
161 low_bits = get_bits(&s->gb, 4);
162
163 if (low_bits)
164 for (i = 0; i < entries; i++)
165 buf[i] = get_bits(&s->gb, low_bits);
166 }
167
168 while (n_zeros < entries) {
169 int steplet = step >> 8;
170
171 if (get_bits_left(&s->gb) <= 0)
172 return AVERROR_INVALIDDATA;
173
174 if (!get_bits1(&s->gb)) {
175 av_assert0(steplet >= 0);
176
177 if (steplet > 0) {
178 bits[x ].bit = dominant;
179 bits[x++].count = steplet;
180 }
181
182 if (!dominant)
183 n_zeros += steplet;
184
185 if (step > INT32_MAX*8LL/9 + 1)
186 return AVERROR_INVALIDDATA;
187 step += step / 8;
188 } else if (steplet > 0) {
189 int actual_run = read_uint_max(s, steplet - 1);
190
191 av_assert0(actual_run >= 0);
192
193 if (actual_run > 0) {
194 bits[x ].bit = dominant;
195 bits[x++].count = actual_run;
196 }
197
198 bits[x ].bit = !dominant;
199 bits[x++].count = 1;
200
201 if (!dominant)
202 n_zeros += actual_run;
203 else
204 n_zeros++;
205
206 step -= step / 8;
207 }
208
209 if (step < 256) {
210 step = 65536 / step;
211 dominant = !dominant;
212 }
213 }
214
215 max_x = x;
216 x = 0;
217 n_zeros = 0;
218 for (i = 0; n_zeros < entries; i++) {
219 if (x >= max_x)
220 return AVERROR_INVALIDDATA;
221
222 if (pos >= entries) {
223 pos = 0;
224 level += passes << low_bits;
225 passes = 1;
226 if (bits[x].bit && bits[x].count > entries - n_zeros)
227 passes = bits[x].count / (entries - n_zeros);
228 }
229
230 if (level > 1 << 16)
231 return AVERROR_INVALIDDATA;
232
233 if (buf[pos] >= level) {
234 if (bits[x].bit)
235 buf[pos] += passes << low_bits;
236 else
237 n_zeros++;
238
239 av_assert1(bits[x].count >= passes);
240 bits[x].count -= passes;
241 x += bits[x].count == 0;
242 }
243
244 pos++;
245 }
246
247 for (i = 0; i < entries; i++) {
248 if (buf[i] && get_bits1(&s->gb)) {
249 buf[i] = -buf[i];
250 }
251 }
252
253 return 0;
254 }
255
256 static inline int shift_down(int a, int b)
257 {
258 return (a >> b) + (a < 0);
259 }
260
261 static inline int shift(int a, int b)
262 {
263 return a + (1 << b - 1) >> b;
264 }
265
266 #define LATTICE_SHIFT 10
267 #define SAMPLE_SHIFT 4
268 #define SAMPLE_FACTOR (1 << SAMPLE_SHIFT)
269
270 static int predictor_calc_error(int *k, int *state, int order, int error)
271 {
272 int i, x = error - (unsigned)shift_down(k[order-1] * (unsigned)state[order-1], LATTICE_SHIFT);
273 int *k_ptr = &(k[order-2]),
274 *state_ptr = &(state[order-2]);
275
276 for (i = order-2; i >= 0; i--, k_ptr--, state_ptr--) {
277 unsigned k_value = *k_ptr, state_value = *state_ptr;
278
279 x -= (unsigned) shift_down(k_value * (unsigned)state_value, LATTICE_SHIFT);
280 state_ptr[1] = state_value + shift_down(k_value * x, LATTICE_SHIFT);
281 }
282
283 // don't drift too far, to avoid overflows
284 x = av_clip(x, -(SAMPLE_FACTOR << 16), SAMPLE_FACTOR << 16);
285
286 state[0] = x;
287
288 return x;
289 }
290
291 static void predictor_init_state(int *k, unsigned *state, int order)
292 {
293 for (int i = order - 2; i >= 0; i--) {
294 unsigned x = state[i];
295
296 for (int j = 0, p = i + 1; p < order; j++, p++) {
297 int tmp = x + shift_down(k[j] * state[p], LATTICE_SHIFT);
298
299 state[p] += shift_down(k[j] * x, LATTICE_SHIFT);
300 x = tmp;
301 }
302 }
303 }
304
305 static int bonk_decode(AVCodecContext *avctx, AVFrame *frame,
306 int *got_frame_ptr, AVPacket *pkt)
307 {
308 BonkContext *s = avctx->priv_data;
309 GetBitContext *gb = &s->gb;
310 const uint8_t *buf;
311 int quant, n, buf_size, input_buf_size;
312 int ret = AVERROR_INVALIDDATA;
313
314 if ((!pkt->size && !s->bitstream_size) || s->nb_samples == 0) {
315 *got_frame_ptr = 0;
316 return pkt->size;
317 }
318
319 buf_size = FFMIN(pkt->size, s->max_framesize - s->bitstream_size);
320 input_buf_size = buf_size;
321 if (s->bitstream_index + s->bitstream_size + buf_size + AV_INPUT_BUFFER_PADDING_SIZE > s->max_framesize) {
322 memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
323 s->bitstream_index = 0;
324 }
325 if (pkt->data)
326 memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], pkt->data, buf_size);
327 buf = &s->bitstream[s->bitstream_index];
328 buf_size += s->bitstream_size;
329 s->bitstream_size = buf_size;
330 if (buf_size < s->max_framesize && pkt->data) {
331 *got_frame_ptr = 0;
332 return input_buf_size;
333 }
334
335 frame->nb_samples = FFMIN(s->samples_per_packet * s->down_sampling, s->nb_samples);
336 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
337 goto fail;
338
339 if ((ret = init_get_bits8(gb, buf, buf_size)) < 0)
340 goto fail;
341
342 skip_bits(gb, s->skip);
343 if ((ret = intlist_read(s, s->k, s->n_taps, 0)) < 0)
344 goto fail;
345
346 for (int i = 0; i < s->n_taps; i++)
347 s->k[i] *= s->quant[i];
348 quant = s->lossless ? 1 : get_bits(&s->gb, 16) * SAMPLE_FACTOR;
349
350 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
351 const int samples_per_packet = s->samples_per_packet;
352 const int down_sampling = s->down_sampling;
353 const int offset = samples_per_packet * down_sampling - 1;
354 int *state = s->state[ch];
355 int *sample = s->samples[ch];
356
357 predictor_init_state(s->k, state, s->n_taps);
358 if ((ret = intlist_read(s, s->input_samples, samples_per_packet, 1)) < 0)
359 goto fail;
360
361 for (int i = 0; i < samples_per_packet; i++) {
362 for (int j = 0; j < s->down_sampling - 1; j++) {
363 sample[0] = predictor_calc_error(s->k, state, s->n_taps, 0);
364 sample++;
365 }
366
367 sample[0] = predictor_calc_error(s->k, state, s->n_taps, s->input_samples[i] * (unsigned)quant);
368 sample++;
369 }
370
371 sample = s->samples[ch];
372 for (int i = 0; i < s->n_taps; i++)
373 state[i] = sample[offset - i];
374 }
375
376 if (s->mid_side && avctx->ch_layout.nb_channels == 2) {
377 for (int i = 0; i < frame->nb_samples; i++) {
378 s->samples[1][i] += shift(s->samples[0][i], 1);
379 s->samples[0][i] -= s->samples[1][i];
380 }
381 }
382
383 if (!s->lossless) {
384 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
385 int *samples = s->samples[ch];
386 for (int i = 0; i < frame->nb_samples; i++)
387 samples[i] = shift(samples[i], 4);
388 }
389 }
390
391 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
392 int16_t *osamples = (int16_t *)frame->extended_data[ch];
393 int *samples = s->samples[ch];
394 for (int i = 0; i < frame->nb_samples; i++)
395 osamples[i] = av_clip_int16(samples[i]);
396 }
397
398 s->nb_samples -= frame->nb_samples;
399
400 s->skip = get_bits_count(gb) - 8 * (get_bits_count(gb) / 8);
401 n = get_bits_count(gb) / 8;
402
403 if (n > buf_size) {
404 fail:
405 s->bitstream_size = 0;
406 s->bitstream_index = 0;
407 return AVERROR_INVALIDDATA;
408 }
409
410 *got_frame_ptr = 1;
411
412 if (s->bitstream_size) {
413 s->bitstream_index += n;
414 s->bitstream_size -= n;
415 return input_buf_size;
416 }
417 return n;
418 }
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 #if FF_API_SUBFRAMES
431 AV_CODEC_CAP_SUBFRAMES |
432 #endif
433 AV_CODEC_CAP_DR1,
434 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
435 .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
436 AV_SAMPLE_FMT_NONE },
437 };
438