Chris@69: /* Copyright (c) 2007-2008 CSIRO Chris@69: Copyright (c) 2007-2008 Xiph.Org Foundation Chris@69: Written by Jean-Marc Valin */ Chris@69: /* Chris@69: Redistribution and use in source and binary forms, with or without Chris@69: modification, are permitted provided that the following conditions Chris@69: are met: Chris@69: Chris@69: - Redistributions of source code must retain the above copyright Chris@69: notice, this list of conditions and the following disclaimer. Chris@69: Chris@69: - Redistributions in binary form must reproduce the above copyright Chris@69: notice, this list of conditions and the following disclaimer in the Chris@69: documentation and/or other materials provided with the distribution. Chris@69: Chris@69: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS Chris@69: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT Chris@69: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR Chris@69: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER Chris@69: OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, Chris@69: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, Chris@69: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR Chris@69: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF Chris@69: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING Chris@69: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS Chris@69: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Chris@69: */ Chris@69: Chris@69: /* This is a simple MDCT implementation that uses a N/4 complex FFT Chris@69: to do most of the work. It should be relatively straightforward to Chris@69: plug in pretty much and FFT here. Chris@69: Chris@69: This replaces the Vorbis FFT (and uses the exact same API), which Chris@69: was a bit too messy and that was ending up duplicating code Chris@69: (might as well use the same FFT everywhere). Chris@69: Chris@69: The algorithm is similar to (and inspired from) Fabrice Bellard's Chris@69: MDCT implementation in FFMPEG, but has differences in signs, ordering Chris@69: and scaling in many places. Chris@69: */ Chris@69: #ifndef __MDCT_MIPSR1_H__ Chris@69: #define __MDCT_MIPSR1_H__ Chris@69: Chris@69: #ifndef SKIP_CONFIG_H Chris@69: #ifdef HAVE_CONFIG_H Chris@69: #include "config.h" Chris@69: #endif Chris@69: #endif Chris@69: Chris@69: #include "mdct.h" Chris@69: #include "kiss_fft.h" Chris@69: #include "_kiss_fft_guts.h" Chris@69: #include Chris@69: #include "os_support.h" Chris@69: #include "mathops.h" Chris@69: #include "stack_alloc.h" Chris@69: Chris@69: /* Forward MDCT trashes the input array */ Chris@69: #define OVERRIDE_clt_mdct_forward Chris@69: void clt_mdct_forward(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar * OPUS_RESTRICT out, Chris@69: const opus_val16 *window, int overlap, int shift, int stride, int arch) Chris@69: { Chris@69: int i; Chris@69: int N, N2, N4; Chris@69: VARDECL(kiss_fft_scalar, f); Chris@69: VARDECL(kiss_fft_cpx, f2); Chris@69: const kiss_fft_state *st = l->kfft[shift]; Chris@69: const kiss_twiddle_scalar *trig; Chris@69: opus_val16 scale; Chris@69: #ifdef FIXED_POINT Chris@69: /* Allows us to scale with MULT16_32_Q16(), which is faster than Chris@69: MULT16_32_Q15() on ARM. */ Chris@69: int scale_shift = st->scale_shift-1; Chris@69: #endif Chris@69: Chris@69: (void)arch; Chris@69: Chris@69: SAVE_STACK; Chris@69: scale = st->scale; Chris@69: Chris@69: N = l->n; Chris@69: trig = l->trig; Chris@69: for (i=0;i>= 1; Chris@69: trig += N; Chris@69: } Chris@69: N2 = N>>1; Chris@69: N4 = N>>2; Chris@69: Chris@69: ALLOC(f, N2, kiss_fft_scalar); Chris@69: ALLOC(f2, N4, kiss_fft_cpx); Chris@69: Chris@69: /* Consider the input to be composed of four blocks: [a, b, c, d] */ Chris@69: /* Window, shuffle, fold */ Chris@69: { Chris@69: /* Temp pointers to make it really clear to the compiler what we're doing */ Chris@69: const kiss_fft_scalar * OPUS_RESTRICT xp1 = in+(overlap>>1); Chris@69: const kiss_fft_scalar * OPUS_RESTRICT xp2 = in+N2-1+(overlap>>1); Chris@69: kiss_fft_scalar * OPUS_RESTRICT yp = f; Chris@69: const opus_val16 * OPUS_RESTRICT wp1 = window+(overlap>>1); Chris@69: const opus_val16 * OPUS_RESTRICT wp2 = window+(overlap>>1)-1; Chris@69: for(i=0;i<((overlap+3)>>2);i++) Chris@69: { Chris@69: /* Real part arranged as -d-cR, Imag part arranged as -b+aR*/ Chris@69: *yp++ = S_MUL_ADD(*wp2, xp1[N2],*wp1,*xp2); Chris@69: *yp++ = S_MUL_SUB(*wp1, *xp1,*wp2, xp2[-N2]); Chris@69: xp1+=2; Chris@69: xp2-=2; Chris@69: wp1+=2; Chris@69: wp2-=2; Chris@69: } Chris@69: wp1 = window; Chris@69: wp2 = window+overlap-1; Chris@69: for(;i>2);i++) Chris@69: { Chris@69: /* Real part arranged as a-bR, Imag part arranged as -c-dR */ Chris@69: *yp++ = *xp2; Chris@69: *yp++ = *xp1; Chris@69: xp1+=2; Chris@69: xp2-=2; Chris@69: } Chris@69: for(;ibitrev[i]] = yc; Chris@69: } Chris@69: } Chris@69: Chris@69: /* N/4 complex FFT, does not downscale anymore */ Chris@69: opus_fft_impl(st, f2); Chris@69: Chris@69: /* Post-rotate */ Chris@69: { Chris@69: /* Temp pointers to make it really clear to the compiler what we're doing */ Chris@69: const kiss_fft_cpx * OPUS_RESTRICT fp = f2; Chris@69: kiss_fft_scalar * OPUS_RESTRICT yp1 = out; Chris@69: kiss_fft_scalar * OPUS_RESTRICT yp2 = out+stride*(N2-1); Chris@69: const kiss_twiddle_scalar *t = &trig[0]; Chris@69: /* Temp pointers to make it really clear to the compiler what we're doing */ Chris@69: for(i=0;ii,t[N4+i] , fp->r,t[i]); Chris@69: yi = S_MUL_ADD(fp->r,t[N4+i] ,fp->i,t[i]); Chris@69: *yp1 = yr; Chris@69: *yp2 = yi; Chris@69: fp++; Chris@69: yp1 += 2*stride; Chris@69: yp2 -= 2*stride; Chris@69: } Chris@69: } Chris@69: RESTORE_STACK; Chris@69: } Chris@69: Chris@69: #define OVERRIDE_clt_mdct_backward Chris@69: void clt_mdct_backward(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar * OPUS_RESTRICT out, Chris@69: const opus_val16 * OPUS_RESTRICT window, int overlap, int shift, int stride, int arch) Chris@69: { Chris@69: int i; Chris@69: int N, N2, N4; Chris@69: const kiss_twiddle_scalar *trig; Chris@69: Chris@69: (void)arch; Chris@69: Chris@69: N = l->n; Chris@69: trig = l->trig; Chris@69: for (i=0;i>= 1; Chris@69: trig += N; Chris@69: } Chris@69: N2 = N>>1; Chris@69: N4 = N>>2; Chris@69: Chris@69: /* Pre-rotate */ Chris@69: { Chris@69: /* Temp pointers to make it really clear to the compiler what we're doing */ Chris@69: const kiss_fft_scalar * OPUS_RESTRICT xp1 = in; Chris@69: const kiss_fft_scalar * OPUS_RESTRICT xp2 = in+stride*(N2-1); Chris@69: kiss_fft_scalar * OPUS_RESTRICT yp = out+(overlap>>1); Chris@69: const kiss_twiddle_scalar * OPUS_RESTRICT t = &trig[0]; Chris@69: const opus_int16 * OPUS_RESTRICT bitrev = l->kfft[shift]->bitrev; Chris@69: for(i=0;ikfft[shift], (kiss_fft_cpx*)(out+(overlap>>1))); Chris@69: Chris@69: /* Post-rotate and de-shuffle from both ends of the buffer at once to make Chris@69: it in-place. */ Chris@69: { Chris@69: kiss_fft_scalar * OPUS_RESTRICT yp0 = out+(overlap>>1); Chris@69: kiss_fft_scalar * OPUS_RESTRICT yp1 = out+(overlap>>1)+N2-2; Chris@69: const kiss_twiddle_scalar *t = &trig[0]; Chris@69: /* Loop to (N4+1)>>1 to handle odd N4. When N4 is odd, the Chris@69: middle pair will be computed twice. */ Chris@69: for(i=0;i<(N4+1)>>1;i++) Chris@69: { Chris@69: kiss_fft_scalar re, im, yr, yi; Chris@69: kiss_twiddle_scalar t0, t1; Chris@69: /* We swap real and imag because we're using an FFT instead of an IFFT. */ Chris@69: re = yp0[1]; Chris@69: im = yp0[0]; Chris@69: t0 = t[i]; Chris@69: t1 = t[N4+i]; Chris@69: /* We'd scale up by 2 here, but instead it's done when mixing the windows */ Chris@69: yr = S_MUL_ADD(re,t0 , im,t1); Chris@69: yi = S_MUL_SUB(re,t1 , im,t0); Chris@69: /* We swap real and imag because we're using an FFT instead of an IFFT. */ Chris@69: re = yp1[1]; Chris@69: im = yp1[0]; Chris@69: yp0[0] = yr; Chris@69: yp1[1] = yi; Chris@69: Chris@69: t0 = t[(N4-i-1)]; Chris@69: t1 = t[(N2-i-1)]; Chris@69: /* We'd scale up by 2 here, but instead it's done when mixing the windows */ Chris@69: yr = S_MUL_ADD(re,t0,im,t1); Chris@69: yi = S_MUL_SUB(re,t1,im,t0); Chris@69: yp1[0] = yr; Chris@69: yp0[1] = yi; Chris@69: yp0 += 2; Chris@69: yp1 -= 2; Chris@69: } Chris@69: } Chris@69: Chris@69: /* Mirror on both sides for TDAC */ Chris@69: { Chris@69: kiss_fft_scalar * OPUS_RESTRICT xp1 = out+overlap-1; Chris@69: kiss_fft_scalar * OPUS_RESTRICT yp1 = out; Chris@69: const opus_val16 * OPUS_RESTRICT wp1 = window; Chris@69: const opus_val16 * OPUS_RESTRICT wp2 = window+overlap-1; Chris@69: Chris@69: for(i = 0; i < overlap/2; i++) Chris@69: { Chris@69: kiss_fft_scalar x1, x2; Chris@69: x1 = *xp1; Chris@69: x2 = *yp1; Chris@69: *yp1++ = MULT16_32_Q15(*wp2, x2) - MULT16_32_Q15(*wp1, x1); Chris@69: *xp1-- = MULT16_32_Q15(*wp1, x2) + MULT16_32_Q15(*wp2, x1); Chris@69: wp1++; Chris@69: wp2--; Chris@69: } Chris@69: } Chris@69: } Chris@69: #endif /* __MDCT_MIPSR1_H__ */