PageRenderTime 60ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/opal-3.10.2/plugins/audio/Speex/libspeex/ltp.c

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