ltp_tm.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /* Copyright (C) 2007 Hong Zhiqian */
  2. /**
  3. @file ltp_tm.h
  4. @author Hong Zhiqian
  5. @brief Various compatibility routines for Speex (TriMedia version)
  6. */
  7. /*
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions
  10. are met:
  11. - Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. - Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. - Neither the name of the Xiph.org Foundation nor the names of its
  17. contributors may be used to endorse or promote products derived from
  18. this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  23. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <ops/custom_defs.h>
  32. #include "profile_tm.h"
  33. #ifdef FIXED_POINT
  34. #define OVERRIDE_INNER_PROD
  35. Int32 inner_prod(const Int16 * restrict x, const Int16 * restrict y, int len)
  36. {
  37. register int sum = 0;
  38. INNERPROD_START();
  39. if ( (int)x & 0x03 == 0 && (int)y & 0x03 == 0 )
  40. {
  41. register int i;
  42. len >>= 1;
  43. for ( i=0 ; i<len ; i+=4 )
  44. {
  45. register int x0, x1, y0, y1, x2, x3, y2, y3;
  46. x0 = ld32x(x,i);
  47. y0 = ld32x(x,i);
  48. x1 = ld32x(x,i+1);
  49. y1 = ld32x(y,i+1);
  50. sum += (ifir16(x0,y0) + ifir16(x1,y1)) >> 6;
  51. x2 = ld32x(x,i+2);
  52. y2 = ld32x(x,i+2);
  53. x3 = ld32x(x,i+3);
  54. y3 = ld32x(x,i+3);
  55. sum += (ifir16(x2,y2) + ifir16(x3,y3)) >> 6;
  56. }
  57. } else
  58. {
  59. len >>= 3;
  60. while( len-- )
  61. {
  62. register int x0, x1, x2, x3, y0, y1, y2, y3;
  63. x0 = pack16lsb(x[0],x[1]);
  64. y0 = pack16lsb(y[0],y[1]);
  65. x1 = pack16lsb(x[2],x[3]);
  66. y1 = pack16lsb(y[2],y[3]);
  67. sum += (ifir16(x0,y0) + ifir16(x1,y1)) >> 6;
  68. x2 = pack16lsb(x[4],x[5]);
  69. y2 = pack16lsb(y[4],y[5]);
  70. x3 = pack16lsb(x[6],x[7]);
  71. y3 = pack16lsb(y[6],y[7]);
  72. sum += (ifir16(x2,y2) + ifir16(x3,y3)) >> 6;
  73. x += 8;
  74. y += 8;
  75. }
  76. }
  77. INNERPROD_STOP();
  78. return sum;
  79. }
  80. #define OVERRIDE_PITCH_XCORR
  81. void pitch_xcorr(const Int16 *_x, const Int16 *_y, Int32 *corr, int len, int nb_pitch, char *stack)
  82. {
  83. register int sum_1, sum_2, sum_3, sum_4;
  84. register int y10, y32, y54, y76, y21, y43, y65;
  85. register int x10, x32;
  86. register int i, j, k, limit;
  87. TMDEBUG_ALIGNMEM(_x);
  88. TMDEBUG_ALIGNMEM(_y);
  89. PITCHXCORR_START();
  90. limit = nb_pitch >> 1;
  91. len >>= 1;
  92. for (i=0 ; i<limit ; i+=2 )
  93. {
  94. sum_1 = sum_2 = sum_3 = sum_4 = 0;
  95. y10 = ld32x(_y,i);
  96. y32 = ld32x(_y,i+1);
  97. for ( j=0 ; j<len ; j+=2 )
  98. {
  99. x10 = ld32x(_x,j);
  100. x32 = ld32x(_x,j+1);
  101. y54 = ld32x(_y,i+j+2);
  102. y76 = ld32x(_y,i+j+3);
  103. sum_1 += (ifir16(x10,y10) + ifir16(x32,y32)) >> 6;
  104. sum_3 += (ifir16(x10,y32) + ifir16(x32,y54)) >> 6;
  105. y21 = funshift2(y32,y10);
  106. y43 = funshift2(y54,y32);
  107. y65 = funshift2(y76,y54);
  108. sum_2 += (ifir16(x10,y21) + ifir16(x32,y43)) >> 6;
  109. sum_4 += (ifir16(x10,y43) + ifir16(x32,y65)) >> 6;
  110. y10 = y54;
  111. y32 = y76;
  112. }
  113. k = i << 1;
  114. corr[nb_pitch-1-k]=sum_1;
  115. corr[nb_pitch-2-k]=sum_2;
  116. corr[nb_pitch-3-k]=sum_3;
  117. corr[nb_pitch-4-k]=sum_4;
  118. }
  119. #ifndef REMARK_ON
  120. (void)stack;
  121. #endif
  122. PITCHXCORR_STOP();
  123. }
  124. #ifndef ttisim
  125. #define OVERRIDE_PITCH_GAIN_SEARCH_3TAP_VQ
  126. static int pitch_gain_search_3tap_vq
  127. (
  128. const signed char *gain_cdbk,
  129. int gain_cdbk_size,
  130. Int16 *C16,
  131. Int16 max_gain
  132. )
  133. {
  134. register int pp = 0x00400040, p=64;
  135. register int g10, g2, g20, g21, g02, g22, g01;
  136. register int cb0, cb1, cb2, cb5432;
  137. register int C10, C32, C54, C76, C98, C83, C2;
  138. register int acc0, acc1, acc2, acc3, sum, gsum, bsum=-VERY_LARGE32;
  139. register int i, best_cdbk=0;
  140. register Int16 tmp;
  141. TMDEBUG_ALIGNMEM(C16);
  142. TMDEBUG_ALIGNMEM(gain_cdbk+2);
  143. PITCHGAINSEARCH3TAPVQ_START();
  144. tmp = ild16(gain_cdbk);
  145. C98 = ld32x(C16,4);
  146. C32 = ld32x(C16,1);
  147. C10 = ld32(C16);
  148. C54 = ld32x(C16,2);
  149. C76 = ld32x(C16,3);
  150. cb0 = sex8(tmp);
  151. cb1 = sex8(tmp>>8);
  152. C83 = funshift2(C98,C32);
  153. C2 = sex16(C32);
  154. gain_cdbk += 2;
  155. #if (TM_UNROLL && TM_UNROLL_PITCHGAINSEARCH3TAPVQ > 0)
  156. #pragma TCS_unroll=4
  157. #pragma TCS_unrollexact=1
  158. #endif
  159. for ( i=0 ; i<gain_cdbk_size ; ++i )
  160. {
  161. cb5432 = ld32x(gain_cdbk,i);
  162. cb2 = sex8(cb5432);
  163. gsum = sex8(cb5432>>8);
  164. sum = 0;
  165. g10 = pack16lsb(cb1 + 32, cb0 + 32);
  166. g2 = cb2 + 32;
  167. g02 = pack16lsb(g10, g2);
  168. acc0 = dspidualmul(g10,pp);
  169. sum += ifir16(acc0,C10);
  170. sum += p * g2 * C2;
  171. g22 = pack16lsb(g02, g02);
  172. g01 = funshift2(g10, g10);
  173. acc1 = dspidualmul(g22, g01);
  174. sum -= ifir16(acc1, C54);
  175. acc2 = dspidualmul(g10, g10);
  176. sum -= ifir16(acc2, C76);
  177. g20 = pack16lsb(g2, g10);
  178. g21 = funshift2(g2, g10);
  179. acc3 = dspidualmul(g20, g21);
  180. sum -= ifir16(acc3, C83);
  181. if ( sum>bsum && gsum<=max_gain )
  182. { bsum = sum;
  183. best_cdbk=i;
  184. }
  185. cb0 = sex8(cb5432 >> 16);
  186. cb1 = asri(24,cb5432);
  187. }
  188. #if (TM_UNROLL && TM_UNROLL_PITCHGAINSEARCH3TAPVQ > 0)
  189. #pragma TCS_unrollexact=0
  190. #pragma TCS_unroll=0
  191. #endif
  192. PITCHGAINSEARCH3TAPVQ_STOP();
  193. return best_cdbk;
  194. }
  195. #endif
  196. #define OVERRIDE_COMPUTE_PITCH_ERROR
  197. #ifndef OVERRIDE_PITCH_GAIN_SEARCH_3TAP_VQ
  198. inline Int32 compute_pitch_error(Int16 *C, Int16 *g, Int16 pitch_control)
  199. {
  200. register int c10, c32, c54, c76, c98, c83;
  201. register int g10, g32, g02, g22, g01, g21, g20;
  202. register int pp, tmp0, tmp1, tmp2, tmp3;
  203. register int sum = 0;
  204. COMPUTEPITCHERROR_START();
  205. g10 = ld32(g);
  206. g32 = ld32x(g,1);
  207. pp = pack16lsb(pitch_control,pitch_control);
  208. c10 = ld32(C);
  209. c32 = ld32x(C,1);
  210. g02 = pack16lsb(g10,g32);
  211. g22 = pack16lsb(g32,g32);
  212. g01 = funshift2(g10,g10);
  213. tmp0 = dspidualmul(g10,pp);
  214. sum += ifir16(tmp0, c10);
  215. sum += pitch_control * sex16(g32) * sex16(c32);
  216. c54 = ld32x(C,2);
  217. c76 = ld32x(C,3);
  218. c98 = ld32x(C,4);
  219. tmp1 = dspidualmul(g22,g01);
  220. sum -= ifir16(tmp1, c54);
  221. tmp2 = dspidualmul(g10,g10);
  222. sum -= ifir16(tmp2,c76);
  223. c83 = funshift2(c98,c32);
  224. g20 = funshift2(g02,g02);
  225. g21 = funshift2(g02,g10);
  226. tmp3 = dspidualmul(g20,g21);
  227. sum -= ifir16(tmp3,c83);
  228. COMPUTEPITCHERROR_STOP();
  229. return sum;
  230. }
  231. #endif
  232. #define OVERRIDE_OPEN_LOOP_NBEST_PITCH
  233. void open_loop_nbest_pitch(Int16 *sw, int start, int end, int len, int *pitch, Int16 *gain, int N, char *stack)
  234. {
  235. VARDECL(int *best_score);
  236. VARDECL(int *best_ener);
  237. VARDECL(Int32 *corr);
  238. VARDECL(Int16 *corr16);
  239. VARDECL(Int16 *ener16);
  240. register int i, j, k, l, N4, N2;
  241. register int _sw10, _sw32, _s0, _s2, limit;
  242. register int *energy;
  243. register int cshift=0, eshift=0;
  244. register int scaledown = 0;
  245. register int e0, _energy0;
  246. ALLOC(corr16, end-start+1, Int16);
  247. ALLOC(ener16, end-start+1, Int16);
  248. ALLOC(corr, end-start+1, Int32);
  249. ALLOC(best_score, N, int);
  250. ALLOC(best_ener, N, int);
  251. energy = corr;
  252. N4 = N << 2;
  253. N2 = N >> 1;
  254. TMDEBUG_ALIGNMEM(sw);
  255. TMDEBUG_ALIGNMEM(pitch);
  256. TMDEBUG_ALIGNMEM(gain);
  257. TMDEBUG_ALIGNMEM(best_score);
  258. TMDEBUG_ALIGNMEM(best_ener);
  259. TMDEBUG_ALIGNMEM(corr16);
  260. TMDEBUG_ALIGNMEM(ener16);
  261. OPENLOOPNBESTPITCH_START();
  262. for ( i=0 ; i<N4 ; i+=4 )
  263. { st32d(i,best_score,-1);
  264. st32d(i,best_ener,0);
  265. st32d(i,pitch,start);
  266. }
  267. for ( j=asri(1,-end) ; j<N2 ; ++j )
  268. { register int _sw10;
  269. _sw10 = ld32x(sw,j);
  270. _sw10 = dspidualabs(_sw10);
  271. if ( _sw10 & 0xC000C000 )
  272. { scaledown = 1;
  273. break;
  274. }
  275. }
  276. if ( scaledown )
  277. {
  278. for ( j=asri(1,-end),k=asli(1,-end) ; j<N2 ; ++j,k+=4 )
  279. { register int _sw10;
  280. _sw10 = ld32x(sw,j);
  281. _sw10 = dualasr(_sw10,1);
  282. st32d(k, sw, _sw10);
  283. }
  284. }
  285. energy[0] = _energy0 = inner_prod(sw-start, sw-start, len);
  286. e0 = inner_prod(sw, sw, len);
  287. j=asri(1,-start-1); k=j+20;
  288. _sw10 = ld32x(sw,j);
  289. _sw32 = ld32x(sw,k);
  290. limit = end-1-start;
  291. for ( i=1,--j,--k ; i<limit ; i+=2,--j,--k )
  292. { register int _energy1, __sw10, __sw32, __s0, __s2;
  293. _s0 = sex16(_sw10);
  294. _s2 = sex16(_sw32);
  295. _energy1 = (_energy0 + ((_s0 * _s0) >> 6)) - ((_s2 * _s2) >> 6);
  296. _energy0 = imax(0,_energy1);
  297. energy[i] = _energy0;
  298. __sw10 = ld32x(sw,j);
  299. __sw32 = ld32x(sw,k);
  300. __s0 = asri(16,__sw10);
  301. __s2 = asri(16,__sw32);
  302. _energy1 = (_energy0 + ((__s0 * __s0) >> 6)) - ((__s2 * __s2) >> 6);
  303. _energy0 = imax(0,_energy1);
  304. energy[i+1] = _energy0;
  305. _sw10 = __sw10;
  306. _sw32 = __sw32;
  307. }
  308. _s0 = sex16(_sw10);
  309. _s2 = sex16(_sw32);
  310. _energy0 = imax(0,(_energy0 + ((_s0 * _s0) >> 6)) - ((_s2 * _s2) >> 6));
  311. energy[i] = _energy0;
  312. eshift = normalize16(energy, ener16, 32766, end-start+1);
  313. /* In fixed-point, this actually overrites the energy array (aliased to corr) */
  314. pitch_xcorr(sw, sw-end, corr, len, end-start+1, stack);
  315. /* Normalize to 180 so we can square it and it still fits in 16 bits */
  316. cshift = normalize16(corr, corr16, 180, end-start+1);
  317. /* If we scaled weighted input down, we need to scale it up again (OK, so we've just lost the LSB, who cares?) */
  318. if ( scaledown )
  319. {
  320. for ( j=asri(1,-end),k=asli(1,-end) ; j<N2 ; ++j,k+=4 )
  321. { register int _sw10;
  322. _sw10 = ld32x(sw,j);
  323. _sw10 = dualasl(_sw10,1);
  324. st32d(k, sw, _sw10);
  325. }
  326. }
  327. /* Search for the best pitch prediction gain */
  328. for ( i=start,l=0 ; i<end ; i+=2,++l )
  329. { register int _corr16, _c0, _c1;
  330. register int _ener16, _e0, _e1;
  331. _corr16 = ld32x(corr16,l);
  332. _corr16 = dspidualmul(_corr16,_corr16);
  333. _c0 = sex16(_corr16);
  334. _c1 = asri(16,_corr16);
  335. _ener16 = ld32x(ener16,l);
  336. _ener16 = dspidualadd(_ener16,0x00010001);
  337. _e0 = sex16(_ener16);
  338. _e1 = asri(16,_ener16);
  339. /* Instead of dividing the tmp by the energy, we multiply on the other side */
  340. if ( (_c0 * best_ener[N-1]) > (best_score[N-1] * _e0) )
  341. {
  342. best_score[N-1] = _c0;
  343. best_ener[N-1] = _e0;
  344. pitch[N-1] = i;
  345. for( j=0 ; j<N-1 ; ++j )
  346. { if ( (_c0 * best_ener[j]) > best_score[j] * _e0 )
  347. { for( k=N-1 ; k>j ; --k )
  348. {
  349. best_score[k]=best_score[k-1];
  350. best_ener[k]=best_ener[k-1];
  351. pitch[k]=pitch[k-1];
  352. }
  353. best_score[j]=_c0;
  354. best_ener[j]=_e0;
  355. pitch[j]=i;
  356. break;
  357. }
  358. }
  359. }
  360. if ( (_c1 * best_ener[N-1]) > (best_score[N-1] * _e1) )
  361. {
  362. best_score[N-1] = _c1;
  363. best_ener[N-1] = _e1;
  364. pitch[N-1] = i+1;
  365. for( j=0 ; j<N-1 ; ++j )
  366. { if ( (_c1 * best_ener[j]) > best_score[j] * _e1 )
  367. { for( k=N-1 ; k>j ; --k )
  368. {
  369. best_score[k]=best_score[k-1];
  370. best_ener[k]=best_ener[k-1];
  371. pitch[k]=pitch[k-1];
  372. }
  373. best_score[j]=_c1;
  374. best_ener[j]=_e1;
  375. pitch[j]=i+1;
  376. break;
  377. }
  378. }
  379. }
  380. }
  381. /* Compute open-loop gain if necessary */
  382. if (gain)
  383. {
  384. for (j=0;j<N;j++)
  385. {
  386. spx_word16_t g;
  387. i=pitch[j];
  388. g = DIV32(SHL32(EXTEND32(corr16[i-start]),cshift), 10+SHR32(MULT16_16(spx_sqrt(e0),spx_sqrt(SHL32(EXTEND32(ener16[i-start]),eshift))),6));
  389. gain[j] = imax(0,g);
  390. }
  391. }
  392. OPENLOOPNBESTPITCH_STOP();
  393. }
  394. #endif