quantize_pvt.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  1. /*
  2. * quantize_pvt source file
  3. *
  4. * Copyright (c) 1999 Takehiro TOMINAGA
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Library General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public
  17. * License along with this library; if not, write to the
  18. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. */
  21. /* $Id: quantize_pvt.c,v 1.55 2001/03/05 20:29:24 markt Exp $ */
  22. #ifdef HAVE_CONFIG_H
  23. # include <config.h>
  24. #endif
  25. #include <assert.h>
  26. #include "util.h"
  27. #include "lame-analysis.h"
  28. #include "tables.h"
  29. #include "reservoir.h"
  30. #include "quantize_pvt.h"
  31. #ifdef WITH_DMALLOC
  32. #include <dmalloc.h>
  33. #endif
  34. #define NSATHSCALE 100 // Assuming dynamic range=96dB, this value should be 92
  35. const char slen1_tab [16] = { 0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 };
  36. const char slen2_tab [16] = { 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3 };
  37. /*
  38. The following table is used to implement the scalefactor
  39. partitioning for MPEG2 as described in section
  40. 2.4.3.2 of the IS. The indexing corresponds to the
  41. way the tables are presented in the IS:
  42. [table_number][row_in_table][column of nr_of_sfb]
  43. */
  44. const int nr_of_sfb_block [6] [3] [4] =
  45. {
  46. {
  47. {6, 5, 5, 5},
  48. {9, 9, 9, 9},
  49. {6, 9, 9, 9}
  50. },
  51. {
  52. {6, 5, 7, 3},
  53. {9, 9, 12, 6},
  54. {6, 9, 12, 6}
  55. },
  56. {
  57. {11, 10, 0, 0},
  58. {18, 18, 0, 0},
  59. {15,18,0,0}
  60. },
  61. {
  62. {7, 7, 7, 0},
  63. {12, 12, 12, 0},
  64. {6, 15, 12, 0}
  65. },
  66. {
  67. {6, 6, 6, 3},
  68. {12, 9, 9, 6},
  69. {6, 12, 9, 6}
  70. },
  71. {
  72. {8, 8, 5, 0},
  73. {15,12,9,0},
  74. {6,18,9,0}
  75. }
  76. };
  77. /* Table B.6: layer3 preemphasis */
  78. const char pretab [SBMAX_l] =
  79. {
  80. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  81. 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0
  82. };
  83. /*
  84. Here are MPEG1 Table B.8 and MPEG2 Table B.1
  85. -- Layer III scalefactor bands.
  86. Index into this using a method such as:
  87. idx = fr_ps->header->sampling_frequency
  88. + (fr_ps->header->version * 3)
  89. */
  90. const scalefac_struct sfBandIndex[9] =
  91. {
  92. { /* Table B.2.b: 22.05 kHz */
  93. {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
  94. {0,4,8,12,18,24,32,42,56,74,100,132,174,192}
  95. },
  96. { /* Table B.2.c: 24 kHz */ /* docs: 332. mpg123(broken): 330 */
  97. {0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278, 332, 394,464,540,576},
  98. {0,4,8,12,18,26,36,48,62,80,104,136,180,192}
  99. },
  100. { /* Table B.2.a: 16 kHz */
  101. {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
  102. {0,4,8,12,18,26,36,48,62,80,104,134,174,192}
  103. },
  104. { /* Table B.8.b: 44.1 kHz */
  105. {0,4,8,12,16,20,24,30,36,44,52,62,74,90,110,134,162,196,238,288,342,418,576},
  106. {0,4,8,12,16,22,30,40,52,66,84,106,136,192}
  107. },
  108. { /* Table B.8.c: 48 kHz */
  109. {0,4,8,12,16,20,24,30,36,42,50,60,72,88,106,128,156,190,230,276,330,384,576},
  110. {0,4,8,12,16,22,28,38,50,64,80,100,126,192}
  111. },
  112. { /* Table B.8.a: 32 kHz */
  113. {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576},
  114. {0,4,8,12,16,22,30,42,58,78,104,138,180,192}
  115. },
  116. { /* MPEG-2.5 11.025 kHz */
  117. {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
  118. {0/3,12/3,24/3,36/3,54/3,78/3,108/3,144/3,186/3,240/3,312/3,402/3,522/3,576/3}
  119. },
  120. { /* MPEG-2.5 12 kHz */
  121. {0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
  122. {0/3,12/3,24/3,36/3,54/3,78/3,108/3,144/3,186/3,240/3,312/3,402/3,522/3,576/3}
  123. },
  124. { /* MPEG-2.5 8 kHz */
  125. {0,12,24,36,48,60,72,88,108,132,160,192,232,280,336,400,476,566,568,570,572,574,576},
  126. {0/3,24/3,48/3,72/3,108/3,156/3,216/3,288/3,372/3,480/3,486/3,492/3,498/3,576/3}
  127. }
  128. };
  129. FLOAT8 pow20[Q_MAX];
  130. FLOAT8 ipow20[Q_MAX];
  131. FLOAT8 pow43[PRECALC_SIZE];
  132. /* initialized in first call to iteration_init */
  133. FLOAT8 adj43asm[PRECALC_SIZE];
  134. FLOAT8 adj43[PRECALC_SIZE];
  135. /************************************************************************/
  136. /* initialization for iteration_loop */
  137. /************************************************************************/
  138. void
  139. iteration_init( lame_global_flags *gfp)
  140. {
  141. lame_internal_flags *gfc=gfp->internal_flags;
  142. III_side_info_t * const l3_side = &gfc->l3_side;
  143. int i;
  144. if ( gfc->iteration_init_init==0 ) {
  145. gfc->iteration_init_init=1;
  146. l3_side->main_data_begin = 0;
  147. compute_ath(gfp,gfc->ATH->l,gfc->ATH->s);
  148. pow43[0] = 0.0;
  149. for(i=1;i<PRECALC_SIZE;i++)
  150. pow43[i] = pow((FLOAT8)i, 4.0/3.0);
  151. adj43asm[0] = 0.0;
  152. for (i = 1; i < PRECALC_SIZE; i++)
  153. adj43asm[i] = i - 0.5 - pow(0.5 * (pow43[i - 1] + pow43[i]),0.75);
  154. for (i = 0; i < PRECALC_SIZE-1; i++)
  155. adj43[i] = (i + 1) - pow(0.5 * (pow43[i] + pow43[i + 1]), 0.75);
  156. adj43[i] = 0.5;
  157. for (i = 0; i < Q_MAX; i++) {
  158. ipow20[i] = pow(2.0, (double)(i - 210) * -0.1875);
  159. pow20[i] = pow(2.0, (double)(i - 210) * 0.25);
  160. }
  161. huffman_init(gfc);
  162. }
  163. }
  164. /*
  165. compute the ATH for each scalefactor band
  166. cd range: 0..96db
  167. Input: 3.3kHz signal 32767 amplitude (3.3kHz is where ATH is smallest = -5db)
  168. longblocks: sfb=12 en0/bw=-11db max_en0 = 1.3db
  169. shortblocks: sfb=5 -9db 0db
  170. Input: 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 (repeated)
  171. longblocks: amp=1 sfb=12 en0/bw=-103 db max_en0 = -92db
  172. amp=32767 sfb=12 -12 db -1.4db
  173. Input: 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 (repeated)
  174. shortblocks: amp=1 sfb=5 en0/bw= -99 -86
  175. amp=32767 sfb=5 -9 db 4db
  176. MAX energy of largest wave at 3.3kHz = 1db
  177. AVE energy of largest wave at 3.3kHz = -11db
  178. Let's take AVE: -11db = maximum signal in sfb=12.
  179. Dynamic range of CD: 96db. Therefor energy of smallest audible wave
  180. in sfb=12 = -11 - 96 = -107db = ATH at 3.3kHz.
  181. ATH formula for this wave: -5db. To adjust to LAME scaling, we need
  182. ATH = ATH_formula - 103 (db)
  183. ATH = ATH * 2.5e-10 (ener)
  184. */
  185. FLOAT8 ATHmdct( lame_global_flags *gfp, FLOAT8 f )
  186. {
  187. lame_internal_flags *gfc = gfp->internal_flags;
  188. FLOAT8 ath;
  189. ath = ATHformula( f , gfp );
  190. if (gfc->nsPsy.use) {
  191. ath -= NSATHSCALE;
  192. } else {
  193. ath -= 114;
  194. }
  195. /* modify the MDCT scaling for the ATH */
  196. ath -= gfp->ATHlower;
  197. /* convert to energy */
  198. ath = pow( 10.0, ath/10.0 );
  199. return ath;
  200. }
  201. void compute_ath( lame_global_flags *gfp, FLOAT8 ATH_l[], FLOAT8 ATH_s[] )
  202. {
  203. lame_internal_flags *gfc = gfp->internal_flags;
  204. int sfb, i, start, end;
  205. FLOAT8 ATH_f;
  206. FLOAT8 samp_freq = gfp->out_samplerate;
  207. for (sfb = 0; sfb < SBMAX_l; sfb++) {
  208. start = gfc->scalefac_band.l[ sfb ];
  209. end = gfc->scalefac_band.l[ sfb+1 ];
  210. ATH_l[sfb]=1e99;
  211. for (i = start ; i < end; i++) {
  212. FLOAT8 freq = i*samp_freq/(2*576);
  213. ATH_f = ATHmdct( gfp, freq ); /* freq in kHz */
  214. ATH_l[sfb] = Min( ATH_l[sfb], ATH_f );
  215. }
  216. }
  217. for (sfb = 0; sfb < SBMAX_s; sfb++){
  218. start = gfc->scalefac_band.s[ sfb ];
  219. end = gfc->scalefac_band.s[ sfb+1 ];
  220. ATH_s[sfb] = 1e99;
  221. for (i = start ; i < end; i++) {
  222. FLOAT8 freq = i*samp_freq/(2*192);
  223. ATH_f = ATHmdct( gfp, freq ); /* freq in kHz */
  224. ATH_s[sfb] = Min( ATH_s[sfb], ATH_f );
  225. }
  226. }
  227. /* no-ATH mode:
  228. * reduce ATH to -200 dB
  229. */
  230. if (gfp->noATH) {
  231. for (sfb = 0; sfb < SBMAX_l; sfb++) {
  232. ATH_l[sfb] = 1E-37;
  233. }
  234. for (sfb = 0; sfb < SBMAX_s; sfb++) {
  235. ATH_s[sfb] = 1E-37;
  236. }
  237. }
  238. }
  239. /* convert from L/R <-> Mid/Side, src == dst allowed */
  240. void ms_convert(FLOAT8 dst[2][576], FLOAT8 src[2][576])
  241. {
  242. FLOAT8 l;
  243. FLOAT8 r;
  244. int i;
  245. for (i = 0; i < 576; ++i) {
  246. l = src[0][i];
  247. r = src[1][i];
  248. dst[0][i] = (l+r) * (FLOAT8)(SQRT2*0.5);
  249. dst[1][i] = (l-r) * (FLOAT8)(SQRT2*0.5);
  250. }
  251. }
  252. /************************************************************************
  253. * allocate bits among 2 channels based on PE
  254. * mt 6/99
  255. ************************************************************************/
  256. int on_pe(lame_global_flags *gfp,FLOAT8 pe[2][2],III_side_info_t *l3_side,
  257. int targ_bits[2],int mean_bits, int gr)
  258. {
  259. lame_internal_flags *gfc=gfp->internal_flags;
  260. gr_info *cod_info;
  261. int extra_bits,tbits,bits;
  262. int add_bits[2];
  263. int ch;
  264. int max_bits; /* maximum allowed bits for this granule */
  265. /* allocate targ_bits for granule */
  266. ResvMaxBits (gfp, mean_bits, &tbits, &extra_bits);
  267. max_bits=tbits+extra_bits;
  268. bits=0;
  269. for (ch=0 ; ch < gfc->channels_out ; ch ++) {
  270. /******************************************************************
  271. * allocate bits for each channel
  272. ******************************************************************/
  273. cod_info = &l3_side->gr[gr].ch[ch].tt;
  274. targ_bits[ch]=Min(MAX_BITS, tbits/gfc->channels_out);
  275. if (gfc->nsPsy.use) {
  276. add_bits[ch] = targ_bits[ch]*pe[gr][ch]/700.0-targ_bits[ch];
  277. } else {
  278. add_bits[ch]=(pe[gr][ch]-750)/1.4;
  279. /* short blocks us a little extra, no matter what the pe */
  280. if (cod_info->block_type==SHORT_TYPE) {
  281. if (add_bits[ch]<mean_bits/4) add_bits[ch]=mean_bits/4;
  282. }
  283. /* at most increase bits by 1.5*average */
  284. if (add_bits[ch] > .75*mean_bits) add_bits[ch]=mean_bits*.75;
  285. if (add_bits[ch] < 0) add_bits[ch]=0;
  286. if ((targ_bits[ch]+add_bits[ch]) > MAX_BITS)
  287. add_bits[ch]=Max(0, MAX_BITS-targ_bits[ch]);
  288. }
  289. bits += add_bits[ch];
  290. }
  291. if (bits > extra_bits)
  292. for (ch=0 ; ch < gfc->channels_out ; ch ++) {
  293. add_bits[ch] = (extra_bits*add_bits[ch])/bits;
  294. }
  295. for (ch=0 ; ch < gfc->channels_out ; ch ++) {
  296. targ_bits[ch] = targ_bits[ch] + add_bits[ch];
  297. extra_bits -= add_bits[ch];
  298. }
  299. return max_bits;
  300. }
  301. void reduce_side(int targ_bits[2],FLOAT8 ms_ener_ratio,int mean_bits,int max_bits)
  302. {
  303. int move_bits;
  304. FLOAT fac;
  305. /* ms_ener_ratio = 0: allocate 66/33 mid/side fac=.33
  306. * ms_ener_ratio =.5: allocate 50/50 mid/side fac= 0 */
  307. /* 75/25 split is fac=.5 */
  308. /* float fac = .50*(.5-ms_ener_ratio[gr])/.5;*/
  309. fac = .33*(.5-ms_ener_ratio)/.5;
  310. if (fac<0) fac=0;
  311. if (fac>.5) fac=.5;
  312. /* number of bits to move from side channel to mid channel */
  313. /* move_bits = fac*targ_bits[1]; */
  314. move_bits = fac*.5*(targ_bits[0]+targ_bits[1]);
  315. if (move_bits > MAX_BITS - targ_bits[0]) {
  316. move_bits = MAX_BITS - targ_bits[0];
  317. }
  318. if (move_bits<0) move_bits=0;
  319. if (targ_bits[1] >= 125) {
  320. /* dont reduce side channel below 125 bits */
  321. if (targ_bits[1]-move_bits > 125) {
  322. /* if mid channel already has 2x more than average, dont bother */
  323. /* mean_bits = bits per granule (for both channels) */
  324. if (targ_bits[0] < mean_bits)
  325. targ_bits[0] += move_bits;
  326. targ_bits[1] -= move_bits;
  327. } else {
  328. targ_bits[0] += targ_bits[1] - 125;
  329. targ_bits[1] = 125;
  330. }
  331. }
  332. move_bits=targ_bits[0]+targ_bits[1];
  333. if (move_bits > max_bits) {
  334. targ_bits[0]=(max_bits*targ_bits[0])/move_bits;
  335. targ_bits[1]=(max_bits*targ_bits[1])/move_bits;
  336. }
  337. }
  338. #if 0
  339. FLOAT8 dreinorm (FLOAT8 a, FLOAT8 b, FLOAT8 c)
  340. {
  341. return pow(pow(a,3.)+pow(b,3.)+pow(c,3.),1./3.);
  342. }
  343. #endif
  344. /*************************************************************************/
  345. /* calc_xmin */
  346. /*************************************************************************/
  347. /*
  348. Calculate the allowed distortion for each scalefactor band,
  349. as determined by the psychoacoustic model.
  350. xmin(sb) = ratio(sb) * en(sb) / bw(sb)
  351. returns number of sfb's with energy > ATH
  352. */
  353. int calc_xmin(
  354. lame_global_flags *gfp,
  355. const FLOAT8 xr [576],
  356. const III_psy_ratio * const ratio,
  357. const gr_info * const cod_info,
  358. III_psy_xmin * const l3_xmin )
  359. {
  360. lame_internal_flags *gfc=gfp->internal_flags;
  361. int sfb,j,start, end, bw,l, b, ath_over=0;
  362. FLOAT8 en0, xmin, ener;
  363. if (cod_info->block_type==SHORT_TYPE) {
  364. for ( j=0, sfb = 0; sfb < SBMAX_s; sfb++ ) {
  365. start = gfc->scalefac_band.s[ sfb ];
  366. end = gfc->scalefac_band.s[ sfb + 1 ];
  367. bw = end - start;
  368. for ( b = 0; b < 3; b++ ) {
  369. for (en0 = 0.0, l = start; l < end; l++) {
  370. ener = xr[j++];
  371. ener = ener * ener;
  372. en0 += ener;
  373. }
  374. en0 /= bw;
  375. if (gfp->ATHonly || gfp->ATHshort) {
  376. xmin = gfc->ATH->adjust * gfc->ATH->s[sfb];
  377. } else {
  378. xmin = ratio->en.s[sfb][b];
  379. if (xmin > 0.0)
  380. xmin = en0 * ratio->thm.s[sfb][b] * gfc->masking_lower / xmin;
  381. xmin = Max(gfc->ATH->adjust * gfc->ATH->s[sfb], xmin);
  382. }
  383. l3_xmin->s[sfb][b] = xmin * bw;
  384. if (gfc->nsPsy.use) {
  385. if (sfb <= 5) {
  386. l3_xmin->s[sfb][b] *= gfc->nsPsy.bass;
  387. } else if (sfb <= 10) {
  388. l3_xmin->s[sfb][b] *= gfc->nsPsy.alto;
  389. } else {
  390. l3_xmin->s[sfb][b] *= gfc->nsPsy.treble;
  391. }
  392. }
  393. if (en0 > gfc->ATH->adjust * gfc->ATH->s[sfb]) ath_over++;
  394. if (gfc->nsPsy.use && (gfp->VBR == vbr_off || gfp->VBR == vbr_abr) && gfp->quality <= 1)
  395. l3_xmin->s[sfb][b] *= 0.001;
  396. }
  397. }
  398. if (gfp->useTemporal) {
  399. for (sfb = 0; sfb < SBMAX_s; sfb++ ) {
  400. for ( b = 1; b < 3; b++ ) {
  401. xmin = l3_xmin->s[sfb][b] * (1.0 - gfc->decay)
  402. + l3_xmin->s[sfb][b-1] * gfc->decay;
  403. if (l3_xmin->s[sfb][b] < xmin)
  404. l3_xmin->s[sfb][b] = xmin;
  405. }
  406. }
  407. }
  408. }else{
  409. if (gfc->nsPsy.use) {
  410. for ( sfb = 0; sfb < SBMAX_l; sfb++ ){
  411. start = gfc->scalefac_band.l[ sfb ];
  412. end = gfc->scalefac_band.l[ sfb+1 ];
  413. for (en0 = 0.0, l = start; l < end; l++ ) {
  414. ener = xr[l] * xr[l];
  415. en0 += ener;
  416. }
  417. if (gfp->ATHonly) {
  418. xmin=gfc->ATH->adjust * gfc->ATH->l[sfb];
  419. } else {
  420. xmin = ratio->en.l[sfb];
  421. if (xmin > 0.0)
  422. xmin = en0 * ratio->thm.l[sfb] * gfc->masking_lower / xmin;
  423. xmin=Max(gfc->ATH->adjust * gfc->ATH->l[sfb], xmin);
  424. }
  425. l3_xmin->l[sfb]=xmin;
  426. if (sfb <= 6) {
  427. l3_xmin->l[sfb] *= gfc->nsPsy.bass;
  428. } else if (sfb <= 13) {
  429. l3_xmin->l[sfb] *= gfc->nsPsy.alto;
  430. } else {
  431. l3_xmin->l[sfb] *= gfc->nsPsy.treble;
  432. }
  433. if (en0 > gfc->ATH->adjust * gfc->ATH->l[sfb]) ath_over++;
  434. if ((gfp->VBR == vbr_off || gfp->VBR == vbr_abr) && gfp->quality <= 1)
  435. l3_xmin->l[sfb] *= 0.001;
  436. }
  437. } else {
  438. for ( sfb = 0; sfb < SBMAX_l; sfb++ ){
  439. start = gfc->scalefac_band.l[ sfb ];
  440. end = gfc->scalefac_band.l[ sfb+1 ];
  441. bw = end - start;
  442. for (en0 = 0.0, l = start; l < end; l++ ) {
  443. ener = xr[l] * xr[l];
  444. en0 += ener;
  445. }
  446. en0 /= bw;
  447. if (gfp->ATHonly) {
  448. xmin=gfc->ATH->adjust * gfc->ATH->l[sfb];
  449. } else {
  450. xmin = ratio->en.l[sfb];
  451. if (xmin > 0.0)
  452. xmin = en0 * ratio->thm.l[sfb] * gfc->masking_lower / xmin;
  453. xmin=Max(gfc->ATH->adjust * gfc->ATH->l[sfb], xmin);
  454. }
  455. l3_xmin->l[sfb]=xmin*bw;
  456. if (en0 > gfc->ATH->adjust * gfc->ATH->l[sfb]) ath_over++;
  457. }
  458. }
  459. }
  460. return ath_over;
  461. }
  462. /*************************************************************************/
  463. /* calc_noise */
  464. /*************************************************************************/
  465. // -oo dB => -1.00
  466. // - 6 dB => -0.97
  467. // - 3 dB => -0.80
  468. // - 2 dB => -0.64
  469. // - 1 dB => -0.38
  470. // 0 dB => 0.00
  471. // + 1 dB => +0.49
  472. // + 2 dB => +1.06
  473. // + 3 dB => +1.68
  474. // + 6 dB => +3.69
  475. // +10 dB => +6.45
  476. double penalties ( double noise )
  477. {
  478. return log ( 0.368 + 0.632 * noise * noise * noise );
  479. }
  480. /* mt 5/99: Function: Improved calc_noise for a single channel */
  481. int calc_noise(
  482. const lame_internal_flags * const gfc,
  483. const FLOAT8 xr [576],
  484. const int ix [576],
  485. const gr_info * const cod_info,
  486. const III_psy_xmin * const l3_xmin,
  487. const III_scalefac_t * const scalefac,
  488. III_psy_xmin * xfsf,
  489. calc_noise_result * const res )
  490. {
  491. int sfb,start, end, j,l, i, over=0;
  492. FLOAT8 sum;
  493. int count=0;
  494. FLOAT8 noise,noise_db;
  495. FLOAT8 over_noise = 1; /* 0 dB relative to masking */
  496. FLOAT8 over_noise_db = 0;
  497. FLOAT8 tot_noise = 1; /* 0 dB relative to masking */
  498. FLOAT8 tot_noise_db = 0; /* 0 dB relative to masking */
  499. FLOAT8 max_noise = 1E-20; /* -200 dB relative to masking */
  500. double klemm_noise = 1E-37;
  501. if (cod_info->block_type == SHORT_TYPE) {
  502. int max_index = gfc->sfb21_extra ? SBMAX_s : SBPSY_s;
  503. for ( j=0, sfb = 0; sfb < max_index; sfb++ ) {
  504. start = gfc->scalefac_band.s[ sfb ];
  505. end = gfc->scalefac_band.s[ sfb+1 ];
  506. for ( i = 0; i < 3; i++ ) {
  507. FLOAT8 step;
  508. int s;
  509. s = (scalefac->s[sfb][i] << (cod_info->scalefac_scale + 1))
  510. + cod_info->subblock_gain[i] * 8;
  511. s = cod_info->global_gain - s;
  512. assert(s<Q_MAX);
  513. assert(s>=0);
  514. step = POW20(s);
  515. sum = 0.0;
  516. l = start;
  517. do {
  518. FLOAT8 temp;
  519. temp = pow43[ix[j]];
  520. temp *= step;
  521. temp -= fabs(xr[j]);
  522. ++j;
  523. sum += temp * temp;
  524. l++;
  525. } while (l < end);
  526. noise = xfsf->s[sfb][i] = sum / l3_xmin->s[sfb][i];
  527. max_noise = Max(max_noise,noise);
  528. klemm_noise += penalties (noise);
  529. noise_db=10*log10(Max(noise,1E-20));
  530. /* multiplying here is adding in dB, but will overflow */
  531. //tot_noise *= Max(noise, 1E-20);
  532. tot_noise_db += noise_db;
  533. if (noise > 1) {
  534. over++;
  535. /* multiplying here is adding in dB, but can overflow */
  536. //over_noise *= noise;
  537. over_noise_db += noise_db;
  538. }
  539. count++;
  540. }
  541. }
  542. }else{ /* cod_info->block_type == SHORT_TYPE */
  543. int max_index = gfc->sfb21_extra ? SBMAX_l : SBPSY_l;
  544. for ( sfb = 0; sfb < max_index; sfb++ ) {
  545. FLOAT8 step;
  546. int s = scalefac->l[sfb];
  547. if (cod_info->preflag)
  548. s += pretab[sfb];
  549. s = cod_info->global_gain - (s << (cod_info->scalefac_scale + 1));
  550. assert(s<Q_MAX);
  551. assert(s>=0);
  552. step = POW20(s);
  553. start = gfc->scalefac_band.l[ sfb ];
  554. end = gfc->scalefac_band.l[ sfb+1 ];
  555. for ( sum = 0.0, l = start; l < end; l++ ) {
  556. FLOAT8 temp;
  557. temp = fabs(xr[l]) - pow43[ix[l]] * step;
  558. sum += temp * temp;
  559. }
  560. noise = xfsf->l[sfb] = sum / l3_xmin->l[sfb];
  561. max_noise=Max(max_noise,noise);
  562. klemm_noise += penalties (noise);
  563. noise_db=10*log10(Max(noise,1E-20));
  564. /* multiplying here is adding in dB, but can overflow */
  565. //tot_noise *= Max(noise, 1E-20);
  566. tot_noise_db += noise_db;
  567. if (noise > 1) {
  568. over++;
  569. /* multiplying here is adding in dB -but can overflow */
  570. //over_noise *= noise;
  571. over_noise_db += noise_db;
  572. }
  573. count++;
  574. }
  575. } /* cod_info->block_type == SHORT_TYPE */
  576. /* normalization at this point by "count" is not necessary, since
  577. * the values are only used to compare with previous values */
  578. res->tot_count = count;
  579. res->over_count = over;
  580. /* convert to db. DO NOT CHANGE THESE */
  581. /* tot_noise = is really the average over each sfb of:
  582. [noise(db) - allowed_noise(db)]
  583. and over_noise is the same average, only over only the
  584. bands with noise > allowed_noise.
  585. */
  586. //res->tot_noise = 10.*log10(Max(1e-20,tot_noise ));
  587. //res->over_noise = 10.*log10(Max(1e+00,over_noise));
  588. res->tot_noise = tot_noise_db;
  589. res->over_noise = over_noise_db;
  590. res->max_noise = 10.*log10(Max(1e-20,max_noise ));
  591. res->klemm_noise = 10.*log10(Max(1e-20,klemm_noise));
  592. return over;
  593. }
  594. /************************************************************************
  595. *
  596. * set_pinfo()
  597. *
  598. * updates plotting data
  599. *
  600. * Mark Taylor 2000-??-??
  601. *
  602. * Robert Hegemann: moved noise/distortion calc into it
  603. *
  604. ************************************************************************/
  605. static
  606. void set_pinfo (
  607. lame_global_flags *gfp,
  608. const gr_info * const cod_info,
  609. const III_psy_ratio * const ratio,
  610. const III_scalefac_t * const scalefac,
  611. const FLOAT8 xr[576],
  612. const int l3_enc[576],
  613. const int gr,
  614. const int ch )
  615. {
  616. lame_internal_flags *gfc=gfp->internal_flags;
  617. int sfb;
  618. int j,i,l,start,end,bw;
  619. FLOAT8 en0,en1;
  620. FLOAT ifqstep = ( cod_info->scalefac_scale == 0 ) ? .5 : 1.0;
  621. III_psy_xmin l3_xmin;
  622. calc_noise_result noise;
  623. III_psy_xmin xfsf;
  624. calc_xmin (gfp,xr, ratio, cod_info, &l3_xmin);
  625. calc_noise (gfc, xr, l3_enc, cod_info, &l3_xmin, scalefac, &xfsf, &noise);
  626. if (cod_info->block_type == SHORT_TYPE) {
  627. for (j=0, sfb = 0; sfb < SBMAX_s; sfb++ ) {
  628. start = gfc->scalefac_band.s[ sfb ];
  629. end = gfc->scalefac_band.s[ sfb + 1 ];
  630. bw = end - start;
  631. for ( i = 0; i < 3; i++ ) {
  632. for ( en0 = 0.0, l = start; l < end; l++ ) {
  633. en0 += xr[j] * xr[j];
  634. ++j;
  635. }
  636. en0=Max(en0/bw,1e-20);
  637. #if 0
  638. {
  639. double tot1,tot2;
  640. if (sfb<SBMAX_s-1) {
  641. if (sfb==0) {
  642. tot1=0;
  643. tot2=0;
  644. }
  645. tot1 += en0;
  646. tot2 += ratio->en.s[sfb][i];
  647. DEBUGF("%i %i sfb=%i mdct=%f fft=%f fft-mdct=%f db \n",
  648. gr,ch,sfb,
  649. 10*log10(Max(1e-25,ratio->en.s[sfb][i])),
  650. 10*log10(Max(1e-25,en0)),
  651. 10*log10((Max(1e-25,en0)/Max(1e-25,ratio->en.s[sfb][i]))));
  652. if (sfb==SBMAX_s-2) {
  653. DEBUGF("%i %i toti %f %f ratio=%f db \n",gr,ch,
  654. 10*log10(Max(1e-25,tot2)),
  655. 10*log(Max(1e-25,tot1)),
  656. 10*log10(Max(1e-25,tot1)/(Max(1e-25,tot2))));
  657. }
  658. }
  659. /*
  660. masking: multiplied by en0/fft_energy
  661. average seems to be about -135db.
  662. */
  663. }
  664. #endif
  665. /* convert to MDCT units */
  666. en1=1e15; /* scaling so it shows up on FFT plot */
  667. gfc->pinfo->xfsf_s[gr][ch][3*sfb+i]
  668. = en1*xfsf.s[sfb][i]*l3_xmin.s[sfb][i]/bw;
  669. gfc->pinfo->en_s[gr][ch][3*sfb+i] = en1*en0;
  670. if (ratio->en.s[sfb][i]>0)
  671. en0 = en0/ratio->en.s[sfb][i];
  672. else
  673. en0=0;
  674. if (gfp->ATHonly || gfp->ATHshort)
  675. en0=0;
  676. gfc->pinfo->thr_s[gr][ch][3*sfb+i] =
  677. en1*Max(en0*ratio->thm.s[sfb][i],gfc->ATH->s[sfb]);
  678. /* there is no scalefactor bands >= SBPSY_s */
  679. if (sfb < SBPSY_s) {
  680. gfc->pinfo->LAMEsfb_s[gr][ch][3*sfb+i]=
  681. -ifqstep*scalefac->s[sfb][i];
  682. } else {
  683. gfc->pinfo->LAMEsfb_s[gr][ch][3*sfb+i]=0;
  684. }
  685. gfc->pinfo->LAMEsfb_s[gr][ch][3*sfb+i] -=
  686. 2*cod_info->subblock_gain[i];
  687. }
  688. }
  689. } else {
  690. for ( sfb = 0; sfb < SBMAX_l; sfb++ ) {
  691. start = gfc->scalefac_band.l[ sfb ];
  692. end = gfc->scalefac_band.l[ sfb+1 ];
  693. bw = end - start;
  694. for ( en0 = 0.0, l = start; l < end; l++ )
  695. en0 += xr[l] * xr[l];
  696. if (!gfc->nsPsy.use) en0/=bw;
  697. /*
  698. DEBUGF("diff = %f \n",10*log10(Max(ratio[gr][ch].en.l[sfb],1e-20))
  699. -(10*log10(en0)+150));
  700. */
  701. #if 0
  702. {
  703. double tot1,tot2;
  704. if (sfb==0) {
  705. tot1=0;
  706. tot2=0;
  707. }
  708. tot1 += en0;
  709. tot2 += ratio->en.l[sfb];
  710. DEBUGF("%i %i sfb=%i mdct=%f fft=%f fft-mdct=%f db \n",
  711. gr,ch,sfb,
  712. 10*log10(Max(1e-25,ratio->en.l[sfb])),
  713. 10*log10(Max(1e-25,en0)),
  714. 10*log10((Max(1e-25,en0)/Max(1e-25,ratio->en.l[sfb]))));
  715. if (sfb==SBMAX_l-1) {
  716. DEBUGF("%i %i toti %f %f ratio=%f db \n",
  717. gr,ch,
  718. 10*log10(Max(1e-25,tot2)),
  719. 10*log(Max(1e-25,tot1)),
  720. 10*log10(Max(1e-25,tot1)/(Max(1e-25,tot2))));
  721. }
  722. /*
  723. masking: multiplied by en0/fft_energy
  724. average seems to be about -147db.
  725. */
  726. }
  727. #endif
  728. /* convert to MDCT units */
  729. en1=1e15; /* scaling so it shows up on FFT plot */
  730. gfc->pinfo->xfsf[gr][ch][sfb] = en1*xfsf.l[sfb]*l3_xmin.l[sfb]/bw;
  731. gfc->pinfo->en[gr][ch][sfb] = en1*en0;
  732. if (ratio->en.l[sfb]>0)
  733. en0 = en0/ratio->en.l[sfb];
  734. else
  735. en0=0;
  736. if (gfp->ATHonly)
  737. en0=0;
  738. gfc->pinfo->thr[gr][ch][sfb] =
  739. en1*Max(en0*ratio->thm.l[sfb],gfc->ATH->l[sfb]);
  740. /* there is no scalefactor bands >= SBPSY_l */
  741. if (sfb<SBPSY_l) {
  742. if (scalefac->l[sfb]<0) /* scfsi! */
  743. gfc->pinfo->LAMEsfb[gr][ch][sfb] =
  744. gfc->pinfo->LAMEsfb[0][ch][sfb];
  745. else
  746. gfc->pinfo->LAMEsfb[gr][ch][sfb] = -ifqstep*scalefac->l[sfb];
  747. }else{
  748. gfc->pinfo->LAMEsfb[gr][ch][sfb] = 0;
  749. }
  750. if (cod_info->preflag && sfb>=11)
  751. gfc->pinfo->LAMEsfb[gr][ch][sfb] -= ifqstep*pretab[sfb];
  752. } /* for sfb */
  753. } /* block type long */
  754. gfc->pinfo->LAMEqss [gr][ch] = cod_info->global_gain;
  755. gfc->pinfo->LAMEmainbits[gr][ch] = cod_info->part2_3_length;
  756. gfc->pinfo->LAMEsfbits [gr][ch] = cod_info->part2_length;
  757. gfc->pinfo->over [gr][ch] = noise.over_count;
  758. gfc->pinfo->max_noise [gr][ch] = noise.max_noise;
  759. gfc->pinfo->over_noise[gr][ch] = noise.over_noise;
  760. gfc->pinfo->tot_noise [gr][ch] = noise.tot_noise;
  761. }
  762. /************************************************************************
  763. *
  764. * set_frame_pinfo()
  765. *
  766. * updates plotting data for a whole frame
  767. *
  768. * Robert Hegemann 2000-10-21
  769. *
  770. ************************************************************************/
  771. void set_frame_pinfo(
  772. lame_global_flags *gfp,
  773. FLOAT8 xr [2][2][576],
  774. III_psy_ratio ratio [2][2],
  775. int l3_enc [2][2][576],
  776. III_scalefac_t scalefac [2][2] )
  777. {
  778. lame_internal_flags *gfc=gfp->internal_flags;
  779. unsigned int gr, ch, sfb;
  780. int act_l3enc[576];
  781. III_scalefac_t act_scalefac [2];
  782. int scsfi[2] = {0,0};
  783. gfc->masking_lower = 1.0;
  784. /* reconstruct the scalefactors in case SCSFI was used
  785. */
  786. for (ch = 0; ch < gfc->channels_out; ch ++) {
  787. for (sfb = 0; sfb < SBMAX_l; sfb ++) {
  788. if (scalefac[1][ch].l[sfb] == -1) {/* scfsi */
  789. act_scalefac[ch].l[sfb] = scalefac[0][ch].l[sfb];
  790. scsfi[ch] = 1;
  791. } else {
  792. act_scalefac[ch].l[sfb] = scalefac[1][ch].l[sfb];
  793. }
  794. }
  795. }
  796. /* for every granule and channel patch l3_enc and set info
  797. */
  798. for (gr = 0; gr < gfc->mode_gr; gr ++) {
  799. for (ch = 0; ch < gfc->channels_out; ch ++) {
  800. int i;
  801. gr_info *cod_info = &gfc->l3_side.gr[gr].ch[ch].tt;
  802. /* revert back the sign of l3enc */
  803. for ( i = 0; i < 576; i++) {
  804. if (xr[gr][ch][i] < 0)
  805. act_l3enc[i] = -l3_enc[gr][ch][i];
  806. else
  807. act_l3enc[i] = +l3_enc[gr][ch][i];
  808. }
  809. if (gr == 1 && scsfi[ch])
  810. set_pinfo (gfp, cod_info, &ratio[gr][ch], &act_scalefac[ch],
  811. xr[gr][ch], act_l3enc, gr, ch);
  812. else
  813. set_pinfo (gfp, cod_info, &ratio[gr][ch], &scalefac[gr][ch],
  814. xr[gr][ch], act_l3enc, gr, ch);
  815. } /* for ch */
  816. } /* for gr */
  817. }
  818. /*********************************************************************
  819. * nonlinear quantization of xr
  820. * More accurate formula than the ISO formula. Takes into account
  821. * the fact that we are quantizing xr -> ix, but we want ix^4/3 to be
  822. * as close as possible to x^4/3. (taking the nearest int would mean
  823. * ix is as close as possible to xr, which is different.)
  824. * From Segher Boessenkool <segher@eastsite.nl> 11/1999
  825. * ASM optimization from
  826. * Mathew Hendry <scampi@dial.pipex.com> 11/1999
  827. * Acy Stapp <AStapp@austin.rr.com> 11/1999
  828. * Takehiro Tominaga <tominaga@isoternet.org> 11/1999
  829. * 9/00: ASM code removed in favor of IEEE754 hack. If you need
  830. * the ASM code, check CVS circa Aug 2000.
  831. *********************************************************************/
  832. #ifdef TAKEHIRO_IEEE754_HACK
  833. typedef union {
  834. float f;
  835. int i;
  836. } fi_union;
  837. #define MAGIC_FLOAT (65536*(128))
  838. #define MAGIC_INT 0x4b000000
  839. void quantize_xrpow(const FLOAT8 xp[576], int pi[576], FLOAT8 istep)
  840. {
  841. /* quantize on xr^(3/4) instead of xr */
  842. int j;
  843. fi_union *fi;
  844. fi = (fi_union *)pi;
  845. for (j = 576 / 4 - 1; j >= 0; --j) {
  846. double x0 = istep * xp[0];
  847. double x1 = istep * xp[1];
  848. double x2 = istep * xp[2];
  849. double x3 = istep * xp[3];
  850. x0 += MAGIC_FLOAT; fi[0].f = x0;
  851. x1 += MAGIC_FLOAT; fi[1].f = x1;
  852. x2 += MAGIC_FLOAT; fi[2].f = x2;
  853. x3 += MAGIC_FLOAT; fi[3].f = x3;
  854. fi[0].f = x0 + (adj43asm - MAGIC_INT)[fi[0].i];
  855. fi[1].f = x1 + (adj43asm - MAGIC_INT)[fi[1].i];
  856. fi[2].f = x2 + (adj43asm - MAGIC_INT)[fi[2].i];
  857. fi[3].f = x3 + (adj43asm - MAGIC_INT)[fi[3].i];
  858. fi[0].i -= MAGIC_INT;
  859. fi[1].i -= MAGIC_INT;
  860. fi[2].i -= MAGIC_INT;
  861. fi[3].i -= MAGIC_INT;
  862. fi += 4;
  863. xp += 4;
  864. }
  865. }
  866. # define ROUNDFAC -0.0946
  867. void quantize_xrpow_ISO(const FLOAT8 xp[576], int pi[576], FLOAT8 istep)
  868. {
  869. /* quantize on xr^(3/4) instead of xr */
  870. int j;
  871. fi_union *fi;
  872. fi = (fi_union *)pi;
  873. for (j=576/4 - 1;j>=0;j--) {
  874. fi[0].f = istep * xp[0] + (ROUNDFAC + MAGIC_FLOAT);
  875. fi[1].f = istep * xp[1] + (ROUNDFAC + MAGIC_FLOAT);
  876. fi[2].f = istep * xp[2] + (ROUNDFAC + MAGIC_FLOAT);
  877. fi[3].f = istep * xp[3] + (ROUNDFAC + MAGIC_FLOAT);
  878. fi[0].i -= MAGIC_INT;
  879. fi[1].i -= MAGIC_INT;
  880. fi[2].i -= MAGIC_INT;
  881. fi[3].i -= MAGIC_INT;
  882. fi+=4;
  883. xp+=4;
  884. }
  885. }
  886. #else
  887. /*********************************************************************
  888. * XRPOW_FTOI is a macro to convert floats to ints.
  889. * if XRPOW_FTOI(x) = nearest_int(x), then QUANTFAC(x)=adj43asm[x]
  890. * ROUNDFAC= -0.0946
  891. *
  892. * if XRPOW_FTOI(x) = floor(x), then QUANTFAC(x)=asj43[x]
  893. * ROUNDFAC=0.4054
  894. *
  895. * Note: using floor() or (int) is extermely slow. On machines where
  896. * the TAKEHIRO_IEEE754_HACK code above does not work, it is worthwile
  897. * to write some ASM for XRPOW_FTOI().
  898. *********************************************************************/
  899. #define XRPOW_FTOI(src,dest) ((dest) = (int)(src))
  900. #define QUANTFAC(rx) adj43[rx]
  901. #define ROUNDFAC 0.4054
  902. void quantize_xrpow(const FLOAT8 xr[576], int ix[576], FLOAT8 istep) {
  903. /* quantize on xr^(3/4) instead of xr */
  904. /* from Wilfried.Behne@t-online.de. Reported to be 2x faster than
  905. the above code (when not using ASM) on PowerPC */
  906. int j;
  907. for ( j = 576/8; j > 0; --j) {
  908. FLOAT8 x1, x2, x3, x4, x5, x6, x7, x8;
  909. int rx1, rx2, rx3, rx4, rx5, rx6, rx7, rx8;
  910. x1 = *xr++ * istep;
  911. x2 = *xr++ * istep;
  912. XRPOW_FTOI(x1, rx1);
  913. x3 = *xr++ * istep;
  914. XRPOW_FTOI(x2, rx2);
  915. x4 = *xr++ * istep;
  916. XRPOW_FTOI(x3, rx3);
  917. x5 = *xr++ * istep;
  918. XRPOW_FTOI(x4, rx4);
  919. x6 = *xr++ * istep;
  920. XRPOW_FTOI(x5, rx5);
  921. x7 = *xr++ * istep;
  922. XRPOW_FTOI(x6, rx6);
  923. x8 = *xr++ * istep;
  924. XRPOW_FTOI(x7, rx7);
  925. x1 += QUANTFAC(rx1);
  926. XRPOW_FTOI(x8, rx8);
  927. x2 += QUANTFAC(rx2);
  928. XRPOW_FTOI(x1,*ix++);
  929. x3 += QUANTFAC(rx3);
  930. XRPOW_FTOI(x2,*ix++);
  931. x4 += QUANTFAC(rx4);
  932. XRPOW_FTOI(x3,*ix++);
  933. x5 += QUANTFAC(rx5);
  934. XRPOW_FTOI(x4,*ix++);
  935. x6 += QUANTFAC(rx6);
  936. XRPOW_FTOI(x5,*ix++);
  937. x7 += QUANTFAC(rx7);
  938. XRPOW_FTOI(x6,*ix++);
  939. x8 += QUANTFAC(rx8);
  940. XRPOW_FTOI(x7,*ix++);
  941. XRPOW_FTOI(x8,*ix++);
  942. }
  943. }
  944. void quantize_xrpow_ISO( const FLOAT8 xr[576], int ix[576], FLOAT8 istep )
  945. {
  946. /* quantize on xr^(3/4) instead of xr */
  947. const FLOAT8 compareval0 = (1.0 - 0.4054)/istep;
  948. int j;
  949. /* depending on architecture, it may be worth calculating a few more
  950. compareval's.
  951. eg. compareval1 = (2.0 - 0.4054/istep);
  952. .. and then after the first compare do this ...
  953. if compareval1>*xr then ix = 1;
  954. On a pentium166, it's only worth doing the one compare (as done here),
  955. as the second compare becomes more expensive than just calculating
  956. the value. Architectures with slow FP operations may want to add some
  957. more comparevals. try it and send your diffs statistically speaking
  958. 73% of all xr*istep values give ix=0
  959. 16% will give 1
  960. 4% will give 2
  961. */
  962. for (j=576;j>0;j--) {
  963. if (compareval0 > *xr) {
  964. *(ix++) = 0;
  965. xr++;
  966. } else {
  967. /* *(ix++) = (int)( istep*(*(xr++)) + 0.4054); */
  968. XRPOW_FTOI( istep*(*(xr++)) + ROUNDFAC , *(ix++) );
  969. }
  970. }
  971. }
  972. #endif