1 /*
2 * Command line parsing related functions
3 *
4 * Copyright (c) 1999 Mark Taylor
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22 /* $Id: parse.c,v 1.68 2001/03/11 11:24:25 aleidinger Exp $ */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <ctype.h>
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32
33 #include "lame.h"
34 #include "brhist.h"
35 #include "parse.h"
36 #include "main.h"
37 #include "get_audio.h"
38
39 /* GLOBAL VARIABLES. set by parse_args() */
40 /* we need to clean this up */
41 sound_file_format input_format;
42 int swapbytes; /* force byte swapping default=0*/
43 int silent;
44 int brhist;
45 float update_interval; /* to use Frank's time status display */
46 int mp3_delay; /* to adjust the number of samples truncated
47 during decode */
48 int mp3_delay_set; /* user specified the value of the mp3 encoder
49 delay to assume for decoding */
50
51 /*
52 * license - Writes version and license to the file specified by fp
53 */
54 static int
lame_version_print(FILE * const fp)55 lame_version_print(FILE*const fp)
56 {
57 const char *v = get_lame_version();
58 const char *u = get_lame_url();
59 const int lenv = strlen(v);
60 const int lenu = strlen (u);
61 const int lw = 80; /* line width of terminal in characters */
62 const int sw = 16; /* static width of text */
63
64 if (lw >= lenv + lenu + sw || lw < lenu + 2)
65 /* text fits in 80 chars/line, or line even too small for url */
66 fprintf(fp, "mp3enc (from lame version %s (%s))\n\n", v, u);
67 else
68 /* text too long, wrap url into next line, right aligned */
69 fprintf(fp, "mp3enc (from lame version %s)\n%*s(%s)\n\n",
70 v, lw - 2 - lenu, "", u);
71 return 0;
72 }
73
74 /* print version & license */
75 int
print_license(const lame_global_flags * gfp,FILE * const fp,const char * ProgramName)76 print_license(const lame_global_flags*gfp, FILE*const fp, const char*ProgramName)
77 {
78 lame_version_print(fp);
79 fprintf(fp, "Can I use LAME in my commercial program?\n\n"
80 "Yes, you can, under the restrictions of the LGPL. In particular, you\n"
81 "can include a compiled version of the LAME library (for example,\n"
82 "lame.dll) with a commercial program. Some notable requirements of\n"
83 "the LGPL:\n"
84 "\n"
85 "1. In your program, you cannot include any source code from LAME, with\n"
86 " the exception of files whose only purpose is to describe the library\n"
87 " interface (such as lame.h).\n"
88 "\n"
89 "2. Any modifications of LAME must be released under the LGPL.\n"
90 " The LAME project (www.mp3dev.org) would appreciate being\n"
91 " notified of any modifications.\n"
92 "\n"
93 "3. You must give prominent notice that your program is:\n"
94 " A. using LAME (including version number)\n"
95 " B. LAME is under the LGPL\n"
96 " C. Provide a copy of the LGPL. (the file COPYING contains the LGPL)\n"
97 " D. Provide a copy of LAME source, or a pointer where the LAME\n"
98 " source can be obtained (such as www.mp3dev.org)\n"
99 " An example of prominent notice would be an \"About the LAME encoding engine\"\n"
100 " button in some pull down menu within the executable of your program.\n"
101 "\n"
102 "4. If you determine that distribution of LAME requires a patent license,\n"
103 " you must obtain such license.\n"
104 "\n"
105 "\n"
106 "*** IMPORTANT NOTE ***\n"
107 "\n"
108 "The decoding functions provided in LAME use the mpglib decoding engine which\n"
109 "is under the GPL. They may not be used by any program not released under the\n"
110 "GPL unless you obtain such permission from the MPG123 project (www.mpg123.de).\n"
111 "\n");
112 return 0;
113 }
114
115 /*
116 * usage - Writes command line syntax to the file specified by fp
117 */
118 /* print general syntax */
119 int
usage(const lame_global_flags * gfp,FILE * const fp,const char * ProgramName)120 usage(const lame_global_flags*gfp, FILE*const fp, const char*ProgramName)
121 {
122 lame_version_print(fp);
123 fprintf(fp, "usage: %s [options] <infile> [outfile]\n\n"
124 " <infile> and/or <outfile> can be \"-\", which means stdin/stdout.\n"
125 "\n"
126 "Try \"%s --help\" for more information\n"
127 " or \"%s --longhelp\"\n"
128 " or \"%s -?\" for a complete options list\n\n",
129 ProgramName, ProgramName, ProgramName, ProgramName);
130 return 0;
131 }
132
133 /*
134 * short_help - Writes command line syntax to the file specified by fp
135 * but only the most important ones, to fit on a vt100 terminal
136 */
137 /* print short syntax help */
138 int
short_help(const lame_global_flags * gfp,FILE * const fp,const char * ProgramName)139 short_help(const lame_global_flags*gfp, FILE*const fp, const char*ProgramName)
140 {
141 lame_version_print(fp);
142 fprintf(fp, "usage: %s [options] <infile> [outfile]\n\n"
143 " <infile> and/or <outfile> can be \"-\", which means stdin/stdout.\n"
144 "\n"
145 "RECOMMENDED:\n"
146 " mp3enc -h input.wav output.mp3\n"
147 "\n"
148 "OPTIONS:\n"
149 " -b bitrate set the bitrate, default 128 kbps\n"
150 " -f fast mode (lower quality)\n"
151 " -h higher quality, but a little slower. Recommended.\n"
152 " -k keep ALL frequencies (disables all filters)\n"
153 " Can cause ringing and twinkling\n"
154 " -m mode (s)tereo, (j)oint, (m)ono or (a)uto\n"
155 " default is (j) or (s) depending on bitrate\n"
156 " -V n quality setting for VBR. default n=%i\n"
157 "\n"
158 " --preset type type must be phone, voice, fm, tape, hifi, cd or studio\n"
159 " \"--preset help\" gives some more infos on these\n"
160 "\n"
161 " --longhelp full list of options\n"
162 "\n",
163 ProgramName, gfp->VBR_q);
164 return 0;
165 }
166
167 /*
168 * wait_for - Writes command line syntax to the file specified by fp
169 */
170 static void
wait_for(FILE * const fp,int lessmode)171 wait_for (FILE*const fp, int lessmode)
172 {
173 if (lessmode ) {
174 fflush(fp);
175 getchar ();
176 } else
177 fprintf(fp, "\n");
178 fprintf(fp, "\n");
179 }
180
181 /* print long syntax help */
182 int
long_help(const lame_global_flags * gfp,FILE * const fp,const char * ProgramName,int lessmode)183 long_help(const lame_global_flags*gfp, FILE*const fp, const char*ProgramName, int lessmode)
184 {
185 lame_version_print(fp);
186 fprintf(fp, "usage: %s [options] <infile> [outfile]\n\n"
187 " <infile> and/or <outfile> can be \"-\", which means stdin/stdout.\n"
188 "\n"
189 "RECOMMENDED:\n"
190 " mp3enc -h input.wav output.mp3\n"
191 "\n"
192 "OPTIONS:\n"
193 " Input options:\n"
194 " -r input is raw pcm\n"
195 " -x force byte-swapping of input\n"
196 " -s sfreq sampling frequency of input file (kHz) - default 44.1 kHz\n"
197 " --mp1input input file is a MPEG Layer I file\n"
198 " --mp2input input file is a MPEG Layer II file\n"
199 " --mp3input input file is a MPEG Layer III file\n"
200 " --ogginput input file is a Ogg Vorbis file",
201 ProgramName);
202 wait_for (fp, lessmode);
203 fprintf(fp, " Operational options:\n"
204 " -m <mode> (s)tereo, (j)oint, (f)orce, (m)ono or (a)auto \n"
205 " default is (s) or (j) depending on bitrate\n"
206 " force = force ms_stereo on all frames.\n"
207 " auto = jstereo, with varialbe mid/side threshold\n"
208 " -a downmix from stereo to mono file for mono encoding\n"
209 " -d allow channels to have different blocktypes\n"
210 " --disptime <arg>print progress report every arg seconds\n"
211 " --ogg encode to Ogg Vorbis instead of MP3\n"
212 " --freeformat produce a free format bitstream\n"
213 " --decode input=mp3 file, output=wav\n"
214 " -t disable writing wav header when using --decode\n"
215 " --comp <arg> choose bitrate to achive a compression ratio of <arg>\n"
216 " --scale <arg> scale input (multiply PCM data) by <arg>\n"
217 " --athonly only use the ATH for masking\n"
218 " --noath disable the ATH for masking\n"
219 " --athlower x lower the ATH x dB\n"
220 " --notemp disable temporal masking effect\n"
221 " --short use short blocks\n"
222 " --noshort do not use short blocks\n"
223 " --voice experimental voice mode\n"
224 " --preset type type must be phone, voice, fm, tape, hifi, cd or studio\n"
225 " \"--preset help\" gives some more infos on these");
226 wait_for (fp, lessmode);
227 fprintf(fp, " Verbosity:\n"
228 " -S don't print progress report, VBR histograms\n"
229 " --silent don't print anything on screen\n"
230 " --quiet don't print anything on screen\n"
231 " --verbose print a lot of useful information, default\n"
232 "\n"
233 " Noise shaping & psycho acoustic algorithms:\n"
234 " -q <arg> <arg> = 0...9. Default -q 5 \n"
235 " -q 0: Highest quality, very slow \n"
236 " -q 9: Poor quality, but fast \n"
237 " -h Same as -q 2. Recommended.\n"
238 " -f Same as -q 7. Fast, ok quality\n");
239 wait_for (fp, lessmode);
240 fprintf(fp, " CBR (constant bitrate, the default) options:\n"
241 " -b <bitrate> set the bitrate in kbps, default 128 kbps\n"
242 "\n"
243 " ABR options:\n"
244 " --abr <bitrate> specify average bitrate desired (instead of quality)\n"
245 "\n"
246 " VBR options:\n"
247 " -v use variable bitrate (VBR) (--vbr-old)\n"
248 " --vbr-old use old variable bitrate (VBR) routine\n"
249 " --vbr-new use new variable bitrate (VBR) routine\n"
250 " --vbr-mtrh a merger of old and new (VBR) routine\n"
251 " -V n quality setting for VBR. default n=%i\n"
252 " 0=high quality,bigger files. 9=smaller files\n"
253 " -b <bitrate> specify minimum allowed bitrate, default 32 kbps\n"
254 " -B <bitrate> specify maximum allowed bitrate, default 320 kbps\n"
255 " -F strictly enforce the -b option, for use with players that\n"
256 " do not support low bitrate mp3 (Apex AD600-A DVD/mp3 player)\n"
257 " -t disable writing Xing VBR informational tag\n"
258 " --nohist disable VBR histogram display", gfp->VBR_q);
259
260 wait_for (fp, lessmode);
261 fprintf(fp, " MP3 header/stream options:\n"
262 " -e <emp> de-emphasis n/5/c (obsolete)\n"
263 " -c mark as copyright\n"
264 " -o mark as non-original\n"
265 " -p error protection. adds 16 bit checksum to every frame\n"
266 " (the checksum is computed correctly)\n"
267 " --nores disable the bit reservoir\n"
268 " --strictly-enforce-ISO comply as much as possible to ISO MPEG spec\n"
269 "\n"
270 " Filter options:\n"
271 " -k keep ALL frequencies (disables all filters),\n"
272 " Can cause ringing and twinkling\n"
273 " --lowpass <freq> frequency(kHz), lowpass filter cutoff above freq\n"
274 " --lowpass-width <freq> frequency(kHz) - default 15%% of lowpass freq\n"
275 " --highpass <freq> frequency(kHz), highpass filter cutoff below freq\n"
276 " --highpass-width <freq> frequency(kHz) - default 15%% of highpass freq\n"
277 " --resample <sfreq> sampling frequency of output file(kHz)- default=automatic\n"
278 " --cwlimit <freq> compute tonality up to freq (in kHz) default 8.8717");
279
280 wait_for (fp, lessmode);
281 fprintf(fp,
282 " ID3 tag options:\n"
283 " --tt <title> audio/song title (max 30 chars for version 1 tag)\n"
284 " --ta <artist> audio/song artist (max 30 chars for version 1 tag)\n"
285 " --tl <album> audio/song album (max 30 chars for version 1 tag)\n"
286 " --ty <year> audio/song year of issue (1 to 9999)\n"
287 " --tc <comment> user-defined text (max 30 chars for v1 tag, 28 for v1.1)\n"
288 " --tn <track> audio/song track number (1 to 255, creates v1.1 tag)\n"
289 " --tg <genre> audio/song genre (name or number in list)\n"
290 " --add-id3v2 force addition of version 2 tag\n"
291 " --id3v1-only add only a version 1 tag\n"
292 " --id3v2-only add only a version 2 tag\n"
293 " --space-id3v1 pad version 1 tag with spaces instead of nulls\n"
294 " --pad-id3v2 pad version 2 tag with extra 128 bytes\n"
295 " --genre-list print alphabetically sorted ID3 genre list and exit\n"
296 "\n"
297 " Note: A version 2 tag will NOT be added unless one of the input fields\n"
298 " won't fit in a version 1 tag (e.g. the title string is longer than 30\n"
299 " characters), or the '--add-id3v2' or '--id3v2-only' options are used,\n"
300 " or output is redirected to stdout."
301 #if defined(HAVE_VORBIS)
302 "\n\n"
303 " Note: All '--t*' options (except those for track and genre) work for Ogg\n"
304 " Vorbis output, but other ID3-specific options are ignored."
305 #endif
306 );
307 wait_for (fp, lessmode);
308 display_bitrates(fp);
309 return 0;
310 }
311
312 static void
display_bitrate(FILE * const fp,const char * const version,const int div,const int index)313 display_bitrate(FILE*const fp, const char*const version, const int div, const int index)
314 {
315 int i;
316
317 fprintf(fp, "\nMPEG-%-3s layer III sample frequencies (kHz): %2d %2d %g\n"
318 "bitrates (kbps):",
319 version, 32 / div, 48 / div, 44.1 / div);
320 for (i = 1; i <= 14; i++)
321 fprintf(fp, " %2i", bitrate_table [index] [i]);
322 fprintf(fp, "\n");
323 }
324
325 int
display_bitrates(FILE * const fp)326 display_bitrates(FILE*const fp)
327 {
328 display_bitrate(fp, "1" , 1, 1);
329 display_bitrate(fp, "2" , 2, 0);
330 display_bitrate(fp, "2.5", 4, 0);
331 fprintf(fp, "\n");
332 fflush(fp);
333 return 0;
334 }
335
336 typedef struct {
337 const char*name; /* name of preset */
338 long resample; /* resample frequency in Hz, or -1 for no resampling */
339 short highpass_freq; /* highpass frequency in Hz, or -1 for no highpass filtering */
340 short lowpass_freq; /* lowpass frequency in Hz, or -1 for no lowpass filtering */
341 short lowpass_width; /* lowpass width in Hz */
342 signed char no_short_blocks; /* use of short blocks, 1: no, 0: yes */
343 signed char quality; /* quality, the same as -f or -h */
344 MPEG_mode mode; /* channel mode (mono, stereo, joint) */
345 short cbr; /* CBR data rate in kbps (8...320) */
346 signed char xvbr_mode; /* VBR mode (0...9) */
347 short vbr_min; /* minimum VBR rate in kbps(8...256) */
348 short vbr_max; /* maximum VBR rate in kbps (16...320) */
349 } preset_t;
350
351 const preset_t Presets [] = {
352 /* name fs fu fo dfo shrt qual mode cbr vbr_mode/min/max */
353 { "phone" , 8000, 125, 3400, 0, 1, 5, MONO , 16, 6, 8, 24 }, /* phone standard 300-3400 */
354 { "phon+" , 11025, 100, 4000, 0, 1, 5, MONO , 24, 4, 16, 32 }, /* phone theoretical limits */
355 { "lw" , 11025, -1, 4000, 0, 0, 5, MONO , 24, 3, 16, 56 }, /* LW */
356 { "mw-eu" , 11025, -1, 4000, 0, 0, 5, MONO , 24, 3, 16, 56 }, /* MW in europe */
357 { "mw-us" , 16000, -1, 7500, 0, 0, 5, MONO , 40, 3, 24, 112 }, /* MW in U.S.A. */
358 { "sw" , 11025, -1, 4000, 0, 0, 5, MONO , 24, 3, 16, 56 }, /* SW */
359 { "fm" , 32000, -1, 15000, 0, 0, 3, JOINT_STEREO, 112, 3, 80, 256 },
360 { "voice" , 24000, -1, 12000, 0, 1, 5, MONO , 56, 4, 40, 112 },
361 { "radio" , -1, -1, 15000, 0, 0, 3, JOINT_STEREO, 128, 3, 96, 256 },
362 { "tape" , -1, -1, 18000, 900, 0, 3, JOINT_STEREO, 128, 3, 96, 256 },
363 { "hifi" , -1, -1, 18000, 900, 0, -1, JOINT_STEREO, 160, 2, 112, 320 },
364 { "cd" , -1, -1, -1, -1, 0, -1, STEREO , 192, 1, 128, 320 },
365 { "studio", -1, -1, -1, -1, 0, -1, STEREO , 256, 0, 160, 320 },
366 };
367
368 /*
369 * Writes presetting info to #stdout#
370 */
371
372 /* print possible combination */
373 static int
presets_info(const lame_global_flags * gfp,FILE * const fp,const char * ProgramName)374 presets_info(const lame_global_flags*gfp, FILE*const fp, const char*ProgramName)
375 {
376 int i;
377
378 fprintf(fp, "\n");
379 lame_version_print(fp);
380
381 fprintf(fp, "Presets are some shortcuts for common settings.\n");
382 fprintf(fp, "They can be combined with -v if you want VBR MP3s.\n");
383
384 fprintf(fp, "\n ");
385 for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)
386 fprintf(fp, strlen(Presets[i].name) <= 4? "%5s ": " %-5s",
387 Presets[i].name);
388 fprintf(fp, "\n=================");
389 for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)
390 fprintf(fp, "======");
391 fprintf(fp, "\n--resample ");
392 for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)
393 if (Presets[i].resample < 0)
394 fprintf(fp, " ");
395 else
396 fprintf(fp, "%6.3g", Presets[i].resample * 1.e-3);
397 fprintf(fp, "\n--highpass ");
398 for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)
399 if (Presets[i].highpass_freq < 0)
400 fprintf(fp, " ");
401 else
402 fprintf(fp, "%6.3g", Presets[i].highpass_freq * 1.e-3);
403 fprintf(fp, "\n--lowpass ");
404 for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)
405 if (Presets[i].lowpass_freq < 0)
406 fprintf(fp, " ");
407 else
408 fprintf(fp, "%6.3g", Presets[i].lowpass_freq * 1.e-3);
409 fprintf(fp, "\n--lowpass-width ");
410 for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)
411 if (Presets[i].lowpass_width < 0)
412 fprintf(fp, " ");
413 else
414 fprintf(fp, "%6.3g", Presets[i].lowpass_width * 1.e-3);
415 fprintf(fp, "\n--noshort ");
416 for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)
417 switch (Presets[i].no_short_blocks ) {
418 case 1:
419 fprintf(fp, " yes");
420 break;
421 case 0:
422 fprintf(fp, " no");
423 break;
424 case -1:
425 fprintf(fp, " ");
426 break;
427 default:
428 assert (0);
429 break;
430 }
431 fprintf(fp, "\n ");
432 for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)
433 switch (Presets[i].mode ) {
434 case MONO:
435 fprintf(fp, " -mm");
436 break;
437 case JOINT_STEREO:
438 fprintf(fp, " -mj");
439 break;
440 case STEREO:
441 fprintf(fp, " -ms");
442 break;
443 case -1:
444 fprintf(fp, " ");
445 break;
446 default:
447 assert (0);
448 break;
449 }
450 fprintf(fp, "\n ");
451 for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)
452 switch (Presets[i].quality ) {
453 case -1:
454 fprintf(fp, " ");
455 break;
456 case 2:
457 fprintf(fp, " -h");
458 break;
459 case 3:
460 fprintf(fp, " -q3");
461 break;
462 case 5:
463 fprintf(fp, " ");
464 break;
465 case 7:
466 fprintf(fp, " -f");
467 break;
468 default:
469 assert (0);
470 break;
471 }
472 fprintf(fp, "\n-b ");
473 for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)
474 fprintf(fp, "%6u", Presets[i].cbr);
475 fprintf(fp, "\n-- PLUS WITH -v ");
476 for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)
477 fprintf(fp, "------");
478 fprintf(fp, "-\n-V ");
479 for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)
480 fprintf(fp, "%6u", Presets[i].xvbr_mode);
481 fprintf(fp, "\n-b ");
482 for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)
483 fprintf(fp, "%6u", Presets[i].vbr_min);
484 fprintf(fp, "\n-B ");
485 for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)
486 fprintf(fp, "%6u", Presets[i].vbr_max);
487 fprintf(fp, "\n----------------");
488 for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)
489 fprintf(fp, "------");
490
491 fprintf(fp, "-\nEXAMPLES:\n");
492 fprintf(fp, " a) --preset fm\n");
493 fprintf(fp, " equal to: -mj -b112 --resample 32 --lowpass 15 --lowpass-width 0\n");
494 fprintf(fp, " b) -v --preset studio\n");
495 fprintf(fp, " equals to: -h -ms -V0 -b160 -B320\n");
496
497 return 0;
498 }
499
500
501 static int
presets_setup(lame_global_flags * gfp,const char * preset_name,const char * ProgramName)502 presets_setup(lame_global_flags*gfp, const char*preset_name, const char*ProgramName)
503 {
504 int i;
505
506 for (i = 0; i < sizeof Presets / sizeof *Presets; i++)
507 if (0 == strncmp(preset_name, Presets[i].name, strlen(preset_name))) {
508 if (Presets[i].resample >= 0)
509 (void) lame_set_out_samplerate(gfp,
510 Presets[i].resample);
511 if (Presets[i].highpass_freq >= 0)
512 gfp ->highpassfreq = Presets[i].highpass_freq,
513 gfp ->highpasswidth = 0;
514 gfp ->lowpassfreq = Presets[i].lowpass_freq;
515 gfp ->lowpasswidth = Presets[i].lowpass_width;
516 gfp ->no_short_blocks = Presets[i].no_short_blocks;
517 (void) lame_set_quality(gfp, Presets[i].quality);
518 (void) lame_set_mode(gfp, Presets[i].mode);
519 gfp ->brate = Presets[i].cbr;
520 gfp ->VBR_q = Presets[i].xvbr_mode;
521 gfp ->VBR_min_bitrate_kbps = Presets[i].vbr_min;
522 gfp ->VBR_max_bitrate_kbps = Presets[i].vbr_max;
523 return 0;
524 }
525 presets_info(gfp, stderr, ProgramName);
526 return -1;
527 }
528
529 static void
genre_list_handler(int num,const char * name,void * cookie)530 genre_list_handler(int num, const char *name, void *cookie)
531 {
532 printf("%3d %s\n", num, name);
533 }
534
535 /*
536 * parse_args - Sets encoding parameters to the specifications of the
537 * command line. Default settings are used for parameters
538 * not specified in the command line.
539 *
540 * If the input file is in WAVE or AIFF format, the sampling frequency is read
541 * from the AIFF header.
542 *
543 * The input and output filenames are read into #inpath# and #outpath#.
544 */
545
546 /* would use real "strcasecmp" but it isn't portable */
547 static int
local_strcasecmp(const char * s1,const char * s2)548 local_strcasecmp(const char*s1, const char*s2)
549 {
550 unsigned char c1, c2;
551
552 do {
553 c1 = tolower(*s1);
554 c2 = tolower(*s2);
555 if (!c1)
556 break;
557 ++s1;
558 ++s2;
559 } while (c1 == c2);
560 return c1 - c2;
561 }
562
563 /*
564 * LAME is a simple frontend which just uses the file extension
565 * to determine the file type. Trying to analyze the file
566 * contents is well beyond the scope of LAME and should not be added.
567 */
568 static int
filename_to_type(const char * FileName)569 filename_to_type(const char*FileName)
570 {
571 int len = strlen(FileName);
572
573 if (len < 4)
574 return sf_unknown;
575
576 FileName += len - 4;
577 if (0 == local_strcasecmp(FileName, ".mpg" ))
578 return sf_mp1;
579 if (0 == local_strcasecmp(FileName, ".mp1" ))
580 return sf_mp1;
581 if (0 == local_strcasecmp(FileName, ".mp2" ))
582 return sf_mp2;
583 if (0 == local_strcasecmp(FileName, ".mp3" ))
584 return sf_mp3;
585 if (0 == local_strcasecmp(FileName, ".ogg" ))
586 return sf_ogg;
587 if (0 == local_strcasecmp(FileName, ".wav" ))
588 return sf_wave;
589 if (0 == local_strcasecmp(FileName, ".aif" ))
590 return sf_aiff;
591 if (0 == local_strcasecmp(FileName, ".raw" ))
592 return sf_raw;
593 return sf_unknown;
594 }
595
596 static int
resample_rate(double freq)597 resample_rate(double freq)
598 {
599 if (freq >= 1.e3)
600 freq *= 1.e-3;
601
602 switch ((int)freq) {
603 case 8:
604 return 8000;
605 case 11:
606 return 11025;
607 case 12:
608 return 12000;
609 case 16:
610 return 16000;
611 case 22:
612 return 22050;
613 case 24:
614 return 24000;
615 case 32:
616 return 32000;
617 case 44:
618 return 44100;
619 case 48:
620 return 48000;
621 default:
622 fprintf(stderr, "Illegal resample frequency: %.3f kHz\n", freq);
623 return 0;
624 }
625 }
626
627 /* Ugly, NOT final version */
628
629 #define T_IF(str) if (0 == local_strcasecmp (token,str) ) {
630 #define T_ELIF(str) } else if (0 == local_strcasecmp (token,str) ) {
631 #define T_ELIF2(str1,str2) } else if (0 == local_strcasecmp(token,str1) || \
632 0 == local_strcasecmp(token,str2)) {
633 #define T_ELSE } else {
634 #define T_END }
635
636 int
parse_args(lame_global_flags * gfp,int argc,char ** argv)637 parse_args(lame_global_flags*gfp, int argc, char **argv)
638 {
639 int err, i, autoconvert = 0;
640 double val;
641 const char *ProgramName = argv[0];
642
643 /* turn on display options. user settings may turn them off below */
644 silent = 0;
645 brhist = 1;
646 mp3_delay = 0;
647 mp3_delay_set = 0;
648 id3tag_init(gfp);
649
650 /* process args */
651 for (i = 0, err = 0; ++i < argc && !err; ) {
652 char c;
653 char *token, *arg, *nextArg;
654 int argUsed;
655
656 token = argv[i];
657 if (*token++ == '-' ) {
658 argUsed = 0;
659 nextArg = i + 1 < argc? argv[i+1]: "";
660
661 if (*token == '-') { /* GNU style */
662 token++;
663
664 T_IF ("resample")
665 argUsed = 1;
666 (void) lame_set_out_samplerate(gfp,
667 resample_rate(atof (nextArg) ));
668
669 T_ELIF ("vbr-old")
670 gfp->VBR = vbr_rh;
671
672 T_ELIF ("vbr-new")
673 gfp->VBR = vbr_mt;
674
675 T_ELIF ("vbr-mtrh")
676 gfp->VBR = vbr_mtrh;
677
678 T_ELIF ("r3mix")
679 gfp->VBR = vbr_rh;
680 gfp->VBR_q = 1;
681 (void) lame_set_quality(gfp, 2);
682 gfp->lowpassfreq = 19500;
683 (void) lame_set_mode(gfp, JOINT_STEREO);
684 gfp->ATHtype = 3;
685 gfp->VBR_min_bitrate_kbps = 64;
686
687 T_ELIF ("abr")
688 argUsed = 1;
689 gfp->VBR = vbr_abr;
690 gfp->VBR_mean_bitrate_kbps = atoi(nextArg);
691 /*
692 * values larger than 8000 are bps (like
693 * Fraunhofer), so it's strange to get 320000
694 * bps MP3 when specifying 8000 bps MP3.
695 */
696 if (gfp ->VBR_mean_bitrate_kbps >= 8000)
697 gfp->VBR_mean_bitrate_kbps = (gfp->VBR_mean_bitrate_kbps + 500 )/1000;
698 gfp->VBR_mean_bitrate_kbps = Min(gfp->VBR_mean_bitrate_kbps, 320);
699 gfp->VBR_mean_bitrate_kbps = Max(gfp->VBR_mean_bitrate_kbps, 8);
700
701 T_ELIF ("mp1input")
702 input_format = sf_mp1;
703
704 T_ELIF ("mp2input")
705 input_format = sf_mp2;
706
707 T_ELIF ("mp3input")
708 input_format = sf_mp3;
709
710 T_ELIF ("ogginput")
711 #if defined(HAVE_VORBIS)
712 input_format = sf_ogg;
713 #else
714 fprintf(stderr, "Error: mp3enc not compiled with Vorbis support\n");
715 return -1;
716 #endif
717 T_ELIF ("ogg")
718 #if defined(HAVE_VORBIS)
719 (void) lame_set_ogg(gfp, 1);
720 #else
721 fprintf(stderr, "Error: mp3enc not compiled with Vorbis support\n");
722 return -1;
723 #endif
724 T_ELIF ("phone")
725 if (presets_setup(gfp, token, ProgramName ) < 0)
726 return -1;
727
728 T_ELIF ("voice")
729 if (presets_setup(gfp, token, ProgramName ) < 0)
730 return -1;
731
732 T_ELIF ("radio")
733 if (presets_setup(gfp, token, ProgramName ) < 0)
734 return -1;
735
736 T_ELIF ("tape")
737 if (presets_setup(gfp, token, ProgramName ) < 0)
738 return -1;
739
740 T_ELIF ("cd")
741 if (presets_setup(gfp, token, ProgramName ) < 0)
742 return -1;
743
744 T_ELIF ("studio")
745 if (presets_setup(gfp, token, ProgramName ) < 0)
746 return -1;
747
748 T_ELIF ("noshort")
749 gfp->no_short_blocks = 1;
750
751 T_ELIF ("short")
752 gfp->no_short_blocks = 0;
753
754 T_ELIF ("decode")
755 (void) lame_set_decode_only(gfp, 1);
756
757 T_ELIF ("decode-mp3delay")
758 mp3_delay = atoi(nextArg);
759 mp3_delay_set = 1;
760 argUsed = 1;
761
762 T_ELIF ("noath")
763 gfp->noATH = 1;
764
765 T_ELIF ("nores")
766 gfp->disable_reservoir = 1;
767 gfp->padding_type = 0;
768
769 T_ELIF ("strictly-enforce-ISO")
770 gfp->strict_ISO = 1;
771
772 T_ELIF ("athonly")
773 gfp->ATHonly = 1;
774
775 T_ELIF ("athlower")
776 argUsed = 1;
777 gfp->ATHlower = atof(nextArg);
778
779 T_ELIF ("athtype")
780 argUsed = 1;
781 gfp->ATHtype = atoi(nextArg);
782
783 T_ELIF ("scale")
784 argUsed = 1;
785 (void) lame_set_scale(gfp, atof(nextArg));
786
787 T_ELIF ("freeformat")
788 gfp->free_format = 1;
789
790 T_ELIF ("athshort")
791 gfp->ATHshort = 1;
792
793 T_ELIF ("nohist")
794 brhist = 0;
795
796 /* options for ID3 tag */
797 T_ELIF ("tt")
798 argUsed = 1;
799 id3tag_set_title(gfp, nextArg);
800
801 T_ELIF ("ta")
802 argUsed = 1;
803 id3tag_set_artist(gfp, nextArg);
804
805 T_ELIF ("tl")
806 argUsed = 1;
807 id3tag_set_album(gfp, nextArg);
808
809 T_ELIF ("ty")
810 argUsed = 1;
811 id3tag_set_year(gfp, nextArg);
812
813 T_ELIF ("tc")
814 argUsed = 1;
815 id3tag_set_comment(gfp, nextArg);
816
817 T_ELIF ("tn")
818 argUsed = 1;
819 id3tag_set_track(gfp, nextArg);
820
821 T_ELIF ("tg")
822 argUsed = 1;
823 if (id3tag_set_genre(gfp, nextArg)) {
824 fprintf(stderr, "Unknown genre: %s. Specify genre name or number\n", nextArg);
825 return -1;
826 }
827
828 T_ELIF ("add-id3v2")
829 id3tag_add_v2(gfp);
830
831 T_ELIF ("id3v1-only")
832 id3tag_v1_only(gfp);
833
834 T_ELIF ("id3v2-only")
835 id3tag_v2_only(gfp);
836
837 T_ELIF ("space-id3v1")
838 id3tag_space_v1(gfp);
839
840 T_ELIF ("pad-id3v2")
841 id3tag_pad_v2(gfp);
842
843 T_ELIF ("genre-list")
844 id3tag_genre_list(genre_list_handler, NULL);
845 return - 2;
846
847 T_ELIF ("lowpass")
848 val = atof(nextArg);
849 argUsed = 1;
850 /* useful are 0.001 kHz...50 kHz, 50 Hz...50000 Hz */
851 gfp ->lowpassfreq = val * (val < 50. ? 1.e3 : 1.e0 ) + 0.5;
852 if (val < 0.001 || val > 50000. ) {
853 fprintf(stderr, "Must specify lowpass with --lowpass freq, freq >= 0.001 kHz\n");
854 return -1;
855 }
856
857 T_ELIF ("lowpass-width")
858 argUsed = 1;
859 gfp->lowpasswidth = 1000.0 * atof(nextArg ) + 0.5;
860 if (gfp->lowpasswidth < 0) {
861 fprintf(stderr, "Must specify lowpass width with --lowpass-width freq, freq >= 0 kHz\n");
862 return -1;
863 }
864
865 T_ELIF ("highpass")
866 val = atof(nextArg);
867 argUsed = 1;
868 /* useful are 0.001 kHz...16 kHz, 16 Hz...50000 Hz */
869 gfp->highpassfreq = val * (val < 16. ? 1.e3 : 1.e0 ) + 0.5;
870 if (val < 0.001 || val > 50000. ) {
871 fprintf(stderr, "Must specify highpass with --highpass freq, freq >= 0.001 kHz\n");
872 return -1;
873 }
874
875 T_ELIF ("highpass-width")
876 argUsed = 1;
877 gfp->highpasswidth = 1000.0 * atof(nextArg ) + 0.5;
878 if (gfp->highpasswidth < 0) {
879 fprintf(stderr, "Must specify highpass width with --highpass-width freq, freq >= 0 kHz\n");
880 return -1;
881 }
882
883 T_ELIF ("cwlimit")
884 val = atof (nextArg);
885 argUsed = 1;
886 /* useful are 0.001 kHz...50 kHz, 50 Hz...50000 Hz */
887 gfp ->cwlimit = val *(val <= 50. ? 1.e3 : 1.e0);
888 if (gfp->cwlimit <= 0 ) {
889 fprintf(stderr, "Must specify cwlimit with --cwlimit freq, freq >= 0.001 kHz\n");
890 return -1;
891 }
892
893 T_ELIF ("comp")
894 argUsed = 1;
895 gfp->compression_ratio = atof(nextArg);
896 if (gfp->compression_ratio < 1.0 ) {
897 fprintf(stderr, "Must specify compression ratio >= 1.0\n");
898 return -1;
899 }
900
901 T_ELIF ("notemp")
902 gfp->useTemporal = 0;
903
904 T_ELIF ("nspsytune")
905 gfp->exp_nspsytune |= 1;
906 gfp->experimentalZ = 1;
907 gfp->experimentalX = 1;
908
909 T_ELIF ("nssafejoint")
910 gfp->exp_nspsytune |= 2;
911
912 T_ELIF ("ns-bass")
913 argUsed = 1;
914 {
915 double d;
916 int k;
917 d = atof(nextArg);
918 k = (int)(d * 4);
919 if (k < -32)
920 k = -32;
921 if (k > 31)
922 k = 31;
923 if (k < 0)
924 k += 64;
925 gfp->exp_nspsytune |= (k << 2);
926 }
927
928 T_ELIF ("ns-alto")
929 argUsed = 1;
930 {
931 double d;
932 int k;
933 d = atof(nextArg);
934 k = (int)(d * 4);
935 if (k < -32)
936 k = -32;
937 if (k > 31)
938 k = 31;
939 if (k < 0)
940 k += 64;
941 gfp->exp_nspsytune |= (k << 8);
942 }
943
944 T_ELIF ("ns-treble")
945 argUsed = 1;
946 {
947 double d;
948 int k;
949 d = atof(nextArg);
950 k = (int)(d * 4);
951 if (k < -32)
952 k = -32;
953 if (k > 31)
954 k = 31;
955 if (k < 0)
956 k += 64;
957 gfp->exp_nspsytune |= (k << 14);
958 }
959
960 /* some more GNU-ish options could be added
961 * brief => few messages on screen (name, status report)
962 * o/output file => specifies output filename
963 * O => stdout
964 * i/input file => specifies input filename
965 * I => stdin
966 */
967 T_ELIF2 ("quiet", "silent")
968 silent = 10; /* on a scale from 1 to 10 be very silent */
969
970 T_ELIF ("verbose")
971 silent = 0; /* print a lot on screen, the default */
972
973 T_ELIF2 ("version", "license")
974 print_license(gfp, stdout, ProgramName);
975 return - 2;
976
977 T_ELIF2 ("help", "usage")
978 short_help(gfp, stdout, ProgramName);
979 return - 2;
980
981 T_ELIF ("longhelp")
982 long_help(gfp, stdout, ProgramName, 0 /* lessmode=NO */);
983 return - 2;
984
985 T_ELIF ("?")
986 long_help(gfp, stdout, ProgramName, 1 /* lessmode=YES */);
987 return - 2;
988
989 T_ELIF ("preset")
990 argUsed = 1;
991 if (presets_setup(gfp, nextArg, ProgramName ) < 0)
992 return -1;
993
994 T_ELIF ("disptime")
995 argUsed = 1;
996 update_interval = atof (nextArg);
997
998 T_ELSE
999 fprintf(stderr, "%s: unrec option --%s\n", ProgramName, token);
1000
1001 T_END
1002 i += argUsed;
1003 } else {
1004 while ((c = *token++) != '\0' ) {
1005 arg = *token ? token : nextArg;
1006 switch (c) {
1007 case 'm':
1008 argUsed = 1;
1009
1010 switch (*arg ) {
1011 case 's':
1012 (void) lame_set_mode(gfp, STEREO);
1013 break;
1014 case 'd':
1015 (void) lame_set_mode(gfp, DUAL_CHANNEL);
1016 fprintf(stderr,
1017 "%s: dual channel is not supported yet, the result (perhaps stereo)\n"
1018 " may not be what you expect\n",
1019 ProgramName);
1020 break;
1021 case 'f':
1022 gfp->force_ms = 1;
1023 /* FALLTHROUGH */
1024 case 'j':
1025 (void) lame_set_mode(gfp, JOINT_STEREO);
1026 break;
1027 case 'm':
1028 (void) lame_set_mode(gfp, MONO);
1029 break;
1030 case 'a':
1031 (void) lame_set_mode_automs(gfp, 1);
1032 /* lame picks mode & uses variable MS threshold */
1033 break;
1034 default :
1035 fprintf(stderr, "%s: -m mode must be s/d/j/f/m not %s\n", ProgramName, arg);
1036 err = 1;
1037 break;
1038 }
1039 break;
1040
1041 case 'V':
1042 argUsed = 1;
1043 /* to change VBR default look in lame.h */
1044 if (gfp->VBR == vbr_off)
1045 gfp->VBR = vbr_default;
1046 gfp->VBR_q = atoi(arg);
1047 if (gfp->VBR_q < 0)
1048 gfp->VBR_q = 0;
1049 if (gfp->VBR_q > 9)
1050 gfp->VBR_q = 9;
1051 break;
1052 case 'v':
1053 /* to change VBR default look in lame.h */
1054 if (gfp->VBR == vbr_off)
1055 gfp->VBR = vbr_default;
1056 break;
1057
1058 case 'q':
1059 argUsed = 1;
1060 {
1061 int tmp_quality = atoi(arg);
1062
1063 /* XXX should we move this into lame_set_quality()? */
1064 if (tmp_quality < 0)
1065 tmp_quality = 0;
1066 if (tmp_quality > 9)
1067 tmp_quality = 9;
1068
1069 (void) lame_set_quality(gfp, tmp_quality);
1070 }
1071 break;
1072 case 'f':
1073 (void) lame_set_quality(gfp, 7);
1074 break;
1075 case 'h':
1076 (void) lame_set_quality(gfp, 2);
1077 break;
1078
1079 case 's':
1080 argUsed = 1;
1081 val = atof(arg);
1082 (void) lame_set_in_samplerate(gfp,
1083 val *(val <= 192 ? 1.e3 : 1.e0 ) + 0.5);
1084 break;
1085 case 'b':
1086 argUsed = 1;
1087 gfp->brate = atoi(arg);
1088 gfp->VBR_min_bitrate_kbps = gfp->brate;
1089 break;
1090 case 'B':
1091 argUsed = 1;
1092 gfp->VBR_max_bitrate_kbps = atoi(arg);
1093 break;
1094 case 'F':
1095 gfp->VBR_hard_min = 1;
1096 break;
1097 case 't': /* dont write VBR tag */
1098 (void) lame_set_bWriteVbrTag(gfp, 0);
1099 (void) lame_set_disable_waveheader(gfp, 1);
1100 break;
1101 case 'r': /* force raw pcm input file */
1102 #if defined(LIBSNDFILE)
1103 fprintf(stderr, "WARNING: libsndfile may ignore -r and perform fseek's on the input.\n"
1104 "Compile without libsndfile if this is a problem.\n");
1105 #endif
1106 input_format = sf_raw;
1107 break;
1108 case 'x': /* force byte swapping */
1109 swapbytes = 1;
1110 break;
1111 case 'p': /* (jo) error_protection: add crc16 information to stream */
1112 gfp->error_protection = 1;
1113 break;
1114 case 'a': /* autoconvert input file from stereo to mono - for mono mp3 encoding */
1115 autoconvert = 1;
1116 (void) lame_set_mode(gfp, MONO);
1117 break;
1118 case 'k':
1119 gfp->lowpassfreq = -1;
1120 gfp->highpassfreq = -1;
1121 break;
1122 case 'd':
1123 gfp->allow_diff_short = 1;
1124 break;
1125 case 'S':
1126 silent = 1;
1127 break;
1128 case 'X':
1129 argUsed = 1;
1130 gfp->experimentalX = atoi(arg);
1131 break;
1132 case 'Y':
1133 gfp->experimentalY = 1;
1134 break;
1135 case 'Z':
1136 gfp->experimentalZ = 1;
1137 break;
1138 #if defined(HAVE_GTK)
1139 case 'g': /* turn on gtk analysis */
1140 (void) lame_set_analysis(gfp, 1);
1141 break;
1142 #endif
1143 case 'e':
1144 argUsed = 1;
1145
1146 switch (*arg) {
1147 case 'n':
1148 gfp ->emphasis = 0;
1149 break;
1150 case '5':
1151 gfp ->emphasis = 1;
1152 break;
1153 case 'c':
1154 gfp ->emphasis = 3;
1155 break;
1156 default :
1157 fprintf(stderr, "%s: -e emp must be n/5/c not %s\n", ProgramName, arg);
1158 err = 1;
1159 break;
1160 }
1161 break;
1162 case 'c':
1163 gfp->copyright = 1;
1164 break;
1165 case 'o':
1166 gfp->original = 0;
1167 break;
1168
1169 case '?':
1170 long_help(gfp, stderr, ProgramName, 0 /* LESSMODE=NO */);
1171 return -1;
1172
1173 default:
1174 fprintf(stderr, "%s: unrec option %c\n", ProgramName, c);
1175 err = 1;
1176 break;
1177 }
1178 if (argUsed) {
1179 if (arg == token)
1180 token = ""; /* no more from token */
1181 else
1182 ++i; /* skip arg we used */
1183 arg = "";
1184 argUsed = 0;
1185 }
1186 }
1187 }
1188 } else {
1189 fprintf(stderr, "%s: excess arg %s\n", ProgramName, argv[i]);
1190 err = 1;
1191 }
1192 }
1193
1194 if (err) {
1195 usage(gfp, stderr, ProgramName);
1196 return -1;
1197 }
1198
1199 // if (inPath[0] == '-')
1200 silent = 1; /* turn off status - it's broken for stdin */
1201
1202 /* some file options not allowed with stdout */
1203 // if (outPath[0] == '-')
1204 (void) lame_set_bWriteVbrTag(gfp, 0); /* turn off VBR tag */
1205
1206 #if !(defined HAVE_MPGLIB || defined AMIGA_MPEGA)
1207 if (input_format == sf_mp1 ||
1208 input_format == sf_mp2 ||
1209 input_format == sf_mp3) {
1210 fprintf(stderr, "Error: libmp3lame not compiled with mpg123 *decoding* support \n");
1211 return -1;
1212 }
1213 #endif
1214
1215 #if !(defined HAVE_VORBIS)
1216 if (input_format == sf_ogg) {
1217 fprintf(stderr, "Error: mp3enc not compiled with Vorbis support\n");
1218 return -1;
1219 }
1220 #endif
1221 /* default guess for number of channels */
1222 if (autoconvert)
1223 (void) lame_set_num_channels(gfp, 2);
1224 else if (MONO == lame_get_mode(gfp ))
1225 (void) lame_set_num_channels(gfp, 1);
1226 else
1227 (void) lame_set_num_channels(gfp, 2);
1228
1229 if (gfp->free_format) {
1230 if (gfp ->brate < 8 || gfp ->brate > 640) {
1231 fprintf(stderr, "For free format, specify a bitrate between 8 and 640 kbps\n");
1232 return -1;
1233 }
1234 }
1235 return 0;
1236 }
1237