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 = (int16_t*)sbc_enc_as16Offset4[pstrCodecParams->s16SamplingFreq]; 71 } else { 72 ps16GenTabPtr = (int16_t*)sbc_enc_as16Offset8[pstrCodecParams->s16SamplingFreq]; 73 } 74 75 for (s32Sb = 0; s32Sb < s32NumOfSubBands; s32Sb++) { 76 if (*pas16ScaleFactor == 0) { 77 *ps16GenBufPtr = -5; 78 } else { 79 s32Loudness = (int32_t)(*pas16ScaleFactor - *ps16GenTabPtr); 80 81 if (s32Loudness > 0) { 82 *ps16GenBufPtr = (int16_t)(s32Loudness >> 1); 83 } else { 84 *ps16GenBufPtr = (int16_t)s32Loudness; 85 } 86 } 87 88 if (*ps16GenBufPtr > s32MaxBitNeed) { 89 s32MaxBitNeed = *ps16GenBufPtr; 90 } 91 pas16ScaleFactor++; 92 ps16GenBufPtr++; 93 ps16GenTabPtr++; 94 } 95 } 96 } 97 98 /* iterative process to find out hwo many bitslices fit into the bitpool */ 99 s32BitSlice = s32MaxBitNeed + 1; 100 s32BitCount = s32BitPool; 101 s32SliceCount = 0; 102 do { 103 s32BitSlice--; 104 s32BitCount -= s32SliceCount; 105 s32SliceCount = 0; 106 ps16GenBufPtr = ps16BitNeed; 107 108 for (s32Sb = 0; s32Sb < 2 * s32NumOfSubBands; s32Sb++) { 109 if ((*ps16GenBufPtr >= s32BitSlice + 1) && (*ps16GenBufPtr < s32BitSlice + 16)) { 110 if (*(ps16GenBufPtr) == s32BitSlice + 1) { 111 s32SliceCount += 2; 112 } else { 113 s32SliceCount++; 114 } 115 } 116 ps16GenBufPtr++; 117 } 118 } while (s32BitCount - s32SliceCount > 0); 119 120 if (s32BitCount - s32SliceCount == 0) { 121 s32BitCount -= s32SliceCount; 122 s32BitSlice--; 123 } 124 125 /* Bits are distributed until the last bitslice is reached */ 126 ps16GenBufPtr = ps16BitNeed; 127 ps16GenArrPtr = pstrCodecParams->as16Bits; 128 for (s32Ch = 0; s32Ch < 2; s32Ch++) { 129 for (s32Sb = 0; s32Sb < s32NumOfSubBands; s32Sb++) { 130 if (*ps16GenBufPtr < s32BitSlice + 2) { 131 *ps16GenArrPtr = 0; 132 } else { 133 *ps16GenArrPtr = ((*(ps16GenBufPtr)-s32BitSlice) < 16) 134 ? (int16_t)(*(ps16GenBufPtr)-s32BitSlice) 135 : 16; 136 } 137 ps16GenBufPtr++; 138 ps16GenArrPtr++; 139 } 140 } 141 142 /* the remaining bits are allocated starting at subband 0 */ 143 s32Ch = 0; 144 s32Sb = 0; 145 ps16GenBufPtr = ps16BitNeed; 146 ps16GenArrPtr -= 2 * s32NumOfSubBands; 147 148 while ((s32BitCount > 0) && (s32Sb < s32NumOfSubBands)) { 149 if ((*(ps16GenArrPtr) >= 2) && (*(ps16GenArrPtr) < 16)) { 150 (*(ps16GenArrPtr))++; 151 s32BitCount--; 152 } else if ((*ps16GenBufPtr == s32BitSlice + 1) && (s32BitCount > 1)) { 153 *(ps16GenArrPtr) = 2; 154 s32BitCount -= 2; 155 } 156 if (s32Ch == 1) { 157 s32Ch = 0; 158 s32Sb++; 159 ps16GenBufPtr = ps16BitNeed + s32Sb; 160 ps16GenArrPtr = pstrCodecParams->as16Bits + s32Sb; 161 162 } else { 163 s32Ch = 1; 164 ps16GenBufPtr = ps16BitNeed + s32NumOfSubBands + s32Sb; 165 ps16GenArrPtr = pstrCodecParams->as16Bits + s32NumOfSubBands + s32Sb; 166 } 167 } 168 169 s32Ch = 0; 170 s32Sb = 0; 171 ps16GenArrPtr = pstrCodecParams->as16Bits; 172 173 while ((s32BitCount > 0) && (s32Sb < s32NumOfSubBands)) { 174 if (*(ps16GenArrPtr) < 16) { 175 (*(ps16GenArrPtr))++; 176 s32BitCount--; 177 } 178 if (s32Ch == 1) { 179 s32Ch = 0; 180 s32Sb++; 181 ps16GenArrPtr = pstrCodecParams->as16Bits + s32Sb; 182 } else { 183 s32Ch = 1; 184 ps16GenArrPtr = pstrCodecParams->as16Bits + s32NumOfSubBands + s32Sb; 185 } 186 } 187 } 188 189 /*End of BitAlloc() function*/ 190