3 * Copyright (C) 2016 Matthew Waters <matthew@centricular.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * SECTION:element-vulkanupload
23 * @title: vulkanupload
25 * vulkanupload uploads data into Vulkan memory objects.
36 GST_DEBUG_CATEGORY (gst_debug_vulkan_upload);
37 #define GST_CAT_DEFAULT gst_debug_vulkan_upload
40 _set_caps_features_with_passthrough (const GstCaps * caps,
41 const gchar * feature_name, GstCapsFeatures * passthrough)
46 tmp = gst_caps_copy (caps);
48 n = gst_caps_get_size (caps);
49 for (i = 0; i < n; i++) {
50 GstCapsFeatures *features, *orig_features;
52 orig_features = gst_caps_get_features (caps, i);
53 features = gst_caps_features_new (feature_name, NULL);
55 m = gst_caps_features_get_size (orig_features);
56 for (j = 0; j < m; j++) {
57 const gchar *feature = gst_caps_features_get_nth (orig_features, j);
59 /* if we already have the features */
60 if (gst_caps_features_contains (features, feature))
63 if (g_strcmp0 (feature, GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY) == 0)
66 if (passthrough && gst_caps_features_contains (passthrough, feature)) {
67 gst_caps_features_add (features, feature);
71 gst_caps_set_features (tmp, i, features);
79 GstVulkanUpload *upload;
83 _buffer_new_impl (GstVulkanUpload * upload)
85 struct BufferUpload *raw = g_new0 (struct BufferUpload, 1);
93 _buffer_transform_caps (gpointer impl, GstPadDirection direction,
96 return gst_caps_ref (caps);
100 _buffer_set_caps (gpointer impl, GstCaps * in_caps, GstCaps * out_caps)
106 _buffer_propose_allocation (gpointer impl, GstQuery * decide_query,
109 struct BufferUpload *raw = impl;
114 GstBufferPool *pool = NULL;
116 gst_query_parse_allocation (query, &caps, &need_pool);
121 if (!gst_video_info_from_caps (&info, caps))
124 /* the normal size of a frame */
128 GstStructure *config;
130 pool = gst_vulkan_buffer_pool_new (raw->upload->device);
132 config = gst_buffer_pool_get_config (pool);
133 gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
135 if (!gst_buffer_pool_set_config (pool, config)) {
136 g_object_unref (pool);
141 gst_query_add_allocation_pool (query, pool, size, 1, 0);
143 g_object_unref (pool);
149 _buffer_perform (gpointer impl, GstBuffer * inbuf, GstBuffer ** outbuf)
151 if (!gst_is_vulkan_buffer_memory (gst_buffer_peek_memory (inbuf, 0)))
152 return GST_FLOW_ERROR;
160 _buffer_free (gpointer impl)
165 static GstStaticCaps _buffer_in_templ =
166 GST_STATIC_CAPS ("video/x-raw(" GST_CAPS_FEATURE_MEMORY_VULKAN_BUFFER ") ;"
168 static GstStaticCaps _buffer_out_templ =
169 GST_STATIC_CAPS ("video/x-raw(" GST_CAPS_FEATURE_MEMORY_VULKAN_BUFFER ")");
171 static const struct UploadMethod buffer_upload = {
176 _buffer_transform_caps,
178 _buffer_propose_allocation,
183 struct RawToBufferUpload
185 GstVulkanUpload *upload;
187 GstVideoInfo in_info;
188 GstVideoInfo out_info;
191 gboolean pool_active;
193 gsize alloc_sizes[GST_VIDEO_MAX_PLANES];
197 _raw_to_buffer_new_impl (GstVulkanUpload * upload)
199 struct RawToBufferUpload *raw = g_new0 (struct RawToBufferUpload, 1);
201 raw->upload = upload;
207 _raw_to_buffer_transform_caps (gpointer impl, GstPadDirection direction,
212 if (direction == GST_PAD_SINK) {
214 _set_caps_features_with_passthrough (caps,
215 GST_CAPS_FEATURE_MEMORY_VULKAN_BUFFER, NULL);
218 _set_caps_features_with_passthrough (caps,
219 GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY, NULL);
226 _raw_to_buffer_set_caps (gpointer impl, GstCaps * in_caps, GstCaps * out_caps)
228 struct RawToBufferUpload *raw = impl;
229 guint out_width, out_height;
232 if (!gst_video_info_from_caps (&raw->in_info, in_caps))
235 if (!gst_video_info_from_caps (&raw->out_info, out_caps))
238 out_width = GST_VIDEO_INFO_WIDTH (&raw->out_info);
239 out_height = GST_VIDEO_INFO_HEIGHT (&raw->out_info);
241 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&raw->out_info); i++) {
242 GstVideoFormat v_format = GST_VIDEO_INFO_FORMAT (&raw->out_info);
243 GstVulkanImageMemory *img_mem;
246 vk_format = gst_vulkan_format_from_video_format (v_format, i);
248 img_mem = (GstVulkanImageMemory *)
249 gst_vulkan_image_memory_alloc (raw->upload->device, vk_format,
250 out_width, out_height, VK_IMAGE_TILING_OPTIMAL,
251 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT,
252 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
254 raw->alloc_sizes[i] = img_mem->requirements.size;
256 gst_memory_unref (GST_MEMORY_CAST (img_mem));
263 _raw_to_buffer_propose_allocation (gpointer impl, GstQuery * decide_query,
266 /* a little trickery with the impl pointer */
267 _buffer_propose_allocation (impl, decide_query, query);
271 _raw_to_buffer_perform (gpointer impl, GstBuffer * inbuf, GstBuffer ** outbuf)
273 struct RawToBufferUpload *raw = impl;
274 GstVideoFrame v_frame;
279 GstStructure *config;
280 guint min = 0, max = 0;
283 raw->pool = gst_vulkan_buffer_pool_new (raw->upload->device);
284 config = gst_buffer_pool_get_config (raw->pool);
285 gst_buffer_pool_config_set_params (config, raw->upload->out_caps, size, min,
287 gst_buffer_pool_set_config (raw->pool, config);
289 if (!raw->pool_active) {
290 gst_buffer_pool_set_active (raw->pool, TRUE);
291 raw->pool_active = TRUE;
295 gst_buffer_pool_acquire_buffer (raw->pool, outbuf,
296 NULL)) != GST_FLOW_OK)
299 if (!gst_video_frame_map (&v_frame, &raw->in_info, inbuf, GST_MAP_READ)) {
300 GST_ELEMENT_ERROR (raw->upload, RESOURCE, NOT_FOUND,
301 ("%s", "Failed to map input buffer"), NULL);
302 return GST_FLOW_ERROR;
305 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&raw->out_info); i++) {
310 mem = gst_buffer_peek_memory (*outbuf, i);
311 if (!gst_memory_map (GST_MEMORY_CAST (mem), &map_info, GST_MAP_WRITE)) {
312 GST_ELEMENT_ERROR (raw->upload, RESOURCE, NOT_FOUND,
313 ("%s", "Failed to map output memory"), NULL);
314 gst_buffer_unref (*outbuf);
316 ret = GST_FLOW_ERROR;
321 GST_VIDEO_INFO_PLANE_STRIDE (&raw->out_info,
322 i) * GST_VIDEO_INFO_COMP_HEIGHT (&raw->out_info, i);
323 g_assert (plane_size < map_info.size);
324 memcpy (map_info.data, v_frame.data[i], plane_size);
326 gst_memory_unmap (GST_MEMORY_CAST (mem), &map_info);
329 gst_video_frame_unmap (&v_frame);
338 _raw_to_buffer_free (gpointer impl)
340 struct RawToBufferUpload *raw = impl;
343 if (raw->pool_active) {
344 gst_buffer_pool_set_active (raw->pool, FALSE);
346 raw->pool_active = FALSE;
347 gst_object_unref (raw->pool);
354 static GstStaticCaps _raw_to_buffer_in_templ = GST_STATIC_CAPS ("video/x-raw");
355 static GstStaticCaps _raw_to_buffer_out_templ =
356 GST_STATIC_CAPS ("video/x-raw(" GST_CAPS_FEATURE_MEMORY_VULKAN_BUFFER ")");
358 static const struct UploadMethod raw_to_buffer_upload = {
360 &_raw_to_buffer_in_templ,
361 &_raw_to_buffer_out_templ,
362 _raw_to_buffer_new_impl,
363 _raw_to_buffer_transform_caps,
364 _raw_to_buffer_set_caps,
365 _raw_to_buffer_propose_allocation,
366 _raw_to_buffer_perform,
370 static const struct UploadMethod *upload_methods[] = {
372 &raw_to_buffer_upload,
376 _get_input_template_caps (void)
381 /* FIXME: cache this and invalidate on changes to upload_methods */
382 for (i = 0; i < G_N_ELEMENTS (upload_methods); i++) {
383 GstCaps *template = gst_static_caps_get (upload_methods[i]->in_template);
384 ret = ret == NULL ? template : gst_caps_merge (ret, template);
387 ret = gst_caps_simplify (ret);
393 _get_output_template_caps (void)
398 /* FIXME: cache this and invalidate on changes to upload_methods */
399 for (i = 0; i < G_N_ELEMENTS (upload_methods); i++) {
400 GstCaps *template = gst_static_caps_get (upload_methods[i]->out_template);
401 ret = ret == NULL ? template : gst_caps_merge (ret, template);
404 ret = gst_caps_simplify (ret);
409 static void gst_vulkan_upload_finalize (GObject * object);
410 static void gst_vulkan_upload_set_property (GObject * object, guint prop_id,
411 const GValue * value, GParamSpec * param_spec);
412 static void gst_vulkan_upload_get_property (GObject * object, guint prop_id,
413 GValue * value, GParamSpec * param_spec);
415 static gboolean gst_vulkan_upload_query (GstBaseTransform * bt,
416 GstPadDirection direction, GstQuery * query);
417 static void gst_vulkan_upload_set_context (GstElement * element,
418 GstContext * context);
419 static GstStateChangeReturn gst_vulkan_upload_change_state (GstElement *
420 element, GstStateChange transition);
422 static gboolean gst_vulkan_upload_set_caps (GstBaseTransform * bt,
423 GstCaps * in_caps, GstCaps * out_caps);
424 static GstCaps *gst_vulkan_upload_transform_caps (GstBaseTransform * bt,
425 GstPadDirection direction, GstCaps * caps, GstCaps * filter);
426 static gboolean gst_vulkan_upload_propose_allocation (GstBaseTransform * bt,
427 GstQuery * decide_query, GstQuery * query);
428 static gboolean gst_vulkan_upload_decide_allocation (GstBaseTransform * bt,
430 static GstFlowReturn gst_vulkan_upload_transform (GstBaseTransform * bt,
431 GstBuffer * inbuf, GstBuffer * outbuf);
432 static GstFlowReturn gst_vulkan_upload_prepare_output_buffer (GstBaseTransform *
433 bt, GstBuffer * inbuf, GstBuffer ** outbuf);
446 /* static guint gst_vulkan_upload_signals[LAST_SIGNAL] = { 0 }; */
448 #define gst_vulkan_upload_parent_class parent_class
449 G_DEFINE_TYPE_WITH_CODE (GstVulkanUpload, gst_vulkan_upload,
450 GST_TYPE_BASE_TRANSFORM, GST_DEBUG_CATEGORY_INIT (gst_debug_vulkan_upload,
451 "vulkanupload", 0, "Vulkan Uploader"));
454 gst_vulkan_upload_class_init (GstVulkanUploadClass * klass)
456 GObjectClass *gobject_class;
457 GstElementClass *gstelement_class;
458 GstBaseTransformClass *gstbasetransform_class;
460 gobject_class = (GObjectClass *) klass;
461 gstelement_class = (GstElementClass *) klass;
462 gstbasetransform_class = (GstBaseTransformClass *) klass;
464 gobject_class->set_property = gst_vulkan_upload_set_property;
465 gobject_class->get_property = gst_vulkan_upload_get_property;
467 gst_element_class_set_metadata (gstelement_class, "Vulkan Uploader",
468 "Filter/Video", "A Vulkan data uploader",
469 "Matthew Waters <matthew@centricular.com>");
474 caps = _get_input_template_caps ();
475 gst_element_class_add_pad_template (gstelement_class,
476 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps));
477 gst_caps_unref (caps);
479 caps = _get_output_template_caps ();
480 gst_element_class_add_pad_template (gstelement_class,
481 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, caps));
482 gst_caps_unref (caps);
485 gobject_class->finalize = gst_vulkan_upload_finalize;
487 gstelement_class->change_state = gst_vulkan_upload_change_state;
488 gstelement_class->set_context = gst_vulkan_upload_set_context;
489 gstbasetransform_class->query = GST_DEBUG_FUNCPTR (gst_vulkan_upload_query);
490 gstbasetransform_class->set_caps = gst_vulkan_upload_set_caps;
491 gstbasetransform_class->transform_caps = gst_vulkan_upload_transform_caps;
492 gstbasetransform_class->propose_allocation =
493 gst_vulkan_upload_propose_allocation;
494 gstbasetransform_class->decide_allocation =
495 gst_vulkan_upload_decide_allocation;
496 gstbasetransform_class->transform = gst_vulkan_upload_transform;
497 gstbasetransform_class->prepare_output_buffer =
498 gst_vulkan_upload_prepare_output_buffer;
502 gst_vulkan_upload_init (GstVulkanUpload * vk_upload)
506 n = G_N_ELEMENTS (upload_methods);
507 vk_upload->upload_impls = g_malloc (sizeof (gpointer) * n);
508 for (i = 0; i < n; i++) {
509 vk_upload->upload_impls[i] = upload_methods[i]->new_impl (vk_upload);
514 gst_vulkan_upload_finalize (GObject * object)
516 GstVulkanUpload *vk_upload = GST_VULKAN_UPLOAD (object);
519 gst_caps_replace (&vk_upload->in_caps, NULL);
520 gst_caps_replace (&vk_upload->out_caps, NULL);
522 for (i = 0; i < G_N_ELEMENTS (upload_methods); i++) {
523 upload_methods[i]->free (vk_upload->upload_impls[i]);
526 G_OBJECT_CLASS (parent_class)->finalize (object);
530 gst_vulkan_upload_set_property (GObject * object, guint prop_id,
531 const GValue * value, GParamSpec * pspec)
533 // GstVulkanUpload *vk_upload = GST_VULKAN_UPLOAD (object);
537 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
543 gst_vulkan_upload_get_property (GObject * object, guint prop_id,
544 GValue * value, GParamSpec * pspec)
546 // GstVulkanUpload *vk_upload = GST_VULKAN_UPLOAD (object);
550 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
556 gst_vulkan_upload_query (GstBaseTransform * bt, GstPadDirection direction,
559 GstVulkanUpload *vk_upload = GST_VULKAN_UPLOAD (bt);
560 gboolean res = FALSE;
562 switch (GST_QUERY_TYPE (query)) {
563 case GST_QUERY_CONTEXT:{
564 res = gst_vulkan_handle_context_query (GST_ELEMENT (vk_upload), query,
565 &vk_upload->display, &vk_upload->instance, &vk_upload->device);
575 return GST_BASE_TRANSFORM_CLASS (parent_class)->query (bt, direction, query);
579 gst_vulkan_upload_set_context (GstElement * element, GstContext * context)
581 GstVulkanUpload *vk_upload = GST_VULKAN_UPLOAD (element);
583 gst_vulkan_handle_set_context (element, context, &vk_upload->display,
584 &vk_upload->instance);
586 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
589 static GstStateChangeReturn
590 gst_vulkan_upload_change_state (GstElement * element, GstStateChange transition)
592 GstVulkanUpload *vk_upload = GST_VULKAN_UPLOAD (element);
593 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
595 GST_DEBUG ("changing state: %s => %s",
596 gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
597 gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
599 switch (transition) {
600 case GST_STATE_CHANGE_NULL_TO_READY:
602 case GST_STATE_CHANGE_READY_TO_PAUSED:
603 if (!gst_vulkan_ensure_element_data (element, &vk_upload->display,
604 &vk_upload->instance)) {
605 GST_ELEMENT_ERROR (vk_upload, RESOURCE, NOT_FOUND,
606 ("Failed to retreive vulkan instance/display"), (NULL));
607 return GST_STATE_CHANGE_FAILURE;
609 if (!gst_vulkan_device_run_context_query (GST_ELEMENT (vk_upload),
610 &vk_upload->device)) {
611 GST_ELEMENT_ERROR (vk_upload, RESOURCE, NOT_FOUND,
612 ("Failed to retreive vulkan device"), (NULL));
613 return GST_STATE_CHANGE_FAILURE;
616 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
622 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
623 if (ret == GST_STATE_CHANGE_FAILURE)
626 switch (transition) {
627 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
629 case GST_STATE_CHANGE_PAUSED_TO_READY:
630 if (vk_upload->display)
631 gst_object_unref (vk_upload->display);
632 vk_upload->display = NULL;
633 if (vk_upload->device)
634 gst_object_unref (vk_upload->device);
635 vk_upload->device = NULL;
636 if (vk_upload->instance)
637 gst_object_unref (vk_upload->instance);
638 vk_upload->instance = NULL;
640 case GST_STATE_CHANGE_READY_TO_NULL:
650 gst_vulkan_upload_transform_caps (GstBaseTransform * bt,
651 GstPadDirection direction, GstCaps * caps, GstCaps * filter)
653 GstVulkanUpload *vk_upload = GST_VULKAN_UPLOAD (bt);
654 GstCaps *result, *tmp;
657 tmp = gst_caps_new_empty ();
659 for (i = 0; i < G_N_ELEMENTS (upload_methods); i++) {
663 if (direction == GST_PAD_SINK) {
664 templ = gst_static_caps_get (upload_methods[i]->in_template);
666 templ = gst_static_caps_get (upload_methods[i]->out_template);
668 if (!gst_caps_can_intersect (caps, templ)) {
669 gst_caps_unref (templ);
672 gst_caps_unref (templ);
674 tmp2 = upload_methods[i]->transform_caps (vk_upload->upload_impls[i],
678 tmp = gst_caps_merge (tmp, tmp2);
682 result = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
683 gst_caps_unref (tmp);
692 gst_vulkan_upload_set_caps (GstBaseTransform * bt, GstCaps * in_caps,
695 GstVulkanUpload *vk_upload = GST_VULKAN_UPLOAD (bt);
696 gboolean found_method = FALSE;
699 gst_caps_replace (&vk_upload->in_caps, in_caps);
700 gst_caps_replace (&vk_upload->out_caps, out_caps);
702 for (i = 0; i < G_N_ELEMENTS (upload_methods); i++) {
705 templ = gst_static_caps_get (upload_methods[i]->in_template);
706 if (!gst_caps_can_intersect (in_caps, templ)) {
707 gst_caps_unref (templ);
710 gst_caps_unref (templ);
712 templ = gst_static_caps_get (upload_methods[i]->out_template);
713 if (!gst_caps_can_intersect (out_caps, templ)) {
714 gst_caps_unref (templ);
717 gst_caps_unref (templ);
719 if (!upload_methods[i]->set_caps (vk_upload->upload_impls[i], in_caps,
723 GST_LOG_OBJECT (bt, "uploader %s accepted caps in: %" GST_PTR_FORMAT
724 " out: %" GST_PTR_FORMAT, upload_methods[i]->name, in_caps, out_caps);
726 vk_upload->current_impl = i;
731 GST_DEBUG_OBJECT (bt,
732 "set caps in: %" GST_PTR_FORMAT " out: %" GST_PTR_FORMAT, in_caps,
739 gst_vulkan_upload_propose_allocation (GstBaseTransform * bt,
740 GstQuery * decide_query, GstQuery * query)
742 GstVulkanUpload *vk_upload = GST_VULKAN_UPLOAD (bt);
745 for (i = 0; i < G_N_ELEMENTS (upload_methods); i++) {
748 templ = gst_static_caps_get (upload_methods[i]->in_template);
749 if (!gst_caps_can_intersect (vk_upload->in_caps, templ)) {
750 gst_caps_unref (templ);
753 gst_caps_unref (templ);
755 templ = gst_static_caps_get (upload_methods[i]->out_template);
756 if (!gst_caps_can_intersect (vk_upload->out_caps, templ)) {
757 gst_caps_unref (templ);
760 gst_caps_unref (templ);
762 upload_methods[i]->propose_allocation (vk_upload->upload_impls[i],
763 decide_query, query);
770 gst_vulkan_upload_decide_allocation (GstBaseTransform * bt, GstQuery * query)
776 _upload_find_method (GstVulkanUpload * vk_upload)
778 vk_upload->current_impl++;
780 if (vk_upload->current_impl >= G_N_ELEMENTS (upload_methods))
783 GST_DEBUG_OBJECT (vk_upload, "attempting upload with uploader %s",
784 upload_methods[vk_upload->current_impl]->name);
790 gst_vulkan_upload_prepare_output_buffer (GstBaseTransform * bt,
791 GstBuffer * inbuf, GstBuffer ** outbuf)
793 GstBaseTransformClass *bclass = GST_BASE_TRANSFORM_GET_CLASS (bt);
794 GstVulkanUpload *vk_upload = GST_VULKAN_UPLOAD (bt);
799 gpointer method_impl;
800 const struct UploadMethod *method;
802 method = upload_methods[vk_upload->current_impl];
803 method_impl = vk_upload->upload_impls[vk_upload->current_impl];
805 ret = method->perform (method_impl, inbuf, outbuf);
806 if (ret != GST_FLOW_OK) {
808 if (!_upload_find_method (vk_upload)) {
809 GST_ELEMENT_ERROR (bt, RESOURCE, NOT_FOUND,
810 ("Could not find suitable uploader"), (NULL));
811 return GST_FLOW_ERROR;
814 method = upload_methods[vk_upload->current_impl];
815 method_impl = vk_upload->upload_impls[vk_upload->current_impl];
816 if (!method->set_caps (method_impl, vk_upload->in_caps,
817 vk_upload->out_caps))
818 /* try the next method */
821 /* try the uploading with the next method */
826 if (ret == GST_FLOW_OK) {
827 /* basetransform doesn't unref if they're the same */
828 if (inbuf != *outbuf)
829 bclass->copy_metadata (bt, inbuf, *outbuf);
836 gst_vulkan_upload_transform (GstBaseTransform * bt, GstBuffer * inbuf,