psymodel.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168
  1. /*
  2. * psymodel.c
  3. *
  4. * Copyright (c) 1999-2000 Mark Taylor
  5. * Copyright (c) 2001-2002 Naoki Shibata
  6. * Copyright (c) 2000-2003 Takehiro Tominaga
  7. * Copyright (c) 2000-2012 Robert Hegemann
  8. * Copyright (c) 2000-2005 Gabriel Bouvigne
  9. * Copyright (c) 2000-2005 Alexander Leidinger
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Library General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2 of the License, or (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Library General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Library General Public
  22. * License along with this library; if not, write to the
  23. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. * Boston, MA 02111-1307, USA.
  25. */
  26. /* $Id: psymodel.c,v 1.216 2017/09/06 19:38:23 aleidinger Exp $ */
  27. /*
  28. PSYCHO ACOUSTICS
  29. This routine computes the psycho acoustics, delayed by one granule.
  30. Input: buffer of PCM data (1024 samples).
  31. This window should be centered over the 576 sample granule window.
  32. The routine will compute the psycho acoustics for
  33. this granule, but return the psycho acoustics computed
  34. for the *previous* granule. This is because the block
  35. type of the previous granule can only be determined
  36. after we have computed the psycho acoustics for the following
  37. granule.
  38. Output: maskings and energies for each scalefactor band.
  39. block type, PE, and some correlation measures.
  40. The PE is used by CBR modes to determine if extra bits
  41. from the bit reservoir should be used. The correlation
  42. measures are used to determine mid/side or regular stereo.
  43. */
  44. /*
  45. Notation:
  46. barks: a non-linear frequency scale. Mapping from frequency to
  47. barks is given by freq2bark()
  48. scalefactor bands: The spectrum (frequencies) are broken into
  49. SBMAX "scalefactor bands". Thes bands
  50. are determined by the MPEG ISO spec. In
  51. the noise shaping/quantization code, we allocate
  52. bits among the partition bands to achieve the
  53. best possible quality
  54. partition bands: The spectrum is also broken into about
  55. 64 "partition bands". Each partition
  56. band is about .34 barks wide. There are about 2-5
  57. partition bands for each scalefactor band.
  58. LAME computes all psycho acoustic information for each partition
  59. band. Then at the end of the computations, this information
  60. is mapped to scalefactor bands. The energy in each scalefactor
  61. band is taken as the sum of the energy in all partition bands
  62. which overlap the scalefactor band. The maskings can be computed
  63. in the same way (and thus represent the average masking in that band)
  64. or by taking the minmum value multiplied by the number of
  65. partition bands used (which represents a minimum masking in that band).
  66. */
  67. /*
  68. The general outline is as follows:
  69. 1. compute the energy in each partition band
  70. 2. compute the tonality in each partition band
  71. 3. compute the strength of each partion band "masker"
  72. 4. compute the masking (via the spreading function applied to each masker)
  73. 5. Modifications for mid/side masking.
  74. Each partition band is considiered a "masker". The strength
  75. of the i'th masker in band j is given by:
  76. s3(bark(i)-bark(j))*strength(i)
  77. The strength of the masker is a function of the energy and tonality.
  78. The more tonal, the less masking. LAME uses a simple linear formula
  79. (controlled by NMT and TMN) which says the strength is given by the
  80. energy divided by a linear function of the tonality.
  81. */
  82. /*
  83. s3() is the "spreading function". It is given by a formula
  84. determined via listening tests.
  85. The total masking in the j'th partition band is the sum over
  86. all maskings i. It is thus given by the convolution of
  87. the strength with s3(), the "spreading function."
  88. masking(j) = sum_over_i s3(i-j)*strength(i) = s3 o strength
  89. where "o" = convolution operator. s3 is given by a formula determined
  90. via listening tests. It is normalized so that s3 o 1 = 1.
  91. Note: instead of a simple convolution, LAME also has the
  92. option of using "additive masking"
  93. The most critical part is step 2, computing the tonality of each
  94. partition band. LAME has two tonality estimators. The first
  95. is based on the ISO spec, and measures how predictiable the
  96. signal is over time. The more predictable, the more tonal.
  97. The second measure is based on looking at the spectrum of
  98. a single granule. The more peaky the spectrum, the more
  99. tonal. By most indications, the latter approach is better.
  100. Finally, in step 5, the maskings for the mid and side
  101. channel are possibly increased. Under certain circumstances,
  102. noise in the mid & side channels is assumed to also
  103. be masked by strong maskers in the L or R channels.
  104. Other data computed by the psy-model:
  105. ms_ratio side-channel / mid-channel masking ratio (for previous granule)
  106. ms_ratio_next side-channel / mid-channel masking ratio for this granule
  107. percep_entropy[2] L and R values (prev granule) of PE - A measure of how
  108. much pre-echo is in the previous granule
  109. percep_entropy_MS[2] mid and side channel values (prev granule) of percep_entropy
  110. energy[4] L,R,M,S energy in each channel, prev granule
  111. blocktype_d[2] block type to use for previous granule
  112. */
  113. #ifdef HAVE_CONFIG_H
  114. # include <config.h>
  115. #endif
  116. #include <float.h>
  117. #include "lame.h"
  118. #include "machine.h"
  119. #include "encoder.h"
  120. #include "util.h"
  121. #include "psymodel.h"
  122. #include "lame_global_flags.h"
  123. #include "fft.h"
  124. #include "lame-analysis.h"
  125. #define NSFIRLEN 21
  126. #ifdef M_LN10
  127. #define LN_TO_LOG10 (M_LN10/10)
  128. #else
  129. #define LN_TO_LOG10 0.2302585093
  130. #endif
  131. /*
  132. L3psycho_anal. Compute psycho acoustics.
  133. Data returned to the calling program must be delayed by one
  134. granule.
  135. This is done in two places.
  136. If we do not need to know the blocktype, the copying
  137. can be done here at the top of the program: we copy the data for
  138. the last granule (computed during the last call) before it is
  139. overwritten with the new data. It looks like this:
  140. 0. static psymodel_data
  141. 1. calling_program_data = psymodel_data
  142. 2. compute psymodel_data
  143. For data which needs to know the blocktype, the copying must be
  144. done at the end of this loop, and the old values must be saved:
  145. 0. static psymodel_data_old
  146. 1. compute psymodel_data
  147. 2. compute possible block type of this granule
  148. 3. compute final block type of previous granule based on #2.
  149. 4. calling_program_data = psymodel_data_old
  150. 5. psymodel_data_old = psymodel_data
  151. */
  152. /* psycho_loudness_approx
  153. jd - 2001 mar 12
  154. in: energy - BLKSIZE/2 elements of frequency magnitudes ^ 2
  155. gfp - uses out_samplerate, ATHtype (also needed for ATHformula)
  156. returns: loudness^2 approximation, a positive value roughly tuned for a value
  157. of 1.0 for signals near clipping.
  158. notes: When calibrated, feeding this function binary white noise at sample
  159. values +32767 or -32768 should return values that approach 3.
  160. ATHformula is used to approximate an equal loudness curve.
  161. future: Data indicates that the shape of the equal loudness curve varies
  162. with intensity. This function might be improved by using an equal
  163. loudness curve shaped for typical playback levels (instead of the
  164. ATH, that is shaped for the threshold). A flexible realization might
  165. simply bend the existing ATH curve to achieve the desired shape.
  166. However, the potential gain may not be enough to justify an effort.
  167. */
  168. static FLOAT
  169. psycho_loudness_approx(FLOAT const *energy, FLOAT const *eql_w)
  170. {
  171. int i;
  172. FLOAT loudness_power;
  173. loudness_power = 0.0;
  174. /* apply weights to power in freq. bands */
  175. for (i = 0; i < BLKSIZE / 2; ++i)
  176. loudness_power += energy[i] * eql_w[i];
  177. loudness_power *= VO_SCALE;
  178. return loudness_power;
  179. }
  180. /* mask_add optimization */
  181. /* init the limit values used to avoid computing log in mask_add when it is not necessary */
  182. /* For example, with i = 10*log10(m2/m1)/10*16 (= log10(m2/m1)*16)
  183. *
  184. * abs(i)>8 is equivalent (as i is an integer) to
  185. * abs(i)>=9
  186. * i>=9 || i<=-9
  187. * equivalent to (as i is the biggest integer smaller than log10(m2/m1)*16
  188. * or the smallest integer bigger than log10(m2/m1)*16 depending on the sign of log10(m2/m1)*16)
  189. * log10(m2/m1)>=9/16 || log10(m2/m1)<=-9/16
  190. * exp10 is strictly increasing thus this is equivalent to
  191. * m2/m1 >= 10^(9/16) || m2/m1<=10^(-9/16) which are comparisons to constants
  192. */
  193. #define I1LIMIT 8 /* as in if(i>8) */
  194. #define I2LIMIT 23 /* as in if(i>24) -> changed 23 */
  195. #define MLIMIT 15 /* as in if(m<15) */
  196. /* pow(10, (I1LIMIT + 1) / 16.0); */
  197. static const FLOAT ma_max_i1 = 3.6517412725483771;
  198. /* pow(10, (I2LIMIT + 1) / 16.0); */
  199. static const FLOAT ma_max_i2 = 31.622776601683793;
  200. /* pow(10, (MLIMIT) / 10.0); */
  201. static const FLOAT ma_max_m = 31.622776601683793;
  202. /*This is the masking table:
  203. According to tonality, values are going from 0dB (TMN)
  204. to 9.3dB (NMT).
  205. After additive masking computation, 8dB are added, so
  206. final values are going from 8dB to 17.3dB
  207. */
  208. static const FLOAT tab[] = {
  209. 1.0 /*pow(10, -0) */ ,
  210. 0.79433 /*pow(10, -0.1) */ ,
  211. 0.63096 /*pow(10, -0.2) */ ,
  212. 0.63096 /*pow(10, -0.2) */ ,
  213. 0.63096 /*pow(10, -0.2) */ ,
  214. 0.63096 /*pow(10, -0.2) */ ,
  215. 0.63096 /*pow(10, -0.2) */ ,
  216. 0.25119 /*pow(10, -0.6) */ ,
  217. 0.11749 /*pow(10, -0.93) */
  218. };
  219. static const int tab_mask_add_delta[] = { 2, 2, 2, 1, 1, 1, 0, 0, -1 };
  220. #define STATIC_ASSERT_EQUAL_DIMENSION(A,B) enum{static_assert_##A=1/((dimension_of(A) == dimension_of(B))?1:0)}
  221. inline static int
  222. mask_add_delta(int i)
  223. {
  224. STATIC_ASSERT_EQUAL_DIMENSION(tab_mask_add_delta,tab);
  225. assert(i < (int)dimension_of(tab));
  226. return tab_mask_add_delta[i];
  227. }
  228. static void
  229. init_mask_add_max_values(void)
  230. {
  231. #ifndef NDEBUG
  232. FLOAT const _ma_max_i1 = pow(10, (I1LIMIT + 1) / 16.0);
  233. FLOAT const _ma_max_i2 = pow(10, (I2LIMIT + 1) / 16.0);
  234. FLOAT const _ma_max_m = pow(10, (MLIMIT) / 10.0);
  235. assert(fabs(ma_max_i1 - _ma_max_i1) <= FLT_EPSILON);
  236. assert(fabs(ma_max_i2 - _ma_max_i2) <= FLT_EPSILON);
  237. assert(fabs(ma_max_m - _ma_max_m ) <= FLT_EPSILON);
  238. #endif
  239. }
  240. /* addition of simultaneous masking Naoki Shibata 2000/7 */
  241. inline static FLOAT
  242. vbrpsy_mask_add(FLOAT m1, FLOAT m2, int b, int delta)
  243. {
  244. static const FLOAT table2[] = {
  245. 1.33352 * 1.33352, 1.35879 * 1.35879, 1.38454 * 1.38454, 1.39497 * 1.39497,
  246. 1.40548 * 1.40548, 1.3537 * 1.3537, 1.30382 * 1.30382, 1.22321 * 1.22321,
  247. 1.14758 * 1.14758,
  248. 1
  249. };
  250. FLOAT ratio;
  251. if (m1 < 0) {
  252. m1 = 0;
  253. }
  254. if (m2 < 0) {
  255. m2 = 0;
  256. }
  257. if (m1 <= 0) {
  258. return m2;
  259. }
  260. if (m2 <= 0) {
  261. return m1;
  262. }
  263. if (m2 > m1) {
  264. ratio = m2 / m1;
  265. }
  266. else {
  267. ratio = m1 / m2;
  268. }
  269. if (abs(b) <= delta) { /* approximately, 1 bark = 3 partitions */
  270. /* originally 'if(i > 8)' */
  271. if (ratio >= ma_max_i1) {
  272. return m1 + m2;
  273. }
  274. else {
  275. int i = (int) (FAST_LOG10_X(ratio, 16.0f));
  276. return (m1 + m2) * table2[i];
  277. }
  278. }
  279. if (ratio < ma_max_i2) {
  280. return m1 + m2;
  281. }
  282. if (m1 < m2) {
  283. m1 = m2;
  284. }
  285. return m1;
  286. }
  287. /* short block threshold calculation (part 2)
  288. partition band bo_s[sfb] is at the transition from scalefactor
  289. band sfb to the next one sfb+1; enn and thmm have to be split
  290. between them
  291. */
  292. static void
  293. convert_partition2scalefac(PsyConst_CB2SB_t const *const gd, FLOAT const *eb, FLOAT const *thr,
  294. FLOAT enn_out[], FLOAT thm_out[])
  295. {
  296. FLOAT enn, thmm;
  297. int sb, b, n = gd->n_sb;
  298. enn = thmm = 0.0f;
  299. for (sb = b = 0; sb < n; ++b, ++sb) {
  300. int const bo_sb = gd->bo[sb];
  301. int const npart = gd->npart;
  302. int const b_lim = bo_sb < npart ? bo_sb : npart;
  303. while (b < b_lim) {
  304. assert(eb[b] >= 0); /* iff failed, it may indicate some index error elsewhere */
  305. assert(thr[b] >= 0);
  306. enn += eb[b];
  307. thmm += thr[b];
  308. b++;
  309. }
  310. if (b >= npart) {
  311. enn_out[sb] = enn;
  312. thm_out[sb] = thmm;
  313. ++sb;
  314. break;
  315. }
  316. assert(eb[b] >= 0); /* iff failed, it may indicate some index error elsewhere */
  317. assert(thr[b] >= 0);
  318. {
  319. /* at transition sfb -> sfb+1 */
  320. FLOAT const w_curr = gd->bo_weight[sb];
  321. FLOAT const w_next = 1.0f - w_curr;
  322. enn += w_curr * eb[b];
  323. thmm += w_curr * thr[b];
  324. enn_out[sb] = enn;
  325. thm_out[sb] = thmm;
  326. enn = w_next * eb[b];
  327. thmm = w_next * thr[b];
  328. }
  329. }
  330. /* zero initialize the rest */
  331. for (; sb < n; ++sb) {
  332. enn_out[sb] = 0;
  333. thm_out[sb] = 0;
  334. }
  335. }
  336. static void
  337. convert_partition2scalefac_s(lame_internal_flags * gfc, FLOAT const *eb, FLOAT const *thr, int chn,
  338. int sblock)
  339. {
  340. PsyStateVar_t *const psv = &gfc->sv_psy;
  341. PsyConst_CB2SB_t const *const gds = &gfc->cd_psy->s;
  342. FLOAT enn[SBMAX_s], thm[SBMAX_s];
  343. int sb;
  344. convert_partition2scalefac(gds, eb, thr, enn, thm);
  345. for (sb = 0; sb < SBMAX_s; ++sb) {
  346. psv->en[chn].s[sb][sblock] = enn[sb];
  347. psv->thm[chn].s[sb][sblock] = thm[sb];
  348. }
  349. }
  350. /* longblock threshold calculation (part 2) */
  351. static void
  352. convert_partition2scalefac_l(lame_internal_flags * gfc, FLOAT const *eb, FLOAT const *thr, int chn)
  353. {
  354. PsyStateVar_t *const psv = &gfc->sv_psy;
  355. PsyConst_CB2SB_t const *const gdl = &gfc->cd_psy->l;
  356. FLOAT *enn = &psv->en[chn].l[0];
  357. FLOAT *thm = &psv->thm[chn].l[0];
  358. convert_partition2scalefac(gdl, eb, thr, enn, thm);
  359. }
  360. static void
  361. convert_partition2scalefac_l_to_s(lame_internal_flags * gfc, FLOAT const *eb, FLOAT const *thr,
  362. int chn)
  363. {
  364. PsyStateVar_t *const psv = &gfc->sv_psy;
  365. PsyConst_CB2SB_t const *const gds = &gfc->cd_psy->l_to_s;
  366. FLOAT enn[SBMAX_s], thm[SBMAX_s];
  367. int sb, sblock;
  368. convert_partition2scalefac(gds, eb, thr, enn, thm);
  369. for (sb = 0; sb < SBMAX_s; ++sb) {
  370. FLOAT const scale = 1. / 64.f;
  371. FLOAT const tmp_enn = enn[sb];
  372. FLOAT const tmp_thm = thm[sb] * scale;
  373. for (sblock = 0; sblock < 3; ++sblock) {
  374. psv->en[chn].s[sb][sblock] = tmp_enn;
  375. psv->thm[chn].s[sb][sblock] = tmp_thm;
  376. }
  377. }
  378. }
  379. static inline FLOAT
  380. NS_INTERP(FLOAT x, FLOAT y, FLOAT r)
  381. {
  382. /* was pow((x),(r))*pow((y),1-(r)) */
  383. if (r >= 1.0f)
  384. return x; /* 99.7% of the time */
  385. if (r <= 0.0f)
  386. return y;
  387. if (y > 0.0f)
  388. return powf(x / y, r) * y; /* rest of the time */
  389. return 0.0f; /* never happens */
  390. }
  391. static FLOAT
  392. pecalc_s(III_psy_ratio const *mr, FLOAT masking_lower)
  393. {
  394. FLOAT pe_s;
  395. static const FLOAT regcoef_s[] = {
  396. 11.8, /* these values are tuned only for 44.1kHz... */
  397. 13.6,
  398. 17.2,
  399. 32,
  400. 46.5,
  401. 51.3,
  402. 57.5,
  403. 67.1,
  404. 71.5,
  405. 84.6,
  406. 97.6,
  407. 130,
  408. /* 255.8 */
  409. };
  410. unsigned int sb, sblock;
  411. pe_s = 1236.28f / 4;
  412. for (sb = 0; sb < SBMAX_s - 1; sb++) {
  413. for (sblock = 0; sblock < 3; sblock++) {
  414. FLOAT const thm = mr->thm.s[sb][sblock];
  415. assert(sb < dimension_of(regcoef_s));
  416. if (thm > 0.0f) {
  417. FLOAT const x = thm * masking_lower;
  418. FLOAT const en = mr->en.s[sb][sblock];
  419. if (en > x) {
  420. if (en > x * 1e10f) {
  421. pe_s += regcoef_s[sb] * (10.0f * LOG10);
  422. }
  423. else {
  424. assert(x > 0);
  425. pe_s += regcoef_s[sb] * FAST_LOG10(en / x);
  426. }
  427. }
  428. }
  429. }
  430. }
  431. return pe_s;
  432. }
  433. static FLOAT
  434. pecalc_l(III_psy_ratio const *mr, FLOAT masking_lower)
  435. {
  436. FLOAT pe_l;
  437. static const FLOAT regcoef_l[] = {
  438. 6.8, /* these values are tuned only for 44.1kHz... */
  439. 5.8,
  440. 5.8,
  441. 6.4,
  442. 6.5,
  443. 9.9,
  444. 12.1,
  445. 14.4,
  446. 15,
  447. 18.9,
  448. 21.6,
  449. 26.9,
  450. 34.2,
  451. 40.2,
  452. 46.8,
  453. 56.5,
  454. 60.7,
  455. 73.9,
  456. 85.7,
  457. 93.4,
  458. 126.1,
  459. /* 241.3 */
  460. };
  461. unsigned int sb;
  462. pe_l = 1124.23f / 4;
  463. for (sb = 0; sb < SBMAX_l - 1; sb++) {
  464. FLOAT const thm = mr->thm.l[sb];
  465. assert(sb < dimension_of(regcoef_l));
  466. if (thm > 0.0f) {
  467. FLOAT const x = thm * masking_lower;
  468. FLOAT const en = mr->en.l[sb];
  469. if (en > x) {
  470. if (en > x * 1e10f) {
  471. pe_l += regcoef_l[sb] * (10.0f * LOG10);
  472. }
  473. else {
  474. assert(x > 0);
  475. pe_l += regcoef_l[sb] * FAST_LOG10(en / x);
  476. }
  477. }
  478. }
  479. }
  480. return pe_l;
  481. }
  482. static void
  483. calc_energy(PsyConst_CB2SB_t const *l, FLOAT const *fftenergy, FLOAT * eb, FLOAT * max, FLOAT * avg)
  484. {
  485. int b, j;
  486. for (b = j = 0; b < l->npart; ++b) {
  487. FLOAT ebb = 0, m = 0;
  488. int i;
  489. for (i = 0; i < l->numlines[b]; ++i, ++j) {
  490. FLOAT const el = fftenergy[j];
  491. assert(el >= 0);
  492. ebb += el;
  493. if (m < el)
  494. m = el;
  495. }
  496. eb[b] = ebb;
  497. max[b] = m;
  498. avg[b] = ebb * l->rnumlines[b];
  499. assert(l->rnumlines[b] >= 0);
  500. assert(ebb >= 0);
  501. assert(eb[b] >= 0);
  502. assert(max[b] >= 0);
  503. assert(avg[b] >= 0);
  504. }
  505. }
  506. static void
  507. calc_mask_index_l(lame_internal_flags const *gfc, FLOAT const *max,
  508. FLOAT const *avg, unsigned char *mask_idx)
  509. {
  510. PsyConst_CB2SB_t const *const gdl = &gfc->cd_psy->l;
  511. FLOAT m, a;
  512. int b, k;
  513. int const last_tab_entry = sizeof(tab) / sizeof(tab[0]) - 1;
  514. b = 0;
  515. a = avg[b] + avg[b + 1];
  516. assert(a >= 0);
  517. if (a > 0.0f) {
  518. m = max[b];
  519. if (m < max[b + 1])
  520. m = max[b + 1];
  521. assert((gdl->numlines[b] + gdl->numlines[b + 1] - 1) > 0);
  522. a = 20.0f * (m * 2.0f - a)
  523. / (a * (gdl->numlines[b] + gdl->numlines[b + 1] - 1));
  524. k = (int) a;
  525. if (k > last_tab_entry)
  526. k = last_tab_entry;
  527. mask_idx[b] = k;
  528. }
  529. else {
  530. mask_idx[b] = 0;
  531. }
  532. for (b = 1; b < gdl->npart - 1; b++) {
  533. a = avg[b - 1] + avg[b] + avg[b + 1];
  534. assert(a >= 0);
  535. if (a > 0.0f) {
  536. m = max[b - 1];
  537. if (m < max[b])
  538. m = max[b];
  539. if (m < max[b + 1])
  540. m = max[b + 1];
  541. assert((gdl->numlines[b - 1] + gdl->numlines[b] + gdl->numlines[b + 1] - 1) > 0);
  542. a = 20.0f * (m * 3.0f - a)
  543. / (a * (gdl->numlines[b - 1] + gdl->numlines[b] + gdl->numlines[b + 1] - 1));
  544. k = (int) a;
  545. if (k > last_tab_entry)
  546. k = last_tab_entry;
  547. mask_idx[b] = k;
  548. }
  549. else {
  550. mask_idx[b] = 0;
  551. }
  552. }
  553. assert(b > 0);
  554. assert(b == gdl->npart - 1);
  555. a = avg[b - 1] + avg[b];
  556. assert(a >= 0);
  557. if (a > 0.0f) {
  558. m = max[b - 1];
  559. if (m < max[b])
  560. m = max[b];
  561. assert((gdl->numlines[b - 1] + gdl->numlines[b] - 1) > 0);
  562. a = 20.0f * (m * 2.0f - a)
  563. / (a * (gdl->numlines[b - 1] + gdl->numlines[b] - 1));
  564. k = (int) a;
  565. if (k > last_tab_entry)
  566. k = last_tab_entry;
  567. mask_idx[b] = k;
  568. }
  569. else {
  570. mask_idx[b] = 0;
  571. }
  572. assert(b == (gdl->npart - 1));
  573. }
  574. static void
  575. vbrpsy_compute_fft_l(lame_internal_flags * gfc, const sample_t * const buffer[2], int chn,
  576. int gr_out, FLOAT fftenergy[HBLKSIZE], FLOAT(*wsamp_l)[BLKSIZE])
  577. {
  578. SessionConfig_t const *const cfg = &gfc->cfg;
  579. PsyStateVar_t *psv = &gfc->sv_psy;
  580. plotting_data *plt = cfg->analysis ? gfc->pinfo : 0;
  581. int j;
  582. if (chn < 2) {
  583. fft_long(gfc, *wsamp_l, chn, buffer);
  584. }
  585. else if (chn == 2) {
  586. FLOAT const sqrt2_half = SQRT2 * 0.5f;
  587. /* FFT data for mid and side channel is derived from L & R */
  588. for (j = BLKSIZE - 1; j >= 0; --j) {
  589. FLOAT const l = wsamp_l[0][j];
  590. FLOAT const r = wsamp_l[1][j];
  591. wsamp_l[0][j] = (l + r) * sqrt2_half;
  592. wsamp_l[1][j] = (l - r) * sqrt2_half;
  593. }
  594. }
  595. /*********************************************************************
  596. * compute energies
  597. *********************************************************************/
  598. fftenergy[0] = wsamp_l[0][0];
  599. fftenergy[0] *= fftenergy[0];
  600. for (j = BLKSIZE / 2 - 1; j >= 0; --j) {
  601. FLOAT const re = (*wsamp_l)[BLKSIZE / 2 - j];
  602. FLOAT const im = (*wsamp_l)[BLKSIZE / 2 + j];
  603. fftenergy[BLKSIZE / 2 - j] = (re * re + im * im) * 0.5f;
  604. }
  605. /* total energy */
  606. {
  607. FLOAT totalenergy = 0.0f;
  608. for (j = 11; j < HBLKSIZE; j++)
  609. totalenergy += fftenergy[j];
  610. psv->tot_ener[chn] = totalenergy;
  611. }
  612. if (plt) {
  613. for (j = 0; j < HBLKSIZE; j++) {
  614. plt->energy[gr_out][chn][j] = plt->energy_save[chn][j];
  615. plt->energy_save[chn][j] = fftenergy[j];
  616. }
  617. }
  618. }
  619. static void
  620. vbrpsy_compute_fft_s(lame_internal_flags const *gfc, const sample_t * const buffer[2], int chn,
  621. int sblock, FLOAT(*fftenergy_s)[HBLKSIZE_s], FLOAT(*wsamp_s)[3][BLKSIZE_s])
  622. {
  623. int j;
  624. if (sblock == 0 && chn < 2) {
  625. fft_short(gfc, *wsamp_s, chn, buffer);
  626. }
  627. if (chn == 2) {
  628. FLOAT const sqrt2_half = SQRT2 * 0.5f;
  629. /* FFT data for mid and side channel is derived from L & R */
  630. for (j = BLKSIZE_s - 1; j >= 0; --j) {
  631. FLOAT const l = wsamp_s[0][sblock][j];
  632. FLOAT const r = wsamp_s[1][sblock][j];
  633. wsamp_s[0][sblock][j] = (l + r) * sqrt2_half;
  634. wsamp_s[1][sblock][j] = (l - r) * sqrt2_half;
  635. }
  636. }
  637. /*********************************************************************
  638. * compute energies
  639. *********************************************************************/
  640. fftenergy_s[sblock][0] = (*wsamp_s)[sblock][0];
  641. fftenergy_s[sblock][0] *= fftenergy_s[sblock][0];
  642. for (j = BLKSIZE_s / 2 - 1; j >= 0; --j) {
  643. FLOAT const re = (*wsamp_s)[sblock][BLKSIZE_s / 2 - j];
  644. FLOAT const im = (*wsamp_s)[sblock][BLKSIZE_s / 2 + j];
  645. fftenergy_s[sblock][BLKSIZE_s / 2 - j] = (re * re + im * im) * 0.5f;
  646. }
  647. }
  648. /*********************************************************************
  649. * compute loudness approximation (used for ATH auto-level adjustment)
  650. *********************************************************************/
  651. static void
  652. vbrpsy_compute_loudness_approximation_l(lame_internal_flags * gfc, int gr_out, int chn,
  653. const FLOAT fftenergy[HBLKSIZE])
  654. {
  655. PsyStateVar_t *psv = &gfc->sv_psy;
  656. if (chn < 2) { /*no loudness for mid/side ch */
  657. gfc->ov_psy.loudness_sq[gr_out][chn] = psv->loudness_sq_save[chn];
  658. psv->loudness_sq_save[chn] = psycho_loudness_approx(fftenergy, gfc->ATH->eql_w);
  659. }
  660. }
  661. /**********************************************************************
  662. * Apply HPF of fs/4 to the input signal.
  663. * This is used for attack detection / handling.
  664. **********************************************************************/
  665. static void
  666. vbrpsy_attack_detection(lame_internal_flags * gfc, const sample_t * const buffer[2], int gr_out,
  667. III_psy_ratio masking_ratio[2][2], III_psy_ratio masking_MS_ratio[2][2],
  668. FLOAT energy[4], FLOAT sub_short_factor[4][3], int ns_attacks[4][4],
  669. int uselongblock[2])
  670. {
  671. FLOAT ns_hpfsmpl[2][576];
  672. SessionConfig_t const *const cfg = &gfc->cfg;
  673. PsyStateVar_t *const psv = &gfc->sv_psy;
  674. plotting_data *plt = cfg->analysis ? gfc->pinfo : 0;
  675. int const n_chn_out = cfg->channels_out;
  676. /* chn=2 and 3 = Mid and Side channels */
  677. int const n_chn_psy = (cfg->mode == JOINT_STEREO) ? 4 : n_chn_out;
  678. int chn, i, j;
  679. memset(&ns_hpfsmpl[0][0], 0, sizeof(ns_hpfsmpl));
  680. /* Don't copy the input buffer into a temporary buffer */
  681. /* unroll the loop 2 times */
  682. for (chn = 0; chn < n_chn_out; chn++) {
  683. static const FLOAT fircoef[] = {
  684. -8.65163e-18 * 2, -0.00851586 * 2, -6.74764e-18 * 2, 0.0209036 * 2,
  685. -3.36639e-17 * 2, -0.0438162 * 2, -1.54175e-17 * 2, 0.0931738 * 2,
  686. -5.52212e-17 * 2, -0.313819 * 2
  687. };
  688. /* apply high pass filter of fs/4 */
  689. const sample_t *const firbuf = &buffer[chn][576 - 350 - NSFIRLEN + 192];
  690. assert(dimension_of(fircoef) == ((NSFIRLEN - 1) / 2));
  691. for (i = 0; i < 576; i++) {
  692. FLOAT sum1, sum2;
  693. sum1 = firbuf[i + 10];
  694. sum2 = 0.0;
  695. for (j = 0; j < ((NSFIRLEN - 1) / 2) - 1; j += 2) {
  696. sum1 += fircoef[j] * (firbuf[i + j] + firbuf[i + NSFIRLEN - j]);
  697. sum2 += fircoef[j + 1] * (firbuf[i + j + 1] + firbuf[i + NSFIRLEN - j - 1]);
  698. }
  699. ns_hpfsmpl[chn][i] = sum1 + sum2;
  700. }
  701. masking_ratio[gr_out][chn].en = psv->en[chn];
  702. masking_ratio[gr_out][chn].thm = psv->thm[chn];
  703. if (n_chn_psy > 2) {
  704. /* MS maskings */
  705. /*percep_MS_entropy [chn-2] = gfc -> pe [chn]; */
  706. masking_MS_ratio[gr_out][chn].en = psv->en[chn + 2];
  707. masking_MS_ratio[gr_out][chn].thm = psv->thm[chn + 2];
  708. }
  709. }
  710. for (chn = 0; chn < n_chn_psy; chn++) {
  711. FLOAT attack_intensity[12];
  712. FLOAT en_subshort[12];
  713. FLOAT en_short[4] = { 0, 0, 0, 0 };
  714. FLOAT const *pf = ns_hpfsmpl[chn & 1];
  715. int ns_uselongblock = 1;
  716. if (chn == 2) {
  717. for (i = 0, j = 576; j > 0; ++i, --j) {
  718. FLOAT const l = ns_hpfsmpl[0][i];
  719. FLOAT const r = ns_hpfsmpl[1][i];
  720. ns_hpfsmpl[0][i] = l + r;
  721. ns_hpfsmpl[1][i] = l - r;
  722. }
  723. }
  724. /***************************************************************
  725. * determine the block type (window type)
  726. ***************************************************************/
  727. /* calculate energies of each sub-shortblocks */
  728. for (i = 0; i < 3; i++) {
  729. en_subshort[i] = psv->last_en_subshort[chn][i + 6];
  730. assert(psv->last_en_subshort[chn][i + 4] > 0);
  731. attack_intensity[i] = en_subshort[i] / psv->last_en_subshort[chn][i + 4];
  732. en_short[0] += en_subshort[i];
  733. }
  734. for (i = 0; i < 9; i++) {
  735. FLOAT const *const pfe = pf + 576 / 9;
  736. FLOAT p = 1.;
  737. for (; pf < pfe; pf++)
  738. if (p < fabs(*pf))
  739. p = fabs(*pf);
  740. psv->last_en_subshort[chn][i] = en_subshort[i + 3] = p;
  741. en_short[1 + i / 3] += p;
  742. if (p > en_subshort[i + 3 - 2]) {
  743. assert(en_subshort[i + 3 - 2] > 0);
  744. p = p / en_subshort[i + 3 - 2];
  745. }
  746. else if (en_subshort[i + 3 - 2] > p * 10.0f) {
  747. assert(p > 0);
  748. p = en_subshort[i + 3 - 2] / (p * 10.0f);
  749. }
  750. else {
  751. p = 0.0;
  752. }
  753. attack_intensity[i + 3] = p;
  754. }
  755. /* pulse like signal detection for fatboy.wav and so on */
  756. for (i = 0; i < 3; ++i) {
  757. FLOAT const enn =
  758. en_subshort[i * 3 + 3] + en_subshort[i * 3 + 4] + en_subshort[i * 3 + 5];
  759. FLOAT factor = 1.f;
  760. if (en_subshort[i * 3 + 5] * 6 < enn) {
  761. factor *= 0.5f;
  762. if (en_subshort[i * 3 + 4] * 6 < enn) {
  763. factor *= 0.5f;
  764. }
  765. }
  766. sub_short_factor[chn][i] = factor;
  767. }
  768. if (plt) {
  769. FLOAT x = attack_intensity[0];
  770. for (i = 1; i < 12; i++) {
  771. if (x < attack_intensity[i]) {
  772. x = attack_intensity[i];
  773. }
  774. }
  775. plt->ers[gr_out][chn] = plt->ers_save[chn];
  776. plt->ers_save[chn] = x;
  777. }
  778. /* compare energies between sub-shortblocks */
  779. {
  780. FLOAT x = gfc->cd_psy->attack_threshold[chn];
  781. for (i = 0; i < 12; i++) {
  782. if (ns_attacks[chn][i / 3] == 0) {
  783. if (attack_intensity[i] > x) {
  784. ns_attacks[chn][i / 3] = (i % 3) + 1;
  785. }
  786. }
  787. }
  788. }
  789. /* should have energy change between short blocks, in order to avoid periodic signals */
  790. /* Good samples to show the effect are Trumpet test songs */
  791. /* GB: tuned (1) to avoid too many short blocks for test sample TRUMPET */
  792. /* RH: tuned (2) to let enough short blocks through for test sample FSOL and SNAPS */
  793. for (i = 1; i < 4; i++) {
  794. FLOAT const u = en_short[i - 1];
  795. FLOAT const v = en_short[i];
  796. FLOAT const m = Max(u, v);
  797. if (m < 40000) { /* (2) */
  798. if (u < 1.7f * v && v < 1.7f * u) { /* (1) */
  799. if (i == 1 && ns_attacks[chn][0] <= ns_attacks[chn][i]) {
  800. ns_attacks[chn][0] = 0;
  801. }
  802. ns_attacks[chn][i] = 0;
  803. }
  804. }
  805. }
  806. if (ns_attacks[chn][0] <= psv->last_attacks[chn]) {
  807. ns_attacks[chn][0] = 0;
  808. }
  809. if (psv->last_attacks[chn] == 3 ||
  810. ns_attacks[chn][0] + ns_attacks[chn][1] + ns_attacks[chn][2] + ns_attacks[chn][3]) {
  811. ns_uselongblock = 0;
  812. if (ns_attacks[chn][1] && ns_attacks[chn][0]) {
  813. ns_attacks[chn][1] = 0;
  814. }
  815. if (ns_attacks[chn][2] && ns_attacks[chn][1]) {
  816. ns_attacks[chn][2] = 0;
  817. }
  818. if (ns_attacks[chn][3] && ns_attacks[chn][2]) {
  819. ns_attacks[chn][3] = 0;
  820. }
  821. }
  822. if (chn < 2) {
  823. uselongblock[chn] = ns_uselongblock;
  824. }
  825. else {
  826. if (ns_uselongblock == 0) {
  827. uselongblock[0] = uselongblock[1] = 0;
  828. }
  829. }
  830. /* there is a one granule delay. Copy maskings computed last call
  831. * into masking_ratio to return to calling program.
  832. */
  833. energy[chn] = psv->tot_ener[chn];
  834. }
  835. }
  836. static void
  837. vbrpsy_skip_masking_s(lame_internal_flags * gfc, int chn, int sblock)
  838. {
  839. if (sblock == 0) {
  840. FLOAT *nbs2 = &gfc->sv_psy.nb_s2[chn][0];
  841. FLOAT *nbs1 = &gfc->sv_psy.nb_s1[chn][0];
  842. int const n = gfc->cd_psy->s.npart;
  843. int b;
  844. for (b = 0; b < n; b++) {
  845. nbs2[b] = nbs1[b];
  846. }
  847. }
  848. }
  849. static void
  850. vbrpsy_calc_mask_index_s(lame_internal_flags const *gfc, FLOAT const *max,
  851. FLOAT const *avg, unsigned char *mask_idx)
  852. {
  853. PsyConst_CB2SB_t const *const gds = &gfc->cd_psy->s;
  854. FLOAT m, a;
  855. int b, k;
  856. int const last_tab_entry = dimension_of(tab) - 1;
  857. b = 0;
  858. a = avg[b] + avg[b + 1];
  859. assert(a >= 0);
  860. if (a > 0.0f) {
  861. m = max[b];
  862. if (m < max[b + 1])
  863. m = max[b + 1];
  864. assert((gds->numlines[b] + gds->numlines[b + 1] - 1) > 0);
  865. a = 20.0f * (m * 2.0f - a)
  866. / (a * (gds->numlines[b] + gds->numlines[b + 1] - 1));
  867. k = (int) a;
  868. if (k > last_tab_entry)
  869. k = last_tab_entry;
  870. mask_idx[b] = k;
  871. }
  872. else {
  873. mask_idx[b] = 0;
  874. }
  875. for (b = 1; b < gds->npart - 1; b++) {
  876. a = avg[b - 1] + avg[b] + avg[b + 1];
  877. assert(b + 1 < gds->npart);
  878. assert(a >= 0);
  879. if (a > 0.0) {
  880. m = max[b - 1];
  881. if (m < max[b])
  882. m = max[b];
  883. if (m < max[b + 1])
  884. m = max[b + 1];
  885. assert((gds->numlines[b - 1] + gds->numlines[b] + gds->numlines[b + 1] - 1) > 0);
  886. a = 20.0f * (m * 3.0f - a)
  887. / (a * (gds->numlines[b - 1] + gds->numlines[b] + gds->numlines[b + 1] - 1));
  888. k = (int) a;
  889. if (k > last_tab_entry)
  890. k = last_tab_entry;
  891. mask_idx[b] = k;
  892. }
  893. else {
  894. mask_idx[b] = 0;
  895. }
  896. }
  897. assert(b > 0);
  898. assert(b == gds->npart - 1);
  899. a = avg[b - 1] + avg[b];
  900. assert(a >= 0);
  901. if (a > 0.0f) {
  902. m = max[b - 1];
  903. if (m < max[b])
  904. m = max[b];
  905. assert((gds->numlines[b - 1] + gds->numlines[b] - 1) > 0);
  906. a = 20.0f * (m * 2.0f - a)
  907. / (a * (gds->numlines[b - 1] + gds->numlines[b] - 1));
  908. k = (int) a;
  909. if (k > last_tab_entry)
  910. k = last_tab_entry;
  911. mask_idx[b] = k;
  912. }
  913. else {
  914. mask_idx[b] = 0;
  915. }
  916. assert(b == (gds->npart - 1));
  917. }
  918. static void
  919. vbrpsy_compute_masking_s(lame_internal_flags * gfc, const FLOAT(*fftenergy_s)[HBLKSIZE_s],
  920. FLOAT * eb, FLOAT * thr, int chn, int sblock)
  921. {
  922. PsyStateVar_t *const psv = &gfc->sv_psy;
  923. PsyConst_CB2SB_t const *const gds = &gfc->cd_psy->s;
  924. FLOAT max[CBANDS], avg[CBANDS];
  925. int i, j, b;
  926. unsigned char mask_idx_s[CBANDS];
  927. memset(max, 0, sizeof(max));
  928. memset(avg, 0, sizeof(avg));
  929. for (b = j = 0; b < gds->npart; ++b) {
  930. FLOAT ebb = 0, m = 0;
  931. int const n = gds->numlines[b];
  932. for (i = 0; i < n; ++i, ++j) {
  933. FLOAT const el = fftenergy_s[sblock][j];
  934. ebb += el;
  935. if (m < el)
  936. m = el;
  937. }
  938. eb[b] = ebb;
  939. assert(ebb >= 0);
  940. max[b] = m;
  941. assert(n > 0);
  942. avg[b] = ebb * gds->rnumlines[b];
  943. assert(avg[b] >= 0);
  944. }
  945. assert(b == gds->npart);
  946. assert(j == 129);
  947. vbrpsy_calc_mask_index_s(gfc, max, avg, mask_idx_s);
  948. for (j = b = 0; b < gds->npart; b++) {
  949. int kk = gds->s3ind[b][0];
  950. int const last = gds->s3ind[b][1];
  951. int const delta = mask_add_delta(mask_idx_s[b]);
  952. int dd, dd_n;
  953. FLOAT x, ecb, avg_mask;
  954. FLOAT const masking_lower = gds->masking_lower[b] * gfc->sv_qnt.masking_lower;
  955. dd = mask_idx_s[kk];
  956. dd_n = 1;
  957. ecb = gds->s3[j] * eb[kk] * tab[mask_idx_s[kk]];
  958. ++j, ++kk;
  959. while (kk <= last) {
  960. dd += mask_idx_s[kk];
  961. dd_n += 1;
  962. x = gds->s3[j] * eb[kk] * tab[mask_idx_s[kk]];
  963. ecb = vbrpsy_mask_add(ecb, x, kk - b, delta);
  964. ++j, ++kk;
  965. }
  966. dd = (1 + 2 * dd) / (2 * dd_n);
  967. avg_mask = tab[dd] * 0.5f;
  968. ecb *= avg_mask;
  969. #if 0 /* we can do PRE ECHO control now here, or do it later */
  970. if (psv->blocktype_old[chn & 0x01] == SHORT_TYPE) {
  971. /* limit calculated threshold by even older granule */
  972. FLOAT const t1 = rpelev_s * psv->nb_s1[chn][b];
  973. FLOAT const t2 = rpelev2_s * psv->nb_s2[chn][b];
  974. FLOAT const tm = (t2 > 0) ? Min(ecb, t2) : ecb;
  975. thr[b] = (t1 > 0) ? NS_INTERP(Min(tm, t1), ecb, 0.6) : ecb;
  976. }
  977. else {
  978. /* limit calculated threshold by older granule */
  979. FLOAT const t1 = rpelev_s * psv->nb_s1[chn][b];
  980. thr[b] = (t1 > 0) ? NS_INTERP(Min(ecb, t1), ecb, 0.6) : ecb;
  981. }
  982. #else /* we do it later */
  983. thr[b] = ecb;
  984. #endif
  985. psv->nb_s2[chn][b] = psv->nb_s1[chn][b];
  986. psv->nb_s1[chn][b] = ecb;
  987. {
  988. /* if THR exceeds EB, the quantization routines will take the difference
  989. * from other bands. in case of strong tonal samples (tonaltest.wav)
  990. * this leads to heavy distortions. that's why we limit THR here.
  991. */
  992. x = max[b];
  993. x *= gds->minval[b];
  994. x *= avg_mask;
  995. if (thr[b] > x) {
  996. thr[b] = x;
  997. }
  998. }
  999. if (masking_lower > 1) {
  1000. thr[b] *= masking_lower;
  1001. }
  1002. if (thr[b] > eb[b]) {
  1003. thr[b] = eb[b];
  1004. }
  1005. if (masking_lower < 1) {
  1006. thr[b] *= masking_lower;
  1007. }
  1008. assert(thr[b] >= 0);
  1009. }
  1010. for (; b < CBANDS; ++b) {
  1011. eb[b] = 0;
  1012. thr[b] = 0;
  1013. }
  1014. }
  1015. static void
  1016. vbrpsy_compute_masking_l(lame_internal_flags * gfc, const FLOAT fftenergy[HBLKSIZE],
  1017. FLOAT eb_l[CBANDS], FLOAT thr[CBANDS], int chn)
  1018. {
  1019. PsyStateVar_t *const psv = &gfc->sv_psy;
  1020. PsyConst_CB2SB_t const *const gdl = &gfc->cd_psy->l;
  1021. FLOAT max[CBANDS], avg[CBANDS];
  1022. unsigned char mask_idx_l[CBANDS + 2];
  1023. int k, b;
  1024. /*********************************************************************
  1025. * Calculate the energy and the tonality of each partition.
  1026. *********************************************************************/
  1027. calc_energy(gdl, fftenergy, eb_l, max, avg);
  1028. calc_mask_index_l(gfc, max, avg, mask_idx_l);
  1029. /*********************************************************************
  1030. * convolve the partitioned energy and unpredictability
  1031. * with the spreading function, s3_l[b][k]
  1032. ********************************************************************/
  1033. k = 0;
  1034. for (b = 0; b < gdl->npart; b++) {
  1035. FLOAT x, ecb, avg_mask, t;
  1036. FLOAT const masking_lower = gdl->masking_lower[b] * gfc->sv_qnt.masking_lower;
  1037. /* convolve the partitioned energy with the spreading function */
  1038. int kk = gdl->s3ind[b][0];
  1039. int const last = gdl->s3ind[b][1];
  1040. int const delta = mask_add_delta(mask_idx_l[b]);
  1041. int dd = 0, dd_n = 0;
  1042. dd = mask_idx_l[kk];
  1043. dd_n += 1;
  1044. ecb = gdl->s3[k] * eb_l[kk] * tab[mask_idx_l[kk]];
  1045. ++k, ++kk;
  1046. while (kk <= last) {
  1047. dd += mask_idx_l[kk];
  1048. dd_n += 1;
  1049. x = gdl->s3[k] * eb_l[kk] * tab[mask_idx_l[kk]];
  1050. t = vbrpsy_mask_add(ecb, x, kk - b, delta);
  1051. #if 0
  1052. ecb += eb_l[kk];
  1053. if (ecb > t) {
  1054. ecb = t;
  1055. }
  1056. #else
  1057. ecb = t;
  1058. #endif
  1059. ++k, ++kk;
  1060. }
  1061. dd = (1 + 2 * dd) / (2 * dd_n);
  1062. avg_mask = tab[dd] * 0.5f;
  1063. ecb *= avg_mask;
  1064. /**** long block pre-echo control ****/
  1065. /* dont use long block pre-echo control if previous granule was
  1066. * a short block. This is to avoid the situation:
  1067. * frame0: quiet (very low masking)
  1068. * frame1: surge (triggers short blocks)
  1069. * frame2: regular frame. looks like pre-echo when compared to
  1070. * frame0, but all pre-echo was in frame1.
  1071. */
  1072. /* chn=0,1 L and R channels
  1073. chn=2,3 S and M channels.
  1074. */
  1075. if (psv->blocktype_old[chn & 0x01] == SHORT_TYPE) {
  1076. FLOAT const ecb_limit = rpelev * psv->nb_l1[chn][b];
  1077. if (ecb_limit > 0) {
  1078. thr[b] = Min(ecb, ecb_limit);
  1079. }
  1080. else {
  1081. /* Robert 071209:
  1082. Because we don't calculate long block psy when we know a granule
  1083. should be of short blocks, we don't have any clue how the granule
  1084. before would have looked like as a long block. So we have to guess
  1085. a little bit for this END_TYPE block.
  1086. Most of the time we get away with this sloppyness. (fingers crossed :)
  1087. The speed increase is worth it.
  1088. */
  1089. thr[b] = Min(ecb, eb_l[b] * NS_PREECHO_ATT2);
  1090. }
  1091. }
  1092. else {
  1093. FLOAT ecb_limit_2 = rpelev2 * psv->nb_l2[chn][b];
  1094. FLOAT ecb_limit_1 = rpelev * psv->nb_l1[chn][b];
  1095. FLOAT ecb_limit;
  1096. if (ecb_limit_2 <= 0) {
  1097. ecb_limit_2 = ecb;
  1098. }
  1099. if (ecb_limit_1 <= 0) {
  1100. ecb_limit_1 = ecb;
  1101. }
  1102. if (psv->blocktype_old[chn & 0x01] == NORM_TYPE) {
  1103. ecb_limit = Min(ecb_limit_1, ecb_limit_2);
  1104. }
  1105. else {
  1106. ecb_limit = ecb_limit_1;
  1107. }
  1108. thr[b] = Min(ecb, ecb_limit);
  1109. }
  1110. psv->nb_l2[chn][b] = psv->nb_l1[chn][b];
  1111. psv->nb_l1[chn][b] = ecb;
  1112. {
  1113. /* if THR exceeds EB, the quantization routines will take the difference
  1114. * from other bands. in case of strong tonal samples (tonaltest.wav)
  1115. * this leads to heavy distortions. that's why we limit THR here.
  1116. */
  1117. x = max[b];
  1118. x *= gdl->minval[b];
  1119. x *= avg_mask;
  1120. if (thr[b] > x) {
  1121. thr[b] = x;
  1122. }
  1123. }
  1124. if (masking_lower > 1) {
  1125. thr[b] *= masking_lower;
  1126. }
  1127. if (thr[b] > eb_l[b]) {
  1128. thr[b] = eb_l[b];
  1129. }
  1130. if (masking_lower < 1) {
  1131. thr[b] *= masking_lower;
  1132. }
  1133. assert(thr[b] >= 0);
  1134. }
  1135. for (; b < CBANDS; ++b) {
  1136. eb_l[b] = 0;
  1137. thr[b] = 0;
  1138. }
  1139. }
  1140. static void
  1141. vbrpsy_compute_block_type(SessionConfig_t const *cfg, int *uselongblock)
  1142. {
  1143. int chn;
  1144. if (cfg->short_blocks == short_block_coupled
  1145. /* force both channels to use the same block type */
  1146. /* this is necessary if the frame is to be encoded in ms_stereo. */
  1147. /* But even without ms_stereo, FhG does this */
  1148. && !(uselongblock[0] && uselongblock[1]))
  1149. uselongblock[0] = uselongblock[1] = 0;
  1150. for (chn = 0; chn < cfg->channels_out; chn++) {
  1151. /* disable short blocks */
  1152. if (cfg->short_blocks == short_block_dispensed) {
  1153. uselongblock[chn] = 1;
  1154. }
  1155. if (cfg->short_blocks == short_block_forced) {
  1156. uselongblock[chn] = 0;
  1157. }
  1158. }
  1159. }
  1160. static void
  1161. vbrpsy_apply_block_type(PsyStateVar_t * psv, int nch, int const *uselongblock, int *blocktype_d)
  1162. {
  1163. int chn;
  1164. /* update the blocktype of the previous granule, since it depends on what
  1165. * happend in this granule */
  1166. for (chn = 0; chn < nch; chn++) {
  1167. int blocktype = NORM_TYPE;
  1168. /* disable short blocks */
  1169. if (uselongblock[chn]) {
  1170. /* no attack : use long blocks */
  1171. assert(psv->blocktype_old[chn] != START_TYPE);
  1172. if (psv->blocktype_old[chn] == SHORT_TYPE)
  1173. blocktype = STOP_TYPE;
  1174. }
  1175. else {
  1176. /* attack : use short blocks */
  1177. blocktype = SHORT_TYPE;
  1178. if (psv->blocktype_old[chn] == NORM_TYPE) {
  1179. psv->blocktype_old[chn] = START_TYPE;
  1180. }
  1181. if (psv->blocktype_old[chn] == STOP_TYPE)
  1182. psv->blocktype_old[chn] = SHORT_TYPE;
  1183. }
  1184. blocktype_d[chn] = psv->blocktype_old[chn]; /* value returned to calling program */
  1185. psv->blocktype_old[chn] = blocktype; /* save for next call to l3psy_anal */
  1186. }
  1187. }
  1188. /***************************************************************
  1189. * compute M/S thresholds from Johnston & Ferreira 1992 ICASSP paper
  1190. ***************************************************************/
  1191. static void
  1192. vbrpsy_compute_MS_thresholds(const FLOAT eb[4][CBANDS], FLOAT thr[4][CBANDS],
  1193. const FLOAT cb_mld[CBANDS], const FLOAT ath_cb[CBANDS], FLOAT athlower,
  1194. FLOAT msfix, int n)
  1195. {
  1196. FLOAT const msfix2 = msfix * 2.f;
  1197. FLOAT rside, rmid;
  1198. int b;
  1199. for (b = 0; b < n; ++b) {
  1200. FLOAT const ebM = eb[2][b];
  1201. FLOAT const ebS = eb[3][b];
  1202. FLOAT const thmL = thr[0][b];
  1203. FLOAT const thmR = thr[1][b];
  1204. FLOAT thmM = thr[2][b];
  1205. FLOAT thmS = thr[3][b];
  1206. /* use this fix if L & R masking differs by 2db or less */
  1207. /* if db = 10*log10(x2/x1) < 2 */
  1208. /* if (x2 < 1.58*x1) { */
  1209. if (thmL <= 1.58f * thmR && thmR <= 1.58f * thmL) {
  1210. FLOAT const mld_m = cb_mld[b] * ebS;
  1211. FLOAT const mld_s = cb_mld[b] * ebM;
  1212. FLOAT const tmp_m = Min(thmS, mld_m);
  1213. FLOAT const tmp_s = Min(thmM, mld_s);
  1214. rmid = Max(thmM, tmp_m);
  1215. rside = Max(thmS, tmp_s);
  1216. }
  1217. else {
  1218. rmid = thmM;
  1219. rside = thmS;
  1220. }
  1221. if (msfix > 0.f) {
  1222. /***************************************************************/
  1223. /* Adjust M/S maskings if user set "msfix" */
  1224. /***************************************************************/
  1225. /* Naoki Shibata 2000 */
  1226. FLOAT thmLR, thmMS;
  1227. FLOAT const ath = ath_cb[b] * athlower;
  1228. FLOAT const tmp_l = Max(thmL, ath);
  1229. FLOAT const tmp_r = Max(thmR, ath);
  1230. thmLR = Min(tmp_l, tmp_r);
  1231. thmM = Max(rmid, ath);
  1232. thmS = Max(rside, ath);
  1233. thmMS = thmM + thmS;
  1234. if (thmMS > 0.f && (thmLR * msfix2) < thmMS) {
  1235. FLOAT const f = thmLR * msfix2 / thmMS;
  1236. thmM *= f;
  1237. thmS *= f;
  1238. assert(thmMS > 0.f);
  1239. }
  1240. rmid = Min(thmM, rmid);
  1241. rside = Min(thmS, rside);
  1242. }
  1243. if (rmid > ebM) {
  1244. rmid = ebM;
  1245. }
  1246. if (rside > ebS) {
  1247. rside = ebS;
  1248. }
  1249. thr[2][b] = rmid;
  1250. thr[3][b] = rside;
  1251. }
  1252. }
  1253. /*
  1254. * NOTE: the bitrate reduction from the inter-channel masking effect is low
  1255. * compared to the chance of getting annyoing artefacts. L3psycho_anal_vbr does
  1256. * not use this feature. (Robert 071216)
  1257. */
  1258. int
  1259. L3psycho_anal_vbr(lame_internal_flags * gfc,
  1260. const sample_t * const buffer[2], int gr_out,
  1261. III_psy_ratio masking_ratio[2][2],
  1262. III_psy_ratio masking_MS_ratio[2][2],
  1263. FLOAT percep_entropy[2], FLOAT percep_MS_entropy[2],
  1264. FLOAT energy[4], int blocktype_d[2])
  1265. {
  1266. SessionConfig_t const *const cfg = &gfc->cfg;
  1267. PsyStateVar_t *const psv = &gfc->sv_psy;
  1268. PsyConst_CB2SB_t const *const gdl = &gfc->cd_psy->l;
  1269. PsyConst_CB2SB_t const *const gds = &gfc->cd_psy->s;
  1270. plotting_data *plt = cfg->analysis ? gfc->pinfo : 0;
  1271. III_psy_xmin last_thm[4];
  1272. /* fft and energy calculation */
  1273. FLOAT(*wsamp_l)[BLKSIZE];
  1274. FLOAT(*wsamp_s)[3][BLKSIZE_s];
  1275. FLOAT fftenergy[HBLKSIZE];
  1276. FLOAT fftenergy_s[3][HBLKSIZE_s];
  1277. FLOAT wsamp_L[2][BLKSIZE];
  1278. FLOAT wsamp_S[2][3][BLKSIZE_s];
  1279. FLOAT eb[4][CBANDS], thr[4][CBANDS];
  1280. FLOAT sub_short_factor[4][3];
  1281. FLOAT thmm;
  1282. FLOAT const pcfact = 0.6f;
  1283. FLOAT const ath_factor =
  1284. (cfg->msfix > 0.f) ? (cfg->ATH_offset_factor * gfc->ATH->adjust_factor) : 1.f;
  1285. const FLOAT(*const_eb)[CBANDS] = (const FLOAT(*)[CBANDS]) eb;
  1286. const FLOAT(*const_fftenergy_s)[HBLKSIZE_s] = (const FLOAT(*)[HBLKSIZE_s]) fftenergy_s;
  1287. /* block type */
  1288. int ns_attacks[4][4] = { {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} };
  1289. int uselongblock[2];
  1290. /* usual variables like loop indices, etc.. */
  1291. int chn, sb, sblock;
  1292. /* chn=2 and 3 = Mid and Side channels */
  1293. int const n_chn_psy = (cfg->mode == JOINT_STEREO) ? 4 : cfg->channels_out;
  1294. memcpy(&last_thm[0], &psv->thm[0], sizeof(last_thm));
  1295. vbrpsy_attack_detection(gfc, buffer, gr_out, masking_ratio, masking_MS_ratio, energy,
  1296. sub_short_factor, ns_attacks, uselongblock);
  1297. vbrpsy_compute_block_type(cfg, uselongblock);
  1298. /* LONG BLOCK CASE */
  1299. {
  1300. for (chn = 0; chn < n_chn_psy; chn++) {
  1301. int const ch01 = chn & 0x01;
  1302. wsamp_l = wsamp_L + ch01;
  1303. vbrpsy_compute_fft_l(gfc, buffer, chn, gr_out, fftenergy, wsamp_l);
  1304. vbrpsy_compute_loudness_approximation_l(gfc, gr_out, chn, fftenergy);
  1305. vbrpsy_compute_masking_l(gfc, fftenergy, eb[chn], thr[chn], chn);
  1306. }
  1307. if (cfg->mode == JOINT_STEREO) {
  1308. if ((uselongblock[0] + uselongblock[1]) == 2) {
  1309. vbrpsy_compute_MS_thresholds(const_eb, thr, gdl->mld_cb, gfc->ATH->cb_l,
  1310. ath_factor, cfg->msfix, gdl->npart);
  1311. }
  1312. }
  1313. /* TODO: apply adaptive ATH masking here ?? */
  1314. for (chn = 0; chn < n_chn_psy; chn++) {
  1315. convert_partition2scalefac_l(gfc, eb[chn], thr[chn], chn);
  1316. convert_partition2scalefac_l_to_s(gfc, eb[chn], thr[chn], chn);
  1317. }
  1318. }
  1319. /* SHORT BLOCKS CASE */
  1320. {
  1321. int const force_short_block_calc = gfc->cd_psy->force_short_block_calc;
  1322. for (sblock = 0; sblock < 3; sblock++) {
  1323. for (chn = 0; chn < n_chn_psy; ++chn) {
  1324. int const ch01 = chn & 0x01;
  1325. if (uselongblock[ch01] && !force_short_block_calc) {
  1326. vbrpsy_skip_masking_s(gfc, chn, sblock);
  1327. }
  1328. else {
  1329. /* compute masking thresholds for short blocks */
  1330. wsamp_s = wsamp_S + ch01;
  1331. vbrpsy_compute_fft_s(gfc, buffer, chn, sblock, fftenergy_s, wsamp_s);
  1332. vbrpsy_compute_masking_s(gfc, const_fftenergy_s, eb[chn], thr[chn], chn,
  1333. sblock);
  1334. }
  1335. }
  1336. if (cfg->mode == JOINT_STEREO) {
  1337. if ((uselongblock[0] + uselongblock[1]) == 0) {
  1338. vbrpsy_compute_MS_thresholds(const_eb, thr, gds->mld_cb, gfc->ATH->cb_s,
  1339. ath_factor, cfg->msfix, gds->npart);
  1340. }
  1341. }
  1342. /* TODO: apply adaptive ATH masking here ?? */
  1343. for (chn = 0; chn < n_chn_psy; ++chn) {
  1344. int const ch01 = chn & 0x01;
  1345. if (!uselongblock[ch01] || force_short_block_calc) {
  1346. convert_partition2scalefac_s(gfc, eb[chn], thr[chn], chn, sblock);
  1347. }
  1348. }
  1349. }
  1350. /**** short block pre-echo control ****/
  1351. for (chn = 0; chn < n_chn_psy; chn++) {
  1352. for (sb = 0; sb < SBMAX_s; sb++) {
  1353. FLOAT new_thmm[3], prev_thm, t1, t2;
  1354. for (sblock = 0; sblock < 3; sblock++) {
  1355. thmm = psv->thm[chn].s[sb][sblock];
  1356. thmm *= NS_PREECHO_ATT0;
  1357. t1 = t2 = thmm;
  1358. if (sblock > 0) {
  1359. prev_thm = new_thmm[sblock - 1];
  1360. }
  1361. else {
  1362. prev_thm = last_thm[chn].s[sb][2];
  1363. }
  1364. if (ns_attacks[chn][sblock] >= 2 || ns_attacks[chn][sblock + 1] == 1) {
  1365. t1 = NS_INTERP(prev_thm, thmm, NS_PREECHO_ATT1 * pcfact);
  1366. }
  1367. thmm = Min(t1, thmm);
  1368. if (ns_attacks[chn][sblock] == 1) {
  1369. t2 = NS_INTERP(prev_thm, thmm, NS_PREECHO_ATT2 * pcfact);
  1370. }
  1371. else if ((sblock == 0 && psv->last_attacks[chn] == 3)
  1372. || (sblock > 0 && ns_attacks[chn][sblock - 1] == 3)) { /* 2nd preceeding block */
  1373. switch (sblock) {
  1374. case 0:
  1375. prev_thm = last_thm[chn].s[sb][1];
  1376. break;
  1377. case 1:
  1378. prev_thm = last_thm[chn].s[sb][2];
  1379. break;
  1380. case 2:
  1381. prev_thm = new_thmm[0];
  1382. break;
  1383. }
  1384. t2 = NS_INTERP(prev_thm, thmm, NS_PREECHO_ATT2 * pcfact);
  1385. }
  1386. thmm = Min(t1, thmm);
  1387. thmm = Min(t2, thmm);
  1388. /* pulse like signal detection for fatboy.wav and so on */
  1389. thmm *= sub_short_factor[chn][sblock];
  1390. new_thmm[sblock] = thmm;
  1391. }
  1392. for (sblock = 0; sblock < 3; sblock++) {
  1393. psv->thm[chn].s[sb][sblock] = new_thmm[sblock];
  1394. }
  1395. }
  1396. }
  1397. }
  1398. for (chn = 0; chn < n_chn_psy; chn++) {
  1399. psv->last_attacks[chn] = ns_attacks[chn][2];
  1400. }
  1401. /***************************************************************
  1402. * determine final block type
  1403. ***************************************************************/
  1404. vbrpsy_apply_block_type(psv, cfg->channels_out, uselongblock, blocktype_d);
  1405. /*********************************************************************
  1406. * compute the value of PE to return ... no delay and advance
  1407. *********************************************************************/
  1408. for (chn = 0; chn < n_chn_psy; chn++) {
  1409. FLOAT *ppe;
  1410. int type;
  1411. III_psy_ratio const *mr;
  1412. if (chn > 1) {
  1413. ppe = percep_MS_entropy - 2;
  1414. type = NORM_TYPE;
  1415. if (blocktype_d[0] == SHORT_TYPE || blocktype_d[1] == SHORT_TYPE)
  1416. type = SHORT_TYPE;
  1417. mr = &masking_MS_ratio[gr_out][chn - 2];
  1418. }
  1419. else {
  1420. ppe = percep_entropy;
  1421. type = blocktype_d[chn];
  1422. mr = &masking_ratio[gr_out][chn];
  1423. }
  1424. if (type == SHORT_TYPE) {
  1425. ppe[chn] = pecalc_s(mr, gfc->sv_qnt.masking_lower);
  1426. }
  1427. else {
  1428. ppe[chn] = pecalc_l(mr, gfc->sv_qnt.masking_lower);
  1429. }
  1430. if (plt) {
  1431. plt->pe[gr_out][chn] = ppe[chn];
  1432. }
  1433. }
  1434. return 0;
  1435. }
  1436. /*
  1437. * The spreading function. Values returned in units of energy
  1438. */
  1439. static FLOAT
  1440. s3_func(FLOAT bark)
  1441. {
  1442. FLOAT tempx, x, tempy, temp;
  1443. tempx = bark;
  1444. if (tempx >= 0)
  1445. tempx *= 3;
  1446. else
  1447. tempx *= 1.5;
  1448. if (tempx >= 0.5 && tempx <= 2.5) {
  1449. temp = tempx - 0.5;
  1450. x = 8.0 * (temp * temp - 2.0 * temp);
  1451. }
  1452. else
  1453. x = 0.0;
  1454. tempx += 0.474;
  1455. tempy = 15.811389 + 7.5 * tempx - 17.5 * sqrt(1.0 + tempx * tempx);
  1456. if (tempy <= -60.0)
  1457. return 0.0;
  1458. tempx = exp((x + tempy) * LN_TO_LOG10);
  1459. /* Normalization. The spreading function should be normalized so that:
  1460. +inf
  1461. /
  1462. | s3 [ bark ] d(bark) = 1
  1463. /
  1464. -inf
  1465. */
  1466. tempx /= .6609193;
  1467. return tempx;
  1468. }
  1469. #if 0
  1470. static FLOAT
  1471. norm_s3_func(void)
  1472. {
  1473. double lim_a = 0, lim_b = 0;
  1474. double x = 0, l, h;
  1475. for (x = 0; s3_func(x) > 1e-20; x -= 1);
  1476. l = x;
  1477. h = 0;
  1478. while (fabs(h - l) > 1e-12) {
  1479. x = (h + l) / 2;
  1480. if (s3_func(x) > 0) {
  1481. h = x;
  1482. }
  1483. else {
  1484. l = x;
  1485. }
  1486. }
  1487. lim_a = l;
  1488. for (x = 0; s3_func(x) > 1e-20; x += 1);
  1489. l = 0;
  1490. h = x;
  1491. while (fabs(h - l) > 1e-12) {
  1492. x = (h + l) / 2;
  1493. if (s3_func(x) > 0) {
  1494. l = x;
  1495. }
  1496. else {
  1497. h = x;
  1498. }
  1499. }
  1500. lim_b = h;
  1501. {
  1502. double sum = 0;
  1503. int const m = 1000;
  1504. int i;
  1505. for (i = 0; i <= m; ++i) {
  1506. double x = lim_a + i * (lim_b - lim_a) / m;
  1507. double y = s3_func(x);
  1508. sum += y;
  1509. }
  1510. {
  1511. double norm = (m + 1) / (sum * (lim_b - lim_a));
  1512. /*printf( "norm = %lf\n",norm); */
  1513. return norm;
  1514. }
  1515. }
  1516. }
  1517. #endif
  1518. static FLOAT
  1519. stereo_demask(double f)
  1520. {
  1521. /* setup stereo demasking thresholds */
  1522. /* formula reverse enginerred from plot in paper */
  1523. double arg = freq2bark(f);
  1524. arg = (Min(arg, 15.5) / 15.5);
  1525. return pow(10.0, 1.25 * (1 - cos(PI * arg)) - 2.5);
  1526. }
  1527. static void
  1528. init_numline(PsyConst_CB2SB_t * gd, FLOAT sfreq, int fft_size,
  1529. int mdct_size, int sbmax, int const *scalepos)
  1530. {
  1531. FLOAT b_frq[CBANDS + 1];
  1532. FLOAT const mdct_freq_frac = sfreq / (2.0f * mdct_size);
  1533. FLOAT const deltafreq = fft_size / (2.0f * mdct_size);
  1534. int partition[HBLKSIZE] = { 0 };
  1535. int i, j, ni;
  1536. int sfb;
  1537. sfreq /= fft_size;
  1538. j = 0;
  1539. ni = 0;
  1540. /* compute numlines, the number of spectral lines in each partition band */
  1541. /* each partition band should be about DELBARK wide. */
  1542. for (i = 0; i < CBANDS; i++) {
  1543. FLOAT bark1;
  1544. int j2, nl;
  1545. bark1 = freq2bark(sfreq * j);
  1546. b_frq[i] = sfreq * j;
  1547. for (j2 = j; freq2bark(sfreq * j2) - bark1 < DELBARK && j2 <= fft_size / 2; j2++);
  1548. nl = j2 - j;
  1549. gd->numlines[i] = nl;
  1550. gd->rnumlines[i] = (nl > 0) ? (1.0f / nl) : 0;
  1551. ni = i + 1;
  1552. while (j < j2) {
  1553. assert(j < HBLKSIZE);
  1554. partition[j++] = i;
  1555. }
  1556. if (j > fft_size / 2) {
  1557. j = fft_size / 2;
  1558. ++i;
  1559. break;
  1560. }
  1561. }
  1562. assert(i < CBANDS);
  1563. b_frq[i] = sfreq * j;
  1564. gd->n_sb = sbmax;
  1565. gd->npart = ni;
  1566. {
  1567. j = 0;
  1568. for (i = 0; i < gd->npart; i++) {
  1569. int const nl = gd->numlines[i];
  1570. FLOAT const freq = sfreq * (j + nl / 2);
  1571. gd->mld_cb[i] = stereo_demask(freq);
  1572. j += nl;
  1573. }
  1574. for (; i < CBANDS; ++i) {
  1575. gd->mld_cb[i] = 1;
  1576. }
  1577. }
  1578. for (sfb = 0; sfb < sbmax; sfb++) {
  1579. int i1, i2, bo;
  1580. int start = scalepos[sfb];
  1581. int end = scalepos[sfb + 1];
  1582. i1 = floor(.5 + deltafreq * (start - .5));
  1583. if (i1 < 0)
  1584. i1 = 0;
  1585. i2 = floor(.5 + deltafreq * (end - .5));
  1586. if (i2 > fft_size / 2)
  1587. i2 = fft_size / 2;
  1588. bo = partition[i2];
  1589. gd->bm[sfb] = (partition[i1] + partition[i2]) / 2;
  1590. gd->bo[sfb] = bo;
  1591. /* calculate how much of this band belongs to current scalefactor band */
  1592. {
  1593. FLOAT const f_tmp = mdct_freq_frac * end;
  1594. FLOAT bo_w = (f_tmp - b_frq[bo]) / (b_frq[bo + 1] - b_frq[bo]);
  1595. if (bo_w < 0) {
  1596. bo_w = 0;
  1597. }
  1598. else {
  1599. if (bo_w > 1) {
  1600. bo_w = 1;
  1601. }
  1602. }
  1603. gd->bo_weight[sfb] = bo_w;
  1604. }
  1605. gd->mld[sfb] = stereo_demask(mdct_freq_frac * start);
  1606. }
  1607. }
  1608. static void
  1609. compute_bark_values(PsyConst_CB2SB_t const *gd, FLOAT sfreq, int fft_size,
  1610. FLOAT * bval, FLOAT * bval_width)
  1611. {
  1612. /* compute bark values of each critical band */
  1613. int k, j = 0, ni = gd->npart;
  1614. sfreq /= fft_size;
  1615. for (k = 0; k < ni; k++) {
  1616. int const w = gd->numlines[k];
  1617. FLOAT bark1, bark2;
  1618. bark1 = freq2bark(sfreq * (j));
  1619. bark2 = freq2bark(sfreq * (j + w - 1));
  1620. bval[k] = .5 * (bark1 + bark2);
  1621. bark1 = freq2bark(sfreq * (j - .5));
  1622. bark2 = freq2bark(sfreq * (j + w - .5));
  1623. bval_width[k] = bark2 - bark1;
  1624. j += w;
  1625. }
  1626. }
  1627. static int
  1628. init_s3_values(FLOAT ** p, int (*s3ind)[2], int npart,
  1629. FLOAT const *bval, FLOAT const *bval_width, FLOAT const *norm)
  1630. {
  1631. FLOAT s3[CBANDS][CBANDS];
  1632. /* The s3 array is not linear in the bark scale.
  1633. * bval[x] should be used to get the bark value.
  1634. */
  1635. int i, j, k;
  1636. int numberOfNoneZero = 0;
  1637. memset(&s3[0][0], 0, sizeof(s3));
  1638. /* s[i][j], the value of the spreading function,
  1639. * centered at band j (masker), for band i (maskee)
  1640. *
  1641. * i.e.: sum over j to spread into signal barkval=i
  1642. * NOTE: i and j are used opposite as in the ISO docs
  1643. */
  1644. for (i = 0; i < npart; i++) {
  1645. for (j = 0; j < npart; j++) {
  1646. FLOAT v = s3_func(bval[i] - bval[j]) * bval_width[j];
  1647. s3[i][j] = v * norm[i];
  1648. }
  1649. }
  1650. for (i = 0; i < npart; i++) {
  1651. for (j = 0; j < npart; j++) {
  1652. if (s3[i][j] > 0.0f)
  1653. break;
  1654. }
  1655. s3ind[i][0] = j;
  1656. for (j = npart - 1; j > 0; j--) {
  1657. if (s3[i][j] > 0.0f)
  1658. break;
  1659. }
  1660. s3ind[i][1] = j;
  1661. numberOfNoneZero += (s3ind[i][1] - s3ind[i][0] + 1);
  1662. }
  1663. *p = lame_calloc(FLOAT, numberOfNoneZero);
  1664. if (!*p)
  1665. return -1;
  1666. k = 0;
  1667. for (i = 0; i < npart; i++)
  1668. for (j = s3ind[i][0]; j <= s3ind[i][1]; j++)
  1669. (*p)[k++] = s3[i][j];
  1670. return 0;
  1671. }
  1672. int
  1673. psymodel_init(lame_global_flags const *gfp)
  1674. {
  1675. lame_internal_flags *const gfc = gfp->internal_flags;
  1676. SessionConfig_t *const cfg = &gfc->cfg;
  1677. PsyStateVar_t *const psv = &gfc->sv_psy;
  1678. PsyConst_t *gd;
  1679. int i, j, b, sb, k;
  1680. FLOAT bvl_a = 13, bvl_b = 24;
  1681. FLOAT snr_l_a = 0, snr_l_b = 0;
  1682. FLOAT snr_s_a = -8.25, snr_s_b = -4.5;
  1683. FLOAT bval[CBANDS];
  1684. FLOAT bval_width[CBANDS];
  1685. FLOAT norm[CBANDS];
  1686. FLOAT const sfreq = cfg->samplerate_out;
  1687. FLOAT xav = 10, xbv = 12;
  1688. FLOAT const minval_low = (0.f - cfg->minval);
  1689. if (gfc->cd_psy != 0) {
  1690. return 0;
  1691. }
  1692. memset(norm, 0, sizeof(norm));
  1693. gd = lame_calloc(PsyConst_t, 1);
  1694. gfc->cd_psy = gd;
  1695. gd->force_short_block_calc = gfp->experimentalZ;
  1696. psv->blocktype_old[0] = psv->blocktype_old[1] = NORM_TYPE; /* the vbr header is long blocks */
  1697. for (i = 0; i < 4; ++i) {
  1698. for (j = 0; j < CBANDS; ++j) {
  1699. psv->nb_l1[i][j] = 1e20;
  1700. psv->nb_l2[i][j] = 1e20;
  1701. psv->nb_s1[i][j] = psv->nb_s2[i][j] = 1.0;
  1702. }
  1703. for (sb = 0; sb < SBMAX_l; sb++) {
  1704. psv->en[i].l[sb] = 1e20;
  1705. psv->thm[i].l[sb] = 1e20;
  1706. }
  1707. for (j = 0; j < 3; ++j) {
  1708. for (sb = 0; sb < SBMAX_s; sb++) {
  1709. psv->en[i].s[sb][j] = 1e20;
  1710. psv->thm[i].s[sb][j] = 1e20;
  1711. }
  1712. psv->last_attacks[i] = 0;
  1713. }
  1714. for (j = 0; j < 9; j++)
  1715. psv->last_en_subshort[i][j] = 10.;
  1716. }
  1717. /* init. for loudness approx. -jd 2001 mar 27 */
  1718. psv->loudness_sq_save[0] = psv->loudness_sq_save[1] = 0.0;
  1719. /*************************************************************************
  1720. * now compute the psychoacoustic model specific constants
  1721. ************************************************************************/
  1722. /* compute numlines, bo, bm, bval, bval_width, mld */
  1723. init_numline(&gd->l, sfreq, BLKSIZE, 576, SBMAX_l, gfc->scalefac_band.l);
  1724. assert(gd->l.npart < CBANDS);
  1725. compute_bark_values(&gd->l, sfreq, BLKSIZE, bval, bval_width);
  1726. /* compute the spreading function */
  1727. for (i = 0; i < gd->l.npart; i++) {
  1728. double snr = snr_l_a;
  1729. if (bval[i] >= bvl_a) {
  1730. snr = snr_l_b * (bval[i] - bvl_a) / (bvl_b - bvl_a)
  1731. + snr_l_a * (bvl_b - bval[i]) / (bvl_b - bvl_a);
  1732. }
  1733. norm[i] = pow(10.0, snr / 10.0);
  1734. }
  1735. i = init_s3_values(&gd->l.s3, gd->l.s3ind, gd->l.npart, bval, bval_width, norm);
  1736. if (i)
  1737. return i;
  1738. /* compute long block specific values, ATH and MINVAL */
  1739. j = 0;
  1740. for (i = 0; i < gd->l.npart; i++) {
  1741. double x;
  1742. /* ATH */
  1743. x = FLOAT_MAX;
  1744. for (k = 0; k < gd->l.numlines[i]; k++, j++) {
  1745. FLOAT const freq = sfreq * j / (1000.0 * BLKSIZE);
  1746. FLOAT level;
  1747. /* freq = Min(.1,freq); *//* ATH below 100 Hz constant, not further climbing */
  1748. level = ATHformula(cfg, freq * 1000) - 20; /* scale to FFT units; returned value is in dB */
  1749. level = pow(10., 0.1 * level); /* convert from dB -> energy */
  1750. level *= gd->l.numlines[i];
  1751. if (x > level)
  1752. x = level;
  1753. }
  1754. gfc->ATH->cb_l[i] = x;
  1755. /* MINVAL.
  1756. For low freq, the strength of the masking is limited by minval
  1757. this is an ISO MPEG1 thing, dont know if it is really needed */
  1758. /* FIXME: it does work to reduce low-freq problems in S53-Wind-Sax
  1759. and lead-voice samples, but introduces some 3 kbps bit bloat too.
  1760. TODO: Further refinement of the shape of this hack.
  1761. */
  1762. x = 20.0 * (bval[i] / xav - 1.0);
  1763. if (x > 6) {
  1764. x = 30;
  1765. }
  1766. if (x < minval_low) {
  1767. x = minval_low;
  1768. }
  1769. if (cfg->samplerate_out < 44000) {
  1770. x = 30;
  1771. }
  1772. x -= 8.;
  1773. gd->l.minval[i] = pow(10.0, x / 10.) * gd->l.numlines[i];
  1774. }
  1775. /************************************************************************
  1776. * do the same things for short blocks
  1777. ************************************************************************/
  1778. init_numline(&gd->s, sfreq, BLKSIZE_s, 192, SBMAX_s, gfc->scalefac_band.s);
  1779. assert(gd->s.npart < CBANDS);
  1780. compute_bark_values(&gd->s, sfreq, BLKSIZE_s, bval, bval_width);
  1781. /* SNR formula. short block is normalized by SNR. is it still right ? */
  1782. j = 0;
  1783. for (i = 0; i < gd->s.npart; i++) {
  1784. double x;
  1785. double snr = snr_s_a;
  1786. if (bval[i] >= bvl_a) {
  1787. snr = snr_s_b * (bval[i] - bvl_a) / (bvl_b - bvl_a)
  1788. + snr_s_a * (bvl_b - bval[i]) / (bvl_b - bvl_a);
  1789. }
  1790. norm[i] = pow(10.0, snr / 10.0);
  1791. /* ATH */
  1792. x = FLOAT_MAX;
  1793. for (k = 0; k < gd->s.numlines[i]; k++, j++) {
  1794. FLOAT const freq = sfreq * j / (1000.0 * BLKSIZE_s);
  1795. FLOAT level;
  1796. /* freq = Min(.1,freq); *//* ATH below 100 Hz constant, not further climbing */
  1797. level = ATHformula(cfg, freq * 1000) - 20; /* scale to FFT units; returned value is in dB */
  1798. level = pow(10., 0.1 * level); /* convert from dB -> energy */
  1799. level *= gd->s.numlines[i];
  1800. if (x > level)
  1801. x = level;
  1802. }
  1803. gfc->ATH->cb_s[i] = x;
  1804. /* MINVAL.
  1805. For low freq, the strength of the masking is limited by minval
  1806. this is an ISO MPEG1 thing, dont know if it is really needed */
  1807. x = 7.0 * (bval[i] / xbv - 1.0);
  1808. if (bval[i] > xbv) {
  1809. x *= 1 + log(1 + x) * 3.1;
  1810. }
  1811. if (bval[i] < xbv) {
  1812. x *= 1 + log(1 - x) * 2.3;
  1813. }
  1814. if (x > 6) {
  1815. x = 30;
  1816. }
  1817. if (x < minval_low) {
  1818. x = minval_low;
  1819. }
  1820. if (cfg->samplerate_out < 44000) {
  1821. x = 30;
  1822. }
  1823. x -= 8;
  1824. gd->s.minval[i] = pow(10.0, x / 10) * gd->s.numlines[i];
  1825. }
  1826. i = init_s3_values(&gd->s.s3, gd->s.s3ind, gd->s.npart, bval, bval_width, norm);
  1827. if (i)
  1828. return i;
  1829. init_mask_add_max_values();
  1830. init_fft(gfc);
  1831. /* setup temporal masking */
  1832. gd->decay = exp(-1.0 * LOG10 / (temporalmask_sustain_sec * sfreq / 192.0));
  1833. {
  1834. FLOAT msfix;
  1835. msfix = NS_MSFIX;
  1836. if (cfg->use_safe_joint_stereo)
  1837. msfix = 1.0;
  1838. if (fabs(cfg->msfix) > 0.0)
  1839. msfix = cfg->msfix;
  1840. cfg->msfix = msfix;
  1841. /* spread only from npart_l bands. Normally, we use the spreading
  1842. * function to convolve from npart_l down to npart_l bands
  1843. */
  1844. for (b = 0; b < gd->l.npart; b++)
  1845. if (gd->l.s3ind[b][1] > gd->l.npart - 1)
  1846. gd->l.s3ind[b][1] = gd->l.npart - 1;
  1847. }
  1848. /* prepare for ATH auto adjustment:
  1849. * we want to decrease the ATH by 12 dB per second
  1850. */
  1851. #define frame_duration (576. * cfg->mode_gr / sfreq)
  1852. gfc->ATH->decay = pow(10., -12. / 10. * frame_duration);
  1853. gfc->ATH->adjust_factor = 0.01; /* minimum, for leading low loudness */
  1854. gfc->ATH->adjust_limit = 1.0; /* on lead, allow adjust up to maximum */
  1855. #undef frame_duration
  1856. assert(gd->l.bo[SBMAX_l - 1] <= gd->l.npart);
  1857. assert(gd->s.bo[SBMAX_s - 1] <= gd->s.npart);
  1858. if (cfg->ATHtype != -1) {
  1859. /* compute equal loudness weights (eql_w) */
  1860. FLOAT freq;
  1861. FLOAT const freq_inc = (FLOAT) cfg->samplerate_out / (FLOAT) (BLKSIZE);
  1862. FLOAT eql_balance = 0.0;
  1863. freq = 0.0;
  1864. for (i = 0; i < BLKSIZE / 2; ++i) {
  1865. /* convert ATH dB to relative power (not dB) */
  1866. /* to determine eql_w */
  1867. freq += freq_inc;
  1868. gfc->ATH->eql_w[i] = 1. / pow(10, ATHformula(cfg, freq) / 10);
  1869. eql_balance += gfc->ATH->eql_w[i];
  1870. }
  1871. eql_balance = 1.0 / eql_balance;
  1872. for (i = BLKSIZE / 2; --i >= 0;) { /* scale weights */
  1873. gfc->ATH->eql_w[i] *= eql_balance;
  1874. }
  1875. }
  1876. {
  1877. for (b = j = 0; b < gd->s.npart; ++b) {
  1878. for (i = 0; i < gd->s.numlines[b]; ++i) {
  1879. ++j;
  1880. }
  1881. }
  1882. assert(j == 129);
  1883. for (b = j = 0; b < gd->l.npart; ++b) {
  1884. for (i = 0; i < gd->l.numlines[b]; ++i) {
  1885. ++j;
  1886. }
  1887. }
  1888. assert(j == 513);
  1889. }
  1890. /* short block attack threshold */
  1891. {
  1892. float x = gfp->attackthre;
  1893. float y = gfp->attackthre_s;
  1894. if (x < 0) {
  1895. x = NSATTACKTHRE;
  1896. }
  1897. if (y < 0) {
  1898. y = NSATTACKTHRE_S;
  1899. }
  1900. gd->attack_threshold[0] = gd->attack_threshold[1] = gd->attack_threshold[2] = x;
  1901. gd->attack_threshold[3] = y;
  1902. }
  1903. {
  1904. float sk_s = -10.f, sk_l = -4.7f;
  1905. static float const sk[] =
  1906. { -7.4, -7.4, -7.4, -9.5, -7.4, -6.1, -5.5, -4.7, -4.7, -4.7, -4.7 };
  1907. if (gfp->VBR_q < 4) {
  1908. sk_l = sk_s = sk[0];
  1909. }
  1910. else {
  1911. sk_l = sk_s = sk[gfp->VBR_q] + gfp->VBR_q_frac * (sk[gfp->VBR_q] - sk[gfp->VBR_q + 1]);
  1912. }
  1913. b = 0;
  1914. for (; b < gd->s.npart; b++) {
  1915. float m = (float) (gd->s.npart - b) / gd->s.npart;
  1916. gd->s.masking_lower[b] = powf(10.f, sk_s * m * 0.1f);
  1917. }
  1918. for (; b < CBANDS; ++b) {
  1919. gd->s.masking_lower[b] = 1.f;
  1920. }
  1921. b = 0;
  1922. for (; b < gd->l.npart; b++) {
  1923. float m = (float) (gd->l.npart - b) / gd->l.npart;
  1924. gd->l.masking_lower[b] = powf(10.f, sk_l * m * 0.1f);
  1925. }
  1926. for (; b < CBANDS; ++b) {
  1927. gd->l.masking_lower[b] = 1.f;
  1928. }
  1929. }
  1930. memcpy(&gd->l_to_s, &gd->l, sizeof(gd->l_to_s));
  1931. init_numline(&gd->l_to_s, sfreq, BLKSIZE, 192, SBMAX_s, gfc->scalefac_band.s);
  1932. return 0;
  1933. }