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