coder.h 11 KB

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