shithub: opus

Download patch

ref: dfd6e714f97712bbfdf82fd78ff8bcf44ef08dfe
parent: 85bbab0b4a15d144930ce47d8bc1c0d5d99ec371
author: Jean-Marc Valin <[email protected]>
date: Thu Dec 9 18:23:34 EST 2010

Adding some hysteresis on the folding threshold frequency

This adds some side-information that can be used to change the
threshold freq arbitrarily.

--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -80,6 +80,7 @@
    int fold_decision;
    int delayedIntra;
    int tonal_average;
+   int lastCodedBands;
 
    int prefilter_period;
    celt_word16 prefilter_gain;
@@ -740,6 +741,7 @@
    int intensity=0;
    int dual_stereo=0;
    int effectiveBytes;
+   int skip;
    SAVE_STACK;
 
    if (nbCompressedBytes<0 || pcm==NULL)
@@ -1103,8 +1105,15 @@
    ALLOC(pulses, st->mode->nbEBands, int);
    ALLOC(fine_priority, st->mode->nbEBands, int);
 
-   bits = nbCompressedBytes*8 - ec_enc_tell(enc, 0) - 1;
-   codedBands = compute_allocation(st->mode, st->start, st->end, offsets, alloc_trim, bits, pulses, fine_quant, fine_priority, C, LM);
+   /* bits =   packet size    -    where we are   - safety - skip signalling */
+   bits = nbCompressedBytes*8 - ec_enc_tell(enc, 0) - 1    - (1<<BITRES);
+   skip=-1;
+   codedBands = compute_allocation(st->mode, st->start, st->end, offsets,
+         alloc_trim, bits, pulses, fine_quant, fine_priority, C, LM, &skip, st->lastCodedBands);
+   st->lastCodedBands = codedBands;
+   for (i=0;i<skip;i++)
+      ec_enc_bit_prob(enc, 0, 32768);
+   ec_enc_bit_prob(enc, 1, 32768);
 
    quant_fine_energy(st->mode, st->start, st->end, bandE, oldBandE, error, fine_quant, enc, C);
 
@@ -1692,6 +1701,7 @@
    celt_word16 postfilter_gain;
    int intensity=0;
    int dual_stereo=0;
+   int skip;
    SAVE_STACK;
 
    if (pcm==NULL)
@@ -1825,8 +1835,19 @@
       intensity = ec_dec_uint(dec, 1+st->end-st->start);
    }
 
-   bits = len*8 - ec_dec_tell(dec, 0) - 1;
-   codedBands = compute_allocation(st->mode, st->start, st->end, offsets, alloc_trim, bits, pulses, fine_quant, fine_priority, C, LM);
+   bits = len*8 - ec_dec_tell(dec, 0) - 1 - (1<<BITRES);
+   skip=0;
+   while (ec_dec_bit_prob(dec, 32768)==0)
+   {
+      skip++;
+      if (skip>21)
+      {
+         dec->error = 1;
+         break;
+      }
+   }
+   codedBands = compute_allocation(st->mode, st->start, st->end, offsets,
+         alloc_trim, bits, pulses, fine_quant, fine_priority, C, LM, &skip, 0);
    
    unquant_fine_energy(st->mode, st->start, st->end, bandE, oldBandE, fine_quant, dec, C);
 
--- a/libcelt/rate.c
+++ b/libcelt/rate.c
@@ -142,7 +142,7 @@
 
 static inline int interp_bits2pulses(const CELTMode *m, int start, int end,
       int *bits1, int *bits2, const int *thresh, int total, int *bits,
-      int *ebits, int *fine_priority, int len, int _C, int LM)
+      int *ebits, int *fine_priority, int len, int _C, int LM, int *skip, int prev)
 {
    int psum;
    int lo, hi;
@@ -192,6 +192,25 @@
       psum += bits[j];
    }
    codedBands++;
+   if (*skip==-1)
+   {
+      *skip=0;
+      for (j=codedBands-1;j>=0;j--)
+      {
+         if ((bits[j] > (7*(m->eBands[j+1]-m->eBands[j])<<LM<<BITRES)>>4 && j<prev)
+               || (bits[j] > (9*(m->eBands[j+1]-m->eBands[j])<<LM<<BITRES)>>4))
+            break;
+         else
+            (*skip)++;
+      }
+      *skip = IMIN(*skip, codedBands-start-1);
+   }
+   for (i=0;i<*skip;i++)
+   {
+      psum = psum - bits[codedBands-1] + ((C+1)<<BITRES);
+      bits[codedBands-1] = C<<BITRES;
+      codedBands--;
+   }
    /* Allocate the remaining bits */
    if (codedBands) {
       int left, perband;
@@ -267,7 +286,7 @@
 }
 
 int compute_allocation(const CELTMode *m, int start, int end, int *offsets, int alloc_trim,
-      int total, int *pulses, int *ebits, int *fine_priority, int _C, int LM)
+      int total, int *pulses, int *ebits, int *fine_priority, int _C, int LM, int *skip, int prev)
 {
    int lo, hi, len, j;
    const int C = CHANNELS(_C);
@@ -286,7 +305,7 @@
 
    /* Below this threshold, we don't allocate any PVQ bits */
    for (j=start;j<end;j++)
-      thresh[j] = (4*(m->eBands[j+1]-m->eBands[j])<<LM<<BITRES)>>3;
+      thresh[j] = ((C+1)<<BITRES) + (3*(m->eBands[j+1]-m->eBands[j])<<LM<<BITRES)>>4;
    /* Tilt of the allocation curve */
    for (j=start;j<end;j++)
       trim_offset[j] = C*(m->eBands[j+1]-m->eBands[j])*(alloc_trim-5-LM)*(m->nbEBands-j-1)
@@ -334,7 +353,7 @@
       bits1[j] += offsets[j];
    }
    codedBands = interp_bits2pulses(m, start, end, bits1, bits2, thresh,
-         total, pulses, ebits, fine_priority, len, C, LM);
+         total, pulses, ebits, fine_priority, len, C, LM, skip, prev);
    RESTORE_STACK;
    return codedBands;
 }
--- a/libcelt/rate.h
+++ b/libcelt/rate.h
@@ -103,7 +103,7 @@
  @return Total number of bits allocated
 */
 int compute_allocation(const CELTMode *m, int start, int end, int *offsets, int alloc_trim,
-      int total, int *pulses, int *ebits, int *fine_priority, int _C, int LM);
+      int total, int *pulses, int *ebits, int *fine_priority, int _C, int LM, int *skip, int prev);
 
 
 #endif