/* ** Copyright 2003-2010, VisualOn, Inc. ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. */ /******************************************************************************* File: bit_cnt.c Content: Huffman Bitcounter & coder functions *******************************************************************************/ #include "bit_cnt.h" #include "aac_rom.h" #define HI_LTAB(a) (a>>8) #define LO_LTAB(a) (a & 0xff) #define EXPAND(a) ((((Word32)(a&0xff00)) << 8)|(Word32)(a&0xff)) /***************************************************************************** * * function name: count1_2_3_4_5_6_7_8_9_10_11 * description: counts tables 1-11 * returns: * input: quantized spectrum * output: bitCount for tables 1-11 * *****************************************************************************/ static void count1_2_3_4_5_6_7_8_9_10_11(const Word16 *values, const Word16 width, Word16 *bitCount) { Word32 t0,t1,t2,t3,i; Word32 bc1_2,bc3_4,bc5_6,bc7_8,bc9_10; Word16 bc11,sc; bc1_2=0; bc3_4=0; bc5_6=0; bc7_8=0; bc9_10=0; bc11=0; sc=0; for(i=0;i0) + (t1>0) + (t2>0) + (t3>0); } bitCount[1]=extract_h(bc1_2); bitCount[2]=extract_l(bc1_2); bitCount[3]=extract_h(bc3_4) + sc; bitCount[4]=extract_l(bc3_4) + sc; bitCount[5]=extract_h(bc5_6); bitCount[6]=extract_l(bc5_6); bitCount[7]=extract_h(bc7_8) + sc; bitCount[8]=extract_l(bc7_8) + sc; bitCount[9]=extract_h(bc9_10) + sc; bitCount[10]=extract_l(bc9_10) + sc; bitCount[11]=bc11 + sc; } /***************************************************************************** * * function name: count3_4_5_6_7_8_9_10_11 * description: counts tables 3-11 * returns: * input: quantized spectrum * output: bitCount for tables 3-11 * *****************************************************************************/ static void count3_4_5_6_7_8_9_10_11(const Word16 *values, const Word16 width, Word16 *bitCount) { Word32 t0,t1,t2,t3, i; Word32 bc3_4,bc5_6,bc7_8,bc9_10; Word16 bc11,sc; bc3_4=0; bc5_6=0; bc7_8=0; bc9_10=0; bc11=0; sc=0; for(i=0;i0) + (t1>0) + (t2>0) + (t3>0); } bitCount[1]=INVALID_BITCOUNT; bitCount[2]=INVALID_BITCOUNT; bitCount[3]=extract_h(bc3_4) + sc; bitCount[4]=extract_l(bc3_4) + sc; bitCount[5]=extract_h(bc5_6); bitCount[6]=extract_l(bc5_6); bitCount[7]=extract_h(bc7_8) + sc; bitCount[8]=extract_l(bc7_8) + sc; bitCount[9]=extract_h(bc9_10) + sc; bitCount[10]=extract_l(bc9_10) + sc; bitCount[11]=bc11 + sc; } /***************************************************************************** * * function name: count5_6_7_8_9_10_11 * description: counts tables 5-11 * returns: * input: quantized spectrum * output: bitCount for tables 5-11 * *****************************************************************************/ static void count5_6_7_8_9_10_11(const Word16 *values, const Word16 width, Word16 *bitCount) { Word32 t0,t1,i; Word32 bc5_6,bc7_8,bc9_10; Word16 bc11,sc; bc5_6=0; bc7_8=0; bc9_10=0; bc11=0; sc=0; for(i=0;i0) + (t1>0); } bitCount[1]=INVALID_BITCOUNT; bitCount[2]=INVALID_BITCOUNT; bitCount[3]=INVALID_BITCOUNT; bitCount[4]=INVALID_BITCOUNT; bitCount[5]=extract_h(bc5_6); bitCount[6]=extract_l(bc5_6); bitCount[7]=extract_h(bc7_8) + sc; bitCount[8]=extract_l(bc7_8) + sc; bitCount[9]=extract_h(bc9_10) + sc; bitCount[10]=extract_l(bc9_10) + sc; bitCount[11]=bc11 + sc; } /***************************************************************************** * * function name: count7_8_9_10_11 * description: counts tables 7-11 * returns: * input: quantized spectrum * output: bitCount for tables 7-11 * *****************************************************************************/ static void count7_8_9_10_11(const Word16 *values, const Word16 width, Word16 *bitCount) { Word32 t0,t1, i; Word32 bc7_8,bc9_10; Word16 bc11,sc; bc7_8=0; bc9_10=0; bc11=0; sc=0; for(i=0;i0) + (t1>0); } bitCount[1]=INVALID_BITCOUNT; bitCount[2]=INVALID_BITCOUNT; bitCount[3]=INVALID_BITCOUNT; bitCount[4]=INVALID_BITCOUNT; bitCount[5]=INVALID_BITCOUNT; bitCount[6]=INVALID_BITCOUNT; bitCount[7]=extract_h(bc7_8) + sc; bitCount[8]=extract_l(bc7_8) + sc; bitCount[9]=extract_h(bc9_10) + sc; bitCount[10]=extract_l(bc9_10) + sc; bitCount[11]=bc11 + sc; } /***************************************************************************** * * function name: count9_10_11 * description: counts tables 9-11 * returns: * input: quantized spectrum * output: bitCount for tables 9-11 * *****************************************************************************/ static void count9_10_11(const Word16 *values, const Word16 width, Word16 *bitCount) { Word32 t0,t1,i; Word32 bc9_10; Word16 bc11,sc; bc9_10=0; bc11=0; sc=0; for(i=0;i0) + (t1>0); } bitCount[1]=INVALID_BITCOUNT; bitCount[2]=INVALID_BITCOUNT; bitCount[3]=INVALID_BITCOUNT; bitCount[4]=INVALID_BITCOUNT; bitCount[5]=INVALID_BITCOUNT; bitCount[6]=INVALID_BITCOUNT; bitCount[7]=INVALID_BITCOUNT; bitCount[8]=INVALID_BITCOUNT; bitCount[9]=extract_h(bc9_10) + sc; bitCount[10]=extract_l(bc9_10) + sc; bitCount[11]=bc11 + sc; } /***************************************************************************** * * function name: count11 * description: counts table 11 * returns: * input: quantized spectrum * output: bitCount for table 11 * *****************************************************************************/ static void count11(const Word16 *values, const Word16 width, Word16 *bitCount) { Word32 t0,t1,i; Word16 bc11,sc; bc11=0; sc=0; for(i=0;i0) + (t1>0); } bitCount[1]=INVALID_BITCOUNT; bitCount[2]=INVALID_BITCOUNT; bitCount[3]=INVALID_BITCOUNT; bitCount[4]=INVALID_BITCOUNT; bitCount[5]=INVALID_BITCOUNT; bitCount[6]=INVALID_BITCOUNT; bitCount[7]=INVALID_BITCOUNT; bitCount[8]=INVALID_BITCOUNT; bitCount[9]=INVALID_BITCOUNT; bitCount[10]=INVALID_BITCOUNT; bitCount[11]=bc11 + sc; } /***************************************************************************** * * function name: countEsc * description: counts table 11 (with Esc) * returns: * input: quantized spectrum * output: bitCount for tables 11 (with Esc) * *****************************************************************************/ static void countEsc(const Word16 *values, const Word16 width, Word16 *bitCount) { Word32 t0,t1,t00,t01,i; Word16 bc11,ec,sc; bc11=0; sc=0; ec=0; for(i=0;i0) + (t1>0); t00 = min(t0,16); t01 = min(t1,16); bc11 = bc11 + huff_ltab11[t00][t01]; if(t0 >= 16){ ec = ec + 5; while(sub(t0=(t0 >> 1), 16) >= 0) { ec = ec + 2; } } if(t1 >= 16){ ec = ec + 5; while(sub(t1=(t1 >> 1), 16) >= 0) { ec = ec + 2; } } } bitCount[1]=INVALID_BITCOUNT; bitCount[2]=INVALID_BITCOUNT; bitCount[3]=INVALID_BITCOUNT; bitCount[4]=INVALID_BITCOUNT; bitCount[5]=INVALID_BITCOUNT; bitCount[6]=INVALID_BITCOUNT; bitCount[7]=INVALID_BITCOUNT; bitCount[8]=INVALID_BITCOUNT; bitCount[9]=INVALID_BITCOUNT; bitCount[10]=INVALID_BITCOUNT; bitCount[11]=bc11 + sc + ec; } typedef void (*COUNT_FUNCTION)(const Word16 *values, const Word16 width, Word16 *bitCount); static COUNT_FUNCTION countFuncTable[CODE_BOOK_ESC_LAV+1] = { count1_2_3_4_5_6_7_8_9_10_11, /* 0 */ count1_2_3_4_5_6_7_8_9_10_11, /* 1 */ count3_4_5_6_7_8_9_10_11, /* 2 */ count5_6_7_8_9_10_11, /* 3 */ count5_6_7_8_9_10_11, /* 4 */ count7_8_9_10_11, /* 5 */ count7_8_9_10_11, /* 6 */ count7_8_9_10_11, /* 7 */ count9_10_11, /* 8 */ count9_10_11, /* 9 */ count9_10_11, /* 10 */ count9_10_11, /* 11 */ count9_10_11, /* 12 */ count11, /* 13 */ count11, /* 14 */ count11, /* 15 */ countEsc /* 16 */ }; /***************************************************************************** * * function name: bitCount * description: count bits * *****************************************************************************/ Word16 bitCount(const Word16 *values, const Word16 width, Word16 maxVal, Word16 *bitCount) { /* check if we can use codebook 0 */ if(maxVal == 0) bitCount[0] = 0; else bitCount[0] = INVALID_BITCOUNT; maxVal = min(maxVal, CODE_BOOK_ESC_LAV); countFuncTable[maxVal](values,width,bitCount); return(0); } /***************************************************************************** * * function name: codeValues * description: write huffum bits * *****************************************************************************/ Word16 codeValues(Word16 *values, Word16 width, Word16 codeBook, HANDLE_BIT_BUF hBitstream) { Word32 i, t0, t1, t2, t3, t00, t01; UWord16 codeWord, codeLength; Word16 sign, signLength; switch (codeBook) { case CODE_BOOK_ZERO_NO: break; case CODE_BOOK_1_NO: for(i=0; i= 16){ Word16 n, p; n=0; p=t0; while(sub(p=(p >> 1), 16) >= 0){ WriteBits(hBitstream,1,1); n = n + 1; } WriteBits(hBitstream,0,1); n = n + 4; WriteBits(hBitstream,(t0 - (1 << n)),n); } if(t1 >= 16){ Word16 n, p; n=0; p=t1; while(sub(p=(p >> 1), 16) >= 0){ WriteBits(hBitstream,1,1); n = n + 1; } WriteBits(hBitstream,0,1); n = n + 4; WriteBits(hBitstream,(t1 - (1 << n)),n); } } break; default: break; } return(0); } Word16 bitCountScalefactorDelta(Word16 delta) { return(huff_ltabscf[delta+CODE_BOOK_SCF_LAV]); } Word16 codeScalefactorDelta(Word16 delta, HANDLE_BIT_BUF hBitstream) { Word32 codeWord; Word16 codeLength; if(delta > CODE_BOOK_SCF_LAV || delta < -CODE_BOOK_SCF_LAV) return(1); codeWord = huff_ctabscf[delta + CODE_BOOK_SCF_LAV]; codeLength = huff_ltabscf[delta + CODE_BOOK_SCF_LAV]; WriteBits(hBitstream,codeWord,codeLength); return(0); }