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++)
953 .
init = sonic_decode_init,
954 .
close = sonic_decode_close,
955 .
decode = sonic_decode_frame,
961 #if CONFIG_SONIC_ENCODER
967 .
init = sonic_encode_init,
968 .encode2 = sonic_encode_frame,
971 .close = sonic_encode_close,
976 #if CONFIG_SONIC_LS_ENCODER
977 AVCodec ff_sonic_ls_encoder = {
982 .
init = sonic_encode_init,
983 .encode2 = sonic_encode_frame,
986 .close = sonic_encode_close,