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_READ)
111 goto error_unsupported_map;
113 if (++mem->map_count == 1) {
114 if (!ensure_surface(mem))
115 goto error_ensure_surface;
116 if (!ensure_image(mem))
117 goto error_ensure_image;
118 if (!gst_vaapi_image_map(mem->image))
119 goto error_map_image;
122 *data = gst_vaapi_image_get_plane(mem->image, plane);
123 *stride = gst_vaapi_image_get_pitch(mem->image, plane);
128 error_unsupported_map:
130 GST_ERROR("unsupported map flags (0x%x)", flags);
133 error_ensure_surface:
135 const GstVideoInfo * const vip = mem->surface_info;
136 GST_ERROR("failed to create %s surface of size %ux%u",
137 GST_VIDEO_INFO_FORMAT_STRING(vip),
138 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
143 const GstVideoInfo * const vip = mem->image_info;
144 GST_ERROR("failed to create %s image of size %ux%u",
145 GST_VIDEO_INFO_FORMAT_STRING(vip),
146 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
151 GST_ERROR("failed to map image %" GST_VAAPI_ID_FORMAT,
152 GST_VAAPI_ID_ARGS(gst_vaapi_image_get_id(mem->image)));
158 gst_video_meta_unmap_vaapi_memory(GstVideoMeta *meta, guint plane,
161 GstVaapiVideoMemory * const mem =
162 GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_get_memory(meta->buffer, 0));
164 g_return_val_if_fail(mem, FALSE);
165 g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
167 g_return_val_if_fail(mem->meta, FALSE);
168 g_return_val_if_fail(mem->surface, FALSE);
169 g_return_val_if_fail(mem->image, FALSE);
171 if (--mem->map_count == 0) {
172 gst_vaapi_image_unmap(mem->image);
174 /* Commit VA image to surface */
175 if ((info->flags & GST_MAP_WRITE) && !mem->use_direct_rendering) {
176 if (!gst_vaapi_surface_put_image(mem->surface, mem->image))
177 goto error_upload_image;
185 GST_ERROR("failed to upload image");
191 gst_vaapi_video_memory_new(GstAllocator *base_allocator,
192 GstVaapiVideoMeta *meta)
194 GstVaapiVideoAllocator * const allocator =
195 GST_VAAPI_VIDEO_ALLOCATOR_CAST(base_allocator);
196 const GstVideoInfo *vip;
197 GstVaapiVideoMemory *mem;
199 mem = g_slice_new(GstVaapiVideoMemory);
203 vip = &allocator->image_info;
204 gst_memory_init(&mem->parent_instance, 0, base_allocator, NULL,
205 GST_VIDEO_INFO_SIZE(vip), 0, 0, GST_VIDEO_INFO_SIZE(vip));
207 mem->surface_info = &allocator->surface_info;
209 mem->image_info = &allocator->image_info;
211 mem->meta = gst_vaapi_video_meta_ref(meta);
213 mem->use_direct_rendering = allocator->has_direct_rendering;
214 return GST_MEMORY_CAST(mem);
218 gst_vaapi_video_memory_free(GstVaapiVideoMemory *mem)
220 g_clear_object(&mem->surface);
221 g_clear_object(&mem->image);
222 gst_vaapi_video_meta_unref(mem->meta);
223 g_slice_free(GstVaapiVideoMemory, mem);
227 gst_vaapi_video_memory_map(GstVaapiVideoMemory *mem, gsize maxsize, guint flags)
229 GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_map() hook");
234 gst_vaapi_video_memory_unmap(GstVaapiVideoMemory *mem)
236 GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_unmap() hook");
239 static GstVaapiVideoMemory *
240 gst_vaapi_video_memory_copy(GstVaapiVideoMemory *mem,
241 gssize offset, gssize size)
243 GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_copy() hook");
247 static GstVaapiVideoMemory *
248 gst_vaapi_video_memory_share(GstVaapiVideoMemory *mem,
249 gssize offset, gssize size)
251 GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_share() hook");
256 gst_vaapi_video_memory_is_span(GstVaapiVideoMemory *mem1,
257 GstVaapiVideoMemory *mem2, gsize *offset_ptr)
259 GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_is_span() hook");
263 /* ------------------------------------------------------------------------ */
264 /* --- GstVaapiVideoAllocator --- */
265 /* ------------------------------------------------------------------------ */
267 #define GST_VAAPI_VIDEO_ALLOCATOR_CLASS(klass) \
268 (G_TYPE_CHECK_CLASS_CAST((klass), \
269 GST_VAAPI_TYPE_VIDEO_ALLOCATOR, \
270 GstVaapiVideoAllocatorClass))
272 #define GST_VAAPI_IS_VIDEO_ALLOCATOR_CLASS(klass) \
273 (G_TYPE_CHECK_CLASS_TYPE((klass), GST_VAAPI_TYPE_VIDEO_ALLOCATOR))
275 G_DEFINE_TYPE(GstVaapiVideoAllocator,
276 gst_vaapi_video_allocator,
280 gst_vaapi_video_allocator_alloc(GstAllocator *allocator, gsize size,
281 GstAllocationParams *params)
283 g_warning("use gst_vaapi_video_memory_new() to allocate from "
284 "GstVaapiVideoMemory allocator");
290 gst_vaapi_video_allocator_free(GstAllocator *allocator, GstMemory *mem)
292 gst_vaapi_video_memory_free(GST_VAAPI_VIDEO_MEMORY_CAST(mem));
296 gst_vaapi_video_allocator_class_init(GstVaapiVideoAllocatorClass *klass)
298 GstAllocatorClass * const allocator_class = GST_ALLOCATOR_CLASS(klass);
300 GST_DEBUG_CATEGORY_INIT(gst_debug_vaapivideomemory,
301 "vaapivideomemory", 0, "VA-API video memory allocator");
303 allocator_class->alloc = gst_vaapi_video_allocator_alloc;
304 allocator_class->free = gst_vaapi_video_allocator_free;
308 gst_vaapi_video_allocator_init(GstVaapiVideoAllocator *allocator)
310 GstAllocator * const base_allocator = GST_ALLOCATOR_CAST(allocator);
312 base_allocator->mem_type = GST_VAAPI_VIDEO_MEMORY_NAME;
313 base_allocator->mem_map = (GstMemoryMapFunction)
314 gst_vaapi_video_memory_map;
315 base_allocator->mem_unmap = (GstMemoryUnmapFunction)
316 gst_vaapi_video_memory_unmap;
317 base_allocator->mem_copy = (GstMemoryCopyFunction)
318 gst_vaapi_video_memory_copy;
319 base_allocator->mem_share = (GstMemoryShareFunction)
320 gst_vaapi_video_memory_share;
321 base_allocator->mem_is_span = (GstMemoryIsSpanFunction)
322 gst_vaapi_video_memory_is_span;
326 gst_video_info_update_from_image(GstVideoInfo *vip, GstVaapiImage *image)
329 guint i, num_planes, data_size;
331 num_planes = gst_vaapi_image_get_plane_count(image);
332 g_return_val_if_fail(num_planes == GST_VIDEO_INFO_N_PLANES(vip), FALSE);
334 /* Determine the base data pointer */
335 data = gst_vaapi_image_get_plane(image, 0);
336 for (i = 1; i < num_planes; i++) {
337 const guchar * const plane = gst_vaapi_image_get_plane(image, i);
341 data_size = gst_vaapi_image_get_data_size(image);
343 /* Check that we don't have disjoint planes */
344 for (i = 0; i < num_planes; i++) {
345 const guchar * const plane = gst_vaapi_image_get_plane(image, i);
346 if (plane - data > data_size)
350 /* Update GstVideoInfo structure */
351 for (i = 0; i < num_planes; i++) {
352 const guchar * const plane = gst_vaapi_image_get_plane(image, i);
353 GST_VIDEO_INFO_PLANE_OFFSET(vip, i) = plane - data;
354 GST_VIDEO_INFO_PLANE_STRIDE(vip, i) =
355 gst_vaapi_image_get_pitch(image, i);
357 GST_VIDEO_INFO_SIZE(vip) = data_size;
362 gst_vaapi_video_allocator_new(GstVaapiDisplay *display, GstCaps *caps)
364 GstVaapiVideoAllocator *allocator;
366 GstVaapiSurface *surface;
367 GstVaapiImage *image;
369 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
370 g_return_val_if_fail(GST_IS_CAPS(caps), NULL);
372 allocator = g_object_new(GST_VAAPI_TYPE_VIDEO_ALLOCATOR, NULL);
376 vip = &allocator->video_info;
377 gst_video_info_init(vip);
378 gst_video_info_from_caps(vip, caps);
380 gst_video_info_set_format(&allocator->surface_info, GST_VIDEO_FORMAT_NV12,
381 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
383 if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_ENCODED) {
386 surface = new_surface(display, vip);
389 image = gst_vaapi_surface_derive_image(surface);
392 if (!gst_vaapi_image_map(image))
394 allocator->has_direct_rendering = gst_video_info_update_from_image(
395 &allocator->surface_info, image);
396 gst_vaapi_image_unmap(image);
397 GST_INFO("has direct-rendering for %s surfaces: %s",
398 GST_VIDEO_INFO_FORMAT_STRING(&allocator->surface_info),
399 allocator->has_direct_rendering ? "yes" : "no");
401 g_clear_object(&surface);
402 g_clear_object(&image);
405 allocator->image_info = *vip;
406 if (GST_VIDEO_INFO_FORMAT(vip) == GST_VIDEO_FORMAT_ENCODED)
407 gst_video_info_set_format(&allocator->image_info, GST_VIDEO_FORMAT_NV12,
408 GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
410 if (allocator->has_direct_rendering)
411 allocator->image_info = allocator->surface_info;
414 image = new_image(display, &allocator->image_info);
417 if (!gst_vaapi_image_map(image))
419 gst_video_info_update_from_image(&allocator->image_info, image);
420 gst_vaapi_image_unmap(image);
422 g_clear_object(&image);
424 return GST_ALLOCATOR_CAST(allocator);