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
internal_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT * context,OI_UINT32 * decoderData,OI_UINT32 decoderDataBytes,OI_BYTE maxChannels,OI_BYTE pcmStride,OI_BOOL enhanced)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 */
OI_SBC_ReadHeader(OI_CODEC_SBC_COMMON_CONTEXT * common,const OI_BYTE * data)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
94 OI_ASSERT(data[0] == OI_SBC_SYNCWORD || data[0] == OI_SBC_ENHANCED_SYNCWORD);
95
96 /* Avoid filling out all these strucutures if we already remember the values
97 * from last time. Just in case we get a stream corresponding to data[1] ==
98 * 0, DecoderReset is responsible for ensuring the lookup table entries have
99 * already been populated
100 */
101 d1 = data[1];
102 if (d1 != frame->cachedInfo) {
103
104 frame->freqIndex = (d1 & (BIT7 | BIT6)) >> 6;
105 frame->frequency = freq_values[frame->freqIndex];
106
107 frame->blocks = (d1 & (BIT5 | BIT4)) >> 4;
108 frame->nrof_blocks = block_values[frame->blocks];
109
110 frame->mode = (d1 & (BIT3 | BIT2)) >> 2;
111 frame->nrof_channels = channel_values[frame->mode];
112
113 frame->alloc = (d1 & BIT1) >> 1;
114
115 frame->subbands = (d1 & BIT0);
116 frame->nrof_subbands = band_values[frame->subbands];
117
118 frame->cachedInfo = d1;
119 }
120 /*
121 * For decode, the bit allocator needs to know the bitpool value
122 */
123 frame->bitpool = data[2];
124 frame->crc = data[3];
125 }
126
127
128 #define LOW(x) ((x)& 0xf)
129 #define HIGH(x) ((x) >> 4)
130
131 /*
132 * Read scalefactor values and prepare the bitstream for OI_SBC_ReadSamples
133 */
OI_SBC_ReadScalefactors(OI_CODEC_SBC_COMMON_CONTEXT * common,const OI_BYTE * b,OI_BITSTREAM * bs)134 PRIVATE void OI_SBC_ReadScalefactors(OI_CODEC_SBC_COMMON_CONTEXT *common,
135 const OI_BYTE *b,
136 OI_BITSTREAM *bs)
137 {
138 OI_UINT i = common->frameInfo.nrof_subbands * common->frameInfo.nrof_channels;
139 OI_INT8 *scale_factor = common->scale_factor;
140 OI_UINT f;
141
142 if (common->frameInfo.nrof_subbands == 8 || common->frameInfo.mode != SBC_JOINT_STEREO) {
143 if (common->frameInfo.mode == SBC_JOINT_STEREO) {
144 common->frameInfo.join = *b++;
145 } else {
146 common->frameInfo.join = 0;
147 }
148 i /= 2;
149 do {
150 *scale_factor++ = HIGH(f = *b++);
151 *scale_factor++ = LOW(f);
152 } while (--i);
153 /*
154 * In this case we know that the scale factors end on a byte boundary so all we need to do
155 * is initialize the bitstream.
156 */
157 OI_BITSTREAM_ReadInit(bs, b);
158 } else {
159 OI_ASSERT(common->frameInfo.nrof_subbands == 4 && common->frameInfo.mode == SBC_JOINT_STEREO);
160 common->frameInfo.join = HIGH(f = *b++);
161 i = (i - 1) / 2;
162 do {
163 *scale_factor++ = LOW(f);
164 *scale_factor++ = HIGH(f = *b++);
165 } while (--i);
166 *scale_factor++ = LOW(f);
167 /*
168 * In 4-subband joint stereo mode, the joint stereo information ends on a half-byte
169 * boundary, so it's necessary to use the bitstream abstraction to read it, 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. */
OI_SBC_ReadSamples(OI_CODEC_SBC_DECODER_CONTEXT * context,OI_BITSTREAM * global_bs)178 PRIVATE void OI_SBC_ReadSamples(OI_CODEC_SBC_DECODER_CONTEXT *context, OI_BITSTREAM *global_bs)
179 {
180 OI_CODEC_SBC_COMMON_CONTEXT *common = &context->common;
181 OI_UINT nrof_blocks = common->frameInfo.nrof_blocks;
182 OI_INT32 * RESTRICT s = common->subdata;
183 OI_UINT8 *ptr = global_bs->ptr.w;
184 OI_UINT32 value = global_bs->value;
185 OI_UINT bitPtr = global_bs->bitPtr;
186
187 const OI_UINT iter_count = common->frameInfo.nrof_channels * common->frameInfo.nrof_subbands / 4;
188 do {
189 OI_UINT i;
190 for (i = 0; i < iter_count; ++i) {
191 OI_UINT32 sf_by4 = ((OI_UINT32*)common->scale_factor)[i];
192 OI_UINT32 bits_by4 = common->bits.uint32[i];
193 OI_UINT n;
194 for (n = 0; n < 4; ++n) {
195 OI_INT32 dequant;
196 OI_UINT bits;
197 OI_INT sf;
198
199 if (OI_CPU_BYTE_ORDER == OI_LITTLE_ENDIAN_BYTE_ORDER) {
200 bits = bits_by4 & 0xFF;
201 bits_by4 >>= 8;
202 sf = sf_by4 & 0xFF;
203 sf_by4 >>= 8;
204 } else {
205 bits = (bits_by4 >> 24) & 0xFF;
206 bits_by4 <<= 8;
207 sf = (sf_by4 >> 24) & 0xFF;
208 sf_by4 <<= 8;
209 }
210 if (bits) {
211 OI_UINT32 raw;
212 OI_BITSTREAM_READUINT(raw, bits, ptr, value, bitPtr);
213 dequant = OI_SBC_Dequant(raw, sf, bits);
214 } else {
215 dequant = 0;
216 }
217 *s++ = dequant;
218 }
219 }
220 } while (--nrof_blocks);
221 }
222
223
224
225 /**
226 @}
227 */
228