media/libopus/celt/mdct.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial