2 * gstvaapivideomemory.c - Gstreamer/VA video memory
4 * Copyright (C) 2013 Intel Corporation
5 * Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
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.
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.
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
23 #include "gst/vaapi/sysdeps.h"
24 #include <gst/vaapi/gstvaapisurfacepool.h>
25 #include <gst/vaapi/gstvaapiimagepool.h>
26 #include "gstvaapivideomemory.h"
28 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapivideomemory);
29 #define GST_CAT_DEFAULT gst_debug_vaapivideomemory
31 #ifndef GST_VIDEO_INFO_FORMAT_STRING
32 #define GST_VIDEO_INFO_FORMAT_STRING(vip) \
33 gst_video_format_to_string(GST_VIDEO_INFO_FORMAT(vip))
36 /* ------------------------------------------------------------------------ */
37 /* --- GstVaapiVideoMemory --- */
38 /* ------------------------------------------------------------------------ */
41 gst_vaapi_video_memory_reset_image(GstVaapiVideoMemory *mem);
44 get_image_data(GstVaapiImage *image)
49 data = gst_vaapi_image_get_plane(image, 0);
50 if (!data || !gst_vaapi_image_get_image(image, &va_image))
53 data -= va_image.offsets[0];
57 static GstVaapiImage *
58 new_image(GstVaapiDisplay *display, const GstVideoInfo *vip)
60 return gst_vaapi_image_new(display, GST_VIDEO_INFO_FORMAT(vip),
61 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
65 ensure_image(GstVaapiVideoMemory *mem)
67 if (!mem->image && mem->use_direct_rendering) {
68 mem->image = gst_vaapi_surface_derive_image(mem->surface);
70 GST_WARNING("failed to derive image, fallbacking to copy");
71 mem->use_direct_rendering = FALSE;
73 else if (gst_vaapi_surface_get_format(mem->surface) !=
74 GST_VIDEO_INFO_FORMAT(mem->image_info)) {
75 gst_vaapi_object_replace(&mem->image, NULL);
76 mem->use_direct_rendering = FALSE;
81 GstVaapiVideoAllocator * const allocator =
82 GST_VAAPI_VIDEO_ALLOCATOR_CAST(GST_MEMORY_CAST(mem)->allocator);
84 mem->image = gst_vaapi_video_pool_get_object(allocator->image_pool);
88 gst_vaapi_video_meta_set_image(mem->meta, mem->image);
92 static GstVaapiSurface *
93 new_surface(GstVaapiDisplay *display, const GstVideoInfo *vip)
95 GstVaapiSurface *surface;
96 GstVaapiChromaType chroma_type;
98 /* Try with explicit format first */
99 if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_ENCODED) {
100 surface = gst_vaapi_surface_new_with_format(display,
101 GST_VIDEO_INFO_FORMAT(vip), GST_VIDEO_INFO_WIDTH(vip),
102 GST_VIDEO_INFO_HEIGHT(vip));
107 /* Try to pick something compatible, i.e. with same chroma type */
108 chroma_type = gst_vaapi_video_format_get_chroma_type(
109 GST_VIDEO_INFO_FORMAT(vip));
112 return gst_vaapi_surface_new(display, chroma_type,
113 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
116 static GstVaapiSurfaceProxy *
117 new_surface_proxy(GstVaapiVideoMemory *mem)
119 GstVaapiVideoAllocator * const allocator =
120 GST_VAAPI_VIDEO_ALLOCATOR_CAST(GST_MEMORY_CAST(mem)->allocator);
122 return gst_vaapi_surface_proxy_new_from_pool(
123 GST_VAAPI_SURFACE_POOL(allocator->surface_pool));
127 ensure_surface(GstVaapiVideoMemory *mem)
130 gst_vaapi_surface_proxy_replace(&mem->proxy,
131 gst_vaapi_video_meta_get_surface_proxy(mem->meta));
134 mem->proxy = new_surface_proxy(mem);
137 gst_vaapi_video_meta_set_surface_proxy(mem->meta, mem->proxy);
140 mem->surface = GST_VAAPI_SURFACE_PROXY_SURFACE(mem->proxy);
141 return mem->surface != NULL;
145 gst_video_meta_map_vaapi_memory(GstVideoMeta *meta, guint plane,
146 GstMapInfo *info, gpointer *data, gint *stride, GstMapFlags flags)
148 GstVaapiVideoMemory * const mem =
149 GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_peek_memory(meta->buffer, 0));
151 g_return_val_if_fail(mem, FALSE);
152 g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
154 g_return_val_if_fail(mem->meta, FALSE);
157 mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_PLANAR)
158 goto error_incompatible_map;
160 /* Map for writing */
161 if (++mem->map_count == 1) {
162 if (!ensure_surface(mem))
163 goto error_ensure_surface;
164 if (!ensure_image(mem))
165 goto error_ensure_image;
167 // Check that we can actually map the surface, or image
168 if ((flags & GST_MAP_READWRITE) == GST_MAP_READWRITE &&
169 !mem->use_direct_rendering)
170 goto error_unsupported_map;
172 // Load VA image from surface
173 if ((flags & GST_MAP_READ) && !mem->use_direct_rendering)
174 gst_vaapi_surface_get_image(mem->surface, mem->image);
176 if (!gst_vaapi_image_map(mem->image))
177 goto error_map_image;
178 mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_PLANAR;
181 *data = gst_vaapi_image_get_plane(mem->image, plane);
182 *stride = gst_vaapi_image_get_pitch(mem->image, plane);
187 error_incompatible_map:
189 GST_ERROR("incompatible map type (%d)", mem->map_type);
192 error_unsupported_map:
194 GST_ERROR("unsupported map flags (0x%x)", flags);
197 error_ensure_surface:
199 const GstVideoInfo * const vip = mem->surface_info;
200 GST_ERROR("failed to create %s surface of size %ux%u",
201 GST_VIDEO_INFO_FORMAT_STRING(vip),
202 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
207 const GstVideoInfo * const vip = mem->image_info;
208 GST_ERROR("failed to create %s image of size %ux%u",
209 GST_VIDEO_INFO_FORMAT_STRING(vip),
210 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
215 GST_ERROR("failed to map image %" GST_VAAPI_ID_FORMAT,
216 GST_VAAPI_ID_ARGS(gst_vaapi_image_get_id(mem->image)));
222 gst_video_meta_unmap_vaapi_memory(GstVideoMeta *meta, guint plane,
225 GstVaapiVideoMemory * const mem =
226 GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_peek_memory(meta->buffer, 0));
228 g_return_val_if_fail(mem, FALSE);
229 g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
231 g_return_val_if_fail(mem->meta, FALSE);
232 g_return_val_if_fail(mem->surface, FALSE);
233 g_return_val_if_fail(mem->image, FALSE);
235 if (--mem->map_count == 0) {
238 /* Unmap VA image used for read/writes */
239 if (info->flags & GST_MAP_READWRITE)
240 gst_vaapi_image_unmap(mem->image);
242 /* Commit VA image to surface */
243 if ((info->flags & GST_MAP_WRITE) && !mem->use_direct_rendering) {
244 if (!gst_vaapi_surface_put_image(mem->surface, mem->image))
245 goto error_upload_image;
253 GST_ERROR("failed to upload image");
259 gst_vaapi_video_memory_new(GstAllocator *base_allocator,
260 GstVaapiVideoMeta *meta)
262 GstVaapiVideoAllocator * const allocator =
263 GST_VAAPI_VIDEO_ALLOCATOR_CAST(base_allocator);
264 const GstVideoInfo *vip;
265 GstVaapiVideoMemory *mem;
267 mem = g_slice_new(GstVaapiVideoMemory);
271 vip = &allocator->image_info;
272 gst_memory_init(&mem->parent_instance, GST_MEMORY_FLAG_NO_SHARE,
273 gst_object_ref(allocator), NULL, GST_VIDEO_INFO_SIZE(vip), 0,
274 0, GST_VIDEO_INFO_SIZE(vip));
277 mem->surface_info = &allocator->surface_info;
279 mem->image_info = &allocator->image_info;
281 mem->meta = gst_vaapi_video_meta_ref(meta);
284 mem->use_direct_rendering = allocator->has_direct_rendering;
285 return GST_MEMORY_CAST(mem);
289 gst_vaapi_video_memory_free(GstVaapiVideoMemory *mem)
292 gst_vaapi_video_memory_reset_image(mem);
293 gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
294 gst_vaapi_video_meta_unref(mem->meta);
295 gst_object_unref(GST_MEMORY_CAST(mem)->allocator);
296 g_slice_free(GstVaapiVideoMemory, mem);
300 gst_vaapi_video_memory_reset_image(GstVaapiVideoMemory *mem)
302 GstVaapiVideoAllocator * const allocator =
303 GST_VAAPI_VIDEO_ALLOCATOR_CAST(GST_MEMORY_CAST(mem)->allocator);
305 if (mem->use_direct_rendering)
306 gst_vaapi_object_replace(&mem->image, NULL);
307 else if (mem->image) {
308 gst_vaapi_video_pool_put_object(allocator->image_pool, mem->image);
314 gst_vaapi_video_memory_reset_surface(GstVaapiVideoMemory *mem)
317 gst_vaapi_video_memory_reset_image(mem);
318 gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
319 gst_vaapi_video_meta_set_surface_proxy(mem->meta, NULL);
323 gst_vaapi_video_memory_map(GstVaapiVideoMemory *mem, gsize maxsize, guint flags)
327 if (mem->map_count == 0) {
328 switch (flags & GST_MAP_READWRITE) {
330 // No flags set: return a GstVaapiSurfaceProxy
331 gst_vaapi_surface_proxy_replace(&mem->proxy,
332 gst_vaapi_video_meta_get_surface_proxy(mem->meta));
334 goto error_no_surface_proxy;
335 mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE;
338 // Only read flag set: return raw pixels
339 if (!ensure_surface(mem))
340 goto error_no_surface;
341 if (!ensure_image(mem))
343 if (!mem->use_direct_rendering)
344 gst_vaapi_surface_get_image(mem->surface, mem->image);
345 if (!gst_vaapi_image_map(mem->image))
346 goto error_map_image;
347 mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_LINEAR;
350 goto error_unsupported_map;
354 switch (mem->map_type) {
355 case GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE:
357 goto error_no_surface_proxy;
360 case GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_LINEAR:
363 data = get_image_data(mem->image);
366 goto error_unsupported_map_type;
372 error_unsupported_map:
373 GST_ERROR("unsupported map flags (0x%x)", flags);
375 error_unsupported_map_type:
376 GST_ERROR("unsupported map type (%d)", mem->map_type);
378 error_no_surface_proxy:
379 GST_ERROR("failed to extract GstVaapiSurfaceProxy from video meta");
382 GST_ERROR("failed to extract VA surface from video buffer");
385 GST_ERROR("failed to extract VA image from video buffer");
388 GST_ERROR("failed to map VA image");
393 gst_vaapi_video_memory_unmap(GstVaapiVideoMemory *mem)
395 if (mem->map_count == 1) {
396 switch (mem->map_type) {
397 case GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE:
398 gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
400 case GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_LINEAR:
401 gst_vaapi_image_unmap(mem->image);
404 goto error_incompatible_map;
412 error_incompatible_map:
413 GST_ERROR("incompatible map type (%d)", mem->map_type);
417 static GstVaapiVideoMemory *
418 gst_vaapi_video_memory_copy(GstVaapiVideoMemory *mem,
419 gssize offset, gssize size)
421 GstVaapiVideoMeta *meta;
425 /* XXX: this implements a soft-copy, i.e. underlying VA surfaces
427 (void)gst_memory_get_sizes(GST_MEMORY_CAST(mem), NULL, &maxsize);
428 if (offset != 0 || (size != -1 && (gsize)size != maxsize))
429 goto error_unsupported;
431 meta = gst_vaapi_video_meta_copy(mem->meta);
433 goto error_allocate_memory;
435 out_mem = gst_vaapi_video_memory_new(GST_MEMORY_CAST(mem)->allocator, meta);
436 gst_vaapi_video_meta_unref(meta);
438 goto error_allocate_memory;
439 return GST_VAAPI_VIDEO_MEMORY_CAST(out_mem);
443 GST_ERROR("failed to copy partial memory (unsupported operation)");
445 error_allocate_memory:
446 GST_ERROR("failed to allocate GstVaapiVideoMemory copy");
450 static GstVaapiVideoMemory *
451 gst_vaapi_video_memory_share(GstVaapiVideoMemory *mem,
452 gssize offset, gssize size)
454 GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_share() hook");
459 gst_vaapi_video_memory_is_span(GstVaapiVideoMemory *mem1,
460 GstVaapiVideoMemory *mem2, gsize *offset_ptr)
462 GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_is_span() hook");
466 /* ------------------------------------------------------------------------ */
467 /* --- GstVaapiVideoAllocator --- */
468 /* ------------------------------------------------------------------------ */
470 #define GST_VAAPI_VIDEO_ALLOCATOR_CLASS(klass) \
471 (G_TYPE_CHECK_CLASS_CAST((klass), \
472 GST_VAAPI_TYPE_VIDEO_ALLOCATOR, \
473 GstVaapiVideoAllocatorClass))
475 #define GST_VAAPI_IS_VIDEO_ALLOCATOR_CLASS(klass) \
476 (G_TYPE_CHECK_CLASS_TYPE((klass), GST_VAAPI_TYPE_VIDEO_ALLOCATOR))
478 G_DEFINE_TYPE(GstVaapiVideoAllocator,
479 gst_vaapi_video_allocator,
483 gst_vaapi_video_allocator_alloc(GstAllocator *allocator, gsize size,
484 GstAllocationParams *params)
486 g_warning("use gst_vaapi_video_memory_new() to allocate from "
487 "GstVaapiVideoMemory allocator");
493 gst_vaapi_video_allocator_free(GstAllocator *allocator, GstMemory *mem)
495 gst_vaapi_video_memory_free(GST_VAAPI_VIDEO_MEMORY_CAST(mem));
499 gst_vaapi_video_allocator_finalize(GObject *object)
501 GstVaapiVideoAllocator * const allocator =
502 GST_VAAPI_VIDEO_ALLOCATOR_CAST(object);
504 gst_vaapi_video_pool_replace(&allocator->surface_pool, NULL);
505 gst_vaapi_video_pool_replace(&allocator->image_pool, NULL);
507 G_OBJECT_CLASS(gst_vaapi_video_allocator_parent_class)->finalize(object);
511 gst_vaapi_video_allocator_class_init(GstVaapiVideoAllocatorClass *klass)
513 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
514 GstAllocatorClass * const allocator_class = GST_ALLOCATOR_CLASS(klass);
516 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapivideomemory,
517 "vaapivideomemory", 0, "VA-API video memory allocator");
519 object_class->finalize = gst_vaapi_video_allocator_finalize;
520 allocator_class->alloc = gst_vaapi_video_allocator_alloc;
521 allocator_class->free = gst_vaapi_video_allocator_free;
525 gst_vaapi_video_allocator_init(GstVaapiVideoAllocator *allocator)
527 GstAllocator * const base_allocator = GST_ALLOCATOR_CAST(allocator);
529 base_allocator->mem_type = GST_VAAPI_VIDEO_MEMORY_NAME;
530 base_allocator->mem_map = (GstMemoryMapFunction)
531 gst_vaapi_video_memory_map;
532 base_allocator->mem_unmap = (GstMemoryUnmapFunction)
533 gst_vaapi_video_memory_unmap;
534 base_allocator->mem_copy = (GstMemoryCopyFunction)
535 gst_vaapi_video_memory_copy;
536 base_allocator->mem_share = (GstMemoryShareFunction)
537 gst_vaapi_video_memory_share;
538 base_allocator->mem_is_span = (GstMemoryIsSpanFunction)
539 gst_vaapi_video_memory_is_span;
543 gst_video_info_update_from_image(GstVideoInfo *vip, GstVaapiImage *image)
545 GstVideoFormat format;
547 guint i, num_planes, data_size, width, height;
549 /* Reset format from image */
550 format = gst_vaapi_image_get_format(image);
551 gst_vaapi_image_get_size(image, &width, &height);
552 gst_video_info_set_format(vip, format, width, height);
554 num_planes = gst_vaapi_image_get_plane_count(image);
555 g_return_val_if_fail(num_planes == GST_VIDEO_INFO_N_PLANES(vip), FALSE);
557 /* Determine the base data pointer */
558 data = get_image_data(image);
559 g_return_val_if_fail(data != NULL, FALSE);
560 data_size = gst_vaapi_image_get_data_size(image);
562 /* Check that we don't have disjoint planes */
563 for (i = 0; i < num_planes; i++) {
564 const guchar * const plane = gst_vaapi_image_get_plane(image, i);
565 if (plane - data > data_size)
569 /* Update GstVideoInfo structure */
570 for (i = 0; i < num_planes; i++) {
571 const guchar * const plane = gst_vaapi_image_get_plane(image, i);
572 GST_VIDEO_INFO_PLANE_OFFSET(vip, i) = plane - data;
573 GST_VIDEO_INFO_PLANE_STRIDE(vip, i) =
574 gst_vaapi_image_get_pitch(image, i);
576 GST_VIDEO_INFO_SIZE(vip) = data_size;
581 gst_vaapi_video_allocator_new(GstVaapiDisplay *display, const GstVideoInfo *vip)
583 GstVaapiVideoAllocator *allocator;
584 GstVaapiSurface *surface;
585 GstVaapiImage *image;
587 g_return_val_if_fail(display != NULL, NULL);
588 g_return_val_if_fail(vip != NULL, NULL);
590 allocator = g_object_new(GST_VAAPI_TYPE_VIDEO_ALLOCATOR, NULL);
594 allocator->video_info = *vip;
595 gst_video_info_set_format(&allocator->surface_info, GST_VIDEO_FORMAT_NV12,
596 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
598 if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_ENCODED) {
601 surface = new_surface(display, vip);
604 image = gst_vaapi_surface_derive_image(surface);
607 if (!gst_vaapi_image_map(image))
609 allocator->has_direct_rendering = gst_video_info_update_from_image(
610 &allocator->surface_info, image);
611 if (GST_VAAPI_IMAGE_FORMAT(image) != GST_VIDEO_INFO_FORMAT(vip))
612 allocator->has_direct_rendering = FALSE;
613 gst_vaapi_image_unmap(image);
614 GST_INFO("has direct-rendering for %s surfaces: %s",
615 GST_VIDEO_INFO_FORMAT_STRING(&allocator->surface_info),
616 allocator->has_direct_rendering ? "yes" : "no");
619 gst_vaapi_object_unref(surface);
621 gst_vaapi_object_unref(image);
624 allocator->surface_pool = gst_vaapi_surface_pool_new(display,
625 &allocator->surface_info);
626 if (!allocator->surface_pool)
627 goto error_create_surface_pool;
629 allocator->image_info = *vip;
630 if (GST_VIDEO_INFO_FORMAT(vip) == GST_VIDEO_FORMAT_ENCODED)
631 gst_video_info_set_format(&allocator->image_info, GST_VIDEO_FORMAT_I420,
632 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
634 if (allocator->has_direct_rendering)
635 allocator->image_info = allocator->surface_info;
638 image = new_image(display, &allocator->image_info);
641 if (!gst_vaapi_image_map(image))
643 gst_video_info_update_from_image(&allocator->image_info, image);
644 gst_vaapi_image_unmap(image);
646 gst_vaapi_object_unref(image);
649 allocator->image_pool = gst_vaapi_image_pool_new(display,
650 &allocator->image_info);
651 if (!allocator->image_pool)
652 goto error_create_image_pool;
653 return GST_ALLOCATOR_CAST(allocator);
656 error_create_surface_pool:
658 GST_ERROR("failed to allocate VA surface pool");
659 gst_object_unref(allocator);
662 error_create_image_pool:
664 GST_ERROR("failed to allocate VA image pool");
665 gst_object_unref(allocator);