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 Pathname: ./src/get_ics_info.c
21
22------------------------------------------------------------------------------
23 REVISION HISTORY
24
25 Description:  Modified from original shareware code
26
27 Description:  Modified to pass variables by reference to eliminate use
28               of global variables.
29
30 Description:  Clean up code.
31
32 Description:  Fix comments before review, remove lpflag[]
33
34 Description:  Update per review comments, and match ISO/IEC 14496-3
35
36 Description:  Update per peer review comments.
37
38 Description:  Remove "rollback" of used bits, since lt_decode is to change.
39
40 Description: Replace some instances of getbits to get9_n_lessbits
41              when the number of bits read is 9 or less and get1bits
42              when only 1 bit is read.
43
44 Who:                                   Date:
45 Description:
46
47------------------------------------------------------------------------------
48 INPUT AND OUTPUT DEFINITIONS
49
50 Inputs:
51    audioObjectType = MP4 Audio Object Type for the current song. Only if
52                    this is set to LTP (MP4AUDIO_LTP) will long term
53                    prediction bits be retrieved. Data type
54                    tMP4AudioObjectType, which is an enumeration, which in
55                    turn is an Int.
56
57    pInputStream  = pointer to a BITS structure, used by the function getbits
58                    to provide data. This is the second parameter to this
59                    function to match its position in getbits().
60                    Data type pointer to BITS structure
61
62    common_window = field read in huffdecode, which tells whether information
63                    is shared between the left and right channel. Long term
64                    prediction (LTP) data is NOT shared even if its a common
65                    window, so this flag is needed to see if another set of
66                    LTP possibly needs to be read. If this flag is false,
67                    pSecondLTPStatus is not touched, it could be NULL if
68                    need be. Data type Bool, which is Int.
69
70    pWindowSequence = pointer to where the the window type of the current
71                    frame and channel should be placed, of data type
72                    WINDOW_SEQUENCE, which is Int. It can take on one
73                    of four values: ONLY_LONG_SEQUENCE, LONG_START_SEQUENCE,
74                    EIGHT_SHORT_SEQUENCE, LONG_STOP_SEQUENCE,
75
76    pWindowShape =  pointer to where the window shape for the current frame
77                    and channel should be placed, of data type WINDOW_SHAPE,
78                    which is Int. It can take on the one of these two values:
79                    SINE_WINDOW, KAISER_BESSEL_WINDOW. It is used in the
80                    "filterbank" section of decoding.
81
82    group         = array that holds the index of the first window in each
83                    group. Data type array of Int, eight elements.
84
85    p_max_sfb     = pointer to where the maximum number of scale factor bands
86                    for the current frame and channel will be placed. Data
87                    type of pointer to Int.
88
89    p_winmap      = array of pointers to all of the possible four window
90                    configurations. This parameter did not need to be pointers,
91                    and could be changed in the future. Data type array of pointers
92                    to FrameInfo structures, length 4.
93
94    pFirstLTPStatus = pointer to a structure where the first LTP
95                    information will be stored. It would be confusing and wrong
96                    to call this left LTP status since if common_window = FALSE,
97                    this function will be called twice - once for the left, once
98                    for the right. It could be done, but extra conditional code
99                    would need to be done.
100                    Data type pointer to LT_PRED_STATUS structure.
101
102    pSecondLTPStatus = pointer to where the right channel of LTP
103                    information will be stored only if common_window is non-zero.
104                    Data type pointer to LT_PRED_STATUS structure.
105
106 Local Stores/Buffers/Pointers Needed: None.
107
108 Global Stores/Buffers/Pointers Needed: None.
109
110 Outputs:
111    status  = 0 implies no error occurred, non-zero otherwise.
112
113 Pointers and Buffers Modified:
114    pInputStream contents are modified in such a way that the number of bits
115        read increases.
116    pWindowSequence contents are updated with the current window for this
117        frame and channel
118    group[] contents will be modified to grouping information. See getgroup
119        source code for a better description of what this is.
120    p_max_sfb contents will be updated with the maximum scale factor bands
121        for this frame and channel.
122    pFirstLTPStatus contents may be updated if the stream has long term
123        prediction information.
124    pSecondLTPStatus contents may be updated if common_window != 0 and LTP data
125        is present.
126
127
128 Local Stores Modified: None
129
130 Global Stores Modified: None
131
132------------------------------------------------------------------------------
133 FUNCTION DESCRIPTION
134
135 This function retrieves the individual channel stream (ICS) information
136 from the bitstream. The information read for the current
137 frame and channel is:
138 - window sequence
139 - window shape for use in the filter bank
140 - number of scale factor bands
141 - long term predication (LTP) information
142 - grouping information
143
144 This function does NOT support MPEG2 style AAC Frequency Domain Predictor,
145 not to be confused with LTP (Long Term Prediction). If such data is found
146 to be on the file an error is generated.
147
148------------------------------------------------------------------------------
149 REQUIREMENTS
150
151 This function is not to use static or global data.
152
153------------------------------------------------------------------------------
154 REFERENCES
155
156  (1) ISO/IEC 14496-3:1999(E) Titled "Information technology - Coding
157      of audio-visual objects Part 3: Audio Subpart 4:"
158      Table 4.4.6 - Syntax of ics_info(), page 16.
159
160
161 (2) MPEG-2 NBC Audio Decoder
162   "This software module was originally developed by AT&T, Dolby
163   Laboratories, Fraunhofer Gesellschaft IIS in the course of development
164   of the MPEG-2 NBC/MPEG-4 Audio standard ISO/IEC 13818-7, 14496-1,2 and
165   3. This software module is an implementation of a part of one or more
166   MPEG-2 NBC/MPEG-4 Audio tools as specified by the MPEG-2 NBC/MPEG-4
167   Audio standard. ISO/IEC  gives users of the MPEG-2 NBC/MPEG-4 Audio
168   standards free license to this software module or modifications thereof
169   for use in hardware or software products claiming conformance to the
170   MPEG-2 NBC/MPEG-4 Audio  standards. Those intending to use this software
171   module in hardware or software products are advised that this use may
172   infringe existing patents. The original developer of this software
173   module and his/her company, the subsequent editors and their companies,
174   and ISO/IEC have no liability for use of this software module or
175   modifications thereof in an implementation. Copyright is not released
176   for non MPEG-2 NBC/MPEG-4 Audio conforming products.The original
177   developer retains full right to use the code for his/her own purpose,
178   assign or donate the code to a third party and to inhibit third party
179   from using the code for non MPEG-2 NBC/MPEG-4 Audio conforming products.
180   This copyright notice must be included in all copies or derivative
181   works."
182   Copyright(c)1996.
183
184------------------------------------------------------------------------------
185 PSEUDO-CODE
186
187    status = 0;
188    first_ltp_data_present = FALSE;
189    second_ltp_data_present = FALSE;
190
191
192    CALL getbits(
193        neededBits = LEN_ICS_RESERV + LEN_WIN_SEQ + LEN_WIN_SH,
194        pInputStream = pInputStream)
195    MODIFYING( pInputStream )
196    RETURNING( temp = returnValue )
197
198    windowSequence = (temp >> LEN_WIN_SH) & ((0x1<<LEN_WIN_SEQ)-1);
199
200    *pWindowShape = (temp) & ((0x1<<LEN_WIN_SH)-1);
201
202    IF (windowSequence == EIGHT_SHORT_SEQUENCE)
203    THEN
204        CALL getbits(
205            neededBits = LEN_MAX_SFBS,
206            pInputStream = pInputStream)
207        MODIFYING(pInputStream)
208        RETURNING(local_max_sfb = returnValue)
209
210        CALL getgroup(
211            group = group,
212            pInputStream = pInputStream)
213        MODIFYING(group)
214        MODIFYING(pInputStream)
215        RETURNING(nothing)
216
217
218    ELSE
219
220        group[0] = 1;
221
222        CALL getbits(
223            neededBits = LEN_MAX_SFBL + LEN_PREDICTOR_DATA_PRESENT,
224            pInputStream = pInputStream)
225        MODIFYING(pInputStream)
226        RETURNING(temp = returnValue)
227
228        predictor_data_present =
229            (Bool) getbits(
230                LEN_BOOLEAN,
231                pInputStream);
232
233        local_max_sfb = (Int)(temp >> LEN_PREDICTOR_DATA_PRESENT);
234
235        predictor_data_present =
236            (Bool) (temp & ((0x1 << LEN_PREDICTOR_DATA_PRESENT)-1));
237
238        IF (local_max_sfb > allowed_max_sfb)
239        THEN
240            status = 1
241        ELSEIF (audioObjectType == MP4AUDIO_LTP)
242        THEN
243            IF (predictor_data_present != FALSE)
244            THEN
245                CALL getbits(
246                    neededBits = LEN_LTP_DATA_PRESENT,
247                    pInputStream = pInputStream)
248                MODIFYING(pInputStream)
249                RETURNING(first_ltp_data_present = returnValue)
250
251                IF (ltp_data_present != FALSE)
252                THEN
253
254                    CALL lt_decode(
255                        win_type = windowSequence,
256                        pInputStream  = pInputStream,
257                        max_sfb = local_max_sfb,
258                        pLt_pred = pFirstLTPStatus)
259                    MODIFYING(pInputStream)
260                    MODIFYING(pFirstLTPStatus)
261                    RETURNING(nothing)
262
263                ENDIF
264
265                IF (common_window != FALSE)
266                THEN
267                    CALL getbits(
268                        neededBits = LEN_LTP_DATA_PRESENT,
269                        pInputStream = pInputStream)
270                    MODIFYING(pInputStream)
271                    RETURNING(second_ltp_data_present = returnValue)
272
273                    IF (second_ltp_data_present != FALSE)
274                    THEN
275
276                        CALL lt_decode(
277                            win_type = windowSequence,
278                            pInputStream  = pInputStream,
279                            max_sfb = local_max_sfb,
280                            pLt_pred = pSecondLTPStatus)
281                        MODIFYING(pInputStream)
282                        MODIFYING(pSecondLTPStatus)
283                        RETURNING(nothing)
284                    ENDIF
285                ENDIF
286            ENDIF
287        ELSE
288            IF  (predictor_data_present != FALSE)
289            THEN
290                status = 1
291            ENDIF
292        END IF
293    ENDIF
294
295    pFirstLTPStatus->ltp_data_present = first_ltp_data_present;
296
297    IF (common_window != FALSE)
298    THEN
299        pSecondLTPStatus->ltp_data_present = second_ltp_data_present;
300    ENDIF
301
302    pFrameInfo = p_winmap[*p_wnd];
303    IF (local_max_sfb > pFrameInfo->sfb_per_frame)
304    THEN
305        status = 1;
306    ENDIF
307
308    *(p_max_sfb) = local_max_sfb;
309
310    MODIFY(*(pWindowSequence))
311    MODIFY(*(pWinShape))
312    MODIFY(*(p_max_sfb))
313    MODIFY(group[])
314    MODIFY(*pInputStream)
315    MODIFY(*pFirstLTPStatus)
316    MODIFY(*pSecondLTPStatus)
317    RETURN (status);
318
319
320
321------------------------------------------------------------------------------
322 RESOURCES USED
323   When the code is written for a specific target processor the
324     the resources used should be documented below.
325
326 STACK USAGE: [stack count for this module] + [variable to represent
327          stack usage for each subroutine called]
328
329     where: [stack usage variable] = stack usage for [subroutine
330         name] (see [filename].ext)
331
332 DATA MEMORY USED: x words
333
334 PROGRAM MEMORY USED: x words
335
336 CLOCK CYCLES: [cycle count equation for this module] + [variable
337           used to represent cycle count for each subroutine
338           called]
339
340     where: [cycle count variable] = cycle count for [subroutine
341        name] (see [filename].ext)
342
343------------------------------------------------------------------------------
344*/
345
346
347/*----------------------------------------------------------------------------
348; INCLUDES
349----------------------------------------------------------------------------*/
350#include "pv_audio_type_defs.h"
351
352#include "e_rawbitstreamconst.h"
353#include "e_tmp4audioobjecttype.h"
354
355#include "s_bits.h"
356#include "s_frameinfo.h"
357#include "s_lt_pred_status.h"
358
359#include "ibstream.h"
360#include "lt_decode.h"
361#include "ltp_common_internal.h" /* For LEN_LTP_DATA_PRESENT constant */
362
363#include "get_ics_info.h"
364#include "huffman.h"        /* For the declaration of getgroup */
365
366/*----------------------------------------------------------------------------
367; MACROS
368; Define module specific macros here
369----------------------------------------------------------------------------*/
370
371/*----------------------------------------------------------------------------
372; DEFINES
373; Include all pre-processor statements here. Include conditional
374; compile variables also.
375----------------------------------------------------------------------------*/
376
377#define LEN_PREDICTOR_DATA_PRESENT (1)
378
379/*----------------------------------------------------------------------------
380; LOCAL FUNCTION DEFINITIONS
381; Function Prototype declaration
382----------------------------------------------------------------------------*/
383
384/*----------------------------------------------------------------------------
385; LOCAL VARIABLE DEFINITIONS
386; Variable declaration - defined here and used outside this module
387----------------------------------------------------------------------------*/
388
389/*----------------------------------------------------------------------------
390; EXTERNAL FUNCTION REFERENCES
391; Declare functions defined elsewhere and referenced in this module
392----------------------------------------------------------------------------*/
393
394/*----------------------------------------------------------------------------
395; EXTERNAL VARIABLES REFERENCES
396; Declare variables used in this module but defined elsewhere
397----------------------------------------------------------------------------*/
398
399/*----------------------------------------------------------------------------
400; FUNCTION CODE
401----------------------------------------------------------------------------*/
402
403Int get_ics_info(
404    const tMP4AudioObjectType  audioObjectType,
405    BITS                      *pInputStream,
406    const Bool                 common_window,
407    WINDOW_SEQUENCE           *pWindowSequence,
408    WINDOW_SHAPE              *pWindowShape,
409    Int                        group[],
410    Int                       *p_max_sfb,
411    FrameInfo                 *p_winmap[],
412    LT_PRED_STATUS            *pFirstLTPStatus,
413    LT_PRED_STATUS            *pSecondLTPStatus)
414{
415    WINDOW_SEQUENCE       windowSequence;
416    UInt                  temp;
417    Bool                  predictor_data_present;
418    UInt                   local_max_sfb;
419    UInt                   allowed_max_sfb;
420    Int                   status = SUCCESS;
421    Bool                  first_ltp_data_present = FALSE;
422    Bool                  second_ltp_data_present = FALSE;
423
424    /*
425     * The following three calls to getbits have been replaced with one
426     * call for speed:
427     *
428     *                  getbits(LEN_ICS_RESERV, pInputStream);
429     * windowSequence = getbits(LEN_WIN_SEQ, pInputStream);
430     * *pWindowShape  = getbits(LEN_WIN_SH, pInputStream);
431     *
432     */
433
434    temp =
435        get9_n_lessbits(
436            LEN_ICS_RESERV + LEN_WIN_SEQ + LEN_WIN_SH,
437            pInputStream);
438
439
440    windowSequence = (WINDOW_SEQUENCE)((temp >> LEN_WIN_SH) & ((0x1 << LEN_WIN_SEQ) - 1));
441
442    *pWindowShape = (WINDOW_SHAPE)((temp) & ((0x1 << LEN_WIN_SH) - 1));
443
444    /*
445     * This pointer should not be NULL as long as the initialization code
446     * has been run, so the test for NULL has been removed.
447     */
448    allowed_max_sfb = p_winmap[windowSequence]->sfb_per_win[0];
449
450    if (windowSequence == EIGHT_SHORT_SEQUENCE)
451    {
452        local_max_sfb =  get9_n_lessbits(LEN_MAX_SFBS,
453                                         pInputStream);
454
455        getgroup(
456            group,
457            pInputStream);
458
459        if (local_max_sfb > allowed_max_sfb)
460        {
461            status = 1;  /* ERROR CODE - needs to be updated */
462        }
463
464    } /* end of TRUE of if (windowSequence == EIGHT_SHORT_SEQUENCE) */
465    else
466    {
467        /* There is only one group for long windows. */
468        group[0] = 1;
469
470        /*
471         * The window is long, get the maximum scale factor bands,
472         * and get long term prediction info.
473         *
474         * Reference [1] states that the audioObjectType is first tested,
475         * then the predictor_data_present is read on either branch of the
476         * if (audioObjectType == MP4AUDIO_LTP). Instead, this code combines
477         * the two calls on both branches into one before the
478         * if, and then in turn combines with another call to getbits, all
479         * in the name of speed.
480         *
481         * This would be the individual calls, without checking the number
482         * of scale factor bands:
483         *
484         *   local_max_sfb =
485         *      (Int) getbits(
486         *          LEN_MAX_SFBL,
487         *           pInputStream);
488         *
489         *  if (audioObjectType == MP4AUDIO_LTP)
490         *  {
491         *        predictor_data_present =
492         *           (Bool) getbits(
493         *              LEN_PREDICTOR_DATA_PRESENT,
494         *              pInputStream);
495         *
496         *     .....   (read LTP data)
497         *
498         *    }
499         *    else
500         *    {
501         *
502         *        predictor_data_present =
503         *           (Bool) getbits(
504         *              LEN_PREDICTOR_DATA_PRESENT,
505         *              pInputStream);
506         *
507         *     .....   (its an error for this library)
508         *     }
509         */
510        temp =
511            get9_n_lessbits(
512                LEN_MAX_SFBL + LEN_PREDICTOR_DATA_PRESENT,
513                pInputStream);
514
515        local_max_sfb = (Int)(temp >> LEN_PREDICTOR_DATA_PRESENT);
516
517        predictor_data_present =
518            (Bool)(temp & ((0x1 << LEN_PREDICTOR_DATA_PRESENT) - 1));
519
520        if (local_max_sfb > allowed_max_sfb)
521        {
522            status = 1;  /* ERROR CODE - needs to be updated */
523        }
524        else if (audioObjectType == MP4AUDIO_LTP)
525        {
526            /*
527             * Note that the predictor data bit has already been
528             * read.
529             */
530
531            /*
532             * If the object type is LTP, the predictor data is
533             * LTP. If the object type is not LTP, the predictor data
534             * is so called "frequency predictor data", which is not
535             * supported by this implementation. Refer to (1)
536             */
537            if (predictor_data_present != FALSE)
538            {
539                first_ltp_data_present =
540                    (Bool) get1bits(/*                        LEN_LTP_DATA_PRESENT,*/
541                        pInputStream);
542
543                if (first_ltp_data_present != FALSE)
544                {
545                    lt_decode(
546                        windowSequence,
547                        pInputStream,
548                        local_max_sfb,
549                        pFirstLTPStatus);
550                }
551                if (common_window != FALSE)
552                {
553                    second_ltp_data_present =
554                        (Bool) get1bits(/*                            LEN_LTP_DATA_PRESENT,*/
555                            pInputStream);
556
557                    if (second_ltp_data_present != FALSE)
558                    {
559                        lt_decode(
560                            windowSequence,
561                            pInputStream,
562                            local_max_sfb,
563                            pSecondLTPStatus);
564                    }
565                } /* if (common_window != FALSE) */
566
567            } /* if (predictor_data_present != FALSE) */
568
569        } /* else if (audioObjectType == MP4AUDIO_LTP) */
570        else
571        {
572            /*
573             * Note that the predictor data bit has already been
574             * read.
575             */
576
577            /*
578             * The object type is not LTP. If there is data, its
579             * frequency predictor data, not supported by this
580             * implementation.
581             */
582            if (predictor_data_present != FALSE)
583            {
584                status = 1; /* ERROR CODE UPDATE LATER */
585            } /* if (predictor_data_present != FALSE) */
586
587        } /* end of "else" clause of if (audioObjectType == MP4AUDIO_LTP) */
588
589    } /*  if (windowSequence == EIGHT_SHORT_SEQUENCE) [FALSE branch] */
590
591
592    /*
593     * Save all local copies.
594     */
595    pFirstLTPStatus->ltp_data_present = first_ltp_data_present;
596    if (common_window != FALSE)
597    {
598        pSecondLTPStatus->ltp_data_present = second_ltp_data_present;
599    }
600
601    *p_max_sfb = local_max_sfb;
602
603    *pWindowSequence = windowSequence;
604
605    return (status);
606
607}  /* get_ics_info */
608
609