blob: 5d6ae2768461340f8682ea5d01e93a7b1f6cdad7
1 | /* ***** BEGIN LICENSE BLOCK ***** |
2 | * Source last modified: $Id: coder.h,v 1.9 2005/04/27 19:20:50 hubbe Exp $ |
3 | * |
4 | * REALNETWORKS CONFIDENTIAL--NOT FOR DISTRIBUTION IN SOURCE CODE FORM |
5 | * Portions Copyright (c) 1995-2002 RealNetworks, Inc. |
6 | * All Rights Reserved. |
7 | * |
8 | * The contents of this file, and the files included with this file, |
9 | * are subject to the current version of the Real Format Source Code |
10 | * Porting and Optimization License, available at |
11 | * https://helixcommunity.org/2005/license/realformatsource (unless |
12 | * RealNetworks otherwise expressly agrees in writing that you are |
13 | * subject to a different license). You may also obtain the license |
14 | * terms directly from RealNetworks. You may not use this file except |
15 | * in compliance with the Real Format Source Code Porting and |
16 | * Optimization License. There are no redistribution rights for the |
17 | * source code of this file. Please see the Real Format Source Code |
18 | * Porting and Optimization License for the rights, obligations and |
19 | * limitations governing use of the contents of the file. |
20 | * |
21 | * RealNetworks is the developer of the Original Code and owns the |
22 | * copyrights in the portions it created. |
23 | * |
24 | * This file, and the files included with this file, is distributed and |
25 | * made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, |
26 | * EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL |
27 | * SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF |
28 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT |
29 | * OR NON-INFRINGEMENT. |
30 | * |
31 | * Technology Compatibility Kit Test Suite(s) Location: |
32 | * https://rarvcode-tck.helixcommunity.org |
33 | * |
34 | * Contributor(s): |
35 | * |
36 | * ***** END LICENSE BLOCK ***** */ |
37 | |
38 | /************************************************************************************** |
39 | * Fixed-point RealAudio 8 decoder |
40 | * Jon Recker (jrecker@real.com), Ken Cooke (kenc@real.com) |
41 | * October 2003 |
42 | * |
43 | * coder.h - private, implementation-specific header file |
44 | **************************************************************************************/ |
45 | |
46 | #ifndef _CODER_H |
47 | #define _CODER_H |
48 | |
49 | #include "gecko2codec.h" /* contains public API */ |
50 | #include "statname.h" /* do name-mangling for static linking */ |
51 | |
52 | #if defined _WIN32 && defined _DEBUG && defined FORTIFY |
53 | #include "fortify.h" |
54 | #endif |
55 | |
56 | #define CODINGDELAY 2 /* frames of coding delay */ |
57 | |
58 | #ifndef ASSERT |
59 | #if defined (_WIN32) && defined (_M_IX86) && (defined (_DEBUG) || defined (REL_ENABLE_ASSERTS)) |
60 | #define ASSERT(x) if (!(x)) __asm int 3; |
61 | #else |
62 | #define ASSERT(x) /* do nothing */ |
63 | #endif |
64 | #endif |
65 | |
66 | #ifndef MAX |
67 | #define MAX(a,b) ((a) > (b) ? (a) : (b)) |
68 | #endif |
69 | |
70 | #ifndef MIN |
71 | #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
72 | #endif |
73 | |
74 | /* do y <<= n, clipping to range [-2^30, 2^30 - 1] (i.e. output has one guard bit) */ |
75 | #define CLIP_2N_SHIFT(y, n) { \ |
76 | int sign = (y) >> 31; \ |
77 | if (sign != (y) >> (30 - (n))) { \ |
78 | (y) = sign ^ (0x3fffffff); \ |
79 | } else { \ |
80 | (y) = (y) << (n); \ |
81 | } \ |
82 | } |
83 | |
84 | #define FBITS_OUT_DQ 12 /* number of fraction bits in output of dequant */ |
85 | #define FBITS_LOST_IMLT 7 /* number of fraction bits lost (>> out) in IMLT */ |
86 | #define GBITS_IN_IMLT 4 /* min guard bits in for IMLT */ |
87 | |
88 | /* coder */ |
89 | #define NBINS 20 /* transform bins per region */ |
90 | #define MAXCATZNS (1 << 7) |
91 | #define NCPLBANDS 20 |
92 | |
93 | #define MAXNCHAN 2 |
94 | #define MAXNSAMP 1024 |
95 | #define MAXREGNS (MAXNSAMP/NBINS) |
96 | |
97 | #define MAXDECBUF 4 |
98 | |
99 | /* composite mlt */ |
100 | #define MAXCSAMP (2*MAXNSAMP) |
101 | #define MAXCREGN (2*MAXREGNS) |
102 | #define NUM_MLT_SIZES 3 |
103 | #define MAXNMLT 1024 |
104 | #define MAXCPLQBITS 6 |
105 | |
106 | /* gain control */ |
107 | #define NPARTS 8 /* parts per half-window */ |
108 | #define MAXNATS 8 |
109 | #define LOCBITS 3 /* must be log2(NPARTS)! */ |
110 | #define GAINBITS 4 /* must match clamping limits! */ |
111 | #define GAINMAX 4 |
112 | #define GAINMIN (-7) /* clamps to GAINMIN<=gain<=GAINMAX */ |
113 | #define GAINDIF (GAINMAX - GAINMIN) |
114 | #define CODE2GAIN(g) ((g) + GAINMIN) |
115 | |
116 | #define NUM_POWTABLES 13 /* number of distinct tables for power envelope coding */ |
117 | #define MAX_HUFF_BITS 16 /* max bits in any Huffman codeword (important - make sure this is true!) */ |
118 | |
119 | /* |
120 | * Random bit generator, using a 32-bit linear feedback shift register. |
121 | * Primitive polynomial is x^32 + x^7 + x^5 + x^3 + x^2 + x^1 + x^0. |
122 | * lfsr = state variable |
123 | * |
124 | * Update step: |
125 | * sign = (lfsr >> 31); |
126 | * lfsr = (lfsr << 1) ^ (sign & FEEDBACK); |
127 | */ |
128 | #define FEEDBACK ((1<<7)|(1<<5)|(1<<3)|(1<<2)|(1<<1)|(1<<0)) |
129 | |
130 | typedef struct _HuffInfo { |
131 | int maxBits; |
132 | unsigned char count[16]; /* number of codes at this length */ |
133 | int offset; |
134 | } HuffInfo; |
135 | |
136 | /* bitstream info */ |
137 | typedef struct _BitStreamInfo { |
138 | unsigned char *buf; |
139 | int off; |
140 | int key; |
141 | } BitStreamInfo; |
142 | |
143 | /* gain control info */ |
144 | typedef struct _GAINC { |
145 | short nats; /* number of attacks, [0..8]*/ |
146 | short loc[MAXNATS]; /* location of attack, [0..7] */ |
147 | short gain[MAXNATS]; /* gain code, [-7..8] */ |
148 | short maxExGain; /* max gain after expansion */ |
149 | } GAINC; |
150 | |
151 | /* buffers for decoding and reconstructing transform coefficients */ |
152 | typedef struct _DecBufs { |
153 | int decmlt[MAXNCHAN][MAXNSAMP]; |
154 | int overlap[MAXNCHAN][MAXNSAMP]; |
155 | |
156 | /* Categorize() */ |
157 | int maxcat[MAXCREGN]; |
158 | int mincat[MAXCREGN]; |
159 | int changes[2 * MAXCATZNS]; /* grows from middle outward */ |
160 | int maxheap[MAXCREGN + 1]; /* upheap sentinel */ |
161 | int minheap[MAXCREGN + 1]; /* upheap sentinel */ |
162 | |
163 | /* DecodeMLT() */ |
164 | int rmsIndex[MAXCREGN]; /* RMS power quant index */ |
165 | int catbuf[MAXCREGN]; |
166 | |
167 | /* JointDecodeMLT() */ |
168 | int cplindex[NCPLBANDS]; |
169 | } DecBufs; |
170 | |
171 | typedef struct _ { |
172 | short adts_hi; |
173 | short adts_lo; |
174 | short lostflag; |
175 | short jointflag; |
176 | short nChannels; |
177 | short nSamples; |
178 | short sampRate; |
179 | short xformIdx; |
180 | short gbMin[MAXNCHAN]; //2 |
181 | short xbits[MAXNCHAN][2]; //4 |
182 | GAINC dgainc[MAXNCHAN][CODINGDELAY]; //18*2*2=72 |
183 | short exgain[2 * NPARTS + 1]; |
184 | int decmlt[MAXNCHAN][MAXNSAMP]; |
185 | int (*overlap)[MAXNCHAN][MAXNSAMP]; |
186 | } DecodeInfo; |
187 | |
188 | typedef struct _Gecko2Info { |
189 | /* general codec params */ |
190 | int nRegions; |
191 | int nFrameBits; |
192 | int sampRate; |
193 | int cplStart; |
194 | int cplQbits; |
195 | int rateBits; |
196 | int cRegions; |
197 | int nCatzns; |
198 | int jointStereo; |
199 | |
200 | /* dither for dequant */ |
201 | int lfsr[MAXNCHAN]; |
202 | |
203 | /* transform info */ |
204 | int rateCode; |
205 | int rmsMax[MAXNCHAN]; |
206 | |
207 | /* bitstream info */ |
208 | BitStreamInfo bsi; |
209 | |
210 | short lostflag; |
211 | short nChannels; |
212 | short nSamples; |
213 | short xformIdx; |
214 | short gbMin[MAXNCHAN]; |
215 | short xbits[MAXNCHAN][2]; |
216 | |
217 | /* gain control info */ |
218 | GAINC dgainc[MAXNCHAN][CODINGDELAY]; |
219 | |
220 | /* data buffers */ |
221 | DecBufs db; |
222 | |
223 | /* decode info management */ |
224 | int rd; |
225 | int wr; |
226 | DecodeInfo block[4]; |
227 | } Gecko2Info; |
228 | |
229 | /* memory allocation */ |
230 | Gecko2Info *AllocateBuffers(void); |
231 | void FreeBuffers(Gecko2Info *gi); |
232 | |
233 | /* bitstream decoding */ |
234 | int DecodeSideInfo(Gecko2Info *gi, unsigned char *buf, int availbits, int ch); |
235 | unsigned int GetBits(BitStreamInfo *bsi, int nBits, int advanceFlag); |
236 | void AdvanceBitstream(BitStreamInfo *bsi, int nBits); |
237 | |
238 | /* huffman decoding */ |
239 | int DecodeHuffmanScalar(const unsigned short *huffTab, const HuffInfo *huffTabInfo, int bitBuf, int *val); |
240 | |
241 | /* gain control parameter decoding */ |
242 | int DecodeGainInfo(Gecko2Info *gi, GAINC *gainc, int availbits); |
243 | void CopyGainInfo(GAINC *gaincDest, GAINC *gaincSource); |
244 | |
245 | /* joint stereo parameter decoding */ |
246 | void JointDecodeMLT(Gecko2Info *gi, int *mltleft, int *mltrght); |
247 | int DecodeCoupleInfo(Gecko2Info *gi, int availbits); |
248 | |
249 | /* transform coefficient decoding */ |
250 | int DecodeEnvelope(Gecko2Info *gi, int availbits, int ch); |
251 | void CategorizeAndExpand(Gecko2Info *gi, int availbits); |
252 | int DecodeTransform(Gecko2Info *gi, int *mlt, int availbits, int *lfsrInit, int ch); |
253 | |
254 | /* inverse transform */ |
255 | void IMLTNoWindow(int tabidx, int *mlt, int gb); |
256 | void R4FFT(int tabidx, int *x); |
257 | |
258 | /* synthesis window, gain control, overlap-add */ |
259 | void DecWindowWithAttacks(int tabidx, int *buf1, int *overlap, short *pcm1, int nChans, GAINC *gainc0, GAINC *gainc1, short fbits[2]); |
260 | void DecWindowNoAttacks(int tabidx, int *buf1, int *overlap, short *pcm1, int nChans); |
261 | |
262 | /* bitpack.c */ |
263 | extern const unsigned char pkkey[4]; |
264 | |
265 | /* hufftabs.c */ |
266 | #define HUFFTAB_COUPLE_OFFSET 2 |
267 | |
268 | extern const HuffInfo huffTabCoupleInfo[5]; |
269 | extern const unsigned short huffTabCouple[119]; |
270 | extern const HuffInfo huffTabPowerInfo[13]; |
271 | extern const unsigned short huffTabPower[312]; |
272 | extern const HuffInfo huffTabVectorInfo[7]; |
273 | extern const unsigned short huffTabVector[1276]; |
274 | |
275 | /* trigtabs.c */ |
276 | extern const int nmltTab[3]; |
277 | extern const int window[256 + 512 + 1024]; |
278 | extern const int windowOffset[3]; |
279 | extern const int cos4sin4tab[256 + 512 + 1024]; |
280 | extern const int cos4sin4tabOffset[3]; |
281 | extern const int cos1sin1tab[514]; |
282 | extern const unsigned char bitrevtab[33 + 65 + 129]; |
283 | extern const int bitrevtabOffset[3]; |
284 | extern const int twidTabEven[4 * 6 + 16 * 6 + 64 * 6]; |
285 | extern const int twidTabOdd[8 * 6 + 32 * 6 + 128 * 6]; |
286 | |
287 | #endif /* _CODER_H */ |
288 |