shithub: opus

Download patch

ref: a5431bfb090b3b3d31744265a86a1d8684c1e34d
parent: 71c9bbff02edd1b8818b782ebf498bd67e183027
author: Jean-Marc Valin <[email protected]>
date: Thu Jan 3 15:53:01 EST 2008

Allowing reduced overlap MDCTs as an alternative
to multiple MDCTs per frame. Not complete yet.

--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -50,6 +50,7 @@
    int frame_size;
    int block_size;
    int nb_blocks;
+   int overlap;
    int channels;
    int Fs;
    
@@ -75,7 +76,7 @@
 
 CELTEncoder *celt_encoder_new(const CELTMode *mode)
 {
-   int i, N, B, C;
+   int i, N, B, C, N4;
    N = mode->mdctSize;
    B = mode->nbMdctBlocks;
    C = mode->nbChannels;
@@ -85,8 +86,10 @@
    st->frame_size = B*N;
    st->block_size = N;
    st->nb_blocks  = B;
+   st->overlap = mode->overlap;
    st->Fs = 44100;
-   
+
+   N4 = (N-st->overlap)/2;
    ec_byte_writeinit(&st->buf);
    ec_enc_init(&st->enc,&st->buf);
 
@@ -97,9 +100,13 @@
    st->in_mem = celt_alloc(N*C*sizeof(float));
    st->mdct_overlap = celt_alloc(N*C*sizeof(float));
    st->out_mem = celt_alloc(MAX_PERIOD*C*sizeof(float));
-   for (i=0;i<N;i++)
-      st->window[i] = st->window[2*N-i-1] = sin(.5*M_PI* sin(.5*M_PI*(i+.5)/N) * sin(.5*M_PI*(i+.5)/N));
-   
+   for (i=0;i<2*N;i++)
+      st->window[i] = 0;
+   for (i=0;i<st->overlap;i++)
+      st->window[N4+i] = st->window[2*N-N4-i-1] 
+            = sin(.5*M_PI* sin(.5*M_PI*(i+.5)/st->overlap) * sin(.5*M_PI*(i+.5)/st->overlap));
+   for (i=0;i<2*N4;i++)
+      st->window[N-N4+i] = 1;
    st->oldBandE = celt_alloc(mode->nbEBands*sizeof(float));
 
    st->preemph = 0.8;
@@ -220,12 +227,12 @@
 
 int celt_encode(CELTEncoder *st, short *pcm)
 {
-   int i, c, N, B, C;
+   int i, c, N, B, C, N4;
    N = st->block_size;
    B = st->nb_blocks;
    C = st->mode->nbChannels;
    float in[(B+1)*C*N];
-   
+
    float X[B*C*N];         /**< Interleaved signal MDCTs */
    float P[B*C*N];         /**< Interleaved pitch MDCTs*/
    float mask[B*C*N];      /**< Masking curve */
@@ -232,19 +239,25 @@
    float bandE[st->mode->nbEBands];
    float gains[st->mode->nbPBands];
    int pitch_index;
-   
+
+   N4 = (N-st->overlap)/2;
+
    for (c=0;c<C;c++)
    {
-      for (i=0;i<N;i++)
-         in[C*i+c] = st->in_mem[C*i+c];
-      for (;i<(B+1)*N;i++)
+      for (i=0;i<N4;i++)
+         in[C*i+c] = 0;
+      for (i=0;i<st->overlap;i++)
+         in[C*(i+N4)+c] = st->in_mem[C*i+c];
+      for (i=0;i<B*N;i++)
       {
-         float tmp = pcm[C*(i-N)+c];
-         in[C*i+c] = tmp - st->preemph*st->preemph_memE[c];
+         float tmp = pcm[C*i+c];
+         in[C*(i+st->overlap+N4)+c] = tmp - st->preemph*st->preemph_memE[c];
          st->preemph_memE[c] = tmp;
       }
-      for (i=0;i<N;i++)
-         st->in_mem[C*i+c] = in[C*(B*N+i)+c];
+      for (i=N*(B+1)-N4;i<N*(B+1);i++)
+         in[C*i+c] = 0;
+      for (i=0;i<st->overlap;i++)
+         st->in_mem[C*i+c] = in[C*(N*(B+1)-N4-st->overlap+i)+c];
    }
    //for (i=0;i<(B+1)*C*N;i++) printf ("%f(%d) ", in[i], i); printf ("\n");
    /* Compute MDCTs */
@@ -375,7 +388,8 @@
    int frame_size;
    int block_size;
    int nb_blocks;
-   
+   int overlap;
+
    ec_byte_buffer buf;
    ec_enc         enc;
 
@@ -395,7 +409,7 @@
 
 CELTDecoder *celt_decoder_new(const CELTMode *mode)
 {
-   int i, N, B, C;
+   int i, N, B, C, N4;
    N = mode->mdctSize;
    B = mode->nbMdctBlocks;
    C = mode->nbChannels;
@@ -405,6 +419,9 @@
    st->frame_size = B*N;
    st->block_size = N;
    st->nb_blocks  = B;
+   st->overlap = mode->overlap;
+
+   N4 = (N-st->overlap)/2;
    
    mdct_init(&st->mdct_lookup, 2*N);
    
@@ -411,8 +428,14 @@
    st->window = celt_alloc(2*N*sizeof(float));
    st->mdct_overlap = celt_alloc(N*C*sizeof(float));
    st->out_mem = celt_alloc(MAX_PERIOD*C*sizeof(float));
-   for (i=0;i<N;i++)
-      st->window[i] = st->window[2*N-i-1] = sin(.5*M_PI* sin(.5*M_PI*(i+.5)/N) * sin(.5*M_PI*(i+.5)/N));
+
+   for (i=0;i<2*N;i++)
+      st->window[i] = 0;
+   for (i=0;i<st->overlap;i++)
+      st->window[N4+i] = st->window[2*N-N4-i-1] 
+            = sin(.5*M_PI* sin(.5*M_PI*(i+.5)/st->overlap) * sin(.5*M_PI*(i+.5)/st->overlap));
+   for (i=0;i<2*N4;i++)
+      st->window[N-N4+i] = 1;
    
    st->oldBandE = celt_alloc(mode->nbEBands*sizeof(float));
 
--- a/libcelt/modes.c
+++ b/libcelt/modes.c
@@ -71,12 +71,15 @@
 
 const int pbank1[PBANDS128+2] = {0, 4, 8, 12, 20, PITCH_END128, 128};
 
-const int qbank3[NBANDS128+2] = {0, 4, 8, 12, 16, 24, 32, 40, 48, 56, 72, 88, 104, 126, 168, 232, 256};
+
+#define NBANDS256 15
+#define PBANDS256 5
 #define PITCH_END256 72
-const int pbank3[PBANDS128+2] = {0, 8, 16, 24, 40, PITCH_END256, 256};
+const int qbank3[NBANDS256+2] = {0, 4, 8, 12, 16, 24, 32, 40, 48, 56, 72, 88, 104, 126, 168, 232, 256};
+const int pbank3[PBANDS256+2] = {0, 8, 16, 24, 40, PITCH_END256, 256};
 
 const CELTMode mode0 = {
-   256,         /**< frameSize */
+   128,         /**< overlap */
    128,         /**< mdctSize */
    2,           /**< nbMdctBlocks */
    1,           /**< channels */
@@ -97,7 +100,7 @@
 
 /* Approx 38 kbps @ 44.1 kHz */
 const CELTMode mode1 = {
-   256,         /**< frameSize */
+   128,         /**< overlap */
    128,         /**< mdctSize */
    2,           /**< nbMdctBlocks */
    1,           /**< channels */
@@ -117,7 +120,7 @@
 
 /* Approx 58 kbps @ 44.1 kHz */
 const CELTMode mode2 = {
-   256,         /**< frameSize */
+   128,         /**< overlap */
    128,         /**< mdctSize */
    2,           /**< nbMdctBlocks */
    1,           /**< channels */
@@ -136,18 +139,18 @@
 };
 
 const CELTMode mode3 = {
-   512,         /**< frameSize */
+   128,         /**< overlap */
    256,         /**< mdctSize */
-   2,           /**< nbMdctBlocks */
+   1,           /**< nbMdctBlocks */
    1,           /**< channels */
    
-   NBANDS128,   /**< nbEBands */
-   PBANDS128,   /**< nbPBands */
+   NBANDS256,   /**< nbEBands */
+   PBANDS256,   /**< nbPBands */
    PITCH_END256,/**< pitchEnd */
    
    qbank3,      /**< eBands */
    pbank3,      /**< pBands*/
-   qpulses2,    /**< nbPulses */
+   qpulses1,    /**< nbPulses */
    
    0.7,         /**< ePredCoef */
    means,       /**< eMeans */
@@ -156,7 +159,7 @@
 
 /* Stereo mode around 120 kbps */
 const CELTMode mode4 = {
-   256,         /**< frameSize */
+   128,         /**< overlap */
    128,         /**< mdctSize */
    2,           /**< nbMdctBlocks */
    2,           /**< channels */
--- a/libcelt/modes.h
+++ b/libcelt/modes.h
@@ -35,7 +35,7 @@
 #include "celt.h"
 
 struct CELTMode {
-   int          frameSize;
+   int          overlap;
    int          mdctSize;
    int          nbMdctBlocks;
    int          nbChannels;