Debian Codegraph @

libmp3lame/lame.c

raw ◀ ▶
1 Debian patch touch this file
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-2019 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$ */
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
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
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
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
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
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
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
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
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
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 /* VBR and ABR don't give good results with noise shaping
1054 * settings used in quality levels below 4.
1055 */
1056 if (gfp->quality < 4)
1057 gfp->quality = 4;
1058
1059 if (cfg->vbr == vbr_off)
1060 (void) lame_set_VBR_mean_bitrate_kbps(gfp, gfp->brate);
1061 /* second, set parameters depending on bitrate */
1062 (void) apply_preset(gfp, gfp->VBR_mean_bitrate_kbps, 0);
1063 gfp->VBR = cfg->vbr;
1064
1065 break;
1066 }
1067 }
1068
1069 /*initialize default values common for all modes */
1070
1071 gfc->sv_qnt.mask_adjust = gfp->maskingadjust;
1072 gfc->sv_qnt.mask_adjust_short = gfp->maskingadjust_short;
1073
1074 /* just another daily changing developer switch */
1075 if (gfp->tune) {
1076 gfc->sv_qnt.mask_adjust += gfp->tune_value_a;
1077 gfc->sv_qnt.mask_adjust_short += gfp->tune_value_a;
1078 }
1079
1080
1081 if (cfg->vbr != vbr_off) { /* choose a min/max bitrate for VBR */
1082 /* if the user didn't specify VBR_max_bitrate: */
1083 cfg->vbr_min_bitrate_index = 1; /* default: allow 8 kbps (MPEG-2) or 32 kbps (MPEG-1) */
1084 cfg->vbr_max_bitrate_index = 14; /* default: allow 160 kbps (MPEG-2) or 320 kbps (MPEG-1) */
1085 if (cfg->samplerate_out < 16000)
1086 cfg->vbr_max_bitrate_index = 8; /* default: allow 64 kbps (MPEG-2.5) */
1087 if (gfp->VBR_min_bitrate_kbps) {
1088 gfp->VBR_min_bitrate_kbps =
1089 FindNearestBitrate(gfp->VBR_min_bitrate_kbps, cfg->version, cfg->samplerate_out);
1090 cfg->vbr_min_bitrate_index =
1091 BitrateIndex(gfp->VBR_min_bitrate_kbps, cfg->version, cfg->samplerate_out);
1092 if (cfg->vbr_min_bitrate_index < 0) {
1093 /* This never happens, because of preceding FindNearestBitrate!
1094 * But, set a sane value, just in case
1095 */
1096 assert(0);
1097 cfg->vbr_min_bitrate_index = 1;
1098 }
1099 }
1100 if (gfp->VBR_max_bitrate_kbps) {
1101 gfp->VBR_max_bitrate_kbps =
1102 FindNearestBitrate(gfp->VBR_max_bitrate_kbps, cfg->version, cfg->samplerate_out);
1103 cfg->vbr_max_bitrate_index =
1104 BitrateIndex(gfp->VBR_max_bitrate_kbps, cfg->version, cfg->samplerate_out);
1105 if (cfg->vbr_max_bitrate_index < 0) {
1106 /* This never happens, because of preceding FindNearestBitrate!
1107 * But, set a sane value, just in case
1108 */
1109 assert(0);
1110 cfg->vbr_max_bitrate_index = cfg->samplerate_out < 16000 ? 8 : 14;
1111 }
1112 }
1113 gfp->VBR_min_bitrate_kbps = bitrate_table[cfg->version][cfg->vbr_min_bitrate_index];
1114 gfp->VBR_max_bitrate_kbps = bitrate_table[cfg->version][cfg->vbr_max_bitrate_index];
1115 gfp->VBR_mean_bitrate_kbps =
1116 Min(bitrate_table[cfg->version][cfg->vbr_max_bitrate_index],
1117 gfp->VBR_mean_bitrate_kbps);
1118 gfp->VBR_mean_bitrate_kbps =
1119 Max(bitrate_table[cfg->version][cfg->vbr_min_bitrate_index],
1120 gfp->VBR_mean_bitrate_kbps);
1121 }
1122
1123 cfg->preset = gfp->preset;
1124 cfg->write_lame_tag = gfp->write_lame_tag;
1125 gfc->sv_qnt.substep_shaping = gfp->substep_shaping;
1126 cfg->noise_shaping = gfp->noise_shaping;
1127 cfg->subblock_gain = gfp->subblock_gain;
1128 cfg->use_best_huffman = gfp->use_best_huffman;
1129 cfg->avg_bitrate = gfp->brate;
1130 cfg->vbr_avg_bitrate_kbps = gfp->VBR_mean_bitrate_kbps;
1131 cfg->compression_ratio = gfp->compression_ratio;
1132
1133 /* initialize internal qval settings */
1134 lame_init_qval(gfp);
1135
1136
1137 /* automatic ATH adjustment on
1138 */
1139 if (gfp->athaa_type < 0)
1140 gfc->ATH->use_adjust = 3;
1141 else
1142 gfc->ATH->use_adjust = gfp->athaa_type;
1143
1144
1145 /* initialize internal adaptive ATH settings -jd */
1146 gfc->ATH->aa_sensitivity_p = pow(10.0, gfp->athaa_sensitivity / -10.0);
1147
1148
1149 if (gfp->short_blocks == short_block_not_set) {
1150 gfp->short_blocks = short_block_allowed;
1151 }
1152
1153 /*Note Jan/2003: Many hardware decoders cannot handle short blocks in regular
1154 stereo mode unless they are coupled (same type in both channels)
1155 it is a rare event (1 frame per min. or so) that LAME would use
1156 uncoupled short blocks, so lets turn them off until we decide
1157 how to handle this. No other encoders allow uncoupled short blocks,
1158 even though it is in the standard. */
1159 /* rh 20040217: coupling makes no sense for mono and dual-mono streams
1160 */
1161 if (gfp->short_blocks == short_block_allowed
1162 && (cfg->mode == JOINT_STEREO || cfg->mode == STEREO)) {
1163 gfp->short_blocks = short_block_coupled;
1164 }
1165
1166 cfg->short_blocks = gfp->short_blocks;
1167
1168
1169 if (lame_get_quant_comp(gfp) < 0)
1170 (void) lame_set_quant_comp(gfp, 1);
1171 if (lame_get_quant_comp_short(gfp) < 0)
1172 (void) lame_set_quant_comp_short(gfp, 0);
1173
1174 if (lame_get_msfix(gfp) < 0)
1175 lame_set_msfix(gfp, 0);
1176
1177 /* select psychoacoustic model */
1178 (void) lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | 1);
1179
1180 if (gfp->ATHtype < 0)
1181 gfp->ATHtype = 4;
1182
1183 if (gfp->ATHcurve < 0)
1184 gfp->ATHcurve = 4;
1185
1186 if (gfp->interChRatio < 0)
1187 gfp->interChRatio = 0;
1188
1189 if (gfp->useTemporal < 0)
1190 gfp->useTemporal = 1; /* on by default */
1191
1192
1193 cfg->interChRatio = gfp->interChRatio;
1194 cfg->msfix = gfp->msfix;
1195 cfg->ATH_offset_db = 0-gfp->ATH_lower_db;
1196 cfg->ATH_offset_factor = powf(10.f, cfg->ATH_offset_db * 0.1f);
1197 cfg->ATHcurve = gfp->ATHcurve;
1198 cfg->ATHtype = gfp->ATHtype;
1199 cfg->ATHonly = gfp->ATHonly;
1200 cfg->ATHshort = gfp->ATHshort;
1201 cfg->noATH = gfp->noATH;
1202
1203 cfg->quant_comp = gfp->quant_comp;
1204 cfg->quant_comp_short = gfp->quant_comp_short;
1205
1206 cfg->use_temporal_masking_effect = gfp->useTemporal;
1207 if (cfg->mode == JOINT_STEREO) {
1208 cfg->use_safe_joint_stereo = gfp->exp_nspsytune & 2;
1209 }
1210 else {
1211 cfg->use_safe_joint_stereo = 0;
1212 }
1213 {
1214 cfg->adjust_bass_db = (gfp->exp_nspsytune >> 2) & 63;
1215 if (cfg->adjust_bass_db >= 32.f)
1216 cfg->adjust_bass_db -= 64.f;
1217 cfg->adjust_bass_db *= 0.25f;
1218
1219 cfg->adjust_alto_db = (gfp->exp_nspsytune >> 8) & 63;
1220 if (cfg->adjust_alto_db >= 32.f)
1221 cfg->adjust_alto_db -= 64.f;
1222 cfg->adjust_alto_db *= 0.25f;
1223
1224 cfg->adjust_treble_db = (gfp->exp_nspsytune >> 14) & 63;
1225 if (cfg->adjust_treble_db >= 32.f)
1226 cfg->adjust_treble_db -= 64.f;
1227 cfg->adjust_treble_db *= 0.25f;
1228
1229 /* to be compatible with Naoki's original code, the next 6 bits
1230 * define only the amount of changing treble for sfb21 */
1231 cfg->adjust_sfb21_db = (gfp->exp_nspsytune >> 20) & 63;
1232 if (cfg->adjust_sfb21_db >= 32.f)
1233 cfg->adjust_sfb21_db -= 64.f;
1234 cfg->adjust_sfb21_db *= 0.25f;
1235 cfg->adjust_sfb21_db += cfg->adjust_treble_db;
1236 }
1237
1238 /* Setting up the PCM input data transform matrix, to apply
1239 * user defined re-scaling, and or two-to-one channel downmix.
1240 */
1241 {
1242 FLOAT m[2][2] = { {1.0f, 0.0f}, {0.0f, 1.0f} };
1243
1244 /* user selected scaling of the samples */
1245 m[0][0] *= gfp->scale;
1246 m[0][1] *= gfp->scale;
1247 m[1][0] *= gfp->scale;
1248 m[1][1] *= gfp->scale;
1249 /* user selected scaling of the channel 0 (left) samples */
1250 m[0][0] *= gfp->scale_left;
1251 m[0][1] *= gfp->scale_left;
1252 /* user selected scaling of the channel 1 (right) samples */
1253 m[1][0] *= gfp->scale_right;
1254 m[1][1] *= gfp->scale_right;
1255 /* Downsample to Mono if 2 channels in and 1 channel out */
1256 if (cfg->channels_in == 2 && cfg->channels_out == 1) {
1257 m[0][0] = 0.5f * (m[0][0] + m[1][0]);
1258 m[0][1] = 0.5f * (m[0][1] + m[1][1]);
1259 m[1][0] = 0;
1260 m[1][1] = 0;
1261 }
1262 cfg->pcm_transform[0][0] = m[0][0];
1263 cfg->pcm_transform[0][1] = m[0][1];
1264 cfg->pcm_transform[1][0] = m[1][0];
1265 cfg->pcm_transform[1][1] = m[1][1];
1266 }
1267
1268 /* padding method as described in
1269 * "MPEG-Layer3 / Bitstream Syntax and Decoding"
1270 * by Martin Sieler, Ralph Sperschneider
1271 *
1272 * note: there is no padding for the very first frame
1273 *
1274 * Robert Hegemann 2000-06-22
1275 */
1276 gfc->sv_enc.slot_lag = gfc->sv_enc.frac_SpF = 0;
1277 if (cfg->vbr == vbr_off)
1278 gfc->sv_enc.slot_lag = gfc->sv_enc.frac_SpF
1279 = ((cfg->version + 1) * 72000L * cfg->avg_bitrate) % cfg->samplerate_out;
1280
1281 (void) lame_init_bitstream(gfp);
1282
1283 iteration_init(gfc);
1284 (void) psymodel_init(gfp);
1285
1286 cfg->buffer_constraint = get_max_frame_buffer_size_by_constraint(cfg, gfp->strict_ISO);
1287
1288
1289 cfg->findReplayGain = gfp->findReplayGain;
1290 cfg->decode_on_the_fly = gfp->decode_on_the_fly;
1291
1292 if (cfg->decode_on_the_fly)
1293 cfg->findPeakSample = 1;
1294
1295 if (cfg->findReplayGain) {
1296 if (InitGainAnalysis(gfc->sv_rpg.rgdata, cfg->samplerate_out) == INIT_GAIN_ANALYSIS_ERROR) {
1297 /* Actually this never happens, our samplerates are the ones RG accepts!
1298 * But just in case, turn RG off
1299 */
1300 assert(0);
1301 cfg->findReplayGain = 0;
1302 }
1303 }
1304
1305 #ifdef DECODE_ON_THE_FLY
1306 if (cfg->decode_on_the_fly && !gfp->decode_only) {
1307 if (gfc->hip) {
1308 hip_decode_exit(gfc->hip);
1309 }
1310 gfc->hip = hip_decode_init();
1311 /* report functions */
1312 hip_set_errorf(gfc->hip, gfp->report.errorf);
1313 hip_set_debugf(gfc->hip, gfp->report.debugf);
1314 hip_set_msgf(gfc->hip, gfp->report.msgf);
1315 }
1316 #endif
1317 /* updating lame internal flags finished successful */
1318 gfc->lame_init_params_successful = 1;
1319 return 0;
1320 }
1321
1322 static void
1323 concatSep(char* dest, char const* sep, char const* str)
1324 {
1325 if (*dest != 0) strcat(dest, sep);
1326 strcat(dest, str);
1327 }
1328
1329 /*
1330 * print_config
1331 *
1332 * Prints some selected information about the coding parameters via
1333 * the macro command MSGF(), which is currently mapped to lame_errorf
1334 * (reports via a error function?), which is a printf-like function
1335 * for <stderr>.
1336 */
1337
1338 void
1339 lame_print_config(const lame_global_flags * gfp)
1340 {
1341 lame_internal_flags const *const gfc = gfp->internal_flags;
1342 SessionConfig_t const *const cfg = &gfc->cfg;
1343 double const out_samplerate = cfg->samplerate_out;
1344 double const in_samplerate = cfg->samplerate_in;
1345
1346 MSGF(gfc, "LAME %s %s (%s)\n", get_lame_version(), get_lame_os_bitness(), get_lame_url());
1347
1348 #if (LAME_ALPHA_VERSION)
1349 MSGF(gfc, "warning: alpha versions should be used for testing only\n");
1350 #endif
1351 if (gfc->CPU_features.MMX
1352 || gfc->CPU_features.AMD_3DNow || gfc->CPU_features.SSE || gfc->CPU_features.SSE2) {
1353 char text[256] = { 0 };
1354 int fft_asm_used = 0;
1355 #ifdef HAVE_NASM
1356 if (gfc->CPU_features.AMD_3DNow) {
1357 fft_asm_used = 1;
1358 }
1359 else if (gfc->CPU_features.SSE) {
1360 fft_asm_used = 2;
1361 }
1362 #else
1363 # if defined( HAVE_XMMINTRIN_H ) && defined( MIN_ARCH_SSE )
1364 {
1365 fft_asm_used = 3;
1366 }
1367 # endif
1368 #endif
1369 if (gfc->CPU_features.MMX) {
1370 #ifdef MMX_choose_table
1371 concatSep(text, ", ", "MMX (ASM used)");
1372 #else
1373 concatSep(text, ", ", "MMX");
1374 #endif
1375 }
1376 if (gfc->CPU_features.AMD_3DNow) {
1377 concatSep(text, ", ", (fft_asm_used == 1) ? "3DNow! (ASM used)" : "3DNow!");
1378 }
1379 if (gfc->CPU_features.SSE) {
1380 #if defined(HAVE_XMMINTRIN_H)
1381 concatSep(text, ", ", "SSE (ASM used)");
1382 #else
1383 concatSep(text, ", ", (fft_asm_used == 2) ? "SSE (ASM used)" : "SSE");
1384 #endif
1385 }
1386 if (gfc->CPU_features.SSE2) {
1387 concatSep(text, ", ", (fft_asm_used == 3) ? "SSE2 (ASM used)" : "SSE2");
1388 }
1389 MSGF(gfc, "CPU features: %s\n", text);
1390 }
1391
1392 if (cfg->channels_in == 2 && cfg->channels_out == 1 /* mono */ ) {
1393 MSGF(gfc, "Autoconverting from stereo to mono. Setting encoding to mono mode.\n");
1394 }
1395
1396 if (isResamplingNecessary(cfg)) {
1397 MSGF(gfc, "Resampling: input %g kHz output %g kHz\n",
1398 1.e-3 * in_samplerate, 1.e-3 * out_samplerate);
1399 }
1400
1401 if (cfg->highpass2 > 0.)
1402 MSGF(gfc,
1403 "Using polyphase highpass filter, transition band: %5.0f Hz - %5.0f Hz\n",
1404 0.5 * cfg->highpass1 * out_samplerate, 0.5 * cfg->highpass2 * out_samplerate);
1405 if (0. < cfg->lowpass1 || 0. < cfg->lowpass2) {
1406 MSGF(gfc,
1407 "Using polyphase lowpass filter, transition band: %5.0f Hz - %5.0f Hz\n",
1408 0.5 * cfg->lowpass1 * out_samplerate, 0.5 * cfg->lowpass2 * out_samplerate);
1409 }
1410 else {
1411 MSGF(gfc, "polyphase lowpass filter disabled\n");
1412 }
1413
1414 if (cfg->free_format) {
1415 MSGF(gfc, "Warning: many decoders cannot handle free format bitstreams\n");
1416 if (cfg->avg_bitrate > 320) {
1417 MSGF(gfc,
1418 "Warning: many decoders cannot handle free format bitrates >320 kbps (see documentation)\n");
1419 }
1420 }
1421 }
1422
1423
1424 /** rh:
1425 * some pretty printing is very welcome at this point!
1426 * so, if someone is willing to do so, please do it!
1427 * add more, if you see more...
1428 */
1429 void
1430 lame_print_internals(const lame_global_flags * gfp)
1431 {
1432 lame_internal_flags const *const gfc = gfp->internal_flags;
1433 SessionConfig_t const *const cfg = &gfc->cfg;
1434 const char *pc = "";
1435
1436 /* compiler/processor optimizations, operational, etc.
1437 */
1438 MSGF(gfc, "\nmisc:\n\n");
1439
1440 MSGF(gfc, "\tscaling: %g\n", gfp->scale);
1441 MSGF(gfc, "\tch0 (left) scaling: %g\n", gfp->scale_left);
1442 MSGF(gfc, "\tch1 (right) scaling: %g\n", gfp->scale_right);
1443 switch (cfg->use_best_huffman) {
1444 default:
1445 pc = "normal";
1446 break;
1447 case 1:
1448 pc = "best (outside loop)";
1449 break;
1450 case 2:
1451 pc = "best (inside loop, slow)";
1452 break;
1453 }
1454 MSGF(gfc, "\thuffman search: %s\n", pc);
1455 MSGF(gfc, "\texperimental Y=%d\n", gfp->experimentalY);
1456 MSGF(gfc, "\t...\n");
1457
1458 /* everything controlling the stream format
1459 */
1460 MSGF(gfc, "\nstream format:\n\n");
1461 switch (cfg->version) {
1462 case 0:
1463 pc = "2.5";
1464 break;
1465 case 1:
1466 pc = "1";
1467 break;
1468 case 2:
1469 pc = "2";
1470 break;
1471 default:
1472 pc = "?";
1473 break;
1474 }
1475 MSGF(gfc, "\tMPEG-%s Layer 3\n", pc);
1476 switch (cfg->mode) {
1477 case JOINT_STEREO:
1478 pc = "joint stereo";
1479 break;
1480 case STEREO:
1481 pc = "stereo";
1482 break;
1483 case DUAL_CHANNEL:
1484 pc = "dual channel";
1485 break;
1486 case MONO:
1487 pc = "mono";
1488 break;
1489 case NOT_SET:
1490 pc = "not set (error)";
1491 break;
1492 default:
1493 pc = "unknown (error)";
1494 break;
1495 }
1496 MSGF(gfc, "\t%d channel - %s\n", cfg->channels_out, pc);
1497
1498 switch (cfg->vbr) {
1499 case vbr_off:
1500 pc = "off";
1501 break;
1502 default:
1503 pc = "all";
1504 break;
1505 }
1506 MSGF(gfc, "\tpadding: %s\n", pc);
1507
1508 if (vbr_default == cfg->vbr)
1509 pc = "(default)";
1510 else if (cfg->free_format)
1511 pc = "(free format)";
1512 else
1513 pc = "";
1514 switch (cfg->vbr) {
1515 case vbr_off:
1516 MSGF(gfc, "\tconstant bitrate - CBR %s\n", pc);
1517 break;
1518 case vbr_abr:
1519 MSGF(gfc, "\tvariable bitrate - ABR %s\n", pc);
1520 break;
1521 case vbr_rh:
1522 MSGF(gfc, "\tvariable bitrate - VBR rh %s\n", pc);
1523 break;
1524 case vbr_mt:
1525 MSGF(gfc, "\tvariable bitrate - VBR mt %s\n", pc);
1526 break;
1527 case vbr_mtrh:
1528 MSGF(gfc, "\tvariable bitrate - VBR mtrh %s\n", pc);
1529 break;
1530 default:
1531 MSGF(gfc, "\t ?? oops, some new one ?? \n");
1532 break;
1533 }
1534 if (cfg->write_lame_tag)
1535 MSGF(gfc, "\tusing LAME Tag\n");
1536 MSGF(gfc, "\t...\n");
1537
1538 /* everything controlling psychoacoustic settings, like ATH, etc.
1539 */
1540 MSGF(gfc, "\npsychoacoustic:\n\n");
1541
1542 switch (cfg->short_blocks) {
1543 default:
1544 case short_block_not_set:
1545 pc = "?";
1546 break;
1547 case short_block_allowed:
1548 pc = "allowed";
1549 break;
1550 case short_block_coupled:
1551 pc = "channel coupled";
1552 break;
1553 case short_block_dispensed:
1554 pc = "dispensed";
1555 break;
1556 case short_block_forced:
1557 pc = "forced";
1558 break;
1559 }
1560 MSGF(gfc, "\tusing short blocks: %s\n", pc);
1561 MSGF(gfc, "\tsubblock gain: %d\n", cfg->subblock_gain);
1562 MSGF(gfc, "\tadjust masking: %g dB\n", gfc->sv_qnt.mask_adjust);
1563 MSGF(gfc, "\tadjust masking short: %g dB\n", gfc->sv_qnt.mask_adjust_short);
1564 MSGF(gfc, "\tquantization comparison: %d\n", cfg->quant_comp);
1565 MSGF(gfc, "\t ^ comparison short blocks: %d\n", cfg->quant_comp_short);
1566 MSGF(gfc, "\tnoise shaping: %d\n", cfg->noise_shaping);
1567 MSGF(gfc, "\t ^ amplification: %d\n", cfg->noise_shaping_amp);
1568 MSGF(gfc, "\t ^ stopping: %d\n", cfg->noise_shaping_stop);
1569
1570 pc = "using";
1571 if (cfg->ATHshort)
1572 pc = "the only masking for short blocks";
1573 if (cfg->ATHonly)
1574 pc = "the only masking";
1575 if (cfg->noATH)
1576 pc = "not used";
1577 MSGF(gfc, "\tATH: %s\n", pc);
1578 MSGF(gfc, "\t ^ type: %d\n", cfg->ATHtype);
1579 MSGF(gfc, "\t ^ shape: %g%s\n", cfg->ATHcurve, " (only for type 4)");
1580 MSGF(gfc, "\t ^ level adjustement: %g dB\n", cfg->ATH_offset_db);
1581 MSGF(gfc, "\t ^ adjust type: %d\n", gfc->ATH->use_adjust);
1582 MSGF(gfc, "\t ^ adjust sensitivity power: %f\n", gfc->ATH->aa_sensitivity_p);
1583
1584 MSGF(gfc, "\texperimental psy tunings by Naoki Shibata\n");
1585 MSGF(gfc, "\t adjust masking bass=%g dB, alto=%g dB, treble=%g dB, sfb21=%g dB\n",
1586 10 * log10(gfc->sv_qnt.longfact[0]),
1587 10 * log10(gfc->sv_qnt.longfact[7]),
1588 10 * log10(gfc->sv_qnt.longfact[14]), 10 * log10(gfc->sv_qnt.longfact[21]));
1589
1590 pc = cfg->use_temporal_masking_effect ? "yes" : "no";
1591 MSGF(gfc, "\tusing temporal masking effect: %s\n", pc);
1592 MSGF(gfc, "\tinterchannel masking ratio: %g\n", cfg->interChRatio);
1593 MSGF(gfc, "\t...\n");
1594
1595 /* that's all ?
1596 */
1597 MSGF(gfc, "\n");
1598 return;
1599 }
1600
1601
1602 static void
1603 save_gain_values(lame_internal_flags * gfc)
1604 {
1605 SessionConfig_t const *const cfg = &gfc->cfg;
1606 RpgStateVar_t const *const rsv = &gfc->sv_rpg;
1607 RpgResult_t *const rov = &gfc->ov_rpg;
1608 /* save the ReplayGain value */
1609 if (cfg->findReplayGain) {
1610 FLOAT const RadioGain = (FLOAT) GetTitleGain(rsv->rgdata);
1611 if (NEQ(RadioGain, GAIN_NOT_ENOUGH_SAMPLES)) {
1612 rov->RadioGain = (int) floor(RadioGain * 10.0 + 0.5); /* round to nearest */
1613 }
1614 else {
1615 rov->RadioGain = 0;
1616 }
1617 }
1618
1619 /* find the gain and scale change required for no clipping */
1620 if (cfg->findPeakSample) {
1621 rov->noclipGainChange = (int) ceil(log10(rov->PeakSample / 32767.0) * 20.0 * 10.0); /* round up */
1622
1623 if (rov->noclipGainChange > 0) { /* clipping occurs */
1624 rov->noclipScale = floor((32767.0f / rov->PeakSample) * 100.0f) / 100.0f; /* round down */
1625 }
1626 else /* no clipping */
1627 rov->noclipScale = -1.0f;
1628 }
1629 }
1630
1631
1632
1633 static int
1634 update_inbuffer_size(lame_internal_flags * gfc, const int nsamples)
1635 {
1636 EncStateVar_t *const esv = &gfc->sv_enc;
1637 if (esv->in_buffer_0 == 0 || esv->in_buffer_nsamples < nsamples) {
1638 if (esv->in_buffer_0) {
1639 free(esv->in_buffer_0);
1640 }
1641 if (esv->in_buffer_1) {
1642 free(esv->in_buffer_1);
1643 }
1644 esv->in_buffer_0 = lame_calloc(sample_t, nsamples);
1645 esv->in_buffer_1 = lame_calloc(sample_t, nsamples);
1646 esv->in_buffer_nsamples = nsamples;
1647 }
1648 if (esv->in_buffer_0 == NULL || esv->in_buffer_1 == NULL) {
1649 if (esv->in_buffer_0) {
1650 free(esv->in_buffer_0);
1651 }
1652 if (esv->in_buffer_1) {
1653 free(esv->in_buffer_1);
1654 }
1655 esv->in_buffer_0 = 0;
1656 esv->in_buffer_1 = 0;
1657 esv->in_buffer_nsamples = 0;
1658 ERRORF(gfc, "Error: can't allocate in_buffer buffer\n");
1659 return -2;
1660 }
1661 return 0;
1662 }
1663
1664
1665 static int
1666 calcNeeded(SessionConfig_t const * cfg)
1667 {
1668 int mf_needed;
1669 int pcm_samples_per_frame = 576 * cfg->mode_gr;
1670
1671 /* some sanity checks */
1672 #if ENCDELAY < MDCTDELAY
1673 # error ENCDELAY is less than MDCTDELAY, see encoder.h
1674 #endif
1675 #if FFTOFFSET > BLKSIZE
1676 # error FFTOFFSET is greater than BLKSIZE, see encoder.h
1677 #endif
1678
1679 mf_needed = BLKSIZE + pcm_samples_per_frame - FFTOFFSET; /* amount needed for FFT */
1680 /*mf_needed = Max(mf_needed, 286 + 576 * (1 + gfc->mode_gr)); */
1681 mf_needed = Max(mf_needed, 512 + pcm_samples_per_frame - 32);
1682
1683 assert(MFSIZE >= mf_needed);
1684
1685 return mf_needed;
1686 }
1687
1688
1689 /*
1690 * THE MAIN LAME ENCODING INTERFACE
1691 * mt 3/00
1692 *
1693 * input pcm data, output (maybe) mp3 frames.
1694 * This routine handles all buffering, resampling and filtering for you.
1695 * The required mp3buffer_size can be computed from num_samples,
1696 * samplerate and encoding rate, but here is a worst case estimate:
1697 *
1698 * mp3buffer_size in bytes = 1.25*num_samples + 7200
1699 *
1700 * return code = number of bytes output in mp3buffer. can be 0
1701 *
1702 * NOTE: this routine uses LAME's internal PCM data representation,
1703 * 'sample_t'. It should not be used by any application.
1704 * applications should use lame_encode_buffer(),
1705 * lame_encode_buffer_float()
1706 * lame_encode_buffer_int()
1707 * etc... depending on what type of data they are working with.
1708 */
1709 static int
1710 lame_encode_buffer_sample_t(lame_internal_flags * gfc,
1711 int nsamples, unsigned char *mp3buf, const int mp3buf_size)
1712 {
1713 SessionConfig_t const *const cfg = &gfc->cfg;
1714 EncStateVar_t *const esv = &gfc->sv_enc;
1715 int pcm_samples_per_frame = 576 * cfg->mode_gr;
1716 int mp3size = 0, ret, i, ch, mf_needed;
1717 int mp3out;
1718 sample_t *mfbuf[2];
1719 sample_t *in_buffer[2];
1720
1721 if (gfc->class_id != LAME_ID)
1722 return -3;
1723
1724 if (nsamples == 0)
1725 return 0;
1726
1727 /* copy out any tags that may have been written into bitstream */
1728 { /* if user specifed buffer size = 0, dont check size */
1729 int const buf_size = mp3buf_size == 0 ? INT_MAX : mp3buf_size;
1730 mp3out = copy_buffer(gfc, mp3buf, buf_size, 0);
1731 }
1732 if (mp3out < 0)
1733 return mp3out; /* not enough buffer space */
1734 mp3buf += mp3out;
1735 mp3size += mp3out;
1736
1737 in_buffer[0] = esv->in_buffer_0;
1738 in_buffer[1] = esv->in_buffer_1;
1739
1740 mf_needed = calcNeeded(cfg);
1741
1742 mfbuf[0] = esv->mfbuf[0];
1743 mfbuf[1] = esv->mfbuf[1];
1744
1745 while (nsamples > 0) {
1746 sample_t const *in_buffer_ptr[2];
1747 int n_in = 0; /* number of input samples processed with fill_buffer */
1748 int n_out = 0; /* number of samples output with fill_buffer */
1749 /* n_in <> n_out if we are resampling */
1750
1751 in_buffer_ptr[0] = in_buffer[0];
1752 in_buffer_ptr[1] = in_buffer[1];
1753 /* copy in new samples into mfbuf, with resampling */
1754 fill_buffer(gfc, mfbuf, &in_buffer_ptr[0], nsamples, &n_in, &n_out);
1755
1756 /* compute ReplayGain of resampled input if requested */
1757 if (cfg->findReplayGain && !cfg->decode_on_the_fly)
1758 if (AnalyzeSamples
1759 (gfc->sv_rpg.rgdata, &mfbuf[0][esv->mf_size], &mfbuf[1][esv->mf_size], n_out,
1760 cfg->channels_out) == GAIN_ANALYSIS_ERROR)
1761 return -6;
1762
1763
1764
1765 /* update in_buffer counters */
1766 nsamples -= n_in;
1767 in_buffer[0] += n_in;
1768 if (cfg->channels_out == 2)
1769 in_buffer[1] += n_in;
1770
1771 /* update mfbuf[] counters */
1772 esv->mf_size += n_out;
1773 assert(esv->mf_size <= MFSIZE);
1774
1775 /* lame_encode_flush may have set gfc->mf_sample_to_encode to 0
1776 * so we have to reinitialize it here when that happened.
1777 */
1778 if (esv->mf_samples_to_encode < 1) {
1779 esv->mf_samples_to_encode = ENCDELAY + POSTDELAY;
1780 }
1781 esv->mf_samples_to_encode += n_out;
1782
1783
1784 if (esv->mf_size >= mf_needed) {
1785 /* encode the frame. */
1786 /* mp3buf = pointer to current location in buffer */
1787 /* mp3buf_size = size of original mp3 output buffer */
1788 /* = 0 if we should not worry about the */
1789 /* buffer size because calling program is */
1790 /* to lazy to compute it */
1791 /* mp3size = size of data written to buffer so far */
1792 /* mp3buf_size-mp3size = amount of space avalable */
1793
1794 int buf_size = mp3buf_size - mp3size;
1795 if (mp3buf_size == 0)
1796 buf_size = INT_MAX;
1797
1798 ret = lame_encode_mp3_frame(gfc, mfbuf[0], mfbuf[1], mp3buf, buf_size);
1799
1800 if (ret < 0)
1801 return ret;
1802 mp3buf += ret;
1803 mp3size += ret;
1804
1805 /* shift out old samples */
1806 esv->mf_size -= pcm_samples_per_frame;
1807 esv->mf_samples_to_encode -= pcm_samples_per_frame;
1808 for (ch = 0; ch < cfg->channels_out; ch++)
1809 for (i = 0; i < esv->mf_size; i++)
1810 mfbuf[ch][i] = mfbuf[ch][i + pcm_samples_per_frame];
1811 }
1812 }
1813 assert(nsamples == 0);
1814
1815 return mp3size;
1816 }
1817
1818 enum PCMSampleType
1819 { pcm_short_type
1820 , pcm_int_type
1821 , pcm_long_type
1822 , pcm_float_type
1823 , pcm_double_type
1824 };
1825
1826 static void
1827 lame_copy_inbuffer(lame_internal_flags* gfc,
1828 void const* l, void const* r, int nsamples,
1829 enum PCMSampleType pcm_type, int jump, FLOAT s)
1830 {
1831 SessionConfig_t const *const cfg = &gfc->cfg;
1832 EncStateVar_t *const esv = &gfc->sv_enc;
1833 sample_t* ib0 = esv->in_buffer_0;
1834 sample_t* ib1 = esv->in_buffer_1;
1835 FLOAT m[2][2];
1836
1837 /* Apply user defined re-scaling */
1838 m[0][0] = s * cfg->pcm_transform[0][0];
1839 m[0][1] = s * cfg->pcm_transform[0][1];
1840 m[1][0] = s * cfg->pcm_transform[1][0];
1841 m[1][1] = s * cfg->pcm_transform[1][1];
1842
1843 /* make a copy of input buffer, changing type to sample_t */
1844 #define COPY_AND_TRANSFORM(T) \
1845 { \
1846 T const *bl = l, *br = r; \
1847 int i; \
1848 for (i = 0; i < nsamples; i++) { \
1849 sample_t const xl = *bl; \
1850 sample_t const xr = *br; \
1851 sample_t const u = xl * m[0][0] + xr * m[0][1]; \
1852 sample_t const v = xl * m[1][0] + xr * m[1][1]; \
1853 ib0[i] = u; \
1854 ib1[i] = v; \
1855 bl += jump; \
1856 br += jump; \
1857 } \
1858 }
1859 switch ( pcm_type ) {
1860 case pcm_short_type:
1861 COPY_AND_TRANSFORM(short int);
1862 break;
1863 case pcm_int_type:
1864 COPY_AND_TRANSFORM(int);
1865 break;
1866 case pcm_long_type:
1867 COPY_AND_TRANSFORM(long int);
1868 break;
1869 case pcm_float_type:
1870 COPY_AND_TRANSFORM(float);
1871 break;
1872 case pcm_double_type:
1873 COPY_AND_TRANSFORM(double);
1874 break;
1875 }
1876 }
1877
1878
1879 static int
1880 lame_encode_buffer_template(lame_global_flags * gfp,
1881 void const* buffer_l, void const* buffer_r, const int nsamples,
1882 unsigned char *mp3buf, const int mp3buf_size, enum PCMSampleType pcm_type, int aa, FLOAT norm)
1883 {
1884 if (is_lame_global_flags_valid(gfp)) {
1885 lame_internal_flags *const gfc = gfp->internal_flags;
1886 if (is_lame_internal_flags_valid(gfc)) {
1887 SessionConfig_t const *const cfg = &gfc->cfg;
1888
1889 if (nsamples == 0)
1890 return 0;
1891
1892 if (update_inbuffer_size(gfc, nsamples) != 0) {
1893 return -2;
1894 }
1895 /* make a copy of input buffer, changing type to sample_t */
1896 if (cfg->channels_in > 1) {
1897 if (buffer_l == 0 || buffer_r == 0) {
1898 return 0;
1899 }
1900 lame_copy_inbuffer(gfc, buffer_l, buffer_r, nsamples, pcm_type, aa, norm);
1901 }
1902 else {
1903 if (buffer_l == 0) {
1904 return 0;
1905 }
1906 lame_copy_inbuffer(gfc, buffer_l, buffer_l, nsamples, pcm_type, aa, norm);
1907 }
1908
1909 return lame_encode_buffer_sample_t(gfc, nsamples, mp3buf, mp3buf_size);
1910 }
1911 }
1912 return -3;
1913 }
1914
1915 int
1916 lame_encode_buffer(lame_global_flags * gfp,
1917 const short int pcm_l[], const short int pcm_r[], const int nsamples,
1918 unsigned char *mp3buf, const int mp3buf_size)
1919 {
1920 return lame_encode_buffer_template(gfp, pcm_l, pcm_r, nsamples, mp3buf, mp3buf_size, pcm_short_type, 1, 1.0);
1921 }
1922
1923
1924 int
1925 lame_encode_buffer_float(lame_global_flags * gfp,
1926 const float pcm_l[], const float pcm_r[], const int nsamples,
1927 unsigned char *mp3buf, const int mp3buf_size)
1928 {
1929 /* input is assumed to be normalized to +/- 32768 for full scale */
1930 return lame_encode_buffer_template(gfp, pcm_l, pcm_r, nsamples, mp3buf, mp3buf_size, pcm_float_type, 1, 1.0);
1931 }
1932
1933
1934 int
1935 lame_encode_buffer_ieee_float(lame_t gfp,
1936 const float pcm_l[], const float pcm_r[], const int nsamples,
1937 unsigned char *mp3buf, const int mp3buf_size)
1938 {
1939 /* input is assumed to be normalized to +/- 1.0 for full scale */
1940 return lame_encode_buffer_template(gfp, pcm_l, pcm_r, nsamples, mp3buf, mp3buf_size, pcm_float_type, 1, 32767.0);
1941 }
1942
1943
1944 int
1945 lame_encode_buffer_interleaved_ieee_float(lame_t gfp,
1946 const float pcm[], const int nsamples,
1947 unsigned char *mp3buf, const int mp3buf_size)
1948 {
1949 /* input is assumed to be normalized to +/- 1.0 for full scale */
1950 return lame_encode_buffer_template(gfp, pcm, pcm+1, nsamples, mp3buf, mp3buf_size, pcm_float_type, 2, 32767.0);
1951 }
1952
1953
1954 int
1955 lame_encode_buffer_ieee_double(lame_t gfp,
1956 const double pcm_l[], const double pcm_r[], const int nsamples,
1957 unsigned char *mp3buf, const int mp3buf_size)
1958 {
1959 /* input is assumed to be normalized to +/- 1.0 for full scale */
1960 return lame_encode_buffer_template(gfp, pcm_l, pcm_r, nsamples, mp3buf, mp3buf_size, pcm_double_type, 1, 32767.0);
1961 }
1962
1963
1964 int
1965 lame_encode_buffer_interleaved_ieee_double(lame_t gfp,
1966 const double pcm[], const int nsamples,
1967 unsigned char *mp3buf, const int mp3buf_size)
1968 {
1969 /* input is assumed to be normalized to +/- 1.0 for full scale */
1970 return lame_encode_buffer_template(gfp, pcm, pcm+1, nsamples, mp3buf, mp3buf_size, pcm_double_type, 2, 32767.0);
1971 }
1972
1973
1974 int
1975 lame_encode_buffer_int(lame_global_flags * gfp,
1976 const int pcm_l[], const int pcm_r[], const int nsamples,
1977 unsigned char *mp3buf, const int mp3buf_size)
1978 {
1979 /* input is assumed to be normalized to +/- MAX_INT for full scale */
1980 FLOAT const norm = (1.0 / (1L << (8 * sizeof(int) - 16)));
1981 return lame_encode_buffer_template(gfp, pcm_l, pcm_r, nsamples, mp3buf, mp3buf_size, pcm_int_type, 1, norm);
1982 }
1983
1984
1985 int
1986 lame_encode_buffer_long2(lame_global_flags * gfp,
1987 const long pcm_l[], const long pcm_r[], const int nsamples,
1988 unsigned char *mp3buf, const int mp3buf_size)
1989 {
1990 /* input is assumed to be normalized to +/- MAX_LONG for full scale */
1991 FLOAT const norm = (1.0 / (1L << (8 * sizeof(long) - 16)));
1992 return lame_encode_buffer_template(gfp, pcm_l, pcm_r, nsamples, mp3buf, mp3buf_size, pcm_long_type, 1, norm);
1993 }
1994
1995
1996 int
1997 lame_encode_buffer_long(lame_global_flags * gfp,
1998 const long pcm_l[], const long pcm_r[], const int nsamples,
1999 unsigned char *mp3buf, const int mp3buf_size)
2000 {
2001 /* input is assumed to be normalized to +/- 32768 for full scale */
2002 return lame_encode_buffer_template(gfp, pcm_l, pcm_r, nsamples, mp3buf, mp3buf_size, pcm_long_type, 1, 1.0);
2003 }
2004
2005
2006
2007 int
2008 lame_encode_buffer_interleaved(lame_global_flags * gfp,
2009 short int pcm[], int nsamples,
2010 unsigned char *mp3buf, int mp3buf_size)
2011 {
2012 /* input is assumed to be normalized to +/- MAX_SHORT for full scale */
2013 return lame_encode_buffer_template(gfp, pcm, pcm+1, nsamples, mp3buf, mp3buf_size, pcm_short_type, 2, 1.0);
2014 }
2015
2016
2017 int
2018 lame_encode_buffer_interleaved_int(lame_t gfp,
2019 const int pcm[], const int nsamples,
2020 unsigned char *mp3buf, const int mp3buf_size)
2021 {
2022 /* input is assumed to be normalized to +/- MAX(int) for full scale */
2023 FLOAT const norm = (1.0 / (1L << (8 * sizeof(int)-16)));
2024 return lame_encode_buffer_template(gfp, pcm, pcm + 1, nsamples, mp3buf, mp3buf_size, pcm_int_type, 2, norm);
2025 }
2026
2027
2028
2029
2030 /*****************************************************************
2031 Flush mp3 buffer, pad with ancillary data so last frame is complete.
2032 Reset reservoir size to 0
2033 but keep all PCM samples and MDCT data in memory
2034 This option is used to break a large file into several mp3 files
2035 that when concatenated together will decode with no gaps
2036 Because we set the reservoir=0, they will also decode seperately
2037 with no errors.
2038 *********************************************************************/
2039 int
2040 lame_encode_flush_nogap(lame_global_flags * gfp, unsigned char *mp3buffer, int mp3buffer_size)
2041 {
2042 int rc = -3;
2043 if (is_lame_global_flags_valid(gfp)) {
2044 lame_internal_flags *const gfc = gfp->internal_flags;
2045 if (is_lame_internal_flags_valid(gfc)) {
2046 flush_bitstream(gfc);
2047 /* if user specifed buffer size = 0, dont check size */
2048 if (mp3buffer_size == 0)
2049 mp3buffer_size = INT_MAX;
2050 rc = copy_buffer(gfc, mp3buffer, mp3buffer_size, 1);
2051 save_gain_values(gfc);
2052 }
2053 }
2054 return rc;
2055 }
2056
2057
2058 /* called by lame_init_params. You can also call this after flush_nogap
2059 if you want to write new id3v2 and Xing VBR tags into the bitstream */
2060 int
2061 lame_init_bitstream(lame_global_flags * gfp)
2062 {
2063 if (is_lame_global_flags_valid(gfp)) {
2064 lame_internal_flags *const gfc = gfp->internal_flags;
2065 if (gfc != 0) {
2066 gfc->ov_enc.frame_number = 0;
2067
2068 if (gfp->write_id3tag_automatic) {
2069 (void) id3tag_write_v2(gfp);
2070 }
2071 /* initialize histogram data optionally used by frontend */
2072 memset(gfc->ov_enc.bitrate_channelmode_hist, 0,
2073 sizeof(gfc->ov_enc.bitrate_channelmode_hist));
2074 memset(gfc->ov_enc.bitrate_blocktype_hist, 0,
2075 sizeof(gfc->ov_enc.bitrate_blocktype_hist));
2076
2077 gfc->ov_rpg.PeakSample = 0.0;
2078
2079 /* Write initial VBR Header to bitstream and init VBR data */
2080 if (gfc->cfg.write_lame_tag)
2081 (void) InitVbrTag(gfp);
2082
2083
2084 return 0;
2085 }
2086 }
2087 return -3;
2088 }
2089
2090 static int
2091 calc_mp3buffer_size_remaining( int mp3buffer_size, int mp3count)
2092 {
2093 /* if user specifed buffer size = 0, dont check size */
2094 if (mp3buffer_size == 0)
2095 return INT_MAX;
2096 else if (mp3buffer_size > 0 && mp3count >= 0 ) {
2097 int const mp3buffer_size_remaining = mp3buffer_size - mp3count;
2098 if (mp3buffer_size_remaining > 0) {
2099 return mp3buffer_size_remaining;
2100 }
2101 assert(mp3buffer_size_remaining >= 0);
2102 /* we reached the end of the output buffer, set to -1
2103 because we have to distinguish this case from the case above,
2104 where the caller did not specify any buffer size
2105 */
2106 return -1;
2107 }
2108 else {
2109 /* values are invalid, block writing into output buffer */
2110 return -1;
2111 }
2112 }
2113
2114 /*****************************************************************/
2115 /* flush internal PCM sample buffers, then mp3 buffers */
2116 /* then write id3 v1 tags into bitstream. */
2117 /*****************************************************************/
2118
2119 int
2120 lame_encode_flush(lame_global_flags * gfp, unsigned char *mp3buffer, int mp3buffer_size)
2121 {
2122 lame_internal_flags *gfc;
2123 SessionConfig_t const *cfg;
2124 EncStateVar_t *esv;
2125 short int buffer[2][1152];
2126 int imp3 = 0, mp3count, mp3buffer_size_remaining;
2127
2128 /* we always add POSTDELAY=288 padding to make sure granule with real
2129 * data can be complety decoded (because of 50% overlap with next granule */
2130 int end_padding;
2131 int frames_left;
2132 int samples_to_encode;
2133 int pcm_samples_per_frame;
2134 int mf_needed;
2135 int is_resampling_necessary;
2136 double resample_ratio = 1;
2137
2138 if (!is_lame_global_flags_valid(gfp)) {
2139 return -3;
2140 }
2141 gfc = gfp->internal_flags;
2142 if (!is_lame_internal_flags_valid(gfc)) {
2143 return -3;
2144 }
2145 cfg = &gfc->cfg;
2146 esv = &gfc->sv_enc;
2147
2148 /* Was flush already called? */
2149 if (esv->mf_samples_to_encode < 1) {
2150 return 0;
2151 }
2152 pcm_samples_per_frame = 576 * cfg->mode_gr;
2153 mf_needed = calcNeeded(cfg);
2154
2155 samples_to_encode = esv->mf_samples_to_encode - POSTDELAY;
2156
2157 memset(buffer, 0, sizeof(buffer));
2158 mp3count = 0;
2159
2160 is_resampling_necessary = isResamplingNecessary(cfg);
2161 if (is_resampling_necessary) {
2162 resample_ratio = (double)cfg->samplerate_in / (double)cfg->samplerate_out;
2163 /* delay due to resampling; needs to be fixed, if resampling code gets changed */
2164 samples_to_encode += 16. / resample_ratio;
2165 }
2166 end_padding = pcm_samples_per_frame - (samples_to_encode % pcm_samples_per_frame);
2167 if (end_padding < 576)
2168 end_padding += pcm_samples_per_frame;
2169 gfc->ov_enc.encoder_padding = end_padding;
2170
2171 frames_left = (samples_to_encode + end_padding) / pcm_samples_per_frame;
2172 while (frames_left > 0 && imp3 >= 0) {
2173 int const frame_num = gfc->ov_enc.frame_number;
2174 int bunch = mf_needed - esv->mf_size;
2175
2176 bunch *= resample_ratio;
2177 if (bunch > 1152) bunch = 1152;
2178 if (bunch < 1) bunch = 1;
2179
2180 mp3buffer_size_remaining = calc_mp3buffer_size_remaining(mp3buffer_size, mp3count);
2181
2182 /* send in a frame of 0 padding until all internal sample buffers
2183 * are flushed
2184 */
2185 imp3 = lame_encode_buffer(gfp, buffer[0], buffer[1], bunch,
2186 mp3buffer, mp3buffer_size_remaining);
2187 if (imp3 > 0) {
2188 mp3buffer += imp3;
2189 mp3count += imp3;
2190 }
2191 { /* even a single pcm sample can produce several frames!
2192 * for example: 1 Hz input file resampled to 8 kHz mpeg2.5
2193 */
2194 int const new_frames = gfc->ov_enc.frame_number - frame_num;
2195 if (new_frames > 0)
2196 frames_left -= new_frames;
2197 }
2198 }
2199 /* Set esv->mf_samples_to_encode to 0, so we may detect
2200 * and break loops calling it more than once in a row.
2201 */
2202 esv->mf_samples_to_encode = 0;
2203
2204 if (imp3 < 0) {
2205 /* some type of fatal error */
2206 return imp3;
2207 }
2208
2209 mp3buffer_size_remaining = calc_mp3buffer_size_remaining(mp3buffer_size, mp3count);
2210
2211 /* mp3 related stuff. bit buffer might still contain some mp3 data */
2212 flush_bitstream(gfc);
2213 imp3 = copy_buffer(gfc, mp3buffer, mp3buffer_size_remaining, 1);
2214 save_gain_values(gfc);
2215 if (imp3 < 0) {
2216 /* some type of fatal error */
2217 return imp3;
2218 }
2219 mp3buffer += imp3;
2220 mp3count += imp3;
2221 mp3buffer_size_remaining = mp3buffer_size - mp3count;
2222 /* if user specifed buffer size = 0, dont check size */
2223 if (mp3buffer_size == 0)
2224 mp3buffer_size_remaining = INT_MAX;
2225
2226 if (gfp->write_id3tag_automatic) {
2227 /* write a id3 tag to the bitstream */
2228 (void) id3tag_write_v1(gfp);
2229
2230 imp3 = copy_buffer(gfc, mp3buffer, mp3buffer_size_remaining, 0);
2231
2232 if (imp3 < 0) {
2233 return imp3;
2234 }
2235 mp3count += imp3;
2236 }
2237 #if 0
2238 {
2239 int const ed = gfc->ov_enc.encoder_delay;
2240 int const ep = gfc->ov_enc.encoder_padding;
2241 int const ns = (gfc->ov_enc.frame_number * pcm_samples_per_frame) - (ed + ep);
2242 double duration = ns;
2243 duration /= cfg->samplerate_out;
2244 MSGF(gfc, "frames=%d\n", gfc->ov_enc.frame_number);
2245 MSGF(gfc, "pcm_samples_per_frame=%d\n", pcm_samples_per_frame);
2246 MSGF(gfc, "encoder delay=%d\n", ed);
2247 MSGF(gfc, "encoder padding=%d\n", ep);
2248 MSGF(gfc, "sample count=%d (%g)\n", ns, cfg->samplerate_in * duration);
2249 MSGF(gfc, "duration=%g sec\n", duration);
2250 }
2251 #endif
2252 return mp3count;
2253 }
2254
2255 /***********************************************************************
2256 *
2257 * lame_close ()
2258 *
2259 * frees internal buffers
2260 *
2261 ***********************************************************************/
2262
2263 int
2264 lame_close(lame_global_flags * gfp)
2265 {
2266 int ret = 0;
2267 if (gfp && gfp->class_id == LAME_ID) {
2268 lame_internal_flags *const gfc = gfp->internal_flags;
2269 gfp->class_id = 0;
2270 if (NULL == gfc || gfc->class_id != LAME_ID) {
2271 ret = -3;
2272 }
2273 if (NULL != gfc) {
2274 gfc->lame_init_params_successful = 0;
2275 gfc->class_id = 0;
2276 /* this routine will free all malloc'd data in gfc, and then free gfc: */
2277 freegfc(gfc);
2278 gfp->internal_flags = NULL;
2279 }
2280 if (gfp->lame_allocated_gfp) {
2281 gfp->lame_allocated_gfp = 0;
2282 free(gfp);
2283 }
2284 }
2285 return ret;
2286 }
2287
2288 /*****************************************************************/
2289 /* flush internal mp3 buffers, and free internal buffers */
2290 /*****************************************************************/
2291 #if DEPRECATED_OR_OBSOLETE_CODE_REMOVED
2292 int CDECL
2293 lame_encode_finish(lame_global_flags * gfp, unsigned char *mp3buffer, int mp3buffer_size);
2294 #else
2295 #endif
2296
2297 int
2298 lame_encode_finish(lame_global_flags * gfp, unsigned char *mp3buffer, int mp3buffer_size)
2299 {
2300 int const ret = lame_encode_flush(gfp, mp3buffer, mp3buffer_size);
2301
2302 (void) lame_close(gfp);
2303
2304 return ret;
2305 }
2306
2307 /*****************************************************************/
2308 /* write VBR Xing header, and ID3 version 1 tag, if asked for */
2309 /*****************************************************************/
2310 void lame_mp3_tags_fid(lame_global_flags * gfp, FILE * fpStream);
2311
2312 void
2313 lame_mp3_tags_fid(lame_global_flags * gfp, FILE * fpStream)
2314 {
2315 lame_internal_flags *gfc;
2316 SessionConfig_t const *cfg;
2317 if (!is_lame_global_flags_valid(gfp)) {
2318 return;
2319 }
2320 gfc = gfp->internal_flags;
2321 if (!is_lame_internal_flags_valid(gfc)) {
2322 return;
2323 }
2324 cfg = &gfc->cfg;
2325 if (!cfg->write_lame_tag) {
2326 return;
2327 }
2328 /* Write Xing header again */
2329 if (fpStream && !fseek(fpStream, 0, SEEK_SET)) {
2330 int rc = PutVbrTag(gfp, fpStream);
2331 switch (rc) {
2332 default:
2333 /* OK */
2334 break;
2335
2336 case -1:
2337 ERRORF(gfc, "Error: could not update LAME tag.\n");
2338 break;
2339
2340 case -2:
2341 ERRORF(gfc, "Error: could not update LAME tag, file not seekable.\n");
2342 break;
2343
2344 case -3:
2345 ERRORF(gfc, "Error: could not update LAME tag, file not readable.\n");
2346 break;
2347 }
2348 }
2349 }
2350
2351
2352 static int
2353 lame_init_internal_flags(lame_internal_flags* gfc)
2354 {
2355 if (NULL == gfc)
2356 return -1;
2357
2358 gfc->cfg.vbr_min_bitrate_index = 1; /* not 0 ????? */
2359 gfc->cfg.vbr_max_bitrate_index = 13; /* not 14 ????? */
2360 gfc->cfg.decode_on_the_fly = 0;
2361 gfc->cfg.findReplayGain = 0;
2362 gfc->cfg.findPeakSample = 0;
2363
2364 gfc->sv_qnt.OldValue[0] = 180;
2365 gfc->sv_qnt.OldValue[1] = 180;
2366 gfc->sv_qnt.CurrentStep[0] = 4;
2367 gfc->sv_qnt.CurrentStep[1] = 4;
2368 gfc->sv_qnt.masking_lower = 1;
2369
2370 /* The reason for
2371 * int mf_samples_to_encode = ENCDELAY + POSTDELAY;
2372 * ENCDELAY = internal encoder delay. And then we have to add POSTDELAY=288
2373 * because of the 50% MDCT overlap. A 576 MDCT granule decodes to
2374 * 1152 samples. To synthesize the 576 samples centered under this granule
2375 * we need the previous granule for the first 288 samples (no problem), and
2376 * the next granule for the next 288 samples (not possible if this is last
2377 * granule). So we need to pad with 288 samples to make sure we can
2378 * encode the 576 samples we are interested in.
2379 */
2380 gfc->sv_enc.mf_samples_to_encode = ENCDELAY + POSTDELAY;
2381 gfc->sv_enc.mf_size = ENCDELAY - MDCTDELAY; /* we pad input with this many 0's */
2382 gfc->ov_enc.encoder_padding = 0;
2383 gfc->ov_enc.encoder_delay = ENCDELAY;
2384
2385 gfc->ov_rpg.RadioGain = 0;
2386 gfc->ov_rpg.noclipGainChange = 0;
2387 gfc->ov_rpg.noclipScale = -1.0;
2388
2389 gfc->ATH = lame_calloc(ATH_t, 1);
2390 if (NULL == gfc->ATH)
2391 return -2; /* maybe error codes should be enumerated in lame.h ?? */
2392
2393 gfc->sv_rpg.rgdata = lame_calloc(replaygain_t, 1);
2394 if (NULL == gfc->sv_rpg.rgdata) {
2395 return -2;
2396 }
2397 return 0;
2398 }
2399
2400 /* initialize mp3 encoder */
2401 #if DEPRECATED_OR_OBSOLETE_CODE_REMOVED
2402 static
2403 #else
2404 #endif
2405 int
2406 lame_init_old(lame_global_flags * gfp)
2407 {
2408 disable_FPE(); /* disable floating point exceptions */
2409
2410 memset(gfp, 0, sizeof(lame_global_flags));
2411
2412 gfp->class_id = LAME_ID;
2413
2414 /* Global flags. set defaults here for non-zero values */
2415 /* see lame.h for description */
2416 /* set integer values to -1 to mean that LAME will compute the
2417 * best value, UNLESS the calling program as set it
2418 * (and the value is no longer -1)
2419 */
2420 gfp->strict_ISO = MDB_MAXIMUM;
2421
2422 gfp->mode = NOT_SET;
2423 gfp->original = 1;
2424 gfp->samplerate_in = 44100;
2425 gfp->num_channels = 2;
2426 gfp->num_samples = MAX_U_32_NUM;
2427
2428 gfp->write_lame_tag = 1;
2429 gfp->quality = -1;
2430 gfp->short_blocks = short_block_not_set;
2431 gfp->subblock_gain = -1;
2432
2433 gfp->lowpassfreq = 0;
2434 gfp->highpassfreq = 0;
2435 gfp->lowpasswidth = -1;
2436 gfp->highpasswidth = -1;
2437
2438 gfp->VBR = vbr_off;
2439 gfp->VBR_q = 4;
2440 gfp->VBR_mean_bitrate_kbps = 128;
2441 gfp->VBR_min_bitrate_kbps = 0;
2442 gfp->VBR_max_bitrate_kbps = 0;
2443 gfp->VBR_hard_min = 0;
2444
2445 gfp->quant_comp = -1;
2446 gfp->quant_comp_short = -1;
2447
2448 gfp->msfix = -1;
2449
2450 gfp->attackthre = -1;
2451 gfp->attackthre_s = -1;
2452
2453 gfp->scale = 1;
2454 gfp->scale_left = 1;
2455 gfp->scale_right = 1;
2456
2457 gfp->ATHcurve = -1;
2458 gfp->ATHtype = -1; /* default = -1 = set in lame_init_params */
2459 /* 2 = equal loudness curve */
2460 gfp->athaa_sensitivity = 0.0; /* no offset */
2461 gfp->athaa_type = -1;
2462 gfp->useTemporal = -1;
2463 gfp->interChRatio = -1;
2464
2465 gfp->findReplayGain = 0;
2466 gfp->decode_on_the_fly = 0;
2467
2468 gfp->asm_optimizations.mmx = 1;
2469 gfp->asm_optimizations.amd3dnow = 1;
2470 gfp->asm_optimizations.sse = 1;
2471
2472 gfp->preset = 0;
2473
2474 gfp->write_id3tag_automatic = 1;
2475
2476 gfp->report.debugf = &lame_report_def;
2477 gfp->report.errorf = &lame_report_def;
2478 gfp->report.msgf = &lame_report_def;
2479
2480 gfp->internal_flags = lame_calloc(lame_internal_flags, 1);
2481
2482 if (lame_init_internal_flags(gfp->internal_flags) < 0) {
2483 freegfc(gfp->internal_flags);
2484 gfp->internal_flags = 0;
2485 return -1;
2486 }
2487 return 0;
2488 }
2489
2490
2491 lame_global_flags *
2492 lame_init(void)
2493 {
2494 lame_global_flags *gfp;
2495 int ret;
2496
2497 init_log_table();
2498
2499 gfp = lame_calloc(lame_global_flags, 1);
2500 if (gfp == NULL)
2501 return NULL;
2502
2503 ret = lame_init_old(gfp);
2504 if (ret != 0) {
2505 free(gfp);
2506 return NULL;
2507 }
2508
2509 gfp->lame_allocated_gfp = 1;
2510 return gfp;
2511 }
2512
2513
2514 /***********************************************************************
2515 *
2516 * some simple statistics
2517 *
2518 * Robert Hegemann 2000-10-11
2519 *
2520 ***********************************************************************/
2521
2522 /* histogram of used bitrate indexes:
2523 * One has to weight them to calculate the average bitrate in kbps
2524 *
2525 * bitrate indices:
2526 * there are 14 possible bitrate indices, 0 has the special meaning
2527 * "free format" which is not possible to mix with VBR and 15 is forbidden
2528 * anyway.
2529 *
2530 * stereo modes:
2531 * 0: LR number of left-right encoded frames
2532 * 1: LR-I number of left-right and intensity encoded frames
2533 * 2: MS number of mid-side encoded frames
2534 * 3: MS-I number of mid-side and intensity encoded frames
2535 *
2536 * 4: number of encoded frames
2537 *
2538 */
2539
2540 void
2541 lame_bitrate_kbps(const lame_global_flags * gfp, int bitrate_kbps[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 int i;
2548 if (cfg->free_format) {
2549 for (i = 0; i < 14; i++)
2550 bitrate_kbps[i] = -1;
2551 bitrate_kbps[0] = cfg->avg_bitrate;
2552 }
2553 else {
2554 for (i = 0; i < 14; i++)
2555 bitrate_kbps[i] = bitrate_table[cfg->version][i + 1];
2556 }
2557 }
2558 }
2559 }
2560
2561
2562 void
2563 lame_bitrate_hist(const lame_global_flags * gfp, int bitrate_count[14])
2564 {
2565 if (is_lame_global_flags_valid(gfp)) {
2566 lame_internal_flags const *const gfc = gfp->internal_flags;
2567 if (is_lame_internal_flags_valid(gfc)) {
2568 SessionConfig_t const *const cfg = &gfc->cfg;
2569 EncResult_t const *const eov = &gfc->ov_enc;
2570 int i;
2571
2572 if (cfg->free_format) {
2573 for (i = 0; i < 14; i++) {
2574 bitrate_count[i] = 0;
2575 }
2576 bitrate_count[0] = eov->bitrate_channelmode_hist[0][4];
2577 }
2578 else {
2579 for (i = 0; i < 14; i++) {
2580 bitrate_count[i] = eov->bitrate_channelmode_hist[i + 1][4];
2581 }
2582 }
2583 }
2584 }
2585 }
2586
2587
2588 void
2589 lame_stereo_mode_hist(const lame_global_flags * gfp, int stmode_count[4])
2590 {
2591 if (is_lame_global_flags_valid(gfp)) {
2592 lame_internal_flags const *const gfc = gfp->internal_flags;
2593 if (is_lame_internal_flags_valid(gfc)) {
2594 EncResult_t const *const eov = &gfc->ov_enc;
2595 int i;
2596
2597 for (i = 0; i < 4; i++) {
2598 stmode_count[i] = eov->bitrate_channelmode_hist[15][i];
2599 }
2600 }
2601 }
2602 }
2603
2604
2605
2606 void
2607 lame_bitrate_stereo_mode_hist(const lame_global_flags * gfp, int bitrate_stmode_count[14][4])
2608 {
2609 if (is_lame_global_flags_valid(gfp)) {
2610 lame_internal_flags const *const gfc = gfp->internal_flags;
2611 if (is_lame_internal_flags_valid(gfc)) {
2612 SessionConfig_t const *const cfg = &gfc->cfg;
2613 EncResult_t const *const eov = &gfc->ov_enc;
2614 int i;
2615 int j;
2616
2617 if (cfg->free_format) {
2618 for (j = 0; j < 14; j++)
2619 for (i = 0; i < 4; i++) {
2620 bitrate_stmode_count[j][i] = 0;
2621 }
2622 for (i = 0; i < 4; i++) {
2623 bitrate_stmode_count[0][i] = eov->bitrate_channelmode_hist[0][i];
2624 }
2625 }
2626 else {
2627 for (j = 0; j < 14; j++) {
2628 for (i = 0; i < 4; i++) {
2629 bitrate_stmode_count[j][i] = eov->bitrate_channelmode_hist[j + 1][i];
2630 }
2631 }
2632 }
2633 }
2634 }
2635 }
2636
2637
2638 void
2639 lame_block_type_hist(const lame_global_flags * gfp, int btype_count[6])
2640 {
2641 if (is_lame_global_flags_valid(gfp)) {
2642 lame_internal_flags const *const gfc = gfp->internal_flags;
2643 if (is_lame_internal_flags_valid(gfc)) {
2644 EncResult_t const *const eov = &gfc->ov_enc;
2645 int i;
2646
2647 for (i = 0; i < 6; ++i) {
2648 btype_count[i] = eov->bitrate_blocktype_hist[15][i];
2649 }
2650 }
2651 }
2652 }
2653
2654
2655
2656 void
2657 lame_bitrate_block_type_hist(const lame_global_flags * gfp, int bitrate_btype_count[14][6])
2658 {
2659 if (is_lame_global_flags_valid(gfp)) {
2660 lame_internal_flags const *const gfc = gfp->internal_flags;
2661 if (is_lame_internal_flags_valid(gfc)) {
2662 SessionConfig_t const *const cfg = &gfc->cfg;
2663 EncResult_t const *const eov = &gfc->ov_enc;
2664 int i, j;
2665
2666 if (cfg->free_format) {
2667 for (j = 0; j < 14; ++j) {
2668 for (i = 0; i < 6; ++i) {
2669 bitrate_btype_count[j][i] = 0;
2670 }
2671 }
2672 for (i = 0; i < 6; ++i) {
2673 bitrate_btype_count[0][i] = eov->bitrate_blocktype_hist[0][i];
2674 }
2675 }
2676 else {
2677 for (j = 0; j < 14; ++j) {
2678 for (i = 0; i < 6; ++i) {
2679 bitrate_btype_count[j][i] = eov->bitrate_blocktype_hist[j + 1][i];
2680 }
2681 }
2682 }
2683 }
2684 }
2685 }
2686
2687 /* end of lame.c */