coder.h
1 /* ***** BEGIN LICENSE BLOCK ***** 2 * Version: RCSL 1.0/RPSL 1.0 3 * 4 * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved. 5 * 6 * The contents of this file, and the files included with this file, are 7 * subject to the current version of the RealNetworks Public Source License 8 * Version 1.0 (the "RPSL") available at 9 * http://www.helixcommunity.org/content/rpsl unless you have licensed 10 * the file under the RealNetworks Community Source License Version 1.0 11 * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl, 12 * in which case the RCSL will apply. You may also obtain the license terms 13 * directly from RealNetworks. You may not use this file except in 14 * compliance with the RPSL or, if you have a valid RCSL with RealNetworks 15 * applicable to this file, the RCSL. Please see the applicable RPSL or 16 * RCSL for the rights, obligations and limitations governing use of the 17 * contents of the file. 18 * 19 * This file is part of the Helix DNA Technology. RealNetworks is the 20 * developer of the Original Code and owns the copyrights in the portions 21 * it created. 22 * 23 * This file, and the files included with this file, is distributed and made 24 * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 25 * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES, 26 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS 27 * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 28 * 29 * Technology Compatibility Kit Test Suite(s) Location: 30 * http://www.helixcommunity.org/content/tck 31 * 32 * Contributor(s): 33 * 34 * ***** END LICENSE BLOCK ***** */ 35 36 /************************************************************************************** 37 * Fixed-point MP3 decoder 38 * Jon Recker (jrecker@real.com), Ken Cooke (kenc@real.com) 39 * June 2003 40 * 41 * coder.h - private, implementation-specific header file 42 **************************************************************************************/ 43 44 #ifndef _CODER_H 45 #define _CODER_H 46 47 #include "mp3common.h" 48 49 #if defined(ASSERT) 50 #undef ASSERT 51 #endif 52 #if defined(_WIN32) && defined(_M_IX86) && (defined (_DEBUG) || defined (REL_ENABLE_ASSERTS)) 53 #define ASSERT(x) if (!(x)) __asm int 3; 54 #else 55 #define ASSERT(x) /* do nothing */ 56 #endif 57 58 #ifndef MAX 59 #define MAX(a,b) ((a) > (b) ? (a) : (b)) 60 #endif 61 62 #ifndef MIN 63 #define MIN(a,b) ((a) < (b) ? (a) : (b)) 64 #endif 65 66 /* clip to range [-2^n, 2^n - 1] */ 67 #define CLIP_2N(y, n) { \ 68 int sign = (y) >> 31; \ 69 if (sign != (y) >> (n)) { \ 70 (y) = sign ^ ((1 << (n)) - 1); \ 71 } \ 72 } 73 74 #define SIBYTES_MPEG1_MONO 17 75 #define SIBYTES_MPEG1_STEREO 32 76 #define SIBYTES_MPEG2_MONO 9 77 #define SIBYTES_MPEG2_STEREO 17 78 79 /* number of fraction bits for pow43Tab (see comments there) */ 80 #define POW43_FRACBITS_LOW 22 81 #define POW43_FRACBITS_HIGH 12 82 83 #define DQ_FRACBITS_OUT 25 /* number of fraction bits in output of dequant */ 84 #define IMDCT_SCALE 2 /* additional scaling (by sqrt(2)) for fast IMDCT36 */ 85 86 #define HUFF_PAIRTABS 32 87 #define BLOCK_SIZE 18 88 #define NBANDS 32 89 #define MAX_REORDER_SAMPS ((192-126)*3) /* largest critical band for short blocks (see sfBandTable) */ 90 #define VBUF_LENGTH (17 * 2 * NBANDS) /* for double-sized vbuf FIFO */ 91 92 /* additional external symbols to name-mangle for static linking */ 93 #define SetBitstreamPointer STATNAME(SetBitstreamPointer) 94 #define GetBits STATNAME(GetBits) 95 #define CalcBitsUsed STATNAME(CalcBitsUsed) 96 #define DequantChannel STATNAME(DequantChannel) 97 #define MidSideProc STATNAME(MidSideProc) 98 #define IntensityProcMPEG1 STATNAME(IntensityProcMPEG1) 99 #define IntensityProcMPEG2 STATNAME(IntensityProcMPEG2) 100 #define PolyphaseMono STATNAME(PolyphaseMono) 101 #define PolyphaseStereo STATNAME(PolyphaseStereo) 102 #define FDCT32 STATNAME(FDCT32) 103 104 #define ISFMpeg1 STATNAME(ISFMpeg1) 105 #define ISFMpeg2 STATNAME(ISFMpeg2) 106 #define ISFIIP STATNAME(ISFIIP) 107 #define uniqueIDTab STATNAME(uniqueIDTab) 108 #define coef32 STATNAME(coef32) 109 #define polyCoef STATNAME(polyCoef) 110 #define csa STATNAME(csa) 111 #define imdctWin STATNAME(imdctWin) 112 113 #define huffTable STATNAME(huffTable) 114 #define huffTabOffset STATNAME(huffTabOffset) 115 #define huffTabLookup STATNAME(huffTabLookup) 116 #define quadTable STATNAME(quadTable) 117 #define quadTabOffset STATNAME(quadTabOffset) 118 #define quadTabMaxBits STATNAME(quadTabMaxBits) 119 120 /* map these to the corresponding 2-bit values in the frame header */ 121 typedef enum { 122 Stereo = 0x00, /* two independent channels, but L and R frames might have different # of bits */ 123 Joint = 0x01, /* coupled channels - layer III: mix of M-S and intensity, Layers I/II: intensity and direct coding only */ 124 Dual = 0x02, /* two independent channels, L and R always have exactly 1/2 the total bitrate */ 125 Mono = 0x03 /* one channel */ 126 } StereoMode; 127 128 typedef struct _BitStreamInfo { 129 unsigned char *bytePtr; 130 unsigned int iCache; 131 int cachedBits; 132 int nBytes; 133 } BitStreamInfo; 134 135 typedef struct _FrameHeader { 136 MPEGVersion ver; /* version ID */ 137 int layer; /* layer index (1, 2, or 3) */ 138 int crc; /* CRC flag: 0 = disabled, 1 = enabled */ 139 int brIdx; /* bitrate index (0 - 15) */ 140 int srIdx; /* sample rate index (0 - 2) */ 141 int paddingBit; /* padding flag: 0 = no padding, 1 = single pad byte */ 142 int privateBit; /* unused */ 143 StereoMode sMode; /* mono/stereo mode */ 144 int modeExt; /* used to decipher joint stereo mode */ 145 int copyFlag; /* copyright flag: 0 = no, 1 = yes */ 146 int origFlag; /* original flag: 0 = copy, 1 = original */ 147 int emphasis; /* deemphasis mode */ 148 int CRCWord; /* CRC word (16 bits, 0 if crc not enabled) */ 149 150 const SFBandTable *sfBand; 151 } FrameHeader; 152 153 typedef struct _SideInfoSub { 154 int part23Length; /* number of bits in main data */ 155 int nBigvals; /* 2x this = first set of Huffman cw's (maximum amplitude can be > 1) */ 156 int globalGain; /* overall gain for dequantizer */ 157 int sfCompress; /* unpacked to figure out number of bits in scale factors */ 158 int winSwitchFlag; /* window switching flag */ 159 int blockType; /* block type */ 160 int mixedBlock; /* 0 = regular block (all short or long), 1 = mixed block */ 161 int tableSelect[3]; /* index of Huffman tables for the big values regions */ 162 int subBlockGain[3]; /* subblock gain offset, relative to global gain */ 163 int region0Count; /* 1+region0Count = num scale factor bands in first region of bigvals */ 164 int region1Count; /* 1+region1Count = num scale factor bands in second region of bigvals */ 165 int preFlag; /* for optional high frequency boost */ 166 int sfactScale; /* scaling of the scalefactors */ 167 int count1TableSelect; /* index of Huffman table for quad codewords */ 168 } SideInfoSub; 169 170 typedef struct _SideInfo { 171 int mainDataBegin; 172 int privateBits; 173 int scfsi[MAX_NCHAN][MAX_SCFBD]; /* 4 scalefactor bands per channel */ 174 175 SideInfoSub sis[MAX_NGRAN][MAX_NCHAN]; 176 } SideInfo; 177 178 typedef struct { 179 int cbType; /* pure long = 0, pure short = 1, mixed = 2 */ 180 int cbEndS[3]; /* number nonzero short cb's, per subbblock */ 181 int cbEndSMax; /* max of cbEndS[] */ 182 int cbEndL; /* number nonzero long cb's */ 183 } CriticalBandInfo; 184 185 typedef struct _DequantInfo { 186 int workBuf[MAX_REORDER_SAMPS]; /* workbuf for reordering short blocks */ 187 CriticalBandInfo cbi[MAX_NCHAN]; /* filled in dequantizer, used in joint stereo reconstruction */ 188 } DequantInfo; 189 190 typedef struct _HuffmanInfo { 191 int huffDecBuf[MAX_NCHAN][MAX_NSAMP]; /* used both for decoded Huffman values and dequantized coefficients */ 192 int nonZeroBound[MAX_NCHAN]; /* number of coeffs in huffDecBuf[ch] which can be > 0 */ 193 int gb[MAX_NCHAN]; /* minimum number of guard bits in huffDecBuf[ch] */ 194 } HuffmanInfo; 195 196 typedef enum _HuffTabType { 197 noBits, 198 oneShot, 199 loopNoLinbits, 200 loopLinbits, 201 quadA, 202 quadB, 203 invalidTab 204 } HuffTabType; 205 206 typedef struct _HuffTabLookup { 207 int linBits; 208 HuffTabType tabType; 209 } HuffTabLookup; 210 211 typedef struct _IMDCTInfo { 212 int outBuf[MAX_NCHAN][BLOCK_SIZE][NBANDS]; /* output of IMDCT */ 213 int overBuf[MAX_NCHAN][MAX_NSAMP / 2]; /* overlap-add buffer (by symmetry, only need 1/2 size) */ 214 int numPrevIMDCT[MAX_NCHAN]; /* how many IMDCT's calculated in this channel on prev. granule */ 215 int prevType[MAX_NCHAN]; 216 int prevWinSwitch[MAX_NCHAN]; 217 int gb[MAX_NCHAN]; 218 } IMDCTInfo; 219 220 typedef struct _BlockCount { 221 int nBlocksLong; 222 int nBlocksTotal; 223 int nBlocksPrev; 224 int prevType; 225 int prevWinSwitch; 226 int currWinSwitch; 227 int gbIn; 228 int gbOut; 229 } BlockCount; 230 231 /* max bits in scalefactors = 5, so use char's to save space */ 232 typedef struct _ScaleFactorInfoSub { 233 char l[23]; /* [band] */ 234 char s[13][3]; /* [band][window] */ 235 } ScaleFactorInfoSub; 236 237 /* used in MPEG 2, 2.5 intensity (joint) stereo only */ 238 typedef struct _ScaleFactorJS { 239 int intensityScale; 240 int slen[4]; 241 int nr[4]; 242 } ScaleFactorJS; 243 244 typedef struct _ScaleFactorInfo { 245 ScaleFactorInfoSub sfis[MAX_NGRAN][MAX_NCHAN]; 246 ScaleFactorJS sfjs; 247 } ScaleFactorInfo; 248 249 /* NOTE - could get by with smaller vbuf if memory is more important than speed 250 * (in Subband, instead of replicating each block in FDCT32 you would do a memmove on the 251 * last 15 blocks to shift them down one, a hardware style FIFO) 252 */ 253 typedef struct _SubbandInfo { 254 int vbuf[MAX_NCHAN * VBUF_LENGTH]; /* vbuf for fast DCT-based synthesis PQMF - double size for speed (no modulo indexing) */ 255 int vindex; /* internal index for tracking position in vbuf */ 256 } SubbandInfo; 257 258 /* bitstream.c */ 259 void SetBitstreamPointer(BitStreamInfo *bsi, int nBytes, unsigned char *buf); 260 unsigned int GetBits(BitStreamInfo *bsi, int nBits); 261 int CalcBitsUsed(BitStreamInfo *bsi, unsigned char *startBuf, int startOffset); 262 263 /* dequant.c, dqchan.c, stproc.c */ 264 int DequantChannel(int *sampleBuf, int *workBuf, int *nonZeroBound, FrameHeader *fh, SideInfoSub *sis, 265 ScaleFactorInfoSub *sfis, CriticalBandInfo *cbi); 266 void MidSideProc(int x[MAX_NCHAN][MAX_NSAMP], int nSamps, int mOut[2]); 267 void IntensityProcMPEG1(int x[MAX_NCHAN][MAX_NSAMP], int nSamps, FrameHeader *fh, ScaleFactorInfoSub *sfis, 268 CriticalBandInfo *cbi, int midSideFlag, int mixFlag, int mOut[2]); 269 void IntensityProcMPEG2(int x[MAX_NCHAN][MAX_NSAMP], int nSamps, FrameHeader *fh, ScaleFactorInfoSub *sfis, 270 CriticalBandInfo *cbi, ScaleFactorJS *sfjs, int midSideFlag, int mixFlag, int mOut[2]); 271 272 /* dct32.c */ 273 // about 1 ms faster in RAM, but very large 274 void FDCT32(int *x, int *d, int offset, int oddBlock, int gb);// __attribute__ ((section (".data"))); 275 276 /* hufftabs.c */ 277 extern const HuffTabLookup huffTabLookup[HUFF_PAIRTABS]; 278 extern const int huffTabOffset[HUFF_PAIRTABS]; 279 extern const unsigned short huffTable[]; 280 extern const unsigned char quadTable[64+16]; 281 extern const int quadTabOffset[2]; 282 extern const int quadTabMaxBits[2]; 283 284 /* polyphase.c (or asmpoly.s) 285 * some platforms require a C++ compile of all source files, 286 * so if we're compiling C as C++ and using native assembly 287 * for these functions we need to prevent C++ name mangling. 288 */ 289 #ifdef __cplusplus 290 extern "C" { 291 #endif 292 void PolyphaseMono(short *pcm, int *vbuf, const int *coefBase); 293 void PolyphaseStereo(short *pcm, int *vbuf, const int *coefBase); 294 #ifdef __cplusplus 295 } 296 #endif 297 298 /* trigtabs.c */ 299 extern const int imdctWin[4][36]; 300 extern const int ISFMpeg1[2][7]; 301 extern const int ISFMpeg2[2][2][16]; 302 extern const int ISFIIP[2][2]; 303 extern const int csa[8][2]; 304 extern const int coef32[31]; 305 extern const int polyCoef[264]; 306 307 #endif /* _CODER_H */