Home | History | Annotate | Download | only in srce
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2014 The Android Open Source Project
      4  *  Copyright 2003 - 2004 Open Interface North America, Inc. All rights
      5  *                        reserved.
      6  *
      7  *  Licensed under the Apache License, Version 2.0 (the "License");
      8  *  you may not use this file except in compliance with the License.
      9  *  You may obtain a copy of the License at:
     10  *
     11  *  http://www.apache.org/licenses/LICENSE-2.0
     12  *
     13  *  Unless required by applicable law or agreed to in writing, software
     14  *  distributed under the License is distributed on an "AS IS" BASIS,
     15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16  *  See the License for the specific language governing permissions and
     17  *  limitations under the License.
     18  *
     19  ******************************************************************************/
     20 
     21 /*******************************************************************************
     22   $Revision: #1 $
     23  ******************************************************************************/
     24 
     25 /**
     26 @file
     27 This file drives SBC decoding.
     28 
     29 @ingroup codec_internal
     30 */
     31 
     32 /**
     33 @addtogroup codec_internal
     34 @{
     35 */
     36 
     37 #include <stdio.h>
     38 #include "oi_bitstream.h"
     39 #include "oi_codec_sbc_private.h"
     40 
     41 OI_CHAR* const OI_Codec_Copyright =
     42     "Copyright 2002-2007 Open Interface North America, Inc. All rights "
     43     "reserved";
     44 
     45 INLINE OI_STATUS internal_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT* context,
     46                                        uint32_t* decoderData,
     47                                        uint32_t decoderDataBytes,
     48                                        OI_BYTE maxChannels, OI_BYTE pcmStride,
     49                                        OI_BOOL enhanced) {
     50   OI_UINT i;
     51   OI_STATUS status;
     52 
     53   for (i = 0; i < sizeof(*context); i++) {
     54     ((char*)context)[i] = 0;
     55   }
     56 
     57 #ifdef SBC_ENHANCED
     58   context->enhancedEnabled = enhanced ? TRUE : FALSE;
     59 #else
     60   context->enhancedEnabled = FALSE;
     61   if (enhanced) {
     62     return OI_STATUS_INVALID_PARAMETERS;
     63   }
     64 #endif
     65 
     66   status = OI_CODEC_SBC_Alloc(&context->common, decoderData, decoderDataBytes,
     67                               maxChannels, pcmStride);
     68 
     69   if (!OI_SUCCESS(status)) {
     70     return status;
     71   }
     72 
     73   context->common.codecInfo = OI_Codec_Copyright;
     74   context->common.maxBitneed = 0;
     75   context->limitFrameFormat = FALSE;
     76   OI_SBC_ExpandFrameFields(&context->common.frameInfo);
     77 
     78   /*PLATFORM_DECODER_RESET(context);*/
     79 
     80   return OI_OK;
     81 }
     82 
     83 /**
     84  * Read the SBC header up to but not including the joint stereo mask. The
     85  * syncword has already been examined, and the enhanced mode flag set, by
     86  * FindSyncword.
     87  */
     88 INLINE void OI_SBC_ReadHeader(OI_CODEC_SBC_COMMON_CONTEXT* common,
     89                               const OI_BYTE* data) {
     90   OI_CODEC_SBC_FRAME_INFO* frame = &common->frameInfo;
     91   uint8_t d1;
     92 
     93   OI_ASSERT(data[0] == OI_SBC_SYNCWORD || data[0] == OI_SBC_ENHANCED_SYNCWORD);
     94 
     95   /* Avoid filling out all these strucutures if we already remember the values
     96    * from last time. Just in case we get a stream corresponding to data[1] ==
     97    * 0, DecoderReset is responsible for ensuring the lookup table entries have
     98    * already been populated
     99    */
    100   d1 = data[1];
    101   if (d1 != frame->cachedInfo) {
    102     frame->freqIndex = (d1 & (BIT7 | BIT6)) >> 6;
    103     frame->frequency = freq_values[frame->freqIndex];
    104 
    105     frame->blocks = (d1 & (BIT5 | BIT4)) >> 4;
    106     frame->nrof_blocks = block_values[frame->blocks];
    107 
    108     frame->mode = (d1 & (BIT3 | BIT2)) >> 2;
    109     frame->nrof_channels = channel_values[frame->mode];
    110 
    111     frame->alloc = (d1 & BIT1) >> 1;
    112 
    113     frame->subbands = (d1 & BIT0);
    114     frame->nrof_subbands = band_values[frame->subbands];
    115 
    116     frame->cachedInfo = d1;
    117   }
    118   /*
    119    * For decode, the bit allocator needs to know the bitpool value
    120    */
    121   frame->bitpool = data[2];
    122   frame->crc = data[3];
    123 }
    124 
    125 #define LOW(x) ((x)&0xf)
    126 #define HIGH(x) ((x) >> 4)
    127 
    128 /*
    129  * Read scalefactor values and prepare the bitstream for OI_SBC_ReadSamples
    130  */
    131 PRIVATE void OI_SBC_ReadScalefactors(OI_CODEC_SBC_COMMON_CONTEXT* common,
    132                                      const OI_BYTE* b, OI_BITSTREAM* bs) {
    133   OI_UINT i = common->frameInfo.nrof_subbands * common->frameInfo.nrof_channels;
    134   int8_t* scale_factor = common->scale_factor;
    135   OI_UINT f;
    136 
    137   if (common->frameInfo.nrof_subbands == 8 ||
    138       common->frameInfo.mode != SBC_JOINT_STEREO) {
    139     if (common->frameInfo.mode == SBC_JOINT_STEREO) {
    140       common->frameInfo.join = *b++;
    141     } else {
    142       common->frameInfo.join = 0;
    143     }
    144     i /= 2;
    145     do {
    146       *scale_factor++ = HIGH(f = *b++);
    147       *scale_factor++ = LOW(f);
    148     } while (--i);
    149     /*
    150      * In this case we know that the scale factors end on a byte boundary so all
    151      * we need to do
    152      * is initialize the bitstream.
    153      */
    154     OI_BITSTREAM_ReadInit(bs, b);
    155   } else {
    156     OI_ASSERT(common->frameInfo.nrof_subbands == 4 &&
    157               common->frameInfo.mode == SBC_JOINT_STEREO);
    158     common->frameInfo.join = HIGH(f = *b++);
    159     i = (i - 1) / 2;
    160     do {
    161       *scale_factor++ = LOW(f);
    162       *scale_factor++ = HIGH(f = *b++);
    163     } while (--i);
    164     *scale_factor++ = LOW(f);
    165     /*
    166      * In 4-subband joint stereo mode, the joint stereo information ends on a
    167      * half-byte
    168      * boundary, so it's necessary to use the bitstream abstraction to read it,
    169      * since
    170      * OI_SBC_ReadSamples will need to pick up in mid-byte.
    171      */
    172     OI_BITSTREAM_ReadInit(bs, b);
    173     *scale_factor++ = OI_BITSTREAM_ReadUINT4Aligned(bs);
    174   }
    175 }
    176 
    177 /** Read quantized subband samples from the input bitstream and expand them. */
    178 PRIVATE void OI_SBC_ReadSamples(OI_CODEC_SBC_DECODER_CONTEXT* context,
    179                                 OI_BITSTREAM* global_bs) {
    180   OI_CODEC_SBC_COMMON_CONTEXT* common = &context->common;
    181   OI_UINT nrof_blocks = common->frameInfo.nrof_blocks;
    182   int32_t* RESTRICT s = common->subdata;
    183   uint8_t* ptr = global_bs->ptr.w;
    184   uint32_t value = global_bs->value;
    185   OI_UINT bitPtr = global_bs->bitPtr;
    186 
    187   const OI_UINT iter_count =
    188       common->frameInfo.nrof_channels * common->frameInfo.nrof_subbands / 4;
    189   do {
    190     OI_UINT i;
    191     for (i = 0; i < iter_count; ++i) {
    192       uint32_t sf_by4 = ((uint32_t*)common->scale_factor)[i];
    193       uint32_t bits_by4 = common->bits.uint32[i];
    194       OI_UINT n;
    195       for (n = 0; n < 4; ++n) {
    196         int32_t dequant;
    197         OI_UINT bits;
    198         OI_INT sf;
    199 
    200         if (OI_CPU_BYTE_ORDER == OI_LITTLE_ENDIAN_BYTE_ORDER) {
    201           bits = bits_by4 & 0xFF;
    202           bits_by4 >>= 8;
    203           sf = sf_by4 & 0xFF;
    204           sf_by4 >>= 8;
    205         } else {
    206           bits = (bits_by4 >> 24) & 0xFF;
    207           bits_by4 <<= 8;
    208           sf = (sf_by4 >> 24) & 0xFF;
    209           sf_by4 <<= 8;
    210         }
    211         if (bits) {
    212           uint32_t raw;
    213           OI_BITSTREAM_READUINT(raw, bits, ptr, value, bitPtr);
    214           dequant = OI_SBC_Dequant(raw, sf, bits);
    215         } else {
    216           dequant = 0;
    217         }
    218         *s++ = dequant;
    219       }
    220     }
    221   } while (--nrof_blocks);
    222 }
    223 
    224 /**
    225 @}
    226 */
    227