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 * contains code for encoder flow and initalization of encoder
22 *
23 ******************************************************************************/
24
25 #include <string.h>
26 #include "common/bt_target.h"
27 #include "sbc_encoder.h"
28 #include "sbc_enc_func_declare.h"
29
30 #if (defined(SBC_ENC_INCLUDED) && SBC_ENC_INCLUDED == TRUE)
31
32 SINT16 EncMaxShiftCounter;
33
34 #if (SBC_JOINT_STE_INCLUDED == TRUE)
35 SINT32 s32LRDiff[SBC_MAX_NUM_OF_BLOCKS] = {0};
36 SINT32 s32LRSum[SBC_MAX_NUM_OF_BLOCKS] = {0};
37 #endif
38
SBC_Encoder(SBC_ENC_PARAMS * pstrEncParams)39 void SBC_Encoder(SBC_ENC_PARAMS *pstrEncParams)
40 {
41 SINT32 s32Ch; /* counter for ch*/
42 SINT32 s32Sb; /* counter for sub-band*/
43 UINT32 u32Count, maxBit = 0; /* loop count*/
44 SINT32 s32MaxValue; /* temp variable to store max value */
45
46 SINT16 *ps16ScfL;
47 SINT32 *SbBuffer;
48 SINT32 s32Blk; /* counter for block*/
49 SINT32 s32NumOfBlocks = pstrEncParams->s16NumOfBlocks;
50 #if (SBC_JOINT_STE_INCLUDED == TRUE)
51 SINT32 s32MaxValue2;
52 UINT32 u32CountSum, u32CountDiff;
53 SINT32 *pSum, *pDiff;
54 #endif
55 register SINT32 s32NumOfSubBands = pstrEncParams->s16NumOfSubBands;
56
57 pstrEncParams->pu8NextPacket = pstrEncParams->pu8Packet;
58
59 #if (SBC_NO_PCM_CPY_OPTION == TRUE)
60 pstrEncParams->ps16NextPcmBuffer = pstrEncParams->ps16PcmBuffer;
61 #else
62 pstrEncParams->ps16NextPcmBuffer = pstrEncParams->as16PcmBuffer;
63 #endif
64 do {
65 /* SBC ananlysis filter*/
66 if (s32NumOfSubBands == 4) {
67 SbcAnalysisFilter4(pstrEncParams);
68 } else {
69 SbcAnalysisFilter8(pstrEncParams);
70 }
71
72 /* compute the scale factor, and save the max */
73 ps16ScfL = pstrEncParams->as16ScaleFactor;
74 s32Ch = pstrEncParams->s16NumOfChannels * s32NumOfSubBands;
75
76 pstrEncParams->ps16NextPcmBuffer += s32Ch * s32NumOfBlocks; /* in case of multible sbc frame to encode update the pcm pointer */
77
78 for (s32Sb = 0; s32Sb < s32Ch; s32Sb++) {
79 SbBuffer = pstrEncParams->s32SbBuffer + s32Sb;
80 s32MaxValue = 0;
81 for (s32Blk = s32NumOfBlocks; s32Blk > 0; s32Blk--) {
82 if (s32MaxValue < abs32(*SbBuffer)) {
83 s32MaxValue = abs32(*SbBuffer);
84 }
85 SbBuffer += s32Ch;
86 }
87
88 u32Count = (s32MaxValue > 0x800000) ? 9 : 0;
89
90 for ( ; u32Count < 15; u32Count++) {
91 if (s32MaxValue <= (SINT32)(0x8000 << u32Count)) {
92 break;
93 }
94 }
95 *ps16ScfL++ = (SINT16)u32Count;
96
97 if (u32Count > maxBit) {
98 maxBit = u32Count;
99 }
100 }
101 /* In case of JS processing,check whether to use JS */
102 #if (SBC_JOINT_STE_INCLUDED == TRUE)
103 if (pstrEncParams->s16ChannelMode == SBC_JOINT_STEREO) {
104 /* Calculate sum and differance scale factors for making JS decision */
105 ps16ScfL = pstrEncParams->as16ScaleFactor ;
106 /* calculate the scale factor of Joint stereo max sum and diff */
107 for (s32Sb = 0; s32Sb < s32NumOfSubBands - 1; s32Sb++) {
108 SbBuffer = pstrEncParams->s32SbBuffer + s32Sb;
109 s32MaxValue2 = 0;
110 s32MaxValue = 0;
111 pSum = s32LRSum;
112 pDiff = s32LRDiff;
113 for (s32Blk = 0; s32Blk < s32NumOfBlocks; s32Blk++) {
114 *pSum = (*SbBuffer + * (SbBuffer + s32NumOfSubBands)) >> 1;
115 if (abs32(*pSum) > s32MaxValue) {
116 s32MaxValue = abs32(*pSum);
117 }
118 pSum++;
119 *pDiff = (*SbBuffer - * (SbBuffer + s32NumOfSubBands)) >> 1;
120 if (abs32(*pDiff) > s32MaxValue2) {
121 s32MaxValue2 = abs32(*pDiff);
122 }
123 pDiff++;
124 SbBuffer += s32Ch;
125 }
126 u32Count = (s32MaxValue > 0x800000) ? 9 : 0;
127 for ( ; u32Count < 15; u32Count++) {
128 if (s32MaxValue <= (SINT32)(0x8000 << u32Count)) {
129 break;
130 }
131 }
132 u32CountSum = u32Count;
133 u32Count = (s32MaxValue2 > 0x800000) ? 9 : 0;
134 for ( ; u32Count < 15; u32Count++) {
135 if (s32MaxValue2 <= (SINT32)(0x8000 << u32Count)) {
136 break;
137 }
138 }
139 u32CountDiff = u32Count;
140 if ( (*ps16ScfL + * (ps16ScfL + s32NumOfSubBands)) > (SINT16)(u32CountSum + u32CountDiff) ) {
141
142 if (u32CountSum > maxBit) {
143 maxBit = u32CountSum;
144 }
145
146 if (u32CountDiff > maxBit) {
147 maxBit = u32CountDiff;
148 }
149
150 *ps16ScfL = (SINT16)u32CountSum;
151 *(ps16ScfL + s32NumOfSubBands) = (SINT16)u32CountDiff;
152
153 SbBuffer = pstrEncParams->s32SbBuffer + s32Sb;
154 pSum = s32LRSum;
155 pDiff = s32LRDiff;
156
157 for (s32Blk = 0; s32Blk < s32NumOfBlocks; s32Blk++) {
158 *SbBuffer = *pSum;
159 *(SbBuffer + s32NumOfSubBands) = *pDiff;
160
161 SbBuffer += s32NumOfSubBands << 1;
162 pSum++;
163 pDiff++;
164 }
165
166 pstrEncParams->as16Join[s32Sb] = 1;
167 } else {
168 pstrEncParams->as16Join[s32Sb] = 0;
169 }
170 ps16ScfL++;
171 }
172 pstrEncParams->as16Join[s32Sb] = 0;
173 }
174 #endif
175
176 pstrEncParams->s16MaxBitNeed = (SINT16)maxBit;
177
178 /* bit allocation */
179 if ((pstrEncParams->s16ChannelMode == SBC_STEREO) || (pstrEncParams->s16ChannelMode == SBC_JOINT_STEREO)) {
180 sbc_enc_bit_alloc_ste(pstrEncParams);
181 } else {
182 sbc_enc_bit_alloc_mono(pstrEncParams);
183 }
184
185 /* Quantize the encoded audio */
186 EncPacking(pstrEncParams);
187 } while (--(pstrEncParams->u8NumPacketToEncode));
188
189 pstrEncParams->u8NumPacketToEncode = 1; /* default is one for retrocompatibility purpose */
190
191 }
192
193 /****************************************************************************
194 * InitSbcAnalysisFilt - Initalizes the input data to 0
195 *
196 * RETURNS : N/A
197 */
SBC_Encoder_Init(SBC_ENC_PARAMS * pstrEncParams)198 void SBC_Encoder_Init(SBC_ENC_PARAMS *pstrEncParams)
199 {
200 UINT16 s16SamplingFreq; /*temp variable to store smpling freq*/
201 SINT16 s16Bitpool; /*to store bit pool value*/
202 SINT16 s16BitRate; /*to store bitrate*/
203 SINT16 s16FrameLen; /*to store frame length*/
204 UINT16 HeaderParams;
205
206 pstrEncParams->u8NumPacketToEncode = 1; /* default is one for retrocompatibility purpose */
207
208 if (pstrEncParams->sbc_mode != SBC_MODE_MSBC) {
209 /* Required number of channels */
210 if (pstrEncParams->s16ChannelMode == SBC_MONO) {
211 pstrEncParams->s16NumOfChannels = 1;
212 } else {
213 pstrEncParams->s16NumOfChannels = 2;
214 }
215
216 /* Bit pool calculation */
217 if (pstrEncParams->s16SamplingFreq == SBC_sf16000) {
218 s16SamplingFreq = 16000;
219 } else if (pstrEncParams->s16SamplingFreq == SBC_sf32000) {
220 s16SamplingFreq = 32000;
221 } else if (pstrEncParams->s16SamplingFreq == SBC_sf44100) {
222 s16SamplingFreq = 44100;
223 } else {
224 s16SamplingFreq = 48000;
225 }
226
227 if ( (pstrEncParams->s16ChannelMode == SBC_JOINT_STEREO)
228 || (pstrEncParams->s16ChannelMode == SBC_STEREO) ) {
229 s16Bitpool = (SINT16)( (pstrEncParams->u16BitRate *
230 pstrEncParams->s16NumOfSubBands * 1000 / s16SamplingFreq)
231 - ( (32 + (4 * pstrEncParams->s16NumOfSubBands *
232 pstrEncParams->s16NumOfChannels)
233 + ( (pstrEncParams->s16ChannelMode - 2) *
234 pstrEncParams->s16NumOfSubBands ) )
235 / pstrEncParams->s16NumOfBlocks) );
236
237 s16FrameLen = 4 + (4 * pstrEncParams->s16NumOfSubBands *
238 pstrEncParams->s16NumOfChannels) / 8
239 + ( ((pstrEncParams->s16ChannelMode - 2) *
240 pstrEncParams->s16NumOfSubBands)
241 + (pstrEncParams->s16NumOfBlocks * s16Bitpool) ) / 8;
242
243 s16BitRate = (8 * s16FrameLen * s16SamplingFreq)
244 / (pstrEncParams->s16NumOfSubBands *
245 pstrEncParams->s16NumOfBlocks * 1000);
246
247 if (s16BitRate > pstrEncParams->u16BitRate) {
248 s16Bitpool--;
249 }
250
251 if (pstrEncParams->s16NumOfSubBands == 8) {
252 pstrEncParams->s16BitPool = (s16Bitpool > 255) ? 255 : s16Bitpool;
253 } else {
254 pstrEncParams->s16BitPool = (s16Bitpool > 128) ? 128 : s16Bitpool;
255 }
256 } else {
257 s16Bitpool = (SINT16)( ((pstrEncParams->s16NumOfSubBands *
258 pstrEncParams->u16BitRate * 1000)
259 / (s16SamplingFreq * pstrEncParams->s16NumOfChannels))
260 - ( ( (32 / pstrEncParams->s16NumOfChannels) +
261 (4 * pstrEncParams->s16NumOfSubBands) )
262 / pstrEncParams->s16NumOfBlocks ) );
263
264 pstrEncParams->s16BitPool = (s16Bitpool >
265 (16 * pstrEncParams->s16NumOfSubBands))
266 ? (16 * pstrEncParams->s16NumOfSubBands) : s16Bitpool;
267 }
268
269 if (pstrEncParams->s16BitPool < 0) {
270 pstrEncParams->s16BitPool = 0;
271 }
272 /* sampling freq */
273 HeaderParams = ((pstrEncParams->s16SamplingFreq & 3) << 6);
274
275 /* number of blocks*/
276 HeaderParams |= (((pstrEncParams->s16NumOfBlocks - 4) & 12) << 2);
277
278 /* channel mode: mono, dual...*/
279 HeaderParams |= ((pstrEncParams->s16ChannelMode & 3) << 2);
280
281 /* Loudness or SNR */
282 HeaderParams |= ((pstrEncParams->s16AllocationMethod & 1) << 1);
283 HeaderParams |= ((pstrEncParams->s16NumOfSubBands >> 3) & 1); /*4 or 8*/
284
285 pstrEncParams->FrameHeader = HeaderParams;
286 } else {
287 // mSBC
288
289 // Use mSBC encoding parameters to reset the control field
290 /* Required number of channels: 1 */
291 pstrEncParams->s16ChannelMode = SBC_MONO;
292 pstrEncParams->s16NumOfChannels = 1;
293
294 /* Required Sampling frequency : 16KHz */
295 pstrEncParams->s16SamplingFreq = SBC_sf16000;
296
297 /* Bit pool value: 26 */
298 pstrEncParams->s16BitPool = 26;
299
300 /* number of subbands: 8 */
301 pstrEncParams->s16NumOfSubBands = 8;
302
303 /* number of blocks: 15 */
304 pstrEncParams->s16NumOfBlocks = 15;
305
306 /* allocation method: loudness */
307 pstrEncParams->s16AllocationMethod = SBC_LOUDNESS;
308
309 /* set the header paramers, unused for mSBC */
310 pstrEncParams->FrameHeader = 0;
311 }
312
313 if (pstrEncParams->s16NumOfSubBands == 4) {
314 if (pstrEncParams->s16NumOfChannels == 1) {
315 EncMaxShiftCounter = ((ENC_VX_BUFFER_SIZE - 4 * 10) >> 2) << 2;
316 } else {
317 EncMaxShiftCounter = ((ENC_VX_BUFFER_SIZE - 4 * 10 * 2) >> 3) << 2;
318 }
319 } else {
320 if (pstrEncParams->s16NumOfChannels == 1) {
321 EncMaxShiftCounter = ((ENC_VX_BUFFER_SIZE - 8 * 10) >> 3) << 3;
322 } else {
323 EncMaxShiftCounter = ((ENC_VX_BUFFER_SIZE - 8 * 10 * 2) >> 4) << 3;
324 }
325 }
326
327 APPL_TRACE_EVENT("SBC_Encoder_Init : bitrate %d, bitpool %d",
328 pstrEncParams->u16BitRate, pstrEncParams->s16BitPool);
329
330 SbcAnalysisInit();
331 }
332
333 #endif /* #if (defined(SBC_ENC_INCLUDED) && SBC_ENC_INCLUDED == TRUE) */
334