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