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.

611 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(DSPContext *dsp, 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] = residual[i] << -shift;
  140. /* Start of best delay searching code */
  141. gain_num = 0;
  142. ener = dsp->scalarproduct_int16(sig_scaled + RES_PREV_DATA_SIZE,
  143. sig_scaled + RES_PREV_DATA_SIZE,
  144. subframe_size, 0);
  145. if (ener) {
  146. sh_ener = FFMAX(av_log2(ener) - 14, 0);
  147. ener >>= sh_ener;
  148. /* Search for best pitch delay.
  149. sum{ r(n) * r(k,n) ] }^2
  150. R'(k)^2 := -------------------------
  151. sum{ r(k,n) * r(k,n) }
  152. R(T) := sum{ r(n) * r(n-T) ] }
  153. where
  154. r(n-T) is integer delayed signal with delay T
  155. r(k,n) is non-integer delayed signal with integer delay best_delay
  156. and fractional delay k */
  157. /* Find integer delay best_delay which maximizes correlation R(T).
  158. This is also equals to numerator of R'(0),
  159. since the fine search (second step) is done with 1/8
  160. precision around best_delay. */
  161. corr_int_num = 0;
  162. best_delay_int = pitch_delay_int - 1;
  163. for (i = pitch_delay_int - 1; i <= pitch_delay_int + 1; i++) {
  164. sum = dsp->scalarproduct_int16(sig_scaled + RES_PREV_DATA_SIZE,
  165. sig_scaled + RES_PREV_DATA_SIZE - i,
  166. subframe_size, 0);
  167. if (sum > corr_int_num) {
  168. corr_int_num = sum;
  169. best_delay_int = i;
  170. }
  171. }
  172. if (corr_int_num) {
  173. /* Compute denominator of pseudo-normalized correlation R'(0). */
  174. corr_int_den = dsp->scalarproduct_int16(sig_scaled - best_delay_int + RES_PREV_DATA_SIZE,
  175. sig_scaled - best_delay_int + RES_PREV_DATA_SIZE,
  176. subframe_size, 0);
  177. /* Compute signals with non-integer delay k (with 1/8 precision),
  178. where k is in [0;6] range.
  179. Entire delay is qual to best_delay+(k+1)/8
  180. This is archieved by applying an interpolation filter of
  181. legth 33 to source signal. */
  182. for (k = 0; k < ANALYZED_FRAC_DELAYS; k++) {
  183. ff_acelp_interpolate(&delayed_signal[k][0],
  184. &sig_scaled[RES_PREV_DATA_SIZE - best_delay_int],
  185. ff_g729_interp_filt_short,
  186. ANALYZED_FRAC_DELAYS+1,
  187. 8 - k - 1,
  188. SHORT_INT_FILT_LEN,
  189. subframe_size + 1);
  190. }
  191. /* Compute denominator of pseudo-normalized correlation R'(k).
  192. corr_den[k][0] is square root of R'(k) denominator, for int(T) == int(T0)
  193. corr_den[k][1] is square root of R'(k) denominator, for int(T) == int(T0)+1
  194. Also compute maximum value of above denominators over all k. */
  195. tmp = corr_int_den;
  196. for (k = 0; k < ANALYZED_FRAC_DELAYS; k++) {
  197. sum = dsp->scalarproduct_int16(&delayed_signal[k][1],
  198. &delayed_signal[k][1],
  199. subframe_size - 1, 0);
  200. corr_den[k][0] = sum + delayed_signal[k][0 ] * delayed_signal[k][0 ];
  201. corr_den[k][1] = sum + delayed_signal[k][subframe_size] * delayed_signal[k][subframe_size];
  202. tmp = FFMAX3(tmp, corr_den[k][0], corr_den[k][1]);
  203. }
  204. sh_gain_den = av_log2(tmp) - 14;
  205. if (sh_gain_den >= 0) {
  206. sh_gain_num = FFMAX(sh_gain_den, sh_ener);
  207. /* Loop through all k and find delay that maximizes
  208. R'(k) correlation.
  209. Search is done in [int(T0)-1; intT(0)+1] range
  210. with 1/8 precision. */
  211. delayed_signal_offset = 1;
  212. best_delay_frac = 0;
  213. gain_den = corr_int_den >> sh_gain_den;
  214. gain_num = corr_int_num >> sh_gain_num;
  215. gain_num_square = gain_num * gain_num;
  216. for (k = 0; k < ANALYZED_FRAC_DELAYS; k++) {
  217. for (i = 0; i < 2; i++) {
  218. int16_t gain_num_short, gain_den_short;
  219. int gain_num_short_square;
  220. /* Compute numerator of pseudo-normalized
  221. correlation R'(k). */
  222. sum = dsp->scalarproduct_int16(&delayed_signal[k][i],
  223. sig_scaled + RES_PREV_DATA_SIZE,
  224. subframe_size, 0);
  225. gain_num_short = FFMAX(sum >> sh_gain_num, 0);
  226. /*
  227. gain_num_short_square gain_num_square
  228. R'(T)^2 = -----------------------, max R'(T)^2= --------------
  229. den gain_den
  230. */
  231. gain_num_short_square = gain_num_short * gain_num_short;
  232. gain_den_short = corr_den[k][i] >> sh_gain_den;
  233. tmp = MULL(gain_num_short_square, gain_den, FRAC_BITS);
  234. tmp2 = MULL(gain_num_square, gain_den_short, FRAC_BITS);
  235. // R'(T)^2 > max R'(T)^2
  236. if (tmp > tmp2) {
  237. gain_num = gain_num_short;
  238. gain_den = gain_den_short;
  239. gain_num_square = gain_num_short_square;
  240. delayed_signal_offset = i;
  241. best_delay_frac = k + 1;
  242. }
  243. }
  244. }
  245. /*
  246. R'(T)^2
  247. 2 * --------- < 1
  248. R(0)
  249. */
  250. L64_temp0 = (int64_t)gain_num_square << ((sh_gain_num << 1) + 1);
  251. L64_temp1 = ((int64_t)gain_den * ener) << (sh_gain_den + sh_ener);
  252. if (L64_temp0 < L64_temp1)
  253. gain_num = 0;
  254. } // if(sh_gain_den >= 0)
  255. } // if(corr_int_num)
  256. } // if(ener)
  257. /* End of best delay searching code */
  258. if (!gain_num) {
  259. memcpy(residual_filt, residual + RES_PREV_DATA_SIZE, subframe_size * sizeof(int16_t));
  260. /* Long-term prediction gain is less than 3dB. Long-term postfilter is disabled. */
  261. return 0;
  262. }
  263. if (best_delay_frac) {
  264. /* Recompute delayed signal with an interpolation filter of length 129. */
  265. ff_acelp_interpolate(residual_filt,
  266. &sig_scaled[RES_PREV_DATA_SIZE - best_delay_int + delayed_signal_offset],
  267. ff_g729_interp_filt_long,
  268. ANALYZED_FRAC_DELAYS + 1,
  269. 8 - best_delay_frac,
  270. LONG_INT_FILT_LEN,
  271. subframe_size + 1);
  272. /* Compute R'(k) correlation's numerator. */
  273. sum = dsp->scalarproduct_int16(residual_filt,
  274. sig_scaled + RES_PREV_DATA_SIZE,
  275. subframe_size, 0);
  276. if (sum < 0) {
  277. gain_long_num = 0;
  278. sh_gain_long_num = 0;
  279. } else {
  280. tmp = FFMAX(av_log2(sum) - 14, 0);
  281. sum >>= tmp;
  282. gain_long_num = sum;
  283. sh_gain_long_num = tmp;
  284. }
  285. /* Compute R'(k) correlation's denominator. */
  286. sum = dsp->scalarproduct_int16(residual_filt, residual_filt, subframe_size, 0);
  287. tmp = FFMAX(av_log2(sum) - 14, 0);
  288. sum >>= tmp;
  289. gain_long_den = sum;
  290. sh_gain_long_den = tmp;
  291. /* Select between original and delayed signal.
  292. Delayed signal will be selected if it increases R'(k)
  293. correlation. */
  294. L_temp0 = gain_num * gain_num;
  295. L_temp0 = MULL(L_temp0, gain_long_den, FRAC_BITS);
  296. L_temp1 = gain_long_num * gain_long_num;
  297. L_temp1 = MULL(L_temp1, gain_den, FRAC_BITS);
  298. tmp = ((sh_gain_long_num - sh_gain_num) << 1) - (sh_gain_long_den - sh_gain_den);
  299. if (tmp > 0)
  300. L_temp0 >>= tmp;
  301. else
  302. L_temp1 >>= -tmp;
  303. /* Check if longer filter increases the values of R'(k). */
  304. if (L_temp1 > L_temp0) {
  305. /* Select long filter. */
  306. selected_signal = residual_filt;
  307. gain_num = gain_long_num;
  308. gain_den = gain_long_den;
  309. sh_gain_num = sh_gain_long_num;
  310. sh_gain_den = sh_gain_long_den;
  311. } else
  312. /* Select short filter. */
  313. selected_signal = &delayed_signal[best_delay_frac-1][delayed_signal_offset];
  314. /* Rescale selected signal to original value. */
  315. if (shift > 0)
  316. for (i = 0; i < subframe_size; i++)
  317. selected_signal[i] <<= shift;
  318. else
  319. for (i = 0; i < subframe_size; i++)
  320. selected_signal[i] >>= -shift;
  321. /* necessary to avoid compiler warning */
  322. selected_signal_const = selected_signal;
  323. } // if(best_delay_frac)
  324. else
  325. selected_signal_const = residual + RES_PREV_DATA_SIZE - (best_delay_int + 1 - delayed_signal_offset);
  326. #ifdef G729_BITEXACT
  327. tmp = sh_gain_num - sh_gain_den;
  328. if (tmp > 0)
  329. gain_den >>= tmp;
  330. else
  331. gain_num >>= -tmp;
  332. if (gain_num > gain_den)
  333. lt_filt_factor_a = MIN_LT_FILT_FACTOR_A;
  334. else {
  335. gain_num >>= 2;
  336. gain_den >>= 1;
  337. lt_filt_factor_a = (gain_den << 15) / (gain_den + gain_num);
  338. }
  339. #else
  340. L64_temp0 = ((int64_t)gain_num) << (sh_gain_num - 1);
  341. L64_temp1 = ((int64_t)gain_den) << sh_gain_den;
  342. lt_filt_factor_a = FFMAX((L64_temp1 << 15) / (L64_temp1 + L64_temp0), MIN_LT_FILT_FACTOR_A);
  343. #endif
  344. /* Filter through selected filter. */
  345. lt_filt_factor_b = 32767 - lt_filt_factor_a + 1;
  346. ff_acelp_weighted_vector_sum(residual_filt, residual + RES_PREV_DATA_SIZE,
  347. selected_signal_const,
  348. lt_filt_factor_a, lt_filt_factor_b,
  349. 1<<14, 15, subframe_size);
  350. // Long-term prediction gain is larger than 3dB.
  351. return 1;
  352. }
  353. /**
  354. * \brief Calculate reflection coefficient for tilt compensation filter (4.2.3).
  355. * \param dsp initialized DSP context
  356. * \param lp_gn (3.12) coefficients of A(z/FORMANT_PP_FACTOR_NUM) filter
  357. * \param lp_gd (3.12) coefficients of A(z/FORMANT_PP_FACTOR_DEN) filter
  358. * \param speech speech to update
  359. * \param subframe_size size of subframe
  360. *
  361. * \return (3.12) reflection coefficient
  362. *
  363. * \remark The routine also calculates the gain term for the short-term
  364. * filter (gf) and multiplies the speech data by 1/gf.
  365. *
  366. * \note All members of lp_gn, except 10-19 must be equal to zero.
  367. */
  368. static int16_t get_tilt_comp(DSPContext *dsp, int16_t *lp_gn,
  369. const int16_t *lp_gd, int16_t* speech,
  370. int subframe_size)
  371. {
  372. int rh1,rh0; // (3.12)
  373. int temp;
  374. int i;
  375. int gain_term;
  376. lp_gn[10] = 4096; //1.0 in (3.12)
  377. /* Apply 1/A(z/FORMANT_PP_FACTOR_DEN) filter to hf. */
  378. ff_celp_lp_synthesis_filter(lp_gn + 11, lp_gd + 1, lp_gn + 11, 22, 10, 0, 0, 0x800);
  379. /* Now lp_gn (starting with 10) contains impulse response
  380. of A(z/FORMANT_PP_FACTOR_NUM)/A(z/FORMANT_PP_FACTOR_DEN) filter. */
  381. rh0 = dsp->scalarproduct_int16(lp_gn + 10, lp_gn + 10, 20, 0);
  382. rh1 = dsp->scalarproduct_int16(lp_gn + 10, lp_gn + 11, 20, 0);
  383. /* downscale to avoid overflow */
  384. temp = av_log2(rh0) - 14;
  385. if (temp > 0) {
  386. rh0 >>= temp;
  387. rh1 >>= temp;
  388. }
  389. if (FFABS(rh1) > rh0 || !rh0)
  390. return 0;
  391. gain_term = 0;
  392. for (i = 0; i < 20; i++)
  393. gain_term += FFABS(lp_gn[i + 10]);
  394. gain_term >>= 2; // (3.12) -> (5.10)
  395. if (gain_term > 0x400) { // 1.0 in (5.10)
  396. temp = 0x2000000 / gain_term; // 1.0/gain_term in (0.15)
  397. for (i = 0; i < subframe_size; i++)
  398. speech[i] = (speech[i] * temp + 0x4000) >> 15;
  399. }
  400. return -(rh1 << 15) / rh0;
  401. }
  402. /**
  403. * \brief Apply tilt compensation filter (4.2.3).
  404. * \param res_pst [in/out] residual signal (partially filtered)
  405. * \param k1 (3.12) reflection coefficient
  406. * \param subframe_size size of subframe
  407. * \param ht_prev_data previous data for 4.2.3, equation 86
  408. *
  409. * \return new value for ht_prev_data
  410. */
  411. static int16_t apply_tilt_comp(int16_t* out, int16_t* res_pst, int refl_coeff,
  412. int subframe_size, int16_t ht_prev_data)
  413. {
  414. int tmp, tmp2;
  415. int i;
  416. int gt, ga;
  417. int fact, sh_fact;
  418. if (refl_coeff > 0) {
  419. gt = (refl_coeff * G729_TILT_FACTOR_PLUS + 0x4000) >> 15;
  420. fact = 0x4000; // 0.5 in (0.15)
  421. sh_fact = 15;
  422. } else {
  423. gt = (refl_coeff * G729_TILT_FACTOR_MINUS + 0x4000) >> 15;
  424. fact = 0x800; // 0.5 in (3.12)
  425. sh_fact = 12;
  426. }
  427. ga = (fact << 15) / av_clip_int16(32768 - FFABS(gt));
  428. gt >>= 1;
  429. /* Apply tilt compensation filter to signal. */
  430. tmp = res_pst[subframe_size - 1];
  431. for (i = subframe_size - 1; i >= 1; i--) {
  432. tmp2 = (res_pst[i] << 15) + ((gt * res_pst[i-1]) << 1);
  433. tmp2 = (tmp2 + 0x4000) >> 15;
  434. tmp2 = (tmp2 * ga * 2 + fact) >> sh_fact;
  435. out[i] = tmp2;
  436. }
  437. tmp2 = (res_pst[0] << 15) + ((gt * ht_prev_data) << 1);
  438. tmp2 = (tmp2 + 0x4000) >> 15;
  439. tmp2 = (tmp2 * ga * 2 + fact) >> sh_fact;
  440. out[0] = tmp2;
  441. return tmp;
  442. }
  443. void ff_g729_postfilter(DSPContext *dsp, int16_t* ht_prev_data, int* voicing,
  444. const int16_t *lp_filter_coeffs, int pitch_delay_int,
  445. int16_t* residual, int16_t* res_filter_data,
  446. int16_t* pos_filter_data, int16_t *speech, int subframe_size)
  447. {
  448. int16_t residual_filt_buf[SUBFRAME_SIZE+11];
  449. int16_t lp_gn[33]; // (3.12)
  450. int16_t lp_gd[11]; // (3.12)
  451. int tilt_comp_coeff;
  452. int i;
  453. /* Zero-filling is necessary for tilt-compensation filter. */
  454. memset(lp_gn, 0, 33 * sizeof(int16_t));
  455. /* Calculate A(z/FORMANT_PP_FACTOR_NUM) filter coefficients. */
  456. for (i = 0; i < 10; i++)
  457. lp_gn[i + 11] = (lp_filter_coeffs[i + 1] * formant_pp_factor_num_pow[i] + 0x4000) >> 15;
  458. /* Calculate A(z/FORMANT_PP_FACTOR_DEN) filter coefficients. */
  459. for (i = 0; i < 10; i++)
  460. lp_gd[i + 1] = (lp_filter_coeffs[i + 1] * formant_pp_factor_den_pow[i] + 0x4000) >> 15;
  461. /* residual signal calculation (one-half of short-term postfilter) */
  462. memcpy(speech - 10, res_filter_data, 10 * sizeof(int16_t));
  463. residual_filter(residual + RES_PREV_DATA_SIZE, lp_gn + 11, speech, subframe_size);
  464. /* Save data to use it in the next subframe. */
  465. memcpy(res_filter_data, speech + subframe_size - 10, 10 * sizeof(int16_t));
  466. /* long-term filter. If long-term prediction gain is larger than 3dB (returned value is
  467. nonzero) then declare current subframe as periodic. */
  468. *voicing = FFMAX(*voicing, long_term_filter(dsp, pitch_delay_int,
  469. residual, residual_filt_buf + 10,
  470. subframe_size));
  471. /* shift residual for using in next subframe */
  472. memmove(residual, residual + subframe_size, RES_PREV_DATA_SIZE * sizeof(int16_t));
  473. /* short-term filter tilt compensation */
  474. tilt_comp_coeff = get_tilt_comp(dsp, lp_gn, lp_gd, residual_filt_buf + 10, subframe_size);
  475. /* Apply second half of short-term postfilter: 1/A(z/FORMANT_PP_FACTOR_DEN) */
  476. ff_celp_lp_synthesis_filter(pos_filter_data + 10, lp_gd + 1,
  477. residual_filt_buf + 10,
  478. subframe_size, 10, 0, 0, 0x800);
  479. memcpy(pos_filter_data, pos_filter_data + subframe_size, 10 * sizeof(int16_t));
  480. *ht_prev_data = apply_tilt_comp(speech, pos_filter_data + 10, tilt_comp_coeff,
  481. subframe_size, *ht_prev_data);
  482. }
  483. /**
  484. * \brief Adaptive gain control (4.2.4)
  485. * \param gain_before gain of speech before applying postfilters
  486. * \param gain_after gain of speech after applying postfilters
  487. * \param speech [in/out] signal buffer
  488. * \param subframe_size length of subframe
  489. * \param gain_prev (3.12) previous value of gain coefficient
  490. *
  491. * \return (3.12) last value of gain coefficient
  492. */
  493. int16_t ff_g729_adaptive_gain_control(int gain_before, int gain_after, int16_t *speech,
  494. int subframe_size, int16_t gain_prev)
  495. {
  496. int gain; // (3.12)
  497. int n;
  498. int exp_before, exp_after;
  499. if(!gain_after && gain_before)
  500. return 0;
  501. if (gain_before) {
  502. exp_before = 14 - av_log2(gain_before);
  503. gain_before = bidir_sal(gain_before, exp_before);
  504. exp_after = 14 - av_log2(gain_after);
  505. gain_after = bidir_sal(gain_after, exp_after);
  506. if (gain_before < gain_after) {
  507. gain = (gain_before << 15) / gain_after;
  508. gain = bidir_sal(gain, exp_after - exp_before - 1);
  509. } else {
  510. gain = ((gain_before - gain_after) << 14) / gain_after + 0x4000;
  511. gain = bidir_sal(gain, exp_after - exp_before);
  512. }
  513. gain = (gain * G729_AGC_FAC1 + 0x4000) >> 15; // gain * (1-0.9875)
  514. } else
  515. gain = 0;
  516. for (n = 0; n < subframe_size; n++) {
  517. // gain_prev = gain + 0.9875 * gain_prev
  518. gain_prev = (G729_AGC_FACTOR * gain_prev + 0x4000) >> 15;
  519. gain_prev = av_clip_int16(gain + gain_prev);
  520. speech[n] = av_clip_int16((speech[n] * gain_prev + 0x2000) >> 14);
  521. }
  522. return gain_prev;
  523. }