41 #define AMR_USE_16BIT_TABLES
68 float prediction_error[4];
82 float hpf_31_mem[2], hpf_400_mem[2];
120 for (i = 0; i < 4; i++)
160 for (i = 0; i < 9; i++)
161 isf_q[i] =
dico1_isf[ind[0]][i] * (1.0f / (1 << 15));
163 for (i = 0; i < 7; i++)
164 isf_q[i + 9] =
dico2_isf[ind[1]][i] * (1.0f / (1 << 15));
166 for (i = 0; i < 5; i++)
169 for (i = 0; i < 4; i++)
172 for (i = 0; i < 7; i++)
187 for (i = 0; i < 9; i++)
188 isf_q[i] =
dico1_isf[ind[0]][i] * (1.0f / (1 << 15));
190 for (i = 0; i < 7; i++)
191 isf_q[i + 9] =
dico2_isf[ind[1]][i] * (1.0f / (1 << 15));
193 for (i = 0; i < 3; i++)
194 isf_q[i] +=
dico21_isf[ind[2]][i] * (1.0f / (1 << 15));
196 for (i = 0; i < 3; i++)
197 isf_q[i + 3] +=
dico22_isf[ind[3]][i] * (1.0f / (1 << 15));
199 for (i = 0; i < 3; i++)
200 isf_q[i + 6] +=
dico23_isf[ind[4]][i] * (1.0f / (1 << 15));
202 for (i = 0; i < 3; i++)
203 isf_q[i + 9] +=
dico24_isf[ind[5]][i] * (1.0f / (1 << 15));
205 for (i = 0; i < 4; i++)
206 isf_q[i + 12] +=
dico25_isf[ind[6]][i] * (1.0f / (1 << 15));
224 isf_q[i] +=
isf_mean[i] * (1.0f / (1 << 15));
241 for (k = 0; k < 3; k++) {
244 isp_q[k][i] = (1.0 - c) * isp4_past[i] + c * isp_q[3][i];
260 uint8_t *base_lag_int,
int subframe)
262 if (subframe == 0 || subframe == 2) {
263 if (pitch_index < 376) {
264 *lag_int = (pitch_index + 137) >> 2;
265 *lag_frac = pitch_index - (*lag_int << 2) + 136;
266 }
else if (pitch_index < 440) {
267 *lag_int = (pitch_index + 257 - 376) >> 1;
268 *lag_frac = (pitch_index - (*lag_int << 1) + 256 - 376) << 1;
271 *lag_int = pitch_index - 280;
275 *base_lag_int = av_clip(*lag_int - 8 - (*lag_frac < 0),
281 *lag_int = (pitch_index + 1) >> 2;
282 *lag_frac = pitch_index - (*lag_int << 2);
283 *lag_int += *base_lag_int;
295 if (subframe == 0 || (subframe == 2 && mode !=
MODE_6k60)) {
296 if (pitch_index < 116) {
297 *lag_int = (pitch_index + 69) >> 1;
298 *lag_frac = (pitch_index - (*lag_int << 1) + 68) << 1;
300 *lag_int = pitch_index - 24;
304 *base_lag_int = av_clip(*lag_int - 8 - (*lag_frac < 0),
307 *lag_int = (pitch_index + 1) >> 1;
308 *lag_frac = (pitch_index - (*lag_int << 1)) << 1;
309 *lag_int += *base_lag_int;
325 int pitch_lag_int, pitch_lag_frac;
338 pitch_lag_int += pitch_lag_frac > 0;
343 exc + 1 - pitch_lag_int,
345 pitch_lag_frac + (pitch_lag_frac > 0 ? 0 : 4),
350 if (amr_subframe->
ltp) {
354 ctx->
pitch_vector[i] = 0.18 * exc[i - 1] + 0.64 * exc[i] +
356 memcpy(exc, ctx->
pitch_vector, AMRWB_SFR_SIZE *
sizeof(
float));
361 #define BIT_STR(x,lsb,len) (((x) >> (lsb)) & ((1 << (len)) - 1))
364 #define BIT_POS(x, p) (((x) >> (p)) & 1)
383 out[0] =
BIT_POS(code, m) ? -pos : pos;
391 out[0] =
BIT_POS(code, 2*m) ? -pos0 : pos0;
392 out[1] =
BIT_POS(code, 2*m) ? -pos1 : pos1;
393 out[1] = pos0 > pos1 ? -out[1] : out[1];
398 int half_2p =
BIT_POS(code, 2*m - 1) << (m - 1);
401 m - 1, off + half_2p);
407 int half_4p, subhalf_2p;
408 int b_offset = 1 << (m - 1);
410 switch (
BIT_STR(code, 4*m - 2, 2)) {
412 half_4p =
BIT_POS(code, 4*m - 3) << (m - 1);
413 subhalf_2p =
BIT_POS(code, 2*m - 3) << (m - 2);
416 m - 2, off + half_4p + subhalf_2p);
418 m - 1, off + half_4p);
424 m - 1, off + b_offset);
430 m - 1, off + b_offset);
436 m - 1, off + b_offset);
443 int half_3p =
BIT_POS(code, 5*m - 1) << (m - 1);
446 m - 1, off + half_3p);
453 int b_offset = 1 << (m - 1);
455 int half_more =
BIT_POS(code, 6*m - 5) << (m - 1);
456 int half_other = b_offset - half_more;
458 switch (
BIT_STR(code, 6*m - 4, 2)) {
461 m - 1, off + half_more);
463 m - 1, off + half_more);
467 m - 1, off + half_other);
469 m - 1, off + half_more);
473 m - 1, off + half_other);
475 m - 1, off + half_more);
481 m - 1, off + b_offset);
496 const uint16_t *pulse_lo,
const enum Mode mode)
501 int spacing = (mode ==
MODE_6k60) ? 2 : 4;
506 for (i = 0; i < 2; i++)
510 for (i = 0; i < 4; i++)
514 for (i = 0; i < 4; i++)
518 for (i = 0; i < 2; i++)
520 for (i = 2; i < 4; i++)
524 for (i = 0; i < 4; i++)
528 for (i = 0; i < 4; i++)
530 ((int) pulse_hi[i] << 14), 4, 1);
533 for (i = 0; i < 2; i++)
535 ((int) pulse_hi[i] << 10), 4, 1);
536 for (i = 2; i < 4; i++)
538 ((int) pulse_hi[i] << 14), 4, 1);
542 for (i = 0; i < 4; i++)
544 ((int) pulse_hi[i] << 11), 4, 1);
550 for (i = 0; i < 4; i++)
552 int pos = (
FFABS(sig_pos[i][j]) - 1) * spacing + i;
554 fixed_vector[pos] += sig_pos[i][j] < 0 ? -1.0 : 1.0;
567 float *fixed_gain_factor,
float *pitch_gain)
572 *pitch_gain = gains[0] * (1.0f / (1 << 14));
573 *fixed_gain_factor = gains[1] * (1.0f / (1 << 11));
590 fixed_vector[i] -= fixed_vector[i - 1] * ctx->
tilt_coef;
594 fixed_vector[i] += fixed_vector[i - ctx->
pitch_lag_int] * 0.85;
607 float *f_vector,
float f_gain,
610 double p_ener = (double) ctx->
dot_productf(p_vector, p_vector,
613 double f_ener = (double) ctx->
dot_productf(f_vector, f_vector,
617 return (p_ener - f_ener) / (p_ener + f_ener);
631 float *fixed_vector,
float *buf)
647 if (ir_filter_nr < 2)
652 for (i = 0; i < 6; i++)
668 if (ir_filter_nr < 2) {
700 acc += (isf[i] - isf_past[i]) * (isf[i] - isf_past[i]);
704 return FFMAX(0.0, 1.25 - acc * 0.8 * 512);
719 float voice_fac,
float stab_fac)
721 float sm_fac = 0.5 * (1 - voice_fac) * stab_fac;
727 if (fixed_gain < *prev_tr_gain) {
728 g0 =
FFMIN(*prev_tr_gain, fixed_gain + fixed_gain *
729 (6226 * (1.0f / (1 << 15))));
731 g0 =
FFMAX(*prev_tr_gain, fixed_gain *
732 (27536 * (1.0f / (1 << 15))));
736 return sm_fac * g0 + (1 - sm_fac) * fixed_gain;
748 float cpe = 0.125 * (1 + voice_fac);
749 float last = fixed_vector[0];
751 fixed_vector[0] -= cpe * fixed_vector[1];
754 float cur = fixed_vector[i];
756 fixed_vector[i] -= cpe * (last + fixed_vector[i + 1]);
760 fixed_vector[AMRWB_SFR_SIZE - 1] -= cpe * last;
774 float fixed_gain,
const float *fixed_vector,
794 energy, AMRWB_SFR_SIZE);
814 out[0] = in[0] + m * mem[0];
817 out[i] = in[i] + out[i - 1] * m;
819 mem[0] = out[AMRWB_SFR_SIZE - 1];
835 int int_part = 0, frac_part;
838 for (j = 0; j < o_size / 5; j++) {
839 out[i] = in[int_part];
843 for (k = 1; k < 5; k++) {
876 return av_clipf((1.0 -
FFMAX(0.0, tilt)) * (1.25 - 0.25 * wsp), 0.1, 1.0);
889 const float *synth_exc,
float hb_gain)
900 energy * hb_gain * hb_gain,
912 for (i = 7; i <
LP_ORDER - 2; i++) {
913 float prod = (diff_isf[i] - mean) * (diff_isf[i - lag] - mean);
928 float diff_isf[
LP_ORDER - 2], diff_mean;
931 int i, j, i_max_corr;
933 isf[LP_ORDER_16k - 1] = isf[
LP_ORDER - 1];
937 diff_isf[i] = isf[i + 1] - isf[i];
940 for (i = 2; i < LP_ORDER - 2; i++)
941 diff_mean += diff_isf[i] * (1.0f / (LP_ORDER - 4));
945 for (i = 0; i < 3; i++) {
948 if (corr_lag[i] > corr_lag[i_max_corr])
953 for (i = LP_ORDER - 1; i < LP_ORDER_16k - 1; i++)
954 isf[i] = isf[i - 1] + isf[i - 1 - i_max_corr]
955 - isf[i - 2 - i_max_corr];
958 est = 7965 + (isf[2] - isf[3] - isf[4]) / 6.0;
959 scale = 0.5 * (
FFMIN(est, 7600) - isf[LP_ORDER - 2]) /
960 (isf[LP_ORDER_16k - 2] - isf[LP_ORDER - 2]);
962 for (i = LP_ORDER - 1, j = 0; i < LP_ORDER_16k - 1; i++, j++)
963 diff_isf[j] = scale * (isf[i] - isf[i - 1]);
966 for (i = 1; i < LP_ORDER_16k -
LP_ORDER; i++)
967 if (diff_isf[i] + diff_isf[i - 1] < 5.0) {
968 if (diff_isf[i] > diff_isf[i - 1]) {
969 diff_isf[i - 1] = 5.0 - diff_isf[i];
971 diff_isf[i] = 5.0 - diff_isf[i - 1];
974 for (i = LP_ORDER - 1, j = 0; i < LP_ORDER_16k - 1; i++, j++)
975 isf[i] = isf[i - 1] + diff_isf[j] * (1.0f / (1 << 15));
978 for (i = 0; i < LP_ORDER_16k - 1; i++)
996 for (i = 0; i <
size; i++) {
997 out[i] = lpc[i] * fac;
1014 const float *exc,
const float *isf,
const float *isf_past)
1053 #ifndef hb_fir_filter
1060 memcpy(data, mem, HB_FIR_SIZE *
sizeof(
float));
1066 out[i] += data[i + j] * fir_coef[j];
1069 memcpy(mem, data + AMRWB_SFR_SIZE_16k, HB_FIR_SIZE *
sizeof(
float));
1093 int *got_frame_ptr,
AVPacket *avpkt)
1099 int buf_size = avpkt->
size;
1100 int expected_fr_size, header_size;
1103 float fixed_gain_factor;
1104 float *synth_fixed_vector;
1105 float synth_fixed_gain;
1106 float voice_fac, stab_fac;
1119 buf_out = (
float *)frame->
data[0];
1129 if (buf_size < expected_fr_size) {
1131 "Frame too small (%d bytes). Truncated file?\n", buf_size);
1169 for (sub = 0; sub < 4; sub++)
1172 for (sub = 0; sub < 4; sub++) {
1200 ctx->
tilt_coef = voice_fac * 0.25 + 0.25;
1211 voice_fac, stab_fac);
1255 sub_buf[i] = (sub_buf[i] + hb_samples[i]) * (1.0f / (1 << 15));
1267 return expected_fr_size;