[Aften-devel] compute_mantissa optim. (and fix)
Status: Beta
Brought to you by:
jbr79
From: Prakash P. <pr...@pu...> - 2006-08-19 20:10:49
|
Hi, following optimizes compute_mantissa. I wanted to get rid of the gross case structure and replaced it by just two conditionals. I also spotted a bug in one of your simplifications: You changed if (s->mant2_cnt == 0) bits += 7; if (++s->mant2_cnt == 3) s->mant2_cnt = 0; to if(!(mant_cnt[1] & 2)) bits += 7; mant_cnt[1]++; Well, you are adding too much bits, thus wasting bits, as you count more often than intended. (Check mant_cnt[1] & 7: 000 001 010 011 100 101 110 111, you would count 4x, but we wanted 3x...) I guess you'll understand the way my patch works, if you look at it. ;-) It is probably that it won't apply cleanly or with offset, as I messed around in other parts of bitalloc , as well. (My profiling stats say, we spend 50% of time in bit_alloc!) Index: bitalloc.c =================================================================== --- bitalloc.c (Revision 58) +++ bitalloc.c (Arbeitskopie) @@ -394,36 +393,38 @@ /** return the size in bits taken by the mantissas */ static int -compute_mantissa_size(int mant_cnt[3], uint8_t *bap, int ncoefs) +compute_mantissa_size(int mant_cnt[5], uint8_t *bap, int ncoefs) { int bits, b, i; + + bits = 0; + for(i=0; i<ncoefs; i++) { + b = bap[i]; + if (b<=4) + ++mant_cnt[b]; + else if (b<=13) + bits += b-1; + else /* b=14 wants 14 and b=15 wants 16 bits */ + bits += 14 + ((b-14)<<1) ; + } - bits = 0; - for(i=0; i<ncoefs; i++) { - b = bap[i]; - switch(b) { - case 0: break; - case 1: if(!(mant_cnt[0] & 2)) bits += 5; - mant_cnt[0]++; - break; - case 2: if(!(mant_cnt[1] & 2)) bits += 7; - mant_cnt[1]++; - break; - case 3: bits += 3; - break; - case 4: if(!(mant_cnt[2] & 1)) bits += 7; - mant_cnt[2]++; - break; - case 14: bits += 14; - break; - case 15: bits += 16; - break; - default: bits += b - 1; - } - } + return bits; } +static int +compute_mantissa_size_finalize(int mant_cnt[5]) +{ + /* mant_cnt[1]: 3 mantissa in 5 bits */ + int bits = (mant_cnt[1] / 3) * 5; + /* mant_cnt[2]: 3 mantissa in 7 bits */ + /* mant_cnt[4]: 2 mantissa in 7 bits */ + bits += ((mant_cnt[2] / 3) + (mant_cnt[4] >> 1)) * 7; + /* mant_cnt[3]: b=3 wants 3 bits */ + bits += mant_cnt[3] * 3; + + return bits; +} /* call to prepare bit allocation */ static void bit_alloc_prepare(A52Context *ctx) @@ -452,7 +465,7 @@ { int blk, ch; int snroffset, bits; - int mant_cnt[3]; + int mant_cnt[5]; A52Frame *frame; A52Block *block; @@ -462,15 +475,19 @@ for(blk=0; blk<A52_NUM_BLOCKS; blk++) { block = &ctx->frame.blocks[blk]; - mant_cnt[0] = mant_cnt[1] = mant_cnt[2] = 0; + /* see compute_mantissa_size_finalize to understand initial vals */ + mant_cnt[0] = mant_cnt[3] = 0; + mant_cnt[1] = 1; + mant_cnt[2] = mant_cnt[4] = 3; for(ch=0; ch<ctx->n_all_channels; ch++) { memset(block->bap[ch], 0, 256); a52_bit_allocation(block->bap[ch], block->psd[ch], block->mask[ch], - blk, ch, frame->ncoefs[ch], snroffset, + blk, ch, frame->ncoefs[ch], snroffset, frame->bit_alloc.floor); bits += compute_mantissa_size(mant_cnt, block->bap[ch], frame->ncoefs[ch]); } + bits += compute_mantissa_size_finalize(mant_cnt); } return bits; -- (°= =°) //\ Prakash Punnoor /\\ V_/ \_V |