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