pnsparam.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* -----------------------------------------------------------------------------------------------------------
  2. Software License for The Fraunhofer FDK AAC Codec Library for Android
  3. © Copyright 1995 - 2015 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. [email protected]
  61. ----------------------------------------------------------------------------------------------------------- */
  62. /******************************** MPEG Audio Encoder **************************
  63. Initial author: M.Lohwasser
  64. contents/description: PNS parameters depending on bitrate and bandwidth
  65. ******************************************************************************/
  66. #include "pnsparam.h"
  67. #include "psy_configuration.h"
  68. typedef struct {
  69. SHORT startFreq;
  70. /* Parameters for detection */
  71. FIXP_SGL refPower;
  72. FIXP_SGL refTonality;
  73. SHORT tnsGainThreshold; /* scaled by TNS_PREDGAIN_SCALE (=1000) */
  74. SHORT tnsPNSGainThreshold; /* scaled by TNS_PREDGAIN_SCALE (=1000) */
  75. FIXP_SGL gapFillThr;
  76. SHORT minSfbWidth;
  77. USHORT detectionAlgorithmFlags;
  78. } PNS_INFO_TAB;
  79. typedef struct {
  80. ULONG brFrom;
  81. ULONG brTo;
  82. UCHAR S16000;
  83. UCHAR S22050;
  84. UCHAR S24000;
  85. UCHAR S32000;
  86. UCHAR S44100;
  87. UCHAR S48000;
  88. } AUTO_PNS_TAB;
  89. static const AUTO_PNS_TAB levelTable_mono[]= {
  90. {0, 11999, 0, 1, 1, 1, 1, 1,},
  91. {12000, 19999, 0, 1, 1, 1, 1, 1,},
  92. {20000, 28999, 0, 2, 1, 1, 1, 1,},
  93. {29000, 40999, 0, 4, 4, 4, 2, 2,},
  94. {41000, 55999, 0, 9, 9, 7, 7, 7,},
  95. {56000, 61999, 0, 0, 0, 0, 9, 9,},
  96. {62000, 75999, 0, 0, 0, 0, 0, 0,},
  97. {76000, 92999, 0, 0, 0, 0, 0, 0,},
  98. {93000, 999999, 0, 0, 0, 0, 0, 0,},
  99. };
  100. static const AUTO_PNS_TAB levelTable_stereo[]= {
  101. {0, 11999, 0, 1, 1, 1, 1, 1,},
  102. {12000, 19999, 0, 3, 1, 1, 1, 1,},
  103. {20000, 28999, 0, 3, 3, 3, 2, 2,},
  104. {29000, 40999, 0, 7, 6, 6, 5, 5,},
  105. {41000, 55999, 0, 9, 9, 7, 7, 7,},
  106. {56000, 79999, 0, 0, 0, 0, 0, 0,},
  107. {80000, 99999, 0, 0, 0, 0, 0, 0,},
  108. {100000,999999, 0, 0, 0, 0, 0, 0,},
  109. };
  110. static const PNS_INFO_TAB pnsInfoTab[] = {
  111. /*0 pns off */
  112. /*1*/ { 4000, FL2FXCONST_SGL(0.04), FL2FXCONST_SGL(0.06), 1150, 1200, FL2FXCONST_SGL(0.02), 8,
  113. USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS /*| JUST_LONG_WINDOW*/ },
  114. /*2*/ { 4000, FL2FXCONST_SGL(0.04), FL2FXCONST_SGL(0.07), 1130, 1300, FL2FXCONST_SGL(0.05), 8,
  115. USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS /*| JUST_LONG_WINDOW*/ },
  116. /*3*/ { 4100, FL2FXCONST_SGL(0.04), FL2FXCONST_SGL(0.07), 1100, 1400, FL2FXCONST_SGL(0.10), 8,
  117. USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS /*| JUST_LONG_WINDOW*/ },
  118. /*4*/ { 4100, FL2FXCONST_SGL(0.03), FL2FXCONST_SGL(0.10), 1100, 1400, FL2FXCONST_SGL(0.15), 8,
  119. USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS /*| JUST_LONG_WINDOW*/ },
  120. /*5*/ { 4300, FL2FXCONST_SGL(0.03), FL2FXCONST_SGL(0.10), 1100, 1400, FL2FXCONST_SGL(0.15), 8,
  121. USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS | JUST_LONG_WINDOW },
  122. /*6*/ { 5000, FL2FXCONST_SGL(0.03), FL2FXCONST_SGL(0.10), 1100, 1400, FL2FXCONST_SGL(0.25), 8,
  123. USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS | JUST_LONG_WINDOW },
  124. /*7*/ { 5500, FL2FXCONST_SGL(0.03), FL2FXCONST_SGL(0.12), 1100, 1400, FL2FXCONST_SGL(0.35), 8,
  125. USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS | JUST_LONG_WINDOW },
  126. /*8*/ { 6000, FL2FXCONST_SGL(0.03), FL2FXCONST_SGL(0.12), 1080, 1400, FL2FXCONST_SGL(0.40), 8,
  127. USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS | JUST_LONG_WINDOW },
  128. /*9*/ { 6000, FL2FXCONST_SGL(0.03), FL2FXCONST_SGL(0.14), 1070, 1400, FL2FXCONST_SGL(0.45), 8,
  129. USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS | JUST_LONG_WINDOW },
  130. };
  131. static const AUTO_PNS_TAB levelTable_lowComplexity[]= {
  132. {0, 27999, 0, 0, 0, 0, 0, 0,},
  133. {28000, 31999, 0, 2, 2, 2, 2, 2,},
  134. {32000, 47999, 0, 3, 3, 3, 3, 3,},
  135. {48000, 48000, 0, 4, 4, 4, 4, 4,},
  136. {48001, 999999, 0, 0, 0, 0, 0, 0,},
  137. };
  138. /* conversion of old LC tuning tables to new (LD enc) structure (only entries which are actually used were converted) */
  139. static const PNS_INFO_TAB pnsInfoTab_lowComplexity[] = {
  140. /*0 pns off */
  141. /* DEFAULT parameter set */
  142. /*1*/ { 4100, FL2FXCONST_SGL(0.03), FL2FXCONST_SGL(0.16), 1100, 1400, FL2FXCONST_SGL(0.5), 16,
  143. USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS | JUST_LONG_WINDOW },
  144. /*2*/ { 4100, FL2FXCONST_SGL(0.05), FL2FXCONST_SGL(0.10), 1410, 1400, FL2FXCONST_SGL(0.5), 16,
  145. USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS | JUST_LONG_WINDOW },
  146. /*3*/ { 4100, FL2FXCONST_SGL(0.05), FL2FXCONST_SGL(0.10), 1100, 1400, FL2FXCONST_SGL(0.5), 16,
  147. USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS | JUST_LONG_WINDOW },
  148. /* LOWSUBST -> PNS is used less often than with DEFAULT parameter set (for br: 48000 - 79999) */
  149. /*4*/ { 4100, FL2FXCONST_SGL(0.20), FL2FXCONST_SGL(0.10), 1410, 1400, FL2FXCONST_SGL(0.5), 16,
  150. USE_POWER_DISTRIBUTION | USE_PSYCH_TONALITY | USE_TNS_GAIN_THR | USE_TNS_PNS | JUST_LONG_WINDOW },
  151. };
  152. /****************************************************************************
  153. function to look up used pns level
  154. ****************************************************************************/
  155. int FDKaacEnc_lookUpPnsUse (int bitRate, int sampleRate, int numChan, const int isLC) {
  156. int hUsePns=0, size, i;
  157. const AUTO_PNS_TAB *levelTable;
  158. if (isLC) {
  159. levelTable = &levelTable_lowComplexity[0];
  160. size = sizeof(levelTable_lowComplexity);
  161. } else
  162. { /* (E)LD */
  163. levelTable = (numChan > 1) ? &levelTable_stereo[0] : &levelTable_mono[0];
  164. size = (numChan > 1) ? sizeof(levelTable_stereo) : sizeof(levelTable_mono);
  165. }
  166. for(i = 0; i < (int) (size/sizeof(AUTO_PNS_TAB)); i++) {
  167. if(((ULONG)bitRate >= levelTable[i].brFrom) &&
  168. ((ULONG)bitRate <= levelTable[i].brTo) )
  169. break;
  170. }
  171. /* sanity check */
  172. if ((int)(sizeof(pnsInfoTab)/sizeof(PNS_INFO_TAB)) < i ) {
  173. return (PNS_TABLE_ERROR);
  174. }
  175. switch (sampleRate) {
  176. case 16000: hUsePns = levelTable[i].S16000; break;
  177. case 22050: hUsePns = levelTable[i].S22050; break;
  178. case 24000: hUsePns = levelTable[i].S24000; break;
  179. case 32000: hUsePns = levelTable[i].S32000; break;
  180. case 44100: hUsePns = levelTable[i].S44100; break;
  181. case 48000: hUsePns = levelTable[i].S48000; break;
  182. default:
  183. if (isLC) {
  184. hUsePns = levelTable[i].S48000;
  185. }
  186. break;
  187. }
  188. return (hUsePns);
  189. }
  190. /*****************************************************************************
  191. functionname: FDKaacEnc_GetPnsParam
  192. description: Gets PNS parameters depending on bitrate and bandwidth
  193. returns: error status
  194. input: Noiseparams struct, bitrate, sampling rate,
  195. number of sfb's, pointer to sfb offset
  196. output: PNS parameters
  197. *****************************************************************************/
  198. AAC_ENCODER_ERROR FDKaacEnc_GetPnsParam(NOISEPARAMS *np,
  199. INT bitRate,
  200. INT sampleRate,
  201. INT sfbCnt,
  202. const INT *sfbOffset,
  203. INT *usePns,
  204. INT numChan,
  205. const int isLC)
  206. {
  207. int i, hUsePns;
  208. const PNS_INFO_TAB *pnsInfo;
  209. if (isLC) {
  210. np->detectionAlgorithmFlags = IS_LOW_COMLEXITY;
  211. pnsInfo = pnsInfoTab_lowComplexity;
  212. }
  213. else
  214. {
  215. np->detectionAlgorithmFlags = 0;
  216. pnsInfo = pnsInfoTab;
  217. }
  218. if (*usePns<=0)
  219. return AAC_ENC_OK;
  220. /* new pns params */
  221. hUsePns = FDKaacEnc_lookUpPnsUse (bitRate, sampleRate, numChan, isLC);
  222. if (hUsePns == 0) {
  223. *usePns = 0;
  224. return AAC_ENC_OK;
  225. }
  226. if (hUsePns == PNS_TABLE_ERROR)
  227. return AAC_ENC_PNS_TABLE_ERROR;
  228. /* select correct row of tuning table */
  229. pnsInfo += hUsePns-1;
  230. np->startSfb = FDKaacEnc_FreqToBandWithRounding( pnsInfo->startFreq,
  231. sampleRate,
  232. sfbCnt,
  233. sfbOffset );
  234. np->detectionAlgorithmFlags |= pnsInfo->detectionAlgorithmFlags;
  235. np->refPower = FX_SGL2FX_DBL(pnsInfo->refPower);
  236. np->refTonality = FX_SGL2FX_DBL(pnsInfo->refTonality);
  237. np->tnsGainThreshold = pnsInfo->tnsGainThreshold;
  238. np->tnsPNSGainThreshold = pnsInfo->tnsPNSGainThreshold;
  239. np->minSfbWidth = pnsInfo->minSfbWidth;
  240. np->gapFillThr = (FIXP_SGL)pnsInfo->gapFillThr;
  241. /* assuming a constant dB/Hz slope in the signal's PSD curve,
  242. the detection threshold needs to be corrected for the width of the band */
  243. for ( i = 0; i < (sfbCnt-1); i++)
  244. {
  245. INT qtmp, sfbWidth;
  246. FIXP_DBL tmp;
  247. sfbWidth = sfbOffset[i+1]-sfbOffset[i];
  248. tmp = fPow(np->refPower, 0, sfbWidth, DFRACT_BITS-1-5, &qtmp);
  249. np->powDistPSDcurve[i] = (FIXP_SGL)((LONG)(scaleValue(tmp, qtmp) >> 16));
  250. }
  251. np->powDistPSDcurve[sfbCnt] = np->powDistPSDcurve[sfbCnt-1];
  252. return AAC_ENC_OK;
  253. }