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