Fix stereo support; correctly deallocate bits_stereo.
authorGregory Maxwell <greg@xiph.org>
Fri, 3 Oct 2008 21:12:44 +0000 (17:12 -0400)
committerJean-Marc Valin <jean-marc.valin@usherbrooke.ca>
Sat, 4 Oct 2008 04:39:17 +0000 (00:39 -0400)
libcelt/bands.c
libcelt/modes.c

index f571488..3cb6a13 100644 (file)
@@ -355,9 +355,15 @@ void quant_bands(const CELTMode *m, celt_norm_t * restrict X, celt_norm_t *P, ce
       int tell;
       int q;
       celt_word16_t n;
       int tell;
       int q;
       celt_word16_t n;
+      celt_int16_t * const *BPbits;
       
       int curr_balance, curr_bits;
       
       
       int curr_balance, curr_bits;
       
+      if (C>1 && stereo_mode[i]==0)
+         BPbits = m->bits_stereo;
+      else
+         BPbits = m->bits;
+
       tell = ec_enc_tell(enc, 4);
       if (i != 0)
          balance -= tell;
       tell = ec_enc_tell(enc, 4);
       if (i != 0)
          balance -= tell;
@@ -366,14 +372,14 @@ void quant_bands(const CELTMode *m, celt_norm_t * restrict X, celt_norm_t *P, ce
       if (curr_balance > 3)
          curr_balance = 3;
       curr_balance = balance / curr_balance;
       if (curr_balance > 3)
          curr_balance = 3;
       curr_balance = balance / curr_balance;
-      q = bits2pulses(m, m->bits[i], pulses[i]+curr_balance);
-      curr_bits = m->bits[i][q];
+      q = bits2pulses(m, BPbits[i], pulses[i]+curr_balance);
+      curr_bits = BPbits[i][q];
       remaining_bits -= curr_bits;
       if (remaining_bits < 0)
       {
          q--;
          remaining_bits += curr_bits;
       remaining_bits -= curr_bits;
       if (remaining_bits < 0)
       {
          q--;
          remaining_bits += curr_bits;
-         curr_bits = m->bits[i][q];
+         curr_bits = BPbits[i][q];
          remaining_bits -= curr_bits;
       }
       balance += pulses[i] + tell;
          remaining_bits -= curr_bits;
       }
       balance += pulses[i] + tell;
@@ -434,9 +440,15 @@ void unquant_bands(const CELTMode *m, celt_norm_t * restrict X, celt_norm_t *P,
       int tell;
       int q;
       celt_word16_t n;
       int tell;
       int q;
       celt_word16_t n;
+      celt_int16_t * const *BPbits;
       
       int curr_balance, curr_bits;
       
       
       int curr_balance, curr_bits;
       
+      if (C>1 && stereo_mode[i]==0)
+         BPbits = m->bits_stereo;
+      else
+         BPbits = m->bits;
+
       tell = ec_dec_tell(dec, 4);
       if (i != 0)
          balance -= tell;
       tell = ec_dec_tell(dec, 4);
       if (i != 0)
          balance -= tell;
@@ -445,14 +457,14 @@ void unquant_bands(const CELTMode *m, celt_norm_t * restrict X, celt_norm_t *P,
       if (curr_balance > 3)
          curr_balance = 3;
       curr_balance = balance / curr_balance;
       if (curr_balance > 3)
          curr_balance = 3;
       curr_balance = balance / curr_balance;
-      q = bits2pulses(m, m->bits[i], pulses[i]+curr_balance);
-      curr_bits = m->bits[i][q];
+      q = bits2pulses(m, BPbits[i], pulses[i]+curr_balance);
+      curr_bits = BPbits[i][q];
       remaining_bits -= curr_bits;
       if (remaining_bits < 0)
       {
          q--;
          remaining_bits += curr_bits;
       remaining_bits -= curr_bits;
       if (remaining_bits < 0)
       {
          q--;
          remaining_bits += curr_bits;
-         curr_bits = m->bits[i][q];
+         curr_bits = BPbits[i][q];
          remaining_bits -= curr_bits;
       }
       balance += pulses[i] + tell;
          remaining_bits -= curr_bits;
       }
       balance += pulses[i] + tell;
index d1dcc7c..bde7322 100644 (file)
@@ -394,7 +394,6 @@ CELTMode *celt_mode_create(celt_int32_t Fs, int channels, int frame_size, int *e
    if (mode->nbChannels>=2)
       mode->bits_stereo = (const celt_int16_t **)compute_alloc_cache(mode, mode->nbChannels);
 
    if (mode->nbChannels>=2)
       mode->bits_stereo = (const celt_int16_t **)compute_alloc_cache(mode, mode->nbChannels);
 
-   mode->bits_stereo = NULL;
 #ifndef SHORTCUTS
    psydecay_init(&mode->psy, MAX_PERIOD/2, mode->Fs);
 #endif
 #ifndef SHORTCUTS
    psydecay_init(&mode->psy, MAX_PERIOD/2, mode->Fs);
 #endif
@@ -430,6 +429,18 @@ void celt_mode_destroy(CELTMode *mode)
       }
    }
    celt_free((int**)mode->bits);
       }
    }
    celt_free((int**)mode->bits);
+   if (mode->bits_stereo != NULL)
+   {
+      for (i=0;i<mode->nbEBands;i++)
+      {
+         if (mode->bits_stereo[i] != prevPtr)
+         {
+            prevPtr = mode->bits_stereo[i];
+            celt_free((int*)mode->bits_stereo[i]);
+         }
+      }
+      celt_free((int**)mode->bits_stereo);
+   }
    if (check_mode(mode) != CELT_OK)
       return;
    celt_free((int*)mode->eBands);
    if (check_mode(mode) != CELT_OK)
       return;
    celt_free((int*)mode->eBands);