From patchwork Wed Jan 19 14:01:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 33660 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp4813401iog; Wed, 19 Jan 2022 06:02:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJxsvckAZbVclxiKD8KyAKIeYxOFfnOTY0BsCP/BZhQ0Qv3ht2gnXcF+YoxQ2Yx0x6GmP4wG X-Received: by 2002:a17:906:6856:: with SMTP id a22mr1507508ejs.515.1642600920029; Wed, 19 Jan 2022 06:02:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642600920; cv=none; d=google.com; s=arc-20160816; b=G0vwlWUEarQUhJpJGZvCA+P/gWMuaGv+/8SJjQMzP3QY+gLKYkhwjI77C+Zmcfm4Xg et/Ijuckb3qBw3LQjx9+Ki9HAYuxsketwKTPJCXQZi22HKfOKSk7QFh6b9917aEXJKTO Sufc7lUHDx1KyF3FXH14a/gRQ/bjWr0Y5lPMGXslk8p6rsuXFAKBAyKG/t5hTTvGExHj 912ePu6Stw25JC/mZTp/PZI1kdhxDKBo+3uVx0s1J/UnlUiGrldCXJ2P1d3IHbetpjAH W8V4aPJ61d0GxyN5d96GGljqYKlDMV5iKK+dMPQDxq0qb+CS40/0it+1XsCGjAxaip9+ Fffw== 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=S04/dGyiUhr6vrZ9/+9uW7kp1OKTS7oqIsI8xuP++k4=; b=vAKOp2AEmwwTBW/oIxlAwXR1+/q7WpHC/zmPiOTD2G+pqkL/N+PYed5hzLlpPDzNPs YFpdWhXG23ftIQZeIlYzkfGrIlOEvODX5JI0jfzGQ13DvLUWVzvVss9gTM+S/5aQRlU0 92Ru68qsNyENdkpurci/oa6UkTvFAeG1iemun9aaHEZWJfLZ3uB0yuloWHn6vOLnodp3 ZgqAYBj4Za2SJmeQtkQh6kRdoPLyJLEtwwpj0XltLGRxfPYrfE+t/4ySVklOIaY0VLN7 vfaj5mZrwnqOim4Jw4fFwmALx8xYXaOftlV/mUMtqFG7mAKS5PIXOhrWh5gzHTORCXNk QrDA== 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 y21si1694880ejk.641.2022.01.19.06.01.58; Wed, 19 Jan 2022 06:02:00 -0800 (PST) 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 8EB2E68B0AA; Wed, 19 Jan 2022 16:01:54 +0200 (EET) 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 52DCE68B087 for ; Wed, 19 Jan 2022 16:01:47 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id E63FE24017C for ; Wed, 19 Jan 2022 15:01:46 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id eHoq-n6jlPUd for ; Wed, 19 Jan 2022 15:01:46 +0100 (CET) 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 4430F240179 for ; Wed, 19 Jan 2022 15:01:46 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 30CF63A067C; Wed, 19 Jan 2022 15:01:46 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Wed, 19 Jan 2022 15:01:31 +0100 Message-Id: <20220119140130.20907-1-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <51796511-a27c-3318-9d97-bb8ce4c972a0@gmail.com> References: <51796511-a27c-3318-9d97-bb8ce4c972a0@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] lavu/hwcontext: clarify behavior on av_hwframe_map() failure 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: Zo4Is+LURmoe Clear anything that av_hwframe_map() might have done to the destination frame, but leave caller-provided fields unchanged. --- Interdiff: diff --git a/libavutil/hwcontext.c b/libavutil/hwcontext.c index ae33da1262..ab9ad3703e 100644 --- a/libavutil/hwcontext.c +++ b/libavutil/hwcontext.c @@ -827,7 +827,9 @@ int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags) src_frames->internal->hw_type->map_from) { ret = src_frames->internal->hw_type->map_from(src_frames, dst, src, flags); - if (ret != AVERROR(ENOSYS)) + if (ret >= 0) + return ret; + else if (ret != AVERROR(ENOSYS)) goto fail; } } @@ -839,7 +841,9 @@ int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags) dst_frames->internal->hw_type->map_to) { ret = dst_frames->internal->hw_type->map_to(dst_frames, dst, src, flags); - if (ret != AVERROR(ENOSYS)) + if (ret >= 0) + return ret; + else if (ret != AVERROR(ENOSYS)) goto fail; } } libavutil/hwcontext.c | 27 +++++++++++++++++++++++++-- libavutil/hwcontext.h | 4 ++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/libavutil/hwcontext.c b/libavutil/hwcontext.c index 31c7840dba..ab9ad3703e 100644 --- a/libavutil/hwcontext.c +++ b/libavutil/hwcontext.c @@ -18,6 +18,7 @@ #include "config.h" +#include "avassert.h" #include "buffer.h" #include "common.h" #include "hwcontext.h" @@ -788,6 +789,8 @@ fail: int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags) { + AVBufferRef *orig_dst_frames = dst->hw_frames_ctx; + enum AVPixelFormat orig_dst_fmt = dst->format; AVHWFramesContext *src_frames, *dst_frames; HWMapDescriptor *hwmap; int ret; @@ -824,8 +827,10 @@ int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags) src_frames->internal->hw_type->map_from) { ret = src_frames->internal->hw_type->map_from(src_frames, dst, src, flags); - if (ret != AVERROR(ENOSYS)) + if (ret >= 0) return ret; + else if (ret != AVERROR(ENOSYS)) + goto fail; } } @@ -836,12 +841,30 @@ int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags) dst_frames->internal->hw_type->map_to) { ret = dst_frames->internal->hw_type->map_to(dst_frames, dst, src, flags); - if (ret != AVERROR(ENOSYS)) + if (ret >= 0) return ret; + else if (ret != AVERROR(ENOSYS)) + goto fail; } } return AVERROR(ENOSYS); + +fail: + // if the caller provided dst frames context, it should be preserved + // by this function + av_assert0(orig_dst_frames == NULL || + orig_dst_frames == dst->hw_frames_ctx); + + // preserve user-provided dst frame fields, but clean + // anything we might have set + dst->hw_frames_ctx = NULL; + av_frame_unref(dst); + + dst->hw_frames_ctx = orig_dst_frames; + dst->format = orig_dst_fmt; + + return ret; } int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx, diff --git a/libavutil/hwcontext.h b/libavutil/hwcontext.h index 04d19d89c2..c18b7e1e8b 100644 --- a/libavutil/hwcontext.h +++ b/libavutil/hwcontext.h @@ -571,6 +571,10 @@ enum { * possible with the given arguments and hwframe setup, while other return * values indicate that it failed somehow. * + * On failure, the destination frame will be left blank, except for the + * hw_frames_ctx/format fields thay may have been set by the caller - those will + * be preserved as they were. + * * @param dst Destination frame, to contain the mapping. * @param src Source frame, to be mapped. * @param flags Some combination of AV_HWFRAME_MAP_* flags.