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, 0, gst_object_ref(allocator), NULL,
273 GST_VIDEO_INFO_SIZE(vip), 0, 0, GST_VIDEO_INFO_SIZE(vip));
276 mem->surface_info = &allocator->surface_info;
278 mem->image_info = &allocator->image_info;
280 mem->meta = gst_vaapi_video_meta_ref(meta);
283 mem->use_direct_rendering = allocator->has_direct_rendering;
284 return GST_MEMORY_CAST(mem);
288 gst_vaapi_video_memory_free(GstVaapiVideoMemory *mem)
291 gst_vaapi_video_memory_reset_image(mem);
292 gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
293 gst_vaapi_video_meta_unref(mem->meta);
294 gst_object_unref(GST_MEMORY_CAST(mem)->allocator);
295 g_slice_free(GstVaapiVideoMemory, mem);
299 gst_vaapi_video_memory_reset_image(GstVaapiVideoMemory *mem)
301 GstVaapiVideoAllocator * const allocator =
302 GST_VAAPI_VIDEO_ALLOCATOR_CAST(GST_MEMORY_CAST(mem)->allocator);
304 if (mem->use_direct_rendering)
305 gst_vaapi_object_replace(&mem->image, NULL);
306 else if (mem->image) {
307 gst_vaapi_video_pool_put_object(allocator->image_pool, mem->image);
313 gst_vaapi_video_memory_reset_surface(GstVaapiVideoMemory *mem)
316 gst_vaapi_video_memory_reset_image(mem);
317 gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
318 gst_vaapi_video_meta_set_surface_proxy(mem->meta, NULL);
322 gst_vaapi_video_memory_map(GstVaapiVideoMemory *mem, gsize maxsize, guint flags)
326 if (mem->map_count == 0) {
327 switch (flags & GST_MAP_READWRITE) {
329 // No flags set: return a GstVaapiSurfaceProxy
330 gst_vaapi_surface_proxy_replace(&mem->proxy,
331 gst_vaapi_video_meta_get_surface_proxy(mem->meta));
333 goto error_no_surface_proxy;
334 mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE;
337 // Only read flag set: return raw pixels
338 if (!ensure_surface(mem))
339 goto error_no_surface;
340 if (!ensure_image(mem))
342 if (!mem->use_direct_rendering)
343 gst_vaapi_surface_get_image(mem->surface, mem->image);
344 if (!gst_vaapi_image_map(mem->image))
345 goto error_map_image;
346 mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_LINEAR;
349 goto error_unsupported_map;
353 switch (mem->map_type) {
354 case GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE:
356 goto error_no_surface_proxy;
359 case GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_LINEAR:
362 data = get_image_data(mem->image);
365 goto error_unsupported_map_type;
371 error_unsupported_map:
372 GST_ERROR("unsupported map flags (0x%x)", flags);
374 error_unsupported_map_type:
375 GST_ERROR("unsupported map type (%d)", mem->map_type);
377 error_no_surface_proxy:
378 GST_ERROR("failed to extract GstVaapiSurfaceProxy from video meta");
381 GST_ERROR("failed to extract VA surface from video buffer");
384 GST_ERROR("failed to extract VA image from video buffer");
387 GST_ERROR("failed to map VA image");
392 gst_vaapi_video_memory_unmap(GstVaapiVideoMemory *mem)
394 if (mem->map_count == 1) {
395 switch (mem->map_type) {
396 case GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE:
397 gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
399 case GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_LINEAR:
400 gst_vaapi_image_unmap(mem->image);
403 goto error_incompatible_map;
411 error_incompatible_map:
412 GST_ERROR("incompatible map type (%d)", mem->map_type);
416 static GstVaapiVideoMemory *
417 gst_vaapi_video_memory_copy(GstVaapiVideoMemory *mem,
418 gssize offset, gssize size)
422 if (offset != 0 || size != -1)
423 goto error_unsupported;
425 out_mem = gst_vaapi_video_memory_new(mem->parent_instance.allocator,
428 goto error_allocate_memory;
429 return GST_VAAPI_VIDEO_MEMORY_CAST(out_mem);
433 GST_ERROR("failed to copy partial memory (unsupported operation)");
435 error_allocate_memory:
436 GST_ERROR("failed to allocate GstVaapiVideoMemory copy");
440 static GstVaapiVideoMemory *
441 gst_vaapi_video_memory_share(GstVaapiVideoMemory *mem,
442 gssize offset, gssize size)
444 GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_share() hook");
449 gst_vaapi_video_memory_is_span(GstVaapiVideoMemory *mem1,
450 GstVaapiVideoMemory *mem2, gsize *offset_ptr)
452 GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_is_span() hook");
456 /* ------------------------------------------------------------------------ */
457 /* --- GstVaapiVideoAllocator --- */
458 /* ------------------------------------------------------------------------ */
460 #define GST_VAAPI_VIDEO_ALLOCATOR_CLASS(klass) \
461 (G_TYPE_CHECK_CLASS_CAST((klass), \
462 GST_VAAPI_TYPE_VIDEO_ALLOCATOR, \
463 GstVaapiVideoAllocatorClass))
465 #define GST_VAAPI_IS_VIDEO_ALLOCATOR_CLASS(klass) \
466 (G_TYPE_CHECK_CLASS_TYPE((klass), GST_VAAPI_TYPE_VIDEO_ALLOCATOR))
468 G_DEFINE_TYPE(GstVaapiVideoAllocator,
469 gst_vaapi_video_allocator,
473 gst_vaapi_video_allocator_alloc(GstAllocator *allocator, gsize size,
474 GstAllocationParams *params)
476 g_warning("use gst_vaapi_video_memory_new() to allocate from "
477 "GstVaapiVideoMemory allocator");
483 gst_vaapi_video_allocator_free(GstAllocator *allocator, GstMemory *mem)
485 gst_vaapi_video_memory_free(GST_VAAPI_VIDEO_MEMORY_CAST(mem));
489 gst_vaapi_video_allocator_finalize(GObject *object)
491 GstVaapiVideoAllocator * const allocator =
492 GST_VAAPI_VIDEO_ALLOCATOR_CAST(object);
494 gst_vaapi_video_pool_replace(&allocator->surface_pool, NULL);
495 gst_vaapi_video_pool_replace(&allocator->image_pool, NULL);
497 G_OBJECT_CLASS(gst_vaapi_video_allocator_parent_class)->finalize(object);
501 gst_vaapi_video_allocator_class_init(GstVaapiVideoAllocatorClass *klass)
503 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
504 GstAllocatorClass * const allocator_class = GST_ALLOCATOR_CLASS(klass);
506 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapivideomemory,
507 "vaapivideomemory", 0, "VA-API video memory allocator");
509 object_class->finalize = gst_vaapi_video_allocator_finalize;
510 allocator_class->alloc = gst_vaapi_video_allocator_alloc;
511 allocator_class->free = gst_vaapi_video_allocator_free;
515 gst_vaapi_video_allocator_init(GstVaapiVideoAllocator *allocator)
517 GstAllocator * const base_allocator = GST_ALLOCATOR_CAST(allocator);
519 base_allocator->mem_type = GST_VAAPI_VIDEO_MEMORY_NAME;
520 base_allocator->mem_map = (GstMemoryMapFunction)
521 gst_vaapi_video_memory_map;
522 base_allocator->mem_unmap = (GstMemoryUnmapFunction)
523 gst_vaapi_video_memory_unmap;
524 base_allocator->mem_copy = (GstMemoryCopyFunction)
525 gst_vaapi_video_memory_copy;
526 base_allocator->mem_share = (GstMemoryShareFunction)
527 gst_vaapi_video_memory_share;
528 base_allocator->mem_is_span = (GstMemoryIsSpanFunction)
529 gst_vaapi_video_memory_is_span;
533 gst_video_info_update_from_image(GstVideoInfo *vip, GstVaapiImage *image)
535 GstVideoFormat format;
537 guint i, num_planes, data_size, width, height;
539 /* Reset format from image */
540 format = gst_vaapi_image_get_format(image);
541 gst_vaapi_image_get_size(image, &width, &height);
542 gst_video_info_set_format(vip, format, width, height);
544 num_planes = gst_vaapi_image_get_plane_count(image);
545 g_return_val_if_fail(num_planes == GST_VIDEO_INFO_N_PLANES(vip), FALSE);
547 /* Determine the base data pointer */
548 data = get_image_data(image);
549 g_return_val_if_fail(data != NULL, FALSE);
550 data_size = gst_vaapi_image_get_data_size(image);
552 /* Check that we don't have disjoint planes */
553 for (i = 0; i < num_planes; i++) {
554 const guchar * const plane = gst_vaapi_image_get_plane(image, i);
555 if (plane - data > data_size)
559 /* Update GstVideoInfo structure */
560 for (i = 0; i < num_planes; i++) {
561 const guchar * const plane = gst_vaapi_image_get_plane(image, i);
562 GST_VIDEO_INFO_PLANE_OFFSET(vip, i) = plane - data;
563 GST_VIDEO_INFO_PLANE_STRIDE(vip, i) =
564 gst_vaapi_image_get_pitch(image, i);
566 GST_VIDEO_INFO_SIZE(vip) = data_size;
571 gst_vaapi_video_allocator_new(GstVaapiDisplay *display, const GstVideoInfo *vip)
573 GstVaapiVideoAllocator *allocator;
574 GstVaapiSurface *surface;
575 GstVaapiImage *image;
577 g_return_val_if_fail(display != NULL, NULL);
578 g_return_val_if_fail(vip != NULL, NULL);
580 allocator = g_object_new(GST_VAAPI_TYPE_VIDEO_ALLOCATOR, NULL);
584 allocator->video_info = *vip;
585 gst_video_info_set_format(&allocator->surface_info, GST_VIDEO_FORMAT_NV12,
586 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
588 if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_ENCODED) {
591 surface = new_surface(display, vip);
594 image = gst_vaapi_surface_derive_image(surface);
597 if (!gst_vaapi_image_map(image))
599 allocator->has_direct_rendering = gst_video_info_update_from_image(
600 &allocator->surface_info, image);
601 if (GST_VAAPI_IMAGE_FORMAT(image) != GST_VIDEO_INFO_FORMAT(vip))
602 allocator->has_direct_rendering = FALSE;
603 gst_vaapi_image_unmap(image);
604 GST_INFO("has direct-rendering for %s surfaces: %s",
605 GST_VIDEO_INFO_FORMAT_STRING(&allocator->surface_info),
606 allocator->has_direct_rendering ? "yes" : "no");
609 gst_vaapi_object_unref(surface);
611 gst_vaapi_object_unref(image);
614 allocator->surface_pool = gst_vaapi_surface_pool_new(display,
615 &allocator->surface_info);
616 if (!allocator->surface_pool)
617 goto error_create_surface_pool;
619 allocator->image_info = *vip;
620 if (GST_VIDEO_INFO_FORMAT(vip) == GST_VIDEO_FORMAT_ENCODED)
621 gst_video_info_set_format(&allocator->image_info, GST_VIDEO_FORMAT_I420,
622 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
624 if (allocator->has_direct_rendering)
625 allocator->image_info = allocator->surface_info;
628 image = new_image(display, &allocator->image_info);
631 if (!gst_vaapi_image_map(image))
633 gst_video_info_update_from_image(&allocator->image_info, image);
634 gst_vaapi_image_unmap(image);
636 gst_vaapi_object_unref(image);
639 allocator->image_pool = gst_vaapi_image_pool_new(display,
640 &allocator->image_info);
641 if (!allocator->image_pool)
642 goto error_create_image_pool;
643 return GST_ALLOCATOR_CAST(allocator);
646 error_create_surface_pool:
648 GST_ERROR("failed to allocate VA surface pool");
649 gst_object_unref(allocator);
652 error_create_image_pool:
654 GST_ERROR("failed to allocate VA image pool");
655 gst_object_unref(allocator);