PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/libAACdec/src/conceal.cpp

https://bitbucket.org/bohlooli/aac
C++ | 1825 lines | 1149 code | 259 blank | 417 comment | 221 complexity | 4d9301ff9bae49953a32aabc120b7b3e MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /* -----------------------------------------------------------------------------------------------------------
  2. Software License for The Fraunhofer FDK AAC Codec Library for Android
  3. © Copyright 1995 - 2012 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
  4. All rights reserved.
  5. 1. INTRODUCTION
  6. The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
  7. the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
  8. This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
  9. AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
  10. audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
  11. independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
  12. of the MPEG specifications.
  13. Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
  14. may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
  15. individually for the purpose of encoding or decoding bit streams in products that are compliant with
  16. the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
  17. these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
  18. software may already be covered under those patent licenses when it is used for those licensed purposes only.
  19. Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
  20. are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
  21. applications information and documentation.
  22. 2. COPYRIGHT LICENSE
  23. Redistribution and use in source and binary forms, with or without modification, are permitted without
  24. payment of copyright license fees provided that you satisfy the following conditions:
  25. You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
  26. your modifications thereto in source code form.
  27. You must retain the complete text of this software license in the documentation and/or other materials
  28. provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
  29. You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
  30. modifications thereto to recipients of copies in binary form.
  31. The name of Fraunhofer may not be used to endorse or promote products derived from this library without
  32. prior written permission.
  33. You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
  34. software or your modifications thereto.
  35. Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
  36. and the date of any change. For modified versions of the FDK AAC Codec, the term
  37. "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
  38. "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
  39. 3. NO PATENT LICENSE
  40. NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
  41. ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
  42. respect to this software.
  43. You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
  44. by appropriate patent licenses.
  45. 4. DISCLAIMER
  46. This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
  47. "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
  48. of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  49. CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
  50. including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
  51. or business interruption, however caused and on any theory of liability, whether in contract, strict
  52. liability, or tort (including negligence), arising in any way out of the use of this software, even if
  53. advised of the possibility of such damage.
  54. 5. CONTACT INFORMATION
  55. Fraunhofer Institute for Integrated Circuits IIS
  56. Attention: Audio and Multimedia Departments - FDK AAC LL
  57. Am Wolfsmantel 33
  58. 91058 Erlangen, Germany
  59. www.iis.fraunhofer.de/amm
  60. amm-info@iis.fraunhofer.de
  61. ----------------------------------------------------------------------------------------------------------- */
  62. /***************************** MPEG-4 AAC Decoder **************************
  63. Author(s): Josef Hoepfl
  64. Description: independent channel concealment
  65. ******************************************************************************/
  66. /*!
  67. \page concealment AAC core concealment
  68. This AAC core implementation includes a concealment function, which can be enabled
  69. using the several defines during compilation.
  70. There are various tests inside the core, starting with simple CRC tests and ending in
  71. a variety of plausibility checks. If such a check indicates an invalid bitstream, then
  72. concealment is applied.
  73. Concealment is also applied when the calling main program indicates a distorted or missing
  74. data frame using the frameOK flag. This is used for error detection on the transport layer.
  75. (See below)
  76. There are three concealment-modes:
  77. 1) Muting: The spectral data is simply set to zero in case of an detected error.
  78. 2) Noise substitution: In case of an detected error, concealment copies the last frame and adds
  79. attenuates the spectral data. For this mode you have to set the #CONCEAL_NOISE define.
  80. Noise substitution adds no additional delay.
  81. 3) Interpolation: The interpolation routine swaps the spectral data from the previous and the
  82. current frame just before the final frequency to time conversion. In case a single frame is
  83. corrupted, concealmant interpolates between the last good and the first good frame to create
  84. the spectral data for the missing frame. If multiple frames are corrupted, concealment
  85. implements first a fade out based on slightly modified spectral values from the last good
  86. frame. As soon as good frames are available, concealmant fades in the new spectral data.
  87. For this mode you have to set the #CONCEAL_INTER define. Note that in this case, you also
  88. need to set #SBR_BS_DELAY_ENABLE, which basically adds approriate delay in the SBR decoder.
  89. Note that the Interpolating-Concealment increases the delay of your decoder by one frame
  90. and that it does require additional resources such as memory and computational complexity.
  91. <h2>How concealment can be used with errors on the transport layer</h2>
  92. Many errors can or have to be detected on the transport layer. For example in IP based systems
  93. packet loss can occur. The transport protocol used should indicate such packet loss by inserting
  94. an empty frame with frameOK=0.
  95. */
  96. #include "conceal.h"
  97. #include "aac_rom.h"
  98. #include "genericStds.h"
  99. /* PNS (of block) */
  100. #include "aacdec_pns.h"
  101. #include "block.h"
  102. #include "FDK_tools_rom.h"
  103. #define CONCEAL_DFLT_COMF_NOISE_LEVEL ( 46 ) /* ~= -70 dB */
  104. /* default settings */
  105. #define CONCEAL_DFLT_FADEOUT_FRAMES ( 5 )
  106. #define CONCEAL_DFLT_FADEIN_FRAMES ( 5 )
  107. #define CONCEAL_DFLT_MUTE_RELEASE_FRAMES ( 3 )
  108. #define CONCEAL_DFLT_FADE_FACTOR ( 0.707106781186548f ) /* 1/sqrt(2) */
  109. /* some often used constants: */
  110. #define FIXP_ZERO FL2FXCONST_DBL(0.0f)
  111. #define FIXP_ONE FL2FXCONST_DBL(1.0f)
  112. #define FIXP_FL_CORRECTION FL2FXCONST_DBL(0.53333333333333333f)
  113. /* For parameter conversion */
  114. #define CONCEAL_PARAMETER_BITS ( 8 )
  115. #define CONCEAL_MAX_QUANT_FACTOR ( (1<<CONCEAL_PARAMETER_BITS)-1 )
  116. /*#define CONCEAL_MIN_ATTENUATION_FACTOR_025 ( FL2FXCONST_DBL(0.971627951577106174) )*/ /* -0.25 dB */
  117. #define CONCEAL_MIN_ATTENUATION_FACTOR_025_LD FL2FXCONST_DBL(-0.041524101186092029596853445212299)
  118. /*#define CONCEAL_MIN_ATTENUATION_FACTOR_050 ( FL2FXCONST_DBL(0.944060876285923380) )*/ /* -0.50 dB */
  119. #define CONCEAL_MIN_ATTENUATION_FACTOR_050_LD FL2FXCONST_DBL(-0.083048202372184059253597008145293)
  120. typedef enum {
  121. CConcealment_NoExpand,
  122. CConcealment_Expand,
  123. CConcealment_Compress
  124. }
  125. CConcealmentExpandType;
  126. static const FIXP_SGL facMod4Table[4] = {
  127. FL2FXCONST_SGL(0.500000000f), /* FIXP_SGL(0x4000), 2^-(1-0,00) */
  128. FL2FXCONST_SGL(0.594603558f), /* FIXP_SGL(0x4c1b), 2^-(1-0,25) */
  129. FL2FXCONST_SGL(0.707106781f), /* FIXP_SGL(0x5a82), 2^-(1-0,50) */
  130. FL2FXCONST_SGL(0.840896415f) /* FIXP_SGL(0x6ba2) 2^-(1-0,75) */
  131. };
  132. static void
  133. CConcealment_CalcBandEnergy (
  134. FIXP_DBL *spectrum,
  135. const SamplingRateInfo *pSamplingRateInfo,
  136. const int blockType,
  137. CConcealmentExpandType ex,
  138. int *sfbEnergy
  139. );
  140. static void
  141. CConcealment_InterpolateBuffer (
  142. FIXP_DBL *spectrum,
  143. SHORT *pSpecScalePrev,
  144. SHORT *pSpecScaleAct,
  145. SHORT *pSpecScaleOut,
  146. int *enPrv,
  147. int *enAct,
  148. int sfbCnt,
  149. const SHORT *pSfbOffset
  150. );
  151. static int
  152. CConcealment_ApplyInter (
  153. CConcealmentInfo *pConcealmentInfo,
  154. CAacDecoderChannelInfo *pAacDecoderChannelInfo,
  155. const SamplingRateInfo *pSamplingRateInfo,
  156. const int samplesPerFrame,
  157. const int improveTonal,
  158. const int frameOk
  159. );
  160. static int
  161. CConcealment_ApplyNoise (
  162. CConcealmentInfo *pConcealmentInfo,
  163. CAacDecoderChannelInfo *pAacDecoderChannelInfo,
  164. CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo,
  165. const SamplingRateInfo *pSamplingRateInfo,
  166. const int samplesPerFrame,
  167. const UINT flags
  168. );
  169. static void
  170. CConcealment_UpdateState (
  171. CConcealmentInfo *pConcealmentInfo,
  172. int frameOk
  173. );
  174. static void
  175. CConcealment_ApplyRandomSign (
  176. int iRandomPhase,
  177. FIXP_DBL *spec,
  178. int samplesPerFrame
  179. );
  180. static int CConcealment_GetWinSeq(int prevWinSeq)
  181. {
  182. int newWinSeq = OnlyLongSequence;
  183. /* Try to have only long blocks */
  184. if ( prevWinSeq == LongStartSequence
  185. || prevWinSeq == EightShortSequence )
  186. {
  187. newWinSeq = LongStopSequence;
  188. }
  189. return (newWinSeq);
  190. }
  191. /*!
  192. \brief Init common concealment information data
  193. \pConcealCommonData Pointer to the concealment common data structure.
  194. \return none
  195. */
  196. void
  197. CConcealment_InitCommonData (CConcealParams *pConcealCommonData)
  198. {
  199. if (pConcealCommonData != NULL)
  200. {
  201. int i;
  202. /* Set default error concealment technique */
  203. pConcealCommonData->method = ConcealMethodInter;
  204. pConcealCommonData->numFadeOutFrames = CONCEAL_DFLT_FADEOUT_FRAMES;
  205. pConcealCommonData->numFadeInFrames = CONCEAL_DFLT_FADEIN_FRAMES;
  206. pConcealCommonData->numMuteReleaseFrames = CONCEAL_DFLT_MUTE_RELEASE_FRAMES;
  207. pConcealCommonData->comfortNoiseLevel = CONCEAL_DFLT_COMF_NOISE_LEVEL;
  208. /* Init fade factors (symetric) */
  209. pConcealCommonData->fadeOutFactor[0] = FL2FXCONST_SGL( CONCEAL_DFLT_FADE_FACTOR );
  210. pConcealCommonData->fadeInFactor[0] = pConcealCommonData->fadeOutFactor[0];
  211. for (i = 1; i < CONCEAL_MAX_NUM_FADE_FACTORS; i++) {
  212. pConcealCommonData->fadeOutFactor[i] = FX_DBL2FX_SGL(fMult(pConcealCommonData->fadeOutFactor[i-1],FL2FXCONST_SGL(CONCEAL_DFLT_FADE_FACTOR)));
  213. pConcealCommonData->fadeInFactor[i] = pConcealCommonData->fadeOutFactor[i];
  214. }
  215. }
  216. }
  217. /*!
  218. \brief Get current concealment method.
  219. \pConcealCommonData Pointer to common concealment data (for all channels)
  220. \return Concealment method.
  221. */
  222. CConcealmentMethod
  223. CConcealment_GetMethod( CConcealParams *pConcealCommonData )
  224. {
  225. CConcealmentMethod method = ConcealMethodNone;
  226. if (pConcealCommonData != NULL) {
  227. method = pConcealCommonData->method;
  228. }
  229. return (method);
  230. }
  231. /*!
  232. \brief Init concealment information for each channel
  233. The function initializes the concealment information. Two methods can be chosen:
  234. 0 = interpolation method (adds delay)
  235. 1 = noise substitution (no delay, low complexity)
  236. \return none
  237. */
  238. void
  239. CConcealment_InitChannelData (
  240. CConcealmentInfo *pConcealChannelInfo,
  241. CConcealParams *pConcealCommonData,
  242. int samplesPerFrame )
  243. {
  244. int i;
  245. pConcealChannelInfo->pConcealParams = pConcealCommonData;
  246. FDKmemclear(pConcealChannelInfo->spectralCoefficient, 1024 * sizeof(FIXP_CNCL));
  247. for (i = 0; i < 8; i++) {
  248. pConcealChannelInfo->specScale[i] = 0;
  249. }
  250. pConcealChannelInfo->iRandomPhase = 0;
  251. pConcealChannelInfo->windowSequence = 0;
  252. pConcealChannelInfo->windowShape = 0;
  253. pConcealChannelInfo->prevFrameOk[0] = 1;
  254. pConcealChannelInfo->prevFrameOk[1] = 1;
  255. pConcealChannelInfo->cntFadeFrames = 0;
  256. pConcealChannelInfo->cntValidFrames = 0;
  257. pConcealChannelInfo->concealState = ConcealState_Ok;
  258. }
  259. /*!
  260. \brief Set error concealment parameters
  261. \concealParams
  262. \method
  263. \fadeOutSlope
  264. \fadeInSlope
  265. \muteRelease
  266. \comfNoiseLevel
  267. \return none
  268. */
  269. AAC_DECODER_ERROR
  270. CConcealment_SetParams (
  271. CConcealParams *concealParams,
  272. int method,
  273. int fadeOutSlope,
  274. int fadeInSlope,
  275. int muteRelease,
  276. int comfNoiseLevel )
  277. {
  278. /* set concealment technique */
  279. if (method != AACDEC_CONCEAL_PARAM_NOT_SPECIFIED) {
  280. switch ((CConcealmentMethod)method)
  281. {
  282. case ConcealMethodMute:
  283. case ConcealMethodNoise:
  284. case ConcealMethodInter:
  285. /* Be sure to enable delay adjustment of SBR decoder! */
  286. if (concealParams == NULL) {
  287. return AAC_DEC_INVALID_HANDLE;
  288. } else {
  289. /* set param */
  290. concealParams->method = (CConcealmentMethod)method;
  291. }
  292. break;
  293. default:
  294. return AAC_DEC_SET_PARAM_FAIL;
  295. }
  296. }
  297. /* set number of frames for fade-out slope */
  298. if (fadeOutSlope != AACDEC_CONCEAL_PARAM_NOT_SPECIFIED) {
  299. if ( (fadeOutSlope < CONCEAL_MAX_NUM_FADE_FACTORS)
  300. && (fadeOutSlope >= 0) )
  301. {
  302. if (concealParams == NULL) {
  303. return AAC_DEC_INVALID_HANDLE;
  304. } else {
  305. /* set param */
  306. concealParams->numFadeOutFrames = fadeOutSlope;
  307. }
  308. } else {
  309. return AAC_DEC_SET_PARAM_FAIL;
  310. }
  311. }
  312. /* set number of frames for fade-in slope */
  313. if (fadeInSlope != AACDEC_CONCEAL_PARAM_NOT_SPECIFIED) {
  314. if ( (fadeInSlope < CONCEAL_MAX_NUM_FADE_FACTORS)
  315. && (fadeInSlope >= 1) )
  316. {
  317. if (concealParams == NULL) {
  318. return AAC_DEC_INVALID_HANDLE;
  319. } else {
  320. /* set param */
  321. concealParams->numFadeInFrames = fadeInSlope;
  322. }
  323. } else {
  324. return AAC_DEC_SET_PARAM_FAIL;
  325. }
  326. }
  327. /* set number of error-free frames after which the muting will be released */
  328. if (muteRelease != AACDEC_CONCEAL_PARAM_NOT_SPECIFIED) {
  329. if ( (muteRelease < (CONCEAL_MAX_NUM_FADE_FACTORS<<1))
  330. && (muteRelease >= 0) )
  331. {
  332. if (concealParams == NULL) {
  333. return AAC_DEC_INVALID_HANDLE;
  334. } else {
  335. /* set param */
  336. concealParams->numMuteReleaseFrames = muteRelease;
  337. }
  338. } else {
  339. return AAC_DEC_SET_PARAM_FAIL;
  340. }
  341. }
  342. /* set confort noise level which will be inserted while in state 'muting' */
  343. if (comfNoiseLevel != AACDEC_CONCEAL_PARAM_NOT_SPECIFIED) {
  344. if ( (comfNoiseLevel < 0)
  345. || (comfNoiseLevel > 127) ) {
  346. return AAC_DEC_SET_PARAM_FAIL;
  347. }
  348. if (concealParams == NULL) {
  349. return AAC_DEC_INVALID_HANDLE;
  350. } else {
  351. concealParams->comfortNoiseLevel = comfNoiseLevel;
  352. }
  353. }
  354. return (AAC_DEC_OK);
  355. }
  356. /*!
  357. \brief Set fade-out/in attenuation factor vectors
  358. \concealParams
  359. \fadeOutAttenuationVector
  360. \fadeInAttenuationVector
  361. \return 0 if OK all other values indicate errors
  362. */
  363. AAC_DECODER_ERROR
  364. CConcealment_SetAttenuation (
  365. CConcealParams *concealParams,
  366. SHORT *fadeOutAttenuationVector,
  367. SHORT *fadeInAttenuationVector )
  368. {
  369. if ( (fadeOutAttenuationVector == NULL)
  370. && (fadeInAttenuationVector == NULL) ) {
  371. return AAC_DEC_SET_PARAM_FAIL;
  372. }
  373. /* Fade-out factors */
  374. if (fadeOutAttenuationVector != NULL)
  375. {
  376. int i;
  377. /* check quantized factors first */
  378. for (i = 0; i < CONCEAL_MAX_NUM_FADE_FACTORS; i++) {
  379. if ((fadeOutAttenuationVector[i] < 0) || (fadeOutAttenuationVector[i] > CONCEAL_MAX_QUANT_FACTOR)) {
  380. return AAC_DEC_SET_PARAM_FAIL;
  381. }
  382. }
  383. if (concealParams == NULL) {
  384. return AAC_DEC_INVALID_HANDLE;
  385. }
  386. /* now dequantize factors */
  387. for (i = 0; i < CONCEAL_MAX_NUM_FADE_FACTORS; i++)
  388. {
  389. concealParams->fadeOutFactor[i] =
  390. FX_DBL2FX_SGL( fLdPow( CONCEAL_MIN_ATTENUATION_FACTOR_025_LD,
  391. 0,
  392. (FIXP_DBL)((INT)(FL2FXCONST_DBL(1.0/2.0)>>(CONCEAL_PARAMETER_BITS-1)) * (INT)fadeOutAttenuationVector[i]),
  393. CONCEAL_PARAMETER_BITS
  394. )
  395. );
  396. }
  397. }
  398. /* Fade-in factors */
  399. if (fadeInAttenuationVector != NULL)
  400. {
  401. int i;
  402. /* check quantized factors first */
  403. for (i = 0; i < CONCEAL_MAX_NUM_FADE_FACTORS; i++) {
  404. if ((fadeInAttenuationVector[i] < 0) || (fadeInAttenuationVector[i] > CONCEAL_MAX_QUANT_FACTOR)) {
  405. return AAC_DEC_SET_PARAM_FAIL;
  406. }
  407. }
  408. if (concealParams == NULL) {
  409. return AAC_DEC_INVALID_HANDLE;
  410. }
  411. /* now dequantize factors */
  412. for (i = 0; i < CONCEAL_MAX_NUM_FADE_FACTORS; i++)
  413. {
  414. concealParams->fadeInFactor[i] =
  415. FX_DBL2FX_SGL( fLdPow( CONCEAL_MIN_ATTENUATION_FACTOR_025_LD,
  416. 0,
  417. (FIXP_DBL)((INT)(FIXP_ONE>>CONCEAL_PARAMETER_BITS) * (INT)fadeInAttenuationVector[i]),
  418. CONCEAL_PARAMETER_BITS
  419. )
  420. );
  421. }
  422. }
  423. return (AAC_DEC_OK);
  424. }
  425. /*!
  426. \brief Get state of concealment module.
  427. \pConcealChannelInfo
  428. \return Concealment state.
  429. */
  430. CConcealmentState
  431. CConcealment_GetState (
  432. CConcealmentInfo *pConcealChannelInfo
  433. )
  434. {
  435. CConcealmentState state = ConcealState_Ok;
  436. if (pConcealChannelInfo != NULL) {
  437. state = pConcealChannelInfo->concealState;
  438. }
  439. return (state);
  440. }
  441. static void CConcealment_fakePnsData (
  442. CPnsData *pPnsData,
  443. CIcsInfo *pIcsInfo,
  444. const SamplingRateInfo *pSamplingRateInfo,
  445. SHORT *pSpecScale,
  446. SHORT *pScaleFactor,
  447. const int level )
  448. {
  449. CPnsInterChannelData *pInterChannelData = pPnsData->pPnsInterChannelData;
  450. int pnsBand, band, group, win;
  451. //int delta = 0;
  452. int windowsPerFrame = GetWindowsPerFrame(pIcsInfo);
  453. int refLevel = (windowsPerFrame > 1) ? 82 : 91;
  454. FDK_ASSERT(level >= 0 && level <= 127);
  455. for (win = 0; win < windowsPerFrame; win++) {
  456. pSpecScale[win] = 31;
  457. }
  458. /* fake ICS info if necessary */
  459. if (!IsValid(pIcsInfo)) {
  460. pIcsInfo->WindowGroups = 1;
  461. if (IsLongBlock(pIcsInfo)) {
  462. pIcsInfo->TotalSfBands = pSamplingRateInfo->NumberOfScaleFactorBands_Long;
  463. pIcsInfo->WindowGroupLength[0] = 1;
  464. }
  465. else {
  466. pIcsInfo->TotalSfBands = pSamplingRateInfo->NumberOfScaleFactorBands_Short;
  467. pIcsInfo->WindowGroupLength[0] = 8;
  468. }
  469. pIcsInfo->MaxSfBands = pIcsInfo->TotalSfBands;
  470. }
  471. /* global activate PNS */
  472. pPnsData->PnsActive = 1;
  473. /* set energy level */
  474. pPnsData->CurrentEnergy = fixMax( 0, refLevel - level );
  475. /*
  476. value: | Avg. RMS power | Avg. RMS power |
  477. | specScale = 22 | specScale = 31 |
  478. -------+----------------+----------------+
  479. 5 | | -99.0 dB
  480. 15 | | -90.0 dB
  481. 25 | | -89.7 dB
  482. 35 | | -85.3 dB
  483. ... | ... | ...
  484. 45 | -69.9 dB | -70.0 dB
  485. 50 | -62.2 dB |
  486. 55 | -55.6 dB | -54.6 dB
  487. 60 | -47.0 dB |
  488. 65 | -39.5 dB | -39.5 dB
  489. 70 | -31.9 dB |
  490. 75 | -24.4 dB | -24.4 dB
  491. 80 | -16.9 dB |
  492. 85 | -9.4 dB (c) | -9.4 dB
  493. 90 | -3.9 dB (c) |
  494. 95 | | -2.1 dB
  495. 100 | | -1.6 dB
  496. 105 | | -1.4 dB
  497. */
  498. for (group=0; group < GetWindowGroups(pIcsInfo); group++)
  499. {
  500. for (band=0; band < GetScaleFactorBandsTransmitted(pIcsInfo); band++)
  501. {
  502. pnsBand = group * 16 + band;
  503. if (pnsBand >= NO_OFBANDS) {
  504. return;
  505. }
  506. //pPnsData->CurrentEnergy += delta ;
  507. pScaleFactor[pnsBand] = pPnsData->CurrentEnergy;
  508. pInterChannelData->correlated[pnsBand] = 0;
  509. pPnsData->pnsUsed[pnsBand] = 1;
  510. }
  511. }
  512. }
  513. /*!
  514. \brief Store data for concealment techniques applied later
  515. Interface function to store data for different concealment strategies
  516. \return none
  517. */
  518. void
  519. CConcealment_Store (
  520. CConcealmentInfo *hConcealmentInfo,
  521. CAacDecoderChannelInfo *pAacDecoderChannelInfo,
  522. CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo )
  523. {
  524. if ( !(pAacDecoderChannelInfo->renderMode == AACDEC_RENDER_LPD
  525. ) )
  526. {
  527. FIXP_DBL *pSpectralCoefficient = SPEC_LONG(pAacDecoderChannelInfo->pSpectralCoefficient);
  528. SHORT *pSpecScale = pAacDecoderChannelInfo->specScale;
  529. CIcsInfo *pIcsInfo = &pAacDecoderChannelInfo->icsInfo;
  530. SHORT tSpecScale[8];
  531. UCHAR tWindowShape, tWindowSequence;
  532. /* store old window infos for swapping */
  533. tWindowSequence = hConcealmentInfo->windowSequence;
  534. tWindowShape = hConcealmentInfo->windowShape;
  535. /* store old scale factors for swapping */
  536. FDKmemcpy(tSpecScale, hConcealmentInfo->specScale, 8*sizeof(SHORT));
  537. /* store new window infos */
  538. hConcealmentInfo->windowSequence = GetWindowSequence(pIcsInfo);
  539. hConcealmentInfo->windowShape = GetWindowShape(pIcsInfo);
  540. hConcealmentInfo->lastWinGrpLen = *(GetWindowGroupLengthTable(pIcsInfo)+GetWindowGroups(pIcsInfo)-1);
  541. /* store new scale factors */
  542. FDKmemcpy(hConcealmentInfo->specScale, pSpecScale, 8*sizeof(SHORT));
  543. if (CConcealment_GetDelay(hConcealmentInfo->pConcealParams) == 0)
  544. {
  545. /* store new spectral bins */
  546. #if (CNCL_FRACT_BITS == DFRACT_BITS)
  547. FDKmemcpy(hConcealmentInfo->spectralCoefficient, pSpectralCoefficient, 1024 * sizeof(FIXP_CNCL));
  548. #else
  549. FIXP_CNCL *RESTRICT pCncl = &hConcealmentInfo->spectralCoefficient[1024-1];
  550. FIXP_DBL *RESTRICT pSpec = &pSpectralCoefficient[1024-1];
  551. int i;
  552. for (i = 1024; i != 0; i--) {
  553. *pCncl-- = FX_DBL2FX_CNCL(*pSpec--);
  554. }
  555. #endif
  556. }
  557. else
  558. {
  559. FIXP_CNCL *RESTRICT pCncl = &hConcealmentInfo->spectralCoefficient[1024-1];
  560. FIXP_DBL *RESTRICT pSpec = &pSpectralCoefficient[1024-1];
  561. int i;
  562. /* swap spectral data */
  563. for (i = 1024; i != 0; i--) {
  564. FIXP_DBL tSpec = *pSpec;
  565. *pSpec-- = FX_CNCL2FX_DBL(*pCncl);
  566. *pCncl-- = FX_DBL2FX_CNCL( tSpec);
  567. }
  568. /* complete swapping of window infos */
  569. pIcsInfo->WindowSequence = tWindowSequence;
  570. pIcsInfo->WindowShape = tWindowShape;
  571. /* complete swapping of scale factors */
  572. FDKmemcpy(pSpecScale, tSpecScale, 8*sizeof(SHORT));
  573. }
  574. }
  575. }
  576. /*!
  577. \brief Apply concealment
  578. Interface function to different concealment strategies
  579. \return none
  580. */
  581. int
  582. CConcealment_Apply (
  583. CConcealmentInfo *hConcealmentInfo,
  584. CAacDecoderChannelInfo *pAacDecoderChannelInfo,
  585. CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo,
  586. const SamplingRateInfo *pSamplingRateInfo,
  587. const int samplesPerFrame,
  588. const UCHAR lastLpdMode,
  589. const int frameOk,
  590. const UINT flags)
  591. {
  592. int appliedProcessing = 0;
  593. if ( (frameOk == 0)
  594. && (pAacDecoderChannelInfo->renderMode != (AACDEC_RENDER_MODE)hConcealmentInfo->lastRenderMode) ) {
  595. /* restore the last render mode to stay in the same domain which allows to do a proper concealment */
  596. pAacDecoderChannelInfo->renderMode = (AACDEC_RENDER_MODE)hConcealmentInfo->lastRenderMode;
  597. } else {
  598. /* otherwise store the current mode */
  599. hConcealmentInfo->lastRenderMode = (SCHAR)pAacDecoderChannelInfo->renderMode;
  600. }
  601. if ( frameOk )
  602. {
  603. /* Rescue current data for concealment in future frames */
  604. CConcealment_Store ( hConcealmentInfo,
  605. pAacDecoderChannelInfo,
  606. pAacDecoderStaticChannelInfo );
  607. /* Reset index to random sign vector to make sign calculation frame agnostic
  608. (only depends on number of subsequently concealed spectral blocks) */
  609. hConcealmentInfo->iRandomPhase = 0;
  610. }
  611. /* hand current frame status to the state machine */
  612. CConcealment_UpdateState( hConcealmentInfo,
  613. frameOk );
  614. if ( !frameOk )
  615. {
  616. /* Create data for signal rendering according to the selected concealment method and decoder operating mode. */
  617. if ( !(pAacDecoderChannelInfo->renderMode == AACDEC_RENDER_LPD
  618. )
  619. )
  620. {
  621. switch (hConcealmentInfo->pConcealParams->method)
  622. {
  623. default:
  624. case ConcealMethodMute:
  625. /* Mute spectral data in case of errors */
  626. FDKmemclear(pAacDecoderChannelInfo->pSpectralCoefficient, samplesPerFrame * sizeof(FIXP_DBL));
  627. /* Set last window shape */
  628. pAacDecoderChannelInfo->icsInfo.WindowShape = hConcealmentInfo->windowShape;
  629. appliedProcessing = 1;
  630. break;
  631. case ConcealMethodNoise:
  632. /* Noise substitution error concealment technique */
  633. appliedProcessing =
  634. CConcealment_ApplyNoise (hConcealmentInfo,
  635. pAacDecoderChannelInfo,
  636. pAacDecoderStaticChannelInfo,
  637. pSamplingRateInfo,
  638. samplesPerFrame,
  639. flags);
  640. break;
  641. case ConcealMethodInter:
  642. /* Energy interpolation concealment based on 3GPP */
  643. appliedProcessing =
  644. CConcealment_ApplyInter (hConcealmentInfo,
  645. pAacDecoderChannelInfo,
  646. pSamplingRateInfo,
  647. samplesPerFrame,
  648. 0, /* don't use tonal improvement */
  649. 0);
  650. break;
  651. }
  652. }
  653. }
  654. /* update history */
  655. hConcealmentInfo->prevFrameOk[0] = hConcealmentInfo->prevFrameOk[1];
  656. hConcealmentInfo->prevFrameOk[1] = frameOk;
  657. return appliedProcessing;
  658. }
  659. /*!
  660. \brief Apply concealment noise substitution
  661. In case of frame lost this function produces a noisy frame with respect to the
  662. energies values of past frame.
  663. \return none
  664. */
  665. static int
  666. CConcealment_ApplyNoise (CConcealmentInfo *pConcealmentInfo,
  667. CAacDecoderChannelInfo *pAacDecoderChannelInfo,
  668. CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo,
  669. const SamplingRateInfo *pSamplingRateInfo,
  670. const int samplesPerFrame,
  671. const UINT flags)
  672. {
  673. CConcealParams *pConcealCommonData = pConcealmentInfo->pConcealParams;
  674. FIXP_DBL *pSpectralCoefficient = SPEC_LONG(pAacDecoderChannelInfo->pSpectralCoefficient);
  675. SHORT *pSpecScale = pAacDecoderChannelInfo->specScale;
  676. CIcsInfo *pIcsInfo = &pAacDecoderChannelInfo->icsInfo;
  677. int appliedProcessing = 0;
  678. FDK_ASSERT((samplesPerFrame>=480) && (samplesPerFrame<=1024));
  679. FDK_ASSERT((samplesPerFrame&0x1F) == 0);
  680. switch (pConcealmentInfo->concealState)
  681. {
  682. case ConcealState_Ok:
  683. /* Nothing to do here! */
  684. break;
  685. case ConcealState_Single:
  686. case ConcealState_FadeOut:
  687. {
  688. /* restore frequency coefficients from buffer with a specific muting */
  689. FIXP_SGL fac;
  690. int win, numWindows = 1;
  691. int windowLen = samplesPerFrame;
  692. int tFadeFrames, lastWindow = 0;
  693. int win_idx_stride = 1;
  694. FDK_ASSERT(pConcealmentInfo != NULL);
  695. FDK_ASSERT(pConcealmentInfo->cntFadeFrames >= 0);
  696. FDK_ASSERT(pConcealmentInfo->cntFadeFrames < CONCEAL_MAX_NUM_FADE_FACTORS);
  697. FDK_ASSERT(pConcealmentInfo->cntFadeFrames <= pConcealCommonData->numFadeOutFrames);
  698. /* get attenuation factor */
  699. tFadeFrames = pConcealmentInfo->cntFadeFrames;
  700. fac = pConcealCommonData->fadeOutFactor[tFadeFrames];
  701. /* set old window parameters */
  702. {
  703. pIcsInfo->WindowShape = pConcealmentInfo->windowShape;
  704. pIcsInfo->WindowSequence = pConcealmentInfo->windowSequence;
  705. if (pConcealmentInfo->windowSequence == 2) {
  706. /* short block handling */
  707. numWindows = 8;
  708. windowLen = samplesPerFrame >> 3;
  709. lastWindow = numWindows - pConcealmentInfo->lastWinGrpLen;
  710. }
  711. }
  712. for (win = 0; win < numWindows; win++) {
  713. FIXP_CNCL *pCncl = pConcealmentInfo->spectralCoefficient + (lastWindow * windowLen);
  714. FIXP_DBL *pOut = pSpectralCoefficient + (win * windowLen);
  715. int i;
  716. FDK_ASSERT((lastWindow * windowLen + windowLen) <= samplesPerFrame);
  717. /* restore frequency coefficients from buffer with a specific attenuation */
  718. for (i = 0; i < windowLen; i++) {
  719. pOut[i] = fMult(pCncl[i], fac);
  720. }
  721. /* apply random change of sign for spectral coefficients */
  722. CConcealment_ApplyRandomSign(pConcealmentInfo->iRandomPhase,
  723. pOut,
  724. windowLen );
  725. /* Increment random phase index to avoid repetition artifacts. */
  726. pConcealmentInfo->iRandomPhase = (pConcealmentInfo->iRandomPhase + 1) & (AAC_NF_NO_RANDOM_VAL - 1);
  727. /* set old scale factors */
  728. pSpecScale[win*win_idx_stride] = pConcealmentInfo->specScale[win_idx_stride*lastWindow++];
  729. if ( (lastWindow >= numWindows)
  730. && (numWindows > 1) )
  731. {
  732. /* end of sequence -> rewind */
  733. lastWindow = numWindows - pConcealmentInfo->lastWinGrpLen;
  734. /* update the attenuation factor to get a faster fade-out */
  735. tFadeFrames += 1;
  736. if (tFadeFrames < pConcealCommonData->numFadeOutFrames) {
  737. fac = pConcealCommonData->fadeOutFactor[tFadeFrames];
  738. } else {
  739. fac = (FIXP_SGL)0;
  740. }
  741. }
  742. }
  743. /* store temp vars */
  744. pConcealmentInfo->cntFadeFrames = tFadeFrames;
  745. appliedProcessing = 1;
  746. }
  747. break;
  748. case ConcealState_Mute:
  749. {
  750. /* set dummy window parameters */
  751. pIcsInfo->Valid = 0; /* Trigger the generation of a consitent IcsInfo */
  752. pIcsInfo->WindowShape = pConcealmentInfo->windowShape; /* Prevent an invalid WindowShape (required for F/T transform) */
  753. pIcsInfo->WindowSequence = CConcealment_GetWinSeq(pConcealmentInfo->windowSequence);
  754. pConcealmentInfo->windowSequence = pIcsInfo->WindowSequence; /* Store for next frame (spectrum in concealment buffer can't be used at all) */
  755. /* mute spectral data */
  756. FDKmemclear(pSpectralCoefficient, samplesPerFrame * sizeof(FIXP_DBL));
  757. if ( !(flags & (AC_USAC|AC_RSVD50))
  758. && pConcealCommonData->comfortNoiseLevel >= 0
  759. && pConcealCommonData->comfortNoiseLevel <= 61 /* -90dB */)
  760. {
  761. /* insert comfort noise using PNS */
  762. CConcealment_fakePnsData (
  763. &pAacDecoderChannelInfo->data.aac.PnsData,
  764. pIcsInfo,
  765. pSamplingRateInfo,
  766. pAacDecoderChannelInfo->pDynData->aSfbScale,
  767. pAacDecoderChannelInfo->pDynData->aScaleFactor,
  768. pConcealCommonData->comfortNoiseLevel
  769. );
  770. CPns_Apply (
  771. &pAacDecoderChannelInfo->data.aac.PnsData,
  772. pIcsInfo,
  773. pAacDecoderChannelInfo->pSpectralCoefficient,
  774. pAacDecoderChannelInfo->specScale,
  775. pAacDecoderChannelInfo->pDynData->aScaleFactor,
  776. pSamplingRateInfo,
  777. pAacDecoderChannelInfo->granuleLength,
  778. 0 /* always apply to first channel */
  779. );
  780. }
  781. appliedProcessing = 1;
  782. }
  783. break;
  784. case ConcealState_FadeIn:
  785. {
  786. FDK_ASSERT(pConcealmentInfo->cntFadeFrames >= 0);
  787. FDK_ASSERT(pConcealmentInfo->cntFadeFrames < CONCEAL_MAX_NUM_FADE_FACTORS);
  788. FDK_ASSERT(pConcealmentInfo->cntFadeFrames < pConcealCommonData->numFadeInFrames);
  789. /* attenuate signal to get a smooth fade-in */
  790. FIXP_DBL *RESTRICT pOut = &pSpectralCoefficient[samplesPerFrame-1];
  791. FIXP_SGL fac = pConcealCommonData->fadeInFactor[pConcealmentInfo->cntFadeFrames];
  792. int i;
  793. for (i = samplesPerFrame; i != 0; i--) {
  794. *pOut = fMult(*pOut, fac);
  795. pOut--;
  796. }
  797. appliedProcessing = 1;
  798. }
  799. break;
  800. default:
  801. /* we shouldn't come here anyway */
  802. FDK_ASSERT(0);
  803. break;
  804. }
  805. return appliedProcessing;
  806. }
  807. /*!
  808. \brief Apply concealment interpolation
  809. The function swaps the data from the current and the previous frame. If an
  810. error has occured, frame interpolation is performed to restore the missing
  811. frame. In case of multiple faulty frames, fade-in and fade-out is applied.
  812. \return none
  813. */
  814. static int
  815. CConcealment_ApplyInter (
  816. CConcealmentInfo *pConcealmentInfo,
  817. CAacDecoderChannelInfo *pAacDecoderChannelInfo,
  818. const SamplingRateInfo *pSamplingRateInfo,
  819. const int samplesPerFrame,
  820. const int improveTonal,
  821. const int frameOk )
  822. {
  823. CConcealParams *pConcealCommonData = pConcealmentInfo->pConcealParams;
  824. FIXP_DBL *pSpectralCoefficient = SPEC_LONG(pAacDecoderChannelInfo->pSpectralCoefficient);
  825. CIcsInfo *pIcsInfo = &pAacDecoderChannelInfo->icsInfo;
  826. SHORT *pSpecScale = pAacDecoderChannelInfo->specScale;
  827. int sfbEnergyPrev[64];
  828. int sfbEnergyAct [64];
  829. int i, appliedProcessing = 0;
  830. /* clear/init */
  831. FDKmemclear(sfbEnergyPrev, 64 * sizeof(int));
  832. FDKmemclear(sfbEnergyAct, 64 * sizeof(int));
  833. if (!frameOk)
  834. {
  835. /* Restore last frame from concealment buffer */
  836. pIcsInfo->WindowShape = pConcealmentInfo->windowShape;
  837. pIcsInfo->WindowSequence = pConcealmentInfo->windowSequence;
  838. /* Restore spectral data */
  839. for (i = 0; i < samplesPerFrame; i++) {
  840. pSpectralCoefficient[i] = FX_CNCL2FX_DBL(pConcealmentInfo->spectralCoefficient[i]);
  841. }
  842. /* Restore scale factors */
  843. FDKmemcpy(pSpecScale, pConcealmentInfo->specScale, 8*sizeof(SHORT));
  844. }
  845. /* if previous frame was not ok */
  846. if (!pConcealmentInfo->prevFrameOk[1]) {
  847. /* if current frame (f_n) is ok and the last but one frame (f_(n-2))
  848. was ok, too, then interpolate both frames in order to generate
  849. the current output frame (f_(n-1)). Otherwise, use the last stored
  850. frame (f_(n-2) or f_(n-3) or ...). */
  851. if (frameOk && pConcealmentInfo->prevFrameOk[0])
  852. {
  853. appliedProcessing = 1;
  854. /* Interpolate both frames in order to generate the current output frame (f_(n-1)). */
  855. if (pIcsInfo->WindowSequence == EightShortSequence) {
  856. /* f_(n-2) == EightShortSequence */
  857. /* short--??????--short, short--??????--long interpolation */
  858. /* short--short---short, short---long---long interpolation */
  859. int wnd;
  860. if (pConcealmentInfo->windowSequence == EightShortSequence) { /* f_n == EightShortSequence */
  861. /* short--short---short interpolation */
  862. int scaleFactorBandsTotal = pSamplingRateInfo->NumberOfScaleFactorBands_Short;
  863. const SHORT *pSfbOffset = pSamplingRateInfo->ScaleFactorBands_Short;
  864. pIcsInfo->WindowShape = 1;
  865. pIcsInfo->WindowSequence = EightShortSequence;
  866. for (wnd = 0; wnd < 8; wnd++)
  867. {
  868. CConcealment_CalcBandEnergy(
  869. &pSpectralCoefficient[wnd * (samplesPerFrame / 8)], /* spec_(n-2) */
  870. pSamplingRateInfo,
  871. EightShortSequence,
  872. CConcealment_NoExpand,
  873. sfbEnergyPrev);
  874. CConcealment_CalcBandEnergy(
  875. &pConcealmentInfo->spectralCoefficient[wnd * (samplesPerFrame / 8)], /* spec_n */
  876. pSamplingRateInfo,
  877. EightShortSequence,
  878. CConcealment_NoExpand,
  879. sfbEnergyAct);
  880. CConcealment_InterpolateBuffer(
  881. &pSpectralCoefficient[wnd * (samplesPerFrame / 8)], /* spec_(n-1) */
  882. &pSpecScale[wnd],
  883. &pConcealmentInfo->specScale[wnd],
  884. &pSpecScale[wnd],
  885. sfbEnergyPrev,
  886. sfbEnergyAct,
  887. scaleFactorBandsTotal,
  888. pSfbOffset);
  889. }
  890. } else { /* f_n != EightShortSequence */
  891. /* short---long---long interpolation */
  892. int scaleFactorBandsTotal = pSamplingRateInfo->NumberOfScaleFactorBands_Long;
  893. const SHORT *pSfbOffset = pSamplingRateInfo->ScaleFactorBands_Long;
  894. SHORT specScaleOut;
  895. CConcealment_CalcBandEnergy(&pSpectralCoefficient[samplesPerFrame - (samplesPerFrame / 8)], /* [wnd] spec_(n-2) */
  896. pSamplingRateInfo,
  897. EightShortSequence,
  898. CConcealment_Expand,
  899. sfbEnergyAct);
  900. CConcealment_CalcBandEnergy(pConcealmentInfo->spectralCoefficient, /* spec_n */
  901. pSamplingRateInfo,
  902. OnlyLongSequence,
  903. CConcealment_NoExpand,
  904. sfbEnergyPrev);
  905. pIcsInfo->WindowShape = 0;
  906. pIcsInfo->WindowSequence = LongStopSequence;
  907. for (i = 0; i < samplesPerFrame ; i++) {
  908. pSpectralCoefficient[i] = pConcealmentInfo->spectralCoefficient[i]; /* spec_n */
  909. }
  910. for (i = 0; i < 8; i++) { /* search for max(specScale) */
  911. if (pSpecScale[i] > pSpecScale[0]) {
  912. pSpecScale[0] = pSpecScale[i];
  913. }
  914. }
  915. CConcealment_InterpolateBuffer(
  916. pSpectralCoefficient, /* spec_(n-1) */
  917. &pConcealmentInfo->specScale[0],
  918. &pSpecScale[0],
  919. &specScaleOut,
  920. sfbEnergyPrev,
  921. sfbEnergyAct,
  922. scaleFactorBandsTotal,
  923. pSfbOffset);
  924. pSpecScale[0] = specScaleOut;
  925. }
  926. } else {
  927. /* long--??????--short, long--??????--long interpolation */
  928. /* long---long---short, long---long---long interpolation */
  929. int scaleFactorBandsTotal = pSamplingRateInfo->NumberOfScaleFactorBands_Long;
  930. const SHORT *pSfbOffset = pSamplingRateInfo->ScaleFactorBands_Long;
  931. SHORT specScaleAct = pConcealmentInfo->specScale[0];
  932. CConcealment_CalcBandEnergy(pSpectralCoefficient, /* spec_(n-2) */
  933. pSamplingRateInfo,
  934. OnlyLongSequence,
  935. CConcealment_NoExpand,
  936. sfbEnergyPrev);
  937. if (pConcealmentInfo->windowSequence == EightShortSequence) { /* f_n == EightShortSequence */
  938. /* long---long---short interpolation */
  939. pIcsInfo->WindowShape = 1;
  940. pIcsInfo->WindowSequence = LongStartSequence;
  941. for (i = 1; i < 8; i++) { /* search for max(specScale) */
  942. if (pConcealmentInfo->specScale[i] > specScaleAct) {
  943. specScaleAct = pConcealmentInfo->specScale[i];
  944. }
  945. }
  946. /* Expand first short spectrum */
  947. CConcealment_CalcBandEnergy(pConcealmentInfo->spectralCoefficient, /* spec_n */
  948. pSamplingRateInfo,
  949. EightShortSequence,
  950. CConcealment_Expand, /* !!! */
  951. sfbEnergyAct);
  952. } else {
  953. /* long---long---long interpolation */
  954. pIcsInfo->WindowShape = 0;
  955. pIcsInfo->WindowSequence = OnlyLongSequence;
  956. CConcealment_CalcBandEnergy(pConcealmentInfo->spectralCoefficient, /* spec_n */
  957. pSamplingRateInfo,
  958. OnlyLongSequence,
  959. CConcealment_NoExpand,
  960. sfbEnergyAct);
  961. }
  962. CConcealment_InterpolateBuffer(
  963. pSpectralCoefficient, /* spec_(n-1) */
  964. &pSpecScale[0],
  965. &specScaleAct,
  966. &pSpecScale[0],
  967. sfbEnergyPrev,
  968. sfbEnergyAct,
  969. scaleFactorBandsTotal,
  970. pSfbOffset);
  971. }
  972. }
  973. /* Noise substitution of sign of the output spectral coefficients */
  974. CConcealment_ApplyRandomSign (pConcealmentInfo->iRandomPhase,
  975. pSpectralCoefficient,
  976. samplesPerFrame);
  977. /* Increment random phase index to avoid repetition artifacts. */
  978. pConcealmentInfo->iRandomPhase = (pConcealmentInfo->iRandomPhase + 1) & (AAC_NF_NO_RANDOM_VAL - 1);
  979. }
  980. /* scale spectrum according to concealment state */
  981. switch (pConcealmentInfo->concealState)
  982. {
  983. case ConcealState_Single:
  984. appliedProcessing = 1;
  985. break;
  986. case ConcealState_FadeOut:
  987. {
  988. FDK_ASSERT(pConcealmentInfo->cntFadeFrames >= 0);
  989. FDK_ASSERT(pConcealmentInfo->cntFadeFrames < CONCEAL_MAX_NUM_FADE_FACTORS);
  990. FDK_ASSERT(pConcealmentInfo->cntFadeFrames < pConcealCommonData->numFadeOutFrames);
  991. /* restore frequency coefficients from buffer with a specific muting */
  992. FIXP_DBL *RESTRICT pOut = &pSpectralCoefficient[samplesPerFrame-1];
  993. FIXP_SGL fac = pConcealCommonData->fadeOutFactor[pConcealmentInfo->cntFadeFrames];
  994. for (i = samplesPerFrame; i != 0; i--) {
  995. *pOut = fMult(*pOut, fac);
  996. pOut--;
  997. }
  998. appliedProcessing = 1;
  999. }
  1000. break;
  1001. case ConcealState_FadeIn:
  1002. {
  1003. FDK_ASSERT(pConcealmentInfo->cntFadeFrames >= 0);
  1004. FDK_ASSERT(pConcealmentInfo->cntFadeFrames < CONCEAL_MAX_NUM_FADE_FACTORS);
  1005. FDK_ASSERT(pConcealmentInfo->cntFadeFrames < pConcealCommonData->numFadeInFrames);
  1006. /* attenuate signal to get a smooth fade-in */
  1007. FIXP_DBL *RESTRICT pOut = &pSpectralCoefficient[samplesPerFrame-1];
  1008. FIXP_SGL fac = pConcealCommonData->fadeInFactor[pConcealmentInfo->cntFadeFrames];
  1009. for (i = samplesPerFrame; i != 0; i--) {
  1010. *pOut = fMult(*pOut, fac);
  1011. pOut--;
  1012. }
  1013. appliedProcessing = 1;
  1014. }
  1015. break;
  1016. case ConcealState_Mute:
  1017. {
  1018. int fac = pConcealCommonData->comfortNoiseLevel;
  1019. /* set dummy window parameters */
  1020. pIcsInfo->Valid = 0; /* Trigger the generation of a consitent IcsInfo */
  1021. pIcsInfo->WindowShape = pConcealmentInfo->windowShape; /* Prevent an invalid WindowShape (required for F/T transform) */
  1022. pIcsInfo->WindowSequence = CConcealment_GetWinSeq(pConcealmentInfo->windowSequence);
  1023. pConcealmentInfo->windowSequence = pIcsInfo->WindowSequence; /* Store for next frame (spectrum in concealment buffer can't be used at all) */
  1024. /* mute spectral data */
  1025. FDKmemclear(pSpectralCoefficient, samplesPerFrame * sizeof(FIXP_DBL));
  1026. if (fac >= 0 && fac <= 61) {
  1027. /* insert comfort noise using PNS */
  1028. CConcealment_fakePnsData (
  1029. &pAacDecoderChannelInfo->data.aac.PnsData,
  1030. pIcsInfo,
  1031. pSamplingRateInfo,
  1032. pAacDecoderChannelInfo->specScale,
  1033. pAacDecoderChannelInfo->pDynData->aScaleFactor,
  1034. fac
  1035. );
  1036. CPns_Apply (
  1037. &pAacDecoderChannelInfo->data.aac.PnsData,
  1038. pIcsInfo,
  1039. pAacDecoderChannelInfo->pSpectralCoefficient,
  1040. pAacDecoderChannelInfo->specScale,
  1041. pAacDecoderChannelInfo->pDynData->aScaleFactor,
  1042. pSamplingRateInfo,
  1043. pAacDecoderChannelInfo->granuleLength,
  1044. 0 /* always apply to first channel */
  1045. );
  1046. }
  1047. appliedProcessing = 1;
  1048. }
  1049. break;
  1050. default:
  1051. /* nothing to do here */
  1052. break;
  1053. }
  1054. return appliedProcessing;
  1055. }
  1056. /*!
  1057. \brief Calculate the spectral energy
  1058. The function calculates band-wise the spectral energy. This is used for
  1059. frame interpolation.
  1060. \return none
  1061. */
  1062. static void
  1063. CConcealment_CalcBandEnergy (
  1064. FIXP_DBL *spectrum,
  1065. const SamplingRateInfo *pSamplingRateInfo,
  1066. const int blockType,
  1067. CConcealmentExpandType expandType,
  1068. int *sfbEnergy )
  1069. {
  1070. const SHORT *pSfbOffset;
  1071. int line, sfb, scaleFactorBandsTotal = 0;
  1072. /* In the following calculations, enAccu is initialized with LSB-value in order to avoid zero energy-level */
  1073. line = 0;
  1074. switch(blockType) {
  1075. case OnlyLongSequence:
  1076. case LongStartSequence:
  1077. case LongStopSequence:
  1078. if (expandType == CConcealment_NoExpand) {
  1079. /* standard long calculation */
  1080. scaleFactorBandsTotal = pSamplingRateInfo->NumberOfScaleFactorBands_Long;
  1081. pSfbOffset = pSamplingRateInfo->ScaleFactorBands_Long;
  1082. for (sfb = 0; sfb < scaleFactorBandsTotal; sfb++) {
  1083. FIXP_DBL enAccu = (FIXP_DBL)(LONG)1;
  1084. int sfbScale = (sizeof(LONG)<<3) - CntLeadingZeros(pSfbOffset[sfb+1] - pSfbOffset[sfb]) - 1;
  1085. /* scaling depends on sfb width. */
  1086. for ( ; line < pSfbOffset[sfb+1]; line++) {
  1087. enAccu += fPow2Div2(*(spectrum + line)) >> sfbScale;
  1088. }
  1089. *(sfbEnergy + sfb) = CntLeadingZeros(enAccu) - 1;
  1090. }
  1091. }
  1092. else {
  1093. /* compress long to short */
  1094. scaleFactorBandsTotal = pSamplingRateInfo->NumberOfScaleFactorBands_Short;
  1095. pSfbOffset = pSamplingRateInfo->ScaleFactorBands_Short;
  1096. for (sfb = 0; sfb < scaleFactorBandsTotal; sfb++) {
  1097. FIXP_DBL enAccu = (FIXP_DBL)(LONG)1;
  1098. int sfbScale = (sizeof(LONG)<<3) - CntLeadingZeros(pSfbOffset[sfb+1] - pSfbOffset[sfb]) - 1;
  1099. /* scaling depends on sfb width. */
  1100. for (; line < pSfbOffset[sfb+1] << 3; line++) {
  1101. enAccu += (enAccu + (fPow2Div2(*(spectrum + line)) >> sfbScale)) >> 3;
  1102. }
  1103. *(sfbEnergy + sfb) = CntLeadingZeros(enAccu) - 1;
  1104. }
  1105. }
  1106. break;
  1107. case EightShortSequence:
  1108. if (expandType == CConcealment_NoExpand) {
  1109. /* standard short calculation */
  1110. scaleFactorBandsTotal = pSamplingRateInfo->NumberOfScaleFactorBands_Short;
  1111. pSfbOffset = pSamplingRateInfo->ScaleFactorBands_Short;
  1112. for (sfb = 0; sfb < scaleFactorBandsTotal; sfb++) {
  1113. FIXP_DBL enAccu = (FIXP_DBL)(LONG)1;
  1114. int sfbScale = (sizeof(LONG)<<3) - CntLeadingZeros(pSfbOffset[sfb+1] - pSfbOffset[sfb]) - 1;
  1115. /* scaling depends on sfb width. */
  1116. for ( ; line < pSfbOffset[sfb+1]; line++) {
  1117. enAccu += fPow2Div2(*(spectrum + line)) >> sfbScale;
  1118. }
  1119. *(sfbEnergy + sfb) = CntLeadingZeros(enAccu) - 1;
  1120. }
  1121. }
  1122. else {
  1123. /* expand short to long spectrum */
  1124. scaleFactorBandsTotal = pSamplingRateInfo->NumberOfScaleFactorBands_Long;
  1125. pSfbOffset = pSamplingRateInfo->ScaleFactorBands_Long;
  1126. for (sfb = 0; sfb < scaleFactorBandsTotal; sfb++) {
  1127. FIXP_DBL enAccu = (FIXP_DBL)(LONG)1;
  1128. int sfbScale = (sizeof(LONG)<<3) - CntLeadingZeros(pSfbOffset[sfb+1] - pSfbOffset[sfb]) - 1;
  1129. /* scaling depends on sfb width. */
  1130. for ( ; line < pSfbOffset[sfb+1]; line++) {
  1131. enAccu += fPow2Div2(*(spectrum + (line >> 3))) >> sfbScale;
  1132. }
  1133. *(sfbEnergy + sfb) = CntLeadingZeros(enAccu) - 1;
  1134. }
  1135. }
  1136. break;
  1137. }
  1138. }
  1139. /*!
  1140. \brief Interpolate buffer
  1141. The function creates the interpolated spectral data according to the
  1142. energy of the last good frame and the current (good) frame.
  1143. \return none
  1144. */
  1145. static void
  1146. CConcealment_InterpolateBuffer (
  1147. FIXP_DBL *spectrum,
  1148. SHORT *pSpecScalePrv,
  1149. SHORT *pSpecScaleAct,
  1150. SHORT *pSpecScaleOut,
  1151. int *enPrv,
  1152. int *enAct,
  1153. int sfbCnt,
  1154. const SHORT *pSfbOffset )
  1155. {
  1156. int sfb, line = 0;
  1157. int fac_shift;
  1158. int fac_mod;
  1159. FIXP_DBL accu;
  1160. for (sfb = 0; sfb < sfbCnt; sfb++) {
  1161. fac_shift = enPrv[sfb] - enAct[sfb] + ((*pSpecScaleAct - *pSpecScalePrv) << 1);
  1162. fac_mod = fac_shift & 3;
  1163. fac_shift = (fac_shift >> 2) + 1;
  1164. fac_shift += *pSpecScalePrv - fixMax(*pSpecScalePrv, *pSpecScaleAct);
  1165. for (; line < pSfbOffset[sfb+1]; line++) {
  1166. accu = fMult(*(spectrum+line), facMod4Table[fac_mod]);
  1167. if (fac_shift < 0) {
  1168. accu >>= -fac_shift;
  1169. } else {
  1170. accu <<= fac_shift;
  1171. }
  1172. *(spectrum+line) = accu;
  1173. }
  1174. }
  1175. *pSpecScaleOut = fixMax(*pSpecScalePrv, *pSpecScaleAct);
  1176. }
  1177. static INT findEquiFadeFrame (
  1178. CConcealParams *pConcealCommonData,
  1179. INT actFadeIndex,
  1180. int direction )
  1181. {
  1182. FIXP_SGL *pFactor;
  1183. FIXP_SGL referenceVal;
  1184. FIXP_SGL minDiff = (FIXP_SGL)MAXVAL_SGL;
  1185. INT numFrames = 0;
  1186. INT nextFadeIndex = 0;
  1187. int i;
  1188. /* init depending on direction */
  1189. if (direction == 0) { /* FADE-OUT => FADE-IN */
  1190. numFrames = pConcealCommonData->numFadeInFrames;
  1191. referenceVal = pConcealCommonData->fadeOutFactor[actFadeIndex] >> 1;
  1192. pFactor = pConcealCommonData->fadeInFactor;
  1193. }
  1194. else { /* FADE-IN => FADE-OUT */
  1195. numFrames = pConcealCommonData->numFadeOutFrames;
  1196. referenceVal = pConcealCommonData->fadeInFactor[actFadeIndex] >> 1;
  1197. pFactor = pConcealCommonData->fadeOutFactor;
  1198. }
  1199. /* search for minimum difference */
  1200. for (i = 0; i < numFrames; i++) {
  1201. FIXP_SGL diff = fixp_abs((pFactor[i]>>1) - referenceVa…

Large files files are truncated, but you can click here to view the full file