ltp_bfin.h 13 KB

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