• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdio.h>
38 #include "oi_bitstream.h"
39 #include "oi_codec_sbc_private.h"
40 
41 OI_CHAR* const OI_Codec_Copyright =
42     "Copyright 2002-2007 Open Interface North America, Inc. All rights "
43     "reserved";
44 
internal_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT * context,uint32_t * decoderData,uint32_t decoderDataBytes,OI_BYTE maxChannels,OI_BYTE pcmStride,OI_BOOL enhanced)45 INLINE OI_STATUS internal_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT* context,
46                                        uint32_t* decoderData,
47                                        uint32_t decoderDataBytes,
48                                        OI_BYTE maxChannels, OI_BYTE pcmStride,
49                                        OI_BOOL enhanced) {
50   OI_UINT i;
51   OI_STATUS status;
52 
53   for (i = 0; i < sizeof(*context); i++) {
54     ((char*)context)[i] = 0;
55   }
56 
57 #ifdef SBC_ENHANCED
58   context->enhancedEnabled = enhanced ? TRUE : FALSE;
59 #else
60   context->enhancedEnabled = FALSE;
61   if (enhanced) {
62     return OI_STATUS_INVALID_PARAMETERS;
63   }
64 #endif
65 
66   status = OI_CODEC_SBC_Alloc(&context->common, decoderData, decoderDataBytes,
67                               maxChannels, pcmStride);
68 
69   if (!OI_SUCCESS(status)) {
70     return status;
71   }
72 
73   context->common.codecInfo = OI_Codec_Copyright;
74   context->common.maxBitneed = 0;
75   context->limitFrameFormat = FALSE;
76   OI_SBC_ExpandFrameFields(&context->common.frameInfo);
77 
78   /*PLATFORM_DECODER_RESET(context);*/
79 
80   return OI_OK;
81 }
82 
83 /**
84  * Read the SBC header up to but not including the joint stereo mask. The
85  * syncword has already been examined, and the enhanced mode flag set, by
86  * 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,
89                               const OI_BYTE* data) {
90   OI_CODEC_SBC_FRAME_INFO* frame = &common->frameInfo;
91   uint8_t d1;
92 
93   OI_ASSERT(data[0] == OI_SBC_SYNCWORD || data[0] == OI_SBC_ENHANCED_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   d1 = data[1];
101   if (d1 != frame->cachedInfo) {
102     frame->freqIndex = (d1 & (BIT7 | BIT6)) >> 6;
103     frame->frequency = freq_values[frame->freqIndex];
104 
105     frame->blocks = (d1 & (BIT5 | BIT4)) >> 4;
106     frame->nrof_blocks = block_values[frame->blocks];
107 
108     frame->mode = (d1 & (BIT3 | BIT2)) >> 2;
109     frame->nrof_channels = channel_values[frame->mode];
110 
111     frame->alloc = (d1 & BIT1) >> 1;
112 
113     frame->subbands = (d1 & BIT0);
114     frame->nrof_subbands = band_values[frame->subbands];
115 
116     frame->cachedInfo = d1;
117   }
118   /*
119    * For decode, the bit allocator needs to know the bitpool value
120    */
121   frame->bitpool = data[2];
122   frame->crc = data[3];
123 }
124 
125 #define LOW(x) ((x)&0xf)
126 #define HIGH(x) ((x) >> 4)
127 
128 /*
129  * Read scalefactor values and prepare the bitstream for OI_SBC_ReadSamples
130  */
OI_SBC_ReadScalefactors(OI_CODEC_SBC_COMMON_CONTEXT * common,const OI_BYTE * b,OI_BITSTREAM * bs)131 PRIVATE void OI_SBC_ReadScalefactors(OI_CODEC_SBC_COMMON_CONTEXT* common,
132                                      const OI_BYTE* b, OI_BITSTREAM* bs) {
133   OI_UINT i = common->frameInfo.nrof_subbands * common->frameInfo.nrof_channels;
134   int8_t* scale_factor = common->scale_factor;
135   OI_UINT f;
136 
137   if (common->frameInfo.nrof_subbands == 8 ||
138       common->frameInfo.mode != SBC_JOINT_STEREO) {
139     if (common->frameInfo.mode == SBC_JOINT_STEREO) {
140       common->frameInfo.join = *b++;
141     } else {
142       common->frameInfo.join = 0;
143     }
144     i /= 2;
145     do {
146       *scale_factor++ = HIGH(f = *b++);
147       *scale_factor++ = LOW(f);
148     } while (--i);
149     /*
150      * In this case we know that the scale factors end on a byte boundary so all
151      * we need to do
152      * is initialize the bitstream.
153      */
154     OI_BITSTREAM_ReadInit(bs, b);
155   } else {
156     OI_ASSERT(common->frameInfo.nrof_subbands == 4 &&
157               common->frameInfo.mode == SBC_JOINT_STEREO);
158     common->frameInfo.join = HIGH(f = *b++);
159     i = (i - 1) / 2;
160     do {
161       *scale_factor++ = LOW(f);
162       *scale_factor++ = HIGH(f = *b++);
163     } while (--i);
164     *scale_factor++ = LOW(f);
165     /*
166      * In 4-subband joint stereo mode, the joint stereo information ends on a
167      * half-byte
168      * boundary, so it's necessary to use the bitstream abstraction to read it,
169      * 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,
179                                 OI_BITSTREAM* global_bs) {
180   OI_CODEC_SBC_COMMON_CONTEXT* common = &context->common;
181   OI_UINT nrof_blocks = common->frameInfo.nrof_blocks;
182   int32_t* RESTRICT s = common->subdata;
183   const uint8_t* ptr = global_bs->ptr.r;
184   uint32_t value = global_bs->value;
185   OI_UINT bitPtr = global_bs->bitPtr;
186   const OI_UINT iter_count =
187       common->frameInfo.nrof_channels * common->frameInfo.nrof_subbands;
188   do {
189     OI_UINT n;
190     for (n = 0; n < iter_count; ++n) {
191       int32_t dequant;
192       OI_UINT bits = common->bits.uint8[n];
193       OI_INT sf = common->scale_factor[n];
194       if (bits) {
195         uint32_t raw;
196         OI_BITSTREAM_READUINT(raw, bits, ptr, value, bitPtr);
197         dequant = OI_SBC_Dequant(raw, sf, bits);
198       } else {
199         dequant = 0;
200       }
201       *s++ = dequant;
202     }
203   } while (--nrof_blocks);
204 }
205 
206 /**
207 @}
208 */
209