ltp.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /* Copyright (C) 2002-2006 Jean-Marc Valin
  2. File: ltp.c
  3. Long-Term Prediction functions
  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 <math.h>
  31. #include "ltp.h"
  32. #include "stack_alloc.h"
  33. #include "filters.h"
  34. #include "math_approx.h"
  35. #include "os_support.h"
  36. #ifndef NULL
  37. #define NULL 0
  38. #endif
  39. #ifdef _USE_SSE
  40. #include "ltp_sse.h"
  41. #elif defined (ARM4_ASM) || defined(ARM5E_ASM)
  42. #include "ltp_arm4.h"
  43. #elif defined (BFIN_ASM)
  44. #include "ltp_bfin.h"
  45. #endif
  46. #ifndef OVERRIDE_INNER_PROD
  47. spx_word32_t inner_prod(const spx_word16_t *x, const spx_word16_t *y, int len)
  48. {
  49. spx_word32_t sum=0;
  50. len >>= 2;
  51. while(len--)
  52. {
  53. spx_word32_t part=0;
  54. part = MAC16_16(part,*x++,*y++);
  55. part = MAC16_16(part,*x++,*y++);
  56. part = MAC16_16(part,*x++,*y++);
  57. part = MAC16_16(part,*x++,*y++);
  58. /* HINT: If you had a 40-bit accumulator, you could shift only at the end */
  59. sum = ADD32(sum,SHR32(part,6));
  60. }
  61. return sum;
  62. }
  63. #endif
  64. #ifndef DISABLE_ENCODER
  65. #ifndef OVERRIDE_PITCH_XCORR
  66. #if 0 /* HINT: Enable this for machines with enough registers (i.e. not x86) */
  67. static void pitch_xcorr(const spx_word16_t *_x, const spx_word16_t *_y, spx_word32_t *corr, int len, int nb_pitch, char *stack)
  68. {
  69. int i,j;
  70. for (i=0;i<nb_pitch;i+=4)
  71. {
  72. /* Compute correlation*/
  73. /*corr[nb_pitch-1-i]=inner_prod(x, _y+i, len);*/
  74. spx_word32_t sum1=0;
  75. spx_word32_t sum2=0;
  76. spx_word32_t sum3=0;
  77. spx_word32_t sum4=0;
  78. const spx_word16_t *y = _y+i;
  79. const spx_word16_t *x = _x;
  80. spx_word16_t y0, y1, y2, y3;
  81. /*y0=y[0];y1=y[1];y2=y[2];y3=y[3];*/
  82. y0=*y++;
  83. y1=*y++;
  84. y2=*y++;
  85. y3=*y++;
  86. for (j=0;j<len;j+=4)
  87. {
  88. spx_word32_t part1;
  89. spx_word32_t part2;
  90. spx_word32_t part3;
  91. spx_word32_t part4;
  92. part1 = MULT16_16(*x,y0);
  93. part2 = MULT16_16(*x,y1);
  94. part3 = MULT16_16(*x,y2);
  95. part4 = MULT16_16(*x,y3);
  96. x++;
  97. y0=*y++;
  98. part1 = MAC16_16(part1,*x,y1);
  99. part2 = MAC16_16(part2,*x,y2);
  100. part3 = MAC16_16(part3,*x,y3);
  101. part4 = MAC16_16(part4,*x,y0);
  102. x++;
  103. y1=*y++;
  104. part1 = MAC16_16(part1,*x,y2);
  105. part2 = MAC16_16(part2,*x,y3);
  106. part3 = MAC16_16(part3,*x,y0);
  107. part4 = MAC16_16(part4,*x,y1);
  108. x++;
  109. y2=*y++;
  110. part1 = MAC16_16(part1,*x,y3);
  111. part2 = MAC16_16(part2,*x,y0);
  112. part3 = MAC16_16(part3,*x,y1);
  113. part4 = MAC16_16(part4,*x,y2);
  114. x++;
  115. y3=*y++;
  116. sum1 = ADD32(sum1,SHR32(part1,6));
  117. sum2 = ADD32(sum2,SHR32(part2,6));
  118. sum3 = ADD32(sum3,SHR32(part3,6));
  119. sum4 = ADD32(sum4,SHR32(part4,6));
  120. }
  121. corr[nb_pitch-1-i]=sum1;
  122. corr[nb_pitch-2-i]=sum2;
  123. corr[nb_pitch-3-i]=sum3;
  124. corr[nb_pitch-4-i]=sum4;
  125. }
  126. }
  127. #else
  128. static void pitch_xcorr(const spx_word16_t *_x, const spx_word16_t *_y, spx_word32_t *corr, int len, int nb_pitch, char *stack)
  129. {
  130. int i;
  131. for (i=0;i<nb_pitch;i++)
  132. {
  133. /* Compute correlation*/
  134. corr[nb_pitch-1-i]=inner_prod(_x, _y+i, len);
  135. }
  136. }
  137. #endif
  138. #endif
  139. #ifndef OVERRIDE_COMPUTE_PITCH_ERROR
  140. static inline spx_word32_t compute_pitch_error(spx_word16_t *C, spx_word16_t *g, spx_word16_t pitch_control)
  141. {
  142. spx_word32_t sum = 0;
  143. sum = ADD32(sum,MULT16_16(MULT16_16_16(g[0],pitch_control),C[0]));
  144. sum = ADD32(sum,MULT16_16(MULT16_16_16(g[1],pitch_control),C[1]));
  145. sum = ADD32(sum,MULT16_16(MULT16_16_16(g[2],pitch_control),C[2]));
  146. sum = SUB32(sum,MULT16_16(MULT16_16_16(g[0],g[1]),C[3]));
  147. sum = SUB32(sum,MULT16_16(MULT16_16_16(g[2],g[1]),C[4]));
  148. sum = SUB32(sum,MULT16_16(MULT16_16_16(g[2],g[0]),C[5]));
  149. sum = SUB32(sum,MULT16_16(MULT16_16_16(g[0],g[0]),C[6]));
  150. sum = SUB32(sum,MULT16_16(MULT16_16_16(g[1],g[1]),C[7]));
  151. sum = SUB32(sum,MULT16_16(MULT16_16_16(g[2],g[2]),C[8]));
  152. return sum;
  153. }
  154. #endif
  155. #ifndef OVERRIDE_OPEN_LOOP_NBEST_PITCH
  156. void open_loop_nbest_pitch(spx_word16_t *sw, int start, int end, int len, int *pitch, spx_word16_t *gain, int N, char *stack)
  157. {
  158. int i,j,k;
  159. VARDECL(spx_word32_t *best_score);
  160. VARDECL(spx_word32_t *best_ener);
  161. spx_word32_t e0;
  162. VARDECL(spx_word32_t *corr);
  163. #ifdef FIXED_POINT
  164. /* In fixed-point, we need only one (temporary) array of 32-bit values and two (corr16, ener16)
  165. arrays for (normalized) 16-bit values */
  166. VARDECL(spx_word16_t *corr16);
  167. VARDECL(spx_word16_t *ener16);
  168. spx_word32_t *energy;
  169. int cshift=0, eshift=0;
  170. int scaledown = 0;
  171. ALLOC(corr16, end-start+1, spx_word16_t);
  172. ALLOC(ener16, end-start+1, spx_word16_t);
  173. ALLOC(corr, end-start+1, spx_word32_t);
  174. energy = corr;
  175. #else
  176. /* In floating-point, we need to float arrays and no normalized copies */
  177. VARDECL(spx_word32_t *energy);
  178. spx_word16_t *corr16;
  179. spx_word16_t *ener16;
  180. ALLOC(energy, end-start+2, spx_word32_t);
  181. ALLOC(corr, end-start+1, spx_word32_t);
  182. corr16 = corr;
  183. ener16 = energy;
  184. #endif
  185. ALLOC(best_score, N, spx_word32_t);
  186. ALLOC(best_ener, N, spx_word32_t);
  187. for (i=0;i<N;i++)
  188. {
  189. best_score[i]=-1;
  190. best_ener[i]=0;
  191. pitch[i]=start;
  192. }
  193. #ifdef FIXED_POINT
  194. for (i=-end;i<len;i++)
  195. {
  196. if (ABS16(sw[i])>16383)
  197. {
  198. scaledown=1;
  199. break;
  200. }
  201. }
  202. /* If the weighted input is close to saturation, then we scale it down */
  203. if (scaledown)
  204. {
  205. for (i=-end;i<len;i++)
  206. {
  207. sw[i]=SHR16(sw[i],1);
  208. }
  209. }
  210. #endif
  211. energy[0]=inner_prod(sw-start, sw-start, len);
  212. e0=inner_prod(sw, sw, len);
  213. for (i=start;i<end;i++)
  214. {
  215. /* Update energy for next pitch*/
  216. energy[i-start+1] = SUB32(ADD32(energy[i-start],SHR32(MULT16_16(sw[-i-1],sw[-i-1]),6)), SHR32(MULT16_16(sw[-i+len-1],sw[-i+len-1]),6));
  217. if (energy[i-start+1] < 0)
  218. energy[i-start+1] = 0;
  219. }
  220. #ifdef FIXED_POINT
  221. eshift = normalize16(energy, ener16, 32766, end-start+1);
  222. #endif
  223. /* In fixed-point, this actually overrites the energy array (aliased to corr) */
  224. pitch_xcorr(sw, sw-end, corr, len, end-start+1, stack);
  225. #ifdef FIXED_POINT
  226. /* Normalize to 180 so we can square it and it still fits in 16 bits */
  227. cshift = normalize16(corr, corr16, 180, end-start+1);
  228. /* If we scaled weighted input down, we need to scale it up again (OK, so we've just lost the LSB, who cares?) */
  229. if (scaledown)
  230. {
  231. for (i=-end;i<len;i++)
  232. {
  233. sw[i]=SHL16(sw[i],1);
  234. }
  235. }
  236. #endif
  237. /* Search for the best pitch prediction gain */
  238. for (i=start;i<=end;i++)
  239. {
  240. spx_word16_t tmp = MULT16_16_16(corr16[i-start],corr16[i-start]);
  241. /* Instead of dividing the tmp by the energy, we multiply on the other side */
  242. if (MULT16_16(tmp,best_ener[N-1])>MULT16_16(best_score[N-1],ADD16(1,ener16[i-start])))
  243. {
  244. /* We can safely put it last and then check */
  245. best_score[N-1]=tmp;
  246. best_ener[N-1]=ener16[i-start]+1;
  247. pitch[N-1]=i;
  248. /* Check if it comes in front of others */
  249. for (j=0;j<N-1;j++)
  250. {
  251. if (MULT16_16(tmp,best_ener[j])>MULT16_16(best_score[j],ADD16(1,ener16[i-start])))
  252. {
  253. for (k=N-1;k>j;k--)
  254. {
  255. best_score[k]=best_score[k-1];
  256. best_ener[k]=best_ener[k-1];
  257. pitch[k]=pitch[k-1];
  258. }
  259. best_score[j]=tmp;
  260. best_ener[j]=ener16[i-start]+1;
  261. pitch[j]=i;
  262. break;
  263. }
  264. }
  265. }
  266. }
  267. /* Compute open-loop gain if necessary */
  268. if (gain)
  269. {
  270. for (j=0;j<N;j++)
  271. {
  272. spx_word16_t g;
  273. i=pitch[j];
  274. g = DIV32(SHL32(EXTEND32(corr16[i-start]),cshift), 10+SHR32(MULT16_16(spx_sqrt(e0),spx_sqrt(SHL32(EXTEND32(ener16[i-start]),eshift))),6));
  275. /* FIXME: g = max(g,corr/energy) */
  276. if (g<0)
  277. g = 0;
  278. gain[j]=g;
  279. }
  280. }
  281. }
  282. #endif
  283. #ifndef OVERRIDE_PITCH_GAIN_SEARCH_3TAP_VQ
  284. static int pitch_gain_search_3tap_vq(
  285. const signed char *gain_cdbk,
  286. int gain_cdbk_size,
  287. spx_word16_t *C16,
  288. spx_word16_t max_gain
  289. )
  290. {
  291. const signed char *ptr=gain_cdbk;
  292. int best_cdbk=0;
  293. spx_word32_t best_sum=-VERY_LARGE32;
  294. spx_word32_t sum=0;
  295. spx_word16_t g[3];
  296. spx_word16_t pitch_control=64;
  297. spx_word16_t gain_sum;
  298. int i;
  299. for (i=0;i<gain_cdbk_size;i++) {
  300. ptr = gain_cdbk+4*i;
  301. g[0]=ADD16((spx_word16_t)ptr[0],32);
  302. g[1]=ADD16((spx_word16_t)ptr[1],32);
  303. g[2]=ADD16((spx_word16_t)ptr[2],32);
  304. gain_sum = (spx_word16_t)ptr[3];
  305. sum = compute_pitch_error(C16, g, pitch_control);
  306. if (sum>best_sum && gain_sum<=max_gain) {
  307. best_sum=sum;
  308. best_cdbk=i;
  309. }
  310. }
  311. return best_cdbk;
  312. }
  313. #endif
  314. /** Finds the best quantized 3-tap pitch predictor by analysis by synthesis */
  315. static spx_word32_t pitch_gain_search_3tap(
  316. const spx_word16_t target[], /* Target vector */
  317. const spx_coef_t ak[], /* LPCs for this subframe */
  318. const spx_coef_t awk1[], /* Weighted LPCs #1 for this subframe */
  319. const spx_coef_t awk2[], /* Weighted LPCs #2 for this subframe */
  320. spx_sig_t exc[], /* Excitation */
  321. const signed char *gain_cdbk,
  322. int gain_cdbk_size,
  323. int pitch, /* Pitch value */
  324. int p, /* Number of LPC coeffs */
  325. int nsf, /* Number of samples in subframe */
  326. SpeexBits *bits,
  327. char *stack,
  328. const spx_word16_t *exc2,
  329. const spx_word16_t *r,
  330. spx_word16_t *new_target,
  331. int *cdbk_index,
  332. int plc_tuning,
  333. spx_word32_t cumul_gain,
  334. int scaledown
  335. )
  336. {
  337. int i,j;
  338. VARDECL(spx_word16_t *tmp1);
  339. VARDECL(spx_word16_t *e);
  340. spx_word16_t *x[3];
  341. spx_word32_t corr[3];
  342. spx_word32_t A[3][3];
  343. spx_word16_t gain[3];
  344. spx_word32_t err;
  345. spx_word16_t max_gain=128;
  346. int best_cdbk=0;
  347. ALLOC(tmp1, 3*nsf, spx_word16_t);
  348. ALLOC(e, nsf, spx_word16_t);
  349. if (cumul_gain > 262144)
  350. max_gain = 31;
  351. x[0]=tmp1;
  352. x[1]=tmp1+nsf;
  353. x[2]=tmp1+2*nsf;
  354. for (j=0;j<nsf;j++)
  355. new_target[j] = target[j];
  356. {
  357. int bound;
  358. VARDECL(spx_mem_t *mm);
  359. int pp=pitch-1;
  360. ALLOC(mm, p, spx_mem_t);
  361. bound = nsf;
  362. if (nsf-pp>0)
  363. bound = pp;
  364. for (j=0;j<bound;j++)
  365. e[j]=exc2[j-pp];
  366. bound = nsf;
  367. if (nsf-pp-pitch>0)
  368. bound = pp+pitch;
  369. for (;j<bound;j++)
  370. e[j]=exc2[j-pp-pitch];
  371. for (;j<nsf;j++)
  372. e[j]=0;
  373. #ifdef FIXED_POINT
  374. /* Scale target and excitation down if needed (avoiding overflow) */
  375. if (scaledown)
  376. {
  377. for (j=0;j<nsf;j++)
  378. e[j] = SHR16(e[j],1);
  379. for (j=0;j<nsf;j++)
  380. new_target[j] = SHR16(new_target[j],1);
  381. }
  382. #endif
  383. for (j=0;j<p;j++)
  384. mm[j] = 0;
  385. iir_mem16(e, ak, e, nsf, p, mm, stack);
  386. for (j=0;j<p;j++)
  387. mm[j] = 0;
  388. filter10(e, awk1, awk2, e, nsf, mm, stack);
  389. for (j=0;j<nsf;j++)
  390. x[2][j] = e[j];
  391. }
  392. for (i=1;i>=0;i--)
  393. {
  394. spx_word16_t e0=exc2[-pitch-1+i];
  395. #ifdef FIXED_POINT
  396. /* Scale excitation down if needed (avoiding overflow) */
  397. if (scaledown)
  398. e0 = SHR16(e0,1);
  399. #endif
  400. x[i][0]=MULT16_16_Q14(r[0], e0);
  401. for (j=0;j<nsf-1;j++)
  402. x[i][j+1]=ADD32(x[i+1][j],MULT16_16_P14(r[j+1], e0));
  403. }
  404. for (i=0;i<3;i++)
  405. corr[i]=inner_prod(x[i],new_target,nsf);
  406. for (i=0;i<3;i++)
  407. for (j=0;j<=i;j++)
  408. A[i][j]=A[j][i]=inner_prod(x[i],x[j],nsf);
  409. {
  410. spx_word32_t C[9];
  411. #ifdef FIXED_POINT
  412. spx_word16_t C16[9];
  413. #else
  414. spx_word16_t *C16=C;
  415. #endif
  416. C[0]=corr[2];
  417. C[1]=corr[1];
  418. C[2]=corr[0];
  419. C[3]=A[1][2];
  420. C[4]=A[0][1];
  421. C[5]=A[0][2];
  422. C[6]=A[2][2];
  423. C[7]=A[1][1];
  424. C[8]=A[0][0];
  425. /*plc_tuning *= 2;*/
  426. if (plc_tuning<2)
  427. plc_tuning=2;
  428. if (plc_tuning>30)
  429. plc_tuning=30;
  430. #ifdef FIXED_POINT
  431. C[0] = SHL32(C[0],1);
  432. C[1] = SHL32(C[1],1);
  433. C[2] = SHL32(C[2],1);
  434. C[3] = SHL32(C[3],1);
  435. C[4] = SHL32(C[4],1);
  436. C[5] = SHL32(C[5],1);
  437. C[6] = MAC16_32_Q15(C[6],MULT16_16_16(plc_tuning,655),C[6]);
  438. C[7] = MAC16_32_Q15(C[7],MULT16_16_16(plc_tuning,655),C[7]);
  439. C[8] = MAC16_32_Q15(C[8],MULT16_16_16(plc_tuning,655),C[8]);
  440. normalize16(C, C16, 32767, 9);
  441. #else
  442. C[6]*=.5*(1+.02*plc_tuning);
  443. C[7]*=.5*(1+.02*plc_tuning);
  444. C[8]*=.5*(1+.02*plc_tuning);
  445. #endif
  446. best_cdbk = pitch_gain_search_3tap_vq(gain_cdbk, gain_cdbk_size, C16, max_gain);
  447. #ifdef FIXED_POINT
  448. gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4]);
  449. gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4+1]);
  450. gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4+2]);
  451. /*printf ("%d %d %d %d\n",gain[0],gain[1],gain[2], best_cdbk);*/
  452. #else
  453. gain[0] = 0.015625*gain_cdbk[best_cdbk*4] + .5;
  454. gain[1] = 0.015625*gain_cdbk[best_cdbk*4+1]+ .5;
  455. gain[2] = 0.015625*gain_cdbk[best_cdbk*4+2]+ .5;
  456. #endif
  457. *cdbk_index=best_cdbk;
  458. }
  459. SPEEX_MEMSET(exc, 0, nsf);
  460. for (i=0;i<3;i++)
  461. {
  462. int j;
  463. int tmp1, tmp3;
  464. int pp=pitch+1-i;
  465. tmp1=nsf;
  466. if (tmp1>pp)
  467. tmp1=pp;
  468. for (j=0;j<tmp1;j++)
  469. exc[j]=MAC16_16(exc[j],SHL16(gain[2-i],7),exc2[j-pp]);
  470. tmp3=nsf;
  471. if (tmp3>pp+pitch)
  472. tmp3=pp+pitch;
  473. for (j=tmp1;j<tmp3;j++)
  474. exc[j]=MAC16_16(exc[j],SHL16(gain[2-i],7),exc2[j-pp-pitch]);
  475. }
  476. for (i=0;i<nsf;i++)
  477. {
  478. spx_word32_t tmp = ADD32(ADD32(MULT16_16(gain[0],x[2][i]),MULT16_16(gain[1],x[1][i])),
  479. MULT16_16(gain[2],x[0][i]));
  480. new_target[i] = SUB16(new_target[i], EXTRACT16(PSHR32(tmp,6)));
  481. }
  482. err = inner_prod(new_target, new_target, nsf);
  483. return err;
  484. }
  485. /** Finds the best quantized 3-tap pitch predictor by analysis by synthesis */
  486. int pitch_search_3tap(
  487. spx_word16_t target[], /* Target vector */
  488. spx_word16_t *sw,
  489. spx_coef_t ak[], /* LPCs for this subframe */
  490. spx_coef_t awk1[], /* Weighted LPCs #1 for this subframe */
  491. spx_coef_t awk2[], /* Weighted LPCs #2 for this subframe */
  492. spx_sig_t exc[], /* Excitation */
  493. const void *par,
  494. int start, /* Smallest pitch value allowed */
  495. int end, /* Largest pitch value allowed */
  496. spx_word16_t pitch_coef, /* Voicing (pitch) coefficient */
  497. int p, /* Number of LPC coeffs */
  498. int nsf, /* Number of samples in subframe */
  499. SpeexBits *bits,
  500. char *stack,
  501. spx_word16_t *exc2,
  502. spx_word16_t *r,
  503. int complexity,
  504. int cdbk_offset,
  505. int plc_tuning,
  506. spx_word32_t *cumul_gain
  507. )
  508. {
  509. int i;
  510. int cdbk_index, pitch=0, best_gain_index=0;
  511. VARDECL(spx_sig_t *best_exc);
  512. VARDECL(spx_word16_t *new_target);
  513. VARDECL(spx_word16_t *best_target);
  514. int best_pitch=0;
  515. spx_word32_t err, best_err=-1;
  516. int N;
  517. const ltp_params *params;
  518. const signed char *gain_cdbk;
  519. int gain_cdbk_size;
  520. int scaledown=0;
  521. VARDECL(int *nbest);
  522. params = (const ltp_params*) par;
  523. gain_cdbk_size = 1<<params->gain_bits;
  524. gain_cdbk = params->gain_cdbk + 4*gain_cdbk_size*cdbk_offset;
  525. N=complexity;
  526. if (N>10)
  527. N=10;
  528. if (N<1)
  529. N=1;
  530. ALLOC(nbest, N, int);
  531. params = (const ltp_params*) par;
  532. if (end<start)
  533. {
  534. speex_bits_pack(bits, 0, params->pitch_bits);
  535. speex_bits_pack(bits, 0, params->gain_bits);
  536. SPEEX_MEMSET(exc, 0, nsf);
  537. return start;
  538. }
  539. #ifdef FIXED_POINT
  540. /* Check if we need to scale everything down in the pitch search to avoid overflows */
  541. for (i=0;i<nsf;i++)
  542. {
  543. if (ABS16(target[i])>16383)
  544. {
  545. scaledown=1;
  546. break;
  547. }
  548. }
  549. for (i=-end;i<0;i++)
  550. {
  551. if (ABS16(exc2[i])>16383)
  552. {
  553. scaledown=1;
  554. break;
  555. }
  556. }
  557. #endif
  558. if (N>end-start+1)
  559. N=end-start+1;
  560. if (end != start)
  561. open_loop_nbest_pitch(sw, start, end, nsf, nbest, NULL, N, stack);
  562. else
  563. nbest[0] = start;
  564. ALLOC(best_exc, nsf, spx_sig_t);
  565. ALLOC(new_target, nsf, spx_word16_t);
  566. ALLOC(best_target, nsf, spx_word16_t);
  567. for (i=0;i<N;i++)
  568. {
  569. pitch=nbest[i];
  570. SPEEX_MEMSET(exc, 0, nsf);
  571. err=pitch_gain_search_3tap(target, ak, awk1, awk2, exc, gain_cdbk, gain_cdbk_size, pitch, p, nsf,
  572. bits, stack, exc2, r, new_target, &cdbk_index, plc_tuning, *cumul_gain, scaledown);
  573. if (err<best_err || best_err<0)
  574. {
  575. SPEEX_COPY(best_exc, exc, nsf);
  576. SPEEX_COPY(best_target, new_target, nsf);
  577. best_err=err;
  578. best_pitch=pitch;
  579. best_gain_index=cdbk_index;
  580. }
  581. }
  582. /*printf ("pitch: %d %d\n", best_pitch, best_gain_index);*/
  583. speex_bits_pack(bits, best_pitch-start, params->pitch_bits);
  584. speex_bits_pack(bits, best_gain_index, params->gain_bits);
  585. #ifdef FIXED_POINT
  586. *cumul_gain = MULT16_32_Q13(SHL16(params->gain_cdbk[4*best_gain_index+3],8), MAX32(1024,*cumul_gain));
  587. #else
  588. *cumul_gain = 0.03125*MAX32(1024,*cumul_gain)*params->gain_cdbk[4*best_gain_index+3];
  589. #endif
  590. /*printf ("%f\n", cumul_gain);*/
  591. /*printf ("encode pitch: %d %d\n", best_pitch, best_gain_index);*/
  592. SPEEX_COPY(exc, best_exc, nsf);
  593. SPEEX_COPY(target, best_target, nsf);
  594. #ifdef FIXED_POINT
  595. /* Scale target back up if needed */
  596. if (scaledown)
  597. {
  598. for (i=0;i<nsf;i++)
  599. target[i]=SHL16(target[i],1);
  600. }
  601. #endif
  602. return pitch;
  603. }
  604. #endif /* DISABLE_ENCODER */
  605. #ifndef DISABLE_DECODER
  606. void pitch_unquant_3tap(
  607. spx_word16_t exc[], /* Input excitation */
  608. spx_word32_t exc_out[], /* Output excitation */
  609. int start, /* Smallest pitch value allowed */
  610. int end, /* Largest pitch value allowed */
  611. spx_word16_t pitch_coef, /* Voicing (pitch) coefficient */
  612. const void *par,
  613. int nsf, /* Number of samples in subframe */
  614. int *pitch_val,
  615. spx_word16_t *gain_val,
  616. SpeexBits *bits,
  617. char *stack,
  618. int count_lost,
  619. int subframe_offset,
  620. spx_word16_t last_pitch_gain,
  621. int cdbk_offset
  622. )
  623. {
  624. int i;
  625. int pitch;
  626. int gain_index;
  627. spx_word16_t gain[3];
  628. const signed char *gain_cdbk;
  629. int gain_cdbk_size;
  630. const ltp_params *params;
  631. params = (const ltp_params*) par;
  632. gain_cdbk_size = 1<<params->gain_bits;
  633. gain_cdbk = params->gain_cdbk + 4*gain_cdbk_size*cdbk_offset;
  634. pitch = speex_bits_unpack_unsigned(bits, params->pitch_bits);
  635. pitch += start;
  636. gain_index = speex_bits_unpack_unsigned(bits, params->gain_bits);
  637. /*printf ("decode pitch: %d %d\n", pitch, gain_index);*/
  638. #ifdef FIXED_POINT
  639. gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4]);
  640. gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4+1]);
  641. gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4+2]);
  642. #else
  643. gain[0] = 0.015625*gain_cdbk[gain_index*4]+.5;
  644. gain[1] = 0.015625*gain_cdbk[gain_index*4+1]+.5;
  645. gain[2] = 0.015625*gain_cdbk[gain_index*4+2]+.5;
  646. #endif
  647. if (count_lost && pitch > subframe_offset)
  648. {
  649. spx_word16_t gain_sum;
  650. if (1) {
  651. #ifdef FIXED_POINT
  652. spx_word16_t tmp = count_lost < 4 ? last_pitch_gain : SHR16(last_pitch_gain,1);
  653. if (tmp>62)
  654. tmp=62;
  655. #else
  656. spx_word16_t tmp = count_lost < 4 ? last_pitch_gain : 0.5 * last_pitch_gain;
  657. if (tmp>.95)
  658. tmp=.95;
  659. #endif
  660. gain_sum = gain_3tap_to_1tap(gain);
  661. if (gain_sum > tmp)
  662. {
  663. spx_word16_t fact = DIV32_16(SHL32(EXTEND32(tmp),14),gain_sum);
  664. for (i=0;i<3;i++)
  665. gain[i]=MULT16_16_Q14(fact,gain[i]);
  666. }
  667. }
  668. }
  669. *pitch_val = pitch;
  670. gain_val[0]=gain[0];
  671. gain_val[1]=gain[1];
  672. gain_val[2]=gain[2];
  673. gain[0] = SHL16(gain[0],7);
  674. gain[1] = SHL16(gain[1],7);
  675. gain[2] = SHL16(gain[2],7);
  676. SPEEX_MEMSET(exc_out, 0, nsf);
  677. for (i=0;i<3;i++)
  678. {
  679. int j;
  680. int tmp1, tmp3;
  681. int pp=pitch+1-i;
  682. tmp1=nsf;
  683. if (tmp1>pp)
  684. tmp1=pp;
  685. for (j=0;j<tmp1;j++)
  686. exc_out[j]=MAC16_16(exc_out[j],gain[2-i],exc[j-pp]);
  687. tmp3=nsf;
  688. if (tmp3>pp+pitch)
  689. tmp3=pp+pitch;
  690. for (j=tmp1;j<tmp3;j++)
  691. exc_out[j]=MAC16_16(exc_out[j],gain[2-i],exc[j-pp-pitch]);
  692. }
  693. /*for (i=0;i<nsf;i++)
  694. exc[i]=PSHR32(exc32[i],13);*/
  695. }
  696. #endif /* DISABLE_DECODER */
  697. #ifndef DISABLE_ENCODER
  698. /** Forced pitch delay and gain */
  699. int forced_pitch_quant(
  700. spx_word16_t target[], /* Target vector */
  701. spx_word16_t *sw,
  702. spx_coef_t ak[], /* LPCs for this subframe */
  703. spx_coef_t awk1[], /* Weighted LPCs #1 for this subframe */
  704. spx_coef_t awk2[], /* Weighted LPCs #2 for this subframe */
  705. spx_sig_t exc[], /* Excitation */
  706. const void *par,
  707. int start, /* Smallest pitch value allowed */
  708. int end, /* Largest pitch value allowed */
  709. spx_word16_t pitch_coef, /* Voicing (pitch) coefficient */
  710. int p, /* Number of LPC coeffs */
  711. int nsf, /* Number of samples in subframe */
  712. SpeexBits *bits,
  713. char *stack,
  714. spx_word16_t *exc2,
  715. spx_word16_t *r,
  716. int complexity,
  717. int cdbk_offset,
  718. int plc_tuning,
  719. spx_word32_t *cumul_gain
  720. )
  721. {
  722. int i;
  723. VARDECL(spx_word16_t *res);
  724. ALLOC(res, nsf, spx_word16_t);
  725. #ifdef FIXED_POINT
  726. if (pitch_coef>63)
  727. pitch_coef=63;
  728. #else
  729. if (pitch_coef>.99)
  730. pitch_coef=.99;
  731. #endif
  732. for (i=0;i<nsf&&i<start;i++)
  733. {
  734. exc[i]=MULT16_16(SHL16(pitch_coef, 7),exc2[i-start]);
  735. }
  736. for (;i<nsf;i++)
  737. {
  738. exc[i]=MULT16_32_Q15(SHL16(pitch_coef, 9),exc[i-start]);
  739. }
  740. for (i=0;i<nsf;i++)
  741. res[i] = EXTRACT16(PSHR32(exc[i], SIG_SHIFT-1));
  742. syn_percep_zero16(res, ak, awk1, awk2, res, nsf, p, stack);
  743. for (i=0;i<nsf;i++)
  744. target[i]=EXTRACT16(SATURATE(SUB32(EXTEND32(target[i]),EXTEND32(res[i])),32700));
  745. return start;
  746. }
  747. #endif /* DISABLE_ENCODER */
  748. #ifndef DISABLE_DECODER
  749. /** Unquantize forced pitch delay and gain */
  750. void forced_pitch_unquant(
  751. spx_word16_t exc[], /* Input excitation */
  752. spx_word32_t exc_out[], /* Output excitation */
  753. int start, /* Smallest pitch value allowed */
  754. int end, /* Largest pitch value allowed */
  755. spx_word16_t pitch_coef, /* Voicing (pitch) coefficient */
  756. const void *par,
  757. int nsf, /* Number of samples in subframe */
  758. int *pitch_val,
  759. spx_word16_t *gain_val,
  760. SpeexBits *bits,
  761. char *stack,
  762. int count_lost,
  763. int subframe_offset,
  764. spx_word16_t last_pitch_gain,
  765. int cdbk_offset
  766. )
  767. {
  768. int i;
  769. #ifdef FIXED_POINT
  770. if (pitch_coef>63)
  771. pitch_coef=63;
  772. #else
  773. if (pitch_coef>.99)
  774. pitch_coef=.99;
  775. #endif
  776. for (i=0;i<nsf;i++)
  777. {
  778. exc_out[i]=MULT16_16(exc[i-start],SHL16(pitch_coef,7));
  779. exc[i] = EXTRACT16(PSHR32(exc_out[i],13));
  780. }
  781. *pitch_val = start;
  782. gain_val[0]=gain_val[2]=0;
  783. gain_val[1] = pitch_coef;
  784. }
  785. #endif /* DISABLE_DECODER */