summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/aacdec/huffspec_fxp.cpp
blob: b18c12d5a391cd66bb186788341ba895abb89f91 (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
/* ------------------------------------------------------------------
 * 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:  huffspec_fxp.c
 Funtions:
    huffspec_fxp

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

 Description:  Modified from original shareware code

 Description:  Modified to pass variables by reference to eliminate use
               of global variables.

 Description: (1) Modified to keep in-line with PV standards
              (2) Eliminated "continue" in if(sect_cb==ZERO_HCB||...)

 Description: (1) Use SectInfo *pSect
              (2) Convert 'Real' to 'Int32', float -> fixed-point
              (3) move BITS *pInputStream to second parameter
              (4) pass in quantSpec and tmp_spec, scratch shared with hufffac
              (5) pass in FrameInfo *pLongFrameInfo, eliminate only_long_info

 Description: (1) Eliminate parameter Hcb *book, because of eliminating
                  function 'hufftab.c', Hcb hcbbook defined as a
                  const structure in 'hcbtables.h'.
              (2) Replace three nested 'for' loops with a for-while loop in
                  the rescaling part.
              (3) Change esc_iquant-> esc_iquant_fxp, call esc_iquant_fxp()
                  by sfb

 Description: Cleaned up include files.

 Description:  Correct definition of stack variable "scale".
        It was defined as Int, but it receives an UInt value,
        this present a problem when Int is 16 bits and
        the sign bit is not interpreted correctly. This does not
        shows for 32-bit implementations. This problem manifest itself
        as a flipping sign on some spectral coefficients (the ones
        multiplied by 0x8000).

 Description: Typecast b_low and b_high to 32-bits before multiplication, this
              assures propoer compilation on a 16-bit platform (TI-C55x)

 Description: Modified to speed up decode_huff_cw

 Description: pass codebook index to decode_huff_cw, delete pointer to Huffman
              structure

 Description: keep memset to quantSpec, remove memset to temp_spec

 Description: Modified per review comments

 Description: Use Binary tree search in decode_huff_cw_binary

 Description: Modified per review comments
              (1) delete unused codes

 Description: (1) Change the interface to decode huffman codeword.
              (2) Move the scaling inside the inverse quantization.
              (3) Change scaling factor accuracy to 10 bits.

 Description:
              (1) delete unused variable max_fac

 Description: Addresses of huffman tables are now found by means of a
              switch statement, this solve linking problem when using the
              /ropi option (Read-only position independent) for some
              compilers

 Who:                                   Date: MM/DD/YYYY
 Description:
------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS

 Inputs:
    pFrameInfo  = ptr to structure that holds Information of current Frame,
                  type FrameInfo

    pInputStream = ptr to structure of bitstream, type BITS

    nsect       = number of sections in current Frame, at fs = 44.1 kHz,
                  range [0, 49] long block, [0,112] short blocks. type Int

    pSect       = ptr to structure that holds section codebook and boundary
                  type SectInfo

    factors[]   = array that contains scalefactors for each sfb, type Int16

    coef[]      = array that holds inverse quantized coefs, Int32 QFormat.

    quantSpec[] = array that holds quantized spectral coefs, type Int

    tmp_spec[]  = temporary buffer to hold the de-interleaved coefs.

    pLongFrameInfo = ptr to structure that holds long frame info

 Local Stores/Buffers/Pointers Needed:
    exptable = array contains the Q15 format data for 2^0, 2^0.25, 2^0.5,
               and 2^0.75, type const Int.

 Global Stores/Buffers/Pointers Needed:
    None

 Outputs:

    return 0 if decoding properly.

 Pointers and Buffers Modified:

    pInputStream    read codeword index and/or sign bits and/or ESC value

    coef            contains the newly inverse quantized 1024 spec coefs,
                    type Int32 Q-format from esc_iquant()

    quantSpec       contains decoded quantized 1024 spec coefs, type Int

    tmp_spec        contains the de-interleaved version of quantSpec

    qFormat         contains Q-Format for each scalefactor band

 Local Stores Modified:
    None

 Global Stores Modified:
    None

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

 This function first reads the section info (codebook and boundary), then
 decode the spectral coefficients if a spectral codebook is used.
 If necessary, get the sign bits, ESC value or the NEC_pulse data. In case of
 short window sequences, the decoded data is de-interleaved before
 multiplied by scalefactors.

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

 This function should set the content of the array 'coef' with the inverse
 quantized and rescaled value of spectral coefficients.

------------------------------------------------------------------------------
 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) ISO/IEC 14496-3: 1999(E)
    Subpart (4)         p56 (spectral_data() parsing and decoding)
                        p26 (Syntax of spectral_data())
                        p74-78 (decoding: unpack_idx, get_sign_bits,
                                getescape, pulse_nc, deinterleave)
                        p72 (inverse quantization: esc_iquant)

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


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

 STACK USAGE: [stack count for this module] + [variable to represent
          stack usage for each subroutine called]

     where: [stack usage variable] = stack usage for [subroutine
         name] (see [filename].ext)

 DATA MEMORY USED: x words

 PROGRAM MEMORY USED: x words

 CLOCK CYCLES: [cycle count equation for this module] + [variable
           used to represent cycle count for each subroutine
           called]

     where: [cycle count variable] = cycle count for [subroutine
        name] (see [filename].ext)

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

/*----------------------------------------------------------------------------
; INCLUDES
----------------------------------------------------------------------------*/
#include    "pv_audio_type_defs.h"
#include    "aac_mem_funcs.h"
#include    "esc_iquant_scaling.h"
#include    "huffman.h"
#include    "unpack_idx.h"
#include    "pulse_nc.h"
#include    "iquant_table.h"
#include    "e_huffmanconst.h"


#include "pv_normalize.h"

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


/*----------------------------------------------------------------------------
; DEFINES
; Include all pre-processor statements here. Include conditional
; compile variables also.
----------------------------------------------------------------------------*/
#define ORDER        (3)

/*
 * Format the table is stored in.
 */
#define QTABLE       (27)

/*
 * Number of bits for data in a signed 32 bit integer.
 */
#define SIGNED32BITS  (31)

/*
 * Round up value for intermediate values obtained from the table
 */
#define ROUND_UP (( ((UInt32) 1) << (QTABLE) )-1)

/*----------------------------------------------------------------------------
; LOCAL FUNCTION DEFINITIONS
; Function Prototype declaration
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
; LOCAL STORE/BUFFER/POINTER DEFINITIONS
; Variable declaration - defined here and used outside this module
----------------------------------------------------------------------------*/
const UInt16 exptable[4] =
{
    0,  /* (2^0.00)<<15 (Q10), use zero to signal no scaling required! */
    19485,  /* (2^0.25)<<15 */
    23171,  /* (2^0.50)<<15 */
    27555   /* (2^0.75)<<15 */

};


/*----------------------------------------------------------------------------
; 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 huffspec_fxp(
    FrameInfo *pFrameInfo,
    BITS      *pInputStream,
    Int       nsect,
    SectInfo  *pSectInfo,
    Int       factors[],
    Int32     coef[],
    Int16     quantSpec[],
    Int16     tmp_spec[],
    const FrameInfo  *pLongFrameInfo,
    PulseInfo  *pPulseInfo,
    Int         qFormat[])
{
    /*----------------------------------------------------------------------------
    ; Define all local variables
    ----------------------------------------------------------------------------*/
    const Hcb       *pHcb;
    Int     i;
    Int     sfb;
    Int     idx_count;
    Int     sect_cb;    /* section codebook */
    Int     dim;
    Int     idx;
    Int     stop_idx;     /* index of 1st coef in next sfb */
    Int     sect_start;   /* start index of sfb in one section*/
    Int     sect_end;     /* index of 1st sfb in next section */
    Int     *pSfbStart;
    Int     *pSfb;
    Int16     *pQuantSpec;        /* probably could be short */
    Int     max = 0;
    /* rescaling parameters */
    Int     nsfb;
    Int     tot_sfb;
    Int     fac;

    Int32   *pCoef; /* ptr to coef[], inverse quantized coefs */
    UInt16     scale;

    Int     power_scale_div_4;
    Int     sfbWidth;

    void (*pUnpack_idx)(
        Int16  quant_spec[],
        Int  codeword_indx,
        const Hcb *pHuffCodebook,
        BITS  *pInputStream,
        Int *max);

    Int(*pDec_huff_tab)(BITS *) = NULL;

    UInt32 temp;
    Int    binaryDigits, QFormat;

    /*----------------------------------------------------------------------------
    ; Function body here
    ----------------------------------------------------------------------------*/

    sect_start = 0;
    stop_idx   = 0;

    /* pSfb: ptr to array that holds stop index of each sfb */
    pSfbStart = pFrameInfo->frame_sfb_top;

    if (pSfbStart == NULL)
    {
        return (-1);   /*  error condition */
    }

    pSfb      = pSfbStart;

    /* decoding spectral values section by section */
    for (i = nsect; i > 0; i--)
    {
        /* read the codebook and section length */
        sect_cb  =  pSectInfo->sect_cb;     /* codebook */
        if ((sect_cb > 15) || (sect_cb < 0))
        {
            return (-1);   /*  error condition */
        }
        sect_end =  pSectInfo->sect_end;    /* # of sfbs */

        if (sect_end < 0)
        {
            return (-1);   /*  error condition */
        }

        pSectInfo++;

        /*  sect_cb       sect_cb - 1
         *  ZERO_HCB        1111b
         *    1             0000b
         *    2             0001b
         *    3             0010b
         *    4             0011b
         *    5             0100b
         *    6             0101b
         *    7             0110b
         *    8             0111b
         *    9             1000b
         *    10            1001b
         *    11            1010b
         *    12            1011b
         * NOISE_HCB        1100b
         * INTENSITY_HCB2   1101b
         * INTENSITY_HCB    1110b
         * if ( ((sect_cb - 1) & 0xC) == 0xC ) is identical to
         * if !((sect_cb == ZERO_HCB) || (sect_cb == NOISE_HCB) ||
         *      (sec_cb == INTENSITY_HCB) || (sect_cb==INTENSITY_HCB2) )
         * use this compare scheme to speed up the execution
         */

        if (((sect_cb - 1) & 0xC) != 0xC)
        {
            /* decode spec in one section */
            if (sect_cb > BY4BOOKS)
            {
                dim = DIMENSION_2; /* set codebook dimension */
            }
            else
            {
                dim = DIMENSION_4;
            }

            pHcb        = &hcbbook_binary[sect_cb];

            if (sect_cb == ESCBOOK)
            {
                pUnpack_idx = &unpack_idx_esc;
            }
            else if (pHcb->signed_cb == FALSE)
            {
                pUnpack_idx = &unpack_idx_sgn;
            }
            else
            {
                pUnpack_idx = &unpack_idx;
            }


            switch (sect_cb)
            {
                case 1:
                    pDec_huff_tab = decode_huff_cw_tab1;
                    break;
                case 2:
                    pDec_huff_tab = decode_huff_cw_tab2;
                    break;
                case 3:
                    pDec_huff_tab = decode_huff_cw_tab3;
                    break;
                case 4:
                    pDec_huff_tab = decode_huff_cw_tab4;
                    break;
                case 5:
                    pDec_huff_tab = decode_huff_cw_tab5;
                    break;
                case 6:
                    pDec_huff_tab = decode_huff_cw_tab6;
                    break;
                case 7:
                    pDec_huff_tab = decode_huff_cw_tab7;
                    break;
                case 8:
                    pDec_huff_tab = decode_huff_cw_tab8;
                    break;
                case 9:
                    pDec_huff_tab = decode_huff_cw_tab9;
                    break;
                case 10:
                    pDec_huff_tab = decode_huff_cw_tab10;
                    break;
                case 11:
                    pDec_huff_tab = decode_huff_cw_tab11;
                    break;
                default:
                    return (-1); /* error condition */
            }

            /* move ptr to first sfb of current section */
            pQuantSpec  = quantSpec + stop_idx;

            /* step through all sfbs in current section */
            for (sfb = sect_start; sfb < sect_end; sfb++)
            {
                idx_count = *pSfb - stop_idx;
                stop_idx  = *pSfb++;

                /* decode all coefs for one sfb */
                while ((idx_count > 0) && (idx_count < 1024))
                {

                    idx = (*pDec_huff_tab)(pInputStream);

                    (*pUnpack_idx)(pQuantSpec,
                                   idx,
                                   pHcb,
                                   pInputStream,
                                   &max);      /* unpack idx -> coefs */

                    pQuantSpec += dim;
                    idx_count  -= dim;

                } /* while(idx_count) */

            } /* for (sfb=sect_start) */
        }
        else
        {

            /* current section uses ZERO_HCB, NOISE_HCB, etc */

            /* move sfb pointer to the start sfb of next section */
            pSfb        = pSfbStart + sect_end;
            /* number of coefs in current section */
            idx_count   = *(pSfb - 1) - stop_idx;

            if ((idx_count > 1024) || (idx_count < 0))
            {
                return (-1);   /*  error condition */
            }

            /*
             * This memset is necessary in terms of (1) net savings in total
             * MIPS and (2) accurate Q-Formats for fft_rx2
             * In case a scalefactor band uses ZERO_HCB, all coefficients of
             * that sfb should be zeros. Without this call to memset, the
             * coefficients for a ZERO_HCB sfb are the "leftovers" of the
             * previous frame, which may not have all zero values. This leads
             * to a drastical increase in the cycles consumed by esc_iquant_fxp
             * and fft_rx2, which is the most "expensive" function of the
             * library.
             * This memset also guarantees the Q_Format for sfbs with all zero
             * coefficients will be set properly.
             * Profiling data on ARM and TMS320C55x proves that there is a net
             * gain in total MIPS if a memset is called here.
             */
            pv_memset(&quantSpec[stop_idx],
                      0,
                      idx_count * sizeof(quantSpec[0]));

            /*
             * This memset is called because pQuantSpec points to tmp_spec
             * after deinterleaving
             */

            pv_memset(&tmp_spec[stop_idx],
                      0,
                      idx_count * sizeof(tmp_spec[0]));


            /* stop_idx is the index of the 1st coef of next section */
            stop_idx    = *(pSfb - 1);

        }/* if (sect_cb) */

        sect_start = sect_end;

    } /* for (i=nsect) */

    /* noisless coding reconstruction */
    if (pFrameInfo->islong != FALSE)
    {
        if (pPulseInfo->pulse_data_present == 1)
        {
            pulse_nc(quantSpec,
                     pPulseInfo,
                     pLongFrameInfo,
                     &max);    /* add pulse data */
        }

        pQuantSpec = quantSpec;

    }
    else
    {
        deinterleave(quantSpec,
                     tmp_spec,
                     pFrameInfo);

        pQuantSpec = tmp_spec;
    }


    /* inverse quantization, Q_format: Int32 */
    /* rescaling */

    /* what we can do here is assuming that we already know maxInput for each band, we have to go
    though each one of them for re-quant and scaling, and pick the right qFormat to apply to
    all spectral coeffs.*/

    if ((max < 0) || (max > 8192))    /* (8192>>ORDER) == 1024 is the inverseQuantTable size */
    {
        return (-1);   /*  error condition */
    }
    else
    {
        /* Get  (max/SPACING) ^ (1/3), in Q Format  */
        temp = inverseQuantTable[(max >> ORDER) + 1];
    }


    /* Round up before shifting down to Q0 */
    temp += ROUND_UP;

    /* shift down to Q0 and multiply by 2 (FACTOR) in one step */
    temp >>= (QTABLE - 1);

    /* Now get max ^ (4/3) in Q0 */
    temp *= max;


    binaryDigits = 31 - pv_normalize(temp);


    /* Prevent negative shifts caused by low maximums. */
    if (binaryDigits < (SIGNED32BITS - QTABLE))
    {
        binaryDigits = SIGNED32BITS - QTABLE;
    }

    QFormat = SIGNED32BITS - binaryDigits;

    /********************/
    tot_sfb = 0;
    nsfb = pFrameInfo->sfb_per_win[0];
    pCoef = coef;

    for (i = pFrameInfo->num_win; i > 0; i--)
    {
        stop_idx  = 0;

        for (sfb = 0; sfb < nsfb; sfb++)
        {
            sfbWidth   =  pFrameInfo->win_sfb_top[0][sfb] - stop_idx;

            if ((sfbWidth < 0) || (sfbWidth > 1024))
            {
                return (-1);   /*  error condition */
            }

            stop_idx  += sfbWidth;

            fac   = factors[tot_sfb] - SF_OFFSET;
            scale = exptable[fac & 0x3];

            power_scale_div_4 = fac >> 2;

            power_scale_div_4++;

            qFormat[tot_sfb] = QFormat;

            esc_iquant_scaling(pQuantSpec,
                               pCoef,
                               sfbWidth,
                               QFormat,
                               scale,
                               max);

            pQuantSpec += sfbWidth;
            qFormat[tot_sfb] -= power_scale_div_4;
            pCoef += sfbWidth;

            tot_sfb++;

        } /* for (sfb) */
    } /* for (i) */


    /*----------------------------------------------------------------------------
    ; Return status
    ----------------------------------------------------------------------------*/
    return SUCCESS;

} /* huffspec_fxp */