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