1 /* -*- mode: C; mode: fold -*- */
2 /*
3 * LAME MP3 encoding engine
4 *
5 * Copyright (c) 1999-2000 Mark Taylor
6 * Copyright (c) 2000-2005 Takehiro Tominaga
7 * Copyright (c) 2000-2017 Robert Hegemann
8 * Copyright (c) 2000-2005 Gabriel Bouvigne
9 * Copyright (c) 2000-2004 Alexander Leidinger
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library 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.
15 *
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.
20 *
21 * You should have received a copy of the GNU Library 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.
25 */
26
27 /* $Id: lame.c,v 1.377 2017/09/26 12:14:02 robert Exp $ */
28
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32
33
34 #include "lame.h"
35 #include "machine.h"
36
37 #include "encoder.h"
38 #include "util.h"
39 #include "lame_global_flags.h"
40 #include "gain_analysis.h"
41 #include "bitstream.h"
42 #include "quantize_pvt.h"
43 #include "set_get.h"
44 #include "quantize.h"
45 #include "psymodel.h"
46 #include "version.h"
47 #include "VbrTag.h"
48 #include "tables.h"
49
50
51 #if defined(__FreeBSD__) && !defined(__alpha__)
52 #include <floatingpoint.h>
53 #endif
54 #ifdef __riscos__
55 #include "asmstuff.h"
56 #endif
57
58 #ifdef __sun__
59 /* woraround for SunOS 4.x, it has SEEK_* defined here */
60 #include <unistd.h>
61 #endif
62
63
64 #define LAME_DEFAULT_QUALITY 3
65
66
67
68 int
is_lame_global_flags_valid(const lame_global_flags * gfp)69 is_lame_global_flags_valid(const lame_global_flags * gfp)
70 {
71 if (gfp == NULL)
72 return 0;
73 if (gfp->class_id != LAME_ID)
74 return 0;
75 return 1;
76 }
77
78
79 int
is_lame_internal_flags_valid(const lame_internal_flags * gfc)80 is_lame_internal_flags_valid(const lame_internal_flags * gfc)
81 {
82 if (gfc == NULL)
83 return 0;
84 if (gfc->class_id != LAME_ID)
85 return 0;
86 if (gfc->lame_init_params_successful <=0)
87 return 0;
88 return 1;
89 }
90
91
92
93 static FLOAT
filter_coef(FLOAT x)94 filter_coef(FLOAT x)
95 {
96 if (x > 1.0)
97 return 0.0;
98 if (x <= 0.0)
99 return 1.0;
100
101 return cos(PI / 2 * x);
102 }
103
104 static void
lame_init_params_ppflt(lame_internal_flags * gfc)105 lame_init_params_ppflt(lame_internal_flags * gfc)
106 {
107 SessionConfig_t *const cfg = &gfc->cfg;
108
109 /***************************************************************/
110 /* compute info needed for polyphase filter (filter type==0, default) */
111 /***************************************************************/
112
113 int band, maxband, minband;
114 FLOAT freq;
115 int lowpass_band = 32;
116 int highpass_band = -1;
117
118 if (cfg->lowpass1 > 0) {
119 minband = 999;
120 for (band = 0; band <= 31; band++) {
121 freq = band / 31.0;
122 /* this band and above will be zeroed: */
123 if (freq >= cfg->lowpass2) {
124 lowpass_band = Min(lowpass_band, band);
125 }
126 if (cfg->lowpass1 < freq && freq < cfg->lowpass2) {
127 minband = Min(minband, band);
128 }
129 }
130
131 /* compute the *actual* transition band implemented by
132 * the polyphase filter */
133 if (minband == 999) {
134 cfg->lowpass1 = (lowpass_band - .75) / 31.0;
135 }
136 else {
137 cfg->lowpass1 = (minband - .75) / 31.0;
138 }
139 cfg->lowpass2 = lowpass_band / 31.0;
140 }
141
142 /* make sure highpass filter is within 90% of what the effective
143 * highpass frequency will be */
144 if (cfg->highpass2 > 0) {
145 if (cfg->highpass2 < .9 * (.75 / 31.0)) {
146 cfg->highpass1 = 0;
147 cfg->highpass2 = 0;
148 MSGF(gfc, "Warning: highpass filter disabled. " "highpass frequency too small\n");
149 }
150 }
151
152 if (cfg->highpass2 > 0) {
153 maxband = -1;
154 for (band = 0; band <= 31; band++) {
155 freq = band / 31.0;
156 /* this band and below will be zereod */
157 if (freq <= cfg->highpass1) {
158 highpass_band = Max(highpass_band, band);
159 }
160 if (cfg->highpass1 < freq && freq < cfg->highpass2) {
161 maxband = Max(maxband, band);
162 }
163 }
164 /* compute the *actual* transition band implemented by
165 * the polyphase filter */
166 cfg->highpass1 = highpass_band / 31.0;
167 if (maxband == -1) {
168 cfg->highpass2 = (highpass_band + .75) / 31.0;
169 }
170 else {
171 cfg->highpass2 = (maxband + .75) / 31.0;
172 }
173 }
174
175 for (band = 0; band < 32; band++) {
176 FLOAT fc1, fc2;
177 freq = band / 31.0f;
178 if (cfg->highpass2 > cfg->highpass1) {
179 fc1 = filter_coef((cfg->highpass2 - freq) / (cfg->highpass2 - cfg->highpass1 + 1e-20));
180 }
181 else {
182 fc1 = 1.0f;
183 }
184 if (cfg->lowpass2 > cfg->lowpass1) {
185 fc2 = filter_coef((freq - cfg->lowpass1) / (cfg->lowpass2 - cfg->lowpass1 + 1e-20));
186 }
187 else {
188 fc2 = 1.0f;
189 }
190 gfc->sv_enc.amp_filter[band] = fc1 * fc2;
191 }
192 }
193
194
195 static void
optimum_bandwidth(double * const lowerlimit,double * const upperlimit,const unsigned bitrate)196 optimum_bandwidth(double *const lowerlimit, double *const upperlimit, const unsigned bitrate)
197 {
198 /*
199 * Input:
200 * bitrate total bitrate in kbps
201 *
202 * Output:
203 * lowerlimit: best lowpass frequency limit for input filter in Hz
204 * upperlimit: best highpass frequency limit for input filter in Hz
205 */
206 int table_index;
207
208 typedef struct {
209 int bitrate; /* only indicative value */
210 int lowpass;
211 } band_pass_t;
212
213 const band_pass_t freq_map[] = {
214 {8, 2000},
215 {16, 3700},
216 {24, 3900},
217 {32, 5500},
218 {40, 7000},
219 {48, 7500},
220 {56, 10000},
221 {64, 11000},
222 {80, 13500},
223 {96, 15100},
224 {112, 15600},
225 {128, 17000},
226 {160, 17500},
227 {192, 18600},
228 {224, 19400},
229 {256, 19700},
230 {320, 20500}
231 };
232
233
234 table_index = nearestBitrateFullIndex(bitrate);
235
236 (void) freq_map[table_index].bitrate;
237 *lowerlimit = freq_map[table_index].lowpass;
238
239
240 /*
241 * Now we try to choose a good high pass filtering frequency.
242 * This value is currently not used.
243 * For fu < 16 kHz: sqrt(fu*fl) = 560 Hz
244 * For fu = 18 kHz: no high pass filtering
245 * This gives:
246 *
247 * 2 kHz => 160 Hz
248 * 3 kHz => 107 Hz
249 * 4 kHz => 80 Hz
250 * 8 kHz => 40 Hz
251 * 16 kHz => 20 Hz
252 * 17 kHz => 10 Hz
253 * 18 kHz => 0 Hz
254 *
255 * These are ad hoc values and these can be optimized if a high pass is available.
256 */
257 /* if (f_low <= 16000)
258 f_high = 16000. * 20. / f_low;
259 else if (f_low <= 18000)
260 f_high = 180. - 0.01 * f_low;
261 else
262 f_high = 0.;*/
263
264 /*
265 * When we sometimes have a good highpass filter, we can add the highpass
266 * frequency to the lowpass frequency
267 */
268
269 /*if (upperlimit != NULL)
270 *upperlimit = f_high;*/
271 (void) upperlimit;
272 }
273
274
275 static int
optimum_samplefreq(int lowpassfreq,int input_samplefreq)276 optimum_samplefreq(int lowpassfreq, int input_samplefreq)
277 {
278 /*
279 * Rules:
280 * - if possible, sfb21 should NOT be used
281 *
282 */
283 int suggested_samplefreq = 44100;
284
285 if (input_samplefreq >= 48000)
286 suggested_samplefreq = 48000;
287 else if (input_samplefreq >= 44100)
288 suggested_samplefreq = 44100;
289 else if (input_samplefreq >= 32000)
290 suggested_samplefreq = 32000;
291 else if (input_samplefreq >= 24000)
292 suggested_samplefreq = 24000;
293 else if (input_samplefreq >= 22050)
294 suggested_samplefreq = 22050;
295 else if (input_samplefreq >= 16000)
296 suggested_samplefreq = 16000;
297 else if (input_samplefreq >= 12000)
298 suggested_samplefreq = 12000;
299 else if (input_samplefreq >= 11025)
300 suggested_samplefreq = 11025;
301 else if (input_samplefreq >= 8000)
302 suggested_samplefreq = 8000;
303
304 if (lowpassfreq == -1)
305 return suggested_samplefreq;
306
307 if (lowpassfreq <= 15960)
308 suggested_samplefreq = 44100;
309 if (lowpassfreq <= 15250)
310 suggested_samplefreq = 32000;
311 if (lowpassfreq <= 11220)
312 suggested_samplefreq = 24000;
313 if (lowpassfreq <= 9970)
314 suggested_samplefreq = 22050;
315 if (lowpassfreq <= 7230)
316 suggested_samplefreq = 16000;
317 if (lowpassfreq <= 5420)
318 suggested_samplefreq = 12000;
319 if (lowpassfreq <= 4510)
320 suggested_samplefreq = 11025;
321 if (lowpassfreq <= 3970)
322 suggested_samplefreq = 8000;
323
324 if (input_samplefreq < suggested_samplefreq) {
325 /* choose a valid MPEG sample frequency above the input sample frequency
326 to avoid SFB21/12 bitrate bloat
327 rh 061115
328 */
329 if (input_samplefreq > 44100) {
330 return 48000;
331 }
332 if (input_samplefreq > 32000) {
333 return 44100;
334 }
335 if (input_samplefreq > 24000) {
336 return 32000;
337 }
338 if (input_samplefreq > 22050) {
339 return 24000;
340 }
341 if (input_samplefreq > 16000) {
342 return 22050;
343 }
344 if (input_samplefreq > 12000) {
345 return 16000;
346 }
347 if (input_samplefreq > 11025) {
348 return 12000;
349 }
350 if (input_samplefreq > 8000) {
351 return 11025;
352 }
353 return 8000;
354 }
355 return suggested_samplefreq;
356 }
357
358
359
360
361
362 /* set internal feature flags. USER should not access these since
363 * some combinations will produce strange results */
364 static void
lame_init_qval(lame_global_flags * gfp)365 lame_init_qval(lame_global_flags * gfp)
366 {
367 lame_internal_flags *const gfc = gfp->internal_flags;
368 SessionConfig_t *const cfg = &gfc->cfg;
369
370 switch (gfp->quality) {
371 default:
372 case 9: /* no psymodel, no noise shaping */
373 cfg->noise_shaping = 0;
374 cfg->noise_shaping_amp = 0;
375 cfg->noise_shaping_stop = 0;
376 cfg->use_best_huffman = 0;
377 cfg->full_outer_loop = 0;
378 break;
379
380 case 8:
381 gfp->quality = 7;
382 /*lint --fallthrough */
383 case 7: /* use psymodel (for short block and m/s switching), but no noise shapping */
384 cfg->noise_shaping = 0;
385 cfg->noise_shaping_amp = 0;
386 cfg->noise_shaping_stop = 0;
387 cfg->use_best_huffman = 0;
388 cfg->full_outer_loop = 0;
389 if (cfg->vbr == vbr_mt || cfg->vbr == vbr_mtrh) {
390 cfg->full_outer_loop = -1;
391 }
392 break;
393
394 case 6:
395 if (cfg->noise_shaping == 0)
396 cfg->noise_shaping = 1;
397 cfg->noise_shaping_amp = 0;
398 cfg->noise_shaping_stop = 0;
399 if (cfg->subblock_gain == -1)
400 cfg->subblock_gain = 1;
401 cfg->use_best_huffman = 0;
402 cfg->full_outer_loop = 0;
403 break;
404
405 case 5:
406 if (cfg->noise_shaping == 0)
407 cfg->noise_shaping = 1;
408 cfg->noise_shaping_amp = 0;
409 cfg->noise_shaping_stop = 0;
410 if (cfg->subblock_gain == -1)
411 cfg->subblock_gain = 1;
412 cfg->use_best_huffman = 0;
413 cfg->full_outer_loop = 0;
414 break;
415
416 case 4:
417 if (cfg->noise_shaping == 0)
418 cfg->noise_shaping = 1;
419 cfg->noise_shaping_amp = 0;
420 cfg->noise_shaping_stop = 0;
421 if (cfg->subblock_gain == -1)
422 cfg->subblock_gain = 1;
423 cfg->use_best_huffman = 1;
424 cfg->full_outer_loop = 0;
425 break;
426
427 case 3:
428 if (cfg->noise_shaping == 0)
429 cfg->noise_shaping = 1;
430 cfg->noise_shaping_amp = 1;
431 cfg->noise_shaping_stop = 1;
432 if (cfg->subblock_gain == -1)
433 cfg->subblock_gain = 1;
434 cfg->use_best_huffman = 1;
435 cfg->full_outer_loop = 0;
436 break;
437
438 case 2:
439 if (cfg->noise_shaping == 0)
440 cfg->noise_shaping = 1;
441 if (gfc->sv_qnt.substep_shaping == 0)
442 gfc->sv_qnt.substep_shaping = 2;
443 cfg->noise_shaping_amp = 1;
444 cfg->noise_shaping_stop = 1;
445 if (cfg->subblock_gain == -1)
446 cfg->subblock_gain = 1;
447 cfg->use_best_huffman = 1; /* inner loop */
448 cfg->full_outer_loop = 0;
449 break;
450
451 case 1:
452 if (cfg->noise_shaping == 0)
453 cfg->noise_shaping = 1;
454 if (gfc->sv_qnt.substep_shaping == 0)
455 gfc->sv_qnt.substep_shaping = 2;
456 cfg->noise_shaping_amp = 2;
457 cfg->noise_shaping_stop = 1;
458 if (cfg->subblock_gain == -1)
459 cfg->subblock_gain = 1;
460 cfg->use_best_huffman = 1;
461 cfg->full_outer_loop = 0;
462 break;
463
464 case 0:
465 if (cfg->noise_shaping == 0)
466 cfg->noise_shaping = 1;
467 if (gfc->sv_qnt.substep_shaping == 0)
468 gfc->sv_qnt.substep_shaping = 2;
469 cfg->noise_shaping_amp = 2;
470 cfg->noise_shaping_stop = 1;
471 if (cfg->subblock_gain == -1)
472 cfg->subblock_gain = 1;
473 cfg->use_best_huffman = 1; /*type 2 disabled because of it slowness,
474 in favor of full outer loop search */
475 cfg->full_outer_loop = 1;
476 break;
477 }
478
479 }
480
481
482
483 static double
linear_int(double a,double b,double m)484 linear_int(double a, double b, double m)
485 {
486 return a + m * (b - a);
487 }
488
489
490
491 /********************************************************************
492 * initialize internal params based on data in gf
493 * (globalflags struct filled in by calling program)
494 *
495 * OUTLINE:
496 *
497 * We first have some complex code to determine bitrate,
498 * output samplerate and mode. It is complicated by the fact
499 * that we allow the user to set some or all of these parameters,
500 * and need to determine best possible values for the rest of them:
501 *
502 * 1. set some CPU related flags
503 * 2. check if we are mono->mono, stereo->mono or stereo->stereo
504 * 3. compute bitrate and output samplerate:
505 * user may have set compression ratio
506 * user may have set a bitrate
507 * user may have set a output samplerate
508 * 4. set some options which depend on output samplerate
509 * 5. compute the actual compression ratio
510 * 6. set mode based on compression ratio
511 *
512 * The remaining code is much simpler - it just sets options
513 * based on the mode & compression ratio:
514 *
515 * set allow_diff_short based on mode
516 * select lowpass filter based on compression ratio & mode
517 * set the bitrate index, and min/max bitrates for VBR modes
518 * disable VBR tag if it is not appropriate
519 * initialize the bitstream
520 * initialize scalefac_band data
521 * set sideinfo_len (based on channels, CRC, out_samplerate)
522 * write an id3v2 tag into the bitstream
523 * write VBR tag into the bitstream
524 * set mpeg1/2 flag
525 * estimate the number of frames (based on a lot of data)
526 *
527 * now we set more flags:
528 * nspsytune:
529 * see code
530 * VBR modes
531 * see code
532 * CBR/ABR
533 * see code
534 *
535 * Finally, we set the algorithm flags based on the gfp->quality value
536 * lame_init_qval(gfp);
537 *
538 ********************************************************************/
539 int
lame_init_params(lame_global_flags * gfp)540 lame_init_params(lame_global_flags * gfp)
541 {
542
543 int i;
544 int j;
545 lame_internal_flags *gfc;
546 SessionConfig_t *cfg;
547
548 if (!is_lame_global_flags_valid(gfp))
549 return -1;
550
551 gfc = gfp->internal_flags;
552 if (gfc == 0)
553 return -1;
554
555 if (is_lame_internal_flags_valid(gfc))
556 return -1; /* already initialized */
557
558 /* start updating lame internal flags */
559 gfc->class_id = LAME_ID;
560 gfc->lame_init_params_successful = 0; /* will be set to one, when we get through until the end */
561
562 if (gfp->samplerate_in < 1)
563 return -1; /* input sample rate makes no sense */
564 if (gfp->num_channels < 1 || 2 < gfp->num_channels)
565 return -1; /* number of input channels makes no sense */
566 if (gfp->samplerate_out != 0) {
567 int v=0;
568 if (SmpFrqIndex(gfp->samplerate_out, &v) < 0)
569 return -1; /* output sample rate makes no sense */
570 }
571
572 cfg = &gfc->cfg;
573
574 cfg->enforce_min_bitrate = gfp->VBR_hard_min;
575 cfg->analysis = gfp->analysis;
576 if (cfg->analysis)
577 gfp->write_lame_tag = 0;
578
579 /* some file options not allowed if output is: not specified or stdout */
580 if (gfc->pinfo != NULL)
581 gfp->write_lame_tag = 0; /* disable Xing VBR tag */
582
583 /* report functions */
584 gfc->report_msg = gfp->report.msgf;
585 gfc->report_dbg = gfp->report.debugf;
586 gfc->report_err = gfp->report.errorf;
587
588 if (gfp->asm_optimizations.amd3dnow)
589 gfc->CPU_features.AMD_3DNow = has_3DNow();
590 else
591 gfc->CPU_features.AMD_3DNow = 0;
592
593 if (gfp->asm_optimizations.mmx)
594 gfc->CPU_features.MMX = has_MMX();
595 else
596 gfc->CPU_features.MMX = 0;
597
598 if (gfp->asm_optimizations.sse) {
599 gfc->CPU_features.SSE = has_SSE();
600 gfc->CPU_features.SSE2 = has_SSE2();
601 }
602 else {
603 gfc->CPU_features.SSE = 0;
604 gfc->CPU_features.SSE2 = 0;
605 }
606
607
608 cfg->vbr = gfp->VBR;
609 cfg->error_protection = gfp->error_protection;
610 cfg->copyright = gfp->copyright;
611 cfg->original = gfp->original;
612 cfg->extension = gfp->extension;
613 cfg->emphasis = gfp->emphasis;
614
615 cfg->channels_in = gfp->num_channels;
616 if (cfg->channels_in == 1)
617 gfp->mode = MONO;
618 cfg->channels_out = (gfp->mode == MONO) ? 1 : 2;
619 if (gfp->mode != JOINT_STEREO)
620 gfp->force_ms = 0; /* forced mid/side stereo for j-stereo only */
621 cfg->force_ms = gfp->force_ms;
622
623 if (cfg->vbr == vbr_off && gfp->VBR_mean_bitrate_kbps != 128 && gfp->brate == 0)
624 gfp->brate = gfp->VBR_mean_bitrate_kbps;
625
626 switch (cfg->vbr) {
627 case vbr_off:
628 case vbr_mtrh:
629 case vbr_mt:
630 /* these modes can handle free format condition */
631 break;
632 default:
633 gfp->free_format = 0; /* mode can't be mixed with free format */
634 break;
635 }
636
637 cfg->free_format = gfp->free_format;
638
639 if (cfg->vbr == vbr_off && gfp->brate == 0) {
640 /* no bitrate or compression ratio specified, use 11.025 */
641 if (EQ(gfp->compression_ratio, 0))
642 gfp->compression_ratio = 11.025; /* rate to compress a CD down to exactly 128000 bps */
643 }
644
645 /* find bitrate if user specify a compression ratio */
646 if (cfg->vbr == vbr_off && gfp->compression_ratio > 0) {
647
648 if (gfp->samplerate_out == 0)
649 gfp->samplerate_out = map2MP3Frequency((int) (0.97 * gfp->samplerate_in)); /* round up with a margin of 3% */
650
651 /* choose a bitrate for the output samplerate which achieves
652 * specified compression ratio
653 */
654 gfp->brate = gfp->samplerate_out * 16 * cfg->channels_out / (1.e3 * gfp->compression_ratio);
655
656 /* we need the version for the bitrate table look up */
657 cfg->samplerate_index = SmpFrqIndex(gfp->samplerate_out, &cfg->version);
658 assert(cfg->samplerate_index >=0);
659
660 if (!cfg->free_format) /* for non Free Format find the nearest allowed bitrate */
661 gfp->brate = FindNearestBitrate(gfp->brate, cfg->version, gfp->samplerate_out);
662 }
663 if (gfp->samplerate_out) {
664 if (gfp->samplerate_out < 16000) {
665 gfp->VBR_mean_bitrate_kbps = Max(gfp->VBR_mean_bitrate_kbps, 8);
666 gfp->VBR_mean_bitrate_kbps = Min(gfp->VBR_mean_bitrate_kbps, 64);
667 }
668 else if (gfp->samplerate_out < 32000) {
669 gfp->VBR_mean_bitrate_kbps = Max(gfp->VBR_mean_bitrate_kbps, 8);
670 gfp->VBR_mean_bitrate_kbps = Min(gfp->VBR_mean_bitrate_kbps, 160);
671 }
672 else {
673 gfp->VBR_mean_bitrate_kbps = Max(gfp->VBR_mean_bitrate_kbps, 32);
674 gfp->VBR_mean_bitrate_kbps = Min(gfp->VBR_mean_bitrate_kbps, 320);
675 }
676 }
677 /* WORK IN PROGRESS */
678 /* mapping VBR scale to internal VBR quality settings */
679 if (gfp->samplerate_out == 0 && (cfg->vbr == vbr_mt || cfg->vbr == vbr_mtrh)) {
680 float const qval = gfp->VBR_q + gfp->VBR_q_frac;
681 struct q_map { int sr_a; float qa, qb, ta, tb; int lp; };
682 struct q_map const m[9]
683 = { {48000, 0.0,6.5, 0.0,6.5, 23700}
684 , {44100, 0.0,6.5, 0.0,6.5, 21780}
685 , {32000, 6.5,8.0, 5.2,6.5, 15800}
686 , {24000, 8.0,8.5, 5.2,6.0, 11850}
687 , {22050, 8.5,9.01, 5.2,6.5, 10892}
688 , {16000, 9.01,9.4, 4.9,6.5, 7903}
689 , {12000, 9.4,9.6, 4.5,6.0, 5928}
690 , {11025, 9.6,9.9, 5.1,6.5, 5446}
691 , { 8000, 9.9,10., 4.9,6.5, 3952}
692 };
693 for (i = 2; i < 9; ++i) {
694 if (gfp->samplerate_in == m[i].sr_a) {
695 if (qval < m[i].qa) {
696 double d = qval / m[i].qa;
697 d = d * m[i].ta;
698 gfp->VBR_q = (int)d;
699 gfp->VBR_q_frac = d - gfp->VBR_q;
700 }
701 }
702 if (gfp->samplerate_in >= m[i].sr_a) {
703 if (m[i].qa <= qval && qval < m[i].qb) {
704 float const q_ = m[i].qb-m[i].qa;
705 float const t_ = m[i].tb-m[i].ta;
706 double d = m[i].ta + t_ * (qval-m[i].qa) / q_;
707 gfp->VBR_q = (int)d;
708 gfp->VBR_q_frac = d - gfp->VBR_q;
709 gfp->samplerate_out = m[i].sr_a;
710 if (gfp->lowpassfreq == 0) {
711 gfp->lowpassfreq = -1;
712 }
713 break;
714 }
715 }
716 }
717 }
718
719 /****************************************************************/
720 /* if a filter has not been enabled, see if we should add one: */
721 /****************************************************************/
722 if (gfp->lowpassfreq == 0) {
723 double lowpass = 16000;
724 double highpass;
725
726 switch (cfg->vbr) {
727 case vbr_off:{
728 optimum_bandwidth(&lowpass, &highpass, gfp->brate);
729 break;
730 }
731 case vbr_abr:{
732 optimum_bandwidth(&lowpass, &highpass, gfp->VBR_mean_bitrate_kbps);
733 break;
734 }
735 case vbr_rh:{
736 int const x[11] = {
737 19500, 19000, 18600, 18000, 17500, 16000, 15600, 14900, 12500, 10000, 3950
738 };
739 if (0 <= gfp->VBR_q && gfp->VBR_q <= 9) {
740 double a = x[gfp->VBR_q], b = x[gfp->VBR_q + 1], m = gfp->VBR_q_frac;
741 lowpass = linear_int(a, b, m);
742 }
743 else {
744 lowpass = 19500;
745 }
746 break;
747 }
748 case vbr_mtrh:
749 case vbr_mt:{
750 int const x[11] = {
751 24000, 19500, 18500, 18000, 17500, 17000, 16500, 15600, 15200, 7230, 3950
752 };
753 if (0 <= gfp->VBR_q && gfp->VBR_q <= 9) {
754 double a = x[gfp->VBR_q], b = x[gfp->VBR_q + 1], m = gfp->VBR_q_frac;
755 lowpass = linear_int(a, b, m);
756 }
757 else {
758 lowpass = 21500;
759 }
760 break;
761 }
762 default:{
763 int const x[11] = {
764 19500, 19000, 18500, 18000, 17500, 16500, 15500, 14500, 12500, 9500, 3950
765 };
766 if (0 <= gfp->VBR_q && gfp->VBR_q <= 9) {
767 double a = x[gfp->VBR_q], b = x[gfp->VBR_q + 1], m = gfp->VBR_q_frac;
768 lowpass = linear_int(a, b, m);
769 }
770 else {
771 lowpass = 19500;
772 }
773 }
774 }
775
776 if (gfp->mode == MONO && (cfg->vbr == vbr_off || cfg->vbr == vbr_abr))
777 lowpass *= 1.5;
778
779 gfp->lowpassfreq = lowpass;
780 }
781
782 if (gfp->samplerate_out == 0) {
783 if (2 * gfp->lowpassfreq > gfp->samplerate_in) {
784 gfp->lowpassfreq = gfp->samplerate_in / 2;
785 }
786 gfp->samplerate_out = optimum_samplefreq((int) gfp->lowpassfreq, gfp->samplerate_in);
787 }
788 if (cfg->vbr == vbr_mt || cfg->vbr == vbr_mtrh) {
789 gfp->lowpassfreq = Min(24000, gfp->lowpassfreq);
790 }
791 else {
792 gfp->lowpassfreq = Min(20500, gfp->lowpassfreq);
793 }
794 gfp->lowpassfreq = Min(gfp->samplerate_out / 2, gfp->lowpassfreq);
795
796 if (cfg->vbr == vbr_off) {
797 gfp->compression_ratio = gfp->samplerate_out * 16 * cfg->channels_out / (1.e3 * gfp->brate);
798 }
799 if (cfg->vbr == vbr_abr) {
800 gfp->compression_ratio =
801 gfp->samplerate_out * 16 * cfg->channels_out / (1.e3 * gfp->VBR_mean_bitrate_kbps);
802 }
803
804 cfg->disable_reservoir = gfp->disable_reservoir;
805 cfg->lowpassfreq = gfp->lowpassfreq;
806 cfg->highpassfreq = gfp->highpassfreq;
807 cfg->samplerate_in = gfp->samplerate_in;
808 cfg->samplerate_out = gfp->samplerate_out;
809 cfg->mode_gr = cfg->samplerate_out <= 24000 ? 1 : 2; /* Number of granules per frame */
810
811
812 /*
813 * sample freq bitrate compression ratio
814 * [kHz] [kbps/channel] for 16 bit input
815 * 44.1 56 12.6
816 * 44.1 64 11.025
817 * 44.1 80 8.82
818 * 22.05 24 14.7
819 * 22.05 32 11.025
820 * 22.05 40 8.82
821 * 16 16 16.0
822 * 16 24 10.667
823 *
824 */
825 /*
826 * For VBR, take a guess at the compression_ratio.
827 * For example:
828 *
829 * VBR_q compression like
830 * - 4.4 320 kbps/44 kHz
831 * 0...1 5.5 256 kbps/44 kHz
832 * 2 7.3 192 kbps/44 kHz
833 * 4 8.8 160 kbps/44 kHz
834 * 6 11 128 kbps/44 kHz
835 * 9 14.7 96 kbps
836 *
837 * for lower bitrates, downsample with --resample
838 */
839
840 switch (cfg->vbr) {
841 case vbr_mt:
842 case vbr_rh:
843 case vbr_mtrh:
844 {
845 /*numbers are a bit strange, but they determine the lowpass value */
846 FLOAT const cmp[] = { 5.7, 6.5, 7.3, 8.2, 10, 11.9, 13, 14, 15, 16.5 };
847 gfp->compression_ratio = cmp[gfp->VBR_q];
848 }
849 break;
850 case vbr_abr:
851 gfp->compression_ratio =
852 cfg->samplerate_out * 16 * cfg->channels_out / (1.e3 * gfp->VBR_mean_bitrate_kbps);
853 break;
854 default:
855 gfp->compression_ratio = cfg->samplerate_out * 16 * cfg->channels_out / (1.e3 * gfp->brate);
856 break;
857 }
858
859
860 /* mode = -1 (not set by user) or
861 * mode = MONO (because of only 1 input channel).
862 * If mode has not been set, then select J-STEREO
863 */
864 if (gfp->mode == NOT_SET) {
865 gfp->mode = JOINT_STEREO;
866 }
867
868 cfg->mode = gfp->mode;
869
870
871 /* apply user driven high pass filter */
872 if (cfg->highpassfreq > 0) {
873 cfg->highpass1 = 2. * cfg->highpassfreq;
874
875 if (gfp->highpasswidth >= 0)
876 cfg->highpass2 = 2. * (cfg->highpassfreq + gfp->highpasswidth);
877 else /* 0% above on default */
878 cfg->highpass2 = (1 + 0.00) * 2. * cfg->highpassfreq;
879
880 cfg->highpass1 /= cfg->samplerate_out;
881 cfg->highpass2 /= cfg->samplerate_out;
882 }
883 else {
884 cfg->highpass1 = 0;
885 cfg->highpass2 = 0;
886 }
887 /* apply user driven low pass filter */
888 cfg->lowpass1 = 0;
889 cfg->lowpass2 = 0;
890 if (cfg->lowpassfreq > 0 && cfg->lowpassfreq < (cfg->samplerate_out / 2) ) {
891 cfg->lowpass2 = 2. * cfg->lowpassfreq;
892 if (gfp->lowpasswidth >= 0) {
893 cfg->lowpass1 = 2. * (cfg->lowpassfreq - gfp->lowpasswidth);
894 if (cfg->lowpass1 < 0) /* has to be >= 0 */
895 cfg->lowpass1 = 0;
896 }
897 else { /* 0% below on default */
898 cfg->lowpass1 = (1 - 0.00) * 2. * cfg->lowpassfreq;
899 }
900 cfg->lowpass1 /= cfg->samplerate_out;
901 cfg->lowpass2 /= cfg->samplerate_out;
902 }
903
904
905
906
907 /**********************************************************************/
908 /* compute info needed for polyphase filter (filter type==0, default) */
909 /**********************************************************************/
910 lame_init_params_ppflt(gfc);
911
912
913 /*******************************************************
914 * samplerate and bitrate index
915 *******************************************************/
916 cfg->samplerate_index = SmpFrqIndex(cfg->samplerate_out, &cfg->version);
917 assert(cfg->samplerate_index >= 0);
918
919 if (cfg->vbr == vbr_off) {
920 if (cfg->free_format) {
921 gfc->ov_enc.bitrate_index = 0;
922 }
923 else {
924 gfp->brate = FindNearestBitrate(gfp->brate, cfg->version, cfg->samplerate_out);
925 gfc->ov_enc.bitrate_index = BitrateIndex(gfp->brate, cfg->version, cfg->samplerate_out);
926 if (gfc->ov_enc.bitrate_index <= 0) {
927 /* This never happens, because of preceding FindNearestBitrate!
928 * But, set a sane value, just in case
929 */
930 assert(0);
931 gfc->ov_enc.bitrate_index = 8;
932 }
933 }
934 }
935 else {
936 gfc->ov_enc.bitrate_index = 1;
937 }
938
939 init_bit_stream_w(gfc);
940
941 j = cfg->samplerate_index + (3 * cfg->version) + 6 * (cfg->samplerate_out < 16000);
942 for (i = 0; i < SBMAX_l + 1; i++)
943 gfc->scalefac_band.l[i] = sfBandIndex[j].l[i];
944
945 for (i = 0; i < PSFB21 + 1; i++) {
946 int const size = (gfc->scalefac_band.l[22] - gfc->scalefac_band.l[21]) / PSFB21;
947 int const start = gfc->scalefac_band.l[21] + i * size;
948 gfc->scalefac_band.psfb21[i] = start;
949 }
950 gfc->scalefac_band.psfb21[PSFB21] = 576;
951
952 for (i = 0; i < SBMAX_s + 1; i++)
953 gfc->scalefac_band.s[i] = sfBandIndex[j].s[i];
954
955 for (i = 0; i < PSFB12 + 1; i++) {
956 int const size = (gfc->scalefac_band.s[13] - gfc->scalefac_band.s[12]) / PSFB12;
957 int const start = gfc->scalefac_band.s[12] + i * size;
958 gfc->scalefac_band.psfb12[i] = start;
959 }
960 gfc->scalefac_band.psfb12[PSFB12] = 192;
961
962 /* determine the mean bitrate for main data */
963 if (cfg->mode_gr == 2) /* MPEG 1 */
964 cfg->sideinfo_len = (cfg->channels_out == 1) ? 4 + 17 : 4 + 32;
965 else /* MPEG 2 */
966 cfg->sideinfo_len = (cfg->channels_out == 1) ? 4 + 9 : 4 + 17;
967
968 if (cfg->error_protection)
969 cfg->sideinfo_len += 2;
970
971 {
972 int k;
973
974 for (k = 0; k < 19; k++)
975 gfc->sv_enc.pefirbuf[k] = 700 * cfg->mode_gr * cfg->channels_out;
976
977 if (gfp->ATHtype == -1)
978 gfp->ATHtype = 4;
979 }
980
981 assert(gfp->VBR_q <= 9);
982 assert(gfp->VBR_q >= 0);
983
984 switch (cfg->vbr) {
985
986 case vbr_mt:
987 case vbr_mtrh:{
988 if (gfp->strict_ISO < 0) {
989 gfp->strict_ISO = MDB_MAXIMUM;
990 }
991 if (gfp->useTemporal < 0) {
992 gfp->useTemporal = 0; /* off by default for this VBR mode */
993 }
994
995 (void) apply_preset(gfp, 500 - (gfp->VBR_q * 10), 0);
996 /* The newer VBR code supports only a limited
997 subset of quality levels:
998 9-5=5 are the same, uses x^3/4 quantization
999 4-0=0 are the same 5 plus best huffman divide code
1000 */
1001 if (gfp->quality < 0)
1002 gfp->quality = LAME_DEFAULT_QUALITY;
1003 if (gfp->quality < 5)
1004 gfp->quality = 0;
1005 if (gfp->quality > 7)
1006 gfp->quality = 7;
1007
1008 /* sfb21 extra only with MPEG-1 at higher sampling rates
1009 */
1010 if (gfp->experimentalY)
1011 gfc->sv_qnt.sfb21_extra = 0;
1012 else
1013 gfc->sv_qnt.sfb21_extra = (cfg->samplerate_out > 44000);
1014
1015 break;
1016
1017 }
1018 case vbr_rh:{
1019
1020 (void) apply_preset(gfp, 500 - (gfp->VBR_q * 10), 0);
1021
1022 /* sfb21 extra only with MPEG-1 at higher sampling rates
1023 */
1024 if (gfp->experimentalY)
1025 gfc->sv_qnt.sfb21_extra = 0;
1026 else
1027 gfc->sv_qnt.sfb21_extra = (cfg->samplerate_out > 44000);
1028
1029 /* VBR needs at least the output of GPSYCHO,
1030 * so we have to garantee that by setting a minimum
1031 * quality level, actually level 6 does it.
1032 * down to level 6
1033 */
1034 if (gfp->quality > 6)
1035 gfp->quality = 6;
1036
1037
1038 if (gfp->quality < 0)
1039 gfp->quality = LAME_DEFAULT_QUALITY;
1040
1041 break;
1042 }
1043
1044 default: /* cbr/abr */ {
1045
1046 /* no sfb21 extra with CBR code
1047 */
1048 gfc->sv_qnt.sfb21_extra = 0;
1049
1050 if (gfp->quality < 0)
1051 gfp->quality = LAME_DEFAULT_QUALITY;
1052
1053
1054 if (cfg->vbr == vbr_off)
1055 (void) lame_set_VBR_mean_bitrate_kbps(gfp, gfp->brate);
1056 /* second, set parameters depending on bitrate */
1057 (void) apply_preset(gfp, gfp->VBR_mean_bitrate_kbps, 0);
1058 gfp->VBR = cfg->vbr;
1059
1060 break;
1061 }
1062 }
1063
1064 /*initialize default values common for all modes */
1065
1066 gfc->sv_qnt.mask_adjust = gfp->maskingadjust;
1067 gfc->sv_qnt.mask_adjust_short = gfp->maskingadjust_short;
1068
1069 /* just another daily changing developer switch */
1070 if (gfp->tune) {
1071 gfc->sv_qnt.mask_adjust += gfp->tune_value_a;
1072 gfc->sv_qnt.mask_adjust_short += gfp->tune_value_a;
1073 }
1074
1075
1076 if (cfg->vbr != vbr_off) { /* choose a min/max bitrate for VBR */
1077 /* if the user didn't specify VBR_max_bitrate: */
1078 cfg->vbr_min_bitrate_index = 1; /* default: allow 8 kbps (MPEG-2) or 32 kbps (MPEG-1) */
1079 cfg->vbr_max_bitrate_index = 14; /* default: allow 160 kbps (MPEG-2) or 320 kbps (MPEG-1) */
1080 if (cfg->samplerate_out < 16000)
1081 cfg->vbr_max_bitrate_index = 8; /* default: allow 64 kbps (MPEG-2.5) */
1082 if (gfp->VBR_min_bitrate_kbps) {
1083 gfp->VBR_min_bitrate_kbps =
1084 FindNearestBitrate(gfp->VBR_min_bitrate_kbps, cfg->version, cfg->samplerate_out);
1085 cfg->vbr_min_bitrate_index =
1086 BitrateIndex(gfp->VBR_min_bitrate_kbps, cfg->version, cfg->samplerate_out);
1087 if (cfg->vbr_min_bitrate_index < 0) {
1088 /* This never happens, because of preceding FindNearestBitrate!
1089 * But, set a sane value, just in case
1090 */
1091 assert(0);
1092 cfg->vbr_min_bitrate_index = 1;
1093 }
1094 }
1095 if (gfp->VBR_max_bitrate_kbps) {
1096 gfp->VBR_max_bitrate_kbps =
1097 FindNearestBitrate(gfp->VBR_max_bitrate_kbps, cfg->version, cfg->samplerate_out);
1098 cfg->vbr_max_bitrate_index =
1099 BitrateIndex(gfp->VBR_max_bitrate_kbps, cfg->version, cfg->samplerate_out);
1100 if (cfg->vbr_max_bitrate_index < 0) {
1101 /* This never happens, because of preceding FindNearestBitrate!
1102 * But, set a sane value, just in case
1103 */
1104 assert(0);
1105 cfg->vbr_max_bitrate_index = cfg->samplerate_out < 16000 ? 8 : 14;
1106 }
1107 }
1108 gfp->VBR_min_bitrate_kbps = bitrate_table[cfg->version][cfg->vbr_min_bitrate_index];
1109 gfp->VBR_max_bitrate_kbps = bitrate_table[cfg->version][cfg->vbr_max_bitrate_index];
1110 gfp->VBR_mean_bitrate_kbps =
1111 Min(bitrate_table[cfg->version][cfg->vbr_max_bitrate_index],
1112 gfp->VBR_mean_bitrate_kbps);
1113 gfp->VBR_mean_bitrate_kbps =
1114 Max(bitrate_table[cfg->version][cfg->vbr_min_bitrate_index],
1115 gfp->VBR_mean_bitrate_kbps);
1116 }
1117
1118 cfg->preset = gfp->preset;
1119 cfg->write_lame_tag = gfp->write_lame_tag;
1120 gfc->sv_qnt.substep_shaping = gfp->substep_shaping;
1121 cfg->noise_shaping = gfp->noise_shaping;
1122 cfg->subblock_gain = gfp->subblock_gain;
1123 cfg->use_best_huffman = gfp->use_best_huffman;
1124 cfg->avg_bitrate = gfp->brate;
1125 cfg->vbr_avg_bitrate_kbps = gfp->VBR_mean_bitrate_kbps;
1126 cfg->compression_ratio = gfp->compression_ratio;
1127
1128 /* initialize internal qval settings */
1129 lame_init_qval(gfp);
1130
1131
1132 /* automatic ATH adjustment on
1133 */
1134 if (gfp->athaa_type < 0)
1135 gfc->ATH->use_adjust = 3;
1136 else
1137 gfc->ATH->use_adjust = gfp->athaa_type;
1138
1139
1140 /* initialize internal adaptive ATH settings -jd */
1141 gfc->ATH->aa_sensitivity_p = pow(10.0, gfp->athaa_sensitivity / -10.0);
1142
1143
1144 if (gfp->short_blocks == short_block_not_set) {
1145 gfp->short_blocks = short_block_allowed;
1146 }
1147
1148 /*Note Jan/2003: Many hardware decoders cannot handle short blocks in regular
1149 stereo mode unless they are coupled (same type in both channels)
1150 it is a rare event (1 frame per min. or so) that LAME would use
1151 uncoupled short blocks, so lets turn them off until we decide
1152 how to handle this. No other encoders allow uncoupled short blocks,
1153 even though it is in the standard. */
1154 /* rh 20040217: coupling makes no sense for mono and dual-mono streams
1155 */
1156 if (gfp->short_blocks == short_block_allowed
1157 && (cfg->mode == JOINT_STEREO || cfg->mode == STEREO)) {
1158 gfp->short_blocks = short_block_coupled;
1159 }
1160
1161 cfg->short_blocks = gfp->short_blocks;
1162
1163
1164 if (lame_get_quant_comp(gfp) < 0)
1165 (void) lame_set_quant_comp(gfp, 1);
1166 if (lame_get_quant_comp_short(gfp) < 0)
1167 (void) lame_set_quant_comp_short(gfp, 0);
1168
1169 if (lame_get_msfix(gfp) < 0)
1170 lame_set_msfix(gfp, 0);
1171
1172 /* select psychoacoustic model */
1173 (void) lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | 1);
1174
1175 if (gfp->ATHtype < 0)
1176 gfp->ATHtype = 4;
1177
1178 if (gfp->ATHcurve < 0)
1179 gfp->ATHcurve = 4;
1180
1181 if (gfp->interChRatio < 0)
1182 gfp->interChRatio = 0;
1183
1184 if (gfp->useTemporal < 0)
1185 gfp->useTemporal = 1; /* on by default */
1186
1187
1188 cfg->interChRatio = gfp->interChRatio;
1189 cfg->msfix = gfp->msfix;
1190 cfg->ATH_offset_db = 0-gfp->ATH_lower_db;
1191 cfg->ATH_offset_factor = powf(10.f, cfg->ATH_offset_db * 0.1f);
1192 cfg->ATHcurve = gfp->ATHcurve;
1193 cfg->ATHtype = gfp->ATHtype;
1194 cfg->ATHonly = gfp->ATHonly;
1195 cfg->ATHshort = gfp->ATHshort;
1196 cfg->noATH = gfp->noATH;
1197
1198 cfg->quant_comp = gfp->quant_comp;
1199 cfg->quant_comp_short = gfp->quant_comp_short;
1200
1201 cfg->use_temporal_masking_effect = gfp->useTemporal;
1202 if (cfg->mode == JOINT_STEREO) {
1203 cfg->use_safe_joint_stereo = gfp->exp_nspsytune & 2;
1204 }
1205 else {
1206 cfg->use_safe_joint_stereo = 0;
1207 }
1208 {
1209 cfg->adjust_bass_db = (gfp->exp_nspsytune >> 2) & 63;
1210 if (cfg->adjust_bass_db >= 32.f)
1211 cfg->adjust_bass_db -= 64.f;
1212 cfg->adjust_bass_db *= 0.25f;
1213
1214 cfg->adjust_alto_db = (gfp->exp_nspsytune >> 8) & 63;
1215 if (cfg->adjust_alto_db >= 32.f)
1216 cfg->adjust_alto_db -= 64.f;
1217 cfg->adjust_alto_db *= 0.25f;
1218
1219 cfg->adjust_treble_db = (gfp->exp_nspsytune >> 14) & 63;
1220 if (cfg->adjust_treble_db >= 32.f)
1221 cfg->adjust_treble_db -= 64.f;
1222 cfg->adjust_treble_db *= 0.25f;
1223
1224 /* to be compatible with Naoki's original code, the next 6 bits
1225 * define only the amount of changing treble for sfb21 */
1226 cfg->adjust_sfb21_db = (gfp->exp_nspsytune >> 20) & 63;
1227 if (cfg->adjust_sfb21_db >= 32.f)
1228 cfg->adjust_sfb21_db -= 64.f;
1229 cfg->adjust_sfb21_db *= 0.25f;
1230 cfg->adjust_sfb21_db += cfg->adjust_treble_db;
1231 }
1232
1233 /* Setting up the PCM input data transform matrix, to apply
1234 * user defined re-scaling, and or two-to-one channel downmix.
1235 */
1236 {
1237 FLOAT m[2][2] = { {1.0f, 0.0f}, {0.0f, 1.0f} };
1238
1239 /* user selected scaling of the samples */
1240 m[0][0] *= gfp->scale;
1241 m[0][1] *= gfp->scale;
1242 m[1][0] *= gfp->scale;
1243 m[1][1] *= gfp->scale;
1244 /* user selected scaling of the channel 0 (left) samples */
1245 m[0][0] *= gfp->scale_left;
1246 m[0][1] *= gfp->scale_left;
1247 /* user selected scaling of the channel 1 (right) samples */
1248 m[1][0] *= gfp->scale_right;
1249 m[1][1] *= gfp->scale_right;
1250 /* Downsample to Mono if 2 channels in and 1 channel out */
1251 if (cfg->channels_in == 2 && cfg->channels_out == 1) {
1252 m[0][0] = 0.5f * (m[0][0] + m[1][0]);
1253 m[0][1] = 0.5f * (m[0][1] + m[1][1]);
1254 m[1][0] = 0;
1255 m[1][1] = 0;
1256 }
1257 cfg->pcm_transform[0][0] = m[0][0];
1258 cfg->pcm_transform[0][1] = m[0][1];
1259 cfg->pcm_transform[1][0] = m[1][0];
1260 cfg->pcm_transform[1][1] = m[1][1];
1261 }
1262
1263 /* padding method as described in
1264 * "MPEG-Layer3 / Bitstream Syntax and Decoding"
1265 * by Martin Sieler, Ralph Sperschneider
1266 *
1267 * note: there is no padding for the very first frame
1268 *
1269 * Robert Hegemann 2000-06-22
1270 */
1271 gfc->sv_enc.slot_lag = gfc->sv_enc.frac_SpF = 0;
1272 if (cfg->vbr == vbr_off)
1273 gfc->sv_enc.slot_lag = gfc->sv_enc.frac_SpF
1274 = ((cfg->version + 1) * 72000L * cfg->avg_bitrate) % cfg->samplerate_out;
1275
1276 (void) lame_init_bitstream(gfp);
1277
1278 iteration_init(gfc);
1279 (void) psymodel_init(gfp);
1280
1281 cfg->buffer_constraint = get_max_frame_buffer_size_by_constraint(cfg, gfp->strict_ISO);
1282
1283
1284 cfg->findReplayGain = gfp->findReplayGain;
1285 cfg->decode_on_the_fly = gfp->decode_on_the_fly;
1286
1287 if (cfg->decode_on_the_fly)
1288 cfg->findPeakSample = 1;
1289
1290 if (cfg->findReplayGain) {
1291 if (InitGainAnalysis(gfc->sv_rpg.rgdata, cfg->samplerate_out) == INIT_GAIN_ANALYSIS_ERROR) {
1292 /* Actually this never happens, our samplerates are the ones RG accepts!
1293 * But just in case, turn RG off
1294 */
1295 assert(0);
1296 cfg->findReplayGain = 0;
1297 }
1298 }
1299
1300 #ifdef DECODE_ON_THE_FLY
1301 if (cfg->decode_on_the_fly && !gfp->decode_only) {
1302 if (gfc->hip) {
1303 hip_decode_exit(gfc->hip);
1304 }
1305 gfc->hip = hip_decode_init();
1306 /* report functions */
1307 hip_set_errorf(gfc->hip, gfp->report.errorf);
1308 hip_set_debugf(gfc->hip, gfp->report.debugf);
1309 hip_set_msgf(gfc->hip, gfp->report.msgf);
1310 }
1311 #endif
1312 /* updating lame internal flags finished successful */
1313 gfc->lame_init_params_successful = 1;
1314 return 0;
1315 }
1316
1317 static void
concatSep(char * dest,char const * sep,char const * str)1318 concatSep(char* dest, char const* sep, char const* str)
1319 {
1320 if (*dest != 0) strcat(dest, sep);
1321 strcat(dest, str);
1322 }
1323
1324 /*
1325 * print_config
1326 *
1327 * Prints some selected information about the coding parameters via
1328 * the macro command MSGF(), which is currently mapped to lame_errorf
1329 * (reports via a error function?), which is a printf-like function
1330 * for <stderr>.
1331 */
1332
1333 void
lame_print_config(const lame_global_flags * gfp)1334 lame_print_config(const lame_global_flags * gfp)
1335 {
1336 lame_internal_flags const *const gfc = gfp->internal_flags;
1337 SessionConfig_t const *const cfg = &gfc->cfg;
1338 double const out_samplerate = cfg->samplerate_out;
1339 double const in_samplerate = cfg->samplerate_in;
1340
1341 MSGF(gfc, "LAME %s %s (%s)\n", get_lame_version(), get_lame_os_bitness(), get_lame_url());
1342
1343 #if (LAME_ALPHA_VERSION)
1344 MSGF(gfc, "warning: alpha versions should be used for testing only\n");
1345 #endif
1346 if (gfc->CPU_features.MMX
1347 || gfc->CPU_features.AMD_3DNow || gfc->CPU_features.SSE || gfc->CPU_features.SSE2) {
1348 char text[256] = { 0 };
1349 int fft_asm_used = 0;
1350 #ifdef HAVE_NASM
1351 if (gfc->CPU_features.AMD_3DNow) {
1352 fft_asm_used = 1;
1353 }
1354 else if (gfc->CPU_features.SSE) {
1355 fft_asm_used = 2;
1356 }
1357 #else
1358 # if defined( HAVE_XMMINTRIN_H ) && defined( MIN_ARCH_SSE )
1359 {
1360 fft_asm_used = 3;
1361 }
1362 # endif
1363 #endif
1364 if (gfc->CPU_features.MMX) {
1365 #ifdef MMX_choose_table
1366 concatSep(text, ", ", "MMX (ASM used)");
1367 #else
1368 concatSep(text, ", ", "MMX");
1369 #endif
1370 }
1371 if (gfc->CPU_features.AMD_3DNow) {
1372 concatSep(text, ", ", (fft_asm_used == 1) ? "3DNow! (ASM used)" : "3DNow!");
1373 }
1374 if (gfc->CPU_features.SSE) {
1375 #if defined(HAVE_XMMINTRIN_H)
1376 concatSep(text, ", ", "SSE (ASM used)");
1377 #else
1378 concatSep(text, ", ", (fft_asm_used == 2) ? "SSE (ASM used)" : "SSE");
1379 #endif
1380 }
1381 if (gfc->CPU_features.SSE2) {
1382 concatSep(text, ", ", (fft_asm_used == 3) ? "SSE2 (ASM used)" : "SSE2");
1383 }
1384 MSGF(gfc, "CPU features: %s\n", text);
1385 }
1386
1387 if (cfg->channels_in == 2 && cfg->channels_out == 1 /* mono */ ) {
1388 MSGF(gfc, "Autoconverting from stereo to mono. Setting encoding to mono mode.\n");
1389 }
1390
1391 if (isResamplingNecessary(cfg)) {
1392 MSGF(gfc, "Resampling: input %g kHz output %g kHz\n",
1393 1.e-3 * in_samplerate, 1.e-3 * out_samplerate);
1394 }
1395
1396 if (cfg->highpass2 > 0.)
1397 MSGF(gfc,
1398 "Using polyphase highpass filter, transition band: %5.0f Hz - %5.0f Hz\n",
1399 0.5 * cfg->highpass1 * out_samplerate, 0.5 * cfg->highpass2 * out_samplerate);
1400 if (0. < cfg->lowpass1 || 0. < cfg->lowpass2) {
1401 MSGF(gfc,
1402 "Using polyphase lowpass filter, transition band: %5.0f Hz - %5.0f Hz\n",
1403 0.5 * cfg->lowpass1 * out_samplerate, 0.5 * cfg->lowpass2 * out_samplerate);
1404 }
1405 else {
1406 MSGF(gfc, "polyphase lowpass filter disabled\n");
1407 }
1408
1409 if (cfg->free_format) {
1410 MSGF(gfc, "Warning: many decoders cannot handle free format bitstreams\n");
1411 if (cfg->avg_bitrate > 320) {
1412 MSGF(gfc,
1413 "Warning: many decoders cannot handle free format bitrates >320 kbps (see documentation)\n");
1414 }
1415 }
1416 }
1417
1418
1419 /** rh:
1420 * some pretty printing is very welcome at this point!
1421 * so, if someone is willing to do so, please do it!
1422 * add more, if you see more...
1423 */
1424 void
lame_print_internals(const lame_global_flags * gfp)1425 lame_print_internals(const lame_global_flags * gfp)
1426 {
1427 lame_internal_flags const *const gfc = gfp->internal_flags;
1428 SessionConfig_t const *const cfg = &gfc->cfg;
1429 const char *pc = "";
1430
1431 /* compiler/processor optimizations, operational, etc.
1432 */
1433 MSGF(gfc, "\nmisc:\n\n");
1434
1435 MSGF(gfc, "\tscaling: %g\n", gfp->scale);
1436 MSGF(gfc, "\tch0 (left) scaling: %g\n", gfp->scale_left);
1437 MSGF(gfc, "\tch1 (right) scaling: %g\n", gfp->scale_right);
1438 switch (cfg->use_best_huffman) {
1439 default:
1440 pc = "normal";
1441 break;
1442 case 1:
1443 pc = "best (outside loop)";
1444 break;
1445 case 2:
1446 pc = "best (inside loop, slow)";
1447 break;
1448 }
1449 MSGF(gfc, "\thuffman search: %s\n", pc);
1450 MSGF(gfc, "\texperimental Y=%d\n", gfp->experimentalY);
1451 MSGF(gfc, "\t...\n");
1452
1453 /* everything controlling the stream format
1454 */
1455 MSGF(gfc, "\nstream format:\n\n");
1456 switch (cfg->version) {
1457 case 0:
1458 pc = "2.5";
1459 break;
1460 case 1:
1461 pc = "1";
1462 break;
1463 case 2:
1464 pc = "2";
1465 break;
1466 default:
1467 pc = "?";
1468 break;
1469 }
1470 MSGF(gfc, "\tMPEG-%s Layer 3\n", pc);
1471 switch (cfg->mode) {
1472 case JOINT_STEREO:
1473 pc = "joint stereo";
1474 break;
1475 case STEREO:
1476 pc = "stereo";
1477 break;
1478 case DUAL_CHANNEL:
1479 pc = "dual channel";
1480 break;
1481 case MONO:
1482 pc = "mono";
1483 break;
1484 case NOT_SET:
1485 pc = "not set (error)";
1486 break;
1487 default:
1488 pc = "unknown (error)";
1489 break;
1490 }
1491 MSGF(gfc, "\t%d channel - %s\n", cfg->channels_out, pc);
1492
1493 switch (cfg->vbr) {
1494 case vbr_off:
1495 pc = "off";
1496 break;
1497 default:
1498 pc = "all";
1499 break;
1500 }
1501 MSGF(gfc, "\tpadding: %s\n", pc);
1502
1503 if (vbr_default == cfg->vbr)
1504 pc = "(default)";
1505 else if (cfg->free_format)
1506 pc = "(free format)";
1507 else
1508 pc = "";
1509 switch (cfg->vbr) {
1510 case vbr_off:
1511 MSGF(gfc, "\tconstant bitrate - CBR %s\n", pc);
1512 break;
1513 case vbr_abr:
1514 MSGF(gfc, "\tvariable bitrate - ABR %s\n", pc);
1515 break;
1516 case vbr_rh:
1517 MSGF(gfc, "\tvariable bitrate - VBR rh %s\n", pc);
1518 break;
1519 case vbr_mt:
1520 MSGF(gfc, "\tvariable bitrate - VBR mt %s\n", pc);
1521 break;
1522 case vbr_mtrh:
1523 MSGF(gfc, "\tvariable bitrate - VBR mtrh %s\n", pc);
1524 break;
1525 default:
1526 MSGF(gfc, "\t ?? oops, some new one ?? \n");
1527 break;
1528 }
1529 if (cfg->write_lame_tag)
1530 MSGF(gfc, "\tusing LAME Tag\n");
1531 MSGF(gfc, "\t...\n");
1532
1533 /* everything controlling psychoacoustic settings, like ATH, etc.
1534 */
1535 MSGF(gfc, "\npsychoacoustic:\n\n");
1536
1537 switch (cfg->short_blocks) {
1538 default:
1539 case short_block_not_set:
1540 pc = "?";
1541 break;
1542 case short_block_allowed:
1543 pc = "allowed";
1544 break;
1545 case short_block_coupled:
1546 pc = "channel coupled";
1547 break;
1548 case short_block_dispensed:
1549 pc = "dispensed";
1550 break;
1551 case short_block_forced:
1552 pc = "forced";
1553 break;
1554 }
1555 MSGF(gfc, "\tusing short blocks: %s\n", pc);
1556 MSGF(gfc, "\tsubblock gain: %d\n", cfg->subblock_gain);
1557 MSGF(gfc, "\tadjust masking: %g dB\n", gfc->sv_qnt.mask_adjust);
1558 MSGF(gfc, "\tadjust masking short: %g dB\n", gfc->sv_qnt.mask_adjust_short);
1559 MSGF(gfc, "\tquantization comparison: %d\n", cfg->quant_comp);
1560 MSGF(gfc, "\t ^ comparison short blocks: %d\n", cfg->quant_comp_short);
1561 MSGF(gfc, "\tnoise shaping: %d\n", cfg->noise_shaping);
1562 MSGF(gfc, "\t ^ amplification: %d\n", cfg->noise_shaping_amp);
1563 MSGF(gfc, "\t ^ stopping: %d\n", cfg->noise_shaping_stop);
1564
1565 pc = "using";
1566 if (cfg->ATHshort)
1567 pc = "the only masking for short blocks";
1568 if (cfg->ATHonly)
1569 pc = "the only masking";
1570 if (cfg->noATH)
1571 pc = "not used";
1572 MSGF(gfc, "\tATH: %s\n", pc);
1573 MSGF(gfc, "\t ^ type: %d\n", cfg->ATHtype);
1574 MSGF(gfc, "\t ^ shape: %g%s\n", cfg->ATHcurve, " (only for type 4)");
1575 MSGF(gfc, "\t ^ level adjustement: %g dB\n", cfg->ATH_offset_db);
1576 MSGF(gfc, "\t ^ adjust type: %d\n", gfc->ATH->use_adjust);
1577 MSGF(gfc, "\t ^ adjust sensitivity power: %f\n", gfc->ATH->aa_sensitivity_p);
1578
1579 MSGF(gfc, "\texperimental psy tunings by Naoki Shibata\n");
1580 MSGF(gfc, "\t adjust masking bass=%g dB, alto=%g dB, treble=%g dB, sfb21=%g dB\n",
1581 10 * log10(gfc->sv_qnt.longfact[0]),
1582 10 * log10(gfc->sv_qnt.longfact[7]),
1583 10 * log10(gfc->sv_qnt.longfact[14]), 10 * log10(gfc->sv_qnt.longfact[21]));
1584
1585 pc = cfg->use_temporal_masking_effect ? "yes" : "no";
1586 MSGF(gfc, "\tusing temporal masking effect: %s\n", pc);
1587 MSGF(gfc, "\tinterchannel masking ratio: %g\n", cfg->interChRatio);
1588 MSGF(gfc, "\t...\n");
1589
1590 /* that's all ?
1591 */
1592 MSGF(gfc, "\n");
1593 return;
1594 }
1595
1596
1597 static void
save_gain_values(lame_internal_flags * gfc)1598 save_gain_values(lame_internal_flags * gfc)
1599 {
1600 SessionConfig_t const *const cfg = &gfc->cfg;
1601 RpgStateVar_t const *const rsv = &gfc->sv_rpg;
1602 RpgResult_t *const rov = &gfc->ov_rpg;
1603 /* save the ReplayGain value */
1604 if (cfg->findReplayGain) {
1605 FLOAT const RadioGain = (FLOAT) GetTitleGain(rsv->rgdata);
1606 if (NEQ(RadioGain, GAIN_NOT_ENOUGH_SAMPLES)) {
1607 rov->RadioGain = (int) floor(RadioGain * 10.0 + 0.5); /* round to nearest */
1608 }
1609 else {
1610 rov->RadioGain = 0;
1611 }
1612 }
1613
1614 /* find the gain and scale change required for no clipping */
1615 if (cfg->findPeakSample) {
1616 rov->noclipGainChange = (int) ceil(log10(rov->PeakSample / 32767.0) * 20.0 * 10.0); /* round up */
1617
1618 if (rov->noclipGainChange > 0) { /* clipping occurs */
1619 rov->noclipScale = floor((32767.0f / rov->PeakSample) * 100.0f) / 100.0f; /* round down */
1620 }
1621 else /* no clipping */
1622 rov->noclipScale = -1.0f;
1623 }
1624 }
1625
1626
1627
1628 static int
update_inbuffer_size(lame_internal_flags * gfc,const int nsamples)1629 update_inbuffer_size(lame_internal_flags * gfc, const int nsamples)
1630 {
1631 EncStateVar_t *const esv = &gfc->sv_enc;
1632 if (esv->in_buffer_0 == 0 || esv->in_buffer_nsamples < nsamples) {
1633 if (esv->in_buffer_0) {
1634 free(esv->in_buffer_0);
1635 }
1636 if (esv->in_buffer_1) {
1637 free(esv->in_buffer_1);
1638 }
1639 esv->in_buffer_0 = lame_calloc(sample_t, nsamples);
1640 esv->in_buffer_1 = lame_calloc(sample_t, nsamples);
1641 esv->in_buffer_nsamples = nsamples;
1642 }
1643 if (esv->in_buffer_0 == NULL || esv->in_buffer_1 == NULL) {
1644 if (esv->in_buffer_0) {
1645 free(esv->in_buffer_0);
1646 }
1647 if (esv->in_buffer_1) {
1648 free(esv->in_buffer_1);
1649 }
1650 esv->in_buffer_0 = 0;
1651 esv->in_buffer_1 = 0;
1652 esv->in_buffer_nsamples = 0;
1653 ERRORF(gfc, "Error: can't allocate in_buffer buffer\n");
1654 return -2;
1655 }
1656 return 0;
1657 }
1658
1659
1660 static int
calcNeeded(SessionConfig_t const * cfg)1661 calcNeeded(SessionConfig_t const * cfg)
1662 {
1663 int mf_needed;
1664 int pcm_samples_per_frame = 576 * cfg->mode_gr;
1665
1666 /* some sanity checks */
1667 #if ENCDELAY < MDCTDELAY
1668 # error ENCDELAY is less than MDCTDELAY, see encoder.h
1669 #endif
1670 #if FFTOFFSET > BLKSIZE
1671 # error FFTOFFSET is greater than BLKSIZE, see encoder.h
1672 #endif
1673
1674 mf_needed = BLKSIZE + pcm_samples_per_frame - FFTOFFSET; /* amount needed for FFT */
1675 /*mf_needed = Max(mf_needed, 286 + 576 * (1 + gfc->mode_gr)); */
1676 mf_needed = Max(mf_needed, 512 + pcm_samples_per_frame - 32);
1677
1678 assert(MFSIZE >= mf_needed);
1679
1680 return mf_needed;
1681 }
1682
1683
1684 /*
1685 * THE MAIN LAME ENCODING INTERFACE
1686 * mt 3/00
1687 *
1688 * input pcm data, output (maybe) mp3 frames.
1689 * This routine handles all buffering, resampling and filtering for you.
1690 * The required mp3buffer_size can be computed from num_samples,
1691 * samplerate and encoding rate, but here is a worst case estimate:
1692 *
1693 * mp3buffer_size in bytes = 1.25*num_samples + 7200
1694 *
1695 * return code = number of bytes output in mp3buffer. can be 0
1696 *
1697 * NOTE: this routine uses LAME's internal PCM data representation,
1698 * 'sample_t'. It should not be used by any application.
1699 * applications should use lame_encode_buffer(),
1700 * lame_encode_buffer_float()
1701 * lame_encode_buffer_int()
1702 * etc... depending on what type of data they are working with.
1703 */
1704 static int
lame_encode_buffer_sample_t(lame_internal_flags * gfc,int nsamples,unsigned char * mp3buf,const int mp3buf_size)1705 lame_encode_buffer_sample_t(lame_internal_flags * gfc,
1706 int nsamples, unsigned char *mp3buf, const int mp3buf_size)
1707 {
1708 SessionConfig_t const *const cfg = &gfc->cfg;
1709 EncStateVar_t *const esv = &gfc->sv_enc;
1710 int pcm_samples_per_frame = 576 * cfg->mode_gr;
1711 int mp3size = 0, ret, i, ch, mf_needed;
1712 int mp3out;
1713 sample_t *mfbuf[2];
1714 sample_t *in_buffer[2];
1715
1716 if (gfc->class_id != LAME_ID)
1717 return -3;
1718
1719 if (nsamples == 0)
1720 return 0;
1721
1722 /* copy out any tags that may have been written into bitstream */
1723 { /* if user specifed buffer size = 0, dont check size */
1724 int const buf_size = mp3buf_size == 0 ? INT_MAX : mp3buf_size;
1725 mp3out = copy_buffer(gfc, mp3buf, buf_size, 0);
1726 }
1727 if (mp3out < 0)
1728 return mp3out; /* not enough buffer space */
1729 mp3buf += mp3out;
1730 mp3size += mp3out;
1731
1732 in_buffer[0] = esv->in_buffer_0;
1733 in_buffer[1] = esv->in_buffer_1;
1734
1735 mf_needed = calcNeeded(cfg);
1736
1737 mfbuf[0] = esv->mfbuf[0];
1738 mfbuf[1] = esv->mfbuf[1];
1739
1740 while (nsamples > 0) {
1741 sample_t const *in_buffer_ptr[2];
1742 int n_in = 0; /* number of input samples processed with fill_buffer */
1743 int n_out = 0; /* number of samples output with fill_buffer */
1744 /* n_in <> n_out if we are resampling */
1745
1746 in_buffer_ptr[0] = in_buffer[0];
1747 in_buffer_ptr[1] = in_buffer[1];
1748 /* copy in new samples into mfbuf, with resampling */
1749 fill_buffer(gfc, mfbuf, &in_buffer_ptr[0], nsamples, &n_in, &n_out);
1750
1751 /* compute ReplayGain of resampled input if requested */
1752 if (cfg->findReplayGain && !cfg->decode_on_the_fly)
1753 if (AnalyzeSamples
1754 (gfc->sv_rpg.rgdata, &mfbuf[0][esv->mf_size], &mfbuf[1][esv->mf_size], n_out,
1755 cfg->channels_out) == GAIN_ANALYSIS_ERROR)
1756 return -6;
1757
1758
1759
1760 /* update in_buffer counters */
1761 nsamples -= n_in;
1762 in_buffer[0] += n_in;
1763 if (cfg->channels_out == 2)
1764 in_buffer[1] += n_in;
1765
1766 /* update mfbuf[] counters */
1767 esv->mf_size += n_out;
1768 assert(esv->mf_size <= MFSIZE);
1769
1770 /* lame_encode_flush may have set gfc->mf_sample_to_encode to 0
1771 * so we have to reinitialize it here when that happened.
1772 */
1773 if (esv->mf_samples_to_encode < 1) {
1774 esv->mf_samples_to_encode = ENCDELAY + POSTDELAY;
1775 }
1776 esv->mf_samples_to_encode += n_out;
1777
1778
1779 if (esv->mf_size >= mf_needed) {
1780 /* encode the frame. */
1781 /* mp3buf = pointer to current location in buffer */
1782 /* mp3buf_size = size of original mp3 output buffer */
1783 /* = 0 if we should not worry about the */
1784 /* buffer size because calling program is */
1785 /* to lazy to compute it */
1786 /* mp3size = size of data written to buffer so far */
1787 /* mp3buf_size-mp3size = amount of space avalable */
1788
1789 int buf_size = mp3buf_size - mp3size;
1790 if (mp3buf_size == 0)
1791 buf_size = INT_MAX;
1792
1793 ret = lame_encode_mp3_frame(gfc, mfbuf[0], mfbuf[1], mp3buf, buf_size);
1794
1795 if (ret < 0)
1796 return ret;
1797 mp3buf += ret;
1798 mp3size += ret;
1799
1800 /* shift out old samples */
1801 esv->mf_size -= pcm_samples_per_frame;
1802 esv->mf_samples_to_encode -= pcm_samples_per_frame;
1803 for (ch = 0; ch < cfg->channels_out; ch++)
1804 for (i = 0; i < esv->mf_size; i++)
1805 mfbuf[ch][i] = mfbuf[ch][i + pcm_samples_per_frame];
1806 }
1807 }
1808 assert(nsamples == 0);
1809
1810 return mp3size;
1811 }
1812
1813 enum PCMSampleType
1814 { pcm_short_type
1815 , pcm_int_type
1816 , pcm_long_type
1817 , pcm_float_type
1818 , pcm_double_type
1819 };
1820
1821 static void
lame_copy_inbuffer(lame_internal_flags * gfc,void const * l,void const * r,int nsamples,enum PCMSampleType pcm_type,int jump,FLOAT s)1822 lame_copy_inbuffer(lame_internal_flags* gfc,
1823 void const* l, void const* r, int nsamples,
1824 enum PCMSampleType pcm_type, int jump, FLOAT s)
1825 {
1826 SessionConfig_t const *const cfg = &gfc->cfg;
1827 EncStateVar_t *const esv = &gfc->sv_enc;
1828 sample_t* ib0 = esv->in_buffer_0;
1829 sample_t* ib1 = esv->in_buffer_1;
1830 FLOAT m[2][2];
1831
1832 /* Apply user defined re-scaling */
1833 m[0][0] = s * cfg->pcm_transform[0][0];
1834 m[0][1] = s * cfg->pcm_transform[0][1];
1835 m[1][0] = s * cfg->pcm_transform[1][0];
1836 m[1][1] = s * cfg->pcm_transform[1][1];
1837
1838 /* make a copy of input buffer, changing type to sample_t */
1839 #define COPY_AND_TRANSFORM(T) \
1840 { \
1841 T const *bl = l, *br = r; \
1842 int i; \
1843 for (i = 0; i < nsamples; i++) { \
1844 sample_t const xl = *bl; \
1845 sample_t const xr = *br; \
1846 sample_t const u = xl * m[0][0] + xr * m[0][1]; \
1847 sample_t const v = xl * m[1][0] + xr * m[1][1]; \
1848 ib0[i] = u; \
1849 ib1[i] = v; \
1850 bl += jump; \
1851 br += jump; \
1852 } \
1853 }
1854 switch ( pcm_type ) {
1855 case pcm_short_type:
1856 COPY_AND_TRANSFORM(short int);
1857 break;
1858 case pcm_int_type:
1859 COPY_AND_TRANSFORM(int);
1860 break;
1861 case pcm_long_type:
1862 COPY_AND_TRANSFORM(long int);
1863 break;
1864 case pcm_float_type:
1865 COPY_AND_TRANSFORM(float);
1866 break;
1867 case pcm_double_type:
1868 COPY_AND_TRANSFORM(double);
1869 break;
1870 }
1871 }
1872
1873
1874 static int
lame_encode_buffer_template(lame_global_flags * gfp,void const * buffer_l,void const * buffer_r,const int nsamples,unsigned char * mp3buf,const int mp3buf_size,enum PCMSampleType pcm_type,int aa,FLOAT norm)1875 lame_encode_buffer_template(lame_global_flags * gfp,
1876 void const* buffer_l, void const* buffer_r, const int nsamples,
1877 unsigned char *mp3buf, const int mp3buf_size, enum PCMSampleType pcm_type, int aa, FLOAT norm)
1878 {
1879 if (is_lame_global_flags_valid(gfp)) {
1880 lame_internal_flags *const gfc = gfp->internal_flags;
1881 if (is_lame_internal_flags_valid(gfc)) {
1882 SessionConfig_t const *const cfg = &gfc->cfg;
1883
1884 if (nsamples == 0)
1885 return 0;
1886
1887 if (update_inbuffer_size(gfc, nsamples) != 0) {
1888 return -2;
1889 }
1890 /* make a copy of input buffer, changing type to sample_t */
1891 if (cfg->channels_in > 1) {
1892 if (buffer_l == 0 || buffer_r == 0) {
1893 return 0;
1894 }
1895 lame_copy_inbuffer(gfc, buffer_l, buffer_r, nsamples, pcm_type, aa, norm);
1896 }
1897 else {
1898 if (buffer_l == 0) {
1899 return 0;
1900 }
1901 lame_copy_inbuffer(gfc, buffer_l, buffer_l, nsamples, pcm_type, aa, norm);
1902 }
1903
1904 return lame_encode_buffer_sample_t(gfc, nsamples, mp3buf, mp3buf_size);
1905 }
1906 }
1907 return -3;
1908 }
1909
1910 int
lame_encode_buffer(lame_global_flags * gfp,const short int pcm_l[],const short int pcm_r[],const int nsamples,unsigned char * mp3buf,const int mp3buf_size)1911 lame_encode_buffer(lame_global_flags * gfp,
1912 const short int pcm_l[], const short int pcm_r[], const int nsamples,
1913 unsigned char *mp3buf, const int mp3buf_size)
1914 {
1915 return lame_encode_buffer_template(gfp, pcm_l, pcm_r, nsamples, mp3buf, mp3buf_size, pcm_short_type, 1, 1.0);
1916 }
1917
1918
1919 int
lame_encode_buffer_float(lame_global_flags * gfp,const float pcm_l[],const float pcm_r[],const int nsamples,unsigned char * mp3buf,const int mp3buf_size)1920 lame_encode_buffer_float(lame_global_flags * gfp,
1921 const float pcm_l[], const float pcm_r[], const int nsamples,
1922 unsigned char *mp3buf, const int mp3buf_size)
1923 {
1924 /* input is assumed to be normalized to +/- 32768 for full scale */
1925 return lame_encode_buffer_template(gfp, pcm_l, pcm_r, nsamples, mp3buf, mp3buf_size, pcm_float_type, 1, 1.0);
1926 }
1927
1928
1929 int
lame_encode_buffer_ieee_float(lame_t gfp,const float pcm_l[],const float pcm_r[],const int nsamples,unsigned char * mp3buf,const int mp3buf_size)1930 lame_encode_buffer_ieee_float(lame_t gfp,
1931 const float pcm_l[], const float pcm_r[], const int nsamples,
1932 unsigned char *mp3buf, const int mp3buf_size)
1933 {
1934 /* input is assumed to be normalized to +/- 1.0 for full scale */
1935 return lame_encode_buffer_template(gfp, pcm_l, pcm_r, nsamples, mp3buf, mp3buf_size, pcm_float_type, 1, 32767.0);
1936 }
1937
1938
1939 int
lame_encode_buffer_interleaved_ieee_float(lame_t gfp,const float pcm[],const int nsamples,unsigned char * mp3buf,const int mp3buf_size)1940 lame_encode_buffer_interleaved_ieee_float(lame_t gfp,
1941 const float pcm[], const int nsamples,
1942 unsigned char *mp3buf, const int mp3buf_size)
1943 {
1944 /* input is assumed to be normalized to +/- 1.0 for full scale */
1945 return lame_encode_buffer_template(gfp, pcm, pcm+1, nsamples, mp3buf, mp3buf_size, pcm_float_type, 2, 32767.0);
1946 }
1947
1948
1949 int
lame_encode_buffer_ieee_double(lame_t gfp,const double pcm_l[],const double pcm_r[],const int nsamples,unsigned char * mp3buf,const int mp3buf_size)1950 lame_encode_buffer_ieee_double(lame_t gfp,
1951 const double pcm_l[], const double pcm_r[], const int nsamples,
1952 unsigned char *mp3buf, const int mp3buf_size)
1953 {
1954 /* input is assumed to be normalized to +/- 1.0 for full scale */
1955 return lame_encode_buffer_template(gfp, pcm_l, pcm_r, nsamples, mp3buf, mp3buf_size, pcm_double_type, 1, 32767.0);
1956 }
1957
1958
1959 int
lame_encode_buffer_interleaved_ieee_double(lame_t gfp,const double pcm[],const int nsamples,unsigned char * mp3buf,const int mp3buf_size)1960 lame_encode_buffer_interleaved_ieee_double(lame_t gfp,
1961 const double pcm[], const int nsamples,
1962 unsigned char *mp3buf, const int mp3buf_size)
1963 {
1964 /* input is assumed to be normalized to +/- 1.0 for full scale */
1965 return lame_encode_buffer_template(gfp, pcm, pcm+1, nsamples, mp3buf, mp3buf_size, pcm_double_type, 2, 32767.0);
1966 }
1967
1968
1969 int
lame_encode_buffer_int(lame_global_flags * gfp,const int pcm_l[],const int pcm_r[],const int nsamples,unsigned char * mp3buf,const int mp3buf_size)1970 lame_encode_buffer_int(lame_global_flags * gfp,
1971 const int pcm_l[], const int pcm_r[], const int nsamples,
1972 unsigned char *mp3buf, const int mp3buf_size)
1973 {
1974 /* input is assumed to be normalized to +/- MAX_INT for full scale */
1975 FLOAT const norm = (1.0 / (1L << (8 * sizeof(int) - 16)));
1976 return lame_encode_buffer_template(gfp, pcm_l, pcm_r, nsamples, mp3buf, mp3buf_size, pcm_int_type, 1, norm);
1977 }
1978
1979
1980 int
lame_encode_buffer_long2(lame_global_flags * gfp,const long pcm_l[],const long pcm_r[],const int nsamples,unsigned char * mp3buf,const int mp3buf_size)1981 lame_encode_buffer_long2(lame_global_flags * gfp,
1982 const long pcm_l[], const long pcm_r[], const int nsamples,
1983 unsigned char *mp3buf, const int mp3buf_size)
1984 {
1985 /* input is assumed to be normalized to +/- MAX_LONG for full scale */
1986 FLOAT const norm = (1.0 / (1L << (8 * sizeof(long) - 16)));
1987 return lame_encode_buffer_template(gfp, pcm_l, pcm_r, nsamples, mp3buf, mp3buf_size, pcm_long_type, 1, norm);
1988 }
1989
1990
1991 int
lame_encode_buffer_long(lame_global_flags * gfp,const long pcm_l[],const long pcm_r[],const int nsamples,unsigned char * mp3buf,const int mp3buf_size)1992 lame_encode_buffer_long(lame_global_flags * gfp,
1993 const long pcm_l[], const long pcm_r[], const int nsamples,
1994 unsigned char *mp3buf, const int mp3buf_size)
1995 {
1996 /* input is assumed to be normalized to +/- 32768 for full scale */
1997 return lame_encode_buffer_template(gfp, pcm_l, pcm_r, nsamples, mp3buf, mp3buf_size, pcm_long_type, 1, 1.0);
1998 }
1999
2000
2001
2002 int
lame_encode_buffer_interleaved(lame_global_flags * gfp,short int pcm[],int nsamples,unsigned char * mp3buf,int mp3buf_size)2003 lame_encode_buffer_interleaved(lame_global_flags * gfp,
2004 short int pcm[], int nsamples,
2005 unsigned char *mp3buf, int mp3buf_size)
2006 {
2007 /* input is assumed to be normalized to +/- MAX_SHORT for full scale */
2008 return lame_encode_buffer_template(gfp, pcm, pcm+1, nsamples, mp3buf, mp3buf_size, pcm_short_type, 2, 1.0);
2009 }
2010
2011
2012 int
lame_encode_buffer_interleaved_int(lame_t gfp,const int pcm[],const int nsamples,unsigned char * mp3buf,const int mp3buf_size)2013 lame_encode_buffer_interleaved_int(lame_t gfp,
2014 const int pcm[], const int nsamples,
2015 unsigned char *mp3buf, const int mp3buf_size)
2016 {
2017 /* input is assumed to be normalized to +/- MAX(int) for full scale */
2018 FLOAT const norm = (1.0 / (1L << (8 * sizeof(int)-16)));
2019 return lame_encode_buffer_template(gfp, pcm, pcm + 1, nsamples, mp3buf, mp3buf_size, pcm_int_type, 2, norm);
2020 }
2021
2022
2023
2024
2025 /*****************************************************************
2026 Flush mp3 buffer, pad with ancillary data so last frame is complete.
2027 Reset reservoir size to 0
2028 but keep all PCM samples and MDCT data in memory
2029 This option is used to break a large file into several mp3 files
2030 that when concatenated together will decode with no gaps
2031 Because we set the reservoir=0, they will also decode seperately
2032 with no errors.
2033 *********************************************************************/
2034 int
lame_encode_flush_nogap(lame_global_flags * gfp,unsigned char * mp3buffer,int mp3buffer_size)2035 lame_encode_flush_nogap(lame_global_flags * gfp, unsigned char *mp3buffer, int mp3buffer_size)
2036 {
2037 int rc = -3;
2038 if (is_lame_global_flags_valid(gfp)) {
2039 lame_internal_flags *const gfc = gfp->internal_flags;
2040 if (is_lame_internal_flags_valid(gfc)) {
2041 flush_bitstream(gfc);
2042 /* if user specifed buffer size = 0, dont check size */
2043 if (mp3buffer_size == 0)
2044 mp3buffer_size = INT_MAX;
2045 rc = copy_buffer(gfc, mp3buffer, mp3buffer_size, 1);
2046 save_gain_values(gfc);
2047 }
2048 }
2049 return rc;
2050 }
2051
2052
2053 /* called by lame_init_params. You can also call this after flush_nogap
2054 if you want to write new id3v2 and Xing VBR tags into the bitstream */
2055 int
lame_init_bitstream(lame_global_flags * gfp)2056 lame_init_bitstream(lame_global_flags * gfp)
2057 {
2058 if (is_lame_global_flags_valid(gfp)) {
2059 lame_internal_flags *const gfc = gfp->internal_flags;
2060 if (gfc != 0) {
2061 gfc->ov_enc.frame_number = 0;
2062
2063 if (gfp->write_id3tag_automatic) {
2064 (void) id3tag_write_v2(gfp);
2065 }
2066 /* initialize histogram data optionally used by frontend */
2067 memset(gfc->ov_enc.bitrate_channelmode_hist, 0,
2068 sizeof(gfc->ov_enc.bitrate_channelmode_hist));
2069 memset(gfc->ov_enc.bitrate_blocktype_hist, 0,
2070 sizeof(gfc->ov_enc.bitrate_blocktype_hist));
2071
2072 gfc->ov_rpg.PeakSample = 0.0;
2073
2074 /* Write initial VBR Header to bitstream and init VBR data */
2075 if (gfc->cfg.write_lame_tag)
2076 (void) InitVbrTag(gfp);
2077
2078
2079 return 0;
2080 }
2081 }
2082 return -3;
2083 }
2084
2085
2086 /*****************************************************************/
2087 /* flush internal PCM sample buffers, then mp3 buffers */
2088 /* then write id3 v1 tags into bitstream. */
2089 /*****************************************************************/
2090
2091 int
lame_encode_flush(lame_global_flags * gfp,unsigned char * mp3buffer,int mp3buffer_size)2092 lame_encode_flush(lame_global_flags * gfp, unsigned char *mp3buffer, int mp3buffer_size)
2093 {
2094 lame_internal_flags *gfc;
2095 SessionConfig_t const *cfg;
2096 EncStateVar_t *esv;
2097 short int buffer[2][1152];
2098 int imp3 = 0, mp3count, mp3buffer_size_remaining;
2099
2100 /* we always add POSTDELAY=288 padding to make sure granule with real
2101 * data can be complety decoded (because of 50% overlap with next granule */
2102 int end_padding;
2103 int frames_left;
2104 int samples_to_encode;
2105 int pcm_samples_per_frame;
2106 int mf_needed;
2107 int is_resampling_necessary;
2108 double resample_ratio = 1;
2109
2110 if (!is_lame_global_flags_valid(gfp)) {
2111 return -3;
2112 }
2113 gfc = gfp->internal_flags;
2114 if (!is_lame_internal_flags_valid(gfc)) {
2115 return -3;
2116 }
2117 cfg = &gfc->cfg;
2118 esv = &gfc->sv_enc;
2119
2120 /* Was flush already called? */
2121 if (esv->mf_samples_to_encode < 1) {
2122 return 0;
2123 }
2124 pcm_samples_per_frame = 576 * cfg->mode_gr;
2125 mf_needed = calcNeeded(cfg);
2126
2127 samples_to_encode = esv->mf_samples_to_encode - POSTDELAY;
2128
2129 memset(buffer, 0, sizeof(buffer));
2130 mp3count = 0;
2131
2132 is_resampling_necessary = isResamplingNecessary(cfg);
2133 if (is_resampling_necessary) {
2134 resample_ratio = (double)cfg->samplerate_in / (double)cfg->samplerate_out;
2135 /* delay due to resampling; needs to be fixed, if resampling code gets changed */
2136 samples_to_encode += 16. / resample_ratio;
2137 }
2138 end_padding = pcm_samples_per_frame - (samples_to_encode % pcm_samples_per_frame);
2139 if (end_padding < 576)
2140 end_padding += pcm_samples_per_frame;
2141 gfc->ov_enc.encoder_padding = end_padding;
2142
2143 frames_left = (samples_to_encode + end_padding) / pcm_samples_per_frame;
2144 while (frames_left > 0 && imp3 >= 0) {
2145 int const frame_num = gfc->ov_enc.frame_number;
2146 int bunch = mf_needed - esv->mf_size;
2147
2148 bunch *= resample_ratio;
2149 if (bunch > 1152) bunch = 1152;
2150 if (bunch < 1) bunch = 1;
2151
2152 mp3buffer_size_remaining = mp3buffer_size - mp3count;
2153
2154 /* if user specifed buffer size = 0, dont check size */
2155 if (mp3buffer_size == 0)
2156 mp3buffer_size_remaining = 0;
2157
2158 /* send in a frame of 0 padding until all internal sample buffers
2159 * are flushed
2160 */
2161 imp3 = lame_encode_buffer(gfp, buffer[0], buffer[1], bunch,
2162 mp3buffer, mp3buffer_size_remaining);
2163
2164 mp3buffer += imp3;
2165 mp3count += imp3;
2166 { /* even a single pcm sample can produce several frames!
2167 * for example: 1 Hz input file resampled to 8 kHz mpeg2.5
2168 */
2169 int const new_frames = gfc->ov_enc.frame_number - frame_num;
2170 if (new_frames > 0)
2171 frames_left -= new_frames;
2172 }
2173 }
2174 /* Set esv->mf_samples_to_encode to 0, so we may detect
2175 * and break loops calling it more than once in a row.
2176 */
2177 esv->mf_samples_to_encode = 0;
2178
2179 if (imp3 < 0) {
2180 /* some type of fatal error */
2181 return imp3;
2182 }
2183
2184 mp3buffer_size_remaining = mp3buffer_size - mp3count;
2185 /* if user specifed buffer size = 0, dont check size */
2186 if (mp3buffer_size == 0)
2187 mp3buffer_size_remaining = INT_MAX;
2188
2189 /* mp3 related stuff. bit buffer might still contain some mp3 data */
2190 flush_bitstream(gfc);
2191 imp3 = copy_buffer(gfc, mp3buffer, mp3buffer_size_remaining, 1);
2192 save_gain_values(gfc);
2193 if (imp3 < 0) {
2194 /* some type of fatal error */
2195 return imp3;
2196 }
2197 mp3buffer += imp3;
2198 mp3count += imp3;
2199 mp3buffer_size_remaining = mp3buffer_size - mp3count;
2200 /* if user specifed buffer size = 0, dont check size */
2201 if (mp3buffer_size == 0)
2202 mp3buffer_size_remaining = INT_MAX;
2203
2204 if (gfp->write_id3tag_automatic) {
2205 /* write a id3 tag to the bitstream */
2206 (void) id3tag_write_v1(gfp);
2207
2208 imp3 = copy_buffer(gfc, mp3buffer, mp3buffer_size_remaining, 0);
2209
2210 if (imp3 < 0) {
2211 return imp3;
2212 }
2213 mp3count += imp3;
2214 }
2215 #if 0
2216 {
2217 int const ed = gfc->ov_enc.encoder_delay;
2218 int const ep = gfc->ov_enc.encoder_padding;
2219 int const ns = (gfc->ov_enc.frame_number * pcm_samples_per_frame) - (ed + ep);
2220 double duration = ns;
2221 duration /= cfg->samplerate_out;
2222 MSGF(gfc, "frames=%d\n", gfc->ov_enc.frame_number);
2223 MSGF(gfc, "pcm_samples_per_frame=%d\n", pcm_samples_per_frame);
2224 MSGF(gfc, "encoder delay=%d\n", ed);
2225 MSGF(gfc, "encoder padding=%d\n", ep);
2226 MSGF(gfc, "sample count=%d (%g)\n", ns, cfg->samplerate_in * duration);
2227 MSGF(gfc, "duration=%g sec\n", duration);
2228 }
2229 #endif
2230 return mp3count;
2231 }
2232
2233 /***********************************************************************
2234 *
2235 * lame_close ()
2236 *
2237 * frees internal buffers
2238 *
2239 ***********************************************************************/
2240
2241 int
lame_close(lame_global_flags * gfp)2242 lame_close(lame_global_flags * gfp)
2243 {
2244 int ret = 0;
2245 if (gfp && gfp->class_id == LAME_ID) {
2246 lame_internal_flags *const gfc = gfp->internal_flags;
2247 gfp->class_id = 0;
2248 if (NULL == gfc || gfc->class_id != LAME_ID) {
2249 ret = -3;
2250 }
2251 if (NULL != gfc) {
2252 gfc->lame_init_params_successful = 0;
2253 gfc->class_id = 0;
2254 /* this routine will free all malloc'd data in gfc, and then free gfc: */
2255 freegfc(gfc);
2256 gfp->internal_flags = NULL;
2257 }
2258 if (gfp->lame_allocated_gfp) {
2259 gfp->lame_allocated_gfp = 0;
2260 free(gfp);
2261 }
2262 }
2263 return ret;
2264 }
2265
2266 /*****************************************************************/
2267 /* flush internal mp3 buffers, and free internal buffers */
2268 /*****************************************************************/
2269 #if DEPRECATED_OR_OBSOLETE_CODE_REMOVED
2270 int CDECL
2271 lame_encode_finish(lame_global_flags * gfp, unsigned char *mp3buffer, int mp3buffer_size);
2272 #else
2273 #endif
2274
2275 int
lame_encode_finish(lame_global_flags * gfp,unsigned char * mp3buffer,int mp3buffer_size)2276 lame_encode_finish(lame_global_flags * gfp, unsigned char *mp3buffer, int mp3buffer_size)
2277 {
2278 int const ret = lame_encode_flush(gfp, mp3buffer, mp3buffer_size);
2279
2280 (void) lame_close(gfp);
2281
2282 return ret;
2283 }
2284
2285 /*****************************************************************/
2286 /* write VBR Xing header, and ID3 version 1 tag, if asked for */
2287 /*****************************************************************/
2288 void lame_mp3_tags_fid(lame_global_flags * gfp, FILE * fpStream);
2289
2290 void
lame_mp3_tags_fid(lame_global_flags * gfp,FILE * fpStream)2291 lame_mp3_tags_fid(lame_global_flags * gfp, FILE * fpStream)
2292 {
2293 lame_internal_flags *gfc;
2294 SessionConfig_t const *cfg;
2295 if (!is_lame_global_flags_valid(gfp)) {
2296 return;
2297 }
2298 gfc = gfp->internal_flags;
2299 if (!is_lame_internal_flags_valid(gfc)) {
2300 return;
2301 }
2302 cfg = &gfc->cfg;
2303 if (!cfg->write_lame_tag) {
2304 return;
2305 }
2306 /* Write Xing header again */
2307 if (fpStream && !fseek(fpStream, 0, SEEK_SET)) {
2308 int rc = PutVbrTag(gfp, fpStream);
2309 switch (rc) {
2310 default:
2311 /* OK */
2312 break;
2313
2314 case -1:
2315 ERRORF(gfc, "Error: could not update LAME tag.\n");
2316 break;
2317
2318 case -2:
2319 ERRORF(gfc, "Error: could not update LAME tag, file not seekable.\n");
2320 break;
2321
2322 case -3:
2323 ERRORF(gfc, "Error: could not update LAME tag, file not readable.\n");
2324 break;
2325 }
2326 }
2327 }
2328
2329
2330 static int
lame_init_internal_flags(lame_internal_flags * gfc)2331 lame_init_internal_flags(lame_internal_flags* gfc)
2332 {
2333 if (NULL == gfc)
2334 return -1;
2335
2336 gfc->cfg.vbr_min_bitrate_index = 1; /* not 0 ????? */
2337 gfc->cfg.vbr_max_bitrate_index = 13; /* not 14 ????? */
2338 gfc->cfg.decode_on_the_fly = 0;
2339 gfc->cfg.findReplayGain = 0;
2340 gfc->cfg.findPeakSample = 0;
2341
2342 gfc->sv_qnt.OldValue[0] = 180;
2343 gfc->sv_qnt.OldValue[1] = 180;
2344 gfc->sv_qnt.CurrentStep[0] = 4;
2345 gfc->sv_qnt.CurrentStep[1] = 4;
2346 gfc->sv_qnt.masking_lower = 1;
2347
2348 /* The reason for
2349 * int mf_samples_to_encode = ENCDELAY + POSTDELAY;
2350 * ENCDELAY = internal encoder delay. And then we have to add POSTDELAY=288
2351 * because of the 50% MDCT overlap. A 576 MDCT granule decodes to
2352 * 1152 samples. To synthesize the 576 samples centered under this granule
2353 * we need the previous granule for the first 288 samples (no problem), and
2354 * the next granule for the next 288 samples (not possible if this is last
2355 * granule). So we need to pad with 288 samples to make sure we can
2356 * encode the 576 samples we are interested in.
2357 */
2358 gfc->sv_enc.mf_samples_to_encode = ENCDELAY + POSTDELAY;
2359 gfc->sv_enc.mf_size = ENCDELAY - MDCTDELAY; /* we pad input with this many 0's */
2360 gfc->ov_enc.encoder_padding = 0;
2361 gfc->ov_enc.encoder_delay = ENCDELAY;
2362
2363 gfc->ov_rpg.RadioGain = 0;
2364 gfc->ov_rpg.noclipGainChange = 0;
2365 gfc->ov_rpg.noclipScale = -1.0;
2366
2367 gfc->ATH = lame_calloc(ATH_t, 1);
2368 if (NULL == gfc->ATH)
2369 return -2; /* maybe error codes should be enumerated in lame.h ?? */
2370
2371 gfc->sv_rpg.rgdata = lame_calloc(replaygain_t, 1);
2372 if (NULL == gfc->sv_rpg.rgdata) {
2373 return -2;
2374 }
2375 return 0;
2376 }
2377
2378 /* initialize mp3 encoder */
2379 #if DEPRECATED_OR_OBSOLETE_CODE_REMOVED
2380 static
2381 #else
2382 #endif
2383 int
lame_init_old(lame_global_flags * gfp)2384 lame_init_old(lame_global_flags * gfp)
2385 {
2386 disable_FPE(); /* disable floating point exceptions */
2387
2388 memset(gfp, 0, sizeof(lame_global_flags));
2389
2390 gfp->class_id = LAME_ID;
2391
2392 /* Global flags. set defaults here for non-zero values */
2393 /* see lame.h for description */
2394 /* set integer values to -1 to mean that LAME will compute the
2395 * best value, UNLESS the calling program as set it
2396 * (and the value is no longer -1)
2397 */
2398 gfp->strict_ISO = MDB_MAXIMUM;
2399
2400 gfp->mode = NOT_SET;
2401 gfp->original = 1;
2402 gfp->samplerate_in = 44100;
2403 gfp->num_channels = 2;
2404 gfp->num_samples = MAX_U_32_NUM;
2405
2406 gfp->write_lame_tag = 1;
2407 gfp->quality = -1;
2408 gfp->short_blocks = short_block_not_set;
2409 gfp->subblock_gain = -1;
2410
2411 gfp->lowpassfreq = 0;
2412 gfp->highpassfreq = 0;
2413 gfp->lowpasswidth = -1;
2414 gfp->highpasswidth = -1;
2415
2416 gfp->VBR = vbr_off;
2417 gfp->VBR_q = 4;
2418 gfp->VBR_mean_bitrate_kbps = 128;
2419 gfp->VBR_min_bitrate_kbps = 0;
2420 gfp->VBR_max_bitrate_kbps = 0;
2421 gfp->VBR_hard_min = 0;
2422
2423 gfp->quant_comp = -1;
2424 gfp->quant_comp_short = -1;
2425
2426 gfp->msfix = -1;
2427
2428 gfp->attackthre = -1;
2429 gfp->attackthre_s = -1;
2430
2431 gfp->scale = 1;
2432 gfp->scale_left = 1;
2433 gfp->scale_right = 1;
2434
2435 gfp->ATHcurve = -1;
2436 gfp->ATHtype = -1; /* default = -1 = set in lame_init_params */
2437 /* 2 = equal loudness curve */
2438 gfp->athaa_sensitivity = 0.0; /* no offset */
2439 gfp->athaa_type = -1;
2440 gfp->useTemporal = -1;
2441 gfp->interChRatio = -1;
2442
2443 gfp->findReplayGain = 0;
2444 gfp->decode_on_the_fly = 0;
2445
2446 gfp->asm_optimizations.mmx = 1;
2447 gfp->asm_optimizations.amd3dnow = 1;
2448 gfp->asm_optimizations.sse = 1;
2449
2450 gfp->preset = 0;
2451
2452 gfp->write_id3tag_automatic = 1;
2453
2454 gfp->report.debugf = &lame_report_def;
2455 gfp->report.errorf = &lame_report_def;
2456 gfp->report.msgf = &lame_report_def;
2457
2458 gfp->internal_flags = lame_calloc(lame_internal_flags, 1);
2459
2460 if (lame_init_internal_flags(gfp->internal_flags) < 0) {
2461 freegfc(gfp->internal_flags);
2462 gfp->internal_flags = 0;
2463 return -1;
2464 }
2465 return 0;
2466 }
2467
2468
2469 lame_global_flags *
lame_init(void)2470 lame_init(void)
2471 {
2472 lame_global_flags *gfp;
2473 int ret;
2474
2475 init_log_table();
2476
2477 gfp = lame_calloc(lame_global_flags, 1);
2478 if (gfp == NULL)
2479 return NULL;
2480
2481 ret = lame_init_old(gfp);
2482 if (ret != 0) {
2483 free(gfp);
2484 return NULL;
2485 }
2486
2487 gfp->lame_allocated_gfp = 1;
2488 return gfp;
2489 }
2490
2491
2492 /***********************************************************************
2493 *
2494 * some simple statistics
2495 *
2496 * Robert Hegemann 2000-10-11
2497 *
2498 ***********************************************************************/
2499
2500 /* histogram of used bitrate indexes:
2501 * One has to weight them to calculate the average bitrate in kbps
2502 *
2503 * bitrate indices:
2504 * there are 14 possible bitrate indices, 0 has the special meaning
2505 * "free format" which is not possible to mix with VBR and 15 is forbidden
2506 * anyway.
2507 *
2508 * stereo modes:
2509 * 0: LR number of left-right encoded frames
2510 * 1: LR-I number of left-right and intensity encoded frames
2511 * 2: MS number of mid-side encoded frames
2512 * 3: MS-I number of mid-side and intensity encoded frames
2513 *
2514 * 4: number of encoded frames
2515 *
2516 */
2517
2518 void
lame_bitrate_kbps(const lame_global_flags * gfp,int bitrate_kbps[14])2519 lame_bitrate_kbps(const lame_global_flags * gfp, int bitrate_kbps[14])
2520 {
2521 if (is_lame_global_flags_valid(gfp)) {
2522 lame_internal_flags const *const gfc = gfp->internal_flags;
2523 if (is_lame_internal_flags_valid(gfc)) {
2524 SessionConfig_t const *const cfg = &gfc->cfg;
2525 int i;
2526 if (cfg->free_format) {
2527 for (i = 0; i < 14; i++)
2528 bitrate_kbps[i] = -1;
2529 bitrate_kbps[0] = cfg->avg_bitrate;
2530 }
2531 else {
2532 for (i = 0; i < 14; i++)
2533 bitrate_kbps[i] = bitrate_table[cfg->version][i + 1];
2534 }
2535 }
2536 }
2537 }
2538
2539
2540 void
lame_bitrate_hist(const lame_global_flags * gfp,int bitrate_count[14])2541 lame_bitrate_hist(const lame_global_flags * gfp, int bitrate_count[14])
2542 {
2543 if (is_lame_global_flags_valid(gfp)) {
2544 lame_internal_flags const *const gfc = gfp->internal_flags;
2545 if (is_lame_internal_flags_valid(gfc)) {
2546 SessionConfig_t const *const cfg = &gfc->cfg;
2547 EncResult_t const *const eov = &gfc->ov_enc;
2548 int i;
2549
2550 if (cfg->free_format) {
2551 for (i = 0; i < 14; i++) {
2552 bitrate_count[i] = 0;
2553 }
2554 bitrate_count[0] = eov->bitrate_channelmode_hist[0][4];
2555 }
2556 else {
2557 for (i = 0; i < 14; i++) {
2558 bitrate_count[i] = eov->bitrate_channelmode_hist[i + 1][4];
2559 }
2560 }
2561 }
2562 }
2563 }
2564
2565
2566 void
lame_stereo_mode_hist(const lame_global_flags * gfp,int stmode_count[4])2567 lame_stereo_mode_hist(const lame_global_flags * gfp, int stmode_count[4])
2568 {
2569 if (is_lame_global_flags_valid(gfp)) {
2570 lame_internal_flags const *const gfc = gfp->internal_flags;
2571 if (is_lame_internal_flags_valid(gfc)) {
2572 EncResult_t const *const eov = &gfc->ov_enc;
2573 int i;
2574
2575 for (i = 0; i < 4; i++) {
2576 stmode_count[i] = eov->bitrate_channelmode_hist[15][i];
2577 }
2578 }
2579 }
2580 }
2581
2582
2583
2584 void
lame_bitrate_stereo_mode_hist(const lame_global_flags * gfp,int bitrate_stmode_count[14][4])2585 lame_bitrate_stereo_mode_hist(const lame_global_flags * gfp, int bitrate_stmode_count[14][4])
2586 {
2587 if (is_lame_global_flags_valid(gfp)) {
2588 lame_internal_flags const *const gfc = gfp->internal_flags;
2589 if (is_lame_internal_flags_valid(gfc)) {
2590 SessionConfig_t const *const cfg = &gfc->cfg;
2591 EncResult_t const *const eov = &gfc->ov_enc;
2592 int i;
2593 int j;
2594
2595 if (cfg->free_format) {
2596 for (j = 0; j < 14; j++)
2597 for (i = 0; i < 4; i++) {
2598 bitrate_stmode_count[j][i] = 0;
2599 }
2600 for (i = 0; i < 4; i++) {
2601 bitrate_stmode_count[0][i] = eov->bitrate_channelmode_hist[0][i];
2602 }
2603 }
2604 else {
2605 for (j = 0; j < 14; j++) {
2606 for (i = 0; i < 4; i++) {
2607 bitrate_stmode_count[j][i] = eov->bitrate_channelmode_hist[j + 1][i];
2608 }
2609 }
2610 }
2611 }
2612 }
2613 }
2614
2615
2616 void
lame_block_type_hist(const lame_global_flags * gfp,int btype_count[6])2617 lame_block_type_hist(const lame_global_flags * gfp, int btype_count[6])
2618 {
2619 if (is_lame_global_flags_valid(gfp)) {
2620 lame_internal_flags const *const gfc = gfp->internal_flags;
2621 if (is_lame_internal_flags_valid(gfc)) {
2622 EncResult_t const *const eov = &gfc->ov_enc;
2623 int i;
2624
2625 for (i = 0; i < 6; ++i) {
2626 btype_count[i] = eov->bitrate_blocktype_hist[15][i];
2627 }
2628 }
2629 }
2630 }
2631
2632
2633
2634 void
lame_bitrate_block_type_hist(const lame_global_flags * gfp,int bitrate_btype_count[14][6])2635 lame_bitrate_block_type_hist(const lame_global_flags * gfp, int bitrate_btype_count[14][6])
2636 {
2637 if (is_lame_global_flags_valid(gfp)) {
2638 lame_internal_flags const *const gfc = gfp->internal_flags;
2639 if (is_lame_internal_flags_valid(gfc)) {
2640 SessionConfig_t const *const cfg = &gfc->cfg;
2641 EncResult_t const *const eov = &gfc->ov_enc;
2642 int i, j;
2643
2644 if (cfg->free_format) {
2645 for (j = 0; j < 14; ++j) {
2646 for (i = 0; i < 6; ++i) {
2647 bitrate_btype_count[j][i] = 0;
2648 }
2649 }
2650 for (i = 0; i < 6; ++i) {
2651 bitrate_btype_count[0][i] = eov->bitrate_blocktype_hist[0][i];
2652 }
2653 }
2654 else {
2655 for (j = 0; j < 14; ++j) {
2656 for (i = 0; i < 6; ++i) {
2657 bitrate_btype_count[j][i] = eov->bitrate_blocktype_hist[j + 1][i];
2658 }
2659 }
2660 }
2661 }
2662 }
2663 }
2664
2665 /* end of lame.c */
2666