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 "gstvaapivideomemory.h"
26 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapivideomemory);
27 #define GST_CAT_DEFAULT gst_debug_vaapivideomemory
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))
34 /* ------------------------------------------------------------------------ */
35 /* --- GstVaapiVideoMemory --- */
36 /* ------------------------------------------------------------------------ */
38 static GstVaapiImage *
39 new_image(GstVaapiDisplay *display, const GstVideoInfo *vip)
41 return gst_vaapi_image_new(display, GST_VIDEO_INFO_FORMAT(vip),
42 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
46 ensure_image(GstVaapiVideoMemory *mem)
48 if (!mem->image && mem->use_direct_rendering) {
49 mem->image = gst_vaapi_surface_derive_image(mem->surface);
51 GST_WARNING("failed to derive image, fallbacking to copy");
52 mem->use_direct_rendering = FALSE;
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;
62 GstVaapiDisplay * const display =
63 gst_vaapi_video_meta_get_display(mem->meta);
65 mem->image = new_image(display, mem->image_info);
69 gst_vaapi_video_meta_set_image(mem->meta, mem->image);
73 static GstVaapiSurface *
74 new_surface(GstVaapiDisplay *display, const GstVideoInfo *vip)
76 GstVaapiSurface *surface;
77 GstVaapiChromaType chroma_type;
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));
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));
93 return gst_vaapi_surface_new(display, chroma_type,
94 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
97 static GstVaapiSurfaceProxy *
98 new_surface_proxy(GstVaapiVideoMemory *mem)
100 GstVaapiVideoAllocator * const allocator =
101 GST_VAAPI_VIDEO_ALLOCATOR_CAST(GST_MEMORY_CAST(mem)->allocator);
103 return gst_vaapi_surface_proxy_new_from_pool(
104 GST_VAAPI_SURFACE_POOL(allocator->surface_pool));
108 ensure_surface(GstVaapiVideoMemory *mem)
111 gst_vaapi_surface_proxy_replace(&mem->proxy,
112 gst_vaapi_video_meta_get_surface_proxy(mem->meta));
115 mem->proxy = new_surface_proxy(mem);
118 gst_vaapi_video_meta_set_surface_proxy(mem->meta, mem->proxy);
121 mem->surface = GST_VAAPI_SURFACE_PROXY_SURFACE(mem->proxy);
122 return mem->surface != NULL;
126 gst_video_meta_map_vaapi_memory(GstVideoMeta *meta, guint plane,
127 GstMapInfo *info, gpointer *data, gint *stride, GstMapFlags flags)
129 GstVaapiVideoMemory * const mem =
130 GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_peek_memory(meta->buffer, 0));
132 g_return_val_if_fail(mem, FALSE);
133 g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
135 g_return_val_if_fail(mem->meta, FALSE);
138 mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_PLANAR)
139 goto error_incompatible_map;
141 /* Map for writing */
142 if (++mem->map_count == 1) {
143 if (!ensure_surface(mem))
144 goto error_ensure_surface;
145 if (!ensure_image(mem))
146 goto error_ensure_image;
148 // Check that we can actually map the surface, or image
149 if ((flags & GST_MAP_READWRITE) == GST_MAP_WRITE &&
150 !mem->use_direct_rendering)
151 goto error_unsupported_map;
153 // Load VA image from surface
154 if ((flags & GST_MAP_READ) && !mem->use_direct_rendering)
155 gst_vaapi_surface_get_image(mem->surface, mem->image);
157 if (!gst_vaapi_image_map(mem->image))
158 goto error_map_image;
159 mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_PLANAR;
162 *data = gst_vaapi_image_get_plane(mem->image, plane);
163 *stride = gst_vaapi_image_get_pitch(mem->image, plane);
168 error_incompatible_map:
170 GST_ERROR("incompatible map type (%d)", mem->map_type);
173 error_unsupported_map:
175 GST_ERROR("unsupported map flags (0x%x)", flags);
178 error_ensure_surface:
180 const GstVideoInfo * const vip = mem->surface_info;
181 GST_ERROR("failed to create %s surface of size %ux%u",
182 GST_VIDEO_INFO_FORMAT_STRING(vip),
183 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
188 const GstVideoInfo * const vip = mem->image_info;
189 GST_ERROR("failed to create %s image of size %ux%u",
190 GST_VIDEO_INFO_FORMAT_STRING(vip),
191 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
196 GST_ERROR("failed to map image %" GST_VAAPI_ID_FORMAT,
197 GST_VAAPI_ID_ARGS(gst_vaapi_image_get_id(mem->image)));
203 gst_video_meta_unmap_vaapi_memory(GstVideoMeta *meta, guint plane,
206 GstVaapiVideoMemory * const mem =
207 GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_peek_memory(meta->buffer, 0));
209 g_return_val_if_fail(mem, FALSE);
210 g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
212 g_return_val_if_fail(mem->meta, FALSE);
213 g_return_val_if_fail(mem->surface, FALSE);
214 g_return_val_if_fail(mem->image, FALSE);
216 if (--mem->map_count == 0) {
219 /* Unmap VA image used for read/writes */
220 if (info->flags & GST_MAP_READWRITE)
221 gst_vaapi_image_unmap(mem->image);
223 /* Commit VA image to surface */
224 if ((info->flags & GST_MAP_WRITE) && !mem->use_direct_rendering) {
225 if (!gst_vaapi_surface_put_image(mem->surface, mem->image))
226 goto error_upload_image;
234 GST_ERROR("failed to upload image");
240 gst_vaapi_video_memory_new(GstAllocator *base_allocator,
241 GstVaapiVideoMeta *meta)
243 GstVaapiVideoAllocator * const allocator =
244 GST_VAAPI_VIDEO_ALLOCATOR_CAST(base_allocator);
245 const GstVideoInfo *vip;
246 GstVaapiVideoMemory *mem;
248 mem = g_slice_new(GstVaapiVideoMemory);
252 vip = &allocator->image_info;
253 gst_memory_init(&mem->parent_instance, 0, gst_object_ref(allocator), NULL,
254 GST_VIDEO_INFO_SIZE(vip), 0, 0, GST_VIDEO_INFO_SIZE(vip));
257 mem->surface_info = &allocator->surface_info;
259 mem->image_info = &allocator->image_info;
261 mem->meta = gst_vaapi_video_meta_ref(meta);
264 mem->use_direct_rendering = allocator->has_direct_rendering;
265 return GST_MEMORY_CAST(mem);
269 gst_vaapi_video_memory_free(GstVaapiVideoMemory *mem)
272 gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
273 gst_vaapi_object_replace(&mem->image, NULL);
274 gst_vaapi_video_meta_unref(mem->meta);
275 gst_object_unref(GST_MEMORY_CAST(mem)->allocator);
276 g_slice_free(GstVaapiVideoMemory, mem);
280 gst_vaapi_video_memory_reset_surface(GstVaapiVideoMemory *mem)
283 gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
284 if (mem->use_direct_rendering)
285 gst_vaapi_object_replace(&mem->image, NULL);
286 gst_vaapi_video_meta_set_surface_proxy(mem->meta, NULL);
290 gst_vaapi_video_memory_map(GstVaapiVideoMemory *mem, gsize maxsize, guint flags)
293 mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE)
294 goto error_incompatible_map;
296 if (mem->map_count == 0) {
297 gst_vaapi_surface_proxy_replace(&mem->proxy,
298 gst_vaapi_video_meta_get_surface_proxy(mem->meta));
300 goto error_no_surface_proxy;
301 mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE;
307 error_incompatible_map:
308 GST_ERROR("failed to map memory to a GstVaapiSurfaceProxy");
310 error_no_surface_proxy:
311 GST_ERROR("failed to extract GstVaapiSurfaceProxy from video meta");
316 gst_vaapi_video_memory_unmap(GstVaapiVideoMemory *mem)
319 mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE)
320 goto error_incompatible_map;
322 if (--mem->map_count == 0) {
323 gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
329 error_incompatible_map:
330 GST_ERROR("incompatible map type (%d)", mem->map_type);
334 static GstVaapiVideoMemory *
335 gst_vaapi_video_memory_copy(GstVaapiVideoMemory *mem,
336 gssize offset, gssize size)
340 if (offset != 0 || size != -1)
341 goto error_unsupported;
343 out_mem = gst_vaapi_video_memory_new(mem->parent_instance.allocator,
346 goto error_allocate_memory;
347 return GST_VAAPI_VIDEO_MEMORY_CAST(out_mem);
351 GST_ERROR("failed to copy partial memory (unsupported operation)");
353 error_allocate_memory:
354 GST_ERROR("failed to allocate GstVaapiVideoMemory copy");
358 static GstVaapiVideoMemory *
359 gst_vaapi_video_memory_share(GstVaapiVideoMemory *mem,
360 gssize offset, gssize size)
362 GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_share() hook");
367 gst_vaapi_video_memory_is_span(GstVaapiVideoMemory *mem1,
368 GstVaapiVideoMemory *mem2, gsize *offset_ptr)
370 GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_is_span() hook");
374 /* ------------------------------------------------------------------------ */
375 /* --- GstVaapiVideoAllocator --- */
376 /* ------------------------------------------------------------------------ */
378 #define GST_VAAPI_VIDEO_ALLOCATOR_CLASS(klass) \
379 (G_TYPE_CHECK_CLASS_CAST((klass), \
380 GST_VAAPI_TYPE_VIDEO_ALLOCATOR, \
381 GstVaapiVideoAllocatorClass))
383 #define GST_VAAPI_IS_VIDEO_ALLOCATOR_CLASS(klass) \
384 (G_TYPE_CHECK_CLASS_TYPE((klass), GST_VAAPI_TYPE_VIDEO_ALLOCATOR))
386 G_DEFINE_TYPE(GstVaapiVideoAllocator,
387 gst_vaapi_video_allocator,
391 gst_vaapi_video_allocator_alloc(GstAllocator *allocator, gsize size,
392 GstAllocationParams *params)
394 g_warning("use gst_vaapi_video_memory_new() to allocate from "
395 "GstVaapiVideoMemory allocator");
401 gst_vaapi_video_allocator_free(GstAllocator *allocator, GstMemory *mem)
403 gst_vaapi_video_memory_free(GST_VAAPI_VIDEO_MEMORY_CAST(mem));
407 gst_vaapi_video_allocator_finalize(GObject *object)
409 GstVaapiVideoAllocator * const allocator =
410 GST_VAAPI_VIDEO_ALLOCATOR_CAST(object);
412 gst_vaapi_video_pool_replace(&allocator->surface_pool, NULL);
414 G_OBJECT_CLASS(gst_vaapi_video_allocator_parent_class)->finalize(object);
418 gst_vaapi_video_allocator_class_init(GstVaapiVideoAllocatorClass *klass)
420 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
421 GstAllocatorClass * const allocator_class = GST_ALLOCATOR_CLASS(klass);
423 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapivideomemory,
424 "vaapivideomemory", 0, "VA-API video memory allocator");
426 object_class->finalize = gst_vaapi_video_allocator_finalize;
427 allocator_class->alloc = gst_vaapi_video_allocator_alloc;
428 allocator_class->free = gst_vaapi_video_allocator_free;
432 gst_vaapi_video_allocator_init(GstVaapiVideoAllocator *allocator)
434 GstAllocator * const base_allocator = GST_ALLOCATOR_CAST(allocator);
436 base_allocator->mem_type = GST_VAAPI_VIDEO_MEMORY_NAME;
437 base_allocator->mem_map = (GstMemoryMapFunction)
438 gst_vaapi_video_memory_map;
439 base_allocator->mem_unmap = (GstMemoryUnmapFunction)
440 gst_vaapi_video_memory_unmap;
441 base_allocator->mem_copy = (GstMemoryCopyFunction)
442 gst_vaapi_video_memory_copy;
443 base_allocator->mem_share = (GstMemoryShareFunction)
444 gst_vaapi_video_memory_share;
445 base_allocator->mem_is_span = (GstMemoryIsSpanFunction)
446 gst_vaapi_video_memory_is_span;
450 gst_video_info_update_from_image(GstVideoInfo *vip, GstVaapiImage *image)
452 GstVideoFormat format;
454 guint i, num_planes, data_size, width, height;
456 /* Reset format from image */
457 format = gst_vaapi_image_get_format(image);
458 gst_vaapi_image_get_size(image, &width, &height);
459 gst_video_info_set_format(vip, format, width, height);
461 num_planes = gst_vaapi_image_get_plane_count(image);
462 g_return_val_if_fail(num_planes == GST_VIDEO_INFO_N_PLANES(vip), FALSE);
464 /* Determine the base data pointer */
465 data = gst_vaapi_image_get_plane(image, 0);
466 for (i = 1; i < num_planes; i++) {
467 const guchar * const plane = gst_vaapi_image_get_plane(image, i);
471 data_size = gst_vaapi_image_get_data_size(image);
473 /* Check that we don't have disjoint planes */
474 for (i = 0; i < num_planes; i++) {
475 const guchar * const plane = gst_vaapi_image_get_plane(image, i);
476 if (plane - data > data_size)
480 /* Update GstVideoInfo structure */
481 for (i = 0; i < num_planes; i++) {
482 const guchar * const plane = gst_vaapi_image_get_plane(image, i);
483 GST_VIDEO_INFO_PLANE_OFFSET(vip, i) = plane - data;
484 GST_VIDEO_INFO_PLANE_STRIDE(vip, i) =
485 gst_vaapi_image_get_pitch(image, i);
487 GST_VIDEO_INFO_SIZE(vip) = data_size;
492 gst_vaapi_video_allocator_new(GstVaapiDisplay *display, const GstVideoInfo *vip)
494 GstVaapiVideoAllocator *allocator;
495 GstVaapiSurface *surface;
496 GstVaapiImage *image;
498 g_return_val_if_fail(display != NULL, NULL);
499 g_return_val_if_fail(vip != NULL, NULL);
501 allocator = g_object_new(GST_VAAPI_TYPE_VIDEO_ALLOCATOR, NULL);
505 allocator->video_info = *vip;
506 gst_video_info_set_format(&allocator->surface_info, GST_VIDEO_FORMAT_NV12,
507 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
509 if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_ENCODED) {
512 surface = new_surface(display, vip);
515 image = gst_vaapi_surface_derive_image(surface);
518 if (GST_VAAPI_IMAGE_FORMAT(image) != GST_VIDEO_INFO_FORMAT(vip))
520 if (!gst_vaapi_image_map(image))
522 allocator->has_direct_rendering = gst_video_info_update_from_image(
523 &allocator->surface_info, image);
524 gst_vaapi_image_unmap(image);
525 GST_INFO("has direct-rendering for %s surfaces: %s",
526 GST_VIDEO_INFO_FORMAT_STRING(&allocator->surface_info),
527 allocator->has_direct_rendering ? "yes" : "no");
530 gst_vaapi_object_unref(surface);
532 gst_vaapi_object_unref(image);
535 allocator->surface_pool = gst_vaapi_surface_pool_new(display,
536 &allocator->surface_info);
537 if (!allocator->surface_pool)
538 goto error_create_pool;
540 allocator->image_info = *vip;
541 if (GST_VIDEO_INFO_FORMAT(vip) == GST_VIDEO_FORMAT_ENCODED)
542 gst_video_info_set_format(&allocator->image_info, GST_VIDEO_FORMAT_I420,
543 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
545 if (allocator->has_direct_rendering)
546 allocator->image_info = allocator->surface_info;
549 image = new_image(display, &allocator->image_info);
552 if (!gst_vaapi_image_map(image))
554 gst_video_info_update_from_image(&allocator->image_info, image);
555 gst_vaapi_image_unmap(image);
557 gst_vaapi_object_unref(image);
559 return GST_ALLOCATOR_CAST(allocator);
564 GST_ERROR("failed to allocate VA surface pool");
565 gst_object_unref(allocator);