Go to the documentation of this file.
22 #include "config_components.h"
50 #define MAX_CHANNELS 2
81 #define LATTICE_SHIFT 10
82 #define SAMPLE_SHIFT 4
83 #define LATTICE_FACTOR (1 << LATTICE_SHIFT)
84 #define SAMPLE_FACTOR (1 << SAMPLE_SHIFT)
86 #define BASE_QUANT 0.6
87 #define RATE_VARIATION 3.0
91 return (
a+(1<<(
b-1))) >>
b;
100 #if CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER
105 static void modified_levinson_durbin(
int *
window,
int window_entries,
106 int *
out,
int out_entries,
int channels,
int *tap_quant)
113 for (
i = 0;
i < out_entries;
i++)
116 double xx = 0.0, xy = 0.0;
118 int *state_ptr = &(
state[0]);
119 j = window_entries -
step;
120 for (;j>0;j--,x_ptr++,state_ptr++)
122 double x_value = *x_ptr;
123 double state_value = *state_ptr;
124 xx += state_value*state_value;
125 xy += x_value*state_value;
141 state_ptr = &(
state[0]);
142 j = window_entries -
step;
143 for (;j>0;j--,x_ptr++,state_ptr++)
145 int x_value = *x_ptr;
146 int state_value = *state_ptr;
153 static inline int code_samplerate(
int samplerate)
157 case 44100:
return 0;
158 case 22050:
return 1;
159 case 11025:
return 2;
160 case 96000:
return 3;
161 case 48000:
return 4;
162 case 32000:
return 5;
163 case 24000:
return 6;
164 case 16000:
return 7;
188 s->decorrelation = 3;
195 s->quantization = 0.0;
201 s->quantization = 1.0;
205 if (
s->num_taps < 32 ||
s->num_taps > 1024 ||
s->num_taps % 32) {
211 s->tap_quant =
av_calloc(
s->num_taps,
sizeof(*
s->tap_quant));
215 for (
i = 0;
i <
s->num_taps;
i++)
221 s->block_align = 2048LL*
s->samplerate/(44100*
s->downsampling);
222 s->frame_size =
s->channels*
s->block_align*
s->downsampling;
224 s->tail_size =
s->num_taps*
s->channels;
229 s->predictor_k =
av_calloc(
s->num_taps,
sizeof(*
s->predictor_k) );
233 coded_samples =
av_calloc(
s->block_align,
s->channels *
sizeof(**
s->coded_samples));
236 for (
i = 0;
i <
s->channels;
i++, coded_samples +=
s->block_align)
237 s->coded_samples[
i] = coded_samples;
239 s->int_samples =
av_calloc(
s->frame_size,
sizeof(*
s->int_samples));
241 s->window_size = ((2*
s->tail_size)+
s->frame_size);
242 s->window =
av_calloc(
s->window_size, 2 *
sizeof(*
s->window));
243 if (!
s->window || !
s->int_samples)
254 if (
s->version >= 2) {
259 put_bits(&pb, 4, code_samplerate(
s->samplerate));
272 av_log(avctx,
AV_LOG_INFO,
"Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
273 s->version,
s->minor_version,
s->lossless,
s->decorrelation,
s->num_taps,
s->block_align,
s->frame_size,
s->downsampling);
297 #define put_rac(C,S,B) \
301 rc_stat2[(S)-state][B]++;\
316 for(
i=e-1;
i>=0;
i--){
328 for(
i=e-1;
i>=0;
i--){
341 static inline int intlist_write(
RangeCoder *
c, uint8_t *
state,
int *buf,
int entries,
int base_2_part)
345 for (
i = 0;
i < entries;
i++)
356 int i, j, ch,
quant = 0, x = 0;
369 for (
i = 0;
i <
s->frame_size;
i++)
373 for (
i = 0;
i <
s->frame_size;
i++)
376 switch(
s->decorrelation)
379 for (
i = 0;
i <
s->frame_size;
i +=
s->channels)
381 s->int_samples[
i] +=
s->int_samples[
i+1];
382 s->int_samples[
i+1] -=
shift(
s->int_samples[
i], 1);
386 for (
i = 0;
i <
s->frame_size;
i +=
s->channels)
387 s->int_samples[
i+1] -=
s->int_samples[
i];
390 for (
i = 0;
i <
s->frame_size;
i +=
s->channels)
391 s->int_samples[
i] -=
s->int_samples[
i+1];
395 memset(
s->window, 0,
s->window_size *
sizeof(*
s->window));
397 for (
i = 0;
i <
s->tail_size;
i++)
398 s->window[x++] =
s->tail[
i];
400 for (
i = 0;
i <
s->frame_size;
i++)
401 s->window[x++] =
s->int_samples[
i];
403 for (
i = 0;
i <
s->tail_size;
i++)
406 for (
i = 0;
i <
s->tail_size;
i++)
407 s->tail[
i] =
s->int_samples[
s->frame_size -
s->tail_size +
i];
410 modified_levinson_durbin(
s->window,
s->window_size,
411 s->predictor_k,
s->num_taps,
s->channels,
s->tap_quant);
413 if ((
ret = intlist_write(&
c,
state,
s->predictor_k,
s->num_taps, 0)) < 0)
416 for (ch = 0; ch <
s->channels; ch++)
419 for (
i = 0;
i <
s->block_align;
i++)
422 for (j = 0; j <
s->downsampling; j++, x +=
s->channels)
424 s->coded_samples[ch][
i] = sum;
431 double energy1 = 0.0, energy2 = 0.0;
432 for (ch = 0; ch <
s->channels; ch++)
434 for (
i = 0;
i <
s->block_align;
i++)
436 double sample =
s->coded_samples[ch][
i];
442 energy2 = sqrt(energy2/(
s->channels*
s->block_align));
443 energy1 =
M_SQRT2*energy1/(
s->channels*
s->block_align);
448 if (energy2 > energy1)
462 for (ch = 0; ch <
s->channels; ch++)
465 for (
i = 0;
i <
s->block_align;
i++)
468 if ((
ret = intlist_write(&
c,
state,
s->coded_samples[ch],
s->block_align, 1)) < 0)
479 #if CONFIG_SONIC_DECODER
480 static const int samplerate_table[] =
481 { 44100, 22050, 11025, 96000, 48000, 32000, 24000, 16000, 8000 };
505 if (
s->version >= 2) {
517 int sample_rate_index;
519 sample_rate_index =
get_bits(&gb, 4);
524 s->samplerate = samplerate_table[sample_rate_index];
526 s->channels,
s->samplerate);
542 if (
s->decorrelation != 3 &&
s->channels != 2) {
548 if (!
s->downsampling) {
557 if (
s->num_taps > 128)
560 s->block_align = 2048LL*
s->samplerate/(44100*
s->downsampling);
561 s->frame_size =
s->channels*
s->block_align*
s->downsampling;
564 if (
s->num_taps *
s->channels >
s->frame_size) {
566 "number of taps times channels (%d * %d) larger than frame size %d\n",
567 s->num_taps,
s->channels,
s->frame_size);
571 av_log(avctx,
AV_LOG_INFO,
"Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
572 s->version,
s->minor_version,
s->lossless,
s->decorrelation,
s->num_taps,
s->block_align,
s->frame_size,
s->downsampling);
575 s->tap_quant =
av_calloc(
s->num_taps,
sizeof(*
s->tap_quant));
579 for (
i = 0;
i <
s->num_taps;
i++)
582 s->predictor_k =
av_calloc(
s->num_taps,
sizeof(*
s->predictor_k));
584 tmp =
av_calloc(
s->num_taps,
s->channels *
sizeof(**
s->predictor_state));
587 for (
i = 0;
i <
s->channels;
i++,
tmp +=
s->num_taps)
588 s->predictor_state[
i] =
tmp;
590 tmp =
av_calloc(
s->block_align,
s->channels *
sizeof(**
s->coded_samples));
593 for (
i = 0;
i <
s->channels;
i++,
tmp +=
s->block_align)
594 s->coded_samples[
i] =
tmp;
596 s->int_samples =
av_calloc(
s->frame_size,
sizeof(*
s->int_samples));
631 for(
i=e-1;
i>=0;
i--){
644 for (
i = 0;
i < entries;
i++)
654 for (
i = order-2;
i >= 0;
i--)
658 for (j = 0, p =
i+1; p < order; j++,p++)
671 int *k_ptr = &(k[order-2]),
672 *state_ptr = &(
state[order-2]);
673 for (
i = order-2;
i >= 0;
i--, k_ptr--, state_ptr--)
675 int k_value = *k_ptr, state_value = *state_ptr;
690 int *got_frame_ptr,
AVPacket *avpkt)
692 const uint8_t *buf = avpkt->
data;
693 int buf_size = avpkt->
size;
700 if (buf_size == 0)
return 0;
716 for (
i = 0;
i <
s->num_taps;
i++)
717 s->predictor_k[
i] *= (
unsigned)
s->tap_quant[
i];
726 for (ch = 0; ch <
s->channels; ch++)
737 for (
i = 0;
i <
s->block_align;
i++)
739 for (j = 0; j <
s->downsampling - 1; j++)
749 for (
i = 0;
i <
s->num_taps;
i++)
750 s->predictor_state[ch][
i] =
s->int_samples[
s->frame_size -
s->channels + ch -
i*
s->channels];
753 switch(
s->decorrelation)
756 for (
i = 0;
i <
s->frame_size;
i +=
s->channels)
758 s->int_samples[
i+1] +=
shift(
s->int_samples[
i], 1);
759 s->int_samples[
i] -=
s->int_samples[
i+1];
763 for (
i = 0;
i <
s->frame_size;
i +=
s->channels)
764 s->int_samples[
i+1] +=
s->int_samples[
i];
767 for (
i = 0;
i <
s->frame_size;
i +=
s->channels)
768 s->int_samples[
i] +=
s->int_samples[
i+1];
773 for (
i = 0;
i <
s->frame_size;
i++)
777 for (
i = 0;
i <
s->frame_size;
i++)
791 .
init = sonic_decode_init,
792 .
close = sonic_decode_close,
799 #if CONFIG_SONIC_ENCODER
808 .
init = sonic_encode_init,
812 .close = sonic_encode_close,
816 #if CONFIG_SONIC_LS_ENCODER
825 .
init = sonic_encode_init,
829 .close = sonic_encode_close,
static void error(const char *err)
int frame_size
Number of samples per channel in an audio frame.
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
static int put_bytes_output(const PutBitContext *s)
int sample_rate
samples per second
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
This structure describes decoded (raw) audio or video data.
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
enum AVChannelOrder order
Channel order used in this layout.
int nb_channels
Number of channels in this layout.
static av_noinline void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed)
static void skip_bits(GetBitContext *s, int n)
static av_cold void close(AVCodecParserContext *s)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static SDL_Window * window
AVCodec p
The public AVCodec.
const struct AVCodec * codec
AVChannelLayout ch_layout
Audio channel layout.
av_cold void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size)
static int predictor_calc_error(int *k, int *state, int order, int error)
#define FF_CODEC_ENCODE_CB(func)
static const uint8_t quant[64]
exp golomb vlc writing stuff
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
#define FF_ARRAY_ELEMS(a)
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
#define FF_CODEC_DECODE_CB(func)
static __device__ float floor(float a)
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
int * coded_samples[MAX_CHANNELS]
#define CODEC_LONG_NAME(str)
const FFCodec ff_sonic_encoder
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
static __device__ float fabs(float a)
int ff_rac_terminate(RangeCoder *c, int version)
Terminates the range coder.
#define ROUNDED_DIV(a, b)
static unsigned int get_bits1(GetBitContext *s)
static int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
const FFCodec ff_sonic_decoder
av_cold void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
int(* init)(AVBSFContext *ctx)
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
enum AVSampleFormat sample_fmt
audio sample format
void ff_build_rac_states(RangeCoder *c, int factor, int max_p)
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
#define CODEC_SAMPLEFMTS(...)
#define AV_LOG_INFO
Standard information.
#define i(width, name, range_min, range_max)
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
const FFCodec ff_sonic_ls_encoder
@ AV_SAMPLE_FMT_S16
signed 16 bits
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
const char * name
Name of the codec implementation.
static int get_rac(RangeCoder *c, uint8_t *const state)
void * av_calloc(size_t nmemb, size_t size)
int * predictor_state[MAX_CHANNELS]
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
main external API structure.
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
static int intlist_read(BonkContext *s, int *buf, int entries, int base_2_part)
Filter the word “frame” indicates either a video frame or a group of audio samples
static int shift(int a, int b)
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
This structure stores compressed data.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static int shift_down(int a, int b)
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
static void predictor_init_state(int *k, unsigned *state, int order)