fix crashes reported by Debian Cylab Mayhem Team
[swftools.git] / lib / lame / takehiro.c
blobdb4d3bbb55b9e44930f2d40accf8ca414ae8455e
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.2 2006/02/09 16:56:23 kramm Exp $ */
24 #include <stdlib.h>
25 #include "config_static.h"
27 #include <assert.h>
28 #include "util.h"
29 #include "l3side.h"
30 #include "tables.h"
31 #include "quantize_pvt.h"
33 #ifdef WITH_DMALLOC
34 #include <dmalloc.h>
35 #endif
37 static const struct
39 const int region0_count;
40 const int region1_count;
41 } subdv_table[ 23 ] =
43 {0, 0}, /* 0 bands */
44 {0, 0}, /* 1 bands */
45 {0, 0}, /* 2 bands */
46 {0, 0}, /* 3 bands */
47 {0, 0}, /* 4 bands */
48 {0, 1}, /* 5 bands */
49 {1, 1}, /* 6 bands */
50 {1, 1}, /* 7 bands */
51 {1, 2}, /* 8 bands */
52 {2, 2}, /* 9 bands */
53 {2, 3}, /* 10 bands */
54 {2, 3}, /* 11 bands */
55 {3, 4}, /* 12 bands */
56 {3, 4}, /* 13 bands */
57 {3, 4}, /* 14 bands */
58 {4, 5}, /* 15 bands */
59 {4, 5}, /* 16 bands */
60 {4, 6}, /* 17 bands */
61 {5, 6}, /* 18 bands */
62 {5, 6}, /* 19 bands */
63 {5, 7}, /* 20 bands */
64 {6, 7}, /* 21 bands */
65 {6, 7}, /* 22 bands */
71 /*************************************************************************/
72 /* ix_max */
73 /*************************************************************************/
75 int
76 ix_max(const int *ix, const int *end)
78 int max1 = 0, max2 = 0;
80 do {
81 int x1 = *ix++;
82 int x2 = *ix++;
83 if (max1 < x1)
84 max1 = x1;
86 if (max2 < x2)
87 max2 = x2;
88 } while (ix < end);
89 if (max1 < max2)
90 max1 = max2;
91 return max1;
102 count_bit_ESC(
103 const int * ix,
104 const int * const end,
105 int t1,
106 const int t2,
107 int * const s )
109 /* ESC-table is used */
110 int linbits = ht[t1].xlen * 65536 + ht[t2].xlen;
111 int sum = 0, sum2;
113 do {
114 int x = *ix++;
115 int y = *ix++;
117 if (x != 0) {
118 if (x > 14) {
119 x = 15;
120 sum += linbits;
122 x *= 16;
125 if (y != 0) {
126 if (y > 14) {
127 y = 15;
128 sum += linbits;
130 x += y;
133 sum += largetbl[x];
134 } while (ix < end);
136 sum2 = sum & 0xffff;
137 sum >>= 16;
139 if (sum > sum2) {
140 sum = sum2;
141 t1 = t2;
144 *s += sum;
145 return t1;
149 inline static int
150 count_bit_noESC(const int * ix, const int * const end, int * const s)
152 /* No ESC-words */
153 int sum1 = 0;
154 const char *hlen1 = ht[1].hlen;
156 do {
157 int x = ix[0] * 2 + ix[1];
158 ix += 2;
159 sum1 += hlen1[x];
160 } while (ix < end);
162 *s += sum1;
163 return 1;
168 inline static int
169 count_bit_noESC_from2(
170 const int * ix,
171 const int * const end,
172 int t1,
173 int * const s )
175 /* No ESC-words */
176 unsigned int sum = 0, sum2;
177 const int xlen = ht[t1].xlen;
178 const unsigned int *hlen;
179 if (t1 == 2)
180 hlen = table23;
181 else
182 hlen = table56;
184 do {
185 int x = ix[0] * xlen + ix[1];
186 ix += 2;
187 sum += hlen[x];
188 } while (ix < end);
190 sum2 = sum & 0xffff;
191 sum >>= 16;
193 if (sum > sum2) {
194 sum = sum2;
195 t1++;
198 *s += sum;
199 return t1;
203 inline static int
204 count_bit_noESC_from3(
205 const int * ix,
206 const int * const end,
207 int t1,
208 int * const s )
210 /* No ESC-words */
211 int sum1 = 0;
212 int sum2 = 0;
213 int sum3 = 0;
214 const int xlen = ht[t1].xlen;
215 const char *hlen1 = ht[t1].hlen;
216 const char *hlen2 = ht[t1+1].hlen;
217 const char *hlen3 = ht[t1+2].hlen;
218 int t;
220 do {
221 int x = ix[0] * xlen + ix[1];
222 ix += 2;
223 sum1 += hlen1[x];
224 sum2 += hlen2[x];
225 sum3 += hlen3[x];
226 } while (ix < end);
228 t = t1;
229 if (sum1 > sum2) {
230 sum1 = sum2;
231 t++;
233 if (sum1 > sum3) {
234 sum1 = sum3;
235 t = t1+2;
237 *s += sum1;
239 return t;
243 /*************************************************************************/
244 /* choose table */
245 /*************************************************************************/
248 Choose the Huffman table that will encode ix[begin..end] with
249 the fewest bits.
251 Note: This code contains knowledge about the sizes and characteristics
252 of the Huffman tables as defined in the IS (Table B.7), and will not work
253 with any arbitrary tables.
256 static int choose_table_nonMMX(
257 const int * ix,
258 const int * const end,
259 int * const s )
261 int max;
262 int choice, choice2;
263 static const int huf_tbl_noESC[] = {
264 1, 2, 5, 7, 7,10,10,13,13,13,13,13,13,13,13 /* char not enough ? */
267 max = ix_max(ix, end);
269 switch (max) {
270 case 0:
271 return max;
273 case 1:
274 return count_bit_noESC(ix, end, s);
276 case 2:
277 case 3:
278 return count_bit_noESC_from2(ix, end, huf_tbl_noESC[max - 1], s);
280 case 4: case 5: case 6:
281 case 7: case 8: case 9:
282 case 10: case 11: case 12:
283 case 13: case 14: case 15:
284 return count_bit_noESC_from3(ix, end, huf_tbl_noESC[max - 1], s);
286 default:
287 /* try tables with linbits */
288 if (max > IXMAX_VAL) {
289 *s = LARGE_BITS;
290 return -1;
292 max -= 15;
293 for (choice2 = 24; choice2 < 32; choice2++) {
294 if (ht[choice2].linmax >= max) {
295 break;
299 for (choice = choice2 - 8; choice < 24; choice++) {
300 if (ht[choice].linmax >= max) {
301 break;
304 return count_bit_ESC(ix, end, choice, choice2, s);
310 /*************************************************************************/
311 /* count_bit */
312 /*************************************************************************/
314 int count_bits(
315 lame_internal_flags * const gfc,
316 int * const ix,
317 const FLOAT8 * const xr,
318 gr_info * const gi)
320 int bits = 0;
321 int i, a1, a2;
322 /* since quantize_xrpow uses table lookup, we need to check this first: */
323 FLOAT8 w = (IXMAX_VAL) / IPOW20(gi->global_gain);
324 for ( i = 0; i < 576; i++ ) {
325 if (xr[i] > w)
326 return LARGE_BITS;
329 if (gfc->quantization)
330 quantize_xrpow(xr, ix, IPOW20(gi->global_gain));
331 else
332 quantize_xrpow_ISO(xr, ix, IPOW20(gi->global_gain));
334 if (gfc->noise_shaping_amp==3) {
335 int sfb;
336 // 0.634521682242439 = 0.5946*2**(.5*0.1875)
337 FLOAT8 roundfac = 0.634521682242439 / IPOW20(gi->global_gain+gi->scalefac_scale);
338 i = 0;
339 for (sfb = 0; sfb < gi->sfb_lmax; sfb++) {
340 int end;
341 if (!gfc->pseudohalf.l[sfb])
342 continue;
344 end = gfc->scalefac_band.l[sfb+1];
345 for (; i < end; i++)
346 if (xr[i] < roundfac)
347 ix[i] = 0;
350 for (sfb = gi->sfb_smin; sfb < SBPSY_s; sfb++) {
351 int start, end, win;
352 start = gfc->scalefac_band.s[sfb];
353 end = gfc->scalefac_band.s[sfb+1];
354 for (win = 0; win < 3; win++) {
355 int j;
356 if (!gfc->pseudohalf.s[sfb][win])
357 continue;
358 for (j = start; j < end; j++, i++)
359 if (xr[i] < roundfac)
360 ix[i] = 0;
370 i=576;
371 /* Determine count1 region */
372 for (; i > 1; i -= 2)
373 if (ix[i - 1] | ix[i - 2])
374 break;
375 gi->count1 = i;
377 /* Determines the number of bits to encode the quadruples. */
378 a1 = a2 = 0;
379 for (; i > 3; i -= 4) {
380 int p;
381 /* hack to check if all values <= 1 */
382 if ((unsigned int)(ix[i-1] | ix[i-2] | ix[i-3] | ix[i-4]) > 1)
383 break;
385 p = ((ix[i-4] * 2 + ix[i-3]) * 2 + ix[i-2]) * 2 + ix[i-1];
386 a1 += t32l[p];
387 a2 += t33l[p];
390 bits = a1;
391 gi->count1table_select = 0;
392 if (a1 > a2) {
393 bits = a2;
394 gi->count1table_select = 1;
397 gi->count1bits = bits;
398 gi->big_values = i;
399 if (i == 0)
400 return bits;
402 if (gi->block_type == SHORT_TYPE) {
403 a1=3*gfc->scalefac_band.s[3];
404 if (a1 > gi->big_values) a1 = gi->big_values;
405 a2 = gi->big_values;
407 }else if (gi->block_type == NORM_TYPE) {
408 assert(i <= 576); /* bv_scf has 576 entries (0..575) */
409 a1 = gi->region0_count = gfc->bv_scf[i-2];
410 a2 = gi->region1_count = gfc->bv_scf[i-1];
412 assert(a1+a2+2 < SBPSY_l);
413 a2 = gfc->scalefac_band.l[a1 + a2 + 2];
414 a1 = gfc->scalefac_band.l[a1 + 1];
415 if (a2 < i)
416 gi->table_select[2] = gfc->choose_table(ix + a2, ix + i, &bits);
418 } else {
419 gi->region0_count = 7;
420 /*gi->region1_count = SBPSY_l - 7 - 1;*/
421 gi->region1_count = SBMAX_l -1 - 7 - 1;
422 a1 = gfc->scalefac_band.l[7 + 1];
423 a2 = i;
424 if (a1 > a2) {
425 a1 = a2;
430 /* have to allow for the case when bigvalues < region0 < region1 */
431 /* (and region0, region1 are ignored) */
432 a1 = Min(a1,i);
433 a2 = Min(a2,i);
435 assert( a1 >= 0 );
436 assert( a2 >= 0 );
438 /* Count the number of bits necessary to code the bigvalues region. */
439 if (0 < a1)
440 gi->table_select[0] = gfc->choose_table(ix, ix + a1, &bits);
441 if (a1 < a2)
442 gi->table_select[1] = gfc->choose_table(ix + a1, ix + a2, &bits);
443 return bits;
446 /***********************************************************************
447 re-calculate the best scalefac_compress using scfsi
448 the saved bits are kept in the bit reservoir.
449 **********************************************************************/
452 inline static void
453 recalc_divide_init(
454 const lame_internal_flags * const gfc,
455 gr_info cod_info,
456 int * const ix,
457 int r01_bits[],
458 int r01_div [],
459 int r0_tbl [],
460 int r1_tbl [] )
462 int r0, r1, bigv, r0t, r1t, bits;
464 bigv = cod_info.big_values;
466 for (r0 = 0; r0 <= 7 + 15; r0++) {
467 r01_bits[r0] = LARGE_BITS;
470 for (r0 = 0; r0 < 16; r0++) {
471 int a1 = gfc->scalefac_band.l[r0 + 1], r0bits;
472 if (a1 >= bigv)
473 break;
474 r0bits = cod_info.part2_length;
475 r0t = gfc->choose_table(ix, ix + a1, &r0bits);
477 for (r1 = 0; r1 < 8; r1++) {
478 int a2 = gfc->scalefac_band.l[r0 + r1 + 2];
479 if (a2 >= bigv)
480 break;
482 bits = r0bits;
483 r1t = gfc->choose_table(ix + a1, ix + a2, &bits);
484 if (r01_bits[r0 + r1] > bits) {
485 r01_bits[r0 + r1] = bits;
486 r01_div[r0 + r1] = r0;
487 r0_tbl[r0 + r1] = r0t;
488 r1_tbl[r0 + r1] = r1t;
494 inline static void
495 recalc_divide_sub(
496 const lame_internal_flags * const gfc,
497 const gr_info cod_info2,
498 gr_info * const gi,
499 const int * const ix,
500 const int r01_bits[],
501 const int r01_div [],
502 const int r0_tbl [],
503 const int r1_tbl [] )
505 int bits, r2, a2, bigv, r2t;
507 bigv = cod_info2.big_values;
509 for (r2 = 2; r2 < SBMAX_l + 1; r2++) {
510 a2 = gfc->scalefac_band.l[r2];
511 if (a2 >= bigv)
512 break;
514 bits = r01_bits[r2 - 2] + cod_info2.count1bits;
515 if (gi->part2_3_length <= bits)
516 break;
518 r2t = gfc->choose_table(ix + a2, ix + bigv, &bits);
519 if (gi->part2_3_length <= bits)
520 continue;
522 memcpy(gi, &cod_info2, sizeof(gr_info));
523 gi->part2_3_length = bits;
524 gi->region0_count = r01_div[r2 - 2];
525 gi->region1_count = r2 - 2 - r01_div[r2 - 2];
526 gi->table_select[0] = r0_tbl[r2 - 2];
527 gi->table_select[1] = r1_tbl[r2 - 2];
528 gi->table_select[2] = r2t;
535 void best_huffman_divide(
536 const lame_internal_flags * const gfc,
537 gr_info * const gi,
538 int * const ix )
540 int i, a1, a2;
541 gr_info cod_info2;
543 int r01_bits[7 + 15 + 1];
544 int r01_div[7 + 15 + 1];
545 int r0_tbl[7 + 15 + 1];
546 int r1_tbl[7 + 15 + 1];
549 /* SHORT BLOCK stuff fails for MPEG2 */
550 if (gi->block_type == SHORT_TYPE && gfc->mode_gr==1)
551 return;
554 memcpy(&cod_info2, gi, sizeof(gr_info));
555 if (gi->block_type == NORM_TYPE) {
556 recalc_divide_init(gfc, cod_info2, ix, r01_bits,r01_div,r0_tbl,r1_tbl);
557 recalc_divide_sub(gfc, cod_info2, gi, ix, r01_bits,r01_div,r0_tbl,r1_tbl);
560 i = cod_info2.big_values;
561 if (i == 0 || (unsigned int)(ix[i-2] | ix[i-1]) > 1)
562 return;
564 i = gi->count1 + 2;
565 if (i > 576)
566 return;
568 /* Determines the number of bits to encode the quadruples. */
569 memcpy(&cod_info2, gi, sizeof(gr_info));
570 cod_info2.count1 = i;
571 a1 = a2 = 0;
573 assert(i <= 576);
575 for (; i > cod_info2.big_values; i -= 4) {
576 int p = ((ix[i-4] * 2 + ix[i-3]) * 2 + ix[i-2]) * 2 + ix[i-1];
577 a1 += t32l[p];
578 a2 += t33l[p];
580 cod_info2.big_values = i;
582 cod_info2.count1table_select = 0;
583 if (a1 > a2) {
584 a1 = a2;
585 cod_info2.count1table_select = 1;
588 cod_info2.count1bits = a1;
589 cod_info2.part2_3_length = a1 + cod_info2.part2_length;
591 if (cod_info2.block_type == NORM_TYPE)
592 recalc_divide_sub(gfc, cod_info2, gi, ix, r01_bits,r01_div,r0_tbl,r1_tbl);
593 else {
594 /* Count the number of bits necessary to code the bigvalues region. */
595 a1 = gfc->scalefac_band.l[7 + 1];
596 if (a1 > i) {
597 a1 = i;
599 if (a1 > 0)
600 cod_info2.table_select[0] =
601 gfc->choose_table(ix, ix + a1, (int *)&cod_info2.part2_3_length);
602 if (i > a1)
603 cod_info2.table_select[1] =
604 gfc->choose_table(ix + a1, ix + i, (int *)&cod_info2.part2_3_length);
605 if (gi->part2_3_length > cod_info2.part2_3_length)
606 memcpy(gi, &cod_info2, sizeof(gr_info));
610 static const int slen1_n[16] = { 1, 1, 1, 1, 8, 2, 2, 2, 4, 4, 4, 8, 8, 8,16,16 };
611 static const int slen2_n[16] = { 1, 2, 4, 8, 1, 2, 4, 8, 2, 4, 8, 2, 4, 8, 4, 8 };
613 void
614 scfsi_calc(int ch,
615 III_side_info_t *l3_side,
616 III_scalefac_t scalefac[2][2])
618 int i, s1, s2, c1, c2;
619 int sfb;
620 gr_info *gi = &l3_side->gr[1].ch[ch].tt;
622 static const int scfsi_band[5] = { 0, 6, 11, 16, 21 };
623 #if 0
624 static const int slen1_n[16] = { 0, 1, 1, 1, 8, 2, 2, 2, 4, 4, 4, 8, 8, 8,16,16 };
625 static const int slen2_n[16] = { 0, 2, 4, 8, 1, 2, 4, 8, 2, 4, 8, 2, 4, 8, 4, 8 };
626 #endif
628 for (i = 0; i < 4; i++)
629 l3_side->scfsi[ch][i] = 0;
631 for (i = 0; i < (sizeof(scfsi_band) / sizeof(int)) - 1; i++) {
632 for (sfb = scfsi_band[i]; sfb < scfsi_band[i + 1]; sfb++) {
633 if (scalefac[0][ch].l[sfb] != scalefac[1][ch].l[sfb])
634 break;
636 if (sfb == scfsi_band[i + 1]) {
637 for (sfb = scfsi_band[i]; sfb < scfsi_band[i + 1]; sfb++) {
638 scalefac[1][ch].l[sfb] = -1;
640 l3_side->scfsi[ch][i] = 1;
644 s1 = c1 = 0;
645 for (sfb = 0; sfb < 11; sfb++) {
646 if (scalefac[1][ch].l[sfb] < 0)
647 continue;
648 c1++;
649 if (s1 < scalefac[1][ch].l[sfb])
650 s1 = scalefac[1][ch].l[sfb];
653 s2 = c2 = 0;
654 for (; sfb < SBPSY_l; sfb++) {
655 if (scalefac[1][ch].l[sfb] < 0)
656 continue;
657 c2++;
658 if (s2 < scalefac[1][ch].l[sfb])
659 s2 = scalefac[1][ch].l[sfb];
662 for (i = 0; i < 16; i++) {
663 if (s1 < slen1_n[i] && s2 < slen2_n[i]) {
664 int c = slen1_tab[i] * c1 + slen2_tab[i] * c2;
665 if (gi->part2_length > c) {
666 gi->part2_length = c;
667 gi->scalefac_compress = i;
674 Find the optimal way to store the scalefactors.
675 Only call this routine after final scalefactors have been
676 chosen and the channel/granule will not be re-encoded.
678 void best_scalefac_store(
679 const lame_internal_flags *gfc,
680 const int gr,
681 const int ch,
682 int l3_enc[2][2][576],
683 III_side_info_t * const l3_side,
684 III_scalefac_t scalefac[2][2] )
687 /* use scalefac_scale if we can */
688 gr_info *gi = &l3_side->gr[gr].ch[ch].tt;
689 int sfb,i,j,j2,l,start,end;
691 /* remove scalefacs from bands with ix=0. This idea comes
692 * from the AAC ISO docs. added mt 3/00 */
693 /* check if l3_enc=0 */
694 for ( sfb = 0; sfb < gi->sfb_lmax; sfb++ ) {
695 if (scalefac[gr][ch].l[sfb]>0) {
696 start = gfc->scalefac_band.l[ sfb ];
697 end = gfc->scalefac_band.l[ sfb+1 ];
698 for ( l = start; l < end; l++ ) if (l3_enc[gr][ch][l]!=0) break;
699 if (l==end) scalefac[gr][ch].l[sfb]=0;
702 for ( j=0, sfb = gi->sfb_smin; sfb < SBPSY_s; sfb++ ) {
703 start = gfc->scalefac_band.s[ sfb ];
704 end = gfc->scalefac_band.s[ sfb+1 ];
705 for ( i = 0; i < 3; i++ ) {
706 if (scalefac[gr][ch].s[sfb][i]>0) {
707 j2 = j;
708 for ( l = start; l < end; l++ )
709 if (l3_enc[gr][ch][j2++ /*3*l+i*/]!=0) break;
710 if (l==end) scalefac[gr][ch].s[sfb][i]=0;
712 j += end-start;
717 gi->part2_3_length -= gi->part2_length;
718 if (!gi->scalefac_scale && !gi->preflag) {
719 int b, s = 0;
720 for (sfb = 0; sfb < gi->sfb_lmax; sfb++) {
721 s |= scalefac[gr][ch].l[sfb];
724 for (sfb = gi->sfb_smin; sfb < SBPSY_s; sfb++) {
725 for (b = 0; b < 3; b++) {
726 s |= scalefac[gr][ch].s[sfb][b];
730 if (!(s & 1) && s != 0) {
731 for (sfb = 0; sfb < gi->sfb_lmax; sfb++) {
732 scalefac[gr][ch].l[sfb] /= 2;
734 for (sfb = gi->sfb_smin; sfb < SBPSY_s; sfb++) {
735 for (b = 0; b < 3; b++) {
736 scalefac[gr][ch].s[sfb][b] /= 2;
740 gi->scalefac_scale = 1;
741 gi->part2_length = 99999999;
742 if (gfc->mode_gr == 2) {
743 scale_bitcount(&scalefac[gr][ch], gi);
744 } else {
745 scale_bitcount_lsf(gfc,&scalefac[gr][ch], gi);
751 for ( i = 0; i < 4; i++ )
752 l3_side->scfsi[ch][i] = 0;
754 if (gfc->mode_gr==2 && gr == 1
755 && l3_side->gr[0].ch[ch].tt.block_type != SHORT_TYPE
756 && l3_side->gr[1].ch[ch].tt.block_type != SHORT_TYPE) {
757 scfsi_calc(ch, l3_side, scalefac);
759 gi->part2_3_length += gi->part2_length;
763 /* number of bits used to encode scalefacs */
765 /* 18*slen1_tab[i] + 18*slen2_tab[i] */
766 static const int scale_short[16] = {
767 0, 18, 36, 54, 54, 36, 54, 72, 54, 72, 90, 72, 90, 108, 108, 126 };
769 /* 17*slen1_tab[i] + 18*slen2_tab[i] */
770 static const int scale_mixed[16] = {
771 0, 18, 36, 54, 51, 35, 53, 71, 52, 70, 88, 69, 87, 105, 104, 122 };
773 /* 11*slen1_tab[i] + 10*slen2_tab[i] */
774 static const int scale_long[16] = {
775 0, 10, 20, 30, 33, 21, 31, 41, 32, 42, 52, 43, 53, 63, 64, 74 };
778 /*************************************************************************/
779 /* scale_bitcount */
780 /*************************************************************************/
782 /* Also calculates the number of bits necessary to code the scalefactors. */
784 int scale_bitcount(
785 III_scalefac_t * const scalefac, gr_info * const cod_info)
787 int i, k, sfb, max_slen1 = 0, max_slen2 = 0, ep = 2;
789 /* maximum values */
790 const int *tab;
793 if ( cod_info->block_type == SHORT_TYPE ) {
794 tab = scale_short;
795 if (cod_info->mixed_block_flag) {
796 tab = scale_mixed;
797 for ( sfb = 0 ; sfb < cod_info->sfb_lmax; sfb++ )
798 if (max_slen1 < scalefac->l[sfb])
799 max_slen1 = scalefac->l[sfb];
802 for ( i = 0; i < 3; i++ ) {
803 for ( sfb = cod_info->sfb_smin; sfb < 6; sfb++ )
804 if (max_slen1 < scalefac->s[sfb][i])
805 max_slen1 = scalefac->s[sfb][i];
806 for (sfb = 6; sfb < SBPSY_s; sfb++ )
807 if (max_slen2 < scalefac->s[sfb][i])
808 max_slen2 = scalefac->s[sfb][i];
811 else
812 { /* block_type == 1,2,or 3 */
813 tab = scale_long;
814 for ( sfb = 0; sfb < 11; sfb++ )
815 if ( scalefac->l[sfb] > max_slen1 )
816 max_slen1 = scalefac->l[sfb];
818 if (!cod_info->preflag) {
819 for ( sfb = 11; sfb < SBPSY_l; sfb++ )
820 if (scalefac->l[sfb] < pretab[sfb])
821 break;
823 if (sfb == SBPSY_l) {
824 cod_info->preflag = 1;
825 for ( sfb = 11; sfb < SBPSY_l; sfb++ )
826 scalefac->l[sfb] -= pretab[sfb];
830 for ( sfb = 11; sfb < SBPSY_l; sfb++ )
831 if ( scalefac->l[sfb] > max_slen2 )
832 max_slen2 = scalefac->l[sfb];
836 /* from Takehiro TOMINAGA <tominaga@isoternet.org> 10/99
837 * loop over *all* posible values of scalefac_compress to find the
838 * one which uses the smallest number of bits. ISO would stop
839 * at first valid index */
840 cod_info->part2_length = LARGE_BITS;
841 for ( k = 0; k < 16; k++ )
843 if ( (max_slen1 < slen1_n[k]) && (max_slen2 < slen2_n[k]) &&
844 (cod_info->part2_length > tab[k])) {
845 cod_info->part2_length=tab[k];
846 cod_info->scalefac_compress=k;
847 ep=0; /* we found a suitable scalefac_compress */
850 return ep;
856 table of largest scalefactor values for MPEG2
858 static const int max_range_sfac_tab[6][4] =
860 { 15, 15, 7, 7},
861 { 15, 15, 7, 0},
862 { 7, 3, 0, 0},
863 { 15, 31, 31, 0},
864 { 7, 7, 7, 0},
865 { 3, 3, 0, 0}
871 /*************************************************************************/
872 /* scale_bitcount_lsf */
873 /*************************************************************************/
875 /* Also counts the number of bits to encode the scalefacs but for MPEG 2 */
876 /* Lower sampling frequencies (24, 22.05 and 16 kHz.) */
878 /* This is reverse-engineered from section 2.4.3.2 of the MPEG2 IS, */
879 /* "Audio Decoding Layer III" */
881 int scale_bitcount_lsf(const lame_internal_flags *gfc,
882 const III_scalefac_t * const scalefac, gr_info * const cod_info)
884 int table_number, row_in_table, partition, nr_sfb, window, over;
885 int i, sfb, max_sfac[ 4 ];
886 const int *partition_table;
889 Set partition table. Note that should try to use table one,
890 but do not yet...
892 if ( cod_info->preflag )
893 table_number = 2;
894 else
895 table_number = 0;
897 for ( i = 0; i < 4; i++ )
898 max_sfac[i] = 0;
900 if ( cod_info->block_type == SHORT_TYPE )
902 row_in_table = 1;
903 partition_table = &nr_of_sfb_block[table_number][row_in_table][0];
904 for ( sfb = 0, partition = 0; partition < 4; partition++ )
906 nr_sfb = partition_table[ partition ] / 3;
907 for ( i = 0; i < nr_sfb; i++, sfb++ )
908 for ( window = 0; window < 3; window++ )
909 if ( scalefac->s[sfb][window] > max_sfac[partition] )
910 max_sfac[partition] = scalefac->s[sfb][window];
913 else
915 row_in_table = 0;
916 partition_table = &nr_of_sfb_block[table_number][row_in_table][0];
917 for ( sfb = 0, partition = 0; partition < 4; partition++ )
919 nr_sfb = partition_table[ partition ];
920 for ( i = 0; i < nr_sfb; i++, sfb++ )
921 if ( scalefac->l[sfb] > max_sfac[partition] )
922 max_sfac[partition] = scalefac->l[sfb];
926 for ( over = 0, partition = 0; partition < 4; partition++ )
928 if ( max_sfac[partition] > max_range_sfac_tab[table_number][partition] )
929 over++;
931 if ( !over )
934 Since no bands have been over-amplified, we can set scalefac_compress
935 and slen[] for the formatter
937 static const int log2tab[] = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 };
939 int slen1, slen2, slen3, slen4;
941 cod_info->sfb_partition_table = nr_of_sfb_block[table_number][row_in_table];
942 for ( partition = 0; partition < 4; partition++ )
943 cod_info->slen[partition] = log2tab[max_sfac[partition]];
945 /* set scalefac_compress */
946 slen1 = cod_info->slen[ 0 ];
947 slen2 = cod_info->slen[ 1 ];
948 slen3 = cod_info->slen[ 2 ];
949 slen4 = cod_info->slen[ 3 ];
951 switch ( table_number )
953 case 0:
954 cod_info->scalefac_compress = (((slen1 * 5) + slen2) << 4)
955 + (slen3 << 2)
956 + slen4;
957 break;
959 case 1:
960 cod_info->scalefac_compress = 400
961 + (((slen1 * 5) + slen2) << 2)
962 + slen3;
963 break;
965 case 2:
966 cod_info->scalefac_compress = 500 + (slen1 * 3) + slen2;
967 break;
969 default:
970 ERRORF(gfc,"intensity stereo not implemented yet\n" );
971 break;
974 #ifdef DEBUG
975 if ( over )
976 ERRORF(gfc, "---WARNING !! Amplification of some bands over limits\n" );
977 #endif
978 if (!over) {
979 assert( cod_info->sfb_partition_table );
980 cod_info->part2_length=0;
981 for ( partition = 0; partition < 4; partition++ )
982 cod_info->part2_length += cod_info->slen[partition] * cod_info->sfb_partition_table[partition];
984 return over;
989 void huffman_init(lame_internal_flags * const gfc)
991 int i;
993 gfc->choose_table = choose_table_nonMMX;
995 #ifdef MMX_choose_table
996 if (gfc->CPU_features.MMX) {
997 extern int choose_table_MMX(const int *ix, const int *end, int *s);
998 gfc->choose_table = choose_table_MMX;
1000 #endif
1002 for (i = 2; i <= 576; i += 2) {
1003 int scfb_anz = 0, index;
1004 while (gfc->scalefac_band.l[++scfb_anz] < i)
1007 index = subdv_table[scfb_anz].region0_count;
1008 while (gfc->scalefac_band.l[index + 1] > i)
1009 index--;
1011 if (index < 0) {
1012 /* this is an indication that everything is going to
1013 be encoded as region0: bigvalues < region0 < region1
1014 so lets set region0, region1 to some value larger
1015 than bigvalues */
1016 index = subdv_table[scfb_anz].region0_count;
1019 gfc->bv_scf[i-2] = index;
1021 index = subdv_table[scfb_anz].region1_count;
1022 while (gfc->scalefac_band.l[index + gfc->bv_scf[i-2] + 2] > i)
1023 index--;
1025 if (index < 0) {
1026 index = subdv_table[scfb_anz].region1_count;
1029 gfc->bv_scf[i-1] = index;