1 /* |
|
2 * Copyright (c) 2010 Ixonos Plc. |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - Initial contribution |
|
11 * |
|
12 * Contributors: |
|
13 * Ixonos Plc |
|
14 * |
|
15 * Description: |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 /************************************************************************** |
|
21 huffdec3.cpp - Huffman decoding and inverse scaling routines for AAC decoder. |
|
22 |
|
23 Author(s): Juha Ojanpera |
|
24 Copyright (c) 2000-2004 by Nokia Research Center, Speech and Audio Systems. |
|
25 *************************************************************************/ |
|
26 |
|
27 /*-- Projetc Headers. --*/ |
|
28 #include "tool2.h" |
|
29 #include "dec_huf.h" |
|
30 |
|
31 /************************************************************************** |
|
32 Title: DecodeSpectralCodeword |
|
33 |
|
34 Purpose: Decodes one spectral Huffman codeword from the bitstream. |
|
35 |
|
36 Usage: y = DecodeSpectralCodeword(huf_info, bs) |
|
37 |
|
38 Input: huf_info - Huffman codebook parameters |
|
39 bs - bitstream parameters |
|
40 |
|
41 Output: y - decoded Huffman index |
|
42 |
|
43 Author(s): Juha Ojanpera |
|
44 *************************************************************************/ |
|
45 |
|
46 INLINE uint32 |
|
47 DecodeSpectralCodeword(Huffman_DecInfo *huf_info, TBitStream *bs) |
|
48 { |
|
49 int16 len; |
|
50 int32 codeword, items; |
|
51 const Huffman_DecCode *huf_code; |
|
52 |
|
53 items = 1; |
|
54 huf_code = huf_info->huf; |
|
55 |
|
56 /*-- The first 5 LSB bits contain the length of codeword. --*/ |
|
57 codeword = (int32) BsGetBits(bs, (int16)(huf_code->huf_param & 31)); |
|
58 |
|
59 while(items < huf_info->cb_len && codeword != huf_code->codeword) |
|
60 { |
|
61 items++; |
|
62 huf_code++; |
|
63 |
|
64 len = (int16) (huf_code->huf_param & 31); |
|
65 if(len) |
|
66 { |
|
67 codeword <<= len; |
|
68 codeword |= BsGetBits(bs, len); |
|
69 } |
|
70 } |
|
71 |
|
72 /*-- Remove the length part from the decoded symbol. --*/ |
|
73 return (huf_code->huf_param >> 5); |
|
74 } |
|
75 |
|
76 const uint32 hCbBitMask[20] = |
|
77 {0x0, 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, |
|
78 0x1FFFL, 0x3FFFL, 0x7FFFL, 0xFFFFL, 0x1FFFFL, 0x3FFFFL, 0x7FFFFL}; |
|
79 |
|
80 /************************************************************************** |
|
81 Title: DecodeSfCodeword |
|
82 |
|
83 Purpose: Decodes one scalefactor Huffman codeword from the bitstream. |
|
84 |
|
85 Usage: y = DecodeCodeword(huf_info, bs) |
|
86 |
|
87 Input: huf_info - Huffman codebook parameters |
|
88 bs - bitstream parameters |
|
89 |
|
90 Output: y - decoded Huffman index |
|
91 |
|
92 Explanation: The Huffman parameters are packed as follows : |
|
93 |
|
94 bits 0 - 4 : Length of codeword |
|
95 5 - 23 : Huffman codeword |
|
96 24 - 31 : Huffman symbol |
|
97 |
|
98 Author(s): Juha Ojanpera |
|
99 *************************************************************************/ |
|
100 |
|
101 INLINE uint32 |
|
102 DecodeSfCodeword(Huffman_DecSfInfo *huf_info, TBitStream *bs) |
|
103 { |
|
104 #define SFCW_MASK ((1 << 19) - 1) |
|
105 #define EXTRACT_SFCW(x) ((x >> 5) & SFCW_MASK) |
|
106 int16 bitsLeft; |
|
107 uint32 lookaheadBits; |
|
108 int32 codeword, len, items; |
|
109 const uint32 *sf_param = huf_info->sf_param; |
|
110 |
|
111 items = 1; |
|
112 |
|
113 /*-- Maximum codeword length is 19 bits! --*/ |
|
114 lookaheadBits = BsLookAhead(bs, 19); |
|
115 bitsLeft = (int16) (19 - (*sf_param & 31)); |
|
116 codeword = lookaheadBits >> bitsLeft; |
|
117 |
|
118 while(items < huf_info->cb_len && codeword != (int32) EXTRACT_SFCW(*sf_param)) |
|
119 { |
|
120 items++; |
|
121 sf_param++; |
|
122 |
|
123 len = (int16) (*sf_param & 31); |
|
124 if(len) |
|
125 { |
|
126 bitsLeft -= len; |
|
127 codeword <<= len; |
|
128 codeword |= (lookaheadBits >> bitsLeft) & hCbBitMask[len]; |
|
129 } |
|
130 } |
|
131 |
|
132 BsSkipNBits(bs, 19 - bitsLeft); |
|
133 |
|
134 return (*sf_param >> 24); |
|
135 } |
|
136 |
|
137 uint32 |
|
138 GetHcb(Huffman_DecSfInfo *huf_info, TBitStream *bs) |
|
139 { |
|
140 return (DecodeSfCodeword(huf_info, bs)); |
|
141 } |
|
142 |
|
143 /************************************************************************** |
|
144 Title: huf_sfac |
|
145 |
|
146 Purpose: Reads scalefactors from the bitstream. |
|
147 |
|
148 Usage: y = huf_sfac(bs, cip, group, cb_map, global_gain, factors, max_sfb) |
|
149 |
|
150 Input: bs - bitstream parameters |
|
151 cip : |
|
152 info - block (long/short) parameters |
|
153 group - grouping info |
|
154 cb_map - codebook for each sfb |
|
155 global_gain - start value for the difference decoding of scalefactors |
|
156 max_sfb - max # of sfb's present in this channel |
|
157 |
|
158 Output: y - TRUE on success, FALSE otherwise |
|
159 cip : |
|
160 is_present - TRUE if IS stereo is used, FALSE otherwise |
|
161 |
|
162 Author(s): Juha Ojanpera |
|
163 *************************************************************************/ |
|
164 |
|
165 int16 |
|
166 huf_sfac(TBitStream *bs, TCh_Info *cip, uint8 *group, uint8 *cb_map, |
|
167 int16 global_gain, int16 *factors, uint8 max_sfb) |
|
168 { |
|
169 Huffman_DecSfInfo *sf_huf; |
|
170 int16 i, b, bb, n, is_pos; |
|
171 int16 noise_pcm_flag, noise_nrg; |
|
172 |
|
173 sf_huf = cip->sf_huf; |
|
174 |
|
175 noise_pcm_flag = 1; |
|
176 |
|
177 /* |
|
178 * Scale factors are dpcm relative to global gain, |
|
179 * intensity positions are dpcm relative to zero |
|
180 */ |
|
181 is_pos = 0; |
|
182 noise_nrg = (int16) (global_gain - NOISE_OFFSET); |
|
183 cip->is_present = cip->pns_present = FALSE; |
|
184 |
|
185 /*-- Get scale factors. --*/ |
|
186 n = cip->info->sfb_per_sbk[0]; |
|
187 for(b = 0, bb = 0; b < cip->info->nsbk; ) |
|
188 { |
|
189 b = *group++; |
|
190 for(i = 0; i < max_sfb; i++) |
|
191 { |
|
192 switch(cb_map[i]) |
|
193 { |
|
194 /*-- Zero book. --*/ |
|
195 case ZERO_HCB: |
|
196 factors[i] = 0; |
|
197 break; |
|
198 |
|
199 /*-- Invalid books. --*/ |
|
200 case BOOKSCL: |
|
201 return (FALSE); |
|
202 |
|
203 /*-- Intensity books. --*/ |
|
204 case INTENSITY_HCB: |
|
205 case INTENSITY_HCB2: |
|
206 cip->is_present = TRUE; |
|
207 is_pos = (int16) (is_pos + DecodeSfCodeword(sf_huf, bs) - MIDFAC); |
|
208 factors[i] = is_pos; |
|
209 break; |
|
210 |
|
211 /*-- Noise books. --*/ |
|
212 case NOISE_HCB: |
|
213 cip->pns_present = TRUE; |
|
214 if(noise_pcm_flag) |
|
215 { |
|
216 noise_pcm_flag = 0; |
|
217 noise_nrg = (int16) (noise_nrg + BsGetBits(bs, NOISE_PCM_BITS) - NOISE_PCM_OFFSET); |
|
218 } |
|
219 else |
|
220 noise_nrg = (int16) (noise_nrg + DecodeSfCodeword(sf_huf, bs) - MIDFAC); |
|
221 |
|
222 factors[i] = noise_nrg; |
|
223 break; |
|
224 |
|
225 /*-- Spectral books. --*/ |
|
226 default: |
|
227 global_gain = (int16) (global_gain + DecodeSfCodeword(sf_huf, bs) - MIDFAC); |
|
228 factors[i] = (int16) (global_gain & TEXP_MASK); |
|
229 break; |
|
230 } |
|
231 } |
|
232 |
|
233 /*-- Expand short block grouping. --*/ |
|
234 if(!cip->info->islong) |
|
235 for(bb++; bb < b; bb++) |
|
236 { |
|
237 COPY_MEMORY(factors + n, factors, n * sizeof(int16)); |
|
238 factors += n; |
|
239 } |
|
240 cb_map += n; factors += n; |
|
241 } |
|
242 |
|
243 return (TRUE); |
|
244 } |
|
245 |
|
246 /************************************************************************** |
|
247 Title: unpack_index... |
|
248 |
|
249 Purpose: Translates Huffman index into n-tuple spectral values. |
|
250 |
|
251 Usage: unpack_index(index, quant) |
|
252 |
|
253 Input: index - decoded Huffman index |
|
254 |
|
255 Output: quant - quantized spectral values |
|
256 |
|
257 Explanation: The index contains already the translated n-tuple spectral |
|
258 values due to computational reasons. The unpacking routines |
|
259 will only extract the codebook dependent bit fields within |
|
260 'index' to obtain the quantized values. |
|
261 |
|
262 Author(s): Juha Ojanpera |
|
263 *************************************************************************/ |
|
264 |
|
265 /* |
|
266 * 2-tuple, quantized values are unsigned. |
|
267 */ |
|
268 INLINE void |
|
269 unpack_index2noOff(uint32 index, int16 *quant) |
|
270 { |
|
271 quant[0] = (index >> 5) & 31; quant[1] = index & 31; |
|
272 } |
|
273 |
|
274 /* |
|
275 * 2-tuple, quantized values are signed. |
|
276 */ |
|
277 INLINE void |
|
278 unpack_index2Off(uint32 index, int16 *quant) |
|
279 { |
|
280 quant[0] = (index >> 4) & 7; if(index & 128) quant[0] = -quant[0]; |
|
281 quant[1] = index & 7; if(index & 8) quant[1] = -quant[1]; |
|
282 } |
|
283 |
|
284 /* |
|
285 * 4-tuple, quantized values are unsigned. |
|
286 */ |
|
287 INLINE void |
|
288 unpack_index4noOff(uint32 index, int16 *quant) |
|
289 { |
|
290 quant[0] = (index >> 6) & 3; quant[1] = (index >> 4) & 3; |
|
291 quant[2] = (index >> 2) & 3; quant[3] = index & 3; |
|
292 } |
|
293 |
|
294 /* |
|
295 * 4-tuple, quantized values are signed. |
|
296 */ |
|
297 INLINE void |
|
298 unpack_index4Off(uint32 index, int16 *quant) |
|
299 { |
|
300 quant[0] = (index >> 6) & 1; if(index & 128) quant[0] = -quant[0]; |
|
301 quant[1] = (index >> 4) & 1; if(index & 32) quant[1] = -quant[1]; |
|
302 quant[2] = (index >> 2) & 1; if(index & 8) quant[2] = -quant[2]; |
|
303 quant[3] = index & 1; if(index & 2) quant[3] = -quant[3]; |
|
304 } |
|
305 |
|
306 /* |
|
307 * Reads, at maximum, 2 sign bits from the bitstream. |
|
308 */ |
|
309 INLINE void |
|
310 get_sign_bits2(int16 *q, TBitStream *bs) |
|
311 { |
|
312 /*-- 1 signals negative, as in 2's complement. --*/ |
|
313 if(q[0]) { if(BsGetBits(bs, 1)) q[0] = -q[0]; } |
|
314 if(q[1]) { if(BsGetBits(bs, 1)) q[1] = -q[1]; } |
|
315 } |
|
316 |
|
317 /* |
|
318 * Reads, at maximum, 4 sign bits from the bitstream. |
|
319 */ |
|
320 INLINE void |
|
321 get_sign_bits4(int16 *q, TBitStream *bs) |
|
322 { |
|
323 /*-- 1 signals negative, as in 2's complement. --*/ |
|
324 if(q[0]) { if(BsGetBits(bs, 1)) q[0] = -q[0]; } |
|
325 if(q[1]) { if(BsGetBits(bs, 1)) q[1] = -q[1]; } |
|
326 if(q[2]) { if(BsGetBits(bs, 1)) q[2] = -q[2]; } |
|
327 if(q[3]) { if(BsGetBits(bs, 1)) q[3] = -q[3]; } |
|
328 } |
|
329 |
|
330 /************************************************************************** |
|
331 Title: getescape |
|
332 |
|
333 Purpose: Decodes escape sequences for codebook 11. |
|
334 |
|
335 Usage: y = getescape(bs, q) |
|
336 |
|
337 Input: bs - bitstream parameters |
|
338 q - decoded quantized value (0-16) |
|
339 |
|
340 Output: y - actual quantized value |
|
341 |
|
342 Author(s): Juha Ojanpera |
|
343 *************************************************************************/ |
|
344 |
|
345 INLINE int16 |
|
346 getescape(TBitStream *bs, int16 q) |
|
347 { |
|
348 int16 i; |
|
349 |
|
350 i = q; |
|
351 |
|
352 /*-- Value 16 is used to indicate that the actual value is escape coded. --*/ |
|
353 if(q == 16 || q == -16) |
|
354 { |
|
355 /* |
|
356 * The length of escape sequence, according to the AAC standard, |
|
357 * is less than 24 bits. |
|
358 */ |
|
359 for(i = 4; i < 24; i++) |
|
360 if(BsGetBits(bs, 1) == 0) |
|
361 break; |
|
362 |
|
363 i = (int16) (BsGetBits(bs, i) + (1 << i)); |
|
364 if(q < 0) |
|
365 i = -i; |
|
366 } |
|
367 |
|
368 return (i); |
|
369 } |
|
370 |
|
371 /************************************************************************** |
|
372 Title: huf_spec |
|
373 |
|
374 Purpose: Huffman decodes and inverse scales quantized spectral values. |
|
375 |
|
376 Usage: y = huf_spec(bs, info, nsect, sect, quant, huf, parseOnly) |
|
377 |
|
378 Input: bs - bitstream parameters |
|
379 info - block (long/short) parameters |
|
380 nsect - # of sections present in this channel |
|
381 sect - sectioning (codebook and length of section) info |
|
382 huf - Spectral Huffman tables |
|
383 parseOnly - 1 if bitstream need to be only parsed, 0 otherwise |
|
384 |
|
385 Output: y - # of spectral bins decoded |
|
386 quant - quantized spectral coefficients |
|
387 |
|
388 Author(s): Juha Ojanpera |
|
389 *************************************************************************/ |
|
390 |
|
391 int16 |
|
392 huf_spec(TBitStream *bs, CInfo *info, int16 nsect, uint8 *sect, int16 *quant, |
|
393 Huffman_DecInfo **huf, uint8 parseOnly) |
|
394 { |
|
395 uint32 temp; |
|
396 int16 i, j, k, stop, *qp; |
|
397 |
|
398 qp = quant; |
|
399 for(k = 0, i = nsect; i; i--, sect += 2) |
|
400 { |
|
401 register Huffman_DecInfo *huf_info; |
|
402 int16 table = sect[0]; |
|
403 int16 top = sect[1]; |
|
404 |
|
405 if(top > 200 || top < 1) goto error_exit; |
|
406 |
|
407 stop = info->bk_sfb_top[top - 1]; |
|
408 |
|
409 switch(table) |
|
410 { |
|
411 /*-- 4-tuple signed quantized coefficients. --*/ |
|
412 case 1: |
|
413 case 2: |
|
414 huf_info = huf[table - 1]; |
|
415 for (j = k; j < stop; j += 4, qp += 4) |
|
416 { |
|
417 temp = DecodeSpectralCodeword(huf_info, bs); |
|
418 if(!parseOnly) unpack_index4Off(temp, qp); |
|
419 } |
|
420 break; |
|
421 |
|
422 /*-- 4-tuple unsigned quantized coefficients. --*/ |
|
423 case 3: |
|
424 case 4: |
|
425 huf_info = huf[table - 1]; |
|
426 for (j = k; j < stop; j += 4, qp += 4) |
|
427 { |
|
428 temp = DecodeSpectralCodeword(huf_info, bs); |
|
429 unpack_index4noOff(temp, qp); |
|
430 get_sign_bits4(qp, bs); |
|
431 } |
|
432 break; |
|
433 |
|
434 /*-- 2-tuple unsigned quantized coefficients. --*/ |
|
435 case 5: |
|
436 case 6: |
|
437 huf_info = huf[table - 1]; |
|
438 for (j = k; j < stop; j += 2, qp += 2) |
|
439 { |
|
440 temp = DecodeSpectralCodeword(huf_info, bs); |
|
441 if(!parseOnly) unpack_index2Off(temp, qp); |
|
442 } |
|
443 break; |
|
444 |
|
445 /*-- 2-tuple signed quantized coefficients. --*/ |
|
446 case 7: |
|
447 case 8: |
|
448 case 9: |
|
449 case 10: |
|
450 huf_info = huf[table - 1]; |
|
451 for (j = k; j < stop; j += 2, qp += 2) |
|
452 { |
|
453 temp = DecodeSpectralCodeword(huf_info, bs); |
|
454 unpack_index2noOff(temp, qp); |
|
455 get_sign_bits2(qp, bs); |
|
456 } |
|
457 break; |
|
458 |
|
459 /*-- 2-tuple signed quantized coefficients (escape codebook). --*/ |
|
460 case 11: |
|
461 huf_info = huf[table - 1]; |
|
462 for (j = k; j < stop; j += 2, qp += 2) |
|
463 { |
|
464 temp = DecodeSpectralCodeword(huf_info, bs); |
|
465 unpack_index2noOff(temp, qp); |
|
466 get_sign_bits2(qp, bs); |
|
467 qp[0] = getescape(bs, qp[0]); |
|
468 qp[1] = getescape(bs, qp[1]); |
|
469 } |
|
470 break; |
|
471 |
|
472 default: |
|
473 if(stop > k) |
|
474 { |
|
475 if(!parseOnly) ZERO_MEMORY(qp, (stop - k) * sizeof(int16)); |
|
476 qp = quant + stop; |
|
477 } |
|
478 else goto error_exit; |
|
479 break; |
|
480 } |
|
481 k = stop; |
|
482 } |
|
483 |
|
484 if(!parseOnly) if(k < LN2) ZERO_MEMORY(quant + k, (LN2 - k) * sizeof(int16)); |
|
485 |
|
486 return (k); |
|
487 |
|
488 error_exit: |
|
489 |
|
490 return (0); |
|
491 } |
|