shithub: opus

Download patch

ref: 6f18d11148fa1ad3ef0aac297b4714089dc4e236
parent: 0ac2b2fe0b7354b4a4b293adbfa3d20be8b62df2
author: Jean-Marc Valin <[email protected]>
date: Wed May 20 18:50:46 EDT 2009

Adds support for up to 32767 pulses for the N=3 case (disabled for now)

--- a/libcelt/bands.c
+++ b/libcelt/bands.c
@@ -421,6 +421,7 @@
    for (i=0;i<m->nbEBands;i++)
    {
       int tell;
+      int N;
       int q;
       celt_word16_t n;
       const celt_int16_t * const *BPbits;
@@ -427,6 +428,7 @@
       
       int curr_balance, curr_bits;
       
+      N = eBands[i+1]-eBands[i];
       BPbits = m->bits;
 
       tell = ec_enc_tell(enc, 4);
@@ -437,14 +439,14 @@
       if (curr_balance > 3)
          curr_balance = 3;
       curr_balance = balance / curr_balance;
-      q = bits2pulses(m, BPbits[i], pulses[i]+curr_balance);
-      curr_bits = BPbits[i][q];
+      q = bits2pulses(m, BPbits[i], N, pulses[i]+curr_balance);
+      curr_bits = pulses2bits(BPbits[i], N, q);
       remaining_bits -= curr_bits;
       while (remaining_bits < 0 && q > 0)
       {
          remaining_bits += curr_bits;
          q--;
-         curr_bits = BPbits[i][q];
+         curr_bits = pulses2bits(BPbits[i], N, q);
          remaining_bits -= curr_bits;
       }
       balance += pulses[i] + tell;
@@ -545,14 +547,14 @@
 
       if (N<5) {
          
-         q1 = bits2pulses(m, BPbits[i], b/2);
-         curr_bits = 2*BPbits[i][q1];
+         q1 = bits2pulses(m, BPbits[i], N, b/2);
+         curr_bits = 2*pulses2bits(BPbits[i], N, q1);
          remaining_bits -= curr_bits;
          while (remaining_bits < 0 && q1 > 0)
          {
             remaining_bits += curr_bits;
             q1--;
-            curr_bits = 2*BPbits[i][q1];
+            curr_bits = 2*pulses2bits(BPbits[i], N, q1);
             remaining_bits -= curr_bits;
          }
          balance += pulses[i] + tell;
@@ -653,9 +655,9 @@
       if (mbits<0)
          mbits=0;
       sbits = b-qalloc-mbits;
-      q1 = bits2pulses(m, BPbits[i], mbits);
-      q2 = bits2pulses(m, BPbits[i], sbits);
-      curr_bits = BPbits[i][q1]+BPbits[i][q2]+qalloc;
+      q1 = bits2pulses(m, BPbits[i], N, mbits);
+      q2 = bits2pulses(m, BPbits[i], N, sbits);
+      curr_bits = pulses2bits(BPbits[i], N, q1)+pulses2bits(BPbits[i], N, q2)+qalloc;
       remaining_bits -= curr_bits;
       while (remaining_bits < 0 && (q1 > 0 || q2 > 0))
       {
@@ -663,10 +665,10 @@
          if (q1>q2)
          {
             q1--;
-            curr_bits = BPbits[i][q1]+BPbits[i][q2]+qalloc;
+            curr_bits = pulses2bits(BPbits[i], N, q1)+pulses2bits(BPbits[i], N, q2)+qalloc;
          } else {
             q2--;
-            curr_bits = BPbits[i][q1]+BPbits[i][q2]+qalloc;
+            curr_bits = pulses2bits(BPbits[i], N, q1)+pulses2bits(BPbits[i], N, q2)+qalloc;
          }
          remaining_bits -= curr_bits;
       }
@@ -775,12 +777,14 @@
    for (i=0;i<m->nbEBands;i++)
    {
       int tell;
+      int N;
       int q;
       celt_word16_t n;
       const celt_int16_t * const *BPbits;
       
       int curr_balance, curr_bits;
-      
+
+      N = eBands[i+1]-eBands[i];
       BPbits = m->bits;
 
       tell = ec_dec_tell(dec, 4);
@@ -791,14 +795,14 @@
       if (curr_balance > 3)
          curr_balance = 3;
       curr_balance = balance / curr_balance;
-      q = bits2pulses(m, BPbits[i], pulses[i]+curr_balance);
-      curr_bits = BPbits[i][q];
+      q = bits2pulses(m, BPbits[i], N, pulses[i]+curr_balance);
+      curr_bits = pulses2bits(BPbits[i], N, q);
       remaining_bits -= curr_bits;
       while (remaining_bits < 0 && q > 0)
       {
          remaining_bits += curr_bits;
          q--;
-         curr_bits = BPbits[i][q];
+         curr_bits = pulses2bits(BPbits[i], N, q);
          remaining_bits -= curr_bits;
       }
       balance += pulses[i] + tell;
@@ -898,14 +902,14 @@
       
       if (N<5) {
          
-         q1 = bits2pulses(m, BPbits[i], b/2);
-         curr_bits = 2*BPbits[i][q1];
+         q1 = bits2pulses(m, BPbits[i], N, b/2);
+         curr_bits = 2*pulses2bits(BPbits[i], N, q1);
          remaining_bits -= curr_bits;
          while (remaining_bits < 0 && q1 > 0)
          {
             remaining_bits += curr_bits;
             q1--;
-            curr_bits = 2*BPbits[i][q1];
+            curr_bits = 2*pulses2bits(BPbits[i], N, q1);
             remaining_bits -= curr_bits;
          }
          balance += pulses[i] + tell;
@@ -992,9 +996,9 @@
       if (mbits<0)
          mbits=0;
       sbits = b-qalloc-mbits;
-      q1 = bits2pulses(m, BPbits[i], mbits);
-      q2 = bits2pulses(m, BPbits[i], sbits);
-      curr_bits = BPbits[i][q1]+BPbits[i][q2]+qalloc;
+      q1 = bits2pulses(m, BPbits[i], N, mbits);
+      q2 = bits2pulses(m, BPbits[i], N, sbits);
+      curr_bits = pulses2bits(BPbits[i], N, q1)+pulses2bits(BPbits[i], N, q2)+qalloc;
       remaining_bits -= curr_bits;
       while (remaining_bits < 0 && (q1 > 0 || q2 > 0))
       {
@@ -1002,10 +1006,10 @@
          if (q1>q2)
          {
             q1--;
-            curr_bits = BPbits[i][q1]+BPbits[i][q2]+qalloc;
+            curr_bits = pulses2bits(BPbits[i], N, q1)+pulses2bits(BPbits[i], N, q2)+qalloc;
          } else {
             q2--;
-            curr_bits = BPbits[i][q1]+BPbits[i][q2]+qalloc;
+            curr_bits = pulses2bits(BPbits[i], N, q1)+pulses2bits(BPbits[i], N, q2)+qalloc;
          }
          remaining_bits -= curr_bits;
       }
--- a/libcelt/rate.h
+++ b/libcelt/rate.h
@@ -39,7 +39,9 @@
 #define BITROUND 8
 #define BITOVERFLOW 30000
 
-static inline int bits2pulses(const CELTMode *m, const celt_int16_t *cache, int bits)
+#include "cwrs.h"
+
+static inline int bits2pulses(const CELTMode *m, const celt_int16_t *cache, int N, int bits)
 {
    int i;
    int lo, hi;
@@ -46,6 +48,25 @@
    lo = 0;
    hi = MAX_PULSES-1;
    
+   /* Use of more than MAX_PULSES is disabled until we are able to cwrs that decently */
+   if (0 && bits > cache[MAX_PULSES-1] && N==3)
+   {
+      /*int pulses;
+      pulses = 127;
+      while (16 + log2_frac(2*(pulses+1)*(pulses+1) + 1, 4) <= bits && pulses < 32767)
+         pulses++;*/
+      lo = 127;
+      hi = 32767;
+      for (i=0;i<15;i++)
+      {
+         int pulses = (lo+hi)>>1;
+         if (16 + log2_frac(2*pulses*pulses + 1, 4) > bits)
+            hi = pulses;
+         else
+            lo = pulses;
+      }
+      return lo;
+   }
    /* Instead of using the "bisection condition" we use a fixed number of 
    iterations because it should be faster */
    /*while (hi-lo != 1)*/
@@ -62,6 +83,21 @@
       return lo;
    else
       return hi;
+}
+
+
+static inline int pulses2bits(const celt_int16_t *cache, int N, int pulses)
+{
+   /* Use of more than MAX_PULSES is disabled until we are able to cwrs that decently */
+   if (0 && pulses > 127)
+   {
+      int bits;
+      celt_assert (N==3);
+      bits = 16 + log2_frac(2*pulses*pulses + 1, 4);
+      /*printf ("%d <- %d\n", bits, pulses);*/
+      return bits;
+   }
+   return cache[pulses];
 }
 
 /** Computes a cache of the pulses->bits mapping in each band */