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