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