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"
30 #include "quantize_pvt.h"
38 const int region0_count
;
39 const int region1_count
;
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 /*************************************************************************/
72 /*************************************************************************/
75 ix_max(const int *ix
, const int *end
)
77 int max1
= 0, max2
= 0;
103 const int * const end
,
108 /* ESC-table is used */
109 int linbits
= ht
[t1
].xlen
* 65536 + ht
[t2
].xlen
;
149 count_bit_noESC(const int * ix
, const int * const end
, int * const s
)
153 const char *hlen1
= ht
[1].hlen
;
156 int x
= ix
[0] * 2 + ix
[1];
168 count_bit_noESC_from2(
170 const int * const end
,
175 unsigned int sum
= 0, sum2
;
176 const int xlen
= ht
[t1
].xlen
;
177 const unsigned int *hlen
;
184 int x
= ix
[0] * xlen
+ ix
[1];
203 count_bit_noESC_from3(
205 const int * const end
,
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
;
220 int x
= ix
[0] * xlen
+ ix
[1];
242 /*************************************************************************/
244 /*************************************************************************/
247 Choose the Huffman table that will encode ix[begin..end] with
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(
257 const int * const end
,
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
);
273 return count_bit_noESC(ix
, end
, s
);
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
);
286 /* try tables with linbits */
287 if (max
> IXMAX_VAL
) {
292 for (choice2
= 24; choice2
< 32; choice2
++) {
293 if (ht
[choice2
].linmax
>= max
) {
298 for (choice
= choice2
- 8; choice
< 24; choice
++) {
299 if (ht
[choice
].linmax
>= max
) {
303 return count_bit_ESC(ix
, end
, choice
, choice2
, s
);
309 /*************************************************************************/
311 /*************************************************************************/
314 lame_internal_flags
* const gfc
,
316 const FLOAT8
* const xr
,
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
++ ) {
328 if (gfc
->quantization
)
329 quantize_xrpow(xr
, ix
, IPOW20(gi
->global_gain
));
331 quantize_xrpow_ISO(xr
, ix
, IPOW20(gi
->global_gain
));
333 if (gfc
->noise_shaping_amp
==3) {
335 // 0.634521682242439 = 0.5946*2**(.5*0.1875)
336 FLOAT8 roundfac
= 0.634521682242439 / IPOW20(gi
->global_gain
+gi
->scalefac_scale
);
338 for (sfb
= 0; sfb
< gi
->sfb_lmax
; sfb
++) {
340 if (!gfc
->pseudohalf
.l
[sfb
])
343 end
= gfc
->scalefac_band
.l
[sfb
+1];
345 if (xr
[i
] < roundfac
)
349 for (sfb
= gi
->sfb_smin
; sfb
< SBPSY_s
; sfb
++) {
351 start
= gfc
->scalefac_band
.s
[sfb
];
352 end
= gfc
->scalefac_band
.s
[sfb
+1];
353 for (win
= 0; win
< 3; win
++) {
355 if (!gfc
->pseudohalf
.s
[sfb
][win
])
357 for (j
= start
; j
< end
; j
++, i
++)
358 if (xr
[i
] < roundfac
)
370 /* Determine count1 region */
371 for (; i
> 1; i
-= 2)
372 if (ix
[i
- 1] | ix
[i
- 2])
376 /* Determines the number of bits to encode the quadruples. */
378 for (; i
> 3; i
-= 4) {
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)
384 p
= ((ix
[i
-4] * 2 + ix
[i
-3]) * 2 + ix
[i
-2]) * 2 + ix
[i
-1];
390 gi
->count1table_select
= 0;
393 gi
->count1table_select
= 1;
396 gi
->count1bits
= 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
;
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];
415 gi
->table_select
[2] = gfc
->choose_table(ix
+ a2
, ix
+ i
, &bits
);
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];
429 /* have to allow for the case when bigvalues < region0 < region1 */
430 /* (and region0, region1 are ignored) */
437 /* Count the number of bits necessary to code the bigvalues region. */
439 gi
->table_select
[0] = gfc
->choose_table(ix
, ix
+ a1
, &bits
);
441 gi
->table_select
[1] = gfc
->choose_table(ix
+ a1
, ix
+ a2
, &bits
);
445 /***********************************************************************
446 re-calculate the best scalefac_compress using scfsi
447 the saved bits are kept in the bit reservoir.
448 **********************************************************************/
453 const lame_internal_flags
* const gfc
,
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
;
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];
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
;
495 const lame_internal_flags
* const gfc
,
496 const gr_info cod_info2
,
498 const int * const ix
,
499 const int r01_bits
[],
500 const int r01_div
[],
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
];
513 bits
= r01_bits
[r2
- 2] + cod_info2
.count1bits
;
514 if (gi
->part2_3_length
<= bits
)
517 r2t
= gfc
->choose_table(ix
+ a2
, ix
+ bigv
, &bits
);
518 if (gi
->part2_3_length
<= bits
)
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
,
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)
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)
567 /* Determines the number of bits to encode the quadruples. */
568 memcpy(&cod_info2
, gi
, sizeof(gr_info
));
569 cod_info2
.count1
= i
;
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];
579 cod_info2
.big_values
= i
;
581 cod_info2
.count1table_select
= 0;
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
);
593 /* Count the number of bits necessary to code the bigvalues region. */
594 a1
= gfc
->scalefac_band
.l
[7 + 1];
599 cod_info2
.table_select
[0] =
600 gfc
->choose_table(ix
, ix
+ a1
, (int *)&cod_info2
.part2_3_length
);
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 };
614 III_side_info_t
*l3_side
,
615 III_scalefac_t scalefac
[2][2])
617 int i
, s1
, s2
, c1
, c2
;
619 gr_info
*gi
= &l3_side
->gr
[1].ch
[ch
].tt
;
621 static const int scfsi_band
[5] = { 0, 6, 11, 16, 21 };
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 };
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
])
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;
644 for (sfb
= 0; sfb
< 11; sfb
++) {
645 if (scalefac
[1][ch
].l
[sfb
] < 0)
648 if (s1
< scalefac
[1][ch
].l
[sfb
])
649 s1
= scalefac
[1][ch
].l
[sfb
];
653 for (; sfb
< SBPSY_l
; sfb
++) {
654 if (scalefac
[1][ch
].l
[sfb
] < 0)
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
,
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) {
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;
716 gi
->part2_3_length
-= gi
->part2_length
;
717 if (!gi
->scalefac_scale
&& !gi
->preflag
) {
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
);
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 /*************************************************************************/
779 /*************************************************************************/
781 /* Also calculates the number of bits necessary to code the scalefactors. */
784 III_scalefac_t
* const scalefac
, gr_info
* const cod_info
)
786 int i
, k
, sfb
, max_slen1
= 0, max_slen2
= 0, ep
= 2;
792 if ( cod_info
->block_type
== SHORT_TYPE
) {
794 if (cod_info
->mixed_block_flag
) {
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
];
811 { /* block_type == 1,2,or 3 */
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
])
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 */
855 table of largest scalefactor values for MPEG2
857 static const int max_range_sfac_tab
[6][4] =
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,
891 if ( cod_info
->preflag
)
896 for ( i
= 0; i
< 4; i
++ )
899 if ( cod_info
->block_type
== SHORT_TYPE
)
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
];
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
] )
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
)
953 cod_info
->scalefac_compress
= (((slen1
* 5) + slen2
) << 4)
959 cod_info
->scalefac_compress
= 400
960 + (((slen1
* 5) + slen2
) << 2)
965 cod_info
->scalefac_compress
= 500 + (slen1
* 3) + slen2
;
969 ERRORF(gfc
,"intensity stereo not implemented yet\n" );
975 ERRORF(gfc
, "---WARNING !! Amplification of some bands over limits\n" );
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
];
988 void huffman_init(lame_internal_flags
* const gfc
)
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
;
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
)
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
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
)
1025 index
= subdv_table
[scfb_anz
].region1_count
;
1028 gfc
->bv_scf
[i
-1] = index
;