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