1 /* -*- mode: C; mode: fold -*- */
3 * LAME MP3 encoding engine
5 * Copyright (c) 1999 Mark Taylor
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.
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.
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.
23 /* $Id: lame.c,v 1.2 2006/02/09 16:54:20 kramm Exp $ */
26 #include "config_static.h"
30 #include "lame-analysis.h"
33 #include "bitstream.h"
36 #include "quantize_pvt.h"
39 #if defined(__FreeBSD__) && !defined(__alpha__)
40 #include <floatingpoint.h>
52 lame_init_params_ppflt_lowpass(FLOAT8 amp_lowpass[32], FLOAT lowpass1,
53 FLOAT lowpass2, int *lowpass_band,
54 int *minband, int *maxband)
59 for (band = 0; band <= 31; band++) {
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;
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));
74 * DEBUGF("lowpass band=%i amp=%f \n",
75 * band, gfc->amp_lowpass[band]);
80 /* lame_init_params_ppflt */
83 /* static void lame_init_params_ppflt (lame_internal_flags *gfc) *//*{{{ */
86 lame_init_params_ppflt(lame_global_flags * gfp)
88 lame_internal_flags *gfc = gfp->internal_flags;
89 /***************************************************************/
90 /* compute info needed for polyphase filter (filter type==0, default) */
91 /***************************************************************/
93 int band, maxband, minband;
96 if (gfc->lowpass1 > 0) {
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;
108 gfc->lowpass1 = (minband - .75) / 31.0;
110 gfc->lowpass2 = gfc->lowpass_band / 31.0;
112 gfc->lowpass_start_band = minband;
113 gfc->lowpass_end_band = maxband;
115 /* as the lowpass may have changed above
116 * calculate the amplification here again
118 for (band = minband; band <= maxband; band++) {
120 gfc->amp_lowpass[band] =
121 cos((PI / 2) * (gfc->lowpass1 - freq) /
122 (gfc->lowpass2 - gfc->lowpass1));
126 gfc->lowpass_start_band = 0;
127 gfc->lowpass_end_band = -1; /* do not to run into for-loops */
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)) {
136 MSGF(gfc, "Warning: highpass filter disabled. "
137 "highpass frequency too small\n");
141 if (gfc->highpass2 > 0) {
144 for (band = 0; band <= 31; band++) {
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;
152 if (gfc->highpass1 < freq && freq < gfc->highpass2) {
153 minband = Min(minband, band);
154 maxband = Max(maxband, band);
155 gfc->amp_highpass[band] =
157 (gfc->highpass2 - freq) /
158 (gfc->highpass2 - gfc->highpass1));
161 DEBUGF("highpass band=%i amp=%f \n",
162 band, gfc->amp_highpass[band]);
165 /* compute the *actual* transition band implemented by
166 * the polyphase filter */
167 gfc->highpass1 = gfc->highpass_band / 31.0;
169 gfc->highpass2 = (gfc->highpass_band + .75) / 31.0;
172 gfc->highpass2 = (maxband + .75) / 31.0;
175 gfc->highpass_start_band = minband;
176 gfc->highpass_end_band = maxband;
178 /* as the highpass may have changed above
179 * calculate the amplification here again
181 for (band = minband; band <= maxband; band++) {
183 gfc->amp_highpass[band] =
184 cos((PI / 2) * (gfc->highpass2 - freq) /
185 (gfc->highpass2 - gfc->highpass1));
189 gfc->highpass_start_band = 0;
190 gfc->highpass_end_band = -1; /* do not to run into for-loops */
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);
206 optimum_bandwidth(double *const lowerlimit,
207 double *const upperlimit,
208 const unsigned bitrate,
209 const int samplefreq,
210 const double channels)
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)
221 * lowerlimit: best lowpass frequency limit for input filter in Hz
222 * upperlimit: best highpass frequency limit for input filter in Hz
228 assert(bitrate >= 8000 && bitrate <= 320000);
229 assert(samplefreq >= 8000 && samplefreq <= 48000);
230 assert(channels == 1 || (channels >= 2 && channels <= 3));
232 if (samplefreq >= 32000)
234 bitrate - (channels ==
235 1 ? (17 + 4) * 8 : (32 + 4) * 8) * samplefreq / 1152;
238 bitrate - (channels ==
239 1 ? (9 + 4) * 8 : (17 + 4) * 8) * samplefreq / 576;
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)
244 br *= 0.5; // the sine and cosine term must share the bitrate
247 * So, now we have the bitrate for every spectral line.
248 * Let's look at the current settings:
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
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
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
281 f_low = br / log10(br * 4.425e-3); // Tests with 8, 16, 32, 64, 112 and 160 kbps
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
290 I'm also extending this to CBR as tests showed that a
291 limited bandwidth is increasing quality
294 f_low = 18400+(f_low-18400)/4;
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
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
329 * These are ad hoc values and these can be optimized if a high pass is available.
332 f_high = 16000. * 20. / f_low;
333 else if (f_low <= 18000)
334 f_high = 180. - 0.01 * f_low;
339 * When we sometimes have a good highpass filter, we can add the highpass
340 * frequency to the lowpass frequency
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;
348 * Now the weak points:
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
358 optimum_samplefreq(int lowpassfreq, int input_samplefreq)
363 * - output sample frequency should NOT be decreased by more than 3% if lowpass allows this
364 * - if possible, sfb21 should NOT be used
366 * Problem: Switches to 32 kHz at 112 kbps
368 if (input_samplefreq <= 8000 * 1.03 || lowpassfreq <= 3622)
370 if (input_samplefreq <= 11025 * 1.03 || lowpassfreq <= 4991)
372 if (input_samplefreq <= 12000 * 1.03 || lowpassfreq <= 5620)
374 if (input_samplefreq <= 16000 * 1.03 || lowpassfreq <= 7244)
376 if (input_samplefreq <= 22050 * 1.03 || lowpassfreq <= 9982)
378 if (input_samplefreq <= 24000 * 1.03 || lowpassfreq <= 11240)
380 if (input_samplefreq <= 32000 * 1.03 || lowpassfreq <= 15264)
382 if (input_samplefreq <= 44100 * 1.03)
388 /* set internal feature flags. USER should not access these since
389 * some combinations will produce strange results */
391 lame_init_qval(lame_global_flags * gfp)
393 lame_internal_flags *gfc = gfp->internal_flags;
395 switch (gfp->quality) {
396 case 9: /* no psymodel, no noise shaping */
397 gfc->filter_type = 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;
408 case 7: /* use psymodel (for short block and m/s switching), but no noise shapping */
409 gfc->filter_type = 0;
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;
420 case 5: /* the default */
421 gfc->filter_type = 0;
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;
433 gfc->filter_type = 0;
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;
443 gfc->filter_type = 0;
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;
453 gfc->filter_type = 0;
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;
462 case 0: /* 0..1 quality */
463 gfc->filter_type = 0; /* 1 not yet coded */
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 */
472 /* modifications to the above rules: */
474 /* -Z option toggles scalefactor_scale: */
475 if ( (gfp->experimentalZ & 1) > 0) {
476 gfc->noise_shaping = 2;
486 /* int lame_init_params (lame_global_flags *gfp) *//*{{{ */
488 /********************************************************************
489 * initialize internal params based on data in gf
490 * (globalflags struct filled in by calling program)
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:
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
509 * The remaining code is much simpler - it just sets options
510 * based on the mode & compression ratio:
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
522 * estimate the number of frames (based on a lot of data)
524 * now we set more flags:
532 * Finally, we set the algorithm flags based on the gfp->quality value
533 * lame_init_qval(gfp);
535 ********************************************************************/
537 lame_init_params(lame_global_flags * const gfp)
542 lame_internal_flags *gfc = gfp->internal_flags;
548 /* report functions */
549 gfc->report.msgf = gfp->report.msgf;
550 gfc->report.debugf = gfp->report.debugf;
551 gfc->report.errorf = gfp->report.errorf;
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();
560 if (NULL == gfc->ATH)
561 gfc->ATH = calloc(1, sizeof(ATH_t));
563 if (NULL == gfc->ATH)
564 return -2; // maybe error codes should be enumerated in lame.h ??
566 if (NULL == gfc->VBR)
567 gfc->VBR = calloc(1, sizeof(VBR_t));
568 if (NULL == gfc->VBR)
571 if (NULL == gfc->PSY)
572 gfc->PSY = calloc(1, sizeof(PSY_t));
573 if (NULL == gfc->PSY)
577 /* Select the fastest functions for this CPU */
578 init_scalar_functions(gfc);
581 gfc->channels_in = gfp->num_channels;
582 if (gfc->channels_in == 1)
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
590 if (gfp->VBR != vbr_off) {
591 gfp->free_format = 0; /* VBR can't be mixed with free format */
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 */
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 */
607 /* find bitrate if user specify a compression ratio */
608 if (gfp->VBR == vbr_off && gfp->compression_ratio > 0) {
610 if (gfp->out_samplerate == 0)
611 gfp->out_samplerate = map2MP3Frequency( (int)( 0.97 * gfp->in_samplerate ) ); /* round up with a margin of 3% */
613 /* choose a bitrate for the output samplerate which achieves
614 * specified compression ratio
617 gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 *
621 /* we need the version for the bitrate table look up */
622 gfc->samplerate_index = SmpFrqIndex(gfp->out_samplerate, &gfp->version);
624 if (!gfp->free_format) /* for non Free Format find the nearest allowed bitrate */
626 FindNearestBitrate(gfp->brate, gfp->version,
627 gfp->out_samplerate);
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 */
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 ) );
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 */
641 if (gfp->VBR == vbr_off && gfp->brate > 0) {
642 gfp->compression_ratio =
643 gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 *
645 if (gfp->compression_ratio > 13.)
646 gfp->out_samplerate =
647 map2MP3Frequency( (int)( (10. * 1.e3 * gfp->brate) /
648 (16 * gfc->channels_out)));
650 if (gfp->VBR == vbr_abr) {
651 gfp->compression_ratio =
652 gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 *
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)));
663 gfp->framesize = 1024;
664 gfp->encoder_delay = ENCDELAY;
665 gfc->coding = coding_Ogg_Vorbis;
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;
674 gfc->frame_size = gfp->framesize;
676 gfc->resample_ratio = (double) gfp->in_samplerate / gfp->out_samplerate;
679 * sample freq bitrate compression ratio
680 * [kHz] [kbps/channel] for 16 bit input
692 * For VBR, take a guess at the compression_ratio.
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
703 * for lower bitrates, downsample with --resample
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];
716 gfp->compression_ratio =
717 gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 *
719 VBR_mean_bitrate_kbps);
722 gfp->compression_ratio =
723 gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 * gfp->brate);
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)
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+)
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.
743 if (gfp->mode == NOT_SET) {
744 if (gfp->compression_ratio < 8)
747 gfp->mode = JOINT_STEREO;
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)
761 /****************************************************************/
762 /* if a filter has not been enabled, see if we should add one: */
763 /****************************************************************/
764 if (gfp->lowpassfreq == 0) {
774 channels = 2. + 0.00;
781 channels = 1.; // just to make data flow analysis happy :-)
786 optimum_bandwidth(&lowpass,
788 gfp->out_samplerate * 16 * gfc->channels_out /
789 gfp->compression_ratio, gfp->out_samplerate, channels);
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);
798 gfp->lowpassfreq = lowpass;
801 if (gfp->out_samplerate !=
802 optimum_samplefreq(lowpass, gfp->in_samplerate)) {
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);
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)
817 2. * (gfp->highpassfreq +
818 gfp->highpasswidth) / gfp->out_samplerate;
819 else /* 0% above on default */
821 (1 + 0.00) * 2. * gfp->highpassfreq / gfp->out_samplerate;
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) {
829 2. * (gfp->lowpassfreq -
830 gfp->lowpasswidth) / gfp->out_samplerate;
831 if (gfc->lowpass1 < 0) /* has to be >= 0 */
834 else { /* 0% below on default */
836 (1 - 0.00) * 2. * gfp->lowpassfreq / gfp->out_samplerate;
843 /**********************************************************************/
844 /* compute info needed for polyphase filter (filter type==0, default) */
845 /**********************************************************************/
846 lame_init_params_ppflt(gfp);
849 /*******************************************************/
850 /* compute info needed for FIR filter (filter_type==1) */
851 /*******************************************************/
856 /*******************************************************
857 * samplerate and bitrate index
858 *******************************************************/
859 gfc->samplerate_index = SmpFrqIndex(gfp->out_samplerate, &gfp->version);
860 if (gfc->samplerate_index < 0)
863 if (gfp->VBR == vbr_off) {
864 if (gfp->free_format) {
865 gfc->bitrate_index = 0;
868 gfc->bitrate_index = BitrateIndex(gfp->brate, gfp->version,
869 gfp->out_samplerate);
870 if (gfc->bitrate_index < 0)
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) */
879 if (gfp->VBR_min_bitrate_kbps)
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)
886 (gfc->VBR_max_bitrate =
887 BitrateIndex(gfp->VBR_max_bitrate_kbps, gfp->version,
888 gfp->out_samplerate)) < 0) return -1;
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];
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);
905 /* for CBR, we will write an "info" tag. */
906 // if ((gfp->VBR == vbr_off))
907 // gfp->bWriteVbrTag = 0;
910 gfp->bWriteVbrTag = 0;
911 #if defined(HAVE_GTK)
913 gfp->bWriteVbrTag = 0;
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 */
920 init_bit_stream_w(gfc);
923 gfc->samplerate_index + (3 * gfp->version) + 6 * (gfp->out_samplerate <
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];
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;
934 gfc->sideinfo_len = (gfc->channels_out == 1) ? 4 + 9 : 4 + 17;
936 if (gfp->error_protection)
937 gfc->sideinfo_len += 2;
939 lame_init_bitstream(gfp);
942 if (gfp->version == 1) /* 0 indicates use lower sample freqs algorithm */
943 gfc->is_mpeg1 = 1; /* yes */
945 gfc->is_mpeg1 = 0; /* no */
947 gfc->Class_ID = LAME_ID;
950 if (gfp->exp_nspsytune & 1) {
954 gfc->nsPsy.safejoint = (gfp->exp_nspsytune & 2) != 0;
955 for (i = 0; i < 19; i++)
956 gfc->nsPsy.pefirbuf[i] = 700;
958 if (gfp->ATHtype == -1)
961 gfc->nsPsy.bass = gfc->nsPsy.alto = gfc->nsPsy.treble = 0;
963 i = (gfp->exp_nspsytune >> 2) & 63;
966 gfc->nsPsy.bass = pow(10, i / 4.0 / 10.0);
967 i = (gfp->exp_nspsytune >> 8) & 63;
970 gfc->nsPsy.alto = pow(10, i / 4.0 / 10.0);
971 i = (gfp->exp_nspsytune >> 14) & 63;
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;
980 gfc->nsPsy.sfb21 = pow(10, (i+j) / 4.0 / 10.0);
983 assert( gfp->VBR_q <= 9 );
984 assert( gfp->VBR_q >= 0 );
986 gfc->PSY->tonalityPatch = 0;
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");
1004 if (gfp->cwlimit <= 0) gfp->cwlimit = 0.42 * gfp->out_samplerate;
1005 gfc->PSY->tonalityPatch = 1;
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];
1012 else /* defaulting to */
1014 gfc->VBR->quality = 2;
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
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;
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;
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;
1038 gfc->VBR->bitpressure = 1;
1040 if (gfp->experimentalY)
1041 gfc->sfb21_extra = 0;
1043 gfc->sfb21_extra = (gfp->out_samplerate > 44000);
1045 if ( gfp->athaa_type < 0 )
1046 gfc->ATH->use_adjust = 3;
1048 gfc->ATH->use_adjust = gfp->athaa_type;
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];
1062 gfc->PSY->tonalityPatch = 1;
1063 gfc->VBR->mask_adjust = dbQ[gfp->VBR_q];
1066 gfc->VBR->bitpressure = 1;
1068 /* use Gabriel's adaptative ATH shape for VBR by default
1070 if (gfp->ATHtype == -1)
1073 /* automatic ATH adjustment on, VBR modes need it
1075 if ( gfp->athaa_type < 0 )
1076 gfc->ATH->use_adjust = 3;
1078 gfc->ATH->use_adjust = gfp->athaa_type;
1080 /* sfb21 extra only with MPEG-1 at higher sampling rates
1082 if ( gfp->experimentalY )
1083 gfc->sfb21_extra = 0;
1085 gfc->sfb21_extra = (gfp->out_samplerate > 44000);
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
1094 if (gfp->quality > 5)
1098 /* default quality setting is 2
1100 if (gfp->quality < 0)
1107 if (gfp->ATHtype == -1)
1110 /* automatic ATH adjustment off by default
1111 * not so important for CBR code?
1113 if ( gfp->athaa_type < 0 )
1114 gfc->ATH->use_adjust = 0;
1116 gfc->ATH->use_adjust = gfp->athaa_type;
1119 /* no sfb21 extra with CBR code
1121 gfc->sfb21_extra = 0;
1123 /* default quality setting for CBR/ABR is 5
1125 if (gfp->quality < 0)
1130 /* just another daily changing developer switch */
1131 if ( gfp->tune ) gfc->VBR->mask_adjust = gfp->tune_value_a;
1133 /* initialize internal qval settings */
1134 lame_init_qval(gfp);
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);
1145 /* initialize internal adaptive ATH settings -jd */
1146 gfc->athaa_sensitivity_p = pow( 10.0, gfp->athaa_sensitivity / -10.0 );
1149 gfc->PSY->cwlimit = gfp->cwlimit <= 0 ? 8871.7f : gfp->cwlimit;
1151 if (gfp->short_blocks == short_block_not_set) {
1152 gfp->short_blocks = short_block_allowed;
1154 if (gfp->short_blocks == short_block_allowed && gfp->mode == JOINT_STEREO) {
1155 gfp->short_blocks = short_block_coupled;
1158 if ( gfp->athaa_loudapprox < 0 ) gfp->athaa_loudapprox = 2;
1160 if (gfp->useTemporal < 0 ) gfp->useTemporal = 1; // on by default
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");
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");
1177 /* void lame_print_config (lame_global_flags *gfp) *//*{{{ */
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
1189 lame_print_config(const lame_global_flags * gfp)
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;
1195 MSGF(gfc, "LAME version %s (%s)\n", get_lame_version(), get_lame_url());
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:");
1202 if (gfc->CPU_features.i387)
1204 if (gfc->CPU_features.MMX)
1205 #ifdef MMX_choose_table
1206 MSGF(gfc, ", MMX (ASM used)");
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");
1219 if (gfp->num_channels == 2 && gfc->channels_out == 1 /* mono */ ) {
1222 "Autoconverting from stereo to mono. Setting encoding to mono mode.\n");
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);
1230 if (gfc->filter_type == 0) {
1231 if (gfc->highpass2 > 0.)
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.) {
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);
1245 MSGF(gfc, "polyphase lowpass filter disabled\n");
1249 MSGF(gfc, "polyphase filters disabled\n");
1252 if (gfp->free_format) {
1254 "Warning: many decoders cannot handle free format bitstreams\n");
1255 if (gfp->brate > 320) {
1258 "Warning: many decoders cannot handle free format bitrates >320 kbps (see documentation)\n");
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...
1270 lame_print_internals( const lame_global_flags * gfp )
1272 lame_internal_flags *gfc = gfp->internal_flags;
1273 const char * pc = "";
1275 /* compiler/processor optimizations, operational, etc.
1277 MSGF( gfc, "\nmisc:\n\n" );
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;
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" );
1294 /* everything controlling the stream format
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;
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;
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;
1319 MSGF( gfc, "\tpadding: %s\n", pc );
1321 if ( vbr_default == gfp->VBR ) pc = "(default)";
1322 else if ( gfp->free_format ) pc = "(free format)";
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;
1332 if (gfp->bWriteVbrTag)
1333 MSGF( gfc, "\tusing LAME Tag\n" );
1334 MSGF( gfc, "\t...\n" );
1336 /* everything controlling psychoacoustic settings, like ATH, etc.
1338 MSGF( gfc, "\npsychoacoustic:\n\n" );
1340 MSGF( gfc, "\ttonality estimation limit: %f Hz\n", gfc->PSY->cwlimit );
1341 switch ( gfp->short_blocks ) {
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;
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 );
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 );
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) );
1371 pc = gfp->useTemporal ? "yes" : "no";
1372 MSGF( gfc, "\tusing temporal masking effect: %s\n", pc );
1373 MSGF( gfc, "\t...\n" );
1383 /* int lame_encode_frame (lame_global_flags *gfp, sample_t inbuf_l[],sample_t inbuf_r[], char *mp3buf, int mp3buf_size) *//*{{{ */
1385 /* routine to feed exactly one frame (gfp->framesize) worth of data to the
1386 encoding engine. All buffering, resampling, etc, handled by calling
1390 lame_encode_frame(lame_global_flags * gfp,
1391 sample_t inbuf_l[], sample_t inbuf_r[],
1392 unsigned char *mp3buf, int mp3buf_size)
1396 #ifdef HAVE_VORBIS_ENCODER
1397 ret = lame_encode_ogg_frame(gfp, inbuf_l, inbuf_r, mp3buf, mp3buf_size);
1399 return -5; /* wanna encode ogg without vorbis */
1403 ret = lame_encode_mp3_frame(gfp, inbuf_l, inbuf_r, mp3buf, mp3buf_size);
1413 /* int lame_encode_buffer (lame_global_flags* gfp, short int buffer_l[], short int buffer_r[], int nsamples, char* mp3buf, int mp3buf_size )*//*{{{ */
1418 * THE MAIN LAME ENCODING INTERFACE
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:
1426 * mp3buffer_size in bytes = 1.25*num_samples + 7200
1428 * return code = number of bytes output in mp3buffer. can be 0
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)
1436 lame_internal_flags *gfc = gfp->internal_flags;
1437 int mp3size = 0, ret, i, ch, mf_needed;
1440 sample_t *in_buffer[2];
1442 if (gfc->Class_ID != LAME_ID)
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
1455 in_buffer[0]=buffer_l;
1456 in_buffer[1]=buffer_r;
1459 /* some sanity checks */
1460 #if ENCDELAY < MDCTDELAY
1461 # error ENCDELAY is less than MDCTDELAY, see encoder.h
1463 #if FFTOFFSET > BLKSIZE
1464 # error FFTOFFSET is greater than BLKSIZE, see encoder.h
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);
1471 mfbuf[0] = gfc->mfbuf[0];
1472 mfbuf[1] = gfc->mfbuf[1];
1474 if (gfp->num_channels == 2 && gfc->channels_out == 1) {
1475 /* downsample to mono */
1476 for (i = 0; i < nsamples; ++i) {
1478 0.5 * ((FLOAT8) in_buffer[0][i] + in_buffer[1][i]);
1479 in_buffer[1][i] = 0.0;
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 */
1489 /* copy in new samples into mfbuf, with resampling & scaling if necessary */
1490 fill_buffer(gfp, mfbuf, in_buffer, nsamples, &n_in, &n_out);
1492 /* update in_buffer counters */
1494 in_buffer[0] += n_in;
1495 if (gfc->channels_out == 2)
1496 in_buffer[1] += n_in;
1498 /* update mfbuf[] counters */
1499 gfc->mf_size += n_out;
1500 assert(gfc->mf_size <= MFSIZE);
1501 gfc->mf_samples_to_encode += n_out;
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
1514 int buf_size=mp3buf_size - mp3size;
1515 if (mp3buf_size==0) buf_size=0;
1518 lame_encode_frame(gfp, mfbuf[0], mfbuf[1], mp3buf,buf_size);
1520 if (ret < 0) goto retr;
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];
1532 assert(nsamples == 0);
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)
1546 lame_internal_flags *gfc = gfp->internal_flags;
1548 sample_t *in_buffer[2];
1550 if (gfc->Class_ID != LAME_ID)
1556 in_buffer[0] = calloc(sizeof(sample_t), nsamples);
1557 in_buffer[1] = calloc(sizeof(sample_t), nsamples);
1559 if (in_buffer[0] == NULL || in_buffer[1] == NULL) {
1560 ERRORF(gfc, "Error: can't allocate in_buffer buffer\n");
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];
1570 ret = lame_encode_buffer_sample_t(gfp,in_buffer[0],in_buffer[1],
1571 nsamples, mp3buf, mp3buf_size);
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)
1585 lame_internal_flags *gfc = gfp->internal_flags;
1587 sample_t *in_buffer[2];
1589 if (gfc->Class_ID != LAME_ID)
1595 in_buffer[0] = calloc(sizeof(sample_t), nsamples);
1596 in_buffer[1] = calloc(sizeof(sample_t), nsamples);
1598 if (in_buffer[0] == NULL || in_buffer[1] == NULL) {
1599 ERRORF(gfc, "Error: can't allocate in_buffer buffer\n");
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];
1609 ret = lame_encode_buffer_sample_t(gfp,in_buffer[0],in_buffer[1],
1610 nsamples, mp3buf, mp3buf_size);
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)
1625 lame_internal_flags *gfc = gfp->internal_flags;
1627 sample_t *in_buffer[2];
1629 if (gfc->Class_ID != LAME_ID)
1635 in_buffer[0] = calloc(sizeof(sample_t), nsamples);
1636 in_buffer[1] = calloc(sizeof(sample_t), nsamples);
1638 if (in_buffer[0] == NULL || in_buffer[1] == NULL) {
1639 ERRORF(gfc, "Error: can't allocate in_buffer buffer\n");
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)));
1650 ret = lame_encode_buffer_sample_t(gfp,in_buffer[0],in_buffer[1],
1651 nsamples, mp3buf, mp3buf_size);
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)
1668 lame_internal_flags *gfc = gfp->internal_flags;
1670 sample_t *in_buffer[2];
1672 if (gfc->Class_ID != LAME_ID)
1678 in_buffer[0] = calloc(sizeof(sample_t), nsamples);
1679 in_buffer[1] = calloc(sizeof(sample_t), nsamples);
1681 if (in_buffer[0] == NULL || in_buffer[1] == NULL) {
1682 ERRORF(gfc, "Error: can't allocate in_buffer buffer\n");
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)));
1693 ret = lame_encode_buffer_sample_t(gfp,in_buffer[0],in_buffer[1],
1694 nsamples, mp3buf, mp3buf_size);
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)
1710 lame_internal_flags *gfc = gfp->internal_flags;
1712 sample_t *in_buffer[2];
1714 if (gfc->Class_ID != LAME_ID)
1720 in_buffer[0] = calloc(sizeof(sample_t), nsamples);
1721 in_buffer[1] = calloc(sizeof(sample_t), nsamples);
1723 if (in_buffer[0] == NULL || in_buffer[1] == NULL) {
1724 ERRORF(gfc, "Error: can't allocate in_buffer buffer\n");
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];
1734 ret = lame_encode_buffer_sample_t(gfp,in_buffer[0],in_buffer[1],
1735 nsamples, mp3buf, mp3buf_size);
1753 lame_encode_buffer_interleaved(lame_global_flags * gfp,
1756 unsigned char *mp3buf, int mp3buf_size)
1759 short int *buffer_l;
1760 short int *buffer_r;
1762 buffer_l = malloc(sizeof(short int) * nsamples);
1763 buffer_r = malloc(sizeof(short int) * nsamples);
1764 if (buffer_l == NULL || buffer_r == NULL) {
1767 for (i = 0; i < nsamples; i++) {
1768 buffer_l[i] = buffer[2 * i];
1769 buffer_r[i] = buffer[2 * i + 1];
1772 lame_encode_buffer(gfp, buffer_l, buffer_r, nsamples, mp3buf,
1782 /* int lame_encode (lame_global_flags* gfp, short int in_buffer[2][1152], char* mp3buf, int size ) *//*{{{ */
1785 /* old LAME interface. use lame_encode_buffer instead */
1788 lame_encode(lame_global_flags * const gfp,
1789 const short int in_buffer[2][1152],
1790 unsigned char *const mp3buf, const int size)
1792 lame_internal_flags *gfc = gfp->internal_flags;
1794 if (gfc->Class_ID != LAME_ID)
1797 return lame_encode_buffer(gfp, in_buffer[0], in_buffer[1], gfp->framesize,
1802 /* int lame_encode_flush (lame_global_flags* gfp, char* mp3buffer, int mp3buffer_size ) *//*{{{ */
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
1814 *********************************************************************/
1816 lame_encode_flush_nogap(lame_global_flags * gfp,
1817 unsigned char *mp3buffer, int mp3buffer_size)
1819 lame_internal_flags *gfc = gfp->internal_flags;
1820 flush_bitstream(gfp);
1821 return copy_buffer(gfc,mp3buffer, mp3buffer_size,1);
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 */
1828 lame_init_bitstream(lame_global_flags * gfp)
1830 lame_internal_flags *gfc = gfp->internal_flags;
1835 id3tag_write_v2(gfp);
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;
1845 /* Write initial VBR Header to bitstream and init VBR data */
1846 if (gfp->bWriteVbrTag)
1854 /*****************************************************************/
1855 /* flush internal PCM sample buffers, then mp3 buffers */
1856 /* then write id3 v1 tags into bitstream. */
1857 /*****************************************************************/
1860 lame_encode_flush(lame_global_flags * gfp,
1861 unsigned char *mp3buffer, int mp3buffer_size)
1863 lame_internal_flags *gfc = gfp->internal_flags;
1864 short int buffer[2][1152];
1865 int imp3 = 0, mp3count, mp3buffer_size_remaining;
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;
1871 memset(buffer, 0, sizeof(buffer));
1875 while (gfc->mf_samples_to_encode > 0) {
1877 mp3buffer_size_remaining = mp3buffer_size - mp3count;
1879 /* if user specifed buffer size = 0, dont check size */
1880 if (mp3buffer_size == 0)
1881 mp3buffer_size_remaining = 0;
1883 /* send in a frame of 0 padding until all internal sample buffers
1886 imp3 = lame_encode_buffer(gfp, buffer[0], buffer[1], gfp->framesize,
1887 mp3buffer, mp3buffer_size_remaining);
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;
1898 /* some type of fatal error */
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;
1911 #ifdef HAVE_VORBIS_ENCODER
1912 /* ogg related stuff */
1913 imp3 = lame_encode_ogg_finish(gfp, mp3buffer, mp3buffer_size_remaining);
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);
1921 /* some type of fatal error */
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;
1932 /* write a id3 tag to the bitstream */
1933 id3tag_write_v1(gfp);
1934 imp3 = copy_buffer(gfc,mp3buffer, mp3buffer_size_remaining, 0);
1941 gfp->encoder_padding=end_padding;
1946 /* void lame_close (lame_global_flags *gfp) *//*{{{ */
1948 /***********************************************************************
1952 * frees internal buffers
1954 ***********************************************************************/
1957 lame_close(lame_global_flags * gfp)
1959 lame_internal_flags *gfc = gfp->internal_flags;
1961 if (gfc->Class_ID != LAME_ID)
1966 // this routien will free all malloc'd data in gfc, and then free gfc:
1969 gfp->internal_flags = NULL;
1971 if (gfp->lame_allocated_gfp)
1979 /* int lame_encode_finish (lame_global_flags* gfp, char* mp3buffer, int mp3buffer_size ) *//*{{{ */
1982 /*****************************************************************/
1983 /* flush internal mp3 buffers, and free internal buffers */
1984 /*****************************************************************/
1987 lame_encode_finish(lame_global_flags * gfp,
1988 unsigned char *mp3buffer, int mp3buffer_size)
1990 int ret = lame_encode_flush(gfp, mp3buffer, mp3buffer_size);
1998 /* void lame_mp3_tags_fid (lame_global_flags *gfp,FILE *fpStream) *//*{{{ */
2000 /*****************************************************************/
2001 /* write VBR Xing header, and ID3 version 1 tag, if asked for */
2002 /*****************************************************************/
2004 lame_mp3_tags_fid(lame_global_flags * gfp, FILE * fpStream)
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;
2010 /* Write Xing header again */
2011 if (fpStream && !fseek(fpStream, 0, SEEK_SET))
2012 PutVbrTag(gfp, fpStream, nQuality);
2018 /* lame_global_flags *lame_init (void) *//*{{{ */
2023 lame_global_flags *gfp;
2026 gfp = calloc(1, sizeof(lame_global_flags));
2030 ret = lame_init_old(gfp);
2036 gfp->lame_allocated_gfp = 1;
2041 /* int lame_init_old (lame_global_flags *gfp) *//*{{{ */
2043 /* initialize mp3 encoder */
2045 lame_init_old(lame_global_flags * gfp)
2047 lame_internal_flags *gfc;
2049 disable_FPE(); // disable floating point exceptions
2051 memset(gfp, 0, sizeof(lame_global_flags));
2054 (gfc = gfp->internal_flags =
2055 calloc(1, sizeof(lame_internal_flags)))) return -1;
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)
2065 gfp->mode = NOT_SET;
2067 gfp->in_samplerate = 1000 * 44.1;
2068 gfp->num_channels = 2;
2069 gfp->num_samples = MAX_U_32_NUM;
2071 gfp->bWriteVbrTag = 1;
2073 gfp->short_blocks = short_block_not_set;
2075 gfp->lowpassfreq = 0;
2076 gfp->highpassfreq = 0;
2077 gfp->lowpasswidth = -1;
2078 gfp->highpasswidth = -1;
2080 gfp->padding_type = PAD_ADJUST;
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;
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 ????? */
2095 gfc->OldValue[0] = 180;
2096 gfc->OldValue[1] = 180;
2097 gfc->CurrentStep = 4;
2098 gfc->masking_lower = 1;
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;
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.
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 */
2122 /* XXX: this wasn't protectes by KLEMM_44 initially! */
2123 gfc->last_ampl = gfc->ampl = +1.0;
2130 /***********************************************************************
2132 * some simple statistics
2134 * Robert Hegemann 2000-10-11
2136 ***********************************************************************/
2138 /* histogram of used bitrate indexes:
2139 * One has to weight them to calculate the average bitrate in kbps
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
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
2152 * 4: number of encoded frames
2157 lame_bitrate_hist(const lame_global_flags * const gfp, int bitrate_count[14])
2159 const lame_internal_flags *gfc;
2162 if (NULL == bitrate_count)
2166 gfc = gfp->internal_flags;
2170 for (i = 0; i < 14; i++)
2171 bitrate_count[i] = gfc->bitrate_stereoMode_Hist[i + 1][4];
2176 lame_bitrate_kbps(const lame_global_flags * const gfp, int bitrate_kbps[14])
2178 const lame_internal_flags *gfc;
2181 if (NULL == bitrate_kbps)
2185 gfc = gfp->internal_flags;
2189 for (i = 0; i < 14; i++)
2190 bitrate_kbps[i] = bitrate_table[gfp->version][i + 1];
2196 lame_stereo_mode_hist(const lame_global_flags * const gfp, int stmode_count[4])
2198 const lame_internal_flags *gfc;
2201 if (NULL == stmode_count)
2205 gfc = gfp->internal_flags;
2209 for (i = 0; i < 4; i++) {
2211 for (j = 0; j < 14; j++)
2212 sum += gfc->bitrate_stereoMode_Hist[j + 1][i];
2213 stmode_count[i] = sum;
2220 lame_bitrate_stereo_mode_hist(const lame_global_flags * const gfp,
2221 int bitrate_stmode_count[14][4])
2223 const lame_internal_flags *gfc;
2227 if (NULL == bitrate_stmode_count)
2231 gfc = gfp->internal_flags;
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];