From patchwork Thu Jul 13 10:55:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 42654 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6da1:b0:131:a7d0:bc6d with SMTP id gl33csp2823574pzb; Thu, 13 Jul 2023 04:00:39 -0700 (PDT) X-Google-Smtp-Source: APBJJlHC4P6bLLz+z4+djmn+0+anxgq+vzy+r43T56bLPC0UDTiLGIoyc28Jun9dyJ7RBqBqprh2 X-Received: by 2002:aa7:d751:0:b0:51f:e92a:62ba with SMTP id a17-20020aa7d751000000b0051fe92a62bamr1336184eds.9.1689246039444; Thu, 13 Jul 2023 04:00:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689246039; cv=none; d=google.com; s=arc-20160816; b=SayHWaZmNzb0nU3KOH4mPTRmeUjO/bcUEU1Af4VDRD+osewOVFY2iwO4uhk4YRqTTe 53nnzZrGBu00itdYgf6F5bZc3DMpNM2q3D2b3mRXCaVD61H9+Te8aBK/FyEtMIBqjEZX tGh8+FLlF8PyK/Krldk/3NLB/AKvQBxuMgt/Nj8VUSlEdrfIfjI54Ss7MDdglgpC4mfM F2TStoJYLExf2c5IE2k8RgkvdPpDXl0/CAhKr+yWLZXM9i9n7LIinXnUMDERTg0fX3Or xgTxOZwMxTuMV175uBbiYA0Ut5HtPSrVrhBBTDy5hTYCwNvfiZHXh6Op0KrjjQuy6B2d R+Ug== 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=waYTzAIWAUd5/HVQeYxSxB+mYH6FPMClyeHjOT25voY=; fh=8ZF2QtiKOjBtyn9wuO2cMeZaLsE4aq02Bq77gGK61E8=; b=oUYQ0IDi5ijvLltUN7WYI4EROxFUa6HLHnkpj2diMPJwNECL7We1vJOvSAFY64Tjtw HeZWQVx2ik8cfsjOdEliCMqytPlfRstz1VoKieqw1lXwNCMLsCCqZnqwWWo7gcDkGnTf ciiVlEQG02FPjP6s5sxzMeLuzbfj8o4bgXlNwZ8QK9wgw3KvRCuqnDa2vWYsGwGJQQ0m uqzydtkmZoyOrJpmoK8I7DlbUaSw9utUS3rI9lhDJtngVW8equheGnCy8Sev7a12IZhQ kYpZXCtcFjWzF4VHEEh4iG9iHG3IP1vQoA9+U2hw7PWY0B2hCFYQ4MNGETDCknsKaiWx A7mw== 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 a18-20020aa7cf12000000b0051dd1d7fbeasi6488796edy.683.2023.07.13.04.00.39; Thu, 13 Jul 2023 04:00:39 -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 584E268C704; Thu, 13 Jul 2023 13:58:10 +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 9C2CB68C69A for ; Thu, 13 Jul 2023 13:57:48 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 5E27F2406CC for ; Thu, 13 Jul 2023 12:57:47 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 2TWcCUWW8IOX for ; Thu, 13 Jul 2023 12:57:44 +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 808E32405EC for ; Thu, 13 Jul 2023 12:57:41 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 62ED33A142E for ; Thu, 13 Jul 2023 12:57:35 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 13 Jul 2023 12:55:30 +0200 Message-Id: <20230713105553.21052-10-anton@khirnov.net> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230713105553.21052-1-anton@khirnov.net> References: <20230713105553.21052-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 10/33] fftools/ffmpeg_mux_init: return error codes from map_*() 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: M5Xl3nOmVIf4 --- fftools/ffmpeg_mux_init.c | 92 +++++++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 37 deletions(-) diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c index 47b6f826b6..8e640610cd 100644 --- a/fftools/ffmpeg_mux_init.c +++ b/fftools/ffmpeg_mux_init.c @@ -1297,7 +1297,7 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, return ost; } -static void map_auto_video(Muxer *mux, const OptionsContext *o) +static int map_auto_video(Muxer *mux, const OptionsContext *o) { AVFormatContext *oc = mux->fc; InputStream *best_ist = NULL; @@ -1306,7 +1306,7 @@ static void map_auto_video(Muxer *mux, const OptionsContext *o) /* video: highest resolution */ if (av_guess_codec(oc->oformat, NULL, oc->url, NULL, AVMEDIA_TYPE_VIDEO) == AV_CODEC_ID_NONE) - return; + return 0; qcr = avformat_query_codec(oc->oformat, oc->oformat->video_codec, 0); for (int j = 0; j < nb_input_files; j++) { @@ -1346,9 +1346,11 @@ static void map_auto_video(Muxer *mux, const OptionsContext *o) } if (best_ist) ost_add(mux, o, AVMEDIA_TYPE_VIDEO, best_ist, NULL); + + return 0; } -static void map_auto_audio(Muxer *mux, const OptionsContext *o) +static int map_auto_audio(Muxer *mux, const OptionsContext *o) { AVFormatContext *oc = mux->fc; InputStream *best_ist = NULL; @@ -1356,7 +1358,7 @@ static void map_auto_audio(Muxer *mux, const OptionsContext *o) /* audio: most channels */ if (av_guess_codec(oc->oformat, NULL, oc->url, NULL, AVMEDIA_TYPE_AUDIO) == AV_CODEC_ID_NONE) - return; + return 0; for (int j = 0; j < nb_input_files; j++) { InputFile *ifile = input_files[j]; @@ -1388,9 +1390,11 @@ static void map_auto_audio(Muxer *mux, const OptionsContext *o) } if (best_ist) ost_add(mux, o, AVMEDIA_TYPE_AUDIO, best_ist, NULL); + + return 0; } -static void map_auto_subtitle(Muxer *mux, const OptionsContext *o) +static int map_auto_subtitle(Muxer *mux, const OptionsContext *o) { AVFormatContext *oc = mux->fc; char *subtitle_codec_name = NULL; @@ -1398,7 +1402,7 @@ static void map_auto_subtitle(Muxer *mux, const OptionsContext *o) /* subtitles: pick first */ MATCH_PER_TYPE_OPT(codec_names, str, subtitle_codec_name, oc, "s"); if (!avcodec_find_encoder(oc->oformat->subtitle_codec) && !subtitle_codec_name) - return; + return 0; for (InputStream *ist = ist_iter(NULL); ist; ist = ist_iter(ist)) if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) { @@ -1426,16 +1430,18 @@ static void map_auto_subtitle(Muxer *mux, const OptionsContext *o) break; } } + + return 0; } -static void map_auto_data(Muxer *mux, const OptionsContext *o) +static int map_auto_data(Muxer *mux, const OptionsContext *o) { AVFormatContext *oc = mux->fc; /* Data only if codec id match */ enum AVCodecID codec_id = av_guess_codec(oc->oformat, NULL, oc->url, NULL, AVMEDIA_TYPE_DATA); if (codec_id == AV_CODEC_ID_NONE) - return; + return 0; for (InputStream *ist = ist_iter(NULL); ist; ist = ist_iter(ist)) { if (ist->user_set_discard == AVDISCARD_ALL) @@ -1444,14 +1450,16 @@ static void map_auto_data(Muxer *mux, const OptionsContext *o) ist->st->codecpar->codec_id == codec_id ) ost_add(mux, o, AVMEDIA_TYPE_DATA, ist, NULL); } + + return 0; } -static void map_manual(Muxer *mux, const OptionsContext *o, const StreamMap *map) +static int map_manual(Muxer *mux, const OptionsContext *o, const StreamMap *map) { InputStream *ist; if (map->disabled) - return; + return 0; if (map->linklabel) { FilterGraph *fg; @@ -1472,7 +1480,7 @@ loop_end: if (!ofilter) { av_log(mux, AV_LOG_FATAL, "Output with label '%s' does not exist " "in any defined filter graph, or was already used elsewhere.\n", map->linklabel); - exit_program(1); + return AVERROR(EINVAL); } av_log(mux, AV_LOG_VERBOSE, "Creating output stream from an explicitly " @@ -1484,16 +1492,16 @@ loop_end: if (ist->user_set_discard == AVDISCARD_ALL) { av_log(mux, AV_LOG_FATAL, "Stream #%d:%d is disabled and cannot be mapped.\n", map->file_index, map->stream_index); - exit_program(1); + return AVERROR(EINVAL); } if(o->subtitle_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) - return; + return 0; if(o-> audio_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) - return; + return 0; if(o-> video_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) - return; + return 0; if(o-> data_disable && ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA) - return; + return 0; if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_UNKNOWN && !copy_unknown_streams) { @@ -1505,13 +1513,15 @@ loop_end: "If you want unsupported types ignored instead " "of failing, please use the -ignore_unknown option\n" "If you want them copied, please use -copy_unknown\n"); - exit_program(1); + return AVERROR(EINVAL); } - return; + return 0; } ost_add(mux, o, ist->st->codecpar->codec_type, ist, NULL); } + + return 0; } static int of_add_attachments(Muxer *mux, const OptionsContext *o) @@ -1583,11 +1593,21 @@ read_fail: static int create_streams(Muxer *mux, const OptionsContext *o) { + static const int (*map_func[])(Muxer *mux, const OptionsContext *o) = { + [AVMEDIA_TYPE_VIDEO] = map_auto_video, + [AVMEDIA_TYPE_AUDIO] = map_auto_audio, + [AVMEDIA_TYPE_SUBTITLE] = map_auto_subtitle, + [AVMEDIA_TYPE_DATA] = map_auto_data, + }; + AVFormatContext *oc = mux->fc; - int auto_disable_v = o->video_disable; - int auto_disable_a = o->audio_disable; - int auto_disable_s = o->subtitle_disable; - int auto_disable_d = o->data_disable; + + int auto_disable = + o->video_disable * (1 << AVMEDIA_TYPE_VIDEO) | + o->audio_disable * (1 << AVMEDIA_TYPE_AUDIO) | + o->subtitle_disable * (1 << AVMEDIA_TYPE_SUBTITLE) | + o->data_disable * (1 << AVMEDIA_TYPE_DATA); + int ret; /* create streams for all unlabeled output pads */ @@ -1599,11 +1619,7 @@ static int create_streams(Muxer *mux, const OptionsContext *o) if (ofilter->linklabel || ofilter->ost) continue; - switch (ofilter->type) { - case AVMEDIA_TYPE_VIDEO: auto_disable_v = 1; break; - case AVMEDIA_TYPE_AUDIO: auto_disable_a = 1; break; - case AVMEDIA_TYPE_SUBTITLE: auto_disable_s = 1; break; - } + auto_disable |= 1 << ofilter->type; av_log(mux, AV_LOG_VERBOSE, "Creating output stream from unlabeled " "output of complex filtergraph %d.", fg->index); @@ -1620,19 +1636,21 @@ static int create_streams(Muxer *mux, const OptionsContext *o) av_log(mux, AV_LOG_VERBOSE, "No explicit maps, mapping streams automatically...\n"); /* pick the "best" stream of each type */ - if (!auto_disable_v) - map_auto_video(mux, o); - if (!auto_disable_a) - map_auto_audio(mux, o); - if (!auto_disable_s) - map_auto_subtitle(mux, o); - if (!auto_disable_d) - map_auto_data(mux, o); + for (int i = 0; i < FF_ARRAY_ELEMS(map_func); i++) { + if (!map_func[i] || auto_disable & (1 << i)) + continue; + ret = map_func[i](mux, o); + if (ret < 0) + return ret; + } } else { av_log(mux, AV_LOG_VERBOSE, "Adding streams from explicit maps...\n"); - for (int i = 0; i < o->nb_stream_maps; i++) - map_manual(mux, o, &o->stream_maps[i]); + for (int i = 0; i < o->nb_stream_maps; i++) { + ret = map_manual(mux, o, &o->stream_maps[i]); + if (ret < 0) + return ret; + } } ret = of_add_attachments(mux, o);