From patchwork Mon Jun 12 22:40:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 3954 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.22.4 with SMTP id 4csp78419vsw; Mon, 12 Jun 2017 15:44:21 -0700 (PDT) X-Received: by 10.223.135.43 with SMTP id a40mr736895wra.138.1497307461033; Mon, 12 Jun 2017 15:44:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1497307460; cv=none; d=google.com; s=arc-20160816; b=qLDxmq2ynlIhXdDertC/mVL+1H4g5MoXS2c86JZlKg7r+4vqOzt4bVnzplsK/zyxR5 ZgIWVhpHWsSFbosIuO/kO1eI1Nn9Xyigx625nj456YnHDsbYCbONjWmiHDH7Rq8Bhcy2 hUTVj3wKxceGGntPjR4NOAZHfIzybBQOSqWMe0mFTYjUYISpRH/3eOCJchD/YYOJKsNu 7oP8XR+EL73zQd57HgQFiHzxv+NU9I0UBcsy+hxVjHz8bXF7wMbO6XCHglB6pjwiRX/6 YZtZ0RtI5Ej6lNOAd/MG2/XgrNIC4SDHvTivLAg33v9zx+RIXQupJlb+u1hwZnkMANsM j87Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=SdCdR4V7+x8EyegxfyHSdQ35luujFqX5CRJaXdToD7o=; b=p0B0A1DlIRqHO102eAKdCVy8TQPzP8gAXZAqtA248/tlCu6UoZJZ0ohXawPgJX3eQI hEO8WoMwhYaDOnzo0sRztQT3rRKxefHQ8kkdlsgr6oFIV2ssDmWbLrLhiZhGlNExs0U5 jDMsQl7/TJ4q76r50dvkMIH5Ozxg5Z0UQq/EhHjaMaK3/Sc03LB1wptSgU1nqewXGOAi Q09OsrkBCJ8f05cyAA/bhqmKi1MlLUCV4tlzjJDvlOS0nmUnKzubPhVf0LFVt88sXwVU +srIAFmPFMs/+6khcNHxbN9YPiveSLYB2Wbr5rG4T8GfIBmINhrT3rYOE9wUWKo7dbbh d6uQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@jkqxz-net.20150623.gappssmtp.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 d41si11283236wrd.222.2017.06.12.15.44.20; Mon, 12 Jun 2017 15:44:20 -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; dkim=neutral (body hash did not verify) header.i=@jkqxz-net.20150623.gappssmtp.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 6B1C068A0CA; Tue, 13 Jun 2017 01:41:20 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr0-f172.google.com (mail-wr0-f172.google.com [209.85.128.172]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 766A268A075 for ; Tue, 13 Jun 2017 01:41:16 +0300 (EEST) Received: by mail-wr0-f172.google.com with SMTP id v104so113618464wrb.0 for ; Mon, 12 Jun 2017 15:41:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jkqxz-net.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references; bh=Toe4SqFh7aFY/FrISIEQ1WpSmEACVTgf8rW+U3vkI10=; b=ygcR5ivNj5oEnSG0d2ZLxFZ27ajM/K3v6vfGmvcBPIz1dcV7Hqg1aaOOEm1eAlaT+D gz5aMEN5PNAd+j4lwCSFqH2SHydHo/cD3GeG8fCulSaroGwunLJoEPRYTGzwHuRokZAB b8+kXz+RTfQvb/fDpeXnUSRWQt6v7ST6dlAhZW/9md8zV8v71qPexZuUfqBR0nvcv1QV ZFC/grDeS9LGD6VsOpNgsl4VpPyMSu35XuHpNDksYnEqYGuMwTR2N8QmAK4T5UA/dL+S aURlcdE7XFySx2w/Ue+6JBp7V+fTj8F4drKuF2EBqBwpfH7ZlE8k3WcaGTqCVmQqYZ70 ZE7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=Toe4SqFh7aFY/FrISIEQ1WpSmEACVTgf8rW+U3vkI10=; b=otB48YHIN78xdr+HWQUS+TSX11SLd62klY2g48Zqn/a9KKj6PeBsH00lzTO38a2V6C tDIt7fngkzcSqpjOUpdmvjZPi5k6glli5VeGmGX3FxUyb5Wx/JgLpYoq4utsjg+6pkHV lOzczD0RZSE+BZdupOBbdCwHWIkHHExG7Wmulj+6BWeiKKWUiuucvBtIT933rf4gLf16 9zP74cO44EuQ/SYGYhlK61ZImByB76USZUqk7ozW3z/UOIWvOpNxwIwWXDbDEGU/sA9R XfesZouvgQDy9pQlUw47acbEe1fVXhSshHCZaNFd1bdjzy5agebJDoFoDI7FeWH4I/q6 aa0A== X-Gm-Message-State: AKS2vOwsVOfsC1tEVB1qY4DQ4e4C1qZlrj6AKtuLISjQUB8cS7pfHS0D pqUU7qwKxiRBYKdv0XE= X-Received: by 10.223.130.199 with SMTP id 65mr719200wrc.150.1497307277835; Mon, 12 Jun 2017 15:41:17 -0700 (PDT) Received: from rywe.jkqxz.net (cpc91242-cmbg18-2-0-cust650.5-4.cable.virginm.net. [82.8.130.139]) by smtp.gmail.com with ESMTPSA id m191sm13098233wmg.30.2017.06.12.15.41.16 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Jun 2017 15:41:17 -0700 (PDT) From: Mark Thompson To: ffmpeg-devel@ffmpeg.org Date: Mon, 12 Jun 2017 23:40:39 +0100 Message-Id: <20170612224041.6750-23-sw@jkqxz.net> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170612224041.6750-1-sw@jkqxz.net> References: <20170612224041.6750-1-sw@jkqxz.net> Subject: [FFmpeg-devel] [PATCH 22/24] vf_hwmap: Add device derivation 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Also refactor a little and improve error messages to make failure cases easier to understand. (cherry picked from commit 38cb05f1c89cae1862b360d4e7e3f0cd2b5bbb67) --- libavfilter/vf_hwmap.c | 67 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 18 deletions(-) diff --git a/libavfilter/vf_hwmap.c b/libavfilter/vf_hwmap.c index 654477c6f2..c0fb42a1bc 100644 --- a/libavfilter/vf_hwmap.c +++ b/libavfilter/vf_hwmap.c @@ -30,10 +30,10 @@ typedef struct HWMapContext { const AVClass *class; - AVBufferRef *hwdevice_ref; AVBufferRef *hwframes_ref; int mode; + char *derive_device_type; int map_backwards; } HWMapContext; @@ -56,6 +56,7 @@ static int hwmap_config_output(AVFilterLink *outlink) HWMapContext *ctx = avctx->priv; AVFilterLink *inlink = avctx->inputs[0]; AVHWFramesContext *hwfc; + AVBufferRef *device; const AVPixFmtDescriptor *desc; int err; @@ -63,30 +64,58 @@ static int hwmap_config_output(AVFilterLink *outlink) av_get_pix_fmt_name(inlink->format), av_get_pix_fmt_name(outlink->format)); + av_buffer_unref(&ctx->hwframes_ref); + + device = avctx->hw_device_ctx; + if (inlink->hw_frames_ctx) { hwfc = (AVHWFramesContext*)inlink->hw_frames_ctx->data; + if (ctx->derive_device_type) { + enum AVHWDeviceType type; + + type = av_hwdevice_find_type_by_name(ctx->derive_device_type); + if (type == AV_HWDEVICE_TYPE_NONE) { + av_log(avctx, AV_LOG_ERROR, "Invalid device type.\n"); + goto fail; + } + + err = av_hwdevice_ctx_create_derived(&device, type, + hwfc->device_ref, 0); + if (err < 0) { + av_log(avctx, AV_LOG_ERROR, "Failed to created derived " + "device context: %d.\n", err); + goto fail; + } + } + desc = av_pix_fmt_desc_get(outlink->format); - if (!desc) - return AVERROR(EINVAL); + if (!desc) { + err = AVERROR(EINVAL); + goto fail; + } if (inlink->format == hwfc->format && (desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) { // Map between two hardware formats (including the case of // undoing an existing mapping). - ctx->hwdevice_ref = av_buffer_ref(avctx->hw_device_ctx); - if (!ctx->hwdevice_ref) { - err = AVERROR(ENOMEM); + if (!device) { + av_log(avctx, AV_LOG_ERROR, "A device reference is " + "required to map to a hardware format.\n"); + err = AVERROR(EINVAL); goto fail; } err = av_hwframe_ctx_create_derived(&ctx->hwframes_ref, outlink->format, - ctx->hwdevice_ref, + device, inlink->hw_frames_ctx, 0); - if (err < 0) + if (err < 0) { + av_log(avctx, AV_LOG_ERROR, "Failed to create derived " + "frames context: %d.\n", err); goto fail; + } } else if ((outlink->format == hwfc->format && inlink->format == hwfc->sw_format) || @@ -94,8 +123,6 @@ static int hwmap_config_output(AVFilterLink *outlink) // Map from a hardware format to a software format, or // undo an existing such mapping. - ctx->hwdevice_ref = NULL; - ctx->hwframes_ref = av_buffer_ref(inlink->hw_frames_ctx); if (!ctx->hwframes_ref) { err = AVERROR(ENOMEM); @@ -119,15 +146,17 @@ static int hwmap_config_output(AVFilterLink *outlink) // returns frames mapped from that to the previous link in // order to fill them without an additional copy. - ctx->map_backwards = 1; - - ctx->hwdevice_ref = av_buffer_ref(avctx->hw_device_ctx); - if (!ctx->hwdevice_ref) { - err = AVERROR(ENOMEM); + if (!device) { + av_log(avctx, AV_LOG_ERROR, "A device reference is " + "required to create new frames with backwards " + "mapping.\n"); + err = AVERROR(EINVAL); goto fail; } - ctx->hwframes_ref = av_hwframe_ctx_alloc(ctx->hwdevice_ref); + ctx->map_backwards = 1; + + ctx->hwframes_ref = av_hwframe_ctx_alloc(device); if (!ctx->hwframes_ref) { err = AVERROR(ENOMEM); goto fail; @@ -165,7 +194,6 @@ static int hwmap_config_output(AVFilterLink *outlink) fail: av_buffer_unref(&ctx->hwframes_ref); - av_buffer_unref(&ctx->hwdevice_ref); return err; } @@ -273,7 +301,6 @@ static av_cold void hwmap_uninit(AVFilterContext *avctx) HWMapContext *ctx = avctx->priv; av_buffer_unref(&ctx->hwframes_ref); - av_buffer_unref(&ctx->hwdevice_ref); } #define OFFSET(x) offsetof(HWMapContext, x) @@ -297,6 +324,10 @@ static const AVOption hwmap_options[] = { 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWFRAME_MAP_DIRECT }, INT_MIN, INT_MAX, FLAGS, "mode" }, + { "derive_device", "Derive a new device of this type", + OFFSET(derive_device_type), AV_OPT_TYPE_STRING, + { .str = NULL }, 0, 0, FLAGS }, + { NULL } };