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 /************************************************************************************************* 32 * SBC encoder scramble code 33 * Purpose: to tie the SBC code with BTE/mobile stack code, 34 * especially for the case when the SBC is ported into a third-party Multimedia chip 35 * 36 * Algorithm: 37 * init process: all counters reset to 0, 38 * calculate base_index: (6 + s16NumOfChannels*s16NumOfSubBands/2) 39 * scramble side: the init process happens every time SBC_Encoder_Init() is called. 40 * descramble side: it would be nice to know if he "init" process has happened. 41 * alter the SBC SYNC word 0x9C (1001 1100) to 0x8C (1000 1100). 42 * 43 * scramble process: 44 * The CRC byte: 45 * Every SBC frame has a frame header. 46 * The 1st byte is the sync word and the following 2 bytes are about the stream format. 47 * They are supposed to be "constant" within a "song" 48 * The 4th byte is the CRC byte. The CRC byte is bound to be random. 49 * Derive 2 items from the CRC byte; one is the "use" bit, the other is the "index". 50 * 51 * SBC keeps 2 sets of "use" & "index"; derived the current and the previous frame. 52 * 53 * The "use" bit is any bit in SBC_PRTC_USE_MASK is set. 54 * If set, SBC uses the "index" from the current frame. 55 * If not set, SBC uses the "index" from the previous frame or 0. 56 * 57 * index = (CRC & 0x3) + ((CRC & 0x30) >> 2) // 8 is the max index 58 * 59 * if(index > 0) 60 * { 61 * p = &u8frame[base_index]; 62 * if((index&1)&&(u16PacketLength > (base_index+index*2))) 63 * { 64 * // odd index: swap 2 bytes 65 * tmp = p[index]; 66 * p[index] = p[index*2]; 67 * p[index*2] = tmp; 68 * } 69 * else 70 * { 71 * // even index: shift by 3 72 * tmp = (p[index] >> 5) + (p[index] << 3); 73 * p[index] = tmp; 74 * } 75 * } 76 * //else index is 0. The frame stays unaltered 77 * 78 */ 79 80 #define SBC_PRTC_CRC_IDX 3 81 #define SBC_PRTC_USE_MASK 0x64 82 #define SBC_PRTC_SYNC_MASK 0x10 83 #define SBC_PRTC_CIDX 0 84 #define SBC_PRTC_LIDX 1 85 typedef struct 86 { 87 UINT8 use; 88 UINT8 idx; 89 } tSBC_FR_CB; 90 91 typedef struct 92 { 93 tSBC_FR_CB fr[2]; 94 UINT8 init; 95 UINT8 index; 96 UINT8 base; 97 } tSBC_PRTC_CB; 98 tSBC_PRTC_CB sbc_prtc_cb; 99 100 #define SBC_PRTC_IDX(sc) (((sc) & 0x3) + (((sc) & 0x30) >> 2)) 101 #define SBC_PRTC_CHK_INIT(ar) {if(sbc_prtc_cb.init == 0){sbc_prtc_cb.init=1; ar[0] &= ~SBC_PRTC_SYNC_MASK;}} 102 #define SBC_PRTC_C2L() {p_last=&sbc_prtc_cb.fr[SBC_PRTC_LIDX]; p_cur=&sbc_prtc_cb.fr[SBC_PRTC_CIDX]; \ 103 p_last->idx = p_cur->idx; p_last->use = p_cur->use;} 104 #define SBC_PRTC_GETC(ar) {p_cur->use = ar[SBC_PRTC_CRC_IDX] & SBC_PRTC_USE_MASK; \ 105 p_cur->idx = SBC_PRTC_IDX(ar[SBC_PRTC_CRC_IDX]);} 106 #define SBC_PRTC_CHK_CRC(ar) {SBC_PRTC_C2L();SBC_PRTC_GETC(ar);sbc_prtc_cb.index = (p_cur->use)?SBC_PRTC_CIDX:SBC_PRTC_LIDX;} 107 #define SBC_PRTC_SCRMB(ar) {idx = sbc_prtc_cb.fr[sbc_prtc_cb.index].idx; \ 108 if(idx > 0){if((idx&1)&&(pstrEncParams->u16PacketLength > (sbc_prtc_cb.base+(idx<<1)))) {tmp2=idx<<1; tmp=ar[idx];ar[idx]=ar[tmp2];ar[tmp2]=tmp;} \ 109 else{tmp2=ar[idx]; tmp=(tmp2>>5)+(tmp2<<3);ar[idx]=(UINT8)tmp;}}} 110 111 #if (SBC_JOINT_STE_INCLUDED == TRUE) 112 SINT32 s32LRDiff[SBC_MAX_NUM_OF_BLOCKS] = {0}; 113 SINT32 s32LRSum[SBC_MAX_NUM_OF_BLOCKS] = {0}; 114 #endif 115 116 void SBC_Encoder(SBC_ENC_PARAMS *pstrEncParams) 117 { 118 SINT32 s32Ch; /* counter for ch*/ 119 SINT32 s32Sb; /* counter for sub-band*/ 120 UINT32 u32Count, maxBit = 0; /* loop count*/ 121 SINT32 s32MaxValue; /* temp variable to store max value */ 122 123 SINT16 *ps16ScfL; 124 SINT32 *SbBuffer; 125 SINT32 s32Blk; /* counter for block*/ 126 SINT32 s32NumOfBlocks = pstrEncParams->s16NumOfBlocks; 127 #if (SBC_JOINT_STE_INCLUDED == TRUE) 128 SINT32 s32MaxValue2; 129 UINT32 u32CountSum,u32CountDiff; 130 SINT32 *pSum, *pDiff; 131 #endif 132 UINT8 *pu8; 133 tSBC_FR_CB *p_cur, *p_last; 134 UINT32 idx, tmp, tmp2; 135 register SINT32 s32NumOfSubBands = pstrEncParams->s16NumOfSubBands; 136 137 pstrEncParams->pu8NextPacket = pstrEncParams->pu8Packet; 138 139 #if (SBC_NO_PCM_CPY_OPTION == TRUE) 140 pstrEncParams->ps16NextPcmBuffer = pstrEncParams->ps16PcmBuffer; 141 #else 142 pstrEncParams->ps16NextPcmBuffer = pstrEncParams->as16PcmBuffer; 143 #endif 144 do 145 { 146 /* SBC ananlysis filter*/ 147 if (s32NumOfSubBands == 4) 148 SbcAnalysisFilter4(pstrEncParams); 149 else 150 SbcAnalysisFilter8(pstrEncParams); 151 152 /* compute the scale factor, and save the max */ 153 ps16ScfL = pstrEncParams->as16ScaleFactor; 154 s32Ch=pstrEncParams->s16NumOfChannels*s32NumOfSubBands; 155 156 pstrEncParams->ps16NextPcmBuffer+=s32Ch*s32NumOfBlocks; /* in case of multible sbc frame to encode update the pcm pointer */ 157 158 for (s32Sb=0; s32Sb<s32Ch; s32Sb++) 159 { 160 SbBuffer=pstrEncParams->s32SbBuffer+s32Sb; 161 s32MaxValue=0; 162 for (s32Blk=s32NumOfBlocks;s32Blk>0;s32Blk--) 163 { 164 if (s32MaxValue<abs32(*SbBuffer)) 165 s32MaxValue=abs32(*SbBuffer); 166 SbBuffer+=s32Ch; 167 } 168 169 u32Count = (s32MaxValue > 0x800000) ? 9 : 0; 170 171 for ( ; u32Count < 15; u32Count++) 172 { 173 if (s32MaxValue <= (SINT32)(0x8000 << u32Count)) 174 break; 175 } 176 *ps16ScfL++ = (SINT16)u32Count; 177 178 if (u32Count > maxBit) 179 maxBit = u32Count; 180 } 181 /* In case of JS processing,check whether to use JS */ 182 #if (SBC_JOINT_STE_INCLUDED == TRUE) 183 if (pstrEncParams->s16ChannelMode == SBC_JOINT_STEREO) 184 { 185 /* Calculate sum and differance scale factors for making JS decision */ 186 ps16ScfL = pstrEncParams->as16ScaleFactor ; 187 /* calculate the scale factor of Joint stereo max sum and diff */ 188 for (s32Sb = 0; s32Sb < s32NumOfSubBands-1; s32Sb++) 189 { 190 SbBuffer=pstrEncParams->s32SbBuffer+s32Sb; 191 s32MaxValue2=0; 192 s32MaxValue=0; 193 pSum = s32LRSum; 194 pDiff = s32LRDiff; 195 for (s32Blk=0;s32Blk<s32NumOfBlocks;s32Blk++) 196 { 197 *pSum=(*SbBuffer+*(SbBuffer+s32NumOfSubBands))>>1; 198 if (abs32(*pSum)>s32MaxValue) 199 s32MaxValue=abs32(*pSum); 200 pSum++; 201 *pDiff=(*SbBuffer-*(SbBuffer+s32NumOfSubBands))>>1; 202 if (abs32(*pDiff)>s32MaxValue2) 203 s32MaxValue2=abs32(*pDiff); 204 pDiff++; 205 SbBuffer+=s32Ch; 206 } 207 u32Count = (s32MaxValue > 0x800000) ? 9 : 0; 208 for ( ; u32Count < 15; u32Count++) 209 { 210 if (s32MaxValue <= (SINT32)(0x8000 << u32Count)) 211 break; 212 } 213 u32CountSum=u32Count; 214 u32Count = (s32MaxValue2 > 0x800000) ? 9 : 0; 215 for ( ; u32Count < 15; u32Count++) 216 { 217 if (s32MaxValue2 <= (SINT32)(0x8000 << u32Count)) 218 break; 219 } 220 u32CountDiff=u32Count; 221 if ( (*ps16ScfL + *(ps16ScfL+s32NumOfSubBands)) > (SINT16)(u32CountSum + u32CountDiff) ) 222 { 223 224 if (u32CountSum > maxBit) 225 maxBit = u32CountSum; 226 227 if (u32CountDiff > maxBit) 228 maxBit = u32CountDiff; 229 230 *ps16ScfL = (SINT16)u32CountSum; 231 *(ps16ScfL+s32NumOfSubBands) = (SINT16)u32CountDiff; 232 233 SbBuffer=pstrEncParams->s32SbBuffer+s32Sb; 234 pSum = s32LRSum; 235 pDiff = s32LRDiff; 236 237 for (s32Blk = 0; s32Blk < s32NumOfBlocks; s32Blk++) 238 { 239 *SbBuffer = *pSum; 240 *(SbBuffer+s32NumOfSubBands) = *pDiff; 241 242 SbBuffer += s32NumOfSubBands<<1; 243 pSum++; 244 pDiff++; 245 } 246 247 pstrEncParams->as16Join[s32Sb] = 1; 248 } 249 else 250 { 251 pstrEncParams->as16Join[s32Sb] = 0; 252 } 253 ps16ScfL++; 254 } 255 pstrEncParams->as16Join[s32Sb] = 0; 256 } 257 #endif 258 259 pstrEncParams->s16MaxBitNeed = (SINT16)maxBit; 260 261 /* bit allocation */ 262 if ((pstrEncParams->s16ChannelMode == SBC_STEREO) || (pstrEncParams->s16ChannelMode == SBC_JOINT_STEREO)) 263 sbc_enc_bit_alloc_ste(pstrEncParams); 264 else 265 sbc_enc_bit_alloc_mono(pstrEncParams); 266 267 /* save the beginning of the frame. pu8NextPacket is modified in EncPacking() */ 268 pu8 = pstrEncParams->pu8NextPacket; 269 /* Quantize the encoded audio */ 270 EncPacking(pstrEncParams); 271 272 /* scramble the code */ 273 SBC_PRTC_CHK_INIT(pu8); 274 SBC_PRTC_CHK_CRC(pu8); 275 #if 0 276 if(pstrEncParams->u16PacketLength > ((sbc_prtc_cb.fr[sbc_prtc_cb.index].idx * 2) + sbc_prtc_cb.base)) 277 printf("len: %d, idx: %d\n", pstrEncParams->u16PacketLength, sbc_prtc_cb.fr[sbc_prtc_cb.index].idx); 278 else 279 printf("len: %d, idx: %d!!!!\n", pstrEncParams->u16PacketLength, sbc_prtc_cb.fr[sbc_prtc_cb.index].idx); 280 #endif 281 SBC_PRTC_SCRMB((&pu8[sbc_prtc_cb.base])); 282 } 283 while(--(pstrEncParams->u8NumPacketToEncode)); 284 285 pstrEncParams->u8NumPacketToEncode = 1; /* default is one for retrocompatibility purpose */ 286 287 } 288 289 /**************************************************************************** 290 * InitSbcAnalysisFilt - Initalizes the input data to 0 291 * 292 * RETURNS : N/A 293 */ 294 void SBC_Encoder_Init(SBC_ENC_PARAMS *pstrEncParams) 295 { 296 UINT16 s16SamplingFreq; /*temp variable to store smpling freq*/ 297 SINT16 s16Bitpool; /*to store bit pool value*/ 298 SINT16 s16BitRate; /*to store bitrate*/ 299 SINT16 s16FrameLen; /*to store frame length*/ 300 UINT16 HeaderParams; 301 302 pstrEncParams->u8NumPacketToEncode = 1; /* default is one for retrocompatibility purpose */ 303 304 /* Required number of channels */ 305 if (pstrEncParams->s16ChannelMode == SBC_MONO) 306 pstrEncParams->s16NumOfChannels = 1; 307 else 308 pstrEncParams->s16NumOfChannels = 2; 309 310 /* Bit pool calculation */ 311 if (pstrEncParams->s16SamplingFreq == SBC_sf16000) 312 s16SamplingFreq = 16000; 313 else if (pstrEncParams->s16SamplingFreq == SBC_sf32000) 314 s16SamplingFreq = 32000; 315 else if (pstrEncParams->s16SamplingFreq == SBC_sf44100) 316 s16SamplingFreq = 44100; 317 else 318 s16SamplingFreq = 48000; 319 320 if ( (pstrEncParams->s16ChannelMode == SBC_JOINT_STEREO) 321 || (pstrEncParams->s16ChannelMode == SBC_STEREO) ) 322 { 323 s16Bitpool = (SINT16)( (pstrEncParams->u16BitRate * 324 pstrEncParams->s16NumOfSubBands * 1000 / s16SamplingFreq) 325 -( (32 + (4 * pstrEncParams->s16NumOfSubBands * 326 pstrEncParams->s16NumOfChannels) 327 + ( (pstrEncParams->s16ChannelMode - 2) * 328 pstrEncParams->s16NumOfSubBands ) ) 329 / pstrEncParams->s16NumOfBlocks) ); 330 331 s16FrameLen = 4 + (4*pstrEncParams->s16NumOfSubBands* 332 pstrEncParams->s16NumOfChannels)/8 333 + ( ((pstrEncParams->s16ChannelMode - 2) * 334 pstrEncParams->s16NumOfSubBands) 335 + (pstrEncParams->s16NumOfBlocks * s16Bitpool) ) / 8; 336 337 s16BitRate = (8 * s16FrameLen * s16SamplingFreq) 338 / (pstrEncParams->s16NumOfSubBands * 339 pstrEncParams->s16NumOfBlocks * 1000); 340 341 if (s16BitRate > pstrEncParams->u16BitRate) 342 s16Bitpool--; 343 344 if(pstrEncParams->s16NumOfSubBands == 8) 345 pstrEncParams->s16BitPool = (s16Bitpool > 255) ? 255 : s16Bitpool; 346 else 347 pstrEncParams->s16BitPool = (s16Bitpool > 128) ? 128 : s16Bitpool; 348 } 349 else 350 { 351 s16Bitpool = (SINT16)( ((pstrEncParams->s16NumOfSubBands * 352 pstrEncParams->u16BitRate * 1000) 353 / (s16SamplingFreq * pstrEncParams->s16NumOfChannels)) 354 -( ( (32 / pstrEncParams->s16NumOfChannels) + 355 (4 * pstrEncParams->s16NumOfSubBands) ) 356 / pstrEncParams->s16NumOfBlocks ) ); 357 358 pstrEncParams->s16BitPool = (s16Bitpool > 359 (16 * pstrEncParams->s16NumOfSubBands)) 360 ? (16*pstrEncParams->s16NumOfSubBands) : s16Bitpool; 361 } 362 363 if (pstrEncParams->s16BitPool < 0) 364 pstrEncParams->s16BitPool = 0; 365 /* sampling freq */ 366 HeaderParams = ((pstrEncParams->s16SamplingFreq & 3)<< 6); 367 368 /* number of blocks*/ 369 HeaderParams |= (((pstrEncParams->s16NumOfBlocks -4) & 12) << 2); 370 371 /* channel mode: mono, dual...*/ 372 HeaderParams |= ((pstrEncParams->s16ChannelMode & 3)<< 2); 373 374 /* Loudness or SNR */ 375 HeaderParams |= ((pstrEncParams->s16AllocationMethod & 1)<< 1); 376 HeaderParams |= ((pstrEncParams->s16NumOfSubBands >> 3) & 1); /*4 or 8*/ 377 pstrEncParams->FrameHeader=HeaderParams; 378 379 if (pstrEncParams->s16NumOfSubBands==4) 380 { 381 if (pstrEncParams->s16NumOfChannels==1) 382 EncMaxShiftCounter=((ENC_VX_BUFFER_SIZE-4*10)>>2)<<2; 383 else 384 EncMaxShiftCounter=((ENC_VX_BUFFER_SIZE-4*10*2)>>3)<<2; 385 } 386 else 387 { 388 if (pstrEncParams->s16NumOfChannels==1) 389 EncMaxShiftCounter=((ENC_VX_BUFFER_SIZE-8*10)>>3)<<3; 390 else 391 EncMaxShiftCounter=((ENC_VX_BUFFER_SIZE-8*10*2)>>4)<<3; 392 } 393 394 APPL_TRACE_EVENT2("SBC_Encoder_Init : bitrate %d, bitpool %d", 395 pstrEncParams->u16BitRate, pstrEncParams->s16BitPool); 396 397 SbcAnalysisInit(); 398 399 memset(&sbc_prtc_cb, 0, sizeof(tSBC_PRTC_CB)); 400 sbc_prtc_cb.base = 6 + pstrEncParams->s16NumOfChannels*pstrEncParams->s16NumOfSubBands/2; 401 } 402