Debian Codegraph @

frontend/parse.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * Command line parsing related functions
3 *
4 * Copyright (c) 1999 Mark Taylor
5 * 2000-2017 Robert Hegemann
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23 /* $Id$ */
24
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28
29 #include <assert.h>
30 #include <ctype.h>
31 #include <math.h>
32
33 #ifdef STDC_HEADERS
34 # include <stdio.h>
35 # include <stdlib.h>
36 # include <string.h>
37 #else
38 # ifndef HAVE_STRCHR
39 # define strchr index
40 # define strrchr rindex
41 # endif
42 char *strchr(), *strrchr();
43 # ifndef HAVE_MEMCPY
44 # define memcpy(d, s, n) bcopy ((s), (d), (n))
45 # define memmove(d, s, n) bcopy ((s), (d), (n))
46 # endif
47 #endif
48
49
50 #ifdef HAVE_LIMITS_H
51 # include <limits.h>
52 #endif
53
54 #include "lame.h"
55
56 #include "parse.h"
57 #include "main.h"
58 #include "get_audio.h"
59 #include "version.h"
60 #include "console.h"
61
62 #undef dimension_of
63 #define dimension_of(array) (sizeof(array)/sizeof(array[0]))
64
65 #ifdef WITH_DMALLOC
66 #include <dmalloc.h>
67 #endif
68
69
70 #ifdef HAVE_ICONV
71 #include <iconv.h>
72 #include <errno.h>
73 #ifdef HAVE_LANGINFO_H
74 #include <locale.h>
75 #include <langinfo.h>
76 #endif
77 #if defined(__KLIBC__) && !defined(iconv_open)
78 /* kLIBC iconv_open() does not support UTF-16LE and //TRANSLIT */
79 static iconv_t os2_iconv_open (const char *tocode, const char *fromcode)
80 {
81 char to[strlen(tocode) + 1];
82 char from[strlen(fromcode) + 1];
83 char *p;
84
85 strcpy(to, tocode);
86 strcpy(from, fromcode);
87
88 if (!strncmp(to, "UTF-16", 6))
89 {
90 strcpy(to, "UCS-2");
91 memmove(to + 5, to + 6, strlen(to + 6));
92 }
93
94 p = strstr(to, "//");
95 if (p)
96 *p = '\0';
97
98 if (!strncmp(from, "UTF-16", 6))
99 {
100 strcpy(from, "UCS-2");
101 memmove(from + 5, from + 6, strlen(from + 6));
102 }
103
104 p = strstr(from, "//");
105 if (p)
106 *p = '\0';
107
108 return iconv_open(to, from);
109 }
110
111 #define iconv_open(t, f) os2_iconv_open(t, f)
112 #endif /* KLIBC iconv */
113 #endif /* HAVE_ICONV */
114
115 #if defined _ALLOW_INTERNAL_OPTIONS
116 #define INTERNAL_OPTS 1
117 #else
118 #define INTERNAL_OPTS 0
119 #endif
120
121 #if (INTERNAL_OPTS!=0)
122 #include "set_get.h"
123 #define DEV_HELP(a) a
124 #else
125 #define DEV_HELP(a)
126 #define lame_set_tune(a,b) (void)0
127 #define lame_set_short_threshold(a,b,c) (void)0
128 #define lame_set_maskingadjust(a,b) (void)0
129 #define lame_set_maskingadjust_short(a,b) (void)0
130 #define lame_set_ATHcurve(a,b) (void)0
131 #define lame_set_preset_notune(a,b) (void)0
132 #define lame_set_substep(a,b) (void)0
133 #define lame_set_subblock_gain(a,b) (void)0
134 #define lame_set_sfscale(a,b) (void)0
135 #endif
136
137 static int const lame_alpha_version_enabled = LAME_ALPHA_VERSION;
138 static int const internal_opts_enabled = INTERNAL_OPTS;
139
140 /* GLOBAL VARIABLES. set by parse_args() */
141 /* we need to clean this up */
142
143 ReaderConfig global_reader = { sf_unknown, 0, 0, 0, 0 };
144 WriterConfig global_writer = { 0 };
145
146 UiConfig global_ui_config = {0,0,0,0};
147
148 DecoderConfig global_decoder;
149
150 RawPCMConfig global_raw_pcm =
151 { /* in_bitwidth */ 16
152 , /* in_signed */ -1
153 , /* in_endian */ ByteOrderLittleEndian
154 };
155
156
157
158 /* possible text encodings */
159 typedef enum TextEncoding
160 { TENC_RAW /* bytes will be stored as-is into ID3 tags, which are Latin1 per definition */
161 , TENC_LATIN1 /* text will be converted from local encoding to Latin1, as ID3 needs it */
162 , TENC_UTF16 /* text will be converted from local encoding to Unicode (UCS-2), as ID3v2 wants it */
163 , TENC_UTF8 /* text will be converted from local encoding to UTF-8, as ID3v2.4 wants it */
164 } TextEncoding;
165
166 #ifdef HAVE_ICONV
167 #define ID3TAGS_EXTENDED
168 /* search for Zero termination in multi-byte strings */
169 static size_t
170 strlenMultiByte(char const* str, size_t w)
171 {
172 size_t n = 0;
173 if (str != 0) {
174 size_t i;
175 for (n = 0; ; ++n) {
176 size_t x = 0;
177 for (i = 0; i < w; ++i) {
178 x += *str++ == 0 ? 1 : 0;
179 }
180 if (x == w) {
181 break;
182 }
183 }
184 }
185 return n;
186 }
187
188 static char*
189 currentCharacterEncoding()
190 {
191 #ifdef HAVE_LANGINFO_H
192 char* cur_code = nl_langinfo(CODESET);
193 #else
194 char* env_lang = getenv("LANG");
195 char* xxx_code = env_lang == NULL ? NULL : strrchr(env_lang, '.');
196 char* cur_code = xxx_code == NULL ? "" : xxx_code+1;
197 #endif
198 return cur_code;
199 }
200
201 static size_t
202 currCharCodeSize(void)
203 {
204 size_t n = 1;
205 char dst[32];
206 char* src = "A";
207 char* cur_code = currentCharacterEncoding();
208 iconv_t xiconv = iconv_open(cur_code, "ISO_8859-1");
209 if (xiconv != (iconv_t)-1) {
210 for (n = 0; n < 32; ++n) {
211 char* i_ptr = src;
212 char* o_ptr = dst;
213 size_t srcln = 1;
214 size_t avail = n;
215 size_t rc = iconv(xiconv, &i_ptr, &srcln, &o_ptr, &avail);
216 if (rc != (size_t)-1) {
217 break;
218 }
219 }
220 iconv_close(xiconv);
221 }
222 return n;
223 }
224
225 #if 0
226 static
227 char* fromLatin1( char* src )
228 {
229 char* dst = 0;
230 if (src != 0) {
231 size_t const l = strlen(src);
232 size_t const n = l*4;
233 dst = calloc(n+4, 4);
234 if (dst != 0) {
235 char* cur_code = currentCharacterEncoding();
236 iconv_t xiconv = iconv_open(cur_code, "ISO_8859-1");
237 if (xiconv != (iconv_t)-1) {
238 char* i_ptr = src;
239 char* o_ptr = dst;
240 size_t srcln = l;
241 size_t avail = n;
242 iconv(xiconv, &i_ptr, &srcln, &o_ptr, &avail);
243 iconv_close(xiconv);
244 }
245 }
246 }
247 return dst;
248 }
249
250 static
251 char* fromUtf16( char* src )
252 {
253 char* dst = 0;
254 if (src != 0) {
255 size_t const l = strlenMultiByte(src, 2);
256 size_t const n = l*4;
257 dst = calloc(n+4, 4);
258 if (dst != 0) {
259 char* cur_code = currentCharacterEncoding();
260 iconv_t xiconv = iconv_open(cur_code, "UTF-16LE");
261 if (xiconv != (iconv_t)-1) {
262 char* i_ptr = (char*)src;
263 char* o_ptr = dst;
264 size_t srcln = l*2;
265 size_t avail = n;
266 iconv(xiconv, &i_ptr, &srcln, &o_ptr, &avail);
267 iconv_close(xiconv);
268 }
269 }
270 }
271 return dst;
272 }
273 #endif
274
275 static
276 char* toLatin1( char* src )
277 {
278 size_t w = currCharCodeSize();
279 char* dst = 0;
280 if (src != 0) {
281 size_t const l = strlenMultiByte(src, w);
282 size_t const n = l*4;
283 dst = calloc(n+4, 4);
284 if (dst != 0) {
285 char* cur_code = currentCharacterEncoding();
286 iconv_t xiconv = iconv_open("ISO_8859-1//TRANSLIT", cur_code);
287 if (xiconv != (iconv_t)-1) {
288 char* i_ptr = (char*)src;
289 char* o_ptr = dst;
290 size_t srcln = l*w;
291 size_t avail = n;
292 iconv(xiconv, &i_ptr, &srcln, &o_ptr, &avail);
293 iconv_close(xiconv);
294 }
295 }
296 }
297 return dst;
298 }
299
300
301 static
302 char* toUtf8( char* src )
303 {
304 size_t w = currCharCodeSize();
305 char* dst = 0;
306 if (src != 0) {
307 /* XXX: size calculation for UTF-16, a bit too much for UTF-8, but not too small */
308 size_t const l = strlenMultiByte(src, w);
309 size_t const n = (l+1)*4;
310 dst = calloc(n+4, 4);
311 if (dst != 0) {
312 char* cur_code = currentCharacterEncoding();
313 iconv_t xiconv = iconv_open("UTF-8//TRANSLIT", cur_code);
314 if (xiconv != (iconv_t)-1) {
315 char* i_ptr = (char*)src;
316 char* o_ptr = &dst[0];
317 size_t srcln = l*w;
318 size_t avail = n;
319 iconv(xiconv, &i_ptr, &srcln, &o_ptr, &avail);
320 iconv_close(xiconv);
321 }
322 }
323 }
324 return dst;
325
326 }
327
328
329 static
330 char* toUtf16( char* src )
331 {
332 size_t w = currCharCodeSize();
333 char* dst = 0;
334 if (src != 0) {
335 size_t const l = strlenMultiByte(src, w);
336 size_t const n = (l+1)*4;
337 dst = calloc(n+4, 4);
338 if (dst != 0) {
339 char* cur_code = currentCharacterEncoding();
340 iconv_t xiconv = iconv_open("UTF-16LE//TRANSLIT", cur_code);
341 dst[0] = 0xff;
342 dst[1] = 0xfe;
343 if (xiconv != (iconv_t)-1) {
344 char* i_ptr = (char*)src;
345 char* o_ptr = &dst[2];
346 size_t srcln = l*w;
347 size_t avail = n;
348 iconv(xiconv, &i_ptr, &srcln, &o_ptr, &avail);
349 iconv_close(xiconv);
350 }
351 }
352 }
353 return dst;
354 }
355 #endif
356
357 #if defined( _WIN32 ) && !defined(__MINGW32__)
358 #define ID3TAGS_EXTENDED
359
360 char* toLatin1(char const* s)
361 {
362 return utf8ToLatin1(s);
363 }
364
365 unsigned short* toUtf16(char const* s)
366 {
367 return utf8ToUtf16(s);
368 }
369
370 char* toUtf8(char const* s)
371 {
372 return local8BitToUtf8(s);
373 }
374 #endif
375
376 static int evaluateArgument(char const* token, char const* arg, char* _EndPtr)
377 {
378 if (arg != 0 && arg != _EndPtr)
379 return 1;
380 error_printf("WARNING: argument missing for '%s'\n", token);
381 return 0;
382 }
383
384 static int getDoubleValue(char const* token, char const* arg, double* ptr)
385 {
386 char *_EndPtr=0;
387 double d = strtod(arg, &_EndPtr);
388 if (ptr != 0) {
389 *ptr = d;
390 }
391 return evaluateArgument(token, arg, _EndPtr);
392 }
393
394 static int getIntValue(char const* token, char const* arg, int* ptr)
395 {
396 char *_EndPtr=0;
397 long d = strtol(arg, &_EndPtr, 10);
398 if (ptr != 0) {
399 *ptr = d;
400 }
401 return evaluateArgument(token, arg, _EndPtr);
402 }
403
404 #ifdef ID3TAGS_EXTENDED
405 static int
406 set_id3v2tag(lame_global_flags* gfp, TextEncoding enc, int type, unsigned short const* str)
407 {
408 switch (enc)
409 {
410 case TENC_UTF8:
411 switch (type)
412 {
413 case 'a': return id3tag_set_textinfo_utf8(gfp, "TPE1", str);
414 case 't': return id3tag_set_textinfo_utf8(gfp, "TIT2", str);
415 case 'l': return id3tag_set_textinfo_utf8(gfp, "TALB", str);
416 case 'g': return id3tag_set_textinfo_utf8(gfp, "TCON", str);
417 case 'c': return id3tag_set_comment_ucs2(gfp, 0, 0, str);
418 case 'n': return id3tag_set_textinfo_utf8(gfp, "TRCK", str);
419 case 'y': return id3tag_set_textinfo_utf8(gfp, "TYER", str);
420 case 'v': return id3tag_set_fieldvalue_ucs2(gfp, str);
421 }
422 ;;
423 case TENC_UTF16:
424 switch (type)
425 {
426 case 'a': return id3tag_set_textinfo_utf16(gfp, "TPE1", str);
427 case 't': return id3tag_set_textinfo_utf16(gfp, "TIT2", str);
428 case 'l': return id3tag_set_textinfo_utf16(gfp, "TALB", str);
429 case 'g': return id3tag_set_textinfo_utf16(gfp, "TCON", str);
430 case 'c': return id3tag_set_comment_utf16(gfp, 0, 0, str);
431 case 'n': return id3tag_set_textinfo_utf16(gfp, "TRCK", str);
432 case 'y': return id3tag_set_textinfo_utf16(gfp, "TYER", str);
433 case 'v': return id3tag_set_fieldvalue_utf16(gfp, str);
434 }
435 ;;
436 default:
437 return -3;
438 }
439 }
440 #endif
441
442 static int
443 set_id3tag(lame_global_flags* gfp, int type, char const* str)
444 {
445 switch (type)
446 {
447 case 'a': return id3tag_set_artist(gfp, str), 0;
448 case 't': return id3tag_set_title(gfp, str), 0;
449 case 'l': return id3tag_set_album(gfp, str), 0;
450 case 'g': return id3tag_set_genre(gfp, str);
451 case 'c': return id3tag_set_comment(gfp, str), 0;
452 case 'n': return id3tag_set_track(gfp, str);
453 case 'y': return id3tag_set_year(gfp, str), 0;
454 case 'v': return id3tag_set_fieldvalue(gfp, str);
455 }
456 return 0;
457 }
458
459 static int
460 id3_tag(lame_global_flags* gfp, int type, TextEncoding enc, char* str)
461 {
462 void* x = 0;
463 int result;
464 if ((enc == TENC_UTF16 || enc == TENC_UTF8) && type != 'v' ) {
465 id3_tag(gfp, type, TENC_LATIN1, str); /* for id3v1 */
466 }
467 switch (enc)
468 {
469 default:
470 #ifdef ID3TAGS_EXTENDED
471 case TENC_LATIN1: x = toLatin1(str); break;
472 case TENC_UTF16: x = toUtf16(str); break;
473 case TENC_UTF8: x = toUtf8(str); break;
474 #else
475 case TENC_RAW: x = strdup(str); break;
476 #endif
477 }
478 switch (enc)
479 {
480 default:
481 #ifdef ID3TAGS_EXTENDED
482 case TENC_LATIN1: result = set_id3tag(gfp, type, x); break;
483 case TENC_UTF16: result = set_id3v2tag(gfp, enc, type, x); break;
484 case TENC_UTF8: result = set_id3v2tag(gfp, enc, type, x); break;
485 #else
486 case TENC_RAW: result = set_id3tag(gfp, type, x); break;
487 #endif
488 }
489 free(x);
490 return result;
491 }
492
493
494
495
496 /************************************************************************
497 *
498 * license
499 *
500 * PURPOSE: Writes version and license to the file specified by fp
501 *
502 ************************************************************************/
503
504 static int
505 lame_version_print(FILE * const fp)
506 {
507 const char *b = get_lame_os_bitness();
508 const char *v = get_lame_version();
509 const char *u = get_lame_url();
510 const size_t lenb = strlen(b);
511 const size_t lenv = strlen(v);
512 const size_t lenu = strlen(u);
513 const size_t lw = 80; /* line width of terminal in characters */
514 const size_t sw = 16; /* static width of text */
515
516 if (lw >= lenb + lenv + lenu + sw || lw < lenu + 2)
517 /* text fits in 80 chars per line, or line even too small for url */
518 if (lenb > 0)
519 fprintf(fp, "LAME %s version %s (%s)\n\n", b, v, u);
520 else
521 fprintf(fp, "LAME version %s (%s)\n\n", v, u);
522 else {
523 int const n_white_spaces = (int)((lenu+2) > lw ? 0 : lw-2-lenu);
524 /* text too long, wrap url into next line, right aligned */
525 if (lenb > 0)
526 fprintf(fp, "LAME %s version %s\n%*s(%s)\n\n", b, v, n_white_spaces, "", u);
527 else
528 fprintf(fp, "LAME version %s\n%*s(%s)\n\n", v, n_white_spaces, "", u);
529 }
530 if (lame_alpha_version_enabled)
531 fprintf(fp, "warning: alpha versions should be used for testing only\n\n");
532
533
534 return 0;
535 }
536
537 static int
538 print_license(FILE * const fp)
539 { /* print version & license */
540 lame_version_print(fp);
541 fprintf(fp,
542 "Copyright (c) 1999-2011 by The LAME Project\n"
543 "Copyright (c) 1999,2000,2001 by Mark Taylor\n"
544 "Copyright (c) 1998 by Michael Cheng\n"
545 "Copyright (c) 1995,1996,1997 by Michael Hipp: mpglib\n" "\n");
546 fprintf(fp,
547 "This library is free software; you can redistribute it and/or\n"
548 "modify it under the terms of the GNU Library General Public\n"
549 "License as published by the Free Software Foundation; either\n"
550 "version 2 of the License, or (at your option) any later version.\n"
551 "\n");
552 fprintf(fp,
553 "This library is distributed in the hope that it will be useful,\n"
554 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
555 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
556 "Library General Public License for more details.\n"
557 "\n");
558 fprintf(fp,
559 "You should have received a copy of the GNU Library General Public\n"
560 "License along with this program. If not, see\n"
561 "<http://www.gnu.org/licenses/>.\n");
562 return 0;
563 }
564
565
566 /************************************************************************
567 *
568 * usage
569 *
570 * PURPOSE: Writes command line syntax to the file specified by fp
571 *
572 ************************************************************************/
573
574 int
575 usage(FILE * const fp, const char *ProgramName)
576 { /* print general syntax */
577 lame_version_print(fp);
578 fprintf(fp,
579 "usage: %s [options] <infile> [outfile]\n"
580 "\n"
581 " <infile> and/or <outfile> can be \"-\", which means stdin/stdout.\n"
582 "\n"
583 "Try:\n"
584 " \"%s --help\" for general usage information\n"
585 " or:\n"
586 " \"%s --preset help\" for information on suggested predefined settings\n"
587 " or:\n"
588 " \"%s --longhelp\"\n"
589 " or \"%s -?\" for a complete options list\n\n",
590 ProgramName, ProgramName, ProgramName, ProgramName, ProgramName);
591 return 0;
592 }
593
594
595 /************************************************************************
596 *
597 * usage
598 *
599 * PURPOSE: Writes command line syntax to the file specified by fp
600 * but only the most important ones, to fit on a vt100 terminal
601 *
602 ************************************************************************/
603
604 int
605 short_help(const lame_global_flags * gfp, FILE * const fp, const char *ProgramName)
606 { /* print short syntax help */
607 lame_version_print(fp);
608 fprintf(fp,
609 "usage: %s [options] <infile> [outfile]\n"
610 "\n"
611 " <infile> and/or <outfile> can be \"-\", which means stdin/stdout.\n"
612 "\n" "RECOMMENDED:\n" " lame -V2 input.wav output.mp3\n" "\n", ProgramName);
613 fprintf(fp,
614 "OPTIONS:\n"
615 " -b bitrate set the bitrate, default 128 kbps\n"
616 " -h higher quality, but a little slower.\n"
617 " -f fast mode (lower quality)\n"
618 " -V n quality setting for VBR. default n=%i\n"
619 " 0=high quality,bigger files. 9.999=smaller files\n",
620 lame_get_VBR_q(gfp));
621 fprintf(fp,
622 " --preset type type must be \"medium\", \"standard\", \"extreme\", \"insane\",\n"
623 " or a value for an average desired bitrate and depending\n"
624 " on the value specified, appropriate quality settings will\n"
625 " be used.\n"
626 " \"--preset help\" gives more info on these\n" "\n");
627 fprintf(fp,
628 #if defined(WIN32)
629 " --priority type sets the process priority\n"
630 " 0,1 = Low priority\n"
631 " 2 = normal priority\n"
632 " 3,4 = High priority\n" "\n"
633 #endif
634 #if defined(__OS2__)
635 " --priority type sets the process priority\n"
636 " 0 = Low priority\n"
637 " 1 = Medium priority\n"
638 " 2 = Regular priority\n"
639 " 3 = High priority\n"
640 " 4 = Maximum priority\n" "\n"
641 #endif
642 " --help id3 ID3 tagging related options\n" "\n"
643 DEV_HELP(
644 " --help dev developer options\n" "\n"
645 )
646 " --longhelp full list of options\n" "\n"
647 " --license print License information\n\n"
648 );
649
650 return 0;
651 }
652
653 /************************************************************************
654 *
655 * usage
656 *
657 * PURPOSE: Writes command line syntax to the file specified by fp
658 *
659 ************************************************************************/
660
661 static void
662 wait_for(FILE * const fp, int lessmode)
663 {
664 if (lessmode) {
665 fflush(fp);
666 getchar();
667 }
668 else {
669 fprintf(fp, "\n");
670 }
671 fprintf(fp, "\n");
672 }
673
674 static void
675 help_id3tag(FILE * const fp)
676 {
677 fprintf(fp,
678 " ID3 tag options:\n"
679 " --tt <title> audio/song title (max 30 chars for version 1 tag)\n"
680 " --ta <artist> audio/song artist (max 30 chars for version 1 tag)\n"
681 " --tl <album> audio/song album (max 30 chars for version 1 tag)\n"
682 " --ty <year> audio/song year of issue (1 to 9999)\n"
683 " --tc <comment> user-defined text (max 30 chars for v1 tag, 28 for v1.1)\n");
684 fprintf(fp,
685 " --tn <track[/total]> audio/song track number and (optionally) the total\n"
686 " number of tracks on the original recording. (track\n"
687 " and total each 1 to 255. just the track number\n"
688 " creates v1.1 tag, providing a total forces v2.0).\n");
689 fprintf(fp,
690 " --tg <genre> audio/song genre (name or number in list)\n"
691 " --ti <file> audio/song albumArt (jpeg/png/gif file, v2.3 tag)\n"
692 " --tv <id=value> user-defined frame specified by id and value (v2.3 tag)\n"
693 " syntax: --tv \"TXXX=description=content\"\n"
694 );
695 fprintf(fp,
696 " --add-id3v2 force addition of version 2 tag\n"
697 " --id3v1-only add only a version 1 tag\n"
698 " --id3v2-only add only a version 2 tag\n"
699 #ifdef ID3TAGS_EXTENDED
700 " --id3v2-utf8 add following options in unicode UTF-8 text encoding\n"
701 " --id3v2-utf16 add following options in unicode UTF-16 text encoding\n"
702 " --id3v2-latin1 add following options in latin-1 text encoding\n"
703 #endif
704 " --space-id3v1 pad version 1 tag with spaces instead of nulls\n"
705 " --pad-id3v2 same as '--pad-id3v2-size 128'\n"
706 " --pad-id3v2-size <value> adds version 2 tag, pad with extra <value> bytes\n"
707 " --genre-list print alphabetically sorted ID3 genre list and exit\n"
708 " --ignore-tag-errors ignore errors in values passed for tags\n" "\n"
709 );
710 fprintf(fp,
711 " Note: A version 2 tag will NOT be added unless one of the input fields\n"
712 " won't fit in a version 1 tag (e.g. the title string is longer than 30\n"
713 " characters), or the '--add-id3v2' or '--id3v2-only' options are used,\n"
714 " or output is redirected to stdout.\n"
715 );
716 }
717
718 static void
719 help_developer_switches(FILE * const fp)
720 {
721 if ( !internal_opts_enabled ) {
722 fprintf(fp,
723 " Note: Almost all of the following switches aren't available in this build!\n\n"
724 );
725 }
726 fprintf(fp,
727 " ATH related:\n"
728 " --noath turns ATH down to a flat noise floor\n"
729 " --athshort ignore GPSYCHO for short blocks, use ATH only\n"
730 " --athonly ignore GPSYCHO completely, use ATH only\n"
731 " --athtype n selects between different ATH types [0-4]\n"
732 " --athlower x lowers ATH by x dB\n"
733 );
734 fprintf(fp,
735 " --athaa-type n ATH auto adjust: 0 'no' else 'loudness based'\n"
736 /** OBSOLETE " --athaa-loudapprox n n=1 total energy or n=2 equal loudness curve\n"*/
737 " --athaa-sensitivity x activation offset in -/+ dB for ATH auto-adjustment\n"
738 "\n");
739 fprintf(fp,
740 " PSY related:\n"
741 " --short use short blocks when appropriate\n"
742 " --noshort do not use short blocks\n"
743 " --allshort use only short blocks\n"
744 );
745 fprintf(fp,
746 "(1) --temporal-masking x x=0 disables, x=1 enables temporal masking effect\n"
747 " --nssafejoint M/S switching criterion\n"
748 " --nsmsfix <arg> M/S switching tuning [effective 0-3.5]\n"
749 "(2) --interch x adjust inter-channel masking ratio\n"
750 " --ns-bass x adjust masking for sfbs 0 - 6 (long) 0 - 5 (short)\n"
751 " --ns-alto x adjust masking for sfbs 7 - 13 (long) 6 - 10 (short)\n"
752 " --ns-treble x adjust masking for sfbs 14 - 21 (long) 11 - 12 (short)\n"
753 );
754 fprintf(fp,
755 " --ns-sfb21 x change ns-treble by x dB for sfb21\n"
756 " --shortthreshold x,y short block switching threshold,\n"
757 " x for L/R/M channel, y for S channel\n"
758 " -Z [n] always do calculate short block maskings\n");
759 fprintf(fp,
760 " Noise Shaping related:\n"
761 "(1) --substep n use pseudo substep noise shaping method types 0-2\n"
762 "(1) -X n[,m] selects between different noise measurements\n"
763 " n for long block, m for short. if m is omitted, m = n\n"
764 " 1: CBR, ABR and VBR-old encoding modes only\n"
765 " 2: ignored\n"
766 );
767 }
768
769 int
770 long_help(const lame_global_flags * gfp, FILE * const fp, const char *ProgramName, int lessmode)
771 { /* print long syntax help */
772 lame_version_print(fp);
773 fprintf(fp,
774 "usage: %s [options] <infile> [outfile]\n"
775 "\n"
776 " <infile> and/or <outfile> can be \"-\", which means stdin/stdout.\n"
777 "\n" "RECOMMENDED:\n" " lame -V2 input.wav output.mp3\n" "\n", ProgramName);
778 fprintf(fp,
779 "OPTIONS:\n"
780 " Input options:\n"
781 " --scale <arg> scale input (multiply PCM data) by <arg>\n"
782 " --scale-l <arg> scale channel 0 (left) input (multiply PCM data) by <arg>\n"
783 " --scale-r <arg> scale channel 1 (right) input (multiply PCM data) by <arg>\n"
784 " --swap-channel swap L/R channels\n"
785 " --ignorelength ignore file length in WAV header\n"
786 " --gain <arg> apply Gain adjustment in decibels, range -20.0 to +12.0\n"
787 );
788 #if (defined HAVE_MPGLIB || defined AMIGA_MPEGA)
789 fprintf(fp,
790 " --mp1input input file is a MPEG Layer I file\n"
791 " --mp2input input file is a MPEG Layer II file\n"
792 " --mp3input input file is a MPEG Layer III file\n"
793 );
794 #endif
795 fprintf(fp,
796 " --nogap <file1> <file2> <...>\n"
797 " gapless encoding for a set of contiguous files\n"
798 " --nogapout <dir>\n"
799 " output dir for gapless encoding (must precede --nogap)\n"
800 " --nogaptags allow the use of VBR tags in gapless encoding\n"
801 " --out-dir <dir> output dir, must exist\n"
802 );
803 fprintf(fp,
804 "\n"
805 " Input options for RAW PCM:\n"
806 " -r input is raw pcm\n"
807 " -s sfreq sampling frequency of input file (kHz) - default 44.1 kHz\n"
808 " --signed input is signed (default)\n"
809 " --unsigned input is unsigned\n"
810 " --bitwidth w input bit width is w (default 16)\n"
811 " -x force byte-swapping of input\n"
812 " --little-endian input is little-endian (default)\n"
813 " --big-endian input is big-endian\n"
814 " -a downmix from stereo to mono file for mono encoding\n"
815 );
816
817 wait_for(fp, lessmode);
818 fprintf(fp,
819 " Operational options:\n"
820 " -m <mode> (j)oint, (s)imple, (f)orce, (d)ual-mono, (m)ono (l)eft (r)ight\n"
821 " default is (j)\n"
822 " joint = Uses the best possible of MS and LR stereo\n"
823 " simple = force LR stereo on all frames\n"
824 " force = force MS stereo on all frames.\n"
825 );
826 fprintf(fp,
827 " --preset type type must be \"medium\", \"standard\", \"extreme\", \"insane\",\n"
828 " or a value for an average desired bitrate and depending\n"
829 " on the value specified, appropriate quality settings will\n"
830 " be used.\n"
831 " \"--preset help\" gives more info on these\n"
832 " --comp <arg> choose bitrate to achieve a compression ratio of <arg>\n");
833 fprintf(fp, " --replaygain-fast compute RG fast but slightly inaccurately (default)\n"
834 #ifdef DECODE_ON_THE_FLY
835 " --replaygain-accurate compute RG more accurately and find the peak sample\n"
836 #endif
837 " --noreplaygain disable ReplayGain analysis\n"
838 #ifdef DECODE_ON_THE_FLY
839 " --clipdetect enable --replaygain-accurate and print a message whether\n"
840 " clipping occurs and how far the waveform is from full scale\n"
841 #endif
842 );
843 fprintf(fp,
844 " --flush flush output stream as soon as possible\n"
845 " --freeformat produce a free format bitstream\n"
846 " --decode input=mp3 file, output=wav\n"
847 " -t disable writing wav header when using --decode\n");
848
849 wait_for(fp, lessmode);
850 fprintf(fp,
851 " Verbosity:\n"
852 " --disptime <arg>print progress report every arg seconds\n"
853 " -S don't print progress report, VBR histograms\n"
854 " --nohist disable VBR histogram display\n"
855 " --quiet don't print anything on screen\n"
856 " --silent don't print anything on screen, but fatal errors\n"
857 " --brief print more useful information\n"
858 " --verbose print a lot of useful information\n" "\n");
859 fprintf(fp,
860 " Noise shaping & psycho acoustic algorithms:\n"
861 " -q <arg> <arg> = 0...9. Default -q 3 \n"
862 " -q 0: Highest quality, very slow \n"
863 " -q 9: Poor quality, but fast \n"
864 " -h Same as -q 2. \n"
865 " -f Same as -q 7. Fast, ok quality\n");
866
867 wait_for(fp, lessmode);
868 fprintf(fp,
869 " CBR (constant bitrate, the default) options:\n"
870 " -b <bitrate> set the bitrate in kbps, default 128 kbps\n"
871 " --cbr enforce use of constant bitrate\n"
872 "\n"
873 " ABR options:\n"
874 " --abr <bitrate> specify average bitrate desired (instead of quality)\n" "\n");
875 fprintf(fp,
876 " VBR options:\n"
877 " -V n quality setting for VBR. default n=%i\n"
878 " 0=high quality,bigger files. 9=smaller files\n"
879 " -v the same as -V 4\n"
880 " --vbr-old use old variable bitrate (VBR) routine\n"
881 " --vbr-new use new variable bitrate (VBR) routine (default)\n"
882 " -Y lets LAME ignore noise in sfb21, like in CBR\n"
883 " (Default for V3 to V9.999)\n"
884 ,
885 lame_get_VBR_q(gfp));
886 fprintf(fp,
887 " -b <bitrate> specify minimum allowed bitrate, default 32 kbps\n"
888 " -B <bitrate> specify maximum allowed bitrate, default 320 kbps\n"
889 " -F strictly enforce the -b option, for use with players that\n"
890 " do not support low bitrate mp3\n"
891 " -t disable writing LAME Tag\n"
892 " -T enable and force writing LAME Tag\n");
893
894 wait_for(fp, lessmode);
895 DEV_HELP(
896 help_developer_switches(fp);
897 wait_for(fp, lessmode);
898 )
899
900 fprintf(fp,
901 " MP3 header/stream options:\n"
902 " -e <emp> de-emphasis n/5/c (obsolete)\n"
903 " -c mark as copyright\n"
904 " -o mark as non-original\n"
905 " -p error protection. adds 16 bit checksum to every frame\n"
906 " (the checksum is computed correctly)\n"
907 " --nores disable the bit reservoir\n"
908 " --strictly-enforce-ISO comply as much as possible to ISO MPEG spec\n");
909 fprintf(fp,
910 " --buffer-constraint <constraint> available values for constraint:\n"
911 " default, strict, maximum\n"
912 "\n"
913 );
914 fprintf(fp,
915 " Filter options:\n"
916 " --lowpass <freq> frequency(kHz), lowpass filter cutoff above freq\n"
917 " --lowpass-width <freq> frequency(kHz) - default 15%% of lowpass freq\n"
918 " --highpass <freq> frequency(kHz), highpass filter cutoff below freq\n"
919 " --highpass-width <freq> frequency(kHz) - default 15%% of highpass freq\n");
920 fprintf(fp,
921 " --resample <sfreq> sampling frequency of output file(kHz)- default=automatic\n");
922
923 wait_for(fp, lessmode);
924 help_id3tag(fp);
925 fprintf(fp,
926 #if defined(WIN32)
927 "\n\nMS-Windows-specific options:\n"
928 " --priority <type> sets the process priority:\n"
929 " 0,1 = Low priority (IDLE_PRIORITY_CLASS)\n"
930 " 2 = normal priority (NORMAL_PRIORITY_CLASS, default)\n"
931 " 3,4 = High priority (HIGH_PRIORITY_CLASS))\n"
932 " Note: Calling '--priority' without a parameter will select priority 0.\n"
933 #endif
934 #if defined(__OS2__)
935 "\n\nOS/2-specific options:\n"
936 " --priority <type> sets the process priority:\n"
937 " 0 = Low priority (IDLE, delta = 0)\n"
938 " 1 = Medium priority (IDLE, delta = +31)\n"
939 " 2 = Regular priority (REGULAR, delta = -31)\n"
940 " 3 = High priority (REGULAR, delta = 0)\n"
941 " 4 = Maximum priority (REGULAR, delta = +31)\n"
942 " Note: Calling '--priority' without a parameter will select priority 0.\n"
943 #endif
944 "\nMisc:\n --license print License information\n\n"
945 );
946
947 #if defined(HAVE_NASM)
948 wait_for(fp, lessmode);
949 fprintf(fp,
950 " Platform specific:\n"
951 " --noasm <instructions> disable assembly optimizations for mmx/3dnow/sse\n");
952 wait_for(fp, lessmode);
953 #endif
954
955 display_bitrates(fp);
956
957 return 0;
958 }
959
960 static void
961 display_bitrate(FILE * const fp, const char *const version, const int d, const int indx)
962 {
963 int i;
964 int nBitrates = 14;
965 if (d == 4)
966 nBitrates = 8;
967
968
969 fprintf(fp,
970 "\nMPEG-%-3s layer III sample frequencies (kHz): %2d %2d %g\n"
971 "bitrates (kbps):", version, 32 / d, 48 / d, 44.1 / d);
972 for (i = 1; i <= nBitrates; i++)
973 fprintf(fp, " %2i", lame_get_bitrate(indx, i));
974 fprintf(fp, "\n");
975 }
976
977 int
978 display_bitrates(FILE * const fp)
979 {
980 display_bitrate(fp, "1", 1, 1);
981 display_bitrate(fp, "2", 2, 0);
982 display_bitrate(fp, "2.5", 4, 0);
983 fprintf(fp, "\n");
984 fflush(fp);
985 return 0;
986 }
987
988
989 /* note: for presets it would be better to externalize them in a file.
990 suggestion: lame --preset <file-name> ...
991 or: lame --preset my-setting ... and my-setting is defined in lame.ini
992 */
993
994 /*
995 Note from GB on 08/25/2002:
996 I am merging --presets and --alt-presets. Old presets are now aliases for
997 corresponding abr values from old alt-presets. This way we now have a
998 unified preset system, and I hope than more people will use the new tuned
999 presets instead of the old unmaintained ones.
1000 */
1001
1002
1003
1004 /************************************************************************
1005 *
1006 * usage
1007 *
1008 * PURPOSE: Writes presetting info to #stdout#
1009 *
1010 ************************************************************************/
1011
1012
1013 static void
1014 presets_longinfo_dm(FILE * msgfp)
1015 {
1016 fprintf(msgfp,
1017 "\n"
1018 "The --preset switches are aliases over LAME settings.\n"
1019 "\n" "\n");
1020 fprintf(msgfp,
1021 "To activate these presets:\n"
1022 "\n" " For VBR modes (generally highest quality):\n" "\n");
1023 fprintf(msgfp,
1024 " --preset medium This preset should provide near transparency to most\n"
1025 " people on most music.\n"
1026 "\n"
1027 " --preset standard This preset should generally be transparent to most\n"
1028 " people on most music and is already quite high\n"
1029 " in quality.\n" "\n");
1030 fprintf(msgfp,
1031 " --preset extreme If you have extremely good hearing and similar\n"
1032 " equipment, this preset will generally provide\n"
1033 " slightly higher quality than the \"standard\" mode.\n" "\n");
1034 fprintf(msgfp,
1035 " For CBR 320kbps (highest quality possible from the --preset switches):\n"
1036 "\n"
1037 " --preset insane This preset will usually be overkill for most people\n"
1038 " and most situations, but if you must have the\n"
1039 " absolute highest quality with no regard to filesize,\n"
1040 " this is the way to go.\n" "\n");
1041 fprintf(msgfp,
1042 " For ABR modes (high quality per given bitrate but not as high as VBR):\n"
1043 "\n"
1044 " --preset <kbps> Using this preset will usually give you good quality\n"
1045 " at a specified bitrate. Depending on the bitrate\n"
1046 " entered, this preset will determine the optimal\n"
1047 " settings for that particular situation. For example:\n"
1048 " \"--preset 185\" activates this preset and uses 185\n"
1049 " as an average kbps.\n" "\n");
1050 fprintf(msgfp,
1051 " \"cbr\" - If you use the ABR mode (read above) with a significant\n"
1052 " bitrate such as 80, 96, 112, 128, 160, 192, 224, 256, 320,\n"
1053 " you can use the \"cbr\" option to force CBR mode encoding\n"
1054 " instead of the standard abr mode. ABR does provide higher\n"
1055 " quality but CBR may be useful in situations such as when\n"
1056 " streaming an mp3 over the internet may be important.\n" "\n");
1057 fprintf(msgfp,
1058 " For example:\n"
1059 "\n"
1060 " --preset standard <input file> <output file>\n"
1061 " or --preset cbr 192 <input file> <output file>\n"
1062 " or --preset 172 <input file> <output file>\n"
1063 " or --preset extreme <input file> <output file>\n" "\n" "\n");
1064 fprintf(msgfp,
1065 "A few aliases are also available for ABR mode:\n"
1066 "phone => 16kbps/mono phon+/lw/mw-eu/sw => 24kbps/mono\n"
1067 "mw-us => 40kbps/mono voice => 56kbps/mono\n"
1068 "fm/radio/tape => 112kbps hifi => 160kbps\n"
1069 "cd => 192kbps studio => 256kbps\n");
1070 }
1071
1072
1073 static int
1074 presets_set(lame_t gfp, int fast, int cbr, const char *preset_name, const char *ProgramName)
1075 {
1076 int mono = 0;
1077
1078 if ((strcmp(preset_name, "help") == 0) && (fast < 1)
1079 && (cbr < 1)) {
1080 lame_version_print(stdout);
1081 presets_longinfo_dm(stdout);
1082 return -1;
1083 }
1084
1085 /*aliases for compatibility with old presets */
1086
1087 if (strcmp(preset_name, "phone") == 0) {
1088 preset_name = "16";
1089 mono = 1;
1090 }
1091 if ((strcmp(preset_name, "phon+") == 0) ||
1092 (strcmp(preset_name, "lw") == 0) ||
1093 (strcmp(preset_name, "mw-eu") == 0) || (strcmp(preset_name, "sw") == 0)) {
1094 preset_name = "24";
1095 mono = 1;
1096 }
1097 if (strcmp(preset_name, "mw-us") == 0) {
1098 preset_name = "40";
1099 mono = 1;
1100 }
1101 if (strcmp(preset_name, "voice") == 0) {
1102 preset_name = "56";
1103 mono = 1;
1104 }
1105 if (strcmp(preset_name, "fm") == 0) {
1106 preset_name = "112";
1107 }
1108 if ((strcmp(preset_name, "radio") == 0) || (strcmp(preset_name, "tape") == 0)) {
1109 preset_name = "112";
1110 }
1111 if (strcmp(preset_name, "hifi") == 0) {
1112 preset_name = "160";
1113 }
1114 if (strcmp(preset_name, "cd") == 0) {
1115 preset_name = "192";
1116 }
1117 if (strcmp(preset_name, "studio") == 0) {
1118 preset_name = "256";
1119 }
1120
1121 if (strcmp(preset_name, "medium") == 0) {
1122 lame_set_VBR_q(gfp, 4);
1123 lame_set_VBR(gfp, vbr_default);
1124 return 0;
1125 }
1126
1127 if (strcmp(preset_name, "standard") == 0) {
1128 lame_set_VBR_q(gfp, 2);
1129 lame_set_VBR(gfp, vbr_default);
1130 return 0;
1131 }
1132
1133 else if (strcmp(preset_name, "extreme") == 0) {
1134 lame_set_VBR_q(gfp, 0);
1135 lame_set_VBR(gfp, vbr_default);
1136 return 0;
1137 }
1138
1139 else if ((strcmp(preset_name, "insane") == 0) && (fast < 1)) {
1140
1141 lame_set_preset(gfp, INSANE);
1142
1143 return 0;
1144 }
1145
1146 /* Generic ABR Preset */
1147 if (((atoi(preset_name)) > 0) && (fast < 1)) {
1148 if ((atoi(preset_name)) >= 8 && (atoi(preset_name)) <= 320) {
1149 lame_set_preset(gfp, atoi(preset_name));
1150
1151 if (cbr == 1)
1152 lame_set_VBR(gfp, vbr_off);
1153
1154 if (mono == 1) {
1155 lame_set_mode(gfp, MONO);
1156 }
1157
1158 return 0;
1159
1160 }
1161 else {
1162 lame_version_print(Console_IO.Error_fp);
1163 error_printf("Error: The bitrate specified is out of the valid range for this preset\n"
1164 "\n"
1165 "When using this mode you must enter a value between \"32\" and \"320\"\n"
1166 "\n" "For further information try: \"%s --preset help\"\n", ProgramName);
1167 return -1;
1168 }
1169 }
1170
1171 lame_version_print(Console_IO.Error_fp);
1172 error_printf("Error: You did not enter a valid profile and/or options with --preset\n"
1173 "\n"
1174 "Available profiles are:\n"
1175 "\n"
1176 " medium\n"
1177 " standard\n"
1178 " extreme\n"
1179 " insane\n"
1180 " <cbr> (ABR Mode) - The ABR Mode is implied. To use it,\n"
1181 " simply specify a bitrate. For example:\n"
1182 " \"--preset 185\" activates this\n"
1183 " preset and uses 185 as an average kbps.\n" "\n");
1184 error_printf(" Some examples:\n"
1185 "\n"
1186 " or \"%s --preset standard <input file> <output file>\"\n"
1187 " or \"%s --preset cbr 192 <input file> <output file>\"\n"
1188 " or \"%s --preset 172 <input file> <output file>\"\n"
1189 " or \"%s --preset extreme <input file> <output file>\"\n"
1190 "\n"
1191 "For further information try: \"%s --preset help\"\n", ProgramName, ProgramName,
1192 ProgramName, ProgramName, ProgramName);
1193 return -1;
1194 }
1195
1196 static void
1197 genre_list_handler(int num, const char *name, void *cookie)
1198 {
1199 (void) cookie;
1200 console_printf("%3d %s\n", num, name);
1201 }
1202
1203
1204 /************************************************************************
1205 *
1206 * parse_args
1207 *
1208 * PURPOSE: Sets encoding parameters to the specifications of the
1209 * command line. Default settings are used for parameters
1210 * not specified in the command line.
1211 *
1212 * If the input file is in WAVE or AIFF format, the sampling frequency is read
1213 * from the AIFF header.
1214 *
1215 * The input and output filenames are read into #inpath# and #outpath#.
1216 *
1217 ************************************************************************/
1218
1219 /* would use real "strcasecmp" but it isn't portable */
1220 static int
1221 local_strcasecmp(const char *s1, const char *s2)
1222 {
1223 unsigned char c1;
1224 unsigned char c2;
1225
1226 do {
1227 c1 = (unsigned char) tolower(*s1);
1228 c2 = (unsigned char) tolower(*s2);
1229 if (!c1) {
1230 break;
1231 }
1232 ++s1;
1233 ++s2;
1234 } while (c1 == c2);
1235 return c1 - c2;
1236 }
1237
1238 static int
1239 local_strncasecmp(const char *s1, const char *s2, int n)
1240 {
1241 unsigned char c1 = 0;
1242 unsigned char c2 = 0;
1243 int cnt = 0;
1244
1245 do {
1246 if (cnt == n) {
1247 break;
1248 }
1249 c1 = (unsigned char) tolower(*s1);
1250 c2 = (unsigned char) tolower(*s2);
1251 if (!c1) {
1252 break;
1253 }
1254 ++s1;
1255 ++s2;
1256 ++cnt;
1257 } while (c1 == c2);
1258 return c1 - c2;
1259 }
1260
1261
1262
1263 /* LAME is a simple frontend which just uses the file extension */
1264 /* to determine the file type. Trying to analyze the file */
1265 /* contents is well beyond the scope of LAME and should not be added. */
1266 static int
1267 filename_to_type(const char *FileName)
1268 {
1269 size_t len = strlen(FileName);
1270
1271 if (len < 4)
1272 return sf_unknown;
1273
1274 FileName += len - 4;
1275 if (0 == local_strcasecmp(FileName, ".mpg"))
1276 return sf_mp123;
1277 if (0 == local_strcasecmp(FileName, ".mp1"))
1278 return sf_mp123;
1279 if (0 == local_strcasecmp(FileName, ".mp2"))
1280 return sf_mp123;
1281 if (0 == local_strcasecmp(FileName, ".mp3"))
1282 return sf_mp123;
1283 if (0 == local_strcasecmp(FileName, ".wav"))
1284 return sf_wave;
1285 if (0 == local_strcasecmp(FileName, ".aif"))
1286 return sf_aiff;
1287 if (0 == local_strcasecmp(FileName, ".raw"))
1288 return sf_raw;
1289 if (0 == local_strcasecmp(FileName, ".ogg"))
1290 return sf_ogg;
1291 return sf_unknown;
1292 }
1293
1294 static int
1295 resample_rate(double freq)
1296 {
1297 if (freq >= 1.e3)
1298 freq *= 1.e-3;
1299
1300 switch ((int) freq) {
1301 case 8:
1302 return 8000;
1303 case 11:
1304 return 11025;
1305 case 12:
1306 return 12000;
1307 case 16:
1308 return 16000;
1309 case 22:
1310 return 22050;
1311 case 24:
1312 return 24000;
1313 case 32:
1314 return 32000;
1315 case 44:
1316 return 44100;
1317 case 48:
1318 return 48000;
1319 default:
1320 error_printf("Illegal resample frequency: %.3f kHz\n", freq);
1321 return 0;
1322 }
1323 }
1324
1325 #ifdef _WIN32
1326 #define SLASH '\\'
1327 #define COLON ':'
1328 #elif __OS2__
1329 #define SLASH '\\'
1330 #else
1331 #define SLASH '/'
1332 #endif
1333
1334 static
1335 size_t scanPath(char const* s, char const** a, char const** b)
1336 {
1337 char const* s1 = s;
1338 char const* s2 = s;
1339 if (s != 0) {
1340 for (; *s; ++s) {
1341 switch (*s) {
1342 case SLASH:
1343 #ifdef _WIN32
1344 case COLON:
1345 #endif
1346 s2 = s;
1347 break;
1348 }
1349 }
1350 #ifdef _WIN32
1351 if (*s2 == COLON) {
1352 ++s2;
1353 }
1354 #endif
1355 }
1356 if (a) {
1357 *a = s1;
1358 }
1359 if (b) {
1360 *b = s2;
1361 }
1362 return s2-s1;
1363 }
1364
1365 static
1366 size_t scanBasename(char const* s, char const** a, char const** b)
1367 {
1368 char const* s1 = s;
1369 char const* s2 = s;
1370 if (s != 0) {
1371 for (; *s; ++s) {
1372 switch (*s) {
1373 case SLASH:
1374 #ifdef _WIN32
1375 case COLON:
1376 #endif
1377 s1 = s2 = s;
1378 break;
1379 case '.':
1380 s2 = s;
1381 break;
1382 }
1383 }
1384 if (s2 == s1) {
1385 s2 = s;
1386 }
1387 if (*s1 == SLASH
1388 #ifdef _WIN32
1389 || *s1 == COLON
1390 #endif
1391 ) {
1392 ++s1;
1393 }
1394 }
1395 if (a != 0) {
1396 *a = s1;
1397 }
1398 if (b != 0) {
1399 *b = s2;
1400 }
1401 return s2-s1;
1402 }
1403
1404 static
1405 int isCommonSuffix(char const* s_ext)
1406 {
1407 char const* suffixes[] =
1408 { ".WAV", ".RAW", ".MP1", ".MP2"
1409 , ".MP3", ".MPG", ".MPA", ".CDA"
1410 , ".OGG", ".AIF", ".AIFF", ".AU"
1411 , ".SND", ".FLAC", ".WV", ".OFR"
1412 , ".TAK", ".MP4", ".M4A", ".PCM"
1413 , ".W64"
1414 };
1415 size_t i;
1416 for (i = 0; i < dimension_of(suffixes); ++i) {
1417 if (local_strcasecmp(s_ext, suffixes[i]) == 0) {
1418 return 1;
1419 }
1420 }
1421 return 0;
1422 }
1423
1424
1425 int generateOutPath(char const* inPath, char const* outDir, char const* s_ext, char* outPath)
1426 {
1427 size_t const max_path = PATH_MAX;
1428 #if 1
1429 size_t i = 0;
1430 int out_dir_used = 0;
1431
1432 if (outDir != 0 && outDir[0] != 0) {
1433 out_dir_used = 1;
1434 while (*outDir) {
1435 outPath[i++] = *outDir++;
1436 if (i >= max_path) {
1437 goto err_generateOutPath;
1438 }
1439 }
1440 if (i > 0 && outPath[i-1] != SLASH) {
1441 outPath[i++] = SLASH;
1442 if (i >= max_path) {
1443 goto err_generateOutPath;
1444 }
1445 }
1446 outPath[i] = 0;
1447 }
1448 else {
1449 char const* pa;
1450 char const* pb;
1451 size_t j, n = scanPath(inPath, &pa, &pb);
1452 if (i+n >= max_path) {
1453 goto err_generateOutPath;
1454 }
1455 for (j = 0; j < n; ++j) {
1456 outPath[i++] = pa[j];
1457 }
1458 if (n > 0) {
1459 outPath[i++] = SLASH;
1460 if (i >= max_path) {
1461 goto err_generateOutPath;
1462 }
1463 }
1464 outPath[i] = 0;
1465 }
1466 {
1467 int replace_suffix = 0;
1468 char const* na;
1469 char const* nb;
1470 size_t j, n = scanBasename(inPath, &na, &nb);
1471 if (i+n >= max_path) {
1472 goto err_generateOutPath;
1473 }
1474 for (j = 0; j < n; ++j) {
1475 outPath[i++] = na[j];
1476 }
1477 outPath[i] = 0;
1478 if (isCommonSuffix(nb) == 1) {
1479 replace_suffix = 1;
1480 if (out_dir_used == 0) {
1481 if (local_strcasecmp(nb, s_ext) == 0) {
1482 replace_suffix = 0;
1483 }
1484 }
1485 }
1486 if (replace_suffix == 0) {
1487 while (*nb) {
1488 outPath[i++] = *nb++;
1489 if (i >= max_path) {
1490 goto err_generateOutPath;
1491 }
1492 }
1493 outPath[i] = 0;
1494 }
1495 }
1496 if (i+5 >= max_path) {
1497 goto err_generateOutPath;
1498 }
1499 while (*s_ext) {
1500 outPath[i++] = *s_ext++;
1501 }
1502 outPath[i] = 0;
1503 return 0;
1504 err_generateOutPath:
1505 error_printf( "error: output file name too long\n" );
1506 return 1;
1507 #else
1508 strncpy(outPath, inPath, PATH_MAX + 1 - 4);
1509 strncat(outPath, s_ext, 4);
1510 return 0;
1511 #endif
1512 }
1513
1514
1515 static int
1516 set_id3_albumart(lame_t gfp, char const* file_name)
1517 {
1518 int ret = -1;
1519 FILE *fpi = 0;
1520
1521 if (file_name == 0) {
1522 return 0;
1523 }
1524 fpi = lame_fopen(file_name, "rb");
1525 if (!fpi) {
1526 ret = 1;
1527 }
1528 else {
1529 size_t size;
1530 char *albumart = 0;
1531
1532 fseek(fpi, 0, SEEK_END);
1533 size = ftell(fpi);
1534 fseek(fpi, 0, SEEK_SET);
1535 albumart = (char *)malloc(size);
1536 if (!albumart) {
1537 ret = 2;
1538 }
1539 else {
1540 if (fread(albumart, 1, size, fpi) != size) {
1541 ret = 3;
1542 }
1543 else {
1544 ret = id3tag_set_albumart(gfp, albumart, size) ? 4 : 0;
1545 }
1546 free(albumart);
1547 }
1548 fclose(fpi);
1549 }
1550 switch (ret) {
1551 case 1: error_printf("Could not find: '%s'.\n", file_name); break;
1552 case 2: error_printf("Insufficient memory for reading the albumart.\n"); break;
1553 case 3: error_printf("Read error: '%s'.\n", file_name); break;
1554 case 4: error_printf("Unsupported image: '%s'.\nSpecify JPEG/PNG/GIF image\n", file_name); break;
1555 default: break;
1556 }
1557 return ret;
1558 }
1559
1560
1561 enum ID3TAG_MODE
1562 { ID3TAG_MODE_DEFAULT
1563 , ID3TAG_MODE_V1_ONLY
1564 , ID3TAG_MODE_V2_ONLY
1565 };
1566
1567 static int dev_only_with_arg(char const* str, char const* token, char const* nextArg, int* argIgnored, int* argUsed)
1568 {
1569 if (0 != local_strcasecmp(token,str)) return 0;
1570 *argUsed = 1;
1571 if (internal_opts_enabled) return 1;
1572 *argIgnored = 1;
1573 error_printf("WARNING: ignoring developer-only switch --%s %s\n", token, nextArg);
1574 return 0;
1575 }
1576
1577 static int dev_only_without_arg(char const* str, char const* token, int* argIgnored)
1578 {
1579 if (0 != local_strcasecmp(token,str)) return 0;
1580 if (internal_opts_enabled) return 1;
1581 *argIgnored = 1;
1582 error_printf("WARNING: ignoring developer-only switch --%s\n", token);
1583 return 0;
1584 }
1585
1586 /* Ugly, NOT final version */
1587
1588 #define T_IF(str) if ( 0 == local_strcasecmp (token,str) ) {
1589 #define T_ELIF(str) } else if ( 0 == local_strcasecmp (token,str) ) {
1590 #define T_ELIF2(str1,str2) } else if ( 0 == local_strcasecmp (token,str1) || 0 == local_strcasecmp (token,str2) ) {
1591 #define T_ELSE } else {
1592 #define T_END }
1593
1594 #define T_ELIF_INTERNAL(str) \
1595 } else if (dev_only_without_arg(str,token,&argIgnored)) {
1596
1597 #define T_ELIF_INTERNAL_WITH_ARG(str) \
1598 } else if (dev_only_with_arg(str,token,nextArg,&argIgnored,&argUsed)) {
1599
1600
1601 static int
1602 parse_args_(lame_global_flags * gfp, int argc, char **argv,
1603 char *const inPath, char *const outPath, char **nogap_inPath, int *num_nogap)
1604 {
1605 char outDir[PATH_MAX+1] = "";
1606 int input_file = 0; /* set to 1 if we parse an input file name */
1607 int i;
1608 int autoconvert = 0;
1609 int nogap = 0;
1610 int nogap_tags = 0; /* set to 1 to use VBR tags in NOGAP mode */
1611 const char *ProgramName = argv[0];
1612 int count_nogap = 0;
1613 int noreplaygain = 0; /* is RG explicitly disabled by the user */
1614 int id3tag_mode = ID3TAG_MODE_DEFAULT;
1615 int ignore_tag_errors = 0; /* Ignore errors in values passed for tags */
1616 #ifdef ID3TAGS_EXTENDED
1617 enum TextEncoding id3_tenc = TENC_UTF16;
1618 #else
1619 enum TextEncoding id3_tenc = TENC_LATIN1;
1620 #endif
1621
1622 #ifdef HAVE_LANGINFO_H
1623 setlocale(LC_CTYPE, "");
1624 #endif
1625 inPath[0] = '\0';
1626 outPath[0] = '\0';
1627 /* turn on display options. user settings may turn them off below */
1628 global_ui_config.silent = 0; /* default */
1629 global_ui_config.brhist = 1;
1630 global_decoder.mp3_delay = 0;
1631 global_decoder.mp3_delay_set = 0;
1632 global_decoder.disable_wav_header = 0;
1633 global_ui_config.print_clipping_info = 0;
1634 id3tag_init(gfp);
1635
1636 /* process args */
1637 for (i = 0; ++i < argc;) {
1638 char *token;
1639 int argUsed;
1640 int argIgnored=0;
1641
1642 token = argv[i];
1643 if (*token++ == '-') {
1644 char *nextArg = i + 1 < argc ? argv[i + 1] : "";
1645 argUsed = 0;
1646 if (!*token) { /* The user wants to use stdin and/or stdout. */
1647 input_file = 1;
1648 if (inPath[0] == '\0')
1649 strncpy(inPath, argv[i], PATH_MAX + 1);
1650 else if (outPath[0] == '\0')
1651 strncpy(outPath, argv[i], PATH_MAX + 1);
1652 }
1653 if (*token == '-') { /* GNU style */
1654 double double_value = 0;
1655 int int_value = 0;
1656 token++;
1657
1658 T_IF("resample")
1659 argUsed = getDoubleValue(token, nextArg, &double_value);
1660 if (argUsed)
1661 (void) lame_set_out_samplerate(gfp, resample_rate(double_value));
1662
1663 T_ELIF("vbr-old")
1664 lame_set_VBR(gfp, vbr_rh);
1665
1666 T_ELIF("vbr-new")
1667 lame_set_VBR(gfp, vbr_mt);
1668
1669 T_ELIF("vbr-mtrh")
1670 lame_set_VBR(gfp, vbr_mtrh);
1671
1672 T_ELIF("cbr")
1673 lame_set_VBR(gfp, vbr_off);
1674
1675 T_ELIF("abr")
1676 /* values larger than 8000 are bps (like Fraunhofer), so it's strange to get 320000 bps MP3 when specifying 8000 bps MP3 */
1677 argUsed = getIntValue(token, nextArg, &int_value);
1678 if (argUsed) {
1679 if (int_value >= 8000) {
1680 int_value = (int_value + 500) / 1000;
1681 }
1682 if (int_value > 320) {
1683 int_value = 320;
1684 }
1685 if (int_value < 8) {
1686 int_value = 8;
1687 }
1688 lame_set_VBR(gfp, vbr_abr);
1689 lame_set_VBR_mean_bitrate_kbps(gfp, int_value);
1690 }
1691
1692 T_ELIF("r3mix")
1693 lame_set_preset(gfp, R3MIX);
1694
1695 T_ELIF("bitwidth")
1696 argUsed = getIntValue(token, nextArg, &int_value);
1697 if (argUsed)
1698 global_raw_pcm.in_bitwidth = int_value;
1699
1700 T_ELIF("signed")
1701 global_raw_pcm.in_signed = 1;
1702
1703 T_ELIF("unsigned")
1704 global_raw_pcm.in_signed = 0;
1705
1706 T_ELIF("little-endian")
1707 global_raw_pcm.in_endian = ByteOrderLittleEndian;
1708
1709 T_ELIF("big-endian")
1710 global_raw_pcm.in_endian = ByteOrderBigEndian;
1711
1712 T_ELIF("mp1input")
1713 global_reader.input_format = sf_mp1;
1714
1715 T_ELIF("mp2input")
1716 global_reader.input_format = sf_mp2;
1717
1718 T_ELIF("mp3input")
1719 global_reader.input_format = sf_mp3;
1720
1721 T_ELIF("ogginput")
1722 error_printf("sorry, vorbis support in LAME is deprecated.\n");
1723 return -1;
1724
1725 T_ELIF("decode")
1726 (void) lame_set_decode_only(gfp, 1);
1727
1728 T_ELIF("flush")
1729 global_writer.flush_write = 1;
1730
1731 T_ELIF("decode-mp3delay")
1732 argUsed = getIntValue(token, nextArg, &int_value);
1733 if (argUsed) {
1734 global_decoder.mp3_delay = int_value;
1735 global_decoder.mp3_delay_set = 1;
1736 }
1737
1738 T_ELIF("nores")
1739 lame_set_disable_reservoir(gfp, 1);
1740
1741 T_ELIF("strictly-enforce-ISO")
1742 lame_set_strict_ISO(gfp, MDB_STRICT_ISO);
1743
1744 T_ELIF("buffer-constraint")
1745 argUsed = 1;
1746 if (strcmp(nextArg, "default") == 0)
1747 (void) lame_set_strict_ISO(gfp, MDB_DEFAULT);
1748 else if (strcmp(nextArg, "strict") == 0)
1749 (void) lame_set_strict_ISO(gfp, MDB_STRICT_ISO);
1750 else if (strcmp(nextArg, "maximum") == 0)
1751 (void) lame_set_strict_ISO(gfp, MDB_MAXIMUM);
1752 else {
1753 error_printf("unknown buffer constraint '%s'\n", nextArg);
1754 return -1;
1755 }
1756
1757 T_ELIF("scale")
1758 argUsed = getDoubleValue(token, nextArg, &double_value);
1759 if (argUsed)
1760 (void) lame_set_scale(gfp, (float) double_value);
1761
1762 T_ELIF("scale-l")
1763 argUsed = getDoubleValue(token, nextArg, &double_value);
1764 if (argUsed)
1765 (void) lame_set_scale_left(gfp, (float) double_value);
1766
1767 T_ELIF("scale-r")
1768 argUsed = getDoubleValue(token, nextArg, &double_value);
1769 if (argUsed)
1770 (void) lame_set_scale_right(gfp, (float) double_value);
1771
1772 T_ELIF("gain")
1773 argUsed = getDoubleValue(token, nextArg, &double_value);
1774 if (argUsed) {
1775 double gain = double_value;
1776 gain = gain > -20.f ? gain : -20.f;
1777 gain = gain < 12.f ? gain : 12.f;
1778 gain = pow(10.f, gain*0.05);
1779 (void) lame_set_scale(gfp, (float) gain);
1780 }
1781
1782 T_ELIF("noasm")
1783 argUsed = 1;
1784 if (!strcmp(nextArg, "mmx"))
1785 (void) lame_set_asm_optimizations(gfp, MMX, 0);
1786 if (!strcmp(nextArg, "3dnow"))
1787 (void) lame_set_asm_optimizations(gfp, AMD_3DNOW, 0);
1788 if (!strcmp(nextArg, "sse"))
1789 (void) lame_set_asm_optimizations(gfp, SSE, 0);
1790
1791 T_ELIF("freeformat")
1792 lame_set_free_format(gfp, 1);
1793
1794 T_ELIF("replaygain-fast")
1795 lame_set_findReplayGain(gfp, 1);
1796
1797 #ifdef DECODE_ON_THE_FLY
1798 T_ELIF("replaygain-accurate")
1799 lame_set_decode_on_the_fly(gfp, 1);
1800 lame_set_findReplayGain(gfp, 1);
1801 #endif
1802
1803 T_ELIF("noreplaygain")
1804 noreplaygain = 1;
1805 lame_set_findReplayGain(gfp, 0);
1806
1807
1808 #ifdef DECODE_ON_THE_FLY
1809 T_ELIF("clipdetect")
1810 global_ui_config.print_clipping_info = 1;
1811 lame_set_decode_on_the_fly(gfp, 1);
1812 #endif
1813
1814 T_ELIF("nohist")
1815 global_ui_config.brhist = 0;
1816
1817 #if defined(__OS2__) || defined(WIN32)
1818 T_ELIF("priority")
1819 argUsed = getIntValue(token, nextArg, &int_value);
1820 if (argUsed)
1821 setProcessPriority(int_value);
1822 #endif
1823
1824 /* options for ID3 tag */
1825 #ifdef ID3TAGS_EXTENDED
1826 T_ELIF2("id3v2-utf16","id3v2-ucs2") /* id3v2-ucs2 for compatibility only */
1827 id3_tenc = TENC_UTF16;
1828 id3tag_add_v2(gfp);
1829
1830 T_ELIF("id3v2-utf8")
1831 id3_tenc = TENC_UTF8;
1832 id3tag_add_v2_4_UTF8(gfp);
1833
1834 T_ELIF("id3v2-latin1")
1835 id3_tenc = TENC_LATIN1;
1836 id3tag_add_v2(gfp);
1837 #endif
1838
1839 T_ELIF("tt")
1840 argUsed = 1;
1841 id3_tag(gfp, 't', id3_tenc, nextArg);
1842
1843 T_ELIF("ta")
1844 argUsed = 1;
1845 id3_tag(gfp, 'a', id3_tenc, nextArg);
1846
1847 T_ELIF("tl")
1848 argUsed = 1;
1849 id3_tag(gfp, 'l', id3_tenc, nextArg);
1850
1851 T_ELIF("ty")
1852 argUsed = 1;
1853 id3_tag(gfp, 'y', id3_tenc, nextArg);
1854
1855 T_ELIF("tc")
1856 argUsed = 1;
1857 id3_tag(gfp, 'c', id3_tenc, nextArg);
1858
1859 T_ELIF("tn")
1860 int ret = id3_tag(gfp, 'n', id3_tenc, nextArg);
1861 argUsed = 1;
1862 if (ret != 0) {
1863 if (0 == ignore_tag_errors) {
1864 if (id3tag_mode == ID3TAG_MODE_V1_ONLY) {
1865 if (global_ui_config.silent < 9) {
1866 error_printf("The track number has to be between 1 and 255 for ID3v1.\n");
1867 }
1868 return -1;
1869 }
1870 else if (id3tag_mode == ID3TAG_MODE_V2_ONLY) {
1871 /* track will be stored as-is in ID3v2 case, so no problem here */
1872 }
1873 else {
1874 if (global_ui_config.silent < 9) {
1875 error_printf("The track number has to be between 1 and 255 for ID3v1, ignored for ID3v1.\n");
1876 }
1877 }
1878 }
1879 }
1880
1881 T_ELIF("tg")
1882 int ret = 0;
1883 argUsed = 1;
1884 if (nextArg != 0 && strlen(nextArg) > 0) {
1885 ret = id3_tag(gfp, 'g', id3_tenc, nextArg);
1886 }
1887 if (ret != 0) {
1888 if (0 == ignore_tag_errors) {
1889 if (ret == -1) {
1890 error_printf("Unknown ID3v1 genre number: '%s'.\n", nextArg);
1891 return -1;
1892 }
1893 else if (ret == -2) {
1894 if (id3tag_mode == ID3TAG_MODE_V1_ONLY) {
1895 error_printf("Unknown ID3v1 genre: '%s'.\n", nextArg);
1896 return -1;
1897 }
1898 else if (id3tag_mode == ID3TAG_MODE_V2_ONLY) {
1899 /* genre will be stored as-is in ID3v2 case, so no problem here */
1900 }
1901 else {
1902 if (global_ui_config.silent < 9) {
1903 error_printf("Unknown ID3v1 genre: '%s'. Setting ID3v1 genre to 'Other'\n", nextArg);
1904 }
1905 }
1906 }
1907 else {
1908 if (global_ui_config.silent < 10)
1909 error_printf("Internal error.\n");
1910 return -1;
1911 }
1912 }
1913 }
1914
1915 T_ELIF("tv")
1916 argUsed = 1;
1917 if (id3_tag(gfp, 'v', id3_tenc, nextArg)) {
1918 if (global_ui_config.silent < 9) {
1919 error_printf("Invalid field value: '%s'. Ignored\n", nextArg);
1920 }
1921 }
1922
1923 T_ELIF("ti")
1924 argUsed = 1;
1925 if (set_id3_albumart(gfp, nextArg) != 0) {
1926 if (! ignore_tag_errors) {
1927 return -1;
1928 }
1929 }
1930
1931 T_ELIF("ignore-tag-errors")
1932 ignore_tag_errors = 1;
1933
1934 T_ELIF("add-id3v2")
1935 id3tag_add_v2(gfp);
1936
1937 T_ELIF("id3v1-only")
1938 id3tag_v1_only(gfp);
1939 id3tag_mode = ID3TAG_MODE_V1_ONLY;
1940
1941 T_ELIF("id3v2-only")
1942 id3tag_v2_only(gfp);
1943 id3tag_mode = ID3TAG_MODE_V2_ONLY;
1944
1945 T_ELIF("space-id3v1")
1946 id3tag_space_v1(gfp);
1947
1948 T_ELIF("pad-id3v2")
1949 id3tag_pad_v2(gfp);
1950
1951 T_ELIF("pad-id3v2-size")
1952 argUsed = getIntValue(token, nextArg, &int_value);
1953 if (argUsed) {
1954 int_value = int_value <= 128000 ? int_value : 128000;
1955 int_value = int_value >= 0 ? int_value : 0;
1956 id3tag_set_pad(gfp, int_value);
1957 }
1958
1959 T_ELIF("genre-list")
1960 id3tag_genre_list(genre_list_handler, NULL);
1961 return -2;
1962
1963
1964 T_ELIF("lowpass")
1965 argUsed = getDoubleValue(token, nextArg, &double_value);
1966 if (argUsed) {
1967 if (double_value < 0) {
1968 lame_set_lowpassfreq(gfp, -1);
1969 }
1970 else {
1971 /* useful are 0.001 kHz...50 kHz, 50 Hz...50000 Hz */
1972 if (double_value < 0.001 || double_value > 50000.) {
1973 error_printf("Must specify lowpass with --lowpass freq, freq >= 0.001 kHz\n");
1974 return -1;
1975 }
1976 lame_set_lowpassfreq(gfp, (int) (double_value * (double_value < 50. ? 1.e3 : 1.e0) + 0.5));
1977 }
1978 }
1979
1980 T_ELIF("lowpass-width")
1981 argUsed = getDoubleValue(token, nextArg, &double_value);
1982 if (argUsed) {
1983 /* useful are 0.001 kHz...16 kHz, 16 Hz...50000 Hz */
1984 if (double_value < 0.001 || double_value > 50000.) {
1985 error_printf
1986 ("Must specify lowpass width with --lowpass-width freq, freq >= 0.001 kHz\n");
1987 return -1;
1988 }
1989 lame_set_lowpasswidth(gfp, (int) (double_value * (double_value < 16. ? 1.e3 : 1.e0) + 0.5));
1990 }
1991
1992 T_ELIF("highpass")
1993 argUsed = getDoubleValue(token, nextArg, &double_value);
1994 if (argUsed) {
1995 if (double_value < 0.0) {
1996 lame_set_highpassfreq(gfp, -1);
1997 }
1998 else {
1999 /* useful are 0.001 kHz...16 kHz, 16 Hz...50000 Hz */
2000 if (double_value < 0.001 || double_value > 50000.) {
2001 error_printf("Must specify highpass with --highpass freq, freq >= 0.001 kHz\n");
2002 return -1;
2003 }
2004 lame_set_highpassfreq(gfp, (int) (double_value * (double_value < 16. ? 1.e3 : 1.e0) + 0.5));
2005 }
2006 }
2007
2008 T_ELIF("highpass-width")
2009 argUsed = getDoubleValue(token, nextArg, &double_value);
2010 if (argUsed) {
2011 /* useful are 0.001 kHz...16 kHz, 16 Hz...50000 Hz */
2012 if (double_value < 0.001 || double_value > 50000.) {
2013 error_printf
2014 ("Must specify highpass width with --highpass-width freq, freq >= 0.001 kHz\n");
2015 return -1;
2016 }
2017 lame_set_highpasswidth(gfp, (int) double_value);
2018 }
2019
2020 T_ELIF("comp")
2021 argUsed = getDoubleValue(token, nextArg, &double_value);
2022 if (argUsed) {
2023 if (double_value < 1.0) {
2024 error_printf("Must specify compression ratio >= 1.0\n");
2025 return -1;
2026 }
2027 else {
2028 lame_set_compression_ratio(gfp, (float) double_value);
2029 }
2030 }
2031
2032 /* some more GNU-ish options could be added
2033 * brief => few messages on screen (name, status report)
2034 * o/output file => specifies output filename
2035 * O => stdout
2036 * i/input file => specifies input filename
2037 * I => stdin
2038 */
2039 T_ELIF("quiet")
2040 global_ui_config.silent = 10; /* on a scale from 1 to 10 be very silent */
2041
2042 T_ELIF("silent")
2043 global_ui_config.silent = 9;
2044
2045 T_ELIF("brief")
2046 global_ui_config.silent = -5; /* print few info on screen */
2047
2048 T_ELIF("verbose")
2049 global_ui_config.silent = -10; /* print a lot on screen */
2050
2051 T_ELIF2("version", "license")
2052 print_license(stdout);
2053 return -2;
2054
2055 T_ELIF2("help", "usage")
2056 if (0 == local_strncasecmp(nextArg, "id3", 3)) {
2057 help_id3tag(stdout);
2058 }
2059 else if (0 == local_strncasecmp(nextArg, "dev", 3)) {
2060 help_developer_switches(stdout);
2061 }
2062 else {
2063 short_help(gfp, stdout, ProgramName);
2064 }
2065 return -2;
2066
2067 T_ELIF("longhelp")
2068 long_help(gfp, stdout, ProgramName, 0 /* lessmode=NO */ );
2069 return -2;
2070
2071 T_ELIF("?")
2072 #ifdef __unix__
2073 FILE *fp = popen("less -Mqc", "w");
2074 long_help(gfp, fp, ProgramName, 0 /* lessmode=NO */ );
2075 pclose(fp);
2076 #else
2077 long_help(gfp, stdout, ProgramName, 1 /* lessmode=YES */ );
2078 #endif
2079 return -2;
2080
2081 T_ELIF2("preset", "alt-preset")
2082 argUsed = 1;
2083 {
2084 int fast = 0, cbr = 0;
2085
2086 while ((strcmp(nextArg, "fast") == 0) || (strcmp(nextArg, "cbr") == 0)) {
2087
2088 if ((strcmp(nextArg, "fast") == 0) && (fast < 1))
2089 fast = 1;
2090 if ((strcmp(nextArg, "cbr") == 0) && (cbr < 1))
2091 cbr = 1;
2092
2093 argUsed++;
2094 nextArg = i + argUsed < argc ? argv[i + argUsed] : "";
2095 }
2096
2097 if (presets_set(gfp, fast, cbr, nextArg, ProgramName) < 0)
2098 return -1;
2099 }
2100
2101 T_ELIF("disptime")
2102 argUsed = getDoubleValue(token, nextArg, &double_value);
2103 if (argUsed)
2104 global_ui_config.update_interval = (float) double_value;
2105
2106 T_ELIF("nogaptags")
2107 nogap_tags = 1;
2108
2109 T_ELIF("nogapout")
2110 int const arg_n = strnlen(nextArg, PATH_MAX);
2111 if (arg_n >= PATH_MAX) {
2112 error_printf("%s: %s argument length (%d) exceeds limit (%d)\n", ProgramName, token, arg_n, PATH_MAX);
2113 return -1;
2114 }
2115 strncpy(outPath, nextArg, PATH_MAX);
2116 outPath[PATH_MAX] = '\0';
2117 argUsed = 1;
2118
2119 T_ELIF("out-dir")
2120 int const arg_n = strnlen(nextArg, PATH_MAX);
2121 if (arg_n >= PATH_MAX) {
2122 error_printf("%s: %s argument length (%d) exceeds limit (%d)\n", ProgramName, token, arg_n, PATH_MAX);
2123 return -1;
2124 }
2125 strncpy(outDir, nextArg, PATH_MAX);
2126 outDir[PATH_MAX] = '\0';
2127 argUsed = 1;
2128
2129 T_ELIF("nogap")
2130 nogap = 1;
2131
2132 T_ELIF("swap-channel")
2133 global_reader.swap_channel = 1;
2134
2135 T_ELIF("ignorelength")
2136 global_reader.ignorewavlength = 1;
2137
2138 T_ELIF ("athaa-sensitivity")
2139 argUsed = getDoubleValue(token, nextArg, &double_value);
2140 if (argUsed)
2141 lame_set_athaa_sensitivity(gfp, (float) double_value);
2142
2143 /* ---------------- lots of dead switches ---------------- */
2144
2145 T_ELIF_INTERNAL("noshort")
2146 (void) lame_set_no_short_blocks(gfp, 1);
2147
2148 T_ELIF_INTERNAL("short")
2149 (void) lame_set_no_short_blocks(gfp, 0);
2150
2151 T_ELIF_INTERNAL("allshort")
2152 (void) lame_set_force_short_blocks(gfp, 1);
2153
2154 T_ELIF_INTERNAL("notemp")
2155 (void) lame_set_useTemporal(gfp, 0);
2156
2157 T_ELIF_INTERNAL_WITH_ARG("interch")
2158 argUsed = getDoubleValue(token, nextArg, &double_value);
2159 if (argUsed)
2160 (void) lame_set_interChRatio(gfp, (float) double_value);
2161
2162 T_ELIF_INTERNAL_WITH_ARG("temporal-masking")
2163 argUsed = getIntValue(token, nextArg, &int_value);
2164 if (argUsed)
2165 (void) lame_set_useTemporal(gfp, int_value ? 1 : 0);
2166
2167 T_ELIF_INTERNAL("nspsytune")
2168 ;
2169
2170 T_ELIF_INTERNAL("nssafejoint")
2171 lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | 2);
2172
2173 T_ELIF_INTERNAL_WITH_ARG("nsmsfix")
2174 argUsed = getDoubleValue(token, nextArg, &double_value);
2175 if (argUsed)
2176 (void) lame_set_msfix(gfp, double_value);
2177
2178 T_ELIF_INTERNAL_WITH_ARG("ns-bass")
2179 argUsed = getDoubleValue(token, nextArg, &double_value);
2180 if (argUsed) {
2181 int k = (int) (double_value * 4);
2182 if (k < -32)
2183 k = -32;
2184 if (k > 31)
2185 k = 31;
2186 if (k < 0)
2187 k += 64;
2188 lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | (k << 2));
2189 }
2190
2191 T_ELIF_INTERNAL_WITH_ARG("ns-alto")
2192 argUsed = getDoubleValue(token, nextArg, &double_value);
2193 if (argUsed) {
2194 int k = (int) (double_value * 4);
2195 if (k < -32)
2196 k = -32;
2197 if (k > 31)
2198 k = 31;
2199 if (k < 0)
2200 k += 64;
2201 lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | (k << 8));
2202 }
2203
2204 T_ELIF_INTERNAL_WITH_ARG("ns-treble")
2205 argUsed = getDoubleValue(token, nextArg, &double_value);
2206 if (argUsed) {
2207 int k = (int) (double_value * 4);
2208 if (k < -32)
2209 k = -32;
2210 if (k > 31)
2211 k = 31;
2212 if (k < 0)
2213 k += 64;
2214 lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | (k << 14));
2215 }
2216
2217 T_ELIF_INTERNAL_WITH_ARG("ns-sfb21")
2218 /* to be compatible with Naoki's original code,
2219 * ns-sfb21 specifies how to change ns-treble for sfb21 */
2220 argUsed = getDoubleValue(token, nextArg, &double_value);
2221 if (argUsed) {
2222 int k = (int) (double_value * 4);
2223 if (k < -32)
2224 k = -32;
2225 if (k > 31)
2226 k = 31;
2227 if (k < 0)
2228 k += 64;
2229 lame_set_exp_nspsytune(gfp, lame_get_exp_nspsytune(gfp) | (k << 20));
2230 }
2231
2232 T_ELIF_INTERNAL_WITH_ARG("tune") /*without helptext */
2233 argUsed = getDoubleValue(token, nextArg, &double_value);
2234 if (argUsed)
2235 lame_set_tune(gfp, (float) double_value);
2236
2237 T_ELIF_INTERNAL_WITH_ARG("shortthreshold")
2238 {
2239 float x, y;
2240 int n = sscanf(nextArg, "%f,%f", &x, &y);
2241 if (n == 1) {
2242 y = x;
2243 }
2244 (void) lame_set_short_threshold(gfp, x, y);
2245 }
2246 T_ELIF_INTERNAL_WITH_ARG("maskingadjust") /*without helptext */
2247 argUsed = getDoubleValue(token, nextArg, &double_value);
2248 if (argUsed)
2249 (void) lame_set_maskingadjust(gfp, (float) double_value);
2250
2251 T_ELIF_INTERNAL_WITH_ARG("maskingadjustshort") /*without helptext */
2252 argUsed = getDoubleValue(token, nextArg, &double_value);
2253 if (argUsed)
2254 (void) lame_set_maskingadjust_short(gfp, (float) double_value);
2255
2256 T_ELIF_INTERNAL_WITH_ARG("athcurve") /*without helptext */
2257 argUsed = getDoubleValue(token, nextArg, &double_value);
2258 if (argUsed)
2259 (void) lame_set_ATHcurve(gfp, (float) double_value);
2260
2261 T_ELIF_INTERNAL("no-preset-tune") /*without helptext */
2262 (void) lame_set_preset_notune(gfp, 0);
2263
2264 T_ELIF_INTERNAL_WITH_ARG("substep")
2265 argUsed = getIntValue(token, nextArg, &int_value);
2266 if (argUsed)
2267 (void) lame_set_substep(gfp, int_value);
2268
2269 T_ELIF_INTERNAL_WITH_ARG("sbgain") /*without helptext */
2270 argUsed = getIntValue(token, nextArg, &int_value);
2271 if (argUsed)
2272 (void) lame_set_subblock_gain(gfp, int_value);
2273
2274 T_ELIF_INTERNAL("sfscale") /*without helptext */
2275 (void) lame_set_sfscale(gfp, 1);
2276
2277 T_ELIF_INTERNAL("noath")
2278 (void) lame_set_noATH(gfp, 1);
2279
2280 T_ELIF_INTERNAL("athonly")
2281 (void) lame_set_ATHonly(gfp, 1);
2282
2283 T_ELIF_INTERNAL("athshort")
2284 (void) lame_set_ATHshort(gfp, 1);
2285
2286 T_ELIF_INTERNAL_WITH_ARG("athlower")
2287 argUsed = getDoubleValue(token, nextArg, &double_value);
2288 if (argUsed)
2289 (void) lame_set_ATHlower(gfp, (float) double_value);
2290
2291 T_ELIF_INTERNAL_WITH_ARG("athtype")
2292 argUsed = getIntValue(token, nextArg, &int_value);
2293 if (argUsed)
2294 (void) lame_set_ATHtype(gfp, int_value);
2295
2296 T_ELIF_INTERNAL_WITH_ARG("athaa-type") /* switch for developing, no DOCU */
2297 /* once was 1:Gaby, 2:Robert, 3:Jon, else:off */
2298 argUsed = getIntValue(token, nextArg, &int_value);
2299 if (argUsed)
2300 (void) lame_set_athaa_type(gfp, int_value); /* now: 0:off else:Jon */
2301
2302 T_ELIF_INTERNAL_WITH_ARG("debug-file") /* switch for developing, no DOCU */
2303 /* file name to print debug info into */
2304 set_debug_file(nextArg);
2305
2306 T_ELSE {
2307 if (!argIgnored) {
2308 error_printf("%s: unrecognized option --%s\n", ProgramName, token);
2309 return -1;
2310 }
2311 argIgnored = 0;
2312 }
2313 T_END i += argUsed;
2314
2315 }
2316 else {
2317 char c;
2318 while ((c = *token++) != '\0') {
2319 double double_value = 0;
2320 int int_value = 0;
2321 char const *arg = *token ? token : nextArg;
2322 switch (c) {
2323 case 'm':
2324 argUsed = 1;
2325
2326 switch (*arg) {
2327 case 's':
2328 (void) lame_set_mode(gfp, STEREO);
2329 break;
2330 case 'd':
2331 (void) lame_set_mode(gfp, DUAL_CHANNEL);
2332 break;
2333 case 'f':
2334 lame_set_force_ms(gfp, 1);
2335 (void) lame_set_mode(gfp, JOINT_STEREO);
2336 break;
2337 case 'j':
2338 lame_set_force_ms(gfp, 0);
2339 (void) lame_set_mode(gfp, JOINT_STEREO);
2340 break;
2341 case 'm':
2342 (void) lame_set_mode(gfp, MONO);
2343 break;
2344 case 'l':
2345 (void) lame_set_mode(gfp, MONO);
2346 (void) lame_set_scale_left(gfp, 2);
2347 (void) lame_set_scale_right(gfp, 0);
2348 break;
2349 case 'r':
2350 (void) lame_set_mode(gfp, MONO);
2351 (void) lame_set_scale_left(gfp, 0);
2352 (void) lame_set_scale_right(gfp, 2);
2353 break;
2354 case 'a': /* same as 'j' ??? */
2355 lame_set_force_ms(gfp, 0);
2356 (void) lame_set_mode(gfp, JOINT_STEREO);
2357 break;
2358 default:
2359 error_printf("%s: -m mode must be s/d/f/j/m/l/r not %s\n", ProgramName,
2360 arg);
2361 return -1;
2362 }
2363 break;
2364
2365 case 'V':
2366 argUsed = getDoubleValue("V", arg, &double_value);
2367 if (argUsed) {
2368 /* to change VBR default look in lame.h */
2369 if (lame_get_VBR(gfp) == vbr_off)
2370 lame_set_VBR(gfp, vbr_default);
2371 lame_set_VBR_quality(gfp, (float) double_value);
2372 }
2373 break;
2374 case 'v':
2375 /* to change VBR default look in lame.h */
2376 if (lame_get_VBR(gfp) == vbr_off)
2377 lame_set_VBR(gfp, vbr_default);
2378 break;
2379
2380 case 'q':
2381 argUsed = getIntValue("q", arg, &int_value);
2382 if (argUsed)
2383 (void) lame_set_quality(gfp, int_value);
2384 break;
2385 case 'f':
2386 (void) lame_set_quality(gfp, 7);
2387 break;
2388 case 'h':
2389 (void) lame_set_quality(gfp, 2);
2390 break;
2391
2392 case 's':
2393 argUsed = getDoubleValue("s", arg, &double_value);
2394 if (argUsed) {
2395 double_value = (int) (double_value * (double_value <= 192 ? 1.e3 : 1.e0) + 0.5);
2396 global_reader.input_samplerate = (int)double_value;
2397 (void) lame_set_in_samplerate(gfp, (int)double_value);
2398 }
2399 break;
2400 case 'b':
2401 argUsed = getIntValue("b", arg, &int_value);
2402 if (argUsed) {
2403 lame_set_brate(gfp, int_value);
2404 lame_set_VBR_min_bitrate_kbps(gfp, lame_get_brate(gfp));
2405 }
2406 break;
2407 case 'B':
2408 argUsed = getIntValue("B", arg, &int_value);
2409 if (argUsed) {
2410 lame_set_VBR_max_bitrate_kbps(gfp, int_value);
2411 }
2412 break;
2413 case 'F':
2414 lame_set_VBR_hard_min(gfp, 1);
2415 break;
2416 case 't': /* dont write VBR tag */
2417 (void) lame_set_bWriteVbrTag(gfp, 0);
2418 global_decoder.disable_wav_header = 1;
2419 break;
2420 case 'T': /* do write VBR tag */
2421 (void) lame_set_bWriteVbrTag(gfp, 1);
2422 nogap_tags = 1;
2423 global_decoder.disable_wav_header = 0;
2424 break;
2425 case 'r': /* force raw pcm input file */
2426 #if defined(LIBSNDFILE)
2427 error_printf
2428 ("WARNING: libsndfile may ignore -r and perform fseek's on the input.\n"
2429 "Compile without libsndfile if this is a problem.\n");
2430 #endif
2431 global_reader.input_format = sf_raw;
2432 break;
2433 case 'x': /* force byte swapping */
2434 global_reader.swapbytes = 1;
2435 break;
2436 case 'p': /* (jo) error_protection: add crc16 information to stream */
2437 lame_set_error_protection(gfp, 1);
2438 break;
2439 case 'a': /* autoconvert input file from stereo to mono - for mono mp3 encoding */
2440 autoconvert = 1;
2441 (void) lame_set_mode(gfp, MONO);
2442 break;
2443 case 'd': /*(void) lame_set_allow_diff_short( gfp, 1 ); */
2444 case 'k': /*lame_set_lowpassfreq(gfp, -1);
2445 lame_set_highpassfreq(gfp, -1); */
2446 error_printf("WARNING: -%c is obsolete.\n", c);
2447 break;
2448 case 'S':
2449 global_ui_config.silent = 5;
2450 break;
2451 case 'X':
2452 /* experimental switch -X:
2453 the differnt types of quant compare are tough
2454 to communicate to endusers, so they shouldn't
2455 bother to toy around with them
2456 */
2457 {
2458 int x, y;
2459 int n = sscanf(arg, "%d,%d", &x, &y);
2460 if (n == 1) {
2461 y = x;
2462 }
2463 argUsed = 1;
2464 if (internal_opts_enabled) {
2465 lame_set_quant_comp(gfp, x);
2466 lame_set_quant_comp_short(gfp, y);
2467 }
2468 }
2469 break;
2470 case 'Y':
2471 lame_set_experimentalY(gfp, 1);
2472 break;
2473 case 'Z':
2474 /* experimental switch -Z:
2475 */
2476 {
2477 int n = 1;
2478 argUsed = sscanf(arg, "%d", &n);
2479 /*if (internal_opts_enabled)*/
2480 {
2481 lame_set_experimentalZ(gfp, n);
2482 }
2483 }
2484 break;
2485 case 'e':
2486 argUsed = 1;
2487
2488 switch (*arg) {
2489 case 'n':
2490 lame_set_emphasis(gfp, 0);
2491 break;
2492 case '5':
2493 lame_set_emphasis(gfp, 1);
2494 break;
2495 case 'c':
2496 lame_set_emphasis(gfp, 3);
2497 break;
2498 default:
2499 error_printf("%s: -e emp must be n/5/c not %s\n", ProgramName, arg);
2500 return -1;
2501 }
2502 break;
2503 case 'c':
2504 lame_set_copyright(gfp, 1);
2505 break;
2506 case 'o':
2507 lame_set_original(gfp, 0);
2508 break;
2509
2510 case '?':
2511 long_help(gfp, stdout, ProgramName, 0 /* LESSMODE=NO */ );
2512 return -1;
2513
2514 default:
2515 error_printf("%s: unrecognized option -%c\n", ProgramName, c);
2516 return -1;
2517 }
2518 if (argUsed) {
2519 if (arg == token)
2520 token = ""; /* no more from token */
2521 else
2522 ++i; /* skip arg we used */
2523 arg = "";
2524 argUsed = 0;
2525 }
2526 }
2527 }
2528 }
2529 else {
2530 if (nogap) {
2531 if ((num_nogap != NULL) && (count_nogap < *num_nogap)) {
2532 strncpy(nogap_inPath[count_nogap++], argv[i], PATH_MAX + 1);
2533 input_file = 1;
2534 }
2535 else {
2536 /* sorry, calling program did not allocate enough space */
2537 error_printf
2538 ("Error: 'nogap option'. Calling program does not allow nogap option, or\n"
2539 "you have exceeded maximum number of input files for the nogap option\n");
2540 if (num_nogap) {
2541 *num_nogap = -1;
2542 }
2543 return -1;
2544 }
2545 }
2546 else {
2547 /* normal options: inputfile [outputfile], and
2548 either one can be a '-' for stdin/stdout */
2549 if (inPath[0] == '\0') {
2550 strncpy(inPath, argv[i], PATH_MAX + 1);
2551 input_file = 1;
2552 }
2553 else {
2554 if (outPath[0] == '\0')
2555 strncpy(outPath, argv[i], PATH_MAX + 1);
2556 else {
2557 error_printf("%s: excess arg %s\n", ProgramName, argv[i]);
2558 return -1;
2559 }
2560 }
2561 }
2562 }
2563 } /* loop over args */
2564
2565 if (!input_file) {
2566 usage(Console_IO.Console_fp, ProgramName);
2567 return -1;
2568 }
2569
2570 if (lame_get_decode_only(gfp) && count_nogap > 0) {
2571 error_printf("combination of nogap and decode not supported!\n");
2572 return -1;
2573 }
2574
2575 if (inPath[0] == '-') {
2576 if (global_ui_config.silent == 0) { /* user didn't overrule default behaviour */
2577 global_ui_config.silent = 1;
2578 }
2579 }
2580 #ifdef WIN32
2581 else
2582 dosToLongFileName(inPath);
2583 #endif
2584
2585 if (outPath[0] == '\0') { /* no explicit output dir or file */
2586 if (count_nogap > 0) { /* in case of nogap encode */
2587 strncpy(outPath, outDir, PATH_MAX);
2588 outPath[PATH_MAX] = '\0'; /* whatever someone set via --out-dir <path> argument */
2589 }
2590 else if (inPath[0] == '-') {
2591 /* if input is stdin, default output is stdout */
2592 strcpy(outPath, "-");
2593 }
2594 else {
2595 char const* s_ext = lame_get_decode_only(gfp) ? ".wav" : ".mp3";
2596 if (generateOutPath(inPath, outDir, s_ext, outPath) != 0) {
2597 return -1;
2598 }
2599 }
2600 }
2601
2602 /* RG is enabled by default */
2603 if (!noreplaygain)
2604 lame_set_findReplayGain(gfp, 1);
2605
2606 /* disable VBR tags with nogap unless the VBR tags are forced */
2607 if (nogap && lame_get_bWriteVbrTag(gfp) && nogap_tags == 0) {
2608 console_printf("Note: Disabling VBR Xing/Info tag since it interferes with --nogap\n");
2609 lame_set_bWriteVbrTag(gfp, 0);
2610 }
2611
2612 /* some file options not allowed with stdout */
2613 if (outPath[0] == '-') {
2614 (void) lame_set_bWriteVbrTag(gfp, 0); /* turn off VBR tag */
2615 }
2616
2617 /* if user did not explicitly specify input is mp3, check file name */
2618 if (global_reader.input_format == sf_unknown)
2619 global_reader.input_format = filename_to_type(inPath);
2620
2621 #if !(defined HAVE_MPGLIB || defined AMIGA_MPEGA || HAVE_MPG123)
2622 if (is_mpeg_file_format(global_reader.input_format)) {
2623 error_printf("Error: libmp3lame not compiled with mpg123 *decoding* support \n");
2624 return -1;
2625 }
2626 #endif
2627
2628 /* default guess for number of channels */
2629 if (autoconvert)
2630 (void) lame_set_num_channels(gfp, 2);
2631 else if (MONO == lame_get_mode(gfp))
2632 (void) lame_set_num_channels(gfp, 1);
2633 else
2634 (void) lame_set_num_channels(gfp, 2);
2635
2636 if (lame_get_free_format(gfp)) {
2637 if (lame_get_brate(gfp) < 8 || lame_get_brate(gfp) > 640) {
2638 error_printf("For free format, specify a bitrate between 8 and 640 kbps\n");
2639 error_printf("with the -b <bitrate> option\n");
2640 return -1;
2641 }
2642 }
2643 if (num_nogap != NULL)
2644 *num_nogap = count_nogap;
2645 return 0;
2646 }
2647
2648 static int
2649 string_to_argv(char* str, char** argv, int N)
2650 {
2651 int argc = 0;
2652 if (str == 0) return argc;
2653 argv[argc++] = "lhama";
2654 for (;;) {
2655 int quoted = 0;
2656 while (isspace(*str)) { /* skip blanks */
2657 ++str;
2658 }
2659 if (*str == '\"') { /* is quoted argument ? */
2660 quoted = 1;
2661 ++str;
2662 }
2663 if (*str == '\0') { /* end of string reached */
2664 break;
2665 }
2666 /* found beginning of some argument */
2667 if (argc < N) {
2668 argv[argc++] = str;
2669 }
2670 /* look out for end of argument, either end of string, blank or quote */
2671 for(; *str != '\0'; ++str) {
2672 if (quoted) {
2673 if (*str == '\"') { /* end of quotation reached */
2674 *str++ = '\0';
2675 break;
2676 }
2677 }
2678 else {
2679 if (isspace(*str)) { /* parameter separator reached */
2680 *str++ = '\0';
2681 break;
2682 }
2683 }
2684 }
2685 }
2686 return argc;
2687 }
2688
2689 static int
2690 merge_argv(int argc, char** argv, int str_argc, char** str_argv, int N)
2691 {
2692 int i;
2693 if (argc > 0) {
2694 str_argv[0] = argv[0];
2695 if (str_argc < 1) str_argc = 1;
2696 }
2697 for (i = 1; i < argc; ++i) {
2698 int j = str_argc + i - 1;
2699 if (j < N) {
2700 str_argv[j] = argv[i];
2701 }
2702 }
2703 return argc + str_argc - 1;
2704 }
2705
2706 #ifdef DEBUG
2707 static void
2708 dump_argv(int argc, char** argv)
2709 {
2710 int i;
2711 for (i = 0; i < argc; ++i) {
2712 printf("%d: \"%s\"\n",i,argv[i]);
2713 }
2714 }
2715 #endif
2716
2717
2718 int parse_args(lame_t gfp, int argc, char **argv, char *const inPath, char *const outPath, char **nogap_inPath, int *num_nogap)
2719 {
2720 char *str_argv[512], *str;
2721 int str_argc, ret;
2722 str = lame_getenv("LAMEOPT");
2723 str_argc = string_to_argv(str, str_argv, dimension_of(str_argv));
2724 str_argc = merge_argv(argc, argv, str_argc, str_argv, dimension_of(str_argv));
2725 #ifdef DEBUG
2726 dump_argv(str_argc, str_argv);
2727 #endif
2728 ret = parse_args_(gfp, str_argc, str_argv, inPath, outPath, nogap_inPath, num_nogap);
2729 free(str);
2730 return ret;
2731 }
2732
2733 /* end of parse.c */