From patchwork Wed Jan 4 13:52:42 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steve Lhomme X-Patchwork-Id: 2047 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp4839726vsb; Wed, 4 Jan 2017 05:53:49 -0800 (PST) X-Received: by 10.28.25.134 with SMTP id 128mr53792542wmz.37.1483538029489; Wed, 04 Jan 2017 05:53:49 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id jv9si49331247wjc.11.2017.01.04.05.53.49; Wed, 04 Jan 2017 05:53:49 -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; dkim=neutral (body hash did not verify) header.i=@gmail.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; dmarc=fail (p=NONE 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 72DB3689C44; Wed, 4 Jan 2017 15:53:15 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f67.google.com (mail-wm0-f67.google.com [74.125.82.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 34F97689AFC for ; Wed, 4 Jan 2017 15:53:09 +0200 (EET) Received: by mail-wm0-f67.google.com with SMTP id l2so66221528wml.2 for ; Wed, 04 Jan 2017 05:53:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=WYt+U9lz0LZ4SojfAtbvZBsZk37RPwXD1/nasGNNyTQ=; b=Kq+Wb08MtLp5CSQyV2LOo46pU0UvwXczFmqCHVPDtbywaI2ZfozscEavuXRR5/060E Y3eXZf+zyDOKiG/RULDAYqQWq97zLXAnxbiLr3WtE5xbEa6BdDzQBtZ4JhQ3oYbx1reO DT2CNzjgeGfrj/UQL1Inb6FLGWANLwl7Rv5IyUVKk6EBZSeCmzOUpsnrnN1heuxNlIMT Hf9qlSEM1JlI1em26AoI9VCkmZcq/xeFqDytmrU3PX2i9houpAJZqBq4wRX+YNi3upQf tpoLCgo86BKAWlJKh+NzIilkrY5u6AscYimiZOyMClTkqtYG0Uawo7on9MzlTMeE4PDB FcYA== 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=WYt+U9lz0LZ4SojfAtbvZBsZk37RPwXD1/nasGNNyTQ=; b=DkTdSrhOByPWz4zGTh0VzFrBlgkXdQqr87by8kdOTlO/znw1kbaFCjCVve558MavX6 mYmY3Qu7VNinnperd5fxKdCqECPMvgnlzz/2EpIxxhbNn0598aZb7iqSLAc2Sz8uodY0 exm7PeooPTiFloTXsBxsU8koNs+uKxNCM5xKya1PvmS1Iuf9HrGecWaXO2hj+Pbt6+jf AlW9f5jEOW2ezddVQs1YpFL7MrRuuS1mUMz0uCjTnjRytERf3u6PS/aBW1pgI2oV1G6O yPwoilBkjmCsMJPjbUvzmtJw9Yniw5piBwkBfqh2IFMZwGoEV30MndKbs3872cNtGF4x uXCA== X-Gm-Message-State: AIkVDXLwyx0n+mTOFHEuaTNfk/U1XSjR7gEXGW0tGG6Xf7y7ffjVP91hDmFseJhuKMgQSA== X-Received: by 10.28.236.83 with SMTP id k80mr54487622wmh.0.1483537993234; Wed, 04 Jan 2017 05:53:13 -0800 (PST) Received: from localhost.localdomain (2a01cb0c000ea9005c35e70f116a1c8d.ipv6.abo.wanadoo.fr. [2a01:cb0c:e:a900:5c35:e70f:116a:1c8d]) by smtp.gmail.com with ESMTPSA id w18sm95247785wme.9.2017.01.04.05.53.11 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 04 Jan 2017 05:53:11 -0800 (PST) From: Steve Lhomme To: ffmpeg-devel@ffmpeg.org Date: Wed, 4 Jan 2017 14:52:42 +0100 Message-Id: <20170104135243.13512-6-robux4@gmail.com> X-Mailer: git-send-email 2.10.2 In-Reply-To: <20170104135243.13512-1-robux4@gmail.com> References: <20170104135243.13512-1-robux4@gmail.com> Subject: [FFmpeg-devel] [PATCH 5/6] ffmpeg: add ffmpeg_d3d11va 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" The code is similar to ffmpeg_dxva2. The decoded output needs to be copied into a staging texture that can be accessed by the CPU as the decoder texture can't be accessed by the CPU. --- Changelog | 1 + Makefile | 1 + configure | 14 ++++ ffmpeg.h | 2 + ffmpeg_d3d11va.c | 212 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ ffmpeg_opt.c | 3 + 6 files changed, 233 insertions(+) create mode 100644 ffmpeg_d3d11va.c diff --git a/Changelog b/Changelog index aff9ab0..bd752ea 100644 --- a/Changelog +++ b/Changelog @@ -12,6 +12,7 @@ version : - 16.8 floating point pcm decoder - 24.0 floating point pcm decoder - Apple Pixlet decoder +- support for decoding through D3D11VA in ffmpeg version 3.2: - libopenmpt demuxer diff --git a/Makefile b/Makefile index 25e1d54..400bc98 100644 --- a/Makefile +++ b/Makefile @@ -37,6 +37,7 @@ ifndef CONFIG_VIDEOTOOLBOX OBJS-ffmpeg-$(CONFIG_VDA) += ffmpeg_videotoolbox.o endif OBJS-ffmpeg-$(CONFIG_CUVID) += ffmpeg_cuvid.o +OBJS-ffmpeg-$(HAVE_D3D11VA_LIB) += ffmpeg_d3d11va.o ffmpeg_guid.o OBJS-ffmpeg-$(HAVE_DXVA2_LIB) += ffmpeg_dxva2.o ffmpeg_guid.o OBJS-ffmpeg-$(HAVE_VDPAU_X11) += ffmpeg_vdpau.o OBJS-ffserver += ffserver_config.o diff --git a/configure b/configure index def530a..9fd1d9e 100755 --- a/configure +++ b/configure @@ -2052,6 +2052,8 @@ HAVE_LIST=" $TYPES_LIST atomics_native dos_paths + d3d11va_lib + d3d11vaapi_cobj dxva2_lib dxva2api_cobj libc_msvcrt @@ -6039,6 +6041,15 @@ enabled dxva2api_h && int main(void) { IDirectXVideoDecoder *o = NULL; IDirectXVideoDecoder_Release(o); return 0; } EOF +enabled d3d11_h && + check_cc < +#include +int main(void) { ID3D11VideoDevice *o = NULL; ID3D11VideoDevice_Release(o); return 0; } +EOF + enabled vaapi && check_lib va/va.h vaInitialize -lva || disable vaapi @@ -6370,6 +6381,9 @@ if test $target_os = "haiku"; then disable posix_memalign fi +enabled_all d3d11va d3d11vaapi_cobj && + enable d3d11va_lib + enabled_all dxva2 dxva2api_cobj CoTaskMemFree && prepend ffmpeg_libs $($ldflags_filter "-lole32") && enable dxva2_lib diff --git a/ffmpeg.h b/ffmpeg.h index ebe5bf0..5faf545 100644 --- a/ffmpeg.h +++ b/ffmpeg.h @@ -67,6 +67,7 @@ enum HWAccelID { HWACCEL_QSV, HWACCEL_VAAPI, HWACCEL_CUVID, + HWACCEL_D3D11VA, }; typedef struct HWAccel { @@ -633,6 +634,7 @@ int ifilter_parameters_from_decoder(InputFilter *ifilter, const AVCodecContext * int ffmpeg_parse_options(int argc, char **argv); int vdpau_init(AVCodecContext *s); +int d3d11va_init(AVCodecContext *s); int dxva2_init(AVCodecContext *s); int vda_init(AVCodecContext *s); int videotoolbox_init(AVCodecContext *s); diff --git a/ffmpeg_d3d11va.c b/ffmpeg_d3d11va.c new file mode 100644 index 0000000..b8ca9d8 --- /dev/null +++ b/ffmpeg_d3d11va.c @@ -0,0 +1,212 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +#if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600 +#undef _WIN32_WINNT +#define _WIN32_WINNT 0x0600 +#endif +#define COBJMACROS + +#include + +#include + +#include "ffmpeg.h" + +#include "libavcodec/d3d11va.h" + +#include "libavutil/avassert.h" +#include "libavutil/buffer.h" +#include "libavutil/frame.h" +#include "libavutil/imgutils.h" +#include "libavutil/pixfmt.h" + +#include "libavutil/hwcontext.h" +#include "libavutil/hwcontext_d3d11va.h" + +typedef struct D3D11VAContext { + D3D11_VIDEO_DECODER_CONFIG decoder_config; + + AVFrame *tmp_frame; + + AVBufferRef *hw_device_ctx; + AVBufferRef *hw_frames_ctx; +} D3D11VAContext; + +typedef D3D11_VIDEO_DECODER_CONFIG DXVA_DECODER_CONFIG; +typedef DXGI_FORMAT DXVA_SURFACE_FORMAT; +typedef D3D11VAContext DXVA_CONTEXT; +typedef AVD3D11VAContext DXVA_AV_CONTEXT; +typedef ID3D11VideoDevice *DXVA_DECODER_SERVICE; +#include "ffmpeg_dxva_template.c" + +static int d3d11va_get_decoder_configuration(AVCodecContext *s, + const D3D11_VIDEO_DECODER_DESC *desc, + D3D11_VIDEO_DECODER_CONFIG *config) +{ + InputStream *ist = s->opaque; + int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR; + unsigned cfg_count = 0; + DXVA_DECODER_CONFIG *cfg_list = NULL; + HRESULT hr; + int i, ret; + + DXVA_CONTEXT *ctx = ist->hwaccel_ctx; + AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)ctx->hw_device_ctx->data; + AVD3D11VADeviceContext *device_hwctx = device_ctx->hwctx; + + hr = ID3D11VideoDevice_GetVideoDecoderConfigCount(device_hwctx->video_device, desc, &cfg_count); + if (FAILED(hr)) { + av_log(NULL, loglevel, "Unable to retrieve decoder configurations\n"); + return AVERROR(EINVAL); + } + + cfg_list = av_malloc(cfg_count * sizeof(DXVA_DECODER_CONFIG)); + if (cfg_list == NULL) + return AVERROR(ENOMEM); + for (i = 0; i < cfg_count; i++) { + hr = ID3D11VideoDevice_GetVideoDecoderConfig(device_hwctx->video_device, desc, i, &cfg_list[i]); + if (FAILED(hr)) { + av_log(NULL, loglevel, "Unable to retrieve decoder configurations. (hr=0x%lX)\n", hr); + free(cfg_list); + return AVERROR(EINVAL); + } + } + + ret = dxva_get_decoder_configuration(s, cfg_list, cfg_count, config); + av_free(cfg_list); + return ret; +} + +static int d3d11va_validate_output(DXVA_DECODER_SERVICE service, GUID guid, DXVA_SURFACE_FORMAT surface_format) +{ + HRESULT hr; + BOOL is_supported = FALSE; + hr = ID3D11VideoDevice_CheckVideoDecoderFormat(service, &guid, surface_format, &is_supported); + return SUCCEEDED(hr) && is_supported; +} + +static int d3d11va_create_decoder(AVCodecContext *s) +{ + InputStream *ist = s->opaque; + int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR; + DXVA_CONTEXT *ctx = ist->hwaccel_ctx; + DXVA_AV_CONTEXT *dxva_ctx = s->hwaccel_context; + GUID *guid_list; + unsigned guid_count, i; + GUID decoder_guid; + const DXGI_FORMAT surface_format = (s->sw_pix_fmt == AV_PIX_FMT_YUV420P10) ? DXGI_FORMAT_P010 : DXGI_FORMAT_NV12; + D3D11_VIDEO_DECODER_DESC desc = { 0 }; + D3D11_VIDEO_DECODER_CONFIG config; + HRESULT hr; + int ret; + + AVD3D11VAFramesContext *frames_hwctx; + AVHWFramesContext *frames_ctx; + + AVHWDeviceContext *device_ctx; + AVD3D11VADeviceContext *device_hwctx; + device_ctx = (AVHWDeviceContext*)ctx->hw_device_ctx->data; + device_hwctx = device_ctx->hwctx; + + guid_count = ID3D11VideoDevice_GetVideoDecoderProfileCount(device_hwctx->video_device); + guid_list = av_malloc(sizeof(*guid_list) * guid_count); + if (guid_list==NULL) { + av_log(NULL, loglevel, "Failed to get the decoder GUIDs\n"); + goto fail; + } + for (i=0; ivideo_device, i, &guid_list[i]); + if (FAILED(hr)) { + av_log(NULL, loglevel, "Failed to retrieve decoder GUID %d\n", i); + av_free(guid_list); + goto fail; + } + } + + ret = dxva_get_decoder_guid(s, device_hwctx->video_device, surface_format, d3d11va_validate_output, + guid_count, guid_list, &decoder_guid); + av_free(guid_list); + if (ret < 0) { + goto fail; + } + + desc.SampleWidth = s->coded_width; + desc.SampleHeight = s->coded_height; + desc.OutputFormat = surface_format; + desc.Guid = decoder_guid; + + ret = d3d11va_get_decoder_configuration(s, &desc, &config); + if (ret < 0) { + goto fail; + } + + ctx->hw_frames_ctx = av_hwframe_ctx_alloc(ctx->hw_device_ctx); + if (!ctx->hw_frames_ctx) + goto fail; + frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data; + frames_hwctx = frames_ctx->hwctx; + frames_ctx->format = AV_PIX_FMT_D3D11VA_VLD; + + dxva_adjust_decoder(s); + + hr = ID3D11VideoDevice_CreateVideoDecoder(device_hwctx->video_device, &desc, + &config, &frames_hwctx->video_decoder); + if (FAILED(hr)) { + av_log(NULL, loglevel, "Failed to create D3D11VA video decoder\n"); + goto fail; + } + + ret = av_hwframe_ctx_init(ctx->hw_frames_ctx); + if (ret < 0) { + av_log(NULL, loglevel, "Failed to initialize the HW frames context\n"); + goto fail; + } + + ctx->decoder_config = config; + + dxva_ctx->cfg = &ctx->decoder_config; + dxva_ctx->surface = frames_hwctx->surfaces; + dxva_ctx->surface_count = frames_hwctx->nb_surfaces; + dxva_ctx->decoder = frames_hwctx->video_decoder; + dxva_ctx->video_context = device_hwctx->video_context; + dxva_ctx->context_mutex = device_hwctx->dev_ctx_mutex; + + return 0; +fail: + if (frames_hwctx->video_decoder) + ID3D11VideoDecoder_Release(frames_hwctx->video_decoder); + av_buffer_unref(&ctx->hw_frames_ctx); + return AVERROR(EINVAL); +} + +int d3d11va_init(AVCodecContext *s) +{ + InputStream *ist = s->opaque; + int ret; + + if (!ist->hwaccel_ctx) { + ret = dxva_alloc(s, AV_HWDEVICE_TYPE_D3D11VA); + if (ret < 0) + return ret; + } + + return dxva_init(s, d3d11va_create_decoder); +} diff --git a/ffmpeg_opt.c b/ffmpeg_opt.c index 6862456..9cc1a55 100644 --- a/ffmpeg_opt.c +++ b/ffmpeg_opt.c @@ -69,6 +69,9 @@ const HWAccel hwaccels[] = { #if HAVE_VDPAU_X11 { "vdpau", vdpau_init, HWACCEL_VDPAU, AV_PIX_FMT_VDPAU }, #endif +#if HAVE_D3D11VA_LIB + { "d3d11va", d3d11va_init, HWACCEL_D3D11VA, AV_PIX_FMT_D3D11VA_VLD }, +#endif #if HAVE_DXVA2_LIB { "dxva2", dxva2_init, HWACCEL_DXVA2, AV_PIX_FMT_DXVA2_VLD }, #endif