Fix a minor, but bitstream-affecting bug
[opus.git] / libcelt / mdct.c
index b2b82f8..428a980 100644 (file)
    and scaling in many places. 
 */
 
+#ifndef SKIP_CONFIG_H
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
+#endif
 
 #include "mdct.h"
 #include "kiss_fft.h"
 #define M_PI 3.141592653
 #endif
 
+#ifndef STATIC_MODES
+
 void clt_mdct_init(mdct_lookup *l,int N, int maxshift)
 {
    int i;
    int N2, N4;
+   kiss_twiddle_scalar *trig;
    l->n = N;
    N2 = N>>1;
    N4 = N>>2;
-   l->kfft = celt_alloc(sizeof(kiss_fft_cfg)*(maxshift+1));
    l->maxshift = maxshift;
    for (i=0;i<=maxshift;i++)
    {
@@ -79,16 +83,16 @@ void clt_mdct_init(mdct_lookup *l,int N, int maxshift)
          return;
 #endif
    }
-   l->trig = (kiss_twiddle_scalar*)celt_alloc((N4+1)*sizeof(kiss_twiddle_scalar));
+   l->trig = trig = (kiss_twiddle_scalar*)celt_alloc((N4+1)*sizeof(kiss_twiddle_scalar));
    if (l->trig==NULL)
      return;
    /* We have enough points that sine isn't necessary */
 #if defined(FIXED_POINT)
    for (i=0;i<=N4;i++)
-      l->trig[i] = TRIG_UPSCALE*celt_cos_norm(DIV32(ADD32(SHL32(EXTEND32(i),17),N2),N));
+      trig[i] = TRIG_UPSCALE*celt_cos_norm(DIV32(ADD32(SHL32(EXTEND32(i),17),N2),N));
 #else
    for (i=0;i<=N4;i++)
-      l->trig[i] = cos(2*M_PI*i/N);
+      trig[i] = (kiss_twiddle_scalar)cos(2*M_PI*i/N);
 #endif
 }
 
@@ -97,10 +101,11 @@ void clt_mdct_clear(mdct_lookup *l)
    int i;
    for (i=0;i<=l->maxshift;i++)
       kiss_fft_free(l->kfft[i]);
-   celt_free(l->kfft);
-   celt_free(l->trig);
+   celt_free((kiss_twiddle_scalar*)l->trig);
 }
 
+#endif /* STATIC_MODES */
+
 void clt_mdct_forward(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar * restrict out, const celt_word16 *window, int overlap, int shift)
 {
    int i;
@@ -117,7 +122,7 @@ void clt_mdct_forward(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar
 #ifdef FIXED_POINT
    sine = TRIG_UPSCALE*(QCONST16(0.7853981f, 15)+N2)/N;
 #else
-   sine = 2*M_PI*(.125f)/N;
+   sine = (kiss_twiddle_scalar)2*M_PI*(.125f)/N;
 #endif
 
    /* Consider the input to be composed of four blocks: [a, b, c, d] */
@@ -163,7 +168,7 @@ void clt_mdct_forward(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar
    /* Pre-rotation */
    {
       kiss_fft_scalar * restrict yp = out;
-      kiss_fft_scalar *t = &l->trig[0];
+      const kiss_twiddle_scalar *t = &l->trig[0];
       for(i=0;i<N4;i++)
       {
          kiss_fft_scalar re, im, yr, yi;
@@ -186,7 +191,7 @@ void clt_mdct_forward(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar
       const kiss_fft_scalar * restrict fp = f;
       kiss_fft_scalar * restrict yp1 = out;
       kiss_fft_scalar * restrict yp2 = out+N2-1;
-      kiss_fft_scalar *t = &l->trig[0];
+      const kiss_twiddle_scalar *t = &l->trig[0];
       /* Temp pointers to make it really clear to the compiler what we're doing */
       for(i=0;i<N4;i++)
       {
@@ -223,7 +228,7 @@ void clt_mdct_backward(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scala
 #ifdef FIXED_POINT
    sine = TRIG_UPSCALE*(QCONST16(0.7853981f, 15)+N2)/N;
 #else
-   sine = 2*M_PI*(.125f)/N;
+   sine = (kiss_twiddle_scalar)2*M_PI*(.125f)/N;
 #endif
    
    /* Pre-rotate */
@@ -232,7 +237,7 @@ void clt_mdct_backward(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scala
       const kiss_fft_scalar * restrict xp1 = in;
       const kiss_fft_scalar * restrict xp2 = in+N2-1;
       kiss_fft_scalar * restrict yp = f2;
-      kiss_fft_scalar *t = &l->trig[0];
+      const kiss_twiddle_scalar *t = &l->trig[0];
       for(i=0;i<N4;i++) 
       {
          kiss_fft_scalar yr, yi;
@@ -252,7 +257,7 @@ void clt_mdct_backward(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scala
    /* Post-rotate */
    {
       kiss_fft_scalar * restrict fp = f;
-      kiss_fft_scalar *t = &l->trig[0];
+      const kiss_twiddle_scalar *t = &l->trig[0];
 
       for(i=0;i<N4;i++)
       {
@@ -280,7 +285,7 @@ void clt_mdct_backward(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scala
          fp2 -= 2;
       }
    }
-
+   out -= (N2-overlap)>>1;
    /* Mirror on both sides for TDAC */
    {
       kiss_fft_scalar * restrict fp1 = f2+N4-1;