diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2019-11-11 11:38:02 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2019-11-11 11:38:02 +0100 |
commit | 0e5af65c467b2423a0b857ae3ad98c91acc1e190 (patch) | |
tree | d07f69550d8886271e44fe79c4dcfb299cafbd38 /fdk-aac/libMpegTPEnc/src | |
parent | efe406d9724f959c8bc2a31802559ca6d41fd897 (diff) | |
download | ODR-AudioEnc-0e5af65c467b2423a0b857ae3ad98c91acc1e190.tar.gz ODR-AudioEnc-0e5af65c467b2423a0b857ae3ad98c91acc1e190.tar.bz2 ODR-AudioEnc-0e5af65c467b2423a0b857ae3ad98c91acc1e190.zip |
Include patched FDK-AAC in the repository
The initial idea was to get the DAB+ patch into upstream, but since
that follows the android source releases, there is no place for a custom
DAB+ patch there.
So instead of having to maintain a patched fdk-aac that has to have the
same .so version as the distribution package on which it is installed,
we prefer having a separate fdk-aac-dab library to avoid collision.
At that point, there's no reason to keep fdk-aac in a separate
repository, as odr-audioenc is the only tool that needs DAB+ encoding
support. Including it here simplifies installation, and makes it
consistent with toolame-dab, also shipped in this repository.
DAB+ decoding support (needed by ODR-SourceCompanion, dablin, etisnoop,
welle.io and others) can be done using upstream FDK-AAC.
Diffstat (limited to 'fdk-aac/libMpegTPEnc/src')
-rw-r--r-- | fdk-aac/libMpegTPEnc/src/tp_version.h | 118 | ||||
-rw-r--r-- | fdk-aac/libMpegTPEnc/src/tpenc_adif.cpp | 186 | ||||
-rw-r--r-- | fdk-aac/libMpegTPEnc/src/tpenc_adif.h | 146 | ||||
-rw-r--r-- | fdk-aac/libMpegTPEnc/src/tpenc_adts.cpp | 319 | ||||
-rw-r--r-- | fdk-aac/libMpegTPEnc/src/tpenc_adts.h | 208 | ||||
-rw-r--r-- | fdk-aac/libMpegTPEnc/src/tpenc_asc.cpp | 996 | ||||
-rw-r--r-- | fdk-aac/libMpegTPEnc/src/tpenc_asc.h | 147 | ||||
-rw-r--r-- | fdk-aac/libMpegTPEnc/src/tpenc_dab.cpp | 467 | ||||
-rw-r--r-- | fdk-aac/libMpegTPEnc/src/tpenc_dab.h | 217 | ||||
-rw-r--r-- | fdk-aac/libMpegTPEnc/src/tpenc_latm.cpp | 850 | ||||
-rw-r--r-- | fdk-aac/libMpegTPEnc/src/tpenc_latm.h | 274 | ||||
-rw-r--r-- | fdk-aac/libMpegTPEnc/src/tpenc_lib.cpp | 713 |
12 files changed, 4641 insertions, 0 deletions
diff --git a/fdk-aac/libMpegTPEnc/src/tp_version.h b/fdk-aac/libMpegTPEnc/src/tp_version.h new file mode 100644 index 0000000..9f1aa22 --- /dev/null +++ b/fdk-aac/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/fdk-aac/libMpegTPEnc/src/tpenc_adif.cpp b/fdk-aac/libMpegTPEnc/src/tpenc_adif.cpp new file mode 100644 index 0000000..b281eff --- /dev/null +++ b/fdk-aac/libMpegTPEnc/src/tpenc_adif.cpp @@ -0,0 +1,186 @@ +/* ----------------------------------------------------------------------------- +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: ADIF Transport Headers writing + +*******************************************************************************/ + +#include "tpenc_adif.h" + +#include "tpenc_lib.h" +#include "tpenc_asc.h" + +int adifWrite_EncodeHeader(ADIF_INFO *adif, HANDLE_FDK_BITSTREAM hBs, + INT adif_buffer_fullness) { + /* ADIF/PCE/ADTS definitions */ + const char adifId[5] = "ADIF"; + const int copyRightIdPresent = 0; + const int originalCopy = 0; + const int home = 0; + int err = 0; + + int i; + + INT totalBitRate = adif->bitRate; + + if (adif->headerWritten) return 0; + + /* Align inside PCE with respect to the first bit of the header */ + UINT alignAnchor = FDKgetValidBits(hBs); + + /* Signal variable bitrate if buffer fullnes exceeds 20 bit */ + adif->bVariableRate = (adif_buffer_fullness >= (INT)(0x1 << 20)) ? 1 : 0; + + FDKwriteBits(hBs, adifId[0], 8); + FDKwriteBits(hBs, adifId[1], 8); + FDKwriteBits(hBs, adifId[2], 8); + FDKwriteBits(hBs, adifId[3], 8); + + FDKwriteBits(hBs, copyRightIdPresent ? 1 : 0, 1); + + if (copyRightIdPresent) { + for (i = 0; i < 72; i++) { + FDKwriteBits(hBs, 0, 1); + } + } + FDKwriteBits(hBs, originalCopy ? 1 : 0, 1); + FDKwriteBits(hBs, home ? 1 : 0, 1); + FDKwriteBits(hBs, adif->bVariableRate ? 1 : 0, 1); + FDKwriteBits(hBs, totalBitRate, 23); + + /* we write only one PCE at the moment */ + FDKwriteBits(hBs, 0, 4); + + if (!adif->bVariableRate) { + FDKwriteBits(hBs, adif_buffer_fullness, 20); + } + /* Write PCE */ + transportEnc_writePCE(hBs, adif->cm, adif->samplingRate, adif->instanceTag, + adif->profile, adif->matrixMixdownA, + (adif->pseudoSurroundEnable) ? 1 : 0, alignAnchor); + + return err; +} + +int adifWrite_GetHeaderBits(ADIF_INFO *adif) { + /* ADIF definitions */ + const int copyRightIdPresent = 0; + + if (adif->headerWritten) return 0; + + int bits = 0; + + bits += 8 * 4; /* ADIF ID */ + + bits += 1; /* Copyright present */ + + if (copyRightIdPresent) bits += 72; /* Copyright ID */ + + bits += 26; + + bits += 4; /* Number of PCE's */ + + if (!adif->bVariableRate) { + bits += 20; + } + + /* write PCE */ + bits = transportEnc_GetPCEBits(adif->cm, adif->matrixMixdownA, bits); + + return bits; +} diff --git a/fdk-aac/libMpegTPEnc/src/tpenc_adif.h b/fdk-aac/libMpegTPEnc/src/tpenc_adif.h new file mode 100644 index 0000000..e001afc --- /dev/null +++ b/fdk-aac/libMpegTPEnc/src/tpenc_adif.h @@ -0,0 +1,146 @@ +/* ----------------------------------------------------------------------------- +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): Alex Goeschel + + Description: Transport Headers support + +*******************************************************************************/ + +#ifndef TPENC_ADIF_H +#define TPENC_ADIF_H + +#include "machine_type.h" +#include "FDK_bitstream.h" + +#include "tp_data.h" + +typedef struct { + CHANNEL_MODE cm; + INT samplingRate; + INT bitRate; + int profile; + int bVariableRate; + int instanceTag; + int headerWritten; + int matrixMixdownA; + int pseudoSurroundEnable; + +} ADIF_INFO; + +/** + * \brief encodes ADIF Header + * + * \param adif pointer to ADIF_INFO structure + * \param hBitStream handle of bitstream, where the ADIF header is written into + * \param adif_buffer_fullness buffer fullness value for the ADIF header + * + * \return 0 on success + */ +int adifWrite_EncodeHeader(ADIF_INFO *adif, HANDLE_FDK_BITSTREAM hBitStream, + INT adif_buffer_fullness); + +/** + * \brief Get bit demand of a ADIF header + * + * \param adif pointer to ADIF_INFO structure + * + * \return amount of bits required to write the ADIF header according to the + * data contained in the adif parameter + */ +int adifWrite_GetHeaderBits(ADIF_INFO *adif); + +#endif /* TPENC_ADIF_H */ diff --git a/fdk-aac/libMpegTPEnc/src/tpenc_adts.cpp b/fdk-aac/libMpegTPEnc/src/tpenc_adts.cpp new file mode 100644 index 0000000..3f7e62c --- /dev/null +++ b/fdk-aac/libMpegTPEnc/src/tpenc_adts.cpp @@ -0,0 +1,319 @@ +/* ----------------------------------------------------------------------------- +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): Alex Groeschel + + Description: ADTS Transport Headers support + +*******************************************************************************/ + +#include "tpenc_adts.h" + +#include "tpenc_lib.h" +#include "tpenc_asc.h" + +int adtsWrite_CrcStartReg( + HANDLE_ADTS pAdts, /*!< pointer to adts stucture */ + HANDLE_FDK_BITSTREAM hBs, /*!< handle to current bit buffer structure */ + int mBits /*!< number of bits in crc region */ +) { + if (pAdts->protection_absent) { + return 0; + } + return (FDKcrcStartReg(&pAdts->crcInfo, hBs, mBits)); +} + +void adtsWrite_CrcEndReg( + HANDLE_ADTS pAdts, /*!< pointer to adts crc info stucture */ + HANDLE_FDK_BITSTREAM hBs, /*!< handle to current bit buffer structure */ + int reg /*!< crc region */ +) { + if (pAdts->protection_absent == 0) { + FDKcrcEndReg(&pAdts->crcInfo, hBs, reg); + } +} + +int adtsWrite_GetHeaderBits(HANDLE_ADTS hAdts) { + int bits = 0; + + if (hAdts->currentBlock == 0) { + /* Static and variable header bits */ + bits = 56; + if (!hAdts->protection_absent) { + /* Add header/ single raw data block CRC bits */ + bits += 16; + if (hAdts->num_raw_blocks > 0) { + /* Add bits of raw data block position markers */ + bits += (hAdts->num_raw_blocks) * 16; + } + } + } + if (!hAdts->protection_absent && hAdts->num_raw_blocks > 0) { + /* Add raw data block CRC bits. Not really part of the header, put they + * cause bit overhead to be accounted. */ + bits += 16; + } + + hAdts->headerBits = bits; + + return bits; +} + +INT adtsWrite_Init(HANDLE_ADTS hAdts, CODER_CONFIG *config) { + /* Sanity checks */ + if (config->nSubFrames < 1 || config->nSubFrames > 4 || + (int)config->aot > 4 || (int)config->aot < 1) { + return -1; + } + + /* fixed header */ + if (config->flags & CC_MPEG_ID) { + hAdts->mpeg_id = 0; /* MPEG 4 */ + } else { + hAdts->mpeg_id = 1; /* MPEG 2 */ + } + hAdts->layer = 0; + hAdts->protection_absent = !(config->flags & CC_PROTECTION); + hAdts->profile = ((int)config->aot) - 1; + hAdts->sample_freq_index = getSamplingRateIndex(config->samplingRate, 4); + hAdts->sample_freq = config->samplingRate; + hAdts->private_bit = 0; + hAdts->channel_mode = config->channelMode; + hAdts->original = 0; + hAdts->home = 0; + /* variable header */ + hAdts->copyright_id = 0; + hAdts->copyright_start = 0; + + hAdts->num_raw_blocks = config->nSubFrames - 1; /* 0 means 1 raw data block */ + + 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 crcIndex = 0; + + hAdts->headerBits = adtsWrite_GetHeaderBits(hAdts); + + FDK_ASSERT(((frame_length + hAdts->headerBits) / 8) < 0x2000); /*13 bit*/ + FDK_ASSERT(buffer_fullness < 0x800); /* 11 bit */ + + if (!hAdts->protection_absent) { + FDKcrcReset(&hAdts->crcInfo); + } + + if (hAdts->currentBlock == 0) { + FDKresetBitbuffer(hBitStream, BS_WRITER); + } + + hAdts->subFrameStartBit = FDKgetValidBits(hBitStream); + + /* Skip new header if this is raw data block 1..n */ + if (hAdts->currentBlock == 0) { + FDKresetBitbuffer(hBitStream, BS_WRITER); + + if (hAdts->num_raw_blocks == 0) { + crcIndex = adtsWrite_CrcStartReg(hAdts, hBitStream, 0); + } + + /* fixed header */ + FDKwriteBits(hBitStream, 0xFFF, 12); + FDKwriteBits(hBitStream, hAdts->mpeg_id, 1); + FDKwriteBits(hBitStream, hAdts->layer, 2); + FDKwriteBits(hBitStream, hAdts->protection_absent, 1); + FDKwriteBits(hBitStream, hAdts->profile, 2); + FDKwriteBits(hBitStream, hAdts->sample_freq_index, 4); + FDKwriteBits(hBitStream, hAdts->private_bit, 1); + FDKwriteBits( + hBitStream, + getChannelConfig(hAdts->channel_mode, 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, buffer_fullness, 11); + FDKwriteBits(hBitStream, hAdts->num_raw_blocks, 2); + + if (!hAdts->protection_absent) { + int i; + + /* End header CRC portion for single raw data block and write dummy zero + * values for unknown fields. */ + if (hAdts->num_raw_blocks == 0) { + adtsWrite_CrcEndReg(hAdts, hBitStream, crcIndex); + } else { + for (i = 0; i < hAdts->num_raw_blocks; i++) { + FDKwriteBits(hBitStream, 0, 16); + } + } + FDKwriteBits(hBitStream, 0, 16); + } + } /* End of ADTS header */ + + return 0; +} + +void adtsWrite_EndRawDataBlock(HANDLE_ADTS hAdts, HANDLE_FDK_BITSTREAM hBs, + int *pBits) { + if (!hAdts->protection_absent) { + FDK_BITSTREAM bsWriter; + + FDKinitBitStream(&bsWriter, hBs->hBitBuf.Buffer, hBs->hBitBuf.bufSize, 0, + BS_WRITER); + FDKpushFor(&bsWriter, 56); + + if (hAdts->num_raw_blocks == 0) { + FDKwriteBits(&bsWriter, FDKcrcGetCRC(&hAdts->crcInfo), 16); + } else { + int distance; + + /* Write CRC of current raw data block */ + FDKwriteBits(hBs, FDKcrcGetCRC(&hAdts->crcInfo), 16); + + /* Write distance to current data block */ + if (hAdts->currentBlock < hAdts->num_raw_blocks) { + FDKpushFor(&bsWriter, hAdts->currentBlock * 16); + distance = + FDKgetValidBits(hBs) - (56 + (hAdts->num_raw_blocks) * 16 + 16); + FDKwriteBits(&bsWriter, distance >> 3, 16); + } + } + FDKsyncCache(&bsWriter); + } + + /* Write total frame lenth for multiple raw data blocks and header CRC */ + if (hAdts->num_raw_blocks > 0 && + hAdts->currentBlock == hAdts->num_raw_blocks) { + FDK_BITSTREAM bsWriter; + int crcIndex = 0; + + FDKinitBitStream(&bsWriter, hBs->hBitBuf.Buffer, hBs->hBitBuf.bufSize, 0, + BS_WRITER); + + if (!hAdts->protection_absent) { + FDKcrcReset(&hAdts->crcInfo); + crcIndex = FDKcrcStartReg(&hAdts->crcInfo, &bsWriter, 0); + } + /* Write total frame length */ + FDKpushFor(&bsWriter, 56 - 28 + 2); + FDKwriteBits(&bsWriter, FDKgetValidBits(hBs) >> 3, 13); + + /* Write header CRC */ + if (!hAdts->protection_absent) { + FDKpushFor(&bsWriter, 11 + 2 + (hAdts->num_raw_blocks) * 16); + FDKcrcEndReg(&hAdts->crcInfo, &bsWriter, crcIndex); + FDKwriteBits(&bsWriter, FDKcrcGetCRC(&hAdts->crcInfo), 16); + } + FDKsyncCache(&bsWriter); + } + + /* Correct *pBits to reflect the amount of bits of the current subframe */ + *pBits -= hAdts->subFrameStartBit; + if (!hAdts->protection_absent && hAdts->num_raw_blocks > 0) { + /* Fixup CRC bits, since they come after each raw data block */ + *pBits += 16; + } + hAdts->currentBlock++; +} diff --git a/fdk-aac/libMpegTPEnc/src/tpenc_adts.h b/fdk-aac/libMpegTPEnc/src/tpenc_adts.h new file mode 100644 index 0000000..fe86306 --- /dev/null +++ b/fdk-aac/libMpegTPEnc/src/tpenc_adts.h @@ -0,0 +1,208 @@ +/* ----------------------------------------------------------------------------- +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): Alex Groeschel + + Description: ADTS Transport writer + +*******************************************************************************/ + +#ifndef TPENC_ADTS_H +#define TPENC_ADTS_H + +#include "tp_data.h" + +#include "FDK_crc.h" + +typedef struct { + INT sample_freq; + CHANNEL_MODE channel_mode; + UCHAR decoderCanDoMpeg4; + UCHAR mpeg_id; + UCHAR layer; + UCHAR protection_absent; + UCHAR profile; + UCHAR sample_freq_index; + UCHAR private_bit; + UCHAR original; + UCHAR home; + UCHAR copyright_id; + UCHAR copyright_start; + USHORT frame_length; + UCHAR num_raw_blocks; + UCHAR BufferFullnesStartFlag; + 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; + +typedef STRUCT_ADTS *HANDLE_ADTS; + +/** + * \brief Initialize ADTS data structure + * + * \param hAdts ADTS data handle + * \param config a valid CODER_CONFIG struct from where the required + * information for the ADTS header is extrated from + * + * \return 0 in case of success. + */ +INT adtsWrite_Init(HANDLE_ADTS hAdts, CODER_CONFIG *config); + +/** + * \brief Get the total bit overhead caused by ADTS + * + * \hAdts handle to ADTS data + * + * \return Amount of additional bits required for the current raw data block + */ +int adtsWrite_GetHeaderBits(HANDLE_ADTS hAdts); + +/** + * \brief Write an ADTS header into the given bitstream. May not write a header + * in case of multiple raw data blocks. + * + * \param hAdts ADTS data handle + * \param hBitStream bitstream handle into which the ADTS may be written into + * \param buffer_fullness the buffer fullness value for the ADTS header + * \param the current raw data block length + * + * \return 0 in case of success. + */ +INT adtsWrite_EncodeHeader(HANDLE_ADTS hAdts, HANDLE_FDK_BITSTREAM hBitStream, + int bufferFullness, int frame_length); +/** + * \brief Finish a ADTS raw data block + * + * \param hAdts ADTS data handle + * \param hBs bitstream handle into which the ADTS may be written into + * \param pBits a pointer to a integer holding the current bitstream buffer bit + * count, which is corrected to the current raw data block boundary. + * + */ +void adtsWrite_EndRawDataBlock(HANDLE_ADTS hAdts, HANDLE_FDK_BITSTREAM hBs, + int *bits); + +/** + * \brief Start CRC region with a maximum number of bits + * If mBits is positive zero padding will be used for CRC calculation, if + * there are less than mBits bits available. If mBits is negative no zero + * padding is done. If mBits is zero the memory for the buffer is + * allocated dynamically, the number of bits is not limited. + * + * \param pAdts ADTS data handle + * \param hBs bitstream handle of which the CRC region ends + * \param mBits limit of number of bits to be considered for the requested CRC + * region + * + * \return ID for the created region, -1 in case of an error + */ +int adtsWrite_CrcStartReg(HANDLE_ADTS pAdts, HANDLE_FDK_BITSTREAM hBs, + int mBits); + +/** + * \brief Ends CRC region identified by reg + * + * \param pAdts ADTS data handle + * \param hBs bitstream handle of which the CRC region ends + * \param reg a CRC region ID returned previously by adtsWrite_CrcStartReg() + */ +void adtsWrite_CrcEndReg(HANDLE_ADTS pAdts, HANDLE_FDK_BITSTREAM hBs, int reg); + +#endif /* TPENC_ADTS_H */ diff --git a/fdk-aac/libMpegTPEnc/src/tpenc_asc.cpp b/fdk-aac/libMpegTPEnc/src/tpenc_asc.cpp new file mode 100644 index 0000000..0b484a0 --- /dev/null +++ b/fdk-aac/libMpegTPEnc/src/tpenc_asc.cpp @@ -0,0 +1,996 @@ +/* ----------------------------------------------------------------------------- +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: + +*******************************************************************************/ + +#include "tp_data.h" + +#include "tpenc_lib.h" +#include "tpenc_asc.h" +#include "FDK_bitstream.h" +#include "genericStds.h" + +#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 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; + +/** + * 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_CONFIGURATION; + +/** + * The following arrays provide the IDs of the consecutive elements for each + * mode. + */ +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}; + +/** + * 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. + * + * \param channel_mode Current channel mode. + * + * \return + * - Pointer to PCE_CONFIGURATION entry, on success. + * - NULL, on failure. + */ +static const PCE_CONFIGURATION *getPceEntry(const CHANNEL_MODE channel_mode) { + UINT i; + const PCE_CONFIGURATION *pce_config = NULL; + + for (i = 0; i < (sizeof(pceConfigTab) / sizeof(CHANNEL_CONFIGURATION)); i++) { + if (pceConfigTab[i].channel_mode == channel_mode) { + pce_config = &pceConfigTab[i].pce_configuration; + break; + } + } + + return pce_config; +} + +int getChannelConfig(const CHANNEL_MODE channel_mode, + const UCHAR channel_config_zero) { + INT 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 chMode; + + if (noChannels <= 8 && noChannels > 0) + chMode = (CHANNEL_MODE)( + (noChannels == 8) ? 7 + : noChannels); /* see : iso/mpeg4 v1 audio subpart1*/ + else + chMode = MODE_UNKNOWN; + + return chMode; +} + +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, 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) { + return -1; + } + + 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 */ + } + + 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; + } + + /* assign cpe and tag information to either front, side or back channels */ + + pEl_list = config->pEl_type; + + 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; + } + } + + /* 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); + } + /* 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); + } + /* 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); + } + /* 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. + - num_assoc_data_elements always 0. */ + + /* Byte alignment: relative to alignAnchor + ADTS: align with respect to the first bit of the raw_data_block() + ADIF: align with respect to the first bit of the header + LATM: align with respect to the first bit of the ASC */ + FDKbyteAlign(hBs, alignAnchor); /* Alignment */ + + /* 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; + + if ((config = getPceEntry(channelMode)) == NULL) { + return -1; /* unsupported channelmapping */ + } + + bits += + 4 + 2 + 4; /* Element instance tag + Object type + Sample rate index */ + bits += 4 + 4 + 4 + 2; /* No (front + side + back + lfe channel) elements */ + bits += 3 + 4; /* No (assoc data + valid cc) elements */ + bits += 1 + 1 + 1; /* Mono + Stereo + Matrix mixdown present */ + + if (matrixMixdownA != 0 && + ((channelMode == MODE_1_2_2) || (channelMode == MODE_1_2_2_1))) { + bits += 3; /* matrix_mixdown_idx + pseudo_surround_enable */ + } + + bits += (1 + 4) * (INT)config->num_front_channel_elements; + bits += (1 + 4) * (INT)config->num_side_channel_elements; + bits += (1 + 4) * (INT)config->num_back_channel_elements; + bits += (4) * (INT)config->num_lfe_channel_elements; + + /* - num_valid_cc_elements always 0. + - num_assoc_data_elements always 0. */ + + if ((bits % 8) != 0) { + bits += (8 - (bits % 8)); /* Alignment */ + } + + bits += 8; /* Comment field bytes */ + + 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 */ + + if ((bits % 8) != 0) { + bits += (8 - (bits % 8)); /* Alignment */ + } + } + + return bits; +} + +static void writeAot(HANDLE_FDK_BITSTREAM hBitstreamBuffer, + AUDIO_OBJECT_TYPE aot) { + int tmp = (int)aot; + + if (tmp > 31) { + FDKwriteBits(hBitstreamBuffer, AOT_ESCAPE, 5); + FDKwriteBits(hBitstreamBuffer, tmp - 32, 6); /* AudioObjectType */ + } else { + FDKwriteBits(hBitstreamBuffer, tmp, 5); + } +} + +static void writeSampleRate(HANDLE_FDK_BITSTREAM hBs, int sampleRate, + int nBits) { + int srIdx = getSamplingRateIndex(sampleRate, nBits); + + FDKwriteBits(hBs, srIdx, nBits); + if (srIdx == (1 << nBits) - 1) { + FDKwriteBits(hBs, sampleRate, 24); + } +} + +static int transportEnc_writeGASpecificConfig(HANDLE_FDK_BITSTREAM asc, + CODER_CONFIG *config, int extFlg, + UINT alignAnchor) { + int aot = config->aot; + int samplesPerFrame = config->samplesPerFrame; + + /* start of GASpecificConfig according to ISO/IEC 14496-3 Subpart 4, 4.4.1 */ + FDKwriteBits(asc, + ((samplesPerFrame == 960 || samplesPerFrame == 480) ? 1 : 0), + 1); /* frameLengthFlag: 1 for a 960/480 (I)MDCT, 0 for a 1024/512 + (I)MDCT*/ + FDKwriteBits(asc, 0, + 1); /* dependsOnCoreCoder: Sampling Rate Coder Specific, see in + ISO/IEC 14496-3 Subpart 4, 4.4.1 */ + FDKwriteBits(asc, extFlg, + 1); /* Extension Flag: Shall be 1 for aot = 17,19,20,21,22,23 */ + + /* Write PCE if channel config is not 1-7 */ + if (getChannelConfig(config->channelMode, 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 */ + } + if ((aot == AOT_ER_AAC_LC) || (aot == AOT_ER_AAC_LTP) || + (aot == AOT_ER_AAC_SCAL) || (aot == AOT_ER_AAC_LD)) { + FDKwriteBits(asc, (config->flags & CC_VCB11) ? 1 : 0, + 1); /* aacSectionDataResillienceFlag */ + FDKwriteBits(asc, (config->flags & CC_RVLC) ? 1 : 0, + 1); /* aacScaleFactorDataResillienceFlag */ + FDKwriteBits(asc, (config->flags & CC_HCR) ? 1 : 0, + 1); /* aacSpectralDataResillienceFlag */ + } + FDKwriteBits(asc, 0, 1); /* extensionFlag3: reserved. Shall be '0' */ + } + return 0; +} + +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, 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*/ + + if (cb->cbSbr != NULL) { + const PCE_CONFIGURATION *pPce; + int e, sbrElementIndex = 0; + + pPce = getPceEntry(config->channelMode); + + 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++; + } + } + } + } + + 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, 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, 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; +} + +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) { + UINT extFlag = 0; + int err; + int epConfig = 0; + + /* Required for the PCE. */ + UINT alignAnchor = FDKgetValidBits(asc); + + /* Extension Flag: Shall be 1 for aot = 17,19,20,21,22,23,39 */ + switch (config->aot) { + case AOT_ER_AAC_LC: + case AOT_ER_AAC_LTP: + case AOT_ER_AAC_SCAL: + case AOT_ER_TWIN_VQ: + case AOT_ER_BSAC: + case AOT_ER_AAC_LD: + case AOT_ER_AAC_ELD: + case AOT_USAC: + extFlag = 1; + break; + default: + break; + } + + if (config->sbrSignaling == SIG_EXPLICIT_HIERARCHICAL && config->sbrPresent) + writeAot(asc, config->extAOT); + else + writeAot(asc, config->aot); + + /* 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; + } + + FDKwriteBits( + asc, getChannelConfig(config->channelMode, config->channelConfigZero), 4); + + if (config->sbrSignaling == SIG_EXPLICIT_HIERARCHICAL && config->sbrPresent) { + writeSampleRate(asc, config->extSamplingRate, 4); + writeAot(asc, config->aot); + } + + switch (config->aot) { + case AOT_AAC_MAIN: + case AOT_AAC_LC: + case AOT_AAC_SSR: + case AOT_AAC_LTP: + case AOT_AAC_SCAL: + case AOT_TWIN_VQ: + case AOT_ER_AAC_LC: + case AOT_ER_AAC_LTP: + case AOT_ER_AAC_SCAL: + case AOT_ER_TWIN_VQ: + case AOT_ER_BSAC: + case AOT_ER_AAC_LD: + err = + transportEnc_writeGASpecificConfig(asc, config, extFlag, alignAnchor); + if (err) return err; + break; + case AOT_ER_AAC_ELD: + err = transportEnc_writeELDSpecificConfig(asc, config, epConfig, cb); + if (err) return err; + break; + case AOT_USAC: + err = transportEnc_writeUsacSpecificConfig(asc, extFlag, config, cb); + if (err) { + return err; + } + break; + default: + return -1; + } + + switch (config->aot) { + case AOT_ER_AAC_LC: + case AOT_ER_AAC_LTP: + case AOT_ER_AAC_SCAL: + case AOT_ER_TWIN_VQ: + case AOT_ER_BSAC: + case AOT_ER_AAC_LD: + case AOT_ER_CELP: + case AOT_ER_HVXC: + case AOT_ER_HILN: + case AOT_ER_PARA: + case AOT_ER_AAC_ELD: + FDKwriteBits(asc, 0, 2); /* epconfig 0 */ + break; + default: + break; + } + + /* backward compatible explicit signaling of extension AOT */ + if (config->sbrSignaling == SIG_EXPLICIT_BW_COMPATIBLE) { + TP_ASC_EXTENSION_ID ascExtId = ASCEXT_UNKOWN; + + if (config->sbrPresent) { + ascExtId = ASCEXT_SBR; + FDKwriteBits(asc, ascExtId, 11); + writeAot(asc, config->extAOT); + 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 */ + } + } + } + + /* Make sure all bits are sync'ed */ + FDKsyncCache(asc); + + return 0; +} diff --git a/fdk-aac/libMpegTPEnc/src/tpenc_asc.h b/fdk-aac/libMpegTPEnc/src/tpenc_asc.h new file mode 100644 index 0000000..5f5621e --- /dev/null +++ b/fdk-aac/libMpegTPEnc/src/tpenc_asc.h @@ -0,0 +1,147 @@ +/* ----------------------------------------------------------------------------- +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): Manuel Jander + + Description: Audio Specific Config writer + +*******************************************************************************/ + +#ifndef TPENC_ASC_H +#define TPENC_ASC_H + +/** + * \brief Get channel config from channel mode. + * + * \param channel_mode channel mode + * \param channel_config_zero no standard channel configuration + * + * \return chanel config + */ +int getChannelConfig(const CHANNEL_MODE channel_mode, + const UCHAR channel_config_zero); + +/** + * \brief Write a Program Config Element. + * + * \param hBs bitstream handle into which the PCE is appended + * \param channelMode the channel mode to be used + * \param sampleRate the sample rate + * \param instanceTagPCE the instance tag of the Program Config Element + * \param profile the MPEG Audio profile to be used + * \param matrix mixdown gain + * \param pseudo surround indication + * \param reference bitstream position for alignment + * \return zero on success, non-zero on failure. + */ +int transportEnc_writePCE(HANDLE_FDK_BITSTREAM hBs, CHANNEL_MODE channelMode, + INT sampleRate, int instanceTagPCE, int profile, + int matrixMixdownA, int pseudoSurroundEnable, + UINT alignAnchor); + +/** + * \brief Get the bit count required by a Program Config Element + * + * \param channelMode the channel mode to be used + * \param matrix mixdown gain + * \param bit offset at which the PCE would start + * \return the amount of bits required for the PCE including the given bit + * offset. + */ +int transportEnc_GetPCEBits(CHANNEL_MODE channelMode, int matrixMixdownA, + int bits); + +#endif /* TPENC_ASC_H */ diff --git a/fdk-aac/libMpegTPEnc/src/tpenc_dab.cpp b/fdk-aac/libMpegTPEnc/src/tpenc_dab.cpp new file mode 100644 index 0000000..202fecf --- /dev/null +++ b/fdk-aac/libMpegTPEnc/src/tpenc_dab.cpp @@ -0,0 +1,467 @@ + +/* ----------------------------------------------------------------------------------------------------------- +Software License for The Fraunhofer FDK AAC Codec Library for Android + +� Copyright 1995 - 2012 Fraunhofer-Gesellschaft zur F�rderung der angewandten Forschung e.V. + All rights reserved. + + 1. INTRODUCTION +The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements +the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio. +This FDK AAC Codec software is intended to be used on a wide variety of Android devices. + +AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual +audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by +independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part +of the MPEG specifications. + +Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer) +may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners +individually for the purpose of encoding or decoding bit streams in products that are compliant with +the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license +these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec +software may already be covered under those patent licenses when it is used for those licensed purposes only. + +Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality, +are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional +applications information and documentation. + +2. COPYRIGHT LICENSE + +Redistribution and use in source and binary forms, with or without modification, are permitted without +payment of copyright license fees provided that you satisfy the following conditions: + +You must retain the complete text of this software license in redistributions of the FDK AAC Codec or +your modifications thereto in source code form. + +You must retain the complete text of this software license in the documentation and/or other materials +provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form. +You must make available free of charge copies of the complete source code of the FDK AAC Codec and your +modifications thereto to recipients of copies in binary form. + +The name of Fraunhofer may not be used to endorse or promote products derived from this library without +prior written permission. + +You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec +software or your modifications thereto. + +Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software +and the date of any change. For modified versions of the FDK AAC Codec, the term +"Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term +"Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android." + +3. NO PATENT LICENSE + +NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer, +ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with +respect to this software. + +You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized +by appropriate patent licenses. + +4. DISCLAIMER + +This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors +"AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties +of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages, +including but not limited to procurement of substitute goods or services; loss of use, data, or profits, +or business interruption, however caused and on any theory of liability, whether in contract, strict +liability, or tort (including negligence), arising in any way out of the use of this software, even if +advised of the possibility of such damage. + +5. CONTACT INFORMATION + +Fraunhofer Institute for Integrated Circuits IIS +Attention: Audio and Multimedia Departments - FDK AAC LL +Am Wolfsmantel 33 +91058 Erlangen, Germany + +www.iis.fraunhofer.de/amm +amm-info@iis.fraunhofer.de +----------------------------------------------------------------------------------------------------------- */ + +/******************************** MPEG Audio Encoder ************************** + + Initial author: serge + contents/description: DAB Transport Headers support + +******************************************************************************/ +#include <stdio.h> +#include "FDK_audio.h" +#include "tpenc_dab.h" + + +#include "tpenc_lib.h" +#include "tpenc_asc.h" + +#include "common_fix.h" + +int dabWrite_CrcStartReg( + HANDLE_DAB pDab, /*!< pointer to dab stucture */ + HANDLE_FDK_BITSTREAM hBs, /*!< handle to current bit buffer structure */ + int mBits /*!< number of bits in crc region */ + ) +{ + //fprintf(stderr, "dabWrite_CrcStartReg(%p): bits in crc region=%d\n", hBs, mBits); + return ( FDKcrcStartReg(&pDab->crcInfo2, hBs, mBits) ); +} + +void dabWrite_CrcEndReg( + HANDLE_DAB pDab, /*!< pointer to dab crc info stucture */ + HANDLE_FDK_BITSTREAM hBs, /*!< handle to current bit buffer structure */ + int reg /*!< crc region */ + ) +{ + //fprintf(stderr, "dabWrite_CrcEndReg(%p): crc region=%d\n", hBs, reg); + FDKcrcEndReg(&pDab->crcInfo2, hBs, reg); +} + +int dabWrite_GetHeaderBits( HANDLE_DAB hDab ) +{ + int bits = 0; + + if (hDab->currentBlock == 0) { + /* Static and variable header bits */ + bits += 16; //header_firecode 16 + bits += 8; //rfa=1, dac_rate=1, sbr_flag=1, aac_channel_mode=1, ps_flag=1, mpeg_surround_config=3 + bits += 12 * hDab->num_raw_blocks; //au_start[1...num_aus] 12 bit AU start position markers + + //4 byte alignment + if (hDab->dac_rate == 0 || hDab->sbr_flag == 0) + bits+=4; + //16sbr => 16 + 5 + 3 + 12*(2-1) => 36 => 40 bits 5 + //24sbr => 16 + 5 + 3 + 12*(3-1) => 48 ok 6 + //32sbr => 16 + 5 + 3 + 12*(4-1) => 60 => 64 bits 8 + //48sbr => 16 + 5 + 3 + 12*(6-1) => 84 => 88 bits 11 + } + + /* Add raw data block CRC bits. Not really part of the header, put they cause bit overhead to be accounted. */ + bits += 16; + + + return bits; +} + + +int dabWrite_CountTotalBitDemandHeader( HANDLE_DAB hDab, unsigned int streamDataLength ) +{ + //fprintf(stderr, "streamDataLength=%d (%d bytes)\n", streamDataLength, streamDataLength >> 3); + return dabWrite_GetHeaderBits(hDab); +} + + +INT dabWrite_Init(HANDLE_DAB hDab, CODER_CONFIG *config) +{ + /* Sanity checks */ + if((int)config->aot > 4 + || (int)config->aot < 1 ) { + return -1; + } + + /* Sanity checks DAB-specific */ + if ( !(config->nSubFrames == 2 && config->samplingRate == 16000 && (config->flags & CC_SBR)) && + !(config->nSubFrames == 3 && config->samplingRate == 24000 && (config->flags & CC_SBR)) && + !(config->nSubFrames == 4 && config->samplingRate == 32000) && + !(config->nSubFrames == 6 && config->samplingRate == 48000)) { + return -1; + } + + hDab->dac_rate = 0; + hDab->aac_channel_mode=0; + hDab->sbr_flag = 0; + hDab->ps_flag = 0; + hDab->mpeg_surround_config=0; + hDab->subchannels_num=config->bitRate/8000; + + + if(config->samplingRate == 24000 || config->samplingRate == 48000) + hDab->dac_rate = 1; + + if (config->extAOT==AOT_SBR || config->extAOT == AOT_PS) + hDab->sbr_flag = 1; + + if(config->extAOT == AOT_PS) + hDab->ps_flag = 1; + + + if(config->channelMode == MODE_2) + hDab->aac_channel_mode = 1; + + //fprintf(stderr, "hDab->dac_rate=%d\n", hDab->dac_rate); + //fprintf(stderr, "hDab->sbr_flag=%d\n", hDab->sbr_flag); + //fprintf(stderr, "hDab->ps_flag=%d\n", hDab->ps_flag); + //fprintf(stderr, "hDab->aac_channel_mode=%d\n", hDab->aac_channel_mode); + //fprintf(stderr, "hDab->subchannels_num=%d\n", hDab->subchannels_num); + //fprintf(stderr, "cc->nSubFrames=%d\n", config->nSubFrames); + + hDab->num_raw_blocks=config->nSubFrames-1; /* 0 means 1 raw data block */ + + FDKcrcInit(&hDab->crcInfo, 0x1021, 0xFFFF, 16); + FDKcrcInit(&hDab->crcFire, 0x782d, 0, 16); + FDKcrcInit(&hDab->crcInfo2, 0x8005, 0xFFFF, 16); + + hDab->currentBlock = 0; + hDab->headerBits = dabWrite_GetHeaderBits(hDab); + + return 0; +} + +int dabWrite_EncodeHeader(HANDLE_DAB hDab, + HANDLE_FDK_BITSTREAM hBitStream, + int buffer_fullness, + int frame_length) +{ + INT crcIndex = 0; + + + FDK_ASSERT(((frame_length+hDab->headerBits)/8)<0x2000); /*13 bit*/ + FDK_ASSERT(buffer_fullness<0x800); /* 11 bit */ + + FDKcrcReset(&hDab->crcInfo); + + +// fprintf(stderr, "dabWrite_EncodeHeader() hDab->currentBlock=%d, frame_length=%d, buffer_fullness=%d\n", +// hDab->currentBlock, frame_length, buffer_fullness); + +// if (hDab->currentBlock == 0) { +// //hDab->subFrameStartPrev=dabWrite_GetHeaderBits(hDab); +// fprintf(stderr, "header bits[%d] [%d]\n", hDab->subFrameStartPrev, hDab->subFrameStartPrev >> 3); +// FDKresetBitbuffer(hBitStream, BS_WRITER); +// } + + //hDab->subFrameStartBit = FDKgetValidBits(hBitStream); +// fprintf(stderr, "dabWrite_EncodeHeader() hDab->subFrameStartBit=%d [%d]\n", hDab->subFrameStartBit, hDab->subFrameStartBit >> 3); + + //hDab->subFrameStartBit = FDKgetValidBits(hBitStream); + /* Skip new header if this is raw data block 1..n */ + if (hDab->currentBlock == 0) + { + FDKresetBitbuffer(hBitStream, BS_WRITER); +// fprintf(stderr, "dabWrite_EncodeHeader() after FDKresetBitbuffer=%d [%d]\n", FDKgetValidBits(hBitStream), FDKgetValidBits(hBitStream) >> 3); + + /* fixed header */ + FDKwriteBits(hBitStream, 0, 16); //header_firecode + FDKwriteBits(hBitStream, 0, 1); //rfa + FDKwriteBits(hBitStream, hDab->dac_rate, 1); + FDKwriteBits(hBitStream, hDab->sbr_flag, 1); + FDKwriteBits(hBitStream, hDab->aac_channel_mode, 1); + FDKwriteBits(hBitStream, hDab->ps_flag, 1); + FDKwriteBits(hBitStream, hDab->mpeg_surround_config, 3); + /* variable header */ + int i; + for(i=0; i<hDab->num_raw_blocks; i++) + FDKwriteBits(hBitStream, 0, 12); + /* padding */ + if (hDab->dac_rate == 0 || hDab->sbr_flag == 0) { + FDKwriteBits(hBitStream, 0, 4); + } + } /* End of DAB header */ + + hDab->subFrameStartBit = FDKgetValidBits(hBitStream); + FDK_ASSERT(FDKgetValidBits(hBitStream) % 8 == 0); //only aligned header + +// fprintf(stderr, "dabWrite_EncodeHeader() FDKgetValidBits(hBitStream)=%d [%d]\n", FDKgetValidBits(hBitStream), FDKgetValidBits(hBitStream) >> 3); + return 0; +} + +int dabWrite_writeExtensionFillPayload(HANDLE_FDK_BITSTREAM hBitStream, int extPayloadBits) +{ +#define EXT_TYPE_BITS ( 4 ) +#define DATA_EL_VERSION_BITS ( 4 ) +#define FILL_NIBBLE_BITS ( 4 ) + +#define EXT_TYPE_BITS ( 4 ) +#define DATA_EL_VERSION_BITS ( 4 ) +#define FILL_NIBBLE_BITS ( 4 ) + + INT extBitsUsed = 0; + INT extPayloadType = EXT_FIL; + //fprintf(stderr, "FDKaacEnc_writeExtensionPayload() extPayloadType=%d\n", extPayloadType); + if (extPayloadBits >= EXT_TYPE_BITS) + { + UCHAR fillByte = 0x00; /* for EXT_FIL and EXT_FILL_DATA */ + + if (hBitStream != NULL) { + FDKwriteBits(hBitStream, extPayloadType, EXT_TYPE_BITS); + } + extBitsUsed += EXT_TYPE_BITS; + + switch (extPayloadType) { + case EXT_FILL_DATA: + fillByte = 0xA5; + case EXT_FIL: + default: + if (hBitStream != NULL) { + int writeBits = extPayloadBits; + FDKwriteBits(hBitStream, 0x00, FILL_NIBBLE_BITS); + writeBits -= 8; /* acount for the extension type and the fill nibble */ + while (writeBits >= 8) { + FDKwriteBits(hBitStream, fillByte, 8); + writeBits -= 8; + } + } + extBitsUsed += FILL_NIBBLE_BITS + (extPayloadBits & ~0x7) - 8; + break; + } + } + + return (extBitsUsed); +} + +void dabWrite_FillRawDataBlock(HANDLE_FDK_BITSTREAM hBitStream, int payloadBits) +{ + INT extBitsUsed = 0; +#define EL_ID_BITS ( 3 ) +#define FILL_EL_COUNT_BITS ( 4 ) +#define FILL_EL_ESC_COUNT_BITS ( 8 ) +#define MAX_FILL_DATA_BYTES ( 269 ) + while (payloadBits >= (EL_ID_BITS + FILL_EL_COUNT_BITS)) { + INT cnt, esc_count=-1, alignBits=7; + + payloadBits -= EL_ID_BITS + FILL_EL_COUNT_BITS; + if (payloadBits >= 15*8) { + payloadBits -= FILL_EL_ESC_COUNT_BITS; + esc_count = 0; /* write esc_count even if cnt becomes smaller 15 */ + } + alignBits = 0; + + cnt = fixMin(MAX_FILL_DATA_BYTES, (payloadBits+alignBits)>>3); + + if (cnt >= 15) { + esc_count = cnt - 15 + 1; + } + + if (hBitStream != NULL) { + /* write bitstream */ + FDKwriteBits(hBitStream, ID_FIL, EL_ID_BITS); + if (esc_count >= 0) { + FDKwriteBits(hBitStream, 15, FILL_EL_COUNT_BITS); + FDKwriteBits(hBitStream, esc_count, FILL_EL_ESC_COUNT_BITS); + } else { + FDKwriteBits(hBitStream, cnt, FILL_EL_COUNT_BITS); + } + } + + extBitsUsed += EL_ID_BITS + FILL_EL_COUNT_BITS + ((esc_count>=0) ? FILL_EL_ESC_COUNT_BITS : 0); + + cnt = fixMin(cnt*8, payloadBits); /* convert back to bits */ +#if 0 + extBitsUsed += FDKaacEnc_writeExtensionPayload( hBitStream, + pExtension->type, + pExtension->pPayload, + cnt ); +#else + extBitsUsed += dabWrite_writeExtensionFillPayload(hBitStream, cnt); +#endif + payloadBits -= cnt; + } +} + +void dabWrite_EndRawDataBlock(HANDLE_DAB hDab, + HANDLE_FDK_BITSTREAM hBs, + int *pBits) +{ + FDK_BITSTREAM bsWriter; + INT crcIndex = 0; + USHORT crcData; + INT writeBits=0; + INT writeBitsNonLastBlock=0; + INT writeBitsLastBlock=0; +#if 1 + if (hDab->currentBlock == hDab->num_raw_blocks) { + //calculate byte-alignment before writing ID_FIL + if((FDKgetValidBits(hBs)+3) % 8){ + writeBits = 8 - ((FDKgetValidBits(hBs)+3) % 8); + } + + INT offset_end = hDab->subchannels_num*110*8 - 2*8 - 3; + writeBitsLastBlock = offset_end - FDKgetValidBits(hBs); + dabWrite_FillRawDataBlock(hBs, writeBitsLastBlock); + FDKsyncCache(hBs); + //fprintf(stderr, "FIL-element written=%d\n", writeBitsLastBlock); + writeBitsLastBlock=writeBits; + } +#endif + FDKwriteBits(hBs, 7, 3); //finalize AU: ID_END + FDKsyncCache(hBs); + //byte-align (if ID_FIL doesn't align it). + if(FDKgetValidBits(hBs) % 8){ + writeBits = 8 - (FDKgetValidBits(hBs) % 8); + FDKwriteBits(hBs, 0x00, writeBits); + FDKsyncCache(hBs); + } + + //fake-written bits alignment for last AU + if (hDab->currentBlock == hDab->num_raw_blocks) + writeBits=writeBitsLastBlock; + + INT frameLen = (FDKgetValidBits(hBs) - hDab->subFrameStartBit) >> 3; + //fprintf(stderr, "frame=%d, offset writeBits=%d\n", frameLen, writeBits); + + FDK_ASSERT(FDKgetValidBits(hBs) % 8 == 0); //only aligned au's + FDK_ASSERT(hDab->subchannels_num*110*8 >= FDKgetValidBits(hBs)+2*8); //don't overlap superframe + + FDKinitBitStream(&bsWriter, hBs->hBitBuf.Buffer, hBs->hBitBuf.bufSize, 0, BS_WRITER); + FDKpushFor(&bsWriter, hDab->subFrameStartBit); + FDKcrcReset(&hDab->crcInfo); + hDab->crcIndex = FDKcrcStartReg(&hDab->crcInfo, &bsWriter, 0); +#if 0 + if (hDab->currentBlock == hDab->num_raw_blocks) { + INT offset_size = hDab->subchannels_num*110*8 - 2*8 - FDKgetValidBits(hBs); + //fprintf(stderr, "offset_size=%d\n", offset_size >> 3); + FDKpushFor(hBs, offset_size); + } +#endif + + FDKpushFor(&bsWriter, FDKgetValidBits(hBs) - hDab->subFrameStartBit); + FDKcrcEndReg(&hDab->crcInfo, &bsWriter, hDab->crcIndex); + crcData = FDKcrcGetCRC(&hDab->crcInfo); + //fprintf(stderr, "crcData = %04x\n", crcData); + /* Write inverted CRC of current raw data block */ + FDKwriteBits(hBs, crcData ^ 0xffff, 16); + FDKsyncCache(hBs); + + + /* Write distance to current data block */ + if(hDab->currentBlock) { + FDKinitBitStream(&bsWriter, hBs->hBitBuf.Buffer, hBs->hBitBuf.bufSize, 0, BS_WRITER); + FDKpushFor(&bsWriter, 24 + (hDab->currentBlock-1)*12); + //fprintf(stderr, "FDKwriteBits() = %d\n", hDab->subFrameStartBit>>3); + FDKwriteBits(&bsWriter, (hDab->subFrameStartBit>>3), 12); + FDKsyncCache(&bsWriter); + } + + /* Write FireCode */ + if (hDab->currentBlock == hDab->num_raw_blocks) { + FDKinitBitStream(&bsWriter, hBs->hBitBuf.Buffer, hBs->hBitBuf.bufSize, 0, BS_WRITER); + FDKpushFor(&bsWriter, 16); + + FDKcrcReset(&hDab->crcFire); + crcIndex = FDKcrcStartReg(&hDab->crcFire, &bsWriter, 72); + FDKpushFor(&bsWriter, 9*8); //9bytes + FDKcrcEndReg(&hDab->crcFire, &bsWriter, crcIndex); + + crcData = FDKcrcGetCRC(&hDab->crcFire); + //fprintf(stderr, "Firecode: %04x\n", crcData); + + FDKinitBitStream(&bsWriter, hBs->hBitBuf.Buffer, hBs->hBitBuf.bufSize, 0, BS_WRITER); + FDKwriteBits(&bsWriter, crcData, 16); + FDKsyncCache(&bsWriter); + } + + if (hDab->currentBlock == 0) + *pBits += hDab->headerBits; + else + *pBits += 16; + + *pBits += writeBits + 3; //size: ID_END + alignment + + /* Correct *pBits to reflect the amount of bits of the current subframe */ + *pBits -= hDab->subFrameStartBit; + /* Fixup CRC bits, since they come after each raw data block */ + + hDab->currentBlock++; + //fprintf(stderr, "dabWrite_EndRawDataBlock() *pBits=%d (%d)\n", *pBits, *pBits >> 3); +} + diff --git a/fdk-aac/libMpegTPEnc/src/tpenc_dab.h b/fdk-aac/libMpegTPEnc/src/tpenc_dab.h new file mode 100644 index 0000000..17b83c6 --- /dev/null +++ b/fdk-aac/libMpegTPEnc/src/tpenc_dab.h @@ -0,0 +1,217 @@ + +/* ----------------------------------------------------------------------------------------------------------- +Software License for The Fraunhofer FDK AAC Codec Library for Android + +� Copyright 1995 - 2012 Fraunhofer-Gesellschaft zur F�rderung der angewandten Forschung e.V. + All rights reserved. + + 1. INTRODUCTION +The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements +the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio. +This FDK AAC Codec software is intended to be used on a wide variety of Android devices. + +AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual +audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by +independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part +of the MPEG specifications. + +Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer) +may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners +individually for the purpose of encoding or decoding bit streams in products that are compliant with +the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license +these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec +software may already be covered under those patent licenses when it is used for those licensed purposes only. + +Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality, +are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional +applications information and documentation. + +2. COPYRIGHT LICENSE + +Redistribution and use in source and binary forms, with or without modification, are permitted without +payment of copyright license fees provided that you satisfy the following conditions: + +You must retain the complete text of this software license in redistributions of the FDK AAC Codec or +your modifications thereto in source code form. + +You must retain the complete text of this software license in the documentation and/or other materials +provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form. +You must make available free of charge copies of the complete source code of the FDK AAC Codec and your +modifications thereto to recipients of copies in binary form. + +The name of Fraunhofer may not be used to endorse or promote products derived from this library without +prior written permission. + +You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec +software or your modifications thereto. + +Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software +and the date of any change. For modified versions of the FDK AAC Codec, the term +"Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term +"Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android." + +3. NO PATENT LICENSE + +NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer, +ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with +respect to this software. + +You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized +by appropriate patent licenses. + +4. DISCLAIMER + +This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors +"AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties +of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages, +including but not limited to procurement of substitute goods or services; loss of use, data, or profits, +or business interruption, however caused and on any theory of liability, whether in contract, strict +liability, or tort (including negligence), arising in any way out of the use of this software, even if +advised of the possibility of such damage. + +5. CONTACT INFORMATION + +Fraunhofer Institute for Integrated Circuits IIS +Attention: Audio and Multimedia Departments - FDK AAC LL +Am Wolfsmantel 33 +91058 Erlangen, Germany + +www.iis.fraunhofer.de/amm +amm-info@iis.fraunhofer.de +----------------------------------------------------------------------------------------------------------- */ + +/******************************** MPEG Audio Encoder ************************** + + Initial author: serge + contents/description: DAB Transport writer + +******************************************************************************/ + +#ifndef TPENC_DAB_H +#define TPENC_DAB_H + + + +#include "tp_data.h" + +#include "FDK_crc.h" + +typedef struct { + USHORT frame_length; + UCHAR dac_rate; + UCHAR aac_channel_mode; + UCHAR sbr_flag; + UCHAR ps_flag; + UCHAR mpeg_surround_config; + UCHAR num_raw_blocks; + UCHAR BufferFullnesStartFlag; + int subchannels_num; + 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 */ + //int subFrameStartPrev; /*!< Bit position where the previous raw data block begins */ + int crcIndex; + FDK_CRCINFO crcInfo; + FDK_CRCINFO crcFire; + FDK_CRCINFO crcInfo2; + USHORT tab[256]; +} STRUCT_DAB; + +typedef STRUCT_DAB *HANDLE_DAB; + +/** + * \brief Initialize DAB data structure + * + * \param hDab DAB data handle + * \param config a valid CODER_CONFIG struct from where the required + * information for the DAB header is extrated from + * + * \return 0 in case of success. + */ +INT dabWrite_Init( + HANDLE_DAB hDab, + CODER_CONFIG *config + ); + +/** + * \brief Get the total bit overhead caused by DAB + * + * \hDab handle to DAB data + * + * \return Amount of additional bits required for the current raw data block + */ +int dabWrite_GetHeaderBits( HANDLE_DAB hDab ); +int dabWrite_CountTotalBitDemandHeader( HANDLE_DAB hDab, unsigned int streamDataLength ); + +/** + * \brief Write an DAB header into the given bitstream. May not write a header + * in case of multiple raw data blocks. + * + * \param hDab DAB data handle + * \param hBitStream bitstream handle into which the DAB may be written into + * \param buffer_fullness the buffer fullness value for the DAB header + * \param the current raw data block length + * + * \return 0 in case of success. + */ +INT dabWrite_EncodeHeader( + HANDLE_DAB hDab, + HANDLE_FDK_BITSTREAM hBitStream, + int bufferFullness, + int frame_length + ); +/** + * \brief Finish a DAB raw data block + * + * \param hDab DAB data handle + * \param hBs bitstream handle into which the DAB may be written into + * \param pBits a pointer to a integer holding the current bitstream buffer bit count, + * which is corrected to the current raw data block boundary. + * + */ +void dabWrite_EndRawDataBlock( + HANDLE_DAB hDab, + HANDLE_FDK_BITSTREAM hBs, + int *bits + ); + + +/** + * \brief Start CRC region with a maximum number of bits + * If mBits is positive zero padding will be used for CRC calculation, if there + * are less than mBits bits available. + * If mBits is negative no zero padding is done. + * If mBits is zero the memory for the buffer is allocated dynamically, the + * number of bits is not limited. + * + * \param pDab DAB data handle + * \param hBs bitstream handle of which the CRC region ends + * \param mBits limit of number of bits to be considered for the requested CRC region + * + * \return ID for the created region, -1 in case of an error + */ +int dabWrite_CrcStartReg( + HANDLE_DAB pDab, + HANDLE_FDK_BITSTREAM hBs, + int mBits + ); + +/** + * \brief Ends CRC region identified by reg + * + * \param pDab DAB data handle + * \param hBs bitstream handle of which the CRC region ends + * \param reg a CRC region ID returned previously by dabWrite_CrcStartReg() + */ +void dabWrite_CrcEndReg( + HANDLE_DAB pDab, + HANDLE_FDK_BITSTREAM hBs, + int reg + ); + + + + +#endif /* TPENC_DAB_H */ + diff --git a/fdk-aac/libMpegTPEnc/src/tpenc_latm.cpp b/fdk-aac/libMpegTPEnc/src/tpenc_latm.cpp new file mode 100644 index 0000000..2d35d48 --- /dev/null +++ b/fdk-aac/libMpegTPEnc/src/tpenc_latm.cpp @@ -0,0 +1,850 @@ +/* ----------------------------------------------------------------------------- +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: + +*******************************************************************************/ + +#include "tpenc_latm.h" + +#include "genericStds.h" + +static const short celpFrameLengthTable[64] = { + 154, 170, 186, 147, 156, 165, 114, 120, 186, 126, 132, 138, 142, + 146, 154, 166, 174, 182, 190, 198, 206, 210, 214, 110, 114, 118, + 120, 122, 218, 230, 242, 254, 266, 278, 286, 294, 318, 342, 358, + 374, 390, 406, 422, 136, 142, 148, 154, 160, 166, 170, 174, 186, + 198, 206, 214, 222, 230, 238, 216, 160, 280, 338, 0, 0}; + +/******* + write value to transport stream + first two bits define the size of the value itself + then the value itself, with a size of 0-3 bytes +*******/ +static UINT transportEnc_LatmWriteValue(HANDLE_FDK_BITSTREAM hBs, int value) { + UCHAR valueBytes = 4; + unsigned int bitsWritten = 0; + int i; + + if (value < (1 << 8)) { + valueBytes = 1; + } else if (value < (1 << 16)) { + valueBytes = 2; + } else if (value < (1 << 24)) { + valueBytes = 3; + } else { + valueBytes = 4; + } + + FDKwriteBits(hBs, valueBytes - 1, 2); /* size of value in Bytes */ + for (i = 0; i < valueBytes; i++) { + /* write most significant Byte first */ + FDKwriteBits(hBs, (UCHAR)(value >> ((valueBytes - 1 - i) << 3)), 8); + } + + bitsWritten = (valueBytes << 3) + 2; + + return bitsWritten; +} + +static UINT transportEnc_LatmCountFixBitDemandHeader(HANDLE_LATM_STREAM hAss) { + int bitDemand = 0; + int insertSetupData = 0; + + /* only if start of new latm frame */ + if (hAss->subFrameCnt == 0) { + /* AudioSyncStream */ + + if (hAss->tt == TT_MP4_LOAS) { + bitDemand += 11; /* syncword */ + bitDemand += 13; /* audioMuxLengthBytes */ + } + + /* AudioMuxElement*/ + + /* AudioMuxElement::Stream Mux Config */ + if (hAss->muxConfigPeriod > 0) { + insertSetupData = (hAss->latmFrameCounter == 0); + } else { + insertSetupData = 0; + } + + if (hAss->tt != TT_MP4_LATM_MCP0) { + /* AudioMuxElement::useSameStreamMux Flag */ + bitDemand += 1; + + if (insertSetupData) { + bitDemand += hAss->streamMuxConfigBits; + } + } + + /* AudioMuxElement::otherDataBits */ + bitDemand += hAss->otherDataLenBits; + + /* AudioMuxElement::ByteAlign */ + if (bitDemand % 8) { + hAss->fillBits = 8 - (bitDemand % 8); + bitDemand += hAss->fillBits; + } else { + hAss->fillBits = 0; + } + } + + return bitDemand; +} + +static UINT transportEnc_LatmCountVarBitDemandHeader( + HANDLE_LATM_STREAM hAss, unsigned int streamDataLength) { + int bitDemand = 0; + int prog, layer; + + /* Payload Length Info*/ + if (hAss->allStreamsSameTimeFraming) { + for (prog = 0; prog < hAss->noProgram; prog++) { + for (layer = 0; layer < LATM_MAX_LAYERS; layer++) { + LATM_LAYER_INFO *p_linfo = &(hAss->m_linfo[prog][layer]); + + if (p_linfo->streamID >= 0) { + switch (p_linfo->frameLengthType) { + case 0: + if (streamDataLength > 0) { + streamDataLength -= bitDemand; + while (streamDataLength >= (255 << 3)) { + bitDemand += 8; + streamDataLength -= (255 << 3); + } + bitDemand += 8; + } + break; + + case 1: + case 4: + case 6: + bitDemand += 2; + break; + + default: + return 0; + } + } + } + } + } else { + /* there are many possibilities to use this mechanism. */ + switch (hAss->varMode) { + case LATMVAR_SIMPLE_SEQUENCE: { + /* Use the sequence generated by the encoder */ + // int streamCntPosition = transportEnc_SetWritePointer( + // hAss->hAssemble, 0 ); int streamCntPosition = FDKgetValidBits( + // hAss->hAssemble ); + bitDemand += 4; + + hAss->varStreamCnt = 0; + for (prog = 0; prog < hAss->noProgram; prog++) { + for (layer = 0; layer < LATM_MAX_LAYERS; layer++) { + LATM_LAYER_INFO *p_linfo = &(hAss->m_linfo[prog][layer]); + + if (p_linfo->streamID >= 0) { + bitDemand += 4; /* streamID */ + switch (p_linfo->frameLengthType) { + case 0: + streamDataLength -= bitDemand; + while (streamDataLength >= (255 << 3)) { + bitDemand += 8; + streamDataLength -= (255 << 3); + } + + bitDemand += 8; + break; + /*bitDemand += 1; endFlag + break;*/ + + case 1: + case 4: + case 6: + + break; + + default: + return 0; + } + hAss->varStreamCnt++; + } + } + } + bitDemand += 4; + // transportEnc_UpdateBitstreamField( hAss->hAssemble, + // streamCntPosition, hAss->varStreamCnt-1, 4 ); UINT pos = + // streamCntPosition-FDKgetValidBits(hAss->hAssemble); FDKpushBack( + // hAss->hAssemble, pos); FDKwriteBits( hAss->hAssemble, + // hAss->varStreamCnt-1, 4); FDKpushFor( hAss->hAssemble, pos-4); + } break; + + default: + return 0; + } + } + + return bitDemand; +} + +TRANSPORTENC_ERROR +CreateStreamMuxConfig(HANDLE_LATM_STREAM hAss, HANDLE_FDK_BITSTREAM hBs, + int bufferFullness, CSTpCallBacks *cb) { + INT streamIDcnt, tmp; + int layer, prog; + + USHORT coreFrameOffset = 0; + + hAss->taraBufferFullness = 0xFF; + hAss->audioMuxVersionA = 0; /* for future extensions */ + hAss->streamMuxConfigBits = 0; + + FDKwriteBits(hBs, hAss->audioMuxVersion, 1); /* audioMuxVersion */ + hAss->streamMuxConfigBits += 1; + + if (hAss->audioMuxVersion == 1) { + FDKwriteBits(hBs, hAss->audioMuxVersionA, 1); /* audioMuxVersionA */ + hAss->streamMuxConfigBits += 1; + } + + if (hAss->audioMuxVersionA == 0) { + if (hAss->audioMuxVersion == 1) { + hAss->streamMuxConfigBits += transportEnc_LatmWriteValue( + hBs, hAss->taraBufferFullness); /* taraBufferFullness */ + } + FDKwriteBits(hBs, hAss->allStreamsSameTimeFraming ? 1 : 0, + 1); /* allStreamsSameTimeFraming */ + FDKwriteBits(hBs, hAss->noSubframes - 1, 6); /* Number of Subframes */ + FDKwriteBits(hBs, hAss->noProgram - 1, 4); /* Number of Programs */ + + hAss->streamMuxConfigBits += 11; + + streamIDcnt = 0; + for (prog = 0; prog < hAss->noProgram; prog++) { + int transLayer = 0; + + FDKwriteBits(hBs, hAss->noLayer[prog] - 1, 3); + hAss->streamMuxConfigBits += 3; + + for (layer = 0; layer < LATM_MAX_LAYERS; layer++) { + LATM_LAYER_INFO *p_linfo = &(hAss->m_linfo[prog][layer]); + CODER_CONFIG *p_lci = hAss->config[prog][layer]; + + p_linfo->streamID = -1; + + if (hAss->config[prog][layer] != NULL) { + int useSameConfig = 0; + + if (transLayer > 0) { + FDKwriteBits(hBs, useSameConfig ? 1 : 0, 1); + hAss->streamMuxConfigBits += 1; + } + if ((useSameConfig == 0) || (transLayer == 0)) { + const UINT alignAnchor = FDKgetValidBits(hBs); + + if (0 != + (transportEnc_writeASC(hBs, hAss->config[prog][layer], cb))) { + return TRANSPORTENC_UNKOWN_ERROR; + } + + if (hAss->audioMuxVersion == 1) { + UINT ascLen = transportEnc_LatmWriteValue(hBs, 0); + FDKbyteAlign(hBs, alignAnchor); + ascLen = FDKgetValidBits(hBs) - alignAnchor - ascLen; + FDKpushBack(hBs, FDKgetValidBits(hBs) - alignAnchor); + + transportEnc_LatmWriteValue(hBs, ascLen); + + 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 */ + } + transLayer++; + + if (!hAss->allStreamsSameTimeFraming) { + if (streamIDcnt >= LATM_MAX_STREAM_ID) + return TRANSPORTENC_INVALID_CONFIG; + } + p_linfo->streamID = streamIDcnt++; + + switch (p_lci->aot) { + case AOT_AAC_MAIN: + case AOT_AAC_LC: + case AOT_AAC_SSR: + case AOT_AAC_LTP: + case AOT_AAC_SCAL: + case AOT_ER_AAC_LD: + case AOT_ER_AAC_ELD: + case AOT_USAC: + p_linfo->frameLengthType = 0; + + FDKwriteBits(hBs, p_linfo->frameLengthType, + 3); /* frameLengthType */ + FDKwriteBits(hBs, bufferFullness, 8); /* bufferFullness */ + hAss->streamMuxConfigBits += 11; + + if (!hAss->allStreamsSameTimeFraming) { + CODER_CONFIG *p_lci_prev = hAss->config[prog][layer - 1]; + if (((p_lci->aot == AOT_AAC_SCAL) || + (p_lci->aot == AOT_ER_AAC_SCAL)) && + ((p_lci_prev->aot == AOT_CELP) || + (p_lci_prev->aot == AOT_ER_CELP))) { + FDKwriteBits(hBs, coreFrameOffset, 6); /* coreFrameOffset */ + hAss->streamMuxConfigBits += 6; + } + } + break; + + case AOT_TWIN_VQ: + p_linfo->frameLengthType = 1; + tmp = ((p_lci->bitsFrame + 7) >> 3) - + 20; /* transmission frame length in bytes */ + if ((tmp < 0)) { + return TRANSPORTENC_INVALID_TRANSMISSION_FRAME_LENGTH; + } + FDKwriteBits(hBs, p_linfo->frameLengthType, + 3); /* frameLengthType */ + FDKwriteBits(hBs, tmp, 9); + hAss->streamMuxConfigBits += 12; + + p_linfo->frameLengthBits = (tmp + 20) << 3; + break; + + case AOT_CELP: + p_linfo->frameLengthType = 4; + FDKwriteBits(hBs, p_linfo->frameLengthType, + 3); /* frameLengthType */ + hAss->streamMuxConfigBits += 3; + { + int i; + for (i = 0; i < 62; i++) { + if (celpFrameLengthTable[i] == p_lci->bitsFrame) break; + } + if (i >= 62) { + return TRANSPORTENC_INVALID_CELP_FRAME_LENGTH; + } + + FDKwriteBits(hBs, i, 6); /* CELPframeLengthTabelIndex */ + hAss->streamMuxConfigBits += 6; + } + p_linfo->frameLengthBits = p_lci->bitsFrame; + break; + + case AOT_HVXC: + p_linfo->frameLengthType = 6; + FDKwriteBits(hBs, p_linfo->frameLengthType, + 3); /* frameLengthType */ + hAss->streamMuxConfigBits += 3; + { + int i; + + if (p_lci->bitsFrame == 40) { + i = 0; + } else if (p_lci->bitsFrame == 80) { + i = 1; + } else { + return TRANSPORTENC_INVALID_FRAME_BITS; + } + FDKwriteBits(hBs, i, 1); /* HVXCframeLengthTableIndex */ + hAss->streamMuxConfigBits += 1; + } + p_linfo->frameLengthBits = p_lci->bitsFrame; + break; + + case AOT_NULL_OBJECT: + default: + return TRANSPORTENC_INVALID_AOT; + } + } + } + } + + FDKwriteBits(hBs, (hAss->otherDataLenBits > 0) ? 1 : 0, + 1); /* otherDataPresent */ + hAss->streamMuxConfigBits += 1; + + if (hAss->otherDataLenBits > 0) { + FDKwriteBits(hBs, 0, 1); + FDKwriteBits(hBs, hAss->otherDataLenBits, 8); + hAss->streamMuxConfigBits += 9; + } + + FDKwriteBits(hBs, 0, 1); /* crcCheckPresent=0 */ + hAss->streamMuxConfigBits += 1; + + } else { /* if ( audioMuxVersionA == 0 ) */ + + /* for future extensions */ + } + + return TRANSPORTENC_OK; +} + +static TRANSPORTENC_ERROR WriteAuPayloadLengthInfo( + HANDLE_FDK_BITSTREAM hBitStream, int AuLengthBits) { + int restBytes; + + if (AuLengthBits % 8) return TRANSPORTENC_INVALID_AU_LENGTH; + + while (AuLengthBits >= 255 * 8) { + FDKwriteBits(hBitStream, 255, 8); /* 255 shows incomplete AU */ + AuLengthBits -= (255 * 8); + } + + restBytes = (AuLengthBits) >> 3; + FDKwriteBits(hBitStream, restBytes, 8); + + return TRANSPORTENC_OK; +} + +static TRANSPORTENC_ERROR transportEnc_LatmSetNrOfSubframes( + HANDLE_LATM_STREAM hAss, INT noSubframes_next) /* nr of access units / + payloads within a latm + frame */ +{ + /* sanity chk */ + if (noSubframes_next < 1 || noSubframes_next > MAX_NR_OF_SUBFRAMES) { + return TRANSPORTENC_LATM_INVALID_NR_OF_SUBFRAMES; + } + + hAss->noSubframes_next = noSubframes_next; + + /* if at start then we can take over the value immediately, otherwise we have + * to wait for the next SMC */ + if ((hAss->subFrameCnt == 0) && (hAss->latmFrameCounter == 0)) { + hAss->noSubframes = noSubframes_next; + } + + return TRANSPORTENC_OK; +} + +static int allStreamsSameTimeFraming(HANDLE_LATM_STREAM hAss, UCHAR noProgram, + UCHAR noLayer[] /* return */) { + int prog, layer; + + signed int lastNoSamples = -1; + signed int minFrameSamples = FDK_INT_MAX; + signed int maxFrameSamples = 0; + + signed int highestSamplingRate = -1; + + for (prog = 0; prog < noProgram; prog++) { + noLayer[prog] = 0; + + for (layer = 0; layer < LATM_MAX_LAYERS; layer++) { + if (hAss->config[prog][layer] != NULL) { + INT hsfSamplesFrame; + + noLayer[prog]++; + + if (highestSamplingRate < 0) + highestSamplingRate = hAss->config[prog][layer]->samplingRate; + + hsfSamplesFrame = hAss->config[prog][layer]->samplesPerFrame * + highestSamplingRate / + hAss->config[prog][layer]->samplingRate; + + if (hsfSamplesFrame <= minFrameSamples) + minFrameSamples = hsfSamplesFrame; + if (hsfSamplesFrame >= maxFrameSamples) + maxFrameSamples = hsfSamplesFrame; + + if (lastNoSamples == -1) { + lastNoSamples = hsfSamplesFrame; + } else { + if (hsfSamplesFrame != lastNoSamples) { + return 0; + } + } + } + } + } + + return 1; +} + +/** + * Initialize LATM/LOAS Stream and add layer 0 at program 0. + */ +static TRANSPORTENC_ERROR transportEnc_InitLatmStream( + HANDLE_LATM_STREAM hAss, int fractDelayPresent, + signed int + muxConfigPeriod, /* insert setup data every muxConfigPeriod frames */ + UINT audioMuxVersion, TRANSPORT_TYPE tt) { + TRANSPORTENC_ERROR ErrorStatus = TRANSPORTENC_OK; + + if (hAss == NULL) return TRANSPORTENC_INVALID_PARAMETER; + + hAss->tt = tt; + + hAss->noProgram = 1; + + hAss->audioMuxVersion = audioMuxVersion; + + /* Fill noLayer array using hAss->config */ + hAss->allStreamsSameTimeFraming = + allStreamsSameTimeFraming(hAss, hAss->noProgram, hAss->noLayer); + /* Only allStreamsSameTimeFraming==1 is supported */ + FDK_ASSERT(hAss->allStreamsSameTimeFraming); + + hAss->fractDelayPresent = fractDelayPresent; + hAss->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; + + /* sync layer related */ + hAss->audioMuxLengthBytes = 0; + + hAss->latmFrameCounter = 0; + hAss->muxConfigPeriod = muxConfigPeriod; + + return ErrorStatus; +} + +/** + * + */ +UINT transportEnc_LatmCountTotalBitDemandHeader(HANDLE_LATM_STREAM hAss, + unsigned int streamDataLength) { + UINT bitDemand = 0; + + switch (hAss->tt) { + case TT_MP4_LOAS: + case TT_MP4_LATM_MCP0: + case TT_MP4_LATM_MCP1: + if (hAss->subFrameCnt == 0) { + bitDemand = transportEnc_LatmCountFixBitDemandHeader(hAss); + } + bitDemand += transportEnc_LatmCountVarBitDemandHeader( + hAss, streamDataLength /*- bitDemand*/); + break; + default: + break; + } + + return bitDemand; +} + +static TRANSPORTENC_ERROR AdvanceAudioMuxElement(HANDLE_LATM_STREAM hAss, + HANDLE_FDK_BITSTREAM hBs, + int auBits, int bufferFullness, + CSTpCallBacks *cb) { + TRANSPORTENC_ERROR ErrorStatus = TRANSPORTENC_OK; + int insertMuxSetup; + + /* Insert setup data to assemble Buffer */ + if (hAss->subFrameCnt == 0) { + if (hAss->muxConfigPeriod > 0) { + insertMuxSetup = (hAss->latmFrameCounter == 0); + } else { + insertMuxSetup = 0; + } + + if (hAss->tt != TT_MP4_LATM_MCP0) { + if (insertMuxSetup) { + FDKwriteBits(hBs, 0, 1); /* useSameStreamMux useNewStreamMux */ + if (TRANSPORTENC_OK != (ErrorStatus = CreateStreamMuxConfig( + hAss, hBs, bufferFullness, cb))) { + return ErrorStatus; + } + } else { + FDKwriteBits(hBs, 1, 1); /* useSameStreamMux */ + } + } + } + + /* PayloadLengthInfo */ + { + int prog, layer; + + for (prog = 0; prog < hAss->noProgram; prog++) { + for (layer = 0; layer < hAss->noLayer[prog]; layer++) { + ErrorStatus = WriteAuPayloadLengthInfo(hBs, auBits); + if (ErrorStatus != TRANSPORTENC_OK) return ErrorStatus; + } + } + } + /* At this point comes the access unit. */ + + return TRANSPORTENC_OK; +} + +TRANSPORTENC_ERROR +transportEnc_LatmWrite(HANDLE_LATM_STREAM hAss, HANDLE_FDK_BITSTREAM hBs, + int auBits, int bufferFullness, CSTpCallBacks *cb) { + TRANSPORTENC_ERROR ErrorStatus; + + if (hAss->subFrameCnt == 0) { + /* Start new frame */ + FDKresetBitbuffer(hBs, BS_WRITER); + } + + hAss->latmSubframeStart = FDKgetValidBits(hBs); + + /* Insert syncword and syncword distance + - only if loas + - we must update the syncword distance (=audiomuxlengthbytes) later + */ + if (hAss->tt == TT_MP4_LOAS && hAss->subFrameCnt == 0) { + /* Start new LOAS frame */ + FDKwriteBits(hBs, 0x2B7, 11); + hAss->audioMuxLengthBytes = 0; + hAss->audioMuxLengthBytesPos = + FDKgetValidBits(hBs); /* store read pointer position */ + FDKwriteBits(hBs, hAss->audioMuxLengthBytes, 13); + } + + ErrorStatus = AdvanceAudioMuxElement(hAss, hBs, auBits, bufferFullness, cb); + + if (ErrorStatus != TRANSPORTENC_OK) return ErrorStatus; + + return ErrorStatus; +} + +void transportEnc_LatmAdjustSubframeBits(HANDLE_LATM_STREAM hAss, int *bits) { + /* Substract bits from possible previous subframe */ + *bits -= hAss->latmSubframeStart; + /* Add fill bits */ + if (hAss->subFrameCnt == 0) { + *bits += hAss->otherDataLenBits; + *bits += hAss->fillBits; + } +} + +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) { + /* Add LOAS frame length if required. */ + if (hAss->tt == TT_MP4_LOAS) { + FDK_BITSTREAM tmpBuf; + + /* Determine frame length info */ + hAss->audioMuxLengthBytes = + ((FDKgetValidBits(hBs) + hAss->otherDataLenBits + 7) >> 3) - + 3; /* 3=Syncword + length */ + + /* 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); + + hAss->subFrameCnt = 0; + + FDKsyncCache(hBs); + *pBytes = (FDKgetValidBits(hBs) + 7) >> 3; + + if (hAss->muxConfigPeriod > 0) { + hAss->latmFrameCounter++; + + if (hAss->latmFrameCounter >= hAss->muxConfigPeriod) { + hAss->latmFrameCounter = 0; + hAss->noSubframes = hAss->noSubframes_next; + } + } + } else { + /* No data this time */ + *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 ErrorStatus; + int fractDelayPresent = 0; + int prog, layer; + + int setupDataDistanceFrames = layerConfig->headerPeriod; + + FDK_ASSERT(setupDataDistanceFrames >= 0); + + for (prog = 0; prog < LATM_MAX_PROGRAMS; prog++) { + for (layer = 0; layer < LATM_MAX_LAYERS; layer++) { + hAss->config[prog][layer] = NULL; + hAss->m_linfo[prog][layer].streamID = -1; + } + } + + hAss->config[0][0] = layerConfig; + hAss->m_linfo[0][0].streamID = 0; + + ErrorStatus = transportEnc_InitLatmStream(hAss, fractDelayPresent, + setupDataDistanceFrames, + (audioMuxVersion) ? 1 : 0, tt); + if (ErrorStatus != TRANSPORTENC_OK) goto bail; + + ErrorStatus = + transportEnc_LatmSetNrOfSubframes(hAss, layerConfig->nSubFrames); + if (ErrorStatus != TRANSPORTENC_OK) goto bail; + + /* Get the size of the StreamMuxConfig somehow */ + 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/fdk-aac/libMpegTPEnc/src/tpenc_latm.h b/fdk-aac/libMpegTPEnc/src/tpenc_latm.h new file mode 100644 index 0000000..d650357 --- /dev/null +++ b/fdk-aac/libMpegTPEnc/src/tpenc_latm.h @@ -0,0 +1,274 @@ +/* ----------------------------------------------------------------------------- +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: + +*******************************************************************************/ + +#ifndef TPENC_LATM_H +#define TPENC_LATM_H + +#include "tpenc_lib.h" +#include "FDK_bitstream.h" + +#define DEFAULT_LATM_NR_OF_SUBFRAMES 1 +#define DEFAULT_LATM_SMC_REPEAT 8 + +#define MAX_AAC_LAYERS 9 + +#define LATM_MAX_PROGRAMS 1 +#define LATM_MAX_STREAM_ID 16 + +#define LATM_MAX_LAYERS 1 /*MAX_AAC_LAYERS*/ + +#define MAX_NR_OF_SUBFRAMES \ + 2 /* set this carefully to avoid buffer overflows \ + */ + +typedef enum { LATMVAR_SIMPLE_SEQUENCE } LATM_VAR_MODE; + +typedef struct { + signed int frameLengthType; + signed int frameLengthBits; + signed int varFrameLengthTable[4]; + signed int streamID; +} LATM_LAYER_INFO; + +typedef struct { + LATM_LAYER_INFO m_linfo[LATM_MAX_PROGRAMS][LATM_MAX_LAYERS]; + CODER_CONFIG *config[LATM_MAX_PROGRAMS][LATM_MAX_LAYERS]; + + LATM_VAR_MODE varMode; + TRANSPORT_TYPE tt; + + int audioMuxLengthBytes; + + int audioMuxLengthBytesPos; + int taraBufferFullness; /* state of the bit reservoir */ + int varStreamCnt; + + UCHAR + latmFrameCounter; /* Current frame number. Counts modulo muxConfigPeriod + */ + UCHAR muxConfigPeriod; /* Distance in frames between MuxConfig */ + + UCHAR + audioMuxVersion; /* AMV1 supports transmission of taraBufferFullness and + ASC lengths */ + UCHAR audioMuxVersionA; /* for future extensions */ + + UCHAR noProgram; + UCHAR noLayer[LATM_MAX_PROGRAMS]; + UCHAR fractDelayPresent; + + UCHAR allStreamsSameTimeFraming; + UCHAR subFrameCnt; /* Current Subframe frame */ + UCHAR noSubframes; /* Number of subframes */ + UINT latmSubframeStart; /* Position of current subframe start */ + UCHAR noSubframes_next; + + UCHAR 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. + * + * \param hLatmStreamInfo HANDLE_LATM_STREAM handle + * \param hBs Bitstream handle + * \param layerConfig a valid CODER_CONFIG struct containing the current audio + * configuration parameters + * \param audioMuxVersion the LATM audioMuxVersion to be used + * \param tt the specific TRANSPORT_TYPE to be used, either TT_MP4_LOAS, + * TT_MP4_LATM_MCP1 or TT_MP4_LATM_MCP0 LOAS + * \param cb callback information structure. + * + * \return an TRANSPORTENC_ERROR error code + */ +TRANSPORTENC_ERROR transportEnc_Latm_Init(HANDLE_LATM_STREAM hLatmStreamInfo, + HANDLE_FDK_BITSTREAM hBs, + CODER_CONFIG *layerConfig, + UINT audioMuxVersion, + TRANSPORT_TYPE tt, CSTpCallBacks *cb); + +/** + * \brief 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 + * + * \param hAss HANDLE_LATM_STREAM handle + * \param streamDataLength the length of the payload + * + * \return the number of bits required by the LATM/LOAS headers + */ +unsigned int transportEnc_LatmCountTotalBitDemandHeader( + HANDLE_LATM_STREAM hAss, unsigned int streamDataLength); + +/** + * \brief Write LATM/LOAS header into given bitstream handle + * + * \param hLatmStreamInfo HANDLE_LATM_STREAM handle + * \param hBitstream Bitstream handle + * \param auBits amount of current payload bits + * \param bufferFullness LATM buffer fullness value + * \param cb callback information structure. + * + * \return an TRANSPORTENC_ERROR error code + */ +TRANSPORTENC_ERROR +transportEnc_LatmWrite(HANDLE_LATM_STREAM hAss, HANDLE_FDK_BITSTREAM hBitstream, + int auBits, int bufferFullness, CSTpCallBacks *cb); + +/** + * \brief Adjust bit count relative to current subframe + * + * \param hAss HANDLE_LATM_STREAM handle + * \param pBits pointer to an int, where the current frame bit count is + * contained, and where the subframe relative bit count will be returned into + * + * \return void + */ +void transportEnc_LatmAdjustSubframeBits(HANDLE_LATM_STREAM hAss, int *pBits); + +/** + * \brief Request an LATM frame, which may, or may not be available + * + * \param hAss HANDLE_LATM_STREAM handle + * \param hBs Bitstream handle + * \param pBytes pointer to an int, where the current frame byte count stored + * into. A return value of zero means that currently no LATM/LOAS frame can be + * returned. The latter is expected in case of multiple subframes being + * used. + * + * \return an TRANSPORTENC_ERROR error code + */ +TRANSPORTENC_ERROR transportEnc_LatmGetFrame(HANDLE_LATM_STREAM hAss, + HANDLE_FDK_BITSTREAM hBs, + int *pBytes); + +/** + * \brief Write a StreamMuxConfig into the given bitstream handle + * + * \param hAss HANDLE_LATM_STREAM handle + * \param hBs Bitstream handle + * \param bufferFullness LATM buffer fullness value + * \param cb callback information structure. + * + * \return void + */ +TRANSPORTENC_ERROR +CreateStreamMuxConfig(HANDLE_LATM_STREAM hAss, HANDLE_FDK_BITSTREAM hBs, + int bufferFullness, CSTpCallBacks *cb); + +#endif /* TPENC_LATM_H */ diff --git a/fdk-aac/libMpegTPEnc/src/tpenc_lib.cpp b/fdk-aac/libMpegTPEnc/src/tpenc_lib.cpp new file mode 100644 index 0000000..316c6e0 --- /dev/null +++ b/fdk-aac/libMpegTPEnc/src/tpenc_lib.cpp @@ -0,0 +1,713 @@ +/* ----------------------------------------------------------------------------- +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): Manuel Jander + + Description: MPEG Transport encode + +*******************************************************************************/ + +#include "tpenc_lib.h" + +/* library info */ +#include "tp_version.h" + +#define MODULE_NAME "transportEnc" + +#include "tpenc_asc.h" + +#include "tpenc_adts.h" + +#include "tpenc_adif.h" + +#include "tpenc_dab.h" + +#include "tpenc_latm.h" + +typedef struct { + int curSubFrame; + int nSubFrames; + int prevBits; +} RAWPACKETS_INFO; + +struct TRANSPORTENC { + CODER_CONFIG config; + TRANSPORT_TYPE transportFmt; /*!< MPEG4 transport type. */ + + FDK_BITSTREAM bitStream; + UCHAR *bsBuffer; + INT bsBufferSize; + + INT pceFrameCounter; /*!< Indicates frame period when PCE must be written in + raw_data_block. -1 means not to write a PCE in + raw_dat_block. */ + union { + STRUCT_ADTS adts; + + ADIF_INFO adif; + + STRUCT_DAB dab; + + LATM_STREAM latm; + + RAWPACKETS_INFO raw; + + } writer; + + CSTpCallBacks callbacks; +}; + +typedef struct _TRANSPORTENC_STRUCT TRANSPORTENC_STRUCT; + +/* + * MEMORY Declaration + */ + +C_ALLOC_MEM(Ram_TransportEncoder, struct TRANSPORTENC, 1) + +TRANSPORTENC_ERROR transportEnc_Open(HANDLE_TRANSPORTENC *phTpEnc) { + HANDLE_TRANSPORTENC hTpEnc; + + if (phTpEnc == NULL) { + return TRANSPORTENC_INVALID_PARAMETER; + } + + hTpEnc = GetRam_TransportEncoder(0); + + if (hTpEnc == NULL) { + return TRANSPORTENC_NO_MEM; + } + + *phTpEnc = hTpEnc; + return TRANSPORTENC_OK; +} + +/** + * \brief Get frame period of PCE in raw_data_block. + * + * - Write PCE only if necessary. PCE can be part of the ASC if chConfig==0 + * whererfore no additonal PCE will be written in raw_data_block. + * - A matrixMixdown coefficient can only be written if chConfig is 5.0 or 5.1. + * - The PCE repetition rate in raw_data_block can be controlled via + * headerPeriod parameter. + * + * \param 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. + * + * \return PCE frame repetition rate. -1 means no PCE present in + * raw_data_block. + */ +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 (getChannelConfig(channelMode, channelConfigZero)) { + case 0: + switch (transportFmt) { + case TT_MP4_ADTS: + case TT_MP4_LATM_MCP0: + case TT_MP4_RAW: + pceFrameCounter = headerPeriod; + break; + case TT_MP4_ADIF: /* ADIF header comprises PCE */ + if ((channelMode == MODE_1_2_2) || (channelMode == MODE_1_2_2_1)) { + pceFrameCounter = headerPeriod; /* repeating pce only meaningful + for potential matrix mixdown */ + break; + } + FDK_FALLTHROUGH; + case TT_MP4_LOAS: /* PCE in ASC if chChonfig==0 */ + case TT_MP4_LATM_MCP1: /* PCE in ASC if chChonfig==0 */ + case TT_DABPLUS: + default: + pceFrameCounter = -1; /* no PCE in raw_data_block */ + } + break; + case 5: /* MODE_1_2_2 */ + case 6: /* MODE_1_2_2_1 */ + /* matrixMixdownCoefficient can only be written if 5.0 and 5.1 config + * present. */ + if (matrixMixdownA != 0) { + switch (transportFmt) { + case TT_MP4_ADIF: /* ADIF header comprises PCE */ + case TT_MP4_ADTS: + case TT_MP4_LOAS: /* no PCE in ASC because chConfig!=0 */ + case TT_MP4_LATM_MCP1: /* no PCE in ASC because chConfig!=0 */ + case TT_MP4_LATM_MCP0: + case TT_MP4_RAW: + pceFrameCounter = headerPeriod; + break; + case TT_DABPLUS: + default: + pceFrameCounter = -1; /* no PCE in raw_data_block */ + } /* switch transportFmt */ + } /* if matrixMixdownA!=0 */ + break; + default: + pceFrameCounter = -1; /* no PCE in raw_data_block */ + } /* switch getChannelConfig() */ + } /* if headerPeriod>0 */ + else { + pceFrameCounter = -1; /* no PCE in raw_data_block */ + } + + return pceFrameCounter; +} + +TRANSPORTENC_ERROR transportEnc_Init(HANDLE_TRANSPORTENC hTpEnc, + UCHAR *bsBuffer, INT bsBufferSize, + TRANSPORT_TYPE transportFmt, + CODER_CONFIG *cconfig, UINT flags) { + /* Copy configuration structure */ + FDKmemcpy(&hTpEnc->config, cconfig, sizeof(CODER_CONFIG)); + + /* Init transportEnc struct. */ + hTpEnc->transportFmt = transportFmt; + + hTpEnc->bsBuffer = bsBuffer; + hTpEnc->bsBufferSize = bsBufferSize; + + FDKinitBitStream(&hTpEnc->bitStream, hTpEnc->bsBuffer, hTpEnc->bsBufferSize, + 0, BS_WRITER); + + switch (transportFmt) { + case TT_MP4_ADIF: + /* Sanity checks */ + if ((hTpEnc->config.aot != AOT_AAC_LC) || + (hTpEnc->config.samplesPerFrame != 1024)) { + return TRANSPORTENC_INVALID_PARAMETER; + } + hTpEnc->writer.adif.headerWritten = 0; + hTpEnc->writer.adif.samplingRate = hTpEnc->config.samplingRate; + hTpEnc->writer.adif.bitRate = hTpEnc->config.bitRate; + hTpEnc->writer.adif.profile = ((int)hTpEnc->config.aot) - 1; + hTpEnc->writer.adif.cm = hTpEnc->config.channelMode; + hTpEnc->writer.adif.bVariableRate = 0; + hTpEnc->writer.adif.instanceTag = 0; + hTpEnc->writer.adif.matrixMixdownA = hTpEnc->config.matrixMixdownA; + hTpEnc->writer.adif.pseudoSurroundEnable = + (hTpEnc->config.flags & CC_PSEUDO_SURROUND) ? 1 : 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_DABPLUS: + /* Sanity checks */ + if ( ( hTpEnc->config.aot != AOT_AAC_LC) + ||(hTpEnc->config.samplesPerFrame != 960) ) + { + return TRANSPORTENC_INVALID_PARAMETER; + } + if ( dabWrite_Init(&hTpEnc->writer.dab, &hTpEnc->config) != 0) { + return TRANSPORTENC_INVALID_PARAMETER; + } + break; + + case TT_MP4_LOAS: + case TT_MP4_LATM_MCP0: + case TT_MP4_LATM_MCP1: { + TRANSPORTENC_ERROR error; + + error = transportEnc_Latm_Init(&hTpEnc->writer.latm, &hTpEnc->bitStream, + &hTpEnc->config, flags & TP_FLAG_LATM_AMV, + transportFmt, &hTpEnc->callbacks); + if (error != TRANSPORTENC_OK) { + return error; + } + } break; + + case TT_MP4_RAW: + hTpEnc->writer.raw.curSubFrame = 0; + hTpEnc->writer.raw.nSubFrames = hTpEnc->config.nSubFrames; + break; + + default: + return TRANSPORTENC_INVALID_PARAMETER; + } + + /* pceFrameCounter indicates if PCE must be written in raw_data_block. */ + hTpEnc->pceFrameCounter = getPceRepetitionRate( + hTpEnc->config.channelMode, hTpEnc->config.channelConfigZero, + transportFmt, hTpEnc->config.headerPeriod, hTpEnc->config.matrixMixdownA); + + return TRANSPORTENC_OK; +} + +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) { + if (hTpEnc == NULL) { + return -1; + } + hTpEnc->callbacks.cbSbr = cbSbr; + 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 err = TRANSPORTENC_OK; + + if (!hTp) { + return TRANSPORTENC_INVALID_PARAMETER; + } + HANDLE_FDK_BITSTREAM hBs = &hTp->bitStream; + + /* In case of writing PCE in raw_data_block frameUsedBits must be adapted. */ + if (hTp->pceFrameCounter >= hTp->config.headerPeriod) { + frameUsedBits += transportEnc_GetPCEBits( + hTp->config.channelMode, hTp->config.matrixMixdownA, + 3); /* Consider 3 bits ID signalling in alignment */ + } + + switch (hTp->transportFmt) { + case TT_MP4_ADIF: + FDKinitBitStream(&hTp->bitStream, hTp->bsBuffer, hTp->bsBufferSize, 0, + BS_WRITER); + 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 /= 32; + bufferFullness = FDKmin(0x7FF, bufferFullness); /* Signal variable rate */ + adtsWrite_EncodeHeader(&hTp->writer.adts, &hTp->bitStream, bufferFullness, + frameUsedBits); + break; + case TT_DABPLUS: + bufferFullness /= ncc; /* Number of Considered Channels */ + bufferFullness /= 32; + bufferFullness = FDKmin(0x7FF, bufferFullness); /* Signal variable rate */ + dabWrite_EncodeHeader( + &hTp->writer.dab, + &hTp->bitStream, + bufferFullness, + frameUsedBits + ); + break; + case TT_MP4_LOAS: + case TT_MP4_LATM_MCP0: + case TT_MP4_LATM_MCP1: + bufferFullness /= ncc; /* Number of Considered Channels */ + bufferFullness /= 32; + bufferFullness = FDKmin(0xFF, bufferFullness); /* Signal variable rate */ + transportEnc_LatmWrite(&hTp->writer.latm, hBs, frameUsedBits, + bufferFullness, &hTp->callbacks); + break; + case TT_MP4_RAW: + if (hTp->writer.raw.curSubFrame >= hTp->writer.raw.nSubFrames) { + hTp->writer.raw.curSubFrame = 0; + FDKinitBitStream(&hTp->bitStream, hTp->bsBuffer, hTp->bsBufferSize, 0, + BS_WRITER); + } + hTp->writer.raw.prevBits = FDKgetValidBits(hBs); + break; + default: + err = TRANSPORTENC_UNSUPPORTED_FORMAT; + break; + } + + /* Write PCE in raw_data_block if required */ + if (hTp->pceFrameCounter >= hTp->config.headerPeriod) { + INT crcIndex = 0; + /* Align inside PCE with repsect to the first bit of the raw_data_block() */ + UINT alignAnchor = FDKgetValidBits(&hTp->bitStream); + + /* Write PCE element ID bits */ + FDKwriteBits(&hTp->bitStream, ID_PCE, 3); + + if ((hTp->transportFmt == TT_MP4_ADTS) && + !hTp->writer.adts.protection_absent) { + crcIndex = adtsWrite_CrcStartReg(&hTp->writer.adts, &hTp->bitStream, 0); + } + + /* Write PCE as first raw_data_block element */ + transportEnc_writePCE( + &hTp->bitStream, hTp->config.channelMode, hTp->config.samplingRate, 0, + 1, hTp->config.matrixMixdownA, + (hTp->config.flags & CC_PSEUDO_SURROUND) ? 1 : 0, alignAnchor); + + if ((hTp->transportFmt == TT_MP4_ADTS) && + !hTp->writer.adts.protection_absent) { + adtsWrite_CrcEndReg(&hTp->writer.adts, &hTp->bitStream, crcIndex); + } + hTp->pceFrameCounter = 0; /* reset pce frame counter */ + } + + if (hTp->pceFrameCounter != -1) { + hTp->pceFrameCounter++; /* Update pceFrameCounter only if PCE writing is + active. */ + } + + return err; +} + +TRANSPORTENC_ERROR transportEnc_EndAccessUnit(HANDLE_TRANSPORTENC hTp, + int *bits) { + switch (hTp->transportFmt) { + case TT_MP4_LATM_MCP0: + case TT_MP4_LATM_MCP1: + case TT_MP4_LOAS: + transportEnc_LatmAdjustSubframeBits(&hTp->writer.latm, bits); + break; + case TT_MP4_ADTS: + adtsWrite_EndRawDataBlock(&hTp->writer.adts, &hTp->bitStream, bits); + break; + case TT_DABPLUS: + dabWrite_EndRawDataBlock(&hTp->writer.dab, &hTp->bitStream, bits); + break; + case TT_MP4_ADIF: + /* Substract ADIF header from AU bits, not to be considered. */ + *bits -= adifWrite_GetHeaderBits(&hTp->writer.adif); + hTp->writer.adif.headerWritten = 1; + break; + case TT_MP4_RAW: + *bits -= hTp->writer.raw.prevBits; + break; + default: + break; + } + + return TRANSPORTENC_OK; +} + +TRANSPORTENC_ERROR transportEnc_GetFrame(HANDLE_TRANSPORTENC hTpEnc, + int *nbytes) { + TRANSPORTENC_ERROR tpErr = TRANSPORTENC_OK; + HANDLE_FDK_BITSTREAM hBs = &hTpEnc->bitStream; + + switch (hTpEnc->transportFmt) { + case TT_MP4_LATM_MCP0: + case TT_MP4_LATM_MCP1: + case TT_MP4_LOAS: + *nbytes = hTpEnc->bsBufferSize; + 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; + hTpEnc->writer.adts.currentBlock = 0; + } else { + *nbytes = 0; + } + break; + case TT_DABPLUS: + if (hTpEnc->writer.dab.currentBlock >= hTpEnc->writer.dab.num_raw_blocks+1) { + *nbytes = (FDKgetValidBits(hBs) + 7)>>3; + hTpEnc->writer.dab.currentBlock = 0; + } else { + *nbytes = 0; + } + break; + case TT_MP4_ADIF: + FDK_ASSERT((INT)FDKgetValidBits(hBs) >= 0); + *nbytes = (FDKgetValidBits(hBs) + 7) >> 3; + break; + case TT_MP4_RAW: + FDKsyncCache(hBs); + hTpEnc->writer.raw.curSubFrame++; + *nbytes = ((FDKgetValidBits(hBs) - hTpEnc->writer.raw.prevBits) + 7) >> 3; + break; + default: + break; + } + + return tpErr; +} + +INT transportEnc_GetStaticBits(HANDLE_TRANSPORTENC hTp, int auBits) { + INT nbits = 0, nPceBits = 0; + + /* Write PCE within raw_data_block in transport lib. */ + if (hTp->pceFrameCounter >= hTp->config.headerPeriod) { + nPceBits = transportEnc_GetPCEBits( + hTp->config.channelMode, hTp->config.matrixMixdownA, + 3); /* Consider 3 bits ID signalling in alignment */ + auBits += nPceBits; /* Adapt required raw_data_block bit consumtpion for AU + length information e.g. in LATM/LOAS configuration. + */ + } + + switch (hTp->transportFmt) { + case TT_MP4_ADIF: + case TT_MP4_RAW: + nbits = 0; /* Do not consider the ADIF header into the total bitrate */ + break; + case TT_MP4_ADTS: + nbits = adtsWrite_GetHeaderBits(&hTp->writer.adts); + break; + case TT_DABPLUS: + nbits = dabWrite_CountTotalBitDemandHeader(&hTp->writer.dab, auBits); + break; + case TT_MP4_LOAS: + case TT_MP4_LATM_MCP0: + case TT_MP4_LATM_MCP1: + nbits = + transportEnc_LatmCountTotalBitDemandHeader(&hTp->writer.latm, auBits); + break; + default: + nbits = 0; + break; + } + + /* PCE is written in the transport library therefore the bit consumption is + * part of the transport static bits. */ + nbits += nPceBits; + + return nbits; +} + +void transportEnc_Close(HANDLE_TRANSPORTENC *phTp) { + if (phTp != NULL) { + if (*phTp != NULL) { + FreeRam_TransportEncoder(phTp); + } + } +} + +int transportEnc_CrcStartReg(HANDLE_TRANSPORTENC hTpEnc, int mBits) { + int crcReg = 0; + + switch (hTpEnc->transportFmt) { + case TT_MP4_ADTS: + crcReg = adtsWrite_CrcStartReg(&hTpEnc->writer.adts, &hTpEnc->bitStream, + mBits); + break; + case TT_DABPLUS: + crcReg = dabWrite_CrcStartReg(&hTpEnc->writer.dab, &hTpEnc->bitStream, mBits); + break; + default: + break; + } + + return crcReg; +} + +void transportEnc_CrcEndReg(HANDLE_TRANSPORTENC hTpEnc, int reg) { + switch (hTpEnc->transportFmt) { + case TT_MP4_ADTS: + adtsWrite_CrcEndReg(&hTpEnc->writer.adts, &hTpEnc->bitStream, reg); + break; + case TT_DABPLUS: + dabWrite_CrcEndReg(&hTpEnc->writer.dab, &hTpEnc->bitStream, reg); + break; + default: + break; + } +} + +TRANSPORTENC_ERROR transportEnc_GetConf(HANDLE_TRANSPORTENC hTpEnc, + CODER_CONFIG *cc, + FDK_BITSTREAM *dataBuffer, + UINT *confType) { + TRANSPORTENC_ERROR tpErr = TRANSPORTENC_OK; + HANDLE_LATM_STREAM hLatmConfig = &hTpEnc->writer.latm; + + *confType = 0; /* set confType variable to default */ + + /* write StreamMuxConfig or AudioSpecificConfig depending on format used */ + switch (hTpEnc->transportFmt) { + case TT_MP4_LATM_MCP0: + case TT_MP4_LATM_MCP1: + case TT_MP4_LOAS: + tpErr = + CreateStreamMuxConfig(hLatmConfig, dataBuffer, 0, &hTpEnc->callbacks); + *confType = 1; /* config is SMC */ + break; + default: + if (transportEnc_writeASC(dataBuffer, cc, &hTpEnc->callbacks) != 0) { + tpErr = TRANSPORTENC_UNKOWN_ERROR; + } + } + + return tpErr; +} + +TRANSPORTENC_ERROR transportEnc_GetLibInfo(LIB_INFO *info) { + int i; + + if (info == NULL) { + return TRANSPORTENC_INVALID_PARAMETER; + } + /* search for next free tab */ + for (i = 0; i < FDK_MODULE_LAST; i++) { + if (info[i].module_id == FDK_NONE) break; + } + if (i == FDK_MODULE_LAST) { + return TRANSPORTENC_UNKOWN_ERROR; + } + info += i; + + info->module_id = FDK_TPENC; + info->version = LIB_VERSION(TP_LIB_VL0, TP_LIB_VL1, TP_LIB_VL2); + LIB_VERSION_STRING(info); +#ifdef __ANDROID__ + info->build_date = ""; + info->build_time = ""; +#else + info->build_date = __DATE__; + info->build_time = __TIME__; +#endif + info->title = TP_LIB_TITLE; + + /* Set flags */ + info->flags = + 0 | CAPF_ADIF | CAPF_ADTS | CAPF_LATM | CAPF_LOAS | CAPF_RAWPACKETS | CAPF_DAB_AAC; + + return TRANSPORTENC_OK; +} |