From 6cfabd35363c3ef5e3b209b867169a500b3ccc3c Mon Sep 17 00:00:00 2001 From: Fraunhofer IIS FDK Date: Mon, 26 Feb 2018 20:17:00 +0100 Subject: Upgrade to FDKv2 Bug: 71430241 Test: CTS DecoderTest and DecoderTestAacDrc original-Change-Id: Iaa20f749b8a04d553b20247cfe1a8930ebbabe30 Apply clang-format also on header files. original-Change-Id: I14de1ef16bbc79ec0283e745f98356a10efeb2e4 Fixes for MPEG-D DRC original-Change-Id: If1de2d74bbbac84b3f67de3b88b83f6a23b8a15c Catch unsupported tw_mdct at an early stage original-Change-Id: Ied9dd00d754162a0e3ca1ae3e6b854315d818afe Fixing PVC transition frames original-Change-Id: Ib75725abe39252806c32d71176308f2c03547a4e Move qmf bands sanity check original-Change-Id: Iab540c3013c174d9490d2ae100a4576f51d8dbc4 Initialize scaling variable original-Change-Id: I3c4087101b70e998c71c1689b122b0d7762e0f9e Add 16 qmf band configuration to getSlotNrgHQ() original-Change-Id: I49a5d30f703a1b126ff163df9656db2540df21f1 Always apply byte alignment at the end of the AudioMuxElement original-Change-Id: I42d560287506d65d4c3de8bfe3eb9a4ebeb4efc7 Setup SBR element only if no parse error exists original-Change-Id: I1915b73704bc80ab882b9173d6bec59cbd073676 Additional array index check in HCR original-Change-Id: I18cc6e501ea683b5009f1bbee26de8ddd04d8267 Fix fade-in index selection in concealment module original-Change-Id: Ibf802ed6ed8c05e9257e1f3b6d0ac1162e9b81c1 Enable explicit backward compatible parser for AAC_LD original-Change-Id: I27e9c678dcb5d40ed760a6d1e06609563d02482d Skip spatial specific config in explicit backward compatible ASC original-Change-Id: Iff7cc365561319e886090cedf30533f562ea4d6e Update flags description in decoder API original-Change-Id: I9a5b4f8da76bb652f5580cbd3ba9760425c43830 Add QMF domain reset function original-Change-Id: I4f89a8a2c0277d18103380134e4ed86996e9d8d6 DRC upgrade v2.1.0 original-Change-Id: I5731c0540139dab220094cd978ef42099fc45b74 Fix integer overflow in sqrtFixp_lookup() original-Change-Id: I429a6f0d19aa2cc957e0f181066f0ca73968c914 Fix integer overflow in invSqrtNorm2() original-Change-Id: I84de5cbf9fb3adeb611db203fe492fabf4eb6155 Fix integer overflow in GenerateRandomVector() original-Change-Id: I3118a641008bd9484d479e5b0b1ee2b5d7d44d74 Fix integer overflow in adjustTimeSlot_EldGrid() original-Change-Id: I29d503c247c5c8282349b79df940416a512fb9d5 Fix integer overflow in FDKsbrEnc_codeEnvelope() original-Change-Id: I6b34b61ebb9d525b0c651ed08de2befc1f801449 Follow-up on: Fix integer overflow in adjustTimeSlot_EldGrid() original-Change-Id: I6f8f578cc7089e5eb7c7b93e580b72ca35ad689a Fix integer overflow in get_pk_v2() original-Change-Id: I63375bed40d45867f6eeaa72b20b1f33e815938c Fix integer overflow in Syn_filt_zero() original-Change-Id: Ie0c02fdfbe03988f9d3b20d10cd9fe4c002d1279 Fix integer overflow in CFac_CalcFacSignal() original-Change-Id: Id2d767c40066c591b51768e978eb8af3b803f0c5 Fix integer overflow in FDKaacEnc_FDKaacEnc_calcPeNoAH() original-Change-Id: Idcbd0f4a51ae2550ed106aa6f3d678d1f9724841 Fix integer overflow in sbrDecoder_calculateGainVec() original-Change-Id: I7081bcbe29c5cede9821b38d93de07c7add2d507 Fix integer overflow in CLpc_SynthesisLattice() original-Change-Id: I4a95ddc18de150102352d4a1845f06094764c881 Fix integer overflow in Pred_Lt4() original-Change-Id: I4dbd012b2de7d07c3e70a47b92e3bfae8dbc750a Fix integer overflow in FDKsbrEnc_InitSbrFastTransientDetector() original-Change-Id: I788cbec1a4a00f44c2f3a72ad7a4afa219807d04 Fix unsigned integer overflow in FDKaacEnc_WriteBitstream() original-Change-Id: I68fc75166e7d2cd5cd45b18dbe3d8c2a92f1822a Fix unsigned integer overflow in FDK_MetadataEnc_Init() original-Change-Id: Ie8d025f9bcdb2442c704bd196e61065c03c10af4 Fix overflow in pseudo random number generators original-Change-Id: I3e2551ee01356297ca14e3788436ede80bd5513c Fix unsigned integer overflow in sbrDecoder_Parse() original-Change-Id: I3f231b2f437e9c37db4d5b964164686710eee971 Fix unsigned integer overflow in longsub() original-Change-Id: I73c2bc50415cac26f1f5a29e125bbe75f9180a6e Fix unsigned integer overflow in CAacDecoder_DecodeFrame() original-Change-Id: Ifce2db4b1454b46fa5f887e9d383f1cc43b291e4 Fix overflow at CLpdChannelStream_Read() original-Change-Id: Idb9d822ce3a4272e4794b643644f5434e2d4bf3f Fix unsigned integer overflow in Hcr_State_BODY_SIGN_ESC__ESC_WORD() original-Change-Id: I1ccf77c0015684b85534c5eb97162740a870b71c Fix unsigned integer overflow in UsacConfig_Parse() original-Change-Id: Ie6d27f84b6ae7eef092ecbff4447941c77864d9f Fix unsigned integer overflow in aacDecoder_drcParse() original-Change-Id: I713f28e883eea3d70b6fa56a7b8f8c22bcf66ca0 Fix unsigned integer overflow in aacDecoder_drcReadCompression() original-Change-Id: Ia34dfeb88c4705c558bce34314f584965cafcf7a Fix unsigned integer overflow in CDataStreamElement_Read() original-Change-Id: Iae896cc1d11f0a893d21be6aa90bd3e60a2c25f0 Fix unsigned integer overflow in transportDec_AdjustEndOfAccessUnit() original-Change-Id: I64cf29a153ee784bb4a16fdc088baabebc0007dc Fix unsigned integer overflow in transportDec_GetAuBitsRemaining() original-Change-Id: I975b3420faa9c16a041874ba0db82e92035962e4 Fix unsigned integer overflow in extractExtendedData() original-Change-Id: I2a59eb09e2053cfb58dfb75fcecfad6b85a80a8f Fix signed integer overflow in CAacDecoder_ExtPayloadParse() original-Change-Id: I4ad5ca4e3b83b5d964f1c2f8c5e7b17c477c7929 Fix unsigned integer overflow in CAacDecoder_DecodeFrame() original-Change-Id: I29a39df77d45c52a0c9c5c83c1ba81f8d0f25090 Follow-up on: Fix integer overflow in CLpc_SynthesisLattice() original-Change-Id: I8fb194ffc073a3432a380845be71036a272d388f Fix signed integer overflow in _interpolateDrcGain() original-Change-Id: I879ec9ab14005069a7c47faf80e8bc6e03d22e60 Fix unsigned integer overflow in FDKreadBits() original-Change-Id: I1f47a6a8037ff70375aa8844947d5681bb4287ad Fix unsigned integer overflow in FDKbyteAlign() original-Change-Id: Id5f3a11a0c9e50fc6f76ed6c572dbd4e9f2af766 Fix unsigned integer overflow in FDK_get32() original-Change-Id: I9d33b8e97e3d38cbb80629cb859266ca0acdce96 Fix unsigned integer overflow in FDK_pushBack() original-Change-Id: Ic87f899bc8c6acf7a377a8ca7f3ba74c3a1e1c19 Fix unsigned integer overflow in FDK_pushForward() original-Change-Id: I3b754382f6776a34be1602e66694ede8e0b8effc Fix unsigned integer overflow in ReadPsData() original-Change-Id: I25361664ba8139e32bbbef2ca8c106a606ce9c37 Fix signed integer overflow in E_UTIL_residu() original-Change-Id: I8c3abd1f437ee869caa8fb5903ce7d3d641b6aad REVERT: Follow-up on: Integer overflow in CLpc_SynthesisLattice(). original-Change-Id: I3d340099acb0414795c8dfbe6362bc0a8f045f9b Follow-up on: Fix integer overflow in CLpc_SynthesisLattice() original-Change-Id: I4aedb8b3a187064e9f4d985175aa55bb99cc7590 Follow-up on: Fix unsigned integer overflow in aacDecoder_drcParse() original-Change-Id: I2aa2e13916213bf52a67e8b0518e7bf7e57fb37d Fix integer overflow in acelp original-Change-Id: Ie6390c136d84055f8b728aefbe4ebef6e029dc77 Fix unsigned integer overflow in aacDecoder_UpdateBitStreamCounters() original-Change-Id: I391ffd97ddb0b2c184cba76139bfb356a3b4d2e2 Adjust concealment default settings original-Change-Id: I6a95db935a327c47df348030bcceafcb29f54b21 Saturate estimatedStartPos original-Change-Id: I27be2085e0ae83ec9501409f65e003f6bcba1ab6 Negative shift exponent in _interpolateDrcGain() original-Change-Id: I18edb26b26d002aafd5e633d4914960f7a359c29 Negative shift exponent in calculateICC() original-Change-Id: I3dcd2ae98d2eb70ee0d59750863cbb2a6f4f8aba Too large shift exponent in FDK_put() original-Change-Id: Ib7d9aaa434d2d8de4a13b720ca0464b31ca9b671 Too large shift exponent in CalcInvLdData() original-Change-Id: I43e6e78d4cd12daeb1dcd5d82d1798bdc2550262 Member access within null pointer of type SBR_CHANNEL original-Change-Id: Idc5e4ea8997810376d2f36bbdf628923b135b097 Member access within null pointer of type CpePersistentData original-Change-Id: Ib6c91cb0d37882768e5baf63324e429589de0d9d Member access within null pointer FDKaacEnc_psyMain() original-Change-Id: I7729b7f4479970531d9dc823abff63ca52e01997 Member access within null pointer FDKaacEnc_GetPnsParam() original-Change-Id: I9aa3b9f3456ae2e0f7483dbd5b3dde95fc62da39 Member access within null pointer FDKsbrEnc_EnvEncodeFrame() original-Change-Id: I67936f90ea714e90b3e81bc0dd1472cc713eb23a Add HCR sanity check original-Change-Id: I6c1d9732ebcf6af12f50b7641400752f74be39f7 Fix memory issue for HBE edge case with 8:3 SBR original-Change-Id: I11ea58a61e69fbe8bf75034b640baee3011e63e9 Additional SBR parametrization sanity check for ELD original-Change-Id: Ie26026fbfe174c2c7b3691f6218b5ce63e322140 Add MPEG-D DRC channel layout check original-Change-Id: Iea70a74f171b227cce636a9eac4ba662777a2f72 Additional out-of-bounds checks in MPEG-D DRC original-Change-Id: Ife4a8c3452c6fde8a0a09e941154a39a769777d4 Change-Id: Ic63cb2f628720f54fe9b572b0cb528e2599c624e --- libAACenc/src/aacenc_tns.cpp | 1654 +++++++++++++++++++----------------------- 1 file changed, 752 insertions(+), 902 deletions(-) (limited to 'libAACenc/src/aacenc_tns.cpp') diff --git a/libAACenc/src/aacenc_tns.cpp b/libAACenc/src/aacenc_tns.cpp index 9a07e8f..4462600 100644 --- a/libAACenc/src/aacenc_tns.cpp +++ b/libAACenc/src/aacenc_tns.cpp @@ -1,74 +1,85 @@ - -/* ----------------------------------------------------------------------------------------------------------- +/* ----------------------------------------------------------------------------- Software License for The Fraunhofer FDK AAC Codec Library for Android -© Copyright 1995 - 2015 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. - All rights reserved. +© Copyright 1995 - 2018 Fraunhofer-Gesellschaft zur Förderung der angewandten +Forschung e.V. All rights reserved. 1. INTRODUCTION -The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements -the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio. -This FDK AAC Codec software is intended to be used on a wide variety of Android devices. - -AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual -audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by -independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part -of the MPEG specifications. - -Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer) -may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners -individually for the purpose of encoding or decoding bit streams in products that are compliant with -the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license -these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec -software may already be covered under those patent licenses when it is used for those licensed purposes only. - -Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality, -are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional -applications information and documentation. +The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software +that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding +scheme for digital audio. This FDK AAC Codec software is intended to be used on +a wide variety of Android devices. + +AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient +general perceptual audio codecs. AAC-ELD is considered the best-performing +full-bandwidth communications codec by independent studies and is widely +deployed. AAC has been standardized by ISO and IEC as part of the MPEG +specifications. + +Patent licenses for necessary patent claims for the FDK AAC Codec (including +those of Fraunhofer) may be obtained through Via Licensing +(www.vialicensing.com) or through the respective patent owners individually for +the purpose of encoding or decoding bit streams in products that are compliant +with the ISO/IEC MPEG audio standards. Please note that most manufacturers of +Android devices already license these patent claims through Via Licensing or +directly from the patent owners, and therefore FDK AAC Codec software may +already be covered under those patent licenses when it is used for those +licensed purposes only. + +Commercially-licensed AAC software libraries, including floating-point versions +with enhanced sound quality, are also available from Fraunhofer. Users are +encouraged to check the Fraunhofer website for additional applications +information and documentation. 2. COPYRIGHT LICENSE -Redistribution and use in source and binary forms, with or without modification, are permitted without -payment of copyright license fees provided that you satisfy the following conditions: +Redistribution and use in source and binary forms, with or without modification, +are permitted without payment of copyright license fees provided that you +satisfy the following conditions: -You must retain the complete text of this software license in redistributions of the FDK AAC Codec or -your modifications thereto in source code form. +You must retain the complete text of this software license in redistributions of +the FDK AAC Codec or your modifications thereto in source code form. -You must retain the complete text of this software license in the documentation and/or other materials -provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form. -You must make available free of charge copies of the complete source code of the FDK AAC Codec and your +You must retain the complete text of this software license in the documentation +and/or other materials provided with redistributions of the FDK AAC Codec or +your modifications thereto in binary form. You must make available free of +charge copies of the complete source code of the FDK AAC Codec and your modifications thereto to recipients of copies in binary form. -The name of Fraunhofer may not be used to endorse or promote products derived from this library without -prior written permission. +The name of Fraunhofer may not be used to endorse or promote products derived +from this library without prior written permission. -You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec -software or your modifications thereto. +You may not charge copyright license fees for anyone to use, copy or distribute +the FDK AAC Codec software or your modifications thereto. -Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software -and the date of any change. For modified versions of the FDK AAC Codec, the term -"Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term -"Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android." +Your modified versions of the FDK AAC Codec must carry prominent notices stating +that you changed the software and the date of any change. For modified versions +of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android" +must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK +AAC Codec Library for Android." 3. NO PATENT LICENSE -NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer, -ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with -respect to this software. +NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without +limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE. +Fraunhofer provides no warranty of patent non-infringement with respect to this +software. -You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized -by appropriate patent licenses. +You may use this FDK AAC Codec software or modifications thereto only for +purposes that are authorized by appropriate patent licenses. 4. DISCLAIMER -This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors -"AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties -of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR -CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages, -including but not limited to procurement of substitute goods or services; loss of use, data, or profits, -or business interruption, however caused and on any theory of liability, whether in contract, strict -liability, or tort (including negligence), arising in any way out of the use of this software, even if -advised of the possibility of such damage. +This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright +holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, +including but not limited to the implied warranties of merchantability and +fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, +or consequential damages, including but not limited to procurement of substitute +goods or services; loss of use, data, or profits, or business interruption, +however caused and on any theory of liability, whether in contract, strict +liability, or tort (including negligence), arising in any way out of the use of +this software, even if advised of the possibility of such damage. 5. CONTACT INFORMATION @@ -79,14 +90,15 @@ Am Wolfsmantel 33 www.iis.fraunhofer.de/amm amm-info@iis.fraunhofer.de ------------------------------------------------------------------------------------------------------------ */ +----------------------------------------------------------------------------- */ + +/**************************** AAC encoder library ****************************** -/******************************** MPEG Audio Encoder ************************** + Author(s): Alex Groeschel, Tobias Chalupka - Initial author: Alex Groeschel, Tobias Chalupka - contents/description: Temporal noise shaping + Description: Temporal noise shaping -******************************************************************************/ +*******************************************************************************/ #include "aacenc_tns.h" #include "psy_const.h" @@ -94,192 +106,213 @@ amm-info@iis.fraunhofer.de #include "tns_func.h" #include "aacEnc_rom.h" #include "aacenc_tns.h" +#include "FDK_lpc.h" #define FILTER_DIRECTION 0 /* 0 = up, 1 = down */ -static const FIXP_DBL acfWindowLong[12+3+1] = { - 0x7fffffff,0x7fb80000,0x7ee00000,0x7d780000,0x7b800000,0x78f80000,0x75e00000,0x72380000, - 0x6e000000,0x69380000,0x63e00000,0x5df80000,0x57800000,0x50780000,0x48e00000,0x40b80000 -}; +static const FIXP_DBL acfWindowLong[12 + 3 + 1] = { + 0x7fffffff, 0x7fb80000, 0x7ee00000, 0x7d780000, 0x7b800000, 0x78f80000, + 0x75e00000, 0x72380000, 0x6e000000, 0x69380000, 0x63e00000, 0x5df80000, + 0x57800000, 0x50780000, 0x48e00000, 0x40b80000}; -static const FIXP_DBL acfWindowShort[4+3+1] = { - 0x7fffffff,0x7e000000,0x78000000,0x6e000000,0x60000000,0x4e000000,0x38000000,0x1e000000 -}; +static const FIXP_DBL acfWindowShort[4 + 3 + 1] = { + 0x7fffffff, 0x7e000000, 0x78000000, 0x6e000000, + 0x60000000, 0x4e000000, 0x38000000, 0x1e000000}; -typedef struct{ - INT bitRateFrom[2]; /* noneSbr=0, useSbr=1 */ - INT bitRateTo[2]; /* noneSbr=0, useSbr=1 */ - TNS_PARAMETER_TABULATED paramTab[2]; /* mono=0, stereo=1 */ +typedef struct { + INT bitRateFrom[2]; /* noneSbr=0, useSbr=1 */ + INT bitRateTo[2]; /* noneSbr=0, useSbr=1 */ + TNS_PARAMETER_TABULATED paramTab[2]; /* mono=0, stereo=1 */ } TNS_INFO_TAB; -#define TNS_TIMERES_SCALE (1) -#define FL2_TIMERES_FIX(a) ( FL2FXCONST_DBL(a/(float)(1<= tnsInfoTab[i].bitRateFrom[sbrLd?1:0]) && - bitRate <= tnsInfoTab[i].bitRateTo[sbrLd?1:0]) - { - tnsConfigTab = &tnsInfoTab[i].paramTab[(channels==1)?0:1]; + for (i = 0; i < (int)(sizeof(tnsInfoTab) / sizeof(TNS_INFO_TAB)); i++) { + if ((bitRate >= tnsInfoTab[i].bitRateFrom[sbrLd ? 1 : 0]) && + bitRate <= tnsInfoTab[i].bitRateTo[sbrLd ? 1 : 0]) { + tnsConfigTab = &tnsInfoTab[i].paramTab[(channels == 1) ? 0 : 1]; } } return tnsConfigTab; } - -static INT getTnsMaxBands( - const INT sampleRate, - const INT granuleLength, - const INT isShortBlock - ) -{ +static INT getTnsMaxBands(const INT sampleRate, const INT granuleLength, + const INT isShortBlock) { int i; INT numBands = -1; const TNS_MAX_TAB_ENTRY *pMaxBandsTab = NULL; int maxBandsTabSize = 0; switch (granuleLength) { + case 960: case 1024: pMaxBandsTab = tnsMaxBandsTab1024; - maxBandsTabSize = sizeof(tnsMaxBandsTab1024)/sizeof(TNS_MAX_TAB_ENTRY); + maxBandsTabSize = sizeof(tnsMaxBandsTab1024) / sizeof(TNS_MAX_TAB_ENTRY); + break; + case 120: + pMaxBandsTab = tnsMaxBandsTab120; + maxBandsTabSize = sizeof(tnsMaxBandsTab120) / sizeof(TNS_MAX_TAB_ENTRY); + break; + case 128: + pMaxBandsTab = tnsMaxBandsTab128; + maxBandsTabSize = sizeof(tnsMaxBandsTab128) / sizeof(TNS_MAX_TAB_ENTRY); + break; + case 240: + pMaxBandsTab = tnsMaxBandsTab240; + maxBandsTabSize = sizeof(tnsMaxBandsTab240) / sizeof(TNS_MAX_TAB_ENTRY); + break; + case 256: + pMaxBandsTab = tnsMaxBandsTab256; + maxBandsTabSize = sizeof(tnsMaxBandsTab256) / sizeof(TNS_MAX_TAB_ENTRY); break; case 480: pMaxBandsTab = tnsMaxBandsTab480; - maxBandsTabSize = sizeof(tnsMaxBandsTab480)/sizeof(TNS_MAX_TAB_ENTRY); + maxBandsTabSize = sizeof(tnsMaxBandsTab480) / sizeof(TNS_MAX_TAB_ENTRY); break; case 512: pMaxBandsTab = tnsMaxBandsTab512; - maxBandsTabSize = sizeof(tnsMaxBandsTab512)/sizeof(TNS_MAX_TAB_ENTRY); + maxBandsTabSize = sizeof(tnsMaxBandsTab512) / sizeof(TNS_MAX_TAB_ENTRY); break; default: numBands = -1; } - if (pMaxBandsTab!=NULL) { - for (i=0; i= pMaxBandsTab[i].samplingRate) { break; } @@ -291,7 +324,7 @@ static INT getTnsMaxBands( /***************************************************************************/ /*! - \brief FDKaacEnc_FreqToBandWithRounding + \brief FDKaacEnc_FreqToBandWidthRounding Returns index of nearest band border @@ -303,38 +336,31 @@ static INT getTnsMaxBands( \return band border ****************************************************************************/ -INT FDKaacEnc_FreqToBandWithRounding( - const INT freq, - const INT fs, - const INT numOfBands, - const INT *bandStartOffset - ) -{ +INT FDKaacEnc_FreqToBandWidthRounding(const INT freq, const INT fs, + const INT numOfBands, + const INT *bandStartOffset) { INT lineNumber, band; /* assert(freq >= 0); */ - lineNumber = (freq*bandStartOffset[numOfBands]*4/fs+1)/2; + lineNumber = (freq * bandStartOffset[numOfBands] * 4 / fs + 1) / 2; /* freq > fs/2 */ - if (lineNumber >= bandStartOffset[numOfBands]) - return numOfBands; + if (lineNumber >= bandStartOffset[numOfBands]) return numOfBands; /* find band the line number lies in */ - for (band=0; bandlineNumber) break; + for (band = 0; band < numOfBands; band++) { + if (bandStartOffset[band + 1] > lineNumber) break; } /* round to nearest band border */ if (lineNumber - bandStartOffset[band] > - bandStartOffset[band+1] - lineNumber ) - { - band++; - } + bandStartOffset[band + 1] - lineNumber) { + band++; + } - return(band); + return (band); } - /***************************************************************************** functionname: FDKaacEnc_InitTnsConfiguration @@ -348,113 +374,132 @@ INT FDKaacEnc_FreqToBandWithRounding( output: *****************************************************************************/ -AAC_ENCODER_ERROR FDKaacEnc_InitTnsConfiguration(INT bitRate, - INT sampleRate, - INT channels, - INT blockType, - INT granuleLength, - INT isLowDelay, - INT ldSbrPresent, - TNS_CONFIG *tC, - PSY_CONFIGURATION *pC, - INT active, - INT useTnsPeak) -{ +AAC_ENCODER_ERROR FDKaacEnc_InitTnsConfiguration( + INT bitRate, INT sampleRate, INT channels, INT blockType, INT granuleLength, + INT isLowDelay, INT ldSbrPresent, TNS_CONFIG *tC, PSY_CONFIGURATION *pC, + INT active, INT useTnsPeak) { int i; - //float acfTimeRes = (blockType == SHORT_WINDOW) ? 0.125f : 0.046875f; + // float acfTimeRes = (blockType == SHORT_WINDOW) ? 0.125f : 0.046875f; - if (channels <= 0) - return (AAC_ENCODER_ERROR)1; + if (channels <= 0) return (AAC_ENCODER_ERROR)1; tC->isLowDelay = isLowDelay; - /* initialize TNS filter flag, order, and coefficient resolution (in bits per coeff) */ - tC->tnsActive = (active) ? TRUE : FALSE; - tC->maxOrder = (blockType == SHORT_WINDOW) ? 5 : 12; /* maximum: 7, 20 */ - if (bitRate < 16000) - tC->maxOrder -= 2; - tC->coefRes = (blockType == SHORT_WINDOW) ? 3 : 4; + /* initialize TNS filter flag, order, and coefficient resolution (in bits per + * coeff) */ + tC->tnsActive = (active) ? TRUE : FALSE; + tC->maxOrder = (blockType == SHORT_WINDOW) ? 5 : 12; /* maximum: 7, 20 */ + if (bitRate < 16000) tC->maxOrder -= 2; + tC->coefRes = (blockType == SHORT_WINDOW) ? 3 : 4; - /* LPC stop line: highest MDCT line to be coded, but do not go beyond TNS_MAX_BANDS! */ - tC->lpcStopBand = getTnsMaxBands(sampleRate, granuleLength, (blockType == SHORT_WINDOW) ? 1 : 0); + /* LPC stop line: highest MDCT line to be coded, but do not go beyond + * TNS_MAX_BANDS! */ + tC->lpcStopBand = getTnsMaxBands(sampleRate, granuleLength, + (blockType == SHORT_WINDOW) ? 1 : 0); if (tC->lpcStopBand < 0) { return (AAC_ENCODER_ERROR)1; } - tC->lpcStopBand = FDKmin(tC->lpcStopBand, pC->sfbActive); - tC->lpcStopLine = pC->sfbOffset[tC->lpcStopBand]; + tC->lpcStopBand = fMin(tC->lpcStopBand, pC->sfbActive); + tC->lpcStopLine = pC->sfbOffset[tC->lpcStopBand]; switch (granuleLength) { + case 960: case 1024: - /* TNS start line: skip lower MDCT lines to prevent artifacts due to filter mismatch */ - tC->lpcStartBand[LOFILT] = (blockType == SHORT_WINDOW) ? 0 : ((sampleRate < 18783) ? 4 : 8); - tC->lpcStartLine[LOFILT] = pC->sfbOffset[tC->lpcStartBand[LOFILT]]; + /* TNS start line: skip lower MDCT lines to prevent artifacts due to + * filter mismatch */ + if (blockType == SHORT_WINDOW) { + tC->lpcStartBand[LOFILT] = 0; + } else { + tC->lpcStartBand[LOFILT] = + (sampleRate < 9391) ? 2 : ((sampleRate < 18783) ? 4 : 8); + } + tC->lpcStartLine[LOFILT] = pC->sfbOffset[tC->lpcStartBand[LOFILT]]; i = tC->lpcStopBand; - while (pC->sfbOffset[i] > (tC->lpcStartLine[LOFILT] + (tC->lpcStopLine - tC->lpcStartLine[LOFILT]) / 4)) i--; - tC->lpcStartBand[HIFILT] = i; - tC->lpcStartLine[HIFILT] = pC->sfbOffset[i]; + while (pC->sfbOffset[i] > + (tC->lpcStartLine[LOFILT] + + (tC->lpcStopLine - tC->lpcStartLine[LOFILT]) / 4)) + i--; + tC->lpcStartBand[HIFILT] = i; + tC->lpcStartLine[HIFILT] = pC->sfbOffset[i]; tC->confTab.threshOn[HIFILT] = 1437; tC->confTab.threshOn[LOFILT] = 1500; tC->confTab.tnsLimitOrder[HIFILT] = tC->maxOrder; - tC->confTab.tnsLimitOrder[LOFILT] = tC->maxOrder - 7; + tC->confTab.tnsLimitOrder[LOFILT] = fMax(0, tC->maxOrder - 7); tC->confTab.tnsFilterDirection[HIFILT] = FILTER_DIRECTION; tC->confTab.tnsFilterDirection[LOFILT] = FILTER_DIRECTION; - tC->confTab.acfSplit[HIFILT] = -1; /* signal Merged4to2QuartersAutoCorrelation in FDKaacEnc_MergedAutoCorrelation*/ - tC->confTab.acfSplit[LOFILT] = -1; /* signal Merged4to2QuartersAutoCorrelation in FDKaacEnc_MergedAutoCorrelation */ + tC->confTab.acfSplit[HIFILT] = + -1; /* signal Merged4to2QuartersAutoCorrelation in + FDKaacEnc_MergedAutoCorrelation*/ + tC->confTab.acfSplit[LOFILT] = + -1; /* signal Merged4to2QuartersAutoCorrelation in + FDKaacEnc_MergedAutoCorrelation */ tC->confTab.filterEnabled[HIFILT] = 1; tC->confTab.filterEnabled[LOFILT] = 1; tC->confTab.seperateFiltersAllowed = 1; - /* compute autocorrelation window based on maximum filter order for given block type */ + /* compute autocorrelation window based on maximum filter order for given + * block type */ /* for (i = 0; i <= tC->maxOrder + 3; i++) { float acfWinTemp = acfTimeRes * i; acfWindow[i] = FL2FXCONST_DBL(1.0f - acfWinTemp * acfWinTemp); } */ if (blockType == SHORT_WINDOW) { - FDKmemcpy(tC->acfWindow[HIFILT], acfWindowShort, FDKmin(sizeof(acfWindowShort), sizeof(tC->acfWindow[HIFILT]))); - FDKmemcpy(tC->acfWindow[LOFILT], acfWindowShort, FDKmin(sizeof(acfWindowShort), sizeof(tC->acfWindow[HIFILT]))); - } - else { - FDKmemcpy(tC->acfWindow[HIFILT], acfWindowLong, FDKmin(sizeof(acfWindowLong), sizeof(tC->acfWindow[HIFILT]))); - FDKmemcpy(tC->acfWindow[LOFILT], acfWindowLong, FDKmin(sizeof(acfWindowLong), sizeof(tC->acfWindow[HIFILT]))); + FDKmemcpy(tC->acfWindow[HIFILT], acfWindowShort, + fMin((LONG)sizeof(acfWindowShort), + (LONG)sizeof(tC->acfWindow[HIFILT]))); + FDKmemcpy(tC->acfWindow[LOFILT], acfWindowShort, + fMin((LONG)sizeof(acfWindowShort), + (LONG)sizeof(tC->acfWindow[HIFILT]))); + } else { + FDKmemcpy(tC->acfWindow[HIFILT], acfWindowLong, + fMin((LONG)sizeof(acfWindowLong), + (LONG)sizeof(tC->acfWindow[HIFILT]))); + FDKmemcpy(tC->acfWindow[LOFILT], acfWindowLong, + fMin((LONG)sizeof(acfWindowLong), + (LONG)sizeof(tC->acfWindow[HIFILT]))); } break; case 480: - case 512: - { - const TNS_PARAMETER_TABULATED* pCfg = FDKaacEnc_GetTnsParam(bitRate, channels, ldSbrPresent); - - if ( pCfg != NULL ) { - - FDKmemcpy(&(tC->confTab), pCfg, sizeof(tC->confTab)); - - tC->lpcStartBand[HIFILT] = FDKaacEnc_FreqToBandWithRounding(pCfg->filterStartFreq[HIFILT], sampleRate, pC->sfbCnt, pC->sfbOffset); - tC->lpcStartLine[HIFILT] = pC->sfbOffset[tC->lpcStartBand[HIFILT]]; - tC->lpcStartBand[LOFILT] = FDKaacEnc_FreqToBandWithRounding(pCfg->filterStartFreq[LOFILT], sampleRate, pC->sfbCnt, pC->sfbOffset); - tC->lpcStartLine[LOFILT] = pC->sfbOffset[tC->lpcStartBand[LOFILT]]; - - FDKaacEnc_CalcGaussWindow(tC->acfWindow[HIFILT], tC->maxOrder+1, sampleRate, granuleLength, pCfg->tnsTimeResolution[HIFILT], TNS_TIMERES_SCALE); - FDKaacEnc_CalcGaussWindow(tC->acfWindow[LOFILT], tC->maxOrder+1, sampleRate, granuleLength, pCfg->tnsTimeResolution[LOFILT], TNS_TIMERES_SCALE); - } - else { - tC->tnsActive = FALSE; /* no configuration available, disable tns tool */ - } + case 512: { + const TNS_PARAMETER_TABULATED *pCfg = + FDKaacEnc_GetTnsParam(bitRate, channels, ldSbrPresent); + if (pCfg != NULL) { + FDKmemcpy(&(tC->confTab), pCfg, sizeof(tC->confTab)); + + tC->lpcStartBand[HIFILT] = FDKaacEnc_FreqToBandWidthRounding( + pCfg->filterStartFreq[HIFILT], sampleRate, pC->sfbCnt, + pC->sfbOffset); + tC->lpcStartLine[HIFILT] = pC->sfbOffset[tC->lpcStartBand[HIFILT]]; + tC->lpcStartBand[LOFILT] = FDKaacEnc_FreqToBandWidthRounding( + pCfg->filterStartFreq[LOFILT], sampleRate, pC->sfbCnt, + pC->sfbOffset); + tC->lpcStartLine[LOFILT] = pC->sfbOffset[tC->lpcStartBand[LOFILT]]; + + FDKaacEnc_CalcGaussWindow( + tC->acfWindow[HIFILT], tC->maxOrder + 1, sampleRate, granuleLength, + pCfg->tnsTimeResolution[HIFILT], TNS_TIMERES_SCALE); + FDKaacEnc_CalcGaussWindow( + tC->acfWindow[LOFILT], tC->maxOrder + 1, sampleRate, granuleLength, + pCfg->tnsTimeResolution[LOFILT], TNS_TIMERES_SCALE); + } else { + tC->tnsActive = + FALSE; /* no configuration available, disable tns tool */ } - break; + } break; default: tC->tnsActive = FALSE; /* no configuration available, disable tns tool */ } return AAC_ENC_OK; - } /***************************************************************************/ @@ -471,29 +516,25 @@ AAC_ENCODER_ERROR FDKaacEnc_InitTnsConfiguration(INT bitRate, \return scale factor ****************************************************************************/ -static inline INT FDKaacEnc_ScaleUpSpectrum( - FIXP_DBL *dest, - const FIXP_DBL *src, - const INT startLine, - const INT stopLine - ) -{ - INT i, scale; - - FIXP_DBL maxVal = FL2FXCONST_DBL(0.f); - - /* Get highest value in given spectrum */ - for (i=startLine; i>scale); - } +static inline FIXP_DBL FDKaacEnc_CalcAutoCorrValue(const FIXP_DBL *spectrum, + const INT startLine, + const INT stopLine, + const INT lag, + const INT scale) { + int i; + FIXP_DBL result = FL2FXCONST_DBL(0.f); + + /* This versions allows to save memory accesses, when computing pow2 */ + /* It is of interest for ARM, XTENSA without parallel memory access */ + if (lag == 0) { + for (i = startLine; i < stopLine; i++) { + result += (fPow2(spectrum[i]) >> scale); } - else { - for (i=startLine; i<(stopLine-lag); i++) { - result += (fMult(spectrum[i], spectrum[i+lag])>>scale); - } + } else { + for (i = startLine; i < (stopLine - lag); i++) { + result += (fMult(spectrum[i], spectrum[i + lag]) >> scale); } + } - return result; + return result; } /***************************************************************************/ @@ -545,172 +584,173 @@ static inline FIXP_DBL FDKaacEnc_CalcAutoCorrValue( \param filter start line ****************************************************************************/ -static inline FIXP_DBL FDKaacEnc_AutoCorrNormFac( - const FIXP_DBL value, - const INT scale, - INT *sc - ) -{ - #define HLM_MIN_NRG 0.0000000037252902984619140625f /* 2^-28 */ - #define MAX_INV_NRGFAC (1.f/HLM_MIN_NRG) - - FIXP_DBL retValue; - FIXP_DBL A, B; - - if (scale>=0) { - A = value; - B = FL2FXCONST_DBL(HLM_MIN_NRG)>>fixMin(DFRACT_BITS-1,scale); - } - else { - A = value>>fixMin(DFRACT_BITS-1,(-scale)); - B = FL2FXCONST_DBL(HLM_MIN_NRG); - } +static inline FIXP_DBL FDKaacEnc_AutoCorrNormFac(const FIXP_DBL value, + const INT scale, INT *sc) { +#define HLM_MIN_NRG 0.0000000037252902984619140625f /* 2^-28 */ +#define MAX_INV_NRGFAC (1.f / HLM_MIN_NRG) - if (A > B) { - int shift = 0; - FIXP_DBL tmp = invSqrtNorm2(value,&shift); + FIXP_DBL retValue; + FIXP_DBL A, B; - retValue = fMult(tmp,tmp); - *sc += (2*shift); - } - else { - /* MAX_INV_NRGFAC*FDKpow(2,-28) = 1/2^-28 * 2^-28 = 1.0 */ - retValue = /*FL2FXCONST_DBL(MAX_INV_NRGFAC*FDKpow(2,-28))*/ (FIXP_DBL)MAXVAL_DBL; - *sc += scale+28; - } + if (scale >= 0) { + A = value; + B = FL2FXCONST_DBL(HLM_MIN_NRG) >> fixMin(DFRACT_BITS - 1, scale); + } else { + A = value >> fixMin(DFRACT_BITS - 1, (-scale)); + B = FL2FXCONST_DBL(HLM_MIN_NRG); + } + + if (A > B) { + int shift = 0; + FIXP_DBL tmp = invSqrtNorm2(value, &shift); - return retValue; + retValue = fMult(tmp, tmp); + *sc += (2 * shift); + } else { + /* MAX_INV_NRGFAC*FDKpow(2,-28) = 1/2^-28 * 2^-28 = 1.0 */ + retValue = + /*FL2FXCONST_DBL(MAX_INV_NRGFAC*FDKpow(2,-28))*/ (FIXP_DBL)MAXVAL_DBL; + *sc += scale + 28; + } + + return retValue; } static void FDKaacEnc_MergedAutoCorrelation( - const FIXP_DBL *spectrum, - const INT isLowDelay, - const FIXP_DBL acfWindow[MAX_NUM_OF_FILTERS][TNS_MAX_ORDER+3+1], - const INT lpcStartLine[MAX_NUM_OF_FILTERS], - const INT lpcStopLine, - const INT maxOrder, - const INT acfSplit[MAX_NUM_OF_FILTERS], - FIXP_DBL *_rxx1, - FIXP_DBL *_rxx2 - ) -{ - int i, idx0, idx1, idx2, idx3, idx4, lag; - FIXP_DBL rxx1_0, rxx2_0, rxx3_0, rxx4_0; - - /* buffer for temporal spectrum */ - C_ALLOC_SCRATCH_START(pSpectrum, FIXP_DBL, (1024)); - - /* pre-initialization output */ - FDKmemclear(&_rxx1[0], sizeof(FIXP_DBL)*(maxOrder+1)); - FDKmemclear(&_rxx2[0], sizeof(FIXP_DBL)*(maxOrder+1)); - - idx0 = idx1 = idx2 = idx3 = idx4 = 0; - - /* MDCT line indices separating the 1st, 2nd, 3rd, and 4th analysis quarters */ - if ( (acfSplit[LOFILT]==-1) || (acfSplit[HIFILT]==-1) ) { - /* autocorrelation function for 1st, 2nd, 3rd, and 4th quarter of the spectrum */ - idx0 = lpcStartLine[LOFILT]; - i = lpcStopLine - lpcStartLine[LOFILT]; - idx1 = idx0 + i / 4; - idx2 = idx0 + i / 2; - idx3 = idx0 + i * 3 / 4; - idx4 = lpcStopLine; - } - else { - FDK_ASSERT(acfSplit[LOFILT]==1); - FDK_ASSERT(acfSplit[HIFILT]==3); - i = (lpcStopLine - lpcStartLine[HIFILT]) / 3; - idx0 = lpcStartLine[LOFILT]; - idx1 = lpcStartLine[HIFILT]; - idx2 = idx1 + i; - idx3 = idx2 + i; - idx4 = lpcStopLine; - } + const FIXP_DBL *spectrum, const INT isLowDelay, + const FIXP_DBL acfWindow[MAX_NUM_OF_FILTERS][TNS_MAX_ORDER + 3 + 1], + const INT lpcStartLine[MAX_NUM_OF_FILTERS], const INT lpcStopLine, + const INT maxOrder, const INT acfSplit[MAX_NUM_OF_FILTERS], FIXP_DBL *_rxx1, + FIXP_DBL *_rxx2) { + int i, idx0, idx1, idx2, idx3, idx4, lag; + FIXP_DBL rxx1_0, rxx2_0, rxx3_0, rxx4_0; + + /* buffer for temporal spectrum */ + C_ALLOC_SCRATCH_START(pSpectrum, FIXP_DBL, (1024)) + + /* MDCT line indices separating the 1st, 2nd, 3rd, and 4th analysis quarters + */ + if ((acfSplit[LOFILT] == -1) || (acfSplit[HIFILT] == -1)) { + /* autocorrelation function for 1st, 2nd, 3rd, and 4th quarter of the + * spectrum */ + idx0 = lpcStartLine[LOFILT]; + i = lpcStopLine - lpcStartLine[LOFILT]; + idx1 = idx0 + i / 4; + idx2 = idx0 + i / 2; + idx3 = idx0 + i * 3 / 4; + idx4 = lpcStopLine; + } else { + FDK_ASSERT(acfSplit[LOFILT] == 1); + FDK_ASSERT(acfSplit[HIFILT] == 3); + i = (lpcStopLine - lpcStartLine[HIFILT]) / 3; + idx0 = lpcStartLine[LOFILT]; + idx1 = lpcStartLine[HIFILT]; + idx2 = idx1 + i; + idx3 = idx2 + i; + idx4 = lpcStopLine; + } - /* copy spectrum to temporal buffer and scale up as much as possible */ - INT sc1 = FDKaacEnc_ScaleUpSpectrum(pSpectrum, spectrum, idx0, idx1); - INT sc2 = FDKaacEnc_ScaleUpSpectrum(pSpectrum, spectrum, idx1, idx2); - INT sc3 = FDKaacEnc_ScaleUpSpectrum(pSpectrum, spectrum, idx2, idx3); - INT sc4 = FDKaacEnc_ScaleUpSpectrum(pSpectrum, spectrum, idx3, idx4); - - /* get scaling values for summation */ - INT nsc1, nsc2, nsc3, nsc4; - for (nsc1=1; (1<dataRaw.Short.subBlockInfo[subBlockNumber] - : &tnsData->dataRaw.Long.subBlockInfo; + FDKmemclear(rxx1, sizeof(rxx1)); + FDKmemclear(rxx2, sizeof(rxx2)); + + TNS_SUBBLOCK_INFO *tsbi = + (blockType == SHORT_WINDOW) + ? &tnsData->dataRaw.Short.subBlockInfo[subBlockNumber] + : &tnsData->dataRaw.Long.subBlockInfo; - tnsData->filtersMerged = FALSE; + tnsData->filtersMerged = FALSE; - tsbi->tnsActive[HIFILT] = FALSE; - tsbi->predictionGain[HIFILT] = 1000; - tsbi->tnsActive[LOFILT] = FALSE; - tsbi->predictionGain[LOFILT] = 1000; + tsbi->tnsActive[HIFILT] = FALSE; + tsbi->predictionGain[HIFILT] = 1000; + tsbi->tnsActive[LOFILT] = FALSE; + tsbi->predictionGain[LOFILT] = 1000; tnsInfo->numOfFilters[subBlockNumber] = 0; - tnsInfo->coefRes[subBlockNumber] = tC->coefRes; + tnsInfo->coefRes[subBlockNumber] = tC->coefRes; for (i = 0; i < tC->maxOrder; i++) { - tnsInfo->coef[subBlockNumber][HIFILT][i] = tnsInfo->coef[subBlockNumber][LOFILT][i] = 0; + tnsInfo->coef[subBlockNumber][HIFILT][i] = + tnsInfo->coef[subBlockNumber][LOFILT][i] = 0; } - tnsInfo->length[subBlockNumber][HIFILT] = tnsInfo->length[subBlockNumber][LOFILT] = 0; - tnsInfo->order [subBlockNumber][HIFILT] = tnsInfo->order [subBlockNumber][LOFILT] = 0; + tnsInfo->length[subBlockNumber][HIFILT] = + tnsInfo->length[subBlockNumber][LOFILT] = 0; + tnsInfo->order[subBlockNumber][HIFILT] = + tnsInfo->order[subBlockNumber][LOFILT] = 0; - if ( (tC->tnsActive) && (tC->maxOrder>0) ) - { + if ((tC->tnsActive) && (tC->maxOrder > 0)) { int sumSqrCoef; FDKaacEnc_MergedAutoCorrelation( - spectrum, - tC->isLowDelay, - tC->acfWindow, - tC->lpcStartLine, - tC->lpcStopLine, - tC->maxOrder, - tC->confTab.acfSplit, - rxx1, - rxx2); - - /* compute higher TNS filter in lattice (ParCor) form with LeRoux-Gueguen algorithm */ - tsbi->predictionGain[HIFILT] = FDKaacEnc_AutoToParcor(rxx2, parcor_tmp, tC->confTab.tnsLimitOrder[HIFILT]); - - /* non-linear quantization of TNS lattice coefficients with given resolution */ - FDKaacEnc_Parcor2Index( - parcor_tmp, - tnsInfo->coef[subBlockNumber][HIFILT], - tC->confTab.tnsLimitOrder[HIFILT], - tC->coefRes); - - /* reduce filter order by truncating trailing zeros, compute sum(abs(coefs)) */ + spectrum, tC->isLowDelay, tC->acfWindow, tC->lpcStartLine, + tC->lpcStopLine, tC->maxOrder, tC->confTab.acfSplit, rxx1, rxx2); + + /* compute higher TNS filter coefficients in lattice form (ParCor) with + * LeRoux-Gueguen/Schur algorithm */ + { + FIXP_DBL predictionGain_m; + INT predictionGain_e; + + CLpc_AutoToParcor(rxx2, 0, parcor_tmp, tC->confTab.tnsLimitOrder[HIFILT], + &predictionGain_m, &predictionGain_e); + tsbi->predictionGain[HIFILT] = + (INT)fMultNorm(predictionGain_m, predictionGain_e, 1000, 31, 31); + } + + /* non-linear quantization of TNS lattice coefficients with given resolution + */ + FDKaacEnc_Parcor2Index(parcor_tmp, tnsInfo->coef[subBlockNumber][HIFILT], + tC->confTab.tnsLimitOrder[HIFILT], tC->coefRes); + + /* reduce filter order by truncating trailing zeros, compute sum(abs(coefs)) + */ for (i = tC->confTab.tnsLimitOrder[HIFILT] - 1; i >= 0; i--) { if (tnsInfo->coef[subBlockNumber][HIFILT][i] != 0) { break; @@ -796,35 +837,49 @@ INT FDKaacEnc_TnsDetect( sumSqrCoef = 0; for (; i >= 0; i--) { - sumSqrCoef += tnsInfo->coef[subBlockNumber][HIFILT][i] * tnsInfo->coef[subBlockNumber][HIFILT][i]; + sumSqrCoef += tnsInfo->coef[subBlockNumber][HIFILT][i] * + tnsInfo->coef[subBlockNumber][HIFILT][i]; } - tnsInfo->direction[subBlockNumber][HIFILT] = tC->confTab.tnsFilterDirection[HIFILT]; + tnsInfo->direction[subBlockNumber][HIFILT] = + tC->confTab.tnsFilterDirection[HIFILT]; tnsInfo->length[subBlockNumber][HIFILT] = sfbCnt - tC->lpcStartBand[HIFILT]; - /* disable TNS if predictionGain is less than 3dB or sumSqrCoef is too small */ - if ((tsbi->predictionGain[HIFILT] > tC->confTab.threshOn[HIFILT]) || (sumSqrCoef > (tC->confTab.tnsLimitOrder[HIFILT]/2 + 2))) - { + /* disable TNS if predictionGain is less than 3dB or sumSqrCoef is too small + */ + if ((tsbi->predictionGain[HIFILT] > tC->confTab.threshOn[HIFILT]) || + (sumSqrCoef > (tC->confTab.tnsLimitOrder[HIFILT] / 2 + 2))) { tsbi->tnsActive[HIFILT] = TRUE; tnsInfo->numOfFilters[subBlockNumber]++; - /* compute second filter for lower quarter; only allowed for long windows! */ - if ( (blockType != SHORT_WINDOW) && - (tC->confTab.filterEnabled[LOFILT]) && (tC->confTab.seperateFiltersAllowed) ) - { + /* compute second filter for lower quarter; only allowed for long windows! + */ + if ((blockType != SHORT_WINDOW) && (tC->confTab.filterEnabled[LOFILT]) && + (tC->confTab.seperateFiltersAllowed)) { /* compute second filter for lower frequencies */ - /* compute TNS filter in lattice (ParCor) form with LeRoux-Gueguen algorithm */ - INT predGain = FDKaacEnc_AutoToParcor(rxx1, parcor_tmp, tC->confTab.tnsLimitOrder[LOFILT]); + /* compute TNS filter in lattice (ParCor) form with LeRoux-Gueguen + * algorithm */ + INT predGain; + { + FIXP_DBL predictionGain_m; + INT predictionGain_e; + + CLpc_AutoToParcor(rxx1, 0, parcor_tmp, + tC->confTab.tnsLimitOrder[LOFILT], + &predictionGain_m, &predictionGain_e); + predGain = + (INT)fMultNorm(predictionGain_m, predictionGain_e, 1000, 31, 31); + } - /* non-linear quantization of TNS lattice coefficients with given resolution */ - FDKaacEnc_Parcor2Index( - parcor_tmp, - tnsInfo->coef[subBlockNumber][LOFILT], - tC->confTab.tnsLimitOrder[LOFILT], - tC->coefRes); + /* non-linear quantization of TNS lattice coefficients with given + * resolution */ + FDKaacEnc_Parcor2Index(parcor_tmp, + tnsInfo->coef[subBlockNumber][LOFILT], + tC->confTab.tnsLimitOrder[LOFILT], tC->coefRes); - /* reduce filter order by truncating trailing zeros, compute sum(abs(coefs)) */ + /* reduce filter order by truncating trailing zeros, compute + * sum(abs(coefs)) */ for (i = tC->confTab.tnsLimitOrder[LOFILT] - 1; i >= 0; i--) { if (tnsInfo->coef[subBlockNumber][LOFILT][i] != 0) { break; @@ -834,29 +889,37 @@ INT FDKaacEnc_TnsDetect( sumSqrCoef = 0; for (; i >= 0; i--) { - sumSqrCoef += tnsInfo->coef[subBlockNumber][LOFILT][i] * tnsInfo->coef[subBlockNumber][LOFILT][i]; + sumSqrCoef += tnsInfo->coef[subBlockNumber][LOFILT][i] * + tnsInfo->coef[subBlockNumber][LOFILT][i]; } - tnsInfo->direction[subBlockNumber][LOFILT] = tC->confTab.tnsFilterDirection[LOFILT]; - tnsInfo->length[subBlockNumber][LOFILT] = tC->lpcStartBand[HIFILT] - tC->lpcStartBand[LOFILT]; - - /* filter lower quarter if gain is high enough, but not if it's too high */ - if ( ( (predGain > tC->confTab.threshOn[LOFILT]) && (predGain < (16000 * tC->confTab.tnsLimitOrder[LOFILT])) ) - || ( (sumSqrCoef > 9) && (sumSqrCoef < 22 * tC->confTab.tnsLimitOrder[LOFILT]) ) ) - { - /* compare lower to upper filter; if they are very similar, merge them */ + tnsInfo->direction[subBlockNumber][LOFILT] = + tC->confTab.tnsFilterDirection[LOFILT]; + tnsInfo->length[subBlockNumber][LOFILT] = + tC->lpcStartBand[HIFILT] - tC->lpcStartBand[LOFILT]; + + /* filter lower quarter if gain is high enough, but not if it's too high + */ + if (((predGain > tC->confTab.threshOn[LOFILT]) && + (predGain < (16000 * tC->confTab.tnsLimitOrder[LOFILT]))) || + ((sumSqrCoef > 9) && + (sumSqrCoef < 22 * tC->confTab.tnsLimitOrder[LOFILT]))) { + /* compare lower to upper filter; if they are very similar, merge them + */ tsbi->tnsActive[LOFILT] = TRUE; sumSqrCoef = 0; for (i = 0; i < tC->confTab.tnsLimitOrder[LOFILT]; i++) { - sumSqrCoef += FDKabs(tnsInfo->coef[subBlockNumber][HIFILT][i] - tnsInfo->coef[subBlockNumber][LOFILT][i]); + sumSqrCoef += fAbs(tnsInfo->coef[subBlockNumber][HIFILT][i] - + tnsInfo->coef[subBlockNumber][LOFILT][i]); } - if ( (sumSqrCoef < 2) && - (tnsInfo->direction[subBlockNumber][LOFILT] == tnsInfo->direction[subBlockNumber][HIFILT]) ) - { + if ((sumSqrCoef < 2) && + (tnsInfo->direction[subBlockNumber][LOFILT] == + tnsInfo->direction[subBlockNumber][HIFILT])) { tnsData->filtersMerged = TRUE; - tnsInfo->length[subBlockNumber][HIFILT] = sfbCnt - tC->lpcStartBand[LOFILT]; + tnsInfo->length[subBlockNumber][HIFILT] = + sfbCnt - tC->lpcStartBand[LOFILT]; for (; i < tnsInfo->order[subBlockNumber][HIFILT]; i++) { - if (FDKabs(tnsInfo->coef[subBlockNumber][HIFILT][i]) > 1) { + if (fAbs(tnsInfo->coef[subBlockNumber][HIFILT][i]) > 1) { break; } } @@ -868,22 +931,19 @@ INT FDKaacEnc_TnsDetect( if (i < tnsInfo->order[subBlockNumber][HIFILT]) { tnsInfo->order[subBlockNumber][HIFILT] = i + 1; } - } - else { + } else { tnsInfo->numOfFilters[subBlockNumber]++; } } /* filter lower part */ - tsbi->predictionGain[LOFILT]=predGain; + tsbi->predictionGain[LOFILT] = predGain; } /* second filter allowed */ - } /* if predictionGain > 1437 ... */ - } /* maxOrder > 0 && tnsActive */ + } /* if predictionGain > 1437 ... */ + } /* maxOrder > 0 && tnsActive */ return 0; - } - /***************************************************************************/ /*! \brief FDKaacLdEnc_TnsSync @@ -898,79 +958,77 @@ INT FDKaacEnc_TnsDetect( \return void ****************************************************************************/ -void FDKaacEnc_TnsSync( - TNS_DATA *tnsDataDest, - const TNS_DATA *tnsDataSrc, - TNS_INFO *tnsInfoDest, - TNS_INFO *tnsInfoSrc, - const INT blockTypeDest, - const INT blockTypeSrc, - const TNS_CONFIG *tC - ) -{ +void FDKaacEnc_TnsSync(TNS_DATA *tnsDataDest, const TNS_DATA *tnsDataSrc, + TNS_INFO *tnsInfoDest, TNS_INFO *tnsInfoSrc, + const INT blockTypeDest, const INT blockTypeSrc, + const TNS_CONFIG *tC) { int i, w, absDiff, nWindows; TNS_SUBBLOCK_INFO *sbInfoDest; const TNS_SUBBLOCK_INFO *sbInfoSrc; - /* if one channel contains short blocks and the other not, do not synchronize */ - if ( (blockTypeSrc == SHORT_WINDOW && blockTypeDest != SHORT_WINDOW) || - (blockTypeDest == SHORT_WINDOW && blockTypeSrc != SHORT_WINDOW) ) - { + /* if one channel contains short blocks and the other not, do not synchronize + */ + if ((blockTypeSrc == SHORT_WINDOW && blockTypeDest != SHORT_WINDOW) || + (blockTypeDest == SHORT_WINDOW && blockTypeSrc != SHORT_WINDOW)) { return; } if (blockTypeDest != SHORT_WINDOW) { sbInfoDest = &tnsDataDest->dataRaw.Long.subBlockInfo; - sbInfoSrc = &tnsDataSrc->dataRaw.Long.subBlockInfo; - nWindows = 1; + sbInfoSrc = &tnsDataSrc->dataRaw.Long.subBlockInfo; + nWindows = 1; } else { sbInfoDest = &tnsDataDest->dataRaw.Short.subBlockInfo[0]; - sbInfoSrc = &tnsDataSrc->dataRaw.Short.subBlockInfo[0]; - nWindows = 8; + sbInfoSrc = &tnsDataSrc->dataRaw.Short.subBlockInfo[0]; + nWindows = 8; } - for (w=0; wtnsActive[HIFILT] || pSbInfoSrcW->tnsActive[HIFILT]) { - for (i = 0; i < tC->maxOrder; i++) { - absDiff = FDKabs(tnsInfoDest->coef[w][HIFILT][i] - tnsInfoSrc->coef[w][HIFILT][i]); - absDiffSum += absDiff; - /* if coefficients diverge too much between channels, do not synchronize */ - if ((absDiff > 1) || (absDiffSum > 2)) { - doSync = 0; - break; - } - } - - if (doSync) { - /* if no significant difference was detected, synchronize coefficient sets */ - if (pSbInfoSrcW->tnsActive[HIFILT]) { - /* no dest filter, or more dest than source filters: use one dest filter */ - if ((!pSbInfoDestW->tnsActive[HIFILT]) || - ((pSbInfoDestW->tnsActive[HIFILT]) && (tnsInfoDest->numOfFilters[w] > tnsInfoSrc->numOfFilters[w]))) - { - pSbInfoDestW->tnsActive[HIFILT] = tnsInfoDest->numOfFilters[w] = 1; - } - tnsDataDest->filtersMerged = tnsDataSrc->filtersMerged; - tnsInfoDest->order [w][HIFILT] = tnsInfoSrc->order [w][HIFILT]; - tnsInfoDest->length [w][HIFILT] = tnsInfoSrc->length [w][HIFILT]; - tnsInfoDest->direction [w][HIFILT] = tnsInfoSrc->direction [w][HIFILT]; - tnsInfoDest->coefCompress[w][HIFILT] = tnsInfoSrc->coefCompress[w][HIFILT]; - - for (i = 0; i < tC->maxOrder; i++) { - tnsInfoDest->coef[w][HIFILT][i] = tnsInfoSrc->coef[w][HIFILT][i]; - } - } - else - pSbInfoDestW->tnsActive[HIFILT] = tnsInfoDest->numOfFilters[w] = 0; - } + for (w = 0; w < nWindows; w++) { + const TNS_SUBBLOCK_INFO *pSbInfoSrcW = sbInfoSrc + w; + TNS_SUBBLOCK_INFO *pSbInfoDestW = sbInfoDest + w; + INT doSync = 1, absDiffSum = 0; + + /* if TNS is active in at least one channel, check if ParCor coefficients of + * higher filter are similar */ + if (pSbInfoDestW->tnsActive[HIFILT] || pSbInfoSrcW->tnsActive[HIFILT]) { + for (i = 0; i < tC->maxOrder; i++) { + absDiff = fAbs(tnsInfoDest->coef[w][HIFILT][i] - + tnsInfoSrc->coef[w][HIFILT][i]); + absDiffSum += absDiff; + /* if coefficients diverge too much between channels, do not synchronize + */ + if ((absDiff > 1) || (absDiffSum > 2)) { + doSync = 0; + break; } + } + if (doSync) { + /* if no significant difference was detected, synchronize coefficient + * sets */ + if (pSbInfoSrcW->tnsActive[HIFILT]) { + /* no dest filter, or more dest than source filters: use one dest + * filter */ + if ((!pSbInfoDestW->tnsActive[HIFILT]) || + ((pSbInfoDestW->tnsActive[HIFILT]) && + (tnsInfoDest->numOfFilters[w] > tnsInfoSrc->numOfFilters[w]))) { + pSbInfoDestW->tnsActive[HIFILT] = tnsInfoDest->numOfFilters[w] = 1; + } + tnsDataDest->filtersMerged = tnsDataSrc->filtersMerged; + tnsInfoDest->order[w][HIFILT] = tnsInfoSrc->order[w][HIFILT]; + tnsInfoDest->length[w][HIFILT] = tnsInfoSrc->length[w][HIFILT]; + tnsInfoDest->direction[w][HIFILT] = tnsInfoSrc->direction[w][HIFILT]; + tnsInfoDest->coefCompress[w][HIFILT] = + tnsInfoSrc->coefCompress[w][HIFILT]; + + for (i = 0; i < tC->maxOrder; i++) { + tnsInfoDest->coef[w][HIFILT][i] = tnsInfoSrc->coef[w][HIFILT][i]; + } + } else + pSbInfoDestW->tnsActive[HIFILT] = tnsInfoDest->numOfFilters[w] = 0; + } } + } } /***************************************************************************/ @@ -990,218 +1048,130 @@ void FDKaacEnc_TnsSync( \return ERROR STATUS ****************************************************************************/ -INT FDKaacEnc_TnsEncode( - TNS_INFO* tnsInfo, - TNS_DATA* tnsData, - const INT numOfSfb, - const TNS_CONFIG *tC, - const INT lowPassLine, - FIXP_DBL* spectrum, - const INT subBlockNumber, - const INT blockType - ) -{ - INT i, startLine, stopLine; - - if ( ( (blockType == SHORT_WINDOW) && (!tnsData->dataRaw.Short.subBlockInfo[subBlockNumber].tnsActive[HIFILT]) ) - || ( (blockType != SHORT_WINDOW) && (!tnsData->dataRaw.Long.subBlockInfo.tnsActive[HIFILT]) ) ) - { - return 1; - } +INT FDKaacEnc_TnsEncode(TNS_INFO *tnsInfo, TNS_DATA *tnsData, + const INT numOfSfb, const TNS_CONFIG *tC, + const INT lowPassLine, FIXP_DBL *spectrum, + const INT subBlockNumber, const INT blockType) { + INT i, startLine, stopLine; + + if (((blockType == SHORT_WINDOW) && + (!tnsData->dataRaw.Short.subBlockInfo[subBlockNumber] + .tnsActive[HIFILT])) || + ((blockType != SHORT_WINDOW) && + (!tnsData->dataRaw.Long.subBlockInfo.tnsActive[HIFILT]))) { + return 1; + } - startLine = (tnsData->filtersMerged) ? tC->lpcStartLine[LOFILT] : tC->lpcStartLine[HIFILT]; - stopLine = tC->lpcStopLine; - - for (i=0; inumOfFilters[subBlockNumber]; i++) { - - INT lpcGainFactor; - FIXP_DBL LpcCoeff[TNS_MAX_ORDER]; - FIXP_DBL workBuffer[TNS_MAX_ORDER]; - FIXP_DBL parcor_tmp[TNS_MAX_ORDER]; - - FDKaacEnc_Index2Parcor( - tnsInfo->coef[subBlockNumber][i], - parcor_tmp, - tnsInfo->order[subBlockNumber][i], - tC->coefRes); - - lpcGainFactor = FDKaacEnc_ParcorToLpc( - parcor_tmp, - LpcCoeff, - tnsInfo->order[subBlockNumber][i], - workBuffer); - - FDKaacEnc_AnalysisFilter( - &spectrum[startLine], - stopLine - startLine, - LpcCoeff, - tnsInfo->order[subBlockNumber][i], - lpcGainFactor); - - /* update for second filter */ - startLine = tC->lpcStartLine[LOFILT]; - stopLine = tC->lpcStartLine[HIFILT]; - } + startLine = (tnsData->filtersMerged) ? tC->lpcStartLine[LOFILT] + : tC->lpcStartLine[HIFILT]; + stopLine = tC->lpcStopLine; + + for (i = 0; i < tnsInfo->numOfFilters[subBlockNumber]; i++) { + INT lpcGainFactor; + FIXP_LPC LpcCoeff[TNS_MAX_ORDER]; + FIXP_DBL workBuffer[TNS_MAX_ORDER]; + FIXP_LPC parcor_tmp[TNS_MAX_ORDER]; + + FDKaacEnc_Index2Parcor(tnsInfo->coef[subBlockNumber][i], parcor_tmp, + tnsInfo->order[subBlockNumber][i], tC->coefRes); + + lpcGainFactor = CLpc_ParcorToLpc( + parcor_tmp, LpcCoeff, tnsInfo->order[subBlockNumber][i], workBuffer); - return(0); + FDKmemclear(workBuffer, TNS_MAX_ORDER * sizeof(FIXP_DBL)); + CLpc_Analysis(&spectrum[startLine], stopLine - startLine, LpcCoeff, + lpcGainFactor, tnsInfo->order[subBlockNumber][i], workBuffer, + NULL); + /* update for second filter */ + startLine = tC->lpcStartLine[LOFILT]; + stopLine = tC->lpcStartLine[HIFILT]; + } + + return (0); } -static void FDKaacEnc_CalcGaussWindow( - FIXP_DBL *win, - const int winSize, - const INT samplingRate, - const INT transformResolution, - const FIXP_DBL timeResolution, - const INT timeResolution_e - ) -{ - #define PI_E (2) - #define PI_M FL2FXCONST_DBL(3.1416f/(float)(1<> (DFRACT_BITS-1)); - tmp = (FIXP_DBL)((LONG)workBuffer[0]^sign); - - if(input[0]=0; j--) { - FIXP_DBL accu1 = fMult(tmp, input[j]); - FIXP_DBL accu2 = fMult(tmp, workBuffer[j]); - workBuffer[j] += accu1; - input[j] += accu2; - } - - workBuffer++; - } - - tmp = fMult((FIXP_DBL)((LONG)TNS_PREDGAIN_SCALE<<21), fDivNorm(fAbs(autoCorr_0), fAbs(input[0]), &scale)); - if ( fMultDiv2(autoCorr_0, input[0]) FDKaacEnc_tnsCoeff3Borders[i]) - index=i; + for (i = 0; i < 8; i++) { + if (parcor > FDKaacEnc_tnsCoeff3Borders[i]) index = i; } - return(index-4); + return (index - 4); } -static INT FDKaacEnc_Search4(FIXP_DBL parcor) -{ - INT i, index=0; +static INT FDKaacEnc_Search4(FIXP_LPC parcor) { + INT i, index = 0; - for(i=0;i<16;i++){ - if(parcor > FDKaacEnc_tnsCoeff4Borders[i]) - index=i; + for (i = 0; i < 16; i++) { + if (parcor > FDKaacEnc_tnsCoeff4Borders[i]) index = i; } - return(index-8); + return (index - 8); } - /***************************************************************************** functionname: FDKaacEnc_Parcor2Index *****************************************************************************/ -static void FDKaacEnc_Parcor2Index( - const FIXP_DBL *parcor, - INT *RESTRICT index, - const INT order, - const INT bitsPerCoeff - ) -{ +static void FDKaacEnc_Parcor2Index(const FIXP_LPC *parcor, INT *RESTRICT index, + const INT order, const INT bitsPerCoeff) { INT i; - for(i=0; i, - ptr. to work buffer (required size: order) - output: LPC coefficients - -*****************************************************************************/ -static INT FDKaacEnc_ParcorToLpc( - const FIXP_DBL *reflCoeff, - FIXP_DBL *RESTRICT LpcCoeff, - const INT numOfCoeff, - FIXP_DBL *RESTRICT workBuffer - ) -{ - INT i, j; - INT shiftval, par2LpcShiftVal = 6; /* 6 should be enough, bec. max(numOfCoeff) = 20 */ - FIXP_DBL maxVal = FL2FXCONST_DBL(0.0f); - - LpcCoeff[0] = reflCoeff[0] >> par2LpcShiftVal; - for(i=1; i> par2LpcShiftVal; - } - - /* normalize LpcCoeff and calc shiftfactor */ - for(i=0; i=par2LpcShiftVal) ? par2LpcShiftVal : shiftval; - - for(i=0; i0) { - - INT idx = 0; - - /* keep filter coefficients twice and save memory copy operation in - modulo state buffer */ -#if defined(ARCH_PREFER_MULT_32x16) - FIXP_SGL coeff[2*TNS_MAX_ORDER]; - const FIXP_SGL *pCoeff; - for(i=0;i=0); - signal[j] = (tmp<