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