You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

615 lines
23KB

  1. /*
  2. * G.729, G729 Annex D postfilter
  3. * Copyright (c) 2008 Vladimir Voroshilov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <inttypes.h>
  22. #include <limits.h>
  23. #include "avcodec.h"
  24. #include "g729.h"
  25. #include "acelp_pitch_delay.h"
  26. #include "g729postfilter.h"
  27. #include "celp_math.h"
  28. #include "acelp_filters.h"
  29. #include "acelp_vectors.h"
  30. #include "celp_filters.h"
  31. #define FRAC_BITS 15
  32. #include "mathops.h"
  33. /**
  34. * short interpolation filter (of length 33, according to spec)
  35. * for computing signal with non-integer delay
  36. */
  37. static const int16_t ff_g729_interp_filt_short[(ANALYZED_FRAC_DELAYS+1)*SHORT_INT_FILT_LEN] = {
  38. 0, 31650, 28469, 23705, 18050, 12266, 7041, 2873,
  39. 0, -1597, -2147, -1992, -1492, -933, -484, -188,
  40. };
  41. /**
  42. * long interpolation filter (of length 129, according to spec)
  43. * for computing signal with non-integer delay
  44. */
  45. static const int16_t ff_g729_interp_filt_long[(ANALYZED_FRAC_DELAYS+1)*LONG_INT_FILT_LEN] = {
  46. 0, 31915, 29436, 25569, 20676, 15206, 9639, 4439,
  47. 0, -3390, -5579, -6549, -6414, -5392, -3773, -1874,
  48. 0, 1595, 2727, 3303, 3319, 2850, 2030, 1023,
  49. 0, -887, -1527, -1860, -1876, -1614, -1150, -579,
  50. 0, 501, 859, 1041, 1044, 892, 631, 315,
  51. 0, -266, -453, -543, -538, -455, -317, -156,
  52. 0, 130, 218, 258, 253, 212, 147, 72,
  53. 0, -59, -101, -122, -123, -106, -77, -40,
  54. };
  55. /**
  56. * formant_pp_factor_num_pow[i] = FORMANT_PP_FACTOR_NUM^(i+1)
  57. */
  58. static const int16_t formant_pp_factor_num_pow[10]= {
  59. /* (0.15) */
  60. 18022, 9912, 5451, 2998, 1649, 907, 499, 274, 151, 83
  61. };
  62. /**
  63. * formant_pp_factor_den_pow[i] = FORMANT_PP_FACTOR_DEN^(i+1)
  64. */
  65. static const int16_t formant_pp_factor_den_pow[10] = {
  66. /* (0.15) */
  67. 22938, 16057, 11240, 7868, 5508, 3856, 2699, 1889, 1322, 925
  68. };
  69. /**
  70. * \brief Residual signal calculation (4.2.1 if G.729)
  71. * \param out [out] output data filtered through A(z/FORMANT_PP_FACTOR_NUM)
  72. * \param filter_coeffs (3.12) A(z/FORMANT_PP_FACTOR_NUM) filter coefficients
  73. * \param in input speech data to process
  74. * \param subframe_size size of one subframe
  75. *
  76. * \note in buffer must contain 10 items of previous speech data before top of the buffer
  77. * \remark It is safe to pass the same buffer for input and output.
  78. */
  79. static void residual_filter(int16_t* out, const int16_t* filter_coeffs, const int16_t* in,
  80. int subframe_size)
  81. {
  82. int i, n;
  83. for (n = subframe_size - 1; n >= 0; n--) {
  84. int sum = 0x800;
  85. for (i = 0; i < 10; i++)
  86. sum += filter_coeffs[i] * in[n - i - 1];
  87. out[n] = in[n] + (sum >> 12);
  88. }
  89. }
  90. /**
  91. * \brief long-term postfilter (4.2.1)
  92. * \param dsp initialized DSP context
  93. * \param pitch_delay_int integer part of the pitch delay in the first subframe
  94. * \param residual filtering input data
  95. * \param residual_filt [out] speech signal with applied A(z/FORMANT_PP_FACTOR_NUM) filter
  96. * \param subframe_size size of subframe
  97. *
  98. * \return 0 if long-term prediction gain is less than 3dB, 1 - otherwise
  99. */
  100. static int16_t long_term_filter(AudioDSPContext *adsp, int pitch_delay_int,
  101. const int16_t* residual, int16_t *residual_filt,
  102. int subframe_size)
  103. {
  104. int i, k, tmp, tmp2;
  105. int sum;
  106. int L_temp0;
  107. int L_temp1;
  108. int64_t L64_temp0;
  109. int64_t L64_temp1;
  110. int16_t shift;
  111. int corr_int_num, corr_int_den;
  112. int ener;
  113. int16_t sh_ener;
  114. int16_t gain_num,gain_den; //selected signal's gain numerator and denominator
  115. int16_t sh_gain_num, sh_gain_den;
  116. int gain_num_square;
  117. int16_t gain_long_num,gain_long_den; //filtered through long interpolation filter signal's gain numerator and denominator
  118. int16_t sh_gain_long_num, sh_gain_long_den;
  119. int16_t best_delay_int, best_delay_frac;
  120. int16_t delayed_signal_offset;
  121. int lt_filt_factor_a, lt_filt_factor_b;
  122. int16_t * selected_signal;
  123. const int16_t * selected_signal_const; //Necessary to avoid compiler warning
  124. int16_t sig_scaled[SUBFRAME_SIZE + RES_PREV_DATA_SIZE];
  125. int16_t delayed_signal[ANALYZED_FRAC_DELAYS][SUBFRAME_SIZE+1];
  126. int corr_den[ANALYZED_FRAC_DELAYS][2];
  127. tmp = 0;
  128. for(i=0; i<subframe_size + RES_PREV_DATA_SIZE; i++)
  129. tmp |= FFABS(residual[i]);
  130. if(!tmp)
  131. shift = 3;
  132. else
  133. shift = av_log2(tmp) - 11;
  134. if (shift > 0)
  135. for (i = 0; i < subframe_size + RES_PREV_DATA_SIZE; i++)
  136. sig_scaled[i] = residual[i] >> shift;
  137. else
  138. for (i = 0; i < subframe_size + RES_PREV_DATA_SIZE; i++)
  139. sig_scaled[i] = (unsigned)residual[i] << -shift;
  140. /* Start of best delay searching code */
  141. gain_num = 0;
  142. ener = adsp->scalarproduct_int16(sig_scaled + RES_PREV_DATA_SIZE,
  143. sig_scaled + RES_PREV_DATA_SIZE,
  144. subframe_size);
  145. if (ener) {
  146. sh_ener = av_log2(ener) - 14;
  147. sh_ener = FFMAX(sh_ener, 0);
  148. ener >>= sh_ener;
  149. /* Search for best pitch delay.
  150. sum{ r(n) * r(k,n) ] }^2
  151. R'(k)^2 := -------------------------
  152. sum{ r(k,n) * r(k,n) }
  153. R(T) := sum{ r(n) * r(n-T) ] }
  154. where
  155. r(n-T) is integer delayed signal with delay T
  156. r(k,n) is non-integer delayed signal with integer delay best_delay
  157. and fractional delay k */
  158. /* Find integer delay best_delay which maximizes correlation R(T).
  159. This is also equals to numerator of R'(0),
  160. since the fine search (second step) is done with 1/8
  161. precision around best_delay. */
  162. corr_int_num = 0;
  163. best_delay_int = pitch_delay_int - 1;
  164. for (i = pitch_delay_int - 1; i <= pitch_delay_int + 1; i++) {
  165. sum = adsp->scalarproduct_int16(sig_scaled + RES_PREV_DATA_SIZE,
  166. sig_scaled + RES_PREV_DATA_SIZE - i,
  167. subframe_size);
  168. if (sum > corr_int_num) {
  169. corr_int_num = sum;
  170. best_delay_int = i;
  171. }
  172. }
  173. if (corr_int_num) {
  174. /* Compute denominator of pseudo-normalized correlation R'(0). */
  175. corr_int_den = adsp->scalarproduct_int16(sig_scaled + RES_PREV_DATA_SIZE - best_delay_int,
  176. sig_scaled + RES_PREV_DATA_SIZE - best_delay_int,
  177. subframe_size);
  178. /* Compute signals with non-integer delay k (with 1/8 precision),
  179. where k is in [0;6] range.
  180. Entire delay is qual to best_delay+(k+1)/8
  181. This is archieved by applying an interpolation filter of
  182. legth 33 to source signal. */
  183. for (k = 0; k < ANALYZED_FRAC_DELAYS; k++) {
  184. ff_acelp_interpolate(&delayed_signal[k][0],
  185. &sig_scaled[RES_PREV_DATA_SIZE - best_delay_int],
  186. ff_g729_interp_filt_short,
  187. ANALYZED_FRAC_DELAYS+1,
  188. 8 - k - 1,
  189. SHORT_INT_FILT_LEN,
  190. subframe_size + 1);
  191. }
  192. /* Compute denominator of pseudo-normalized correlation R'(k).
  193. corr_den[k][0] is square root of R'(k) denominator, for int(T) == int(T0)
  194. corr_den[k][1] is square root of R'(k) denominator, for int(T) == int(T0)+1
  195. Also compute maximum value of above denominators over all k. */
  196. tmp = corr_int_den;
  197. for (k = 0; k < ANALYZED_FRAC_DELAYS; k++) {
  198. sum = adsp->scalarproduct_int16(&delayed_signal[k][1],
  199. &delayed_signal[k][1],
  200. subframe_size - 1);
  201. corr_den[k][0] = sum + delayed_signal[k][0 ] * delayed_signal[k][0 ];
  202. corr_den[k][1] = sum + delayed_signal[k][subframe_size] * delayed_signal[k][subframe_size];
  203. tmp = FFMAX3(tmp, corr_den[k][0], corr_den[k][1]);
  204. }
  205. sh_gain_den = av_log2(tmp) - 14;
  206. if (sh_gain_den >= 0) {
  207. sh_gain_num = FFMAX(sh_gain_den, sh_ener);
  208. /* Loop through all k and find delay that maximizes
  209. R'(k) correlation.
  210. Search is done in [int(T0)-1; intT(0)+1] range
  211. with 1/8 precision. */
  212. delayed_signal_offset = 1;
  213. best_delay_frac = 0;
  214. gain_den = corr_int_den >> sh_gain_den;
  215. gain_num = corr_int_num >> sh_gain_num;
  216. gain_num_square = gain_num * gain_num;
  217. for (k = 0; k < ANALYZED_FRAC_DELAYS; k++) {
  218. for (i = 0; i < 2; i++) {
  219. int16_t gain_num_short, gain_den_short;
  220. int gain_num_short_square;
  221. /* Compute numerator of pseudo-normalized
  222. correlation R'(k). */
  223. sum = adsp->scalarproduct_int16(&delayed_signal[k][i],
  224. sig_scaled + RES_PREV_DATA_SIZE,
  225. subframe_size);
  226. gain_num_short = FFMAX(sum >> sh_gain_num, 0);
  227. /*
  228. gain_num_short_square gain_num_square
  229. R'(T)^2 = -----------------------, max R'(T)^2= --------------
  230. den gain_den
  231. */
  232. gain_num_short_square = gain_num_short * gain_num_short;
  233. gain_den_short = corr_den[k][i] >> sh_gain_den;
  234. tmp = MULL(gain_num_short_square, gain_den, FRAC_BITS);
  235. tmp2 = MULL(gain_num_square, gain_den_short, FRAC_BITS);
  236. // R'(T)^2 > max R'(T)^2
  237. if (tmp > tmp2) {
  238. gain_num = gain_num_short;
  239. gain_den = gain_den_short;
  240. gain_num_square = gain_num_short_square;
  241. delayed_signal_offset = i;
  242. best_delay_frac = k + 1;
  243. }
  244. }
  245. }
  246. /*
  247. R'(T)^2
  248. 2 * --------- < 1
  249. R(0)
  250. */
  251. L64_temp0 = (int64_t)gain_num_square << ((sh_gain_num << 1) + 1);
  252. L64_temp1 = ((int64_t)gain_den * ener) << (sh_gain_den + sh_ener);
  253. if (L64_temp0 < L64_temp1)
  254. gain_num = 0;
  255. } // if(sh_gain_den >= 0)
  256. } // if(corr_int_num)
  257. } // if(ener)
  258. /* End of best delay searching code */
  259. if (!gain_num) {
  260. memcpy(residual_filt, residual + RES_PREV_DATA_SIZE, subframe_size * sizeof(int16_t));
  261. /* Long-term prediction gain is less than 3dB. Long-term postfilter is disabled. */
  262. return 0;
  263. }
  264. if (best_delay_frac) {
  265. /* Recompute delayed signal with an interpolation filter of length 129. */
  266. ff_acelp_interpolate(residual_filt,
  267. &sig_scaled[RES_PREV_DATA_SIZE - best_delay_int + delayed_signal_offset],
  268. ff_g729_interp_filt_long,
  269. ANALYZED_FRAC_DELAYS + 1,
  270. 8 - best_delay_frac,
  271. LONG_INT_FILT_LEN,
  272. subframe_size + 1);
  273. /* Compute R'(k) correlation's numerator. */
  274. sum = adsp->scalarproduct_int16(residual_filt,
  275. sig_scaled + RES_PREV_DATA_SIZE,
  276. subframe_size);
  277. if (sum < 0) {
  278. gain_long_num = 0;
  279. sh_gain_long_num = 0;
  280. } else {
  281. tmp = av_log2(sum) - 14;
  282. tmp = FFMAX(tmp, 0);
  283. sum >>= tmp;
  284. gain_long_num = sum;
  285. sh_gain_long_num = tmp;
  286. }
  287. /* Compute R'(k) correlation's denominator. */
  288. sum = adsp->scalarproduct_int16(residual_filt, residual_filt, subframe_size);
  289. tmp = av_log2(sum) - 14;
  290. tmp = FFMAX(tmp, 0);
  291. sum >>= tmp;
  292. gain_long_den = sum;
  293. sh_gain_long_den = tmp;
  294. /* Select between original and delayed signal.
  295. Delayed signal will be selected if it increases R'(k)
  296. correlation. */
  297. L_temp0 = gain_num * gain_num;
  298. L_temp0 = MULL(L_temp0, gain_long_den, FRAC_BITS);
  299. L_temp1 = gain_long_num * gain_long_num;
  300. L_temp1 = MULL(L_temp1, gain_den, FRAC_BITS);
  301. tmp = ((sh_gain_long_num - sh_gain_num) << 1) - (sh_gain_long_den - sh_gain_den);
  302. if (tmp > 0)
  303. L_temp0 >>= tmp;
  304. else
  305. L_temp1 >>= -tmp;
  306. /* Check if longer filter increases the values of R'(k). */
  307. if (L_temp1 > L_temp0) {
  308. /* Select long filter. */
  309. selected_signal = residual_filt;
  310. gain_num = gain_long_num;
  311. gain_den = gain_long_den;
  312. sh_gain_num = sh_gain_long_num;
  313. sh_gain_den = sh_gain_long_den;
  314. } else
  315. /* Select short filter. */
  316. selected_signal = &delayed_signal[best_delay_frac-1][delayed_signal_offset];
  317. /* Rescale selected signal to original value. */
  318. if (shift > 0)
  319. for (i = 0; i < subframe_size; i++)
  320. selected_signal[i] <<= shift;
  321. else
  322. for (i = 0; i < subframe_size; i++)
  323. selected_signal[i] >>= -shift;
  324. /* necessary to avoid compiler warning */
  325. selected_signal_const = selected_signal;
  326. } // if(best_delay_frac)
  327. else
  328. selected_signal_const = residual + RES_PREV_DATA_SIZE - (best_delay_int + 1 - delayed_signal_offset);
  329. #ifdef G729_BITEXACT
  330. tmp = sh_gain_num - sh_gain_den;
  331. if (tmp > 0)
  332. gain_den >>= tmp;
  333. else
  334. gain_num >>= -tmp;
  335. if (gain_num > gain_den)
  336. lt_filt_factor_a = MIN_LT_FILT_FACTOR_A;
  337. else {
  338. gain_num >>= 2;
  339. gain_den >>= 1;
  340. lt_filt_factor_a = (gain_den << 15) / (gain_den + gain_num);
  341. }
  342. #else
  343. L64_temp0 = (((int64_t)gain_num) << sh_gain_num) >> 1;
  344. L64_temp1 = ((int64_t)gain_den) << sh_gain_den;
  345. lt_filt_factor_a = FFMAX((L64_temp1 << 15) / (L64_temp1 + L64_temp0), MIN_LT_FILT_FACTOR_A);
  346. #endif
  347. /* Filter through selected filter. */
  348. lt_filt_factor_b = 32767 - lt_filt_factor_a + 1;
  349. ff_acelp_weighted_vector_sum(residual_filt, residual + RES_PREV_DATA_SIZE,
  350. selected_signal_const,
  351. lt_filt_factor_a, lt_filt_factor_b,
  352. 1<<14, 15, subframe_size);
  353. // Long-term prediction gain is larger than 3dB.
  354. return 1;
  355. }
  356. /**
  357. * \brief Calculate reflection coefficient for tilt compensation filter (4.2.3).
  358. * \param dsp initialized DSP context
  359. * \param lp_gn (3.12) coefficients of A(z/FORMANT_PP_FACTOR_NUM) filter
  360. * \param lp_gd (3.12) coefficients of A(z/FORMANT_PP_FACTOR_DEN) filter
  361. * \param speech speech to update
  362. * \param subframe_size size of subframe
  363. *
  364. * \return (3.12) reflection coefficient
  365. *
  366. * \remark The routine also calculates the gain term for the short-term
  367. * filter (gf) and multiplies the speech data by 1/gf.
  368. *
  369. * \note All members of lp_gn, except 10-19 must be equal to zero.
  370. */
  371. static int16_t get_tilt_comp(AudioDSPContext *adsp, int16_t *lp_gn,
  372. const int16_t *lp_gd, int16_t* speech,
  373. int subframe_size)
  374. {
  375. int rh1,rh0; // (3.12)
  376. int temp;
  377. int i;
  378. int gain_term;
  379. lp_gn[10] = 4096; //1.0 in (3.12)
  380. /* Apply 1/A(z/FORMANT_PP_FACTOR_DEN) filter to hf. */
  381. ff_celp_lp_synthesis_filter(lp_gn + 11, lp_gd + 1, lp_gn + 11, 22, 10, 0, 0, 0x800);
  382. /* Now lp_gn (starting with 10) contains impulse response
  383. of A(z/FORMANT_PP_FACTOR_NUM)/A(z/FORMANT_PP_FACTOR_DEN) filter. */
  384. rh0 = adsp->scalarproduct_int16(lp_gn + 10, lp_gn + 10, 20);
  385. rh1 = adsp->scalarproduct_int16(lp_gn + 10, lp_gn + 11, 20);
  386. /* downscale to avoid overflow */
  387. temp = av_log2(rh0) - 14;
  388. if (temp > 0) {
  389. rh0 >>= temp;
  390. rh1 >>= temp;
  391. }
  392. if (FFABS(rh1) > rh0 || !rh0)
  393. return 0;
  394. gain_term = 0;
  395. for (i = 0; i < 20; i++)
  396. gain_term += FFABS(lp_gn[i + 10]);
  397. gain_term >>= 2; // (3.12) -> (5.10)
  398. if (gain_term > 0x400) { // 1.0 in (5.10)
  399. temp = 0x2000000 / gain_term; // 1.0/gain_term in (0.15)
  400. for (i = 0; i < subframe_size; i++)
  401. speech[i] = (speech[i] * temp + 0x4000) >> 15;
  402. }
  403. return -(rh1 << 15) / rh0;
  404. }
  405. /**
  406. * \brief Apply tilt compensation filter (4.2.3).
  407. * \param res_pst [in/out] residual signal (partially filtered)
  408. * \param k1 (3.12) reflection coefficient
  409. * \param subframe_size size of subframe
  410. * \param ht_prev_data previous data for 4.2.3, equation 86
  411. *
  412. * \return new value for ht_prev_data
  413. */
  414. static int16_t apply_tilt_comp(int16_t* out, int16_t* res_pst, int refl_coeff,
  415. int subframe_size, int16_t ht_prev_data)
  416. {
  417. int tmp, tmp2;
  418. int i;
  419. int gt, ga;
  420. int fact, sh_fact;
  421. if (refl_coeff > 0) {
  422. gt = (refl_coeff * G729_TILT_FACTOR_PLUS + 0x4000) >> 15;
  423. fact = 0x4000; // 0.5 in (0.15)
  424. sh_fact = 15;
  425. } else {
  426. gt = (refl_coeff * G729_TILT_FACTOR_MINUS + 0x4000) >> 15;
  427. fact = 0x800; // 0.5 in (3.12)
  428. sh_fact = 12;
  429. }
  430. ga = (fact << 15) / av_clip_int16(32768 - FFABS(gt));
  431. gt >>= 1;
  432. /* Apply tilt compensation filter to signal. */
  433. tmp = res_pst[subframe_size - 1];
  434. for (i = subframe_size - 1; i >= 1; i--) {
  435. tmp2 = (gt * res_pst[i-1]) * 2 + 0x4000;
  436. tmp2 = res_pst[i] + (tmp2 >> 15);
  437. tmp2 = (tmp2 * ga * 2 + fact) >> sh_fact;
  438. out[i] = tmp2;
  439. }
  440. tmp2 = (gt * ht_prev_data) * 2 + 0x4000;
  441. tmp2 = res_pst[0] + (tmp2 >> 15);
  442. tmp2 = (tmp2 * ga * 2 + fact) >> sh_fact;
  443. out[0] = tmp2;
  444. return tmp;
  445. }
  446. void ff_g729_postfilter(AudioDSPContext *adsp, int16_t* ht_prev_data, int* voicing,
  447. const int16_t *lp_filter_coeffs, int pitch_delay_int,
  448. int16_t* residual, int16_t* res_filter_data,
  449. int16_t* pos_filter_data, int16_t *speech, int subframe_size)
  450. {
  451. int16_t residual_filt_buf[SUBFRAME_SIZE+11];
  452. int16_t lp_gn[33]; // (3.12)
  453. int16_t lp_gd[11]; // (3.12)
  454. int tilt_comp_coeff;
  455. int i;
  456. /* Zero-filling is necessary for tilt-compensation filter. */
  457. memset(lp_gn, 0, 33 * sizeof(int16_t));
  458. /* Calculate A(z/FORMANT_PP_FACTOR_NUM) filter coefficients. */
  459. for (i = 0; i < 10; i++)
  460. lp_gn[i + 11] = (lp_filter_coeffs[i + 1] * formant_pp_factor_num_pow[i] + 0x4000) >> 15;
  461. /* Calculate A(z/FORMANT_PP_FACTOR_DEN) filter coefficients. */
  462. for (i = 0; i < 10; i++)
  463. lp_gd[i + 1] = (lp_filter_coeffs[i + 1] * formant_pp_factor_den_pow[i] + 0x4000) >> 15;
  464. /* residual signal calculation (one-half of short-term postfilter) */
  465. memcpy(speech - 10, res_filter_data, 10 * sizeof(int16_t));
  466. residual_filter(residual + RES_PREV_DATA_SIZE, lp_gn + 11, speech, subframe_size);
  467. /* Save data to use it in the next subframe. */
  468. memcpy(res_filter_data, speech + subframe_size - 10, 10 * sizeof(int16_t));
  469. /* long-term filter. If long-term prediction gain is larger than 3dB (returned value is
  470. nonzero) then declare current subframe as periodic. */
  471. i = long_term_filter(adsp, pitch_delay_int,
  472. residual, residual_filt_buf + 10,
  473. subframe_size);
  474. *voicing = FFMAX(*voicing, i);
  475. /* shift residual for using in next subframe */
  476. memmove(residual, residual + subframe_size, RES_PREV_DATA_SIZE * sizeof(int16_t));
  477. /* short-term filter tilt compensation */
  478. tilt_comp_coeff = get_tilt_comp(adsp, lp_gn, lp_gd, residual_filt_buf + 10, subframe_size);
  479. /* Apply second half of short-term postfilter: 1/A(z/FORMANT_PP_FACTOR_DEN) */
  480. ff_celp_lp_synthesis_filter(pos_filter_data + 10, lp_gd + 1,
  481. residual_filt_buf + 10,
  482. subframe_size, 10, 0, 0, 0x800);
  483. memcpy(pos_filter_data, pos_filter_data + subframe_size, 10 * sizeof(int16_t));
  484. *ht_prev_data = apply_tilt_comp(speech, pos_filter_data + 10, tilt_comp_coeff,
  485. subframe_size, *ht_prev_data);
  486. }
  487. /**
  488. * \brief Adaptive gain control (4.2.4)
  489. * \param gain_before gain of speech before applying postfilters
  490. * \param gain_after gain of speech after applying postfilters
  491. * \param speech [in/out] signal buffer
  492. * \param subframe_size length of subframe
  493. * \param gain_prev (3.12) previous value of gain coefficient
  494. *
  495. * \return (3.12) last value of gain coefficient
  496. */
  497. int16_t ff_g729_adaptive_gain_control(int gain_before, int gain_after, int16_t *speech,
  498. int subframe_size, int16_t gain_prev)
  499. {
  500. int gain; // (3.12)
  501. int n;
  502. int exp_before, exp_after;
  503. if(!gain_after && gain_before)
  504. return 0;
  505. if (gain_before) {
  506. exp_before = 14 - av_log2(gain_before);
  507. gain_before = bidir_sal(gain_before, exp_before);
  508. exp_after = 14 - av_log2(gain_after);
  509. gain_after = bidir_sal(gain_after, exp_after);
  510. if (gain_before < gain_after) {
  511. gain = (gain_before << 15) / gain_after;
  512. gain = bidir_sal(gain, exp_after - exp_before - 1);
  513. } else {
  514. gain = ((gain_before - gain_after) << 14) / gain_after + 0x4000;
  515. gain = bidir_sal(gain, exp_after - exp_before);
  516. }
  517. gain = (gain * G729_AGC_FAC1 + 0x4000) >> 15; // gain * (1-0.9875)
  518. } else
  519. gain = 0;
  520. for (n = 0; n < subframe_size; n++) {
  521. // gain_prev = gain + 0.9875 * gain_prev
  522. gain_prev = (G729_AGC_FACTOR * gain_prev + 0x4000) >> 15;
  523. gain_prev = av_clip_int16(gain + gain_prev);
  524. speech[n] = av_clip_int16((speech[n] * gain_prev + 0x2000) >> 14);
  525. }
  526. return gain_prev;
  527. }