coder.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Source last modified: $Id: coder.h,v 1.2 2005/06/27 21:06:00 gwright Exp $
  3. *
  4. * Portions Copyright (c) 1995-2005 RealNetworks, Inc. All Rights Reserved.
  5. *
  6. * The contents of this file, and the files included with this file,
  7. * are subject to the current version of the RealNetworks Public
  8. * Source License (the "RPSL") available at
  9. * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10. * the file under the current version of the RealNetworks Community
  11. * Source License (the "RCSL") available at
  12. * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
  13. * will apply. You may also obtain the license terms directly from
  14. * RealNetworks. You may not use this file except in compliance with
  15. * the RPSL or, if you have a valid RCSL with RealNetworks applicable
  16. * to this file, the RCSL. Please see the applicable RPSL or RCSL for
  17. * the rights, obligations and limitations governing use of the
  18. * contents of the file.
  19. *
  20. * This file is part of the Helix DNA Technology. RealNetworks is the
  21. * developer of the Original Code and owns the copyrights in the
  22. * portions it created.
  23. *
  24. * This file, and the files included with this file, is distributed
  25. * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
  26. * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
  27. * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
  28. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
  29. * ENJOYMENT OR NON-INFRINGEMENT.
  30. *
  31. * Technology Compatibility Kit Test Suite(s) Location:
  32. * http://www.helixcommunity.org/content/tck
  33. *
  34. * Contributor(s):
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. /**************************************************************************************
  38. * Fixed-point HE-AAC decoder
  39. * Jon Recker (jrecker@real.com)
  40. * February 2005
  41. *
  42. * coder.h - definitions of platform-specific data structures, functions, and tables
  43. **************************************************************************************/
  44. #ifndef _CODER_H
  45. #define _CODER_H
  46. #include "aaccommon.h"
  47. #include "bitstream.h"
  48. #ifndef ASSERT
  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. #endif
  55. #ifndef MAX
  56. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  57. #endif
  58. #ifndef MIN
  59. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  60. #endif
  61. #define NWINDOWS_LONG 1
  62. #define NWINDOWS_SHORT 8
  63. #define DATA_BUF_SIZE 510 /* max count = 255 + 255 */
  64. #define FILL_BUF_SIZE 269 /* max count = 15 + 255 - 1*/
  65. #define ADIF_COPYID_SIZE 9
  66. #define MAX_COMMENT_BYTES 255
  67. #define MAX_NUM_FCE 15
  68. #define MAX_NUM_SCE 15
  69. #define MAX_NUM_BCE 15
  70. #define MAX_NUM_LCE 3
  71. #define MAX_NUM_ADE 7
  72. #define MAX_NUM_CCE 15
  73. #define CHAN_ELEM_IS_CPE(x) (((x) & 0x10) >> 4) /* bit 4 = SCE/CPE flag */
  74. #define CHAN_ELEM_GET_TAG(x) (((x) & 0x0f) >> 0) /* bits 3-0 = instance tag */
  75. #define CHAN_ELEM_SET_CPE(x) (((x) & 0x01) << 4) /* bit 4 = SCE/CPE flag */
  76. #define CHAN_ELEM_SET_TAG(x) (((x) & 0x0f) << 0) /* bits 3-0 = instance tag */
  77. #define MAX_HUFF_BITS 20
  78. #define HUFFTAB_SPEC_OFFSET 1
  79. /* do y <<= n, clipping to range [-2^30, 2^30 - 1] (i.e. output has one guard bit) */
  80. /*
  81. #define CLIP_2N_SHIFT(y, n) { \
  82. int sign = (y) >> 31; \
  83. if (sign != (y) >> (30 - (n))) { \
  84. (y) = sign ^ (0x3fffffff); \
  85. } else { \
  86. (y) = (y) << (n); \
  87. } \
  88. }
  89. */
  90. /* clip to [-2^n, 2^n-1], valid range of n = [1, 30] */
  91. /*
  92. #define CLIP_2N(val, n) { \
  93. if ((val) >> 31 != (val) >> (n)) \
  94. (val) = ((val) >> 31) ^ ((1 << (n)) - 1); \
  95. }
  96. */
  97. #define SF_DQ_OFFSET 15
  98. #define FBITS_OUT_DQ 20
  99. #define FBITS_OUT_DQ_OFF (FBITS_OUT_DQ - SF_DQ_OFFSET) /* number of fraction bits out of dequant, including 2^15 bias */
  100. #define FBITS_IN_IMDCT FBITS_OUT_DQ_OFF /* number of fraction bits into IMDCT */
  101. #define GBITS_IN_DCT4 4 /* min guard bits in for DCT4 */
  102. #define FBITS_LOST_DCT4 1 /* number of fraction bits lost (>> out) in DCT-IV */
  103. #define FBITS_LOST_WND 1 /* number of fraction bits lost (>> out) in synthesis window (neg = gain frac bits) */
  104. #define FBITS_LOST_IMDCT (FBITS_LOST_DCT4 + FBITS_LOST_WND)
  105. #define FBITS_OUT_IMDCT (FBITS_IN_IMDCT - FBITS_LOST_IMDCT)
  106. #define NUM_IMDCT_SIZES 2
  107. /* additional external symbols to name-mangle for static linking */
  108. #define DecodeProgramConfigElement STATNAME(DecodeProgramConfigElement)
  109. #define DecodeHuffmanScalar STATNAME(DecodeHuffmanScalar)
  110. #define DecodeSpectrumLong STATNAME(DecodeSpectrumLong)
  111. #define DecodeSpectrumShort STATNAME(DecodeSpectrumShort)
  112. #define DecodeICSInfo STATNAME(DecodeICSInfo)
  113. #define DCT4 STATNAME(DCT4)
  114. #define R4FFT STATNAME(R4FFT)
  115. #define DecWindowOverlapNoClip STATNAME(DecWindowOverlapNoClip)
  116. #define DecWindowOverlapLongStartNoClip STATNAME(DecWindowOverlapLongStartNoClip)
  117. #define DecWindowOverlapLongStopNoClip STATNAME(DecWindowOverlapLongStopNoClip)
  118. #define DecWindowOverlapShortNoClip STATNAME(DecWindowOverlapShortNoClip)
  119. #define huffTabSpecInfo STATNAME(huffTabSpecInfo)
  120. #define huffTabSpec STATNAME(huffTabSpec)
  121. #define huffTabScaleFactInfo STATNAME(huffTabScaleFactInfo)
  122. #define huffTabScaleFact STATNAME(huffTabScaleFact)
  123. #define cos4sin4tab STATNAME(cos4sin4tab)
  124. #define cos4sin4tabOffset STATNAME(cos4sin4tabOffset)
  125. #define cos1sin1tab STATNAME(cos1sin1tab)
  126. #define sinWindow STATNAME(sinWindow)
  127. #define sinWindowOffset STATNAME(sinWindowOffset)
  128. #define kbdWindow STATNAME(kbdWindow)
  129. #define kbdWindowOffset STATNAME(kbdWindowOffset)
  130. #define bitrevtab STATNAME(bitrevtab)
  131. #define bitrevtabOffset STATNAME(bitrevtabOffset)
  132. #define uniqueIDTab STATNAME(uniqueIDTab)
  133. #define twidTabEven STATNAME(twidTabEven)
  134. #define twidTabOdd STATNAME(twidTabOdd)
  135. typedef struct _HuffInfo {
  136. int maxBits; /* number of bits in longest codeword */
  137. unsigned /*char*/ int count[MAX_HUFF_BITS]; /* count[i] = number of codes with length i+1 bits */
  138. int offset; /* offset into symbol table */
  139. } HuffInfo;
  140. typedef struct _PulseInfo {
  141. unsigned char pulseDataPresent;
  142. unsigned char numPulse;
  143. unsigned char startSFB;
  144. unsigned char offset[MAX_PULSES];
  145. unsigned char amp[MAX_PULSES];
  146. } PulseInfo;
  147. typedef struct _TNSInfo {
  148. unsigned char tnsDataPresent;
  149. unsigned char numFilt[MAX_TNS_FILTERS]; /* max 1 filter each for 8 short windows, or 3 filters for 1 long window */
  150. unsigned char coefRes[MAX_TNS_FILTERS];
  151. unsigned char length[MAX_TNS_FILTERS];
  152. unsigned char order[MAX_TNS_FILTERS];
  153. unsigned char dir[MAX_TNS_FILTERS];
  154. signed char coef[MAX_TNS_COEFS]; /* max 3 filters * 20 coefs for 1 long window, or 1 filter * 7 coefs for each of 8 short windows */
  155. } TNSInfo;
  156. typedef struct _GainControlInfo {
  157. unsigned char gainControlDataPresent;
  158. unsigned char maxBand;
  159. unsigned char adjNum[MAX_GAIN_BANDS][MAX_GAIN_WIN];
  160. unsigned char alevCode[MAX_GAIN_BANDS][MAX_GAIN_WIN][MAX_GAIN_ADJUST];
  161. unsigned char alocCode[MAX_GAIN_BANDS][MAX_GAIN_WIN][MAX_GAIN_ADJUST];
  162. } GainControlInfo;
  163. typedef struct _ICSInfo {
  164. unsigned char icsResBit;
  165. unsigned char winSequence;
  166. unsigned char winShape;
  167. unsigned char maxSFB;
  168. unsigned char sfGroup;
  169. unsigned char predictorDataPresent;
  170. unsigned char predictorReset;
  171. unsigned char predictorResetGroupNum;
  172. unsigned char predictionUsed[MAX_PRED_SFB];
  173. unsigned char numWinGroup;
  174. unsigned char winGroupLen[MAX_WIN_GROUPS];
  175. } ICSInfo;
  176. typedef struct _ADTSHeader {
  177. /* fixed */
  178. unsigned char id; /* MPEG bit - should be 1 */
  179. unsigned char layer; /* MPEG layer - should be 0 */
  180. unsigned char protectBit; /* 0 = CRC word follows, 1 = no CRC word */
  181. unsigned char profile; /* 0 = main, 1 = LC, 2 = SSR, 3 = reserved */
  182. unsigned char sampRateIdx; /* sample rate index range = [0, 11] */
  183. unsigned char privateBit; /* ignore */
  184. unsigned char channelConfig; /* 0 = implicit, >0 = use default table */
  185. unsigned char origCopy; /* 0 = copy, 1 = original */
  186. unsigned char home; /* ignore */
  187. /* variable */
  188. unsigned char copyBit; /* 1 bit of the 72-bit copyright ID (transmitted as 1 bit per frame) */
  189. unsigned char copyStart; /* 1 = this bit starts the 72-bit ID, 0 = it does not */
  190. int frameLength; /* length of frame */
  191. int bufferFull; /* number of 32-bit words left in enc buffer, 0x7FF = VBR */
  192. unsigned char numRawDataBlocks; /* number of raw data blocks in frame */
  193. /* CRC */
  194. int crcCheckWord; /* 16-bit CRC check word (present if protectBit == 0) */
  195. } ADTSHeader;
  196. typedef struct _ADIFHeader {
  197. unsigned char copyBit; /* 0 = no copyright ID, 1 = 72-bit copyright ID follows immediately */
  198. unsigned char origCopy; /* 0 = copy, 1 = original */
  199. unsigned char home; /* ignore */
  200. unsigned char bsType; /* bitstream type: 0 = CBR, 1 = VBR */
  201. int bitRate; /* bitRate: CBR = bits/sec, VBR = peak bits/frame, 0 = unknown */
  202. unsigned char numPCE; /* number of program config elements (max = 16) */
  203. int bufferFull; /* bits left in bit reservoir */
  204. unsigned char copyID[ADIF_COPYID_SIZE]; /* optional 72-bit copyright ID */
  205. } ADIFHeader;
  206. /* sizeof(ProgConfigElement) = 82 bytes (if KEEP_PCE_COMMENTS not defined) */
  207. typedef struct _ProgConfigElement {
  208. unsigned char elemInstTag; /* element instance tag */
  209. unsigned char profile; /* 0 = main, 1 = LC, 2 = SSR, 3 = reserved */
  210. unsigned char sampRateIdx; /* sample rate index range = [0, 11] */
  211. unsigned char numFCE; /* number of front channel elements (max = 15) */
  212. unsigned char numSCE; /* number of side channel elements (max = 15) */
  213. unsigned char numBCE; /* number of back channel elements (max = 15) */
  214. unsigned char numLCE; /* number of LFE channel elements (max = 3) */
  215. unsigned char numADE; /* number of associated data elements (max = 7) */
  216. unsigned char numCCE; /* number of valid channel coupling elements (max = 15) */
  217. unsigned char monoMixdown; /* mono mixdown: bit 4 = present flag, bits 3-0 = element number */
  218. unsigned char stereoMixdown; /* stereo mixdown: bit 4 = present flag, bits 3-0 = element number */
  219. unsigned char matrixMixdown; /* matrix mixdown: bit 4 = present flag, bit 3 = unused,
  220. bits 2-1 = index, bit 0 = pseudo-surround enable */
  221. unsigned char fce[MAX_NUM_FCE]; /* front element channel pair: bit 4 = SCE/CPE flag, bits 3-0 = inst tag */
  222. unsigned char sce[MAX_NUM_SCE]; /* side element channel pair: bit 4 = SCE/CPE flag, bits 3-0 = inst tag */
  223. unsigned char bce[MAX_NUM_BCE]; /* back element channel pair: bit 4 = SCE/CPE flag, bits 3-0 = inst tag */
  224. unsigned char lce[MAX_NUM_LCE]; /* instance tag for LFE elements */
  225. unsigned char ade[MAX_NUM_ADE]; /* instance tag for ADE elements */
  226. unsigned char cce[MAX_NUM_BCE]; /* channel coupling elements: bit 4 = switching flag, bits 3-0 = inst tag */
  227. #ifdef KEEP_PCE_COMMENTS
  228. /* make this optional - if not enabled, decoder will just skip comments */
  229. unsigned char commentBytes;
  230. unsigned char commentField[MAX_COMMENT_BYTES];
  231. #endif
  232. } ProgConfigElement;
  233. /* state info struct for baseline (MPEG-4 LC) decoding */
  234. typedef struct _PSInfoBase {
  235. /* header information */
  236. ADTSHeader fhADTS;
  237. ADIFHeader fhADIF;
  238. ProgConfigElement pce[MAX_NUM_PCE_ADIF];
  239. int dataCount;
  240. unsigned char dataBuf[DATA_BUF_SIZE];
  241. int fillCount;
  242. unsigned char fillBuf[FILL_BUF_SIZE];
  243. /* state information which is the same throughout whole frame */
  244. int nChans;
  245. int useImpChanMap;
  246. int sampRateIdx;
  247. /* state information which can be overwritten by subsequent elements within frame */
  248. ICSInfo icsInfo[MAX_NCHANS_ELEM];
  249. int commonWin;
  250. short scaleFactors[MAX_NCHANS_ELEM][MAX_SF_BANDS];
  251. unsigned char sfbCodeBook[MAX_NCHANS_ELEM][MAX_SF_BANDS];
  252. int msMaskPresent;
  253. unsigned char msMaskBits[MAX_MS_MASK_BYTES];
  254. int pnsUsed[MAX_NCHANS_ELEM];
  255. int pnsLastVal;
  256. int intensityUsed[MAX_NCHANS_ELEM];
  257. PulseInfo pulseInfo[MAX_NCHANS_ELEM];
  258. TNSInfo tnsInfo[MAX_NCHANS_ELEM];
  259. int tnsLPCBuf[MAX_TNS_ORDER];
  260. int tnsWorkBuf[MAX_TNS_ORDER];
  261. GainControlInfo gainControlInfo[MAX_NCHANS_ELEM];
  262. int gbCurrent[MAX_NCHANS_ELEM];
  263. int coef[MAX_NCHANS_ELEM][AAC_MAX_NSAMPS];
  264. #ifdef AAC_ENABLE_SBR
  265. int sbrWorkBuf[MAX_NCHANS_ELEM][AAC_MAX_NSAMPS];
  266. #endif
  267. /* state information which must be saved for each element and used in next frame */
  268. int overlap[AAC_MAX_NCHANS][AAC_MAX_NSAMPS];
  269. int prevWinShape[AAC_MAX_NCHANS];
  270. } PSInfoBase;
  271. /* private implementation-specific functions */
  272. /* decelmnt.c */
  273. int DecodeProgramConfigElement(ProgConfigElement *pce, BitStreamInfo *bsi);
  274. /* huffman.c */
  275. int DecodeHuffmanScalar(const signed short *huffTab, const HuffInfo *huffTabInfo, unsigned int bitBuf, signed int *val);
  276. void DecodeSpectrumLong(PSInfoBase *psi, BitStreamInfo *bsi, int ch);
  277. void DecodeSpectrumShort(PSInfoBase *psi, BitStreamInfo *bsi, int ch);
  278. /* noiseless.c */
  279. void DecodeICSInfo(BitStreamInfo *bsi, ICSInfo *icsInfo, int sampRateIdx);
  280. /* dct4.c */
  281. void DCT4(int tabidx, int *coef, int gb);
  282. /* fft.c */
  283. void R4FFT(int tabidx, int *x);
  284. /* sbrimdct.c */
  285. void DecWindowOverlapNoClip(int *buf0, int *over0, int *out0, int winTypeCurr, int winTypePrev);
  286. void DecWindowOverlapLongStartNoClip(int *buf0, int *over0, int *out0, int winTypeCurr, int winTypePrev);
  287. void DecWindowOverlapLongStopNoClip(int *buf0, int *over0, int *out0, int winTypeCurr, int winTypePrev);
  288. void DecWindowOverlapShortNoClip(int *buf0, int *over0, int *out0, int winTypeCurr, int winTypePrev);
  289. /* hufftabs.c */
  290. extern const HuffInfo huffTabSpecInfo[11];
  291. extern const signed short huffTabSpec[1241];
  292. extern const HuffInfo huffTabScaleFactInfo;
  293. extern const signed short huffTabScaleFact[121];
  294. /* trigtabs.c */
  295. extern const int cos4sin4tabOffset[NUM_IMDCT_SIZES];
  296. extern const int sinWindowOffset[NUM_IMDCT_SIZES];
  297. extern const int kbdWindowOffset[NUM_IMDCT_SIZES];
  298. extern const unsigned char bitrevtab[17 + 129];
  299. extern const int bitrevtabOffset[NUM_IMDCT_SIZES];
  300. #ifdef HELIX_CONFIG_AAC_GENERATE_TRIGTABS_FLOAT
  301. /* trigtabs_fltgen.c */
  302. extern int cos4sin4tab[128 + 1024];
  303. extern int cos1sin1tab[514];
  304. extern int sinWindow[128 + 1024];
  305. extern int kbdWindow[128 + 1024];
  306. extern int twidTabEven[4*6 + 16*6 + 64*6];
  307. extern int twidTabOdd[8*6 + 32*6 + 128*6];
  308. #else
  309. /* trigtabs.c */
  310. extern const int cos4sin4tab[128 + 1024];
  311. extern const int cos1sin1tab[514];
  312. extern const int sinWindow[128 + 1024];
  313. extern const int kbdWindow[128 + 1024];
  314. extern const int twidTabEven[4*6 + 16*6 + 64*6];
  315. extern const int twidTabOdd[8*6 + 32*6 + 128*6];
  316. #endif
  317. #endif /* _CODER_H */