lsp_tm.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* Copyright (C) 2007 Hong Zhiqian */
  2. /**
  3. @file lsp_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_LSP_INTERPOLATE
  35. void lsp_interpolate(Int16 *old_lsp, Int16 *new_lsp, Int16 *interp_lsp, int len, int subframe, int nb_subframes)
  36. {
  37. register int tmp = DIV32_16(SHL32(EXTEND32(1 + subframe),14),nb_subframes);
  38. register int tmp2 = 16384-tmp;
  39. register int in_0, in_1, factor, out_1, out_2, olsp, nlsp, ilsp;
  40. register int i;
  41. TMDEBUG_ALIGNMEM(old_lsp);
  42. TMDEBUG_ALIGNMEM(new_lsp);
  43. TMDEBUG_ALIGNMEM(interp_lsp);
  44. LSPINTERPOLATE_START();
  45. factor = pack16lsb(tmp,tmp2);
  46. len >>= 1;
  47. for ( i=0 ; i<len ; ++i )
  48. {
  49. olsp = ld32x(old_lsp,i); // olsp[i+1],olsp[i]
  50. nlsp = ld32x(new_lsp,i); // nlsp[i+1],nlsp[i]
  51. in_0 = pack16lsb(nlsp,olsp);
  52. in_1 = pack16msb(nlsp,olsp);
  53. out_1 = 8192 + ifir16(in_0,factor);
  54. out_2 = 8192 + ifir16(in_1,factor);
  55. ilsp = pack16lsb(out_2 >> 14, out_1 >> 14);
  56. st32d(i << 2, interp_lsp, ilsp);
  57. }
  58. LSPINTERPOLATE_STOP();
  59. }
  60. #define OVERRIDE_CHEB_POLY_EVA
  61. static inline Int32 cheb_poly_eva(Int16 *coef, Int16 x, int m, char *stack)
  62. {
  63. register int c10, c32, c54;
  64. register int sum, b0, f0, f1, f2, f3;
  65. register int xx, f32, f10;
  66. CHEBPOLYEVA_START();
  67. xx = sex16(x);
  68. b0 = iclipi(xx,16383);
  69. #if 0
  70. c10 = ld32(coef);
  71. c32 = ld32x(coef,1);
  72. c54 = ld32x(coef,2);
  73. #else
  74. c10 = pack16lsb(coef[1],coef[0]);
  75. c32 = pack16lsb(coef[3],coef[2]);
  76. c54 = pack16lsb(coef[5],coef[4]);
  77. #endif
  78. f0 = ((xx * b0) >> 13) - 16384;
  79. f1 = ((xx * f0) >> 13) - b0;
  80. f2 = ((xx * f1) >> 13) - f0;
  81. if ( m == 4 )
  82. { sum = sex16(c54);
  83. f32 = pack16lsb(xx,f0);
  84. f10 = pack16lsb(f1,f2);
  85. } else
  86. { sum = asri(16,c54);
  87. sum += ((sex16(c54) * xx) + 8192) >> 14;
  88. f3 = ((xx * f2) >> 13) - f1;
  89. f32 = pack16lsb(f0,f1);
  90. f10 = pack16lsb(f2,f3);
  91. }
  92. sum += (ifir16(c32,f32) + 8192) >> 14;
  93. sum += (ifir16(c10,f10) + 8192) >> 14;
  94. #ifndef REMARK_ON
  95. (void)stack;
  96. #endif
  97. CHEBPOLYEVA_STOP();
  98. return sum;
  99. }
  100. #define OVERRIDE_LSP_ENFORCE_MARGIN
  101. void lsp_enforce_margin(Int16 *lsp, int len, Int16 margin)
  102. {
  103. register int i;
  104. register int m = margin;
  105. register int m2 = 25736-margin;
  106. register int lsp0, lsp1, lsp2;
  107. TMDEBUG_ALIGNMEM(lsp);
  108. LSPENFORCEMARGIN_START();
  109. lsp0 = ld32(lsp);
  110. lsp1 = asri(16,lsp0);
  111. lsp0 = sex16(lsp0);
  112. lsp2 = lsp[len-1];
  113. if ( lsp0 < m )
  114. { lsp0 = m;
  115. lsp[0] = m;
  116. }
  117. if ( lsp2 > m2 )
  118. { lsp2 = m2;
  119. lsp[len-1] = m2;
  120. }
  121. for ( i=1 ; i<len-1 ; ++i )
  122. {
  123. lsp0 += m;
  124. lsp2 = lsp[i+1];
  125. m2 = lsp2 - m;
  126. if ( lsp1 < lsp0 )
  127. { lsp1 = lsp0;
  128. lsp[i] = lsp0;
  129. }
  130. if ( lsp1 > m2 )
  131. { lsp1 = (lsp1 >> 1) + (m2 >> 1);
  132. lsp[i] = lsp1;
  133. }
  134. lsp0 = lsp1;
  135. lsp1 = lsp2;
  136. }
  137. LSPENFORCEMARGIN_STOP();
  138. }
  139. #define OVERRIDE_LSP_TO_LPC
  140. void lsp_to_lpc(Int16 *freq, Int16 *ak,int lpcrdr, char *stack)
  141. {
  142. VARDECL(Int16 *freqn);
  143. VARDECL(int **xp);
  144. VARDECL(int *xpmem);
  145. VARDECL(int **xq);
  146. VARDECL(int *xqmem);
  147. register int i, j, k;
  148. register int xout1,xout2,xin;
  149. register int m;
  150. LSPTOLPC_START();
  151. m = lpcrdr>>1;
  152. /*
  153. Reconstruct P(z) and Q(z) by cascading second order polynomials
  154. in form 1 - 2cos(w)z(-1) + z(-2), where w is the LSP frequency.
  155. In the time domain this is:
  156. y(n) = x(n) - 2cos(w)x(n-1) + x(n-2)
  157. This is what the ALLOCS below are trying to do:
  158. int xp[m+1][lpcrdr+1+2]; // P matrix in QIMP
  159. int xq[m+1][lpcrdr+1+2]; // Q matrix in QIMP
  160. These matrices store the output of each stage on each row. The
  161. final (m-th) row has the output of the final (m-th) cascaded
  162. 2nd order filter. The first row is the impulse input to the
  163. system (not written as it is known).
  164. The version below takes advantage of the fact that a lot of the
  165. outputs are zero or known, for example if we put an inpulse
  166. into the first section the "clock" it 10 times only the first 3
  167. outputs samples are non-zero (it's an FIR filter).
  168. */
  169. ALLOC(xp, (m+1), int*);
  170. ALLOC(xpmem, (m+1)*(lpcrdr+1+2), int);
  171. ALLOC(xq, (m+1), int*);
  172. ALLOC(xqmem, (m+1)*(lpcrdr+1+2), int);
  173. for ( i=0; i<=m; i++ )
  174. { xp[i] = xpmem + i*(lpcrdr+1+2);
  175. xq[i] = xqmem + i*(lpcrdr+1+2);
  176. }
  177. /* work out 2cos terms in Q14 */
  178. ALLOC(freqn, lpcrdr, Int16);
  179. for ( j=0,k=0 ; j<m ; ++j,k+=2 )
  180. { register int f;
  181. f = ld32x(freq,j);
  182. freqn[k] = ANGLE2X(sex16(f));
  183. freqn[k+1] = ANGLE2X(asri(16,f));
  184. }
  185. #define QIMP 21 /* scaling for impulse */
  186. xin = SHL32(EXTEND32(1), (QIMP-1)); /* 0.5 in QIMP format */
  187. /* first col and last non-zero values of each row are trivial */
  188. for(i=0;i<=m;i++)
  189. { xp[i][1] = 0;
  190. xp[i][2] = xin;
  191. xp[i][2+2*i] = xin;
  192. xq[i][1] = 0;
  193. xq[i][2] = xin;
  194. xq[i][2+2*i] = xin;
  195. }
  196. /* 2nd row (first output row) is trivial */
  197. xp[1][3] = -MULT16_32_Q14(freqn[0],xp[0][2]);
  198. xq[1][3] = -MULT16_32_Q14(freqn[1],xq[0][2]);
  199. xout1 = xout2 = 0;
  200. for( i=1 ; i<m ; ++i)
  201. { register int f, f0, f1, m0, m1;
  202. k = 2*(i+1)-1;
  203. f = ld32x(freqn,i);
  204. f0 = sex16(f);
  205. f1 = asri(16,f);
  206. for( j=1 ; j<k ; ++j)
  207. { register int _m0, _m1;
  208. _m0 = MULT16_32_Q14(f0,xp[i][j+1]);
  209. xp[i+1][j+2] = ADD32(SUB32(xp[i][j+2], _m0), xp[i][j]);
  210. _m1 = MULT16_32_Q14(f1,xq[i][j+1]);
  211. xq[i+1][j+2] = ADD32(SUB32(xq[i][j+2], _m1), xq[i][j]);
  212. }
  213. m0 = MULT16_32_Q14(f0,xp[i][j+1]);
  214. xp[i+1][j+2] = SUB32(xp[i][j], m0);
  215. m1 = MULT16_32_Q14(f1,xq[i][j+1]);
  216. xq[i+1][j+2] = SUB32(xq[i][j], m1);
  217. }
  218. for( i=0,j=3 ; i<lpcrdr ; ++j,++i )
  219. { register int _a0, _xp0, _xq0;
  220. _xp0 = xp[m][j];
  221. _xq0 = xq[m][j];
  222. _a0 = PSHR32(_xp0 + xout1 + _xq0 - xout2, QIMP-13);
  223. xout1 = _xp0;
  224. xout2 = _xq0;
  225. ak[i] = iclipi(_a0,32767);
  226. }
  227. LSPTOLPC_STOP();
  228. }
  229. #endif