From patchwork Sun Feb 16 20:59:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 17805 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 DE5D544A6EF for ; Sun, 16 Feb 2020 23:00:05 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BAFBF6881BD; Sun, 16 Feb 2020 23:00:05 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8E739687ED3 for ; Sun, 16 Feb 2020 22:59:59 +0200 (EET) Received: by mail-wr1-f48.google.com with SMTP id n10so15317038wrm.1 for ; Sun, 16 Feb 2020 12:59:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jkqxz-net.20150623.gappssmtp.com; s=20150623; h=to:from:subject:message-id:date:user-agent:mime-version :content-language:content-transfer-encoding; bh=5FUj4M62CETJslBfKOYF2yJhKjnkEKIsahPU+vQhy2Q=; b=CMh9MyI/KqqYzb/i/k0JYapafDpHIAyVzy+l4fzzukInhnTvJ1qTQCtNXoE1KUaWvC 0S/RtaWQlH8SqzO2bpRgVgv2Fkm5t3FKFl2WhjINeqgKWLF4QOrWDlp9ftMi4Yf5JOA5 OxAOVmGxQa4zp1ZnGKleg9Z88VBxxpxQ5u2halj9lGH9CywD8XGHVcdsFd3du7IMFY8U EttMNkS8ulwu/L5ilSl/vBi7VXgSnI/aFdnpzkDX+4bkKE4DpcYn/a0+VasYQv5Y3r9R gys9KD6QVslIIYp7zm4YJfO9yHzc4Lxq57J4MbZ78uBQ9xBVkus2MUNqL9OBZ228xhep +zJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language:content-transfer-encoding; bh=5FUj4M62CETJslBfKOYF2yJhKjnkEKIsahPU+vQhy2Q=; b=BuJsyBITgzdWpq1THlQphAh4DEqAY+W4+2rz2jG8wtBaawfZQ+1AyVjyyIPa1ECLYZ 0te2vuuBc/nbVit+Nx52mnJyldQQhRCASn+v+UPY7os5is6qw5zyR9raft4dx3nMESGZ aL5yPzc96VqOfWoEutjVq7mY/ueEy6Yl/YYuILTen2/yG/zIG7Dxfta+ys9+ZpigIWsv 2T+UbT3e7BTd4p2sP/RSut4Bk4WPI7rpH4PFvsZeQIal4M+DrKqNy+w5g2QjvPOT1IXg RYB4nAhoEuuwoxegAF+rPuFcVKYCTdeMFJzPQ6vsQsAb2xuBqQ55eAkJKdY2glIUN0wC lVYA== X-Gm-Message-State: APjAAAXrGubTjZ6vIT/m5L/yiWP5C+VbNcbn67ASKDKufBxxmD/AC22R lM3wzuXVB/1D4wTpyDPzKFry7QYbuxw= X-Google-Smtp-Source: APXvYqxaCSAZEUggYogc6JoUgwBCFAlTqYdTOxrOqWByVXmHVnV424WyjARHvKsubAZugNYWSf/hIw== X-Received: by 2002:a05:6000:1112:: with SMTP id z18mr16675955wrw.140.1581886798572; Sun, 16 Feb 2020 12:59:58 -0800 (PST) Received: from [192.168.0.3] (cpc91242-cmbg18-2-0-cust650.5-4.cable.virginm.net. [82.8.130.139]) by smtp.gmail.com with ESMTPSA id a6sm17674420wrm.69.2020.02.16.12.59.56 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 16 Feb 2020 12:59:57 -0800 (PST) To: FFmpeg development discussions and patches From: Mark Thompson Message-ID: Date: Sun, 16 Feb 2020 20:59:54 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.2 MIME-Version: 1.0 Content-Language: en-US Subject: [FFmpeg-devel] [PATCH] hwcontext_vaapi: Only accept a render node when deriving from DRM device 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" If we are given a non-render node, try to find the matching render node and fail if that isn't possible. libva will not accept a non-render device which is not DRM master, because it requires legacy DRM authentication to succeed in that case: . This is annoying for kmsgrab because in most recording situations DRM master is already held by something else (such as a windowing system), leading to device derivation not working and forcing the user to create the target VAAPI device separately. --- Fixes a longstanding annoyance with -vf hwmap=derive_device=vaapi. (E.g. .) libavutil/hwcontext_vaapi.c | 46 ++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/libavutil/hwcontext_vaapi.c b/libavutil/hwcontext_vaapi.c index cf117640f2..d3bea3070b 100644 --- a/libavutil/hwcontext_vaapi.c +++ b/libavutil/hwcontext_vaapi.c @@ -1628,6 +1628,7 @@ static int vaapi_device_derive(AVHWDeviceContext *ctx, AVDRMDeviceContext *src_hwctx = src_ctx->hwctx; VADisplay *display; VAAPIDevicePriv *priv; + int fd; if (src_hwctx->fd < 0) { av_log(ctx, AV_LOG_ERROR, "DRM instance requires an associated " @@ -1635,17 +1636,56 @@ static int vaapi_device_derive(AVHWDeviceContext *ctx, return AVERROR(EINVAL); } +#if CONFIG_LIBDRM + { + int node_type = drmGetNodeTypeFromFd(src_hwctx->fd); + char *render_node; + if (node_type < 0) { + av_log(ctx, AV_LOG_ERROR, "DRM instance fd does not appear " + "to refer to a DRM device.\n"); + return AVERROR(EINVAL); + } + if (node_type == DRM_NODE_RENDER) { + fd = src_hwctx->fd; + } else { + render_node = drmGetRenderDeviceNameFromFd(src_hwctx->fd); + if (!render_node) { + av_log(ctx, AV_LOG_ERROR, "Failed to find a render node " + "matching the DRM device.\n"); + return AVERROR(ENODEV); + } + fd = open(render_node, O_RDWR); + if (fd < 0) { + av_log(ctx, AV_LOG_ERROR, "Failed to open render node %s" + "matching the DRM device.\n", render_node); + free(render_node); + return AVERROR(errno); + } + av_log(ctx, AV_LOG_VERBOSE, "Using render node %s in place " + "of non-render DRM device.\n", render_node); + free(render_node); + } + } +#else + fd = src_hwctx->fd; +#endif + priv = av_mallocz(sizeof(*priv)); if (!priv) return AVERROR(ENOMEM); - // Inherits the fd from the source context, which will close it. - priv->drm_fd = -1; + if (fd == src_hwctx->fd) { + // The fd is inherited from the source context and we are holding + // a reference to that, we don't want to close it from here. + priv->drm_fd = -1; + } else { + priv->drm_fd = fd; + } ctx->user_opaque = priv; ctx->free = &vaapi_device_free; - display = vaGetDisplayDRM(src_hwctx->fd); + display = vaGetDisplayDRM(fd); if (!display) { av_log(ctx, AV_LOG_ERROR, "Failed to open a VA display from " "DRM device.\n");