filters.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /* Copyright (C) 2002-2006 Jean-Marc Valin
  2. File: filters.c
  3. Various analysis/synthesis filters
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. - Neither the name of the Xiph.org Foundation nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "config.h"
  28. #include "filters.h"
  29. #include "stack_alloc.h"
  30. #include "arch.h"
  31. #include "math_approx.h"
  32. #include "ltp.h"
  33. #include <math.h>
  34. #ifdef _USE_SSE
  35. #include "filters_sse.h"
  36. #elif defined (ARM4_ASM) || defined(ARM5E_ASM)
  37. #include "filters_arm4.h"
  38. #elif defined (BFIN_ASM)
  39. #include "filters_bfin.h"
  40. #endif
  41. void bw_lpc(spx_word16_t gamma, const spx_coef_t *lpc_in, spx_coef_t *lpc_out, int order)
  42. {
  43. int i;
  44. spx_word16_t tmp=gamma;
  45. for (i=0;i<order;i++)
  46. {
  47. lpc_out[i] = MULT16_16_P15(tmp,lpc_in[i]);
  48. tmp = MULT16_16_P15(tmp, gamma);
  49. }
  50. }
  51. void sanitize_values32(spx_word32_t *vec, spx_word32_t min_val, spx_word32_t max_val, int len)
  52. {
  53. int i;
  54. for (i=0;i<len;i++)
  55. {
  56. /* It's important we do the test that way so we can catch NaNs, which are neither greater nor smaller */
  57. if (!(vec[i]>=min_val && vec[i] <= max_val))
  58. {
  59. if (vec[i] < min_val)
  60. vec[i] = min_val;
  61. else if (vec[i] > max_val)
  62. vec[i] = max_val;
  63. else /* Has to be NaN */
  64. vec[i] = 0;
  65. }
  66. }
  67. }
  68. void highpass(const spx_word16_t *x, spx_word16_t *y, int len, int filtID, spx_mem_t *mem)
  69. {
  70. int i;
  71. #ifdef FIXED_POINT
  72. const spx_word16_t Pcoef[5][3] = {{16384, -31313, 14991}, {16384, -31569, 15249}, {16384, -31677, 15328}, {16384, -32313, 15947}, {16384, -22446, 6537}};
  73. const spx_word16_t Zcoef[5][3] = {{15672, -31344, 15672}, {15802, -31601, 15802}, {15847, -31694, 15847}, {16162, -32322, 16162}, {14418, -28836, 14418}};
  74. #else
  75. const spx_word16_t Pcoef[5][3] = {{1.00000f, -1.91120f, 0.91498f}, {1.00000f, -1.92683f, 0.93071f}, {1.00000f, -1.93338f, 0.93553f}, {1.00000f, -1.97226f, 0.97332f}, {1.00000f, -1.37000f, 0.39900f}};
  76. const spx_word16_t Zcoef[5][3] = {{0.95654f, -1.91309f, 0.95654f}, {0.96446f, -1.92879f, 0.96446f}, {0.96723f, -1.93445f, 0.96723f}, {0.98645f, -1.97277f, 0.98645f}, {0.88000f, -1.76000f, 0.88000f}};
  77. #endif
  78. const spx_word16_t *den, *num;
  79. if (filtID>4)
  80. filtID=4;
  81. den = Pcoef[filtID]; num = Zcoef[filtID];
  82. /*return;*/
  83. for (i=0;i<len;i++)
  84. {
  85. spx_word16_t yi;
  86. spx_word32_t vout = ADD32(MULT16_16(num[0], x[i]),mem[0]);
  87. yi = EXTRACT16(SATURATE(PSHR32(vout,14),32767));
  88. mem[0] = ADD32(MAC16_16(mem[1], num[1],x[i]), SHL32(MULT16_32_Q15(-den[1],vout),1));
  89. mem[1] = ADD32(MULT16_16(num[2],x[i]), SHL32(MULT16_32_Q15(-den[2],vout),1));
  90. y[i] = yi;
  91. }
  92. }
  93. #ifdef FIXED_POINT
  94. /* FIXME: These functions are ugly and probably introduce too much error */
  95. void signal_mul(const spx_sig_t *x, spx_sig_t *y, spx_word32_t scale, int len)
  96. {
  97. int i;
  98. for (i=0;i<len;i++)
  99. {
  100. y[i] = SHL32(MULT16_32_Q14(EXTRACT16(SHR32(x[i],7)),scale),7);
  101. }
  102. }
  103. void signal_div(const spx_word16_t *x, spx_word16_t *y, spx_word32_t scale, int len)
  104. {
  105. int i;
  106. if (scale > SHL32(EXTEND32(SIG_SCALING), 8))
  107. {
  108. spx_word16_t scale_1;
  109. scale = PSHR32(scale, SIG_SHIFT);
  110. scale_1 = EXTRACT16(PDIV32_16(SHL32(EXTEND32(SIG_SCALING),7),scale));
  111. for (i=0;i<len;i++)
  112. {
  113. y[i] = MULT16_16_P15(scale_1, x[i]);
  114. }
  115. } else if (scale > SHR32(EXTEND32(SIG_SCALING), 2)) {
  116. spx_word16_t scale_1;
  117. scale = PSHR32(scale, SIG_SHIFT-5);
  118. scale_1 = DIV32_16(SHL32(EXTEND32(SIG_SCALING),3),scale);
  119. for (i=0;i<len;i++)
  120. {
  121. y[i] = PSHR32(MULT16_16(scale_1, SHL16(x[i],2)),8);
  122. }
  123. } else {
  124. spx_word16_t scale_1;
  125. scale = PSHR32(scale, SIG_SHIFT-7);
  126. if (scale < 5)
  127. scale = 5;
  128. scale_1 = DIV32_16(SHL32(EXTEND32(SIG_SCALING),3),scale);
  129. for (i=0;i<len;i++)
  130. {
  131. y[i] = PSHR32(MULT16_16(scale_1, SHL16(x[i],2)),6);
  132. }
  133. }
  134. }
  135. #else
  136. void signal_mul(const spx_sig_t *x, spx_sig_t *y, spx_word32_t scale, int len)
  137. {
  138. int i;
  139. for (i=0;i<len;i++)
  140. y[i] = scale*x[i];
  141. }
  142. void signal_div(const spx_sig_t *x, spx_sig_t *y, spx_word32_t scale, int len)
  143. {
  144. int i;
  145. float scale_1 = 1/scale;
  146. for (i=0;i<len;i++)
  147. y[i] = scale_1*x[i];
  148. }
  149. #endif
  150. #ifdef FIXED_POINT
  151. spx_word16_t compute_rms(const spx_sig_t *x, int len)
  152. {
  153. int i;
  154. spx_word32_t sum=0;
  155. spx_sig_t max_val=1;
  156. int sig_shift;
  157. for (i=0;i<len;i++)
  158. {
  159. spx_sig_t tmp = x[i];
  160. if (tmp<0)
  161. tmp = -tmp;
  162. if (tmp > max_val)
  163. max_val = tmp;
  164. }
  165. sig_shift=0;
  166. while (max_val>16383)
  167. {
  168. sig_shift++;
  169. max_val >>= 1;
  170. }
  171. for (i=0;i<len;i+=4)
  172. {
  173. spx_word32_t sum2=0;
  174. spx_word16_t tmp;
  175. tmp = EXTRACT16(SHR32(x[i],sig_shift));
  176. sum2 = MAC16_16(sum2,tmp,tmp);
  177. tmp = EXTRACT16(SHR32(x[i+1],sig_shift));
  178. sum2 = MAC16_16(sum2,tmp,tmp);
  179. tmp = EXTRACT16(SHR32(x[i+2],sig_shift));
  180. sum2 = MAC16_16(sum2,tmp,tmp);
  181. tmp = EXTRACT16(SHR32(x[i+3],sig_shift));
  182. sum2 = MAC16_16(sum2,tmp,tmp);
  183. sum = ADD32(sum,SHR32(sum2,6));
  184. }
  185. return EXTRACT16(PSHR32(SHL32(EXTEND32(spx_sqrt(DIV32(sum,len))),(sig_shift+3)),SIG_SHIFT));
  186. }
  187. spx_word16_t compute_rms16(const spx_word16_t *x, int len)
  188. {
  189. int i;
  190. spx_word16_t max_val=10;
  191. for (i=0;i<len;i++)
  192. {
  193. spx_sig_t tmp = x[i];
  194. if (tmp<0)
  195. tmp = -tmp;
  196. if (tmp > max_val)
  197. max_val = tmp;
  198. }
  199. if (max_val>16383)
  200. {
  201. spx_word32_t sum=0;
  202. for (i=0;i<len;i+=4)
  203. {
  204. spx_word32_t sum2=0;
  205. sum2 = MAC16_16(sum2,SHR16(x[i],1),SHR16(x[i],1));
  206. sum2 = MAC16_16(sum2,SHR16(x[i+1],1),SHR16(x[i+1],1));
  207. sum2 = MAC16_16(sum2,SHR16(x[i+2],1),SHR16(x[i+2],1));
  208. sum2 = MAC16_16(sum2,SHR16(x[i+3],1),SHR16(x[i+3],1));
  209. sum = ADD32(sum,SHR32(sum2,6));
  210. }
  211. return SHL16(spx_sqrt(DIV32(sum,len)),4);
  212. } else {
  213. spx_word32_t sum=0;
  214. int sig_shift=0;
  215. if (max_val < 8192)
  216. sig_shift=1;
  217. if (max_val < 4096)
  218. sig_shift=2;
  219. if (max_val < 2048)
  220. sig_shift=3;
  221. for (i=0;i<len;i+=4)
  222. {
  223. spx_word32_t sum2=0;
  224. sum2 = MAC16_16(sum2,SHL16(x[i],sig_shift),SHL16(x[i],sig_shift));
  225. sum2 = MAC16_16(sum2,SHL16(x[i+1],sig_shift),SHL16(x[i+1],sig_shift));
  226. sum2 = MAC16_16(sum2,SHL16(x[i+2],sig_shift),SHL16(x[i+2],sig_shift));
  227. sum2 = MAC16_16(sum2,SHL16(x[i+3],sig_shift),SHL16(x[i+3],sig_shift));
  228. sum = ADD32(sum,SHR32(sum2,6));
  229. }
  230. return SHL16(spx_sqrt(DIV32(sum,len)),3-sig_shift);
  231. }
  232. }
  233. #ifndef OVERRIDE_NORMALIZE16
  234. int normalize16(const spx_sig_t *x, spx_word16_t *y, spx_sig_t max_scale, int len)
  235. {
  236. int i;
  237. spx_sig_t max_val=1;
  238. int sig_shift;
  239. for (i=0;i<len;i++)
  240. {
  241. spx_sig_t tmp = x[i];
  242. if (tmp<0)
  243. tmp = NEG32(tmp);
  244. if (tmp >= max_val)
  245. max_val = tmp;
  246. }
  247. sig_shift=0;
  248. while (max_val>max_scale)
  249. {
  250. sig_shift++;
  251. max_val >>= 1;
  252. }
  253. for (i=0;i<len;i++)
  254. y[i] = EXTRACT16(SHR32(x[i], sig_shift));
  255. return sig_shift;
  256. }
  257. #endif
  258. #else
  259. spx_word16_t compute_rms(const spx_sig_t *x, int len)
  260. {
  261. int i;
  262. float sum=0;
  263. for (i=0;i<len;i++)
  264. {
  265. sum += x[i]*x[i];
  266. }
  267. return sqrt(.1+sum/len);
  268. }
  269. spx_word16_t compute_rms16(const spx_word16_t *x, int len)
  270. {
  271. return compute_rms(x, len);
  272. }
  273. #endif
  274. #ifndef OVERRIDE_FILTER_MEM16
  275. void filter_mem16(const spx_word16_t *x, const spx_coef_t *num, const spx_coef_t *den, spx_word16_t *y, int N, int ord, spx_mem_t *mem, char *stack)
  276. {
  277. int i,j;
  278. spx_word16_t xi,yi,nyi;
  279. for (i=0;i<N;i++)
  280. {
  281. xi= x[i];
  282. yi = EXTRACT16(SATURATE(ADD32(EXTEND32(x[i]),PSHR32(mem[0],LPC_SHIFT)),32767));
  283. nyi = NEG16(yi);
  284. for (j=0;j<ord-1;j++)
  285. {
  286. mem[j] = MAC16_16(MAC16_16(mem[j+1], num[j],xi), den[j],nyi);
  287. }
  288. mem[ord-1] = ADD32(MULT16_16(num[ord-1],xi), MULT16_16(den[ord-1],nyi));
  289. y[i] = yi;
  290. }
  291. }
  292. #endif
  293. #ifndef OVERRIDE_IIR_MEM16
  294. void iir_mem16(const spx_word16_t *x, const spx_coef_t *den, spx_word16_t *y, int N, int ord, spx_mem_t *mem, char *stack)
  295. {
  296. int i,j;
  297. spx_word16_t yi,nyi;
  298. for (i=0;i<N;i++)
  299. {
  300. yi = EXTRACT16(SATURATE(ADD32(EXTEND32(x[i]),PSHR32(mem[0],LPC_SHIFT)),32767));
  301. nyi = NEG16(yi);
  302. for (j=0;j<ord-1;j++)
  303. {
  304. mem[j] = MAC16_16(mem[j+1],den[j],nyi);
  305. }
  306. mem[ord-1] = MULT16_16(den[ord-1],nyi);
  307. y[i] = yi;
  308. }
  309. }
  310. #endif
  311. #ifndef OVERRIDE_FIR_MEM16
  312. void fir_mem16(const spx_word16_t *x, const spx_coef_t *num, spx_word16_t *y, int N, int ord, spx_mem_t *mem, char *stack)
  313. {
  314. int i,j;
  315. spx_word16_t xi,yi;
  316. for (i=0;i<N;i++)
  317. {
  318. xi=x[i];
  319. yi = EXTRACT16(SATURATE(ADD32(EXTEND32(x[i]),PSHR32(mem[0],LPC_SHIFT)),32767));
  320. for (j=0;j<ord-1;j++)
  321. {
  322. mem[j] = MAC16_16(mem[j+1], num[j],xi);
  323. }
  324. mem[ord-1] = MULT16_16(num[ord-1],xi);
  325. y[i] = yi;
  326. }
  327. }
  328. #endif
  329. void syn_percep_zero16(const spx_word16_t *xx, const spx_coef_t *ak, const spx_coef_t *awk1, const spx_coef_t *awk2, spx_word16_t *y, int N, int ord, char *stack)
  330. {
  331. int i;
  332. VARDECL(spx_mem_t *mem);
  333. ALLOC(mem, ord, spx_mem_t);
  334. for (i=0;i<ord;i++)
  335. mem[i]=0;
  336. iir_mem16(xx, ak, y, N, ord, mem, stack);
  337. for (i=0;i<ord;i++)
  338. mem[i]=0;
  339. filter_mem16(y, awk1, awk2, y, N, ord, mem, stack);
  340. }
  341. void residue_percep_zero16(const spx_word16_t *xx, const spx_coef_t *ak, const spx_coef_t *awk1, const spx_coef_t *awk2, spx_word16_t *y, int N, int ord, char *stack)
  342. {
  343. int i;
  344. VARDECL(spx_mem_t *mem);
  345. ALLOC(mem, ord, spx_mem_t);
  346. for (i=0;i<ord;i++)
  347. mem[i]=0;
  348. filter_mem16(xx, ak, awk1, y, N, ord, mem, stack);
  349. for (i=0;i<ord;i++)
  350. mem[i]=0;
  351. fir_mem16(y, awk2, y, N, ord, mem, stack);
  352. }
  353. #ifndef OVERRIDE_COMPUTE_IMPULSE_RESPONSE
  354. void compute_impulse_response(const spx_coef_t *ak, const spx_coef_t *awk1, const spx_coef_t *awk2, spx_word16_t *y, int N, int ord, char *stack)
  355. {
  356. int i,j;
  357. spx_word16_t y1, ny1i, ny2i;
  358. VARDECL(spx_mem_t *mem1);
  359. VARDECL(spx_mem_t *mem2);
  360. ALLOC(mem1, ord, spx_mem_t);
  361. ALLOC(mem2, ord, spx_mem_t);
  362. y[0] = LPC_SCALING;
  363. for (i=0;i<ord;i++)
  364. y[i+1] = awk1[i];
  365. i++;
  366. for (;i<N;i++)
  367. y[i] = VERY_SMALL;
  368. for (i=0;i<ord;i++)
  369. mem1[i] = mem2[i] = 0;
  370. for (i=0;i<N;i++)
  371. {
  372. y1 = ADD16(y[i], EXTRACT16(PSHR32(mem1[0],LPC_SHIFT)));
  373. ny1i = NEG16(y1);
  374. y[i] = PSHR32(ADD32(SHL32(EXTEND32(y1),LPC_SHIFT+1),mem2[0]),LPC_SHIFT);
  375. ny2i = NEG16(y[i]);
  376. for (j=0;j<ord-1;j++)
  377. {
  378. mem1[j] = MAC16_16(mem1[j+1], awk2[j],ny1i);
  379. mem2[j] = MAC16_16(mem2[j+1], ak[j],ny2i);
  380. }
  381. mem1[ord-1] = MULT16_16(awk2[ord-1],ny1i);
  382. mem2[ord-1] = MULT16_16(ak[ord-1],ny2i);
  383. }
  384. }
  385. #endif
  386. /* Decomposes a signal into low-band and high-band using a QMF */
  387. void qmf_decomp(const spx_word16_t *xx, const spx_word16_t *aa, spx_word16_t *y1, spx_word16_t *y2, int N, int M, spx_word16_t *mem, char *stack)
  388. {
  389. int i,j,k,M2;
  390. VARDECL(spx_word16_t *a);
  391. VARDECL(spx_word16_t *x);
  392. spx_word16_t *x2;
  393. ALLOC(a, M, spx_word16_t);
  394. ALLOC(x, N+M-1, spx_word16_t);
  395. x2=x+M-1;
  396. M2=M>>1;
  397. for (i=0;i<M;i++)
  398. a[M-i-1]= aa[i];
  399. for (i=0;i<M-1;i++)
  400. x[i]=mem[M-i-2];
  401. for (i=0;i<N;i++)
  402. x[i+M-1]=SHR16(xx[i],1);
  403. for (i=0;i<M-1;i++)
  404. mem[i]=SHR16(xx[N-i-1],1);
  405. for (i=0,k=0;i<N;i+=2,k++)
  406. {
  407. spx_word32_t y1k=0, y2k=0;
  408. for (j=0;j<M2;j++)
  409. {
  410. y1k=ADD32(y1k,MULT16_16(a[j],ADD16(x[i+j],x2[i-j])));
  411. y2k=SUB32(y2k,MULT16_16(a[j],SUB16(x[i+j],x2[i-j])));
  412. j++;
  413. y1k=ADD32(y1k,MULT16_16(a[j],ADD16(x[i+j],x2[i-j])));
  414. y2k=ADD32(y2k,MULT16_16(a[j],SUB16(x[i+j],x2[i-j])));
  415. }
  416. y1[k] = EXTRACT16(SATURATE(PSHR32(y1k,15),32767));
  417. y2[k] = EXTRACT16(SATURATE(PSHR32(y2k,15),32767));
  418. }
  419. }
  420. /* Re-synthesised a signal from the QMF low-band and high-band signals */
  421. void qmf_synth(const spx_word16_t *x1, const spx_word16_t *x2, const spx_word16_t *a, spx_word16_t *y, int N, int M, spx_word16_t *mem1, spx_word16_t *mem2, char *stack)
  422. /* assumptions:
  423. all odd x[i] are zero -- well, actually they are left out of the array now
  424. N and M are multiples of 4 */
  425. {
  426. int i, j;
  427. int M2, N2;
  428. VARDECL(spx_word16_t *xx1);
  429. VARDECL(spx_word16_t *xx2);
  430. M2 = M>>1;
  431. N2 = N>>1;
  432. ALLOC(xx1, M2+N2, spx_word16_t);
  433. ALLOC(xx2, M2+N2, spx_word16_t);
  434. for (i = 0; i < N2; i++)
  435. xx1[i] = x1[N2-1-i];
  436. for (i = 0; i < M2; i++)
  437. xx1[N2+i] = mem1[2*i+1];
  438. for (i = 0; i < N2; i++)
  439. xx2[i] = x2[N2-1-i];
  440. for (i = 0; i < M2; i++)
  441. xx2[N2+i] = mem2[2*i+1];
  442. for (i = 0; i < N2; i += 2) {
  443. spx_sig_t y0, y1, y2, y3;
  444. spx_word16_t x10, x20;
  445. y0 = y1 = y2 = y3 = 0;
  446. x10 = xx1[N2-2-i];
  447. x20 = xx2[N2-2-i];
  448. for (j = 0; j < M2; j += 2) {
  449. spx_word16_t x11, x21;
  450. spx_word16_t a0, a1;
  451. a0 = a[2*j];
  452. a1 = a[2*j+1];
  453. x11 = xx1[N2-1+j-i];
  454. x21 = xx2[N2-1+j-i];
  455. #ifdef FIXED_POINT
  456. /* We multiply twice by the same coef to avoid overflows */
  457. y0 = MAC16_16(MAC16_16(y0, a0, x11), NEG16(a0), x21);
  458. y1 = MAC16_16(MAC16_16(y1, a1, x11), a1, x21);
  459. y2 = MAC16_16(MAC16_16(y2, a0, x10), NEG16(a0), x20);
  460. y3 = MAC16_16(MAC16_16(y3, a1, x10), a1, x20);
  461. #else
  462. y0 = ADD32(y0,MULT16_16(a0, x11-x21));
  463. y1 = ADD32(y1,MULT16_16(a1, x11+x21));
  464. y2 = ADD32(y2,MULT16_16(a0, x10-x20));
  465. y3 = ADD32(y3,MULT16_16(a1, x10+x20));
  466. #endif
  467. a0 = a[2*j+2];
  468. a1 = a[2*j+3];
  469. x10 = xx1[N2+j-i];
  470. x20 = xx2[N2+j-i];
  471. #ifdef FIXED_POINT
  472. /* We multiply twice by the same coef to avoid overflows */
  473. y0 = MAC16_16(MAC16_16(y0, a0, x10), NEG16(a0), x20);
  474. y1 = MAC16_16(MAC16_16(y1, a1, x10), a1, x20);
  475. y2 = MAC16_16(MAC16_16(y2, a0, x11), NEG16(a0), x21);
  476. y3 = MAC16_16(MAC16_16(y3, a1, x11), a1, x21);
  477. #else
  478. y0 = ADD32(y0,MULT16_16(a0, x10-x20));
  479. y1 = ADD32(y1,MULT16_16(a1, x10+x20));
  480. y2 = ADD32(y2,MULT16_16(a0, x11-x21));
  481. y3 = ADD32(y3,MULT16_16(a1, x11+x21));
  482. #endif
  483. }
  484. #ifdef FIXED_POINT
  485. y[2*i] = EXTRACT16(SATURATE32(PSHR32(y0,15),32767));
  486. y[2*i+1] = EXTRACT16(SATURATE32(PSHR32(y1,15),32767));
  487. y[2*i+2] = EXTRACT16(SATURATE32(PSHR32(y2,15),32767));
  488. y[2*i+3] = EXTRACT16(SATURATE32(PSHR32(y3,15),32767));
  489. #else
  490. /* Normalize up explicitly if we're in float */
  491. y[2*i] = 2.f*y0;
  492. y[2*i+1] = 2.f*y1;
  493. y[2*i+2] = 2.f*y2;
  494. y[2*i+3] = 2.f*y3;
  495. #endif
  496. }
  497. for (i = 0; i < M2; i++)
  498. mem1[2*i+1] = xx1[i];
  499. for (i = 0; i < M2; i++)
  500. mem2[2*i+1] = xx2[i];
  501. }
  502. #ifdef FIXED_POINT
  503. #if 0
  504. const spx_word16_t shift_filt[3][7] = {{-33, 1043, -4551, 19959, 19959, -4551, 1043},
  505. {-98, 1133, -4425, 29179, 8895, -2328, 444},
  506. {444, -2328, 8895, 29179, -4425, 1133, -98}};
  507. #else
  508. const spx_word16_t shift_filt[3][7] = {{-390, 1540, -4993, 20123, 20123, -4993, 1540},
  509. {-1064, 2817, -6694, 31589, 6837, -990, -209},
  510. {-209, -990, 6837, 31589, -6694, 2817, -1064}};
  511. #endif
  512. #else
  513. #if 0
  514. const float shift_filt[3][7] = {{-9.9369e-04, 3.1831e-02, -1.3889e-01, 6.0910e-01, 6.0910e-01, -1.3889e-01, 3.1831e-02},
  515. {-0.0029937, 0.0345613, -0.1350474, 0.8904793, 0.2714479, -0.0710304, 0.0135403},
  516. {0.0135403, -0.0710304, 0.2714479, 0.8904793, -0.1350474, 0.0345613, -0.0029937}};
  517. #else
  518. const float shift_filt[3][7] = {{-0.011915f, 0.046995f, -0.152373f, 0.614108f, 0.614108f, -0.152373f, 0.046995f},
  519. {-0.0324855f, 0.0859768f, -0.2042986f, 0.9640297f, 0.2086420f, -0.0302054f, -0.0063646f},
  520. {-0.0063646f, -0.0302054f, 0.2086420f, 0.9640297f, -0.2042986f, 0.0859768f, -0.0324855f}};
  521. #endif
  522. #endif
  523. int interp_pitch(
  524. spx_word16_t *exc, /*decoded excitation*/
  525. spx_word16_t *interp, /*decoded excitation*/
  526. int pitch, /*pitch period*/
  527. int len
  528. )
  529. {
  530. int i,j,k;
  531. spx_word32_t corr[4][7];
  532. spx_word32_t maxcorr;
  533. int maxi, maxj;
  534. for (i=0;i<7;i++)
  535. {
  536. corr[0][i] = inner_prod(exc, exc-pitch-3+i, len);
  537. }
  538. for (i=0;i<3;i++)
  539. {
  540. for (j=0;j<7;j++)
  541. {
  542. int i1, i2;
  543. spx_word32_t tmp=0;
  544. i1 = 3-j;
  545. if (i1<0)
  546. i1 = 0;
  547. i2 = 10-j;
  548. if (i2>7)
  549. i2 = 7;
  550. for (k=i1;k<i2;k++)
  551. tmp += MULT16_32_Q15(shift_filt[i][k],corr[0][j+k-3]);
  552. corr[i+1][j] = tmp;
  553. }
  554. }
  555. maxi=maxj=0;
  556. maxcorr = corr[0][0];
  557. for (i=0;i<4;i++)
  558. {
  559. for (j=0;j<7;j++)
  560. {
  561. if (corr[i][j] > maxcorr)
  562. {
  563. maxcorr = corr[i][j];
  564. maxi=i;
  565. maxj=j;
  566. }
  567. }
  568. }
  569. for (i=0;i<len;i++)
  570. {
  571. spx_word32_t tmp = 0;
  572. if (maxi>0)
  573. {
  574. for (k=0;k<7;k++)
  575. {
  576. tmp += MULT16_16(exc[i-(pitch-maxj+3)+k-3],shift_filt[maxi-1][k]);
  577. }
  578. } else {
  579. tmp = SHL32(exc[i-(pitch-maxj+3)],15);
  580. }
  581. interp[i] = PSHR32(tmp,15);
  582. }
  583. return pitch-maxj+3;
  584. }
  585. void multicomb(
  586. spx_word16_t *exc, /*decoded excitation*/
  587. spx_word16_t *new_exc, /*enhanced excitation*/
  588. spx_coef_t *ak, /*LPC filter coefs*/
  589. int p, /*LPC order*/
  590. int nsf, /*sub-frame size*/
  591. int pitch, /*pitch period*/
  592. int max_pitch,
  593. spx_word16_t comb_gain, /*gain of comb filter*/
  594. char *stack
  595. )
  596. {
  597. int i;
  598. VARDECL(spx_word16_t *iexc);
  599. spx_word16_t old_ener, new_ener;
  600. int corr_pitch;
  601. spx_word16_t iexc0_mag, iexc1_mag, exc_mag;
  602. spx_word32_t corr0, corr1;
  603. spx_word16_t gain0, gain1;
  604. spx_word16_t pgain1, pgain2;
  605. spx_word16_t c1, c2;
  606. spx_word16_t g1, g2;
  607. spx_word16_t ngain;
  608. spx_word16_t gg1, gg2;
  609. #ifdef FIXED_POINT
  610. int scaledown=0;
  611. #endif
  612. #if 0 /* Set to 1 to enable full pitch search */
  613. int nol_pitch[6];
  614. spx_word16_t nol_pitch_coef[6];
  615. spx_word16_t ol_pitch_coef;
  616. open_loop_nbest_pitch(exc, 20, 120, nsf,
  617. nol_pitch, nol_pitch_coef, 6, stack);
  618. corr_pitch=nol_pitch[0];
  619. ol_pitch_coef = nol_pitch_coef[0];
  620. /*Try to remove pitch multiples*/
  621. for (i=1;i<6;i++)
  622. {
  623. #ifdef FIXED_POINT
  624. if ((nol_pitch_coef[i]>MULT16_16_Q15(nol_pitch_coef[0],19661)) &&
  625. #else
  626. if ((nol_pitch_coef[i]>.6*nol_pitch_coef[0]) &&
  627. #endif
  628. (ABS(2*nol_pitch[i]-corr_pitch)<=2 || ABS(3*nol_pitch[i]-corr_pitch)<=3 ||
  629. ABS(4*nol_pitch[i]-corr_pitch)<=4 || ABS(5*nol_pitch[i]-corr_pitch)<=5))
  630. {
  631. corr_pitch = nol_pitch[i];
  632. }
  633. }
  634. #else
  635. corr_pitch = pitch;
  636. #endif
  637. ALLOC(iexc, 2*nsf, spx_word16_t);
  638. interp_pitch(exc, iexc, corr_pitch, 80);
  639. if (corr_pitch>max_pitch)
  640. interp_pitch(exc, iexc+nsf, 2*corr_pitch, 80);
  641. else
  642. interp_pitch(exc, iexc+nsf, -corr_pitch, 80);
  643. #ifdef FIXED_POINT
  644. for (i=0;i<nsf;i++)
  645. {
  646. if (ABS16(exc[i])>16383)
  647. {
  648. scaledown = 1;
  649. break;
  650. }
  651. }
  652. if (scaledown)
  653. {
  654. for (i=0;i<nsf;i++)
  655. exc[i] = SHR16(exc[i],1);
  656. for (i=0;i<2*nsf;i++)
  657. iexc[i] = SHR16(iexc[i],1);
  658. }
  659. #endif
  660. /*interp_pitch(exc, iexc+2*nsf, 2*corr_pitch, 80);*/
  661. /*printf ("%d %d %f\n", pitch, corr_pitch, max_corr*ener_1);*/
  662. iexc0_mag = spx_sqrt(1000+inner_prod(iexc,iexc,nsf));
  663. iexc1_mag = spx_sqrt(1000+inner_prod(iexc+nsf,iexc+nsf,nsf));
  664. exc_mag = spx_sqrt(1+inner_prod(exc,exc,nsf));
  665. corr0 = inner_prod(iexc,exc,nsf);
  666. if (corr0<0)
  667. corr0=0;
  668. corr1 = inner_prod(iexc+nsf,exc,nsf);
  669. if (corr1<0)
  670. corr1=0;
  671. #ifdef FIXED_POINT
  672. /* Doesn't cost much to limit the ratio and it makes the rest easier */
  673. if (SHL32(EXTEND32(iexc0_mag),6) < EXTEND32(exc_mag))
  674. iexc0_mag = ADD16(1,PSHR16(exc_mag,6));
  675. if (SHL32(EXTEND32(iexc1_mag),6) < EXTEND32(exc_mag))
  676. iexc1_mag = ADD16(1,PSHR16(exc_mag,6));
  677. #endif
  678. if (corr0 > MULT16_16(iexc0_mag,exc_mag))
  679. pgain1 = QCONST16(1., 14);
  680. else
  681. pgain1 = PDIV32_16(SHL32(PDIV32(corr0, exc_mag),14),iexc0_mag);
  682. if (corr1 > MULT16_16(iexc1_mag,exc_mag))
  683. pgain2 = QCONST16(1., 14);
  684. else
  685. pgain2 = PDIV32_16(SHL32(PDIV32(corr1, exc_mag),14),iexc1_mag);
  686. gg1 = PDIV32_16(SHL32(EXTEND32(exc_mag),8), iexc0_mag);
  687. gg2 = PDIV32_16(SHL32(EXTEND32(exc_mag),8), iexc1_mag);
  688. if (comb_gain>0)
  689. {
  690. #ifdef FIXED_POINT
  691. c1 = (MULT16_16_Q15(QCONST16(.4,15),comb_gain)+QCONST16(.07,15));
  692. c2 = QCONST16(.5,15)+MULT16_16_Q14(QCONST16(1.72,14),(c1-QCONST16(.07,15)));
  693. #else
  694. c1 = .4*comb_gain+.07;
  695. c2 = .5+1.72*(c1-.07);
  696. #endif
  697. } else
  698. {
  699. c1=c2=0;
  700. }
  701. #ifdef FIXED_POINT
  702. g1 = 32767 - MULT16_16_Q13(MULT16_16_Q15(c2, pgain1),pgain1);
  703. g2 = 32767 - MULT16_16_Q13(MULT16_16_Q15(c2, pgain2),pgain2);
  704. #else
  705. g1 = 1-c2*pgain1*pgain1;
  706. g2 = 1-c2*pgain2*pgain2;
  707. #endif
  708. if (g1<c1)
  709. g1 = c1;
  710. if (g2<c1)
  711. g2 = c1;
  712. g1 = (spx_word16_t)PDIV32_16(SHL32(EXTEND32(c1),14),(spx_word16_t)g1);
  713. g2 = (spx_word16_t)PDIV32_16(SHL32(EXTEND32(c1),14),(spx_word16_t)g2);
  714. if (corr_pitch>max_pitch)
  715. {
  716. gain0 = MULT16_16_Q15(QCONST16(.7,15),MULT16_16_Q14(g1,gg1));
  717. gain1 = MULT16_16_Q15(QCONST16(.3,15),MULT16_16_Q14(g2,gg2));
  718. } else {
  719. gain0 = MULT16_16_Q15(QCONST16(.6,15),MULT16_16_Q14(g1,gg1));
  720. gain1 = MULT16_16_Q15(QCONST16(.6,15),MULT16_16_Q14(g2,gg2));
  721. }
  722. for (i=0;i<nsf;i++)
  723. new_exc[i] = ADD16(exc[i], EXTRACT16(PSHR32(ADD32(MULT16_16(gain0,iexc[i]), MULT16_16(gain1,iexc[i+nsf])),8)));
  724. /* FIXME: compute_rms16 is currently not quite accurate enough (but close) */
  725. new_ener = compute_rms16(new_exc, nsf);
  726. old_ener = compute_rms16(exc, nsf);
  727. if (old_ener < 1)
  728. old_ener = 1;
  729. if (new_ener < 1)
  730. new_ener = 1;
  731. if (old_ener > new_ener)
  732. old_ener = new_ener;
  733. ngain = PDIV32_16(SHL32(EXTEND32(old_ener),14),new_ener);
  734. for (i=0;i<nsf;i++)
  735. new_exc[i] = MULT16_16_Q14(ngain, new_exc[i]);
  736. #ifdef FIXED_POINT
  737. if (scaledown)
  738. {
  739. for (i=0;i<nsf;i++)
  740. exc[i] = SHL16(exc[i],1);
  741. for (i=0;i<nsf;i++)
  742. new_exc[i] = SHL16(SATURATE16(new_exc[i],16383),1);
  743. }
  744. #endif
  745. }