dfbb9c5619686048cef8c47bec26ff12b11507d4
[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  *    Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
6  *
7  *  This library is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU Lesser General Public License
9  *  as published by the Free Software Foundation; either version 2.1
10  *  of the License, or (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this library; if not, write to the Free
19  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  *  Boston, MA 02110-1301 USA
21  */
22
23 #include "gst/vaapi/sysdeps.h"
24 #include <gst/vaapi/gstvaapisurfacepool.h>
25 #include <gst/vaapi/gstvaapiimagepool.h>
26 #include "gstvaapivideomemory.h"
27
28 GST_DEBUG_CATEGORY_STATIC(gst_debug_vaapivideomemory);
29 #define GST_CAT_DEFAULT gst_debug_vaapivideomemory
30
31 #ifndef GST_VIDEO_INFO_FORMAT_STRING
32 #define GST_VIDEO_INFO_FORMAT_STRING(vip) \
33     gst_video_format_to_string(GST_VIDEO_INFO_FORMAT(vip))
34 #endif
35
36 /* ------------------------------------------------------------------------ */
37 /* --- GstVaapiVideoMemory                                              --- */
38 /* ------------------------------------------------------------------------ */
39
40 static void
41 gst_vaapi_video_memory_reset_image(GstVaapiVideoMemory *mem);
42
43 static guchar *
44 get_image_data(GstVaapiImage *image)
45 {
46     guchar *data;
47     VAImage va_image;
48
49     data = gst_vaapi_image_get_plane(image, 0);
50     if (!data || !gst_vaapi_image_get_image(image, &va_image))
51         return NULL;
52
53     data -= va_image.offsets[0];
54     return data;
55 }
56
57 static GstVaapiImage *
58 new_image(GstVaapiDisplay *display, const GstVideoInfo *vip)
59 {
60     return gst_vaapi_image_new(display, GST_VIDEO_INFO_FORMAT(vip),
61         GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
62 }
63
64 static gboolean
65 ensure_image(GstVaapiVideoMemory *mem)
66 {
67     if (!mem->image && mem->use_direct_rendering) {
68         mem->image = gst_vaapi_surface_derive_image(mem->surface);
69         if (!mem->image) {
70             GST_WARNING("failed to derive image, fallbacking to copy");
71             mem->use_direct_rendering = FALSE;
72         }
73         else if (gst_vaapi_surface_get_format(mem->surface) !=
74                  GST_VIDEO_INFO_FORMAT(mem->image_info)) {
75             gst_vaapi_object_replace(&mem->image, NULL);
76             mem->use_direct_rendering = FALSE;
77         }
78     }
79
80     if (!mem->image) {
81         GstVaapiVideoAllocator * const allocator =
82             GST_VAAPI_VIDEO_ALLOCATOR_CAST(GST_MEMORY_CAST(mem)->allocator);
83
84         mem->image = gst_vaapi_video_pool_get_object(allocator->image_pool);
85         if (!mem->image)
86             return FALSE;
87     }
88     gst_vaapi_video_meta_set_image(mem->meta, mem->image);
89     return TRUE;
90 }
91
92 static GstVaapiSurface *
93 new_surface(GstVaapiDisplay *display, const GstVideoInfo *vip)
94 {
95     GstVaapiSurface *surface;
96     GstVaapiChromaType chroma_type;
97
98     /* Try with explicit format first */
99     if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_ENCODED) {
100         surface = gst_vaapi_surface_new_with_format(display,
101             GST_VIDEO_INFO_FORMAT(vip), GST_VIDEO_INFO_WIDTH(vip),
102             GST_VIDEO_INFO_HEIGHT(vip));
103         if (surface)
104             return surface;
105     }
106
107     /* Try to pick something compatible, i.e. with same chroma type */
108     chroma_type = gst_vaapi_video_format_get_chroma_type(
109         GST_VIDEO_INFO_FORMAT(vip));
110     if (!chroma_type)
111         return NULL;
112     return gst_vaapi_surface_new(display, chroma_type,
113         GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
114 }
115
116 static GstVaapiSurfaceProxy *
117 new_surface_proxy(GstVaapiVideoMemory *mem)
118 {
119     GstVaapiVideoAllocator * const allocator =
120         GST_VAAPI_VIDEO_ALLOCATOR_CAST(GST_MEMORY_CAST(mem)->allocator);
121
122     return gst_vaapi_surface_proxy_new_from_pool(
123         GST_VAAPI_SURFACE_POOL(allocator->surface_pool));
124 }
125
126 static gboolean
127 ensure_surface(GstVaapiVideoMemory *mem)
128 {
129     if (!mem->proxy) {
130         gst_vaapi_surface_proxy_replace(&mem->proxy,
131             gst_vaapi_video_meta_get_surface_proxy(mem->meta));
132
133         if (!mem->proxy) {
134             mem->proxy = new_surface_proxy(mem);
135             if (!mem->proxy)
136                 return FALSE;
137             gst_vaapi_video_meta_set_surface_proxy(mem->meta, mem->proxy);
138         }
139     }
140     mem->surface = GST_VAAPI_SURFACE_PROXY_SURFACE(mem->proxy);
141     return mem->surface != NULL;
142 }
143
144 gboolean
145 gst_video_meta_map_vaapi_memory(GstVideoMeta *meta, guint plane,
146     GstMapInfo *info, gpointer *data, gint *stride, GstMapFlags flags)
147 {
148     GstVaapiVideoMemory * const mem =
149         GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_peek_memory(meta->buffer, 0));
150
151     g_return_val_if_fail(mem, FALSE);
152     g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
153                              allocator), FALSE);
154     g_return_val_if_fail(mem->meta, FALSE);
155
156     if (mem->map_type &&
157         mem->map_type != GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_PLANAR)
158         goto error_incompatible_map;
159
160     /* Map for writing */
161     if (++mem->map_count == 1) {
162         if (!ensure_surface(mem))
163             goto error_ensure_surface;
164         if (!ensure_image(mem))
165             goto error_ensure_image;
166
167         // Check that we can actually map the surface, or image
168         if ((flags & GST_MAP_READWRITE) == GST_MAP_READWRITE &&
169             !mem->use_direct_rendering)
170             goto error_unsupported_map;
171
172         // Load VA image from surface
173         if ((flags & GST_MAP_READ) && !mem->use_direct_rendering)
174             gst_vaapi_surface_get_image(mem->surface, mem->image);
175
176         if (!gst_vaapi_image_map(mem->image))
177             goto error_map_image;
178         mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_PLANAR;
179     }
180
181     *data = gst_vaapi_image_get_plane(mem->image, plane);
182     *stride = gst_vaapi_image_get_pitch(mem->image, plane);
183     info->flags = flags;
184     return TRUE;
185
186     /* ERRORS */
187 error_incompatible_map:
188     {
189         GST_ERROR("incompatible map type (%d)", mem->map_type);
190         return FALSE;
191     }
192 error_unsupported_map:
193     {
194         GST_ERROR("unsupported map flags (0x%x)", flags);
195         return FALSE;
196     }
197 error_ensure_surface:
198     {
199         const GstVideoInfo * const vip = mem->surface_info;
200         GST_ERROR("failed to create %s surface of size %ux%u",
201                   GST_VIDEO_INFO_FORMAT_STRING(vip),
202                   GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
203         return FALSE;
204     }
205 error_ensure_image:
206     {
207         const GstVideoInfo * const vip = mem->image_info;
208         GST_ERROR("failed to create %s image of size %ux%u",
209                   GST_VIDEO_INFO_FORMAT_STRING(vip),
210                   GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
211         return FALSE;
212     }
213 error_map_image:
214     {
215         GST_ERROR("failed to map image %" GST_VAAPI_ID_FORMAT,
216                   GST_VAAPI_ID_ARGS(gst_vaapi_image_get_id(mem->image)));
217         return FALSE;
218     }
219 }
220
221 gboolean
222 gst_video_meta_unmap_vaapi_memory(GstVideoMeta *meta, guint plane,
223     GstMapInfo *info)
224 {
225     GstVaapiVideoMemory * const mem =
226         GST_VAAPI_VIDEO_MEMORY_CAST(gst_buffer_peek_memory(meta->buffer, 0));
227
228     g_return_val_if_fail(mem, FALSE);
229     g_return_val_if_fail(GST_VAAPI_IS_VIDEO_ALLOCATOR(mem->parent_instance.
230                              allocator), FALSE);
231     g_return_val_if_fail(mem->meta, FALSE);
232     g_return_val_if_fail(mem->surface, FALSE);
233     g_return_val_if_fail(mem->image, FALSE);
234
235     if (--mem->map_count == 0) {
236         mem->map_type = 0;
237
238         /* Unmap VA image used for read/writes */
239         if (info->flags & GST_MAP_READWRITE)
240             gst_vaapi_image_unmap(mem->image);
241
242         /* Commit VA image to surface */
243         if ((info->flags & GST_MAP_WRITE) && !mem->use_direct_rendering) {
244             if (!gst_vaapi_surface_put_image(mem->surface, mem->image))
245                 goto error_upload_image;
246         }
247     }
248     return TRUE;
249
250     /* ERRORS */
251 error_upload_image:
252     {
253         GST_ERROR("failed to upload image");
254         return FALSE;
255     }
256 }
257
258 GstMemory *
259 gst_vaapi_video_memory_new(GstAllocator *base_allocator,
260     GstVaapiVideoMeta *meta)
261 {
262     GstVaapiVideoAllocator * const allocator =
263         GST_VAAPI_VIDEO_ALLOCATOR_CAST(base_allocator);
264     const GstVideoInfo *vip;
265     GstVaapiVideoMemory *mem;
266
267     mem = g_slice_new(GstVaapiVideoMemory);
268     if (!mem)
269         return NULL;
270
271     vip = &allocator->image_info;
272     gst_memory_init(&mem->parent_instance, 0, gst_object_ref(allocator), NULL,
273         GST_VIDEO_INFO_SIZE(vip), 0, 0, GST_VIDEO_INFO_SIZE(vip));
274
275     mem->proxy = NULL;
276     mem->surface_info = &allocator->surface_info;
277     mem->surface = NULL;
278     mem->image_info = &allocator->image_info;
279     mem->image = NULL;
280     mem->meta = gst_vaapi_video_meta_ref(meta);
281     mem->map_type = 0;
282     mem->map_count = 0;
283     mem->use_direct_rendering = allocator->has_direct_rendering;
284     return GST_MEMORY_CAST(mem);
285 }
286
287 static void
288 gst_vaapi_video_memory_free(GstVaapiVideoMemory *mem)
289 {
290     mem->surface = NULL;
291     gst_vaapi_video_memory_reset_image(mem);
292     gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
293     gst_vaapi_video_meta_unref(mem->meta);
294     gst_object_unref(GST_MEMORY_CAST(mem)->allocator);
295     g_slice_free(GstVaapiVideoMemory, mem);
296 }
297
298 void
299 gst_vaapi_video_memory_reset_image(GstVaapiVideoMemory *mem)
300 {
301     GstVaapiVideoAllocator * const allocator =
302         GST_VAAPI_VIDEO_ALLOCATOR_CAST(GST_MEMORY_CAST(mem)->allocator);
303
304     if (mem->use_direct_rendering)
305         gst_vaapi_object_replace(&mem->image, NULL);
306     else if (mem->image) {
307         gst_vaapi_video_pool_put_object(allocator->image_pool, mem->image);
308         mem->image = NULL;
309     }
310 }
311
312 void
313 gst_vaapi_video_memory_reset_surface(GstVaapiVideoMemory *mem)
314 {
315     mem->surface = NULL;
316     gst_vaapi_video_memory_reset_image(mem);
317     gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
318     gst_vaapi_video_meta_set_surface_proxy(mem->meta, NULL);
319 }
320
321 static gpointer
322 gst_vaapi_video_memory_map(GstVaapiVideoMemory *mem, gsize maxsize, guint flags)
323 {
324     gpointer data;
325
326     if (mem->map_count == 0) {
327         switch (flags & GST_MAP_READWRITE) {
328         case 0:
329             // No flags set: return a GstVaapiSurfaceProxy
330             gst_vaapi_surface_proxy_replace(&mem->proxy,
331                 gst_vaapi_video_meta_get_surface_proxy(mem->meta));
332             if (!mem->proxy)
333                 goto error_no_surface_proxy;
334             mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE;
335             break;
336         case GST_MAP_READ:
337             // Only read flag set: return raw pixels
338             if (!ensure_surface(mem))
339                 goto error_no_surface;
340             if (!ensure_image(mem))
341                 goto error_no_image;
342             if (!mem->use_direct_rendering)
343                 gst_vaapi_surface_get_image(mem->surface, mem->image);
344             if (!gst_vaapi_image_map(mem->image))
345                 goto error_map_image;
346             mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_LINEAR;
347             break;
348         default:
349             goto error_unsupported_map;
350         }
351     }
352
353     switch (mem->map_type) {
354     case GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE:
355         if (!mem->proxy)
356             goto error_no_surface_proxy;
357         data = mem->proxy;
358         break;
359     case GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_LINEAR:
360         if (!mem->image)
361             goto error_no_image;
362         data = get_image_data(mem->image);
363         break;
364     default:
365         goto error_unsupported_map_type;
366     }
367     mem->map_count++;
368     return data;
369
370     /* ERRORS */
371 error_unsupported_map:
372     GST_ERROR("unsupported map flags (0x%x)", flags);
373     return NULL;
374 error_unsupported_map_type:
375     GST_ERROR("unsupported map type (%d)", mem->map_type);
376     return NULL;
377 error_no_surface_proxy:
378     GST_ERROR("failed to extract GstVaapiSurfaceProxy from video meta");
379     return NULL;
380 error_no_surface:
381     GST_ERROR("failed to extract VA surface from video buffer");
382     return NULL;
383 error_no_image:
384     GST_ERROR("failed to extract VA image from video buffer");
385     return NULL;
386 error_map_image:
387     GST_ERROR("failed to map VA image");
388     return NULL;
389 }
390
391 static void
392 gst_vaapi_video_memory_unmap(GstVaapiVideoMemory *mem)
393 {
394     if (mem->map_count == 1) {
395         switch (mem->map_type) {
396         case GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE:
397             gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
398             break;
399         case GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_LINEAR:
400             gst_vaapi_image_unmap(mem->image);
401             break;
402         default:
403             goto error_incompatible_map;
404         }
405         mem->map_type = 0;
406     }
407     mem->map_count--;
408     return;
409
410     /* ERRORS */
411 error_incompatible_map:
412     GST_ERROR("incompatible map type (%d)", mem->map_type);
413     return;
414 }
415
416 static GstVaapiVideoMemory *
417 gst_vaapi_video_memory_copy(GstVaapiVideoMemory *mem,
418     gssize offset, gssize size)
419 {
420     GstMemory *out_mem;
421
422     if (offset != 0 || size != -1)
423         goto error_unsupported;
424
425     out_mem = gst_vaapi_video_memory_new(mem->parent_instance.allocator,
426         mem->meta);
427     if (!out_mem)
428         goto error_allocate_memory;
429     return GST_VAAPI_VIDEO_MEMORY_CAST(out_mem);
430
431     /* ERRORS */
432 error_unsupported:
433     GST_ERROR("failed to copy partial memory (unsupported operation)");
434     return NULL;
435 error_allocate_memory:
436     GST_ERROR("failed to allocate GstVaapiVideoMemory copy");
437     return NULL;
438 }
439
440 static GstVaapiVideoMemory *
441 gst_vaapi_video_memory_share(GstVaapiVideoMemory *mem,
442     gssize offset, gssize size)
443 {
444     GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_share() hook");
445     return NULL;
446 }
447
448 static gboolean
449 gst_vaapi_video_memory_is_span(GstVaapiVideoMemory *mem1,
450     GstVaapiVideoMemory *mem2, gsize *offset_ptr)
451 {
452     GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_is_span() hook");
453     return FALSE;
454 }
455
456 /* ------------------------------------------------------------------------ */
457 /* --- GstVaapiVideoAllocator                                           --- */
458 /* ------------------------------------------------------------------------ */
459
460 #define GST_VAAPI_VIDEO_ALLOCATOR_CLASS(klass)  \
461     (G_TYPE_CHECK_CLASS_CAST((klass),           \
462         GST_VAAPI_TYPE_VIDEO_ALLOCATOR,         \
463         GstVaapiVideoAllocatorClass))
464
465 #define GST_VAAPI_IS_VIDEO_ALLOCATOR_CLASS(klass) \
466     (G_TYPE_CHECK_CLASS_TYPE((klass), GST_VAAPI_TYPE_VIDEO_ALLOCATOR))
467
468 G_DEFINE_TYPE(GstVaapiVideoAllocator,
469               gst_vaapi_video_allocator,
470               GST_TYPE_ALLOCATOR)
471
472 static GstMemory *
473 gst_vaapi_video_allocator_alloc(GstAllocator *allocator, gsize size,
474     GstAllocationParams *params)
475 {
476     g_warning("use gst_vaapi_video_memory_new() to allocate from "
477         "GstVaapiVideoMemory allocator");
478
479     return NULL;
480 }
481
482 static void
483 gst_vaapi_video_allocator_free(GstAllocator *allocator, GstMemory *mem)
484 {
485     gst_vaapi_video_memory_free(GST_VAAPI_VIDEO_MEMORY_CAST(mem));
486 }
487
488 static void
489 gst_vaapi_video_allocator_finalize(GObject *object)
490 {
491     GstVaapiVideoAllocator * const allocator =
492         GST_VAAPI_VIDEO_ALLOCATOR_CAST(object);
493
494     gst_vaapi_video_pool_replace(&allocator->surface_pool, NULL);
495     gst_vaapi_video_pool_replace(&allocator->image_pool, NULL);
496
497     G_OBJECT_CLASS(gst_vaapi_video_allocator_parent_class)->finalize(object);
498 }
499
500 static void
501 gst_vaapi_video_allocator_class_init(GstVaapiVideoAllocatorClass *klass)
502 {
503     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
504     GstAllocatorClass * const allocator_class = GST_ALLOCATOR_CLASS(klass);
505
506     GST_DEBUG_CATEGORY_INIT(gst_debug_vaapivideomemory,
507         "vaapivideomemory", 0, "VA-API video memory allocator");
508
509     object_class->finalize      = gst_vaapi_video_allocator_finalize;
510     allocator_class->alloc      = gst_vaapi_video_allocator_alloc;
511     allocator_class->free       = gst_vaapi_video_allocator_free;
512 }
513
514 static void
515 gst_vaapi_video_allocator_init(GstVaapiVideoAllocator *allocator)
516 {
517     GstAllocator * const base_allocator = GST_ALLOCATOR_CAST(allocator);
518
519     base_allocator->mem_type = GST_VAAPI_VIDEO_MEMORY_NAME;
520     base_allocator->mem_map = (GstMemoryMapFunction)
521         gst_vaapi_video_memory_map;
522     base_allocator->mem_unmap = (GstMemoryUnmapFunction)
523         gst_vaapi_video_memory_unmap;
524     base_allocator->mem_copy = (GstMemoryCopyFunction)
525         gst_vaapi_video_memory_copy;
526     base_allocator->mem_share = (GstMemoryShareFunction)
527         gst_vaapi_video_memory_share;
528     base_allocator->mem_is_span = (GstMemoryIsSpanFunction)
529         gst_vaapi_video_memory_is_span;
530 }
531
532 static gboolean
533 gst_video_info_update_from_image(GstVideoInfo *vip, GstVaapiImage *image)
534 {
535     GstVideoFormat format;
536     const guchar *data;
537     guint i, num_planes, data_size, width, height;
538
539     /* Reset format from image */
540     format = gst_vaapi_image_get_format(image);
541     gst_vaapi_image_get_size(image, &width, &height);
542     gst_video_info_set_format(vip, format, width, height);
543
544     num_planes = gst_vaapi_image_get_plane_count(image);
545     g_return_val_if_fail(num_planes == GST_VIDEO_INFO_N_PLANES(vip), FALSE);
546
547     /* Determine the base data pointer */
548     data = get_image_data(image);
549     g_return_val_if_fail(data != NULL, FALSE);
550     data_size = gst_vaapi_image_get_data_size(image);
551
552     /* Check that we don't have disjoint planes */
553     for (i = 0; i < num_planes; i++) {
554         const guchar * const plane = gst_vaapi_image_get_plane(image, i);
555         if (plane - data > data_size)
556             return FALSE;
557     }
558
559     /* Update GstVideoInfo structure */
560     for (i = 0; i < num_planes; i++) {
561         const guchar * const plane = gst_vaapi_image_get_plane(image, i);
562         GST_VIDEO_INFO_PLANE_OFFSET(vip, i) = plane - data;
563         GST_VIDEO_INFO_PLANE_STRIDE(vip, i) =
564             gst_vaapi_image_get_pitch(image, i);
565     }
566     GST_VIDEO_INFO_SIZE(vip) = data_size;
567     return TRUE;
568 }
569
570 GstAllocator *
571 gst_vaapi_video_allocator_new(GstVaapiDisplay *display, const GstVideoInfo *vip)
572 {
573     GstVaapiVideoAllocator *allocator;
574     GstVaapiSurface *surface;
575     GstVaapiImage *image;
576
577     g_return_val_if_fail(display != NULL, NULL);
578     g_return_val_if_fail(vip != NULL, NULL);
579
580     allocator = g_object_new(GST_VAAPI_TYPE_VIDEO_ALLOCATOR, NULL);
581     if (!allocator)
582         return NULL;
583
584     allocator->video_info = *vip;
585     gst_video_info_set_format(&allocator->surface_info, GST_VIDEO_FORMAT_NV12,
586         GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
587
588     if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_ENCODED) {
589         image = NULL;
590         do {
591             surface = new_surface(display, vip);
592             if (!surface)
593                 break;
594             image = gst_vaapi_surface_derive_image(surface);
595             if (!image)
596                 break;
597             if (!gst_vaapi_image_map(image))
598                 break;
599             allocator->has_direct_rendering = gst_video_info_update_from_image(
600                 &allocator->surface_info, image);
601             if (GST_VAAPI_IMAGE_FORMAT(image) != GST_VIDEO_INFO_FORMAT(vip))
602                allocator->has_direct_rendering = FALSE;
603             gst_vaapi_image_unmap(image);
604             GST_INFO("has direct-rendering for %s surfaces: %s",
605                      GST_VIDEO_INFO_FORMAT_STRING(&allocator->surface_info),
606                      allocator->has_direct_rendering ? "yes" : "no");
607         } while (0);
608         if (surface)
609             gst_vaapi_object_unref(surface);
610         if (image)
611             gst_vaapi_object_unref(image);
612     }
613
614     allocator->surface_pool = gst_vaapi_surface_pool_new(display,
615         &allocator->surface_info);
616     if (!allocator->surface_pool)
617         goto error_create_surface_pool;
618
619     allocator->image_info = *vip;
620     if (GST_VIDEO_INFO_FORMAT(vip) == GST_VIDEO_FORMAT_ENCODED)
621         gst_video_info_set_format(&allocator->image_info, GST_VIDEO_FORMAT_I420,
622             GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
623
624     if (allocator->has_direct_rendering)
625         allocator->image_info = allocator->surface_info;
626     else {
627         do {
628             image = new_image(display, &allocator->image_info);
629             if (!image)
630                 break;
631             if (!gst_vaapi_image_map(image))
632                 break;
633             gst_video_info_update_from_image(&allocator->image_info, image);
634             gst_vaapi_image_unmap(image);
635         } while (0);
636         gst_vaapi_object_unref(image);
637     }
638
639     allocator->image_pool = gst_vaapi_image_pool_new(display,
640         &allocator->image_info);
641     if (!allocator->image_pool)
642         goto error_create_image_pool;
643     return GST_ALLOCATOR_CAST(allocator);
644
645     /* ERRORS */
646 error_create_surface_pool:
647     {
648         GST_ERROR("failed to allocate VA surface pool");
649         gst_object_unref(allocator);
650         return NULL;
651     }
652 error_create_image_pool:
653     {
654         GST_ERROR("failed to allocate VA image pool");
655         gst_object_unref(allocator);
656         return NULL;
657     }
658 }