Newer
Older
monitord / lame-3.97 / libmp3lame / .svn / text-base / takehiro.c.svn-base
@root root on 23 Jan 2012 35 KB Migration from SVN revision 455
  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.  
  23. /* $Id: takehiro.c,v 1.61.2.1 2005/11/20 14:08:25 bouvigne Exp $ */
  24.  
  25. #ifdef HAVE_CONFIG_H
  26. # include <config.h>
  27. #endif
  28.  
  29. #include <assert.h>
  30. #include "util.h"
  31. #include "l3side.h"
  32. #include "tables.h"
  33. #include "quantize_pvt.h"
  34.  
  35. #ifdef WITH_DMALLOC
  36. #include <dmalloc.h>
  37. #endif
  38.  
  39. static const struct
  40. {
  41. const int region0_count;
  42. const int region1_count;
  43. } subdv_table[ 23 ] =
  44. {
  45. {0, 0}, /* 0 bands */
  46. {0, 0}, /* 1 bands */
  47. {0, 0}, /* 2 bands */
  48. {0, 0}, /* 3 bands */
  49. {0, 0}, /* 4 bands */
  50. {0, 1}, /* 5 bands */
  51. {1, 1}, /* 6 bands */
  52. {1, 1}, /* 7 bands */
  53. {1, 2}, /* 8 bands */
  54. {2, 2}, /* 9 bands */
  55. {2, 3}, /* 10 bands */
  56. {2, 3}, /* 11 bands */
  57. {3, 4}, /* 12 bands */
  58. {3, 4}, /* 13 bands */
  59. {3, 4}, /* 14 bands */
  60. {4, 5}, /* 15 bands */
  61. {4, 5}, /* 16 bands */
  62. {4, 6}, /* 17 bands */
  63. {5, 6}, /* 18 bands */
  64. {5, 6}, /* 19 bands */
  65. {5, 7}, /* 20 bands */
  66. {6, 7}, /* 21 bands */
  67. {6, 7}, /* 22 bands */
  68. };
  69.  
  70.  
  71.  
  72.  
  73.  
  74. /*********************************************************************
  75. * nonlinear quantization of xr
  76. * More accurate formula than the ISO formula. Takes into account
  77. * the fact that we are quantizing xr -> ix, but we want ix^4/3 to be
  78. * as close as possible to x^4/3. (taking the nearest int would mean
  79. * ix is as close as possible to xr, which is different.)
  80. *
  81. * From Segher Boessenkool <segher@eastsite.nl> 11/1999
  82. *
  83. * 09/2000: ASM code removed in favor of IEEE754 hack by Takehiro
  84. * Tominaga. If you need the ASM code, check CVS circa Aug 2000.
  85. *
  86. * 01/2004: Optimizations by Gabriel Bouvigne
  87. *********************************************************************/
  88.  
  89.  
  90.  
  91.  
  92.  
  93. void quantize_lines_xrpow_01(int l, FLOAT istep, const FLOAT* xr, int* ix)
  94. {
  95. const FLOAT compareval0 = (1.0 - 0.4054)/istep;
  96.  
  97. assert (l>0);
  98. l= l>>1;
  99. while (l--) {
  100. *(ix++) = (compareval0 > *xr++) ? 0 : 1;
  101. *(ix++) = (compareval0 > *xr++) ? 0 : 1;
  102. }
  103. }
  104.  
  105.  
  106.  
  107. #ifdef TAKEHIRO_IEEE754_HACK
  108.  
  109. typedef union {
  110. float f;
  111. int i;
  112. } fi_union;
  113.  
  114. #define MAGIC_FLOAT (65536*(128))
  115. #define MAGIC_INT 0x4b000000
  116.  
  117.  
  118. void quantize_lines_xrpow(int l, FLOAT istep, const FLOAT* xp, int* pi)
  119. {
  120. fi_union *fi;
  121. int remaining;
  122.  
  123. assert (l>0);
  124.  
  125. fi = (fi_union *)pi;
  126.  
  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.  
  136. x0 += MAGIC_FLOAT; fi[0].f = x0;
  137. x1 += MAGIC_FLOAT; fi[1].f = x1;
  138. x2 += MAGIC_FLOAT; fi[2].f = x2;
  139. x3 += MAGIC_FLOAT; fi[3].f = x3;
  140.  
  141. fi[0].f = x0 + (adj43asm - MAGIC_INT)[fi[0].i];
  142. fi[1].f = x1 + (adj43asm - MAGIC_INT)[fi[1].i];
  143. fi[2].f = x2 + (adj43asm - MAGIC_INT)[fi[2].i];
  144. fi[3].f = x3 + (adj43asm - MAGIC_INT)[fi[3].i];
  145.  
  146. fi[0].i -= MAGIC_INT;
  147. fi[1].i -= MAGIC_INT;
  148. fi[2].i -= MAGIC_INT;
  149. fi[3].i -= MAGIC_INT;
  150. fi += 4;
  151. xp += 4;
  152. };
  153. if (remaining) {
  154. double x0 = istep * xp[0];
  155. double x1 = istep * xp[1];
  156.  
  157. x0 += MAGIC_FLOAT; fi[0].f = x0;
  158. x1 += MAGIC_FLOAT; fi[1].f = x1;
  159.  
  160. fi[0].f = x0 + (adj43asm - MAGIC_INT)[fi[0].i];
  161. fi[1].f = x1 + (adj43asm - MAGIC_INT)[fi[1].i];
  162.  
  163. fi[0].i -= MAGIC_INT;
  164. fi[1].i -= MAGIC_INT;
  165. }
  166.  
  167. }
  168.  
  169.  
  170. # define ROUNDFAC -0.0946
  171. void quantize_lines_xrpow_ISO(int l, FLOAT istep, const FLOAT* xp, int* pi)
  172. {
  173. fi_union *fi;
  174. int remaining;
  175.  
  176. assert (l>0);
  177.  
  178. fi = (fi_union *)pi;
  179.  
  180. l = l>>1;
  181. remaining = l%2;
  182. l = l>>1;
  183. while (l--) {
  184. fi[0].f = istep * xp[0] + (ROUNDFAC + MAGIC_FLOAT);
  185. fi[1].f = istep * xp[1] + (ROUNDFAC + MAGIC_FLOAT);
  186. fi[2].f = istep * xp[2] + (ROUNDFAC + MAGIC_FLOAT);
  187. fi[3].f = istep * xp[3] + (ROUNDFAC + MAGIC_FLOAT);
  188.  
  189. fi[0].i -= MAGIC_INT;
  190. fi[1].i -= MAGIC_INT;
  191. fi[2].i -= MAGIC_INT;
  192. fi[3].i -= MAGIC_INT;
  193. fi+=4;
  194. xp+=4;
  195. };
  196. if (remaining) {
  197. fi[0].f = istep * xp[0] + (ROUNDFAC + MAGIC_FLOAT);
  198. fi[1].f = istep * xp[1] + (ROUNDFAC + MAGIC_FLOAT);
  199.  
  200. fi[0].i -= MAGIC_INT;
  201. fi[1].i -= MAGIC_INT;
  202. }
  203.  
  204. }
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211. #else
  212.  
  213. /*********************************************************************
  214. * XRPOW_FTOI is a macro to convert floats to ints.
  215. * if XRPOW_FTOI(x) = nearest_int(x), then QUANTFAC(x)=adj43asm[x]
  216. * ROUNDFAC= -0.0946
  217. *
  218. * if XRPOW_FTOI(x) = floor(x), then QUANTFAC(x)=asj43[x]
  219. * ROUNDFAC=0.4054
  220. *
  221. * Note: using floor() or (int) is extremely slow. On machines where
  222. * the TAKEHIRO_IEEE754_HACK code above does not work, it is worthwile
  223. * to write some ASM for XRPOW_FTOI().
  224. *********************************************************************/
  225. #define XRPOW_FTOI(src,dest) ((dest) = (int)(src))
  226. #define QUANTFAC(rx) adj43[rx]
  227. #define ROUNDFAC 0.4054
  228.  
  229.  
  230. void quantize_lines_xrpow(int l, FLOAT istep, const FLOAT* xr, int* ix)
  231. {
  232. int remaining;
  233.  
  234. assert (l>0);
  235.  
  236. l = l>>1;
  237. remaining = l%2;
  238. l = l>>1;
  239. while (l--) {
  240. FLOAT x0, x1, x2, x3;
  241. int rx0, rx1, rx2, rx3;
  242.  
  243. x0 = *xr++ * istep;
  244. x1 = *xr++ * istep;
  245. XRPOW_FTOI(x0, rx0);
  246. x2 = *xr++ * istep;
  247. XRPOW_FTOI(x1, rx1);
  248. x3 = *xr++ * istep;
  249. XRPOW_FTOI(x2, rx2);
  250. x0 += QUANTFAC(rx0);
  251. XRPOW_FTOI(x3, rx3);
  252. x1 += QUANTFAC(rx1);
  253. XRPOW_FTOI(x0,*ix++);
  254. x2 += QUANTFAC(rx2);
  255. XRPOW_FTOI(x1,*ix++);
  256. x3 += QUANTFAC(rx3);
  257. XRPOW_FTOI(x2,*ix++);
  258. XRPOW_FTOI(x3,*ix++);
  259. };
  260. if (remaining) {
  261. FLOAT x0, x1;
  262. int rx0, rx1;
  263.  
  264. x0 = *xr++ * istep;
  265. x1 = *xr++ * istep;
  266. XRPOW_FTOI(x0, rx0);
  267. XRPOW_FTOI(x1, rx1);
  268. x0 += QUANTFAC(rx0);
  269. x1 += QUANTFAC(rx1);
  270. XRPOW_FTOI(x0,*ix++);
  271. XRPOW_FTOI(x1,*ix++);
  272. }
  273.  
  274. }
  275.  
  276.  
  277.  
  278. void quantize_lines_xrpow_ISO(int l, FLOAT istep, const FLOAT* xr, int* ix)
  279. {
  280.  
  281. const FLOAT compareval0 = (1.0 - 0.4054)/istep;
  282. const FLOAT compareval1 = (2.0 - 0.4054)/istep;
  283.  
  284. assert (l>0);
  285.  
  286. /* depending on architecture, it may be worth calculating a few more
  287. compareval's.
  288.  
  289. eg. compareval1 = (2.0 - 0.4054)/istep;
  290. .. and then after the first compare do this ...
  291. if compareval1>*xr then ix = 1;
  292.  
  293. On a pentium166, it's only worth doing the one compare (as done here),
  294. as the second compare becomes more expensive than just calculating
  295. the value. Architectures with slow FP operations may want to add some
  296. more comparevals. try it and send your diffs statistically speaking
  297.  
  298. 73% of all xr*istep values give ix=0
  299. 16% will give 1
  300. 4% will give 2
  301. */
  302. while (l--) {
  303. if (compareval0 > *xr) {
  304. *(ix++) = 0;
  305. xr++;
  306. } else if (compareval1 > *xr) {
  307. *(ix++) = 1;
  308. xr++;
  309. } else {
  310. /* *(ix++) = (int)( istep*(*(xr++)) + 0.4054); */
  311. XRPOW_FTOI( istep*(*(xr++)) + ROUNDFAC , *(ix++) );
  312. }
  313. }
  314.  
  315. }
  316.  
  317.  
  318.  
  319.  
  320. #endif
  321.  
  322.  
  323.  
  324. /*********************************************************************
  325. * Quantization function
  326. * This function will select which lines to quantize and call the
  327. * proper quantization function
  328. *********************************************************************/
  329.  
  330. static void quantize_xrpow(const FLOAT *xp, int *pi, FLOAT istep, gr_info * const cod_info, calc_noise_data* prev_noise, lame_internal_flags * const gfc)
  331. {
  332. /* quantize on xr^(3/4) instead of xr */
  333. int sfb;
  334. int sfbmax;
  335. int j=0;
  336. int prev_data_use;
  337. int *iData;
  338. int accumulate=0;
  339. int accumulate01=0;
  340. int *acc_iData;
  341. const FLOAT *acc_xp;
  342.  
  343. iData = pi;
  344. acc_xp = xp;
  345. acc_iData = iData;
  346.  
  347.  
  348. /* Reusing previously computed data does not seems to work if global gain
  349. is changed. Finding why it behaves this way would allow to use a cache of
  350. previously computed values (let's 10 cached values per sfb) that would
  351. probably provide a noticeable speedup*/
  352. prev_data_use = (prev_noise &&
  353. (cod_info->global_gain == prev_noise->global_gain));
  354.  
  355. if (cod_info->block_type == SHORT_TYPE)
  356. sfbmax = 38;
  357. else
  358. sfbmax = 21;
  359.  
  360. for (sfb = 0; sfb <= sfbmax; sfb++) {
  361. int step = -1;
  362.  
  363. if (prev_data_use || cod_info->block_type == NORM_TYPE) {
  364. step =
  365. cod_info->global_gain
  366. - ((cod_info->scalefac[sfb] + (cod_info->preflag ? pretab[sfb] : 0))
  367. << (cod_info->scalefac_scale + 1))
  368. - cod_info->subblock_gain[cod_info->window[sfb]] * 8;
  369. }
  370. assert( cod_info->width[sfb] >= 0 );
  371. if (prev_data_use && (prev_noise->step[sfb] == step)){
  372. /* do not recompute this part,
  373. but compute accumulated lines */
  374. if (accumulate) {
  375. gfc->quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
  376. accumulate = 0;
  377. }
  378. if (accumulate01) {
  379. quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
  380. accumulate01 = 0;
  381. }
  382. } else { /*should compute this part*/
  383. int l;
  384. l = cod_info->width[sfb];
  385.  
  386. if ((j+cod_info->width[sfb])>cod_info->max_nonzero_coeff) {
  387. /*do not compute upper zero part*/
  388. int usefullsize;
  389. usefullsize = cod_info->max_nonzero_coeff - j +1;
  390. memset(&pi[cod_info->max_nonzero_coeff],0,
  391. sizeof(int)*(576-cod_info->max_nonzero_coeff));
  392. l = usefullsize;
  393.  
  394. if (l<0) {
  395. l = 0;
  396. }
  397.  
  398. /* no need to compute higher sfb values */
  399. sfb = sfbmax + 1;
  400. }
  401.  
  402. /*accumulate lines to quantize*/
  403. if (!accumulate && !accumulate01) {
  404. acc_iData = iData;
  405. acc_xp = xp;
  406. }
  407. if (prev_noise &&
  408. prev_noise->sfb_count1 > 0 &&
  409. sfb >= prev_noise->sfb_count1 &&
  410. prev_noise->step[sfb] > 0 &&
  411. step >= prev_noise->step[sfb]) {
  412.  
  413. if (accumulate) {
  414. gfc->quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
  415. accumulate = 0;
  416. acc_iData = iData;
  417. acc_xp = xp;
  418. }
  419. accumulate01 += l;
  420. } else {
  421. if (accumulate01) {
  422. quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
  423. accumulate01 = 0;
  424. acc_iData = iData;
  425. acc_xp = xp;
  426. }
  427. accumulate += l;
  428. }
  429.  
  430. if ( l <= 0 ) {
  431. /* rh: 20040215
  432. * may happen due to "prev_data_use" optimization
  433. */
  434. if (accumulate01) {
  435. quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
  436. accumulate01 = 0;
  437. }
  438. if (accumulate) {
  439. gfc->quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
  440. accumulate = 0;
  441. }
  442.  
  443. break; /* ends for-loop */
  444. }
  445. }
  446. if (sfb <= sfbmax) {
  447. iData += cod_info->width[sfb];
  448. xp += cod_info->width[sfb];
  449. j += cod_info->width[sfb];
  450. }
  451. }
  452. if (accumulate) { /*last data part*/
  453. gfc->quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
  454. accumulate = 0;
  455. }
  456. if (accumulate01) { /*last data part*/
  457. quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
  458. accumulate01 = 0;
  459. }
  460.  
  461. }
  462.  
  463.  
  464.  
  465.  
  466. void quantize_init (lame_internal_flags * const gfc)
  467. {
  468. if (gfc->quantization)
  469. gfc->quantize_lines_xrpow = quantize_lines_xrpow;
  470. else
  471. gfc->quantize_lines_xrpow = quantize_lines_xrpow_ISO;
  472. }
  473.  
  474.  
  475.  
  476.  
  477.  
  478. /*************************************************************************/
  479. /* ix_max */
  480. /*************************************************************************/
  481.  
  482. int
  483. ix_max(const int *ix, const int *end)
  484. {
  485. int max1 = 0, max2 = 0;
  486.  
  487. do {
  488. int x1 = *ix++;
  489. int x2 = *ix++;
  490. if (max1 < x1)
  491. max1 = x1;
  492.  
  493. if (max2 < x2)
  494. max2 = x2;
  495. } while (ix < end);
  496. if (max1 < max2)
  497. max1 = max2;
  498. return max1;
  499. }
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508. int
  509. count_bit_ESC(
  510. const int * ix,
  511. const int * const end,
  512. int t1,
  513. const int t2,
  514. int * const s )
  515. {
  516. /* ESC-table is used */
  517. int linbits = ht[t1].xlen * 65536 + ht[t2].xlen;
  518. int sum = 0, sum2;
  519.  
  520. do {
  521. int x = *ix++;
  522. int y = *ix++;
  523.  
  524. if (x != 0) {
  525. if (x > 14) {
  526. x = 15;
  527. sum += linbits;
  528. }
  529. x *= 16;
  530. }
  531.  
  532. if (y != 0) {
  533. if (y > 14) {
  534. y = 15;
  535. sum += linbits;
  536. }
  537. x += y;
  538. }
  539.  
  540. sum += largetbl[x];
  541. } while (ix < end);
  542.  
  543. sum2 = sum & 0xffff;
  544. sum >>= 16;
  545.  
  546. if (sum > sum2) {
  547. sum = sum2;
  548. t1 = t2;
  549. }
  550.  
  551. *s += sum;
  552. return t1;
  553. }
  554.  
  555.  
  556. inline static int
  557. count_bit_noESC(const int * ix, const int * const end, int * const s)
  558. {
  559. /* No ESC-words */
  560. int sum1 = 0;
  561. const char *hlen1 = ht[1].hlen;
  562.  
  563. do {
  564. int x = ix[0] * 2 + ix[1];
  565. ix += 2;
  566. sum1 += hlen1[x];
  567. } while (ix < end);
  568.  
  569. *s += sum1;
  570. return 1;
  571. }
  572.  
  573.  
  574.  
  575. inline static int
  576. count_bit_noESC_from2(
  577. const int * ix,
  578. const int * const end,
  579. int t1,
  580. int * const s )
  581. {
  582. /* No ESC-words */
  583. unsigned int sum = 0, sum2;
  584. const int xlen = ht[t1].xlen;
  585. const unsigned int *hlen;
  586. if (t1 == 2)
  587. hlen = table23;
  588. else
  589. hlen = table56;
  590.  
  591. do {
  592. int x = ix[0] * xlen + ix[1];
  593. ix += 2;
  594. sum += hlen[x];
  595. } while (ix < end);
  596.  
  597. sum2 = sum & 0xffff;
  598. sum >>= 16;
  599.  
  600. if (sum > sum2) {
  601. sum = sum2;
  602. t1++;
  603. }
  604.  
  605. *s += sum;
  606. return t1;
  607. }
  608.  
  609.  
  610. inline static int
  611. count_bit_noESC_from3(
  612. const int * ix,
  613. const int * const end,
  614. int t1,
  615. int * const s )
  616. {
  617. /* No ESC-words */
  618. int sum1 = 0;
  619. int sum2 = 0;
  620. int sum3 = 0;
  621. const int xlen = ht[t1].xlen;
  622. const char *hlen1 = ht[t1].hlen;
  623. const char *hlen2 = ht[t1+1].hlen;
  624. const char *hlen3 = ht[t1+2].hlen;
  625. int t;
  626.  
  627. do {
  628. int x = ix[0] * xlen + ix[1];
  629. ix += 2;
  630. sum1 += hlen1[x];
  631. sum2 += hlen2[x];
  632. sum3 += hlen3[x];
  633. } while (ix < end);
  634.  
  635. t = t1;
  636. if (sum1 > sum2) {
  637. sum1 = sum2;
  638. t++;
  639. }
  640. if (sum1 > sum3) {
  641. sum1 = sum3;
  642. t = t1+2;
  643. }
  644. *s += sum1;
  645.  
  646. return t;
  647. }
  648.  
  649.  
  650. /*************************************************************************/
  651. /* choose table */
  652. /*************************************************************************/
  653.  
  654. /*
  655. Choose the Huffman table that will encode ix[begin..end] with
  656. the fewest bits.
  657.  
  658. Note: This code contains knowledge about the sizes and characteristics
  659. of the Huffman tables as defined in the IS (Table B.7), and will not work
  660. with any arbitrary tables.
  661. */
  662.  
  663. static int choose_table_nonMMX(
  664. const int * ix,
  665. const int * const end,
  666. int * const s )
  667. {
  668. int max;
  669. int choice, choice2;
  670. static const int huf_tbl_noESC[] = {
  671. 1, 2, 5, 7, 7,10,10,13,13,13,13,13,13,13,13
  672. };
  673.  
  674. max = ix_max(ix, end);
  675.  
  676. switch (max) {
  677. case 0:
  678. return max;
  679.  
  680. case 1:
  681. return count_bit_noESC(ix, end, s);
  682.  
  683. case 2:
  684. case 3:
  685. return count_bit_noESC_from2(ix, end, huf_tbl_noESC[max - 1], s);
  686.  
  687. case 4: case 5: case 6:
  688. case 7: case 8: case 9:
  689. case 10: case 11: case 12:
  690. case 13: case 14: case 15:
  691. return count_bit_noESC_from3(ix, end, huf_tbl_noESC[max - 1], s);
  692.  
  693. default:
  694. /* try tables with linbits */
  695. if (max > IXMAX_VAL) {
  696. *s = LARGE_BITS;
  697. return -1;
  698. }
  699. max -= 15;
  700. for (choice2 = 24; choice2 < 32; choice2++) {
  701. if (ht[choice2].linmax >= max) {
  702. break;
  703. }
  704. }
  705.  
  706. for (choice = choice2 - 8; choice < 24; choice++) {
  707. if (ht[choice].linmax >= max) {
  708. break;
  709. }
  710. }
  711. return count_bit_ESC(ix, end, choice, choice2, s);
  712. }
  713. }
  714.  
  715.  
  716.  
  717. /*************************************************************************/
  718. /* count_bit */
  719. /*************************************************************************/
  720. int noquant_count_bits(
  721. lame_internal_flags * const gfc,
  722. gr_info * const gi,
  723. calc_noise_data* prev_noise)
  724. {
  725. int bits = 0;
  726. int i, a1, a2;
  727. int *const ix = gi->l3_enc;
  728.  
  729. i = Min(576, ((gi->max_nonzero_coeff+2)>>1)<<1);
  730.  
  731. if (prev_noise)
  732. prev_noise->sfb_count1 = 0;
  733.  
  734. /* Determine count1 region */
  735. for (; i > 1; i -= 2)
  736. if (ix[i - 1] | ix[i - 2])
  737. break;
  738. gi->count1 = i;
  739.  
  740. /* Determines the number of bits to encode the quadruples. */
  741. a1 = a2 = 0;
  742. for (; i > 3; i -= 4) {
  743. int p;
  744. /* hack to check if all values <= 1 */
  745. if ((unsigned int)(ix[i-1] | ix[i-2] | ix[i-3] | ix[i-4]) > 1)
  746. break;
  747.  
  748. p = ((ix[i-4] * 2 + ix[i-3]) * 2 + ix[i-2]) * 2 + ix[i-1];
  749. a1 += t32l[p];
  750. a2 += t33l[p];
  751. }
  752.  
  753. bits = a1;
  754. gi->count1table_select = 0;
  755. if (a1 > a2) {
  756. bits = a2;
  757. gi->count1table_select = 1;
  758. }
  759.  
  760. gi->count1bits = bits;
  761. gi->big_values = i;
  762. if (i == 0)
  763. return bits;
  764.  
  765. if (gi->block_type == SHORT_TYPE) {
  766. a1=3*gfc->scalefac_band.s[3];
  767. if (a1 > gi->big_values) a1 = gi->big_values;
  768. a2 = gi->big_values;
  769.  
  770. }else if (gi->block_type == NORM_TYPE) {
  771. assert(i <= 576); /* bv_scf has 576 entries (0..575) */
  772. a1 = gi->region0_count = gfc->bv_scf[i-2];
  773. a2 = gi->region1_count = gfc->bv_scf[i-1];
  774.  
  775. assert(a1+a2+2 < SBPSY_l);
  776. a2 = gfc->scalefac_band.l[a1 + a2 + 2];
  777. a1 = gfc->scalefac_band.l[a1 + 1];
  778. if (a2 < i)
  779. gi->table_select[2] = gfc->choose_table(ix + a2, ix + i, &bits);
  780.  
  781. } else {
  782. gi->region0_count = 7;
  783. /*gi->region1_count = SBPSY_l - 7 - 1;*/
  784. gi->region1_count = SBMAX_l -1 - 7 - 1;
  785. a1 = gfc->scalefac_band.l[7 + 1];
  786. a2 = i;
  787. if (a1 > a2) {
  788. a1 = a2;
  789. }
  790. }
  791.  
  792.  
  793. /* have to allow for the case when bigvalues < region0 < region1 */
  794. /* (and region0, region1 are ignored) */
  795. a1 = Min(a1,i);
  796. a2 = Min(a2,i);
  797. assert( a1 >= 0 );
  798. assert( a2 >= 0 );
  799.  
  800. /* Count the number of bits necessary to code the bigvalues region. */
  801. if (0 < a1)
  802. gi->table_select[0] = gfc->choose_table(ix, ix + a1, &bits);
  803. if (a1 < a2)
  804. gi->table_select[1] = gfc->choose_table(ix + a1, ix + a2, &bits);
  805. if (gfc->use_best_huffman == 2) {
  806. gi->part2_3_length = bits;
  807. best_huffman_divide (gfc, gi);
  808. bits = gi->part2_3_length;
  809. }
  810.  
  811.  
  812. if (prev_noise) {
  813. if (gi->block_type == NORM_TYPE) {
  814. int line = 0;
  815. int sfb = 0;
  816. while (gfc->scalefac_band.l[sfb] < gi->big_values) {
  817. sfb++;
  818. }
  819. prev_noise->sfb_count1 = sfb;
  820. }
  821. }
  822.  
  823. return bits;
  824. }
  825.  
  826. int count_bits(
  827. lame_internal_flags * const gfc,
  828. const FLOAT * const xr,
  829. gr_info * const gi,
  830. calc_noise_data* prev_noise
  831. )
  832. {
  833. int *const ix = gi->l3_enc;
  834.  
  835. /* since quantize_xrpow uses table lookup, we need to check this first: */
  836. FLOAT w = (IXMAX_VAL) / IPOW20(gi->global_gain);
  837.  
  838. if (gi->xrpow_max > w)
  839. return LARGE_BITS;
  840.  
  841. quantize_xrpow(xr, ix, IPOW20(gi->global_gain), gi, prev_noise, gfc);
  842.  
  843. if (gfc->substep_shaping & 2) {
  844. int sfb, j = 0;
  845. /* 0.634521682242439 = 0.5946*2**(.5*0.1875) */
  846. const FLOAT roundfac =
  847. 0.634521682242439 / IPOW20(gi->global_gain+gi->scalefac_scale);
  848. for (sfb = 0; sfb < gi->sfbmax; sfb++) {
  849. int width = gi->width[sfb];
  850. int l;
  851. assert( width >= 0 );
  852. j += width;
  853. if (!gfc->pseudohalf[sfb])
  854. continue;
  855. for (l = -width; l < 0; l++)
  856. if (xr[j+l] < roundfac)
  857. ix[j+l] = 0.0;
  858. }
  859. }
  860. return noquant_count_bits(gfc, gi, prev_noise);
  861. }
  862. /***********************************************************************
  863. re-calculate the best scalefac_compress using scfsi
  864. the saved bits are kept in the bit reservoir.
  865. **********************************************************************/
  866.  
  867.  
  868. inline static void
  869. recalc_divide_init(
  870. const lame_internal_flags * const gfc,
  871. gr_info *cod_info,
  872. int * const ix,
  873. int r01_bits[],
  874. int r01_div [],
  875. int r0_tbl [],
  876. int r1_tbl [] )
  877. {
  878. int r0, r1, bigv, r0t, r1t, bits;
  879.  
  880. bigv = cod_info->big_values;
  881.  
  882. for (r0 = 0; r0 <= 7 + 15; r0++) {
  883. r01_bits[r0] = LARGE_BITS;
  884. }
  885.  
  886. for (r0 = 0; r0 < 16; r0++) {
  887. int a1 = gfc->scalefac_band.l[r0 + 1], r0bits;
  888. if (a1 >= bigv)
  889. break;
  890. r0bits = 0;
  891. r0t = gfc->choose_table(ix, ix + a1, &r0bits);
  892.  
  893. for (r1 = 0; r1 < 8; r1++) {
  894. int a2 = gfc->scalefac_band.l[r0 + r1 + 2];
  895. if (a2 >= bigv)
  896. break;
  897.  
  898. bits = r0bits;
  899. r1t = gfc->choose_table(ix + a1, ix + a2, &bits);
  900. if (r01_bits[r0 + r1] > bits) {
  901. r01_bits[r0 + r1] = bits;
  902. r01_div[r0 + r1] = r0;
  903. r0_tbl[r0 + r1] = r0t;
  904. r1_tbl[r0 + r1] = r1t;
  905. }
  906. }
  907. }
  908. }
  909.  
  910. inline static void
  911. recalc_divide_sub(
  912. const lame_internal_flags * const gfc,
  913. const gr_info *cod_info2,
  914. gr_info * const gi,
  915. const int * const ix,
  916. const int r01_bits[],
  917. const int r01_div [],
  918. const int r0_tbl [],
  919. const int r1_tbl [] )
  920. {
  921. int bits, r2, a2, bigv, r2t;
  922.  
  923. bigv = cod_info2->big_values;
  924.  
  925. for (r2 = 2; r2 < SBMAX_l + 1; r2++) {
  926. a2 = gfc->scalefac_band.l[r2];
  927. if (a2 >= bigv)
  928. break;
  929.  
  930. bits = r01_bits[r2 - 2] + cod_info2->count1bits;
  931. if (gi->part2_3_length <= bits)
  932. break;
  933.  
  934. r2t = gfc->choose_table(ix + a2, ix + bigv, &bits);
  935. if (gi->part2_3_length <= bits)
  936. continue;
  937.  
  938. memcpy(gi, cod_info2, sizeof(gr_info));
  939. gi->part2_3_length = bits;
  940. gi->region0_count = r01_div[r2 - 2];
  941. gi->region1_count = r2 - 2 - r01_div[r2 - 2];
  942. gi->table_select[0] = r0_tbl[r2 - 2];
  943. gi->table_select[1] = r1_tbl[r2 - 2];
  944. gi->table_select[2] = r2t;
  945. }
  946. }
  947.  
  948.  
  949.  
  950.  
  951. void best_huffman_divide(
  952. const lame_internal_flags * const gfc,
  953. gr_info * const gi)
  954. {
  955. int i, a1, a2;
  956. gr_info cod_info2;
  957. int * const ix = gi->l3_enc;
  958.  
  959. int r01_bits[7 + 15 + 1];
  960. int r01_div[7 + 15 + 1];
  961. int r0_tbl[7 + 15 + 1];
  962. int r1_tbl[7 + 15 + 1];
  963.  
  964.  
  965. /* SHORT BLOCK stuff fails for MPEG2 */
  966. if (gi->block_type == SHORT_TYPE && gfc->mode_gr==1)
  967. return;
  968.  
  969.  
  970. memcpy(&cod_info2, gi, sizeof(gr_info));
  971. if (gi->block_type == NORM_TYPE) {
  972. recalc_divide_init(gfc, gi, ix, r01_bits,r01_div,r0_tbl,r1_tbl);
  973. recalc_divide_sub(gfc, &cod_info2, gi, ix, r01_bits,r01_div,r0_tbl,r1_tbl);
  974. }
  975.  
  976. i = cod_info2.big_values;
  977. if (i == 0 || (unsigned int)(ix[i-2] | ix[i-1]) > 1)
  978. return;
  979.  
  980. i = gi->count1 + 2;
  981. if (i > 576)
  982. return;
  983.  
  984. /* Determines the number of bits to encode the quadruples. */
  985. memcpy(&cod_info2, gi, sizeof(gr_info));
  986. cod_info2.count1 = i;
  987. a1 = a2 = 0;
  988.  
  989. assert(i <= 576);
  990. for (; i > cod_info2.big_values; i -= 4) {
  991. int p = ((ix[i-4] * 2 + ix[i-3]) * 2 + ix[i-2]) * 2 + ix[i-1];
  992. a1 += t32l[p];
  993. a2 += t33l[p];
  994. }
  995. cod_info2.big_values = i;
  996.  
  997. cod_info2.count1table_select = 0;
  998. if (a1 > a2) {
  999. a1 = a2;
  1000. cod_info2.count1table_select = 1;
  1001. }
  1002.  
  1003. cod_info2.count1bits = a1;
  1004.  
  1005. if (cod_info2.block_type == NORM_TYPE)
  1006. recalc_divide_sub(gfc, &cod_info2, gi, ix, r01_bits,r01_div,r0_tbl,r1_tbl);
  1007. else {
  1008. /* Count the number of bits necessary to code the bigvalues region. */
  1009. cod_info2.part2_3_length = a1;
  1010. a1 = gfc->scalefac_band.l[7 + 1];
  1011. if (a1 > i) {
  1012. a1 = i;
  1013. }
  1014. if (a1 > 0)
  1015. cod_info2.table_select[0] =
  1016. gfc->choose_table(ix, ix + a1, (int *)&cod_info2.part2_3_length);
  1017. if (i > a1)
  1018. cod_info2.table_select[1] =
  1019. gfc->choose_table(ix + a1, ix + i, (int *)&cod_info2.part2_3_length);
  1020. if (gi->part2_3_length > cod_info2.part2_3_length)
  1021. memcpy(gi, &cod_info2, sizeof(gr_info));
  1022. }
  1023. }
  1024.  
  1025. static const int slen1_n[16] = { 1, 1, 1, 1, 8, 2, 2, 2, 4, 4, 4, 8, 8, 8,16,16 };
  1026. static const int slen2_n[16] = { 1, 2, 4, 8, 1, 2, 4, 8, 2, 4, 8, 2, 4, 8, 4, 8 };
  1027. const int slen1_tab [16] = { 0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 };
  1028. const int slen2_tab [16] = { 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3 };
  1029.  
  1030. static void
  1031. scfsi_calc(int ch,
  1032. III_side_info_t *l3_side)
  1033. {
  1034. int i, s1, s2, c1, c2;
  1035. int sfb;
  1036. gr_info *gi = &l3_side->tt[1][ch];
  1037. gr_info *g0 = &l3_side->tt[0][ch];
  1038.  
  1039. for (i = 0; i < (sizeof(scfsi_band) / sizeof(int)) - 1; i++) {
  1040. for (sfb = scfsi_band[i]; sfb < scfsi_band[i + 1]; sfb++) {
  1041. if (g0->scalefac[sfb] != gi->scalefac[sfb]
  1042. && gi->scalefac[sfb] >= 0)
  1043. break;
  1044. }
  1045. if (sfb == scfsi_band[i + 1]) {
  1046. for (sfb = scfsi_band[i]; sfb < scfsi_band[i + 1]; sfb++) {
  1047. gi->scalefac[sfb] = -1;
  1048. }
  1049. l3_side->scfsi[ch][i] = 1;
  1050. }
  1051. }
  1052.  
  1053. s1 = c1 = 0;
  1054. for (sfb = 0; sfb < 11; sfb++) {
  1055. if (gi->scalefac[sfb] == -1)
  1056. continue;
  1057. c1++;
  1058. if (s1 < gi->scalefac[sfb])
  1059. s1 = gi->scalefac[sfb];
  1060. }
  1061.  
  1062. s2 = c2 = 0;
  1063. for (; sfb < SBPSY_l; sfb++) {
  1064. if (gi->scalefac[sfb] == -1)
  1065. continue;
  1066. c2++;
  1067. if (s2 < gi->scalefac[sfb])
  1068. s2 = gi->scalefac[sfb];
  1069. }
  1070.  
  1071. for (i = 0; i < 16; i++) {
  1072. if (s1 < slen1_n[i] && s2 < slen2_n[i]) {
  1073. int c = slen1_tab[i] * c1 + slen2_tab[i] * c2;
  1074. if (gi->part2_length > c) {
  1075. gi->part2_length = c;
  1076. gi->scalefac_compress = i;
  1077. }
  1078. }
  1079. }
  1080. }
  1081.  
  1082. /*
  1083. Find the optimal way to store the scalefactors.
  1084. Only call this routine after final scalefactors have been
  1085. chosen and the channel/granule will not be re-encoded.
  1086. */
  1087. void best_scalefac_store(
  1088. const lame_internal_flags *gfc,
  1089. const int gr,
  1090. const int ch,
  1091. III_side_info_t * const l3_side)
  1092. {
  1093. /* use scalefac_scale if we can */
  1094. gr_info *gi = &l3_side->tt[gr][ch];
  1095. int sfb,i,j,l;
  1096. int recalc = 0;
  1097.  
  1098. /* remove scalefacs from bands with ix=0. This idea comes
  1099. * from the AAC ISO docs. added mt 3/00 */
  1100. /* check if l3_enc=0 */
  1101. j = 0;
  1102. for ( sfb = 0; sfb < gi->sfbmax; sfb++ ) {
  1103. int width = gi->width[sfb];
  1104. assert( width >= 0 );
  1105. j += width;
  1106. for (l = -width; l < 0; l++) {
  1107. if (gi->l3_enc[l+j]!=0)
  1108. break;
  1109. }
  1110. if (l==0)
  1111. gi->scalefac[sfb] = recalc = -2; /* anything goes. */
  1112. /* only best_scalefac_store and calc_scfsi
  1113. * know--and only they should know--about the magic number -2.
  1114. */
  1115. }
  1116.  
  1117. if (!gi->scalefac_scale && !gi->preflag) {
  1118. int s = 0;
  1119. for (sfb = 0; sfb < gi->sfbmax; sfb++)
  1120. if (gi->scalefac[sfb] > 0)
  1121. s |= gi->scalefac[sfb];
  1122.  
  1123. if (!(s & 1) && s != 0) {
  1124. for (sfb = 0; sfb < gi->sfbmax; sfb++)
  1125. if (gi->scalefac[sfb] > 0)
  1126. gi->scalefac[sfb] >>= 1;
  1127. gi->scalefac_scale = recalc = 1;
  1128. }
  1129. }
  1130.  
  1131. if (!gi->preflag && gi->block_type != SHORT_TYPE && gfc->mode_gr==2) {
  1132. for (sfb = 11; sfb < SBPSY_l; sfb++)
  1133. if (gi->scalefac[sfb] < pretab[sfb] && gi->scalefac[sfb] != -2)
  1134. break;
  1135. if (sfb == SBPSY_l) {
  1136. for (sfb = 11; sfb < SBPSY_l; sfb++)
  1137. if (gi->scalefac[sfb] > 0)
  1138. gi->scalefac[sfb] -= pretab[sfb];
  1139.  
  1140. gi->preflag = recalc = 1;
  1141. }
  1142. }
  1143.  
  1144. for ( i = 0; i < 4; i++ )
  1145. l3_side->scfsi[ch][i] = 0;
  1146.  
  1147. if (gfc->mode_gr==2 && gr == 1
  1148. && l3_side->tt[0][ch].block_type != SHORT_TYPE
  1149. && l3_side->tt[1][ch].block_type != SHORT_TYPE) {
  1150. scfsi_calc(ch, l3_side);
  1151. recalc = 0;
  1152. }
  1153. for ( sfb = 0; sfb < gi->sfbmax; sfb++ ) {
  1154. if ( gi->scalefac[sfb] == -2 ) {
  1155. gi->scalefac[sfb] = 0; /* if anything goes, then 0 is a good choice */
  1156. }
  1157. }
  1158. if (recalc) {
  1159. if (gfc->mode_gr == 2) {
  1160. scale_bitcount(gi);
  1161. } else {
  1162. scale_bitcount_lsf(gfc, gi);
  1163. }
  1164. }
  1165. }
  1166.  
  1167.  
  1168. #ifndef NDEBUG
  1169. static int all_scalefactors_not_negative( int const* scalefac, int n )
  1170. {
  1171. int i;
  1172. for ( i = 0; i < n; ++i ) {
  1173. if ( scalefac[i] < 0 ) return 0;
  1174. }
  1175. return 1;
  1176. }
  1177. #endif
  1178.  
  1179.  
  1180. /* number of bits used to encode scalefacs */
  1181.  
  1182. /* 18*slen1_tab[i] + 18*slen2_tab[i] */
  1183. static const int scale_short[16] = {
  1184. 0, 18, 36, 54, 54, 36, 54, 72, 54, 72, 90, 72, 90, 108, 108, 126 };
  1185.  
  1186. /* 17*slen1_tab[i] + 18*slen2_tab[i] */
  1187. static const int scale_mixed[16] = {
  1188. 0, 18, 36, 54, 51, 35, 53, 71, 52, 70, 88, 69, 87, 105, 104, 122 };
  1189.  
  1190. /* 11*slen1_tab[i] + 10*slen2_tab[i] */
  1191. static const int scale_long[16] = {
  1192. 0, 10, 20, 30, 33, 21, 31, 41, 32, 42, 52, 43, 53, 63, 64, 74 };
  1193.  
  1194.  
  1195. /*************************************************************************/
  1196. /* scale_bitcount */
  1197. /*************************************************************************/
  1198.  
  1199. /* Also calculates the number of bits necessary to code the scalefactors. */
  1200.  
  1201. int scale_bitcount(gr_info * const cod_info)
  1202. {
  1203. int k, sfb, max_slen1 = 0, max_slen2 = 0;
  1204.  
  1205. /* maximum values */
  1206. const int *tab;
  1207. int *scalefac = cod_info->scalefac;
  1208. assert( all_scalefactors_not_negative( scalefac, cod_info->sfbmax ) );
  1209. if ( cod_info->block_type == SHORT_TYPE ) {
  1210. tab = scale_short;
  1211. if (cod_info->mixed_block_flag)
  1212. tab = scale_mixed;
  1213. }
  1214. else
  1215. { /* block_type == 1,2,or 3 */
  1216. tab = scale_long;
  1217. if (!cod_info->preflag) {
  1218. for ( sfb = 11; sfb < SBPSY_l; sfb++ )
  1219. if (scalefac[sfb] < pretab[sfb])
  1220. break;
  1221.  
  1222. if (sfb == SBPSY_l) {
  1223. cod_info->preflag = 1;
  1224. for ( sfb = 11; sfb < SBPSY_l; sfb++ )
  1225. scalefac[sfb] -= pretab[sfb];
  1226. }
  1227. }
  1228. }
  1229.  
  1230. for (sfb = 0; sfb < cod_info->sfbdivide; sfb++)
  1231. if (max_slen1 < scalefac[sfb])
  1232. max_slen1 = scalefac[sfb];
  1233.  
  1234. for (; sfb < cod_info->sfbmax; sfb++)
  1235. if (max_slen2 < scalefac[sfb])
  1236. max_slen2 = scalefac[sfb];
  1237.  
  1238. /* from Takehiro TOMINAGA <tominaga@isoternet.org> 10/99
  1239. * loop over *all* posible values of scalefac_compress to find the
  1240. * one which uses the smallest number of bits. ISO would stop
  1241. * at first valid index */
  1242. cod_info->part2_length = LARGE_BITS;
  1243. for ( k = 0; k < 16; k++ ) {
  1244. if (max_slen1 < slen1_n[k] && max_slen2 < slen2_n[k]
  1245. && cod_info->part2_length > tab[k]) {
  1246. cod_info->part2_length=tab[k];
  1247. cod_info->scalefac_compress=k;
  1248. }
  1249. }
  1250. return cod_info->part2_length == LARGE_BITS;
  1251. }
  1252.  
  1253.  
  1254.  
  1255. /*
  1256. table of largest scalefactor values for MPEG2
  1257. */
  1258. static const int max_range_sfac_tab[6][4] =
  1259. {
  1260. { 15, 15, 7, 7},
  1261. { 15, 15, 7, 0},
  1262. { 7, 3, 0, 0},
  1263. { 15, 31, 31, 0},
  1264. { 7, 7, 7, 0},
  1265. { 3, 3, 0, 0}
  1266. };
  1267.  
  1268.  
  1269.  
  1270.  
  1271. /*************************************************************************/
  1272. /* scale_bitcount_lsf */
  1273. /*************************************************************************/
  1274.  
  1275. /* Also counts the number of bits to encode the scalefacs but for MPEG 2 */
  1276. /* Lower sampling frequencies (24, 22.05 and 16 kHz.) */
  1277. /* This is reverse-engineered from section 2.4.3.2 of the MPEG2 IS, */
  1278. /* "Audio Decoding Layer III" */
  1279.  
  1280. int scale_bitcount_lsf(const lame_internal_flags *gfc,
  1281. gr_info * const cod_info)
  1282. {
  1283. int table_number, row_in_table, partition, nr_sfb, window, over;
  1284. int i, sfb, max_sfac[ 4 ];
  1285. const int *partition_table;
  1286. int *scalefac = cod_info->scalefac;
  1287.  
  1288. /*
  1289. Set partition table. Note that should try to use table one,
  1290. but do not yet...
  1291. */
  1292. if ( cod_info->preflag )
  1293. table_number = 2;
  1294. else
  1295. table_number = 0;
  1296.  
  1297. for ( i = 0; i < 4; i++ )
  1298. max_sfac[i] = 0;
  1299.  
  1300. if ( cod_info->block_type == SHORT_TYPE )
  1301. {
  1302. row_in_table = 1;
  1303. partition_table = &nr_of_sfb_block[table_number][row_in_table][0];
  1304. for ( sfb = 0, partition = 0; partition < 4; partition++ )
  1305. {
  1306. nr_sfb = partition_table[ partition ] / 3;
  1307. for ( i = 0; i < nr_sfb; i++, sfb++ )
  1308. for ( window = 0; window < 3; window++ )
  1309. if ( scalefac[sfb*3+window] > max_sfac[partition] )
  1310. max_sfac[partition] = scalefac[sfb*3+window];
  1311. }
  1312. }
  1313. else
  1314. {
  1315. row_in_table = 0;
  1316. partition_table = &nr_of_sfb_block[table_number][row_in_table][0];
  1317. for ( sfb = 0, partition = 0; partition < 4; partition++ )
  1318. {
  1319. nr_sfb = partition_table[ partition ];
  1320. for ( i = 0; i < nr_sfb; i++, sfb++ )
  1321. if ( scalefac[sfb] > max_sfac[partition] )
  1322. max_sfac[partition] = scalefac[sfb];
  1323. }
  1324. }
  1325.  
  1326. for ( over = 0, partition = 0; partition < 4; partition++ )
  1327. {
  1328. if ( max_sfac[partition] > max_range_sfac_tab[table_number][partition] )
  1329. over++;
  1330. }
  1331. if ( !over )
  1332. {
  1333. /*
  1334. Since no bands have been over-amplified, we can set scalefac_compress
  1335. and slen[] for the formatter
  1336. */
  1337. static const int log2tab[] = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 };
  1338.  
  1339. int slen1, slen2, slen3, slen4;
  1340.  
  1341. cod_info->sfb_partition_table = nr_of_sfb_block[table_number][row_in_table];
  1342. for ( partition = 0; partition < 4; partition++ )
  1343. cod_info->slen[partition] = log2tab[max_sfac[partition]];
  1344.  
  1345. /* set scalefac_compress */
  1346. slen1 = cod_info->slen[ 0 ];
  1347. slen2 = cod_info->slen[ 1 ];
  1348. slen3 = cod_info->slen[ 2 ];
  1349. slen4 = cod_info->slen[ 3 ];
  1350.  
  1351. switch ( table_number )
  1352. {
  1353. case 0:
  1354. cod_info->scalefac_compress = (((slen1 * 5) + slen2) << 4)
  1355. + (slen3 << 2)
  1356. + slen4;
  1357. break;
  1358.  
  1359. case 1:
  1360. cod_info->scalefac_compress = 400
  1361. + (((slen1 * 5) + slen2) << 2)
  1362. + slen3;
  1363. break;
  1364.  
  1365. case 2:
  1366. cod_info->scalefac_compress = 500 + (slen1 * 3) + slen2;
  1367. break;
  1368.  
  1369. default:
  1370. ERRORF(gfc,"intensity stereo not implemented yet\n" );
  1371. break;
  1372. }
  1373. }
  1374. #ifdef DEBUG
  1375. if ( over )
  1376. ERRORF(gfc, "---WARNING !! Amplification of some bands over limits\n" );
  1377. #endif
  1378. if (!over) {
  1379. assert( cod_info->sfb_partition_table );
  1380. cod_info->part2_length=0;
  1381. for ( partition = 0; partition < 4; partition++ )
  1382. cod_info->part2_length += cod_info->slen[partition] * cod_info->sfb_partition_table[partition];
  1383. }
  1384. return over;
  1385. }
  1386.  
  1387.  
  1388.  
  1389. void huffman_init(lame_internal_flags * const gfc)
  1390. {
  1391. int i;
  1392.  
  1393. gfc->choose_table = choose_table_nonMMX;
  1394. #ifdef MMX_choose_table
  1395. if (gfc->CPU_features.MMX) {
  1396. extern int choose_table_MMX(const int *ix, const int * const end, int * const s);
  1397. gfc->choose_table = choose_table_MMX;
  1398. }
  1399. #endif
  1400.  
  1401. for (i = 2; i <= 576; i += 2) {
  1402. int scfb_anz = 0, index;
  1403. while (gfc->scalefac_band.l[++scfb_anz] < i)
  1404. ;
  1405.  
  1406. index = subdv_table[scfb_anz].region0_count;
  1407. while (gfc->scalefac_band.l[index + 1] > i)
  1408. index--;
  1409.  
  1410. if (index < 0) {
  1411. /* this is an indication that everything is going to
  1412. be encoded as region0: bigvalues < region0 < region1
  1413. so lets set region0, region1 to some value larger
  1414. than bigvalues */
  1415. index = subdv_table[scfb_anz].region0_count;
  1416. }
  1417.  
  1418. gfc->bv_scf[i-2] = index;
  1419.  
  1420. index = subdv_table[scfb_anz].region1_count;
  1421. while (gfc->scalefac_band.l[index + gfc->bv_scf[i-2] + 2] > i)
  1422. index--;
  1423.  
  1424. if (index < 0) {
  1425. index = subdv_table[scfb_anz].region1_count;
  1426. }
  1427.  
  1428. gfc->bv_scf[i-1] = index;
  1429. }
  1430. }
  1431.