2 * gstvaapivideomemory.c - Gstreamer/VA video memory
4 * Copyright (C) 2013 Intel Corporation
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1
9 * of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA
22 #include "gst/vaapi/sysdeps.h"
23 #include "gstvaapivideomemory.h"
25 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapivideomemory);
26 #define GST_CAT_DEFAULT gst_debug_vaapivideomemory
28 #ifndef GST_VIDEO_INFO_FORMAT_STRING
29 #define GST_VIDEO_INFO_FORMAT_STRING(vip) \
30 gst_video_format_to_string(GST_VIDEO_INFO_FORMAT(vip))
33 /* ------------------------------------------------------------------------ */
34 /* --- GstVaapiVideoMemory --- */
35 /* ------------------------------------------------------------------------ */
37 static GstVaapiImage *
38 new_image(GstVaapiDisplay *display, const GstVideoInfo *vip)
40 return gst_vaapi_image_new(display, GST_VIDEO_INFO_FORMAT(vip),
41 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
45 ensure_image(GstVaapiVideoMemory *mem)
47 if (!mem->image && mem->use_direct_rendering) {
48 mem->image = gst_vaapi_surface_derive_image(mem->surface);
50 GST_WARNING("failed to derive image, fallbacking to copy");
51 mem->use_direct_rendering = FALSE;
56 GstVaapiDisplay * const display =
57 gst_vaapi_video_meta_get_display(mem->meta);
59 mem->image = new_image(display, mem->image_info);
63 gst_vaapi_video_meta_set_image(mem->meta, mem->image);
67 static GstVaapiSurface *
68 new_surface(GstVaapiDisplay *display, const GstVideoInfo *vip)
70 GstVaapiSurface *surface;
72 /* Try with explicit format first */
73 surface = gst_vaapi_surface_new_with_format(display,
74 GST_VIDEO_INFO_FORMAT(vip), GST_VIDEO_INFO_WIDTH(vip),
75 GST_VIDEO_INFO_HEIGHT(vip));
79 /* Try to pick something compatible. Best bet: NV12 (YUV 4:2:0) */
80 if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_NV12)
82 return gst_vaapi_surface_new(display, GST_VAAPI_CHROMA_TYPE_YUV420,
83 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
87 ensure_surface(GstVaapiVideoMemory *mem)
90 GstVaapiDisplay * const display =
91 gst_vaapi_video_meta_get_display(mem->meta);
93 mem->surface = new_surface(display, mem->surface_info);
97 gst_vaapi_video_meta_set_surface(mem->meta, mem->surface);
102 gst_video_meta_map_vaapi_memory(GstVideoMeta *meta, guint plane,
103 GstMapInfo *info, gpointer *data, gint *stride, GstMapFlags flags)
105 GstVaapiVideoMemory * const mem =
106 GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_get_memory(meta->buffer, 0));
108 g_return_val_if_fail(mem, FALSE);
109 g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
111 g_return_val_if_fail(mem->meta, FALSE);
114 mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_PLANAR)
115 goto error_incompatible_map;
116 if ((flags & GST_MAP_READWRITE) != GST_MAP_WRITE)
117 goto error_unsupported_map;
119 /* Map for writing */
120 if (++mem->map_count == 1) {
121 if (!ensure_surface(mem))
122 goto error_ensure_surface;
123 if (!ensure_image(mem))
124 goto error_ensure_image;
125 if (!gst_vaapi_image_map(mem->image))
126 goto error_map_image;
127 mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_PLANAR;
130 *data = gst_vaapi_image_get_plane(mem->image, plane);
131 *stride = gst_vaapi_image_get_pitch(mem->image, plane);
136 error_incompatible_map:
138 GST_ERROR("incompatible map type (%d)", mem->map_type);
141 error_unsupported_map:
143 GST_ERROR("unsupported map flags (0x%x)", flags);
146 error_ensure_surface:
148 const GstVideoInfo * const vip = mem->surface_info;
149 GST_ERROR("failed to create %s surface of size %ux%u",
150 GST_VIDEO_INFO_FORMAT_STRING(vip),
151 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
156 const GstVideoInfo * const vip = mem->image_info;
157 GST_ERROR("failed to create %s image of size %ux%u",
158 GST_VIDEO_INFO_FORMAT_STRING(vip),
159 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
164 GST_ERROR("failed to map image %" GST_VAAPI_ID_FORMAT,
165 GST_VAAPI_ID_ARGS(gst_vaapi_image_get_id(mem->image)));
171 gst_video_meta_unmap_vaapi_memory(GstVideoMeta *meta, guint plane,
174 GstVaapiVideoMemory * const mem =
175 GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_get_memory(meta->buffer, 0));
177 g_return_val_if_fail(mem, FALSE);
178 g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
180 g_return_val_if_fail(mem->meta, FALSE);
181 g_return_val_if_fail(mem->surface, FALSE);
182 g_return_val_if_fail(mem->image, FALSE);
184 if (--mem->map_count == 0) {
187 /* Unmap VA image used for read/writes */
188 if (info->flags & GST_MAP_READWRITE)
189 gst_vaapi_image_unmap(mem->image);
191 /* Commit VA image to surface */
192 if ((info->flags & GST_MAP_WRITE) && !mem->use_direct_rendering) {
193 if (!gst_vaapi_surface_put_image(mem->surface, mem->image))
194 goto error_upload_image;
202 GST_ERROR("failed to upload image");
208 gst_vaapi_video_memory_new(GstAllocator *base_allocator,
209 GstVaapiVideoMeta *meta)
211 GstVaapiVideoAllocator * const allocator =
212 GST_VAAPI_VIDEO_ALLOCATOR_CAST(base_allocator);
213 const GstVideoInfo *vip;
214 GstVaapiVideoMemory *mem;
216 mem = g_slice_new(GstVaapiVideoMemory);
220 vip = &allocator->image_info;
221 gst_memory_init(&mem->parent_instance, 0, base_allocator, NULL,
222 GST_VIDEO_INFO_SIZE(vip), 0, 0, GST_VIDEO_INFO_SIZE(vip));
225 mem->surface_info = &allocator->surface_info;
227 mem->image_info = &allocator->image_info;
229 mem->meta = gst_vaapi_video_meta_ref(meta);
232 mem->use_direct_rendering = allocator->has_direct_rendering;
233 return GST_MEMORY_CAST(mem);
237 gst_vaapi_video_memory_free(GstVaapiVideoMemory *mem)
239 gst_vaapi_object_replace(&mem->surface, NULL);
240 gst_vaapi_object_replace(&mem->image, NULL);
241 gst_vaapi_video_meta_unref(mem->meta);
242 g_slice_free(GstVaapiVideoMemory, mem);
246 gst_vaapi_video_memory_map(GstVaapiVideoMemory *mem, gsize maxsize, guint flags)
249 mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE)
250 goto error_incompatible_map;
252 if (mem->map_count == 0) {
253 gst_vaapi_surface_proxy_replace(&mem->proxy,
254 gst_vaapi_video_meta_get_surface_proxy(mem->meta));
256 goto error_no_surface_proxy;
257 mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE;
263 error_incompatible_map:
264 GST_ERROR("failed to map memory to a GstVaapiSurfaceProxy");
266 error_no_surface_proxy:
267 GST_ERROR("failed to extract GstVaapiSurfaceProxy from video meta");
272 gst_vaapi_video_memory_unmap(GstVaapiVideoMemory *mem)
275 mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE)
276 goto error_incompatible_map;
278 if (--mem->map_count == 0) {
279 gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
285 error_incompatible_map:
286 GST_ERROR("incompatible map type (%d)", mem->map_type);
290 static GstVaapiVideoMemory *
291 gst_vaapi_video_memory_copy(GstVaapiVideoMemory *mem,
292 gssize offset, gssize size)
296 if (offset != 0 || size != -1)
297 goto error_unsupported;
299 out_mem = gst_vaapi_video_memory_new(mem->parent_instance.allocator,
302 goto error_allocate_memory;
303 return GST_VAAPI_VIDEO_MEMORY_CAST(out_mem);
307 GST_ERROR("failed to copy partial memory (unsupported operation)");
309 error_allocate_memory:
310 GST_ERROR("failed to allocate GstVaapiVideoMemory copy");
314 static GstVaapiVideoMemory *
315 gst_vaapi_video_memory_share(GstVaapiVideoMemory *mem,
316 gssize offset, gssize size)
318 GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_share() hook");
323 gst_vaapi_video_memory_is_span(GstVaapiVideoMemory *mem1,
324 GstVaapiVideoMemory *mem2, gsize *offset_ptr)
326 GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_is_span() hook");
330 /* ------------------------------------------------------------------------ */
331 /* --- GstVaapiVideoAllocator --- */
332 /* ------------------------------------------------------------------------ */
334 #define GST_VAAPI_VIDEO_ALLOCATOR_CLASS(klass) \
335 (G_TYPE_CHECK_CLASS_CAST((klass), \
336 GST_VAAPI_TYPE_VIDEO_ALLOCATOR, \
337 GstVaapiVideoAllocatorClass))
339 #define GST_VAAPI_IS_VIDEO_ALLOCATOR_CLASS(klass) \
340 (G_TYPE_CHECK_CLASS_TYPE((klass), GST_VAAPI_TYPE_VIDEO_ALLOCATOR))
342 G_DEFINE_TYPE(GstVaapiVideoAllocator,
343 gst_vaapi_video_allocator,
347 gst_vaapi_video_allocator_alloc(GstAllocator *allocator, gsize size,
348 GstAllocationParams *params)
350 g_warning("use gst_vaapi_video_memory_new() to allocate from "
351 "GstVaapiVideoMemory allocator");
357 gst_vaapi_video_allocator_free(GstAllocator *allocator, GstMemory *mem)
359 gst_vaapi_video_memory_free(GST_VAAPI_VIDEO_MEMORY_CAST(mem));
363 gst_vaapi_video_allocator_class_init(GstVaapiVideoAllocatorClass *klass)
365 GstAllocatorClass * const allocator_class = GST_ALLOCATOR_CLASS(klass);
367 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapivideomemory,
368 "vaapivideomemory", 0, "VA-API video memory allocator");
370 allocator_class->alloc = gst_vaapi_video_allocator_alloc;
371 allocator_class->free = gst_vaapi_video_allocator_free;
375 gst_vaapi_video_allocator_init(GstVaapiVideoAllocator *allocator)
377 GstAllocator * const base_allocator = GST_ALLOCATOR_CAST(allocator);
379 base_allocator->mem_type = GST_VAAPI_VIDEO_MEMORY_NAME;
380 base_allocator->mem_map = (GstMemoryMapFunction)
381 gst_vaapi_video_memory_map;
382 base_allocator->mem_unmap = (GstMemoryUnmapFunction)
383 gst_vaapi_video_memory_unmap;
384 base_allocator->mem_copy = (GstMemoryCopyFunction)
385 gst_vaapi_video_memory_copy;
386 base_allocator->mem_share = (GstMemoryShareFunction)
387 gst_vaapi_video_memory_share;
388 base_allocator->mem_is_span = (GstMemoryIsSpanFunction)
389 gst_vaapi_video_memory_is_span;
393 gst_video_info_update_from_image(GstVideoInfo *vip, GstVaapiImage *image)
395 GstVideoFormat format;
397 guint i, num_planes, data_size, width, height;
399 /* Reset format from image */
400 format = gst_vaapi_image_get_format(image);
401 gst_vaapi_image_get_size(image, &width, &height);
402 gst_video_info_set_format(vip, format, width, height);
404 num_planes = gst_vaapi_image_get_plane_count(image);
405 g_return_val_if_fail(num_planes == GST_VIDEO_INFO_N_PLANES(vip), FALSE);
407 /* Determine the base data pointer */
408 data = gst_vaapi_image_get_plane(image, 0);
409 for (i = 1; i < num_planes; i++) {
410 const guchar * const plane = gst_vaapi_image_get_plane(image, i);
414 data_size = gst_vaapi_image_get_data_size(image);
416 /* Check that we don't have disjoint planes */
417 for (i = 0; i < num_planes; i++) {
418 const guchar * const plane = gst_vaapi_image_get_plane(image, i);
419 if (plane - data > data_size)
423 /* Update GstVideoInfo structure */
424 for (i = 0; i < num_planes; i++) {
425 const guchar * const plane = gst_vaapi_image_get_plane(image, i);
426 GST_VIDEO_INFO_PLANE_OFFSET(vip, i) = plane - data;
427 GST_VIDEO_INFO_PLANE_STRIDE(vip, i) =
428 gst_vaapi_image_get_pitch(image, i);
430 GST_VIDEO_INFO_SIZE(vip) = data_size;
435 gst_vaapi_video_allocator_new(GstVaapiDisplay *display, GstCaps *caps)
437 GstVaapiVideoAllocator *allocator;
439 GstVaapiSurface *surface;
440 GstVaapiImage *image;
442 g_return_val_if_fail(display != NULL, NULL);
443 g_return_val_if_fail(GST_IS_CAPS(caps), NULL);
445 allocator = g_object_new(GST_VAAPI_TYPE_VIDEO_ALLOCATOR, NULL);
449 vip = &allocator->video_info;
450 gst_video_info_init(vip);
451 gst_video_info_from_caps(vip, caps);
453 gst_video_info_set_format(&allocator->surface_info, GST_VIDEO_FORMAT_NV12,
454 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
456 if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_ENCODED) {
459 surface = new_surface(display, vip);
462 image = gst_vaapi_surface_derive_image(surface);
465 if (!gst_vaapi_image_map(image))
467 allocator->has_direct_rendering = gst_video_info_update_from_image(
468 &allocator->surface_info, image);
469 gst_vaapi_image_unmap(image);
470 GST_INFO("has direct-rendering for %s surfaces: %s",
471 GST_VIDEO_INFO_FORMAT_STRING(&allocator->surface_info),
472 allocator->has_direct_rendering ? "yes" : "no");
475 gst_vaapi_object_unref(surface);
477 gst_vaapi_object_unref(image);
480 allocator->image_info = *vip;
481 if (GST_VIDEO_INFO_FORMAT(vip) == GST_VIDEO_FORMAT_ENCODED)
482 gst_video_info_set_format(&allocator->image_info, GST_VIDEO_FORMAT_NV12,
483 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
485 if (allocator->has_direct_rendering)
486 allocator->image_info = allocator->surface_info;
489 image = new_image(display, &allocator->image_info);
492 if (!gst_vaapi_image_map(image))
494 gst_video_info_update_from_image(&allocator->image_info, image);
495 gst_vaapi_image_unmap(image);
497 gst_vaapi_object_unref(image);
499 return GST_ALLOCATOR_CAST(allocator);