From patchwork Wed Apr 11 22:56:25 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Kravchenko X-Patchwork-Id: 8410 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.1.70 with SMTP id c67csp1240611jad; Wed, 11 Apr 2018 15:56:40 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/siURuQhtV1k+FBSA72QZ4ArAOQfYlZe1OY4a9ij7LzrWYiQFMu0I5h4L01ep0cAFwOoRH X-Received: by 10.223.156.145 with SMTP id d17mr2144088wre.175.1523487400344; Wed, 11 Apr 2018 15:56:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523487400; cv=none; d=google.com; s=arc-20160816; b=fFpp7fmcuv2qIGf6fKAZgbopR5N1ZxqLUZp+S+1YJqNaCWZsepPnJvUmYxCxa1Kl/W fc3IoDxcwXjhE4v8vxbqSSCT+wOPZQdlkB6n6C3Cz2+2HTG91IztUNqw5aM77N5kIqgh beeZyn6AyiozLYw7bbuY2MdAQ5L7nTy4/Y3Fiy7MSZQKqEDdUqR+OAbOQs0g2kpNPBnD wZNDX+pmdyKr7f7TNQZRiZX1KfyKK6sAO3tsWIQZwPKdxeD89qPS/dBQlvLOnZCXM9VC VdeXDBj7eOsR2RjMZd3LPdl+8ul4tsiMg1vpTVaVZmaOarMMPRpkFJe7InJjQqw0w+2F N83g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject :content-language:thread-index:mime-version:message-id:date :in-reply-to:references:to:from:dkim-signature:delivered-to :arc-authentication-results; bh=3sjZWP8iiFo2efc1M1ySdnfiIkgZvGNfVexoWCBO1eo=; b=qoSm7JeQMcuRdh0XJucR0sXNGkqjQvfbYr75LU8vyzgNotC0CCvVlHsYsMi1/EdzPs 0MvtgjPBuWaxHc5cu759egIJMOLSX/9+sUqiSRJ9v+AMM7ivuA/Bb2ZmppmLVvXS1+SP 1yCt0h9Lc4rCNF5cBXg1c4ZFY4PjkIrWeAbS/sm24LHHoctigyyHSeELvkPkdoh36/nS 0QSOR1MEBy+EaYrwliCFLmZwKCrT1LPF/7Du0r4iZed41ZeheG5wNLdQ49xxg14JWHiN +YyXXdEqilM4amTmMRJ6uEg7DQlWErICymBeDzQKN1P4Zms22DBqH+Kqciaut7vV85v+ VHWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=IHw/vi8X; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id g58si1608114wrg.171.2018.04.11.15.56.39; Wed, 11 Apr 2018 15:56:40 -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=@gmail.com header.s=20161025 header.b=IHw/vi8X; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id AA72068A288; Thu, 12 Apr 2018 01:56:13 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lf0-f45.google.com (mail-lf0-f45.google.com [209.85.215.45]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 99A7A68A1A3 for ; Thu, 12 Apr 2018 01:56:07 +0300 (EEST) Received: by mail-lf0-f45.google.com with SMTP id m200-v6so4933629lfm.4 for ; Wed, 11 Apr 2018 15:56:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:references:in-reply-to:subject:date:message-id:mime-version :thread-index:content-language; bh=3oK2xDKRyUfVzTq8mp1t68KFGDpW2m4OWx1oirTP1MQ=; b=IHw/vi8XgryKf6Q+Kf9VgfgODr+2muNh6phsfD9nViEWSlGR6dWp45djjUiOwIwSJD zq5/c6/PkFwK5CfDl7Rr5++6kc8IRUiAy7fAMspKywzw9bW5ga28cklV+yZAOVxq0MYr rko20CsHfdUVns2K2N11csdAusIQFNJE4CV2lcWTlJ1r5umxorhJjcy0y96QXX29/DIG t/a3+gyKLzwsjICh9NkBPFLDEXXCnIumnVn3oeVicE0FB4zvtAFxV66a+SbiKo8m/0TL B9gnzHMOzHcp0lE1gTNWhbybxzGbjCmfICLNwhxm1nrSYTxlNPa8EmUFm2wIvopzmFLw Hi2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:references:in-reply-to:subject:date :message-id:mime-version:thread-index:content-language; bh=3oK2xDKRyUfVzTq8mp1t68KFGDpW2m4OWx1oirTP1MQ=; b=DsgzH8jEfmU74ZkRna5AAjygeVWgLx5RWszhj2i3K//SyPRYm07leuC5N6c2We5I8H Ig9iyXajjOdTFUNQNwj2bFzxPhMY72O3scng+SCjMG2cFcUFjc+kuvjAWRSwSN7TF/9v PaZBwhnkTV+lu/3fXSKGU3o2vXdEPeze74ZZKsQwl4ZrK6Sk2jTPM+Fr2ZAbYTpCYks8 a7VsuHuBhL8UDgYAoETDicNmZChD5ZGepPnKv8sO/YdfADwBaFDCYfidQhoQTvJigL2L +66cMT2BUDFnbTiLkuIcD/rRyiOucd188eCTxC4a4IYM57Myp3eSrpaIJ0QOBs2guzK4 prkg== X-Gm-Message-State: ALQs6tDyoV9PoXmvdjoZAQ/oCzZPhefFyla2g8U7vB5ofAEp3t09YBK4 xXtxl5b1hZuuqtAKRq1ZGdBWryG9 X-Received: by 10.46.101.16 with SMTP id z16mr4091577ljb.72.1523487390122; Wed, 11 Apr 2018 15:56:30 -0700 (PDT) Received: from KAANBWIN10 ([188.227.65.188]) by smtp.gmail.com with ESMTPSA id b202-v6sm50180lfg.16.2018.04.11.15.56.28 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 11 Apr 2018 15:56:28 -0700 (PDT) From: "Alexander Kravchenko" To: "'FFmpeg development discussions and patches'" References: <002301d3d1d9$d69c43f0$83d4cbd0$@gmail.com> <002601d3d1dc$8fe3fd10$afabf730$@gmail.com> In-Reply-To: <002601d3d1dc$8fe3fd10$afabf730$@gmail.com> Date: Thu, 12 Apr 2018 01:56:25 +0300 Message-ID: <000101d3d1e8$5213e610$f63bb230$@gmail.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 15.0 Thread-Index: AQIg9uKJxOWomL2jsrzvcMw0XwuIdAKWiIfWo00tyyA= Content-Language: en-us Subject: Re: [FFmpeg-devel] [PATCH] lavc/amfenc: device type AV_HWDEVICE_TYPE_DXVA2 support 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" I am sorry, sending patch one more time. Outlook was wrapping text. Sending patch in message body and in attachment --- libavcodec/amfenc.c | 123 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 102 insertions(+), 21 deletions(-) diff --git a/libavcodec/amfenc.c b/libavcodec/amfenc.c index b9418b6791..c1f65f909d 100644 --- a/libavcodec/amfenc.c +++ b/libavcodec/amfenc.c @@ -24,6 +24,9 @@ #if CONFIG_D3D11VA #include "libavutil/hwcontext_d3d11va.h" #endif +#if CONFIG_DXVA2 +#include "libavutil/hwcontext_dxva2.h" +#endif #include "libavutil/mem.h" #include "libavutil/pixdesc.h" #include "libavutil/time.h" @@ -50,6 +53,9 @@ const enum AVPixelFormat ff_amf_pix_fmts[] = { AV_PIX_FMT_YUV420P, #if CONFIG_D3D11VA AV_PIX_FMT_D3D11, +#endif +#if CONFIG_DXVA2 + AV_PIX_FMT_DXVA2_VLD, #endif AV_PIX_FMT_NONE }; @@ -69,6 +75,7 @@ static const FormatMap format_map[] = { AV_PIX_FMT_YUV420P, AMF_SURFACE_YUV420P }, { AV_PIX_FMT_YUYV422, AMF_SURFACE_YUY2 }, { AV_PIX_FMT_D3D11, AMF_SURFACE_NV12 }, + { AV_PIX_FMT_DXVA2_VLD, AMF_SURFACE_NV12 }, }; @@ -152,6 +159,23 @@ static int amf_load_library(AVCodecContext *avctx) return 0; } +static void get_dx9_device_from_devmgr(IDirect3DDeviceManager9 *devmgr, IDirect3DDevice9 **device, void *avcl) +{ + HRESULT hr; + HANDLE device_handle; + + if (SUCCEEDED(devmgr->lpVtbl->OpenDeviceHandle(devmgr, &device_handle))) { + if (SUCCEEDED(devmgr->lpVtbl->LockDevice(devmgr, device_handle, device, FALSE))) { + devmgr->lpVtbl->UnlockDevice(devmgr, device_handle, FALSE); + } else { + av_log(avcl, AV_LOG_INFO, "Failed to lock device handle for Direct3D9 device: %lx.\n", (unsigned long)hr); + } + devmgr->lpVtbl->CloseDeviceHandle(devmgr, device_handle); + } else { + av_log(avcl, AV_LOG_INFO, "Failed to open device handle for Direct3D9 device: %lx.\n", (unsigned long)hr); + } +} + static int amf_init_context(AVCodecContext *avctx) { AmfContext *ctx = avctx->priv_data; @@ -177,34 +201,58 @@ static int amf_init_context(AVCodecContext *avctx) res = ctx->factory->pVtbl->CreateContext(ctx->factory, &ctx->context); AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "CreateContext() failed with error %d\n", res); // try to reuse existing DX device -#if CONFIG_D3D11VA if (avctx->hw_frames_ctx) { - AVHWFramesContext *device_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data; - if (device_ctx->device_ctx->type == AV_HWDEVICE_TYPE_D3D11VA) { - if (amf_av_to_amf_format(device_ctx->sw_format) != AMF_SURFACE_UNKNOWN) { - if (device_ctx->device_ctx->hwctx) { - AVD3D11VADeviceContext *device_d3d11 = (AVD3D11VADeviceContext *)device_ctx->device_ctx->hwctx; + AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data; + if (amf_av_to_amf_format(frames_ctx->sw_format) != AMF_SURFACE_UNKNOWN) { + if (frames_ctx->device_ctx->hwctx) { +#if CONFIG_D3D11VA + if (frames_ctx->device_ctx->type == AV_HWDEVICE_TYPE_D3D11VA) { + AVD3D11VADeviceContext *device_d3d11 = (AVD3D11VADeviceContext *)frames_ctx->device_ctx->hwctx; res = ctx->context->pVtbl->InitDX11(ctx->context, device_d3d11->device, AMF_DX11_1); if (res == AMF_OK) { ctx->hw_frames_ctx = av_buffer_ref(avctx->hw_frames_ctx); if (!ctx->hw_frames_ctx) { return AVERROR(ENOMEM); } - if (device_ctx->initial_pool_size > 0) - ctx->hwsurfaces_in_queue_max = device_ctx->initial_pool_size - 1; + if (frames_ctx->initial_pool_size > 0) + ctx->hwsurfaces_in_queue_max = frames_ctx->initial_pool_size - 1; } else { - if(res == AMF_NOT_SUPPORTED) + if (res == AMF_NOT_SUPPORTED) av_log(avctx, AV_LOG_INFO, "avctx->hw_frames_ctx has D3D11 device which doesn't have D3D11VA interface, switching to default\n"); else av_log(avctx, AV_LOG_INFO, "avctx->hw_frames_ctx has non-AMD device, switching to default\n"); } } - } else { - av_log(avctx, AV_LOG_INFO, "avctx->hw_frames_ctx has format not uspported by AMF, switching to default\n"); +#endif +#if CONFIG_DXVA2 + if (frames_ctx->device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) { + AVDXVA2DeviceContext *device_dxva2 = (AVDXVA2DeviceContext *)frames_ctx->device_ctx->hwctx; + IDirect3DDevice9 *device_dx9 = NULL; + get_dx9_device_from_devmgr(device_dxva2->devmgr, &device_dx9, avctx); + res = ctx->context->pVtbl->InitDX9(ctx->context, device_dx9); + device_dx9->lpVtbl->Release(device_dx9); + if (res == AMF_OK) { + ctx->hw_frames_ctx = av_buffer_ref(avctx->hw_frames_ctx); + if (!ctx->hw_frames_ctx) { + return AVERROR(ENOMEM); + } + if (frames_ctx->initial_pool_size > 0) + ctx->hwsurfaces_in_queue_max = frames_ctx->initial_pool_size - 1; + } else { + if (res == AMF_NOT_SUPPORTED) + av_log(avctx, AV_LOG_INFO, "avctx->hw_frames_ctx has D3D device which doesn't have DXVA2 interface, switching to default\n"); + else + av_log(avctx, AV_LOG_INFO, "avctx->hw_frames_ctx has non-AMD device, switching to default\n"); + } + } +#endif } + } else { + av_log(avctx, AV_LOG_INFO, "avctx->hw_frames_ctx has format not uspported by AMF, switching to default\n"); } } else if (avctx->hw_device_ctx) { AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)(avctx->hw_device_ctx->data); +#if CONFIG_D3D11VA if (device_ctx->type == AV_HWDEVICE_TYPE_D3D11VA) { if (device_ctx->hwctx) { AVD3D11VADeviceContext *device_d3d11 = (AVD3D11VADeviceContext *)device_ctx->hwctx; @@ -222,8 +270,28 @@ static int amf_init_context(AVCodecContext *avctx) } } } - } +#endif +#if CONFIG_DXVA2 + if (device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) { + AVDXVA2DeviceContext *device_dxva2 = (AVDXVA2DeviceContext *)device_ctx->hwctx; + IDirect3DDevice9 *device_dx9 = NULL; + get_dx9_device_from_devmgr(device_dxva2->devmgr, &device_dx9, avctx); + res = ctx->context->pVtbl->InitDX9(ctx->context, device_dx9); + device_dx9->lpVtbl->Release(device_dx9); + if (res == AMF_OK) { + ctx->hw_device_ctx = av_buffer_ref(avctx->hw_device_ctx); + if (!ctx->hw_device_ctx) { + return AVERROR(ENOMEM); + } + } else { + if (res == AMF_NOT_SUPPORTED) + av_log(avctx, AV_LOG_INFO, "avctx->hw_device_ctx has D3D device which doesn't have DXVA2 interface, switching to default\n"); + else + av_log(avctx, AV_LOG_INFO, "avctx->hw_device_ctx has non-AMD device, switching to default\n"); + } + } #endif + } if (!ctx->hw_frames_ctx && !ctx->hw_device_ctx) { res = ctx->context->pVtbl->InitDX11(ctx->context, NULL, AMF_DX11_1); if (res != AMF_OK) { @@ -559,18 +627,31 @@ int ff_amf_send_frame(AVCodecContext *avctx, const AVFrame *frame) (AVHWDeviceContext*)ctx->hw_device_ctx->data) )) { AMFBuffer *frame_ref_storage_buffer; - #if CONFIG_D3D11VA - static const GUID AMFTextureArrayIndexGUID = { 0x28115527, 0xe7c3, 0x4b66, { 0x99, 0xd3, 0x4f, 0x2a, 0xe6, 0xb4, 0x7f, 0xaf } }; - ID3D11Texture2D *texture = (ID3D11Texture2D*)frame->data[0]; // actual texture - int index = (int)(size_t)frame->data[1]; // index is a slice in texture array is - set to tell AMF which slice to use - texture->lpVtbl->SetPrivateData(texture, &AMFTextureArrayIndexGUID, sizeof(index), &index); + if (((AVHWFramesContext*)frame->hw_frames_ctx->data)->device_ctx->type == AV_HWDEVICE_TYPE_D3D11VA) { + static const GUID AMFTextureArrayIndexGUID = { 0x28115527, 0xe7c3, 0x4b66, { 0x99, 0xd3, 0x4f, 0x2a, 0xe6, 0xb4, 0x7f, 0xaf } }; + + ID3D11Texture2D *texture = (ID3D11Texture2D*)frame->data[0]; // actual texture + int index = (int)(size_t)frame->data[1]; // index is a slice in texture array is - set to tell AMF which slice to use + texture->lpVtbl->SetPrivateData(texture, &AMFTextureArrayIndexGUID, sizeof(index), &index); - res = ctx->context->pVtbl->CreateSurfaceFromDX11Native(ctx->context, texture, &surface, NULL); // wrap to AMF surface - AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "CreateSurfaceFromDX11Native() failed with error %d\n", res); + res = ctx->context->pVtbl->CreateSurfaceFromDX11Native(ctx->context, texture, &surface, NULL); // wrap to AMF surface + AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "CreateSurfaceFromDX11Native() failed with error %d\n", res); - // input HW surfaces can be vertically aligned by 16; tell AMF the real size - surface->pVtbl->SetCrop(surface, 0, 0, frame->width, frame->height); + // input HW surfaces can be vertically aligned by 16; tell AMF the real size + surface->pVtbl->SetCrop(surface, 0, 0, frame->width, frame->height); + } +#endif +#if CONFIG_DXVA2 + if (((AVHWFramesContext*)frame->hw_frames_ctx->data)->device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) { + IDirect3DSurface9 *texture = (IDirect3DSurface9*)frame->data[3]; // actual texture + + res = ctx->context->pVtbl->CreateSurfaceFromDX9Native(ctx->context, texture, &surface, NULL); // wrap to AMF surface + AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), "CreateSurfaceFromDX9Native() failed with error %d\n", res); + + // input HW surfaces can be vertically aligned by 16; tell AMF the real size + surface->pVtbl->SetCrop(surface, 0, 0, frame->width, frame->height); + } #endif frame_ref_storage_buffer = amf_create_buffer_with_frame_ref(frame, ctx->context);