shithub: opus

Download patch

ref: 9d35ccdaea23f2b46411eeff815d99e6c4679dc5
parent: bf94045f4126f4ae90c6491e0d66d173cdb92e16
author: Jean-Marc Valin <[email protected]>
date: Mon Dec 10 12:57:19 EST 2007

Fixed stereo version of the pitch estimator

--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -88,7 +88,7 @@
    ec_enc_init(&st->enc,&st->buf);
 
    mdct_init(&st->mdct_lookup, 2*N);
-   st->fft = spx_fft_init(MAX_PERIOD);
+   st->fft = spx_fft_init(MAX_PERIOD*C);
    
    st->window = celt_alloc(2*N*sizeof(float));
    st->in_mem = celt_alloc(N*C*sizeof(float));
@@ -237,7 +237,7 @@
          in[C*(B*N+i)+c] *= st->window[N+i];
       }
    }
-   find_spectral_pitch(st->fft, in, st->out_mem, MAX_PERIOD, (B+1)*N, &pitch_index);
+   find_spectral_pitch(st->fft, in, st->out_mem, MAX_PERIOD, (B+1)*N, C, &pitch_index);
    ec_enc_uint(&st->enc, pitch_index, MAX_PERIOD-(B+1)*N);
    
    /* Compute MDCTs of the pitch part */
@@ -314,6 +314,8 @@
          {
             float tmp = st->out_mem[C*(MAX_PERIOD+(i-B)*N)+C*j+c] + st->preemph*st->preemph_memD[c];
             st->preemph_memD[c] = tmp;
+            if (tmp > 32767) tmp = 32767;
+            if (tmp < -32767) tmp = -32767;
             pcm[C*i*N+C*j+c] = (short)floor(.5+tmp);
          }
       }
@@ -443,6 +445,8 @@
          {
             float tmp = st->out_mem[C*(MAX_PERIOD+(i-B)*N)+C*j+c] + st->preemph*st->preemph_memD[c];
             st->preemph_memD[c] = tmp;
+            if (tmp > 32767) tmp = 32767;
+            if (tmp < -32767) tmp = -32767;
             pcm[C*i*N+C*j+c] = (short)floor(.5+tmp);
          }
       }
@@ -520,6 +524,8 @@
          {
             float tmp = st->out_mem[C*(MAX_PERIOD+(i-B)*N)+C*j+c] + st->preemph*st->preemph_memD[c];
             st->preemph_memD[c] = tmp;
+            if (tmp > 32767) tmp = 32767;
+            if (tmp < -32767) tmp = -32767;
             pcm[C*i*N+C*j+c] = (short)floor(.5+tmp);
          }
       }
--- a/libcelt/pitch.c
+++ b/libcelt/pitch.c
@@ -27,27 +27,34 @@
 #include "pitch.h"
 #include "psy.h"
 
-void find_spectral_pitch(void *fft, float *x, float *y, int lag, int len, int *pitch)
+void find_spectral_pitch(void *fft, float *x, float *y, int lag, int len, int C, int *pitch)
 {
+   int c;
    int n2 = lag/2;
-   float xx[lag];
-   float X[lag];
-   float Y[lag];
-   float curve[n2];
+   float xx[lag*C];
+   float yy[lag*C];
+   float X[lag*C];
+   float Y[lag*C];
+   float curve[n2*C];
    int i;
    
-   for (i=0;i<lag;i++)
+   for (i=0;i<C*lag;i++)
       xx[i] = 0;
-   for (i=0;i<len;i++)
-      xx[i] = x[i];
-   
+   for (c=0;c<C;c++)
+   {
+      for (i=0;i<len;i++)
+         xx[c*lag+i] = x[C*i+c];
+      for (i=0;i<lag;i++)
+         yy[c*lag+i] = y[C*i+c];
+      
+   }
    spx_fft(fft, xx, X);
-   spx_fft(fft, y, Y);
+   spx_fft(fft, yy, Y);
    
-   compute_masking(X, curve, lag, 44100);
+   compute_masking(X, curve, lag*C, 44100);
    
    X[0] = 0;
-   for (i=1;i<lag/2;i++)
+   for (i=1;i<C*n2;i++)
    {
       float n;
       //n = 1.f/(1e1+sqrt(sqrt((X[2*i-1]*X[2*i-1] + X[2*i  ]*X[2*i  ])*(Y[2*i-1]*Y[2*i-1] + Y[2*i  ]*Y[2*i  ]))));
@@ -58,8 +65,8 @@
       X[2*i-1] = (X[2*i-1]*Y[2*i-1] + X[2*i  ]*Y[2*i  ])*n;
       X[2*i  ] = (- X[2*i  ]*Y[2*i-1] + tmp*Y[2*i  ])*n;
    }
-   X[lag-1] = 0;
-   X[0] = X[lag-1] = 0;
+   X[C*lag-1] = 0;
+   X[0] = X[C*lag-1] = 0;
    spx_ifft(fft, X, xx);
    
    float max_corr=-1e10;
--- a/libcelt/pitch.h
+++ b/libcelt/pitch.h
@@ -23,6 +23,6 @@
 #ifndef _PITCH_H
 #define _PITCH_H
 
-void find_spectral_pitch(void *fft, float *x, float *y, int lag, int len, int *pitch);
+void find_spectral_pitch(void *fft, float *x, float *y, int lag, int len, int C, int *pitch);
 
 #endif
--- a/libcelt/testcelt.c
+++ b/libcelt/testcelt.c
@@ -59,9 +59,9 @@
    {
       fread(in, sizeof(short), FRAME_SIZE*CHANNELS, fin);
       celt_encode(enc, in);
+#if 1
       data = celt_encoder_get_bytes(enc, &len);
       //printf ("%d\n", len);
-#if 1
       /* this is to simulate packet loss */
       if (rand()%100==-1)
          celt_decode(dec, NULL, len, in);