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 89 /* BK4BTSTACK_CHANGE START */ 90 INLINE void OI_SBC_ReadHeader_mSBC(OI_CODEC_SBC_COMMON_CONTEXT *common, const OI_BYTE *data){ 91 OI_CODEC_SBC_FRAME_INFO *frame = &common->frameInfo; 92 93 OI_ASSERT(data[0] == OI_mSBC_SYNCWORD); 94 95 /* Avoid filling out all these strucutures if we already remember the values 96 * from last time. Just in case we get a stream corresponding to data[1] == 97 * 0, DecoderReset is responsible for ensuring the lookup table entries have 98 * already been populated 99 */ 100 frame->reserved_for_future_use[0] = data[1]; 101 frame->reserved_for_future_use[1] = data[2]; 102 103 frame->freqIndex = 0; 104 frame->frequency = 16000; 105 106 frame->blocks = 4; // ? 107 frame->nrof_blocks = 15; 108 109 frame->mode = 0; 110 frame->nrof_channels = 1; 111 112 frame->alloc = SBC_LOUDNESS; 113 114 frame->subbands = 1; 115 frame->nrof_subbands = 8; 116 117 frame->bitpool = 26; 118 119 frame->crc = data[3]; 120 121 frame->cachedInfo = 0; 122 } 123 124 /* BK4BTSTACK_CHANGE END */ 125 126 127 INLINE void OI_SBC_ReadHeader(OI_CODEC_SBC_COMMON_CONTEXT *common, const OI_BYTE *data) 128 { 129 OI_CODEC_SBC_FRAME_INFO *frame = &common->frameInfo; 130 OI_UINT8 d1; 131 132 OI_ASSERT(data[0] == OI_SBC_SYNCWORD || data[0] == OI_SBC_ENHANCED_SYNCWORD); 133 134 /* Avoid filling out all these strucutures if we already remember the values 135 * from last time. Just in case we get a stream corresponding to data[1] == 136 * 0, DecoderReset is responsible for ensuring the lookup table entries have 137 * already been populated 138 */ 139 d1 = data[1]; 140 if (d1 != frame->cachedInfo) { 141 142 frame->freqIndex = (d1 & (BIT7 | BIT6)) >> 6; 143 frame->frequency = freq_values[frame->freqIndex]; 144 145 frame->blocks = (d1 & (BIT5 | BIT4)) >> 4; 146 frame->nrof_blocks = block_values[frame->blocks]; 147 148 frame->mode = (d1 & (BIT3 | BIT2)) >> 2; 149 frame->nrof_channels = channel_values[frame->mode]; 150 151 frame->alloc = (d1 & BIT1) >> 1; 152 153 frame->subbands = (d1 & BIT0); 154 frame->nrof_subbands = band_values[frame->subbands]; 155 156 frame->cachedInfo = d1; 157 } 158 /* 159 * For decode, the bit allocator needs to know the bitpool value 160 */ 161 frame->bitpool = data[2]; 162 frame->crc = data[3]; 163 } 164 165 166 #define LOW(x) ((x)& 0xf) 167 #define HIGH(x) ((x) >> 4) 168 169 /* 170 * Read scalefactor values and prepare the bitstream for OI_SBC_ReadSamples 171 */ 172 PRIVATE void OI_SBC_ReadScalefactors(OI_CODEC_SBC_COMMON_CONTEXT *common, 173 const OI_BYTE *b, 174 OI_BITSTREAM *bs) 175 { 176 OI_UINT i = common->frameInfo.nrof_subbands * common->frameInfo.nrof_channels; 177 OI_INT8 *scale_factor = common->scale_factor; 178 OI_UINT f; 179 180 if (common->frameInfo.nrof_subbands == 8 || common->frameInfo.mode != SBC_JOINT_STEREO) { 181 if (common->frameInfo.mode == SBC_JOINT_STEREO) { 182 common->frameInfo.join = *b++; 183 } else { 184 common->frameInfo.join = 0; 185 } 186 i /= 2; 187 do { 188 *scale_factor++ = HIGH(f = *b++); 189 *scale_factor++ = LOW(f); 190 } while (--i); 191 /* 192 * In this case we know that the scale factors end on a byte boundary so all we need to do 193 * is initialize the bitstream. 194 */ 195 OI_BITSTREAM_ReadInit(bs, b); 196 } else { 197 OI_ASSERT(common->frameInfo.nrof_subbands == 4 && common->frameInfo.mode == SBC_JOINT_STEREO); 198 common->frameInfo.join = HIGH(f = *b++); 199 i = (i - 1) / 2; 200 do { 201 *scale_factor++ = LOW(f); 202 *scale_factor++ = HIGH(f = *b++); 203 } while (--i); 204 *scale_factor++ = LOW(f); 205 /* 206 * In 4-subband joint stereo mode, the joint stereo information ends on a half-byte 207 * boundary, so it's necessary to use the bitstream abstraction to read it, since 208 * OI_SBC_ReadSamples will need to pick up in mid-byte. 209 */ 210 OI_BITSTREAM_ReadInit(bs, b); 211 *scale_factor++ = OI_BITSTREAM_ReadUINT4Aligned(bs); 212 } 213 } 214 215 /** Read quantized subband samples from the input bitstream and expand them. */ 216 PRIVATE void OI_SBC_ReadSamples(OI_CODEC_SBC_DECODER_CONTEXT *context, OI_BITSTREAM *global_bs) 217 { 218 OI_CODEC_SBC_COMMON_CONTEXT *common = &context->common; 219 OI_UINT nrof_blocks = common->frameInfo.nrof_blocks; 220 OI_INT32 * RESTRICT s = common->subdata; 221 OI_UINT8 *ptr = global_bs->ptr.w; 222 OI_UINT32 value = global_bs->value; 223 OI_UINT bitPtr = global_bs->bitPtr; 224 225 const OI_UINT iter_count = common->frameInfo.nrof_channels * common->frameInfo.nrof_subbands / 4; 226 do { 227 OI_UINT i; 228 for (i = 0; i < iter_count; ++i) { 229 OI_UINT32 sf_by4 = ((OI_UINT32*)common->scale_factor)[i]; 230 OI_UINT32 bits_by4 = common->bits.uint32[i]; 231 OI_UINT n; 232 for (n = 0; n < 4; ++n) { 233 OI_INT32 dequant; 234 OI_UINT bits; 235 OI_INT sf; 236 237 if (OI_CPU_BYTE_ORDER == OI_LITTLE_ENDIAN_BYTE_ORDER) { 238 bits = bits_by4 & 0xFF; 239 bits_by4 >>= 8; 240 sf = sf_by4 & 0xFF; 241 sf_by4 >>= 8; 242 } else { 243 bits = (bits_by4 >> 24) & 0xFF; 244 bits_by4 <<= 8; 245 sf = (sf_by4 >> 24) & 0xFF; 246 sf_by4 <<= 8; 247 } 248 249 if (bits) { 250 OI_UINT32 raw; 251 // return raw == audio sample (uint16) 252 // bits == number of bits to read from stream 253 // ptr == position in stream 254 // value == 32bit value 255 // bitPtr offset in 32bit value 256 OI_BITSTREAM_READUINT(raw, bits, ptr, value, bitPtr); 257 // dequant == sb_sample (int32) 258 dequant = OI_SBC_Dequant(raw, sf, bits); 259 } else { 260 dequant = 0; 261 } 262 *s++ = dequant; 263 } 264 } 265 } while (--nrof_blocks); 266 } 267 268 269 270 /** 271 @} 272 */ 273