diff --git a/sys/d3d11/gstd3d11_fwd.h b/sys/d3d11/gstd3d11_fwd.h index b3e70e7abc..abc1c40a40 100644 --- a/sys/d3d11/gstd3d11_fwd.h +++ b/sys/d3d11/gstd3d11_fwd.h @@ -55,6 +55,15 @@ typedef struct _GstD3D11BufferPoolPrivate GstD3D11BufferPoolPrivate; typedef struct _GstD3D11Format GstD3D11Format; +typedef struct _GstD3D11BaseFilter GstD3D11BaseFilter; +typedef struct _GstD3D11BaseFilterClass GstD3D11BaseFilterClass; + +typedef struct _GstD3D11Upload GstD3D11Upload; +typedef struct _GstD3D11UploadClass GstD3D11UploadClass; + +typedef struct _GstD3D11Download GstD3D11Download; +typedef struct _GstD3D11DownloadClass GstD3D11DownloadClass; + G_END_DECLS #endif /* __GST_D3D11_FWD_H__ */ diff --git a/sys/d3d11/gstd3d11basefilter.c b/sys/d3d11/gstd3d11basefilter.c new file mode 100644 index 0000000000..a1a8edcdca --- /dev/null +++ b/sys/d3d11/gstd3d11basefilter.c @@ -0,0 +1,256 @@ +/* GStreamer + * Copyright (C) <2019> Seungha Yang + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "gstd3d11basefilter.h" +#include "gstd3d11utils.h" +#include "gstd3d11device.h" +#include "gstd3d11bufferpool.h" +#include "gstd3d11memory.h" + +GST_DEBUG_CATEGORY_STATIC (gst_d3d11_base_filter_debug); +#define GST_CAT_DEFAULT gst_d3d11_base_filter_debug + +enum +{ + PROP_0, + PROP_ADAPTER, +}; + +#define DEFAULT_ADAPTER -1 + +#define gst_d3d11_base_filter_parent_class parent_class +G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstD3D11BaseFilter, gst_d3d11_base_filter, + GST_TYPE_BASE_TRANSFORM, GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, + "d3d11basefilter", 0, "d3d11 basefilter")); + +static void gst_d3d11_base_filter_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec); +static void gst_d3d11_base_filter_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec); +static void gst_d3d11_base_filter_dispose (GObject * object); +static void gst_d3d11_base_filter_set_context (GstElement * element, + GstContext * context); +static gboolean gst_d3d11_base_filter_start (GstBaseTransform * trans); +static gboolean gst_d3d11_base_filter_stop (GstBaseTransform * trans); +static gboolean gst_d3d11_base_filter_set_caps (GstBaseTransform * trans, + GstCaps * incaps, GstCaps * outcaps); +static gboolean gst_d3d11_base_filter_get_unit_size (GstBaseTransform * trans, + GstCaps * caps, gsize * size); +static gboolean +gst_d3d11_base_filter_query (GstBaseTransform * trans, + GstPadDirection direction, GstQuery * query); + +static void +gst_d3d11_base_filter_class_init (GstD3D11BaseFilterClass * klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + GstElementClass *element_class = GST_ELEMENT_CLASS (klass); + GstBaseTransformClass *trans_class = GST_BASE_TRANSFORM_CLASS (klass); + + gobject_class->set_property = gst_d3d11_base_filter_set_property; + gobject_class->get_property = gst_d3d11_base_filter_get_property; + gobject_class->dispose = gst_d3d11_base_filter_dispose; + + g_object_class_install_property (gobject_class, PROP_ADAPTER, + g_param_spec_int ("adapter", "Adapter", + "Adapter index for creating device (-1 for default)", + -1, G_MAXINT32, DEFAULT_ADAPTER, + G_PARAM_READWRITE | GST_PARAM_MUTABLE_READY | + G_PARAM_STATIC_STRINGS)); + + element_class->set_context = + GST_DEBUG_FUNCPTR (gst_d3d11_base_filter_set_context); + + trans_class->passthrough_on_same_caps = TRUE; + + trans_class->start = GST_DEBUG_FUNCPTR (gst_d3d11_base_filter_start); + trans_class->stop = GST_DEBUG_FUNCPTR (gst_d3d11_base_filter_stop); + trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_d3d11_base_filter_set_caps); + trans_class->get_unit_size = + GST_DEBUG_FUNCPTR (gst_d3d11_base_filter_get_unit_size); + trans_class->query = GST_DEBUG_FUNCPTR (gst_d3d11_base_filter_query); +} + +static void +gst_d3d11_base_filter_init (GstD3D11BaseFilter * filter) +{ + filter->adapter = DEFAULT_ADAPTER; +} + +static void +gst_d3d11_base_filter_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec) +{ + GstD3D11BaseFilter *filter = GST_D3D11_BASE_FILTER (object); + + switch (prop_id) { + case PROP_ADAPTER: + filter->adapter = g_value_get_int (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_d3d11_base_filter_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec) +{ + GstD3D11BaseFilter *filter = GST_D3D11_BASE_FILTER (object); + + switch (prop_id) { + case PROP_ADAPTER: + g_value_set_int (value, filter->adapter); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_d3d11_base_filter_dispose (GObject * object) +{ + GstD3D11BaseFilter *filter = GST_D3D11_BASE_FILTER (object); + + gst_clear_object (&filter->device); + + G_OBJECT_CLASS (parent_class)->dispose (object); +} + +static void +gst_d3d11_base_filter_set_context (GstElement * element, GstContext * context) +{ + GstD3D11BaseFilter *filter = GST_D3D11_BASE_FILTER (element); + + gst_d3d11_handle_set_context (element, + context, filter->adapter, &filter->device); + + GST_ELEMENT_CLASS (parent_class)->set_context (element, context); +} + +static gboolean +gst_d3d11_base_filter_start (GstBaseTransform * trans) +{ + GstD3D11BaseFilter *filter = GST_D3D11_BASE_FILTER (trans); + + if (!gst_d3d11_ensure_element_data (GST_ELEMENT_CAST (filter), + filter->adapter, &filter->device)) { + GST_ERROR_OBJECT (filter, "Failed to get D3D11 device"); + return FALSE; + } + + return TRUE; +} + +static gboolean +gst_d3d11_base_filter_stop (GstBaseTransform * trans) +{ + GstD3D11BaseFilter *filter = GST_D3D11_BASE_FILTER (trans); + + gst_clear_object (&filter->device); + + return TRUE; +} + +static gboolean +gst_d3d11_base_filter_set_caps (GstBaseTransform * trans, GstCaps * incaps, + GstCaps * outcaps) +{ + GstD3D11BaseFilter *filter = GST_D3D11_BASE_FILTER (trans); + GstVideoInfo in_info, out_info; + GstD3D11BaseFilterClass *klass; + gboolean res; + + if (!filter->device) { + GST_ERROR_OBJECT (filter, "No available D3D11 device"); + return FALSE; + } + + /* input caps */ + if (!gst_video_info_from_caps (&in_info, incaps)) + goto invalid_caps; + + /* output caps */ + if (!gst_video_info_from_caps (&out_info, outcaps)) + goto invalid_caps; + + klass = GST_D3D11_BASE_FILTER_GET_CLASS (filter); + if (klass->set_info) + res = klass->set_info (filter, incaps, &in_info, outcaps, &out_info); + else + res = TRUE; + + if (res) { + filter->in_info = in_info; + filter->out_info = out_info; + } + + return res; + + /* ERRORS */ +invalid_caps: + { + GST_ERROR_OBJECT (filter, "invalid caps"); + return FALSE; + } +} + +static gboolean +gst_d3d11_base_filter_get_unit_size (GstBaseTransform * trans, GstCaps * caps, + gsize * size) +{ + gboolean ret = FALSE; + GstVideoInfo info; + + ret = gst_video_info_from_caps (&info, caps); + if (ret) + *size = GST_VIDEO_INFO_SIZE (&info); + + return TRUE; +} + +static gboolean +gst_d3d11_base_filter_query (GstBaseTransform * trans, + GstPadDirection direction, GstQuery * query) +{ + GstD3D11BaseFilter *filter = GST_D3D11_BASE_FILTER (trans); + + switch (GST_QUERY_TYPE (query)) { + case GST_QUERY_CONTEXT: + { + gboolean ret; + ret = gst_d3d11_handle_context_query (GST_ELEMENT (filter), query, + filter->device); + if (ret) + return TRUE; + break; + } + default: + break; + } + + return GST_BASE_TRANSFORM_CLASS (parent_class)->query (trans, direction, + query); +} diff --git a/sys/d3d11/gstd3d11basefilter.h b/sys/d3d11/gstd3d11basefilter.h new file mode 100644 index 0000000000..1750959240 --- /dev/null +++ b/sys/d3d11/gstd3d11basefilter.h @@ -0,0 +1,65 @@ +/* GStreamer + * Copyright (C) 2019 Seungha Yang + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef __GST_D3D11_BASE_FILTER_H__ +#define __GST_D3D11_BASE_FILTER_H__ + +#include +#include +#include + +#include "gstd3d11_fwd.h" + +G_BEGIN_DECLS + +#define GST_TYPE_D3D11_BASE_FILTER (gst_d3d11_base_filter_get_type()) +#define GST_D3D11_BASE_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_D3D11_BASE_FILTER,GstD3D11BaseFilter)) +#define GST_D3D11_BASE_FILTER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_D3D11_BASE_FILTER,GstD3D11BaseFilterClass)) +#define GST_D3D11_BASE_FILTER_GET_CLASS(obj) (GST_D3D11_BASE_FILTER_CLASS(G_OBJECT_GET_CLASS(obj))) +#define GST_IS_D3D11_BASE_FILTER(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_D3D11_BASE_FILTER)) +#define GST_IS_D3D11_BASE_FILTER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_D3D11_BASE_FILTER)) +#define GST_D3D11_BASE_FILTER_CAST(obj) ((GstD3D11BaseFilter*)(obj)) + +struct _GstD3D11BaseFilter +{ + GstBaseTransform parent; + + GstD3D11Device *device; + + GstVideoInfo in_info; + GstVideoInfo out_info; + + /* properties */ + gint adapter; +}; + +struct _GstD3D11BaseFilterClass +{ + GstBaseTransformClass parent_class; + + gboolean (*set_info) (GstD3D11BaseFilter *filter, + GstCaps *incaps, GstVideoInfo *in_info, + GstCaps *outcaps, GstVideoInfo *out_info); +}; + +GType gst_d3d11_base_filter_get_type (void); + +G_END_DECLS + +#endif /* __GST_D3D11_BASE_FILTER_H__ */ diff --git a/sys/d3d11/gstd3d11download.c b/sys/d3d11/gstd3d11download.c new file mode 100644 index 0000000000..31ad80722e --- /dev/null +++ b/sys/d3d11/gstd3d11download.c @@ -0,0 +1,343 @@ +/* GStreamer + * Copyright (C) <2019> Seungha Yang + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "gstd3d11download.h" +#include "gstd3d11memory.h" +#include "gstd3d11device.h" +#include "gstd3d11bufferpool.h" + +GST_DEBUG_CATEGORY_STATIC (gst_d3d11_download_debug); +#define GST_CAT_DEFAULT gst_d3d11_download_debug + +static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES + (GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY, GST_D3D11_FORMATS) "; " + GST_VIDEO_CAPS_MAKE (GST_D3D11_FORMATS) + )); + +static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_D3D11_FORMATS) + )); + +#define gst_d3d11_download_parent_class parent_class +G_DEFINE_TYPE (GstD3D11Download, gst_d3d11_download, + GST_TYPE_D3D11_BASE_FILTER); + +static GstCaps *gst_d3d11_download_transform_caps (GstBaseTransform * trans, + GstPadDirection direction, GstCaps * caps, GstCaps * filter); +static gboolean gst_d3d11_download_propose_allocation (GstBaseTransform * trans, + GstQuery * decide_query, GstQuery * query); +static gboolean gst_d3d11_download_decide_allocation (GstBaseTransform * trans, + GstQuery * query); +static GstFlowReturn gst_d3d11_download_transform (GstBaseTransform * trans, + GstBuffer * inbuf, GstBuffer * outbuf); + +static void +gst_d3d11_download_class_init (GstD3D11DownloadClass * klass) +{ + GstElementClass *element_class = GST_ELEMENT_CLASS (klass); + GstBaseTransformClass *trans_class = GST_BASE_TRANSFORM_CLASS (klass); + + gst_element_class_add_static_pad_template (element_class, &sink_template); + gst_element_class_add_static_pad_template (element_class, &src_template); + + gst_element_class_set_static_metadata (element_class, + "Direct3D11 downloader", "Filter/Video", + "Downloads D3D11 texture memory into system memory", + "Seungha Yang "); + + trans_class->passthrough_on_same_caps = TRUE; + + trans_class->transform_caps = + GST_DEBUG_FUNCPTR (gst_d3d11_download_transform_caps); + trans_class->propose_allocation = + GST_DEBUG_FUNCPTR (gst_d3d11_download_propose_allocation); + trans_class->decide_allocation = + GST_DEBUG_FUNCPTR (gst_d3d11_download_decide_allocation); + trans_class->transform = GST_DEBUG_FUNCPTR (gst_d3d11_download_transform); + + GST_DEBUG_CATEGORY_INIT (gst_d3d11_download_debug, + "d3d11download", 0, "d3d11download Element"); +} + +static void +gst_d3d11_download_init (GstD3D11Download * download) +{ +} + +static GstCaps * +_set_caps_features (const GstCaps * caps, const gchar * feature_name) +{ + GstCaps *tmp = gst_caps_copy (caps); + guint n = gst_caps_get_size (tmp); + guint i = 0; + + for (i = 0; i < n; i++) + gst_caps_set_features (tmp, i, + gst_caps_features_from_string (feature_name)); + + return tmp; +} + +static GstCaps * +gst_d3d11_download_transform_caps (GstBaseTransform * trans, + GstPadDirection direction, GstCaps * caps, GstCaps * filter) +{ + GstCaps *result, *tmp; + + GST_DEBUG_OBJECT (trans, + "Transforming caps %" GST_PTR_FORMAT " in direction %s", caps, + (direction == GST_PAD_SINK) ? "sink" : "src"); + + if (direction == GST_PAD_SINK) { + tmp = _set_caps_features (caps, GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY); + tmp = gst_caps_merge (gst_caps_ref (caps), tmp); + } else { + GstCaps *newcaps; + tmp = gst_caps_ref (caps); + + newcaps = _set_caps_features (caps, GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY); + tmp = gst_caps_merge (tmp, newcaps); + } + + if (filter) { + result = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST); + gst_caps_unref (tmp); + } else { + result = tmp; + } + + GST_DEBUG_OBJECT (trans, "returning caps: %" GST_PTR_FORMAT, result); + + return result; +} + +static gboolean +gst_d3d11_download_propose_allocation (GstBaseTransform * trans, + GstQuery * decide_query, GstQuery * query) +{ + GstD3D11BaseFilter *filter = GST_D3D11_BASE_FILTER (trans); + GstVideoInfo info; + GstBufferPool *pool; + GstCaps *caps; + guint size; + + if (!GST_BASE_TRANSFORM_CLASS (parent_class)->propose_allocation (trans, + decide_query, query)) + return FALSE; + + /* passthrough, we're done */ + if (decide_query == NULL) + return TRUE; + + gst_query_parse_allocation (query, &caps, NULL); + + if (caps == NULL) + return FALSE; + + if (!gst_video_info_from_caps (&info, caps)) + return FALSE; + + if (gst_query_get_n_allocation_pools (query) == 0) { + GstCapsFeatures *features; + GstStructure *config; + gboolean is_d3d11 = FALSE; + + features = gst_caps_get_features (caps, 0); + + if (features && gst_caps_features_contains (features, + GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY)) { + GST_DEBUG_OBJECT (filter, "upstream support d3d11 memory"); + pool = gst_d3d11_buffer_pool_new (filter->device); + is_d3d11 = TRUE; + } else { + pool = gst_video_buffer_pool_new (); + } + + config = gst_buffer_pool_get_config (pool); + + gst_buffer_pool_config_add_option (config, + GST_BUFFER_POOL_OPTION_VIDEO_META); + + /* d3d11 pool does not support video alignment */ + if (!is_d3d11) { + gst_buffer_pool_config_add_option (config, + GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT); + } + + size = GST_VIDEO_INFO_SIZE (&info); + gst_buffer_pool_config_set_params (config, caps, size, 0, 0); + + if (!gst_buffer_pool_set_config (pool, config)) + goto config_failed; + + gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL); + + /* d3d11 buffer pool might update buffer size by self */ + if (is_d3d11) { + size = GST_D3D11_BUFFER_POOL (pool)->buffer_size; + } + + gst_query_add_allocation_pool (query, pool, size, 0, 0); + + gst_object_unref (pool); + } + + return TRUE; + + /* ERRORS */ +config_failed: + { + GST_ERROR_OBJECT (filter, "failed to set config"); + gst_object_unref (pool); + return FALSE; + } +} + +static gboolean +gst_d3d11_download_decide_allocation (GstBaseTransform * trans, + GstQuery * query) +{ + GstBufferPool *pool = NULL; + GstStructure *config; + guint min, max, size; + gboolean update_pool; + GstCaps *outcaps = NULL; + + if (gst_query_get_n_allocation_pools (query) > 0) { + gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max); + + if (!pool) + gst_query_parse_allocation (query, &outcaps, NULL); + + update_pool = TRUE; + } else { + GstVideoInfo vinfo; + + gst_query_parse_allocation (query, &outcaps, NULL); + gst_video_info_from_caps (&vinfo, outcaps); + size = vinfo.size; + min = max = 0; + update_pool = FALSE; + } + + if (!pool) + pool = gst_video_buffer_pool_new (); + + config = gst_buffer_pool_get_config (pool); + gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META); + if (outcaps) + gst_buffer_pool_config_set_params (config, outcaps, size, 0, 0); + gst_buffer_pool_set_config (pool, config); + + if (update_pool) + gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max); + else + gst_query_add_allocation_pool (query, pool, size, min, max); + + gst_object_unref (pool); + + return GST_BASE_TRANSFORM_CLASS (parent_class)->decide_allocation (trans, + query); +} + +typedef struct +{ + GstD3D11BaseFilter *filter; + GstBuffer *inbuf; + GstBuffer *outbuf; + GstFlowReturn ret; +} UploadTransformData; + +static void +download_transform (GstD3D11Device * device, UploadTransformData * data) +{ + GstVideoFrame in_frame, out_frame; + GstD3D11BaseFilter *filter = data->filter; + gint i; + + if (!gst_video_frame_map (&in_frame, &filter->in_info, data->inbuf, + GST_MAP_READ | GST_VIDEO_FRAME_MAP_FLAG_NO_REF)) + goto invalid_buffer; + + if (!gst_video_frame_map (&out_frame, &filter->out_info, data->outbuf, + GST_MAP_WRITE | GST_VIDEO_FRAME_MAP_FLAG_NO_REF)) { + gst_video_frame_unmap (&in_frame); + goto invalid_buffer; + } + + for (i = 0; i < GST_VIDEO_FRAME_N_PLANES (&in_frame); i++) { + if (!gst_video_frame_copy_plane (&out_frame, &in_frame, i)) { + GST_ERROR_OBJECT (filter, "Couldn't copy %dth plane", i); + data->ret = GST_FLOW_ERROR; + return; + } + } + + gst_video_frame_unmap (&out_frame); + gst_video_frame_unmap (&in_frame); + + data->ret = GST_FLOW_OK; + return; + + /* ERRORS */ +invalid_buffer: + { + GST_ELEMENT_WARNING (filter, CORE, NOT_IMPLEMENTED, (NULL), + ("invalid video buffer received")); + data->ret = GST_FLOW_ERROR; + return; + } +} + +static GstFlowReturn +gst_d3d11_download_transform (GstBaseTransform * trans, GstBuffer * inbuf, + GstBuffer * outbuf) +{ + GstD3D11BaseFilter *filter = GST_D3D11_BASE_FILTER (trans); + GstMemory *mem; + GstD3D11Device *device; + UploadTransformData data; + + mem = gst_buffer_peek_memory (inbuf, 0); + if (gst_is_d3d11_memory (mem)) { + GstD3D11Memory *dmem = (GstD3D11Memory *) mem; + device = dmem->device; + } else { + device = filter->device; + } + + data.filter = filter; + data.inbuf = inbuf; + data.outbuf = outbuf; + data.ret = GST_FLOW_OK; + + gst_d3d11_device_thread_add (device, + (GstD3D11DeviceThreadFunc) download_transform, &data); + + return data.ret; +} diff --git a/sys/d3d11/gstd3d11download.h b/sys/d3d11/gstd3d11download.h new file mode 100644 index 0000000000..4ca4f43589 --- /dev/null +++ b/sys/d3d11/gstd3d11download.h @@ -0,0 +1,48 @@ +/* GStreamer + * Copyright (C) <2019> Seungha Yang + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef __GST_D3D11_DOWNLOAD_H__ +#define __GST_D3D11_DOWNLOAD_H__ + +#include "gstd3d11basefilter.h" + +G_BEGIN_DECLS + +#define GST_TYPE_D3D11_DOWNLOAD (gst_d3d11_download_get_type()) +#define GST_D3D11_DOWNLOAD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_D3D11_DOWNLOAD,GstD3D11Download)) +#define GST_D3D11_DOWNLOAD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_D3D11_DOWNLOAD,GstD3D11DownloadClass)) +#define GST_D3D11_DOWNLOAD_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GST_TYPE_D3D11_DOWNLOAD,GstD3D11DownloadClass)) +#define GST_IS_D3D11_DOWNLOAD(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_D3D11_DOWNLOAD)) +#define GST_IS_D3D11_DOWNLOAD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_D3D11_DOWNLOAD)) + +struct _GstD3D11Download +{ + GstD3D11BaseFilter parent; +}; + +struct _GstD3D11DownloadClass +{ + GstD3D11BaseFilterClass parent_class; +}; + +GType gst_d3d11_download_get_type (void); + +G_END_DECLS + +#endif /* __GST_D3D11_DOWNLOAD_H__ */ diff --git a/sys/d3d11/gstd3d11upload.c b/sys/d3d11/gstd3d11upload.c new file mode 100644 index 0000000000..e2bf4ce44c --- /dev/null +++ b/sys/d3d11/gstd3d11upload.c @@ -0,0 +1,348 @@ +/* GStreamer + * Copyright (C) <2019> Seungha Yang + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "gstd3d11upload.h" +#include "gstd3d11memory.h" +#include "gstd3d11device.h" +#include "gstd3d11bufferpool.h" + +GST_DEBUG_CATEGORY_STATIC (gst_d3d11_upload_debug); +#define GST_CAT_DEFAULT gst_d3d11_upload_debug + +static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_D3D11_FORMATS) "; " + GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY, + GST_D3D11_FORMATS)) + ); + +static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES + (GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY, GST_D3D11_FORMATS))); + +#define gst_d3d11_upload_parent_class parent_class +G_DEFINE_TYPE (GstD3D11Upload, gst_d3d11_upload, GST_TYPE_D3D11_BASE_FILTER); + +static GstCaps *gst_d3d11_upload_transform_caps (GstBaseTransform * trans, + GstPadDirection direction, GstCaps * caps, GstCaps * filter); +static gboolean gst_d3d11_upload_propose_allocation (GstBaseTransform * trans, + GstQuery * decide_query, GstQuery * query); +static gboolean gst_d3d11_upload_decide_allocation (GstBaseTransform * trans, + GstQuery * query); +static GstFlowReturn gst_d3d11_upload_transform (GstBaseTransform * trans, + GstBuffer * inbuf, GstBuffer * outbuf); + +static void +gst_d3d11_upload_class_init (GstD3D11UploadClass * klass) +{ + GstElementClass *element_class = GST_ELEMENT_CLASS (klass); + GstBaseTransformClass *trans_class = GST_BASE_TRANSFORM_CLASS (klass); + + gst_element_class_add_static_pad_template (element_class, &sink_template); + gst_element_class_add_static_pad_template (element_class, &src_template); + + gst_element_class_set_static_metadata (element_class, + "Direct3D11 uploader", "Filter/Video", + "Uploads data into D3D11 texture memory", + "Seungha Yang "); + + trans_class->passthrough_on_same_caps = TRUE; + + trans_class->transform_caps = + GST_DEBUG_FUNCPTR (gst_d3d11_upload_transform_caps); + trans_class->propose_allocation = + GST_DEBUG_FUNCPTR (gst_d3d11_upload_propose_allocation); + trans_class->decide_allocation = + GST_DEBUG_FUNCPTR (gst_d3d11_upload_decide_allocation); + trans_class->transform = GST_DEBUG_FUNCPTR (gst_d3d11_upload_transform); + + GST_DEBUG_CATEGORY_INIT (gst_d3d11_upload_debug, + "d3d11upload", 0, "d3d11upload Element"); +} + +static void +gst_d3d11_upload_init (GstD3D11Upload * upload) +{ +} + +static GstCaps * +_set_caps_features (const GstCaps * caps, const gchar * feature_name) +{ + GstCaps *tmp = gst_caps_copy (caps); + guint n = gst_caps_get_size (tmp); + guint i = 0; + + for (i = 0; i < n; i++) + gst_caps_set_features (tmp, i, + gst_caps_features_from_string (feature_name)); + + return tmp; +} + +static GstCaps * +gst_d3d11_upload_transform_caps (GstBaseTransform * trans, + GstPadDirection direction, GstCaps * caps, GstCaps * filter) +{ + GstCaps *result, *tmp; + + GST_DEBUG_OBJECT (trans, + "Transforming caps %" GST_PTR_FORMAT " in direction %s", caps, + (direction == GST_PAD_SINK) ? "sink" : "src"); + + if (direction == GST_PAD_SINK) { + tmp = _set_caps_features (caps, GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY); + tmp = gst_caps_merge (gst_caps_ref (caps), tmp); + } else { + GstCaps *newcaps; + tmp = gst_caps_ref (caps); + + newcaps = _set_caps_features (caps, GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY); + tmp = gst_caps_merge (tmp, newcaps); + } + + if (filter) { + result = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST); + gst_caps_unref (tmp); + } else { + result = tmp; + } + + GST_DEBUG_OBJECT (trans, "returning caps: %" GST_PTR_FORMAT, result); + + return result; +} + +static gboolean +gst_d3d11_upload_propose_allocation (GstBaseTransform * trans, + GstQuery * decide_query, GstQuery * query) +{ + GstD3D11BaseFilter *filter = GST_D3D11_BASE_FILTER (trans); + GstVideoInfo info; + GstBufferPool *pool; + GstCaps *caps; + guint size; + + if (!GST_BASE_TRANSFORM_CLASS (parent_class)->propose_allocation (trans, + decide_query, query)) + return FALSE; + + /* passthrough, we're done */ + if (decide_query == NULL) + return TRUE; + + gst_query_parse_allocation (query, &caps, NULL); + + if (caps == NULL) + return FALSE; + + if (!gst_video_info_from_caps (&info, caps)) + return FALSE; + + if (gst_query_get_n_allocation_pools (query) == 0) { + GstCapsFeatures *features; + GstStructure *config; + gboolean is_d3d11 = FALSE; + + features = gst_caps_get_features (caps, 0); + + if (features && gst_caps_features_contains (features, + GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY)) { + GST_DEBUG_OBJECT (filter, "upstream support d3d11 memory"); + pool = gst_d3d11_buffer_pool_new (filter->device); + is_d3d11 = TRUE; + } else { + pool = gst_video_buffer_pool_new (); + } + + config = gst_buffer_pool_get_config (pool); + + gst_buffer_pool_config_add_option (config, + GST_BUFFER_POOL_OPTION_VIDEO_META); + + /* d3d11 pool does not support video alignment */ + if (!is_d3d11) { + gst_buffer_pool_config_add_option (config, + GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT); + } + + size = GST_VIDEO_INFO_SIZE (&info); + gst_buffer_pool_config_set_params (config, caps, size, 0, 0); + + if (!gst_buffer_pool_set_config (pool, config)) + goto config_failed; + + gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL); + + /* d3d11 buffer pool might update buffer size by self */ + if (is_d3d11) + size = GST_D3D11_BUFFER_POOL (pool)->buffer_size; + + gst_query_add_allocation_pool (query, pool, size, 0, 0); + gst_object_unref (pool); + } + + return TRUE; + + /* ERRORS */ +config_failed: + { + GST_ERROR_OBJECT (filter, "failed to set config"); + gst_object_unref (pool); + return FALSE; + } +} + +static gboolean +gst_d3d11_upload_decide_allocation (GstBaseTransform * trans, GstQuery * query) +{ + GstD3D11BaseFilter *filter = GST_D3D11_BASE_FILTER (trans); + GstCaps *outcaps = NULL; + GstBufferPool *pool = NULL; + guint size, min, max; + GstStructure *config; + gboolean update_pool = FALSE; + + gst_query_parse_allocation (query, &outcaps, NULL); + + if (!outcaps) + return FALSE; + + if (gst_query_get_n_allocation_pools (query) > 0) { + gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max); + if (pool && !GST_IS_D3D11_BUFFER_POOL (pool)) { + gst_object_unref (pool); + pool = NULL; + } + + update_pool = TRUE; + } else { + GstVideoInfo vinfo; + gst_video_info_from_caps (&vinfo, outcaps); + size = GST_VIDEO_INFO_SIZE (&vinfo); + min = max = 0; + } + + if (!pool) { + GST_DEBUG_OBJECT (trans, "create our pool"); + + pool = gst_d3d11_buffer_pool_new (filter->device); + } + + config = gst_buffer_pool_get_config (pool); + gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META); + gst_buffer_pool_config_set_params (config, outcaps, size, min, max); + gst_buffer_pool_set_config (pool, config); + + /* update size with calculated one */ + size = GST_D3D11_BUFFER_POOL (pool)->buffer_size; + + if (update_pool) + gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max); + else + gst_query_add_allocation_pool (query, pool, size, min, max); + + gst_object_unref (pool); + + return GST_BASE_TRANSFORM_CLASS (parent_class)->decide_allocation (trans, + query); +} + +typedef struct +{ + GstD3D11BaseFilter *filter; + GstBuffer *inbuf; + GstBuffer *outbuf; + GstFlowReturn ret; +} UploadTransformData; + +static void +upload_transform (GstD3D11Device * device, UploadTransformData * data) +{ + GstVideoFrame in_frame, out_frame; + GstD3D11BaseFilter *filter = data->filter; + gint i; + + if (!gst_video_frame_map (&in_frame, &filter->in_info, data->inbuf, + GST_MAP_READ | GST_VIDEO_FRAME_MAP_FLAG_NO_REF)) + goto invalid_buffer; + + if (!gst_video_frame_map (&out_frame, &filter->out_info, data->outbuf, + GST_MAP_WRITE | GST_VIDEO_FRAME_MAP_FLAG_NO_REF)) { + gst_video_frame_unmap (&in_frame); + goto invalid_buffer; + } + + for (i = 0; i < GST_VIDEO_FRAME_N_PLANES (&in_frame); i++) { + if (!gst_video_frame_copy_plane (&out_frame, &in_frame, i)) { + GST_ERROR_OBJECT (filter, "Couldn't copy %dth plane", i); + data->ret = GST_FLOW_ERROR; + return; + } + } + + gst_video_frame_unmap (&out_frame); + gst_video_frame_unmap (&in_frame); + + data->ret = GST_FLOW_OK; + return; + + /* ERRORS */ +invalid_buffer: + { + GST_ELEMENT_WARNING (filter, CORE, NOT_IMPLEMENTED, (NULL), + ("invalid video buffer received")); + data->ret = GST_FLOW_ERROR; + return; + } +} + +static GstFlowReturn +gst_d3d11_upload_transform (GstBaseTransform * trans, GstBuffer * inbuf, + GstBuffer * outbuf) +{ + GstD3D11BaseFilter *filter = GST_D3D11_BASE_FILTER (trans); + GstMemory *mem; + GstD3D11Device *device; + UploadTransformData data; + + mem = gst_buffer_peek_memory (outbuf, 0); + if (gst_is_d3d11_memory (mem)) { + GstD3D11Memory *dmem = (GstD3D11Memory *) mem; + device = dmem->device; + } else { + device = filter->device; + } + + data.filter = filter; + data.inbuf = inbuf; + data.outbuf = outbuf; + data.ret = GST_FLOW_OK; + + gst_d3d11_device_thread_add (device, + (GstD3D11DeviceThreadFunc) upload_transform, &data); + + return data.ret; +} diff --git a/sys/d3d11/gstd3d11upload.h b/sys/d3d11/gstd3d11upload.h new file mode 100644 index 0000000000..851c400f9d --- /dev/null +++ b/sys/d3d11/gstd3d11upload.h @@ -0,0 +1,48 @@ +/* GStreamer + * Copyright (C) <2019> Seungha Yang + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef __GST_D3D11_UPLOAD_H__ +#define __GST_D3D11_UPLOAD_H__ + +#include "gstd3d11basefilter.h" + +G_BEGIN_DECLS + +#define GST_TYPE_D3D11_UPLOAD (gst_d3d11_upload_get_type()) +#define GST_D3D11_UPLOAD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_D3D11_UPLOAD,GstD3D11Upload)) +#define GST_D3D11_UPLOAD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_D3D11_UPLOAD,GstD3D11UploadClass)) +#define GST_D3D11_UPLOAD_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GST_TYPE_D3D11_UPLOAD,GstD3D11UploadClass)) +#define GST_IS_D3D11_UPLOAD(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_D3D11_UPLOAD)) +#define GST_IS_D3D11_UPLOAD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_D3D11_UPLOAD)) + +struct _GstD3D11Upload +{ + GstD3D11BaseFilter parent; +}; + +struct _GstD3D11UploadClass +{ + GstD3D11BaseFilterClass parent_class; +}; + +GType gst_d3d11_upload_get_type (void); + +G_END_DECLS + +#endif /* __GST_D3D11_UPLOAD_H__ */ diff --git a/sys/d3d11/meson.build b/sys/d3d11/meson.build index 5a012ad4bc..6f1c131d97 100644 --- a/sys/d3d11/meson.build +++ b/sys/d3d11/meson.build @@ -7,6 +7,9 @@ d3d11_sources = [ 'gstd3d11window.c', 'plugin.c', 'gstd3d11format.c', + 'gstd3d11basefilter.c', + 'gstd3d11upload.c', + 'gstd3d11download.c', ] have_d3d11 = false diff --git a/sys/d3d11/plugin.c b/sys/d3d11/plugin.c index 11d95a644f..2abba45de5 100644 --- a/sys/d3d11/plugin.c +++ b/sys/d3d11/plugin.c @@ -23,12 +23,21 @@ #include #include "gstd3d11videosink.h" +#include "gstd3d11upload.h" +#include "gstd3d11download.h" static gboolean plugin_init (GstPlugin * plugin) { - return gst_element_register (plugin, + gst_element_register (plugin, "d3d11videosink", GST_RANK_SECONDARY - 1, GST_TYPE_D3D11_VIDEO_SINK); + + gst_element_register (plugin, + "d3d11upload", GST_RANK_NONE, GST_TYPE_D3D11_UPLOAD); + gst_element_register (plugin, + "d3d11download", GST_RANK_NONE, GST_TYPE_D3D11_DOWNLOAD); + + return TRUE; } GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,