upgrade to xpdf 3.00.
[swftools.git] / lib / lame / takehiro.c
blob13507ba353792a41060529e66bd93eb960ba52d2
1 /*
2 * MP3 huffman table selecting and bit counting
4 * Copyright (c) 1999 Takehiro TOMINAGA
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.
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.
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.
22 /* $Id: takehiro.c,v 1.1 2002/04/28 17:30:29 kramm Exp $ */
24 #include "config_static.h"
26 #include <assert.h>
27 #include "util.h"
28 #include "l3side.h"
29 #include "tables.h"
30 #include "quantize_pvt.h"
32 #ifdef WITH_DMALLOC
33 #include <dmalloc.h>
34 #endif
36 static const struct
38 const int region0_count;
39 const int region1_count;
40 } subdv_table[ 23 ] =
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 */
70 /*************************************************************************/
71 /* ix_max */
72 /*************************************************************************/
74 int
75 ix_max(const int *ix, const int *end)
77 int max1 = 0, max2 = 0;
79 do {
80 int x1 = *ix++;
81 int x2 = *ix++;
82 if (max1 < x1)
83 max1 = x1;
85 if (max2 < x2)
86 max2 = x2;
87 } while (ix < end);
88 if (max1 < max2)
89 max1 = max2;
90 return max1;
101 count_bit_ESC(
102 const int * ix,
103 const int * const end,
104 int t1,
105 const int t2,
106 int * const s )
108 /* ESC-table is used */
109 int linbits = ht[t1].xlen * 65536 + ht[t2].xlen;
110 int sum = 0, sum2;
112 do {
113 int x = *ix++;
114 int y = *ix++;
116 if (x != 0) {
117 if (x > 14) {
118 x = 15;
119 sum += linbits;
121 x *= 16;
124 if (y != 0) {
125 if (y > 14) {
126 y = 15;
127 sum += linbits;
129 x += y;
132 sum += largetbl[x];
133 } while (ix < end);
135 sum2 = sum & 0xffff;
136 sum >>= 16;
138 if (sum > sum2) {
139 sum = sum2;
140 t1 = t2;
143 *s += sum;
144 return t1;
148 inline static int
149 count_bit_noESC(const int * ix, const int * const end, int * const s)
151 /* No ESC-words */
152 int sum1 = 0;
153 const char *hlen1 = ht[1].hlen;
155 do {
156 int x = ix[0] * 2 + ix[1];
157 ix += 2;
158 sum1 += hlen1[x];
159 } while (ix < end);
161 *s += sum1;
162 return 1;
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 )
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;
183 do {
184 int x = ix[0] * xlen + ix[1];
185 ix += 2;
186 sum += hlen[x];
187 } while (ix < end);
189 sum2 = sum & 0xffff;
190 sum >>= 16;
192 if (sum > sum2) {
193 sum = sum2;
194 t1++;
197 *s += sum;
198 return t1;
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 )
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;
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);
227 t = t1;
228 if (sum1 > sum2) {
229 sum1 = sum2;
230 t++;
232 if (sum1 > sum3) {
233 sum1 = sum3;
234 t = t1+2;
236 *s += sum1;
238 return t;
242 /*************************************************************************/
243 /* choose table */
244 /*************************************************************************/
247 Choose the Huffman table that will encode ix[begin..end] with
248 the fewest bits.
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.
255 static int choose_table_nonMMX(
256 const int * ix,
257 const int * const end,
258 int * const s )
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 ? */
266 max = ix_max(ix, end);
268 switch (max) {
269 case 0:
270 return max;
272 case 1:
273 return count_bit_noESC(ix, end, s);
275 case 2:
276 case 3:
277 return count_bit_noESC_from2(ix, end, huf_tbl_noESC[max - 1], s);
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);
285 default:
286 /* try tables with linbits */
287 if (max > IXMAX_VAL) {
288 *s = LARGE_BITS;
289 return -1;
291 max -= 15;
292 for (choice2 = 24; choice2 < 32; choice2++) {
293 if (ht[choice2].linmax >= max) {
294 break;
298 for (choice = choice2 - 8; choice < 24; choice++) {
299 if (ht[choice].linmax >= max) {
300 break;
303 return count_bit_ESC(ix, end, choice, choice2, s);
309 /*************************************************************************/
310 /* count_bit */
311 /*************************************************************************/
313 int count_bits(
314 lame_internal_flags * const gfc,
315 int * const ix,
316 const FLOAT8 * const xr,
317 gr_info * const gi)
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;
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));
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;
343 end = gfc->scalefac_band.l[sfb+1];
344 for (; i < end; i++)
345 if (xr[i] < roundfac)
346 ix[i] = 0;
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;
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;
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;
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];
389 bits = a1;
390 gi->count1table_select = 0;
391 if (a1 > a2) {
392 bits = a2;
393 gi->count1table_select = 1;
396 gi->count1bits = bits;
397 gi->big_values = i;
398 if (i == 0)
399 return bits;
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;
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];
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);
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;
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);
434 assert( a1 >= 0 );
435 assert( a2 >= 0 );
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;
445 /***********************************************************************
446 re-calculate the best scalefac_compress using scfsi
447 the saved bits are kept in the bit reservoir.
448 **********************************************************************/
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 [] )
461 int r0, r1, bigv, r0t, r1t, bits;
463 bigv = cod_info.big_values;
465 for (r0 = 0; r0 <= 7 + 15; r0++) {
466 r01_bits[r0] = LARGE_BITS;
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);
476 for (r1 = 0; r1 < 8; r1++) {
477 int a2 = gfc->scalefac_band.l[r0 + r1 + 2];
478 if (a2 >= bigv)
479 break;
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;
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 [] )
504 int bits, r2, a2, bigv, r2t;
506 bigv = cod_info2.big_values;
508 for (r2 = 2; r2 < SBMAX_l + 1; r2++) {
509 a2 = gfc->scalefac_band.l[r2];
510 if (a2 >= bigv)
511 break;
513 bits = r01_bits[r2 - 2] + cod_info2.count1bits;
514 if (gi->part2_3_length <= bits)
515 break;
517 r2t = gfc->choose_table(ix + a2, ix + bigv, &bits);
518 if (gi->part2_3_length <= bits)
519 continue;
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;
534 void best_huffman_divide(
535 const lame_internal_flags * const gfc,
536 gr_info * const gi,
537 int * const ix )
539 int i, a1, a2;
540 gr_info cod_info2;
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];
548 /* SHORT BLOCK stuff fails for MPEG2 */
549 if (gi->block_type == SHORT_TYPE && gfc->mode_gr==1)
550 return;
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);
559 i = cod_info2.big_values;
560 if (i == 0 || (unsigned int)(ix[i-2] | ix[i-1]) > 1)
561 return;
563 i = gi->count1 + 2;
564 if (i > 576)
565 return;
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;
572 assert(i <= 576);
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];
579 cod_info2.big_values = i;
581 cod_info2.count1table_select = 0;
582 if (a1 > a2) {
583 a1 = a2;
584 cod_info2.count1table_select = 1;
587 cod_info2.count1bits = a1;
588 cod_info2.part2_3_length = a1 + cod_info2.part2_length;
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;
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));
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 };
612 void
613 scfsi_calc(int ch,
614 III_side_info_t *l3_side,
615 III_scalefac_t scalefac[2][2])
617 int i, s1, s2, c1, c2;
618 int sfb;
619 gr_info *gi = &l3_side->gr[1].ch[ch].tt;
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
627 for (i = 0; i < 4; i++)
628 l3_side->scfsi[ch][i] = 0;
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;
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;
639 l3_side->scfsi[ch][i] = 1;
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];
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];
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;
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.
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] )
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;
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;
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;
711 j += end-start;
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];
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];
729 if (!(s & 1) && s != 0) {
730 for (sfb = 0; sfb < gi->sfb_lmax; sfb++) {
731 scalefac[gr][ch].l[sfb] /= 2;
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;
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);
750 for ( i = 0; i < 4; i++ )
751 l3_side->scfsi[ch][i] = 0;
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);
758 gi->part2_3_length += gi->part2_length;
762 /* number of bits used to encode scalefacs */
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 };
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 };
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 };
777 /*************************************************************************/
778 /* scale_bitcount */
779 /*************************************************************************/
781 /* Also calculates the number of bits necessary to code the scalefactors. */
783 int scale_bitcount(
784 III_scalefac_t * const scalefac, gr_info * const cod_info)
786 int i, k, sfb, max_slen1 = 0, max_slen2 = 0, ep = 2;
788 /* maximum values */
789 const int *tab;
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];
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];
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];
817 if (!cod_info->preflag) {
818 for ( sfb = 11; sfb < SBPSY_l; sfb++ )
819 if (scalefac->l[sfb] < pretab[sfb])
820 break;
822 if (sfb == SBPSY_l) {
823 cod_info->preflag = 1;
824 for ( sfb = 11; sfb < SBPSY_l; sfb++ )
825 scalefac->l[sfb] -= pretab[sfb];
829 for ( sfb = 11; sfb < SBPSY_l; sfb++ )
830 if ( scalefac->l[sfb] > max_slen2 )
831 max_slen2 = scalefac->l[sfb];
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++ )
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 */
849 return ep;
855 table of largest scalefactor values for MPEG2
857 static const int max_range_sfac_tab[6][4] =
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}
870 /*************************************************************************/
871 /* scale_bitcount_lsf */
872 /*************************************************************************/
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.) */
877 /* This is reverse-engineered from section 2.4.3.2 of the MPEG2 IS, */
878 /* "Audio Decoding Layer III" */
880 int scale_bitcount_lsf(const lame_internal_flags *gfc,
881 const III_scalefac_t * const scalefac, gr_info * const cod_info)
883 int table_number, row_in_table, partition, nr_sfb, window, over;
884 int i, sfb, max_sfac[ 4 ];
885 const int *partition_table;
888 Set partition table. Note that should try to use table one,
889 but do not yet...
891 if ( cod_info->preflag )
892 table_number = 2;
893 else
894 table_number = 0;
896 for ( i = 0; i < 4; i++ )
897 max_sfac[i] = 0;
899 if ( cod_info->block_type == SHORT_TYPE )
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++ )
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];
912 else
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++ )
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];
925 for ( over = 0, partition = 0; partition < 4; partition++ )
927 if ( max_sfac[partition] > max_range_sfac_tab[table_number][partition] )
928 over++;
930 if ( !over )
933 Since no bands have been over-amplified, we can set scalefac_compress
934 and slen[] for the formatter
936 static const int log2tab[] = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 };
938 int slen1, slen2, slen3, slen4;
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]];
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 ];
950 switch ( table_number )
952 case 0:
953 cod_info->scalefac_compress = (((slen1 * 5) + slen2) << 4)
954 + (slen3 << 2)
955 + slen4;
956 break;
958 case 1:
959 cod_info->scalefac_compress = 400
960 + (((slen1 * 5) + slen2) << 2)
961 + slen3;
962 break;
964 case 2:
965 cod_info->scalefac_compress = 500 + (slen1 * 3) + slen2;
966 break;
968 default:
969 ERRORF(gfc,"intensity stereo not implemented yet\n" );
970 break;
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];
983 return over;
988 void huffman_init(lame_internal_flags * const gfc)
990 int i;
992 gfc->choose_table = choose_table_nonMMX;
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;
999 #endif
1001 for (i = 2; i <= 576; i += 2) {
1002 int scfb_anz = 0, index;
1003 while (gfc->scalefac_band.l[++scfb_anz] < i)
1006 index = subdv_table[scfb_anz].region0_count;
1007 while (gfc->scalefac_band.l[index + 1] > i)
1008 index--;
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;
1018 gfc->bv_scf[i-2] = index;
1020 index = subdv_table[scfb_anz].region1_count;
1021 while (gfc->scalefac_band.l[index + gfc->bv_scf[i-2] + 2] > i)
1022 index--;
1024 if (index < 0) {
1025 index = subdv_table[scfb_anz].region1_count;
1028 gfc->bv_scf[i-1] = index;