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