From patchwork Sat Jul 15 10:46:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 42734 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6da1:b0:131:a7d0:bc6d with SMTP id gl33csp4241619pzb; Sat, 15 Jul 2023 03:54:13 -0700 (PDT) X-Google-Smtp-Source: APBJJlGZu0pfCnD4FXxew6KYOrP24cumde8+fIOC9qQttWjlhutbVUhRnMwzTo+d8SOlOfuzMT23 X-Received: by 2002:a17:906:314c:b0:957:1df0:9cbf with SMTP id e12-20020a170906314c00b009571df09cbfmr5049423eje.19.1689418452731; Sat, 15 Jul 2023 03:54:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689418452; cv=none; d=google.com; s=arc-20160816; b=tVPq3pW1Usa2z0h5hYY4Yri2xt9CKkUgDwx26tL/mngFiwPs/ZjJjSO0UeVfoweWdh /r09cTMuVc7SATppJ4Pm/TCQCblH33SuDBHHptnKhgUaJTi01+ytXEmuzBOWyz7a+5vo k23ODHMqNjKeGZiIrcOSWa7u4HbMofOH5E5mbFtHMiFlovmHczCnAbGigl+Zkp+zYOV5 xLF55/AItG0DWI7VtmIVy6hjzywnX+KNsXNgqh96uxHiX+h5dn+fFREva3HOuNHILXTD uae6w13rsenhxjTnz3LzqrYOv04e6m8MqFBBuFcgXLlPjxjUMYQfKF462576uDF5IYP1 RNwQ== 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=PpA3eohRlzZHVZaKUdgjXW+GcsfVjSaITt2hMUzQV1c=; fh=8ZF2QtiKOjBtyn9wuO2cMeZaLsE4aq02Bq77gGK61E8=; b=oCJ144FUfitPCUmHgd4NLQXRDWrtMeuT2wDTbSV6DtBfbwQdeDziGadRrroANzfErA mjD5GGNB76hfgnj19+HYoc8MdZqgRSxvw2kLHaUOxoBGOLNZQSnIP4rw9w5aNpyBObbW G9GTXMlGskIIWqfGvYnvndDSO+kKeWfGZgVpSUJ6z7ZgCiT8a65AMxCuyxV3jLH3mU5r WrFu0EiyaS7Vn2c7Oy8vV9yw6AYoaq8Gk+vWi7jzLvNBEk6XIkjw2ejR2BizqoRqkia9 l4spgUJDUxtTBfMYG5N3lxws6xlPZfdBULJkymbI6CyBebFsSt1UMR2cpZSGauwCaA7u EjJw== 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 k22-20020a170906a39600b0099279b2cdd5si10503849ejz.825.2023.07.15.03.54.12; Sat, 15 Jul 2023 03:54:12 -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 963B268C773; Sat, 15 Jul 2023 13:53:44 +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 B43C968C744 for ; Sat, 15 Jul 2023 13:53:36 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 727A92404F8 for ; Sat, 15 Jul 2023 12:53:36 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id X12wcy57B6G6 for ; Sat, 15 Jul 2023 12:53:34 +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 A93A32404EA for ; Sat, 15 Jul 2023 12:53:32 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 9F7053A26F1 for ; Sat, 15 Jul 2023 12:46:21 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Sat, 15 Jul 2023 12:46:00 +0200 Message-Id: <20230715104611.17902-36-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 36/47] fftools/cmdutils: add error handling to filter_codec_opts() 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: OTxFRtX/Mqu+ --- fftools/cmdutils.c | 41 ++++++++++++++++++++++++++------------- fftools/cmdutils.h | 8 +++++--- fftools/ffmpeg_demux.c | 5 ++++- fftools/ffmpeg_mux_init.c | 11 ++++++++--- fftools/ffplay.c | 6 +++++- fftools/ffprobe.c | 8 ++++++-- 6 files changed, 56 insertions(+), 23 deletions(-) diff --git a/fftools/cmdutils.c b/fftools/cmdutils.c index 137e69f2c3..9d9d8d44a6 100644 --- a/fftools/cmdutils.c +++ b/fftools/cmdutils.c @@ -943,8 +943,9 @@ int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec) return ret; } -AVDictionary *filter_codec_opts(const AVDictionary *opts, enum AVCodecID codec_id, - AVFormatContext *s, AVStream *st, const AVCodec *codec) +int filter_codec_opts(const AVDictionary *opts, enum AVCodecID codec_id, + AVFormatContext *s, AVStream *st, const AVCodec *codec, + AVDictionary **dst) { AVDictionary *ret = NULL; const AVDictionaryEntry *t = NULL; @@ -977,12 +978,16 @@ AVDictionary *filter_codec_opts(const AVDictionary *opts, enum AVCodecID codec_i char *p = strchr(t->key, ':'); /* check stream specification in opt name */ - if (p) - switch (check_stream_specifier(s, st, p + 1)) { - case 1: *p = 0; break; - case 0: continue; - default: exit_program(1); - } + if (p) { + int err = check_stream_specifier(s, st, p + 1); + if (err < 0) { + av_dict_free(&ret); + return err; + } else if (!err) + continue; + + *p = 0; + } if (av_opt_find(&cc, t->key, NULL, flags, AV_OPT_SEARCH_FAKE_OBJ) || !codec || @@ -998,14 +1003,16 @@ AVDictionary *filter_codec_opts(const AVDictionary *opts, enum AVCodecID codec_i if (p) *p = ':'; } - return ret; + + *dst = ret; + return 0; } int setup_find_stream_info_opts(AVFormatContext *s, AVDictionary *codec_opts, AVDictionary ***dst) { - int i; + int ret; AVDictionary **opts; *dst = NULL; @@ -1017,11 +1024,19 @@ int setup_find_stream_info_opts(AVFormatContext *s, if (!opts) return AVERROR(ENOMEM); - for (i = 0; i < s->nb_streams; i++) - opts[i] = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id, - s, s->streams[i], NULL); + for (int i = 0; i < s->nb_streams; i++) { + ret = filter_codec_opts(codec_opts, s->streams[i]->codecpar->codec_id, + s, s->streams[i], NULL, &opts[i]); + if (ret < 0) + goto fail; + } *dst = opts; return 0; +fail: + for (int i = 0; i < s->nb_streams; i++) + av_dict_free(&opts[i]); + av_freep(&opts); + return ret; } int grow_array(void **array, int elem_size, int *size, int new_size) diff --git a/fftools/cmdutils.h b/fftools/cmdutils.h index 7e7fb700b7..97e49850c5 100644 --- a/fftools/cmdutils.h +++ b/fftools/cmdutils.h @@ -333,10 +333,12 @@ int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec); * @param st A stream from s for which the options should be filtered. * @param codec The particular codec for which the options should be filtered. * If null, the default one is looked up according to the codec id. - * @return a pointer to the created dictionary + * @param dst a pointer to the created dictionary + * @return a non-negative number on success, a negative error code on failure */ -AVDictionary *filter_codec_opts(const AVDictionary *opts, enum AVCodecID codec_id, - AVFormatContext *s, AVStream *st, const AVCodec *codec); +int filter_codec_opts(const AVDictionary *opts, enum AVCodecID codec_id, + AVFormatContext *s, AVStream *st, const AVCodec *codec, + AVDictionary **dst); /** * Setup AVCodecContext options for avformat_find_stream_info(). diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c index a41b59ceb8..d612c5f434 100644 --- a/fftools/ffmpeg_demux.c +++ b/fftools/ffmpeg_demux.c @@ -1176,7 +1176,10 @@ static int ist_add(const OptionsContext *o, Demuxer *d, AVStream *st) if (ret < 0) return ret; - ist->decoder_opts = filter_codec_opts(o->g->codec_opts, ist->st->codecpar->codec_id, ic, st, ist->dec); + ret = filter_codec_opts(o->g->codec_opts, ist->st->codecpar->codec_id, + ic, st, ist->dec, &ist->decoder_opts); + if (ret < 0) + return ret; ist->reinit_filters = -1; MATCH_PER_STREAM_OPT(reinit_filters, i, ist->reinit_filters, ic, st); diff --git a/fftools/ffmpeg_mux_init.c b/fftools/ffmpeg_mux_init.c index 92d62c7b89..63d86209ff 100644 --- a/fftools/ffmpeg_mux_init.c +++ b/fftools/ffmpeg_mux_init.c @@ -1217,8 +1217,10 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, const char *enc_stats_pre = NULL, *enc_stats_post = NULL, *mux_stats = NULL; const char *enc_time_base = NULL; - ost->encoder_opts = filter_codec_opts(o->g->codec_opts, enc->codec_id, - oc, st, enc->codec); + ret = filter_codec_opts(o->g->codec_opts, enc->codec_id, + oc, st, enc->codec, &ost->encoder_opts); + if (ret < 0) + return ret; MATCH_PER_STREAM_OPT(presets, str, preset, oc, st); ost->autoscale = 1; @@ -1316,7 +1318,10 @@ static int ost_add(Muxer *mux, const OptionsContext *o, enum AVMediaType type, ost->enc_timebase = q; } } else { - ost->encoder_opts = filter_codec_opts(o->g->codec_opts, AV_CODEC_ID_NONE, oc, st, NULL); + ret = filter_codec_opts(o->g->codec_opts, AV_CODEC_ID_NONE, oc, st, + NULL, &ost->encoder_opts); + if (ret < 0) + return ret; } diff --git a/fftools/ffplay.c b/fftools/ffplay.c index 4e26b3309d..df20c6a29d 100644 --- a/fftools/ffplay.c +++ b/fftools/ffplay.c @@ -2584,7 +2584,11 @@ static int stream_component_open(VideoState *is, int stream_index) if (fast) avctx->flags2 |= AV_CODEC_FLAG2_FAST; - opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], codec); + ret = filter_codec_opts(codec_opts, avctx->codec_id, ic, + ic->streams[stream_index], codec, &opts); + if (ret < 0) + goto fail; + if (!av_dict_get(opts, "threads", NULL, 0)) av_dict_set(&opts, "threads", "auto", 0); if (stream_lowres) diff --git a/fftools/ffprobe.c b/fftools/ffprobe.c index ba55437760..da8fc89830 100644 --- a/fftools/ffprobe.c +++ b/fftools/ffprobe.c @@ -3421,8 +3421,12 @@ static int open_input_file(InputFile *ifile, const char *filename, continue; } { - AVDictionary *opts = filter_codec_opts(codec_opts, stream->codecpar->codec_id, - fmt_ctx, stream, codec); + AVDictionary *opts; + + err = filter_codec_opts(codec_opts, stream->codecpar->codec_id, + fmt_ctx, stream, codec, &opts); + if (err < 0) + exit(1); ist->dec_ctx = avcodec_alloc_context3(codec); if (!ist->dec_ctx)