acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

aac_decoder.h (24969B)

      1 // based on helix aac decoder
      2 #pragma once
      3 //#pragma GCC optimize ("O3")
      4 //#pragma GCC diagnostic ignored "-Wnarrowing"
      5 
      6 #include "Arduino.h"
      7 
      8 #define AAC_ENABLE_MPEG4
      9 
     10 #if (defined CONFIG_IDF_TARGET_ESP32S3 && defined BOARD_HAS_PSRAM)
     11     #define AAC_ENABLE_SBR  // needs additional 60KB DRAM,
     12 #endif
     13 
     14 #define ASSERT(x) /* do nothing */
     15 
     16 #ifndef MAX
     17 #define MAX(a,b)    std::max(a,b)
     18 #endif
     19 
     20 #ifndef MIN
     21 #define MIN(a,b)    std::min(a,b)
     22 #endif
     23 
     24 
     25 /* AAC file format */
     26 enum {
     27     AAC_FF_Unknown = 0,        /* should be 0 on init */
     28     AAC_FF_ADTS    = 1,
     29     AAC_FF_ADIF    = 2,
     30     AAC_FF_RAW     =  3
     31 };
     32 
     33 /* syntactic element type */
     34 enum {
     35     AAC_ID_INVALID = -1,
     36     AAC_ID_SCE     =  0,
     37     AAC_ID_CPE     =  1,
     38     AAC_ID_CCE     =  2,
     39     AAC_ID_LFE     =  3,
     40     AAC_ID_DSE     =  4,
     41     AAC_ID_PCE     =  5,
     42     AAC_ID_FIL     =  6,
     43     AAC_ID_END     =  7
     44 };
     45 
     46 enum {
     47     ERR_AAC_NONE                          =   0,
     48     ERR_AAC_INDATA_UNDERFLOW              =  -1,
     49     ERR_AAC_NULL_POINTER                  =  -2,
     50     ERR_AAC_INVALID_ADTS_HEADER           =  -3,
     51     ERR_AAC_INVALID_ADIF_HEADER           =  -4,
     52     ERR_AAC_INVALID_FRAME                 =  -5,
     53     ERR_AAC_MPEG4_UNSUPPORTED             =  -6,
     54     ERR_AAC_CHANNEL_MAP                   =  -7,
     55     ERR_AAC_SYNTAX_ELEMENT                =  -8,
     56     ERR_AAC_DEQUANT                       =  -9,
     57     ERR_AAC_STEREO_PROCESS                = -10,
     58     ERR_AAC_PNS                           = -11,
     59     ERR_AAC_SHORT_BLOCK_DEINT             = -12,
     60     ERR_AAC_TNS                           = -13,
     61     ERR_AAC_IMDCT                         = -14,
     62     ERR_AAC_NCHANS_TOO_HIGH               = -15,
     63     ERR_AAC_SBR_INIT                      = -16,
     64     ERR_AAC_SBR_BITSTREAM                 = -17,
     65     ERR_AAC_SBR_DATA                      = -18,
     66     ERR_AAC_SBR_PCM_FORMAT                = -19,
     67     ERR_AAC_SBR_NCHANS_TOO_HIGH           = -20,
     68     ERR_AAC_SBR_SINGLERATE_UNSUPPORTED    = -21,
     69     ERR_AAC_RAWBLOCK_PARAMS               = -22,
     70     ERR_AAC_UNKNOWN                       = -9999
     71 };
     72 
     73 enum {
     74     SBR_GRID_FIXFIX = 0,
     75     SBR_GRID_FIXVAR = 1,
     76     SBR_GRID_VARFIX = 2,
     77     SBR_GRID_VARVAR = 3
     78 };
     79 
     80 enum {
     81     HuffTabSBR_tEnv15 =    0,
     82     HuffTabSBR_fEnv15 =    1,
     83     HuffTabSBR_tEnv15b =   2,
     84     HuffTabSBR_fEnv15b =   3,
     85     HuffTabSBR_tEnv30 =    4,
     86     HuffTabSBR_fEnv30 =    5,
     87     HuffTabSBR_tEnv30b =   6,
     88     HuffTabSBR_fEnv30b =   7,
     89     HuffTabSBR_tNoise30 =  8,
     90     HuffTabSBR_fNoise30 =  5,
     91     HuffTabSBR_tNoise30b = 9,
     92     HuffTabSBR_fNoise30b = 7
     93 };
     94 
     95 typedef struct _AACDecInfo_t {
     96     /* raw decoded data, before rounding to 16-bit PCM (for postprocessing such as SBR) */
     97     void  *rawSampleBuf[2];
     98     int   rawSampleBytes;
     99     int   rawSampleFBits;
    100     /* fill data (can be used for processing SBR or other extensions) */
    101     uint8_t *fillBuf;
    102     int   fillCount;
    103     int   fillExtType;
    104     int   prevBlockID;    /* block information */
    105     int   currBlockID;
    106     int   currInstTag;
    107     int   sbDeinterleaveReqd[2]; // [MAX_NCHANS_ELEM]
    108     int   adtsBlocksLeft;
    109     int   bitRate;    /* user-accessible info */
    110     int   nChans;
    111     int   sampRate;
    112     float compressionRatio;
    113     int   id;         /* 0: MPEG-4, 1: MPEG2 */
    114     int   profile;    /* 0: Main profile, 1: LowComplexity (LC), 2: ScalableSamplingRate (SSR), 3: reserved */
    115     int   format;
    116     int   sbrEnabled;
    117     int   tnsUsed;
    118     int   pnsUsed;
    119     int   frameCount;
    120 } AACDecInfo_t;
    121 
    122 
    123 typedef struct _aac_BitStreamInfo_t {
    124     uint8_t *bytePtr;
    125     unsigned int iCache;
    126     int cachedBits;
    127     int nBytes;
    128 } aac_BitStreamInfo_t;
    129 
    130 typedef union _U64 {
    131     int64_t w64;
    132     struct {
    133         unsigned int lo32;
    134         signed int  hi32;
    135     } r;
    136 } U64;
    137 
    138 typedef struct _AACFrameInfo_t {
    139     int bitRate;
    140     int nChans;
    141     int sampRateCore;
    142     int sampRateOut;
    143     int bitsPerSample;
    144     int outputSamps;
    145     int profile;
    146     int tnsUsed;
    147     int pnsUsed;
    148 } AACFrameInfo_t;
    149 
    150 typedef struct _HuffInfo_t {
    151     int maxBits;              /* number of bits in longest codeword */
    152     uint8_t count[20];        /*  count[MAX_HUFF_BITS] = number of codes with length i+1 bits */
    153     int offset;               /* offset into symbol table */
    154 } HuffInfo_t;
    155 
    156 typedef struct _PulseInfo_t {
    157     uint8_t pulseDataPresent;
    158     uint8_t numPulse;
    159     uint8_t startSFB;
    160     uint8_t offset[4]; // [MAX_PULSES]
    161     uint8_t amp[4];    // [MAX_PULSES]
    162 } PulseInfo_t;
    163 
    164 typedef struct _TNSInfo_t {
    165     uint8_t tnsDataPresent;
    166     uint8_t numFilt[8]; // [MAX_TNS_FILTERS] max 1 filter each for 8 short windows, or 3 filters for 1 long window
    167     uint8_t coefRes[8]; // [MAX_TNS_FILTERS]
    168     uint8_t length[8];  // [MAX_TNS_FILTERS]
    169     uint8_t order[8];   // [MAX_TNS_FILTERS]
    170     uint8_t dir[8];     // [MAX_TNS_FILTERS]
    171     int8_t   coef[60];        // [MAX_TNS_COEFS] max 3 filters * 20 coefs for 1 long window,
    172                               //  or 1 filter * 7 coefs for each of 8 short windows
    173 } TNSInfo_t;
    174 
    175 typedef struct _GainControlInfo_t {
    176     uint8_t gainControlDataPresent;
    177     uint8_t maxBand;
    178     uint8_t adjNum[3][8];      // [MAX_GAIN_BANDS][MAX_GAIN_WIN]
    179     uint8_t alevCode[3][8][7]; // [MAX_GAIN_BANDS][MAX_GAIN_WIN][MAX_GAIN_ADJUST]
    180     uint8_t alocCode[3][8][7]; // [MAX_GAIN_BANDS][MAX_GAIN_WIN][MAX_GAIN_ADJUST]
    181 } GainControlInfo_t;
    182 
    183 typedef struct _ICSInfo_t {
    184     uint8_t icsResBit;
    185     uint8_t winSequence;
    186     uint8_t winShape;
    187     uint8_t maxSFB;
    188     uint8_t sfGroup;
    189     uint8_t predictorDataPresent;
    190     uint8_t predictorReset;
    191     uint8_t predictorResetGroupNum;
    192     uint8_t predictionUsed[41]; // [MAX_PRED_SFB]
    193     uint8_t numWinGroup;
    194     uint8_t winGroupLen[8];     // [MAX_WIN_GROUPS]
    195 } ICSInfo_t;
    196 
    197 typedef struct _ADTSHeader_t {
    198     /* fixed */
    199     uint8_t id;                         /* MPEG bit - should be 1 */
    200     uint8_t layer;                      /* MPEG layer - should be 0 */
    201     uint8_t protectBit;                 /* 0 = CRC word follows, 1 = no CRC word */
    202     uint8_t profile;                    /* 0 = main, 1 = LC, 2 = SSR, 3 = reserved */
    203     uint8_t sampRateIdx;                /* sample rate index range = [0, 11] */
    204     uint8_t privateBit;                 /* ignore */
    205     uint8_t channelConfig;              /* 0 = implicit, >0 = use default table */
    206     uint8_t origCopy;                   /* 0 = copy, 1 = original */
    207     uint8_t home;                       /* ignore */
    208     /* variable */
    209     uint8_t copyBit;                    /* 1 bit of the 72-bit copyright ID (transmitted as 1 bit per frame) */
    210     uint8_t copyStart;                  /* 1 = this bit starts the 72-bit ID, 0 = it does not */
    211     int     frameLength;                /* length of frame */
    212     int     bufferFull;                 /* number of 32-bit words left in enc buffer, 0x7FF = VBR */
    213     uint8_t numRawDataBlocks;           /* number of raw data blocks in frame */
    214     /* CRC */
    215     int     crcCheckWord;                     /* 16-bit CRC check word (present if protectBit == 0) */
    216 } ADTSHeader_t;
    217 
    218 typedef struct _ADIFHeader_t {
    219     uint8_t  copyBit;                    /* 0 = no copyright ID, 1 = 72-bit copyright ID follows immediately */
    220     uint8_t  origCopy;                   /* 0 = copy, 1 = original */
    221     uint8_t  home;                       /* ignore */
    222     uint8_t  bsType;                     /* bitstream type: 0 = CBR, 1 = VBR */
    223     int      bitRate;                    /* bitRate: CBR = bits/sec, VBR = peak bits/frame, 0 = unknown */
    224     uint8_t  numPCE;                     /* number of program config elements (max = 16) */
    225     int      bufferFull;                 /* bits left in bit reservoir */
    226     uint8_t  copyID[9];                  /* [ADIF_COPYID_SIZE] optional 72-bit copyright ID */
    227 } ADIFHeader_t;
    228 
    229 /* sizeof(ProgConfigElement_t) = 82 bytes (if KEEP_PCE_COMMENTS not defined) */
    230 typedef struct _ProgConfigElement_t {
    231     uint8_t  elemInstTag;   /* element instance tag */
    232     uint8_t  profile;       /* 0 = main, 1 = LC, 2 = SSR, 3 = reserved */
    233     uint8_t  sampRateIdx;   /* sample rate index range = [0, 11] */
    234     uint8_t  numFCE;        /* number of front channel elements (max = 15) */
    235     uint8_t  numSCE;        /* number of side channel elements (max = 15) */
    236     uint8_t  numBCE;        /* number of back channel elements (max = 15) */
    237     uint8_t  numLCE;        /* number of LFE channel elements (max = 3) */
    238     uint8_t  numADE;        /* number of associated data elements (max = 7) */
    239     uint8_t  numCCE;        /* number of valid channel coupling elements (max = 15) */
    240     uint8_t  monoMixdown;   /* mono mixdown: bit 4 = present flag, bits 3-0 = element number */
    241     uint8_t  stereoMixdown; /* stereo mixdown: bit 4 = present flag, bits 3-0 = element number */
    242     uint8_t  matrixMixdown; /* bit 4 = present flag, bit 3 = unused,bits 2-1 = index, bit 0 = pseudo-surround enable */
    243     uint8_t  fce[15];       /* [MAX_NUM_FCE] front element channel pair: bit 4 = SCE/CPE flag, bits 3-0 = inst tag */
    244     uint8_t  sce[15];       /* [MAX_NUM_SCE] side element channel pair: bit 4 = SCE/CPE flag, bits 3-0 = inst tag */
    245     uint8_t  bce[15];       /* [MAX_NUM_BCE] back element channel pair: bit 4 = SCE/CPE flag, bits 3-0 = inst tag */
    246     uint8_t  lce[3];        /* [MAX_NUM_LCE] instance tag for LFE elements */
    247     uint8_t  ade[7];        /* [MAX_NUM_ADE] instance tag for ADE elements */
    248     uint8_t  cce[15];       /* [MAX_NUM_BCE] channel coupling elements: bit 4 = switching flag, bits 3-0 = inst tag */
    249 } ProgConfigElement_t;
    250 
    251 typedef struct _SBRHeader {
    252     int      count;
    253 
    254     uint8_t  ampRes;
    255     uint8_t  startFreq;
    256     uint8_t  stopFreq;
    257     uint8_t  crossOverBand;
    258     uint8_t  resBitsHdr;
    259     uint8_t  hdrExtra1;
    260     uint8_t  hdrExtra2;
    261 
    262     uint8_t  freqScale;
    263     uint8_t  alterScale;
    264     uint8_t  noiseBands;
    265 
    266     uint8_t  limiterBands;
    267     uint8_t  limiterGains;
    268     uint8_t  interpFreq;
    269     uint8_t  smoothMode;
    270 } SBRHeader;
    271 
    272 /* need one SBRGrid per channel, updated every frame */
    273 typedef struct _SBRGrid {
    274     uint8_t  frameClass;
    275     uint8_t  ampResFrame;
    276     uint8_t  pointer;
    277 
    278     uint8_t  numEnv;                       /* L_E */
    279     uint8_t  envTimeBorder[5 + 1];   // [MAX_NUM_ENV+1] /* t_E */
    280     uint8_t  freqRes[5];             // [MAX_NUM_ENV]/* r */
    281     uint8_t  numNoiseFloors;                           /* L_Q */
    282     uint8_t  noiseTimeBorder[2 + 1]; // [MAX_NUM_NOISE_FLOORS+1] /* t_Q */
    283 
    284     uint8_t  numEnvPrev;
    285     uint8_t  numNoiseFloorsPrev;
    286     uint8_t  freqResPrev;
    287 } SBRGrid;
    288 
    289 /* need one SBRFreq per element (SCE/CPE/LFE), updated only on header reset */
    290 typedef struct _SBRFreq {
    291     int      kStart;               /* k_x */
    292     int      nMaster;
    293     int      nHigh;
    294     int      nLow;
    295     int      nLimiter;             /* N_l */
    296     int      numQMFBands;          /* M */
    297     int      numNoiseFloorBands;   /* Nq */
    298     int      kStartPrev;
    299     int      numQMFBandsPrev;
    300     uint8_t  freqMaster[48 + 1];     // [MAX_QMF_BANDS + 1]      /* not necessary to save this  after derived tables are generated */
    301     uint8_t  freqHigh[48 + 1];       // [MAX_QMF_BANDS + 1]
    302     uint8_t  freqLow[48 / 2 + 1];    // [MAX_QMF_BANDS / 2 + 1]  /* nLow = nHigh - (nHigh >> 1) */
    303     uint8_t  freqNoise[5 + 1];       // [MAX_NUM_NOISE_FLOOR_BANDS+1]
    304     uint8_t  freqLimiter[48 / 2 + 5];// [MAX_QMF_BANDS / 2 + MAX_NUM_PATCHES]    /* max (intermediate) size = nLow + numPatches - 1 */
    305 
    306     uint8_t  numPatches;
    307     uint8_t  patchNumSubbands[5 + 1];  // [MAX_NUM_PATCHES + 1]
    308     uint8_t  patchStartSubband[5 + 1]; // [MAX_NUM_PATCHES + 1]
    309 } SBRFreq;
    310 
    311 typedef struct _SBRChan {
    312     int      reset;
    313     uint8_t  deltaFlagEnv[5];          // [MAX_NUM_ENV]
    314     uint8_t  deltaFlagNoise[2];        // [MAX_NUM_NOISE_FLOORS]
    315     int8_t   envDataQuant[5][48];      // [MAX_NUM_ENV][MAX_QMF_BANDS] /* range = [0, 127] */
    316     int8_t   noiseDataQuant[2][5];     // [MAX_NUM_NOISE_FLOORS][MAX_NUM_NOISE_FLOOR_BANDS]
    317 
    318     uint8_t  invfMode[2][5];           // [2][MAX_NUM_NOISE_FLOOR_BANDS] /* invfMode[0/1][band] = prev/curr */
    319     int      chirpFact[5];             // [MAX_NUM_NOISE_FLOOR_BANDS]  /* bwArray */
    320     uint8_t  addHarmonicFlag[2];       /* addHarmonicFlag[0/1] = prev/curr */
    321     uint8_t  addHarmonic[2][64];       /* addHarmonic[0/1][band] = prev/curr */
    322 
    323     int      gbMask[2];    /* gbMask[0/1] = XBuf[0-31]/XBuf[32-39] */
    324     int8_t   laPrev;
    325 
    326     int      noiseTabIndex;
    327     int      sinIndex;
    328     int      gainNoiseIndex;
    329     int      gTemp[5][48];  // [MAX_NUM_SMOOTH_COEFS][MAX_QMF_BANDS]
    330     int      qTemp[5][48];  // [MAX_NUM_SMOOTH_COEFS][MAX_QMF_BANDS]
    331 
    332 } SBRChan;
    333 
    334 
    335 /* state info struct for baseline (MPEG-4 LC) decoding */
    336 typedef struct _PSInfoBase_t {
    337     int      dataCount;
    338     uint8_t  dataBuf[510]; // [DATA_BUF_SIZE]
    339     int      fillCount;
    340     uint8_t  fillBuf[269]; //[FILL_BUF_SIZE]
    341     /* state information which is the same throughout whole frame */
    342     int      nChans;
    343     int      useImpChanMap;
    344     int      sampRateIdx;
    345     /* state information which can be overwritten by subsequent elements within frame */
    346     ICSInfo_t  icsInfo[2]; // [MAX_NCHANS_ELEM]
    347     int      commonWin;
    348     short    scaleFactors[2][15*8]; // [MAX_NCHANS_ELEM][MAX_SF_BANDS]
    349     uint8_t  sfbCodeBook[2][15*8]; // [MAX_NCHANS_ELEM][MAX_SF_BANDS]
    350     int      msMaskPresent;
    351     uint8_t  msMaskBits[(15 * 8 + 7) >> 3]; // [MAX_MS_MASK_BYTES]
    352     int      pnsUsed[2]; // [MAX_NCHANS_ELEM]
    353     int      pnsLastVal;
    354     int      intensityUsed[2]; // [MAX_NCHANS_ELEM]
    355 //    PulseInfo_t           pulseInfo[2]; // [MAX_NCHANS_ELEM]
    356     TNSInfo_t   tnsInfo[2]; // [MAX_NCHANS_ELEM]
    357     int      tnsLPCBuf[20]; // [MAX_TNS_ORDER]
    358     int      tnsWorkBuf[20]; //[MAX_TNS_ORDER]
    359     GainControlInfo_t     gainControlInfo[2]; // [MAX_NCHANS_ELEM]
    360     int      gbCurrent[2];  // [MAX_NCHANS_ELEM]
    361     int      coef[2][1024]; // [MAX_NCHANS_ELEM][AAC_MAX_NSAMPS]
    362 #ifdef AAC_ENABLE_SBR
    363     int      sbrWorkBuf[2][1024]; // [MAX_NCHANS_ELEM][AAC_MAX_NSAMPS];
    364 #endif
    365     /* state information which must be saved for each element and used in next frame */
    366     int      overlap[2][1024];  // [AAC_MAX_NCHANS][AAC_MAX_NSAMPS]
    367     int      prevWinShape[2]; // [AAC_MAX_NCHANS]
    368 } PSInfoBase_t;
    369 
    370 typedef struct _PSInfoSBR {
    371     /* save for entire file */
    372     int      frameCount;
    373     int      sampRateIdx;
    374 
    375     /* state info that must be saved for each channel */
    376     SBRHeader   sbrHdr[2];
    377     SBRGrid     sbrGrid[2];
    378     SBRFreq     sbrFreq[2];
    379     SBRChan     sbrChan[2];
    380 
    381     /* temp variables, no need to save between blocks */
    382     uint8_t  dataExtra;
    383     uint8_t  resBitsData;
    384     uint8_t  extendedDataPresent;
    385     int      extendedDataSize;
    386 
    387     int8_t   envDataDequantScale[2][5];  // [MAX_NCHANS_ELEM][MAX_NUM_ENV
    388     int      envDataDequant[2][5][48];   // [MAX_NCHANS_ELEM][MAX_NUM_ENV][MAX_QMF_BANDS
    389     int      noiseDataDequant[2][2][5];  // [MAX_NCHANS_ELEM][MAX_NUM_NOISE_FLOORS][MAX_NUM_NOISE_FLOOR_BANDS]
    390 
    391     int      eCurr[48];    // [MAX_QMF_BANDS]
    392     uint8_t  eCurrExp[48]; // [MAX_QMF_BANDS]
    393     uint8_t  eCurrExpMax;
    394     int8_t   la;
    395 
    396     int      crcCheckWord;
    397     int      couplingFlag;
    398     int      envBand;
    399     int      eOMGainMax;
    400     int      gainMax;
    401     int      gainMaxFBits;
    402     int      noiseFloorBand;
    403     int      qp1Inv;
    404     int      qqp1Inv;
    405     int      sMapped;
    406     int      sBand;
    407     int      highBand;
    408 
    409     int      sumEOrigMapped;
    410     int      sumECurrGLim;
    411     int      sumSM;
    412     int      sumQM;
    413     int      gLimBoost[48];
    414     int      qmLimBoost[48];
    415     int      smBoost[48];
    416 
    417     int      smBuf[48];
    418     int      qmLimBuf[48];
    419     int      gLimBuf[48];
    420     int      gLimFbits[48];
    421 
    422     int      gFiltLast[48];
    423     int      qFiltLast[48];
    424 
    425     /* large buffers */
    426     int      delayIdxQMFA[2];        // [AAC_MAX_NCHANS]
    427     int      delayQMFA[2][10 * 32];  // [AAC_MAX_NCHANS][DELAY_SAMPS_QMFA]
    428     int      delayIdxQMFS[2];        // [AAC_MAX_NCHANS]
    429     int      delayQMFS[2][10 * 128]; // [AAC_MAX_NCHANS][DELAY_SAMPS_QMFS]
    430     int      XBufDelay[2][8][64][2]; // [AAC_MAX_NCHANS][HF_GEN][64][2]
    431     int      XBuf[32+8][64][2];
    432 } PSInfoSBR_t;
    433 
    434 bool AACDecoder_AllocateBuffers(void);
    435 int AACFlushCodec();
    436 void AACDecoder_FreeBuffers(void);
    437 bool AACDecoder_IsInit(void);
    438 int AACFindSyncWord(uint8_t *buf, int nBytes);
    439 int AACSetRawBlockParams(int copyLast, int nChans, int sampRateCore, int profile);
    440 int AACDecode(uint8_t *inbuf, int *bytesLeft, short *outbuf);
    441 int AACGetSampRate();
    442 int AACGetChannels();
    443 int AACGetID(); // 0-MPEG4, 1-MPEG2
    444 uint8_t AACGetProfile(); // 0-Main, 1-LC, 2-SSR, 3-reserved
    445 uint8_t AACGetFormat(); // 0-unknown 1-ADTS 2-ADIF, 3-RAW
    446 int AACGetBitsPerSample();
    447 int AACGetBitrate();
    448 int AACGetOutputSamps();
    449 int AACGetBitrate();
    450 void DecodeLPCCoefs(int order, int res, int8_t *filtCoef, int *a, int *b);
    451 int FilterRegion(int size, int dir, int order, int *audioCoef, int *a, int *hist);
    452 int TNSFilter(int ch);
    453 int DecodeSingleChannelElement();
    454 int DecodeChannelPairElement();
    455 int DecodeLFEChannelElement();
    456 int DecodeDataStreamElement();
    457 int DecodeProgramConfigElement(uint8_t idx);
    458 int DecodeFillElement();
    459 int DecodeNextElement(uint8_t **buf, int *bitOffset, int *bitsAvail);
    460 void PreMultiply(int tabidx, int *zbuf1);
    461 void PostMultiply(int tabidx, int *fft1);
    462 void PreMultiplyRescale(int tabidx, int *zbuf1, int es);
    463 void PostMultiplyRescale(int tabidx, int *fft1, int es);
    464 void DCT4(int tabidx, int *coef, int gb);
    465 void BitReverse(int *inout, int tabidx);
    466 void R4FirstPass(int *x, int bg);
    467 void R8FirstPass(int *x, int bg);
    468 void R4Core(int *x, int bg, int gp, int *wtab);
    469 void R4FFT(int tabidx, int *x);
    470 void UnpackZeros(int nVals, int *coef);
    471 void UnpackQuads(int cb, int nVals, int *coef);
    472 void UnpackPairsNoEsc(int cb, int nVals, int *coef);
    473 void UnpackPairsEsc(int cb, int nVals, int *coef);
    474 void DecodeSpectrumLong(int ch);
    475 void DecodeSpectrumShort(int ch);
    476 void DecWindowOverlap(int *buf0, int *over0, short *pcm0, int nChans, int winTypeCurr, int winTypePrev);
    477 void DecWindowOverlapLongStart(int *buf0, int *over0, short *pcm0, int nChans, int winTypeCurr, int winTypePrev);
    478 void DecWindowOverlapLongStop(int *buf0, int *over0, short *pcm0, int nChans, int winTypeCurr, int winTypePrev);
    479 void DecWindowOverlapShort(int *buf0, int *over0, short *pcm0, int nChans, int winTypeCurr, int winTypePrev);
    480 int IMDCT(int ch, int chOut, short *outbuf);
    481 void DecodeICSInfo(ICSInfo_t *icsInfo, int sampRateIdx);
    482 void DecodeSectionData(int winSequence, int numWinGrp, int maxSFB, uint8_t *sfbCodeBook);
    483 int DecodeOneScaleFactor();
    484 void DecodeScaleFactors(int numWinGrp, int maxSFB, int globalGain, uint8_t *sfbCodeBook, short *scaleFactors);
    485 void DecodePulseInfo(uint8_t ch);
    486 void DecodeTNSInfo(int winSequence, TNSInfo_t *ti, int8_t *tnsCoef);
    487 void DecodeGainControlInfo(int winSequence, GainControlInfo_t *gi);
    488 void DecodeICS(int ch);
    489 int DecodeNoiselessData(uint8_t **buf, int *bitOffset, int *bitsAvail, int ch);
    490 int DecodeHuffmanScalar(const signed short *huffTab, const HuffInfo_t *huffTabInfo, unsigned int bitBuf, int32_t *val);
    491 int UnpackADTSHeader(uint8_t **buf, int *bitOffset, int *bitsAvail);
    492 int GetADTSChannelMapping(uint8_t *buf, int bitOffset, int bitsAvail);
    493 int GetNumChannelsADIF(int nPCE);
    494 int GetSampleRateIdxADIF(int nPCE);
    495 int UnpackADIFHeader(uint8_t **buf, int *bitOffset, int *bitsAvail);
    496 int SetRawBlockParams(int copyLast, int nChans, int sampRate, int profile);
    497 int PrepareRawBlock();
    498 int DequantBlock(int *inbuf, int nSamps, int scale);
    499 int AACDequantize(int ch);
    500 int DeinterleaveShortBlocks(int ch);
    501 unsigned int Get32BitVal(unsigned int *last);
    502 int InvRootR(int r);
    503 int ScaleNoiseVector(int *coef, int nVals, int sf);
    504 void GenerateNoiseVector(int *coef, int *last, int nVals);
    505 void CopyNoiseVector(int *coefL, int *coefR, int nVals);
    506 int PNS(int ch);
    507 int GetSampRateIdx(int sampRate);
    508 void StereoProcessGroup(int *coefL, int *coefR, const uint16_t *sfbTab, int msMaskPres, uint8_t *msMaskPtr,
    509         int msMaskOffset, int maxSFB, uint8_t *cbRight, short *sfRight, int *gbCurrent);
    510 int StereoProcess();
    511 int RatioPowInv(int a, int b, int c);
    512 int SqrtFix(int q, int fBitsIn, int *fBitsOut);
    513 int InvRNormalized(int r);
    514 void BitReverse32(int *inout);
    515 void R8FirstPass32(int *r0);
    516 void R4Core32(int *r0);
    517 void FFT32C(int *x);
    518 void CVKernel1(int *XBuf, int *accBuf);
    519 void CVKernel2(int *XBuf, int *accBuf);
    520 void SetBitstreamPointer(int nBytes, uint8_t *buf);
    521 inline void RefillBitstreamCache();
    522 unsigned int GetBits(int nBits);
    523 unsigned int GetBitsNoAdvance(int nBits);
    524 void AdvanceBitstream(int nBits);
    525 int CalcBitsUsed(uint8_t *startBuf, int startOffset);
    526 void ByteAlignBitstream();
    527 // SBR
    528 void InitSBRState();
    529 int DecodeSBRBitstream(int chBase);
    530 int DecodeSBRData(int chBase, short *outbuf);
    531 int FlushCodecSBR();
    532 void BubbleSort(uint8_t *v, int nItems);
    533 uint8_t VMin(uint8_t *v, int nItems);
    534 uint8_t VMax(uint8_t *v, int nItems);
    535 int CalcFreqMasterScaleZero(uint8_t *freqMaster, int alterScale, int k0, int k2);
    536 int CalcFreqMaster(uint8_t *freqMaster, int freqScale, int alterScale, int k0, int k2);
    537 int CalcFreqHigh(uint8_t *freqHigh, uint8_t *freqMaster, int nMaster, int crossOverBand);
    538 int CalcFreqLow(uint8_t *freqLow, uint8_t *freqHigh, int nHigh);
    539 int CalcFreqNoise(uint8_t *freqNoise, uint8_t *freqLow, int nLow, int kStart, int k2, int noiseBands);
    540 int BuildPatches(uint8_t *patchNumSubbands, uint8_t *patchStartSubband, uint8_t *freqMaster, int nMaster, int k0,
    541         int kStart, int numQMFBands, int sampRateIdx);
    542 int FindFreq(uint8_t *freq, int nFreq, uint8_t val);
    543 void RemoveFreq(uint8_t *freq, int nFreq, int removeIdx);
    544 int CalcFreqLimiter(uint8_t *freqLimiter, uint8_t *patchNumSubbands, uint8_t *freqLow, int nLow, int kStart,
    545         int limiterBands, int numPatches);
    546 int CalcFreqTables(SBRHeader *sbrHdr, SBRFreq *sbrFreq, int sampRateIdx);
    547 void EstimateEnvelope(SBRHeader *sbrHdr, SBRGrid *sbrGrid, SBRFreq *sbrFreq, int env);
    548 int GetSMapped(SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChan, int env, int band, int la);
    549 void CalcMaxGain(SBRHeader *sbrHdr, SBRGrid *sbrGrid, SBRFreq *sbrFreq, int ch, int env, int lim, int fbitsDQ);
    550 void CalcNoiseDivFactors(int q, int *qp1Inv, int *qqp1Inv);
    551 void CalcComponentGains(SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChan, int ch, int env, int lim, int fbitsDQ);
    552 void ApplyBoost(SBRFreq *sbrFreq, int lim, int fbitsDQ);
    553 void CalcGain(SBRHeader *sbrHdr, SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChan, int ch, int env);
    554 void MapHF(SBRHeader *sbrHdr, SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChan, int env, int hfReset);
    555 void AdjustHighFreq(SBRHeader *sbrHdr, SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChan, int ch);
    556 int CalcCovariance1(int *XBuf, int *p01reN, int *p01imN, int *p12reN, int *p12imN, int *p11reN, int *p22reN);
    557 int CalcCovariance2(int *XBuf, int *p02reN, int *p02imN);
    558 void CalcLPCoefs(int *XBuf, int *a0re, int *a0im, int *a1re, int *a1im, int gb);
    559 void GenerateHighFreq(SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChan, int ch);
    560 int DecodeHuffmanScalar(const signed int *huffTab, const HuffInfo_t *huffTabInfo, unsigned int bitBuf, signed int *val);
    561 int DecodeOneSymbol(int huffTabIndex);
    562 int DequantizeEnvelope(int nBands, int ampRes, int8_t *envQuant, int *envDequant);
    563 void DequantizeNoise(int nBands, int8_t *noiseQuant, int *noiseDequant);
    564 void DecodeSBREnvelope(SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChan, int ch);
    565 void DecodeSBRNoise(SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChan, int ch);
    566 void UncoupleSBREnvelope(SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChanR);
    567 void UncoupleSBRNoise(SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChanR);
    568 void DecWindowOverlapNoClip(int *buf0, int *over0, int *out0, int winTypeCurr, int winTypePrev);
    569 void DecWindowOverlapLongStartNoClip(int *buf0, int *over0, int *out0, int winTypeCurr, int winTypePrev);
    570 void DecWindowOverlapLongStopNoClip(int *buf0, int *over0, int *out0, int winTypeCurr, int winTypePrev);
    571 void DecWindowOverlapShortNoClip(int *buf0, int *over0, int *out0, int winTypeCurr, int winTypePrev);
    572 void PreMultiply64(int *zbuf1);
    573 void PostMultiply64(int *fft1, int nSampsOut);
    574 void QMFAnalysisConv(int *cTab, int *delay, int dIdx, int *uBuf);
    575 int QMFAnalysis(int *inbuf, int *delay, int *XBuf, int fBitsIn, int *delayIdx, int qmfaBands);
    576 void QMFSynthesisConv(int *cPtr, int *delay, int dIdx, short *outbuf, int nChans);
    577 void QMFSynthesis(int *inbuf, int *delay, int *delayIdx, int qmfsBands, short *outbuf, int nChans);
    578 int UnpackSBRHeader(SBRHeader *sbrHdr);
    579 void UnpackSBRGrid(SBRHeader *sbrHdr, SBRGrid *sbrGrid);
    580 void UnpackDeltaTimeFreq(int numEnv, uint8_t *deltaFlagEnv, int numNoiseFloors, uint8_t *deltaFlagNoise);
    581 void UnpackInverseFilterMode(int numNoiseFloorBands, uint8_t *mode);
    582 void UnpackSinusoids(int nHigh, int addHarmonicFlag, uint8_t *addHarmonic);
    583 void CopyCouplingGrid(SBRGrid *sbrGridLeft, SBRGrid *sbrGridRight);
    584 void CopyCouplingInverseFilterMode(int numNoiseFloorBands, uint8_t *modeLeft, uint8_t *modeRight);
    585 void UnpackSBRSingleChannel(int chBase);
    586 void UnpackSBRChannelPair(int chBase);