From 83293a2bb69f96a5fa7f14cfdea45441c9cec0d9 Mon Sep 17 00:00:00 2001 From: piratfm Date: Tue, 16 Apr 2013 16:45:20 +0300 Subject: AU alignment changed (last AU takes all possible space in superframe). Now player works more stable. --- aac-enc-dabplus.c | 15 ++++- libAACenc/src/aacenc.cpp | 2 + libAACenc/src/aacenc_lib.cpp | 1 + libAACenc/src/bitenc.cpp | 26 +++++--- libAACenc/src/qc_main.cpp | 6 +- libMpegTPEnc/src/tpenc_dab.cpp | 135 +++++++++++++++++++++++++++++++++++++++-- libMpegTPEnc/src/tpenc_dab.h | 1 + 7 files changed, 170 insertions(+), 16 deletions(-) diff --git a/aac-enc-dabplus.c b/aac-enc-dabplus.c index b1152a5..977077a 100644 --- a/aac-enc-dabplus.c +++ b/aac-enc-dabplus.c @@ -101,7 +101,6 @@ int main(int argc, char *argv[]) { int wav_format, bits_per_sample, sample_rate=48000, channels=2; uint8_t* input_buf; int16_t* convert_buf; - uint8_t* rs_encoded_buf; void *rs_handler = NULL; int aot = AOT_DABPLUS_AAC_LC; int afterburner = 0, raw_input=0; @@ -255,6 +254,13 @@ int main(int argc, char *argv[]) { fprintf(stderr, "Unable to set the RAW transmux\n"); return 1; } + + /*if (aacEncoder_SetParam(handle, AACENC_BITRATEMODE, 7 *AACENC_BR_MODE_SFR*) != AACENC_OK) { + fprintf(stderr, "Unable to set the bitrate mode\n"); + return 1; + }*/ + + fprintf(stderr, "AAC bitrate set to: %d\n", subchannel_index*8000); if (aacEncoder_SetParam(handle, AACENC_BITRATE, subchannel_index*8000) != AACENC_OK) { fprintf(stderr, "Unable to set the bitrate\n"); @@ -278,7 +284,6 @@ int main(int argc, char *argv[]) { int input_size = channels*2*info.frameLength; input_buf = (uint8_t*) malloc(input_size); convert_buf = (int16_t*) malloc(input_size); - rs_encoded_buf = (uint8_t*) malloc(120*subchannel_index); /* symsize=8, gfpoly=0x11d, fcr=0, prim=1, nroots=10, pad=135 */ rs_handler = init_rs_char(8, 0x11d, 0, 1, 10, 135); @@ -295,8 +300,14 @@ int main(int argc, char *argv[]) { fprintf(stderr, "(outbuf_size mod 5) = %d\n", outbuf_size % 5); } + fprintf(stderr, "outbuf_size: %d\n", outbuf_size); + //outbuf_size += (4 * subchannel_index * (8*8)/8) - outbuf_size/5; + fprintf(stderr, "outbuf_size: %d\n", outbuf_size); + int frame=0; while (1) { + memset(outbuf, 0x00, outbuf_size); + AACENC_BufDesc in_buf = { 0 }, out_buf = { 0 }; AACENC_InArgs in_args = { 0 }; AACENC_OutArgs out_args = { 0 }; diff --git a/libAACenc/src/aacenc.cpp b/libAACenc/src/aacenc.cpp index 1b1fd0d..89a8830 100644 --- a/libAACenc/src/aacenc.cpp +++ b/libAACenc/src/aacenc.cpp @@ -754,6 +754,7 @@ AAC_ENCODER_ERROR FDKaacEnc_EncodeFrame( HANDLE_AAC_ENC hAacEnc, /* advance psychoacoustics */ for (el=0; elnElements; el++) { ELEMENT_INFO elInfo = cm->elInfo[el]; + //fprintf(stderr, "elInfo.elType=%d\n", elInfo.elType); if ( (elInfo.elType == ID_SCE) || (elInfo.elType == ID_CPE) @@ -972,6 +973,7 @@ AAC_ENCODER_ERROR FDKaacEnc_EncodeFrame( HANDLE_AAC_ENC hAacEnc, /*-------------------------------------------- */ /* for ( all sub frames ) ... */ + fprintf(stderr, "totalBits=%d, qcOut->totalBits=%d, qcOut->totFillBits=%d\n", totalBits, qcOut->totalBits, qcOut->totFillBits); /* write bitstream header */ transportEnc_WriteAccessUnit( hTpEnc, diff --git a/libAACenc/src/aacenc_lib.cpp b/libAACenc/src/aacenc_lib.cpp index efde05f..b88a22c 100644 --- a/libAACenc/src/aacenc_lib.cpp +++ b/libAACenc/src/aacenc_lib.cpp @@ -1893,6 +1893,7 @@ AACENC_ERROR aacEncoder_SetParam( case 3: case 4: case 5: + case 7: case 8: settings->userBitrateMode = value; hAacEncoder->InitFlags |= AACENC_INIT_CONFIG | AACENC_INIT_TRANSPORT; diff --git a/libAACenc/src/bitenc.cpp b/libAACenc/src/bitenc.cpp index c521cc9..3b969de 100644 --- a/libAACenc/src/bitenc.cpp +++ b/libAACenc/src/bitenc.cpp @@ -87,7 +87,7 @@ amm-info@iis.fraunhofer.de contents/description: Bitstream encoder ******************************************************************************/ - +#include #include "bitenc.h" #include "bit_cnt.h" #include "dyn_bits.h" @@ -645,7 +645,7 @@ static INT FDKaacEnc_writeExtensionPayload( HANDLE_FDK_BITSTREAM hBitStream, #define FILL_NIBBLE_BITS ( 4 ) INT extBitsUsed = 0; - + //fprintf(stderr, "FDKaacEnc_writeExtensionPayload() extPayloadType=%d\n", extPayloadType); if (extPayloadBits >= EXT_TYPE_BITS) { UCHAR fillByte = 0x00; /* for EXT_FIL and EXT_FILL_DATA */ @@ -749,7 +749,7 @@ static INT FDKaacEnc_writeDataStreamElement( HANDLE_TRANSPORTENC hTpEnc, #define MAX_DSE_DATA_BYTES ( 510 ) INT dseBitsUsed = 0; - + fprintf(stderr, "FDKaacEnc_writeDataStreamElement() dataPayloadBytes=%d\n", dataPayloadBytes); while (dataPayloadBytes > 0) { int esc_count = -1; @@ -840,6 +840,7 @@ INT FDKaacEnc_writeExtensionData( HANDLE_TRANSPORTENC hTpEnc, hBitStream = transportEnc_GetBitstream(hTpEnc); } + //fprintf(stderr, "FDKaacEnc_writeExtensionData() pExtension->type=%d\n", pExtension->type); if (syntaxFlags & (AC_SCALABLE|AC_ER)) { if ( syntaxFlags & AC_DRM ) @@ -1393,7 +1394,7 @@ AAC_ENCODER_ERROR FDKaacEnc_WriteBitstream(HANDLE_TRANSPORTENC hTpEnc, if (channelElementExtensionWritten[i][n]==0) { - /* Write all ramaining extension payloads in element */ + /* Write all remaining extension payloads in element */ FDKaacEnc_writeExtensionData( hTpEnc, &qcOut->qcElement[i]->extension[n], 0, @@ -1430,9 +1431,16 @@ AAC_ENCODER_ERROR FDKaacEnc_WriteBitstream(HANDLE_TRANSPORTENC hTpEnc, /* Add fill data / stuffing bits */ n = qcOut->nExtensions; - qcOut->extension[n].type = EXT_FILL_DATA; - qcOut->extension[n].nPayloadBits = qcOut->totFillBits; - qcOut->nExtensions++; + +// if (!(syntaxFlags & AC_DAB)) { + qcOut->extension[n].type = EXT_FILL_DATA; + qcOut->extension[n].nPayloadBits = qcOut->totFillBits; + qcOut->nExtensions++; +// } else { +// doByteAlign = 0; +// } + if (syntaxFlags & AC_DAB) + doByteAlign = 0; /* Write global extension payload and fill data */ for (n = 0; (n < qcOut->nExtensions) && (n < (2+2)); n++) @@ -1448,7 +1456,7 @@ AAC_ENCODER_ERROR FDKaacEnc_WriteBitstream(HANDLE_TRANSPORTENC hTpEnc, /* For EXT_FIL or EXT_FILL_DATA we could do an additional sanity check here */ } - if (!(syntaxFlags & (AC_SCALABLE|AC_ER))) { + if (!(syntaxFlags & (AC_SCALABLE|AC_ER|AC_DAB))) { FDKwriteBits(hBs, ID_END, EL_ID_BITS); } @@ -1467,9 +1475,11 @@ AAC_ENCODER_ERROR FDKaacEnc_WriteBitstream(HANDLE_TRANSPORTENC hTpEnc, transportEnc_EndAccessUnit(hTpEnc, &frameBits); if (frameBits != qcOut->totalBits + qcKernel->globHdrBits){ + fprintf(stderr, "frameBits != qcOut->totalBits + qcKernel->globHdrBits: %d != %d + %d", frameBits, qcOut->totalBits, qcKernel->globHdrBits); return AAC_ENC_WRITTEN_BITS_ERROR; } + fprintf(stderr, "ErrorStatus=%d", ErrorStatus); return ErrorStatus; } diff --git a/libAACenc/src/qc_main.cpp b/libAACenc/src/qc_main.cpp index e09769e..9866a3a 100644 --- a/libAACenc/src/qc_main.cpp +++ b/libAACenc/src/qc_main.cpp @@ -1378,7 +1378,7 @@ AAC_ENCODER_ERROR FDKaacEnc_FinalizeBitConsumption(CHANNEL_MAPPING *cm, /* Get total consumed bits in AU */ qcOut->totalBits = qcOut->staticBits + qcOut->usedDynBits + qcOut->totFillBits + qcOut->elementExtBits + qcOut->globalExtBits; - +#if 1 if (qcKernel->bitrateMode==QCDATA_BR_MODE_CBR) { /* Now we can get the exact transport bit amount, and hopefully it is equal to the estimated value */ @@ -1420,7 +1420,7 @@ AAC_ENCODER_ERROR FDKaacEnc_FinalizeBitConsumption(CHANNEL_MAPPING *cm, } } /* MODE_CBR */ - +#endif /* Update exact number of consumed header bits. */ qcKernel->globHdrBits = transportEnc_GetStaticBits(hTpEnc, qcOut->totalBits); @@ -1441,6 +1441,8 @@ AAC_ENCODER_ERROR FDKaacEnc_FinalizeBitConsumption(CHANNEL_MAPPING *cm, aot, epConfig ); + //fprintf(stderr, "FinalizeBitConsumption(): totFillBits=%d, qcOut->totFillBits=%d \n", totFillBits, qcOut->totFillBits); + /* now distribute extra fillbits and alignbits */ alignBits = 7 - (qcOut->staticBits + qcOut->usedDynBits + qcOut->elementExtBits + qcOut->totFillBits + qcOut->globalExtBits -1)%8; diff --git a/libMpegTPEnc/src/tpenc_dab.cpp b/libMpegTPEnc/src/tpenc_dab.cpp index 8f2603a..afdac78 100644 --- a/libMpegTPEnc/src/tpenc_dab.cpp +++ b/libMpegTPEnc/src/tpenc_dab.cpp @@ -95,6 +95,7 @@ amm-info@iis.fraunhofer.de #include "tpenc_lib.h" #include "tpenc_asc.h" +#include "common_fix.h" int dabWrite_CrcStartReg( HANDLE_DAB pDab, /*!< pointer to dab stucture */ @@ -103,7 +104,7 @@ int dabWrite_CrcStartReg( ) { //fprintf(stderr, "dabWrite_CrcStartReg(%p): bits in crc region=%d\n", hBs, mBits); - //return ( FDKcrcStartReg(&pDab->crcInfo, hBs, mBits) ); + return ( FDKcrcStartReg(&pDab->crcInfo2, hBs, mBits) ); } void dabWrite_CrcEndReg( @@ -113,7 +114,7 @@ void dabWrite_CrcEndReg( ) { //fprintf(stderr, "dabWrite_CrcEndReg(%p): crc region=%d\n", hBs, reg); - //FDKcrcEndReg(&pDab->crcInfo, hBs, reg); + FDKcrcEndReg(&pDab->crcInfo2, hBs, reg); } int dabWrite_GetHeaderBits( HANDLE_DAB hDab ) @@ -198,6 +199,7 @@ INT dabWrite_Init(HANDLE_DAB hDab, CODER_CONFIG *config) FDKcrcInit(&hDab->crcInfo, 0x1021, 0xFFFF, 16); FDKcrcInit(&hDab->crcFire, 0x782d, 0, 16); + FDKcrcInit(&hDab->crcInfo2, 0x8005, 0xFFFF, 16); hDab->currentBlock = 0; hDab->headerBits = dabWrite_GetHeaderBits(hDab); @@ -263,6 +265,99 @@ int dabWrite_EncodeHeader(HANDLE_DAB hDab, return 0; } +int dabWrite_writeExtensionFillPayload(HANDLE_FDK_BITSTREAM hBitStream, int extPayloadBits) +{ +#define EXT_TYPE_BITS ( 4 ) +#define DATA_EL_VERSION_BITS ( 4 ) +#define FILL_NIBBLE_BITS ( 4 ) + +#define EXT_TYPE_BITS ( 4 ) +#define DATA_EL_VERSION_BITS ( 4 ) +#define FILL_NIBBLE_BITS ( 4 ) + + INT extBitsUsed = 0; + INT extPayloadType = EXT_FIL; + //fprintf(stderr, "FDKaacEnc_writeExtensionPayload() extPayloadType=%d\n", extPayloadType); + if (extPayloadBits >= EXT_TYPE_BITS) + { + UCHAR fillByte = 0x00; /* for EXT_FIL and EXT_FILL_DATA */ + + if (hBitStream != NULL) { + FDKwriteBits(hBitStream, extPayloadType, EXT_TYPE_BITS); + } + extBitsUsed += EXT_TYPE_BITS; + + switch (extPayloadType) { + case EXT_FILL_DATA: + fillByte = 0xA5; + case EXT_FIL: + default: + if (hBitStream != NULL) { + int writeBits = extPayloadBits; + FDKwriteBits(hBitStream, 0x00, FILL_NIBBLE_BITS); + writeBits -= 8; /* acount for the extension type and the fill nibble */ + while (writeBits >= 8) { + FDKwriteBits(hBitStream, fillByte, 8); + writeBits -= 8; + } + } + extBitsUsed += FILL_NIBBLE_BITS + (extPayloadBits & ~0x7) - 8; + break; + } + } + + return (extBitsUsed); +} + +void dabWrite_FillRawDataBlock(HANDLE_FDK_BITSTREAM hBitStream, int payloadBits) +{ + INT extBitsUsed = 0; +#define EL_ID_BITS ( 3 ) +#define FILL_EL_COUNT_BITS ( 4 ) +#define FILL_EL_ESC_COUNT_BITS ( 8 ) +#define MAX_FILL_DATA_BYTES ( 269 ) + while (payloadBits >= (EL_ID_BITS + FILL_EL_COUNT_BITS)) { + INT cnt, esc_count=-1, alignBits=7; + + payloadBits -= EL_ID_BITS + FILL_EL_COUNT_BITS; + if (payloadBits >= 15*8) { + payloadBits -= FILL_EL_ESC_COUNT_BITS; + esc_count = 0; /* write esc_count even if cnt becomes smaller 15 */ + } + alignBits = 0; + + cnt = fixMin(MAX_FILL_DATA_BYTES, (payloadBits+alignBits)>>3); + + if (cnt >= 15) { + esc_count = cnt - 15 + 1; + } + + if (hBitStream != NULL) { + /* write bitstream */ + FDKwriteBits(hBitStream, ID_FIL, EL_ID_BITS); + if (esc_count >= 0) { + FDKwriteBits(hBitStream, 15, FILL_EL_COUNT_BITS); + FDKwriteBits(hBitStream, esc_count, FILL_EL_ESC_COUNT_BITS); + } else { + FDKwriteBits(hBitStream, cnt, FILL_EL_COUNT_BITS); + } + } + + extBitsUsed += EL_ID_BITS + FILL_EL_COUNT_BITS + ((esc_count>=0) ? FILL_EL_ESC_COUNT_BITS : 0); + + cnt = fixMin(cnt*8, payloadBits); /* convert back to bits */ +#if 0 + extBitsUsed += FDKaacEnc_writeExtensionPayload( hBitStream, + pExtension->type, + pExtension->pPayload, + cnt ); +#else + extBitsUsed += dabWrite_writeExtensionFillPayload(hBitStream, cnt); +#endif + payloadBits -= cnt; + } +} + void dabWrite_EndRawDataBlock(HANDLE_DAB hDab, HANDLE_FDK_BITSTREAM hBs, int *pBits) @@ -270,10 +365,41 @@ void dabWrite_EndRawDataBlock(HANDLE_DAB hDab, FDK_BITSTREAM bsWriter; INT crcIndex = 0; USHORT crcData; + INT writeBits=0; + INT writeBitsNonLastBlock=0; + INT writeBitsLastBlock=0; +#if 1 + if (hDab->currentBlock == hDab->num_raw_blocks) { + //calculate byte-alignment before writing ID_FIL + if((FDKgetValidBits(hBs)+3) % 8){ + writeBits = 8 - ((FDKgetValidBits(hBs)+3) % 8); + } + + INT offset_end = hDab->subchannels_num*110*8 - 2*8 - 3; + writeBitsLastBlock = offset_end - FDKgetValidBits(hBs); + dabWrite_FillRawDataBlock(hBs, writeBitsLastBlock); + FDKsyncCache(hBs); + fprintf(stderr, "FIL-element written=%d\n", writeBitsLastBlock); + writeBitsLastBlock=writeBits; + } +#endif + FDKwriteBits(hBs, 7, 3); //finalize AU: ID_END + FDKsyncCache(hBs); + //byte-align (if ID_FIL doesn't align it). + if(FDKgetValidBits(hBs) % 8){ + writeBits = 8 - (FDKgetValidBits(hBs) % 8); + FDKwriteBits(hBs, 0x00, writeBits); + FDKsyncCache(hBs); + } + + //fake-written bits alignment for last AU + if (hDab->currentBlock == hDab->num_raw_blocks) + writeBits=writeBitsLastBlock; + INT frameLen = (FDKgetValidBits(hBs) - hDab->subFrameStartBit) >> 3; - //fprintf(stderr, "frame=%d\n", frameLen); + fprintf(stderr, "frame=%d, offset writeBits=%d\n", frameLen, writeBits); FDK_ASSERT(FDKgetValidBits(hBs) % 8 == 0); //only aligned au's - FDK_ASSERT(hDab->subchannels_num*110*8 > FDKgetValidBits(hBs)+2*8); //don't overlap superframe + FDK_ASSERT(hDab->subchannels_num*110*8 >= FDKgetValidBits(hBs)+2*8); //don't overlap superframe FDKinitBitStream(&bsWriter, hBs->hBitBuf.Buffer, hBs->hBitBuf.bufSize, 0, BS_WRITER); FDKpushFor(&bsWriter, hDab->subFrameStartBit); @@ -328,6 +454,7 @@ void dabWrite_EndRawDataBlock(HANDLE_DAB hDab, else *pBits += 16; + *pBits += writeBits + 3; //size: ID_END + alignment /* Correct *pBits to reflect the amount of bits of the current subframe */ *pBits -= hDab->subFrameStartBit; diff --git a/libMpegTPEnc/src/tpenc_dab.h b/libMpegTPEnc/src/tpenc_dab.h index 5afa4b8..17b83c6 100644 --- a/libMpegTPEnc/src/tpenc_dab.h +++ b/libMpegTPEnc/src/tpenc_dab.h @@ -114,6 +114,7 @@ typedef struct { int crcIndex; FDK_CRCINFO crcInfo; FDK_CRCINFO crcFire; + FDK_CRCINFO crcInfo2; USHORT tab[256]; } STRUCT_DAB; -- cgit v1.2.3