filters.c 24 KB

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