summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/aacdec/decode_huff_cw_binary.cpp
blob: 0d1561b5575352f2fbdd7aab71ff9b0072f9d880 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/* ------------------------------------------------------------------
 * Copyright (C) 1998-2009 PacketVideo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 * -------------------------------------------------------------------
 */
/*

 Pathname: ./src/decode_huff_cw_binary.c
 Funtions:
    decode_huff_cw_tab1
    decode_huff_cw_tab2
    decode_huff_cw_tab3
    decode_huff_cw_tab4
    decode_huff_cw_tab5
    decode_huff_cw_tab6
    decode_huff_cw_tab7
    decode_huff_cw_tab8
    decode_huff_cw_tab9
    decode_huff_cw_tab10
    decode_huff_cw_tab11
    decode_huff_cw_scl


------------------------------------------------------------------------------
 REVISION HISTORY

 Description: Updated per review comments
              (1) make cw sgined and change "if(cw&0x80000000)" to if(cw<0)
              (2)

 Description: Create specific functions for different huffman tables.


 Description: Added ( Int16) castings to eliminate several compiler warnings


 Description: Modified huffman tables to allocate int32 variables instead of
              int16, which lead to data missaligned for some compiler.
              Eliminated casting and unused variables


 Who:                                   Date:
 Description:

------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS

 Inputs:
    BITS          *pInputStream = pointer to input bit stream

 Local Stores/Buffers/Pointers Needed:


 Global Stores/Buffers/Pointers Needed:


 Outputs:
    idx = bit field extracted from a leaf entry of packed Huffman Tables

 Pointers and Buffers Modified:

 Local Stores Modified:

 Global Stores Modified:


------------------------------------------------------------------------------
 FUNCTION DESCRIPTION

   These functions are used to decode huffman codewords from the input
   bitstream using combined binary search and look-up table approach.

   First the codewords are grouped and the input symbol is determined
   which group it belongs. Then within that group, a look-up table is
   used to determine which codeword the symbol is.
   The table is created by ordering the codeword in the table according to their
   normalized shifted binary value, i.e., all the codewords are left
   shifted to meet the maximum codelength. Example, max codelength is
   10, the codeword with lenth 3 will left shift by 7.
   The binary values of after the shift are sorted.
   Then the sorted table is divided into several partition.
   At the VLC decoding period, input is read in at max codelenght.
   The partition is decided using if-else logic.
   Inside each partition, a look-up table is used to map the input value
   to a correct symbol. Table entries can appear to be repeated according
   to the humming distance between adjacent codewords.

------------------------------------------------------------------------------
 REQUIREMENTS


------------------------------------------------------------------------------
 REFERENCES

 (1) MPEG-2 NBC Audio Decoder
   "This software module was originally developed by AT&T, Dolby
   Laboratories, Fraunhofer Gesellschaft IIS in the course of development
   of the MPEG-2 NBC/MPEG-4 Audio standard ISO/IEC 13818-7, 14496-1,2 and
   3. This software module is an implementation of a part of one or more
   MPEG-2 NBC/MPEG-4 Audio tools as specified by the MPEG-2 NBC/MPEG-4
   Audio standard. ISO/IEC  gives users of the MPEG-2 NBC/MPEG-4 Audio
   standards free license to this software module or modifications thereof
   for use in hardware or software products claiming conformance to the
   MPEG-2 NBC/MPEG-4 Audio  standards. Those intending to use this software
   module in hardware or software products are advised that this use may
   infringe existing patents. The original developer of this software
   module and his/her company, the subsequent editors and their companies,
   and ISO/IEC have no liability for use of this software module or
   modifications thereof in an implementation. Copyright is not released
   for non MPEG-2 NBC/MPEG-4 Audio conforming products.The original
   developer retains full right to use the code for his/her own purpose,
   assign or donate the code to a third party and to inhibit third party
   from using the code for non MPEG-2 NBC/MPEG-4 Audio conforming products.
   This copyright notice must be included in all copies or derivative
   works."
   Copyright(c)1996.

 (2) Introduction to Algorithms,
     Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest.
     The MIT press, 1990

 (3) "Selecting an Optimal Huffman Decoder for AAC",
     Vladimir Z. Mesarovic, et al.
     AES 111th Convention, September 21-24, 2001, New York, USA

------------------------------------------------------------------------------
 PSEUDO-CODE


------------------------------------------------------------------------------
 RESOURCES USED
   When the code is written for a specific target processor the
     the resources used should be documented below.

 STACK USAGE:

 DATA MEMORY USED: x words

 PROGRAM MEMORY USED: x words

 CLOCK CYCLES:


------------------------------------------------------------------------------
*/


/*----------------------------------------------------------------------------
; INCLUDES
----------------------------------------------------------------------------*/
#include    "pv_audio_type_defs.h"
#include    "huffman.h"


/*----------------------------------------------------------------------------
; MACROS
; Define module specific macros here
----------------------------------------------------------------------------*/


/*----------------------------------------------------------------------------
; DEFINES
; Include all pre-processor statements here. Include conditional
; compile variables also.
----------------------------------------------------------------------------*/
#define    MAX_CW_LEN  (19)
#define    MASK_IDX    (0x1FF)
#define    MASK_RIGHT  (0xFE00)

#define    UPPER16      (16)
#define    MASK_LOW16   (0xFFFF)
/*----------------------------------------------------------------------------
; LOCAL FUNCTION DEFINITIONS
; Function Prototype declaration
----------------------------------------------------------------------------*/


/*----------------------------------------------------------------------------
; LOCAL STORE/BUFFER/POINTER DEFINITIONS
; Variable declaration - defined here and used outside this module
----------------------------------------------------------------------------*/


/*----------------------------------------------------------------------------
; EXTERNAL FUNCTION REFERENCES
; Declare functions defined elsewhere and referenced in this module
----------------------------------------------------------------------------*/


/*----------------------------------------------------------------------------
; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
; Declare variables used in this module but defined elsewhere
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
; FUNCTION CODE
----------------------------------------------------------------------------*/

Int decode_huff_cw_tab1(
    BITS          *pInputStream)
{
    Int32  tab;
    Int32  cw;

    cw  = get17_n_lessbits(
              11,
              pInputStream);
    if ((cw >> 10) == 0)
    {
        pInputStream->usedBits -= (11 - 1);
        return 40; /* idx is 40 */
    }
    else if ((cw >> 6) <= 23)
    {
        tab = (cw >> 6) - 16;
    }
    else if ((cw >> 4) <= 119)
    {
        tab = (cw >> 4) - 96 + 8;
    }
    else if ((cw >> 2) <= 503)
    {
        tab = (cw >> 2) - 480 + 32;
    }
    else
    {
        tab = cw - 2016 + 56;
    }

    tab = *(huff_tab1 + tab);

    pInputStream->usedBits -= (11 - (tab & MASK_LOW16));
    return ((Int)(tab >> UPPER16));
}


Int decode_huff_cw_tab2(
    BITS          *pInputStream)
{
    Int32  tab;
    Int32  cw;

    cw  = get9_n_lessbits(
              9,
              pInputStream);
    if ((cw >> 6) == 0)
    {
        pInputStream->usedBits -= (9 - 3); /* used 3 bits */
        return 40; /* idx is 40 */
    }
    else if ((cw >> 3) <= 49)
    {
        tab = (cw >> 3) - 8;
    }
    else if ((cw >> 2) <= 114)
    {
        tab = (cw >> 2) - 100 + 42;
    }
    else if ((cw >> 1) <= 248)
    {
        tab = (cw >> 1) - 230 + 57;
    }
    else
    {
        tab = cw - 498 + 76;
    }

    tab = *(huff_tab2 + tab);

    pInputStream->usedBits -= (9 - (tab & MASK_LOW16));
    return ((Int)(tab >> UPPER16));
}


Int decode_huff_cw_tab3(
    BITS          *pInputStream)
{
    Int32  tab;
    Int32  cw;

    cw  = get17_n_lessbits(
              16,
              pInputStream);
    if ((cw >> 15) == 0)
    {
        pInputStream->usedBits -= (16 - 1); /* used 1 bits */
        return 0; /* idx is 0 */
    }
    else if ((cw >> 10) <= 57)
    {
        tab = (cw >> 10) - 32;
    }
    else if ((cw >> 7) <= 500)
    {
        tab = (cw >> 7) - 464 + 26;
    }
    else if ((cw >> 6) <= 1016)
    {
        tab = (cw >> 6) - 1002 + 63;
    }
    else if ((cw >> 4) <= 4092)
    {
        tab = (cw >> 4) - 4068 + 78;
    }
    else
    {
        tab = cw - 65488 + 103;
    }

    tab = *(huff_tab3 + tab);

    pInputStream->usedBits -= (16 - (tab & MASK_LOW16));
    return ((Int)(tab >> UPPER16));
}


Int decode_huff_cw_tab4(
    BITS          *pInputStream)
{
    Int32  tab;
    Int32  cw;

    cw  = get17_n_lessbits(
              12,
              pInputStream);

    if ((cw >> 7) <= 25)
    {
        tab = (cw >> 7);
    }
    else if ((cw >> 4) <= 246)
    {
        tab = (cw >> 4) - 208 + 26;
    }
    else if ((cw >> 2) <= 1017)
    {
        tab = (cw >> 2) - 988 + 65;
    }
    else
    {
        tab = cw - 4072 + 95;
    }

    tab = *(huff_tab4 + tab);

    pInputStream->usedBits -= (12 - (tab & MASK_LOW16));
    return ((Int)(tab >> UPPER16));
}



Int decode_huff_cw_tab5(
    BITS          *pInputStream)
{
    Int32  tab;
    Int32  cw;

    cw  = get17_n_lessbits(
              13,
              pInputStream);

    if ((cw >> 12) == 0)
    {
        pInputStream->usedBits -= (13 - 1); /* used 1 bits */
        return 40; /* idx is 40 */
    }
    else if ((cw >> 8) <= 27)
    {
        tab = (cw >> 8) - 16;
    }
    else if ((cw >> 5) <= 243)
    {
        tab = (cw >> 5) - 224 + 12;
    }
    else if ((cw >> 3) <= 1011)
    {
        tab = (cw >> 3) - 976 + 32;
    }
    else if ((cw >> 2) <= 2041)
    {
        tab = (cw >> 2) - 2024 + 68;
    }
    else
    {
        tab = cw - 8168 + 86;
    }

    tab = *(huff_tab5 + tab);

    pInputStream->usedBits -= (13 - (tab & MASK_LOW16));
    return ((Int)(tab >> UPPER16));
}



Int decode_huff_cw_tab6(
    BITS          *pInputStream)
{
    Int32  tab;
    Int32  cw;

    cw  = get17_n_lessbits(
              11,
              pInputStream);

    if ((cw >> 7) <= 8)
    {
        tab = (cw >> 7);
    }
    else if ((cw >> 4) <= 116)
    {
        tab = (cw >> 4) - 72 + 9;
    }
    else if ((cw >> 2) <= 506)
    {
        tab = (cw >> 2) - 468 + 54;
    }
    else
    {
        tab = cw - 2028 + 93;
    }

    tab = *(huff_tab6 + tab);

    pInputStream->usedBits -= (11 - (tab & MASK_LOW16));
    return ((Int)(tab >> UPPER16));
}



Int decode_huff_cw_tab7(
    BITS          *pInputStream)
{
    Int32  tab;
    Int32  cw;

    cw  = get17_n_lessbits(
              12,
              pInputStream);

    if ((cw >> 11) == 0)
    {
        pInputStream->usedBits -= (12 - 1); /* used 1 bits */
        return 0; /* idx is 0 */
    }
    else if ((cw >> 6) <= 55)
    {
        tab = (cw >> 6) - 32;
    }
    else if ((cw >> 4) <= 243)
    {
        tab = (cw >> 4) - 224 + 24;
    }
    else if ((cw >> 2) <= 1018)
    {
        tab = (cw >> 2) - 976 + 44;
    }
    else
    {
        tab = cw - 4076 + 87;
    }

    tab = *(huff_tab7 + tab);

    pInputStream->usedBits -= (12 - (tab & MASK_LOW16));
    return ((Int)(tab >> UPPER16));
}



Int decode_huff_cw_tab8(
    BITS          *pInputStream)
{
    Int32  tab;
    Int32  cw;

    cw  = get17_n_lessbits(
              10,
              pInputStream);

    if ((cw >> 5) <= 20)
    {
        tab = (cw >> 5);
    }
    else if ((cw >> 3) <= 117)
    {
        tab = (cw >> 3) - 84 + 21;
    }
    else if ((cw >> 2) <= 250)
    {
        tab = (cw >> 2) - 236 + 55;
    }
    else
    {
        tab = cw - 1004 + 70;
    }

    tab = *(huff_tab8 + tab);

    pInputStream->usedBits -= (10 - (tab & MASK_LOW16));
    return ((Int)(tab >> UPPER16));
}


Int decode_huff_cw_tab9(
    BITS          *pInputStream)
{
    Int32  tab;
    Int32  cw;

    cw  = get17_n_lessbits(
              15,
              pInputStream);

    if ((cw >> 11) <= 12)
    {
        tab = (cw >> 11);
    }
    else if ((cw >> 8) <= 114)
    {
        tab = (cw >> 8) - 104 + 13;
    }
    else if ((cw >> 6) <= 486)
    {
        tab = (cw >> 6) - 460 + 24;
    }
    else if ((cw >> 5) <= 993)
    {
        tab = (cw >> 5) - 974 + 51;
    }
    else if ((cw >> 4) <= 2018)
    {
        tab = (cw >> 4) - 1988 + 71;
    }
    else if ((cw >> 3) <= 4075)
    {
        tab = (cw >> 3) - 4038 + 102;
    }
    else if ((cw >> 2) <= 8183)
    {
        tab = (cw >> 2) - 8152 + 140;
    }
    else
    {
        tab = cw - 32736 + 172;
    }

    tab = *(huff_tab9 + tab);

    pInputStream->usedBits -= (15 - (tab & MASK_LOW16));
    return ((Int)(tab >> UPPER16));
}


Int decode_huff_cw_tab10(
    BITS          *pInputStream)
{
    Int32  tab;
    Int32  cw;

    cw  = get17_n_lessbits(
              12,
              pInputStream);

    if ((cw >> 6) <= 41)
    {
        tab = (cw >> 6);
    }
    else if ((cw >> 5) <= 100)
    {
        tab = (cw >> 5) - 84 + 42;
    }
    else if ((cw >> 4) <= 226)
    {
        tab = (cw >> 4) - 202 + 59;
    }
    else if ((cw >> 3) <= 484)
    {
        tab = (cw >> 3) - 454 + 84;
    }
    else if ((cw >> 2) <= 1010)
    {
        tab = (cw >> 2) - 970 + 115;
    }
    else if ((cw >> 1) <= 2043)
    {
        tab = (cw >> 1) - 2022 + 156;
    }
    else
    {
        tab = cw - 4088 + 178;
    }

    tab = *(huff_tab10 + tab);

    pInputStream->usedBits -= (12 - (tab & MASK_LOW16));
    return ((Int)(tab >> UPPER16));
}


Int decode_huff_cw_tab11(
    BITS          *pInputStream)
{
    Int32  tab;
    Int32  cw;

    cw  = get17_n_lessbits(
              12,
              pInputStream);

    if ((cw >> 6) <= 26)
    {
        tab = (cw >> 6);
    }
    else if ((cw >> 5) <= 69)
    {
        tab = (cw >> 5) - 54 + 27;
    }
    else if ((cw >> 4) <= 198)
    {
        tab = (cw >> 4) - 140 + 43;
    }
    else if ((cw >> 3) <= 452)
    {
        tab = (cw >> 3) - 398 + 102;
    }
    else if ((cw >> 2) <= 1000)
    {
        tab = (cw >> 2) - 906 + 157;
    }
    else if ((cw >> 1) <= 2044)
    {
        tab = (cw >> 1) - 2002 + 252;
    }
    else
    {
        tab = cw - 4090 + 295;
    }

    tab = *(huff_tab11 + tab);

    pInputStream->usedBits -= (12 - (tab & MASK_LOW16));
    return ((Int)(tab >> UPPER16));
}


Int decode_huff_scl(
    BITS          *pInputStream)
{
    Int32  tab;
    Int32  cw;

    cw  = getbits(
              19,
              pInputStream);

    if ((cw >> 18) == 0)
    {
        pInputStream->usedBits -= (19 - 1); /* used 1 bits */
        return 60; /* idx is 60 */
    }
    else if ((cw >> 13) <= 59)
    {
        tab = (cw >> 13) - 32;
    }
    else if ((cw >> 10) <= 505)
    {
        tab = (cw >> 10) - 480 + 28;
    }
    else if ((cw >> 7) <= 4089)
    {
        tab = (cw >> 7) - 4048 + 54;
    }
    else if ((cw >> 5) <= 16377)
    {
        tab = (cw >> 5) - 16360 + 96;
    }
    else if ((cw >> 3) <= 65526)
    {
        tab = (cw >> 3) - 65512 + 114;
    }
    else if ((cw >> 1) <= 262120)
    {
        tab = (cw >> 1) - 262108 + 129;
    }
    else
    {
        tab = cw - 524242 + 142;
    }

    tab = *(huff_tab_scl + tab);

    pInputStream->usedBits -= (19 - (tab & MASK_LOW16));
    return ((Int)(tab >> UPPER16));
}