41 #define MAX_CHANNELS 2
71 #define LATTICE_SHIFT 10
72 #define SAMPLE_SHIFT 4
73 #define LATTICE_FACTOR (1 << LATTICE_SHIFT)
74 #define SAMPLE_FACTOR (1 << SAMPLE_SHIFT)
76 #define BASE_QUANT 0.6
77 #define RATE_VARIATION 3.0
82 return -( (-a + b/2)/b );
89 return (a+(1<<(b-1))) >>
b;
94 return (a>>b)+((a<0)?1:0);
102 for (i = 0; i < entries; i++)
112 for (i = 0; i < entries; i++)
120 #define ADAPT_LEVEL 8
122 static int bits_to_store(uint64_t x)
141 bits = bits_to_store(max);
143 for (i = 0; i < bits-1; i++)
146 if ( (value | (1 << (bits-1))) <= max)
147 put_bits(pb, 1, value & (1 << (bits-1)));
150 static unsigned int read_uint_max(
GetBitContext *gb,
int max)
152 int i,
bits, value = 0;
157 bits = bits_to_store(max);
159 for (i = 0; i < bits-1; i++)
163 if ( (value | (1<<(bits-1))) <= max)
165 value += 1 << (bits-1);
172 int i, j, x = 0, low_bits = 0, max = 0;
173 int step = 256, pos = 0, dominant = 0, any = 0;
184 for (i = 0; i < entries; i++)
185 energy += abs(buf[i]);
187 low_bits = bits_to_store(energy / (entries * 2));
194 for (i = 0; i < entries; i++)
196 put_bits(pb, low_bits, abs(buf[i]));
197 copy[i] = abs(buf[i]) >> low_bits;
209 for (i = 0; i <= max; i++)
211 for (j = 0; j < entries; j++)
213 bits[x++] = copy[j] > i;
219 int steplet = step >> 8;
221 if (pos + steplet > x)
224 for (i = 0; i < steplet; i++)
225 if (bits[i+pos] != dominant)
233 step += step / ADAPT_LEVEL;
239 while (((pos + interloper) < x) && (bits[pos + interloper] == dominant))
243 write_uint_max(pb, interloper, (step >> 8) - 1);
245 pos += interloper + 1;
246 step -= step / ADAPT_LEVEL;
252 dominant = !dominant;
257 for (i = 0; i < entries; i++)
269 int i, low_bits = 0, x = 0;
270 int n_zeros = 0, step = 256, dominant = 0;
271 int pos = 0,
level = 0;
282 for (i = 0; i < entries; i++)
288 while (n_zeros < entries)
290 int steplet = step >> 8;
294 for (i = 0; i < steplet; i++)
295 bits[x++] = dominant;
300 step += step / ADAPT_LEVEL;
304 int actual_run = read_uint_max(gb, steplet-1);
308 for (i = 0; i < actual_run; i++)
309 bits[x++] = dominant;
311 bits[x++] = !dominant;
314 n_zeros += actual_run;
318 step -= step / ADAPT_LEVEL;
324 dominant = !dominant;
330 for (i = 0; n_zeros < entries; i++)
337 level += 1 << low_bits;
340 if (buf[pos] >=
level)
347 buf[pos] += 1 << low_bits;
356 for (i = 0; i < entries; i++)
370 for (i = order-2; i >= 0; i--)
372 int j, p, x = state[i];
374 for (j = 0, p = i+1; p < order; j++,p++)
388 int *k_ptr = &(k[order-2]),
389 *state_ptr = &(state[order-2]);
390 for (i = order-2; i >= 0; i--, k_ptr--, state_ptr--)
392 int k_value = *k_ptr, state_value = *state_ptr;
397 for (i = order-2; i >= 0; i--)
413 #if CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER
418 static void modified_levinson_durbin(
int *window,
int window_entries,
419 int *
out,
int out_entries,
int channels,
int *tap_quant)
424 memcpy(state, window, 4* window_entries);
426 for (i = 0; i < out_entries; i++)
428 int step = (i+1)*channels, k, j;
429 double xx = 0.0, xy = 0.0;
431 int *x_ptr = &(window[step]), *state_ptr = &(state[0]);
432 j = window_entries - step;
433 for (;j>=0;j--,x_ptr++,state_ptr++)
435 double x_value = *x_ptr, state_value = *state_ptr;
436 xx += state_value*state_value;
437 xy += x_value*state_value;
440 for (j = 0; j <= (window_entries - step); j++);
442 double stepval = window[step+j], stateval = window[j];
445 xx += stateval*stateval;
446 xy += stepval*stateval;
452 k = (int)(floor(-xy/xx * (
double)
LATTICE_FACTOR / (double)(tap_quant[i]) + 0.5));
463 x_ptr = &(window[step]);
464 state_ptr = &(state[0]);
465 j = window_entries - step;
466 for (;j>=0;j--,x_ptr++,state_ptr++)
468 int x_value = *x_ptr, state_value = *state_ptr;
473 for (j=0; j <= (window_entries - step); j++)
475 int stepval = window[step+j], stateval=state[j];
485 static inline int code_samplerate(
int samplerate)
489 case 44100:
return 0;
490 case 22050:
return 1;
491 case 11025:
return 2;
492 case 96000:
return 3;
493 case 48000:
return 4;
494 case 32000:
return 5;
495 case 24000:
return 6;
496 case 16000:
return 7;
595 av_log(avctx,
AV_LOG_INFO,
"Sonic: ver: %d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
631 int i, j, ch,
quant = 0, x = 0;
633 const short *
samples = (
const int16_t*)frame->
data[0];
687 for (ch = 0; ch < s->
channels; ch++)
702 double energy1 = 0.0, energy2 = 0.0;
703 for (ch = 0; ch < s->
channels; ch++)
709 energy1 += fabs(sample);
719 if (energy2 > energy1)
736 for (ch = 0; ch < s->
channels; ch++)
755 #if CONFIG_SONIC_DECODER
756 static const int samplerate_table[] =
757 { 44100, 22050, 11025, 96000, 48000, 32000, 24000, 16000, 8000 };
819 av_log(avctx,
AV_LOG_INFO,
"Sonic: ver: %d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
867 void *
data,
int *got_frame_ptr,
871 int buf_size = avpkt->
size;
874 int i,
quant, ch, j, ret;
877 if (buf_size == 0)
return 0;
903 for (ch = 0; ch < s->
channels; ch++)
967 .
init = sonic_decode_init,
968 .
close = sonic_decode_close,
969 .
decode = sonic_decode_frame,
975 #if CONFIG_SONIC_ENCODER
981 .
init = sonic_encode_init,
982 .encode2 = sonic_encode_frame,
984 .
close = sonic_encode_close,
989 #if CONFIG_SONIC_LS_ENCODER
990 AVCodec ff_sonic_ls_encoder = {
995 .
init = sonic_encode_init,
996 .encode2 = sonic_encode_frame,
998 .
close = sonic_encode_close,