28 #define UNCHECKED_BITSTREAM_READER 1
50 #define MAX_LSPS_ALIGN16 16
53 #define MAX_FRAMESIZE 160
54 #define MAX_SIGNAL_HISTORY 416
55 #define MAX_SFRAMESIZE (MAX_FRAMESIZE * MAX_FRAMES)
57 #define SFRAME_CACHE_MAXSIZE 256
146 int spillover_bitsize;
149 int history_nsamples;
155 int denoise_strength;
157 int denoise_tilt_corr;
166 int frame_lsp_bitsize;
168 int sframe_lsp_bitsize;
175 int block_pitch_nbits;
178 int block_delta_pitch_nbits;
182 int block_delta_pitch_hrange;
184 uint16_t block_conv_table[4];
200 int has_residual_lsps;
212 int sframe_cache_size;
247 int aw_first_pulse_off[2];
249 int aw_next_pulse_off_cache;
257 float gain_pred_err[6];
276 float sin[511], cos[511];
278 float postfilter_agc;
312 10, 10, 10, 12, 12, 12,
315 static const uint16_t codes[] = {
316 0x0000, 0x0001, 0x0002,
317 0x000c, 0x000d, 0x000e,
318 0x003c, 0x003d, 0x003e,
319 0x00fc, 0x00fd, 0x00fe,
320 0x03fc, 0x03fd, 0x03fe,
321 0x0ffc, 0x0ffd, 0x0ffe,
322 0x3ffc, 0x3ffd, 0x3ffe, 0x3fff
324 int cntr[8] = { 0 }, n, res;
326 memset(vbm_tree, 0xff,
sizeof(vbm_tree[0]) * 25);
327 for (n = 0; n < 17; n++) {
331 vbm_tree[res * 3 + cntr[res]++] = n;
334 bits, 1, 1, codes, 2, 2, 132);
343 int n,
flags, pitch_range, lsp16_flag;
356 "Invalid extradata size %d (should be 46)\n",
370 memcpy(&s->
sin[255], s->
cos, 256 *
sizeof(s->
cos[0]));
371 for (n = 0; n < 255; n++) {
372 s->
sin[n] = -s->
sin[510 - n];
373 s->
cos[510 - n] = s->
cos[n];
379 "Invalid denoise filter strength %d (max=11)\n",
387 lsp16_flag = flags & 0x1000;
397 for (n = 0; n < s->
lsps; n++)
409 if (pitch_range <= 0) {
419 int min_sr = ((((1 << 8) - 50) * 400) + 0xFF) >> 8,
423 "Unsupported samplerate %d (min=%d, max=%d)\n",
476 const float *speech_synth,
477 int size,
float alpha,
float *gain_mem)
480 float speech_energy = 0.0, postfilter_energy = 0.0, gain_scale_factor;
481 float mem = *gain_mem;
483 for (i = 0; i <
size; i++) {
484 speech_energy += fabsf(speech_synth[i]);
485 postfilter_energy += fabsf(in[i]);
487 gain_scale_factor = (1.0 - alpha) * speech_energy / postfilter_energy;
489 for (i = 0; i <
size; i++) {
490 mem = alpha * mem + gain_scale_factor;
491 out[i] = in[i] * mem;
516 const float *in,
float *out,
int size)
519 float optimal_gain = 0, dot;
522 *best_hist_ptr =
NULL;
527 if (dot > optimal_gain) {
531 }
while (--ptr >= end);
533 if (optimal_gain <= 0)
539 if (optimal_gain <= dot) {
540 dot = dot / (dot + 0.6 * optimal_gain);
545 for (n = 0; n <
size; n++)
546 out[n] = best_hist_ptr[n] + dot * (in[n] - best_hist_ptr[n]);
575 int fcb_type,
float *
coeffs,
int remainder)
578 float irange, angle_mul, gain_mul, range, sq;
583 #define log_range(var, assign) do { \
584 float tmp = log10f(assign); var = tmp; \
585 max = FFMAX(max, tmp); min = FFMIN(min, tmp); \
587 log_range(last_coeff, lpcs[1] * lpcs[1]);
588 for (n = 1; n < 64; n++)
589 log_range(lpcs[n], lpcs[n * 2] * lpcs[n * 2] +
590 lpcs[n * 2 + 1] * lpcs[n * 2 + 1]);
601 irange = 64.0 / range;
605 for (n = 0; n <= 64; n++) {
608 idx =
FFMAX(0,
lrint((max - lpcs[n]) * irange) - 1);
610 lpcs[n] = angle_mul * pwr;
613 idx = (pwr * gain_mul - 0.0295) * 70.570526123;
616 powf(1.0331663, idx - 127);
629 idx = 255 + av_clip(lpcs[64], -255, 255);
630 coeffs[0] = coeffs[0] * s->
cos[idx];
631 idx = 255 + av_clip(lpcs[64] - 2 * lpcs[63], -255, 255);
632 last_coeff = coeffs[64] * s->
cos[idx];
634 idx = 255 + av_clip(-lpcs[64] - 2 * lpcs[n - 1], -255, 255);
635 coeffs[n * 2 + 1] = coeffs[n] * s->
sin[idx];
636 coeffs[n * 2] = coeffs[n] * s->
cos[idx];
640 idx = 255 + av_clip( lpcs[64] - 2 * lpcs[n - 1], -255, 255);
641 coeffs[n * 2 + 1] = coeffs[n] * s->
sin[idx];
642 coeffs[n * 2] = coeffs[n] * s->
cos[idx];
650 memset(&coeffs[remainder], 0,
sizeof(coeffs[0]) * (128 - remainder));
654 coeffs[remainder - 1] = 0;
660 for (n = 0; n < remainder; n++)
691 float *synth_pf,
int size,
694 int remainder, lim, n;
700 tilted_lpcs[0] = 1.0;
701 memcpy(&tilted_lpcs[1], lpcs,
sizeof(lpcs[0]) * s->
lsps);
702 memset(&tilted_lpcs[s->
lsps + 1], 0,
703 sizeof(tilted_lpcs[0]) * (128 - s->
lsps - 1));
705 tilted_lpcs, s->
lsps + 2);
711 remainder =
FFMIN(127 - size, size - 1);
716 memset(&synth_pf[size], 0,
sizeof(synth_pf[0]) * (128 - size));
721 for (n = 1; n < 64; n++) {
722 float v1 = synth_pf[n * 2], v2 = synth_pf[n * 2 + 1];
723 synth_pf[n * 2] = v1 *
coeffs[n * 2] - v2 *
coeffs[n * 2 + 1];
724 synth_pf[n * 2 + 1] = v2 * coeffs[n * 2] + v1 * coeffs[n * 2 + 1];
732 for (n = 0; n < lim; n++)
742 for (n = 0; n < lim; n++)
744 if (lim < remainder) {
774 const float *lpcs,
float *zero_exc_pf,
775 int fcb_type,
int pitch)
779 *synth_filter_in = zero_exc_pf;
788 synth_filter_in = synth_filter_in_buf;
792 synth_filter_in, size, s->
lsps);
793 memcpy(&synth_pf[-s->
lsps], &synth_pf[size - s->
lsps],
794 sizeof(synth_pf[0]) * s->
lsps);
806 (
const float[2]) { -1.99997, 1.0 },
807 (
const float[2]) { -1.9330735188, 0.93589198496 },
827 const uint16_t *values,
828 const uint16_t *
sizes,
831 const double *base_q)
835 memset(lsps, 0, num *
sizeof(*lsps));
836 for (n = 0; n < n_stages; n++) {
837 const uint8_t *t_off = &table[values[n] * num];
838 double base = base_q[n], mul = mul_q[n];
840 for (m = 0; m < num; m++)
841 lsps[m] += base + mul * t_off[m];
843 table += sizes[n] * num;
860 static const uint16_t vec_sizes[4] = { 256, 64, 32, 32 };
861 static const double mul_lsf[4] = {
862 5.2187144800e-3, 1.4626986422e-3,
863 9.6179549166e-4, 1.1325736225e-3
865 static const double base_lsf[4] = {
866 M_PI * -2.15522e-1,
M_PI * -6.1646e-2,
867 M_PI * -3.3486e-2,
M_PI * -5.7408e-2
885 double *i_lsps,
const double *old,
886 double *
a1,
double *
a2,
int q_mode)
888 static const uint16_t vec_sizes[3] = { 128, 64, 64 };
889 static const double mul_lsf[3] = {
890 2.5807601174e-3, 1.2354460219e-3, 1.1763821673e-3
892 static const double base_lsf[3] = {
893 M_PI * -1.07448e-1,
M_PI * -5.2706e-2,
M_PI * -5.1634e-2
895 const float (*ipol_tab)[2][10] = q_mode ?
907 for (n = 0; n < 10; n++) {
908 double delta = old[n] - i_lsps[n];
909 a1[n] = ipol_tab[
interpol][0][n] * delta + i_lsps[n];
910 a1[10 + n] = ipol_tab[
interpol][1][n] * delta + i_lsps[n];
922 static const uint16_t vec_sizes[5] = { 256, 64, 128, 64, 128 };
923 static const double mul_lsf[5] = {
924 3.3439586280e-3, 6.9908173703e-4,
925 3.3216608306e-3, 1.0334960326e-3,
928 static const double base_lsf[5] = {
929 M_PI * -1.27576e-1,
M_PI * -2.4292e-2,
930 M_PI * -1.28094e-1,
M_PI * -3.2128e-2,
954 double *i_lsps,
const double *old,
955 double *
a1,
double *
a2,
int q_mode)
957 static const uint16_t vec_sizes[3] = { 128, 128, 128 };
958 static const double mul_lsf[3] = {
959 1.2232979501e-3, 1.4062241527e-3, 1.6114744851e-3
961 static const double base_lsf[3] = {
964 const float (*ipol_tab)[2][16] = q_mode ?
976 for (n = 0; n < 16; n++) {
977 double delta = old[n] - i_lsps[n];
978 a1[n] = ipol_tab[
interpol][0][n] * delta + i_lsps[n];
979 a1[16 + n] = ipol_tab[
interpol][1][n] * delta + i_lsps[n];
1006 static const int16_t start_offset[94] = {
1007 -11, -9, -7, -5, -3, -1, 1, 3, 5, 7, 9, 11,
1008 13, 15, 18, 17, 19, 20, 21, 22, 23, 24, 25, 26,
1009 27, 28, 29, 30, 31, 32, 33, 35, 37, 39, 41, 43,
1010 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67,
1011 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91,
1012 93, 95, 97, 99, 101, 103, 105, 107, 109, 111, 113, 115,
1013 117, 119, 121, 123, 125, 127, 129, 131, 133, 135, 137, 139,
1014 141, 143, 145, 147, 149, 151, 153, 155, 157, 159
1020 if ((bits =
get_bits(gb, 6)) >= 54) {
1022 bits += (bits - 54) * 3 +
get_bits(gb, 2);
1028 for (offset = start_offset[bits]; offset < 0; offset += pitch[0]) ;
1041 if (start_offset[bits] < 0)
1057 uint16_t use_mask_mem[9];
1058 uint16_t *use_mask = use_mask_mem + 2;
1067 pulse_start, n, idx, range, aidx, start_off = 0;
1076 if (block_idx == 0) {
1085 pulse_start = s->
aw_n_pulses[block_idx] > 0 ? pulse_off - range / 2 : 0;
1090 memset(&use_mask[-2], 0, 2 *
sizeof(use_mask[0]));
1091 memset( use_mask, -1, 5 *
sizeof(use_mask[0]));
1092 memset(&use_mask[5], 0, 2 *
sizeof(use_mask[0]));
1096 uint16_t *use_mask_ptr = &use_mask[idx >> 4];
1097 int first_sh = 16 - (idx & 15);
1098 *use_mask_ptr++ &= 0xFFFFu << first_sh;
1099 excl_range -= first_sh;
1100 if (excl_range >= 16) {
1101 *use_mask_ptr++ = 0;
1102 *use_mask_ptr &= 0xFFFF >> (excl_range - 16);
1104 *use_mask_ptr &= 0xFFFF >> excl_range;
1109 for (n = 0; n <= aidx; pulse_start++) {
1110 for (idx = pulse_start; idx < 0; idx += fcb->
pitch_lag) ;
1112 if (use_mask[0]) idx = 0x0F;
1113 else if (use_mask[1]) idx = 0x1F;
1114 else if (use_mask[2]) idx = 0x2F;
1115 else if (use_mask[3]) idx = 0x3F;
1116 else if (use_mask[4]) idx = 0x4F;
1120 if (use_mask[idx >> 4] & (0x8000 >> (idx & 15))) {
1121 use_mask[idx >> 4] &= ~(0x8000 >> (idx & 15));
1127 fcb->
x[fcb->
n] = start_off;
1150 int n, v_mask, i_mask, sh, n_pulses;
1164 for (n = n_pulses - 1; n >= 0; n--, val >>= sh) {
1165 fcb->
y[fcb->
n] = (val & v_mask) ? -1.0 : 1.0;
1166 fcb->
x[fcb->
n] = (val & i_mask) * n_pulses + n +
1168 while (fcb->
x[fcb->
n] < 0)
1174 int num2 = (val & 0x1FF) >> 1,
delta, idx;
1176 if (num2 < 1 * 79) {
delta = 1; idx = num2 + 1; }
1177 else if (num2 < 2 * 78) {
delta = 3; idx = num2 + 1 - 1 * 77; }
1178 else if (num2 < 3 * 77) {
delta = 5; idx = num2 + 1 - 2 * 76; }
1179 else {
delta = 7; idx = num2 + 1 - 3 * 75; }
1180 v = (val & 0x200) ? -1.0 : 1.0;
1185 fcb->
x[fcb->
n + 1] = idx;
1186 fcb->
y[fcb->
n + 1] = (val & 1) ? -v : v;
1204 static int pRNG(
int frame_cntr,
int block_num,
int block_size)
1216 static const unsigned int div_tbl[9][2] = {
1217 { 8332, 3 * 715827883
U },
1218 { 4545, 0 * 390451573
U },
1219 { 3124, 11 * 268435456
U },
1220 { 2380, 15 * 204522253
U },
1221 { 1922, 23 * 165191050
U },
1222 { 1612, 23 * 138547333
U },
1223 { 1388, 27 * 119304648
U },
1224 { 1219, 16 * 104755300
U },
1225 { 1086, 39 * 93368855
U }
1227 unsigned int z, y, x =
MUL16(block_num, 1877) + frame_cntr;
1228 if (x >= 0xFFFF) x -= 0xFFFF;
1230 y = x - 9 *
MULH(477218589, x);
1231 z = (uint16_t) (x * div_tbl[y][0] +
UMULH(x, div_tbl[y][1]));
1233 return z % (1000 - block_size);
1241 int block_idx,
int size,
1263 for (n = 0; n <
size; n++)
1272 int block_idx,
int size,
1273 int block_pitch_sh2,
1277 static const float gain_coeff[6] = {
1278 0.8169, -0.06545, 0.1726, 0.0185, -0.0359, 0.0458
1281 int n, idx, gain_weight;
1285 memset(pulses, 0,
sizeof(*pulses) * size);
1303 for (n = 0; n < 5; n++) {
1309 fcb.
x[fcb.
n] = n + 5 * pos1;
1310 fcb.
y[fcb.
n++] = sign;
1311 if (n < frame_desc->dbl_pulses) {
1313 fcb.
x[fcb.
n] = n + 5 * pos2;
1314 fcb.
y[fcb.
n++] = (pos1 < pos2) ? -sign : sign;
1333 for (n = 0; n < gain_weight; n++)
1339 for (n = 0; n <
size; n +=
len) {
1341 int abs_idx = block_idx * size + n;
1344 int pitch = (pitch_sh16 + 0x6FFF) >> 16;
1345 int idx_sh16 = ((pitch << 16) - pitch_sh16) * 8 + 0x58000;
1346 idx = idx_sh16 >> 16;
1349 next_idx_sh16 = (idx_sh16) &~ 0xFFFF;
1351 next_idx_sh16 = (idx_sh16 + 0x10000) &~ 0xFFFF;
1362 int block_pitch = block_pitch_sh2 >> 2;
1363 idx = block_pitch_sh2 & 3;
1370 sizeof(
float) * size);
1375 acb_gain, fcb_gain, size);
1395 int block_idx,
int size,
1396 int block_pitch_sh2,
1397 const double *lsps,
const double *prev_lsps,
1399 float *excitation,
float *synth)
1410 frame_desc, excitation);
1413 fac = (block_idx + 0.5) / frame_desc->
n_blocks;
1414 for (n = 0; n < s->
lsps; n++)
1415 i_lsps[n] = cos(prev_lsps[n] + fac * (lsps[n] - prev_lsps[n]));
1439 const double *lsps,
const double *prev_lsps,
1440 float *excitation,
float *synth)
1443 int n, n_blocks_x2, log_n_blocks_x2, cur_pitch_val;
1451 "Invalid frame type VLC code, skipping\n");
1474 int fac = n * 2 + 1;
1476 pitch[n] = (
MUL16(fac, cur_pitch_val) +
1518 last_block_pitch = av_clip(block_pitch,
1524 if (block_pitch < t1) {
1528 if (block_pitch <
t2) {
1533 if (block_pitch <
t3) {
1540 pitch[n] = bl_pitch_sh2 >> 2;
1545 bl_pitch_sh2 = pitch[n] << 2;
1554 synth_block(s, gb, n, block_nsamples, bl_pitch_sh2,
1556 &excitation[n * block_nsamples],
1557 &synth[n * block_nsamples]);
1566 for (n = 0; n < s->
lsps; n++)
1567 i_lsps[n] = cos(0.5 * (prev_lsps[n] + lsps[n]));
1573 for (n = 0; n < s->
lsps; n++)
1574 i_lsps[n] = cos(lsps[n]);
1576 postfilter(s, &synth[80], &samples[80], 80, lpcs,
1580 memcpy(samples, synth, 160 *
sizeof(synth[0]));
1620 lsps[0] =
FFMAX(lsps[0], 0.0015 *
M_PI);
1621 for (n = 1; n < num; n++)
1622 lsps[n] =
FFMAX(lsps[n], lsps[n - 1] + 0.0125 *
M_PI);
1623 lsps[num - 1] =
FFMIN(lsps[num - 1], 0.9985 *
M_PI);
1627 for (n = 1; n < num; n++) {
1628 if (lsps[n] < lsps[n - 1]) {
1629 for (m = 1; m < num; m++) {
1630 double tmp = lsps[
m];
1631 for (l = m - 1; l >= 0; l--) {
1632 if (lsps[l] <= tmp)
break;
1633 lsps[l + 1] = lsps[l];
1655 int n, need_bits, bd_idx;
1677 int aw_idx_is_ext = 0;
1707 need_bits = 2 * !aw_idx_is_ext;
1740 int n, res, n_samples = 480;
1749 s->
lsps *
sizeof(*synth));
1775 if ((n_samples =
get_bits(gb, 12)) > 480) {
1777 "Superframe encodes >480 samples (%d), not allowed\n",
1786 for (n = 0; n < s->
lsps; n++)
1787 prev_lsps[n] = s->
prev_lsps[n] - mean_lsf[n];
1794 for (n = 0; n < s->
lsps; n++) {
1795 lsps[0][n] = mean_lsf[n] + (a1[n] - a2[n * 2]);
1796 lsps[1][n] = mean_lsf[n] + (a1[s->
lsps + n] - a2[n * 2 + 1]);
1797 lsps[2][n] += mean_lsf[n];
1799 for (n = 0; n < 3; n++)
1813 for (n = 0; n < 3; n++) {
1817 if (s->
lsps == 10) {
1822 for (m = 0; m < s->
lsps; m++)
1823 lsps[n][m] += mean_lsf[m];
1829 lsps[n], n == 0 ? s->
prev_lsps : lsps[n - 1],
1831 &synth[s->
lsps + n * MAX_FRAMESIZE]))) {
1851 s->
lsps *
sizeof(*synth));
1882 }
while (res == 0x3F);
1907 int rmn_bytes, rmn_bits;
1910 if (rmn_bits < nbits)
1914 rmn_bits &= 7; rmn_bytes >>= 3;
1915 if ((rmn_bits =
FFMIN(rmn_bits, nbits)) > 0)
1918 FFMIN(nbits - rmn_bits, rmn_bytes << 3));
1933 int *got_frame_ptr,
AVPacket *avpkt)
1988 }
else if (*got_frame_ptr) {
2031 for (n = 0; n < s->
lsps; n++)