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)
229 put_bits(pb, 1, value & (1 << (bits-1)));
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++)
278 put_bits(pb, low_bits, abs(buf[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;
422 if (buf[pos] >=
level)
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 void modified_levinson_durbin(
int *window,
int window_entries,
501 int *
out,
int out_entries,
int channels,
int *tap_quant)
506 memcpy(state, window, 4* window_entries);
508 for (i = 0; i < out_entries; i++)
510 int step = (i+1)*channels, k, j;
511 double xx = 0.0, xy = 0.0;
513 int *x_ptr = &(window[step]);
514 int *state_ptr = &(state[0]);
515 j = window_entries - step;
516 for (;j>0;j--,x_ptr++,state_ptr++)
518 double x_value = *x_ptr;
519 double state_value = *state_ptr;
520 xx += state_value*state_value;
521 xy += x_value*state_value;
524 for (j = 0; j <= (window_entries - step); j++);
526 double stepval = window[step+j];
527 double stateval = window[j];
530 xx += stateval*stateval;
531 xy += stepval*stateval;
537 k = (int)(floor(-xy/xx * (
double)
LATTICE_FACTOR / (double)(tap_quant[i]) + 0.5));
548 x_ptr = &(window[step]);
549 state_ptr = &(state[0]);
550 j = window_entries - step;
551 for (;j>0;j--,x_ptr++,state_ptr++)
553 int x_value = *x_ptr;
554 int state_value = *state_ptr;
559 for (j=0; j <= (window_entries - step); j++)
561 int stepval = window[step+j];
562 int stateval=state[j];
572 static inline int code_samplerate(
int samplerate)
576 case 44100:
return 0;
577 case 22050:
return 1;
578 case 11025:
return 2;
579 case 96000:
return 3;
580 case 48000:
return 4;
581 case 32000:
return 5;
582 case 24000:
return 6;
583 case 16000:
return 7;
688 av_log(avctx,
AV_LOG_INFO,
"Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
718 int i, j, ch,
quant = 0, x = 0;
720 const short *samples = (
const int16_t*)frame->
data[0];
728 memset(state, 128,
sizeof(state));
777 for (ch = 0; ch < s->
channels; ch++)
792 double energy1 = 0.0, energy2 = 0.0;
793 for (ch = 0; ch < s->
channels; ch++)
799 energy1 += fabs(sample);
809 if (energy2 > energy1)
815 quant = av_clip(quant, 1, 65534);
823 for (ch = 0; ch < s->
channels; ch++)
842 #if CONFIG_SONIC_DECODER
843 static const int samplerate_table[] =
844 { 44100, 22050, 11025, 96000, 48000, 32000, 24000, 16000, 8000 };
911 av_log(avctx,
AV_LOG_INFO,
"Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
959 void *
data,
int *got_frame_ptr,
963 int buf_size = avpkt->
size;
971 if (buf_size == 0)
return 0;
976 samples = (int16_t *)frame->
data[0];
980 memset(state, 128,
sizeof(state));
997 for (ch = 0; ch < s->
channels; ch++)
1059 .
init = sonic_decode_init,
1060 .
close = sonic_decode_close,
1061 .
decode = sonic_decode_frame,
1066 #if CONFIG_SONIC_ENCODER
1073 .
init = sonic_encode_init,
1074 .encode2 = sonic_encode_frame,
1077 .close = sonic_encode_close,
1081 #if CONFIG_SONIC_LS_ENCODER
1082 AVCodec ff_sonic_ls_encoder = {
1088 .
init = sonic_encode_init,
1089 .encode2 = sonic_encode_frame,
1092 .close = sonic_encode_close,