texture: move to core libgstvaapi base library.
[platform/upstream/gstreamer.git] / gst / vaapi / gstvaapiuploader.c
1 /*
2  *  gstvaapiuploader.c - VA-API video upload helper
3  *
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>
8  *
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.
13  *
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.
18  *
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
23  */
24
25 #include "gst/vaapi/sysdeps.h"
26 #include <string.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>
31
32 #include "gstvaapiuploader.h"
33 #include "gstvaapipluginutil.h"
34 #include "gstvaapivideobuffer.h"
35
36 #define GST_HELPER_NAME "vaapiupload"
37 #define GST_HELPER_DESC "VA-API video uploader"
38
39 GST_DEBUG_CATEGORY_STATIC (gst_debug_vaapi_uploader);
40 #define GST_CAT_DEFAULT gst_debug_vaapi_uploader
41
42 G_DEFINE_TYPE (GstVaapiUploader, gst_vaapi_uploader, G_TYPE_OBJECT);
43
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))
49
50 struct _GstVaapiUploaderPrivate
51 {
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;
59 };
60
61 enum
62 {
63   PROP_0,
64
65   PROP_DISPLAY,
66 };
67
68 static void
69 gst_vaapi_uploader_destroy (GstVaapiUploader * uploader)
70 {
71   GstVaapiUploaderPrivate *const priv = uploader->priv;
72
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);
77 }
78
79 static gboolean
80 ensure_display (GstVaapiUploader * uploader, GstVaapiDisplay * display)
81 {
82   GstVaapiUploaderPrivate *const priv = uploader->priv;
83
84   gst_vaapi_display_replace (&priv->display, display);
85   return TRUE;
86 }
87
88 static gboolean
89 ensure_image (GstVaapiImage * image)
90 {
91   guint i, num_planes, width, height;
92
93   /* Make the image fully dirty */
94   if (!gst_vaapi_image_map (image))
95     return FALSE;
96
97   gst_vaapi_image_get_size (image, &width, &height);
98
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);
102     if (plane)
103       memset (plane, 0, gst_vaapi_image_get_pitch (image, i));
104   }
105
106   if (!gst_vaapi_image_unmap (image))
107     gst_vaapi_image_unmap (image);
108   return TRUE;
109 }
110
111 static gboolean
112 ensure_allowed_caps (GstVaapiUploader * uploader)
113 {
114   GstVaapiUploaderPrivate *const priv = uploader->priv;
115   GstVaapiSurface *surface = NULL;
116   GArray *formats = NULL, *out_formats = NULL;
117   GstCaps *out_caps;
118   guint i;
119   gboolean success = FALSE;
120
121   enum
122   { WIDTH = 64, HEIGHT = 64 };
123
124   if (priv->allowed_caps)
125     return TRUE;
126
127   formats = gst_vaapi_display_get_image_formats (priv->display);
128   if (!formats)
129     goto cleanup;
130
131   out_formats = g_array_sized_new (FALSE, FALSE, sizeof (GstVideoFormat),
132       formats->len);
133   if (!out_formats)
134     goto cleanup;
135
136   surface = gst_vaapi_surface_new (priv->display,
137       GST_VAAPI_CHROMA_TYPE_YUV420, WIDTH, HEIGHT);
138   if (!surface)
139     goto cleanup;
140
141   for (i = 0; i < formats->len; i++) {
142     const GstVideoFormat format = g_array_index (formats, GstVideoFormat, i);
143     GstVaapiImage *image;
144
145     if (format == GST_VIDEO_FORMAT_UNKNOWN)
146       continue;
147     image = gst_vaapi_image_new (priv->display, format, WIDTH, HEIGHT);
148     if (!image)
149       continue;
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);
153   }
154
155   out_caps = gst_vaapi_video_format_new_template_caps_from_list (out_formats);
156   if (!out_caps)
157     goto cleanup;
158
159   gst_caps_replace (&priv->allowed_caps, out_caps);
160   gst_caps_unref (out_caps);
161   success = TRUE;
162
163 cleanup:
164   if (out_formats)
165     g_array_unref (out_formats);
166   if (formats)
167     g_array_unref (formats);
168   if (surface)
169     gst_vaapi_object_unref (surface);
170   return success;
171 }
172
173 static gboolean
174 ensure_image_pool (GstVaapiUploader * uploader, GstCaps * caps,
175     gboolean * caps_changed_ptr)
176 {
177   GstVaapiUploaderPrivate *const priv = uploader->priv;
178   GstVaapiVideoPool *pool;
179   GstVideoInfo vi;
180   GstVideoFormat format;
181   guint width, height;
182
183   if (!gst_video_info_from_caps (&vi, caps))
184     return FALSE;
185
186   format = GST_VIDEO_INFO_FORMAT (&vi);
187   width = GST_VIDEO_INFO_WIDTH (&vi);
188   height = GST_VIDEO_INFO_HEIGHT (&vi);
189
190   *caps_changed_ptr =
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)
195     return TRUE;
196
197   pool = gst_vaapi_image_pool_new (priv->display, &vi);
198   if (!pool)
199     return FALSE;
200
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);
204   return TRUE;
205 }
206
207 static gboolean
208 ensure_surface_pool (GstVaapiUploader * uploader, GstCaps * caps,
209     gboolean * caps_changed_ptr)
210 {
211   GstVaapiUploaderPrivate *const priv = uploader->priv;
212   GstVaapiVideoPool *pool;
213   GstVideoInfo vi;
214   GstVideoFormat format;
215   guint width, height;
216
217   if (!gst_video_info_from_caps (&vi, caps))
218     return FALSE;
219
220   format = GST_VIDEO_INFO_FORMAT (&vi);
221   width = GST_VIDEO_INFO_WIDTH (&vi);
222   height = GST_VIDEO_INFO_HEIGHT (&vi);
223
224   *caps_changed_ptr =
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)
229     return TRUE;
230
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);
243     }
244   }
245
246   pool = gst_vaapi_surface_pool_new (priv->display, &vi);
247   if (!pool)
248     return FALSE;
249
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);
253   return TRUE;
254 }
255
256 static void
257 gst_vaapi_uploader_finalize (GObject * object)
258 {
259   gst_vaapi_uploader_destroy (GST_VAAPI_UPLOADER_CAST (object));
260
261   G_OBJECT_CLASS (gst_vaapi_uploader_parent_class)->finalize (object);
262 }
263
264 static void
265 gst_vaapi_uploader_set_property (GObject * object, guint prop_id,
266     const GValue * value, GParamSpec * pspec)
267 {
268   GstVaapiUploader *const uploader = GST_VAAPI_UPLOADER_CAST (object);
269
270   switch (prop_id) {
271     case PROP_DISPLAY:
272       ensure_display (uploader, g_value_get_pointer (value));
273       break;
274     default:
275       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
276       break;
277   }
278 }
279
280 static void
281 gst_vaapi_uploader_get_property (GObject * object, guint prop_id,
282     GValue * value, GParamSpec * pspec)
283 {
284   GstVaapiUploader *const uploader = GST_VAAPI_UPLOADER_CAST (object);
285
286   switch (prop_id) {
287     case PROP_DISPLAY:
288       g_value_set_pointer (value, uploader->priv->display);
289       break;
290     default:
291       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
292       break;
293   }
294 }
295
296 static void
297 gst_vaapi_uploader_class_init (GstVaapiUploaderClass * klass)
298 {
299   GObjectClass *const object_class = G_OBJECT_CLASS (klass);
300
301   GST_DEBUG_CATEGORY_INIT (gst_debug_vaapi_uploader,
302       GST_HELPER_NAME, 0, GST_HELPER_DESC);
303
304   g_type_class_add_private (klass, sizeof (GstVaapiUploaderPrivate));
305
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;
309
310   g_object_class_install_property (object_class,
311       PROP_DISPLAY,
312       g_param_spec_pointer ("display",
313           "Display",
314           "The GstVaapiDisplay this object is bound to",
315           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
316 }
317
318 static void
319 gst_vaapi_uploader_init (GstVaapiUploader * uploader)
320 {
321   GstVaapiUploaderPrivate *priv;
322
323   priv = GST_VAAPI_UPLOADER_GET_PRIVATE (uploader);
324   uploader->priv = priv;
325
326   gst_video_info_init (&priv->image_info);
327   gst_video_info_init (&priv->surface_info);
328 }
329
330 GstVaapiUploader *
331 gst_vaapi_uploader_new (GstVaapiDisplay * display)
332 {
333   return g_object_new (GST_VAAPI_TYPE_UPLOADER, "display", display, NULL);
334 }
335
336 gboolean
337 gst_vaapi_uploader_ensure_display (GstVaapiUploader * uploader,
338     GstVaapiDisplay * display)
339 {
340   g_return_val_if_fail (GST_VAAPI_IS_UPLOADER (uploader), FALSE);
341   g_return_val_if_fail (display != NULL, FALSE);
342
343   return ensure_display (uploader, display);
344 }
345
346 gboolean
347 gst_vaapi_uploader_ensure_caps (GstVaapiUploader * uploader,
348     GstCaps * src_caps, GstCaps * out_caps)
349 {
350   GstVaapiUploaderPrivate *priv;
351   GstVaapiImage *image;
352   gboolean image_caps_changed, surface_caps_changed;
353
354   g_return_val_if_fail (GST_VAAPI_IS_UPLOADER (uploader), FALSE);
355   g_return_val_if_fail (src_caps != NULL, FALSE);
356
357   if (!out_caps)
358     out_caps = src_caps;
359
360   if (!ensure_image_pool (uploader, src_caps, &image_caps_changed))
361     return FALSE;
362   if (!ensure_surface_pool (uploader, out_caps, &surface_caps_changed))
363     return FALSE;
364   if (!image_caps_changed && !surface_caps_changed)
365     return TRUE;
366
367   priv = uploader->priv;
368   priv->direct_rendering = 0;
369
370   /* Check if we can alias source and output buffers (same data_size) */
371   image = gst_vaapi_video_pool_get_object (priv->images);
372   if (image) {
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);
380   }
381
382   GST_INFO ("direct-rendering: level %u", priv->direct_rendering);
383   return TRUE;
384 }
385
386 gboolean
387 gst_vaapi_uploader_process (GstVaapiUploader * uploader,
388     GstBuffer * src_buffer, GstBuffer * out_buffer)
389 {
390   GstVaapiVideoMeta *src_meta, *out_meta;
391   GstVaapiSurface *surface;
392   GstVaapiImage *image;
393
394   g_return_val_if_fail (GST_VAAPI_IS_UPLOADER (uploader), FALSE);
395
396   out_meta = gst_buffer_get_vaapi_video_meta (out_buffer);
397   if (!out_meta) {
398     GST_WARNING ("expected an output video buffer");
399     return FALSE;
400   }
401
402   surface = gst_vaapi_video_meta_get_surface (out_meta);
403   g_return_val_if_fail (surface != NULL, FALSE);
404
405   src_meta = gst_buffer_get_vaapi_video_meta (src_buffer);
406   if (src_meta) {
407     /* GstVaapiVideoBuffer with mapped VA image */
408     image = gst_vaapi_video_meta_get_image (src_meta);
409     if (!image || !gst_vaapi_image_unmap (image))
410       return FALSE;
411   } else {
412     /* Regular GstBuffer that needs to be uploaded to a VA image */
413     image = gst_vaapi_video_meta_get_image (out_meta);
414     if (!image) {
415       image = gst_vaapi_video_pool_get_object (uploader->priv->images);
416       if (!image)
417         return FALSE;
418       gst_vaapi_video_meta_set_image (out_meta, image);
419     }
420     if (!gst_vaapi_image_update_from_buffer (image, src_buffer, NULL))
421       return FALSE;
422   }
423   g_return_val_if_fail (image != NULL, FALSE);
424
425   if (!gst_vaapi_surface_put_image (surface, image)) {
426     GST_WARNING ("failed to upload YUV buffer to VA surface");
427     return FALSE;
428   }
429
430   /* Map again for next uploads */
431   if (!gst_vaapi_image_map (image))
432     return FALSE;
433   return TRUE;
434 }
435
436 GstCaps *
437 gst_vaapi_uploader_get_caps (GstVaapiUploader * uploader)
438 {
439   g_return_val_if_fail (GST_VAAPI_IS_UPLOADER (uploader), NULL);
440
441   if (!ensure_allowed_caps (uploader))
442     return NULL;
443   return uploader->priv->allowed_caps;
444 }
445
446 GstBuffer *
447 gst_vaapi_uploader_get_buffer (GstVaapiUploader * uploader)
448 {
449   GstVaapiUploaderPrivate *priv;
450   GstVaapiImage *image;
451   GstVaapiVideoMeta *meta;
452   GstVaapiSurfaceProxy *proxy;
453   GstBuffer *buffer;
454
455   g_return_val_if_fail (GST_VAAPI_IS_UPLOADER (uploader), NULL);
456
457   priv = uploader->priv;
458
459   buffer = gst_vaapi_video_buffer_new_from_pool (priv->images);
460   if (!buffer) {
461     GST_WARNING ("failed to allocate video buffer");
462     goto error;
463   }
464
465   proxy =
466       gst_vaapi_surface_proxy_new_from_pool (GST_VAAPI_SURFACE_POOL
467       (priv->surfaces));
468   if (!proxy) {
469     GST_WARNING ("failed to allocate VA surface");
470     goto error;
471   }
472
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);
476
477   image = gst_vaapi_video_meta_get_image (meta);
478   if (!gst_vaapi_image_map (image)) {
479     GST_WARNING ("failed to map VA image");
480     goto error;
481   }
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);
485 #endif
486   return buffer;
487
488 error:
489   gst_buffer_unref (buffer);
490   return buffer;
491 }
492
493 gboolean
494 gst_vaapi_uploader_has_direct_rendering (GstVaapiUploader * uploader)
495 {
496   g_return_val_if_fail (GST_VAAPI_IS_UPLOADER (uploader), FALSE);
497
498   return uploader->priv->direct_rendering;
499 }