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_audio_specific_config.c
21
22------------------------------------------------------------------------------
23 REVISION HISTORY
24
25 Description: Modified per review comments
26
27 Description: Modified per second review comments
28              (1) change audioObjectType to Int
29              (2) do not set pVars->prog_config.profile
30              (3) clean up status flag, default to SUCCESS
31              (4) fix multiple lines comments
32
33 Description: Change getbits.h to ibstream.h
34
35 Description: Modified per review comments
36              (1) updated revision history
37              (2) declare audioObjectType as enum type
38
39 Description: Replace some instances of getbits to get9_n_lessbits
40              when the number of bits read is 9 or less.
41
42 Description: Added support for backward and non-backward (explicit)
43              mode for Parametric Stereo (PS) used in enhanced AAC+
44
45 Who:                              Date:
46 Description:
47
48------------------------------------------------------------------------------
49 INPUT AND OUTPUT DEFINITIONS
50
51 Inputs:
52    pVars = pointer to the structure that holds all information for
53            this instance of the library. pVars->prog_config is directly
54            used, and pVars->mc_info, pVars->prog_config,
55            pVars->pWinSeqInfo, pVars->SFBWidth128 are needed indirectly
56            for calling set_mc_info. Data type pointer to tDec_Int_File
57
58 Local Stores/Buffers/Pointers Needed:
59    None
60
61 Global Stores/Buffers/Pointers Needed:
62    None
63
64 Outputs:
65    status = 0 if successfully decoded AudioSpecificConfig
66             1 if un-supported config is used for this release
67
68 Pointers and Buffers Modified:
69    pVars->prog_config contents are updated with the information read in.
70    pVars->mc_info contents are updated with channel information.
71    pVars->pWinSeqInfo contents are updated with window information.
72    pVars->SFBWidth128 contents are updated with scale factor band width data.
73
74 Local Stores Modified:
75    None
76
77 Global Stores Modified:
78    None
79
80------------------------------------------------------------------------------
81 FUNCTION DESCRIPTION
82
83 This function reads the bitstream for the structure "AudioSpecificConfig",
84 and sets the decoder configuration that is needed by the decoder to be able
85 to decode the media properly.
86
87------------------------------------------------------------------------------
88 REQUIREMENTS
89
90 This function shall not use global variables
91
92------------------------------------------------------------------------------
93 REFERENCES
94
95 (1) ISO/IEC 14496-3: 1999(E)
96 Part 3
97 Subpart 1  p18     1.6   Interface to MPEG-4 Systems
98 Subpart 4  p13     4.4.1 GA Specific Configuration
99 Amendment  p10     6.2.1 AudioSpecificInfo
100 Amendment  p78     8.2   Decoder configuration (GASpecificConfig)
101
102 (2) AAC DecoderSpecificInfo Information
103   PacketVideo descriptions - San Diego
104
105------------------------------------------------------------------------------
106 PSEUDO-CODE
107
108    status = SUCCESS;
109
110    pInputStream = &(pVars->inputStream);
111
112    temp = CALL getbits(
113                    neededBits = LEN_OBJ_TYPE + LEN_SAMP_RATE_IDX,
114                    pInputStream = pInputStream)
115           MODIFYING (pInputStream)
116           RETURNING (temp)
117
118    audioObjectType = (temp & 0x1f0) >> 4;
119
120    pVars->prog_config.profile = audioObjectType;
121
122    pVars->prog_config.sampling_rate_idx = temp & 0xf;
123
124    IF (pVars->prog_config.sampling_rate_idx == 0xf)
125    THEN
126        sampling_rate = CALL getbits(
127                            neededBits = LEN_SAMP_RATE,
128                            pInputStream = pInputStream);
129                        MODIFYING (pInputStream)
130                        RETURNING (sampling_rate)
131    ENDIF
132
133    channel_config = CALL getbits(
134                            neededBits = LEN_CHAN_CONFIG,
135                            pInputStream = pInputStream);
136                        MODIFYING (pInputStream)
137                        RETURNING (channel_config)
138
139    IF (channel_config > 2)
140    THEN
141        status = 1;
142    ENDIF
143
144    IF (((audioObjectType == MP4AUDIO_AAC_MAIN)     OR
145        (audioObjectType == MP4AUDIO_AAC_LC)        OR
146        (audioObjectType == MP4AUDIO_AAC_SSR)       OR
147        (audioObjectType == MP4AUDIO_LTP)           OR
148        (audioObjectType == MP4AUDIO_AAC_SCALABLE)  OR
149        (audioObjectType == MP4AUDIO_TWINVQ)) AND (status == -1))
150    THEN
151        status = CALL get_GA_specific_config(
152                            pVars = pVars,
153                            channel_config = channel_config,
154                            audioObjectType = audioObjectType,
155                            pInputStream = pInputStream);
156                      MODIFYING (pVars->mc_info,channel_config,pInputStream)
157                      RETURNING (status)
158
159    ENDIF
160
161    IF (audioObjectType == MP4AUDIO_CELP)
162    THEN
163        status = 1;
164    ENDIF
165
166    IF (audioObjectType == MP4AUDIO_HVXC)
167    THEN
168        status = 1;
169    ENDIF
170
171    IF (audioObjectType == MP4AUDIO_TTSI)
172    THEN
173        status = 1;
174    ENDIF
175
176    IF ((audioObjectType == 13) OR (audioObjectType == 14) OR
177        (audioObjectType == 15) OR (audioObjectType == 16))
178    THEN
179        status = 1;
180    ENDIF
181
182    IF (((audioObjectType == MP4AUDIO_ER_AAC_LC)       OR
183         (audioObjectType == MP4AUDIO_ER_AAC_LTP)      OR
184         (audioObjectType == MP4AUDIO_ER_AAC_SCALABLE) OR
185         (audioObjectType == MP4AUDIO_ER_TWINVQ)       OR
186         (audioObjectType == MP4AUDIO_ER_BSAC)         OR
187         (audioObjectType == MP4AUDIO_ER_AAC_LD)) AND (status == -1))
188    THEN
189        status = 1;
190    ENDIF
191
192    IF (audioObjectType == MP4AUDIO_ER_CELP)
193    THEN
194        status = 1;
195    ENDIF
196
197    IF (audioObjectType == MP4AUDIO_ER_HVXC)
198    THEN
199        status = 1;
200    ENDIF
201
202    IF ((audioObjectType == MP4AUDIO_ER_HILN) OR
203        (audioObjectType == MP4AUDIO_PARAMETRIC))
204    THEN
205        status = 1;
206    ENDIF
207
208    IF ((audioObjectType == MP4AUDIO_ER_AAC_LC)       OR
209        (audioObjectType == MP4AUDIO_ER_AAC_LTP)      OR
210        (audioObjectType == MP4AUDIO_ER_AAC_SCALABLE) OR
211        (audioObjectType == MP4AUDIO_ER_TWINVQ)       OR
212        (audioObjectType == MP4AUDIO_ER_BSAC)         OR
213        (audioObjectType == MP4AUDIO_ER_AAC_LD)       OR
214        (audioObjectType == MP4AUDIO_ER_CELP)         OR
215        (audioObjectType == MP4AUDIO_ER_HVXC)         OR
216        (audioObjectType == MP4AUDIO_ER_HILN)         OR
217        (audioObjectType == MP4AUDIO_PARAMETRIC))
218    THEN
219        epConfig = CALL getbits(
220                            neededBits = LEN_EP_CONFIG,
221                            pInputStream = pInputStream);
222                      MODIFYING (pInputStream)
223                      RETURNING (epConfig)
224
225        IF (epConfig == 2)
226        THEN
227            status = 1;
228        ENDIF
229
230    ENDIF
231
232    RETURN status;
233
234------------------------------------------------------------------------------
235 RESOURCES USED
236   When the code is written for a specific target processor the
237     the resources used should be documented below.
238
239 STACK USAGE: [stack count for this module] + [variable to represent
240          stack usage for each subroutine called]
241
242     where: [stack usage variable] = stack usage for [subroutine
243         name] (see [filename].ext)
244
245 DATA MEMORY USED: x words
246
247 PROGRAM MEMORY USED: x words
248
249 CLOCK CYCLES: [cycle count equation for this module] + [variable
250           used to represent cycle count for each subroutine
251           called]
252
253     where: [cycle count variable] = cycle count for [subroutine
254        name] (see [filename].ext)
255
256------------------------------------------------------------------------------
257*/
258
259
260/*----------------------------------------------------------------------------
261; INCLUDES
262----------------------------------------------------------------------------*/
263#include    "pv_audio_type_defs.h"
264#include    "e_mp4ff_const.h"
265#include    "e_tmp4audioobjecttype.h"
266#include    "get_audio_specific_config.h"
267#include    "get_ga_specific_config.h"
268#include    "ibstream.h"
269#include    "sfb.h"                   /* Where samp_rate_info[] is declared */
270
271/*----------------------------------------------------------------------------
272; MACROS
273; Define module specific macros here
274----------------------------------------------------------------------------*/
275
276
277/*----------------------------------------------------------------------------
278; DEFINES
279; Include all pre-processor statements here. Include conditional
280; compile variables also.
281----------------------------------------------------------------------------*/
282
283/*----------------------------------------------------------------------------
284; LOCAL FUNCTION DEFINITIONS
285; Function Prototype declaration
286----------------------------------------------------------------------------*/
287
288/*----------------------------------------------------------------------------
289; LOCAL STORE/BUFFER/POINTER DEFINITIONS
290; Variable declaration - defined here and used outside this module
291----------------------------------------------------------------------------*/
292
293/*----------------------------------------------------------------------------
294; EXTERNAL FUNCTION REFERENCES
295; Declare functions defined elsewhere and referenced in this module
296----------------------------------------------------------------------------*/
297
298/*----------------------------------------------------------------------------
299; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
300; Declare variables used in this module but defined elsewhere
301----------------------------------------------------------------------------*/
302
303/*----------------------------------------------------------------------------
304; FUNCTION CODE
305----------------------------------------------------------------------------*/
306Int get_audio_specific_config(tDec_Int_File   * const pVars)
307{
308
309    UInt    temp;
310    tMP4AudioObjectType     audioObjectType;
311    //UInt32  sampling_rate;
312    UInt    channel_config;
313    UInt    syncExtensionType;
314    UInt    extensionAudioObjectType = 0;
315    UInt    extensionSamplingFrequencyIndex = 0;
316    BITS   *pInputStream;
317    Int     status;
318
319    status = SUCCESS;
320
321    pInputStream = &(pVars->inputStream);
322
323    pVars->mc_info.upsamplingFactor = 1;   /*  default to regular AAC */
324
325    temp =  get9_n_lessbits(LEN_OBJ_TYPE + LEN_SAMP_RATE_IDX,
326                            pInputStream);
327
328    /*
329     * The following code can directly set the values of elements in
330     * MC_Info, rather than first setting the values in pVars->prog_config
331     * and then copy these values to MC_Info by calling set_mc_info.
332     * In order to keep consistent with get_prog_config (ADIF) and
333     * get_adts_header (ADTS), the code here is still copying
334     * the info, and set the pVars->current_program = 0
335     */
336
337    /* AudioObjectType */
338    audioObjectType = (tMP4AudioObjectType)((temp & 0x1f0) >> 4);
339
340    pVars->mc_info.ExtendedAudioObjectType =  audioObjectType;   /* default */
341    /* saving an audioObjectType into a profile field */
342    /* pVars->prog_config.profile = audioObjectType; */
343
344    /* sampling rate index */
345    pVars->prog_config.sampling_rate_idx = temp & 0xf;
346
347    if (pVars->prog_config.sampling_rate_idx > 0xb)
348    {
349        /*
350         *  Only support 12 sampling frequencies from array samp_rate_info ( see sfb.cpp)
351         *  7350 Hz (index 0xc) is not supported, the other indexes are reserved or escape
352         */
353        if (pVars->prog_config.sampling_rate_idx == 0xf) /* escape sequence */
354        {
355            /*
356             * sampling rate not listed in Table 1.6.2,
357             * this release does not support this
358             */
359            /*sampling_rate =  getbits( LEN_SAMP_RATE,
360                                      pInputStream);*/
361            getbits(LEN_SAMP_RATE, pInputStream); /* future use */
362        }
363
364        status = 1;
365    }
366
367    channel_config =  get9_n_lessbits(LEN_CHAN_CONFIG,
368                                      pInputStream);
369
370    if ((channel_config > 2) && (!pVars->aacConfigUtilityEnabled))
371    {
372        /*
373         * AAC lib does not support more than two channels
374         * signal error when in decoder mode
375         * do not test when in utility mode
376         */
377        status = 1;
378
379    }
380
381    if (audioObjectType == MP4AUDIO_SBR || audioObjectType == MP4AUDIO_PS)
382    {
383        /* to disable explicit backward compatiblity check */
384        pVars->mc_info.ExtendedAudioObjectType = MP4AUDIO_SBR;
385        pVars->mc_info.sbrPresentFlag = 1;
386
387        if (audioObjectType == MP4AUDIO_PS)
388        {
389            pVars->mc_info.psPresentFlag = 1;
390            pVars->mc_info.ExtendedAudioObjectType = MP4AUDIO_PS;
391        }
392
393        extensionSamplingFrequencyIndex = /* extensionSamplingFrequencyIndex */
394            get9_n_lessbits(LEN_SAMP_RATE_IDX,
395                            pInputStream);
396        if (extensionSamplingFrequencyIndex == 0x0f)
397        {
398            /*
399             * sampling rate not listed in Table 1.6.2,
400             * this release does not support this
401                */
402            /*sampling_rate = getbits( LEN_SAMP_RATE,
403                                     pInputStream);*/
404            getbits(LEN_SAMP_RATE, pInputStream);
405        }
406
407        audioObjectType = (tMP4AudioObjectType) get9_n_lessbits(LEN_OBJ_TYPE ,
408                          pInputStream);
409    }
410
411
412    if ((/*(audioObjectType == MP4AUDIO_AAC_MAIN)     ||*/
413                (audioObjectType == MP4AUDIO_AAC_LC)        ||
414                /*(audioObjectType == MP4AUDIO_AAC_SSR)       ||*/
415                (audioObjectType == MP4AUDIO_LTP)           /*||*/
416                /*(audioObjectType == MP4AUDIO_AAC_SCALABLE)  ||*/
417                /*(audioObjectType == MP4AUDIO_TWINVQ)*/) && (status == SUCCESS))
418    {
419        status = get_GA_specific_config(pVars,
420                                        pInputStream,
421                                        channel_config,
422                                        audioObjectType);
423
424        /*
425         *  verify that Program config returned a supported audio object type
426         */
427
428        if ((pVars->mc_info.audioObjectType != MP4AUDIO_AAC_LC) &&
429                (pVars->mc_info.audioObjectType != MP4AUDIO_LTP))
430        {
431            return 1;   /* status != SUCCESS invalid aot */
432        }
433    }
434    else
435    {
436        return 1;   /* status != SUCCESS invalid aot or invalid parameter */
437    }
438
439    /*
440     *  SBR tool explicit signaling ( backward compatible )
441     */
442    if (extensionAudioObjectType != MP4AUDIO_SBR)
443    {
444        syncExtensionType = (UInt)get17_n_lessbits(LEN_SYNC_EXTENSION_TYPE,
445                            pInputStream);
446
447        if (syncExtensionType == 0x2b7)
448        {
449            extensionAudioObjectType = get9_n_lessbits( /* extensionAudioObjectType */
450                                           LEN_OBJ_TYPE,
451                                           pInputStream);
452
453            if (extensionAudioObjectType == MP4AUDIO_SBR)
454            {
455                pVars->mc_info.sbrPresentFlag = get1bits(pInputStream);  /* sbrPresentFlag */
456                if (pVars->mc_info.sbrPresentFlag == 1)
457                {
458                    extensionSamplingFrequencyIndex =
459                        get9_n_lessbits( /* extensionSamplingFrequencyIndex */
460                            LEN_SAMP_RATE_IDX,
461                            pInputStream);
462                    if (pVars->aacPlusEnabled == true)
463                    {
464#ifdef AAC_PLUS
465                        pVars->mc_info.upsamplingFactor = (samp_rate_info[extensionSamplingFrequencyIndex].samp_rate >> 1) ==
466                                                          samp_rate_info[pVars->prog_config.sampling_rate_idx].samp_rate ? 2 : 1;
467
468                        if ((Int)extensionSamplingFrequencyIndex == pVars->prog_config.sampling_rate_idx)
469                        {
470                            /*
471                             *  Disable SBR decoding for any sbr-downsampled file whose SF is >= 24 KHz
472                             */
473                            if (pVars->prog_config.sampling_rate_idx < 6)
474                            {
475                                pVars->aacPlusEnabled = false;
476                            }
477
478                            pVars->mc_info.bDownSampledSbr = true;
479                        }
480                        pVars->prog_config.sampling_rate_idx = extensionSamplingFrequencyIndex;
481
482#endif
483                    }
484
485                    if (extensionSamplingFrequencyIndex == 0x0f)
486                    {
487                        /*
488                         * sampling rate not listed in Table 1.6.2,
489                         * this release does not support this
490                         */
491                        /*sampling_rate = getbits( LEN_SAMP_RATE,
492                                                 pInputStream);*/
493                        getbits(LEN_SAMP_RATE, pInputStream);
494                    }
495                    /* syncExtensionType */
496                    syncExtensionType = (UInt)get17_n_lessbits(LEN_SYNC_EXTENSION_TYPE,
497                                        pInputStream);
498                    if (syncExtensionType == 0x548)
499                    {
500                        pVars->mc_info.psPresentFlag = get1bits(pInputStream);  /* psPresentFlag */
501                        if (pVars->mc_info.psPresentFlag)
502                        {
503                            extensionAudioObjectType = MP4AUDIO_PS;
504                        }
505                    }
506                    else
507                    {
508                        /*
509                        * Rewind bitstream pointer so that the syncExtensionType reading has no
510                        * effect when decoding raw bitstream
511                            */
512                        pVars->inputStream.usedBits -= LEN_SYNC_EXTENSION_TYPE;
513                    }
514
515                    pVars->mc_info.ExtendedAudioObjectType = (eMP4AudioObjectType)extensionAudioObjectType;
516                }
517            }
518        }
519        else if (!status)
520        {
521            /*
522             * Rewind bitstream pointer so that the syncExtensionType reading has no
523             * effect when decoding raw bitstream
524             */
525            pVars->inputStream.usedBits -= LEN_SYNC_EXTENSION_TYPE;
526
527#ifdef AAC_PLUS
528
529            /*
530             *  For implicit signalling, no hint that sbr or ps is used, so we need to
531             *  check the sampling frequency of the aac content, if lesser or equal to
532             *  24 KHz, by defualt upsample, otherwise, do nothing
533             */
534            if ((pVars->prog_config.sampling_rate_idx >= 6) && (pVars->aacPlusEnabled == true) &&
535                    audioObjectType == MP4AUDIO_AAC_LC)
536            {
537                pVars->mc_info.upsamplingFactor = 2;
538                pVars->prog_config.sampling_rate_idx -= 3;
539                pVars->mc_info.sbrPresentFlag = 1;
540                pVars->sbrDecoderData.SbrChannel[0].syncState = SBR_NOT_INITIALIZED;
541                pVars->sbrDecoderData.SbrChannel[1].syncState = SBR_NOT_INITIALIZED;
542
543            }
544#endif
545
546        }
547    }
548    else    /*  MP4AUDIO_SBR was detected  */
549    {
550        /*
551         *  Set the real output frequency use by the SBR tool, define tentative upsample ratio
552         */
553        if (pVars->aacPlusEnabled == true)
554        {
555#ifdef AAC_PLUS
556            pVars->mc_info.upsamplingFactor = (samp_rate_info[extensionSamplingFrequencyIndex].samp_rate >> 1) ==
557                                              samp_rate_info[pVars->prog_config.sampling_rate_idx].samp_rate ? 2 : 1;
558
559            if ((Int)extensionSamplingFrequencyIndex == pVars->prog_config.sampling_rate_idx)
560            {
561                /*
562                 *  Disable SBR decoding for any sbr-downsampled file whose SF is >= 24 KHz
563                 */
564                if (pVars->prog_config.sampling_rate_idx < 6)
565                {
566                    pVars->aacPlusEnabled = false;
567                }
568                pVars->mc_info.bDownSampledSbr = true;
569            }
570            pVars->prog_config.sampling_rate_idx = extensionSamplingFrequencyIndex;
571
572
573
574#endif
575
576
577
578
579        }
580
581    }  /*  if ( extensionAudioObjectType != MP4AUDIO_SBR ) */
582
583    /*
584     * The following object types are not supported in this release,
585     * however, keep these interfaces for future implementation
586     */
587
588    /*
589     *if (audioObjectType == MP4AUDIO_CELP)
590     *{
591     *    status = 1;
592     *}
593     */
594
595    /*
596     *if (audioObjectType == MP4AUDIO_HVXC)
597     *{
598     *    status = 1;
599     *}
600     */
601
602    /*
603     *if (audioObjectType == MP4AUDIO_TTSI)
604     *{
605     *    status = 1;
606     *}
607     */
608
609    /*
610     *if ((audioObjectType == 13) || (audioObjectType == 14) ||
611     *   (audioObjectType == 15) || (audioObjectType == 16))
612     *{
613     *    status = 1;
614     *}
615     */
616
617    /* The following objects are Amendment 1 objects */
618    /*
619     *if (((audioObjectType == MP4AUDIO_ER_AAC_LC)       ||
620     *    (audioObjectType == MP4AUDIO_ER_AAC_LTP)      ||
621     *    (audioObjectType == MP4AUDIO_ER_AAC_SCALABLE) ||
622     *    (audioObjectType == MP4AUDIO_ER_TWINVQ)       ||
623     *    (audioObjectType == MP4AUDIO_ER_BSAC)         ||
624     *    (audioObjectType == MP4AUDIO_ER_AAC_LD)) && (status == -1))
625     *{
626     */
627    /*
628     * should call get_GA_specific_config
629     * for this release, do not support Error Resilience
630     * temporary solution is set status flag and exit decoding
631     */
632    /*    status = 1;
633    *}
634    */
635
636    /*
637     *if (audioObjectType == MP4AUDIO_ER_CELP)
638     * {
639     *    status = 1;
640     *}
641     */
642
643    /*
644     *if (audioObjectType == MP4AUDIO_ER_HVXC)
645     *{
646     *    status = 1;
647     *}
648     */
649
650    /*
651     *if ((audioObjectType == MP4AUDIO_ER_HILN) ||
652     *    (audioObjectType == MP4AUDIO_PARAMETRIC))
653     *{
654     *    status = 1;
655     *}
656     */
657
658    /*
659     *if ((audioObjectType == MP4AUDIO_ER_AAC_LC)       ||
660     *    (audioObjectType == MP4AUDIO_ER_AAC_LTP)      ||
661     *    (audioObjectType == MP4AUDIO_ER_AAC_SCALABLE) ||
662     *    (audioObjectType == MP4AUDIO_ER_TWINVQ)       ||
663     *    (audioObjectType == MP4AUDIO_ER_BSAC)         ||
664     *    (audioObjectType == MP4AUDIO_ER_AAC_LD)       ||
665     *    (audioObjectType == MP4AUDIO_ER_CELP)         ||
666     *    (audioObjectType == MP4AUDIO_ER_HVXC)         ||
667     *    (audioObjectType == MP4AUDIO_ER_HILN)         ||
668     *    (audioObjectType == MP4AUDIO_PARAMETRIC))
669     *{
670     */
671    /* error protection config */
672    /*
673     *     epConfig =
674     *       getbits(
675     *           LEN_EP_CONFIG,
676     *           pInputStream);
677     *
678     *   if (epConfig == 2)
679     *   {
680     */
681    /* should call ErrorProtectionSpecificConfig() */
682    /*
683     *       status = 1;
684     *   }
685     *
686     *}
687     */
688
689    return status;
690
691}
692