2 * MP3 huffman table selecting and bit counting
4 * Copyright (c) 1999-2005 Takehiro TOMINAGA
5 * Copyright (c) 2002-2005 Gabriel Bouvigne
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 /* $Id: takehiro.c,v 1.79 2011/05/07 16:05:17 rbrito Exp $ */
34 #include "quantize_pvt.h"
39 const int region0_count;
40 const int region1_count;
94 /*********************************************************************
95 * nonlinear quantization of xr
96 * More accurate formula than the ISO formula. Takes into account
97 * the fact that we are quantizing xr -> ix, but we want ix^4/3 to be
98 * as close as possible to x^4/3. (taking the nearest int would mean
99 * ix is as close as possible to xr, which is different.)
101 * From Segher Boessenkool <segher@eastsite.nl> 11/1999
103 * 09/2000: ASM code removed in favor of IEEE754 hack by Takehiro
104 * Tominaga. If you need the ASM code, check CVS circa Aug 2000.
106 * 01/2004: Optimizations by Gabriel Bouvigne
107 *********************************************************************/
114 quantize_lines_xrpow_01(unsigned int l, FLOAT istep, const FLOAT * xr, int *ix)
116 const FLOAT compareval0 = (1.0f - 0.4054f) / istep;
121 for (i = 0; i < l; i += 2) {
122 FLOAT const xr_0 = xr[i+0];
123 FLOAT const xr_1 = xr[i+1];
124 int const ix_0 = (compareval0 > xr_0) ? 0 : 1;
125 int const ix_1 = (compareval0 > xr_1) ? 0 : 1;
133 #ifdef TAKEHIRO_IEEE754_HACK
140 #define MAGIC_FLOAT (65536*(128))
141 #define MAGIC_INT 0x4b000000
145 quantize_lines_xrpow(unsigned int l, FLOAT istep, const FLOAT * xp, int *pi)
148 unsigned int remaining;
152 fi = (fi_union *) pi;
158 double x0 = istep * xp[0];
159 double x1 = istep * xp[1];
160 double x2 = istep * xp[2];
161 double x3 = istep * xp[3];
172 fi[0].f = x0 + adj43asm[fi[0].i - MAGIC_INT];
173 fi[1].f = x1 + adj43asm[fi[1].i - MAGIC_INT];
174 fi[2].f = x2 + adj43asm[fi[2].i - MAGIC_INT];
175 fi[3].f = x3 + adj43asm[fi[3].i - MAGIC_INT];
177 fi[0].i -= MAGIC_INT;
178 fi[1].i -= MAGIC_INT;
179 fi[2].i -= MAGIC_INT;
180 fi[3].i -= MAGIC_INT;
185 double x0 = istep * xp[0];
186 double x1 = istep * xp[1];
193 fi[0].f = x0 + adj43asm[fi[0].i - MAGIC_INT];
194 fi[1].f = x1 + adj43asm[fi[1].i - MAGIC_INT];
196 fi[0].i -= MAGIC_INT;
197 fi[1].i -= MAGIC_INT;
205 /*********************************************************************
206 * XRPOW_FTOI is a macro to convert floats to ints.
207 * if XRPOW_FTOI(x) = nearest_int(x), then QUANTFAC(x)=adj43asm[x]
210 * if XRPOW_FTOI(x) = floor(x), then QUANTFAC(x)=asj43[x]
213 * Note: using floor() or (int) is extremely slow. On machines where
214 * the TAKEHIRO_IEEE754_HACK code above does not work, it is worthwile
215 * to write some ASM for XRPOW_FTOI().
216 *********************************************************************/
217 #define XRPOW_FTOI(src,dest) ((dest) = (int)(src))
218 #define QUANTFAC(rx) adj43[rx]
219 #define ROUNDFAC 0.4054
223 quantize_lines_xrpow(unsigned int l, FLOAT istep, const FLOAT * xr, int *ix)
225 unsigned int remaining;
233 FLOAT x0, x1, x2, x3;
234 int rx0, rx1, rx2, rx3;
246 XRPOW_FTOI(x0, *ix++);
248 XRPOW_FTOI(x1, *ix++);
250 XRPOW_FTOI(x2, *ix++);
251 XRPOW_FTOI(x3, *ix++);
263 XRPOW_FTOI(x0, *ix++);
264 XRPOW_FTOI(x1, *ix++);
275 /*********************************************************************
276 * Quantization function
277 * This function will select which lines to quantize and call the
278 * proper quantization function
279 *********************************************************************/
282 quantize_xrpow(const FLOAT * xp, int *pi, FLOAT istep, gr_info const *const cod_info,
283 calc_noise_data const *prev_noise)
285 /* quantize on xr^(3/4) instead of xr */
292 int accumulate01 = 0;
301 /* Reusing previously computed data does not seems to work if global gain
302 is changed. Finding why it behaves this way would allow to use a cache of
303 previously computed values (let's 10 cached values per sfb) that would
304 probably provide a noticeable speedup */
305 prev_data_use = (prev_noise && (cod_info->global_gain == prev_noise->global_gain));
307 if (cod_info->block_type == SHORT_TYPE)
312 for (sfb = 0; sfb <= sfbmax; sfb++) {
315 if (prev_data_use || cod_info->block_type == NORM_TYPE) {
317 cod_info->global_gain
318 - ((cod_info->scalefac[sfb] + (cod_info->preflag ? pretab[sfb] : 0))
319 << (cod_info->scalefac_scale + 1))
320 - cod_info->subblock_gain[cod_info->window[sfb]] * 8;
322 assert(cod_info->width[sfb] >= 0);
323 if (prev_data_use && (prev_noise->step[sfb] == step)) {
324 /* do not recompute this part,
325 but compute accumulated lines */
327 quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
331 quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
335 else { /*should compute this part */
337 l = cod_info->width[sfb];
339 if ((j + cod_info->width[sfb]) > cod_info->max_nonzero_coeff) {
340 /*do not compute upper zero part */
342 usefullsize = cod_info->max_nonzero_coeff - j + 1;
343 memset(&pi[cod_info->max_nonzero_coeff], 0,
344 sizeof(int) * (576 - cod_info->max_nonzero_coeff));
351 /* no need to compute higher sfb values */
355 /*accumulate lines to quantize */
356 if (!accumulate && !accumulate01) {
361 prev_noise->sfb_count1 > 0 &&
362 sfb >= prev_noise->sfb_count1 &&
363 prev_noise->step[sfb] > 0 && step >= prev_noise->step[sfb]) {
366 quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
375 quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
385 * may happen due to "prev_data_use" optimization
388 quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
392 quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
396 break; /* ends for-loop */
400 iData += cod_info->width[sfb];
401 xp += cod_info->width[sfb];
402 j += cod_info->width[sfb];
405 if (accumulate) { /*last data part */
406 quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
409 if (accumulate01) { /*last data part */
410 quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
419 /*************************************************************************/
421 /*************************************************************************/
424 ix_max(const int *ix, const int *end)
426 int max1 = 0, max2 = 0;
429 int const x1 = *ix++;
430 int const x2 = *ix++;
450 count_bit_ESC(const int *ix, const int *const end, int t1, const int t2, unsigned int *const s)
452 /* ESC-table is used */
453 unsigned int const linbits = ht[t1].xlen * 65536u + ht[t2].xlen;
454 unsigned int sum = 0, sum2;
457 unsigned int x = *ix++;
458 unsigned int y = *ix++;
473 sum2 = sum & 0xffffu;
487 count_bit_noESC(const int *ix, const int *end, int mx, unsigned int *s)
490 unsigned int sum1 = 0;
491 const uint8_t *const hlen1 = ht[1].hlen;
495 unsigned int const x0 = *ix++;
496 unsigned int const x1 = *ix++;
497 sum1 += hlen1[ x0+x0 + x1 ];
505 static const int huf_tbl_noESC[] = {
506 1, 2, 5, 7, 7, 10, 10, 13, 13, 13, 13, 13, 13, 13, 13
511 count_bit_noESC_from2(const int *ix, const int *end, int max, unsigned int *s)
513 int t1 = huf_tbl_noESC[max - 1];
515 const unsigned int xlen = ht[t1].xlen;
516 uint32_t const* table = (t1 == 2) ? &table23[0] : &table56[0];
517 unsigned int sum = 0, sum2;
520 unsigned int const x0 = *ix++;
521 unsigned int const x1 = *ix++;
522 sum += table[ x0 * xlen + x1 ];
525 sum2 = sum & 0xffffu;
539 count_bit_noESC_from3(const int *ix, const int *end, int max, unsigned int * s)
541 int t1 = huf_tbl_noESC[max - 1];
543 unsigned int sum1 = 0;
544 unsigned int sum2 = 0;
545 unsigned int sum3 = 0;
546 const unsigned int xlen = ht[t1].xlen;
547 const uint8_t *const hlen1 = ht[t1].hlen;
548 const uint8_t *const hlen2 = ht[t1 + 1].hlen;
549 const uint8_t *const hlen3 = ht[t1 + 2].hlen;
553 unsigned int x0 = *ix++;
554 unsigned int x1 = *ix++;
555 unsigned int x = x0 * xlen + x1;
576 /*************************************************************************/
578 /*************************************************************************/
581 Choose the Huffman table that will encode ix[begin..end] with
584 Note: This code contains knowledge about the sizes and characteristics
585 of the Huffman tables as defined in the IS (Table B.7), and will not work
586 with any arbitrary tables.
588 static int count_bit_null(const int* ix, const int* end, int max, unsigned int* s)
597 typedef int (*count_fnc)(const int* ix, const int* end, int max, unsigned int* s);
599 static count_fnc count_fncs[] =
602 , &count_bit_noESC_from2
603 , &count_bit_noESC_from2
604 , &count_bit_noESC_from3
605 , &count_bit_noESC_from3
606 , &count_bit_noESC_from3
607 , &count_bit_noESC_from3
608 , &count_bit_noESC_from3
609 , &count_bit_noESC_from3
610 , &count_bit_noESC_from3
611 , &count_bit_noESC_from3
612 , &count_bit_noESC_from3
613 , &count_bit_noESC_from3
614 , &count_bit_noESC_from3
615 , &count_bit_noESC_from3
619 choose_table_nonMMX(const int *ix, const int *const end, int *const _s)
621 unsigned int* s = (unsigned int*)_s;
624 max = ix_max(ix, end);
627 return count_fncs[max](ix, end, max, s);
629 /* try tables with linbits */
630 if (max > IXMAX_VAL) {
635 for (choice2 = 24; choice2 < 32; choice2++) {
636 if (ht[choice2].linmax >= max) {
641 for (choice = choice2 - 8; choice < 24; choice++) {
642 if (ht[choice].linmax >= max) {
646 return count_bit_ESC(ix, end, choice, choice2, s);
651 /*************************************************************************/
653 /*************************************************************************/
655 noquant_count_bits(lame_internal_flags const *const gfc,
656 gr_info * const gi, calc_noise_data * prev_noise)
658 SessionConfig_t const *const cfg = &gfc->cfg;
661 int const *const ix = gi->l3_enc;
663 i = Min(576, ((gi->max_nonzero_coeff + 2) >> 1) << 1);
666 prev_noise->sfb_count1 = 0;
668 /* Determine count1 region */
669 for (; i > 1; i -= 2)
670 if (ix[i - 1] | ix[i - 2])
674 /* Determines the number of bits to encode the quadruples. */
676 for (; i > 3; i -= 4) {
682 /* hack to check if all values <= 1 */
683 if ((unsigned int) (x4 | x3 | x2 | x1) > 1)
686 p = ((x4 * 2 + x3) * 2 + x2) * 2 + x1;
692 gi->count1table_select = 0;
695 gi->count1table_select = 1;
698 gi->count1bits = bits;
703 if (gi->block_type == SHORT_TYPE) {
704 a1 = 3 * gfc->scalefac_band.s[3];
705 if (a1 > gi->big_values)
710 else if (gi->block_type == NORM_TYPE) {
711 assert(i <= 576); /* bv_scf has 576 entries (0..575) */
712 a1 = gi->region0_count = gfc->sv_qnt.bv_scf[i - 2];
713 a2 = gi->region1_count = gfc->sv_qnt.bv_scf[i - 1];
715 assert(a1 + a2 + 2 < SBPSY_l);
716 a2 = gfc->scalefac_band.l[a1 + a2 + 2];
717 a1 = gfc->scalefac_band.l[a1 + 1];
719 gi->table_select[2] = gfc->choose_table(ix + a2, ix + i, &bits);
723 gi->region0_count = 7;
724 /*gi->region1_count = SBPSY_l - 7 - 1; */
725 gi->region1_count = SBMAX_l - 1 - 7 - 1;
726 a1 = gfc->scalefac_band.l[7 + 1];
734 /* have to allow for the case when bigvalues < region0 < region1 */
735 /* (and region0, region1 are ignored) */
742 /* Count the number of bits necessary to code the bigvalues region. */
744 gi->table_select[0] = gfc->choose_table(ix, ix + a1, &bits);
746 gi->table_select[1] = gfc->choose_table(ix + a1, ix + a2, &bits);
747 if (cfg->use_best_huffman == 2) {
748 gi->part2_3_length = bits;
749 best_huffman_divide(gfc, gi);
750 bits = gi->part2_3_length;
755 if (gi->block_type == NORM_TYPE) {
757 while (gfc->scalefac_band.l[sfb] < gi->big_values) {
760 prev_noise->sfb_count1 = sfb;
768 count_bits(lame_internal_flags const *const gfc,
769 const FLOAT * const xr, gr_info * const gi, calc_noise_data * prev_noise)
771 int *const ix = gi->l3_enc;
773 /* since quantize_xrpow uses table lookup, we need to check this first: */
774 FLOAT const w = (IXMAX_VAL) / IPOW20(gi->global_gain);
776 if (gi->xrpow_max > w)
779 quantize_xrpow(xr, ix, IPOW20(gi->global_gain), gi, prev_noise);
781 if (gfc->sv_qnt.substep_shaping & 2) {
783 /* 0.634521682242439 = 0.5946*2**(.5*0.1875) */
784 int const gain = gi->global_gain + gi->scalefac_scale;
785 const FLOAT roundfac = 0.634521682242439 / IPOW20(gain);
786 for (sfb = 0; sfb < gi->sfbmax; sfb++) {
787 int const width = gi->width[sfb];
789 if (!gfc->sv_qnt.pseudohalf[sfb]) {
794 for (k = j, j += width; k < j; ++k) {
795 ix[k] = (xr[k] >= roundfac) ? ix[k] : 0;
800 return noquant_count_bits(gfc, gi, prev_noise);
803 /***********************************************************************
804 re-calculate the best scalefac_compress using scfsi
805 the saved bits are kept in the bit reservoir.
806 **********************************************************************/
810 recalc_divide_init(const lame_internal_flags * const gfc,
811 gr_info const *cod_info,
812 int const *const ix, int r01_bits[], int r01_div[], int r0_tbl[], int r1_tbl[])
814 int r0, r1, bigv, r0t, r1t, bits;
816 bigv = cod_info->big_values;
818 for (r0 = 0; r0 <= 7 + 15; r0++) {
819 r01_bits[r0] = LARGE_BITS;
822 for (r0 = 0; r0 < 16; r0++) {
823 int const a1 = gfc->scalefac_band.l[r0 + 1];
828 r0t = gfc->choose_table(ix, ix + a1, &r0bits);
830 for (r1 = 0; r1 < 8; r1++) {
831 int const a2 = gfc->scalefac_band.l[r0 + r1 + 2];
836 r1t = gfc->choose_table(ix + a1, ix + a2, &bits);
837 if (r01_bits[r0 + r1] > bits) {
838 r01_bits[r0 + r1] = bits;
839 r01_div[r0 + r1] = r0;
840 r0_tbl[r0 + r1] = r0t;
841 r1_tbl[r0 + r1] = r1t;
848 recalc_divide_sub(const lame_internal_flags * const gfc,
849 const gr_info * cod_info2,
852 const int r01_bits[], const int r01_div[], const int r0_tbl[], const int r1_tbl[])
854 int bits, r2, a2, bigv, r2t;
856 bigv = cod_info2->big_values;
858 for (r2 = 2; r2 < SBMAX_l + 1; r2++) {
859 a2 = gfc->scalefac_band.l[r2];
863 bits = r01_bits[r2 - 2] + cod_info2->count1bits;
864 if (gi->part2_3_length <= bits)
867 r2t = gfc->choose_table(ix + a2, ix + bigv, &bits);
868 if (gi->part2_3_length <= bits)
871 memcpy(gi, cod_info2, sizeof(gr_info));
872 gi->part2_3_length = bits;
873 gi->region0_count = r01_div[r2 - 2];
874 gi->region1_count = r2 - 2 - r01_div[r2 - 2];
875 gi->table_select[0] = r0_tbl[r2 - 2];
876 gi->table_select[1] = r1_tbl[r2 - 2];
877 gi->table_select[2] = r2t;
885 best_huffman_divide(const lame_internal_flags * const gfc, gr_info * const gi)
887 SessionConfig_t const *const cfg = &gfc->cfg;
890 int const *const ix = gi->l3_enc;
892 int r01_bits[7 + 15 + 1];
893 int r01_div[7 + 15 + 1];
894 int r0_tbl[7 + 15 + 1];
895 int r1_tbl[7 + 15 + 1];
898 /* SHORT BLOCK stuff fails for MPEG2 */
899 if (gi->block_type == SHORT_TYPE && cfg->mode_gr == 1)
903 memcpy(&cod_info2, gi, sizeof(gr_info));
904 if (gi->block_type == NORM_TYPE) {
905 recalc_divide_init(gfc, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl);
906 recalc_divide_sub(gfc, &cod_info2, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl);
909 i = cod_info2.big_values;
910 if (i == 0 || (unsigned int) (ix[i - 2] | ix[i - 1]) > 1)
917 /* Determines the number of bits to encode the quadruples. */
918 memcpy(&cod_info2, gi, sizeof(gr_info));
919 cod_info2.count1 = i;
924 for (; i > cod_info2.big_values; i -= 4) {
925 int const p = ((ix[i - 4] * 2 + ix[i - 3]) * 2 + ix[i - 2]) * 2 + ix[i - 1];
929 cod_info2.big_values = i;
931 cod_info2.count1table_select = 0;
934 cod_info2.count1table_select = 1;
937 cod_info2.count1bits = a1;
939 if (cod_info2.block_type == NORM_TYPE)
940 recalc_divide_sub(gfc, &cod_info2, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl);
942 /* Count the number of bits necessary to code the bigvalues region. */
943 cod_info2.part2_3_length = a1;
944 a1 = gfc->scalefac_band.l[7 + 1];
949 cod_info2.table_select[0] =
950 gfc->choose_table(ix, ix + a1, (int *) &cod_info2.part2_3_length);
952 cod_info2.table_select[1] =
953 gfc->choose_table(ix + a1, ix + i, (int *) &cod_info2.part2_3_length);
954 if (gi->part2_3_length > cod_info2.part2_3_length)
955 memcpy(gi, &cod_info2, sizeof(gr_info));
959 static const int slen1_n[16] = { 1, 1, 1, 1, 8, 2, 2, 2, 4, 4, 4, 8, 8, 8, 16, 16 };
960 static const int slen2_n[16] = { 1, 2, 4, 8, 1, 2, 4, 8, 2, 4, 8, 2, 4, 8, 4, 8 };
961 const int slen1_tab[16] = { 0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 };
962 const int slen2_tab[16] = { 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3 };
965 scfsi_calc(int ch, III_side_info_t * l3_side)
970 gr_info *const gi = &l3_side->tt[1][ch];
971 gr_info const *const g0 = &l3_side->tt[0][ch];
973 for (i = 0; i < (sizeof(scfsi_band) / sizeof(int)) - 1; i++) {
974 for (sfb = scfsi_band[i]; sfb < scfsi_band[i + 1]; sfb++) {
975 if (g0->scalefac[sfb] != gi->scalefac[sfb]
976 && gi->scalefac[sfb] >= 0)
979 if (sfb == scfsi_band[i + 1]) {
980 for (sfb = scfsi_band[i]; sfb < scfsi_band[i + 1]; sfb++) {
981 gi->scalefac[sfb] = -1;
983 l3_side->scfsi[ch][i] = 1;
988 for (sfb = 0; sfb < 11; sfb++) {
989 if (gi->scalefac[sfb] == -1)
992 if (s1 < gi->scalefac[sfb])
993 s1 = gi->scalefac[sfb];
997 for (; sfb < SBPSY_l; sfb++) {
998 if (gi->scalefac[sfb] == -1)
1001 if (s2 < gi->scalefac[sfb])
1002 s2 = gi->scalefac[sfb];
1005 for (i = 0; i < 16; i++) {
1006 if (s1 < slen1_n[i] && s2 < slen2_n[i]) {
1007 int const c = slen1_tab[i] * c1 + slen2_tab[i] * c2;
1008 if (gi->part2_length > c) {
1009 gi->part2_length = c;
1010 gi->scalefac_compress = (int)i;
1017 Find the optimal way to store the scalefactors.
1018 Only call this routine after final scalefactors have been
1019 chosen and the channel/granule will not be re-encoded.
1022 best_scalefac_store(const lame_internal_flags * gfc,
1023 const int gr, const int ch, III_side_info_t * const l3_side)
1025 SessionConfig_t const *const cfg = &gfc->cfg;
1026 /* use scalefac_scale if we can */
1027 gr_info *const gi = &l3_side->tt[gr][ch];
1031 /* remove scalefacs from bands with ix=0. This idea comes
1032 * from the AAC ISO docs. added mt 3/00 */
1033 /* check if l3_enc=0 */
1035 for (sfb = 0; sfb < gi->sfbmax; sfb++) {
1036 int const width = gi->width[sfb];
1038 for (l = j, j += width; l < j; ++l) {
1039 if (gi->l3_enc[l] != 0)
1043 gi->scalefac[sfb] = recalc = -2; /* anything goes. */
1044 /* only best_scalefac_store and calc_scfsi
1045 * know--and only they should know--about the magic number -2.
1049 if (!gi->scalefac_scale && !gi->preflag) {
1051 for (sfb = 0; sfb < gi->sfbmax; sfb++)
1052 if (gi->scalefac[sfb] > 0)
1053 s |= gi->scalefac[sfb];
1055 if (!(s & 1) && s != 0) {
1056 for (sfb = 0; sfb < gi->sfbmax; sfb++)
1057 if (gi->scalefac[sfb] > 0)
1058 gi->scalefac[sfb] >>= 1;
1060 gi->scalefac_scale = recalc = 1;
1064 if (!gi->preflag && gi->block_type != SHORT_TYPE && cfg->mode_gr == 2) {
1065 for (sfb = 11; sfb < SBPSY_l; sfb++)
1066 if (gi->scalefac[sfb] < pretab[sfb] && gi->scalefac[sfb] != -2)
1068 if (sfb == SBPSY_l) {
1069 for (sfb = 11; sfb < SBPSY_l; sfb++)
1070 if (gi->scalefac[sfb] > 0)
1071 gi->scalefac[sfb] -= pretab[sfb];
1073 gi->preflag = recalc = 1;
1077 for (i = 0; i < 4; i++)
1078 l3_side->scfsi[ch][i] = 0;
1080 if (cfg->mode_gr == 2 && gr == 1
1081 && l3_side->tt[0][ch].block_type != SHORT_TYPE
1082 && l3_side->tt[1][ch].block_type != SHORT_TYPE) {
1083 scfsi_calc(ch, l3_side);
1086 for (sfb = 0; sfb < gi->sfbmax; sfb++) {
1087 if (gi->scalefac[sfb] == -2) {
1088 gi->scalefac[sfb] = 0; /* if anything goes, then 0 is a good choice */
1092 (void) scale_bitcount(gfc, gi);
1099 all_scalefactors_not_negative(int const *scalefac, int n)
1102 for (i = 0; i < n; ++i) {
1103 if (scalefac[i] < 0)
1111 /* number of bits used to encode scalefacs */
1113 /* 18*slen1_tab[i] + 18*slen2_tab[i] */
1114 static const int scale_short[16] = {
1115 0, 18, 36, 54, 54, 36, 54, 72, 54, 72, 90, 72, 90, 108, 108, 126
1118 /* 17*slen1_tab[i] + 18*slen2_tab[i] */
1119 static const int scale_mixed[16] = {
1120 0, 18, 36, 54, 51, 35, 53, 71, 52, 70, 88, 69, 87, 105, 104, 122
1123 /* 11*slen1_tab[i] + 10*slen2_tab[i] */
1124 static const int scale_long[16] = {
1125 0, 10, 20, 30, 33, 21, 31, 41, 32, 42, 52, 43, 53, 63, 64, 74
1129 /*************************************************************************/
1130 /* scale_bitcount */
1131 /*************************************************************************/
1133 /* Also calculates the number of bits necessary to code the scalefactors. */
1136 mpeg1_scale_bitcount(const lame_internal_flags * gfc, gr_info * const cod_info)
1138 int k, sfb, max_slen1 = 0, max_slen2 = 0;
1140 /* maximum values */
1142 int *const scalefac = cod_info->scalefac;
1145 assert(all_scalefactors_not_negative(scalefac, cod_info->sfbmax));
1147 if (cod_info->block_type == SHORT_TYPE) {
1149 if (cod_info->mixed_block_flag)
1152 else { /* block_type == 1,2,or 3 */
1154 if (!cod_info->preflag) {
1155 for (sfb = 11; sfb < SBPSY_l; sfb++)
1156 if (scalefac[sfb] < pretab[sfb])
1159 if (sfb == SBPSY_l) {
1160 cod_info->preflag = 1;
1161 for (sfb = 11; sfb < SBPSY_l; sfb++)
1162 scalefac[sfb] -= pretab[sfb];
1167 for (sfb = 0; sfb < cod_info->sfbdivide; sfb++)
1168 if (max_slen1 < scalefac[sfb])
1169 max_slen1 = scalefac[sfb];
1171 for (; sfb < cod_info->sfbmax; sfb++)
1172 if (max_slen2 < scalefac[sfb])
1173 max_slen2 = scalefac[sfb];
1175 /* from Takehiro TOMINAGA <tominaga@isoternet.org> 10/99
1176 * loop over *all* posible values of scalefac_compress to find the
1177 * one which uses the smallest number of bits. ISO would stop
1178 * at first valid index */
1179 cod_info->part2_length = LARGE_BITS;
1180 for (k = 0; k < 16; k++) {
1181 if (max_slen1 < slen1_n[k] && max_slen2 < slen2_n[k]
1182 && cod_info->part2_length > tab[k]) {
1183 cod_info->part2_length = tab[k];
1184 cod_info->scalefac_compress = k;
1187 return cod_info->part2_length == LARGE_BITS;
1193 table of largest scalefactor values for MPEG2
1195 static const int max_range_sfac_tab[6][4] = {
1207 /*************************************************************************/
1208 /* scale_bitcount_lsf */
1209 /*************************************************************************/
1211 /* Also counts the number of bits to encode the scalefacs but for MPEG 2 */
1212 /* Lower sampling frequencies (24, 22.05 and 16 kHz.) */
1214 /* This is reverse-engineered from section 2.4.3.2 of the MPEG2 IS, */
1215 /* "Audio Decoding Layer III" */
1218 mpeg2_scale_bitcount(const lame_internal_flags * gfc, gr_info * const cod_info)
1220 int table_number, row_in_table, partition, nr_sfb, window, over;
1221 int i, sfb, max_sfac[4];
1222 const int *partition_table;
1223 int const *const scalefac = cod_info->scalefac;
1226 Set partition table. Note that should try to use table one,
1229 if (cod_info->preflag)
1234 for (i = 0; i < 4; i++)
1237 if (cod_info->block_type == SHORT_TYPE) {
1239 partition_table = &nr_of_sfb_block[table_number][row_in_table][0];
1240 for (sfb = 0, partition = 0; partition < 4; partition++) {
1241 nr_sfb = partition_table[partition] / 3;
1242 for (i = 0; i < nr_sfb; i++, sfb++)
1243 for (window = 0; window < 3; window++)
1244 if (scalefac[sfb * 3 + window] > max_sfac[partition])
1245 max_sfac[partition] = scalefac[sfb * 3 + window];
1250 partition_table = &nr_of_sfb_block[table_number][row_in_table][0];
1251 for (sfb = 0, partition = 0; partition < 4; partition++) {
1252 nr_sfb = partition_table[partition];
1253 for (i = 0; i < nr_sfb; i++, sfb++)
1254 if (scalefac[sfb] > max_sfac[partition])
1255 max_sfac[partition] = scalefac[sfb];
1259 for (over = 0, partition = 0; partition < 4; partition++) {
1260 if (max_sfac[partition] > max_range_sfac_tab[table_number][partition])
1265 Since no bands have been over-amplified, we can set scalefac_compress
1266 and slen[] for the formatter
1268 static const int log2tab[] = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 };
1270 int slen1, slen2, slen3, slen4;
1272 cod_info->sfb_partition_table = nr_of_sfb_block[table_number][row_in_table];
1273 for (partition = 0; partition < 4; partition++)
1274 cod_info->slen[partition] = log2tab[max_sfac[partition]];
1276 /* set scalefac_compress */
1277 slen1 = cod_info->slen[0];
1278 slen2 = cod_info->slen[1];
1279 slen3 = cod_info->slen[2];
1280 slen4 = cod_info->slen[3];
1282 switch (table_number) {
1284 cod_info->scalefac_compress = (((slen1 * 5) + slen2) << 4)
1290 cod_info->scalefac_compress = 400 + (((slen1 * 5) + slen2) << 2)
1295 cod_info->scalefac_compress = 500 + (slen1 * 3) + slen2;
1299 ERRORF(gfc, "intensity stereo not implemented yet\n");
1305 ERRORF(gfc, "---WARNING !! Amplification of some bands over limits\n");
1308 assert(cod_info->sfb_partition_table);
1309 cod_info->part2_length = 0;
1310 for (partition = 0; partition < 4; partition++)
1311 cod_info->part2_length +=
1312 cod_info->slen[partition] * cod_info->sfb_partition_table[partition];
1319 scale_bitcount(const lame_internal_flags * gfc, gr_info * cod_info)
1321 if (gfc->cfg.mode_gr == 2) {
1322 return mpeg1_scale_bitcount(gfc, cod_info);
1325 return mpeg2_scale_bitcount(gfc, cod_info);
1330 #ifdef MMX_choose_table
1331 extern int choose_table_MMX(const int *ix, const int *const end, int *const s);
1335 huffman_init(lame_internal_flags * const gfc)
1339 gfc->choose_table = choose_table_nonMMX;
1341 #ifdef MMX_choose_table
1342 if (gfc->CPU_features.MMX) {
1343 gfc->choose_table = choose_table_MMX;
1347 for (i = 2; i <= 576; i += 2) {
1348 int scfb_anz = 0, bv_index;
1349 while (gfc->scalefac_band.l[++scfb_anz] < i);
1351 bv_index = subdv_table[scfb_anz].region0_count;
1352 while (gfc->scalefac_band.l[bv_index + 1] > i)
1356 /* this is an indication that everything is going to
1357 be encoded as region0: bigvalues < region0 < region1
1358 so lets set region0, region1 to some value larger
1360 bv_index = subdv_table[scfb_anz].region0_count;
1363 gfc->sv_qnt.bv_scf[i - 2] = bv_index;
1365 bv_index = subdv_table[scfb_anz].region1_count;
1366 while (gfc->scalefac_band.l[bv_index + gfc->sv_qnt.bv_scf[i - 2] + 2] > i)
1370 bv_index = subdv_table[scfb_anz].region1_count;
1373 gfc->sv_qnt.bv_scf[i - 1] = bv_index;