2 (c) Copyright 1998-2001 - Tord Jansson
3 ======================================
5 This file is part of the BladeEnc MP3 Encoder, based on
6 ISO's reference code for MPEG Layer 3 compression, and might
7 contain smaller or larger sections that are directly taken
8 from ISO's reference code.
10 All changes to the ISO reference code herein are either
11 copyrighted by Tord Jansson (tord.jansson@swipnet.se)
12 or sublicensed to Tord Jansson by a third party.
14 BladeEnc is free software; you can redistribute this file
15 and/or modify it under the terms of the GNU Lesser General Public
16 License as published by the Free Software Foundation; either
17 version 2.1 of the License, or (at your option) any later version.
21 ------------ Changes ------------
23 2000-11-10 Andre Piotrowski
25 - reformatted, used 'static' functions, global variables, less parameters
29 - a lot of functions redesigned to let the bladetable become superfluous:
30 genNoisePowTab(), iteration_loop(), calc_noise(), preemphasis(), amp_scalefac_bands()
31 - bladTabValue() replaced by cutting_crew()
35 - bug fix: module - reset fInit_huffman_read_flag
38 - speed up : implemented partial quantizing
41 - speed up : implemented faster Huffman coding
42 - integration : improved (optional) Huffman coding
43 [choosing better tables]
44 - integration : improved (optional) binary search
45 [in fact, this is a BUG FIX (original dist10 bug)]
48 - bug fix : original dist10's outer_loop could cause an endless loop (huff_bits <= 0).
49 - speed up : faster part2_length/scale_bitcount calculation
50 - integration : improved (optional) scalefactor compression
51 [smart preflag switching and choosing best compression]
54 - integration : improved (optional) quantanf algorithm
55 [calculating exact quantizer step boundaries]
56 - integration : improved (optional) preemphasing/amplifying algorithm
57 [preemphase only if iteration==1, according to ISO]
58 - integration : improved (optional) outer_loop algorithm
59 [amplify the bands, marked as "should be amplified"]
62 - definitely killed SCFSI
66 - use some explicit type casting to avoid compiler warnings
67 - clear some backward compatability flags for 0.93.10
70 - implemented flag CHECK_TJ_OVERFLOW to check for huffman table overflow
77 /* ======================================================================================== */
78 /* keeping backward compatability */
79 /* ======================================================================================== */
81 #define ORG_HUFFMAN_CODING 0 /* 0 = use better Huffman tables for shorter code */
82 #define ORG_BINARY_SEARCH 0 /* 0 = use a correct implemented binary search */
83 #define ORG_QUANTANF_INIT 0 /* 0 = use better quantization start value */
84 #define ORG_PREEMPHASING 0 /* 0 = use a more ISO-like preemphasing algorithm */
85 #define ORG_SCF_COMPRESS 0 /* 0 = choose better scalefactor compression tables and smart preemphasing */
86 #define ORG_OUTER_LOOP 0 /* 0 = differ between marked as "been amplified" and "should be amplified" */
87 #define ORG_HIGHEST_SFB 1 /* 0 = cut off highest frequencies (in last scale factor band) */
89 #define CHECK_TJ_OVERFLOW 1 /* 1 = check for huffman table overflow */
95 #define infinity 99999999
112 #include "l3bitstream.h"
113 #include "reservoir.h"
115 #include "loop-pvt.h"
121 #if ORG_HUFFMAN_CODING
122 static void tiny_single_Huffman_2 /* Escape tables */
126 unsigned table0, /* 15... */
134 static int amplify_short (void);
135 static int amplify_long
144 int my_nint (double in)
148 return (int)(in - 0.5);
150 return (int)(in + 0.5);
158 Here are MPEG1 Table B.8 and MPEG2 Table B.1
159 -- Layer III scalefactor bands.
160 Index into this using a method such as:
161 idx = fr_ps->header->sampling_frequency
162 + (fr_ps->header->version * 3)
165 struct scalefac_struct sfBandIndex[3] =
167 { /* Table B.8.b: 44.1 kHz */
168 {0,4,8,12,16,20,24,30,36,44,52,62,74,90,110,134,162,196,238,288,342,418,576},
169 {0,4,8,12,16,22,30,40,52,66,84,106,136,192}
171 { /* Table B.8.c: 48 kHz */
172 {0,4,8,12,16,20,24,30,36,42,50,60,72,88,106,128,156,190,230,276,330,384,576},
173 {0,4,8,12,16,22,28,38,50,64,80,100,126,192}
175 { /* Table B.8.a: 32 kHz */
176 {0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576},
177 {0,4,8,12,16,22,30,42,58,78,104,138,180,192}
184 The following table is used to implement the scalefactor
185 partitioning for MPEG2 as described in section
186 2.4.3.2 of the IS. The indexing corresponds to the
187 way the tables are presented in the IS:
189 [table_number][row_in_table][column of nr_of_sfb]
192 static unsigned nr_of_sfb_block[6][3][4] =
228 /* Table B.6: layer3 preemphasis */
238 /* This is the scfsi_band table from 2.4.2.7 of the IS */
239 int scfsi_band_long[5] = { 0, 6, 11, 16, 21 };
243 int *scalefac_band_long;
244 int *scalefac_band_short;
246 int fInit_iteration_loop;
248 #if ORG_BINARY_SEARCH || ORG_QUANTANF_INIT || CHECK_TJ_OVERFLOW
253 /* We need this initialization here since some compilers chokes on the
254 the declaration if it constains an initialization that point directly
258 void fixStatic_loop( void )
260 scalefac_band_long = &sfBandIndex[0].l[0];
261 scalefac_band_short = &sfBandIndex[0].s[0];
268 /* ======================================================================== */
269 /* generating the power tables */
270 /* ======================================================================== */
275 #define POW216_MAX (BOT/2 * 16)
276 #define POW216_MIN (-25 * 16)
280 static double pow216_space[POW216_MAX-POW216_MIN+1];
281 static double *pow216 = pow216_space - POW216_MIN;
282 static double noisePowTab[8191+15];
286 void genNoisePowTab (void)
290 for (i = POW216_MIN; i <= POW216_MAX; i++)
291 pow216[i] = pow (2.0, (double)i/16.0);
293 for (i = 0; i < 8191+15; i++)
294 noisePowTab[i] = pow (i, 4.0/3.0);
301 /* ======================================================================== */
302 /* static variables */
303 /* ======================================================================== */
307 static int gr; /* the current granule */
308 static int ch; /* the current channel */
312 static III_side_info_t *side_info; /* the current side information */
313 static gr_info *cod_info; /* the current coding information */
317 static double *xr_org_l; /* the initial magnitudes of the spectral values */
318 static double xr34_l[576]; /* the magnitudes powered by 3/4 */
319 static int *ix_l; /* quantized values */
321 static double energy_l[SFB_LMAX];
322 static double xmin_l[SFB_LMAX]; /* the allowed distortion of the scalefactor band */
323 static double xfsf_l[SFB_LMAX]; /* the current distortion of the scalefactor band */
324 static int expo16_l[SFB_LMAX]; /* sixteen times the scale factor band exponent */
325 static int *scalefac_l; /* the current scale factors */
326 static int *scalefac_0_l; /* scale factors for first granule */
328 static double (*xr_org_s)[3]; /* some short block versions */
329 static double (*xr34_s)[3] = (double (*)[3]) xr34_l;
330 static int (*ix_s)[3];
332 static double energy_s[SFB_SMAX][3];
333 static double xmin_s[SFB_SMAX][3];
334 static double xfsf_s[SFB_SMAX][3];
335 static int expo16_s[SFB_SMAX][3];
336 static int (*scalefac_s)[3];
340 static int max_used_sfb_l;
341 static int min_used_sfb_s;
343 static int end_sfb_l;
344 static int end_sfb_s;
348 static double xmax_l[SFB_LMAX]; /* The initial (absolute) maximum magnitude */
349 static int xmax_line_l[SFB_LMAX]; /* of the long bands and their line indices */
351 static double xmax_s[SFB_SMAX][3]; /* Guess ... */
352 static int xmax_line_s[SFB_SMAX][3];
356 static int mark_idx_l; /* speed up - partial quantizing */
357 static int mark_tab_l[SFB_LMAX]; /* changed sfb-s */
359 static int mark_idx_s;
360 static int mark_tab_s[SFB_SMAX*3*2]; /* changed (sfb,b)-s */
364 #if !ORG_QUANTANF_INIT
366 static int lo_quant_l [SFB_LMAX];
367 static int hi_quant_l [SBMAX_l];
369 static int lo_quant_s [SFB_SMAX][3];
370 static int hi_quant_s [SFB_SMAX][3];
372 static int the_lo_quant;
373 static int the_hi_quant;
375 static double log_2, cc, dd;
383 /* ======================================================================== */
385 /* ======================================================================== */
390 double xr_org[2][2][576],
391 III_psy_ratio *ratio,
392 III_side_info_t *l3_side,
393 int l3_enc[2][2][576],
396 double xr_dec[2][2][576],
397 III_scalefac_t *scalefac,
404 int i, sfb, b, scfsi_band;
407 int *main_data_begin;
412 #if ORG_QUANTANF_INIT
415 double total_energy, temp, x;
421 main_data_begin = &side_info->main_data_begin;
422 info = fr_ps->header;
425 side_info->resvDrain = 0;
427 if (!fInit_iteration_loop)
429 *main_data_begin = 0;
430 fInit_iteration_loop = 1;
432 #if !ORG_QUANTANF_INIT
434 cc = 4.0/3.0 * log(8205.0 - 0.5 + 0.0946) / log_2;
435 dd = 4.0/3.0 * log( 1.0 - 0.5 + 0.0946) / log_2;
440 scalefac_band_long = &sfBandIndex[info->sampling_frequency].l[0];
441 scalefac_band_short = &sfBandIndex[info->sampling_frequency].s[0];
444 ResvFrameBegin (fr_ps, side_info, mean_bits, bitsPerFrame);
446 for (gr = 0; gr < mode_gr; gr++)
448 for (ch = 0; ch < stereo; ch++)
450 xr_org_l = xr_org[gr][ch];
451 xr_org_s = (double (*)[3]) xr_org_l;
453 ix_l = l3_enc[gr][ch];
454 ix_s = (int (*)[3]) ix_l;
456 cod_info = &side_info->gr[gr].ch[ch].tt;
458 scalefac_l = scalefac->l[gr][ch]; scalefac_0_l = scalefac->l[0][ch];
459 scalefac_s = scalefac->s[gr][ch];
463 /* reset of iteration variables */
465 for (scfsi_band = 0; scfsi_band < 4; scfsi_band++)
466 cod_info->slen[scfsi_band] = 0;
468 cod_info->sfb_partition_table = &nr_of_sfb_block[0][0][0];
469 cod_info->part2_3_length = 0;
470 cod_info->big_values = 0;
471 cod_info->count1 = 0;
472 cod_info->scalefac_compress = 0;
473 cod_info->table_select[0] = 0;
474 cod_info->table_select[1] = 0;
475 cod_info->table_select[2] = 0;
476 cod_info->subblock_gain[0] = 0;
477 cod_info->subblock_gain[1] = 0;
478 cod_info->subblock_gain[2] = 0;
479 cod_info->region0_count = 0;
480 cod_info->region1_count = 0;
481 cod_info->part2_length = 0;
482 cod_info->preflag = 0;
483 cod_info->scalefac_scale = 0;
484 cod_info->quantizerStepSize = 0.0;
485 cod_info->count1table_select = 0;
489 /* ======== gr_deco ======== */
491 if (cod_info->window_switching_flag && (cod_info->block_type == SHORT_TYPE))
493 if (cod_info->mixed_block_flag)
496 In mixed blocks there come first 8 long scale factor band areas covering
497 the place normally used by the first 3 short scale factor band areas.
499 max_used_sfb_l = cod_info->sfb_lmax = 8;
500 min_used_sfb_s = cod_info->sfb_smax = 3;
502 /* The following values don«t need to be set again and again ... */
503 cod_info->region0_count = 7; /* scalefac_band_long[7+1 ] = 36 */
504 cod_info->region1_count = 13; /* scalefac_band_long[7+13+2] = 576 (no region2) */
508 max_used_sfb_l = cod_info->sfb_lmax = 0; /* No long blocks */
509 min_used_sfb_s = cod_info->sfb_smax = 0;
511 /* The following values don«t need to be set again and again ... */
512 cod_info->region0_count = 8; /* scalefac_band_short[(8+1 )/3] = 12 ( 12*3 = 36) */
513 cod_info->region1_count = 36; /* 36? should be 29: scalefac_band_short[(8+29+2)/3] = 192 (192*3 = 576) */
514 /* probably meant : scalefac_band_short[36/3 + 1 ] = 192 (192*3 = 576) */
515 /* 2000-02-27 AP no effect on output because block_type != NORM_TYPE */
518 /* to access the entire array we need the last scalefac_band_short area */
519 end_sfb_l = max_used_sfb_l; /*cod_info->sfb_lmax;*/
520 end_sfb_s = SFB_SMAX;
522 /* The following values don«t need to be set again and again ... */
523 cod_info->count1 = 0; /* (zero_region-bigv_region) / 4; */
524 cod_info->big_values = 288; /* bigv_region / 2; */
526 cod_info->count1table_select = 1; /* sum0 == sum1 == 0 */
528 cod_info->address1 = 36; /* choose one of the region0_count formulas above */
529 cod_info->address2 = 576; /* bigv_region; */
530 cod_info->address3 = 0;
534 max_used_sfb_l = cod_info->sfb_lmax = SBMAX_l;
535 min_used_sfb_s = cod_info->sfb_smax = SBMAX_s; /* No short blocks */
537 /* to access the entire array we need the last scalefac_band_long area */
538 end_sfb_l = SFB_LMAX;
539 end_sfb_s = min_used_sfb_s; /*cod_info->sfb_smax;*/
543 /* reset of iteration variables */
545 for (sfb = 0; sfb < max_used_sfb_l/*SFB_LMAX-1*/; sfb++)
547 for (sfb = min_used_sfb_s/*0*/; sfb < SFB_SMAX-1; sfb++)
548 for (b = 0; b < 3; b++)
549 scalefac_s[sfb][b] = 0;
551 /* ======== calc_xmin and start of quantanf_init ======== */
553 Calculate the allowed distortion for each scalefactor band,
554 as determined by the psychoacoustic model.
555 xmin(sb) = ratio(sb) * energy(sb) / bandwidth(sb)
558 #if ORG_QUANTANF_INIT
563 for (sfb = 0; sfb < end_sfb_l; sfb++)
565 start = scalefac_band_long[sfb];
566 end = scalefac_band_long[sfb+1];
571 xmax_line_l[sfb] = start;
575 if (sfb < max_used_sfb_l)
578 for (i = start; i < end; i++)
580 if ((x = fabs(xr_org_l[i])) != 0.0)
582 xr34_l[i] = sqrt(x * sqrt(x));
584 # if ORG_QUANTANF_INIT
590 xmax_line_l[sfb] = i;
598 else /* cut off the (highest frequency) entries in the unused scale factor band */
600 for (i = start; i < end; i++)
604 total_energy += energy_l[sfb] = temp;
606 if (sfb < max_used_sfb_l)
607 xmin_l[sfb] = ratio->l[gr][ch][sfb] * temp;
610 for (sfb = min_used_sfb_s; sfb < end_sfb_s; sfb++)
612 start = scalefac_band_short[sfb];
613 end = scalefac_band_short[sfb+1];
615 for (b = 0; b < 3; b++)
617 expo16_s[sfb][b] = 0;
619 xmax_s[sfb][b] = 0.0;
620 xmax_line_s[sfb][b] = start;
627 for (i = start; i < end; i++)
629 if ((x = fabs(xr_org_s[i][b])) != 0.0)
631 xr34_s[i][b] = sqrt(x * sqrt(x));
633 #if ORG_QUANTANF_INIT
636 if (x > xmax_s[sfb][b])
639 xmax_line_s[sfb][b] = i;
647 else /* cut off the (highest frequency) entries in the unused scale factor band */
649 for (i = start; i < end; i++)
653 total_energy += energy_s[sfb][b] = temp;
655 if (sfb < SFB_SMAX-1)
656 xmin_s[sfb][b] = ratio->s[gr][ch][sfb][b] * temp;
661 /* ======== calc_scfsi ======== */
663 /* None of the granules contains short blocks */
664 if (!cod_info->window_switching_flag || (cod_info->block_type != SHORT_TYPE))
668 for (scfsi_band = 0; scfsi_band < 4; scfsi_band++)
669 side_info->scfsi[ch][scfsi_band] = 0;
675 /* calculation of number of available bit( per granule ) */
676 max_bits = ResvMaxBits (fr_ps, side_info, &pe[gr][ch], mean_bits);
680 /* all spectral values zero ? */
681 if (total_energy != 0.0)
686 #if ORG_QUANTANF_INIT
688 /* ======== quantanf_init (remaining) ======== */
690 #define system_const 8.0
691 #define minlimit -100.0
693 temp = my_nint (system_const * (log_sum/288.0 - log(total_energy/576.0)));
697 SS 19-12-96. Starting value of
698 global_gain or quantizerStepSize
699 has to be reduced for iteration_loop
703 cod_info->quantizerStepSize = temp;
705 #else /* ORG_QUANTANF_INIT */
707 double xmax, the_xmax;
709 the_lo_quant = -infinity; /* "-infinity" */
710 the_hi_quant = -infinity; /* the real maximum for high_quant is about +4 ! */
714 for (sfb = 0; sfb < end_sfb_l; sfb++)
719 lo_quant_l[sfb] = -infinity;
720 hi_quant_l[sfb] = -infinity;
724 lo_quant_l[sfb] = floor (4.0 * (log(xmax)/log_2 - cc)) + 1;
725 hi_quant_l[sfb] = floor (4.0 * (log(xmax)/log_2 - dd)) + 1;
730 the_lo_quant = lo_quant_l[sfb];
731 the_hi_quant = hi_quant_l[sfb];
736 for (sfb = min_used_sfb_s; sfb < end_sfb_s; sfb++)
738 for (b = 0; b < 3; b++)
740 xmax = xmax_s[sfb][b];
743 lo_quant_s[sfb][b] = -infinity;
744 hi_quant_s[sfb][b] = -infinity;
748 lo_quant_s[sfb][b] = floor (4.0 * (log(xmax)/log_2 - cc) /* - 8 * cod_info->subblock_gain[b] */) + 1;
749 hi_quant_s[sfb][b] = floor (4.0 * (log(xmax)/log_2 - dd) /* - 8 * cod_info->subblock_gain[b] */) + 1;
754 the_lo_quant = lo_quant_s[sfb][b];
755 the_hi_quant = hi_quant_s[sfb][b];
763 Try the power table at its least boundary
764 I«ve never reached this deep before!
766 assert (the_lo_quant > -POW216_MAX);
768 cod_info->quantizerStepSize = the_lo_quant;
770 #endif /* ORG_QUANTANF_INIT */
774 cod_info->part2_3_length = outer_loop (max_bits, fr_ps);
777 ResvAdjust (fr_ps, cod_info, side_info, mean_bits);
779 cod_info->global_gain = my_nint (cod_info->quantizerStepSize + 210.0);
780 /* assert (cod_info->global_gain < 256); */
784 ResvFrameEnd (fr_ps, side_info, mean_bits);
791 /* ======================================================================== */
793 /* ======================================================================== */
795 The outer iteration loop controls the masking conditions
796 of all scalefactorbands. It computes the best scalefac and
797 global gain. This module calls the inner iteration loop
800 static int outer_loop
806 int scalesave_l[SFB_LMAX-1];
807 int scalesave_s[SFB_SMAX-1][3];
808 int bits, huff_bits, save_preflag, save_compress, save_part2_length;
809 int sfb, b, over, iteration;
812 /* reset the pointers of our changed sfb [(sfb,b)] indices list */
813 mark_idx_l = mark_idx_s = 0;
817 cod_info->preflag = 0; /* assignments are all done in iteration_loop() */
818 cod_info->scalefac_compress = 0; /* just to show what«s going on ... */
819 cod_info->part2_length = 0; /* == part2_length(fr_ps) because of slen1_tab[0] = slen2_tab[0] = 0 */
822 huff_bits = max_bits /* - cod_info->part2_length */; /* try first without scaling */
827 bits = bin_search_StepSize (max_bits, cod_info->quantizerStepSize); /* speeds things up a bit */
831 for (sfb = 0; sfb < SFB_LMAX-1; sfb++) /* save scaling factors */
832 scalesave_l[sfb] = scalefac_l[sfb];
834 for (sfb = 0; sfb < SFB_SMAX-1; sfb++)
835 for (b = 0; b < 3; b++)
836 scalesave_s[sfb][b] = scalefac_s[sfb][b];
838 save_preflag = cod_info->preflag;
839 save_compress = cod_info->scalefac_compress;
840 save_part2_length = cod_info->part2_length;
842 calc_noise (); /* distortion calculation */
844 over = amplify (iteration);
848 misplaced break condition in original dist10:
849 - There is one flag only for both, marking a scalefactor band as
850 "should be amplified" or marking it as "is amplified", namely
851 a corresponding scalefactor greater than zero!
852 - The desired amplification of the scalefactors bands marked as
853 "should be amplified" is actually not done before the next call
854 to quantize(), respectively partial_quantize().
855 - Thus, it can happen that all scalefactor bands are marked, but
856 not all of the marked bands are amplified.
857 - Since loop_break() doesn't know that, the outer loop frequently
858 gets terminated to early.
864 huff_bits = max_bits - needed_bits_for_storing_scalefactors (fr_ps);
866 break; /* not enough space to store the scale factors */
869 /* We have to wait checking this break condition */
870 /* until needed_bits_for_storing_scalefactors() */
871 /* set the scalefac compress index!!! */
873 break; /* no more bands to amplify */
879 Most of the times, only a few bands will be changed,
880 so why quantize the whole area?
883 #if ORG_BINARY_SEARCH || ORG_QUANTANF_INIT || CHECK_TJ_OVERFLOW
884 assert (!tjBitOverflow2);
888 while (bits > huff_bits)
890 cod_info->quantizerStepSize += 1.0;
892 #if ORG_BINARY_SEARCH || ORG_QUANTANF_INIT || CHECK_TJ_OVERFLOW
893 assert (!tjBitOverflow2);
895 bits = count_bits ();
900 A break would mean to restore the parameters of the last iteration,
901 but we like to accept the current state. If you want to avoid the
902 'goto', you have to to take the long way home and place the loop
903 break condition in front of the call to calc_noise().
906 goto take_that_and_party;
911 cod_info->preflag = save_preflag;
912 cod_info->scalefac_compress = save_compress;
913 cod_info->part2_length = save_part2_length;
915 for (sfb = 0; sfb < SFB_LMAX-1; sfb++)
916 scalefac_l[sfb] = scalesave_l[sfb];
918 for (sfb = 0; sfb < SFB_SMAX-1; sfb++)
919 for (b = 0; b < 3; b++)
920 scalefac_s[sfb][b] = scalesave_s[sfb][b];
923 cod_info->part2_3_length = cod_info->part2_length + bits;
926 return cod_info->part2_3_length;
933 /* ======================================================================================== */
934 /* needed_bits_for_storing_scalefactors */
935 /* ======================================================================================== */
937 counts the bits needed to code the scale factors (cod_info->part2_length)
938 and the compression index (cod_info->scalefac_compress).
940 If there is no suitable index, it returns "infinity".
943 static int needed_bits_for_storing_scalefactors
949 static int slen1[16] = { 0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 };
950 static int slen2[16] = { 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3 };
954 static int pow2_slen1[16] = { 1, 1, 1, 1, 8, 2, 2, 2, 4, 4, 4, 8, 8, 8, 16, 16};
956 static int pow2_slen2[16] = { 1, 2, 4, 8, 1, 2, 4, 8, 2, 4, 8, 2, 4, 8, 4, 8};
958 /* (8+9) * slen1[k] + (9+9) * slen2[k] */
959 static int part2_len_m[16] = { 0, 18, 36, 54, 51, 35, 53, 71, 52, 70, 88, 69, 87,105,104,122};
960 /* (9+9) * slen1[k] + (9+9) * slen2[k] */
961 static int part2_len_s[16] = { 0, 18, 36, 54, 54, 36, 54, 72, 54, 72, 90, 72, 90,108,108,126};
962 /* (6+5) * slen1[k] + (5+5) * slen2[k] */
963 static int part2_len_l[16] = { 0, 10, 20, 30, 33, 21, 31, 41, 32, 42, 52, 43, 53, 63, 64, 74};
966 int max_slen1, max_slen2, *table;
969 max_slen1 = max_slen2 = 0;
971 if (cod_info->window_switching_flag && (cod_info->block_type == SHORT_TYPE))
973 if (cod_info->mixed_block_flag)
977 for (sfb = 0; sfb < 8; sfb++)
978 if (scalefac_l[sfb] > max_slen1)
979 max_slen1 = scalefac_l[sfb];
981 for (sfb = 3; sfb < 6; sfb++)
982 for (b = 0; b < 3; b++)
983 if (scalefac_s[sfb][b] > max_slen1)
984 max_slen1 = scalefac_s[sfb][b];
990 for (sfb = 0; sfb < 6; sfb++)
991 for (b = 0; b < 3; b++)
992 if (scalefac_s[sfb][b] > max_slen1)
993 max_slen1 = scalefac_s[sfb][b];
996 for (sfb = 6; sfb < 12/*SBMAX_s*/; sfb++)
997 for (b = 0; b < 3; b++)
998 if (scalefac_s[sfb][b] > max_slen2)
999 max_slen2 = scalefac_s[sfb][b];
1003 table = part2_len_l;
1005 for (sfb = 0; sfb < 11; sfb++)
1006 if (scalefac_l[sfb] > max_slen1)
1007 max_slen1 = scalefac_l[sfb];
1010 #if ORG_SCF_COMPRESS && !ORG_PREEMPHASING
1011 /* This was seen in LAME */
1012 if (!cod_info->preflag)
1014 for (sfb = 11; sfb < SBMAX_l; sfb++)
1015 if (scalefac_l[sfb] < (1 + cod_info->scalefac_scale) * pretab[sfb])
1020 for (sfb = 11; sfb < SBMAX_l; sfb++)
1021 scalefac_l[sfb] -= (1 + cod_info->scalefac_scale) * pretab[sfb];
1022 cod_info->preflag = 1;
1028 for (sfb = 11; sfb < 21/*SBMAX_l*/; sfb++)
1029 if (scalefac_l[sfb] > max_slen2)
1030 max_slen2 = scalefac_l[sfb];
1034 cod_info->part2_length = infinity;
1036 for (k = 0; k < 16; k++)
1038 if (max_slen1 < pow2_slen1[k] && max_slen2 < pow2_slen2[k])
1040 #if ORG_SCF_COMPRESS
1041 cod_info->scalefac_compress = k;
1042 cod_info->part2_length = table[k];
1045 if (cod_info->part2_length > table[k])
1047 cod_info->scalefac_compress = k;
1048 cod_info->part2_length = table[k];
1055 return cod_info->part2_length;
1062 /* ======================================================================================== */
1064 /* ======================================================================================== */
1066 calculates the distortion introduced by the qunatization
1067 in each scale factor band.
1069 static void calc_noise (void)
1071 int i, b, sfb, start, end, off;
1072 double f, sum, temp;
1075 off = -4 * (int)cod_info->quantizerStepSize;
1078 for (sfb = 0; sfb < max_used_sfb_l; sfb++)
1080 if (ix_l[xmax_line_l[sfb]] == 0) /* quantized values all zero? */
1082 xfsf_l[sfb] = energy_l[sfb]; /* see calculation of xmin_l */
1086 start = scalefac_band_long[sfb];
1087 end = scalefac_band_long[sfb+1];
1091 f = pow216[expo16_l[sfb] + off];
1093 for (i = start; i < end; i++)
1095 temp = fabs(xr_org_l[i]) - noisePowTab[ix_l[i]] / f;
1103 for (b = 0; b < 3; b++)
1105 off = -4 * ((int)cod_info->quantizerStepSize + 8 * cod_info->subblock_gain[b]);
1107 for (sfb = min_used_sfb_s; sfb < SFB_SMAX-1; sfb++)
1109 if (ix_s[xmax_line_s[sfb][b]] == 0) /* quantized values all zero? */
1111 xfsf_s[sfb][b] = energy_s[sfb][b]; /* see calculation of xmin_s */
1115 start = scalefac_band_short[sfb];
1116 end = scalefac_band_short[sfb+1];
1120 f = pow216[expo16_s[sfb][b] + off];
1122 for (i = start; i < end; i++)
1124 temp = fabs(xr_org_s[i][b]) - noisePowTab[ix_s[i][b]] / f;
1128 xfsf_s[sfb][b] = sum;
1138 /* ======================================================================================== */
1140 /* ======================================================================================== */
1142 returns zero if there is a scalefac which has not been amplified.
1143 Otherwise it returns one.
1146 static int loop_break (void)
1150 for (sfb = 0; sfb < cod_info->sfb_lmax; sfb++)
1151 if (scalefac_l[sfb] == 0)
1154 for (sfb = min_used_sfb_s; sfb < 12; sfb++)
1155 for (b = 0; b < 3; b++)
1156 if (scalefac_s[sfb][b] == 0)
1166 /* ======================================================================================== */
1167 /* preemphasing and amplifying */
1168 /* ======================================================================================== */
1170 Preemphasing: see ISO 11172-3 section C.1.5.4.3.4
1171 Amplifying : see ISO 11172-3 section C.1.5.4.3.5
1173 amplifying the scalefactor bands that violate the masking threshold.
1181 if (cod_info->window_switching_flag && cod_info->block_type == SHORT_TYPE)
1182 return amplify_short ();
1184 return amplify_long (iteration);
1191 static int amplify_short (void)
1193 int sfb, b, over, expo16_off;
1195 expo16_off = 16 * (1 + cod_info->scalefac_scale) / 2;
1199 for (sfb = 0; sfb < max_used_sfb_l; sfb++)
1201 if (xfsf_l[sfb] > xmin_l[sfb])
1204 expo16_l[sfb] += expo16_off;
1206 mark_tab_l[mark_idx_l++] = sfb;
1211 for (sfb = min_used_sfb_s; sfb < SBMAX_s; sfb++)
1213 for (b = 0; b < 3; b++)
1215 if (xfsf_s[sfb][b] > xmin_s[sfb][b])
1217 scalefac_s[sfb][b]++;
1218 expo16_s[sfb][b] += expo16_off;
1220 mark_tab_s[mark_idx_s++] = sfb;
1221 mark_tab_s[mark_idx_s++] = b;
1233 static int amplify_long
1238 int pre_expo_off[SFB_LMAX];
1240 int sfb, stop_at, over = 0;
1244 stop_at = max_used_sfb_l;
1246 expo16_off = 16 * (1 + cod_info->scalefac_scale) / 2;
1250 Preemphasis is switched on if in all the upper four scalefactor
1251 bands the actual distortion exceeds the threshold after the
1252 first call of the inner loop.
1254 Original bug of dist10 - preemphasis() didn't know 'iteration'!!!
1256 #if !ORG_PREEMPHASING
1259 if (!cod_info->preflag)
1261 for (sfb = max_used_sfb_l-4; sfb < max_used_sfb_l; sfb++)
1262 if (xfsf_l[sfb] <= xmin_l[sfb])
1263 goto no_preemphasing;
1265 cod_info->preflag = 1;
1267 stop_at = 11; /* pretab[sfb] = 0 for sfb = 0..10 */
1269 for (sfb = stop_at; sfb < max_used_sfb_l; sfb++)
1271 expo16_l[sfb] += pre_expo_off[sfb] = expo16_off * pretab[sfb];
1273 mark_tab_l[mark_idx_l++] = sfb;
1281 for (sfb = 0; sfb < stop_at; sfb++)
1283 if (xfsf_l[sfb] > xmin_l[sfb])
1286 expo16_l[sfb] += expo16_off;
1289 mark_tab_l[mark_idx_l++] = sfb;
1292 for (sfb = stop_at; sfb < max_used_sfb_l; sfb++) /* The just preemphased bands have to be treated differently */
1294 if (xfsf_l[sfb] > xmin_l[sfb] * pow216[2*pre_expo_off[sfb]])
1297 expo16_l[sfb] += expo16_off;
1310 /* ======================================================================== */
1312 /* ======================================================================== */
1314 Quantization of the vector xr ( -> ix)
1317 static int INLINE cutting_crew (FLOAT in)
1321 retVal = (int) (in + 0.4054);
1323 #if ORG_BINARY_SEARCH || ORG_QUANTANF_INIT || CHECK_TJ_OVERFLOW
1324 if (retVal > 8191+14)
1325 tjBitOverflow2 = TRUE;
1333 static void quantize (void)
1335 int sfb, i, b, start, end;
1338 for (sfb = 0; sfb < end_sfb_l; sfb++)
1340 start = scalefac_band_long[sfb];
1341 end = scalefac_band_long[sfb+1];
1343 /* (expo16_l[sfb] - 16/4 * quant_step) * 3/4 */
1344 f = pow216[(expo16_l[sfb]/4 - (int)cod_info->quantizerStepSize) * 3];
1346 for (i = start; i < end; i += 2)
1349 y = xr34_l[i+1] * f;
1350 ix_l[i ] = cutting_crew (z);
1351 ix_l[i+1] = cutting_crew (y);
1355 for (sfb = min_used_sfb_s; sfb < end_sfb_s; sfb++)
1357 start = scalefac_band_short[sfb];
1358 end = scalefac_band_short[sfb+1];
1360 for (b = 0; b < 3; b++)
1362 /* (expo_s[sfb][b] - 16/4 * (quant_step + 8 * cod_info->subblock_gain[b])) * 3/4 */
1363 f = pow216[(expo16_s[sfb][b] / 4 - (int)cod_info->quantizerStepSize - 8 * cod_info->subblock_gain[b]) * 3];
1365 for (i = start; i < end; i += 2)
1367 z = xr34_s[i ][b] * f;
1368 y = xr34_s[i+1][b] * f;
1369 ix_s[i ][b] = cutting_crew (z);
1370 ix_s[i+1][b] = cutting_crew (y);
1378 static void partial_quantize (void)
1380 int sfb, i, b, start, end;
1385 sfb = mark_tab_l[--mark_idx_l];
1387 start = scalefac_band_long[sfb];
1388 end = scalefac_band_long[sfb+1];
1390 /* (expo16_l[sfb] - 16/4 * quant_step) * 3/4 */
1391 f = pow216[(expo16_l[sfb]/4 - (int)cod_info->quantizerStepSize) * 3];
1393 for (i = start; i < end; i += 2)
1396 y = xr34_l[i+1] * f;
1397 ix_l[i ] = cutting_crew (z);
1398 ix_l[i+1] = cutting_crew (y);
1404 b = mark_tab_s[--mark_idx_s];
1405 sfb = mark_tab_s[--mark_idx_s];
1407 start = scalefac_band_short[sfb];
1408 end = scalefac_band_short[sfb+1];
1410 /* (expo_16s[sfb][b] - 16/4 * (quant_step + 8 * cod_info->subblock_gain[b])) * 3/4 */
1411 f = pow216[(expo16_s[sfb][b] / 4 - (int)cod_info->quantizerStepSize - 8 * cod_info->subblock_gain[b]) * 3];
1413 for (i = start; i < end; i += 2)
1415 z = xr34_s[i ][b] * f;
1416 y = xr34_s[i+1][b] * f;
1417 ix_s[i ][b] = cutting_crew (z);
1418 ix_s[i+1][b] = cutting_crew (y);
1427 /* ======================================================================== */
1429 /* ======================================================================== */
1433 unsigned region0_count;
1434 unsigned region1_count;
1435 } subdv_table[ 23 ] =
1437 {0, 0}, /* 0 bands */
1438 {0, 0}, /* 1 bands */
1439 {0, 0}, /* 2 bands */
1440 {0, 0}, /* 3 bands */
1441 {0, 0}, /* 4 bands */
1442 {0, 1}, /* 5 bands */
1443 {1, 1}, /* 6 bands */
1444 {1, 1}, /* 7 bands */
1445 {1, 2}, /* 8 bands */
1446 {2, 2}, /* 9 bands */
1447 {2, 3}, /* 10 bands */
1448 {2, 3}, /* 11 bands */
1449 {3, 4}, /* 12 bands */
1450 {3, 4}, /* 13 bands */
1451 {3, 4}, /* 14 bands */
1452 {4, 5}, /* 15 bands */
1453 {4, 5}, /* 16 bands */
1454 {4, 6}, /* 17 bands */
1455 {5, 6}, /* 18 bands */
1456 {5, 6}, /* 19 bands */
1457 {5, 7}, /* 20 bands */
1458 {6, 7}, /* 21 bands */
1459 {6, 7}, /* 22 bands */
1465 Calculation of rzero, count1, big_values
1466 (Partitions ix into big values, quadruples and zeros).
1468 Determines the number of bits to encode the quadruples.
1470 Presumable subdivides the bigvalue region which will
1471 use separate Huffman tables.
1473 Select huffman code tables for bigvalues regions
1475 Count the number of bits necessary to code the bigvalues region.
1478 static int count_bits (void)
1480 cod_info->table_select[0] = 0;
1481 cod_info->table_select[1] = 0;
1482 cod_info->table_select[2] = 0;
1484 if (cod_info->window_switching_flag && (cod_info->block_type == SHORT_TYPE))
1485 return count_bits_short ();
1487 return count_bits_long ();
1494 static int count_bits_short (void)
1496 unsigned int bits = 0;
1499 Within each scalefactor band, data is given for successive
1500 time windows, beginning with window 0 and ending with window 2.
1501 Within each window, the quantized values are then arranged in
1502 order of increasing frequency...
1505 unsigned int max, temp;
1509 the first part --- 8 long blocks or 3 short blocks
1514 if (cod_info->mixed_block_flag)
1516 for (sfb = 0; sfb < 8; sfb++)
1517 if ((temp = ix_l[xmax_line_l[sfb]]) > max)
1519 choose_table_long (0, 36, max, &cod_info->table_select[0], &bits);
1524 for (sfb = 0; sfb < 3; sfb++)
1525 for (b = 0; b < 3; b++)
1526 if ((temp = ix_s[xmax_line_s[sfb][b]][b]) > max)
1528 choose_table_short (0, 3, max, &cod_info->table_select[0], &bits);
1533 the second part --- short blocks only
1537 for (sfb = 3; sfb < SFB_SMAX; sfb++)
1538 for (b = 0; b < 3; b++)
1539 if ((temp = ix_s[xmax_line_s[sfb][b]][b]) > max)
1541 choose_table_short (3, SFB_SMAX, max, &cod_info->table_select[1], &bits);
1550 static int count_bits_long (void)
1559 int sfb_anz, index0, index1, sfb, i;
1565 for (zero_region = 576; zero_region > 1; zero_region -= 2)
1566 if (ix_l[zero_region-1]) break;
1567 else if (ix_l[zero_region-2]) break;
1569 for (bigv_region = zero_region; bigv_region > 3; bigv_region -= 4)
1571 if (ix_l[bigv_region-1] > 1) break;
1572 else if (ix_l[bigv_region-2] > 1) break;
1573 else if (ix_l[bigv_region-3] > 1) break;
1574 else if (ix_l[bigv_region-4] > 1) break;
1577 if (ix_l[bigv_region-1]) bits++, p |= 8;
1578 if (ix_l[bigv_region-2]) bits++, p |= 4;
1579 if (ix_l[bigv_region-3]) bits++, p |= 2;
1580 if (ix_l[bigv_region-4]) bits++, p |= 1;
1582 sum0 += ht[32].hlen[p];
1583 sum1 += ht[33].hlen[p];
1586 cod_info->count1 = (zero_region-bigv_region) / 4;
1587 cod_info->big_values = bigv_region / 2;
1592 cod_info->count1table_select = 0;
1597 cod_info->count1table_select = 1;
1603 while (scalefac_band_long[sfb_anz] < bigv_region)
1606 if (cod_info->window_switching_flag) /* START_TYPE, STOP_TYPE */
1608 index0 = (cod_info->region0_count = 7) + 1;
1609 cod_info->region1_count = 13;
1610 index1 = sfb_anz - index0; if (index0 + index1 < 22) index1++;
1612 cod_info->address1 = 36;
1613 cod_info->address2 = bigv_region;
1614 cod_info->address3 = 0;
1616 else /* NORM_TYPE */
1618 index0 = (cod_info->region0_count = subdv_table[sfb_anz].region0_count) + 1;
1619 index1 = (cod_info->region1_count = subdv_table[sfb_anz].region1_count) + 1;
1621 cod_info->address1 = scalefac_band_long[index0];
1622 cod_info->address2 = scalefac_band_long[index0 + index1];
1623 cod_info->address3 = bigv_region;
1626 if (cod_info->address1 > 0)
1629 for (sfb = 0; sfb < index0; sfb++)
1630 if ((temp = ix_l[xmax_line_l[sfb]]) > max)
1632 choose_table_long (0, cod_info->address1, max, &cod_info->table_select[0], &bits);
1635 if (cod_info->address2 > cod_info->address1)
1638 for (sfb = index0; sfb < index0+index1; sfb++)
1639 if ((temp = ix_l[xmax_line_l[sfb]]) > max)
1641 choose_table_long (cod_info->address1, cod_info->address2, max, &cod_info->table_select[1], &bits);
1644 if (bigv_region > cod_info->address2)
1647 for (sfb = index0+index1; sfb < sfb_anz-1; sfb++)
1648 if ((temp = ix_l[xmax_line_l[sfb]]) > max)
1650 for (i = scalefac_band_long[sfb_anz-1]; i < bigv_region; i++)
1651 if ((temp = ix_l[i]) > max)
1653 choose_table_long (cod_info->address2, bigv_region, max, &cod_info->table_select[2], &bits);
1657 { /* no big_values region */
1658 cod_info->region0_count = 0;
1659 cod_info->region1_count = 0;
1661 cod_info->address1 = 0;
1662 cod_info->address2 = 0;
1663 cod_info->address3 = 0;
1674 /* ======================================================================== */
1675 /* bin_search_step_size */
1676 /* ======================================================================== */
1678 The following optional code written by Seymour Shlien
1679 will speed up the outer_loop code which is called
1680 by iteration_loop. When BIN_SEARCH is defined, the
1681 outer_loop function precedes the call to the function inner_loop
1682 with a call to bin_search gain defined below, which
1683 returns a good starting quantizerStepSize.
1685 The function count_bits() [a sequence of statements, originally part of inner_loop()]
1686 was completely rewritten.
1689 changed the behaviour:
1690 now, it returns the found number of bits <= desired_rate
1693 static int bin_search_StepSize
1702 #if ORG_BINARY_SEARCH || ORG_QUANTANF_INIT
1705 int bot = the_hi_quant;
1709 #if ORG_BINARY_SEARCH
1716 next = (top + bot) / 2;
1717 cod_info->quantizerStepSize = next;
1719 tjBitOverflow2 = FALSE;
1724 bits = count_bits ();
1726 if (bits > desired_rate)
1731 while ((bits != desired_rate) && (abs(last-next) > 1));
1733 #else /* ORG_BINARY_SEARCH */
1737 next = top + (bot - top) / 2;
1738 cod_info->quantizerStepSize = next;
1740 #if ORG_BINARY_SEARCH || ORG_QUANTANF_INIT || CHECK_TJ_OVERFLOW
1741 tjBitOverflow2 = FALSE;
1746 bits = count_bits ();
1749 bits = count_bits ();
1752 if (bits > desired_rate)
1759 #endif /* ORG_BINARY_SEARCH */
1761 if (bits > desired_rate)
1763 cod_info->quantizerStepSize = next+1;
1764 #if ORG_BINARY_SEARCH || ORG_QUANTANF_INIT || CHECK_TJ_OVERFLOW
1765 tjBitOverflow2 = FALSE;
1767 assert(! tjBitOverflow2);
1771 bits = count_bits ();
1772 assert(bits <= desired_rate);
1783 /* ======================================================================================== */
1784 /* choose_table_long */
1785 /* ======================================================================================== */
1787 Choose the Huffman table that will encode ix[start..end] with the fewest
1788 bits and increases the bit_sum by the amount of these bits.
1790 Note: This code contains knowledge about the sizes and characteristics
1791 of the Huffman tables as defined in the IS (Table B.7), and will not work
1792 with any arbitrary tables.
1794 static void choose_table_long
1803 unsigned choice0, choice1;
1814 choice0 = 1; /* we can start with 1 because ht[0].xlen == 0 <= max */
1815 while (ht[choice0].xlen <= max)
1820 case 1: single_Huffman (start, end,/* 1 */ table, bit_sum); break;
1821 case 2: double_Huffman (start, end, 2, 3, table, bit_sum); break;
1822 case 5: double_Huffman (start, end, 5, 6, table, bit_sum); break;
1823 case 7: triple_Huffman (start, end, 7, 8, 9, table, bit_sum); break;
1824 case 10: triple_Huffman (start, end, 10, 11, 12, table, bit_sum); break;
1825 case 13: double_Huffman (start, end, 13, 15, table, bit_sum); break;
1828 #if !ORG_HUFFMAN_CODING /* no part of original BladeEnc */
1831 triple_Huffman_2 (start, end,/* 13, 15, 24, */ table, bit_sum);
1838 #if ORG_HUFFMAN_CODING
1839 choice0 = 15; while (ht[choice0].linmax < max) choice0++;
1841 choice0 = 16; while (ht[choice0].linmax < max) choice0++;
1844 assert(choice0 < 24);
1845 choice1 = 24; while (ht[choice1].linmax < max) choice1++;
1846 assert(choice1 < 32);
1848 #if ORG_HUFFMAN_CODING
1849 double_Huffman_2 (start, end, choice1, choice0, table, bit_sum);
1851 double_Huffman_2 (start, end, choice0, choice1, table, bit_sum);
1860 /* ======================================================================================== */
1861 /* choose_table_short */
1862 /* ======================================================================================== */
1864 Choose the Huffman table that will encode ix[start_sfb..end_sfb][0..2]
1865 with the fewest bits and increases the bit_sum by the amount of these bits.
1867 Note: This code contains knowledge about the sizes and characteristics
1868 of the Huffman tables as defined in the IS (Table B.7), and will not work
1869 with any arbitrary tables.
1872 static void choose_table_short
1882 #if !ORG_HUFFMAN_CODING
1887 start = 3 * scalefac_band_short[start_sfb];
1888 end = 3 * scalefac_band_short[ end_sfb];
1898 choice0 = 1; /* we can start with 1 because ht[0].xlen == 0 <= max */
1899 while (ht[choice0].xlen <= max)
1902 #if ORG_HUFFMAN_CODING
1903 tiny_single_Huffman (start, end, choice0, table, bit_sum);
1907 case 1: tiny_single_Huffman (start, end,/* 1 */ table, bit_sum); break;
1908 case 2: tiny_double_Huffman (start, end, 2, 3, table, bit_sum); break;
1909 case 5: tiny_double_Huffman (start, end, 5, 6, table, bit_sum); break;
1910 case 7: tiny_triple_Huffman (start, end, 7, 8, 9, table, bit_sum); break;
1911 case 10: tiny_triple_Huffman (start, end, 10, 11, 12, table, bit_sum); break;
1912 case 13: tiny_double_Huffman (start, end, 13, 15, table, bit_sum); break;
1916 #if !ORG_HUFFMAN_CODING /* no part of original BladeEnc */
1919 tiny_triple_Huffman_2 (start, end,/* 13, 15, 24, */ table, bit_sum);
1926 #if ORG_HUFFMAN_CODING
1928 choice0 = 15; while (ht[choice0].linmax < max) choice0++;
1929 assert(choice0 < 24);
1930 tiny_single_Huffman_2 (start, end, choice0, table, bit_sum);
1934 choice0 = 16; while (ht[choice0].linmax < max) choice0++;
1935 assert(choice0 < 24);
1936 choice1 = 24; while (ht[choice1].linmax < max) choice1++;
1937 assert(choice1 < 32);
1938 tiny_double_Huffman_2 (start, end, choice0, choice1, table, bit_sum);
1948 /* ======================================================================================== */
1950 /* ======================================================================================== */
1955 That case, we don«t need to decide which is the best table.
1958 static void single_Huffman
1962 /* unsigned table0, == 1 */
1970 unsigned bits0, signs, idx;
1972 static struct huffcodetab *h0 = ht + /* table0 */ 1; /* static because of the constant!!! */
1974 #if 0 /* not needed */
1975 static unsigned ylen = h0->ylen; /* == 2 */
1978 int *pos = ix_l + start;
1979 int *fin = ix_l + end;
1987 v = *pos++; if (v) {signs++; idx = v /* * ylen */ + v;}
1988 v = *pos++; if (v) {signs++; idx += v;}
1990 if (*pos++) {signs++; idx = 2;}
1991 if (*pos++) {signs++; idx++;}
1993 bits0 += h0->hlen[idx];
1996 *choice = /* table0 */ 1;
1997 *sum += bits0 + signs;
2002 #if ORG_HUFFMAN_CODING
2003 static void tiny_single_Huffman
2013 unsigned bits0, signs, idx0, idx1, idx2;
2015 struct huffcodetab *h0 = ht + table0;
2017 unsigned ylen = h0->ylen;
2019 int *pos = ix_l + start;
2020 int *fin = ix_l + end;
2026 idx0 = idx1 = idx2 = 0;
2028 v0 = *pos++; if (v0) {signs++; idx0 = v0 * ylen;}
2029 v1 = *pos++; if (v1) {signs++; idx1 = v1 * ylen;}
2030 v2 = *pos++; if (v2) {signs++; idx2 = v2 * ylen;}
2031 v0 = *pos++; if (v0) {signs++; idx0 += v0;}
2032 v1 = *pos++; if (v1) {signs++; idx1 += v1;}
2033 v2 = *pos++; if (v2) {signs++; idx2 += v2;}
2035 bits0 += h0->hlen[idx0] + h0->hlen[idx1] + h0->hlen[idx2];
2039 *sum += bits0 + signs;
2042 static void tiny_single_Huffman
2046 /* unsigned table0 == 1 */
2054 unsigned bits0, signs, idx0, idx1, idx2;
2056 static struct huffcodetab *h0 = ht + /* table0 */ 1; /* static because of the constant!!! */
2058 #if 0 /* not needed */
2059 static unsigned ylen = h0->ylen; /* == 2 --- static because of the constant!!! */
2062 int *pos = ix_l + start;
2063 int *fin = ix_l + end;
2069 idx0 = idx1 = idx2 = 0;
2071 if (*pos++) {signs++; idx0 = 2;}
2072 if (*pos++) {signs++; idx1 = 2;}
2073 if (*pos++) {signs++; idx2 = 2;}
2074 if (*pos++) {signs++; idx0++;}
2075 if (*pos++) {signs++; idx1++;}
2076 if (*pos++) {signs++; idx2++;}
2078 bits0 += h0->hlen[idx0] + h0->hlen[idx1] + h0->hlen[idx2];
2081 *choice = /* table0 */ 1;
2082 *sum += bits0 + signs;
2088 #if ORG_HUFFMAN_CODING
2089 static void tiny_single_Huffman_2 /* Escape tables */
2093 unsigned table0, /* 15... */
2099 unsigned bits0, signs, xbits, idx0, idx1, idx2;
2101 struct huffcodetab *h0 = ht + table0;
2103 #if 0 /* not needed */
2104 static unsigned ylen = h0->ylen; /* == h1->ylen == 16 --- static because of the constant!!! */
2106 int *pos = ix_l + start;
2107 int *fin = ix_l + end;
2109 bits0 = signs = xbits = 0;
2113 idx0 = idx1 = idx2 = 0;
2115 v0 = *pos++; if (v0) {if (v0 > 14) {v0 = 15; xbits++;} signs++; idx0 = v0 /* * ylen */ << 4;}
2116 v1 = *pos++; if (v1) {if (v1 > 14) {v1 = 15; xbits++;} signs++; idx1 = v1 /* * ylen */ << 4;}
2117 v2 = *pos++; if (v2) {if (v2 > 14) {v2 = 15; xbits++;} signs++; idx2 = v2 /* * ylen */ << 4;}
2118 v0 = *pos++; if (v0) {if (v0 > 14) {v0 = 15; xbits++;} signs++; idx0 += v0;}
2119 v1 = *pos++; if (v1) {if (v1 > 14) {v1 = 15; xbits++;} signs++; idx1 += v1;}
2120 v2 = *pos++; if (v2) {if (v2 > 14) {v2 = 15; xbits++;} signs++; idx2 += v2;}
2122 bits0 += h0->hlen[idx0] + h0->hlen[idx1] + h0->hlen[idx2];
2125 bits0 += xbits * h0->linbits;
2128 *sum += bits0 + signs;
2137 The following function is called for the most maximum values below 16 (respectively 15)
2140 static void double_Huffman
2144 unsigned table0, /* 2, 5, 13 */
2145 unsigned table1, /* 3, 6, 15 */
2151 unsigned bits0, bits1, signs, idx;
2153 struct huffcodetab *h0 = ht + table0;
2154 struct huffcodetab *h1 = ht + table1;
2156 unsigned ylen = h0->ylen; /* == h1->ylen */
2158 int *pos = ix_l + start;
2159 int *fin = ix_l + end;
2161 bits0 = bits1 = signs = 0;
2166 v = *pos++; if (v) {signs++; idx = v * ylen;}
2167 v = *pos++; if (v) {signs++; idx += v;}
2168 bits0 += h0->hlen[idx];
2169 bits1 += h1->hlen[idx];
2175 *sum += bits0 + signs;
2180 *sum += bits1 + signs;
2186 static void tiny_double_Huffman
2190 unsigned table0, /* 2, 5, 13 */
2191 unsigned table1, /* 3, 6, 15 */
2197 unsigned bits0, bits1, signs, idx0, idx1, idx2;
2199 struct huffcodetab *h0 = ht + table0;
2200 struct huffcodetab *h1 = ht + table1;
2202 unsigned ylen = h0->ylen; /* == h1->ylen */
2204 int *pos = ix_l + start;
2205 int *fin = ix_l + end;
2207 bits0 = bits1 = signs = 0;
2211 idx0 = idx1 = idx2 = 0;
2212 v0 = *pos++; if (v0) {signs++; idx0 = v0 * ylen;}
2213 v1 = *pos++; if (v1) {signs++; idx1 = v1 * ylen;}
2214 v2 = *pos++; if (v2) {signs++; idx2 = v2 * ylen;}
2215 v0 = *pos++; if (v0) {signs++; idx0 += v0;}
2216 v1 = *pos++; if (v1) {signs++; idx1 += v1;}
2217 v2 = *pos++; if (v2) {signs++; idx2 += v2;}
2218 bits0 += h0->hlen[idx0] + h0->hlen[idx1] + h0->hlen[idx2];
2219 bits1 += h1->hlen[idx0] + h1->hlen[idx1] + h1->hlen[idx2];
2225 *sum += bits0 + signs;
2230 *sum += bits1 + signs;
2237 poor men«s brave tailor --- only three at a blow...
2240 static void triple_Huffman
2244 unsigned table0, /* 7, 10 */
2245 unsigned table1, /* 8, 11 */
2246 unsigned table2, /* 9, 12 */
2252 unsigned bits0, bits1, bits2, signs, idx;
2254 struct huffcodetab *h0 = ht + table0;
2255 struct huffcodetab *h1 = ht + table1;
2256 struct huffcodetab *h2 = ht + table2;
2258 unsigned ylen = h0->ylen; /* == h1->ylen == h2->ylen */
2260 int *pos = ix_l + start;
2261 int *fin = ix_l + end;
2263 bits0 = bits1 = bits2 = signs = 0;
2268 v = *pos++; if (v) {signs++; idx = v * ylen;}
2269 v = *pos++; if (v) {signs++; idx += v;}
2270 bits0 += h0->hlen[idx];
2271 bits1 += h1->hlen[idx];
2272 bits2 += h2->hlen[idx];
2275 if (bits0 < bits1 && bits0 < bits2)
2278 *sum += bits0 + signs;
2280 else if (bits1 < bits2)
2283 *sum += bits1 + signs;
2288 *sum += bits2 + signs;
2294 static void tiny_triple_Huffman
2298 unsigned table0, /* 7, 10 */
2299 unsigned table1, /* 8, 11 */
2300 unsigned table2, /* 9, 12 */
2306 unsigned bits0, bits1, bits2, signs, idx0, idx1, idx2;
2308 struct huffcodetab *h0 = ht + table0;
2309 struct huffcodetab *h1 = ht + table1;
2310 struct huffcodetab *h2 = ht + table2;
2312 unsigned ylen = h0->ylen; /* == h1->ylen == h2->ylen */
2314 int *pos = ix_l + start;
2315 int *fin = ix_l + end;
2317 bits0 = bits1 = bits2 = signs = 0;
2321 idx0 = idx1 = idx2 = 0;
2322 v0 = *pos++; if (v0) {signs++; idx0 = v0 * ylen;}
2323 v1 = *pos++; if (v1) {signs++; idx1 = v1 * ylen;}
2324 v2 = *pos++; if (v2) {signs++; idx2 = v2 * ylen;}
2325 v0 = *pos++; if (v0) {signs++; idx0 += v0;}
2326 v1 = *pos++; if (v1) {signs++; idx1 += v1;}
2327 v2 = *pos++; if (v2) {signs++; idx2 += v2;}
2328 bits0 += h0->hlen[idx0] + h0->hlen[idx1] + h0->hlen[idx2];
2329 bits1 += h1->hlen[idx0] + h1->hlen[idx1] + h1->hlen[idx2];
2330 bits2 += h2->hlen[idx0] + h2->hlen[idx1] + h2->hlen[idx2];
2333 if (bits0 < bits1 && bits0 < bits2)
2336 *sum += bits0 + signs;
2338 else if (bits1 < bits2)
2341 *sum += bits1 + signs;
2346 *sum += bits2 + signs;
2355 The escape table 24 deals with linbits=4 instead of linbits=0 in case of table 13 and 15.
2356 Nevertheless, sometimes it produces the better result...
2357 Furthermore we take advantage because of the constant table numbers.
2360 static void triple_Huffman_2
2364 /* unsigned table0, == 13 */
2365 /* unsigned table1, == 15 */
2366 /* unsigned table2, == 24 */
2372 unsigned bits0, bits1, bits2, signs, idx;
2374 static struct huffcodetab *h0 = ht + /* table0 */ 13; /* all static declarations because of the constant values!!! */
2375 static struct huffcodetab *h1 = ht + /* table1 */ 15;
2376 static struct huffcodetab *h2 = ht + /* table2 */ 24;
2378 #if 0 /* not needed */
2379 static unsigned ylen = h0->ylen; /* == h1->ylen == h2->ylen */ /* == 16 */
2382 int *pos = ix_l + start;
2383 int *fin = ix_l + end;
2385 bits0 = bits1 = bits2 = signs = 0;
2390 v = *pos++; if (v) {if (v == 15) bits2 += /* h2->linbits */ 4; signs++; idx = v /* * ylen */ << 4;}
2391 v = *pos++; if (v) {if (v == 15) bits2 += /* h2->linbits */ 4; signs++; idx += v;}
2392 bits0 += h0->hlen[idx];
2393 bits1 += h1->hlen[idx];
2394 bits2 += h2->hlen[idx];
2397 if (bits0 < bits1 && bits0 < bits2)
2399 *choice = /* table0 */ 13;
2400 *sum += bits0 + signs;
2402 else if (bits1 < bits2)
2404 *choice = /* table1 */ 15;
2405 *sum += bits1 + signs;
2409 *choice = /* table2 */ 24;
2410 *sum += bits2 + signs;
2416 static void tiny_triple_Huffman_2
2420 /* unsigned table0, == 13 */
2421 /* unsigned table1, == 15 */
2422 /* unsigned table2, == 24 */
2428 unsigned bits0, bits1, bits2, signs, idx0, idx1, idx2;
2430 static struct huffcodetab *h0 = ht + /* table0 */ 13; /* all static declarations because of the constant values!!! */
2431 static struct huffcodetab *h1 = ht + /* table1 */ 15;
2432 static struct huffcodetab *h2 = ht + /* table2 */ 24;
2434 #if 0 /* not needed */
2435 static unsigned ylen = h0->ylen; /* == h1->ylen == h2->ylen */ /* == 16 */
2438 int *pos = ix_l + start;
2439 int *fin = ix_l + end;
2441 bits0 = bits1 = bits2 = signs = 0;
2445 idx0 = idx1 = idx2 = 0;
2446 v0 = *pos++; if (v0) {if (v0 == 15) bits2 += /* h2->linbits */ 4; signs++; idx0 = v0 /* * ylen */ << 4;}
2447 v1 = *pos++; if (v1) {if (v1 == 15) bits2 += /* h2->linbits */ 4; signs++; idx1 = v1 /* * ylen */ << 4;}
2448 v2 = *pos++; if (v2) {if (v2 == 15) bits2 += /* h2->linbits */ 4; signs++; idx2 = v2 /* * ylen */ << 4;}
2449 v0 = *pos++; if (v0) {if (v0 == 15) bits2 += /* h2->linbits */ 4; signs++; idx0 += v0;}
2450 v1 = *pos++; if (v1) {if (v1 == 15) bits2 += /* h2->linbits */ 4; signs++; idx1 += v1;}
2451 v2 = *pos++; if (v2) {if (v2 == 15) bits2 += /* h2->linbits */ 4; signs++; idx2 += v2;}
2452 bits0 += h0->hlen[idx0] + h0->hlen[idx1] + h0->hlen[idx2];
2453 bits1 += h1->hlen[idx0] + h1->hlen[idx1] + h1->hlen[idx2];
2454 bits2 += h2->hlen[idx0] + h2->hlen[idx1] + h2->hlen[idx2];
2457 if (bits0 < bits1 && bits0 < bits2)
2459 *choice = /* table0 */ 13;
2460 *sum += bits0 + signs;
2462 else if (bits1 < bits2)
2464 *choice = /* table1 */ 15;
2465 *sum += bits1 + signs;
2469 *choice = /* table2 */ 24;
2470 *sum += bits2 + signs;
2479 In case of two escape tables, we esepecially have to take care for
2480 the possibly different linbits values...
2483 static void double_Huffman_2 /* Escape tables */
2487 unsigned table0, /* 16... */
2488 unsigned table1, /* 24... */
2494 unsigned bits0, bits1, signs, xbits, idx;
2496 struct huffcodetab *h0 = ht + table0;
2497 struct huffcodetab *h1 = ht + table1;
2499 #if 0 /* not needed */
2500 static unsigned ylen = h0->ylen; /* == h1->ylen */ /* == 16 */
2502 unsigned linbits0 = h0->linbits;
2503 unsigned linbits1 = h1->linbits;
2505 int *pos = ix_l + start;
2506 int *fin = ix_l + end;
2508 bits0 = bits1 = signs = xbits = 0;
2513 v = *pos++; if (v) {if (v > 14) {v = 15; xbits++;/*bits0 += linbits0; bits1 += linbits1;*/} signs++; idx = v /* * ylen */ << 4;}
2514 v = *pos++; if (v) {if (v > 14) {v = 15; xbits++;/*bits0 += linbits0; bits1 += linbits1;*/} signs++; idx += v;}
2515 bits0 += h0->hlen[idx];
2516 bits1 += h1->hlen[idx];
2518 bits0 += xbits * linbits0;
2519 bits1 += xbits * linbits1;
2524 *sum += bits0 + signs;
2529 *sum += bits1 + signs;
2535 static void tiny_double_Huffman_2 /* Escape tables */
2539 unsigned table0, /* 16... */
2540 unsigned table1, /* 24... */
2546 unsigned bits0, bits1, signs, xbits, idx0, idx1, idx2;
2548 struct huffcodetab *h0 = ht + table0;
2549 struct huffcodetab *h1 = ht + table1;
2551 #if 0 /* not needed */
2552 static unsigned ylen = h0->ylen; /* == h1->ylen == 16 --- static because of the constant!!! */
2554 int *pos = ix_l + start;
2555 int *fin = ix_l + end;
2557 bits0 = bits1 = signs = xbits = 0;
2561 idx0 = idx1 = idx2 = 0;
2563 v0 = *pos++; if (v0) {if (v0 > 14) {v0 = 15; xbits++;} signs++; idx0 = v0 /* * ylen */ << 4;}
2564 v1 = *pos++; if (v1) {if (v1 > 14) {v1 = 15; xbits++;} signs++; idx1 = v1 /* * ylen */ << 4;}
2565 v2 = *pos++; if (v2) {if (v2 > 14) {v2 = 15; xbits++;} signs++; idx2 = v2 /* * ylen */ << 4;}
2566 v0 = *pos++; if (v0) {if (v0 > 14) {v0 = 15; xbits++;} signs++; idx0 += v0;}
2567 v1 = *pos++; if (v1) {if (v1 > 14) {v1 = 15; xbits++;} signs++; idx1 += v1;}
2568 v2 = *pos++; if (v2) {if (v2 > 14) {v2 = 15; xbits++;} signs++; idx2 += v2;}
2570 bits0 += h0->hlen[idx0] + h0->hlen[idx1] + h0->hlen[idx2];
2571 bits1 += h1->hlen[idx0] + h1->hlen[idx1] + h1->hlen[idx2];
2574 bits0 += xbits * h0->linbits;
2575 bits1 += xbits * h1->linbits;
2580 *sum += bits0 + signs;
2585 *sum += bits1 + signs;