plugins: fix GstVaapiVideoMemory to allocate VA surface proxies.
[vaapi:sree-gstreamer-vaapi.git] / gst / vaapi / gstvaapivideomemory.c
1 /*
2  *  gstvaapivideomemory.c - Gstreamer/VA video memory
3  *
4  *  Copyright (C) 2013 Intel Corporation
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public License
8  *  as published by the Free Software Foundation; either version 2.1
9  *  of the License, or (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free
18  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  *  Boston, MA 02110-1301 USA
20  */
21
22 #include "gst/vaapi/sysdeps.h"
23 #include "gstvaapivideomemory.h"
24
25 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapivideomemory);
26 #define GST_CAT_DEFAULT gst_debug_vaapivideomemory
27
28 #ifndef GST_VIDEO_INFO_FORMAT_STRING
29 #define GST_VIDEO_INFO_FORMAT_STRING(vip) \
30     gst_video_format_to_string(GST_VIDEO_INFO_FORMAT(vip))
31 #endif
32
33 /* ------------------------------------------------------------------------ */
34 /* --- GstVaapiVideoMemory                                              --- */
35 /* ------------------------------------------------------------------------ */
36
37 static GstVaapiImage *
38 new_image(GstVaapiDisplay *display, const GstVideoInfo *vip)
39 {
40     return gst_vaapi_image_new(display, GST_VIDEO_INFO_FORMAT(vip),
41         GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
42 }
43
44 static gboolean
45 ensure_image(GstVaapiVideoMemory *mem)
46 {
47     if (!mem->image && mem->use_direct_rendering) {
48         mem->image = gst_vaapi_surface_derive_image(mem->surface);
49         if (!mem->image) {
50             GST_WARNING("failed to derive image, fallbacking to copy");
51             mem->use_direct_rendering = FALSE;
52         }
53     }
54
55     if (!mem->image) {
56         GstVaapiDisplay * const display =
57             gst_vaapi_video_meta_get_display(mem->meta);
58
59         mem->image = new_image(display, mem->image_info);
60         if (!mem->image)
61             return FALSE;
62     }
63     gst_vaapi_video_meta_set_image(mem->meta, mem->image);
64     return TRUE;
65 }
66
67 static GstVaapiSurface *
68 new_surface(GstVaapiDisplay *display, const GstVideoInfo *vip)
69 {
70     GstVaapiSurface *surface;
71
72     /* Try with explicit format first */
73     surface = gst_vaapi_surface_new_with_format(display,
74         GST_VIDEO_INFO_FORMAT(vip), GST_VIDEO_INFO_WIDTH(vip),
75         GST_VIDEO_INFO_HEIGHT(vip));
76     if (surface)
77         return surface;
78
79     /* Try to pick something compatible. Best bet: NV12 (YUV 4:2:0) */
80     if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_NV12)
81         return NULL;
82     return gst_vaapi_surface_new(display, GST_VAAPI_CHROMA_TYPE_YUV420,
83         GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
84 }
85
86 static GstVaapiSurfaceProxy *
87 new_surface_proxy(GstVaapiVideoMemory *mem)
88 {
89     GstVaapiVideoAllocator * const allocator =
90         GST_VAAPI_VIDEO_ALLOCATOR_CAST(GST_MEMORY_CAST(mem)->allocator);
91
92     return gst_vaapi_surface_proxy_new_from_pool(
93         GST_VAAPI_SURFACE_POOL(allocator->surface_pool));
94 }
95
96 static gboolean
97 ensure_surface(GstVaapiVideoMemory *mem)
98 {
99     if (!mem->proxy) {
100         gst_vaapi_surface_proxy_replace(&mem->proxy,
101             gst_vaapi_video_meta_get_surface_proxy(mem->meta));
102
103         if (!mem->proxy) {
104             mem->proxy = new_surface_proxy(mem);
105             if (!mem->proxy)
106                 return FALSE;
107             gst_vaapi_video_meta_set_surface_proxy(mem->meta, mem->proxy);
108         }
109         mem->surface = GST_VAAPI_SURFACE_PROXY_SURFACE(mem->proxy);
110     }
111     g_return_val_if_fail(mem->surface != NULL, FALSE);
112     return TRUE;
113 }
114
115 gboolean
116 gst_video_meta_map_vaapi_memory(GstVideoMeta *meta, guint plane,
117     GstMapInfo *info, gpointer *data, gint *stride, GstMapFlags flags)
118 {
119     GstVaapiVideoMemory * const mem =
120         GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_get_memory(meta->buffer, 0));
121
122     g_return_val_if_fail(mem, FALSE);
123     g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
124                              allocator), FALSE);
125     g_return_val_if_fail(mem->meta, FALSE);
126
127     if (mem->map_type &&
128         mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_PLANAR)
129         goto error_incompatible_map;
130     if ((flags & GST_MAP_READWRITE) != GST_MAP_WRITE)
131         goto error_unsupported_map;
132
133     /* Map for writing */
134     if (++mem->map_count == 1) {
135         if (!ensure_surface(mem))
136             goto error_ensure_surface;
137         if (!ensure_image(mem))
138             goto error_ensure_image;
139         if (!gst_vaapi_image_map(mem->image))
140             goto error_map_image;
141         mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_PLANAR;
142     }
143
144     *data = gst_vaapi_image_get_plane(mem->image, plane);
145     *stride = gst_vaapi_image_get_pitch(mem->image, plane);
146     info->flags = flags;
147     return TRUE;
148
149     /* ERRORS */
150 error_incompatible_map:
151     {
152         GST_ERROR("incompatible map type (%d)", mem->map_type);
153         return FALSE;
154     }
155 error_unsupported_map:
156     {
157         GST_ERROR("unsupported map flags (0x%x)", flags);
158         return FALSE;
159     }
160 error_ensure_surface:
161     {
162         const GstVideoInfo * const vip = mem->surface_info;
163         GST_ERROR("failed to create %s surface of size %ux%u",
164                   GST_VIDEO_INFO_FORMAT_STRING(vip),
165                   GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
166         return FALSE;
167     }
168 error_ensure_image:
169     {
170         const GstVideoInfo * const vip = mem->image_info;
171         GST_ERROR("failed to create %s image of size %ux%u",
172                   GST_VIDEO_INFO_FORMAT_STRING(vip),
173                   GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
174         return FALSE;
175     }
176 error_map_image:
177     {
178         GST_ERROR("failed to map image %" GST_VAAPI_ID_FORMAT,
179                   GST_VAAPI_ID_ARGS(gst_vaapi_image_get_id(mem->image)));
180         return FALSE;
181     }
182 }
183
184 gboolean
185 gst_video_meta_unmap_vaapi_memory(GstVideoMeta *meta, guint plane,
186     GstMapInfo *info)
187 {
188     GstVaapiVideoMemory * const mem =
189         GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_get_memory(meta->buffer, 0));
190
191     g_return_val_if_fail(mem, FALSE);
192     g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
193                              allocator), FALSE);
194     g_return_val_if_fail(mem->meta, FALSE);
195     g_return_val_if_fail(mem->surface, FALSE);
196     g_return_val_if_fail(mem->image, FALSE);
197
198     if (--mem->map_count == 0) {
199         mem->map_type = 0;
200
201         /* Unmap VA image used for read/writes */
202         if (info->flags & GST_MAP_READWRITE)
203             gst_vaapi_image_unmap(mem->image);
204
205         /* Commit VA image to surface */
206         if ((info->flags & GST_MAP_WRITE) && !mem->use_direct_rendering) {
207             if (!gst_vaapi_surface_put_image(mem->surface, mem->image))
208                 goto error_upload_image;
209         }
210     }
211     return TRUE;
212
213     /* ERRORS */
214 error_upload_image:
215     {
216         GST_ERROR("failed to upload image");
217         return FALSE;
218     }
219 }
220
221 GstMemory *
222 gst_vaapi_video_memory_new(GstAllocator *base_allocator,
223     GstVaapiVideoMeta *meta)
224 {
225     GstVaapiVideoAllocator * const allocator =
226         GST_VAAPI_VIDEO_ALLOCATOR_CAST(base_allocator);
227     const GstVideoInfo *vip;
228     GstVaapiVideoMemory *mem;
229
230     mem = g_slice_new(GstVaapiVideoMemory);
231     if (!mem)
232         return NULL;
233
234     vip = &allocator->image_info;
235     gst_memory_init(&mem->parent_instance, 0, gst_object_ref(allocator), NULL,
236         GST_VIDEO_INFO_SIZE(vip), 0, 0, GST_VIDEO_INFO_SIZE(vip));
237
238     mem->proxy = NULL;
239     mem->surface_info = &allocator->surface_info;
240     mem->surface = NULL;
241     mem->image_info = &allocator->image_info;
242     mem->image = NULL;
243     mem->meta = gst_vaapi_video_meta_ref(meta);
244     mem->map_type = 0;
245     mem->map_count = 0;
246     mem->use_direct_rendering = allocator->has_direct_rendering;
247     return GST_MEMORY_CAST(mem);
248 }
249
250 static void
251 gst_vaapi_video_memory_free(GstVaapiVideoMemory *mem)
252 {
253     mem->surface = NULL;
254     gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
255     gst_vaapi_object_replace(&mem->image, NULL);
256     gst_vaapi_video_meta_unref(mem->meta);
257     gst_object_unref(GST_MEMORY_CAST(mem)->allocator);
258     g_slice_free(GstVaapiVideoMemory, mem);
259 }
260
261 static gpointer
262 gst_vaapi_video_memory_map(GstVaapiVideoMemory *mem, gsize maxsize, guint flags)
263 {
264     if (mem->map_type &&
265         mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE)
266         goto error_incompatible_map;
267
268     if (mem->map_count == 0) {
269         gst_vaapi_surface_proxy_replace(&mem->proxy,
270             gst_vaapi_video_meta_get_surface_proxy(mem->meta));
271         if (!mem->proxy)
272             goto error_no_surface_proxy;
273         mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE;
274     }
275     mem->map_count++;
276     return mem->proxy;
277
278     /* ERRORS */
279 error_incompatible_map:
280     GST_ERROR("failed to map memory to a GstVaapiSurfaceProxy");
281     return NULL;
282 error_no_surface_proxy:
283     GST_ERROR("failed to extract GstVaapiSurfaceProxy from video meta");
284     return NULL;
285 }
286
287 static void
288 gst_vaapi_video_memory_unmap(GstVaapiVideoMemory *mem)
289 {
290     if (mem->map_type &&
291         mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE)
292         goto error_incompatible_map;
293
294     if (--mem->map_count == 0) {
295         gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
296         mem->map_type = 0;
297     }
298     return;
299
300     /* ERRORS */
301 error_incompatible_map:
302     GST_ERROR("incompatible map type (%d)", mem->map_type);
303     return;
304 }
305
306 static GstVaapiVideoMemory *
307 gst_vaapi_video_memory_copy(GstVaapiVideoMemory *mem,
308     gssize offset, gssize size)
309 {
310     GstMemory *out_mem;
311
312     if (offset != 0 || size != -1)
313         goto error_unsupported;
314
315     out_mem = gst_vaapi_video_memory_new(mem->parent_instance.allocator,
316         mem->meta);
317     if (!out_mem)
318         goto error_allocate_memory;
319     return GST_VAAPI_VIDEO_MEMORY_CAST(out_mem);
320
321     /* ERRORS */
322 error_unsupported:
323     GST_ERROR("failed to copy partial memory (unsupported operation)");
324     return NULL;
325 error_allocate_memory:
326     GST_ERROR("failed to allocate GstVaapiVideoMemory copy");
327     return NULL;
328 }
329
330 static GstVaapiVideoMemory *
331 gst_vaapi_video_memory_share(GstVaapiVideoMemory *mem,
332     gssize offset, gssize size)
333 {
334     GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_share() hook");
335     return NULL;
336 }
337
338 static gboolean
339 gst_vaapi_video_memory_is_span(GstVaapiVideoMemory *mem1,
340     GstVaapiVideoMemory *mem2, gsize *offset_ptr)
341 {
342     GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_is_span() hook");
343     return FALSE;
344 }
345
346 /* ------------------------------------------------------------------------ */
347 /* --- GstVaapiVideoAllocator                                           --- */
348 /* ------------------------------------------------------------------------ */
349
350 #define GST_VAAPI_VIDEO_ALLOCATOR_CLASS(klass)  \
351     (G_TYPE_CHECK_CLASS_CAST((klass),           \
352         GST_VAAPI_TYPE_VIDEO_ALLOCATOR,         \
353         GstVaapiVideoAllocatorClass))
354
355 #define GST_VAAPI_IS_VIDEO_ALLOCATOR_CLASS(klass) \
356     (G_TYPE_CHECK_CLASS_TYPE((klass), GST_VAAPI_TYPE_VIDEO_ALLOCATOR))
357
358 G_DEFINE_TYPE(GstVaapiVideoAllocator,
359               gst_vaapi_video_allocator,
360               GST_TYPE_ALLOCATOR)
361
362 static GstMemory *
363 gst_vaapi_video_allocator_alloc(GstAllocator *allocator, gsize size,
364     GstAllocationParams *params)
365 {
366     g_warning("use gst_vaapi_video_memory_new() to allocate from "
367         "GstVaapiVideoMemory allocator");
368
369     return NULL;
370 }
371
372 static void
373 gst_vaapi_video_allocator_free(GstAllocator *allocator, GstMemory *mem)
374 {
375     gst_vaapi_video_memory_free(GST_VAAPI_VIDEO_MEMORY_CAST(mem));
376 }
377
378 static void
379 gst_vaapi_video_allocator_finalize(GObject *object)
380 {
381     GstVaapiVideoAllocator * const allocator =
382         GST_VAAPI_VIDEO_ALLOCATOR_CAST(object);
383
384     gst_vaapi_video_pool_replace(&allocator->surface_pool, NULL);
385
386     G_OBJECT_CLASS(gst_vaapi_video_allocator_parent_class)->finalize(object);
387 }
388
389 static void
390 gst_vaapi_video_allocator_class_init(GstVaapiVideoAllocatorClass *klass)
391 {
392     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
393     GstAllocatorClass * const allocator_class = GST_ALLOCATOR_CLASS(klass);
394
395     GST_DEBUG_CATEGORY_INIT(gst_debug_vaapivideomemory,
396         "vaapivideomemory", 0, "VA-API video memory allocator");
397
398     object_class->finalize      = gst_vaapi_video_allocator_finalize;
399     allocator_class->alloc      = gst_vaapi_video_allocator_alloc;
400     allocator_class->free       = gst_vaapi_video_allocator_free;
401 }
402
403 static void
404 gst_vaapi_video_allocator_init(GstVaapiVideoAllocator *allocator)
405 {
406     GstAllocator * const base_allocator = GST_ALLOCATOR_CAST(allocator);
407
408     base_allocator->mem_type = GST_VAAPI_VIDEO_MEMORY_NAME;
409     base_allocator->mem_map = (GstMemoryMapFunction)
410         gst_vaapi_video_memory_map;
411     base_allocator->mem_unmap = (GstMemoryUnmapFunction)
412         gst_vaapi_video_memory_unmap;
413     base_allocator->mem_copy = (GstMemoryCopyFunction)
414         gst_vaapi_video_memory_copy;
415     base_allocator->mem_share = (GstMemoryShareFunction)
416         gst_vaapi_video_memory_share;
417     base_allocator->mem_is_span = (GstMemoryIsSpanFunction)
418         gst_vaapi_video_memory_is_span;
419 }
420
421 static gboolean
422 gst_video_info_update_from_image(GstVideoInfo *vip, GstVaapiImage *image)
423 {
424     GstVideoFormat format;
425     const guchar *data;
426     guint i, num_planes, data_size, width, height;
427
428     /* Reset format from image */
429     format = gst_vaapi_image_get_format(image);
430     gst_vaapi_image_get_size(image, &width, &height);
431     gst_video_info_set_format(vip, format, width, height);
432
433     num_planes = gst_vaapi_image_get_plane_count(image);
434     g_return_val_if_fail(num_planes == GST_VIDEO_INFO_N_PLANES(vip), FALSE);
435
436     /* Determine the base data pointer */
437     data = gst_vaapi_image_get_plane(image, 0);
438     for (i = 1; i < num_planes; i++) {
439         const guchar * const plane = gst_vaapi_image_get_plane(image, i);
440         if (data > plane)
441             data = plane;
442     }
443     data_size = gst_vaapi_image_get_data_size(image);
444
445     /* Check that we don't have disjoint planes */
446     for (i = 0; i < num_planes; i++) {
447         const guchar * const plane = gst_vaapi_image_get_plane(image, i);
448         if (plane - data > data_size)
449             return FALSE;
450     }
451
452     /* Update GstVideoInfo structure */
453     for (i = 0; i < num_planes; i++) {
454         const guchar * const plane = gst_vaapi_image_get_plane(image, i);
455         GST_VIDEO_INFO_PLANE_OFFSET(vip, i) = plane - data;
456         GST_VIDEO_INFO_PLANE_STRIDE(vip, i) =
457             gst_vaapi_image_get_pitch(image, i);
458     }
459     GST_VIDEO_INFO_SIZE(vip) = data_size;
460     return TRUE;
461 }
462
463 GstAllocator *
464 gst_vaapi_video_allocator_new(GstVaapiDisplay *display, GstCaps *caps)
465 {
466     GstVaapiVideoAllocator *allocator;
467     GstVideoInfo *vip;
468     GstVaapiSurface *surface;
469     GstVaapiImage *image;
470
471     g_return_val_if_fail(display != NULL, NULL);
472     g_return_val_if_fail(GST_IS_CAPS(caps), NULL);
473
474     allocator = g_object_new(GST_VAAPI_TYPE_VIDEO_ALLOCATOR, NULL);
475     if (!allocator)
476         return NULL;
477
478     vip = &allocator->video_info;
479     gst_video_info_init(vip);
480     gst_video_info_from_caps(vip, caps);
481
482     gst_video_info_set_format(&allocator->surface_info, GST_VIDEO_FORMAT_NV12,
483         GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
484
485     if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_ENCODED) {
486         image = NULL;
487         do {
488             surface = new_surface(display, vip);
489             if (!surface)
490                 break;
491             image = gst_vaapi_surface_derive_image(surface);
492             if (!image)
493                 break;
494             if (!gst_vaapi_image_map(image))
495                 break;
496             allocator->has_direct_rendering = gst_video_info_update_from_image(
497                 &allocator->surface_info, image);
498             gst_vaapi_image_unmap(image);
499             GST_INFO("has direct-rendering for %s surfaces: %s",
500                      GST_VIDEO_INFO_FORMAT_STRING(&allocator->surface_info),
501                      allocator->has_direct_rendering ? "yes" : "no");
502         } while (0);
503         if (surface)
504             gst_vaapi_object_unref(surface);
505         if (image)
506             gst_vaapi_object_unref(image);
507     }
508
509     allocator->surface_pool = gst_vaapi_surface_pool_new(display,
510         &allocator->surface_info);
511     if (!allocator->surface_pool)
512         goto error_create_pool;
513
514     allocator->image_info = *vip;
515     if (GST_VIDEO_INFO_FORMAT(vip) == GST_VIDEO_FORMAT_ENCODED)
516         gst_video_info_set_format(&allocator->image_info, GST_VIDEO_FORMAT_NV12,
517             GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
518
519     if (allocator->has_direct_rendering)
520         allocator->image_info = allocator->surface_info;
521     else {
522         do {
523             image = new_image(display, &allocator->image_info);
524             if (!image)
525                 break;
526             if (!gst_vaapi_image_map(image))
527                 break;
528             gst_video_info_update_from_image(&allocator->image_info, image);
529             gst_vaapi_image_unmap(image);
530         } while (0);
531         gst_vaapi_object_unref(image);
532     }
533     return GST_ALLOCATOR_CAST(allocator);
534
535     /* ERRORS */
536 error_create_pool:
537     {
538         GST_ERROR("failed to allocate VA surface pool");
539         gst_object_unref(allocator);
540         return NULL;
541     }
542 }