"Fossies" - the Fresh Open Source Software Archive

Member "faac-1_30/libfaac/frame.c" (16 Oct 2019, 30071 Bytes) of package /linux/misc/faac-1_30.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "frame.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.28_vs_1.29.9.2.

    1 /*
    2  * FAAC - Freeware Advanced Audio Coder
    3  * Copyright (C) 2001 Menno Bakker
    4  *
    5  * This library is free software; you can redistribute it and/or
    6  * modify it under the terms of the GNU Lesser General Public
    7  * License as published by the Free Software Foundation; either
    8  * version 2.1 of the License, or (at your option) any later version.
    9  *
   10  * This library is distributed in the hope that it will be useful,
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   13  * Lesser General Public License for more details.
   14 
   15  * You should have received a copy of the GNU Lesser General Public
   16  * License along with this library; if not, write to the Free Software
   17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   18  *
   19  */
   20 
   21 #include <stdio.h>
   22 #include <stdlib.h>
   23 #include <assert.h>
   24 #include <string.h>
   25 
   26 #include "frame.h"
   27 #include "coder.h"
   28 #include "channels.h"
   29 #include "bitstream.h"
   30 #include "filtbank.h"
   31 #include "util.h"
   32 #include "tns.h"
   33 #include "stereo.h"
   34 
   35 #if (defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined(PACKAGE_VERSION)
   36 #include "win32_ver.h"
   37 #endif
   38 
   39 static char *libfaacName = PACKAGE_VERSION;
   40 static char *libCopyright =
   41   "FAAC - Freeware Advanced Audio Coder (http://faac.sourceforge.net/)\n"
   42   " Copyright (C) 1999,2000,2001  Menno Bakker\n"
   43   " Copyright (C) 2002,2003,2017  Krzysztof Nikiel\n"
   44   "This software is based on the ISO MPEG-4 reference source code.\n";
   45 
   46 static const psymodellist_t psymodellist[] = {
   47   {&psymodel2, "knipsycho psychoacoustic"},
   48   {NULL}
   49 };
   50 
   51 static SR_INFO srInfo[12+1];
   52 
   53 // default bandwidth/samplerate ratio
   54 static const struct {
   55     double fac;
   56     double freq;
   57 } g_bw = {0.42, 18000};
   58 
   59 int FAACAPI faacEncGetVersion( char **faac_id_string,
   60                                 char **faac_copyright_string)
   61 {
   62   if (faac_id_string)
   63     *faac_id_string = libfaacName;
   64 
   65   if (faac_copyright_string)
   66     *faac_copyright_string = libCopyright;
   67 
   68   return FAAC_CFG_VERSION;
   69 }
   70 
   71 
   72 int FAACAPI faacEncGetDecoderSpecificInfo(faacEncHandle hpEncoder,unsigned char** ppBuffer,unsigned long* pSizeOfDecoderSpecificInfo)
   73 {
   74     faacEncStruct* hEncoder = (faacEncStruct*)hpEncoder;
   75     BitStream* pBitStream = NULL;
   76 
   77     if((hEncoder == NULL) || (ppBuffer == NULL) || (pSizeOfDecoderSpecificInfo == NULL)) {
   78         return -1;
   79     }
   80 
   81     if(hEncoder->config.mpegVersion == MPEG2){
   82         return -2; /* not supported */
   83     }
   84 
   85     *pSizeOfDecoderSpecificInfo = 2;
   86     *ppBuffer = malloc(2);
   87 
   88     if(*ppBuffer != NULL){
   89 
   90         memset(*ppBuffer,0,*pSizeOfDecoderSpecificInfo);
   91         pBitStream = OpenBitStream(*pSizeOfDecoderSpecificInfo, *ppBuffer);
   92         PutBit(pBitStream, hEncoder->config.aacObjectType, 5);
   93         PutBit(pBitStream, hEncoder->sampleRateIdx, 4);
   94         PutBit(pBitStream, hEncoder->numChannels, 4);
   95         CloseBitStream(pBitStream);
   96 
   97         return 0;
   98     } else {
   99         return -3;
  100     }
  101 }
  102 
  103 
  104 faacEncConfigurationPtr FAACAPI faacEncGetCurrentConfiguration(faacEncHandle hpEncoder)
  105 {
  106     faacEncStruct* hEncoder = (faacEncStruct*)hpEncoder;
  107     faacEncConfigurationPtr config = &(hEncoder->config);
  108 
  109     return config;
  110 }
  111 
  112 int FAACAPI faacEncSetConfiguration(faacEncHandle hpEncoder,
  113                                     faacEncConfigurationPtr config)
  114 {
  115     faacEncStruct* hEncoder = (faacEncStruct*)hpEncoder;
  116     int i;
  117     int maxqual = hEncoder->config.outputFormat ? MAXQUALADTS : MAXQUAL;
  118 
  119     hEncoder->config.jointmode = config->jointmode;
  120     hEncoder->config.useLfe = config->useLfe;
  121     hEncoder->config.useTns = config->useTns;
  122     hEncoder->config.aacObjectType = config->aacObjectType;
  123     hEncoder->config.mpegVersion = config->mpegVersion;
  124     hEncoder->config.outputFormat = config->outputFormat;
  125     hEncoder->config.inputFormat = config->inputFormat;
  126     hEncoder->config.shortctl = config->shortctl;
  127 
  128     assert((hEncoder->config.outputFormat == 0) || (hEncoder->config.outputFormat == 1));
  129 
  130     switch( hEncoder->config.inputFormat )
  131     {
  132         case FAAC_INPUT_16BIT:
  133         //case FAAC_INPUT_24BIT:
  134         case FAAC_INPUT_32BIT:
  135         case FAAC_INPUT_FLOAT:
  136             break;
  137 
  138         default:
  139             return 0;
  140             break;
  141     }
  142 
  143     if (hEncoder->config.aacObjectType != LOW)
  144         return 0;
  145 
  146 #ifdef DRM
  147     config->pnslevel = 0;
  148 #endif
  149 
  150     /* Re-init TNS for new profile */
  151     TnsInit(hEncoder);
  152 
  153     /* Check for correct bitrate */
  154     if (!hEncoder->sampleRate || !hEncoder->numChannels)
  155         return 0;
  156     if (config->bitRate > (MaxBitrate(hEncoder->sampleRate) / hEncoder->numChannels))
  157         config->bitRate = MaxBitrate(hEncoder->sampleRate) / hEncoder->numChannels;
  158 #if 0
  159     if (config->bitRate < MinBitrate())
  160         return 0;
  161 #endif
  162 
  163     if (config->bitRate && !config->bandWidth)
  164     {
  165         config->bandWidth = (double)config->bitRate * hEncoder->sampleRate * g_bw.fac / 50000.0;
  166         if (config->bandWidth > g_bw.freq)
  167             config->bandWidth = g_bw.freq;
  168 
  169         if (!config->quantqual)
  170         {
  171             config->quantqual = (double)config->bitRate * hEncoder->numChannels / 1280;
  172             if (config->quantqual > 100)
  173                 config->quantqual = (config->quantqual - 100) * 3.0 + 100;
  174         }
  175     }
  176 
  177     if (!config->quantqual)
  178         config->quantqual = DEFQUAL;
  179 
  180     hEncoder->config.bitRate = config->bitRate;
  181 
  182     if (!config->bandWidth)
  183     {
  184         config->bandWidth = g_bw.fac * hEncoder->sampleRate;
  185     }
  186 
  187     hEncoder->config.bandWidth = config->bandWidth;
  188 
  189     // check bandwidth
  190     if (hEncoder->config.bandWidth < 100)
  191         hEncoder->config.bandWidth = 100;
  192     if (hEncoder->config.bandWidth > (hEncoder->sampleRate / 2))
  193         hEncoder->config.bandWidth = hEncoder->sampleRate / 2;
  194 
  195     if (config->quantqual > maxqual)
  196         config->quantqual = maxqual;
  197     if (config->quantqual < MINQUAL)
  198         config->quantqual = MINQUAL;
  199 
  200     hEncoder->config.quantqual = config->quantqual;
  201 
  202     if (config->jointmode == JOINT_MS)
  203         config->pnslevel = 0;
  204     if (config->pnslevel < 0)
  205         config->pnslevel = 0;
  206     if (config->pnslevel > 10)
  207         config->pnslevel = 10;
  208     hEncoder->aacquantCfg.pnslevel = config->pnslevel;
  209     /* set quantization quality */
  210     hEncoder->aacquantCfg.quality = config->quantqual;
  211     CalcBW(&hEncoder->config.bandWidth,
  212               hEncoder->sampleRate,
  213               hEncoder->srInfo,
  214               &hEncoder->aacquantCfg);
  215 
  216     // reset psymodel
  217     hEncoder->psymodel->PsyEnd(&hEncoder->gpsyInfo, hEncoder->psyInfo, hEncoder->numChannels);
  218     if (config->psymodelidx >= (sizeof(psymodellist) / sizeof(psymodellist[0]) - 1))
  219         config->psymodelidx = (sizeof(psymodellist) / sizeof(psymodellist[0])) - 2;
  220 
  221     hEncoder->config.psymodelidx = config->psymodelidx;
  222     hEncoder->psymodel = (psymodel_t *)psymodellist[hEncoder->config.psymodelidx].ptr;
  223     hEncoder->psymodel->PsyInit(&hEncoder->gpsyInfo, hEncoder->psyInfo, hEncoder->numChannels,
  224             hEncoder->sampleRate, hEncoder->srInfo->cb_width_long,
  225             hEncoder->srInfo->num_cb_long, hEncoder->srInfo->cb_width_short,
  226             hEncoder->srInfo->num_cb_short);
  227 
  228     /* load channel_map */
  229     for( i = 0; i < MAX_CHANNELS; i++ )
  230         hEncoder->config.channel_map[i] = config->channel_map[i];
  231 
  232     /* OK */
  233     return 1;
  234 }
  235 
  236 faacEncHandle FAACAPI faacEncOpen(unsigned long sampleRate,
  237                                   unsigned int numChannels,
  238                                   unsigned long *inputSamples,
  239                                   unsigned long *maxOutputBytes)
  240 {
  241     unsigned int channel;
  242     faacEncStruct* hEncoder;
  243 
  244     if (numChannels > MAX_CHANNELS)
  245     return NULL;
  246 
  247     *inputSamples = FRAME_LEN*numChannels;
  248     *maxOutputBytes = ADTS_FRAMESIZE;
  249 
  250 #ifdef DRM
  251     *maxOutputBytes += 1; /* for CRC */
  252 #endif
  253 
  254     hEncoder = (faacEncStruct*)AllocMemory(sizeof(faacEncStruct));
  255     SetMemory(hEncoder, 0, sizeof(faacEncStruct));
  256 
  257     hEncoder->numChannels = numChannels;
  258     hEncoder->sampleRate = sampleRate;
  259     hEncoder->sampleRateIdx = GetSRIndex(sampleRate);
  260 
  261     /* Initialize variables to default values */
  262     hEncoder->frameNum = 0;
  263     hEncoder->flushFrame = 0;
  264 
  265     /* Default configuration */
  266     hEncoder->config.version = FAAC_CFG_VERSION;
  267     hEncoder->config.name = libfaacName;
  268     hEncoder->config.copyright = libCopyright;
  269     hEncoder->config.mpegVersion = MPEG4;
  270     hEncoder->config.aacObjectType = LOW;
  271     hEncoder->config.jointmode = JOINT_IS;
  272     hEncoder->config.pnslevel = 4;
  273     hEncoder->config.useLfe = 1;
  274     hEncoder->config.useTns = 0;
  275     hEncoder->config.bitRate = 64000;
  276     hEncoder->config.bandWidth = g_bw.fac * hEncoder->sampleRate;
  277     hEncoder->config.quantqual = 0;
  278     hEncoder->config.psymodellist = (psymodellist_t *)psymodellist;
  279     hEncoder->config.psymodelidx = 0;
  280     hEncoder->psymodel =
  281       (psymodel_t *)hEncoder->config.psymodellist[hEncoder->config.psymodelidx].ptr;
  282     hEncoder->config.shortctl = SHORTCTL_NORMAL;
  283 
  284     /* default channel map is straight-through */
  285     for( channel = 0; channel < MAX_CHANNELS; channel++ )
  286         hEncoder->config.channel_map[channel] = channel;
  287 
  288     hEncoder->config.outputFormat = ADTS_STREAM;
  289 
  290     /*
  291         be compatible with software which assumes 24bit in 32bit PCM
  292     */
  293     hEncoder->config.inputFormat = FAAC_INPUT_32BIT;
  294 
  295     /* find correct sampling rate depending parameters */
  296     hEncoder->srInfo = &srInfo[hEncoder->sampleRateIdx];
  297 
  298     for (channel = 0; channel < numChannels; channel++)
  299     {
  300         hEncoder->coderInfo[channel].prev_window_shape = SINE_WINDOW;
  301         hEncoder->coderInfo[channel].window_shape = SINE_WINDOW;
  302         hEncoder->coderInfo[channel].block_type = ONLY_LONG_WINDOW;
  303         hEncoder->coderInfo[channel].groups.n = 1;
  304         hEncoder->coderInfo[channel].groups.len[0] = 1;
  305 
  306         hEncoder->sampleBuff[channel] = NULL;
  307         hEncoder->nextSampleBuff[channel] = NULL;
  308         hEncoder->next2SampleBuff[channel] = NULL;
  309     }
  310 
  311     /* Initialize coder functions */
  312     fft_initialize( &hEncoder->fft_tables );
  313 
  314     hEncoder->psymodel->PsyInit(&hEncoder->gpsyInfo, hEncoder->psyInfo, hEncoder->numChannels,
  315         hEncoder->sampleRate, hEncoder->srInfo->cb_width_long,
  316         hEncoder->srInfo->num_cb_long, hEncoder->srInfo->cb_width_short,
  317         hEncoder->srInfo->num_cb_short);
  318 
  319     FilterBankInit(hEncoder);
  320 
  321     TnsInit(hEncoder);
  322 
  323     /* Return handle */
  324     return hEncoder;
  325 }
  326 
  327 int FAACAPI faacEncClose(faacEncHandle hpEncoder)
  328 {
  329     faacEncStruct* hEncoder = (faacEncStruct*)hpEncoder;
  330     unsigned int channel;
  331 
  332     /* Deinitialize coder functions */
  333     hEncoder->psymodel->PsyEnd(&hEncoder->gpsyInfo, hEncoder->psyInfo, hEncoder->numChannels);
  334 
  335     FilterBankEnd(hEncoder);
  336 
  337     fft_terminate(&hEncoder->fft_tables);
  338 
  339     /* Free remaining buffer memory */
  340     for (channel = 0; channel < hEncoder->numChannels; channel++)
  341     {
  342         if (hEncoder->sampleBuff[channel])
  343             FreeMemory(hEncoder->sampleBuff[channel]);
  344         if (hEncoder->nextSampleBuff[channel])
  345             FreeMemory(hEncoder->nextSampleBuff[channel]);
  346         if (hEncoder->next2SampleBuff[channel])
  347             FreeMemory (hEncoder->next2SampleBuff[channel]);
  348         if (hEncoder->next3SampleBuff[channel])
  349             FreeMemory (hEncoder->next3SampleBuff[channel]);
  350     }
  351 
  352     /* Free handle */
  353     if (hEncoder)
  354         FreeMemory(hEncoder);
  355 
  356     BlocStat();
  357 
  358     return 0;
  359 }
  360 
  361 int FAACAPI faacEncEncode(faacEncHandle hpEncoder,
  362                           int32_t *inputBuffer,
  363                           unsigned int samplesInput,
  364                           unsigned char *outputBuffer,
  365                           unsigned int bufferSize
  366                           )
  367 {
  368     faacEncStruct* hEncoder = (faacEncStruct*)hpEncoder;
  369     unsigned int channel, i;
  370     int sb, frameBytes;
  371     unsigned int offset;
  372     BitStream *bitStream; /* bitstream used for writing the frame to */
  373 #ifdef DRM
  374     int desbits, diff;
  375     double fix;
  376 #endif
  377 
  378     /* local copy's of parameters */
  379     ChannelInfo *channelInfo = hEncoder->channelInfo;
  380     CoderInfo *coderInfo = hEncoder->coderInfo;
  381     unsigned int numChannels = hEncoder->numChannels;
  382     unsigned int useLfe = hEncoder->config.useLfe;
  383     unsigned int useTns = hEncoder->config.useTns;
  384     unsigned int jointmode = hEncoder->config.jointmode;
  385     unsigned int bandWidth = hEncoder->config.bandWidth;
  386     unsigned int shortctl = hEncoder->config.shortctl;
  387     int maxqual = hEncoder->config.outputFormat ? MAXQUALADTS : MAXQUAL;
  388 
  389     /* Increase frame number */
  390     hEncoder->frameNum++;
  391 
  392     if (samplesInput == 0)
  393         hEncoder->flushFrame++;
  394 
  395     /* After 4 flush frames all samples have been encoded,
  396        return 0 bytes written */
  397     if (hEncoder->flushFrame > 4)
  398         return 0;
  399 
  400     /* Determine the channel configuration */
  401     GetChannelInfo(channelInfo, numChannels, useLfe);
  402 
  403     /* Update current sample buffers */
  404     for (channel = 0; channel < numChannels; channel++)
  405     {
  406         double *tmp;
  407 
  408 
  409         if (!hEncoder->sampleBuff[channel])
  410             hEncoder->sampleBuff[channel] = (double*)AllocMemory(FRAME_LEN*sizeof(double));
  411 
  412         tmp = hEncoder->sampleBuff[channel];
  413 
  414         hEncoder->sampleBuff[channel]       = hEncoder->nextSampleBuff[channel];
  415         hEncoder->nextSampleBuff[channel]   = hEncoder->next2SampleBuff[channel];
  416         hEncoder->next2SampleBuff[channel]  = hEncoder->next3SampleBuff[channel];
  417         hEncoder->next3SampleBuff[channel]  = tmp;
  418 
  419         if (samplesInput == 0)
  420         {
  421             /* start flushing*/
  422             for (i = 0; i < FRAME_LEN; i++)
  423                 hEncoder->next3SampleBuff[channel][i] = 0.0;
  424         }
  425         else
  426         {
  427             int samples_per_channel = samplesInput/numChannels;
  428 
  429             /* handle the various input formats and channel remapping */
  430             switch( hEncoder->config.inputFormat )
  431             {
  432                 case FAAC_INPUT_16BIT:
  433                     {
  434                         short *input_channel = (short*)inputBuffer + hEncoder->config.channel_map[channel];
  435 
  436                         for (i = 0; i < samples_per_channel; i++)
  437                         {
  438                             hEncoder->next3SampleBuff[channel][i] = (double)*input_channel;
  439                             input_channel += numChannels;
  440                         }
  441                     }
  442                     break;
  443 
  444                 case FAAC_INPUT_32BIT:
  445                     {
  446                         int32_t *input_channel = (int32_t*)inputBuffer + hEncoder->config.channel_map[channel];
  447 
  448                         for (i = 0; i < samples_per_channel; i++)
  449                         {
  450                             hEncoder->next3SampleBuff[channel][i] = (1.0/256) * (double)*input_channel;
  451                             input_channel += numChannels;
  452                         }
  453                     }
  454                     break;
  455 
  456                 case FAAC_INPUT_FLOAT:
  457                     {
  458                         float *input_channel = (float*)inputBuffer + hEncoder->config.channel_map[channel];
  459 
  460                         for (i = 0; i < samples_per_channel; i++)
  461                         {
  462                             hEncoder->next3SampleBuff[channel][i] = (double)*input_channel;
  463                             input_channel += numChannels;
  464                         }
  465                     }
  466                     break;
  467 
  468                 default:
  469                     return -1; /* invalid input format */
  470                     break;
  471             }
  472 
  473             for (i = (int)(samplesInput/numChannels); i < FRAME_LEN; i++)
  474                 hEncoder->next3SampleBuff[channel][i] = 0.0;
  475         }
  476 
  477         /* Psychoacoustics */
  478         /* Update buffers and run FFT on new samples */
  479         /* LFE psychoacoustic can run without it */
  480         if (!channelInfo[channel].lfe || channelInfo[channel].cpe)
  481         {
  482             hEncoder->psymodel->PsyBufferUpdate(
  483                     &hEncoder->fft_tables,
  484                     &hEncoder->gpsyInfo,
  485                     &hEncoder->psyInfo[channel],
  486                     hEncoder->next3SampleBuff[channel],
  487                     bandWidth,
  488                     hEncoder->srInfo->cb_width_short,
  489                     hEncoder->srInfo->num_cb_short);
  490         }
  491     }
  492 
  493     if (hEncoder->frameNum <= 3) /* Still filling up the buffers */
  494         return 0;
  495 
  496     /* Psychoacoustics */
  497     hEncoder->psymodel->PsyCalculate(channelInfo, &hEncoder->gpsyInfo, hEncoder->psyInfo,
  498         hEncoder->srInfo->cb_width_long, hEncoder->srInfo->num_cb_long,
  499         hEncoder->srInfo->cb_width_short,
  500         hEncoder->srInfo->num_cb_short, numChannels, (double)hEncoder->aacquantCfg.quality / DEFQUAL);
  501 
  502     hEncoder->psymodel->BlockSwitch(coderInfo, hEncoder->psyInfo, numChannels);
  503 
  504     /* force block type */
  505     if (shortctl == SHORTCTL_NOSHORT)
  506     {
  507         for (channel = 0; channel < numChannels; channel++)
  508         {
  509             coderInfo[channel].block_type = ONLY_LONG_WINDOW;
  510         }
  511     }
  512     else if ((hEncoder->frameNum <= 4) || (shortctl == SHORTCTL_NOLONG))
  513     {
  514         for (channel = 0; channel < numChannels; channel++)
  515         {
  516             coderInfo[channel].block_type = ONLY_SHORT_WINDOW;
  517         }
  518     }
  519 
  520     /* AAC Filterbank, MDCT with overlap and add */
  521     for (channel = 0; channel < numChannels; channel++) {
  522         FilterBank(hEncoder,
  523             &coderInfo[channel],
  524             hEncoder->sampleBuff[channel],
  525             hEncoder->freqBuff[channel],
  526             hEncoder->overlapBuff[channel],
  527             MOVERLAPPED);
  528     }
  529 
  530     for (channel = 0; channel < numChannels; channel++) {
  531         channelInfo[channel].msInfo.is_present = 0;
  532 
  533         if (coderInfo[channel].block_type == ONLY_SHORT_WINDOW) {
  534             coderInfo[channel].sfbn = hEncoder->aacquantCfg.max_cbs;
  535 
  536             offset = 0;
  537             for (sb = 0; sb < coderInfo[channel].sfbn; sb++) {
  538                 coderInfo[channel].sfb_offset[sb] = offset;
  539                 offset += hEncoder->srInfo->cb_width_short[sb];
  540             }
  541             coderInfo[channel].sfb_offset[sb] = offset;
  542             BlocGroup(hEncoder->freqBuff[channel], coderInfo + channel, &hEncoder->aacquantCfg);
  543         } else {
  544             coderInfo[channel].sfbn = hEncoder->aacquantCfg.max_cbl;
  545 
  546             coderInfo[channel].groups.n = 1;
  547             coderInfo[channel].groups.len[0] = 1;
  548 
  549             offset = 0;
  550             for (sb = 0; sb < coderInfo[channel].sfbn; sb++) {
  551                 coderInfo[channel].sfb_offset[sb] = offset;
  552                 offset += hEncoder->srInfo->cb_width_long[sb];
  553             }
  554             coderInfo[channel].sfb_offset[sb] = offset;
  555         }
  556     }
  557 
  558     /* Perform TNS analysis and filtering */
  559     for (channel = 0; channel < numChannels; channel++) {
  560         if ((!channelInfo[channel].lfe) && (useTns)) {
  561             TnsEncode(&(coderInfo[channel].tnsInfo),
  562                       coderInfo[channel].sfbn,
  563                       coderInfo[channel].sfbn,
  564                       coderInfo[channel].block_type,
  565                       coderInfo[channel].sfb_offset,
  566                       hEncoder->freqBuff[channel]);
  567         } else {
  568             coderInfo[channel].tnsInfo.tnsDataPresent = 0;      /* TNS not used for LFE */
  569         }
  570     }
  571 
  572     for (channel = 0; channel < numChannels; channel++) {
  573       // reduce LFE bandwidth
  574         if (!channelInfo[channel].cpe && channelInfo[channel].lfe)
  575         {
  576                     coderInfo[channel].sfbn = 3;
  577         }
  578     }
  579 
  580     AACstereo(coderInfo, channelInfo, hEncoder->freqBuff, numChannels,
  581               (double)hEncoder->aacquantCfg.quality/DEFQUAL, jointmode);
  582 
  583 #ifdef DRM
  584     /* loop the quantization until the desired bit-rate is reached */
  585     diff = 1; /* to enter while loop */
  586     hEncoder->aacquantCfg.quality = 120; /* init quality setting */
  587     while (diff > 0) { /* if too many bits, do it again */
  588 #endif
  589     for (channel = 0; channel < numChannels; channel++) {
  590         BlocQuant(&coderInfo[channel], hEncoder->freqBuff[channel],
  591                   &(hEncoder->aacquantCfg));
  592     }
  593 
  594 #ifdef DRM
  595     /* Write the AAC bitstream */
  596     bitStream = OpenBitStream(bufferSize, outputBuffer);
  597     WriteBitstream(hEncoder, coderInfo, channelInfo, bitStream, numChannels);
  598 
  599     /* Close the bitstream and return the number of bytes written */
  600     frameBytes = CloseBitStream(bitStream);
  601 
  602     /* now calculate desired bits and compare with actual encoded bits */
  603     desbits = (int) ((double) numChannels * (hEncoder->config.bitRate * FRAME_LEN)
  604             / hEncoder->sampleRate);
  605 
  606     diff = ((frameBytes - 1 /* CRC */) * 8) - desbits;
  607 
  608     /* do linear correction according to relative difference */
  609     fix = (double) desbits / ((frameBytes - 1 /* CRC */) * 8);
  610 
  611     /* speed up convergence. A value of 0.92 gives approx up to 10 iterations */
  612     if (fix > 0.92)
  613         fix = 0.92;
  614 
  615     hEncoder->aacquantCfg.quality *= fix;
  616 
  617     /* quality should not go lower than 1, set diff to exit loop */
  618     if (hEncoder->aacquantCfg.quality <= 1)
  619         diff = -1;
  620     }
  621 #endif
  622 
  623     // fix max_sfb in CPE mode
  624     for (channel = 0; channel < numChannels; channel++)
  625     {
  626         if (channelInfo[channel].present
  627                 && (channelInfo[channel].cpe)
  628                 && (channelInfo[channel].ch_is_left))
  629         {
  630             CoderInfo *cil, *cir;
  631 
  632             cil = &coderInfo[channel];
  633             cir = &coderInfo[channelInfo[channel].paired_ch];
  634 
  635                         cil->sfbn = cir->sfbn = max(cil->sfbn, cir->sfbn);
  636         }
  637     }
  638 #ifndef DRM
  639     /* Write the AAC bitstream */
  640     bitStream = OpenBitStream(bufferSize, outputBuffer);
  641 
  642     if (WriteBitstream(hEncoder, coderInfo, channelInfo, bitStream, numChannels) < 0)
  643         return -1;
  644 
  645     /* Close the bitstream and return the number of bytes written */
  646     frameBytes = CloseBitStream(bitStream);
  647 
  648     /* Adjust quality to get correct average bitrate */
  649     if (hEncoder->config.bitRate)
  650     {
  651         int desbits = numChannels * (hEncoder->config.bitRate * FRAME_LEN)
  652             / hEncoder->sampleRate;
  653         double fix = (double)desbits / (double)(frameBytes * 8);
  654 
  655         if (fix < 0.9)
  656             fix += 0.1;
  657         else if (fix > 1.1)
  658             fix -= 0.1;
  659         else
  660             fix = 1.0;
  661 
  662         fix = (fix - 1.0) * 0.5 + 1.0;
  663         // printf("q: %.1f(f:%.4f)\n", hEncoder->aacquantCfg.quality, fix);
  664 
  665         hEncoder->aacquantCfg.quality *= fix;
  666 
  667         if (hEncoder->aacquantCfg.quality > maxqual)
  668             hEncoder->aacquantCfg.quality = maxqual;
  669         if (hEncoder->aacquantCfg.quality < 10)
  670             hEncoder->aacquantCfg.quality = 10;
  671     }
  672 #endif
  673 
  674     return frameBytes;
  675 }
  676 
  677 
  678 #ifdef DRM
  679 /* Scalefactorband data table for 960 transform length */
  680 /* all parameters which are different from the 1024 transform length table are
  681    marked with an "x" */
  682 static SR_INFO srInfo[12+1] =
  683 {
  684     { 96000, 40/*x*/, 12,
  685         {
  686             4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  687             8, 8, 8, 8, 8, 12, 12, 12, 12, 12, 16, 16, 24, 28,
  688             36, 44, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 0/*x*/
  689         },{
  690             4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 28/*x*/
  691         }
  692     }, { 88200, 40/*x*/, 12,
  693         {
  694             4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  695             8, 8, 8, 8, 8, 12, 12, 12, 12, 12, 16, 16, 24, 28,
  696             36, 44, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 0/*x*/
  697         },{
  698             4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 28/*x*/
  699         }
  700     }, { 64000, 45/*x*/, 12,
  701         {
  702             4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  703             8, 8, 8, 8, 12, 12, 12, 16, 16, 16, 20, 24, 24, 28,
  704             36, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
  705             40, 40, 40, 16/*x*/, 0/*x*/
  706         },{
  707             4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 28/*x*/
  708         }
  709     }, { 48000, 49, 14,
  710         {
  711             4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  8,  8,  8,  8,  8,  8,  8,
  712             12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28, 32, 32, 32, 32, 32, 32,
  713             32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32/*x*/
  714         }, {
  715             4,  4,  4,  4,  4,  8,  8,  8, 12, 12, 12, 16, 16, 8/*x*/
  716         }
  717     }, { 44100, 49, 14,
  718         {
  719             4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  8,  8,  8,  8,  8,  8,  8,
  720             12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28, 32, 32, 32, 32, 32, 32,
  721             32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32/*x*/
  722         }, {
  723             4,  4,  4,  4,  4,  8,  8,  8, 12, 12, 12, 16, 16, 8/*x*/
  724         }
  725     }, { 32000, 49/*x*/, 14,
  726         {
  727             4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  8,  8,  8,  8,
  728             8,  8,  8,  12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28,
  729             28, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
  730             32, 32, 32, 32, 32, 32, 32, 0/*x*/, 0/*x*/
  731         },{
  732             4,  4,  4,  4,  4,  8,  8,  8,  12, 12, 12, 16, 16, 16
  733         }
  734     }, { 24000, 46/*x*/, 15,
  735         {
  736             4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  8,  8,  8,  8,  8,  8,  8,
  737             8,  8,  8,  12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28, 32,
  738             36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 0/*x*/
  739         }, {
  740             4,  4,  4,  4,  4,  4,  4,  8,  8,  8, 12, 12, 16, 16, 12/*x*/
  741         }
  742     }, { 22050, 46/*x*/, 15,
  743         {
  744             4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  8,  8,  8,  8,  8,  8,  8,
  745             8,  8,  8,  12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28, 32,
  746             36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 0/*x*/
  747         }, {
  748             4,  4,  4,  4,  4,  4,  4,  8,  8,  8, 12, 12, 16, 16, 12/*x*/
  749         }
  750     }, { 16000, 42/*x*/, 15,
  751         {
  752             8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 12, 12, 12,
  753             12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24,
  754             24, 28, 28, 32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 0/*x*/
  755         }, {
  756             4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 12/*x*/
  757         }
  758     }, { 12000, 42/*x*/, 15,
  759         {
  760             8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 12, 12, 12,
  761             12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24,
  762             24, 28, 28, 32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 0/*x*/
  763         }, {
  764             4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 12/*x*/
  765         }
  766     }, { 11025, 42/*x*/, 15,
  767         {
  768             8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 12, 12, 12,
  769             12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24,
  770             24, 28, 28, 32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 0/*x*/
  771         }, {
  772             4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 12/*x*/
  773         }
  774     }, { 8000, 40, 15,
  775         {
  776             12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 16,
  777             16, 16, 16, 16, 16, 16, 20, 20, 20, 20, 24, 24, 24, 28,
  778             28, 32, 36, 36, 40, 44, 48, 52, 56, 60, 64, 16/*x*/
  779         }, {
  780             4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 12, 16, 20, 12/*x*/
  781         }
  782     },
  783     { -1 }
  784 };
  785 #else
  786 /* Scalefactorband data table for 1024 transform length */
  787 static SR_INFO srInfo[12+1] =
  788 {
  789     { 96000, 41, 12,
  790         {
  791             4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  792             8, 8, 8, 8, 8, 12, 12, 12, 12, 12, 16, 16, 24, 28,
  793             36, 44, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
  794         },{
  795             4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36
  796         }
  797     }, { 88200, 41, 12,
  798         {
  799             4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  800             8, 8, 8, 8, 8, 12, 12, 12, 12, 12, 16, 16, 24, 28,
  801             36, 44, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
  802         },{
  803             4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36
  804         }
  805     }, { 64000, 47, 12,
  806         {
  807             4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  808             8, 8, 8, 8, 12, 12, 12, 16, 16, 16, 20, 24, 24, 28,
  809             36, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
  810             40, 40, 40, 40, 40
  811         },{
  812             4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 32
  813         }
  814     }, { 48000, 49, 14,
  815         {
  816             4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  8,  8,  8,  8,  8,  8,  8,
  817             12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28, 32, 32, 32, 32, 32, 32,
  818             32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 96
  819         }, {
  820             4,  4,  4,  4,  4,  8,  8,  8, 12, 12, 12, 16, 16, 16
  821         }
  822     }, { 44100, 49, 14,
  823         {
  824             4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  8,  8,  8,  8,  8,  8,  8,
  825             12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28, 32, 32, 32, 32, 32, 32,
  826             32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 96
  827         }, {
  828             4,  4,  4,  4,  4,  8,  8,  8, 12, 12, 12, 16, 16, 16
  829         }
  830     }, { 32000, 51, 14,
  831         {
  832             4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  8,  8,  8,  8,
  833             8,  8,  8,  12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28,
  834             28, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
  835             32, 32, 32, 32, 32, 32, 32, 32, 32
  836         },{
  837             4,  4,  4,  4,  4,  8,  8,  8,  12, 12, 12, 16, 16, 16
  838         }
  839     }, { 24000, 47, 15,
  840         {
  841             4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  8,  8,  8,  8,  8,  8,  8,
  842             8,  8,  8,  12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28, 32,
  843             36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 64
  844         }, {
  845             4,  4,  4,  4,  4,  4,  4,  8,  8,  8, 12, 12, 16, 16, 20
  846         }
  847     }, { 22050, 47, 15,
  848         {
  849             4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  8,  8,  8,  8,  8,  8,  8,
  850             8,  8,  8,  12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28, 32,
  851             36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 64
  852         }, {
  853             4,  4,  4,  4,  4,  4,  4,  8,  8,  8, 12, 12, 16, 16, 20
  854         }
  855     }, { 16000, 43, 15,
  856         {
  857             8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 12, 12, 12,
  858             12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24,
  859             24, 28, 28, 32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 64
  860         }, {
  861             4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 20
  862         }
  863     }, { 12000, 43, 15,
  864         {
  865             8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 12, 12, 12,
  866             12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24,
  867             24, 28, 28, 32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 64
  868         }, {
  869             4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 20
  870         }
  871     }, { 11025, 43, 15,
  872         {
  873             8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 12, 12, 12,
  874             12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24,
  875             24, 28, 28, 32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 64
  876         }, {
  877             4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 20
  878         }
  879     }, { 8000, 40, 15,
  880         {
  881             12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 16,
  882             16, 16, 16, 16, 16, 16, 20, 20, 20, 20, 24, 24, 24, 28,
  883             28, 32, 36, 36, 40, 44, 48, 52, 56, 60, 64, 80
  884         }, {
  885             4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 12, 16, 20, 20
  886         }
  887     },
  888     { -1 }
  889 };
  890 #endif