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