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;
57 GstVaapiDisplay * const display =
58 gst_vaapi_video_meta_get_display(mem->meta);
60 mem->image = new_image(display, mem->image_info);
64 gst_vaapi_video_meta_set_image(mem->meta, mem->image);
68 static GstVaapiSurface *
69 new_surface(GstVaapiDisplay *display, const GstVideoInfo *vip)
71 GstVaapiSurface *surface;
72 GstVaapiChromaType chroma_type;
74 /* Try with explicit format first */
75 if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_ENCODED) {
76 surface = gst_vaapi_surface_new_with_format(display,
77 GST_VIDEO_INFO_FORMAT(vip), GST_VIDEO_INFO_WIDTH(vip),
78 GST_VIDEO_INFO_HEIGHT(vip));
83 /* Try to pick something compatible, i.e. with same chroma type */
84 chroma_type = gst_vaapi_video_format_get_chroma_type(
85 GST_VIDEO_INFO_FORMAT(vip));
88 return gst_vaapi_surface_new(display, chroma_type,
89 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
92 static GstVaapiSurfaceProxy *
93 new_surface_proxy(GstVaapiVideoMemory *mem)
95 GstVaapiVideoAllocator * const allocator =
96 GST_VAAPI_VIDEO_ALLOCATOR_CAST(GST_MEMORY_CAST(mem)->allocator);
98 return gst_vaapi_surface_proxy_new_from_pool(
99 GST_VAAPI_SURFACE_POOL(allocator->surface_pool));
103 ensure_surface(GstVaapiVideoMemory *mem)
106 gst_vaapi_surface_proxy_replace(&mem->proxy,
107 gst_vaapi_video_meta_get_surface_proxy(mem->meta));
110 mem->proxy = new_surface_proxy(mem);
113 gst_vaapi_video_meta_set_surface_proxy(mem->meta, mem->proxy);
115 mem->surface = GST_VAAPI_SURFACE_PROXY_SURFACE(mem->proxy);
117 g_return_val_if_fail(mem->surface != NULL, FALSE);
122 gst_video_meta_map_vaapi_memory(GstVideoMeta *meta, guint plane,
123 GstMapInfo *info, gpointer *data, gint *stride, GstMapFlags flags)
125 GstVaapiVideoMemory * const mem =
126 GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_peek_memory(meta->buffer, 0));
128 g_return_val_if_fail(mem, FALSE);
129 g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
131 g_return_val_if_fail(mem->meta, FALSE);
134 mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_PLANAR)
135 goto error_incompatible_map;
137 /* Map for writing */
138 if (++mem->map_count == 1) {
139 if (!ensure_surface(mem))
140 goto error_ensure_surface;
141 if (!ensure_image(mem))
142 goto error_ensure_image;
144 // Check that we can actually map the surface, or image
145 if ((flags & GST_MAP_READWRITE) != GST_MAP_WRITE &&
146 !mem->use_direct_rendering)
147 goto error_unsupported_map;
149 if (!gst_vaapi_image_map(mem->image))
150 goto error_map_image;
151 mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_PLANAR;
154 *data = gst_vaapi_image_get_plane(mem->image, plane);
155 *stride = gst_vaapi_image_get_pitch(mem->image, plane);
160 error_incompatible_map:
162 GST_ERROR("incompatible map type (%d)", mem->map_type);
165 error_unsupported_map:
167 GST_ERROR("unsupported map flags (0x%x)", flags);
170 error_ensure_surface:
172 const GstVideoInfo * const vip = mem->surface_info;
173 GST_ERROR("failed to create %s surface of size %ux%u",
174 GST_VIDEO_INFO_FORMAT_STRING(vip),
175 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
180 const GstVideoInfo * const vip = mem->image_info;
181 GST_ERROR("failed to create %s image of size %ux%u",
182 GST_VIDEO_INFO_FORMAT_STRING(vip),
183 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
188 GST_ERROR("failed to map image %" GST_VAAPI_ID_FORMAT,
189 GST_VAAPI_ID_ARGS(gst_vaapi_image_get_id(mem->image)));
195 gst_video_meta_unmap_vaapi_memory(GstVideoMeta *meta, guint plane,
198 GstVaapiVideoMemory * const mem =
199 GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_peek_memory(meta->buffer, 0));
201 g_return_val_if_fail(mem, FALSE);
202 g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
204 g_return_val_if_fail(mem->meta, FALSE);
205 g_return_val_if_fail(mem->surface, FALSE);
206 g_return_val_if_fail(mem->image, FALSE);
208 if (--mem->map_count == 0) {
211 /* Unmap VA image used for read/writes */
212 if (info->flags & GST_MAP_READWRITE)
213 gst_vaapi_image_unmap(mem->image);
215 /* Commit VA image to surface */
216 if ((info->flags & GST_MAP_WRITE) && !mem->use_direct_rendering) {
217 if (!gst_vaapi_surface_put_image(mem->surface, mem->image))
218 goto error_upload_image;
226 GST_ERROR("failed to upload image");
232 gst_vaapi_video_memory_new(GstAllocator *base_allocator,
233 GstVaapiVideoMeta *meta)
235 GstVaapiVideoAllocator * const allocator =
236 GST_VAAPI_VIDEO_ALLOCATOR_CAST(base_allocator);
237 const GstVideoInfo *vip;
238 GstVaapiVideoMemory *mem;
240 mem = g_slice_new(GstVaapiVideoMemory);
244 vip = &allocator->image_info;
245 gst_memory_init(&mem->parent_instance, 0, gst_object_ref(allocator), NULL,
246 GST_VIDEO_INFO_SIZE(vip), 0, 0, GST_VIDEO_INFO_SIZE(vip));
249 mem->surface_info = &allocator->surface_info;
251 mem->image_info = &allocator->image_info;
253 mem->meta = gst_vaapi_video_meta_ref(meta);
256 mem->use_direct_rendering = allocator->has_direct_rendering;
257 return GST_MEMORY_CAST(mem);
261 gst_vaapi_video_memory_free(GstVaapiVideoMemory *mem)
264 gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
265 gst_vaapi_object_replace(&mem->image, NULL);
266 gst_vaapi_video_meta_unref(mem->meta);
267 gst_object_unref(GST_MEMORY_CAST(mem)->allocator);
268 g_slice_free(GstVaapiVideoMemory, mem);
272 gst_vaapi_video_memory_reset_surface(GstVaapiVideoMemory *mem)
275 gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
276 if (mem->use_direct_rendering)
277 gst_vaapi_object_replace(&mem->image, NULL);
278 gst_vaapi_video_meta_set_surface_proxy(mem->meta, NULL);
282 gst_vaapi_video_memory_map(GstVaapiVideoMemory *mem, gsize maxsize, guint flags)
285 mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE)
286 goto error_incompatible_map;
288 if (mem->map_count == 0) {
289 gst_vaapi_surface_proxy_replace(&mem->proxy,
290 gst_vaapi_video_meta_get_surface_proxy(mem->meta));
292 goto error_no_surface_proxy;
293 mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE;
299 error_incompatible_map:
300 GST_ERROR("failed to map memory to a GstVaapiSurfaceProxy");
302 error_no_surface_proxy:
303 GST_ERROR("failed to extract GstVaapiSurfaceProxy from video meta");
308 gst_vaapi_video_memory_unmap(GstVaapiVideoMemory *mem)
311 mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE)
312 goto error_incompatible_map;
314 if (--mem->map_count == 0) {
315 gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
321 error_incompatible_map:
322 GST_ERROR("incompatible map type (%d)", mem->map_type);
326 static GstVaapiVideoMemory *
327 gst_vaapi_video_memory_copy(GstVaapiVideoMemory *mem,
328 gssize offset, gssize size)
332 if (offset != 0 || size != -1)
333 goto error_unsupported;
335 out_mem = gst_vaapi_video_memory_new(mem->parent_instance.allocator,
338 goto error_allocate_memory;
339 return GST_VAAPI_VIDEO_MEMORY_CAST(out_mem);
343 GST_ERROR("failed to copy partial memory (unsupported operation)");
345 error_allocate_memory:
346 GST_ERROR("failed to allocate GstVaapiVideoMemory copy");
350 static GstVaapiVideoMemory *
351 gst_vaapi_video_memory_share(GstVaapiVideoMemory *mem,
352 gssize offset, gssize size)
354 GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_share() hook");
359 gst_vaapi_video_memory_is_span(GstVaapiVideoMemory *mem1,
360 GstVaapiVideoMemory *mem2, gsize *offset_ptr)
362 GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_is_span() hook");
366 /* ------------------------------------------------------------------------ */
367 /* --- GstVaapiVideoAllocator --- */
368 /* ------------------------------------------------------------------------ */
370 #define GST_VAAPI_VIDEO_ALLOCATOR_CLASS(klass) \
371 (G_TYPE_CHECK_CLASS_CAST((klass), \
372 GST_VAAPI_TYPE_VIDEO_ALLOCATOR, \
373 GstVaapiVideoAllocatorClass))
375 #define GST_VAAPI_IS_VIDEO_ALLOCATOR_CLASS(klass) \
376 (G_TYPE_CHECK_CLASS_TYPE((klass), GST_VAAPI_TYPE_VIDEO_ALLOCATOR))
378 G_DEFINE_TYPE(GstVaapiVideoAllocator,
379 gst_vaapi_video_allocator,
383 gst_vaapi_video_allocator_alloc(GstAllocator *allocator, gsize size,
384 GstAllocationParams *params)
386 g_warning("use gst_vaapi_video_memory_new() to allocate from "
387 "GstVaapiVideoMemory allocator");
393 gst_vaapi_video_allocator_free(GstAllocator *allocator, GstMemory *mem)
395 gst_vaapi_video_memory_free(GST_VAAPI_VIDEO_MEMORY_CAST(mem));
399 gst_vaapi_video_allocator_finalize(GObject *object)
401 GstVaapiVideoAllocator * const allocator =
402 GST_VAAPI_VIDEO_ALLOCATOR_CAST(object);
404 gst_vaapi_video_pool_replace(&allocator->surface_pool, NULL);
406 G_OBJECT_CLASS(gst_vaapi_video_allocator_parent_class)->finalize(object);
410 gst_vaapi_video_allocator_class_init(GstVaapiVideoAllocatorClass *klass)
412 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
413 GstAllocatorClass * const allocator_class = GST_ALLOCATOR_CLASS(klass);
415 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapivideomemory,
416 "vaapivideomemory", 0, "VA-API video memory allocator");
418 object_class->finalize = gst_vaapi_video_allocator_finalize;
419 allocator_class->alloc = gst_vaapi_video_allocator_alloc;
420 allocator_class->free = gst_vaapi_video_allocator_free;
424 gst_vaapi_video_allocator_init(GstVaapiVideoAllocator *allocator)
426 GstAllocator * const base_allocator = GST_ALLOCATOR_CAST(allocator);
428 base_allocator->mem_type = GST_VAAPI_VIDEO_MEMORY_NAME;
429 base_allocator->mem_map = (GstMemoryMapFunction)
430 gst_vaapi_video_memory_map;
431 base_allocator->mem_unmap = (GstMemoryUnmapFunction)
432 gst_vaapi_video_memory_unmap;
433 base_allocator->mem_copy = (GstMemoryCopyFunction)
434 gst_vaapi_video_memory_copy;
435 base_allocator->mem_share = (GstMemoryShareFunction)
436 gst_vaapi_video_memory_share;
437 base_allocator->mem_is_span = (GstMemoryIsSpanFunction)
438 gst_vaapi_video_memory_is_span;
442 gst_video_info_update_from_image(GstVideoInfo *vip, GstVaapiImage *image)
444 GstVideoFormat format;
446 guint i, num_planes, data_size, width, height;
448 /* Reset format from image */
449 format = gst_vaapi_image_get_format(image);
450 gst_vaapi_image_get_size(image, &width, &height);
451 gst_video_info_set_format(vip, format, width, height);
453 num_planes = gst_vaapi_image_get_plane_count(image);
454 g_return_val_if_fail(num_planes == GST_VIDEO_INFO_N_PLANES(vip), FALSE);
456 /* Determine the base data pointer */
457 data = gst_vaapi_image_get_plane(image, 0);
458 for (i = 1; i < num_planes; i++) {
459 const guchar * const plane = gst_vaapi_image_get_plane(image, i);
463 data_size = gst_vaapi_image_get_data_size(image);
465 /* Check that we don't have disjoint planes */
466 for (i = 0; i < num_planes; i++) {
467 const guchar * const plane = gst_vaapi_image_get_plane(image, i);
468 if (plane - data > data_size)
472 /* Update GstVideoInfo structure */
473 for (i = 0; i < num_planes; i++) {
474 const guchar * const plane = gst_vaapi_image_get_plane(image, i);
475 GST_VIDEO_INFO_PLANE_OFFSET(vip, i) = plane - data;
476 GST_VIDEO_INFO_PLANE_STRIDE(vip, i) =
477 gst_vaapi_image_get_pitch(image, i);
479 GST_VIDEO_INFO_SIZE(vip) = data_size;
484 gst_vaapi_video_allocator_new(GstVaapiDisplay *display, const GstVideoInfo *vip)
486 GstVaapiVideoAllocator *allocator;
487 GstVaapiSurface *surface;
488 GstVaapiImage *image;
490 g_return_val_if_fail(display != NULL, NULL);
491 g_return_val_if_fail(vip != NULL, NULL);
493 allocator = g_object_new(GST_VAAPI_TYPE_VIDEO_ALLOCATOR, NULL);
497 allocator->video_info = *vip;
498 gst_video_info_set_format(&allocator->surface_info, GST_VIDEO_FORMAT_NV12,
499 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
501 if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_ENCODED) {
504 surface = new_surface(display, vip);
507 image = gst_vaapi_surface_derive_image(surface);
510 if (GST_VAAPI_IMAGE_FORMAT(image) != GST_VIDEO_INFO_FORMAT(vip))
512 if (!gst_vaapi_image_map(image))
514 allocator->has_direct_rendering = gst_video_info_update_from_image(
515 &allocator->surface_info, image);
516 gst_vaapi_image_unmap(image);
517 GST_INFO("has direct-rendering for %s surfaces: %s",
518 GST_VIDEO_INFO_FORMAT_STRING(&allocator->surface_info),
519 allocator->has_direct_rendering ? "yes" : "no");
522 gst_vaapi_object_unref(surface);
524 gst_vaapi_object_unref(image);
527 allocator->surface_pool = gst_vaapi_surface_pool_new(display,
528 &allocator->surface_info);
529 if (!allocator->surface_pool)
530 goto error_create_pool;
532 allocator->image_info = *vip;
533 if (GST_VIDEO_INFO_FORMAT(vip) == GST_VIDEO_FORMAT_ENCODED)
534 gst_video_info_set_format(&allocator->image_info, GST_VIDEO_FORMAT_NV12,
535 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
537 if (allocator->has_direct_rendering)
538 allocator->image_info = allocator->surface_info;
541 image = new_image(display, &allocator->image_info);
544 if (!gst_vaapi_image_map(image))
546 gst_video_info_update_from_image(&allocator->image_info, image);
547 gst_vaapi_image_unmap(image);
549 gst_vaapi_object_unref(image);
551 return GST_ALLOCATOR_CAST(allocator);
556 GST_ERROR("failed to allocate VA surface pool");
557 gst_object_unref(allocator);