1
/* -*- mode: C; mode: fold -*- */
3
* LAME MP3 encoding engine
5
* Copyright (c) 1999-2000 Mark Taylor
6
* Copyright (c) 2000-2005 Takehiro Tominaga
7
* Copyright (c) 2000-2005 Robert Hegemann
8
* Copyright (c) 2000-2005 Gabriel Bouvigne
9
* Copyright (c) 2000-2004 Alexander Leidinger
11
* This library is free software; you can redistribute it and/or
12
* modify it under the terms of the GNU Lesser General Public
13
* License as published by the Free Software Foundation; either
14
* version 2 of the License, or (at your option) any later version.
16
* This library is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
* Library General Public License for more details.
21
* You should have received a copy of the GNU Lesser General Public
22
* License along with this library; if not, write to the
23
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24
* Boston, MA 02111-1307, USA.
27
/* $Id: lame.c,v 1.323.2.8 2010/02/20 21:08:55 robert Exp $ */
39
#include "lame_global_flags.h"
40
#include "gain_analysis.h"
41
#include "bitstream.h"
42
#include "quantize_pvt.h"
50
#if defined(__FreeBSD__) && !defined(__alpha__)
51
#include <floatingpoint.h>
58
/* woraround for SunOS 4.x, it has SEEK_* defined here */
63
#define LAME_DEFAULT_QUALITY 3
73
return cos(PI / 2 * x);
77
lame_init_params_ppflt(lame_global_flags const *gfp)
79
lame_internal_flags *const gfc = gfp->internal_flags;
80
/***************************************************************/
81
/* compute info needed for polyphase filter (filter type==0, default) */
82
/***************************************************************/
84
int band, maxband, minband;
86
int lowpass_band = 32;
87
int highpass_band = -1;
89
if (gfc->lowpass1 > 0) {
91
for (band = 0; band <= 31; band++) {
93
/* this band and above will be zeroed: */
94
if (freq >= gfc->lowpass2) {
95
lowpass_band = Min(lowpass_band, band);
97
if (gfc->lowpass1 < freq && freq < gfc->lowpass2) {
98
minband = Min(minband, band);
102
/* compute the *actual* transition band implemented by
103
* the polyphase filter */
104
if (minband == 999) {
105
gfc->lowpass1 = (lowpass_band - .75) / 31.0;
108
gfc->lowpass1 = (minband - .75) / 31.0;
110
gfc->lowpass2 = lowpass_band / 31.0;
113
/* make sure highpass filter is within 90% of what the effective
114
* highpass frequency will be */
115
if (gfc->highpass2 > 0) {
116
if (gfc->highpass2 < .9 * (.75 / 31.0)) {
119
MSGF(gfc, "Warning: highpass filter disabled. " "highpass frequency too small\n");
123
if (gfc->highpass2 > 0) {
125
for (band = 0; band <= 31; band++) {
127
/* this band and below will be zereod */
128
if (freq <= gfc->highpass1) {
129
highpass_band = Max(highpass_band, band);
131
if (gfc->highpass1 < freq && freq < gfc->highpass2) {
132
maxband = Max(maxband, band);
135
/* compute the *actual* transition band implemented by
136
* the polyphase filter */
137
gfc->highpass1 = highpass_band / 31.0;
139
gfc->highpass2 = (highpass_band + .75) / 31.0;
142
gfc->highpass2 = (maxband + .75) / 31.0;
146
for (band = 0; band < 32; band++) {
149
if (gfc->highpass2 > gfc->highpass1) {
150
fc1 = filter_coef((gfc->highpass2 - freq) / (gfc->highpass2 - gfc->highpass1 + 1e-20));
155
if (gfc->lowpass2 > gfc->lowpass1) {
156
fc2 = filter_coef((freq - gfc->lowpass1) / (gfc->lowpass2 - gfc->lowpass1 + 1e-20));
161
gfc->amp_filter[band] = fc1 * fc2;
167
optimum_bandwidth(double *const lowerlimit, double *const upperlimit, const unsigned bitrate)
171
* bitrate total bitrate in kbps
174
* lowerlimit: best lowpass frequency limit for input filter in Hz
175
* upperlimit: best highpass frequency limit for input filter in Hz
180
int bitrate; /* only indicative value */
184
const band_pass_t freq_map[] = {
205
table_index = nearestBitrateFullIndex(bitrate);
207
*lowerlimit = freq_map[table_index].lowpass;
211
* Now we try to choose a good high pass filtering frequency.
212
* This value is currently not used.
213
* For fu < 16 kHz: sqrt(fu*fl) = 560 Hz
214
* For fu = 18 kHz: no high pass filtering
225
* These are ad hoc values and these can be optimized if a high pass is available.
227
/* if (f_low <= 16000)
228
f_high = 16000. * 20. / f_low;
229
else if (f_low <= 18000)
230
f_high = 180. - 0.01 * f_low;
235
* When we sometimes have a good highpass filter, we can add the highpass
236
* frequency to the lowpass frequency
239
/*if (upperlimit != NULL)
240
*upperlimit = f_high;*/
246
optimum_samplefreq(int lowpassfreq, int input_samplefreq)
250
* - if possible, sfb21 should NOT be used
253
int suggested_samplefreq = 44100;
255
if (input_samplefreq >= 48000)
256
suggested_samplefreq = 48000;
257
else if (input_samplefreq >= 44100)
258
suggested_samplefreq = 44100;
259
else if (input_samplefreq >= 32000)
260
suggested_samplefreq = 32000;
261
else if (input_samplefreq >= 24000)
262
suggested_samplefreq = 24000;
263
else if (input_samplefreq >= 22050)
264
suggested_samplefreq = 22050;
265
else if (input_samplefreq >= 16000)
266
suggested_samplefreq = 16000;
267
else if (input_samplefreq >= 12000)
268
suggested_samplefreq = 12000;
269
else if (input_samplefreq >= 11025)
270
suggested_samplefreq = 11025;
271
else if (input_samplefreq >= 8000)
272
suggested_samplefreq = 8000;
274
if (lowpassfreq == -1)
275
return suggested_samplefreq;
277
if (lowpassfreq <= 15960)
278
suggested_samplefreq = 44100;
279
if (lowpassfreq <= 15250)
280
suggested_samplefreq = 32000;
281
if (lowpassfreq <= 11220)
282
suggested_samplefreq = 24000;
283
if (lowpassfreq <= 9970)
284
suggested_samplefreq = 22050;
285
if (lowpassfreq <= 7230)
286
suggested_samplefreq = 16000;
287
if (lowpassfreq <= 5420)
288
suggested_samplefreq = 12000;
289
if (lowpassfreq <= 4510)
290
suggested_samplefreq = 11025;
291
if (lowpassfreq <= 3970)
292
suggested_samplefreq = 8000;
294
if (input_samplefreq < suggested_samplefreq) {
295
/* choose a valid MPEG sample frequency above the input sample frequency
296
to avoid SFB21/12 bitrate bloat
299
if (input_samplefreq > 44100) {
302
if (input_samplefreq > 32000) {
305
if (input_samplefreq > 24000) {
308
if (input_samplefreq > 22050) {
311
if (input_samplefreq > 16000) {
314
if (input_samplefreq > 12000) {
317
if (input_samplefreq > 11025) {
320
if (input_samplefreq > 8000) {
325
return suggested_samplefreq;
332
/* set internal feature flags. USER should not access these since
333
* some combinations will produce strange results */
335
lame_init_qval(lame_global_flags * gfp)
337
lame_internal_flags *const gfc = gfp->internal_flags;
339
switch (gfp->quality) {
341
case 9: /* no psymodel, no noise shaping */
343
gfc->noise_shaping = 0;
344
gfc->noise_shaping_amp = 0;
345
gfc->noise_shaping_stop = 0;
346
gfc->use_best_huffman = 0;
347
gfc->full_outer_loop = 0;
352
/*lint --fallthrough */
353
case 7: /* use psymodel (for short block and m/s switching), but no noise shapping */
355
gfc->noise_shaping = 0;
356
gfc->noise_shaping_amp = 0;
357
gfc->noise_shaping_stop = 0;
358
gfc->use_best_huffman = 0;
359
gfc->full_outer_loop = 0;
364
if (gfc->noise_shaping == 0)
365
gfc->noise_shaping = 1;
366
gfc->noise_shaping_amp = 0;
367
gfc->noise_shaping_stop = 0;
368
if (gfc->subblock_gain == -1)
369
gfc->subblock_gain = 1;
370
gfc->use_best_huffman = 0;
371
gfc->full_outer_loop = 0;
376
if (gfc->noise_shaping == 0)
377
gfc->noise_shaping = 1;
378
gfc->noise_shaping_amp = 0;
379
gfc->noise_shaping_stop = 0;
380
if (gfc->subblock_gain == -1)
381
gfc->subblock_gain = 1;
382
gfc->use_best_huffman = 0;
383
gfc->full_outer_loop = 0;
388
if (gfc->noise_shaping == 0)
389
gfc->noise_shaping = 1;
390
gfc->noise_shaping_amp = 0;
391
gfc->noise_shaping_stop = 0;
392
if (gfc->subblock_gain == -1)
393
gfc->subblock_gain = 1;
394
gfc->use_best_huffman = 1;
395
gfc->full_outer_loop = 0;
400
if (gfc->noise_shaping == 0)
401
gfc->noise_shaping = 1;
402
gfc->noise_shaping_amp = 1;
403
gfc->noise_shaping_stop = 1;
404
if (gfc->subblock_gain == -1)
405
gfc->subblock_gain = 1;
406
gfc->use_best_huffman = 1;
407
gfc->full_outer_loop = 0;
412
if (gfc->noise_shaping == 0)
413
gfc->noise_shaping = 1;
414
if (gfc->substep_shaping == 0)
415
gfc->substep_shaping = 2;
416
gfc->noise_shaping_amp = 1;
417
gfc->noise_shaping_stop = 1;
418
if (gfc->subblock_gain == -1)
419
gfc->subblock_gain = 1;
420
gfc->use_best_huffman = 1; /* inner loop */
421
gfc->full_outer_loop = 0;
426
if (gfc->noise_shaping == 0)
427
gfc->noise_shaping = 1;
428
if (gfc->substep_shaping == 0)
429
gfc->substep_shaping = 2;
430
gfc->noise_shaping_amp = 2;
431
gfc->noise_shaping_stop = 1;
432
if (gfc->subblock_gain == -1)
433
gfc->subblock_gain = 1;
434
gfc->use_best_huffman = 1;
435
gfc->full_outer_loop = 0;
440
if (gfc->noise_shaping == 0)
441
gfc->noise_shaping = 1;
442
if (gfc->substep_shaping == 0)
443
gfc->substep_shaping = 2;
444
gfc->noise_shaping_amp = 2;
445
gfc->noise_shaping_stop = 1;
446
if (gfc->subblock_gain == -1)
447
gfc->subblock_gain = 1;
448
gfc->use_best_huffman = 1; /*type 2 disabled because of it slowness,
449
in favor of full outer loop search */
450
gfc->full_outer_loop = 0; /* full outer loop search disabled because
451
of audible distortions it may generate
461
linear_int(double a, double b, double m)
463
return a + m * (b - a);
468
/********************************************************************
469
* initialize internal params based on data in gf
470
* (globalflags struct filled in by calling program)
474
* We first have some complex code to determine bitrate,
475
* output samplerate and mode. It is complicated by the fact
476
* that we allow the user to set some or all of these parameters,
477
* and need to determine best possible values for the rest of them:
479
* 1. set some CPU related flags
480
* 2. check if we are mono->mono, stereo->mono or stereo->stereo
481
* 3. compute bitrate and output samplerate:
482
* user may have set compression ratio
483
* user may have set a bitrate
484
* user may have set a output samplerate
485
* 4. set some options which depend on output samplerate
486
* 5. compute the actual compression ratio
487
* 6. set mode based on compression ratio
489
* The remaining code is much simpler - it just sets options
490
* based on the mode & compression ratio:
492
* set allow_diff_short based on mode
493
* select lowpass filter based on compression ratio & mode
494
* set the bitrate index, and min/max bitrates for VBR modes
495
* disable VBR tag if it is not appropriate
496
* initialize the bitstream
497
* initialize scalefac_band data
498
* set sideinfo_len (based on channels, CRC, out_samplerate)
499
* write an id3v2 tag into the bitstream
500
* write VBR tag into the bitstream
502
* estimate the number of frames (based on a lot of data)
504
* now we set more flags:
512
* Finally, we set the algorithm flags based on the gfp->quality value
513
* lame_init_qval(gfp);
515
********************************************************************/
517
lame_init_params(lame_global_flags * gfp)
522
lame_internal_flags *const gfc = gfp->internal_flags;
526
/* report functions */
527
gfc->report.msgf = gfp->report.msgf;
528
gfc->report.debugf = gfp->report.debugf;
529
gfc->report.errorf = gfp->report.errorf;
531
if (gfp->asm_optimizations.amd3dnow)
532
gfc->CPU_features.AMD_3DNow = has_3DNow();
534
gfc->CPU_features.AMD_3DNow = 0;
536
if (gfp->asm_optimizations.mmx)
537
gfc->CPU_features.MMX = has_MMX();
539
gfc->CPU_features.MMX = 0;
541
if (gfp->asm_optimizations.sse) {
542
gfc->CPU_features.SSE = has_SSE();
543
gfc->CPU_features.SSE2 = has_SSE2();
546
gfc->CPU_features.SSE = 0;
547
gfc->CPU_features.SSE2 = 0;
551
if (NULL == gfc->ATH)
552
gfc->ATH = calloc(1, sizeof(ATH_t));
554
if (NULL == gfc->ATH)
555
return -2; /* maybe error codes should be enumerated in lame.h ?? */
557
if (NULL == gfc->PSY)
558
gfc->PSY = calloc(1, sizeof(PSY_t));
559
if (NULL == gfc->PSY) {
561
gfp->internal_flags = NULL;
565
if (NULL == gfc->rgdata)
566
gfc->rgdata = calloc(1, sizeof(replaygain_t));
567
if (NULL == gfc->rgdata) {
569
gfp->internal_flags = NULL;
573
gfc->channels_in = gfp->num_channels;
574
if (gfc->channels_in == 1)
576
gfc->channels_out = (gfp->mode == MONO) ? 1 : 2;
577
gfc->mode_ext = MPG_MD_MS_LR;
578
if (gfp->mode == MONO)
579
gfp->force_ms = 0; /* don't allow forced mid/side stereo for mono output */
581
if (gfp->VBR == vbr_off && gfp->VBR_mean_bitrate_kbps != 128 && gfp->brate == 0)
582
gfp->brate = gfp->VBR_mean_bitrate_kbps;
588
/* these modes can handle free format condition */
591
gfp->free_format = 0; /* mode can't be mixed with free format */
595
if (gfp->VBR == vbr_off && gfp->brate == 0) {
596
/* no bitrate or compression ratio specified, use 11.025 */
597
if (EQ(gfp->compression_ratio, 0))
598
gfp->compression_ratio = 11.025; /* rate to compress a CD down to exactly 128000 bps */
601
/* find bitrate if user specify a compression ratio */
602
if (gfp->VBR == vbr_off && gfp->compression_ratio > 0) {
604
if (gfp->out_samplerate == 0)
605
gfp->out_samplerate = map2MP3Frequency((int) (0.97 * gfp->in_samplerate)); /* round up with a margin of 3% */
607
/* choose a bitrate for the output samplerate which achieves
608
* specified compression ratio
610
gfp->brate = gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 * gfp->compression_ratio);
612
/* we need the version for the bitrate table look up */
613
gfc->samplerate_index = SmpFrqIndex(gfp->out_samplerate, &gfp->version);
615
if (!gfp->free_format) /* for non Free Format find the nearest allowed bitrate */
616
gfp->brate = FindNearestBitrate(gfp->brate, gfp->version, gfp->out_samplerate);
618
if (gfp->out_samplerate) {
619
if (gfp->out_samplerate < 16000) {
620
gfp->VBR_mean_bitrate_kbps = Max(gfp->VBR_mean_bitrate_kbps, 8);
621
gfp->VBR_mean_bitrate_kbps = Min(gfp->VBR_mean_bitrate_kbps, 64);
623
else if (gfp->out_samplerate < 32000) {
624
gfp->VBR_mean_bitrate_kbps = Max(gfp->VBR_mean_bitrate_kbps, 8);
625
gfp->VBR_mean_bitrate_kbps = Min(gfp->VBR_mean_bitrate_kbps, 160);
628
gfp->VBR_mean_bitrate_kbps = Max(gfp->VBR_mean_bitrate_kbps, 32);
629
gfp->VBR_mean_bitrate_kbps = Min(gfp->VBR_mean_bitrate_kbps, 320);
633
/****************************************************************/
634
/* if a filter has not been enabled, see if we should add one: */
635
/****************************************************************/
636
if (gfp->lowpassfreq == 0) {
637
double lowpass = 16000;
642
optimum_bandwidth(&lowpass, &highpass, gfp->brate);
646
optimum_bandwidth(&lowpass, &highpass, gfp->VBR_mean_bitrate_kbps);
651
19500, 19000, 18600, 18000, 17500, 16000, 15600, 14900, 12500, 10000, 3950
653
if (0 <= gfp->VBR_q && gfp->VBR_q <= 9) {
654
double a = x[gfp->VBR_q], b = x[gfp->VBR_q + 1], m = gfp->VBR_q_frac;
655
lowpass = linear_int(a, b, m);
664
19500, 19000, 18500, 18000, 17500, 16500, 15500, 14500, 12500, 9500, 3950
666
if (0 <= gfp->VBR_q && gfp->VBR_q <= 9) {
667
double a = x[gfp->VBR_q], b = x[gfp->VBR_q + 1], m = gfp->VBR_q_frac;
668
lowpass = linear_int(a, b, m);
676
if (gfp->mode == MONO && (gfp->VBR == vbr_off || gfp->VBR == vbr_abr))
679
gfp->lowpassfreq = lowpass;
682
if (gfp->out_samplerate == 0) {
683
if (2 * gfp->lowpassfreq > gfp->in_samplerate) {
684
gfp->lowpassfreq = gfp->in_samplerate / 2;
686
gfp->out_samplerate = optimum_samplefreq((int) gfp->lowpassfreq, gfp->in_samplerate);
689
gfp->lowpassfreq = Min(20500, gfp->lowpassfreq);
690
gfp->lowpassfreq = Min(gfp->out_samplerate / 2, gfp->lowpassfreq);
692
if (gfp->VBR == vbr_off) {
693
gfp->compression_ratio = gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 * gfp->brate);
695
if (gfp->VBR == vbr_abr) {
696
gfp->compression_ratio =
697
gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 * gfp->VBR_mean_bitrate_kbps);
700
/* do not compute ReplayGain values and do not find the peak sample
701
if we can't store them */
702
if (!gfp->bWriteVbrTag) {
703
gfp->findReplayGain = 0;
704
gfp->decode_on_the_fly = 0;
705
gfc->findPeakSample = 0;
707
gfc->findReplayGain = gfp->findReplayGain;
708
gfc->decode_on_the_fly = gfp->decode_on_the_fly;
710
if (gfc->decode_on_the_fly)
711
gfc->findPeakSample = 1;
713
if (gfc->findReplayGain) {
714
if (InitGainAnalysis(gfc->rgdata, gfp->out_samplerate) == INIT_GAIN_ANALYSIS_ERROR) {
716
gfp->internal_flags = NULL;
721
#ifdef DECODE_ON_THE_FLY
722
if (gfc->decode_on_the_fly && !gfp->decode_only) {
724
hip_decode_exit(gfc->hip);
726
gfc->hip = hip_decode_init();
730
gfc->mode_gr = gfp->out_samplerate <= 24000 ? 1 : 2; /* Number of granules per frame */
731
gfp->framesize = 576 * gfc->mode_gr;
732
gfp->encoder_delay = ENCDELAY;
734
gfc->resample_ratio = (double) gfp->in_samplerate / gfp->out_samplerate;
737
* sample freq bitrate compression ratio
738
* [kHz] [kbps/channel] for 16 bit input
750
* For VBR, take a guess at the compression_ratio.
753
* VBR_q compression like
754
* - 4.4 320 kbps/44 kHz
755
* 0...1 5.5 256 kbps/44 kHz
756
* 2 7.3 192 kbps/44 kHz
757
* 4 8.8 160 kbps/44 kHz
758
* 6 11 128 kbps/44 kHz
761
* for lower bitrates, downsample with --resample
769
/*numbers are a bit strange, but they determine the lowpass value */
770
FLOAT const cmp[] = { 5.7, 6.5, 7.3, 8.2, 10, 11.9, 13, 14, 15, 16.5 };
771
gfp->compression_ratio = cmp[gfp->VBR_q];
775
gfp->compression_ratio =
776
gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 * gfp->VBR_mean_bitrate_kbps);
779
gfp->compression_ratio = gfp->out_samplerate * 16 * gfc->channels_out / (1.e3 * gfp->brate);
784
/* mode = -1 (not set by user) or
785
* mode = MONO (because of only 1 input channel).
786
* If mode has not been set, then select J-STEREO
788
if (gfp->mode == NOT_SET) {
789
gfp->mode = JOINT_STEREO;
793
/* apply user driven high pass filter */
794
if (gfp->highpassfreq > 0) {
795
gfc->highpass1 = 2. * gfp->highpassfreq;
797
if (gfp->highpasswidth >= 0)
798
gfc->highpass2 = 2. * (gfp->highpassfreq + gfp->highpasswidth);
799
else /* 0% above on default */
800
gfc->highpass2 = (1 + 0.00) * 2. * gfp->highpassfreq;
802
gfc->highpass1 /= gfp->out_samplerate;
803
gfc->highpass2 /= gfp->out_samplerate;
809
/* apply user driven low pass filter */
810
if (gfp->lowpassfreq > 0) {
811
gfc->lowpass2 = 2. * gfp->lowpassfreq;
812
if (gfp->lowpasswidth >= 0) {
813
gfc->lowpass1 = 2. * (gfp->lowpassfreq - gfp->lowpasswidth);
814
if (gfc->lowpass1 < 0) /* has to be >= 0 */
817
else { /* 0% below on default */
818
gfc->lowpass1 = (1 - 0.00) * 2. * gfp->lowpassfreq;
820
gfc->lowpass1 /= gfp->out_samplerate;
821
gfc->lowpass2 /= gfp->out_samplerate;
831
/**********************************************************************/
832
/* compute info needed for polyphase filter (filter type==0, default) */
833
/**********************************************************************/
834
lame_init_params_ppflt(gfp);
837
/*******************************************************
838
* samplerate and bitrate index
839
*******************************************************/
840
gfc->samplerate_index = SmpFrqIndex(gfp->out_samplerate, &gfp->version);
841
if (gfc->samplerate_index < 0) {
843
gfp->internal_flags = NULL;
847
if (gfp->VBR == vbr_off) {
848
if (gfp->free_format) {
849
gfc->bitrate_index = 0;
852
gfp->brate = FindNearestBitrate(gfp->brate, gfp->version, gfp->out_samplerate);
853
gfc->bitrate_index = BitrateIndex(gfp->brate, gfp->version, gfp->out_samplerate);
854
if (gfc->bitrate_index <= 0) {
856
gfp->internal_flags = NULL;
862
gfc->bitrate_index = 1;
865
/* for CBR, we will write an "info" tag. */
866
/* if ((gfp->VBR == vbr_off)) */
867
/* gfp->bWriteVbrTag = 0; */
870
gfp->bWriteVbrTag = 0;
872
/* some file options not allowed if output is: not specified or stdout */
873
if (gfc->pinfo != NULL)
874
gfp->bWriteVbrTag = 0; /* disable Xing VBR tag */
876
init_bit_stream_w(gfc);
878
j = gfc->samplerate_index + (3 * gfp->version) + 6 * (gfp->out_samplerate < 16000);
879
for (i = 0; i < SBMAX_l + 1; i++)
880
gfc->scalefac_band.l[i] = sfBandIndex[j].l[i];
882
for (i = 0; i < PSFB21 + 1; i++) {
883
int const size = (gfc->scalefac_band.l[22] - gfc->scalefac_band.l[21]) / PSFB21;
884
int const start = gfc->scalefac_band.l[21] + i * size;
885
gfc->scalefac_band.psfb21[i] = start;
887
gfc->scalefac_band.psfb21[PSFB21] = 576;
889
for (i = 0; i < SBMAX_s + 1; i++)
890
gfc->scalefac_band.s[i] = sfBandIndex[j].s[i];
892
for (i = 0; i < PSFB12 + 1; i++) {
893
int const size = (gfc->scalefac_band.s[13] - gfc->scalefac_band.s[12]) / PSFB12;
894
int const start = gfc->scalefac_band.s[12] + i * size;
895
gfc->scalefac_band.psfb12[i] = start;
897
gfc->scalefac_band.psfb12[PSFB12] = 192;
899
/* determine the mean bitrate for main data */
900
if (gfp->version == 1) /* MPEG 1 */
901
gfc->sideinfo_len = (gfc->channels_out == 1) ? 4 + 17 : 4 + 32;
903
gfc->sideinfo_len = (gfc->channels_out == 1) ? 4 + 9 : 4 + 17;
905
if (gfp->error_protection)
906
gfc->sideinfo_len += 2;
908
(void) lame_init_bitstream(gfp);
910
gfc->Class_ID = LAME_ID;
912
/*if (gfp->exp_nspsytune & 1) */ {
915
for (k = 0; k < 19; k++)
916
gfc->nsPsy.pefirbuf[k] = 700 * gfc->mode_gr * gfc->channels_out;
918
if (gfp->ATHtype == -1)
922
assert(gfp->VBR_q <= 9);
923
assert(gfp->VBR_q >= 0);
929
/*lint --fallthrough */
931
if (gfp->useTemporal < 0) {
932
gfp->useTemporal = 0; /* off by default for this VBR mode */
935
(void) apply_preset(gfp, 500 - (gfp->VBR_q * 10), 0);
936
/* The newer VBR code supports only a limited
937
subset of quality levels:
938
9-5=5 are the same, uses x^3/4 quantization
939
4-0=0 are the same 5 plus best huffman divide code
941
if (gfp->quality < 0)
942
gfp->quality = LAME_DEFAULT_QUALITY;
943
if (gfp->quality < 5)
945
if (gfp->quality > 5)
948
gfc->PSY->mask_adjust = gfp->maskingadjust;
949
gfc->PSY->mask_adjust_short = gfp->maskingadjust_short;
951
/* sfb21 extra only with MPEG-1 at higher sampling rates
953
if (gfp->experimentalY)
954
gfc->sfb21_extra = 0;
956
gfc->sfb21_extra = (gfp->out_samplerate > 44000);
958
gfc->iteration_loop = VBR_new_iteration_loop;
964
(void) apply_preset(gfp, 500 - (gfp->VBR_q * 10), 0);
966
gfc->PSY->mask_adjust = gfp->maskingadjust;
967
gfc->PSY->mask_adjust_short = gfp->maskingadjust_short;
969
/* sfb21 extra only with MPEG-1 at higher sampling rates
971
if (gfp->experimentalY)
972
gfc->sfb21_extra = 0;
974
gfc->sfb21_extra = (gfp->out_samplerate > 44000);
976
/* VBR needs at least the output of GPSYCHO,
977
* so we have to garantee that by setting a minimum
978
* quality level, actually level 6 does it.
981
if (gfp->quality > 6)
985
if (gfp->quality < 0)
986
gfp->quality = LAME_DEFAULT_QUALITY;
988
gfc->iteration_loop = VBR_old_iteration_loop;
992
default: /* cbr/abr */ {
995
/* no sfb21 extra with CBR code
997
gfc->sfb21_extra = 0;
999
if (gfp->quality < 0)
1000
gfp->quality = LAME_DEFAULT_QUALITY;
1003
vbrmode = lame_get_VBR(gfp);
1004
if (vbrmode == vbr_off)
1005
(void) lame_set_VBR_mean_bitrate_kbps(gfp, gfp->brate);
1006
/* second, set parameters depending on bitrate */
1007
(void) apply_preset(gfp, gfp->VBR_mean_bitrate_kbps, 0);
1008
(void) lame_set_VBR(gfp, vbrmode);
1010
gfc->PSY->mask_adjust = gfp->maskingadjust;
1011
gfc->PSY->mask_adjust_short = gfp->maskingadjust_short;
1013
if (vbrmode == vbr_off) {
1014
gfc->iteration_loop = CBR_iteration_loop;
1017
gfc->iteration_loop = ABR_iteration_loop;
1023
/*initialize default values common for all modes */
1026
if (lame_get_VBR(gfp) != vbr_off) { /* choose a min/max bitrate for VBR */
1027
/* if the user didn't specify VBR_max_bitrate: */
1028
gfc->VBR_min_bitrate = 1; /* default: allow 8 kbps (MPEG-2) or 32 kbps (MPEG-1) */
1029
gfc->VBR_max_bitrate = 14; /* default: allow 160 kbps (MPEG-2) or 320 kbps (MPEG-1) */
1030
if (gfp->out_samplerate < 16000)
1031
gfc->VBR_max_bitrate = 8; /* default: allow 64 kbps (MPEG-2.5) */
1032
if (gfp->VBR_min_bitrate_kbps) {
1033
gfp->VBR_min_bitrate_kbps =
1034
FindNearestBitrate(gfp->VBR_min_bitrate_kbps, gfp->version, gfp->out_samplerate);
1035
gfc->VBR_min_bitrate =
1036
BitrateIndex(gfp->VBR_min_bitrate_kbps, gfp->version, gfp->out_samplerate);
1037
if (gfc->VBR_min_bitrate < 0)
1040
if (gfp->VBR_max_bitrate_kbps) {
1041
gfp->VBR_max_bitrate_kbps =
1042
FindNearestBitrate(gfp->VBR_max_bitrate_kbps, gfp->version, gfp->out_samplerate);
1043
gfc->VBR_max_bitrate =
1044
BitrateIndex(gfp->VBR_max_bitrate_kbps, gfp->version, gfp->out_samplerate);
1045
if (gfc->VBR_max_bitrate < 0)
1048
gfp->VBR_min_bitrate_kbps = bitrate_table[gfp->version][gfc->VBR_min_bitrate];
1049
gfp->VBR_max_bitrate_kbps = bitrate_table[gfp->version][gfc->VBR_max_bitrate];
1050
gfp->VBR_mean_bitrate_kbps =
1051
Min(bitrate_table[gfp->version][gfc->VBR_max_bitrate], gfp->VBR_mean_bitrate_kbps);
1052
gfp->VBR_mean_bitrate_kbps =
1053
Max(bitrate_table[gfp->version][gfc->VBR_min_bitrate], gfp->VBR_mean_bitrate_kbps);
1057
/* just another daily changing developer switch */
1059
gfc->PSY->mask_adjust += gfp->tune_value_a;
1060
gfc->PSY->mask_adjust_short += gfp->tune_value_a;
1063
/* initialize internal qval settings */
1064
lame_init_qval(gfp);
1067
/* automatic ATH adjustment on
1069
if (gfp->athaa_type < 0)
1070
gfc->ATH->use_adjust = 3;
1072
gfc->ATH->use_adjust = gfp->athaa_type;
1075
/* initialize internal adaptive ATH settings -jd */
1076
gfc->ATH->aa_sensitivity_p = pow(10.0, gfp->athaa_sensitivity / -10.0);
1079
if (gfp->short_blocks == short_block_not_set) {
1080
gfp->short_blocks = short_block_allowed;
1083
/*Note Jan/2003: Many hardware decoders cannot handle short blocks in regular
1084
stereo mode unless they are coupled (same type in both channels)
1085
it is a rare event (1 frame per min. or so) that LAME would use
1086
uncoupled short blocks, so lets turn them off until we decide
1087
how to handle this. No other encoders allow uncoupled short blocks,
1088
even though it is in the standard. */
1089
/* rh 20040217: coupling makes no sense for mono and dual-mono streams
1091
if (gfp->short_blocks == short_block_allowed
1092
&& (gfp->mode == JOINT_STEREO || gfp->mode == STEREO)) {
1093
gfp->short_blocks = short_block_coupled;
1097
if (lame_get_quant_comp(gfp) < 0)
1098
(void) lame_set_quant_comp(gfp, 1);
1099
if (lame_get_quant_comp_short(gfp) < 0)
1100
(void) lame_set_quant_comp_short(gfp, 0);
1102
if (lame_get_msfix(gfp) < 0)
1103
lame_set_msfix(gfp, 0);
1105
/* select psychoacoustic model */
1106
(void) lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | 1);
1108
if (lame_get_short_threshold_lrm(gfp) < 0)
1109
(void) lame_set_short_threshold_lrm(gfp, NSATTACKTHRE);
1110
if (lame_get_short_threshold_s(gfp) < 0)
1111
(void) lame_set_short_threshold_s(gfp, NSATTACKTHRE_S);
1116
if (gfp->ATHtype < 0)
1119
if (gfp->ATHcurve < 0)
1122
if (gfp->athaa_loudapprox < 0)
1123
gfp->athaa_loudapprox = 2;
1125
if (gfp->interChRatio < 0)
1126
gfp->interChRatio = 0;
1128
if (gfp->useTemporal < 0)
1129
gfp->useTemporal = 1; /* on by default */
1133
/* padding method as described in
1134
* "MPEG-Layer3 / Bitstream Syntax and Decoding"
1135
* by Martin Sieler, Ralph Sperschneider
1137
* note: there is no padding for the very first frame
1139
* Robert Hegemann 2000-06-22
1141
gfc->slot_lag = gfc->frac_SpF = 0;
1142
if (gfp->VBR == vbr_off)
1143
gfc->slot_lag = gfc->frac_SpF
1144
= ((gfp->version + 1) * 72000L * gfp->brate) % gfp->out_samplerate;
1146
iteration_init(gfp);
1147
(void) psymodel_init(gfp);
1155
* Prints some selected information about the coding parameters via
1156
* the macro command MSGF(), which is currently mapped to lame_errorf
1157
* (reports via a error function?), which is a printf-like function
1162
lame_print_config(const lame_global_flags * gfp)
1164
lame_internal_flags const *const gfc = gfp->internal_flags;
1165
double const out_samplerate = gfp->out_samplerate;
1166
double const in_samplerate = gfp->out_samplerate * gfc->resample_ratio;
1168
MSGF(gfc, "LAME %s %s (%s)\n", get_lame_version(), get_lame_os_bitness(), get_lame_url());
1170
#if (LAME_ALPHA_VERSION)
1171
MSGF(gfc, "warning: alpha versions should be used for testing only\n");
1173
if (gfc->CPU_features.MMX
1174
|| gfc->CPU_features.AMD_3DNow || gfc->CPU_features.SSE || gfc->CPU_features.SSE2) {
1175
int fft_asm_used = 0;
1177
if (gfc->CPU_features.AMD_3DNow) {
1180
else if (gfc->CPU_features.SSE) {
1188
MSGF(gfc, "CPU features: ");
1190
if (gfc->CPU_features.MMX) {
1191
#ifdef MMX_choose_table
1192
MSGF(gfc, "MMX (ASM used)");
1197
if (gfc->CPU_features.AMD_3DNow) {
1198
if (fft_asm_used == 1) {
1199
MSGF(gfc, ", 3DNow! (ASM used)");
1202
MSGF(gfc, ", 3DNow!");
1205
if (gfc->CPU_features.SSE) {
1206
#if defined(HAVE_XMMINTRIN_H)
1207
MSGF(gfc, ", SSE (ASM used)");
1209
if (fft_asm_used == 2) {
1210
MSGF(gfc, ", SSE (ASM used)");
1217
if (gfc->CPU_features.SSE2) {
1218
MSGF(gfc, ", SSE2");
1223
if (gfp->num_channels == 2 && gfc->channels_out == 1 /* mono */ ) {
1224
MSGF(gfc, "Autoconverting from stereo to mono. Setting encoding to mono mode.\n");
1227
if (NEQ(gfc->resample_ratio, 1.)) {
1228
MSGF(gfc, "Resampling: input %g kHz output %g kHz\n",
1229
1.e-3 * in_samplerate, 1.e-3 * out_samplerate);
1232
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, 0.5 * gfc->highpass2 * out_samplerate);
1236
if (0. < gfc->lowpass1 || 0. < gfc->lowpass2) {
1238
"Using polyphase lowpass filter, transition band: %5.0f Hz - %5.0f Hz\n",
1239
0.5 * gfc->lowpass1 * out_samplerate, 0.5 * gfc->lowpass2 * out_samplerate);
1242
MSGF(gfc, "polyphase lowpass filter disabled\n");
1245
if (gfp->free_format) {
1246
MSGF(gfc, "Warning: many decoders cannot handle free format bitstreams\n");
1247
if (gfp->brate > 320) {
1249
"Warning: many decoders cannot handle free format bitrates >320 kbps (see documentation)\n");
1256
* some pretty printing is very welcome at this point!
1257
* so, if someone is willing to do so, please do it!
1258
* add more, if you see more...
1261
lame_print_internals(const lame_global_flags * gfp)
1263
lame_internal_flags const *const gfc = gfp->internal_flags;
1264
const char *pc = "";
1266
/* compiler/processor optimizations, operational, etc.
1268
MSGF(gfc, "\nmisc:\n\n");
1270
MSGF(gfc, "\tscaling: %g\n", gfp->scale);
1271
MSGF(gfc, "\tch0 (left) scaling: %g\n", gfp->scale_left);
1272
MSGF(gfc, "\tch1 (right) scaling: %g\n", gfp->scale_right);
1273
switch (gfc->use_best_huffman) {
1278
pc = "best (outside loop)";
1281
pc = "best (inside loop, slow)";
1284
MSGF(gfc, "\thuffman search: %s\n", pc);
1285
MSGF(gfc, "\texperimental Y=%d\n", gfp->experimentalY);
1286
MSGF(gfc, "\t...\n");
1288
/* everything controlling the stream format
1290
MSGF(gfc, "\nstream format:\n\n");
1291
switch (gfp->version) {
1305
MSGF(gfc, "\tMPEG-%s Layer 3\n", pc);
1306
switch (gfp->mode) {
1308
pc = "joint stereo";
1314
pc = "dual channel";
1320
pc = "not set (error)";
1323
pc = "unknown (error)";
1326
MSGF(gfc, "\t%d channel - %s\n", gfc->channels_out, pc);
1336
MSGF(gfc, "\tpadding: %s\n", pc);
1338
if (vbr_default == gfp->VBR)
1340
else if (gfp->free_format)
1341
pc = "(free format)";
1346
MSGF(gfc, "\tconstant bitrate - CBR %s\n", pc);
1349
MSGF(gfc, "\tvariable bitrate - ABR %s\n", pc);
1352
MSGF(gfc, "\tvariable bitrate - VBR rh %s\n", pc);
1355
MSGF(gfc, "\tvariable bitrate - VBR mt %s\n", pc);
1358
MSGF(gfc, "\tvariable bitrate - VBR mtrh %s\n", pc);
1361
MSGF(gfc, "\t ?? oops, some new one ?? \n");
1364
if (gfp->bWriteVbrTag)
1365
MSGF(gfc, "\tusing LAME Tag\n");
1366
MSGF(gfc, "\t...\n");
1368
/* everything controlling psychoacoustic settings, like ATH, etc.
1370
MSGF(gfc, "\npsychoacoustic:\n\n");
1372
switch (gfp->short_blocks) {
1374
case short_block_not_set:
1377
case short_block_allowed:
1380
case short_block_coupled:
1381
pc = "channel coupled";
1383
case short_block_dispensed:
1386
case short_block_forced:
1390
MSGF(gfc, "\tusing short blocks: %s\n", pc);
1391
MSGF(gfc, "\tsubblock gain: %d\n", gfc->subblock_gain);
1392
MSGF(gfc, "\tadjust masking: %g dB\n", gfc->PSY->mask_adjust);
1393
MSGF(gfc, "\tadjust masking short: %g dB\n", gfc->PSY->mask_adjust_short);
1394
MSGF(gfc, "\tquantization comparison: %d\n", gfp->quant_comp);
1395
MSGF(gfc, "\t ^ comparison short blocks: %d\n", gfp->quant_comp_short);
1396
MSGF(gfc, "\tnoise shaping: %d\n", gfc->noise_shaping);
1397
MSGF(gfc, "\t ^ amplification: %d\n", gfc->noise_shaping_amp);
1398
MSGF(gfc, "\t ^ stopping: %d\n", gfc->noise_shaping_stop);
1402
pc = "the only masking for short blocks";
1404
pc = "the only masking";
1407
MSGF(gfc, "\tATH: %s\n", pc);
1408
MSGF(gfc, "\t ^ type: %d\n", gfp->ATHtype);
1409
MSGF(gfc, "\t ^ shape: %g%s\n", gfp->ATHcurve, " (only for type 4)");
1410
MSGF(gfc, "\t ^ level adjustement: %g\n", gfp->ATHlower);
1411
MSGF(gfc, "\t ^ adjust type: %d\n", gfc->ATH->use_adjust);
1412
MSGF(gfc, "\t ^ adjust sensitivity power: %f\n", gfc->ATH->aa_sensitivity_p);
1413
MSGF(gfc, "\t ^ adapt threshold type: %d\n", gfp->athaa_loudapprox);
1415
MSGF(gfc, "\texperimental psy tunings by Naoki Shibata\n");
1416
MSGF(gfc, "\t adjust masking bass=%g dB, alto=%g dB, treble=%g dB, sfb21=%g dB\n",
1417
10 * log10(gfc->nsPsy.longfact[0]),
1418
10 * log10(gfc->nsPsy.longfact[7]),
1419
10 * log10(gfc->nsPsy.longfact[14]), 10 * log10(gfc->nsPsy.longfact[21]));
1421
pc = gfp->useTemporal ? "yes" : "no";
1422
MSGF(gfc, "\tusing temporal masking effect: %s\n", pc);
1423
MSGF(gfc, "\tinterchannel masking ratio: %g\n", gfp->interChRatio);
1424
MSGF(gfc, "\t...\n");
1434
/* routine to feed exactly one frame (gfp->framesize) worth of data to the
1435
encoding engine. All buffering, resampling, etc, handled by calling
1439
lame_encode_frame(lame_global_flags * gfp,
1440
sample_t inbuf_l[], sample_t inbuf_r[], unsigned char *mp3buf, int mp3buf_size)
1443
ret = lame_encode_mp3_frame(gfp, inbuf_l, inbuf_r, mp3buf, mp3buf_size);
1449
update_inbuffer_size(lame_internal_flags * gfc, const int nsamples)
1451
if (gfc->in_buffer_0 == 0 || gfc->in_buffer_nsamples < nsamples) {
1452
if (gfc->in_buffer_0) {
1453
free(gfc->in_buffer_0);
1455
if (gfc->in_buffer_1) {
1456
free(gfc->in_buffer_1);
1458
gfc->in_buffer_0 = calloc(sizeof(sample_t), nsamples);
1459
gfc->in_buffer_1 = calloc(sizeof(sample_t), nsamples);
1460
gfc->in_buffer_nsamples = nsamples;
1462
if (gfc->in_buffer_0 == NULL || gfc->in_buffer_1 == NULL) {
1463
if (gfc->in_buffer_0) {
1464
free(gfc->in_buffer_0);
1466
if (gfc->in_buffer_1) {
1467
free(gfc->in_buffer_1);
1469
gfc->in_buffer_0 = 0;
1470
gfc->in_buffer_1 = 0;
1471
gfc->in_buffer_nsamples = 0;
1472
ERRORF(gfc, "Error: can't allocate in_buffer buffer\n");
1480
calcNeeded(lame_global_flags* gfp)
1483
/* some sanity checks */
1484
#if ENCDELAY < MDCTDELAY
1485
# error ENCDELAY is less than MDCTDELAY, see encoder.h
1487
#if FFTOFFSET > BLKSIZE
1488
# error FFTOFFSET is greater than BLKSIZE, see encoder.h
1491
mf_needed = BLKSIZE + gfp->framesize - FFTOFFSET; /* amount needed for FFT */
1492
/*mf_needed = Max(mf_needed, 286 + 576 * (1 + gfc->mode_gr)); */
1493
mf_needed = Max(mf_needed, 512 + gfp->framesize - 32);
1495
assert(MFSIZE >= mf_needed);
1501
* THE MAIN LAME ENCODING INTERFACE
1504
* input pcm data, output (maybe) mp3 frames.
1505
* This routine handles all buffering, resampling and filtering for you.
1506
* The required mp3buffer_size can be computed from num_samples,
1507
* samplerate and encoding rate, but here is a worst case estimate:
1509
* mp3buffer_size in bytes = 1.25*num_samples + 7200
1511
* return code = number of bytes output in mp3buffer. can be 0
1513
* NOTE: this routine uses LAME's internal PCM data representation,
1514
* 'sample_t'. It should not be used by any application.
1515
* applications should use lame_encode_buffer(),
1516
* lame_encode_buffer_float()
1517
* lame_encode_buffer_int()
1518
* etc... depending on what type of data they are working with.
1521
lame_encode_buffer_sample_t(lame_global_flags * gfp,
1522
sample_t buffer_l[],
1523
sample_t buffer_r[],
1524
int nsamples, unsigned char *mp3buf, const int mp3buf_size)
1526
lame_internal_flags *const gfc = gfp->internal_flags;
1527
int mp3size = 0, ret, i, ch, mf_needed;
1530
sample_t *in_buffer[2];
1532
if (gfc->Class_ID != LAME_ID)
1538
/* copy out any tags that may have been written into bitstream */
1539
mp3out = copy_buffer(gfc, mp3buf, mp3buf_size, 0);
1541
return mp3out; /* not enough buffer space */
1546
in_buffer[0] = buffer_l;
1547
in_buffer[1] = buffer_r;
1550
/* Apply user defined re-scaling */
1552
/* user selected scaling of the samples */
1553
if (NEQ(gfp->scale, 0) && NEQ(gfp->scale, 1.0)) {
1554
for (i = 0; i < nsamples; ++i) {
1555
in_buffer[0][i] *= gfp->scale;
1556
if (gfc->channels_out == 2)
1557
in_buffer[1][i] *= gfp->scale;
1561
/* user selected scaling of the channel 0 (left) samples */
1562
if (NEQ(gfp->scale_left, 0) && NEQ(gfp->scale_left, 1.0)) {
1563
for (i = 0; i < nsamples; ++i) {
1564
in_buffer[0][i] *= gfp->scale_left;
1568
/* user selected scaling of the channel 1 (right) samples */
1569
if (NEQ(gfp->scale_right, 0) && NEQ(gfp->scale_right, 1.0)) {
1570
for (i = 0; i < nsamples; ++i) {
1571
in_buffer[1][i] *= gfp->scale_right;
1575
/* Downsample to Mono if 2 channels in and 1 channel out */
1576
if (gfp->num_channels == 2 && gfc->channels_out == 1) {
1577
for (i = 0; i < nsamples; ++i) {
1578
in_buffer[0][i] = 0.5 * ((FLOAT) in_buffer[0][i] + in_buffer[1][i]);
1579
in_buffer[1][i] = 0.0;
1583
mf_needed = calcNeeded(gfp);
1585
mfbuf[0] = gfc->mfbuf[0];
1586
mfbuf[1] = gfc->mfbuf[1];
1588
while (nsamples > 0) {
1589
sample_t const *in_buffer_ptr[2];
1590
int n_in = 0; /* number of input samples processed with fill_buffer */
1591
int n_out = 0; /* number of samples output with fill_buffer */
1592
/* n_in <> n_out if we are resampling */
1594
in_buffer_ptr[0] = in_buffer[0];
1595
in_buffer_ptr[1] = in_buffer[1];
1596
/* copy in new samples into mfbuf, with resampling */
1597
fill_buffer(gfp, mfbuf, &in_buffer_ptr[0], nsamples, &n_in, &n_out);
1599
/* compute ReplayGain of resampled input if requested */
1600
if (gfc->findReplayGain && !gfc->decode_on_the_fly)
1602
(gfc->rgdata, &mfbuf[0][gfc->mf_size], &mfbuf[1][gfc->mf_size], n_out,
1603
gfc->channels_out) == GAIN_ANALYSIS_ERROR)
1608
/* update in_buffer counters */
1610
in_buffer[0] += n_in;
1611
if (gfc->channels_out == 2)
1612
in_buffer[1] += n_in;
1614
/* update mfbuf[] counters */
1615
gfc->mf_size += n_out;
1616
assert(gfc->mf_size <= MFSIZE);
1618
/* lame_encode_flush may have set gfc->mf_sample_to_encode to 0
1619
* so we have to reinitialize it here when that happened.
1621
if (gfc->mf_samples_to_encode < 1) {
1622
gfc->mf_samples_to_encode = ENCDELAY + POSTDELAY;
1624
gfc->mf_samples_to_encode += n_out;
1627
if (gfc->mf_size >= mf_needed) {
1628
/* encode the frame. */
1629
/* mp3buf = pointer to current location in buffer */
1630
/* mp3buf_size = size of original mp3 output buffer */
1631
/* = 0 if we should not worry about the */
1632
/* buffer size because calling program is */
1633
/* to lazy to compute it */
1634
/* mp3size = size of data written to buffer so far */
1635
/* mp3buf_size-mp3size = amount of space avalable */
1637
int buf_size = mp3buf_size - mp3size;
1638
if (mp3buf_size == 0)
1641
ret = lame_encode_frame(gfp, mfbuf[0], mfbuf[1], mp3buf, buf_size);
1648
/* shift out old samples */
1649
gfc->mf_size -= gfp->framesize;
1650
gfc->mf_samples_to_encode -= gfp->framesize;
1651
for (ch = 0; ch < gfc->channels_out; ch++)
1652
for (i = 0; i < gfc->mf_size; i++)
1653
mfbuf[ch][i] = mfbuf[ch][i + gfp->framesize];
1656
assert(nsamples == 0);
1663
lame_encode_buffer(lame_global_flags * gfp,
1664
const short int buffer_l[],
1665
const short int buffer_r[],
1666
const int nsamples, unsigned char *mp3buf, const int mp3buf_size)
1668
lame_internal_flags *const gfc = gfp->internal_flags;
1670
sample_t *in_buffer[2];
1672
if (gfc->Class_ID != LAME_ID)
1678
if (update_inbuffer_size(gfc, nsamples) != 0) {
1682
in_buffer[0] = gfc->in_buffer_0;
1683
in_buffer[1] = gfc->in_buffer_1;
1685
/* make a copy of input buffer, changing type to sample_t */
1686
for (i = 0; i < nsamples; i++) {
1687
in_buffer[0][i] = buffer_l[i];
1688
if (gfc->channels_in > 1)
1689
in_buffer[1][i] = buffer_r[i];
1692
return lame_encode_buffer_sample_t(gfp, in_buffer[0], in_buffer[1],
1693
nsamples, mp3buf, mp3buf_size);
1698
lame_encode_buffer_float(lame_global_flags * gfp,
1699
const float buffer_l[],
1700
const float buffer_r[],
1701
const int nsamples, unsigned char *mp3buf, const int mp3buf_size)
1703
lame_internal_flags *const gfc = gfp->internal_flags;
1705
sample_t *in_buffer[2];
1707
if (gfc->Class_ID != LAME_ID)
1713
if (update_inbuffer_size(gfc, nsamples) != 0) {
1717
in_buffer[0] = gfc->in_buffer_0;
1718
in_buffer[1] = gfc->in_buffer_1;
1720
/* make a copy of input buffer, changing type to sample_t */
1721
for (i = 0; i < nsamples; i++) {
1722
in_buffer[0][i] = buffer_l[i];
1723
if (gfc->channels_in > 1)
1724
in_buffer[1][i] = buffer_r[i];
1727
return lame_encode_buffer_sample_t(gfp, in_buffer[0], in_buffer[1],
1728
nsamples, mp3buf, mp3buf_size);
1733
lame_encode_buffer_int(lame_global_flags * gfp,
1734
const int buffer_l[],
1735
const int buffer_r[],
1736
const int nsamples, unsigned char *mp3buf, const int mp3buf_size)
1738
lame_internal_flags *const gfc = gfp->internal_flags;
1740
sample_t *in_buffer[2];
1742
if (gfc->Class_ID != LAME_ID)
1748
if (update_inbuffer_size(gfc, nsamples) != 0) {
1752
in_buffer[0] = gfc->in_buffer_0;
1753
in_buffer[1] = gfc->in_buffer_1;
1755
/* make a copy of input buffer, changing type to sample_t */
1756
for (i = 0; i < nsamples; i++) {
1757
/* internal code expects +/- 32768.0 */
1758
in_buffer[0][i] = buffer_l[i] * (1.0 / (1L << (8 * sizeof(int) - 16)));
1759
if (gfc->channels_in > 1)
1760
in_buffer[1][i] = buffer_r[i] * (1.0 / (1L << (8 * sizeof(int) - 16)));
1763
return lame_encode_buffer_sample_t(gfp, in_buffer[0], in_buffer[1],
1764
nsamples, mp3buf, mp3buf_size);
1771
lame_encode_buffer_long2(lame_global_flags * gfp,
1772
const long buffer_l[],
1773
const long buffer_r[],
1774
const int nsamples, unsigned char *mp3buf, const int mp3buf_size)
1776
lame_internal_flags *const gfc = gfp->internal_flags;
1778
sample_t *in_buffer[2];
1780
if (gfc->Class_ID != LAME_ID)
1786
if (update_inbuffer_size(gfc, nsamples) != 0) {
1790
in_buffer[0] = gfc->in_buffer_0;
1791
in_buffer[1] = gfc->in_buffer_1;
1793
/* make a copy of input buffer, changing type to sample_t */
1794
for (i = 0; i < nsamples; i++) {
1795
/* internal code expects +/- 32768.0 */
1796
in_buffer[0][i] = buffer_l[i] * (1.0 / (1L << (8 * sizeof(long) - 16)));
1797
if (gfc->channels_in > 1)
1798
in_buffer[1][i] = buffer_r[i] * (1.0 / (1L << (8 * sizeof(long) - 16)));
1801
return lame_encode_buffer_sample_t(gfp, in_buffer[0], in_buffer[1],
1802
nsamples, mp3buf, mp3buf_size);
1809
lame_encode_buffer_long(lame_global_flags * gfp,
1810
const long buffer_l[],
1811
const long buffer_r[],
1812
const int nsamples, unsigned char *mp3buf, const int mp3buf_size)
1814
lame_internal_flags *const gfc = gfp->internal_flags;
1816
sample_t *in_buffer[2];
1818
if (gfc->Class_ID != LAME_ID)
1824
if (update_inbuffer_size(gfc, nsamples) != 0) {
1828
in_buffer[0] = gfc->in_buffer_0;
1829
in_buffer[1] = gfc->in_buffer_1;
1831
/* make a copy of input buffer, changing type to sample_t */
1832
for (i = 0; i < nsamples; i++) {
1833
in_buffer[0][i] = buffer_l[i];
1834
if (gfc->channels_in > 1)
1835
in_buffer[1][i] = buffer_r[i];
1838
return lame_encode_buffer_sample_t(gfp, in_buffer[0], in_buffer[1],
1839
nsamples, mp3buf, mp3buf_size);
1853
lame_encode_buffer_interleaved(lame_global_flags * gfp,
1855
int nsamples, unsigned char *mp3buf, int mp3buf_size)
1857
lame_internal_flags *const gfc = gfp->internal_flags;
1859
sample_t *in_buffer[2];
1861
if (update_inbuffer_size(gfc, nsamples) != 0) {
1865
in_buffer[0] = gfc->in_buffer_0;
1866
in_buffer[1] = gfc->in_buffer_1;
1868
for (i = 0; i < nsamples; i++) {
1869
in_buffer[0][i] = buffer[2 * i];
1870
in_buffer[1][i] = buffer[2 * i + 1];
1872
return lame_encode_buffer_sample_t(gfp, in_buffer[0], in_buffer[1], nsamples, mp3buf,
1878
lame_encode(lame_global_flags * const gfp,
1879
const short int in_buffer[2][1152], unsigned char *const mp3buf, const int size)
1881
lame_internal_flags const *const gfc = gfp->internal_flags;
1883
if (gfc->Class_ID != LAME_ID)
1886
return lame_encode_buffer(gfp, in_buffer[0], in_buffer[1], gfp->framesize, mp3buf, size);
1890
/*****************************************************************
1891
Flush mp3 buffer, pad with ancillary data so last frame is complete.
1892
Reset reservoir size to 0
1893
but keep all PCM samples and MDCT data in memory
1894
This option is used to break a large file into several mp3 files
1895
that when concatenated together will decode with no gaps
1896
Because we set the reservoir=0, they will also decode seperately
1898
*********************************************************************/
1900
lame_encode_flush_nogap(lame_global_flags * gfp, unsigned char *mp3buffer, int mp3buffer_size)
1902
lame_internal_flags *const gfc = gfp->internal_flags;
1903
flush_bitstream(gfp);
1904
return copy_buffer(gfc, mp3buffer, mp3buffer_size, 1);
1908
/* called by lame_init_params. You can also call this after flush_nogap
1909
if you want to write new id3v2 and Xing VBR tags into the bitstream */
1911
lame_init_bitstream(lame_global_flags * gfp)
1913
lame_internal_flags *const gfc = gfp->internal_flags;
1916
if (gfp->write_id3tag_automatic) {
1917
(void) id3tag_write_v2(gfp);
1919
/* initialize histogram data optionally used by frontend */
1920
memset(gfc->bitrate_stereoMode_Hist, 0, sizeof(gfc->bitrate_stereoMode_Hist));
1921
memset(gfc->bitrate_blockType_Hist, 0, sizeof(gfc->bitrate_blockType_Hist));
1923
gfc->PeakSample = 0.0;
1925
/* Write initial VBR Header to bitstream and init VBR data */
1926
if (gfp->bWriteVbrTag)
1927
(void) InitVbrTag(gfp);
1934
/*****************************************************************/
1935
/* flush internal PCM sample buffers, then mp3 buffers */
1936
/* then write id3 v1 tags into bitstream. */
1937
/*****************************************************************/
1940
lame_encode_flush(lame_global_flags * gfp, unsigned char *mp3buffer, int mp3buffer_size)
1942
lame_internal_flags *const gfc = gfp->internal_flags;
1943
short int buffer[2][1152];
1944
int imp3 = 0, mp3count, mp3buffer_size_remaining;
1946
/* we always add POSTDELAY=288 padding to make sure granule with real
1947
* data can be complety decoded (because of 50% overlap with next granule */
1950
int samples_to_encode = gfc->mf_samples_to_encode - POSTDELAY;
1951
int mf_needed = calcNeeded(gfp);
1953
/* Was flush already called? */
1954
if (gfc->mf_samples_to_encode < 1) {
1957
memset(buffer, 0, sizeof(buffer));
1960
if (gfp->in_samplerate != gfp->out_samplerate) {
1961
/* delay due to resampling; needs to be fixed, if resampling code gets changed */
1962
samples_to_encode += 16.*gfp->out_samplerate/gfp->in_samplerate;
1964
end_padding = gfp->framesize - (samples_to_encode % gfp->framesize);
1965
if (end_padding < 576)
1966
end_padding += gfp->framesize;
1967
gfp->encoder_padding = end_padding;
1969
frames_left = (samples_to_encode + end_padding) / gfp->framesize;
1971
/* send in a frame of 0 padding until all internal sample buffers are flushed */
1972
while (frames_left > 0 && imp3 >= 0) {
1973
int bunch = mf_needed-gfc->mf_size;
1974
int frame_num = gfp->frameNum;
1976
bunch *= gfp->in_samplerate;
1977
bunch /= gfp->out_samplerate;
1978
if (bunch > 1152) bunch = 1152;
1979
if (bunch < 1) bunch = 1;
1981
mp3buffer_size_remaining = mp3buffer_size - mp3count;
1983
/* if user specifed buffer size = 0, dont check size */
1984
if (mp3buffer_size == 0)
1985
mp3buffer_size_remaining = 0;
1987
imp3 = lame_encode_buffer(gfp, buffer[0], buffer[1], bunch,
1988
mp3buffer, mp3buffer_size_remaining);
1992
frames_left -= (frame_num != gfp->frameNum) ? 1 : 0;
1994
/* Set gfc->mf_samples_to_encode to 0, so we may detect
1995
* and break loops calling it more than once in a row.
1997
gfc->mf_samples_to_encode = 0;
2000
/* some type of fatal error */
2004
mp3buffer_size_remaining = mp3buffer_size - mp3count;
2005
/* if user specifed buffer size = 0, dont check size */
2006
if (mp3buffer_size == 0)
2007
mp3buffer_size_remaining = 0;
2009
/* mp3 related stuff. bit buffer might still contain some mp3 data */
2010
flush_bitstream(gfp);
2011
imp3 = copy_buffer(gfc, mp3buffer, mp3buffer_size_remaining, 1);
2013
/* some type of fatal error */
2018
mp3buffer_size_remaining = mp3buffer_size - mp3count;
2019
/* if user specifed buffer size = 0, dont check size */
2020
if (mp3buffer_size == 0)
2021
mp3buffer_size_remaining = 0;
2023
if (gfp->write_id3tag_automatic) {
2024
/* write a id3 tag to the bitstream */
2025
(void) id3tag_write_v1(gfp);
2027
imp3 = copy_buffer(gfc, mp3buffer, mp3buffer_size_remaining, 0);
2036
int const ed = gfp->encoder_delay;
2037
int const ep = gfp->encoder_padding;
2038
int const ns = (gfp->frameNum*gfp->framesize) - (ed + ep);
2039
double duration = ns;
2040
duration /= gfp->out_samplerate;
2041
MSGF(gfc, "frames=%d\n", gfp->frameNum);
2042
MSGF(gfc, "framesize=%d\n", gfp->framesize);
2043
MSGF(gfc, "encoder delay=%d\n", ed);
2044
MSGF(gfc, "encoder padding=%d\n", ep);
2045
MSGF(gfc, "sample count=%d (%g)\n", ns, gfp->in_samplerate*duration);
2046
MSGF(gfc, "duration=%g sec\n", duration);
2047
MSGF(gfc, "mf_size=%d\n",gfc->mf_size);
2048
MSGF(gfc, "mf_samples_to_encode=%d\n",gfc->mf_samples_to_encode);
2054
/***********************************************************************
2058
* frees internal buffers
2060
***********************************************************************/
2063
lame_close(lame_global_flags * gfp)
2066
if (gfp && gfp->class_id == LAME_ID) {
2067
lame_internal_flags *const gfc = gfp->internal_flags;
2069
if (NULL == gfc || gfc->Class_ID != LAME_ID) {
2074
/* this routine will free all malloc'd data in gfc, and then free gfc: */
2076
gfp->internal_flags = NULL;
2078
if (gfp->lame_allocated_gfp) {
2079
gfp->lame_allocated_gfp = 0;
2086
/*****************************************************************/
2087
/* flush internal mp3 buffers, and free internal buffers */
2088
/*****************************************************************/
2089
#if DEPRECATED_OR_OBSOLETE_CODE_REMOVED
2092
lame_encode_finish(lame_global_flags * gfp, unsigned char *mp3buffer, int mp3buffer_size);
2097
lame_encode_finish(lame_global_flags * gfp, unsigned char *mp3buffer, int mp3buffer_size)
2099
int const ret = lame_encode_flush(gfp, mp3buffer, mp3buffer_size);
2101
(void) lame_close(gfp);
2106
/*****************************************************************/
2107
/* write VBR Xing header, and ID3 version 1 tag, if asked for */
2108
/*****************************************************************/
2109
void lame_mp3_tags_fid(lame_global_flags * gfp, FILE * fpStream);
2112
lame_mp3_tags_fid(lame_global_flags * gfp, FILE * fpStream)
2114
if (gfp->bWriteVbrTag) {
2115
/* Write Xing header again */
2116
if (fpStream && !fseek(fpStream, 0, SEEK_SET)) {
2117
lame_internal_flags *gfc = gfp->internal_flags;
2118
int rc = PutVbrTag(gfp, fpStream);
2125
ERRORF(gfc, "Error: could not update LAME tag.\n");
2129
ERRORF(gfc, "Error: could not update LAME tag, file not seekable.\n");
2133
ERRORF(gfc, "Error: could not update LAME tag, file not readable.\n");
2142
/* initialize mp3 encoder */
2143
#if DEPRECATED_OR_OBSOLETE_CODE_REMOVED
2148
lame_init_old(lame_global_flags * gfp)
2150
lame_internal_flags *gfc;
2152
disable_FPE(); /* disable floating point exceptions */
2154
memset(gfp, 0, sizeof(lame_global_flags));
2156
gfp->class_id = LAME_ID;
2158
if (NULL == (gfc = gfp->internal_flags = calloc(1, sizeof(lame_internal_flags))))
2161
/* Global flags. set defaults here for non-zero values */
2162
/* see lame.h for description */
2163
/* set integer values to -1 to mean that LAME will compute the
2164
* best value, UNLESS the calling program as set it
2165
* (and the value is no longer -1)
2169
gfp->mode = NOT_SET;
2171
gfp->in_samplerate = 44100;
2172
gfp->num_channels = 2;
2173
gfp->num_samples = MAX_U_32_NUM;
2175
gfp->bWriteVbrTag = 1;
2177
gfp->short_blocks = short_block_not_set;
2178
gfc->subblock_gain = -1;
2180
gfp->lowpassfreq = 0;
2181
gfp->highpassfreq = 0;
2182
gfp->lowpasswidth = -1;
2183
gfp->highpasswidth = -1;
2188
gfp->VBR_mean_bitrate_kbps = 128;
2189
gfp->VBR_min_bitrate_kbps = 0;
2190
gfp->VBR_max_bitrate_kbps = 0;
2191
gfp->VBR_hard_min = 0;
2192
gfc->VBR_min_bitrate = 1; /* not 0 ????? */
2193
gfc->VBR_max_bitrate = 13; /* not 14 ????? */
2195
gfp->quant_comp = -1;
2196
gfp->quant_comp_short = -1;
2200
gfc->resample_ratio = 1;
2202
gfc->OldValue[0] = 180;
2203
gfc->OldValue[1] = 180;
2204
gfc->CurrentStep[0] = 4;
2205
gfc->CurrentStep[1] = 4;
2206
gfc->masking_lower = 1;
2207
gfc->nsPsy.attackthre = -1;
2208
gfc->nsPsy.attackthre_s = -1;
2212
gfp->athaa_type = -1;
2213
gfp->ATHtype = -1; /* default = -1 = set in lame_init_params */
2214
gfp->athaa_loudapprox = -1; /* 1 = flat loudness approx. (total energy) */
2215
/* 2 = equal loudness curve */
2216
gfp->athaa_sensitivity = 0.0; /* no offset */
2217
gfp->useTemporal = -1;
2218
gfp->interChRatio = -1;
2221
* int mf_samples_to_encode = ENCDELAY + POSTDELAY;
2222
* ENCDELAY = internal encoder delay. And then we have to add POSTDELAY=288
2223
* because of the 50% MDCT overlap. A 576 MDCT granule decodes to
2224
* 1152 samples. To synthesize the 576 samples centered under this granule
2225
* we need the previous granule for the first 288 samples (no problem), and
2226
* the next granule for the next 288 samples (not possible if this is last
2227
* granule). So we need to pad with 288 samples to make sure we can
2228
* encode the 576 samples we are interested in.
2230
gfc->mf_samples_to_encode = ENCDELAY + POSTDELAY;
2231
gfp->encoder_padding = 0;
2232
gfc->mf_size = ENCDELAY - MDCTDELAY; /* we pad input with this many 0's */
2234
gfp->findReplayGain = 0;
2235
gfp->decode_on_the_fly = 0;
2237
gfc->decode_on_the_fly = 0;
2238
gfc->findReplayGain = 0;
2239
gfc->findPeakSample = 0;
2242
gfc->AudiophileGain = 0;
2243
gfc->noclipGainChange = 0;
2244
gfc->noclipScale = -1.0;
2246
gfp->asm_optimizations.mmx = 1;
2247
gfp->asm_optimizations.amd3dnow = 1;
2248
gfp->asm_optimizations.sse = 1;
2252
gfp->write_id3tag_automatic = 1;
2260
lame_global_flags *gfp;
2265
gfp = calloc(1, sizeof(lame_global_flags));
2269
ret = lame_init_old(gfp);
2275
gfp->lame_allocated_gfp = 1;
2280
/***********************************************************************
2282
* some simple statistics
2284
* Robert Hegemann 2000-10-11
2286
***********************************************************************/
2288
/* histogram of used bitrate indexes:
2289
* One has to weight them to calculate the average bitrate in kbps
2292
* there are 14 possible bitrate indices, 0 has the special meaning
2293
* "free format" which is not possible to mix with VBR and 15 is forbidden
2297
* 0: LR number of left-right encoded frames
2298
* 1: LR-I number of left-right and intensity encoded frames
2299
* 2: MS number of mid-side encoded frames
2300
* 3: MS-I number of mid-side and intensity encoded frames
2302
* 4: number of encoded frames
2307
lame_bitrate_kbps(const lame_global_flags * gfp, int bitrate_kbps[14])
2309
const lame_internal_flags *gfc;
2312
if (NULL == bitrate_kbps)
2316
gfc = gfp->internal_flags;
2320
if (gfp->free_format) {
2321
for (i = 0; i < 14; i++)
2322
bitrate_kbps[i] = -1;
2323
bitrate_kbps[0] = gfp->brate;
2326
for (i = 0; i < 14; i++)
2327
bitrate_kbps[i] = bitrate_table[gfp->version][i + 1];
2333
lame_bitrate_hist(const lame_global_flags * gfp, int bitrate_count[14])
2335
const lame_internal_flags *gfc;
2338
if (NULL == bitrate_count)
2342
gfc = gfp->internal_flags;
2346
if (gfp->free_format) {
2347
for (i = 0; i < 14; i++)
2348
bitrate_count[i] = 0;
2349
bitrate_count[0] = gfc->bitrate_stereoMode_Hist[0][4];
2352
for (i = 0; i < 14; i++)
2353
bitrate_count[i] = gfc->bitrate_stereoMode_Hist[i + 1][4];
2359
lame_stereo_mode_hist(const lame_global_flags * gfp, int stmode_count[4])
2361
const lame_internal_flags *gfc;
2364
if (NULL == stmode_count)
2368
gfc = gfp->internal_flags;
2372
for (i = 0; i < 4; i++) {
2373
stmode_count[i] = gfc->bitrate_stereoMode_Hist[15][i];
2380
lame_bitrate_stereo_mode_hist(const lame_global_flags * gfp, int bitrate_stmode_count[14][4])
2382
const lame_internal_flags *gfc;
2386
if (NULL == bitrate_stmode_count)
2390
gfc = gfp->internal_flags;
2394
if (gfp->free_format) {
2395
for (j = 0; j < 14; j++)
2396
for (i = 0; i < 4; i++)
2397
bitrate_stmode_count[j][i] = 0;
2398
for (i = 0; i < 4; i++)
2399
bitrate_stmode_count[0][i] = gfc->bitrate_stereoMode_Hist[0][i];
2402
for (j = 0; j < 14; j++)
2403
for (i = 0; i < 4; i++)
2404
bitrate_stmode_count[j][i] = gfc->bitrate_stereoMode_Hist[j + 1][i];
2410
lame_block_type_hist(const lame_global_flags * gfp, int btype_count[6])
2412
const lame_internal_flags *gfc;
2415
if (NULL == btype_count)
2419
gfc = gfp->internal_flags;
2423
for (i = 0; i < 6; ++i) {
2424
btype_count[i] = gfc->bitrate_blockType_Hist[15][i];
2431
lame_bitrate_block_type_hist(const lame_global_flags * gfp, int bitrate_btype_count[14][6])
2433
const lame_internal_flags *gfc;
2436
if (NULL == bitrate_btype_count)
2440
gfc = gfp->internal_flags;
2444
if (gfp->free_format) {
2445
for (j = 0; j < 14; ++j)
2446
for (i = 0; i < 6; ++i)
2447
bitrate_btype_count[j][i] = 0;
2448
for (i = 0; i < 6; ++i)
2449
bitrate_btype_count[0][i] = gfc->bitrate_blockType_Hist[0][i];
2452
for (j = 0; j < 14; ++j)
2453
for (i = 0; i < 6; ++i)
2454
bitrate_btype_count[j][i] = gfc->bitrate_blockType_Hist[j + 1][i];