xref: /btstack/3rd-party/bluedroid/decoder/srce/decoder-private.c (revision 435e3c4e0d833cf29306bcbe25c84a0f767a8539)
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 "oi_codec_sbc_private.h"
37 #include "oi_bitstream.h"
38 #include <stdio.h>
39 
40 OI_CHAR * const OI_Codec_Copyright = "Copyright 2002-2007 Open Interface North America, Inc. All rights reserved";
41 
42 INLINE OI_STATUS internal_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT *context,
43                                        OI_UINT32 *decoderData,
44                                        OI_UINT32 decoderDataBytes,
45                                        OI_BYTE maxChannels,
46                                        OI_BYTE pcmStride,
47                                        OI_BOOL enhanced)
48 {
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, maxChannels, pcmStride);
66 
67     if (!OI_SUCCESS(status)) {
68         return status;
69     }
70 
71     context->common.codecInfo = OI_Codec_Copyright;
72     context->common.maxBitneed = 0;
73     context->limitFrameFormat = FALSE;
74     OI_SBC_ExpandFrameFields(&context->common.frameInfo);
75 
76     /*PLATFORM_DECODER_RESET(context);*/
77 
78     return OI_OK;
79 }
80 
81 
82 
83 
84 /**
85  * Read the SBC header up to but not including the joint stereo mask.  The syncword has already been
86  * examined, and the enhanced mode flag set, by FindSyncword.
87  */
88 INLINE void OI_SBC_ReadHeader(OI_CODEC_SBC_COMMON_CONTEXT *common, const OI_BYTE *data)
89 {
90     OI_CODEC_SBC_FRAME_INFO *frame = &common->frameInfo;
91     OI_UINT8 d1;
92 
93     /* BK4BTSTACK_CHANGE START */
94     if (common->mSBCEnabled){
95         OI_ASSERT(data[0] == OI_mSBC_SYNCWORD);
96     } else {
97         OI_ASSERT(data[0] == OI_SBC_SYNCWORD || data[0] == OI_SBC_ENHANCED_SYNCWORD);
98     }
99     /* BK4BTSTACK_CHANGE END */
100 
101     /* Avoid filling out all these strucutures if we already remember the values
102      * from last time. Just in case we get a stream corresponding to data[1] ==
103      * 0, DecoderReset is responsible for ensuring the lookup table entries have
104      * already been populated
105      */
106     d1 = data[1];
107     if (d1 != frame->cachedInfo) {
108 
109         frame->freqIndex = (d1 & (BIT7 | BIT6)) >> 6;
110         frame->frequency = freq_values[frame->freqIndex];
111 
112         frame->blocks = (d1 & (BIT5 | BIT4)) >> 4;
113 
114         /* BK4BTSTACK_CHANGE START */
115         if (common->mSBCEnabled){
116             frame->nrof_blocks = 15;
117         } else {
118             frame->nrof_blocks = block_values[frame->blocks];
119         }
120         /* BK4BTSTACK_CHANGE END */
121 
122         frame->mode = (d1 & (BIT3 | BIT2)) >> 2;
123         frame->nrof_channels = channel_values[frame->mode];
124 
125         frame->alloc = (d1 & BIT1) >> 1;
126 
127         frame->subbands = (d1 & BIT0);
128         frame->nrof_subbands = band_values[frame->subbands];
129 
130         frame->cachedInfo = d1;
131     }
132     /*
133      * For decode, the bit allocator needs to know the bitpool value
134      */
135     frame->bitpool = data[2];
136     frame->crc = data[3];
137 }
138 
139 
140 #define LOW(x)  ((x)& 0xf)
141 #define HIGH(x) ((x) >> 4)
142 
143 /*
144  * Read scalefactor values and prepare the bitstream for OI_SBC_ReadSamples
145  */
146 PRIVATE void OI_SBC_ReadScalefactors(OI_CODEC_SBC_COMMON_CONTEXT *common,
147                              const OI_BYTE *b,
148                              OI_BITSTREAM *bs)
149 {
150     OI_UINT i = common->frameInfo.nrof_subbands * common->frameInfo.nrof_channels;
151     OI_INT8 *scale_factor = common->scale_factor;
152     OI_UINT f;
153 
154     if (common->frameInfo.nrof_subbands == 8 || common->frameInfo.mode != SBC_JOINT_STEREO) {
155         if (common->frameInfo.mode == SBC_JOINT_STEREO) {
156             common->frameInfo.join = *b++;
157         } else {
158             common->frameInfo.join = 0;
159         }
160         i /= 2;
161         do {
162             *scale_factor++ = HIGH(f = *b++);
163             *scale_factor++ = LOW(f);
164         } while (--i);
165         /*
166          * In this case we know that the scale factors end on a byte boundary so all we need to do
167          * is initialize the bitstream.
168          */
169         OI_BITSTREAM_ReadInit(bs, b);
170     } else {
171         OI_ASSERT(common->frameInfo.nrof_subbands == 4 && common->frameInfo.mode == SBC_JOINT_STEREO);
172         common->frameInfo.join = HIGH(f = *b++);
173         i = (i - 1) / 2;
174         do {
175             *scale_factor++ = LOW(f);
176             *scale_factor++ = HIGH(f = *b++);
177         } while (--i);
178         *scale_factor++ = LOW(f);
179         /*
180          * In 4-subband joint stereo mode, the joint stereo information ends on a half-byte
181          * boundary, so it's necessary to use the bitstream abstraction to read it, since
182          * OI_SBC_ReadSamples will need to pick up in mid-byte.
183          */
184         OI_BITSTREAM_ReadInit(bs, b);
185         *scale_factor++ = OI_BITSTREAM_ReadUINT4Aligned(bs);
186     }
187 }
188 
189 /** Read quantized subband samples from the input bitstream and expand them. */
190 PRIVATE void OI_SBC_ReadSamples(OI_CODEC_SBC_DECODER_CONTEXT *context, OI_BITSTREAM *global_bs)
191 {
192     OI_CODEC_SBC_COMMON_CONTEXT *common = &context->common;
193     OI_UINT nrof_blocks = common->frameInfo.nrof_blocks;
194     OI_INT32 * RESTRICT s = common->subdata;
195     OI_UINT8 *ptr = global_bs->ptr.w;
196     OI_UINT32 value = global_bs->value;
197     OI_UINT bitPtr = global_bs->bitPtr;
198 
199     const OI_UINT iter_count = common->frameInfo.nrof_channels * common->frameInfo.nrof_subbands / 4;
200     do {
201         OI_UINT i;
202         for (i = 0; i < iter_count; ++i) {
203             OI_UINT32 sf_by4 = ((OI_UINT32*)common->scale_factor)[i];
204             OI_UINT32 bits_by4 = common->bits.uint32[i];
205             OI_UINT n;
206             for (n = 0; n < 4; ++n) {
207                 OI_INT32 dequant;
208                 OI_UINT bits;
209                 OI_INT sf;
210 
211                 if (OI_CPU_BYTE_ORDER == OI_LITTLE_ENDIAN_BYTE_ORDER) {
212                     bits = bits_by4 & 0xFF;
213                     bits_by4 >>= 8;
214                     sf = sf_by4 & 0xFF;
215                     sf_by4 >>= 8;
216                 } else {
217                     bits = (bits_by4 >> 24) & 0xFF;
218                     bits_by4 <<= 8;
219                     sf = (sf_by4 >> 24) & 0xFF;
220                     sf_by4 <<= 8;
221                 }
222 
223                 if (bits) {
224                     OI_UINT32 raw;
225                     // return raw == audio sample (uint16)
226                     // bits == number of bits to read from stream
227                     // ptr  == position in stream
228                     // value == 32bit value
229                     // bitPtr offset in 32bit value
230                     OI_BITSTREAM_READUINT(raw, bits, ptr, value, bitPtr);
231                     // dequant == sb_sample (int32)
232                     dequant = OI_SBC_Dequant(raw, sf, bits);
233                 } else {
234                     dequant = 0;
235                 }
236                 *s++ = dequant;
237             }
238         }
239     } while (--nrof_blocks);
240 }
241 
242 
243 
244 /**
245 @}
246 */
247