1 /****************************************************************************** 2 * 3 * Copyright 1999-2012 Broadcom Corporation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 /****************************************************************************** 20 * 21 * This file contains the code for bit allocation algorithm. It calculates 22 * the number of bits required for the encoded stream of data. 23 * 24 ******************************************************************************/ 25 26 /*Includes*/ 27 #include "sbc_enc_func_declare.h" 28 #include "sbc_encoder.h" 29 30 /*global arrays*/ 31 extern const int16_t sbc_enc_as16Offset4[4][4]; 32 extern const int16_t sbc_enc_as16Offset8[4][8]; 33 34 /**************************************************************************** 35 * BitAlloc - Calculates the required number of bits for the given scale factor 36 * and the number of subbands. 37 * 38 * RETURNS : N/A 39 */ 40 sbc_enc_bit_alloc_ste(SBC_ENC_PARAMS * pstrCodecParams)41void sbc_enc_bit_alloc_ste(SBC_ENC_PARAMS* pstrCodecParams) { 42 /* CAUTIOM -> mips optim for arm 32 require to use int32_t instead of int16_t 43 */ 44 /* Do not change variable type or name */ 45 int32_t s32MaxBitNeed; /*to store the max bits needed per sb*/ 46 int32_t s32BitCount; /*the used number of bits*/ 47 int32_t s32SliceCount; /*to store hwo many slices can be put in bitpool*/ 48 int32_t s32BitSlice; /*number of bitslices in bitpool*/ 49 int32_t s32Sb; /*counter for sub-band*/ 50 int32_t s32Ch; /*counter for channel*/ 51 int16_t* ps16BitNeed; /*temp memory to store required number of bits*/ 52 int32_t s32Loudness; /*used in Loudness calculation*/ 53 int16_t *ps16GenBufPtr, *pas16ScaleFactor; 54 int16_t* ps16GenArrPtr; 55 int16_t* ps16GenTabPtr; 56 int32_t s32NumOfSubBands = pstrCodecParams->s16NumOfSubBands; 57 int32_t s32BitPool = pstrCodecParams->s16BitPool; 58 59 /* bitneed values are derived from scale factor */ 60 if (pstrCodecParams->s16AllocationMethod == SBC_SNR) { 61 ps16BitNeed = pstrCodecParams->as16ScaleFactor; 62 s32MaxBitNeed = pstrCodecParams->s16MaxBitNeed; 63 } else { 64 ps16BitNeed = pstrCodecParams->s16ScartchMemForBitAlloc; 65 pas16ScaleFactor = pstrCodecParams->as16ScaleFactor; 66 s32MaxBitNeed = 0; 67 ps16GenBufPtr = ps16BitNeed; 68 for (s32Ch = 0; s32Ch < 2; s32Ch++) { 69 if (s32NumOfSubBands == 4) { 70 ps16GenTabPtr = 71 (int16_t*)sbc_enc_as16Offset4[pstrCodecParams->s16SamplingFreq]; 72 } else { 73 ps16GenTabPtr = 74 (int16_t*)sbc_enc_as16Offset8[pstrCodecParams->s16SamplingFreq]; 75 } 76 77 for (s32Sb = 0; s32Sb < s32NumOfSubBands; s32Sb++) { 78 if (*pas16ScaleFactor == 0) 79 *ps16GenBufPtr = -5; 80 else { 81 s32Loudness = (int32_t)(*pas16ScaleFactor - *ps16GenTabPtr); 82 83 if (s32Loudness > 0) 84 *ps16GenBufPtr = (int16_t)(s32Loudness >> 1); 85 else 86 *ps16GenBufPtr = (int16_t)s32Loudness; 87 } 88 89 if (*ps16GenBufPtr > s32MaxBitNeed) s32MaxBitNeed = *ps16GenBufPtr; 90 pas16ScaleFactor++; 91 ps16GenBufPtr++; 92 ps16GenTabPtr++; 93 } 94 } 95 } 96 97 /* iterative process to find out hwo many bitslices fit into the bitpool */ 98 s32BitSlice = s32MaxBitNeed + 1; 99 s32BitCount = s32BitPool; 100 s32SliceCount = 0; 101 do { 102 s32BitSlice--; 103 s32BitCount -= s32SliceCount; 104 s32SliceCount = 0; 105 ps16GenBufPtr = ps16BitNeed; 106 107 for (s32Sb = 0; s32Sb < 2 * s32NumOfSubBands; s32Sb++) { 108 if ((*ps16GenBufPtr >= s32BitSlice + 1) && 109 (*ps16GenBufPtr < s32BitSlice + 16)) { 110 if (*(ps16GenBufPtr) == s32BitSlice + 1) 111 s32SliceCount += 2; 112 else 113 s32SliceCount++; 114 } 115 ps16GenBufPtr++; 116 } 117 } while (s32BitCount - s32SliceCount > 0); 118 119 if (s32BitCount - s32SliceCount == 0) { 120 s32BitCount -= s32SliceCount; 121 s32BitSlice--; 122 } 123 124 /* Bits are distributed until the last bitslice is reached */ 125 ps16GenBufPtr = ps16BitNeed; 126 ps16GenArrPtr = pstrCodecParams->as16Bits; 127 for (s32Ch = 0; s32Ch < 2; s32Ch++) { 128 for (s32Sb = 0; s32Sb < s32NumOfSubBands; s32Sb++) { 129 if (*ps16GenBufPtr < s32BitSlice + 2) 130 *ps16GenArrPtr = 0; 131 else 132 *ps16GenArrPtr = ((*(ps16GenBufPtr)-s32BitSlice) < 16) 133 ? (int16_t)(*(ps16GenBufPtr)-s32BitSlice) 134 : 16; 135 ps16GenBufPtr++; 136 ps16GenArrPtr++; 137 } 138 } 139 140 /* the remaining bits are allocated starting at subband 0 */ 141 s32Ch = 0; 142 s32Sb = 0; 143 ps16GenBufPtr = ps16BitNeed; 144 ps16GenArrPtr -= 2 * s32NumOfSubBands; 145 146 while ((s32BitCount > 0) && (s32Sb < s32NumOfSubBands)) { 147 if ((*(ps16GenArrPtr) >= 2) && (*(ps16GenArrPtr) < 16)) { 148 (*(ps16GenArrPtr))++; 149 s32BitCount--; 150 } else if ((*ps16GenBufPtr == s32BitSlice + 1) && (s32BitCount > 1)) { 151 *(ps16GenArrPtr) = 2; 152 s32BitCount -= 2; 153 } 154 if (s32Ch == 1) { 155 s32Ch = 0; 156 s32Sb++; 157 ps16GenBufPtr = ps16BitNeed + s32Sb; 158 ps16GenArrPtr = pstrCodecParams->as16Bits + s32Sb; 159 160 } else { 161 s32Ch = 1; 162 ps16GenBufPtr = ps16BitNeed + s32NumOfSubBands + s32Sb; 163 ps16GenArrPtr = pstrCodecParams->as16Bits + s32NumOfSubBands + s32Sb; 164 } 165 } 166 167 s32Ch = 0; 168 s32Sb = 0; 169 ps16GenArrPtr = pstrCodecParams->as16Bits; 170 171 while ((s32BitCount > 0) && (s32Sb < s32NumOfSubBands)) { 172 if (*(ps16GenArrPtr) < 16) { 173 (*(ps16GenArrPtr))++; 174 s32BitCount--; 175 } 176 if (s32Ch == 1) { 177 s32Ch = 0; 178 s32Sb++; 179 ps16GenArrPtr = pstrCodecParams->as16Bits + s32Sb; 180 } else { 181 s32Ch = 1; 182 ps16GenArrPtr = pstrCodecParams->as16Bits + s32NumOfSubBands + s32Sb; 183 } 184 } 185 } 186 187 /*End of BitAlloc() function*/ 188