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