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