26#define BITSTREAM_READER_LE
66 s->bitstream_size = 0;
89 s->nb_samples = UINT64_MAX;
93 if (!
s->n_taps ||
s->n_taps > 2048)
97 if (!
s->down_sampling)
101 if (!
s->samples_per_packet)
104 if (
s->down_sampling *
s->samples_per_packet <
s->n_taps)
115 s->input_samples =
av_calloc(
s->samples_per_packet,
sizeof(*
s->input_samples));
116 if (!
s->input_samples)
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])
124 s->bits =
av_calloc(
s->max_framesize * 8,
sizeof(*
s->bits));
128 for (
int i = 0;
i < 512;
i++) {
129 s->quant[
i] = sqrt(
i + 1);
153 int i, low_bits = 0, x = 0, max_x;
154 int n_zeros = 0, step = 256, dominant = 0;
159 memset(buf, 0, entries *
sizeof(*buf));
164 for (
i = 0;
i < entries;
i++)
168 while (n_zeros < entries) {
169 int steplet = step >> 8;
178 bits[x ].bit = dominant;
179 bits[x++].count = steplet;
185 if (step > INT32_MAX*8LL/9 + 1)
188 }
else if (steplet > 0) {
193 if (actual_run > 0) {
194 bits[x ].bit = dominant;
195 bits[x++].count = actual_run;
198 bits[x ].bit = !dominant;
202 n_zeros += actual_run;
211 dominant = !dominant;
218 for (
i = 0; n_zeros < entries;
i++) {
222 if (
pos >= entries) {
224 level += passes << low_bits;
226 if (
bits[x].
bit &&
bits[x].count > entries - n_zeros)
227 passes =
bits[x].count / (entries - n_zeros);
235 buf[
pos] += passes << low_bits;
240 bits[x].count -= passes;
241 x +=
bits[x].count == 0;
247 for (
i = 0;
i < entries;
i++) {
258 return (
a >>
b) + (
a < 0);
263 return a + (1 <<
b - 1) >>
b;
266#define LATTICE_SHIFT 10
267#define SAMPLE_SHIFT 4
268#define SAMPLE_FACTOR (1 << SAMPLE_SHIFT)
273 int *k_ptr = &(k[order-2]),
274 *state_ptr = &(
state[order-2]);
276 for (
i = order-2;
i >= 0;
i--, k_ptr--, state_ptr--) {
277 unsigned k_value = *k_ptr, state_value = *state_ptr;
293 for (
int i = order - 2;
i >= 0;
i--) {
296 for (
int j = 0, p =
i + 1; p < order; j++, p++) {
311 int quant, n, buf_size, input_buf_size;
314 if ((!
pkt->size && !
s->bitstream_size) ||
s->nb_samples == 0) {
319 buf_size =
FFMIN(
pkt->size,
s->max_framesize -
s->bitstream_size);
320 input_buf_size = buf_size;
322 memmove(
s->bitstream, &
s->bitstream[
s->bitstream_index],
s->bitstream_size);
323 s->bitstream_index = 0;
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) {
332 return input_buf_size;
335 frame->nb_samples =
FFMIN(
s->samples_per_packet *
s->down_sampling,
s->nb_samples);
346 for (
int i = 0;
i <
s->n_taps;
i++)
347 s->k[
i] *=
s->quant[
i];
352 const int down_sampling =
s->down_sampling;
354 int *
state =
s->state[ch];
362 for (
int j = 0; j <
s->down_sampling - 1; j++) {
372 for (
int i = 0;
i <
s->n_taps;
i++)
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];
385 int *samples =
s->samples[ch];
386 for (
int i = 0;
i <
frame->nb_samples;
i++)
387 samples[
i] =
shift(samples[
i], 4);
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++)
398 s->nb_samples -=
frame->nb_samples;
405 s->bitstream_size = 0;
406 s->bitstream_index = 0;
412 if (
s->bitstream_size) {
413 s->bitstream_index += n;
414 s->bitstream_size -= n;
415 return input_buf_size;
const FFCodec ff_bonk_decoder
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
static uint32_t samples_per_packet(const AVCodecParameters *par)
#define i(width, name, range_min, range_max)
#define bit(string, value)
#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...
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
int(* init)(AVBSFContext *ctx)
static struct @346255127015250356166251341105367306144006377143 state
static const uint8_t bits[8]
bitstream reader API header.
static int get_bits_left(GetBitContext *gb)
static unsigned int get_bits1(GetBitContext *s)
static void skip_bits(GetBitContext *s, int n)
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
static int get_bits_count(const GetBitContext *s)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
static void predictor_init_state(int *k, unsigned *state, int order)
static unsigned read_uint_max(BonkContext *s, uint32_t max)
static int shift_down(int a, int b)
static av_cold int bonk_init(AVCodecContext *avctx)
static int bonk_decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *pkt)
static av_cold int bonk_close(AVCodecContext *avctx)
static int predictor_calc_error(int *k, int *state, int order, int error)
static int intlist_read(BonkContext *s, int *buf, int entries, int base_2_part)
static int shift(int a, int b)
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
int nb_channels
Number of channels in this layout.
main external API structure.
AVChannelLayout ch_layout
Audio channel layout.
enum AVSampleFormat sample_fmt
audio sample format
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
static void error(const char *err)
static const uint8_t quant[64]