Debian Codegraph @

frontend/parse.c

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