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