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 =
73             (int16_t*)sbc_enc_as16Offset4[pstrCodecParams->s16SamplingFreq];
74       } else {
75         ps16GenTabPtr =
76             (int16_t*)sbc_enc_as16Offset8[pstrCodecParams->s16SamplingFreq];
77       }
78       for (s32Sb = 0; s32Sb < s32NumOfSubBands; s32Sb++) {
79         if (pstrCodecParams
80                 ->as16ScaleFactor[s32Ch * s32NumOfSubBands + s32Sb] == 0)
81           *(ps16GenBufPtr) = -5;
82         else {
83           s32Loudness = (int32_t)(
84               pstrCodecParams
85                   ->as16ScaleFactor[s32Ch * s32NumOfSubBands + s32Sb] -
86               *ps16GenTabPtr);
87           if (s32Loudness > 0)
88             *(ps16GenBufPtr) = (int16_t)(s32Loudness >> 1);
89           else
90             *(ps16GenBufPtr) = (int16_t)s32Loudness;
91         }
92         ps16GenBufPtr++;
93         ps16GenTabPtr++;
94       }
95     }
96 
97     /* max bitneed index is searched*/
98     s32MaxBitNeed = 0;
99     ps16GenBufPtr = ps16BitNeed + s32Ch * s32NumOfSubBands;
100     for (s32Sb = 0; s32Sb < s32NumOfSubBands; s32Sb++) {
101       if (*(ps16GenBufPtr) > s32MaxBitNeed) s32MaxBitNeed = *(ps16GenBufPtr);
102 
103       ps16GenBufPtr++;
104     }
105     ps16GenBufPtr = ps16BitNeed + s32Ch * s32NumOfSubBands;
106     /*iterative process to find hwo many bitslices fit into the bitpool*/
107     s32BitSlice = s32MaxBitNeed + 1;
108     s32BitCount = pstrCodecParams->s16BitPool;
109     s32SliceCount = 0;
110     do {
111       s32BitSlice--;
112       s32BitCount -= s32SliceCount;
113       s32SliceCount = 0;
114 
115       for (s32Sb = 0; s32Sb < s32NumOfSubBands; s32Sb++) {
116         if ((((*ps16GenBufPtr - s32BitSlice) < 16) &&
117              (*ps16GenBufPtr - s32BitSlice) >= 1)) {
118           if ((*ps16GenBufPtr - s32BitSlice) == 1)
119             s32SliceCount += 2;
120           else
121             s32SliceCount++;
122         }
123         ps16GenBufPtr++;
124 
125       } /*end of for*/
126       ps16GenBufPtr = ps16BitNeed + s32Ch * s32NumOfSubBands;
127     } while (s32BitCount - s32SliceCount > 0);
128 
129     if (s32BitCount == 0) {
130       s32BitCount -= s32SliceCount;
131       s32BitSlice--;
132     }
133 
134     /*Bits are distributed until the last bitslice is reached*/
135     ps16GenArrPtr = pstrCodecParams->as16Bits + s32Ch * s32NumOfSubBands;
136     ps16GenBufPtr = ps16BitNeed + s32Ch * s32NumOfSubBands;
137     for (s32Sb = 0; s32Sb < s32NumOfSubBands; s32Sb++) {
138       if (*(ps16GenBufPtr) < s32BitSlice + 2)
139         *(ps16GenArrPtr) = 0;
140       else
141         *(ps16GenArrPtr) = ((*(ps16GenBufPtr)-s32BitSlice) < 16)
142                                ? (int16_t)(*(ps16GenBufPtr)-s32BitSlice)
143                                : 16;
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