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