From 5c0c936d8f8ca65a44150f90ae0c583650ccb019 Mon Sep 17 00:00:00 2001 From: Jean-Marc Valin Date: Tue, 31 Aug 2010 10:11:11 -0400 Subject: [PATCH] Fine energy allocation cleanup --- libcelt/celt.c | 4 ++-- libcelt/rate.c | 35 ++++++++++++++++++++--------------- libcelt/rate.h | 2 +- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/libcelt/celt.c b/libcelt/celt.c index 4727adfd..404ad4ab 100644 --- a/libcelt/celt.c +++ b/libcelt/celt.c @@ -839,7 +839,7 @@ int celt_encode_with_ec_float(CELTEncoder * restrict st, const celt_sig * pcm, c for (i=0;imode->nbEBands;i++) offsets[i] = 0; bits = nbCompressedBytes*8 - ec_enc_tell(enc, 0) - 1; - compute_allocation(st->mode, st->start, st->end, offsets, bits, pulses, fine_quant, fine_priority, C, M); + compute_allocation(st->mode, st->start, st->end, offsets, bits, pulses, fine_quant, fine_priority, C, LM); quant_fine_energy(st->mode, st->start, st->end, bandE, oldBandE, error, fine_quant, enc, C); @@ -1509,7 +1509,7 @@ int celt_decode_with_ec_float(CELTDecoder * restrict st, const unsigned char *da bits = len*8 - ec_dec_tell(dec, 0) - 1; ALLOC(fine_quant, st->mode->nbEBands, int); - compute_allocation(st->mode, st->start, st->end, offsets, bits, pulses, fine_quant, fine_priority, C, M); + compute_allocation(st->mode, st->start, st->end, offsets, bits, pulses, fine_quant, fine_priority, C, LM); /*bits = ec_dec_tell(dec, 0); compute_fine_allocation(st->mode, fine_quant, (20*C+len*8/5-(ec_dec_tell(dec, 0)-bits))/C);*/ diff --git a/libcelt/rate.c b/libcelt/rate.c index d8448020..52414e25 100644 --- a/libcelt/rate.c +++ b/libcelt/rate.c @@ -136,7 +136,7 @@ void compute_pulse_cache(CELTMode *m, int LM) -static inline void interp_bits2pulses(const CELTMode *m, int start, int end, int *bits1, int *bits2, int total, int *bits, int *ebits, int *fine_priority, int len, int _C, int M) +static inline void interp_bits2pulses(const CELTMode *m, int start, int end, int *bits1, int *bits2, int total, int *bits, int *ebits, int *fine_priority, int len, int _C, int LM) { int psum; int lo, hi; @@ -145,7 +145,7 @@ static inline void interp_bits2pulses(const CELTMode *m, int start, int end, int const int C = CHANNELS(_C); SAVE_STACK; - logM = log2_frac(M, BITRES); + logM = LM<eBands[j+1]-m->eBands[j]; - N=M*N0; + N=N0<logN[j] + logM); + /* Compensate for the extra DoF in stereo */ den=(C*N+ ((C==2 && N>2) ? 1 : 0)); - /* Offset for the number of fine bits compared to their "fair share" of total/N */ - offset = N*C*(((m->logN[j] + logM)>>1)-FINE_OFFSET); + /* Offset for the number of fine bits by log2(N)/2 + FINE_OFFSET + compared to their "fair share" of total/N */ + offset = (NClogN>>1)-N*C*FINE_OFFSET; /* N=2 is the only point that doesn't match the curve */ if (N==2) @@ -195,10 +200,11 @@ static inline void interp_bits2pulses(const CELTMode *m, int start, int end, int /* Changing the offset for allocating the second and third fine energy bit */ if (bits[j] + offset < den*2<logN[j] + logM)*N*C>>2; + offset += NClogN>>2; else if (bits[j] + offset < den*3<logN[j] + logM)*N*C>>3; + offset += NClogN>>3; + /* Divide with rounding */ ebits[j] = (bits[j] + offset + (den<<(BITRES-1))) / (den< (bits[j]>>BITRES)) ebits[j] = bits[j]/C >> BITRES; + /* More than that is useless because that's about as far as PVQ can go */ if (ebits[j]>7) ebits[j]=7; - if (ebits[j]<0) - ebits[j]=0; - /* The bits used for fine allocation can't be used for pulses */ + /* The other bits are assigned to PVQ */ bits[j] -= C*ebits[j]<eBands[j+1]-m->eBands[j]; - bits1[j] = (C*M*N*m->allocVectors[mid*len+j] + offsets[j]); + bits1[j] = ((C*N*m->allocVectors[mid*len+j]<eBands[j+1]-m->eBands[j]; - bits1[j] = C*M*N*m->allocVectors[lo*len+j] + offsets[j]; - bits2[j] = C*M*N*m->allocVectors[hi*len+j] + offsets[j]; + bits1[j] = (C*N*m->allocVectors[lo*len+j]<allocVectors[hi*len+j]<