ltp_bfin.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /* Copyright (C) 2005 Analog Devices */
  2. /**
  3. @file ltp_bfin.h
  4. @author Jean-Marc Valin
  5. @brief Long-Term Prediction functions (Blackfin 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. #define OVERRIDE_INNER_PROD
  32. spx_word32_t inner_prod(const spx_word16_t *x, const spx_word16_t *y, int len)
  33. {
  34. spx_word32_t sum=0;
  35. __asm__ __volatile__ (
  36. "P0 = %3;\n\t"
  37. "P1 = %1;\n\t"
  38. "P2 = %2;\n\t"
  39. "I0 = P1;\n\t"
  40. "I1 = P2;\n\t"
  41. "L0 = 0;\n\t"
  42. "L1 = 0;\n\t"
  43. "A0 = 0;\n\t"
  44. "R0.L = W[I0++] || R1.L = W[I1++];\n\t"
  45. "LOOP inner%= LC0 = P0;\n\t"
  46. "LOOP_BEGIN inner%=;\n\t"
  47. "A0 += R0.L*R1.L (IS) || R0.L = W[I0++] || R1.L = W[I1++];\n\t"
  48. "LOOP_END inner%=;\n\t"
  49. "A0 += R0.L*R1.L (IS);\n\t"
  50. "A0 = A0 >>> 6;\n\t"
  51. "R0 = A0;\n\t"
  52. "%0 = R0;\n\t"
  53. : "=m" (sum)
  54. : "m" (x), "m" (y), "d" (len-1)
  55. : "P0", "P1", "P2", "R0", "R1", "A0", "I0", "I1", "L0", "L1", "R3"
  56. );
  57. return sum;
  58. }
  59. #define OVERRIDE_PITCH_XCORR
  60. void pitch_xcorr(const spx_word16_t *_x, const spx_word16_t *_y, spx_word32_t *corr, int len, int nb_pitch, char *stack)
  61. {
  62. corr += nb_pitch - 1;
  63. __asm__ __volatile__ (
  64. "P2 = %0;\n\t"
  65. "I0 = P2;\n\t" /* x in I0 */
  66. "B0 = P2;\n\t" /* x in B0 */
  67. "R0 = %3;\n\t" /* len in R0 */
  68. "P3 = %3;\n\t"
  69. "P3 += -2;\n\t" /* len in R0 */
  70. "P4 = %4;\n\t" /* nb_pitch in R0 */
  71. "R1 = R0 << 1;\n\t" /* number of bytes in x */
  72. "L0 = R1;\n\t"
  73. "P0 = %1;\n\t"
  74. "P1 = %2;\n\t"
  75. "B1 = P1;\n\t"
  76. "L1 = 0;\n\t" /*Disable looping on I1*/
  77. "r0 = [I0++];\n\t"
  78. "LOOP pitch%= LC0 = P4 >> 1;\n\t"
  79. "LOOP_BEGIN pitch%=;\n\t"
  80. "I1 = P0;\n\t"
  81. "A1 = A0 = 0;\n\t"
  82. "R1 = [I1++];\n\t"
  83. "LOOP inner_prod%= LC1 = P3 >> 1;\n\t"
  84. "LOOP_BEGIN inner_prod%=;\n\t"
  85. "A1 += R0.L*R1.H, A0 += R0.L*R1.L (IS) || R1.L = W[I1++];\n\t"
  86. "A1 += R0.H*R1.L, A0 += R0.H*R1.H (IS) || R1.H = W[I1++] || R0 = [I0++];\n\t"
  87. "LOOP_END inner_prod%=;\n\t"
  88. "A1 += R0.L*R1.H, A0 += R0.L*R1.L (IS) || R1.L = W[I1++];\n\t"
  89. "A1 += R0.H*R1.L, A0 += R0.H*R1.H (IS) || R0 = [I0++];\n\t"
  90. "A0 = A0 >>> 6;\n\t"
  91. "A1 = A1 >>> 6;\n\t"
  92. "R2 = A0, R3 = A1;\n\t"
  93. "[P1--] = r2;\n\t"
  94. "[P1--] = r3;\n\t"
  95. "P0 += 4;\n\t"
  96. "LOOP_END pitch%=;\n\t"
  97. "L0 = 0;\n\t"
  98. : : "m" (_x), "m" (_y), "m" (corr), "m" (len), "m" (nb_pitch)
  99. : "A0", "A1", "P0", "P1", "P2", "P3", "P4", "R0", "R1", "R2", "R3", "I0", "I1", "L0", "L1", "B0", "B1", "memory"
  100. );
  101. }
  102. #define OVERRIDE_COMPUTE_PITCH_ERROR
  103. static inline spx_word32_t compute_pitch_error(spx_word16_t *C, spx_word16_t *g, spx_word16_t pitch_control)
  104. {
  105. spx_word32_t sum;
  106. __asm__ __volatile__
  107. (
  108. "A0 = 0;\n\t"
  109. "R0 = W[%1++];\n\t"
  110. "R1.L = %2.L*%5.L (IS);\n\t"
  111. "A0 += R1.L*R0.L (IS) || R0 = W[%1++];\n\t"
  112. "R1.L = %3.L*%5.L (IS);\n\t"
  113. "A0 += R1.L*R0.L (IS) || R0 = W[%1++];\n\t"
  114. "R1.L = %4.L*%5.L (IS);\n\t"
  115. "A0 += R1.L*R0.L (IS) || R0 = W[%1++];\n\t"
  116. "R1.L = %2.L*%3.L (IS);\n\t"
  117. "A0 -= R1.L*R0.L (IS) || R0 = W[%1++];\n\t"
  118. "R1.L = %4.L*%3.L (IS);\n\t"
  119. "A0 -= R1.L*R0.L (IS) || R0 = W[%1++];\n\t"
  120. "R1.L = %4.L*%2.L (IS);\n\t"
  121. "A0 -= R1.L*R0.L (IS) || R0 = W[%1++];\n\t"
  122. "R1.L = %2.L*%2.L (IS);\n\t"
  123. "A0 -= R1.L*R0.L (IS) || R0 = W[%1++];\n\t"
  124. "R1.L = %3.L*%3.L (IS);\n\t"
  125. "A0 -= R1.L*R0.L (IS) || R0 = W[%1++];\n\t"
  126. "R1.L = %4.L*%4.L (IS);\n\t"
  127. "A0 -= R1.L*R0.L (IS);\n\t"
  128. "%0 = A0;\n\t"
  129. : "=&D" (sum), "=a" (C)
  130. : "d" (g[0]), "d" (g[1]), "d" (g[2]), "d" (pitch_control), "1" (C)
  131. : "R0", "R1", "R2", "A0"
  132. );
  133. return sum;
  134. }
  135. #define OVERRIDE_OPEN_LOOP_NBEST_PITCH
  136. #ifdef OVERRIDE_OPEN_LOOP_NBEST_PITCH
  137. 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)
  138. {
  139. int i,j,k;
  140. VARDECL(spx_word32_t *best_score);
  141. VARDECL(spx_word32_t *best_ener);
  142. spx_word32_t e0;
  143. VARDECL(spx_word32_t *corr);
  144. VARDECL(spx_word32_t *energy);
  145. ALLOC(best_score, N, spx_word32_t);
  146. ALLOC(best_ener, N, spx_word32_t);
  147. ALLOC(corr, end-start+1, spx_word32_t);
  148. ALLOC(energy, end-start+2, spx_word32_t);
  149. for (i=0;i<N;i++)
  150. {
  151. best_score[i]=-1;
  152. best_ener[i]=0;
  153. pitch[i]=start;
  154. }
  155. energy[0]=inner_prod(sw-start, sw-start, len);
  156. e0=inner_prod(sw, sw, len);
  157. /* energy update -------------------------------------*/
  158. __asm__ __volatile__
  159. (
  160. " P0 = %0;\n\t"
  161. " I1 = %1;\n\t"
  162. " L1 = 0;\n\t"
  163. " I2 = %2;\n\t"
  164. " L2 = 0;\n\t"
  165. " R2 = [P0++];\n\t"
  166. " R3 = 0;\n\t"
  167. " LSETUP (eu1, eu2) LC1 = %3;\n\t"
  168. "eu1: R1.L = W [I1--] || R0.L = W [I2--] ;\n\t"
  169. " R1 = R1.L * R1.L (IS);\n\t"
  170. " R0 = R0.L * R0.L (IS);\n\t"
  171. " R1 >>>= 6;\n\t"
  172. " R1 = R1 + R2;\n\t"
  173. " R0 >>>= 6;\n\t"
  174. " R1 = R1 - R0;\n\t"
  175. " R2 = MAX(R1,R3);\n\t"
  176. "eu2: [P0++] = R2;\n\t"
  177. : : "d" (energy), "d" (&sw[-start-1]), "d" (&sw[-start+len-1]),
  178. "a" (end-start)
  179. : "P0", "I1", "I2", "R0", "R1", "R2", "R3"
  180. #if (__GNUC__ == 4)
  181. , "LC1"
  182. #endif
  183. );
  184. pitch_xcorr(sw, sw-end, corr, len, end-start+1, stack);
  185. /* FIXME: Fixed-point and floating-point code should be merged */
  186. {
  187. VARDECL(spx_word16_t *corr16);
  188. VARDECL(spx_word16_t *ener16);
  189. ALLOC(corr16, end-start+1, spx_word16_t);
  190. ALLOC(ener16, end-start+1, spx_word16_t);
  191. /* Normalize to 180 so we can square it and it still fits in 16 bits */
  192. normalize16(corr, corr16, 180, end-start+1);
  193. normalize16(energy, ener16, 180, end-start+1);
  194. if (N == 1) {
  195. /* optimised asm to handle N==1 case */
  196. __asm__ __volatile__
  197. (
  198. " I0 = %1;\n\t" /* I0: corr16[] */
  199. " L0 = 0;\n\t"
  200. " I1 = %2;\n\t" /* I1: energy */
  201. " L1 = 0;\n\t"
  202. " R2 = -1;\n\t" /* R2: best score */
  203. " R3 = 0;\n\t" /* R3: best energy */
  204. " P0 = %4;\n\t" /* P0: best pitch */
  205. " P1 = %4;\n\t" /* P1: counter */
  206. " LSETUP (sl1, sl2) LC1 = %3;\n\t"
  207. "sl1: R0.L = W [I0++] || R1.L = W [I1++];\n\t"
  208. " R0 = R0.L * R0.L (IS);\n\t"
  209. " R1 += 1;\n\t"
  210. " R4 = R0.L * R3.L;\n\t"
  211. " R5 = R2.L * R1.L;\n\t"
  212. " cc = R5 < R4;\n\t"
  213. " if cc R2 = R0;\n\t"
  214. " if cc R3 = R1;\n\t"
  215. " if cc P0 = P1;\n\t"
  216. "sl2: P1 += 1;\n\t"
  217. " %0 = P0;\n\t"
  218. : "=&d" (pitch[0])
  219. : "a" (corr16), "a" (ener16), "a" (end+1-start), "d" (start)
  220. : "P0", "P1", "I0", "I1", "R0", "R1", "R2", "R3", "R4", "R5"
  221. #if (__GNUC__ == 4)
  222. , "LC1"
  223. #endif
  224. );
  225. }
  226. else {
  227. for (i=start;i<=end;i++)
  228. {
  229. spx_word16_t tmp = MULT16_16_16(corr16[i-start],corr16[i-start]);
  230. /* Instead of dividing the tmp by the energy, we multiply on the other side */
  231. if (MULT16_16(tmp,best_ener[N-1])>MULT16_16(best_score[N-1],ADD16(1,ener16[i-start])))
  232. {
  233. /* We can safely put it last and then check */
  234. best_score[N-1]=tmp;
  235. best_ener[N-1]=ener16[i-start]+1;
  236. pitch[N-1]=i;
  237. /* Check if it comes in front of others */
  238. for (j=0;j<N-1;j++)
  239. {
  240. if (MULT16_16(tmp,best_ener[j])>MULT16_16(best_score[j],ADD16(1,ener16[i-start])))
  241. {
  242. for (k=N-1;k>j;k--)
  243. {
  244. best_score[k]=best_score[k-1];
  245. best_ener[k]=best_ener[k-1];
  246. pitch[k]=pitch[k-1];
  247. }
  248. best_score[j]=tmp;
  249. best_ener[j]=ener16[i-start]+1;
  250. pitch[j]=i;
  251. break;
  252. }
  253. }
  254. }
  255. }
  256. }
  257. }
  258. /* Compute open-loop gain */
  259. if (gain)
  260. {
  261. for (j=0;j<N;j++)
  262. {
  263. spx_word16_t g;
  264. i=pitch[j];
  265. g = DIV32(corr[i-start], 10+SHR32(MULT16_16(spx_sqrt(e0),spx_sqrt(energy[i-start])),6));
  266. /* FIXME: g = max(g,corr/energy) */
  267. if (g<0)
  268. g = 0;
  269. gain[j]=g;
  270. }
  271. }
  272. }
  273. #endif
  274. #define OVERRIDE_PITCH_GAIN_SEARCH_3TAP_VQ
  275. #ifdef OVERRIDE_PITCH_GAIN_SEARCH_3TAP_VQ
  276. static int pitch_gain_search_3tap_vq(
  277. const signed char *gain_cdbk,
  278. int gain_cdbk_size,
  279. spx_word16_t *C16,
  280. spx_word16_t max_gain
  281. )
  282. {
  283. const signed char *ptr=gain_cdbk;
  284. int best_cdbk=0;
  285. spx_word32_t best_sum=-VERY_LARGE32;
  286. spx_word32_t sum=0;
  287. spx_word16_t g[3];
  288. spx_word16_t pitch_control=64;
  289. spx_word16_t gain_sum;
  290. int i;
  291. /* fast asm version of VQ codebook search */
  292. __asm__ __volatile__
  293. (
  294. " P0 = %2;\n\t" /* P0: ptr to gain_cdbk */
  295. " L1 = 0;\n\t" /* no circ addr for L1 */
  296. " %0 = 0;\n\t" /* %0: best_sum */
  297. " %1 = 0;\n\t" /* %1: best_cbdk */
  298. " P1 = 0;\n\t" /* P1: loop counter */
  299. " LSETUP (pgs1, pgs2) LC1 = %4;\n\t"
  300. "pgs1: R2 = B [P0++] (X);\n\t" /* R2: g[0] */
  301. " R3 = B [P0++] (X);\n\t" /* R3: g[1] */
  302. " R4 = B [P0++] (X);\n\t" /* R4: g[2] */
  303. " R2 += 32;\n\t"
  304. " R3 += 32;\n\t"
  305. " R4 += 32;\n\t"
  306. " R4.H = 64;\n\t" /* R4.H: pitch_control */
  307. " R0 = B [P0++] (X);\n\t"
  308. " B0 = R0;\n\t" /* BO: gain_sum */
  309. /* compute_pitch_error() -------------------------------*/
  310. " I1 = %3;\n\t" /* I1: ptr to C */
  311. " A0 = 0;\n\t"
  312. " R0.L = W[I1++];\n\t"
  313. " R1.L = R2.L*R4.H (IS);\n\t"
  314. " A0 += R1.L*R0.L (IS) || R0.L = W[I1++];\n\t"
  315. " R1.L = R3.L*R4.H (IS);\n\t"
  316. " A0 += R1.L*R0.L (IS) || R0.L = W[I1++];\n\t"
  317. " R1.L = R4.L*R4.H (IS);\n\t"
  318. " A0 += R1.L*R0.L (IS) || R0.L = W[I1++];\n\t"
  319. " R1.L = R2.L*R3.L (IS);\n\t"
  320. " A0 -= R1.L*R0.L (IS) || R0.L = W[I1++];\n\t"
  321. " R1.L = R4.L*R3.L (IS);\n\t"
  322. " A0 -= R1.L*R0.L (IS) || R0.L = W[I1++];\n\t"
  323. " R1.L = R4.L*R2.L (IS);\n\t"
  324. " A0 -= R1.L*R0.L (IS) || R0.L = W[I1++];\n\t"
  325. " R1.L = R2.L*R2.L (IS);\n\t"
  326. " A0 -= R1.L*R0.L (IS) || R0.L = W[I1++];\n\t"
  327. " R1.L = R3.L*R3.L (IS);\n\t"
  328. " A0 -= R1.L*R0.L (IS) || R0.L = W[I1++];\n\t"
  329. " R1.L = R4.L*R4.L (IS);\n\t"
  330. " R0 = (A0 -= R1.L*R0.L) (IS);\n\t"
  331. /*
  332. Re-arrange the if-then to code efficiently on the Blackfin:
  333. if (sum>best_sum && gain_sum<=max_gain) ------ (1)
  334. if (sum>best_sum && !(gain_sum>max_gain)) ------ (2)
  335. if (max_gain<=gain_sum) { ------ (3)
  336. sum = -VERY_LARGE32;
  337. }
  338. if (best_sum<=sum)
  339. The blackin cc instructions are all of the form:
  340. cc = x < y (or cc = x <= y)
  341. */
  342. " R1 = B0\n\t"
  343. " R2 = %5\n\t"
  344. " R3 = %6\n\t"
  345. " cc = R2 <= R1;\n\t"
  346. " if cc R0 = R3;\n\t"
  347. " cc = %0 <= R0;\n\t"
  348. " if cc %0 = R0;\n\t"
  349. " if cc %1 = P1;\n\t"
  350. "pgs2: P1 += 1;\n\t"
  351. : "=&d" (best_sum), "=&d" (best_cdbk)
  352. : "a" (gain_cdbk), "a" (C16), "a" (gain_cdbk_size), "a" (max_gain),
  353. "b" (-VERY_LARGE32)
  354. : "R0", "R1", "R2", "R3", "R4", "P0",
  355. "P1", "I1", "L1", "A0", "B0"
  356. #if (__GNUC__ == 4)
  357. , "LC1"
  358. #endif
  359. );
  360. return best_cdbk;
  361. }
  362. #endif