pvmp3_decode_huff_cw.cpp revision d9618f23226f46c752e56f712bc4b505117d8b4b
1/* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18/*
19------------------------------------------------------------------------------
20
21   PacketVideo Corp.
22   MP3 Decoder Library
23
24   Filename: pvmp3_decode_huff_cw.cpp
25
26 Funtions:
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
45
46     Date: 09/21/2007
47
48------------------------------------------------------------------------------
49 REVISION HISTORY
50
51
52 Description:
53
54------------------------------------------------------------------------------
55 INPUT AND OUTPUT DEFINITIONS
56
57 Inputs:
58    BITS          *pMainData = pointer to input mp3 Main data bit stream
59
60
61 Outputs:
62    cw = bit field extracted from a leaf entry of packed mp3 Huffman Tables
63
64
65------------------------------------------------------------------------------
66 FUNCTION DESCRIPTION
67
68   These functions are used to decode huffman codewords from the input
69   bitstream using combined binary search and look-up table approach.
70
71------------------------------------------------------------------------------
72 REQUIREMENTS
73
74
75------------------------------------------------------------------------------
76 REFERENCES
77 [1] ISO MPEG Audio Subgroup Software Simulation Group (1996)
78     ISO 13818-3 MPEG-2 Audio Decoder - Lower Sampling Frequency Extension
79
80 [2] Introduction to Algorithms,
81     Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest.
82     The MIT press, 1990
83
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
87
88------------------------------------------------------------------------------
89 PSEUDO-CODE
90
91------------------------------------------------------------------------------
92*/
93
94
95/*----------------------------------------------------------------------------
96; INCLUDES
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"
102
103/*----------------------------------------------------------------------------
104; MACROS
105; Define module specific macros here
106----------------------------------------------------------------------------*/
107
108
109/*----------------------------------------------------------------------------
110; DEFINES
111; Include all pre-processor statements here. Include conditional
112; compile variables also.
113----------------------------------------------------------------------------*/
114
115
116/*----------------------------------------------------------------------------
117; LOCAL FUNCTION DEFINITIONS
118; Function Prototype declaration
119----------------------------------------------------------------------------*/
120
121/*----------------------------------------------------------------------------
122; LOCAL STORE/BUFFER/POINTER DEFINITIONS
123; Variable declaration - defined here and used outside this module
124----------------------------------------------------------------------------*/
125
126/*----------------------------------------------------------------------------
127; EXTERNAL FUNCTION REFERENCES
128; Declare functions defined elsewhere and referenced in this module
129----------------------------------------------------------------------------*/
130
131/*----------------------------------------------------------------------------
132; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
133; Declare variables used in this module but defined elsewhere
134----------------------------------------------------------------------------*/
135
136/*----------------------------------------------------------------------------
137; FUNCTION CODE
138----------------------------------------------------------------------------*/
139
140uint16 pvmp3_decode_huff_cw_tab0(tmp3Bits *pMainData)
141{
142    OSCL_UNUSED_ARG(pMainData);
143    return(0);
144
145}
146
147/*----------------------------------------------------------------------------
148; FUNCTION CODE
149----------------------------------------------------------------------------*/
150uint16 pvmp3_decode_huff_cw_tab1(tmp3Bits *pMainData)
151{
152    uint32 tmp;
153    uint16 cw;
154
155    tmp = getUpTo9bits(pMainData, 3);    /*  hufftable1  */
156
157    cw = *(huffTable_1 + tmp);
158    pMainData->usedBits -= (3 - (cw & 0xFF));
159    return(cw >> 8);
160
161}
162
163
164/*----------------------------------------------------------------------------
165; FUNCTION CODE
166----------------------------------------------------------------------------*/
167uint16 pvmp3_decode_huff_cw_tab2(tmp3Bits *pMainData)
168{
169    uint32 tmp;
170    uint16 cw;
171
172    tmp = getUpTo9bits(pMainData, 6);    /*  huffTable_2,3  */
173
174    if (tmp >> 3)
175    {
176        tmp = (tmp >> 3) - 1;
177    }
178    else
179    {
180        tmp = tmp + 7;
181    }
182
183    cw = *(huffTable_2 + tmp);
184    pMainData->usedBits -= (6 - (cw & 0xFF));
185
186    return(cw >> 8);
187}
188
189
190/*----------------------------------------------------------------------------
191; FUNCTION CODE
192----------------------------------------------------------------------------*/
193uint16 pvmp3_decode_huff_cw_tab3(tmp3Bits *pMainData)
194{
195    uint32 tmp;
196    uint16 cw;
197
198    tmp = getUpTo9bits(pMainData, 6);    /*  huffTable_2,3  */
199
200    if (tmp >> 3)
201    {
202        tmp = (tmp >> 3) - 1;
203    }
204    else
205    {
206        tmp = tmp + 7;
207    }
208
209    cw = *(huffTable_3 + tmp);
210    pMainData->usedBits -= (6 - (cw & 0xFF));
211
212    return(cw >> 8);
213}
214
215
216/*----------------------------------------------------------------------------
217; FUNCTION CODE
218----------------------------------------------------------------------------*/
219uint16 pvmp3_decode_huff_cw_tab5(tmp3Bits *pMainData)
220{
221    uint32 tmp;
222    uint16 cw;
223
224    tmp = getUpTo9bits(pMainData, 8);    /*  huffTable_5  */
225
226    if ((tmp >> 5))
227    {
228        tmp = (tmp >> 5) - 1;
229    }
230    else if ((tmp >> 1) >= 2)
231    {
232        tmp = (tmp >> 1) - 2 + 7;
233    }
234    else
235    {
236        tmp = (tmp & 3) + 21;
237    }
238
239    cw = *(huffTable_5 + tmp);
240    pMainData->usedBits -= (8 - (cw & 0xFF));
241
242    return(cw >> 8);
243}
244
245/*----------------------------------------------------------------------------
246; FUNCTION CODE
247----------------------------------------------------------------------------*/
248uint16 pvmp3_decode_huff_cw_tab6(tmp3Bits *pMainData)
249{
250    uint32 tmp;
251    uint16 cw;
252
253    tmp = getUpTo9bits(pMainData, 7);    /*  huffTable_6  */
254    if ((tmp >> 3) >= 3)
255    {
256        tmp = (tmp >> 3) - 3;
257    }
258    else if (tmp >> 1)
259    {
260        tmp = (tmp >> 1) - 1 + 13;
261    }
262    else
263    {
264        tmp = tmp + 24;
265    }
266
267    cw = *(huffTable_6 + tmp);
268    pMainData->usedBits -= (7 - (cw & 0xFF));
269
270    return(cw >> 8);
271}
272
273
274/*----------------------------------------------------------------------------
275; FUNCTION CODE
276----------------------------------------------------------------------------*/
277uint16 pvmp3_decode_huff_cw_tab7(tmp3Bits *pMainData)
278{
279    uint32 tmp;
280    uint16 cw;
281
282    tmp = getUpTo17bits(pMainData, 10);    /*  huffTable_7  */
283    if ((tmp >> 7) >= 2)
284    {
285        tmp = (tmp >> 7) - 2;
286    }
287    else if ((tmp >> 4) >= 7)
288    {
289        tmp = (tmp >> 4) - 7 + 6;
290    }
291    else if ((tmp >> 1) >=  2)
292    {
293        tmp = (tmp >> 1) - 2 + 15;
294    }
295    else
296    {
297        tmp = (tmp & 3) + 69;
298    }
299
300    cw = *(huffTable_7 + tmp);
301    pMainData->usedBits -= (10 - (cw & 0xFF));
302
303    return(cw >> 8);
304}
305
306
307/*----------------------------------------------------------------------------
308; FUNCTION CODE
309----------------------------------------------------------------------------*/
310uint16 pvmp3_decode_huff_cw_tab8(tmp3Bits *pMainData)
311{
312    uint32 tmp;
313    uint16 cw;
314
315    tmp = getUpTo17bits(pMainData, 11);    /*  huffTable_8  */
316    if ((tmp >> 7) >= 2)
317    {
318        tmp = (tmp >> 7) - 2;
319    }
320    else if ((tmp >> 5) >= 5)
321    {
322        tmp = (tmp >> 5) - 5 + 14;
323    }
324    else if ((tmp >> 2) >= 3)
325    {
326        tmp = (tmp >> 2) - 3 + 17;
327    }
328    else
329    {
330        tmp = (tmp) + 54;
331    }
332
333    cw = *(huffTable_8 + tmp);
334    pMainData->usedBits -= (11 - (cw & 0xFF));
335
336    return(cw >> 8);
337}
338
339
340/*----------------------------------------------------------------------------
341; FUNCTION CODE
342----------------------------------------------------------------------------*/
343uint16 pvmp3_decode_huff_cw_tab9(tmp3Bits *pMainData)
344{
345    uint32 tmp;
346    uint16 cw;
347
348    tmp = getUpTo9bits(pMainData, 9);    /*  huffTable_9  */
349    if ((tmp >> 5) >= 5)
350    {
351        tmp = (tmp >> 5) - 5;
352    }
353    else if ((tmp >> 3) >= 6)
354    {
355        tmp = (tmp >> 3) - 6 + 11;
356    }
357    else if ((tmp >> 1) >= 4)
358    {
359        tmp = (tmp >> 1) - 4 + 25;
360    }
361    else
362    {
363        tmp = tmp + 45;
364    }
365
366    cw = *(huffTable_9 + tmp);
367    pMainData->usedBits -= (9 - (cw & 0xFF));
368
369    return(cw >> 8);
370}
371
372
373/*----------------------------------------------------------------------------
374; FUNCTION CODE
375----------------------------------------------------------------------------*/
376uint16 pvmp3_decode_huff_cw_tab10(tmp3Bits *pMainData)
377{
378    uint32 tmp;
379    uint16 cw;
380
381    tmp = getUpTo17bits(pMainData, 11);    /*  huffTable_10  */
382    if (tmp >> 10)
383    {
384        tmp = (tmp >> 10) - 1;
385    }
386    else if ((tmp >> 7) >= 3)
387    {
388        tmp = (tmp >> 7) - 3 + 1;
389    }
390    else if ((tmp >> 5) >= 8)
391    {
392        tmp = (tmp >> 5) - 8 + 6;
393    }
394    else if ((tmp >> 3) >= 18)
395    {
396        tmp = (tmp >> 3) - 18 + 10;
397    }
398    else if ((tmp >> 2) >= 24)
399    {
400        tmp = (tmp >> 2) - 24 + 24;
401    }
402    else if ((tmp >> 1) >= 12)
403    {
404        tmp = (tmp >> 1) - 12 + 36;
405    }
406    else
407    {
408        tmp = (tmp) + 72;
409    }
410
411    cw = *(huffTable_10 + tmp);
412    pMainData->usedBits -= (11 - (cw & 0xFF));
413
414    return(cw >> 8);
415}
416
417
418/*----------------------------------------------------------------------------
419; FUNCTION CODE
420----------------------------------------------------------------------------*/
421uint16 pvmp3_decode_huff_cw_tab11(tmp3Bits *pMainData)
422{
423    uint32 tmp;
424    uint16 cw;
425
426    tmp = getUpTo17bits(pMainData, 11);    /*  huffTable_11  */
427    if ((tmp >> 8) >= 3)
428    {
429        tmp = (tmp >> 8) - 3;
430    }
431    else if ((tmp >> 6) >= 7)
432    {
433        tmp = (tmp >> 6) - 7 + 5;
434    }
435    else if ((tmp >> 3) >= 32)
436    {
437        tmp = (tmp >> 3) - 32 + 10;
438    }
439    else if ((tmp >> 2) >= 10)
440    {
441        tmp = (tmp >> 2) - 10 + 34;
442    }
443    else if ((tmp >> 1) >= 8)
444    {
445        tmp = (tmp >> 1) - 8 + 88;
446    }
447    else
448    {
449        tmp = (tmp & 0xFF) + 100;
450    }
451    cw = *(huffTable_11 + tmp);
452    pMainData->usedBits -= (11 - (cw & 0xFF));
453
454    return(cw >> 8);
455}
456
457
458/*----------------------------------------------------------------------------
459; FUNCTION CODE
460----------------------------------------------------------------------------*/
461uint16 pvmp3_decode_huff_cw_tab12(tmp3Bits *pMainData)
462{
463    uint32 tmp;
464    uint16 cw;
465
466    tmp = getUpTo17bits(pMainData, 10);    /*  huffTable_12  */
467    if ((tmp >> 7) >= 5)
468    {
469        tmp = (tmp >> 7) - 5;
470    }
471    else if ((tmp >> 5) >= 12)
472    {
473        tmp = (tmp >> 5) - 12 + 3;
474    }
475    else if ((tmp >> 4) >= 17)
476    {
477        tmp = (tmp >> 4) - 17 + 11;
478    }
479    else if ((tmp >> 2) >= 32)
480    {
481        tmp = (tmp >> 2) - 32 + 18;
482    }
483    else if ((tmp >> 1) >= 16)
484    {
485        tmp = (tmp >> 1) - 16 + 54;
486    }
487    else
488    {
489        tmp = (tmp & 0x1F) + 102;
490
491    }
492    cw = *(huffTable_12 + tmp);
493    pMainData->usedBits -= (10 - (cw & 0xFF));
494
495    return(cw >> 8);
496}
497
498
499/*----------------------------------------------------------------------------
500; FUNCTION CODE
501----------------------------------------------------------------------------*/
502uint16 pvmp3_decode_huff_cw_tab13(tmp3Bits *pMainData)
503{
504    uint32 tmp;
505    uint16 cw;
506
507    tmp = getNbits(pMainData, 19);    /*  huffTable_13  */
508    if (tmp >> 18)
509    {
510        tmp = 0;
511    }
512    else if ((tmp >> 15) >= 4)
513    {
514        tmp = (tmp >> 15) - 4 + 1;
515    }
516    else if ((tmp >> 11) >= 32)
517    {
518        tmp = (tmp >> 11) - 32 + 5;
519    }
520    else if ((tmp >> 9) >= 64)
521    {
522        tmp = (tmp >> 9) - 64 + 37;
523    }
524    else if ((tmp >> 8) >= 64)
525    {
526        tmp = (tmp >> 8) - 64 + 101;
527    }
528    else if ((tmp >> 7) >= 64)
529    {
530        tmp = (tmp >> 7) - 64 + 165;
531    }
532    else if ((tmp >> 6) >= 32)
533    {
534        tmp = (tmp >> 6) - 32 + 229;
535    }
536    else if ((tmp >> 5) >= 32)
537    {
538        tmp = (tmp >> 5) - 32 + 325;
539    }
540    else if ((tmp >> 4) >= 32)
541    {
542        tmp = (tmp >> 4) - 32 + 357;
543    }
544    else if ((tmp >> 3) >= 32)
545    {
546        tmp = (tmp >> 3) - 32 + 389;
547    }
548    else if ((tmp >> 2) >= 2)
549    {
550        tmp = (tmp >> 2) - 2 + 421;
551    }
552    else
553    {
554        tmp = (tmp & 0x7) + 483;
555    }
556
557    cw = *(huffTable_13 + tmp);
558    pMainData->usedBits -= (19 - (cw & 0xFF));
559
560    return(cw >> 8);
561}
562
563
564/*----------------------------------------------------------------------------
565; FUNCTION CODE
566----------------------------------------------------------------------------*/
567uint16 pvmp3_decode_huff_cw_tab15(tmp3Bits *pMainData)
568{
569    uint32 tmp;
570    uint16 cw;
571
572    tmp = getUpTo17bits(pMainData, 13);    /*  huffTable_15  */
573    if ((tmp >> 9) >= 10)
574    {
575        tmp = (tmp >> 9) - 10;
576    }
577    else if ((tmp >> 6) >= 39)
578    {
579        tmp = (tmp >> 6) - 39 + 6;
580    }
581    else if ((tmp >> 4) >= 62)
582    {
583        tmp = (tmp >> 4) - 62 + 47;
584    }
585    else if ((tmp >> 3) >= 60)
586    {
587        tmp = (tmp >> 3) - 60 + 141;
588    }
589    else if ((tmp >> 2) >= 64)
590    {
591        tmp = (tmp >> 2) - 64 + 205;
592    }
593    else if ((tmp >> 1) >= 32)
594    {
595        tmp = (tmp >> 1) - 32 + 261;
596    }
597    else
598    {
599        tmp = (tmp & 0x3f) + 357;
600    }
601
602    cw = *(huffTable_15 + tmp);
603    pMainData->usedBits -= (13 - (cw & 0xFF));
604
605    return(cw >> 8);
606}
607
608
609/*----------------------------------------------------------------------------
610; FUNCTION CODE
611----------------------------------------------------------------------------*/
612uint16 pvmp3_decode_huff_cw_tab16(tmp3Bits *pMainData)
613{
614    uint32 tmp;
615    uint16 cw;
616
617    tmp = getUpTo17bits(pMainData, 17);    /*  huffTable_16  */
618    if (tmp >> 16)
619    {
620        tmp = 0;
621    }
622    else if ((tmp >> 13) >= 4)
623    {
624        tmp = (tmp >> 13) - 4 + 1;
625    }
626    else if ((tmp >> 9) >= 38)
627    {
628        tmp = (tmp >> 9) - 38 + 5;
629    }
630    else if ((tmp >> 7) >= 94)
631    {
632        tmp = (tmp >> 7) - 94 + 31;
633    }
634    else if ((tmp >> 5) >= 214)
635    {
636        tmp = (tmp >> 5) - 214 + 89;
637    }
638    else if ((tmp >> 3) >= 704)
639    {
640        if ((tmp >> 4) >= 384)
641        {
642            tmp = (tmp >> 4) - 384 + 315;
643        }
644        else
645        {
646            tmp = (tmp >> 3) - 704 + 251;
647        }
648    }
649    else if ((tmp >> 8) >= 14)
650    {
651        tmp = (tmp >> 8) - 14 + 359;
652    }
653    else if ((tmp) >= 3456)
654    {
655        if ((tmp >> 2) >= 868)
656        {
657            tmp = (tmp >> 2) - 868 + 383;
658        }
659        else
660        {
661            tmp = (tmp) - 3456 + 367;
662        }
663    }
664    else
665    {
666        tmp = ((tmp >> 6) & 0x3f) + 411;
667    }
668
669    cw = *(huffTable_16 + tmp);
670    pMainData->usedBits -= (17 - (cw & 0xFF));
671
672    return(cw >> 8);
673}
674
675
676
677/*----------------------------------------------------------------------------
678; FUNCTION CODE
679----------------------------------------------------------------------------*/
680uint16 pvmp3_decode_huff_cw_tab24(tmp3Bits *pMainData)
681{
682    uint32 tmp;
683    uint16 cw;
684
685    tmp = getUpTo17bits(pMainData, 12);    /*  huffTable_24  */
686    if ((tmp >> 6) >= 41)
687    {
688        tmp = (tmp >> 6) - 41;
689    }
690    else if ((tmp >> 3) >= 218)
691    {
692        tmp = (tmp >> 3) - 218 + 23;
693    }
694    else if ((tmp >> 2) >= 336)
695    {
696        tmp = (tmp >> 2) - 336 + 133;
697    }
698    else if ((tmp >> 1) >= 520)
699    {
700        tmp = (tmp >> 1) - 520 + 233;
701    }
702    else if ((tmp) >= 1024)
703    {
704        tmp = (tmp) - 1024 + 385;
705    }
706    else if ((tmp >> 1) >= 352)
707    {
708        if ((tmp >> 8) == 3)
709        {
710            tmp = (tmp >> 8) - 3 + 433;
711        }
712        else
713        {
714            tmp = (tmp >> 1) - 352 + 401;
715        }
716    }
717    else
718    {
719        tmp = ((tmp >> 4) & 0x3f) + 434;
720    }
721
722    cw = *(huffTable_24 + tmp);
723    pMainData->usedBits -= (12 - (cw & 0xFF));
724
725    return(cw >> 8);
726}
727
728
729/*----------------------------------------------------------------------------
730; FUNCTION CODE
731----------------------------------------------------------------------------*/
732uint16 pvmp3_decode_huff_cw_tab32(tmp3Bits *pMainData)
733{
734    uint32 tmp = getUpTo9bits(pMainData, 6);    /*  huffTable_32  */
735    if ((tmp >> 5))
736    {
737        pMainData->usedBits -= 5;
738        return(0);
739    }
740    else
741    {
742        uint16 cw = *(huffTable_32 + (tmp & 0x1f));
743        pMainData->usedBits -= (6 - (cw & 0xFF));
744
745        return(cw >> 8);
746    }
747
748}
749
750
751uint16 pvmp3_decode_huff_cw_tab33(tmp3Bits *pMainData)
752{
753
754    uint16 tmp = getUpTo9bits(pMainData, 4);    /*  huffTable_33  */
755
756    return((0x0f - tmp));
757}
758
759