summaryrefslogtreecommitdiffstats
path: root/libMpegTPEnc/src
diff options
context:
space:
mode:
authorDave Burke <daveburke@google.com>2012-04-17 09:51:45 -0700
committerDave Burke <daveburke@google.com>2012-04-17 23:04:43 -0700
commit9bf37cc9712506b2483650c82d3c41152337ef7e (patch)
tree77db44e2bae06e3d144b255628be2b7a55c581d3 /libMpegTPEnc/src
parenta37315fe10ee143d6d0b28c19d41a476a23e63ea (diff)
downloadODR-AudioEnc-9bf37cc9712506b2483650c82d3c41152337ef7e.tar.gz
ODR-AudioEnc-9bf37cc9712506b2483650c82d3c41152337ef7e.tar.bz2
ODR-AudioEnc-9bf37cc9712506b2483650c82d3c41152337ef7e.zip
Fraunhofer AAC codec.
License boilerplate update to follow. Change-Id: I2810460c11a58b6d148d84673cc031f3685e79b5
Diffstat (limited to 'libMpegTPEnc/src')
-rw-r--r--libMpegTPEnc/src/Android.mk21
-rw-r--r--libMpegTPEnc/src/tpenc_adif.cpp120
-rw-r--r--libMpegTPEnc/src/tpenc_adif.h73
-rw-r--r--libMpegTPEnc/src/tpenc_adts.cpp253
-rw-r--r--libMpegTPEnc/src/tpenc_adts.h156
-rw-r--r--libMpegTPEnc/src/tpenc_asc.cpp503
-rw-r--r--libMpegTPEnc/src/tpenc_asc.h80
-rw-r--r--libMpegTPEnc/src/tpenc_latm.cpp820
-rw-r--r--libMpegTPEnc/src/tpenc_latm.h204
-rw-r--r--libMpegTPEnc/src/tpenc_lib.cpp569
-rw-r--r--libMpegTPEnc/src/version8
11 files changed, 2807 insertions, 0 deletions
diff --git a/libMpegTPEnc/src/Android.mk b/libMpegTPEnc/src/Android.mk
new file mode 100644
index 0000000..5d6d7e0
--- /dev/null
+++ b/libMpegTPEnc/src/Android.mk
@@ -0,0 +1,21 @@
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := \
+ tpenc_adif.cpp \
+ tpenc_adts.cpp \
+ tpenc_asc.cpp \
+ tpenc_latm.cpp \
+ tpenc_lib.cpp
+
+LOCAL_CFLAGS := -DANDROID
+
+LOCAL_C_INCLUDES += \
+ $(LOCAL_PATH) \
+ $(LOCAL_PATH)/../include \
+ $(LOCAL_PATH)/../../libSYS/include \
+ $(LOCAL_PATH)/../../libFDK/include
+
+LOCAL_MODULE:= libMpegTPEnc
+
+include $(BUILD_STATIC_LIBRARY)
diff --git a/libMpegTPEnc/src/tpenc_adif.cpp b/libMpegTPEnc/src/tpenc_adif.cpp
new file mode 100644
index 0000000..d217643
--- /dev/null
+++ b/libMpegTPEnc/src/tpenc_adif.cpp
@@ -0,0 +1,120 @@
+/******************************** MPEG Audio Encoder **************************
+
+ (C) Copyright Fraunhofer IIS (2009)
+ All Rights Reserved
+
+ Please be advised that this software and/or program delivery is
+ Confidential Information of Fraunhofer and subject to and covered by the
+
+ Fraunhofer IIS Software Evaluation Agreement
+ between Google Inc. and Fraunhofer
+ effective and in full force since March 1, 2012.
+
+ You may use this software and/or program only under the terms and
+ conditions described in the above mentioned Fraunhofer IIS Software
+ Evaluation Agreement. Any other and/or further use requires a separate agreement.
+
+
+ This software and/or program is protected by copyright law and international
+ treaties. Any reproduction or distribution of this software and/or program,
+ or any portion of it, may result in severe civil and criminal penalties, and
+ will be prosecuted to the maximum extent possible under law.
+
+ $Id$
+ contents/description: ADIF Transport Headers writing
+
+******************************************************************************/
+
+#include "tpenc_adif.h"
+
+#include "tpenc_lib.h"
+#include "tpenc_asc.h"
+
+
+
+int adifWrite_EncodeHeader(ADIF_INFO *adif,
+ HANDLE_FDK_BITSTREAM hBs,
+ INT adif_buffer_fullness)
+{
+ /* ADIF/PCE/ADTS definitions */
+ const char adifId[5]="ADIF";
+ const int copyRightIdPresent=0;
+ const int originalCopy=0;
+ const int home=0;
+
+ int i;
+
+ INT sampleRate = adif->samplingRate;
+ INT totalBitRate = adif->bitRate;
+
+ if (adif->headerWritten)
+ return 0;
+
+ /* Align inside PCE with respect to the first bit of the header */
+ UINT alignAnchor = FDKgetValidBits(hBs);
+
+ /* Signal variable bitrate if buffer fullnes exceeds 20 bit */
+ adif->bVariableRate = ( adif_buffer_fullness >= (INT)(0x1<<20) ) ? 1 : 0;
+
+ FDKwriteBits(hBs, adifId[0],8);
+ FDKwriteBits(hBs, adifId[1],8);
+ FDKwriteBits(hBs, adifId[2],8);
+ FDKwriteBits(hBs, adifId[3],8);
+
+
+ FDKwriteBits(hBs, copyRightIdPresent ? 1:0,1);
+
+ if(copyRightIdPresent) {
+ for(i=0;i<72;i++) {
+ FDKwriteBits(hBs,0,1);
+ }
+ }
+ FDKwriteBits(hBs, originalCopy ? 1:0,1);
+ FDKwriteBits(hBs, home ? 1:0,1);
+ FDKwriteBits(hBs, adif->bVariableRate?1:0, 1);
+ FDKwriteBits(hBs, totalBitRate,23);
+
+ /* we write only one PCE at the moment */
+ FDKwriteBits(hBs, 0, 4);
+
+ if(!adif->bVariableRate) {
+ FDKwriteBits(hBs, adif_buffer_fullness, 20);
+ }
+
+ /* Write PCE */
+ transportEnc_writePCE(hBs, adif->cm, sampleRate, adif->instanceTag, adif->profile, 0, 0, alignAnchor);
+
+ return 0;
+}
+
+int adifWrite_GetHeaderBits(ADIF_INFO *adif)
+{
+ /* ADIF definitions */
+ const int copyRightIdPresent=0;
+
+ if (adif->headerWritten)
+ return 0;
+
+ int bits = 0;
+
+ bits += 8*4; /* ADIF ID */
+
+ bits += 1; /* Copyright present */
+
+ if (copyRightIdPresent)
+ bits += 72; /* Copyright ID */
+
+ bits += 26;
+
+ bits += 4; /* Number of PCE's */
+
+ if(!adif->bVariableRate) {
+ bits += 20;
+ }
+
+ /* write PCE */
+ bits = transportEnc_GetPCEBits(adif->cm, 0, bits);
+
+ return bits;
+}
+
diff --git a/libMpegTPEnc/src/tpenc_adif.h b/libMpegTPEnc/src/tpenc_adif.h
new file mode 100644
index 0000000..989ed9a
--- /dev/null
+++ b/libMpegTPEnc/src/tpenc_adif.h
@@ -0,0 +1,73 @@
+/******************************** MPEG Audio Encoder **************************
+
+ (C) Copyright Fraunhofer IIS (2009)
+ All Rights Reserved
+
+ Please be advised that this software and/or program delivery is
+ Confidential Information of Fraunhofer and subject to and covered by the
+
+ Fraunhofer IIS Software Evaluation Agreement
+ between Google Inc. and Fraunhofer
+ effective and in full force since March 1, 2012.
+
+ You may use this software and/or program only under the terms and
+ conditions described in the above mentioned Fraunhofer IIS Software
+ Evaluation Agreement. Any other and/or further use requires a separate agreement.
+
+
+ This software and/or program is protected by copyright law and international
+ treaties. Any reproduction or distribution of this software and/or program,
+ or any portion of it, may result in severe civil and criminal penalties, and
+ will be prosecuted to the maximum extent possible under law.
+
+ $Id$
+ Initial author: Alex Goeschel
+ contents/description: Transport Headers support
+
+******************************************************************************/
+
+#ifndef TPENC_ADIF_H
+#define TPENC_ADIF_H
+
+#include "machine_type.h"
+#include "FDK_bitstream.h"
+
+#include "tp_data.h"
+
+typedef struct {
+ CHANNEL_MODE cm;
+ INT samplingRate;
+ INT bitRate;
+ int profile;
+ int bVariableRate;
+ int instanceTag;
+ int headerWritten;
+} ADIF_INFO;
+
+/**
+ * \brief encodes ADIF Header
+ *
+ * \param adif pointer to ADIF_INFO structure
+ * \param hBitStream handle of bitstream, where the ADIF header is written into
+ * \param adif_buffer_fullness buffer fullness value for the ADIF header
+ *
+ * \return 0 on success
+ */
+int adifWrite_EncodeHeader(
+ ADIF_INFO *adif,
+ HANDLE_FDK_BITSTREAM hBitStream,
+ INT adif_buffer_fullness
+ );
+
+/**
+ * \brief Get bit demand of a ADIF header
+ *
+ * \param adif pointer to ADIF_INFO structure
+ *
+ * \return amount of bits required to write the ADIF header according to the data
+ * contained in the adif parameter
+ */
+int adifWrite_GetHeaderBits( ADIF_INFO *adif );
+
+#endif /* TPENC_ADIF_H */
+
diff --git a/libMpegTPEnc/src/tpenc_adts.cpp b/libMpegTPEnc/src/tpenc_adts.cpp
new file mode 100644
index 0000000..86b11c1
--- /dev/null
+++ b/libMpegTPEnc/src/tpenc_adts.cpp
@@ -0,0 +1,253 @@
+/******************************** MPEG Audio Encoder **************************
+
+ (C) copyright Fraunhofer-IIS (1999)
+ All Rights Reserved
+
+ Please be advised that this software and/or program delivery is
+ Confidential Information of Fraunhofer and subject to and covered by the
+
+ Fraunhofer IIS Software Evaluation Agreement
+ between Google Inc. and Fraunhofer
+ effective and in full force since March 1, 2012.
+
+ You may use this software and/or program only under the terms and
+ conditions described in the above mentioned Fraunhofer IIS Software
+ Evaluation Agreement. Any other and/or further use requires a separate agreement.
+
+
+ This software and/or program is protected by copyright law and international
+ treaties. Any reproduction or distribution of this software and/or program,
+ or any portion of it, may result in severe civil and criminal penalties, and
+ will be prosecuted to the maximum extent possible under law.
+
+ $Id$
+ Initial author: Alex Groeschel
+ contents/description: ADTS Transport Headers support
+
+******************************************************************************/
+
+#include "tpenc_adts.h"
+
+
+#include "tpenc_lib.h"
+#include "tpenc_asc.h"
+
+
+int adtsWrite_CrcStartReg(
+ HANDLE_ADTS pAdts, /*!< pointer to adts stucture */
+ HANDLE_FDK_BITSTREAM hBs, /*!< handle to current bit buffer structure */
+ int mBits /*!< number of bits in crc region */
+ )
+{
+ if (pAdts->protection_absent) {
+ return 0;
+ }
+ return ( FDKcrcStartReg(&pAdts->crcInfo, hBs, mBits) );
+}
+
+void adtsWrite_CrcEndReg(
+ HANDLE_ADTS pAdts, /*!< pointer to adts crc info stucture */
+ HANDLE_FDK_BITSTREAM hBs, /*!< handle to current bit buffer structure */
+ int reg /*!< crc region */
+ )
+{
+ if (pAdts->protection_absent == 0)
+ {
+ FDKcrcEndReg(&pAdts->crcInfo, hBs, reg);
+ }
+}
+
+int adtsWrite_GetHeaderBits( HANDLE_ADTS hAdts )
+{
+ int bits = 0;
+
+ if (hAdts->currentBlock == 0) {
+ /* Static and variable header bits */
+ bits = 56;
+ if (!hAdts->protection_absent) {
+ /* Add header/ single raw data block CRC bits */
+ bits += 16;
+ if (hAdts->num_raw_blocks>0) {
+ /* Add bits of raw data block position markers */
+ bits += (hAdts->num_raw_blocks)*16;
+ }
+ }
+ }
+ if (!hAdts->protection_absent && hAdts->num_raw_blocks>0) {
+ /* Add raw data block CRC bits. Not really part of the header, put they cause bit overhead to be accounted. */
+ bits += 16;
+ }
+
+ hAdts->headerBits = bits;
+
+ return bits;
+}
+
+INT adtsWrite_Init(HANDLE_ADTS hAdts, CODER_CONFIG *config)
+{
+ /* Sanity checks */
+ if ( config->nSubFrames < 1
+ || config->nSubFrames > 4
+ || (int)config->aot > 4
+ || (int)config->aot < 1 ) {
+ return -1;
+ }
+
+ /* fixed header */
+ if (config->flags & CC_MPEG_ID) {
+ hAdts->mpeg_id = 0; /* MPEG 4 */
+ } else {
+ hAdts->mpeg_id = 1; /* MPEG 2 */
+ }
+ hAdts->layer=0;
+ hAdts->protection_absent = ! (config->flags & CC_PROTECTION);
+ hAdts->profile = ((int)config->aot) - 1;
+ hAdts->sample_freq_index = getSamplingRateIndex(config->samplingRate);
+ hAdts->sample_freq = config->samplingRate;
+ hAdts->private_bit=0;
+ hAdts->channel_mode = config->channelMode;
+ hAdts->original=0;
+ hAdts->home=0;
+ /* variable header */
+ hAdts->copyright_id=0;
+ hAdts->copyright_start=0;
+
+ hAdts->num_raw_blocks=config->nSubFrames-1; /* 0 means 1 raw data block */
+
+ FDKcrcInit(&hAdts->crcInfo, 0x8005, 0xFFFF, 16);
+
+ hAdts->currentBlock = 0;
+
+
+ return 0;
+}
+
+int adtsWrite_EncodeHeader(HANDLE_ADTS hAdts,
+ HANDLE_FDK_BITSTREAM hBitStream,
+ int buffer_fullness,
+ int frame_length)
+{
+ INT crcIndex = 0;
+
+
+ hAdts->headerBits = adtsWrite_GetHeaderBits(hAdts);
+
+ FDK_ASSERT(((frame_length+hAdts->headerBits)/8)<0x2000); /*13 bit*/
+ FDK_ASSERT(buffer_fullness<0x800); /* 11 bit */
+
+ if (!hAdts->protection_absent) {
+ FDKcrcReset(&hAdts->crcInfo);
+ }
+
+ if (hAdts->currentBlock == 0) {
+ FDKresetBitbuffer(hBitStream, BS_WRITER);
+ }
+
+ hAdts->subFrameStartBit = FDKgetValidBits(hBitStream);
+
+ /* Skip new header if this is raw data block 1..n */
+ if (hAdts->currentBlock == 0)
+ {
+ FDKresetBitbuffer(hBitStream, BS_WRITER);
+
+ if (hAdts->num_raw_blocks == 0) {
+ crcIndex = adtsWrite_CrcStartReg(hAdts, hBitStream, 0);
+ }
+
+ /* fixed header */
+ FDKwriteBits(hBitStream, 0xFFF, 12);
+ FDKwriteBits(hBitStream, hAdts->mpeg_id, 1);
+ FDKwriteBits(hBitStream, hAdts->layer, 2);
+ FDKwriteBits(hBitStream, hAdts->protection_absent, 1);
+ FDKwriteBits(hBitStream, hAdts->profile, 2);
+ FDKwriteBits(hBitStream, hAdts->sample_freq_index, 4);
+ FDKwriteBits(hBitStream, hAdts->private_bit, 1);
+ FDKwriteBits(hBitStream, getChannelConfig(hAdts->channel_mode), 3);
+ FDKwriteBits(hBitStream, hAdts->original, 1);
+ FDKwriteBits(hBitStream, hAdts->home, 1);
+ /* variable header */
+ FDKwriteBits(hBitStream, hAdts->copyright_id, 1);
+ FDKwriteBits(hBitStream, hAdts->copyright_start, 1);
+ FDKwriteBits(hBitStream, (frame_length + hAdts->headerBits)>>3, 13);
+ FDKwriteBits(hBitStream, buffer_fullness, 11);
+ FDKwriteBits(hBitStream, hAdts->num_raw_blocks, 2);
+
+ if (!hAdts->protection_absent) {
+ int i;
+
+ /* End header CRC portion for single raw data block and write dummy zero values for unknown fields. */
+ if (hAdts->num_raw_blocks == 0) {
+ adtsWrite_CrcEndReg(hAdts, hBitStream, crcIndex);
+ } else {
+ for (i=0; i<hAdts->num_raw_blocks; i++) {
+ FDKwriteBits(hBitStream, 0, 16);
+ }
+ }
+ FDKwriteBits(hBitStream, 0, 16);
+ }
+ } /* End of ADTS header */
+
+ return 0;
+}
+
+void adtsWrite_EndRawDataBlock(HANDLE_ADTS hAdts,
+ HANDLE_FDK_BITSTREAM hBs,
+ int *pBits)
+{
+ if (!hAdts->protection_absent) {
+ FDK_BITSTREAM bsWriter;
+
+ FDKinitBitStream(&bsWriter, hBs->hBitBuf.Buffer, hBs->hBitBuf.bufSize, 0, BS_WRITER);
+ FDKpushFor(&bsWriter, 56);
+
+ if (hAdts->num_raw_blocks == 0) {
+ FDKwriteBits(&bsWriter, FDKcrcGetCRC(&hAdts->crcInfo), 16);
+ } else {
+ int distance;
+
+ /* Write CRC of current raw data block */
+ FDKwriteBits(hBs, FDKcrcGetCRC(&hAdts->crcInfo), 16);
+
+ /* Write distance to current data block */
+ if (hAdts->currentBlock < hAdts->num_raw_blocks) {
+ FDKpushFor(&bsWriter, hAdts->currentBlock*16);
+ distance = FDKgetValidBits(hBs) - (56 + (hAdts->num_raw_blocks)*16 + 16);
+ FDKwriteBits(&bsWriter, distance>>3, 16);
+ }
+ }
+ FDKsyncCache(&bsWriter);
+ }
+
+ /* Write total frame lenth for multiple raw data blocks and header CRC */
+ if (hAdts->num_raw_blocks > 0 && hAdts->currentBlock == hAdts->num_raw_blocks) {
+ FDK_BITSTREAM bsWriter;
+ int crcIndex = 0;
+
+ FDKinitBitStream(&bsWriter, hBs->hBitBuf.Buffer, hBs->hBitBuf.bufSize, 0, BS_WRITER);
+
+ if (!hAdts->protection_absent) {
+ FDKcrcReset(&hAdts->crcInfo);
+ crcIndex = FDKcrcStartReg(&hAdts->crcInfo, &bsWriter, 0);
+ }
+ /* Write total frame length */
+ FDKpushFor(&bsWriter, 56-28+2);
+ FDKwriteBits(&bsWriter, FDKgetValidBits(hBs)>>3, 13);
+
+ /* Write header CRC */
+ if (!hAdts->protection_absent) {
+ FDKpushFor(&bsWriter, 11+2 + (hAdts->num_raw_blocks)*16);
+ FDKcrcEndReg(&hAdts->crcInfo, &bsWriter, crcIndex);
+ FDKwriteBits(&bsWriter, FDKcrcGetCRC(&hAdts->crcInfo), 16);
+ }
+ FDKsyncCache(&bsWriter);
+ }
+
+ /* Correct *pBits to reflect the amount of bits of the current subframe */
+ *pBits -= hAdts->subFrameStartBit;
+ if (!hAdts->protection_absent && hAdts->num_raw_blocks > 0) {
+ /* Fixup CRC bits, since they come after each raw data block */
+ *pBits += 16;
+ }
+ hAdts->currentBlock++;
+}
+
diff --git a/libMpegTPEnc/src/tpenc_adts.h b/libMpegTPEnc/src/tpenc_adts.h
new file mode 100644
index 0000000..1608769
--- /dev/null
+++ b/libMpegTPEnc/src/tpenc_adts.h
@@ -0,0 +1,156 @@
+/******************************** MPEG Audio Encoder **************************
+
+ (C) copyright Fraunhofer-IIS (1999)
+ All Rights Reserved
+
+ Please be advised that this software and/or program delivery is
+ Confidential Information of Fraunhofer and subject to and covered by the
+
+ Fraunhofer IIS Software Evaluation Agreement
+ between Google Inc. and Fraunhofer
+ effective and in full force since March 1, 2012.
+
+ You may use this software and/or program only under the terms and
+ conditions described in the above mentioned Fraunhofer IIS Software
+ Evaluation Agreement. Any other and/or further use requires a separate agreement.
+
+
+ This software and/or program is protected by copyright law and international
+ treaties. Any reproduction or distribution of this software and/or program,
+ or any portion of it, may result in severe civil and criminal penalties, and
+ will be prosecuted to the maximum extent possible under law.
+
+ $Id$
+ Initial author: Alex Groeschel
+ contents/description: ADTS Transport writer
+
+******************************************************************************/
+
+#ifndef TPENC_ADTS_H
+#define TPENC_ADTS_H
+
+
+
+#include "tp_data.h"
+
+#include "FDK_crc.h"
+
+typedef struct {
+ INT sample_freq;
+ CHANNEL_MODE channel_mode;
+ UCHAR decoderCanDoMpeg4;
+ UCHAR mpeg_id;
+ UCHAR layer;
+ UCHAR protection_absent;
+ UCHAR profile;
+ UCHAR sample_freq_index;
+ UCHAR private_bit;
+ UCHAR original;
+ UCHAR home;
+ UCHAR copyright_id;
+ UCHAR copyright_start;
+ USHORT frame_length;
+ UCHAR num_raw_blocks;
+ UCHAR BufferFullnesStartFlag;
+ int headerBits; /*!< Header bit demand for the current raw data block */
+ int currentBlock; /*!< Index of current raw data block */
+ int subFrameStartBit; /*!< Bit position where the current raw data block begins */
+ FDK_CRCINFO crcInfo;
+} STRUCT_ADTS;
+
+typedef STRUCT_ADTS *HANDLE_ADTS;
+
+/**
+ * \brief Initialize ADTS data structure
+ *
+ * \param hAdts ADTS data handle
+ * \param config a valid CODER_CONFIG struct from where the required
+ * information for the ADTS header is extrated from
+ *
+ * \return 0 in case of success.
+ */
+INT adtsWrite_Init(
+ HANDLE_ADTS hAdts,
+ CODER_CONFIG *config
+ );
+
+/**
+ * \brief Get the total bit overhead caused by ADTS
+ *
+ * \hAdts handle to ADTS data
+ *
+ * \return Amount of additional bits required for the current raw data block
+ */
+int adtsWrite_GetHeaderBits( HANDLE_ADTS hAdts );
+
+/**
+ * \brief Write an ADTS header into the given bitstream. May not write a header
+ * in case of multiple raw data blocks.
+ *
+ * \param hAdts ADTS data handle
+ * \param hBitStream bitstream handle into which the ADTS may be written into
+ * \param buffer_fullness the buffer fullness value for the ADTS header
+ * \param the current raw data block length
+ *
+ * \return 0 in case of success.
+ */
+INT adtsWrite_EncodeHeader(
+ HANDLE_ADTS hAdts,
+ HANDLE_FDK_BITSTREAM hBitStream,
+ int bufferFullness,
+ int frame_length
+ );
+/**
+ * \brief Finish a ADTS raw data block
+ *
+ * \param hAdts ADTS data handle
+ * \param hBs bitstream handle into which the ADTS may be written into
+ * \param pBits a pointer to a integer holding the current bitstream buffer bit count,
+ * which is corrected to the current raw data block boundary.
+ *
+ */
+void adtsWrite_EndRawDataBlock(
+ HANDLE_ADTS hAdts,
+ HANDLE_FDK_BITSTREAM hBs,
+ int *bits
+ );
+
+
+/**
+ * \brief Start CRC region with a maximum number of bits
+ * If mBits is positive zero padding will be used for CRC calculation, if there
+ * are less than mBits bits available.
+ * If mBits is negative no zero padding is done.
+ * If mBits is zero the memory for the buffer is allocated dynamically, the
+ * number of bits is not limited.
+ *
+ * \param pAdts ADTS data handle
+ * \param hBs bitstream handle of which the CRC region ends
+ * \param mBits limit of number of bits to be considered for the requested CRC region
+ *
+ * \return ID for the created region, -1 in case of an error
+ */
+int adtsWrite_CrcStartReg(
+ HANDLE_ADTS pAdts,
+ HANDLE_FDK_BITSTREAM hBs,
+ int mBits
+ );
+
+/**
+ * \brief Ends CRC region identified by reg
+ *
+ * \param pAdts ADTS data handle
+ * \param hBs bitstream handle of which the CRC region ends
+ * \param reg a CRC region ID returned previously by adtsWrite_CrcStartReg()
+ */
+void adtsWrite_CrcEndReg(
+ HANDLE_ADTS pAdts,
+ HANDLE_FDK_BITSTREAM hBs,
+ int reg
+ );
+
+
+
+
+#endif /* TPENC_ADTS_H */
+
diff --git a/libMpegTPEnc/src/tpenc_asc.cpp b/libMpegTPEnc/src/tpenc_asc.cpp
new file mode 100644
index 0000000..674ffe2
--- /dev/null
+++ b/libMpegTPEnc/src/tpenc_asc.cpp
@@ -0,0 +1,503 @@
+/***************************** MPEG-4 AAC Encoder **************************
+
+ (C) Copyright Fraunhofer IIS (2005)
+ All Rights Reserved
+
+ Please be advised that this software and/or program delivery is
+ Confidential Information of Fraunhofer and subject to and covered by the
+
+ Fraunhofer IIS Software Evaluation Agreement
+ between Google Inc. and Fraunhofer
+ effective and in full force since March 1, 2012.
+
+ You may use this software and/or program only under the terms and
+ conditions described in the above mentioned Fraunhofer IIS Software
+ Evaluation Agreement. Any other and/or further use requires a separate agreement.
+
+
+ $Id$
+ Author(s):
+ Description:
+
+ This software and/or program is protected by copyright law and international
+ treaties. Any reproduction or distribution of this software and/or program,
+ or any portion of it, may result in severe civil and criminal penalties, and
+ will be prosecuted to the maximum extent possible under law.
+
+******************************************************************************/
+
+#include "tp_data.h"
+
+#include "tpenc_lib.h"
+#include "tpenc_asc.h"
+#include "FDK_bitstream.h"
+#include "genericStds.h"
+
+#define ASC_FLAG_EXT 0x0001
+#define ASC_FLAG_SBR 0x0002
+#define ASC_FLAG_SBRCRC 0x0004
+#define ASC_FLAG_VCB11 0x0010
+#define ASC_FLAG_RVLC 0x0020
+#define ASC_FLAG_HCR 0x0040
+#define ASC_FLAG_HCR 0x0040
+
+
+#define PCE_MAX_ELEMENTS 8
+
+/**
+ * Describe a PCE based on placed channel elements and element type sequence.
+ */
+typedef struct {
+
+ UCHAR num_front_channel_elements; /*!< Number of front channel elements. */
+ UCHAR num_side_channel_elements; /*!< Number of side channel elements. */
+ UCHAR num_back_channel_elements; /*!< Number of back channel elements. */
+ UCHAR num_lfe_channel_elements; /*!< Number of lfe channel elements. */
+ MP4_ELEMENT_ID el_list[PCE_MAX_ELEMENTS];/*!< List contains sequence describing the elements
+ in present channel mode. (MPEG order) */
+} PCE_CONFIGURATION;
+
+
+/**
+ * Map an incoming channel mode to a existing PCE configuration entry.
+ */
+typedef struct {
+
+ CHANNEL_MODE channel_mode; /*!< Present channel mode. */
+ PCE_CONFIGURATION pce_configuration; /*!< Program config element description. */
+
+} CHANNEL_CONFIGURATION;
+
+
+/**
+ * \brief Table contains all supported channel modes and according PCE configuration description.
+ *
+ * The number of channel element parameter describes the kind of consecutively elements.
+ * E.g. MODE_1_2_2_2_1 means:
+ * - First 2 elements (SCE,CPE) are front channel elements.
+ * - Following element (CPE) is a side channel element.
+ * - Next element (CPE) is a back channel element.
+ * - Last element (LFE) is a lfe channel element.
+ */
+static const CHANNEL_CONFIGURATION pceConfigTab[] =
+{
+ { MODE_1, { 1, 0, 0, 0, { ID_SCE, ID_NONE, ID_NONE, ID_NONE, ID_NONE, ID_NONE, ID_NONE, ID_NONE } } },
+ { MODE_2, { 1, 0, 0, 0, { ID_CPE, ID_NONE, ID_NONE, ID_NONE, ID_NONE, ID_NONE, ID_NONE, ID_NONE } } },
+ { MODE_1_2, { 2, 0, 0, 0, { ID_SCE, ID_CPE, ID_NONE, ID_NONE, ID_NONE, ID_NONE, ID_NONE, ID_NONE } } },
+ { MODE_1_2_1, { 2, 0, 1, 0, { ID_SCE, ID_CPE, ID_SCE, ID_NONE, ID_NONE, ID_NONE, ID_NONE, ID_NONE } } },
+ { MODE_1_2_2, { 2, 0, 1, 0, { ID_SCE, ID_CPE, ID_CPE, ID_NONE, ID_NONE, ID_NONE, ID_NONE, ID_NONE } } },
+ { MODE_1_2_2_1, { 2, 0, 1, 1, { ID_SCE, ID_CPE, ID_CPE, ID_LFE, ID_NONE, ID_NONE, ID_NONE, ID_NONE } } },
+ { MODE_1_2_2_2_1, { 2, 1, 1, 1, { ID_SCE, ID_CPE, ID_CPE, ID_CPE, ID_LFE, ID_NONE, ID_NONE, ID_NONE } } },
+
+ { MODE_1_1, { 2, 0, 0, 0, { ID_SCE, ID_SCE, ID_NONE, ID_NONE, ID_NONE, ID_NONE, ID_NONE, ID_NONE } } },
+ { MODE_1_1_1_1, { 2, 2, 0, 0, { ID_SCE, ID_SCE, ID_SCE, ID_SCE, ID_NONE, ID_NONE, ID_NONE, ID_NONE } } },
+ { MODE_1_1_1_1_1_1, { 2, 2, 2, 0, { ID_SCE, ID_SCE, ID_SCE, ID_SCE, ID_SCE, ID_SCE, ID_NONE, ID_NONE } } },
+ { MODE_1_1_1_1_1_1_1_1, { 3, 2, 3, 0, { ID_SCE, ID_SCE, ID_SCE, ID_SCE, ID_SCE, ID_SCE, ID_SCE, ID_SCE } } },
+
+ { MODE_2_2, { 1, 0, 1, 0, { ID_CPE, ID_CPE, ID_NONE, ID_NONE, ID_NONE, ID_NONE, ID_NONE, ID_NONE } } },
+ { MODE_2_2_2, { 1, 1, 1, 0, { ID_CPE, ID_CPE, ID_CPE, ID_NONE, ID_NONE, ID_NONE, ID_NONE, ID_NONE } } },
+ { MODE_2_2_2_2, { 4, 0, 0, 0, { ID_CPE, ID_CPE, ID_CPE, ID_CPE, ID_NONE, ID_NONE, ID_NONE, ID_NONE } } },
+
+ { MODE_2_1, { 1, 0, 1, 0, { ID_CPE, ID_SCE, ID_NONE, ID_NONE, ID_NONE, ID_NONE, ID_NONE, ID_NONE } } }
+};
+
+
+/**
+ * \brief Get program config element description for existing channel mode.
+ *
+ * \param channel_mode Current channel mode.
+ *
+ * \return
+ * - Pointer to PCE_CONFIGURATION entry, on success.
+ * - NULL, on failure.
+ */
+static const PCE_CONFIGURATION* getPceEntry(
+ const CHANNEL_MODE channel_mode
+ )
+{
+ UINT i;
+ const PCE_CONFIGURATION *pce_config = NULL;
+
+ for (i=0; i < (sizeof(pceConfigTab)/sizeof(CHANNEL_CONFIGURATION)); i++) {
+ if (pceConfigTab[i].channel_mode == channel_mode) {
+ pce_config = &pceConfigTab[i].pce_configuration;
+ }
+ }
+
+ return pce_config;
+}
+
+int getChannelConfig( CHANNEL_MODE channel_mode )
+{
+ INT chan_config = 0;
+
+ switch(channel_mode) {
+ case MODE_1: chan_config = 1; break;
+ case MODE_2: chan_config = 2; break;
+ case MODE_1_2: chan_config = 3; break;
+ case MODE_1_2_1: chan_config = 4; break;
+ case MODE_1_2_2: chan_config = 5; break;
+ case MODE_1_2_2_1: chan_config = 6; break;
+ case MODE_1_2_2_2_1: chan_config = 7; break;
+
+ default: chan_config = 0;
+ }
+
+ return chan_config;
+}
+
+CHANNEL_MODE transportEnc_GetChannelMode( int noChannels )
+{
+ CHANNEL_MODE chMode;
+
+ if (noChannels <= 8 && noChannels > 0)
+ chMode = (CHANNEL_MODE)((noChannels == 8) ? 7 : noChannels); /* see : iso/mpeg4 v1 audio subpart1*/
+ else
+ chMode = MODE_UNKNOWN;
+
+ return chMode;
+}
+
+#ifdef TP_PCE_ENABLE
+int transportEnc_writePCE(HANDLE_FDK_BITSTREAM hBs,
+ CHANNEL_MODE channelMode,
+ INT sampleRate,
+ int instanceTagPCE,
+ int profile,
+ int matrixMixdownA,
+ int pseudoSurroundEnable,
+ UINT alignAnchor)
+{
+ int sampleRateIndex, i;
+ const PCE_CONFIGURATION* config = NULL;
+ const MP4_ELEMENT_ID* pEl_list = NULL;
+ UCHAR cpeCnt=0, sceCnt=0, lfeCnt=0;
+
+ sampleRateIndex = getSamplingRateIndex(sampleRate);
+ if (sampleRateIndex == 15) {
+ return -1;
+ }
+
+ if ((config=getPceEntry(channelMode))==NULL) {
+ return -1;
+ }
+
+ /* Pointer to first element in element list. */
+ pEl_list = &config->el_list[0];
+
+ FDKwriteBits(hBs, instanceTagPCE, 4); /* Element instance tag */
+ FDKwriteBits(hBs, profile, 2); /* Object type */
+ FDKwriteBits(hBs, sampleRateIndex, 4); /* Sample rate index*/
+
+ FDKwriteBits(hBs, config->num_front_channel_elements, 4); /* Front channel Elements */
+ FDKwriteBits(hBs, config->num_side_channel_elements , 4); /* No Side Channel Elements */
+ FDKwriteBits(hBs, config->num_back_channel_elements , 4); /* No Back channel Elements */
+ FDKwriteBits(hBs, config->num_lfe_channel_elements , 2); /* No Lfe channel elements */
+
+ FDKwriteBits(hBs, 0, 3); /* No assoc data elements */
+ FDKwriteBits(hBs, 0, 4); /* No valid cc elements */
+ FDKwriteBits(hBs, 0, 1); /* Mono mixdown present */
+ FDKwriteBits(hBs, 0, 1); /* Stereo mixdown present */
+
+ if ( matrixMixdownA!=0 && ((channelMode==MODE_1_2_2)||(channelMode==MODE_1_2_2_1)) ) {
+ FDKwriteBits(hBs, 1, 1); /* Matrix mixdown present */
+ FDKwriteBits(hBs, (matrixMixdownA-1)&0x3, 2); /* matrix_mixdown_idx */
+ FDKwriteBits(hBs, pseudoSurroundEnable&0x1, 1); /* pseudo_surround_enable */
+ }
+ else {
+ FDKwriteBits(hBs, 0, 1); /* Matrix mixdown not present */
+ }
+
+ for(i=0; i<config->num_front_channel_elements; i++) {
+ UCHAR isCpe = (*pEl_list++==ID_CPE) ? 1 : 0;
+ UCHAR tag = (isCpe) ? cpeCnt++ : sceCnt++;
+ FDKwriteBits(hBs, isCpe, 1); /* Front channel Elements is CPE? */
+ FDKwriteBits(hBs, tag, 4); /* Front channel Instance Tag.*/
+ }
+ for(i=0; i<config->num_side_channel_elements; i++) {
+ UCHAR isCpe = (*pEl_list++==ID_CPE) ? 1 : 0;
+ UCHAR tag = (isCpe) ? cpeCnt++ : sceCnt++;
+ FDKwriteBits(hBs, isCpe, 1); /* Front channel Elements is CPE? */
+ FDKwriteBits(hBs, tag, 4); /* Front channel Instance Tag.*/
+ }
+ for(i=0; i<config->num_back_channel_elements; i++) {
+ UCHAR isCpe = (*pEl_list++==ID_CPE) ? 1 : 0;
+ UCHAR tag = (isCpe) ? cpeCnt++ : sceCnt++;
+ FDKwriteBits(hBs, isCpe, 1); /* Front channel Elements is CPE? */
+ FDKwriteBits(hBs, tag, 4); /* Front channel Instance Tag.*/
+ }
+ for(i=0; i<config->num_lfe_channel_elements; i++) {
+ FDKwriteBits(hBs, lfeCnt++, 4); /* LFE channel Instance Tag. */
+ }
+
+ /* - num_valid_cc_elements always 0.
+ - num_assoc_data_elements always 0. */
+
+ /* Byte alignment: relative to alignAnchor
+ ADTS: align with respect to the first bit of the raw_data_block()
+ ADIF: align with respect to the first bit of the header
+ LATM: align with respect to the first bit of the ASC */
+ FDKbyteAlign(hBs, alignAnchor); /* Alignment */
+
+ FDKwriteBits(hBs, 0 ,8); /* Do no write any comment. */
+
+ /* - comment_field_bytes always 0. */
+
+ return 0;
+}
+
+int transportEnc_GetPCEBits(CHANNEL_MODE channelMode,
+ int matrixMixdownA,
+ int bits)
+{
+ const PCE_CONFIGURATION* config = NULL;
+
+ if ((config=getPceEntry(channelMode))==NULL) {
+ return -1; /* unsupported channelmapping */
+ }
+
+ bits += 4 + 2 + 4; /* Element instance tag + Object type + Sample rate index */
+ bits += 4 + 4 + 4 + 2; /* No (front + side + back + lfe channel) elements */
+ bits += 3 + 4; /* No (assoc data + valid cc) elements */
+ bits += 1 + 1 + 1 ; /* Mono + Stereo + Matrix mixdown present */
+
+ if ( matrixMixdownA!=0 && ((channelMode==MODE_1_2_2)||(channelMode==MODE_1_2_2_1)) ) {
+ bits +=3; /* matrix_mixdown_idx + pseudo_surround_enable */
+ }
+
+ bits += (1+4) * (INT)config->num_front_channel_elements;
+ bits += (1+4) * (INT)config->num_side_channel_elements;
+ bits += (1+4) * (INT)config->num_back_channel_elements;
+ bits += (4) * (INT)config->num_lfe_channel_elements;
+
+ /* - num_valid_cc_elements always 0.
+ - num_assoc_data_elements always 0. */
+
+ if ((bits%8) != 0) {
+ bits += (8 - (bits%8)); /* Alignment */
+ }
+
+ bits += 8; /* Comment field bytes */
+
+ /* - comment_field_bytes alwys 0. */
+
+ return bits;
+}
+#endif /* TP_PCE_ENABLE */
+
+static void writeAot(HANDLE_FDK_BITSTREAM hBitstreamBuffer, AUDIO_OBJECT_TYPE aot)
+{
+ int tmp = (int) aot;
+
+ if (tmp > 31) {
+ FDKwriteBits( hBitstreamBuffer, AOT_ESCAPE, 5 );
+ FDKwriteBits( hBitstreamBuffer, tmp-32, 6 ); /* AudioObjectType */
+ } else {
+ FDKwriteBits( hBitstreamBuffer, tmp, 5 );
+ }
+}
+
+static void writeSampleRate(HANDLE_FDK_BITSTREAM hBitstreamBuffer, int sampleRate)
+{
+ int sampleRateIndex = getSamplingRateIndex(sampleRate);
+
+ FDKwriteBits( hBitstreamBuffer, sampleRateIndex, 4 );
+ if( sampleRateIndex == 15 ) {
+ FDKwriteBits( hBitstreamBuffer, sampleRate, 24 );
+ }
+}
+
+#ifdef TP_GA_ENABLE
+static
+int transportEnc_writeGASpecificConfig(
+ HANDLE_FDK_BITSTREAM asc,
+ CODER_CONFIG *config,
+ int extFlg,
+ UINT alignAnchor
+ )
+{
+ int aot = config->aot;
+ int samplesPerFrame = config->samplesPerFrame;
+
+ /* start of GASpecificConfig according to ISO/IEC 14496-3 Subpart 4, 4.4.1 */
+ FDKwriteBits( asc, ((samplesPerFrame==960 || samplesPerFrame==480)?1:0), 1); /* frameLengthFlag: 1 for a 960/480 (I)MDCT, 0 for a 1024/512 (I)MDCT*/
+ FDKwriteBits( asc, 0, 1); /* dependsOnCoreCoder: Sampling Rate Coder Specific, see in ISO/IEC 14496-3 Subpart 4, 4.4.1 */
+ FDKwriteBits( asc, extFlg, 1 ); /* Extension Flag: Shall be 1 for aot = 17,19,20,21,22,23 */
+
+ /* Write PCE if channel config is not 1-7 */
+ if (getChannelConfig(config->channelMode) == 0) {
+ transportEnc_writePCE(asc, config->channelMode, config->samplingRate, 0, 1, 0, 0, alignAnchor);
+ }
+ if (extFlg) {
+ if (aot == AOT_ER_BSAC) {
+ FDKwriteBits( asc, config->BSACnumOfSubFrame, 5 ); /* numOfSubFrame */
+ FDKwriteBits( asc, config->BSAClayerLength, 11 ); /* layer_length */
+ }
+ if ((aot == AOT_ER_AAC_LC) || (aot == AOT_ER_AAC_LTP) ||
+ (aot == AOT_ER_AAC_SCAL) || (aot == AOT_ER_AAC_LD))
+ {
+ FDKwriteBits( asc, (config->flags & CC_VCB11) ? 1 : 0, 1 ); /* aacSectionDataResillienceFlag */
+ FDKwriteBits( asc, (config->flags & CC_RVLC) ? 1 : 0, 1 ); /* aacScaleFactorDataResillienceFlag */
+ FDKwriteBits( asc, (config->flags & CC_HCR) ? 1 : 0, 1 ); /* aacSpectralDataResillienceFlag */
+ }
+ FDKwriteBits( asc, 0, 1 ); /* extensionFlag3: reserved. Shall be '0' */
+ }
+ return 0;
+}
+#endif /* TP_GA_ENABLE */
+
+#ifdef TP_ELD_ENABLE
+
+static
+int transportEnc_writeELDSpecificConfig(
+ HANDLE_FDK_BITSTREAM hBs,
+ CODER_CONFIG *config,
+ int epConfig,
+ int flags,
+ CSTpCallBacks *cb
+ )
+{
+ /* ELD specific config */
+ if (config->channelMode == MODE_1_1) {
+ return -1;
+ }
+ FDKwriteBits(hBs, (config->samplesPerFrame == 480) ? 1 : 0, 1);
+
+ FDKwriteBits(hBs, (flags & ASC_FLAG_VCB11) ? 1:0, 1);
+ FDKwriteBits(hBs, (flags & ASC_FLAG_RVLC ) ? 1:0, 1);
+ FDKwriteBits(hBs, (flags & ASC_FLAG_HCR ) ? 1:0, 1);
+
+ FDKwriteBits(hBs, (flags & ASC_FLAG_SBR) ? 1:0, 1); /* SBR header flag */
+ if ( (flags & ASC_FLAG_SBR) ) {
+ FDKwriteBits(hBs, (config->samplingRate == config->extSamplingRate) ? 0:1, 1); /* Samplerate Flag */
+ FDKwriteBits(hBs, (flags &ASC_FLAG_SBRCRC) ? 1:0, 1); /* SBR CRC flag*/
+
+ if (cb->cbSbr != NULL) {
+ const PCE_CONFIGURATION *pPce;
+ int e;
+
+ pPce = getPceEntry(config->channelMode);
+
+ for (e=0; e<PCE_MAX_ELEMENTS && pPce->el_list[e] != ID_NONE; e++ ) {
+ if ( (pPce->el_list[e] == ID_SCE) || (pPce->el_list[e] == ID_CPE) ) {
+ cb->cbSbr(cb->cbSbrData, hBs, 0, 0, 0, config->aot, pPce->el_list[e], e);
+ }
+ }
+ }
+ }
+
+ FDKwriteBits(hBs, 0, 4); /* ELDEXT_TERM */
+
+ return 0;
+}
+#endif /* TP_ELD_ENABLE */
+
+
+int transportEnc_writeASC (
+ HANDLE_FDK_BITSTREAM asc,
+ CODER_CONFIG *config,
+ CSTpCallBacks *cb
+ )
+{
+ UINT flags = 0;
+ int err;
+ int epConfig = 0;
+
+ /* Required for the PCE. */
+ UINT alignAnchor = FDKgetValidBits(asc);
+
+ /* Extension Flag: Shall be 1 for aot = 17,19,20,21,22,23,39 */
+ switch (config->aot) {
+ case AOT_ER_AAC_LC:
+ case AOT_ER_AAC_LTP:
+ case AOT_ER_AAC_SCAL:
+ case AOT_ER_TWIN_VQ:
+ case AOT_ER_BSAC:
+ case AOT_ER_AAC_LD:
+ case AOT_ER_AAC_ELD:
+ case AOT_USAC:
+ flags |= ASC_FLAG_EXT;
+ break;
+ default:
+ break;
+ }
+ if (config->flags & CC_SBR) {
+ flags |= ASC_FLAG_SBR;
+ }
+
+ if (config->extAOT == AOT_SBR || config->extAOT == AOT_PS)
+ writeAot(asc, config->extAOT);
+ else
+ writeAot(asc, config->aot);
+
+ {
+ writeSampleRate(asc, config->samplingRate);
+ }
+
+ /* Try to guess a reasonable channel mode if not given */
+ if (config->channelMode == MODE_INVALID) {
+ config->channelMode = transportEnc_GetChannelMode(config->noChannels);
+ if (config->channelMode == MODE_INVALID)
+ return -1;
+ }
+
+ FDKwriteBits( asc, getChannelConfig(config->channelMode), 4 );
+
+ if (config->extAOT == AOT_SBR || config->extAOT == AOT_PS) {
+ writeSampleRate(asc, config->extSamplingRate);
+ writeAot(asc, config->aot);
+ }
+
+ switch (config->aot) {
+#ifdef TP_GA_ENABLE
+ case AOT_AAC_MAIN:
+ case AOT_AAC_LC:
+ case AOT_AAC_SSR:
+ case AOT_AAC_LTP:
+ case AOT_AAC_SCAL:
+ case AOT_TWIN_VQ:
+ case AOT_ER_AAC_LC:
+ case AOT_ER_AAC_LTP:
+ case AOT_ER_AAC_SCAL:
+ case AOT_ER_TWIN_VQ:
+ case AOT_ER_BSAC:
+ case AOT_ER_AAC_LD:
+ err = transportEnc_writeGASpecificConfig(asc, config, (flags & ASC_FLAG_EXT) ? 1:0, alignAnchor);
+ if (err)
+ return err;
+ break;
+#endif /* TP_GA_ENABLE */
+#ifdef TP_ELD_ENABLE
+ case AOT_ER_AAC_ELD:
+ err = transportEnc_writeELDSpecificConfig(asc, config, epConfig, flags, cb);
+ if (err)
+ return err;
+ break;
+#endif /* TP_ELD_ENABLE */
+ default:
+ return -1;
+ }
+
+ switch (config->aot) {
+ case AOT_ER_AAC_LC:
+ case AOT_ER_AAC_LTP:
+ case AOT_ER_AAC_SCAL:
+ case AOT_ER_TWIN_VQ:
+ case AOT_ER_BSAC:
+ case AOT_ER_AAC_LD:
+ case AOT_ER_CELP:
+ case AOT_ER_HVXC:
+ case AOT_ER_HILN:
+ case AOT_ER_PARA:
+ case AOT_ER_AAC_ELD:
+ FDKwriteBits( asc, 0, 2 ); /* epconfig 0 */
+ break;
+ default:
+ break;
+ }
+
+ /* Make sure all bits are sync'ed */
+ FDKsyncCache( asc );
+
+ return 0;
+}
diff --git a/libMpegTPEnc/src/tpenc_asc.h b/libMpegTPEnc/src/tpenc_asc.h
new file mode 100644
index 0000000..fbc444b
--- /dev/null
+++ b/libMpegTPEnc/src/tpenc_asc.h
@@ -0,0 +1,80 @@
+/***************************** MPEG-4 AAC Encoder **************************
+
+ (C) Copyright Fraunhofer IIS (2005)
+ All Rights Reserved
+
+ Please be advised that this software and/or program delivery is
+ Confidential Information of Fraunhofer and subject to and covered by the
+
+ Fraunhofer IIS Software Evaluation Agreement
+ between Google Inc. and Fraunhofer
+ effective and in full force since March 1, 2012.
+
+ You may use this software and/or program only under the terms and
+ conditions described in the above mentioned Fraunhofer IIS Software
+ Evaluation Agreement. Any other and/or further use requires a separate agreement.
+
+
+ $Id$
+ Author(s): Manuel Jander
+ Description: Audio Specific Config writer
+
+ This software and/or program is protected by copyright law and international
+ treaties. Any reproduction or distribution of this software and/or program,
+ or any portion of it, may result in severe civil and criminal penalties, and
+ will be prosecuted to the maximum extent possible under law.
+
+******************************************************************************/
+
+#ifndef TPENC_ASC_H
+#define TPENC_ASC_H
+
+/**
+ * \brief Get channel config from channel mode.
+ *
+ * \param channel_mode channel mode
+ *
+ * \return chanel config
+ */
+int getChannelConfig( CHANNEL_MODE channel_mode );
+
+/**
+ * \brief Write a Program Config Element.
+ *
+ * \param hBs bitstream handle into which the PCE is appended
+ * \param channelMode the channel mode to be used
+ * \param sampleRate the sample rate
+ * \param instanceTagPCE the instance tag of the Program Config Element
+ * \param profile the MPEG Audio profile to be used
+ * \param matrix mixdown gain
+ * \param pseudo surround indication
+ * \param reference bitstream position for alignment
+ * \return zero on success, non-zero on failure.
+ */
+int transportEnc_writePCE(
+ HANDLE_FDK_BITSTREAM hBs,
+ CHANNEL_MODE channelMode,
+ INT sampleRate,
+ int instanceTagPCE,
+ int profile,
+ int matrixMixdownA,
+ int pseudoSurroundEnable,
+ UINT alignAnchor
+ );
+
+/**
+ * \brief Get the bit count required by a Program Config Element
+ *
+ * \param channelMode the channel mode to be used
+ * \param matrix mixdown gain
+ * \param bit offset at which the PCE would start
+ * \return the amount of bits required for the PCE including the given bit offset.
+ */
+int transportEnc_GetPCEBits(
+ CHANNEL_MODE channelMode,
+ int matrixMixdownA,
+ int bits
+ );
+
+#endif /* TPENC_ASC_H */
+
diff --git a/libMpegTPEnc/src/tpenc_latm.cpp b/libMpegTPEnc/src/tpenc_latm.cpp
new file mode 100644
index 0000000..a26d0da
--- /dev/null
+++ b/libMpegTPEnc/src/tpenc_latm.cpp
@@ -0,0 +1,820 @@
+/***************************** MPEG-4 AAC Encoder **************************
+
+ (C) Copyright Fraunhofer IIS (2001)
+ All Rights Reserved
+
+ Please be advised that this software and/or program delivery is
+ Confidential Information of Fraunhofer and subject to and covered by the
+
+ Fraunhofer IIS Software Evaluation Agreement
+ between Google Inc. and Fraunhofer
+ effective and in full force since March 1, 2012.
+
+ You may use this software and/or program only under the terms and
+ conditions described in the above mentioned Fraunhofer IIS Software
+ Evaluation Agreement. Any other and/or further use requires a separate agreement.
+
+
+ $Id$
+ Author(s):
+ Description:
+
+ This software and/or program is protected by copyright law and international
+ treaties. Any reproduction or distribution of this software and/or program,
+ or any portion of it, may result in severe civil and criminal penalties, and
+ will be prosecuted to the maximum extent possible under law.
+
+******************************************************************************/
+
+#include "tpenc_latm.h"
+
+
+#include "genericStds.h"
+
+static const short celpFrameLengthTable[64] = {
+ 154, 170, 186, 147, 156, 165, 114, 120,
+ 186, 126, 132, 138, 142, 146, 154, 166,
+ 174, 182, 190, 198, 206, 210, 214, 110,
+ 114, 118, 120, 122, 218, 230, 242, 254,
+ 266, 278, 286, 294, 318, 342, 358, 374,
+ 390, 406, 422, 136, 142, 148, 154, 160,
+ 166, 170, 174, 186, 198, 206, 214, 222,
+ 230, 238, 216, 160, 280, 338, 0, 0
+};
+
+/*******
+ write value to transport stream
+ first two bits define the size of the value itself
+ then the value itself, with a size of 0-3 bytes
+*******/
+static
+UINT transportEnc_LatmWriteValue(HANDLE_FDK_BITSTREAM hBs, int value)
+{
+ UCHAR valueBytes = 4;
+ unsigned int bitsWritten = 0;
+ int i;
+
+ if ( value < (1<<8) ) {
+ valueBytes = 1;
+ } else if ( value < (1<<16) ) {
+ valueBytes = 2;
+ } else if ( value < (1<<24) ) {
+ valueBytes = 3;
+ } else {
+ valueBytes = 4;
+ }
+
+ FDKwriteBits(hBs, valueBytes-1, 2 ); /* size of value in Bytes */
+ for (i=0; i<valueBytes; i++) {
+ /* write most significant Byte first */
+ FDKwriteBits(hBs, (UCHAR)(value>>((valueBytes-1-i)<<3)), 8);
+ }
+
+ bitsWritten = (valueBytes<<3)+2;
+
+ return bitsWritten;
+}
+
+static
+UINT transportEnc_LatmCountFixBitDemandHeader ( HANDLE_LATM_STREAM hAss )
+{
+ int bitDemand = 0;
+ int insertSetupData = 0 ;
+
+ /* only if start of new latm frame */
+ if (hAss->subFrameCnt==0)
+ {
+ /* AudioSyncStream */
+
+ if (hAss->tt == TT_MP4_LOAS) {
+ bitDemand += 11 ; /* syncword */
+ bitDemand += 13 ; /* audioMuxLengthBytes */
+ }
+
+ /* AudioMuxElement*/
+
+ /* AudioMuxElement::Stream Mux Config */
+ if (hAss->muxConfigPeriod > 0) {
+ insertSetupData = (hAss->latmFrameCounter == 0);
+ } else {
+ insertSetupData = 0;
+ }
+
+ if (hAss->tt != TT_MP4_LATM_MCP0) {
+ /* AudioMuxElement::useSameStreamMux Flag */
+ bitDemand+=1;
+
+ if( insertSetupData ) {
+ bitDemand += hAss->streamMuxConfigBits;
+ }
+ }
+
+ /* AudioMuxElement::otherDataBits */
+ bitDemand += 8*hAss->otherDataLenBytes;
+
+ /* AudioMuxElement::ByteAlign */
+ if ( bitDemand % 8 ) {
+ hAss->fillBits = 8 - (bitDemand % 8);
+ bitDemand += hAss->fillBits ;
+ } else {
+ hAss->fillBits = 0;
+ }
+ }
+
+ return bitDemand ;
+}
+
+static
+UINT transportEnc_LatmCountVarBitDemandHeader ( HANDLE_LATM_STREAM hAss , unsigned int streamDataLength )
+{
+ int bitDemand = 0;
+ int prog, layer;
+
+ /* Payload Length Info*/
+ if( hAss->allStreamsSameTimeFraming ) {
+ for( prog=0; prog<hAss->noProgram; prog++ ) {
+ for( layer=0; layer<LATM_MAX_LAYERS; layer++ ) {
+ LATM_LAYER_INFO *p_linfo = &(hAss->m_linfo[prog][layer]);
+
+ if( p_linfo->streamID >= 0 ) {
+ switch( p_linfo->frameLengthType ) {
+ case 0:
+ if ( streamDataLength > 0 ) {
+ streamDataLength -= bitDemand ;
+ while( streamDataLength >= (255<<3) ) {
+ bitDemand+=8;
+ streamDataLength -= (255<<3);
+ }
+ bitDemand += 8;
+ }
+ break;
+
+ case 1:
+ case 4:
+ case 6:
+ bitDemand += 2;
+ break;
+
+ default:
+ return 0;
+ }
+ }
+ }
+ }
+ } else {
+ /* there are many possibilities to use this mechanism. */
+ switch( hAss->varMode ) {
+ case LATMVAR_SIMPLE_SEQUENCE: {
+ /* Use the sequence generated by the encoder */
+ //int streamCntPosition = transportEnc_SetWritePointer( hAss->hAssemble, 0 );
+ //int streamCntPosition = FDKgetValidBits( hAss->hAssemble );
+ bitDemand+=4;
+
+ hAss->varStreamCnt = 0;
+ for( prog=0; prog<hAss->noProgram; prog++ ) {
+ for( layer=0; layer<LATM_MAX_LAYERS; layer++ ) {
+ LATM_LAYER_INFO *p_linfo = &(hAss->m_linfo[prog][layer]);
+
+ if( p_linfo->streamID >= 0 ) {
+
+ bitDemand+=4; /* streamID */
+ switch( p_linfo->frameLengthType ) {
+ case 0:
+ streamDataLength -= bitDemand ;
+ while( streamDataLength >= (255<<3) ) {
+ bitDemand+=8;
+ streamDataLength -= (255<<3);
+ }
+
+ bitDemand += 8;
+ break;
+ /*bitDemand += 1; endFlag
+ break;*/
+
+ case 1:
+ case 4:
+ case 6:
+
+ break;
+
+ default:
+ return 0;
+ }
+ hAss->varStreamCnt++;
+ }
+ }
+ }
+ bitDemand+=4;
+ //transportEnc_UpdateBitstreamField( hAss->hAssemble, streamCntPosition, hAss->varStreamCnt-1, 4 );
+ //UINT pos = streamCntPosition-FDKgetValidBits(hAss->hAssemble);
+ //FDKpushBack( hAss->hAssemble, pos);
+ //FDKwriteBits( hAss->hAssemble, hAss->varStreamCnt-1, 4);
+ //FDKpushFor( hAss->hAssemble, pos-4);
+ }
+ break;
+
+ default:
+ return 0;
+ }
+ }
+
+ return bitDemand ;
+}
+
+TRANSPORTENC_ERROR
+CreateStreamMuxConfig(
+ HANDLE_LATM_STREAM hAss,
+ HANDLE_FDK_BITSTREAM hBs,
+ int bufferFullness,
+ CSTpCallBacks *cb
+ )
+{
+ INT streamIDcnt, tmp;
+ int layer, prog;
+
+ USHORT coreFrameOffset=0;
+
+ hAss->audioMuxVersionA = 0; /* for future extensions */
+ hAss->streamMuxConfigBits = 0;
+
+ FDKwriteBits( hBs, hAss->audioMuxVersion, 1 ); /* audioMuxVersion */
+ hAss->streamMuxConfigBits += 1;
+
+ if ( hAss->audioMuxVersion == 1 ) {
+ FDKwriteBits( hBs, hAss->audioMuxVersionA, 1 ); /* audioMuxVersionA */
+ hAss->streamMuxConfigBits+=1;
+ }
+
+ if ( hAss->audioMuxVersionA == 0 )
+ {
+ if ( hAss->audioMuxVersion == 1 ) {
+ hAss->streamMuxConfigBits+= transportEnc_LatmWriteValue( hBs, hAss->taraBufferFullness );/* taraBufferFullness */
+ }
+ FDKwriteBits( hBs, hAss->allStreamsSameTimeFraming ? 1:0, 1 ); /* allStreamsSameTimeFraming */
+ FDKwriteBits( hBs, hAss->noSubframes-1, 6 ); /* Number of Subframes */
+ FDKwriteBits( hBs, hAss->noProgram-1, 4 ); /* Number of Programs */
+
+ hAss->streamMuxConfigBits+=11;
+
+ streamIDcnt = 0;
+ for( prog=0; prog<hAss->noProgram; prog++ ) {
+ int transLayer = 0;
+
+ FDKwriteBits( hBs, hAss->noLayer[prog]-1, 3 );
+ hAss->streamMuxConfigBits+=3;
+
+ for( layer=0; layer<LATM_MAX_LAYERS; layer++ ) {
+ LATM_LAYER_INFO *p_linfo = &(hAss->m_linfo[prog][layer]);
+ CODER_CONFIG *p_lci = hAss->config[prog][layer];
+
+ p_linfo->streamID = -1;
+
+ if( hAss->config[prog][layer] != NULL ) {
+ int useSameConfig = 0;
+
+ if( transLayer > 0 ) {
+ FDKwriteBits( hBs, useSameConfig ? 1 : 0, 1 );
+ hAss->streamMuxConfigBits+=1;
+ }
+ if( (useSameConfig == 0) || (transLayer==0) ) {
+ UINT bits;
+
+ if ( hAss->audioMuxVersion == 1 ) {
+ FDKpushFor(hBs, 2); /* align to ASC, even if we do not know the length of the "ascLen" field yet */
+ }
+
+ bits = FDKgetValidBits( hBs );
+
+ transportEnc_writeASC(
+ hBs,
+ hAss->config[prog][layer],
+ cb
+ );
+
+ bits = FDKgetValidBits( hBs ) - bits;
+
+ if ( hAss->audioMuxVersion == 1 ) {
+ FDKpushBack(hBs, bits+2);
+ hAss->streamMuxConfigBits += transportEnc_LatmWriteValue( hBs, bits );
+ transportEnc_writeASC(
+ hBs,
+ hAss->config[prog][layer],
+ cb
+ );
+ }
+
+ hAss->streamMuxConfigBits += bits; /* add asc length to smc summary */
+ }
+ transLayer++;
+
+ if( !hAss->allStreamsSameTimeFraming ) {
+ if( streamIDcnt >= LATM_MAX_STREAM_ID )
+ return TRANSPORTENC_INVALID_CONFIG;
+ }
+ p_linfo->streamID = streamIDcnt++;
+
+ switch( p_lci->aot ) {
+ case AOT_AAC_MAIN :
+ case AOT_AAC_LC :
+ case AOT_AAC_SSR :
+ case AOT_AAC_LTP :
+ case AOT_AAC_SCAL :
+ case AOT_ER_AAC_LD :
+ case AOT_ER_AAC_ELD :
+ case AOT_USAC:
+ case AOT_RSVD50:
+ p_linfo->frameLengthType = 0;
+
+ FDKwriteBits( hBs, p_linfo->frameLengthType, 3 ); /* frameLengthType */
+ FDKwriteBits( hBs, bufferFullness, 8 ); /* bufferFullness */
+ hAss->streamMuxConfigBits+=11;
+
+ if ( !hAss->allStreamsSameTimeFraming ) {
+ CODER_CONFIG *p_lci_prev = hAss->config[prog][layer-1];
+ if ( ((p_lci->aot == AOT_AAC_SCAL) || (p_lci->aot == AOT_ER_AAC_SCAL)) &&
+ ((p_lci_prev->aot == AOT_CELP) || (p_lci_prev->aot == AOT_ER_CELP)) ) {
+ FDKwriteBits( hBs, coreFrameOffset, 6 ); /* coreFrameOffset */
+ hAss->streamMuxConfigBits+=6;
+ }
+ }
+ break;
+
+ case AOT_TWIN_VQ:
+ p_linfo->frameLengthType = 1;
+ tmp = ( (p_lci->bitsFrame+7) >> 3 ) - 20; /* transmission frame length in bytes */
+ if( (tmp < 0) ) {
+ return TRANSPORTENC_INVALID_TRANSMISSION_FRAME_LENGTH;
+ }
+ FDKwriteBits( hBs, p_linfo->frameLengthType, 3 ); /* frameLengthType */
+ FDKwriteBits( hBs, tmp, 9 );
+ hAss->streamMuxConfigBits+=12;
+
+ p_linfo->frameLengthBits = (tmp+20) << 3;
+ break;
+
+ case AOT_CELP:
+ p_linfo->frameLengthType = 4;
+ FDKwriteBits( hBs, p_linfo->frameLengthType, 3 ); /* frameLengthType */
+ hAss->streamMuxConfigBits+=3;
+ {
+ int i;
+ for( i=0; i<62; i++ ) {
+ if( celpFrameLengthTable[i] == p_lci->bitsFrame )
+ break;
+ }
+ if( i>=62 ) {
+ return TRANSPORTENC_INVALID_CELP_FRAME_LENGTH;
+ }
+
+ FDKwriteBits( hBs, i, 6 ); /* CELPframeLengthTabelIndex */
+ hAss->streamMuxConfigBits+=6;
+ }
+ p_linfo->frameLengthBits = p_lci->bitsFrame;
+ break;
+
+ case AOT_HVXC:
+ p_linfo->frameLengthType = 6;
+ FDKwriteBits( hBs, p_linfo->frameLengthType, 3 ); /* frameLengthType */
+ hAss->streamMuxConfigBits+=3;
+ {
+ int i;
+
+ if( p_lci->bitsFrame == 40 ) {
+ i = 0;
+ } else if( p_lci->bitsFrame == 80 ) {
+ i = 1;
+ } else {
+ return TRANSPORTENC_INVALID_FRAME_BITS;
+ }
+ FDKwriteBits( hBs, i, 1 ); /* HVXCframeLengthTableIndex */
+ hAss->streamMuxConfigBits+=1;
+ }
+ p_linfo->frameLengthBits = p_lci->bitsFrame;
+ break;
+
+ case AOT_NULL_OBJECT:
+ default:
+ return TRANSPORTENC_INVALID_AOT;
+ }
+ }
+ }
+ }
+
+ FDKwriteBits( hBs, (hAss->otherDataLenBytes>0) ? 1:0, 1 ); /* otherDataPresent */
+ hAss->streamMuxConfigBits+=1;
+
+ if( hAss->otherDataLenBytes > 0 ) {
+
+ INT otherDataLenTmp = hAss->otherDataLenBytes;
+ INT escCnt = 0;
+ INT otherDataLenEsc = 1;
+
+ while(otherDataLenTmp) {
+ otherDataLenTmp >>= 8;
+ escCnt ++;
+ }
+
+ do {
+ otherDataLenTmp = (hAss->otherDataLenBytes>>(escCnt*8)) & 0xFF;
+ escCnt--;
+ otherDataLenEsc = escCnt>0;
+
+ FDKwriteBits( hBs, otherDataLenEsc, 1 );
+ FDKwriteBits( hBs, otherDataLenTmp, 8 );
+ hAss->streamMuxConfigBits+=9;
+ } while(otherDataLenEsc);
+ }
+
+ {
+ USHORT crcCheckPresent=0;
+ USHORT crcCheckSum=0;
+
+ FDKwriteBits( hBs, crcCheckPresent, 1 ); /* crcCheckPresent */
+ hAss->streamMuxConfigBits+=1;
+ if ( crcCheckPresent ){
+ FDKwriteBits( hBs, crcCheckSum, 8 ); /* crcCheckSum */
+ hAss->streamMuxConfigBits+=8;
+ }
+ }
+
+ } else { /* if ( audioMuxVersionA == 0 ) */
+
+ /* for future extensions */
+
+ }
+
+ return TRANSPORTENC_OK;
+}
+
+
+static TRANSPORTENC_ERROR
+WriteAuPayloadLengthInfo( HANDLE_FDK_BITSTREAM hBitStream, int AuLengthBits )
+{
+ int restBytes;
+
+ if( AuLengthBits % 8 )
+ return TRANSPORTENC_INVALID_AU_LENGTH;
+
+ while( AuLengthBits >= 255*8 ) {
+ FDKwriteBits( hBitStream, 255, 8 ); /* 255 shows incomplete AU */
+ AuLengthBits -= (255*8);
+ }
+
+ restBytes = (AuLengthBits) >> 3;
+ FDKwriteBits( hBitStream, restBytes, 8 );
+
+ return TRANSPORTENC_OK;
+}
+
+static
+TRANSPORTENC_ERROR transportEnc_LatmSetNrOfSubframes( HANDLE_LATM_STREAM hAss,
+ INT noSubframes_next) /* nr of access units / payloads within a latm frame */
+{
+ /* sanity chk */
+ if (noSubframes_next < 1 || noSubframes_next > MAX_NR_OF_SUBFRAMES) {
+ return TRANSPORTENC_LATM_INVALID_NR_OF_SUBFRAMES;
+ }
+
+ hAss->noSubframes_next = noSubframes_next;
+
+ /* if at start then we can take over the value immediately, otherwise we have to wait for the next SMC */
+ if ( (hAss->subFrameCnt == 0) && (hAss->latmFrameCounter == 0) ) {
+ hAss->noSubframes = noSubframes_next;
+ }
+
+ return TRANSPORTENC_OK;
+}
+
+static
+int allStreamsSameTimeFraming( HANDLE_LATM_STREAM hAss, UCHAR noProgram, UCHAR noLayer[] /* return */ )
+{
+ int prog, layer;
+
+ signed int lastNoSamples = -1;
+ signed int minFrameSamples = FDK_INT_MAX;
+ signed int maxFrameSamples = 0;
+
+ signed int highestSamplingRate = -1;
+
+ for( prog=0; prog<noProgram; prog++ ) {
+ noLayer[prog] = 0;
+
+ for( layer=0; layer<LATM_MAX_LAYERS; layer++ )
+ {
+ if( hAss->config[prog][layer] != NULL )
+ {
+ INT hsfSamplesFrame;
+
+ noLayer[prog]++;
+
+ if( highestSamplingRate < 0 )
+ highestSamplingRate = hAss->config[prog][layer]->samplingRate;
+
+ hsfSamplesFrame = hAss->config[prog][layer]->samplesPerFrame * highestSamplingRate / hAss->config[prog][layer]->samplingRate;
+
+ if( hsfSamplesFrame <= minFrameSamples ) minFrameSamples = hsfSamplesFrame;
+ if( hsfSamplesFrame >= maxFrameSamples ) maxFrameSamples = hsfSamplesFrame;
+
+ if( lastNoSamples == -1 ) {
+ lastNoSamples = hsfSamplesFrame;
+ } else {
+ if( hsfSamplesFrame != lastNoSamples ) {
+ return 0;
+ }
+ }
+ }
+ }
+ }
+
+ return 1;
+}
+
+/**
+ * Initialize LATM/LOAS Stream and add layer 0 at program 0.
+ */
+static
+TRANSPORTENC_ERROR transportEnc_InitLatmStream( HANDLE_LATM_STREAM hAss,
+ int fractDelayPresent,
+ signed int muxConfigPeriod, /* insert setup data every muxConfigPeriod frames */
+ UINT audioMuxVersion,
+ TRANSPORT_TYPE tt
+ )
+{
+ TRANSPORTENC_ERROR ErrorStatus = TRANSPORTENC_OK;
+
+ if (hAss == NULL)
+ return TRANSPORTENC_INVALID_PARAMETER;
+
+ hAss->tt = tt;
+
+ hAss->noProgram = 1;
+
+ hAss->audioMuxVersion = audioMuxVersion;
+
+ /* Fill noLayer array using hAss->config */
+ hAss->allStreamsSameTimeFraming = allStreamsSameTimeFraming( hAss, hAss->noProgram, hAss->noLayer );
+ /* Only allStreamsSameTimeFraming==1 is supported */
+ FDK_ASSERT(hAss->allStreamsSameTimeFraming);
+
+ hAss->fractDelayPresent = fractDelayPresent;
+ hAss->otherDataLenBytes = 0;
+
+ hAss->varMode = LATMVAR_SIMPLE_SEQUENCE;
+
+ /* initialize counters */
+ hAss->subFrameCnt = 0;
+ hAss->noSubframes = DEFAULT_LATM_NR_OF_SUBFRAMES;
+ hAss->noSubframes_next = DEFAULT_LATM_NR_OF_SUBFRAMES;
+
+ /* sync layer related */
+ hAss->audioMuxLengthBytes = 0;
+
+ hAss->latmFrameCounter = 0;
+ hAss->muxConfigPeriod = muxConfigPeriod;
+
+ return ErrorStatus;
+}
+
+
+/**
+ *
+ */
+UINT transportEnc_LatmCountTotalBitDemandHeader ( HANDLE_LATM_STREAM hAss , unsigned int streamDataLength )
+{
+ UINT bitDemand = 0;
+
+ switch (hAss->tt) {
+ case TT_MP4_LOAS:
+ case TT_MP4_LATM_MCP0:
+ case TT_MP4_LATM_MCP1:
+ if (hAss->subFrameCnt == 0) {
+ bitDemand = transportEnc_LatmCountFixBitDemandHeader ( hAss );
+ }
+ bitDemand += transportEnc_LatmCountVarBitDemandHeader ( hAss , streamDataLength /*- bitDemand*/);
+ break;
+ default:
+ break;
+ }
+
+ return bitDemand;
+}
+
+static TRANSPORTENC_ERROR
+AdvanceAudioMuxElement (
+ HANDLE_LATM_STREAM hAss,
+ HANDLE_FDK_BITSTREAM hBs,
+ int auBits,
+ int bufferFullness,
+ CSTpCallBacks *cb
+ )
+{
+ TRANSPORTENC_ERROR ErrorStatus = TRANSPORTENC_OK;
+ int insertMuxSetup;
+
+ /* Insert setup data to assemble Buffer */
+ if (hAss->subFrameCnt == 0)
+ {
+ if (hAss->muxConfigPeriod > 0) {
+ insertMuxSetup = (hAss->latmFrameCounter == 0);
+ } else {
+ insertMuxSetup = 0;
+ }
+
+ if (hAss->tt != TT_MP4_LATM_MCP0) {
+ if( insertMuxSetup ) {
+ FDKwriteBits( hBs, 0, 1 ); /* useSameStreamMux useNewStreamMux */
+ CreateStreamMuxConfig(hAss, hBs, bufferFullness, cb);
+ if (ErrorStatus != TRANSPORTENC_OK)
+ return ErrorStatus;
+ } else {
+ FDKwriteBits( hBs, 1, 1 ); /* useSameStreamMux */
+ }
+ }
+ }
+
+ /* PayloadLengthInfo */
+ {
+ int prog, layer;
+
+ for (prog = 0; prog < hAss->noProgram; prog++) {
+ for (layer = 0; layer < hAss->noLayer[prog]; layer++) {
+ ErrorStatus = WriteAuPayloadLengthInfo( hBs, auBits );
+ if (ErrorStatus != TRANSPORTENC_OK)
+ return ErrorStatus;
+ }
+ }
+ }
+ /* At this point comes the access unit. */
+
+ return TRANSPORTENC_OK;
+}
+
+TRANSPORTENC_ERROR
+transportEnc_LatmWrite (
+ HANDLE_LATM_STREAM hAss,
+ HANDLE_FDK_BITSTREAM hBs,
+ int auBits,
+ int bufferFullness,
+ CSTpCallBacks *cb
+ )
+{
+ TRANSPORTENC_ERROR ErrorStatus;
+
+ if (hAss->subFrameCnt == 0) {
+ /* Start new frame */
+ FDKresetBitbuffer(hBs, BS_WRITER);
+ }
+
+ hAss->latmSubframeStart = FDKgetValidBits(hBs);
+
+ /* Insert syncword and syncword distance
+ - only if loas
+ - we must update the syncword distance (=audiomuxlengthbytes) later
+ */
+ if( hAss->tt == TT_MP4_LOAS && hAss->subFrameCnt == 0)
+ {
+ /* Start new LOAS frame */
+ FDKwriteBits( hBs, 0x2B7, 11 );
+ hAss->audioMuxLengthBytes = 0;
+ hAss->audioMuxLengthBytesPos = FDKgetValidBits( hBs ); /* store read pointer position */
+ FDKwriteBits( hBs, hAss->audioMuxLengthBytes, 13 );
+ }
+
+ ErrorStatus = AdvanceAudioMuxElement(
+ hAss,
+ hBs,
+ auBits,
+ bufferFullness,
+ cb
+ );
+
+ if (ErrorStatus != TRANSPORTENC_OK)
+ return ErrorStatus;
+
+ return ErrorStatus;
+}
+
+void transportEnc_LatmAdjustSubframeBits(HANDLE_LATM_STREAM hAss,
+ int *bits)
+{
+ /* Substract bits from possible previous subframe */
+ *bits -= hAss->latmSubframeStart;
+ /* Add fill bits */
+ if (hAss->subFrameCnt == 0)
+ *bits += hAss->fillBits;
+}
+
+
+void transportEnc_LatmGetFrame(HANDLE_LATM_STREAM hAss,
+ HANDLE_FDK_BITSTREAM hBs,
+ int *bytes)
+{
+
+ hAss->subFrameCnt++;
+ if (hAss->subFrameCnt >= hAss->noSubframes)
+ {
+
+ /* Add LOAS frame length if required. */
+ if (hAss->tt == TT_MP4_LOAS)
+ {
+ int latmBytes;
+
+ latmBytes = (FDKgetValidBits(hBs)+7) >> 3;
+
+ /* write length info into assembler buffer */
+ hAss->audioMuxLengthBytes = latmBytes - 3; /* 3=Syncword + length */
+ {
+ FDK_BITSTREAM tmpBuf;
+
+ FDKinitBitStream( &tmpBuf, hBs->hBitBuf.Buffer, hBs->hBitBuf.bufSize, 0, BS_WRITER ) ;
+ FDKpushFor( &tmpBuf, hAss->audioMuxLengthBytesPos );
+ FDKwriteBits( &tmpBuf, hAss->audioMuxLengthBytes, 13 );
+ FDKsyncCache( &tmpBuf );
+ }
+ }
+
+ /* Write AudioMuxElement byte alignment fill bits */
+ FDKwriteBits(hBs, 0, hAss->fillBits);
+
+ FDK_ASSERT( (FDKgetValidBits(hBs) % 8) == 0);
+
+ hAss->subFrameCnt = 0;
+
+ FDKsyncCache(hBs);
+ *bytes = (FDKgetValidBits(hBs) + 7)>>3;
+ //FDKfetchBuffer(hBs, buffer, (UINT*)bytes);
+
+ if (hAss->muxConfigPeriod > 0)
+ {
+ hAss->latmFrameCounter++;
+
+ if (hAss->latmFrameCounter >= hAss->muxConfigPeriod) {
+ hAss->latmFrameCounter = 0;
+ hAss->noSubframes = hAss->noSubframes_next;
+ }
+ }
+ } else {
+ /* No data this time */
+ *bytes = 0;
+ }
+}
+
+/**
+ * Init LATM/LOAS
+ */
+TRANSPORTENC_ERROR transportEnc_Latm_Init(
+ HANDLE_LATM_STREAM hAss,
+ HANDLE_FDK_BITSTREAM hBs,
+ CODER_CONFIG *layerConfig,
+ UINT audioMuxVersion,
+ TRANSPORT_TYPE tt,
+ CSTpCallBacks *cb
+ )
+{
+ TRANSPORTENC_ERROR ErrorStatus;
+ int fractDelayPresent = 0;
+ int prog, layer;
+
+ int setupDataDistanceFrames = layerConfig->headerPeriod;
+
+ FDK_ASSERT(setupDataDistanceFrames>=0);
+
+ for (prog=0; prog<LATM_MAX_PROGRAMS; prog++) {
+ for (layer=0; layer<LATM_MAX_LAYERS; layer++) {
+ hAss->config[prog][layer] = NULL;
+ hAss->m_linfo[prog][layer].streamID = -1;
+ }
+ }
+
+ hAss->config[0][0] = layerConfig;
+ hAss->m_linfo[0][0].streamID = 0;
+
+ ErrorStatus = transportEnc_InitLatmStream( hAss,
+ fractDelayPresent,
+ setupDataDistanceFrames,
+ (audioMuxVersion)?1:0,
+ tt
+ );
+ if (ErrorStatus != TRANSPORTENC_OK)
+ goto bail;
+
+ ErrorStatus = transportEnc_LatmSetNrOfSubframes(
+ hAss,
+ layerConfig->nSubFrames
+ );
+ if (ErrorStatus != TRANSPORTENC_OK)
+ goto bail;
+
+ /* Get the size of the StreamMuxConfig somehow */
+ AdvanceAudioMuxElement(hAss, hBs, 0, 0, cb);
+ //CreateStreamMuxConfig(hAss, hBs, 0);
+
+bail:
+ return ErrorStatus;
+}
+
+
+
+
+
+
diff --git a/libMpegTPEnc/src/tpenc_latm.h b/libMpegTPEnc/src/tpenc_latm.h
new file mode 100644
index 0000000..29dba5b
--- /dev/null
+++ b/libMpegTPEnc/src/tpenc_latm.h
@@ -0,0 +1,204 @@
+/***************************** MPEG-4 AAC Encoder **************************
+
+ (C) Copyright Fraunhofer IIS (2001)
+ All Rights Reserved
+
+ Please be advised that this software and/or program delivery is
+ Confidential Information of Fraunhofer and subject to and covered by the
+
+ Fraunhofer IIS Software Evaluation Agreement
+ between Google Inc. and Fraunhofer
+ effective and in full force since March 1, 2012.
+
+ You may use this software and/or program only under the terms and
+ conditions described in the above mentioned Fraunhofer IIS Software
+ Evaluation Agreement. Any other and/or further use requires a separate agreement.
+
+
+ $Id$
+ Author(s):
+ Description:
+
+ This software and/or program is protected by copyright law and international
+ treaties. Any reproduction or distribution of this software and/or program,
+ or any portion of it, may result in severe civil and criminal penalties, and
+ will be prosecuted to the maximum extent possible under law.
+
+******************************************************************************/
+
+#ifndef TPENC_LATM_H
+#define TPENC_LATM_H
+
+
+
+#include "tpenc_lib.h"
+#include "FDK_bitstream.h"
+
+
+#define DEFAULT_LATM_NR_OF_SUBFRAMES 1
+#define DEFAULT_LATM_SMC_REPEAT 8
+
+#define MAX_AAC_LAYERS 9
+
+#define LATM_MAX_PROGRAMS 1
+#define LATM_MAX_STREAM_ID 16
+
+#define LATM_MAX_LAYERS 1 /*MAX_AAC_LAYERS*/
+
+#define MAX_NR_OF_SUBFRAMES 2 /* set this carefully to avoid buffer overflows */
+
+typedef enum { LATMVAR_SIMPLE_SEQUENCE } LATM_VAR_MODE;
+
+typedef struct {
+ signed int frameLengthType;
+ signed int frameLengthBits;
+ signed int varFrameLengthTable[4];
+ signed int streamID;
+} LATM_LAYER_INFO;
+
+#define LATM_ASSEMBLEBUF_SIZE ((200+16)*8)
+#define LATM_SETUPBUF_SIZE (200*8)
+
+typedef struct {
+ LATM_LAYER_INFO m_linfo[LATM_MAX_PROGRAMS][LATM_MAX_LAYERS];
+ CODER_CONFIG *config[LATM_MAX_PROGRAMS][LATM_MAX_LAYERS];
+
+ LATM_VAR_MODE varMode;
+ TRANSPORT_TYPE tt;
+
+ int audioMuxLengthBytes;
+
+ int audioMuxLengthBytesPos;
+ int taraBufferFullness; /* state of the bit reservoir */
+ int varStreamCnt;
+ unsigned int otherDataLenBytes;
+
+ UCHAR latmFrameCounter; /* Current frame number. Counts modulo muxConfigPeriod */
+ UCHAR muxConfigPeriod; /* Distance in frames between MuxConfig */
+
+ UCHAR audioMuxVersion; /* AMV1 supports transmission of taraBufferFullness and ASC lengths */
+ UCHAR audioMuxVersionA; /* for future extensions */
+
+ UCHAR noProgram;
+ UCHAR noLayer[LATM_MAX_PROGRAMS];
+ UCHAR fractDelayPresent;
+
+ UCHAR allStreamsSameTimeFraming;
+ UCHAR subFrameCnt; /* Current Subframe frame */
+ UCHAR noSubframes; /* Number of subframes */
+ UINT latmSubframeStart; /* Position of current subframe start */
+ UCHAR noSubframes_next;
+
+ UCHAR fillBits; /* AudioMuxElement fill bits */
+ UCHAR streamMuxConfigBits;
+
+} LATM_STREAM;
+
+typedef LATM_STREAM *HANDLE_LATM_STREAM;
+
+/**
+ * \brief Initialize LATM_STREAM Handle. Creates automatically one program with one layer with
+ * the given layerConfig. The layerConfig must be persisten because references to this pointer
+ * are made at any time again.
+ * Use transportEnc_Latm_AddLayer() to add more programs/layers.
+ *
+ * \param hLatmStreamInfo HANDLE_LATM_STREAM handle
+ * \param hBs Bitstream handle
+ * \param layerConfig a valid CODER_CONFIG struct containing the current audio configuration parameters
+ * \param audioMuxVersion the LATM audioMuxVersion to be used
+ * \param tt the specific TRANSPORT_TYPE to be used, either TT_MP4_LOAS, TT_MP4_LATM_MCP1 or TT_MP4_LATM_MCP0 LOAS
+ * \param cb callback information structure.
+ *
+ * \return an TRANSPORTENC_ERROR error code
+ */
+TRANSPORTENC_ERROR transportEnc_Latm_Init(
+ HANDLE_LATM_STREAM hLatmStreamInfo,
+ HANDLE_FDK_BITSTREAM hBs,
+ CODER_CONFIG *layerConfig,
+ UINT audioMuxVersion,
+ TRANSPORT_TYPE tt,
+ CSTpCallBacks *cb
+ );
+
+/**
+ * \brief Get bit demand of next LATM/LOAS header
+ *
+ * \param hAss HANDLE_LATM_STREAM handle
+ * \param streamDataLength the length of the payload
+ *
+ * \return the number of bits required by the LATM/LOAS headers
+ */
+unsigned int transportEnc_LatmCountTotalBitDemandHeader (
+ HANDLE_LATM_STREAM hAss,
+ unsigned int streamDataLength
+ );
+
+/**
+ * \brief Write LATM/LOAS header into given bitstream handle
+ *
+ * \param hLatmStreamInfo HANDLE_LATM_STREAM handle
+ * \param hBitstream Bitstream handle
+ * \param auBits amount of current payload bits
+ * \param bufferFullness LATM buffer fullness value
+ * \param cb callback information structure.
+ *
+ * \return an TRANSPORTENC_ERROR error code
+ */
+TRANSPORTENC_ERROR
+transportEnc_LatmWrite (
+ HANDLE_LATM_STREAM hAss,
+ HANDLE_FDK_BITSTREAM hBitstream,
+ int auBits,
+ int bufferFullness,
+ CSTpCallBacks *cb
+ );
+
+/**
+ * \brief Adjust bit count relative to current subframe
+ *
+ * \param hAss HANDLE_LATM_STREAM handle
+ * \param pBits pointer to an int, where the current frame bit count is contained,
+ * and where the subframe relative bit count will be returned into
+ *
+ * \return void
+ */
+void transportEnc_LatmAdjustSubframeBits(HANDLE_LATM_STREAM hAss,
+ int *pBits);
+
+/**
+ * \brief Request an LATM frame, which may, or may not be available
+ *
+ * \param hAss HANDLE_LATM_STREAM handle
+ * \param hBs Bitstream handle
+ * \param pBytes pointer to an int, where the current frame byte count stored into.
+ * A return value of zero means that currently no LATM/LOAS frame can be returned.
+ * The latter is expected in case of multiple subframes being used.
+ *
+ * \return void
+ */
+void transportEnc_LatmGetFrame(
+ HANDLE_LATM_STREAM hAss,
+ HANDLE_FDK_BITSTREAM hBs,
+ int *pBytes
+ );
+
+/**
+ * \brief Write a StreamMuxConfig into the given bitstream handle
+ *
+ * \param hAss HANDLE_LATM_STREAM handle
+ * \param hBs Bitstream handle
+ * \param bufferFullness LATM buffer fullness value
+ * \param cb callback information structure.
+ *
+ * \return void
+ */
+TRANSPORTENC_ERROR
+CreateStreamMuxConfig(
+ HANDLE_LATM_STREAM hAss,
+ HANDLE_FDK_BITSTREAM hBs,
+ int bufferFullness,
+ CSTpCallBacks *cb
+ );
+
+
+#endif /* TPENC_LATM_H */
diff --git a/libMpegTPEnc/src/tpenc_lib.cpp b/libMpegTPEnc/src/tpenc_lib.cpp
new file mode 100644
index 0000000..9afb669
--- /dev/null
+++ b/libMpegTPEnc/src/tpenc_lib.cpp
@@ -0,0 +1,569 @@
+/************************** MPEG-4 Transport Encoder ************************
+
+ (C) Copyright Fraunhofer IIS (2006)
+ All Rights Reserved
+
+ Please be advised that this software and/or program delivery is
+ Confidential Information of Fraunhofer and subject to and covered by the
+
+ Fraunhofer IIS Software Evaluation Agreement
+ between Google Inc. and Fraunhofer
+ effective and in full force since March 1, 2012.
+
+ You may use this software and/or program only under the terms and
+ conditions described in the above mentioned Fraunhofer IIS Software
+ Evaluation Agreement. Any other and/or further use requires a separate agreement.
+
+
+ $Id$
+ Author(s): Manuel Jander
+ Description: MPEG Transport encode
+
+ This software and/or program is protected by copyright law and international
+ treaties. Any reproduction or distribution of this software and/or program,
+ or any portion of it, may result in severe civil and criminal penalties, and
+ will be prosecuted to the maximum extent possible under law.
+
+******************************************************************************/
+
+#include "tpenc_lib.h"
+
+/* library info */
+#include "version"
+
+#define MODULE_NAME "transportEnc"
+
+#include "tpenc_asc.h"
+#include "conv_string.h"
+
+#include "tpenc_adts.h"
+
+#include "tpenc_adif.h"
+
+#include "tpenc_latm.h"
+
+
+
+typedef struct {
+ int curSubFrame;
+ int nSubFrames;
+ int prevBits;
+} RAWPACKETS_INFO;
+
+struct TRANSPORTENC
+{
+ CODER_CONFIG config;
+ TRANSPORT_TYPE transportFmt; /*!< MPEG4 transport type. */
+
+ FDK_BITSTREAM bitStream;
+ UCHAR *bsBuffer;
+ INT bsBufferSize;
+
+ INT pceFrameCounter; /*!< Indicates frame period when PCE must be written in raw_data_block.
+ -1 means not to write a PCE in raw_dat_block. */
+ union {
+ STRUCT_ADTS adts;
+
+ ADIF_INFO adif;
+
+ LATM_STREAM latm;
+
+ RAWPACKETS_INFO raw;
+
+
+
+ } writer;
+
+ CSTpCallBacks callbacks;
+};
+
+typedef struct _TRANSPORTENC_STRUCT TRANSPORTENC_STRUCT;
+
+
+/*
+ * MEMORY Declaration
+ */
+
+C_ALLOC_MEM(Ram_TransportEncoder, TRANSPORTENC, 1)
+
+TRANSPORTENC_ERROR transportEnc_Open( HANDLE_TRANSPORTENC *phTpEnc )
+{
+ HANDLE_TRANSPORTENC hTpEnc = GetRam_TransportEncoder(0);
+
+ if ( hTpEnc == NULL ) {
+ return TRANSPORTENC_INVALID_PARAMETER;
+ }
+
+ *phTpEnc = hTpEnc;
+ return TRANSPORTENC_OK;
+}
+
+/**
+ * \brief Get frame period of PCE in raw_data_block.
+ *
+ * - Write PCE only if necessary. PCE can be part of the ASC if chConfig==0 whererfore
+ * no additonal PCE will be written in raw_data_block.
+ * - A matrixMixdown coefficient can only be written if chConfig is 5.0 or 5.1.
+ * - The PCE repetition rate in raw_data_block can be controlled via headerPeriod parameter.
+ *
+ * \param channelConfig Channel Configuration derived from Channel Mode
+ * \param transportFmt Format of the transport to be written.
+ * \param headerPeriod Chosen PCE frame repetition rate.
+ * \param matrixMixdownA Indicates if a valid Matrix Mixdown coefficient is available.
+ *
+ * \return PCE frame repetition rate. -1 means no PCE present in raw_data_block.
+ */
+static INT getPceRepetitionRate(
+ const int channelConfig,
+ const TRANSPORT_TYPE transportFmt,
+ const int headerPeriod,
+ const int matrixMixdownA
+ )
+{
+ INT pceFrameCounter = -1; /* variable to be returned */
+
+ if (headerPeriod>0) {
+ switch ( channelConfig ) {
+ case 0:
+ switch (transportFmt) {
+ case TT_MP4_ADTS:
+ case TT_MP4_LATM_MCP0:
+ case TT_MP4_RAW:
+ pceFrameCounter = headerPeriod;
+ break;
+ case TT_MP4_ADIF: /* ADIF header comprises PCE */
+ case TT_MP4_LOAS: /* PCE in ASC if chChonfig==0 */
+ case TT_MP4_LATM_MCP1: /* PCE in ASC if chChonfig==0 */
+ case TT_DRM: /* PCE not allowed in DRM */
+ default:
+ pceFrameCounter = -1; /* no PCE in raw_data_block */
+ }
+ break;
+ case 5: /* MODE_1_2_2 */
+ case 6: /* MODE_1_2_2_1 */
+ /* matrixMixdownCoefficient can only be written if 5.0 and 5.1 config present. */
+ if (matrixMixdownA!=0) {
+ switch (transportFmt) {
+ case TT_MP4_ADIF: /* ADIF header comprises PCE */
+ case TT_MP4_ADTS:
+ case TT_MP4_LOAS: /* no PCE in ASC because chConfig!=0 */
+ case TT_MP4_LATM_MCP1: /* no PCE in ASC because chConfig!=0 */
+ case TT_MP4_LATM_MCP0:
+ case TT_MP4_RAW:
+ pceFrameCounter = headerPeriod;
+ break;
+ case TT_DRM: /* PCE not allowed in DRM */
+ default:
+ pceFrameCounter = -1; /* no PCE in raw_data_block */
+ } /* switch transportFmt */
+ } /* if matrixMixdownA!=0 */
+ break;
+ default:
+ pceFrameCounter = -1; /* no PCE in raw_data_block */
+ } /* switch getChannelConfig() */
+ } /* if headerPeriod>0 */
+ else {
+ pceFrameCounter = -1; /* no PCE in raw_data_block */
+ }
+
+ return pceFrameCounter;
+}
+
+TRANSPORTENC_ERROR transportEnc_Init(
+ HANDLE_TRANSPORTENC hTpEnc,
+ UCHAR *bsBuffer,
+ INT bsBufferSize,
+ TRANSPORT_TYPE transportFmt,
+ CODER_CONFIG *cconfig,
+ UINT flags
+ )
+{
+ /* Copy configuration structure */
+ FDKmemcpy(&hTpEnc->config, cconfig, sizeof(CODER_CONFIG));
+
+ /* Init transportEnc struct. */
+ hTpEnc->transportFmt = transportFmt;
+
+ hTpEnc->bsBuffer = bsBuffer;
+ hTpEnc->bsBufferSize = bsBufferSize;
+
+ FDKinitBitStream(&hTpEnc->bitStream, hTpEnc->bsBuffer, hTpEnc->bsBufferSize, 0, BS_WRITER);
+
+ switch (transportFmt) {
+
+ case TT_MP4_ADIF:
+ /* Sanity checks */
+ if ( (hTpEnc->config.aot != AOT_AAC_LC)
+ ||(hTpEnc->config.samplesPerFrame != 1024))
+ {
+ return TRANSPORTENC_INVALID_PARAMETER;
+ }
+ hTpEnc->writer.adif.headerWritten = 0;
+ hTpEnc->writer.adif.samplingRate = hTpEnc->config.samplingRate;
+ hTpEnc->writer.adif.bitRate = hTpEnc->config.bitRate;
+ hTpEnc->writer.adif.profile = ((int)hTpEnc->config.aot) - 1;
+ hTpEnc->writer.adif.cm = hTpEnc->config.channelMode;
+ hTpEnc->writer.adif.bVariableRate = 0;
+ hTpEnc->writer.adif.instanceTag = 0;
+ break;
+
+ case TT_MP4_ADTS:
+ /* Sanity checks */
+ if ( ( hTpEnc->config.aot != AOT_AAC_LC)
+ ||(hTpEnc->config.samplesPerFrame != 1024) )
+ {
+ return TRANSPORTENC_INVALID_PARAMETER;
+ }
+ if ( adtsWrite_Init(&hTpEnc->writer.adts, &hTpEnc->config) != 0) {
+ return TRANSPORTENC_INVALID_PARAMETER;
+ }
+ break;
+
+ case TT_MP4_LOAS:
+ case TT_MP4_LATM_MCP0:
+ case TT_MP4_LATM_MCP1:
+ {
+ TRANSPORTENC_ERROR error;
+
+ error = transportEnc_Latm_Init(
+ &hTpEnc->writer.latm,
+ &hTpEnc->bitStream,
+ &hTpEnc->config,
+ flags & TP_FLAG_LATM_AMV,
+ transportFmt,
+ &hTpEnc->callbacks
+ );
+ if (error != TRANSPORTENC_OK) {
+ return error;
+ }
+ }
+ break;
+
+ case TT_MP4_RAW:
+ hTpEnc->writer.raw.curSubFrame = 0;
+ hTpEnc->writer.raw.nSubFrames = hTpEnc->config.nSubFrames;
+ break;
+
+
+
+ default:
+ return TRANSPORTENC_INVALID_PARAMETER;
+ }
+
+ /* pceFrameCounter indicates if PCE must be written in raw_data_block. */
+ hTpEnc->pceFrameCounter = getPceRepetitionRate(
+ getChannelConfig(hTpEnc->config.channelMode),
+ transportFmt,
+ hTpEnc->config.headerPeriod,
+ hTpEnc->config.matrixMixdownA);
+
+ return TRANSPORTENC_OK;
+}
+
+HANDLE_FDK_BITSTREAM transportEnc_GetBitstream( HANDLE_TRANSPORTENC hTp )
+{
+ return &hTp->bitStream;
+}
+
+int transportEnc_RegisterSbrCallback( HANDLE_TRANSPORTENC hTpEnc, const cbSbr_t cbSbr, void* user_data)
+{
+ if (hTpEnc == NULL) {
+ return -1;
+ }
+ hTpEnc->callbacks.cbSbr = cbSbr;
+ hTpEnc->callbacks.cbSbrData = user_data;
+ return 0;
+}
+
+
+TRANSPORTENC_ERROR transportEnc_WriteAccessUnit(
+ HANDLE_TRANSPORTENC hTp,
+ INT frameUsedBits,
+ int bufferFullness,
+ int ncc
+ )
+{
+ TRANSPORTENC_ERROR err = TRANSPORTENC_OK;
+
+ if (!hTp) {
+ return TRANSPORTENC_INVALID_PARAMETER;
+ }
+ HANDLE_FDK_BITSTREAM hBs = &hTp->bitStream;
+
+ /* In case of writing PCE in raw_data_block frameUsedBits must be adapted. */
+ if (hTp->pceFrameCounter>=hTp->config.headerPeriod) {
+ frameUsedBits += transportEnc_GetPCEBits(hTp->config.channelMode, hTp->config.matrixMixdownA, 3); /* Consider 3 bits ID signalling in alignment */
+ }
+
+ switch (hTp->transportFmt) {
+ case TT_MP4_ADIF:
+ FDKinitBitStream(&hTp->bitStream, hTp->bsBuffer, hTp->bsBufferSize, 0, BS_WRITER);
+ adifWrite_EncodeHeader(
+ &hTp->writer.adif,
+ hBs,
+ bufferFullness
+ );
+ break;
+ case TT_MP4_ADTS:
+ bufferFullness /= ncc; /* Number of Considered Channels */
+ bufferFullness /= 32;
+ bufferFullness = FDKmin(0x7FF, bufferFullness); /* Signal variable rate */
+ adtsWrite_EncodeHeader(
+ &hTp->writer.adts,
+ &hTp->bitStream,
+ bufferFullness,
+ frameUsedBits
+ );
+ break;
+ case TT_MP4_LOAS:
+ case TT_MP4_LATM_MCP0:
+ case TT_MP4_LATM_MCP1:
+ bufferFullness /= ncc; /* Number of Considered Channels */
+ bufferFullness /= 32;
+ bufferFullness = FDKmin(0xFF, bufferFullness); /* Signal variable rate */
+ transportEnc_LatmWrite(
+ &hTp->writer.latm,
+ hBs,
+ frameUsedBits,
+ bufferFullness,
+ &hTp->callbacks
+ );
+ break;
+ case TT_MP4_RAW:
+ if (hTp->writer.raw.curSubFrame >= hTp->writer.raw.nSubFrames) {
+ hTp->writer.raw.curSubFrame = 0;
+ FDKinitBitStream(&hTp->bitStream, hTp->bsBuffer, hTp->bsBufferSize, 0, BS_WRITER);
+ }
+ hTp->writer.raw.prevBits = FDKgetValidBits(hBs);
+ break;
+ default:
+ err = TRANSPORTENC_UNSUPPORTED_FORMAT;
+ break;
+ }
+
+ /* Write PCE in raw_data_block if required */
+ if (hTp->pceFrameCounter>=hTp->config.headerPeriod) {
+ INT crcIndex = 0;
+ /* Align inside PCE with repsect to the first bit of the raw_data_block() */
+ UINT alignAnchor = FDKgetValidBits(&hTp->bitStream);
+
+ /* Write PCE element ID bits */
+ FDKwriteBits(&hTp->bitStream, ID_PCE, 3);
+
+ if ( (hTp->transportFmt==TT_MP4_ADTS) && !hTp->writer.adts.protection_absent) {
+ crcIndex = adtsWrite_CrcStartReg(&hTp->writer.adts, &hTp->bitStream, 0);
+ }
+
+ /* Write PCE as first raw_data_block element */
+ transportEnc_writePCE(&hTp->bitStream, hTp->config.channelMode, hTp->config.samplingRate, 0, 1, hTp->config.matrixMixdownA, hTp->config.flags & CC_PSEUDO_SURROUND, alignAnchor);
+
+ if ( (hTp->transportFmt==TT_MP4_ADTS) && !hTp->writer.adts.protection_absent) {
+ adtsWrite_CrcEndReg(&hTp->writer.adts, &hTp->bitStream, crcIndex);
+ }
+ hTp->pceFrameCounter = 0; /* reset pce frame counter */
+ }
+
+ if (hTp->pceFrameCounter!=-1) {
+ hTp->pceFrameCounter++; /* Update pceFrameCounter only if PCE writing is active. */
+ }
+
+ return err;
+}
+
+
+TRANSPORTENC_ERROR transportEnc_EndAccessUnit(HANDLE_TRANSPORTENC hTp, int *bits)
+{
+ switch (hTp->transportFmt) {
+ case TT_MP4_LATM_MCP0:
+ case TT_MP4_LATM_MCP1:
+ case TT_MP4_LOAS:
+ transportEnc_LatmAdjustSubframeBits(&hTp->writer.latm, bits);
+ break;
+ case TT_MP4_ADTS:
+ adtsWrite_EndRawDataBlock(&hTp->writer.adts, &hTp->bitStream, bits);
+ break;
+ case TT_MP4_ADIF:
+ /* Substract ADIF header from AU bits, not to be considered. */
+ *bits -= adifWrite_GetHeaderBits(&hTp->writer.adif);
+ hTp->writer.adif.headerWritten = 1;
+ break;
+ case TT_MP4_RAW:
+ *bits -= hTp->writer.raw.prevBits;
+ break;
+ default:
+ break;
+ }
+
+ return TRANSPORTENC_OK;
+}
+
+TRANSPORTENC_ERROR transportEnc_GetFrame(HANDLE_TRANSPORTENC hTpEnc, int *nbytes)
+{
+ HANDLE_FDK_BITSTREAM hBs = &hTpEnc->bitStream;
+
+ switch (hTpEnc->transportFmt) {
+ case TT_MP4_LATM_MCP0:
+ case TT_MP4_LATM_MCP1:
+ case TT_MP4_LOAS:
+ *nbytes = hTpEnc->bsBufferSize;
+ transportEnc_LatmGetFrame(&hTpEnc->writer.latm, hBs, nbytes);
+ break;
+ case TT_MP4_ADTS:
+ if (hTpEnc->writer.adts.currentBlock >= hTpEnc->writer.adts.num_raw_blocks+1) {
+ *nbytes = (FDKgetValidBits(hBs) + 7)>>3;
+ hTpEnc->writer.adts.currentBlock = 0;
+ } else {
+ *nbytes = 0;
+ }
+ break;
+ case TT_MP4_ADIF:
+ FDK_ASSERT((INT)FDKgetValidBits(hBs) >= 0);
+ *nbytes = (FDKgetValidBits(hBs) + 7)>>3;
+ break;
+ case TT_MP4_RAW:
+ FDKsyncCache(hBs);
+ hTpEnc->writer.raw.curSubFrame++;
+ *nbytes = ((FDKgetValidBits(hBs)-hTpEnc->writer.raw.prevBits) + 7)>>3;
+ break;
+ default:
+ break;
+ }
+
+ return TRANSPORTENC_OK;
+}
+
+INT transportEnc_GetStaticBits( HANDLE_TRANSPORTENC hTp, int auBits )
+{
+ INT nbits = 0, nPceBits = 0;
+
+ /* Write PCE within raw_data_block in transport lib. */
+ if (hTp->pceFrameCounter>=hTp->config.headerPeriod) {
+ nPceBits = transportEnc_GetPCEBits(hTp->config.channelMode, hTp->config.matrixMixdownA, 3); /* Consider 3 bits ID signalling in alignment */
+ auBits += nPceBits; /* Adapt required raw_data_block bit consumtpion for AU length information e.g. in LATM/LOAS configuration. */
+ }
+
+ switch (hTp->transportFmt) {
+ case TT_MP4_ADIF:
+ case TT_MP4_RAW:
+ nbits = 0; /* Do not consider the ADIF header into the total bitrate */
+ break;
+ case TT_MP4_ADTS:
+ nbits = adtsWrite_GetHeaderBits(&hTp->writer.adts);
+ break;
+ case TT_MP4_LOAS:
+ case TT_MP4_LATM_MCP0:
+ case TT_MP4_LATM_MCP1:
+ nbits = transportEnc_LatmCountTotalBitDemandHeader( &hTp->writer.latm, auBits );
+ break;
+ default:
+ nbits = 0;
+ break;
+ }
+
+ /* PCE is written in the transport library therefore the bit consumption is part of the transport static bits. */
+ nbits += nPceBits;
+
+ return nbits;
+}
+
+void transportEnc_Close(HANDLE_TRANSPORTENC *phTp)
+{
+ if (phTp != NULL)
+ {
+ if (*phTp != NULL) {
+ FreeRam_TransportEncoder(phTp);
+ }
+ }
+}
+
+int transportEnc_CrcStartReg(HANDLE_TRANSPORTENC hTpEnc, int mBits)
+{
+ int crcReg = 0;
+
+ switch (hTpEnc->transportFmt) {
+ case TT_MP4_ADTS:
+ crcReg = adtsWrite_CrcStartReg(&hTpEnc->writer.adts, &hTpEnc->bitStream, mBits);
+ break;
+ default:
+ break;
+ }
+
+ return crcReg;
+}
+
+void transportEnc_CrcEndReg(HANDLE_TRANSPORTENC hTpEnc, int reg)
+{
+ switch (hTpEnc->transportFmt) {
+ case TT_MP4_ADTS:
+ adtsWrite_CrcEndReg(&hTpEnc->writer.adts, &hTpEnc->bitStream, reg);
+ break;
+ default:
+ break;
+ }
+}
+
+
+TRANSPORTENC_ERROR transportEnc_GetConf(HANDLE_TRANSPORTENC hTpEnc,
+ CODER_CONFIG *cc,
+ FDK_BITSTREAM *dataBuffer,
+ UINT *confType)
+{
+ TRANSPORTENC_ERROR tpErr = TRANSPORTENC_OK;
+ HANDLE_LATM_STREAM hLatmConfig = &hTpEnc->writer.latm;
+
+ *confType = 0; /* set confType variable to default */
+
+ /* write StreamMuxConfig or AudioSpecificConfig depending on format used */
+ switch (hTpEnc->transportFmt)
+ {
+ case TT_MP4_LATM_MCP0:
+ case TT_MP4_LATM_MCP1:
+ case TT_MP4_LOAS:
+ tpErr = CreateStreamMuxConfig(hLatmConfig, dataBuffer, 0, &hTpEnc->callbacks);
+ *confType = 1; /* config is SMC */
+ break;
+ default:
+ if (transportEnc_writeASC(dataBuffer, cc, &hTpEnc->callbacks) != 0) {
+ tpErr = TRANSPORTENC_UNKOWN_ERROR;
+ }
+ }
+
+ return tpErr;
+
+}
+
+TRANSPORTENC_ERROR transportEnc_GetLibInfo( LIB_INFO *info )
+{
+ int i;
+
+ if (info == NULL) {
+ return TRANSPORTENC_INVALID_PARAMETER;
+ }
+ /* search for next free tab */
+ for (i = 0; i < FDK_MODULE_LAST; i++) {
+ if (info[i].module_id == FDK_NONE) break;
+ }
+ if (i == FDK_MODULE_LAST) {
+ return TRANSPORTENC_UNKOWN_ERROR;
+ }
+ info += i;
+
+ info->module_id = FDK_TPENC;
+ info->version = LIB_VERSION(TP_LIB_VL0, TP_LIB_VL1, TP_LIB_VL2);
+ LIB_VERSION_STRING(info);
+ info->build_date = __DATE__;
+ info->build_time = __TIME__;
+ info->title = TP_LIB_TITLE;
+
+ /* Set flags */
+ info->flags = 0
+ | CAPF_ADIF
+ | CAPF_ADTS
+ | CAPF_LATM
+ | CAPF_LOAS
+ | CAPF_RAWPACKETS
+ ;
+
+ return TRANSPORTENC_OK;
+}
+
diff --git a/libMpegTPEnc/src/version b/libMpegTPEnc/src/version
new file mode 100644
index 0000000..6aa18cf
--- /dev/null
+++ b/libMpegTPEnc/src/version
@@ -0,0 +1,8 @@
+
+/* library info */
+#define TP_LIB_VL0 2
+#define TP_LIB_VL1 2
+#define TP_LIB_VL2 0
+#define TP_LIB_TITLE "MPEG Transport"
+#define TP_LIB_BUILD_DATE __DATE__
+#define TP_LIB_BUILD_TIME __TIME__