Home | History | Annotate | Download | only in srce
      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  *  This file contains the code for bit allocation algorithm. It calculates
     22  *  the number of bits required for the encoded stream of data.
     23  *
     24  ******************************************************************************/
     25 
     26 /*Includes*/
     27 #include "sbc_encoder.h"
     28 #include "sbc_enc_func_declare.h"
     29 
     30 /*global arrays*/
     31 extern const SINT16 sbc_enc_as16Offset4[4][4];
     32 extern const SINT16 sbc_enc_as16Offset8[4][8];
     33 
     34 /****************************************************************************
     35 * BitAlloc - Calculates the required number of bits for the given scale factor
     36 * and the number of subbands.
     37 *
     38 * RETURNS : N/A
     39 */
     40 
     41 void sbc_enc_bit_alloc_ste(SBC_ENC_PARAMS *pstrCodecParams)
     42 {
     43 	/* CAUTIOM -> mips optim for arm 32 require to use SINT32 instead of SINT16 */
     44 	/* Do not change variable type or name */
     45     SINT32 s32MaxBitNeed;   /*to store the max bits needed per sb*/
     46     SINT32 s32BitCount;     /*the used number of bits*/
     47     SINT32 s32SliceCount;   /*to store hwo many slices can be put in bitpool*/
     48     SINT32 s32BitSlice;     /*number of bitslices in bitpool*/
     49     SINT32 s32Sb;           /*counter for sub-band*/
     50     SINT32 s32Ch;           /*counter for channel*/
     51     SINT16 *ps16BitNeed;    /*temp memory to store required number of bits*/
     52     SINT32 s32Loudness;     /*used in Loudness calculation*/
     53     SINT16 *ps16GenBufPtr,*pas16ScaleFactor;
     54     SINT16 *ps16GenArrPtr;
     55     SINT16 *ps16GenTabPtr;
     56     SINT32  s32NumOfSubBands = pstrCodecParams->s16NumOfSubBands;
     57     SINT32  s32BitPool       = pstrCodecParams->s16BitPool;
     58 
     59     /* bitneed values are derived from scale factor */
     60     if (pstrCodecParams->s16AllocationMethod == SBC_SNR)
     61     {
     62         ps16BitNeed   = pstrCodecParams->as16ScaleFactor;
     63         s32MaxBitNeed = pstrCodecParams->s16MaxBitNeed;
     64     }
     65     else
     66     {
     67         ps16BitNeed   = pstrCodecParams->s16ScartchMemForBitAlloc;
     68         pas16ScaleFactor=pstrCodecParams->as16ScaleFactor;
     69         s32MaxBitNeed = 0;
     70         ps16GenBufPtr = ps16BitNeed;
     71         for (s32Ch = 0; s32Ch < 2; s32Ch++)
     72         {
     73             if (s32NumOfSubBands == 4)
     74             {
     75                 ps16GenTabPtr = (SINT16 *)sbc_enc_as16Offset4[pstrCodecParams->s16SamplingFreq];
     76             }
     77             else
     78             {
     79                 ps16GenTabPtr = (SINT16 *)sbc_enc_as16Offset8[pstrCodecParams->s16SamplingFreq];
     80             }
     81 
     82             for (s32Sb = 0; s32Sb < s32NumOfSubBands; s32Sb++)
     83             {
     84                 if (*pas16ScaleFactor == 0)
     85                     *ps16GenBufPtr = -5;
     86                 else
     87                 {
     88                     s32Loudness = (SINT32)(*pas16ScaleFactor - *ps16GenTabPtr);
     89 
     90                     if (s32Loudness > 0)
     91                         *ps16GenBufPtr = (SINT16)(s32Loudness >> 1);
     92                     else
     93                         *ps16GenBufPtr = (SINT16)s32Loudness;
     94                 }
     95 
     96                 if (*ps16GenBufPtr > s32MaxBitNeed)
     97                     s32MaxBitNeed = *ps16GenBufPtr;
     98                 pas16ScaleFactor++;
     99                 ps16GenBufPtr++;
    100                 ps16GenTabPtr++;
    101             }
    102         }
    103     }
    104 
    105     /* iterative process to find out hwo many bitslices fit into the bitpool */
    106     s32BitSlice = s32MaxBitNeed + 1;
    107     s32BitCount = s32BitPool;
    108     s32SliceCount = 0;
    109     do
    110     {
    111         s32BitSlice --;
    112         s32BitCount -= s32SliceCount;
    113         s32SliceCount = 0;
    114         ps16GenBufPtr = ps16BitNeed;
    115 
    116         for (s32Sb = 0; s32Sb < 2*s32NumOfSubBands; s32Sb++)
    117         {
    118             if ( (*ps16GenBufPtr >= s32BitSlice + 1) && (*ps16GenBufPtr < s32BitSlice + 16) )
    119             {
    120                 if (*(ps16GenBufPtr) == s32BitSlice+1)
    121                     s32SliceCount += 2;
    122                 else
    123                     s32SliceCount++;
    124             }
    125             ps16GenBufPtr++;
    126         }
    127     } while (s32BitCount-s32SliceCount>0);
    128 
    129     if (s32BitCount-s32SliceCount == 0)
    130     {
    131         s32BitCount -= s32SliceCount;
    132         s32BitSlice --;
    133     }
    134 
    135     /* Bits are distributed until the last bitslice is reached */
    136     ps16GenBufPtr = ps16BitNeed;
    137     ps16GenArrPtr = pstrCodecParams->as16Bits;
    138     for (s32Ch = 0; s32Ch < 2; s32Ch++)
    139     {
    140         for (s32Sb = 0; s32Sb < s32NumOfSubBands; s32Sb++)
    141         {
    142             if (*ps16GenBufPtr < s32BitSlice+2)
    143                 *ps16GenArrPtr = 0;
    144             else
    145                 *ps16GenArrPtr = ((*(ps16GenBufPtr)-s32BitSlice) < 16) ?
    146                                         (SINT16)(*(ps16GenBufPtr)-s32BitSlice):16;
    147             ps16GenBufPtr++;
    148             ps16GenArrPtr++;
    149         }
    150     }
    151 
    152     /* the remaining bits are allocated starting at subband 0 */
    153     s32Ch=0;
    154     s32Sb=0;
    155     ps16GenBufPtr = ps16BitNeed;
    156     ps16GenArrPtr -= 2*s32NumOfSubBands;
    157 
    158     while ( (s32BitCount > 0) && (s32Sb < s32NumOfSubBands) )
    159     {
    160         if ( (*(ps16GenArrPtr) >= 2) && (*(ps16GenArrPtr) < 16) )
    161         {
    162             (*(ps16GenArrPtr))++;
    163             s32BitCount--;
    164         }
    165         else if ((*ps16GenBufPtr == s32BitSlice+1) && (s32BitCount > 1))
    166         {
    167             *(ps16GenArrPtr) = 2;
    168             s32BitCount -= 2;
    169         }
    170         if(s32Ch == 1)
    171         {
    172             s32Ch = 0;
    173             s32Sb++;
    174             ps16GenBufPtr = ps16BitNeed+s32Sb;
    175             ps16GenArrPtr = pstrCodecParams->as16Bits+s32Sb;
    176 
    177         }
    178         else
    179         {
    180             s32Ch =1;
    181             ps16GenBufPtr = ps16BitNeed+s32NumOfSubBands+s32Sb;
    182             ps16GenArrPtr = pstrCodecParams->as16Bits+s32NumOfSubBands+s32Sb;
    183         }
    184     }
    185 
    186     s32Ch=0;
    187     s32Sb=0;
    188     ps16GenArrPtr = pstrCodecParams->as16Bits;
    189 
    190     while ((s32BitCount >0) && (s32Sb < s32NumOfSubBands))
    191     {
    192         if(*(ps16GenArrPtr) < 16)
    193         {
    194             (*(ps16GenArrPtr))++;
    195             s32BitCount--;
    196         }
    197         if (s32Ch == 1)
    198         {
    199             s32Ch = 0;
    200             s32Sb++;
    201             ps16GenArrPtr = pstrCodecParams->as16Bits+s32Sb;
    202         }
    203         else
    204         {
    205             s32Ch = 1;
    206             ps16GenArrPtr = pstrCodecParams->as16Bits+s32NumOfSubBands+s32Sb;
    207         }
    208     }
    209 }
    210 
    211 /*End of BitAlloc() function*/
    212 
    213