PageRenderTime 116ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/external/speex/libspeex/ltp.c

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk
C | 839 lines | 713 code | 60 blank | 66 comment | 86 complexity | ac21d3253702898e7e9815f27b7a0db5 MD5 | raw file
  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 <speex/speex_bits.h>
  35. #include "math_approx.h"
  36. #include "os_support.h"
  37. #ifndef NULL
  38. #define NULL 0
  39. #endif
  40. #ifdef _USE_SSE
  41. #include "ltp_sse.h"
  42. #elif defined (ARM4_ASM) || defined(ARM5E_ASM)
  43. #include "ltp_arm4.h"
  44. #elif defined (BFIN_ASM)
  45. #include "ltp_bfin.h"
  46. #endif
  47. #ifndef OVERRIDE_INNER_PROD
  48. spx_word32_t inner_prod(const spx_word16_t *x, const spx_word16_t *y, int len)
  49. {
  50. spx_word32_t sum=0;
  51. len >>= 2;
  52. while(len--)
  53. {
  54. spx_word32_t part=0;
  55. part = MAC16_16(part,*x++,*y++);
  56. part = MAC16_16(part,*x++,*y++);
  57. part = MAC16_16(part,*x++,*y++);
  58. part = MAC16_16(part,*x++,*y++);
  59. /* HINT: If you had a 40-bit accumulator, you could shift only at the end */
  60. sum = ADD32(sum,SHR32(part,6));
  61. }
  62. return sum;
  63. }
  64. #endif
  65. #ifndef OVERRIDE_PITCH_XCORR
  66. #if 0 /* HINT: Enable this for machines with enough registers (i.e. not x86) */
  67. 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. 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. VARDECL(spx_mem_t *mm);
  358. int pp=pitch-1;
  359. ALLOC(mm, p, spx_mem_t);
  360. for (j=0;j<nsf;j++)
  361. {
  362. if (j-pp<0)
  363. e[j]=exc2[j-pp];
  364. else if (j-pp-pitch<0)
  365. e[j]=exc2[j-pp-pitch];
  366. else
  367. e[j]=0;
  368. }
  369. #ifdef FIXED_POINT
  370. /* Scale target and excitation down if needed (avoiding overflow) */
  371. if (scaledown)
  372. {
  373. for (j=0;j<nsf;j++)
  374. e[j] = SHR16(e[j],1);
  375. for (j=0;j<nsf;j++)
  376. new_target[j] = SHR16(new_target[j],1);
  377. }
  378. #endif
  379. for (j=0;j<p;j++)
  380. mm[j] = 0;
  381. iir_mem16(e, ak, e, nsf, p, mm, stack);
  382. for (j=0;j<p;j++)
  383. mm[j] = 0;
  384. filter_mem16(e, awk1, awk2, e, nsf, p, mm, stack);
  385. for (j=0;j<nsf;j++)
  386. x[2][j] = e[j];
  387. }
  388. for (i=1;i>=0;i--)
  389. {
  390. spx_word16_t e0=exc2[-pitch-1+i];
  391. #ifdef FIXED_POINT
  392. /* Scale excitation down if needed (avoiding overflow) */
  393. if (scaledown)
  394. e0 = SHR16(e0,1);
  395. #endif
  396. x[i][0]=MULT16_16_Q14(r[0], e0);
  397. for (j=0;j<nsf-1;j++)
  398. x[i][j+1]=ADD32(x[i+1][j],MULT16_16_P14(r[j+1], e0));
  399. }
  400. for (i=0;i<3;i++)
  401. corr[i]=inner_prod(x[i],new_target,nsf);
  402. for (i=0;i<3;i++)
  403. for (j=0;j<=i;j++)
  404. A[i][j]=A[j][i]=inner_prod(x[i],x[j],nsf);
  405. {
  406. spx_word32_t C[9];
  407. #ifdef FIXED_POINT
  408. spx_word16_t C16[9];
  409. #else
  410. spx_word16_t *C16=C;
  411. #endif
  412. C[0]=corr[2];
  413. C[1]=corr[1];
  414. C[2]=corr[0];
  415. C[3]=A[1][2];
  416. C[4]=A[0][1];
  417. C[5]=A[0][2];
  418. C[6]=A[2][2];
  419. C[7]=A[1][1];
  420. C[8]=A[0][0];
  421. /*plc_tuning *= 2;*/
  422. if (plc_tuning<2)
  423. plc_tuning=2;
  424. if (plc_tuning>30)
  425. plc_tuning=30;
  426. #ifdef FIXED_POINT
  427. C[0] = SHL32(C[0],1);
  428. C[1] = SHL32(C[1],1);
  429. C[2] = SHL32(C[2],1);
  430. C[3] = SHL32(C[3],1);
  431. C[4] = SHL32(C[4],1);
  432. C[5] = SHL32(C[5],1);
  433. C[6] = MAC16_32_Q15(C[6],MULT16_16_16(plc_tuning,655),C[6]);
  434. C[7] = MAC16_32_Q15(C[7],MULT16_16_16(plc_tuning,655),C[7]);
  435. C[8] = MAC16_32_Q15(C[8],MULT16_16_16(plc_tuning,655),C[8]);
  436. normalize16(C, C16, 32767, 9);
  437. #else
  438. C[6]*=.5*(1+.02*plc_tuning);
  439. C[7]*=.5*(1+.02*plc_tuning);
  440. C[8]*=.5*(1+.02*plc_tuning);
  441. #endif
  442. best_cdbk = pitch_gain_search_3tap_vq(gain_cdbk, gain_cdbk_size, C16, max_gain);
  443. #ifdef FIXED_POINT
  444. gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4]);
  445. gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4+1]);
  446. gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4+2]);
  447. /*printf ("%d %d %d %d\n",gain[0],gain[1],gain[2], best_cdbk);*/
  448. #else
  449. gain[0] = 0.015625*gain_cdbk[best_cdbk*4] + .5;
  450. gain[1] = 0.015625*gain_cdbk[best_cdbk*4+1]+ .5;
  451. gain[2] = 0.015625*gain_cdbk[best_cdbk*4+2]+ .5;
  452. #endif
  453. *cdbk_index=best_cdbk;
  454. }
  455. SPEEX_MEMSET(exc, 0, nsf);
  456. for (i=0;i<3;i++)
  457. {
  458. int j;
  459. int tmp1, tmp3;
  460. int pp=pitch+1-i;
  461. tmp1=nsf;
  462. if (tmp1>pp)
  463. tmp1=pp;
  464. for (j=0;j<tmp1;j++)
  465. exc[j]=MAC16_16(exc[j],SHL16(gain[2-i],7),exc2[j-pp]);
  466. tmp3=nsf;
  467. if (tmp3>pp+pitch)
  468. tmp3=pp+pitch;
  469. for (j=tmp1;j<tmp3;j++)
  470. exc[j]=MAC16_16(exc[j],SHL16(gain[2-i],7),exc2[j-pp-pitch]);
  471. }
  472. for (i=0;i<nsf;i++)
  473. {
  474. spx_word32_t tmp = ADD32(ADD32(MULT16_16(gain[0],x[2][i]),MULT16_16(gain[1],x[1][i])),
  475. MULT16_16(gain[2],x[0][i]));
  476. new_target[i] = SUB16(new_target[i], EXTRACT16(PSHR32(tmp,6)));
  477. }
  478. err = inner_prod(new_target, new_target, nsf);
  479. return err;
  480. }
  481. /** Finds the best quantized 3-tap pitch predictor by analysis by synthesis */
  482. int pitch_search_3tap(
  483. spx_word16_t target[], /* Target vector */
  484. spx_word16_t *sw,
  485. spx_coef_t ak[], /* LPCs for this subframe */
  486. spx_coef_t awk1[], /* Weighted LPCs #1 for this subframe */
  487. spx_coef_t awk2[], /* Weighted LPCs #2 for this subframe */
  488. spx_sig_t exc[], /* Excitation */
  489. const void *par,
  490. int start, /* Smallest pitch value allowed */
  491. int end, /* Largest pitch value allowed */
  492. spx_word16_t pitch_coef, /* Voicing (pitch) coefficient */
  493. int p, /* Number of LPC coeffs */
  494. int nsf, /* Number of samples in subframe */
  495. SpeexBits *bits,
  496. char *stack,
  497. spx_word16_t *exc2,
  498. spx_word16_t *r,
  499. int complexity,
  500. int cdbk_offset,
  501. int plc_tuning,
  502. spx_word32_t *cumul_gain
  503. )
  504. {
  505. int i;
  506. int cdbk_index, pitch=0, best_gain_index=0;
  507. VARDECL(spx_sig_t *best_exc);
  508. VARDECL(spx_word16_t *new_target);
  509. VARDECL(spx_word16_t *best_target);
  510. int best_pitch=0;
  511. spx_word32_t err, best_err=-1;
  512. int N;
  513. const ltp_params *params;
  514. const signed char *gain_cdbk;
  515. int gain_cdbk_size;
  516. int scaledown=0;
  517. VARDECL(int *nbest);
  518. params = (const ltp_params*) par;
  519. gain_cdbk_size = 1<<params->gain_bits;
  520. gain_cdbk = params->gain_cdbk + 4*gain_cdbk_size*cdbk_offset;
  521. N=complexity;
  522. if (N>10)
  523. N=10;
  524. if (N<1)
  525. N=1;
  526. ALLOC(nbest, N, int);
  527. params = (const ltp_params*) par;
  528. if (end<start)
  529. {
  530. speex_bits_pack(bits, 0, params->pitch_bits);
  531. speex_bits_pack(bits, 0, params->gain_bits);
  532. SPEEX_MEMSET(exc, 0, nsf);
  533. return start;
  534. }
  535. #ifdef FIXED_POINT
  536. /* Check if we need to scale everything down in the pitch search to avoid overflows */
  537. for (i=0;i<nsf;i++)
  538. {
  539. if (ABS16(target[i])>16383)
  540. {
  541. scaledown=1;
  542. break;
  543. }
  544. }
  545. for (i=-end;i<nsf;i++)
  546. {
  547. if (ABS16(exc2[i])>16383)
  548. {
  549. scaledown=1;
  550. break;
  551. }
  552. }
  553. #endif
  554. if (N>end-start+1)
  555. N=end-start+1;
  556. if (end != start)
  557. open_loop_nbest_pitch(sw, start, end, nsf, nbest, NULL, N, stack);
  558. else
  559. nbest[0] = start;
  560. ALLOC(best_exc, nsf, spx_sig_t);
  561. ALLOC(new_target, nsf, spx_word16_t);
  562. ALLOC(best_target, nsf, spx_word16_t);
  563. for (i=0;i<N;i++)
  564. {
  565. pitch=nbest[i];
  566. SPEEX_MEMSET(exc, 0, nsf);
  567. err=pitch_gain_search_3tap(target, ak, awk1, awk2, exc, gain_cdbk, gain_cdbk_size, pitch, p, nsf,
  568. bits, stack, exc2, r, new_target, &cdbk_index, plc_tuning, *cumul_gain, scaledown);
  569. if (err<best_err || best_err<0)
  570. {
  571. SPEEX_COPY(best_exc, exc, nsf);
  572. SPEEX_COPY(best_target, new_target, nsf);
  573. best_err=err;
  574. best_pitch=pitch;
  575. best_gain_index=cdbk_index;
  576. }
  577. }
  578. /*printf ("pitch: %d %d\n", best_pitch, best_gain_index);*/
  579. speex_bits_pack(bits, best_pitch-start, params->pitch_bits);
  580. speex_bits_pack(bits, best_gain_index, params->gain_bits);
  581. #ifdef FIXED_POINT
  582. *cumul_gain = MULT16_32_Q13(SHL16(params->gain_cdbk[4*best_gain_index+3],8), MAX32(1024,*cumul_gain));
  583. #else
  584. *cumul_gain = 0.03125*MAX32(1024,*cumul_gain)*params->gain_cdbk[4*best_gain_index+3];
  585. #endif
  586. /*printf ("%f\n", cumul_gain);*/
  587. /*printf ("encode pitch: %d %d\n", best_pitch, best_gain_index);*/
  588. SPEEX_COPY(exc, best_exc, nsf);
  589. SPEEX_COPY(target, best_target, nsf);
  590. #ifdef FIXED_POINT
  591. /* Scale target back up if needed */
  592. if (scaledown)
  593. {
  594. for (i=0;i<nsf;i++)
  595. target[i]=SHL16(target[i],1);
  596. }
  597. #endif
  598. return pitch;
  599. }
  600. void pitch_unquant_3tap(
  601. spx_word16_t exc[], /* Input excitation */
  602. spx_word32_t exc_out[], /* Output excitation */
  603. int start, /* Smallest pitch value allowed */
  604. int end, /* Largest pitch value allowed */
  605. spx_word16_t pitch_coef, /* Voicing (pitch) coefficient */
  606. const void *par,
  607. int nsf, /* Number of samples in subframe */
  608. int *pitch_val,
  609. spx_word16_t *gain_val,
  610. SpeexBits *bits,
  611. char *stack,
  612. int count_lost,
  613. int subframe_offset,
  614. spx_word16_t last_pitch_gain,
  615. int cdbk_offset
  616. )
  617. {
  618. int i;
  619. int pitch;
  620. int gain_index;
  621. spx_word16_t gain[3];
  622. const signed char *gain_cdbk;
  623. int gain_cdbk_size;
  624. const ltp_params *params;
  625. params = (const ltp_params*) par;
  626. gain_cdbk_size = 1<<params->gain_bits;
  627. gain_cdbk = params->gain_cdbk + 4*gain_cdbk_size*cdbk_offset;
  628. pitch = speex_bits_unpack_unsigned(bits, params->pitch_bits);
  629. pitch += start;
  630. gain_index = speex_bits_unpack_unsigned(bits, params->gain_bits);
  631. /*printf ("decode pitch: %d %d\n", pitch, gain_index);*/
  632. #ifdef FIXED_POINT
  633. gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4]);
  634. gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4+1]);
  635. gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4+2]);
  636. #else
  637. gain[0] = 0.015625*gain_cdbk[gain_index*4]+.5;
  638. gain[1] = 0.015625*gain_cdbk[gain_index*4+1]+.5;
  639. gain[2] = 0.015625*gain_cdbk[gain_index*4+2]+.5;
  640. #endif
  641. if (count_lost && pitch > subframe_offset)
  642. {
  643. spx_word16_t gain_sum;
  644. if (1) {
  645. #ifdef FIXED_POINT
  646. spx_word16_t tmp = count_lost < 4 ? last_pitch_gain : SHR16(last_pitch_gain,1);
  647. if (tmp>62)
  648. tmp=62;
  649. #else
  650. spx_word16_t tmp = count_lost < 4 ? last_pitch_gain : 0.5 * last_pitch_gain;
  651. if (tmp>.95)
  652. tmp=.95;
  653. #endif
  654. gain_sum = gain_3tap_to_1tap(gain);
  655. if (gain_sum > tmp)
  656. {
  657. spx_word16_t fact = DIV32_16(SHL32(EXTEND32(tmp),14),gain_sum);
  658. for (i=0;i<3;i++)
  659. gain[i]=MULT16_16_Q14(fact,gain[i]);
  660. }
  661. }
  662. }
  663. *pitch_val = pitch;
  664. gain_val[0]=gain[0];
  665. gain_val[1]=gain[1];
  666. gain_val[2]=gain[2];
  667. gain[0] = SHL16(gain[0],7);
  668. gain[1] = SHL16(gain[1],7);
  669. gain[2] = SHL16(gain[2],7);
  670. SPEEX_MEMSET(exc_out, 0, nsf);
  671. for (i=0;i<3;i++)
  672. {
  673. int j;
  674. int tmp1, tmp3;
  675. int pp=pitch+1-i;
  676. tmp1=nsf;
  677. if (tmp1>pp)
  678. tmp1=pp;
  679. for (j=0;j<tmp1;j++)
  680. exc_out[j]=MAC16_16(exc_out[j],gain[2-i],exc[j-pp]);
  681. tmp3=nsf;
  682. if (tmp3>pp+pitch)
  683. tmp3=pp+pitch;
  684. for (j=tmp1;j<tmp3;j++)
  685. exc_out[j]=MAC16_16(exc_out[j],gain[2-i],exc[j-pp-pitch]);
  686. }
  687. /*for (i=0;i<nsf;i++)
  688. exc[i]=PSHR32(exc32[i],13);*/
  689. }
  690. /** Forced pitch delay and gain */
  691. int forced_pitch_quant(
  692. spx_word16_t target[], /* Target vector */
  693. spx_word16_t *sw,
  694. spx_coef_t ak[], /* LPCs for this subframe */
  695. spx_coef_t awk1[], /* Weighted LPCs #1 for this subframe */
  696. spx_coef_t awk2[], /* Weighted LPCs #2 for this subframe */
  697. spx_sig_t exc[], /* Excitation */
  698. const void *par,
  699. int start, /* Smallest pitch value allowed */
  700. int end, /* Largest pitch value allowed */
  701. spx_word16_t pitch_coef, /* Voicing (pitch) coefficient */
  702. int p, /* Number of LPC coeffs */
  703. int nsf, /* Number of samples in subframe */
  704. SpeexBits *bits,
  705. char *stack,
  706. spx_word16_t *exc2,
  707. spx_word16_t *r,
  708. int complexity,
  709. int cdbk_offset,
  710. int plc_tuning,
  711. spx_word32_t *cumul_gain
  712. )
  713. {
  714. int i;
  715. VARDECL(spx_word16_t *res);
  716. ALLOC(res, nsf, spx_word16_t);
  717. #ifdef FIXED_POINT
  718. if (pitch_coef>63)
  719. pitch_coef=63;
  720. #else
  721. if (pitch_coef>.99)
  722. pitch_coef=.99;
  723. #endif
  724. for (i=0;i<nsf&&i<start;i++)
  725. {
  726. exc[i]=MULT16_16(SHL16(pitch_coef, 7),exc2[i-start]);
  727. }
  728. for (;i<nsf;i++)
  729. {
  730. exc[i]=MULT16_32_Q15(SHL16(pitch_coef, 9),exc[i-start]);
  731. }
  732. for (i=0;i<nsf;i++)
  733. res[i] = EXTRACT16(PSHR32(exc[i], SIG_SHIFT-1));
  734. syn_percep_zero16(res, ak, awk1, awk2, res, nsf, p, stack);
  735. for (i=0;i<nsf;i++)
  736. target[i]=EXTRACT16(SATURATE(SUB32(EXTEND32(target[i]),EXTEND32(res[i])),32700));
  737. return start;
  738. }
  739. /** Unquantize forced pitch delay and gain */
  740. void forced_pitch_unquant(
  741. spx_word16_t exc[], /* Input excitation */
  742. spx_word32_t exc_out[], /* Output excitation */
  743. int start, /* Smallest pitch value allowed */
  744. int end, /* Largest pitch value allowed */
  745. spx_word16_t pitch_coef, /* Voicing (pitch) coefficient */
  746. const void *par,
  747. int nsf, /* Number of samples in subframe */
  748. int *pitch_val,
  749. spx_word16_t *gain_val,
  750. SpeexBits *bits,
  751. char *stack,
  752. int count_lost,
  753. int subframe_offset,
  754. spx_word16_t last_pitch_gain,
  755. int cdbk_offset
  756. )
  757. {
  758. int i;
  759. #ifdef FIXED_POINT
  760. if (pitch_coef>63)
  761. pitch_coef=63;
  762. #else
  763. if (pitch_coef>.99)
  764. pitch_coef=.99;
  765. #endif
  766. for (i=0;i<nsf;i++)
  767. {
  768. exc_out[i]=MULT16_16(exc[i-start],SHL16(pitch_coef,7));
  769. exc[i] = EXTRACT16(PSHR32(exc_out[i],13));
  770. }
  771. *pitch_val = start;
  772. gain_val[0]=gain_val[2]=0;
  773. gain_val[1] = pitch_coef;
  774. }