plugins: allow buffer mappings to GstVaapiSurfaceProxy.
[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  *
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     GstVaapiImageFormat format;
41
42     format = gst_vaapi_image_format_from_video(GST_VIDEO_INFO_FORMAT(vip));
43     if (!format)
44         return NULL;
45
46     return gst_vaapi_image_new(display, format,
47         GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
48 }
49
50 static gboolean
51 ensure_image(GstVaapiVideoMemory *mem)
52 {
53     if (!mem->image && mem->use_direct_rendering) {
54         mem->image = gst_vaapi_surface_derive_image(mem->surface);
55         if (!mem->image) {
56             GST_WARNING("failed to derive image, fallbacking to copy");
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     if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_NV12)
77         return NULL;
78
79     return gst_vaapi_surface_new(display, GST_VAAPI_CHROMA_TYPE_YUV420,
80         GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
81 }
82
83 static gboolean
84 ensure_surface(GstVaapiVideoMemory *mem)
85 {
86     if (!mem->surface) {
87         GstVaapiDisplay * const display =
88             gst_vaapi_video_meta_get_display(mem->meta);
89
90         mem->surface = new_surface(display, mem->surface_info);
91         if (!mem->surface)
92             return FALSE;
93     }
94     gst_vaapi_video_meta_set_surface(mem->meta, mem->surface);
95     return TRUE;
96 }
97
98 gboolean
99 gst_video_meta_map_vaapi_memory(GstVideoMeta *meta, guint plane,
100     GstMapInfo *info, gpointer *data, gint *stride, GstMapFlags flags)
101 {
102     GstVaapiVideoMemory * const mem =
103         GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_get_memory(meta->buffer, 0));
104
105     g_return_val_if_fail(mem, FALSE);
106     g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
107                              allocator), FALSE);
108     g_return_val_if_fail(mem->meta, FALSE);
109
110     if (mem->map_type &&
111         mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_PLANAR)
112         goto error_incompatible_map;
113     if ((flags & GST_MAP_READWRITE) != GST_MAP_WRITE)
114         goto error_unsupported_map;
115
116     /* Map for writing */
117     if (++mem->map_count == 1) {
118         if (!ensure_surface(mem))
119             goto error_ensure_surface;
120         if (!ensure_image(mem))
121             goto error_ensure_image;
122         if (!gst_vaapi_image_map(mem->image))
123             goto error_map_image;
124         mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_PLANAR;
125     }
126
127     *data = gst_vaapi_image_get_plane(mem->image, plane);
128     *stride = gst_vaapi_image_get_pitch(mem->image, plane);
129     info->flags = flags;
130     return TRUE;
131
132     /* ERRORS */
133 error_incompatible_map:
134     {
135         GST_ERROR("incompatible map type (%d)", mem->map_type);
136         return FALSE;
137     }
138 error_unsupported_map:
139     {
140         GST_ERROR("unsupported map flags (0x%x)", flags);
141         return FALSE;
142     }
143 error_ensure_surface:
144     {
145         const GstVideoInfo * const vip = mem->surface_info;
146         GST_ERROR("failed to create %s surface of size %ux%u",
147                   GST_VIDEO_INFO_FORMAT_STRING(vip),
148                   GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
149         return FALSE;
150     }
151 error_ensure_image:
152     {
153         const GstVideoInfo * const vip = mem->image_info;
154         GST_ERROR("failed to create %s image of size %ux%u",
155                   GST_VIDEO_INFO_FORMAT_STRING(vip),
156                   GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
157         return FALSE;
158     }
159 error_map_image:
160     {
161         GST_ERROR("failed to map image %" GST_VAAPI_ID_FORMAT,
162                   GST_VAAPI_ID_ARGS(gst_vaapi_image_get_id(mem->image)));
163         return FALSE;
164     }
165 }
166
167 gboolean
168 gst_video_meta_unmap_vaapi_memory(GstVideoMeta *meta, guint plane,
169     GstMapInfo *info)
170 {
171     GstVaapiVideoMemory * const mem =
172         GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_get_memory(meta->buffer, 0));
173
174     g_return_val_if_fail(mem, FALSE);
175     g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
176                              allocator), FALSE);
177     g_return_val_if_fail(mem->meta, FALSE);
178     g_return_val_if_fail(mem->surface, FALSE);
179     g_return_val_if_fail(mem->image, FALSE);
180
181     if (--mem->map_count == 0) {
182         mem->map_type = 0;
183
184         /* Unmap VA image used for read/writes */
185         if (info->flags & GST_MAP_READWRITE)
186             gst_vaapi_image_unmap(mem->image);
187
188         /* Commit VA image to surface */
189         if ((info->flags & GST_MAP_WRITE) && !mem->use_direct_rendering) {
190             if (!gst_vaapi_surface_put_image(mem->surface, mem->image))
191                 goto error_upload_image;
192         }
193     }
194     return TRUE;
195
196     /* ERRORS */
197 error_upload_image:
198     {
199         GST_ERROR("failed to upload image");
200         return FALSE;
201     }
202 }
203
204 GstMemory *
205 gst_vaapi_video_memory_new(GstAllocator *base_allocator,
206     GstVaapiVideoMeta *meta)
207 {
208     GstVaapiVideoAllocator * const allocator =
209         GST_VAAPI_VIDEO_ALLOCATOR_CAST(base_allocator);
210     const GstVideoInfo *vip;
211     GstVaapiVideoMemory *mem;
212
213     mem = g_slice_new(GstVaapiVideoMemory);
214     if (!mem)
215         return NULL;
216
217     vip = &allocator->image_info;
218     gst_memory_init(&mem->parent_instance, 0, base_allocator, NULL,
219         GST_VIDEO_INFO_SIZE(vip), 0, 0, GST_VIDEO_INFO_SIZE(vip));
220
221     mem->proxy = NULL;
222     mem->surface_info = &allocator->surface_info;
223     mem->surface = NULL;
224     mem->image_info = &allocator->image_info;
225     mem->image = NULL;
226     mem->meta = gst_vaapi_video_meta_ref(meta);
227     mem->map_type = 0;
228     mem->map_count = 0;
229     mem->use_direct_rendering = allocator->has_direct_rendering;
230     return GST_MEMORY_CAST(mem);
231 }
232
233 static void
234 gst_vaapi_video_memory_free(GstVaapiVideoMemory *mem)
235 {
236     gst_vaapi_object_replace(&mem->surface, NULL);
237     gst_vaapi_object_replace(&mem->image, NULL);
238     gst_vaapi_video_meta_unref(mem->meta);
239     g_slice_free(GstVaapiVideoMemory, mem);
240 }
241
242 static gpointer
243 gst_vaapi_video_memory_map(GstVaapiVideoMemory *mem, gsize maxsize, guint flags)
244 {
245     if (mem->map_type &&
246         mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE)
247         goto error_incompatible_map;
248
249     if (mem->map_count == 0) {
250         gst_vaapi_surface_proxy_replace(&mem->proxy,
251             gst_vaapi_video_meta_get_surface_proxy(mem->meta));
252         if (!mem->proxy)
253             goto error_no_surface_proxy;
254         mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE;
255     }
256     mem->map_count++;
257     return mem->proxy;
258
259     /* ERRORS */
260 error_incompatible_map:
261     GST_ERROR("failed to map memory to a GstVaapiSurfaceProxy");
262     return NULL;
263 error_no_surface_proxy:
264     GST_ERROR("failed to extract GstVaapiSurfaceProxy from video meta");
265     return NULL;
266 }
267
268 static void
269 gst_vaapi_video_memory_unmap(GstVaapiVideoMemory *mem)
270 {
271     if (mem->map_type &&
272         mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE)
273         goto error_incompatible_map;
274
275     if (--mem->map_count == 0) {
276         gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
277         mem->map_type = 0;
278     }
279     return;
280
281     /* ERRORS */
282 error_incompatible_map:
283     GST_ERROR("incompatible map type (%d)", mem->map_type);
284     return;
285 }
286
287 static GstVaapiVideoMemory *
288 gst_vaapi_video_memory_copy(GstVaapiVideoMemory *mem,
289     gssize offset, gssize size)
290 {
291     GstMemory *out_mem;
292
293     if (offset != 0 || size != -1)
294         goto error_unsupported;
295
296     out_mem = gst_vaapi_video_memory_new(mem->parent_instance.allocator,
297         mem->meta);
298     if (!out_mem)
299         goto error_allocate_memory;
300     return GST_VAAPI_VIDEO_MEMORY_CAST(out_mem);
301
302     /* ERRORS */
303 error_unsupported:
304     GST_ERROR("failed to copy partial memory (unsupported operation)");
305     return NULL;
306 error_allocate_memory:
307     GST_ERROR("failed to allocate GstVaapiVideoMemory copy");
308     return NULL;
309 }
310
311 static GstVaapiVideoMemory *
312 gst_vaapi_video_memory_share(GstVaapiVideoMemory *mem,
313     gssize offset, gssize size)
314 {
315     GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_share() hook");
316     return NULL;
317 }
318
319 static gboolean
320 gst_vaapi_video_memory_is_span(GstVaapiVideoMemory *mem1,
321     GstVaapiVideoMemory *mem2, gsize *offset_ptr)
322 {
323     GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_is_span() hook");
324     return FALSE;
325 }
326
327 /* ------------------------------------------------------------------------ */
328 /* --- GstVaapiVideoAllocator                                           --- */
329 /* ------------------------------------------------------------------------ */
330
331 #define GST_VAAPI_VIDEO_ALLOCATOR_CLASS(klass)  \
332     (G_TYPE_CHECK_CLASS_CAST((klass),           \
333         GST_VAAPI_TYPE_VIDEO_ALLOCATOR,         \
334         GstVaapiVideoAllocatorClass))
335
336 #define GST_VAAPI_IS_VIDEO_ALLOCATOR_CLASS(klass) \
337     (G_TYPE_CHECK_CLASS_TYPE((klass), GST_VAAPI_TYPE_VIDEO_ALLOCATOR))
338
339 G_DEFINE_TYPE(GstVaapiVideoAllocator,
340               gst_vaapi_video_allocator,
341               GST_TYPE_ALLOCATOR)
342
343 static GstMemory *
344 gst_vaapi_video_allocator_alloc(GstAllocator *allocator, gsize size,
345     GstAllocationParams *params)
346 {
347     g_warning("use gst_vaapi_video_memory_new() to allocate from "
348         "GstVaapiVideoMemory allocator");
349
350     return NULL;
351 }
352
353 static void
354 gst_vaapi_video_allocator_free(GstAllocator *allocator, GstMemory *mem)
355 {
356     gst_vaapi_video_memory_free(GST_VAAPI_VIDEO_MEMORY_CAST(mem));
357 }
358
359 static void
360 gst_vaapi_video_allocator_class_init(GstVaapiVideoAllocatorClass *klass)
361 {
362     GstAllocatorClass * const allocator_class = GST_ALLOCATOR_CLASS(klass);
363
364     GST_DEBUG_CATEGORY_INIT(gst_debug_vaapivideomemory,
365         "vaapivideomemory", 0, "VA-API video memory allocator");
366
367     allocator_class->alloc      = gst_vaapi_video_allocator_alloc;
368     allocator_class->free       = gst_vaapi_video_allocator_free;
369 }
370
371 static void
372 gst_vaapi_video_allocator_init(GstVaapiVideoAllocator *allocator)
373 {
374     GstAllocator * const base_allocator = GST_ALLOCATOR_CAST(allocator);
375
376     base_allocator->mem_type = GST_VAAPI_VIDEO_MEMORY_NAME;
377     base_allocator->mem_map = (GstMemoryMapFunction)
378         gst_vaapi_video_memory_map;
379     base_allocator->mem_unmap = (GstMemoryUnmapFunction)
380         gst_vaapi_video_memory_unmap;
381     base_allocator->mem_copy = (GstMemoryCopyFunction)
382         gst_vaapi_video_memory_copy;
383     base_allocator->mem_share = (GstMemoryShareFunction)
384         gst_vaapi_video_memory_share;
385     base_allocator->mem_is_span = (GstMemoryIsSpanFunction)
386         gst_vaapi_video_memory_is_span;
387 }
388
389 static gboolean
390 gst_video_info_update_from_image(GstVideoInfo *vip, GstVaapiImage *image)
391 {
392     const guchar *data;
393     guint i, num_planes, data_size;
394
395     num_planes = gst_vaapi_image_get_plane_count(image);
396     g_return_val_if_fail(num_planes == GST_VIDEO_INFO_N_PLANES(vip), FALSE);
397
398     /* Determine the base data pointer */
399     data = gst_vaapi_image_get_plane(image, 0);
400     for (i = 1; i < num_planes; i++) {
401         const guchar * const plane = gst_vaapi_image_get_plane(image, i);
402         if (data > plane)
403             data = plane;
404     }
405     data_size = gst_vaapi_image_get_data_size(image);
406
407     /* Check that we don't have disjoint planes */
408     for (i = 0; i < num_planes; i++) {
409         const guchar * const plane = gst_vaapi_image_get_plane(image, i);
410         if (plane - data > data_size)
411             return FALSE;
412     }
413
414     /* Update GstVideoInfo structure */
415     for (i = 0; i < num_planes; i++) {
416         const guchar * const plane = gst_vaapi_image_get_plane(image, i);
417         GST_VIDEO_INFO_PLANE_OFFSET(vip, i) = plane - data;
418         GST_VIDEO_INFO_PLANE_STRIDE(vip, i) =
419             gst_vaapi_image_get_pitch(image, i);
420     }
421     GST_VIDEO_INFO_SIZE(vip) = data_size;
422     return TRUE;
423 }
424
425 GstAllocator *
426 gst_vaapi_video_allocator_new(GstVaapiDisplay *display, GstCaps *caps)
427 {
428     GstVaapiVideoAllocator *allocator;
429     GstVideoInfo *vip;
430     GstVaapiSurface *surface;
431     GstVaapiImage *image;
432
433     g_return_val_if_fail(display != NULL, NULL);
434     g_return_val_if_fail(GST_IS_CAPS(caps), NULL);
435
436     allocator = g_object_new(GST_VAAPI_TYPE_VIDEO_ALLOCATOR, NULL);
437     if (!allocator)
438         return NULL;
439
440     vip = &allocator->video_info;
441     gst_video_info_init(vip);
442     gst_video_info_from_caps(vip, caps);
443
444     gst_video_info_set_format(&allocator->surface_info, GST_VIDEO_FORMAT_NV12,
445         GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
446
447     if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_ENCODED) {
448         image = NULL;
449         do {
450             surface = new_surface(display, vip);
451             if (!surface)
452                 break;
453             image = gst_vaapi_surface_derive_image(surface);
454             if (!image)
455                 break;
456             if (!gst_vaapi_image_map(image))
457                 break;
458             allocator->has_direct_rendering = gst_video_info_update_from_image(
459                 &allocator->surface_info, image);
460             gst_vaapi_image_unmap(image);
461             GST_INFO("has direct-rendering for %s surfaces: %s",
462                      GST_VIDEO_INFO_FORMAT_STRING(&allocator->surface_info),
463                      allocator->has_direct_rendering ? "yes" : "no");
464         } while (0);
465         if (surface)
466             gst_vaapi_object_unref(surface);
467         if (image)
468             gst_vaapi_object_unref(image);
469     }
470
471     allocator->image_info = *vip;
472     if (GST_VIDEO_INFO_FORMAT(vip) == GST_VIDEO_FORMAT_ENCODED)
473         gst_video_info_set_format(&allocator->image_info, GST_VIDEO_FORMAT_NV12,
474             GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
475
476     if (allocator->has_direct_rendering)
477         allocator->image_info = allocator->surface_info;
478     else {
479         do {
480             image = new_image(display, &allocator->image_info);
481             if (!image)
482                 break;
483             if (!gst_vaapi_image_map(image))
484                 break;
485             gst_video_info_update_from_image(&allocator->image_info, image);
486             gst_vaapi_image_unmap(image);
487         } while (0);
488         gst_vaapi_object_unref(image);
489     }
490     return GST_ALLOCATOR_CAST(allocator);
491 }