[FFmpeg-devel] [PATCH 29/47] fftools/ffmpeg: return errors from find_codec_or_die() instead of aborting
Anton Khirnov
anton at khirnov.net
Sat Jul 15 13:45:53 EEST 2023
Rename the function to just find_codec().
---
fftools/ffmpeg.h | 4 +--
fftools/ffmpeg_demux.c | 54 +++++++++++++++++++++++++++------------
fftools/ffmpeg_mux_init.c | 4 ++-
fftools/ffmpeg_opt.c | 13 ++++++----
4 files changed, 50 insertions(+), 25 deletions(-)
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 8a94cd7861..60dff87436 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -710,8 +710,8 @@ void assert_avoptions(AVDictionary *m);
int assert_file_overwrite(const char *filename);
char *file_read(const char *filename);
AVDictionary *strip_specifiers(const AVDictionary *dict);
-const AVCodec *find_codec_or_die(void *logctx, const char *name,
- enum AVMediaType type, int encoder);
+int find_codec(void *logctx, const char *name,
+ enum AVMediaType type, int encoder, const AVCodec **codec);
int parse_and_set_vsync(const char *arg, int *vsync_var, int file_idx, int st_idx, int is_global);
int check_filter_outputs(void);
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index 5a41db9b21..a41b59ceb8 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -911,19 +911,22 @@ int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple)
return 0;
}
-static const AVCodec *choose_decoder(const OptionsContext *o, AVFormatContext *s, AVStream *st,
- enum HWAccelID hwaccel_id, enum AVHWDeviceType hwaccel_device_type)
+static int choose_decoder(const OptionsContext *o, AVFormatContext *s, AVStream *st,
+ enum HWAccelID hwaccel_id, enum AVHWDeviceType hwaccel_device_type,
+ const AVCodec **pcodec)
{
char *codec_name = NULL;
MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st);
if (codec_name) {
- const AVCodec *codec = find_codec_or_die(NULL, codec_name, st->codecpar->codec_type, 0);
- st->codecpar->codec_id = codec->id;
- if (recast_media && st->codecpar->codec_type != codec->type)
- st->codecpar->codec_type = codec->type;
- return codec;
+ int ret = find_codec(NULL, codec_name, st->codecpar->codec_type, 0, pcodec);
+ if (ret < 0)
+ return ret;
+ st->codecpar->codec_id = (*pcodec)->id;
+ if (recast_media && st->codecpar->codec_type != (*pcodec)->type)
+ st->codecpar->codec_type = (*pcodec)->type;
+ return 0;
} else {
if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
hwaccel_id == HWACCEL_GENERIC &&
@@ -942,13 +945,15 @@ static const AVCodec *choose_decoder(const OptionsContext *o, AVFormatContext *s
if (config->device_type == hwaccel_device_type) {
av_log(NULL, AV_LOG_VERBOSE, "Selecting decoder '%s' because of requested hwaccel method %s\n",
c->name, av_hwdevice_get_type_name(hwaccel_device_type));
- return c;
+ *pcodec = c;
+ return 0;
}
}
}
}
- return avcodec_find_decoder(st->codecpar->codec_id);
+ *pcodec = avcodec_find_decoder(st->codecpar->codec_id);
+ return 0;
}
}
@@ -1166,7 +1171,11 @@ static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st)
}
}
- ist->dec = choose_decoder(o, ic, st, ist->hwaccel_id, ist->hwaccel_device_type);
+ ret = choose_decoder(o, ic, st, ist->hwaccel_id, ist->hwaccel_device_type,
+ &ist->dec);
+ if (ret < 0)
+ return ret;
+
ist->decoder_opts = filter_codec_opts(o->g->codec_opts, ist->st->codecpar->codec_id, ic, st, ist->dec);
ist->reinit_filters = -1;
@@ -1357,7 +1366,7 @@ int ifile_open(const OptionsContext *o, const char *filename)
InputFile *f;
AVFormatContext *ic;
const AVInputFormat *file_iformat = NULL;
- int err, i, ret;
+ int err, i, ret = 0;
int64_t timestamp;
AVDictionary *unused_opts = NULL;
const AVDictionaryEntry *e = NULL;
@@ -1455,13 +1464,19 @@ int ifile_open(const OptionsContext *o, const char *filename)
MATCH_PER_TYPE_OPT(codec_names, str, data_codec_name, ic, "d");
if (video_codec_name)
- ic->video_codec = find_codec_or_die(NULL, video_codec_name , AVMEDIA_TYPE_VIDEO , 0);
+ ret = err_merge(ret, find_codec(NULL, video_codec_name , AVMEDIA_TYPE_VIDEO , 0,
+ &ic->video_codec));
if (audio_codec_name)
- ic->audio_codec = find_codec_or_die(NULL, audio_codec_name , AVMEDIA_TYPE_AUDIO , 0);
+ ret = err_merge(ret, find_codec(NULL, audio_codec_name , AVMEDIA_TYPE_AUDIO , 0,
+ &ic->audio_codec));
if (subtitle_codec_name)
- ic->subtitle_codec = find_codec_or_die(NULL, subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0);
+ ret = err_merge(ret, find_codec(NULL, subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0,
+ &ic->subtitle_codec));
if (data_codec_name)
- ic->data_codec = find_codec_or_die(NULL, data_codec_name , AVMEDIA_TYPE_DATA , 0);
+ ret = err_merge(ret, find_codec(NULL, data_codec_name , AVMEDIA_TYPE_DATA, 0,
+ &ic->data_codec));
+ if (ret < 0)
+ return ret;
ic->video_codec_id = video_codec_name ? ic->video_codec->id : AV_CODEC_ID_NONE;
ic->audio_codec_id = audio_codec_name ? ic->audio_codec->id : AV_CODEC_ID_NONE;
@@ -1496,8 +1511,13 @@ int ifile_open(const OptionsContext *o, const char *filename)
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], HWACCEL_NONE, AV_HWDEVICE_TYPE_NONE);
+ for (i = 0; i < ic->nb_streams; i++) {
+ const AVCodec *dummy;
+ ret = choose_decoder(o, ic, ic->streams[i], HWACCEL_NONE, AV_HWDEVICE_TYPE_NONE,
+ &dummy);
+ if (ret < 0)
+ return ret;
+ }
if (o->find_stream_info) {
AVDictionary **opts;
diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c
index cb943ac408..aebec0c573 100644
--- a/fftools/ffmpeg_mux_init.c
+++ b/fftools/ffmpeg_mux_init.c
@@ -140,7 +140,9 @@ static int choose_encoder(const OptionsContext *o, AVFormatContext *s,
return AVERROR_ENCODER_NOT_FOUND;
}
} else if (strcmp(codec_name, "copy")) {
- *enc = find_codec_or_die(ost, codec_name, ost->type, 1);
+ int ret = find_codec(ost, codec_name, ost->type, 1, enc);
+ if (ret < 0)
+ return ret;
ost->par_in->codec_id = (*enc)->id;
}
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index eef6d5e749..5a5a26d0a5 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -637,8 +637,8 @@ static int opt_recording_timestamp(void *optctx, const char *opt, const char *ar
return 0;
}
-const AVCodec *find_codec_or_die(void *logctx, const char *name,
- enum AVMediaType type, int encoder)
+int find_codec(void *logctx, const char *name,
+ enum AVMediaType type, int encoder, const AVCodec **pcodec)
{
const AVCodecDescriptor *desc;
const char *codec_string = encoder ? "encoder" : "decoder";
@@ -658,13 +658,16 @@ const AVCodec *find_codec_or_die(void *logctx, const char *name,
if (!codec) {
av_log(logctx, AV_LOG_FATAL, "Unknown %s '%s'\n", codec_string, name);
- exit_program(1);
+ return encoder ? AVERROR_ENCODER_NOT_FOUND :
+ AVERROR_DECODER_NOT_FOUND;
}
if (codec->type != type && !recast_media) {
av_log(logctx, AV_LOG_FATAL, "Invalid %s type '%s'\n", codec_string, name);
- exit_program(1);
+ return AVERROR(EINVAL);
}
- return codec;
+
+ *pcodec = codec;
+ return 0;;
}
int assert_file_overwrite(const char *filename)
--
2.40.1
More information about the ffmpeg-devel
mailing list