1236 lines · 35.8 KB · C
RawDownload
/* *	Command line parsing related functions * *	Copyright (c) 1999 Mark Taylor * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ /* $Id: parse.c,v 1.68 2001/03/11 11:24:25 aleidinger Exp $ */ #include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>#include <ctype.h>#ifdef HAVE_CONFIG_H# include <config.h>#endif #include "lame.h"#include "brhist.h"#include "parse.h"#include "main.h"#include "get_audio.h" /* GLOBAL VARIABLES.  set by parse_args() *//* we need to clean this up */sound_file_format input_format;int	swapbytes;		/* force byte swapping   default=0*/int	silent;int	brhist;float	update_interval;	/* to use Frank's time status display */int	mp3_delay;		/* to adjust the number of samples truncated                               during decode */int	mp3_delay_set;		/* user specified the value of the mp3 encoder                               delay to assume for decoding */ /** license - Writes version and license to the file specified by fp*/static intlame_version_print(FILE*const fp){	const char *v = get_lame_version();	const char *u = get_lame_url();	const int lenv = strlen(v);	const int lenu = strlen (u);	const int lw = 80;	/* line width of terminal in characters */	const int sw = 16;	/* static width of text */ 	if (lw >= lenv + lenu + sw || lw < lenu + 2)		/* text fits in 80 chars/line, or line even too small for url */		fprintf(fp, "mp3enc (from lame version %s (%s))\n\n", v, u);	else		/* text too long, wrap url into next line, right aligned */		fprintf(fp, "mp3enc (from lame version %s)\n%*s(%s)\n\n",			v, lw - 2 - lenu, "", u);	return 0;} /* print version & license */intprint_license(const lame_global_flags*gfp, FILE*const fp, const char*ProgramName){	lame_version_print(fp);	fprintf(fp, "Can I use LAME in my commercial program?\n\n"		"Yes, you can, under the restrictions of the LGPL.  In particular, you\n"		"can include a compiled version of the LAME library (for example,\n"		"lame.dll) with a commercial program.  Some notable requirements of\n"		"the LGPL:\n"		"\n"		"1. In your program, you cannot include any source code from LAME, with\n"		"   the exception of files whose only purpose is to describe the library\n"		"   interface (such as lame.h).\n"		"\n"		"2. Any modifications of LAME must be released under the LGPL.\n"		"   The LAME project (www.mp3dev.org) would appreciate being\n"		"   notified of any modifications.\n"		"\n"		"3. You must give prominent notice that your program is:\n"		"      A. using LAME (including version number)\n"		"      B. LAME is under the LGPL\n"		"      C. Provide a copy of the LGPL.  (the file COPYING contains the LGPL)\n"		"      D. Provide a copy of LAME source, or a pointer where the LAME\n"		"         source can be obtained (such as www.mp3dev.org)\n"		"   An example of prominent notice would be an \"About the LAME encoding engine\"\n"		"   button in some pull down menu within the executable of your program.\n"		"\n"		"4. If you determine that distribution of LAME requires a patent license,\n"		"   you must obtain such license.\n"		"\n"		"\n"		"*** IMPORTANT NOTE ***\n"		"\n"		"The decoding functions provided in LAME use the mpglib decoding engine which\n"		"is under the GPL.  They may not be used by any program not released under the\n"		"GPL unless you obtain such permission from the MPG123 project (www.mpg123.de).\n"		"\n");	return 0;} /** usage - Writes command line syntax to the file specified by fp*//* print general syntax */intusage(const lame_global_flags*gfp, FILE*const fp, const char*ProgramName){	lame_version_print(fp);	fprintf(fp, "usage: %s [options] <infile> [outfile]\n\n"		"    <infile> and/or <outfile> can be \"-\", which means stdin/stdout.\n"		"\n"		"Try  \"%s --help\"     for more information\n"		"  or \"%s --longhelp\"\n"		"  or \"%s -?\"         for a complete options list\n\n",		ProgramName, ProgramName, ProgramName, ProgramName);	return 0;} /** short_help - Writes command line syntax to the file specified by fp*           but only the most important ones, to fit on a vt100 terminal*//* print short syntax help */intshort_help(const lame_global_flags*gfp, FILE*const fp, const char*ProgramName){	lame_version_print(fp);	fprintf(fp, "usage: %s [options] <infile> [outfile]\n\n"		"    <infile> and/or <outfile> can be \"-\", which means stdin/stdout.\n"		"\n"		"RECOMMENDED:\n"		"    mp3enc -h input.wav output.mp3\n"		"\n"		"OPTIONS:\n"		"    -b bitrate      set the bitrate, default 128 kbps\n"		"    -f              fast mode (lower quality)\n"		"    -h              higher quality, but a little slower.  Recommended.\n"		"    -k              keep ALL frequencies (disables all filters)\n"		"                    Can cause ringing and twinkling\n"		"    -m mode         (s)tereo, (j)oint, (m)ono or (a)uto\n"		"                    default is (j) or (s) depending on bitrate\n"		"    -V n            quality setting for VBR.  default n=%i\n"		"\n"		"    --preset type   type must be phone, voice, fm, tape, hifi, cd or studio\n"		"                    \"--preset help\" gives some more infos on these\n"		"\n"		"    --longhelp      full list of options\n"		"\n",		ProgramName, gfp->VBR_q);	return 0;} /** wait_for - Writes command line syntax to the file specified by fp*/static voidwait_for (FILE*const fp, int lessmode){	if (lessmode ) {		fflush(fp);		getchar ();	} else		fprintf(fp, "\n");	fprintf(fp, "\n");} /* print long syntax help */intlong_help(const lame_global_flags*gfp, FILE*const fp, const char*ProgramName, int lessmode){	lame_version_print(fp);	fprintf(fp, "usage: %s [options] <infile> [outfile]\n\n"		"    <infile> and/or <outfile> can be \"-\", which means stdin/stdout.\n"		"\n"		"RECOMMENDED:\n"		"    mp3enc -h input.wav output.mp3\n"		"\n"		"OPTIONS:\n"		"  Input options:\n"		"    -r              input is raw pcm\n"		"    -x              force byte-swapping of input\n"		"    -s sfreq        sampling frequency of input file (kHz) - default 44.1 kHz\n"		"    --mp1input      input file is a MPEG Layer I   file\n"		"    --mp2input      input file is a MPEG Layer II  file\n"		"    --mp3input      input file is a MPEG Layer III file\n"		"    --ogginput      input file is a Ogg Vorbis file",		ProgramName);	wait_for (fp, lessmode);	fprintf(fp, "  Operational options:\n"		"    -m <mode>       (s)tereo, (j)oint, (f)orce, (m)ono or (a)auto  \n"		"                    default is (s) or (j) depending on bitrate\n"		"                    force = force ms_stereo on all frames.\n"		"                    auto = jstereo, with varialbe mid/side threshold\n"		"    -a              downmix from stereo to mono file for mono encoding\n"		"    -d              allow channels to have different blocktypes\n"		"    --disptime <arg>print progress report every arg seconds\n"		"    --ogg           encode to Ogg Vorbis instead of MP3\n"		"    --freeformat    produce a free format bitstream\n"		"    --decode        input=mp3 file, output=wav\n"		"    -t              disable writing wav header when using --decode\n"		"    --comp  <arg>   choose bitrate to achive a compression ratio of <arg>\n"		"    --scale <arg>   scale input (multiply PCM data) by <arg>\n"		"    --athonly       only use the ATH for masking\n"		"    --noath         disable the ATH for masking\n"		"    --athlower x    lower the ATH x dB\n"		"    --notemp        disable temporal masking effect\n"		"    --short         use short blocks\n"		"    --noshort       do not use short blocks\n"		"    --voice         experimental voice mode\n"		"    --preset type   type must be phone, voice, fm, tape, hifi, cd or studio\n"		"                    \"--preset help\" gives some more infos on these");	wait_for (fp, lessmode);	fprintf(fp, "  Verbosity:\n"		"    -S              don't print progress report, VBR histograms\n"		"    --silent        don't print anything on screen\n"		"    --quiet         don't print anything on screen\n"		"    --verbose       print a lot of useful information, default\n"		"\n"		"  Noise shaping & psycho acoustic algorithms:\n"		"    -q <arg>        <arg> = 0...9.  Default  -q 5 \n"		"                    -q 0:  Highest quality, very slow \n"		"                    -q 9:  Poor quality, but fast \n"		"    -h              Same as -q 2.   Recommended.\n"		"    -f              Same as -q 7.   Fast, ok quality\n");	wait_for (fp, lessmode);	fprintf(fp, "  CBR (constant bitrate, the default) options:\n"		"    -b <bitrate>    set the bitrate in kbps, default 128 kbps\n"		"\n"		"  ABR options:\n"		"    --abr <bitrate> specify average bitrate desired (instead of quality)\n"		"\n"		"  VBR options:\n"		"    -v              use variable bitrate (VBR) (--vbr-old)\n"		"    --vbr-old       use old variable bitrate (VBR) routine\n"		"    --vbr-new       use new variable bitrate (VBR) routine\n"		"    --vbr-mtrh      a merger of old and new (VBR) routine\n"		"    -V n            quality setting for VBR.  default n=%i\n"		"                    0=high quality,bigger files. 9=smaller files\n"		"    -b <bitrate>    specify minimum allowed bitrate, default  32 kbps\n"		"    -B <bitrate>    specify maximum allowed bitrate, default 320 kbps\n"		"    -F              strictly enforce the -b option, for use with players that\n"		"                    do not support low bitrate mp3 (Apex AD600-A DVD/mp3 player)\n"		"    -t              disable writing Xing VBR informational tag\n"		"    --nohist        disable VBR histogram display", gfp->VBR_q); 	wait_for (fp, lessmode);	fprintf(fp, "  MP3 header/stream options:\n"		"    -e <emp>        de-emphasis n/5/c  (obsolete)\n"		"    -c              mark as copyright\n"		"    -o              mark as non-original\n"		"    -p              error protection.  adds 16 bit checksum to every frame\n"		"                    (the checksum is computed correctly)\n"		"    --nores         disable the bit reservoir\n"		"    --strictly-enforce-ISO   comply as much as possible to ISO MPEG spec\n"		"\n"		"  Filter options:\n"		"    -k              keep ALL frequencies (disables all filters),\n"		"                    Can cause ringing and twinkling\n"		"  --lowpass <freq>        frequency(kHz), lowpass filter cutoff above freq\n"		"  --lowpass-width <freq>  frequency(kHz) - default 15%% of lowpass freq\n"		"  --highpass <freq>       frequency(kHz), highpass filter cutoff below freq\n"		"  --highpass-width <freq> frequency(kHz) - default 15%% of highpass freq\n"		"  --resample <sfreq>  sampling frequency of output file(kHz)- default=automatic\n"		"  --cwlimit <freq>    compute tonality up to freq (in kHz) default 8.8717"); 	wait_for (fp, lessmode);	fprintf(fp,		"  ID3 tag options:\n"		"    --tt <title>    audio/song title (max 30 chars for version 1 tag)\n"		"    --ta <artist>   audio/song artist (max 30 chars for version 1 tag)\n"		"    --tl <album>    audio/song album (max 30 chars for version 1 tag)\n"		"    --ty <year>     audio/song year of issue (1 to 9999)\n"		"    --tc <comment>  user-defined text (max 30 chars for v1 tag, 28 for v1.1)\n"		"    --tn <track>    audio/song track number (1 to 255, creates v1.1 tag)\n"		"    --tg <genre>    audio/song genre (name or number in list)\n"		"    --add-id3v2     force addition of version 2 tag\n"		"    --id3v1-only    add only a version 1 tag\n"		"    --id3v2-only    add only a version 2 tag\n"		"    --space-id3v1   pad version 1 tag with spaces instead of nulls\n"		"    --pad-id3v2     pad version 2 tag with extra 128 bytes\n"		"    --genre-list    print alphabetically sorted ID3 genre list and exit\n"		"\n"		"    Note: A version 2 tag will NOT be added unless one of the input fields\n"		"    won't fit in a version 1 tag (e.g. the title string is longer than 30\n"		"    characters), or the '--add-id3v2' or '--id3v2-only' options are used,\n"		"    or output is redirected to stdout."#if defined(HAVE_VORBIS)		"\n\n"		"    Note: All '--t*' options (except those for track and genre) work for Ogg\n"		"    Vorbis output, but other ID3-specific options are ignored."#endif		);	wait_for (fp, lessmode);	display_bitrates(fp);	return 0;} static voiddisplay_bitrate(FILE*const fp, const char*const version, const int div, const int index){	int	i; 	fprintf(fp, "\nMPEG-%-3s layer III sample frequencies (kHz):  %2d  %2d  %g\n"		"bitrates (kbps):",		version, 32 / div, 48 / div, 44.1 / div);	for (i = 1; i <= 14; i++)		fprintf(fp, " %2i", bitrate_table [index] [i]);	fprintf(fp, "\n");} intdisplay_bitrates(FILE*const fp){	display_bitrate(fp, "1"  , 1, 1);	display_bitrate(fp, "2"  , 2, 0);	display_bitrate(fp, "2.5", 4, 0);	fprintf(fp, "\n");	fflush(fp);	return 0;} typedef struct {	const char*name;	/* name of preset */	long	resample;	/* resample frequency in Hz, or -1 for no resampling */	short	highpass_freq;	/* highpass frequency in Hz, or -1 for no highpass filtering */	short	lowpass_freq;	/* lowpass frequency in Hz, or -1 for no lowpass filtering */	short	lowpass_width;	/* lowpass width in Hz */	signed char no_short_blocks;	/* use of short blocks, 1: no, 0: yes */	signed char quality;	/* quality, the same as -f or -h */	MPEG_mode mode;		/* channel mode (mono, stereo, joint) */	short	cbr;		/* CBR data rate in kbps (8...320) */	signed char xvbr_mode;	/* VBR mode (0...9) */	short	vbr_min;	/* minimum VBR rate in kbps(8...256) */	short	vbr_max;	/* maximum VBR rate in kbps (16...320) */} preset_t; const preset_t Presets [] = {/* name       fs     fu    fo    dfo shrt qual  mode       cbr vbr_mode/min/max */{ "phone" ,  8000, 125,  3400,    0,  1,  5, MONO        ,  16,  6,   8,  24 },	/* phone standard 300-3400 */{ "phon+" , 11025, 100,  4000,    0,  1,  5, MONO        ,  24,  4,  16,  32 },	/* phone theoretical limits */{ "lw"    , 11025,  -1,  4000,    0,  0,  5, MONO        ,  24,  3,  16,  56 },	/* LW */{ "mw-eu" , 11025,  -1,  4000,    0,  0,  5, MONO        ,  24,  3,  16,  56 },	/* MW in europe */{ "mw-us" , 16000,  -1,  7500,    0,  0,  5, MONO        ,  40,  3,  24, 112 },	/* MW in U.S.A. */{ "sw"    , 11025,  -1,  4000,    0,  0,  5, MONO        ,  24,  3,  16,  56 },	/* SW */{ "fm"    , 32000,  -1, 15000,    0,  0,  3, JOINT_STEREO, 112,  3,  80, 256 },{ "voice" , 24000,  -1, 12000,    0,  1,  5, MONO        ,  56,  4,  40, 112 },{ "radio" ,    -1,  -1, 15000,    0,  0,  3, JOINT_STEREO, 128,  3,  96, 256 },{ "tape"  ,    -1,  -1, 18000,  900,  0,  3, JOINT_STEREO, 128,  3,  96, 256 },{ "hifi"  ,    -1,  -1, 18000,  900,  0, -1, JOINT_STEREO, 160,  2, 112, 320 },{ "cd"    ,    -1,  -1,    -1,   -1,  0, -1, STEREO      , 192,  1, 128, 320 },{ "studio",    -1,  -1,    -1,   -1,  0, -1, STEREO      , 256,  0, 160, 320 },}; /** Writes presetting info to #stdout#*/ /* print possible combination */static intpresets_info(const lame_global_flags*gfp, FILE*const fp, const char*ProgramName){	int	i; 	fprintf(fp, "\n");	lame_version_print(fp); 	fprintf(fp, "Presets are some shortcuts for common settings.\n");	fprintf(fp, "They can be combined with -v if you want VBR MP3s.\n"); 	fprintf(fp, "\n                ");	for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)		fprintf(fp,  strlen(Presets[i].name) <= 4? "%5s ": " %-5s",			Presets[i].name);	fprintf(fp, "\n=================");	for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)		fprintf(fp,  "======");	fprintf(fp, "\n--resample      ");	for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)		if (Presets[i].resample < 0)			fprintf(fp,  "      ");		else			fprintf(fp,  "%6.3g",  Presets[i].resample * 1.e-3);	fprintf(fp, "\n--highpass      ");	for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)		if (Presets[i].highpass_freq < 0)			fprintf(fp,  "      ");		else			fprintf(fp,  "%6.3g",  Presets[i].highpass_freq * 1.e-3);	fprintf(fp, "\n--lowpass       ");	for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)		if (Presets[i].lowpass_freq < 0)			fprintf(fp,  "      ");		else			fprintf(fp,  "%6.3g",  Presets[i].lowpass_freq * 1.e-3);	fprintf(fp, "\n--lowpass-width ");	for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)		if (Presets[i].lowpass_width < 0)			fprintf(fp,  "      ");		else			fprintf(fp,  "%6.3g",  Presets[i].lowpass_width * 1.e-3);	fprintf(fp, "\n--noshort       ");	for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)		switch (Presets[i].no_short_blocks ) {		case 1:			fprintf(fp,  "   yes");			break;		case 0:			fprintf(fp,  "    no");			break;		case -1:			fprintf(fp,  "      ");			break;		default:			assert (0);			break;		}	fprintf(fp, "\n                ");	for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)		switch (Presets[i].mode ) {		case MONO:			fprintf(fp, "   -mm");			break;		case JOINT_STEREO:			fprintf(fp, "   -mj");			break;		case STEREO:			fprintf(fp, "   -ms");			break;		case -1:			fprintf(fp, "      ");			break;		default:			assert (0);			break;		}	fprintf(fp, "\n                ");	for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)		switch (Presets[i].quality ) {		case -1:			fprintf(fp, "      ");			break;		case 2:			fprintf(fp, "    -h");			break;		case 3:			fprintf(fp, "   -q3");			break;		case 5:			fprintf(fp, "      ");			break;		case 7:			fprintf(fp, "    -f");			break;		default:			assert (0);			break;		}	fprintf(fp, "\n-b              ");	for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)		fprintf(fp,  "%6u", Presets[i].cbr);	fprintf(fp, "\n-- PLUS WITH -v ");	for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)		fprintf(fp,  "------");	fprintf(fp, "-\n-V              ");	for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)		fprintf(fp,  "%6u", Presets[i].xvbr_mode);	fprintf(fp, "\n-b              ");	for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)		fprintf(fp,  "%6u", Presets[i].vbr_min);	fprintf(fp, "\n-B              ");	for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)		fprintf(fp,  "%6u", Presets[i].vbr_max);	fprintf(fp, "\n----------------");	for (i = 0; i < sizeof(Presets) / sizeof(*Presets); i++)		fprintf(fp,  "------"); 	fprintf(fp, "-\nEXAMPLES:\n");	fprintf(fp, " a) --preset fm\n");	fprintf(fp, "    equal to: -mj -b112 --resample 32 --lowpass 15 --lowpass-width 0\n");	fprintf(fp, " b) -v --preset studio\n");	fprintf(fp, "    equals to: -h -ms -V0 -b160 -B320\n"); 	return 0;}  

Lines 1–500 of 1236.