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);
150 for(i=e-1; i>=0; i--){
164 for (i = 0; i < entries; i++)
174 for (i = 0; i < entries; i++)
184 for (i = 0; i < entries; i++)
194 for (i = 0; i < entries; i++)
202 #define ADAPT_LEVEL 8 204 static int bits_to_store(uint64_t x)
223 bits = bits_to_store(max);
225 for (i = 0; i < bits-1; i++)
228 if ( (
value | (1 << (bits-1))) <= max)
232 static unsigned int read_uint_max(
GetBitContext *gb,
int max)
234 int i, bits,
value = 0;
239 bits = bits_to_store(max);
241 for (i = 0; i < bits-1; i++)
245 if ( (value | (1<<(bits-1))) <= max)
247 value += 1 << (bits-1);
254 int i, j, x = 0, low_bits = 0, max = 0;
255 int step = 256, pos = 0, dominant = 0, any = 0;
258 copy =
av_calloc(entries,
sizeof(*copy));
266 for (i = 0; i < entries; i++)
267 energy += abs(
buf[i]);
269 low_bits = bits_to_store(energy / (entries * 2));
276 for (i = 0; i < entries; i++)
279 copy[i] = abs(
buf[i]) >> low_bits;
284 bits =
av_calloc(entries*max,
sizeof(*bits));
291 for (i = 0; i <= max; i++)
293 for (j = 0; j < entries; j++)
295 bits[x++] = copy[j] > i;
301 int steplet = step >> 8;
303 if (pos + steplet > x)
306 for (i = 0; i < steplet; i++)
307 if (bits[i+pos] != dominant)
315 step += step / ADAPT_LEVEL;
321 while (((pos + interloper) < x) && (bits[pos + interloper] == dominant))
325 write_uint_max(pb, interloper, (step >> 8) - 1);
327 pos += interloper + 1;
328 step -= step / ADAPT_LEVEL;
334 dominant = !dominant;
339 for (i = 0; i < entries; i++)
351 int i, low_bits = 0, x = 0;
352 int n_zeros = 0, step = 256, dominant = 0;
353 int pos = 0,
level = 0;
354 int *bits =
av_calloc(entries,
sizeof(*bits));
364 for (i = 0; i < entries; i++)
370 while (n_zeros < entries)
372 int steplet = step >> 8;
376 for (i = 0; i < steplet; i++)
377 bits[x++] = dominant;
382 step += step / ADAPT_LEVEL;
386 int actual_run = read_uint_max(gb, steplet-1);
390 for (i = 0; i < actual_run; i++)
391 bits[x++] = dominant;
393 bits[x++] = !dominant;
396 n_zeros += actual_run;
400 step -= step / ADAPT_LEVEL;
406 dominant = !dominant;
412 for (i = 0; n_zeros < entries; i++)
419 level += 1 << low_bits;
429 buf[pos] += 1 << low_bits;
438 for (i = 0; i < entries; i++)
452 for (i = order-2; i >= 0; i--)
454 int j, p, x = state[i];
456 for (j = 0, p = i+1; p < order; j++,p++)
470 int *k_ptr = &(k[order-2]),
471 *state_ptr = &(state[order-2]);
472 for (i = order-2; i >= 0; i--, k_ptr--, state_ptr--)
474 int k_value = *k_ptr, state_value = *state_ptr;
479 for (i = order-2; i >= 0; i--)
495 #if CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER 500 static int modified_levinson_durbin(
int *
window,
int window_entries,
509 memcpy(state,
window, 4* window_entries);
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;
540 k = (
int)(floor(-xy/xx * (
double)
LATTICE_FACTOR / (double)(tap_quant[i]) + 0.5));
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];
576 static inline int code_samplerate(
int samplerate)
580 case 44100:
return 0;
581 case 22050:
return 1;
582 case 11025:
return 2;
583 case 96000:
return 3;
584 case 48000:
return 4;
585 case 32000:
return 5;
586 case 24000:
return 6;
587 case 16000:
return 7;
695 av_log(avctx,
AV_LOG_INFO,
"Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
725 int i, j,
ch,
quant = 0, x = 0;
727 const short *samples = (
const int16_t*)frame->
data[0];
787 for (ch = 0; ch < s->
channels; ch++)
802 double energy1 = 0.0, energy2 = 0.0;
803 for (ch = 0; ch < s->
channels; ch++)
809 energy1 += fabs(sample);
819 if (energy2 > energy1)
825 quant = av_clip(quant, 1, 65534);
833 for (ch = 0; ch < s->
channels; ch++)
852 #if CONFIG_SONIC_DECODER 853 static const int samplerate_table[] =
854 { 44100, 22050, 11025, 96000, 48000, 32000, 24000, 16000, 8000 };
889 int sample_rate_index;
891 sample_rate_index =
get_bits(&gb, 4);
896 s->
samplerate = samplerate_table[sample_rate_index];
933 "number of taps times channels (%d * %d) larger than frame size %d\n",
938 av_log(avctx,
AV_LOG_INFO,
"Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
991 void *
data,
int *got_frame_ptr,
995 int buf_size = avpkt->
size;
1003 if (buf_size == 0)
return 0;
1008 samples = (int16_t *)frame->
data[0];
1012 memset(state, 128,
sizeof(state));
1029 for (ch = 0; ch < s->
channels; ch++)
1091 .
init = sonic_decode_init,
1092 .close = sonic_decode_close,
1093 .
decode = sonic_decode_frame,
1098 #if CONFIG_SONIC_ENCODER 1105 .
init = sonic_encode_init,
1106 .encode2 = sonic_encode_frame,
1109 .close = sonic_encode_close,
1113 #if CONFIG_SONIC_LS_ENCODER 1120 .
init = sonic_encode_init,
1121 .encode2 = sonic_encode_frame,
1124 .close = sonic_encode_close,
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)
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi - 0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(INT64_C(1)<< 63))) #define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={ FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64), };static void cpy1(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, len);} static void cpy2(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 2 *len);} static void cpy4(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 4 *len);} static void cpy8(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 8 *len);} AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags) { AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;if(channels==1){ in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);} ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map) { switch(av_get_bytes_per_sample(in_fmt)){ case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;} } if(HAVE_X86ASM &&HAVE_MMX) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);return ctx;} void swri_audio_convert_free(AudioConvert **ctx) { av_freep(ctx);} int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len) { int ch;int off=0;const int os=(out->planar ? 1 :out->ch_count) *out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask) { int planes=in->planar ? in->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;} if(ctx->out_simd_align_mask) { int planes=out->planar ? out->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;} if(ctx->simd_f &&!ctx->ch_map &&!misaligned){ off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){ if(out->planar==in->planar){ int planes=out->planar ? out->ch_count :1;for(ch=0;ch< planes;ch++){ ctx->simd_f(out-> ch ch
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
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_rac_terminate(RangeCoder *c)
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 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 ...
static void error(const char *err)
#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.
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])