blob: 1d626d469aaf78152f879fd9b1a90ffc561c9989
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 | /************************************************************************************** |
39 | * Fixed-point HE-AAC decoder |
40 | * Jon Recker (jrecker@real.com) |
41 | * February 2005 |
42 | * |
43 | * coder.h - definitions of platform-specific data structures, functions, and tables |
44 | **************************************************************************************/ |
45 | |
46 | #ifndef _CODER_H |
47 | #define _CODER_H |
48 | |
49 | #include "aaccommon.h" |
50 | #include "bitstream.h" |
51 | #include "aacdec.h" |
52 | |
53 | #ifndef ASSERT |
54 | #if defined(_WIN32) && defined(_M_IX86) && (defined (_DEBUG) || defined (REL_ENABLE_ASSERTS)) |
55 | #define ASSERT(x) if (!(x)) __asm int 3; |
56 | #else |
57 | #define ASSERT(x) /* do nothing */ |
58 | #endif |
59 | #endif |
60 | |
61 | #ifndef MAX |
62 | #define MAX(a,b) ((a) > (b) ? (a) : (b)) |
63 | #endif |
64 | |
65 | #ifndef MIN |
66 | #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
67 | #endif |
68 | |
69 | #define NWINDOWS_LONG 1 |
70 | #define NWINDOWS_SHORT 8 |
71 | |
72 | #define DATA_BUF_SIZE 510 /* max count = 255 + 255 */ |
73 | #define FILL_BUF_SIZE 269 /* max count = 15 + 255 - 1*/ |
74 | #define ADIF_COPYID_SIZE 9 |
75 | #define MAX_COMMENT_BYTES 255 |
76 | |
77 | #define MAX_NUM_FCE 15 |
78 | #define MAX_NUM_SCE 15 |
79 | #define MAX_NUM_BCE 15 |
80 | #define MAX_NUM_LCE 3 |
81 | #define MAX_NUM_ADE 7 |
82 | #define MAX_NUM_CCE 15 |
83 | |
84 | #define CHAN_ELEM_IS_CPE(x) (((x) & 0x10) >> 4) /* bit 4 = SCE/CPE flag */ |
85 | #define CHAN_ELEM_GET_TAG(x) (((x) & 0x0f) >> 0) /* bits 3-0 = instance tag */ |
86 | |
87 | #define CHAN_ELEM_SET_CPE(x) (((x) & 0x01) << 4) /* bit 4 = SCE/CPE flag */ |
88 | #define CHAN_ELEM_SET_TAG(x) (((x) & 0x0f) << 0) /* bits 3-0 = instance tag */ |
89 | |
90 | #define MAX_HUFF_BITS 20 |
91 | #define HUFFTAB_SPEC_OFFSET 1 |
92 | |
93 | /* do y <<= n, clipping to range [-2^30, 2^30 - 1] (i.e. output has one guard bit) */ |
94 | #define CLIP_2N_SHIFT(y, n) { \ |
95 | int sign = (y) >> 31; \ |
96 | if (sign != (y) >> (30 - (n))) { \ |
97 | (y) = sign ^ (0x3fffffff); \ |
98 | } else { \ |
99 | (y) = (y) << (n); \ |
100 | } \ |
101 | } |
102 | |
103 | /* clip to [-2^n, 2^n-1], valid range of n = [1, 30] */ |
104 | #define CLIP_2N(val, n) { \ |
105 | if ((val) >> 31 != (val) >> (n)) \ |
106 | (val) = ((val) >> 31) ^ ((1 << (n)) - 1); \ |
107 | } |
108 | |
109 | #define SF_DQ_OFFSET 15 |
110 | #define FBITS_OUT_DQ 20 |
111 | #define FBITS_OUT_DQ_OFF (FBITS_OUT_DQ - SF_DQ_OFFSET) /* number of fraction bits out of dequant, including 2^15 bias */ |
112 | |
113 | #define FBITS_IN_IMDCT FBITS_OUT_DQ_OFF /* number of fraction bits into IMDCT */ |
114 | #define GBITS_IN_DCT4 4 /* min guard bits in for DCT4 */ |
115 | |
116 | #define FBITS_LOST_DCT4 1 /* number of fraction bits lost (>> out) in DCT-IV */ |
117 | #define FBITS_LOST_WND 1 /* number of fraction bits lost (>> out) in synthesis window (neg = gain frac bits) */ |
118 | #define FBITS_LOST_IMDCT (FBITS_LOST_DCT4 + FBITS_LOST_WND) |
119 | #define FBITS_OUT_IMDCT (FBITS_IN_IMDCT - FBITS_LOST_IMDCT) |
120 | |
121 | #define NUM_IMDCT_SIZES 2 |
122 | |
123 | /* additional external symbols to name-mangle for static linking */ |
124 | #define DecodeProgramConfigElement STATNAME(DecodeProgramConfigElement) |
125 | #define DecodeHuffmanScalar STATNAME(DecodeHuffmanScalar) |
126 | #define DecodeSpectrumLong STATNAME(DecodeSpectrumLong) |
127 | #define DecodeSpectrumShort STATNAME(DecodeSpectrumShort) |
128 | #define DecodeICSInfo STATNAME(DecodeICSInfo) |
129 | #define DCT4 STATNAME(DCT4) |
130 | #define R4FFT STATNAME(R4FFT) |
131 | |
132 | #define DecWindowOverlapNoClip STATNAME(DecWindowOverlapNoClip) |
133 | #define DecWindowOverlapLongStartNoClip STATNAME(DecWindowOverlapLongStartNoClip) |
134 | #define DecWindowOverlapLongStopNoClip STATNAME(DecWindowOverlapLongStopNoClip) |
135 | #define DecWindowOverlapShortNoClip STATNAME(DecWindowOverlapShortNoClip) |
136 | |
137 | #define huffTabSpecInfo STATNAME(huffTabSpecInfo) |
138 | #define huffTabSpec STATNAME(huffTabSpec) |
139 | #define huffTabScaleFactInfo STATNAME(huffTabScaleFactInfo) |
140 | #define huffTabScaleFact STATNAME(huffTabScaleFact) |
141 | #define cos4sin4tab STATNAME(cos4sin4tab) |
142 | #define cos4sin4tabOffset STATNAME(cos4sin4tabOffset) |
143 | #define cos1sin1tab STATNAME(cos1sin1tab) |
144 | #define sinWindow STATNAME(sinWindow) |
145 | #define sinWindowOffset STATNAME(sinWindowOffset) |
146 | #define kbdWindow STATNAME(kbdWindow) |
147 | #define kbdWindowOffset STATNAME(kbdWindowOffset) |
148 | #define bitrevtab STATNAME(bitrevtab) |
149 | #define bitrevtabOffset STATNAME(bitrevtabOffset) |
150 | #define uniqueIDTab STATNAME(uniqueIDTab) |
151 | #define twidTabEven STATNAME(twidTabEven) |
152 | #define twidTabOdd STATNAME(twidTabOdd) |
153 | |
154 | typedef struct _HuffInfo { |
155 | int maxBits; /* number of bits in longest codeword */ |
156 | unsigned char count[MAX_HUFF_BITS]; /* count[i] = number of codes with length i+1 bits */ |
157 | int offset; /* offset into symbol table */ |
158 | } HuffInfo; |
159 | |
160 | typedef struct _PulseInfo { |
161 | unsigned char pulseDataPresent; |
162 | unsigned char numPulse; |
163 | unsigned char startSFB; |
164 | unsigned char offset[MAX_PULSES]; |
165 | unsigned char amp[MAX_PULSES]; |
166 | } PulseInfo; |
167 | |
168 | typedef struct _TNSInfo { |
169 | unsigned char tnsDataPresent; |
170 | unsigned char numFilt[MAX_TNS_FILTERS]; /* max 1 filter each for 8 short windows, or 3 filters for 1 long window */ |
171 | unsigned char coefRes[MAX_TNS_FILTERS]; |
172 | unsigned char length[MAX_TNS_FILTERS]; |
173 | unsigned char order[MAX_TNS_FILTERS]; |
174 | unsigned char dir[MAX_TNS_FILTERS]; |
175 | 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 */ |
176 | } TNSInfo; |
177 | |
178 | typedef struct _GainControlInfo { |
179 | unsigned char gainControlDataPresent; |
180 | unsigned char maxBand; |
181 | unsigned char adjNum[MAX_GAIN_BANDS][MAX_GAIN_WIN]; |
182 | unsigned char alevCode[MAX_GAIN_BANDS][MAX_GAIN_WIN][MAX_GAIN_ADJUST]; |
183 | unsigned char alocCode[MAX_GAIN_BANDS][MAX_GAIN_WIN][MAX_GAIN_ADJUST]; |
184 | } GainControlInfo; |
185 | |
186 | typedef struct _ICSInfo { |
187 | unsigned char icsResBit; |
188 | unsigned char winSequence; |
189 | unsigned char winShape; |
190 | unsigned char maxSFB; |
191 | unsigned char sfGroup; |
192 | unsigned char predictorDataPresent; |
193 | unsigned char predictorReset; |
194 | unsigned char predictorResetGroupNum; |
195 | unsigned char predictionUsed[MAX_PRED_SFB]; |
196 | unsigned char numWinGroup; |
197 | unsigned char winGroupLen[MAX_WIN_GROUPS]; |
198 | } ICSInfo; |
199 | |
200 | typedef struct _ADTSHeader { |
201 | /* fixed */ |
202 | unsigned char id; /* MPEG bit - should be 1 */ |
203 | unsigned char layer; /* MPEG layer - should be 0 */ |
204 | unsigned char protectBit; /* 0 = CRC word follows, 1 = no CRC word */ |
205 | unsigned char profile; /* 0 = main, 1 = LC, 2 = SSR, 3 = reserved */ |
206 | unsigned char sampRateIdx; /* sample rate index range = [0, 11] */ |
207 | unsigned char privateBit; /* ignore */ |
208 | unsigned char channelConfig; /* 0 = implicit, >0 = use default table */ |
209 | unsigned char origCopy; /* 0 = copy, 1 = original */ |
210 | unsigned char home; /* ignore */ |
211 | |
212 | /* variable */ |
213 | unsigned char copyBit; /* 1 bit of the 72-bit copyright ID (transmitted as 1 bit per frame) */ |
214 | unsigned char copyStart; /* 1 = this bit starts the 72-bit ID, 0 = it does not */ |
215 | int frameLength; /* length of frame */ |
216 | int bufferFull; /* number of 32-bit words left in enc buffer, 0x7FF = VBR */ |
217 | unsigned char numRawDataBlocks; /* number of raw data blocks in frame */ |
218 | |
219 | /* CRC */ |
220 | int crcCheckWord; /* 16-bit CRC check word (present if protectBit == 0) */ |
221 | } ADTSHeader; |
222 | |
223 | typedef struct _ADIFHeader { |
224 | unsigned char copyBit; /* 0 = no copyright ID, 1 = 72-bit copyright ID follows immediately */ |
225 | unsigned char origCopy; /* 0 = copy, 1 = original */ |
226 | unsigned char home; /* ignore */ |
227 | unsigned char bsType; /* bitstream type: 0 = CBR, 1 = VBR */ |
228 | int bitRate; /* bitRate: CBR = bits/sec, VBR = peak bits/frame, 0 = unknown */ |
229 | unsigned char numPCE; /* number of program config elements (max = 16) */ |
230 | int bufferFull; /* bits left in bit reservoir */ |
231 | unsigned char copyID[ADIF_COPYID_SIZE]; /* optional 72-bit copyright ID */ |
232 | } ADIFHeader; |
233 | |
234 | /* sizeof(ProgConfigElement) = 82 bytes (if KEEP_PCE_COMMENTS not defined) */ |
235 | typedef struct _ProgConfigElement { |
236 | unsigned char elemInstTag; /* element instance tag */ |
237 | unsigned char profile; /* 0 = main, 1 = LC, 2 = SSR, 3 = reserved */ |
238 | unsigned char sampRateIdx; /* sample rate index range = [0, 11] */ |
239 | unsigned char numFCE; /* number of front channel elements (max = 15) */ |
240 | unsigned char numSCE; /* number of side channel elements (max = 15) */ |
241 | unsigned char numBCE; /* number of back channel elements (max = 15) */ |
242 | unsigned char numLCE; /* number of LFE channel elements (max = 3) */ |
243 | unsigned char numADE; /* number of associated data elements (max = 7) */ |
244 | unsigned char numCCE; /* number of valid channel coupling elements (max = 15) */ |
245 | unsigned char monoMixdown; /* mono mixdown: bit 4 = present flag, bits 3-0 = element number */ |
246 | unsigned char stereoMixdown; /* stereo mixdown: bit 4 = present flag, bits 3-0 = element number */ |
247 | unsigned char matrixMixdown; /* matrix mixdown: bit 4 = present flag, bit 3 = unused, |
248 | bits 2-1 = index, bit 0 = pseudo-surround enable */ |
249 | unsigned char fce[MAX_NUM_FCE]; /* front element channel pair: bit 4 = SCE/CPE flag, bits 3-0 = inst tag */ |
250 | unsigned char sce[MAX_NUM_SCE]; /* side element channel pair: bit 4 = SCE/CPE flag, bits 3-0 = inst tag */ |
251 | unsigned char bce[MAX_NUM_BCE]; /* back element channel pair: bit 4 = SCE/CPE flag, bits 3-0 = inst tag */ |
252 | unsigned char lce[MAX_NUM_LCE]; /* instance tag for LFE elements */ |
253 | unsigned char ade[MAX_NUM_ADE]; /* instance tag for ADE elements */ |
254 | unsigned char cce[MAX_NUM_BCE]; /* channel coupling elements: bit 4 = switching flag, bits 3-0 = inst tag */ |
255 | |
256 | #ifdef KEEP_PCE_COMMENTS |
257 | /* make this optional - if not enabled, decoder will just skip comments */ |
258 | unsigned char commentBytes; |
259 | unsigned char commentField[MAX_COMMENT_BYTES]; |
260 | #endif |
261 | |
262 | } ProgConfigElement; |
263 | |
264 | /* state info struct for baseline (MPEG-4 LC) decoding */ |
265 | typedef struct _PSInfoBase { |
266 | /* header information */ |
267 | ADTSHeader fhADTS; |
268 | ADIFHeader fhADIF; |
269 | ProgConfigElement pce[MAX_NUM_PCE_ADIF]; |
270 | int dataCount; |
271 | unsigned char dataBuf[DATA_BUF_SIZE]; |
272 | int fillCount; |
273 | unsigned char fillBuf[FILL_BUF_SIZE]; |
274 | |
275 | /* state information which is the same throughout whole frame */ |
276 | int nChans; |
277 | int useImpChanMap; |
278 | int sampRateIdx; |
279 | |
280 | /* state information which can be overwritten by subsequent elements within frame */ |
281 | ICSInfo icsInfo[MAX_NCHANS_ELEM]; |
282 | |
283 | int commonWin; |
284 | short scaleFactors[MAX_NCHANS_ELEM][MAX_SF_BANDS]; |
285 | unsigned char sfbCodeBook[MAX_NCHANS_ELEM][MAX_SF_BANDS]; |
286 | |
287 | int msMaskPresent; |
288 | unsigned char msMaskBits[MAX_MS_MASK_BYTES]; |
289 | |
290 | int pnsUsed[MAX_NCHANS_ELEM]; |
291 | int pnsLastVal; |
292 | int intensityUsed[MAX_NCHANS_ELEM]; |
293 | |
294 | PulseInfo pulseInfo[MAX_NCHANS_ELEM]; |
295 | |
296 | TNSInfo tnsInfo[MAX_NCHANS_ELEM]; |
297 | int tnsLPCBuf[MAX_TNS_ORDER]; |
298 | int tnsWorkBuf[MAX_TNS_ORDER]; |
299 | |
300 | GainControlInfo gainControlInfo[MAX_NCHANS_ELEM]; |
301 | |
302 | int gbCurrent[MAX_NCHANS_ELEM]; |
303 | int coef[MAX_NCHANS_ELEM][AAC_MAX_NSAMPS]; |
304 | #ifdef AAC_ENABLE_SBR |
305 | int sbrWorkBuf[MAX_NCHANS_ELEM][AAC_MAX_NSAMPS]; |
306 | #endif |
307 | /* state information which must be saved for each element and used in next frame */ |
308 | int overlap[AAC_MAX_NCHANS][AAC_MAX_NSAMPS]; |
309 | int prevWinShape[AAC_MAX_NCHANS]; |
310 | |
311 | } PSInfoBase; |
312 | |
313 | /* private implementation-specific functions */ |
314 | |
315 | /* decelmnt.c */ |
316 | int DecodeProgramConfigElement(ProgConfigElement *pce, BitStreamInfo *bsi); |
317 | |
318 | /* huffman.c */ |
319 | int DecodeHuffmanScalar(const signed short *huffTab, const HuffInfo *huffTabInfo, unsigned int bitBuf, signed int *val); |
320 | void DecodeSpectrumLong(PSInfoBase *psi, BitStreamInfo *bsi, int ch); |
321 | void DecodeSpectrumShort(PSInfoBase *psi, BitStreamInfo *bsi, int ch); |
322 | |
323 | /* noiseless.c */ |
324 | void DecodeICSInfo(BitStreamInfo *bsi, ICSInfo *icsInfo, int sampRateIdx); |
325 | |
326 | /* dct4.c */ |
327 | void DCT4(int tabidx, int *coef, int gb); |
328 | |
329 | /* fft.c */ |
330 | void R4FFT(int tabidx, int *x); |
331 | |
332 | /* sbrimdct.c */ |
333 | void DecWindowOverlapNoClip(int *buf0, int *over0, int *out0, int winTypeCurr, int winTypePrev); |
334 | void DecWindowOverlapLongStartNoClip(int *buf0, int *over0, int *out0, int winTypeCurr, int winTypePrev); |
335 | void DecWindowOverlapLongStopNoClip(int *buf0, int *over0, int *out0, int winTypeCurr, int winTypePrev); |
336 | void DecWindowOverlapShortNoClip(int *buf0, int *over0, int *out0, int winTypeCurr, int winTypePrev); |
337 | |
338 | /* hufftabs.c */ |
339 | extern const HuffInfo huffTabSpecInfo[11]; |
340 | extern const signed short huffTabSpec[1241]; |
341 | extern const HuffInfo huffTabScaleFactInfo; |
342 | extern const signed short huffTabScaleFact[121]; |
343 | |
344 | /* trigtabs.c */ |
345 | extern const int cos4sin4tabOffset[NUM_IMDCT_SIZES]; |
346 | extern const int sinWindowOffset[NUM_IMDCT_SIZES]; |
347 | extern const int kbdWindowOffset[NUM_IMDCT_SIZES]; |
348 | extern const unsigned char bitrevtab[17 + 129]; |
349 | extern const int bitrevtabOffset[NUM_IMDCT_SIZES]; |
350 | |
351 | #ifdef HELIX_CONFIG_AAC_GENERATE_TRIGTABS_FLOAT |
352 | /* trigtabs_fltgen.c */ |
353 | extern int cos4sin4tab[128 + 1024]; |
354 | extern int cos1sin1tab[514]; |
355 | extern int sinWindow[128 + 1024]; |
356 | extern int kbdWindow[128 + 1024]; |
357 | extern int twidTabEven[4 * 6 + 16 * 6 + 64 * 6]; |
358 | extern int twidTabOdd[8 * 6 + 32 * 6 + 128 * 6]; |
359 | #else |
360 | /* trigtabs.c */ |
361 | extern const int cos4sin4tab[128 + 1024]; |
362 | extern const int cos1sin1tab[514]; |
363 | extern const int sinWindow[128 + 1024]; |
364 | extern const int kbdWindow[128 + 1024]; |
365 | extern const int twidTabEven[4 * 6 + 16 * 6 + 64 * 6]; |
366 | extern const int twidTabOdd[8 * 6 + 32 * 6 + 128 * 6]; |
367 | #endif |
368 | |
369 | #endif /* _CODER_H */ |
370 | |
371 |