Home | History | Annotate | Download | only in aacdec
      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   Filename: sbr_applied.c
     21 
     22 ------------------------------------------------------------------------------
     23  REVISION HISTORY
     24 
     25 
     26  Who:                                   Date: MM/DD/YYYY
     27  Description:
     28 
     29 ------------------------------------------------------------------------------
     30  INPUT AND OUTPUT DEFINITIONS
     31 
     32     INPUT
     33 
     34     SBRDECODER self,
     35     SBRBITSTREAM * stream,
     36     float *timeData,
     37     int numChannels
     38 
     39     OUTPUT
     40 
     41     errorCode, noError if successful
     42 
     43 ------------------------------------------------------------------------------
     44  FUNCTION DESCRIPTION
     45 
     46         sbr decoder processing, set up SBR decoder phase 2 in case of
     47         different cotrol data
     48 
     49 ------------------------------------------------------------------------------
     50  REQUIREMENTS
     51 
     52 
     53 ------------------------------------------------------------------------------
     54  REFERENCES
     55 
     56 SC 29 Software Copyright Licencing Disclaimer:
     57 
     58 This software module was originally developed by
     59   Coding Technologies
     60 
     61 and edited by
     62   -
     63 
     64 in the course of development of the ISO/IEC 13818-7 and ISO/IEC 14496-3
     65 standards for reference purposes and its performance may not have been
     66 optimized. This software module is an implementation of one or more tools as
     67 specified by the ISO/IEC 13818-7 and ISO/IEC 14496-3 standards.
     68 ISO/IEC gives users free license to this software module or modifications
     69 thereof for use in products claiming conformance to audiovisual and
     70 image-coding related ITU Recommendations and/or ISO/IEC International
     71 Standards. ISO/IEC gives users the same free license to this software module or
     72 modifications thereof for research purposes and further ISO/IEC standardisation.
     73 Those intending to use this software module in products are advised that its
     74 use may infringe existing patents. ISO/IEC have no liability for use of this
     75 software module or modifications thereof. Copyright is not released for
     76 products that do not conform to audiovisual and image-coding related ITU
     77 Recommendations and/or ISO/IEC International Standards.
     78 The original developer retains full right to modify and use the code for its
     79 own purpose, assign or donate the code to a third party and to inhibit third
     80 parties from using the code for products that do not conform to audiovisual and
     81 image-coding related ITU Recommendations and/or ISO/IEC International Standards.
     82 This copyright notice must be included in all copies or derivative works.
     83 Copyright (c) ISO/IEC 2002.
     84 
     85 ------------------------------------------------------------------------------
     86  PSEUDO-CODE
     87 
     88 ------------------------------------------------------------------------------
     89 */
     90 
     91 
     92 /*----------------------------------------------------------------------------
     93 ; INCLUDES
     94 ----------------------------------------------------------------------------*/
     95 #ifdef AAC_PLUS
     96 
     97 
     98 #include    "sbr_applied.h"
     99 #include    "sbr_read_data.h"
    100 
    101 #include    "sbr_decode_envelope.h"
    102 #include    "decode_noise_floorlevels.h"
    103 #include    "sbr_requantize_envelope_data.h"
    104 #include    "sbr_envelope_unmapping.h"
    105 #include    "sbr_dec.h"
    106 #include    "e_sbr_element_id.h"
    107 #include    "aac_mem_funcs.h"
    108 
    109 #ifdef PARAMETRICSTEREO
    110 #include    "ps_bstr_decoding.h"
    111 #include    "ps_allocate_decoder.h"
    112 
    113 #endif
    114 
    115 #include    "init_sbr_dec.h"
    116 
    117 
    118 /*----------------------------------------------------------------------------
    119 ; MACROS
    120 ; Define module specific macros here
    121 ----------------------------------------------------------------------------*/
    122 
    123 
    124 /*----------------------------------------------------------------------------
    125 ; DEFINES
    126 ; Include all pre-processor statements here. Include conditional
    127 ; compile variables also.
    128 ----------------------------------------------------------------------------*/
    129 #define LEFT  (0)
    130 #define RIGHT (1)
    131 
    132 
    133 /*----------------------------------------------------------------------------
    134 ; LOCAL FUNCTION DEFINITIONS
    135 ; Function Prototype declaration
    136 ----------------------------------------------------------------------------*/
    137 
    138 /*----------------------------------------------------------------------------
    139 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
    140 ; Variable declaration - defined here and used outside this module
    141 ----------------------------------------------------------------------------*/
    142 /*----------------------------------------------------------------------------
    143 ; EXTERNAL FUNCTION REFERENCES
    144 ; Declare functions defined elsewhere and referenced in this module
    145 ----------------------------------------------------------------------------*/
    146 
    147 /*----------------------------------------------------------------------------
    148 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
    149 ; Declare variables used in this module but defined elsewhere
    150 ----------------------------------------------------------------------------*/
    151 
    152 /*----------------------------------------------------------------------------
    153 ; FUNCTION CODE
    154 ----------------------------------------------------------------------------*/
    155 
    156 SBR_ERROR  sbr_applied(SBRDECODER_DATA * self,
    157                        SBRBITSTREAM * stream,
    158                        Int16 *ch_left,
    159                        Int16 *ch_right,
    160                        Int16 *timeData,
    161                        SBR_DEC *sbrDec,
    162                        tDec_Int_File  *pVars,
    163                        Int32 numChannels)
    164 {
    165     SBR_ERROR err = SBRDEC_OK ;
    166 
    167     Int32 eleChannels = 0;
    168 
    169     SBR_CHANNEL *SbrChannel = self->SbrChannel;
    170 
    171     /* Get SBR or PS Data only when available */
    172     if (stream->NrElements)
    173     {
    174         /* read frame data from bitstream */
    175 
    176         err = sbr_read_data(self,
    177                             sbrDec,
    178                             stream);
    179 
    180         if (err != SBRDEC_OK)
    181         {
    182             /*
    183              * This error condition disables any further SBR processing
    184              */
    185             self->SbrChannel[LEFT].syncState = UPSAMPLING;
    186             if (eleChannels == 2)
    187             {
    188                 self->SbrChannel[RIGHT].syncState = UPSAMPLING;
    189             }
    190         }
    191 
    192         /*
    193          *  Setting bistream and decoding type is only done once,
    194          */
    195         if (SbrChannel[LEFT].syncState == SBR_ACTIVE && self->setStreamType)
    196         {
    197             self->setStreamType = 0;  /* Disable Lock for AAC stream type setting  */
    198 
    199 #ifdef HQ_SBR
    200 #ifdef PARAMETRICSTEREO
    201 
    202             Int sbrEnablePS = self->hParametricStereoDec->psDetected;
    203 
    204             pVars->mc_info.psPresentFlag  = sbrEnablePS;
    205 
    206             if (sbrEnablePS)   /* Initialize PS arrays */
    207             {
    208                 pVars->mc_info.ExtendedAudioObjectType = MP4AUDIO_PS;
    209                 ps_allocate_decoder(self, 32);
    210 
    211                 /* Disable LC (or Enable HQ)  if PS is detected */
    212                 sbrDec->LC_aacP_DecoderFlag = OFF;
    213             }
    214             else
    215             {
    216                 /*
    217                  *  Do not downgrade stream type from eaac+, if it has been explicitly declared
    218                  */
    219                 if (pVars->mc_info.ExtendedAudioObjectType != MP4AUDIO_PS)
    220                 {
    221                     pVars->mc_info.ExtendedAudioObjectType = MP4AUDIO_SBR;
    222 
    223                     if (pVars->mc_info.nch > 1)
    224                     {
    225                         sbrDec->LC_aacP_DecoderFlag = ON;    /* Enable LC for stereo */
    226                     }
    227                     else
    228                     {
    229                         sbrDec->LC_aacP_DecoderFlag = OFF;    /* Disable LC, Enable HQ for mono */
    230                     }
    231                 }
    232                 else
    233                 {
    234                     sbrEnablePS = 1;  /* Force this condition as it was explicititly declared */
    235                     pVars->mc_info.psPresentFlag  = sbrEnablePS;
    236 
    237                 }
    238             }
    239 #else
    240 
    241             pVars->mc_info.ExtendedAudioObjectType = MP4AUDIO_SBR;
    242 
    243             if (pVars->mc_info.nch > 1)
    244             {
    245                 sbrDec->LC_aacP_DecoderFlag = ON;    /* Enable LC for stereo */
    246             }
    247             else
    248             {
    249                 sbrDec->LC_aacP_DecoderFlag = OFF;    /* Disable LC, Enable HQ for mono */
    250             }
    251 #endif
    252 
    253 #else
    254             pVars->mc_info.ExtendedAudioObjectType = MP4AUDIO_SBR;
    255 
    256             sbrDec->LC_aacP_DecoderFlag = ON;       /* Enable LC for all sbr decoding */
    257 
    258 #endif
    259 
    260         }   /*   (SbrChannel[LEFT].syncState == SBR_ACTIVE && lock)  */
    261         else
    262         {
    263             /*
    264              *  Default setting for upsampler
    265              */
    266             if (pVars->mc_info.ExtendedAudioObjectType == MP4AUDIO_AAC_LC)
    267             {
    268                 /*
    269                  *  Change only in implicit signalling, otherwise keep original declaration
    270                  */
    271                 pVars->mc_info.ExtendedAudioObjectType = MP4AUDIO_SBR;
    272             }
    273 
    274 #ifdef HQ_SBR
    275             if (pVars->mc_info.nch > 1)
    276             {
    277                 sbrDec->LC_aacP_DecoderFlag = ON;    /* Enable LC for stereo */
    278             }
    279             else
    280             {
    281                 sbrDec->LC_aacP_DecoderFlag = OFF;    /* Disable LC, Enable HQ for mono */
    282             }
    283 #else
    284             sbrDec->LC_aacP_DecoderFlag = ON;       /* Enable LC for all sbr decoding */
    285 
    286 #endif
    287             /* mask error and let upsampler run */
    288             err = SBRDEC_OK;
    289 
    290         }
    291 
    292         /* decoding */
    293         eleChannels = (stream->sbrElement [LEFT].ElementID == SBR_ID_CPE) ? 2 : 1;
    294 
    295         if (SbrChannel[LEFT].syncState == SBR_ACTIVE)
    296         {
    297 
    298             sbr_decode_envelope(&(SbrChannel[LEFT].frameData));
    299 
    300             decode_noise_floorlevels(&(SbrChannel[LEFT].frameData));
    301 
    302             if (! SbrChannel[LEFT].frameData.coupling)
    303             {
    304                 sbr_requantize_envelope_data(&(SbrChannel[LEFT].frameData));
    305             }
    306 
    307             if (eleChannels == 2)
    308             {
    309 
    310                 sbr_decode_envelope(&(SbrChannel[RIGHT].frameData));
    311 
    312                 decode_noise_floorlevels(&(SbrChannel[RIGHT].frameData));
    313 
    314                 if (SbrChannel[RIGHT].frameData.coupling)
    315                 {
    316                     sbr_envelope_unmapping(&(SbrChannel[ LEFT].frameData),
    317                                            &(SbrChannel[RIGHT].frameData));
    318                 }
    319                 else
    320                 {
    321                     sbr_requantize_envelope_data(&(SbrChannel[RIGHT].frameData));
    322                 }
    323             }
    324         }
    325         else            /* enable upsampling until valid SBR is obtained */
    326         {
    327             /*
    328              *  Incomplete sbr frame, or disabled SBR section
    329              *  Set the decoder to act as a regular upsampler
    330              */
    331 
    332             init_sbr_dec((sbrDec->outSampleRate >> 1),
    333                          pVars->mc_info.upsamplingFactor,
    334                          sbrDec,
    335                          &(self->SbrChannel[LEFT].frameData));
    336 
    337             if ((eleChannels == 2) && (SbrChannel[RIGHT].syncState != SBR_ACTIVE))
    338             {
    339                 init_sbr_dec((sbrDec->outSampleRate >> 1),
    340                              pVars->mc_info.upsamplingFactor,
    341                              sbrDec,
    342                              &(self->SbrChannel[RIGHT].frameData));
    343 
    344             }
    345 
    346         }
    347 
    348     }
    349 
    350 
    351 #ifdef HQ_SBR
    352 #ifdef PARAMETRICSTEREO
    353     if (pVars->mc_info.ExtendedAudioObjectType == MP4AUDIO_PS)
    354     {
    355         ps_bstr_decoding(self->hParametricStereoDec);
    356         /* allocate pointer for rigth channel qmf filter history  */
    357         Int16 *tempInt16Ptr = (Int16 *)SbrChannel[RIGHT].frameData.V;
    358         self->hParametricStereoDec->R_ch_qmf_filter_history = (Int32 *)tempInt16Ptr;
    359 
    360 
    361         /*
    362          * 1824 (48*38) Int32 needed by each matrix sbrQmfBufferReal, sbrQmfBufferImag
    363          * pVars->share.predictedSamples  has 2048 available
    364          * pVars->fxpCoef[1]  has 2048 available
    365          */
    366         SbrChannel[LEFT].frameData.sbrQmfBufferReal = pVars->share.predictedSamples;
    367         SbrChannel[LEFT].frameData.sbrQmfBufferImag = &pVars->fxpCoef[0][920];
    368 
    369         sbr_dec(ch_left,
    370                 timeData,
    371                 &(SbrChannel[LEFT].frameData),
    372                 (SbrChannel[LEFT].syncState == SBR_ACTIVE),
    373                 sbrDec,
    374                 &timeData[RIGHT],
    375                 self->hParametricStereoDec,
    376                 pVars);
    377     }
    378     else
    379     {
    380 #endif
    381 #endif
    382 
    383         SbrChannel[LEFT].frameData.sbrQmfBufferReal = pVars->fxpCoef[LEFT];
    384 #ifdef HQ_SBR
    385         SbrChannel[LEFT].frameData.sbrQmfBufferImag = pVars->fxpCoef[RIGHT];
    386 #endif
    387 
    388         sbr_dec(ch_left,
    389                 timeData,
    390                 &(SbrChannel[LEFT].frameData),
    391                 (SbrChannel[LEFT].syncState == SBR_ACTIVE),
    392                 sbrDec,
    393 #ifdef HQ_SBR
    394 #ifdef PARAMETRICSTEREO
    395                 NULL,
    396                 NULL,
    397 #endif
    398 #endif
    399                 pVars);
    400 
    401         if (numChannels == 2)
    402         {
    403             SbrChannel[RIGHT].frameData.sbrQmfBufferReal = pVars->fxpCoef[LEFT];
    404 #ifdef HQ_SBR
    405             SbrChannel[RIGHT].frameData.sbrQmfBufferImag = pVars->fxpCoef[RIGHT];
    406 #endif
    407 
    408             sbr_dec(ch_right,
    409                     &timeData[RIGHT],
    410                     &(SbrChannel[RIGHT].frameData),
    411                     (SbrChannel[RIGHT].syncState == SBR_ACTIVE),
    412                     sbrDec,
    413 #ifdef HQ_SBR
    414 #ifdef PARAMETRICSTEREO
    415                     NULL,
    416                     NULL,
    417 #endif
    418 #endif
    419                     pVars);
    420 
    421         }
    422 
    423 
    424 #ifdef HQ_SBR
    425 #ifdef PARAMETRICSTEREO
    426     }
    427 #endif
    428 #endif
    429 
    430     return err;
    431 }
    432 
    433 
    434 #endif
    435 
    436