blob: fdf310f6ff8a33bac924f3de8d648da16c9c017e
1 | /* ***** BEGIN LICENSE BLOCK ***** |
2 | * Source last modified: $Id: huffman.c,v 1.2 2005/05/24 16:01:55 albertofloyd 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 | * huffman.c - Huffman decoding |
44 | **************************************************************************************/ |
45 | |
46 | #include "coder.h" |
47 | |
48 | /************************************************************************************** |
49 | * Function: DecodeHuffmanScalar |
50 | * |
51 | * Description: decode one Huffman symbol from bitstream |
52 | * |
53 | * Inputs: pointers to Huffman table and info struct |
54 | * left-aligned bit buffer with >= huffTabInfo->maxBits bits |
55 | * |
56 | * Outputs: decoded symbol in *val |
57 | * |
58 | * Return: number of bits in symbol |
59 | * |
60 | * Notes: assumes canonical Huffman codes: |
61 | * first CW always 0, we have "count" CW's of length "nBits" bits |
62 | * starting CW for codes of length nBits+1 = |
63 | * (startCW[nBits] + count[nBits]) << 1 |
64 | * if there are no codes at nBits, then we just keep << 1 each time |
65 | * (since count[nBits] = 0) |
66 | **************************************************************************************/ |
67 | int DecodeHuffmanScalar(const signed short *huffTab, const HuffInfo *huffTabInfo, unsigned int bitBuf, signed int *val) |
68 | { |
69 | unsigned int count, start, shift, t; |
70 | const unsigned char *countPtr; |
71 | const signed short *map; |
72 | |
73 | map = huffTab + huffTabInfo->offset; |
74 | countPtr = huffTabInfo->count; |
75 | |
76 | start = 0; |
77 | count = 0; |
78 | shift = 32; |
79 | do { |
80 | start += count; |
81 | start <<= 1; |
82 | map += count; |
83 | count = *countPtr++; |
84 | shift--; |
85 | t = (bitBuf >> shift) - start; |
86 | } while (t >= count); |
87 | |
88 | *val = (signed int)map[t]; |
89 | return (countPtr - huffTabInfo->count); |
90 | } |
91 | |
92 | #define APPLY_SIGN(v, s) {(v) ^= ((signed int)(s) >> 31); (v) -= ((signed int)(s) >> 31);} |
93 | |
94 | #define GET_QUAD_SIGNBITS(v) (((unsigned int)(v) << 17) >> 29) /* bits 14-12, unsigned */ |
95 | #define GET_QUAD_W(v) (((signed int)(v) << 20) >> 29) /* bits 11-9, sign-extend */ |
96 | #define GET_QUAD_X(v) (((signed int)(v) << 23) >> 29) /* bits 8-6, sign-extend */ |
97 | #define GET_QUAD_Y(v) (((signed int)(v) << 26) >> 29) /* bits 5-3, sign-extend */ |
98 | #define GET_QUAD_Z(v) (((signed int)(v) << 29) >> 29) /* bits 2-0, sign-extend */ |
99 | |
100 | #define GET_PAIR_SIGNBITS(v) (((unsigned int)(v) << 20) >> 30) /* bits 11-10, unsigned */ |
101 | #define GET_PAIR_Y(v) (((signed int)(v) << 22) >> 27) /* bits 9-5, sign-extend */ |
102 | #define GET_PAIR_Z(v) (((signed int)(v) << 27) >> 27) /* bits 4-0, sign-extend */ |
103 | |
104 | #define GET_ESC_SIGNBITS(v) (((unsigned int)(v) << 18) >> 30) /* bits 13-12, unsigned */ |
105 | #define GET_ESC_Y(v) (((signed int)(v) << 20) >> 26) /* bits 11-6, sign-extend */ |
106 | #define GET_ESC_Z(v) (((signed int)(v) << 26) >> 26) /* bits 5-0, sign-extend */ |
107 | |
108 | /************************************************************************************** |
109 | * Function: UnpackZeros |
110 | * |
111 | * Description: fill a section of coefficients with zeros |
112 | * |
113 | * Inputs: number of coefficients |
114 | * |
115 | * Outputs: nVals zeros, starting at coef |
116 | * |
117 | * Return: none |
118 | * |
119 | * Notes: assumes nVals is always a multiple of 4 because all scalefactor bands |
120 | * are a multiple of 4 coefficients long |
121 | **************************************************************************************/ |
122 | static void UnpackZeros(int nVals, int *coef) |
123 | { |
124 | while (nVals > 0) { |
125 | *coef++ = 0; |
126 | *coef++ = 0; |
127 | *coef++ = 0; |
128 | *coef++ = 0; |
129 | nVals -= 4; |
130 | } |
131 | } |
132 | |
133 | /************************************************************************************** |
134 | * Function: UnpackQuads |
135 | * |
136 | * Description: decode a section of 4-way vector Huffman coded coefficients |
137 | * |
138 | * Inputs BitStreamInfo struct pointing to start of codewords for this section |
139 | * index of Huffman codebook |
140 | * number of coefficients |
141 | * |
142 | * Outputs: nVals coefficients, starting at coef |
143 | * |
144 | * Return: none |
145 | * |
146 | * Notes: assumes nVals is always a multiple of 4 because all scalefactor bands |
147 | * are a multiple of 4 coefficients long |
148 | **************************************************************************************/ |
149 | static void UnpackQuads(BitStreamInfo *bsi, int cb, int nVals, int *coef) |
150 | { |
151 | int w, x, y, z, maxBits, nCodeBits, nSignBits, val; |
152 | unsigned int bitBuf; |
153 | |
154 | maxBits = huffTabSpecInfo[cb - HUFFTAB_SPEC_OFFSET].maxBits + 4; |
155 | while (nVals > 0) { |
156 | /* decode quad */ |
157 | bitBuf = GetBitsNoAdvance(bsi, maxBits) << (32 - maxBits); |
158 | nCodeBits = DecodeHuffmanScalar(huffTabSpec, &huffTabSpecInfo[cb - HUFFTAB_SPEC_OFFSET], bitBuf, &val); |
159 | |
160 | w = GET_QUAD_W(val); |
161 | x = GET_QUAD_X(val); |
162 | y = GET_QUAD_Y(val); |
163 | z = GET_QUAD_Z(val); |
164 | |
165 | bitBuf <<= nCodeBits; |
166 | nSignBits = (int)GET_QUAD_SIGNBITS(val); |
167 | AdvanceBitstream(bsi, nCodeBits + nSignBits); |
168 | if (nSignBits) { |
169 | if (w) { |
170 | APPLY_SIGN(w, bitBuf); |
171 | bitBuf <<= 1; |
172 | } |
173 | if (x) { |
174 | APPLY_SIGN(x, bitBuf); |
175 | bitBuf <<= 1; |
176 | } |
177 | if (y) { |
178 | APPLY_SIGN(y, bitBuf); |
179 | bitBuf <<= 1; |
180 | } |
181 | if (z) { |
182 | APPLY_SIGN(z, bitBuf); |
183 | bitBuf <<= 1; |
184 | } |
185 | } |
186 | *coef++ = w; |
187 | *coef++ = x; |
188 | *coef++ = y; |
189 | *coef++ = z; |
190 | nVals -= 4; |
191 | } |
192 | } |
193 | |
194 | /************************************************************************************** |
195 | * Function: UnpackPairsNoEsc |
196 | * |
197 | * Description: decode a section of 2-way vector Huffman coded coefficients, |
198 | * using non-esc tables (5 through 10) |
199 | * |
200 | * Inputs BitStreamInfo struct pointing to start of codewords for this section |
201 | * index of Huffman codebook (must not be the escape codebook) |
202 | * number of coefficients |
203 | * |
204 | * Outputs: nVals coefficients, starting at coef |
205 | * |
206 | * Return: none |
207 | * |
208 | * Notes: assumes nVals is always a multiple of 2 because all scalefactor bands |
209 | * are a multiple of 4 coefficients long |
210 | **************************************************************************************/ |
211 | static void UnpackPairsNoEsc(BitStreamInfo *bsi, int cb, int nVals, int *coef) |
212 | { |
213 | int y, z, maxBits, nCodeBits, nSignBits, val; |
214 | unsigned int bitBuf; |
215 | |
216 | maxBits = huffTabSpecInfo[cb - HUFFTAB_SPEC_OFFSET].maxBits + 2; |
217 | while (nVals > 0) { |
218 | /* decode pair */ |
219 | bitBuf = GetBitsNoAdvance(bsi, maxBits) << (32 - maxBits); |
220 | nCodeBits = DecodeHuffmanScalar(huffTabSpec, &huffTabSpecInfo[cb - HUFFTAB_SPEC_OFFSET], bitBuf, &val); |
221 | |
222 | y = GET_PAIR_Y(val); |
223 | z = GET_PAIR_Z(val); |
224 | |
225 | bitBuf <<= nCodeBits; |
226 | nSignBits = GET_PAIR_SIGNBITS(val); |
227 | AdvanceBitstream(bsi, nCodeBits + nSignBits); |
228 | if (nSignBits) { |
229 | if (y) { |
230 | APPLY_SIGN(y, bitBuf); |
231 | bitBuf <<= 1; |
232 | } |
233 | if (z) { |
234 | APPLY_SIGN(z, bitBuf); |
235 | bitBuf <<= 1; |
236 | } |
237 | } |
238 | *coef++ = y; |
239 | *coef++ = z; |
240 | nVals -= 2; |
241 | } |
242 | } |
243 | |
244 | /************************************************************************************** |
245 | * Function: UnpackPairsEsc |
246 | * |
247 | * Description: decode a section of 2-way vector Huffman coded coefficients, |
248 | * using esc table (11) |
249 | * |
250 | * Inputs BitStreamInfo struct pointing to start of codewords for this section |
251 | * index of Huffman codebook (must be the escape codebook) |
252 | * number of coefficients |
253 | * |
254 | * Outputs: nVals coefficients, starting at coef |
255 | * |
256 | * Return: none |
257 | * |
258 | * Notes: assumes nVals is always a multiple of 2 because all scalefactor bands |
259 | * are a multiple of 4 coefficients long |
260 | **************************************************************************************/ |
261 | static void UnpackPairsEsc(BitStreamInfo *bsi, int cb, int nVals, int *coef) |
262 | { |
263 | int y, z, maxBits, nCodeBits, nSignBits, n, val; |
264 | unsigned int bitBuf; |
265 | |
266 | maxBits = huffTabSpecInfo[cb - HUFFTAB_SPEC_OFFSET].maxBits + 2; |
267 | while (nVals > 0) { |
268 | /* decode pair with escape value */ |
269 | bitBuf = GetBitsNoAdvance(bsi, maxBits) << (32 - maxBits); |
270 | nCodeBits = DecodeHuffmanScalar(huffTabSpec, &huffTabSpecInfo[cb - HUFFTAB_SPEC_OFFSET], bitBuf, &val); |
271 | |
272 | y = GET_ESC_Y(val); |
273 | z = GET_ESC_Z(val); |
274 | |
275 | bitBuf <<= nCodeBits; |
276 | nSignBits = GET_ESC_SIGNBITS(val); |
277 | AdvanceBitstream(bsi, nCodeBits + nSignBits); |
278 | |
279 | if (y == 16) { |
280 | n = 4; |
281 | while (GetBits(bsi, 1) == 1) { |
282 | n++; |
283 | } |
284 | y = (1 << n) + GetBits(bsi, n); |
285 | } |
286 | if (z == 16) { |
287 | n = 4; |
288 | while (GetBits(bsi, 1) == 1) { |
289 | n++; |
290 | } |
291 | z = (1 << n) + GetBits(bsi, n); |
292 | } |
293 | |
294 | if (nSignBits) { |
295 | if (y) { |
296 | APPLY_SIGN(y, bitBuf); |
297 | bitBuf <<= 1; |
298 | } |
299 | if (z) { |
300 | APPLY_SIGN(z, bitBuf); |
301 | bitBuf <<= 1; |
302 | } |
303 | } |
304 | |
305 | *coef++ = y; |
306 | *coef++ = z; |
307 | nVals -= 2; |
308 | } |
309 | } |
310 | |
311 | /************************************************************************************** |
312 | * Function: DecodeSpectrumLong |
313 | * |
314 | * Description: decode transform coefficients for frame with one long block |
315 | * |
316 | * Inputs: platform specific info struct |
317 | * BitStreamInfo struct pointing to start of spectral data |
318 | * (14496-3, table 4.4.29) |
319 | * index of current channel |
320 | * |
321 | * Outputs: decoded, quantized coefficients for this channel |
322 | * |
323 | * Return: none |
324 | * |
325 | * Notes: adds in pulse data if present |
326 | * fills coefficient buffer with zeros in any region not coded with |
327 | * codebook in range [1, 11] (including sfb's above sfbMax) |
328 | **************************************************************************************/ |
329 | int DecodeSpectrumLong(PSInfoBase *psi, BitStreamInfo *bsi, int ch) |
330 | { |
331 | int i, sfb, cb, nVals, offset; |
332 | const short *sfbTab; |
333 | unsigned char *sfbCodeBook; |
334 | int *coef; |
335 | ICSInfo *icsInfo; |
336 | PulseInfo *pi; |
337 | |
338 | coef = psi->coef[ch]; |
339 | icsInfo = (ch == 1 && psi->commonWin == 1) ? &(psi->icsInfo[0]) : &(psi->icsInfo[ch]); |
340 | |
341 | /* decode long block */ |
342 | sfbTab = sfBandTabLong + sfBandTabLongOffset[psi->sampRateIdx]; |
343 | sfbCodeBook = psi->sfbCodeBook[ch]; |
344 | for (sfb = 0; sfb < icsInfo->maxSFB; sfb++) { |
345 | cb = *sfbCodeBook++; |
346 | nVals = sfbTab[sfb + 1] - sfbTab[sfb]; |
347 | |
348 | if (cb == 0) { |
349 | UnpackZeros(nVals, coef); |
350 | } else if (cb <= 4) { |
351 | UnpackQuads(bsi, cb, nVals, coef); |
352 | } else if (cb <= 10) { |
353 | UnpackPairsNoEsc(bsi, cb, nVals, coef); |
354 | } else if (cb == 11) { |
355 | UnpackPairsEsc(bsi, cb, nVals, coef); |
356 | } else { |
357 | UnpackZeros(nVals, coef); |
358 | } |
359 | |
360 | coef += nVals; |
361 | } |
362 | |
363 | /* fill with zeros above maxSFB */ |
364 | nVals = NSAMPS_LONG - sfbTab[sfb]; |
365 | UnpackZeros(nVals, coef); |
366 | |
367 | /* add pulse data, if present */ |
368 | pi = &psi->pulseInfo[ch]; |
369 | if (pi->pulseDataPresent) { |
370 | coef = psi->coef[ch]; |
371 | offset = sfbTab[pi->startSFB]; |
372 | for (i = 0; i < pi->numPulse; i++) { |
373 | offset += pi->offset[i]; |
374 | if (coef[offset] > 0) { |
375 | coef[offset] += pi->amp[i]; |
376 | } else { |
377 | coef[offset] -= pi->amp[i]; |
378 | } |
379 | } |
380 | ASSERT(offset < NSAMPS_LONG, ERR_AAC_HUFFMAN_DECODING); |
381 | } |
382 | return ERR_AAC_NONE; |
383 | } |
384 | |
385 | /************************************************************************************** |
386 | * Function: DecodeSpectrumShort |
387 | * |
388 | * Description: decode transform coefficients for frame with eight short blocks |
389 | * |
390 | * Inputs: platform specific info struct |
391 | * BitStreamInfo struct pointing to start of spectral data |
392 | * (14496-3, table 4.4.29) |
393 | * index of current channel |
394 | * |
395 | * Outputs: decoded, quantized coefficients for this channel |
396 | * |
397 | * Return: none |
398 | * |
399 | * Notes: fills coefficient buffer with zeros in any region not coded with |
400 | * codebook in range [1, 11] (including sfb's above sfbMax) |
401 | * deinterleaves window groups into 8 windows |
402 | **************************************************************************************/ |
403 | int DecodeSpectrumShort(PSInfoBase *psi, BitStreamInfo *bsi, int ch) |
404 | { |
405 | int gp, cb, nVals = 0, win, offset, sfb; |
406 | const short *sfbTab; |
407 | unsigned char *sfbCodeBook; |
408 | int *coef; |
409 | ICSInfo *icsInfo; |
410 | coef = psi->coef[ch]; |
411 | icsInfo = (ch == 1 && psi->commonWin == 1) ? &(psi->icsInfo[0]) : &(psi->icsInfo[ch]); |
412 | |
413 | /* decode short blocks, deinterleaving in-place */ |
414 | sfbTab = sfBandTabShort + sfBandTabShortOffset[psi->sampRateIdx]; |
415 | sfbCodeBook = psi->sfbCodeBook[ch]; |
416 | for (gp = 0; gp < icsInfo->numWinGroup; gp++) { |
417 | for (sfb = 0; sfb < icsInfo->maxSFB; sfb++) { |
418 | nVals = sfbTab[sfb + 1] - sfbTab[sfb]; |
419 | cb = *sfbCodeBook++; |
420 | |
421 | for (win = 0; win < icsInfo->winGroupLen[gp]; win++) { |
422 | offset = win * NSAMPS_SHORT; |
423 | if (cb == 0) { |
424 | UnpackZeros(nVals, coef + offset); |
425 | } else if (cb <= 4) { |
426 | UnpackQuads(bsi, cb, nVals, coef + offset); |
427 | } else if (cb <= 10) { |
428 | UnpackPairsNoEsc(bsi, cb, nVals, coef + offset); |
429 | } else if (cb == 11) { |
430 | UnpackPairsEsc(bsi, cb, nVals, coef + offset); |
431 | } else { |
432 | UnpackZeros(nVals, coef + offset); |
433 | } |
434 | } |
435 | coef += nVals; |
436 | } |
437 | |
438 | /* fill with zeros above maxSFB */ |
439 | for (win = 0; win < icsInfo->winGroupLen[gp]; win++) { |
440 | offset = win * NSAMPS_SHORT; |
441 | nVals = NSAMPS_SHORT - sfbTab[sfb]; |
442 | UnpackZeros(nVals, coef + offset); |
443 | } |
444 | coef += nVals; |
445 | coef += (icsInfo->winGroupLen[gp] - 1) * NSAMPS_SHORT; |
446 | } |
447 | |
448 | ASSERT(coef == psi->coef[ch] + NSAMPS_LONG, ERR_AAC_COFF_EXCEED_RANGE); |
449 | return ERR_AAC_NONE; |
450 | } |
451 |