1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
19 ------------------------------------------------------------------------------
24 Filename: pvmp3_decode_huff_cw.cpp
27 pvmp3_decode_huff_cw_tab0
28 pvmp3_decode_huff_cw_tab1
29 pvmp3_decode_huff_cw_tab2
30 pvmp3_decode_huff_cw_tab3
31 pvmp3_decode_huff_cw_tab5
32 pvmp3_decode_huff_cw_tab6
33 pvmp3_decode_huff_cw_tab7
34 pvmp3_decode_huff_cw_tab8
35 pvmp3_decode_huff_cw_tab9
36 pvmp3_decode_huff_cw_tab10
37 pvmp3_decode_huff_cw_tab11
38 pvmp3_decode_huff_cw_tab12
39 pvmp3_decode_huff_cw_tab13
40 pvmp3_decode_huff_cw_tab15
41 pvmp3_decode_huff_cw_tab16
42 pvmp3_decode_huff_cw_tab24
43 pvmp3_decode_huff_cw_tab32
44 pvmp3_decode_huff_cw_tab33
48 ------------------------------------------------------------------------------
54 ------------------------------------------------------------------------------
55 INPUT AND OUTPUT DEFINITIONS
58 BITS *pMainData = pointer to input mp3 Main data bit stream
62 cw = bit field extracted from a leaf entry of packed mp3 Huffman Tables
65 ------------------------------------------------------------------------------
68 These functions are used to decode huffman codewords from the input
69 bitstream using combined binary search and look-up table approach.
71 ------------------------------------------------------------------------------
75 ------------------------------------------------------------------------------
77 [1] ISO MPEG Audio Subgroup Software Simulation Group (1996)
78 ISO 13818-3 MPEG-2 Audio Decoder - Lower Sampling Frequency Extension
80 [2] Introduction to Algorithms,
81 Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest.
84 [3] "Selecting an Optimal Huffman Decoder for AAC",
85 Vladimir Z. Mesarovic, et al.
86 AES 111th Convention, September 21-24, 2001, New York, USA
88 ------------------------------------------------------------------------------
91 ------------------------------------------------------------------------------
95 /*----------------------------------------------------------------------------
97 ----------------------------------------------------------------------------*/
98 #include "pv_mp3dec_fxd_op.h"
99 #include "pvmp3_tables.h"
100 #include "pvmp3_getbits.h"
101 #include "pvmp3_decode_huff_cw.h"
103 /*----------------------------------------------------------------------------
105 ; Define module specific macros here
106 ----------------------------------------------------------------------------*/
109 /*----------------------------------------------------------------------------
111 ; Include all pre-processor statements here. Include conditional
112 ; compile variables also.
113 ----------------------------------------------------------------------------*/
116 /*----------------------------------------------------------------------------
117 ; LOCAL FUNCTION DEFINITIONS
118 ; Function Prototype declaration
119 ----------------------------------------------------------------------------*/
121 /*----------------------------------------------------------------------------
122 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
123 ; Variable declaration - defined here and used outside this module
124 ----------------------------------------------------------------------------*/
126 /*----------------------------------------------------------------------------
127 ; EXTERNAL FUNCTION REFERENCES
128 ; Declare functions defined elsewhere and referenced in this module
129 ----------------------------------------------------------------------------*/
131 /*----------------------------------------------------------------------------
132 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
133 ; Declare variables used in this module but defined elsewhere
134 ----------------------------------------------------------------------------*/
136 /*----------------------------------------------------------------------------
138 ----------------------------------------------------------------------------*/
140 uint16 pvmp3_decode_huff_cw_tab0(tmp3Bits *pMainData)
142 OSCL_UNUSED_ARG(pMainData);
147 /*----------------------------------------------------------------------------
149 ----------------------------------------------------------------------------*/
150 uint16 pvmp3_decode_huff_cw_tab1(tmp3Bits *pMainData)
155 tmp = getUpTo9bits(pMainData, 3); /* hufftable1 */
157 cw = *(huffTable_1 + tmp);
158 pMainData->usedBits -= (3 - (cw & 0xFF));
164 /*----------------------------------------------------------------------------
166 ----------------------------------------------------------------------------*/
167 uint16 pvmp3_decode_huff_cw_tab2(tmp3Bits *pMainData)
172 tmp = getUpTo9bits(pMainData, 6); /* huffTable_2,3 */
176 tmp = (tmp >> 3) - 1;
183 cw = *(huffTable_2 + tmp);
184 pMainData->usedBits -= (6 - (cw & 0xFF));
190 /*----------------------------------------------------------------------------
192 ----------------------------------------------------------------------------*/
193 uint16 pvmp3_decode_huff_cw_tab3(tmp3Bits *pMainData)
198 tmp = getUpTo9bits(pMainData, 6); /* huffTable_2,3 */
202 tmp = (tmp >> 3) - 1;
209 cw = *(huffTable_3 + tmp);
210 pMainData->usedBits -= (6 - (cw & 0xFF));
216 /*----------------------------------------------------------------------------
218 ----------------------------------------------------------------------------*/
219 uint16 pvmp3_decode_huff_cw_tab5(tmp3Bits *pMainData)
224 tmp = getUpTo9bits(pMainData, 8); /* huffTable_5 */
228 tmp = (tmp >> 5) - 1;
230 else if ((tmp >> 1) >= 2)
232 tmp = (tmp >> 1) - 2 + 7;
236 tmp = (tmp & 3) + 21;
239 cw = *(huffTable_5 + tmp);
240 pMainData->usedBits -= (8 - (cw & 0xFF));
245 /*----------------------------------------------------------------------------
247 ----------------------------------------------------------------------------*/
248 uint16 pvmp3_decode_huff_cw_tab6(tmp3Bits *pMainData)
253 tmp = getUpTo9bits(pMainData, 7); /* huffTable_6 */
256 tmp = (tmp >> 3) - 3;
260 tmp = (tmp >> 1) - 1 + 13;
267 cw = *(huffTable_6 + tmp);
268 pMainData->usedBits -= (7 - (cw & 0xFF));
274 /*----------------------------------------------------------------------------
276 ----------------------------------------------------------------------------*/
277 uint16 pvmp3_decode_huff_cw_tab7(tmp3Bits *pMainData)
282 tmp = getUpTo17bits(pMainData, 10); /* huffTable_7 */
285 tmp = (tmp >> 7) - 2;
287 else if ((tmp >> 4) >= 7)
289 tmp = (tmp >> 4) - 7 + 6;
291 else if ((tmp >> 1) >= 2)
293 tmp = (tmp >> 1) - 2 + 15;
297 tmp = (tmp & 3) + 69;
300 cw = *(huffTable_7 + tmp);
301 pMainData->usedBits -= (10 - (cw & 0xFF));
307 /*----------------------------------------------------------------------------
309 ----------------------------------------------------------------------------*/
310 uint16 pvmp3_decode_huff_cw_tab8(tmp3Bits *pMainData)
315 tmp = getUpTo17bits(pMainData, 11); /* huffTable_8 */
318 tmp = (tmp >> 7) - 2;
320 else if ((tmp >> 5) >= 5)
322 tmp = (tmp >> 5) - 5 + 14;
324 else if ((tmp >> 2) >= 3)
326 tmp = (tmp >> 2) - 3 + 17;
333 cw = *(huffTable_8 + tmp);
334 pMainData->usedBits -= (11 - (cw & 0xFF));
340 /*----------------------------------------------------------------------------
342 ----------------------------------------------------------------------------*/
343 uint16 pvmp3_decode_huff_cw_tab9(tmp3Bits *pMainData)
348 tmp = getUpTo9bits(pMainData, 9); /* huffTable_9 */
351 tmp = (tmp >> 5) - 5;
353 else if ((tmp >> 3) >= 6)
355 tmp = (tmp >> 3) - 6 + 11;
357 else if ((tmp >> 1) >= 4)
359 tmp = (tmp >> 1) - 4 + 25;
366 cw = *(huffTable_9 + tmp);
367 pMainData->usedBits -= (9 - (cw & 0xFF));
373 /*----------------------------------------------------------------------------
375 ----------------------------------------------------------------------------*/
376 uint16 pvmp3_decode_huff_cw_tab10(tmp3Bits *pMainData)
381 tmp = getUpTo17bits(pMainData, 11); /* huffTable_10 */
384 tmp = (tmp >> 10) - 1;
386 else if ((tmp >> 7) >= 3)
388 tmp = (tmp >> 7) - 3 + 1;
390 else if ((tmp >> 5) >= 8)
392 tmp = (tmp >> 5) - 8 + 6;
394 else if ((tmp >> 3) >= 18)
396 tmp = (tmp >> 3) - 18 + 10;
398 else if ((tmp >> 2) >= 24)
400 tmp = (tmp >> 2) - 24 + 24;
402 else if ((tmp >> 1) >= 12)
404 tmp = (tmp >> 1) - 12 + 36;
411 cw = *(huffTable_10 + tmp);
412 pMainData->usedBits -= (11 - (cw & 0xFF));
418 /*----------------------------------------------------------------------------
420 ----------------------------------------------------------------------------*/
421 uint16 pvmp3_decode_huff_cw_tab11(tmp3Bits *pMainData)
426 tmp = getUpTo17bits(pMainData, 11); /* huffTable_11 */
429 tmp = (tmp >> 8) - 3;
431 else if ((tmp >> 6) >= 7)
433 tmp = (tmp >> 6) - 7 + 5;
435 else if ((tmp >> 3) >= 32)
437 tmp = (tmp >> 3) - 32 + 10;
439 else if ((tmp >> 2) >= 10)
441 tmp = (tmp >> 2) - 10 + 34;
443 else if ((tmp >> 1) >= 8)
445 tmp = (tmp >> 1) - 8 + 88;
449 tmp = (tmp & 0xFF) + 100;
451 cw = *(huffTable_11 + tmp);
452 pMainData->usedBits -= (11 - (cw & 0xFF));
458 /*----------------------------------------------------------------------------
460 ----------------------------------------------------------------------------*/
461 uint16 pvmp3_decode_huff_cw_tab12(tmp3Bits *pMainData)
466 tmp = getUpTo17bits(pMainData, 10); /* huffTable_12 */
469 tmp = (tmp >> 7) - 5;
471 else if ((tmp >> 5) >= 12)
473 tmp = (tmp >> 5) - 12 + 3;
475 else if ((tmp >> 4) >= 17)
477 tmp = (tmp >> 4) - 17 + 11;
479 else if ((tmp >> 2) >= 32)
481 tmp = (tmp >> 2) - 32 + 18;
483 else if ((tmp >> 1) >= 16)
485 tmp = (tmp >> 1) - 16 + 54;
489 tmp = (tmp & 0x1F) + 102;
492 cw = *(huffTable_12 + tmp);
493 pMainData->usedBits -= (10 - (cw & 0xFF));
499 /*----------------------------------------------------------------------------
501 ----------------------------------------------------------------------------*/
502 uint16 pvmp3_decode_huff_cw_tab13(tmp3Bits *pMainData)
507 tmp = getNbits(pMainData, 19); /* huffTable_13 */
512 else if ((tmp >> 15) >= 4)
514 tmp = (tmp >> 15) - 4 + 1;
516 else if ((tmp >> 11) >= 32)
518 tmp = (tmp >> 11) - 32 + 5;
520 else if ((tmp >> 9) >= 64)
522 tmp = (tmp >> 9) - 64 + 37;
524 else if ((tmp >> 8) >= 64)
526 tmp = (tmp >> 8) - 64 + 101;
528 else if ((tmp >> 7) >= 64)
530 tmp = (tmp >> 7) - 64 + 165;
532 else if ((tmp >> 6) >= 32)
534 tmp = (tmp >> 6) - 32 + 229;
536 else if ((tmp >> 5) >= 32)
538 tmp = (tmp >> 5) - 32 + 325;
540 else if ((tmp >> 4) >= 32)
542 tmp = (tmp >> 4) - 32 + 357;
544 else if ((tmp >> 3) >= 32)
546 tmp = (tmp >> 3) - 32 + 389;
548 else if ((tmp >> 2) >= 2)
550 tmp = (tmp >> 2) - 2 + 421;
554 tmp = (tmp & 0x7) + 483;
557 cw = *(huffTable_13 + tmp);
558 pMainData->usedBits -= (19 - (cw & 0xFF));
564 /*----------------------------------------------------------------------------
566 ----------------------------------------------------------------------------*/
567 uint16 pvmp3_decode_huff_cw_tab15(tmp3Bits *pMainData)
572 tmp = getUpTo17bits(pMainData, 13); /* huffTable_15 */
573 if ((tmp >> 9) >= 10)
575 tmp = (tmp >> 9) - 10;
577 else if ((tmp >> 6) >= 39)
579 tmp = (tmp >> 6) - 39 + 6;
581 else if ((tmp >> 4) >= 62)
583 tmp = (tmp >> 4) - 62 + 47;
585 else if ((tmp >> 3) >= 60)
587 tmp = (tmp >> 3) - 60 + 141;
589 else if ((tmp >> 2) >= 64)
591 tmp = (tmp >> 2) - 64 + 205;
593 else if ((tmp >> 1) >= 32)
595 tmp = (tmp >> 1) - 32 + 261;
599 tmp = (tmp & 0x3f) + 357;
602 cw = *(huffTable_15 + tmp);
603 pMainData->usedBits -= (13 - (cw & 0xFF));
609 /*----------------------------------------------------------------------------
611 ----------------------------------------------------------------------------*/
612 uint16 pvmp3_decode_huff_cw_tab16(tmp3Bits *pMainData)
617 tmp = getUpTo17bits(pMainData, 17); /* huffTable_16 */
622 else if ((tmp >> 13) >= 4)
624 tmp = (tmp >> 13) - 4 + 1;
626 else if ((tmp >> 9) >= 38)
628 tmp = (tmp >> 9) - 38 + 5;
630 else if ((tmp >> 7) >= 94)
632 tmp = (tmp >> 7) - 94 + 31;
634 else if ((tmp >> 5) >= 214)
636 tmp = (tmp >> 5) - 214 + 89;
638 else if ((tmp >> 3) >= 704)
640 if ((tmp >> 4) >= 384)
642 tmp = (tmp >> 4) - 384 + 315;
646 tmp = (tmp >> 3) - 704 + 251;
649 else if ((tmp >> 8) >= 14)
651 tmp = (tmp >> 8) - 14 + 359;
653 else if ((tmp) >= 3456)
655 if ((tmp >> 2) >= 868)
657 tmp = (tmp >> 2) - 868 + 383;
661 tmp = (tmp) - 3456 + 367;
666 tmp = ((tmp >> 6) & 0x3f) + 411;
669 cw = *(huffTable_16 + tmp);
670 pMainData->usedBits -= (17 - (cw & 0xFF));
677 /*----------------------------------------------------------------------------
679 ----------------------------------------------------------------------------*/
680 uint16 pvmp3_decode_huff_cw_tab24(tmp3Bits *pMainData)
685 tmp = getUpTo17bits(pMainData, 12); /* huffTable_24 */
686 if ((tmp >> 6) >= 41)
688 tmp = (tmp >> 6) - 41;
690 else if ((tmp >> 3) >= 218)
692 tmp = (tmp >> 3) - 218 + 23;
694 else if ((tmp >> 2) >= 336)
696 tmp = (tmp >> 2) - 336 + 133;
698 else if ((tmp >> 1) >= 520)
700 tmp = (tmp >> 1) - 520 + 233;
702 else if ((tmp) >= 1024)
704 tmp = (tmp) - 1024 + 385;
706 else if ((tmp >> 1) >= 352)
710 tmp = (tmp >> 8) - 3 + 433;
714 tmp = (tmp >> 1) - 352 + 401;
719 tmp = ((tmp >> 4) & 0x3f) + 434;
722 cw = *(huffTable_24 + tmp);
723 pMainData->usedBits -= (12 - (cw & 0xFF));
729 /*----------------------------------------------------------------------------
731 ----------------------------------------------------------------------------*/
732 uint16 pvmp3_decode_huff_cw_tab32(tmp3Bits *pMainData)
734 uint32 tmp = getUpTo9bits(pMainData, 6); /* huffTable_32 */
737 pMainData->usedBits -= 5;
742 uint16 cw = *(huffTable_32 + (tmp & 0x1f));
743 pMainData->usedBits -= (6 - (cw & 0xFF));
751 uint16 pvmp3_decode_huff_cw_tab33(tmp3Bits *pMainData)
754 uint16 tmp = getUpTo9bits(pMainData, 4); /* huffTable_33 */
756 return((0x0f - tmp));