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 <unistd.h>
36 #include "gstmsdkallocator.h"
37 #include "gstmsdkallocator_libva.h"
38 #include "msdk_libva.h"
39
40 mfxStatus
gst_msdk_frame_alloc(mfxHDL pthis,mfxFrameAllocRequest * req,mfxFrameAllocResponse * resp)41 gst_msdk_frame_alloc (mfxHDL pthis, mfxFrameAllocRequest * req,
42 mfxFrameAllocResponse * resp)
43 {
44 VAStatus va_status;
45 mfxStatus status = MFX_ERR_NONE;
46 gint i;
47 guint format;
48 guint va_fourcc = 0;
49 VASurfaceID *surfaces = NULL;
50 VASurfaceAttrib attribs[2];
51 guint num_attribs = 0;
52 mfxMemId *mids = NULL;
53 GstMsdkContext *context = (GstMsdkContext *) pthis;
54 GstMsdkMemoryID *msdk_mids = NULL;
55 GstMsdkAllocResponse *msdk_resp = NULL;
56 mfxU32 fourcc = req->Info.FourCC;
57 mfxU16 surfaces_num = req->NumFrameSuggested;
58
59 /* MFX_MAKEFOURCC('V','P','8','S') is used for MFX_FOURCC_VP9_SEGMAP surface
60 * in MSDK and this surface is an internal surface. The external allocator
61 * shouldn't be used for this surface allocation
62 *
63 * See https://github.com/Intel-Media-SDK/MediaSDK/issues/762
64 */
65 if (req->Type & MFX_MEMTYPE_INTERNAL_FRAME
66 && fourcc == MFX_MAKEFOURCC ('V', 'P', '8', 'S'))
67 return MFX_ERR_UNSUPPORTED;
68
69 if (req->Type & MFX_MEMTYPE_EXTERNAL_FRAME) {
70 GstMsdkAllocResponse *cached =
71 gst_msdk_context_get_cached_alloc_responses_by_request (context, req);
72 if (cached) {
73 /* check if enough frames were allocated */
74 if (req->NumFrameSuggested > cached->response.NumFrameActual)
75 return MFX_ERR_MEMORY_ALLOC;
76
77 *resp = cached->response;
78 g_atomic_int_inc (&cached->refcount);
79 return MFX_ERR_NONE;
80 }
81 }
82
83 /* The VA API does not define any surface types and the application can use either
84 * MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET or
85 * MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET to indicate data in video memory.
86 */
87 if (!(req->Type & (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET |
88 MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)))
89 return MFX_ERR_UNSUPPORTED;
90
91 va_fourcc = gst_msdk_get_va_fourcc_from_mfx_fourcc (fourcc);
92
93 msdk_mids =
94 (GstMsdkMemoryID *) g_slice_alloc0 (surfaces_num *
95 sizeof (GstMsdkMemoryID));
96 mids = (mfxMemId *) g_slice_alloc0 (surfaces_num * sizeof (mfxMemId));
97 surfaces =
98 (VASurfaceID *) g_slice_alloc0 (surfaces_num * sizeof (VASurfaceID));
99 msdk_resp =
100 (GstMsdkAllocResponse *) g_slice_alloc0 (sizeof (GstMsdkAllocResponse));
101
102 if (va_fourcc != VA_FOURCC_P208) {
103 attribs[0].type = VASurfaceAttribPixelFormat;
104 attribs[0].flags = VA_SURFACE_ATTRIB_SETTABLE;
105 attribs[0].value.type = VAGenericValueTypeInteger;
106 attribs[0].value.value.i = va_fourcc;
107 num_attribs = 1;
108
109 /* set VA_SURFACE_ATTRIB_USAGE_HINT_ENCODER flag for encoding */
110 #if (MFX_VERSION >= 1025)
111 if ((req->Type & MFX_MEMTYPE_VIDEO_MEMORY_ENCODER_TARGET) &&
112 (req->Type & MFX_MEMTYPE_FROM_ENCODE)) {
113 attribs[1].type = VASurfaceAttribUsageHint;
114 attribs[1].flags = VA_SURFACE_ATTRIB_SETTABLE;
115 attribs[1].value.type = VAGenericValueTypeInteger;
116 attribs[1].value.value.i = VA_SURFACE_ATTRIB_USAGE_HINT_ENCODER;
117 num_attribs = 2;
118 }
119 #endif
120
121 format =
122 gst_msdk_get_va_rt_format_from_mfx_rt_format (req->Info.ChromaFormat);
123
124 if (format == VA_RT_FORMAT_YUV420 && va_fourcc == VA_FOURCC_P010)
125 #if VA_CHECK_VERSION(1, 2, 0)
126 format = VA_RT_FORMAT_YUV420_10;
127 #else
128 format = VA_RT_FORMAT_YUV420_10BPP;
129 #endif
130
131 #if VA_CHECK_VERSION(1, 4, 1)
132 if (format == VA_RT_FORMAT_YUV444 && va_fourcc == VA_FOURCC_A2R10G10B10)
133 format = VA_RT_FORMAT_RGB32_10;
134 #endif
135
136 #if ((MFX_VERSION >= 1027) && VA_CHECK_VERSION(1, 2, 0))
137 if (format == VA_RT_FORMAT_YUV422 && va_fourcc == VA_FOURCC_Y210)
138 format = VA_RT_FORMAT_YUV422_10;
139 else if (format == VA_RT_FORMAT_YUV444 && va_fourcc == VA_FOURCC_Y410)
140 format = VA_RT_FORMAT_YUV444_10;
141 #endif
142
143 #if ((MFX_VERSION >= 1031) && VA_CHECK_VERSION(1, 2, 0))
144 if (format == VA_RT_FORMAT_YUV420 && va_fourcc == VA_FOURCC_P016)
145 format = VA_RT_FORMAT_YUV420_12;
146
147 if (format == VA_RT_FORMAT_YUV422 && va_fourcc == VA_FOURCC_Y216)
148 format = VA_RT_FORMAT_YUV422_12;
149
150 if (format == VA_RT_FORMAT_YUV444 && va_fourcc == VA_FOURCC_Y416)
151 format = VA_RT_FORMAT_YUV444_12;
152 #endif
153
154 #if (MFX_VERSION >= 2004)
155 if (format == VA_RT_FORMAT_YUV444 && (va_fourcc == VA_FOURCC_RGBP
156 || va_fourcc == VA_FOURCC_BGRP))
157 format = VA_RT_FORMAT_RGBP;
158 #endif
159
160 va_status = vaCreateSurfaces (gst_msdk_context_get_handle (context),
161 format,
162 req->Info.Width, req->Info.Height, surfaces, surfaces_num, attribs,
163 num_attribs);
164
165 status = gst_msdk_get_mfx_status_from_va_status (va_status);
166 if (status != MFX_ERR_NONE) {
167 GST_WARNING ("failed to create VA surface");
168 return status;
169 }
170
171 for (i = 0; i < surfaces_num; i++) {
172 /* Get dmabuf handle if MFX_MEMTYPE_EXPORT_FRAME */
173 if (req->Type & MFX_MEMTYPE_EXPORT_FRAME) {
174 VADRMPRIMESurfaceDescriptor va_desc = { 0 };
175 uint32_t export_flags = VA_EXPORT_SURFACE_SEPARATE_LAYERS |
176 VA_EXPORT_SURFACE_READ_WRITE;
177
178 va_status =
179 vaExportSurfaceHandle (gst_msdk_context_get_handle (context),
180 surfaces[i], VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2, export_flags,
181 &va_desc);
182
183 status = gst_msdk_get_mfx_status_from_va_status (va_status);
184
185 if (MFX_ERR_NONE != status) {
186 GST_ERROR ("Failed to export surface");
187 return status;
188 }
189
190 g_assert (va_desc.num_objects);
191
192 /* This plugin supports single object only */
193 if (va_desc.num_objects > 1) {
194 GST_ERROR ("Can not support multiple objects");
195 return MFX_ERR_UNSUPPORTED;
196 }
197
198 msdk_mids[i].desc = va_desc;
199 }
200
201 /* Don't use image for DMABuf */
202 msdk_mids[i].image.image_id = VA_INVALID_ID;
203 msdk_mids[i].image.buf = VA_INVALID_ID;
204
205 msdk_mids[i].surface = &surfaces[i];
206 mids[i] = (mfxMemId *) & msdk_mids[i];
207 }
208 } else {
209 /* This is requested from the driver when h265 encoding.
210 * These buffers will be used inside the driver and released by
211 * gst_msdk_frame_free functions. Application doesn't need to handle these buffers.
212 *
213 * See https://github.com/Intel-Media-SDK/samples/issues/13 for more details.
214 */
215 VAContextID context_id = req->AllocId;
216 gint width32 = 32 * ((req->Info.Width + 31) >> 5);
217 gint height32 = 32 * ((req->Info.Height + 31) >> 5);
218 guint64 codedbuf_size = (width32 * height32) * 400LL / (16 * 16);
219
220 for (i = 0; i < surfaces_num; i++) {
221 VABufferID coded_buf;
222
223 va_status = vaCreateBuffer (gst_msdk_context_get_handle (context),
224 context_id, VAEncCodedBufferType, codedbuf_size, 1, NULL, &coded_buf);
225
226 status = gst_msdk_get_mfx_status_from_va_status (va_status);
227 if (status < MFX_ERR_NONE) {
228 GST_ERROR ("failed to create buffer");
229 return status;
230 }
231
232 surfaces[i] = coded_buf;
233 msdk_mids[i].surface = &surfaces[i];
234 msdk_mids[i].fourcc = fourcc;
235
236 /* Don't use image for P208 */
237 msdk_mids[i].image.image_id = VA_INVALID_ID;
238 msdk_mids[i].image.buf = VA_INVALID_ID;
239
240 mids[i] = (mfxMemId *) & msdk_mids[i];
241 }
242 }
243
244 resp->mids = mids;
245 resp->NumFrameActual = surfaces_num;
246
247 msdk_resp->response = *resp;
248 msdk_resp->request = *req;
249 msdk_resp->refcount = 1;
250
251 gst_msdk_context_add_alloc_response (context, msdk_resp);
252
253 return status;
254 }
255
256 mfxStatus
gst_msdk_frame_free(mfxHDL pthis,mfxFrameAllocResponse * resp)257 gst_msdk_frame_free (mfxHDL pthis, mfxFrameAllocResponse * resp)
258 {
259 GstMsdkContext *context = (GstMsdkContext *) pthis;
260 VAStatus va_status = VA_STATUS_SUCCESS;
261 mfxStatus status;
262 GstMsdkMemoryID *mem_id;
263 VADisplay dpy;
264 gint i;
265 GstMsdkAllocResponse *cached = NULL;
266
267 cached = gst_msdk_context_get_cached_alloc_responses (context, resp);
268
269 if (cached) {
270 if (!g_atomic_int_dec_and_test (&cached->refcount))
271 return MFX_ERR_NONE;
272 } else
273 return MFX_ERR_NONE;
274
275 if (!gst_msdk_context_remove_alloc_response (context, resp))
276 return MFX_ERR_NONE;
277
278 mem_id = resp->mids[0];
279 dpy = gst_msdk_context_get_handle (context);
280
281 if (mem_id->fourcc != MFX_FOURCC_P8) {
282 /* Make sure that all the vaImages are destroyed */
283 for (i = 0; i < resp->NumFrameActual; i++) {
284 GstMsdkMemoryID *mem = resp->mids[i];
285
286 /* Release prime fd if used */
287 if (mem->desc.num_objects) {
288 g_assert (mem->desc.num_objects == 1);
289 close (mem->desc.objects[0].fd);
290 mem->desc.num_objects = 0;
291 }
292
293 if (mem->image.image_id != VA_INVALID_ID &&
294 vaDestroyImage (dpy, mem->image.image_id) == VA_STATUS_SUCCESS) {
295 mem_id->image.image_id = VA_INVALID_ID;
296 mem_id->image.buf = VA_INVALID_ID;
297 }
298 }
299
300 va_status =
301 vaDestroySurfaces (dpy, (VASurfaceID *) mem_id->surface,
302 resp->NumFrameActual);
303 } else {
304 VASurfaceID *surfaces = mem_id->surface;
305
306 for (i = 0; i < resp->NumFrameActual; i++) {
307 va_status = vaDestroyBuffer (dpy, surfaces[i]);
308 }
309 }
310
311 g_slice_free1 (resp->NumFrameActual * sizeof (VASurfaceID), mem_id->surface);
312 g_slice_free1 (resp->NumFrameActual * sizeof (GstMsdkMemoryID), mem_id);
313 g_slice_free1 (resp->NumFrameActual * sizeof (mfxMemId), resp->mids);
314
315 status = gst_msdk_get_mfx_status_from_va_status (va_status);
316 return status;
317 }
318
319 mfxStatus
gst_msdk_frame_lock(mfxHDL pthis,mfxMemId mid,mfxFrameData * data)320 gst_msdk_frame_lock (mfxHDL pthis, mfxMemId mid, mfxFrameData * data)
321 {
322 GstMsdkContext *context = (GstMsdkContext *) pthis;
323 VAStatus va_status;
324 mfxStatus status;
325 mfxU8 *buf = NULL;
326 VASurfaceID *va_surface;
327 VADisplay dpy;
328 GstMsdkMemoryID *mem_id;
329
330 mem_id = (GstMsdkMemoryID *) mid;
331 va_surface = mem_id->surface;
332 dpy = gst_msdk_context_get_handle (context);
333
334 if (mem_id->desc.num_objects) {
335 GST_WARNING ("Couldn't map the buffer since dmabuf is already in use");
336 return MFX_ERR_LOCK_MEMORY;
337 }
338
339 if (mem_id->fourcc != MFX_FOURCC_P8) {
340 va_status = vaDeriveImage (dpy, *va_surface, &mem_id->image);
341 status = gst_msdk_get_mfx_status_from_va_status (va_status);
342
343 if (status != MFX_ERR_NONE) {
344 GST_WARNING ("failed to derive image");
345 return status;
346 }
347
348 va_status = vaMapBuffer (dpy, mem_id->image.buf, (void **) &buf);
349 status = gst_msdk_get_mfx_status_from_va_status (va_status);
350
351 if (status != MFX_ERR_NONE) {
352 GST_WARNING ("failed to map");
353 if (vaDestroyImage (dpy, mem_id->image.image_id) == VA_STATUS_SUCCESS) {
354 mem_id->image.image_id = VA_INVALID_ID;
355 mem_id->image.buf = VA_INVALID_ID;
356 }
357 return status;
358 }
359
360 switch (mem_id->image.format.fourcc) {
361 case VA_FOURCC_NV12:
362 case VA_FOURCC_P010:
363 case VA_FOURCC_P016:
364 data->Pitch = mem_id->image.pitches[0];
365 data->Y = buf + mem_id->image.offsets[0];
366 data->UV = buf + mem_id->image.offsets[1];
367 break;
368 case VA_FOURCC_YV12:
369 data->Pitch = mem_id->image.pitches[0];
370 data->Y = buf + mem_id->image.offsets[0];
371 data->U = buf + mem_id->image.offsets[2];
372 data->V = buf + mem_id->image.offsets[1];
373 break;
374 case VA_FOURCC_YUY2:
375 data->Pitch = mem_id->image.pitches[0];
376 data->Y = buf + mem_id->image.offsets[0];
377 data->U = data->Y + 1;
378 data->V = data->Y + 3;
379 break;
380 case VA_FOURCC_UYVY:
381 data->Pitch = mem_id->image.pitches[0];
382 data->Y = buf + mem_id->image.offsets[0];
383 data->U = data->U + 1;
384 data->V = data->U + 2;
385 break;
386 case VA_FOURCC_ARGB:
387 data->Pitch = mem_id->image.pitches[0];
388 data->B = buf + mem_id->image.offsets[0];
389 data->G = data->B + 1;
390 data->R = data->B + 2;
391 data->A = data->B + 3;
392 break;
393 #if (MFX_VERSION >= 1028)
394 case VA_FOURCC_RGB565:
395 data->Pitch = mem_id->image.pitches[0];
396 data->R = buf + mem_id->image.offsets[0];
397 data->G = data->R;
398 data->B = data->R;
399 break;
400 #endif
401 case VA_FOURCC_AYUV:
402 data->PitchHigh = (mfxU16) (mem_id->image.pitches[0] / (1 << 16));
403 data->PitchLow = (mfxU16) (mem_id->image.pitches[0] % (1 << 16));
404 data->V = buf + mem_id->image.offsets[0];
405 data->U = data->V + 1;
406 data->Y = data->V + 2;
407 data->A = data->V + 3;
408 break;
409 #if VA_CHECK_VERSION(1, 4, 1)
410 case VA_FOURCC_A2R10G10B10:
411 data->Pitch = mem_id->image.pitches[0];
412 data->R = buf + mem_id->image.offsets[0];
413 data->G = data->R;
414 data->B = data->R;
415 data->A = data->R;
416 break;
417 #endif
418 #if VA_CHECK_VERSION(1, 2, 0)
419 case VA_FOURCC_Y210:
420 case VA_FOURCC_Y216:
421 data->Pitch = mem_id->image.pitches[0];
422 data->Y = buf + mem_id->image.offsets[0];
423 data->U = data->Y + 2;
424 data->V = data->Y + 6;
425 break;
426 case VA_FOURCC_Y410:
427 data->Pitch = mem_id->image.pitches[0];
428 data->U = buf + mem_id->image.offsets[0]; /* data->Y410 */
429 break;
430 case VA_FOURCC_Y416:
431 data->Pitch = mem_id->image.pitches[0];
432 data->U = buf + mem_id->image.offsets[0];
433 data->Y = data->U + 2;
434 data->V = data->U + 4;
435 data->A = data->U + 6;
436 break;
437 #endif
438 case VA_FOURCC_ABGR:
439 data->Pitch = mem_id->image.pitches[0];
440 data->R = buf + mem_id->image.offsets[0];
441 data->G = data->R + 1;
442 data->B = data->R + 2;
443 data->A = data->R + 3;
444 break;
445
446 #if (MFX_VERSION >= 2004)
447 case VA_FOURCC_RGBP:
448 data->Pitch = mem_id->image.pitches[0];
449 data->R = buf + mem_id->image.offsets[0];
450 data->G = buf + mem_id->image.offsets[1];
451 data->B = buf + mem_id->image.offsets[2];
452 break;
453 case VA_FOURCC_BGRP:
454 data->Pitch = mem_id->image.pitches[0];
455 data->B = buf + mem_id->image.offsets[0];
456 data->G = buf + mem_id->image.offsets[1];
457 data->R = buf + mem_id->image.offsets[2];
458 break;
459 #endif
460
461 default:
462 g_assert_not_reached ();
463 break;
464 }
465 } else {
466 VACodedBufferSegment *coded_buffer_segment;
467 va_status =
468 vaMapBuffer (dpy, *va_surface, (void **) (&coded_buffer_segment));
469 status = gst_msdk_get_mfx_status_from_va_status (va_status);
470 if (MFX_ERR_NONE == status)
471 data->Y = (mfxU8 *) coded_buffer_segment->buf;
472 }
473
474 return status;
475 }
476
477 mfxStatus
gst_msdk_frame_unlock(mfxHDL pthis,mfxMemId mid,mfxFrameData * ptr)478 gst_msdk_frame_unlock (mfxHDL pthis, mfxMemId mid, mfxFrameData * ptr)
479 {
480 GstMsdkContext *context = (GstMsdkContext *) pthis;
481 VAStatus va_status;
482 mfxStatus status;
483 VADisplay dpy;
484 GstMsdkMemoryID *mem_id;
485
486 mem_id = (GstMsdkMemoryID *) mid;
487 dpy = gst_msdk_context_get_handle (context);
488
489 g_assert (mem_id->desc.num_objects == 0);
490
491 if (mem_id->fourcc != MFX_FOURCC_P8) {
492 vaUnmapBuffer (dpy, mem_id->image.buf);
493 va_status = vaDestroyImage (dpy, mem_id->image.image_id);
494
495 if (va_status == VA_STATUS_SUCCESS) {
496 mem_id->image.image_id = VA_INVALID_ID;
497 mem_id->image.buf = VA_INVALID_ID;
498 }
499 } else {
500 va_status = vaUnmapBuffer (dpy, *(mem_id->surface));
501 }
502
503 status = gst_msdk_get_mfx_status_from_va_status (va_status);
504
505 return status;
506 }
507
508 mfxStatus
gst_msdk_frame_get_hdl(mfxHDL pthis,mfxMemId mid,mfxHDL * hdl)509 gst_msdk_frame_get_hdl (mfxHDL pthis, mfxMemId mid, mfxHDL * hdl)
510 {
511 GstMsdkMemoryID *mem_id;
512
513 if (!hdl || !mid)
514 return MFX_ERR_INVALID_HANDLE;
515
516 mem_id = mid;
517 *hdl = mem_id->surface;
518
519 return MFX_ERR_NONE;
520 }
521
522 void
gst_msdk_set_frame_allocator(GstMsdkContext * context)523 gst_msdk_set_frame_allocator (GstMsdkContext * context)
524 {
525 mfxFrameAllocator gst_msdk_frame_allocator = {
526 .pthis = context,
527 .Alloc = gst_msdk_frame_alloc,
528 .Lock = gst_msdk_frame_lock,
529 .Unlock = gst_msdk_frame_unlock,
530 .GetHDL = gst_msdk_frame_get_hdl,
531 .Free = gst_msdk_frame_free,
532 };
533
534 gst_msdk_context_set_frame_allocator (context, &gst_msdk_frame_allocator);
535 }
536
537 gboolean
gst_msdk_get_dmabuf_info_from_surface(mfxFrameSurface1 * surface,gint * handle,gsize * size)538 gst_msdk_get_dmabuf_info_from_surface (mfxFrameSurface1 * surface,
539 gint * handle, gsize * size)
540 {
541 GstMsdkMemoryID *mem_id;
542 g_return_val_if_fail (surface, FALSE);
543
544 mem_id = (GstMsdkMemoryID *) surface->Data.MemId;
545
546 g_assert (mem_id->desc.num_objects == 1);
547
548 if (handle)
549 *handle = mem_id->desc.objects[0].fd;
550 if (size)
551 *size = mem_id->desc.objects[0].size;
552
553 return TRUE;
554 }
555
556 gboolean
gst_msdk_export_dmabuf_to_vasurface(GstMsdkContext * context,GstVideoInfo * vinfo,gint fd,VASurfaceID * surface_id)557 gst_msdk_export_dmabuf_to_vasurface (GstMsdkContext * context,
558 GstVideoInfo * vinfo, gint fd, VASurfaceID * surface_id)
559 {
560 GstVideoFormat format;
561 guint width, height, size, i;
562 unsigned long extbuf_handle;
563 guint va_fourcc = 0, va_chroma = 0;
564 VASurfaceAttrib attribs[2], *attrib;
565 VASurfaceAttribExternalBuffers extbuf;
566 VAStatus va_status;
567 mfxStatus status = MFX_ERR_NONE;
568
569 g_return_val_if_fail (context != NULL, FALSE);
570 g_return_val_if_fail (vinfo != NULL, FALSE);
571 g_return_val_if_fail (fd >= 0, FALSE);
572
573 extbuf_handle = (guintptr) (fd);
574
575 format = GST_VIDEO_INFO_FORMAT (vinfo);
576 width = GST_VIDEO_INFO_WIDTH (vinfo);
577 height = GST_VIDEO_INFO_HEIGHT (vinfo);
578 size = GST_VIDEO_INFO_SIZE (vinfo);
579
580 /* Fixme: Move to common format handling util */
581 switch (format) {
582 case GST_VIDEO_FORMAT_NV12:
583 va_chroma = VA_RT_FORMAT_YUV420;
584 va_fourcc = VA_FOURCC_NV12;
585 break;
586 case GST_VIDEO_FORMAT_BGRA:
587 va_chroma = VA_RT_FORMAT_YUV444;
588 va_fourcc = VA_FOURCC_BGRA;
589 break;
590 case GST_VIDEO_FORMAT_YUY2:
591 va_chroma = VA_RT_FORMAT_YUV422;
592 va_fourcc = VA_FOURCC_YUY2;
593 break;
594 case GST_VIDEO_FORMAT_P010_10LE:
595 #if VA_CHECK_VERSION(1, 2, 0)
596 va_chroma = VA_RT_FORMAT_YUV420_10;
597 #else
598 va_chroma = VA_RT_FORMAT_YUV420_10BPP;
599 #endif
600 va_fourcc = VA_FOURCC_P010;
601 break;
602 case GST_VIDEO_FORMAT_UYVY:
603 va_chroma = VA_RT_FORMAT_YUV422;
604 va_fourcc = VA_FOURCC_UYVY;
605 break;
606 #if (MFX_VERSION >= 1028)
607 case GST_VIDEO_FORMAT_RGB16:
608 va_chroma = VA_RT_FORMAT_RGB16;
609 va_fourcc = VA_FOURCC_RGB565;
610 break;
611 #endif
612 case GST_VIDEO_FORMAT_VUYA:
613 va_chroma = VA_RT_FORMAT_YUV444;
614 va_fourcc = VA_FOURCC_AYUV;
615 break;
616 #if VA_CHECK_VERSION(1, 4, 1)
617 case GST_VIDEO_FORMAT_BGR10A2_LE:
618 va_chroma = VA_RT_FORMAT_RGB32_10;
619 va_fourcc = VA_FOURCC_A2R10G10B10;
620 break;
621 #endif
622 #if VA_CHECK_VERSION(1, 2, 0)
623 case GST_VIDEO_FORMAT_Y210:
624 va_chroma = VA_RT_FORMAT_YUV422_10;
625 va_fourcc = VA_FOURCC_Y210;
626 break;
627 case GST_VIDEO_FORMAT_Y410:
628 va_chroma = VA_RT_FORMAT_YUV444_10;
629 va_fourcc = VA_FOURCC_Y410;
630 break;
631 case GST_VIDEO_FORMAT_P012_LE:
632 va_chroma = VA_RT_FORMAT_YUV420_12;
633 va_fourcc = VA_FOURCC_P016;
634 break;
635 case GST_VIDEO_FORMAT_Y212_LE:
636 va_chroma = VA_RT_FORMAT_YUV422_12;
637 va_fourcc = VA_FOURCC_Y216;
638 break;
639 case GST_VIDEO_FORMAT_Y412_LE:
640 va_chroma = VA_RT_FORMAT_YUV444_12;
641 va_fourcc = VA_FOURCC_Y416;
642 break;
643 #endif
644 #if (MFX_VERSION >= 2004)
645 case GST_VIDEO_FORMAT_RGBP:
646 va_chroma = VA_RT_FORMAT_RGBP;
647 va_fourcc = VA_FOURCC_RGBP;
648 break;
649 case GST_VIDEO_FORMAT_BGRP:
650 va_chroma = VA_RT_FORMAT_RGBP;
651 va_fourcc = VA_FOURCC_BGRP;
652 break;
653 #endif
654 default:
655 goto error_unsupported_format;
656 }
657
658 /* Fill the VASurfaceAttribExternalBuffers */
659 extbuf.pixel_format = va_fourcc;
660 extbuf.width = width;
661 extbuf.height = height;
662 extbuf.data_size = size;
663 extbuf.num_planes = GST_VIDEO_INFO_N_PLANES (vinfo);
664 for (i = 0; i < extbuf.num_planes; i++) {
665 extbuf.pitches[i] = GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i);
666 extbuf.offsets[i] = GST_VIDEO_INFO_PLANE_OFFSET (vinfo, i);
667 }
668 extbuf.buffers = (uintptr_t *) & extbuf_handle;
669 extbuf.num_buffers = 1;
670 extbuf.flags = 0;
671 extbuf.private_data = NULL;
672
673 /* Fill the Surface Attributes */
674 attrib = attribs;
675 attrib->type = VASurfaceAttribMemoryType;
676 attrib->flags = VA_SURFACE_ATTRIB_SETTABLE;
677 attrib->value.type = VAGenericValueTypeInteger;
678 attrib->value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
679 attrib++;
680 attrib->type = VASurfaceAttribExternalBufferDescriptor;
681 attrib->flags = VA_SURFACE_ATTRIB_SETTABLE;
682 attrib->value.type = VAGenericValueTypePointer;
683 attrib->value.value.p = &extbuf;
684 attrib++;
685
686 va_status = vaCreateSurfaces (gst_msdk_context_get_handle (context),
687 va_chroma, width, height, surface_id, 1, attribs, attrib - attribs);
688 status = gst_msdk_get_mfx_status_from_va_status (va_status);
689 if (status != MFX_ERR_NONE)
690 goto error_create_surface;
691
692 return TRUE;
693
694 error_unsupported_format:
695 {
696 GST_ERROR ("Unsupported Video format %s, Can't export dmabuf to vaSurface",
697 gst_video_format_to_string (format));
698 return FALSE;
699 }
700 error_create_surface:
701 {
702 GST_ERROR ("Failed to create the VASurface from DRM_PRIME FD");
703 return FALSE;
704 }
705 }
706
707 /**
708 * gst_msdk_replace_mfx_memid:
709 * This method replace the internal VA Suface in mfxSurface with a new one
710 *
711 * Caution: Not a thread-safe routine, this method is here to work around
712 * the dmabuf-import use case with dynamic memID replacement where msdk
713 * originally Inited with fake memIDs.
714 *
715 * Don't use anywhere else unless you really know what you are doing!
716 */
717 gboolean
gst_msdk_replace_mfx_memid(GstMsdkContext * context,mfxFrameSurface1 * mfx_surface,VASurfaceID surface_id)718 gst_msdk_replace_mfx_memid (GstMsdkContext * context,
719 mfxFrameSurface1 * mfx_surface, VASurfaceID surface_id)
720 {
721 GstMsdkMemoryID *msdk_mid = NULL;
722 VADisplay dpy;
723 VASurfaceID *old_surface_id;
724 VAStatus va_status;
725 mfxStatus status = MFX_ERR_NONE;
726
727 g_return_val_if_fail (mfx_surface != NULL, FALSE);
728 g_return_val_if_fail (context != NULL, FALSE);
729
730 msdk_mid = (GstMsdkMemoryID *) mfx_surface->Data.MemId;
731 dpy = gst_msdk_context_get_handle (context);
732
733 /* Destroy the underlined VAImage if already mapped */
734 if (msdk_mid->image.image_id != VA_INVALID_ID
735 && msdk_mid->image.buf != VA_INVALID_ID) {
736 status =
737 gst_msdk_frame_unlock ((mfxHDL) context, (mfxMemId) msdk_mid, NULL);
738 if (status != MFX_ERR_NONE)
739 goto error_destroy_va_image;
740 }
741
742 /* Destroy the associated VASurface */
743 old_surface_id = msdk_mid->surface;
744 if (*old_surface_id != VA_INVALID_ID) {
745 va_status = vaDestroySurfaces (dpy, old_surface_id, 1);
746 status = gst_msdk_get_mfx_status_from_va_status (va_status);
747 if (status != MFX_ERR_NONE)
748 goto error_destroy_va_surface;
749 }
750
751 *msdk_mid->surface = surface_id;
752
753 return TRUE;
754
755 error_destroy_va_image:
756 {
757 GST_ERROR ("Failed to Destroy the VAImage");
758 return FALSE;
759 }
760 error_destroy_va_surface:
761 {
762 GST_ERROR ("Failed to Destroy the VASurfaceID %x", *old_surface_id);
763 return FALSE;
764 }
765 }
766