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--){
115 put_rac(c, state+22+i, (a>>i)&1);
119 put_rac(c, state+11 + e, v < 0);
126 for(i=e-1; i>=0; i--){
131 put_rac(c, state+11 + 10, v < 0);
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)
226 bits = bits_to_store(max);
228 for (i = 0; i < bits-1; i++)
231 if ( (
value | (1 << (bits-1))) <= max)
242 bits = bits_to_store(max);
244 for (i = 0; i < bits-1; i++)
248 if ( (value | (1<<(bits-1))) <= max)
250 value += 1 << (bits-1);
257 int i, j, x = 0, low_bits = 0,
max = 0;
258 int step = 256,
pos = 0, dominant = 0, any = 0;
261 copy =
av_calloc(entries,
sizeof(*copy));
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++)
282 copy[
i] =
abs(buf[i]) >> low_bits;
294 for (i = 0; i <=
max; i++)
296 for (j = 0; j < entries; j++)
298 bits[x++] = copy[j] >
i;
304 int steplet = step >> 8;
306 if (
pos + steplet > x)
309 for (i = 0; i < steplet; i++)
310 if (bits[i+pos] != dominant)
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;
425 if (buf[pos] >=
level)
432 buf[
pos] += 1 << low_bits;
441 for (i = 0; i < entries; i++)
455 for (i = order-2; i >= 0; i--)
457 int j, p, x = state[
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 int modified_levinson_durbin(
int *
window,
int window_entries,
512 memcpy(state,
window, 4* window_entries);
514 for (i = 0; i < out_entries; i++)
517 double xx = 0.0, xy = 0.0;
519 int *x_ptr = &(
window[step]);
520 int *state_ptr = &(state[0]);
521 j = window_entries - step;
522 for (;j>0;j--,x_ptr++,state_ptr++)
524 double x_value = *x_ptr;
525 double state_value = *state_ptr;
526 xx += state_value*state_value;
527 xy += x_value*state_value;
530 for (j = 0; j <= (window_entries - step); j++);
532 double stepval =
window[step+j];
533 double stateval =
window[j];
536 xx += stateval*stateval;
537 xy += stepval*stateval;
543 k = (
int)(floor(-xy/xx * (
double)
LATTICE_FACTOR / (double)(tap_quant[i]) + 0.5));
555 state_ptr = &(state[0]);
556 j = window_entries - step;
557 for (;j>0;j--,x_ptr++,state_ptr++)
559 int x_value = *x_ptr;
560 int state_value = *state_ptr;
565 for (j=0; j <= (window_entries - step); j++)
567 int stepval =
window[step+j];
568 int stateval=state[j];
579 static inline int code_samplerate(
int samplerate)
583 case 44100:
return 0;
584 case 22050:
return 1;
585 case 11025:
return 2;
586 case 96000:
return 3;
587 case 48000:
return 4;
588 case 32000:
return 5;
589 case 24000:
return 6;
590 case 16000:
return 7;
698 av_log(avctx,
AV_LOG_INFO,
"Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
728 int i, j, ch,
quant = 0, x = 0;
730 const short *samples = (
const int16_t*)frame->
data[0];
790 for (ch = 0; ch < s->
channels; ch++)
805 double energy1 = 0.0, energy2 = 0.0;
806 for (ch = 0; ch < s->
channels; ch++)
812 energy1 += fabs(sample);
822 if (energy2 > energy1)
828 quant = av_clip(quant, 1, 65534);
836 for (ch = 0; ch < s->
channels; ch++)
855 #if CONFIG_SONIC_DECODER 856 static const int samplerate_table[] =
857 { 44100, 22050, 11025, 96000, 48000, 32000, 24000, 16000, 8000 };
892 int sample_rate_index;
894 sample_rate_index =
get_bits(&gb, 4);
899 s->
samplerate = samplerate_table[sample_rate_index];
936 "number of taps times channels (%d * %d) larger than frame size %d\n",
941 av_log(avctx,
AV_LOG_INFO,
"Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
992 void *
data,
int *got_frame_ptr,
996 int buf_size = avpkt->
size;
1000 int i,
quant, ch, j, ret;
1004 if (buf_size == 0)
return 0;
1009 samples = (int16_t *)frame->
data[0];
1013 memset(state, 128,
sizeof(state));
1030 for (ch = 0; ch < s->
channels; ch++)
1095 .
init = sonic_decode_init,
1096 .close = sonic_decode_close,
1097 .
decode = sonic_decode_frame,
1103 #if CONFIG_SONIC_ENCODER 1110 .
init = sonic_encode_init,
1111 .encode2 = sonic_encode_frame,
1115 .close = sonic_encode_close,
1119 #if CONFIG_SONIC_LS_ENCODER 1126 .
init = sonic_encode_init,
1127 .encode2 = sonic_encode_frame,
1131 .close = sonic_encode_close,
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
const struct AVCodec * codec
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static int shift(int a, int b)
static void copy(const float *p1, float *p2, const int length)
This structure describes decoded (raw) audio or video data.
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
int * predictor_state[MAX_CHANNELS]
static av_cold int init(AVCodecContext *avctx)
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
static void error(const char *err)
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
AVCodec ff_sonic_ls_encoder
enum AVSampleFormat sample_fmt
audio sample format
static int get_rac(RangeCoder *c, uint8_t *const state)
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
bitstream reader API header.
#define ROUNDED_DIV(a, b)
#define i(width, name, range_min, range_max)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
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 put_bits_count(PutBitContext *s)
static av_flatten int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
void ff_build_rac_states(RangeCoder *c, int factor, int max_p)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
#define FF_ARRAY_ELEMS(a)
int frame_size
Number of samples per channel in an audio frame.
#define AV_LOG_INFO
Standard information.
Libavcodec external API header.
static void set_se_golomb(PutBitContext *pb, int i)
write signed exp golomb code.
AVSampleFormat
Audio sample formats.
int sample_rate
samples per second
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
main external API structure.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
static unsigned int get_bits1(GetBitContext *s)
static void skip_bits(GetBitContext *s, int n)
av_cold void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size)
av_cold void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size)
static int intlist_write(RangeCoder *c, uint8_t *state, int *buf, int entries, int base_2_part)
static void predictor_init_state(int *k, int *state, int order)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int ff_rac_terminate(RangeCoder *c, int version)
Terminates the range coder.
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
int channels
number of audio channels
int * coded_samples[MAX_CHANNELS]
static int predictor_calc_error(int *k, int *state, int order, int error)
static enum AVSampleFormat sample_fmts[]
static int shift_down(int a, int b)
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
static int intlist_read(RangeCoder *c, uint8_t *state, int *buf, int entries, int base_2_part)
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])