shithub: opus

Download patch

ref: 13bffd288d535766375a2f79fe0ead2d000a0873
parent: 8cf29f0991bea7460adbd90fd39d95328b9a3065
author: Timothy B. Terriberry <[email protected]>
date: Sat Jan 29 19:12:31 EST 2011

Add generic fine-energy rebalancing.

This extends the previous rebalancing for fine energy in N=1 bands
 to also allocate extra fine bits for bands that go over their cap.

--- a/libcelt/rate.c
+++ b/libcelt/rate.c
@@ -156,9 +156,7 @@
          {
             int N0;
             int max_bits;
-            int rmask;
             N0 = m->eBands[j+1]-m->eBands[j];
-            rmask = N0==1 ? (1<<shift)-1 : 0;
             /* N=1 bands only have a sign bit and fine bits. */
             if (N0<<i == 1)
               max_bits = C*(1+MAX_FINE_BITS)<<BITRES;
@@ -243,8 +241,8 @@
                celt_assert(qb >= 0);
                max_bits += C*qb<<BITRES;
             }
-            celt_assert(max_bits+rmask>>shift < 256);
-            *cap++ = (unsigned char)(max_bits+rmask>>shift);
+            celt_assert(max_bits>>shift < 256);
+            *cap++ = (unsigned char)(max_bits>>shift);
          }
       }
    }
@@ -439,13 +437,18 @@
       int N0, N, den;
       int offset;
       int NClogN;
+      int excess;
 
       celt_assert(bits[j] >= 0);
       N0 = m->eBands[j+1]-m->eBands[j];
       N=N0<<LM;
+      bits[j] += balance;
 
       if (N>1)
       {
+         excess = IMAX(bits[j]-cap[j],0);
+         bits[j] -= excess;
+
          /* Compensate for the extra DoF in stereo */
          den=(C*N+ ((C==2 && N>2) ? 1 : 0));
 
@@ -480,35 +483,40 @@
              final fine energy pass */
          fine_priority[j] = ebits[j]*(den<<BITRES) >= bits[j]+offset;
 
+         /* Remove the allocated fine bits; the rest are assigned to PVQ */
+         bits[j] -= C*ebits[j]<<BITRES;
+
       } else {
          /* For N=1, all bits go to fine energy except for a single sign bit */
-         ebits[j] = IMIN(IMAX(0,(bits[j] >> stereo >> BITRES)-1),MAX_FINE_BITS);
-         fine_priority[j] = (ebits[j]+1)*C<<BITRES >= (bits[j]-balance);
-         /* N=1 bands can't take advantage of the re-balancing in
-             quant_all_bands() because they don't have shape, only fine energy.
-            Instead, do the re-balancing here.*/
-         balance = IMAX(0,bits[j] - ((ebits[j]+1)*C<<BITRES));
-         if (j+1<codedBands)
-         {
-            bits[j] -= balance;
-            bits[j+1] += balance;
-         }
+         excess = IMAX(0,bits[j]-(C<<BITRES));
+         bits[j] -= excess;
+         ebits[j] = 0;
+         fine_priority[j] = 1;
       }
 
-      /* Sweep any bits over the cap into the first band.
-         They'll be reallocated by the normal rebalancing code, which gives
-          them the best chance to be used _somewhere_. */
+      /* Fine energy can't take advantage of the re-balancing in
+          quant_all_bands().
+         Instead, do the re-balancing here.*/
+      if(excess > 0)
       {
-         int tmp = IMAX(bits[j]-cap[j],0);
-         bits[j] -= tmp;
-         bits[start] += tmp;
+         int extra_fine;
+         int extra_bits;
+         extra_fine = IMIN(excess >> stereo+BITRES, MAX_FINE_BITS-ebits[j]);
+         ebits[j] += extra_fine;
+         extra_bits = extra_fine*C<<BITRES;
+         fine_priority[j] = extra_bits >= excess-balance;
+         excess -= extra_bits;
       }
+      balance = excess;
 
-      /* Remove the allocated fine bits; the other bits are assigned to PVQ */
-      bits[j] -= C*ebits[j]<<BITRES;
       celt_assert(bits[j] >= 0);
       celt_assert(ebits[j] >= 0);
    }
+   /* Sweep any bits over the caps into the first band.
+      They'll be reallocated by the normal rebalancing code, which gives
+       them the best chance to be used _somewhere_. */
+   bits[start]+=balance;
+
    /* The skipped bands use all their bits for fine energy. */
    for (;j<end;j++)
    {
--- a/libcelt/static_modes_fixed.c
+++ b/libcelt/static_modes_fixed.c
@@ -22,7 +22,7 @@
 static const unsigned char cache_bits50[392] = {
 40, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 40, 15, 23, 28, 31, 34, 36, 38, 39, 41, 42, 43, 44, 45, 46, 47, 47, 49, 50, 51, 52, 53, 54, 55, 55, 57, 58, 59, 60, 61, 62, 63, 63, 65, 66, 67, 68, 69, 70, 71, 71, 40, 20, 33, 41, 48, 53, 57, 61, 64, 66, 69, 71, 73, 75, 76, 78, 80, 82, 85, 87, 89, 91, 92, 94, 96, 98, 101, 103, 105, 107, 108, 110, 112, 114, 117, 119, 121, 123, 124, 126, 128, 40, 23, 39, 51, 60, 67, 73, 79, 83, 87, 91, 94, 97, 100, 102, 105, 107, 111, 115, 118, 121, 124, 126, 129, 131, 135, 139, 142, 145, 148, 150, 153, 155, 159, 163, 166, 169, 172, 174, 177, 179, 35, 28, 49, 65, 78, 89, 99, 107, 114, 120, 126, 132, 136, 141, 145, 149, 153, 159, 165, 171, 176, 180, 185, 189, 192, 199, 205, 211, 216, 220, 225, 229, 232, 239, 245, 251, 21, 33, 58, 79, 97, 112, 125, 137, 148, 157, 166, 174, 182, 189, 195, 201, 207, 217, 227, 235, 243, 251, 17, 35, 63, 86, 106, 123, 139, 152, 165, 177, 187, 197, 206, 214, 222, 230, 237, 250, 25, 31, 55, 75, 91, 105, 117, 128, 138, 146, 154, 161, 168, 174, 180, 185, 190, 200, 208, 215, 222, 229, 235, 240, 245, 255, 16, 36, 65, 89, 110, 128, 144, 159, 173, 185, 196, 207, 217, 226, 234, 242, 250, 11, 41, 74, 103, 128, 151, 172, 191, 209, 225, 241, 255, 9, 43, 79, 110, 138, 163, 186, 207, 227, 246, 12, 39, 71, 99, 123, 144, 164, 182, 198, 214, 228, 241, 253, 9, 44, 81, 113, 142, 168, 192, 214, 235, 255, 7, 49, 90, 127, 160, 191, 220, 247, 6, 51, 95, 134, 170, 203, 234, 7, 47, 87, 123, 155, 184, 212, 237, 6, 52, 97, 137, 174, 208, 240, 5, 57, 106, 151, 192, 231, 5, 59, 111, 158, 202, 243, 5, 55, 103, 147, 187, 224, 5, 60, 113, 161, 206, 248, 4, 65, 122, 175, 224, 4, 67, 127, 182, 234, };
 static const unsigned char cache_caps50[168] = {
-18, 18, 18, 18, 18, 18, 18, 18, 28, 28, 28, 28, 55, 55, 55, 92, 92, 117, 149, 142, 140, 18, 18, 18, 18, 18, 18, 18, 18, 38, 38, 38, 38, 62, 62, 62, 99, 99, 125, 157, 147, 145, 14, 14, 14, 14, 14, 14, 14, 14, 27, 27, 27, 27, 58, 58, 58, 94, 94, 120, 153, 145, 142, 19, 19, 19, 19, 19, 19, 19, 19, 31, 31, 31, 31, 62, 62, 62, 98, 98, 124, 157, 148, 144, 14, 14, 14, 14, 14, 14, 14, 14, 29, 29, 29, 29, 60, 60, 60, 95, 95, 120, 154, 147, 143, 16, 16, 16, 16, 16, 16, 16, 16, 31, 31, 31, 31, 62, 62, 62, 97, 97, 122, 156, 148, 145, 15, 15, 15, 15, 15, 15, 15, 15, 30, 30, 30, 30, 60, 60, 60, 95, 95, 121, 154, 147, 144, 16, 16, 16, 16, 16, 16, 16, 16, 31, 31, 31, 31, 61, 61, 61, 96, 96, 122, 155, 148, 145, };
+18, 18, 18, 18, 18, 18, 18, 18, 28, 28, 28, 28, 55, 55, 55, 92, 92, 117, 149, 142, 140, 18, 18, 18, 18, 18, 18, 18, 18, 38, 38, 38, 38, 62, 62, 62, 99, 99, 125, 157, 147, 145, 14, 14, 14, 14, 14, 14, 14, 14, 27, 27, 27, 27, 58, 58, 58, 94, 94, 120, 153, 145, 142, 19, 19, 19, 19, 19, 19, 19, 19, 31, 31, 31, 31, 62, 62, 62, 98, 98, 124, 157, 148, 144, 13, 13, 13, 13, 13, 13, 13, 13, 29, 29, 29, 29, 60, 60, 60, 95, 95, 120, 154, 147, 143, 15, 15, 15, 15, 15, 15, 15, 15, 31, 31, 31, 31, 62, 62, 62, 97, 97, 122, 156, 148, 145, 14, 14, 14, 14, 14, 14, 14, 14, 30, 30, 30, 30, 60, 60, 60, 95, 95, 121, 154, 147, 144, 15, 15, 15, 15, 15, 15, 15, 15, 31, 31, 31, 31, 61, 61, 61, 96, 96, 122, 155, 148, 145, };
 #endif
 
 #ifndef FFT_TWIDDLES48000_960
--- a/libcelt/static_modes_float.c
+++ b/libcelt/static_modes_float.c
@@ -22,7 +22,7 @@
 static const unsigned char cache_bits50[392] = {
 40, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 40, 15, 23, 28, 31, 34, 36, 38, 39, 41, 42, 43, 44, 45, 46, 47, 47, 49, 50, 51, 52, 53, 54, 55, 55, 57, 58, 59, 60, 61, 62, 63, 63, 65, 66, 67, 68, 69, 70, 71, 71, 40, 20, 33, 41, 48, 53, 57, 61, 64, 66, 69, 71, 73, 75, 76, 78, 80, 82, 85, 87, 89, 91, 92, 94, 96, 98, 101, 103, 105, 107, 108, 110, 112, 114, 117, 119, 121, 123, 124, 126, 128, 40, 23, 39, 51, 60, 67, 73, 79, 83, 87, 91, 94, 97, 100, 102, 105, 107, 111, 115, 118, 121, 124, 126, 129, 131, 135, 139, 142, 145, 148, 150, 153, 155, 159, 163, 166, 169, 172, 174, 177, 179, 35, 28, 49, 65, 78, 89, 99, 107, 114, 120, 126, 132, 136, 141, 145, 149, 153, 159, 165, 171, 176, 180, 185, 189, 192, 199, 205, 211, 216, 220, 225, 229, 232, 239, 245, 251, 21, 33, 58, 79, 97, 112, 125, 137, 148, 157, 166, 174, 182, 189, 195, 201, 207, 217, 227, 235, 243, 251, 17, 35, 63, 86, 106, 123, 139, 152, 165, 177, 187, 197, 206, 214, 222, 230, 237, 250, 25, 31, 55, 75, 91, 105, 117, 128, 138, 146, 154, 161, 168, 174, 180, 185, 190, 200, 208, 215, 222, 229, 235, 240, 245, 255, 16, 36, 65, 89, 110, 128, 144, 159, 173, 185, 196, 207, 217, 226, 234, 242, 250, 11, 41, 74, 103, 128, 151, 172, 191, 209, 225, 241, 255, 9, 43, 79, 110, 138, 163, 186, 207, 227, 246, 12, 39, 71, 99, 123, 144, 164, 182, 198, 214, 228, 241, 253, 9, 44, 81, 113, 142, 168, 192, 214, 235, 255, 7, 49, 90, 127, 160, 191, 220, 247, 6, 51, 95, 134, 170, 203, 234, 7, 47, 87, 123, 155, 184, 212, 237, 6, 52, 97, 137, 174, 208, 240, 5, 57, 106, 151, 192, 231, 5, 59, 111, 158, 202, 243, 5, 55, 103, 147, 187, 224, 5, 60, 113, 161, 206, 248, 4, 65, 122, 175, 224, 4, 67, 127, 182, 234, };
 static const unsigned char cache_caps50[168] = {
-18, 18, 18, 18, 18, 18, 18, 18, 28, 28, 28, 28, 55, 55, 55, 92, 92, 117, 149, 142, 140, 18, 18, 18, 18, 18, 18, 18, 18, 38, 38, 38, 38, 62, 62, 62, 99, 99, 125, 157, 147, 145, 14, 14, 14, 14, 14, 14, 14, 14, 27, 27, 27, 27, 58, 58, 58, 94, 94, 120, 153, 145, 142, 19, 19, 19, 19, 19, 19, 19, 19, 31, 31, 31, 31, 62, 62, 62, 98, 98, 124, 157, 148, 144, 14, 14, 14, 14, 14, 14, 14, 14, 29, 29, 29, 29, 60, 60, 60, 95, 95, 120, 154, 147, 143, 16, 16, 16, 16, 16, 16, 16, 16, 31, 31, 31, 31, 62, 62, 62, 97, 97, 122, 156, 148, 145, 15, 15, 15, 15, 15, 15, 15, 15, 30, 30, 30, 30, 60, 60, 60, 95, 95, 121, 154, 147, 144, 16, 16, 16, 16, 16, 16, 16, 16, 31, 31, 31, 31, 61, 61, 61, 96, 96, 122, 155, 148, 145, };
+18, 18, 18, 18, 18, 18, 18, 18, 28, 28, 28, 28, 55, 55, 55, 92, 92, 117, 149, 142, 140, 18, 18, 18, 18, 18, 18, 18, 18, 38, 38, 38, 38, 62, 62, 62, 99, 99, 125, 157, 147, 145, 14, 14, 14, 14, 14, 14, 14, 14, 27, 27, 27, 27, 58, 58, 58, 94, 94, 120, 153, 145, 142, 19, 19, 19, 19, 19, 19, 19, 19, 31, 31, 31, 31, 62, 62, 62, 98, 98, 124, 157, 148, 144, 13, 13, 13, 13, 13, 13, 13, 13, 29, 29, 29, 29, 60, 60, 60, 95, 95, 120, 154, 147, 143, 15, 15, 15, 15, 15, 15, 15, 15, 31, 31, 31, 31, 62, 62, 62, 97, 97, 122, 156, 148, 145, 14, 14, 14, 14, 14, 14, 14, 14, 30, 30, 30, 30, 60, 60, 60, 95, 95, 121, 154, 147, 144, 15, 15, 15, 15, 15, 15, 15, 15, 31, 31, 31, 31, 61, 61, 61, 96, 96, 122, 155, 148, 145, };
 #endif
 
 #ifndef FFT_TWIDDLES48000_960