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