diff options
Diffstat (limited to 'libMpegTPEnc/src')
-rw-r--r-- | libMpegTPEnc/src/tp_version.h | 118 | ||||
-rw-r--r-- | libMpegTPEnc/src/tpenc_adif.cpp | 198 | ||||
-rw-r--r-- | libMpegTPEnc/src/tpenc_adif.h | 141 | ||||
-rw-r--r-- | libMpegTPEnc/src/tpenc_adts.cpp | 246 | ||||
-rw-r--r-- | libMpegTPEnc/src/tpenc_adts.h | 190 | ||||
-rw-r--r-- | libMpegTPEnc/src/tpenc_asc.cpp | 1026 | ||||
-rw-r--r-- | libMpegTPEnc/src/tpenc_asc.h | 151 | ||||
-rw-r--r-- | libMpegTPEnc/src/tpenc_latm.cpp | 933 | ||||
-rw-r--r-- | libMpegTPEnc/src/tpenc_latm.h | 274 | ||||
-rw-r--r-- | libMpegTPEnc/src/tpenc_lib.cpp | 553 | ||||
-rw-r--r-- | libMpegTPEnc/src/version | 13 |
11 files changed, 2191 insertions, 1652 deletions
diff --git a/libMpegTPEnc/src/tp_version.h b/libMpegTPEnc/src/tp_version.h new file mode 100644 index 0000000..9f1aa22 --- /dev/null +++ b/libMpegTPEnc/src/tp_version.h @@ -0,0 +1,118 @@ +/* ----------------------------------------------------------------------------- +Software License for The Fraunhofer FDK AAC Codec Library for Android + +© Copyright 1995 - 2018 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 transport format encoder library ********************* + + Author(s): + + Description: + +*******************************************************************************/ + +#if !defined(TP_VERSION_H) +#define TP_VERSION_H + +/* library info */ +#define TP_LIB_VL0 3 +#define TP_LIB_VL1 0 +#define TP_LIB_VL2 0 +#define TP_LIB_TITLE "MPEG Transport" +#ifdef __ANDROID__ +#define TP_LIB_BUILD_DATE "" +#define TP_LIB_BUILD_TIME "" +#else +#define TP_LIB_BUILD_DATE __DATE__ +#define TP_LIB_BUILD_TIME __TIME__ +#endif +#endif /* !defined(TP_VERSION_H) */ diff --git a/libMpegTPEnc/src/tpenc_adif.cpp b/libMpegTPEnc/src/tpenc_adif.cpp index b48a32e..b281eff 100644 --- a/libMpegTPEnc/src/tpenc_adif.cpp +++ b/libMpegTPEnc/src/tpenc_adif.cpp @@ -1,74 +1,85 @@ - -/* ----------------------------------------------------------------------------------------------------------- +/* ----------------------------------------------------------------------------- Software License for The Fraunhofer FDK AAC Codec Library for Android -© Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. - All rights reserved. +© Copyright 1995 - 2018 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. +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: +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 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 +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. +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. +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." +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. +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. +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. +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 @@ -79,104 +90,97 @@ Am Wolfsmantel 33 www.iis.fraunhofer.de/amm amm-info@iis.fraunhofer.de ------------------------------------------------------------------------------------------------------------ */ +----------------------------------------------------------------------------- */ + +/******************* MPEG transport format encoder library ********************* -/******************************** MPEG Audio Encoder ************************** + Author(s): - contents/description: ADIF Transport Headers writing + 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) -{ +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; + const char adifId[5] = "ADIF"; + const int copyRightIdPresent = 0; + const int originalCopy = 0; + const int home = 0; + int err = 0; int i; - INT sampleRate = adif->samplingRate; INT totalBitRate = adif->bitRate; - if (adif->headerWritten) - return 0; + 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; + 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, adifId[0], 8); + FDKwriteBits(hBs, adifId[1], 8); + FDKwriteBits(hBs, adifId[2], 8); + FDKwriteBits(hBs, adifId[3], 8); + FDKwriteBits(hBs, copyRightIdPresent ? 1 : 0, 1); - FDKwriteBits(hBs, copyRightIdPresent ? 1:0,1); - - if(copyRightIdPresent) { - for(i=0;i<72;i++) { - FDKwriteBits(hBs,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); + 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) { + if (!adif->bVariableRate) { FDKwriteBits(hBs, adif_buffer_fullness, 20); } - /* Write PCE */ - transportEnc_writePCE(hBs, adif->cm, sampleRate, adif->instanceTag, adif->profile, 0, 0, alignAnchor); + transportEnc_writePCE(hBs, adif->cm, adif->samplingRate, adif->instanceTag, + adif->profile, adif->matrixMixdownA, + (adif->pseudoSurroundEnable) ? 1 : 0, alignAnchor); - return 0; + return err; } -int adifWrite_GetHeaderBits(ADIF_INFO *adif) -{ +int adifWrite_GetHeaderBits(ADIF_INFO *adif) { /* ADIF definitions */ - const int copyRightIdPresent=0; + const int copyRightIdPresent = 0; - if (adif->headerWritten) - return 0; + if (adif->headerWritten) return 0; int bits = 0; - bits += 8*4; /* ADIF ID */ + bits += 8 * 4; /* ADIF ID */ bits += 1; /* Copyright present */ - if (copyRightIdPresent) - bits += 72; /* Copyright ID */ + if (copyRightIdPresent) bits += 72; /* Copyright ID */ bits += 26; bits += 4; /* Number of PCE's */ - if(!adif->bVariableRate) { + if (!adif->bVariableRate) { bits += 20; } /* write PCE */ - bits = transportEnc_GetPCEBits(adif->cm, 0, bits); + bits = transportEnc_GetPCEBits(adif->cm, adif->matrixMixdownA, bits); return bits; } - diff --git a/libMpegTPEnc/src/tpenc_adif.h b/libMpegTPEnc/src/tpenc_adif.h index d590354..e001afc 100644 --- a/libMpegTPEnc/src/tpenc_adif.h +++ b/libMpegTPEnc/src/tpenc_adif.h @@ -1,74 +1,85 @@ - -/* ----------------------------------------------------------------------------------------------------------- +/* ----------------------------------------------------------------------------- Software License for The Fraunhofer FDK AAC Codec Library for Android -© Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. - All rights reserved. +© Copyright 1995 - 2018 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. +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: +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 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 +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. +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. +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." +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. +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. +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. +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 @@ -79,14 +90,15 @@ Am Wolfsmantel 33 www.iis.fraunhofer.de/amm amm-info@iis.fraunhofer.de ------------------------------------------------------------------------------------------------------------ */ +----------------------------------------------------------------------------- */ + +/******************* MPEG transport format encoder library ********************* -/******************************** MPEG Audio Encoder ************************** + Author(s): Alex Goeschel - Initial author: Alex Goeschel - contents/description: Transport Headers support + Description: Transport Headers support -******************************************************************************/ +*******************************************************************************/ #ifndef TPENC_ADIF_H #define TPENC_ADIF_H @@ -104,6 +116,9 @@ typedef struct { int bVariableRate; int instanceTag; int headerWritten; + int matrixMixdownA; + int pseudoSurroundEnable; + } ADIF_INFO; /** @@ -115,21 +130,17 @@ typedef struct { * * \return 0 on success */ -int adifWrite_EncodeHeader( - ADIF_INFO *adif, - HANDLE_FDK_BITSTREAM hBitStream, - INT adif_buffer_fullness - ); +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 + * \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 ); +int adifWrite_GetHeaderBits(ADIF_INFO *adif); #endif /* TPENC_ADIF_H */ - diff --git a/libMpegTPEnc/src/tpenc_adts.cpp b/libMpegTPEnc/src/tpenc_adts.cpp index f4f3178..3f7e62c 100644 --- a/libMpegTPEnc/src/tpenc_adts.cpp +++ b/libMpegTPEnc/src/tpenc_adts.cpp @@ -1,74 +1,85 @@ - -/* ----------------------------------------------------------------------------------------------------------- +/* ----------------------------------------------------------------------------- Software License for The Fraunhofer FDK AAC Codec Library for Android -© Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. - All rights reserved. +© Copyright 1995 - 2018 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. +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: +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 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 +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. +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. +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." +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. +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. +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. +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 @@ -79,48 +90,43 @@ Am Wolfsmantel 33 www.iis.fraunhofer.de/amm amm-info@iis.fraunhofer.de ------------------------------------------------------------------------------------------------------------ */ +----------------------------------------------------------------------------- */ -/******************************** MPEG Audio Encoder ************************** +/******************* MPEG transport format encoder library ********************* - Initial author: Alex Groeschel - contents/description: ADTS Transport Headers support + Author(s): Alex Groeschel -******************************************************************************/ + Description: ADTS Transport Headers support -#include "tpenc_adts.h" +*******************************************************************************/ +#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 */ - ) -{ + 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) ); + 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) - { + 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 adtsWrite_GetHeaderBits(HANDLE_ADTS hAdts) { int bits = 0; if (hAdts->currentBlock == 0) { @@ -129,14 +135,15 @@ int adtsWrite_GetHeaderBits( HANDLE_ADTS hAdts ) if (!hAdts->protection_absent) { /* Add header/ single raw data block CRC bits */ bits += 16; - if (hAdts->num_raw_blocks>0) { + if (hAdts->num_raw_blocks > 0) { /* Add bits of raw data block position markers */ - bits += (hAdts->num_raw_blocks)*16; + 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. */ + 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; } @@ -145,13 +152,10 @@ int adtsWrite_GetHeaderBits( HANDLE_ADTS hAdts ) return bits; } -INT adtsWrite_Init(HANDLE_ADTS hAdts, CODER_CONFIG *config) -{ +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 ) { + if (config->nSubFrames < 1 || config->nSubFrames > 4 || + (int)config->aot > 4 || (int)config->aot < 1) { return -1; } @@ -161,41 +165,38 @@ INT adtsWrite_Init(HANDLE_ADTS hAdts, CODER_CONFIG *config) } else { hAdts->mpeg_id = 1; /* MPEG 2 */ } - hAdts->layer=0; - hAdts->protection_absent = ! (config->flags & CC_PROTECTION); + 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_index = getSamplingRateIndex(config->samplingRate, 4); hAdts->sample_freq = config->samplingRate; - hAdts->private_bit=0; + hAdts->private_bit = 0; hAdts->channel_mode = config->channelMode; - hAdts->original=0; - hAdts->home=0; + hAdts->original = 0; + hAdts->home = 0; /* variable header */ - hAdts->copyright_id=0; - hAdts->copyright_start=0; + hAdts->copyright_id = 0; + hAdts->copyright_start = 0; - hAdts->num_raw_blocks=config->nSubFrames-1; /* 0 means 1 raw data block */ + hAdts->num_raw_blocks = config->nSubFrames - 1; /* 0 means 1 raw data block */ + + hAdts->channel_config_zero = config->channelConfigZero; 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 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 */ + FDK_ASSERT(((frame_length + hAdts->headerBits) / 8) < 0x2000); /*13 bit*/ + FDK_ASSERT(buffer_fullness < 0x800); /* 11 bit */ if (!hAdts->protection_absent) { FDKcrcReset(&hAdts->crcInfo); @@ -208,8 +209,7 @@ int adtsWrite_EncodeHeader(HANDLE_ADTS hAdts, hAdts->subFrameStartBit = FDKgetValidBits(hBitStream); /* Skip new header if this is raw data block 1..n */ - if (hAdts->currentBlock == 0) - { + if (hAdts->currentBlock == 0) { FDKresetBitbuffer(hBitStream, BS_WRITER); if (hAdts->num_raw_blocks == 0) { @@ -224,24 +224,27 @@ int adtsWrite_EncodeHeader(HANDLE_ADTS hAdts, 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, + getChannelConfig(hAdts->channel_mode, hAdts->channel_config_zero), 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, (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. */ + /* 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++) { + for (i = 0; i < hAdts->num_raw_blocks; i++) { FDKwriteBits(hBitStream, 0, 16); } } @@ -252,14 +255,13 @@ int adtsWrite_EncodeHeader(HANDLE_ADTS hAdts, return 0; } -void adtsWrite_EndRawDataBlock(HANDLE_ADTS hAdts, - HANDLE_FDK_BITSTREAM hBs, - int *pBits) -{ +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); + FDKinitBitStream(&bsWriter, hBs->hBitBuf.Buffer, hBs->hBitBuf.bufSize, 0, + BS_WRITER); FDKpushFor(&bsWriter, 56); if (hAdts->num_raw_blocks == 0) { @@ -272,32 +274,35 @@ void adtsWrite_EndRawDataBlock(HANDLE_ADTS hAdts, /* 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); + 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) { + 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); + 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); + 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); + FDKpushFor(&bsWriter, 11 + 2 + (hAdts->num_raw_blocks) * 16); FDKcrcEndReg(&hAdts->crcInfo, &bsWriter, crcIndex); FDKwriteBits(&bsWriter, FDKcrcGetCRC(&hAdts->crcInfo), 16); } @@ -312,4 +317,3 @@ void adtsWrite_EndRawDataBlock(HANDLE_ADTS hAdts, } hAdts->currentBlock++; } - diff --git a/libMpegTPEnc/src/tpenc_adts.h b/libMpegTPEnc/src/tpenc_adts.h index c12c7c7..fe86306 100644 --- a/libMpegTPEnc/src/tpenc_adts.h +++ b/libMpegTPEnc/src/tpenc_adts.h @@ -1,74 +1,85 @@ - -/* ----------------------------------------------------------------------------------------------------------- +/* ----------------------------------------------------------------------------- Software License for The Fraunhofer FDK AAC Codec Library for Android -© Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. - All rights reserved. +© Copyright 1995 - 2018 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. +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: +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 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 +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. +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. +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." +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. +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. +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. +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 @@ -79,20 +90,19 @@ Am Wolfsmantel 33 www.iis.fraunhofer.de/amm amm-info@iis.fraunhofer.de ------------------------------------------------------------------------------------------------------------ */ +----------------------------------------------------------------------------- */ + +/******************* MPEG transport format encoder library ********************* -/******************************** MPEG Audio Encoder ************************** + Author(s): Alex Groeschel - Initial author: Alex Groeschel - contents/description: ADTS Transport writer + Description: ADTS Transport writer -******************************************************************************/ +*******************************************************************************/ #ifndef TPENC_ADTS_H #define TPENC_ADTS_H - - #include "tp_data.h" #include "FDK_crc.h" @@ -114,9 +124,11 @@ typedef struct { 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 */ + UCHAR channel_config_zero; + 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; @@ -131,10 +143,7 @@ typedef STRUCT_ADTS *HANDLE_ADTS; * * \return 0 in case of success. */ -INT adtsWrite_Init( - HANDLE_ADTS hAdts, - CODER_CONFIG *config - ); +INT adtsWrite_Init(HANDLE_ADTS hAdts, CODER_CONFIG *config); /** * \brief Get the total bit overhead caused by ADTS @@ -143,7 +152,7 @@ INT adtsWrite_Init( * * \return Amount of additional bits required for the current raw data block */ -int adtsWrite_GetHeaderBits( HANDLE_ADTS hAdts ); +int adtsWrite_GetHeaderBits(HANDLE_ADTS hAdts); /** * \brief Write an ADTS header into the given bitstream. May not write a header @@ -156,47 +165,36 @@ int adtsWrite_GetHeaderBits( HANDLE_ADTS hAdts ); * * \return 0 in case of success. */ -INT adtsWrite_EncodeHeader( - HANDLE_ADTS hAdts, - HANDLE_FDK_BITSTREAM hBitStream, - int bufferFullness, - int frame_length - ); +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. + * \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 - ); - +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. + * 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 + * \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 - ); +int adtsWrite_CrcStartReg(HANDLE_ADTS pAdts, HANDLE_FDK_BITSTREAM hBs, + int mBits); /** * \brief Ends CRC region identified by reg @@ -205,14 +203,6 @@ int adtsWrite_CrcStartReg( * \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 - ); - - - +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 index bc4302e..ce4e364 100644 --- a/libMpegTPEnc/src/tpenc_asc.cpp +++ b/libMpegTPEnc/src/tpenc_asc.cpp @@ -1,74 +1,85 @@ - -/* ----------------------------------------------------------------------------------------------------------- +/* ----------------------------------------------------------------------------- Software License for The Fraunhofer FDK AAC Codec Library for Android -© Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. - All rights reserved. +© Copyright 1995 - 2018 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. +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: +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 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 +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. +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. +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." +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. +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. +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. +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 @@ -79,14 +90,15 @@ Am Wolfsmantel 33 www.iis.fraunhofer.de/amm amm-info@iis.fraunhofer.de ------------------------------------------------------------------------------------------------------------ */ +----------------------------------------------------------------------------- */ -/***************************** MPEG-4 AAC Encoder ************************** +/******************* MPEG transport format encoder library ********************* Author(s): + Description: -******************************************************************************/ +*******************************************************************************/ #include "tp_data.h" @@ -95,70 +107,162 @@ amm-info@iis.fraunhofer.de #include "FDK_bitstream.h" #include "genericStds.h" -#define PCE_MAX_ELEMENTS 8 +#include "FDK_crc.h" + +#define PCE_HEIGHT_EXT_SYNC (0xAC) +#define HEIGHT_NORMAL 0 +#define HEIGHT_TOP 1 +#define HEIGHT_BOTTOM 2 +#define MAX_FRONT_ELEMENTS 8 +#define MAX_SIDE_ELEMENTS 3 +#define MAX_BACK_ELEMENTS 4 /** - * Describe a PCE based on placed channel elements and element type sequence. + * Describe additional PCE height information for front, side and back channel + * elements. */ typedef struct { + UCHAR + num_front_height_channel_elements[2]; /*!< Number of front channel + elements in top [0] and bottom + [1] plane. */ + UCHAR num_side_height_channel_elements[2]; /*!< Number of side channel + elements in top [0] and bottom + [1] plane. */ + UCHAR num_back_height_channel_elements[2]; /*!< Number of back channel + elements in top [0] and bottom + [1] plane. */ +} PCE_HEIGHT_NUM; - 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) */ +/** + * 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. */ + const MP4_ELEMENT_ID + *pEl_type; /*!< List contains sequence describing the elements + in present channel mode. (MPEG order) */ + const PCE_HEIGHT_NUM *pHeight_num; } 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_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 3 elements (SCE,CPE,CPE) are front channel elements. - * - Next element (CPE) is a back channel element. - * - Last element (LFE) is a lfe channel element. + * The following arrays provide the IDs of the consecutive elements for each + * mode. */ -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, { 3, 0, 1, 1, { ID_SCE, ID_CPE, ID_CPE, ID_CPE, ID_LFE, ID_NONE, ID_NONE, ID_NONE } } }, - +static const MP4_ELEMENT_ID elType_1[] = {ID_SCE}; +static const MP4_ELEMENT_ID elType_2[] = {ID_CPE}; +static const MP4_ELEMENT_ID elType_1_2[] = {ID_SCE, ID_CPE}; +static const MP4_ELEMENT_ID elType_1_2_1[] = {ID_SCE, ID_CPE, ID_SCE}; +static const MP4_ELEMENT_ID elType_1_2_2[] = {ID_SCE, ID_CPE, ID_CPE}; +static const MP4_ELEMENT_ID elType_1_2_2_1[] = {ID_SCE, ID_CPE, ID_CPE, ID_LFE}; +static const MP4_ELEMENT_ID elType_1_2_2_2_1[] = {ID_SCE, ID_CPE, ID_CPE, + ID_CPE, ID_LFE}; +static const MP4_ELEMENT_ID elType_6_1[] = {ID_SCE, ID_CPE, ID_CPE, ID_SCE, + ID_LFE}; +static const MP4_ELEMENT_ID elType_7_1_back[] = {ID_SCE, ID_CPE, ID_CPE, ID_CPE, + ID_LFE}; +static const MP4_ELEMENT_ID elType_7_1_top_front[] = {ID_SCE, ID_CPE, ID_CPE, + ID_LFE, ID_CPE}; +static const MP4_ELEMENT_ID elType_7_1_rear_surround[] = { + ID_SCE, ID_CPE, ID_CPE, ID_CPE, ID_LFE}; +static const MP4_ELEMENT_ID elType_7_1_front_center[] = {ID_SCE, ID_CPE, ID_CPE, + ID_CPE, ID_LFE}; - { 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 } } }, - - { MODE_7_1_REAR_SURROUND, { 2, 0, 2, 1, { ID_SCE, ID_CPE, ID_CPE, ID_CPE, ID_LFE, ID_NONE, ID_NONE, ID_NONE } } }, - { MODE_7_1_FRONT_CENTER, { 3, 0, 1, 1, { ID_SCE, ID_CPE, ID_CPE, ID_CPE, ID_LFE, ID_NONE, ID_NONE, ID_NONE } } }, +/** + * The following arrays provide information on how many front, side and back + * elements are assigned to the top or bottom plane for each mode that comprises + * height information. + */ +static const PCE_HEIGHT_NUM heightNum_7_1_top_front = {{1, 0}, {0, 0}, {0, 0}}; +/** + * \brief Table contains all supported channel modes and according PCE + configuration description. + * + * The mode identifier is followed by the number of front, side, back, and LFE + elements. + * These are followed by a pointer to the IDs of the consecutive elements + (ID_SCE, ID_CPE, ID_LFE). + * + * For some modes (MODE_7_1_TOP_FRONT and MODE_22_2) additional height + information is transmitted. + * In this case the additional pointer provides information on how many front, + side and back elements + * are assigned to the top or bottom plane.The elements are arranged in the + following order: normal height (front, side, back, LFE), top height (front, + side, back), bottom height (front, side, back). + * + * + * E.g. MODE_7_1_TOP_FRONT means: + * - 3 elements are front channel elements. + * - 0 elements are side channel elements. + * - 1 element is back channel element. + * - 1 element is an LFE channel element. + * - the element order is ID_SCE, ID_CPE, ID_CPE, + ID_LFE, ID_CPE. + * - 1 of the front elements is in the top plane. + * + * This leads to the following mapping for the cconsecutive elements in the + MODE_7_1_TOP_FRONT bitstream: + * - ID_SCE -> normal height front, + - ID_CPE -> normal height front, + - ID_CPE -> normal height back, + - ID_LFE -> normal height LFE, + - ID_CPE -> top height front. + */ +static const CHANNEL_CONFIGURATION pceConfigTab[] = { + {MODE_1, + {1, 0, 0, 0, elType_1, + NULL}}, /* don't transmit height information in this mode */ + {MODE_2, + {1, 0, 0, 0, elType_2, + NULL}}, /* don't transmit height information in this mode */ + {MODE_1_2, + {2, 0, 0, 0, elType_1_2, + NULL}}, /* don't transmit height information in this mode */ + {MODE_1_2_1, + {2, 0, 1, 0, elType_1_2_1, + NULL}}, /* don't transmit height information in this mode */ + {MODE_1_2_2, + {2, 0, 1, 0, elType_1_2_2, + NULL}}, /* don't transmit height information in this mode */ + {MODE_1_2_2_1, + {2, 0, 1, 1, elType_1_2_2_1, + NULL}}, /* don't transmit height information in this mode */ + {MODE_1_2_2_2_1, + {3, 0, 1, 1, elType_1_2_2_2_1, + NULL}}, /* don't transmit height information in this mode */ + + {MODE_6_1, + {2, 0, 2, 1, elType_6_1, + NULL}}, /* don't transmit height information in this mode */ + {MODE_7_1_BACK, + {2, 0, 2, 1, elType_7_1_back, + NULL}}, /* don't transmit height information in this mode */ + {MODE_7_1_TOP_FRONT, + {3, 0, 1, 1, elType_7_1_top_front, &heightNum_7_1_top_front}}, + + {MODE_7_1_REAR_SURROUND, + {2, 0, 2, 1, elType_7_1_rear_surround, + NULL}}, /* don't transmit height information in this mode */ + {MODE_7_1_FRONT_CENTER, + {3, 0, 1, 1, elType_7_1_front_center, + NULL}} /* don't transmit height information in this mode */ }; - /** * \brief Get program config element description for existing channel mode. * @@ -168,123 +272,260 @@ static const CHANNEL_CONFIGURATION pceConfigTab[] = * - Pointer to PCE_CONFIGURATION entry, on success. * - NULL, on failure. */ -static const PCE_CONFIGURATION* getPceEntry( - const CHANNEL_MODE channel_mode - ) -{ +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++) { + for (i = 0; i < (sizeof(pceConfigTab) / sizeof(CHANNEL_CONFIGURATION)); i++) { if (pceConfigTab[i].channel_mode == channel_mode) { pce_config = &pceConfigTab[i].pce_configuration; + break; } } return pce_config; } -int getChannelConfig( CHANNEL_MODE channel_mode ) -{ +int getChannelConfig(const CHANNEL_MODE channel_mode, + const UCHAR channel_config_zero) { 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; + if (channel_config_zero != 0) { + chan_config = 0; + } else { + 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; + case MODE_6_1: + chan_config = 11; + break; + case MODE_7_1_BACK: + chan_config = 12; + break; + case MODE_7_1_TOP_FRONT: + chan_config = 14; + break; + default: + chan_config = 0; + } } return chan_config; } -CHANNEL_MODE transportEnc_GetChannelMode( int noChannels ) -{ +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*/ + 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 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); + const PCE_CONFIGURATION *config = NULL; + const MP4_ELEMENT_ID *pEl_list = NULL; + UCHAR cpeCnt = 0, sceCnt = 0, lfeCnt = 0, frntCnt = 0, sdCnt = 0, bckCnt = 0, + isCpe = 0, tag = 0, normalFrontEnd = 0, normalSideEnd = 0, + normalBackEnd = 0, topFrontEnd = 0, topSideEnd = 0, topBackEnd = 0, + bottomFrontEnd = 0, bottomSideEnd = 0; +#ifdef FDK_ASSERT_ENABLE + UCHAR bottomBackEnd = 0; +#endif + enum elementDepth { FRONT, SIDE, BACK } elDepth; + + sampleRateIndex = getSamplingRateIndex(sampleRate, 4); if (sampleRateIndex == 15) { return -1; } - if ((config=getPceEntry(channelMode))==NULL) { + if ((config = getPceEntry(channelMode)) == NULL) { return -1; } - /* Pointer to first element in element list. */ - pEl_list = &config->el_list[0]; + FDK_ASSERT(config->num_front_channel_elements <= MAX_FRONT_ELEMENTS); + FDK_ASSERT(config->num_side_channel_elements <= MAX_SIDE_ELEMENTS); + FDK_ASSERT(config->num_back_channel_elements <= MAX_BACK_ELEMENTS); + + UCHAR frontIsCpe[MAX_FRONT_ELEMENTS] = {0}, + frontTag[MAX_FRONT_ELEMENTS] = {0}, sideIsCpe[MAX_SIDE_ELEMENTS] = {0}, + sideTag[MAX_SIDE_ELEMENTS] = {0}, backIsCpe[MAX_BACK_ELEMENTS] = {0}, + backTag[MAX_BACK_ELEMENTS] = {0}; + + /* Write general information */ + + 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) ? 1 : 0, + 1); /* pseudo_surround_enable */ + } else { + FDKwriteBits(hBs, 0, 1); /* Matrix mixdown not present */ + } - FDKwriteBits(hBs, instanceTagPCE, 4); /* Element instance tag */ - FDKwriteBits(hBs, profile, 2); /* Object type */ - FDKwriteBits(hBs, sampleRateIndex, 4); /* Sample rate index*/ + if (config->pHeight_num != NULL) { + /* we have up to three different height levels, and in each height level we + * may have front, side and back channels. We need to know where each + * section ends to correctly count the tags */ + normalFrontEnd = config->num_front_channel_elements - + config->pHeight_num->num_front_height_channel_elements[0] - + config->pHeight_num->num_front_height_channel_elements[1]; + normalSideEnd = normalFrontEnd + config->num_side_channel_elements - + config->pHeight_num->num_side_height_channel_elements[0] - + config->pHeight_num->num_side_height_channel_elements[1]; + normalBackEnd = normalSideEnd + config->num_back_channel_elements - + config->pHeight_num->num_back_height_channel_elements[0] - + config->pHeight_num->num_back_height_channel_elements[1]; + + topFrontEnd = + normalBackEnd + config->num_lfe_channel_elements + + config->pHeight_num->num_front_height_channel_elements[0]; /* only + normal + height + LFEs + assumed */ + topSideEnd = + topFrontEnd + config->pHeight_num->num_side_height_channel_elements[0]; + topBackEnd = + topSideEnd + config->pHeight_num->num_back_height_channel_elements[0]; + + bottomFrontEnd = + topBackEnd + config->pHeight_num->num_front_height_channel_elements[1]; + bottomSideEnd = bottomFrontEnd + + config->pHeight_num->num_side_height_channel_elements[1]; +#ifdef FDK_ASSERT_ENABLE + bottomBackEnd = bottomSideEnd + + config->pHeight_num->num_back_height_channel_elements[1]; +#endif + + } else { + /* we have only one height level, so we don't care about top or bottom */ + normalFrontEnd = config->num_front_channel_elements; + normalSideEnd = normalFrontEnd + config->num_side_channel_elements; + normalBackEnd = normalSideEnd + config->num_back_channel_elements; + } - 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 */ + /* assign cpe and tag information to either front, side or back channels */ - 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 */ + pEl_list = config->pEl_type; - 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)?1:0, 1); /* pseudo_surround_enable */ - } - else { - FDKwriteBits(hBs, 0, 1); /* Matrix mixdown not present */ + for (i = 0; i < config->num_front_channel_elements + + config->num_side_channel_elements + + config->num_back_channel_elements + + config->num_lfe_channel_elements; + i++) { + if (*pEl_list == ID_LFE) { + pEl_list++; + continue; + } + isCpe = (*pEl_list++ == ID_CPE) ? 1 : 0; + tag = (isCpe) ? cpeCnt++ : sceCnt++; + + if (i < normalFrontEnd) + elDepth = FRONT; + else if (i < normalSideEnd) + elDepth = SIDE; + else if (i < normalBackEnd) + elDepth = BACK; + else if (i < topFrontEnd) + elDepth = FRONT; + else if (i < topSideEnd) + elDepth = SIDE; + else if (i < topBackEnd) + elDepth = BACK; + else if (i < bottomFrontEnd) + elDepth = FRONT; + else if (i < bottomSideEnd) + elDepth = SIDE; + else { + elDepth = BACK; + FDK_ASSERT(i < bottomBackEnd); /* won't fail if implementation of pce + configuration table is correct */ + } + + switch (elDepth) { + case FRONT: + FDK_ASSERT(frntCnt < config->num_front_channel_elements); + frontIsCpe[frntCnt] = isCpe; + frontTag[frntCnt++] = tag; + break; + case SIDE: + FDK_ASSERT(sdCnt < config->num_side_channel_elements); + sideIsCpe[sdCnt] = isCpe; + sideTag[sdCnt++] = tag; + break; + case BACK: + FDK_ASSERT(bckCnt < config->num_back_channel_elements); + backIsCpe[bckCnt] = isCpe; + backTag[bckCnt++] = tag; + break; + } } - 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.*/ + /* Write front channel isCpe and tags */ + for (i = 0; i < config->num_front_channel_elements; i++) { + FDKwriteBits(hBs, frontIsCpe[i], 1); + FDKwriteBits(hBs, frontTag[i], 4); } - 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.*/ + /* Write side channel isCpe and tags */ + for (i = 0; i < config->num_side_channel_elements; i++) { + FDKwriteBits(hBs, sideIsCpe[i], 1); + FDKwriteBits(hBs, sideTag[i], 4); } - 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.*/ + /* Write back channel isCpe and tags */ + for (i = 0; i < config->num_back_channel_elements; i++) { + FDKwriteBits(hBs, backIsCpe[i], 1); + FDKwriteBits(hBs, backTag[i], 4); } - for(i=0; i<config->num_lfe_channel_elements; i++) { - FDKwriteBits(hBs, lfeCnt++, 4); /* LFE channel Instance Tag. */ + /* Write LFE information */ + for (i = 0; i < config->num_lfe_channel_elements; i++) { + FDKwriteBits(hBs, lfeCnt++, 4); /* LFE channel Instance Tag. */ } /* - num_valid_cc_elements always 0. @@ -294,167 +535,348 @@ int transportEnc_writePCE(HANDLE_FDK_BITSTREAM hBs, 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. */ + FDKbyteAlign(hBs, alignAnchor); /* Alignment */ + + /* Write comment information */ + + if (config->pHeight_num != NULL) { + /* embed height information in comment field */ + + INT commentBytes = + 1 /* PCE_HEIGHT_EXT_SYNC */ + + ((((config->num_front_channel_elements + + config->num_side_channel_elements + + config->num_back_channel_elements) + << 1) + + 7) >> + 3) /* 2 bit height info per element, round up to full bytes */ + + 1; /* CRC */ + + FDKwriteBits(hBs, commentBytes, 8); /* comment size. */ + + FDK_CRCINFO crcInfo; /* CRC state info */ + INT crcReg; + + FDKcrcInit(&crcInfo, 0x07, 0xFF, 8); + crcReg = FDKcrcStartReg(&crcInfo, hBs, 0); + + FDKwriteBits(hBs, PCE_HEIGHT_EXT_SYNC, 8); /* indicate height extension */ + + /* front channel height information */ + for (i = 0; + i < config->num_front_channel_elements - + config->pHeight_num->num_front_height_channel_elements[0] - + config->pHeight_num->num_front_height_channel_elements[1]; + i++) + FDKwriteBits(hBs, HEIGHT_NORMAL, 2); + for (i = 0; i < config->pHeight_num->num_front_height_channel_elements[0]; + i++) + FDKwriteBits(hBs, HEIGHT_TOP, 2); + for (i = 0; i < config->pHeight_num->num_front_height_channel_elements[1]; + i++) + FDKwriteBits(hBs, HEIGHT_BOTTOM, 2); + + /* side channel height information */ + for (i = 0; + i < config->num_side_channel_elements - + config->pHeight_num->num_side_height_channel_elements[0] - + config->pHeight_num->num_side_height_channel_elements[1]; + i++) + FDKwriteBits(hBs, HEIGHT_NORMAL, 2); + for (i = 0; i < config->pHeight_num->num_side_height_channel_elements[0]; + i++) + FDKwriteBits(hBs, HEIGHT_TOP, 2); + for (i = 0; i < config->pHeight_num->num_side_height_channel_elements[1]; + i++) + FDKwriteBits(hBs, HEIGHT_BOTTOM, 2); + + /* back channel height information */ + for (i = 0; + i < config->num_back_channel_elements - + config->pHeight_num->num_back_height_channel_elements[0] - + config->pHeight_num->num_back_height_channel_elements[1]; + i++) + FDKwriteBits(hBs, HEIGHT_NORMAL, 2); + for (i = 0; i < config->pHeight_num->num_back_height_channel_elements[0]; + i++) + FDKwriteBits(hBs, HEIGHT_TOP, 2); + for (i = 0; i < config->pHeight_num->num_back_height_channel_elements[1]; + i++) + FDKwriteBits(hBs, HEIGHT_BOTTOM, 2); + + FDKbyteAlign(hBs, alignAnchor); /* Alignment */ + + FDKcrcEndReg(&crcInfo, hBs, crcReg); + FDKwriteBits(hBs, FDKcrcGetCRC(&crcInfo), 8); + + } else { + FDKwriteBits(hBs, 0, + 8); /* Do no write any comment or height information. */ + } return 0; } -int transportEnc_GetPCEBits(CHANNEL_MODE channelMode, - int matrixMixdownA, - int bits) -{ - const PCE_CONFIGURATION* config = NULL; +int transportEnc_GetPCEBits(CHANNEL_MODE channelMode, int matrixMixdownA, + int bits) { + const PCE_CONFIGURATION *config = NULL; - if ((config=getPceEntry(channelMode))==NULL) { - return -1; /* unsupported channelmapping */ + 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 */ + 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 */ + 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; + 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 */ + if ((bits % 8) != 0) { + bits += (8 - (bits % 8)); /* Alignment */ } - bits += 8; /* Comment field bytes */ + bits += 8; /* Comment field bytes */ + + if (config->pHeight_num != NULL) { + /* Comment field (height extension) */ + + bits += + 8 /* PCE_HEIGHT_EXT_SYNC */ + + + ((config->num_front_channel_elements + + config->num_side_channel_elements + config->num_back_channel_elements) + << 1) /* 2 bit height info per element */ + + 8; /* CRC */ - /* - comment_field_bytes alwys 0. */ + if ((bits % 8) != 0) { + bits += (8 - (bits % 8)); /* Alignment */ + } + } return bits; } -#endif /* TP_PCE_ENABLE */ -static void writeAot(HANDLE_FDK_BITSTREAM hBitstreamBuffer, AUDIO_OBJECT_TYPE aot) -{ - int tmp = (int) aot; +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 ); - } + 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); +static void writeSampleRate(HANDLE_FDK_BITSTREAM hBs, int sampleRate, + int nBits) { + int srIdx = getSamplingRateIndex(sampleRate, nBits); - FDKwriteBits( hBitstreamBuffer, sampleRateIndex, 4 ); - if( sampleRateIndex == 15 ) { - FDKwriteBits( hBitstreamBuffer, sampleRate, 24 ); + FDKwriteBits(hBs, srIdx, nBits); + if (srIdx == (1 << nBits) - 1) { + FDKwriteBits(hBs, sampleRate, 24); } } -#ifdef TP_GA_ENABLE -static -int transportEnc_writeGASpecificConfig( - HANDLE_FDK_BITSTREAM asc, - CODER_CONFIG *config, - int extFlg, - UINT alignAnchor - ) -{ +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 */ + 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, config->matrixMixdownA, (config->flags&CC_PSEUDO_SURROUND)?1:0, alignAnchor); + if (getChannelConfig(config->channelMode, config->channelConfigZero) == 0) { + transportEnc_writePCE(asc, config->channelMode, config->samplingRate, 0, 1, + config->matrixMixdownA, + (config->flags & CC_PSEUDO_SURROUND) ? 1 : 0, + alignAnchor); + } + if ((aot == AOT_AAC_SCAL) || (aot == AOT_ER_AAC_SCAL)) { + FDKwriteBits(asc, 0, 3); /* layerNr */ } if (extFlg) { if (aot == AOT_ER_BSAC) { - FDKwriteBits( asc, config->BSACnumOfSubFrame, 5 ); /* numOfSubFrame */ - FDKwriteBits( asc, config->BSAClayerLength, 11 ); /* layer_length */ + 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 */ + 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' */ + 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; + +static int transportEnc_writeELDSpecificConfig(HANDLE_FDK_BITSTREAM hBs, + CODER_CONFIG *config, + int epConfig, + CSTpCallBacks *cb) { + UINT frameLengthFlag = 0; + switch (config->samplesPerFrame) { + case 512: + case 256: + case 128: + case 64: + frameLengthFlag = 0; + break; + case 480: + case 240: + case 160: + case 120: + case 60: + frameLengthFlag = 1; + break; } - 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, frameLengthFlag, 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*/ + 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; + int e, sbrElementIndex = 0; 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); + for (e = 0; e < pPce->num_front_channel_elements + + pPce->num_side_channel_elements + + pPce->num_back_channel_elements + + pPce->num_lfe_channel_elements; + e++) { + if ((pPce->pEl_type[e] == ID_SCE) || (pPce->pEl_type[e] == ID_CPE)) { + cb->cbSbr(cb->cbSbrData, hBs, 0, 0, 0, config->aot, pPce->pEl_type[e], + sbrElementIndex, 0, 0, 0, NULL, 1); + sbrElementIndex++; } } } } - FDKwriteBits(hBs, 0, 4); /* ELDEXT_TERM */ + if ((config->flags & CC_SAC) && (cb->cbSsc != NULL)) { + FDKwriteBits(hBs, ELDEXT_LDSAC, 4); + + const INT eldExtLen = + (cb->cbSsc(cb->cbSscData, NULL, config->aot, config->extSamplingRate, 0, + 0, 0, 0, NULL) + + 7) >> + 3; + INT cnt = eldExtLen; + + if (cnt < 0xF) { + FDKwriteBits(hBs, cnt, 4); + } else { + FDKwriteBits(hBs, 0xF, 4); + cnt -= 0xF; + + if (cnt < 0xFF) { + FDKwriteBits(hBs, cnt, 8); + } else { + FDKwriteBits(hBs, 0xFF, 8); + cnt -= 0xFF; + + FDK_ASSERT(cnt <= 0xFFFF); + FDKwriteBits(hBs, cnt, 16); + } + } + + cb->cbSsc(cb->cbSscData, hBs, config->aot, config->extSamplingRate, 0, 0, 0, + 0, NULL); + } + + if (config->downscaleSamplingRate != 0 && + config->downscaleSamplingRate != config->extSamplingRate) { + /* downscale active */ + + /* eldExtLenDsc: Number of bytes for the ELD downscale extension (srIdx + needs 1 byte + + downscaleSamplingRate needs additional 3 bytes) */ + int eldExtLenDsc = 1; + int downscaleSamplingRate = config->downscaleSamplingRate; + FDKwriteBits(hBs, ELDEXT_DOWNSCALEINFO, 4); /* ELDEXT_DOWNSCALEINFO */ + + if ((downscaleSamplingRate != 96000) && (downscaleSamplingRate != 88200) && + (downscaleSamplingRate != 64000) && (downscaleSamplingRate != 48000) && + (downscaleSamplingRate != 44100) && (downscaleSamplingRate != 32000) && + (downscaleSamplingRate != 24000) && (downscaleSamplingRate != 22050) && + (downscaleSamplingRate != 16000) && (downscaleSamplingRate != 12000) && + (downscaleSamplingRate != 11025) && (downscaleSamplingRate != 8000) && + (downscaleSamplingRate != 7350)) { + eldExtLenDsc = 4; /* length extends to 4 if downscaleSamplingRate's value + is not one of the listed values */ + } + + FDKwriteBits(hBs, eldExtLenDsc, 4); + writeSampleRate(hBs, downscaleSamplingRate, 4); + FDKwriteBits(hBs, 0x0, 4); /* fill_nibble */ + } + + FDKwriteBits(hBs, ELDEXT_TERM, 4); /* ELDEXT_TERM */ return 0; } -#endif /* TP_ELD_ENABLE */ +static int transportEnc_writeUsacSpecificConfig(HANDLE_FDK_BITSTREAM hBs, + int extFlag, CODER_CONFIG *cc, + CSTpCallBacks *cb) { + FDK_BITSTREAM usacConf; + int usacConfigBits = cc->rawConfigBits; + + if ((usacConfigBits <= 0) || + ((usacConfigBits + 7) / 8 > (int)sizeof(cc->rawConfig))) { + return TRANSPORTENC_UNSUPPORTED_FORMAT; + } + FDKinitBitStream(&usacConf, cc->rawConfig, BUFSIZE_DUMMY_VALUE, + usacConfigBits, BS_READER); + + for (; usacConfigBits > 0; usacConfigBits--) { + UINT tmp = FDKreadBit(&usacConf); + FDKwriteBits(hBs, tmp, 1); + } + FDKsyncCache(hBs); + + return TRANSPORTENC_OK; +} -int transportEnc_writeASC ( - HANDLE_FDK_BITSTREAM asc, - CODER_CONFIG *config, - CSTpCallBacks *cb - ) -{ +int transportEnc_writeASC(HANDLE_FDK_BITSTREAM asc, CODER_CONFIG *config, + CSTpCallBacks *cb) { UINT extFlag = 0; int err; int epConfig = 0; @@ -472,37 +894,35 @@ int transportEnc_writeASC ( case AOT_ER_AAC_LD: case AOT_ER_AAC_ELD: case AOT_USAC: - extFlag = 1; - break; + extFlag = 1; + break; default: - break; + break; } - if (config->sbrSignaling==SIG_EXPLICIT_HIERARCHICAL && config->sbrPresent) + if (config->sbrSignaling == SIG_EXPLICIT_HIERARCHICAL && config->sbrPresent) writeAot(asc, config->extAOT); else writeAot(asc, config->aot); - { - writeSampleRate(asc, config->samplingRate); - } + /* In case of USAC it is the output not the core sampling rate */ + writeSampleRate(asc, config->samplingRate, 4); /* 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; + if (config->channelMode == MODE_INVALID) return -1; } - FDKwriteBits( asc, getChannelConfig(config->channelMode), 4 ); + FDKwriteBits( + asc, getChannelConfig(config->channelMode, config->channelConfigZero), 4); - if (config->sbrSignaling==SIG_EXPLICIT_HIERARCHICAL && config->sbrPresent) { - writeSampleRate(asc, config->extSamplingRate); + if (config->sbrSignaling == SIG_EXPLICIT_HIERARCHICAL && config->sbrPresent) { + writeSampleRate(asc, config->extSamplingRate, 4); writeAot(asc, config->aot); } switch (config->aot) { -#ifdef TP_GA_ENABLE case AOT_AAC_MAIN: case AOT_AAC_LC: case AOT_AAC_SSR: @@ -515,18 +935,20 @@ int transportEnc_writeASC ( 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; + 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) + if (err) return err; + break; + case AOT_USAC: + err = transportEnc_writeUsacSpecificConfig(asc, extFlag, config, cb); + if (err) { return err; + } break; -#endif /* TP_ELD_ENABLE */ default: return -1; } @@ -543,34 +965,32 @@ int transportEnc_writeASC ( case AOT_ER_HILN: case AOT_ER_PARA: case AOT_ER_AAC_ELD: - FDKwriteBits( asc, 0, 2 ); /* epconfig 0 */ + FDKwriteBits(asc, 0, 2); /* epconfig 0 */ break; default: break; } /* backward compatible explicit signaling of extension AOT */ - if (config->sbrSignaling==SIG_EXPLICIT_BW_COMPATIBLE) - { + if (config->sbrSignaling == SIG_EXPLICIT_BW_COMPATIBLE) { TP_ASC_EXTENSION_ID ascExtId = ASCEXT_UNKOWN; if (config->sbrPresent) { - ascExtId=ASCEXT_SBR; - FDKwriteBits( asc, ascExtId, 11 ); + ascExtId = ASCEXT_SBR; + FDKwriteBits(asc, ascExtId, 11); writeAot(asc, config->extAOT); - FDKwriteBits( asc, 1, 1 ); /* sbrPresentFlag=1 */ - writeSampleRate(asc, config->extSamplingRate); + FDKwriteBits(asc, 1, 1); /* sbrPresentFlag=1 */ + writeSampleRate(asc, config->extSamplingRate, 4); if (config->psPresent) { - ascExtId=ASCEXT_PS; - FDKwriteBits( asc, ascExtId, 11 ); - FDKwriteBits( asc, 1, 1 ); /* psPresentFlag=1 */ + ascExtId = ASCEXT_PS; + FDKwriteBits(asc, ascExtId, 11); + FDKwriteBits(asc, 1, 1); /* psPresentFlag=1 */ } } - } /* Make sure all bits are sync'ed */ - FDKsyncCache( asc ); + FDKsyncCache(asc); return 0; } diff --git a/libMpegTPEnc/src/tpenc_asc.h b/libMpegTPEnc/src/tpenc_asc.h index 47fe7a1..5f5621e 100644 --- a/libMpegTPEnc/src/tpenc_asc.h +++ b/libMpegTPEnc/src/tpenc_asc.h @@ -1,74 +1,85 @@ - -/* ----------------------------------------------------------------------------------------------------------- +/* ----------------------------------------------------------------------------- Software License for The Fraunhofer FDK AAC Codec Library for Android -© Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. - All rights reserved. +© Copyright 1995 - 2018 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. +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: +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 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 +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. +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. +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." +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. +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. +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. +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 @@ -79,14 +90,15 @@ Am Wolfsmantel 33 www.iis.fraunhofer.de/amm amm-info@iis.fraunhofer.de ------------------------------------------------------------------------------------------------------------ */ +----------------------------------------------------------------------------- */ + +/******************* MPEG transport format encoder library ********************* -/***************************** MPEG-4 AAC Encoder ************************** + Author(s): Manuel Jander - Author(s): Manuel Jander Description: Audio Specific Config writer -******************************************************************************/ +*******************************************************************************/ #ifndef TPENC_ASC_H #define TPENC_ASC_H @@ -95,10 +107,12 @@ amm-info@iis.fraunhofer.de * \brief Get channel config from channel mode. * * \param channel_mode channel mode + * \param channel_config_zero no standard channel configuration * * \return chanel config */ -int getChannelConfig( CHANNEL_MODE channel_mode ); +int getChannelConfig(const CHANNEL_MODE channel_mode, + const UCHAR channel_config_zero); /** * \brief Write a Program Config Element. @@ -113,16 +127,10 @@ int getChannelConfig( CHANNEL_MODE channel_mode ); * \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 - ); +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 @@ -130,13 +138,10 @@ int transportEnc_writePCE( * \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. + * \return the amount of bits required for the PCE including the given bit + * offset. */ -int transportEnc_GetPCEBits( - CHANNEL_MODE channelMode, - int matrixMixdownA, - int bits - ); +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 index f292019..2d35d48 100644 --- a/libMpegTPEnc/src/tpenc_latm.cpp +++ b/libMpegTPEnc/src/tpenc_latm.cpp @@ -1,74 +1,85 @@ - -/* ----------------------------------------------------------------------------------------------------------- +/* ----------------------------------------------------------------------------- Software License for The Fraunhofer FDK AAC Codec Library for Android -© Copyright 1995 - 2015 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. - All rights reserved. +© Copyright 1995 - 2018 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. +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: +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 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 +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. +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. +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." +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. +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. +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. +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 @@ -79,78 +90,69 @@ Am Wolfsmantel 33 www.iis.fraunhofer.de/amm amm-info@iis.fraunhofer.de ------------------------------------------------------------------------------------------------------------ */ +----------------------------------------------------------------------------- */ -/***************************** MPEG-4 AAC Encoder ************************** +/******************* MPEG transport format encoder library ********************* 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 -}; + 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) -{ +static UINT transportEnc_LatmWriteValue(HANDLE_FDK_BITSTREAM hBs, int value) { UCHAR valueBytes = 4; unsigned int bitsWritten = 0; int i; - if ( value < (1<<8) ) { + if (value < (1 << 8)) { valueBytes = 1; - } else if ( value < (1<<16) ) { + } else if (value < (1 << 16)) { valueBytes = 2; - } else if ( value < (1<<24) ) { + } 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++) { + 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); + FDKwriteBits(hBs, (UCHAR)(value >> ((valueBytes - 1 - i) << 3)), 8); } - bitsWritten = (valueBytes<<3)+2; + bitsWritten = (valueBytes << 3) + 2; return bitsWritten; } -static -UINT transportEnc_LatmCountFixBitDemandHeader ( HANDLE_LATM_STREAM hAss ) -{ +static UINT transportEnc_LatmCountFixBitDemandHeader(HANDLE_LATM_STREAM hAss) { int bitDemand = 0; - int insertSetupData = 0 ; + int insertSetupData = 0; /* only if start of new latm frame */ - if (hAss->subFrameCnt==0) - { + if (hAss->subFrameCnt == 0) { /* AudioSyncStream */ if (hAss->tt == TT_MP4_LOAS) { - bitDemand += 11 ; /* syncword */ - bitDemand += 13 ; /* audioMuxLengthBytes */ + bitDemand += 11; /* syncword */ + bitDemand += 13; /* audioMuxLengthBytes */ } /* AudioMuxElement*/ @@ -164,191 +166,184 @@ UINT transportEnc_LatmCountFixBitDemandHeader ( HANDLE_LATM_STREAM hAss ) if (hAss->tt != TT_MP4_LATM_MCP0) { /* AudioMuxElement::useSameStreamMux Flag */ - bitDemand+=1; + bitDemand += 1; - if( insertSetupData ) { + if (insertSetupData) { bitDemand += hAss->streamMuxConfigBits; } } /* AudioMuxElement::otherDataBits */ - bitDemand += 8*hAss->otherDataLenBytes; + bitDemand += hAss->otherDataLenBits; /* AudioMuxElement::ByteAlign */ - if ( bitDemand % 8 ) { - hAss->fillBits = 8 - (bitDemand % 8); - bitDemand += hAss->fillBits ; + if (bitDemand % 8) { + hAss->fillBits = 8 - (bitDemand % 8); + bitDemand += hAss->fillBits; } else { hAss->fillBits = 0; } } - return bitDemand ; + return bitDemand; } -static -UINT transportEnc_LatmCountVarBitDemandHeader ( HANDLE_LATM_STREAM hAss , unsigned int streamDataLength ) -{ +static UINT transportEnc_LatmCountVarBitDemandHeader( + HANDLE_LATM_STREAM hAss, unsigned int streamDataLength) { int bitDemand = 0; - int prog, layer; + int prog, layer; /* Payload Length Info*/ - if( hAss->allStreamsSameTimeFraming ) { - for( prog=0; prog<hAss->noProgram; prog++ ) { - for( layer=0; layer<LATM_MAX_LAYERS; layer++ ) { + 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 ) { + if (p_linfo->streamID >= 0) { + switch (p_linfo->frameLengthType) { case 0: - streamDataLength -= bitDemand ; - while( streamDataLength >= (255<<3) ) { - bitDemand+=8; - streamDataLength -= (255<<3); + if (streamDataLength > 0) { + streamDataLength -= bitDemand; + while (streamDataLength >= (255 << 3)) { + bitDemand += 8; + streamDataLength -= (255 << 3); + } + bitDemand += 8; } - - bitDemand += 8; break; - /*bitDemand += 1; endFlag - break;*/ case 1: case 4: case 6: - + bitDemand += 2; break; default: - return 0; - } - hAss->varStreamCnt++; + return 0; } } } - 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; + } 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;*/ - default: - return 0; + 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 ; + return bitDemand; } TRANSPORTENC_ERROR -CreateStreamMuxConfig( - HANDLE_LATM_STREAM hAss, - HANDLE_FDK_BITSTREAM hBs, - int bufferFullness, - CSTpCallBacks *cb - ) -{ +CreateStreamMuxConfig(HANDLE_LATM_STREAM hAss, HANDLE_FDK_BITSTREAM hBs, + int bufferFullness, CSTpCallBacks *cb) { INT streamIDcnt, tmp; int layer, prog; - USHORT coreFrameOffset=0; + USHORT coreFrameOffset = 0; - hAss->taraBufferFullness = 0xFF; - hAss->audioMuxVersionA = 0; /* for future extensions */ + hAss->taraBufferFullness = 0xFF; + hAss->audioMuxVersionA = 0; /* for future extensions */ hAss->streamMuxConfigBits = 0; - FDKwriteBits( hBs, hAss->audioMuxVersion, 1 ); /* audioMuxVersion */ + FDKwriteBits(hBs, hAss->audioMuxVersion, 1); /* audioMuxVersion */ hAss->streamMuxConfigBits += 1; - if ( hAss->audioMuxVersion == 1 ) { - FDKwriteBits( hBs, hAss->audioMuxVersionA, 1 ); /* audioMuxVersionA */ - 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 */ + 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 */ + 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; + hAss->streamMuxConfigBits += 11; streamIDcnt = 0; - for( prog=0; prog<hAss->noProgram; prog++ ) { + for (prog = 0; prog < hAss->noProgram; prog++) { int transLayer = 0; - FDKwriteBits( hBs, hAss->noLayer[prog]-1, 3 ); - hAss->streamMuxConfigBits+=3; + 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]; + 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 ) { + if (hAss->config[prog][layer] != NULL) { int useSameConfig = 0; - if( transLayer > 0 ) { - FDKwriteBits( hBs, useSameConfig ? 1 : 0, 1 ); - hAss->streamMuxConfigBits+=1; + if (transLayer > 0) { + FDKwriteBits(hBs, useSameConfig ? 1 : 0, 1); + hAss->streamMuxConfigBits += 1; } - if( (useSameConfig == 0) || (transLayer==0) ) { + if ((useSameConfig == 0) || (transLayer == 0)) { const UINT alignAnchor = FDKgetValidBits(hBs); - transportEnc_writeASC( - hBs, - hAss->config[prog][layer], - cb - ); + if (0 != + (transportEnc_writeASC(hBs, hAss->config[prog][layer], cb))) { + return TRANSPORTENC_UNKOWN_ERROR; + } - if ( hAss->audioMuxVersion == 1 ) { + if (hAss->audioMuxVersion == 1) { UINT ascLen = transportEnc_LatmWriteValue(hBs, 0); FDKbyteAlign(hBs, alignAnchor); ascLen = FDKgetValidBits(hBs) - alignAnchor - ascLen; @@ -356,180 +351,160 @@ CreateStreamMuxConfig( transportEnc_LatmWriteValue(hBs, ascLen); - transportEnc_writeASC( - hBs, - hAss->config[prog][layer], - cb - ); + if (0 != + (transportEnc_writeASC(hBs, hAss->config[prog][layer], cb))) { + return TRANSPORTENC_UNKOWN_ERROR; + } FDKbyteAlign(hBs, alignAnchor); /* asc length fillbits */ } - hAss->streamMuxConfigBits += FDKgetValidBits(hBs) - alignAnchor; /* add asc length to smc summary */ + hAss->streamMuxConfigBits += + FDKgetValidBits(hBs) - + alignAnchor; /* add asc length to smc summary */ } transLayer++; - if( !hAss->allStreamsSameTimeFraming ) { - if( streamIDcnt >= LATM_MAX_STREAM_ID ) + 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: - 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; + 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: + 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; + 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; + 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; } - if( i>=62 ) { - return TRANSPORTENC_INVALID_CELP_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; - 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; + 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; } - FDKwriteBits( hBs, i, 1 ); /* HVXCframeLengthTableIndex */ - hAss->streamMuxConfigBits+=1; - } - p_linfo->frameLengthBits = p_lci->bitsFrame; - break; + p_linfo->frameLengthBits = p_lci->bitsFrame; + break; - case AOT_NULL_OBJECT: - default: - return TRANSPORTENC_INVALID_AOT; + 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; + FDKwriteBits(hBs, (hAss->otherDataLenBits > 0) ? 1 : 0, + 1); /* otherDataPresent */ + hAss->streamMuxConfigBits += 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); + if (hAss->otherDataLenBits > 0) { + FDKwriteBits(hBs, 0, 1); + FDKwriteBits(hBs, hAss->otherDataLenBits, 8); + hAss->streamMuxConfigBits += 9; } - { - USHORT crcCheckPresent=0; - USHORT crcCheckSum=0; + FDKwriteBits(hBs, 0, 1); /* crcCheckPresent=0 */ + hAss->streamMuxConfigBits += 1; - FDKwriteBits( hBs, crcCheckPresent, 1 ); /* crcCheckPresent */ - hAss->streamMuxConfigBits+=1; - if ( crcCheckPresent ){ - FDKwriteBits( hBs, crcCheckSum, 8 ); /* crcCheckSum */ - hAss->streamMuxConfigBits+=8; - } - } - - } else { /* if ( audioMuxVersionA == 0 ) */ + } else { /* if ( audioMuxVersionA == 0 ) */ /* for future extensions */ - } return TRANSPORTENC_OK; } - -static TRANSPORTENC_ERROR -WriteAuPayloadLengthInfo( HANDLE_FDK_BITSTREAM hBitStream, int AuLengthBits ) -{ +static TRANSPORTENC_ERROR WriteAuPayloadLengthInfo( + HANDLE_FDK_BITSTREAM hBitStream, int AuLengthBits) { int restBytes; - if( AuLengthBits % 8 ) - return TRANSPORTENC_INVALID_AU_LENGTH; + if (AuLengthBits % 8) return TRANSPORTENC_INVALID_AU_LENGTH; - while( AuLengthBits >= 255*8 ) { - FDKwriteBits( hBitStream, 255, 8 ); /* 255 shows incomplete AU */ - AuLengthBits -= (255*8); + while (AuLengthBits >= 255 * 8) { + FDKwriteBits(hBitStream, 255, 8); /* 255 shows incomplete AU */ + AuLengthBits -= (255 * 8); } restBytes = (AuLengthBits) >> 3; - FDKwriteBits( hBitStream, restBytes, 8 ); + 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 */ +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) { @@ -538,48 +513,50 @@ TRANSPORTENC_ERROR transportEnc_LatmSetNrOfSubframes( HANDLE_LATM_STREAM hAss, 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) ) { + /* 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 */ ) -{ +static int allStreamsSameTimeFraming(HANDLE_LATM_STREAM hAss, UCHAR noProgram, + UCHAR noLayer[] /* return */) { int prog, layer; - signed int lastNoSamples = -1; + signed int lastNoSamples = -1; signed int minFrameSamples = FDK_INT_MAX; signed int maxFrameSamples = 0; signed int highestSamplingRate = -1; - for( prog=0; prog<noProgram; prog++ ) { + for (prog = 0; prog < noProgram; prog++) { noLayer[prog] = 0; - for( layer=0; layer<LATM_MAX_LAYERS; layer++ ) - { - if( hAss->config[prog][layer] != NULL ) - { + for (layer = 0; layer < LATM_MAX_LAYERS; layer++) { + if (hAss->config[prog][layer] != NULL) { INT hsfSamplesFrame; noLayer[prog]++; - if( highestSamplingRate < 0 ) + if (highestSamplingRate < 0) highestSamplingRate = hAss->config[prog][layer]->samplingRate; - hsfSamplesFrame = hAss->config[prog][layer]->samplesPerFrame * 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 (hsfSamplesFrame <= minFrameSamples) + minFrameSamples = hsfSamplesFrame; + if (hsfSamplesFrame >= maxFrameSamples) + maxFrameSamples = hsfSamplesFrame; - if( lastNoSamples == -1 ) { - lastNoSamples = hsfSamplesFrame; + if (lastNoSamples == -1) { + lastNoSamples = hsfSamplesFrame; } else { - if( hsfSamplesFrame != lastNoSamples ) { + if (hsfSamplesFrame != lastNoSamples) { return 0; } } @@ -593,18 +570,14 @@ int allStreamsSameTimeFraming( HANDLE_LATM_STREAM hAss, UCHAR noProgram, UCHAR n /** * 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 - ) -{ +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; + if (hAss == NULL) return TRANSPORTENC_INVALID_PARAMETER; hAss->tt = tt; @@ -613,82 +586,78 @@ TRANSPORTENC_ERROR transportEnc_InitLatmStream( HANDLE_LATM_STREAM hAss, hAss->audioMuxVersion = audioMuxVersion; /* Fill noLayer array using hAss->config */ - hAss->allStreamsSameTimeFraming = allStreamsSameTimeFraming( hAss, hAss->noProgram, hAss->noLayer ); + hAss->allStreamsSameTimeFraming = + allStreamsSameTimeFraming(hAss, hAss->noProgram, hAss->noLayer); /* Only allStreamsSameTimeFraming==1 is supported */ FDK_ASSERT(hAss->allStreamsSameTimeFraming); hAss->fractDelayPresent = fractDelayPresent; - hAss->otherDataLenBytes = 0; + hAss->otherDataLenBits = 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; + 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->audioMuxLengthBytes = 0; - hAss->latmFrameCounter = 0; + hAss->latmFrameCounter = 0; hAss->muxConfigPeriod = muxConfigPeriod; return ErrorStatus; } - /** * */ -UINT transportEnc_LatmCountTotalBitDemandHeader ( HANDLE_LATM_STREAM hAss , unsigned int streamDataLength ) -{ +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; + 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 - ) -{ +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->subFrameCnt == 0) { if (hAss->muxConfigPeriod > 0) { insertMuxSetup = (hAss->latmFrameCounter == 0); - } else { + } 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) + if (insertMuxSetup) { + FDKwriteBits(hBs, 0, 1); /* useSameStreamMux useNewStreamMux */ + if (TRANSPORTENC_OK != (ErrorStatus = CreateStreamMuxConfig( + hAss, hBs, bufferFullness, cb))) { return ErrorStatus; + } } else { - FDKwriteBits( hBs, 1, 1 ); /* useSameStreamMux */ + FDKwriteBits(hBs, 1, 1); /* useSameStreamMux */ } } } @@ -699,9 +668,8 @@ AdvanceAudioMuxElement ( 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; + ErrorStatus = WriteAuPayloadLengthInfo(hBs, auBits); + if (ErrorStatus != TRANSPORTENC_OK) return ErrorStatus; } } } @@ -711,14 +679,8 @@ AdvanceAudioMuxElement ( } TRANSPORTENC_ERROR -transportEnc_LatmWrite ( - HANDLE_LATM_STREAM hAss, - HANDLE_FDK_BITSTREAM hBs, - int auBits, - int bufferFullness, - CSTpCallBacks *cb - ) -{ +transportEnc_LatmWrite(HANDLE_LATM_STREAM hAss, HANDLE_FDK_BITSTREAM hBs, + int auBits, int bufferFullness, CSTpCallBacks *cb) { TRANSPORTENC_ERROR ErrorStatus; if (hAss->subFrameCnt == 0) { @@ -732,81 +694,76 @@ transportEnc_LatmWrite ( - only if loas - we must update the syncword distance (=audiomuxlengthbytes) later */ - if( hAss->tt == TT_MP4_LOAS && hAss->subFrameCnt == 0) - { + if (hAss->tt == TT_MP4_LOAS && hAss->subFrameCnt == 0) { /* Start new LOAS frame */ - FDKwriteBits( hBs, 0x2B7, 11 ); + FDKwriteBits(hBs, 0x2B7, 11); hAss->audioMuxLengthBytes = 0; - hAss->audioMuxLengthBytesPos = FDKgetValidBits( hBs ); /* store read pointer position */ - FDKwriteBits( hBs, hAss->audioMuxLengthBytes, 13 ); + hAss->audioMuxLengthBytesPos = + FDKgetValidBits(hBs); /* store read pointer position */ + FDKwriteBits(hBs, hAss->audioMuxLengthBytes, 13); } - ErrorStatus = AdvanceAudioMuxElement( - hAss, - hBs, - auBits, - bufferFullness, - cb - ); + ErrorStatus = AdvanceAudioMuxElement(hAss, hBs, auBits, bufferFullness, cb); - if (ErrorStatus != TRANSPORTENC_OK) - return ErrorStatus; + if (ErrorStatus != TRANSPORTENC_OK) return ErrorStatus; return ErrorStatus; } -void transportEnc_LatmAdjustSubframeBits(HANDLE_LATM_STREAM hAss, - int *bits) -{ +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) + if (hAss->subFrameCnt == 0) { + *bits += hAss->otherDataLenBits; *bits += hAss->fillBits; + } } - -void transportEnc_LatmGetFrame(HANDLE_LATM_STREAM hAss, - HANDLE_FDK_BITSTREAM hBs, - int *bytes) -{ +TRANSPORTENC_ERROR transportEnc_LatmGetFrame(HANDLE_LATM_STREAM hAss, + HANDLE_FDK_BITSTREAM hBs, + int *pBytes) { + TRANSPORTENC_ERROR ErrorStatus = TRANSPORTENC_OK; hAss->subFrameCnt++; - if (hAss->subFrameCnt >= hAss->noSubframes) - { - + if (hAss->subFrameCnt >= hAss->noSubframes) { /* Add LOAS frame length if required. */ - if (hAss->tt == TT_MP4_LOAS) - { - int latmBytes; - - latmBytes = (FDKgetValidBits(hBs)+7) >> 3; + if (hAss->tt == TT_MP4_LOAS) { + FDK_BITSTREAM tmpBuf; - /* write length info into assembler buffer */ - hAss->audioMuxLengthBytes = latmBytes - 3; /* 3=Syncword + length */ - { - FDK_BITSTREAM tmpBuf; + /* Determine frame length info */ + hAss->audioMuxLengthBytes = + ((FDKgetValidBits(hBs) + hAss->otherDataLenBits + 7) >> 3) - + 3; /* 3=Syncword + length */ - FDKinitBitStream( &tmpBuf, hBs->hBitBuf.Buffer, hBs->hBitBuf.bufSize, 0, BS_WRITER ) ; - FDKpushFor( &tmpBuf, hAss->audioMuxLengthBytesPos ); - FDKwriteBits( &tmpBuf, hAss->audioMuxLengthBytes, 13 ); - FDKsyncCache( &tmpBuf ); + /* Check frame length info */ + if (hAss->audioMuxLengthBytes >= (1 << 13)) { + ErrorStatus = TRANSPORTENC_INVALID_AU_LENGTH; + goto bail; } + + /* Write length info into assembler buffer */ + FDKinitBitStream(&tmpBuf, hBs->hBitBuf.Buffer, hBs->hBitBuf.bufSize, 0, + BS_WRITER); + FDKpushFor(&tmpBuf, hAss->audioMuxLengthBytesPos); + FDKwriteBits(&tmpBuf, hAss->audioMuxLengthBytes, 13); + FDKsyncCache(&tmpBuf); } + /* Write AudioMuxElement other data bits */ + FDKwriteBits(hBs, 0, hAss->otherDataLenBits); + /* Write AudioMuxElement byte alignment fill bits */ FDKwriteBits(hBs, 0, hAss->fillBits); - FDK_ASSERT( (FDKgetValidBits(hBs) % 8) == 0); + FDK_ASSERT((FDKgetValidBits(hBs) % 8) == 0); hAss->subFrameCnt = 0; FDKsyncCache(hBs); - *bytes = (FDKgetValidBits(hBs) + 7)>>3; - //FDKfetchBuffer(hBs, buffer, (UINT*)bytes); + *pBytes = (FDKgetValidBits(hBs) + 7) >> 3; - if (hAss->muxConfigPeriod > 0) - { + if (hAss->muxConfigPeriod > 0) { hAss->latmFrameCounter++; if (hAss->latmFrameCounter >= hAss->muxConfigPeriod) { @@ -816,32 +773,32 @@ void transportEnc_LatmGetFrame(HANDLE_LATM_STREAM hAss, } } else { /* No data this time */ - *bytes = 0; + *pBytes = 0; } + +bail: + return ErrorStatus; } /** * 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 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); + FDK_ASSERT(setupDataDistanceFrames >= 0); - for (prog=0; prog<LATM_MAX_PROGRAMS; prog++) { - for (layer=0; layer<LATM_MAX_LAYERS; layer++) { + 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; } @@ -850,32 +807,44 @@ TRANSPORTENC_ERROR transportEnc_Latm_Init( 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_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; + 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); + if (TRANSPORTENC_OK != + (ErrorStatus = AdvanceAudioMuxElement(hAss, hBs, 0, 0, cb))) { + goto bail; + } + + // CreateStreamMuxConfig(hAss, hBs, 0); bail: return ErrorStatus; } +TRANSPORTENC_ERROR transportEnc_LatmAddOtherDataBits(HANDLE_LATM_STREAM hAss, + const int otherDataBits) { + TRANSPORTENC_ERROR ErrorStatus = TRANSPORTENC_OK; + if ((hAss->otherDataLenBits != 0) || (otherDataBits % 8 != 0)) { + /* This implementation allows to add other data bits only once. + To keep existing alignment only whole bytes are allowed. */ + ErrorStatus = TRANSPORTENC_UNKOWN_ERROR; + } else { + /* Ensure correct addional bits in payload. */ + if (hAss->tt == TT_MP4_LATM_MCP0) { + hAss->otherDataLenBits = otherDataBits; + } else { + hAss->otherDataLenBits = otherDataBits - 9; + hAss->streamMuxConfigBits += 9; + } + } - - - + return ErrorStatus; +} diff --git a/libMpegTPEnc/src/tpenc_latm.h b/libMpegTPEnc/src/tpenc_latm.h index 34eea58..d650357 100644 --- a/libMpegTPEnc/src/tpenc_latm.h +++ b/libMpegTPEnc/src/tpenc_latm.h @@ -1,74 +1,85 @@ - -/* ----------------------------------------------------------------------------------------------------------- +/* ----------------------------------------------------------------------------- Software License for The Fraunhofer FDK AAC Codec Library for Android -© Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. - All rights reserved. +© Copyright 1995 - 2018 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. +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: +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 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 +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. +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. +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." +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. +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. +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. +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 @@ -79,35 +90,35 @@ Am Wolfsmantel 33 www.iis.fraunhofer.de/amm amm-info@iis.fraunhofer.de ------------------------------------------------------------------------------------------------------------ */ +----------------------------------------------------------------------------- */ -/***************************** MPEG-4 AAC Encoder ************************** +/******************* MPEG transport format encoder library ********************* 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 DEFAULT_LATM_SMC_REPEAT 8 -#define MAX_AAC_LAYERS 9 +#define MAX_AAC_LAYERS 9 -#define LATM_MAX_PROGRAMS 1 -#define LATM_MAX_STREAM_ID 16 +#define LATM_MAX_PROGRAMS 1 +#define LATM_MAX_STREAM_ID 16 -#define LATM_MAX_LAYERS 1 /*MAX_AAC_LAYERS*/ +#define LATM_MAX_LAYERS 1 /*MAX_AAC_LAYERS*/ -#define MAX_NR_OF_SUBFRAMES 2 /* set this carefully to avoid buffer overflows */ +#define MAX_NR_OF_SUBFRAMES \ + 2 /* set this carefully to avoid buffer overflows \ + */ typedef enum { LATMVAR_SIMPLE_SEQUENCE } LATM_VAR_MODE; @@ -118,67 +129,80 @@ typedef struct { 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_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; + LATM_VAR_MODE varMode; + TRANSPORT_TYPE tt; - int audioMuxLengthBytes; + int audioMuxLengthBytes; - int audioMuxLengthBytesPos; - int taraBufferFullness; /* state of the bit reservoir */ - int varStreamCnt; - unsigned int otherDataLenBytes; + int audioMuxLengthBytesPos; + int taraBufferFullness; /* state of the bit reservoir */ + int varStreamCnt; - UCHAR latmFrameCounter; /* Current frame number. Counts modulo muxConfigPeriod */ - UCHAR muxConfigPeriod; /* Distance in frames between MuxConfig */ + 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 + audioMuxVersion; /* AMV1 supports transmission of taraBufferFullness and + ASC lengths */ + UCHAR audioMuxVersionA; /* for future extensions */ - UCHAR noProgram; - UCHAR noLayer[LATM_MAX_PROGRAMS]; - UCHAR fractDelayPresent; + 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 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; + UCHAR otherDataLenBits; /* AudioMuxElement other data bits */ + UCHAR fillBits; /* AudioMuxElement fill bits */ + UINT 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. + * \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 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 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 - ); +TRANSPORTENC_ERROR transportEnc_Latm_Init(HANDLE_LATM_STREAM hLatmStreamInfo, + HANDLE_FDK_BITSTREAM hBs, + CODER_CONFIG *layerConfig, + UINT audioMuxVersion, + TRANSPORT_TYPE tt, CSTpCallBacks *cb); + +/** + * \brief Write addional other data bits in AudioMuxElement + * + * \param hAss HANDLE_LATM_STREAM handle + * \param otherDataBits number of other data bits to be written + * + * \return an TRANSPORTENC_ERROR error code + */ +TRANSPORTENC_ERROR transportEnc_LatmAddOtherDataBits(HANDLE_LATM_STREAM hAss, + const int otherDataBits); /** * \brief Get bit demand of next LATM/LOAS header @@ -188,10 +212,8 @@ TRANSPORTENC_ERROR transportEnc_Latm_Init( * * \return the number of bits required by the LATM/LOAS headers */ -unsigned int transportEnc_LatmCountTotalBitDemandHeader ( - HANDLE_LATM_STREAM hAss, - unsigned int streamDataLength - ); +unsigned int transportEnc_LatmCountTotalBitDemandHeader( + HANDLE_LATM_STREAM hAss, unsigned int streamDataLength); /** * \brief Write LATM/LOAS header into given bitstream handle @@ -205,42 +227,35 @@ unsigned int transportEnc_LatmCountTotalBitDemandHeader ( * \return an TRANSPORTENC_ERROR error code */ TRANSPORTENC_ERROR -transportEnc_LatmWrite ( - HANDLE_LATM_STREAM hAss, - HANDLE_FDK_BITSTREAM hBitstream, - int auBits, - int bufferFullness, - CSTpCallBacks *cb - ); +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 + * \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); +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. + * \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 + * \return an TRANSPORTENC_ERROR error code */ -void transportEnc_LatmGetFrame( - HANDLE_LATM_STREAM hAss, - HANDLE_FDK_BITSTREAM hBs, - int *pBytes - ); +TRANSPORTENC_ERROR transportEnc_LatmGetFrame(HANDLE_LATM_STREAM hAss, + HANDLE_FDK_BITSTREAM hBs, + int *pBytes); /** * \brief Write a StreamMuxConfig into the given bitstream handle @@ -253,12 +268,7 @@ void transportEnc_LatmGetFrame( * \return void */ TRANSPORTENC_ERROR -CreateStreamMuxConfig( - HANDLE_LATM_STREAM hAss, - HANDLE_FDK_BITSTREAM hBs, - int bufferFullness, - CSTpCallBacks *cb - ); - +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 index 24fb32f..a8567b9 100644 --- a/libMpegTPEnc/src/tpenc_lib.cpp +++ b/libMpegTPEnc/src/tpenc_lib.cpp @@ -1,74 +1,85 @@ - -/* ----------------------------------------------------------------------------------------------------------- +/* ----------------------------------------------------------------------------- Software License for The Fraunhofer FDK AAC Codec Library for Android -© Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. - All rights reserved. +© Copyright 1995 - 2018 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. +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: +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 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 +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. +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. +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." +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. +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. +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. +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 @@ -79,24 +90,24 @@ Am Wolfsmantel 33 www.iis.fraunhofer.de/amm amm-info@iis.fraunhofer.de ------------------------------------------------------------------------------------------------------------ */ +----------------------------------------------------------------------------- */ + +/******************* MPEG transport format encoder library ********************* -/************************** MPEG-4 Transport Encoder ************************ + Author(s): Manuel Jander - Author(s): Manuel Jander Description: MPEG Transport encode -******************************************************************************/ +*******************************************************************************/ #include "tpenc_lib.h" /* library info */ -#include "version" +#include "tp_version.h" #define MODULE_NAME "transportEnc" #include "tpenc_asc.h" -#include "conv_string.h" #include "tpenc_adts.h" @@ -104,25 +115,23 @@ amm-info@iis.fraunhofer.de #include "tpenc_latm.h" - - typedef struct { int curSubFrame; int nSubFrames; int prevBits; } RAWPACKETS_INFO; -struct TRANSPORTENC -{ +struct TRANSPORTENC { CODER_CONFIG config; - TRANSPORT_TYPE transportFmt; /*!< MPEG4 transport type. */ + 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. */ + 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; @@ -132,8 +141,6 @@ struct TRANSPORTENC RAWPACKETS_INFO raw; - - } writer; CSTpCallBacks callbacks; @@ -141,24 +148,22 @@ struct TRANSPORTENC typedef struct _TRANSPORTENC_STRUCT TRANSPORTENC_STRUCT; - /* * MEMORY Declaration */ -C_ALLOC_MEM(Ram_TransportEncoder, TRANSPORTENC, 1) +C_ALLOC_MEM(Ram_TransportEncoder, struct TRANSPORTENC, 1) -TRANSPORTENC_ERROR transportEnc_Open( HANDLE_TRANSPORTENC *phTpEnc ) -{ +TRANSPORTENC_ERROR transportEnc_Open(HANDLE_TRANSPORTENC *phTpEnc) { HANDLE_TRANSPORTENC hTpEnc; - if ( phTpEnc == NULL ){ + if (phTpEnc == NULL) { return TRANSPORTENC_INVALID_PARAMETER; } hTpEnc = GetRam_TransportEncoder(0); - if ( hTpEnc == NULL ) { + if (hTpEnc == NULL) { return TRANSPORTENC_NO_MEM; } @@ -169,29 +174,31 @@ TRANSPORTENC_ERROR transportEnc_Open( HANDLE_TRANSPORTENC *phTpEnc ) /** * \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. + * - 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. + * - The PCE repetition rate in raw_data_block can be controlled via + * headerPeriod parameter. * - * \param channelConfig Channel Configuration derived from Channel Mode + * \param channelMode Encoder Channel Mode. + * \param channelConfigZero No standard channel configuration. * \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. + * \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. + * \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 - ) -{ +static INT getPceRepetitionRate(const CHANNEL_MODE channelMode, + const int channelConfigZero, + const TRANSPORT_TYPE transportFmt, + const int headerPeriod, + const int matrixMixdownA) { INT pceFrameCounter = -1; /* variable to be returned */ - if (headerPeriod>0) { - switch ( channelConfig ) { + if (headerPeriod > 0) { + switch (getChannelConfig(channelMode, channelConfigZero)) { case 0: switch (transportFmt) { case TT_MP4_ADTS: @@ -199,53 +206,52 @@ static INT getPceRepetitionRate( 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 */ + case TT_MP4_ADIF: /* ADIF header comprises PCE */ + if ((channelMode == MODE_1_2_2) || (channelMode == MODE_1_2_2_1)) { + pceFrameCounter = headerPeriod; /* repeating pce only meaningful + for potential matrix mixdown */ + break; + } + case TT_MP4_LOAS: /* PCE in ASC if chChonfig==0 */ + case TT_MP4_LATM_MCP1: /* PCE in ASC if chChonfig==0 */ default: - pceFrameCounter = -1; /* no PCE in raw_data_block */ + 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) { + /* 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_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_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 */ + 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 */ + pceFrameCounter = -1; /* no PCE in raw_data_block */ + } /* switch getChannelConfig() */ + } /* if headerPeriod>0 */ else { - pceFrameCounter = -1; /* no PCE in raw_data_block */ + 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 - ) -{ +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)); @@ -255,86 +261,95 @@ TRANSPORTENC_ERROR transportEnc_Init( hTpEnc->bsBuffer = bsBuffer; hTpEnc->bsBufferSize = bsBufferSize; - FDKinitBitStream(&hTpEnc->bitStream, hTpEnc->bsBuffer, hTpEnc->bsBufferSize, 0, BS_WRITER); + 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; + hTpEnc->writer.adif.matrixMixdownA = hTpEnc->config.matrixMixdownA; + hTpEnc->writer.adif.pseudoSurroundEnable = + (hTpEnc->config.flags & CC_PSEUDO_SURROUND) ? 1 : 0; + break; - 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_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: - { + 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 - ); + 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; - + } break; + case TT_MP4_RAW: + hTpEnc->writer.raw.curSubFrame = 0; + hTpEnc->writer.raw.nSubFrames = hTpEnc->config.nSubFrames; + break; - default: - return TRANSPORTENC_INVALID_PARAMETER; + 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); + hTpEnc->config.channelMode, hTpEnc->config.channelConfigZero, + transportFmt, hTpEnc->config.headerPeriod, hTpEnc->config.matrixMixdownA); return TRANSPORTENC_OK; } -HANDLE_FDK_BITSTREAM transportEnc_GetBitstream( HANDLE_TRANSPORTENC hTp ) -{ +TRANSPORTENC_ERROR transportEnc_AddOtherDataBits(HANDLE_TRANSPORTENC hTpEnc, + const int nBits) { + TRANSPORTENC_ERROR tpErr = TRANSPORTENC_OK; + + switch (hTpEnc->transportFmt) { + case TT_MP4_LATM_MCP0: + case TT_MP4_LATM_MCP1: + case TT_MP4_LOAS: + tpErr = transportEnc_LatmAddOtherDataBits(&hTpEnc->writer.latm, nBits); + break; + case TT_MP4_ADTS: + case TT_MP4_ADIF: + case TT_MP4_RAW: + default: + tpErr = TRANSPORTENC_UNKOWN_ERROR; + } + + return tpErr; +} + +HANDLE_FDK_BITSTREAM transportEnc_GetBitstream(HANDLE_TRANSPORTENC hTp) { return &hTp->bitStream; } -int transportEnc_RegisterSbrCallback( HANDLE_TRANSPORTENC hTpEnc, const cbSbr_t cbSbr, void* user_data) -{ +int transportEnc_RegisterSbrCallback(HANDLE_TRANSPORTENC hTpEnc, + const cbSbr_t cbSbr, void *user_data) { if (hTpEnc == NULL) { return -1; } @@ -342,15 +357,29 @@ int transportEnc_RegisterSbrCallback( HANDLE_TRANSPORTENC hTpEnc, const cbSbr_t hTpEnc->callbacks.cbSbrData = user_data; return 0; } +int transportEnc_RegisterUsacCallback(HANDLE_TRANSPORTENC hTpEnc, + const cbUsac_t cbUsac, void *user_data) { + if (hTpEnc == NULL) { + return -1; + } + hTpEnc->callbacks.cbUsac = cbUsac; + hTpEnc->callbacks.cbUsacData = user_data; + return 0; +} +int transportEnc_RegisterSscCallback(HANDLE_TRANSPORTENC hTpEnc, + const cbSsc_t cbSsc, void *user_data) { + if (hTpEnc == NULL) { + return -1; + } + hTpEnc->callbacks.cbSsc = cbSsc; + hTpEnc->callbacks.cbSscData = user_data; + return 0; +} -TRANSPORTENC_ERROR transportEnc_WriteAccessUnit( - HANDLE_TRANSPORTENC hTp, - INT frameUsedBits, - int bufferFullness, - int ncc - ) -{ +TRANSPORTENC_ERROR transportEnc_WriteAccessUnit(HANDLE_TRANSPORTENC hTp, + INT frameUsedBits, + int bufferFullness, int ncc) { TRANSPORTENC_ERROR err = TRANSPORTENC_OK; if (!hTp) { @@ -359,48 +388,41 @@ TRANSPORTENC_ERROR transportEnc_WriteAccessUnit( 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 */ + 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 - ); + FDKinitBitStream(&hTp->bitStream, hTp->bsBuffer, hTp->bsBufferSize, 0, + BS_WRITER); + if (0 != adifWrite_EncodeHeader(&hTp->writer.adif, hBs, bufferFullness)) { + err = TRANSPORTENC_INVALID_CONFIG; + } break; case TT_MP4_ADTS: - bufferFullness /= ncc; /* Number of Considered Channels */ + bufferFullness /= ncc; /* Number of Considered Channels */ bufferFullness /= 32; bufferFullness = FDKmin(0x7FF, bufferFullness); /* Signal variable rate */ - adtsWrite_EncodeHeader( - &hTp->writer.adts, - &hTp->bitStream, - bufferFullness, - frameUsedBits - ); + 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 /= 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; + 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); + FDKinitBitStream(&hTp->bitStream, hTp->bsBuffer, hTp->bsBufferSize, 0, + BS_WRITER); } hTp->writer.raw.prevBits = FDKgetValidBits(hBs); break; @@ -410,7 +432,7 @@ TRANSPORTENC_ERROR transportEnc_WriteAccessUnit( } /* Write PCE in raw_data_block if required */ - if (hTp->pceFrameCounter>=hTp->config.headerPeriod) { + 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); @@ -418,29 +440,34 @@ TRANSPORTENC_ERROR transportEnc_WriteAccessUnit( /* Write PCE element ID bits */ FDKwriteBits(&hTp->bitStream, ID_PCE, 3); - if ( (hTp->transportFmt==TT_MP4_ADTS) && !hTp->writer.adts.protection_absent) { + 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)?1:0, alignAnchor); + transportEnc_writePCE( + &hTp->bitStream, hTp->config.channelMode, hTp->config.samplingRate, 0, + 1, hTp->config.matrixMixdownA, + (hTp->config.flags & CC_PSEUDO_SURROUND) ? 1 : 0, alignAnchor); - if ( (hTp->transportFmt==TT_MP4_ADTS) && !hTp->writer.adts.protection_absent) { + 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. */ + 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) -{ +TRANSPORTENC_ERROR transportEnc_EndAccessUnit(HANDLE_TRANSPORTENC hTp, + int *bits) { switch (hTp->transportFmt) { case TT_MP4_LATM_MCP0: case TT_MP4_LATM_MCP1: @@ -465,8 +492,9 @@ TRANSPORTENC_ERROR transportEnc_EndAccessUnit(HANDLE_TRANSPORTENC hTp, int *bits return TRANSPORTENC_OK; } -TRANSPORTENC_ERROR transportEnc_GetFrame(HANDLE_TRANSPORTENC hTpEnc, int *nbytes) -{ +TRANSPORTENC_ERROR transportEnc_GetFrame(HANDLE_TRANSPORTENC hTpEnc, + int *nbytes) { + TRANSPORTENC_ERROR tpErr = TRANSPORTENC_OK; HANDLE_FDK_BITSTREAM hBs = &hTpEnc->bitStream; switch (hTpEnc->transportFmt) { @@ -474,11 +502,12 @@ TRANSPORTENC_ERROR transportEnc_GetFrame(HANDLE_TRANSPORTENC hTpEnc, int *nbytes case TT_MP4_LATM_MCP1: case TT_MP4_LOAS: *nbytes = hTpEnc->bsBufferSize; - transportEnc_LatmGetFrame(&hTpEnc->writer.latm, hBs, nbytes); + tpErr = 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; + 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; @@ -486,28 +515,31 @@ TRANSPORTENC_ERROR transportEnc_GetFrame(HANDLE_TRANSPORTENC hTpEnc, int *nbytes break; case TT_MP4_ADIF: FDK_ASSERT((INT)FDKgetValidBits(hBs) >= 0); - *nbytes = (FDKgetValidBits(hBs) + 7)>>3; + *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; + *nbytes = ((FDKgetValidBits(hBs) - hTpEnc->writer.raw.prevBits) + 7) >> 3; break; default: break; } - return TRANSPORTENC_OK; + return tpErr; } -INT transportEnc_GetStaticBits( HANDLE_TRANSPORTENC hTp, int auBits ) -{ +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. */ + 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) { @@ -521,73 +553,70 @@ INT transportEnc_GetStaticBits( HANDLE_TRANSPORTENC hTp, int auBits ) case TT_MP4_LOAS: case TT_MP4_LATM_MCP0: case TT_MP4_LATM_MCP1: - nbits = transportEnc_LatmCountTotalBitDemandHeader( &hTp->writer.latm, auBits ); + 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. */ + /* 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) - { +void transportEnc_Close(HANDLE_TRANSPORTENC *phTp) { + if (phTp != NULL) { if (*phTp != NULL) { FreeRam_TransportEncoder(phTp); } } } -int transportEnc_CrcStartReg(HANDLE_TRANSPORTENC hTpEnc, int mBits) -{ +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; + 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) -{ +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; + 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 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) - { + switch (hTpEnc->transportFmt) { case TT_MP4_LATM_MCP0: case TT_MP4_LATM_MCP1: case TT_MP4_LOAS: - tpErr = CreateStreamMuxConfig(hLatmConfig, dataBuffer, 0, &hTpEnc->callbacks); + tpErr = + CreateStreamMuxConfig(hLatmConfig, dataBuffer, 0, &hTpEnc->callbacks); *confType = 1; /* config is SMC */ break; default: @@ -597,11 +626,9 @@ TRANSPORTENC_ERROR transportEnc_GetConf(HANDLE_TRANSPORTENC hTpEnc, } return tpErr; - } -TRANSPORTENC_ERROR transportEnc_GetLibInfo( LIB_INFO *info ) -{ +TRANSPORTENC_ERROR transportEnc_GetLibInfo(LIB_INFO *info) { int i; if (info == NULL) { @@ -629,14 +656,8 @@ TRANSPORTENC_ERROR transportEnc_GetLibInfo( LIB_INFO *info ) info->title = TP_LIB_TITLE; /* Set flags */ - info->flags = 0 - | CAPF_ADIF - | CAPF_ADTS - | CAPF_LATM - | CAPF_LOAS - | CAPF_RAWPACKETS - ; + 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 deleted file mode 100644 index 8742568..0000000 --- a/libMpegTPEnc/src/version +++ /dev/null @@ -1,13 +0,0 @@ - -/* library info */ -#define TP_LIB_VL0 2 -#define TP_LIB_VL1 3 -#define TP_LIB_VL2 6 -#define TP_LIB_TITLE "MPEG Transport" -#ifdef __ANDROID__ -#define TP_LIB_BUILD_DATE "" -#define TP_LIB_BUILD_TIME "" -#else -#define TP_LIB_BUILD_DATE __DATE__ -#define TP_LIB_BUILD_TIME __TIME__ -#endif |