coder.h 11 KB

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