From 9bf37cc9712506b2483650c82d3c41152337ef7e Mon Sep 17 00:00:00 2001 From: Dave Burke Date: Tue, 17 Apr 2012 09:51:45 -0700 Subject: Fraunhofer AAC codec. License boilerplate update to follow. Change-Id: I2810460c11a58b6d148d84673cc031f3685e79b5 --- libAACenc/src/bit_cnt.cpp | 1060 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1060 insertions(+) create mode 100644 libAACenc/src/bit_cnt.cpp (limited to 'libAACenc/src/bit_cnt.cpp') diff --git a/libAACenc/src/bit_cnt.cpp b/libAACenc/src/bit_cnt.cpp new file mode 100644 index 0000000..f1a9b73 --- /dev/null +++ b/libAACenc/src/bit_cnt.cpp @@ -0,0 +1,1060 @@ +/******************************** MPEG Audio Encoder ************************** + + (C) Copyright Fraunhofer IIS (1999) + All Rights Reserved + + Please be advised that this software and/or program delivery is + Confidential Information of Fraunhofer and subject to and covered by the + + Fraunhofer IIS Software Evaluation Agreement + between Google Inc. and Fraunhofer + effective and in full force since March 1, 2012. + + You may use this software and/or program only under the terms and + conditions described in the above mentioned Fraunhofer IIS Software + Evaluation Agreement. Any other and/or further use requires a separate agreement. + + + This software and/or program is protected by copyright law and international + treaties. Any reproduction or distribution of this software and/or program, + or any portion of it, may result in severe civil and criminal penalties, and + will be prosecuted to the maximum extent possible under law. + + $Id$ + Initial author: M.Werner + contents/description: Huffman Bitcounter & coder + +******************************************************************************/ + +#include "bit_cnt.h" + +#include "aacEnc_ram.h" + +#define HI_LTAB(a) (a>>16) +#define LO_LTAB(a) (a & 0xffff) + +/***************************************************************************** + + + functionname: FDKaacEnc_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 FDKaacEnc_count1_2_3_4_5_6_7_8_9_10_11(const SHORT *RESTRICT values, + const INT width, + INT *bitCount) +{ + + INT i; + INT bc1_2,bc3_4,bc5_6,bc7_8,bc9_10,bc11,sc; + INT t0,t1,t2,t3; + 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]=HI_LTAB(bc1_2); + bitCount[2]=LO_LTAB(bc1_2); + bitCount[3]=HI_LTAB(bc3_4)+sc; + bitCount[4]=LO_LTAB(bc3_4)+sc; + bitCount[5]=HI_LTAB(bc5_6); + bitCount[6]=LO_LTAB(bc5_6); + bitCount[7]=HI_LTAB(bc7_8)+sc; + bitCount[8]=LO_LTAB(bc7_8)+sc; + bitCount[9]=HI_LTAB(bc9_10)+sc; + bitCount[10]=LO_LTAB(bc9_10)+sc; + bitCount[11]=bc11+sc; + +} + + +/***************************************************************************** + + functionname: FDKaacEnc_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 FDKaacEnc_count3_4_5_6_7_8_9_10_11(const SHORT *RESTRICT values, + const INT width, + INT *bitCount) +{ + + INT i; + INT bc3_4,bc5_6,bc7_8,bc9_10,bc11,sc; + INT t0,t1,t2,t3; + + 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]=HI_LTAB(bc3_4)+sc; + bitCount[4]=LO_LTAB(bc3_4)+sc; + bitCount[5]=HI_LTAB(bc5_6); + bitCount[6]=LO_LTAB(bc5_6); + bitCount[7]=HI_LTAB(bc7_8)+sc; + bitCount[8]=LO_LTAB(bc7_8)+sc; + bitCount[9]=HI_LTAB(bc9_10)+sc; + bitCount[10]=LO_LTAB(bc9_10)+sc; + bitCount[11]=bc11+sc; +} + + + +/***************************************************************************** + + functionname: FDKaacEnc_count5_6_7_8_9_10_11 + description: counts tables 5-11 + returns: + input: quantized spectrum + output: bitCount for tables 5-11 + +*****************************************************************************/ + + +static void FDKaacEnc_count5_6_7_8_9_10_11(const SHORT *RESTRICT values, + const INT width, + INT *bitCount) +{ + + INT i; + INT bc5_6,bc7_8,bc9_10,bc11,sc; + INT t0,t1; + 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]=HI_LTAB(bc5_6); + bitCount[6]=LO_LTAB(bc5_6); + bitCount[7]=HI_LTAB(bc7_8)+sc; + bitCount[8]=LO_LTAB(bc7_8)+sc; + bitCount[9]=HI_LTAB(bc9_10)+sc; + bitCount[10]=LO_LTAB(bc9_10)+sc; + bitCount[11]=bc11+sc; + +} + + +/***************************************************************************** + + functionname: FDKaacEnc_count7_8_9_10_11 + description: counts tables 7-11 + returns: + input: quantized spectrum + output: bitCount for tables 7-11 + +*****************************************************************************/ + +static void FDKaacEnc_count7_8_9_10_11(const SHORT *RESTRICT values, + const INT width, + INT *bitCount) +{ + + INT i; + INT bc7_8,bc9_10,bc11,sc; + INT t0,t1; + + 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]=HI_LTAB(bc7_8)+sc; + bitCount[8]=LO_LTAB(bc7_8)+sc; + bitCount[9]=HI_LTAB(bc9_10)+sc; + bitCount[10]=LO_LTAB(bc9_10)+sc; + bitCount[11]=bc11+sc; + +} + +/***************************************************************************** + + functionname: FDKaacEnc_count9_10_11 + description: counts tables 9-11 + returns: + input: quantized spectrum + output: bitCount for tables 9-11 + +*****************************************************************************/ + + + +static void FDKaacEnc_count9_10_11(const SHORT *RESTRICT values, + const INT width, + INT *bitCount) +{ + + INT i; + INT bc9_10,bc11,sc; + INT t0,t1; + + 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]=HI_LTAB(bc9_10)+sc; + bitCount[10]=LO_LTAB(bc9_10)+sc; + bitCount[11]=bc11+sc; + +} + +/***************************************************************************** + + functionname: FDKaacEnc_count11 + description: counts table 11 + returns: + input: quantized spectrum + output: bitCount for table 11 + +*****************************************************************************/ + +static void FDKaacEnc_count11(const SHORT *RESTRICT values, + const INT width, + INT *bitCount) +{ + + INT i; + INT bc11,sc; + INT t0,t1; + + 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; +} + +/***************************************************************************** + + functionname: FDKaacEnc_countEsc + description: counts table 11 (with Esc) + returns: + input: quantized spectrum + output: bitCount for tables 11 (with Esc) + +*****************************************************************************/ + +static void FDKaacEnc_countEsc(const SHORT *RESTRICT values, + const INT width, + INT *RESTRICT bitCount) +{ + + INT i; + INT bc11,ec,sc; + INT t0,t1,t00,t01; + + bc11=0; + sc=0; + ec=0; + for(i=0;i0)+(t1>0); + + t00 = fixMin(t0,16); + t01 = fixMin(t1,16); + bc11+= (INT) FDKaacEnc_huff_ltab11[t00][t01]; + + if(t0>=16){ + ec+=5; + while((t0>>=1) >= 16) + ec+=2; + } + + if(t1>=16){ + ec+=5; + while((t1>>=1) >= 16) + ec+=2; + } + } + + for (i=0; i<11; i++) + bitCount[i]=INVALID_BITCOUNT; + + bitCount[11]=bc11+sc+ec; +} + + +typedef void (*COUNT_FUNCTION)(const SHORT *RESTRICT values, + const INT width, + INT *RESTRICT bitCount); + +static const COUNT_FUNCTION countFuncTable[CODE_BOOK_ESC_LAV+1] = +{ + + FDKaacEnc_count1_2_3_4_5_6_7_8_9_10_11, /* 0 */ + FDKaacEnc_count1_2_3_4_5_6_7_8_9_10_11, /* 1 */ + FDKaacEnc_count3_4_5_6_7_8_9_10_11, /* 2 */ + FDKaacEnc_count5_6_7_8_9_10_11, /* 3 */ + FDKaacEnc_count5_6_7_8_9_10_11, /* 4 */ + FDKaacEnc_count7_8_9_10_11, /* 5 */ + FDKaacEnc_count7_8_9_10_11, /* 6 */ + FDKaacEnc_count7_8_9_10_11, /* 7 */ + FDKaacEnc_count9_10_11, /* 8 */ + FDKaacEnc_count9_10_11, /* 9 */ + FDKaacEnc_count9_10_11, /* 10 */ + FDKaacEnc_count9_10_11, /* 11 */ + FDKaacEnc_count9_10_11, /* 12 */ + FDKaacEnc_count11, /* 13 */ + FDKaacEnc_count11, /* 14 */ + FDKaacEnc_count11, /* 15 */ + FDKaacEnc_countEsc /* 16 */ +}; + + + +INT FDKaacEnc_bitCount(const SHORT *values, + const INT width, + INT maxVal, + INT *bitCount) +{ + + /* + check if we can use codebook 0 + */ + + if(maxVal == 0) + bitCount[0] = 0; + else + bitCount[0] = INVALID_BITCOUNT; + + maxVal = fixMin(maxVal,(INT)CODE_BOOK_ESC_LAV); + countFuncTable[maxVal](values,width,bitCount); + return(0); +} + + + + +/* + count difference between actual and zeroed lines +*/ +INT FDKaacEnc_countValues(SHORT *RESTRICT values, INT width, INT codeBook) +{ + + INT i,t0,t1,t2,t3,t00,t01; + INT codeLength; + INT signLength; + INT bitCnt=0; + + switch(codeBook){ + case CODE_BOOK_ZERO_NO: + break; + + case CODE_BOOK_1_NO: + for(i=0; i=16){ + INT n,p; + n=0; + p=t0; + while((p>>=1) >=16){ + bitCnt++; + n++; + } + bitCnt+=(n+5); + } + if(t1 >=16){ + INT n,p; + n=0; + p=t1; + while((p>>=1) >=16){ + bitCnt++; + n++; + } + bitCnt+=(n+5); + } + } + break; + + default: + break; + } + + return(bitCnt); +} + + + +INT FDKaacEnc_codeValues(SHORT *RESTRICT values, INT width, INT codeBook, HANDLE_FDK_BITSTREAM hBitstream) +{ + + INT i,t0,t1,t2,t3,t00,t01; + INT codeWord,codeLength; + INT sign,signLength; + + switch(codeBook){ + case CODE_BOOK_ZERO_NO: + break; + + case CODE_BOOK_1_NO: + for(i=0; i=16){ + INT n,p; + n=0; + p=t0; + while((p>>=1) >=16){ + FDKwriteBits(hBitstream,1,1); + n++; + } + FDKwriteBits(hBitstream,0,1); + FDKwriteBits(hBitstream,t0-(1<<(n+4)),n+4); + } + if(t1 >=16){ + INT n,p; + n=0; + p=t1; + while((p>>=1) >=16){ + FDKwriteBits(hBitstream,1,1); + n++; + } + FDKwriteBits(hBitstream,0,1); + FDKwriteBits(hBitstream,t1-(1<<(n+4)),n+4); + } + } + break; + + default: + break; + } + return(0); +} + +INT FDKaacEnc_codeScalefactorDelta(INT delta, HANDLE_FDK_BITSTREAM hBitstream) +{ + INT codeWord,codeLength; + + if(fixp_abs(delta) >CODE_BOOK_SCF_LAV) + return(1); + + codeWord = FDKaacEnc_huff_ctabscf[delta+CODE_BOOK_SCF_LAV]; + codeLength = (INT)FDKaacEnc_huff_ltabscf[delta+CODE_BOOK_SCF_LAV]; + FDKwriteBits(hBitstream,codeWord,codeLength); + return(0); +} + + + -- cgit v1.2.3