Home | History | Annotate | Download | only in src
      1 /* -----------------------------------------------------------------------------
      2 Software License for The Fraunhofer FDK AAC Codec Library for Android
      3 
      4  Copyright  1995 - 2018 Fraunhofer-Gesellschaft zur Frderung der angewandten
      5 Forschung e.V. All rights reserved.
      6 
      7  1.    INTRODUCTION
      8 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
      9 that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
     10 scheme for digital audio. This FDK AAC Codec software is intended to be used on
     11 a wide variety of Android devices.
     12 
     13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
     14 general perceptual audio codecs. AAC-ELD is considered the best-performing
     15 full-bandwidth communications codec by independent studies and is widely
     16 deployed. AAC has been standardized by ISO and IEC as part of the MPEG
     17 specifications.
     18 
     19 Patent licenses for necessary patent claims for the FDK AAC Codec (including
     20 those of Fraunhofer) may be obtained through Via Licensing
     21 (www.vialicensing.com) or through the respective patent owners individually for
     22 the purpose of encoding or decoding bit streams in products that are compliant
     23 with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
     24 Android devices already license these patent claims through Via Licensing or
     25 directly from the patent owners, and therefore FDK AAC Codec software may
     26 already be covered under those patent licenses when it is used for those
     27 licensed purposes only.
     28 
     29 Commercially-licensed AAC software libraries, including floating-point versions
     30 with enhanced sound quality, are also available from Fraunhofer. Users are
     31 encouraged to check the Fraunhofer website for additional applications
     32 information and documentation.
     33 
     34 2.    COPYRIGHT LICENSE
     35 
     36 Redistribution and use in source and binary forms, with or without modification,
     37 are permitted without payment of copyright license fees provided that you
     38 satisfy the following conditions:
     39 
     40 You must retain the complete text of this software license in redistributions of
     41 the FDK AAC Codec or your modifications thereto in source code form.
     42 
     43 You must retain the complete text of this software license in the documentation
     44 and/or other materials provided with redistributions of the FDK AAC Codec or
     45 your modifications thereto in binary form. You must make available free of
     46 charge copies of the complete source code of the FDK AAC Codec and your
     47 modifications thereto to recipients of copies in binary form.
     48 
     49 The name of Fraunhofer may not be used to endorse or promote products derived
     50 from this library without prior written permission.
     51 
     52 You may not charge copyright license fees for anyone to use, copy or distribute
     53 the FDK AAC Codec software or your modifications thereto.
     54 
     55 Your modified versions of the FDK AAC Codec must carry prominent notices stating
     56 that you changed the software and the date of any change. For modified versions
     57 of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
     58 must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
     59 AAC Codec Library for Android."
     60 
     61 3.    NO PATENT LICENSE
     62 
     63 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
     64 limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
     65 Fraunhofer provides no warranty of patent non-infringement with respect to this
     66 software.
     67 
     68 You may use this FDK AAC Codec software or modifications thereto only for
     69 purposes that are authorized by appropriate patent licenses.
     70 
     71 4.    DISCLAIMER
     72 
     73 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
     74 holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
     75 including but not limited to the implied warranties of merchantability and
     76 fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     77 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
     78 or consequential damages, including but not limited to procurement of substitute
     79 goods or services; loss of use, data, or profits, or business interruption,
     80 however caused and on any theory of liability, whether in contract, strict
     81 liability, or tort (including negligence), arising in any way out of the use of
     82 this software, even if advised of the possibility of such damage.
     83 
     84 5.    CONTACT INFORMATION
     85 
     86 Fraunhofer Institute for Integrated Circuits IIS
     87 Attention: Audio and Multimedia Departments - FDK AAC LL
     88 Am Wolfsmantel 33
     89 91058 Erlangen, Germany
     90 
     91 www.iis.fraunhofer.de/amm
     92 amm-info (at) iis.fraunhofer.de
     93 ----------------------------------------------------------------------------- */
     94 
     95 /**************************** AAC encoder library ******************************
     96 
     97    Author(s):   M. Werner
     98 
     99    Description: Quantizing & coding
    100 
    101 *******************************************************************************/
    102 
    103 #include "qc_main.h"
    104 #include "quantize.h"
    105 #include "interface.h"
    106 #include "adj_thr.h"
    107 #include "sf_estim.h"
    108 #include "bit_cnt.h"
    109 #include "dyn_bits.h"
    110 #include "channel_map.h"
    111 #include "aacEnc_ram.h"
    112 
    113 #include "genericStds.h"
    114 
    115 #define AACENC_DZQ_BR_THR 32000 /* Dead zone quantizer bitrate threshold */
    116 
    117 typedef struct {
    118   QCDATA_BR_MODE bitrateMode;
    119   LONG vbrQualFactor;
    120 } TAB_VBR_QUAL_FACTOR;
    121 
    122 static const TAB_VBR_QUAL_FACTOR tableVbrQualFactor[] = {
    123     {QCDATA_BR_MODE_VBR_1,
    124      FL2FXCONST_DBL(0.160f)}, /* Approx. 32 -  48 (AC-LC),  32 -  56
    125                                  (AAC-LD/ELD) kbps/channel */
    126     {QCDATA_BR_MODE_VBR_2,
    127      FL2FXCONST_DBL(0.148f)}, /* Approx. 40 -  56 (AC-LC),  40 -  64
    128                                  (AAC-LD/ELD) kbps/channel */
    129     {QCDATA_BR_MODE_VBR_3,
    130      FL2FXCONST_DBL(0.135f)}, /* Approx. 48 -  64 (AC-LC),  48 -  72
    131                                  (AAC-LD/ELD) kbps/channel */
    132     {QCDATA_BR_MODE_VBR_4,
    133      FL2FXCONST_DBL(0.111f)}, /* Approx. 64 -  80 (AC-LC),  64 -  88
    134                                  (AAC-LD/ELD) kbps/channel */
    135     {QCDATA_BR_MODE_VBR_5,
    136      FL2FXCONST_DBL(0.070f)} /* Approx. 96 - 120 (AC-LC), 112 - 144
    137                                 (AAC-LD/ELD) kbps/channel */
    138 };
    139 
    140 static INT isConstantBitrateMode(const QCDATA_BR_MODE bitrateMode) {
    141   return (((bitrateMode == QCDATA_BR_MODE_CBR) ||
    142            (bitrateMode == QCDATA_BR_MODE_SFR) ||
    143            (bitrateMode == QCDATA_BR_MODE_FF))
    144               ? 1
    145               : 0);
    146 }
    147 
    148 typedef enum {
    149   FRAME_LEN_BYTES_MODULO = 1,
    150   FRAME_LEN_BYTES_INT = 2
    151 } FRAME_LEN_RESULT_MODE;
    152 
    153 /* forward declarations */
    154 
    155 static INT FDKaacEnc_calcMaxValueInSfb(INT sfbCnt, INT maxSfbPerGroup,
    156                                        INT sfbPerGroup, INT* RESTRICT sfbOffset,
    157                                        SHORT* RESTRICT quantSpectrum,
    158                                        UINT* RESTRICT maxValue);
    159 
    160 static void FDKaacEnc_crashRecovery(INT nChannels,
    161                                     PSY_OUT_ELEMENT* psyOutElement,
    162                                     QC_OUT* qcOut, QC_OUT_ELEMENT* qcElement,
    163                                     INT bitsToSave, AUDIO_OBJECT_TYPE aot,
    164                                     UINT syntaxFlags, SCHAR epConfig);
    165 
    166 static AAC_ENCODER_ERROR FDKaacEnc_reduceBitConsumption(
    167     int* iterations, const int maxIterations, int gainAdjustment,
    168     int* chConstraintsFulfilled, int* calculateQuant, int nChannels,
    169     PSY_OUT_ELEMENT* psyOutElement, QC_OUT* qcOut, QC_OUT_ELEMENT* qcOutElement,
    170     ELEMENT_BITS* elBits, AUDIO_OBJECT_TYPE aot, UINT syntaxFlags,
    171     SCHAR epConfig);
    172 
    173 void FDKaacEnc_QCClose(QC_STATE** phQCstate, QC_OUT** phQC);
    174 
    175 /*****************************************************************************
    176 
    177     functionname: FDKaacEnc_calcFrameLen
    178     description:
    179     returns:
    180     input:
    181     output:
    182 
    183 *****************************************************************************/
    184 static INT FDKaacEnc_calcFrameLen(INT bitRate, INT sampleRate,
    185                                   INT granuleLength,
    186                                   FRAME_LEN_RESULT_MODE mode) {
    187   INT result;
    188 
    189   result = ((granuleLength) >> 3) * (bitRate);
    190 
    191   switch (mode) {
    192     case FRAME_LEN_BYTES_MODULO:
    193       result %= sampleRate;
    194       break;
    195     case FRAME_LEN_BYTES_INT:
    196       result /= sampleRate;
    197       break;
    198   }
    199   return (result);
    200 }
    201 
    202 /*****************************************************************************
    203 
    204     functionname:FDKaacEnc_framePadding
    205     description: Calculates if padding is needed for actual frame
    206     returns:
    207     input:
    208     output:
    209 
    210 *****************************************************************************/
    211 static INT FDKaacEnc_framePadding(INT bitRate, INT sampleRate,
    212                                   INT granuleLength, INT* paddingRest) {
    213   INT paddingOn;
    214   INT difference;
    215 
    216   paddingOn = 0;
    217 
    218   difference = FDKaacEnc_calcFrameLen(bitRate, sampleRate, granuleLength,
    219                                       FRAME_LEN_BYTES_MODULO);
    220   *paddingRest -= difference;
    221 
    222   if (*paddingRest <= 0) {
    223     paddingOn = 1;
    224     *paddingRest += sampleRate;
    225   }
    226 
    227   return (paddingOn);
    228 }
    229 
    230 /*********************************************************************************
    231 
    232          functionname: FDKaacEnc_QCOutNew
    233          description:
    234          return:
    235 
    236 **********************************************************************************/
    237 AAC_ENCODER_ERROR FDKaacEnc_QCOutNew(QC_OUT** phQC, const INT nElements,
    238                                      const INT nChannels, const INT nSubFrames,
    239                                      UCHAR* dynamic_RAM) {
    240   AAC_ENCODER_ERROR ErrorStatus;
    241   int n, i;
    242   int elInc = 0, chInc = 0;
    243 
    244   for (n = 0; n < nSubFrames; n++) {
    245     phQC[n] = GetRam_aacEnc_QCout(n);
    246     if (phQC[n] == NULL) {
    247       ErrorStatus = AAC_ENC_NO_MEMORY;
    248       goto QCOutNew_bail;
    249     }
    250 
    251     for (i = 0; i < nChannels; i++) {
    252       phQC[n]->pQcOutChannels[i] = GetRam_aacEnc_QCchannel(chInc, dynamic_RAM);
    253       if (phQC[n]->pQcOutChannels[i] == NULL) {
    254         ErrorStatus = AAC_ENC_NO_MEMORY;
    255         goto QCOutNew_bail;
    256       }
    257 
    258       chInc++;
    259     } /* nChannels */
    260 
    261     for (i = 0; i < nElements; i++) {
    262       phQC[n]->qcElement[i] = GetRam_aacEnc_QCelement(elInc);
    263       if (phQC[n]->qcElement[i] == NULL) {
    264         ErrorStatus = AAC_ENC_NO_MEMORY;
    265         goto QCOutNew_bail;
    266       }
    267       elInc++;
    268 
    269       /* initialize pointer to dynamic buffer which are used in adjust
    270        * thresholds */
    271       phQC[n]->qcElement[i]->dynMem_Ah_Flag = dynamic_RAM + (P_BUF_1);
    272       phQC[n]->qcElement[i]->dynMem_Thr_Exp =
    273           dynamic_RAM + (P_BUF_1) + ADJ_THR_AH_FLAG_SIZE;
    274       phQC[n]->qcElement[i]->dynMem_SfbNActiveLinesLdData =
    275           dynamic_RAM + (P_BUF_1) + ADJ_THR_AH_FLAG_SIZE + ADJ_THR_THR_EXP_SIZE;
    276 
    277     } /* nElements */
    278 
    279   } /* nSubFrames */
    280 
    281   return AAC_ENC_OK;
    282 
    283 QCOutNew_bail:
    284   return ErrorStatus;
    285 }
    286 
    287 /*********************************************************************************
    288 
    289          functionname: FDKaacEnc_QCOutInit
    290          description:
    291          return:
    292 
    293 **********************************************************************************/
    294 AAC_ENCODER_ERROR FDKaacEnc_QCOutInit(QC_OUT* phQC[(1)], const INT nSubFrames,
    295                                       const CHANNEL_MAPPING* cm) {
    296   INT n, i, ch;
    297 
    298   for (n = 0; n < nSubFrames; n++) {
    299     INT chInc = 0;
    300     for (i = 0; i < cm->nElements; i++) {
    301       for (ch = 0; ch < cm->elInfo[i].nChannelsInEl; ch++) {
    302         phQC[n]->qcElement[i]->qcOutChannel[ch] =
    303             phQC[n]->pQcOutChannels[chInc];
    304         chInc++;
    305       } /* chInEl */
    306     }   /* nElements */
    307   }     /* nSubFrames */
    308 
    309   return AAC_ENC_OK;
    310 }
    311 
    312 /*********************************************************************************
    313 
    314          functionname: FDKaacEnc_QCNew
    315          description:
    316          return:
    317 
    318 **********************************************************************************/
    319 AAC_ENCODER_ERROR FDKaacEnc_QCNew(QC_STATE** phQC, INT nElements,
    320                                   UCHAR* dynamic_RAM) {
    321   AAC_ENCODER_ERROR ErrorStatus;
    322   int i;
    323 
    324   QC_STATE* hQC = GetRam_aacEnc_QCstate();
    325   *phQC = hQC;
    326   if (hQC == NULL) {
    327     ErrorStatus = AAC_ENC_NO_MEMORY;
    328     goto QCNew_bail;
    329   }
    330 
    331   if (FDKaacEnc_AdjThrNew(&hQC->hAdjThr, nElements)) {
    332     ErrorStatus = AAC_ENC_NO_MEMORY;
    333     goto QCNew_bail;
    334   }
    335 
    336   if (FDKaacEnc_BCNew(&(hQC->hBitCounter), dynamic_RAM)) {
    337     ErrorStatus = AAC_ENC_NO_MEMORY;
    338     goto QCNew_bail;
    339   }
    340 
    341   for (i = 0; i < nElements; i++) {
    342     hQC->elementBits[i] = GetRam_aacEnc_ElementBits(i);
    343     if (hQC->elementBits[i] == NULL) {
    344       ErrorStatus = AAC_ENC_NO_MEMORY;
    345       goto QCNew_bail;
    346     }
    347   }
    348 
    349   return AAC_ENC_OK;
    350 
    351 QCNew_bail:
    352   FDKaacEnc_QCClose(phQC, NULL);
    353   return ErrorStatus;
    354 }
    355 
    356 /*********************************************************************************
    357 
    358          functionname: FDKaacEnc_QCInit
    359          description:
    360          return:
    361 
    362 **********************************************************************************/
    363 AAC_ENCODER_ERROR FDKaacEnc_QCInit(QC_STATE* hQC, struct QC_INIT* init,
    364                                    const ULONG initFlags) {
    365   AAC_ENCODER_ERROR err = AAC_ENC_OK;
    366 
    367   int i;
    368   hQC->maxBitsPerFrame = init->maxBits;
    369   hQC->minBitsPerFrame = init->minBits;
    370   hQC->nElements = init->channelMapping->nElements;
    371   if ((initFlags != 0) || ((init->bitrateMode != QCDATA_BR_MODE_FF) &&
    372                            (hQC->bitResTotMax != init->bitRes))) {
    373     hQC->bitResTot = init->bitRes;
    374   }
    375   hQC->bitResTotMax = init->bitRes;
    376   hQC->maxBitFac = init->maxBitFac;
    377   hQC->bitrateMode = init->bitrateMode;
    378   hQC->invQuant = init->invQuant;
    379   hQC->maxIterations = init->maxIterations;
    380 
    381   if (isConstantBitrateMode(hQC->bitrateMode)) {
    382     /* 0: full bitreservoir, 1: reduced bitreservoir, 2: disabled bitreservoir
    383      */
    384     hQC->bitResMode = init->bitResMode;
    385   } else {
    386     hQC->bitResMode = AACENC_BR_MODE_FULL; /* full bitreservoir */
    387   }
    388 
    389   hQC->padding.paddingRest = init->padding.paddingRest;
    390 
    391   hQC->globHdrBits = init->staticBits; /* Bit overhead due to transport */
    392 
    393   err = FDKaacEnc_InitElementBits(
    394       hQC, init->channelMapping, init->bitrate,
    395       (init->averageBits / init->nSubFrames) - hQC->globHdrBits,
    396       hQC->maxBitsPerFrame / init->channelMapping->nChannelsEff);
    397   if (err != AAC_ENC_OK) goto bail;
    398 
    399   hQC->vbrQualFactor = FL2FXCONST_DBL(0.f);
    400   for (i = 0;
    401        i < (int)(sizeof(tableVbrQualFactor) / sizeof(TAB_VBR_QUAL_FACTOR));
    402        i++) {
    403     if (hQC->bitrateMode == tableVbrQualFactor[i].bitrateMode) {
    404       hQC->vbrQualFactor = (FIXP_DBL)tableVbrQualFactor[i].vbrQualFactor;
    405       break;
    406     }
    407   }
    408 
    409   if (init->channelMapping->nChannelsEff == 1 &&
    410       (init->bitrate / init->channelMapping->nChannelsEff) <
    411           AACENC_DZQ_BR_THR &&
    412       init->isLowDelay !=
    413           0) /* watch out here: init->bitrate is the bitrate "minus" the
    414                 standard SBR bitrate (=2500kbps) --> for the FDK the OFFSTE
    415                 tuning should start somewhere below 32000kbps-2500kbps ... so
    416                 everything is fine here */
    417   {
    418     hQC->dZoneQuantEnable = 1;
    419   } else {
    420     hQC->dZoneQuantEnable = 0;
    421   }
    422 
    423   FDKaacEnc_AdjThrInit(
    424       hQC->hAdjThr, init->meanPe, hQC->invQuant, init->channelMapping,
    425       init->sampleRate, /* output sample rate */
    426       init->bitrate,    /* total bitrate */
    427       init->isLowDelay, /* if set, calc bits2PE factor
    428                            depending on samplerate */
    429       init->bitResMode  /* for a small bitreservoir, the pe
    430                            correction is calc'd differently */
    431       ,
    432       hQC->dZoneQuantEnable, init->bitDistributionMode, hQC->vbrQualFactor);
    433 
    434 bail:
    435   return err;
    436 }
    437 
    438 /*********************************************************************************
    439 
    440          functionname: FDKaacEnc_QCMainPrepare
    441          description:
    442          return:
    443 
    444 **********************************************************************************/
    445 AAC_ENCODER_ERROR FDKaacEnc_QCMainPrepare(
    446     ELEMENT_INFO* elInfo, ATS_ELEMENT* RESTRICT adjThrStateElement,
    447     PSY_OUT_ELEMENT* RESTRICT psyOutElement,
    448     QC_OUT_ELEMENT* RESTRICT qcOutElement, AUDIO_OBJECT_TYPE aot,
    449     UINT syntaxFlags, SCHAR epConfig) {
    450   AAC_ENCODER_ERROR ErrorStatus = AAC_ENC_OK;
    451   INT nChannels = elInfo->nChannelsInEl;
    452 
    453   PSY_OUT_CHANNEL** RESTRICT psyOutChannel =
    454       psyOutElement->psyOutChannel; /* may be modified in-place */
    455 
    456   FDKaacEnc_CalcFormFactor(qcOutElement->qcOutChannel, psyOutChannel,
    457                            nChannels);
    458 
    459   /* prepare and calculate PE without reduction */
    460   FDKaacEnc_peCalculation(&qcOutElement->peData, psyOutChannel,
    461                           qcOutElement->qcOutChannel, &psyOutElement->toolsInfo,
    462                           adjThrStateElement, nChannels);
    463 
    464   ErrorStatus = FDKaacEnc_ChannelElementWrite(
    465       NULL, elInfo, NULL, psyOutElement, psyOutElement->psyOutChannel,
    466       syntaxFlags, aot, epConfig, &qcOutElement->staticBitsUsed, 0);
    467 
    468   return ErrorStatus;
    469 }
    470 
    471 /*********************************************************************************
    472 
    473          functionname: FDKaacEnc_AdjustBitrate
    474          description:  adjusts framelength via padding on a frame to frame
    475 basis, to achieve a bitrate that demands a non byte aligned framelength return:
    476 errorcode
    477 
    478 **********************************************************************************/
    479 AAC_ENCODER_ERROR FDKaacEnc_AdjustBitrate(
    480     QC_STATE* RESTRICT hQC, CHANNEL_MAPPING* RESTRICT cm, INT* avgTotalBits,
    481     INT bitRate,       /* total bitrate */
    482     INT sampleRate,    /* output sampling rate */
    483     INT granuleLength) /* frame length */
    484 {
    485   INT paddingOn;
    486   INT frameLen;
    487 
    488   /* Do we need an extra padding byte? */
    489   paddingOn = FDKaacEnc_framePadding(bitRate, sampleRate, granuleLength,
    490                                      &hQC->padding.paddingRest);
    491 
    492   frameLen =
    493       paddingOn + FDKaacEnc_calcFrameLen(bitRate, sampleRate, granuleLength,
    494                                          FRAME_LEN_BYTES_INT);
    495 
    496   *avgTotalBits = frameLen << 3;
    497 
    498   return AAC_ENC_OK;
    499 }
    500 
    501 #define isAudioElement(elType) \
    502   ((elType == ID_SCE) || (elType == ID_CPE) || (elType == ID_LFE))
    503 
    504 /*********************************************************************************
    505 
    506          functionname: FDKaacEnc_distributeElementDynBits
    507          description:  distributes all bits over all elements. The relative bit
    508                        distibution is described in the ELEMENT_INFO of the
    509                        appropriate element. The bit distribution table is
    510                        initialized in FDKaacEnc_InitChannelMapping().
    511          return:       errorcode
    512 
    513 **********************************************************************************/
    514 static AAC_ENCODER_ERROR FDKaacEnc_distributeElementDynBits(
    515     QC_STATE* hQC, QC_OUT_ELEMENT* qcElement[((8))], CHANNEL_MAPPING* cm,
    516     INT codeBits) {
    517   INT i;             /* counter variable */
    518   INT totalBits = 0; /* sum of bits over all elements */
    519 
    520   for (i = (cm->nElements - 1); i >= 0; i--) {
    521     if (isAudioElement(cm->elInfo[i].elType)) {
    522       qcElement[i]->grantedDynBits =
    523           fMax(0, fMultI(hQC->elementBits[i]->relativeBitsEl, codeBits));
    524       totalBits += qcElement[i]->grantedDynBits;
    525     }
    526   }
    527 
    528   /* Due to inaccuracies with the multiplication, codeBits may differ from
    529      totalBits. For that case, the difference must be added/substracted again
    530      to/from one element, i.e:
    531      Negative differences are substracted from the element with the most bits.
    532      Positive differences are added to the element with the least bits.
    533   */
    534   if (codeBits != totalBits) {
    535     INT elMaxBits = cm->nElements - 1; /* element with the most bits */
    536     INT elMinBits = cm->nElements - 1; /* element with the least bits */
    537 
    538     /* Search for biggest and smallest audio element */
    539     for (i = (cm->nElements - 1); i >= 0; i--) {
    540       if (isAudioElement(cm->elInfo[i].elType)) {
    541         if (qcElement[i]->grantedDynBits >
    542             qcElement[elMaxBits]->grantedDynBits) {
    543           elMaxBits = i;
    544         }
    545         if (qcElement[i]->grantedDynBits <
    546             qcElement[elMinBits]->grantedDynBits) {
    547           elMinBits = i;
    548         }
    549       }
    550     }
    551     /* Compensate for bit distibution difference */
    552     if (codeBits - totalBits > 0) {
    553       qcElement[elMinBits]->grantedDynBits += codeBits - totalBits;
    554     } else {
    555       qcElement[elMaxBits]->grantedDynBits += codeBits - totalBits;
    556     }
    557   }
    558 
    559   return AAC_ENC_OK;
    560 }
    561 
    562 /**
    563  * \brief  Verify whether minBitsPerFrame criterion can be satisfied.
    564  *
    565  * This function evaluates the bit consumption only if minBitsPerFrame parameter
    566  * is not 0. In hyperframing mode the difference between grantedDynBits and
    567  * usedDynBits of all sub frames results the number of fillbits to be written.
    568  * This bits can be distrubitued in superframe to reach minBitsPerFrame bit
    569  * consumption in single AU's. The return value denotes if enough desired fill
    570  * bits are available to achieve minBitsPerFrame in all frames. This check can
    571  * only be used within superframes.
    572  *
    573  * \param qcOut            Pointer to coding data struct.
    574  * \param minBitsPerFrame  Minimal number of bits to be consumed in each frame.
    575  * \param nSubFrames       Number of frames in superframe
    576  *
    577  * \return
    578  *          - 1: all fine
    579  *          - 0: criterion not fulfilled
    580  */
    581 static int checkMinFrameBitsDemand(QC_OUT** qcOut, const INT minBitsPerFrame,
    582                                    const INT nSubFrames) {
    583   int result = 1; /* all fine*/
    584   return result;
    585 }
    586 
    587 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    588 
    589 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    590 /*********************************************************************************
    591 
    592          functionname: FDKaacEnc_getMinimalStaticBitdemand
    593          description:  calculate minmal size of static bits by reduction ,
    594                        to zero spectrum and deactivating tns and MS
    595          return:       number of static bits
    596 
    597 **********************************************************************************/
    598 static int FDKaacEnc_getMinimalStaticBitdemand(CHANNEL_MAPPING* cm,
    599                                                PSY_OUT** psyOut) {
    600   AUDIO_OBJECT_TYPE aot = AOT_AAC_LC;
    601   UINT syntaxFlags = 0;
    602   SCHAR epConfig = -1;
    603   int i, bitcount = 0;
    604 
    605   for (i = 0; i < cm->nElements; i++) {
    606     ELEMENT_INFO elInfo = cm->elInfo[i];
    607 
    608     if ((elInfo.elType == ID_SCE) || (elInfo.elType == ID_CPE) ||
    609         (elInfo.elType == ID_LFE)) {
    610       INT minElBits = 0;
    611 
    612       FDKaacEnc_ChannelElementWrite(NULL, &elInfo, NULL,
    613                                     psyOut[0]->psyOutElement[i],
    614                                     psyOut[0]->psyOutElement[i]->psyOutChannel,
    615                                     syntaxFlags, aot, epConfig, &minElBits, 1);
    616       bitcount += minElBits;
    617     }
    618   }
    619 
    620   return bitcount;
    621 }
    622 
    623 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    624 
    625 static AAC_ENCODER_ERROR FDKaacEnc_prepareBitDistribution(
    626     QC_STATE* hQC, PSY_OUT** psyOut, QC_OUT** qcOut, CHANNEL_MAPPING* cm,
    627     QC_OUT_ELEMENT* qcElement[(1)][((8))], INT avgTotalBits,
    628     INT* totalAvailableBits, INT* avgTotalDynBits) {
    629   int i;
    630   /* get maximal allowed dynamic bits */
    631   qcOut[0]->grantedDynBits =
    632       (fixMin(hQC->maxBitsPerFrame, avgTotalBits) - hQC->globHdrBits) & ~7;
    633   qcOut[0]->grantedDynBits -= (qcOut[0]->globalExtBits + qcOut[0]->staticBits +
    634                                qcOut[0]->elementExtBits);
    635   qcOut[0]->maxDynBits = ((hQC->maxBitsPerFrame) & ~7) -
    636                          (qcOut[0]->globalExtBits + qcOut[0]->staticBits +
    637                           qcOut[0]->elementExtBits);
    638   /* assure that enough bits are available */
    639   if ((qcOut[0]->grantedDynBits + hQC->bitResTot) < 0) {
    640     /* crash recovery allows to reduce static bits to a minimum */
    641     if ((qcOut[0]->grantedDynBits + hQC->bitResTot) <
    642         (FDKaacEnc_getMinimalStaticBitdemand(cm, psyOut) -
    643          qcOut[0]->staticBits))
    644       return AAC_ENC_BITRES_TOO_LOW;
    645   }
    646 
    647   /* distribute dynamic bits to each element */
    648   FDKaacEnc_distributeElementDynBits(hQC, qcElement[0], cm,
    649                                      qcOut[0]->grantedDynBits);
    650 
    651   *avgTotalDynBits = 0; /*frameDynBits;*/
    652 
    653   *totalAvailableBits = avgTotalBits;
    654 
    655   /* sum up corrected granted PE */
    656   qcOut[0]->totalGrantedPeCorr = 0;
    657 
    658   for (i = 0; i < cm->nElements; i++) {
    659     ELEMENT_INFO elInfo = cm->elInfo[i];
    660     int nChannels = elInfo.nChannelsInEl;
    661 
    662     if ((elInfo.elType == ID_SCE) || (elInfo.elType == ID_CPE) ||
    663         (elInfo.elType == ID_LFE)) {
    664       /* for ( all sub frames ) ... */
    665       FDKaacEnc_DistributeBits(
    666           hQC->hAdjThr, hQC->hAdjThr->adjThrStateElem[i],
    667           psyOut[0]->psyOutElement[i]->psyOutChannel, &qcElement[0][i]->peData,
    668           &qcElement[0][i]->grantedPe, &qcElement[0][i]->grantedPeCorr,
    669           nChannels, psyOut[0]->psyOutElement[i]->commonWindow,
    670           qcElement[0][i]->grantedDynBits, hQC->elementBits[i]->bitResLevelEl,
    671           hQC->elementBits[i]->maxBitResBitsEl, hQC->maxBitFac,
    672           hQC->bitResMode);
    673 
    674       *totalAvailableBits += hQC->elementBits[i]->bitResLevelEl;
    675       /* get total corrected granted PE */
    676       qcOut[0]->totalGrantedPeCorr += qcElement[0][i]->grantedPeCorr;
    677     } /*  -end- if(ID_SCE || ID_CPE || ID_LFE) */
    678 
    679   } /* -end- element loop */
    680 
    681   *totalAvailableBits = fMin(hQC->maxBitsPerFrame, (*totalAvailableBits));
    682 
    683   return AAC_ENC_OK;
    684 }
    685 
    686 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    687 static AAC_ENCODER_ERROR FDKaacEnc_updateUsedDynBits(
    688     INT* sumDynBitsConsumed, QC_OUT_ELEMENT* qcElement[((8))],
    689     CHANNEL_MAPPING* cm) {
    690   INT i;
    691 
    692   *sumDynBitsConsumed = 0;
    693 
    694   for (i = 0; i < cm->nElements; i++) {
    695     ELEMENT_INFO elInfo = cm->elInfo[i];
    696 
    697     if ((elInfo.elType == ID_SCE) || (elInfo.elType == ID_CPE) ||
    698         (elInfo.elType == ID_LFE)) {
    699       /* sum up bits consumed */
    700       *sumDynBitsConsumed += qcElement[i]->dynBitsUsed;
    701     } /*  -end- if(ID_SCE || ID_CPE || ID_LFE) */
    702 
    703   } /* -end- element loop */
    704 
    705   return AAC_ENC_OK;
    706 }
    707 
    708 static INT FDKaacEnc_getTotalConsumedDynBits(QC_OUT** qcOut, INT nSubFrames) {
    709   INT c, totalBits = 0;
    710 
    711   /* sum up bit consumption for all sub frames */
    712   for (c = 0; c < nSubFrames; c++) {
    713     /* bit consumption not valid if dynamic bits
    714        not available in one sub frame */
    715     if (qcOut[c]->usedDynBits == -1) return -1;
    716     totalBits += qcOut[c]->usedDynBits;
    717   }
    718 
    719   return totalBits;
    720 }
    721 
    722 static INT FDKaacEnc_getTotalConsumedBits(QC_OUT** qcOut,
    723                                           QC_OUT_ELEMENT* qcElement[(1)][((8))],
    724                                           CHANNEL_MAPPING* cm, INT globHdrBits,
    725                                           INT nSubFrames) {
    726   int c, i;
    727   int totalUsedBits = 0;
    728 
    729   for (c = 0; c < nSubFrames; c++) {
    730     int dataBits = 0;
    731     for (i = 0; i < cm->nElements; i++) {
    732       if ((cm->elInfo[i].elType == ID_SCE) ||
    733           (cm->elInfo[i].elType == ID_CPE) ||
    734           (cm->elInfo[i].elType == ID_LFE)) {
    735         dataBits += qcElement[c][i]->dynBitsUsed +
    736                     qcElement[c][i]->staticBitsUsed +
    737                     qcElement[c][i]->extBitsUsed;
    738       }
    739     }
    740     dataBits += qcOut[c]->globalExtBits;
    741 
    742     totalUsedBits += (8 - (dataBits) % 8) % 8;
    743     totalUsedBits += dataBits + globHdrBits; /* header bits for every frame */
    744   }
    745   return totalUsedBits;
    746 }
    747 
    748 static AAC_ENCODER_ERROR FDKaacEnc_BitResRedistribution(
    749     QC_STATE* const hQC, const CHANNEL_MAPPING* const cm,
    750     const INT avgTotalBits) {
    751   /* check bitreservoir fill level */
    752   if (hQC->bitResTot < 0) {
    753     return AAC_ENC_BITRES_TOO_LOW;
    754   } else if (hQC->bitResTot > hQC->bitResTotMax) {
    755     return AAC_ENC_BITRES_TOO_HIGH;
    756   } else {
    757     INT i;
    758     INT totalBits = 0, totalBits_max = 0;
    759 
    760     const int totalBitreservoir =
    761         fMin(hQC->bitResTot, (hQC->maxBitsPerFrame - avgTotalBits));
    762     const int totalBitreservoirMax =
    763         fMin(hQC->bitResTotMax, (hQC->maxBitsPerFrame - avgTotalBits));
    764 
    765     for (i = (cm->nElements - 1); i >= 0; i--) {
    766       if ((cm->elInfo[i].elType == ID_SCE) ||
    767           (cm->elInfo[i].elType == ID_CPE) ||
    768           (cm->elInfo[i].elType == ID_LFE)) {
    769         hQC->elementBits[i]->bitResLevelEl =
    770             fMultI(hQC->elementBits[i]->relativeBitsEl, totalBitreservoir);
    771         totalBits += hQC->elementBits[i]->bitResLevelEl;
    772 
    773         hQC->elementBits[i]->maxBitResBitsEl =
    774             fMultI(hQC->elementBits[i]->relativeBitsEl, totalBitreservoirMax);
    775         totalBits_max += hQC->elementBits[i]->maxBitResBitsEl;
    776       }
    777     }
    778     for (i = 0; i < cm->nElements; i++) {
    779       if ((cm->elInfo[i].elType == ID_SCE) ||
    780           (cm->elInfo[i].elType == ID_CPE) ||
    781           (cm->elInfo[i].elType == ID_LFE)) {
    782         int deltaBits = fMax(totalBitreservoir - totalBits,
    783                              -hQC->elementBits[i]->bitResLevelEl);
    784         hQC->elementBits[i]->bitResLevelEl += deltaBits;
    785         totalBits += deltaBits;
    786 
    787         deltaBits = fMax(totalBitreservoirMax - totalBits_max,
    788                          -hQC->elementBits[i]->maxBitResBitsEl);
    789         hQC->elementBits[i]->maxBitResBitsEl += deltaBits;
    790         totalBits_max += deltaBits;
    791       }
    792     }
    793   }
    794 
    795   return AAC_ENC_OK;
    796 }
    797 
    798 AAC_ENCODER_ERROR FDKaacEnc_QCMain(QC_STATE* RESTRICT hQC, PSY_OUT** psyOut,
    799                                    QC_OUT** qcOut, INT avgTotalBits,
    800                                    CHANNEL_MAPPING* cm,
    801                                    const AUDIO_OBJECT_TYPE aot,
    802                                    UINT syntaxFlags, SCHAR epConfig) {
    803   int i, c;
    804   AAC_ENCODER_ERROR ErrorStatus = AAC_ENC_OK;
    805   INT avgTotalDynBits = 0; /* maximal allowed dynamic bits for all frames */
    806   INT totalAvailableBits = 0;
    807   INT nSubFrames = 1;
    808 
    809   /*-------------------------------------------- */
    810   /* redistribute total bitreservoir to elements */
    811   ErrorStatus = FDKaacEnc_BitResRedistribution(hQC, cm, avgTotalBits);
    812   if (ErrorStatus != AAC_ENC_OK) {
    813     return ErrorStatus;
    814   }
    815 
    816   /*-------------------------------------------- */
    817   /* fastenc needs one time threshold simulation,
    818      in case of multiple frames, one more guess has to be calculated */
    819 
    820   /*-------------------------------------------- */
    821   /* helper pointer */
    822   QC_OUT_ELEMENT* qcElement[(1)][((8))];
    823 
    824   /* work on a copy of qcChannel and qcElement */
    825   for (i = 0; i < cm->nElements; i++) {
    826     ELEMENT_INFO elInfo = cm->elInfo[i];
    827 
    828     if ((elInfo.elType == ID_SCE) || (elInfo.elType == ID_CPE) ||
    829         (elInfo.elType == ID_LFE)) {
    830       /* for ( all sub frames ) ... */
    831       for (c = 0; c < nSubFrames; c++) {
    832         { qcElement[c][i] = qcOut[c]->qcElement[i]; }
    833       }
    834     }
    835   }
    836 
    837   /*-------------------------------------------- */
    838   /*-------------------------------------------- */
    839   if (isConstantBitrateMode(hQC->bitrateMode)) {
    840     /* calc granted dynamic bits for sub frame and
    841        distribute it to each element */
    842     ErrorStatus = FDKaacEnc_prepareBitDistribution(
    843         hQC, psyOut, qcOut, cm, qcElement, avgTotalBits, &totalAvailableBits,
    844         &avgTotalDynBits);
    845 
    846     if (ErrorStatus != AAC_ENC_OK) {
    847       return ErrorStatus;
    848     }
    849   } else {
    850     qcOut[0]->grantedDynBits =
    851         ((hQC->maxBitsPerFrame - (hQC->globHdrBits)) & ~7) -
    852         (qcOut[0]->globalExtBits + qcOut[0]->staticBits +
    853          qcOut[0]->elementExtBits);
    854     qcOut[0]->maxDynBits = qcOut[0]->grantedDynBits;
    855 
    856     totalAvailableBits = hQC->maxBitsPerFrame;
    857     avgTotalDynBits = 0;
    858   }
    859 
    860   /* for ( all sub frames ) ... */
    861   for (c = 0; c < nSubFrames; c++) {
    862     /* for CBR and VBR mode */
    863     FDKaacEnc_AdjustThresholds(hQC->hAdjThr, qcElement[c], qcOut[c],
    864                                psyOut[c]->psyOutElement,
    865                                isConstantBitrateMode(hQC->bitrateMode), cm);
    866 
    867   } /* -end- sub frame counter */
    868 
    869   /*-------------------------------------------- */
    870   INT iterations[(1)][((8))];
    871   INT chConstraintsFulfilled[(1)][((8))][(2)];
    872   INT calculateQuant[(1)][((8))][(2)];
    873   INT constraintsFulfilled[(1)][((8))];
    874   /*-------------------------------------------- */
    875 
    876   /* for ( all sub frames ) ... */
    877   for (c = 0; c < nSubFrames; c++) {
    878     for (i = 0; i < cm->nElements; i++) {
    879       ELEMENT_INFO elInfo = cm->elInfo[i];
    880       INT ch, nChannels = elInfo.nChannelsInEl;
    881 
    882       if ((elInfo.elType == ID_SCE) || (elInfo.elType == ID_CPE) ||
    883           (elInfo.elType == ID_LFE)) {
    884         /* Turn thresholds into scalefactors, optimize bit consumption and
    885          * verify conformance */
    886         FDKaacEnc_EstimateScaleFactors(
    887             psyOut[c]->psyOutElement[i]->psyOutChannel,
    888             qcElement[c][i]->qcOutChannel, hQC->invQuant, hQC->dZoneQuantEnable,
    889             cm->elInfo[i].nChannelsInEl);
    890 
    891         /*-------------------------------------------- */
    892         constraintsFulfilled[c][i] = 1;
    893         iterations[c][i] = 0;
    894 
    895         for (ch = 0; ch < nChannels; ch++) {
    896           chConstraintsFulfilled[c][i][ch] = 1;
    897           calculateQuant[c][i][ch] = 1;
    898         }
    899 
    900         /*-------------------------------------------- */
    901 
    902       } /*  -end- if(ID_SCE || ID_CPE || ID_LFE) */
    903 
    904     } /* -end- element loop */
    905 
    906     qcOut[c]->usedDynBits = -1;
    907 
    908   } /* -end- sub frame counter */
    909 
    910   INT quantizationDone = 0;
    911   INT sumDynBitsConsumedTotal = 0;
    912   INT decreaseBitConsumption = -1; /* no direction yet! */
    913 
    914   /*-------------------------------------------- */
    915   /* -start- Quantization loop ...               */
    916   /*-------------------------------------------- */
    917   do /* until max allowed bits per frame and maxDynBits!=-1*/
    918   {
    919     quantizationDone = 0;
    920 
    921     c = 0; /* get frame to process */
    922 
    923     for (i = 0; i < cm->nElements; i++) {
    924       ELEMENT_INFO elInfo = cm->elInfo[i];
    925       INT ch, nChannels = elInfo.nChannelsInEl;
    926 
    927       if ((elInfo.elType == ID_SCE) || (elInfo.elType == ID_CPE) ||
    928           (elInfo.elType == ID_LFE)) {
    929         do /* until element bits < nChannels*MIN_BUFSIZE_PER_EFF_CHAN */
    930         {
    931           do /* until spectral values < MAX_QUANT */
    932           {
    933             /*-------------------------------------------- */
    934             if (!constraintsFulfilled[c][i]) {
    935               if ((ErrorStatus = FDKaacEnc_reduceBitConsumption(
    936                        &iterations[c][i], hQC->maxIterations,
    937                        (decreaseBitConsumption) ? 1 : -1,
    938                        chConstraintsFulfilled[c][i], calculateQuant[c][i],
    939                        nChannels, psyOut[c]->psyOutElement[i], qcOut[c],
    940                        qcElement[c][i], hQC->elementBits[i], aot, syntaxFlags,
    941                        epConfig)) != AAC_ENC_OK) {
    942                 return ErrorStatus;
    943               }
    944             }
    945 
    946             /*-------------------------------------------- */
    947             /*-------------------------------------------- */
    948             constraintsFulfilled[c][i] = 1;
    949 
    950             /*-------------------------------------------- */
    951             /* quantize spectrum (per each channel) */
    952             for (ch = 0; ch < nChannels; ch++) {
    953               /*-------------------------------------------- */
    954               chConstraintsFulfilled[c][i][ch] = 1;
    955 
    956               /*-------------------------------------------- */
    957 
    958               if (calculateQuant[c][i][ch]) {
    959                 QC_OUT_CHANNEL* qcOutCh = qcElement[c][i]->qcOutChannel[ch];
    960                 PSY_OUT_CHANNEL* psyOutCh =
    961                     psyOut[c]->psyOutElement[i]->psyOutChannel[ch];
    962 
    963                 calculateQuant[c][i][ch] =
    964                     0; /* calculate quantization only if necessary */
    965 
    966                 /*-------------------------------------------- */
    967                 FDKaacEnc_QuantizeSpectrum(
    968                     psyOutCh->sfbCnt, psyOutCh->maxSfbPerGroup,
    969                     psyOutCh->sfbPerGroup, psyOutCh->sfbOffsets,
    970                     qcOutCh->mdctSpectrum, qcOutCh->globalGain, qcOutCh->scf,
    971                     qcOutCh->quantSpec, hQC->dZoneQuantEnable);
    972 
    973                 /*-------------------------------------------- */
    974                 if (FDKaacEnc_calcMaxValueInSfb(
    975                         psyOutCh->sfbCnt, psyOutCh->maxSfbPerGroup,
    976                         psyOutCh->sfbPerGroup, psyOutCh->sfbOffsets,
    977                         qcOutCh->quantSpec,
    978                         qcOutCh->maxValueInSfb) > MAX_QUANT) {
    979                   chConstraintsFulfilled[c][i][ch] = 0;
    980                   constraintsFulfilled[c][i] = 0;
    981                   /* if quanizted value out of range; increase global gain! */
    982                   decreaseBitConsumption = 1;
    983                 }
    984 
    985                 /*-------------------------------------------- */
    986 
    987               } /* if calculateQuant[c][i][ch] */
    988 
    989             } /* channel loop */
    990 
    991             /*-------------------------------------------- */
    992             /* quantize spectrum (per each channel) */
    993 
    994             /*-------------------------------------------- */
    995 
    996           } while (!constraintsFulfilled[c][i]); /* does not regard bit
    997                                                     consumption */
    998 
    999           /*-------------------------------------------- */
   1000           /*-------------------------------------------- */
   1001           qcElement[c][i]->dynBitsUsed = 0; /* reset dynamic bits */
   1002 
   1003           /* quantization valid in current channel! */
   1004           for (ch = 0; ch < nChannels; ch++) {
   1005             QC_OUT_CHANNEL* qcOutCh = qcElement[c][i]->qcOutChannel[ch];
   1006             PSY_OUT_CHANNEL* psyOutCh =
   1007                 psyOut[c]->psyOutElement[i]->psyOutChannel[ch];
   1008 
   1009             /* count dynamic bits */
   1010             INT chDynBits = FDKaacEnc_dynBitCount(
   1011                 hQC->hBitCounter, qcOutCh->quantSpec, qcOutCh->maxValueInSfb,
   1012                 qcOutCh->scf, psyOutCh->lastWindowSequence, psyOutCh->sfbCnt,
   1013                 psyOutCh->maxSfbPerGroup, psyOutCh->sfbPerGroup,
   1014                 psyOutCh->sfbOffsets, &qcOutCh->sectionData, psyOutCh->noiseNrg,
   1015                 psyOutCh->isBook, psyOutCh->isScale, syntaxFlags);
   1016 
   1017             /* sum up dynamic channel bits */
   1018             qcElement[c][i]->dynBitsUsed += chDynBits;
   1019           }
   1020 
   1021           /* save dynBitsUsed for correction of bits2pe relation */
   1022           if (hQC->hAdjThr->adjThrStateElem[i]->dynBitsLast == -1) {
   1023             hQC->hAdjThr->adjThrStateElem[i]->dynBitsLast =
   1024                 qcElement[c][i]->dynBitsUsed;
   1025           }
   1026 
   1027           /* hold total bit consumption in present element below maximum allowed
   1028            */
   1029           if (qcElement[c][i]->dynBitsUsed >
   1030               ((nChannels * MIN_BUFSIZE_PER_EFF_CHAN) -
   1031                qcElement[c][i]->staticBitsUsed -
   1032                qcElement[c][i]->extBitsUsed)) {
   1033             constraintsFulfilled[c][i] = 0;
   1034           }
   1035 
   1036         } while (!constraintsFulfilled[c][i]);
   1037 
   1038       } /*  -end- if(ID_SCE || ID_CPE || ID_LFE) */
   1039 
   1040     } /* -end- element loop */
   1041 
   1042     /* update dynBits of current subFrame */
   1043     FDKaacEnc_updateUsedDynBits(&qcOut[c]->usedDynBits, qcElement[c], cm);
   1044 
   1045     /* get total consumed bits, dyn bits in all sub frames have to be valid */
   1046     sumDynBitsConsumedTotal =
   1047         FDKaacEnc_getTotalConsumedDynBits(qcOut, nSubFrames);
   1048 
   1049     if (sumDynBitsConsumedTotal == -1) {
   1050       quantizationDone = 0; /* bit consumption not valid in all sub frames */
   1051     } else {
   1052       int sumBitsConsumedTotal = FDKaacEnc_getTotalConsumedBits(
   1053           qcOut, qcElement, cm, hQC->globHdrBits, nSubFrames);
   1054 
   1055       /* in all frames are valid dynamic bits */
   1056       if (((sumBitsConsumedTotal < totalAvailableBits) ||
   1057            sumDynBitsConsumedTotal == 0) &&
   1058           (decreaseBitConsumption == 1) &&
   1059           checkMinFrameBitsDemand(qcOut, hQC->minBitsPerFrame, nSubFrames)
   1060           /*()*/) {
   1061         quantizationDone = 1; /* exit bit adjustment */
   1062       }
   1063       if (sumBitsConsumedTotal > totalAvailableBits &&
   1064           (decreaseBitConsumption == 0)) {
   1065         quantizationDone = 0; /* reset! */
   1066       }
   1067     }
   1068 
   1069     /*-------------------------------------------- */
   1070 
   1071     int emergencyIterations = 1;
   1072     int dynBitsOvershoot = 0;
   1073 
   1074     for (c = 0; c < nSubFrames; c++) {
   1075       for (i = 0; i < cm->nElements; i++) {
   1076         ELEMENT_INFO elInfo = cm->elInfo[i];
   1077 
   1078         if ((elInfo.elType == ID_SCE) || (elInfo.elType == ID_CPE) ||
   1079             (elInfo.elType == ID_LFE)) {
   1080           /* iteration limitation */
   1081           emergencyIterations &=
   1082               ((iterations[c][i] < hQC->maxIterations) ? 0 : 1);
   1083         }
   1084       }
   1085       /* detection if used dyn bits exceeds the maximal allowed criterion */
   1086       dynBitsOvershoot |=
   1087           ((qcOut[c]->usedDynBits > qcOut[c]->maxDynBits) ? 1 : 0);
   1088     }
   1089 
   1090     if (quantizationDone == 0 || dynBitsOvershoot) {
   1091       int sumBitsConsumedTotal = FDKaacEnc_getTotalConsumedBits(
   1092           qcOut, qcElement, cm, hQC->globHdrBits, nSubFrames);
   1093 
   1094       if ((sumDynBitsConsumedTotal >= avgTotalDynBits) ||
   1095           (sumDynBitsConsumedTotal == 0)) {
   1096         quantizationDone = 1;
   1097       }
   1098       if (emergencyIterations && (sumBitsConsumedTotal < totalAvailableBits)) {
   1099         quantizationDone = 1;
   1100       }
   1101       if ((sumBitsConsumedTotal > totalAvailableBits) ||
   1102           !checkMinFrameBitsDemand(qcOut, hQC->minBitsPerFrame, nSubFrames)) {
   1103         quantizationDone = 0;
   1104       }
   1105       if ((sumBitsConsumedTotal < totalAvailableBits) &&
   1106           checkMinFrameBitsDemand(qcOut, hQC->minBitsPerFrame, nSubFrames)) {
   1107         decreaseBitConsumption = 0;
   1108       } else {
   1109         decreaseBitConsumption = 1;
   1110       }
   1111 
   1112       if (dynBitsOvershoot) {
   1113         quantizationDone = 0;
   1114         decreaseBitConsumption = 1;
   1115       }
   1116 
   1117       /* reset constraints fullfilled flags */
   1118       FDKmemclear(constraintsFulfilled, sizeof(constraintsFulfilled));
   1119       FDKmemclear(chConstraintsFulfilled, sizeof(chConstraintsFulfilled));
   1120 
   1121     } /* quantizationDone */
   1122 
   1123   } while (!quantizationDone);
   1124 
   1125   /*-------------------------------------------- */
   1126   /* ... -end- Quantization loop                 */
   1127   /*-------------------------------------------- */
   1128 
   1129   /*-------------------------------------------- */
   1130   /*-------------------------------------------- */
   1131 
   1132   return AAC_ENC_OK;
   1133 }
   1134 
   1135 static AAC_ENCODER_ERROR FDKaacEnc_reduceBitConsumption(
   1136     int* iterations, const int maxIterations, int gainAdjustment,
   1137     int* chConstraintsFulfilled, int* calculateQuant, int nChannels,
   1138     PSY_OUT_ELEMENT* psyOutElement, QC_OUT* qcOut, QC_OUT_ELEMENT* qcOutElement,
   1139     ELEMENT_BITS* elBits, AUDIO_OBJECT_TYPE aot, UINT syntaxFlags,
   1140     SCHAR epConfig) {
   1141   int ch;
   1142 
   1143   /** SOLVING PROBLEM **/
   1144   if ((*iterations) < maxIterations) {
   1145     /* increase gain (+ next iteration) */
   1146     for (ch = 0; ch < nChannels; ch++) {
   1147       if (!chConstraintsFulfilled[ch]) {
   1148         qcOutElement->qcOutChannel[ch]->globalGain += gainAdjustment;
   1149         calculateQuant[ch] = 1; /* global gain has changed, recalculate
   1150                                    quantization in next iteration! */
   1151       }
   1152     }
   1153   } else if ((*iterations) == maxIterations) {
   1154     if (qcOutElement->dynBitsUsed == 0) {
   1155       return AAC_ENC_QUANT_ERROR;
   1156     } else {
   1157       /* crash recovery */
   1158       INT bitsToSave = 0;
   1159       if ((bitsToSave = fixMax(
   1160                (qcOutElement->dynBitsUsed + 8) -
   1161                    (elBits->bitResLevelEl + qcOutElement->grantedDynBits),
   1162                (qcOutElement->dynBitsUsed + qcOutElement->staticBitsUsed + 8) -
   1163                    (elBits->maxBitsEl))) > 0) {
   1164         FDKaacEnc_crashRecovery(nChannels, psyOutElement, qcOut, qcOutElement,
   1165                                 bitsToSave, aot, syntaxFlags, epConfig);
   1166       } else {
   1167         for (ch = 0; ch < nChannels; ch++) {
   1168           qcOutElement->qcOutChannel[ch]->globalGain += 1;
   1169         }
   1170       }
   1171       for (ch = 0; ch < nChannels; ch++) {
   1172         calculateQuant[ch] = 1;
   1173       }
   1174     }
   1175   } else {
   1176     /* (*iterations) > maxIterations */
   1177     return AAC_ENC_QUANT_ERROR;
   1178   }
   1179   (*iterations)++;
   1180 
   1181   return AAC_ENC_OK;
   1182 }
   1183 
   1184 AAC_ENCODER_ERROR FDKaacEnc_updateFillBits(CHANNEL_MAPPING* cm,
   1185                                            QC_STATE* qcKernel,
   1186                                            ELEMENT_BITS* RESTRICT elBits[((8))],
   1187                                            QC_OUT** qcOut) {
   1188   switch (qcKernel->bitrateMode) {
   1189     case QCDATA_BR_MODE_SFR:
   1190       break;
   1191 
   1192     case QCDATA_BR_MODE_FF:
   1193       break;
   1194     case QCDATA_BR_MODE_VBR_1:
   1195     case QCDATA_BR_MODE_VBR_2:
   1196     case QCDATA_BR_MODE_VBR_3:
   1197     case QCDATA_BR_MODE_VBR_4:
   1198     case QCDATA_BR_MODE_VBR_5:
   1199       qcOut[0]->totFillBits =
   1200           (qcOut[0]->grantedDynBits - qcOut[0]->usedDynBits) &
   1201           7; /* precalculate alignment bits */
   1202       qcOut[0]->totalBits = qcOut[0]->staticBits + qcOut[0]->usedDynBits +
   1203                             qcOut[0]->totFillBits + qcOut[0]->elementExtBits +
   1204                             qcOut[0]->globalExtBits;
   1205       qcOut[0]->totFillBits +=
   1206           (fixMax(0, qcKernel->minBitsPerFrame - qcOut[0]->totalBits) + 7) & ~7;
   1207       break;
   1208     case QCDATA_BR_MODE_CBR:
   1209     case QCDATA_BR_MODE_INVALID:
   1210     default:
   1211       INT bitResSpace = qcKernel->bitResTotMax - qcKernel->bitResTot;
   1212       /* processing fill-bits */
   1213       INT deltaBitRes = qcOut[0]->grantedDynBits - qcOut[0]->usedDynBits;
   1214       qcOut[0]->totFillBits = fixMax(
   1215           (deltaBitRes & 7), (deltaBitRes - (fixMax(0, bitResSpace - 7) & ~7)));
   1216       qcOut[0]->totalBits = qcOut[0]->staticBits + qcOut[0]->usedDynBits +
   1217                             qcOut[0]->totFillBits + qcOut[0]->elementExtBits +
   1218                             qcOut[0]->globalExtBits;
   1219       qcOut[0]->totFillBits +=
   1220           (fixMax(0, qcKernel->minBitsPerFrame - qcOut[0]->totalBits) + 7) & ~7;
   1221       break;
   1222   } /* switch (qcKernel->bitrateMode) */
   1223 
   1224   return AAC_ENC_OK;
   1225 }
   1226 
   1227 /*********************************************************************************
   1228 
   1229          functionname: FDKaacEnc_calcMaxValueInSfb
   1230          description:
   1231          return:
   1232 
   1233 **********************************************************************************/
   1234 
   1235 static INT FDKaacEnc_calcMaxValueInSfb(INT sfbCnt, INT maxSfbPerGroup,
   1236                                        INT sfbPerGroup, INT* RESTRICT sfbOffset,
   1237                                        SHORT* RESTRICT quantSpectrum,
   1238                                        UINT* RESTRICT maxValue) {
   1239   INT sfbOffs, sfb;
   1240   INT maxValueAll = 0;
   1241 
   1242   for (sfbOffs = 0; sfbOffs < sfbCnt; sfbOffs += sfbPerGroup)
   1243     for (sfb = 0; sfb < maxSfbPerGroup; sfb++) {
   1244       INT line;
   1245       INT maxThisSfb = 0;
   1246       for (line = sfbOffset[sfbOffs + sfb]; line < sfbOffset[sfbOffs + sfb + 1];
   1247            line++) {
   1248         INT tmp = fixp_abs(quantSpectrum[line]);
   1249         maxThisSfb = fixMax(tmp, maxThisSfb);
   1250       }
   1251 
   1252       maxValue[sfbOffs + sfb] = maxThisSfb;
   1253       maxValueAll = fixMax(maxThisSfb, maxValueAll);
   1254     }
   1255   return maxValueAll;
   1256 }
   1257 
   1258 /*********************************************************************************
   1259 
   1260          functionname: FDKaacEnc_updateBitres
   1261          description:
   1262          return:
   1263 
   1264 **********************************************************************************/
   1265 void FDKaacEnc_updateBitres(CHANNEL_MAPPING* cm, QC_STATE* qcKernel,
   1266                             QC_OUT** qcOut) {
   1267   switch (qcKernel->bitrateMode) {
   1268     case QCDATA_BR_MODE_VBR_1:
   1269     case QCDATA_BR_MODE_VBR_2:
   1270     case QCDATA_BR_MODE_VBR_3:
   1271     case QCDATA_BR_MODE_VBR_4:
   1272     case QCDATA_BR_MODE_VBR_5:
   1273       /* variable bitrate */
   1274       qcKernel->bitResTot =
   1275           fMin(qcKernel->maxBitsPerFrame, qcKernel->bitResTotMax);
   1276       break;
   1277     case QCDATA_BR_MODE_CBR:
   1278     case QCDATA_BR_MODE_SFR:
   1279     case QCDATA_BR_MODE_INVALID:
   1280     default:
   1281       int c = 0;
   1282       /* constant bitrate */
   1283       {
   1284         qcKernel->bitResTot += qcOut[c]->grantedDynBits -
   1285                                (qcOut[c]->usedDynBits + qcOut[c]->totFillBits +
   1286                                 qcOut[c]->alignBits);
   1287       }
   1288       break;
   1289   }
   1290 }
   1291 
   1292 /*********************************************************************************
   1293 
   1294          functionname: FDKaacEnc_FinalizeBitConsumption
   1295          description:
   1296          return:
   1297 
   1298 **********************************************************************************/
   1299 AAC_ENCODER_ERROR FDKaacEnc_FinalizeBitConsumption(
   1300     CHANNEL_MAPPING* cm, QC_STATE* qcKernel, QC_OUT* qcOut,
   1301     QC_OUT_ELEMENT** qcElement, HANDLE_TRANSPORTENC hTpEnc,
   1302     AUDIO_OBJECT_TYPE aot, UINT syntaxFlags, SCHAR epConfig) {
   1303   QC_OUT_EXTENSION fillExtPayload;
   1304   INT totFillBits, alignBits;
   1305 
   1306   /* Get total consumed bits in AU */
   1307   qcOut->totalBits = qcOut->staticBits + qcOut->usedDynBits +
   1308                      qcOut->totFillBits + qcOut->elementExtBits +
   1309                      qcOut->globalExtBits;
   1310 
   1311   if (qcKernel->bitrateMode == QCDATA_BR_MODE_CBR) {
   1312     /* Now we can get the exact transport bit amount, and hopefully it is equal
   1313      * to the estimated value */
   1314     INT exactTpBits = transportEnc_GetStaticBits(hTpEnc, qcOut->totalBits);
   1315 
   1316     if (exactTpBits != qcKernel->globHdrBits) {
   1317       INT diffFillBits = 0;
   1318 
   1319       /* How many bits can be take by bitreservoir */
   1320       const INT bitresSpace =
   1321           qcKernel->bitResTotMax -
   1322           (qcKernel->bitResTot +
   1323            (qcOut->grantedDynBits - (qcOut->usedDynBits + qcOut->totFillBits)));
   1324 
   1325       /* Number of bits which can be moved to bitreservoir. */
   1326       const INT bitsToBitres = qcKernel->globHdrBits - exactTpBits;
   1327       FDK_ASSERT(bitsToBitres >= 0); /* is always positive */
   1328 
   1329       /* If bitreservoir can not take all bits, move ramaining bits to fillbits
   1330        */
   1331       diffFillBits = fMax(0, bitsToBitres - bitresSpace);
   1332 
   1333       /* Assure previous alignment */
   1334       diffFillBits = (diffFillBits + 7) & ~7;
   1335 
   1336       /* Move as many bits as possible to bitreservoir */
   1337       qcKernel->bitResTot += (bitsToBitres - diffFillBits);
   1338 
   1339       /* Write remaing bits as fill bits */
   1340       qcOut->totFillBits += diffFillBits;
   1341       qcOut->totalBits += diffFillBits;
   1342       qcOut->grantedDynBits += diffFillBits;
   1343 
   1344       /* Get new header bits */
   1345       qcKernel->globHdrBits =
   1346           transportEnc_GetStaticBits(hTpEnc, qcOut->totalBits);
   1347 
   1348       if (qcKernel->globHdrBits != exactTpBits) {
   1349         /* In previous step, fill bits and corresponding total bits were changed
   1350            when bitreservoir was completely filled. Now we can take the too much
   1351            taken bits caused by header overhead from bitreservoir.
   1352          */
   1353         qcKernel->bitResTot -= (qcKernel->globHdrBits - exactTpBits);
   1354       }
   1355     }
   1356 
   1357   } /* MODE_CBR */
   1358 
   1359   /* Update exact number of consumed header bits. */
   1360   qcKernel->globHdrBits = transportEnc_GetStaticBits(hTpEnc, qcOut->totalBits);
   1361 
   1362   /* Save total fill bits and distribut to alignment and fill bits */
   1363   totFillBits = qcOut->totFillBits;
   1364 
   1365   /* fake a fill extension payload */
   1366   FDKmemclear(&fillExtPayload, sizeof(QC_OUT_EXTENSION));
   1367 
   1368   fillExtPayload.type = EXT_FILL_DATA;
   1369   fillExtPayload.nPayloadBits = totFillBits;
   1370 
   1371   /* ask bitstream encoder how many of that bits can be written in a fill
   1372    * extension data entity */
   1373   qcOut->totFillBits = FDKaacEnc_writeExtensionData(NULL, &fillExtPayload, 0, 0,
   1374                                                     syntaxFlags, aot, epConfig);
   1375 
   1376   /* now distribute extra fillbits and alignbits */
   1377   alignBits =
   1378       7 - (qcOut->staticBits + qcOut->usedDynBits + qcOut->elementExtBits +
   1379            qcOut->totFillBits + qcOut->globalExtBits - 1) %
   1380               8;
   1381 
   1382   /* Maybe we could remove this */
   1383   if (((alignBits + qcOut->totFillBits - totFillBits) == 8) &&
   1384       (qcOut->totFillBits > 8))
   1385     qcOut->totFillBits -= 8;
   1386 
   1387   qcOut->totalBits = qcOut->staticBits + qcOut->usedDynBits +
   1388                      qcOut->totFillBits + alignBits + qcOut->elementExtBits +
   1389                      qcOut->globalExtBits;
   1390 
   1391   if ((qcOut->totalBits > qcKernel->maxBitsPerFrame) ||
   1392       (qcOut->totalBits < qcKernel->minBitsPerFrame)) {
   1393     return AAC_ENC_QUANT_ERROR;
   1394   }
   1395 
   1396   qcOut->alignBits = alignBits;
   1397 
   1398   return AAC_ENC_OK;
   1399 }
   1400 
   1401 /*********************************************************************************
   1402 
   1403          functionname: FDKaacEnc_crashRecovery
   1404          description:  fulfills constraints by means of brute force...
   1405                        => bits are saved by cancelling out spectral lines!!
   1406                           (beginning at the highest frequencies)
   1407          return:       errorcode
   1408 
   1409 **********************************************************************************/
   1410 
   1411 static void FDKaacEnc_crashRecovery(INT nChannels,
   1412                                     PSY_OUT_ELEMENT* psyOutElement,
   1413                                     QC_OUT* qcOut, QC_OUT_ELEMENT* qcElement,
   1414                                     INT bitsToSave, AUDIO_OBJECT_TYPE aot,
   1415                                     UINT syntaxFlags, SCHAR epConfig) {
   1416   INT ch;
   1417   INT savedBits = 0;
   1418   INT sfb, sfbGrp;
   1419   INT bitsPerScf[(2)][MAX_GROUPED_SFB];
   1420   INT sectionToScf[(2)][MAX_GROUPED_SFB];
   1421   INT* sfbOffset;
   1422   INT sect, statBitsNew;
   1423   QC_OUT_CHANNEL** qcChannel = qcElement->qcOutChannel;
   1424   PSY_OUT_CHANNEL** psyChannel = psyOutElement->psyOutChannel;
   1425 
   1426   /* create a table which converts frq-bins to bit-demand...    [bitsPerScf] */
   1427   /* ...and another one which holds the corresponding sections [sectionToScf] */
   1428   for (ch = 0; ch < nChannels; ch++) {
   1429     sfbOffset = psyChannel[ch]->sfbOffsets;
   1430 
   1431     for (sect = 0; sect < qcChannel[ch]->sectionData.noOfSections; sect++) {
   1432       INT codeBook = qcChannel[ch]->sectionData.huffsection[sect].codeBook;
   1433 
   1434       for (sfb = qcChannel[ch]->sectionData.huffsection[sect].sfbStart;
   1435            sfb < qcChannel[ch]->sectionData.huffsection[sect].sfbStart +
   1436                      qcChannel[ch]->sectionData.huffsection[sect].sfbCnt;
   1437            sfb++) {
   1438         bitsPerScf[ch][sfb] = 0;
   1439         if ((codeBook != CODE_BOOK_PNS_NO) /*&&
   1440              (sfb < (qcChannel[ch]->sectionData.noOfGroups*qcChannel[ch]->sectionData.maxSfbPerGroup))*/) {
   1441           INT sfbStartLine = sfbOffset[sfb];
   1442           INT noOfLines = sfbOffset[sfb + 1] - sfbStartLine;
   1443           bitsPerScf[ch][sfb] = FDKaacEnc_countValues(
   1444               &(qcChannel[ch]->quantSpec[sfbStartLine]), noOfLines, codeBook);
   1445         }
   1446         sectionToScf[ch][sfb] = sect;
   1447       }
   1448     }
   1449   }
   1450 
   1451   /* LOWER [maxSfb] IN BOTH CHANNELS!! */
   1452   /* Attention: in case of stereo: maxSfbL == maxSfbR, GroupingL == GroupingR ;
   1453    */
   1454 
   1455   for (sfb = qcChannel[0]->sectionData.maxSfbPerGroup - 1; sfb >= 0; sfb--) {
   1456     for (sfbGrp = 0; sfbGrp < psyChannel[0]->sfbCnt;
   1457          sfbGrp += psyChannel[0]->sfbPerGroup) {
   1458       for (ch = 0; ch < nChannels; ch++) {
   1459         sect = sectionToScf[ch][sfbGrp + sfb];
   1460         qcChannel[ch]->sectionData.huffsection[sect].sfbCnt--;
   1461         savedBits += bitsPerScf[ch][sfbGrp + sfb];
   1462 
   1463         if (qcChannel[ch]->sectionData.huffsection[sect].sfbCnt == 0) {
   1464           savedBits += (psyChannel[ch]->lastWindowSequence != SHORT_WINDOW)
   1465                            ? FDKaacEnc_sideInfoTabLong[0]
   1466                            : FDKaacEnc_sideInfoTabShort[0];
   1467         }
   1468       }
   1469     }
   1470 
   1471     /* ...have enough bits been saved? */
   1472     if (savedBits >= bitsToSave) break;
   1473 
   1474   } /* sfb loop */
   1475 
   1476   /* if not enough bits saved,
   1477      clean whole spectrum and remove side info overhead */
   1478   if (sfb == -1) {
   1479     sfb = 0;
   1480   }
   1481 
   1482   for (ch = 0; ch < nChannels; ch++) {
   1483     qcChannel[ch]->sectionData.maxSfbPerGroup = sfb;
   1484     psyChannel[ch]->maxSfbPerGroup = sfb;
   1485     /* when no spectrum is coded save tools info in bitstream */
   1486     if (sfb == 0) {
   1487       FDKmemclear(&psyChannel[ch]->tnsInfo, sizeof(TNS_INFO));
   1488       FDKmemclear(&psyOutElement->toolsInfo, sizeof(TOOLSINFO));
   1489     }
   1490   }
   1491   /* dynamic bits will be updated in iteration loop */
   1492 
   1493   { /* if stop sfb has changed save bits in side info, e.g. MS or TNS coding */
   1494     ELEMENT_INFO elInfo;
   1495 
   1496     FDKmemclear(&elInfo, sizeof(ELEMENT_INFO));
   1497     elInfo.nChannelsInEl = nChannels;
   1498     elInfo.elType = (nChannels == 2) ? ID_CPE : ID_SCE;
   1499 
   1500     FDKaacEnc_ChannelElementWrite(NULL, &elInfo, NULL, psyOutElement,
   1501                                   psyChannel, syntaxFlags, aot, epConfig,
   1502                                   &statBitsNew, 0);
   1503   }
   1504 
   1505   savedBits = qcElement->staticBitsUsed - statBitsNew;
   1506 
   1507   /* update static and dynamic bits */
   1508   qcElement->staticBitsUsed -= savedBits;
   1509   qcElement->grantedDynBits += savedBits;
   1510 
   1511   qcOut->staticBits -= savedBits;
   1512   qcOut->grantedDynBits += savedBits;
   1513   qcOut->maxDynBits += savedBits;
   1514 }
   1515 
   1516 void FDKaacEnc_QCClose(QC_STATE** phQCstate, QC_OUT** phQC) {
   1517   int n, i;
   1518 
   1519   if (phQC != NULL) {
   1520     for (n = 0; n < (1); n++) {
   1521       if (phQC[n] != NULL) {
   1522         QC_OUT* hQC = phQC[n];
   1523         for (i = 0; i < (8); i++) {
   1524         }
   1525 
   1526         for (i = 0; i < ((8)); i++) {
   1527           if (hQC->qcElement[i]) FreeRam_aacEnc_QCelement(&hQC->qcElement[i]);
   1528         }
   1529 
   1530         FreeRam_aacEnc_QCout(&phQC[n]);
   1531       }
   1532     }
   1533   }
   1534 
   1535   if (phQCstate != NULL) {
   1536     if (*phQCstate != NULL) {
   1537       QC_STATE* hQCstate = *phQCstate;
   1538 
   1539       if (hQCstate->hAdjThr != NULL) FDKaacEnc_AdjThrClose(&hQCstate->hAdjThr);
   1540 
   1541       if (hQCstate->hBitCounter != NULL)
   1542         FDKaacEnc_BCClose(&hQCstate->hBitCounter);
   1543 
   1544       for (i = 0; i < ((8)); i++) {
   1545         if (hQCstate->elementBits[i] != NULL) {
   1546           FreeRam_aacEnc_ElementBits(&hQCstate->elementBits[i]);
   1547         }
   1548       }
   1549       FreeRam_aacEnc_QCstate(phQCstate);
   1550     }
   1551   }
   1552 }
   1553