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  *  contains code for encoder flow and initialization of encoder
22  *
23  ******************************************************************************/
24 
25 #include "sbc_encoder.h"
26 
27 #include "sbc_enc_func_declare.h"
28 
29 #define abs32(x) (((x) >= 0) ? (x) : (-(x)))
30 
31 int16_t EncMaxShiftCounter;
32 
33 #if (SBC_JOINT_STE_INCLUDED == TRUE)
34 int32_t s32LRDiff[SBC_MAX_NUM_OF_BLOCKS] = {0};
35 int32_t s32LRSum[SBC_MAX_NUM_OF_BLOCKS] = {0};
36 #endif
37 
SBC_Encode(SBC_ENC_PARAMS * pstrEncParams,int16_t * input,uint8_t * output)38 uint32_t SBC_Encode(SBC_ENC_PARAMS* pstrEncParams, int16_t* input, uint8_t* output) {
39   int32_t s32Ch;                 /* counter for ch*/
40   int32_t s32Sb;                 /* counter for sub-band*/
41   uint32_t u32Count, maxBit = 0; /* loop count*/
42   int32_t s32MaxValue;           /* temp variable to store max value */
43 
44   int16_t* ps16ScfL;
45   int32_t* SbBuffer;
46   int32_t s32Blk; /* counter for block*/
47   int32_t s32NumOfBlocks = pstrEncParams->s16NumOfBlocks;
48 #if (SBC_JOINT_STE_INCLUDED == TRUE)
49   int32_t s32MaxValue2;
50   uint32_t u32CountSum, u32CountDiff;
51   int32_t *pSum, *pDiff;
52 #endif
53   register int32_t s32NumOfSubBands = pstrEncParams->s16NumOfSubBands;
54 
55   /* SBC ananlysis filter*/
56   if (s32NumOfSubBands == 4) {
57     SbcAnalysisFilter4(pstrEncParams, input);
58   } else {
59     SbcAnalysisFilter8(pstrEncParams, input);
60   }
61 
62   /* compute the scale factor, and save the max */
63   ps16ScfL = pstrEncParams->as16ScaleFactor;
64   s32Ch = pstrEncParams->s16NumOfChannels * s32NumOfSubBands;
65 
66   for (s32Sb = 0; s32Sb < s32Ch; s32Sb++) {
67     SbBuffer = pstrEncParams->s32SbBuffer + s32Sb;
68     s32MaxValue = 0;
69     for (s32Blk = s32NumOfBlocks; s32Blk > 0; s32Blk--) {
70       if (s32MaxValue < abs32(*SbBuffer)) {
71         s32MaxValue = abs32(*SbBuffer);
72       }
73       SbBuffer += s32Ch;
74     }
75 
76     u32Count = (s32MaxValue > 0x800000) ? 9 : 0;
77 
78     for (; u32Count < 15; u32Count++) {
79       if (s32MaxValue <= (int32_t)(0x8000 << u32Count)) {
80         break;
81       }
82     }
83     *ps16ScfL++ = (int16_t)u32Count;
84 
85     if (u32Count > maxBit) {
86       maxBit = u32Count;
87     }
88   }
89 /* In case of JS processing,check whether to use JS */
90 #if (SBC_JOINT_STE_INCLUDED == TRUE)
91   if (pstrEncParams->s16ChannelMode == SBC_JOINT_STEREO) {
92     /* Calculate sum and difference  scale factors for making JS decision   */
93     ps16ScfL = pstrEncParams->as16ScaleFactor;
94     /* calculate the scale factor of Joint stereo max sum and diff */
95     for (s32Sb = 0; s32Sb < s32NumOfSubBands - 1; s32Sb++) {
96       SbBuffer = pstrEncParams->s32SbBuffer + s32Sb;
97       s32MaxValue2 = 0;
98       s32MaxValue = 0;
99       pSum = s32LRSum;
100       pDiff = s32LRDiff;
101       for (s32Blk = 0; s32Blk < s32NumOfBlocks; s32Blk++) {
102         *pSum = (*SbBuffer + *(SbBuffer + s32NumOfSubBands)) >> 1;
103         if (abs32(*pSum) > s32MaxValue) {
104           s32MaxValue = abs32(*pSum);
105         }
106         pSum++;
107         *pDiff = (*SbBuffer - *(SbBuffer + s32NumOfSubBands)) >> 1;
108         if (abs32(*pDiff) > s32MaxValue2) {
109           s32MaxValue2 = abs32(*pDiff);
110         }
111         pDiff++;
112         SbBuffer += s32Ch;
113       }
114       u32Count = (s32MaxValue > 0x800000) ? 9 : 0;
115       for (; u32Count < 15; u32Count++) {
116         if (s32MaxValue <= (int32_t)(0x8000 << u32Count)) {
117           break;
118         }
119       }
120       u32CountSum = u32Count;
121       u32Count = (s32MaxValue2 > 0x800000) ? 9 : 0;
122       for (; u32Count < 15; u32Count++) {
123         if (s32MaxValue2 <= (int32_t)(0x8000 << u32Count)) {
124           break;
125         }
126       }
127       u32CountDiff = u32Count;
128       if ((*ps16ScfL + *(ps16ScfL + s32NumOfSubBands)) > (int16_t)(u32CountSum + u32CountDiff)) {
129         if (u32CountSum > maxBit) {
130           maxBit = u32CountSum;
131         }
132 
133         if (u32CountDiff > maxBit) {
134           maxBit = u32CountDiff;
135         }
136 
137         *ps16ScfL = (int16_t)u32CountSum;
138         *(ps16ScfL + s32NumOfSubBands) = (int16_t)u32CountDiff;
139 
140         SbBuffer = pstrEncParams->s32SbBuffer + s32Sb;
141         pSum = s32LRSum;
142         pDiff = s32LRDiff;
143 
144         for (s32Blk = 0; s32Blk < s32NumOfBlocks; s32Blk++) {
145           *SbBuffer = *pSum;
146           *(SbBuffer + s32NumOfSubBands) = *pDiff;
147 
148           SbBuffer += s32NumOfSubBands << 1;
149           pSum++;
150           pDiff++;
151         }
152 
153         pstrEncParams->as16Join[s32Sb] = 1;
154       } else {
155         pstrEncParams->as16Join[s32Sb] = 0;
156       }
157       ps16ScfL++;
158     }
159     pstrEncParams->as16Join[s32Sb] = 0;
160   }
161 #endif
162 
163   pstrEncParams->s16MaxBitNeed = (int16_t)maxBit;
164 
165   /* bit allocation */
166   if ((pstrEncParams->s16ChannelMode == SBC_STEREO) ||
167       (pstrEncParams->s16ChannelMode == SBC_JOINT_STEREO)) {
168     sbc_enc_bit_alloc_ste(pstrEncParams);
169   } else {
170     sbc_enc_bit_alloc_mono(pstrEncParams);
171   }
172 
173   /* Quantize the encoded audio */
174   return EncPacking(pstrEncParams, output);
175 }
176 
177 /****************************************************************************
178  * InitSbcAnalysisFilt - Initializes the input data to 0
179  *
180  * RETURNS : N/A
181  */
SBC_Encoder_Init(SBC_ENC_PARAMS * pstrEncParams)182 void SBC_Encoder_Init(SBC_ENC_PARAMS* pstrEncParams) {
183   uint16_t s16SamplingFreq; /*temp variable to store smpling freq*/
184   int16_t s16Bitpool;       /*to store bit pool value*/
185   int16_t s16BitRate;       /*to store bitrate*/
186   int16_t s16FrameLen;      /*to store frame length*/
187   uint16_t HeaderParams;
188 
189   /* Required number of channels */
190   if (pstrEncParams->s16ChannelMode == SBC_MONO) {
191     pstrEncParams->s16NumOfChannels = 1;
192   } else {
193     pstrEncParams->s16NumOfChannels = 2;
194   }
195 
196   /* Bit pool calculation */
197   if (pstrEncParams->s16SamplingFreq == SBC_sf16000) {
198     s16SamplingFreq = 16000;
199   } else if (pstrEncParams->s16SamplingFreq == SBC_sf32000) {
200     s16SamplingFreq = 32000;
201   } else if (pstrEncParams->s16SamplingFreq == SBC_sf44100) {
202     s16SamplingFreq = 44100;
203   } else {
204     s16SamplingFreq = 48000;
205   }
206 
207   if ((pstrEncParams->s16ChannelMode == SBC_JOINT_STEREO) ||
208       (pstrEncParams->s16ChannelMode == SBC_STEREO)) {
209     s16Bitpool =
210             (int16_t)((pstrEncParams->u16BitRate * pstrEncParams->s16NumOfSubBands * 1000 /
211                        s16SamplingFreq) -
212                       ((32 +
213                         (4 * pstrEncParams->s16NumOfSubBands * pstrEncParams->s16NumOfChannels) +
214                         ((pstrEncParams->s16ChannelMode - 2) * pstrEncParams->s16NumOfSubBands)) /
215                        pstrEncParams->s16NumOfBlocks));
216 
217     s16FrameLen = 4 + (4 * pstrEncParams->s16NumOfSubBands * pstrEncParams->s16NumOfChannels) / 8 +
218                   (((pstrEncParams->s16ChannelMode - 2) * pstrEncParams->s16NumOfSubBands) +
219                    (pstrEncParams->s16NumOfBlocks * s16Bitpool)) /
220                           8;
221 
222     s16BitRate = (8 * s16FrameLen * s16SamplingFreq) /
223                  (pstrEncParams->s16NumOfSubBands * pstrEncParams->s16NumOfBlocks * 1000);
224 
225     if (s16BitRate > pstrEncParams->u16BitRate) {
226       s16Bitpool--;
227     }
228 
229     if (pstrEncParams->s16NumOfSubBands == 8) {
230       pstrEncParams->s16BitPool = (s16Bitpool > 255) ? 255 : s16Bitpool;
231     } else {
232       pstrEncParams->s16BitPool = (s16Bitpool > 128) ? 128 : s16Bitpool;
233     }
234   } else {
235     s16Bitpool = (int16_t)(((pstrEncParams->s16NumOfSubBands * pstrEncParams->u16BitRate * 1000) /
236                             (s16SamplingFreq * pstrEncParams->s16NumOfChannels)) -
237                            (((32 / pstrEncParams->s16NumOfChannels) +
238                              (4 * pstrEncParams->s16NumOfSubBands)) /
239                             pstrEncParams->s16NumOfBlocks));
240 
241     pstrEncParams->s16BitPool = (s16Bitpool > (16 * pstrEncParams->s16NumOfSubBands))
242                                         ? (16 * pstrEncParams->s16NumOfSubBands)
243                                         : s16Bitpool;
244   }
245 
246   if (pstrEncParams->s16BitPool < 0) {
247     pstrEncParams->s16BitPool = 0;
248   }
249   /* sampling freq */
250   HeaderParams = ((pstrEncParams->s16SamplingFreq & 3) << 6);
251 
252   /* number of blocks*/
253   HeaderParams |= (((pstrEncParams->s16NumOfBlocks - 4) & 12) << 2);
254 
255   /* channel mode: mono, dual...*/
256   HeaderParams |= ((pstrEncParams->s16ChannelMode & 3) << 2);
257 
258   /* Loudness or SNR */
259   HeaderParams |= ((pstrEncParams->s16AllocationMethod & 1) << 1);
260   HeaderParams |= ((pstrEncParams->s16NumOfSubBands >> 3) & 1); /*4 or 8*/
261   pstrEncParams->FrameHeader = HeaderParams;
262 
263   if (pstrEncParams->s16NumOfSubBands == 4) {
264     if (pstrEncParams->s16NumOfChannels == 1) {
265       EncMaxShiftCounter = ((ENC_VX_BUFFER_SIZE - 4 * 10) >> 2) << 2;
266     } else {
267       EncMaxShiftCounter = ((ENC_VX_BUFFER_SIZE - 4 * 10 * 2) >> 3) << 2;
268     }
269   } else {
270     if (pstrEncParams->s16NumOfChannels == 1) {
271       EncMaxShiftCounter = ((ENC_VX_BUFFER_SIZE - 8 * 10) >> 3) << 3;
272     } else {
273       EncMaxShiftCounter = ((ENC_VX_BUFFER_SIZE - 8 * 10 * 2) >> 4) << 3;
274     }
275   }
276 
277   SbcAnalysisInit();
278 }
279