quantize_pvt.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. /*
  2. * quantize_pvt source file
  3. *
  4. * Copyright (c) 1999-2002 Takehiro Tominaga
  5. * Copyright (c) 2000-2012 Robert Hegemann
  6. * Copyright (c) 2001 Naoki Shibata
  7. * Copyright (c) 2002-2005 Gabriel Bouvigne
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Library General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Library General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Library General Public
  20. * License along with this library; if not, write to the
  21. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  22. * Boston, MA 02111-1307, USA.
  23. */
  24. /* $Id: quantize_pvt.c,v 1.175 2017/09/06 15:07:30 robert Exp $ */
  25. #ifdef HAVE_CONFIG_H
  26. # include <config.h>
  27. #endif
  28. #include "lame.h"
  29. #include "machine.h"
  30. #include "encoder.h"
  31. #include "util.h"
  32. #include "quantize_pvt.h"
  33. #include "reservoir.h"
  34. #include "lame-analysis.h"
  35. #include <float.h>
  36. #define NSATHSCALE 100 /* Assuming dynamic range=96dB, this value should be 92 */
  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. {6, 5, 5, 5},
  47. {9, 9, 9, 9},
  48. {6, 9, 9, 9}
  49. },
  50. {
  51. {6, 5, 7, 3},
  52. {9, 9, 12, 6},
  53. {6, 9, 12, 6}
  54. },
  55. {
  56. {11, 10, 0, 0},
  57. {18, 18, 0, 0},
  58. {15, 18, 0, 0}
  59. },
  60. {
  61. {7, 7, 7, 0},
  62. {12, 12, 12, 0},
  63. {6, 15, 12, 0}
  64. },
  65. {
  66. {6, 6, 6, 3},
  67. {12, 9, 9, 6},
  68. {6, 12, 9, 6}
  69. },
  70. {
  71. {8, 8, 5, 0},
  72. {15, 12, 9, 0},
  73. {6, 18, 9, 0}
  74. }
  75. };
  76. /* Table B.6: layer3 preemphasis */
  77. const int pretab[SBMAX_l] = {
  78. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  79. 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0
  80. };
  81. /*
  82. Here are MPEG1 Table B.8 and MPEG2 Table B.1
  83. -- Layer III scalefactor bands.
  84. Index into this using a method such as:
  85. idx = fr_ps->header->sampling_frequency
  86. + (fr_ps->header->version * 3)
  87. */
  88. const scalefac_struct sfBandIndex[9] = {
  89. { /* Table B.2.b: 22.05 kHz */
  90. {0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464,
  91. 522, 576},
  92. {0, 4, 8, 12, 18, 24, 32, 42, 56, 74, 100, 132, 174, 192}
  93. , {0, 0, 0, 0, 0, 0, 0} /* sfb21 pseudo sub bands */
  94. , {0, 0, 0, 0, 0, 0, 0} /* sfb12 pseudo sub bands */
  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,
  98. 540, 576},
  99. {0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 136, 180, 192}
  100. , {0, 0, 0, 0, 0, 0, 0} /* sfb21 pseudo sub bands */
  101. , {0, 0, 0, 0, 0, 0, 0} /* sfb12 pseudo sub bands */
  102. },
  103. { /* Table B.2.a: 16 kHz */
  104. {0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464,
  105. 522, 576},
  106. {0, 4, 8, 12, 18, 26, 36, 48, 62, 80, 104, 134, 174, 192}
  107. , {0, 0, 0, 0, 0, 0, 0} /* sfb21 pseudo sub bands */
  108. , {0, 0, 0, 0, 0, 0, 0} /* sfb12 pseudo sub bands */
  109. },
  110. { /* Table B.8.b: 44.1 kHz */
  111. {0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 52, 62, 74, 90, 110, 134, 162, 196, 238, 288, 342, 418,
  112. 576},
  113. {0, 4, 8, 12, 16, 22, 30, 40, 52, 66, 84, 106, 136, 192}
  114. , {0, 0, 0, 0, 0, 0, 0} /* sfb21 pseudo sub bands */
  115. , {0, 0, 0, 0, 0, 0, 0} /* sfb12 pseudo sub bands */
  116. },
  117. { /* Table B.8.c: 48 kHz */
  118. {0, 4, 8, 12, 16, 20, 24, 30, 36, 42, 50, 60, 72, 88, 106, 128, 156, 190, 230, 276, 330, 384,
  119. 576},
  120. {0, 4, 8, 12, 16, 22, 28, 38, 50, 64, 80, 100, 126, 192}
  121. , {0, 0, 0, 0, 0, 0, 0} /* sfb21 pseudo sub bands */
  122. , {0, 0, 0, 0, 0, 0, 0} /* sfb12 pseudo sub bands */
  123. },
  124. { /* Table B.8.a: 32 kHz */
  125. {0, 4, 8, 12, 16, 20, 24, 30, 36, 44, 54, 66, 82, 102, 126, 156, 194, 240, 296, 364, 448, 550,
  126. 576},
  127. {0, 4, 8, 12, 16, 22, 30, 42, 58, 78, 104, 138, 180, 192}
  128. , {0, 0, 0, 0, 0, 0, 0} /* sfb21 pseudo sub bands */
  129. , {0, 0, 0, 0, 0, 0, 0} /* sfb12 pseudo sub bands */
  130. },
  131. { /* MPEG-2.5 11.025 kHz */
  132. {0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464,
  133. 522, 576},
  134. {0 / 3, 12 / 3, 24 / 3, 36 / 3, 54 / 3, 78 / 3, 108 / 3, 144 / 3, 186 / 3, 240 / 3, 312 / 3,
  135. 402 / 3, 522 / 3, 576 / 3}
  136. , {0, 0, 0, 0, 0, 0, 0} /* sfb21 pseudo sub bands */
  137. , {0, 0, 0, 0, 0, 0, 0} /* sfb12 pseudo sub bands */
  138. },
  139. { /* MPEG-2.5 12 kHz */
  140. {0, 6, 12, 18, 24, 30, 36, 44, 54, 66, 80, 96, 116, 140, 168, 200, 238, 284, 336, 396, 464,
  141. 522, 576},
  142. {0 / 3, 12 / 3, 24 / 3, 36 / 3, 54 / 3, 78 / 3, 108 / 3, 144 / 3, 186 / 3, 240 / 3, 312 / 3,
  143. 402 / 3, 522 / 3, 576 / 3}
  144. , {0, 0, 0, 0, 0, 0, 0} /* sfb21 pseudo sub bands */
  145. , {0, 0, 0, 0, 0, 0, 0} /* sfb12 pseudo sub bands */
  146. },
  147. { /* MPEG-2.5 8 kHz */
  148. {0, 12, 24, 36, 48, 60, 72, 88, 108, 132, 160, 192, 232, 280, 336, 400, 476, 566, 568, 570,
  149. 572, 574, 576},
  150. {0 / 3, 24 / 3, 48 / 3, 72 / 3, 108 / 3, 156 / 3, 216 / 3, 288 / 3, 372 / 3, 480 / 3, 486 / 3,
  151. 492 / 3, 498 / 3, 576 / 3}
  152. , {0, 0, 0, 0, 0, 0, 0} /* sfb21 pseudo sub bands */
  153. , {0, 0, 0, 0, 0, 0, 0} /* sfb12 pseudo sub bands */
  154. }
  155. };
  156. /* FIXME: move global variables in some struct */
  157. FLOAT pow20[Q_MAX + Q_MAX2 + 1];
  158. FLOAT ipow20[Q_MAX];
  159. FLOAT pow43[PRECALC_SIZE];
  160. /* initialized in first call to iteration_init */
  161. #ifdef TAKEHIRO_IEEE754_HACK
  162. FLOAT adj43asm[PRECALC_SIZE];
  163. #else
  164. FLOAT adj43[PRECALC_SIZE];
  165. #endif
  166. /*
  167. compute the ATH for each scalefactor band
  168. cd range: 0..96db
  169. Input: 3.3kHz signal 32767 amplitude (3.3kHz is where ATH is smallest = -5db)
  170. longblocks: sfb=12 en0/bw=-11db max_en0 = 1.3db
  171. shortblocks: sfb=5 -9db 0db
  172. Input: 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 (repeated)
  173. longblocks: amp=1 sfb=12 en0/bw=-103 db max_en0 = -92db
  174. amp=32767 sfb=12 -12 db -1.4db
  175. Input: 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 (repeated)
  176. shortblocks: amp=1 sfb=5 en0/bw= -99 -86
  177. amp=32767 sfb=5 -9 db 4db
  178. MAX energy of largest wave at 3.3kHz = 1db
  179. AVE energy of largest wave at 3.3kHz = -11db
  180. Let's take AVE: -11db = maximum signal in sfb=12.
  181. Dynamic range of CD: 96db. Therefor energy of smallest audible wave
  182. in sfb=12 = -11 - 96 = -107db = ATH at 3.3kHz.
  183. ATH formula for this wave: -5db. To adjust to LAME scaling, we need
  184. ATH = ATH_formula - 103 (db)
  185. ATH = ATH * 2.5e-10 (ener)
  186. */
  187. static FLOAT
  188. ATHmdct(SessionConfig_t const *cfg, FLOAT f)
  189. {
  190. FLOAT ath;
  191. ath = ATHformula(cfg, f);
  192. if (cfg->ATHfixpoint > 0) {
  193. ath -= cfg->ATHfixpoint;
  194. }
  195. else {
  196. ath -= NSATHSCALE;
  197. }
  198. ath += cfg->ATH_offset_db;
  199. /* modify the MDCT scaling for the ATH and convert to energy */
  200. ath = powf(10.0f, ath * 0.1f);
  201. return ath;
  202. }
  203. static void
  204. compute_ath(lame_internal_flags const* gfc)
  205. {
  206. SessionConfig_t const *const cfg = &gfc->cfg;
  207. FLOAT *const ATH_l = gfc->ATH->l;
  208. FLOAT *const ATH_psfb21 = gfc->ATH->psfb21;
  209. FLOAT *const ATH_s = gfc->ATH->s;
  210. FLOAT *const ATH_psfb12 = gfc->ATH->psfb12;
  211. int sfb, i, start, end;
  212. FLOAT ATH_f;
  213. FLOAT const samp_freq = cfg->samplerate_out;
  214. for (sfb = 0; sfb < SBMAX_l; sfb++) {
  215. start = gfc->scalefac_band.l[sfb];
  216. end = gfc->scalefac_band.l[sfb + 1];
  217. ATH_l[sfb] = FLOAT_MAX;
  218. for (i = start; i < end; i++) {
  219. FLOAT const freq = i * samp_freq / (2 * 576);
  220. ATH_f = ATHmdct(cfg, freq); /* freq in kHz */
  221. ATH_l[sfb] = Min(ATH_l[sfb], ATH_f);
  222. }
  223. }
  224. for (sfb = 0; sfb < PSFB21; sfb++) {
  225. start = gfc->scalefac_band.psfb21[sfb];
  226. end = gfc->scalefac_band.psfb21[sfb + 1];
  227. ATH_psfb21[sfb] = FLOAT_MAX;
  228. for (i = start; i < end; i++) {
  229. FLOAT const freq = i * samp_freq / (2 * 576);
  230. ATH_f = ATHmdct(cfg, freq); /* freq in kHz */
  231. ATH_psfb21[sfb] = Min(ATH_psfb21[sfb], ATH_f);
  232. }
  233. }
  234. for (sfb = 0; sfb < SBMAX_s; sfb++) {
  235. start = gfc->scalefac_band.s[sfb];
  236. end = gfc->scalefac_band.s[sfb + 1];
  237. ATH_s[sfb] = FLOAT_MAX;
  238. for (i = start; i < end; i++) {
  239. FLOAT const freq = i * samp_freq / (2 * 192);
  240. ATH_f = ATHmdct(cfg, freq); /* freq in kHz */
  241. ATH_s[sfb] = Min(ATH_s[sfb], ATH_f);
  242. }
  243. ATH_s[sfb] *= (gfc->scalefac_band.s[sfb + 1] - gfc->scalefac_band.s[sfb]);
  244. }
  245. for (sfb = 0; sfb < PSFB12; sfb++) {
  246. start = gfc->scalefac_band.psfb12[sfb];
  247. end = gfc->scalefac_band.psfb12[sfb + 1];
  248. ATH_psfb12[sfb] = FLOAT_MAX;
  249. for (i = start; i < end; i++) {
  250. FLOAT const freq = i * samp_freq / (2 * 192);
  251. ATH_f = ATHmdct(cfg, freq); /* freq in kHz */
  252. ATH_psfb12[sfb] = Min(ATH_psfb12[sfb], ATH_f);
  253. }
  254. /*not sure about the following */
  255. ATH_psfb12[sfb] *= (gfc->scalefac_band.s[13] - gfc->scalefac_band.s[12]);
  256. }
  257. /* no-ATH mode:
  258. * reduce ATH to -200 dB
  259. */
  260. if (cfg->noATH) {
  261. for (sfb = 0; sfb < SBMAX_l; sfb++) {
  262. ATH_l[sfb] = 1E-20;
  263. }
  264. for (sfb = 0; sfb < PSFB21; sfb++) {
  265. ATH_psfb21[sfb] = 1E-20;
  266. }
  267. for (sfb = 0; sfb < SBMAX_s; sfb++) {
  268. ATH_s[sfb] = 1E-20;
  269. }
  270. for (sfb = 0; sfb < PSFB12; sfb++) {
  271. ATH_psfb12[sfb] = 1E-20;
  272. }
  273. }
  274. /* work in progress, don't rely on it too much
  275. */
  276. gfc->ATH->floor = 10. * log10(ATHmdct(cfg, -1.));
  277. /*
  278. { FLOAT g=10000, t=1e30, x;
  279. for ( f = 100; f < 10000; f++ ) {
  280. x = ATHmdct( cfg, f );
  281. if ( t > x ) t = x, g = f;
  282. }
  283. printf("min=%g\n", g);
  284. } */
  285. }
  286. static float const payload_long[2][4] =
  287. { {-0.000f, -0.000f, -0.000f, +0.000f}
  288. , {-0.500f, -0.250f, -0.025f, +0.500f}
  289. };
  290. static float const payload_short[2][4] =
  291. { {-0.000f, -0.000f, -0.000f, +0.000f}
  292. , {-2.000f, -1.000f, -0.050f, +0.500f}
  293. };
  294. /************************************************************************/
  295. /* initialization for iteration_loop */
  296. /************************************************************************/
  297. void
  298. iteration_init(lame_internal_flags * gfc)
  299. {
  300. SessionConfig_t const *const cfg = &gfc->cfg;
  301. III_side_info_t *const l3_side = &gfc->l3_side;
  302. FLOAT adjust, db;
  303. int i, sel;
  304. if (gfc->iteration_init_init == 0) {
  305. gfc->iteration_init_init = 1;
  306. l3_side->main_data_begin = 0;
  307. compute_ath(gfc);
  308. pow43[0] = 0.0;
  309. for (i = 1; i < PRECALC_SIZE; i++)
  310. pow43[i] = pow((FLOAT) i, 4.0 / 3.0);
  311. #ifdef TAKEHIRO_IEEE754_HACK
  312. adj43asm[0] = 0.0;
  313. for (i = 1; i < PRECALC_SIZE; i++)
  314. adj43asm[i] = i - 0.5 - pow(0.5 * (pow43[i - 1] + pow43[i]), 0.75);
  315. #else
  316. for (i = 0; i < PRECALC_SIZE - 1; i++)
  317. adj43[i] = (i + 1) - pow(0.5 * (pow43[i] + pow43[i + 1]), 0.75);
  318. adj43[i] = 0.5;
  319. #endif
  320. for (i = 0; i < Q_MAX; i++)
  321. ipow20[i] = pow(2.0, (double) (i - 210) * -0.1875);
  322. for (i = 0; i <= Q_MAX + Q_MAX2; i++)
  323. pow20[i] = pow(2.0, (double) (i - 210 - Q_MAX2) * 0.25);
  324. huffman_init(gfc);
  325. init_xrpow_core_init(gfc);
  326. sel = 1;/* RH: all modes like vbr-new (cfg->vbr == vbr_mt || cfg->vbr == vbr_mtrh) ? 1 : 0;*/
  327. /* long */
  328. db = cfg->adjust_bass_db + payload_long[sel][0];
  329. adjust = powf(10.f, db * 0.1f);
  330. for (i = 0; i <= 6; ++i) {
  331. gfc->sv_qnt.longfact[i] = adjust;
  332. }
  333. db = cfg->adjust_alto_db + payload_long[sel][1];
  334. adjust = powf(10.f, db * 0.1f);
  335. for (; i <= 13; ++i) {
  336. gfc->sv_qnt.longfact[i] = adjust;
  337. }
  338. db = cfg->adjust_treble_db + payload_long[sel][2];
  339. adjust = powf(10.f, db * 0.1f);
  340. for (; i <= 20; ++i) {
  341. gfc->sv_qnt.longfact[i] = adjust;
  342. }
  343. db = cfg->adjust_sfb21_db + payload_long[sel][3];
  344. adjust = powf(10.f, db * 0.1f);
  345. for (; i < SBMAX_l; ++i) {
  346. gfc->sv_qnt.longfact[i] = adjust;
  347. }
  348. /* short */
  349. db = cfg->adjust_bass_db + payload_short[sel][0];
  350. adjust = powf(10.f, db * 0.1f);
  351. for (i = 0; i <= 2; ++i) {
  352. gfc->sv_qnt.shortfact[i] = adjust;
  353. }
  354. db = cfg->adjust_alto_db + payload_short[sel][1];
  355. adjust = powf(10.f, db * 0.1f);
  356. for (; i <= 6; ++i) {
  357. gfc->sv_qnt.shortfact[i] = adjust;
  358. }
  359. db = cfg->adjust_treble_db + payload_short[sel][2];
  360. adjust = powf(10.f, db * 0.1f);
  361. for (; i <= 11; ++i) {
  362. gfc->sv_qnt.shortfact[i] = adjust;
  363. }
  364. db = cfg->adjust_sfb21_db + payload_short[sel][3];
  365. adjust = powf(10.f, db * 0.1f);
  366. for (; i < SBMAX_s; ++i) {
  367. gfc->sv_qnt.shortfact[i] = adjust;
  368. }
  369. }
  370. }
  371. /************************************************************************
  372. * allocate bits among 2 channels based on PE
  373. * mt 6/99
  374. * bugfixes rh 8/01: often allocated more than the allowed 4095 bits
  375. ************************************************************************/
  376. int
  377. on_pe(lame_internal_flags * gfc, const FLOAT pe[][2], int targ_bits[2], int mean_bits, int gr, int cbr)
  378. {
  379. SessionConfig_t const *const cfg = &gfc->cfg;
  380. int extra_bits = 0, tbits, bits;
  381. int add_bits[2] = {0, 0};
  382. int max_bits; /* maximum allowed bits for this granule */
  383. int ch;
  384. /* allocate targ_bits for granule */
  385. ResvMaxBits(gfc, mean_bits, &tbits, &extra_bits, cbr);
  386. max_bits = tbits + extra_bits;
  387. if (max_bits > MAX_BITS_PER_GRANULE) /* hard limit per granule */
  388. max_bits = MAX_BITS_PER_GRANULE;
  389. for (bits = 0, ch = 0; ch < cfg->channels_out; ++ch) {
  390. /******************************************************************
  391. * allocate bits for each channel
  392. ******************************************************************/
  393. targ_bits[ch] = Min(MAX_BITS_PER_CHANNEL, tbits / cfg->channels_out);
  394. add_bits[ch] = targ_bits[ch] * pe[gr][ch] / 700.0 - targ_bits[ch];
  395. /* at most increase bits by 1.5*average */
  396. if (add_bits[ch] > mean_bits * 3 / 4)
  397. add_bits[ch] = mean_bits * 3 / 4;
  398. if (add_bits[ch] < 0)
  399. add_bits[ch] = 0;
  400. if (add_bits[ch] + targ_bits[ch] > MAX_BITS_PER_CHANNEL)
  401. add_bits[ch] = Max(0, MAX_BITS_PER_CHANNEL - targ_bits[ch]);
  402. bits += add_bits[ch];
  403. }
  404. if (bits > extra_bits && bits > 0) {
  405. for (ch = 0; ch < cfg->channels_out; ++ch) {
  406. add_bits[ch] = extra_bits * add_bits[ch] / bits;
  407. }
  408. }
  409. for (ch = 0; ch < cfg->channels_out; ++ch) {
  410. targ_bits[ch] += add_bits[ch];
  411. extra_bits -= add_bits[ch];
  412. }
  413. for (bits = 0, ch = 0; ch < cfg->channels_out; ++ch) {
  414. bits += targ_bits[ch];
  415. }
  416. if (bits > MAX_BITS_PER_GRANULE) {
  417. int sum = 0;
  418. for (ch = 0; ch < cfg->channels_out; ++ch) {
  419. targ_bits[ch] *= MAX_BITS_PER_GRANULE;
  420. targ_bits[ch] /= bits;
  421. sum += targ_bits[ch];
  422. }
  423. assert(sum <= MAX_BITS_PER_GRANULE);
  424. }
  425. return max_bits;
  426. }
  427. void
  428. reduce_side(int targ_bits[2], FLOAT ms_ener_ratio, int mean_bits, int max_bits)
  429. {
  430. int move_bits;
  431. FLOAT fac;
  432. assert(max_bits <= MAX_BITS_PER_GRANULE);
  433. assert(targ_bits[0] + targ_bits[1] <= MAX_BITS_PER_GRANULE);
  434. /* ms_ener_ratio = 0: allocate 66/33 mid/side fac=.33
  435. * ms_ener_ratio =.5: allocate 50/50 mid/side fac= 0 */
  436. /* 75/25 split is fac=.5 */
  437. /* float fac = .50*(.5-ms_ener_ratio[gr])/.5; */
  438. fac = .33 * (.5 - ms_ener_ratio) / .5;
  439. if (fac < 0)
  440. fac = 0;
  441. if (fac > .5)
  442. fac = .5;
  443. /* number of bits to move from side channel to mid channel */
  444. /* move_bits = fac*targ_bits[1]; */
  445. move_bits = fac * .5 * (targ_bits[0] + targ_bits[1]);
  446. if (move_bits > MAX_BITS_PER_CHANNEL - targ_bits[0]) {
  447. move_bits = MAX_BITS_PER_CHANNEL - targ_bits[0];
  448. }
  449. if (move_bits < 0)
  450. move_bits = 0;
  451. if (targ_bits[1] >= 125) {
  452. /* dont reduce side channel below 125 bits */
  453. if (targ_bits[1] - move_bits > 125) {
  454. /* if mid channel already has 2x more than average, dont bother */
  455. /* mean_bits = bits per granule (for both channels) */
  456. if (targ_bits[0] < mean_bits)
  457. targ_bits[0] += move_bits;
  458. targ_bits[1] -= move_bits;
  459. }
  460. else {
  461. targ_bits[0] += targ_bits[1] - 125;
  462. targ_bits[1] = 125;
  463. }
  464. }
  465. move_bits = targ_bits[0] + targ_bits[1];
  466. if (move_bits > max_bits) {
  467. targ_bits[0] = (max_bits * targ_bits[0]) / move_bits;
  468. targ_bits[1] = (max_bits * targ_bits[1]) / move_bits;
  469. }
  470. assert(targ_bits[0] <= MAX_BITS_PER_CHANNEL);
  471. assert(targ_bits[1] <= MAX_BITS_PER_CHANNEL);
  472. assert(targ_bits[0] + targ_bits[1] <= MAX_BITS_PER_GRANULE);
  473. }
  474. /**
  475. * Robert Hegemann 2001-04-27:
  476. * this adjusts the ATH, keeping the original noise floor
  477. * affects the higher frequencies more than the lower ones
  478. */
  479. FLOAT
  480. athAdjust(FLOAT a, FLOAT x, FLOAT athFloor, float ATHfixpoint)
  481. {
  482. /* work in progress
  483. */
  484. FLOAT const o = 90.30873362f;
  485. FLOAT const p = (ATHfixpoint < 1.f) ? 94.82444863f : ATHfixpoint;
  486. FLOAT u = FAST_LOG10_X(x, 10.0f);
  487. FLOAT const v = a * a;
  488. FLOAT w = 0.0f;
  489. u -= athFloor; /* undo scaling */
  490. if (v > 1E-20f)
  491. w = 1.f + FAST_LOG10_X(v, 10.0f / o);
  492. if (w < 0)
  493. w = 0.f;
  494. u *= w;
  495. u += athFloor + o - p; /* redo scaling */
  496. return powf(10.f, 0.1f * u);
  497. }
  498. /*************************************************************************/
  499. /* calc_xmin */
  500. /*************************************************************************/
  501. /*
  502. Calculate the allowed distortion for each scalefactor band,
  503. as determined by the psychoacoustic model.
  504. xmin(sb) = ratio(sb) * en(sb) / bw(sb)
  505. returns number of sfb's with energy > ATH
  506. */
  507. int
  508. calc_xmin(lame_internal_flags const *gfc,
  509. III_psy_ratio const *const ratio, gr_info * const cod_info, FLOAT * pxmin)
  510. {
  511. SessionConfig_t const *const cfg = &gfc->cfg;
  512. int sfb, gsfb, j = 0, ath_over = 0, k;
  513. ATH_t const *const ATH = gfc->ATH;
  514. const FLOAT *const xr = cod_info->xr;
  515. int max_nonzero;
  516. for (gsfb = 0; gsfb < cod_info->psy_lmax; gsfb++) {
  517. FLOAT en0, xmin;
  518. FLOAT rh1, rh2, rh3;
  519. int width, l;
  520. xmin = athAdjust(ATH->adjust_factor, ATH->l[gsfb], ATH->floor, cfg->ATHfixpoint);
  521. xmin *= gfc->sv_qnt.longfact[gsfb];
  522. width = cod_info->width[gsfb];
  523. rh1 = xmin / width;
  524. #ifdef DBL_EPSILON
  525. rh2 = DBL_EPSILON;
  526. #else
  527. rh2 = 2.2204460492503131e-016;
  528. #endif
  529. en0 = 0.0;
  530. for (l = 0; l < width; ++l) {
  531. FLOAT const xa = xr[j++];
  532. FLOAT const x2 = xa * xa;
  533. en0 += x2;
  534. rh2 += (x2 < rh1) ? x2 : rh1;
  535. }
  536. if (en0 > xmin)
  537. ath_over++;
  538. if (en0 < xmin) {
  539. rh3 = en0;
  540. }
  541. else if (rh2 < xmin) {
  542. rh3 = xmin;
  543. }
  544. else {
  545. rh3 = rh2;
  546. }
  547. xmin = rh3;
  548. {
  549. FLOAT const e = ratio->en.l[gsfb];
  550. if (e > 1e-12f) {
  551. FLOAT x;
  552. x = en0 * ratio->thm.l[gsfb] / e;
  553. x *= gfc->sv_qnt.longfact[gsfb];
  554. if (xmin < x)
  555. xmin = x;
  556. }
  557. }
  558. xmin = Max(xmin, DBL_EPSILON);
  559. cod_info->energy_above_cutoff[gsfb] = (en0 > xmin+1e-14f) ? 1 : 0;
  560. *pxmin++ = xmin;
  561. } /* end of long block loop */
  562. /*use this function to determine the highest non-zero coeff */
  563. max_nonzero = 0;
  564. for (k = 575; k > 0; --k) {
  565. if (fabs(xr[k]) > 1e-12f) {
  566. max_nonzero = k;
  567. break;
  568. }
  569. }
  570. if (cod_info->block_type != SHORT_TYPE) { /* NORM, START or STOP type, but not SHORT */
  571. max_nonzero |= 1; /* only odd numbers */
  572. }
  573. else {
  574. max_nonzero /= 6; /* 3 short blocks */
  575. max_nonzero *= 6;
  576. max_nonzero += 5;
  577. }
  578. if (gfc->sv_qnt.sfb21_extra == 0 && cfg->samplerate_out < 44000) {
  579. int const sfb_l = (cfg->samplerate_out <= 8000) ? 17 : 21;
  580. int const sfb_s = (cfg->samplerate_out <= 8000) ? 9 : 12;
  581. int limit = 575;
  582. if (cod_info->block_type != SHORT_TYPE) { /* NORM, START or STOP type, but not SHORT */
  583. limit = gfc->scalefac_band.l[sfb_l]-1;
  584. }
  585. else {
  586. limit = 3*gfc->scalefac_band.s[sfb_s]-1;
  587. }
  588. if (max_nonzero > limit) {
  589. max_nonzero = limit;
  590. }
  591. }
  592. cod_info->max_nonzero_coeff = max_nonzero;
  593. for (sfb = cod_info->sfb_smin; gsfb < cod_info->psymax; sfb++, gsfb += 3) {
  594. int width, b, l;
  595. FLOAT tmpATH;
  596. tmpATH = athAdjust(ATH->adjust_factor, ATH->s[sfb], ATH->floor, cfg->ATHfixpoint);
  597. tmpATH *= gfc->sv_qnt.shortfact[sfb];
  598. width = cod_info->width[gsfb];
  599. for (b = 0; b < 3; b++) {
  600. FLOAT en0 = 0.0, xmin = tmpATH;
  601. FLOAT rh1, rh2, rh3;
  602. rh1 = tmpATH / width;
  603. #ifdef DBL_EPSILON
  604. rh2 = DBL_EPSILON;
  605. #else
  606. rh2 = 2.2204460492503131e-016;
  607. #endif
  608. for (l = 0; l < width; ++l) {
  609. FLOAT const xa = xr[j++];
  610. FLOAT const x2 = xa * xa;
  611. en0 += x2;
  612. rh2 += (x2 < rh1) ? x2 : rh1;
  613. }
  614. if (en0 > tmpATH)
  615. ath_over++;
  616. if (en0 < tmpATH) {
  617. rh3 = en0;
  618. }
  619. else if (rh2 < tmpATH) {
  620. rh3 = tmpATH;
  621. }
  622. else {
  623. rh3 = rh2;
  624. }
  625. xmin = rh3;
  626. {
  627. FLOAT const e = ratio->en.s[sfb][b];
  628. if (e > 1e-12f) {
  629. FLOAT x;
  630. x = en0 * ratio->thm.s[sfb][b] / e;
  631. x *= gfc->sv_qnt.shortfact[sfb];
  632. if (xmin < x)
  633. xmin = x;
  634. }
  635. }
  636. xmin = Max(xmin, DBL_EPSILON);
  637. cod_info->energy_above_cutoff[gsfb+b] = (en0 > xmin+1e-14f) ? 1 : 0;
  638. *pxmin++ = xmin;
  639. } /* b */
  640. if (cfg->use_temporal_masking_effect) {
  641. if (pxmin[-3] > pxmin[-3 + 1])
  642. pxmin[-3 + 1] += (pxmin[-3] - pxmin[-3 + 1]) * gfc->cd_psy->decay;
  643. if (pxmin[-3 + 1] > pxmin[-3 + 2])
  644. pxmin[-3 + 2] += (pxmin[-3 + 1] - pxmin[-3 + 2]) * gfc->cd_psy->decay;
  645. }
  646. } /* end of short block sfb loop */
  647. return ath_over;
  648. }
  649. static FLOAT
  650. calc_noise_core_c(const gr_info * const cod_info, int *startline, int l, FLOAT step)
  651. {
  652. FLOAT noise = 0;
  653. int j = *startline;
  654. const int *const ix = cod_info->l3_enc;
  655. if (j > cod_info->count1) {
  656. while (l--) {
  657. FLOAT temp;
  658. temp = cod_info->xr[j];
  659. j++;
  660. noise += temp * temp;
  661. temp = cod_info->xr[j];
  662. j++;
  663. noise += temp * temp;
  664. }
  665. }
  666. else if (j > cod_info->big_values) {
  667. FLOAT ix01[2];
  668. ix01[0] = 0;
  669. ix01[1] = step;
  670. while (l--) {
  671. FLOAT temp;
  672. temp = fabs(cod_info->xr[j]) - ix01[ix[j]];
  673. j++;
  674. noise += temp * temp;
  675. temp = fabs(cod_info->xr[j]) - ix01[ix[j]];
  676. j++;
  677. noise += temp * temp;
  678. }
  679. }
  680. else {
  681. while (l--) {
  682. FLOAT temp;
  683. temp = fabs(cod_info->xr[j]) - pow43[ix[j]] * step;
  684. j++;
  685. noise += temp * temp;
  686. temp = fabs(cod_info->xr[j]) - pow43[ix[j]] * step;
  687. j++;
  688. noise += temp * temp;
  689. }
  690. }
  691. *startline = j;
  692. return noise;
  693. }
  694. /*************************************************************************/
  695. /* calc_noise */
  696. /*************************************************************************/
  697. /* -oo dB => -1.00 */
  698. /* - 6 dB => -0.97 */
  699. /* - 3 dB => -0.80 */
  700. /* - 2 dB => -0.64 */
  701. /* - 1 dB => -0.38 */
  702. /* 0 dB => 0.00 */
  703. /* + 1 dB => +0.49 */
  704. /* + 2 dB => +1.06 */
  705. /* + 3 dB => +1.68 */
  706. /* + 6 dB => +3.69 */
  707. /* +10 dB => +6.45 */
  708. int
  709. calc_noise(gr_info const *const cod_info,
  710. FLOAT const *l3_xmin,
  711. FLOAT * distort, calc_noise_result * const res, calc_noise_data * prev_noise)
  712. {
  713. int sfb, l, over = 0;
  714. FLOAT over_noise_db = 0;
  715. FLOAT tot_noise_db = 0; /* 0 dB relative to masking */
  716. FLOAT max_noise = -20.0; /* -200 dB relative to masking */
  717. int j = 0;
  718. const int *scalefac = cod_info->scalefac;
  719. res->over_SSD = 0;
  720. for (sfb = 0; sfb < cod_info->psymax; sfb++) {
  721. int const s =
  722. cod_info->global_gain - (((*scalefac++) + (cod_info->preflag ? pretab[sfb] : 0))
  723. << (cod_info->scalefac_scale + 1))
  724. - cod_info->subblock_gain[cod_info->window[sfb]] * 8;
  725. FLOAT const r_l3_xmin = 1.f / *l3_xmin++;
  726. FLOAT distort_ = 0.0f;
  727. FLOAT noise = 0.0f;
  728. if (prev_noise && (prev_noise->step[sfb] == s)) {
  729. /* use previously computed values */
  730. j += cod_info->width[sfb];
  731. distort_ = r_l3_xmin * prev_noise->noise[sfb];
  732. noise = prev_noise->noise_log[sfb];
  733. }
  734. else {
  735. FLOAT const step = POW20(s);
  736. l = cod_info->width[sfb] >> 1;
  737. if ((j + cod_info->width[sfb]) > cod_info->max_nonzero_coeff) {
  738. int usefullsize;
  739. usefullsize = cod_info->max_nonzero_coeff - j + 1;
  740. if (usefullsize > 0)
  741. l = usefullsize >> 1;
  742. else
  743. l = 0;
  744. }
  745. noise = calc_noise_core_c(cod_info, &j, l, step);
  746. if (prev_noise) {
  747. /* save noise values */
  748. prev_noise->step[sfb] = s;
  749. prev_noise->noise[sfb] = noise;
  750. }
  751. distort_ = r_l3_xmin * noise;
  752. /* multiplying here is adding in dB, but can overflow */
  753. noise = FAST_LOG10(Max(distort_, 1E-20f));
  754. if (prev_noise) {
  755. /* save noise values */
  756. prev_noise->noise_log[sfb] = noise;
  757. }
  758. }
  759. *distort++ = distort_;
  760. if (prev_noise) {
  761. /* save noise values */
  762. prev_noise->global_gain = cod_info->global_gain;;
  763. }
  764. /*tot_noise *= Max(noise, 1E-20); */
  765. tot_noise_db += noise;
  766. if (noise > 0.0) {
  767. int tmp;
  768. tmp = Max((int) (noise * 10 + .5), 1);
  769. res->over_SSD += tmp * tmp;
  770. over++;
  771. /* multiplying here is adding in dB -but can overflow */
  772. /*over_noise *= noise; */
  773. over_noise_db += noise;
  774. }
  775. max_noise = Max(max_noise, noise);
  776. }
  777. res->over_count = over;
  778. res->tot_noise = tot_noise_db;
  779. res->over_noise = over_noise_db;
  780. res->max_noise = max_noise;
  781. return over;
  782. }
  783. /************************************************************************
  784. *
  785. * set_pinfo()
  786. *
  787. * updates plotting data
  788. *
  789. * Mark Taylor 2000-??-??
  790. *
  791. * Robert Hegemann: moved noise/distortion calc into it
  792. *
  793. ************************************************************************/
  794. static void
  795. set_pinfo(lame_internal_flags const *gfc,
  796. gr_info * const cod_info, const III_psy_ratio * const ratio, const int gr, const int ch)
  797. {
  798. SessionConfig_t const *const cfg = &gfc->cfg;
  799. int sfb, sfb2;
  800. int j, i, l, start, end, bw;
  801. FLOAT en0, en1;
  802. FLOAT const ifqstep = (cod_info->scalefac_scale == 0) ? .5 : 1.0;
  803. int const *const scalefac = cod_info->scalefac;
  804. FLOAT l3_xmin[SFBMAX], xfsf[SFBMAX];
  805. calc_noise_result noise;
  806. (void) calc_xmin(gfc, ratio, cod_info, l3_xmin);
  807. (void) calc_noise(cod_info, l3_xmin, xfsf, &noise, 0);
  808. j = 0;
  809. sfb2 = cod_info->sfb_lmax;
  810. if (cod_info->block_type != SHORT_TYPE && !cod_info->mixed_block_flag)
  811. sfb2 = 22;
  812. for (sfb = 0; sfb < sfb2; sfb++) {
  813. start = gfc->scalefac_band.l[sfb];
  814. end = gfc->scalefac_band.l[sfb + 1];
  815. bw = end - start;
  816. for (en0 = 0.0; j < end; j++)
  817. en0 += cod_info->xr[j] * cod_info->xr[j];
  818. en0 /= bw;
  819. /* convert to MDCT units */
  820. en1 = 1e15; /* scaling so it shows up on FFT plot */
  821. gfc->pinfo->en[gr][ch][sfb] = en1 * en0;
  822. gfc->pinfo->xfsf[gr][ch][sfb] = en1 * l3_xmin[sfb] * xfsf[sfb] / bw;
  823. if (ratio->en.l[sfb] > 0 && !cfg->ATHonly)
  824. en0 = en0 / ratio->en.l[sfb];
  825. else
  826. en0 = 0.0;
  827. gfc->pinfo->thr[gr][ch][sfb] = en1 * Max(en0 * ratio->thm.l[sfb], gfc->ATH->l[sfb]);
  828. /* there is no scalefactor bands >= SBPSY_l */
  829. gfc->pinfo->LAMEsfb[gr][ch][sfb] = 0;
  830. if (cod_info->preflag && sfb >= 11)
  831. gfc->pinfo->LAMEsfb[gr][ch][sfb] = -ifqstep * pretab[sfb];
  832. if (sfb < SBPSY_l) {
  833. assert(scalefac[sfb] >= 0); /* scfsi should be decoded by caller side */
  834. gfc->pinfo->LAMEsfb[gr][ch][sfb] -= ifqstep * scalefac[sfb];
  835. }
  836. } /* for sfb */
  837. if (cod_info->block_type == SHORT_TYPE) {
  838. sfb2 = sfb;
  839. for (sfb = cod_info->sfb_smin; sfb < SBMAX_s; sfb++) {
  840. start = gfc->scalefac_band.s[sfb];
  841. end = gfc->scalefac_band.s[sfb + 1];
  842. bw = end - start;
  843. for (i = 0; i < 3; i++) {
  844. for (en0 = 0.0, l = start; l < end; l++) {
  845. en0 += cod_info->xr[j] * cod_info->xr[j];
  846. j++;
  847. }
  848. en0 = Max(en0 / bw, 1e-20);
  849. /* convert to MDCT units */
  850. en1 = 1e15; /* scaling so it shows up on FFT plot */
  851. gfc->pinfo->en_s[gr][ch][3 * sfb + i] = en1 * en0;
  852. gfc->pinfo->xfsf_s[gr][ch][3 * sfb + i] = en1 * l3_xmin[sfb2] * xfsf[sfb2] / bw;
  853. if (ratio->en.s[sfb][i] > 0)
  854. en0 = en0 / ratio->en.s[sfb][i];
  855. else
  856. en0 = 0.0;
  857. if (cfg->ATHonly || cfg->ATHshort)
  858. en0 = 0;
  859. gfc->pinfo->thr_s[gr][ch][3 * sfb + i] =
  860. en1 * Max(en0 * ratio->thm.s[sfb][i], gfc->ATH->s[sfb]);
  861. /* there is no scalefactor bands >= SBPSY_s */
  862. gfc->pinfo->LAMEsfb_s[gr][ch][3 * sfb + i]
  863. = -2.0 * cod_info->subblock_gain[i];
  864. if (sfb < SBPSY_s) {
  865. gfc->pinfo->LAMEsfb_s[gr][ch][3 * sfb + i] -= ifqstep * scalefac[sfb2];
  866. }
  867. sfb2++;
  868. }
  869. }
  870. } /* block type short */
  871. gfc->pinfo->LAMEqss[gr][ch] = cod_info->global_gain;
  872. gfc->pinfo->LAMEmainbits[gr][ch] = cod_info->part2_3_length + cod_info->part2_length;
  873. gfc->pinfo->LAMEsfbits[gr][ch] = cod_info->part2_length;
  874. gfc->pinfo->over[gr][ch] = noise.over_count;
  875. gfc->pinfo->max_noise[gr][ch] = noise.max_noise * 10.0;
  876. gfc->pinfo->over_noise[gr][ch] = noise.over_noise * 10.0;
  877. gfc->pinfo->tot_noise[gr][ch] = noise.tot_noise * 10.0;
  878. gfc->pinfo->over_SSD[gr][ch] = noise.over_SSD;
  879. }
  880. /************************************************************************
  881. *
  882. * set_frame_pinfo()
  883. *
  884. * updates plotting data for a whole frame
  885. *
  886. * Robert Hegemann 2000-10-21
  887. *
  888. ************************************************************************/
  889. void
  890. set_frame_pinfo(lame_internal_flags * gfc, const III_psy_ratio ratio[2][2])
  891. {
  892. SessionConfig_t const *const cfg = &gfc->cfg;
  893. int ch;
  894. int gr;
  895. /* for every granule and channel patch l3_enc and set info
  896. */
  897. for (gr = 0; gr < cfg->mode_gr; gr++) {
  898. for (ch = 0; ch < cfg->channels_out; ch++) {
  899. gr_info *const cod_info = &gfc->l3_side.tt[gr][ch];
  900. int scalefac_sav[SFBMAX];
  901. memcpy(scalefac_sav, cod_info->scalefac, sizeof(scalefac_sav));
  902. /* reconstruct the scalefactors in case SCFSI was used
  903. */
  904. if (gr == 1) {
  905. int sfb;
  906. for (sfb = 0; sfb < cod_info->sfb_lmax; sfb++) {
  907. if (cod_info->scalefac[sfb] < 0) /* scfsi */
  908. cod_info->scalefac[sfb] = gfc->l3_side.tt[0][ch].scalefac[sfb];
  909. }
  910. }
  911. set_pinfo(gfc, cod_info, &ratio[gr][ch], gr, ch);
  912. memcpy(cod_info->scalefac, scalefac_sav, sizeof(scalefac_sav));
  913. } /* for ch */
  914. } /* for gr */
  915. }