summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/aacdec/getics.cpp
blob: 8d76744358844a45bfa42ae8804d25aa4df78581 (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
/* ------------------------------------------------------------------
 * 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: getics.c

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

 Description:  Modified from original shareware code

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

 Description: Remove pass-in parameter global_gain, define it on stack.

 Description: (1) Modified to bring in-line with PV standards
              (2) Modified pass in parameters
              (3) Removed multiple returns, removed some if branch
              (4) Replace for loop with pv_memset

 Description: Remove prstflag, fix copyright.

 Description: Fix pseudo-code

 Description: Remove lpflag from get_ics_info

 Description: (1) Removed widx, therefore, pChVarsWin is eliminated from
                  pass in parameter

 Description: merged the above changes from Michael and Wen

 Description: Removed initialization of "pTnsFrameInfo->num_subblocks" since
 this element was removed from that structure, as a part of
 rearchitecting the TNS routines to use memory more efficiently.

 Description:
 (1) Added #include of "e_HuffmanConst.h"
     Previously, this function was relying on another include file
     to include "e_HuffmanConst.h"

 (2) Updated the copyright header.

 (3) Added #include of <stdlib.h> for NULL macro definition.

 Description:
 (1) Removed the first parameter to getics.c  This extra
     FrameInfo was not needed, the contents of winmap can be used.
 (2) Removed the memcpy of the data from winmap to the temporary
     FrameInfo.

 Description: Replace some instances of getbits to get1bits
              when only 1 bit is read.

 Description:

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

 Inputs:
    pInputStream    =   pointer to structure that holds input stream,
                        Type BITS

    common_window   =   flag that indicates whether left and right channel
                        share the same window sequence & shape, Type Int

    pVars           =   pointer to structure that holds decoder information
                        Type tDec_Int_File

    pChVarsCh       =   pointer to structure that holds channel related
                        decoding information, Type tDec_Int_Chan

    group[]         =   pointer to array that contains window grouping
                        information of current frame, Type UChar

    pMax_sfb        =   pointer to variable that stores maximum active
                        scalefactor bands of current frame, Type UChar

    pCodebookMap    =   pointer to array that holds the indexes of all
                        Huffman codebooks used for current frame, ordered
                        from section 0 to last section. Type UChar

    pTnsFrameInfo   =   pointer to structure that holds TNS information.
                        Type TNS_frame_info

    pWinMap         =   array of pointers which points to structures that
                        hold information of long and short window sequences
                        Type FrameInfo

    pPulseInfo       =   pointer to structure that holds pulse data decoding
                        information, Type Nec_info

    sect[]          =   array of structures that hold section codebook and
                        section length in current frame, Type SectInfo

 Local Stores/Buffers/Pointers Needed:
    None

 Global Stores/Buffers/Pointers Needed:
    None

 Outputs:
    status = 0  if success
             1  otherwise

 Pointers and Buffers Modified:
    pCodebookMap    contents are replaced by the indexes of all the huffman
                    codebooks used for current frame

    pWinMap         For short windows, the contents of frame_sfb_top are
                    modified by calc_gsfb_table, with the top coefficient
                    index of each scalefactor band.

 Local Stores Modified:
    None

 Global Stores Modified:
    None

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

 This function decodes individual channel stream by calling other Huffman
 decoding functions.

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

 This function replaces the contents of pCodebookMap with the decoded
 codebook indexes. By calling hufffac, it decodes scale factor data. Call
 huffspec_fxp to decode spectral coefficients of current frame.

------------------------------------------------------------------------------
 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           p24 (Table 4.4.24)
                        p54 (4.5.2.3.2)

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

    pGroup = group;

    global_gain = CALL getbits(
                            neededBits   = LEN_SCL_PCM,
                            pInputStream = pInputStream)
                        MODIFYING(pInputStream)
                        ReTURNING(global_gain)

    IF (common_window == FALSE)
    THEN
        status = CALL get_ics_info(
                        pVars->mc_info.audioObjectType,
                        pInputStream,
                        common_window,
                       &pChVars->wnd,
                       &pChVars->wnd_shape_this_bk,
                        group,
                        pMax_sfb,
                        pWinMap,
                        &pChVars->lt_status,
                        NULL)
                    MODIFYING(pInputStream,pChVars,group,max_sfb,lt_status)
                    RETURNING(status)
    ENDIF

    memcpy(pFrameInfo, pWinMap[pChVars->wnd], sizeof(FrameInfo))

    IF (*pMax_sfb > 0)
    THEN

        i      = 0;
        totSfb = 0;

        DO

            totSfb++;

        WHILE( *pGroup++ < pFrameInfo->num_win);

        totSfb  *=  pFrameInfo->sfb_per_win[0];

        nsect = CALL huffcb(
                        sect,
                        pInputStream,
                        pFrameInfo->sectbits,
                        totSfb,
                        pFrameInfo->sfb_per_win[0],
                       *pMax_sfb)
                    MODIFYING(sect,pInputStream,sectbits)
                    RETURNING(nsect)

        IF (nsect == 0)
        THEN
            status = 1

        ENDIF

        sectStart = 0;
        FOR (i = 0; i < nsect; i++)

            cb  = sect[i].sect_cb;
            sectWidth =  sect[i].sect_end - sectStart;
            sectStart += sectWidth;

            WHILE (sectWidth > 0)

                *pCodebookMap++ = cb
                 sectWidth--
            ENDWHILE

        ENDFOR (i)

    ELSE

        memset(pCodebookMap,ZERO_HCB,MAXBANDS*sizeof(*pCodebookMap));

    ENDIF (*pMax_sfb)

    IF (pFrameInfo->islong == FALSE)
    THEN
        CALL calc_gsfb_table(
                pFramInfo = pFrameInfo,
                group[]   = group)
              MODIFYING(pFrameInfo->frame_sfb_top)
              RETURNING(void)
    ENDIF

    IF (status == SUCCESS)
    THEN
        status = CALL hufffac(
                        pFrameInfo,
                        pInputStream,
                        group,
                        nsect,
                        sect,
                        global_gain,
                        pChVars->factors,
                        pVars->huffBookUsed)
                    MODIFYING(pInputStream,factors)
                    RETURNING(status)

    ENDIF (status)

    IF (status == SUCCESS)
    THEN
        present = CALL getbits(
                        neededBits   = LEN_PULSE_PRES,
                        pInputStream = pInputStream)
                    MODIFYING(pInputStream)
                    RETURNING(present)

        pPulseInfo->pulse_data_present = present;

        IF (present != FALSE)
        THEN
            IF (pFrameInfo->islong == 1)
            THEN
                CALL get_pulse_data(
                          pPulseInfo = pPulseInfo,
                          pInputStream = pInputStream)
                    MODIFYING(pInputStream,pPulseInfo)
                    RETURNING(void)

            ELSE

                status = 1;

            ENDIF (pFrameInfo)
        ENDIF (present)

    ENDIF (status)

    IF (status == SUCCESS)
    THEN
        present = CALL getbits(
                        neededBits = LEN_TNS_PRES,
                        pInputStream = pInputStream)
                    MODIFYING(pInputStream)
                    RETURNING(present)

        pTnsFrameInfo->tns_data_present = present;

        IF (present != FALSE)
        THEN
            CALL get_tns(
                    pFrameInfo = pFrameInfo,
                    pTnsFrameInfo = pTnsFrameInfo,
                    pInputStream = pInputStream)
                MODIFYING(pInputStream, pTnsFrameInfo)
                RETURNING(void)
        ELSE

            FOR (i = pTnsFrameInfo->n_subblocks - 1; i >= 0 ; i--)

                pTnsFrameInfo->info[i].n_filt = 0;
            ENDFOR

        ENDIF(present)

    ENDIF (status)

    IF (status == SUCCESS)
    THEN
        present = CALL getbits(
                        neededBits = LEN_GAIN_PRES,
                        pInputStream = pInputStream)
                MODIFYING(pInputStream)
                RETURNING(present)

        IF (present != FALSE)
        THEN
            status = 1;
        ENDIF
    ENDIF (status)

    IF (status == SUCCESS)
    THEN
        status = CALL huffspec_fxp(
                        pFrameInfo,
                        pInputStream,
                        nsect,
                        sect,
                        pChVars->factors,
                        pChVars->fxpCoef,
                        pVars->quantSpec,
                        pVars->tmp_spec,
                        pWinMap[ONLY_LONG_WINDOW],
                        pPulseInfo,
                        pChVars->qFormat)
                MODIFYING(pInputStream,fxpCoef,quantSpec,tmp_spec,qFormat)
                RETURNING(status)
    ENDIF

    RETURN status

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


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

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


/*----------------------------------------------------------------------------
; DEFINES
; Include all pre-processor statements here. Include conditional
; compile variables also.
----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
; 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 getics(
    BITS            *pInputStream,
    Int             common_window,
    tDec_Int_File   *pVars,
    tDec_Int_Chan   *pChVars,
    Int             group[],
    Int             *pMax_sfb,
    Int             *pCodebookMap,
    TNS_frame_info  *pTnsFrameInfo,
    FrameInfo       **pWinMap,
    PulseInfo       *pPulseInfo,
    SectInfo        sect[])
{
    /*----------------------------------------------------------------------------
    ; Define all local variables
    ----------------------------------------------------------------------------*/
    Int     status = SUCCESS;

    Int     nsect = 0;
    Int     i;
    Int     cb;
    Int     sectWidth;
    Int     sectStart;
    Int     totSfb;
    Int     *pGroup;

    FrameInfo *pFrameInfo;

    Int     global_gain; /* originally passed in from huffdecode */
    Bool    present;

    /*----------------------------------------------------------------------------
    ; Function body here
    ----------------------------------------------------------------------------*/
    pGroup = group;

    /* read global gain from Input bitstream */
    global_gain =
        get9_n_lessbits(
            LEN_SCL_PCM,
            pInputStream);

    if (common_window == FALSE)
    {
        status = get_ics_info(
                     pVars->mc_info.audioObjectType,
                     pInputStream,
                     common_window,
                     &pChVars->wnd,
                     &pChVars->wnd_shape_this_bk,
                     group,
                     pMax_sfb,
                     pWinMap,
                     &pChVars->pShareWfxpCoef->lt_status,
                     NULL);
    }

    pFrameInfo = pWinMap[pChVars->wnd];

    /* First, calculate total number of scalefactor bands
     * for this grouping. Then, decode section data
     */
    if (*pMax_sfb > 0)
    {

        /* calculate total number of sfb */
        i      = 0;
        totSfb = 0;

        do
        {
            totSfb++;

        }
        while (*pGroup++ < pFrameInfo->num_win);

        totSfb  *=  pFrameInfo->sfb_per_win[0];

        /* decode section data */
        nsect =
            huffcb(
                sect,
                pInputStream,
                pFrameInfo->sectbits,
                totSfb,
                pFrameInfo->sfb_per_win[0],
                *pMax_sfb);

        if (nsect == 0)
        {
            status = 1;     /* decode section data error */

        }/* if (nsect) */

        /* generate "linear" description from section info
         * stored as codebook for each scalefactor band and group
         * when nsect == 0, for-loop does not execute
         */
        sectStart = 0;
        for (i = 0; i < nsect; i++)
        {
            cb  = sect[i].sect_cb;
            sectWidth =  sect[i].sect_end - sectStart;
            sectStart += sectWidth;

            while (sectWidth > 0)
            {
                *pCodebookMap++ = cb;   /* cannot use memset for Int */
                sectWidth--;
            }

        } /* for (i) */

    }
    else
    {
        /* set all sections with ZERO_HCB */
        pv_memset(
            pCodebookMap,
            ZERO_HCB,
            MAXBANDS*sizeof(*pCodebookMap));
        /*
                for (i=MAXBANDS; i>0; i--)
                {
                    *(pCodebookMap++) = ZERO_HCB;
                }
        */

    } /* if (*pMax_sfb) */

    /* calculate band offsets
     * (because of grouping and interleaving this cannot be
     * a constant: store it in pFrameInfo->frame_sfb_top)
     */
    if (pFrameInfo->islong == FALSE)
    {
        calc_gsfb_table(
            pFrameInfo,
            group);
    }

    /* decode scale factor data */
    if (status == SUCCESS)
    {
        status =
            hufffac(
                pFrameInfo,
                pInputStream,
                group,
                nsect,
                sect,
                global_gain,
                pChVars->pShareWfxpCoef->factors,
                pVars->scratch.huffbook_used);

    } /* if (status) */

    /* noiseless coding */
    if (status == SUCCESS)
    {
        present =
            get1bits(pInputStream);

        pPulseInfo->pulse_data_present = present;

        if (present != FALSE)
        {
            if (pFrameInfo->islong == 1)
            {
                status = get_pulse_data(
                             pPulseInfo,
                             pInputStream);
            }
            else
            {
                /* CommonExit(1,"Pulse data not allowed for short blocks"); */
                status = 1;

            } /* if (pFrameInfo) */
        } /* if (present) */

    } /* if (status) */


    /* decode tns data */
    if (status == SUCCESS)
    {
        present =
            get1bits(pInputStream);

        pTnsFrameInfo->tns_data_present = present;

        if (present != FALSE)
        {
            get_tns(
                pChVars->pShareWfxpCoef->max_sfb,
                pInputStream,
                pChVars->wnd,
                pFrameInfo,
                &pVars->mc_info,
                pTnsFrameInfo,
                pVars->scratch.tns_decode_coef);
        }
        else
        {
            for (i = pFrameInfo->num_win - 1; i >= 0 ; i--)
            {
                pTnsFrameInfo->n_filt[i] = 0;
            }

        } /* if(present) */

    } /* if (status) */

    /* gain control */
    if (status == SUCCESS)
    {
        present =
            get1bits(pInputStream);

        if (present != FALSE)
        {
            /* CommonExit(1, "Gain control not implemented"); */
            status = 1;
        }
    } /* if (status) */

    if (status == SUCCESS)
    {
        status =
            huffspec_fxp(
                pFrameInfo,
                pInputStream,
                nsect,
                sect,
                pChVars->pShareWfxpCoef->factors,
                pChVars->fxpCoef,
                pVars->share.a.quantSpec,
                pVars->scratch.tmp_spec,
                pWinMap[ONLY_LONG_WINDOW],
                pPulseInfo,
                pChVars->pShareWfxpCoef->qFormat);
    }

    /*----------------------------------------------------------------------------
    ; Return status
    ----------------------------------------------------------------------------*/

    return status;

} /* getics */