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 #include <stdio.h> 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 /* BK4BTSTACK_CHANGE START */ 70 #ifndef OI_mSBC_SYNCWORD 71 #define OI_mSBC_SYNCWORD 0xad 72 #endif 73 /* BK4BTSTACK_CHANGE END */ 74 75 #ifndef OI_SBC_SYNCWORD 76 #define OI_SBC_SYNCWORD 0x9c 77 #endif 78 79 #ifndef DIVIDE 80 #define DIVIDE(a, b) ((a) / (b)) 81 #endif 82 83 typedef union { 84 OI_UINT8 uint8[SBC_MAX_BANDS]; 85 OI_UINT32 uint32[SBC_MAX_BANDS / 4]; 86 } BITNEED_UNION1; 87 88 typedef union { 89 OI_UINT8 uint8[2 * SBC_MAX_BANDS]; 90 OI_UINT32 uint32[2 * SBC_MAX_BANDS / 4]; 91 } BITNEED_UNION2; 92 93 static const OI_UINT16 freq_values[] = { 16000, 32000, 44100, 48000 }; 94 static const OI_UINT8 block_values[] = { 4, 8, 12, 16 }; 95 static const OI_UINT8 channel_values[] = { 1, 2, 2, 2 }; 96 static const OI_UINT8 band_values[] = { 4, 8 }; 97 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 OI_UINT8 *r; 106 OI_UINT8 *w; 107 } ptr; 108 OI_UINT32 value; 109 OI_UINT bitPtr; 110 } OI_BITSTREAM; 111 112 113 #define VALID_INT16(x) (((x) >= OI_INT16_MIN) && ((x) <= OI_INT16_MAX)) 114 #define VALID_INT32(x) (((x) >= OI_INT32_MIN) && ((x) <= OI_INT32_MAX)) 115 116 #define DCTII_8_SHIFT_IN 0 117 #define DCTII_8_SHIFT_OUT 16-DCTII_8_SHIFT_IN 118 119 #define DCTII_8_SHIFT_0 (DCTII_8_SHIFT_OUT) 120 #define DCTII_8_SHIFT_1 (DCTII_8_SHIFT_OUT) 121 #define DCTII_8_SHIFT_2 (DCTII_8_SHIFT_OUT) 122 #define DCTII_8_SHIFT_3 (DCTII_8_SHIFT_OUT) 123 #define DCTII_8_SHIFT_4 (DCTII_8_SHIFT_OUT) 124 #define DCTII_8_SHIFT_5 (DCTII_8_SHIFT_OUT) 125 #define DCTII_8_SHIFT_6 (DCTII_8_SHIFT_OUT-1) 126 #define DCTII_8_SHIFT_7 (DCTII_8_SHIFT_OUT-2) 127 128 #define DCT_SHIFT 15 129 130 #define DCTIII_4_SHIFT_IN 2 131 #define DCTIII_4_SHIFT_OUT 15 132 133 #define DCTIII_8_SHIFT_IN 3 134 #define DCTIII_8_SHIFT_OUT 14 135 136 OI_UINT computeBitneed(OI_CODEC_SBC_COMMON_CONTEXT *common, 137 OI_UINT8 *bitneeds, 138 OI_UINT ch, 139 OI_UINT *preferredBitpool); 140 141 void oneChannelBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT *common, 142 BITNEED_UNION1 *bitneeds, 143 OI_UINT ch, 144 OI_UINT bitcount); 145 146 147 OI_INT adjustToFitBitpool(const OI_UINT bitpool, 148 OI_UINT32 *bitneeds, 149 const OI_UINT subbands, 150 OI_UINT bitcount, 151 OI_UINT *excess); 152 153 INLINE OI_INT allocAdjustedBits(OI_UINT8 *dest, 154 OI_INT bits, 155 OI_INT excess); 156 157 INLINE OI_INT allocExcessBits(OI_UINT8 *dest, 158 OI_INT excess); 159 160 PRIVATE OI_UINT32 internal_CalculateBitrate(OI_CODEC_SBC_FRAME_INFO *frame); 161 162 PRIVATE OI_UINT16 internal_CalculateFramelen(OI_CODEC_SBC_FRAME_INFO *frame); 163 164 void monoBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT *common); 165 166 typedef void (*BIT_ALLOC)(OI_CODEC_SBC_COMMON_CONTEXT *common); 167 168 PRIVATE OI_STATUS internal_DecodeRaw(OI_CODEC_SBC_DECODER_CONTEXT *context, 169 OI_UINT8 bitpool, 170 const OI_BYTE **frameData, 171 OI_UINT32 *frameBytes, 172 OI_INT16 *pcmData, 173 OI_UINT32 *pcmBytes); 174 175 INLINE OI_STATUS internal_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT *context, 176 OI_UINT32 *decoderData, 177 OI_UINT32 decoderDataBytes, 178 OI_BYTE maxChannels, 179 OI_BYTE pcmStride, 180 OI_BOOL enhanced); 181 182 INLINE OI_UINT16 OI_SBC_CalculateFrameAndHeaderlen(OI_CODEC_SBC_FRAME_INFO *frame, OI_UINT *headerLen_); 183 184 PRIVATE OI_UINT32 OI_SBC_MaxBitpool(OI_CODEC_SBC_FRAME_INFO *frame); 185 186 PRIVATE void OI_SBC_ComputeBitAllocation(OI_CODEC_SBC_COMMON_CONTEXT *frame); 187 PRIVATE OI_UINT8 OI_SBC_CalculateChecksum(OI_CODEC_SBC_FRAME_INFO *frame, OI_BYTE const *data); 188 PRIVATE OI_UINT8 OI_SBC_CalculateChecksum_mSBC(OI_CODEC_SBC_FRAME_INFO *frame, OI_BYTE const *data); 189 190 /* Transform functions */ 191 PRIVATE void shift_buffer(SBC_BUFFER_T *dest, SBC_BUFFER_T *src, OI_UINT wordCount); 192 PRIVATE void cosineModulateSynth4(SBC_BUFFER_T * RESTRICT out, OI_INT32 const * RESTRICT in); 193 PRIVATE void SynthWindow40_int32_int32_symmetry_with_sum(OI_INT16 *pcm, SBC_BUFFER_T buffer[80], OI_UINT strideShift); 194 195 INLINE void dct3_4(OI_INT32 * RESTRICT out, OI_INT32 const * RESTRICT in); 196 PRIVATE void analyze4_generated(SBC_BUFFER_T analysisBuffer[RESTRICT 40], 197 OI_INT16 *pcm, 198 OI_UINT strideShift, 199 OI_INT32 subband[4]); 200 201 INLINE void dct3_8(OI_INT32 * RESTRICT out, OI_INT32 const * RESTRICT in); 202 203 PRIVATE void analyze8_generated(SBC_BUFFER_T analysisBuffer[RESTRICT 80], 204 OI_INT16 *pcm, 205 OI_UINT strideShift, 206 OI_INT32 subband[8]); 207 208 #ifdef SBC_ENHANCED 209 PRIVATE void analyze8_enhanced_generated(SBC_BUFFER_T analysisBuffer[RESTRICT 112], 210 OI_INT16 *pcm, 211 OI_UINT strideShift, 212 OI_INT32 subband[8]); 213 #endif 214 215 /* Decoder functions */ 216 INLINE void OI_SBC_ReadHeader_mSBC(OI_CODEC_SBC_COMMON_CONTEXT *common, const OI_BYTE *data); 217 INLINE void OI_SBC_ReadHeader(OI_CODEC_SBC_COMMON_CONTEXT *common, const OI_BYTE *data); 218 PRIVATE void OI_SBC_ReadScalefactors(OI_CODEC_SBC_COMMON_CONTEXT *common, const OI_BYTE *b, OI_BITSTREAM *bs); 219 PRIVATE void OI_SBC_ReadSamples(OI_CODEC_SBC_DECODER_CONTEXT *common, OI_BITSTREAM *ob); 220 PRIVATE void OI_SBC_ReadSamplesJoint(OI_CODEC_SBC_DECODER_CONTEXT *common, OI_BITSTREAM *global_bs); 221 PRIVATE void OI_SBC_SynthFrame(OI_CODEC_SBC_DECODER_CONTEXT *context, OI_INT16 *pcm, OI_UINT start_block, OI_UINT nrof_blocks); 222 INLINE OI_INT32 OI_SBC_Dequant(OI_UINT32 raw, OI_UINT scale_factor, OI_UINT bits); 223 PRIVATE OI_BOOL OI_SBC_ExamineCommandPacket(OI_CODEC_SBC_DECODER_CONTEXT *context, const OI_BYTE *data, OI_UINT32 len); 224 PRIVATE void OI_SBC_GenerateTestSignal(OI_INT16 pcmData[][2], OI_UINT32 sampleCount); 225 226 PRIVATE void OI_SBC_ExpandFrameFields(OI_CODEC_SBC_FRAME_INFO *frame); 227 PRIVATE OI_STATUS OI_CODEC_SBC_Alloc(OI_CODEC_SBC_COMMON_CONTEXT *common, 228 OI_UINT32 *codecDataAligned, 229 OI_UINT32 codecDataBytes, 230 OI_UINT8 maxChannels, 231 OI_UINT8 pcmStride); 232 /** 233 @} 234 */ 235 236 #endif /* _OI_CODEC_SBC_PRIVATE_H */ 237 238