oi_codec_sbc_private.h revision ee96a3c60fca590d38025925c072d264e06493c4
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
28Function 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
76typedef union {
77    uint8_t uint8[SBC_MAX_BANDS];
78    uint32_t uint32[SBC_MAX_BANDS / 4];
79} BITNEED_UNION1;
80
81typedef union {
82    uint8_t uint8[2 * SBC_MAX_BANDS];
83    uint32_t uint32[2 * SBC_MAX_BANDS / 4];
84} BITNEED_UNION2;
85
86static const uint16_t freq_values[] =    { 16000, 32000, 44100, 48000 };
87static const uint8_t block_values[] =    { 4, 8, 12, 16 };
88static const uint8_t channel_values[] =  { 1, 2, 2, 2 };
89static const uint8_t band_values[] =     { 4, 8 };
90
91
92#define TEST_MODE_SENTINEL "OINA"
93#define TEST_MODE_SENTINEL_LENGTH 4
94
95/** Used internally. */
96typedef struct {
97    union {
98        const uint8_t *r;
99        uint8_t *w;
100    } ptr;
101    uint32_t 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
129OI_UINT computeBitneed(OI_CODEC_SBC_COMMON_CONTEXT *common,
130                              uint8_t *bitneeds,
131                              OI_UINT ch,
132                              OI_UINT *preferredBitpool);
133
134void oneChannelBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT *common,
135                                    BITNEED_UNION1 *bitneeds,
136                                    OI_UINT ch,
137                                    OI_UINT bitcount);
138
139
140OI_INT adjustToFitBitpool(const OI_UINT bitpool,
141                                 uint32_t *bitneeds,
142                                 const OI_UINT subbands,
143                                 OI_UINT bitcount,
144                                 OI_UINT *excess);
145
146INLINE OI_INT allocAdjustedBits(uint8_t *dest,
147                                OI_INT bits,
148                                OI_INT excess);
149
150INLINE OI_INT allocExcessBits(uint8_t *dest,
151                              OI_INT excess);
152
153PRIVATE uint32_t internal_CalculateBitrate(OI_CODEC_SBC_FRAME_INFO *frame);
154
155PRIVATE uint16_t internal_CalculateFramelen(OI_CODEC_SBC_FRAME_INFO *frame);
156
157void monoBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT *common);
158
159typedef void (*BIT_ALLOC)(OI_CODEC_SBC_COMMON_CONTEXT *common);
160
161PRIVATE OI_STATUS internal_DecodeRaw(OI_CODEC_SBC_DECODER_CONTEXT *context,
162                                     uint8_t bitpool,
163                                     const OI_BYTE **frameData,
164                                     uint32_t *frameBytes,
165                                     int16_t *pcmData,
166                                     uint32_t *pcmBytes);
167
168INLINE OI_STATUS internal_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT *context,
169                                       uint32_t *decoderData,
170                                       uint32_t decoderDataBytes,
171                                       OI_BYTE maxChannels,
172                                       OI_BYTE pcmStride,
173                                       OI_BOOL enhanced);
174
175INLINE uint16_t OI_SBC_CalculateFrameAndHeaderlen(OI_CODEC_SBC_FRAME_INFO *frame, OI_UINT *headerLen_);
176
177PRIVATE uint32_t OI_SBC_MaxBitpool(OI_CODEC_SBC_FRAME_INFO *frame);
178
179PRIVATE void OI_SBC_ComputeBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT *frame);
180PRIVATE uint8_t OI_SBC_CalculateChecksum(OI_CODEC_SBC_FRAME_INFO *frame, OI_BYTE const *data);
181
182/* Transform functions */
183PRIVATE void shift_buffer(SBC_BUFFER_T *dest, SBC_BUFFER_T *src, OI_UINT wordCount);
184PRIVATE void cosineModulateSynth4(SBC_BUFFER_T * RESTRICT out, int32_t const * RESTRICT in);
185PRIVATE void SynthWindow40_int32_int32_symmetry_with_sum(int16_t *pcm, SBC_BUFFER_T buffer[80], OI_UINT strideShift);
186
187INLINE void dct3_4(int32_t * RESTRICT out, int32_t const * RESTRICT in);
188PRIVATE void analyze4_generated(SBC_BUFFER_T analysisBuffer[RESTRICT 40],
189                                int16_t *pcm,
190                                OI_UINT strideShift,
191                                int32_t subband[4]);
192
193INLINE void dct3_8(int32_t * RESTRICT out, int32_t const * RESTRICT in);
194
195PRIVATE void analyze8_generated(SBC_BUFFER_T analysisBuffer[RESTRICT 80],
196                                int16_t *pcm,
197                                OI_UINT strideShift,
198                                int32_t subband[8]);
199
200#ifdef SBC_ENHANCED
201PRIVATE void analyze8_enhanced_generated(SBC_BUFFER_T analysisBuffer[RESTRICT 112],
202                                         int16_t *pcm,
203                                         OI_UINT strideShift,
204                                         int32_t subband[8]);
205#endif
206
207/* Decoder functions */
208
209INLINE  void OI_SBC_ReadHeader(OI_CODEC_SBC_COMMON_CONTEXT *common, const OI_BYTE *data);
210PRIVATE void OI_SBC_ReadScalefactors(OI_CODEC_SBC_COMMON_CONTEXT *common, const OI_BYTE *b, OI_BITSTREAM *bs);
211PRIVATE void OI_SBC_ReadSamples(OI_CODEC_SBC_DECODER_CONTEXT *common, OI_BITSTREAM *ob);
212PRIVATE void OI_SBC_ReadSamplesJoint(OI_CODEC_SBC_DECODER_CONTEXT *common, OI_BITSTREAM *global_bs);
213PRIVATE void OI_SBC_SynthFrame(OI_CODEC_SBC_DECODER_CONTEXT *context, int16_t *pcm, OI_UINT start_block, OI_UINT nrof_blocks);
214INLINE int32_t OI_SBC_Dequant(uint32_t raw, OI_UINT scale_factor, OI_UINT bits);
215PRIVATE OI_BOOL OI_SBC_ExamineCommandPacket(OI_CODEC_SBC_DECODER_CONTEXT *context, const OI_BYTE *data, uint32_t len);
216PRIVATE void OI_SBC_GenerateTestSignal(int16_t pcmData[][2], uint32_t sampleCount);
217
218PRIVATE void OI_SBC_ExpandFrameFields(OI_CODEC_SBC_FRAME_INFO *frame);
219PRIVATE OI_STATUS OI_CODEC_SBC_Alloc(OI_CODEC_SBC_COMMON_CONTEXT *common,
220                                     uint32_t *codecDataAligned,
221                                     uint32_t codecDataBytes,
222                                     uint8_t maxChannels,
223                                     uint8_t pcmStride);
224/**
225@}
226*/
227
228#endif /* _OI_CODEC_SBC_PRIVATE_H */
229
230