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