[vlc-devel] [PATCH 7/7] video_chroma: add a filter to convert from CPU to a D3D9 surface
Steve Lhomme
robux4 at videolabs.io
Thu May 11 09:36:09 CEST 2017
---
modules/MODULES_LIST | 1 +
modules/video_chroma/Makefile.am | 5 +-
modules/video_chroma/d3d9_from_cpu.c | 261 +++++++++++++++++++++++++++++++++++
3 files changed, 266 insertions(+), 1 deletion(-)
create mode 100644 modules/video_chroma/d3d9_from_cpu.c
diff --git a/modules/MODULES_LIST b/modules/MODULES_LIST
index 9c3217bc9d..9cb9a274cb 100644
--- a/modules/MODULES_LIST
+++ b/modules/MODULES_LIST
@@ -86,6 +86,7 @@ $Id$
* crystalhd: crystalhd decoder
* cvdsub: CVD subtitles decoder
* cvpx_i420: filter to copy from OS X accelerated surfaces to CPU in YUV
+ * d3d9_out: Convert YUV planes to D3D9 GPU textures
* d3d11_out: Convert YUV planes to D3D11 GPU textures
* d3d11_surface: Convert D3D11 GPU textures to YUV planes
* d3d11va: Direct3D11 hardware-accelerated decoding
diff --git a/modules/video_chroma/Makefile.am b/modules/video_chroma/Makefile.am
index 956fb6dce6..09662c0e2e 100644
--- a/modules/video_chroma/Makefile.am
+++ b/modules/video_chroma/Makefile.am
@@ -116,10 +116,13 @@ endif
# DXVA2
libdxa9_plugin_la_SOURCES = video_chroma/dxa9.c \
video_chroma/d3d9_fmt.h video_chroma/copy.c video_chroma/copy.h
+libd3d9_out_plugin_la_SOURCES = video_chroma/d3d9_from_cpu.c \
+ video_chroma/d3d9_fmt.h video_chroma/copy.c video_chroma/copy.h
if HAVE_AVCODEC_DXVA2
chroma_LTLIBRARIES += \
- libdxa9_plugin.la
+ libdxa9_plugin.la \
+ libd3d9_out_plugin.la
endif
# D3D11VA
diff --git a/modules/video_chroma/d3d9_from_cpu.c b/modules/video_chroma/d3d9_from_cpu.c
new file mode 100644
index 0000000000..cc1390cfb8
--- /dev/null
+++ b/modules/video_chroma/d3d9_from_cpu.c
@@ -0,0 +1,261 @@
+/*****************************************************************************
+ * dxva_from_cpu.c : D3D9 CPU to GPU surface conversion module for vlc
+ *****************************************************************************
+ * Copyright (C) 2017 VLC authors, VideoLAN and VideoLabs
+ *
+ * Authors: Steve Lhomme <robux4 at videolabs.io>
+ *
+ * This program 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.
+ *
+ * This program 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Preamble
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <assert.h>
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_filter.h>
+#include <vlc_picture.h>
+#include <vlc_modules.h>
+
+#include <windows.h>
+#define COBJMACROS
+#include <d3d9.h>
+#include "d3d9_fmt.h"
+
+struct filter_sys_t {
+ IDirect3DDevice9 *d3ddev;
+ filter_t *filter;
+ picture_t *staging;
+};
+
+static void DestroyPicture(picture_t *picture)
+{
+ picture_sys_t *p_sys = picture->p_sys;
+ ReleasePictureSys( p_sys );
+ free(p_sys);
+ free(picture);
+}
+
+static void DeleteFilter( filter_t * p_filter )
+{
+ if( p_filter->p_module )
+ module_unneed( p_filter, p_filter->p_module );
+
+ es_format_Clean( &p_filter->fmt_in );
+ es_format_Clean( &p_filter->fmt_out );
+
+ vlc_object_release( p_filter );
+}
+
+static picture_t *NewBuffer(filter_t *p_filter)
+{
+ filter_t *p_parent = p_filter->owner.sys;
+ return p_parent->p_sys->staging;
+}
+
+static filter_t *CreateFilter( vlc_object_t *p_this, const es_format_t *p_fmt_in,
+ vlc_fourcc_t dst_chroma )
+{
+ filter_t *p_filter;
+
+ p_filter = vlc_object_create( p_this, sizeof(filter_t) );
+ if (unlikely(p_filter == NULL))
+ return NULL;
+
+ p_filter->b_allow_fmt_out_change = false;
+ p_filter->owner.video.buffer_new = NewBuffer;
+ p_filter->owner.sys = p_this;
+
+ es_format_InitFromVideo( &p_filter->fmt_in, &p_fmt_in->video );
+ es_format_InitFromVideo( &p_filter->fmt_out, &p_fmt_in->video );
+ p_filter->fmt_out.i_codec = p_filter->fmt_out.video.i_chroma = dst_chroma;
+ p_filter->p_module = module_need( p_filter, "video converter", NULL, false );
+
+ if( !p_filter->p_module )
+ {
+ msg_Dbg( p_filter, "no video converter found" );
+ DeleteFilter( p_filter );
+ return NULL;
+ }
+
+ return p_filter;
+}
+
+static void D3D9_YV12(filter_t *p_filter, picture_t *src, picture_t *dst)
+{
+ filter_sys_t *sys = (filter_sys_t*) p_filter->p_sys;
+ picture_sys_t *p_sys = dst->p_sys;
+
+ D3DSURFACE_DESC texDesc;
+ IDirect3DSurface9_GetDesc( p_sys->surface, &texDesc);
+
+ D3DLOCKED_RECT d3drect;
+ HRESULT hr = IDirect3DSurface9_LockRect(sys->staging->p_sys->surface, &d3drect, NULL, 0);
+ if (FAILED(hr))
+ return;
+
+ picture_UpdatePlanes(sys->staging, d3drect.pBits, d3drect.Pitch, true);
+
+ picture_Hold( src );
+ sys->filter->pf_video_filter(sys->filter, src);
+
+ IDirect3DSurface9_UnlockRect(sys->staging->p_sys->surface);
+
+ RECT visibleSource = {
+ .right = dst->format.i_width, .bottom = dst->format.i_height,
+ };
+ IDirect3DDevice9_StretchRect( sys->d3ddev,
+ sys->staging->p_sys->surface, &visibleSource,
+ dst->p_sys->surface, &visibleSource,
+ D3DTEXF_NONE );
+}
+
+VIDEO_FILTER_WRAPPER (D3D9_YV12)
+
+static int OpenConverter( vlc_object_t *obj )
+{
+ filter_t *p_filter = (filter_t *)obj;
+ int err = VLC_EGENERIC;
+ LPDIRECT3DSURFACE9 texture = NULL;
+ IDirect3DDevice9 *d3ddev = NULL;
+ filter_t *p_cpu_filter = NULL;
+ picture_t *p_dst = NULL;
+ video_format_t fmt_staging;
+
+ if ( p_filter->fmt_out.video.i_chroma != VLC_CODEC_D3D9_OPAQUE )
+ return VLC_EGENERIC;
+
+ if ( p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height
+ || p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width )
+ return VLC_EGENERIC;
+
+ switch( p_filter->fmt_in.video.i_chroma ) {
+ case VLC_CODEC_I420:
+ case VLC_CODEC_YV12:
+ p_filter->pf_video_filter = D3D9_YV12_Filter;
+ break;
+ default:
+ return VLC_EGENERIC;
+ }
+
+ picture_t *peek = filter_NewPicture(p_filter);
+ if (peek == NULL)
+ return VLC_EGENERIC;
+ assert(peek->p_sys != NULL);
+
+ video_format_Init(&fmt_staging, 0);
+ D3DSURFACE_DESC texDesc;
+ IDirect3DSurface9_GetDesc( peek->p_sys->surface, &texDesc);
+ vlc_fourcc_t d3d_fourcc = texDesc.Format;
+ if (d3d_fourcc == 0)
+ goto done;
+
+ if ( p_filter->fmt_in.video.i_chroma != d3d_fourcc )
+ {
+ picture_resource_t res;
+ res.pf_destroy = DestroyPicture;
+ res.p_sys = calloc(1, sizeof(picture_sys_t));
+ if (res.p_sys == NULL) {
+ err = VLC_ENOMEM;
+ goto done;
+ }
+ //res.p_sys->context = peek->p_sys->context;
+ //res.p_sys->formatTexture = texDesc.Format;
+
+ video_format_Copy(&fmt_staging, &p_filter->fmt_out.video);
+ fmt_staging.i_chroma = d3d_fourcc;
+ fmt_staging.i_height = texDesc.Height;
+ fmt_staging.i_width = texDesc.Width;
+
+ p_dst = picture_NewFromResource(&fmt_staging, &res);
+ if (p_dst == NULL) {
+ msg_Err(p_filter, "Failed to map create the temporary picture.");
+ goto done;
+ }
+ plane_Setup(p_dst->format.i_chroma, &p_dst->format, p_dst->p, &p_dst->i_planes, &p_dst->format.i_height);
+
+ IDirect3DSurface9_GetDevice(peek->p_sys->surface, &d3ddev);
+ HRESULT hr = IDirect3DDevice9_CreateOffscreenPlainSurface(d3ddev,
+ p_dst->format.i_width,
+ p_dst->format.i_height,
+ texDesc.Format,
+ D3DPOOL_DEFAULT,
+ &texture,
+ NULL);
+ if (FAILED(hr)) {
+ msg_Err(p_filter, "Failed to create a %4.4s staging texture to extract surface pixels (hr=0x%0lx)", FOURCC_STR(texDesc.Format), hr );
+ goto done;
+ }
+ res.p_sys->surface = texture;
+ //ID3D11DeviceContext_AddRef(p_dst->p_sys->context);
+
+ p_cpu_filter = CreateFilter(VLC_OBJECT(p_filter), &p_filter->fmt_in, p_dst->format.i_chroma);
+ if (!p_cpu_filter)
+ goto done;
+ }
+
+ filter_sys_t *p_sys = calloc(1, sizeof(filter_sys_t));
+ if (!p_sys) {
+ err = VLC_ENOMEM;
+ goto done;
+ }
+ p_sys->d3ddev = d3ddev;
+ p_sys->filter = p_cpu_filter;
+ p_sys->staging = p_dst;
+ p_filter->p_sys = p_sys;
+ err = VLC_SUCCESS;
+
+done:
+ video_format_Clean(&fmt_staging);
+ picture_Release(peek);
+ if (err != VLC_SUCCESS)
+ {
+ if (d3ddev)
+ IDirect3DDevice9_Release(d3ddev);
+ if (p_cpu_filter)
+ DeleteFilter( p_cpu_filter );
+ if (texture)
+ IDirect3DSurface9_Release(texture);
+ }
+ return err;
+}
+
+static void CloseConverter( vlc_object_t *obj )
+{
+ filter_t *p_filter = (filter_t *)obj;
+ filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
+ DeleteFilter(p_sys->filter);
+ picture_Release(p_sys->staging);
+ IDirect3DDevice9_Release(p_sys->d3ddev);
+ free( p_sys );
+ p_filter->p_sys = NULL;
+}
+
+/*****************************************************************************
+ * Module descriptor.
+ *****************************************************************************/
+
+vlc_module_begin ()
+ set_description( N_("Conversions from YUV to D3D9") )
+ set_capability( "video converter", 10 )
+ set_callbacks( OpenConverter, CloseConverter )
+vlc_module_end ()
--
2.12.1
More information about the vlc-devel
mailing list