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 $ */
25 #include "config_static.h"
31 #include "quantize_pvt.h"
39 const int region0_count
;
40 const int region1_count
;
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 /*************************************************************************/
73 /*************************************************************************/
76 ix_max(const int *ix
, const int *end
)
78 int max1
= 0, max2
= 0;
104 const int * const end
,
109 /* ESC-table is used */
110 int linbits
= ht
[t1
].xlen
* 65536 + ht
[t2
].xlen
;
150 count_bit_noESC(const int * ix
, const int * const end
, int * const s
)
154 const char *hlen1
= ht
[1].hlen
;
157 int x
= ix
[0] * 2 + ix
[1];
169 count_bit_noESC_from2(
171 const int * const end
,
176 unsigned int sum
= 0, sum2
;
177 const int xlen
= ht
[t1
].xlen
;
178 const unsigned int *hlen
;
185 int x
= ix
[0] * xlen
+ ix
[1];
204 count_bit_noESC_from3(
206 const int * const end
,
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
;
221 int x
= ix
[0] * xlen
+ ix
[1];
243 /*************************************************************************/
245 /*************************************************************************/
248 Choose the Huffman table that will encode ix[begin..end] with
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(
258 const int * const end
,
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
);
274 return count_bit_noESC(ix
, end
, s
);
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
);
287 /* try tables with linbits */
288 if (max
> IXMAX_VAL
) {
293 for (choice2
= 24; choice2
< 32; choice2
++) {
294 if (ht
[choice2
].linmax
>= max
) {
299 for (choice
= choice2
- 8; choice
< 24; choice
++) {
300 if (ht
[choice
].linmax
>= max
) {
304 return count_bit_ESC(ix
, end
, choice
, choice2
, s
);
310 /*************************************************************************/
312 /*************************************************************************/
315 lame_internal_flags
* const gfc
,
317 const FLOAT8
* const xr
,
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
++ ) {
329 if (gfc
->quantization
)
330 quantize_xrpow(xr
, ix
, IPOW20(gi
->global_gain
));
332 quantize_xrpow_ISO(xr
, ix
, IPOW20(gi
->global_gain
));
334 if (gfc
->noise_shaping_amp
==3) {
336 // 0.634521682242439 = 0.5946*2**(.5*0.1875)
337 FLOAT8 roundfac
= 0.634521682242439 / IPOW20(gi
->global_gain
+gi
->scalefac_scale
);
339 for (sfb
= 0; sfb
< gi
->sfb_lmax
; sfb
++) {
341 if (!gfc
->pseudohalf
.l
[sfb
])
344 end
= gfc
->scalefac_band
.l
[sfb
+1];
346 if (xr
[i
] < roundfac
)
350 for (sfb
= gi
->sfb_smin
; sfb
< SBPSY_s
; sfb
++) {
352 start
= gfc
->scalefac_band
.s
[sfb
];
353 end
= gfc
->scalefac_band
.s
[sfb
+1];
354 for (win
= 0; win
< 3; win
++) {
356 if (!gfc
->pseudohalf
.s
[sfb
][win
])
358 for (j
= start
; j
< end
; j
++, i
++)
359 if (xr
[i
] < roundfac
)
371 /* Determine count1 region */
372 for (; i
> 1; i
-= 2)
373 if (ix
[i
- 1] | ix
[i
- 2])
377 /* Determines the number of bits to encode the quadruples. */
379 for (; i
> 3; i
-= 4) {
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)
385 p
= ((ix
[i
-4] * 2 + ix
[i
-3]) * 2 + ix
[i
-2]) * 2 + ix
[i
-1];
391 gi
->count1table_select
= 0;
394 gi
->count1table_select
= 1;
397 gi
->count1bits
= 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
;
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];
416 gi
->table_select
[2] = gfc
->choose_table(ix
+ a2
, ix
+ i
, &bits
);
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];
430 /* have to allow for the case when bigvalues < region0 < region1 */
431 /* (and region0, region1 are ignored) */
438 /* Count the number of bits necessary to code the bigvalues region. */
440 gi
->table_select
[0] = gfc
->choose_table(ix
, ix
+ a1
, &bits
);
442 gi
->table_select
[1] = gfc
->choose_table(ix
+ a1
, ix
+ a2
, &bits
);
446 /***********************************************************************
447 re-calculate the best scalefac_compress using scfsi
448 the saved bits are kept in the bit reservoir.
449 **********************************************************************/
454 const lame_internal_flags
* const gfc
,
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
;
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];
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
;
496 const lame_internal_flags
* const gfc
,
497 const gr_info cod_info2
,
499 const int * const ix
,
500 const int r01_bits
[],
501 const int r01_div
[],
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
];
514 bits
= r01_bits
[r2
- 2] + cod_info2
.count1bits
;
515 if (gi
->part2_3_length
<= bits
)
518 r2t
= gfc
->choose_table(ix
+ a2
, ix
+ bigv
, &bits
);
519 if (gi
->part2_3_length
<= bits
)
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
,
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)
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)
568 /* Determines the number of bits to encode the quadruples. */
569 memcpy(&cod_info2
, gi
, sizeof(gr_info
));
570 cod_info2
.count1
= i
;
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];
580 cod_info2
.big_values
= i
;
582 cod_info2
.count1table_select
= 0;
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
);
594 /* Count the number of bits necessary to code the bigvalues region. */
595 a1
= gfc
->scalefac_band
.l
[7 + 1];
600 cod_info2
.table_select
[0] =
601 gfc
->choose_table(ix
, ix
+ a1
, (int *)&cod_info2
.part2_3_length
);
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 };
615 III_side_info_t
*l3_side
,
616 III_scalefac_t scalefac
[2][2])
618 int i
, s1
, s2
, c1
, c2
;
620 gr_info
*gi
= &l3_side
->gr
[1].ch
[ch
].tt
;
622 static const int scfsi_band
[5] = { 0, 6, 11, 16, 21 };
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 };
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
])
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;
645 for (sfb
= 0; sfb
< 11; sfb
++) {
646 if (scalefac
[1][ch
].l
[sfb
] < 0)
649 if (s1
< scalefac
[1][ch
].l
[sfb
])
650 s1
= scalefac
[1][ch
].l
[sfb
];
654 for (; sfb
< SBPSY_l
; sfb
++) {
655 if (scalefac
[1][ch
].l
[sfb
] < 0)
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
,
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) {
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;
717 gi
->part2_3_length
-= gi
->part2_length
;
718 if (!gi
->scalefac_scale
&& !gi
->preflag
) {
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
);
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 /*************************************************************************/
780 /*************************************************************************/
782 /* Also calculates the number of bits necessary to code the scalefactors. */
785 III_scalefac_t
* const scalefac
, gr_info
* const cod_info
)
787 int i
, k
, sfb
, max_slen1
= 0, max_slen2
= 0, ep
= 2;
793 if ( cod_info
->block_type
== SHORT_TYPE
) {
795 if (cod_info
->mixed_block_flag
) {
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
];
812 { /* block_type == 1,2,or 3 */
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
])
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 */
856 table of largest scalefactor values for MPEG2
858 static const int max_range_sfac_tab
[6][4] =
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,
892 if ( cod_info
->preflag
)
897 for ( i
= 0; i
< 4; i
++ )
900 if ( cod_info
->block_type
== SHORT_TYPE
)
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
];
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
] )
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
)
954 cod_info
->scalefac_compress
= (((slen1
* 5) + slen2
) << 4)
960 cod_info
->scalefac_compress
= 400
961 + (((slen1
* 5) + slen2
) << 2)
966 cod_info
->scalefac_compress
= 500 + (slen1
* 3) + slen2
;
970 ERRORF(gfc
,"intensity stereo not implemented yet\n" );
976 ERRORF(gfc
, "---WARNING !! Amplification of some bands over limits\n" );
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
];
989 void huffman_init(lame_internal_flags
* const gfc
)
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
;
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
)
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
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
)
1026 index
= subdv_table
[scfb_anz
].region1_count
;
1029 gfc
->bv_scf
[i
-1] = index
;