plugins: silence check for direct-rendering mode in video memory.
[platform/upstream/gstreamer-vaapi.git] / gst / vaapi / gstvaapivideomemory.c
1 /*
2  *  gstvaapivideomemory.c - Gstreamer/VA video memory
3  *
4  *  Copyright (C) 2013 Intel Corporation
5  *
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.
10  *
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.
15  *
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
20  */
21
22 #include "gst/vaapi/sysdeps.h"
23 #include "gstvaapivideomemory.h"
24
25 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapivideomemory);
26 #define GST_CAT_DEFAULT gst_debug_vaapivideomemory
27
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))
31 #endif
32
33 /* ------------------------------------------------------------------------ */
34 /* --- GstVaapiVideoMemory                                              --- */
35 /* ------------------------------------------------------------------------ */
36
37 static GstVaapiImage *
38 new_image(GstVaapiDisplay *display, const GstVideoInfo *vip)
39 {
40     GstVaapiImageFormat format;
41
42     format = gst_vaapi_image_format_from_video(GST_VIDEO_INFO_FORMAT(vip));
43     if (!format)
44         return NULL;
45
46     return gst_vaapi_image_new(display, format,
47         GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
48 }
49
50 static gboolean
51 ensure_image(GstVaapiVideoMemory *mem)
52 {
53     if (!mem->image && mem->use_direct_rendering) {
54         mem->image = gst_vaapi_surface_derive_image(mem->surface);
55         if (!mem->image) {
56             GST_WARNING("failed to derive image, fallbacking to copy");
57             mem->use_direct_rendering = FALSE;
58         }
59     }
60
61     if (!mem->image) {
62         GstVaapiDisplay * const display =
63             gst_vaapi_video_meta_get_display(mem->meta);
64
65         mem->image = new_image(display, mem->image_info);
66         if (!mem->image)
67             return FALSE;
68     }
69     gst_vaapi_video_meta_set_image(mem->meta, mem->image);
70     return TRUE;
71 }
72
73 static GstVaapiSurface *
74 new_surface(GstVaapiDisplay *display, const GstVideoInfo *vip)
75 {
76     if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_NV12)
77         return NULL;
78
79     return gst_vaapi_surface_new(display, GST_VAAPI_CHROMA_TYPE_YUV420,
80         GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
81 }
82
83 static gboolean
84 ensure_surface(GstVaapiVideoMemory *mem)
85 {
86     if (!mem->surface) {
87         GstVaapiDisplay * const display =
88             gst_vaapi_video_meta_get_display(mem->meta);
89
90         mem->surface = new_surface(display, mem->surface_info);
91         if (!mem->surface)
92             return FALSE;
93     }
94     gst_vaapi_video_meta_set_surface(mem->meta, mem->surface);
95     return TRUE;
96 }
97
98 gboolean
99 gst_video_meta_map_vaapi_memory(GstVideoMeta *meta, guint plane,
100     GstMapInfo *info, gpointer *data, gint *stride, GstMapFlags flags)
101 {
102     GstVaapiVideoMemory * const mem =
103         GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_get_memory(meta->buffer, 0));
104
105     g_return_val_if_fail(mem, FALSE);
106     g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
107                              allocator), FALSE);
108     g_return_val_if_fail(mem->meta, FALSE);
109
110     if ((flags & GST_MAP_READWRITE) != GST_MAP_WRITE)
111         goto error_unsupported_map;
112
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;
121     }
122
123     *data = gst_vaapi_image_get_plane(mem->image, plane);
124     *stride = gst_vaapi_image_get_pitch(mem->image, plane);
125     info->flags = flags;
126     return TRUE;
127
128     /* ERRORS */
129 error_unsupported_map:
130     {
131         GST_ERROR("unsupported map flags (0x%x)", flags);
132         return FALSE;
133     }
134 error_ensure_surface:
135     {
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));
140         return FALSE;
141     }
142 error_ensure_image:
143     {
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));
148         return FALSE;
149     }
150 error_map_image:
151     {
152         GST_ERROR("failed to map image %" GST_VAAPI_ID_FORMAT,
153                   GST_VAAPI_ID_ARGS(gst_vaapi_image_get_id(mem->image)));
154         return FALSE;
155     }
156 }
157
158 gboolean
159 gst_video_meta_unmap_vaapi_memory(GstVideoMeta *meta, guint plane,
160     GstMapInfo *info)
161 {
162     GstVaapiVideoMemory * const mem =
163         GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_get_memory(meta->buffer, 0));
164
165     g_return_val_if_fail(mem, FALSE);
166     g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
167                              allocator), FALSE);
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);
171
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);
176
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;
181         }
182     }
183     return TRUE;
184
185     /* ERRORS */
186 error_upload_image:
187     {
188         GST_ERROR("failed to upload image");
189         return FALSE;
190     }
191 }
192
193 GstMemory *
194 gst_vaapi_video_memory_new(GstAllocator *base_allocator,
195     GstVaapiVideoMeta *meta)
196 {
197     GstVaapiVideoAllocator * const allocator =
198         GST_VAAPI_VIDEO_ALLOCATOR_CAST(base_allocator);
199     const GstVideoInfo *vip;
200     GstVaapiVideoMemory *mem;
201
202     mem = g_slice_new(GstVaapiVideoMemory);
203     if (!mem)
204         return NULL;
205
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));
209
210     mem->surface_info = &allocator->surface_info;
211     mem->surface = NULL;
212     mem->image_info = &allocator->image_info;
213     mem->image = NULL;
214     mem->meta = gst_vaapi_video_meta_ref(meta);
215     mem->map_count = 0;
216     mem->use_direct_rendering = allocator->has_direct_rendering;
217     return GST_MEMORY_CAST(mem);
218 }
219
220 static void
221 gst_vaapi_video_memory_free(GstVaapiVideoMemory *mem)
222 {
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);
227 }
228
229 static gpointer
230 gst_vaapi_video_memory_map(GstVaapiVideoMemory *mem, gsize maxsize, guint flags)
231 {
232     GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_map() hook");
233     return NULL;
234 }
235
236 static void
237 gst_vaapi_video_memory_unmap(GstVaapiVideoMemory *mem)
238 {
239     GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_unmap() hook");
240 }
241
242 static GstVaapiVideoMemory *
243 gst_vaapi_video_memory_copy(GstVaapiVideoMemory *mem,
244     gssize offset, gssize size)
245 {
246     GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_copy() hook");
247     return NULL;
248 }
249
250 static GstVaapiVideoMemory *
251 gst_vaapi_video_memory_share(GstVaapiVideoMemory *mem,
252     gssize offset, gssize size)
253 {
254     GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_share() hook");
255     return NULL;
256 }
257
258 static gboolean
259 gst_vaapi_video_memory_is_span(GstVaapiVideoMemory *mem1,
260     GstVaapiVideoMemory *mem2, gsize *offset_ptr)
261 {
262     GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_is_span() hook");
263     return FALSE;
264 }
265
266 /* ------------------------------------------------------------------------ */
267 /* --- GstVaapiVideoAllocator                                           --- */
268 /* ------------------------------------------------------------------------ */
269
270 #define GST_VAAPI_VIDEO_ALLOCATOR_CLASS(klass)  \
271     (G_TYPE_CHECK_CLASS_CAST((klass),           \
272         GST_VAAPI_TYPE_VIDEO_ALLOCATOR,         \
273         GstVaapiVideoAllocatorClass))
274
275 #define GST_VAAPI_IS_VIDEO_ALLOCATOR_CLASS(klass) \
276     (G_TYPE_CHECK_CLASS_TYPE((klass), GST_VAAPI_TYPE_VIDEO_ALLOCATOR))
277
278 G_DEFINE_TYPE(GstVaapiVideoAllocator,
279               gst_vaapi_video_allocator,
280               GST_TYPE_ALLOCATOR)
281
282 static GstMemory *
283 gst_vaapi_video_allocator_alloc(GstAllocator *allocator, gsize size,
284     GstAllocationParams *params)
285 {
286     g_warning("use gst_vaapi_video_memory_new() to allocate from "
287         "GstVaapiVideoMemory allocator");
288
289     return NULL;
290 }
291
292 static void
293 gst_vaapi_video_allocator_free(GstAllocator *allocator, GstMemory *mem)
294 {
295     gst_vaapi_video_memory_free(GST_VAAPI_VIDEO_MEMORY_CAST(mem));
296 }
297
298 static void
299 gst_vaapi_video_allocator_class_init(GstVaapiVideoAllocatorClass *klass)
300 {
301     GstAllocatorClass * const allocator_class = GST_ALLOCATOR_CLASS(klass);
302
303     GST_DEBUG_CATEGORY_INIT(gst_debug_vaapivideomemory,
304         "vaapivideomemory", 0, "VA-API video memory allocator");
305
306     allocator_class->alloc      = gst_vaapi_video_allocator_alloc;
307     allocator_class->free       = gst_vaapi_video_allocator_free;
308 }
309
310 static void
311 gst_vaapi_video_allocator_init(GstVaapiVideoAllocator *allocator)
312 {
313     GstAllocator * const base_allocator = GST_ALLOCATOR_CAST(allocator);
314
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;
326 }
327
328 static gboolean
329 gst_video_info_update_from_image(GstVideoInfo *vip, GstVaapiImage *image)
330 {
331     const guchar *data;
332     guint i, num_planes, data_size;
333
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);
336
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);
341         if (data > plane)
342             data = plane;
343     }
344     data_size = gst_vaapi_image_get_data_size(image);
345
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)
350             return FALSE;
351     }
352
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);
359     }
360     GST_VIDEO_INFO_SIZE(vip) = data_size;
361     return TRUE;
362 }
363
364 GstAllocator *
365 gst_vaapi_video_allocator_new(GstVaapiDisplay *display, GstCaps *caps)
366 {
367     GstVaapiVideoAllocator *allocator;
368     GstVideoInfo *vip;
369     GstVaapiSurface *surface;
370     GstVaapiImage *image;
371
372     g_return_val_if_fail(display != NULL, NULL);
373     g_return_val_if_fail(GST_IS_CAPS(caps), NULL);
374
375     allocator = g_object_new(GST_VAAPI_TYPE_VIDEO_ALLOCATOR, NULL);
376     if (!allocator)
377         return NULL;
378
379     vip = &allocator->video_info;
380     gst_video_info_init(vip);
381     gst_video_info_from_caps(vip, caps);
382
383     gst_video_info_set_format(&allocator->surface_info, GST_VIDEO_FORMAT_NV12,
384         GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
385
386     if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_ENCODED) {
387         image = NULL;
388         do {
389             surface = new_surface(display, vip);
390             if (!surface)
391                 break;
392             image = gst_vaapi_surface_derive_image(surface);
393             if (!image)
394                 break;
395             if (!gst_vaapi_image_map(image))
396                 break;
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");
403         } while (0);
404         if (surface)
405             gst_vaapi_object_unref(surface);
406         if (image)
407             gst_vaapi_object_unref(image);
408     }
409
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));
414
415     if (allocator->has_direct_rendering)
416         allocator->image_info = allocator->surface_info;
417     else {
418         do {
419             image = new_image(display, &allocator->image_info);
420             if (!image)
421                 break;
422             if (!gst_vaapi_image_map(image))
423                 break;
424             gst_video_info_update_from_image(&allocator->image_info, image);
425             gst_vaapi_image_unmap(image);
426         } while (0);
427         gst_vaapi_object_unref(image);
428     }
429     return GST_ALLOCATOR_CAST(allocator);
430 }