Home | History | Annotate | Download | only in src
      1 
      2 /* -----------------------------------------------------------------------------------------------------------
      3 Software License for The Fraunhofer FDK AAC Codec Library for Android
      4 
      5  Copyright  1995 - 2012 Fraunhofer-Gesellschaft zur Frderung der angewandten Forschung e.V.
      6   All rights reserved.
      7 
      8  1.    INTRODUCTION
      9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
     10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
     11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
     12 
     13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
     14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
     15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
     16 of the MPEG specifications.
     17 
     18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
     19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
     20 individually for the purpose of encoding or decoding bit streams in products that are compliant with
     21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
     22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
     23 software may already be covered under those patent licenses when it is used for those licensed purposes only.
     24 
     25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
     26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
     27 applications information and documentation.
     28 
     29 2.    COPYRIGHT LICENSE
     30 
     31 Redistribution and use in source and binary forms, with or without modification, are permitted without
     32 payment of copyright license fees provided that you satisfy the following conditions:
     33 
     34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
     35 your modifications thereto in source code form.
     36 
     37 You must retain the complete text of this software license in the documentation and/or other materials
     38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
     39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
     40 modifications thereto to recipients of copies in binary form.
     41 
     42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
     43 prior written permission.
     44 
     45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
     46 software or your modifications thereto.
     47 
     48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
     49 and the date of any change. For modified versions of the FDK AAC Codec, the term
     50 "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
     51 "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
     52 
     53 3.    NO PATENT LICENSE
     54 
     55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
     56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
     57 respect to this software.
     58 
     59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
     60 by appropriate patent licenses.
     61 
     62 4.    DISCLAIMER
     63 
     64 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
     65 "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
     66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
     68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
     69 or business interruption, however caused and on any theory of liability, whether in contract, strict
     70 liability, or tort (including negligence), arising in any way out of the use of this software, even if
     71 advised of the possibility of such damage.
     72 
     73 5.    CONTACT INFORMATION
     74 
     75 Fraunhofer Institute for Integrated Circuits IIS
     76 Attention: Audio and Multimedia Departments - FDK AAC LL
     77 Am Wolfsmantel 33
     78 91058 Erlangen, Germany
     79 
     80 www.iis.fraunhofer.de/amm
     81 amm-info (at) iis.fraunhofer.de
     82 ----------------------------------------------------------------------------------------------------------- */
     83 
     84 /*****************************  MPEG-4 AAC Decoder  **************************
     85 
     86    Author(s):   Christian Griebel
     87    Description: Dynamic range control (DRC) decoder tool for SBR
     88 
     89 ******************************************************************************/
     90 
     91 #include "sbrdec_drc.h"
     92 
     93 
     94 /* DRC - Offset table for QMF interpolation. */
     95 static const int offsetTab[2][16] =
     96 {
     97   { 0, 4, 8, 12, 16, 20, 24, 28, 0, 0, 0, 0, 0, 0, 0, 0 },  /* 1024 framing */
     98   { 0, 4, 8, 12, 16, 19, 22, 26, 0, 0, 0, 0, 0, 0, 0, 0 }   /*  960 framing */
     99 };
    100 
    101 /*!
    102   \brief Initialize DRC QMF factors
    103 
    104   \hDrcData Handle to DRC channel data.
    105 
    106   \return none
    107 */
    108 void sbrDecoder_drcInitChannel (
    109     HANDLE_SBR_DRC_CHANNEL  hDrcData )
    110 {
    111   int band;
    112 
    113   if (hDrcData == NULL) {
    114     return;
    115   }
    116 
    117   for (band = 0; band < (64); band++) {
    118     hDrcData->prevFact_mag[band] = FL2FXCONST_DBL(0.5f);
    119   }
    120 
    121   for (band = 0; band < SBRDEC_MAX_DRC_BANDS; band++) {
    122     hDrcData->currFact_mag[band] = FL2FXCONST_DBL(0.5f);
    123     hDrcData->nextFact_mag[band] = FL2FXCONST_DBL(0.5f);
    124   }
    125 
    126   hDrcData->prevFact_exp = 1;
    127   hDrcData->currFact_exp = 1;
    128   hDrcData->nextFact_exp = 1;
    129 
    130   hDrcData->numBandsCurr = 0;
    131   hDrcData->numBandsNext = 0;
    132 
    133   hDrcData->winSequenceCurr = 0;
    134   hDrcData->winSequenceNext = 0;
    135 
    136   hDrcData->drcInterpolationSchemeCurr = 0;
    137   hDrcData->drcInterpolationSchemeNext = 0;
    138 
    139   hDrcData->enable = 0;
    140 }
    141 
    142 
    143 /*!
    144   \brief Swap DRC QMF scaling factors after they have been applied.
    145 
    146   \hDrcData Handle to DRC channel data.
    147 
    148   \return none
    149 */
    150 void sbrDecoder_drcUpdateChannel (
    151     HANDLE_SBR_DRC_CHANNEL  hDrcData )
    152 {
    153   if (hDrcData == NULL) {
    154     return;
    155   }
    156   if (hDrcData->enable != 1) {
    157     return;
    158   }
    159 
    160   /* swap previous data */
    161   FDKmemcpy( hDrcData->currFact_mag,
    162              hDrcData->nextFact_mag,
    163              SBRDEC_MAX_DRC_BANDS * sizeof(FIXP_DBL) );
    164 
    165   hDrcData->currFact_exp = hDrcData->nextFact_exp;
    166 
    167   hDrcData->numBandsCurr = hDrcData->numBandsNext;
    168 
    169   FDKmemcpy( hDrcData->bandTopCurr,
    170              hDrcData->bandTopNext,
    171              SBRDEC_MAX_DRC_BANDS * sizeof(USHORT) );
    172 
    173   hDrcData->drcInterpolationSchemeCurr = hDrcData->drcInterpolationSchemeNext;
    174 
    175   hDrcData->winSequenceCurr = hDrcData->winSequenceNext;
    176 }
    177 
    178 
    179 /*!
    180   \brief Apply DRC factors slot based.
    181 
    182   \hDrcData Handle to DRC channel data.
    183   \qmfRealSlot Pointer to real valued QMF data of one time slot.
    184   \qmfImagSlot Pointer to the imaginary QMF data of one time slot.
    185   \col Number of the time slot.
    186   \numQmfSubSamples Total number of time slots for one frame.
    187   \scaleFactor Pointer to the out scale factor of the time slot.
    188 
    189   \return None.
    190 */
    191 void sbrDecoder_drcApplySlot (
    192     HANDLE_SBR_DRC_CHANNEL  hDrcData,
    193     FIXP_DBL   *qmfRealSlot,
    194     FIXP_DBL   *qmfImagSlot,
    195     int  col,
    196     int  numQmfSubSamples,
    197     int  maxShift
    198   )
    199 {
    200   const int *offset;
    201 
    202   int band, bottomMdct, topMdct, bin, useLP;
    203   int indx = numQmfSubSamples - (numQmfSubSamples >> 1) - 10;   /* l_border */
    204   int frameLenFlag = (numQmfSubSamples == 30) ? 1 : 0;
    205 
    206   const FIXP_DBL *fact_mag = NULL;
    207   INT fact_exp = 0;
    208   UINT numBands = 0;
    209   USHORT *bandTop = NULL;
    210   int shortDrc = 0;
    211 
    212   FIXP_DBL alphaValue = FL2FXCONST_DBL(0.0f);
    213 
    214   if (hDrcData == NULL) {
    215     return;
    216   }
    217   if (hDrcData->enable != 1) {
    218     return;
    219   }
    220 
    221   offset = offsetTab[frameLenFlag];
    222 
    223   useLP = (qmfImagSlot == NULL) ? 1 : 0;
    224 
    225   col += indx;
    226   bottomMdct = 0;
    227   bin = 0;
    228 
    229   /* get respective data and calc interpolation factor */
    230   if (col < (numQmfSubSamples>>1)) {  /* first half of current frame */
    231     if (hDrcData->winSequenceCurr != 2) { /* long window */
    232       int j = col + (numQmfSubSamples>>1);
    233 
    234       if (hDrcData->drcInterpolationSchemeCurr == 0) {
    235         INT k = (frameLenFlag) ? 0x4444444 : 0x4000000;
    236 
    237         alphaValue = (FIXP_DBL)(j * k);
    238       }
    239       else {
    240         if (j >= offset[hDrcData->drcInterpolationSchemeCurr - 1]) {
    241           alphaValue = (FIXP_DBL)MAXVAL_DBL;
    242         }
    243       }
    244     }
    245     else {  /* short windows */
    246       shortDrc = 1;
    247     }
    248 
    249     fact_mag = hDrcData->currFact_mag;
    250     fact_exp = hDrcData->currFact_exp;
    251     numBands = hDrcData->numBandsCurr;
    252     bandTop = hDrcData->bandTopCurr;
    253   }
    254   else if (col < numQmfSubSamples) {  /* second half of current frame */
    255     if (hDrcData->winSequenceNext != 2) { /* next: long window */
    256       int j = col - (numQmfSubSamples>>1);
    257 
    258       if (hDrcData->drcInterpolationSchemeNext == 0) {
    259         INT k = (frameLenFlag) ? 0x4444444 : 0x4000000;
    260 
    261         alphaValue = (FIXP_DBL)(j * k);
    262       }
    263       else {
    264         if (j >= offset[hDrcData->drcInterpolationSchemeNext - 1]) {
    265           alphaValue = (FIXP_DBL)MAXVAL_DBL;
    266         }
    267       }
    268 
    269       fact_mag = hDrcData->nextFact_mag;
    270       fact_exp = hDrcData->nextFact_exp;
    271       numBands = hDrcData->numBandsNext;
    272       bandTop = hDrcData->bandTopNext;
    273     }
    274     else {  /* next: short windows */
    275       if (hDrcData->winSequenceCurr != 2) {  /* current: long window */
    276         alphaValue = (FIXP_DBL)0;
    277 
    278         fact_mag = hDrcData->nextFact_mag;
    279         fact_exp = hDrcData->nextFact_exp;
    280         numBands = hDrcData->numBandsNext;
    281         bandTop = hDrcData->bandTopNext;
    282       }
    283       else {  /* current: short windows */
    284         shortDrc = 1;
    285 
    286         fact_mag = hDrcData->currFact_mag;
    287         fact_exp = hDrcData->currFact_exp;
    288         numBands = hDrcData->numBandsCurr;
    289         bandTop = hDrcData->bandTopCurr;
    290       }
    291     }
    292   }
    293   else {  /* first half of next frame */
    294     if (hDrcData->winSequenceNext != 2) { /* long window */
    295       int j = col - (numQmfSubSamples>>1);
    296 
    297       if (hDrcData->drcInterpolationSchemeNext == 0) {
    298         INT k = (frameLenFlag) ? 0x4444444 : 0x4000000;
    299 
    300         alphaValue = (FIXP_DBL)(j * k);
    301       }
    302       else {
    303         if (j >= offset[hDrcData->drcInterpolationSchemeNext - 1]) {
    304           alphaValue = (FIXP_DBL)MAXVAL_DBL;
    305         }
    306       }
    307     }
    308     else {  /* short windows */
    309       shortDrc = 1;
    310     }
    311 
    312     fact_mag = hDrcData->nextFact_mag;
    313     fact_exp = hDrcData->nextFact_exp;
    314     numBands = hDrcData->numBandsNext;
    315     bandTop = hDrcData->bandTopNext;
    316 
    317     col -= numQmfSubSamples;
    318   }
    319 
    320 
    321   /* process bands */
    322   for (band = 0; band < (int)numBands; band++) {
    323     int bottomQmf, topQmf;
    324 
    325     FIXP_DBL drcFact_mag = (FIXP_DBL)MAXVAL_DBL;
    326 
    327     topMdct = (bandTop[band]+1) << 2;
    328 
    329     if (!shortDrc) {  /* long window */
    330       if (frameLenFlag) {
    331         /* 960 framing */
    332         bottomMdct = 30 * (bottomMdct / 30);
    333         topMdct    = 30 * (topMdct / 30);
    334 
    335         bottomQmf = fMultIfloor((FIXP_DBL)0x4444444, bottomMdct);
    336         topQmf    = fMultIfloor((FIXP_DBL)0x4444444, topMdct);
    337       }
    338       else {
    339         /* 1024 framing */
    340         bottomMdct &= ~0x1f;
    341         topMdct    &= ~0x1f;
    342 
    343         bottomQmf = bottomMdct >> 5;
    344         topQmf    = topMdct >> 5;
    345       }
    346 
    347       if (band == ((int)numBands-1)) {
    348         topQmf = (64);
    349       }
    350 
    351       for (bin = bottomQmf; bin < topQmf; bin++) {
    352         FIXP_DBL drcFact1_mag = hDrcData->prevFact_mag[bin];
    353         FIXP_DBL drcFact2_mag = fact_mag[band];
    354 
    355         /* normalize scale factors */
    356         if (hDrcData->prevFact_exp < maxShift) {
    357           drcFact1_mag >>= maxShift - hDrcData->prevFact_exp;
    358         }
    359         if (fact_exp < maxShift) {
    360           drcFact2_mag >>= maxShift - fact_exp;
    361         }
    362 
    363         /* interpolate */
    364         if (alphaValue == (FIXP_DBL)0) {
    365           drcFact_mag = drcFact1_mag;
    366         } else if (alphaValue == (FIXP_DBL)MAXVAL_DBL) {
    367           drcFact_mag = drcFact2_mag;
    368         } else {
    369           drcFact_mag = fMult(alphaValue, drcFact2_mag) + fMult(((FIXP_DBL)MAXVAL_DBL - alphaValue), drcFact1_mag);
    370         }
    371 
    372         /* apply scaling */
    373         qmfRealSlot[bin] = fMult(qmfRealSlot[bin], drcFact_mag);
    374         if (!useLP) {
    375           qmfImagSlot[bin] = fMult(qmfImagSlot[bin], drcFact_mag);
    376         }
    377 
    378         /* save previous factors */
    379         if (col == (numQmfSubSamples>>1)-1) {
    380           hDrcData->prevFact_mag[bin] = fact_mag[band];
    381         }
    382       }
    383     }
    384     else {  /* short windows */
    385       int startSample, stopSample;
    386       FIXP_DBL invFrameSizeDiv8 = (frameLenFlag) ? (FIXP_DBL)0x1111111 : (FIXP_DBL)0x1000000;
    387 
    388       if (frameLenFlag) {
    389         /*  960 framing */
    390         bottomMdct = 30/8 * (bottomMdct*8/30);
    391         topMdct    = 30/8 * (topMdct*8/30);
    392       }
    393       else {
    394         /* 1024 framing */
    395         bottomMdct &= ~0x03;
    396         topMdct    &= ~0x03;
    397       }
    398 
    399       /* startSample is truncated to the nearest corresponding start subsample in
    400          the QMF of the short window bottom is present in:*/
    401       startSample  = ((fMultIfloor( invFrameSizeDiv8, bottomMdct ) & 0x7) * numQmfSubSamples) >> 3;
    402 
    403       /* stopSample is rounded upwards to the nearest corresponding stop subsample
    404          in the QMF of the short window top is present in. */
    405       stopSample  = ((fMultIceil( invFrameSizeDiv8, topMdct ) & 0xf) * numQmfSubSamples) >> 3;
    406 
    407       bottomQmf = fMultIfloor( invFrameSizeDiv8, ((bottomMdct%(numQmfSubSamples<<2)) << 5) );
    408       topQmf    = fMultIfloor( invFrameSizeDiv8, ((topMdct%(numQmfSubSamples<<2)) << 5) );
    409 
    410       /* extend last band */
    411       if (band == ((int)numBands-1)) {
    412         topQmf = (64);
    413         stopSample = numQmfSubSamples;
    414       }
    415 
    416       if (topQmf == 0) {
    417         topQmf = (64);
    418       }
    419 
    420       /* save previous factors */
    421       if (stopSample == numQmfSubSamples) {
    422         int tmpBottom = bottomQmf;
    423 
    424         if (((numQmfSubSamples-1) & ~0x03) > startSample) {
    425             tmpBottom = 0;    /* band starts in previous short window */
    426         }
    427 
    428         for (bin = tmpBottom; bin < topQmf; bin++) {
    429           hDrcData->prevFact_mag[bin] = fact_mag[band];
    430         }
    431       }
    432 
    433       /* apply */
    434       if ((col >= startSample) && (col < stopSample)) {
    435         if ((col & ~0x03) > startSample) {
    436             bottomQmf = 0;    /* band starts in previous short window */
    437         }
    438         if (col < ((stopSample-1) & ~0x03)) {
    439             topQmf = (64);   /* band ends in next short window */
    440         }
    441 
    442         drcFact_mag = fact_mag[band];
    443 
    444         /* normalize scale factor */
    445         if (fact_exp < maxShift) {
    446           drcFact_mag >>= maxShift - fact_exp;
    447         }
    448 
    449         /* apply scaling */
    450         for (bin = bottomQmf; bin < topQmf; bin++) {
    451           qmfRealSlot[bin] = fMult(qmfRealSlot[bin], drcFact_mag);
    452           if (!useLP) {
    453             qmfImagSlot[bin] = fMult(qmfImagSlot[bin], drcFact_mag);
    454           }
    455         }
    456       }
    457     }
    458 
    459     bottomMdct = topMdct;
    460   }   /* end of bands loop */
    461 
    462   if (col == (numQmfSubSamples>>1)-1) {
    463     hDrcData->prevFact_exp = fact_exp;
    464   }
    465 }
    466 
    467 
    468 /*!
    469   \brief Apply DRC factors frame based.
    470 
    471   \hDrcData Handle to DRC channel data.
    472   \qmfRealSlot Pointer to real valued QMF data of the whole frame.
    473   \qmfImagSlot Pointer to the imaginary QMF data of the whole frame.
    474   \numQmfSubSamples Total number of time slots for one frame.
    475   \scaleFactor Pointer to the out scale factor of the frame.
    476 
    477   \return None.
    478 */
    479 void sbrDecoder_drcApply (
    480     HANDLE_SBR_DRC_CHANNEL  hDrcData,
    481     FIXP_DBL **QmfBufferReal,
    482     FIXP_DBL **QmfBufferImag,
    483     int  numQmfSubSamples,
    484     int *scaleFactor
    485   )
    486 {
    487   int col;
    488   int maxShift = 0;
    489 
    490   if (hDrcData == NULL) {
    491     return;
    492   }
    493   if ( (hDrcData->enable == 0)
    494     || ((hDrcData->numBandsCurr == 0) && (hDrcData->numBandsNext == 0))
    495      ) {
    496     return;  /* Avoid changing the scaleFactor even though the processing is disabled. */
    497   }
    498 
    499   /* get max scale factor */
    500   if (hDrcData->prevFact_exp > maxShift) {
    501     maxShift = hDrcData->prevFact_exp;
    502   }
    503   if (hDrcData->currFact_exp > maxShift) {
    504     maxShift = hDrcData->currFact_exp;
    505   }
    506   if (hDrcData->nextFact_exp > maxShift) {
    507     maxShift = hDrcData->nextFact_exp;
    508   }
    509 
    510   for (col = 0; col < numQmfSubSamples; col++)
    511   {
    512     FIXP_DBL *qmfSlotReal = QmfBufferReal[col];
    513     FIXP_DBL *qmfSlotImag = (QmfBufferImag == NULL) ? NULL : QmfBufferImag[col];
    514 
    515     sbrDecoder_drcApplySlot (
    516       hDrcData,
    517       qmfSlotReal,
    518       qmfSlotImag,
    519       col,
    520       numQmfSubSamples,
    521       maxShift
    522     );
    523   }
    524 
    525   *scaleFactor += maxShift;
    526 }
    527 
    528