00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <inttypes.h>
00022 #include <limits.h>
00023
00024 #include "avcodec.h"
00025 #include "g729.h"
00026 #include "acelp_pitch_delay.h"
00027 #include "g729postfilter.h"
00028 #include "celp_math.h"
00029 #include "acelp_filters.h"
00030 #include "acelp_vectors.h"
00031 #include "celp_filters.h"
00032
00033 #define FRAC_BITS 15
00034 #include "mathops.h"
00035
00040 static const int16_t ff_g729_interp_filt_short[(ANALYZED_FRAC_DELAYS+1)*SHORT_INT_FILT_LEN] = {
00041 0, 31650, 28469, 23705, 18050, 12266, 7041, 2873,
00042 0, -1597, -2147, -1992, -1492, -933, -484, -188,
00043 };
00044
00049 static const int16_t ff_g729_interp_filt_long[(ANALYZED_FRAC_DELAYS+1)*LONG_INT_FILT_LEN] = {
00050 0, 31915, 29436, 25569, 20676, 15206, 9639, 4439,
00051 0, -3390, -5579, -6549, -6414, -5392, -3773, -1874,
00052 0, 1595, 2727, 3303, 3319, 2850, 2030, 1023,
00053 0, -887, -1527, -1860, -1876, -1614, -1150, -579,
00054 0, 501, 859, 1041, 1044, 892, 631, 315,
00055 0, -266, -453, -543, -538, -455, -317, -156,
00056 0, 130, 218, 258, 253, 212, 147, 72,
00057 0, -59, -101, -122, -123, -106, -77, -40,
00058 };
00059
00063 static const int16_t formant_pp_factor_num_pow[10]= {
00064
00065 18022, 9912, 5451, 2998, 1649, 907, 499, 274, 151, 83
00066 };
00067
00071 static const int16_t formant_pp_factor_den_pow[10] = {
00072
00073 22938, 16057, 11240, 7868, 5508, 3856, 2699, 1889, 1322, 925
00074 };
00075
00086 static void residual_filter(int16_t* out, const int16_t* filter_coeffs, const int16_t* in,
00087 int subframe_size)
00088 {
00089 int i, n;
00090
00091 for (n = subframe_size - 1; n >= 0; n--) {
00092 int sum = 0x800;
00093 for (i = 0; i < 10; i++)
00094 sum += filter_coeffs[i] * in[n - i - 1];
00095
00096 out[n] = in[n] + (sum >> 12);
00097 }
00098 }
00099
00110 static int16_t long_term_filter(DSPContext *dsp, int pitch_delay_int,
00111 const int16_t* residual, int16_t *residual_filt,
00112 int subframe_size)
00113 {
00114 int i, k, tmp, tmp2;
00115 int sum;
00116 int L_temp0;
00117 int L_temp1;
00118 int64_t L64_temp0;
00119 int64_t L64_temp1;
00120 int16_t shift;
00121 int corr_int_num, corr_int_den;
00122
00123 int ener;
00124 int16_t sh_ener;
00125
00126 int16_t gain_num,gain_den;
00127 int16_t sh_gain_num, sh_gain_den;
00128 int gain_num_square;
00129
00130 int16_t gain_long_num,gain_long_den;
00131 int16_t sh_gain_long_num, sh_gain_long_den;
00132
00133 int16_t best_delay_int, best_delay_frac;
00134
00135 int16_t delayed_signal_offset;
00136 int lt_filt_factor_a, lt_filt_factor_b;
00137
00138 int16_t * selected_signal;
00139 const int16_t * selected_signal_const;
00140
00141 int16_t sig_scaled[SUBFRAME_SIZE + RES_PREV_DATA_SIZE];
00142 int16_t delayed_signal[ANALYZED_FRAC_DELAYS][SUBFRAME_SIZE+1];
00143 int corr_den[ANALYZED_FRAC_DELAYS][2];
00144
00145 tmp = 0;
00146 for(i=0; i<subframe_size + RES_PREV_DATA_SIZE; i++)
00147 tmp |= FFABS(residual[i]);
00148
00149 if(!tmp)
00150 shift = 3;
00151 else
00152 shift = av_log2(tmp) - 11;
00153
00154 if (shift > 0)
00155 for (i = 0; i < subframe_size + RES_PREV_DATA_SIZE; i++)
00156 sig_scaled[i] = residual[i] >> shift;
00157 else
00158 for (i = 0; i < subframe_size + RES_PREV_DATA_SIZE; i++)
00159 sig_scaled[i] = residual[i] << -shift;
00160
00161
00162 gain_num = 0;
00163
00164 ener = dsp->scalarproduct_int16(sig_scaled + RES_PREV_DATA_SIZE,
00165 sig_scaled + RES_PREV_DATA_SIZE,
00166 subframe_size);
00167 if (ener) {
00168 sh_ener = FFMAX(av_log2(ener) - 14, 0);
00169 ener >>= sh_ener;
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 corr_int_num = 0;
00191 best_delay_int = pitch_delay_int - 1;
00192 for (i = pitch_delay_int - 1; i <= pitch_delay_int + 1; i++) {
00193 sum = dsp->scalarproduct_int16(sig_scaled + RES_PREV_DATA_SIZE,
00194 sig_scaled + RES_PREV_DATA_SIZE - i,
00195 subframe_size);
00196 if (sum > corr_int_num) {
00197 corr_int_num = sum;
00198 best_delay_int = i;
00199 }
00200 }
00201 if (corr_int_num) {
00202
00203 corr_int_den = dsp->scalarproduct_int16(sig_scaled - best_delay_int + RES_PREV_DATA_SIZE,
00204 sig_scaled - best_delay_int + RES_PREV_DATA_SIZE,
00205 subframe_size);
00206
00207
00208
00209
00210
00211
00212 for (k = 0; k < ANALYZED_FRAC_DELAYS; k++) {
00213 ff_acelp_interpolate(&delayed_signal[k][0],
00214 &sig_scaled[RES_PREV_DATA_SIZE - best_delay_int],
00215 ff_g729_interp_filt_short,
00216 ANALYZED_FRAC_DELAYS+1,
00217 8 - k - 1,
00218 SHORT_INT_FILT_LEN,
00219 subframe_size + 1);
00220 }
00221
00222
00223
00224
00225
00226
00227
00228 tmp = corr_int_den;
00229 for (k = 0; k < ANALYZED_FRAC_DELAYS; k++) {
00230 sum = dsp->scalarproduct_int16(&delayed_signal[k][1],
00231 &delayed_signal[k][1],
00232 subframe_size - 1);
00233 corr_den[k][0] = sum + delayed_signal[k][0 ] * delayed_signal[k][0 ];
00234 corr_den[k][1] = sum + delayed_signal[k][subframe_size] * delayed_signal[k][subframe_size];
00235
00236 tmp = FFMAX3(tmp, corr_den[k][0], corr_den[k][1]);
00237 }
00238
00239 sh_gain_den = av_log2(tmp) - 14;
00240 if (sh_gain_den >= 0) {
00241
00242 sh_gain_num = FFMAX(sh_gain_den, sh_ener);
00243
00244
00245
00246
00247 delayed_signal_offset = 1;
00248 best_delay_frac = 0;
00249 gain_den = corr_int_den >> sh_gain_den;
00250 gain_num = corr_int_num >> sh_gain_num;
00251 gain_num_square = gain_num * gain_num;
00252 for (k = 0; k < ANALYZED_FRAC_DELAYS; k++) {
00253 for (i = 0; i < 2; i++) {
00254 int16_t gain_num_short, gain_den_short;
00255 int gain_num_short_square;
00256
00257
00258 sum = dsp->scalarproduct_int16(&delayed_signal[k][i],
00259 sig_scaled + RES_PREV_DATA_SIZE,
00260 subframe_size);
00261 gain_num_short = FFMAX(sum >> sh_gain_num, 0);
00262
00263
00264
00265
00266
00267
00268 gain_num_short_square = gain_num_short * gain_num_short;
00269 gain_den_short = corr_den[k][i] >> sh_gain_den;
00270
00271 tmp = MULL(gain_num_short_square, gain_den, FRAC_BITS);
00272 tmp2 = MULL(gain_num_square, gain_den_short, FRAC_BITS);
00273
00274
00275 if (tmp > tmp2) {
00276 gain_num = gain_num_short;
00277 gain_den = gain_den_short;
00278 gain_num_square = gain_num_short_square;
00279 delayed_signal_offset = i;
00280 best_delay_frac = k + 1;
00281 }
00282 }
00283 }
00284
00285
00286
00287
00288
00289
00290 L64_temp0 = (int64_t)gain_num_square << ((sh_gain_num << 1) + 1);
00291 L64_temp1 = ((int64_t)gain_den * ener) << (sh_gain_den + sh_ener);
00292 if (L64_temp0 < L64_temp1)
00293 gain_num = 0;
00294 }
00295 }
00296 }
00297
00298
00299 if (!gain_num) {
00300 memcpy(residual_filt, residual + RES_PREV_DATA_SIZE, subframe_size * sizeof(int16_t));
00301
00302
00303 return 0;
00304 }
00305 if (best_delay_frac) {
00306
00307 ff_acelp_interpolate(residual_filt,
00308 &sig_scaled[RES_PREV_DATA_SIZE - best_delay_int + delayed_signal_offset],
00309 ff_g729_interp_filt_long,
00310 ANALYZED_FRAC_DELAYS + 1,
00311 8 - best_delay_frac,
00312 LONG_INT_FILT_LEN,
00313 subframe_size + 1);
00314
00315 sum = dsp->scalarproduct_int16(residual_filt,
00316 sig_scaled + RES_PREV_DATA_SIZE,
00317 subframe_size);
00318
00319 if (sum < 0) {
00320 gain_long_num = 0;
00321 sh_gain_long_num = 0;
00322 } else {
00323 tmp = FFMAX(av_log2(sum) - 14, 0);
00324 sum >>= tmp;
00325 gain_long_num = sum;
00326 sh_gain_long_num = tmp;
00327 }
00328
00329
00330 sum = dsp->scalarproduct_int16(residual_filt, residual_filt, subframe_size);
00331
00332 tmp = FFMAX(av_log2(sum) - 14, 0);
00333 sum >>= tmp;
00334 gain_long_den = sum;
00335 sh_gain_long_den = tmp;
00336
00337
00338
00339
00340 L_temp0 = gain_num * gain_num;
00341 L_temp0 = MULL(L_temp0, gain_long_den, FRAC_BITS);
00342
00343 L_temp1 = gain_long_num * gain_long_num;
00344 L_temp1 = MULL(L_temp1, gain_den, FRAC_BITS);
00345
00346 tmp = ((sh_gain_long_num - sh_gain_num) << 1) - (sh_gain_long_den - sh_gain_den);
00347 if (tmp > 0)
00348 L_temp0 >>= tmp;
00349 else
00350 L_temp1 >>= -tmp;
00351
00352
00353 if (L_temp1 > L_temp0) {
00354
00355 selected_signal = residual_filt;
00356 gain_num = gain_long_num;
00357 gain_den = gain_long_den;
00358 sh_gain_num = sh_gain_long_num;
00359 sh_gain_den = sh_gain_long_den;
00360 } else
00361
00362 selected_signal = &delayed_signal[best_delay_frac-1][delayed_signal_offset];
00363
00364
00365 if (shift > 0)
00366 for (i = 0; i < subframe_size; i++)
00367 selected_signal[i] <<= shift;
00368 else
00369 for (i = 0; i < subframe_size; i++)
00370 selected_signal[i] >>= -shift;
00371
00372
00373 selected_signal_const = selected_signal;
00374 }
00375 else
00376 selected_signal_const = residual + RES_PREV_DATA_SIZE - (best_delay_int + 1 - delayed_signal_offset);
00377 #ifdef G729_BITEXACT
00378 tmp = sh_gain_num - sh_gain_den;
00379 if (tmp > 0)
00380 gain_den >>= tmp;
00381 else
00382 gain_num >>= -tmp;
00383
00384 if (gain_num > gain_den)
00385 lt_filt_factor_a = MIN_LT_FILT_FACTOR_A;
00386 else {
00387 gain_num >>= 2;
00388 gain_den >>= 1;
00389 lt_filt_factor_a = (gain_den << 15) / (gain_den + gain_num);
00390 }
00391 #else
00392 L64_temp0 = ((int64_t)gain_num) << (sh_gain_num - 1);
00393 L64_temp1 = ((int64_t)gain_den) << sh_gain_den;
00394 lt_filt_factor_a = FFMAX((L64_temp1 << 15) / (L64_temp1 + L64_temp0), MIN_LT_FILT_FACTOR_A);
00395 #endif
00396
00397
00398 lt_filt_factor_b = 32767 - lt_filt_factor_a + 1;
00399
00400 ff_acelp_weighted_vector_sum(residual_filt, residual + RES_PREV_DATA_SIZE,
00401 selected_signal_const,
00402 lt_filt_factor_a, lt_filt_factor_b,
00403 1<<14, 15, subframe_size);
00404
00405
00406 return 1;
00407 }
00408
00424 static int16_t get_tilt_comp(DSPContext *dsp, int16_t *lp_gn,
00425 const int16_t *lp_gd, int16_t* speech,
00426 int subframe_size)
00427 {
00428 int rh1,rh0;
00429 int temp;
00430 int i;
00431 int gain_term;
00432
00433 lp_gn[10] = 4096;
00434
00435
00436 ff_celp_lp_synthesis_filter(lp_gn + 11, lp_gd + 1, lp_gn + 11, 22, 10, 0, 0, 0x800);
00437
00438
00439
00440 rh0 = dsp->scalarproduct_int16(lp_gn + 10, lp_gn + 10, 20);
00441 rh1 = dsp->scalarproduct_int16(lp_gn + 10, lp_gn + 11, 20);
00442
00443
00444 temp = av_log2(rh0) - 14;
00445 if (temp > 0) {
00446 rh0 >>= temp;
00447 rh1 >>= temp;
00448 }
00449
00450 if (FFABS(rh1) > rh0 || !rh0)
00451 return 0;
00452
00453 gain_term = 0;
00454 for (i = 0; i < 20; i++)
00455 gain_term += FFABS(lp_gn[i + 10]);
00456 gain_term >>= 2;
00457
00458 if (gain_term > 0x400) {
00459 temp = 0x2000000 / gain_term;
00460 for (i = 0; i < subframe_size; i++)
00461 speech[i] = (speech[i] * temp + 0x4000) >> 15;
00462 }
00463
00464 return -(rh1 << 15) / rh0;
00465 }
00466
00476 static int16_t apply_tilt_comp(int16_t* out, int16_t* res_pst, int refl_coeff,
00477 int subframe_size, int16_t ht_prev_data)
00478 {
00479 int tmp, tmp2;
00480 int i;
00481 int gt, ga;
00482 int fact, sh_fact;
00483
00484 if (refl_coeff > 0) {
00485 gt = (refl_coeff * G729_TILT_FACTOR_PLUS + 0x4000) >> 15;
00486 fact = 0x4000;
00487 sh_fact = 15;
00488 } else {
00489 gt = (refl_coeff * G729_TILT_FACTOR_MINUS + 0x4000) >> 15;
00490 fact = 0x800;
00491 sh_fact = 12;
00492 }
00493 ga = (fact << 15) / av_clip_int16(32768 - FFABS(gt));
00494 gt >>= 1;
00495
00496
00497 tmp = res_pst[subframe_size - 1];
00498
00499 for (i = subframe_size - 1; i >= 1; i--) {
00500 tmp2 = (res_pst[i] << 15) + ((gt * res_pst[i-1]) << 1);
00501 tmp2 = (tmp2 + 0x4000) >> 15;
00502
00503 tmp2 = (tmp2 * ga * 2 + fact) >> sh_fact;
00504 out[i] = tmp2;
00505 }
00506 tmp2 = (res_pst[0] << 15) + ((gt * ht_prev_data) << 1);
00507 tmp2 = (tmp2 + 0x4000) >> 15;
00508 tmp2 = (tmp2 * ga * 2 + fact) >> sh_fact;
00509 out[0] = tmp2;
00510
00511 return tmp;
00512 }
00513
00514 void ff_g729_postfilter(DSPContext *dsp, int16_t* ht_prev_data, int* voicing,
00515 const int16_t *lp_filter_coeffs, int pitch_delay_int,
00516 int16_t* residual, int16_t* res_filter_data,
00517 int16_t* pos_filter_data, int16_t *speech, int subframe_size)
00518 {
00519 int16_t residual_filt_buf[SUBFRAME_SIZE+11];
00520 int16_t lp_gn[33];
00521 int16_t lp_gd[11];
00522 int tilt_comp_coeff;
00523 int i;
00524
00525
00526 memset(lp_gn, 0, 33 * sizeof(int16_t));
00527
00528
00529 for (i = 0; i < 10; i++)
00530 lp_gn[i + 11] = (lp_filter_coeffs[i + 1] * formant_pp_factor_num_pow[i] + 0x4000) >> 15;
00531
00532
00533 for (i = 0; i < 10; i++)
00534 lp_gd[i + 1] = (lp_filter_coeffs[i + 1] * formant_pp_factor_den_pow[i] + 0x4000) >> 15;
00535
00536
00537 memcpy(speech - 10, res_filter_data, 10 * sizeof(int16_t));
00538 residual_filter(residual + RES_PREV_DATA_SIZE, lp_gn + 11, speech, subframe_size);
00539
00540 memcpy(res_filter_data, speech + subframe_size - 10, 10 * sizeof(int16_t));
00541
00542
00543
00544 *voicing = FFMAX(*voicing, long_term_filter(dsp, pitch_delay_int,
00545 residual, residual_filt_buf + 10,
00546 subframe_size));
00547
00548
00549 memmove(residual, residual + subframe_size, RES_PREV_DATA_SIZE * sizeof(int16_t));
00550
00551
00552 tilt_comp_coeff = get_tilt_comp(dsp, lp_gn, lp_gd, residual_filt_buf + 10, subframe_size);
00553
00554
00555 ff_celp_lp_synthesis_filter(pos_filter_data + 10, lp_gd + 1,
00556 residual_filt_buf + 10,
00557 subframe_size, 10, 0, 0, 0x800);
00558 memcpy(pos_filter_data, pos_filter_data + subframe_size, 10 * sizeof(int16_t));
00559
00560 *ht_prev_data = apply_tilt_comp(speech, pos_filter_data + 10, tilt_comp_coeff,
00561 subframe_size, *ht_prev_data);
00562 }
00563
00574 int16_t ff_g729_adaptive_gain_control(int gain_before, int gain_after, int16_t *speech,
00575 int subframe_size, int16_t gain_prev)
00576 {
00577 int gain;
00578 int n;
00579 int exp_before, exp_after;
00580
00581 if(!gain_after && gain_before)
00582 return 0;
00583
00584 if (gain_before) {
00585
00586 exp_before = 14 - av_log2(gain_before);
00587 gain_before = bidir_sal(gain_before, exp_before);
00588
00589 exp_after = 14 - av_log2(gain_after);
00590 gain_after = bidir_sal(gain_after, exp_after);
00591
00592 if (gain_before < gain_after) {
00593 gain = (gain_before << 15) / gain_after;
00594 gain = bidir_sal(gain, exp_after - exp_before - 1);
00595 } else {
00596 gain = ((gain_before - gain_after) << 14) / gain_after + 0x4000;
00597 gain = bidir_sal(gain, exp_after - exp_before);
00598 }
00599 gain = (gain * G729_AGC_FAC1 + 0x4000) >> 15;
00600 } else
00601 gain = 0;
00602
00603 for (n = 0; n < subframe_size; n++) {
00604
00605 gain_prev = (G729_AGC_FACTOR * gain_prev + 0x4000) >> 15;
00606 gain_prev = av_clip_int16(gain + gain_prev);
00607 speech[n] = av_clip_int16((speech[n] * gain_prev + 0x2000) >> 14);
00608 }
00609 return gain_prev;
00610 }