plugins: fix ref counting of GstVaapiVideoMemory allocator.
[vaapi: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     return gst_vaapi_image_new(display, GST_VIDEO_INFO_FORMAT(vip),
41         GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
42 }
43
44 static gboolean
45 ensure_image(GstVaapiVideoMemory *mem)
46 {
47     if (!mem->image && mem->use_direct_rendering) {
48         mem->image = gst_vaapi_surface_derive_image(mem->surface);
49         if (!mem->image) {
50             GST_WARNING("failed to derive image, fallbacking to copy");
51             mem->use_direct_rendering = FALSE;
52         }
53     }
54
55     if (!mem->image) {
56         GstVaapiDisplay * const display =
57             gst_vaapi_video_meta_get_display(mem->meta);
58
59         mem->image = new_image(display, mem->image_info);
60         if (!mem->image)
61             return FALSE;
62     }
63     gst_vaapi_video_meta_set_image(mem->meta, mem->image);
64     return TRUE;
65 }
66
67 static GstVaapiSurface *
68 new_surface(GstVaapiDisplay *display, const GstVideoInfo *vip)
69 {
70     GstVaapiSurface *surface;
71
72     /* Try with explicit format first */
73     surface = gst_vaapi_surface_new_with_format(display,
74         GST_VIDEO_INFO_FORMAT(vip), GST_VIDEO_INFO_WIDTH(vip),
75         GST_VIDEO_INFO_HEIGHT(vip));
76     if (surface)
77         return surface;
78
79     /* Try to pick something compatible. Best bet: NV12 (YUV 4:2:0) */
80     if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_NV12)
81         return NULL;
82     return gst_vaapi_surface_new(display, GST_VAAPI_CHROMA_TYPE_YUV420,
83         GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
84 }
85
86 static gboolean
87 ensure_surface(GstVaapiVideoMemory *mem)
88 {
89     if (!mem->surface) {
90         GstVaapiDisplay * const display =
91             gst_vaapi_video_meta_get_display(mem->meta);
92
93         mem->surface = new_surface(display, mem->surface_info);
94         if (!mem->surface)
95             return FALSE;
96     }
97     gst_vaapi_video_meta_set_surface(mem->meta, mem->surface);
98     return TRUE;
99 }
100
101 gboolean
102 gst_video_meta_map_vaapi_memory(GstVideoMeta *meta, guint plane,
103     GstMapInfo *info, gpointer *data, gint *stride, GstMapFlags flags)
104 {
105     GstVaapiVideoMemory * const mem =
106         GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_get_memory(meta->buffer, 0));
107
108     g_return_val_if_fail(mem, FALSE);
109     g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
110                              allocator), FALSE);
111     g_return_val_if_fail(mem->meta, FALSE);
112
113     if (mem->map_type &&
114         mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_PLANAR)
115         goto error_incompatible_map;
116     if ((flags & GST_MAP_READWRITE) != GST_MAP_WRITE)
117         goto error_unsupported_map;
118
119     /* Map for writing */
120     if (++mem->map_count == 1) {
121         if (!ensure_surface(mem))
122             goto error_ensure_surface;
123         if (!ensure_image(mem))
124             goto error_ensure_image;
125         if (!gst_vaapi_image_map(mem->image))
126             goto error_map_image;
127         mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_PLANAR;
128     }
129
130     *data = gst_vaapi_image_get_plane(mem->image, plane);
131     *stride = gst_vaapi_image_get_pitch(mem->image, plane);
132     info->flags = flags;
133     return TRUE;
134
135     /* ERRORS */
136 error_incompatible_map:
137     {
138         GST_ERROR("incompatible map type (%d)", mem->map_type);
139         return FALSE;
140     }
141 error_unsupported_map:
142     {
143         GST_ERROR("unsupported map flags (0x%x)", flags);
144         return FALSE;
145     }
146 error_ensure_surface:
147     {
148         const GstVideoInfo * const vip = mem->surface_info;
149         GST_ERROR("failed to create %s surface of size %ux%u",
150                   GST_VIDEO_INFO_FORMAT_STRING(vip),
151                   GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
152         return FALSE;
153     }
154 error_ensure_image:
155     {
156         const GstVideoInfo * const vip = mem->image_info;
157         GST_ERROR("failed to create %s image of size %ux%u",
158                   GST_VIDEO_INFO_FORMAT_STRING(vip),
159                   GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
160         return FALSE;
161     }
162 error_map_image:
163     {
164         GST_ERROR("failed to map image %" GST_VAAPI_ID_FORMAT,
165                   GST_VAAPI_ID_ARGS(gst_vaapi_image_get_id(mem->image)));
166         return FALSE;
167     }
168 }
169
170 gboolean
171 gst_video_meta_unmap_vaapi_memory(GstVideoMeta *meta, guint plane,
172     GstMapInfo *info)
173 {
174     GstVaapiVideoMemory * const mem =
175         GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_get_memory(meta->buffer, 0));
176
177     g_return_val_if_fail(mem, FALSE);
178     g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
179                              allocator), FALSE);
180     g_return_val_if_fail(mem->meta, FALSE);
181     g_return_val_if_fail(mem->surface, FALSE);
182     g_return_val_if_fail(mem->image, FALSE);
183
184     if (--mem->map_count == 0) {
185         mem->map_type = 0;
186
187         /* Unmap VA image used for read/writes */
188         if (info->flags & GST_MAP_READWRITE)
189             gst_vaapi_image_unmap(mem->image);
190
191         /* Commit VA image to surface */
192         if ((info->flags & GST_MAP_WRITE) && !mem->use_direct_rendering) {
193             if (!gst_vaapi_surface_put_image(mem->surface, mem->image))
194                 goto error_upload_image;
195         }
196     }
197     return TRUE;
198
199     /* ERRORS */
200 error_upload_image:
201     {
202         GST_ERROR("failed to upload image");
203         return FALSE;
204     }
205 }
206
207 GstMemory *
208 gst_vaapi_video_memory_new(GstAllocator *base_allocator,
209     GstVaapiVideoMeta *meta)
210 {
211     GstVaapiVideoAllocator * const allocator =
212         GST_VAAPI_VIDEO_ALLOCATOR_CAST(base_allocator);
213     const GstVideoInfo *vip;
214     GstVaapiVideoMemory *mem;
215
216     mem = g_slice_new(GstVaapiVideoMemory);
217     if (!mem)
218         return NULL;
219
220     vip = &allocator->image_info;
221     gst_memory_init(&mem->parent_instance, 0, gst_object_ref(allocator), NULL,
222         GST_VIDEO_INFO_SIZE(vip), 0, 0, GST_VIDEO_INFO_SIZE(vip));
223
224     mem->proxy = NULL;
225     mem->surface_info = &allocator->surface_info;
226     mem->surface = NULL;
227     mem->image_info = &allocator->image_info;
228     mem->image = NULL;
229     mem->meta = gst_vaapi_video_meta_ref(meta);
230     mem->map_type = 0;
231     mem->map_count = 0;
232     mem->use_direct_rendering = allocator->has_direct_rendering;
233     return GST_MEMORY_CAST(mem);
234 }
235
236 static void
237 gst_vaapi_video_memory_free(GstVaapiVideoMemory *mem)
238 {
239     gst_vaapi_object_replace(&mem->surface, NULL);
240     gst_vaapi_object_replace(&mem->image, NULL);
241     gst_vaapi_video_meta_unref(mem->meta);
242     gst_object_unref(GST_MEMORY_CAST(mem)->allocator);
243     g_slice_free(GstVaapiVideoMemory, mem);
244 }
245
246 static gpointer
247 gst_vaapi_video_memory_map(GstVaapiVideoMemory *mem, gsize maxsize, guint flags)
248 {
249     if (mem->map_type &&
250         mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE)
251         goto error_incompatible_map;
252
253     if (mem->map_count == 0) {
254         gst_vaapi_surface_proxy_replace(&mem->proxy,
255             gst_vaapi_video_meta_get_surface_proxy(mem->meta));
256         if (!mem->proxy)
257             goto error_no_surface_proxy;
258         mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE;
259     }
260     mem->map_count++;
261     return mem->proxy;
262
263     /* ERRORS */
264 error_incompatible_map:
265     GST_ERROR("failed to map memory to a GstVaapiSurfaceProxy");
266     return NULL;
267 error_no_surface_proxy:
268     GST_ERROR("failed to extract GstVaapiSurfaceProxy from video meta");
269     return NULL;
270 }
271
272 static void
273 gst_vaapi_video_memory_unmap(GstVaapiVideoMemory *mem)
274 {
275     if (mem->map_type &&
276         mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE)
277         goto error_incompatible_map;
278
279     if (--mem->map_count == 0) {
280         gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
281         mem->map_type = 0;
282     }
283     return;
284
285     /* ERRORS */
286 error_incompatible_map:
287     GST_ERROR("incompatible map type (%d)", mem->map_type);
288     return;
289 }
290
291 static GstVaapiVideoMemory *
292 gst_vaapi_video_memory_copy(GstVaapiVideoMemory *mem,
293     gssize offset, gssize size)
294 {
295     GstMemory *out_mem;
296
297     if (offset != 0 || size != -1)
298         goto error_unsupported;
299
300     out_mem = gst_vaapi_video_memory_new(mem->parent_instance.allocator,
301         mem->meta);
302     if (!out_mem)
303         goto error_allocate_memory;
304     return GST_VAAPI_VIDEO_MEMORY_CAST(out_mem);
305
306     /* ERRORS */
307 error_unsupported:
308     GST_ERROR("failed to copy partial memory (unsupported operation)");
309     return NULL;
310 error_allocate_memory:
311     GST_ERROR("failed to allocate GstVaapiVideoMemory copy");
312     return NULL;
313 }
314
315 static GstVaapiVideoMemory *
316 gst_vaapi_video_memory_share(GstVaapiVideoMemory *mem,
317     gssize offset, gssize size)
318 {
319     GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_share() hook");
320     return NULL;
321 }
322
323 static gboolean
324 gst_vaapi_video_memory_is_span(GstVaapiVideoMemory *mem1,
325     GstVaapiVideoMemory *mem2, gsize *offset_ptr)
326 {
327     GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_is_span() hook");
328     return FALSE;
329 }
330
331 /* ------------------------------------------------------------------------ */
332 /* --- GstVaapiVideoAllocator                                           --- */
333 /* ------------------------------------------------------------------------ */
334
335 #define GST_VAAPI_VIDEO_ALLOCATOR_CLASS(klass)  \
336     (G_TYPE_CHECK_CLASS_CAST((klass),           \
337         GST_VAAPI_TYPE_VIDEO_ALLOCATOR,         \
338         GstVaapiVideoAllocatorClass))
339
340 #define GST_VAAPI_IS_VIDEO_ALLOCATOR_CLASS(klass) \
341     (G_TYPE_CHECK_CLASS_TYPE((klass), GST_VAAPI_TYPE_VIDEO_ALLOCATOR))
342
343 G_DEFINE_TYPE(GstVaapiVideoAllocator,
344               gst_vaapi_video_allocator,
345               GST_TYPE_ALLOCATOR)
346
347 static GstMemory *
348 gst_vaapi_video_allocator_alloc(GstAllocator *allocator, gsize size,
349     GstAllocationParams *params)
350 {
351     g_warning("use gst_vaapi_video_memory_new() to allocate from "
352         "GstVaapiVideoMemory allocator");
353
354     return NULL;
355 }
356
357 static void
358 gst_vaapi_video_allocator_free(GstAllocator *allocator, GstMemory *mem)
359 {
360     gst_vaapi_video_memory_free(GST_VAAPI_VIDEO_MEMORY_CAST(mem));
361 }
362
363 static void
364 gst_vaapi_video_allocator_class_init(GstVaapiVideoAllocatorClass *klass)
365 {
366     GstAllocatorClass * const allocator_class = GST_ALLOCATOR_CLASS(klass);
367
368     GST_DEBUG_CATEGORY_INIT(gst_debug_vaapivideomemory,
369         "vaapivideomemory", 0, "VA-API video memory allocator");
370
371     allocator_class->alloc      = gst_vaapi_video_allocator_alloc;
372     allocator_class->free       = gst_vaapi_video_allocator_free;
373 }
374
375 static void
376 gst_vaapi_video_allocator_init(GstVaapiVideoAllocator *allocator)
377 {
378     GstAllocator * const base_allocator = GST_ALLOCATOR_CAST(allocator);
379
380     base_allocator->mem_type = GST_VAAPI_VIDEO_MEMORY_NAME;
381     base_allocator->mem_map = (GstMemoryMapFunction)
382         gst_vaapi_video_memory_map;
383     base_allocator->mem_unmap = (GstMemoryUnmapFunction)
384         gst_vaapi_video_memory_unmap;
385     base_allocator->mem_copy = (GstMemoryCopyFunction)
386         gst_vaapi_video_memory_copy;
387     base_allocator->mem_share = (GstMemoryShareFunction)
388         gst_vaapi_video_memory_share;
389     base_allocator->mem_is_span = (GstMemoryIsSpanFunction)
390         gst_vaapi_video_memory_is_span;
391 }
392
393 static gboolean
394 gst_video_info_update_from_image(GstVideoInfo *vip, GstVaapiImage *image)
395 {
396     GstVideoFormat format;
397     const guchar *data;
398     guint i, num_planes, data_size, width, height;
399
400     /* Reset format from image */
401     format = gst_vaapi_image_get_format(image);
402     gst_vaapi_image_get_size(image, &width, &height);
403     gst_video_info_set_format(vip, format, width, height);
404
405     num_planes = gst_vaapi_image_get_plane_count(image);
406     g_return_val_if_fail(num_planes == GST_VIDEO_INFO_N_PLANES(vip), FALSE);
407
408     /* Determine the base data pointer */
409     data = gst_vaapi_image_get_plane(image, 0);
410     for (i = 1; i < num_planes; i++) {
411         const guchar * const plane = gst_vaapi_image_get_plane(image, i);
412         if (data > plane)
413             data = plane;
414     }
415     data_size = gst_vaapi_image_get_data_size(image);
416
417     /* Check that we don't have disjoint planes */
418     for (i = 0; i < num_planes; i++) {
419         const guchar * const plane = gst_vaapi_image_get_plane(image, i);
420         if (plane - data > data_size)
421             return FALSE;
422     }
423
424     /* Update GstVideoInfo structure */
425     for (i = 0; i < num_planes; i++) {
426         const guchar * const plane = gst_vaapi_image_get_plane(image, i);
427         GST_VIDEO_INFO_PLANE_OFFSET(vip, i) = plane - data;
428         GST_VIDEO_INFO_PLANE_STRIDE(vip, i) =
429             gst_vaapi_image_get_pitch(image, i);
430     }
431     GST_VIDEO_INFO_SIZE(vip) = data_size;
432     return TRUE;
433 }
434
435 GstAllocator *
436 gst_vaapi_video_allocator_new(GstVaapiDisplay *display, GstCaps *caps)
437 {
438     GstVaapiVideoAllocator *allocator;
439     GstVideoInfo *vip;
440     GstVaapiSurface *surface;
441     GstVaapiImage *image;
442
443     g_return_val_if_fail(display != NULL, NULL);
444     g_return_val_if_fail(GST_IS_CAPS(caps), NULL);
445
446     allocator = g_object_new(GST_VAAPI_TYPE_VIDEO_ALLOCATOR, NULL);
447     if (!allocator)
448         return NULL;
449
450     vip = &allocator->video_info;
451     gst_video_info_init(vip);
452     gst_video_info_from_caps(vip, caps);
453
454     gst_video_info_set_format(&allocator->surface_info, GST_VIDEO_FORMAT_NV12,
455         GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
456
457     if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_ENCODED) {
458         image = NULL;
459         do {
460             surface = new_surface(display, vip);
461             if (!surface)
462                 break;
463             image = gst_vaapi_surface_derive_image(surface);
464             if (!image)
465                 break;
466             if (!gst_vaapi_image_map(image))
467                 break;
468             allocator->has_direct_rendering = gst_video_info_update_from_image(
469                 &allocator->surface_info, image);
470             gst_vaapi_image_unmap(image);
471             GST_INFO("has direct-rendering for %s surfaces: %s",
472                      GST_VIDEO_INFO_FORMAT_STRING(&allocator->surface_info),
473                      allocator->has_direct_rendering ? "yes" : "no");
474         } while (0);
475         if (surface)
476             gst_vaapi_object_unref(surface);
477         if (image)
478             gst_vaapi_object_unref(image);
479     }
480
481     allocator->image_info = *vip;
482     if (GST_VIDEO_INFO_FORMAT(vip) == GST_VIDEO_FORMAT_ENCODED)
483         gst_video_info_set_format(&allocator->image_info, GST_VIDEO_FORMAT_NV12,
484             GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
485
486     if (allocator->has_direct_rendering)
487         allocator->image_info = allocator->surface_info;
488     else {
489         do {
490             image = new_image(display, &allocator->image_info);
491             if (!image)
492                 break;
493             if (!gst_vaapi_image_map(image))
494                 break;
495             gst_video_info_update_from_image(&allocator->image_info, image);
496             gst_vaapi_image_unmap(image);
497         } while (0);
498         gst_vaapi_object_unref(image);
499     }
500     return GST_ALLOCATOR_CAST(allocator);
501 }