fftools/ffmpeg_mux_init: return error codes from metadata processing instead of aborting

pull/389/head
Anton Khirnov 2 years ago
parent dd44871eb9
commit 45473258ff
  1. 63
      fftools/ffmpeg_mux_init.c

@ -1851,8 +1851,8 @@ fail:
* @param index for type c/p, chapter/program index is written here * @param index for type c/p, chapter/program index is written here
* @param stream_spec for type s, the stream specifier is written here * @param stream_spec for type s, the stream specifier is written here
*/ */
static void parse_meta_type(void *logctx, const char *arg, static int parse_meta_type(void *logctx, const char *arg,
char *type, int *index, const char **stream_spec) char *type, int *index, const char **stream_spec)
{ {
if (*arg) { if (*arg) {
*type = *arg; *type = *arg;
@ -1862,7 +1862,7 @@ static void parse_meta_type(void *logctx, const char *arg,
case 's': case 's':
if (*(++arg) && *arg != ':') { if (*(++arg) && *arg != ':') {
av_log(logctx, AV_LOG_FATAL, "Invalid metadata specifier %s.\n", arg); av_log(logctx, AV_LOG_FATAL, "Invalid metadata specifier %s.\n", arg);
exit_program(1); return AVERROR(EINVAL);
} }
*stream_spec = *arg == ':' ? arg + 1 : ""; *stream_spec = *arg == ':' ? arg + 1 : "";
break; break;
@ -1873,14 +1873,16 @@ static void parse_meta_type(void *logctx, const char *arg,
break; break;
default: default:
av_log(logctx, AV_LOG_FATAL, "Invalid metadata type %c.\n", *arg); av_log(logctx, AV_LOG_FATAL, "Invalid metadata type %c.\n", *arg);
exit_program(1); return AVERROR(EINVAL);
} }
} else } else
*type = 'g'; *type = 'g';
return 0;
} }
static void of_add_metadata(OutputFile *of, AVFormatContext *oc, static int of_add_metadata(OutputFile *of, AVFormatContext *oc,
const OptionsContext *o) const OptionsContext *o)
{ {
for (int i = 0; i < o->nb_metadata; i++) { for (int i = 0; i < o->nb_metadata; i++) {
AVDictionary **m; AVDictionary **m;
@ -1892,11 +1894,14 @@ static void of_add_metadata(OutputFile *of, AVFormatContext *oc,
if (!val) { if (!val) {
av_log(of, AV_LOG_FATAL, "No '=' character in metadata string %s.\n", av_log(of, AV_LOG_FATAL, "No '=' character in metadata string %s.\n",
o->metadata[i].u.str); o->metadata[i].u.str);
exit_program(1); return AVERROR(EINVAL);
} }
*val++ = 0; *val++ = 0;
parse_meta_type(of, o->metadata[i].specifier, &type, &index, &stream_spec); ret = parse_meta_type(of, o->metadata[i].specifier, &type, &index, &stream_spec);
if (ret < 0)
return ret;
if (type == 's') { if (type == 's') {
for (int j = 0; j < oc->nb_streams; j++) { for (int j = 0; j < oc->nb_streams; j++) {
OutputStream *ost = of->streams[j]; OutputStream *ost = of->streams[j];
@ -1922,7 +1927,7 @@ static void of_add_metadata(OutputFile *of, AVFormatContext *oc,
} }
#endif #endif
} else if (ret < 0) } else if (ret < 0)
exit_program(1); return ret;
} }
} else { } else {
switch (type) { switch (type) {
@ -1932,24 +1937,26 @@ static void of_add_metadata(OutputFile *of, AVFormatContext *oc,
case 'c': case 'c':
if (index < 0 || index >= oc->nb_chapters) { if (index < 0 || index >= oc->nb_chapters) {
av_log(of, AV_LOG_FATAL, "Invalid chapter index %d in metadata specifier.\n", index); av_log(of, AV_LOG_FATAL, "Invalid chapter index %d in metadata specifier.\n", index);
exit_program(1); return AVERROR(EINVAL);
} }
m = &oc->chapters[index]->metadata; m = &oc->chapters[index]->metadata;
break; break;
case 'p': case 'p':
if (index < 0 || index >= oc->nb_programs) { if (index < 0 || index >= oc->nb_programs) {
av_log(of, AV_LOG_FATAL, "Invalid program index %d in metadata specifier.\n", index); av_log(of, AV_LOG_FATAL, "Invalid program index %d in metadata specifier.\n", index);
exit_program(1); return AVERROR(EINVAL);
} }
m = &oc->programs[index]->metadata; m = &oc->programs[index]->metadata;
break; break;
default: default:
av_log(of, AV_LOG_FATAL, "Invalid metadata specifier %s.\n", o->metadata[i].specifier); av_log(of, AV_LOG_FATAL, "Invalid metadata specifier %s.\n", o->metadata[i].specifier);
exit_program(1); return AVERROR(EINVAL);
} }
av_dict_set(m, o->metadata[i].u.str, *val ? val : NULL, 0); av_dict_set(m, o->metadata[i].u.str, *val ? val : NULL, 0);
} }
} }
return 0;
} }
static int copy_chapters(InputFile *ifile, OutputFile *ofile, AVFormatContext *os, static int copy_chapters(InputFile *ifile, OutputFile *ofile, AVFormatContext *os,
@ -2008,8 +2015,11 @@ static int copy_metadata(Muxer *mux, AVFormatContext *ic,
const char *istream_spec = NULL, *ostream_spec = NULL; const char *istream_spec = NULL, *ostream_spec = NULL;
int idx_in = 0, idx_out = 0; int idx_in = 0, idx_out = 0;
parse_meta_type(mux, inspec, &type_in, &idx_in, &istream_spec); ret = parse_meta_type(mux, inspec, &type_in, &idx_in, &istream_spec);
parse_meta_type(mux, outspec, &type_out, &idx_out, &ostream_spec); if (ret >= 0)
ret = parse_meta_type(mux, outspec, &type_out, &idx_out, &ostream_spec);
if (ret < 0)
return ret;
if (type_in == 'g' || type_out == 'g' || !*outspec) if (type_in == 'g' || type_out == 'g' || !*outspec)
*metadata_global_manual = 1; *metadata_global_manual = 1;
@ -2026,7 +2036,7 @@ static int copy_metadata(Muxer *mux, AVFormatContext *ic,
if ((index) < 0 || (index) >= (nb_elems)) {\ if ((index) < 0 || (index) >= (nb_elems)) {\
av_log(mux, AV_LOG_FATAL, "Invalid %s index %d while processing metadata maps.\n",\ av_log(mux, AV_LOG_FATAL, "Invalid %s index %d while processing metadata maps.\n",\
(desc), (index));\ (desc), (index));\
exit_program(1);\ return AVERROR(EINVAL);\
} }
#define SET_DICT(type, meta, context, index)\ #define SET_DICT(type, meta, context, index)\
@ -2057,11 +2067,11 @@ static int copy_metadata(Muxer *mux, AVFormatContext *ic,
meta_in = &ic->streams[i]->metadata; meta_in = &ic->streams[i]->metadata;
break; break;
} else if (ret < 0) } else if (ret < 0)
exit_program(1); return ret;
} }
if (!meta_in) { if (!meta_in) {
av_log(mux, AV_LOG_FATAL, "Stream specifier %s does not match any streams.\n", istream_spec); av_log(mux, AV_LOG_FATAL, "Stream specifier %s does not match any streams.\n", istream_spec);
exit_program(1); return AVERROR(EINVAL);
} }
} }
@ -2071,7 +2081,7 @@ static int copy_metadata(Muxer *mux, AVFormatContext *ic,
meta_out = &oc->streams[i]->metadata; meta_out = &oc->streams[i]->metadata;
av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE); av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
} else if (ret < 0) } else if (ret < 0)
exit_program(1); return ret;
} }
} else } else
av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE); av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
@ -2087,6 +2097,7 @@ static int copy_meta(Muxer *mux, const OptionsContext *o)
int metadata_global_manual = 0; int metadata_global_manual = 0;
int metadata_streams_manual = 0; int metadata_streams_manual = 0;
int metadata_chapters_manual = 0; int metadata_chapters_manual = 0;
int ret;
/* copy metadata */ /* copy metadata */
for (int i = 0; i < o->nb_metadata_map; i++) { for (int i = 0; i < o->nb_metadata_map; i++) {
@ -2098,11 +2109,13 @@ static int copy_meta(Muxer *mux, const OptionsContext *o)
"processing metadata maps\n", in_file_index); "processing metadata maps\n", in_file_index);
return AVERROR(EINVAL); return AVERROR(EINVAL);
} }
copy_metadata(mux, ret = copy_metadata(mux,
in_file_index >= 0 ? input_files[in_file_index]->ctx : NULL, in_file_index >= 0 ? input_files[in_file_index]->ctx : NULL,
o->metadata_map[i].specifier, *p ? p + 1 : p, o->metadata_map[i].specifier, *p ? p + 1 : p,
&metadata_global_manual, &metadata_streams_manual, &metadata_global_manual, &metadata_streams_manual,
&metadata_chapters_manual); &metadata_chapters_manual);
if (ret < 0)
return ret;
} }
/* copy chapters */ /* copy chapters */
@ -2541,7 +2554,9 @@ int of_open(const OptionsContext *o, const char *filename)
if (err < 0) if (err < 0)
return err; return err;
of_add_metadata(of, oc, o); err = of_add_metadata(of, oc, o);
if (err < 0)
return err;
err = set_dispositions(mux, o); err = set_dispositions(mux, o);
if (err < 0) { if (err < 0) {

Loading…
Cancel
Save