43 #define MAX_CHANNELS 2
74 #define LATTICE_SHIFT 10
75 #define SAMPLE_SHIFT 4
76 #define LATTICE_FACTOR (1 << LATTICE_SHIFT)
77 #define SAMPLE_FACTOR (1 << SAMPLE_SHIFT)
79 #define BASE_QUANT 0.6
80 #define RATE_VARIATION 3.0
84 return (
a+(1<<(
b-1))) >>
b;
95 #define put_rac(C,S,B) \
99 rc_stat2[(S)-state][B]++;\
114 for(
i=e-1;
i>=0;
i--){
126 for(
i=e-1;
i>=0;
i--){
153 for(
i=e-1;
i>=0;
i--){
167 for (
i = 0;
i < entries;
i++)
177 for (
i = 0;
i < entries;
i++)
187 for (
i = 0;
i < entries;
i++)
197 for (
i = 0;
i < entries;
i++)
205 #define ADAPT_LEVEL 8
207 static int bits_to_store(uint64_t x)
257 int i, j, x = 0, low_bits = 0,
max = 0;
258 int step = 256,
pos = 0, dominant = 0, any = 0;
269 for (
i = 0;
i < entries;
i++)
270 energy +=
abs(buf[
i]);
272 low_bits = bits_to_store(energy / (entries * 2));
279 for (
i = 0;
i < entries;
i++)
294 for (
i = 0;
i <=
max;
i++)
296 for (j = 0; j < entries; j++)
304 int steplet = step >> 8;
306 if (
pos + steplet > x)
309 for (
i = 0;
i < steplet;
i++)
318 step += step / ADAPT_LEVEL;
324 while (((
pos + interloper) < x) && (
bits[
pos + interloper] == dominant))
328 write_uint_max(pb, interloper, (step >> 8) - 1);
330 pos += interloper + 1;
331 step -= step / ADAPT_LEVEL;
337 dominant = !dominant;
342 for (
i = 0;
i < entries;
i++)
354 int i, low_bits = 0, x = 0;
355 int n_zeros = 0, step = 256, dominant = 0;
367 for (
i = 0;
i < entries;
i++)
373 while (n_zeros < entries)
375 int steplet = step >> 8;
379 for (
i = 0;
i < steplet;
i++)
380 bits[x++] = dominant;
385 step += step / ADAPT_LEVEL;
389 int actual_run = read_uint_max(gb, steplet-1);
393 for (
i = 0;
i < actual_run;
i++)
394 bits[x++] = dominant;
396 bits[x++] = !dominant;
399 n_zeros += actual_run;
403 step -= step / ADAPT_LEVEL;
409 dominant = !dominant;
415 for (
i = 0; n_zeros < entries;
i++)
422 level += 1 << low_bits;
432 buf[
pos] += 1 << low_bits;
441 for (
i = 0;
i < entries;
i++)
455 for (
i = order-2;
i >= 0;
i--)
459 for (j = 0, p =
i+1; p < order; j++,p++)
473 int *k_ptr = &(k[order-2]),
474 *state_ptr = &(
state[order-2]);
475 for (
i = order-2;
i >= 0;
i--, k_ptr--, state_ptr--)
477 int k_value = *k_ptr, state_value = *state_ptr;
482 for (
i = order-2;
i >= 0;
i--)
498 #if CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER
503 static void modified_levinson_durbin(
int *
window,
int window_entries,
504 int *
out,
int out_entries,
int channels,
int *tap_quant)
511 for (
i = 0;
i < out_entries;
i++)
514 double xx = 0.0, xy = 0.0;
516 int *x_ptr = &(
window[step]);
517 int *state_ptr = &(
state[0]);
518 j = window_entries - step;
519 for (;j>0;j--,x_ptr++,state_ptr++)
521 double x_value = *x_ptr;
522 double state_value = *state_ptr;
523 xx += state_value*state_value;
524 xy += x_value*state_value;
527 for (j = 0; j <= (window_entries - step); j++);
529 double stepval =
window[step+j];
530 double stateval =
window[j];
533 xx += stateval*stateval;
534 xy += stepval*stateval;
552 state_ptr = &(
state[0]);
553 j = window_entries - step;
554 for (;j>0;j--,x_ptr++,state_ptr++)
556 int x_value = *x_ptr;
557 int state_value = *state_ptr;
562 for (j=0; j <= (window_entries - step); j++)
564 int stepval =
window[step+j];
565 int stateval=
state[j];
573 static inline int code_samplerate(
int samplerate)
577 case 44100:
return 0;
578 case 22050:
return 1;
579 case 11025:
return 2;
580 case 96000:
return 3;
581 case 48000:
return 4;
582 case 32000:
return 5;
583 case 24000:
return 6;
584 case 16000:
return 7;
608 s->decorrelation = 3;
615 s->quantization = 0.0;
621 s->quantization = 1.0;
625 if (
s->num_taps < 32 ||
s->num_taps > 1024 ||
s->num_taps % 32) {
631 s->tap_quant =
av_calloc(
s->num_taps,
sizeof(*
s->tap_quant));
635 for (
i = 0;
i <
s->num_taps;
i++)
641 s->block_align = 2048LL*
s->samplerate/(44100*
s->downsampling);
642 s->frame_size =
s->channels*
s->block_align*
s->downsampling;
644 s->tail_size =
s->num_taps*
s->channels;
649 s->predictor_k =
av_calloc(
s->num_taps,
sizeof(*
s->predictor_k) );
653 coded_samples =
av_calloc(
s->block_align,
s->channels *
sizeof(**
s->coded_samples));
656 for (
i = 0;
i <
s->channels;
i++, coded_samples +=
s->block_align)
657 s->coded_samples[
i] = coded_samples;
659 s->int_samples =
av_calloc(
s->frame_size,
sizeof(*
s->int_samples));
661 s->window_size = ((2*
s->tail_size)+
s->frame_size);
662 s->window =
av_calloc(
s->window_size, 2 *
sizeof(*
s->window));
663 if (!
s->window || !
s->int_samples)
674 if (
s->version >= 2) {
679 put_bits(&pb, 4, code_samplerate(
s->samplerate));
692 av_log(avctx,
AV_LOG_INFO,
"Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
693 s->version,
s->minor_version,
s->lossless,
s->decorrelation,
s->num_taps,
s->block_align,
s->frame_size,
s->downsampling);
719 int i, j, ch,
quant = 0, x = 0;
721 const short *samples = (
const int16_t*)
frame->
data[0];
732 for (
i = 0;
i <
s->frame_size;
i++)
733 s->int_samples[
i] = samples[
i];
736 for (
i = 0;
i <
s->frame_size;
i++)
739 switch(
s->decorrelation)
742 for (
i = 0;
i <
s->frame_size;
i +=
s->channels)
744 s->int_samples[
i] +=
s->int_samples[
i+1];
745 s->int_samples[
i+1] -=
shift(
s->int_samples[
i], 1);
749 for (
i = 0;
i <
s->frame_size;
i +=
s->channels)
750 s->int_samples[
i+1] -=
s->int_samples[
i];
753 for (
i = 0;
i <
s->frame_size;
i +=
s->channels)
754 s->int_samples[
i] -=
s->int_samples[
i+1];
758 memset(
s->window, 0,
s->window_size *
sizeof(*
s->window));
760 for (
i = 0;
i <
s->tail_size;
i++)
761 s->window[x++] =
s->tail[
i];
763 for (
i = 0;
i <
s->frame_size;
i++)
764 s->window[x++] =
s->int_samples[
i];
766 for (
i = 0;
i <
s->tail_size;
i++)
769 for (
i = 0;
i <
s->tail_size;
i++)
770 s->tail[
i] =
s->int_samples[
s->frame_size -
s->tail_size +
i];
773 modified_levinson_durbin(
s->window,
s->window_size,
774 s->predictor_k,
s->num_taps,
s->channels,
s->tap_quant);
779 for (ch = 0; ch <
s->channels; ch++)
782 for (
i = 0;
i <
s->block_align;
i++)
785 for (j = 0; j <
s->downsampling; j++, x +=
s->channels)
787 s->coded_samples[ch][
i] = sum;
794 double energy1 = 0.0, energy2 = 0.0;
795 for (ch = 0; ch <
s->channels; ch++)
797 for (
i = 0;
i <
s->block_align;
i++)
799 double sample =
s->coded_samples[ch][
i];
805 energy2 = sqrt(energy2/(
s->channels*
s->block_align));
806 energy1 =
M_SQRT2*energy1/(
s->channels*
s->block_align);
811 if (energy2 > energy1)
825 for (ch = 0; ch <
s->channels; ch++)
828 for (
i = 0;
i <
s->block_align;
i++)
844 #if CONFIG_SONIC_DECODER
845 static const int samplerate_table[] =
846 { 44100, 22050, 11025, 96000, 48000, 32000, 24000, 16000, 8000 };
870 if (
s->version >= 2) {
882 int sample_rate_index;
884 sample_rate_index =
get_bits(&gb, 4);
889 s->samplerate = samplerate_table[sample_rate_index];
891 s->channels,
s->samplerate);
905 if (
s->decorrelation != 3 &&
s->channels != 2) {
911 if (!
s->downsampling) {
920 s->block_align = 2048LL*
s->samplerate/(44100*
s->downsampling);
921 s->frame_size =
s->channels*
s->block_align*
s->downsampling;
924 if (
s->num_taps *
s->channels >
s->frame_size) {
926 "number of taps times channels (%d * %d) larger than frame size %d\n",
927 s->num_taps,
s->channels,
s->frame_size);
931 av_log(avctx,
AV_LOG_INFO,
"Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
932 s->version,
s->minor_version,
s->lossless,
s->decorrelation,
s->num_taps,
s->block_align,
s->frame_size,
s->downsampling);
935 s->tap_quant =
av_calloc(
s->num_taps,
sizeof(*
s->tap_quant));
939 for (
i = 0;
i <
s->num_taps;
i++)
942 s->predictor_k =
av_calloc(
s->num_taps,
sizeof(*
s->predictor_k));
944 tmp =
av_calloc(
s->num_taps,
s->channels *
sizeof(**
s->predictor_state));
947 for (
i = 0;
i <
s->channels;
i++,
tmp +=
s->num_taps)
948 s->predictor_state[
i] =
tmp;
950 tmp =
av_calloc(
s->block_align,
s->channels *
sizeof(**
s->coded_samples));
953 for (
i = 0;
i <
s->channels;
i++,
tmp +=
s->block_align)
954 s->coded_samples[
i] =
tmp;
956 s->int_samples =
av_calloc(
s->frame_size,
sizeof(*
s->int_samples));
978 void *
data,
int *got_frame_ptr,
982 int buf_size = avpkt->
size;
990 if (buf_size == 0)
return 0;
1006 for (
i = 0;
i <
s->num_taps;
i++)
1007 s->predictor_k[
i] *= (
unsigned)
s->tap_quant[
i];
1016 for (ch = 0; ch <
s->channels; ch++)
1027 for (
i = 0;
i <
s->block_align;
i++)
1029 for (j = 0; j <
s->downsampling - 1; j++)
1039 for (
i = 0;
i <
s->num_taps;
i++)
1040 s->predictor_state[ch][
i] =
s->int_samples[
s->frame_size -
s->channels + ch -
i*
s->channels];
1043 switch(
s->decorrelation)
1046 for (
i = 0;
i <
s->frame_size;
i +=
s->channels)
1048 s->int_samples[
i+1] +=
shift(
s->int_samples[
i], 1);
1049 s->int_samples[
i] -=
s->int_samples[
i+1];
1053 for (
i = 0;
i <
s->frame_size;
i +=
s->channels)
1054 s->int_samples[
i+1] +=
s->int_samples[
i];
1057 for (
i = 0;
i <
s->frame_size;
i +=
s->channels)
1058 s->int_samples[
i] +=
s->int_samples[
i+1];
1063 for (
i = 0;
i <
s->frame_size;
i++)
1067 for (
i = 0;
i <
s->frame_size;
i++)
1081 .
init = sonic_decode_init,
1082 .close = sonic_decode_close,
1083 .
decode = sonic_decode_frame,
1089 #if CONFIG_SONIC_ENCODER
1096 .
init = sonic_encode_init,
1097 .encode2 = sonic_encode_frame,
1101 .close = sonic_encode_close,
1105 #if CONFIG_SONIC_LS_ENCODER
1112 .
init = sonic_encode_init,
1113 .encode2 = sonic_encode_frame,
1117 .close = sonic_encode_close,
static enum AVSampleFormat sample_fmts[]
AVCodec ff_sonic_ls_encoder
Libavcodec external API header.
static av_cold int init(AVCodecContext *avctx)
#define ROUNDED_DIV(a, b)
#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)
static __device__ float floor(float a)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
static SDL_Window * window
bitstream reader API header.
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 unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
static void set_se_golomb(PutBitContext *pb, int i)
write signed exp golomb code.
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_INFO
Standard information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
AVSampleFormat
Audio sample formats.
@ AV_SAMPLE_FMT_S16
signed 16 bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static int put_bits_count(PutBitContext *s)
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
int ff_rac_terminate(RangeCoder *c, int version)
Terminates the range coder.
av_cold void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size)
void ff_build_rac_states(RangeCoder *c, int factor, int max_p)
av_cold void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size)
static int get_rac(RangeCoder *c, uint8_t *const state)
#define FF_ARRAY_ELEMS(a)
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])
static int intlist_read(RangeCoder *c, uint8_t *state, int *buf, int entries, int base_2_part)
static int shift_down(int a, int b)
static av_flatten int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
static int predictor_calc_error(int *k, int *state, int order, int error)
static void predictor_init_state(int *k, int *state, int order)
static int intlist_write(RangeCoder *c, uint8_t *state, int *buf, int entries, int base_2_part)
static int shift(int a, int b)
main external API structure.
enum AVSampleFormat sample_fmt
audio sample format
const struct AVCodec * codec
int sample_rate
samples per second
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
int channels
number of audio channels
int frame_size
Number of samples per channel in an audio frame.
const char * name
Name of the codec implementation.
This structure describes decoded (raw) audio or video data.
int nb_samples
number of audio samples (per channel) described by this frame
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
This structure stores compressed data.
int * predictor_state[MAX_CHANNELS]
int * coded_samples[MAX_CHANNELS]
static void error(const char *err)
static void copy(const float *p1, float *p2, const int length)