From patchwork Sun Apr 9 14:08:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 41069 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:4645:b0:e3:3194:9d20 with SMTP id eb5csp1592560pzb; Sun, 9 Apr 2023 07:13:06 -0700 (PDT) X-Google-Smtp-Source: AKy350aFAFNDjAimfubPQskATMRTWLB9H1hx0XLwuRD+HIbocxMgpRRZcvZU56/H9xAS1izQ4Peg X-Received: by 2002:a05:6402:1603:b0:4fa:ba31:6c66 with SMTP id f3-20020a056402160300b004faba316c66mr3974319edv.42.1681049585928; Sun, 09 Apr 2023 07:13:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681049585; cv=none; d=google.com; s=arc-20160816; b=gwmZvgg4LwkMb+wfGxGCguLDW/G0XmSAeezj68QGzzKtn9QCzIi/HogPiIF56qjQEg hf5Yez8hya0gx1RWfLMVxP6p4zjPpycduRblTt6yGbEYu54Cwt680oVLFqT2jNnrooOY qJEUwIT6M8XekROdk+VLgJUdUVbhK+8pDVsMYRzaSjmxixUHE337KEs8aanVG8B728Kz dVSBcGsdwgptYgiftQgloFixc70N0lSOk01ZbsZYG677fU9MSyfaX7/zWMOoR+RVHibD TCUqLmygBjhANZn73mEj138BuMJsQFpU4ukqBTvJj+LfX5EUXwrpZmIdKQULffJcJkb3 snUw== 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=e34+W+EXEoltV6vP6PvdQcD6p2liSnOLC67RtL8WBzE=; b=ZRCCtmRN3LMXqndPVl/dodorbq9BlB4X5R0Ec0tIqZJOCrWbKTpLEmQeI90XRKEE/b D34ROMvgPBv+16urcIbBSCEBslER1+pILKWvDITUKaS0vaHQEZ7tAK+yvuzYInH4KL6e TAp9woo8/oWbPJpouqMcdmIv0ZXZeSvRSWga9g7fxtIs5UgsXVRqbb2+q8poeWKux2Br gHXUQGOkyGN/C5DNnWe7sbXpIxypTywwsuOjhdcsZvVWvG+o0Ghu38GIbPe+UdzPCOEt CyflVpQQhQRXo5oFj4iwnz0es8zMBgWfqygre6pjoCKjIu+ObprTDIwwc6dpwDtyE9dD NBUg== 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 w18-20020a056402129200b004cfc2f60ca3si7298558edv.50.2023.04.09.07.13.05; Sun, 09 Apr 2023 07:13:05 -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 256B668BE98; Sun, 9 Apr 2023 17:09:43 +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 0FB0468BBF7 for ; Sun, 9 Apr 2023 17:09:17 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id C902B2406CD for ; Sun, 9 Apr 2023 16:09:14 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id bVxTs3EEvDaN for ; Sun, 9 Apr 2023 16:09:13 +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 3033A2405F9 for ; Sun, 9 Apr 2023 16:09:10 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 206F83A05CB for ; Sun, 9 Apr 2023 16:09:10 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Sun, 9 Apr 2023 16:08:33 +0200 Message-Id: <20230409140853.28858-9-anton@khirnov.net> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230409140853.28858-1-anton@khirnov.net> References: <20230409140853.28858-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 09/29] fftools/ffmpeg: eliminate the main_return_code global 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: iPLtvwoBAkeZ Properly pass muxing return codes through the call stack instead. Slightly changes behavior in case of errors: * the output IO stream is closed even if writing the trailer returns an error, which should be more correct * all files get properly closed with -xerror, even if one of them fails --- fftools/ffmpeg.c | 18 ++++++++---------- fftools/ffmpeg.h | 12 +++++++++++- fftools/ffmpeg_mux.c | 12 +++++------- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 816b6c4b08..7431482acc 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -318,7 +318,6 @@ static volatile int received_sigterm = 0; static volatile int received_nb_signals = 0; static atomic_int transcode_init_done = ATOMIC_VAR_INIT(0); static volatile int ffmpeg_exited = 0; -int main_return_code = 0; static int64_t copy_ts_first_pts = AV_NOPTS_VALUE; static void @@ -2951,9 +2950,8 @@ static int transcode(void) /* write the trailer if needed */ for (i = 0; i < nb_output_files; i++) { - ret = of_write_trailer(output_files[i]); - if (ret < 0 && exit_on_error) - exit_program(1); + int err = of_write_trailer(output_files[i]); + ret = err_merge(ret, err); } /* dump report by using the first video and audio streams */ @@ -2975,7 +2973,7 @@ static int transcode(void) exit_program(1); } - return 0; + return ret; } static BenchmarkTimeStamps get_benchmark_time_stamps(void) @@ -3061,9 +3059,8 @@ int main(int argc, char **argv) } current_time = ti = get_benchmark_time_stamps(); - if (transcode() < 0) - exit_program(1); - if (do_benchmark) { + ret = transcode(); + if (ret >= 0 && do_benchmark) { int64_t utime, stime, rtime; current_time = get_benchmark_time_stamps(); utime = current_time.user_usec - ti.user_usec; @@ -3078,6 +3075,7 @@ int main(int argc, char **argv) if ((decode_error_stat[0] + decode_error_stat[1]) * max_error_rate < decode_error_stat[1]) exit_program(69); - exit_program(received_nb_signals ? 255 : main_return_code); - return main_return_code; + ret = received_nb_signals ? 255 : ret; + exit_program(ret); + return ret; } diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 84ce017320..ddfeeabf75 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -756,7 +756,6 @@ extern const OptionDef options[]; extern HWDevice *filter_hw_device; extern unsigned nb_output_dumped; -extern int main_return_code; extern int ignore_unknown_streams; extern int copy_unknown_streams; @@ -887,6 +886,17 @@ void close_output_stream(OutputStream *ost); int trigger_fix_sub_duration_heartbeat(OutputStream *ost, const AVPacket *pkt); void update_benchmark(const char *fmt, ...); +/** + * Merge two return codes - return one of the error codes if at least one of + * them was negative, 0 otherwise. + * Currently just picks the first one, eventually we might want to do something + * more sophisticated, like sorting them by priority. + */ +static inline int err_merge(int err0, int err1) +{ + return (err0 < 0) ? err0 : FFMIN(err1, 0); +} + #define SPECIFIER_OPT_FMT_str "%s" #define SPECIFIER_OPT_FMT_i "%i" #define SPECIFIER_OPT_FMT_i64 "%"PRId64 diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index 5663b8f11d..c2b7993e66 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -602,7 +602,7 @@ int of_write_trailer(OutputFile *of) { Muxer *mux = mux_from_of(of); AVFormatContext *fc = mux->fc; - int ret; + int ret, mux_result = 0; if (!mux->tq) { av_log(mux, AV_LOG_ERROR, @@ -611,14 +611,12 @@ int of_write_trailer(OutputFile *of) return AVERROR(EINVAL); } - ret = thread_stop(mux); - if (ret < 0) - main_return_code = ret; + mux_result = thread_stop(mux); ret = av_write_trailer(fc); if (ret < 0) { av_log(mux, AV_LOG_ERROR, "Error writing trailer: %s\n", av_err2str(ret)); - return ret; + mux_result = err_merge(mux_result, ret); } mux->last_filesize = filesize(fc->pb); @@ -627,11 +625,11 @@ int of_write_trailer(OutputFile *of) ret = avio_closep(&fc->pb); if (ret < 0) { av_log(mux, AV_LOG_ERROR, "Error closing file: %s\n", av_err2str(ret)); - return ret; + mux_result = err_merge(mux_result, ret); } } - return 0; + return mux_result; } static void ost_free(OutputStream **post)