home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Sound / LAME / src / takehiro.c < prev    next >
C/C++ Source or Header  |  2000-08-06  |  26KB  |  1,071 lines

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