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 GstVaapiImageFormat format;
42 format = gst_vaapi_image_format_from_video(GST_VIDEO_INFO_FORMAT(vip));
46 return gst_vaapi_image_new(display, format,
47 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
51 ensure_image(GstVaapiVideoMemory *mem)
53 if (!mem->image && mem->use_direct_rendering) {
54 mem->image = gst_vaapi_surface_derive_image(mem->surface);
56 GST_WARNING("failed to derive image, fallbacking to copy");
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 if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_NV12)
79 return gst_vaapi_surface_new(display, GST_VAAPI_CHROMA_TYPE_YUV420,
80 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
84 ensure_surface(GstVaapiVideoMemory *mem)
87 GstVaapiDisplay * const display =
88 gst_vaapi_video_meta_get_display(mem->meta);
90 mem->surface = new_surface(display, mem->surface_info);
94 gst_vaapi_video_meta_set_surface(mem->meta, mem->surface);
99 gst_video_meta_map_vaapi_memory(GstVideoMeta *meta, guint plane,
100 GstMapInfo *info, gpointer *data, gint *stride, GstMapFlags flags)
102 GstVaapiVideoMemory * const mem =
103 GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_get_memory(meta->buffer, 0));
105 g_return_val_if_fail(mem, FALSE);
106 g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
108 g_return_val_if_fail(mem->meta, FALSE);
111 mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_PLANAR)
112 goto error_incompatible_map;
113 if ((flags & GST_MAP_READWRITE) != GST_MAP_WRITE)
114 goto error_unsupported_map;
116 /* Map for writing */
117 if (++mem->map_count == 1) {
118 if (!ensure_surface(mem))
119 goto error_ensure_surface;
120 if (!ensure_image(mem))
121 goto error_ensure_image;
122 if (!gst_vaapi_image_map(mem->image))
123 goto error_map_image;
124 mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_PLANAR;
127 *data = gst_vaapi_image_get_plane(mem->image, plane);
128 *stride = gst_vaapi_image_get_pitch(mem->image, plane);
133 error_incompatible_map:
135 GST_ERROR("incompatible map type (%d)", mem->map_type);
138 error_unsupported_map:
140 GST_ERROR("unsupported map flags (0x%x)", flags);
143 error_ensure_surface:
145 const GstVideoInfo * const vip = mem->surface_info;
146 GST_ERROR("failed to create %s surface of size %ux%u",
147 GST_VIDEO_INFO_FORMAT_STRING(vip),
148 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
153 const GstVideoInfo * const vip = mem->image_info;
154 GST_ERROR("failed to create %s image of size %ux%u",
155 GST_VIDEO_INFO_FORMAT_STRING(vip),
156 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
161 GST_ERROR("failed to map image %" GST_VAAPI_ID_FORMAT,
162 GST_VAAPI_ID_ARGS(gst_vaapi_image_get_id(mem->image)));
168 gst_video_meta_unmap_vaapi_memory(GstVideoMeta *meta, guint plane,
171 GstVaapiVideoMemory * const mem =
172 GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_get_memory(meta->buffer, 0));
174 g_return_val_if_fail(mem, FALSE);
175 g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
177 g_return_val_if_fail(mem->meta, FALSE);
178 g_return_val_if_fail(mem->surface, FALSE);
179 g_return_val_if_fail(mem->image, FALSE);
181 if (--mem->map_count == 0) {
184 /* Unmap VA image used for read/writes */
185 if (info->flags & GST_MAP_READWRITE)
186 gst_vaapi_image_unmap(mem->image);
188 /* Commit VA image to surface */
189 if ((info->flags & GST_MAP_WRITE) && !mem->use_direct_rendering) {
190 if (!gst_vaapi_surface_put_image(mem->surface, mem->image))
191 goto error_upload_image;
199 GST_ERROR("failed to upload image");
205 gst_vaapi_video_memory_new(GstAllocator *base_allocator,
206 GstVaapiVideoMeta *meta)
208 GstVaapiVideoAllocator * const allocator =
209 GST_VAAPI_VIDEO_ALLOCATOR_CAST(base_allocator);
210 const GstVideoInfo *vip;
211 GstVaapiVideoMemory *mem;
213 mem = g_slice_new(GstVaapiVideoMemory);
217 vip = &allocator->image_info;
218 gst_memory_init(&mem->parent_instance, 0, base_allocator, NULL,
219 GST_VIDEO_INFO_SIZE(vip), 0, 0, GST_VIDEO_INFO_SIZE(vip));
222 mem->surface_info = &allocator->surface_info;
224 mem->image_info = &allocator->image_info;
226 mem->meta = gst_vaapi_video_meta_ref(meta);
229 mem->use_direct_rendering = allocator->has_direct_rendering;
230 return GST_MEMORY_CAST(mem);
234 gst_vaapi_video_memory_free(GstVaapiVideoMemory *mem)
236 gst_vaapi_object_replace(&mem->surface, NULL);
237 gst_vaapi_object_replace(&mem->image, NULL);
238 gst_vaapi_video_meta_unref(mem->meta);
239 g_slice_free(GstVaapiVideoMemory, mem);
243 gst_vaapi_video_memory_map(GstVaapiVideoMemory *mem, gsize maxsize, guint flags)
246 mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE)
247 goto error_incompatible_map;
249 if (mem->map_count == 0) {
250 gst_vaapi_surface_proxy_replace(&mem->proxy,
251 gst_vaapi_video_meta_get_surface_proxy(mem->meta));
253 goto error_no_surface_proxy;
254 mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE;
260 error_incompatible_map:
261 GST_ERROR("failed to map memory to a GstVaapiSurfaceProxy");
263 error_no_surface_proxy:
264 GST_ERROR("failed to extract GstVaapiSurfaceProxy from video meta");
269 gst_vaapi_video_memory_unmap(GstVaapiVideoMemory *mem)
272 mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE)
273 goto error_incompatible_map;
275 if (--mem->map_count == 0) {
276 gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
282 error_incompatible_map:
283 GST_ERROR("incompatible map type (%d)", mem->map_type);
287 static GstVaapiVideoMemory *
288 gst_vaapi_video_memory_copy(GstVaapiVideoMemory *mem,
289 gssize offset, gssize size)
293 if (offset != 0 || size != -1)
294 goto error_unsupported;
296 out_mem = gst_vaapi_video_memory_new(mem->parent_instance.allocator,
299 goto error_allocate_memory;
300 return GST_VAAPI_VIDEO_MEMORY_CAST(out_mem);
304 GST_ERROR("failed to copy partial memory (unsupported operation)");
306 error_allocate_memory:
307 GST_ERROR("failed to allocate GstVaapiVideoMemory copy");
311 static GstVaapiVideoMemory *
312 gst_vaapi_video_memory_share(GstVaapiVideoMemory *mem,
313 gssize offset, gssize size)
315 GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_share() hook");
320 gst_vaapi_video_memory_is_span(GstVaapiVideoMemory *mem1,
321 GstVaapiVideoMemory *mem2, gsize *offset_ptr)
323 GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_is_span() hook");
327 /* ------------------------------------------------------------------------ */
328 /* --- GstVaapiVideoAllocator --- */
329 /* ------------------------------------------------------------------------ */
331 #define GST_VAAPI_VIDEO_ALLOCATOR_CLASS(klass) \
332 (G_TYPE_CHECK_CLASS_CAST((klass), \
333 GST_VAAPI_TYPE_VIDEO_ALLOCATOR, \
334 GstVaapiVideoAllocatorClass))
336 #define GST_VAAPI_IS_VIDEO_ALLOCATOR_CLASS(klass) \
337 (G_TYPE_CHECK_CLASS_TYPE((klass), GST_VAAPI_TYPE_VIDEO_ALLOCATOR))
339 G_DEFINE_TYPE(GstVaapiVideoAllocator,
340 gst_vaapi_video_allocator,
344 gst_vaapi_video_allocator_alloc(GstAllocator *allocator, gsize size,
345 GstAllocationParams *params)
347 g_warning("use gst_vaapi_video_memory_new() to allocate from "
348 "GstVaapiVideoMemory allocator");
354 gst_vaapi_video_allocator_free(GstAllocator *allocator, GstMemory *mem)
356 gst_vaapi_video_memory_free(GST_VAAPI_VIDEO_MEMORY_CAST(mem));
360 gst_vaapi_video_allocator_class_init(GstVaapiVideoAllocatorClass *klass)
362 GstAllocatorClass * const allocator_class = GST_ALLOCATOR_CLASS(klass);
364 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapivideomemory,
365 "vaapivideomemory", 0, "VA-API video memory allocator");
367 allocator_class->alloc = gst_vaapi_video_allocator_alloc;
368 allocator_class->free = gst_vaapi_video_allocator_free;
372 gst_vaapi_video_allocator_init(GstVaapiVideoAllocator *allocator)
374 GstAllocator * const base_allocator = GST_ALLOCATOR_CAST(allocator);
376 base_allocator->mem_type = GST_VAAPI_VIDEO_MEMORY_NAME;
377 base_allocator->mem_map = (GstMemoryMapFunction)
378 gst_vaapi_video_memory_map;
379 base_allocator->mem_unmap = (GstMemoryUnmapFunction)
380 gst_vaapi_video_memory_unmap;
381 base_allocator->mem_copy = (GstMemoryCopyFunction)
382 gst_vaapi_video_memory_copy;
383 base_allocator->mem_share = (GstMemoryShareFunction)
384 gst_vaapi_video_memory_share;
385 base_allocator->mem_is_span = (GstMemoryIsSpanFunction)
386 gst_vaapi_video_memory_is_span;
390 gst_video_info_update_from_image(GstVideoInfo *vip, GstVaapiImage *image)
393 guint i, num_planes, data_size;
395 num_planes = gst_vaapi_image_get_plane_count(image);
396 g_return_val_if_fail(num_planes == GST_VIDEO_INFO_N_PLANES(vip), FALSE);
398 /* Determine the base data pointer */
399 data = gst_vaapi_image_get_plane(image, 0);
400 for (i = 1; i < num_planes; i++) {
401 const guchar * const plane = gst_vaapi_image_get_plane(image, i);
405 data_size = gst_vaapi_image_get_data_size(image);
407 /* Check that we don't have disjoint planes */
408 for (i = 0; i < num_planes; i++) {
409 const guchar * const plane = gst_vaapi_image_get_plane(image, i);
410 if (plane - data > data_size)
414 /* Update GstVideoInfo structure */
415 for (i = 0; i < num_planes; i++) {
416 const guchar * const plane = gst_vaapi_image_get_plane(image, i);
417 GST_VIDEO_INFO_PLANE_OFFSET(vip, i) = plane - data;
418 GST_VIDEO_INFO_PLANE_STRIDE(vip, i) =
419 gst_vaapi_image_get_pitch(image, i);
421 GST_VIDEO_INFO_SIZE(vip) = data_size;
426 gst_vaapi_video_allocator_new(GstVaapiDisplay *display, GstCaps *caps)
428 GstVaapiVideoAllocator *allocator;
430 GstVaapiSurface *surface;
431 GstVaapiImage *image;
433 g_return_val_if_fail(display != NULL, NULL);
434 g_return_val_if_fail(GST_IS_CAPS(caps), NULL);
436 allocator = g_object_new(GST_VAAPI_TYPE_VIDEO_ALLOCATOR, NULL);
440 vip = &allocator->video_info;
441 gst_video_info_init(vip);
442 gst_video_info_from_caps(vip, caps);
444 gst_video_info_set_format(&allocator->surface_info, GST_VIDEO_FORMAT_NV12,
445 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
447 if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_ENCODED) {
450 surface = new_surface(display, vip);
453 image = gst_vaapi_surface_derive_image(surface);
456 if (!gst_vaapi_image_map(image))
458 allocator->has_direct_rendering = gst_video_info_update_from_image(
459 &allocator->surface_info, image);
460 gst_vaapi_image_unmap(image);
461 GST_INFO("has direct-rendering for %s surfaces: %s",
462 GST_VIDEO_INFO_FORMAT_STRING(&allocator->surface_info),
463 allocator->has_direct_rendering ? "yes" : "no");
466 gst_vaapi_object_unref(surface);
468 gst_vaapi_object_unref(image);
471 allocator->image_info = *vip;
472 if (GST_VIDEO_INFO_FORMAT(vip) == GST_VIDEO_FORMAT_ENCODED)
473 gst_video_info_set_format(&allocator->image_info, GST_VIDEO_FORMAT_NV12,
474 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
476 if (allocator->has_direct_rendering)
477 allocator->image_info = allocator->surface_info;
480 image = new_image(display, &allocator->image_info);
483 if (!gst_vaapi_image_map(image))
485 gst_video_info_update_from_image(&allocator->image_info, image);
486 gst_vaapi_image_unmap(image);
488 gst_vaapi_object_unref(image);
490 return GST_ALLOCATOR_CAST(allocator);