xref: /plan9/sys/src/games/mp3enc/lame.c (revision 8f5875f3e9b20916b4c52ad4336922bc8653eb7b)
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.100 2001/03/25 23:14:45 markt Exp $ */
24 
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 
29 
30 #include <assert.h>
31 #include "lame-analysis.h"
32 #include "lame.h"
33 #include "util.h"
34 #include "bitstream.h"
35 #include "version.h"
36 #include "tables.h"
37 #include "quantize_pvt.h"
38 #include "VbrTag.h"
39 
40 #if defined(__FreeBSD__) && !defined(__alpha__)
41 #include <floatingpoint.h>
42 #endif
43 #ifdef __riscos__
44 #include "asmstuff.h"
45 #endif
46 
47 #ifdef WITH_DMALLOC
48 #include <dmalloc.h>
49 #endif
50 
51 
52 static void
lame_init_params_ppflt_lowpass(FLOAT8 amp_lowpass[32],FLOAT lowpass1,FLOAT lowpass2,int * lowpass_band,int * minband,int * maxband)53 lame_init_params_ppflt_lowpass(FLOAT8 amp_lowpass[32], FLOAT lowpass1,
54                                FLOAT lowpass2, int *lowpass_band,
55                                int *minband, int *maxband)
56 {
57     int     band;
58     FLOAT8  freq;
59 
60     for (band = 0; band <= 31; band++) {
61         freq = band / 31.0;
62         amp_lowpass[band] = 1;
63         /* this band and above will be zeroed: */
64         if (freq >= lowpass2) {
65             *lowpass_band = Min(*lowpass_band, band);
66             amp_lowpass[band] = 0;
67         }
68         if (lowpass1 < freq && freq < lowpass2) {
69             *minband = Min(*minband, band);
70             *maxband = Max(*maxband, band);
71             amp_lowpass[band] = cos((PI / 2) *
72                                     (lowpass1 - freq) / (lowpass2 - lowpass1));
73         }
74         /*
75          * DEBUGF("lowpass band=%i  amp=%f \n",
76          *      band, gfc->amp_lowpass[band]);
77          */
78     }
79 }
80 
81 /* lame_init_params_ppflt */
82 
83 /*}}}*/
84 /* static void   lame_init_params_ppflt         (lame_internal_flags *gfc)                                                                                        *//*{{{ */
85 
86 static void
lame_init_params_ppflt(lame_global_flags * gfp)87 lame_init_params_ppflt(lame_global_flags * gfp)
88 {
89     lame_internal_flags *gfc = gfp->internal_flags;
90   /**/
91     /* compute info needed for polyphase filter (filter type==0, default) */
92   /**/
93 
94     int     band, maxband, minband;
95     FLOAT8  freq;
96 
97     if (gfc->lowpass1 > 0) {
98         minband = 999;
99         maxband = -1;
100         lame_init_params_ppflt_lowpass(gfc->amp_lowpass,
101                                        gfc->lowpass1, gfc->lowpass2,
102                                        &gfc->lowpass_band, &minband, &maxband);
103         /* compute the *actual* transition band implemented by
104          * the polyphase filter */
105         if (minband == 999) {
106             gfc->lowpass1 = (gfc->lowpass_band - .75) / 31.0;
107         }
108         else {
109             gfc->lowpass1 = (minband - .75) / 31.0;
110         }
111         gfc->lowpass2 = gfc->lowpass_band / 31.0;
112 
113         gfc->lowpass_start_band = minband;
114         gfc->lowpass_end_band = maxband;
115 
116         /* as the lowpass may have changed above
117          * calculate the amplification here again
118          */
119         for (band = minband; band <= maxband; band++) {
120             freq = band / 31.0;
121             gfc->amp_lowpass[band] =
122                 cos((PI / 2) * (gfc->lowpass1 - freq) /
123                     (gfc->lowpass2 - gfc->lowpass1));
124         }
125     }
126     else {
127         gfc->lowpass_start_band = 0;
128         gfc->lowpass_end_band = -1; /* do not to run into for-loops */
129     }
130 
131     /* make sure highpass filter is within 90% of what the effective
132      * highpass frequency will be */
133     if (gfc->highpass2 > 0) {
134         if (gfc->highpass2 < .9 * (.75 / 31.0)) {
135             gfc->highpass1 = 0;
136             gfc->highpass2 = 0;
137             MSGF(gfc, "Warning: highpass filter disabled.  "
138                  "highpass frequency too small\n");
139         }
140     }
141 
142     if (gfc->highpass2 > 0) {
143         minband = 999;
144         maxband = -1;
145         for (band = 0; band <= 31; band++) {
146             freq = band / 31.0;
147             gfc->amp_highpass[band] = 1;
148             /* this band and below will be zereod */
149             if (freq <= gfc->highpass1) {
150                 gfc->highpass_band = Max(gfc->highpass_band, band);
151                 gfc->amp_highpass[band] = 0;
152             }
153             if (gfc->highpass1 < freq && freq < gfc->highpass2) {
154                 minband = Min(minband, band);
155                 maxband = Max(maxband, band);
156                 gfc->amp_highpass[band] =
157                     cos((PI / 2) *
158                         (gfc->highpass2 - freq) /
159                         (gfc->highpass2 - gfc->highpass1));
160             }
161             /*
162                DEBUGF("highpass band=%i  amp=%f \n",
163                band, gfc->amp_highpass[band]);
164              */
165         }
166         /* compute the *actual* transition band implemented by
167          * the polyphase filter */
168         gfc->highpass1 = gfc->highpass_band / 31.0;
169         if (maxband == -1) {
170             gfc->highpass2 = (gfc->highpass_band + .75) / 31.0;
171         }
172         else {
173             gfc->highpass2 = (maxband + .75) / 31.0;
174         }
175 
176         gfc->highpass_start_band = minband;
177         gfc->highpass_end_band = maxband;
178 
179         /* as the highpass may have changed above
180          * calculate the amplification here again
181          */
182         for (band = minband; band <= maxband; band++) {
183             freq = band / 31.0;
184             gfc->amp_highpass[band] =
185                 cos((PI / 2) * (gfc->highpass2 - freq) /
186                     (gfc->highpass2 - gfc->highpass1));
187         }
188     }
189     else {
190         gfc->highpass_start_band = 0;
191         gfc->highpass_end_band = -1; /* do not to run into for-loops */
192     }
193     /*
194        DEBUGF("lowpass band with amp=0:  %i \n",gfc->lowpass_band);
195        DEBUGF("highpass band with amp=0:  %i \n",gfc->highpass_band);
196        DEBUGF("lowpass band start:  %i \n",gfc->lowpass_start_band);
197        DEBUGF("lowpass band end:    %i \n",gfc->lowpass_end_band);
198        DEBUGF("highpass band start:  %i \n",gfc->highpass_start_band);
199        DEBUGF("highpass band end:    %i \n",gfc->highpass_end_band);
200      */
201 }
202 
203 /*}}}*/
204 
205 
206 static void
optimum_bandwidth(double * const lowerlimit,double * const upperlimit,const unsigned bitrate,const int samplefreq,const double channels,lame_global_flags * gfp)207 optimum_bandwidth(double *const lowerlimit,
208                   double *const upperlimit,
209                   const unsigned bitrate,
210                   const int samplefreq,
211                   const double channels, lame_global_flags * gfp)
212 {
213 /*
214  *  Input:
215  *      bitrate     total bitrate in bps
216  *      samplefreq  output sampling frequency in Hz
217  *      channels    1 for mono, 2+epsilon for MS stereo, 3 for LR stereo
218  *                  epsilon is the percentage of LR frames for typical audio
219  *                  (I use 'Fade to Gray' by Metallica)
220  *
221  *   Output:
222  *      lowerlimit: best lowpass frequency limit for input filter in Hz
223  *      upperlimit: best highpass frequency limit for input filter in Hz
224  */
225     double  f_low;
226     double  f_high;
227     double  br;
228 
229     assert(bitrate >= 8000 && bitrate <= 320000);
230     assert(samplefreq >= 8000 && samplefreq <= 48000);
231     assert(channels == 1 || (channels >= 2 && channels <= 3));
232 
233     if (samplefreq >= 32000)
234         br =
235             bitrate - (channels ==
236                        1 ? (17 + 4) * 8 : (32 + 4) * 8) * samplefreq / 1152;
237     else
238         br =
239             bitrate - (channels ==
240                        1 ? (9 + 4) * 8 : (17 + 4) * 8) * samplefreq / 576;
241 
242     if (channels >= 2.)
243         br /= 1.75 + 0.25 * (channels - 2.); // MS needs 1.75x mono, LR needs 2.00x mono (experimental data of a lot of albums)
244 
245     br *= 0.5;          // the sine and cosine term must share the bitrate
246 
247 /*
248  *  So, now we have the bitrate for every spectral line.
249  *  Let's look at the current settings:
250  *
251  *    Bitrate   limit    bits/line
252  *     8 kbps   0.34 kHz  4.76
253  *    16 kbps   1.9 kHz   2.06
254  *    24 kbps   2.8 kHz   2.21
255  *    32 kbps   3.85 kHz  2.14
256  *    40 kbps   5.1 kHz   2.06
257  *    48 kbps   5.6 kHz   2.21
258  *    56 kbps   7.0 kHz   2.10
259  *    64 kbps   7.7 kHz   2.14
260  *    80 kbps  10.1 kHz   2.08
261  *    96 kbps  11.2 kHz   2.24
262  *   112 kbps  14.0 kHz   2.12
263  *   128 kbps  15.4 kHz   2.17
264  *   160 kbps  18.2 kHz   2.05
265  *   192 kbps  21.1 kHz   2.14
266  *   224 kbps  22.0 kHz   2.41
267  *   256 kbps  22.0 kHz   2.78
268  *
269  *   What can we see?
270  *       Value for 8 kbps is nonsense (although 8 kbps and stereo is nonsense)
271  *       Values are between 2.05 and 2.24 for 16...192 kbps
272  *       Some bitrate lack the following bitrates have: 16, 40, 80, 160 kbps
273  *       A lot of bits per spectral line have: 24, 48, 96 kbps
274  *
275  *   What I propose?
276  *       A slightly with the bitrate increasing bits/line function. It is
277  *       better to decrease NMR for low bitrates to get a little bit more
278  *       bandwidth. So we have a better trade off between twickling and
279  *       muffled sound.
280  */
281 
282     f_low = br / log10(br * 4.425e-3); // Tests with 8, 16, 32, 64, 112 and 160 kbps
283 
284 /*
285  *  What we get now?
286  *
287  *    Bitrate       limit  bits/line	difference
288  *     8 kbps (8)  1.89 kHz  0.86          +1.6 kHz
289  *    16 kbps (8)  3.16 kHz  1.24          +1.2 kHz
290  *    32 kbps(16)  5.08 kHz  1.54          +1.2 kHz
291  *    56 kbps(22)  7.88 kHz  1.80          +0.9 kHz
292  *    64 kbps(22)  8.83 kHz  1.86          +1.1 kHz
293  *   112 kbps(32) 14.02 kHz  2.12           0.0 kHz
294  *   112 kbps(44) 13.70 kHz  2.11          -0.3 kHz
295  *   128 kbps     15.40 kHz  2.17           0.0 kHz
296  *   160 kbps     16.80 kHz  2.22          -1.4 kHz
297  *   192 kbps     19.66 kHz  2.30          -1.4 kHz
298  *   256 kbps     22.05 kHz  2.78           0.0 kHz
299  */
300 
301 #if 0
302 /*
303  *  Beginning at 128 kbps/jstereo, we can use the following additional
304  *  strategy:
305  *
306  *      For every increase of f_low in a way that the ATH(f_low)
307  *      increases by 4 dB we force an additional NMR of 1.25 dB.
308  *      These are the setting of the VBR quality selecting scheme
309  *      for V <= 4.
310  */
311     {
312         double  br_sw = (128000 - (32 + 4) * 8 * 44100 / 1152) / 1.75 * 0.5;
313         double  f_low_sw = br_sw / log10(br_sw * 4.425e-3);
314 
315         // printf ("br_sw=%f  f_low_sw=%f\n", br_sw, f_low_sw );
316         // printf ("br   =%f  f_low   =%f\n", br   , f_low    );
317         // fflush (stdout);
318 
319         while (f_low > f_low_sw) {
320             double  dATH = ATHformula(f_low, gfp) - ATHformula(f_low_sw, gfp); // [dB]
321             double  dNMR = br / f_low - br_sw / f_low_sw; // bit
322 
323             // printf ("br   =%f  f_low   =%f\n", br   , f_low    );
324             // printf ("dATH =%f  dNMR    =%f\n", dATH , dNMR     );
325             // fflush (stdout);
326 
327 
328             if (dATH / 4.0 < dNMR * 6.0206 / 1.25) // 1 bit = 6.0206... dB
329                 break;
330             f_low -= 25.;
331         }
332     }
333 #endif
334 
335 /*
336  *  Now we try to choose a good high pass filtering frequency.
337  *  This value is currently not used.
338  *    For fu < 16 kHz:  sqrt(fu*fl) = 560 Hz
339  *    For fu = 18 kHz:  no high pass filtering
340  *  This gives:
341  *
342  *   2 kHz => 160 Hz
343  *   3 kHz => 107 Hz
344  *   4 kHz =>  80 Hz
345  *   8 kHz =>  40 Hz
346  *  16 kHz =>  20 Hz
347  *  17 kHz =>  10 Hz
348  *  18 kHz =>   0 Hz
349  *
350  *  These are ad hoc values and these can be optimized if a high pass is available.
351  */
352     if (f_low <= 16000)
353         f_high = 16000. * 20. / f_low;
354     else if (f_low <= 18000)
355         f_high = 180. - 0.01 * f_low;
356     else
357         f_high = 0.;
358 
359     /*
360      *  When we sometimes have a good highpass filter, we can add the highpass
361      *  frequency to the lowpass frequency
362      */
363 
364     if (lowerlimit != NULL)
365         *lowerlimit = f_low /* + f_high */ ;
366     if (upperlimit != NULL)
367         *upperlimit = f_high;
368 /*
369  * Now the weak points:
370  *
371  *   - the formula f_low=br/log10(br*4.425e-3) is an ad hoc formula
372  *     (but has a physical background and is easy to tune)
373  *   - the switch to the ATH based bandwidth selecting is the ad hoc
374  *     value of 128 kbps
375  */
376 }
377 
378 static int
optimum_samplefreq(int lowpassfreq,int input_samplefreq)379 optimum_samplefreq(int lowpassfreq, int input_samplefreq)
380 {
381 /*
382  * Rules:
383  *
384  *  - output sample frequency should NOT be decreased by more than 3% if lowpass allows this
385  *  - if possible, sfb21 should NOT be used
386  *
387  *  Problem: Switches to 32 kHz at 112 kbps
388  */
389     if (input_samplefreq <= 8000 * 1.03 || lowpassfreq <= 3622)
390         return 8000;
391     if (input_samplefreq <= 11025 * 1.03 || lowpassfreq <= 4991)
392         return 11025;
393     if (input_samplefreq <= 12000 * 1.03 || lowpassfreq <= 5620)
394         return 12000;
395     if (input_samplefreq <= 16000 * 1.03 || lowpassfreq <= 7244)
396         return 16000;
397     if (input_samplefreq <= 22050 * 1.03 || lowpassfreq <= 9982)
398         return 22050;
399     if (input_samplefreq <= 24000 * 1.03 || lowpassfreq <= 11240)
400         return 24000;
401     if (input_samplefreq <= 32000 * 1.03 || lowpassfreq <= 15264)
402         return 32000;
403     if (input_samplefreq <= 44100 * 1.03)
404         return 44100;
405     return 48000;
406 }
407 
408 
409 /* set internal feature flags.  USER should not access these since
410  * some combinations will produce strange results */
411 void
lame_init_qval(lame_global_flags * gfp)412 lame_init_qval(lame_global_flags * gfp)
413 {
414     lame_internal_flags *gfc = gfp->internal_flags;
415 
416     switch (gfp->quality) {
417     case 9:            /* no psymodel, no noise shaping */
418         gfc->filter_type = 0;
419         gfc->psymodel = 0;
420         gfc->quantization = 0;
421         gfc->noise_shaping = 0;
422         gfc->noise_shaping_amp = 0;
423         gfc->noise_shaping_stop = 0;
424         gfc->use_best_huffman = 0;
425         break;
426 
427     case 8:
428         gfp->quality = 7;
429     case 7:            /* use psymodel (for short block and m/s switching), but no noise shapping */
430         gfc->filter_type = 0;
431         gfc->psymodel = 1;
432         gfc->quantization = 0;
433         gfc->noise_shaping = 0;
434         gfc->noise_shaping_amp = 0;
435         gfc->noise_shaping_stop = 0;
436         gfc->use_best_huffman = 0;
437         break;
438 
439     case 6:
440         gfp->quality = 5;
441     case 5:            /* the default */
442         gfc->filter_type = 0;
443         gfc->psymodel = 1;
444         gfc->quantization = 0;
445         gfc->noise_shaping = 1;
446          /**/ gfc->noise_shaping_amp = 0;
447         gfc->noise_shaping_stop = 0;
448         gfc->use_best_huffman = 0;
449         break;
450 
451     case 4:
452         gfp->quality = 3;
453     case 3:
454         gfc->filter_type = 0;
455         gfc->psymodel = 1;
456         gfc->quantization = 1;
457         gfc->noise_shaping = 1;
458         gfc->noise_shaping_amp = 0;
459         gfc->noise_shaping_stop = 0;
460         gfc->use_best_huffman = 1;
461         break;
462 
463     case 2:
464         gfc->filter_type = 0;
465         gfc->psymodel = 1;
466         gfc->quantization = 1;
467         gfc->noise_shaping = 1;
468         gfc->noise_shaping_amp = 1;
469         gfc->noise_shaping_stop = 1;
470         gfc->use_best_huffman = 1;
471         break;
472 
473     case 1:
474         gfc->filter_type = 0;
475         gfc->psymodel = 1;
476         gfc->quantization = 1;
477         gfc->noise_shaping = 1;
478         gfc->noise_shaping_amp = 2;
479         gfc->noise_shaping_stop = 1;
480         gfc->use_best_huffman = 1;
481         break;
482 
483     case 0:            /* 0..1 quality */
484         gfc->filter_type = 0; /* 1 not yet coded */
485         gfc->psymodel = 1;
486         gfc->quantization = 1;
487         gfc->noise_shaping = 1; /* 2=usually lowers quality */
488         gfc->noise_shaping_amp = 2;
489         gfc->noise_shaping_stop = 1;
490         gfc->use_best_huffman = 1; /* 2 not yet coded */
491     }
492 
493     /* modifications to the above rules: */
494 
495     /* -Z option enables scalefactor_scale: */
496     if (gfp->experimentalZ) {
497         gfc->noise_shaping = 2;
498     }
499 
500     if (gfp->exp_nspsytune & 1) {
501         if (gfp->quality <= 2)
502             gfc->noise_shaping = 2; /* use scalefac_scale */
503     }
504 
505 }
506 
507 
508 
509 
510 
511 
512 
513 /* int           lame_init_params               (lame_global_flags *gfp)                                                                                          *//*{{{ */
514 
515 /*
516  *   initialize internal params based on data in gf
517  *   (globalflags struct filled in by calling program)
518  *
519  *  OUTLINE:
520  *
521  * We first have some complex code to determine bitrate,
522  * output samplerate and mode.  It is complicated by the fact
523  * that we allow the user to set some or all of these parameters,
524  * and need to determine best possible values for the rest of them:
525  *
526  *  1. set some CPU related flags
527  *  2. check if we are mono->mono, stereo->mono or stereo->stereo
528  *  3.  compute bitrate and output samplerate:
529  *          user may have set compression ratio
530  *          user may have set a bitrate
531  *          user may have set a output samplerate
532  *  4. set some options which depend on output samplerate
533  *  5. compute the actual compression ratio
534  *  6. set mode based on compression ratio
535  *
536  *  The remaining code is much simpler - it just sets options
537  *  based on the mode & compression ratio:
538  *
539  *   set allow_diff_short based on mode
540  *   select lowpass filter based on compression ratio & mode
541  *   set the bitrate index, and min/max bitrates for VBR modes
542  *   disable VBR tag if it is not appropriate
543  *   initialize the bitstream
544  *   initialize scalefac_band data
545  *   set sideinfo_len (based on channels, CRC, out_samplerate)
546  *   write an id3v2 tag into the bitstream
547  *   write VBR tag into the bitstream
548  *   set mpeg1/2 flag
549  *   estimate the number of frames (based on a lot of data)
550  *
551  *   now we set more flags:
552  *   nspsytune:
553  *      see code
554  *   VBR modes
555  *      see code
556  *   CBR/ABR
557  *      see code
558  *
559  *  Finally, we set the algorithm flags based on the gfp->quality value
560  *  lame_init_qval(gfp);
561  *
562  */
563 int
lame_init_params(lame_global_flags * const gfp)564 lame_init_params(lame_global_flags * const gfp)
565 {
566 
567     int     i;
568     int     j;
569     lame_internal_flags *gfc = gfp->internal_flags;
570 
571     gfc->gfp = gfp;
572 
573     gfc->Class_ID = 0;
574 
575     /* report functions */
576     gfc->report.msgf   = gfp->report.msgf;
577     gfc->report.debugf = gfp->report.debugf;
578     gfc->report.errorf = gfp->report.errorf;
579 
580     gfc->CPU_features.i387 = has_i387();
581     gfc->CPU_features.AMD_3DNow = has_3DNow();
582     gfc->CPU_features.MMX = has_MMX();
583     gfc->CPU_features.SIMD = has_SIMD();
584     gfc->CPU_features.SIMD2 = has_SIMD2();
585 
586 
587     if (NULL == gfc->ATH)
588         gfc->ATH = calloc(1, sizeof(ATH_t));
589 
590     if (NULL == gfc->ATH)
591         return -2;  // maybe error codes should be enumerated in lame.h ??
592 
593 #ifdef KLEMM_44
594     /* Select the fastest functions for this CPU */
595     init_scalar_functions(gfc);
596 #endif
597 
598     gfc->channels_in = gfp->num_channels;
599     if (gfc->channels_in == 1)
600         gfp->mode = MONO;
601     gfc->channels_out = (gfp->mode == MONO) ? 1 : 2;
602     gfc->mode_ext = MPG_MD_LR_LR;
603     if (gfp->mode == MONO)
604         gfp->force_ms = 0; // don't allow forced mid/side stereo for mono output
605 
606 
607     if (gfp->VBR != vbr_off) {
608         gfp->free_format = 0; /* VBR can't be mixed with free format */
609     }
610 
611     if (gfp->VBR == vbr_off && gfp->brate == 0) {
612         /* no bitrate or compression ratio specified, use a compression ratio of 11.025 */
613         if (gfp->compression_ratio == 0)
614             gfp->compression_ratio = 11.025;
615 		/* rate to compress a CD down to exactly 128000 bps */
616     }
617 
618 
619     if (gfp->VBR == vbr_off && gfp->brate == 0) {
620         /* no bitrate or compression ratio specified, use 11.025 */
621         if (gfp->compression_ratio == 0)
622             gfp->compression_ratio = 11.025;
623 		/* rate to compress a CD down to exactly 128000 bps */
624     }
625 
626     /* find bitrate if user specify a compression ratio */
627     if (gfp->VBR == vbr_off && gfp->compression_ratio > 0) {
628 
629         if (gfp->out_samplerate == 0)
630             gfp->out_samplerate = map2MP3Frequency(0.97 * gfp->in_samplerate);
631 			/* round up with a margin of 3% */
632 
633         /* choose a bitrate for the output samplerate which achieves
634          * specified compression ratio
635          */
636 	gfp->brate = gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 *
637 		gfp->compression_ratio);
638 
639         /* we need the version for the bitrate table look up */
640         gfc->samplerate_index = SmpFrqIndex(gfp->out_samplerate, &gfp->version);
641 
642         if (!gfp->free_format) /* for non Free Format find the nearest allowed bitrate */
643             gfp->brate =
644                 FindNearestBitrate(gfp->brate, gfp->version,
645                                    gfp->out_samplerate);
646     }
647 
648 	/*
649 	 * at 160 kbps (MPEG-2/2.5)/ 320 kbps (MPEG-1), only Free format
650 	 * or CBR are possible, no VBR
651 	 */
652     if (gfp->VBR != vbr_off && gfp->brate >= 320)
653         gfp->VBR = vbr_off;
654 
655     if (gfp->out_samplerate == 0) {
656 	/* if output sample frequency is not given, find a useful value */
657         gfp->out_samplerate = map2MP3Frequency(0.97 * gfp->in_samplerate);
658 
659 
660         /* check if user specified bitrate requires downsampling, if compression    */
661         /* ratio is > 13, choose a new samplerate to get the ratio down to about 10 */
662 
663         if (gfp->VBR == vbr_off && gfp->brate > 0) {
664             gfp->compression_ratio = gfp->out_samplerate * 16 *
665 		gfc->channels_out / (1.e3 * gfp->brate);
666             if (gfp->compression_ratio > 13.)
667                 gfp->out_samplerate = map2MP3Frequency((10. * 1.e3 *
668 			gfp->brate) / (16 * gfc->channels_out));
669         }
670         if (gfp->VBR == vbr_abr) {
671             gfp->compression_ratio = gfp->out_samplerate * 16 *
672 		gfc->channels_out / (1.e3 * gfp->VBR_mean_bitrate_kbps);
673             if (gfp->compression_ratio > 13.)
674                 gfp->out_samplerate =
675                     map2MP3Frequency((10. * 1.e3 * gfp->VBR_mean_bitrate_kbps) /
676                                      (16 * gfc->channels_out));
677         }
678     }
679 
680     if (gfp->ogg) {
681         gfp->framesize = 1024;
682         gfp->encoder_delay = ENCDELAY;
683         gfc->coding = coding_Ogg_Vorbis;
684     }
685     else {
686         gfc->mode_gr = gfp->out_samplerate <= 24000 ? 1 : 2; // Number of granules per frame
687         gfp->framesize = 576 * gfc->mode_gr;
688         gfp->encoder_delay = ENCDELAY;
689         gfc->coding = coding_MPEG_Layer_3;
690     }
691 
692     gfc->frame_size = gfp->framesize;
693 
694     gfc->resample_ratio = (double) gfp->in_samplerate / gfp->out_samplerate;
695 
696     /*
697      *  sample freq       bitrate     compression ratio
698      *     [kHz]      [kbps/channel]   for 16 bit input
699      *     44.1            56               12.6
700      *     44.1            64               11.025
701      *     44.1            80                8.82
702      *     22.05           24               14.7
703      *     22.05           32               11.025
704      *     22.05           40                8.82
705      *     16              16               16.0
706      *     16              24               10.667
707      *
708      */
709     /*
710      *  For VBR, take a guess at the compression_ratio.
711      *  For example:
712      *
713      *    VBR_q    compression     like
714      *     -        4.4         320 kbps/44 kHz
715      *   0...1      5.5         256 kbps/44 kHz
716      *     2        7.3         192 kbps/44 kHz
717      *     4        8.8         160 kbps/44 kHz
718      *     6       11           128 kbps/44 kHz
719      *     9       14.7          96 kbps
720      *
721      *  for lower bitrates, downsample with --resample
722      */
723 
724     switch (gfp->VBR) {
725     case vbr_mt:
726     case vbr_rh:
727     case vbr_mtrh:
728         {
729             FLOAT8  cmp[] = { 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
730             gfp->compression_ratio = cmp[gfp->VBR_q];
731         }
732         break;
733     case vbr_abr:
734         gfp->compression_ratio = gfp->out_samplerate * 16 * gfc->channels_out /
735 		(1.e3 * gfp->VBR_mean_bitrate_kbps);
736         break;
737     default:
738         gfp->compression_ratio =
739             gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 * gfp->brate);
740         break;
741     }
742 
743 
744     /* mode = -1 (not set by user) or
745      * mode = MONO (because of only 1 input channel).
746      * If mode has been set, then select between STEREO or J-STEREO
747      * At higher quality (lower compression) use STEREO instead of J-STEREO.
748      * (unless the user explicitly specified a mode)
749      *
750      * The threshold to switch to STEREO is:
751      *    48 kHz:   171 kbps (used at 192+)
752      *    44.1 kHz: 160 kbps (used at 160+)
753      *    32 kHz:   119 kbps (used at 128+)
754      *
755      *   Note, that for 32 kHz/128 kbps J-STEREO FM recordings sound much
756      *   better than STEREO, so I'm not so very happy with that.
757      *   fs < 32 kHz I have not tested.
758      */
759     if (gfp->mode == NOT_SET) {
760         if (gfp->compression_ratio < 8)
761             gfp->mode = STEREO;
762         else
763             gfp->mode = JOINT_STEREO;
764     }
765 
766     /* KLEMM's jstereo with ms threshold adjusted via compression ratio */
767     if (gfp->mode_automs) {
768         if (gfp->mode != MONO && gfp->compression_ratio < 6.6)
769             gfp->mode = STEREO;
770     }
771 
772 
773     if (gfp->allow_diff_short == -1) {
774         if (gfp->mode == STEREO)
775             gfp->allow_diff_short = 1;
776     }
777 
778 
779 
780 
781   /**/
782   /* if a filter has not been enabled, see if we should add one: */
783   /**/
784     if (gfp->lowpassfreq == 0) {
785         double  lowpass;
786         double  highpass;
787         double  channels;
788 
789         switch (gfp->mode) {
790         case MONO:
791             channels = 1.;
792             break;
793         case JOINT_STEREO:
794             channels = 2. + 0.00;
795             break;
796         case DUAL_CHANNEL:
797         case STEREO:
798             channels = 3.;
799             break;
800         default:
801             channels = 1.;  // just to make data flow analysis happy :-)
802             assert(0);
803             break;
804         }
805 
806         optimum_bandwidth(&lowpass,
807                           &highpass,
808                           gfp->out_samplerate * 16 * gfc->channels_out /
809                           gfp->compression_ratio, gfp->out_samplerate, channels,
810                           gfp);
811 
812         if (lowpass < 0.5 * gfp->out_samplerate) {
813             //MSGF(gfc,"Lowpass @ %7.1f Hz\n", lowpass);
814             gfc->lowpass1 = gfc->lowpass2 =
815                 lowpass / (0.5 * gfp->out_samplerate);
816         }
817         if (0 && gfp->out_samplerate !=
818             optimum_samplefreq(lowpass, gfp->in_samplerate)) {
819             MSGF(gfc,
820                  "I would suggest to use %u Hz instead of %u Hz sample frequency\n",
821                  optimum_samplefreq(lowpass, gfp->in_samplerate),
822                  gfp->out_samplerate);
823         }
824         fflush(stderr);
825     }
826 
827     /* apply user driven high pass filter */
828     if (gfp->highpassfreq > 0) {
829         gfc->highpass1 = 2. * gfp->highpassfreq / gfp->out_samplerate;
830 					/* will always be >=0 */
831         if (gfp->highpasswidth >= 0)
832             gfc->highpass2 = 2. * (gfp->highpassfreq + gfp->highpasswidth) /
833 		gfp->out_samplerate;
834         else            	/* 0% above on default */
835             gfc->highpass2 =
836                 (1 + 0.00) * 2. * gfp->highpassfreq / gfp->out_samplerate;
837     }
838 
839     /* apply user driven low pass filter */
840     if (gfp->lowpassfreq > 0) {
841         gfc->lowpass2 = 2. * gfp->lowpassfreq / gfp->out_samplerate;
842 			/* will always be >=0 */
843         if (gfp->lowpasswidth >= 0) {
844             gfc->lowpass1 = 2. * (gfp->lowpassfreq - gfp->lowpasswidth) /
845 		gfp->out_samplerate;
846             if (gfc->lowpass1 < 0) /* has to be >= 0 */
847                 gfc->lowpass1 = 0;
848         }
849         else {          /* 0% below on default */
850             gfc->lowpass1 =
851                 (1 - 0.00) * 2. * gfp->lowpassfreq / gfp->out_samplerate;
852         }
853     }
854 
855   /**/
856   /* compute info needed for polyphase filter (filter type==0, default) */
857   /**/
858     lame_init_params_ppflt(gfp);
859 
860 
861   /*
862    * compute info needed for FIR filter (filter_type==1)
863    */
864    /* not yet coded */
865 
866 
867 
868   /*
869    * samplerate and bitrate index
870    */
871     gfc->samplerate_index = SmpFrqIndex(gfp->out_samplerate, &gfp->version);
872     if (gfc->samplerate_index < 0)
873         return -1;
874 
875     if (gfp->VBR == vbr_off) {
876         if (gfp->free_format)
877             gfc->bitrate_index = 0;
878         else {
879             gfc->bitrate_index = BitrateIndex(gfp->brate, gfp->version,
880                                               gfp->out_samplerate);
881             if (gfc->bitrate_index < 0)
882                 return -1;
883         }
884     }
885     else {              /* choose a min/max bitrate for VBR */
886         /* if the user didn't specify VBR_max_bitrate: */
887         gfc->VBR_min_bitrate = 1; /* default: allow   8 kbps (MPEG-2) or  32 kbps (MPEG-1) */
888         gfc->VBR_max_bitrate = 14; /* default: allow 160 kbps (MPEG-2) or 320 kbps (MPEG-1) */
889 
890         if (gfp->VBR_min_bitrate_kbps)
891             if (
892                 (gfc->VBR_min_bitrate =
893                  BitrateIndex(gfp->VBR_min_bitrate_kbps, gfp->version,
894                               gfp->out_samplerate)) < 0) return -1;
895         if (gfp->VBR_max_bitrate_kbps)
896             if (
897                 (gfc->VBR_max_bitrate =
898                  BitrateIndex(gfp->VBR_max_bitrate_kbps, gfp->version,
899                               gfp->out_samplerate)) < 0) return -1;
900 
901         gfp->VBR_min_bitrate_kbps =
902             bitrate_table[gfp->version][gfc->VBR_min_bitrate];
903         gfp->VBR_max_bitrate_kbps =
904             bitrate_table[gfp->version][gfc->VBR_max_bitrate];
905 
906         gfp->VBR_mean_bitrate_kbps =
907             Min(bitrate_table[gfp->version][gfc->VBR_max_bitrate],
908                 gfp->VBR_mean_bitrate_kbps);
909         gfp->VBR_mean_bitrate_kbps =
910             Max(bitrate_table[gfp->version][gfc->VBR_min_bitrate],
911                 gfp->VBR_mean_bitrate_kbps);
912 
913 
914     }
915 
916     /* Do not write VBR tag if VBR flag is not specified */
917     if (gfp->VBR == vbr_off)
918         gfp->bWriteVbrTag = 0;
919     if (gfp->ogg)
920         gfp->bWriteVbrTag = 0;
921     if (gfp->analysis)
922         gfp->bWriteVbrTag = 0;
923 
924     /* some file options not allowed if output is: not specified or stdout */
925     if (gfc->pinfo != NULL)
926         gfp->bWriteVbrTag = 0; /* disable Xing VBR tag */
927 
928     init_bit_stream_w(gfc);
929 
930     j = gfc->samplerate_index + (3 * gfp->version) + 6 * (gfp->out_samplerate <
931                                                           16000);
932     for (i = 0; i < SBMAX_l + 1; i++)
933         gfc->scalefac_band.l[i] = sfBandIndex[j].l[i];
934     for (i = 0; i < SBMAX_s + 1; i++)
935         gfc->scalefac_band.s[i] = sfBandIndex[j].s[i];
936 
937     /* determine the mean bitrate for main data */
938     if (gfp->version == 1)		/* MPEG 1 */
939         gfc->sideinfo_len = (gfc->channels_out == 1) ? 4 + 17 : 4 + 32;
940     else				/* MPEG 2 */
941         gfc->sideinfo_len = (gfc->channels_out == 1) ? 4 + 9 : 4 + 17;
942 
943     if (gfp->error_protection)
944         gfc->sideinfo_len += 2;
945 
946 
947     /*
948      *  Write id3v2 tag into the bitstream.
949      *  This tag must be before the Xing VBR header.
950      */
951     if (!gfp->ogg)
952         id3tag_write_v2(gfp);
953 
954 
955     /* Write initial VBR Header to bitstream */
956     if (gfp->bWriteVbrTag)
957         InitVbrTag(gfp);
958 
959     if (gfp->version == 1) /* 0 indicates use lower sample freqs algorithm */
960         gfc->is_mpeg1 = 1; /* yes */
961     else
962         gfc->is_mpeg1 = 0; /* no */
963 
964     /* estimate total frames.  */
965     gfp->totalframes =
966         2 + gfp->num_samples / (gfc->resample_ratio * gfp->framesize);
967     gfc->Class_ID = LAME_ID;
968 
969     if (gfp->exp_nspsytune & 1) {
970         int     i;
971 
972         gfc->nsPsy.use = 1;
973         gfc->nsPsy.safejoint = (gfp->exp_nspsytune & 2) != 0;
974         for (i = 0; i < 19; i++)
975             gfc->nsPsy.pefirbuf[i] = 700;
976 
977         if (gfp->VBR == vbr_mtrh || gfp->VBR == vbr_mt) {
978             ERRORF(gfc, "\n**** nspsytune doesn't support --vbr-new **** \n\n");
979             gfp->VBR = vbr_rh;
980         }
981 
982         if (gfp->ATHtype == -1)
983             gfp->ATHtype = 0;
984 
985         gfc->nsPsy.bass = gfc->nsPsy.alto = gfc->nsPsy.treble = 0;
986 
987         i = (gfp->exp_nspsytune >> 2) & 63;
988         if (i >= 32)
989             i -= 64;
990         gfc->nsPsy.bass = pow(10, i / 4.0 / 10.0);
991         i = (gfp->exp_nspsytune >> 8) & 63;
992         if (i >= 32)
993             i -= 64;
994         gfc->nsPsy.alto = pow(10, i / 4.0 / 10.0);
995         i = (gfp->exp_nspsytune >> 14) & 63;
996         if (i >= 32)
997             i -= 64;
998         gfc->nsPsy.treble = pow(10, i / 4.0 / 10.0);
999     }
1000 
1001     switch (gfp->VBR) {
1002     case vbr_mtrh:
1003         /*  default quality for --vbr-mtrh is 1
1004          */
1005         if (gfp->quality < 0)
1006             gfp->quality = 1;
1007 
1008         /*  tonality
1009          */
1010         if (gfp->cwlimit <= 0)
1011             gfp->cwlimit = 0.454 * gfp->out_samplerate;
1012 
1013         /*  fall through */
1014     case vbr_mt:
1015         /*  use Gaby's ATH for vbr-mtrh by default
1016          */
1017         if (gfp->ATHtype == -1)
1018             gfp->ATHtype = 2;
1019 
1020         /*  fall through */
1021     case vbr_rh:
1022         /*  use Roel's tweaked Gaby-ATH for VBR by default
1023          */
1024         if (gfp->ATHtype == -1)
1025             gfp->ATHtype = 2;
1026 
1027         /*  automatic ATH adjustment on, VBR modes need it
1028          */
1029         gfc->ATH->use_adjust = 1;
1030 
1031         /*  sfb21 extra only with MPEG-1 at higher sampling rates
1032          */
1033         gfc->sfb21_extra = (gfp->out_samplerate > 44000);
1034 
1035         /*  VBR needs at least the output of GPSYCHO,
1036          *  so we have to garantee that by setting a minimum
1037          *  quality level, actually level 5 does it.
1038          *  the -v and -V x settings switch the quality to level 2
1039          *  you would have to add a -q 5 to reduce the quality
1040          *  down to level 5
1041          */
1042         if (gfp->quality > 5)
1043             gfp->quality = 5;
1044 
1045         /*  default quality setting is 2
1046          */
1047         if (gfp->quality < 0)
1048             gfp->quality = 2;
1049 
1050         /*  allow left and right channels to have different block types
1051          */
1052         gfp->allow_diff_short = 1;
1053         break;
1054     default:
1055         /*  automatic ATH adjustment off, not so important for CBR code
1056          */
1057         gfc->ATH->use_adjust = 0;
1058 
1059         /*  use Frank's ATH for CBR/ABR by default
1060          */
1061         if (gfp->ATHtype == -1)
1062             gfp->ATHtype = 2;
1063 
1064         /*  no sfb21 extra with CBR code
1065          */
1066         gfc->sfb21_extra = 0;
1067 
1068         /*  default quality setting for CBR/ABR is 5
1069          */
1070         if (gfp->quality < 0)
1071             gfp->quality = 5;
1072         break;
1073     }
1074 
1075     /* initialize internal qval settings */
1076     lame_init_qval(gfp);
1077 
1078 #ifdef KLEMM_44
1079     gfc->mfbuf[0] = (sample_t *) calloc(sizeof(sample_t), MFSIZE);
1080     gfc->mfbuf[1] = (sample_t *) calloc(sizeof(sample_t), MFSIZE);
1081     gfc->sampfreq_in = unround_samplefrequency(gfp->in_samplerate);
1082     gfc->sampfreq_out = gfp->out_samplerate;
1083     gfc->resample_in = resample_open(gfc->sampfreq_in, gfc->sampfreq_out,
1084 	-1 .0 /* Auto */ , 32);
1085 #endif
1086     return 0;
1087 }
1088 
1089 /*}}}*/
1090 /* void          lame_print_config              (lame_global_flags *gfp)                                                                                          *//*{{{ */
1091 
1092 /*
1093  *  print_config
1094  *
1095  *  Prints some selected information about the coding parameters via
1096  *  the macro command MSGF(), which is currently mapped to lame_errorf
1097  *  (reports via a error function?), which is a printf-like function
1098  *  for <stderr>.
1099  */
1100 
1101 void
lame_print_config(const lame_global_flags * gfp)1102 lame_print_config(const lame_global_flags * gfp)
1103 {
1104     lame_internal_flags *gfc = gfp->internal_flags;
1105     double  out_samplerate = gfp->out_samplerate;
1106     double  in_samplerate = gfp->out_samplerate * gfc->resample_ratio;
1107 
1108     MSGF(gfc, "mp3enc (from lame version %s (%s))\n", get_lame_version(), get_lame_url());
1109 
1110     if (gfc->CPU_features.MMX
1111         || gfc->CPU_features.AMD_3DNow
1112         || gfc->CPU_features.SIMD || gfc->CPU_features.SIMD2) {
1113         MSGF(gfc, "CPU features:");
1114 
1115         if (gfc->CPU_features.i387)
1116             MSGF(gfc, " i387");
1117         if (gfc->CPU_features.MMX)
1118 #ifdef MMX_choose_table
1119             MSGF(gfc, ", MMX (ASM used)");
1120 #else
1121             MSGF(gfc, ", MMX");
1122 #endif
1123         if (gfc->CPU_features.AMD_3DNow)
1124             MSGF(gfc, ", 3DNow!");
1125         if (gfc->CPU_features.SIMD)
1126             MSGF(gfc, ", SIMD");
1127         if (gfc->CPU_features.SIMD2)
1128             MSGF(gfc, ", SIMD2");
1129         MSGF(gfc, "\n");
1130     }
1131 
1132     if (gfp->num_channels == 2 && gfc->channels_out == 1 /* mono */ ) {
1133         MSGF
1134             (gfc,
1135              "Autoconverting from stereo to mono. Setting encoding to mono mode.\n");
1136     }
1137 
1138     if (gfc->resample_ratio != 1.) {
1139         MSGF(gfc, "Resampling:  input %g kHz  output %g kHz\n",
1140              1.e-3 * in_samplerate, 1.e-3 * out_samplerate);
1141     }
1142 
1143     if (gfc->filter_type == 0) {
1144         if (gfc->highpass2 > 0.)
1145             MSGF
1146                 (gfc,
1147                  "Using polyphase highpass filter, transition band: %5.0f Hz - %5.0f Hz\n",
1148                  0.5 * gfc->highpass1 * out_samplerate,
1149                  0.5 * gfc->highpass2 * out_samplerate);
1150         if (gfc->lowpass1 > 0.) {
1151             MSGF
1152                 (gfc,
1153                  "Using polyphase lowpass  filter, transition band: %5.0f Hz - %5.0f Hz\n",
1154                  0.5 * gfc->lowpass1 * out_samplerate,
1155                  0.5 * gfc->lowpass2 * out_samplerate);
1156         }
1157         else {
1158             MSGF(gfc, "polyphase lowpass filter disabled\n");
1159         }
1160     }
1161     else {
1162         MSGF(gfc, "polyphase filters disabled\n");
1163     }
1164 
1165     if (gfp->free_format) {
1166         MSGF(gfc,
1167              "Warning: many decoders cannot handle free format bitstreams\n");
1168         if (gfp->brate > 320) {
1169             MSGF
1170                 (gfc,
1171                  "Warning: many decoders cannot handle free format bitrates >320 kbps (see documentation)\n");
1172         }
1173     }
1174 }
1175 
1176 
1177 /* int           lame_encode_frame              (lame_global_flags *gfp, sample_t inbuf_l[],sample_t inbuf_r[], char *mp3buf, int mp3buf_size)                    *//*{{{ */
1178 
1179 /* routine to feed exactly one frame (gfp->framesize) worth of data to the
1180 encoding engine.  All buffering, resampling, etc, handled by calling
1181 program.
1182 */
1183 int
lame_encode_frame(lame_global_flags * gfp,sample_t inbuf_l[],sample_t inbuf_r[],unsigned char * mp3buf,int mp3buf_size)1184 lame_encode_frame(lame_global_flags * gfp,
1185                   sample_t inbuf_l[], sample_t inbuf_r[],
1186                   unsigned char *mp3buf, int mp3buf_size)
1187 {
1188     int     ret;
1189     if (gfp->ogg) {
1190 #ifdef HAVE_VORBIS
1191         ret = lame_encode_ogg_frame(gfp, inbuf_l, inbuf_r, mp3buf, mp3buf_size);
1192 #else
1193         return -5;      /* wanna encode ogg without vorbis */
1194 #endif
1195     }
1196     else {
1197         ret = lame_encode_mp3_frame(gfp, inbuf_l, inbuf_r, mp3buf, mp3buf_size);
1198     }
1199 
1200     /* check to see if we underestimated totalframes */
1201     gfp->frameNum++;
1202     if (gfp->totalframes < gfp->frameNum)
1203         gfp->totalframes = gfp->frameNum;
1204     return ret;
1205 }
1206 
1207 /*}}}*/
1208 /* int           lame_encode_buffer             (lame_global_flags* gfp, short int buffer_l[], short int buffer_r[], int nsamples, char* mp3buf, int mp3buf_size )*//*{{{ */
1209 
1210 
1211 
1212 /*
1213  * THE MAIN LAME ENCODING INTERFACE
1214  * mt 3/00
1215  *
1216  * input pcm data, output (maybe) mp3 frames.
1217  * This routine handles all buffering, resampling and filtering for you.
1218  * The required mp3buffer_size can be computed from num_samples,
1219  * samplerate and encoding rate, but here is a worst case estimate:
1220  *
1221  * mp3buffer_size in bytes = 1.25*num_samples + 7200
1222  *
1223  * return code = number of bytes output in mp3buffer.  can be 0
1224 */
1225 int
lame_encode_buffer_sample_t(lame_global_flags * gfp,sample_t buffer_l[],sample_t buffer_r[],int nsamples,unsigned char * mp3buf,const int mp3buf_size)1226 lame_encode_buffer_sample_t(lame_global_flags * gfp,
1227                    sample_t buffer_l[],
1228                    sample_t buffer_r[],
1229                    int nsamples, unsigned char *mp3buf, const int mp3buf_size)
1230 {
1231     lame_internal_flags *gfc = gfp->internal_flags;
1232     int     mp3size = 0, ret, i, ch, mf_needed;
1233     sample_t *mfbuf[2];
1234     sample_t *in_buffer[2];
1235 
1236     if (gfc->Class_ID != LAME_ID)
1237         return -3;
1238 
1239     if (nsamples == 0)
1240         return 0;
1241 
1242     in_buffer[0]=buffer_l;
1243     in_buffer[1]=buffer_r;
1244 
1245 
1246     /* some sanity checks */
1247 #if ENCDELAY < MDCTDELAY
1248 # error ENCDELAY is less than MDCTDELAY, see encoder.h
1249 #endif
1250 #if FFTOFFSET > BLKSIZE
1251 # error FFTOFFSET is greater than BLKSIZE, see encoder.h
1252 #endif
1253 
1254     mf_needed = BLKSIZE + gfp->framesize - FFTOFFSET; /* amount needed for FFT */
1255     mf_needed = Max(mf_needed, 286 + 576 * (1 + gfc->mode_gr)); /* amount needed for MDCT/filterbank */
1256     assert(MFSIZE >= mf_needed);
1257 
1258     mfbuf[0] = gfc->mfbuf[0];
1259     mfbuf[1] = gfc->mfbuf[1];
1260 
1261     if (gfp->num_channels == 2 && gfc->channels_out == 1) {
1262         /* downsample to mono */
1263         for (i = 0; i < nsamples; ++i) {
1264             in_buffer[0][i] =
1265                 0.5 * ((FLOAT8) in_buffer[0][i] + in_buffer[1][i]);
1266             in_buffer[1][i] = 0.0;
1267         }
1268     }
1269 
1270 
1271     while (nsamples > 0) {
1272         int     n_in = 0;    /* number of input samples processed with fill_buffer */
1273         int     n_out = 0;   /* number of samples output with fill_buffer */
1274         /* n_in <> n_out if we are resampling */
1275 
1276         /* copy in new samples into mfbuf, with resampling & scaling if necessary */
1277         fill_buffer(gfp, mfbuf, in_buffer, nsamples, &n_in, &n_out);
1278 
1279         /* update in_buffer counters */
1280         nsamples -= n_in;
1281         in_buffer[0] += n_in;
1282         if (gfc->channels_out == 2)
1283             in_buffer[1] += n_in;
1284 
1285         /* update mfbuf[] counters */
1286         gfc->mf_size += n_out;
1287         assert(gfc->mf_size <= MFSIZE);
1288         gfc->mf_samples_to_encode += n_out;
1289 
1290 
1291         if (gfc->mf_size >= mf_needed) {
1292             /* encode the frame.  */
1293             ret =
1294                 lame_encode_frame(gfp, mfbuf[0], mfbuf[1], mp3buf, mp3buf_size);
1295 
1296             if (ret < 0)
1297                 goto retr;
1298             mp3buf += ret;
1299             mp3size += ret;
1300 
1301             /* shift out old samples */
1302             gfc->mf_size -= gfp->framesize;
1303             gfc->mf_samples_to_encode -= gfp->framesize;
1304             for (ch = 0; ch < gfc->channels_out; ch++)
1305                 for (i = 0; i < gfc->mf_size; i++)
1306                     mfbuf[ch][i] = mfbuf[ch][i + gfp->framesize];
1307         }
1308     }
1309     assert(nsamples == 0);
1310     ret = mp3size;
1311 
1312   retr:
1313     return ret;
1314 }
1315 
1316 
1317 int
lame_encode_buffer(lame_global_flags * gfp,const short int buffer_l[],const short int buffer_r[],int nsamples,unsigned char * mp3buf,const int mp3buf_size)1318 lame_encode_buffer(lame_global_flags * gfp,
1319                    const short int buffer_l[],
1320                    const short int buffer_r[],
1321                    int nsamples, unsigned char *mp3buf, const int mp3buf_size)
1322 {
1323     lame_internal_flags *gfc = gfp->internal_flags;
1324     int     ret, i;
1325     sample_t *in_buffer[2];
1326 
1327     if (gfc->Class_ID != LAME_ID)
1328         return -3;
1329 
1330     if (nsamples == 0)
1331         return 0;
1332 
1333     in_buffer[0] = calloc(sizeof(sample_t), nsamples);
1334     in_buffer[1] = calloc(sizeof(sample_t), nsamples);
1335 
1336     if (in_buffer[0] == NULL || in_buffer[1] == NULL) {
1337         ERRORF(gfc, "Error: can't allocate in_buffer buffer\n");
1338         return -2;
1339     }
1340 
1341     /* make a copy of input buffer, changing type to sample_t */
1342     for (i = 0; i < nsamples; i++) {
1343         in_buffer[0][i] = buffer_l[i];
1344         in_buffer[1][i] = buffer_r[i];
1345     }
1346 
1347     ret = lame_encode_buffer_sample_t(gfp,in_buffer[0],in_buffer[1],
1348 				      nsamples, mp3buf, mp3buf_size);
1349 
1350     free(in_buffer[0]);
1351     free(in_buffer[1]);
1352     return ret;
1353 }
1354 
1355 
1356 int
lame_encode_buffer_float(lame_global_flags * gfp,const float buffer_l[],const float buffer_r[],int nsamples,unsigned char * mp3buf,const int mp3buf_size)1357 lame_encode_buffer_float(lame_global_flags * gfp,
1358                    const float buffer_l[],
1359                    const float buffer_r[],
1360                    int nsamples, unsigned char *mp3buf, const int mp3buf_size)
1361 {
1362     lame_internal_flags *gfc = gfp->internal_flags;
1363     int     ret, i;
1364     sample_t *in_buffer[2];
1365 
1366     if (gfc->Class_ID != LAME_ID)
1367         return -3;
1368 
1369     if (nsamples == 0)
1370         return 0;
1371 
1372     in_buffer[0] = calloc(sizeof(sample_t), nsamples);
1373     in_buffer[1] = calloc(sizeof(sample_t), nsamples);
1374 
1375     if (in_buffer[0] == NULL || in_buffer[1] == NULL) {
1376         ERRORF(gfc, "Error: can't allocate in_buffer buffer\n");
1377         return -2;
1378     }
1379 
1380     /* make a copy of input buffer, changing type to sample_t */
1381     for (i = 0; i < nsamples; i++) {
1382         in_buffer[0][i] = buffer_l[i];
1383         in_buffer[1][i] = buffer_r[i];
1384     }
1385 
1386     ret = lame_encode_buffer_sample_t(gfp,in_buffer[0],in_buffer[1],
1387 				      nsamples, mp3buf, mp3buf_size);
1388 
1389     free(in_buffer[0]);
1390     free(in_buffer[1]);
1391     return ret;
1392 }
1393 
1394 
1395 
1396 int
lame_encode_buffer_long(lame_global_flags * gfp,const long buffer_l[],const long buffer_r[],int nsamples,unsigned char * mp3buf,const int mp3buf_size)1397 lame_encode_buffer_long(lame_global_flags * gfp,
1398                    const long buffer_l[],
1399                    const long buffer_r[],
1400                    int nsamples, unsigned char *mp3buf, const int mp3buf_size)
1401 {
1402     lame_internal_flags *gfc = gfp->internal_flags;
1403     int     ret, i;
1404     sample_t *in_buffer[2];
1405 
1406     if (gfc->Class_ID != LAME_ID)
1407         return -3;
1408 
1409     if (nsamples == 0)
1410         return 0;
1411 
1412     in_buffer[0] = calloc(sizeof(sample_t), nsamples);
1413     in_buffer[1] = calloc(sizeof(sample_t), nsamples);
1414 
1415     if (in_buffer[0] == NULL || in_buffer[1] == NULL) {
1416         ERRORF(gfc, "Error: can't allocate in_buffer buffer\n");
1417         return -2;
1418     }
1419 
1420     /* make a copy of input buffer, changing type to sample_t */
1421     for (i = 0; i < nsamples; i++) {
1422         in_buffer[0][i] = buffer_l[i];
1423         in_buffer[1][i] = buffer_r[i];
1424     }
1425 
1426     ret = lame_encode_buffer_sample_t(gfp,in_buffer[0],in_buffer[1],
1427 				      nsamples, mp3buf, mp3buf_size);
1428 
1429     free(in_buffer[0]);
1430     free(in_buffer[1]);
1431     return ret;
1432 }
1433 
1434 
1435 
1436 
1437 
1438 
1439 
1440 
1441 
1442 
1443 
1444 int
lame_encode_buffer_interleaved(lame_global_flags * gfp,short int buffer[],int nsamples,unsigned char * mp3buf,int mp3buf_size)1445 lame_encode_buffer_interleaved(lame_global_flags * gfp,
1446                                short int buffer[],
1447                                int nsamples,
1448                                unsigned char *mp3buf, int mp3buf_size)
1449 {
1450     int     ret, i;
1451     short int *buffer_l;
1452     short int *buffer_r;
1453 
1454     buffer_l = malloc(sizeof(short int) * nsamples);
1455     buffer_r = malloc(sizeof(short int) * nsamples);
1456     if (buffer_l == NULL || buffer_r == NULL) {
1457         return -2;
1458     }
1459     for (i = 0; i < nsamples; i++) {
1460         buffer_l[i] = buffer[2 * i];
1461         buffer_r[i] = buffer[2 * i + 1];
1462     }
1463     ret =
1464         lame_encode_buffer(gfp, buffer_l, buffer_r, nsamples, mp3buf,
1465                            mp3buf_size);
1466     free(buffer_l);
1467     free(buffer_r);
1468     return ret;
1469 
1470 }
1471 
1472 
1473 /*}}}*/
1474 /* int           lame_encode                    (lame_global_flags* gfp, short int in_buffer[2][1152], char* mp3buf, int size )                                   *//*{{{ */
1475 
1476 
1477 /* old LAME interface.  use lame_encode_buffer instead */
1478 
1479 int
lame_encode(lame_global_flags * const gfp,const short int in_buffer[2][1152],unsigned char * const mp3buf,const int size)1480 lame_encode(lame_global_flags * const gfp,
1481             const short int in_buffer[2][1152],
1482             unsigned char *const mp3buf, const int size)
1483 {
1484     lame_internal_flags *gfc = gfp->internal_flags;
1485 
1486     if (gfc->Class_ID != LAME_ID)
1487         return -3;
1488 
1489     return lame_encode_buffer(gfp, in_buffer[0], in_buffer[1], gfp->framesize,
1490                               mp3buf, size);
1491 }
1492 
1493 /*}}}*/
1494 /* int           lame_encode_flush              (lame_global_flags* gfp, char* mp3buffer, int mp3buffer_size )                                                    *//*{{{ */
1495 
1496 /**/
1497 /* flush internal mp3 buffers,                                   */
1498 /**/
1499 
1500 int
lame_encode_flush(lame_global_flags * gfp,unsigned char * mp3buffer,int mp3buffer_size)1501 lame_encode_flush(lame_global_flags * gfp,
1502                   unsigned char *mp3buffer, int mp3buffer_size)
1503 {
1504     short int buffer[2][1152];
1505     int     imp3 = 0, mp3count, mp3buffer_size_remaining;
1506     lame_internal_flags *gfc = gfp->internal_flags;
1507 
1508     memset(buffer, 0, sizeof(buffer));
1509     mp3count = 0;
1510 
1511     while (gfc->mf_samples_to_encode > 0) {
1512 
1513         mp3buffer_size_remaining = mp3buffer_size - mp3count;
1514 
1515         /* if user specifed buffer size = 0, dont check size */
1516         if (mp3buffer_size == 0)
1517             mp3buffer_size_remaining = 0;
1518 
1519         /* send in a frame of 0 padding until all internal sample buffers
1520          * are flushed
1521          */
1522         imp3 = lame_encode_buffer(gfp, buffer[0], buffer[1], gfp->framesize,
1523                                   mp3buffer, mp3buffer_size_remaining);
1524         /* don't count the above padding: */
1525         gfc->mf_samples_to_encode -= gfp->framesize;
1526 
1527         if (imp3 < 0) {
1528             /* some type of fatal error */
1529             return imp3;
1530         }
1531         mp3buffer += imp3;
1532         mp3count += imp3;
1533     }
1534 
1535     mp3buffer_size_remaining = mp3buffer_size - mp3count;
1536     /* if user specifed buffer size = 0, dont check size */
1537     if (mp3buffer_size == 0)
1538         mp3buffer_size_remaining = 0;
1539 
1540     if (gfp->ogg) {
1541 #ifdef HAVE_VORBIS
1542         /* ogg related stuff */
1543         imp3 = lame_encode_ogg_finish(gfp, mp3buffer, mp3buffer_size_remaining);
1544 #endif
1545     }
1546     else {
1547         /* mp3 related stuff.  bit buffer might still contain some mp3 data */
1548         flush_bitstream(gfp);
1549         /* write a id3 tag to the bitstream */
1550         id3tag_write_v1(gfp);
1551         imp3 = copy_buffer(mp3buffer, mp3buffer_size_remaining, &gfc->bs);
1552     }
1553 
1554     if (imp3 < 0) {
1555         return imp3;
1556     }
1557     mp3count += imp3;
1558     return mp3count;
1559 }
1560 
1561 /*}}}*/
1562 /* void          lame_close                     (lame_global_flags *gfp)                                                                                          *//*{{{ */
1563 
1564 /*
1565  *
1566  *      lame_close ()
1567  *
1568  *  frees internal buffers
1569  *
1570  */
1571 
1572 int
lame_close(lame_global_flags * gfp)1573 lame_close(lame_global_flags * gfp)
1574 {
1575     lame_internal_flags *gfc = gfp->internal_flags;
1576 
1577     if (gfc->Class_ID != LAME_ID)
1578         return -3;
1579 
1580     gfc->Class_ID = 0;
1581 
1582     // this routien will free all malloc'd data in gfc, and then free gfc:
1583     freegfc(gfc);
1584 
1585     gfp->internal_flags = NULL;
1586 
1587     if (gfp->lame_allocated_gfp)
1588         free(gfp);
1589 
1590     return 0;
1591 }
1592 
1593 
1594 /*}}}*/
1595 /* int           lame_encode_finish             (lame_global_flags* gfp, char* mp3buffer, int mp3buffer_size )                                                    *//*{{{ */
1596 
1597 
1598 /**/
1599 /* flush internal mp3 buffers, and free internal buffers         */
1600 /**/
1601 
1602 int
lame_encode_finish(lame_global_flags * gfp,unsigned char * mp3buffer,int mp3buffer_size)1603 lame_encode_finish(lame_global_flags * gfp,
1604                    unsigned char *mp3buffer, int mp3buffer_size)
1605 {
1606     int     ret = lame_encode_flush(gfp, mp3buffer, mp3buffer_size);
1607 
1608     lame_close(gfp);
1609 
1610     return ret;
1611 }
1612 
1613 /*}}}*/
1614 /* void          lame_mp3_tags_fid              (lame_global_flags *gfp,FILE *fpStream)                                                                           *//*{{{ */
1615 
1616 /**/
1617 /* write VBR Xing header, and ID3 version 1 tag, if asked for    */
1618 /**/
1619 void
lame_mp3_tags_fid(lame_global_flags * gfp,FILE * fpStream)1620 lame_mp3_tags_fid(lame_global_flags * gfp, FILE * fpStream)
1621 {
1622     if (gfp->bWriteVbrTag && (gfp->VBR != vbr_off)) {
1623         /* Map VBR_q to Xing quality value: 0=worst, 100=best */
1624         int     nQuality = ((9-gfp->VBR_q) * 100) / 9;
1625 
1626         /* Write Xing header again */
1627         if (fpStream && !fseek(fpStream, 0, SEEK_SET))
1628             PutVbrTag(gfp, fpStream, nQuality);
1629     }
1630 
1631 
1632 }
1633 /*}}}*/
1634 /* lame_global_flags *lame_init                 (void)                                                                                                            *//*{{{ */
1635 
1636 lame_global_flags *
lame_init(void)1637 lame_init(void)
1638 {
1639     lame_global_flags *gfp;
1640     int     ret;
1641 
1642     gfp = calloc(1, sizeof(lame_global_flags));
1643     if (gfp == NULL)
1644         return NULL;
1645 
1646     ret = lame_init_old(gfp);
1647     if (ret != 0) {
1648         free(gfp);
1649         return NULL;
1650     }
1651 
1652     gfp->lame_allocated_gfp = 1;
1653     return gfp;
1654 }
1655 
1656 /*}}}*/
1657 /* int           lame_init_old                  (lame_global_flags *gfp)                                                                                          *//*{{{ */
1658 
1659 /* initialize mp3 encoder */
1660 int
lame_init_old(lame_global_flags * gfp)1661 lame_init_old(lame_global_flags * gfp)
1662 {
1663     lame_internal_flags *gfc;
1664 
1665     disable_FPE();      // disable floating point exceptions
1666 
1667     memset(gfp, 0, sizeof(lame_global_flags));
1668 
1669     if (NULL ==
1670         (gfc = gfp->internal_flags =
1671          calloc(1, sizeof(lame_internal_flags)))) return -1;
1672 
1673     /* Global flags.  set defaults here for non-zero values */
1674     /* see lame.h for description */
1675     /* set integer values to -1 to mean that LAME will compute the
1676      * best value, UNLESS the calling program as set it
1677      * (and the value is no longer -1)
1678      */
1679 
1680 
1681     gfp->mode = NOT_SET;
1682     gfp->original = 1;
1683     gfp->in_samplerate = 1000 * 44.1;
1684     gfp->num_channels = 2;
1685     gfp->num_samples = MAX_U_32_NUM;
1686 
1687     gfp->bWriteVbrTag = 1;
1688     gfp->quality = -1;
1689     gfp->allow_diff_short = -1;
1690 
1691     gfp->lowpassfreq = 0;
1692     gfp->highpassfreq = 0;
1693     gfp->lowpasswidth = -1;
1694     gfp->highpasswidth = -1;
1695 
1696     gfp->padding_type = 2;
1697     gfp->VBR = vbr_off;
1698     gfp->VBR_q = 4;
1699     gfp->VBR_mean_bitrate_kbps = 128;
1700     gfp->VBR_min_bitrate_kbps = 0;
1701     gfp->VBR_max_bitrate_kbps = 0;
1702     gfp->VBR_hard_min = 0;
1703 
1704 
1705     gfc->resample_ratio = 1;
1706     gfc->lowpass_band = 32;
1707     gfc->highpass_band = -1;
1708     gfc->VBR_min_bitrate = 1; /* not  0 ????? */
1709     gfc->VBR_max_bitrate = 13; /* not 14 ????? */
1710 
1711     gfc->OldValue[0] = 180;
1712     gfc->OldValue[1] = 180;
1713     gfc->CurrentStep = 4;
1714     gfc->masking_lower = 1;
1715 
1716     gfp->ATHtype = -1;  /* default = -1 = set in lame_init_params */
1717     gfp->useTemporal = 1;
1718 
1719     /* The reason for
1720      *       int mf_samples_to_encode = ENCDELAY + 288;
1721      * ENCDELAY = internal encoder delay.  And then we have to add 288
1722      * because of the 50% MDCT overlap.  A 576 MDCT granule decodes to
1723      * 1152 samples.  To synthesize the 576 samples centered under this granule
1724      * we need the previous granule for the first 288 samples (no problem), and
1725      * the next granule for the next 288 samples (not possible if this is last
1726      * granule).  So we need to pad with 288 samples to make sure we can
1727      * encode the 576 samples we are interested in.
1728      */
1729     gfc->mf_samples_to_encode = ENCDELAY + 288;
1730     gfc->mf_size = ENCDELAY - MDCTDELAY; /* we pad input with this many 0's */
1731 
1732 #ifdef KLEMM_44
1733     /* XXX: this wasn't protectes by KLEMM_44 initially! */
1734     gfc->last_ampl = gfc->ampl = +1.0;
1735 #endif
1736 
1737     return 0;
1738 }
1739 
1740 /*}}}*/
1741 
1742 /*
1743  *
1744  *  some simple statistics
1745  *
1746  *  Robert Hegemann 2000-10-11
1747  *
1748  */
1749 
1750 /*  histogram of used bitrate indexes:
1751  *  One has to weight them to calculate the average bitrate in kbps
1752  *
1753  *  bitrate indices:
1754  *  there are 14 possible bitrate indices, 0 has the special meaning
1755  *  "free format" which is not possible to mix with VBR and 15 is forbidden
1756  *  anyway.
1757  *
1758  *  stereo modes:
1759  *  0: LR   number of left-right encoded frames
1760  *  1: LR-I number of left-right and intensity encoded frames
1761  *  2: MS   number of mid-side encoded frames
1762  *  3: MS-I number of mid-side and intensity encoded frames
1763  *
1764  *  4: number of encoded frames
1765  *
1766  */
1767 
1768 void
lame_bitrate_hist(const lame_global_flags * const gfp,int bitrate_count[14])1769 lame_bitrate_hist(const lame_global_flags * const gfp, int bitrate_count[14])
1770 {
1771     const lame_internal_flags *gfc;
1772     int     i;
1773 
1774     if (NULL == bitrate_count)
1775         return;
1776     if (NULL == gfp)
1777         return;
1778     gfc = gfp->internal_flags;
1779     if (NULL == gfc)
1780         return;
1781 
1782     for (i = 0; i < 14; i++)
1783         bitrate_count[i] = gfc->bitrate_stereoMode_Hist[i + 1][4];
1784 }
1785 
1786 
1787 void
lame_bitrate_kbps(const lame_global_flags * const gfp,int bitrate_kbps[14])1788 lame_bitrate_kbps(const lame_global_flags * const gfp, int bitrate_kbps[14])
1789 {
1790     const lame_internal_flags *gfc;
1791     int     i;
1792 
1793     if (NULL == bitrate_kbps)
1794         return;
1795     if (NULL == gfp)
1796         return;
1797     gfc = gfp->internal_flags;
1798     if (NULL == gfc)
1799         return;
1800 
1801     for (i = 0; i < 14; i++)
1802         bitrate_kbps[i] = bitrate_table[gfp->version][i + 1];
1803 }
1804 
1805 
1806 
1807 void
lame_stereo_mode_hist(const lame_global_flags * const gfp,int stmode_count[4])1808 lame_stereo_mode_hist(const lame_global_flags * const gfp, int stmode_count[4])
1809 {
1810     const lame_internal_flags *gfc;
1811     int     i;
1812 
1813     if (NULL == stmode_count)
1814         return;
1815     if (NULL == gfp)
1816         return;
1817     gfc = gfp->internal_flags;
1818     if (NULL == gfc)
1819         return;
1820 
1821     for (i = 0; i < 4; i++) {
1822         int     j, sum = 0;
1823         for (j = 0; j < 14; j++)
1824             sum += gfc->bitrate_stereoMode_Hist[j + 1][i];
1825         stmode_count[i] = sum;
1826     }
1827 }
1828 
1829 
1830 
1831 void
lame_bitrate_stereo_mode_hist(const lame_global_flags * const gfp,int bitrate_stmode_count[14][4])1832 lame_bitrate_stereo_mode_hist(const lame_global_flags * const gfp,
1833                               int bitrate_stmode_count[14][4])
1834 {
1835     const lame_internal_flags *gfc;
1836     int     i;
1837     int     j;
1838 
1839     if (NULL == bitrate_stmode_count)
1840         return;
1841     if (NULL == gfp)
1842         return;
1843     gfc = gfp->internal_flags;
1844     if (NULL == gfc)
1845         return;
1846 
1847     for (j = 0; j < 14; j++)
1848         for (i = 0; i < 4; i++)
1849             bitrate_stmode_count[j][i] = gfc->bitrate_stereoMode_Hist[j + 1][i];
1850 }
1851 
1852 /* end of lame.c */
1853