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: ps_decorrelate.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 
     33 
     34 ------------------------------------------------------------------------------
     35  FUNCTION DESCRIPTION
     36 
     37   Decorrelation
     38   Decorrelation is achieved by means of all-pass filtering and delaying
     39   Sub-band samples s_k(n) are converted into de-correlated sub-bands samples
     40   d_k(n). k index for frequency, n time index
     41 
     42 
     43      _______                                              ________
     44     |       |                                  _______   |        |
     45   ->|Hybrid | LF ----                         |       |->| Hybrid |-->
     46     | Anal. |        |                        |       |  | Synth  |   QMF -> L
     47      -------         o----------------------->|       |   --------    Synth
     48 QMF                  |                s_k(n)  |Stereo |-------------->
     49 Anal.              -------------------------->|       |
     50      _______       | |                        |       |   ________
     51     |       | HF --o |   -----------          |Process|  |        |
     52   ->| Delay |      |  ->|           |-------->|       |->| Hybrid |-->
     53      -------       |    |decorrelate| d_k(n)  |       |  | Synth  |   QMF -> R
     54                    ---->|           |-------->|       |   --------    Synth
     55                          -----------          |_______|-------------->
     56 
     57 
     58   Delay is introduced to compensate QMF bands not passed through Hybrid
     59   Analysis
     60 
     61 ------------------------------------------------------------------------------
     62  REQUIREMENTS
     63 
     64 
     65 ------------------------------------------------------------------------------
     66  REFERENCES
     67 
     68 SC 29 Software Copyright Licencing Disclaimer:
     69 
     70 This software module was originally developed by
     71   Coding Technologies
     72 
     73 and edited by
     74   -
     75 
     76 in the course of development of the ISO/IEC 13818-7 and ISO/IEC 14496-3
     77 standards for reference purposes and its performance may not have been
     78 optimized. This software module is an implementation of one or more tools as
     79 specified by the ISO/IEC 13818-7 and ISO/IEC 14496-3 standards.
     80 ISO/IEC gives users free license to this software module or modifications
     81 thereof for use in products claiming conformance to audiovisual and
     82 image-coding related ITU Recommendations and/or ISO/IEC International
     83 Standards. ISO/IEC gives users the same free license to this software module or
     84 modifications thereof for research purposes and further ISO/IEC standardisation.
     85 Those intending to use this software module in products are advised that its
     86 use may infringe existing patents. ISO/IEC have no liability for use of this
     87 software module or modifications thereof. Copyright is not released for
     88 products that do not conform to audiovisual and image-coding related ITU
     89 Recommendations and/or ISO/IEC International Standards.
     90 The original developer retains full right to modify and use the code for its
     91 own purpose, assign or donate the code to a third party and to inhibit third
     92 parties from using the code for products that do not conform to audiovisual and
     93 image-coding related ITU Recommendations and/or ISO/IEC International Standards.
     94 This copyright notice must be included in all copies or derivative works.
     95 Copyright (c) ISO/IEC 2003.
     96 
     97 ------------------------------------------------------------------------------
     98  PSEUDO-CODE
     99 
    100 ------------------------------------------------------------------------------
    101 */
    102 
    103 
    104 /*----------------------------------------------------------------------------
    105 ; INCLUDES
    106 ----------------------------------------------------------------------------*/
    107 
    108 #ifdef AAC_PLUS
    109 
    110 #ifdef PARAMETRICSTEREO
    111 #include    "pv_audio_type_defs.h"
    112 #include    "ps_decorrelate.h"
    113 #include    "aac_mem_funcs.h"
    114 #include    "ps_all_pass_filter_coeff.h"
    115 #include    "ps_pwr_transient_detection.h"
    116 #include    "ps_all_pass_fract_delay_filter.h"
    117 #include    "fxp_mul32.h"
    118 
    119 /*----------------------------------------------------------------------------
    120 ; MACROS
    121 ; Define module specific macros here
    122 ----------------------------------------------------------------------------*/
    123 
    124 #ifndef min
    125 #define min(a, b) ((a) < (b) ? (a) : (b))
    126 #endif
    127 
    128 
    129 /*----------------------------------------------------------------------------
    130 ; DEFINES
    131 ; Include all pre-processor statements here. Include conditional
    132 ; compile variables also.
    133 ----------------------------------------------------------------------------*/
    134 
    135 /*----------------------------------------------------------------------------
    136 ; LOCAL FUNCTION DEFINITIONS
    137 ; Function Prototype declaration
    138 ----------------------------------------------------------------------------*/
    139 
    140 
    141 /*----------------------------------------------------------------------------
    142 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
    143 ; Variable declaration - defined here and used outside this module
    144 ----------------------------------------------------------------------------*/
    145 
    146 
    147 /*----------------------------------------------------------------------------
    148 ; EXTERNAL FUNCTION REFERENCES
    149 ; Declare functions defined elsewhere and referenced in this module
    150 ----------------------------------------------------------------------------*/
    151 
    152 
    153 
    154 
    155 /*----------------------------------------------------------------------------
    156 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
    157 ; Declare variables used in this module but defined elsewhere
    158 ----------------------------------------------------------------------------*/
    159 
    160 
    161 /*----------------------------------------------------------------------------
    162 ; FUNCTION CODE
    163 ----------------------------------------------------------------------------*/
    164 void ps_decorrelate(STRUCT_PS_DEC *h_ps_dec,
    165                     Int32 *rIntBufferLeft,
    166                     Int32 *iIntBufferLeft,
    167                     Int32 *rIntBufferRight,
    168                     Int32 *iIntBufferRight,
    169                     Int32 scratch_mem[])
    170 {
    171     Int32 sb;
    172     Int32 maxsb;
    173     Int32 gr;
    174     Int32 sb_delay;
    175     Int32 bin;
    176 
    177 
    178 
    179 
    180     Int32 *aLeftReal;
    181     Int32 *aLeftImag;
    182     Int32 *aRightReal;
    183     Int32 *aRightImag;
    184 
    185     Int32 *aTransRatio = scratch_mem;   /* use  NO_BINS == 20 */
    186 
    187 
    188     Int32 ***pppRealDelayRBufferSer;
    189     Int32 ***pppImagDelayRBufferSer;
    190 
    191     Int32 **ppRealDelayBuffer;
    192     Int32 **ppImagDelayBuffer;
    193 
    194     const Int32(*ppFractDelayPhaseFactorSer)[3];
    195     /*
    196      *  Power transient estimation and detection
    197      */
    198 
    199 
    200     ps_pwr_transient_detection(h_ps_dec,
    201                                rIntBufferLeft,
    202                                iIntBufferLeft,
    203                                aTransRatio);
    204 
    205 
    206     aLeftReal = h_ps_dec->mHybridRealLeft;
    207     aLeftImag = h_ps_dec->mHybridImagLeft;
    208     aRightReal = h_ps_dec->mHybridRealRight;
    209     aRightImag = h_ps_dec->mHybridImagRight;
    210 
    211     pppRealDelayRBufferSer = h_ps_dec->aaaRealDelayRBufferSerSubQmf;
    212     pppImagDelayRBufferSer = h_ps_dec->aaaImagDelayRBufferSerSubQmf;
    213 
    214     ppRealDelayBuffer = h_ps_dec->aaRealDelayBufferSubQmf;
    215     ppImagDelayBuffer = h_ps_dec->aaImagDelayBufferSubQmf;
    216 
    217 
    218 
    219     ppFractDelayPhaseFactorSer = aaFractDelayPhaseFactorSerSubQmf;
    220 
    221 
    222     /*
    223      *   NO_IID_GROUPS (SUBQMF_GROUPS (12) + QMF_GROUPS (10)) == 22
    224      */
    225 
    226     for (gr = 0; gr < SUBQMF_GROUPS; gr++)      /*  0 to 9 */
    227     {
    228         Int32 rIn;
    229         Int32 iIn;
    230         Int32 *pt_rTmp;
    231         Int32 *pt_iTmp;
    232         Int32 rTmp;
    233         Int32 cmplx;
    234         Int32 tmp1, tmp2;
    235 
    236         /* sb = subQMF/QMF subband */
    237 
    238         sb = groupBorders[gr];
    239 
    240         /*
    241          *  For lower subbands
    242          *  Apply all-pass filtering
    243          *
    244          */
    245         pt_rTmp = &ppRealDelayBuffer[sb][h_ps_dec->delayBufIndex];
    246         pt_iTmp = &ppImagDelayBuffer[sb][h_ps_dec->delayBufIndex];
    247 
    248         tmp1 = aLeftReal[sb];
    249         tmp2 = aLeftImag[sb];
    250         rIn = *pt_rTmp >> 1;
    251         iIn = *pt_iTmp >> 1;
    252 
    253 
    254         *pt_rTmp = tmp1;
    255         *pt_iTmp = tmp2;
    256 
    257         /*
    258          *  Fractional delay vector
    259          *
    260          *  phi_fract(k) = exp(-j*pi*q_phi*f_center(k))       0<= k <= SUBQMF_GROUPS
    261          *
    262          *  q_phi = 0.39
    263          *  f_center(k) frequency vector
    264          */
    265 
    266         cmplx =  aFractDelayPhaseFactorSubQmf[sb];
    267 
    268         aRightReal[sb]  = cmplx_mul32_by_16(rIn, -iIn, cmplx);
    269         aRightImag[sb]  = cmplx_mul32_by_16(iIn,  rIn, cmplx);
    270 
    271         ps_all_pass_fract_delay_filter_type_I(h_ps_dec->aDelayRBufIndexSer,
    272                                               sb,
    273                                               ppFractDelayPhaseFactorSer[sb],
    274                                               pppRealDelayRBufferSer,
    275                                               pppImagDelayRBufferSer,
    276                                               &aRightReal[sb],
    277                                               &aRightImag[sb]);
    278 
    279         bin = bins2groupMap[gr];
    280         rTmp = aTransRatio[bin];
    281 
    282         if (rTmp != 0x7FFFFFFF)
    283         {
    284             aRightReal[sb] = fxp_mul32_Q31(rTmp, aRightReal[sb]) << 1;
    285             aRightImag[sb] = fxp_mul32_Q31(rTmp, aRightImag[sb]) << 1;
    286         }
    287 
    288 
    289     } /* gr */
    290 
    291     aLeftReal = rIntBufferLeft;
    292     aLeftImag = iIntBufferLeft;
    293     aRightReal = rIntBufferRight;
    294     aRightImag = iIntBufferRight;
    295 
    296     pppRealDelayRBufferSer = h_ps_dec->aaaRealDelayRBufferSerQmf;
    297     pppImagDelayRBufferSer = h_ps_dec->aaaImagDelayRBufferSerQmf;
    298 
    299     ppRealDelayBuffer = h_ps_dec->aaRealDelayBufferQmf;
    300     ppImagDelayBuffer = h_ps_dec->aaImagDelayBufferQmf;
    301 
    302 
    303 
    304     ppFractDelayPhaseFactorSer = aaFractDelayPhaseFactorSerQmf;
    305 
    306 
    307     for (gr = SUBQMF_GROUPS; gr < NO_BINS; gr++)     /* 10 to 20 */
    308     {
    309 
    310         maxsb = min(h_ps_dec->usb, groupBorders[gr+1]);
    311 
    312         /* sb = subQMF/QMF subband */
    313 
    314         for (sb = groupBorders[gr]; sb < maxsb; sb++)
    315         {
    316 
    317             Int32 rIn, iIn;
    318             Int32 *pt_rTmp, *pt_iTmp;
    319             Int32 cmplx;
    320             Int32 tmp1, tmp2;
    321             Int32 rTmp;
    322 
    323 
    324             sb_delay = sb - NO_QMF_CHANNELS_IN_HYBRID;  /* NO_QMF_CHANNELS_IN_HYBRID == 3 */
    325 
    326             /*
    327              *  For lower subbands
    328              *  Apply all-pass filtering
    329              *
    330              */
    331             pt_rTmp = &ppRealDelayBuffer[sb_delay][h_ps_dec->delayBufIndex];
    332             pt_iTmp = &ppImagDelayBuffer[sb_delay][h_ps_dec->delayBufIndex];
    333 
    334             rIn = *pt_rTmp >> 1;
    335             iIn = *pt_iTmp >> 1;
    336 
    337             tmp1 = aLeftReal[sb];
    338             tmp2 = aLeftImag[sb];
    339             *pt_rTmp = tmp1;
    340             *pt_iTmp = tmp2;
    341 
    342             /*
    343              *  Fractional delay vector
    344              *
    345              *  phi_fract(k) = exp(-j*pi*q_phi*f_center(k))       0<= k <= SUBQMF_GROUPS
    346              *
    347              *  q_phi = 0.39
    348              *  f_center(k) frequency vector
    349              */
    350 
    351             cmplx =  aFractDelayPhaseFactor[sb_delay];
    352             aRightReal[sb] = cmplx_mul32_by_16(rIn, -iIn, cmplx);
    353             aRightImag[sb] = cmplx_mul32_by_16(iIn,  rIn, cmplx);
    354 
    355             ps_all_pass_fract_delay_filter_type_II(h_ps_dec->aDelayRBufIndexSer,
    356                                                    sb_delay,
    357                                                    ppFractDelayPhaseFactorSer[sb_delay],
    358                                                    pppRealDelayRBufferSer,
    359                                                    pppImagDelayRBufferSer,
    360                                                    &aRightReal[sb],
    361                                                    &aRightImag[sb],
    362                                                    sb);
    363 
    364             rTmp = aTransRatio[gr-2];
    365             if (rTmp != 0x7FFFFFFF)
    366             {
    367                 aRightReal[sb] = fxp_mul32_Q31(rTmp, aRightReal[sb]) << 1;
    368                 aRightImag[sb] = fxp_mul32_Q31(rTmp, aRightImag[sb]) << 1;
    369             }
    370 
    371 
    372         } /* sb */
    373 
    374     }
    375 
    376 
    377     maxsb = min(h_ps_dec->usb, 35);  /*  35 == groupBorders[NO_BINS + 1] */
    378 
    379     /* sb = subQMF/QMF subband */
    380     {
    381         Int32 factor = aTransRatio[NO_BINS-2];
    382 
    383         for (sb = 23; sb < maxsb; sb++)    /*  23 == groupBorders[NO_BINS] */
    384         {
    385 
    386             Int32  tmp, tmp2;
    387             Int32 *pt_rTmp, *pt_iTmp;
    388 
    389             sb_delay = sb - NO_QMF_CHANNELS_IN_HYBRID;  /*  == 3 */
    390 
    391             /*
    392              *  For the Upper Bands apply delay only
    393              *                          -D(k)
    394              *  Apply Delay   H_k(z) = z         , D(k) == 1 or 14
    395              *
    396              */
    397             Int32 k = sb - NO_ALLPASS_CHANNELS;  /* == 23 */
    398 
    399 
    400             pt_rTmp = &ppRealDelayBuffer[sb_delay][h_ps_dec->aDelayBufIndex[ k]];
    401             pt_iTmp = &ppImagDelayBuffer[sb_delay][h_ps_dec->aDelayBufIndex[ k]];
    402 
    403             if (++h_ps_dec->aDelayBufIndex[ k] >= LONG_DELAY)     /* == 14 */
    404             {
    405                 h_ps_dec->aDelayBufIndex[ k] = 0;
    406             }
    407 
    408 
    409             tmp  = *pt_rTmp;
    410             tmp2 = *pt_iTmp;
    411 
    412             if (aTransRatio[NO_BINS-2] < 0x7FFFFFFF)
    413             {
    414                 aRightReal[sb] = fxp_mul32_Q31(factor, tmp) << 1;
    415                 aRightImag[sb] = fxp_mul32_Q31(factor, tmp2) << 1;
    416             }
    417             else
    418             {
    419                 aRightReal[sb] = tmp;
    420                 aRightImag[sb] = tmp2;
    421             }
    422 
    423 
    424             tmp  = aLeftReal[sb];
    425             tmp2 = aLeftImag[sb];
    426             *pt_rTmp = tmp;
    427             *pt_iTmp = tmp2;
    428 
    429 
    430         } /* sb */
    431     }
    432 
    433 
    434     maxsb = min(h_ps_dec->usb, 64);     /*  64 == groupBorders[NO_BINS+2] */
    435 
    436     /* sb = subQMF/QMF subband */
    437 
    438     {
    439 
    440         for (sb = 35; sb < maxsb; sb++)    /*  35 == groupBorders[NO_BINS+1] */
    441         {
    442 
    443             Int32 *pt_rTmp, *pt_iTmp;
    444 
    445             sb_delay = sb - NO_QMF_CHANNELS_IN_HYBRID;  /*  == 3 */
    446 
    447             /*
    448              *  For the Upper Bands apply delay only
    449              *                          -D(k)
    450              *  Apply Delay   H_k(z) = z         , D(k) == 1 or 14
    451              *
    452              */
    453 
    454             pt_rTmp = &ppRealDelayBuffer[sb_delay][0];
    455             pt_iTmp = &ppImagDelayBuffer[sb_delay][0];
    456 
    457             aRightReal[sb] = *pt_rTmp;
    458             aRightImag[sb] = *pt_iTmp;
    459 
    460 
    461             if (aTransRatio[NO_BINS-1] < 0x7FFFFFFF)
    462             {
    463                 aRightReal[sb] = fxp_mul32_Q31(aTransRatio[NO_BINS-1], aRightReal[sb]) << 1;
    464                 aRightImag[sb] = fxp_mul32_Q31(aTransRatio[NO_BINS-1], aRightImag[sb]) << 1;
    465             }
    466 
    467             *pt_rTmp = aLeftReal[sb];
    468             *pt_iTmp = aLeftImag[sb];
    469 
    470 
    471         } /* sb */
    472     }
    473 
    474 
    475     if (++h_ps_dec->delayBufIndex >= DELAY_ALLPASS)
    476     {
    477         h_ps_dec->delayBufIndex = 0;
    478     }
    479 
    480     if (++h_ps_dec->aDelayRBufIndexSer[0] >= 3)
    481     {
    482         h_ps_dec->aDelayRBufIndexSer[0] = 0;
    483     }
    484     if (++h_ps_dec->aDelayRBufIndexSer[1] >= 4)
    485     {
    486         h_ps_dec->aDelayRBufIndexSer[1] = 0;
    487     }
    488     if (++h_ps_dec->aDelayRBufIndexSer[2] >= 5)
    489     {
    490         h_ps_dec->aDelayRBufIndexSer[2] = 0;
    491     }
    492 
    493 
    494 } /* END deCorrelate */
    495 #endif
    496 
    497 
    498 #endif
    499 
    500