diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2012-07-11 10:15:24 -0700 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2012-07-11 10:15:24 -0700 |
commit | 2228e360595641dd906bf1773307f43d304f5b2e (patch) | |
tree | 57f3d390ebb0782cc0de0fb984c8ea7e45b4f386 /libMpegTPEnc | |
download | fdk-aac-dabplus-2228e360595641dd906bf1773307f43d304f5b2e.tar.gz fdk-aac-dabplus-2228e360595641dd906bf1773307f43d304f5b2e.tar.bz2 fdk-aac-dabplus-2228e360595641dd906bf1773307f43d304f5b2e.zip |
Snapshot 2bda038c163298531d47394bc2c09e1409c5d0db
Change-Id: If584e579464f28b97d50e51fc76ba654a5536c54
Diffstat (limited to 'libMpegTPEnc')
-rw-r--r-- | libMpegTPEnc/include/mpegFileWrite.h | 140 | ||||
-rw-r--r-- | libMpegTPEnc/include/tp_data.h | 340 | ||||
-rw-r--r-- | libMpegTPEnc/include/tpenc_lib.h | 296 | ||||
-rw-r--r-- | libMpegTPEnc/src/tpenc_adif.cpp | 182 | ||||
-rw-r--r-- | libMpegTPEnc/src/tpenc_adif.h | 135 | ||||
-rw-r--r-- | libMpegTPEnc/src/tpenc_adts.cpp | 315 | ||||
-rw-r--r-- | libMpegTPEnc/src/tpenc_adts.h | 218 | ||||
-rw-r--r-- | libMpegTPEnc/src/tpenc_asc.cpp | 552 | ||||
-rw-r--r-- | libMpegTPEnc/src/tpenc_asc.h | 142 | ||||
-rw-r--r-- | libMpegTPEnc/src/tpenc_latm.cpp | 882 | ||||
-rw-r--r-- | libMpegTPEnc/src/tpenc_latm.h | 264 | ||||
-rw-r--r-- | libMpegTPEnc/src/tpenc_lib.cpp | 631 | ||||
-rw-r--r-- | libMpegTPEnc/src/version | 8 |
13 files changed, 4105 insertions, 0 deletions
diff --git a/libMpegTPEnc/include/mpegFileWrite.h b/libMpegTPEnc/include/mpegFileWrite.h new file mode 100644 index 0000000..6716d8f --- /dev/null +++ b/libMpegTPEnc/include/mpegFileWrite.h @@ -0,0 +1,140 @@ + +/* ----------------------------------------------------------------------------------------------------------- +Software License for The Fraunhofer FDK AAC Codec Library for Android + +© Copyright 1995 - 2012 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. + All rights reserved. + + 1. INTRODUCTION +The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements +the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio. +This FDK AAC Codec software is intended to be used on a wide variety of Android devices. + +AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual +audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by +independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part +of the MPEG specifications. + +Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer) +may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners +individually for the purpose of encoding or decoding bit streams in products that are compliant with +the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license +these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec +software may already be covered under those patent licenses when it is used for those licensed purposes only. + +Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality, +are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional +applications information and documentation. + +2. COPYRIGHT LICENSE + +Redistribution and use in source and binary forms, with or without modification, are permitted without +payment of copyright license fees provided that you satisfy the following conditions: + +You must retain the complete text of this software license in redistributions of the FDK AAC Codec or +your modifications thereto in source code form. + +You must retain the complete text of this software license in the documentation and/or other materials +provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form. +You must make available free of charge copies of the complete source code of the FDK AAC Codec and your +modifications thereto to recipients of copies in binary form. + +The name of Fraunhofer may not be used to endorse or promote products derived from this library without +prior written permission. + +You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec +software or your modifications thereto. + +Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software +and the date of any change. For modified versions of the FDK AAC Codec, the term +"Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term +"Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android." + +3. NO PATENT LICENSE + +NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer, +ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with +respect to this software. + +You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized +by appropriate patent licenses. + +4. DISCLAIMER + +This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors +"AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties +of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages, +including but not limited to procurement of substitute goods or services; loss of use, data, or profits, +or business interruption, however caused and on any theory of liability, whether in contract, strict +liability, or tort (including negligence), arising in any way out of the use of this software, even if +advised of the possibility of such damage. + +5. CONTACT INFORMATION + +Fraunhofer Institute for Integrated Circuits IIS +Attention: Audio and Multimedia Departments - FDK AAC LL +Am Wolfsmantel 33 +91058 Erlangen, Germany + +www.iis.fraunhofer.de/amm +amm-info@iis.fraunhofer.de +----------------------------------------------------------------------------------------------------------- */ + +/***************************** MPEG-4 AAC Decoder ************************** + + Author(s): Manuel Jander + Description: Bitstream data provider for MP4 decoders + +******************************************************************************/ + +#include "machine_type.h" +#include "FDK_audio.h" + +/*!< If MPFWRITE_MP4FF_ENABLE is set, include support for MPEG ISO fileformat. + If not set, no .mp4, .m4a and .3gp files can be used for input. */ +/* #define MPFWRITE_MP4FF_ENABLE */ + +typedef struct STRUCT_FILEWRITE *HANDLE_FILEWRITE; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * \brief Open an MPEG audio file. + * \param mpegFileWrite_Filename String of the filename to be opened. + * \param fileFmt Transport format to use. + * \param conf + * \param confSize + * \return MPEG file write handle. + */ +HANDLE_FILEWRITE mpegFileWrite_Open( char *mpegFileWrite_Filename, + FILE_FORMAT fileFmt, + TRANSPORT_TYPE transportType, + UCHAR *conf, + UINT confSize + ); + +/** + * \brief Write to an MPEG audio file. + * \param inBuffer Buffer to write. + * \param bufferSize Size of buffer to write in bytes. + * \return 0 on sucess, -1 on unsupported file format or write error. + */ +int mpegFileWrite_Write( HANDLE_FILEWRITE hFileWrite, + UCHAR *inBuffer, + int bufferSize + ); + +/** + * \brief Deallocate memory and close file. + * \param hFileWrite MPEG file write handle. + * \return 0 on sucess. + */ +int mpegFileWrite_Close( HANDLE_FILEWRITE *hFileWrite ); + + +#ifdef __cplusplus +} +#endif diff --git a/libMpegTPEnc/include/tp_data.h b/libMpegTPEnc/include/tp_data.h new file mode 100644 index 0000000..6e8218a --- /dev/null +++ b/libMpegTPEnc/include/tp_data.h @@ -0,0 +1,340 @@ + +/* ----------------------------------------------------------------------------------------------------------- +Software License for The Fraunhofer FDK AAC Codec Library for Android + +© Copyright 1995 - 2012 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. + All rights reserved. + + 1. INTRODUCTION +The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements +the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio. +This FDK AAC Codec software is intended to be used on a wide variety of Android devices. + +AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual +audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by +independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part +of the MPEG specifications. + +Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer) +may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners +individually for the purpose of encoding or decoding bit streams in products that are compliant with +the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license +these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec +software may already be covered under those patent licenses when it is used for those licensed purposes only. + +Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality, +are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional +applications information and documentation. + +2. COPYRIGHT LICENSE + +Redistribution and use in source and binary forms, with or without modification, are permitted without +payment of copyright license fees provided that you satisfy the following conditions: + +You must retain the complete text of this software license in redistributions of the FDK AAC Codec or +your modifications thereto in source code form. + +You must retain the complete text of this software license in the documentation and/or other materials +provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form. +You must make available free of charge copies of the complete source code of the FDK AAC Codec and your +modifications thereto to recipients of copies in binary form. + +The name of Fraunhofer may not be used to endorse or promote products derived from this library without +prior written permission. + +You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec +software or your modifications thereto. + +Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software +and the date of any change. For modified versions of the FDK AAC Codec, the term +"Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term +"Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android." + +3. NO PATENT LICENSE + +NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer, +ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with +respect to this software. + +You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized +by appropriate patent licenses. + +4. DISCLAIMER + +This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors +"AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties +of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages, +including but not limited to procurement of substitute goods or services; loss of use, data, or profits, +or business interruption, however caused and on any theory of liability, whether in contract, strict +liability, or tort (including negligence), arising in any way out of the use of this software, even if +advised of the possibility of such damage. + +5. CONTACT INFORMATION + +Fraunhofer Institute for Integrated Circuits IIS +Attention: Audio and Multimedia Departments - FDK AAC LL +Am Wolfsmantel 33 +91058 Erlangen, Germany + +www.iis.fraunhofer.de/amm +amm-info@iis.fraunhofer.de +----------------------------------------------------------------------------------------------------------- */ + +/***************************** MPEG-4 AAC Decoder ************************** + + Author(s): Manuel Jander + Description: MPEG Transport data tables + +******************************************************************************/ + +#ifndef __TP_DATA_H__ +#define __TP_DATA_H__ + +#include "machine_type.h" +#include "FDK_audio.h" +#include "FDK_bitstream.h" + +/* + * Configuration + */ +#define TP_GA_ENABLE +/* #define TP_CELP_ENABLE */ +/* #define TP_HVXC_ENABLE */ +/* #define TP_SLS_ENABLE */ +#define TP_ELD_ENABLE +/* #define TP_USAC_ENABLE */ +/* #define TP_RSVD50_ENABLE */ + +#if defined(TP_GA_ENABLE) || defined(TP_SLS_ENABLE) +#define TP_PCE_ENABLE /**< Enable full PCE support */ +#endif + +/** + * ProgramConfig struct. + */ +/* ISO/IEC 14496-3 4.4.1.1 Table 4.2 Program config element */ +#define PC_FSB_CHANNELS_MAX 16 /* Front/Side/Back channels */ +#define PC_LFE_CHANNELS_MAX 4 +#define PC_ASSOCDATA_MAX 8 +#define PC_CCEL_MAX 16 /* CC elements */ +#define PC_COMMENTLENGTH 256 + +typedef struct +{ +#ifdef TP_PCE_ENABLE + /* PCE bitstream elements: */ + UCHAR ElementInstanceTag; + UCHAR Profile; + UCHAR SamplingFrequencyIndex; + UCHAR NumFrontChannelElements; + UCHAR NumSideChannelElements; + UCHAR NumBackChannelElements; + UCHAR NumLfeChannelElements; + UCHAR NumAssocDataElements; + UCHAR NumValidCcElements; + + UCHAR MonoMixdownPresent; + UCHAR MonoMixdownElementNumber; + + UCHAR StereoMixdownPresent; + UCHAR StereoMixdownElementNumber; + + UCHAR MatrixMixdownIndexPresent; + UCHAR MatrixMixdownIndex; + UCHAR PseudoSurroundEnable; + + UCHAR FrontElementIsCpe[PC_FSB_CHANNELS_MAX]; + UCHAR FrontElementTagSelect[PC_FSB_CHANNELS_MAX]; + + UCHAR SideElementIsCpe[PC_FSB_CHANNELS_MAX]; + UCHAR SideElementTagSelect[PC_FSB_CHANNELS_MAX]; + + UCHAR BackElementIsCpe[PC_FSB_CHANNELS_MAX]; + UCHAR BackElementTagSelect[PC_FSB_CHANNELS_MAX]; + + UCHAR LfeElementTagSelect[PC_LFE_CHANNELS_MAX]; + + UCHAR AssocDataElementTagSelect[PC_ASSOCDATA_MAX]; + + UCHAR CcElementIsIndSw[PC_CCEL_MAX]; + UCHAR ValidCcElementTagSelect[PC_CCEL_MAX]; + + UCHAR CommentFieldBytes; + UCHAR Comment[PC_COMMENTLENGTH]; +#endif /* TP_PCE_ENABLE */ + + /* Helper variables for administration: */ + UCHAR isValid; /*!< Flag showing if PCE has been read successfully. */ + UCHAR NumChannels; /*!< Amount of audio channels summing all channel elements including LFEs */ + UCHAR NumEffectiveChannels; /*!< Amount of audio channels summing only SCEs and CPEs */ + UCHAR elCounter; + +} CProgramConfig; + +typedef enum { + ASCEXT_UNKOWN = -1, + ASCEXT_SBR = 0x2b7, + ASCEXT_PS = 0x548, + ASCEXT_MPS = 0x76a, + ASCEXT_SAOC = 0x7cb, + ASCEXT_LDMPS = 0x7cc + +} TP_ASC_EXTENSION_ID; + +#ifdef TP_GA_ENABLE +/** + * GaSpecificConfig struct + */ +typedef struct { + UINT m_frameLengthFlag ; + UINT m_dependsOnCoreCoder ; + UINT m_coreCoderDelay ; + + UINT m_extensionFlag ; + UINT m_extensionFlag3 ; + + UINT m_layer; + UINT m_numOfSubFrame; + UINT m_layerLength; + +} CSGaSpecificConfig; +#endif /* TP_GA_ENABLE */ + + + + +#ifdef TP_ELD_ENABLE + +typedef enum { + ELDEXT_TERM = 0x0, /* Termination tag */ + ELDEXT_SAOC = 0x1, /* SAOC config */ + ELDEXT_LDSAC = 0x2 /* LD MPEG Surround config */ + /* reserved */ +} ASC_ELD_EXT_TYPE; + +typedef struct { + UCHAR m_frameLengthFlag; + + UCHAR m_sbrPresentFlag; + UCHAR m_useLdQmfTimeAlign; /* Use LD-MPS QMF in SBR to achive time alignment */ + UCHAR m_sbrSamplingRate; + UCHAR m_sbrCrcFlag; + +} CSEldSpecificConfig; +#endif /* TP_ELD_ENABLE */ + + + + +/** + * Audio configuration struct, suitable for encoder and decoder configuration. + */ +typedef struct { + + /* XYZ Specific Data */ + union { +#ifdef TP_GA_ENABLE + CSGaSpecificConfig m_gaSpecificConfig; /**< General audio specific configuration. */ +#endif /* TP_GA_ENABLE */ +#ifdef TP_ELD_ENABLE + CSEldSpecificConfig m_eldSpecificConfig; /**< ELD specific configuration. */ +#endif /* TP_ELD_ENABLE */ + } m_sc; + + /* Common ASC parameters */ +#ifdef TP_PCE_ENABLE + CProgramConfig m_progrConfigElement; /**< Program configuration. */ +#endif /* TP_PCE_ENABLE */ + + AUDIO_OBJECT_TYPE m_aot; /**< Audio Object Type. */ + UINT m_samplingFrequency; /**< Samplerate. */ + UINT m_samplesPerFrame; /**< Amount of samples per frame. */ + UINT m_directMapping; /**< Document this please !! */ + + AUDIO_OBJECT_TYPE m_extensionAudioObjectType; /**< Audio object type */ + UINT m_extensionSamplingFrequency; /**< Samplerate */ + + SCHAR m_channelConfiguration; /**< Channel configuration index */ + + SCHAR m_epConfig; /**< Error protection index */ + SCHAR m_vcb11Flag; /**< aacSectionDataResilienceFlag */ + SCHAR m_rvlcFlag; /**< aacScalefactorDataResilienceFlag */ + SCHAR m_hcrFlag; /**< aacSpectralDataResilienceFlag */ + + SCHAR m_sbrPresentFlag; /**< Flag indicating the presence of SBR data in the bitstream */ + SCHAR m_psPresentFlag; /**< Flag indicating the presence of parametric stereo data in the bitstream */ + UCHAR m_samplingFrequencyIndex; /**< Samplerate index */ + UCHAR m_extensionSamplingFrequencyIndex; /**< Samplerate index */ + SCHAR m_extensionChannelConfiguration; /**< Channel configuration index */ + +} CSAudioSpecificConfig; + +typedef INT (*cbUpdateConfig_t)(void*, const CSAudioSpecificConfig*); +typedef INT (*cbSsc_t)( + void*, HANDLE_FDK_BITSTREAM, + const AUDIO_OBJECT_TYPE coreCodec, + const INT samplingFrequency, + const INT muxMode, + const INT configBytes + ); +typedef INT (*cbSbr_t)( + void * self, + HANDLE_FDK_BITSTREAM hBs, + const INT sampleRateIn, + const INT sampleRateOut, + const INT samplesPerFrame, + const AUDIO_OBJECT_TYPE coreCodec, + const MP4_ELEMENT_ID elementID, + const INT elementIndex + ); + +typedef struct { + cbUpdateConfig_t cbUpdateConfig; /*!< Function pointer for Config change notify callback. */ + void *cbUpdateConfigData; /*!< User data pointer for Config change notify callback. */ + cbSsc_t cbSsc; /*!< Function pointer for SSC parser callback. */ + void *cbSscData; /*!< User data pointer for SSC parser callback. */ + cbSbr_t cbSbr; /*!< Function pointer for SBR header parser callback. */ + void *cbSbrData; /*!< User data pointer for SBR header parser callback. */ +} CSTpCallBacks; + +static const UINT SamplingRateTable[] = +{ 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350, 0, 0, + 0 +}; + +static inline +int getSamplingRateIndex( UINT samplingRate ) +{ + UINT sf_index, tableSize=sizeof(SamplingRateTable)/sizeof(UINT); + + for (sf_index=0; sf_index<tableSize; sf_index++) { + if( SamplingRateTable[sf_index] == samplingRate ) break; + } + + if (sf_index>tableSize-1) { + return tableSize-1; + } + + return sf_index; +} + +/* + * Get Channel count from channel configuration + */ +static inline int getNumberOfTotalChannels(int channelConfig) +{ + if (channelConfig > 0 && channelConfig < 8) + return (channelConfig == 7)?8:channelConfig; + else + return 0; +} + +static inline +int getNumberOfEffectiveChannels(const int channelConfig) +{ + const int n[] = {0,1,2,3,4,5,5,7}; + return n[channelConfig]; +} + +#endif /* __TP_DATA_H__ */ diff --git a/libMpegTPEnc/include/tpenc_lib.h b/libMpegTPEnc/include/tpenc_lib.h new file mode 100644 index 0000000..a06e7a7 --- /dev/null +++ b/libMpegTPEnc/include/tpenc_lib.h @@ -0,0 +1,296 @@ + +/* ----------------------------------------------------------------------------------------------------------- +Software License for The Fraunhofer FDK AAC Codec Library for Android + +© Copyright 1995 - 2012 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. + All rights reserved. + + 1. INTRODUCTION +The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements +the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio. +This FDK AAC Codec software is intended to be used on a wide variety of Android devices. + +AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual +audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by +independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part +of the MPEG specifications. + +Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer) +may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners +individually for the purpose of encoding or decoding bit streams in products that are compliant with +the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license +these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec +software may already be covered under those patent licenses when it is used for those licensed purposes only. + +Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality, +are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional +applications information and documentation. + +2. COPYRIGHT LICENSE + +Redistribution and use in source and binary forms, with or without modification, are permitted without +payment of copyright license fees provided that you satisfy the following conditions: + +You must retain the complete text of this software license in redistributions of the FDK AAC Codec or +your modifications thereto in source code form. + +You must retain the complete text of this software license in the documentation and/or other materials +provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form. +You must make available free of charge copies of the complete source code of the FDK AAC Codec and your +modifications thereto to recipients of copies in binary form. + +The name of Fraunhofer may not be used to endorse or promote products derived from this library without +prior written permission. + +You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec +software or your modifications thereto. + +Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software +and the date of any change. For modified versions of the FDK AAC Codec, the term +"Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term +"Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android." + +3. NO PATENT LICENSE + +NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer, +ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with +respect to this software. + +You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized +by appropriate patent licenses. + +4. DISCLAIMER + +This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors +"AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties +of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages, +including but not limited to procurement of substitute goods or services; loss of use, data, or profits, +or business interruption, however caused and on any theory of liability, whether in contract, strict +liability, or tort (including negligence), arising in any way out of the use of this software, even if +advised of the possibility of such damage. + +5. CONTACT INFORMATION + +Fraunhofer Institute for Integrated Circuits IIS +Attention: Audio and Multimedia Departments - FDK AAC LL +Am Wolfsmantel 33 +91058 Erlangen, Germany + +www.iis.fraunhofer.de/amm +amm-info@iis.fraunhofer.de +----------------------------------------------------------------------------------------------------------- */ + +/************************** MPEG-4 Transport Encoder ************************ + + Author(s): Manuel Jander + Description: MPEG Transport encode + +******************************************************************************/ + +#ifndef __TPENC_LIB_H__ +#define __TPENC_LIB_H__ + +#include "tp_data.h" +#include "FDK_bitstream.h" + +#define TRANSPORTENC_INBUF_SIZE 8192 + +typedef enum { + TRANSPORTENC_OK = 0, /*!< All fine. */ + TRANSPORTENC_NO_MEM, /*!< Out of memory. */ + TRANSPORTENC_UNKOWN_ERROR = 1, /*!< Unknown error (embarrasing). */ + TRANSPORTENC_INVALID_PARAMETER, /*!< An invalid parameter was passed to a function . */ + TRANSPORTENC_PARSE_ERROR, /*!< Bitstream data contained inconsistencies (wrong syntax). */ + TRANSPORTENC_UNSUPPORTED_FORMAT, /*!< Unsupported transport format. */ + TRANSPORTENC_NOT_ENOUGH_BITS, /*!< Out of bits. Provide more bits and try again. */ + + TRANSPORTENC_INVALID_CONFIG, /*!< Error in configuration. */ + TRANSPORTENC_LATM_INVALID_NR_OF_SUBFRAMES, /*!< LATM: number of subframes out of range. */ + TRANSPORTENC_LOAS_NOT_AVAILABLE, /*!< LOAS format not supported. */ + TRANSPORTENC_INVALID_LATM_ALIGNMENT, /*!< AudioMuxElement length not aligned to 1 byte. */ + + TRANSPORTENC_INVALID_TRANSMISSION_FRAME_LENGTH, /*!< Invalid transmission frame length (< 0). */ + TRANSPORTENC_INVALID_CELP_FRAME_LENGTH, /*!< Invalid CELP frame length found (>= 62). */ + TRANSPORTENC_INVALID_FRAME_BITS, /*!< Frame bits is not 40 and not 80. */ + TRANSPORTENC_INVALID_AOT, /*!< Unknown AOT found. */ + TRANSPORTENC_INVALID_AU_LENGTH /*!< Invalid Access Unit length (not byte-aligned). */ + +} TRANSPORTENC_ERROR; + +typedef struct TRANSPORTENC *HANDLE_TRANSPORTENC; + +/** + * \brief Determine a reasonable channel configuration on the basis of channel_mode. + * \param noChannels Number of audio channels. + * \return CHANNEL_MODE value that matches the given amount of audio channels. + */ +CHANNEL_MODE transportEnc_GetChannelMode( int noChannels ); + +/** + * \brief Register SBR heaqder writer callback. + * \param hTp Handle of transport decoder. + * \param cbUpdateConfig Pointer to a callback function to handle SBR header writing. + * \param user_data void pointer for user data passed to the callback as first parameter. + * \return 0 on success. + */ +int transportEnc_RegisterSbrCallback ( + HANDLE_TRANSPORTENC hTpEnc, + const cbSbr_t cbSbr, + void* user_data + ); + +/** + * \brief Register SSC writer callback. + * \param hTp Handle of transport decoder. + * \param cbUpdateConfig Pointer to a callback function to handle SSC writing. + * \param user_data void pointer for user data passed to the callback as first parameter. + * \return 0 on success. + */ +int transportEnc_RegisterSscCallback ( + HANDLE_TRANSPORTENC hTpEnc, + const cbSsc_t cbSsc, + void* user_data + ); + +/** + * \brief Write ASC from given parameters. + * \param asc A HANDLE_FDK_BITSTREAM where the ASC is written to. + * \param config Structure containing the codec configuration settings. + * \param cb callback information structure. + * \return 0 on success. + */ +int transportEnc_writeASC ( + HANDLE_FDK_BITSTREAM asc, + CODER_CONFIG *config, + CSTpCallBacks *cb + ); + + +/* Defintion of flags that can be passed to transportEnc_Open() */ +#define TP_FLAG_MPEG4 1 /** MPEG4 (instead of MPEG2) */ +#define TP_FLAG_LATM_AMV 2 /** LATM AudioMuxVersion */ +#define TP_FLAG_LATM_AMVA 4 /** LATM AudioMuxVersionA */ + +/** + * \brief Allocate transport encoder. + * \param phTpEnc Pointer to transport encoder handle. + * \return Error code. + */ +TRANSPORTENC_ERROR transportEnc_Open( HANDLE_TRANSPORTENC *phTpEnc ); + +/** + * \brief Init transport encoder. + * \param bsBuffer Pointer to transport encoder. + * \param bsBuffer Pointer to bitstream buffer. + * \param bsBufferSize Size in bytes of bsBuffer. + * \param transportFmt Format of the transport to be written. + * \param config Pointer to a valid CODER_CONFIG struct. + * \param flags Transport encoder flags. + * \return Error code. + */ +TRANSPORTENC_ERROR transportEnc_Init( + HANDLE_TRANSPORTENC hTpEnc, + UCHAR *bsBuffer, + INT bsBufferSize, + TRANSPORT_TYPE transportFmt, + CODER_CONFIG *config, + UINT flags + ); + +/** + * \brief Get transport encoder bitstream. + * \param hTp Pointer to a transport encoder handle. + * \return The handle to the requested FDK bitstream. + */ +HANDLE_FDK_BITSTREAM transportEnc_GetBitstream( HANDLE_TRANSPORTENC hTp ); + +/** + * \brief Get amount of bits required by the transport headers. + * \param hTp Handle of transport encoder. + * \param auBits Amount of payload bits required for the current subframe. + * \return Error code. + */ +INT transportEnc_GetStaticBits( HANDLE_TRANSPORTENC hTp, int auBits ); + +/** + * \brief Close transport encoder. This function assures that all allocated memory is freed. + * \param phTp Pointer to a previously allocated transport encoder handle. + */ +void transportEnc_Close( HANDLE_TRANSPORTENC *phTp ); + +/** + * \brief Write one access unit. + * \param hTp Handle of transport encoder. + * \param total_bits Amount of total access unit bits. + * \param bufferFullness Value of current buffer fullness in bits. + * \param noConsideredChannels Number of bitrate wise considered channels (all minus LFE channels). + * \return Error code. + */ +TRANSPORTENC_ERROR transportEnc_WriteAccessUnit( HANDLE_TRANSPORTENC hTp, + INT total_bits, + int bufferFullness, + int noConsideredChannels ); + +/** + * \brief Inform the transportEnc layer that writing of access unit has finished. This function + * is required to be called when the encoder has finished writing one Access + * one Access Unit for bitstream housekeeping. + * \param hTp Transport handle. + * \param pBits Pointer to an int, where the current amount of frame bits is passed + * and where the current amount of subframe bits is returned. + * + * OR: This integer is modified by the amount of extra bit alignment that may occurr. + * + * \return Error code. + */ +TRANSPORTENC_ERROR transportEnc_EndAccessUnit( HANDLE_TRANSPORTENC hTp, int *pBits); + +/* + * \brief Get a payload frame. + * \param hTpEnc Transport encoder handle. + * \param nBytes Pointer to an int to hold the frame size in bytes. Returns zero + * if currently there is no complete frame for output (number of sub frames > 1). + * \return Error code. + */ +TRANSPORTENC_ERROR transportEnc_GetFrame(HANDLE_TRANSPORTENC hTpEnc, int *nbytes); + +/* ADTS CRC support */ + +/** + * \brief Set current bitstream position as start of a new data region. + * \param hTpEnc Transport encoder handle. + * \param mBits Size in bits of the data region. Set to 0 if it should not be of a fixed size. + * \return Data region ID, which should be used when calling transportEnc_CrcEndReg(). + */ +int transportEnc_CrcStartReg(HANDLE_TRANSPORTENC hTpEnc, int mBits); + +/** + * \brief Set end of data region. + * \param hTpEnc Transport encoder handle. + * \param reg Data region ID, opbtained from transportEnc_CrcStartReg(). + * \return void + */ +void transportEnc_CrcEndReg(HANDLE_TRANSPORTENC hTpEnc, int reg); + +/** + * \brief Get AudioSpecificConfig or StreamMuxConfig from transport encoder handle and write it to dataBuffer. + * \param hTpEnc Transport encoder handle. + * \param cc Pointer to the current and valid configuration contained in a CODER_CONFIG struct. + * \param dataBuffer Bitbuffer holding binary configuration. + * \param confType Pointer to an UINT where the configuration type is returned (0:ASC, 1:SMC). + * \return Error code. + */ +TRANSPORTENC_ERROR transportEnc_GetConf( HANDLE_TRANSPORTENC hTpEnc, + CODER_CONFIG *cc, + FDK_BITSTREAM *dataBuffer, + UINT *confType ); + +/** + * \brief Get information (version among other things) of the transport encoder library. + * \param info Pointer to an allocated LIB_INFO struct. + * \return Error code. + */ +TRANSPORTENC_ERROR transportEnc_GetLibInfo( LIB_INFO *info ); + +#endif /* #ifndef __TPENC_LIB_H__ */ diff --git a/libMpegTPEnc/src/tpenc_adif.cpp b/libMpegTPEnc/src/tpenc_adif.cpp new file mode 100644 index 0000000..06bd30b --- /dev/null +++ b/libMpegTPEnc/src/tpenc_adif.cpp @@ -0,0 +1,182 @@ + +/* ----------------------------------------------------------------------------------------------------------- +Software License for The Fraunhofer FDK AAC Codec Library for Android + +© Copyright 1995 - 2012 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. + All rights reserved. + + 1. INTRODUCTION +The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements +the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio. +This FDK AAC Codec software is intended to be used on a wide variety of Android devices. + +AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual +audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by +independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part +of the MPEG specifications. + +Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer) +may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners +individually for the purpose of encoding or decoding bit streams in products that are compliant with +the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license +these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec +software may already be covered under those patent licenses when it is used for those licensed purposes only. + +Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality, +are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional +applications information and documentation. + +2. COPYRIGHT LICENSE + +Redistribution and use in source and binary forms, with or without modification, are permitted without +payment of copyright license fees provided that you satisfy the following conditions: + +You must retain the complete text of this software license in redistributions of the FDK AAC Codec or +your modifications thereto in source code form. + +You must retain the complete text of this software license in the documentation and/or other materials +provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form. +You must make available free of charge copies of the complete source code of the FDK AAC Codec and your +modifications thereto to recipients of copies in binary form. + +The name of Fraunhofer may not be used to endorse or promote products derived from this library without +prior written permission. + +You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec +software or your modifications thereto. + +Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software +and the date of any change. For modified versions of the FDK AAC Codec, the term +"Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term +"Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android." + +3. NO PATENT LICENSE + +NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer, +ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with +respect to this software. + +You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized +by appropriate patent licenses. + +4. DISCLAIMER + +This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors +"AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties +of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages, +including but not limited to procurement of substitute goods or services; loss of use, data, or profits, +or business interruption, however caused and on any theory of liability, whether in contract, strict +liability, or tort (including negligence), arising in any way out of the use of this software, even if +advised of the possibility of such damage. + +5. CONTACT INFORMATION + +Fraunhofer Institute for Integrated Circuits IIS +Attention: Audio and Multimedia Departments - FDK AAC LL +Am Wolfsmantel 33 +91058 Erlangen, Germany + +www.iis.fraunhofer.de/amm +amm-info@iis.fraunhofer.de +----------------------------------------------------------------------------------------------------------- */ + +/******************************** MPEG Audio Encoder ************************** + + 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..0921712 --- /dev/null +++ b/libMpegTPEnc/src/tpenc_adif.h @@ -0,0 +1,135 @@ + +/* ----------------------------------------------------------------------------------------------------------- +Software License for The Fraunhofer FDK AAC Codec Library for Android + +© Copyright 1995 - 2012 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. + All rights reserved. + + 1. INTRODUCTION +The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements +the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio. +This FDK AAC Codec software is intended to be used on a wide variety of Android devices. + +AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual +audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by +independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part +of the MPEG specifications. + +Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer) +may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners +individually for the purpose of encoding or decoding bit streams in products that are compliant with +the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license +these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec +software may already be covered under those patent licenses when it is used for those licensed purposes only. + +Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality, +are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional +applications information and documentation. + +2. COPYRIGHT LICENSE + +Redistribution and use in source and binary forms, with or without modification, are permitted without +payment of copyright license fees provided that you satisfy the following conditions: + +You must retain the complete text of this software license in redistributions of the FDK AAC Codec or +your modifications thereto in source code form. + +You must retain the complete text of this software license in the documentation and/or other materials +provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form. +You must make available free of charge copies of the complete source code of the FDK AAC Codec and your +modifications thereto to recipients of copies in binary form. + +The name of Fraunhofer may not be used to endorse or promote products derived from this library without +prior written permission. + +You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec +software or your modifications thereto. + +Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software +and the date of any change. For modified versions of the FDK AAC Codec, the term +"Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term +"Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android." + +3. NO PATENT LICENSE + +NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer, +ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with +respect to this software. + +You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized +by appropriate patent licenses. + +4. DISCLAIMER + +This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors +"AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties +of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages, +including but not limited to procurement of substitute goods or services; loss of use, data, or profits, +or business interruption, however caused and on any theory of liability, whether in contract, strict +liability, or tort (including negligence), arising in any way out of the use of this software, even if +advised of the possibility of such damage. + +5. CONTACT INFORMATION + +Fraunhofer Institute for Integrated Circuits IIS +Attention: Audio and Multimedia Departments - FDK AAC LL +Am Wolfsmantel 33 +91058 Erlangen, Germany + +www.iis.fraunhofer.de/amm +amm-info@iis.fraunhofer.de +----------------------------------------------------------------------------------------------------------- */ + +/******************************** MPEG Audio Encoder ************************** + + 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..39460f0 --- /dev/null +++ b/libMpegTPEnc/src/tpenc_adts.cpp @@ -0,0 +1,315 @@ + +/* ----------------------------------------------------------------------------------------------------------- +Software License for The Fraunhofer FDK AAC Codec Library for Android + +© Copyright 1995 - 2012 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. + All rights reserved. + + 1. INTRODUCTION +The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements +the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio. +This FDK AAC Codec software is intended to be used on a wide variety of Android devices. + +AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual +audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by +independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part +of the MPEG specifications. + +Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer) +may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners +individually for the purpose of encoding or decoding bit streams in products that are compliant with +the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license +these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec +software may already be covered under those patent licenses when it is used for those licensed purposes only. + +Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality, +are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional +applications information and documentation. + +2. COPYRIGHT LICENSE + +Redistribution and use in source and binary forms, with or without modification, are permitted without +payment of copyright license fees provided that you satisfy the following conditions: + +You must retain the complete text of this software license in redistributions of the FDK AAC Codec or +your modifications thereto in source code form. + +You must retain the complete text of this software license in the documentation and/or other materials +provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form. +You must make available free of charge copies of the complete source code of the FDK AAC Codec and your +modifications thereto to recipients of copies in binary form. + +The name of Fraunhofer may not be used to endorse or promote products derived from this library without +prior written permission. + +You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec +software or your modifications thereto. + +Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software +and the date of any change. For modified versions of the FDK AAC Codec, the term +"Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term +"Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android." + +3. NO PATENT LICENSE + +NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer, +ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with +respect to this software. + +You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized +by appropriate patent licenses. + +4. DISCLAIMER + +This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors +"AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties +of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages, +including but not limited to procurement of substitute goods or services; loss of use, data, or profits, +or business interruption, however caused and on any theory of liability, whether in contract, strict +liability, or tort (including negligence), arising in any way out of the use of this software, even if +advised of the possibility of such damage. + +5. CONTACT INFORMATION + +Fraunhofer Institute for Integrated Circuits IIS +Attention: Audio and Multimedia Departments - FDK AAC LL +Am Wolfsmantel 33 +91058 Erlangen, Germany + +www.iis.fraunhofer.de/amm +amm-info@iis.fraunhofer.de +----------------------------------------------------------------------------------------------------------- */ + +/******************************** MPEG Audio Encoder ************************** + + 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..9123f1e --- /dev/null +++ b/libMpegTPEnc/src/tpenc_adts.h @@ -0,0 +1,218 @@ + +/* ----------------------------------------------------------------------------------------------------------- +Software License for The Fraunhofer FDK AAC Codec Library for Android + +© Copyright 1995 - 2012 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. + All rights reserved. + + 1. INTRODUCTION +The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements +the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio. +This FDK AAC Codec software is intended to be used on a wide variety of Android devices. + +AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual +audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by +independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part +of the MPEG specifications. + +Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer) +may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners +individually for the purpose of encoding or decoding bit streams in products that are compliant with +the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license +these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec +software may already be covered under those patent licenses when it is used for those licensed purposes only. + +Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality, +are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional +applications information and documentation. + +2. COPYRIGHT LICENSE + +Redistribution and use in source and binary forms, with or without modification, are permitted without +payment of copyright license fees provided that you satisfy the following conditions: + +You must retain the complete text of this software license in redistributions of the FDK AAC Codec or +your modifications thereto in source code form. + +You must retain the complete text of this software license in the documentation and/or other materials +provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form. +You must make available free of charge copies of the complete source code of the FDK AAC Codec and your +modifications thereto to recipients of copies in binary form. + +The name of Fraunhofer may not be used to endorse or promote products derived from this library without +prior written permission. + +You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec +software or your modifications thereto. + +Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software +and the date of any change. For modified versions of the FDK AAC Codec, the term +"Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term +"Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android." + +3. NO PATENT LICENSE + +NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer, +ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with +respect to this software. + +You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized +by appropriate patent licenses. + +4. DISCLAIMER + +This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors +"AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties +of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages, +including but not limited to procurement of substitute goods or services; loss of use, data, or profits, +or business interruption, however caused and on any theory of liability, whether in contract, strict +liability, or tort (including negligence), arising in any way out of the use of this software, even if +advised of the possibility of such damage. + +5. CONTACT INFORMATION + +Fraunhofer Institute for Integrated Circuits IIS +Attention: Audio and Multimedia Departments - FDK AAC LL +Am Wolfsmantel 33 +91058 Erlangen, Germany + +www.iis.fraunhofer.de/amm +amm-info@iis.fraunhofer.de +----------------------------------------------------------------------------------------------------------- */ + +/******************************** MPEG Audio Encoder ************************** + + 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..6200c14 --- /dev/null +++ b/libMpegTPEnc/src/tpenc_asc.cpp @@ -0,0 +1,552 @@ + +/* ----------------------------------------------------------------------------------------------------------- +Software License for The Fraunhofer FDK AAC Codec Library for Android + +© Copyright 1995 - 2012 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. + All rights reserved. + + 1. INTRODUCTION +The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements +the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio. +This FDK AAC Codec software is intended to be used on a wide variety of Android devices. + +AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual +audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by +independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part +of the MPEG specifications. + +Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer) +may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners +individually for the purpose of encoding or decoding bit streams in products that are compliant with +the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license +these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec +software may already be covered under those patent licenses when it is used for those licensed purposes only. + +Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality, +are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional +applications information and documentation. + +2. COPYRIGHT LICENSE + +Redistribution and use in source and binary forms, with or without modification, are permitted without +payment of copyright license fees provided that you satisfy the following conditions: + +You must retain the complete text of this software license in redistributions of the FDK AAC Codec or +your modifications thereto in source code form. + +You must retain the complete text of this software license in the documentation and/or other materials +provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form. +You must make available free of charge copies of the complete source code of the FDK AAC Codec and your +modifications thereto to recipients of copies in binary form. + +The name of Fraunhofer may not be used to endorse or promote products derived from this library without +prior written permission. + +You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec +software or your modifications thereto. + +Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software +and the date of any change. For modified versions of the FDK AAC Codec, the term +"Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term +"Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android." + +3. NO PATENT LICENSE + +NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer, +ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with +respect to this software. + +You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized +by appropriate patent licenses. + +4. DISCLAIMER + +This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors +"AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties +of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages, +including but not limited to procurement of substitute goods or services; loss of use, data, or profits, +or business interruption, however caused and on any theory of liability, whether in contract, strict +liability, or tort (including negligence), arising in any way out of the use of this software, even if +advised of the possibility of such damage. + +5. CONTACT INFORMATION + +Fraunhofer Institute for Integrated Circuits IIS +Attention: Audio and Multimedia Departments - FDK AAC LL +Am Wolfsmantel 33 +91058 Erlangen, Germany + +www.iis.fraunhofer.de/amm +amm-info@iis.fraunhofer.de +----------------------------------------------------------------------------------------------------------- */ + +/***************************** MPEG-4 AAC Encoder ************************** + + Author(s): + Description: + +******************************************************************************/ + +#include "tp_data.h" + +#include "tpenc_lib.h" +#include "tpenc_asc.h" +#include "FDK_bitstream.h" +#include "genericStds.h" + +#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, + CSTpCallBacks *cb + ) +{ + /* ELD specific config */ + if (config->channelMode == MODE_1_1) { + return -1; + } + FDKwriteBits(hBs, (config->samplesPerFrame == 480) ? 1 : 0, 1); + + FDKwriteBits(hBs, (config->flags & CC_VCB11 ) ? 1:0, 1); + FDKwriteBits(hBs, (config->flags & CC_RVLC ) ? 1:0, 1); + FDKwriteBits(hBs, (config->flags & CC_HCR ) ? 1:0, 1); + + FDKwriteBits(hBs, (config->flags & CC_SBR) ? 1:0, 1); /* SBR header flag */ + if ( (config->flags & CC_SBR) ) { + FDKwriteBits(hBs, (config->samplingRate == config->extSamplingRate) ? 0:1, 1); /* Samplerate Flag */ + FDKwriteBits(hBs, (config->flags & CC_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 extFlag = 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: + extFlag = 1; + break; + default: + break; + } + + 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, extFlag, 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, 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..990f74f --- /dev/null +++ b/libMpegTPEnc/src/tpenc_asc.h @@ -0,0 +1,142 @@ + +/* ----------------------------------------------------------------------------------------------------------- +Software License for The Fraunhofer FDK AAC Codec Library for Android + +© Copyright 1995 - 2012 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. + All rights reserved. + + 1. INTRODUCTION +The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements +the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio. +This FDK AAC Codec software is intended to be used on a wide variety of Android devices. + +AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual +audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by +independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part +of the MPEG specifications. + +Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer) +may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners +individually for the purpose of encoding or decoding bit streams in products that are compliant with +the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license +these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec +software may already be covered under those patent licenses when it is used for those licensed purposes only. + +Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality, +are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional +applications information and documentation. + +2. COPYRIGHT LICENSE + +Redistribution and use in source and binary forms, with or without modification, are permitted without +payment of copyright license fees provided that you satisfy the following conditions: + +You must retain the complete text of this software license in redistributions of the FDK AAC Codec or +your modifications thereto in source code form. + +You must retain the complete text of this software license in the documentation and/or other materials +provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form. +You must make available free of charge copies of the complete source code of the FDK AAC Codec and your +modifications thereto to recipients of copies in binary form. + +The name of Fraunhofer may not be used to endorse or promote products derived from this library without +prior written permission. + +You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec +software or your modifications thereto. + +Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software +and the date of any change. For modified versions of the FDK AAC Codec, the term +"Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term +"Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android." + +3. NO PATENT LICENSE + +NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer, +ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with +respect to this software. + +You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized +by appropriate patent licenses. + +4. DISCLAIMER + +This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors +"AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties +of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages, +including but not limited to procurement of substitute goods or services; loss of use, data, or profits, +or business interruption, however caused and on any theory of liability, whether in contract, strict +liability, or tort (including negligence), arising in any way out of the use of this software, even if +advised of the possibility of such damage. + +5. CONTACT INFORMATION + +Fraunhofer Institute for Integrated Circuits IIS +Attention: Audio and Multimedia Departments - FDK AAC LL +Am Wolfsmantel 33 +91058 Erlangen, Germany + +www.iis.fraunhofer.de/amm +amm-info@iis.fraunhofer.de +----------------------------------------------------------------------------------------------------------- */ + +/***************************** MPEG-4 AAC Encoder ************************** + + Author(s): Manuel Jander + Description: Audio Specific Config writer + +******************************************************************************/ + +#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..54fd717 --- /dev/null +++ b/libMpegTPEnc/src/tpenc_latm.cpp @@ -0,0 +1,882 @@ + +/* ----------------------------------------------------------------------------------------------------------- +Software License for The Fraunhofer FDK AAC Codec Library for Android + +© Copyright 1995 - 2012 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. + All rights reserved. + + 1. INTRODUCTION +The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements +the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio. +This FDK AAC Codec software is intended to be used on a wide variety of Android devices. + +AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual +audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by +independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part +of the MPEG specifications. + +Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer) +may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners +individually for the purpose of encoding or decoding bit streams in products that are compliant with +the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license +these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec +software may already be covered under those patent licenses when it is used for those licensed purposes only. + +Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality, +are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional +applications information and documentation. + +2. COPYRIGHT LICENSE + +Redistribution and use in source and binary forms, with or without modification, are permitted without +payment of copyright license fees provided that you satisfy the following conditions: + +You must retain the complete text of this software license in redistributions of the FDK AAC Codec or +your modifications thereto in source code form. + +You must retain the complete text of this software license in the documentation and/or other materials +provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form. +You must make available free of charge copies of the complete source code of the FDK AAC Codec and your +modifications thereto to recipients of copies in binary form. + +The name of Fraunhofer may not be used to endorse or promote products derived from this library without +prior written permission. + +You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec +software or your modifications thereto. + +Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software +and the date of any change. For modified versions of the FDK AAC Codec, the term +"Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term +"Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android." + +3. NO PATENT LICENSE + +NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer, +ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with +respect to this software. + +You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized +by appropriate patent licenses. + +4. DISCLAIMER + +This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors +"AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties +of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages, +including but not limited to procurement of substitute goods or services; loss of use, data, or profits, +or business interruption, however caused and on any theory of liability, whether in contract, strict +liability, or tort (including negligence), arising in any way out of the use of this software, even if +advised of the possibility of such damage. + +5. CONTACT INFORMATION + +Fraunhofer Institute for Integrated Circuits IIS +Attention: Audio and Multimedia Departments - FDK AAC LL +Am Wolfsmantel 33 +91058 Erlangen, Germany + +www.iis.fraunhofer.de/amm +amm-info@iis.fraunhofer.de +----------------------------------------------------------------------------------------------------------- */ + +/***************************** MPEG-4 AAC Encoder ************************** + + Author(s): + Description: + +******************************************************************************/ + +#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..fd95a60 --- /dev/null +++ b/libMpegTPEnc/src/tpenc_latm.h @@ -0,0 +1,264 @@ + +/* ----------------------------------------------------------------------------------------------------------- +Software License for The Fraunhofer FDK AAC Codec Library for Android + +© Copyright 1995 - 2012 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. + All rights reserved. + + 1. INTRODUCTION +The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements +the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio. +This FDK AAC Codec software is intended to be used on a wide variety of Android devices. + +AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual +audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by +independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part +of the MPEG specifications. + +Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer) +may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners +individually for the purpose of encoding or decoding bit streams in products that are compliant with +the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license +these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec +software may already be covered under those patent licenses when it is used for those licensed purposes only. + +Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality, +are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional +applications information and documentation. + +2. COPYRIGHT LICENSE + +Redistribution and use in source and binary forms, with or without modification, are permitted without +payment of copyright license fees provided that you satisfy the following conditions: + +You must retain the complete text of this software license in redistributions of the FDK AAC Codec or +your modifications thereto in source code form. + +You must retain the complete text of this software license in the documentation and/or other materials +provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form. +You must make available free of charge copies of the complete source code of the FDK AAC Codec and your +modifications thereto to recipients of copies in binary form. + +The name of Fraunhofer may not be used to endorse or promote products derived from this library without +prior written permission. + +You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec +software or your modifications thereto. + +Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software +and the date of any change. For modified versions of the FDK AAC Codec, the term +"Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term +"Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android." + +3. NO PATENT LICENSE + +NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer, +ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with +respect to this software. + +You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized +by appropriate patent licenses. + +4. DISCLAIMER + +This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors +"AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties +of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages, +including but not limited to procurement of substitute goods or services; loss of use, data, or profits, +or business interruption, however caused and on any theory of liability, whether in contract, strict +liability, or tort (including negligence), arising in any way out of the use of this software, even if +advised of the possibility of such damage. + +5. CONTACT INFORMATION + +Fraunhofer Institute for Integrated Circuits IIS +Attention: Audio and Multimedia Departments - FDK AAC LL +Am Wolfsmantel 33 +91058 Erlangen, Germany + +www.iis.fraunhofer.de/amm +amm-info@iis.fraunhofer.de +----------------------------------------------------------------------------------------------------------- */ + +/***************************** MPEG-4 AAC Encoder ************************** + + Author(s): + Description: + +******************************************************************************/ + +#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; + + +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..4f3177d --- /dev/null +++ b/libMpegTPEnc/src/tpenc_lib.cpp @@ -0,0 +1,631 @@ + +/* ----------------------------------------------------------------------------------------------------------- +Software License for The Fraunhofer FDK AAC Codec Library for Android + +© Copyright 1995 - 2012 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. + All rights reserved. + + 1. INTRODUCTION +The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements +the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio. +This FDK AAC Codec software is intended to be used on a wide variety of Android devices. + +AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual +audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by +independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part +of the MPEG specifications. + +Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer) +may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners +individually for the purpose of encoding or decoding bit streams in products that are compliant with +the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license +these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec +software may already be covered under those patent licenses when it is used for those licensed purposes only. + +Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality, +are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional +applications information and documentation. + +2. COPYRIGHT LICENSE + +Redistribution and use in source and binary forms, with or without modification, are permitted without +payment of copyright license fees provided that you satisfy the following conditions: + +You must retain the complete text of this software license in redistributions of the FDK AAC Codec or +your modifications thereto in source code form. + +You must retain the complete text of this software license in the documentation and/or other materials +provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form. +You must make available free of charge copies of the complete source code of the FDK AAC Codec and your +modifications thereto to recipients of copies in binary form. + +The name of Fraunhofer may not be used to endorse or promote products derived from this library without +prior written permission. + +You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec +software or your modifications thereto. + +Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software +and the date of any change. For modified versions of the FDK AAC Codec, the term +"Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term +"Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android." + +3. NO PATENT LICENSE + +NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer, +ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with +respect to this software. + +You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized +by appropriate patent licenses. + +4. DISCLAIMER + +This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors +"AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties +of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages, +including but not limited to procurement of substitute goods or services; loss of use, data, or profits, +or business interruption, however caused and on any theory of liability, whether in contract, strict +liability, or tort (including negligence), arising in any way out of the use of this software, even if +advised of the possibility of such damage. + +5. CONTACT INFORMATION + +Fraunhofer Institute for Integrated Circuits IIS +Attention: Audio and Multimedia Departments - FDK AAC LL +Am Wolfsmantel 33 +91058 Erlangen, Germany + +www.iis.fraunhofer.de/amm +amm-info@iis.fraunhofer.de +----------------------------------------------------------------------------------------------------------- */ + +/************************** MPEG-4 Transport Encoder ************************ + + Author(s): Manuel Jander + Description: MPEG Transport encode + +******************************************************************************/ + +#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..5b9a425 --- /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 1 +#define TP_LIB_TITLE "MPEG Transport" +#define TP_LIB_BUILD_DATE __DATE__ +#define TP_LIB_BUILD_TIME __TIME__ |