plugins: cope with GST_VAAPI_IS_xxx() macros removal.
[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_READ)
111         goto error_unsupported_map;
112
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;
120     }
121
122     *data = gst_vaapi_image_get_plane(mem->image, plane);
123     *stride = gst_vaapi_image_get_pitch(mem->image, plane);
124     info->flags = flags;
125     return TRUE;
126
127     /* ERRORS */
128 error_unsupported_map:
129     {
130         GST_ERROR("unsupported map flags (0x%x)", flags);
131         return FALSE;
132     }
133 error_ensure_surface:
134     {
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));
139         return FALSE;
140     }
141 error_ensure_image:
142     {
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));
147         return FALSE;
148     }
149 error_map_image:
150     {
151         GST_ERROR("failed to map image %" GST_VAAPI_ID_FORMAT,
152                   GST_VAAPI_ID_ARGS(gst_vaapi_image_get_id(mem->image)));
153         return FALSE;
154     }
155 }
156
157 gboolean
158 gst_video_meta_unmap_vaapi_memory(GstVideoMeta *meta, guint plane,
159     GstMapInfo *info)
160 {
161     GstVaapiVideoMemory * const mem =
162         GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_get_memory(meta->buffer, 0));
163
164     g_return_val_if_fail(mem, FALSE);
165     g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
166                              allocator), FALSE);
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);
170
171     if (--mem->map_count == 0) {
172         gst_vaapi_image_unmap(mem->image);
173
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;
178         }
179     }
180     return TRUE;
181
182     /* ERRORS */
183 error_upload_image:
184     {
185         GST_ERROR("failed to upload image");
186         return FALSE;
187     }
188 }
189
190 GstMemory *
191 gst_vaapi_video_memory_new(GstAllocator *base_allocator,
192     GstVaapiVideoMeta *meta)
193 {
194     GstVaapiVideoAllocator * const allocator =
195         GST_VAAPI_VIDEO_ALLOCATOR_CAST(base_allocator);
196     const GstVideoInfo *vip;
197     GstVaapiVideoMemory *mem;
198
199     mem = g_slice_new(GstVaapiVideoMemory);
200     if (!mem)
201         return NULL;
202
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));
206
207     mem->surface_info = &allocator->surface_info;
208     mem->surface = NULL;
209     mem->image_info = &allocator->image_info;
210     mem->image = NULL;
211     mem->meta = gst_vaapi_video_meta_ref(meta);
212     mem->map_count = 0;
213     mem->use_direct_rendering = allocator->has_direct_rendering;
214     return GST_MEMORY_CAST(mem);
215 }
216
217 static void
218 gst_vaapi_video_memory_free(GstVaapiVideoMemory *mem)
219 {
220     gst_vaapi_object_replace(&mem->surface, NULL);
221     gst_vaapi_object_replace(&mem->image, NULL);
222     gst_vaapi_video_meta_unref(mem->meta);
223     g_slice_free(GstVaapiVideoMemory, mem);
224 }
225
226 static gpointer
227 gst_vaapi_video_memory_map(GstVaapiVideoMemory *mem, gsize maxsize, guint flags)
228 {
229     GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_map() hook");
230     return NULL;
231 }
232
233 static void
234 gst_vaapi_video_memory_unmap(GstVaapiVideoMemory *mem)
235 {
236     GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_unmap() hook");
237 }
238
239 static GstVaapiVideoMemory *
240 gst_vaapi_video_memory_copy(GstVaapiVideoMemory *mem,
241     gssize offset, gssize size)
242 {
243     GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_copy() hook");
244     return NULL;
245 }
246
247 static GstVaapiVideoMemory *
248 gst_vaapi_video_memory_share(GstVaapiVideoMemory *mem,
249     gssize offset, gssize size)
250 {
251     GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_share() hook");
252     return NULL;
253 }
254
255 static gboolean
256 gst_vaapi_video_memory_is_span(GstVaapiVideoMemory *mem1,
257     GstVaapiVideoMemory *mem2, gsize *offset_ptr)
258 {
259     GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_is_span() hook");
260     return FALSE;
261 }
262
263 /* ------------------------------------------------------------------------ */
264 /* --- GstVaapiVideoAllocator                                           --- */
265 /* ------------------------------------------------------------------------ */
266
267 #define GST_VAAPI_VIDEO_ALLOCATOR_CLASS(klass)  \
268     (G_TYPE_CHECK_CLASS_CAST((klass),           \
269         GST_VAAPI_TYPE_VIDEO_ALLOCATOR,         \
270         GstVaapiVideoAllocatorClass))
271
272 #define GST_VAAPI_IS_VIDEO_ALLOCATOR_CLASS(klass) \
273     (G_TYPE_CHECK_CLASS_TYPE((klass), GST_VAAPI_TYPE_VIDEO_ALLOCATOR))
274
275 G_DEFINE_TYPE(GstVaapiVideoAllocator,
276               gst_vaapi_video_allocator,
277               GST_TYPE_ALLOCATOR)
278
279 static GstMemory *
280 gst_vaapi_video_allocator_alloc(GstAllocator *allocator, gsize size,
281     GstAllocationParams *params)
282 {
283     g_warning("use gst_vaapi_video_memory_new() to allocate from "
284         "GstVaapiVideoMemory allocator");
285
286     return NULL;
287 }
288
289 static void
290 gst_vaapi_video_allocator_free(GstAllocator *allocator, GstMemory *mem)
291 {
292     gst_vaapi_video_memory_free(GST_VAAPI_VIDEO_MEMORY_CAST(mem));
293 }
294
295 static void
296 gst_vaapi_video_allocator_class_init(GstVaapiVideoAllocatorClass *klass)
297 {
298     GstAllocatorClass * const allocator_class = GST_ALLOCATOR_CLASS(klass);
299
300     GST_DEBUG_CATEGORY_INIT(gst_debug_vaapivideomemory,
301         "vaapivideomemory", 0, "VA-API video memory allocator");
302
303     allocator_class->alloc      = gst_vaapi_video_allocator_alloc;
304     allocator_class->free       = gst_vaapi_video_allocator_free;
305 }
306
307 static void
308 gst_vaapi_video_allocator_init(GstVaapiVideoAllocator *allocator)
309 {
310     GstAllocator * const base_allocator = GST_ALLOCATOR_CAST(allocator);
311
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;
323 }
324
325 static gboolean
326 gst_video_info_update_from_image(GstVideoInfo *vip, GstVaapiImage *image)
327 {
328     const guchar *data;
329     guint i, num_planes, data_size;
330
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);
333
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);
338         if (data > plane)
339             data = plane;
340     }
341     data_size = gst_vaapi_image_get_data_size(image);
342
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)
347             return FALSE;
348     }
349
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);
356     }
357     GST_VIDEO_INFO_SIZE(vip) = data_size;
358     return TRUE;
359 }
360
361 GstAllocator *
362 gst_vaapi_video_allocator_new(GstVaapiDisplay *display, GstCaps *caps)
363 {
364     GstVaapiVideoAllocator *allocator;
365     GstVideoInfo *vip;
366     GstVaapiSurface *surface;
367     GstVaapiImage *image;
368
369     g_return_val_if_fail(display != NULL, NULL);
370     g_return_val_if_fail(GST_IS_CAPS(caps), NULL);
371
372     allocator = g_object_new(GST_VAAPI_TYPE_VIDEO_ALLOCATOR, NULL);
373     if (!allocator)
374         return NULL;
375
376     vip = &allocator->video_info;
377     gst_video_info_init(vip);
378     gst_video_info_from_caps(vip, caps);
379
380     gst_video_info_set_format(&allocator->surface_info, GST_VIDEO_FORMAT_NV12,
381         GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
382
383     if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_ENCODED) {
384         image = NULL;
385         do {
386             surface = new_surface(display, vip);
387             if (!surface)
388                 break;
389             image = gst_vaapi_surface_derive_image(surface);
390             if (!image)
391                 break;
392             if (!gst_vaapi_image_map(image))
393                 break;
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");
400         } while (0);
401         gst_vaapi_object_unref(surface);
402         gst_vaapi_object_unref(image);
403     }
404
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));
409
410     if (allocator->has_direct_rendering)
411         allocator->image_info = allocator->surface_info;
412     else {
413         do {
414             image = new_image(display, &allocator->image_info);
415             if (!image)
416                 break;
417             if (!gst_vaapi_image_map(image))
418                 break;
419             gst_video_info_update_from_image(&allocator->image_info, image);
420             gst_vaapi_image_unmap(image);
421         } while (0);
422         gst_vaapi_object_unref(image);
423     }
424     return GST_ALLOCATOR_CAST(allocator);
425 }