2016-11-04 03:08:30 -04:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999-2012 Broadcom Corporation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* contains code for encoder flow and initalization of encoder
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include <string.h>
|
2018-04-08 00:10:50 -04:00
|
|
|
#include "common/bt_target.h"
|
2016-11-04 03:08:30 -04:00
|
|
|
#include "sbc_encoder.h"
|
|
|
|
#include "sbc_enc_func_declare.h"
|
|
|
|
|
2017-03-17 03:57:30 -04:00
|
|
|
#if (defined(SBC_ENC_INCLUDED) && SBC_ENC_INCLUDED == TRUE)
|
|
|
|
|
2016-11-04 03:08:30 -04:00
|
|
|
SINT16 EncMaxShiftCounter;
|
|
|
|
|
|
|
|
#if (SBC_JOINT_STE_INCLUDED == TRUE)
|
|
|
|
SINT32 s32LRDiff[SBC_MAX_NUM_OF_BLOCKS] = {0};
|
|
|
|
SINT32 s32LRSum[SBC_MAX_NUM_OF_BLOCKS] = {0};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void SBC_Encoder(SBC_ENC_PARAMS *pstrEncParams)
|
|
|
|
{
|
|
|
|
SINT32 s32Ch; /* counter for ch*/
|
|
|
|
SINT32 s32Sb; /* counter for sub-band*/
|
|
|
|
UINT32 u32Count, maxBit = 0; /* loop count*/
|
|
|
|
SINT32 s32MaxValue; /* temp variable to store max value */
|
|
|
|
|
|
|
|
SINT16 *ps16ScfL;
|
|
|
|
SINT32 *SbBuffer;
|
|
|
|
SINT32 s32Blk; /* counter for block*/
|
|
|
|
SINT32 s32NumOfBlocks = pstrEncParams->s16NumOfBlocks;
|
|
|
|
#if (SBC_JOINT_STE_INCLUDED == TRUE)
|
|
|
|
SINT32 s32MaxValue2;
|
2016-11-26 00:09:55 -05:00
|
|
|
UINT32 u32CountSum, u32CountDiff;
|
2016-11-04 03:08:30 -04:00
|
|
|
SINT32 *pSum, *pDiff;
|
|
|
|
#endif
|
|
|
|
register SINT32 s32NumOfSubBands = pstrEncParams->s16NumOfSubBands;
|
|
|
|
|
|
|
|
pstrEncParams->pu8NextPacket = pstrEncParams->pu8Packet;
|
|
|
|
|
|
|
|
#if (SBC_NO_PCM_CPY_OPTION == TRUE)
|
|
|
|
pstrEncParams->ps16NextPcmBuffer = pstrEncParams->ps16PcmBuffer;
|
|
|
|
#else
|
|
|
|
pstrEncParams->ps16NextPcmBuffer = pstrEncParams->as16PcmBuffer;
|
|
|
|
#endif
|
2016-11-26 00:09:55 -05:00
|
|
|
do {
|
2016-11-04 03:08:30 -04:00
|
|
|
/* SBC ananlysis filter*/
|
2016-11-26 00:09:55 -05:00
|
|
|
if (s32NumOfSubBands == 4) {
|
2016-11-04 03:08:30 -04:00
|
|
|
SbcAnalysisFilter4(pstrEncParams);
|
2016-11-26 00:09:55 -05:00
|
|
|
} else {
|
2016-11-04 03:08:30 -04:00
|
|
|
SbcAnalysisFilter8(pstrEncParams);
|
2016-11-26 00:09:55 -05:00
|
|
|
}
|
2016-11-04 03:08:30 -04:00
|
|
|
|
|
|
|
/* compute the scale factor, and save the max */
|
|
|
|
ps16ScfL = pstrEncParams->as16ScaleFactor;
|
2016-11-26 00:09:55 -05:00
|
|
|
s32Ch = pstrEncParams->s16NumOfChannels * s32NumOfSubBands;
|
|
|
|
|
|
|
|
pstrEncParams->ps16NextPcmBuffer += s32Ch * s32NumOfBlocks; /* in case of multible sbc frame to encode update the pcm pointer */
|
|
|
|
|
|
|
|
for (s32Sb = 0; s32Sb < s32Ch; s32Sb++) {
|
|
|
|
SbBuffer = pstrEncParams->s32SbBuffer + s32Sb;
|
|
|
|
s32MaxValue = 0;
|
|
|
|
for (s32Blk = s32NumOfBlocks; s32Blk > 0; s32Blk--) {
|
|
|
|
if (s32MaxValue < abs32(*SbBuffer)) {
|
|
|
|
s32MaxValue = abs32(*SbBuffer);
|
|
|
|
}
|
|
|
|
SbBuffer += s32Ch;
|
2016-11-04 03:08:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
u32Count = (s32MaxValue > 0x800000) ? 9 : 0;
|
|
|
|
|
2016-11-26 00:09:55 -05:00
|
|
|
for ( ; u32Count < 15; u32Count++) {
|
|
|
|
if (s32MaxValue <= (SINT32)(0x8000 << u32Count)) {
|
2016-11-04 03:08:30 -04:00
|
|
|
break;
|
2016-11-26 00:09:55 -05:00
|
|
|
}
|
2016-11-04 03:08:30 -04:00
|
|
|
}
|
|
|
|
*ps16ScfL++ = (SINT16)u32Count;
|
|
|
|
|
2016-11-26 00:09:55 -05:00
|
|
|
if (u32Count > maxBit) {
|
2016-11-04 03:08:30 -04:00
|
|
|
maxBit = u32Count;
|
2016-11-26 00:09:55 -05:00
|
|
|
}
|
2016-11-04 03:08:30 -04:00
|
|
|
}
|
|
|
|
/* In case of JS processing,check whether to use JS */
|
|
|
|
#if (SBC_JOINT_STE_INCLUDED == TRUE)
|
2016-11-26 00:09:55 -05:00
|
|
|
if (pstrEncParams->s16ChannelMode == SBC_JOINT_STEREO) {
|
2016-11-04 03:08:30 -04:00
|
|
|
/* Calculate sum and differance scale factors for making JS decision */
|
|
|
|
ps16ScfL = pstrEncParams->as16ScaleFactor ;
|
|
|
|
/* calculate the scale factor of Joint stereo max sum and diff */
|
2016-11-26 00:09:55 -05:00
|
|
|
for (s32Sb = 0; s32Sb < s32NumOfSubBands - 1; s32Sb++) {
|
|
|
|
SbBuffer = pstrEncParams->s32SbBuffer + s32Sb;
|
|
|
|
s32MaxValue2 = 0;
|
|
|
|
s32MaxValue = 0;
|
2016-11-04 03:08:30 -04:00
|
|
|
pSum = s32LRSum;
|
|
|
|
pDiff = s32LRDiff;
|
2016-11-26 00:09:55 -05:00
|
|
|
for (s32Blk = 0; s32Blk < s32NumOfBlocks; s32Blk++) {
|
|
|
|
*pSum = (*SbBuffer + * (SbBuffer + s32NumOfSubBands)) >> 1;
|
|
|
|
if (abs32(*pSum) > s32MaxValue) {
|
|
|
|
s32MaxValue = abs32(*pSum);
|
|
|
|
}
|
2016-11-04 03:08:30 -04:00
|
|
|
pSum++;
|
2016-11-26 00:09:55 -05:00
|
|
|
*pDiff = (*SbBuffer - * (SbBuffer + s32NumOfSubBands)) >> 1;
|
|
|
|
if (abs32(*pDiff) > s32MaxValue2) {
|
|
|
|
s32MaxValue2 = abs32(*pDiff);
|
|
|
|
}
|
2016-11-04 03:08:30 -04:00
|
|
|
pDiff++;
|
2016-11-26 00:09:55 -05:00
|
|
|
SbBuffer += s32Ch;
|
2016-11-04 03:08:30 -04:00
|
|
|
}
|
|
|
|
u32Count = (s32MaxValue > 0x800000) ? 9 : 0;
|
2016-11-26 00:09:55 -05:00
|
|
|
for ( ; u32Count < 15; u32Count++) {
|
|
|
|
if (s32MaxValue <= (SINT32)(0x8000 << u32Count)) {
|
2016-11-04 03:08:30 -04:00
|
|
|
break;
|
2016-11-26 00:09:55 -05:00
|
|
|
}
|
2016-11-04 03:08:30 -04:00
|
|
|
}
|
2016-11-26 00:09:55 -05:00
|
|
|
u32CountSum = u32Count;
|
2016-11-04 03:08:30 -04:00
|
|
|
u32Count = (s32MaxValue2 > 0x800000) ? 9 : 0;
|
2016-11-26 00:09:55 -05:00
|
|
|
for ( ; u32Count < 15; u32Count++) {
|
|
|
|
if (s32MaxValue2 <= (SINT32)(0x8000 << u32Count)) {
|
2016-11-04 03:08:30 -04:00
|
|
|
break;
|
2016-11-26 00:09:55 -05:00
|
|
|
}
|
2016-11-04 03:08:30 -04:00
|
|
|
}
|
2016-11-26 00:09:55 -05:00
|
|
|
u32CountDiff = u32Count;
|
|
|
|
if ( (*ps16ScfL + * (ps16ScfL + s32NumOfSubBands)) > (SINT16)(u32CountSum + u32CountDiff) ) {
|
2016-11-04 03:08:30 -04:00
|
|
|
|
2016-11-26 00:09:55 -05:00
|
|
|
if (u32CountSum > maxBit) {
|
2016-11-04 03:08:30 -04:00
|
|
|
maxBit = u32CountSum;
|
2016-11-26 00:09:55 -05:00
|
|
|
}
|
2016-11-04 03:08:30 -04:00
|
|
|
|
2016-11-26 00:09:55 -05:00
|
|
|
if (u32CountDiff > maxBit) {
|
2016-11-04 03:08:30 -04:00
|
|
|
maxBit = u32CountDiff;
|
2016-11-26 00:09:55 -05:00
|
|
|
}
|
2016-11-04 03:08:30 -04:00
|
|
|
|
|
|
|
*ps16ScfL = (SINT16)u32CountSum;
|
2016-11-26 00:09:55 -05:00
|
|
|
*(ps16ScfL + s32NumOfSubBands) = (SINT16)u32CountDiff;
|
2016-11-04 03:08:30 -04:00
|
|
|
|
2016-11-26 00:09:55 -05:00
|
|
|
SbBuffer = pstrEncParams->s32SbBuffer + s32Sb;
|
2016-11-04 03:08:30 -04:00
|
|
|
pSum = s32LRSum;
|
|
|
|
pDiff = s32LRDiff;
|
|
|
|
|
2016-11-26 00:09:55 -05:00
|
|
|
for (s32Blk = 0; s32Blk < s32NumOfBlocks; s32Blk++) {
|
2016-11-04 03:08:30 -04:00
|
|
|
*SbBuffer = *pSum;
|
2016-11-26 00:09:55 -05:00
|
|
|
*(SbBuffer + s32NumOfSubBands) = *pDiff;
|
2016-11-04 03:08:30 -04:00
|
|
|
|
2016-11-26 00:09:55 -05:00
|
|
|
SbBuffer += s32NumOfSubBands << 1;
|
2016-11-04 03:08:30 -04:00
|
|
|
pSum++;
|
|
|
|
pDiff++;
|
|
|
|
}
|
|
|
|
|
|
|
|
pstrEncParams->as16Join[s32Sb] = 1;
|
2016-11-26 00:09:55 -05:00
|
|
|
} else {
|
2016-11-04 03:08:30 -04:00
|
|
|
pstrEncParams->as16Join[s32Sb] = 0;
|
|
|
|
}
|
|
|
|
ps16ScfL++;
|
|
|
|
}
|
|
|
|
pstrEncParams->as16Join[s32Sb] = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
pstrEncParams->s16MaxBitNeed = (SINT16)maxBit;
|
|
|
|
|
|
|
|
/* bit allocation */
|
2016-11-26 00:09:55 -05:00
|
|
|
if ((pstrEncParams->s16ChannelMode == SBC_STEREO) || (pstrEncParams->s16ChannelMode == SBC_JOINT_STEREO)) {
|
2016-11-04 03:08:30 -04:00
|
|
|
sbc_enc_bit_alloc_ste(pstrEncParams);
|
2016-11-26 00:09:55 -05:00
|
|
|
} else {
|
2016-11-04 03:08:30 -04:00
|
|
|
sbc_enc_bit_alloc_mono(pstrEncParams);
|
2016-11-26 00:09:55 -05:00
|
|
|
}
|
2016-11-04 03:08:30 -04:00
|
|
|
|
|
|
|
/* Quantize the encoded audio */
|
|
|
|
EncPacking(pstrEncParams);
|
2016-11-26 00:09:55 -05:00
|
|
|
} while (--(pstrEncParams->u8NumPacketToEncode));
|
2016-11-04 03:08:30 -04:00
|
|
|
|
|
|
|
pstrEncParams->u8NumPacketToEncode = 1; /* default is one for retrocompatibility purpose */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* InitSbcAnalysisFilt - Initalizes the input data to 0
|
|
|
|
*
|
|
|
|
* RETURNS : N/A
|
|
|
|
*/
|
|
|
|
void SBC_Encoder_Init(SBC_ENC_PARAMS *pstrEncParams)
|
|
|
|
{
|
|
|
|
UINT16 s16SamplingFreq; /*temp variable to store smpling freq*/
|
|
|
|
SINT16 s16Bitpool; /*to store bit pool value*/
|
|
|
|
SINT16 s16BitRate; /*to store bitrate*/
|
|
|
|
SINT16 s16FrameLen; /*to store frame length*/
|
|
|
|
UINT16 HeaderParams;
|
|
|
|
|
|
|
|
pstrEncParams->u8NumPacketToEncode = 1; /* default is one for retrocompatibility purpose */
|
|
|
|
|
2018-05-25 02:23:38 -04:00
|
|
|
if (pstrEncParams->sbc_mode != SBC_MODE_MSBC) {
|
|
|
|
/* Required number of channels */
|
|
|
|
if (pstrEncParams->s16ChannelMode == SBC_MONO) {
|
|
|
|
pstrEncParams->s16NumOfChannels = 1;
|
|
|
|
} else {
|
|
|
|
pstrEncParams->s16NumOfChannels = 2;
|
|
|
|
}
|
2016-11-04 03:08:30 -04:00
|
|
|
|
2018-05-25 02:23:38 -04:00
|
|
|
/* Bit pool calculation */
|
|
|
|
if (pstrEncParams->s16SamplingFreq == SBC_sf16000) {
|
|
|
|
s16SamplingFreq = 16000;
|
|
|
|
} else if (pstrEncParams->s16SamplingFreq == SBC_sf32000) {
|
|
|
|
s16SamplingFreq = 32000;
|
|
|
|
} else if (pstrEncParams->s16SamplingFreq == SBC_sf44100) {
|
|
|
|
s16SamplingFreq = 44100;
|
|
|
|
} else {
|
|
|
|
s16SamplingFreq = 48000;
|
2016-11-26 00:09:55 -05:00
|
|
|
}
|
2016-11-04 03:08:30 -04:00
|
|
|
|
2018-05-25 02:23:38 -04:00
|
|
|
if ( (pstrEncParams->s16ChannelMode == SBC_JOINT_STEREO)
|
|
|
|
|| (pstrEncParams->s16ChannelMode == SBC_STEREO) ) {
|
|
|
|
s16Bitpool = (SINT16)( (pstrEncParams->u16BitRate *
|
|
|
|
pstrEncParams->s16NumOfSubBands * 1000 / s16SamplingFreq)
|
|
|
|
- ( (32 + (4 * pstrEncParams->s16NumOfSubBands *
|
|
|
|
pstrEncParams->s16NumOfChannels)
|
|
|
|
+ ( (pstrEncParams->s16ChannelMode - 2) *
|
|
|
|
pstrEncParams->s16NumOfSubBands ) )
|
|
|
|
/ pstrEncParams->s16NumOfBlocks) );
|
|
|
|
|
|
|
|
s16FrameLen = 4 + (4 * pstrEncParams->s16NumOfSubBands *
|
|
|
|
pstrEncParams->s16NumOfChannels) / 8
|
|
|
|
+ ( ((pstrEncParams->s16ChannelMode - 2) *
|
|
|
|
pstrEncParams->s16NumOfSubBands)
|
|
|
|
+ (pstrEncParams->s16NumOfBlocks * s16Bitpool) ) / 8;
|
|
|
|
|
|
|
|
s16BitRate = (8 * s16FrameLen * s16SamplingFreq)
|
|
|
|
/ (pstrEncParams->s16NumOfSubBands *
|
|
|
|
pstrEncParams->s16NumOfBlocks * 1000);
|
|
|
|
|
|
|
|
if (s16BitRate > pstrEncParams->u16BitRate) {
|
|
|
|
s16Bitpool--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pstrEncParams->s16NumOfSubBands == 8) {
|
|
|
|
pstrEncParams->s16BitPool = (s16Bitpool > 255) ? 255 : s16Bitpool;
|
|
|
|
} else {
|
|
|
|
pstrEncParams->s16BitPool = (s16Bitpool > 128) ? 128 : s16Bitpool;
|
|
|
|
}
|
2016-11-26 00:09:55 -05:00
|
|
|
} else {
|
2018-05-25 02:23:38 -04:00
|
|
|
s16Bitpool = (SINT16)( ((pstrEncParams->s16NumOfSubBands *
|
|
|
|
pstrEncParams->u16BitRate * 1000)
|
|
|
|
/ (s16SamplingFreq * pstrEncParams->s16NumOfChannels))
|
|
|
|
- ( ( (32 / pstrEncParams->s16NumOfChannels) +
|
|
|
|
(4 * pstrEncParams->s16NumOfSubBands) )
|
|
|
|
/ pstrEncParams->s16NumOfBlocks ) );
|
|
|
|
|
|
|
|
pstrEncParams->s16BitPool = (s16Bitpool >
|
|
|
|
(16 * pstrEncParams->s16NumOfSubBands))
|
|
|
|
? (16 * pstrEncParams->s16NumOfSubBands) : s16Bitpool;
|
2016-11-26 00:09:55 -05:00
|
|
|
}
|
2018-05-25 02:23:38 -04:00
|
|
|
|
|
|
|
if (pstrEncParams->s16BitPool < 0) {
|
|
|
|
pstrEncParams->s16BitPool = 0;
|
|
|
|
}
|
|
|
|
/* sampling freq */
|
|
|
|
HeaderParams = ((pstrEncParams->s16SamplingFreq & 3) << 6);
|
|
|
|
|
|
|
|
/* number of blocks*/
|
|
|
|
HeaderParams |= (((pstrEncParams->s16NumOfBlocks - 4) & 12) << 2);
|
|
|
|
|
|
|
|
/* channel mode: mono, dual...*/
|
|
|
|
HeaderParams |= ((pstrEncParams->s16ChannelMode & 3) << 2);
|
|
|
|
|
|
|
|
/* Loudness or SNR */
|
|
|
|
HeaderParams |= ((pstrEncParams->s16AllocationMethod & 1) << 1);
|
|
|
|
HeaderParams |= ((pstrEncParams->s16NumOfSubBands >> 3) & 1); /*4 or 8*/
|
|
|
|
|
|
|
|
pstrEncParams->FrameHeader = HeaderParams;
|
2016-11-26 00:09:55 -05:00
|
|
|
} else {
|
2018-05-25 02:23:38 -04:00
|
|
|
// mSBC
|
2016-11-04 03:08:30 -04:00
|
|
|
|
2018-05-25 02:23:38 -04:00
|
|
|
// Use mSBC encoding parameters to reset the control field
|
|
|
|
/* Required number of channels: 1 */
|
|
|
|
pstrEncParams->s16ChannelMode = SBC_MONO;
|
|
|
|
pstrEncParams->s16NumOfChannels = 1;
|
|
|
|
|
|
|
|
/* Required Sampling frequency : 16KHz */
|
|
|
|
pstrEncParams->s16SamplingFreq = SBC_sf16000;
|
2016-11-04 03:08:30 -04:00
|
|
|
|
2018-05-25 02:23:38 -04:00
|
|
|
/* Bit pool value: 26 */
|
|
|
|
pstrEncParams->s16BitPool = 26;
|
2016-11-04 03:08:30 -04:00
|
|
|
|
2018-05-25 02:23:38 -04:00
|
|
|
/* number of subbands: 8 */
|
|
|
|
pstrEncParams->s16NumOfSubBands = 8;
|
2016-11-04 03:08:30 -04:00
|
|
|
|
2018-05-25 02:23:38 -04:00
|
|
|
/* number of blocks: 15 */
|
|
|
|
pstrEncParams->s16NumOfBlocks = 15;
|
|
|
|
|
|
|
|
/* allocation method: loudness */
|
|
|
|
pstrEncParams->s16AllocationMethod = SBC_LOUDNESS;
|
|
|
|
|
|
|
|
/* set the header paramers, unused for mSBC */
|
|
|
|
pstrEncParams->FrameHeader = 0;
|
|
|
|
}
|
2016-11-26 00:09:55 -05:00
|
|
|
|
|
|
|
if (pstrEncParams->s16NumOfSubBands == 4) {
|
|
|
|
if (pstrEncParams->s16NumOfChannels == 1) {
|
|
|
|
EncMaxShiftCounter = ((ENC_VX_BUFFER_SIZE - 4 * 10) >> 2) << 2;
|
|
|
|
} else {
|
|
|
|
EncMaxShiftCounter = ((ENC_VX_BUFFER_SIZE - 4 * 10 * 2) >> 3) << 2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (pstrEncParams->s16NumOfChannels == 1) {
|
|
|
|
EncMaxShiftCounter = ((ENC_VX_BUFFER_SIZE - 8 * 10) >> 3) << 3;
|
|
|
|
} else {
|
|
|
|
EncMaxShiftCounter = ((ENC_VX_BUFFER_SIZE - 8 * 10 * 2) >> 4) << 3;
|
|
|
|
}
|
2016-11-04 03:08:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
APPL_TRACE_EVENT("SBC_Encoder_Init : bitrate %d, bitpool %d",
|
2016-11-26 00:09:55 -05:00
|
|
|
pstrEncParams->u16BitRate, pstrEncParams->s16BitPool);
|
2016-11-04 03:08:30 -04:00
|
|
|
|
|
|
|
SbcAnalysisInit();
|
|
|
|
}
|
2017-03-17 03:57:30 -04:00
|
|
|
|
|
|
|
#endif /* #if (defined(SBC_ENC_INCLUDED) && SBC_ENC_INCLUDED == TRUE) */
|