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 const int16_t sbc_enc_as16Offset4[4][4] = {
32 {-1, 0, 0, 0}, {-2, 0, 0, 1}, {-2, 0, 0, 1}, {-2, 0, 0, 1}};
33 const int16_t sbc_enc_as16Offset8[4][8] = {{-2, 0, 0, 0, 0, 0, 0, 1},
34 {-3, 0, 0, 0, 0, 0, 1, 2},
35 {-4, 0, 0, 0, 0, 0, 1, 2},
36 {-4, 0, 0, 0, 0, 0, 1, 2}};
37
38 /****************************************************************************
39 * BitAlloc - Calculates the required number of bits for the given scale factor
40 * and the number of subbands.
41 *
42 * RETURNS : N/A
43 */
44
sbc_enc_bit_alloc_mono(SBC_ENC_PARAMS * pstrCodecParams)45 void sbc_enc_bit_alloc_mono(SBC_ENC_PARAMS* pstrCodecParams) {
46 int32_t s32MaxBitNeed; /*to store the max bits needed per sb*/
47 int32_t s32BitCount; /*the used number of bits*/
48 int32_t s32SliceCount; /*to store hwo many slices can be put in bitpool*/
49 int32_t s32BitSlice; /*number of bitslices in bitpool*/
50 int32_t s32Sb; /*counter for sub-band*/
51 int32_t s32Ch; /*counter for channel*/
52 int16_t* ps16BitNeed; /*temp memory to store required number of bits*/
53 int32_t s32Loudness; /*used in Loudness calculation*/
54 int16_t* ps16GenBufPtr;
55 int16_t* ps16GenArrPtr;
56 int16_t* ps16GenTabPtr;
57 int32_t s32NumOfSubBands = pstrCodecParams->s16NumOfSubBands;
58
59 ps16BitNeed = pstrCodecParams->s16ScartchMemForBitAlloc;
60
61 for (s32Ch = 0; s32Ch < pstrCodecParams->s16NumOfChannels; s32Ch++) {
62 ps16GenBufPtr = ps16BitNeed + s32Ch * s32NumOfSubBands;
63 ps16GenArrPtr = pstrCodecParams->as16Bits + s32Ch * SBC_MAX_NUM_OF_SUBBANDS;
64
65 /* bitneed values are derived from scale factor */
66 if (pstrCodecParams->s16AllocationMethod == SBC_SNR) {
67 ps16BitNeed = pstrCodecParams->as16ScaleFactor;
68 ps16GenBufPtr = ps16BitNeed + s32Ch * s32NumOfSubBands;
69 } else {
70 ps16GenBufPtr = ps16BitNeed + s32Ch * s32NumOfSubBands;
71 if (s32NumOfSubBands == 4) {
72 ps16GenTabPtr = (int16_t*)sbc_enc_as16Offset4[pstrCodecParams->s16SamplingFreq];
73 } else {
74 ps16GenTabPtr = (int16_t*)sbc_enc_as16Offset8[pstrCodecParams->s16SamplingFreq];
75 }
76 for (s32Sb = 0; s32Sb < s32NumOfSubBands; s32Sb++) {
77 if (pstrCodecParams->as16ScaleFactor[s32Ch * s32NumOfSubBands + s32Sb] == 0) {
78 *(ps16GenBufPtr) = -5;
79 } else {
80 s32Loudness =
81 (int32_t)(pstrCodecParams->as16ScaleFactor[s32Ch * s32NumOfSubBands + s32Sb] -
82 *ps16GenTabPtr);
83 if (s32Loudness > 0) {
84 *(ps16GenBufPtr) = (int16_t)(s32Loudness >> 1);
85 } else {
86 *(ps16GenBufPtr) = (int16_t)s32Loudness;
87 }
88 }
89 ps16GenBufPtr++;
90 ps16GenTabPtr++;
91 }
92 }
93
94 /* max bitneed index is searched*/
95 s32MaxBitNeed = 0;
96 ps16GenBufPtr = ps16BitNeed + s32Ch * s32NumOfSubBands;
97 for (s32Sb = 0; s32Sb < s32NumOfSubBands; s32Sb++) {
98 if (*(ps16GenBufPtr) > s32MaxBitNeed) {
99 s32MaxBitNeed = *(ps16GenBufPtr);
100 }
101
102 ps16GenBufPtr++;
103 }
104 ps16GenBufPtr = ps16BitNeed + s32Ch * s32NumOfSubBands;
105 /*iterative process to find hwo many bitslices fit into the bitpool*/
106 s32BitSlice = s32MaxBitNeed + 1;
107 s32BitCount = pstrCodecParams->s16BitPool;
108 s32SliceCount = 0;
109 do {
110 s32BitSlice--;
111 s32BitCount -= s32SliceCount;
112 s32SliceCount = 0;
113
114 for (s32Sb = 0; s32Sb < s32NumOfSubBands; s32Sb++) {
115 if (((*ps16GenBufPtr - s32BitSlice) < 16) && (*ps16GenBufPtr - s32BitSlice) >= 1) {
116 if ((*ps16GenBufPtr - s32BitSlice) == 1) {
117 s32SliceCount += 2;
118 } else {
119 s32SliceCount++;
120 }
121 }
122 ps16GenBufPtr++;
123
124 } /*end of for*/
125 ps16GenBufPtr = ps16BitNeed + s32Ch * s32NumOfSubBands;
126 } while (s32BitCount - s32SliceCount > 0);
127
128 if (s32BitCount == 0) {
129 s32BitCount -= s32SliceCount;
130 s32BitSlice--;
131 }
132
133 /*Bits are distributed until the last bitslice is reached*/
134 ps16GenArrPtr = pstrCodecParams->as16Bits + s32Ch * s32NumOfSubBands;
135 ps16GenBufPtr = ps16BitNeed + s32Ch * s32NumOfSubBands;
136 for (s32Sb = 0; s32Sb < s32NumOfSubBands; s32Sb++) {
137 if (*(ps16GenBufPtr) < s32BitSlice + 2) {
138 *(ps16GenArrPtr) = 0;
139 } else {
140 *(ps16GenArrPtr) = ((*(ps16GenBufPtr)-s32BitSlice) < 16)
141 ? (int16_t)(*(ps16GenBufPtr)-s32BitSlice)
142 : 16;
143 }
144
145 ps16GenBufPtr++;
146 ps16GenArrPtr++;
147 }
148 ps16GenArrPtr = pstrCodecParams->as16Bits + s32Ch * s32NumOfSubBands;
149 ps16GenBufPtr = ps16BitNeed + s32Ch * s32NumOfSubBands;
150 /*the remaining bits are allocated starting at subband 0*/
151 s32Sb = 0;
152 while ((s32BitCount > 0) && (s32Sb < s32NumOfSubBands)) {
153 if ((*(ps16GenArrPtr) >= 2) && (*(ps16GenArrPtr) < 16)) {
154 (*(ps16GenArrPtr))++;
155 s32BitCount--;
156 } else if ((*(ps16GenBufPtr) == s32BitSlice + 1) && (s32BitCount > 1)) {
157 *(ps16GenArrPtr) = 2;
158 s32BitCount -= 2;
159 }
160 s32Sb++;
161 ps16GenArrPtr++;
162 ps16GenBufPtr++;
163 }
164 ps16GenArrPtr = pstrCodecParams->as16Bits + s32Ch * s32NumOfSubBands;
165
166 s32Sb = 0;
167 while ((s32BitCount > 0) && (s32Sb < s32NumOfSubBands)) {
168 if (*(ps16GenArrPtr) < 16) {
169 (*(ps16GenArrPtr))++;
170 s32BitCount--;
171 }
172 s32Sb++;
173 ps16GenArrPtr++;
174 }
175 }
176 }
177 /*End of BitAlloc() function*/
178