FFmpeg
Loading...
Searching...
No Matches
bonk.c
Go to the documentation of this file.
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
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
29typedef struct BitCount {
30 uint8_t bit;
31 unsigned count;
32} BitCount;
33
56
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
72{
73 BonkContext *s = avctx->priv_data;
74
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");
82 }
83
84 if (avctx->ch_layout.nb_channels < 1 || avctx->ch_layout.nb_channels > 2)
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
135static 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
151static 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
256static inline int shift_down(int a, int b)
257{
258 return (a >> b) + (a < 0);
259}
260
261static 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
270static 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
291static 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
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) {
404fail:
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
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,
428 .close = bonk_close,
429 .p.capabilities = AV_CODEC_CAP_DELAY |
431 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
432};
const FFCodec ff_bonk_decoder
Definition bonk.c:420
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
static uint32_t samples_per_packet(const AVCodecParameters *par)
Definition cafenc.c:62
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define bit(string, value)
Definition cbs_mpeg2.c:56
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define av_clip
Definition common.h:100
#define av_clip_int16
Definition common.h:115
long long int64_t
Definition coverity.c:34
#define max(a, b)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVPacket * pkt
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
double value
Definition eval.c:102
static struct @346255127015250356166251341105367306144006377143 state
static const uint8_t bits[8]
Definition fastaudio.c:100
#define sample
bitstream reader API header.
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
#define fail
Definition test.h:479
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition codec.h:79
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_BONK
Definition codec_id.h:550
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition samplefmt.h:64
int a
#define b
Definition input.c:43
#define AV_RL32(p)
#define AV_RL16(p)
unsigned offset
Definition libaomenc.c:763
static void predictor_init_state(int *k, unsigned *state, int order)
Definition bonk.c:291
static unsigned read_uint_max(BonkContext *s, uint32_t max)
Definition bonk.c:135
static int shift_down(int a, int b)
Definition bonk.c:256
static av_cold int bonk_init(AVCodecContext *avctx)
Definition bonk.c:71
static int bonk_decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *pkt)
Definition bonk.c:305
#define SAMPLE_FACTOR
Definition bonk.c:268
static av_cold int bonk_close(AVCodecContext *avctx)
Definition bonk.c:57
#define LATTICE_SHIFT
Definition bonk.c:266
static int predictor_calc_error(int *k, int *state, int order, int error)
Definition bonk.c:270
static int intlist_read(BonkContext *s, int *buf, int entries, int base_2_part)
Definition bonk.c:151
static int shift(int a, int b)
Definition bonk.c:261
#define av_cold
Definition attributes.h:117
#define FFMIN(a, b)
Definition macros.h:49
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
unsigned int pos
Definition spdifenc.c:431
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
uint8_t bit
Definition bonk.c:30
unsigned count
Definition bonk.c:31
int k[2048]
Definition bonk.c:50
GetBitContext gb
Definition bonk.c:35
int n_taps
Definition bonk.c:46
int * samples[2]
Definition bonk.c:51
int mid_side
Definition bonk.c:45
uint8_t * bitstream
Definition bonk.c:38
int bitstream_size
Definition bonk.c:40
int state[2][2048]
Definition bonk.c:50
uint64_t nb_samples
Definition bonk.c:43
int bitstream_index
Definition bonk.c:41
int * input_samples
Definition bonk.c:52
BitCount * bits
Definition bonk.c:54
int64_t max_framesize
Definition bonk.c:39
uint8_t quant[2048]
Definition bonk.c:53
int skip
Definition bonk.c:36
int lossless
Definition bonk.c:44
int samples_per_packet
Definition bonk.c:48
int down_sampling
Definition bonk.c:47
uint8_t level
Definition svq3.c:208
#define av_freep(p)
#define av_log(a,...)
static void error(const char *err)
static uint8_t tmp[40]
Definition aes_ctr.c:52
static const uint8_t quant[64]
Definition vmixdec.c:71