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