From patchwork Sat Jul 15 10:45:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 42706 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6da1:b0:131:a7d0:bc6d with SMTP id gl33csp4239734pzb; Sat, 15 Jul 2023 03:48:51 -0700 (PDT) X-Google-Smtp-Source: APBJJlGijRVoNHEPDsUAiP7YXeIFefrloSoCQ5udYZPrxq774xrFpetL3u0AmJz3ITyREQRlKXVH X-Received: by 2002:a05:6402:b1a:b0:51d:8953:1c89 with SMTP id bm26-20020a0564020b1a00b0051d89531c89mr6784141edb.8.1689418130855; Sat, 15 Jul 2023 03:48:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689418130; cv=none; d=google.com; s=arc-20160816; b=ywxx1qqQypGNu9q2Lvq9gycltA3j8lF6UvZFCHoy1KI3LxVrV4fB1Adx2XQO0Q4u9k gG2OjwOoclxCcJ8ofEDyiJ5/WW2n2ACP2IKhhuJWJXa+EPKpizXOaGAPub9JVWX3L94h QZZ4fssdZJTRl5T9tdJpofzbv6gw44B6DT9bPliW7cTJEQSk5+tTHQltD8e2lEALPTMm CpQs8LiyVpC6YsG2FGiifa64L9gzLsFZuCe3fpYzmijGYXtVpFuy1i22DhnTPCxVMqzZ wjuhDVPGn0y/YPADmfjYt/CrlyHsWU4So8ymuPmtdTGwVos3HpTwYwvGtaqNFLUSd9CY thrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=/wpYPhdTFXaxkPy/v1teCLTuBV9RGVekOtbw3h/OZlM=; fh=8ZF2QtiKOjBtyn9wuO2cMeZaLsE4aq02Bq77gGK61E8=; b=z7TpiRUBLArtXmgpBVRy7iKE9hlw9pfbA59U3/fXQysviBpn9vqw81AgXrSxLoSrxi ckPyH+wpS+gNarGRxo/xvUY9QjVFL+rdhEq/oJFhY1mkoMJ65A+kiANJWEY64gg+AA0r dNlZbCQ6EdSzGCQ/77LfyjdsArAs6U8nmyXFMmTT43ga7Z9mLNFHzYWg00cjqHXn6mJb laEt+7TMQMfl3x3q/s/0ANfun/MhviJy+EnFEOhmV29ofF/xPPnZDX4RMDkQY8Z0SEbT BT9EiPJudRxo8tYjhs4g4XmShPJ3F0dDarE9IYE87VxrA4Jc7K3sIjOqEM70ZXBjH9DJ SVPw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id j10-20020aa7c40a000000b0051e5c4351absi10131088edq.82.2023.07.15.03.48.50; Sat, 15 Jul 2023 03:48:50 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 666CA68C701; Sat, 15 Jul 2023 13:46:50 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7494868C645 for ; Sat, 15 Jul 2023 13:46:39 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 3A6CC240591 for ; Sat, 15 Jul 2023 12:46:39 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id poYr4Iv-ID8a for ; Sat, 15 Jul 2023 12:46:37 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id EB99C240D1A for ; Sat, 15 Jul 2023 12:46:26 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 4F9143A216F for ; Sat, 15 Jul 2023 12:46:21 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Sat, 15 Jul 2023 12:45:53 +0200 Message-Id: <20230715104611.17902-29-anton@khirnov.net> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230715104611.17902-1-anton@khirnov.net> References: <20230715104611.17902-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 29/47] fftools/ffmpeg: return errors from find_codec_or_die() instead of aborting X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 3n5B+jOsT/MY 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)