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