123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
-
- #include "coder.h"
-
- int UnpackADTSHeader(AACDecInfo *aacDecInfo, unsigned char **buf, int *bitOffset, int *bitsAvail)
- {
- int bitsUsed;
- PSInfoBase *psi;
- BitStreamInfo bsi;
- ADTSHeader *fhADTS;
-
- if (!aacDecInfo || !aacDecInfo->psInfoBase)
- return ERR_AAC_NULL_POINTER;
- psi = (PSInfoBase *)(aacDecInfo->psInfoBase);
- fhADTS = &(psi->fhADTS);
-
- SetBitstreamPointer(&bsi, (*bitsAvail + 7) >> 3, *buf);
- GetBits(&bsi, *bitOffset);
-
- if (GetBits(&bsi, 12) != 0x0fff) {
- return ERR_AAC_INVALID_ADTS_HEADER;
- }
-
- fhADTS->id = GetBits(&bsi, 1);
- fhADTS->layer = GetBits(&bsi, 2);
- fhADTS->protectBit = GetBits(&bsi, 1);
- fhADTS->profile = GetBits(&bsi, 2);
- fhADTS->sampRateIdx = GetBits(&bsi, 4);
- fhADTS->privateBit = GetBits(&bsi, 1);
- fhADTS->channelConfig = GetBits(&bsi, 3);
- fhADTS->origCopy = GetBits(&bsi, 1);
- fhADTS->home = GetBits(&bsi, 1);
-
- fhADTS->copyBit = GetBits(&bsi, 1);
- fhADTS->copyStart = GetBits(&bsi, 1);
- fhADTS->frameLength = GetBits(&bsi, 13);
- fhADTS->bufferFull = GetBits(&bsi, 11);
- fhADTS->numRawDataBlocks = GetBits(&bsi, 2) + 1;
-
- if (fhADTS->protectBit == 0)
- fhADTS->crcCheckWord = GetBits(&bsi, 16);
-
- ByteAlignBitstream(&bsi);
-
- if (fhADTS->layer != 0 || fhADTS->profile != AAC_PROFILE_LC ||
- fhADTS->sampRateIdx >= NUM_SAMPLE_RATES || fhADTS->channelConfig >= NUM_DEF_CHAN_MAPS)
- return ERR_AAC_INVALID_ADTS_HEADER;
- #ifndef AAC_ENABLE_MPEG4
- if (fhADTS->id != 1)
- return ERR_AAC_MPEG4_UNSUPPORTED;
- #endif
-
- psi->sampRateIdx = fhADTS->sampRateIdx;
- if (!psi->useImpChanMap)
- psi->nChans = channelMapTab[fhADTS->channelConfig];
-
- aacDecInfo->prevBlockID = AAC_ID_INVALID;
- aacDecInfo->currBlockID = AAC_ID_INVALID;
- aacDecInfo->currInstTag = -1;
-
- aacDecInfo->bitRate = 0;
- aacDecInfo->nChans = psi->nChans;
- aacDecInfo->sampRate = sampRateTab[psi->sampRateIdx];
- aacDecInfo->profile = fhADTS->profile;
- aacDecInfo->sbrEnabled = 0;
- aacDecInfo->adtsBlocksLeft = fhADTS->numRawDataBlocks;
-
- bitsUsed = CalcBitsUsed(&bsi, *buf, *bitOffset);
- *buf += (bitsUsed + *bitOffset) >> 3;
- *bitOffset = (bitsUsed + *bitOffset) & 0x07;
- *bitsAvail -= bitsUsed ;
- if (*bitsAvail < 0)
- return ERR_AAC_INDATA_UNDERFLOW;
- return ERR_AAC_NONE;
- }
- int GetADTSChannelMapping(AACDecInfo *aacDecInfo, unsigned char *buf, int bitOffset, int bitsAvail)
- {
- int ch, nChans, elementChans, err;
- PSInfoBase *psi;
-
- if (!aacDecInfo || !aacDecInfo->psInfoBase)
- return ERR_AAC_NULL_POINTER;
- psi = (PSInfoBase *)(aacDecInfo->psInfoBase);
- nChans = 0;
- do {
-
- err = DecodeNextElement(aacDecInfo, &buf, &bitOffset, &bitsAvail);
- if (err)
- return err;
- elementChans = elementNumChans[aacDecInfo->currBlockID];
- nChans += elementChans;
- for (ch = 0; ch < elementChans; ch++) {
- err = DecodeNoiselessData(aacDecInfo, &buf, &bitOffset, &bitsAvail, ch);
- if (err)
- return err;
- }
- } while (aacDecInfo->currBlockID != AAC_ID_END);
- if (nChans <= 0)
- return ERR_AAC_CHANNEL_MAP;
-
- psi->nChans = nChans;
- aacDecInfo->nChans = psi->nChans;
- psi->useImpChanMap = 1;
- return ERR_AAC_NONE;
- }
- static int GetNumChannelsADIF(ProgConfigElement *fhPCE, int nPCE)
- {
- int i, j, nChans;
- if (nPCE < 1 || nPCE > MAX_NUM_PCE_ADIF)
- return -1;
- nChans = 0;
- for (i = 0; i < nPCE; i++) {
-
- if (fhPCE[i].profile != AAC_PROFILE_LC || fhPCE[i].numCCE > 0)
- return -1;
-
- nChans += fhPCE[i].numFCE;
- nChans += fhPCE[i].numSCE;
- nChans += fhPCE[i].numBCE;
- nChans += fhPCE[i].numLCE;
-
- for (j = 0; j < fhPCE[i].numFCE; j++) {
- if (CHAN_ELEM_IS_CPE(fhPCE[i].fce[j]))
- nChans++;
- }
- for (j = 0; j < fhPCE[i].numSCE; j++) {
- if (CHAN_ELEM_IS_CPE(fhPCE[i].sce[j]))
- nChans++;
- }
- for (j = 0; j < fhPCE[i].numBCE; j++) {
- if (CHAN_ELEM_IS_CPE(fhPCE[i].bce[j]))
- nChans++;
- }
- }
- return nChans;
- }
- static int GetSampleRateIdxADIF(ProgConfigElement *fhPCE, int nPCE)
- {
- int i, idx;
- if (nPCE < 1 || nPCE > MAX_NUM_PCE_ADIF)
- return -1;
-
- idx = fhPCE[0].sampRateIdx;
- for (i = 1; i < nPCE; i++) {
- if (fhPCE[i].sampRateIdx != idx)
- return -1;
- }
- return idx;
- }
- int UnpackADIFHeader(AACDecInfo *aacDecInfo, unsigned char **buf, int *bitOffset, int *bitsAvail)
- {
- int i, bitsUsed;
- PSInfoBase *psi;
- BitStreamInfo bsi;
- ADIFHeader *fhADIF;
- ProgConfigElement *pce;
-
- if (!aacDecInfo || !aacDecInfo->psInfoBase)
- return ERR_AAC_NULL_POINTER;
- psi = (PSInfoBase *)(aacDecInfo->psInfoBase);
-
- SetBitstreamPointer(&bsi, (*bitsAvail + 7) >> 3, *buf);
- GetBits(&bsi, *bitOffset);
-
- fhADIF = &(psi->fhADIF);
- pce = psi->pce;
-
- if (GetBits(&bsi, 8) != 'A' || GetBits(&bsi, 8) != 'D' || GetBits(&bsi, 8) != 'I' || GetBits(&bsi, 8) != 'F')
- return ERR_AAC_INVALID_ADIF_HEADER;
-
- fhADIF->copyBit = GetBits(&bsi, 1);
- if (fhADIF->copyBit) {
- for (i = 0; i < ADIF_COPYID_SIZE; i++)
- fhADIF->copyID[i] = GetBits(&bsi, 8);
- }
- fhADIF->origCopy = GetBits(&bsi, 1);
- fhADIF->home = GetBits(&bsi, 1);
- fhADIF->bsType = GetBits(&bsi, 1);
- fhADIF->bitRate = GetBits(&bsi, 23);
- fhADIF->numPCE = GetBits(&bsi, 4) + 1;
- if (fhADIF->bsType == 0)
- fhADIF->bufferFull = GetBits(&bsi, 20);
-
- for (i = 0; i < fhADIF->numPCE; i++)
- DecodeProgramConfigElement(pce + i, &bsi);
-
- ByteAlignBitstream(&bsi);
-
- psi->nChans = GetNumChannelsADIF(pce, fhADIF->numPCE);
- psi->sampRateIdx = GetSampleRateIdxADIF(pce, fhADIF->numPCE);
-
- if (psi->nChans < 0 || psi->sampRateIdx < 0 || psi->sampRateIdx >= NUM_SAMPLE_RATES)
- return ERR_AAC_INVALID_ADIF_HEADER;
-
-
- aacDecInfo->prevBlockID = AAC_ID_INVALID;
- aacDecInfo->currBlockID = AAC_ID_INVALID;
- aacDecInfo->currInstTag = -1;
-
- aacDecInfo->bitRate = 0;
- aacDecInfo->nChans = psi->nChans;
- aacDecInfo->sampRate = sampRateTab[psi->sampRateIdx];
- aacDecInfo->profile = pce[0].profile;
- aacDecInfo->sbrEnabled = 0;
-
- bitsUsed = CalcBitsUsed(&bsi, *buf, *bitOffset);
- *buf += (bitsUsed + *bitOffset) >> 3;
- *bitOffset = (bitsUsed + *bitOffset) & 0x07;
- *bitsAvail -= bitsUsed ;
- if (*bitsAvail < 0)
- return ERR_AAC_INDATA_UNDERFLOW;
- return ERR_AAC_NONE;
- }
- int SetRawBlockParams(AACDecInfo *aacDecInfo, int copyLast, int nChans, int sampRate, int profile)
- {
- int idx;
- PSInfoBase *psi;
-
- if (!aacDecInfo || !aacDecInfo->psInfoBase)
- return ERR_AAC_NULL_POINTER;
- psi = (PSInfoBase *)(aacDecInfo->psInfoBase);
- if (!copyLast) {
- aacDecInfo->profile = profile;
- psi->nChans = nChans;
- for (idx = 0; idx < NUM_SAMPLE_RATES; idx++) {
- if (sampRate == sampRateTab[idx]) {
- psi->sampRateIdx = idx;
- break;
- }
- }
- if (idx == NUM_SAMPLE_RATES)
- return ERR_AAC_INVALID_FRAME;
- }
- aacDecInfo->nChans = psi->nChans;
- aacDecInfo->sampRate = sampRateTab[psi->sampRateIdx];
-
- if (psi->sampRateIdx >= NUM_SAMPLE_RATES || psi->sampRateIdx < 0 || aacDecInfo->profile != AAC_PROFILE_LC)
- return ERR_AAC_RAWBLOCK_PARAMS;
- return ERR_AAC_NONE;
- }
- int PrepareRawBlock(AACDecInfo *aacDecInfo)
- {
-
- if (!aacDecInfo || !aacDecInfo->psInfoBase)
- return ERR_AAC_NULL_POINTER;
-
- aacDecInfo->prevBlockID = AAC_ID_INVALID;
- aacDecInfo->currBlockID = AAC_ID_INVALID;
- aacDecInfo->currInstTag = -1;
-
- aacDecInfo->bitRate = 0;
- aacDecInfo->sbrEnabled = 0;
- return ERR_AAC_NONE;
- }
- int FlushCodec(AACDecInfo *aacDecInfo)
- {
- PSInfoBase *psi;
-
- if (!aacDecInfo || !aacDecInfo->psInfoBase)
- return ERR_AAC_NULL_POINTER;
- psi = (PSInfoBase *)(aacDecInfo->psInfoBase);
-
- ClearBuffer(psi->overlap, AAC_MAX_NCHANS * AAC_MAX_NSAMPS * sizeof(int));
- ClearBuffer(psi->prevWinShape, AAC_MAX_NCHANS * sizeof(int));
- return ERR_AAC_NONE;
- }
|