From patchwork Thu Mar 4 06:19:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suji Velupillai X-Patchwork-Id: 26083 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 9967244BC8A for ; Thu, 4 Mar 2021 08:20:09 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 6B80168A115; Thu, 4 Mar 2021 08:20:09 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-oi1-f227.google.com (mail-oi1-f227.google.com [209.85.167.227]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0ACA7680B20 for ; Thu, 4 Mar 2021 08:20:02 +0200 (EET) Received: by mail-oi1-f227.google.com with SMTP id w65so5373235oie.7 for ; Wed, 03 Mar 2021 22:20:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=from:to:cc:subject:date:message-id; bh=pCgcIwoLnTRiSD4H9+7m38ryhl4rRQ5s8wo7zRW89Fw=; b=fmhudsyY1gFmXk+rlP53A9t68wMcAv9I4Bp93wsJRXq5QPPPP7lpAcXWB5QXKNhVbv 7pl4vlSFD0mzvSw5oSNs+TNLER8C8GL2vOlutVmBYI3qUAyAprqeS+Vh7jExCT1p5x47 BEJUMxAYx7CnX03lX3Q27JE/IpUZupmGUvElE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=pCgcIwoLnTRiSD4H9+7m38ryhl4rRQ5s8wo7zRW89Fw=; b=PLycu7YCEg8tt1AX7BYmEXxmkuxEcT5zMthq4URHlXL5vMDStSR5Y5wYs5PCr2BXY+ 34yFzh14/+zJv+sggZU4ylMhmeS/nwgTYgajkqdxx8H5rMHx3U0ZSeUhmrBVf6aE8BPG ctZvMXIfawBn3zZHgIj2CIjC8RcU98SLXC1vT3NOpJyhus59/GrqKyzaBTDNZJFlJvsn 6NPJoASW9bcd0AQ6wESXs9ffJEK2YfBQC4aCbKOKlQiuz7xQt665uNMufJMg592A7kmb vStOvhD6tfEKvv2L8EBr1lYqxlelsB826uyuPv1d/0hEib/yppDTmR88ruPwDT91FO/a sVsg== X-Gm-Message-State: AOAM533jWJ2ZZvDQjzKlfQbn5KEwm50Pwe+yMmKaV0v+aUNublEe0Zsh 9xbt8VxMxL/7OLSIo2C7kIA/t+KO54RIedecrUoWnSaJlD27CY+HUAIMiwWHuz8XF+6pPJ5weK0 358Zx0baj1hxlGWc4NfOHxlbtbrU7YtCyNLG3DxUveSebG+2q45sSLKVsrUPZOBMozQYfVsJv9f IqjexMPbNMk/vqaPMPQR4= X-Google-Smtp-Source: ABdhPJwqxLVxDl70LaAmGPMnwW7UkIe0dXk44dntJTGmcFbYVsYf/OZUWy3A21ReT9jkOQMT80D5NonHBz7I X-Received: by 2002:aca:de09:: with SMTP id v9mr1936289oig.50.1614838801293; Wed, 03 Mar 2021 22:20:01 -0800 (PST) Received: from suji-Vostro-460.ric.broadcom.com ([192.19.228.250]) by smtp-relay.gmail.com with ESMTPS id w3sm5056212oow.27.2021.03.03.22.20.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Mar 2021 22:20:01 -0800 (PST) X-Relaying-Domain: broadcom.com From: suji.velupillai@broadcom.com To: ffmpeg-devel@ffmpeg.org Date: Wed, 3 Mar 2021 22:19:09 -0800 Message-Id: <20210304061909.8114-1-suji.velupillai@broadcom.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avutil/hwcontext: add support to allow hardware to ref/unref frame X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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 Cc: suji.velupillai@broadcom.com, patrick.rault@broadcom.com MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Patrick Rault Add support to allow attached hardware to add/remove reference to the frame buffer mirroring the ffmpeg. Signed-off-by: Patrick Rault Signed-off-by: Suji Velupillai --- libavutil/frame.c | 7 ++++++- libavutil/hwcontext.c | 21 +++++++++++++++++++++ libavutil/hwcontext.h | 15 +++++++++++++++ libavutil/hwcontext_internal.h | 2 ++ 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/libavutil/frame.c b/libavutil/frame.c index eab51b6a32..6f8db14bf5 100644 --- a/libavutil/frame.c +++ b/libavutil/frame.c @@ -22,6 +22,7 @@ #include "common.h" #include "dict.h" #include "frame.h" +#include "hwcontext.h" #include "imgutils.h" #include "mem.h" #include "samplefmt.h" @@ -492,7 +493,7 @@ int av_frame_ref(AVFrame *dst, const AVFrame *src) dst->nb_extended_buf = src->nb_extended_buf; for (i = 0; i < src->nb_extended_buf; i++) { - dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]); + dst->extended_buf[i] = av_buffer_ref(dst); if (!dst->extended_buf[i]) { ret = AVERROR(ENOMEM); goto fail; @@ -530,6 +531,8 @@ int av_frame_ref(AVFrame *dst, const AVFrame *src) memcpy(dst->data, src->data, sizeof(src->data)); memcpy(dst->linesize, src->linesize, sizeof(src->linesize)); + av_hwframe_ref_buffer(dst); + return 0; fail: @@ -571,6 +574,8 @@ FF_DISABLE_DEPRECATION_WARNINGS FF_ENABLE_DEPRECATION_WARNINGS #endif + av_hwframe_unref_buffer(frame); + av_buffer_unref(&frame->hw_frames_ctx); av_buffer_unref(&frame->opaque_ref); diff --git a/libavutil/hwcontext.c b/libavutil/hwcontext.c index d13d0f7c9b..5e2209725e 100644 --- a/libavutil/hwcontext.c +++ b/libavutil/hwcontext.c @@ -562,6 +562,27 @@ int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags) return 0; } +int av_hwframe_ref_buffer(AVFrame *frame) +{ + int ret = 0; + + if (frame->hw_frames_ctx) { + AVHWFramesContext *ctx = (AVHWFramesContext*)frame->hw_frames_ctx->data; + if (ctx->internal->hw_type->frames_ref_buffer) + ret = ctx->internal->hw_type->frames_ref_buffer(frame); + } + return ret; +} + +void av_hwframe_unref_buffer(AVFrame *frame) +{ + if (frame->hw_frames_ctx) { + AVHWFramesContext *ctx = (AVHWFramesContext*)frame->hw_frames_ctx->data; + if (ctx->internal->hw_type->frames_unref_buffer) + ctx->internal->hw_type->frames_unref_buffer(frame); + } +} + void *av_hwdevice_hwconfig_alloc(AVBufferRef *ref) { AVHWDeviceContext *ctx = (AVHWDeviceContext*)ref->data; diff --git a/libavutil/hwcontext.h b/libavutil/hwcontext.h index 04d19d89c2..9dc74b9e46 100644 --- a/libavutil/hwcontext.h +++ b/libavutil/hwcontext.h @@ -381,6 +381,21 @@ int av_hwframe_ctx_init(AVBufferRef *ref); */ int av_hwframe_get_buffer(AVBufferRef *hwframe_ctx, AVFrame *frame, int flags); +/** + * Allow hardware to add a reference on a buffer + * + * @param frame Hardware to add reference to the frame + * @return 0 on success, a negative AVERROR code on failure + */ +int av_hwframe_ref_buffer(AVFrame *frame); + +/** + * Allow to release hardware specific attached to the frame + * + * @param frame Hardware to release reference on the frame + */ +void av_hwframe_unref_buffer(AVFrame *frame); + /** * Copy data to or from a hw surface. At least one of dst/src must have an * AVHWFramesContext attached. diff --git a/libavutil/hwcontext_internal.h b/libavutil/hwcontext_internal.h index e6266494ac..fde34932c6 100644 --- a/libavutil/hwcontext_internal.h +++ b/libavutil/hwcontext_internal.h @@ -81,6 +81,8 @@ typedef struct HWContextType { void (*frames_uninit)(AVHWFramesContext *ctx); int (*frames_get_buffer)(AVHWFramesContext *ctx, AVFrame *frame); + int (*frames_ref_buffer)(AVFrame *frame); + void (*frames_unref_buffer)(AVFrame *frame); int (*transfer_get_formats)(AVHWFramesContext *ctx, enum AVHWFrameTransferDirection dir, enum AVPixelFormat **formats);