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.18 2001/02/27 09:59:18 robert Exp $ */
32 #include "quantize_pvt.h"
40 const int region0_count;
41 const int region1_count;
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 */
72 /*************************************************************************/
74 /*************************************************************************/
77 ix_max(const int *ix, const int *end)
79 int max1 = 0, max2 = 0;
98 const int * const end,
103 /* ESC-table is used */
104 int linbits = ht[t1].xlen * 65536 + ht[t2].xlen;
144 count_bit_noESC(const int * ix, const int * const end, int * const s)
148 const char *hlen1 = ht[1].hlen;
151 int x = ix[0] * 2 + ix[1];
163 count_bit_noESC_from2(
165 const int * const end,
170 unsigned int sum = 0, sum2;
171 const int xlen = ht[t1].xlen;
172 const unsigned int *hlen;
179 int x = ix[0] * xlen + ix[1];
198 count_bit_noESC_from3(
200 const int * const end,
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;
215 int x = ix[0] * xlen + ix[1];
237 /*************************************************************************/
239 /*************************************************************************/
242 Choose the Huffman table that will encode ix[begin..end] with
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.
253 const int * const end,
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 ? */
262 max = ix_max(ix, end);
269 return count_bit_noESC(ix, end, s);
273 return count_bit_noESC_from2(ix, end, huf_tbl_noESC[max - 1], s);
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);
282 /* try tables with linbits */
283 if (max > IXMAX_VAL) {
288 for (choice2 = 24; choice2 < 32; choice2++) {
289 if (ht[choice2].linmax >= max) {
294 for (choice = choice2 - 8; choice < 24; choice++) {
295 if (ht[choice].linmax >= max) {
299 return count_bit_ESC(ix, end, choice, choice2, s);
305 /*************************************************************************/
307 /*************************************************************************/
310 Function: Count the number of bits necessary to code the subregion.
314 int count_bits_long(lame_internal_flags * const gfc, const int ix[576], gr_info * const gi)
320 /* Determine count1 region */
321 for (; i > 1; i -= 2)
322 if (ix[i - 1] | ix[i - 2])
326 /* Determines the number of bits to encode the quadruples. */
328 for (; i > 3; i -= 4) {
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)
334 p = ((ix[i-4] * 2 + ix[i-3]) * 2 + ix[i-2]) * 2 + ix[i-1];
340 gi->count1table_select = 0;
343 gi->count1table_select = 1;
346 gi->count1bits = bits;
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;
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];
361 // assert(a1+a2+2 < SBPSY_l);
362 a2 = gfc->scalefac_band.l[a1 + a2 + 2];
363 a1 = gfc->scalefac_band.l[a1 + 1];
365 gi->table_select[2] = gfc->choose_table(ix + a2, ix + i, &bits);
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];
379 /* have to allow for the case when bigvalues < region0 < region1 */
380 /* (and region0, region1 are ignored) */
384 // assert( a1 >= 0 );
385 // assert( a2 >= 0 );
387 /* Count the number of bits necessary to code the bigvalues region. */
389 gi->table_select[0] = gfc->choose_table(ix, ix + a1, &bits);
391 gi->table_select[1] = gfc->choose_table(ix + a1, ix + a2, &bits);
400 lame_internal_flags * const gfc,
402 const FLOAT8 * const xr,
403 gr_info * const cod_info)
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++ ) {
413 if (gfc->quantization)
414 quantize_xrpow(xr, ix, IPOW20(cod_info->global_gain));
416 quantize_xrpow_ISO(xr, ix, IPOW20(cod_info->global_gain));
418 bits=count_bits_long(gfc, ix, cod_info);
423 /***********************************************************************
424 re-calculate the best scalefac_compress using scfsi
425 the saved bits are kept in the bit reservoir.
426 **********************************************************************/
431 const lame_internal_flags * const gfc,
439 int r0, r1, bigv, r0t, r1t, bits;
441 bigv = cod_info.big_values;
443 for (r0 = 0; r0 <= 7 + 15; r0++) {
444 r01_bits[r0] = LARGE_BITS;
447 for (r0 = 0; r0 < 16; r0++) {
448 int a1 = gfc->scalefac_band.l[r0 + 1], r0bits;
451 r0bits = cod_info.part2_length;
452 r0t = gfc->choose_table(ix, ix + a1, &r0bits);
454 for (r1 = 0; r1 < 8; r1++) {
455 int a2 = gfc->scalefac_band.l[r0 + r1 + 2];
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;
473 const lame_internal_flags * const gfc,
474 const gr_info cod_info2,
476 const int * const ix,
477 const int r01_bits[],
478 const int r01_div [],
480 const int r1_tbl [] )
482 int bits, r2, a2, bigv, r2t;
484 bigv = cod_info2.big_values;
486 for (r2 = 2; r2 < SBMAX_l + 1; r2++) {
487 a2 = gfc->scalefac_band.l[r2];
491 bits = r01_bits[r2 - 2] + cod_info2.count1bits;
492 if (gi->part2_3_length <= bits)
495 r2t = gfc->choose_table(ix + a2, ix + bigv, &bits);
496 if (gi->part2_3_length <= bits)
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;
512 void best_huffman_divide(
513 const lame_internal_flags * const gfc,
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];
528 /* SHORT BLOCK stuff fails for MPEG2 */
529 if (gi->block_type == SHORT_TYPE && gfc->mode_gr==1)
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);
539 i = cod_info2.big_values;
540 if (i == 0 || (unsigned int)(ix[i-2] | ix[i-1]) > 1)
547 /* Determines the number of bits to encode the quadruples. */
548 memcpy(&cod_info2, gi, sizeof(gr_info));
549 cod_info2.count1 = i;
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];
559 cod_info2.big_values = i;
561 cod_info2.count1table_select = 0;
564 cod_info2.count1table_select = 1;
567 cod_info2.count1bits = a1;
568 cod_info2.part2_3_length = a1 + cod_info2.part2_length;
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);
573 /* Count the number of bits necessary to code the bigvalues region. */
574 a1 = gfc->scalefac_band.l[7 + 1];
579 cod_info2.table_select[0] =
580 gfc->choose_table(ix, ix + a1, (int *)&cod_info2.part2_3_length);
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));
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 };
594 III_side_info_t *l3_side,
595 III_scalefac_t scalefac[2][2])
597 int i, s1, s2, c1, c2;
599 gr_info *gi = &l3_side->gr[1].ch[ch].tt;
601 static const int scfsi_band[5] = { 0, 6, 11, 16, 21 };
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 };
607 for (i = 0; i < 4; i++)
608 l3_side->scfsi[ch][i] = 0;
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])
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;
619 l3_side->scfsi[ch][i] = 1;
624 for (sfb = 0; sfb < 11; sfb++) {
625 if (scalefac[1][ch].l[sfb] < 0)
628 if (s1 < scalefac[1][ch].l[sfb])
629 s1 = scalefac[1][ch].l[sfb];
633 for (; sfb < SBPSY_l; sfb++) {
634 if (scalefac[1][ch].l[sfb] < 0)
637 if (s2 < scalefac[1][ch].l[sfb])
638 s2 = scalefac[1][ch].l[sfb];
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;
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.
657 void best_scalefac_store(
658 const lame_internal_flags *gfc,
661 int l3_enc[2][2][576],
662 III_side_info_t * const l3_side,
663 III_scalefac_t scalefac[2][2] )
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;
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;
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) {
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;
696 gi->part2_3_length -= gi->part2_length;
697 if (!gi->scalefac_scale && !gi->preflag) {
699 for (sfb = 0; sfb < gi->sfb_lmax; sfb++) {
700 s |= scalefac[gr][ch].l[sfb];
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];
709 if (!(s & 1) && s != 0) {
710 for (sfb = 0; sfb < gi->sfb_lmax; sfb++) {
711 scalefac[gr][ch].l[sfb] /= 2;
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;
719 gi->scalefac_scale = 1;
720 gi->part2_length = 99999999;
721 if (gfc->mode_gr == 2) {
722 scale_bitcount(&scalefac[gr][ch], gi);
724 scale_bitcount_lsf(gfc,&scalefac[gr][ch], gi);
730 for ( i = 0; i < 4; i++ )
731 l3_side->scfsi[ch][i] = 0;
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);
738 gi->part2_3_length += gi->part2_length;
742 /* number of bits used to encode scalefacs */
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 };
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 };
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 };
757 /*************************************************************************/
759 /*************************************************************************/
761 /* Also calculates the number of bits necessary to code the scalefactors. */
764 III_scalefac_t * const scalefac, gr_info * const cod_info)
766 int i, k, sfb, max_slen1 = 0, max_slen2 = 0, ep = 2;
772 if ( cod_info->block_type == SHORT_TYPE ) {
774 if (cod_info->mixed_block_flag) {
776 for ( sfb = 0 ; sfb < cod_info->sfb_lmax; sfb++ )
777 if (max_slen1 < scalefac->l[sfb])
778 max_slen1 = scalefac->l[sfb];
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];
791 { /* block_type == 1,2,or 3 */
793 for ( sfb = 0; sfb < 11; sfb++ )
794 if ( scalefac->l[sfb] > max_slen1 )
795 max_slen1 = scalefac->l[sfb];
797 if (!cod_info->preflag) {
798 for ( sfb = 11; sfb < SBPSY_l; sfb++ )
799 if (scalefac->l[sfb] < pretab[sfb])
802 if (sfb == SBPSY_l) {
803 cod_info->preflag = 1;
804 for ( sfb = 11; sfb < SBPSY_l; sfb++ )
805 scalefac->l[sfb] -= pretab[sfb];
809 for ( sfb = 11; sfb < SBPSY_l; sfb++ )
810 if ( scalefac->l[sfb] > max_slen2 )
811 max_slen2 = scalefac->l[sfb];
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++ )
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 */
835 table of largest scalefactor values for MPEG2
837 static const int max_range_sfac_tab[6][4] =
850 /*************************************************************************/
851 /* scale_bitcount_lsf */
852 /*************************************************************************/
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.) */
857 /* This is reverse-engineered from section 2.4.3.2 of the MPEG2 IS, */
858 /* "Audio Decoding Layer III" */
860 int scale_bitcount_lsf(const lame_internal_flags *gfc,
861 const III_scalefac_t * const scalefac, gr_info * const cod_info)
863 int table_number, row_in_table, partition, nr_sfb, window, over;
864 int i, sfb, max_sfac[ 4 ];
865 const int *partition_table;
868 Set partition table. Note that should try to use table one,
871 if ( cod_info->preflag )
876 for ( i = 0; i < 4; i++ )
879 if ( cod_info->block_type == SHORT_TYPE )
882 partition_table = &nr_of_sfb_block[table_number][row_in_table][0];
883 for ( sfb = 0, partition = 0; partition < 4; partition++ )
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];
895 partition_table = &nr_of_sfb_block[table_number][row_in_table][0];
896 for ( sfb = 0, partition = 0; partition < 4; partition++ )
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];
905 for ( over = 0, partition = 0; partition < 4; partition++ )
907 if ( max_sfac[partition] > max_range_sfac_tab[table_number][partition] )
913 Since no bands have been over-amplified, we can set scalefac_compress
914 and slen[] for the formatter
916 static const int log2tab[] = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 };
918 int slen1, slen2, slen3, slen4;
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]];
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 ];
930 switch ( table_number )
933 cod_info->scalefac_compress = (((slen1 * 5) + slen2) << 4)
939 cod_info->scalefac_compress = 400
940 + (((slen1 * 5) + slen2) << 2)
945 cod_info->scalefac_compress = 500 + (slen1 * 3) + slen2;
949 ERRORF(gfc,"intensity stereo not implemented yet\n" );
955 ERRORF(gfc, "---WARNING !! Amplification of some bands over limits\n" );
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];
968 void huffman_init(lame_internal_flags * const gfc)
972 gfc->choose_table = choose_table_nonMMX;
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;
981 for (i = 2; i <= 576; i += 2) {
982 int scfb_anz = 0, index;
983 while (gfc->scalefac_band.l[++scfb_anz] < i)
986 index = subdv_table[scfb_anz].region0_count;
987 while (gfc->scalefac_band.l[index + 1] > i)
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
995 index = subdv_table[scfb_anz].region0_count;
998 gfc->bv_scf[i-2] = index;
1000 index = subdv_table[scfb_anz].region1_count;
1001 while (gfc->scalefac_band.l[index + gfc->bv_scf[i-2] + 2] > i)
1005 index = subdv_table[scfb_anz].region1_count;
1008 gfc->bv_scf[i-1] = index;