41 #define MAX_CHANNELS 2
70 #define LATTICE_SHIFT 10
71 #define SAMPLE_SHIFT 4
72 #define LATTICE_FACTOR (1 << LATTICE_SHIFT)
73 #define SAMPLE_FACTOR (1 << SAMPLE_SHIFT)
75 #define BASE_QUANT 0.6
76 #define RATE_VARIATION 3.0
80 return (a+(1<<(b-1))) >>
b;
93 for (i = 0; i < entries; i++)
103 for (i = 0; i < entries; i++)
111 #define ADAPT_LEVEL 8
113 static int bits_to_store(uint64_t x)
132 bits = bits_to_store(max);
134 for (i = 0; i < bits-1; i++)
137 if ( (value | (1 << (bits-1))) <= max)
138 put_bits(pb, 1, value & (1 << (bits-1)));
141 static unsigned int read_uint_max(
GetBitContext *gb,
int max)
143 int i,
bits, value = 0;
148 bits = bits_to_store(max);
150 for (i = 0; i < bits-1; i++)
154 if ( (value | (1<<(bits-1))) <= max)
156 value += 1 << (bits-1);
163 int i, j, x = 0, low_bits = 0, max = 0;
164 int step = 256, pos = 0, dominant = 0, any = 0;
167 copy =
av_calloc(entries,
sizeof(*copy));
175 for (i = 0; i < entries; i++)
176 energy += abs(buf[i]);
178 low_bits = bits_to_store(energy / (entries * 2));
185 for (i = 0; i < entries; i++)
187 put_bits(pb, low_bits, abs(buf[i]));
188 copy[i] = abs(buf[i]) >> low_bits;
193 bits =
av_calloc(entries*max,
sizeof(*bits));
200 for (i = 0; i <= max; i++)
202 for (j = 0; j < entries; j++)
204 bits[x++] = copy[j] > i;
210 int steplet = step >> 8;
212 if (pos + steplet > x)
215 for (i = 0; i < steplet; i++)
216 if (bits[i+pos] != dominant)
224 step += step / ADAPT_LEVEL;
230 while (((pos + interloper) < x) && (bits[pos + interloper] == dominant))
234 write_uint_max(pb, interloper, (step >> 8) - 1);
236 pos += interloper + 1;
237 step -= step / ADAPT_LEVEL;
243 dominant = !dominant;
248 for (i = 0; i < entries; i++)
260 int i, low_bits = 0, x = 0;
261 int n_zeros = 0, step = 256, dominant = 0;
262 int pos = 0,
level = 0;
263 int *bits =
av_calloc(entries,
sizeof(*bits));
273 for (i = 0; i < entries; i++)
279 while (n_zeros < entries)
281 int steplet = step >> 8;
285 for (i = 0; i < steplet; i++)
286 bits[x++] = dominant;
291 step += step / ADAPT_LEVEL;
295 int actual_run = read_uint_max(gb, steplet-1);
299 for (i = 0; i < actual_run; i++)
300 bits[x++] = dominant;
302 bits[x++] = !dominant;
305 n_zeros += actual_run;
309 step -= step / ADAPT_LEVEL;
315 dominant = !dominant;
321 for (i = 0; n_zeros < entries; i++)
328 level += 1 << low_bits;
331 if (buf[pos] >=
level)
338 buf[pos] += 1 << low_bits;
347 for (i = 0; i < entries; i++)
361 for (i = order-2; i >= 0; i--)
363 int j, p, x = state[i];
365 for (j = 0, p = i+1; p < order; j++,p++)
379 int *k_ptr = &(k[order-2]),
380 *state_ptr = &(state[order-2]);
381 for (i = order-2; i >= 0; i--, k_ptr--, state_ptr--)
383 int k_value = *k_ptr, state_value = *state_ptr;
388 for (i = order-2; i >= 0; i--)
404 #if CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER
409 static void modified_levinson_durbin(
int *window,
int window_entries,
410 int *
out,
int out_entries,
int channels,
int *tap_quant)
415 memcpy(state, window, 4* window_entries);
417 for (i = 0; i < out_entries; i++)
419 int step = (i+1)*channels, k, j;
420 double xx = 0.0, xy = 0.0;
422 int *x_ptr = &(window[step]);
423 int *state_ptr = &(state[0]);
424 j = window_entries - step;
425 for (;j>0;j--,x_ptr++,state_ptr++)
427 double x_value = *x_ptr;
428 double state_value = *state_ptr;
429 xx += state_value*state_value;
430 xy += x_value*state_value;
433 for (j = 0; j <= (window_entries - step); j++);
435 double stepval = window[step+j];
436 double stateval = window[j];
439 xx += stateval*stateval;
440 xy += stepval*stateval;
446 k = (int)(floor(-xy/xx * (
double)
LATTICE_FACTOR / (double)(tap_quant[i]) + 0.5));
457 x_ptr = &(window[step]);
458 state_ptr = &(state[0]);
459 j = window_entries - step;
460 for (;j>0;j--,x_ptr++,state_ptr++)
462 int x_value = *x_ptr;
463 int state_value = *state_ptr;
468 for (j=0; j <= (window_entries - step); j++)
470 int stepval = window[step+j];
471 int stateval=state[j];
481 static inline int code_samplerate(
int samplerate)
485 case 44100:
return 0;
486 case 22050:
return 1;
487 case 11025:
return 2;
488 case 96000:
return 3;
489 case 48000:
return 4;
490 case 32000:
return 5;
491 case 24000:
return 6;
492 case 16000:
return 7;
591 av_log(avctx,
AV_LOG_INFO,
"Sonic: ver: %d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
621 int i, j, ch,
quant = 0, x = 0;
623 const short *samples = (
const int16_t*)frame->
data[0];
677 for (ch = 0; ch < s->
channels; ch++)
692 double energy1 = 0.0, energy2 = 0.0;
693 for (ch = 0; ch < s->
channels; ch++)
699 energy1 += fabs(sample);
709 if (energy2 > energy1)
715 quant = av_clip(quant, 1, 65534);
723 for (ch = 0; ch < s->
channels; ch++)
742 #if CONFIG_SONIC_DECODER
743 static const int samplerate_table[] =
744 { 44100, 22050, 11025, 96000, 48000, 32000, 24000, 16000, 8000 };
807 av_log(avctx,
AV_LOG_INFO,
"Sonic: ver: %d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
855 void *
data,
int *got_frame_ptr,
859 int buf_size = avpkt->
size;
866 if (buf_size == 0)
return 0;
871 samples = (int16_t *)frame->
data[0];
890 for (ch = 0; ch < s->
channels; ch++)
954 .
init = sonic_decode_init,
955 .
close = sonic_decode_close,
956 .
decode = sonic_decode_frame,
961 #if CONFIG_SONIC_ENCODER
968 .
init = sonic_encode_init,
969 .encode2 = sonic_encode_frame,
972 .close = sonic_encode_close,
976 #if CONFIG_SONIC_LS_ENCODER
977 AVCodec ff_sonic_ls_encoder = {
983 .
init = sonic_encode_init,
984 .encode2 = sonic_encode_frame,
987 .close = sonic_encode_close,
static int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part)
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and/or allocate data.
void * av_calloc(size_t nmemb, size_t size) av_malloc_attrib
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
static int shift(int a, int b)
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)
static void set_ue_golomb(PutBitContext *pb, int i)
write unsigned exp golomb code.
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
enum AVSampleFormat sample_fmt
audio sample format
const char * name
Name of the codec implementation.
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
static int get_bits_count(const GetBitContext *s)
bitstream reader API header.
static void copy(LZOContext *c, int cnt)
Copies bytes from input to output buffer with checking.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
static int get_ue_golomb(GetBitContext *gb)
read unsigned exp golomb code.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Libavcodec external API header.
static int put_bits_count(PutBitContext *s)
int frame_size
Number of samples per channel in an audio frame.
AVSampleFormat
Audio Sample Formats.
#define AV_LOG_INFO
Standard information.
static void set_se_golomb(PutBitContext *pb, int i)
write signed exp golomb code.
static av_const unsigned int ff_sqrt(unsigned int a)
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.
static void close(AVCodecParserContext *s)
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)
static void predictor_init_state(int *k, int *state, int order)
#define ROUNDED_DIV(a, b)
static int intlist_read(GetBitContext *gb, int *buf, int entries, int base_2_part)
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
#define CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
#define AVERROR_INVALIDDATA
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);ff_audio_convert_init_arm(ac);ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
int channels
number of audio channels
int * coded_samples[MAX_CHANNELS]
static const uint8_t * align_get_bits(GetBitContext *s)
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)
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
This structure stores compressed 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.
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...