polygon intersector: added horizontal line reconstruction
[swftools.git] / lib / lame / lame.c
1 /* -*- mode: C; mode: fold -*- */
2 /*
3  *      LAME MP3 encoding engine
4  *
5  *      Copyright (c) 1999 Mark Taylor
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /* $Id: lame.c,v 1.2 2006/02/09 16:56:23 kramm Exp $ */
24
25 #include <stdlib.h>
26 #include "config_static.h"
27
28
29 #include <assert.h>
30 #include "lame-analysis.h"
31 #include "lame.h"
32 #include "util.h"
33 #include "bitstream.h"
34 #include "version.h"
35 #include "tables.h"
36 #include "quantize_pvt.h"
37 #include "VbrTag.h"
38
39 #if defined(__FreeBSD__) && !defined(__alpha__)
40 #include <floatingpoint.h>
41 #endif
42 #ifdef __riscos__
43 #include "asmstuff.h"
44 #endif
45
46 #ifdef WITH_DMALLOC
47 #include <dmalloc.h>
48 #endif
49
50
51 static void
52 lame_init_params_ppflt_lowpass(FLOAT8 amp_lowpass[32], FLOAT lowpass1,
53                                FLOAT lowpass2, int *lowpass_band,
54                                int *minband, int *maxband)
55 {
56     int     band;
57     FLOAT8  freq;
58
59     for (band = 0; band <= 31; band++) {
60         freq = band / 31.0;
61         amp_lowpass[band] = 1;
62         /* this band and above will be zeroed: */
63         if (freq >= lowpass2) {
64             *lowpass_band = Min(*lowpass_band, band);
65             amp_lowpass[band] = 0;
66         }
67         if (lowpass1 < freq && freq < lowpass2) {
68             *minband = Min(*minband, band);
69             *maxband = Max(*maxband, band);
70             amp_lowpass[band] = cos((PI / 2) *
71                                     (lowpass1 - freq) / (lowpass2 - lowpass1));
72         }
73         /*
74          * DEBUGF("lowpass band=%i  amp=%f \n",
75          *      band, gfc->amp_lowpass[band]);
76          */
77     }
78 }
79
80 /* lame_init_params_ppflt */
81
82 /*}}}*/
83 /* static void   lame_init_params_ppflt         (lame_internal_flags *gfc)                                                                                        *//*{{{ */
84
85 static void
86 lame_init_params_ppflt(lame_global_flags * gfp)
87 {
88     lame_internal_flags *gfc = gfp->internal_flags;
89   /***************************************************************/
90     /* compute info needed for polyphase filter (filter type==0, default) */
91   /***************************************************************/
92
93     int     band, maxband, minband;
94     FLOAT8  freq;
95
96     if (gfc->lowpass1 > 0) {
97         minband = 999;
98         maxband = -1;
99         lame_init_params_ppflt_lowpass(gfc->amp_lowpass,
100                                        gfc->lowpass1, gfc->lowpass2,
101                                        &gfc->lowpass_band, &minband, &maxband);
102         /* compute the *actual* transition band implemented by
103          * the polyphase filter */
104         if (minband == 999) {
105             gfc->lowpass1 = (gfc->lowpass_band - .75) / 31.0;
106         }
107         else {
108             gfc->lowpass1 = (minband - .75) / 31.0;
109         }
110         gfc->lowpass2 = gfc->lowpass_band / 31.0;
111
112         gfc->lowpass_start_band = minband;
113         gfc->lowpass_end_band = maxband;
114
115         /* as the lowpass may have changed above
116          * calculate the amplification here again
117          */
118         for (band = minband; band <= maxband; band++) {
119             freq = band / 31.0;
120             gfc->amp_lowpass[band] =
121                 cos((PI / 2) * (gfc->lowpass1 - freq) /
122                     (gfc->lowpass2 - gfc->lowpass1));
123         }
124     }
125     else {
126         gfc->lowpass_start_band = 0;
127         gfc->lowpass_end_band = -1; /* do not to run into for-loops */
128     }
129
130     /* make sure highpass filter is within 90% of what the effective
131      * highpass frequency will be */
132     if (gfc->highpass2 > 0) {
133         if (gfc->highpass2 < .9 * (.75 / 31.0)) {
134             gfc->highpass1 = 0;
135             gfc->highpass2 = 0;
136             MSGF(gfc, "Warning: highpass filter disabled.  "
137                  "highpass frequency too small\n");
138         }
139     }
140
141     if (gfc->highpass2 > 0) {
142         minband = 999;
143         maxband = -1;
144         for (band = 0; band <= 31; band++) {
145             freq = band / 31.0;
146             gfc->amp_highpass[band] = 1;
147             /* this band and below will be zereod */
148             if (freq <= gfc->highpass1) {
149                 gfc->highpass_band = Max(gfc->highpass_band, band);
150                 gfc->amp_highpass[band] = 0;
151             }
152             if (gfc->highpass1 < freq && freq < gfc->highpass2) {
153                 minband = Min(minband, band);
154                 maxband = Max(maxband, band);
155                 gfc->amp_highpass[band] =
156                     cos((PI / 2) *
157                         (gfc->highpass2 - freq) /
158                         (gfc->highpass2 - gfc->highpass1));
159             }
160             /*
161                DEBUGF("highpass band=%i  amp=%f \n",
162                band, gfc->amp_highpass[band]);
163              */
164         }
165         /* compute the *actual* transition band implemented by
166          * the polyphase filter */
167         gfc->highpass1 = gfc->highpass_band / 31.0;
168         if (maxband == -1) {
169             gfc->highpass2 = (gfc->highpass_band + .75) / 31.0;
170         }
171         else {
172             gfc->highpass2 = (maxband + .75) / 31.0;
173         }
174
175         gfc->highpass_start_band = minband;
176         gfc->highpass_end_band = maxband;
177
178         /* as the highpass may have changed above
179          * calculate the amplification here again
180          */
181         for (band = minband; band <= maxband; band++) {
182             freq = band / 31.0;
183             gfc->amp_highpass[band] =
184                 cos((PI / 2) * (gfc->highpass2 - freq) /
185                     (gfc->highpass2 - gfc->highpass1));
186         }
187     }
188     else {
189         gfc->highpass_start_band = 0;
190         gfc->highpass_end_band = -1; /* do not to run into for-loops */
191     }
192     /*
193        DEBUGF("lowpass band with amp=0:  %i \n",gfc->lowpass_band);
194        DEBUGF("highpass band with amp=0:  %i \n",gfc->highpass_band);
195        DEBUGF("lowpass band start:  %i \n",gfc->lowpass_start_band);
196        DEBUGF("lowpass band end:    %i \n",gfc->lowpass_end_band);
197        DEBUGF("highpass band start:  %i \n",gfc->highpass_start_band);
198        DEBUGF("highpass band end:    %i \n",gfc->highpass_end_band);
199      */
200 }
201
202 /*}}}*/
203
204
205 static void
206 optimum_bandwidth(double *const lowerlimit,
207                   double *const upperlimit,
208                   const unsigned bitrate,
209                   const int samplefreq,
210                   const double channels)
211 {
212 /* 
213  *  Input:
214  *      bitrate     total bitrate in bps
215  *      samplefreq  output sampling frequency in Hz
216  *      channels    1 for mono, 2+epsilon for MS stereo, 3 for LR stereo
217  *                  epsilon is the percentage of LR frames for typical audio
218  *                  (I use 'Fade to Gray' by Metallica)
219  *
220  *   Output:
221  *      lowerlimit: best lowpass frequency limit for input filter in Hz
222  *      upperlimit: best highpass frequency limit for input filter in Hz
223  */
224     double  f_low;
225     double  f_high;
226     double  br;
227
228     assert(bitrate >= 8000 && bitrate <= 320000);
229     assert(samplefreq >= 8000 && samplefreq <= 48000);
230     assert(channels == 1 || (channels >= 2 && channels <= 3));
231
232     if (samplefreq >= 32000)
233         br =
234             bitrate - (channels ==
235                        1 ? (17 + 4) * 8 : (32 + 4) * 8) * samplefreq / 1152;
236     else
237         br =
238             bitrate - (channels ==
239                        1 ? (9 + 4) * 8 : (17 + 4) * 8) * samplefreq / 576;
240
241     if (channels >= 2.)
242         br /= 1.75 + 0.25 * (channels - 2.); // MS needs 1.75x mono, LR needs 2.00x mono (experimental data of a lot of albums)
243
244     br *= 0.5;          // the sine and cosine term must share the bitrate
245
246 /* 
247  *  So, now we have the bitrate for every spectral line.
248  *  Let's look at the current settings:
249  *
250  *    Bitrate   limit    bits/line
251  *     8 kbps   0.34 kHz  4.76
252  *    16 kbps   1.9 kHz   2.06
253  *    24 kbps   2.8 kHz   2.21
254  *    32 kbps   3.85 kHz  2.14
255  *    40 kbps   5.1 kHz   2.06
256  *    48 kbps   5.6 kHz   2.21
257  *    56 kbps   7.0 kHz   2.10
258  *    64 kbps   7.7 kHz   2.14
259  *    80 kbps  10.1 kHz   2.08
260  *    96 kbps  11.2 kHz   2.24
261  *   112 kbps  14.0 kHz   2.12
262  *   128 kbps  15.4 kHz   2.17
263  *   160 kbps  18.2 kHz   2.05
264  *   192 kbps  21.1 kHz   2.14
265  *   224 kbps  22.0 kHz   2.41
266  *   256 kbps  22.0 kHz   2.78
267  *
268  *   What can we see?
269  *       Value for 8 kbps is nonsense (although 8 kbps and stereo is nonsense)
270  *       Values are between 2.05 and 2.24 for 16...192 kbps
271  *       Some bitrate lack the following bitrates have: 16, 40, 80, 160 kbps
272  *       A lot of bits per spectral line have: 24, 48, 96 kbps
273  *
274  *   What I propose?
275  *       A slightly with the bitrate increasing bits/line function. It is
276  *       better to decrease NMR for low bitrates to get a little bit more
277  *       bandwidth. So we have a better trade off between twickling and
278  *       muffled sound.
279  */
280
281     f_low = br / log10(br * 4.425e-3); // Tests with 8, 16, 32, 64, 112 and 160 kbps
282  
283 /*
284 GB 04/04/01
285 sfb21 is a huge bitrate consumer in vbr with the new ath.
286 Need to reduce the lowpass to more reasonable values. This extra lowpass
287 won't reduce quality over 3.87 as the previous ath was doing this lowpass
288 */
289 /*GB 22/05/01
290 I'm also extending this to CBR as tests showed that a
291 limited bandwidth is increasing quality
292 */
293     if (f_low>18400)
294             f_low = 18400+(f_low-18400)/4;
295
296 /*
297  *  What we get now?
298  *
299  *    Bitrate       limit  bits/line    difference
300  *     8 kbps (8)  1.89 kHz  0.86          +1.6 kHz
301  *    16 kbps (8)  3.16 kHz  1.24          +1.2 kHz
302  *    32 kbps(16)  5.08 kHz  1.54          +1.2 kHz
303  *    56 kbps(22)  7.88 kHz  1.80          +0.9 kHz
304  *    64 kbps(22)  8.83 kHz  1.86          +1.1 kHz
305  *   112 kbps(32) 14.02 kHz  2.12           0.0 kHz
306  *   112 kbps(44) 13.70 kHz  2.11          -0.3 kHz
307  *   128 kbps     15.40 kHz  2.17           0.0 kHz
308  *   160 kbps     16.80 kHz  2.22          -1.4 kHz 
309  *   192 kbps     19.66 kHz  2.30          -1.4 kHz
310  *   256 kbps     22.05 kHz  2.78           0.0 kHz
311  */
312
313
314 /*
315  *  Now we try to choose a good high pass filtering frequency.
316  *  This value is currently not used.
317  *    For fu < 16 kHz:  sqrt(fu*fl) = 560 Hz
318  *    For fu = 18 kHz:  no high pass filtering
319  *  This gives:
320  *
321  *   2 kHz => 160 Hz
322  *   3 kHz => 107 Hz
323  *   4 kHz =>  80 Hz
324  *   8 kHz =>  40 Hz
325  *  16 kHz =>  20 Hz
326  *  17 kHz =>  10 Hz
327  *  18 kHz =>   0 Hz
328  *
329  *  These are ad hoc values and these can be optimized if a high pass is available.
330  */
331     if (f_low <= 16000)
332         f_high = 16000. * 20. / f_low;
333     else if (f_low <= 18000)
334         f_high = 180. - 0.01 * f_low;
335     else
336         f_high = 0.;
337
338     /*  
339      *  When we sometimes have a good highpass filter, we can add the highpass
340      *  frequency to the lowpass frequency
341      */
342
343     if (lowerlimit != NULL)
344         *lowerlimit = (f_low>0.5 * samplefreq ? 0.5 * samplefreq : f_low); // roel - fixes mono "-b320 -a"
345     if (upperlimit != NULL)
346         *upperlimit = f_high;
347 /*
348  * Now the weak points:
349  *
350  *   - the formula f_low=br/log10(br*4.425e-3) is an ad hoc formula
351  *     (but has a physical background and is easy to tune)
352  *   - the switch to the ATH based bandwidth selecting is the ad hoc
353  *     value of 128 kbps
354  */
355 }
356
357 static int
358 optimum_samplefreq(int lowpassfreq, int input_samplefreq)
359 {
360 /*
361  * Rules:
362  *
363  *  - output sample frequency should NOT be decreased by more than 3% if lowpass allows this
364  *  - if possible, sfb21 should NOT be used
365  *
366  *  Problem: Switches to 32 kHz at 112 kbps
367  */
368     if (input_samplefreq <= 8000 * 1.03 || lowpassfreq <= 3622)
369         return 8000;
370     if (input_samplefreq <= 11025 * 1.03 || lowpassfreq <= 4991)
371         return 11025;
372     if (input_samplefreq <= 12000 * 1.03 || lowpassfreq <= 5620)
373         return 12000;
374     if (input_samplefreq <= 16000 * 1.03 || lowpassfreq <= 7244)
375         return 16000;
376     if (input_samplefreq <= 22050 * 1.03 || lowpassfreq <= 9982)
377         return 22050;
378     if (input_samplefreq <= 24000 * 1.03 || lowpassfreq <= 11240)
379         return 24000;
380     if (input_samplefreq <= 32000 * 1.03 || lowpassfreq <= 15264)
381         return 32000;
382     if (input_samplefreq <= 44100 * 1.03)
383         return 44100;
384     return 48000;
385 }
386
387
388 /* set internal feature flags.  USER should not access these since
389  * some combinations will produce strange results */
390 void
391 lame_init_qval(lame_global_flags * gfp)
392 {
393     lame_internal_flags *gfc = gfp->internal_flags;
394
395     switch (gfp->quality) {
396     case 9:            /* no psymodel, no noise shaping */
397         gfc->filter_type = 0;
398         gfc->psymodel = 0;
399         gfc->quantization = 0;
400         gfc->noise_shaping = 0;
401         gfc->noise_shaping_amp = 0;
402         gfc->noise_shaping_stop = 0;
403         gfc->use_best_huffman = 0;
404         break;
405
406     case 8:
407         gfp->quality = 7;
408     case 7:            /* use psymodel (for short block and m/s switching), but no noise shapping */
409         gfc->filter_type = 0;
410         gfc->psymodel = 1;
411         gfc->quantization = 0;
412         gfc->noise_shaping = 0;
413         gfc->noise_shaping_amp = 0;
414         gfc->noise_shaping_stop = 0;
415         gfc->use_best_huffman = 0;
416         break;
417
418     case 6:
419         gfp->quality = 5;
420     case 5:            /* the default */
421         gfc->filter_type = 0;
422         gfc->psymodel = 1;
423         gfc->quantization = 0;
424         gfc->noise_shaping = 1;
425          /**/ gfc->noise_shaping_amp = 0;
426         gfc->noise_shaping_stop = 0;
427         gfc->use_best_huffman = 0;
428         break;
429
430     case 4:
431         gfp->quality = 3;
432     case 3:
433         gfc->filter_type = 0;
434         gfc->psymodel = 1;
435         gfc->quantization = 1;
436         gfc->noise_shaping = 1;
437         gfc->noise_shaping_amp = 0;
438         gfc->noise_shaping_stop = 0;
439         gfc->use_best_huffman = 1;
440         break;
441
442     case 2:
443         gfc->filter_type = 0;
444         gfc->psymodel = 1;
445         gfc->quantization = 1;
446         gfc->noise_shaping = 1;
447         gfc->noise_shaping_amp = 1;
448         gfc->noise_shaping_stop = 1;
449         gfc->use_best_huffman = 1;
450         break;
451
452     case 1:
453         gfc->filter_type = 0;
454         gfc->psymodel = 1;
455         gfc->quantization = 1;
456         gfc->noise_shaping = 1;
457         gfc->noise_shaping_amp = 2;
458         gfc->noise_shaping_stop = 1;
459         gfc->use_best_huffman = 1;
460         break;
461
462     case 0:            /* 0..1 quality */
463         gfc->filter_type = 0; /* 1 not yet coded */
464         gfc->psymodel = 1;
465         gfc->quantization = 1;
466         gfc->noise_shaping = 1; /* 2=usually lowers quality */
467         gfc->noise_shaping_amp = 3;
468         gfc->noise_shaping_stop = 1;
469         gfc->use_best_huffman = 1; /* 2 not yet coded */
470     }
471
472     /* modifications to the above rules: */
473
474     /* -Z option toggles scalefactor_scale: */
475     if ( (gfp->experimentalZ & 1) > 0) {
476         gfc->noise_shaping = 2;
477     }
478 }
479
480
481
482
483
484
485
486 /* int           lame_init_params               (lame_global_flags *gfp)                                                                                          *//*{{{ */
487
488 /********************************************************************
489  *   initialize internal params based on data in gf
490  *   (globalflags struct filled in by calling program)
491  *
492  *  OUTLINE:
493  *
494  * We first have some complex code to determine bitrate, 
495  * output samplerate and mode.  It is complicated by the fact
496  * that we allow the user to set some or all of these parameters,
497  * and need to determine best possible values for the rest of them:
498  *
499  *  1. set some CPU related flags
500  *  2. check if we are mono->mono, stereo->mono or stereo->stereo
501  *  3.  compute bitrate and output samplerate:
502  *          user may have set compression ratio
503  *          user may have set a bitrate  
504  *          user may have set a output samplerate
505  *  4. set some options which depend on output samplerate
506  *  5. compute the actual compression ratio
507  *  6. set mode based on compression ratio
508  *
509  *  The remaining code is much simpler - it just sets options
510  *  based on the mode & compression ratio: 
511  *   
512  *   set allow_diff_short based on mode
513  *   select lowpass filter based on compression ratio & mode
514  *   set the bitrate index, and min/max bitrates for VBR modes
515  *   disable VBR tag if it is not appropriate
516  *   initialize the bitstream
517  *   initialize scalefac_band data
518  *   set sideinfo_len (based on channels, CRC, out_samplerate)
519  *   write an id3v2 tag into the bitstream
520  *   write VBR tag into the bitstream
521  *   set mpeg1/2 flag
522  *   estimate the number of frames (based on a lot of data)
523  *         
524  *   now we set more flags:
525  *   nspsytune:
526  *      see code
527  *   VBR modes
528  *      see code      
529  *   CBR/ABR
530  *      see code   
531  *
532  *  Finally, we set the algorithm flags based on the gfp->quality value
533  *  lame_init_qval(gfp);
534  *
535  ********************************************************************/
536 int
537 lame_init_params(lame_global_flags * const gfp)
538 {
539
540     int     i;
541     int     j;
542     lame_internal_flags *gfc = gfp->internal_flags;
543
544     gfc->gfp = gfp;
545
546     gfc->Class_ID = 0;
547
548     /* report functions */
549     gfc->report.msgf   = gfp->report.msgf;
550     gfc->report.debugf = gfp->report.debugf;
551     gfc->report.errorf = gfp->report.errorf;
552
553     gfc->CPU_features.i387 = has_i387();
554     gfc->CPU_features.AMD_3DNow = has_3DNow();
555     gfc->CPU_features.MMX = has_MMX();
556     gfc->CPU_features.SIMD = has_SIMD();
557     gfc->CPU_features.SIMD2 = has_SIMD2();
558
559
560     if (NULL == gfc->ATH)
561         gfc->ATH = calloc(1, sizeof(ATH_t));
562
563     if (NULL == gfc->ATH)
564         return -2;  // maybe error codes should be enumerated in lame.h ??
565
566     if (NULL == gfc->VBR)
567         gfc->VBR = calloc(1, sizeof(VBR_t));
568     if (NULL == gfc->VBR)
569         return -2;
570         
571     if (NULL == gfc->PSY)
572         gfc->PSY = calloc(1, sizeof(PSY_t));
573     if (NULL == gfc->PSY)
574         return -2;
575         
576 #ifdef KLEMM_44
577     /* Select the fastest functions for this CPU */
578     init_scalar_functions(gfc);
579 #endif
580
581     gfc->channels_in = gfp->num_channels;
582     if (gfc->channels_in == 1)
583         gfp->mode = MONO;
584     gfc->channels_out = (gfp->mode == MONO) ? 1 : 2;
585     gfc->mode_ext = MPG_MD_LR_LR;
586     if (gfp->mode == MONO)
587         gfp->force_ms = 0; // don't allow forced mid/side stereo for mono output
588
589
590     if (gfp->VBR != vbr_off) {
591         gfp->free_format = 0; /* VBR can't be mixed with free format */
592     }
593
594     if (gfp->VBR == vbr_off && gfp->brate == 0) {
595         /* no bitrate or compression ratio specified, use a compression ratio of 11.025 */
596         if (gfp->compression_ratio == 0)
597             gfp->compression_ratio = 11.025; /* rate to compress a CD down to exactly 128000 bps */
598     }
599
600
601     if (gfp->VBR == vbr_off && gfp->brate == 0) {
602         /* no bitrate or compression ratio specified, use 11.025 */
603         if (gfp->compression_ratio == 0)
604             gfp->compression_ratio = 11.025; /* rate to compress a CD down to exactly 128000 bps */
605     }
606
607     /* find bitrate if user specify a compression ratio */
608     if (gfp->VBR == vbr_off && gfp->compression_ratio > 0) {
609
610         if (gfp->out_samplerate == 0)
611             gfp->out_samplerate = map2MP3Frequency( (int)( 0.97 * gfp->in_samplerate ) ); /* round up with a margin of 3% */
612
613         /* choose a bitrate for the output samplerate which achieves
614          * specified compression ratio 
615          */
616         gfp->brate =
617             gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 *
618                                                             gfp->
619                                                             compression_ratio);
620
621         /* we need the version for the bitrate table look up */
622         gfc->samplerate_index = SmpFrqIndex(gfp->out_samplerate, &gfp->version);
623
624         if (!gfp->free_format) /* for non Free Format find the nearest allowed bitrate */
625             gfp->brate =
626                 FindNearestBitrate(gfp->brate, gfp->version,
627                                    gfp->out_samplerate);
628     }
629
630     if (gfp->VBR != vbr_off && gfp->brate >= 320)
631         gfp->VBR = vbr_off; /* at 160 kbps (MPEG-2/2.5)/ 320 kbps (MPEG-1) only Free format or CBR are possible, no VBR */
632
633
634     if (gfp->out_samplerate == 0) { /* if output sample frequency is not given, find an useful value */
635         gfp->out_samplerate = map2MP3Frequency( (int)( 0.97 * gfp->in_samplerate ) );
636
637
638         /* check if user specified bitrate requires downsampling, if compression    */
639         /* ratio is > 13, choose a new samplerate to get the ratio down to about 10 */
640
641         if (gfp->VBR == vbr_off && gfp->brate > 0) {
642             gfp->compression_ratio =
643                 gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 *
644                                                                 gfp->brate);
645             if (gfp->compression_ratio > 13.)
646                 gfp->out_samplerate =
647                     map2MP3Frequency( (int)( (10. * 1.e3 * gfp->brate) /
648                                      (16 * gfc->channels_out)));
649         }
650         if (gfp->VBR == vbr_abr) {
651             gfp->compression_ratio =
652                 gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 *
653                                                                 gfp->
654                                                                 VBR_mean_bitrate_kbps);
655             if (gfp->compression_ratio > 13.)
656                 gfp->out_samplerate =
657                     map2MP3Frequency((int)((10. * 1.e3 * gfp->VBR_mean_bitrate_kbps) /
658                                      (16 * gfc->channels_out)));
659         }
660     }
661
662     if (gfp->ogg) {
663         gfp->framesize = 1024;
664         gfp->encoder_delay = ENCDELAY;
665         gfc->coding = coding_Ogg_Vorbis;
666     }
667     else {
668         gfc->mode_gr = gfp->out_samplerate <= 24000 ? 1 : 2; // Number of granules per frame
669         gfp->framesize = 576 * gfc->mode_gr;
670         gfp->encoder_delay = ENCDELAY;
671         gfc->coding = coding_MPEG_Layer_3;
672     }
673
674     gfc->frame_size = gfp->framesize;
675
676     gfc->resample_ratio = (double) gfp->in_samplerate / gfp->out_samplerate;
677
678     /* 
679      *  sample freq       bitrate     compression ratio
680      *     [kHz]      [kbps/channel]   for 16 bit input
681      *     44.1            56               12.6
682      *     44.1            64               11.025
683      *     44.1            80                8.82
684      *     22.05           24               14.7
685      *     22.05           32               11.025
686      *     22.05           40                8.82
687      *     16              16               16.0
688      *     16              24               10.667
689      *
690      */
691     /* 
692      *  For VBR, take a guess at the compression_ratio. 
693      *  For example:
694      *
695      *    VBR_q    compression     like
696      *     -        4.4         320 kbps/44 kHz
697      *   0...1      5.5         256 kbps/44 kHz
698      *     2        7.3         192 kbps/44 kHz
699      *     4        8.8         160 kbps/44 kHz
700      *     6       11           128 kbps/44 kHz
701      *     9       14.7          96 kbps
702      *
703      *  for lower bitrates, downsample with --resample
704      */
705
706     switch (gfp->VBR) {
707     case vbr_mt:
708     case vbr_rh:
709     case vbr_mtrh:
710         {
711             FLOAT8  cmp[] = { 5.7, 6.5, 7.3, 8.2, 9.1, 10, 11, 12, 13, 14 };
712             gfp->compression_ratio = cmp[gfp->VBR_q];
713         }
714         break;
715     case vbr_abr:
716         gfp->compression_ratio =
717             gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 *
718                                                             gfp->
719                                                             VBR_mean_bitrate_kbps);
720         break;
721     default:
722         gfp->compression_ratio =
723             gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 * gfp->brate);
724         break;
725     }
726
727
728     /* mode = -1 (not set by user) or 
729      * mode = MONO (because of only 1 input channel).  
730      * If mode has been set, then select between STEREO or J-STEREO
731      * At higher quality (lower compression) use STEREO instead of J-STEREO.
732      * (unless the user explicitly specified a mode)
733      *
734      * The threshold to switch to STEREO is:
735      *    48 kHz:   171 kbps (used at 192+)
736      *    44.1 kHz: 160 kbps (used at 160+)
737      *    32 kHz:   119 kbps (used at 128+)
738      *
739      *   Note, that for 32 kHz/128 kbps J-STEREO FM recordings sound much
740      *   better than STEREO, so I'm not so very happy with that. 
741      *   fs < 32 kHz I have not tested.
742      */
743     if (gfp->mode == NOT_SET) {
744         if (gfp->compression_ratio < 8)
745             gfp->mode = STEREO;
746         else
747             gfp->mode = JOINT_STEREO;
748     }
749
750     /* KLEMM's jstereo with ms threshold adjusted via compression ratio */
751     if (gfp->mode_automs) {
752         if (gfp->mode != MONO && gfp->compression_ratio < 6.6)
753             gfp->mode = STEREO;
754     }
755
756
757
758
759
760
761   /****************************************************************/
762   /* if a filter has not been enabled, see if we should add one: */
763   /****************************************************************/
764     if (gfp->lowpassfreq == 0) {
765         double  lowpass;
766         double  highpass;
767         double  channels;
768
769         switch (gfp->mode) {
770         case MONO:
771             channels = 1.;
772             break;
773         case JOINT_STEREO:
774             channels = 2. + 0.00;
775             break;
776         case DUAL_CHANNEL:
777         case STEREO:
778             channels = 3.;
779             break;
780         default:    
781             channels = 1.;  // just to make data flow analysis happy :-)
782             assert(0);
783             break;
784         }
785
786         optimum_bandwidth(&lowpass,
787                           &highpass,
788                           gfp->out_samplerate * 16 * gfc->channels_out /
789                           gfp->compression_ratio, gfp->out_samplerate, channels);
790                         
791 /*  roel - is this still needed?
792                 if (lowpass > 0.5 * gfp->out_samplerate) {
793             //MSGF(gfc,"Lowpass @ %7.1f Hz\n", lowpass);
794             gfc->lowpass1 = gfc->lowpass2 =
795                 lowpass / (0.5 * gfp->out_samplerate);
796         }
797 */
798         gfp->lowpassfreq = lowpass;
799
800 #if 0
801         if (gfp->out_samplerate !=
802             optimum_samplefreq(lowpass, gfp->in_samplerate)) {
803             MSGF(gfc,
804                  "I would suggest to use %u Hz instead of %u Hz sample frequency\n",
805                  optimum_samplefreq(lowpass, gfp->in_samplerate),
806                  gfp->out_samplerate);
807         }
808         fflush(stderr);
809 #endif
810     }
811
812     /* apply user driven high pass filter */
813     if (gfp->highpassfreq > 0) {
814         gfc->highpass1 = 2. * gfp->highpassfreq / gfp->out_samplerate; /* will always be >=0 */
815         if (gfp->highpasswidth >= 0)
816             gfc->highpass2 =
817                 2. * (gfp->highpassfreq +
818                       gfp->highpasswidth) / gfp->out_samplerate;
819         else            /* 0% above on default */
820             gfc->highpass2 =
821                 (1 + 0.00) * 2. * gfp->highpassfreq / gfp->out_samplerate;
822     }
823
824     /* apply user driven low pass filter */
825     if (gfp->lowpassfreq > 0) {
826         gfc->lowpass2 = 2. * gfp->lowpassfreq / gfp->out_samplerate; /* will always be >=0 */
827         if (gfp->lowpasswidth >= 0) {
828             gfc->lowpass1 =
829                 2. * (gfp->lowpassfreq -
830                       gfp->lowpasswidth) / gfp->out_samplerate;
831             if (gfc->lowpass1 < 0) /* has to be >= 0 */
832                 gfc->lowpass1 = 0;
833         }
834         else {          /* 0% below on default */
835             gfc->lowpass1 =
836                 (1 - 0.00) * 2. * gfp->lowpassfreq / gfp->out_samplerate;
837         }
838     }
839
840
841
842
843   /**********************************************************************/
844   /* compute info needed for polyphase filter (filter type==0, default) */
845   /**********************************************************************/
846     lame_init_params_ppflt(gfp);
847
848
849   /*******************************************************/
850   /* compute info needed for FIR filter (filter_type==1) */
851   /*******************************************************/
852    /* not yet coded */
853
854
855
856   /*******************************************************
857    * samplerate and bitrate index
858    *******************************************************/
859     gfc->samplerate_index = SmpFrqIndex(gfp->out_samplerate, &gfp->version);
860     if (gfc->samplerate_index < 0)
861         return -1;
862
863     if (gfp->VBR == vbr_off) {
864         if (gfp->free_format) {
865             gfc->bitrate_index = 0;
866         }
867         else {
868             gfc->bitrate_index = BitrateIndex(gfp->brate, gfp->version,
869                                               gfp->out_samplerate);
870             if (gfc->bitrate_index < 0)
871                 return -1;
872         }
873     }
874     else {              /* choose a min/max bitrate for VBR */
875         /* if the user didn't specify VBR_max_bitrate: */
876         gfc->VBR_min_bitrate = 1; /* default: allow   8 kbps (MPEG-2) or  32 kbps (MPEG-1) */
877         gfc->VBR_max_bitrate = 14; /* default: allow 160 kbps (MPEG-2) or 320 kbps (MPEG-1) */
878
879         if (gfp->VBR_min_bitrate_kbps)
880             if (
881                 (gfc->VBR_min_bitrate =
882                  BitrateIndex(gfp->VBR_min_bitrate_kbps, gfp->version,
883                               gfp->out_samplerate)) < 0) return -1;
884         if (gfp->VBR_max_bitrate_kbps)
885             if (
886                 (gfc->VBR_max_bitrate =
887                  BitrateIndex(gfp->VBR_max_bitrate_kbps, gfp->version,
888                               gfp->out_samplerate)) < 0) return -1;
889
890         gfp->VBR_min_bitrate_kbps =
891             bitrate_table[gfp->version][gfc->VBR_min_bitrate];
892         gfp->VBR_max_bitrate_kbps =
893             bitrate_table[gfp->version][gfc->VBR_max_bitrate];
894
895         gfp->VBR_mean_bitrate_kbps =
896             Min(bitrate_table[gfp->version][gfc->VBR_max_bitrate],
897                 gfp->VBR_mean_bitrate_kbps);
898         gfp->VBR_mean_bitrate_kbps =
899             Max(bitrate_table[gfp->version][gfc->VBR_min_bitrate],
900                 gfp->VBR_mean_bitrate_kbps);
901
902
903     }
904
905     /* for CBR, we will write an "info" tag. */
906     //    if ((gfp->VBR == vbr_off)) 
907     //  gfp->bWriteVbrTag = 0;
908
909     if (gfp->ogg)
910         gfp->bWriteVbrTag = 0;
911 #if defined(HAVE_GTK)
912     if (gfp->analysis)
913         gfp->bWriteVbrTag = 0;
914 #endif
915
916     /* some file options not allowed if output is: not specified or stdout */
917     if (gfc->pinfo != NULL)
918         gfp->bWriteVbrTag = 0; /* disable Xing VBR tag */
919
920     init_bit_stream_w(gfc);
921
922     j =
923         gfc->samplerate_index + (3 * gfp->version) + 6 * (gfp->out_samplerate <
924                                                           16000);
925     for (i = 0; i < SBMAX_l + 1; i++)
926         gfc->scalefac_band.l[i] = sfBandIndex[j].l[i];
927     for (i = 0; i < SBMAX_s + 1; i++)
928         gfc->scalefac_band.s[i] = sfBandIndex[j].s[i];
929
930     /* determine the mean bitrate for main data */
931     if (gfp->version == 1) /* MPEG 1 */
932         gfc->sideinfo_len = (gfc->channels_out == 1) ? 4 + 17 : 4 + 32;
933     else                /* MPEG 2 */
934         gfc->sideinfo_len = (gfc->channels_out == 1) ? 4 + 9 : 4 + 17;
935
936     if (gfp->error_protection)
937         gfc->sideinfo_len += 2;
938
939     lame_init_bitstream(gfp);
940
941
942     if (gfp->version == 1) /* 0 indicates use lower sample freqs algorithm */
943         gfc->is_mpeg1 = 1; /* yes */
944     else
945         gfc->is_mpeg1 = 0; /* no */
946
947     gfc->Class_ID = LAME_ID;
948
949
950     if (gfp->exp_nspsytune & 1) {
951         int     i,j;
952
953         gfc->nsPsy.use = 1;
954         gfc->nsPsy.safejoint = (gfp->exp_nspsytune & 2) != 0;
955         for (i = 0; i < 19; i++)
956             gfc->nsPsy.pefirbuf[i] = 700;
957
958         if (gfp->ATHtype == -1)
959             gfp->ATHtype = 4;
960
961         gfc->nsPsy.bass = gfc->nsPsy.alto = gfc->nsPsy.treble = 0;
962
963         i = (gfp->exp_nspsytune >> 2) & 63;
964         if (i >= 32)
965             i -= 64;
966         gfc->nsPsy.bass = pow(10, i / 4.0 / 10.0);
967         i = (gfp->exp_nspsytune >> 8) & 63;
968         if (i >= 32)
969             i -= 64;
970         gfc->nsPsy.alto = pow(10, i / 4.0 / 10.0);
971         i = (gfp->exp_nspsytune >> 14) & 63;
972         if (i >= 32)
973             i -= 64;
974         gfc->nsPsy.treble = pow(10, i / 4.0 / 10.0);
975         /*  to be compatible with Naoki's original code, the next 6 bits
976          *  define only the amount of changing treble for sfb21 */
977         j = (gfp->exp_nspsytune >> 20) & 63;
978         if (j >= 32)
979             j -= 64;
980         gfc->nsPsy.sfb21 = pow(10, (i+j) / 4.0 / 10.0);
981     }
982
983     assert( gfp->VBR_q <= 9 );
984     assert( gfp->VBR_q >= 0 );
985     
986     gfc->PSY->tonalityPatch = 0;
987   
988     switch (gfp->VBR) {
989
990     case vbr_mt:
991         gfp->VBR = vbr_mtrh;
992         
993     case vbr_mtrh:
994
995         if (gfp->ATHtype < 0) gfp->ATHtype = 4;
996         if (gfp->quality < 0) gfp->quality = 2;
997         if (gfp->quality > 7) {
998             gfp->quality = 7;     // needs psymodel
999             ERRORF( gfc, "VBR needs a psymodel, switching to quality level 7\n");
1000         }
1001
1002         /*  tonality
1003          */
1004         if (gfp->cwlimit <= 0) gfp->cwlimit = 0.42 * gfp->out_samplerate;
1005         gfc->PSY->tonalityPatch = 1;
1006
1007         if ( gfp->experimentalX <= 4 && gfp->experimentalX >= 0 )
1008         {   /* map experimentalX settings to internal secltions */
1009             static char const map[] = {2,1,0,3,6};
1010             gfc->VBR->quality = map[gfp->experimentalX];
1011         }
1012         else    /* defaulting to */
1013         {
1014             gfc->VBR->quality = 2;
1015         }   
1016         if ( gfc->VBR->quality > 5 ) {
1017             static float const dbQ[10] = { -6,-4.75,-3.5,-2.25,-1,.25,1.5,2.75,4,5.25 };
1018             gfc->VBR->mask_adjust = dbQ[gfp->VBR_q];
1019             gfc->VBR->smooth = 1;   // not finally
1020         }
1021         else {
1022             static const float dbQ[10]={-2.,-1.0,-.66,-.33,0.,0.33,.66,1.0,1.33,1.66};
1023             gfc->VBR->mask_adjust = dbQ[gfp->VBR_q];
1024             gfc->VBR->smooth = 1;
1025         }
1026         if ( gfc->VBR->quality == 1 ) {
1027             static float const dbQ[10] = { -2., -1.4, -.7, 0, .7, 1.5, 2.3, 3.1, 4., 5 };
1028             gfc->VBR->mask_adjust = dbQ[gfp->VBR_q];
1029             gfc->VBR->smooth = 0;    
1030         }
1031         if ( gfc->VBR->quality == 0 ) {
1032             static float const dbQ[10] = { -1., -.6, -.3, 0, 1, 2, 3, 4, 5, 6};
1033             gfc->VBR->mask_adjust = dbQ[gfp->VBR_q];
1034             gfc->VBR->smooth = 0;    
1035             gfc->PSY->tonalityPatch = 0;
1036         }
1037         
1038         gfc->VBR->bitpressure = 1;
1039         
1040         if (gfp->experimentalY)
1041             gfc->sfb21_extra = 0;
1042         else
1043             gfc->sfb21_extra = (gfp->out_samplerate > 44000);
1044         
1045         if ( gfp->athaa_type < 0 )
1046             gfc->ATH->use_adjust = 3;
1047         else
1048             gfc->ATH->use_adjust = gfp->athaa_type;
1049         
1050         break;
1051         
1052
1053     case vbr_rh:
1054
1055         if (gfp->VBR == vbr_rh) /* because of above fall thru */
1056         {   static const FLOAT8 dbQ[10]={-2.,-1.0,-.66,-.33,0.,0.33,.66,1.0,1.33,1.66};
1057             static const FLOAT8 dbQns[10]={- 4,- 3,-2,-1,0,0.7,1.4,2.1,2.8,3.5};
1058             /*static const FLOAT8 atQns[10]={-16,-12,-8,-4,0,  1,  2,  3,  4,  5};*/
1059             if ( gfc->nsPsy.use )
1060                 gfc->VBR->mask_adjust = dbQns[gfp->VBR_q];
1061             else {
1062                 gfc->PSY->tonalityPatch = 1;
1063                 gfc->VBR->mask_adjust = dbQ[gfp->VBR_q]; 
1064             }
1065         }
1066         gfc->VBR->bitpressure = 1;
1067         
1068         /*  use Gabriel's adaptative ATH shape for VBR by default
1069          */
1070         if (gfp->ATHtype == -1)
1071             gfp->ATHtype = 4;
1072
1073         /*  automatic ATH adjustment on, VBR modes need it
1074          */
1075         if ( gfp->athaa_type < 0 )
1076             gfc->ATH->use_adjust = 3;
1077         else
1078             gfc->ATH->use_adjust = gfp->athaa_type;
1079
1080         /*  sfb21 extra only with MPEG-1 at higher sampling rates
1081          */
1082         if ( gfp->experimentalY )
1083             gfc->sfb21_extra = 0;
1084         else 
1085             gfc->sfb21_extra = (gfp->out_samplerate > 44000);
1086
1087         /*  VBR needs at least the output of GPSYCHO,
1088          *  so we have to garantee that by setting a minimum 
1089          *  quality level, actually level 5 does it.
1090          *  the -v and -V x settings switch the quality to level 2
1091          *  you would have to add a -q 5 to reduce the quality
1092          *  down to level 5
1093          */
1094         if (gfp->quality > 5)
1095             gfp->quality = 5;
1096
1097
1098         /*  default quality setting is 2
1099          */
1100         if (gfp->quality < 0)
1101             gfp->quality = 2;
1102
1103         break;
1104
1105     default:
1106
1107         if (gfp->ATHtype == -1)
1108             gfp->ATHtype = 2;
1109
1110         /*  automatic ATH adjustment off by default
1111          *  not so important for CBR code?
1112          */
1113         if ( gfp->athaa_type < 0 )
1114             gfc->ATH->use_adjust = 0;
1115         else
1116             gfc->ATH->use_adjust = gfp->athaa_type;
1117
1118
1119         /*  no sfb21 extra with CBR code
1120          */
1121         gfc->sfb21_extra = 0;
1122
1123         /*  default quality setting for CBR/ABR is 5
1124          */
1125         if (gfp->quality < 0)
1126             gfp->quality = 5;
1127
1128         break;
1129     }
1130     /*  just another daily changing developer switch  */
1131     if ( gfp->tune ) gfc->VBR->mask_adjust = gfp->tune_value_a;
1132
1133     /* initialize internal qval settings */
1134     lame_init_qval(gfp);
1135
1136 #ifdef KLEMM_44
1137     gfc->mfbuf[0] = (sample_t *) calloc(sizeof(sample_t), MFSIZE);
1138     gfc->mfbuf[1] = (sample_t *) calloc(sizeof(sample_t), MFSIZE);
1139     gfc->sampfreq_in = unround_samplefrequency(gfp->in_samplerate);
1140     gfc->sampfreq_out = gfp->out_samplerate;
1141     gfc->resample_in = resample_open(gfc->sampfreq_in,
1142                                      gfc->sampfreq_out, -1.0 /* Auto */, 32);
1143 #endif
1144
1145     /* initialize internal adaptive ATH settings  -jd */
1146     gfc->athaa_sensitivity_p = pow( 10.0, gfp->athaa_sensitivity / -10.0 );
1147
1148
1149     gfc->PSY->cwlimit = gfp->cwlimit <= 0 ? 8871.7f : gfp->cwlimit;
1150     
1151     if (gfp->short_blocks == short_block_not_set) {
1152         gfp->short_blocks =  short_block_allowed;
1153     }
1154     if (gfp->short_blocks == short_block_allowed && gfp->mode == JOINT_STEREO) {
1155         gfp->short_blocks =  short_block_coupled;
1156     }    
1157     
1158     if ( gfp->athaa_loudapprox < 0 ) gfp->athaa_loudapprox = 2;
1159     
1160     if (gfp->useTemporal < 0 ) gfp->useTemporal = 1;  // on by default
1161
1162
1163     if ( gfp->experimentalY )
1164         MSGF(gfc,"\n *** WARNING *** the meaning of the experimental -Y has changed!\n"
1165                    "                 now it tells LAME to ignore sfb21 noise shaping (VBR)\n\n");
1166
1167     if ( gfp->preset_expopts && gfc->presetTune.use < 1 )
1168         MSGF(gfc,"\n*** WARNING ***\n\n"
1169                          "Specialized tunings for the preset you are using have been deactivated.\n"
1170                  "This is *NOT* recommended and will lead to a decrease in quality!\n"
1171                      "\n*** WARNING ***\n\n");
1172
1173     return 0;
1174 }
1175
1176 /*}}}*/
1177 /* void          lame_print_config              (lame_global_flags *gfp)                                                                                          *//*{{{ */
1178
1179 /*
1180  *  print_config
1181  *
1182  *  Prints some selected information about the coding parameters via 
1183  *  the macro command MSGF(), which is currently mapped to lame_errorf 
1184  *  (reports via a error function?), which is a printf-like function 
1185  *  for <stderr>.
1186  */
1187
1188 void
1189 lame_print_config(const lame_global_flags * gfp)
1190 {
1191     lame_internal_flags *gfc = gfp->internal_flags;
1192     double  out_samplerate = gfp->out_samplerate;
1193     double  in_samplerate = gfp->out_samplerate * gfc->resample_ratio;
1194
1195     MSGF(gfc, "LAME version %s (%s)\n", get_lame_version(), get_lame_url());
1196
1197     if (gfc->CPU_features.MMX
1198         || gfc->CPU_features.AMD_3DNow
1199         || gfc->CPU_features.SIMD || gfc->CPU_features.SIMD2) {
1200         MSGF(gfc, "CPU features:");
1201
1202         if (gfc->CPU_features.i387)
1203             MSGF(gfc, " i387");
1204         if (gfc->CPU_features.MMX)
1205 #ifdef MMX_choose_table
1206             MSGF(gfc, ", MMX (ASM used)");
1207 #else
1208             MSGF(gfc, ", MMX");
1209 #endif
1210         if (gfc->CPU_features.AMD_3DNow)
1211             MSGF(gfc, ", 3DNow!");
1212         if (gfc->CPU_features.SIMD)
1213             MSGF(gfc, ", SIMD");
1214         if (gfc->CPU_features.SIMD2)
1215             MSGF(gfc, ", SIMD2");
1216         MSGF(gfc, "\n");
1217     }
1218
1219     if (gfp->num_channels == 2 && gfc->channels_out == 1 /* mono */ ) {
1220         MSGF
1221             (gfc,
1222              "Autoconverting from stereo to mono. Setting encoding to mono mode.\n");
1223     }
1224
1225     if (gfc->resample_ratio != 1.) {
1226         MSGF(gfc, "Resampling:  input %g kHz  output %g kHz\n",
1227              1.e-3 * in_samplerate, 1.e-3 * out_samplerate);
1228     }
1229
1230     if (gfc->filter_type == 0) {
1231         if (gfc->highpass2 > 0.)
1232             MSGF
1233                 (gfc,
1234                  "Using polyphase highpass filter, transition band: %5.0f Hz - %5.0f Hz\n",
1235                  0.5 * gfc->highpass1 * out_samplerate,
1236                  0.5 * gfc->highpass2 * out_samplerate);
1237         if (gfc->lowpass1 > 0.) {
1238             MSGF
1239                 (gfc,
1240                  "Using polyphase lowpass  filter, transition band: %5.0f Hz - %5.0f Hz\n",
1241                  0.5 * gfc->lowpass1 * out_samplerate,
1242                  0.5 * gfc->lowpass2 * out_samplerate);
1243         }
1244         else {
1245             MSGF(gfc, "polyphase lowpass filter disabled\n");
1246         }
1247     }
1248     else {
1249         MSGF(gfc, "polyphase filters disabled\n");
1250     }
1251
1252     if (gfp->free_format) {
1253         MSGF(gfc,
1254              "Warning: many decoders cannot handle free format bitstreams\n");
1255         if (gfp->brate > 320) {
1256             MSGF
1257                 (gfc,
1258                  "Warning: many decoders cannot handle free format bitrates >320 kbps (see documentation)\n");
1259         }
1260     }
1261 }
1262
1263
1264 /**     rh:
1265  *      some pretty printing is very welcome at this point!
1266  *      so, if someone is willing to do so, please do it!
1267  *      add more, if you see more...
1268  */
1269 void 
1270 lame_print_internals( const lame_global_flags * gfp )
1271 {
1272     lame_internal_flags *gfc = gfp->internal_flags;
1273     const char * pc = "";
1274
1275     /*  compiler/processor optimizations, operational, etc.
1276      */
1277     MSGF( gfc, "\nmisc:\n\n" );
1278     
1279     MSGF( gfc, "\tscaling: %f\n", gfp->scale );
1280     MSGF( gfc, "\tch0 (left) scaling: %f\n", gfp->scale_left );
1281     MSGF( gfc, "\tch1 (right) scaling: %f\n", gfp->scale_right );
1282     MSGF( gfc, "\tfilter type: %d\n", gfc->filter_type );
1283     pc = gfc->quantization ? "xr^3/4" : "ISO";
1284     MSGF( gfc, "\tquantization: %s\n", pc );
1285     switch( gfc->use_best_huffman ) {
1286     default: pc = "normal"; break;
1287     case  1: pc = "best (outside loop)"; break;
1288     case  2: pc = "best (inside loop, slow)"; break;
1289     } 
1290     MSGF( gfc, "\thuffman search: %s\n", pc ); 
1291     MSGF( gfc, "\texperimental X=%d Y=%d Z=%d\n", gfp->experimentalX, gfp->experimentalY, gfp->experimentalZ );
1292     MSGF( gfc, "\t...\n" );
1293
1294     /*  everything controlling the stream format 
1295      */
1296     MSGF( gfc, "\nstream format:\n\n" );
1297     switch ( gfp->version ) {
1298     case 0:  pc = "2.5"; break;
1299     case 1:  pc = "1";   break;
1300     case 2:  pc = "2";   break;
1301     default: pc = "?";   break;
1302     }
1303     MSGF( gfc, "\tMPEG-%s Layer 3\n", pc );
1304     switch ( gfp->mode ) {
1305     case JOINT_STEREO: pc = "joint stereo";   break;
1306     case STEREO      : pc = "stereo";         break;
1307     case DUAL_CHANNEL: pc = "dual channel";   break;
1308     case MONO        : pc = "mono";           break;
1309     case NOT_SET     : pc = "not set (error)"; break;
1310     default          : pc = "unknown (error)"; break;
1311     }
1312     MSGF( gfc, "\t%d channel - %s\n", gfc->channels_out, pc );
1313     switch ( gfp->padding_type ) {
1314     case PAD_NO    : pc = "off";    break;
1315     case PAD_ALL   : pc = "all";    break;
1316     case PAD_ADJUST: pc = "auto";   break;
1317     default        : pc = "(error)"; break;
1318     }
1319     MSGF( gfc, "\tpadding: %s\n", pc );
1320     
1321     if ( vbr_default == gfp->VBR )  pc = "(default)";
1322     else if ( gfp->free_format )    pc = "(free format)";
1323     else pc = "";
1324     switch ( gfp->VBR ) {
1325     case vbr_off : MSGF( gfc, "\tconstant bitrate - CBR %s\n",      pc ); break;
1326     case vbr_abr : MSGF( gfc, "\tvariable bitrate - ABR %s\n",      pc ); break;
1327     case vbr_rh  : MSGF( gfc, "\tvariable bitrate - VBR rh %s\n",   pc ); break;
1328     case vbr_mt  : MSGF( gfc, "\tvariable bitrate - VBR mt %s\n",   pc ); break;
1329     case vbr_mtrh: MSGF( gfc, "\tvariable bitrate - VBR mtrh %s\n", pc ); break; 
1330     default      : MSGF( gfc, "\t ?? oops, some new one ?? \n" );         break;
1331     }
1332     if (gfp->bWriteVbrTag) 
1333     MSGF( gfc, "\tusing LAME Tag\n" );
1334     MSGF( gfc, "\t...\n" );
1335     
1336     /*  everything controlling psychoacoustic settings, like ATH, etc.
1337      */
1338     MSGF( gfc, "\npsychoacoustic:\n\n" );
1339     
1340     MSGF( gfc, "\ttonality estimation limit: %f Hz\n", gfc->PSY->cwlimit );
1341     switch ( gfp->short_blocks ) {
1342     default:
1343     case short_block_not_set:   pc = "?";               break;
1344     case short_block_allowed:   pc = "allowed";         break;
1345     case short_block_coupled:   pc = "channel coupled"; break;
1346     case short_block_dispensed: pc = "dispensed";       break;
1347     case short_block_forced:    pc = "forced";          break;
1348     }
1349     MSGF( gfc, "\tusing short blocks: %s\n", pc );    
1350     MSGF( gfc, "\tadjust masking: %f dB\n", gfc->VBR->mask_adjust );
1351     MSGF( gfc, "\tpsymodel: %d\n", gfc->psymodel );
1352     MSGF( gfc, "\tnoise shaping: %d\n", gfc->noise_shaping );
1353     MSGF( gfc, "\t ^ amplification: %d\n", gfc->noise_shaping_amp );
1354     MSGF( gfc, "\t ^ stopping: %d\n", gfc->noise_shaping_stop );
1355     
1356     pc = "using";
1357     if ( gfp->ATHshort ) pc = "the only masking for short blocks";
1358     if ( gfp->ATHonly  ) pc = "the only masking";
1359     if ( gfp->noATH    ) pc = "not used";
1360     MSGF( gfc, "\tATH: %s\n", pc );
1361     MSGF( gfc, "\t ^ type: %d\n", gfp->ATHtype );
1362     MSGF( gfc, "\t ^ adjust type: %d\n", gfc->ATH->use_adjust );
1363     MSGF( gfc, "\t ^ adapt threshold type: %d\n", gfp->athaa_loudapprox );
1364     
1365     if ( gfc->nsPsy.use ) {
1366     MSGF( gfc, "\texperimental psy tunings by Naoki Shibata\n" ); 
1367     MSGF( gfc, "\t   adjust masking bass=%g dB, alto=%g dB, treble=%g dB, sfb21=%g dB\n", 
1368         10*log10(gfc->nsPsy.bass),   10*log10(gfc->nsPsy.alto), 
1369         10*log10(gfc->nsPsy.treble), 10*log10(gfc->nsPsy.sfb21) );
1370     }
1371     pc = gfp->useTemporal ? "yes" : "no";
1372     MSGF( gfc, "\tusing temporal masking effect: %s\n", pc );
1373     MSGF( gfc, "\t...\n" );
1374     
1375     /*  that's all ?
1376      */
1377     MSGF( gfc, "\n" );
1378     return;
1379 }
1380
1381
1382
1383 /* int           lame_encode_frame              (lame_global_flags *gfp, sample_t inbuf_l[],sample_t inbuf_r[], char *mp3buf, int mp3buf_size)                    *//*{{{ */
1384
1385 /* routine to feed exactly one frame (gfp->framesize) worth of data to the 
1386 encoding engine.  All buffering, resampling, etc, handled by calling
1387 program.  
1388 */
1389 int
1390 lame_encode_frame(lame_global_flags * gfp,
1391                   sample_t inbuf_l[], sample_t inbuf_r[],
1392                   unsigned char *mp3buf, int mp3buf_size)
1393 {
1394     int     ret;
1395     if (gfp->ogg) {
1396 #ifdef HAVE_VORBIS_ENCODER
1397         ret = lame_encode_ogg_frame(gfp, inbuf_l, inbuf_r, mp3buf, mp3buf_size);
1398 #else
1399         return -5;      /* wanna encode ogg without vorbis */
1400 #endif
1401     }
1402     else {
1403         ret = lame_encode_mp3_frame(gfp, inbuf_l, inbuf_r, mp3buf, mp3buf_size);
1404     }
1405
1406
1407
1408     gfp->frameNum++;
1409     return ret;
1410 }
1411
1412 /*}}}*/
1413 /* int           lame_encode_buffer             (lame_global_flags* gfp, short int buffer_l[], short int buffer_r[], int nsamples, char* mp3buf, int mp3buf_size )*//*{{{ */
1414
1415
1416
1417 /*
1418  * THE MAIN LAME ENCODING INTERFACE
1419  * mt 3/00
1420  *
1421  * input pcm data, output (maybe) mp3 frames.
1422  * This routine handles all buffering, resampling and filtering for you.
1423  * The required mp3buffer_size can be computed from num_samples,
1424  * samplerate and encoding rate, but here is a worst case estimate:
1425  *
1426  * mp3buffer_size in bytes = 1.25*num_samples + 7200
1427  *
1428  * return code = number of bytes output in mp3buffer.  can be 0
1429 */
1430 int
1431 lame_encode_buffer_sample_t(lame_global_flags * gfp,
1432                    sample_t buffer_l[],
1433                    sample_t buffer_r[],
1434                    int nsamples, unsigned char *mp3buf, const int mp3buf_size)
1435 {
1436     lame_internal_flags *gfc = gfp->internal_flags;
1437     int     mp3size = 0, ret, i, ch, mf_needed;
1438     int mp3out;
1439     sample_t *mfbuf[2];
1440     sample_t *in_buffer[2];
1441
1442     if (gfc->Class_ID != LAME_ID)
1443         return -3;
1444
1445     if (nsamples == 0)
1446         return 0;
1447
1448     /* copy out any tags that may have been written into bitstream */
1449     mp3out = copy_buffer(gfc,mp3buf,mp3buf_size,0);
1450     if (mp3out<0) return mp3out;  // not enough buffer space
1451     mp3buf += mp3out;
1452     mp3size += mp3out;
1453
1454
1455     in_buffer[0]=buffer_l;
1456     in_buffer[1]=buffer_r;  
1457
1458
1459     /* some sanity checks */
1460 #if ENCDELAY < MDCTDELAY
1461 # error ENCDELAY is less than MDCTDELAY, see encoder.h
1462 #endif
1463 #if FFTOFFSET > BLKSIZE
1464 # error FFTOFFSET is greater than BLKSIZE, see encoder.h
1465 #endif
1466
1467     mf_needed = BLKSIZE + gfp->framesize - FFTOFFSET; /* amount needed for FFT */
1468     mf_needed = Max(mf_needed, 286 + 576 * (1 + gfc->mode_gr)); /* amount needed for MDCT/filterbank */
1469     assert(MFSIZE >= mf_needed);
1470
1471     mfbuf[0] = gfc->mfbuf[0];
1472     mfbuf[1] = gfc->mfbuf[1];
1473
1474     if (gfp->num_channels == 2 && gfc->channels_out == 1) {
1475         /* downsample to mono */
1476         for (i = 0; i < nsamples; ++i) {
1477             in_buffer[0][i] =
1478                 0.5 * ((FLOAT8) in_buffer[0][i] + in_buffer[1][i]);
1479             in_buffer[1][i] = 0.0;
1480         }
1481     }
1482
1483
1484     while (nsamples > 0) {
1485         int     n_in = 0;    /* number of input samples processed with fill_buffer */
1486         int     n_out = 0;   /* number of samples output with fill_buffer */
1487         /* n_in <> n_out if we are resampling */
1488
1489         /* copy in new samples into mfbuf, with resampling & scaling if necessary */
1490         fill_buffer(gfp, mfbuf, in_buffer, nsamples, &n_in, &n_out);
1491
1492         /* update in_buffer counters */
1493         nsamples -= n_in;
1494         in_buffer[0] += n_in;
1495         if (gfc->channels_out == 2)
1496             in_buffer[1] += n_in;
1497
1498         /* update mfbuf[] counters */
1499         gfc->mf_size += n_out;
1500         assert(gfc->mf_size <= MFSIZE);
1501         gfc->mf_samples_to_encode += n_out;
1502
1503
1504         if (gfc->mf_size >= mf_needed) {
1505             /* encode the frame.  */
1506             // mp3buf              = pointer to current location in buffer
1507             // mp3buf_size         = size of original mp3 output buffer
1508             //                     = 0 if we should not worry about the
1509             //                       buffer size because calling program is 
1510             //                       to lazy to compute it
1511             // mp3size             = size of data written to buffer so far
1512             // mp3buf_size-mp3size = amount of space avalable 
1513
1514             int buf_size=mp3buf_size - mp3size;
1515             if (mp3buf_size==0) buf_size=0;
1516
1517             ret =
1518                 lame_encode_frame(gfp, mfbuf[0], mfbuf[1], mp3buf,buf_size);
1519
1520             if (ret < 0) goto retr;
1521             mp3buf += ret;
1522             mp3size += ret;
1523
1524             /* shift out old samples */
1525             gfc->mf_size -= gfp->framesize;
1526             gfc->mf_samples_to_encode -= gfp->framesize;
1527             for (ch = 0; ch < gfc->channels_out; ch++)
1528                 for (i = 0; i < gfc->mf_size; i++)
1529                     mfbuf[ch][i] = mfbuf[ch][i + gfp->framesize];
1530         }
1531     }
1532     assert(nsamples == 0);
1533     ret = mp3size;
1534
1535   retr:
1536     return ret;
1537 }
1538
1539
1540 int
1541 lame_encode_buffer(lame_global_flags * gfp,
1542                    const short int buffer_l[],
1543                    const short int buffer_r[],
1544                    const int nsamples, unsigned char *mp3buf, const int mp3buf_size)
1545 {
1546     lame_internal_flags *gfc = gfp->internal_flags;
1547     int     ret, i;
1548     sample_t *in_buffer[2];
1549
1550     if (gfc->Class_ID != LAME_ID)
1551         return -7; //-3
1552
1553     if (nsamples == 0)
1554         return 0;
1555
1556     in_buffer[0] = calloc(sizeof(sample_t), nsamples);
1557     in_buffer[1] = calloc(sizeof(sample_t), nsamples);
1558
1559     if (in_buffer[0] == NULL || in_buffer[1] == NULL) {
1560         ERRORF(gfc, "Error: can't allocate in_buffer buffer\n");
1561         return -2;
1562     }
1563
1564     /* make a copy of input buffer, changing type to sample_t */
1565     for (i = 0; i < nsamples; i++) {
1566         in_buffer[0][i] = buffer_l[i];
1567         in_buffer[1][i] = buffer_r[i];
1568     }
1569
1570     ret = lame_encode_buffer_sample_t(gfp,in_buffer[0],in_buffer[1],
1571                                       nsamples, mp3buf, mp3buf_size);
1572     
1573     free(in_buffer[0]);
1574     free(in_buffer[1]);
1575     return ret;
1576 }
1577
1578
1579 int
1580 lame_encode_buffer_float(lame_global_flags * gfp,
1581                    const float buffer_l[],
1582                    const float buffer_r[],
1583                    const int nsamples, unsigned char *mp3buf, const int mp3buf_size)
1584 {
1585     lame_internal_flags *gfc = gfp->internal_flags;
1586     int     ret, i;
1587     sample_t *in_buffer[2];
1588
1589     if (gfc->Class_ID != LAME_ID)
1590         return -3;
1591
1592     if (nsamples == 0)
1593         return 0;
1594
1595     in_buffer[0] = calloc(sizeof(sample_t), nsamples);
1596     in_buffer[1] = calloc(sizeof(sample_t), nsamples);
1597
1598     if (in_buffer[0] == NULL || in_buffer[1] == NULL) {
1599         ERRORF(gfc, "Error: can't allocate in_buffer buffer\n");
1600         return -2;
1601     }
1602
1603     /* make a copy of input buffer, changing type to sample_t */
1604     for (i = 0; i < nsamples; i++) {
1605         in_buffer[0][i] = buffer_l[i];
1606         in_buffer[1][i] = buffer_r[i];
1607     }
1608
1609     ret = lame_encode_buffer_sample_t(gfp,in_buffer[0],in_buffer[1],
1610                                       nsamples, mp3buf, mp3buf_size);
1611     
1612     free(in_buffer[0]);
1613     free(in_buffer[1]);
1614     return ret;
1615 }
1616
1617
1618
1619 int
1620 lame_encode_buffer_int(lame_global_flags * gfp,
1621                    const int buffer_l[],
1622                    const int buffer_r[],
1623                    const int nsamples, unsigned char *mp3buf, const int mp3buf_size)
1624 {
1625     lame_internal_flags *gfc = gfp->internal_flags;
1626     int     ret, i;
1627     sample_t *in_buffer[2];
1628
1629     if (gfc->Class_ID != LAME_ID)
1630         return -3;
1631
1632     if (nsamples == 0)
1633         return 0;
1634
1635     in_buffer[0] = calloc(sizeof(sample_t), nsamples);
1636     in_buffer[1] = calloc(sizeof(sample_t), nsamples);
1637
1638     if (in_buffer[0] == NULL || in_buffer[1] == NULL) {
1639         ERRORF(gfc, "Error: can't allocate in_buffer buffer\n");
1640         return -2;
1641     }
1642
1643     /* make a copy of input buffer, changing type to sample_t */
1644     for (i = 0; i < nsamples; i++) {
1645                                 /* internal code expects +/- 32768.0 */
1646       in_buffer[0][i] = buffer_l[i] * (1.0 / ( 1 << (8 * sizeof(int) - 16)));
1647       in_buffer[1][i] = buffer_r[i] * (1.0 / ( 1 << (8 * sizeof(int) - 16)));
1648     }
1649
1650     ret = lame_encode_buffer_sample_t(gfp,in_buffer[0],in_buffer[1],
1651                                       nsamples, mp3buf, mp3buf_size);
1652     
1653     free(in_buffer[0]);
1654     free(in_buffer[1]);
1655     return ret;
1656
1657 }
1658
1659
1660
1661
1662 int
1663 lame_encode_buffer_long2(lame_global_flags * gfp,
1664                    const long buffer_l[],
1665                    const long buffer_r[],
1666                    const int nsamples, unsigned char *mp3buf, const int mp3buf_size)
1667 {
1668     lame_internal_flags *gfc = gfp->internal_flags;
1669     int     ret, i;
1670     sample_t *in_buffer[2];
1671
1672     if (gfc->Class_ID != LAME_ID)
1673         return -3;
1674
1675     if (nsamples == 0)
1676         return 0;
1677
1678     in_buffer[0] = calloc(sizeof(sample_t), nsamples);
1679     in_buffer[1] = calloc(sizeof(sample_t), nsamples);
1680
1681     if (in_buffer[0] == NULL || in_buffer[1] == NULL) {
1682         ERRORF(gfc, "Error: can't allocate in_buffer buffer\n");
1683         return -2;
1684     }
1685
1686     /* make a copy of input buffer, changing type to sample_t */
1687     for (i = 0; i < nsamples; i++) {
1688                                 /* internal code expects +/- 32768.0 */
1689       in_buffer[0][i] = buffer_l[i] * (1.0 / ( 1 << (8 * sizeof(long) - 16)));
1690       in_buffer[1][i] = buffer_r[i] * (1.0 / ( 1 << (8 * sizeof(long) - 16)));
1691     }
1692
1693     ret = lame_encode_buffer_sample_t(gfp,in_buffer[0],in_buffer[1],
1694                                       nsamples, mp3buf, mp3buf_size);
1695     
1696     free(in_buffer[0]);
1697     free(in_buffer[1]);
1698     return ret;
1699
1700 }
1701
1702
1703
1704 int
1705 lame_encode_buffer_long(lame_global_flags * gfp,
1706                    const long buffer_l[],
1707                    const long buffer_r[],
1708                    const int nsamples, unsigned char *mp3buf, const int mp3buf_size)
1709 {
1710     lame_internal_flags *gfc = gfp->internal_flags;
1711     int     ret, i;
1712     sample_t *in_buffer[2];
1713
1714     if (gfc->Class_ID != LAME_ID)
1715         return -3;
1716
1717     if (nsamples == 0)
1718         return 0;
1719
1720     in_buffer[0] = calloc(sizeof(sample_t), nsamples);
1721     in_buffer[1] = calloc(sizeof(sample_t), nsamples);
1722
1723     if (in_buffer[0] == NULL || in_buffer[1] == NULL) {
1724         ERRORF(gfc, "Error: can't allocate in_buffer buffer\n");
1725         return -2;
1726     }
1727
1728     /* make a copy of input buffer, changing type to sample_t */
1729     for (i = 0; i < nsamples; i++) {
1730         in_buffer[0][i] = buffer_l[i];
1731         in_buffer[1][i] = buffer_r[i];
1732     }
1733
1734     ret = lame_encode_buffer_sample_t(gfp,in_buffer[0],in_buffer[1],
1735                                       nsamples, mp3buf, mp3buf_size);
1736     
1737     free(in_buffer[0]);
1738     free(in_buffer[1]);
1739     return ret;
1740 }
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752 int
1753 lame_encode_buffer_interleaved(lame_global_flags * gfp,
1754                                short int buffer[],
1755                                int nsamples,
1756                                unsigned char *mp3buf, int mp3buf_size)
1757 {
1758     int     ret, i;
1759     short int *buffer_l;
1760     short int *buffer_r;
1761
1762     buffer_l = malloc(sizeof(short int) * nsamples);
1763     buffer_r = malloc(sizeof(short int) * nsamples);
1764     if (buffer_l == NULL || buffer_r == NULL) {
1765         return -2;
1766     }
1767     for (i = 0; i < nsamples; i++) {
1768         buffer_l[i] = buffer[2 * i];
1769         buffer_r[i] = buffer[2 * i + 1];
1770     }
1771     ret =
1772         lame_encode_buffer(gfp, buffer_l, buffer_r, nsamples, mp3buf,
1773                            mp3buf_size);
1774     free(buffer_l);
1775     free(buffer_r);
1776     return ret;
1777
1778 }
1779
1780
1781 /*}}}*/
1782 /* int           lame_encode                    (lame_global_flags* gfp, short int in_buffer[2][1152], char* mp3buf, int size )                                   *//*{{{ */
1783
1784
1785 /* old LAME interface.  use lame_encode_buffer instead */
1786
1787 int
1788 lame_encode(lame_global_flags * const gfp,
1789             const short int in_buffer[2][1152],
1790             unsigned char *const mp3buf, const int size)
1791 {
1792     lame_internal_flags *gfc = gfp->internal_flags;
1793
1794     if (gfc->Class_ID != LAME_ID)
1795         return -3;
1796
1797     return lame_encode_buffer(gfp, in_buffer[0], in_buffer[1], gfp->framesize,
1798                               mp3buf, size);
1799 }
1800
1801 /*}}}*/
1802 /* int           lame_encode_flush              (lame_global_flags* gfp, char* mp3buffer, int mp3buffer_size )                                                    *//*{{{ */
1803
1804
1805
1806 /*****************************************************************
1807  Flush mp3 buffer, pad with ancillary data so last frame is complete.
1808  Reset reservoir size to 0                  
1809  but keep all PCM samples and MDCT data in memory             
1810  This option is used to break a large file into several mp3 files 
1811  that when concatenated together will decode with no gaps         
1812  Because we set the reservoir=0, they will also decode seperately 
1813  with no errors. 
1814 *********************************************************************/
1815 int
1816 lame_encode_flush_nogap(lame_global_flags * gfp,
1817                   unsigned char *mp3buffer, int mp3buffer_size)
1818 {
1819     lame_internal_flags *gfc = gfp->internal_flags;
1820     flush_bitstream(gfp);
1821     return copy_buffer(gfc,mp3buffer, mp3buffer_size,1);
1822 }
1823
1824
1825 /* called by lame_init_params.  You can also call this after flush_nogap 
1826    if you want to write new id3v2 and Xing VBR tags into the bitstream */
1827 int
1828 lame_init_bitstream(lame_global_flags * gfp)
1829 {
1830     lame_internal_flags *gfc = gfp->internal_flags;
1831     int i;
1832     gfp->frameNum=0;
1833
1834     if (!gfp->ogg)
1835         id3tag_write_v2(gfp);
1836
1837     /* initialize histogram data optionally used by frontend */
1838     for ( i = 0; i < 16; i++ ) 
1839         gfc->bitrate_stereoMode_Hist [i] [0] =
1840             gfc->bitrate_stereoMode_Hist [i] [1] =
1841             gfc->bitrate_stereoMode_Hist [i] [2] =
1842             gfc->bitrate_stereoMode_Hist [i] [3] =
1843             gfc->bitrate_stereoMode_Hist [i] [4] = 0;
1844
1845     /* Write initial VBR Header to bitstream and init VBR data */
1846     if (gfp->bWriteVbrTag)
1847         InitVbrTag(gfp);
1848
1849     
1850     return 0;
1851 }
1852
1853
1854 /*****************************************************************/
1855 /* flush internal PCM sample buffers, then mp3 buffers           */
1856 /* then write id3 v1 tags into bitstream.                        */
1857 /*****************************************************************/
1858
1859 int
1860 lame_encode_flush(lame_global_flags * gfp,
1861                   unsigned char *mp3buffer, int mp3buffer_size)
1862 {
1863     lame_internal_flags *gfc = gfp->internal_flags;
1864     short int buffer[2][1152];
1865     int     imp3 = 0, mp3count, mp3buffer_size_remaining;
1866
1867     /* we always add POSTDELAY=288 padding to make sure granule with real
1868      * data can be complety decoded (because of 50% overlap with next granule */
1869     int end_padding=POSTDELAY;  
1870
1871     memset(buffer, 0, sizeof(buffer));
1872     mp3count = 0;
1873
1874
1875     while (gfc->mf_samples_to_encode > 0) {
1876
1877         mp3buffer_size_remaining = mp3buffer_size - mp3count;
1878
1879         /* if user specifed buffer size = 0, dont check size */
1880         if (mp3buffer_size == 0)
1881             mp3buffer_size_remaining = 0;
1882
1883         /* send in a frame of 0 padding until all internal sample buffers
1884          * are flushed 
1885          */
1886         imp3 = lame_encode_buffer(gfp, buffer[0], buffer[1], gfp->framesize,
1887                                   mp3buffer, mp3buffer_size_remaining);
1888
1889         /* don't count the above padding: */
1890         gfc->mf_samples_to_encode -= gfp->framesize;
1891         if (gfc->mf_samples_to_encode < 0) {
1892             /* we added extra padding to the end */
1893             end_padding += -gfc->mf_samples_to_encode;  
1894         }
1895
1896
1897         if (imp3 < 0) {
1898             /* some type of fatal error */
1899             return imp3;
1900         }
1901         mp3buffer += imp3;
1902         mp3count += imp3;
1903     }
1904
1905     mp3buffer_size_remaining = mp3buffer_size - mp3count;
1906     /* if user specifed buffer size = 0, dont check size */
1907     if (mp3buffer_size == 0)
1908         mp3buffer_size_remaining = 0;
1909
1910     if (gfp->ogg) {
1911 #ifdef HAVE_VORBIS_ENCODER
1912         /* ogg related stuff */
1913         imp3 = lame_encode_ogg_finish(gfp, mp3buffer, mp3buffer_size_remaining);
1914 #endif
1915     }
1916     else {
1917         /* mp3 related stuff.  bit buffer might still contain some mp3 data */
1918         flush_bitstream(gfp);
1919         imp3 = copy_buffer(gfc,mp3buffer, mp3buffer_size_remaining, 1);
1920         if (imp3 < 0) {
1921             /* some type of fatal error */
1922             return imp3;
1923         }
1924         mp3buffer += imp3;
1925         mp3count += imp3;
1926         mp3buffer_size_remaining = mp3buffer_size - mp3count;
1927         /* if user specifed buffer size = 0, dont check size */
1928         if (mp3buffer_size == 0)
1929             mp3buffer_size_remaining = 0;
1930
1931
1932         /* write a id3 tag to the bitstream */
1933         id3tag_write_v1(gfp);
1934         imp3 = copy_buffer(gfc,mp3buffer, mp3buffer_size_remaining, 0);
1935     }
1936
1937     if (imp3 < 0) {
1938         return imp3;
1939     }
1940     mp3count += imp3;
1941     gfp->encoder_padding=end_padding;
1942     return mp3count;
1943 }
1944
1945 /*}}}*/
1946 /* void          lame_close                     (lame_global_flags *gfp)                                                                                          *//*{{{ */
1947
1948 /***********************************************************************
1949  *
1950  *      lame_close ()
1951  *
1952  *  frees internal buffers
1953  *
1954  ***********************************************************************/
1955
1956 int
1957 lame_close(lame_global_flags * gfp)
1958 {
1959     lame_internal_flags *gfc = gfp->internal_flags;
1960
1961     if (gfc->Class_ID != LAME_ID)
1962         return -3;
1963
1964     gfc->Class_ID = 0;
1965
1966     // this routien will free all malloc'd data in gfc, and then free gfc:
1967     freegfc(gfc);
1968
1969     gfp->internal_flags = NULL;
1970
1971     if (gfp->lame_allocated_gfp)
1972         free(gfp);
1973
1974     return 0;
1975 }
1976
1977
1978 /*}}}*/
1979 /* int           lame_encode_finish             (lame_global_flags* gfp, char* mp3buffer, int mp3buffer_size )                                                    *//*{{{ */
1980
1981
1982 /*****************************************************************/
1983 /* flush internal mp3 buffers, and free internal buffers         */
1984 /*****************************************************************/
1985
1986 int
1987 lame_encode_finish(lame_global_flags * gfp,
1988                    unsigned char *mp3buffer, int mp3buffer_size)
1989 {
1990     int     ret = lame_encode_flush(gfp, mp3buffer, mp3buffer_size);
1991
1992     lame_close(gfp);
1993
1994     return ret;
1995 }
1996
1997 /*}}}*/
1998 /* void          lame_mp3_tags_fid              (lame_global_flags *gfp,FILE *fpStream)                                                                           *//*{{{ */
1999
2000 /*****************************************************************/
2001 /* write VBR Xing header, and ID3 version 1 tag, if asked for    */
2002 /*****************************************************************/
2003 void
2004 lame_mp3_tags_fid(lame_global_flags * gfp, FILE * fpStream)
2005 {
2006     if (gfp->bWriteVbrTag) {
2007         /* Map VBR_q to Xing quality value: 0=worst, 100=best */
2008         int     nQuality = ((9-gfp->VBR_q) * 100) / 9;
2009
2010         /* Write Xing header again */
2011         if (fpStream && !fseek(fpStream, 0, SEEK_SET))
2012             PutVbrTag(gfp, fpStream, nQuality);
2013     }
2014
2015
2016 }
2017 /*}}}*/
2018 /* lame_global_flags *lame_init                 (void)                                                                                                            *//*{{{ */
2019
2020 lame_global_flags *
2021 lame_init(void)
2022 {
2023     lame_global_flags *gfp;
2024     int     ret;
2025
2026     gfp = calloc(1, sizeof(lame_global_flags));
2027     if (gfp == NULL)
2028         return NULL;
2029
2030     ret = lame_init_old(gfp);
2031     if (ret != 0) {
2032         free(gfp);
2033         return NULL;
2034     }
2035
2036     gfp->lame_allocated_gfp = 1;
2037     return gfp;
2038 }
2039
2040 /*}}}*/
2041 /* int           lame_init_old                  (lame_global_flags *gfp)                                                                                          *//*{{{ */
2042
2043 /* initialize mp3 encoder */
2044 int
2045 lame_init_old(lame_global_flags * gfp)
2046 {
2047     lame_internal_flags *gfc;
2048
2049     disable_FPE();      // disable floating point exceptions
2050
2051     memset(gfp, 0, sizeof(lame_global_flags));
2052
2053     if (NULL ==
2054         (gfc = gfp->internal_flags =
2055          calloc(1, sizeof(lame_internal_flags)))) return -1;
2056
2057     /* Global flags.  set defaults here for non-zero values */
2058     /* see lame.h for description */
2059     /* set integer values to -1 to mean that LAME will compute the
2060      * best value, UNLESS the calling program as set it
2061      * (and the value is no longer -1)
2062      */
2063
2064
2065     gfp->mode = NOT_SET;
2066     gfp->original = 1;
2067     gfp->in_samplerate = 1000 * 44.1;
2068     gfp->num_channels = 2;
2069     gfp->num_samples = MAX_U_32_NUM;
2070
2071     gfp->bWriteVbrTag = 1;
2072     gfp->quality = -1;
2073     gfp->short_blocks = short_block_not_set;
2074
2075     gfp->lowpassfreq = 0;
2076     gfp->highpassfreq = 0;
2077     gfp->lowpasswidth = -1;
2078     gfp->highpasswidth = -1;
2079
2080     gfp->padding_type = PAD_ADJUST;
2081     gfp->VBR = vbr_off;
2082     gfp->VBR_q = 4;
2083     gfp->VBR_mean_bitrate_kbps = 128;
2084     gfp->VBR_min_bitrate_kbps = 0;
2085     gfp->VBR_max_bitrate_kbps = 0;
2086     gfp->VBR_hard_min = 0;
2087
2088
2089     gfc->resample_ratio = 1;
2090     gfc->lowpass_band = 32;
2091     gfc->highpass_band = -1;
2092     gfc->VBR_min_bitrate = 1; /* not  0 ????? */
2093     gfc->VBR_max_bitrate = 13; /* not 14 ????? */
2094
2095     gfc->OldValue[0] = 180;
2096     gfc->OldValue[1] = 180;
2097     gfc->CurrentStep = 4;
2098     gfc->masking_lower = 1;
2099
2100     gfp->athaa_type = -1;
2101     gfp->ATHtype = -1;  /* default = -1 = set in lame_init_params */
2102     gfp->athaa_loudapprox = -1; /* 1 = flat loudness approx. (total energy) */
2103                                 /* 2 = equal loudness curve */
2104     gfp->athaa_sensitivity = 0.0; /* no offset */
2105     gfp->useTemporal = -1;
2106
2107     /* The reason for
2108      *       int mf_samples_to_encode = ENCDELAY + POSTDELAY;
2109      * ENCDELAY = internal encoder delay.  And then we have to add POSTDELAY=288
2110      * because of the 50% MDCT overlap.  A 576 MDCT granule decodes to
2111      * 1152 samples.  To synthesize the 576 samples centered under this granule
2112      * we need the previous granule for the first 288 samples (no problem), and
2113      * the next granule for the next 288 samples (not possible if this is last
2114      * granule).  So we need to pad with 288 samples to make sure we can
2115      * encode the 576 samples we are interested in.
2116      */
2117     gfc->mf_samples_to_encode = ENCDELAY + POSTDELAY;
2118     gfp->encoder_padding = 0;
2119     gfc->mf_size = ENCDELAY - MDCTDELAY; /* we pad input with this many 0's */
2120
2121 #ifdef KLEMM_44
2122     /* XXX: this wasn't protectes by KLEMM_44 initially! */
2123     gfc->last_ampl = gfc->ampl = +1.0;
2124 #endif
2125     return 0;
2126 }
2127
2128 /*}}}*/
2129
2130 /***********************************************************************
2131  *
2132  *  some simple statistics
2133  *
2134  *  Robert Hegemann 2000-10-11
2135  *
2136  ***********************************************************************/
2137
2138 /*  histogram of used bitrate indexes:
2139  *  One has to weight them to calculate the average bitrate in kbps
2140  *
2141  *  bitrate indices:
2142  *  there are 14 possible bitrate indices, 0 has the special meaning 
2143  *  "free format" which is not possible to mix with VBR and 15 is forbidden
2144  *  anyway.
2145  *
2146  *  stereo modes:
2147  *  0: LR   number of left-right encoded frames
2148  *  1: LR-I number of left-right and intensity encoded frames
2149  *  2: MS   number of mid-side encoded frames
2150  *  3: MS-I number of mid-side and intensity encoded frames
2151  *
2152  *  4: number of encoded frames
2153  *
2154  */
2155
2156 void
2157 lame_bitrate_hist(const lame_global_flags * const gfp, int bitrate_count[14])
2158 {
2159     const lame_internal_flags *gfc;
2160     int     i;
2161
2162     if (NULL == bitrate_count)
2163         return;
2164     if (NULL == gfp)
2165         return;
2166     gfc = gfp->internal_flags;
2167     if (NULL == gfc)
2168         return;
2169
2170     for (i = 0; i < 14; i++)
2171         bitrate_count[i] = gfc->bitrate_stereoMode_Hist[i + 1][4];
2172 }
2173
2174
2175 void
2176 lame_bitrate_kbps(const lame_global_flags * const gfp, int bitrate_kbps[14])
2177 {
2178     const lame_internal_flags *gfc;
2179     int     i;
2180
2181     if (NULL == bitrate_kbps)
2182         return;
2183     if (NULL == gfp)
2184         return;
2185     gfc = gfp->internal_flags;
2186     if (NULL == gfc)
2187         return;
2188
2189     for (i = 0; i < 14; i++)
2190         bitrate_kbps[i] = bitrate_table[gfp->version][i + 1];
2191 }
2192
2193
2194
2195 void
2196 lame_stereo_mode_hist(const lame_global_flags * const gfp, int stmode_count[4])
2197 {
2198     const lame_internal_flags *gfc;
2199     int     i;
2200
2201     if (NULL == stmode_count)
2202         return;
2203     if (NULL == gfp)
2204         return;
2205     gfc = gfp->internal_flags;
2206     if (NULL == gfc)
2207         return;
2208
2209     for (i = 0; i < 4; i++) {
2210         int     j, sum = 0;
2211         for (j = 0; j < 14; j++)
2212             sum += gfc->bitrate_stereoMode_Hist[j + 1][i];
2213         stmode_count[i] = sum;
2214     }
2215 }
2216
2217
2218
2219 void
2220 lame_bitrate_stereo_mode_hist(const lame_global_flags * const gfp,
2221                               int bitrate_stmode_count[14][4])
2222 {
2223     const lame_internal_flags *gfc;
2224     int     i;
2225     int     j;
2226
2227     if (NULL == bitrate_stmode_count)
2228         return;
2229     if (NULL == gfp)
2230         return;
2231     gfc = gfp->internal_flags;
2232     if (NULL == gfc)
2233         return;
2234
2235     for (j = 0; j < 14; j++)
2236         for (i = 0; i < 4; i++)
2237             bitrate_stmode_count[j][i] = gfc->bitrate_stereoMode_Hist[j + 1][i];
2238 }
2239
2240 /* end of lame.c */