static int using_stdin = 0;
static int input_sync;
-void reset_options(OptionsContext *o)
+static void uninit_options(OptionsContext *o)
{
const OptionDef *po = options;
int i;
av_freep(&o->stream_maps);
av_freep(&o->meta_data_maps);
av_freep(&o->streamid_map);
+}
+static void init_options(OptionsContext *o)
+{
memset(o, 0, sizeof(*o));
o->mux_max_delay = 0.7;
o->recording_time = INT64_MAX;
o->limit_filesize = UINT64_MAX;
o->chapters_input_file = INT_MAX;
-
- uninit_opts();
- init_opts();
}
-
static double parse_frame_aspect_ratio(const char *arg)
{
int x = 0, y = 0;
if (map[0] == '[') {
/* this mapping refers to lavfi output */
const char *c = map + 1;
- o->stream_maps = grow_array(o->stream_maps, sizeof(*o->stream_maps),
- &o->nb_stream_maps, o->nb_stream_maps + 1);
+ GROW_ARRAY(o->stream_maps, o->nb_stream_maps);
m = &o->stream_maps[o->nb_stream_maps - 1];
m->linklabel = av_get_token(&c, "]");
if (!m->linklabel) {
if (check_stream_specifier(input_files[file_idx]->ctx, input_files[file_idx]->ctx->streams[i],
*p == ':' ? p + 1 : p) <= 0)
continue;
- o->stream_maps = grow_array(o->stream_maps, sizeof(*o->stream_maps),
- &o->nb_stream_maps, o->nb_stream_maps + 1);
+ GROW_ARRAY(o->stream_maps, o->nb_stream_maps);
m = &o->stream_maps[o->nb_stream_maps - 1];
m->file_index = file_idx;
static int opt_attach(void *optctx, const char *opt, const char *arg)
{
OptionsContext *o = optctx;
- o->attachments = grow_array(o->attachments, sizeof(*o->attachments),
- &o->nb_attachments, o->nb_attachments + 1);
+ GROW_ARRAY(o->attachments, o->nb_attachments);
o->attachments[o->nb_attachments - 1] = arg;
return 0;
}
if (!ist)
exit(1);
- input_streams = grow_array(input_streams, sizeof(*input_streams), &nb_input_streams, nb_input_streams + 1);
+ GROW_ARRAY(input_streams, nb_input_streams);
input_streams[nb_input_streams - 1] = ist;
ist->st = st;
ist->file_index = nb_input_files;
ist->discard = 1;
st->discard = AVDISCARD_ALL;
- ist->opts = filter_codec_opts(codec_opts, ist->st->codec->codec_id, ic, st, NULL);
ist->ts_scale = 1.0;
MATCH_PER_STREAM_OPT(ts_scale, dbl, ist->ts_scale, ic, st);
ist->dec = choose_decoder(o, ic, st);
+ ist->opts = filter_codec_opts(o->g->codec_opts, ist->st->codec->codec_id, ic, st, ist->dec);
switch (dec->codec_type) {
case AVMEDIA_TYPE_VIDEO:
avio_close(out);
}
-static int opt_input_file(void *optctx, const char *opt, const char *filename)
+static int open_input_file(OptionsContext *o, const char *filename)
{
- OptionsContext *o = optctx;
AVFormatContext *ic;
AVInputFormat *file_iformat = NULL;
int err, i, ret;
}
if (o->nb_audio_sample_rate) {
snprintf(buf, sizeof(buf), "%d", o->audio_sample_rate[o->nb_audio_sample_rate - 1].u.i);
- av_dict_set(&format_opts, "sample_rate", buf, 0);
+ av_dict_set(&o->g->format_opts, "sample_rate", buf, 0);
}
if (o->nb_audio_channels) {
/* because we set audio_channels based on both the "ac" and
AV_OPT_SEARCH_FAKE_OBJ)) {
snprintf(buf, sizeof(buf), "%d",
o->audio_channels[o->nb_audio_channels - 1].u.i);
- av_dict_set(&format_opts, "channels", buf, 0);
+ av_dict_set(&o->g->format_opts, "channels", buf, 0);
}
}
if (o->nb_frame_rates) {
if (file_iformat && file_iformat->priv_class &&
av_opt_find(&file_iformat->priv_class, "framerate", NULL, 0,
AV_OPT_SEARCH_FAKE_OBJ)) {
- av_dict_set(&format_opts, "framerate",
+ av_dict_set(&o->g->format_opts, "framerate",
o->frame_rates[o->nb_frame_rates - 1].u.str, 0);
}
}
if (o->nb_frame_sizes) {
- av_dict_set(&format_opts, "video_size", o->frame_sizes[o->nb_frame_sizes - 1].u.str, 0);
+ av_dict_set(&o->g->format_opts, "video_size", o->frame_sizes[o->nb_frame_sizes - 1].u.str, 0);
}
if (o->nb_frame_pix_fmts)
- av_dict_set(&format_opts, "pixel_format", o->frame_pix_fmts[o->nb_frame_pix_fmts - 1].u.str, 0);
+ av_dict_set(&o->g->format_opts, "pixel_format", o->frame_pix_fmts[o->nb_frame_pix_fmts - 1].u.str, 0);
ic->flags |= AVFMT_FLAG_NONBLOCK;
ic->interrupt_callback = int_cb;
/* open the input file with generic libav function */
- err = avformat_open_input(&ic, filename, file_iformat, &format_opts);
+ err = avformat_open_input(&ic, filename, file_iformat, &o->g->format_opts);
if (err < 0) {
print_error(filename, err);
exit(1);
}
- assert_avoptions(format_opts);
+ assert_avoptions(o->g->format_opts);
/* apply forced codec ids */
for (i = 0; i < ic->nb_streams; i++)
choose_decoder(o, ic, ic->streams[i]);
/* Set AVCodecContext options for avformat_find_stream_info */
- opts = setup_find_stream_info_opts(ic, codec_opts);
+ opts = setup_find_stream_info_opts(ic, o->g->codec_opts);
orig_nb_streams = ic->nb_streams;
/* If not enough info to get the stream parameters, we decode the
/* dump the file content */
av_dump_format(ic, nb_input_files, filename, 0);
- input_files = grow_array(input_files, sizeof(*input_files), &nb_input_files, nb_input_files + 1);
+ GROW_ARRAY(input_files, nb_input_files);
if (!(input_files[nb_input_files - 1] = av_mallocz(sizeof(*input_files[0]))))
exit(1);
av_dict_free(&opts[i]);
av_freep(&opts);
- reset_options(o);
return 0;
}
if (oc->nb_streams - 1 < o->nb_streamid_map)
st->id = o->streamid_map[oc->nb_streams - 1];
- output_streams = grow_array(output_streams, sizeof(*output_streams), &nb_output_streams,
- nb_output_streams + 1);
+ GROW_ARRAY(output_streams, nb_output_streams);
if (!(ost = av_mallocz(sizeof(*ost))))
exit(1);
output_streams[nb_output_streams - 1] = ost;
AVIOContext *s = NULL;
char *buf = NULL, *arg = NULL, *preset = NULL;
- ost->opts = filter_codec_opts(codec_opts, ost->enc->id, oc, st, ost->enc);
+ ost->opts = filter_codec_opts(o->g->codec_opts, ost->enc->id, oc, st, ost->enc);
MATCH_PER_STREAM_OPT(presets, str, preset, oc, st);
if (preset && (!(ret = get_preset_file_2(preset, ost->enc->name, &s)))) {
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
- av_opt_get_int(sws_opts, "sws_flags", 0, &ost->sws_flags);
+ av_opt_get_int(o->g->sws_opts, "sws_flags", 0, &ost->sws_flags);
ost->pix_fmts[0] = ost->pix_fmts[1] = AV_PIX_FMT_NONE;
if (p) p++;
}
video_enc->rc_override_count = i;
- if (!video_enc->rc_initial_buffer_occupancy)
- video_enc->rc_initial_buffer_occupancy = video_enc->rc_buffer_size * 3 / 4;
video_enc->intra_dc_precision = intra_dc_precision - 8;
/* two pass mode */
return 0;
}
-void opt_output_file(void *optctx, const char *filename)
+static int open_output_file(OptionsContext *o, const char *filename)
{
- OptionsContext *o = optctx;
AVFormatContext *oc;
int i, j, err;
AVOutputFormat *file_oformat;
avio_close(pb);
}
- output_files = grow_array(output_files, sizeof(*output_files), &nb_output_files, nb_output_files + 1);
+ GROW_ARRAY(output_files, nb_output_files);
if (!(output_files[nb_output_files - 1] = av_mallocz(sizeof(*output_files[0]))))
exit(1);
output_files[nb_output_files - 1]->start_time = o->start_time;
output_files[nb_output_files - 1]->limit_filesize = o->limit_filesize;
output_files[nb_output_files - 1]->shortest = o->shortest;
- av_dict_copy(&output_files[nb_output_files - 1]->opts, format_opts, 0);
+ av_dict_copy(&output_files[nb_output_files - 1]->opts, o->g->format_opts, 0);
/* check filename in case of an image number is expected */
if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
}
}
- reset_options(o);
+ return 0;
}
static int opt_target(void *optctx, const char *opt, const char *arg)
static int opt_filter_complex(void *optctx, const char *opt, const char *arg)
{
- filtergraphs = grow_array(filtergraphs, sizeof(*filtergraphs),
- &nb_filtergraphs, nb_filtergraphs + 1);
+ GROW_ARRAY(filtergraphs, nb_filtergraphs);
if (!(filtergraphs[nb_filtergraphs - 1] = av_mallocz(sizeof(*filtergraphs[0]))))
return AVERROR(ENOMEM);
filtergraphs[nb_filtergraphs - 1]->index = nb_filtergraphs - 1;
printf("\n");
}
+enum OptGroup {
+ GROUP_OUTFILE,
+ GROUP_INFILE,
+};
+
+static const OptionGroupDef groups[] = {
+ [GROUP_OUTFILE] = { "output file", NULL },
+ [GROUP_INFILE] = { "input file", "i" },
+};
+
+static int open_files(OptionGroupList *l, const char *inout,
+ int (*open_file)(OptionsContext*, const char*))
+{
+ int i, ret;
+
+ for (i = 0; i < l->nb_groups; i++) {
+ OptionGroup *g = &l->groups[i];
+ OptionsContext o;
+
+ init_options(&o);
+ o.g = g;
+
+ ret = parse_optgroup(&o, g);
+ if (ret < 0) {
+ av_log(NULL, AV_LOG_ERROR, "Error parsing options for %s file "
+ "%s.\n", inout, g->arg);
+ return ret;
+ }
+
+ av_log(NULL, AV_LOG_DEBUG, "Opening an %s file: %s.\n", inout, g->arg);
+ ret = open_file(&o, g->arg);
+ uninit_options(&o);
+ if (ret < 0) {
+ av_log(NULL, AV_LOG_ERROR, "Error opening %s file %s.\n",
+ inout, g->arg);
+ return ret;
+ }
+ av_log(NULL, AV_LOG_DEBUG, "Successfully openened the file.\n");
+ }
+
+ return 0;
+}
+
+int avconv_parse_options(int argc, char **argv)
+{
+ OptionParseContext octx;
+ uint8_t error[128];
+ int ret;
+
+ memset(&octx, 0, sizeof(octx));
+
+ /* split the commandline into an internal representation */
+ ret = split_commandline(&octx, argc, argv, options, groups,
+ FF_ARRAY_ELEMS(groups));
+ if (ret < 0) {
+ av_log(NULL, AV_LOG_FATAL, "Error splitting the argument list: ");
+ goto fail;
+ }
+
+ /* apply global options */
+ ret = parse_optgroup(NULL, &octx.global_opts);
+ if (ret < 0) {
+ av_log(NULL, AV_LOG_FATAL, "Error parsing global options: ");
+ goto fail;
+ }
+
+ /* open input files */
+ ret = open_files(&octx.groups[GROUP_INFILE], "input", open_input_file);
+ if (ret < 0) {
+ av_log(NULL, AV_LOG_FATAL, "Error opening input files: ");
+ goto fail;
+ }
+
+ /* open output files */
+ ret = open_files(&octx.groups[GROUP_OUTFILE], "output", open_output_file);
+ if (ret < 0) {
+ av_log(NULL, AV_LOG_FATAL, "Error opening output files: ");
+ goto fail;
+ }
+
+fail:
+ uninit_parse_context(&octx);
+ if (ret < 0) {
+ av_strerror(ret, error, sizeof(error));
+ av_log(NULL, AV_LOG_FATAL, "%s\n", error);
+ }
+ return ret;
+}
#define OFFSET(x) offsetof(OptionsContext, x)
const OptionDef options[] = {
#include "cmdutils_common_opts.h"
{ "f", HAS_ARG | OPT_STRING | OPT_OFFSET, { .off = OFFSET(format) },
"force format", "fmt" },
- { "i", HAS_ARG | OPT_PERFILE, { .func_arg = opt_input_file },
- "input file name", "filename" },
{ "y", OPT_BOOL, { &file_overwrite },
"overwrite output files" },
{ "c", HAS_ARG | OPT_STRING | OPT_SPEC, { .off = OFFSET(codec_names) },
{ "dcodec", HAS_ARG | OPT_DATA | OPT_PERFILE | OPT_EXPERT, { .func_arg = opt_data_codec },
"force data codec ('copy' to copy stream)", "codec" },
- { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, { .func_arg = opt_default },
- "generic catch all option", "" },
{ NULL, },
};