FFmpeg coverage


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