Commit e2f73f20 authored by Josh Coalson's avatar Josh Coalson
Browse files

redo option parsing using getopt_long

parent a1ae61d7
......@@ -33,443 +33,355 @@
#include "encode.h"
#include "file.h"
#if 0
/*[JEC] was:#if HAVE_GETOPT_LONG*/
/*[JEC] see flac/include/share/getopt.h as to why the change */
# include <getopt.h>
#else
# include "share/getopt.h"
#endif
typedef enum { RAW, WAV, AIF } FileFormat;
static int short_usage(const char *message, ...);
static int long_usage(const char *message, ...);
static int do_it();
static void init_options();
static int parse_options(int argc, char *argv[]);
static int parse_option(int short_option, const char *long_option, const char *option_argument);
static void free_options();
static int usage_error(const char *message, ...);
static void short_usage();
static void long_usage();
static void format_mistake(const char *infilename, const char *wrong, const char *right);
static int encode_file(const char *infilename, const char *forced_outfilename, FLAC__bool is_last_file);
static int decode_file(const char *infilename, const char *forced_outfilename);
FLAC__bool verify = false, verbose = true, continue_through_decode_errors = false, lax = false, test_only = false, analyze = false, use_ogg = false;
FLAC__bool do_mid_side = true, loose_mid_side = false, do_exhaustive_model_search = false, do_escape_coding = false, do_qlp_coeff_prec_search = false;
FLAC__bool force_to_stdout = false, force_raw_format = false, delete_input = false, sector_align = false;
const char *cmdline_forced_outfilename = 0, *output_prefix = 0;
analysis_options aopts = { false, false };
int padding = -1;
unsigned max_lpc_order = 8;
unsigned qlp_coeff_precision = 0;
FLAC__uint64 skip = 0;
int format_is_big_endian = -1, format_is_unsigned_samples = false;
int format_channels = -1, format_bps = -1, format_sample_rate = -1;
int blocksize = -1, min_residual_partition_order = -1, max_residual_partition_order = -1, rice_parameter_search_dist = -1;
char requested_seek_points[50000]; /* @@@ bad MAGIC NUMBER */
int num_requested_seek_points = -1; /* -1 => no -S options were given, 0 => -S- was given */
FLAC__int32 align_reservoir_0[588], align_reservoir_1[588]; /* for carrying over samples from --sector-align */
FLAC__int32 *align_reservoir[2] = { align_reservoir_0, align_reservoir_1 };
unsigned align_reservoir_samples = 0; /* 0 .. 587 */
static void die(const char *message);
static char *local_strdup(const char *source);
/*
* FLAC__share__getopt format struct; note that for long options with no
* short option equivalent we just set the 'val' field to 0.
*/
static struct FLAC__share__option long_options_[] = {
/*
* general options
*/
{ "--help", 0, 0, 'H' },
{ "--decode", 0, 0, 'd' },
{ "--analyze", 0, 0, 'a' },
{ "--test", 0, 0, 't' },
{ "--stdout", 0, 0, 'c' },
{ "--silent", 0, 0, 's' },
{ "--delete-input-file", 0, 0, 0 },
{ "--output-prefix", 1, 0, 0 },
{ "--output-name", 1, 0, 'o' },
{ "--skip", 1, 0, 0 },
/*
* decoding options
*/
{ "--decode-through-errors", 0, 0, 'F' },
/*
* encoding options
*/
{ "--compression-level-0", 0, 0, '0' },
{ "--compression-level-1", 0, 0, '1' },
{ "--compression-level-2", 0, 0, '2' },
{ "--compression-level-3", 0, 0, '3' },
{ "--compression-level-4", 0, 0, '4' },
{ "--compression-level-5", 0, 0, '5' },
{ "--compression-level-6", 0, 0, '6' },
{ "--compression-level-7", 0, 0, '7' },
{ "--compression-level-8", 0, 0, '8' },
{ "--compression-level-9", 0, 0, '9' },
{ "--best", 0, 0, '8' },
{ "--fast", 0, 0, '0' },
{ "--super-secret-impractical-compression-level", 0, 0, 0 },
{ "--verify", 0, 0, 'V' },
{ "--force-raw-input", 0, 0, 0 },
{ "--lax", 0, 0, 0 },
{ "--sector-align", 0, 0, 0 },
{ "--seekpoint", 1, 0, 'S' },
{ "--padding", 1, 0, 'P' },
#ifdef FLAC__HAS_OGG
{ "--ogg", 0, 0, 0 },
#endif
{ "--blocksize", 1, 0, 'b' },
{ "--exhaustive-model-search", 0, 0, 'e' },
#if 0
/* @@@ deprecated: */
{ "--escape-coding", 0, 0, 'E' },
#endif
{ "--max-lpc-order", 1, 0, 'l' },
{ "--mid-side", 0, 0, 'm' },
{ "--adaptive-mid-side", 0, 0, 'M' },
{ "--qlp-coeff-precision-search", 0, 0, 'p' },
{ "--qlp-coeff-precision", 1, 0, 'q' },
{ "--rice-partition-order", 1, 0, 'r' },
#if 0
/* @@@ deprecated: */
{ "--rice-parameter-search-distance", 1, 0, 'R' },
#endif
{ "--endian", 1, 0, 0 },
{ "--channels", 1, 0, 0 },
{ "--bps", 1, 0, 0 },
{ "--sample-rate", 1, 0, 0 },
{ "--sign", 1, 0, 0 },
/*
* analysis options
*/
{ "--residual-gnu-plot", 0, 0, 0 },
{ "--residual-text", 0, 0, 0 },
/*
* negatives
*/
{ "--no-decode-through-errors", 0, 0, 0 },
{ "--no-silent", 0, 0, 0 },
{ "--no-seektable", 0, 0, 0 },
{ "--no-delete-input-file", 0, 0, 0 },
{ "--no-sector-align", 0, 0, 0 },
{ "--no-lax", 0, 0, 0 },
#ifdef FLAC__HAS_OGG
{ "--no-ogg", 0, 0, 0 },
#endif
{ "--no-exhaustive-model-search", 0, 0, 0 },
#if 0
/* @@@ deprecated: */
{ "--no-escape-coding", 0, 0, 0 },
#endif
{ "--no-mid-side", 0, 0, 0 },
{ "--no-adaptive-mid-side", 0, 0, 0 },
{ "--no-qlp-coeff-prec-search", 0, 0, 0 },
{ "--no-padding", 0, 0, 0 },
{ "--no-verify", 0, 0, 0 },
{ "--no-residual-gnuplot", 0, 0, 0 },
{ "--no-residual-text", 0, 0, 0 },
{0, 0, 0, 0}
};
/*
* global to hold command-line option values
*/
static struct {
FLAC__bool show_long_help;
FLAC__bool mode_decode;
FLAC__bool verify;
FLAC__bool verbose;
FLAC__bool continue_through_decode_errors;
FLAC__bool lax;
FLAC__bool test_only;
FLAC__bool analyze;
FLAC__bool use_ogg;
FLAC__bool do_mid_side;
FLAC__bool loose_mid_side;
FLAC__bool do_exhaustive_model_search;
FLAC__bool do_escape_coding;
FLAC__bool do_qlp_coeff_prec_search;
FLAC__bool force_to_stdout;
FLAC__bool force_raw_format;
FLAC__bool delete_input;
FLAC__bool sector_align;
const char *cmdline_forced_outfilename;
const char *output_prefix;
analysis_options aopts;
int padding;
unsigned max_lpc_order;
unsigned qlp_coeff_precision;
FLAC__uint64 skip;
int format_is_big_endian;
int format_is_unsigned_samples;
int format_channels;
int format_bps;
int format_sample_rate;
int blocksize;
int min_residual_partition_order;
int max_residual_partition_order;
int rice_parameter_search_dist;
char requested_seek_points[50000]; /* @@@ bad MAGIC NUMBER */
int num_requested_seek_points; /* -1 => no -S options were given, 0 => -S- was given */
unsigned num_files;
char **filenames;
} option_values;
/*
* miscellaneous globals
*/
static FLAC__int32 align_reservoir_0[588], align_reservoir_1[588]; /* for carrying over samples from --sector-align */
static FLAC__int32 *align_reservoir[2] = { align_reservoir_0, align_reservoir_1 };
static unsigned align_reservoir_samples = 0; /* 0 .. 587 */
static const char *flac_suffix = ".flac", *ogg_suffix = ".ogg";
int main(int argc, char *argv[])
{
int i, retval = 0;
FLAC__bool mode_decode = false;
if(argc <= 1)
return short_usage(0);
/* get the options */
for(i = 1; i < argc; i++) {
if(argv[i][0] != '-' || argv[i][1] == 0)
break;
if(0 == strcmp(argv[i], "-H") || 0 == strcmp(argv[i], "--help"))
return long_usage(0);
else if(0 == strcmp(argv[i], "-d"))
mode_decode = true;
else if(0 == strcmp(argv[i], "-a")) {
mode_decode = true;
analyze = true;
}
else if(0 == strcmp(argv[i], "-t")) {
mode_decode = true;
test_only = true;
}
else if(0 == strcmp(argv[i], "-c"))
force_to_stdout = true;
else if(0 == strcmp(argv[i], "-F"))
continue_through_decode_errors = true;
else if(0 == strcmp(argv[i], "-F-"))
continue_through_decode_errors = false;
else if(0 == strcmp(argv[i], "-s"))
verbose = false;
else if(0 == strcmp(argv[i], "-s-"))
verbose = true;
else if(0 == strcmp(argv[i], "-S")) {
if(++i >= argc)
return long_usage("ERROR: must specify a value with -S\n");
if(num_requested_seek_points < 0)
num_requested_seek_points = 0;
num_requested_seek_points++;
strcat(requested_seek_points, argv[i]);
strcat(requested_seek_points, "<");
}
else if(0 == strcmp(argv[i], "-S-")) {
num_requested_seek_points = 0;
requested_seek_points[0] = '\0';
}
else if(0 == strcmp(argv[i], "--delete-input-file"))
delete_input = true;
else if(0 == strcmp(argv[i], "--delete-input-file-"))
delete_input = false;
else if(0 == strcmp(argv[i], "--output-prefix")) {
if(++i >= argc)
return long_usage("ERROR: must specify a value with --output-prefix\n");
output_prefix = argv[i];
}
else if(0 == strcmp(argv[i], "--sector-align"))
sector_align = true;
else if(0 == strcmp(argv[i], "--sector-align-"))
sector_align = false;
else if(0 == strcmp(argv[i], "--skip")) {
if(++i >= argc)
return long_usage("ERROR: must specify a value with --skip\n");
skip = (FLAC__uint64)atoi(argv[i]); /* @@@ takes a pretty damn big file to overflow atoi() here, but it could happen */
}
else if(0 == strcmp(argv[i], "--lax"))
lax = true;
else if(0 == strcmp(argv[i], "--lax-"))
lax = false;
#ifdef FLAC__HAS_OGG
else if(0 == strcmp(argv[i], "--ogg"))
use_ogg = true;
else if(0 == strcmp(argv[i], "--ogg-"))
use_ogg = false;
#endif
else if(0 == strcmp(argv[i], "-b")) {
if(++i >= argc)
return long_usage("ERROR: must specify a value with -b\n");
blocksize = atoi(argv[i]);
}
else if(0 == strcmp(argv[i], "-e"))
do_exhaustive_model_search = true;
else if(0 == strcmp(argv[i], "-e-"))
do_exhaustive_model_search = false;
else if(0 == strcmp(argv[i], "-E"))
do_escape_coding = true;
else if(0 == strcmp(argv[i], "-E-"))
do_escape_coding = false;
else if(0 == strcmp(argv[i], "-l")) {
if(++i >= argc)
return long_usage("ERROR: must specify a value with -l\n");
max_lpc_order = atoi(argv[i]);
}
else if(0 == strcmp(argv[i], "-m")) {
do_mid_side = true;
loose_mid_side = false;
}
else if(0 == strcmp(argv[i], "-m-"))
do_mid_side = loose_mid_side = false;
else if(0 == strcmp(argv[i], "-M"))
loose_mid_side = do_mid_side = true;
else if(0 == strcmp(argv[i], "-M-"))
loose_mid_side = do_mid_side = false;
else if(0 == strcmp(argv[i], "-o")) {
if(++i >= argc)
return long_usage("ERROR: must specify a value with -o\n");
cmdline_forced_outfilename = argv[i];
}
else if(0 == strcmp(argv[i], "-p"))
do_qlp_coeff_prec_search = true;
else if(0 == strcmp(argv[i], "-p-"))
do_qlp_coeff_prec_search = false;
else if(0 == strcmp(argv[i], "-P")) {
if(++i >= argc)
return long_usage("ERROR: must specify a value with -P\n");
padding = atoi(argv[i]);
if(padding < 0)
return long_usage("ERROR: argument to -P must be >= 0\n");
}
else if(0 == strcmp(argv[i], "-P-"))
padding = -1;
else if(0 == strcmp(argv[i], "-q")) {
if(++i >= argc)
return long_usage("ERROR: must specify a value with -q\n");
qlp_coeff_precision = atoi(argv[i]);
}
else if(0 == strcmp(argv[i], "-r")) {
char *p;
if(++i >= argc)
return long_usage("ERROR: must specify a value with -r\n");
p = strchr(argv[i], ',');
if(0 == p) {
min_residual_partition_order = 0;
max_residual_partition_order = atoi(argv[i]);
int retval = 0;
init_options();
if((retval = parse_options(argc, argv)) == 0)
retval = do_it();
free_options();
return retval;
}
int do_it()
{
int retval = 0;
if(option_values.show_long_help) {
long_usage();
return 0;
}
else {
if(option_values.num_files == 0) {
short_usage();
return 0;
}
/*
* tweak options; validate the values
*/
if(!option_values.mode_decode) {
if(option_values.blocksize < 0) {
if(option_values.max_lpc_order == 0)
option_values.blocksize = 1152;
else
option_values.blocksize = 4608;
}
else {
min_residual_partition_order = atoi(argv[i]);
max_residual_partition_order = atoi(++p);
if(option_values.max_residual_partition_order < 0) {
if(option_values.blocksize <= 1152)
option_values.max_residual_partition_order = 2;
else if(option_values.blocksize <= 2304)
option_values.max_residual_partition_order = 3;
else if(option_values.blocksize <= 4608)
option_values.max_residual_partition_order = 3;
else
option_values.max_residual_partition_order = 4;
option_values.min_residual_partition_order = option_values.max_residual_partition_order;
}
if(option_values.rice_parameter_search_dist < 0) {
option_values.rice_parameter_search_dist = 0;
}
}
else if(0 == strcmp(argv[i], "--old-unworking-do-not-use-R")) {
if(++i >= argc)
return long_usage("ERROR: must specify a value with -R\n");
rice_parameter_search_dist = atoi(argv[i]);
}
else if(0 == strcmp(argv[i], "-V"))
verify = true;
else if(0 == strcmp(argv[i], "-V-"))
verify = false;
else if(0 == strcmp(argv[i], "-fb"))
format_is_big_endian = true;
else if(0 == strcmp(argv[i], "-fl"))
format_is_big_endian = false;
else if(0 == strcmp(argv[i], "-fc")) {
if(++i >= argc)
return long_usage("ERROR: must specify a value with -fc\n");
format_channels = atoi(argv[i]);
}
else if(0 == strcmp(argv[i], "-fp")) {
if(++i >= argc)
return long_usage("ERROR: must specify a value with -fp\n");
format_bps = atoi(argv[i]);
}
else if(0 == strcmp(argv[i], "-fs")) {
if(++i >= argc)
return long_usage("ERROR: must specify a value with -fs\n");
format_sample_rate = atoi(argv[i]);
}
else if(0 == strcmp(argv[i], "-fu"))
format_is_unsigned_samples = true;
else if(0 == strcmp(argv[i], "-fr"))
force_raw_format = true;
else if(0 == strcmp(argv[i], "--a-rgp"))
aopts.do_residual_gnuplot = true;
else if(0 == strcmp(argv[i], "--a-rgp-"))
aopts.do_residual_gnuplot = false;
else if(0 == strcmp(argv[i], "--a-rtext"))
aopts.do_residual_text = true;
else if(0 == strcmp(argv[i], "--a-rtext-"))
aopts.do_residual_text = false;
else if(0 == strcmp(argv[i], "-0") || 0 == strcmp(argv[i], "--fast")) {
do_exhaustive_model_search = false;
do_escape_coding = false;
do_mid_side = false;
loose_mid_side = false;
qlp_coeff_precision = 0;
min_residual_partition_order = max_residual_partition_order = 2;
rice_parameter_search_dist = 0;
max_lpc_order = 0;
}
else if(0 == strcmp(argv[i], "-1")) {
do_exhaustive_model_search = false;
do_escape_coding = false;
do_mid_side = true;
loose_mid_side = true;
qlp_coeff_precision = 0;
min_residual_partition_order = max_residual_partition_order = 2;
rice_parameter_search_dist = 0;
max_lpc_order = 0;
}
else if(0 == strcmp(argv[i], "-2")) {
do_exhaustive_model_search = false;
do_escape_coding = false;
do_mid_side = true;
loose_mid_side = false;
qlp_coeff_precision = 0;
min_residual_partition_order = 0;
max_residual_partition_order = 3;
rice_parameter_search_dist = 0;
max_lpc_order = 0;
}
else if(0 == strcmp(argv[i], "-3")) {
do_exhaustive_model_search = false;
do_escape_coding = false;
do_mid_side = false;
loose_mid_side = false;
qlp_coeff_precision = 0;
min_residual_partition_order = max_residual_partition_order = 3;
rice_parameter_search_dist = 0;
max_lpc_order = 6;
}
else if(0 == strcmp(argv[i], "-4")) {
do_exhaustive_model_search = false;
do_escape_coding = false;
do_mid_side = true;
loose_mid_side = true;
qlp_coeff_precision = 0;
min_residual_partition_order = max_residual_partition_order = 3;
rice_parameter_search_dist = 0;
max_lpc_order = 8;
}
else if(0 == strcmp(argv[i], "-5")) {
do_exhaustive_model_search = false;
do_escape_coding = false;
do_mid_side = true;
loose_mid_side = false;
qlp_coeff_precision = 0;
min_residual_partition_order = max_residual_partition_order = 3;
rice_parameter_search_dist = 0;
max_lpc_order = 8;
}
else if(0 == strcmp(argv[i], "-6")) {
do_exhaustive_model_search = false;
do_escape_coding = false;
do_mid_side = true;
loose_mid_side = false;
qlp_coeff_precision = 0;
min_residual_partition_order = 0;
max_residual_partition_order = 4;
rice_parameter_search_dist = 0;
max_lpc_order = 8;
}
else if(0 == strcmp(argv[i], "-7")) {
do_exhaustive_model_search = true;
do_escape_coding = false;
do_mid_side = true;
loose_mid_side = false;
qlp_coeff_precision = 0;
min_residual_partition_order = 0;
max_residual_partition_order = 6;
rice_parameter_search_dist = 0;
max_lpc_order = 8;
}
else if(0 == strcmp(argv[i], "-8") || 0 == strcmp(argv[i], "--best")) {
do_exhaustive_model_search = true;
do_escape_coding = false;
do_mid_side = true;
loose_mid_side = false;
qlp_coeff_precision = 0;
min_residual_partition_order = 0;
max_residual_partition_order = 6;
rice_parameter_search_dist = 0;
max_lpc_order = 12;
}
else if(0 == strcmp(argv[i], "--super-secret-impractical-compression-level")) {
do_exhaustive_model_search = true;
do_escape_coding = true;
do_mid_side = true;
loose_mid_side = false;
do_qlp_coeff_prec_search = true;
min_residual_partition_order = 0;
max_residual_partition_order = 16;
rice_parameter_search_dist = 0;
max_lpc_order = 32;
}
else if(isdigit((int)(argv[i][1]))) {
return long_usage("ERROR: compression level '%s' is reserved\n", argv[i]);
}
else {
return long_usage("ERROR: invalid option '%s'\n", argv[i]);
if(option_values.test_only) {
if(option_values.skip > 0)
return usage_error("ERROR: --skip is not allowed in test mode\n");
}
}
}
/* tweak options; validate the values */
if(!mode_decode) {
if(blocksize < 0) {
if(max_lpc_order == 0)
blocksize = 1152;
else
blocksize = 4608;
}
if(max_residual_partition_order < 0) {
if(blocksize <= 1152)
max_residual_partition_order = 2;
else if(blocksize <= 2304)
max_residual_partition_order = 3;
else if(blocksize <= 4608)
max_residual_partition_order = 3;
else
max_residual_partition_order = 4;
min_residual_partition_order = max_residual_partition_order;
FLAC__ASSERT(option_values.blocksize >= 0 || option_values.mode_decode);
if(option_values.format_channels >= 0) {
if(option_values.format_channels == 0 || (unsigned)option_values.format_channels > FLAC__MAX_CHANNELS)
return usage_error("ERROR: invalid number of channels '%u', must be > 0 and <= %u\n", option_values.format_channels, FLAC__MAX_CHANNELS);
}
if(rice_parameter_search_dist < 0) {
rice_parameter_search_dist = 0;
if(option_values.format_bps >= 0) {
if(option_values.format_bps != 8 && option_values.format_bps != 16 && option_values.format_bps != 24)
return usage_error("ERROR: invalid bits per sample '%u' (must be 8/16/24)\n", option_values.format_bps);
}
}
else {
if(test_only) {
if(skip > 0)
return long_usage("ERROR: --skip is not allowed in test mode\n");
if(option_values.format_sample_rate >= 0) {
if(!FLAC__format_sample_rate_is_valid(option_values.format_sample_rate))
return usage_error("ERROR: invalid sample rate '%u', must be > 0 and <= %u\n", option_values.format_sample_rate, FLAC__MAX_SAMPLE_RATE);
}
if(!option_values.mode_decode && ((unsigned)option_values.blocksize < FLAC__MIN_BLOCK_SIZE || (unsigned)option_values.blocksize > FLAC__MAX_BLOCK_SIZE)) {
return usage_error("ERROR: invalid blocksize '%u', must be >= %u and <= %u\n", (unsigned)option_values.blocksize, FLAC__MIN_BLOCK_SIZE, FLAC__MAX_BLOCK_SIZE);
}
if(option_values.qlp_coeff_precision > 0 && option_values.qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION) {
return usage_error("ERROR: invalid value for -q '%u', must be 0 or >= %u\n", option_values.qlp_coeff_precision, FLAC__MIN_QLP_COEFF_PRECISION);
}
}
FLAC__ASSERT(blocksize >= 0 || mode_decode);