vaapivideomemory: disallow memory shares across buffers, use a copy.
[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, GST_MEMORY_FLAG_NO_SHARE,
273         gst_object_ref(allocator), NULL, GST_VIDEO_INFO_SIZE(vip), 0,
274         0, GST_VIDEO_INFO_SIZE(vip));
275
276     mem->proxy = NULL;
277     mem->surface_info = &allocator->surface_info;
278     mem->surface = NULL;
279     mem->image_info = &allocator->image_info;
280     mem->image = NULL;
281     mem->meta = gst_vaapi_video_meta_ref(meta);
282     mem->map_type = 0;
283     mem->map_count = 0;
284     mem->use_direct_rendering = allocator->has_direct_rendering;
285     return GST_MEMORY_CAST(mem);
286 }
287
288 static void
289 gst_vaapi_video_memory_free(GstVaapiVideoMemory *mem)
290 {
291     mem->surface = NULL;
292     gst_vaapi_video_memory_reset_image(mem);
293     gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
294     gst_vaapi_video_meta_unref(mem->meta);
295     gst_object_unref(GST_MEMORY_CAST(mem)->allocator);
296     g_slice_free(GstVaapiVideoMemory, mem);
297 }
298
299 void
300 gst_vaapi_video_memory_reset_image(GstVaapiVideoMemory *mem)
301 {
302     GstVaapiVideoAllocator * const allocator =
303         GST_VAAPI_VIDEO_ALLOCATOR_CAST(GST_MEMORY_CAST(mem)->allocator);
304
305     if (mem->use_direct_rendering)
306         gst_vaapi_object_replace(&mem->image, NULL);
307     else if (mem->image) {
308         gst_vaapi_video_pool_put_object(allocator->image_pool, mem->image);
309         mem->image = NULL;
310     }
311 }
312
313 void
314 gst_vaapi_video_memory_reset_surface(GstVaapiVideoMemory *mem)
315 {
316     mem->surface = NULL;
317     gst_vaapi_video_memory_reset_image(mem);
318     gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
319     gst_vaapi_video_meta_set_surface_proxy(mem->meta, NULL);
320 }
321
322 static gpointer
323 gst_vaapi_video_memory_map(GstVaapiVideoMemory *mem, gsize maxsize, guint flags)
324 {
325     gpointer data;
326
327     if (mem->map_count == 0) {
328         switch (flags & GST_MAP_READWRITE) {
329         case 0:
330             // No flags set: return a GstVaapiSurfaceProxy
331             gst_vaapi_surface_proxy_replace(&mem->proxy,
332                 gst_vaapi_video_meta_get_surface_proxy(mem->meta));
333             if (!mem->proxy)
334                 goto error_no_surface_proxy;
335             mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE;
336             break;
337         case GST_MAP_READ:
338             // Only read flag set: return raw pixels
339             if (!ensure_surface(mem))
340                 goto error_no_surface;
341             if (!ensure_image(mem))
342                 goto error_no_image;
343             if (!mem->use_direct_rendering)
344                 gst_vaapi_surface_get_image(mem->surface, mem->image);
345             if (!gst_vaapi_image_map(mem->image))
346                 goto error_map_image;
347             mem->map_type = GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_LINEAR;
348             break;
349         default:
350             goto error_unsupported_map;
351         }
352     }
353
354     switch (mem->map_type) {
355     case GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE:
356         if (!mem->proxy)
357             goto error_no_surface_proxy;
358         data = mem->proxy;
359         break;
360     case GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_LINEAR:
361         if (!mem->image)
362             goto error_no_image;
363         data = get_image_data(mem->image);
364         break;
365     default:
366         goto error_unsupported_map_type;
367     }
368     mem->map_count++;
369     return data;
370
371     /* ERRORS */
372 error_unsupported_map:
373     GST_ERROR("unsupported map flags (0x%x)", flags);
374     return NULL;
375 error_unsupported_map_type:
376     GST_ERROR("unsupported map type (%d)", mem->map_type);
377     return NULL;
378 error_no_surface_proxy:
379     GST_ERROR("failed to extract GstVaapiSurfaceProxy from video meta");
380     return NULL;
381 error_no_surface:
382     GST_ERROR("failed to extract VA surface from video buffer");
383     return NULL;
384 error_no_image:
385     GST_ERROR("failed to extract VA image from video buffer");
386     return NULL;
387 error_map_image:
388     GST_ERROR("failed to map VA image");
389     return NULL;
390 }
391
392 static void
393 gst_vaapi_video_memory_unmap(GstVaapiVideoMemory *mem)
394 {
395     if (mem->map_count == 1) {
396         switch (mem->map_type) {
397         case GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_SURFACE:
398             gst_vaapi_surface_proxy_replace(&mem->proxy, NULL);
399             break;
400         case GST_VAAPI_VIDEO_MEMORY_MAP_TYPE_LINEAR:
401             gst_vaapi_image_unmap(mem->image);
402             break;
403         default:
404             goto error_incompatible_map;
405         }
406         mem->map_type = 0;
407     }
408     mem->map_count--;
409     return;
410
411     /* ERRORS */
412 error_incompatible_map:
413     GST_ERROR("incompatible map type (%d)", mem->map_type);
414     return;
415 }
416
417 static GstVaapiVideoMemory *
418 gst_vaapi_video_memory_copy(GstVaapiVideoMemory *mem,
419     gssize offset, gssize size)
420 {
421     GstVaapiVideoMeta *meta;
422     GstMemory *out_mem;
423     gsize maxsize;
424
425     /* XXX: this implements a soft-copy, i.e. underlying VA surfaces
426        are not copied */
427     (void)gst_memory_get_sizes(GST_MEMORY_CAST(mem), NULL, &maxsize);
428     if (offset != 0 || (size != -1 && (gsize)size != maxsize))
429         goto error_unsupported;
430
431     meta = gst_vaapi_video_meta_copy(mem->meta);
432     if (!meta)
433         goto error_allocate_memory;
434
435     out_mem = gst_vaapi_video_memory_new(GST_MEMORY_CAST(mem)->allocator, meta);
436     gst_vaapi_video_meta_unref(meta);
437     if (!out_mem)
438         goto error_allocate_memory;
439     return GST_VAAPI_VIDEO_MEMORY_CAST(out_mem);
440
441     /* ERRORS */
442 error_unsupported:
443     GST_ERROR("failed to copy partial memory (unsupported operation)");
444     return NULL;
445 error_allocate_memory:
446     GST_ERROR("failed to allocate GstVaapiVideoMemory copy");
447     return NULL;
448 }
449
450 static GstVaapiVideoMemory *
451 gst_vaapi_video_memory_share(GstVaapiVideoMemory *mem,
452     gssize offset, gssize size)
453 {
454     GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_share() hook");
455     return NULL;
456 }
457
458 static gboolean
459 gst_vaapi_video_memory_is_span(GstVaapiVideoMemory *mem1,
460     GstVaapiVideoMemory *mem2, gsize *offset_ptr)
461 {
462     GST_FIXME("unimplemented GstVaapiVideoAllocator::mem_is_span() hook");
463     return FALSE;
464 }
465
466 /* ------------------------------------------------------------------------ */
467 /* --- GstVaapiVideoAllocator                                           --- */
468 /* ------------------------------------------------------------------------ */
469
470 #define GST_VAAPI_VIDEO_ALLOCATOR_CLASS(klass)  \
471     (G_TYPE_CHECK_CLASS_CAST((klass),           \
472         GST_VAAPI_TYPE_VIDEO_ALLOCATOR,         \
473         GstVaapiVideoAllocatorClass))
474
475 #define GST_VAAPI_IS_VIDEO_ALLOCATOR_CLASS(klass) \
476     (G_TYPE_CHECK_CLASS_TYPE((klass), GST_VAAPI_TYPE_VIDEO_ALLOCATOR))
477
478 G_DEFINE_TYPE(GstVaapiVideoAllocator,
479               gst_vaapi_video_allocator,
480               GST_TYPE_ALLOCATOR)
481
482 static GstMemory *
483 gst_vaapi_video_allocator_alloc(GstAllocator *allocator, gsize size,
484     GstAllocationParams *params)
485 {
486     g_warning("use gst_vaapi_video_memory_new() to allocate from "
487         "GstVaapiVideoMemory allocator");
488
489     return NULL;
490 }
491
492 static void
493 gst_vaapi_video_allocator_free(GstAllocator *allocator, GstMemory *mem)
494 {
495     gst_vaapi_video_memory_free(GST_VAAPI_VIDEO_MEMORY_CAST(mem));
496 }
497
498 static void
499 gst_vaapi_video_allocator_finalize(GObject *object)
500 {
501     GstVaapiVideoAllocator * const allocator =
502         GST_VAAPI_VIDEO_ALLOCATOR_CAST(object);
503
504     gst_vaapi_video_pool_replace(&allocator->surface_pool, NULL);
505     gst_vaapi_video_pool_replace(&allocator->image_pool, NULL);
506
507     G_OBJECT_CLASS(gst_vaapi_video_allocator_parent_class)->finalize(object);
508 }
509
510 static void
511 gst_vaapi_video_allocator_class_init(GstVaapiVideoAllocatorClass *klass)
512 {
513     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
514     GstAllocatorClass * const allocator_class = GST_ALLOCATOR_CLASS(klass);
515
516     GST_DEBUG_CATEGORY_INIT(gst_debug_vaapivideomemory,
517         "vaapivideomemory", 0, "VA-API video memory allocator");
518
519     object_class->finalize      = gst_vaapi_video_allocator_finalize;
520     allocator_class->alloc      = gst_vaapi_video_allocator_alloc;
521     allocator_class->free       = gst_vaapi_video_allocator_free;
522 }
523
524 static void
525 gst_vaapi_video_allocator_init(GstVaapiVideoAllocator *allocator)
526 {
527     GstAllocator * const base_allocator = GST_ALLOCATOR_CAST(allocator);
528
529     base_allocator->mem_type = GST_VAAPI_VIDEO_MEMORY_NAME;
530     base_allocator->mem_map = (GstMemoryMapFunction)
531         gst_vaapi_video_memory_map;
532     base_allocator->mem_unmap = (GstMemoryUnmapFunction)
533         gst_vaapi_video_memory_unmap;
534     base_allocator->mem_copy = (GstMemoryCopyFunction)
535         gst_vaapi_video_memory_copy;
536     base_allocator->mem_share = (GstMemoryShareFunction)
537         gst_vaapi_video_memory_share;
538     base_allocator->mem_is_span = (GstMemoryIsSpanFunction)
539         gst_vaapi_video_memory_is_span;
540 }
541
542 static gboolean
543 gst_video_info_update_from_image(GstVideoInfo *vip, GstVaapiImage *image)
544 {
545     GstVideoFormat format;
546     const guchar *data;
547     guint i, num_planes, data_size, width, height;
548
549     /* Reset format from image */
550     format = gst_vaapi_image_get_format(image);
551     gst_vaapi_image_get_size(image, &width, &height);
552     gst_video_info_set_format(vip, format, width, height);
553
554     num_planes = gst_vaapi_image_get_plane_count(image);
555     g_return_val_if_fail(num_planes == GST_VIDEO_INFO_N_PLANES(vip), FALSE);
556
557     /* Determine the base data pointer */
558     data = get_image_data(image);
559     g_return_val_if_fail(data != NULL, FALSE);
560     data_size = gst_vaapi_image_get_data_size(image);
561
562     /* Check that we don't have disjoint planes */
563     for (i = 0; i < num_planes; i++) {
564         const guchar * const plane = gst_vaapi_image_get_plane(image, i);
565         if (plane - data > data_size)
566             return FALSE;
567     }
568
569     /* Update GstVideoInfo structure */
570     for (i = 0; i < num_planes; i++) {
571         const guchar * const plane = gst_vaapi_image_get_plane(image, i);
572         GST_VIDEO_INFO_PLANE_OFFSET(vip, i) = plane - data;
573         GST_VIDEO_INFO_PLANE_STRIDE(vip, i) =
574             gst_vaapi_image_get_pitch(image, i);
575     }
576     GST_VIDEO_INFO_SIZE(vip) = data_size;
577     return TRUE;
578 }
579
580 GstAllocator *
581 gst_vaapi_video_allocator_new(GstVaapiDisplay *display, const GstVideoInfo *vip)
582 {
583     GstVaapiVideoAllocator *allocator;
584     GstVaapiSurface *surface;
585     GstVaapiImage *image;
586
587     g_return_val_if_fail(display != NULL, NULL);
588     g_return_val_if_fail(vip != NULL, NULL);
589
590     allocator = g_object_new(GST_VAAPI_TYPE_VIDEO_ALLOCATOR, NULL);
591     if (!allocator)
592         return NULL;
593
594     allocator->video_info = *vip;
595     gst_video_info_set_format(&allocator->surface_info, GST_VIDEO_FORMAT_NV12,
596         GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
597
598     if (GST_VIDEO_INFO_FORMAT(vip) != GST_VIDEO_FORMAT_ENCODED) {
599         image = NULL;
600         do {
601             surface = new_surface(display, vip);
602             if (!surface)
603                 break;
604             image = gst_vaapi_surface_derive_image(surface);
605             if (!image)
606                 break;
607             if (!gst_vaapi_image_map(image))
608                 break;
609             allocator->has_direct_rendering = gst_video_info_update_from_image(
610                 &allocator->surface_info, image);
611             if (GST_VAAPI_IMAGE_FORMAT(image) != GST_VIDEO_INFO_FORMAT(vip))
612                allocator->has_direct_rendering = FALSE;
613             gst_vaapi_image_unmap(image);
614             GST_INFO("has direct-rendering for %s surfaces: %s",
615                      GST_VIDEO_INFO_FORMAT_STRING(&allocator->surface_info),
616                      allocator->has_direct_rendering ? "yes" : "no");
617         } while (0);
618         if (surface)
619             gst_vaapi_object_unref(surface);
620         if (image)
621             gst_vaapi_object_unref(image);
622     }
623
624     allocator->surface_pool = gst_vaapi_surface_pool_new(display,
625         &allocator->surface_info);
626     if (!allocator->surface_pool)
627         goto error_create_surface_pool;
628
629     allocator->image_info = *vip;
630     if (GST_VIDEO_INFO_FORMAT(vip) == GST_VIDEO_FORMAT_ENCODED)
631         gst_video_info_set_format(&allocator->image_info, GST_VIDEO_FORMAT_I420,
632             GST_VIDEO_INFO_WIDTH(vip), GST_VIDEO_INFO_HEIGHT(vip));
633
634     if (allocator->has_direct_rendering)
635         allocator->image_info = allocator->surface_info;
636     else {
637         do {
638             image = new_image(display, &allocator->image_info);
639             if (!image)
640                 break;
641             if (!gst_vaapi_image_map(image))
642                 break;
643             gst_video_info_update_from_image(&allocator->image_info, image);
644             gst_vaapi_image_unmap(image);
645         } while (0);
646         gst_vaapi_object_unref(image);
647     }
648
649     allocator->image_pool = gst_vaapi_image_pool_new(display,
650         &allocator->image_info);
651     if (!allocator->image_pool)
652         goto error_create_image_pool;
653     return GST_ALLOCATOR_CAST(allocator);
654
655     /* ERRORS */
656 error_create_surface_pool:
657     {
658         GST_ERROR("failed to allocate VA surface pool");
659         gst_object_unref(allocator);
660         return NULL;
661     }
662 error_create_image_pool:
663     {
664         GST_ERROR("failed to allocate VA image pool");
665         gst_object_unref(allocator);
666         return NULL;
667     }
668 }