Subversion Repositories planix.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
/*
2
 * MP3 quantization
3
 *
4
 * Copyright (c) 1999 Mark Taylor
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Library General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the GNU
14
 * Library General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Library General Public
17
 * License along with this library; if not, write to the
18
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19
 * Boston, MA 02111-1307, USA.
20
 */
21
 
22
/* $Id: quantize.c,v 1.57 2001/02/27 06:14:57 markt Exp $ */
23
 
24
#ifdef HAVE_CONFIG_H
25
# include <config.h>
26
#endif
27
 
28
#include <math.h>
29
#include <assert.h>
30
#include "util.h"
31
#include "l3side.h"
32
#include "quantize.h"
33
#include "reservoir.h"
34
#include "quantize_pvt.h"
35
#include "lame-analysis.h"
36
 
37
#ifdef WITH_DMALLOC
38
#include <dmalloc.h>
39
#endif
40
 
41
 
42
/************************************************************************
43
 *
44
 *      init_outer_loop()
45
 *  mt 6/99                                    
46
 *
47
 *  initializes cod_info, scalefac and xrpow
48
 *
49
 *  returns 0 if all energies in xr are zero, else 1                    
50
 *
51
 ************************************************************************/
52
 
53
static int 
54
init_outer_loop(
55
    gr_info *const cod_info, 
56
    III_scalefac_t *const scalefac, 
57
    const int is_mpeg1,
58
    const FLOAT8 xr[576], 
59
    FLOAT8 xrpow[576] )
60
{
61
    FLOAT8 tmp, sum = 0;
62
    int i;
63
 
64
    /*  initialize fresh cod_info
65
     */
66
    cod_info->part2_3_length      = 0;
67
    cod_info->big_values          = 0;
68
    cod_info->count1              = 0;
69
    cod_info->global_gain         = 210;
70
    cod_info->scalefac_compress   = 0;
71
    /* window_switching_flag was set in psymodel.c? */
72
    /* block_type            was set in psymodel.c? */
73
    /* mixed_block_flag      would be set in ^      */
74
    cod_info->table_select [0]    = 0;
75
    cod_info->table_select [1]    = 0;
76
    cod_info->table_select [2]    = 0;
77
    cod_info->subblock_gain[0]    = 0;
78
    cod_info->subblock_gain[1]    = 0;
79
    cod_info->subblock_gain[2]    = 0;
80
    cod_info->region0_count       = 0;
81
    cod_info->region1_count       = 0;
82
    cod_info->preflag             = 0;
83
    cod_info->scalefac_scale      = 0;
84
    cod_info->count1table_select  = 0;
85
    cod_info->part2_length        = 0;
86
    if (cod_info->block_type == SHORT_TYPE) {
87
        cod_info->sfb_lmax        = 0;
88
        cod_info->sfb_smin        = 0;
89
	if (cod_info->mixed_block_flag) {
90
            /*
91
             *  MPEG-1:      sfbs 0-7 long block, 3-12 short blocks 
92
             *  MPEG-2(.5):  sfbs 0-5 long block, 3-12 short blocks
93
             */ 
94
            cod_info->sfb_lmax    = is_mpeg1 ? 8 : 6;
95
	    cod_info->sfb_smin    = 3;
96
	}
97
    } else {
98
        cod_info->sfb_lmax        = SBPSY_l;
99
        cod_info->sfb_smin        = SBPSY_s;
100
    }   
101
    cod_info->count1bits          = 0;  
102
    cod_info->sfb_partition_table = nr_of_sfb_block[0][0];
103
    cod_info->slen[0]             = 0;
104
    cod_info->slen[1]             = 0;
105
    cod_info->slen[2]             = 0;
106
    cod_info->slen[3]             = 0;
107
 
108
    /*  fresh scalefactors are all zero
109
     */
110
    memset(scalefac, 0, sizeof(III_scalefac_t));
111
 
112
    /*  check if there is some energy we have to quantize
113
     *  and calculate xrpow matching our fresh scalefactors
114
     */
115
    for (i = 0; i < 576; ++i) {
116
        tmp = fabs (xr[i]);
117
	sum += tmp;
118
        xrpow[i] = sqrt (tmp * sqrt(tmp));
119
    }
120
   /*  return 1 if we have something to quantize, else 0
121
    */
122
   return sum > (FLOAT8)1E-20;
123
}
124
 
125
 
126
 
127
/************************************************************************
128
 *
129
 *      bin_search_StepSize()
130
 *
131
 *  author/date??
132
 *
133
 *  binary step size search
134
 *  used by outer_loop to get a quantizer step size to start with
135
 *
136
 ************************************************************************/
137
 
138
typedef enum {
139
    BINSEARCH_NONE,
140
    BINSEARCH_UP, 
141
    BINSEARCH_DOWN
142
} binsearchDirection_t;
143
 
144
int 
145
bin_search_StepSize(
146
          lame_internal_flags * const gfc,
147
          gr_info * const cod_info,
148
    const int             desired_rate, 
149
    const int             start, 
150
    const FLOAT8          xrpow [576],
151
          int             l3enc [576] ) 
152
{
153
    int nBits;
154
    int CurrentStep;
155
    int flag_GoneOver = 0;
156
    int StepSize      = start;
157
 
158
    binsearchDirection_t Direction = BINSEARCH_NONE;
159
    assert(gfc->CurrentStep);
160
    CurrentStep = gfc->CurrentStep;
161
 
162
    do {
163
        cod_info->global_gain = StepSize;
164
        nBits = count_bits(gfc,l3enc,xrpow,cod_info);  
165
 
166
        if (CurrentStep == 1) break; /* nothing to adjust anymore */
167
 
168
        if (flag_GoneOver) CurrentStep /= 2;
169
 
170
        if (nBits > desired_rate) {  
171
            /* increase Quantize_StepSize */
172
            if (Direction == BINSEARCH_DOWN && !flag_GoneOver) {
173
                flag_GoneOver = 1;
174
                CurrentStep  /= 2; /* late adjust */
175
            }
176
            Direction = BINSEARCH_UP;
177
            StepSize += CurrentStep;
178
            if (StepSize > 255) break;
179
        }
180
        else if (nBits < desired_rate) {
181
            /* decrease Quantize_StepSize */
182
            if (Direction == BINSEARCH_UP && !flag_GoneOver) {
183
                flag_GoneOver = 1;
184
                CurrentStep  /= 2; /* late adjust */
185
            }
186
            Direction = BINSEARCH_DOWN;
187
            StepSize -= CurrentStep;
188
            if (StepSize < 0) break;
189
        }
190
        else break; /* nBits == desired_rate;; most unlikely to happen.*/
191
    } while (1); /* For-ever, break is adjusted. */
192
 
193
    CurrentStep = start - StepSize;
194
 
195
    gfc->CurrentStep = CurrentStep/4 != 0 ? 4 : 2;
196
 
197
    return nBits;
198
}
199
 
200
 
201
 
202
 
203
/*************************************************************************** 
204
 *
205
 *         inner_loop ()                                                     
206
 *
207
 *  author/date??
208
 *
209
 *  The code selects the best global gain for a particular set of scalefacs 
210
 *
211
 ***************************************************************************/ 
212
 
213
int 
214
inner_loop(
215
          lame_internal_flags * const gfc,
216
          gr_info * const cod_info,
217
    const int             max_bits,
218
    const FLOAT8          xrpow [576],
219
          int             l3enc [576] )
220
{
221
    int bits;
222
 
223
    assert(max_bits >= 0);
224
 
225
    /*  scalefactors may have changed, so count bits
226
     */
227
    bits=count_bits(gfc,l3enc,xrpow,cod_info);
228
 
229
    /*  increase quantizer stepsize until needed bits are below maximum
230
     */
231
    while (bits > max_bits) {
232
        cod_info->global_gain++;
233
        bits = count_bits (gfc, l3enc, xrpow, cod_info);
234
    } 
235
 
236
    return bits;
237
}
238
 
239
 
240
 
241
/*************************************************************************
242
 *
243
 *      loop_break()                                               
244
 *
245
 *  author/date??
246
 *
247
 *  Function: Returns zero if there is a scalefac which has not been
248
 *            amplified. Otherwise it returns one. 
249
 *
250
 *************************************************************************/
251
 
252
inline 
253
static int
254
loop_break( 
255
    const gr_info        * const cod_info,
256
    const III_scalefac_t * const scalefac ) 
257
{
258
    unsigned int i, sfb;
259
 
260
    for (sfb = 0; sfb < cod_info->sfb_lmax; sfb++)
261
        if (scalefac->l[sfb] == 0)
262
            return 0;
263
 
264
    for (sfb = cod_info->sfb_smin; sfb < SBPSY_s; sfb++)
265
        for (i = 0; i < 3; i++) 
266
            if (scalefac->s[sfb][i] == 0 && cod_info->subblock_gain[i] == 0)
267
                return 0;
268
 
269
    return 1;
270
}
271
 
272
 
273
 
274
 
275
/*************************************************************************
276
 *
277
 *      quant_compare()                                               
278
 *
279
 *  author/date??
280
 *
281
 *  several different codes to decide which quantization is better
282
 *
283
 *************************************************************************/
284
 
285
inline 
286
static int 
287
quant_compare(
288
    const int                       experimentalX,
289
    const calc_noise_result * const best,
290
    const calc_noise_result * const calc )
291
{
292
    /*
293
       noise is given in decibels (dB) relative to masking thesholds.
294
 
295
       over_noise:  ??? (the previous comment is fully wrong)
296
       tot_noise:   ??? (the previous comment is fully wrong)
297
       max_noise:   max quantization noise 
298
 
299
     */
300
    int better;
301
 
302
    switch (experimentalX) {
303
        default:
304
        case 0: 
305
	    better = calc->over_count  < best->over_count
306
               ||  ( calc->over_count == best->over_count  &&
307
                     calc->over_noise  < best->over_noise )
308
               ||  ( calc->over_count == best->over_count  &&
309
                     calc->over_noise == best->over_noise  &&
310
                     calc->tot_noise   < best->tot_noise  ); 
311
	    break;
312
        case 1: 
313
	    better = calc->max_noise < best->max_noise; 
314
	    break;
315
        case 2: 
316
	    better = calc->tot_noise < best->tot_noise; 
317
	    break;
318
        case 3: 
319
	    better = calc->tot_noise < best->tot_noise  &&
320
                     calc->max_noise < best->max_noise+2; 
321
	    break;
322
        case 4: 
323
	    better = ( calc->max_noise <= 0  &&
324
                       best->max_noise >  2 )
325
                 ||  ( calc->max_noise <= 0  &&
326
                       best->max_noise <  0  &&
327
                       best->max_noise >  calc->max_noise-2  &&
328
                       calc->tot_noise <  best->tot_noise )
329
                 ||  ( calc->max_noise <= 0  &&
330
                       best->max_noise >  0  &&
331
                       best->max_noise >  calc->max_noise-2  &&
332
                       calc->tot_noise <  best->tot_noise+best->over_noise )
333
                 ||  ( calc->max_noise >  0  &&
334
                       best->max_noise > -0.5  &&
335
                       best->max_noise >  calc->max_noise-1  &&
336
                       calc->tot_noise+calc->over_noise < best->tot_noise+best->over_noise )
337
                 ||  ( calc->max_noise >  0  &&
338
                       best->max_noise > -1  &&
339
                       best->max_noise >  calc->max_noise-1.5  &&
340
                       calc->tot_noise+calc->over_noise+calc->over_noise < best->tot_noise+best->over_noise+best->over_noise );
341
            break;
342
        case 5: 
343
	    better =   calc->over_noise  < best->over_noise
344
                 ||  ( calc->over_noise == best->over_noise  &&
345
                       calc->tot_noise   < best->tot_noise ); 
346
	    break;
347
        case 6: 
348
	    better =   calc->over_noise  < best->over_noise
349
                 ||  ( calc->over_noise == best->over_noise  &&
350
                     ( calc->max_noise   < best->max_noise  
351
		     ||  ( calc->max_noise  == best->max_noise  &&
352
                           calc->tot_noise  <= best->tot_noise )
353
		      )); 
354
	    break;
355
        case 7: 
356
	    better =   calc->over_count < best->over_count
357
                   ||  calc->over_noise < best->over_noise; 
358
	    break;
359
        case 8: 
360
	    better =   calc->klemm_noise < best->klemm_noise;
361
            break;
362
    }   
363
 
364
    return better;
365
}
366
 
367
 
368
 
369
/*************************************************************************
370
 *
371
 *          amp_scalefac_bands() 
372
 *
373
 *  author/date??
374
 *        
375
 *  Amplify the scalefactor bands that violate the masking threshold.
376
 *  See ISO 11172-3 Section C.1.5.4.3.5
377
 * 
378
 *  distort[] = noise/masking
379
 *  distort[] > 1   ==> noise is not masked
380
 *  distort[] < 1   ==> noise is masked
381
 *  max_dist = maximum value of distort[]
382
 *  
383
 *  Three algorithms:
384
 *  noise_shaping_amp
385
 *        0             Amplify all bands with distort[]>1.
386
 *
387
 *        1             Amplify all bands with distort[] >= max_dist^(.5);
388
 *                     ( 50% in the db scale)
389
 *
390
 *        2             Amplify first band with distort[] >= max_dist;
391
 *                       
392
 *
393
 *  For algorithms 0 and 1, if max_dist < 1, then amplify all bands 
394
 *  with distort[] >= .95*max_dist.  This is to make sure we always
395
 *  amplify at least one band.  
396
 * 
397
 *
398
 *************************************************************************/
399
static void 
400
amp_scalefac_bands(
401
    lame_global_flags *gfp,
402
    const gr_info  *const cod_info, 
403
    III_scalefac_t *const scalefac,
404
    III_psy_xmin *distort,
405
    FLOAT8 xrpow[576] )
406
{
407
  lame_internal_flags *gfc=gfp->internal_flags;
408
  int start, end, l,i,j,sfb;
409
  FLOAT8 ifqstep34, trigger;
410
 
411
  if (cod_info->scalefac_scale == 0) {
412
    ifqstep34 = 1.29683955465100964055; /* 2**(.75*.5)*/
413
  } else {
414
    ifqstep34 = 1.68179283050742922612;  /* 2**(.75*1) */
415
  }
416
 
417
  /* compute maximum value of distort[]  */
418
  trigger = 0;
419
  for (sfb = 0; sfb < cod_info->sfb_lmax; sfb++) {
420
    if (trigger < distort->l[sfb])
421
        trigger = distort->l[sfb];
422
  }
423
  for (sfb = cod_info->sfb_smin; sfb < SBPSY_s; sfb++) {
424
    for (i = 0; i < 3; i++ ) {
425
      if (trigger < distort->s[sfb][i])
426
          trigger = distort->s[sfb][i];
427
    }
428
  }
429
 
430
  switch (gfc->noise_shaping_amp) {
431
 
432
  case 2:
433
    /* amplify exactly 1 band */
434
    //trigger = distort_thresh;
435
    break;
436
 
437
  case 1:
438
    /* amplify bands within 50% of max (on db scale) */
439
    if (trigger>1.0)
440
        trigger = pow(trigger, .5);
441
    else
442
      trigger *= .95;
443
    break;
444
 
445
  case 0:
446
  default:
447
    /* ISO algorithm.  amplify all bands with distort>1 */
448
    if (trigger>1.0)
449
        trigger=1.0;
450
    else
451
        trigger *= .95;
452
    break;
453
  }
454
 
455
  for (sfb = 0; sfb < cod_info->sfb_lmax; sfb++ ) {
456
    start = gfc->scalefac_band.l[sfb];
457
    end   = gfc->scalefac_band.l[sfb+1];
458
    if (distort->l[sfb]>=trigger  ) {
459
      scalefac->l[sfb]++;
460
      for ( l = start; l < end; l++ )
461
	xrpow[l] *= ifqstep34;
462
      if (gfc->noise_shaping_amp==2) goto done;
463
    }
464
  }
465
 
466
  for ( j=0,sfb = cod_info->sfb_smin; sfb < SBPSY_s; sfb++ ) {
467
    start = gfc->scalefac_band.s[sfb];
468
    end   = gfc->scalefac_band.s[sfb+1];
469
    for ( i = 0; i < 3; i++ ) {
470
      int j2 = j;
471
      if ( distort->s[sfb][i]>=trigger) {
472
	scalefac->s[sfb][i]++;
473
	for (l = start; l < end; l++) 
474
	  xrpow[j2++] *= ifqstep34;
475
        if (gfc->noise_shaping_amp==2) goto done;
476
      }
477
      j += end-start;
478
    }
479
  }
480
 done:
481
 return;
482
}
483
 
484
/*************************************************************************
485
 *
486
 *      inc_scalefac_scale()
487
 *
488
 *  Takehiro Tominaga 2000-xx-xx
489
 *
490
 *  turns on scalefac scale and adjusts scalefactors
491
 *
492
 *************************************************************************/
493
 
494
static void
495
inc_scalefac_scale (
496
    const lame_internal_flags        * const gfc, 
497
          gr_info        * const cod_info, 
498
          III_scalefac_t * const scalefac,
499
          FLOAT8                 xrpow[576] )
500
{
501
    int start, end, l,i,j;
502
    int sfb;
503
    const FLOAT8 ifqstep34 = 1.29683955465100964055;
504
 
505
    for (sfb = 0; sfb < cod_info->sfb_lmax; sfb++) {
506
        int s = scalefac->l[sfb] + (cod_info->preflag ? pretab[sfb] : 0);
507
        if (s & 1) {
508
            s++;
509
            start = gfc->scalefac_band.l[sfb];
510
            end   = gfc->scalefac_band.l[sfb+1];
511
            for (l = start; l < end; l++) 
512
                xrpow[l] *= ifqstep34;
513
        }
514
        scalefac->l[sfb]  = s >> 1;
515
        cod_info->preflag = 0;
516
    }
517
 
518
    for (j = 0, sfb = cod_info->sfb_smin; sfb < SBPSY_s; sfb++) {
519
    start = gfc->scalefac_band.s[sfb];
520
    end   = gfc->scalefac_band.s[sfb+1];
521
    for (i = 0; i < 3; i++) {
522
        int j2 = j;
523
        if (scalefac->s[sfb][i] & 1) {
524
        scalefac->s[sfb][i]++;
525
        for (l = start; l < end; l++) 
526
            xrpow[j2++] *= ifqstep34;
527
        }
528
        scalefac->s[sfb][i] >>= 1;
529
        j += end-start;
530
    }
531
    }
532
    cod_info->scalefac_scale = 1;
533
}
534
 
535
 
536
 
537
/*************************************************************************
538
 *
539
 *      inc_subblock_gain()
540
 *
541
 *  Takehiro Tominaga 2000-xx-xx
542
 *
543
 *  increases the subblock gain and adjusts scalefactors
544
 *
545
 *************************************************************************/
546
 
547
static int 
548
inc_subblock_gain (
549
    const lame_internal_flags        * const gfc,
550
          gr_info        * const cod_info,
551
          III_scalefac_t * const scalefac,
552
          FLOAT8                 xrpow[576] )
553
{
554
    int window;
555
 
556
    for (window = 0; window < 3; window++) {
557
        int s1, s2, l;
558
        int sfb;
559
        s1 = s2 = 0;
560
 
561
        for (sfb = cod_info->sfb_smin; sfb < 6; sfb++) {
562
            if (s1 < scalefac->s[sfb][window])
563
            s1 = scalefac->s[sfb][window];
564
        }
565
        for (; sfb < SBPSY_s; sfb++) {
566
            if (s2 < scalefac->s[sfb][window])
567
            s2 = scalefac->s[sfb][window];
568
        }
569
 
570
        if (s1 < 16 && s2 < 8)
571
            continue;
572
 
573
        if (cod_info->subblock_gain[window] >= 7)
574
            return 1;
575
 
576
        /* even though there is no scalefactor for sfb12
577
         * subblock gain affects upper frequencies too, that's why
578
         * we have to go up to SBMAX_s
579
         */
580
        cod_info->subblock_gain[window]++;
581
        for (sfb = cod_info->sfb_smin; sfb < SBMAX_s; sfb++) {
582
            int i, width;
583
            int s = scalefac->s[sfb][window];
584
            FLOAT8 amp;
585
 
586
            if (s < 0)
587
                continue;
588
            s = s - (4 >> cod_info->scalefac_scale);
589
            if (s >= 0) {
590
                scalefac->s[sfb][window] = s;
591
                continue;
592
            }
593
 
594
            scalefac->s[sfb][window] = 0;
595
            width = gfc->scalefac_band.s[sfb] - gfc->scalefac_band.s[sfb+1];
596
            i = gfc->scalefac_band.s[sfb] * 3 + width * window;
597
            amp = IPOW20(210 + (s << (cod_info->scalefac_scale + 1)));
598
            for (l = 0; l < width; l++) {
599
                xrpow[l] *= amp;
600
            }
601
        }
602
    }
603
    return 0;
604
}
605
 
606
 
607
 
608
/********************************************************************
609
 *
610
 *      balance_noise()
611
 *
612
 *  Takehiro Tominaga /date??
613
 *  Robert Hegemann 2000-09-06: made a function of it
614
 *
615
 *  amplifies scalefactor bands, 
616
 *   - if all are already amplified returns 0
617
 *   - if some bands are amplified too much:
618
 *      * try to increase scalefac_scale
619
 *      * if already scalefac_scale was set
620
 *          try on short blocks to increase subblock gain
621
 *
622
 ********************************************************************/
623
inline
624
static int 
625
balance_noise (
626
    lame_global_flags  *const gfp,
627
    gr_info        * const cod_info,
628
    III_scalefac_t * const scalefac, 
629
    III_psy_xmin           *distort,
630
    FLOAT8                 xrpow[576] )
631
{
632
    lame_internal_flags *const gfc = (lame_internal_flags *)gfp->internal_flags;
633
    int status;
634
 
635
    amp_scalefac_bands ( gfp, cod_info, scalefac, distort, xrpow);
636
 
637
    /* check to make sure we have not amplified too much 
638
     * loop_break returns 0 if there is an unamplified scalefac
639
     * scale_bitcount returns 0 if no scalefactors are too large
640
     */
641
 
642
    status = loop_break (cod_info, scalefac);
643
 
644
    if (status) 
645
        return 0; /* all bands amplified */
646
 
647
    /* not all scalefactors have been amplified.  so these 
648
     * scalefacs are possibly valid.  encode them: 
649
     */
650
    if (gfc->is_mpeg1)
651
        status = scale_bitcount (scalefac, cod_info);
652
    else 
653
        status = scale_bitcount_lsf (gfc, scalefac, cod_info);
654
 
655
    if (!status) 
656
        return 1; /* amplified some bands not exceeding limits */
657
 
658
    /*  some scalefactors are too large.
659
     *  lets try setting scalefac_scale=1 
660
     */
661
    if (gfc->noise_shaping > 1) {
662
	if (!cod_info->scalefac_scale) {
663
	    inc_scalefac_scale (gfc, cod_info, scalefac, xrpow);
664
	    status = 0;
665
	} else {
666
	    if (cod_info->block_type == SHORT_TYPE ) {
667
		status = inc_subblock_gain (gfc, cod_info, scalefac, xrpow)
668
		    || loop_break (cod_info, scalefac);
669
	    }
670
	}
671
    }
672
 
673
    if (!status) {
674
        if (gfc->is_mpeg1 == 1) 
675
            status = scale_bitcount (scalefac, cod_info);
676
        else 
677
            status = scale_bitcount_lsf (gfc, scalefac, cod_info);
678
    }    
679
    return !status;
680
}
681
 
682
 
683
 
684
/************************************************************************
685
 *
686
 *  outer_loop ()                                                       
687
 *
688
 *  Function: The outer iteration loop controls the masking conditions  
689
 *  of all scalefactorbands. It computes the best scalefac and          
690
 *  global gain. This module calls the inner iteration loop             
691
 * 
692
 *  mt 5/99 completely rewritten to allow for bit reservoir control,   
693
 *  mid/side channels with L/R or mid/side masking thresholds, 
694
 *  and chooses best quantization instead of last quantization when 
695
 *  no distortion free quantization can be found.  
696
 *  
697
 *  added VBR support mt 5/99
698
 *
699
 *  some code shuffle rh 9/00
700
 ************************************************************************/
701
 
702
static int 
703
outer_loop (
704
   lame_global_flags *gfp,
705
          gr_info        * const cod_info,
706
    const FLOAT8                 xr[576],   /* magnitudes of spectral values */
707
    const III_psy_xmin   * const l3_xmin,   /* allowed distortion of the scalefactor */
708
          III_scalefac_t * const scalefac,  /* scalefactors */
709
          FLOAT8                 xrpow[576], /* coloured magnitudes of spectral values */
710
          int                    l3enc[576], /* vector of quantized values ix(0..575) */
711
    const int                    ch, 
712
    const int                    targ_bits )  /* maximum allowed bits */
713
{
714
    lame_internal_flags *gfc=gfp->internal_flags;
715
    III_scalefac_t save_scalefac;
716
    gr_info save_cod_info;
717
    FLOAT8 save_xrpow[576];
718
    III_psy_xmin   distort;
719
    calc_noise_result noise_info;
720
    calc_noise_result best_noise_info;
721
    int l3_enc_w[576]; 
722
    int iteration = 0;
723
    int bits_found = 0;
724
    int huff_bits;
725
    int real_bits;
726
    int better;
727
    int over=0;
728
 
729
    int notdone = 1;
730
    int copy = 0;
731
    int age = 0;
732
 
733
    noise_info.over_count = 100;
734
    noise_info.tot_count  = 100;
735
    noise_info.max_noise  = 0;
736
    noise_info.tot_noise  = 0;
737
    noise_info.over_noise = 0;
738
 
739
    best_noise_info.over_count = 100;
740
 
741
    bits_found = bin_search_StepSize (gfc, cod_info, targ_bits, 
742
                                      gfc->OldValue[ch], xrpow, l3_enc_w);
743
    gfc->OldValue[ch] = cod_info->global_gain;
744
 
745
    /* BEGIN MAIN LOOP */
746
    do {
747
        iteration ++;
748
 
749
        /* inner_loop starts with the initial quantization step computed above
750
         * and slowly increases until the bits < huff_bits.
751
         * Thus it is important not to start with too large of an inital
752
         * quantization step.  Too small is ok, but inner_loop will take longer 
753
         */
754
        huff_bits = targ_bits - cod_info->part2_length;
755
        if (huff_bits < 0) {
756
            assert(iteration != 1);
757
            /*  scale factors too large, not enough bits. 
758
             *  use previous quantizaton */
759
            break;
760
        }
761
        /*  if this is the first iteration, 
762
         *  see if we can reuse the quantization computed in 
763
         *  bin_search_StepSize above */
764
 
765
        if (iteration == 1) {
766
            if (bits_found > huff_bits) {
767
                cod_info->global_gain++;
768
                real_bits = inner_loop (gfc, cod_info, huff_bits, xrpow, 
769
                                        l3_enc_w);
770
            } else {
771
                real_bits = bits_found;
772
            }
773
        } else {
774
            real_bits = inner_loop (gfc, cod_info, huff_bits, xrpow,
775
                                    l3_enc_w);
776
        }
777
 
778
        cod_info->part2_3_length = real_bits;
779
 
780
        /* compute the distortion in this quantization */
781
        if (gfc->noise_shaping) 
782
            /* coefficients and thresholds both l/r (or both mid/side) */
783
            over = calc_noise (gfc, xr, l3_enc_w, cod_info, l3_xmin, 
784
                               scalefac, &distort, &noise_info);
785
        else {
786
            /* fast mode, no noise shaping, we are ready */
787
            best_noise_info = noise_info;
788
            over = 0;
789
            copy = 0;
790
            memcpy(l3enc, l3_enc_w, sizeof(int)*576);
791
            break;
792
        }
793
 
794
 
795
        /* check if this quantization is better
796
         * than our saved quantization */
797
        if (iteration == 1) /* the first iteration is always better */
798
            better = 1;
799
        else
800
            better = quant_compare (gfp->experimentalX, 
801
                                    &best_noise_info, &noise_info);
802
 
803
        /* save data so we can restore this quantization later */    
804
        if (better) {
805
            copy = 0;
806
            best_noise_info = noise_info;
807
            memcpy(l3enc, l3_enc_w, sizeof(int)*576);
808
            age = 0;
809
        }
810
        else
811
            age ++;
812
 
813
 
814
        /******************************************************************/
815
        /* stopping criterion */
816
        /******************************************************************/
817
        /* if no bands with distortion and -X0, we are done */
818
        if (0==gfc->noise_shaping_stop && 
819
            0==gfp->experimentalX &&
820
	    (over == 0 || best_noise_info.over_count == 0) )
821
            break;
822
        /* Otherwise, allow up to 3 unsuccesful tries in serial, then stop 
823
         * if our best quantization so far had no distorted bands. This
824
         * gives us more possibilities for different quant_compare modes.
825
         * Much more than 3 makes not a big difference, it is only slower.
826
         */
827
        if (age > 3 && best_noise_info.over_count == 0) 
828
            break;
829
 
830
 
831
 
832
 
833
 
834
        /* Check if the last scalefactor band is distorted.
835
         * in VBR mode we can't get rid of the distortion, so quit now
836
         * and VBR mode will try again with more bits.  
837
         * (makes a 10% speed increase, the files I tested were
838
         * binary identical, 2000/05/20 Robert.Hegemann@gmx.de)
839
         * distort[] > 1 means noise > allowed noise
840
         */
841
        if (gfc->sfb21_extra) {
842
            if (cod_info->block_type == SHORT_TYPE) {
843
                if (distort.s[SBMAX_s-1][0] > 1 ||
844
                    distort.s[SBMAX_s-1][1] > 1 ||
845
                    distort.s[SBMAX_s-1][2] > 1) break;
846
            } else {
847
                if (distort.l[SBMAX_l-1] > 1) break;
848
            }
849
        }
850
 
851
        /* save data so we can restore this quantization later */    
852
        if (better) {
853
            copy = 1;
854
            save_scalefac = *scalefac;
855
            save_cod_info = *cod_info;
856
            if (gfp->VBR == vbr_rh || gfp->VBR == vbr_mtrh) {
857
                /* store for later reuse */
858
                memcpy(save_xrpow, xrpow, sizeof(FLOAT8)*576);
859
            }
860
        }
861
 
862
        notdone = balance_noise (gfp, cod_info, scalefac, &distort, xrpow);
863
 
864
        if (notdone == 0) 
865
            break;
866
    }
867
    while (1); /* main iteration loop, breaks adjusted */
868
 
869
    /*  finish up
870
     */
871
    if (copy) {
872
        *cod_info = save_cod_info;
873
        *scalefac = save_scalefac;
874
        if (gfp->VBR == vbr_rh || gfp->VBR == vbr_mtrh)
875
            /* restore for reuse on next try */
876
            memcpy(xrpow, save_xrpow, sizeof(FLOAT8)*576);
877
    }
878
    cod_info->part2_3_length += cod_info->part2_length;
879
 
880
    assert (cod_info->global_gain < 256);
881
 
882
    return best_noise_info.over_count;
883
}
884
 
885
 
886
 
887
 
888
/************************************************************************
889
 *
890
 *      iteration_finish()                                                    
891
 *
892
 *  Robert Hegemann 2000-09-06
893
 *
894
 *  update reservoir status after FINAL quantization/bitrate 
895
 *
896
 *  rh 2000-09-06: it will not work with CBR due to the bitstream formatter
897
 *            you will get "Error: MAX_HEADER_BUF too small in bitstream.c"
898
 *
899
 ************************************************************************/
900
 
901
static void 
902
iteration_finish (
903
    lame_internal_flags *gfc,
904
    FLOAT8          xr      [2][2][576],
905
    int             l3_enc  [2][2][576],
906
    III_psy_ratio   ratio   [2][2],  
907
    III_scalefac_t  scalefac[2][2],
908
    const int       mean_bits )
909
{
910
    III_side_info_t *l3_side = &gfc->l3_side;
911
    int gr, ch, i;
912
 
913
    for (gr = 0; gr < gfc->mode_gr; gr++) {
914
        for (ch = 0; ch < gfc->channels_out; ch++) {
915
            gr_info *cod_info = &l3_side->gr[gr].ch[ch].tt;
916
 
917
            /*  try some better scalefac storage
918
             */
919
            best_scalefac_store (gfc, gr, ch, l3_enc, l3_side, scalefac);
920
 
921
            /*  best huffman_divide may save some bits too
922
             */
923
            if (gfc->use_best_huffman == 1) 
924
                best_huffman_divide (gfc, gr, ch, cod_info, l3_enc[gr][ch]);
925
 
926
            /*  update reservoir status after FINAL quantization/bitrate
927
             */
928
            ResvAdjust (gfc, cod_info, l3_side, mean_bits);
929
 
930
            /*  set the sign of l3_enc from the sign of xr
931
             */
932
            for (i = 0; i < 576; i++) {
933
                if (xr[gr][ch][i] < 0) l3_enc[gr][ch][i] *= -1; 
934
            }
935
        } /* for ch */
936
    }    /* for gr */
937
 
938
    ResvFrameEnd (gfc, l3_side, mean_bits);
939
}
940
 
941
 
942
 
943
/*********************************************************************
944
 *
945
 *      VBR_encode_granule()
946
 *
947
 *  2000-09-04 Robert Hegemann
948
 *
949
 *********************************************************************/
950
 
951
static void
952
VBR_encode_granule (
953
          lame_global_flags *gfp,
954
          gr_info        * const cod_info,
955
          FLOAT8                 xr[576],     /* magnitudes of spectral values */
956
    const III_psy_xmin   * const l3_xmin,     /* allowed distortion of the scalefactor */
957
          III_scalefac_t * const scalefac,    /* scalefactors */
958
          FLOAT8                 xrpow[576],  /* coloured magnitudes of spectral values */
959
          int                    l3_enc[576], /* vector of quantized values ix(0..575) */
960
    const int                    ch, 
961
          int                    min_bits, 
962
          int                    max_bits )
963
{
964
    //lame_internal_flags *gfc=gfp->internal_flags;
965
    gr_info         bst_cod_info;
966
    III_scalefac_t  bst_scalefac;
967
    FLOAT8          bst_xrpow [576]; 
968
    int             bst_l3_enc[576];
969
    int Max_bits  = max_bits;
970
    int real_bits = max_bits+1;
971
    int this_bits = min_bits+(max_bits-min_bits)/2;
972
    int dbits, over;
973
 
974
    assert(Max_bits <= MAX_BITS);
975
 
976
    bst_cod_info = *cod_info;
977
    memset(&bst_scalefac, 0, sizeof(III_scalefac_t));
978
    memcpy(&bst_xrpow, xrpow, sizeof(FLOAT8)*576);
979
 
980
    /*  search within round about 40 bits of optimal
981
     */
982
    do {
983
        assert(this_bits >= min_bits);
984
        assert(this_bits <= max_bits);
985
 
986
        over = outer_loop ( gfp, cod_info, xr, l3_xmin, scalefac,
987
                            xrpow, l3_enc, ch, this_bits );
988
 
989
        /*  is quantization as good as we are looking for ?
990
         *  in this case: is no scalefactor band distorted?
991
         */
992
        if (over <= 0) {
993
            /*  now we know it can be done with "real_bits"
994
             *  and maybe we can skip some iterations
995
             */
996
            real_bits = cod_info->part2_3_length;
997
 
998
            /*  store best quantization so far
999
             */
1000
            bst_cod_info = *cod_info;
1001
            bst_scalefac = *scalefac;
1002
            memcpy(bst_xrpow, xrpow, sizeof(FLOAT8)*576);
1003
            memcpy(bst_l3_enc, l3_enc, sizeof(int)*576);
1004
 
1005
            /*  try with fewer bits
1006
             */
1007
            max_bits  = real_bits-32;
1008
            dbits     = max_bits-min_bits;
1009
            this_bits = min_bits+dbits/2;
1010
        } 
1011
        else {
1012
            /*  try with more bits
1013
             */
1014
            min_bits  = this_bits+32;
1015
            dbits     = max_bits-min_bits;
1016
            this_bits = min_bits+dbits/2;
1017
 
1018
            if (dbits>8) {
1019
                /*  start again with best quantization so far
1020
                 */
1021
                *cod_info = bst_cod_info;
1022
                *scalefac = bst_scalefac;
1023
                memcpy(xrpow, bst_xrpow, sizeof(FLOAT8)*576);
1024
            }
1025
        }
1026
    } while (dbits>8);
1027
 
1028
    if (real_bits <= Max_bits) {
1029
        /*  restore best quantization found
1030
         */
1031
        *cod_info = bst_cod_info;
1032
        *scalefac = bst_scalefac;
1033
        memcpy(l3_enc, bst_l3_enc, sizeof(int)*576);
1034
    }
1035
    assert(cod_info->part2_3_length <= Max_bits);
1036
}
1037
 
1038
 
1039
 
1040
/************************************************************************
1041
 *
1042
 *      get_framebits()   
1043
 *
1044
 *  Robert Hegemann 2000-09-05
1045
 *
1046
 *  calculates
1047
 *  * how many bits are available for analog silent granules
1048
 *  * how many bits to use for the lowest allowed bitrate
1049
 *  * how many bits each bitrate would provide
1050
 *
1051
 ************************************************************************/
1052
 
1053
static void 
1054
get_framebits (
1055
    lame_global_flags *gfp,
1056
    int     * const analog_mean_bits,
1057
    int     * const min_mean_bits,
1058
    int             frameBits[15] )
1059
{
1060
    lame_internal_flags *gfc=gfp->internal_flags;
1061
    int bitsPerFrame, mean_bits, i;
1062
    III_side_info_t *l3_side = &gfc->l3_side;
1063
 
1064
    /*  always use at least this many bits per granule per channel 
1065
     *  unless we detect analog silence, see below 
1066
     */
1067
    gfc->bitrate_index = gfc->VBR_min_bitrate;
1068
    getframebits (gfp, &bitsPerFrame, &mean_bits);
1069
    *min_mean_bits = mean_bits / gfc->channels_out;
1070
 
1071
    /*  bits for analog silence 
1072
     */
1073
    gfc->bitrate_index = 1;
1074
    getframebits (gfp, &bitsPerFrame, &mean_bits);
1075
    *analog_mean_bits = mean_bits / gfc->channels_out;
1076
 
1077
    for (i = 1; i <= gfc->VBR_max_bitrate; i++) {
1078
        gfc->bitrate_index = i;
1079
        getframebits (gfp, &bitsPerFrame, &mean_bits);
1080
        frameBits[i] = ResvFrameBegin (gfp, l3_side, mean_bits, bitsPerFrame);
1081
    }
1082
}
1083
 
1084
 
1085
 
1086
/************************************************************************
1087
 *
1088
 *      calc_min_bits()   
1089
 *
1090
 *  Robert Hegemann 2000-09-04
1091
 *
1092
 *  determine minimal bit skeleton
1093
 *
1094
 ************************************************************************/
1095
inline
1096
static int 
1097
calc_min_bits (
1098
    lame_global_flags *gfp,
1099
    const gr_info * const cod_info,
1100
    const int             pe,
1101
    const FLOAT8          ms_ener_ratio, 
1102
    const int             bands,    
1103
    const int             mch_bits,
1104
    const int             analog_mean_bits,
1105
    const int             min_mean_bits,
1106
    const int             analog_silence,
1107
    const int             ch )
1108
{
1109
    lame_internal_flags *gfc=gfp->internal_flags;
1110
    int min_bits, min_pe_bits;
1111
 
1112
    if (gfc->nsPsy.use) return 1;
1113
 
1114
    /*  base amount of minimum bits
1115
     */
1116
    min_bits = Max (125, min_mean_bits);
1117
 
1118
    if (gfc->mode_ext == MPG_MD_MS_LR && ch == 1)  
1119
        min_bits = Max (min_bits, mch_bits/5);
1120
 
1121
    /*  bit skeleton based on PE
1122
     */
1123
    if (cod_info->block_type == SHORT_TYPE) 
1124
        /*  if LAME switches to short blocks then pe is
1125
         *  >= 1000 on medium surge
1126
         *  >= 3000 on big surge
1127
         */
1128
        min_pe_bits = (pe-350) * bands/39;
1129
    else 
1130
        min_pe_bits = (pe-350) * bands/22;
1131
 
1132
    if (gfc->mode_ext == MPG_MD_MS_LR && ch == 1) {
1133
        /*  side channel will use a lower bit skeleton based on PE
1134
         */ 
1135
        FLOAT8 fac  = .33 * (.5 - ms_ener_ratio) / .5;
1136
        min_pe_bits = (int)(min_pe_bits * ((1-fac)/(1+fac)));
1137
    }
1138
    min_pe_bits = Min (min_pe_bits, (1820 * gfp->out_samplerate / 44100));
1139
 
1140
    /*  determine final minimum bits
1141
     */
1142
    if (analog_silence && !gfp->VBR_hard_min) 
1143
        min_bits = analog_mean_bits;
1144
    else 
1145
        min_bits = Max (min_bits, min_pe_bits);
1146
 
1147
    return min_bits;
1148
}
1149
 
1150
 
1151
 
1152
/************************************************************************
1153
 *
1154
 *      calc_max_bits()   
1155
 *
1156
 *  Robert Hegemann 2000-09-05
1157
 *
1158
 *  determine maximal bit skeleton
1159
 *
1160
 ************************************************************************/
1161
inline
1162
static int 
1163
calc_max_bits (
1164
    const lame_internal_flags * const gfc,
1165
    const int             frameBits[15],
1166
    const int             min_bits )
1167
{
1168
    int max_bits;
1169
 
1170
    max_bits  = frameBits[gfc->VBR_max_bitrate];
1171
    max_bits /= gfc->channels_out * gfc->mode_gr;
1172
    max_bits  = Min (1200 + max_bits, MAX_BITS - 195 * (gfc->channels_out - 1));
1173
    max_bits  = Max (max_bits, min_bits);
1174
 
1175
    return max_bits;
1176
}
1177
 
1178
 
1179
 
1180
/*********************************************************************
1181
 *
1182
 *      VBR_prepare()
1183
 *
1184
 *  2000-09-04 Robert Hegemann
1185
 *
1186
 *  * converts LR to MS coding when necessary 
1187
 *  * calculates allowed/adjusted quantization noise amounts
1188
 *  * detects analog silent frames
1189
 *
1190
 *  some remarks:
1191
 *  - lower masking depending on Quality setting
1192
 *  - quality control together with adjusted ATH MDCT scaling
1193
 *    on lower quality setting allocate more noise from
1194
 *    ATH masking, and on higher quality setting allocate
1195
 *    less noise from ATH masking.
1196
 *  - experiments show that going more than 2dB over GPSYCHO's
1197
 *    limits ends up in very annoying artefacts
1198
 *
1199
 *********************************************************************/
1200
 
1201
/* RH: this one needs to be overhauled sometime */
1202
 
1203
static int 
1204
VBR_prepare (
1205
          lame_global_flags *gfp,
1206
          FLOAT8          pe            [2][2],
1207
          FLOAT8          ms_ener_ratio [2], 
1208
          FLOAT8          xr            [2][2][576],
1209
          III_psy_ratio   ratio         [2][2], 
1210
          III_psy_xmin    l3_xmin       [2][2],
1211
          int             frameBits     [16],
1212
          int            *analog_mean_bits,
1213
          int            *min_mean_bits,
1214
          int             min_bits      [2][2],
1215
          int             max_bits      [2][2],
1216
          int             bands         [2][2] )
1217
{
1218
    lame_internal_flags *gfc=gfp->internal_flags;
1219
    static const FLOAT8 dbQ[10]={-2.,-1.0,-.66,-.33,0.,0.33,.66,1.0,1.33,1.66};
1220
    static const FLOAT8 dbQns[10]={- 4,- 3,-2,-1,0,0.7,1.4,2.1,2.8,3.5};
1221
    /*static const FLOAT8 atQns[10]={-16,-12,-8,-4,0,  1,  2,  3,  4,  5};*/
1222
 
1223
    static const FLOAT8 dbQmtrh[10]=
1224
        { -4., -3., -2., -1., 0., 0.5, 1., 1.5, 2., 2.5 };
1225
 
1226
    FLOAT8   masking_lower_db, adjust = 0.0;
1227
    int      gr, ch;
1228
    int      used_bits = 0, bits;
1229
    int      analog_silence = 1;
1230
 
1231
    assert( gfp->VBR_q <= 9 );
1232
    assert( gfp->VBR_q >= 0 );
1233
 
1234
    get_framebits (gfp, analog_mean_bits, min_mean_bits, frameBits);
1235
 
1236
    for (gr = 0; gr < gfc->mode_gr; gr++) {
1237
        if (gfc->mode_ext == MPG_MD_MS_LR) 
1238
            ms_convert (xr[gr], xr[gr]); 
1239
 
1240
        for (ch = 0; ch < gfc->channels_out; ch++) {
1241
            gr_info *cod_info = &gfc->l3_side.gr[gr].ch[ch].tt;
1242
 
1243
            if (cod_info->block_type == SHORT_TYPE) 
1244
                adjust = 5/(1+exp(3.5-pe[gr][ch]/300.))-0.14;
1245
            else 
1246
                adjust = 2/(1+exp(3.5-pe[gr][ch]/300.))-0.05;
1247
 
1248
	    if (vbr_mtrh == gfp->VBR) {
1249
	        masking_lower_db   = dbQmtrh[gfp->VBR_q] - adjust; 
1250
            } 
1251
            else if (gfc->nsPsy.use && gfp->ATHtype == 0) {
1252
	        masking_lower_db   = dbQns[gfp->VBR_q] - adjust; 
1253
	    } 
1254
            else {
1255
	        masking_lower_db   = dbQ[gfp->VBR_q] - adjust; 
1256
	    }
1257
            gfc->masking_lower = pow (10.0, masking_lower_db * 0.1);
1258
 
1259
            bands[gr][ch] = calc_xmin (gfp, xr[gr][ch], ratio[gr]+ch, 
1260
                                       cod_info, l3_xmin[gr]+ch);
1261
            if (bands[gr][ch]) 
1262
                analog_silence = 0;
1263
 
1264
 
1265
            min_bits[gr][ch] = calc_min_bits (gfp, cod_info, (int)pe[gr][ch],
1266
                                      ms_ener_ratio[gr], bands[gr][ch],
1267
                                      0, *analog_mean_bits, 
1268
                                      *min_mean_bits, analog_silence, ch);
1269
 
1270
            max_bits[gr][ch] = calc_max_bits (gfc, frameBits, min_bits[gr][ch]); 
1271
            used_bits += min_bits[gr][ch];           
1272
 
1273
        } /* for ch */
1274
    }  /* for gr */
1275
 
1276
    *min_mean_bits = Max(125, *min_mean_bits);
1277
    bits = 0.8*frameBits[gfc->VBR_max_bitrate];
1278
    if (used_bits >= bits) 
1279
        for (gr = 0; gr < gfc->mode_gr; gr++) {
1280
            for (ch = 0; ch < gfc->channels_out; ch++) {
1281
                min_bits[gr][ch] *= bits;
1282
                min_bits[gr][ch] /= used_bits;
1283
                if (min_bits[gr][ch] < *min_mean_bits)
1284
                    min_bits[gr][ch] = *min_mean_bits;
1285
                max_bits[gr][ch] *= bits;
1286
                max_bits[gr][ch] /= used_bits;
1287
                if (max_bits[gr][ch] < *min_mean_bits)
1288
                    max_bits[gr][ch] = *min_mean_bits;
1289
            }
1290
        }
1291
 
1292
 
1293
    return analog_silence;
1294
}
1295
 
1296
 
1297
 
1298
/************************************************************************
1299
 *
1300
 *      VBR_iteration_loop()   
1301
 *
1302
 *  tries to find out how many bits are needed for each granule and channel
1303
 *  to get an acceptable quantization. An appropriate bitrate will then be
1304
 *  choosed for quantization.  rh 8/99                          
1305
 *
1306
 *  Robert Hegemann 2000-09-06 rewrite
1307
 *
1308
 ************************************************************************/
1309
 
1310
void 
1311
VBR_iteration_loop (
1312
    lame_global_flags *gfp,
1313
    FLOAT8             pe           [2][2],
1314
    FLOAT8             ms_ener_ratio[2], 
1315
    FLOAT8             xr           [2][2][576],
1316
    III_psy_ratio      ratio        [2][2], 
1317
    int                l3_enc       [2][2][576],
1318
    III_scalefac_t     scalefac     [2][2] )
1319
{
1320
    lame_internal_flags *gfc=gfp->internal_flags;
1321
    III_psy_xmin l3_xmin[2][2];
1322
 
1323
    FLOAT8    xrpow[576];
1324
    int       bands[2][2];
1325
    int       frameBits[15];
1326
    int       bitsPerFrame;
1327
    int       save_bits[2][2];
1328
    int       used_bits, used_bits2;
1329
    int       bits;
1330
    int       min_bits[2][2], max_bits[2][2];
1331
    int       analog_mean_bits, min_mean_bits;
1332
    int       mean_bits;
1333
    int       ch, num_chan, gr, analog_silence;
1334
    int       reduce_s_ch, sfb21_extra;
1335
    gr_info             *cod_info;
1336
    III_side_info_t     *l3_side  = &gfc->l3_side;
1337
 
1338
    if (gfc->mode_ext == MPG_MD_MS_LR && gfp->quality >= 5) {
1339
        /*  my experiences are, that side channel reduction  
1340
         *  does more harm than good when VBR encoding
1341
         *  (Robert.Hegemann@gmx.de 2000-02-18)
1342
         *  2000-09-06: code is enabled at quality level 5
1343
         */
1344
        reduce_s_ch = 1;
1345
        num_chan    = 1;
1346
    } else {
1347
        reduce_s_ch = 0;
1348
        num_chan    = gfc->channels_out;
1349
    }
1350
 
1351
    analog_silence = VBR_prepare (gfp, pe, ms_ener_ratio, xr, ratio, 
1352
                                  l3_xmin, frameBits, &analog_mean_bits,
1353
                                  &min_mean_bits, min_bits, max_bits, bands);
1354
 
1355
    /*---------------------------------*/
1356
    do {  
1357
 
1358
    /*  quantize granules with lowest possible number of bits
1359
     */
1360
 
1361
    used_bits = 0;
1362
    used_bits2 = 0;
1363
 
1364
    for (gr = 0; gr < gfc->mode_gr; gr++) {
1365
        for (ch = 0; ch < num_chan; ch++) {
1366
            int ret; 
1367
            cod_info = &l3_side->gr[gr].ch[ch].tt;
1368
 
1369
            /*  init_outer_loop sets up cod_info, scalefac and xrpow 
1370
             */
1371
            ret = init_outer_loop(cod_info, &scalefac[gr][ch], gfc->is_mpeg1,
1372
				  xr[gr][ch], xrpow);
1373
            if (ret == 0) {
1374
                /*  xr contains no energy 
1375
                 *  l3_enc, our encoding data, will be quantized to zero
1376
                 */
1377
                memset(l3_enc[gr][ch], 0, sizeof(int)*576);
1378
                save_bits[gr][ch] = 0;
1379
                continue; /* with next channel */
1380
            }
1381
#if 0
1382
            if (gfc->mode_ext == MPG_MD_MS_LR && ch == 1)  
1383
                min_bits[gr][ch] = Max (min_bits[gr][ch], save_bits[gr][0]/5);
1384
#endif
1385
 
1386
            if (gfp->VBR == vbr_mtrh) {
1387
                ret = VBR_noise_shaping2 (gfp, xr[gr][ch], xrpow, 
1388
                                        &ratio[gr][ch], l3_enc[gr][ch], 0, 
1389
                                        min_bits[gr][ch], max_bits[gr][ch], 
1390
                                        &scalefac[gr][ch],
1391
                                        &l3_xmin[gr][ch], gr, ch );
1392
                if (ret < 0)
1393
                    cod_info->part2_3_length = 100000;
1394
            } 
1395
            else
1396
                VBR_encode_granule (gfp, cod_info, xr[gr][ch], &l3_xmin[gr][ch],
1397
                                    &scalefac[gr][ch], xrpow, l3_enc[gr][ch],
1398
                                    ch, min_bits[gr][ch], max_bits[gr][ch] );
1399
 
1400
            used_bits += cod_info->part2_3_length;
1401
            save_bits[gr][ch] = Min(MAX_BITS, cod_info->part2_3_length);
1402
            used_bits2 += Min(MAX_BITS, cod_info->part2_3_length);
1403
        } /* for ch */
1404
    }    /* for gr */
1405
 
1406
    /*  special on quality=5, we didn't quantize side channel above
1407
     */
1408
    if (reduce_s_ch) {
1409
        /*  number of bits needed was found for MID channel above.  Use formula
1410
         *  (fixed bitrate code) to set the side channel bits */
1411
        for (gr = 0; gr < gfc->mode_gr; gr++) {
1412
            FLOAT8 fac = .33*(.5-ms_ener_ratio[gr])/.5;
1413
            save_bits[gr][1] = (int)(((1-fac)/(1+fac)) * save_bits[gr][0]);
1414
            save_bits[gr][1] = Max (analog_mean_bits, save_bits[gr][1]);
1415
            used_bits += save_bits[gr][1];
1416
        }
1417
    }
1418
 
1419
    /*  find lowest bitrate able to hold used bits
1420
     */
1421
    if (analog_silence && !gfp->VBR_hard_min) 
1422
        /*  we detected analog silence and the user did not specify 
1423
         *  any hard framesize limit, so start with smallest possible frame
1424
         */
1425
        gfc->bitrate_index = 1;
1426
    else
1427
        gfc->bitrate_index = gfc->VBR_min_bitrate;
1428
 
1429
    for( ; gfc->bitrate_index < gfc->VBR_max_bitrate; gfc->bitrate_index++) {
1430
        if (used_bits <= frameBits[gfc->bitrate_index]) break; 
1431
    }
1432
 
1433
    getframebits (gfp, &bitsPerFrame, &mean_bits);
1434
    bits = ResvFrameBegin (gfp, l3_side, mean_bits, bitsPerFrame);
1435
 
1436
    if (used_bits > bits){
1437
        //printf("# %d used %d have %d\n",gfp->frameNum,used_bits,bits);
1438
        if(gfp->VBR == vbr_mtrh) {        
1439
            for (gr = 0; gr < gfc->mode_gr; gr++) {
1440
                for (ch = 0; ch < gfc->channels_out; ch++) {
1441
                    max_bits[gr][ch] = save_bits[gr][ch];
1442
                    max_bits[gr][ch] *= frameBits[gfc->bitrate_index];
1443
                    max_bits[gr][ch] /= used_bits2;
1444
                    max_bits[gr][ch] = Max(min_bits[gr][ch],max_bits[gr][ch]);
1445
                }
1446
            }
1447
        }
1448
        else {
1449
            for (gr = 0; gr < gfc->mode_gr; gr++) {
1450
                for (ch = 0; ch < gfc->channels_out; ch++) {
1451
                    int sfb;
1452
                    cod_info = &l3_side->gr[gr].ch[ch].tt;
1453
                    if (cod_info->block_type == SHORT_TYPE) {
1454
                        for (sfb = 0; sfb < SBMAX_s; sfb++) {
1455
                            l3_xmin[gr][ch].s[sfb][0] *= 1.+.029*sfb*sfb/SBMAX_s/SBMAX_s;
1456
                            l3_xmin[gr][ch].s[sfb][1] *= 1.+.029*sfb*sfb/SBMAX_s/SBMAX_s;
1457
                            l3_xmin[gr][ch].s[sfb][2] *= 1.+.029*sfb*sfb/SBMAX_s/SBMAX_s;
1458
                        }
1459
                    }
1460
                    else {
1461
                        for (sfb = 0; sfb < SBMAX_l; sfb++) 
1462
                            l3_xmin[gr][ch].l[sfb] *= 1.+.029*sfb*sfb/SBMAX_l/SBMAX_l;
1463
                    }
1464
//                  min_bits[gr][ch] = Max(min_mean_bits, 0.9*min_bits[gr][ch]);
1465
                    max_bits[gr][ch] = Max(min_mean_bits, 0.9*max_bits[gr][ch]);
1466
                }
1467
            }
1468
        }
1469
    }
1470
 
1471
    } while (used_bits > bits);
1472
    /*--------------------------------------*/
1473
 
1474
    /*  ignore sfb21 by the following (maybe) noise shaping  
1475
     */
1476
    sfb21_extra = gfc->sfb21_extra;
1477
    gfc->sfb21_extra = 0;     
1478
 
1479
    /*  quantize granules which violate bit constraints again
1480
     *  and side channel when in quality=5 reduce_side is used
1481
     */  
1482
    for (gr = 0; gr < gfc->mode_gr; gr++) {
1483
        for (ch = 0; ch < gfc->channels_out; ch++) {
1484
            int ret;
1485
            cod_info = &l3_side->gr[gr].ch[ch].tt;
1486
 
1487
            if (used_bits <= bits && ! (reduce_s_ch && ch == 1))
1488
                /*  we have enough bits
1489
                 *  and have already encoded the side channel 
1490
                 */
1491
                continue; /* with next ch */
1492
 
1493
            if (used_bits > bits) {
1494
                /*  repartion available bits in same proportion
1495
                 */
1496
                save_bits[gr][ch] *= frameBits[gfc->bitrate_index];
1497
                save_bits[gr][ch] /= used_bits;
1498
            }
1499
            /*  init_outer_loop sets up cod_info, scalefac and xrpow 
1500
             */
1501
            ret = init_outer_loop(cod_info, &scalefac[gr][ch], gfc->is_mpeg1,
1502
				  xr[gr][ch], xrpow);
1503
            if (ret == 0) 
1504
            {
1505
                /*  xr contains no energy 
1506
                 *  l3_enc, our encoding data, will be quantized to zero
1507
                 */
1508
                memset(l3_enc[gr][ch], 0, sizeof(int)*576);
1509
            }
1510
            else {
1511
                /*  xr contains energy we will have to encode 
1512
                 *  masking abilities were previously calculated
1513
                 *  find some good quantization in outer_loop 
1514
                 */
1515
                outer_loop (gfp, cod_info, xr[gr][ch], &l3_xmin[gr][ch],
1516
                            &scalefac[gr][ch], xrpow, l3_enc[gr][ch], ch,
1517
                            save_bits[gr][ch]);
1518
            }
1519
        } /* ch */
1520
    }  /* gr */
1521
 
1522
    gfc->sfb21_extra = sfb21_extra;
1523
 
1524
    iteration_finish (gfc, xr, l3_enc, ratio, scalefac, mean_bits);
1525
}
1526
 
1527
 
1528
 
1529
 
1530
 
1531
 
1532
/********************************************************************
1533
 *
1534
 *  calc_target_bits()
1535
 *
1536
 *  calculates target bits for ABR encoding
1537
 *
1538
 *  mt 2000/05/31
1539
 *
1540
 ********************************************************************/
1541
 
1542
static void 
1543
calc_target_bits (
1544
    lame_global_flags * gfp,
1545
    FLOAT8               pe            [2][2],
1546
    FLOAT8               ms_ener_ratio [2],
1547
    int                  targ_bits     [2][2],
1548
    int                 *analog_silence_bits,
1549
    int                 *max_frame_bits )
1550
{
1551
    lame_internal_flags *gfc=gfp->internal_flags;
1552
    III_side_info_t *l3_side = &gfc->l3_side;
1553
    FLOAT8 res_factor;
1554
    int gr, ch, totbits, mean_bits, bitsPerFrame;
1555
 
1556
    gfc->bitrate_index = gfc->VBR_max_bitrate;
1557
    getframebits (gfp, &bitsPerFrame, &mean_bits);
1558
    *max_frame_bits = ResvFrameBegin (gfp, l3_side, mean_bits, bitsPerFrame);
1559
 
1560
    gfc->bitrate_index = 1;
1561
    getframebits (gfp, &bitsPerFrame, &mean_bits);
1562
    *analog_silence_bits = mean_bits / gfc->channels_out;
1563
 
1564
    mean_bits  = gfp->VBR_mean_bitrate_kbps * gfp->framesize * 1000;
1565
    mean_bits /= gfp->out_samplerate;
1566
    mean_bits -= gfc->sideinfo_len*8;
1567
    mean_bits /= gfc->mode_gr;
1568
 
1569
    res_factor = .90 + .10 * (11.0 - gfp->compression_ratio) / (11.0 - 5.5);
1570
    if (res_factor <  .90)
1571
        res_factor =  .90; 
1572
    if (res_factor > 1.00) 
1573
        res_factor = 1.00;
1574
 
1575
    for (gr = 0; gr < gfc->mode_gr; gr++) {
1576
        for (ch = 0; ch < gfc->channels_out; ch++) {
1577
            targ_bits[gr][ch] = res_factor * (mean_bits / gfc->channels_out);
1578
 
1579
            if (pe[gr][ch] > 700) {
1580
                int add_bits = (pe[gr][ch] - 700) / 1.4;
1581
 
1582
                gr_info *cod_info = &l3_side->gr[gr].ch[ch].tt;
1583
                targ_bits[gr][ch] = res_factor * (mean_bits / gfc->channels_out);
1584
 
1585
                /* short blocks use a little extra, no matter what the pe */
1586
                if (cod_info->block_type == SHORT_TYPE) {
1587
                    if (add_bits < mean_bits/4) 
1588
                        add_bits = mean_bits/4; 
1589
                }
1590
                /* at most increase bits by 1.5*average */
1591
                if (add_bits > mean_bits*3/4)
1592
                    add_bits = mean_bits*3/4;
1593
                else
1594
                if (add_bits < 0) 
1595
                    add_bits = 0;
1596
 
1597
                targ_bits[gr][ch] += add_bits;
1598
            }
1599
        }/* for ch */
1600
    }   /* for gr */
1601
 
1602
    if (gfc->mode_ext == MPG_MD_MS_LR) 
1603
        for (gr = 0; gr < gfc->mode_gr; gr++) {
1604
            reduce_side (targ_bits[gr], ms_ener_ratio[gr], mean_bits,
1605
			 MAX_BITS);
1606
        }
1607
 
1608
    /*  sum target bits
1609
     */
1610
    totbits=0;
1611
    for (gr = 0; gr < gfc->mode_gr; gr++) {
1612
        for (ch = 0; ch < gfc->channels_out; ch++) {
1613
            if (targ_bits[gr][ch] > MAX_BITS) 
1614
                targ_bits[gr][ch] = MAX_BITS;
1615
            totbits += targ_bits[gr][ch];
1616
        }
1617
    }
1618
 
1619
    /*  repartion target bits if needed
1620
     */
1621
    if (totbits > *max_frame_bits) {
1622
        for(gr = 0; gr < gfc->mode_gr; gr++) {
1623
            for(ch = 0; ch < gfc->channels_out; ch++) {
1624
                targ_bits[gr][ch] *= *max_frame_bits; 
1625
                targ_bits[gr][ch] /= totbits; 
1626
            }
1627
        }
1628
    }
1629
}
1630
 
1631
 
1632
 
1633
 
1634
 
1635
 
1636
/********************************************************************
1637
 *
1638
 *  ABR_iteration_loop()
1639
 *
1640
 *  encode a frame with a disired average bitrate
1641
 *
1642
 *  mt 2000/05/31
1643
 *
1644
 ********************************************************************/
1645
 
1646
void 
1647
ABR_iteration_loop(
1648
    lame_global_flags *gfp,
1649
    FLOAT8             pe           [2][2],
1650
    FLOAT8             ms_ener_ratio[2], 
1651
    FLOAT8             xr           [2][2][576],
1652
    III_psy_ratio      ratio        [2][2], 
1653
    int                l3_enc       [2][2][576],
1654
    III_scalefac_t     scalefac     [2][2] )
1655
{
1656
    lame_internal_flags *gfc=gfp->internal_flags;
1657
    III_psy_xmin l3_xmin;
1658
    FLOAT8    xrpow[576];
1659
    int       targ_bits[2][2];
1660
    int       bitsPerFrame, mean_bits, totbits, max_frame_bits;
1661
    int       ch, gr, ath_over, ret;
1662
    int       analog_silence_bits;
1663
    gr_info             *cod_info = NULL;
1664
    III_side_info_t     *l3_side  = &gfc->l3_side;
1665
 
1666
    calc_target_bits (gfp, pe, ms_ener_ratio, targ_bits, 
1667
                      &analog_silence_bits, &max_frame_bits);
1668
 
1669
    /*  encode granules
1670
     */
1671
    totbits=0;
1672
    for (gr = 0; gr < gfc->mode_gr; gr++) {
1673
 
1674
        if (gfc->mode_ext == MPG_MD_MS_LR) 
1675
            ms_convert (xr[gr], xr[gr]);
1676
 
1677
        for (ch = 0; ch < gfc->channels_out; ch++) {
1678
            cod_info = &l3_side->gr[gr].ch[ch].tt;
1679
 
1680
            /*  cod_info, scalefac and xrpow get initialized in init_outer_loop
1681
             */
1682
            ret = init_outer_loop(cod_info, &scalefac[gr][ch], gfc->is_mpeg1,
1683
				  xr[gr][ch], xrpow);
1684
            if (ret == 0) {
1685
                /*  xr contains no energy 
1686
                 *  l3_enc, our encoding data, will be quantized to zero
1687
                 */
1688
                memset(l3_enc[gr][ch], 0, sizeof(int)*576);
1689
            } 
1690
            else {
1691
                /*  xr contains energy we will have to encode 
1692
                 *  calculate the masking abilities
1693
                 *  find some good quantization in outer_loop 
1694
                 */
1695
                ath_over = calc_xmin (gfp, xr[gr][ch], &ratio[gr][ch],
1696
                                      cod_info, &l3_xmin);
1697
                if (0 == ath_over) /* analog silence */
1698
                    targ_bits[gr][ch] = analog_silence_bits;
1699
 
1700
                outer_loop (gfp, cod_info, xr[gr][ch], &l3_xmin,
1701
                            &scalefac[gr][ch], xrpow, l3_enc[gr][ch],
1702
                            ch, targ_bits[gr][ch]);
1703
            }
1704
 
1705
            totbits += cod_info->part2_3_length;
1706
        } /* ch */
1707
    }  /* gr */
1708
 
1709
    /*  find a bitrate which can handle totbits 
1710
     */
1711
    for (gfc->bitrate_index =  gfc->VBR_min_bitrate ;
1712
         gfc->bitrate_index <= gfc->VBR_max_bitrate;
1713
         gfc->bitrate_index++    ) {
1714
        getframebits (gfp, &bitsPerFrame, &mean_bits);
1715
        max_frame_bits = ResvFrameBegin (gfp, l3_side, mean_bits, bitsPerFrame);
1716
        if (totbits <= max_frame_bits) break; 
1717
    }
1718
    assert (gfc->bitrate_index <= gfc->VBR_max_bitrate);
1719
 
1720
    iteration_finish (gfc, xr, l3_enc, ratio, scalefac, mean_bits);
1721
}
1722
 
1723
 
1724
 
1725
 
1726
 
1727
 
1728
/************************************************************************
1729
 *
1730
 *      iteration_loop()                                                    
1731
 *
1732
 *  author/date??
1733
 *
1734
 *  encodes one frame of MP3 data with constant bitrate
1735
 *
1736
 ************************************************************************/
1737
 
1738
void 
1739
iteration_loop(
1740
    lame_global_flags *gfp, 
1741
    FLOAT8             pe           [2][2],
1742
    FLOAT8             ms_ener_ratio[2],  
1743
    FLOAT8             xr           [2][2][576],
1744
    III_psy_ratio      ratio        [2][2],  
1745
    int                l3_enc       [2][2][576],
1746
    III_scalefac_t     scalefac     [2][2] )
1747
{
1748
    lame_internal_flags *gfc=gfp->internal_flags;
1749
    III_psy_xmin l3_xmin[2];
1750
    FLOAT8 xrpow[576];
1751
    int    targ_bits[2];
1752
    int    bitsPerFrame;
1753
    int    mean_bits, max_bits, bit_rate;
1754
    int    gr, ch, i;
1755
    III_side_info_t     *l3_side = &gfc->l3_side;
1756
    gr_info             *cod_info;
1757
 
1758
    bit_rate = bitrate_table [gfp->version] [gfc->bitrate_index];
1759
    getframebits (gfp, &bitsPerFrame, &mean_bits);
1760
    ResvFrameBegin (gfp, l3_side, mean_bits, bitsPerFrame );
1761
 
1762
    /* quantize! */
1763
    for (gr = 0; gr < gfc->mode_gr; gr++) {
1764
 
1765
        /*  calculate needed bits
1766
         */
1767
        max_bits = on_pe (gfp, pe, l3_side, targ_bits, mean_bits, gr);
1768
 
1769
        if (gfc->mode_ext == MPG_MD_MS_LR) {
1770
            ms_convert (xr[gr], xr[gr]);
1771
            reduce_side (targ_bits, ms_ener_ratio[gr], mean_bits, max_bits);
1772
        }
1773
 
1774
        for (ch=0 ; ch < gfc->channels_out ; ch ++) {
1775
            cod_info = &l3_side->gr[gr].ch[ch].tt; 
1776
 
1777
            /*  init_outer_loop sets up cod_info, scalefac and xrpow 
1778
             */
1779
            i = init_outer_loop(cod_info, &scalefac[gr][ch], gfc->is_mpeg1,
1780
				xr[gr][ch], xrpow);
1781
            if (i == 0) {
1782
                /*  xr contains no energy, l3_enc will be quantized to zero
1783
                 */
1784
                memset(l3_enc[gr][ch], 0, sizeof(int)*576);
1785
            }
1786
            else {
1787
                /*  xr contains energy we will have to encode 
1788
                 *  calculate the masking abilities
1789
                 *  find some good quantization in outer_loop 
1790
                 */
1791
                calc_xmin (gfp, xr[gr][ch], &ratio[gr][ch], cod_info, 
1792
                           &l3_xmin[ch]);
1793
                outer_loop (gfp, cod_info, xr[gr][ch], &l3_xmin[ch], 
1794
                            &scalefac[gr][ch], xrpow, l3_enc[gr][ch],
1795
                            ch, targ_bits[ch]);
1796
            }
1797
            assert (cod_info->part2_3_length <= MAX_BITS);
1798
 
1799
            /*  try some better scalefac storage
1800
             */
1801
            best_scalefac_store (gfc, gr, ch, l3_enc, l3_side, scalefac);
1802
 
1803
            /*  best huffman_divide may save some bits too
1804
             */
1805
            if (gfc->use_best_huffman == 1) 
1806
                best_huffman_divide (gfc, gr, ch, cod_info, l3_enc[gr][ch]);
1807
 
1808
            /*  update reservoir status after FINAL quantization/bitrate
1809
             */
1810
#undef  NORES_TEST
1811
#ifndef NORES_TEST
1812
            ResvAdjust (gfc, cod_info, l3_side, mean_bits);
1813
#endif      
1814
            /*  set the sign of l3_enc from the sign of xr
1815
             */
1816
            for (i = 0; i < 576; i++) {
1817
                if (xr[gr][ch][i] < 0) l3_enc[gr][ch][i] *= -1; 
1818
            }
1819
        } /* for ch */
1820
    }    /* for gr */
1821
 
1822
#ifdef NORES_TEST
1823
    /* replace ResvAdjust above with this code if you do not want
1824
       the second granule to use bits saved by the first granule.
1825
       Requires using the --nores.  This is useful for testing only */
1826
    for (gr = 0; gr < gfc->mode_gr; gr++) {
1827
        for (ch =  0; ch < gfc->channels_out; ch++) {
1828
            cod_info = &l3_side->gr[gr].ch[ch].tt;
1829
            ResvAdjust (gfc, cod_info, l3_side, mean_bits);
1830
        }
1831
    }
1832
#endif
1833
 
1834
    ResvFrameEnd (gfc, l3_side, mean_bits);
1835
}
1836
 
1837
 
1838