From 9a6c4966309edea641602fea556e5ef654f95f08 Mon Sep 17 00:00:00 2001 From: Jean-Marc Valin Date: Mon, 9 Feb 2009 00:45:48 -0500 Subject: [PATCH] Changed the allocator to be smarter about the way it allocates fine energy bits. Also, doing better rounding of the bits. --- libcelt/cwrs.h | 2 ++ libcelt/modes.c | 29 +++++++--------------------- libcelt/rate.c | 51 +++++++++++++++++++++++++------------------------ 3 files changed, 35 insertions(+), 47 deletions(-) diff --git a/libcelt/cwrs.h b/libcelt/cwrs.h index 9f7418ee..eabdf75e 100644 --- a/libcelt/cwrs.h +++ b/libcelt/cwrs.h @@ -36,6 +36,8 @@ #include "entenc.h" #include "entdec.h" +int log2_frac(ec_uint32 val, int frac); + /* Returns log of an integer with fractional accuracy */ int log2_frac64(ec_uint64 val, int frac); /* Whether the CWRS codebook will fit into 32 bits */ diff --git a/libcelt/modes.c b/libcelt/modes.c index 567c6a2d..c5b8738e 100644 --- a/libcelt/modes.c +++ b/libcelt/modes.c @@ -228,7 +228,7 @@ static void compute_allocation_table(CELTMode *mode, int res) celt_int32_t alloc; edge = mode->eBands[eband+1]*res; alloc = band_allocation[i*BARK_BANDS+j]; - alloc = alloc*C*mode->mdctSize/256; + alloc = alloc*C*mode->mdctSize; if (edge < bark_freq[j+1]) { int num, den; @@ -243,30 +243,15 @@ static void compute_allocation_table(CELTMode *mode, int res) } } } - /* Compute fine energy resolution and update the pulse allocation table to subtract that */ for (i=0;inbAllocVectors;i++) { - int sum = 0; for (j=0;jnbEBands;j++) - { - int ebits; - int min_bits=0; - if (allocVectors[i*mode->nbEBands+j] > 0) - min_bits = 1; - ebits = IMAX(min_bits , allocVectors[i*mode->nbEBands+j] / (C*(mode->eBands[j+1]-mode->eBands[j]))); - if (ebits>7) - ebits=7; - /* The bits used for fine allocation can't be used for pulses */ - /* However, we give two "free" bits to all modes to compensate for the fact that some energy - resolution is needed regardless of the frame size. */ - if (ebits>1) - allocVectors[i*mode->nbEBands+j] -= C*(ebits-2); - if (allocVectors[i*mode->nbEBands+j] < 0) - allocVectors[i*mode->nbEBands+j] = 0; - sum += ebits; - allocEnergy[i*(mode->nbEBands+1)+j] = ebits; - } - allocEnergy[i*(mode->nbEBands+1)+mode->nbEBands] = sum; + allocVectors[i*mode->nbEBands+j] = (allocVectors[i*mode->nbEBands+j]+128)/256; + } + for (i=0;inbAllocVectors;i++) + { + for (j=0;jnbEBands;j++) + allocVectors[i*mode->nbEBands+j] += C; } mode->energy_alloc = allocEnergy; mode->allocVectors = allocVectors; diff --git a/libcelt/rate.c b/libcelt/rate.c index 7d3100c2..9c830493 100644 --- a/libcelt/rate.c +++ b/libcelt/rate.c @@ -73,9 +73,9 @@ celt_int16_t **compute_alloc_cache(CELTMode *m, int C) -static int interp_bits2pulses(const CELTMode *m, int *bits1, int *bits2, int *ebits1, int *ebits2, int total, int *bits, int *ebits, int len) +static void interp_bits2pulses(const CELTMode *m, int *bits1, int *bits2, int total, int *bits, int *ebits, int len) { - int esum, psum; + int psum; int lo, hi; int j; const int C = CHANNELS(m); @@ -86,26 +86,16 @@ static int interp_bits2pulses(const CELTMode *m, int *bits1, int *bits2, int *eb { int mid = (lo+hi)>>1; psum = 0; - esum = 0; for (j=0;j>BITRES; psum += ((1< (total-C*esum)< (total<>BITRES; - esum += ebits[j]; - } - for (j=0;j= C>>BITRES) + min_bits = 1; + /* Offset for the number of fine bits compared to their "fair share" of total/N */ + offset = 45 - log2_frac(m->eBands[j+1]-m->eBands[j], 4); + ebits[j] = IMAX(min_bits , ((bits[j]+C*(m->eBands[j+1]-m->eBands[j])/2) / (C*(m->eBands[j+1]-m->eBands[j])) - offset)>>BITRES ); + /* Make sure not to bust */ + if (C*ebits[j] > (bits[j]>>BITRES)) + ebits[j] = bits[j]/C >> BITRES; + + if (ebits[j]>7) + ebits[j]=7; + /* The bits used for fine allocation can't be used for pulses */ + bits[j] -= C*ebits[j]<nbEBands; ALLOC(bits1, len, int); ALLOC(bits2, len, int); - ALLOC(ebits1, len, int); - ALLOC(ebits2, len, int); lo = 0; hi = m->nbAllocVectors - 1; @@ -157,7 +160,7 @@ void compute_allocation(const CELTMode *m, int *offsets, const int *stereo_mode, /*printf ("%d ", bits[j]);*/ } /*printf ("\n");*/ - if (psum > (total-C*m->energy_alloc[mid*(len+1)+len])< (total<energy_alloc[lo*(len+1)+j]; - ebits2[j] = m->energy_alloc[hi*(len+1)+j]; bits1[j] = m->allocVectors[lo*len+j] + offsets[j]; bits2[j] = m->allocVectors[hi*len+j] + offsets[j]; if (bits1[j] < 0) @@ -175,7 +176,7 @@ void compute_allocation(const CELTMode *m, int *offsets, const int *stereo_mode, if (bits2[j] < 0) bits2[j] = 0; } - remaining_bits = interp_bits2pulses(m, bits1, bits2, ebits1, ebits2, total, pulses, ebits, len); + interp_bits2pulses(m, bits1, bits2, total, pulses, ebits, len); RESTORE_STACK; }