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, OI_UINT ch, 136 OI_UINT* preferredBitpool); 137 138 void oneChannelBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT* common, BITNEED_UNION1* bitneeds, 139 OI_UINT ch, OI_UINT bitcount); 140 141 OI_INT adjustToFitBitpool(const OI_UINT bitpool, uint32_t* bitneeds, const OI_UINT subbands, 142 OI_UINT bitcount, OI_UINT* excess); 143 144 INLINE OI_INT allocAdjustedBits(uint8_t* dest, OI_INT bits, OI_INT excess); 145 146 INLINE OI_INT allocExcessBits(uint8_t* dest, OI_INT excess); 147 148 PRIVATE uint32_t internal_CalculateBitrate(OI_CODEC_SBC_FRAME_INFO* frame); 149 150 PRIVATE uint16_t internal_CalculateFramelen(OI_CODEC_SBC_FRAME_INFO* frame); 151 152 void monoBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT* common); 153 154 typedef void (*BIT_ALLOC)(OI_CODEC_SBC_COMMON_CONTEXT* common); 155 156 PRIVATE OI_STATUS internal_DecodeRaw(OI_CODEC_SBC_DECODER_CONTEXT* context, uint8_t bitpool, 157 const OI_BYTE** frameData, uint32_t* frameBytes, 158 int16_t* pcmData, uint32_t* pcmBytes); 159 160 INLINE OI_STATUS internal_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT* context, uint32_t* decoderData, 161 uint32_t decoderDataBytes, OI_BYTE maxChannels, 162 OI_BYTE pcmStride, OI_BOOL enhanced); 163 164 INLINE uint16_t OI_SBC_CalculateFrameAndHeaderlen(OI_CODEC_SBC_FRAME_INFO* frame, 165 OI_UINT* headerLen_); 166 167 PRIVATE uint32_t OI_SBC_MaxBitpool(OI_CODEC_SBC_FRAME_INFO* frame); 168 169 PRIVATE void OI_SBC_ComputeBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT* frame); 170 PRIVATE uint8_t OI_SBC_CalculateChecksum(OI_CODEC_SBC_FRAME_INFO* frame, OI_BYTE const* data); 171 172 /* Transform functions */ 173 PRIVATE void shift_buffer(SBC_BUFFER_T* dest, SBC_BUFFER_T* src, OI_UINT wordCount); 174 PRIVATE void cosineModulateSynth4(SBC_BUFFER_T* RESTRICT out, int32_t const* RESTRICT in); 175 PRIVATE void SynthWindow40_int32_int32_symmetry_with_sum(int16_t* pcm, SBC_BUFFER_T buffer[80], 176 OI_UINT strideShift); 177 178 INLINE void dct3_4(int32_t* RESTRICT out, int32_t const* RESTRICT in); 179 PRIVATE void analyze4_generated(SBC_BUFFER_T analysisBuffer[RESTRICT 40], int16_t* pcm, 180 OI_UINT strideShift, int32_t subband[4]); 181 182 INLINE void dct3_8(int32_t* RESTRICT out, int32_t const* RESTRICT in); 183 184 PRIVATE void analyze8_generated(SBC_BUFFER_T analysisBuffer[RESTRICT 80], int16_t* pcm, 185 OI_UINT strideShift, int32_t subband[8]); 186 187 #ifdef SBC_ENHANCED 188 PRIVATE void analyze8_enhanced_generated(SBC_BUFFER_T analysisBuffer[RESTRICT 112], int16_t* pcm, 189 OI_UINT strideShift, int32_t subband[8]); 190 #endif 191 192 /* Decoder functions */ 193 194 INLINE void OI_SBC_ReadHeader(OI_CODEC_SBC_COMMON_CONTEXT* common, const OI_BYTE* data); 195 PRIVATE void OI_SBC_ReadScalefactors(OI_CODEC_SBC_COMMON_CONTEXT* common, const OI_BYTE* b, 196 OI_BITSTREAM* bs); 197 PRIVATE void OI_SBC_ReadSamples(OI_CODEC_SBC_DECODER_CONTEXT* common, OI_BITSTREAM* ob); 198 PRIVATE void OI_SBC_ReadSamplesJoint(OI_CODEC_SBC_DECODER_CONTEXT* common, OI_BITSTREAM* global_bs); 199 PRIVATE void OI_SBC_SynthFrame(OI_CODEC_SBC_DECODER_CONTEXT* context, int16_t* pcm, 200 OI_UINT start_block, OI_UINT nrof_blocks); 201 INLINE int32_t OI_SBC_Dequant(uint32_t raw, OI_UINT scale_factor, OI_UINT bits); 202 PRIVATE OI_BOOL OI_SBC_ExamineCommandPacket(OI_CODEC_SBC_DECODER_CONTEXT* context, 203 const OI_BYTE* data, uint32_t len); 204 PRIVATE void OI_SBC_GenerateTestSignal(int16_t pcmData[][2], uint32_t sampleCount); 205 206 PRIVATE void OI_SBC_ExpandFrameFields(OI_CODEC_SBC_FRAME_INFO* frame); 207 PRIVATE OI_STATUS OI_CODEC_SBC_Alloc(OI_CODEC_SBC_COMMON_CONTEXT* common, 208 uint32_t* codecDataAligned, uint32_t codecDataBytes, 209 uint8_t maxChannels, uint8_t pcmStride); 210 /** 211 @} 212 */ 213 214 #endif /* _OI_CODEC_SBC_PRIVATE_H */ 215