sbr.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Source last modified: $Id: sbr.h,v 1.2 2005/05/20 18:05:41 jrecker 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. * sbr.h - definitions of platform-specific SBR data structures, functions, and tables
  43. **************************************************************************************/
  44. #ifndef _SBR_H
  45. #define _SBR_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 NUM_TIME_SLOTS 16
  62. #define SAMPLES_PER_SLOT 2 /* RATE in spec */
  63. #define NUM_SAMPLE_RATES_SBR 9 /* downsampled (single-rate) mode unsupported, so only use Fs_sbr >= 16 kHz */
  64. #define MAX_NUM_ENV 5
  65. #define MAX_NUM_NOISE_FLOORS 2
  66. #define MAX_NUM_NOISE_FLOOR_BANDS 5 /* max Nq, see 4.6.18.3.6 */
  67. #define MAX_NUM_PATCHES 5
  68. #define MAX_NUM_SMOOTH_COEFS 5
  69. #define HF_GEN 8
  70. #define HF_ADJ 2
  71. #define MAX_QMF_BANDS 48 /* max QMF subbands covered by SBR (4.6.18.3.6) */
  72. #define FBITS_IN_QMFA 14
  73. #define FBITS_LOST_QMFA (1 + 2 + 3 + 2 + 1) /* 1 from cTab, 2 in premul, 3 in FFT, 2 in postmul, 1 for implicit scaling by 2.0 */
  74. #define FBITS_OUT_QMFA (FBITS_IN_QMFA - FBITS_LOST_QMFA)
  75. #define MIN_GBITS_IN_QMFS 2
  76. #define FBITS_IN_QMFS FBITS_OUT_QMFA
  77. #define FBITS_LOST_DCT4_64 (2 + 3 + 2) /* 2 in premul, 3 in FFT, 2 in postmul */
  78. #define FBITS_OUT_DQ_ENV 29 /* dequantized env scalefactors are Q(29 - envDataDequantScale) */
  79. #define FBITS_OUT_DQ_NOISE 24 /* range of Q_orig = [2^-24, 2^6] */
  80. #define NOISE_FLOOR_OFFSET 6
  81. /* see comments in ApplyBoost() */
  82. #define FBITS_GLIM_BOOST 24
  83. #define FBITS_QLIM_BOOST 14
  84. #define MAX_HUFF_BITS 20
  85. #define NUM_QMF_DELAY_BUFS 10
  86. #define DELAY_SAMPS_QMFA (NUM_QMF_DELAY_BUFS * 32)
  87. #define DELAY_SAMPS_QMFS (NUM_QMF_DELAY_BUFS * 128)
  88. /* additional external symbols to name-mangle for static linking */
  89. #define FFT32C STATNAME(FFT32C)
  90. #define CalcFreqTables STATNAME(CalcFreqTables)
  91. #define AdjustHighFreq STATNAME(AdjustHighFreq)
  92. #define GenerateHighFreq STATNAME(GenerateHighFreq)
  93. #define DecodeSBREnvelope STATNAME(DecodeSBREnvelope)
  94. #define DecodeSBRNoise STATNAME(DecodeSBRNoise)
  95. #define UncoupleSBREnvelope STATNAME(UncoupleSBREnvelope)
  96. #define UncoupleSBRNoise STATNAME(UncoupleSBRNoise)
  97. #define InvRNormalized STATNAME(InvRNormalized)
  98. #define RatioPowInv STATNAME(RatioPowInv)
  99. #define SqrtFix STATNAME(SqrtFix)
  100. #define QMFAnalysis STATNAME(QMFAnalysis)
  101. #define QMFSynthesis STATNAME(QMFSynthesis)
  102. #define GetSampRateIdx STATNAME(GetSampRateIdx)
  103. #define UnpackSBRHeader STATNAME(UnpackSBRHeader)
  104. #define UnpackSBRSingleChannel STATNAME(UnpackSBRSingleChannel)
  105. #define UnpackSBRChannelPair STATNAME(UnpackSBRChannelPair)
  106. /* asm functions */
  107. #define CVKernel1 STATNAME(CVKernel1)
  108. #define CVKernel2 STATNAME(CVKernel2)
  109. #define QMFAnalysisConv STATNAME(QMFAnalysisConv)
  110. #define QMFSynthesisConv STATNAME(QMFSynthesisConv)
  111. #define k0Tab STATNAME(k0Tab)
  112. #define k2Tab STATNAME(k2Tab)
  113. #define goalSBTab STATNAME(goalSBTab)
  114. #define huffTabSBR STATNAME(huffTabSBR)
  115. #define huffTabSBRInfo STATNAME(huffTabSBRInfo)
  116. #define log2Tab STATNAME(log2Tab)
  117. #define noiseTab STATNAME(noiseTab)
  118. #define cTabA STATNAME(cTabA)
  119. #define cTabS STATNAME(cTabS)
  120. /* do y <<= n, clipping to range [-2^30, 2^30 - 1] (i.e. output has one guard bit) */
  121. #define CLIP_2N_SHIFT30(y, n) { \
  122. int sign = (y) >> 31; \
  123. if (sign != (y) >> (30 - (n))) { \
  124. (y) = sign ^ (0x3fffffff); \
  125. } else { \
  126. (y) = (y) << (n); \
  127. } \
  128. }
  129. /*
  130. #define CLIP_2N(y, n) { \
  131. int sign = (y) >> 31; \
  132. if (sign != ((y) >> (n))) { \
  133. (y) = sign ^ ((1 << (n)) - 1); \
  134. } \
  135. }
  136. */
  137. enum {
  138. SBR_GRID_FIXFIX = 0,
  139. SBR_GRID_FIXVAR = 1,
  140. SBR_GRID_VARFIX = 2,
  141. SBR_GRID_VARVAR = 3
  142. };
  143. enum {
  144. HuffTabSBR_tEnv15 = 0,
  145. HuffTabSBR_fEnv15 = 1,
  146. HuffTabSBR_tEnv15b = 2,
  147. HuffTabSBR_fEnv15b = 3,
  148. HuffTabSBR_tEnv30 = 4,
  149. HuffTabSBR_fEnv30 = 5,
  150. HuffTabSBR_tEnv30b = 6,
  151. HuffTabSBR_fEnv30b = 7,
  152. HuffTabSBR_tNoise30 = 8,
  153. HuffTabSBR_fNoise30 = 5,
  154. HuffTabSBR_tNoise30b = 9,
  155. HuffTabSBR_fNoise30b = 7
  156. };
  157. typedef struct _HuffInfo {
  158. int maxBits; /* number of bits in longest codeword */
  159. unsigned /*char*/ int count[MAX_HUFF_BITS]; /* count[i] = number of codes with length i+1 bits */
  160. int offset; /* offset into symbol table */
  161. } HuffInfo;
  162. /* need one SBRHeader per element (SCE/CPE), updated only on new header */
  163. typedef struct _SBRHeader {
  164. int count;
  165. unsigned char ampRes;
  166. unsigned char startFreq;
  167. unsigned char stopFreq;
  168. unsigned char crossOverBand;
  169. unsigned char resBitsHdr;
  170. unsigned char hdrExtra1;
  171. unsigned char hdrExtra2;
  172. unsigned char freqScale;
  173. unsigned char alterScale;
  174. unsigned char noiseBands;
  175. unsigned char limiterBands;
  176. unsigned char limiterGains;
  177. unsigned char interpFreq;
  178. unsigned char smoothMode;
  179. } SBRHeader;
  180. /* need one SBRGrid per channel, updated every frame */
  181. typedef struct _SBRGrid {
  182. unsigned char frameClass;
  183. unsigned char ampResFrame;
  184. unsigned char pointer;
  185. unsigned char numEnv; /* L_E */
  186. unsigned char envTimeBorder[MAX_NUM_ENV+1]; /* t_E */
  187. unsigned char freqRes[MAX_NUM_ENV]; /* r */
  188. unsigned char numNoiseFloors; /* L_Q */
  189. unsigned char noiseTimeBorder[MAX_NUM_NOISE_FLOORS+1]; /* t_Q */
  190. unsigned char numEnvPrev;
  191. unsigned char numNoiseFloorsPrev;
  192. unsigned char freqResPrev;
  193. } SBRGrid;
  194. /* need one SBRFreq per element (SCE/CPE/LFE), updated only on header reset */
  195. typedef struct _SBRFreq {
  196. int kStart; /* k_x */
  197. int nMaster;
  198. int nHigh;
  199. int nLow;
  200. int nLimiter; /* N_l */
  201. int numQMFBands; /* M */
  202. int numNoiseFloorBands; /* Nq */
  203. int kStartPrev;
  204. int numQMFBandsPrev;
  205. unsigned char freqMaster[MAX_QMF_BANDS + 1]; /* not necessary to save this after derived tables are generated */
  206. unsigned char freqHigh[MAX_QMF_BANDS + 1];
  207. unsigned char freqLow[MAX_QMF_BANDS / 2 + 1]; /* nLow = nHigh - (nHigh >> 1) */
  208. unsigned char freqNoise[MAX_NUM_NOISE_FLOOR_BANDS+1];
  209. unsigned char freqLimiter[MAX_QMF_BANDS / 2 + MAX_NUM_PATCHES]; /* max (intermediate) size = nLow + numPatches - 1 */
  210. unsigned char numPatches;
  211. unsigned char patchNumSubbands[MAX_NUM_PATCHES + 1];
  212. unsigned char patchStartSubband[MAX_NUM_PATCHES + 1];
  213. } SBRFreq;
  214. typedef struct _SBRChan {
  215. int reset;
  216. unsigned char deltaFlagEnv[MAX_NUM_ENV];
  217. unsigned char deltaFlagNoise[MAX_NUM_NOISE_FLOORS];
  218. signed char envDataQuant[MAX_NUM_ENV][MAX_QMF_BANDS]; /* range = [0, 127] */
  219. signed char noiseDataQuant[MAX_NUM_NOISE_FLOORS][MAX_NUM_NOISE_FLOOR_BANDS];
  220. unsigned char invfMode[2][MAX_NUM_NOISE_FLOOR_BANDS]; /* invfMode[0/1][band] = prev/curr */
  221. int chirpFact[MAX_NUM_NOISE_FLOOR_BANDS]; /* bwArray */
  222. unsigned char addHarmonicFlag[2]; /* addHarmonicFlag[0/1] = prev/curr */
  223. unsigned char addHarmonic[2][64]; /* addHarmonic[0/1][band] = prev/curr */
  224. int gbMask[2]; /* gbMask[0/1] = XBuf[0-31]/XBuf[32-39] */
  225. signed char laPrev;
  226. int noiseTabIndex;
  227. int sinIndex;
  228. int gainNoiseIndex;
  229. int gTemp[MAX_NUM_SMOOTH_COEFS][MAX_QMF_BANDS];
  230. int qTemp[MAX_NUM_SMOOTH_COEFS][MAX_QMF_BANDS];
  231. } SBRChan;
  232. typedef struct _PSInfoSBR {
  233. /* save for entire file */
  234. int frameCount;
  235. int sampRateIdx;
  236. /* state info that must be saved for each channel */
  237. SBRHeader sbrHdr[AAC_MAX_NCHANS];
  238. SBRGrid sbrGrid[AAC_MAX_NCHANS];
  239. SBRFreq sbrFreq[AAC_MAX_NCHANS];
  240. SBRChan sbrChan[AAC_MAX_NCHANS];
  241. /* temp variables, no need to save between blocks */
  242. unsigned char dataExtra;
  243. unsigned char resBitsData;
  244. unsigned char extendedDataPresent;
  245. int extendedDataSize;
  246. signed char envDataDequantScale[MAX_NCHANS_ELEM][MAX_NUM_ENV];
  247. int envDataDequant[MAX_NCHANS_ELEM][MAX_NUM_ENV][MAX_QMF_BANDS];
  248. int noiseDataDequant[MAX_NCHANS_ELEM][MAX_NUM_NOISE_FLOORS][MAX_NUM_NOISE_FLOOR_BANDS];
  249. int eCurr[MAX_QMF_BANDS];
  250. unsigned char eCurrExp[MAX_QMF_BANDS];
  251. unsigned char eCurrExpMax;
  252. signed char la;
  253. int crcCheckWord;
  254. int couplingFlag;
  255. int envBand;
  256. int eOMGainMax;
  257. int gainMax;
  258. int gainMaxFBits;
  259. int noiseFloorBand;
  260. int qp1Inv;
  261. int qqp1Inv;
  262. int sMapped;
  263. int sBand;
  264. int highBand;
  265. int sumEOrigMapped;
  266. int sumECurrGLim;
  267. int sumSM;
  268. int sumQM;
  269. int gLimBoost[MAX_QMF_BANDS];
  270. int qmLimBoost[MAX_QMF_BANDS];
  271. int smBoost[MAX_QMF_BANDS];
  272. int smBuf[MAX_QMF_BANDS];
  273. int qmLimBuf[MAX_QMF_BANDS];
  274. int gLimBuf[MAX_QMF_BANDS];
  275. int gLimFbits[MAX_QMF_BANDS];
  276. int gFiltLast[MAX_QMF_BANDS];
  277. int qFiltLast[MAX_QMF_BANDS];
  278. /* large buffers */
  279. int delayIdxQMFA[AAC_MAX_NCHANS];
  280. int delayQMFA[AAC_MAX_NCHANS][DELAY_SAMPS_QMFA];
  281. int delayIdxQMFS[AAC_MAX_NCHANS];
  282. int delayQMFS[AAC_MAX_NCHANS][DELAY_SAMPS_QMFS];
  283. int XBufDelay[AAC_MAX_NCHANS][HF_GEN][64][2];
  284. int XBuf[32+8][64][2];
  285. } PSInfoSBR;
  286. /* sbrfft.c */
  287. void FFT32C(int *x);
  288. /* sbrfreq.c */
  289. int CalcFreqTables(SBRHeader *sbrHdr, SBRFreq *sbrFreq, int sampRateIdx);
  290. /* sbrhfadj.c */
  291. void AdjustHighFreq(PSInfoSBR *psi, SBRHeader *sbrHdr, SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChan, int ch);
  292. /* sbrhfgen.c */
  293. void GenerateHighFreq(PSInfoSBR *psi, SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChan, int ch);
  294. /* sbrhuff.c */
  295. void DecodeSBREnvelope(BitStreamInfo *bsi, PSInfoSBR *psi, SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChan, int ch);
  296. void DecodeSBRNoise(BitStreamInfo *bsi, PSInfoSBR *psi, SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChan, int ch);
  297. void UncoupleSBREnvelope(PSInfoSBR *psi, SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChanR);
  298. void UncoupleSBRNoise(PSInfoSBR *psi, SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChanR);
  299. /* sbrmath.c */
  300. int InvRNormalized(int r);
  301. int RatioPowInv(int a, int b, int c);
  302. int SqrtFix(int x, int fBitsIn, int *fBitsOut);
  303. /* sbrqmf.c */
  304. int QMFAnalysis(int *inbuf, int *delay, int *XBuf, int fBitsIn, int *delayIdx, int qmfaBands);
  305. void QMFSynthesis(int *inbuf, int *delay, int *delayIdx, int qmfsBands, short *outbuf, int nChans);
  306. /* sbrside.c */
  307. int GetSampRateIdx(int sampRate);
  308. int UnpackSBRHeader(BitStreamInfo *bsi, SBRHeader *sbrHdr);
  309. void UnpackSBRSingleChannel(BitStreamInfo *bsi, PSInfoSBR *psi, int chOut);
  310. void UnpackSBRChannelPair(BitStreamInfo *bsi, PSInfoSBR *psi, int chOut);
  311. /* sbrtabs.c */
  312. extern const unsigned char k0Tab[NUM_SAMPLE_RATES_SBR][16];
  313. extern const unsigned char k2Tab[NUM_SAMPLE_RATES_SBR][14];
  314. extern const unsigned char goalSBTab[NUM_SAMPLE_RATES_SBR];
  315. extern const HuffInfo huffTabSBRInfo[10];
  316. extern const signed int /*short*/ huffTabSBR[604];
  317. extern const int log2Tab[65];
  318. extern const int noiseTab[512*2];
  319. extern const int cTabA[165];
  320. extern const int cTabS[640];
  321. #endif /* _SBR_H */