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