replaced libart with new polygon code
[swftools.git] / lib / bladeenc / loop.c
1 /*
2                         (c) Copyright 1998-2001 - Tord Jansson
3                         ======================================
4
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.
9
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.
13
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.
18
19
20
21         ------------    Changes    ------------
22
23         2000-11-10  Andre Piotrowski
24
25         -       reformatted, used 'static' functions, global variables, less parameters
26
27         2000-11-11  ap
28
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()
32
33         2000-11-22  ap
34
35         -       bug fix:  module - reset fInit_huffman_read_flag
36
37         2000-11-28  ap
38         -       speed up        :  implemented partial quantizing
39
40         2000-11-30  ap
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)]
46
47         2000-12-02  ap
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]
52
53         2000-12-03  ap
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"]
60
61         2000-12-10  ap
62         -       definitely killed SCFSI
63
64         2001-01-12  ap
65
66         -       use some explicit type casting to avoid compiler warnings
67         -       clear some backward compatability flags for 0.93.10
68
69         2001-04-07  ap
70         -       implemented flag CHECK_TJ_OVERFLOW to check for huffman table overflow
71 */
72
73
74
75
76
77 /*  ========================================================================================  */
78 /*              keeping backward compatability                                                */
79 /*  ========================================================================================  */
80
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) */
88
89 #define         CHECK_TJ_OVERFLOW               1   /* 1 = check for huffman table overflow */
90
91
92
93
94
95 #define         infinity                                99999999
96
97
98
99
100
101 #include        <stdio.h>
102 #include        <stdlib.h>
103 #include        <math.h>
104 #include        <assert.h>
105
106 #include        "system.h"
107 #include        "common.h"
108
109 #include        "l3side.h"
110 #include        "l3psy.h"
111 #include        "huffman.h"
112 #include        "l3bitstream.h"
113 #include        "reservoir.h"
114 #include        "loop.h"
115 #include        "loop-pvt.h"
116
117
118
119
120
121 #if ORG_HUFFMAN_CODING
122 static  void                    tiny_single_Huffman_2   /* Escape tables */
123 (
124         unsigned                                start,
125         unsigned                                end,
126         unsigned                                table0,   /* 15... */
127         unsigned                                *choice,
128         unsigned                                *sum
129 );
130 #endif
131
132
133
134 static  int                             amplify_short (void);
135 static  int                             amplify_long
136 (
137         int                                             iteration
138 );
139
140
141
142
143
144 int                                             my_nint (double in)
145 {
146
147         if (in < 0)
148                 return (int)(in - 0.5);
149         else
150                 return (int)(in + 0.5);
151 }
152
153
154
155
156
157 /*
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)
163 */
164
165 struct scalefac_struct sfBandIndex[3] =
166 {
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}
170         },
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}
174         },
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}
178         }
179 };
180
181
182
183 /*
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:
188
189         [table_number][row_in_table][column of nr_of_sfb]
190 */
191
192 static unsigned nr_of_sfb_block[6][3][4] =
193 {
194         {
195                 { 6, 5, 5, 5},
196                 { 9, 9, 9, 9},
197                 { 6, 9, 9, 9}
198         },
199         {
200                 {6, 5,  7, 3},
201                 {9, 9, 12, 6},
202                 {6, 9, 12, 6}
203         },
204         {
205                 {11,10, 0, 0},
206                 {18,18, 0, 0},
207                 {15,18, 0, 0}
208         },
209         {
210                 { 7, 7, 7, 0},
211                 {12,12,12, 0},
212                 { 6,15,12, 0}
213         },
214         {
215                 { 6, 6, 6, 3},
216                 {12, 9, 9, 6},
217                 { 6,12, 9, 6}
218         },
219         {
220                 { 8, 8, 5, 0},
221                 {15,12, 9, 0},
222                 { 6,18, 9, 0}
223         }
224 };
225
226
227
228 /* Table B.6: layer3 preemphasis */
229 int  pretab[21] =
230 {
231         0, 0, 0, 0, 0, 0, 0,
232         0, 0, 0, 0, 1, 1, 1, 
233         1, 2, 2, 3, 3, 3, 2
234 };
235
236
237
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 };
240
241
242
243 int                                             *scalefac_band_long;
244 int                                             *scalefac_band_short;
245
246 int                                             fInit_iteration_loop;
247
248 #if ORG_BINARY_SEARCH || ORG_QUANTANF_INIT || CHECK_TJ_OVERFLOW
249 int                                             tjBitOverflow2;
250 #endif
251
252
253 /* We need this initialization here since some compilers chokes on the
254    the declaration if it constains an initialization that point directly
255    into a struct.
256 */
257
258 void fixStatic_loop( void )
259 {
260         scalefac_band_long  = &sfBandIndex[0].l[0];
261         scalefac_band_short = &sfBandIndex[0].s[0];
262 }
263
264
265
266
267
268 /*  ========================================================================  */
269 /*              generating the power tables                                   */
270 /*  ========================================================================  */
271
272
273
274 #define BOT                                             200
275 #define POW216_MAX                              (BOT/2 * 16)
276 #define POW216_MIN                              (-25 * 16)
277
278
279
280 static  double                                  pow216_space[POW216_MAX-POW216_MIN+1];
281 static  double                                  *pow216 = pow216_space - POW216_MIN;
282 static  double                                  noisePowTab[8191+15];
283
284
285
286 void genNoisePowTab (void)
287 {
288         int     i;
289
290         for (i = POW216_MIN;  i <= POW216_MAX;  i++)
291                 pow216[i] = pow (2.0, (double)i/16.0);
292
293         for (i = 0;  i < 8191+15;  i++)
294                 noisePowTab[i] = pow (i, 4.0/3.0);
295 }
296
297
298
299
300
301 /*  ========================================================================  */
302 /*              static variables                                              */
303 /*  ========================================================================  */
304
305
306
307 static  int                                             gr;   /* the current granule */
308 static  int                                             ch;   /* the current channel */
309
310
311
312 static  III_side_info_t                 *side_info;   /* the current side information */
313 static  gr_info                                 *cod_info;    /* the current coding information */
314
315
316
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 */
320
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 */
327
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];
331
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];
337
338
339
340 static  int                                             max_used_sfb_l;
341 static  int                                             min_used_sfb_s;
342
343 static  int                                             end_sfb_l;
344 static  int                                             end_sfb_s;
345
346
347
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 */
350
351 static  double                              xmax_s[SFB_SMAX][3];        /* Guess ... */
352 static  int                                xmax_line_s[SFB_SMAX][3];
353
354
355
356 static  int                                             mark_idx_l;                             /* speed up - partial quantizing */
357 static  int                                             mark_tab_l[SFB_LMAX];   /* changed sfb-s                 */ 
358
359 static  int                                             mark_idx_s;
360 static  int                                             mark_tab_s[SFB_SMAX*3*2];       /* changed (sfb,b)-s         */
361
362
363
364 #if !ORG_QUANTANF_INIT
365
366 static  int                                             lo_quant_l [SFB_LMAX];
367 static  int                                             hi_quant_l [SBMAX_l];
368
369 static  int                                             lo_quant_s [SFB_SMAX][3];
370 static  int                                             hi_quant_s [SFB_SMAX][3];
371
372 static  int                                             the_lo_quant;
373 static  int                                             the_hi_quant;
374
375 static  double                                  log_2, cc, dd;
376
377 #endif
378
379
380
381
382
383 /*  ========================================================================  */
384 /*              iteration_loop                                                */
385 /*  ========================================================================  */
386
387 void                                    iteration_loop
388 (
389         double                                  pe[][2],
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],
394         int                                             mean_bits,
395         int                                             stereo,
396         double                                  xr_dec[2][2][576],
397         III_scalefac_t                  *scalefac,
398         frame_params                    *fr_ps,
399         int                                             ancillary_pad,
400         int                                             bitsPerFrame
401 )
402 {
403         int                                             max_bits;
404         int                                             i, sfb, b, scfsi_band;
405         int                                             mode_gr;
406
407         int                                             *main_data_begin;
408         layer                                   *info;
409
410         int                                             start, end;
411
412 #if ORG_QUANTANF_INIT
413         double                                  log_sum;
414 #endif
415         double                                  total_energy, temp, x;
416
417
418
419         side_info = l3_side;
420
421         main_data_begin = &side_info->main_data_begin;
422         info = fr_ps->header;
423
424
425         side_info->resvDrain = 0;
426
427         if (!fInit_iteration_loop)
428         {
429                 *main_data_begin = 0;
430                 fInit_iteration_loop = 1;
431
432 #if !ORG_QUANTANF_INIT
433                 log_2 = log(2.0);
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;
436 #endif
437         }
438         mode_gr = 2;
439
440         scalefac_band_long  = &sfBandIndex[info->sampling_frequency].l[0];
441         scalefac_band_short = &sfBandIndex[info->sampling_frequency].s[0];
442
443
444         ResvFrameBegin (fr_ps, side_info, mean_bits, bitsPerFrame);
445
446         for (gr = 0;  gr < mode_gr;  gr++)
447         {
448                 for (ch = 0;  ch < stereo;  ch++)
449                 {
450                         xr_org_l = xr_org[gr][ch];
451                         xr_org_s = (double (*)[3]) xr_org_l;
452
453                         ix_l = l3_enc[gr][ch];
454                         ix_s = (int (*)[3]) ix_l;
455
456                         cod_info = &side_info->gr[gr].ch[ch].tt;
457
458                         scalefac_l = scalefac->l[gr][ch];  scalefac_0_l = scalefac->l[0][ch];
459                         scalefac_s = scalefac->s[gr][ch];
460
461
462
463                         /* reset of iteration variables */
464
465                         for (scfsi_band = 0;  scfsi_band < 4;  scfsi_band++)
466                                 cod_info->slen[scfsi_band] = 0;
467
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;
486
487
488
489 /*  ========    gr_deco    ========  */
490
491         if (cod_info->window_switching_flag && (cod_info->block_type == SHORT_TYPE))
492         {
493                 if (cod_info->mixed_block_flag)
494                 {
495                         /*
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.
498                         */
499                         max_used_sfb_l = cod_info->sfb_lmax = 8;
500                         min_used_sfb_s = cod_info->sfb_smax = 3;
501
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) */
505                 }
506                 else
507                 {
508                         max_used_sfb_l = cod_info->sfb_lmax = 0;  /* No long blocks */
509                         min_used_sfb_s = cod_info->sfb_smax = 0;
510
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 */
516                 }
517
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;
521
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; */
525
526                 cod_info->count1table_select = 1;   /* sum0 == sum1 == 0 */
527
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;
531         }
532         else
533         {
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 */
536
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;*/
540         }
541
542
543                         /* reset of iteration variables */
544
545                         for (sfb = 0;  sfb < max_used_sfb_l/*SFB_LMAX-1*/;  sfb++)
546                                 scalefac_l[sfb] = 0;
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;
550
551 /*  ========    calc_xmin and start of quantanf_init    ========  */
552 /*
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)
556 */
557
558 #if ORG_QUANTANF_INIT
559         log_sum = 0.0;
560 #endif
561         total_energy = 0.0;
562
563         for (sfb = 0;  sfb < end_sfb_l;  sfb++)
564         {
565                 start = scalefac_band_long[sfb];
566                 end   = scalefac_band_long[sfb+1];
567
568                 expo16_l[sfb] = 0;
569
570                 xmax_l[sfb] = 0.0;
571                 xmax_line_l[sfb] = start;
572
573                 temp = 0.0;
574 #if !ORG_HIGHEST_SFB
575                 if (sfb < max_used_sfb_l)
576                 {
577 #endif
578                         for (i = start;  i < end;  i++)
579                         {
580                                 if ((x = fabs(xr_org_l[i])) != 0.0)
581                                 {
582                                         xr34_l[i] = sqrt(x * sqrt(x));
583                                         temp += x*x;
584 #       if ORG_QUANTANF_INIT
585                                         log_sum += log(x);
586 #       endif
587                                         if (x > xmax_l[sfb])
588                                         {
589                                                 xmax_l[sfb] = x;
590                                                 xmax_line_l[sfb] = i;
591                                         }
592                                 }
593                                 else
594                                         xr34_l[i] = 0.0;
595                         }
596 #if !ORG_HIGHEST_SFB
597                 }
598                 else   /* cut off the (highest frequency) entries in the unused scale factor band */
599                 {
600                         for (i = start;  i < end;  i++)
601                                 xr34_l[i] = 0.0;
602                 }
603 #endif
604                 total_energy += energy_l[sfb] = temp;
605
606                 if (sfb < max_used_sfb_l)
607                         xmin_l[sfb] = ratio->l[gr][ch][sfb] * temp;
608         }
609
610         for (sfb = min_used_sfb_s;  sfb < end_sfb_s;  sfb++)
611         {
612                 start = scalefac_band_short[sfb];
613                 end   = scalefac_band_short[sfb+1];
614
615                 for (b = 0;  b < 3;  b++)
616                 {
617                         expo16_s[sfb][b] = 0;
618
619                         xmax_s[sfb][b] = 0.0;
620                         xmax_line_s[sfb][b] = start;
621
622                         temp = 0.0;
623 #if !ORG_HIGHEST_SFB
624                         if (sfb < SBMAX_s)
625                         {
626 #endif
627                                 for (i = start;  i < end;  i++)
628                                 {
629                                         if ((x = fabs(xr_org_s[i][b])) != 0.0)
630                                         {
631                                                 xr34_s[i][b] = sqrt(x * sqrt(x));
632                                                 temp += x*x;
633 #if ORG_QUANTANF_INIT
634                                                 log_sum += log(x);
635 #endif
636                                                 if (x > xmax_s[sfb][b])
637                                                 {
638                                                         xmax_s[sfb][b] = x;
639                                                         xmax_line_s[sfb][b] = i;
640                                                 }
641                                         }
642                                         else
643                                                 xr34_s[i][b] = 0.0;
644                                 }
645 #if !ORG_HIGHEST_SFB
646                         }
647                         else   /* cut off the (highest frequency) entries in the unused scale factor band */
648                         {
649                                 for (i = start;  i < end;  i++)
650                                         xr34_s[i][b] = 0.0;
651                         }
652 #endif
653                         total_energy += energy_s[sfb][b] = temp;
654
655                         if (sfb < SFB_SMAX-1)
656                                 xmin_s[sfb][b] = ratio->s[gr][ch][sfb][b] * temp;
657                 }
658         }
659
660
661 /*  ========    calc_scfsi    ========  */
662
663         /* None of the granules contains short blocks */
664         if (!cod_info->window_switching_flag || (cod_info->block_type != SHORT_TYPE))
665         {
666                 if (gr == 1)
667                 {
668                         for (scfsi_band = 0;  scfsi_band < 4;  scfsi_band++)
669                                 side_info->scfsi[ch][scfsi_band] = 0;
670                 }
671         }
672
673
674
675                         /* calculation of number of available bit( per granule ) */
676                         max_bits = ResvMaxBits (fr_ps, side_info, &pe[gr][ch], mean_bits);
677
678
679
680                         /* all spectral values zero ? */
681                         if (total_energy != 0.0)
682                         {
683
684
685
686 #if ORG_QUANTANF_INIT
687
688 /*  ========    quantanf_init (remaining)    ========  */
689
690 #define system_const                       8.0
691 #define minlimit                                -100.0
692
693         temp = my_nint (system_const * (log_sum/288.0 - log(total_energy/576.0)));
694         if (temp < minlimit)
695                 temp = minlimit;
696         /*
697                 SS 19-12-96. Starting value of
698                 global_gain or quantizerStepSize 
699                 has to be reduced for iteration_loop
700         */
701         temp -= 70.0;
702
703                                 cod_info->quantizerStepSize = temp;
704
705 #else   /* ORG_QUANTANF_INIT */
706
707         double                                  xmax, the_xmax;
708
709         the_lo_quant = -infinity;   /* "-infinity" */
710         the_hi_quant = -infinity;   /* the real maximum for high_quant is about +4 ! */
711
712         the_xmax = -1.0;
713
714         for (sfb = 0;  sfb < end_sfb_l;  sfb++)
715         {
716                 xmax = xmax_l[sfb];
717                 if (xmax == 0.0)
718                 {
719                         lo_quant_l[sfb] = -infinity;
720                         hi_quant_l[sfb] = -infinity;
721                 }
722                 else
723                 {
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;
726
727                         if (xmax > the_xmax)
728                         {
729                                 the_xmax = xmax;
730                                 the_lo_quant = lo_quant_l[sfb];
731                                 the_hi_quant = hi_quant_l[sfb];
732                         }
733                 }
734         }
735
736         for (sfb = min_used_sfb_s;  sfb < end_sfb_s;  sfb++)
737         {
738                 for (b = 0;  b < 3;  b++)
739                 {
740                         xmax = xmax_s[sfb][b];
741                         if (xmax == 0.0)
742                         {
743                                 lo_quant_s[sfb][b] = -infinity;
744                                 hi_quant_s[sfb][b] = -infinity;
745                         }
746                         else
747                         {
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;
750
751                                 if (xmax > the_xmax)
752                                 {
753                                         the_xmax = xmax;
754                                         the_lo_quant = lo_quant_s[sfb][b];
755                                         the_hi_quant = hi_quant_s[sfb][b];
756                                 }
757                         }
758                 }               
759         }
760
761
762         /*
763                 Try the power table at its least boundary
764                 I«ve never reached this deep before!
765         */
766         assert (the_lo_quant > -POW216_MAX);
767
768         cod_info->quantizerStepSize = the_lo_quant;
769
770 #endif   /* ORG_QUANTANF_INIT */
771
772
773
774                                 cod_info->part2_3_length = outer_loop (max_bits, fr_ps);
775                         }
776
777                         ResvAdjust (fr_ps, cod_info, side_info, mean_bits);
778
779                         cod_info->global_gain = my_nint (cod_info->quantizerStepSize + 210.0);
780 /*                      assert (cod_info->global_gain < 256); */
781                 }       /* for ch */
782         }       /* for gr */
783
784         ResvFrameEnd (fr_ps, side_info, mean_bits);
785 }
786
787
788
789
790
791 /*  ========================================================================  */
792 /*              outer_loop                                                    */
793 /*  ========================================================================  */
794 /*
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
798 */
799
800 static  int                             outer_loop
801 (
802         int                                             max_bits,
803         frame_params                    *fr_ps
804 )
805 {
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;
810
811
812         /* reset the pointers of our changed sfb [(sfb,b)] indices list */
813         mark_idx_l = mark_idx_s = 0;
814
815
816 #if 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 */
820 #endif
821
822         huff_bits = max_bits /* - cod_info->part2_length */;   /* try first without scaling */
823
824
825         iteration = 1;
826
827         bits = bin_search_StepSize (max_bits, cod_info->quantizerStepSize);  /* speeds things up a bit */
828
829         while (1)
830         {
831                 for (sfb = 0;  sfb < SFB_LMAX-1;  sfb++)  /* save scaling factors */
832                         scalesave_l[sfb] = scalefac_l[sfb];
833
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];
837
838                 save_preflag      = cod_info->preflag;
839                 save_compress     = cod_info->scalefac_compress;
840                 save_part2_length = cod_info->part2_length;
841
842                 calc_noise ();  /* distortion calculation */
843
844                 over = amplify (iteration);
845
846 #if ORG_OUTER_LOOP
847 /*
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.
859 */
860                 if (loop_break ())
861                         break;
862 #endif
863
864                 huff_bits = max_bits - needed_bits_for_storing_scalefactors (fr_ps);
865                 if (huff_bits < 0)
866                         break;   /* not enough space to store the scale factors */
867
868
869                 /* We have to wait checking this break condition */
870                 /* until needed_bits_for_storing_scalefactors()  */
871                 /* set the scalefac compress index!!!            */
872                 if (over == 0)
873                         break;   /* no more bands to amplify */
874
875
876                 iteration++;
877
878                 /*
879                         Most of the times, only a few bands will be changed,
880                         so why quantize the whole area?
881                 */
882                 partial_quantize ();
883 #if ORG_BINARY_SEARCH || ORG_QUANTANF_INIT || CHECK_TJ_OVERFLOW
884 assert (!tjBitOverflow2);
885 #endif
886                 bits = count_bits();
887
888                 while (bits > huff_bits)
889                 {
890                         cod_info->quantizerStepSize += 1.0;
891                         quantize ();
892 #if ORG_BINARY_SEARCH || ORG_QUANTANF_INIT || CHECK_TJ_OVERFLOW
893 assert (!tjBitOverflow2);
894 #endif
895                         bits = count_bits ();
896                 }
897
898 #if !ORG_OUTER_LOOP
899                 /*
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().
904                 */
905                 if (loop_break())
906                         goto take_that_and_party;
907 #endif
908         }
909
910
911         cod_info->preflag           = save_preflag;
912         cod_info->scalefac_compress = save_compress;
913         cod_info->part2_length      = save_part2_length;
914
915         for (sfb = 0;  sfb < SFB_LMAX-1;  sfb++)
916                 scalefac_l[sfb] = scalesave_l[sfb];    
917
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];
921
922 take_that_and_party:
923         cod_info->part2_3_length    = cod_info->part2_length + bits;
924
925
926         return cod_info->part2_3_length;
927 }
928
929
930
931
932
933 /*  ========================================================================================  */
934 /*      needed_bits_for_storing_scalefactors                                                  */
935 /*  ========================================================================================  */
936 /*
937         counts the bits needed to code the scale factors (cod_info->part2_length)
938         and the compression index (cod_info->scalefac_compress).
939
940         If there is no suitable index, it returns "infinity".
941 */
942
943 static  int                             needed_bits_for_storing_scalefactors
944 (
945         frame_params                    *fr_ps
946 )
947 {
948 #if 0
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 };
951 #endif
952
953         /* 2^^slen1[k] */
954         static  int                              pow2_slen1[16] = {  1,  1,  1,  1,  8,  2,  2,  2,  4,  4,  4,  8,  8,  8, 16, 16};
955         /* 2^^slen2[k] */
956         static  int                              pow2_slen2[16] = {  1,  2,  4,  8,  1,  2,  4,  8,  2,  4,  8,  2,  4,  8,  4,  8};
957
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};
964
965         int                                             sfb, b, k;
966         int                                             max_slen1, max_slen2, *table;
967
968
969         max_slen1 = max_slen2 = 0;
970
971         if (cod_info->window_switching_flag  &&  (cod_info->block_type == SHORT_TYPE))
972         {
973                 if (cod_info->mixed_block_flag)
974                 {
975                         table = part2_len_m;
976
977                         for (sfb = 0;  sfb < 8;  sfb++)
978                                 if (scalefac_l[sfb] > max_slen1)
979                                         max_slen1 = scalefac_l[sfb];
980
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];
985                 }
986                 else
987                 {
988                         table = part2_len_s;
989
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];
994                 }
995
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];
1000         }
1001         else
1002         {
1003                 table = part2_len_l;
1004
1005                 for (sfb = 0;  sfb < 11;  sfb++)
1006                         if (scalefac_l[sfb] > max_slen1)
1007                                 max_slen1 = scalefac_l[sfb];
1008
1009
1010 #if ORG_SCF_COMPRESS && !ORG_PREEMPHASING
1011                 /* This was seen in LAME */
1012                 if (!cod_info->preflag)
1013                 {
1014                         for (sfb = 11;  sfb < SBMAX_l;  sfb++)
1015                                 if (scalefac_l[sfb] < (1 + cod_info->scalefac_scale) * pretab[sfb])
1016                                         break;
1017
1018                         if (sfb == SBMAX_l)
1019                         {
1020                                 for (sfb = 11;  sfb < SBMAX_l;  sfb++)
1021                                         scalefac_l[sfb] -= (1 + cod_info->scalefac_scale) * pretab[sfb];
1022                                 cod_info->preflag = 1;
1023                         }
1024                 }
1025 #endif
1026
1027
1028                 for (sfb = 11;  sfb < 21/*SBMAX_l*/;  sfb++)
1029                         if (scalefac_l[sfb] > max_slen2)
1030                                 max_slen2 = scalefac_l[sfb];
1031         }
1032
1033
1034         cod_info->part2_length = infinity;
1035
1036         for (k = 0;  k < 16;  k++)
1037         {
1038                 if (max_slen1 < pow2_slen1[k]  &&  max_slen2 < pow2_slen2[k])
1039                 {
1040 #if ORG_SCF_COMPRESS
1041                         cod_info->scalefac_compress = k;
1042                         cod_info->part2_length      = table[k];
1043                         break;
1044 #else
1045                         if (cod_info->part2_length > table[k])
1046                         {
1047                                 cod_info->scalefac_compress = k;
1048                                 cod_info->part2_length      = table[k];
1049                         }
1050 #endif
1051                 }
1052         }
1053
1054
1055         return cod_info->part2_length;
1056 }
1057
1058
1059
1060
1061
1062 /*  ========================================================================================  */
1063 /*              calc_noise                                                                    */
1064 /*  ========================================================================================  */
1065 /*
1066         calculates the distortion introduced by the qunatization
1067         in each scale factor band.
1068 */
1069 static  void                    calc_noise (void)
1070 {
1071         int                                             i, b, sfb, start, end, off;
1072         double                                  f, sum, temp;
1073
1074
1075         off = -4 * (int)cod_info->quantizerStepSize;
1076
1077
1078         for (sfb = 0;  sfb < max_used_sfb_l;  sfb++)
1079         {
1080                 if (ix_l[xmax_line_l[sfb]] == 0)   /* quantized values all zero? */
1081                 {
1082                         xfsf_l[sfb] = energy_l[sfb];   /* see calculation of xmin_l */
1083                 }
1084                 else
1085                 {
1086                         start = scalefac_band_long[sfb];
1087                         end   = scalefac_band_long[sfb+1];
1088
1089                         sum = 0.0;
1090
1091                         f = pow216[expo16_l[sfb] + off];
1092
1093                         for (i = start;  i < end;  i++)
1094                         {
1095                                 temp = fabs(xr_org_l[i]) - noisePowTab[ix_l[i]] / f;
1096                                 sum += temp * temp;
1097                         }
1098
1099                         xfsf_l[sfb] = sum;
1100                 }
1101         }
1102
1103         for (b = 0;  b < 3;  b++)
1104         {
1105                 off = -4 * ((int)cod_info->quantizerStepSize + 8 * cod_info->subblock_gain[b]);
1106
1107                 for (sfb = min_used_sfb_s;  sfb < SFB_SMAX-1;  sfb++)
1108                 {
1109                         if (ix_s[xmax_line_s[sfb][b]] == 0)   /* quantized values all zero? */
1110                         {
1111                                 xfsf_s[sfb][b] = energy_s[sfb][b];   /* see calculation of xmin_s */
1112                         }
1113                         else
1114                         {
1115                                 start = scalefac_band_short[sfb];
1116                                 end   = scalefac_band_short[sfb+1];
1117
1118                                 sum = 0.0;
1119
1120                                 f = pow216[expo16_s[sfb][b] + off];
1121
1122                                 for (i = start;  i < end;  i++)
1123                                 {
1124                                         temp = fabs(xr_org_s[i][b]) - noisePowTab[ix_s[i][b]] / f;
1125                                         sum += temp * temp;
1126                                 }       
1127
1128                                 xfsf_s[sfb][b] = sum;
1129                         }
1130                 }
1131         }
1132 }
1133
1134
1135
1136
1137
1138 /*  ========================================================================================  */
1139 /*              loop_break                                                                    */
1140 /*  ========================================================================================  */
1141 /*
1142         returns zero if there is a scalefac which has not been amplified.
1143         Otherwise it returns one. 
1144 */
1145
1146 static  int                             loop_break (void)
1147 {
1148         int                                             sfb, b;
1149
1150         for (sfb = 0;  sfb < cod_info->sfb_lmax;  sfb++)
1151                 if (scalefac_l[sfb] == 0)
1152                         return 0;
1153
1154         for (sfb = min_used_sfb_s;  sfb < 12;  sfb++)
1155                 for (b = 0;  b < 3;  b++)
1156                         if (scalefac_s[sfb][b] == 0)
1157                                 return 0;
1158
1159         return 1;
1160 }
1161
1162
1163
1164
1165
1166 /*  ========================================================================================  */
1167 /*              preemphasing and amplifying                                                   */
1168 /*  ========================================================================================  */
1169 /*
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
1172
1173         amplifying the scalefactor bands that violate the masking threshold.
1174 */
1175
1176 static  int                             amplify
1177 (
1178         int                                             iteration
1179 )
1180 {
1181         if (cod_info->window_switching_flag  &&  cod_info->block_type == SHORT_TYPE)
1182                 return amplify_short ();
1183         else
1184                 return amplify_long (iteration);
1185 }
1186
1187
1188
1189
1190
1191 static  int                             amplify_short (void)
1192 {
1193         int                                             sfb, b, over, expo16_off;
1194
1195         expo16_off = 16 * (1 + cod_info->scalefac_scale) / 2;
1196         over = 0;
1197
1198 #ifdef  MIXED_BLOCKS
1199         for (sfb = 0;  sfb < max_used_sfb_l;  sfb++)
1200         {
1201                 if (xfsf_l[sfb] > xmin_l[sfb])
1202                 {
1203                         scalefac_l[sfb]++;
1204                         expo16_l[sfb] += expo16_off;
1205                         over++;
1206                         mark_tab_l[mark_idx_l++] = sfb;
1207                 }
1208         }
1209 #endif
1210
1211         for (sfb = min_used_sfb_s;  sfb < SBMAX_s;  sfb++)
1212         {
1213                 for (b = 0;  b < 3;  b++)
1214                 {
1215                         if (xfsf_s[sfb][b] > xmin_s[sfb][b])
1216                         {
1217                                 scalefac_s[sfb][b]++;
1218                                 expo16_s[sfb][b] += expo16_off;
1219                                 over++;
1220                                 mark_tab_s[mark_idx_s++] = sfb;
1221                                 mark_tab_s[mark_idx_s++] = b;
1222                         }
1223                 }
1224         }
1225
1226         return over;
1227 }
1228
1229
1230
1231
1232
1233 static  int                             amplify_long
1234 (
1235         int                                             iteration
1236 )
1237 {
1238         int                                             pre_expo_off[SFB_LMAX];
1239
1240         int                                             sfb, stop_at, over = 0;
1241         int                                             expo16_off;
1242
1243
1244         stop_at = max_used_sfb_l;
1245
1246         expo16_off = 16 * (1 + cod_info->scalefac_scale) / 2;
1247
1248
1249         /*
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.
1253
1254                 Original bug of dist10 - preemphasis() didn't know 'iteration'!!!
1255         */
1256 #if !ORG_PREEMPHASING
1257         if (iteration == 1)
1258 #endif
1259                 if (!cod_info->preflag)
1260                 {       
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;
1264
1265                         cod_info->preflag = 1;
1266
1267                         stop_at = 11;   /* pretab[sfb] = 0  for  sfb = 0..10 */
1268
1269                         for (sfb = stop_at;  sfb < max_used_sfb_l;  sfb++)
1270                         {
1271                                 expo16_l[sfb] += pre_expo_off[sfb] = expo16_off * pretab[sfb];
1272
1273                                 mark_tab_l[mark_idx_l++] = sfb;
1274                         }
1275                 }
1276
1277
1278 no_preemphasing:
1279
1280
1281         for (sfb = 0;  sfb < stop_at;  sfb++)
1282         {
1283                 if (xfsf_l[sfb] > xmin_l[sfb])
1284                 {
1285                         over++;
1286                         expo16_l[sfb] += expo16_off;
1287                         scalefac_l[sfb]++;
1288
1289                         mark_tab_l[mark_idx_l++] = sfb;
1290                 }
1291         }
1292         for (sfb = stop_at;  sfb < max_used_sfb_l;  sfb++)   /* The just preemphased bands have to be treated differently */
1293         {
1294                 if (xfsf_l[sfb] > xmin_l[sfb] * pow216[2*pre_expo_off[sfb]])
1295                 {
1296                         over++;
1297                         expo16_l[sfb] += expo16_off;
1298                         scalefac_l[sfb]++;
1299                 }
1300         }
1301
1302
1303         return over;
1304 }
1305
1306
1307
1308
1309
1310 /*  ========================================================================  */
1311 /*              quantize                                                      */
1312 /*  ========================================================================  */
1313 /*
1314         Quantization of the vector xr ( -> ix)
1315 */
1316
1317 static  int INLINE              cutting_crew (FLOAT in)
1318 {
1319         int                                             retVal;
1320
1321         retVal = (int) (in + 0.4054);
1322
1323 #if ORG_BINARY_SEARCH || ORG_QUANTANF_INIT || CHECK_TJ_OVERFLOW
1324         if (retVal > 8191+14)
1325                 tjBitOverflow2 = TRUE;
1326 #endif
1327
1328         return retVal;
1329 }
1330
1331
1332
1333 static  void quantize (void)
1334 {
1335         int                                             sfb, i, b, start, end;
1336         double                                  f, z, y;
1337
1338         for (sfb = 0;  sfb < end_sfb_l;  sfb++)
1339         {
1340                 start = scalefac_band_long[sfb];
1341                 end   = scalefac_band_long[sfb+1];
1342
1343                                 /* (expo16_l[sfb] - 16/4 * quant_step) * 3/4 */
1344                         f = pow216[(expo16_l[sfb]/4 - (int)cod_info->quantizerStepSize) * 3];
1345
1346                         for (i = start;  i < end;  i += 2)
1347                         {
1348                                 z = xr34_l[i  ] * f;
1349                                 y = xr34_l[i+1] * f;
1350                                 ix_l[i  ] = cutting_crew (z);
1351                                 ix_l[i+1] = cutting_crew (y);
1352                         }
1353         }
1354
1355         for (sfb = min_used_sfb_s;  sfb < end_sfb_s;  sfb++)
1356         {
1357                 start = scalefac_band_short[sfb];
1358                 end   = scalefac_band_short[sfb+1];
1359
1360                 for (b = 0;  b < 3;  b++)
1361                 {
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];
1364
1365                                 for (i = start;  i < end;  i += 2)
1366                                 {
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);
1371                                 }
1372                 }
1373         }
1374 }
1375
1376
1377
1378 static  void                    partial_quantize (void)
1379 {
1380         int                                             sfb, i, b, start, end;
1381         double                                  f, z, y;
1382
1383         while (mark_idx_l)
1384         {
1385                 sfb = mark_tab_l[--mark_idx_l];
1386
1387                 start = scalefac_band_long[sfb];
1388                 end   = scalefac_band_long[sfb+1];
1389
1390                         /* (expo16_l[sfb] - 16/4 * quant_step) * 3/4 */
1391                 f = pow216[(expo16_l[sfb]/4 - (int)cod_info->quantizerStepSize) * 3];
1392
1393                 for (i = start;  i < end;  i += 2)
1394                 {
1395                         z = xr34_l[i  ] * f;
1396                         y = xr34_l[i+1] * f;
1397                         ix_l[i  ] = cutting_crew (z);
1398                         ix_l[i+1] = cutting_crew (y);
1399                 }
1400         }
1401
1402         while (mark_idx_s)
1403         {
1404                 b   = mark_tab_s[--mark_idx_s];
1405                 sfb = mark_tab_s[--mark_idx_s];
1406
1407                 start = scalefac_band_short[sfb];
1408                 end   = scalefac_band_short[sfb+1];
1409
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];
1412
1413                 for (i = start;  i < end;  i += 2)
1414                 {
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);
1419                 }
1420         }
1421 }
1422
1423
1424
1425
1426
1427 /*  ========================================================================  */
1428 /*              count_bits                                                    */
1429 /*  ========================================================================  */
1430
1431 struct
1432 {
1433         unsigned region0_count;
1434         unsigned region1_count;
1435 } subdv_table[ 23 ] =
1436 {
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 */
1460 };
1461
1462
1463
1464 /*
1465         Calculation of rzero, count1, big_values
1466         (Partitions ix into big values, quadruples and zeros).
1467
1468         Determines the number of bits to encode the quadruples.
1469
1470         Presumable subdivides the bigvalue region which will
1471         use separate Huffman tables.
1472
1473         Select huffman code tables for bigvalues regions
1474
1475         Count the number of bits necessary to code the bigvalues region.
1476 */
1477
1478 static  int                             count_bits (void)
1479 {
1480         cod_info->table_select[0] = 0;
1481         cod_info->table_select[1] = 0;
1482         cod_info->table_select[2] = 0;
1483
1484         if (cod_info->window_switching_flag && (cod_info->block_type == SHORT_TYPE))
1485                 return count_bits_short ();
1486         else
1487                 return count_bits_long ();
1488 }
1489
1490
1491
1492
1493
1494 static  int                             count_bits_short (void)
1495 {
1496         unsigned int                    bits = 0;
1497
1498         /*
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...
1503         */
1504         int                                             sfb, b;
1505         unsigned int                    max, temp;
1506
1507
1508         /*
1509                 the first part --- 8 long blocks or 3 short blocks
1510         */
1511
1512         max = 0;
1513 #ifdef MIXED_BLOCKS
1514         if (cod_info->mixed_block_flag)
1515         {
1516                 for (sfb = 0;  sfb < 8;  sfb++)
1517                         if ((temp = ix_l[xmax_line_l[sfb]]) > max)
1518                                 max = temp;
1519                 choose_table_long (0, 36, max, &cod_info->table_select[0], &bits);
1520         }
1521         else
1522 #endif
1523         {
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)
1527                                         max = temp;
1528                 choose_table_short (0, 3, max, &cod_info->table_select[0], &bits);
1529         }
1530
1531
1532         /*
1533                 the second part --- short blocks only
1534         */
1535
1536         max = 0;
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)
1540                                 max = temp;
1541         choose_table_short (3, SFB_SMAX, max, &cod_info->table_select[1], &bits);
1542
1543         return bits;
1544 }
1545
1546
1547
1548
1549
1550 static  int                             count_bits_long (void)
1551 {
1552         int                                             zero_region;
1553         int                                             bigv_region;
1554
1555         unsigned                                bits = 0;
1556         int                                             sum0 = 0;
1557         int                                             sum1 = 0;
1558
1559         int                                             sfb_anz, index0, index1, sfb, i;
1560         unsigned                                max, temp;
1561
1562         int                                             p;
1563
1564
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;
1568
1569         for (bigv_region = zero_region;  bigv_region > 3;  bigv_region -= 4)
1570         {
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;
1575
1576                 p = 0;
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;
1581
1582                 sum0 += ht[32].hlen[p];
1583                 sum1 += ht[33].hlen[p];
1584         }
1585
1586         cod_info->count1     = (zero_region-bigv_region) / 4;
1587         cod_info->big_values =              bigv_region  / 2;
1588
1589         if (sum0 < sum1)
1590         {
1591                 bits += sum0;
1592                 cod_info->count1table_select = 0;
1593         }
1594         else
1595         {
1596                 bits += sum1;
1597                 cod_info->count1table_select = 1;
1598         }
1599
1600         if (bigv_region)
1601         {
1602                 sfb_anz = 1;
1603                 while (scalefac_band_long[sfb_anz] < bigv_region)
1604                         sfb_anz++;
1605
1606                 if (cod_info->window_switching_flag)   /* START_TYPE, STOP_TYPE */
1607                 {
1608                         index0 = (cod_info->region0_count =  7) + 1;
1609                                   cod_info->region1_count = 13;
1610                         index1 = sfb_anz - index0;  if (index0 + index1 < 22)  index1++;
1611
1612                         cod_info->address1 =  36;
1613                         cod_info->address2 = bigv_region;
1614                         cod_info->address3 =   0;
1615                 }
1616                 else   /* NORM_TYPE */  
1617                 {
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;
1620
1621                         cod_info->address1 = scalefac_band_long[index0];
1622                         cod_info->address2 = scalefac_band_long[index0 + index1];
1623                         cod_info->address3 = bigv_region;
1624                 }
1625
1626                 if (cod_info->address1 > 0)
1627                 {
1628                         max = 0;
1629                         for (sfb = 0;  sfb < index0;  sfb++)
1630                                 if ((temp = ix_l[xmax_line_l[sfb]]) > max)
1631                                         max = temp;
1632                         choose_table_long (0, cod_info->address1, max, &cod_info->table_select[0], &bits);
1633                 }
1634
1635                 if (cod_info->address2 > cod_info->address1)
1636                 {
1637                         max = 0;
1638                         for (sfb = index0;  sfb < index0+index1;  sfb++)
1639                                 if ((temp = ix_l[xmax_line_l[sfb]]) > max)
1640                                         max = temp;
1641                         choose_table_long (cod_info->address1, cod_info->address2, max, &cod_info->table_select[1], &bits);
1642                 }
1643
1644                 if (bigv_region > cod_info->address2)
1645                 {
1646                         max = 0;
1647                         for (sfb = index0+index1;  sfb < sfb_anz-1;  sfb++)
1648                                 if ((temp = ix_l[xmax_line_l[sfb]]) > max)
1649                                         max = temp;
1650                         for (i = scalefac_band_long[sfb_anz-1]; i < bigv_region;  i++)
1651                                 if ((temp = ix_l[i]) > max)
1652                                         max = temp;
1653                         choose_table_long (cod_info->address2, bigv_region, max, &cod_info->table_select[2], &bits);
1654                 }
1655         }
1656         else
1657         {       /* no big_values region */
1658                 cod_info->region0_count = 0;
1659                 cod_info->region1_count = 0;
1660
1661                 cod_info->address1 = 0;
1662                 cod_info->address2 = 0;
1663                 cod_info->address3 = 0;
1664         }
1665
1666
1667         return bits;
1668 }
1669
1670
1671
1672
1673
1674 /*  ========================================================================  */
1675 /*              bin_search_step_size                                          */
1676 /*  ========================================================================  */
1677 /*
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.
1684
1685         The function count_bits() [a sequence of statements, originally part of inner_loop()]
1686         was completely rewritten.
1687
1688
1689         changed the behaviour:
1690         now, it returns the found number of bits <= desired_rate
1691 */
1692
1693 static  int                             bin_search_StepSize
1694 (
1695         int                                             desired_rate,
1696         double                                  start
1697 )
1698 {
1699         int                                             bits;
1700
1701         int                                             top = start;
1702 #if ORG_BINARY_SEARCH || ORG_QUANTANF_INIT
1703         int                                             bot = 200;
1704 #else
1705         int                                             bot = the_hi_quant;
1706 #endif
1707         int                                             next = start;
1708
1709 #if ORG_BINARY_SEARCH
1710
1711         int                                             last;
1712
1713         do
1714         {
1715                 last = next;
1716                 next = (top + bot) / 2;
1717                 cod_info->quantizerStepSize = next;
1718
1719                 tjBitOverflow2 = FALSE;
1720                 quantize ();
1721                 if (tjBitOverflow2)
1722                         bits = infinity;
1723                 else
1724                         bits = count_bits ();
1725
1726                 if (bits > desired_rate) 
1727                         top = next;
1728                 else 
1729                         bot = next;
1730         }
1731         while ((bits != desired_rate)  &&  (abs(last-next) > 1));
1732
1733 #else   /* ORG_BINARY_SEARCH */
1734
1735         do
1736         {
1737                 next = top + (bot - top) / 2;
1738                 cod_info->quantizerStepSize = next;
1739
1740 #if ORG_BINARY_SEARCH || ORG_QUANTANF_INIT || CHECK_TJ_OVERFLOW
1741                 tjBitOverflow2 = FALSE;
1742                 quantize ();
1743                 if (tjBitOverflow2)
1744                         bits = infinity;
1745                 else
1746                         bits = count_bits ();
1747 #else
1748                 quantize ();
1749                 bits = count_bits ();
1750 #endif
1751
1752                 if (bits > desired_rate) 
1753                         top = next + 1;
1754                 else 
1755                         bot = next;
1756         }
1757         while (top < bot);
1758
1759 #endif   /* ORG_BINARY_SEARCH */
1760
1761         if (bits > desired_rate)
1762         {
1763                 cod_info->quantizerStepSize = next+1;
1764 #if ORG_BINARY_SEARCH || ORG_QUANTANF_INIT || CHECK_TJ_OVERFLOW
1765                 tjBitOverflow2 = FALSE;
1766                 quantize ();
1767 assert(! tjBitOverflow2);
1768 #else
1769                 quantize ();
1770 #endif
1771                 bits = count_bits ();
1772 assert(bits <= desired_rate);
1773         }
1774
1775         return bits;
1776
1777 }
1778
1779
1780
1781
1782
1783 /*  ========================================================================================  */
1784 /*              choose_table_long                                                             */
1785 /*  ========================================================================================  */
1786 /*
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.
1789
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.
1793 */
1794 static  void                    choose_table_long
1795 (
1796         unsigned                                start,
1797         unsigned                                end,
1798         unsigned                                max,
1799         unsigned                                *table,
1800         unsigned                                *bit_sum
1801 )
1802 {
1803         unsigned                                choice0, choice1;
1804
1805
1806         if (max == 0)
1807         {
1808                 *table = 0;
1809                 return;
1810         }
1811
1812         if (max < 15)
1813         {
1814                 choice0 = 1;  /* we can start with 1 because ht[0].xlen == 0 <= max */
1815                 while (ht[choice0].xlen <= max)
1816                         choice0++;
1817
1818                 switch (choice0)
1819                 {
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;
1826                 }
1827         }
1828 #if !ORG_HUFFMAN_CODING   /* no part of original BladeEnc */
1829         else if (max == 15)
1830         {
1831                 triple_Huffman_2 (start, end,/* 13, 15, 24, */ table, bit_sum);
1832         }
1833 #endif
1834         else
1835         {
1836                 max -= 15;
1837
1838 #if ORG_HUFFMAN_CODING
1839                 choice0 = 15;  while (ht[choice0].linmax < max)  choice0++;
1840 #else
1841                 choice0 = 16;  while (ht[choice0].linmax < max)  choice0++;
1842 #endif
1843
1844 assert(choice0 < 24);
1845                 choice1 = 24;  while (ht[choice1].linmax < max)  choice1++;
1846 assert(choice1 < 32);
1847
1848 #if ORG_HUFFMAN_CODING
1849                 double_Huffman_2 (start, end, choice1, choice0, table, bit_sum);
1850 #else
1851                 double_Huffman_2 (start, end, choice0, choice1, table, bit_sum);
1852 #endif
1853         }
1854 }
1855
1856
1857
1858
1859
1860 /*  ========================================================================================  */
1861 /*              choose_table_short                                                            */
1862 /*  ========================================================================================  */
1863 /*
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.
1866
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.
1870 */
1871
1872 static  void                    choose_table_short
1873 (
1874         unsigned                                start_sfb,
1875         unsigned                                end_sfb,
1876         unsigned                                max,
1877         unsigned                                *table,
1878         unsigned                                *bit_sum
1879 )
1880 {
1881         unsigned                                choice0;
1882 #if !ORG_HUFFMAN_CODING
1883         unsigned                                choice1;
1884 #endif
1885         int                                             start, end;
1886
1887         start = 3 * scalefac_band_short[start_sfb];
1888         end   = 3 * scalefac_band_short[  end_sfb];
1889
1890         if (max == 0)
1891         {
1892                 *table = 0;
1893                 return;
1894         }
1895
1896         if (max < 15)
1897         {
1898                 choice0 = 1;  /* we can start with 1 because ht[0].xlen == 0 <= max */
1899                 while (ht[choice0].xlen <= max)
1900                         choice0++;
1901
1902 #if ORG_HUFFMAN_CODING
1903                                   tiny_single_Huffman (start, end, choice0,    table, bit_sum);
1904 #else
1905                 switch (choice0)
1906                 {
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;
1913                 }
1914 #endif
1915         }
1916 #if !ORG_HUFFMAN_CODING   /* no part of original BladeEnc */
1917         else if (max == 15)
1918         {
1919                 tiny_triple_Huffman_2 (start, end,/* 13, 15, 24, */ table, bit_sum);
1920         }
1921 #endif
1922         else
1923         {
1924                 max -= 15;
1925
1926 #if ORG_HUFFMAN_CODING
1927
1928                 choice0 = 15;  while (ht[choice0].linmax < max)  choice0++;
1929 assert(choice0 < 24);
1930                 tiny_single_Huffman_2 (start, end, choice0, table, bit_sum);
1931
1932 #else
1933
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);
1939
1940 #endif
1941         }
1942 }
1943
1944
1945
1946
1947
1948 /*  ========================================================================================  */
1949 /*      Huffmania                                                                             */
1950 /*  ========================================================================================  */
1951
1952
1953
1954 /*
1955         That case, we don«t need to decide which is the best table.
1956 */
1957
1958 static  void                    single_Huffman
1959 (
1960         unsigned                                start,
1961         unsigned                                end,
1962 /*      unsigned                                table0, == 1 */
1963         unsigned                                *choice,
1964         unsigned                                *sum
1965 )
1966 {
1967 /*
1968         int                                             v;
1969 */
1970         unsigned                                bits0, signs, idx;
1971
1972         static  struct huffcodetab              *h0 = ht + /* table0 */ 1;   /* static because of the constant!!! */
1973
1974 #if 0   /* not needed */
1975         static  unsigned                                ylen = h0->ylen;   /* == 2 */
1976 #endif
1977
1978         int                                             *pos = ix_l + start;
1979         int                                             *fin = ix_l + end;
1980
1981         bits0 = signs = 0;
1982
1983         while (pos < fin)
1984         {
1985                 idx = 0;
1986 #if 0
1987                 v = *pos++;  if (v)  {signs++;  idx = v /* * ylen */ + v;}
1988                 v = *pos++;  if (v)  {signs++;  idx += v;}
1989 #else
1990                 if (*pos++)  {signs++;  idx = 2;}
1991                 if (*pos++)  {signs++;  idx++;}
1992 #endif
1993                 bits0 += h0->hlen[idx];
1994         }
1995
1996         *choice = /* table0 */ 1;
1997         *sum += bits0 + signs;
1998 }
1999
2000
2001
2002 #if ORG_HUFFMAN_CODING
2003 static  void                    tiny_single_Huffman
2004 (
2005         unsigned                                start,
2006         unsigned                                end,
2007         unsigned                                table0,
2008         unsigned                                *choice,
2009         unsigned                                *sum
2010 )
2011 {
2012         int                                             v0, v1, v2;
2013         unsigned                                bits0, signs, idx0, idx1, idx2;
2014
2015         struct huffcodetab              *h0 = ht + table0;
2016
2017         unsigned                                ylen = h0->ylen;
2018
2019         int                                             *pos = ix_l + start;
2020         int                                             *fin = ix_l + end;
2021
2022         bits0 = signs = 0;
2023
2024         while (pos < fin)
2025         {
2026                 idx0 = idx1 = idx2 = 0;
2027
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;}
2034
2035                 bits0 += h0->hlen[idx0] + h0->hlen[idx1] + h0->hlen[idx2];
2036         }
2037
2038         *choice = table0;
2039         *sum += bits0 + signs;
2040 }
2041 #else
2042 static  void tiny_single_Huffman
2043 (
2044         unsigned                                start,
2045         unsigned                                end,
2046 /*      unsigned                                table0 == 1 */
2047         unsigned                                *choice,
2048         unsigned                                *sum
2049 )
2050 {
2051 /*
2052         int                                             v0, v1, v2;
2053 */
2054         unsigned                                bits0, signs, idx0, idx1, idx2;
2055
2056         static  struct huffcodetab              *h0 = ht + /* table0 */ 1;   /* static because of the constant!!! */
2057
2058 #if 0   /* not needed */
2059         static  unsigned                                ylen = h0->ylen;   /* == 2 --- static because of the constant!!! */
2060 #endif
2061
2062         int                                             *pos = ix_l + start;
2063         int                                             *fin = ix_l + end;
2064
2065         bits0 = signs = 0;
2066
2067         while (pos < fin)
2068         {
2069                 idx0 = idx1 = idx2 = 0;
2070
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++;}
2077
2078                 bits0 += h0->hlen[idx0] + h0->hlen[idx1] + h0->hlen[idx2];
2079         }
2080
2081         *choice = /* table0 */ 1;
2082         *sum += bits0 + signs;
2083 }
2084 #endif
2085
2086
2087
2088 #if ORG_HUFFMAN_CODING
2089 static  void tiny_single_Huffman_2   /* Escape tables */
2090 (
2091         unsigned                                start,
2092         unsigned                                end,
2093         unsigned                                table0,   /* 15... */
2094         unsigned                                *choice,
2095         unsigned                                *sum
2096 )
2097 {
2098         int                                             v0, v1, v2;
2099         unsigned                                bits0, signs, xbits, idx0, idx1, idx2;
2100
2101         struct huffcodetab              *h0 = ht + table0;
2102
2103 #if 0   /* not needed */
2104         static  unsigned                                ylen = h0->ylen;   /* == h1->ylen == 16 --- static because of the constant!!! */
2105 #endif
2106         int                                             *pos = ix_l + start;
2107         int                                             *fin = ix_l + end;
2108
2109         bits0 = signs = xbits = 0;
2110
2111         while (pos < fin)
2112         {
2113                 idx0 = idx1 = idx2 = 0;
2114
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;}
2121
2122                 bits0 += h0->hlen[idx0] + h0->hlen[idx1] + h0->hlen[idx2];
2123         }
2124
2125         bits0 += xbits * h0->linbits;
2126
2127         *choice = table0;
2128         *sum += bits0 + signs;
2129 }
2130 #endif
2131
2132
2133
2134
2135
2136 /*
2137         The following function is called for the most maximum values below 16 (respectively 15)
2138 */
2139
2140 static  void                    double_Huffman
2141 (
2142         unsigned                                start,
2143         unsigned                                end,
2144         unsigned                                table0,   /* 2, 5, 13 */
2145         unsigned                                table1,   /* 3, 6, 15 */
2146         unsigned                                *choice,
2147         unsigned                                *sum
2148 )
2149 {
2150         int                                             v;
2151         unsigned                                bits0, bits1, signs, idx;
2152
2153         struct huffcodetab              *h0 = ht + table0;
2154         struct huffcodetab              *h1 = ht + table1;
2155
2156         unsigned                                ylen = h0->ylen;   /* == h1->ylen */
2157
2158         int                                             *pos = ix_l + start;
2159         int                                             *fin = ix_l + end;
2160
2161         bits0 = bits1 = signs = 0;
2162
2163         while (pos < fin)
2164         {
2165                 idx = 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];
2170         }
2171
2172         if (bits0 < bits1)
2173         {
2174                 *choice = table0;
2175                 *sum += bits0 + signs;
2176         }
2177         else
2178         {
2179                 *choice = table1;
2180                 *sum += bits1 + signs;
2181         }
2182 }
2183
2184
2185
2186 static  void                    tiny_double_Huffman
2187 (
2188         unsigned                                start,
2189         unsigned                                end,
2190         unsigned                                table0,   /* 2, 5, 13 */
2191         unsigned                                table1,   /* 3, 6, 15 */
2192         unsigned                                *choice,
2193         unsigned                                *sum
2194 )
2195 {
2196         int                                             v0, v1, v2;
2197         unsigned                                bits0, bits1, signs, idx0, idx1, idx2;
2198
2199         struct huffcodetab              *h0 = ht + table0;
2200         struct huffcodetab              *h1 = ht + table1;
2201
2202         unsigned                                ylen = h0->ylen;   /* == h1->ylen */
2203
2204         int                                             *pos = ix_l + start;
2205         int                                             *fin = ix_l + end;
2206
2207         bits0 = bits1 = signs = 0;
2208
2209         while (pos < fin)
2210         {
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];
2220         }
2221
2222         if (bits0 < bits1)
2223         {
2224                 *choice = table0;
2225                 *sum += bits0 + signs;
2226         }
2227         else
2228         {
2229                 *choice = table1;
2230                 *sum += bits1 + signs;
2231         }
2232 }
2233
2234
2235
2236 /*
2237         poor men«s brave tailor --- only three at a blow...
2238 */
2239
2240 static  void                    triple_Huffman
2241 (
2242         unsigned                                start,
2243         unsigned                                end,
2244         unsigned                                table0,   /* 7, 10 */
2245         unsigned                                table1,   /* 8, 11 */
2246         unsigned                                table2,   /* 9, 12 */
2247         unsigned                                *choice,
2248         unsigned                                *sum
2249 )
2250 {
2251         int                                             v;
2252         unsigned                                bits0, bits1, bits2, signs, idx;
2253
2254         struct huffcodetab              *h0 = ht + table0;
2255         struct huffcodetab              *h1 = ht + table1;
2256         struct huffcodetab              *h2 = ht + table2;
2257
2258         unsigned                                ylen = h0->ylen;   /* == h1->ylen == h2->ylen */
2259
2260         int                                             *pos = ix_l + start;
2261         int                                             *fin = ix_l + end;
2262
2263         bits0 = bits1 = bits2 = signs = 0;
2264
2265         while (pos < fin)
2266         {
2267                 idx = 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];
2273         }
2274
2275         if (bits0 < bits1  &&  bits0 < bits2)
2276         {
2277                 *choice = table0;
2278                 *sum += bits0 + signs;
2279         }
2280         else if (bits1 < bits2)
2281         {
2282                 *choice = table1;
2283                 *sum += bits1 + signs;
2284         }
2285         else
2286         {
2287                 *choice = table2;
2288                 *sum += bits2 + signs;
2289         }
2290 }
2291
2292
2293
2294 static  void                    tiny_triple_Huffman
2295 (
2296         unsigned                                start,
2297         unsigned                                end,
2298         unsigned                                table0,   /* 7, 10 */
2299         unsigned                                table1,   /* 8, 11 */
2300         unsigned                                table2,   /* 9, 12 */
2301         unsigned                                *choice,
2302         unsigned                                *sum
2303 )
2304 {
2305         int                                             v0, v1, v2;
2306         unsigned                                bits0, bits1, bits2, signs, idx0, idx1, idx2;
2307
2308         struct huffcodetab              *h0 = ht + table0;
2309         struct huffcodetab              *h1 = ht + table1;
2310         struct huffcodetab              *h2 = ht + table2;
2311
2312         unsigned                                ylen = h0->ylen;   /* == h1->ylen == h2->ylen */
2313
2314         int                                             *pos = ix_l + start;
2315         int                                             *fin = ix_l + end;
2316
2317         bits0 = bits1 = bits2 = signs = 0;
2318
2319         while (pos < fin)
2320         {
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];
2331         }
2332
2333         if (bits0 < bits1  &&  bits0 < bits2)
2334         {
2335                 *choice = table0;
2336                 *sum += bits0 + signs;
2337         }
2338         else if (bits1 < bits2)
2339         {
2340                 *choice = table1;
2341                 *sum += bits1 + signs;
2342         }
2343         else
2344         {
2345                 *choice = table2;
2346                 *sum += bits2 + signs;
2347         }
2348 }
2349
2350
2351
2352
2353
2354 /*
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.
2358 */
2359
2360 static  void                    triple_Huffman_2
2361 (
2362         unsigned                                start,
2363         unsigned                                end,
2364 /*      unsigned                                table0,   == 13 */
2365 /*      unsigned                                table1,   == 15 */
2366 /*      unsigned                                table2,   == 24 */
2367         unsigned                                *choice,
2368         unsigned                                *sum
2369 )
2370 {
2371         int                                             v;
2372         unsigned                                bits0, bits1, bits2, signs, idx;
2373
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;
2377
2378 #if 0   /* not needed */
2379         static  unsigned                                ylen = h0->ylen;   /* == h1->ylen == h2->ylen */   /* == 16 */
2380 #endif
2381
2382         int                                             *pos = ix_l + start;
2383         int                                             *fin = ix_l + end;
2384
2385         bits0 = bits1 = bits2 = signs = 0;
2386
2387         while (pos < fin)
2388         {
2389                 idx = 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];
2395         }
2396
2397         if (bits0 < bits1  &&  bits0 < bits2)
2398         {
2399                 *choice = /* table0 */ 13;
2400                 *sum += bits0 + signs;
2401         }
2402         else if (bits1 < bits2)
2403         {
2404                 *choice = /* table1 */ 15;
2405                 *sum += bits1 + signs;
2406         }
2407         else
2408         {
2409                 *choice = /* table2 */ 24;
2410                 *sum += bits2 + signs;
2411         }
2412 }
2413
2414
2415
2416 static  void                    tiny_triple_Huffman_2
2417 (
2418         unsigned                                start,
2419         unsigned                                end,
2420 /*      unsigned                                table0,   == 13 */
2421 /*      unsigned                                table1,   == 15 */
2422 /*      unsigned                                table2,   == 24 */
2423         unsigned                                *choice,
2424         unsigned                                *sum
2425 )
2426 {
2427         int                                             v0, v1, v2;
2428         unsigned                                bits0, bits1, bits2, signs, idx0, idx1, idx2;
2429
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;
2433
2434 #if 0   /* not needed */
2435         static  unsigned                                ylen = h0->ylen;   /* == h1->ylen == h2->ylen */   /* == 16 */
2436 #endif
2437
2438         int                                             *pos = ix_l + start;
2439         int                                             *fin = ix_l + end;
2440
2441         bits0 = bits1 = bits2 = signs = 0;
2442
2443         while (pos < fin)
2444         {
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];
2455         }
2456
2457         if (bits0 < bits1  &&  bits0 < bits2)
2458         {
2459                 *choice = /* table0 */ 13;
2460                 *sum += bits0 + signs;
2461         }
2462         else if (bits1 < bits2)
2463         {
2464                 *choice = /* table1 */ 15;
2465                 *sum += bits1 + signs;
2466         }
2467         else
2468         {
2469                 *choice = /* table2 */ 24;
2470                 *sum += bits2 + signs;
2471         }
2472 }
2473
2474
2475
2476
2477
2478 /*
2479         In case of two escape tables, we esepecially have to take care for
2480         the possibly different linbits values...
2481 */
2482
2483 static  void                    double_Huffman_2   /* Escape tables */
2484 (
2485         unsigned                                start,
2486         unsigned                                end,
2487         unsigned                                table0,   /* 16... */
2488         unsigned                                table1,   /* 24... */
2489         unsigned                                *choice,
2490         unsigned                                *sum
2491 )
2492 {
2493         int                                             v;
2494         unsigned                                bits0, bits1, signs, xbits, idx;
2495
2496         struct huffcodetab              *h0 = ht + table0;
2497         struct huffcodetab              *h1 = ht + table1;
2498
2499 #if 0   /* not needed */
2500         static  unsigned                                ylen = h0->ylen;   /* == h1->ylen */   /* == 16 */
2501 #endif
2502         unsigned                                linbits0 = h0->linbits;
2503         unsigned                                linbits1 = h1->linbits;
2504
2505         int                                             *pos = ix_l + start;
2506         int                                             *fin = ix_l + end;
2507
2508         bits0 = bits1 = signs = xbits = 0;
2509
2510         while (pos < fin)
2511         {
2512                 idx = 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];
2517         }
2518         bits0 += xbits * linbits0;
2519         bits1 += xbits * linbits1;
2520
2521         if (bits0 < bits1)
2522         {
2523                 *choice = table0;
2524                 *sum += bits0 + signs;
2525         }
2526         else
2527         {
2528                 *choice = table1;
2529                 *sum += bits1 + signs;
2530         }
2531 }
2532
2533
2534
2535 static  void                    tiny_double_Huffman_2   /* Escape tables */
2536 (
2537         unsigned                                start,
2538         unsigned                                end,
2539         unsigned                                table0,   /* 16... */
2540         unsigned                                table1,   /* 24... */
2541         unsigned                                *choice,
2542         unsigned                                *sum
2543 )
2544 {
2545         int                                             v0, v1, v2;
2546         unsigned                                bits0, bits1, signs, xbits, idx0, idx1, idx2;
2547
2548         struct huffcodetab              *h0 = ht + table0;
2549         struct huffcodetab              *h1 = ht + table1;
2550
2551 #if 0   /* not needed */
2552         static  unsigned                                ylen = h0->ylen;   /* == h1->ylen == 16 --- static because of the constant!!! */
2553 #endif
2554         int                                             *pos = ix_l + start;
2555         int                                             *fin = ix_l + end;
2556
2557         bits0 = bits1 = signs = xbits = 0;
2558
2559         while (pos < fin)
2560         {
2561                 idx0 = idx1 = idx2 = 0;
2562
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;}
2569
2570                 bits0 += h0->hlen[idx0] + h0->hlen[idx1] + h0->hlen[idx2];
2571                 bits1 += h1->hlen[idx0] + h1->hlen[idx1] + h1->hlen[idx2];
2572         }
2573
2574         bits0 += xbits * h0->linbits;
2575         bits1 += xbits * h1->linbits;
2576
2577         if (bits0 < bits1)
2578         {
2579                 *choice = table0;
2580                 *sum += bits0 + signs;
2581         }
2582         else
2583         {
2584                 *choice = table1;
2585                 *sum += bits1 + signs;
2586         }
2587 }
2588
2589
2590