2 * gstvaapiuploader.c - VA-API video upload helper
4 * Copyright (C) 2010-2011 Splitted-Desktop Systems
5 * Author: Gwenole Beauchesne <gwenole.beauchesne@splitted-desktop.com>
6 * Copyright (C) 2011-2013 Intel Corporation
7 * Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301 USA
25 #include "gst/vaapi/sysdeps.h"
27 #include <gst/video/video.h>
28 #include <gst/vaapi/gstvaapisurface.h>
29 #include <gst/vaapi/gstvaapiimagepool.h>
30 #include <gst/vaapi/gstvaapisurfacepool.h>
32 #include "gstvaapiuploader.h"
33 #include "gstvaapipluginutil.h"
34 #include "gstvaapivideobuffer.h"
36 #define GST_HELPER_NAME "vaapiupload"
37 #define GST_HELPER_DESC "VA-API video uploader"
39 GST_DEBUG_CATEGORY_STATIC (gst_debug_vaapi_uploader);
40 #define GST_CAT_DEFAULT gst_debug_vaapi_uploader
42 G_DEFINE_TYPE (GstVaapiUploader, gst_vaapi_uploader, G_TYPE_OBJECT);
44 #define GST_VAAPI_UPLOADER_CAST(obj) \
45 ((GstVaapiUploader *)(obj))
46 #define GST_VAAPI_UPLOADER_GET_PRIVATE(obj) \
47 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_VAAPI_TYPE_UPLOADER, \
48 GstVaapiUploaderPrivate))
50 struct _GstVaapiUploaderPrivate
52 GstVaapiDisplay *display;
53 GstCaps *allowed_caps;
54 GstVaapiVideoPool *images;
55 GstVideoInfo image_info;
56 GstVaapiVideoPool *surfaces;
57 GstVideoInfo surface_info;
58 guint direct_rendering;
69 gst_vaapi_uploader_destroy (GstVaapiUploader * uploader)
71 GstVaapiUploaderPrivate *const priv = uploader->priv;
73 gst_caps_replace (&priv->allowed_caps, NULL);
74 gst_vaapi_video_pool_replace (&priv->images, NULL);
75 gst_vaapi_video_pool_replace (&priv->surfaces, NULL);
76 gst_vaapi_display_replace (&priv->display, NULL);
80 ensure_display (GstVaapiUploader * uploader, GstVaapiDisplay * display)
82 GstVaapiUploaderPrivate *const priv = uploader->priv;
84 gst_vaapi_display_replace (&priv->display, display);
89 ensure_image (GstVaapiImage * image)
91 guint i, num_planes, width, height;
93 /* Make the image fully dirty */
94 if (!gst_vaapi_image_map (image))
97 gst_vaapi_image_get_size (image, &width, &height);
99 num_planes = gst_vaapi_image_get_plane_count (image);
100 for (i = 0; i < num_planes; i++) {
101 guchar *const plane = gst_vaapi_image_get_plane (image, i);
103 memset (plane, 0, gst_vaapi_image_get_pitch (image, i));
106 if (!gst_vaapi_image_unmap (image))
107 gst_vaapi_image_unmap (image);
112 ensure_allowed_caps (GstVaapiUploader * uploader)
114 GstVaapiUploaderPrivate *const priv = uploader->priv;
115 GstVaapiSurface *surface = NULL;
116 GArray *formats = NULL, *out_formats = NULL;
119 gboolean success = FALSE;
122 { WIDTH = 64, HEIGHT = 64 };
124 if (priv->allowed_caps)
127 formats = gst_vaapi_display_get_image_formats (priv->display);
131 out_formats = g_array_sized_new (FALSE, FALSE, sizeof (GstVideoFormat),
136 surface = gst_vaapi_surface_new (priv->display,
137 GST_VAAPI_CHROMA_TYPE_YUV420, WIDTH, HEIGHT);
141 for (i = 0; i < formats->len; i++) {
142 const GstVideoFormat format = g_array_index (formats, GstVideoFormat, i);
143 GstVaapiImage *image;
145 if (format == GST_VIDEO_FORMAT_UNKNOWN)
147 image = gst_vaapi_image_new (priv->display, format, WIDTH, HEIGHT);
150 if (ensure_image (image) && gst_vaapi_surface_put_image (surface, image))
151 g_array_append_val (out_formats, format);
152 gst_vaapi_object_unref (image);
155 out_caps = gst_vaapi_video_format_new_template_caps_from_list (out_formats);
159 gst_caps_replace (&priv->allowed_caps, out_caps);
160 gst_caps_unref (out_caps);
165 g_array_unref (out_formats);
167 g_array_unref (formats);
169 gst_vaapi_object_unref (surface);
174 ensure_image_pool (GstVaapiUploader * uploader, GstCaps * caps,
175 gboolean * caps_changed_ptr)
177 GstVaapiUploaderPrivate *const priv = uploader->priv;
178 GstVaapiVideoPool *pool;
180 GstVideoFormat format;
183 if (!gst_video_info_from_caps (&vi, caps))
186 format = GST_VIDEO_INFO_FORMAT (&vi);
187 width = GST_VIDEO_INFO_WIDTH (&vi);
188 height = GST_VIDEO_INFO_HEIGHT (&vi);
191 format != GST_VIDEO_INFO_FORMAT (&priv->image_info) ||
192 width != GST_VIDEO_INFO_WIDTH (&priv->image_info) ||
193 height != GST_VIDEO_INFO_HEIGHT (&priv->image_info);
194 if (!*caps_changed_ptr)
197 pool = gst_vaapi_image_pool_new (priv->display, &vi);
201 gst_video_info_set_format (&priv->image_info, format, width, height);
202 gst_vaapi_video_pool_replace (&priv->images, pool);
203 gst_vaapi_video_pool_unref (pool);
208 ensure_surface_pool (GstVaapiUploader * uploader, GstCaps * caps,
209 gboolean * caps_changed_ptr)
211 GstVaapiUploaderPrivate *const priv = uploader->priv;
212 GstVaapiVideoPool *pool;
214 GstVideoFormat format;
217 if (!gst_video_info_from_caps (&vi, caps))
220 format = GST_VIDEO_INFO_FORMAT (&vi);
221 width = GST_VIDEO_INFO_WIDTH (&vi);
222 height = GST_VIDEO_INFO_HEIGHT (&vi);
225 format != GST_VIDEO_INFO_FORMAT (&priv->surface_info) ||
226 width != GST_VIDEO_INFO_WIDTH (&priv->surface_info) ||
227 height != GST_VIDEO_INFO_HEIGHT (&priv->surface_info);
228 if (!*caps_changed_ptr)
231 /* Always try to downsample source buffers to YUV 4:2:0 format as
232 this saves memory bandwidth for further rendering */
233 /* XXX: this also means that visual quality is not preserved */
234 if (format != GST_VIDEO_FORMAT_ENCODED) {
235 const GstVaapiChromaType chroma_type =
236 gst_vaapi_video_format_get_chroma_type (format);
237 if (chroma_type != GST_VAAPI_CHROMA_TYPE_YUV420) {
238 const GstVideoFormat image_format =
239 GST_VIDEO_INFO_FORMAT (&priv->image_info);
240 GST_INFO ("use implicit conversion of %s buffers to NV12 surfaces",
241 gst_video_format_to_string (image_format));
242 gst_video_info_set_format (&vi, GST_VIDEO_FORMAT_NV12, width, height);
246 pool = gst_vaapi_surface_pool_new (priv->display, &vi);
250 gst_video_info_set_format (&priv->surface_info, format, width, height);
251 gst_vaapi_video_pool_replace (&priv->surfaces, pool);
252 gst_vaapi_video_pool_unref (pool);
257 gst_vaapi_uploader_finalize (GObject * object)
259 gst_vaapi_uploader_destroy (GST_VAAPI_UPLOADER_CAST (object));
261 G_OBJECT_CLASS (gst_vaapi_uploader_parent_class)->finalize (object);
265 gst_vaapi_uploader_set_property (GObject * object, guint prop_id,
266 const GValue * value, GParamSpec * pspec)
268 GstVaapiUploader *const uploader = GST_VAAPI_UPLOADER_CAST (object);
272 ensure_display (uploader, g_value_get_pointer (value));
275 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
281 gst_vaapi_uploader_get_property (GObject * object, guint prop_id,
282 GValue * value, GParamSpec * pspec)
284 GstVaapiUploader *const uploader = GST_VAAPI_UPLOADER_CAST (object);
288 g_value_set_pointer (value, uploader->priv->display);
291 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
297 gst_vaapi_uploader_class_init (GstVaapiUploaderClass * klass)
299 GObjectClass *const object_class = G_OBJECT_CLASS (klass);
301 GST_DEBUG_CATEGORY_INIT (gst_debug_vaapi_uploader,
302 GST_HELPER_NAME, 0, GST_HELPER_DESC);
304 g_type_class_add_private (klass, sizeof (GstVaapiUploaderPrivate));
306 object_class->finalize = gst_vaapi_uploader_finalize;
307 object_class->set_property = gst_vaapi_uploader_set_property;
308 object_class->get_property = gst_vaapi_uploader_get_property;
310 g_object_class_install_property (object_class,
312 g_param_spec_pointer ("display",
314 "The GstVaapiDisplay this object is bound to",
315 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
319 gst_vaapi_uploader_init (GstVaapiUploader * uploader)
321 GstVaapiUploaderPrivate *priv;
323 priv = GST_VAAPI_UPLOADER_GET_PRIVATE (uploader);
324 uploader->priv = priv;
326 gst_video_info_init (&priv->image_info);
327 gst_video_info_init (&priv->surface_info);
331 gst_vaapi_uploader_new (GstVaapiDisplay * display)
333 return g_object_new (GST_VAAPI_TYPE_UPLOADER, "display", display, NULL);
337 gst_vaapi_uploader_ensure_display (GstVaapiUploader * uploader,
338 GstVaapiDisplay * display)
340 g_return_val_if_fail (GST_VAAPI_IS_UPLOADER (uploader), FALSE);
341 g_return_val_if_fail (display != NULL, FALSE);
343 return ensure_display (uploader, display);
347 gst_vaapi_uploader_ensure_caps (GstVaapiUploader * uploader,
348 GstCaps * src_caps, GstCaps * out_caps)
350 GstVaapiUploaderPrivate *priv;
351 GstVaapiImage *image;
352 gboolean image_caps_changed, surface_caps_changed;
354 g_return_val_if_fail (GST_VAAPI_IS_UPLOADER (uploader), FALSE);
355 g_return_val_if_fail (src_caps != NULL, FALSE);
360 if (!ensure_image_pool (uploader, src_caps, &image_caps_changed))
362 if (!ensure_surface_pool (uploader, out_caps, &surface_caps_changed))
364 if (!image_caps_changed && !surface_caps_changed)
367 priv = uploader->priv;
368 priv->direct_rendering = 0;
370 /* Check if we can alias source and output buffers (same data_size) */
371 image = gst_vaapi_video_pool_get_object (priv->images);
373 if ((gst_vaapi_image_get_format (image) ==
374 GST_VIDEO_INFO_FORMAT (&priv->image_info)) &&
375 gst_vaapi_image_is_linear (image) &&
376 (gst_vaapi_image_get_data_size (image) ==
377 GST_VIDEO_INFO_SIZE (&priv->image_info)))
378 priv->direct_rendering = 1;
379 gst_vaapi_video_pool_put_object (priv->images, image);
382 GST_INFO ("direct-rendering: level %u", priv->direct_rendering);
387 gst_vaapi_uploader_process (GstVaapiUploader * uploader,
388 GstBuffer * src_buffer, GstBuffer * out_buffer)
390 GstVaapiVideoMeta *src_meta, *out_meta;
391 GstVaapiSurface *surface;
392 GstVaapiImage *image;
394 g_return_val_if_fail (GST_VAAPI_IS_UPLOADER (uploader), FALSE);
396 out_meta = gst_buffer_get_vaapi_video_meta (out_buffer);
398 GST_WARNING ("expected an output video buffer");
402 surface = gst_vaapi_video_meta_get_surface (out_meta);
403 g_return_val_if_fail (surface != NULL, FALSE);
405 src_meta = gst_buffer_get_vaapi_video_meta (src_buffer);
407 /* GstVaapiVideoBuffer with mapped VA image */
408 image = gst_vaapi_video_meta_get_image (src_meta);
409 if (!image || !gst_vaapi_image_unmap (image))
412 /* Regular GstBuffer that needs to be uploaded to a VA image */
413 image = gst_vaapi_video_meta_get_image (out_meta);
415 image = gst_vaapi_video_pool_get_object (uploader->priv->images);
418 gst_vaapi_video_meta_set_image (out_meta, image);
420 if (!gst_vaapi_image_update_from_buffer (image, src_buffer, NULL))
423 g_return_val_if_fail (image != NULL, FALSE);
425 if (!gst_vaapi_surface_put_image (surface, image)) {
426 GST_WARNING ("failed to upload YUV buffer to VA surface");
430 /* Map again for next uploads */
431 if (!gst_vaapi_image_map (image))
437 gst_vaapi_uploader_get_caps (GstVaapiUploader * uploader)
439 g_return_val_if_fail (GST_VAAPI_IS_UPLOADER (uploader), NULL);
441 if (!ensure_allowed_caps (uploader))
443 return uploader->priv->allowed_caps;
447 gst_vaapi_uploader_get_buffer (GstVaapiUploader * uploader)
449 GstVaapiUploaderPrivate *priv;
450 GstVaapiImage *image;
451 GstVaapiVideoMeta *meta;
452 GstVaapiSurfaceProxy *proxy;
455 g_return_val_if_fail (GST_VAAPI_IS_UPLOADER (uploader), NULL);
457 priv = uploader->priv;
459 buffer = gst_vaapi_video_buffer_new_from_pool (priv->images);
461 GST_WARNING ("failed to allocate video buffer");
466 gst_vaapi_surface_proxy_new_from_pool (GST_VAAPI_SURFACE_POOL
469 GST_WARNING ("failed to allocate VA surface");
473 meta = gst_buffer_get_vaapi_video_meta (buffer);
474 gst_vaapi_video_meta_set_surface_proxy (meta, proxy);
475 gst_vaapi_surface_proxy_unref (proxy);
477 image = gst_vaapi_video_meta_get_image (meta);
478 if (!gst_vaapi_image_map (image)) {
479 GST_WARNING ("failed to map VA image");
482 #if !GST_CHECK_VERSION(1,0,0)
483 GST_BUFFER_DATA (buffer) = gst_vaapi_image_get_plane (image, 0);
484 GST_BUFFER_SIZE (buffer) = gst_vaapi_image_get_data_size (image);
489 gst_buffer_unref (buffer);
494 gst_vaapi_uploader_has_direct_rendering (GstVaapiUploader * uploader)
496 g_return_val_if_fail (GST_VAAPI_IS_UPLOADER (uploader), FALSE);
498 return uploader->priv->direct_rendering;