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