documentation: fixed a heap o' typos
[platform/upstream/gstreamer.git] / sys / msdk / gstmsdkallocator_libva.c
1 /* GStreamer Intel MSDK plugin
2  * Copyright (c) 2018, Intel Corporation
3  * Copyright (c) 2018, Igalia S.L.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of the copyright holder nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGDECE
29  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <va/va.h>
34 #include <va/va_drmcommon.h>
35 #include "gstmsdkallocator.h"
36 #include "gstmsdkallocator_libva.h"
37 #include "msdk_libva.h"
38
39 mfxStatus
40 gst_msdk_frame_alloc (mfxHDL pthis, mfxFrameAllocRequest * req,
41     mfxFrameAllocResponse * resp)
42 {
43   VAStatus va_status;
44   mfxStatus status = MFX_ERR_NONE;
45   gint i;
46   guint format;
47   guint va_fourcc = 0;
48   VASurfaceID *surfaces = NULL;
49   VASurfaceAttrib attribs[2];
50   guint num_attribs = 0;
51   mfxMemId *mids = NULL;
52   GstMsdkContext *context = (GstMsdkContext *) pthis;
53   GstMsdkMemoryID *msdk_mids = NULL;
54   GstMsdkAllocResponse *msdk_resp = NULL;
55   mfxU32 fourcc = req->Info.FourCC;
56   mfxU16 surfaces_num = req->NumFrameSuggested;
57
58   /* MFX_MAKEFOURCC('V','P','8','S') is used for MFX_FOURCC_VP9_SEGMAP surface
59    * in MSDK and this surface is an internal surface. The external allocator
60    * shouldn't be used for this surface allocation
61    *
62    * See https://github.com/Intel-Media-SDK/MediaSDK/issues/762
63    */
64   if (req->Type & MFX_MEMTYPE_INTERNAL_FRAME
65       && fourcc == MFX_MAKEFOURCC ('V', 'P', '8', 'S'))
66     return MFX_ERR_UNSUPPORTED;
67
68   if (req->Type & MFX_MEMTYPE_EXTERNAL_FRAME) {
69     GstMsdkAllocResponse *cached =
70         gst_msdk_context_get_cached_alloc_responses_by_request (context, req);
71     if (cached) {
72       /* check if enough frames were allocated */
73       if (req->NumFrameSuggested > cached->response.NumFrameActual)
74         return MFX_ERR_MEMORY_ALLOC;
75
76       *resp = cached->response;
77       g_atomic_int_inc (&cached->refcount);
78       return MFX_ERR_NONE;
79     }
80   }
81
82   /* The VA API does not define any surface types and the application can use either
83    * MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET or
84    * MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET to indicate data in video memory.
85    */
86   if (!(req->Type & (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET |
87               MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)))
88     return MFX_ERR_UNSUPPORTED;
89
90   va_fourcc = gst_msdk_get_va_fourcc_from_mfx_fourcc (fourcc);
91
92   msdk_mids =
93       (GstMsdkMemoryID *) g_slice_alloc0 (surfaces_num *
94       sizeof (GstMsdkMemoryID));
95   mids = (mfxMemId *) g_slice_alloc0 (surfaces_num * sizeof (mfxMemId));
96   surfaces =
97       (VASurfaceID *) g_slice_alloc0 (surfaces_num * sizeof (VASurfaceID));
98   msdk_resp =
99       (GstMsdkAllocResponse *) g_slice_alloc0 (sizeof (GstMsdkAllocResponse));
100
101   if (va_fourcc != VA_FOURCC_P208) {
102     attribs[0].type = VASurfaceAttribPixelFormat;
103     attribs[0].flags = VA_SURFACE_ATTRIB_SETTABLE;
104     attribs[0].value.type = VAGenericValueTypeInteger;
105     attribs[0].value.value.i = va_fourcc;
106     num_attribs = 1;
107
108     /* set VA_SURFACE_ATTRIB_USAGE_HINT_ENCODER flag for encoding */
109     if ((req->Type & MFX_MEMTYPE_VIDEO_MEMORY_ENCODER_TARGET) &&
110         (req->Type & MFX_MEMTYPE_FROM_ENCODE)) {
111       attribs[1].type = VASurfaceAttribUsageHint;
112       attribs[1].flags = VA_SURFACE_ATTRIB_SETTABLE;
113       attribs[1].value.type = VAGenericValueTypeInteger;
114       attribs[1].value.value.i = VA_SURFACE_ATTRIB_USAGE_HINT_ENCODER;
115       num_attribs = 2;
116     }
117
118     format =
119         gst_msdk_get_va_rt_format_from_mfx_rt_format (req->Info.ChromaFormat);
120
121     if (format == VA_RT_FORMAT_YUV420 && va_fourcc == VA_FOURCC_P010)
122 #if VA_CHECK_VERSION(1, 2, 0)
123       format = VA_RT_FORMAT_YUV420_10;
124 #else
125       format = VA_RT_FORMAT_YUV420_10BPP;
126 #endif
127
128 #if VA_CHECK_VERSION(1, 4, 1)
129     if (format == VA_RT_FORMAT_YUV444 && va_fourcc == VA_FOURCC_A2R10G10B10)
130       format = VA_RT_FORMAT_RGB32_10;
131 #endif
132
133 #if ((MFX_VERSION >= 1027) && VA_CHECK_VERSION(1, 2, 0))
134     if (format == VA_RT_FORMAT_YUV422 && va_fourcc == VA_FOURCC_Y210)
135       format = VA_RT_FORMAT_YUV422_10;
136     else if (format == VA_RT_FORMAT_YUV444 && va_fourcc == VA_FOURCC_Y410)
137       format = VA_RT_FORMAT_YUV444_10;
138 #endif
139
140     va_status = vaCreateSurfaces (gst_msdk_context_get_handle (context),
141         format,
142         req->Info.Width, req->Info.Height, surfaces, surfaces_num, attribs,
143         num_attribs);
144
145     status = gst_msdk_get_mfx_status_from_va_status (va_status);
146     if (status != MFX_ERR_NONE) {
147       GST_WARNING ("failed to create VA surface");
148       return status;
149     }
150
151     for (i = 0; i < surfaces_num; i++) {
152       /* Get dmabuf handle if MFX_MEMTYPE_EXPORT_FRAME */
153       if (req->Type & MFX_MEMTYPE_EXPORT_FRAME) {
154         msdk_mids[i].info.mem_type = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
155         va_status =
156             vaDeriveImage (gst_msdk_context_get_handle (context), surfaces[i],
157             &msdk_mids[i].image);
158         status = gst_msdk_get_mfx_status_from_va_status (va_status);
159
160         if (MFX_ERR_NONE != status) {
161           GST_ERROR ("failed to derive image");
162           return status;
163         }
164
165         va_status =
166             vaAcquireBufferHandle (gst_msdk_context_get_handle (context),
167             msdk_mids[i].image.buf, &msdk_mids[i].info);
168         status = gst_msdk_get_mfx_status_from_va_status (va_status);
169
170         if (MFX_ERR_NONE != status) {
171           GST_ERROR ("failed to get dmabuf handle");
172           va_status = vaDestroyImage (gst_msdk_context_get_handle (context),
173               msdk_mids[i].image.image_id);
174           if (va_status == VA_STATUS_SUCCESS) {
175             msdk_mids[i].image.image_id = VA_INVALID_ID;
176             msdk_mids[i].image.buf = VA_INVALID_ID;
177           }
178         }
179       } else {
180         /* useful to check the image mapping state later */
181         msdk_mids[i].image.image_id = VA_INVALID_ID;
182         msdk_mids[i].image.buf = VA_INVALID_ID;
183       }
184
185       msdk_mids[i].surface = &surfaces[i];
186       mids[i] = (mfxMemId *) & msdk_mids[i];
187     }
188   } else {
189     /* This is requested from the driver when h265 encoding.
190      * These buffers will be used inside the driver and released by
191      * gst_msdk_frame_free functions. Application doesn't need to handle these buffers.
192      *
193      * See https://github.com/Intel-Media-SDK/samples/issues/13 for more details.
194      */
195     VAContextID context_id = req->AllocId;
196     gint width32 = 32 * ((req->Info.Width + 31) >> 5);
197     gint height32 = 32 * ((req->Info.Height + 31) >> 5);
198     guint64 codedbuf_size = (width32 * height32) * 400LL / (16 * 16);
199
200     for (i = 0; i < surfaces_num; i++) {
201       VABufferID coded_buf;
202
203       va_status = vaCreateBuffer (gst_msdk_context_get_handle (context),
204           context_id, VAEncCodedBufferType, codedbuf_size, 1, NULL, &coded_buf);
205
206       status = gst_msdk_get_mfx_status_from_va_status (va_status);
207       if (status < MFX_ERR_NONE) {
208         GST_ERROR ("failed to create buffer");
209         return status;
210       }
211
212       surfaces[i] = coded_buf;
213       msdk_mids[i].surface = &surfaces[i];
214       msdk_mids[i].fourcc = fourcc;
215       mids[i] = (mfxMemId *) & msdk_mids[i];
216     }
217   }
218
219   resp->mids = mids;
220   resp->NumFrameActual = surfaces_num;
221
222   msdk_resp->response = *resp;
223   msdk_resp->request = *req;
224   msdk_resp->refcount = 1;
225
226   gst_msdk_context_add_alloc_response (context, msdk_resp);
227
228   return status;
229 }
230
231 mfxStatus
232 gst_msdk_frame_free (mfxHDL pthis, mfxFrameAllocResponse * resp)
233 {
234   GstMsdkContext *context = (GstMsdkContext *) pthis;
235   VAStatus va_status = VA_STATUS_SUCCESS;
236   mfxStatus status;
237   GstMsdkMemoryID *mem_id;
238   VADisplay dpy;
239   gint i;
240   GstMsdkAllocResponse *cached = NULL;
241
242   cached = gst_msdk_context_get_cached_alloc_responses (context, resp);
243
244   if (cached) {
245     if (!g_atomic_int_dec_and_test (&cached->refcount))
246       return MFX_ERR_NONE;
247   } else
248     return MFX_ERR_NONE;
249
250   if (!gst_msdk_context_remove_alloc_response (context, resp))
251     return MFX_ERR_NONE;
252
253   mem_id = resp->mids[0];
254   dpy = gst_msdk_context_get_handle (context);
255
256   if (mem_id->fourcc != MFX_FOURCC_P8) {
257     /* Make sure that all the vaImages are destroyed */
258     for (i = 0; i < resp->NumFrameActual; i++) {
259       GstMsdkMemoryID *mem = resp->mids[i];
260
261       /* Release dmabuf handle if used */
262       if (mem->info.mem_type == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME)
263         vaReleaseBufferHandle (dpy, mem->image.buf);
264
265       if (mem->image.image_id != VA_INVALID_ID &&
266           vaDestroyImage (dpy, mem->image.image_id) == VA_STATUS_SUCCESS) {
267         mem_id->image.image_id = VA_INVALID_ID;
268         mem_id->image.buf = VA_INVALID_ID;
269       }
270     }
271
272     va_status =
273         vaDestroySurfaces (dpy, (VASurfaceID *) mem_id->surface,
274         resp->NumFrameActual);
275   } else {
276     VASurfaceID *surfaces = mem_id->surface;
277
278     for (i = 0; i < resp->NumFrameActual; i++) {
279       va_status = vaDestroyBuffer (dpy, surfaces[i]);
280     }
281   }
282
283   g_slice_free1 (resp->NumFrameActual * sizeof (VASurfaceID), mem_id->surface);
284   g_slice_free1 (resp->NumFrameActual * sizeof (GstMsdkMemoryID), mem_id);
285   g_slice_free1 (resp->NumFrameActual * sizeof (mfxMemId), resp->mids);
286
287   status = gst_msdk_get_mfx_status_from_va_status (va_status);
288   return status;
289 }
290
291 mfxStatus
292 gst_msdk_frame_lock (mfxHDL pthis, mfxMemId mid, mfxFrameData * data)
293 {
294   GstMsdkContext *context = (GstMsdkContext *) pthis;
295   VAStatus va_status;
296   mfxStatus status;
297   mfxU8 *buf = NULL;
298   VASurfaceID *va_surface;
299   VADisplay dpy;
300   GstMsdkMemoryID *mem_id;
301
302   mem_id = (GstMsdkMemoryID *) mid;
303   va_surface = mem_id->surface;
304   dpy = gst_msdk_context_get_handle (context);
305
306   if (mem_id->info.mem_type == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME) {
307     GST_WARNING ("Couldn't map the buffer since dmabuf is already in use");
308     return MFX_ERR_LOCK_MEMORY;
309   }
310
311   if (mem_id->fourcc != MFX_FOURCC_P8) {
312     va_status = vaDeriveImage (dpy, *va_surface, &mem_id->image);
313     status = gst_msdk_get_mfx_status_from_va_status (va_status);
314
315     if (status != MFX_ERR_NONE) {
316       GST_WARNING ("failed to derive image");
317       return status;
318     }
319
320     va_status = vaMapBuffer (dpy, mem_id->image.buf, (void **) &buf);
321     status = gst_msdk_get_mfx_status_from_va_status (va_status);
322
323     if (status != MFX_ERR_NONE) {
324       GST_WARNING ("failed to map");
325       if (vaDestroyImage (dpy, mem_id->image.image_id) == VA_STATUS_SUCCESS) {
326         mem_id->image.image_id = VA_INVALID_ID;
327         mem_id->image.buf = VA_INVALID_ID;
328       }
329       return status;
330     }
331
332     switch (mem_id->image.format.fourcc) {
333       case VA_FOURCC_NV12:
334       case VA_FOURCC_P010:
335         data->Pitch = mem_id->image.pitches[0];
336         data->Y = buf + mem_id->image.offsets[0];
337         data->UV = buf + mem_id->image.offsets[1];
338         break;
339       case VA_FOURCC_YV12:
340         data->Pitch = mem_id->image.pitches[0];
341         data->Y = buf + mem_id->image.offsets[0];
342         data->U = buf + mem_id->image.offsets[2];
343         data->V = buf + mem_id->image.offsets[1];
344         break;
345       case VA_FOURCC_YUY2:
346         data->Pitch = mem_id->image.pitches[0];
347         data->Y = buf + mem_id->image.offsets[0];
348         data->U = data->Y + 1;
349         data->V = data->Y + 3;
350         break;
351       case VA_FOURCC_UYVY:
352         data->Pitch = mem_id->image.pitches[0];
353         data->Y = buf + mem_id->image.offsets[0];
354         data->U = data->U + 1;
355         data->V = data->U + 2;
356         break;
357       case VA_FOURCC_ARGB:
358         data->Pitch = mem_id->image.pitches[0];
359         data->B = buf + mem_id->image.offsets[0];
360         data->G = data->B + 1;
361         data->R = data->B + 2;
362         data->A = data->B + 3;
363         break;
364 #if (MFX_VERSION >= 1028)
365       case VA_FOURCC_RGB565:
366         data->Pitch = mem_id->image.pitches[0];
367         data->R = buf + mem_id->image.offsets[0];
368         data->G = data->R;
369         data->B = data->R;
370         break;
371 #endif
372       case VA_FOURCC_AYUV:
373         data->PitchHigh = (mfxU16) (mem_id->image.pitches[0] / (1 << 16));
374         data->PitchLow = (mfxU16) (mem_id->image.pitches[0] % (1 << 16));
375         data->V = buf + mem_id->image.offsets[0];
376         data->U = data->V + 1;
377         data->Y = data->V + 2;
378         data->A = data->V + 3;
379         break;
380 #if VA_CHECK_VERSION(1, 4, 1)
381       case VA_FOURCC_A2R10G10B10:
382         data->Pitch = mem_id->image.pitches[0];
383         data->R = buf + mem_id->image.offsets[0];
384         data->G = data->R;
385         data->B = data->R;
386         data->A = data->R;
387         break;
388 #endif
389 #if VA_CHECK_VERSION(1, 2, 0)
390       case VA_FOURCC_Y210:
391         data->Pitch = mem_id->image.pitches[0];
392         data->Y = buf + mem_id->image.offsets[0];
393         data->U = data->Y + 2;
394         data->V = data->Y + 6;
395         break;
396       case VA_FOURCC_Y410:
397         data->Pitch = mem_id->image.pitches[0];
398         data->U = buf + mem_id->image.offsets[0];       /* data->Y410 */
399         break;
400 #endif
401       case VA_FOURCC_ABGR:
402         data->Pitch = mem_id->image.pitches[0];
403         data->R = buf + mem_id->image.offsets[0];
404         data->G = data->R + 1;
405         data->B = data->R + 2;
406         data->A = data->R + 3;
407         break;
408
409       default:
410         g_assert_not_reached ();
411         break;
412     }
413   } else {
414     VACodedBufferSegment *coded_buffer_segment;
415     va_status =
416         vaMapBuffer (dpy, *va_surface, (void **) (&coded_buffer_segment));
417     status = gst_msdk_get_mfx_status_from_va_status (va_status);
418     if (MFX_ERR_NONE == status)
419       data->Y = (mfxU8 *) coded_buffer_segment->buf;
420   }
421
422   return status;
423 }
424
425 mfxStatus
426 gst_msdk_frame_unlock (mfxHDL pthis, mfxMemId mid, mfxFrameData * ptr)
427 {
428   GstMsdkContext *context = (GstMsdkContext *) pthis;
429   VAStatus va_status;
430   mfxStatus status;
431   VADisplay dpy;
432   GstMsdkMemoryID *mem_id;
433
434   mem_id = (GstMsdkMemoryID *) mid;
435   dpy = gst_msdk_context_get_handle (context);
436
437   if (mem_id->fourcc != MFX_FOURCC_P8) {
438     vaUnmapBuffer (dpy, mem_id->image.buf);
439     va_status = vaDestroyImage (dpy, mem_id->image.image_id);
440
441     if (va_status == VA_STATUS_SUCCESS) {
442       mem_id->image.image_id = VA_INVALID_ID;
443       mem_id->image.buf = VA_INVALID_ID;
444     }
445   } else {
446     va_status = vaUnmapBuffer (dpy, *(mem_id->surface));
447   }
448
449   status = gst_msdk_get_mfx_status_from_va_status (va_status);
450
451   return status;
452 }
453
454 mfxStatus
455 gst_msdk_frame_get_hdl (mfxHDL pthis, mfxMemId mid, mfxHDL * hdl)
456 {
457   GstMsdkMemoryID *mem_id;
458
459   if (!hdl || !mid)
460     return MFX_ERR_INVALID_HANDLE;
461
462   mem_id = mid;
463   *hdl = mem_id->surface;
464
465   return MFX_ERR_NONE;
466 }
467
468 void
469 gst_msdk_set_frame_allocator (GstMsdkContext * context)
470 {
471   mfxFrameAllocator gst_msdk_frame_allocator = {
472     .pthis = context,
473     .Alloc = gst_msdk_frame_alloc,
474     .Lock = gst_msdk_frame_lock,
475     .Unlock = gst_msdk_frame_unlock,
476     .GetHDL = gst_msdk_frame_get_hdl,
477     .Free = gst_msdk_frame_free,
478   };
479
480   gst_msdk_context_set_frame_allocator (context, &gst_msdk_frame_allocator);
481 }
482
483 gboolean
484 gst_msdk_get_dmabuf_info_from_surface (mfxFrameSurface1 * surface,
485     gint * handle, gsize * size)
486 {
487   GstMsdkMemoryID *mem_id;
488   g_return_val_if_fail (surface, FALSE);
489
490   mem_id = (GstMsdkMemoryID *) surface->Data.MemId;
491   if (handle)
492     *handle = mem_id->info.handle;
493   if (size)
494     *size = mem_id->info.mem_size;
495
496   return TRUE;
497 }
498
499 gboolean
500 gst_msdk_export_dmabuf_to_vasurface (GstMsdkContext * context,
501     GstVideoInfo * vinfo, gint fd, VASurfaceID * surface_id)
502 {
503   GstVideoFormat format;
504   guint width, height, size, i;
505   unsigned long extbuf_handle;
506   guint va_fourcc = 0, va_chroma = 0;
507   VASurfaceAttrib attribs[2], *attrib;
508   VASurfaceAttribExternalBuffers extbuf;
509   VAStatus va_status;
510   mfxStatus status = MFX_ERR_NONE;
511
512   g_return_val_if_fail (context != NULL, FALSE);
513   g_return_val_if_fail (vinfo != NULL, FALSE);
514   g_return_val_if_fail (fd >= 0, FALSE);
515
516   extbuf_handle = (guintptr) (fd);
517
518   format = GST_VIDEO_INFO_FORMAT (vinfo);
519   width = GST_VIDEO_INFO_WIDTH (vinfo);
520   height = GST_VIDEO_INFO_HEIGHT (vinfo);
521   size = GST_VIDEO_INFO_SIZE (vinfo);
522
523   /* Fixme: Move to common format handling util */
524   switch (format) {
525     case GST_VIDEO_FORMAT_NV12:
526       va_chroma = VA_RT_FORMAT_YUV420;
527       va_fourcc = VA_FOURCC_NV12;
528       break;
529     case GST_VIDEO_FORMAT_BGRA:
530       va_chroma = VA_RT_FORMAT_YUV444;
531       va_fourcc = VA_FOURCC_BGRA;
532       break;
533     case GST_VIDEO_FORMAT_YUY2:
534       va_chroma = VA_RT_FORMAT_YUV422;
535       va_fourcc = VA_FOURCC_YUY2;
536       break;
537     case GST_VIDEO_FORMAT_P010_10LE:
538 #if VA_CHECK_VERSION(1, 2, 0)
539       va_chroma = VA_RT_FORMAT_YUV420_10;
540 #else
541       va_chroma = VA_RT_FORMAT_YUV420_10BPP;
542 #endif
543       va_fourcc = VA_FOURCC_P010;
544       break;
545     case GST_VIDEO_FORMAT_UYVY:
546       va_chroma = VA_RT_FORMAT_YUV422;
547       va_fourcc = VA_FOURCC_UYVY;
548       break;
549 #if (MFX_VERSION >= 1028)
550     case GST_VIDEO_FORMAT_RGB16:
551       va_chroma = VA_RT_FORMAT_RGB16;
552       va_fourcc = VA_FOURCC_RGB565;
553       break;
554 #endif
555     case GST_VIDEO_FORMAT_VUYA:
556       va_chroma = VA_RT_FORMAT_YUV444;
557       va_fourcc = VA_FOURCC_AYUV;
558       break;
559 #if VA_CHECK_VERSION(1, 4, 1)
560     case GST_VIDEO_FORMAT_BGR10A2_LE:
561       va_chroma = VA_RT_FORMAT_RGB32_10;
562       va_fourcc = VA_FOURCC_A2R10G10B10;
563       break;
564 #endif
565 #if VA_CHECK_VERSION(1, 2, 0)
566     case GST_VIDEO_FORMAT_Y210:
567       va_chroma = VA_RT_FORMAT_YUV422_10;
568       va_fourcc = VA_FOURCC_Y210;
569       break;
570     case GST_VIDEO_FORMAT_Y410:
571       va_chroma = VA_RT_FORMAT_YUV444_10;
572       va_fourcc = VA_FOURCC_Y410;
573       break;
574 #endif
575     default:
576       goto error_unsupported_format;
577   }
578
579   /* Fill the VASurfaceAttribExternalBuffers */
580   extbuf.pixel_format = va_fourcc;
581   extbuf.width = width;
582   extbuf.height = height;
583   extbuf.data_size = size;
584   extbuf.num_planes = GST_VIDEO_INFO_N_PLANES (vinfo);
585   for (i = 0; i < extbuf.num_planes; i++) {
586     extbuf.pitches[i] = GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i);
587     extbuf.offsets[i] = GST_VIDEO_INFO_PLANE_OFFSET (vinfo, i);
588   }
589   extbuf.buffers = (uintptr_t *) & extbuf_handle;
590   extbuf.num_buffers = 1;
591   extbuf.flags = 0;
592   extbuf.private_data = NULL;
593
594   /* Fill the Surface Attributes */
595   attrib = attribs;
596   attrib->type = VASurfaceAttribMemoryType;
597   attrib->flags = VA_SURFACE_ATTRIB_SETTABLE;
598   attrib->value.type = VAGenericValueTypeInteger;
599   attrib->value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
600   attrib++;
601   attrib->type = VASurfaceAttribExternalBufferDescriptor;
602   attrib->flags = VA_SURFACE_ATTRIB_SETTABLE;
603   attrib->value.type = VAGenericValueTypePointer;
604   attrib->value.value.p = &extbuf;
605   attrib++;
606
607   va_status = vaCreateSurfaces (gst_msdk_context_get_handle (context),
608       va_chroma, width, height, surface_id, 1, attribs, attrib - attribs);
609   status = gst_msdk_get_mfx_status_from_va_status (va_status);
610   if (status != MFX_ERR_NONE)
611     goto error_create_surface;
612
613   return TRUE;
614
615 error_unsupported_format:
616   {
617     GST_ERROR ("Unsupported Video format %s, Can't export dmabuf to vaSurface",
618         gst_video_format_to_string (format));
619     return FALSE;
620   }
621 error_create_surface:
622   {
623     GST_ERROR ("Failed to create the VASurface from DRM_PRIME FD");
624     return FALSE;
625   }
626 }
627
628 /**
629  * gst_msdk_replace_mfx_memid:
630  * This method replace the internal VA Suface in mfxSurface with a new one
631  *
632  * Caution: Not a thread-safe routine, this method is here to work around
633  * the dmabuf-import use case with dynamic memID replacement where msdk
634  * originally Inited with fake memIDs.
635  *
636  * Don't use anywhere else unless you really know what you are doing!
637  */
638 gboolean
639 gst_msdk_replace_mfx_memid (GstMsdkContext * context,
640     mfxFrameSurface1 * mfx_surface, VASurfaceID surface_id)
641 {
642   GstMsdkMemoryID *msdk_mid = NULL;
643   VADisplay dpy;
644   VASurfaceID *old_surface_id;
645   VAStatus va_status;
646   mfxStatus status = MFX_ERR_NONE;
647
648   g_return_val_if_fail (mfx_surface != NULL, FALSE);
649   g_return_val_if_fail (context != NULL, FALSE);
650
651   msdk_mid = (GstMsdkMemoryID *) mfx_surface->Data.MemId;
652   dpy = gst_msdk_context_get_handle (context);
653
654   /* Destroy the underlined VAImage if already mapped */
655   if (msdk_mid->image.image_id != VA_INVALID_ID
656       && msdk_mid->image.buf != VA_INVALID_ID) {
657     status =
658         gst_msdk_frame_unlock ((mfxHDL) context, (mfxMemId) msdk_mid, NULL);
659     if (status != MFX_ERR_NONE)
660       goto error_destroy_va_image;
661   }
662
663   /* Destroy the associated VASurface */
664   old_surface_id = msdk_mid->surface;
665   if (*old_surface_id != VA_INVALID_ID) {
666     va_status = vaDestroySurfaces (dpy, old_surface_id, 1);
667     status = gst_msdk_get_mfx_status_from_va_status (va_status);
668     if (status != MFX_ERR_NONE)
669       goto error_destroy_va_surface;
670   }
671
672   *msdk_mid->surface = surface_id;
673
674   return TRUE;
675
676 error_destroy_va_image:
677   {
678     GST_ERROR ("Failed to Destroy the VAImage");
679     return FALSE;
680   }
681 error_destroy_va_surface:
682   {
683     GST_ERROR ("Failed to Destroy the VASurfaceID %x", *old_surface_id);
684     return FALSE;
685   }
686 }