takehiro.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  1. /*
  2. * MP3 huffman table selecting and bit counting
  3. *
  4. * Copyright (c) 1999-2005 Takehiro TOMINAGA
  5. * Copyright (c) 2002-2005 Gabriel Bouvigne
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. */
  22. /* $Id: takehiro.c,v 1.80 2017/09/06 15:07:30 robert Exp $ */
  23. #ifdef HAVE_CONFIG_H
  24. # include <config.h>
  25. #endif
  26. #include "lame.h"
  27. #include "machine.h"
  28. #include "encoder.h"
  29. #include "util.h"
  30. #include "quantize_pvt.h"
  31. #include "tables.h"
  32. static const struct {
  33. const int region0_count;
  34. const int region1_count;
  35. } subdv_table[23] = {
  36. {
  37. 0, 0}, /* 0 bands */
  38. {
  39. 0, 0}, /* 1 bands */
  40. {
  41. 0, 0}, /* 2 bands */
  42. {
  43. 0, 0}, /* 3 bands */
  44. {
  45. 0, 0}, /* 4 bands */
  46. {
  47. 0, 1}, /* 5 bands */
  48. {
  49. 1, 1}, /* 6 bands */
  50. {
  51. 1, 1}, /* 7 bands */
  52. {
  53. 1, 2}, /* 8 bands */
  54. {
  55. 2, 2}, /* 9 bands */
  56. {
  57. 2, 3}, /* 10 bands */
  58. {
  59. 2, 3}, /* 11 bands */
  60. {
  61. 3, 4}, /* 12 bands */
  62. {
  63. 3, 4}, /* 13 bands */
  64. {
  65. 3, 4}, /* 14 bands */
  66. {
  67. 4, 5}, /* 15 bands */
  68. {
  69. 4, 5}, /* 16 bands */
  70. {
  71. 4, 6}, /* 17 bands */
  72. {
  73. 5, 6}, /* 18 bands */
  74. {
  75. 5, 6}, /* 19 bands */
  76. {
  77. 5, 7}, /* 20 bands */
  78. {
  79. 6, 7}, /* 21 bands */
  80. {
  81. 6, 7}, /* 22 bands */
  82. };
  83. /*********************************************************************
  84. * nonlinear quantization of xr
  85. * More accurate formula than the ISO formula. Takes into account
  86. * the fact that we are quantizing xr -> ix, but we want ix^4/3 to be
  87. * as close as possible to x^4/3. (taking the nearest int would mean
  88. * ix is as close as possible to xr, which is different.)
  89. *
  90. * From Segher Boessenkool <[email protected]> 11/1999
  91. *
  92. * 09/2000: ASM code removed in favor of IEEE754 hack by Takehiro
  93. * Tominaga. If you need the ASM code, check CVS circa Aug 2000.
  94. *
  95. * 01/2004: Optimizations by Gabriel Bouvigne
  96. *********************************************************************/
  97. static void
  98. quantize_lines_xrpow_01(unsigned int l, FLOAT istep, const FLOAT * xr, int *ix)
  99. {
  100. const FLOAT compareval0 = (1.0f - 0.4054f) / istep;
  101. unsigned int i;
  102. assert(l > 0);
  103. assert(l % 2 == 0);
  104. for (i = 0; i < l; i += 2) {
  105. FLOAT const xr_0 = xr[i+0];
  106. FLOAT const xr_1 = xr[i+1];
  107. int const ix_0 = (compareval0 > xr_0) ? 0 : 1;
  108. int const ix_1 = (compareval0 > xr_1) ? 0 : 1;
  109. ix[i+0] = ix_0;
  110. ix[i+1] = ix_1;
  111. }
  112. }
  113. #ifdef TAKEHIRO_IEEE754_HACK
  114. typedef union {
  115. float f;
  116. int i;
  117. } fi_union;
  118. #define MAGIC_FLOAT (65536*(128))
  119. #define MAGIC_INT 0x4b000000
  120. static void
  121. quantize_lines_xrpow(unsigned int l, FLOAT istep, const FLOAT * xp, int *pi)
  122. {
  123. fi_union *fi;
  124. unsigned int remaining;
  125. assert(l > 0);
  126. fi = (fi_union *) pi;
  127. l = l >> 1;
  128. remaining = l % 2;
  129. l = l >> 1;
  130. while (l--) {
  131. double x0 = istep * xp[0];
  132. double x1 = istep * xp[1];
  133. double x2 = istep * xp[2];
  134. double x3 = istep * xp[3];
  135. x0 += MAGIC_FLOAT;
  136. fi[0].f = x0;
  137. x1 += MAGIC_FLOAT;
  138. fi[1].f = x1;
  139. x2 += MAGIC_FLOAT;
  140. fi[2].f = x2;
  141. x3 += MAGIC_FLOAT;
  142. fi[3].f = x3;
  143. fi[0].f = x0 + adj43asm[fi[0].i - MAGIC_INT];
  144. fi[1].f = x1 + adj43asm[fi[1].i - MAGIC_INT];
  145. fi[2].f = x2 + adj43asm[fi[2].i - MAGIC_INT];
  146. fi[3].f = x3 + adj43asm[fi[3].i - MAGIC_INT];
  147. fi[0].i -= MAGIC_INT;
  148. fi[1].i -= MAGIC_INT;
  149. fi[2].i -= MAGIC_INT;
  150. fi[3].i -= MAGIC_INT;
  151. fi += 4;
  152. xp += 4;
  153. };
  154. if (remaining) {
  155. double x0 = istep * xp[0];
  156. double x1 = istep * xp[1];
  157. x0 += MAGIC_FLOAT;
  158. fi[0].f = x0;
  159. x1 += MAGIC_FLOAT;
  160. fi[1].f = x1;
  161. fi[0].f = x0 + adj43asm[fi[0].i - MAGIC_INT];
  162. fi[1].f = x1 + adj43asm[fi[1].i - MAGIC_INT];
  163. fi[0].i -= MAGIC_INT;
  164. fi[1].i -= MAGIC_INT;
  165. }
  166. }
  167. #else
  168. /*********************************************************************
  169. * XRPOW_FTOI is a macro to convert floats to ints.
  170. * if XRPOW_FTOI(x) = nearest_int(x), then QUANTFAC(x)=adj43asm[x]
  171. * ROUNDFAC= -0.0946
  172. *
  173. * if XRPOW_FTOI(x) = floor(x), then QUANTFAC(x)=asj43[x]
  174. * ROUNDFAC=0.4054
  175. *
  176. * Note: using floor() or (int) is extremely slow. On machines where
  177. * the TAKEHIRO_IEEE754_HACK code above does not work, it is worthwile
  178. * to write some ASM for XRPOW_FTOI().
  179. *********************************************************************/
  180. #define XRPOW_FTOI(src,dest) ((dest) = (int)(src))
  181. #define QUANTFAC(rx) adj43[rx]
  182. #define ROUNDFAC 0.4054
  183. static void
  184. quantize_lines_xrpow(unsigned int l, FLOAT istep, const FLOAT * xr, int *ix)
  185. {
  186. unsigned int remaining;
  187. assert(l > 0);
  188. l = l >> 1;
  189. remaining = l % 2;
  190. l = l >> 1;
  191. while (l--) {
  192. FLOAT x0, x1, x2, x3;
  193. int rx0, rx1, rx2, rx3;
  194. x0 = *xr++ * istep;
  195. x1 = *xr++ * istep;
  196. XRPOW_FTOI(x0, rx0);
  197. x2 = *xr++ * istep;
  198. XRPOW_FTOI(x1, rx1);
  199. x3 = *xr++ * istep;
  200. XRPOW_FTOI(x2, rx2);
  201. x0 += QUANTFAC(rx0);
  202. XRPOW_FTOI(x3, rx3);
  203. x1 += QUANTFAC(rx1);
  204. XRPOW_FTOI(x0, *ix++);
  205. x2 += QUANTFAC(rx2);
  206. XRPOW_FTOI(x1, *ix++);
  207. x3 += QUANTFAC(rx3);
  208. XRPOW_FTOI(x2, *ix++);
  209. XRPOW_FTOI(x3, *ix++);
  210. };
  211. if (remaining) {
  212. FLOAT x0, x1;
  213. int rx0, rx1;
  214. x0 = *xr++ * istep;
  215. x1 = *xr++ * istep;
  216. XRPOW_FTOI(x0, rx0);
  217. XRPOW_FTOI(x1, rx1);
  218. x0 += QUANTFAC(rx0);
  219. x1 += QUANTFAC(rx1);
  220. XRPOW_FTOI(x0, *ix++);
  221. XRPOW_FTOI(x1, *ix++);
  222. }
  223. }
  224. #endif
  225. /*********************************************************************
  226. * Quantization function
  227. * This function will select which lines to quantize and call the
  228. * proper quantization function
  229. *********************************************************************/
  230. static void
  231. quantize_xrpow(const FLOAT * xp, int *pi, FLOAT istep, gr_info const *const cod_info,
  232. calc_noise_data const *prev_noise)
  233. {
  234. /* quantize on xr^(3/4) instead of xr */
  235. int sfb;
  236. int sfbmax;
  237. int j = 0;
  238. int prev_data_use;
  239. int *iData;
  240. int accumulate = 0;
  241. int accumulate01 = 0;
  242. int *acc_iData;
  243. const FLOAT *acc_xp;
  244. iData = pi;
  245. acc_xp = xp;
  246. acc_iData = iData;
  247. /* Reusing previously computed data does not seems to work if global gain
  248. is changed. Finding why it behaves this way would allow to use a cache of
  249. previously computed values (let's 10 cached values per sfb) that would
  250. probably provide a noticeable speedup */
  251. prev_data_use = (prev_noise && (cod_info->global_gain == prev_noise->global_gain));
  252. if (cod_info->block_type == SHORT_TYPE)
  253. sfbmax = 38;
  254. else
  255. sfbmax = 21;
  256. for (sfb = 0; sfb <= sfbmax; sfb++) {
  257. int step = -1;
  258. if (prev_data_use || cod_info->block_type == NORM_TYPE) {
  259. step =
  260. cod_info->global_gain
  261. - ((cod_info->scalefac[sfb] + (cod_info->preflag ? pretab[sfb] : 0))
  262. << (cod_info->scalefac_scale + 1))
  263. - cod_info->subblock_gain[cod_info->window[sfb]] * 8;
  264. }
  265. assert(cod_info->width[sfb] >= 0);
  266. if (prev_data_use && (prev_noise->step[sfb] == step)) {
  267. /* do not recompute this part,
  268. but compute accumulated lines */
  269. if (accumulate) {
  270. quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
  271. accumulate = 0;
  272. }
  273. if (accumulate01) {
  274. quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
  275. accumulate01 = 0;
  276. }
  277. }
  278. else { /*should compute this part */
  279. int l;
  280. l = cod_info->width[sfb];
  281. if ((j + cod_info->width[sfb]) > cod_info->max_nonzero_coeff) {
  282. /*do not compute upper zero part */
  283. int usefullsize;
  284. usefullsize = cod_info->max_nonzero_coeff - j + 1;
  285. memset(&pi[cod_info->max_nonzero_coeff], 0,
  286. sizeof(int) * (576 - cod_info->max_nonzero_coeff));
  287. l = usefullsize;
  288. if (l < 0) {
  289. l = 0;
  290. }
  291. /* no need to compute higher sfb values */
  292. sfb = sfbmax + 1;
  293. }
  294. /*accumulate lines to quantize */
  295. if (!accumulate && !accumulate01) {
  296. acc_iData = iData;
  297. acc_xp = xp;
  298. }
  299. if (prev_noise &&
  300. prev_noise->sfb_count1 > 0 &&
  301. sfb >= prev_noise->sfb_count1 &&
  302. prev_noise->step[sfb] > 0 && step >= prev_noise->step[sfb]) {
  303. if (accumulate) {
  304. quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
  305. accumulate = 0;
  306. acc_iData = iData;
  307. acc_xp = xp;
  308. }
  309. accumulate01 += l;
  310. }
  311. else {
  312. if (accumulate01) {
  313. quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
  314. accumulate01 = 0;
  315. acc_iData = iData;
  316. acc_xp = xp;
  317. }
  318. accumulate += l;
  319. }
  320. if (l <= 0) {
  321. /* rh: 20040215
  322. * may happen due to "prev_data_use" optimization
  323. */
  324. if (accumulate01) {
  325. quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
  326. accumulate01 = 0;
  327. }
  328. if (accumulate) {
  329. quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
  330. accumulate = 0;
  331. }
  332. break; /* ends for-loop */
  333. }
  334. }
  335. if (sfb <= sfbmax) {
  336. iData += cod_info->width[sfb];
  337. xp += cod_info->width[sfb];
  338. j += cod_info->width[sfb];
  339. }
  340. }
  341. if (accumulate) { /*last data part */
  342. quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
  343. accumulate = 0;
  344. }
  345. if (accumulate01) { /*last data part */
  346. quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
  347. accumulate01 = 0;
  348. }
  349. }
  350. /*************************************************************************/
  351. /* ix_max */
  352. /*************************************************************************/
  353. static int
  354. ix_max(const int *ix, const int *end)
  355. {
  356. int max1 = 0, max2 = 0;
  357. do {
  358. int const x1 = *ix++;
  359. int const x2 = *ix++;
  360. if (max1 < x1)
  361. max1 = x1;
  362. if (max2 < x2)
  363. max2 = x2;
  364. } while (ix < end);
  365. if (max1 < max2)
  366. max1 = max2;
  367. return max1;
  368. }
  369. static int
  370. count_bit_ESC(const int *ix, const int *const end, int t1, const int t2, unsigned int *const s)
  371. {
  372. /* ESC-table is used */
  373. unsigned int const linbits = ht[t1].xlen * 65536u + ht[t2].xlen;
  374. unsigned int sum = 0, sum2;
  375. do {
  376. unsigned int x = *ix++;
  377. unsigned int y = *ix++;
  378. if (x >= 15u) {
  379. x = 15u;
  380. sum += linbits;
  381. }
  382. if (y >= 15u) {
  383. y = 15u;
  384. sum += linbits;
  385. }
  386. x <<= 4u;
  387. x += y;
  388. sum += largetbl[x];
  389. } while (ix < end);
  390. sum2 = sum & 0xffffu;
  391. sum >>= 16u;
  392. if (sum > sum2) {
  393. sum = sum2;
  394. t1 = t2;
  395. }
  396. *s += sum;
  397. return t1;
  398. }
  399. static int
  400. count_bit_noESC(const int *ix, const int *end, int mx, unsigned int *s)
  401. {
  402. /* No ESC-words */
  403. unsigned int sum1 = 0;
  404. const uint8_t *const hlen1 = ht[1].hlen;
  405. (void) mx;
  406. do {
  407. unsigned int const x0 = *ix++;
  408. unsigned int const x1 = *ix++;
  409. sum1 += hlen1[ x0+x0 + x1 ];
  410. } while (ix < end);
  411. *s += sum1;
  412. return 1;
  413. }
  414. static const int huf_tbl_noESC[] = {
  415. 1, 2, 5, 7, 7, 10, 10, 13, 13, 13, 13, 13, 13, 13, 13
  416. };
  417. static int
  418. count_bit_noESC_from2(const int *ix, const int *end, int max, unsigned int *s)
  419. {
  420. int t1 = huf_tbl_noESC[max - 1];
  421. /* No ESC-words */
  422. const unsigned int xlen = ht[t1].xlen;
  423. uint32_t const* table = (t1 == 2) ? &table23[0] : &table56[0];
  424. unsigned int sum = 0, sum2;
  425. do {
  426. unsigned int const x0 = *ix++;
  427. unsigned int const x1 = *ix++;
  428. sum += table[ x0 * xlen + x1 ];
  429. } while (ix < end);
  430. sum2 = sum & 0xffffu;
  431. sum >>= 16u;
  432. if (sum > sum2) {
  433. sum = sum2;
  434. t1++;
  435. }
  436. *s += sum;
  437. return t1;
  438. }
  439. inline static int
  440. count_bit_noESC_from3(const int *ix, const int *end, int max, unsigned int * s)
  441. {
  442. int t1 = huf_tbl_noESC[max - 1];
  443. /* No ESC-words */
  444. unsigned int sum1 = 0;
  445. unsigned int sum2 = 0;
  446. unsigned int sum3 = 0;
  447. const unsigned int xlen = ht[t1].xlen;
  448. const uint8_t *const hlen1 = ht[t1].hlen;
  449. const uint8_t *const hlen2 = ht[t1 + 1].hlen;
  450. const uint8_t *const hlen3 = ht[t1 + 2].hlen;
  451. int t;
  452. do {
  453. unsigned int x0 = *ix++;
  454. unsigned int x1 = *ix++;
  455. unsigned int x = x0 * xlen + x1;
  456. sum1 += hlen1[x];
  457. sum2 += hlen2[x];
  458. sum3 += hlen3[x];
  459. } while (ix < end);
  460. t = t1;
  461. if (sum1 > sum2) {
  462. sum1 = sum2;
  463. t++;
  464. }
  465. if (sum1 > sum3) {
  466. sum1 = sum3;
  467. t = t1 + 2;
  468. }
  469. *s += sum1;
  470. return t;
  471. }
  472. /*************************************************************************/
  473. /* choose table */
  474. /*************************************************************************/
  475. /*
  476. Choose the Huffman table that will encode ix[begin..end] with
  477. the fewest bits.
  478. Note: This code contains knowledge about the sizes and characteristics
  479. of the Huffman tables as defined in the IS (Table B.7), and will not work
  480. with any arbitrary tables.
  481. */
  482. static int count_bit_null(const int* ix, const int* end, int max, unsigned int* s)
  483. {
  484. (void) ix;
  485. (void) end;
  486. (void) max;
  487. (void) s;
  488. return 0;
  489. }
  490. typedef int (*count_fnc)(const int* ix, const int* end, int max, unsigned int* s);
  491. static const count_fnc count_fncs[] =
  492. { &count_bit_null
  493. , &count_bit_noESC
  494. , &count_bit_noESC_from2
  495. , &count_bit_noESC_from2
  496. , &count_bit_noESC_from3
  497. , &count_bit_noESC_from3
  498. , &count_bit_noESC_from3
  499. , &count_bit_noESC_from3
  500. , &count_bit_noESC_from3
  501. , &count_bit_noESC_from3
  502. , &count_bit_noESC_from3
  503. , &count_bit_noESC_from3
  504. , &count_bit_noESC_from3
  505. , &count_bit_noESC_from3
  506. , &count_bit_noESC_from3
  507. , &count_bit_noESC_from3
  508. };
  509. static int
  510. choose_table_nonMMX(const int *ix, const int *const end, int *const _s)
  511. {
  512. unsigned int* s = (unsigned int*)_s;
  513. unsigned int max;
  514. int choice, choice2;
  515. max = ix_max(ix, end);
  516. if (max <= 15) {
  517. return count_fncs[max](ix, end, max, s);
  518. }
  519. /* try tables with linbits */
  520. if (max > IXMAX_VAL) {
  521. *s = LARGE_BITS;
  522. return -1;
  523. }
  524. max -= 15u;
  525. for (choice2 = 24; choice2 < 32; choice2++) {
  526. if (ht[choice2].linmax >= max) {
  527. break;
  528. }
  529. }
  530. for (choice = choice2 - 8; choice < 24; choice++) {
  531. if (ht[choice].linmax >= max) {
  532. break;
  533. }
  534. }
  535. return count_bit_ESC(ix, end, choice, choice2, s);
  536. }
  537. /*************************************************************************/
  538. /* count_bit */
  539. /*************************************************************************/
  540. int
  541. noquant_count_bits(lame_internal_flags const *const gfc,
  542. gr_info * const gi, calc_noise_data * prev_noise)
  543. {
  544. SessionConfig_t const *const cfg = &gfc->cfg;
  545. int bits = 0;
  546. int i, a1, a2;
  547. int const *const ix = gi->l3_enc;
  548. i = Min(576, ((gi->max_nonzero_coeff + 2) >> 1) << 1);
  549. if (prev_noise)
  550. prev_noise->sfb_count1 = 0;
  551. /* Determine count1 region */
  552. for (; i > 1; i -= 2)
  553. if (ix[i - 1] | ix[i - 2])
  554. break;
  555. gi->count1 = i;
  556. /* Determines the number of bits to encode the quadruples. */
  557. a1 = a2 = 0;
  558. for (; i > 3; i -= 4) {
  559. int x4 = ix[i-4];
  560. int x3 = ix[i-3];
  561. int x2 = ix[i-2];
  562. int x1 = ix[i-1];
  563. int p;
  564. /* hack to check if all values <= 1 */
  565. if ((unsigned int) (x4 | x3 | x2 | x1) > 1)
  566. break;
  567. p = ((x4 * 2 + x3) * 2 + x2) * 2 + x1;
  568. a1 += t32l[p];
  569. a2 += t33l[p];
  570. }
  571. bits = a1;
  572. gi->count1table_select = 0;
  573. if (a1 > a2) {
  574. bits = a2;
  575. gi->count1table_select = 1;
  576. }
  577. gi->count1bits = bits;
  578. gi->big_values = i;
  579. if (i == 0)
  580. return bits;
  581. if (gi->block_type == SHORT_TYPE) {
  582. a1 = 3 * gfc->scalefac_band.s[3];
  583. if (a1 > gi->big_values)
  584. a1 = gi->big_values;
  585. a2 = gi->big_values;
  586. }
  587. else if (gi->block_type == NORM_TYPE) {
  588. assert(i <= 576); /* bv_scf has 576 entries (0..575) */
  589. a1 = gi->region0_count = gfc->sv_qnt.bv_scf[i - 2];
  590. a2 = gi->region1_count = gfc->sv_qnt.bv_scf[i - 1];
  591. assert(a1 + a2 + 2 < SBPSY_l);
  592. a2 = gfc->scalefac_band.l[a1 + a2 + 2];
  593. a1 = gfc->scalefac_band.l[a1 + 1];
  594. if (a2 < i)
  595. gi->table_select[2] = gfc->choose_table(ix + a2, ix + i, &bits);
  596. }
  597. else {
  598. gi->region0_count = 7;
  599. /*gi->region1_count = SBPSY_l - 7 - 1; */
  600. gi->region1_count = SBMAX_l - 1 - 7 - 1;
  601. a1 = gfc->scalefac_band.l[7 + 1];
  602. a2 = i;
  603. if (a1 > a2) {
  604. a1 = a2;
  605. }
  606. }
  607. /* have to allow for the case when bigvalues < region0 < region1 */
  608. /* (and region0, region1 are ignored) */
  609. a1 = Min(a1, i);
  610. a2 = Min(a2, i);
  611. assert(a1 >= 0);
  612. assert(a2 >= 0);
  613. /* Count the number of bits necessary to code the bigvalues region. */
  614. if (0 < a1)
  615. gi->table_select[0] = gfc->choose_table(ix, ix + a1, &bits);
  616. if (a1 < a2)
  617. gi->table_select[1] = gfc->choose_table(ix + a1, ix + a2, &bits);
  618. if (cfg->use_best_huffman == 2) {
  619. gi->part2_3_length = bits;
  620. best_huffman_divide(gfc, gi);
  621. bits = gi->part2_3_length;
  622. }
  623. if (prev_noise) {
  624. if (gi->block_type == NORM_TYPE) {
  625. int sfb = 0;
  626. while (gfc->scalefac_band.l[sfb] < gi->big_values) {
  627. sfb++;
  628. }
  629. prev_noise->sfb_count1 = sfb;
  630. }
  631. }
  632. return bits;
  633. }
  634. int
  635. count_bits(lame_internal_flags const *const gfc,
  636. const FLOAT * const xr, gr_info * const gi, calc_noise_data * prev_noise)
  637. {
  638. int *const ix = gi->l3_enc;
  639. /* since quantize_xrpow uses table lookup, we need to check this first: */
  640. FLOAT const w = (IXMAX_VAL) / IPOW20(gi->global_gain);
  641. if (gi->xrpow_max > w)
  642. return LARGE_BITS;
  643. quantize_xrpow(xr, ix, IPOW20(gi->global_gain), gi, prev_noise);
  644. if (gfc->sv_qnt.substep_shaping & 2) {
  645. int sfb, j = 0;
  646. /* 0.634521682242439 = 0.5946*2**(.5*0.1875) */
  647. int const gain = gi->global_gain + gi->scalefac_scale;
  648. const FLOAT roundfac = 0.634521682242439 / IPOW20(gain);
  649. for (sfb = 0; sfb < gi->sfbmax; sfb++) {
  650. int const width = gi->width[sfb];
  651. assert(width >= 0);
  652. if (!gfc->sv_qnt.pseudohalf[sfb]) {
  653. j += width;
  654. }
  655. else {
  656. int k;
  657. for (k = j, j += width; k < j; ++k) {
  658. ix[k] = (xr[k] >= roundfac) ? ix[k] : 0;
  659. }
  660. }
  661. }
  662. }
  663. return noquant_count_bits(gfc, gi, prev_noise);
  664. }
  665. /***********************************************************************
  666. re-calculate the best scalefac_compress using scfsi
  667. the saved bits are kept in the bit reservoir.
  668. **********************************************************************/
  669. inline static void
  670. recalc_divide_init(const lame_internal_flags * const gfc,
  671. gr_info const *cod_info,
  672. int const *const ix, int r01_bits[], int r01_div[], int r0_tbl[], int r1_tbl[])
  673. {
  674. int r0, r1, bigv, r0t, r1t, bits;
  675. bigv = cod_info->big_values;
  676. for (r0 = 0; r0 <= 7 + 15; r0++) {
  677. r01_bits[r0] = LARGE_BITS;
  678. }
  679. for (r0 = 0; r0 < 16; r0++) {
  680. int const a1 = gfc->scalefac_band.l[r0 + 1];
  681. int r0bits;
  682. if (a1 >= bigv)
  683. break;
  684. r0bits = 0;
  685. r0t = gfc->choose_table(ix, ix + a1, &r0bits);
  686. for (r1 = 0; r1 < 8; r1++) {
  687. int const a2 = gfc->scalefac_band.l[r0 + r1 + 2];
  688. if (a2 >= bigv)
  689. break;
  690. bits = r0bits;
  691. r1t = gfc->choose_table(ix + a1, ix + a2, &bits);
  692. if (r01_bits[r0 + r1] > bits) {
  693. r01_bits[r0 + r1] = bits;
  694. r01_div[r0 + r1] = r0;
  695. r0_tbl[r0 + r1] = r0t;
  696. r1_tbl[r0 + r1] = r1t;
  697. }
  698. }
  699. }
  700. }
  701. inline static void
  702. recalc_divide_sub(const lame_internal_flags * const gfc,
  703. const gr_info * cod_info2,
  704. gr_info * const gi,
  705. const int *const ix,
  706. const int r01_bits[], const int r01_div[], const int r0_tbl[], const int r1_tbl[])
  707. {
  708. int bits, r2, a2, bigv, r2t;
  709. bigv = cod_info2->big_values;
  710. for (r2 = 2; r2 < SBMAX_l + 1; r2++) {
  711. a2 = gfc->scalefac_band.l[r2];
  712. if (a2 >= bigv)
  713. break;
  714. bits = r01_bits[r2 - 2] + cod_info2->count1bits;
  715. if (gi->part2_3_length <= bits)
  716. break;
  717. r2t = gfc->choose_table(ix + a2, ix + bigv, &bits);
  718. if (gi->part2_3_length <= bits)
  719. continue;
  720. memcpy(gi, cod_info2, sizeof(gr_info));
  721. gi->part2_3_length = bits;
  722. gi->region0_count = r01_div[r2 - 2];
  723. gi->region1_count = r2 - 2 - r01_div[r2 - 2];
  724. gi->table_select[0] = r0_tbl[r2 - 2];
  725. gi->table_select[1] = r1_tbl[r2 - 2];
  726. gi->table_select[2] = r2t;
  727. }
  728. }
  729. void
  730. best_huffman_divide(const lame_internal_flags * const gfc, gr_info * const gi)
  731. {
  732. SessionConfig_t const *const cfg = &gfc->cfg;
  733. int i, a1, a2;
  734. gr_info cod_info2;
  735. int const *const ix = gi->l3_enc;
  736. int r01_bits[7 + 15 + 1];
  737. int r01_div[7 + 15 + 1];
  738. int r0_tbl[7 + 15 + 1];
  739. int r1_tbl[7 + 15 + 1];
  740. /* SHORT BLOCK stuff fails for MPEG2 */
  741. if (gi->block_type == SHORT_TYPE && cfg->mode_gr == 1)
  742. return;
  743. memcpy(&cod_info2, gi, sizeof(gr_info));
  744. if (gi->block_type == NORM_TYPE) {
  745. recalc_divide_init(gfc, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl);
  746. recalc_divide_sub(gfc, &cod_info2, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl);
  747. }
  748. i = cod_info2.big_values;
  749. if (i == 0 || (unsigned int) (ix[i - 2] | ix[i - 1]) > 1)
  750. return;
  751. i = gi->count1 + 2;
  752. if (i > 576)
  753. return;
  754. /* Determines the number of bits to encode the quadruples. */
  755. memcpy(&cod_info2, gi, sizeof(gr_info));
  756. cod_info2.count1 = i;
  757. a1 = a2 = 0;
  758. assert(i <= 576);
  759. for (; i > cod_info2.big_values; i -= 4) {
  760. int const p = ((ix[i - 4] * 2 + ix[i - 3]) * 2 + ix[i - 2]) * 2 + ix[i - 1];
  761. a1 += t32l[p];
  762. a2 += t33l[p];
  763. }
  764. cod_info2.big_values = i;
  765. cod_info2.count1table_select = 0;
  766. if (a1 > a2) {
  767. a1 = a2;
  768. cod_info2.count1table_select = 1;
  769. }
  770. cod_info2.count1bits = a1;
  771. if (cod_info2.block_type == NORM_TYPE)
  772. recalc_divide_sub(gfc, &cod_info2, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl);
  773. else {
  774. /* Count the number of bits necessary to code the bigvalues region. */
  775. cod_info2.part2_3_length = a1;
  776. a1 = gfc->scalefac_band.l[7 + 1];
  777. if (a1 > i) {
  778. a1 = i;
  779. }
  780. if (a1 > 0)
  781. cod_info2.table_select[0] =
  782. gfc->choose_table(ix, ix + a1, (int *) &cod_info2.part2_3_length);
  783. if (i > a1)
  784. cod_info2.table_select[1] =
  785. gfc->choose_table(ix + a1, ix + i, (int *) &cod_info2.part2_3_length);
  786. if (gi->part2_3_length > cod_info2.part2_3_length)
  787. memcpy(gi, &cod_info2, sizeof(gr_info));
  788. }
  789. }
  790. static const int slen1_n[16] = { 1, 1, 1, 1, 8, 2, 2, 2, 4, 4, 4, 8, 8, 8, 16, 16 };
  791. static const int slen2_n[16] = { 1, 2, 4, 8, 1, 2, 4, 8, 2, 4, 8, 2, 4, 8, 4, 8 };
  792. const int slen1_tab[16] = { 0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 };
  793. const int slen2_tab[16] = { 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3 };
  794. static void
  795. scfsi_calc(int ch, III_side_info_t * l3_side)
  796. {
  797. unsigned int i;
  798. int s1, s2, c1, c2;
  799. int sfb;
  800. gr_info *const gi = &l3_side->tt[1][ch];
  801. gr_info const *const g0 = &l3_side->tt[0][ch];
  802. for (i = 0; i < (sizeof(scfsi_band) / sizeof(int)) - 1; i++) {
  803. for (sfb = scfsi_band[i]; sfb < scfsi_band[i + 1]; sfb++) {
  804. if (g0->scalefac[sfb] != gi->scalefac[sfb]
  805. && gi->scalefac[sfb] >= 0)
  806. break;
  807. }
  808. if (sfb == scfsi_band[i + 1]) {
  809. for (sfb = scfsi_band[i]; sfb < scfsi_band[i + 1]; sfb++) {
  810. gi->scalefac[sfb] = -1;
  811. }
  812. l3_side->scfsi[ch][i] = 1;
  813. }
  814. }
  815. s1 = c1 = 0;
  816. for (sfb = 0; sfb < 11; sfb++) {
  817. if (gi->scalefac[sfb] == -1)
  818. continue;
  819. c1++;
  820. if (s1 < gi->scalefac[sfb])
  821. s1 = gi->scalefac[sfb];
  822. }
  823. s2 = c2 = 0;
  824. for (; sfb < SBPSY_l; sfb++) {
  825. if (gi->scalefac[sfb] == -1)
  826. continue;
  827. c2++;
  828. if (s2 < gi->scalefac[sfb])
  829. s2 = gi->scalefac[sfb];
  830. }
  831. for (i = 0; i < 16; i++) {
  832. if (s1 < slen1_n[i] && s2 < slen2_n[i]) {
  833. int const c = slen1_tab[i] * c1 + slen2_tab[i] * c2;
  834. if (gi->part2_length > c) {
  835. gi->part2_length = c;
  836. gi->scalefac_compress = (int)i;
  837. }
  838. }
  839. }
  840. }
  841. /*
  842. Find the optimal way to store the scalefactors.
  843. Only call this routine after final scalefactors have been
  844. chosen and the channel/granule will not be re-encoded.
  845. */
  846. void
  847. best_scalefac_store(const lame_internal_flags * gfc,
  848. const int gr, const int ch, III_side_info_t * const l3_side)
  849. {
  850. SessionConfig_t const *const cfg = &gfc->cfg;
  851. /* use scalefac_scale if we can */
  852. gr_info *const gi = &l3_side->tt[gr][ch];
  853. int sfb, i, j, l;
  854. int recalc = 0;
  855. /* remove scalefacs from bands with ix=0. This idea comes
  856. * from the AAC ISO docs. added mt 3/00 */
  857. /* check if l3_enc=0 */
  858. j = 0;
  859. for (sfb = 0; sfb < gi->sfbmax; sfb++) {
  860. int const width = gi->width[sfb];
  861. assert(width >= 0);
  862. for (l = j, j += width; l < j; ++l) {
  863. if (gi->l3_enc[l] != 0)
  864. break;
  865. }
  866. if (l == j)
  867. gi->scalefac[sfb] = recalc = -2; /* anything goes. */
  868. /* only best_scalefac_store and calc_scfsi
  869. * know--and only they should know--about the magic number -2.
  870. */
  871. }
  872. if (!gi->scalefac_scale && !gi->preflag) {
  873. int s = 0;
  874. for (sfb = 0; sfb < gi->sfbmax; sfb++)
  875. if (gi->scalefac[sfb] > 0)
  876. s |= gi->scalefac[sfb];
  877. if (!(s & 1) && s != 0) {
  878. for (sfb = 0; sfb < gi->sfbmax; sfb++)
  879. if (gi->scalefac[sfb] > 0)
  880. gi->scalefac[sfb] >>= 1;
  881. gi->scalefac_scale = recalc = 1;
  882. }
  883. }
  884. if (!gi->preflag && gi->block_type != SHORT_TYPE && cfg->mode_gr == 2) {
  885. for (sfb = 11; sfb < SBPSY_l; sfb++)
  886. if (gi->scalefac[sfb] < pretab[sfb] && gi->scalefac[sfb] != -2)
  887. break;
  888. if (sfb == SBPSY_l) {
  889. for (sfb = 11; sfb < SBPSY_l; sfb++)
  890. if (gi->scalefac[sfb] > 0)
  891. gi->scalefac[sfb] -= pretab[sfb];
  892. gi->preflag = recalc = 1;
  893. }
  894. }
  895. for (i = 0; i < 4; i++)
  896. l3_side->scfsi[ch][i] = 0;
  897. if (cfg->mode_gr == 2 && gr == 1
  898. && l3_side->tt[0][ch].block_type != SHORT_TYPE
  899. && l3_side->tt[1][ch].block_type != SHORT_TYPE) {
  900. scfsi_calc(ch, l3_side);
  901. recalc = 0;
  902. }
  903. for (sfb = 0; sfb < gi->sfbmax; sfb++) {
  904. if (gi->scalefac[sfb] == -2) {
  905. gi->scalefac[sfb] = 0; /* if anything goes, then 0 is a good choice */
  906. }
  907. }
  908. if (recalc) {
  909. (void) scale_bitcount(gfc, gi);
  910. }
  911. }
  912. #ifndef NDEBUG
  913. static int
  914. all_scalefactors_not_negative(int const *scalefac, int n)
  915. {
  916. int i;
  917. for (i = 0; i < n; ++i) {
  918. if (scalefac[i] < 0)
  919. return 0;
  920. }
  921. return 1;
  922. }
  923. #endif
  924. /* number of bits used to encode scalefacs */
  925. /* 18*slen1_tab[i] + 18*slen2_tab[i] */
  926. static const int scale_short[16] = {
  927. 0, 18, 36, 54, 54, 36, 54, 72, 54, 72, 90, 72, 90, 108, 108, 126
  928. };
  929. /* 17*slen1_tab[i] + 18*slen2_tab[i] */
  930. static const int scale_mixed[16] = {
  931. 0, 18, 36, 54, 51, 35, 53, 71, 52, 70, 88, 69, 87, 105, 104, 122
  932. };
  933. /* 11*slen1_tab[i] + 10*slen2_tab[i] */
  934. static const int scale_long[16] = {
  935. 0, 10, 20, 30, 33, 21, 31, 41, 32, 42, 52, 43, 53, 63, 64, 74
  936. };
  937. /*************************************************************************/
  938. /* scale_bitcount */
  939. /*************************************************************************/
  940. /* Also calculates the number of bits necessary to code the scalefactors. */
  941. static int
  942. mpeg1_scale_bitcount(const lame_internal_flags * gfc, gr_info * const cod_info)
  943. {
  944. int k, sfb, max_slen1 = 0, max_slen2 = 0;
  945. /* maximum values */
  946. const int *tab;
  947. int *const scalefac = cod_info->scalefac;
  948. (void) gfc;
  949. assert(all_scalefactors_not_negative(scalefac, cod_info->sfbmax));
  950. if (cod_info->block_type == SHORT_TYPE) {
  951. tab = scale_short;
  952. if (cod_info->mixed_block_flag)
  953. tab = scale_mixed;
  954. }
  955. else { /* block_type == 1,2,or 3 */
  956. tab = scale_long;
  957. if (!cod_info->preflag) {
  958. for (sfb = 11; sfb < SBPSY_l; sfb++)
  959. if (scalefac[sfb] < pretab[sfb])
  960. break;
  961. if (sfb == SBPSY_l) {
  962. cod_info->preflag = 1;
  963. for (sfb = 11; sfb < SBPSY_l; sfb++)
  964. scalefac[sfb] -= pretab[sfb];
  965. }
  966. }
  967. }
  968. for (sfb = 0; sfb < cod_info->sfbdivide; sfb++)
  969. if (max_slen1 < scalefac[sfb])
  970. max_slen1 = scalefac[sfb];
  971. for (; sfb < cod_info->sfbmax; sfb++)
  972. if (max_slen2 < scalefac[sfb])
  973. max_slen2 = scalefac[sfb];
  974. /* from Takehiro TOMINAGA <[email protected]> 10/99
  975. * loop over *all* posible values of scalefac_compress to find the
  976. * one which uses the smallest number of bits. ISO would stop
  977. * at first valid index */
  978. cod_info->part2_length = LARGE_BITS;
  979. for (k = 0; k < 16; k++) {
  980. if (max_slen1 < slen1_n[k] && max_slen2 < slen2_n[k]
  981. && cod_info->part2_length > tab[k]) {
  982. cod_info->part2_length = tab[k];
  983. cod_info->scalefac_compress = k;
  984. }
  985. }
  986. return cod_info->part2_length == LARGE_BITS;
  987. }
  988. /*
  989. table of largest scalefactor values for MPEG2
  990. */
  991. static const int max_range_sfac_tab[6][4] = {
  992. {15, 15, 7, 7},
  993. {15, 15, 7, 0},
  994. {7, 3, 0, 0},
  995. {15, 31, 31, 0},
  996. {7, 7, 7, 0},
  997. {3, 3, 0, 0}
  998. };
  999. /*************************************************************************/
  1000. /* scale_bitcount_lsf */
  1001. /*************************************************************************/
  1002. /* Also counts the number of bits to encode the scalefacs but for MPEG 2 */
  1003. /* Lower sampling frequencies (24, 22.05 and 16 kHz.) */
  1004. /* This is reverse-engineered from section 2.4.3.2 of the MPEG2 IS, */
  1005. /* "Audio Decoding Layer III" */
  1006. static int
  1007. mpeg2_scale_bitcount(const lame_internal_flags * gfc, gr_info * const cod_info)
  1008. {
  1009. int table_number, row_in_table, partition, nr_sfb, window, over;
  1010. int i, sfb, max_sfac[4];
  1011. const int *partition_table;
  1012. int const *const scalefac = cod_info->scalefac;
  1013. /*
  1014. Set partition table. Note that should try to use table one,
  1015. but do not yet...
  1016. */
  1017. if (cod_info->preflag)
  1018. table_number = 2;
  1019. else
  1020. table_number = 0;
  1021. for (i = 0; i < 4; i++)
  1022. max_sfac[i] = 0;
  1023. if (cod_info->block_type == SHORT_TYPE) {
  1024. row_in_table = 1;
  1025. partition_table = &nr_of_sfb_block[table_number][row_in_table][0];
  1026. for (sfb = 0, partition = 0; partition < 4; partition++) {
  1027. nr_sfb = partition_table[partition] / 3;
  1028. for (i = 0; i < nr_sfb; i++, sfb++)
  1029. for (window = 0; window < 3; window++)
  1030. if (scalefac[sfb * 3 + window] > max_sfac[partition])
  1031. max_sfac[partition] = scalefac[sfb * 3 + window];
  1032. }
  1033. }
  1034. else {
  1035. row_in_table = 0;
  1036. partition_table = &nr_of_sfb_block[table_number][row_in_table][0];
  1037. for (sfb = 0, partition = 0; partition < 4; partition++) {
  1038. nr_sfb = partition_table[partition];
  1039. for (i = 0; i < nr_sfb; i++, sfb++)
  1040. if (scalefac[sfb] > max_sfac[partition])
  1041. max_sfac[partition] = scalefac[sfb];
  1042. }
  1043. }
  1044. for (over = 0, partition = 0; partition < 4; partition++) {
  1045. if (max_sfac[partition] > max_range_sfac_tab[table_number][partition])
  1046. over++;
  1047. }
  1048. if (!over) {
  1049. /*
  1050. Since no bands have been over-amplified, we can set scalefac_compress
  1051. and slen[] for the formatter
  1052. */
  1053. static const int log2tab[] = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 };
  1054. int slen1, slen2, slen3, slen4;
  1055. cod_info->sfb_partition_table = nr_of_sfb_block[table_number][row_in_table];
  1056. for (partition = 0; partition < 4; partition++)
  1057. cod_info->slen[partition] = log2tab[max_sfac[partition]];
  1058. /* set scalefac_compress */
  1059. slen1 = cod_info->slen[0];
  1060. slen2 = cod_info->slen[1];
  1061. slen3 = cod_info->slen[2];
  1062. slen4 = cod_info->slen[3];
  1063. switch (table_number) {
  1064. case 0:
  1065. cod_info->scalefac_compress = (((slen1 * 5) + slen2) << 4)
  1066. + (slen3 << 2)
  1067. + slen4;
  1068. break;
  1069. case 1:
  1070. cod_info->scalefac_compress = 400 + (((slen1 * 5) + slen2) << 2)
  1071. + slen3;
  1072. break;
  1073. case 2:
  1074. cod_info->scalefac_compress = 500 + (slen1 * 3) + slen2;
  1075. break;
  1076. default:
  1077. ERRORF(gfc, "intensity stereo not implemented yet\n");
  1078. break;
  1079. }
  1080. }
  1081. #ifdef DEBUG
  1082. if (over)
  1083. ERRORF(gfc, "---WARNING !! Amplification of some bands over limits\n");
  1084. #endif
  1085. if (!over) {
  1086. assert(cod_info->sfb_partition_table);
  1087. cod_info->part2_length = 0;
  1088. for (partition = 0; partition < 4; partition++)
  1089. cod_info->part2_length +=
  1090. cod_info->slen[partition] * cod_info->sfb_partition_table[partition];
  1091. }
  1092. return over;
  1093. }
  1094. int
  1095. scale_bitcount(const lame_internal_flags * gfc, gr_info * cod_info)
  1096. {
  1097. if (gfc->cfg.mode_gr == 2) {
  1098. return mpeg1_scale_bitcount(gfc, cod_info);
  1099. }
  1100. else {
  1101. return mpeg2_scale_bitcount(gfc, cod_info);
  1102. }
  1103. }
  1104. #ifdef MMX_choose_table
  1105. extern int choose_table_MMX(const int *ix, const int *const end, int *const s);
  1106. #endif
  1107. void
  1108. huffman_init(lame_internal_flags * const gfc)
  1109. {
  1110. int i;
  1111. gfc->choose_table = choose_table_nonMMX;
  1112. #ifdef MMX_choose_table
  1113. if (gfc->CPU_features.MMX) {
  1114. gfc->choose_table = choose_table_MMX;
  1115. }
  1116. #endif
  1117. for (i = 2; i <= 576; i += 2) {
  1118. int scfb_anz = 0, bv_index;
  1119. while (gfc->scalefac_band.l[++scfb_anz] < i);
  1120. bv_index = subdv_table[scfb_anz].region0_count;
  1121. while (gfc->scalefac_band.l[bv_index + 1] > i)
  1122. bv_index--;
  1123. if (bv_index < 0) {
  1124. /* this is an indication that everything is going to
  1125. be encoded as region0: bigvalues < region0 < region1
  1126. so lets set region0, region1 to some value larger
  1127. than bigvalues */
  1128. bv_index = subdv_table[scfb_anz].region0_count;
  1129. }
  1130. gfc->sv_qnt.bv_scf[i - 2] = bv_index;
  1131. bv_index = subdv_table[scfb_anz].region1_count;
  1132. while (gfc->scalefac_band.l[bv_index + gfc->sv_qnt.bv_scf[i - 2] + 2] > i)
  1133. bv_index--;
  1134. if (bv_index < 0) {
  1135. bv_index = subdv_table[scfb_anz].region1_count;
  1136. }
  1137. gfc->sv_qnt.bv_scf[i - 1] = bv_index;
  1138. }
  1139. }