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