From patchwork Thu Jul 13 10:55:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 42647 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6da1:b0:131:a7d0:bc6d with SMTP id gl33csp2822779pzb; Thu, 13 Jul 2023 03:59:18 -0700 (PDT) X-Google-Smtp-Source: APBJJlGPNVbEtdFC0odRzOHeTNBLRXHXsY2udckGE65S/O7DSs5IR6lxLXkN6FC7nB68mGSxrLiD X-Received: by 2002:a2e:7212:0:b0:2b6:eeb3:da94 with SMTP id n18-20020a2e7212000000b002b6eeb3da94mr1101430ljc.22.1689245958587; Thu, 13 Jul 2023 03:59:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689245958; cv=none; d=google.com; s=arc-20160816; b=R1V0zDy4raVD+XkCu1b7mc9K440g1ZfPLRTukLn0oYElFqnVGC8I1Gw4uNFEI6zZJB EhTJtdImUfVZcozNSqGTz/Me36v5SpIFUOhi8b4A8N08nAojd+csySbV5q5bp9pRd2MV /C0JXh9ZUFpFjEzO0KwLA4iVQw11a5PwBRbjZvb6wwQh0KsvRu/XA3OwE9lBV7NNvkg5 aMHHkI6hJlErruwB/0gazx4k3MwX0eeEYDCTGHeozAEUj4qCXQponyc1bZJOukJcs+KZ WzasJpfV3iRZIRjlZ53DEpekkMsUrqs6c+FEjKRdNWl4hMUttDfp2P8wqbIJ7ENsDQxK o31w== 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=3X4A9yKqRZssaOkoZf3haEnYSaZguE4Dvcubh41eS4A=; fh=8ZF2QtiKOjBtyn9wuO2cMeZaLsE4aq02Bq77gGK61E8=; b=c4BZrcWp/Fe2AWYxTct2rHpDNjq87Y2xdEQScWJ3HufjrvYpbU7MZz+w3qESmjpBlw odJLSB7eAKEtZmoU340qjzCG99nTN3/VzSx6VZHYSJrZI/HeY78oio/308ItW2WjI0BL H5QZqE8YW55xt14YPwzf+c+sP6mXvrSAUtyd9M/7SNrI7+lZvgvY+w5+jlvAd9EyJ2g4 i9mZ6n7yAbnLgqKd9sEi4wR/CeM6dPCS1pC2E4ie/D3PgTyU+P2U0c/H4Yx36degOnIs Ibbq/wjto4RFYv+Klqx+fSGZosbzWUSJz4NRtcbZLXn3ka/wrFeLCARR7iG4884z2w+g CeDg== 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 j25-20020a170906279900b00992d6e88087si7242306ejc.955.2023.07.13.03.59.18; Thu, 13 Jul 2023 03:59:18 -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 0FF6268C6E2; Thu, 13 Jul 2023 13:58:00 +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 246B268C67B for ; Thu, 13 Jul 2023 13:57:52 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id B640B2404EA for ; Thu, 13 Jul 2023 12:57:51 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id EY0Aed26QeWY for ; Thu, 13 Jul 2023 12:57:50 +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 C7727240D1E 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 7980C3A15A9 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:32 +0200 Message-Id: <20230713105553.21052-12-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 12/33] fftools/ffmpeg_mux_init: return error codes from ost_add() 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: I6KfgwkLF+/l --- fftools/ffmpeg_mux_init.c | 88 +++++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 35 deletions(-) diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c index bd4c0a9f97..8ad799f951 100644 --- a/fftools/ffmpeg_mux_init.c +++ b/fftools/ffmpeg_mux_init.c @@ -979,9 +979,9 @@ fail: return ret; } -static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, - enum AVMediaType type, InputStream *ist, - OutputFilter *ofilter) +static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, + InputStream *ist, OutputFilter *ofilter, + OutputStream **post) { AVFormatContext *oc = mux->fc; MuxStream *ms; @@ -996,7 +996,7 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, st = avformat_new_stream(oc, NULL); if (!st) - report_and_exit(AVERROR(ENOMEM)); + return AVERROR(ENOMEM); if (oc->nb_streams - 1 < o->nb_streamid_map) st->id = o->streamid_map[oc->nb_streams - 1]; @@ -1006,11 +1006,11 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, ost->par_in = avcodec_parameters_alloc(); if (!ost->par_in) - report_and_exit(AVERROR(ENOMEM)); + return AVERROR(ENOMEM); ms->muxing_queue = av_fifo_alloc2(8, sizeof(AVPacket*), 0); if (!ms->muxing_queue) - report_and_exit(AVERROR(ENOMEM)); + return AVERROR(ENOMEM); ms->last_mux_dts = AV_NOPTS_VALUE; ost->st = st; @@ -1022,17 +1022,17 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, ret = choose_encoder(o, oc, ost, &enc); if (ret < 0) { av_log(ost, AV_LOG_FATAL, "Error selecting an encoder\n"); - exit_program(1); + return ret; } if (enc) { ost->enc_ctx = avcodec_alloc_context3(enc); if (!ost->enc_ctx) - report_and_exit(AVERROR(ENOMEM)); + return AVERROR(ENOMEM); ret = enc_alloc(&ost->enc, enc); if (ret < 0) - report_and_exit(ret); + return ret; av_strlcat(ms->log_name, "/", sizeof(ms->log_name)); av_strlcat(ms->log_name, enc->name, sizeof(ms->log_name)); @@ -1042,7 +1042,7 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, "Streamcopy requested for output stream fed " "from a complex filtergraph. Filtering and streamcopy " "cannot be used together.\n"); - exit_program(1); + return AVERROR(EINVAL); } av_strlcat(ms->log_name, "/copy", sizeof(ms->log_name)); @@ -1063,7 +1063,7 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, ms->pkt = av_packet_alloc(); if (!ms->pkt) - report_and_exit(AVERROR(ENOMEM)); + return AVERROR(ENOMEM); if (ost->enc_ctx) { AVCodecContext *enc = ost->enc_ctx; @@ -1088,7 +1088,8 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, continue; if (!(arg = strchr(buf, '='))) { av_log(ost, AV_LOG_FATAL, "Invalid line found in the preset file.\n"); - exit_program(1); + ret = AVERROR(EINVAL); + break; } *arg++ = 0; av_dict_set(&ost->encoder_opts, buf, arg, AV_DICT_DONT_OVERWRITE); @@ -1099,7 +1100,7 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, if (ret) { av_log(ost, AV_LOG_FATAL, "Preset %s specified, but could not be opened.\n", preset); - exit_program(1); + return ret; } MATCH_PER_STREAM_OPT(enc_stats_pre, str, enc_stats_pre, oc, st); @@ -1111,7 +1112,7 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, ret = enc_stats_init(ost, &ost->enc_stats_pre, 1, enc_stats_pre, format); if (ret < 0) - exit_program(1); + return ret; } MATCH_PER_STREAM_OPT(enc_stats_post, str, enc_stats_post, oc, st); @@ -1123,7 +1124,7 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, ret = enc_stats_init(ost, &ost->enc_stats_post, 0, enc_stats_post, format); if (ret < 0) - exit_program(1); + return ret; } MATCH_PER_STREAM_OPT(mux_stats, str, mux_stats, oc, st); @@ -1135,7 +1136,7 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, ret = enc_stats_init(ost, &ms->stats, 0, mux_stats, format); if (ret < 0) - exit_program(1); + return ret; } MATCH_PER_STREAM_OPT(enc_time_bases, str, enc_time_base, oc, st); @@ -1144,14 +1145,14 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, if (av_parse_ratio(&q, enc_time_base, INT_MAX, 0, NULL) < 0 || q.den <= 0) { av_log(ost, AV_LOG_FATAL, "Invalid time base: %s\n", enc_time_base); - exit_program(1); + return AVERROR(EINVAL); } if (q.num < 0) { if (!ost->ist) { av_log(ost, AV_LOG_FATAL, "Cannot use input stream timebase for encoding - " "no input stream available\n"); - exit_program(1); + return AVERROR(EINVAL); } q = ost->ist->st->time_base; } @@ -1175,7 +1176,7 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, if (av_parse_ratio(&q, time_base, INT_MAX, 0, NULL) < 0 || q.num <= 0 || q.den <= 0) { av_log(ost, AV_LOG_FATAL, "Invalid time base: %s\n", time_base); - exit_program(1); + return AVERROR(EINVAL); } st->time_base = q; } @@ -1198,7 +1199,7 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, ret = av_bsf_list_parse_str(bsfs, &ms->bsf_ctx); if (ret < 0) { av_log(ost, AV_LOG_ERROR, "Error parsing bitstream filter sequence '%s': %s\n", bsfs, av_err2str(ret)); - exit_program(1); + return ret; } } @@ -1258,7 +1259,7 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO) { ret = ost_get_filters(o, oc, ost, &filters); if (ret < 0) - exit_program(1); + return ret; } if (ost->enc && @@ -1271,7 +1272,7 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, if (ret < 0) { av_log(ost, AV_LOG_ERROR, "Error initializing a simple filtergraph\n"); - exit_program(1); + return ret; } } } else if (ost->ist) { @@ -1279,14 +1280,14 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, if (ret < 0) { av_log(ost, AV_LOG_ERROR, "Error binding an input stream\n"); - exit_program(1); + return ret; } } if (ost->ist && !ost->enc) { ret = streamcopy_init(mux, ost); if (ret < 0) - exit_program(1); + return ret; } // copy estimated duration as a hint to the muxer @@ -1295,7 +1296,10 @@ static OutputStream *ost_add(Muxer *mux, const OptionsContext *o, ms->stream_duration_tb = ist->st->time_base; } - return ost; + if (post) + *post = ost; + + return 0; } static int map_auto_video(Muxer *mux, const OptionsContext *o) @@ -1346,7 +1350,7 @@ static int map_auto_video(Muxer *mux, const OptionsContext *o) } } if (best_ist) - ost_add(mux, o, AVMEDIA_TYPE_VIDEO, best_ist, NULL); + return ost_add(mux, o, AVMEDIA_TYPE_VIDEO, best_ist, NULL, NULL); return 0; } @@ -1390,7 +1394,7 @@ static int map_auto_audio(Muxer *mux, const OptionsContext *o) } } if (best_ist) - ost_add(mux, o, AVMEDIA_TYPE_AUDIO, best_ist, NULL); + return ost_add(mux, o, AVMEDIA_TYPE_AUDIO, best_ist, NULL, NULL); return 0; } @@ -1427,8 +1431,7 @@ static int map_auto_subtitle(Muxer *mux, const OptionsContext *o) input_descriptor && output_descriptor && (!input_descriptor->props || !output_descriptor->props)) { - ost_add(mux, o, AVMEDIA_TYPE_SUBTITLE, ist, NULL); - break; + return ost_add(mux, o, AVMEDIA_TYPE_SUBTITLE, ist, NULL, NULL); } } @@ -1448,8 +1451,11 @@ static int map_auto_data(Muxer *mux, const OptionsContext *o) if (ist->user_set_discard == AVDISCARD_ALL) continue; if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA && - ist->st->codecpar->codec_id == codec_id ) - ost_add(mux, o, AVMEDIA_TYPE_DATA, ist, NULL); + ist->st->codecpar->codec_id == codec_id) { + int ret = ost_add(mux, o, AVMEDIA_TYPE_DATA, ist, NULL, NULL); + if (ret < 0) + return ret; + } } return 0; @@ -1458,6 +1464,7 @@ static int map_auto_data(Muxer *mux, const OptionsContext *o) static int map_manual(Muxer *mux, const OptionsContext *o, const StreamMap *map) { InputStream *ist; + int ret; if (map->disabled) return 0; @@ -1487,7 +1494,9 @@ loop_end: av_log(mux, AV_LOG_VERBOSE, "Creating output stream from an explicitly " "mapped complex filtergraph %d, output [%s]\n", fg->index, map->linklabel); - ost_add(mux, o, ofilter->type, NULL, ofilter); + ret = ost_add(mux, o, ofilter->type, NULL, ofilter, NULL); + if (ret < 0) + return ret; } else { ist = input_files[map->file_index]->streams[map->stream_index]; if (ist->user_set_discard == AVDISCARD_ALL) { @@ -1519,7 +1528,9 @@ loop_end: return 0; } - ost_add(mux, o, ist->st->codecpar->codec_type, ist, NULL); + ret = ost_add(mux, o, ist->st->codecpar->codec_type, ist, NULL, NULL); + if (ret < 0) + return ret; } return 0; @@ -1580,7 +1591,12 @@ read_fail: av_log(mux, AV_LOG_VERBOSE, "Creating attachment stream from file %s\n", o->attachments[i]); - ost = ost_add(mux, o, AVMEDIA_TYPE_ATTACHMENT, NULL, NULL); + err = ost_add(mux, o, AVMEDIA_TYPE_ATTACHMENT, NULL, NULL, &ost); + if (err < 0) { + av_freep(&attachment); + return err; + } + ost->attachment_filename = o->attachments[i]; ost->par_in->extradata = attachment; ost->par_in->extradata_size = len; @@ -1629,7 +1645,9 @@ static int create_streams(Muxer *mux, const OptionsContext *o) av_get_media_type_string(ofilter->type)); av_log(mux, AV_LOG_VERBOSE, "\n"); - ost_add(mux, o, ofilter->type, NULL, ofilter); + ret = ost_add(mux, o, ofilter->type, NULL, ofilter, NULL); + if (ret < 0) + return ret; } }