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 #ifndef _OI_CODEC_SBC_PRIVATE_H
20 #define _OI_CODEC_SBC_PRIVATE_H
21 
22 /**********************************************************************************
23   $Revision: #1 $
24 ***********************************************************************************/
25 
26 /**
27 @file
28 Function prototypes and macro definitions used internally by the codec.
29 
30 @ingroup codec_internal
31 */
32 
33 /**
34 @addtogroup codec_internal
35 @{
36 */
37 
38 #ifdef USE_RESTRICT_KEYWORD
39 #define RESTRICT restrict
40 #else
41 #define RESTRICT
42 #endif
43 
44 #ifdef CODEC_DEBUG
45 #include <stdio.h>
46 #define ERROR(x) do { printf x; printf("\n"); } while (0)
47 #else
48 #define ERROR(x)
49 #endif
50 
51 #ifdef TRACE_EXECUTION
52 #define TRACE(x) do { printf x; printf("\n"); } while (0)
53 #else
54 #define TRACE(x)
55 #endif
56 
57 #ifndef PRIVATE
58 #define PRIVATE
59 #endif
60 
61 #ifndef INLINE
62 #define INLINE
63 #endif
64 
65 #include "oi_assert.h"
66 #include "oi_codec_sbc.h"
67 
68 #ifndef OI_SBC_SYNCWORD
69 #define OI_SBC_SYNCWORD 0x9c
70 #endif
71 
72 #ifndef DIVIDE
73 #define DIVIDE(a, b) ((a) / (b))
74 #endif
75 
76 typedef union {
77     OI_UINT8 uint8[SBC_MAX_BANDS];
78     OI_UINT32 uint32[SBC_MAX_BANDS / 4];
79 } BITNEED_UNION1;
80 
81 typedef union {
82     OI_UINT8 uint8[2 * SBC_MAX_BANDS];
83     OI_UINT32 uint32[2 * SBC_MAX_BANDS / 4];
84 } BITNEED_UNION2;
85 
86 static const OI_UINT16 freq_values[] =    { 16000, 32000, 44100, 48000 };
87 static const OI_UINT8 block_values[] =    { 4, 8, 12, 16 };
88 static const OI_UINT8 channel_values[] =  { 1, 2, 2, 2 };
89 static const OI_UINT8 band_values[] =     { 4, 8 };
90 
91 
92 #define TEST_MODE_SENTINEL "OINA"
93 #define TEST_MODE_SENTINEL_LENGTH 4
94 
95 /** Used internally. */
96 typedef struct {
97     union {
98         const OI_UINT8 *r;
99         OI_UINT8 *w;
100     } ptr;
101     OI_UINT32 value;
102     OI_UINT bitPtr;
103 } OI_BITSTREAM;
104 
105 
106 #define VALID_INT16(x) (((x) >= OI_INT16_MIN) && ((x) <= OI_INT16_MAX))
107 #define VALID_INT32(x) (((x) >= OI_INT32_MIN) && ((x) <= OI_INT32_MAX))
108 
109 #define DCTII_8_SHIFT_IN 0
110 #define DCTII_8_SHIFT_OUT 16-DCTII_8_SHIFT_IN
111 
112 #define DCTII_8_SHIFT_0 (DCTII_8_SHIFT_OUT)
113 #define DCTII_8_SHIFT_1 (DCTII_8_SHIFT_OUT)
114 #define DCTII_8_SHIFT_2 (DCTII_8_SHIFT_OUT)
115 #define DCTII_8_SHIFT_3 (DCTII_8_SHIFT_OUT)
116 #define DCTII_8_SHIFT_4 (DCTII_8_SHIFT_OUT)
117 #define DCTII_8_SHIFT_5 (DCTII_8_SHIFT_OUT)
118 #define DCTII_8_SHIFT_6 (DCTII_8_SHIFT_OUT-1)
119 #define DCTII_8_SHIFT_7 (DCTII_8_SHIFT_OUT-2)
120 
121 #define DCT_SHIFT 15
122 
123 #define DCTIII_4_SHIFT_IN 2
124 #define DCTIII_4_SHIFT_OUT 15
125 
126 #define DCTIII_8_SHIFT_IN 3
127 #define DCTIII_8_SHIFT_OUT 14
128 
129 OI_UINT computeBitneed(OI_CODEC_SBC_COMMON_CONTEXT *common,
130                        OI_UINT8 *bitneeds,
131                        OI_UINT ch,
132                        OI_UINT *preferredBitpool);
133 
134 void oneChannelBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT *common,
135                              BITNEED_UNION1 *bitneeds,
136                              OI_UINT ch,
137                              OI_UINT bitcount);
138 
139 
140 OI_INT adjustToFitBitpool(const OI_UINT bitpool,
141                           OI_UINT32 *bitneeds,
142                           const OI_UINT subbands,
143                           OI_UINT bitcount,
144                           OI_UINT *excess);
145 
146 OI_INT allocAdjustedBits(OI_UINT8 *dest,
147                                 OI_INT bits,
148                                 OI_INT excess);
149 
150 OI_INT allocExcessBits(OI_UINT8 *dest,
151                               OI_INT excess);
152 
153 PRIVATE OI_UINT32 internal_CalculateBitrate(OI_CODEC_SBC_FRAME_INFO *frame);
154 
155 PRIVATE OI_UINT16 internal_CalculateFramelen(OI_CODEC_SBC_FRAME_INFO *frame);
156 
157 void monoBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT *common);
158 
159 typedef void (*BIT_ALLOC)(OI_CODEC_SBC_COMMON_CONTEXT *common);
160 
161 PRIVATE OI_STATUS internal_DecodeRaw(OI_CODEC_SBC_DECODER_CONTEXT *context,
162                                      OI_UINT8 bitpool,
163                                      const OI_BYTE **frameData,
164                                      OI_UINT32 *frameBytes,
165                                      OI_INT16 *pcmData,
166                                      OI_UINT32 *pcmBytes);
167 
168 OI_STATUS internal_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT *context,
169                                        OI_UINT32 *decoderData,
170                                        OI_UINT32 decoderDataBytes,
171                                        OI_BYTE maxChannels,
172                                        OI_BYTE pcmStride,
173                                        OI_BOOL enhanced,
174                                        OI_BOOL msbc_enable);
175 
176 OI_UINT16 OI_SBC_CalculateFrameAndHeaderlen(OI_CODEC_SBC_FRAME_INFO *frame, OI_UINT *headerLen_);
177 
178 PRIVATE OI_UINT32 OI_SBC_MaxBitpool(OI_CODEC_SBC_FRAME_INFO *frame);
179 
180 PRIVATE void OI_SBC_ComputeBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT *frame);
181 PRIVATE OI_UINT8 OI_SBC_CalculateChecksum(OI_CODEC_SBC_FRAME_INFO *frame, OI_BYTE const *data);
182 
183 /* Transform functions */
184 PRIVATE void shift_buffer(SBC_BUFFER_T *dest, SBC_BUFFER_T *src, OI_UINT wordCount);
185 PRIVATE void cosineModulateSynth4(SBC_BUFFER_T *RESTRICT out, OI_INT32 const *RESTRICT in);
186 PRIVATE void SynthWindow40_int32_int32_symmetry_with_sum(OI_INT16 *pcm, SBC_BUFFER_T buffer[80], OI_UINT strideShift);
187 
188 void dct3_4(OI_INT32 *RESTRICT out, OI_INT32 const *RESTRICT in);
189 PRIVATE void analyze4_generated(SBC_BUFFER_T analysisBuffer[RESTRICT 40],
190                                 OI_INT16 *pcm,
191                                 OI_UINT strideShift,
192                                 OI_INT32 subband[4]);
193 
194 void dct3_8(OI_INT32 *RESTRICT out, OI_INT32 const *RESTRICT in);
195 
196 PRIVATE void analyze8_generated(SBC_BUFFER_T analysisBuffer[RESTRICT 80],
197                                 OI_INT16 *pcm,
198                                 OI_UINT strideShift,
199                                 OI_INT32 subband[8]);
200 
201 #ifdef SBC_ENHANCED
202 PRIVATE void analyze8_enhanced_generated(SBC_BUFFER_T analysisBuffer[RESTRICT 112],
203         OI_INT16 *pcm,
204         OI_UINT strideShift,
205         OI_INT32 subband[8]);
206 #endif
207 
208 /* Decoder functions */
209 
210 void OI_SBC_ReadHeader(OI_CODEC_SBC_COMMON_CONTEXT *common, const OI_BYTE *data);
211 PRIVATE void OI_SBC_ReadScalefactors(OI_CODEC_SBC_COMMON_CONTEXT *common, const OI_BYTE *b, OI_BITSTREAM *bs);
212 PRIVATE void OI_SBC_ReadSamples(OI_CODEC_SBC_DECODER_CONTEXT *common, OI_BITSTREAM *ob);
213 PRIVATE void OI_SBC_ReadSamplesJoint(OI_CODEC_SBC_DECODER_CONTEXT *common, OI_BITSTREAM *global_bs);
214 PRIVATE void OI_SBC_SynthFrame(OI_CODEC_SBC_DECODER_CONTEXT *context, OI_INT16 *pcm, OI_UINT start_block, OI_UINT nrof_blocks);
215 OI_INT32 OI_SBC_Dequant(OI_UINT32 raw, OI_UINT scale_factor, OI_UINT bits);
216 PRIVATE OI_BOOL OI_SBC_ExamineCommandPacket(OI_CODEC_SBC_DECODER_CONTEXT *context, const OI_BYTE *data, OI_UINT32 len);
217 PRIVATE void OI_SBC_GenerateTestSignal(OI_INT16 pcmData[][2], OI_UINT32 sampleCount);
218 
219 PRIVATE void OI_SBC_ExpandFrameFields(OI_CODEC_SBC_FRAME_INFO *frame);
220 PRIVATE OI_STATUS OI_CODEC_SBC_Alloc(OI_CODEC_SBC_COMMON_CONTEXT *common,
221                                      OI_UINT32 *codecDataAligned,
222                                      OI_UINT32 codecDataBytes,
223                                      OI_UINT8 maxChannels,
224                                      OI_UINT8 pcmStride);
225 /**
226 @}
227 */
228 
229 #endif /* _OI_CODEC_SBC_PRIVATE_H */
230