1 /******************************************************************************
2  *
3  *  Copyright 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 "oi_bitstream.h"
38 #include "oi_codec_sbc_private.h"
39 
40 OI_CHAR* const OI_Codec_Copyright =
41         "Copyright 2002-2007 Open Interface North America, Inc. All rights "
42         "reserved";
43 
internal_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT * context,uint32_t * decoderData,uint32_t decoderDataBytes,OI_BYTE maxChannels,OI_BYTE pcmStride,OI_BOOL enhanced)44 INLINE OI_STATUS internal_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT* context, uint32_t* decoderData,
45                                        uint32_t decoderDataBytes, OI_BYTE maxChannels,
46                                        OI_BYTE pcmStride, OI_BOOL enhanced) {
47   OI_UINT i;
48   OI_STATUS status;
49 
50   for (i = 0; i < sizeof(*context); i++) {
51     ((char*)context)[i] = 0;
52   }
53 
54 #ifdef SBC_ENHANCED
55   context->enhancedEnabled = enhanced ? TRUE : FALSE;
56 #else
57   context->enhancedEnabled = FALSE;
58   if (enhanced) {
59     return OI_STATUS_INVALID_PARAMETERS;
60   }
61 #endif
62 
63   status = OI_CODEC_SBC_Alloc(&context->common, decoderData, decoderDataBytes, maxChannels,
64                               pcmStride);
65 
66   if (!OI_SUCCESS(status)) {
67     return status;
68   }
69 
70   context->common.codecInfo = OI_Codec_Copyright;
71   context->common.maxBitneed = 0;
72   context->limitFrameFormat = FALSE;
73   OI_SBC_ExpandFrameFields(&context->common.frameInfo);
74 
75   /*PLATFORM_DECODER_RESET(context);*/
76 
77   return OI_OK;
78 }
79 
80 /**
81  * Read the SBC header up to but not including the joint stereo mask. The
82  * syncword has already been examined, and the enhanced mode flag set, by
83  * FindSyncword.
84  */
OI_SBC_ReadHeader(OI_CODEC_SBC_COMMON_CONTEXT * common,const OI_BYTE * data)85 INLINE void OI_SBC_ReadHeader(OI_CODEC_SBC_COMMON_CONTEXT* common, const OI_BYTE* data) {
86   OI_CODEC_SBC_FRAME_INFO* frame = &common->frameInfo;
87   uint8_t d1;
88 
89   OI_ASSERT(data[0] == OI_SBC_SYNCWORD || data[0] == OI_SBC_ENHANCED_SYNCWORD);
90 
91   /* Avoid filling out all these strucutures if we already remember the values
92    * from last time. Just in case we get a stream corresponding to data[1] ==
93    * 0, DecoderReset is responsible for ensuring the lookup table entries have
94    * already been populated
95    */
96   d1 = data[1];
97   if (d1 != frame->cachedInfo) {
98     frame->freqIndex = (d1 & (BIT7 | BIT6)) >> 6;
99     frame->frequency = freq_values[frame->freqIndex];
100 
101     frame->blocks = (d1 & (BIT5 | BIT4)) >> 4;
102     frame->nrof_blocks = block_values[frame->blocks];
103 
104     frame->mode = (d1 & (BIT3 | BIT2)) >> 2;
105     frame->nrof_channels = channel_values[frame->mode];
106 
107     frame->alloc = (d1 & BIT1) >> 1;
108 
109     frame->subbands = (d1 & BIT0);
110     frame->nrof_subbands = band_values[frame->subbands];
111 
112     frame->cachedInfo = d1;
113   }
114   /*
115    * For decode, the bit allocator needs to know the bitpool value
116    */
117   frame->bitpool = data[2];
118   frame->crc = data[3];
119 }
120 
121 #define LOW(x) ((x) & 0xf)
122 #define HIGH(x) ((x) >> 4)
123 
124 /*
125  * Read scalefactor values and prepare the bitstream for OI_SBC_ReadSamples
126  */
OI_SBC_ReadScalefactors(OI_CODEC_SBC_COMMON_CONTEXT * common,const OI_BYTE * b,OI_BITSTREAM * bs)127 PRIVATE void OI_SBC_ReadScalefactors(OI_CODEC_SBC_COMMON_CONTEXT* common, const OI_BYTE* b,
128                                      OI_BITSTREAM* bs) {
129   OI_UINT i = common->frameInfo.nrof_subbands * common->frameInfo.nrof_channels;
130   int8_t* scale_factor = common->scale_factor;
131   OI_UINT f;
132 
133   if (common->frameInfo.nrof_subbands == 8 || common->frameInfo.mode != SBC_JOINT_STEREO) {
134     if (common->frameInfo.mode == SBC_JOINT_STEREO) {
135       common->frameInfo.join = *b++;
136     } else {
137       common->frameInfo.join = 0;
138     }
139     i /= 2;
140     do {
141       *scale_factor++ = HIGH(f = *b++);
142       *scale_factor++ = LOW(f);
143     } while (--i);
144     /*
145      * In this case we know that the scale factors end on a byte boundary so all
146      * we need to do
147      * is initialize the bitstream.
148      */
149     OI_BITSTREAM_ReadInit(bs, b);
150   } else {
151     OI_ASSERT(common->frameInfo.nrof_subbands == 4 && common->frameInfo.mode == SBC_JOINT_STEREO);
152     common->frameInfo.join = HIGH(f = *b++);
153     i = (i - 1) / 2;
154     do {
155       *scale_factor++ = LOW(f);
156       *scale_factor++ = HIGH(f = *b++);
157     } while (--i);
158     *scale_factor++ = LOW(f);
159     /*
160      * In 4-subband joint stereo mode, the joint stereo information ends on a
161      * half-byte
162      * boundary, so it's necessary to use the bitstream abstraction to read it,
163      * since
164      * OI_SBC_ReadSamples will need to pick up in mid-byte.
165      */
166     OI_BITSTREAM_ReadInit(bs, b);
167     *scale_factor++ = OI_BITSTREAM_ReadUINT4Aligned(bs);
168   }
169 }
170 
171 /** Read quantized subband samples from the input bitstream and expand them. */
OI_SBC_ReadSamples(OI_CODEC_SBC_DECODER_CONTEXT * context,OI_BITSTREAM * global_bs)172 PRIVATE void OI_SBC_ReadSamples(OI_CODEC_SBC_DECODER_CONTEXT* context, OI_BITSTREAM* global_bs) {
173   OI_CODEC_SBC_COMMON_CONTEXT* common = &context->common;
174   OI_UINT nrof_blocks = common->frameInfo.nrof_blocks;
175   int32_t* RESTRICT s = common->subdata;
176   const uint8_t* ptr = global_bs->ptr.r;
177   uint32_t value = global_bs->value;
178   OI_UINT bitPtr = global_bs->bitPtr;
179   const OI_UINT iter_count = common->frameInfo.nrof_channels * common->frameInfo.nrof_subbands;
180   do {
181     OI_UINT n;
182     for (n = 0; n < iter_count; ++n) {
183       int32_t dequant;
184       OI_UINT bits = common->bits.uint8[n];
185       OI_INT sf = common->scale_factor[n];
186       if (bits) {
187         uint32_t raw;
188         OI_BITSTREAM_READUINT(raw, bits, ptr, value, bitPtr);
189         dequant = OI_SBC_Dequant(raw, sf, bits);
190       } else {
191         dequant = 0;
192       }
193       *s++ = dequant;
194     }
195   } while (--nrof_blocks);
196 }
197 
198 /**
199 @}
200 */
201