2
0

mdct_mipsr1.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* Copyright (c) 2007-2008 CSIRO
  2. Copyright (c) 2007-2008 Xiph.Org Foundation
  3. Written by Jean-Marc Valin */
  4. /*
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. - Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. - Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  14. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  17. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  21. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /* This is a simple MDCT implementation that uses a N/4 complex FFT
  26. to do most of the work. It should be relatively straightforward to
  27. plug in pretty much and FFT here.
  28. This replaces the Vorbis FFT (and uses the exact same API), which
  29. was a bit too messy and that was ending up duplicating code
  30. (might as well use the same FFT everywhere).
  31. The algorithm is similar to (and inspired from) Fabrice Bellard's
  32. MDCT implementation in FFMPEG, but has differences in signs, ordering
  33. and scaling in many places.
  34. */
  35. #ifndef __MDCT_MIPSR1_H__
  36. #define __MDCT_MIPSR1_H__
  37. #ifndef SKIP_CONFIG_H
  38. #ifdef HAVE_CONFIG_H
  39. #include "config.h"
  40. #endif
  41. #endif
  42. #include "mdct.h"
  43. #include "kiss_fft.h"
  44. #include "_kiss_fft_guts.h"
  45. #include <math.h>
  46. #include "os_support.h"
  47. #include "mathops.h"
  48. #include "stack_alloc.h"
  49. /* Forward MDCT trashes the input array */
  50. #define OVERRIDE_clt_mdct_forward
  51. void clt_mdct_forward(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar * OPUS_RESTRICT out,
  52. const opus_val16 *window, int overlap, int shift, int stride, int arch)
  53. {
  54. int i;
  55. int N, N2, N4;
  56. VARDECL(kiss_fft_scalar, f);
  57. VARDECL(kiss_fft_cpx, f2);
  58. const kiss_fft_state *st = l->kfft[shift];
  59. const kiss_twiddle_scalar *trig;
  60. opus_val16 scale;
  61. #ifdef FIXED_POINT
  62. /* Allows us to scale with MULT16_32_Q16(), which is faster than
  63. MULT16_32_Q15() on ARM. */
  64. int scale_shift = st->scale_shift-1;
  65. #endif
  66. (void)arch;
  67. SAVE_STACK;
  68. scale = st->scale;
  69. N = l->n;
  70. trig = l->trig;
  71. for (i=0;i<shift;i++)
  72. {
  73. N >>= 1;
  74. trig += N;
  75. }
  76. N2 = N>>1;
  77. N4 = N>>2;
  78. ALLOC(f, N2, kiss_fft_scalar);
  79. ALLOC(f2, N4, kiss_fft_cpx);
  80. /* Consider the input to be composed of four blocks: [a, b, c, d] */
  81. /* Window, shuffle, fold */
  82. {
  83. /* Temp pointers to make it really clear to the compiler what we're doing */
  84. const kiss_fft_scalar * OPUS_RESTRICT xp1 = in+(overlap>>1);
  85. const kiss_fft_scalar * OPUS_RESTRICT xp2 = in+N2-1+(overlap>>1);
  86. kiss_fft_scalar * OPUS_RESTRICT yp = f;
  87. const opus_val16 * OPUS_RESTRICT wp1 = window+(overlap>>1);
  88. const opus_val16 * OPUS_RESTRICT wp2 = window+(overlap>>1)-1;
  89. for(i=0;i<((overlap+3)>>2);i++)
  90. {
  91. /* Real part arranged as -d-cR, Imag part arranged as -b+aR*/
  92. *yp++ = S_MUL_ADD(*wp2, xp1[N2],*wp1,*xp2);
  93. *yp++ = S_MUL_SUB(*wp1, *xp1,*wp2, xp2[-N2]);
  94. xp1+=2;
  95. xp2-=2;
  96. wp1+=2;
  97. wp2-=2;
  98. }
  99. wp1 = window;
  100. wp2 = window+overlap-1;
  101. for(;i<N4-((overlap+3)>>2);i++)
  102. {
  103. /* Real part arranged as a-bR, Imag part arranged as -c-dR */
  104. *yp++ = *xp2;
  105. *yp++ = *xp1;
  106. xp1+=2;
  107. xp2-=2;
  108. }
  109. for(;i<N4;i++)
  110. {
  111. /* Real part arranged as a-bR, Imag part arranged as -c-dR */
  112. *yp++ = S_MUL_SUB(*wp2, *xp2, *wp1, xp1[-N2]);
  113. *yp++ = S_MUL_ADD(*wp2, *xp1, *wp1, xp2[N2]);
  114. xp1+=2;
  115. xp2-=2;
  116. wp1+=2;
  117. wp2-=2;
  118. }
  119. }
  120. /* Pre-rotation */
  121. {
  122. kiss_fft_scalar * OPUS_RESTRICT yp = f;
  123. const kiss_twiddle_scalar *t = &trig[0];
  124. for(i=0;i<N4;i++)
  125. {
  126. kiss_fft_cpx yc;
  127. kiss_twiddle_scalar t0, t1;
  128. kiss_fft_scalar re, im, yr, yi;
  129. t0 = t[i];
  130. t1 = t[N4+i];
  131. re = *yp++;
  132. im = *yp++;
  133. yr = S_MUL_SUB(re,t0,im,t1);
  134. yi = S_MUL_ADD(im,t0,re,t1);
  135. yc.r = yr;
  136. yc.i = yi;
  137. yc.r = PSHR32(MULT16_32_Q16(scale, yc.r), scale_shift);
  138. yc.i = PSHR32(MULT16_32_Q16(scale, yc.i), scale_shift);
  139. f2[st->bitrev[i]] = yc;
  140. }
  141. }
  142. /* N/4 complex FFT, does not downscale anymore */
  143. opus_fft_impl(st, f2);
  144. /* Post-rotate */
  145. {
  146. /* Temp pointers to make it really clear to the compiler what we're doing */
  147. const kiss_fft_cpx * OPUS_RESTRICT fp = f2;
  148. kiss_fft_scalar * OPUS_RESTRICT yp1 = out;
  149. kiss_fft_scalar * OPUS_RESTRICT yp2 = out+stride*(N2-1);
  150. const kiss_twiddle_scalar *t = &trig[0];
  151. /* Temp pointers to make it really clear to the compiler what we're doing */
  152. for(i=0;i<N4;i++)
  153. {
  154. kiss_fft_scalar yr, yi;
  155. yr = S_MUL_SUB(fp->i,t[N4+i] , fp->r,t[i]);
  156. yi = S_MUL_ADD(fp->r,t[N4+i] ,fp->i,t[i]);
  157. *yp1 = yr;
  158. *yp2 = yi;
  159. fp++;
  160. yp1 += 2*stride;
  161. yp2 -= 2*stride;
  162. }
  163. }
  164. RESTORE_STACK;
  165. }
  166. #define OVERRIDE_clt_mdct_backward
  167. void clt_mdct_backward(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar * OPUS_RESTRICT out,
  168. const opus_val16 * OPUS_RESTRICT window, int overlap, int shift, int stride, int arch)
  169. {
  170. int i;
  171. int N, N2, N4;
  172. const kiss_twiddle_scalar *trig;
  173. (void)arch;
  174. N = l->n;
  175. trig = l->trig;
  176. for (i=0;i<shift;i++)
  177. {
  178. N >>= 1;
  179. trig += N;
  180. }
  181. N2 = N>>1;
  182. N4 = N>>2;
  183. /* Pre-rotate */
  184. {
  185. /* Temp pointers to make it really clear to the compiler what we're doing */
  186. const kiss_fft_scalar * OPUS_RESTRICT xp1 = in;
  187. const kiss_fft_scalar * OPUS_RESTRICT xp2 = in+stride*(N2-1);
  188. kiss_fft_scalar * OPUS_RESTRICT yp = out+(overlap>>1);
  189. const kiss_twiddle_scalar * OPUS_RESTRICT t = &trig[0];
  190. const opus_int16 * OPUS_RESTRICT bitrev = l->kfft[shift]->bitrev;
  191. for(i=0;i<N4;i++)
  192. {
  193. int rev;
  194. kiss_fft_scalar yr, yi;
  195. rev = *bitrev++;
  196. yr = S_MUL_ADD(*xp2, t[i] , *xp1, t[N4+i]);
  197. yi = S_MUL_SUB(*xp1, t[i] , *xp2, t[N4+i]);
  198. /* We swap real and imag because we use an FFT instead of an IFFT. */
  199. yp[2*rev+1] = yr;
  200. yp[2*rev] = yi;
  201. /* Storing the pre-rotation directly in the bitrev order. */
  202. xp1+=2*stride;
  203. xp2-=2*stride;
  204. }
  205. }
  206. opus_fft_impl(l->kfft[shift], (kiss_fft_cpx*)(out+(overlap>>1)));
  207. /* Post-rotate and de-shuffle from both ends of the buffer at once to make
  208. it in-place. */
  209. {
  210. kiss_fft_scalar * OPUS_RESTRICT yp0 = out+(overlap>>1);
  211. kiss_fft_scalar * OPUS_RESTRICT yp1 = out+(overlap>>1)+N2-2;
  212. const kiss_twiddle_scalar *t = &trig[0];
  213. /* Loop to (N4+1)>>1 to handle odd N4. When N4 is odd, the
  214. middle pair will be computed twice. */
  215. for(i=0;i<(N4+1)>>1;i++)
  216. {
  217. kiss_fft_scalar re, im, yr, yi;
  218. kiss_twiddle_scalar t0, t1;
  219. /* We swap real and imag because we're using an FFT instead of an IFFT. */
  220. re = yp0[1];
  221. im = yp0[0];
  222. t0 = t[i];
  223. t1 = t[N4+i];
  224. /* We'd scale up by 2 here, but instead it's done when mixing the windows */
  225. yr = S_MUL_ADD(re,t0 , im,t1);
  226. yi = S_MUL_SUB(re,t1 , im,t0);
  227. /* We swap real and imag because we're using an FFT instead of an IFFT. */
  228. re = yp1[1];
  229. im = yp1[0];
  230. yp0[0] = yr;
  231. yp1[1] = yi;
  232. t0 = t[(N4-i-1)];
  233. t1 = t[(N2-i-1)];
  234. /* We'd scale up by 2 here, but instead it's done when mixing the windows */
  235. yr = S_MUL_ADD(re,t0,im,t1);
  236. yi = S_MUL_SUB(re,t1,im,t0);
  237. yp1[0] = yr;
  238. yp0[1] = yi;
  239. yp0 += 2;
  240. yp1 -= 2;
  241. }
  242. }
  243. /* Mirror on both sides for TDAC */
  244. {
  245. kiss_fft_scalar * OPUS_RESTRICT xp1 = out+overlap-1;
  246. kiss_fft_scalar * OPUS_RESTRICT yp1 = out;
  247. const opus_val16 * OPUS_RESTRICT wp1 = window;
  248. const opus_val16 * OPUS_RESTRICT wp2 = window+overlap-1;
  249. for(i = 0; i < overlap/2; i++)
  250. {
  251. kiss_fft_scalar x1, x2;
  252. x1 = *xp1;
  253. x2 = *yp1;
  254. *yp1++ = MULT16_32_Q15(*wp2, x2) - MULT16_32_Q15(*wp1, x1);
  255. *xp1-- = MULT16_32_Q15(*wp1, x2) + MULT16_32_Q15(*wp2, x1);
  256. wp1++;
  257. wp2--;
  258. }
  259. }
  260. }
  261. #endif /* __MDCT_MIPSR1_H__ */