Go to the documentation of this file.
44 #define MAX_CHANNELS 2
75 #define LATTICE_SHIFT 10
76 #define SAMPLE_SHIFT 4
77 #define LATTICE_FACTOR (1 << LATTICE_SHIFT)
78 #define SAMPLE_FACTOR (1 << SAMPLE_SHIFT)
80 #define BASE_QUANT 0.6
81 #define RATE_VARIATION 3.0
85 return (
a+(1<<(
b-1))) >>
b;
96 #define put_rac(C,S,B) \
100 rc_stat2[(S)-state][B]++;\
115 for(
i=e-1;
i>=0;
i--){
127 for(
i=e-1;
i>=0;
i--){
154 for(
i=e-1;
i>=0;
i--){
168 for (
i = 0;
i < entries;
i++)
178 for (
i = 0;
i < entries;
i++)
188 for (
i = 0;
i < entries;
i++)
198 for (
i = 0;
i < entries;
i++)
206 #define ADAPT_LEVEL 8
208 static int bits_to_store(uint64_t x)
258 int i, j, x = 0, low_bits = 0,
max = 0;
259 int step = 256,
pos = 0, dominant = 0, any = 0;
270 for (
i = 0;
i < entries;
i++)
271 energy +=
abs(buf[
i]);
273 low_bits = bits_to_store(energy / (entries * 2));
280 for (
i = 0;
i < entries;
i++)
295 for (
i = 0;
i <=
max;
i++)
297 for (j = 0; j < entries; j++)
305 int steplet =
step >> 8;
307 if (
pos + steplet > x)
310 for (
i = 0;
i < steplet;
i++)
325 while (((
pos + interloper) < x) && (
bits[
pos + interloper] == dominant))
329 write_uint_max(pb, interloper, (
step >> 8) - 1);
331 pos += interloper + 1;
338 dominant = !dominant;
343 for (
i = 0;
i < entries;
i++)
355 int i, low_bits = 0, x = 0;
356 int n_zeros = 0,
step = 256, dominant = 0;
368 for (
i = 0;
i < entries;
i++)
374 while (n_zeros < entries)
376 int steplet =
step >> 8;
380 for (
i = 0;
i < steplet;
i++)
381 bits[x++] = dominant;
390 int actual_run = read_uint_max(gb, steplet-1);
394 for (
i = 0;
i < actual_run;
i++)
395 bits[x++] = dominant;
397 bits[x++] = !dominant;
400 n_zeros += actual_run;
410 dominant = !dominant;
416 for (
i = 0; n_zeros < entries;
i++)
423 level += 1 << low_bits;
433 buf[
pos] += 1 << low_bits;
442 for (
i = 0;
i < entries;
i++)
456 for (
i = order-2;
i >= 0;
i--)
460 for (j = 0, p =
i+1; p < order; j++,p++)
474 int *k_ptr = &(k[order-2]),
475 *state_ptr = &(
state[order-2]);
476 for (
i = order-2;
i >= 0;
i--, k_ptr--, state_ptr--)
478 int k_value = *k_ptr, state_value = *state_ptr;
483 for (
i = order-2;
i >= 0;
i--)
499 #if CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER
504 static void modified_levinson_durbin(
int *
window,
int window_entries,
505 int *
out,
int out_entries,
int channels,
int *tap_quant)
512 for (
i = 0;
i < out_entries;
i++)
515 double xx = 0.0, xy = 0.0;
518 int *state_ptr = &(
state[0]);
519 j = window_entries -
step;
520 for (;j>0;j--,x_ptr++,state_ptr++)
522 double x_value = *x_ptr;
523 double state_value = *state_ptr;
524 xx += state_value*state_value;
525 xy += x_value*state_value;
528 for (j = 0; j <= (window_entries -
step); j++);
531 double stateval =
window[j];
534 xx += stateval*stateval;
535 xy += stepval*stateval;
553 state_ptr = &(
state[0]);
554 j = window_entries -
step;
555 for (;j>0;j--,x_ptr++,state_ptr++)
557 int x_value = *x_ptr;
558 int state_value = *state_ptr;
563 for (j=0; j <= (window_entries -
step); j++)
566 int stateval=
state[j];
574 static inline int code_samplerate(
int samplerate)
578 case 44100:
return 0;
579 case 22050:
return 1;
580 case 11025:
return 2;
581 case 96000:
return 3;
582 case 48000:
return 4;
583 case 32000:
return 5;
584 case 24000:
return 6;
585 case 16000:
return 7;
609 s->decorrelation = 3;
616 s->quantization = 0.0;
622 s->quantization = 1.0;
626 if (
s->num_taps < 32 ||
s->num_taps > 1024 ||
s->num_taps % 32) {
632 s->tap_quant =
av_calloc(
s->num_taps,
sizeof(*
s->tap_quant));
636 for (
i = 0;
i <
s->num_taps;
i++)
642 s->block_align = 2048LL*
s->samplerate/(44100*
s->downsampling);
643 s->frame_size =
s->channels*
s->block_align*
s->downsampling;
645 s->tail_size =
s->num_taps*
s->channels;
650 s->predictor_k =
av_calloc(
s->num_taps,
sizeof(*
s->predictor_k) );
654 coded_samples =
av_calloc(
s->block_align,
s->channels *
sizeof(**
s->coded_samples));
657 for (
i = 0;
i <
s->channels;
i++, coded_samples +=
s->block_align)
658 s->coded_samples[
i] = coded_samples;
660 s->int_samples =
av_calloc(
s->frame_size,
sizeof(*
s->int_samples));
662 s->window_size = ((2*
s->tail_size)+
s->frame_size);
663 s->window =
av_calloc(
s->window_size, 2 *
sizeof(*
s->window));
664 if (!
s->window || !
s->int_samples)
675 if (
s->version >= 2) {
680 put_bits(&pb, 4, code_samplerate(
s->samplerate));
693 av_log(avctx,
AV_LOG_INFO,
"Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
694 s->version,
s->minor_version,
s->lossless,
s->decorrelation,
s->num_taps,
s->block_align,
s->frame_size,
s->downsampling);
720 int i, j, ch,
quant = 0, x = 0;
733 for (
i = 0;
i <
s->frame_size;
i++)
737 for (
i = 0;
i <
s->frame_size;
i++)
740 switch(
s->decorrelation)
743 for (
i = 0;
i <
s->frame_size;
i +=
s->channels)
745 s->int_samples[
i] +=
s->int_samples[
i+1];
746 s->int_samples[
i+1] -=
shift(
s->int_samples[
i], 1);
750 for (
i = 0;
i <
s->frame_size;
i +=
s->channels)
751 s->int_samples[
i+1] -=
s->int_samples[
i];
754 for (
i = 0;
i <
s->frame_size;
i +=
s->channels)
755 s->int_samples[
i] -=
s->int_samples[
i+1];
759 memset(
s->window, 0,
s->window_size *
sizeof(*
s->window));
761 for (
i = 0;
i <
s->tail_size;
i++)
762 s->window[x++] =
s->tail[
i];
764 for (
i = 0;
i <
s->frame_size;
i++)
765 s->window[x++] =
s->int_samples[
i];
767 for (
i = 0;
i <
s->tail_size;
i++)
770 for (
i = 0;
i <
s->tail_size;
i++)
771 s->tail[
i] =
s->int_samples[
s->frame_size -
s->tail_size +
i];
774 modified_levinson_durbin(
s->window,
s->window_size,
775 s->predictor_k,
s->num_taps,
s->channels,
s->tap_quant);
780 for (ch = 0; ch <
s->channels; ch++)
783 for (
i = 0;
i <
s->block_align;
i++)
786 for (j = 0; j <
s->downsampling; j++, x +=
s->channels)
788 s->coded_samples[ch][
i] = sum;
795 double energy1 = 0.0, energy2 = 0.0;
796 for (ch = 0; ch <
s->channels; ch++)
798 for (
i = 0;
i <
s->block_align;
i++)
800 double sample =
s->coded_samples[ch][
i];
806 energy2 = sqrt(energy2/(
s->channels*
s->block_align));
807 energy1 =
M_SQRT2*energy1/(
s->channels*
s->block_align);
812 if (energy2 > energy1)
826 for (ch = 0; ch <
s->channels; ch++)
829 for (
i = 0;
i <
s->block_align;
i++)
843 #if CONFIG_SONIC_DECODER
844 static const int samplerate_table[] =
845 { 44100, 22050, 11025, 96000, 48000, 32000, 24000, 16000, 8000 };
869 if (
s->version >= 2) {
881 int sample_rate_index;
883 sample_rate_index =
get_bits(&gb, 4);
888 s->samplerate = samplerate_table[sample_rate_index];
890 s->channels,
s->samplerate);
904 if (
s->decorrelation != 3 &&
s->channels != 2) {
910 if (!
s->downsampling) {
919 s->block_align = 2048LL*
s->samplerate/(44100*
s->downsampling);
920 s->frame_size =
s->channels*
s->block_align*
s->downsampling;
923 if (
s->num_taps *
s->channels >
s->frame_size) {
925 "number of taps times channels (%d * %d) larger than frame size %d\n",
926 s->num_taps,
s->channels,
s->frame_size);
930 av_log(avctx,
AV_LOG_INFO,
"Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
931 s->version,
s->minor_version,
s->lossless,
s->decorrelation,
s->num_taps,
s->block_align,
s->frame_size,
s->downsampling);
934 s->tap_quant =
av_calloc(
s->num_taps,
sizeof(*
s->tap_quant));
938 for (
i = 0;
i <
s->num_taps;
i++)
941 s->predictor_k =
av_calloc(
s->num_taps,
sizeof(*
s->predictor_k));
943 tmp =
av_calloc(
s->num_taps,
s->channels *
sizeof(**
s->predictor_state));
946 for (
i = 0;
i <
s->channels;
i++,
tmp +=
s->num_taps)
947 s->predictor_state[
i] =
tmp;
949 tmp =
av_calloc(
s->block_align,
s->channels *
sizeof(**
s->coded_samples));
952 for (
i = 0;
i <
s->channels;
i++,
tmp +=
s->block_align)
953 s->coded_samples[
i] =
tmp;
955 s->int_samples =
av_calloc(
s->frame_size,
sizeof(*
s->int_samples));
977 void *
data,
int *got_frame_ptr,
980 const uint8_t *buf = avpkt->
data;
981 int buf_size = avpkt->
size;
989 if (buf_size == 0)
return 0;
1005 for (
i = 0;
i <
s->num_taps;
i++)
1006 s->predictor_k[
i] *= (
unsigned)
s->tap_quant[
i];
1015 for (ch = 0; ch <
s->channels; ch++)
1026 for (
i = 0;
i <
s->block_align;
i++)
1028 for (j = 0; j <
s->downsampling - 1; j++)
1038 for (
i = 0;
i <
s->num_taps;
i++)
1039 s->predictor_state[ch][
i] =
s->int_samples[
s->frame_size -
s->channels + ch -
i*
s->channels];
1042 switch(
s->decorrelation)
1045 for (
i = 0;
i <
s->frame_size;
i +=
s->channels)
1047 s->int_samples[
i+1] +=
shift(
s->int_samples[
i], 1);
1048 s->int_samples[
i] -=
s->int_samples[
i+1];
1052 for (
i = 0;
i <
s->frame_size;
i +=
s->channels)
1053 s->int_samples[
i+1] +=
s->int_samples[
i];
1056 for (
i = 0;
i <
s->frame_size;
i +=
s->channels)
1057 s->int_samples[
i] +=
s->int_samples[
i+1];
1062 for (
i = 0;
i <
s->frame_size;
i++)
1066 for (
i = 0;
i <
s->frame_size;
i++)
1080 .
init = sonic_decode_init,
1081 .close = sonic_decode_close,
1082 .
decode = sonic_decode_frame,
1088 #if CONFIG_SONIC_ENCODER
1095 .
init = sonic_encode_init,
1096 .encode2 = sonic_encode_frame,
1100 .close = sonic_encode_close,
1104 #if CONFIG_SONIC_LS_ENCODER
1111 .
init = sonic_encode_init,
1112 .encode2 = sonic_encode_frame,
1116 .close = sonic_encode_close,
static void error(const char *err)
static int intlist_write(RangeCoder *c, uint8_t *state, int *buf, int entries, int base_2_part)
int frame_size
Number of samples per channel in an audio frame.
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
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 enum AVSampleFormat sample_fmts[]
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
const AVCodec ff_sonic_encoder
static void skip_bits(GetBitContext *s, int n)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static SDL_Window * window
const struct AVCodec * codec
av_cold void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size)
static int quant(float coef, const float Q, const float rounding)
Quantize one coefficient.
#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.
const AVCodec ff_sonic_decoder
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
static av_flatten int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
static __device__ float floor(float a)
static int predictor_calc_error(int *k, int *state, int order, int error)
int * coded_samples[MAX_CHANNELS]
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
static int intlist_read(RangeCoder *c, uint8_t *state, int *buf, int entries, int base_2_part)
static __device__ float fabs(float a)
static void predictor_init_state(int *k, int *state, int order)
int ff_rac_terminate(RangeCoder *c, int version)
Terminates the range coder.
#define ROUNDED_DIV(a, b)
static unsigned int get_bits1(GetBitContext *s)
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
static av_always_inline av_flatten void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed, uint64_t rc_stat[256][2], uint64_t rc_stat2[32][2])
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
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.
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static void copy(const float *p1, float *p2, const int length)
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 AV_LOG_INFO
Standard information.
int channels
number of audio channels
#define i(width, name, range_min, range_max)
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
AVSampleFormat
Audio sample formats.
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
@ 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.
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.
const AVCodec ff_sonic_ls_encoder
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 set_se_golomb(PutBitContext *pb, int i)
write signed exp golomb code.