FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/bonk.c
Date: 2023-01-27 00:32:08
Exec Total Coverage
Lines: 0 224 0.0%
Functions: 0 9 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
105 if (s->down_sampling * s->samples_per_packet < s->n_taps)
106 return AVERROR_INVALIDDATA;
107
108 s->max_framesize = s->samples_per_packet * avctx->ch_layout.nb_channels * s->down_sampling * 16LL;
109 if (s->max_framesize > (INT32_MAX - AV_INPUT_BUFFER_PADDING_SIZE) / 8)
110 return AVERROR_INVALIDDATA;
111
112 s->bitstream = av_calloc(s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE, sizeof(*s->bitstream));
113 if (!s->bitstream)
114 return AVERROR(ENOMEM);
115
116 s->input_samples = av_calloc(s->samples_per_packet, sizeof(*s->input_samples));
117 if (!s->input_samples)
118 return AVERROR(ENOMEM);
119
120 s->samples[0] = av_calloc(s->samples_per_packet * s->down_sampling, sizeof(*s->samples[0]));
121 s->samples[1] = av_calloc(s->samples_per_packet * s->down_sampling, sizeof(*s->samples[0]));
122 if (!s->samples[0] || !s->samples[1])
123 return AVERROR(ENOMEM);
124
125 s->bits = av_calloc(s->max_framesize * 8, sizeof(*s->bits));
126 if (!s->bits)
127 return AVERROR(ENOMEM);
128
129 for (int i = 0; i < 512; i++) {
130 s->quant[i] = sqrt(i + 1);
131 }
132
133 return 0;
134 }
135
136 static unsigned read_uint_max(BonkContext *s, uint32_t max)
137 {
138 unsigned value = 0;
139
140 if (max == 0)
141 return 0;
142
143 av_assert0(max >> 31 == 0);
144
145 for (unsigned i = 1; i <= max - value; i+=i)
146 if (get_bits1(&s->gb))
147 value += i;
148
149 return value;
150 }
151
152 static int intlist_read(BonkContext *s, int *buf, int entries, int base_2_part)
153 {
154 int i, low_bits = 0, x = 0, max_x;
155 int n_zeros = 0, step = 256, dominant = 0;
156 int pos = 0, level = 0;
157 BitCount *bits = s->bits;
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 (pos >= entries) {
220 pos = 0;
221 level += 1 << low_bits;
222 }
223
224 if (level > 1 << 16)
225 return AVERROR_INVALIDDATA;
226
227 if (x >= max_x)
228 return AVERROR_INVALIDDATA;
229
230 if (buf[pos] >= level) {
231 if (bits[x].bit)
232 buf[pos] += 1 << low_bits;
233 else
234 n_zeros++;
235
236 bits[x].count--;
237 x += bits[x].count == 0;
238 }
239
240 pos++;
241 }
242
243 for (i = 0; i < entries; i++) {
244 if (buf[i] && get_bits1(&s->gb)) {
245 buf[i] = -buf[i];
246 }
247 }
248
249 return 0;
250 }
251
252 static inline int shift_down(int a, int b)
253 {
254 return (a >> b) + (a < 0);
255 }
256
257 static inline int shift(int a, int b)
258 {
259 return a + (1 << b - 1) >> b;
260 }
261
262 #define LATTICE_SHIFT 10
263 #define SAMPLE_SHIFT 4
264 #define SAMPLE_FACTOR (1 << SAMPLE_SHIFT)
265
266 static int predictor_calc_error(int *k, int *state, int order, int error)
267 {
268 int i, x = error - shift_down(k[order-1] * state[order-1], LATTICE_SHIFT);
269 int *k_ptr = &(k[order-2]),
270 *state_ptr = &(state[order-2]);
271
272 for (i = order-2; i >= 0; i--, k_ptr--, state_ptr--) {
273 unsigned k_value = *k_ptr, state_value = *state_ptr;
274
275 x -= shift_down(k_value * state_value, LATTICE_SHIFT);
276 state_ptr[1] = state_value + shift_down(k_value * x, LATTICE_SHIFT);
277 }
278
279 // don't drift too far, to avoid overflows
280 x = av_clip(x, -(SAMPLE_FACTOR << 16), SAMPLE_FACTOR << 16);
281
282 state[0] = x;
283
284 return x;
285 }
286
287 static void predictor_init_state(int *k, unsigned *state, int order)
288 {
289 for (int i = order - 2; i >= 0; i--) {
290 unsigned x = state[i];
291
292 for (int j = 0, p = i + 1; p < order; j++, p++) {
293 int tmp = x + shift_down(k[j] * state[p], LATTICE_SHIFT);
294
295 state[p] += shift_down(k[j] * x, LATTICE_SHIFT);
296 x = tmp;
297 }
298 }
299 }
300
301 static int bonk_decode(AVCodecContext *avctx, AVFrame *frame,
302 int *got_frame_ptr, AVPacket *pkt)
303 {
304 BonkContext *s = avctx->priv_data;
305 GetBitContext *gb = &s->gb;
306 const uint8_t *buf;
307 int quant, n, buf_size, input_buf_size;
308 int ret = AVERROR_INVALIDDATA;
309
310 if ((!pkt->size && !s->bitstream_size) || s->nb_samples == 0) {
311 *got_frame_ptr = 0;
312 return pkt->size;
313 }
314
315 buf_size = FFMIN(pkt->size, s->max_framesize - s->bitstream_size);
316 input_buf_size = buf_size;
317 if (s->bitstream_index + s->bitstream_size + buf_size + AV_INPUT_BUFFER_PADDING_SIZE > s->max_framesize) {
318 memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
319 s->bitstream_index = 0;
320 }
321 if (pkt->data)
322 memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], pkt->data, buf_size);
323 buf = &s->bitstream[s->bitstream_index];
324 buf_size += s->bitstream_size;
325 s->bitstream_size = buf_size;
326 if (buf_size < s->max_framesize && pkt->data) {
327 *got_frame_ptr = 0;
328 return input_buf_size;
329 }
330
331 frame->nb_samples = FFMIN(s->samples_per_packet * s->down_sampling, s->nb_samples);
332 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
333 return ret;
334
335 if ((ret = init_get_bits8(gb, buf, buf_size)) < 0)
336 return ret;
337
338 skip_bits(gb, s->skip);
339 if ((ret = intlist_read(s, s->k, s->n_taps, 0)) < 0)
340 goto fail;
341
342 for (int i = 0; i < s->n_taps; i++)
343 s->k[i] *= s->quant[i];
344 quant = s->lossless ? 1 : get_bits(&s->gb, 16) * SAMPLE_FACTOR;
345
346 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
347 const int samples_per_packet = s->samples_per_packet;
348 const int down_sampling = s->down_sampling;
349 const int offset = samples_per_packet * down_sampling - 1;
350 int *state = s->state[ch];
351 int *sample = s->samples[ch];
352
353 predictor_init_state(s->k, state, s->n_taps);
354 if ((ret = intlist_read(s, s->input_samples, samples_per_packet, 1)) < 0)
355 goto fail;
356
357 for (int i = 0; i < samples_per_packet; i++) {
358 for (int j = 0; j < s->down_sampling - 1; j++) {
359 sample[0] = predictor_calc_error(s->k, state, s->n_taps, 0);
360 sample++;
361 }
362
363 sample[0] = predictor_calc_error(s->k, state, s->n_taps, s->input_samples[i] * (unsigned)quant);
364 sample++;
365 }
366
367 sample = s->samples[ch];
368 for (int i = 0; i < s->n_taps; i++)
369 state[i] = sample[offset - i];
370 }
371
372 if (s->mid_side && avctx->ch_layout.nb_channels == 2) {
373 for (int i = 0; i < frame->nb_samples; i++) {
374 s->samples[1][i] += shift(s->samples[0][i], 1);
375 s->samples[0][i] -= s->samples[1][i];
376 }
377 }
378
379 if (!s->lossless) {
380 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
381 int *samples = s->samples[ch];
382 for (int i = 0; i < frame->nb_samples; i++)
383 samples[i] = shift(samples[i], 4);
384 }
385 }
386
387 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
388 int16_t *osamples = (int16_t *)frame->extended_data[ch];
389 int *samples = s->samples[ch];
390 for (int i = 0; i < frame->nb_samples; i++)
391 osamples[i] = av_clip_int16(samples[i]);
392 }
393
394 s->nb_samples -= frame->nb_samples;
395
396 s->skip = get_bits_count(gb) - 8 * (get_bits_count(gb) / 8);
397 n = get_bits_count(gb) / 8;
398
399 if (n > buf_size) {
400 fail:
401 s->bitstream_size = 0;
402 s->bitstream_index = 0;
403 return AVERROR_INVALIDDATA;
404 }
405
406 *got_frame_ptr = 1;
407
408 if (s->bitstream_size) {
409 s->bitstream_index += n;
410 s->bitstream_size -= n;
411 return input_buf_size;
412 }
413 return n;
414 }
415
416 const FFCodec ff_bonk_decoder = {
417 .p.name = "bonk",
418 CODEC_LONG_NAME("Bonk audio"),
419 .p.type = AVMEDIA_TYPE_AUDIO,
420 .p.id = AV_CODEC_ID_BONK,
421 .priv_data_size = sizeof(BonkContext),
422 .init = bonk_init,
423 FF_CODEC_DECODE_CB(bonk_decode),
424 .close = bonk_close,
425 .p.capabilities = AV_CODEC_CAP_DELAY |
426 AV_CODEC_CAP_DR1 |
427 AV_CODEC_CAP_SUBFRAMES,
428 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
429 .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
430 AV_SAMPLE_FMT_NONE },
431 };
432