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);
110 if ((flags & GST_MAP_READWRITE) != GST_MAP_WRITE)
111 goto error_unsupported_map;
113 /* Map for writing */
114 if (++mem->map_count == 1) {
115 if (!ensure_surface(mem))
116 goto error_ensure_surface;
117 if (!ensure_image(mem))
118 goto error_ensure_image;
119 if (!gst_vaapi_image_map(mem->image))
120 goto error_map_image;
123 *data = gst_vaapi_image_get_plane(mem->image, plane);
124 *stride = gst_vaapi_image_get_pitch(mem->image, plane);
129 error_unsupported_map:
131 GST_ERROR("unsupported map flags (0x%x)", flags);
134 error_ensure_surface:
136 const GstVideoInfo * const vip = mem->surface_info;
137 GST_ERROR("failed to create %s surface of size %ux%u",
138 GST_VIDEO_INFO_FORMAT_STRING(vip),
139 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
144 const GstVideoInfo * const vip = mem->image_info;
145 GST_ERROR("failed to create %s image of size %ux%u",
146 GST_VIDEO_INFO_FORMAT_STRING(vip),
147 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
152 GST_ERROR("failed to map image %" GST_VAAPI_ID_FORMAT,
153 GST_VAAPI_ID_ARGS(gst_vaapi_image_get_id(mem->image)));
159 gst_video_meta_unmap_vaapi_memory(GstVideoMeta *meta, guint plane,
162 GstVaapiVideoMemory * const mem =
163 GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_get_memory(meta->buffer, 0));
165 g_return_val_if_fail(mem, FALSE);
166 g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
168 g_return_val_if_fail(mem->meta, FALSE);
169 g_return_val_if_fail(mem->surface, FALSE);
170 g_return_val_if_fail(mem->image, FALSE);
172 if (--mem->map_count == 0) {
173 /* Unmap VA image used for read/writes */
174 if (info->flags & GST_MAP_READWRITE)
175 gst_vaapi_image_unmap(mem->image);
177 /* Commit VA image to surface */
178 if ((info->flags & GST_MAP_WRITE) && !mem->use_direct_rendering) {
179 if (!gst_vaapi_surface_put_image(mem->surface, mem->image))
180 goto error_upload_image;
188 GST_ERROR("failed to upload image");
194 gst_vaapi_video_memory_new(GstAllocator *base_allocator,
195 GstVaapiVideoMeta *meta)
197 GstVaapiVideoAllocator * const allocator =
198 GST_VAAPI_VIDEO_ALLOCATOR_CAST(base_allocator);
199 const GstVideoInfo *vip;
200 GstVaapiVideoMemory *mem;
202 mem = g_slice_new(GstVaapiVideoMemory);
206 vip = &allocator->image_info;
207 gst_memory_init(&mem->parent_instance, 0, base_allocator, NULL,
208 GST_VIDEO_INFO_SIZE(vip), 0, 0, GST_VIDEO_INFO_SIZE(vip));
210 mem->surface_info = &allocator->surface_info;
212 mem->image_info = &allocator->image_info;
214 mem->meta = gst_vaapi_video_meta_ref(meta);
216 mem->use_direct_rendering = allocator->has_direct_rendering;
217 return GST_MEMORY_CAST(mem);
221 gst_vaapi_video_memory_free(GstVaapiVideoMemory *mem)
223 gst_vaapi_object_replace(&mem->surface, NULL);
224 gst_vaapi_object_replace(&mem->image, NULL);
225 gst_vaapi_video_meta_unref(mem->meta);
226 g_slice_free(GstVaapiVideoMemory, mem);
230 gst_vaapi_video_memory_map(GstVaapiVideoMemory *mem, gsize maxsize, guint flags)
232 GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_map() hook");
237 gst_vaapi_video_memory_unmap(GstVaapiVideoMemory *mem)
239 GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_unmap() hook");
242 static GstVaapiVideoMemory *
243 gst_vaapi_video_memory_copy(GstVaapiVideoMemory *mem,
244 gssize offset, gssize size)
246 GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_copy() hook");
250 static GstVaapiVideoMemory *
251 gst_vaapi_video_memory_share(GstVaapiVideoMemory *mem,
252 gssize offset, gssize size)
254 GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_share() hook");
259 gst_vaapi_video_memory_is_span(GstVaapiVideoMemory *mem1,
260 GstVaapiVideoMemory *mem2, gsize *offset_ptr)
262 GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_is_span() hook");
266 /* ------------------------------------------------------------------------ */
267 /* --- GstVaapiVideoAllocator --- */
268 /* ------------------------------------------------------------------------ */
270 #define GST_VAAPI_VIDEO_ALLOCATOR_CLASS(klass) \
271 (G_TYPE_CHECK_CLASS_CAST((klass), \
272 GST_VAAPI_TYPE_VIDEO_ALLOCATOR, \
273 GstVaapiVideoAllocatorClass))
275 #define GST_VAAPI_IS_VIDEO_ALLOCATOR_CLASS(klass) \
276 (G_TYPE_CHECK_CLASS_TYPE((klass), GST_VAAPI_TYPE_VIDEO_ALLOCATOR))
278 G_DEFINE_TYPE(GstVaapiVideoAllocator,
279 gst_vaapi_video_allocator,
283 gst_vaapi_video_allocator_alloc(GstAllocator *allocator, gsize size,
284 GstAllocationParams *params)
286 g_warning("use gst_vaapi_video_memory_new() to allocate from "
287 "GstVaapiVideoMemory allocator");
293 gst_vaapi_video_allocator_free(GstAllocator *allocator, GstMemory *mem)
295 gst_vaapi_video_memory_free(GST_VAAPI_VIDEO_MEMORY_CAST(mem));
299 gst_vaapi_video_allocator_class_init(GstVaapiVideoAllocatorClass *klass)
301 GstAllocatorClass * const allocator_class = GST_ALLOCATOR_CLASS(klass);
303 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapivideomemory,
304 "vaapivideomemory", 0, "VA-API video memory allocator");
306 allocator_class->alloc = gst_vaapi_video_allocator_alloc;
307 allocator_class->free = gst_vaapi_video_allocator_free;
311 gst_vaapi_video_allocator_init(GstVaapiVideoAllocator *allocator)
313 GstAllocator * const base_allocator = GST_ALLOCATOR_CAST(allocator);
315 base_allocator->mem_type = GST_VAAPI_VIDEO_MEMORY_NAME;
316 base_allocator->mem_map = (GstMemoryMapFunction)
317 gst_vaapi_video_memory_map;
318 base_allocator->mem_unmap = (GstMemoryUnmapFunction)
319 gst_vaapi_video_memory_unmap;
320 base_allocator->mem_copy = (GstMemoryCopyFunction)
321 gst_vaapi_video_memory_copy;
322 base_allocator->mem_share = (GstMemoryShareFunction)
323 gst_vaapi_video_memory_share;
324 base_allocator->mem_is_span = (GstMemoryIsSpanFunction)
325 gst_vaapi_video_memory_is_span;
329 gst_video_info_update_from_image(GstVideoInfo *vip, GstVaapiImage *image)
332 guint i, num_planes, data_size;
334 num_planes = gst_vaapi_image_get_plane_count(image);
335 g_return_val_if_fail(num_planes == GST_VIDEO_INFO_N_PLANES(vip), FALSE);
337 /* Determine the base data pointer */
338 data = gst_vaapi_image_get_plane(image, 0);
339 for (i = 1; i < num_planes; i++) {
340 const guchar * const plane = gst_vaapi_image_get_plane(image, i);
344 data_size = gst_vaapi_image_get_data_size(image);
346 /* Check that we don't have disjoint planes */
347 for (i = 0; i < num_planes; i++) {
348 const guchar * const plane = gst_vaapi_image_get_plane(image, i);
349 if (plane - data > data_size)
353 /* Update GstVideoInfo structure */
354 for (i = 0; i < num_planes; i++) {
355 const guchar * const plane = gst_vaapi_image_get_plane(image, i);
356 GST_VIDEO_INFO_PLANE_OFFSET(vip, i) = plane - data;
357 GST_VIDEO_INFO_PLANE_STRIDE(vip, i) =
358 gst_vaapi_image_get_pitch(image, i);
360 GST_VIDEO_INFO_SIZE(vip) = data_size;
365 gst_vaapi_video_allocator_new(GstVaapiDisplay *display, GstCaps *caps)
367 GstVaapiVideoAllocator *allocator;
369 GstVaapiSurface *surface;
370 GstVaapiImage *image;
372 g_return_val_if_fail(display != NULL, NULL);
373 g_return_val_if_fail(GST_IS_CAPS(caps), NULL);
375 allocator = g_object_new(GST_VAAPI_TYPE_VIDEO_ALLOCATOR, NULL);
379 vip = &allocator->video_info;
380 gst_video_info_init(vip);
381 gst_video_info_from_caps(vip, caps);
383 gst_video_info_set_format(&allocator->surface_info, GST_VIDEO_FORMAT_NV12,
384 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
386 if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_ENCODED) {
389 surface = new_surface(display, vip);
392 image = gst_vaapi_surface_derive_image(surface);
395 if (!gst_vaapi_image_map(image))
397 allocator->has_direct_rendering = gst_video_info_update_from_image(
398 &allocator->surface_info, image);
399 gst_vaapi_image_unmap(image);
400 GST_INFO("has direct-rendering for %s surfaces: %s",
401 GST_VIDEO_INFO_FORMAT_STRING(&allocator->surface_info),
402 allocator->has_direct_rendering ? "yes" : "no");
405 gst_vaapi_object_unref(surface);
407 gst_vaapi_object_unref(image);
410 allocator->image_info = *vip;
411 if (GST_VIDEO_INFO_FORMAT(vip) == GST_VIDEO_FORMAT_ENCODED)
412 gst_video_info_set_format(&allocator->image_info, GST_VIDEO_FORMAT_NV12,
413 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
415 if (allocator->has_direct_rendering)
416 allocator->image_info = allocator->surface_info;
419 image = new_image(display, &allocator->image_info);
422 if (!gst_vaapi_image_map(image))
424 gst_video_info_update_from_image(&allocator->image_info, image);
425 gst_vaapi_image_unmap(image);
427 gst_vaapi_object_unref(image);
429 return GST_ALLOCATOR_CAST(allocator);