1 /* GStreamer Intel MSDK plugin
2 * Copyright (c) 2018, Intel Corporation
3 * Copyright (c) 2018, Igalia S.L.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
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.
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.
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.
34 #include <va/va_drmcommon.h>
35 #include "gstmsdkallocator.h"
36 #include "gstmsdkallocator_libva.h"
37 #include "msdk_libva.h"
40 gst_msdk_frame_alloc (mfxHDL pthis, mfxFrameAllocRequest * req,
41 mfxFrameAllocResponse * resp)
44 mfxStatus status = MFX_ERR_NONE;
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;
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
62 * See https://github.com/Intel-Media-SDK/MediaSDK/issues/762
64 if (req->Type & MFX_MEMTYPE_INTERNAL_FRAME
65 && fourcc == MFX_MAKEFOURCC ('V', 'P', '8', 'S'))
66 return MFX_ERR_UNSUPPORTED;
68 if (req->Type & MFX_MEMTYPE_EXTERNAL_FRAME) {
69 GstMsdkAllocResponse *cached =
70 gst_msdk_context_get_cached_alloc_responses_by_request (context, req);
72 /* check if enough frames were allocated */
73 if (req->NumFrameSuggested > cached->response.NumFrameActual)
74 return MFX_ERR_MEMORY_ALLOC;
76 *resp = cached->response;
77 g_atomic_int_inc (&cached->refcount);
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.
86 if (!(req->Type & (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET |
87 MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)))
88 return MFX_ERR_UNSUPPORTED;
90 va_fourcc = gst_msdk_get_va_fourcc_from_mfx_fourcc (fourcc);
93 (GstMsdkMemoryID *) g_slice_alloc0 (surfaces_num *
94 sizeof (GstMsdkMemoryID));
95 mids = (mfxMemId *) g_slice_alloc0 (surfaces_num * sizeof (mfxMemId));
97 (VASurfaceID *) g_slice_alloc0 (surfaces_num * sizeof (VASurfaceID));
99 (GstMsdkAllocResponse *) g_slice_alloc0 (sizeof (GstMsdkAllocResponse));
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;
108 /* set VA_SURFACE_ATTRIB_USAGE_HINT_ENCODER flag for encoding */
109 #if (MFX_VERSION >= 1025)
110 if ((req->Type & MFX_MEMTYPE_VIDEO_MEMORY_ENCODER_TARGET) &&
111 (req->Type & MFX_MEMTYPE_FROM_ENCODE)) {
112 attribs[1].type = VASurfaceAttribUsageHint;
113 attribs[1].flags = VA_SURFACE_ATTRIB_SETTABLE;
114 attribs[1].value.type = VAGenericValueTypeInteger;
115 attribs[1].value.value.i = VA_SURFACE_ATTRIB_USAGE_HINT_ENCODER;
121 gst_msdk_get_va_rt_format_from_mfx_rt_format (req->Info.ChromaFormat);
123 if (format == VA_RT_FORMAT_YUV420 && va_fourcc == VA_FOURCC_P010)
124 #if VA_CHECK_VERSION(1, 2, 0)
125 format = VA_RT_FORMAT_YUV420_10;
127 format = VA_RT_FORMAT_YUV420_10BPP;
130 #if VA_CHECK_VERSION(1, 4, 1)
131 if (format == VA_RT_FORMAT_YUV444 && va_fourcc == VA_FOURCC_A2R10G10B10)
132 format = VA_RT_FORMAT_RGB32_10;
135 #if ((MFX_VERSION >= 1027) && VA_CHECK_VERSION(1, 2, 0))
136 if (format == VA_RT_FORMAT_YUV422 && va_fourcc == VA_FOURCC_Y210)
137 format = VA_RT_FORMAT_YUV422_10;
138 else if (format == VA_RT_FORMAT_YUV444 && va_fourcc == VA_FOURCC_Y410)
139 format = VA_RT_FORMAT_YUV444_10;
142 #if ((MFX_VERSION >= 1031) && VA_CHECK_VERSION(1, 2, 0))
143 if (format == VA_RT_FORMAT_YUV420 && va_fourcc == VA_FOURCC_P016)
144 format = VA_RT_FORMAT_YUV420_12;
146 if (format == VA_RT_FORMAT_YUV422 && va_fourcc == VA_FOURCC_Y216)
147 format = VA_RT_FORMAT_YUV422_12;
150 va_status = vaCreateSurfaces (gst_msdk_context_get_handle (context),
152 req->Info.Width, req->Info.Height, surfaces, surfaces_num, attribs,
155 status = gst_msdk_get_mfx_status_from_va_status (va_status);
156 if (status != MFX_ERR_NONE) {
157 GST_WARNING ("failed to create VA surface");
161 for (i = 0; i < surfaces_num; i++) {
162 /* Get dmabuf handle if MFX_MEMTYPE_EXPORT_FRAME */
163 if (req->Type & MFX_MEMTYPE_EXPORT_FRAME) {
164 msdk_mids[i].info.mem_type = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
166 vaDeriveImage (gst_msdk_context_get_handle (context), surfaces[i],
167 &msdk_mids[i].image);
168 status = gst_msdk_get_mfx_status_from_va_status (va_status);
170 if (MFX_ERR_NONE != status) {
171 GST_ERROR ("failed to derive image");
176 vaAcquireBufferHandle (gst_msdk_context_get_handle (context),
177 msdk_mids[i].image.buf, &msdk_mids[i].info);
178 status = gst_msdk_get_mfx_status_from_va_status (va_status);
180 if (MFX_ERR_NONE != status) {
181 GST_ERROR ("failed to get dmabuf handle");
182 va_status = vaDestroyImage (gst_msdk_context_get_handle (context),
183 msdk_mids[i].image.image_id);
184 if (va_status == VA_STATUS_SUCCESS) {
185 msdk_mids[i].image.image_id = VA_INVALID_ID;
186 msdk_mids[i].image.buf = VA_INVALID_ID;
190 /* useful to check the image mapping state later */
191 msdk_mids[i].image.image_id = VA_INVALID_ID;
192 msdk_mids[i].image.buf = VA_INVALID_ID;
195 msdk_mids[i].surface = &surfaces[i];
196 mids[i] = (mfxMemId *) & msdk_mids[i];
199 /* This is requested from the driver when h265 encoding.
200 * These buffers will be used inside the driver and released by
201 * gst_msdk_frame_free functions. Application doesn't need to handle these buffers.
203 * See https://github.com/Intel-Media-SDK/samples/issues/13 for more details.
205 VAContextID context_id = req->AllocId;
206 gint width32 = 32 * ((req->Info.Width + 31) >> 5);
207 gint height32 = 32 * ((req->Info.Height + 31) >> 5);
208 guint64 codedbuf_size = (width32 * height32) * 400LL / (16 * 16);
210 for (i = 0; i < surfaces_num; i++) {
211 VABufferID coded_buf;
213 va_status = vaCreateBuffer (gst_msdk_context_get_handle (context),
214 context_id, VAEncCodedBufferType, codedbuf_size, 1, NULL, &coded_buf);
216 status = gst_msdk_get_mfx_status_from_va_status (va_status);
217 if (status < MFX_ERR_NONE) {
218 GST_ERROR ("failed to create buffer");
222 surfaces[i] = coded_buf;
223 msdk_mids[i].surface = &surfaces[i];
224 msdk_mids[i].fourcc = fourcc;
225 mids[i] = (mfxMemId *) & msdk_mids[i];
230 resp->NumFrameActual = surfaces_num;
232 msdk_resp->response = *resp;
233 msdk_resp->request = *req;
234 msdk_resp->refcount = 1;
236 gst_msdk_context_add_alloc_response (context, msdk_resp);
242 gst_msdk_frame_free (mfxHDL pthis, mfxFrameAllocResponse * resp)
244 GstMsdkContext *context = (GstMsdkContext *) pthis;
245 VAStatus va_status = VA_STATUS_SUCCESS;
247 GstMsdkMemoryID *mem_id;
250 GstMsdkAllocResponse *cached = NULL;
252 cached = gst_msdk_context_get_cached_alloc_responses (context, resp);
255 if (!g_atomic_int_dec_and_test (&cached->refcount))
260 if (!gst_msdk_context_remove_alloc_response (context, resp))
263 mem_id = resp->mids[0];
264 dpy = gst_msdk_context_get_handle (context);
266 if (mem_id->fourcc != MFX_FOURCC_P8) {
267 /* Make sure that all the vaImages are destroyed */
268 for (i = 0; i < resp->NumFrameActual; i++) {
269 GstMsdkMemoryID *mem = resp->mids[i];
271 /* Release dmabuf handle if used */
272 if (mem->info.mem_type == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME)
273 vaReleaseBufferHandle (dpy, mem->image.buf);
275 if (mem->image.image_id != VA_INVALID_ID &&
276 vaDestroyImage (dpy, mem->image.image_id) == VA_STATUS_SUCCESS) {
277 mem_id->image.image_id = VA_INVALID_ID;
278 mem_id->image.buf = VA_INVALID_ID;
283 vaDestroySurfaces (dpy, (VASurfaceID *) mem_id->surface,
284 resp->NumFrameActual);
286 VASurfaceID *surfaces = mem_id->surface;
288 for (i = 0; i < resp->NumFrameActual; i++) {
289 va_status = vaDestroyBuffer (dpy, surfaces[i]);
293 g_slice_free1 (resp->NumFrameActual * sizeof (VASurfaceID), mem_id->surface);
294 g_slice_free1 (resp->NumFrameActual * sizeof (GstMsdkMemoryID), mem_id);
295 g_slice_free1 (resp->NumFrameActual * sizeof (mfxMemId), resp->mids);
297 status = gst_msdk_get_mfx_status_from_va_status (va_status);
302 gst_msdk_frame_lock (mfxHDL pthis, mfxMemId mid, mfxFrameData * data)
304 GstMsdkContext *context = (GstMsdkContext *) pthis;
308 VASurfaceID *va_surface;
310 GstMsdkMemoryID *mem_id;
312 mem_id = (GstMsdkMemoryID *) mid;
313 va_surface = mem_id->surface;
314 dpy = gst_msdk_context_get_handle (context);
316 if (mem_id->info.mem_type == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME) {
317 GST_WARNING ("Couldn't map the buffer since dmabuf is already in use");
318 return MFX_ERR_LOCK_MEMORY;
321 if (mem_id->fourcc != MFX_FOURCC_P8) {
322 va_status = vaDeriveImage (dpy, *va_surface, &mem_id->image);
323 status = gst_msdk_get_mfx_status_from_va_status (va_status);
325 if (status != MFX_ERR_NONE) {
326 GST_WARNING ("failed to derive image");
330 va_status = vaMapBuffer (dpy, mem_id->image.buf, (void **) &buf);
331 status = gst_msdk_get_mfx_status_from_va_status (va_status);
333 if (status != MFX_ERR_NONE) {
334 GST_WARNING ("failed to map");
335 if (vaDestroyImage (dpy, mem_id->image.image_id) == VA_STATUS_SUCCESS) {
336 mem_id->image.image_id = VA_INVALID_ID;
337 mem_id->image.buf = VA_INVALID_ID;
342 switch (mem_id->image.format.fourcc) {
346 data->Pitch = mem_id->image.pitches[0];
347 data->Y = buf + mem_id->image.offsets[0];
348 data->UV = buf + mem_id->image.offsets[1];
351 data->Pitch = mem_id->image.pitches[0];
352 data->Y = buf + mem_id->image.offsets[0];
353 data->U = buf + mem_id->image.offsets[2];
354 data->V = buf + mem_id->image.offsets[1];
357 data->Pitch = mem_id->image.pitches[0];
358 data->Y = buf + mem_id->image.offsets[0];
359 data->U = data->Y + 1;
360 data->V = data->Y + 3;
363 data->Pitch = mem_id->image.pitches[0];
364 data->Y = buf + mem_id->image.offsets[0];
365 data->U = data->U + 1;
366 data->V = data->U + 2;
369 data->Pitch = mem_id->image.pitches[0];
370 data->B = buf + mem_id->image.offsets[0];
371 data->G = data->B + 1;
372 data->R = data->B + 2;
373 data->A = data->B + 3;
375 #if (MFX_VERSION >= 1028)
376 case VA_FOURCC_RGB565:
377 data->Pitch = mem_id->image.pitches[0];
378 data->R = buf + mem_id->image.offsets[0];
384 data->PitchHigh = (mfxU16) (mem_id->image.pitches[0] / (1 << 16));
385 data->PitchLow = (mfxU16) (mem_id->image.pitches[0] % (1 << 16));
386 data->V = buf + mem_id->image.offsets[0];
387 data->U = data->V + 1;
388 data->Y = data->V + 2;
389 data->A = data->V + 3;
391 #if VA_CHECK_VERSION(1, 4, 1)
392 case VA_FOURCC_A2R10G10B10:
393 data->Pitch = mem_id->image.pitches[0];
394 data->R = buf + mem_id->image.offsets[0];
400 #if VA_CHECK_VERSION(1, 2, 0)
403 data->Pitch = mem_id->image.pitches[0];
404 data->Y = buf + mem_id->image.offsets[0];
405 data->U = data->Y + 2;
406 data->V = data->Y + 6;
409 data->Pitch = mem_id->image.pitches[0];
410 data->U = buf + mem_id->image.offsets[0]; /* data->Y410 */
414 data->Pitch = mem_id->image.pitches[0];
415 data->R = buf + mem_id->image.offsets[0];
416 data->G = data->R + 1;
417 data->B = data->R + 2;
418 data->A = data->R + 3;
422 g_assert_not_reached ();
426 VACodedBufferSegment *coded_buffer_segment;
428 vaMapBuffer (dpy, *va_surface, (void **) (&coded_buffer_segment));
429 status = gst_msdk_get_mfx_status_from_va_status (va_status);
430 if (MFX_ERR_NONE == status)
431 data->Y = (mfxU8 *) coded_buffer_segment->buf;
438 gst_msdk_frame_unlock (mfxHDL pthis, mfxMemId mid, mfxFrameData * ptr)
440 GstMsdkContext *context = (GstMsdkContext *) pthis;
444 GstMsdkMemoryID *mem_id;
446 mem_id = (GstMsdkMemoryID *) mid;
447 dpy = gst_msdk_context_get_handle (context);
449 if (mem_id->fourcc != MFX_FOURCC_P8) {
450 vaUnmapBuffer (dpy, mem_id->image.buf);
451 va_status = vaDestroyImage (dpy, mem_id->image.image_id);
453 if (va_status == VA_STATUS_SUCCESS) {
454 mem_id->image.image_id = VA_INVALID_ID;
455 mem_id->image.buf = VA_INVALID_ID;
458 va_status = vaUnmapBuffer (dpy, *(mem_id->surface));
461 status = gst_msdk_get_mfx_status_from_va_status (va_status);
467 gst_msdk_frame_get_hdl (mfxHDL pthis, mfxMemId mid, mfxHDL * hdl)
469 GstMsdkMemoryID *mem_id;
472 return MFX_ERR_INVALID_HANDLE;
475 *hdl = mem_id->surface;
481 gst_msdk_set_frame_allocator (GstMsdkContext * context)
483 mfxFrameAllocator gst_msdk_frame_allocator = {
485 .Alloc = gst_msdk_frame_alloc,
486 .Lock = gst_msdk_frame_lock,
487 .Unlock = gst_msdk_frame_unlock,
488 .GetHDL = gst_msdk_frame_get_hdl,
489 .Free = gst_msdk_frame_free,
492 gst_msdk_context_set_frame_allocator (context, &gst_msdk_frame_allocator);
496 gst_msdk_get_dmabuf_info_from_surface (mfxFrameSurface1 * surface,
497 gint * handle, gsize * size)
499 GstMsdkMemoryID *mem_id;
500 g_return_val_if_fail (surface, FALSE);
502 mem_id = (GstMsdkMemoryID *) surface->Data.MemId;
504 *handle = mem_id->info.handle;
506 *size = mem_id->info.mem_size;
512 gst_msdk_export_dmabuf_to_vasurface (GstMsdkContext * context,
513 GstVideoInfo * vinfo, gint fd, VASurfaceID * surface_id)
515 GstVideoFormat format;
516 guint width, height, size, i;
517 unsigned long extbuf_handle;
518 guint va_fourcc = 0, va_chroma = 0;
519 VASurfaceAttrib attribs[2], *attrib;
520 VASurfaceAttribExternalBuffers extbuf;
522 mfxStatus status = MFX_ERR_NONE;
524 g_return_val_if_fail (context != NULL, FALSE);
525 g_return_val_if_fail (vinfo != NULL, FALSE);
526 g_return_val_if_fail (fd >= 0, FALSE);
528 extbuf_handle = (guintptr) (fd);
530 format = GST_VIDEO_INFO_FORMAT (vinfo);
531 width = GST_VIDEO_INFO_WIDTH (vinfo);
532 height = GST_VIDEO_INFO_HEIGHT (vinfo);
533 size = GST_VIDEO_INFO_SIZE (vinfo);
535 /* Fixme: Move to common format handling util */
537 case GST_VIDEO_FORMAT_NV12:
538 va_chroma = VA_RT_FORMAT_YUV420;
539 va_fourcc = VA_FOURCC_NV12;
541 case GST_VIDEO_FORMAT_BGRA:
542 va_chroma = VA_RT_FORMAT_YUV444;
543 va_fourcc = VA_FOURCC_BGRA;
545 case GST_VIDEO_FORMAT_YUY2:
546 va_chroma = VA_RT_FORMAT_YUV422;
547 va_fourcc = VA_FOURCC_YUY2;
549 case GST_VIDEO_FORMAT_P010_10LE:
550 #if VA_CHECK_VERSION(1, 2, 0)
551 va_chroma = VA_RT_FORMAT_YUV420_10;
553 va_chroma = VA_RT_FORMAT_YUV420_10BPP;
555 va_fourcc = VA_FOURCC_P010;
557 case GST_VIDEO_FORMAT_UYVY:
558 va_chroma = VA_RT_FORMAT_YUV422;
559 va_fourcc = VA_FOURCC_UYVY;
561 #if (MFX_VERSION >= 1028)
562 case GST_VIDEO_FORMAT_RGB16:
563 va_chroma = VA_RT_FORMAT_RGB16;
564 va_fourcc = VA_FOURCC_RGB565;
567 case GST_VIDEO_FORMAT_VUYA:
568 va_chroma = VA_RT_FORMAT_YUV444;
569 va_fourcc = VA_FOURCC_AYUV;
571 #if VA_CHECK_VERSION(1, 4, 1)
572 case GST_VIDEO_FORMAT_BGR10A2_LE:
573 va_chroma = VA_RT_FORMAT_RGB32_10;
574 va_fourcc = VA_FOURCC_A2R10G10B10;
577 #if VA_CHECK_VERSION(1, 2, 0)
578 case GST_VIDEO_FORMAT_Y210:
579 va_chroma = VA_RT_FORMAT_YUV422_10;
580 va_fourcc = VA_FOURCC_Y210;
582 case GST_VIDEO_FORMAT_Y410:
583 va_chroma = VA_RT_FORMAT_YUV444_10;
584 va_fourcc = VA_FOURCC_Y410;
586 case GST_VIDEO_FORMAT_P012_LE:
587 va_chroma = VA_RT_FORMAT_YUV420_12;
588 va_fourcc = VA_FOURCC_P016;
592 goto error_unsupported_format;
595 /* Fill the VASurfaceAttribExternalBuffers */
596 extbuf.pixel_format = va_fourcc;
597 extbuf.width = width;
598 extbuf.height = height;
599 extbuf.data_size = size;
600 extbuf.num_planes = GST_VIDEO_INFO_N_PLANES (vinfo);
601 for (i = 0; i < extbuf.num_planes; i++) {
602 extbuf.pitches[i] = GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i);
603 extbuf.offsets[i] = GST_VIDEO_INFO_PLANE_OFFSET (vinfo, i);
605 extbuf.buffers = (uintptr_t *) & extbuf_handle;
606 extbuf.num_buffers = 1;
608 extbuf.private_data = NULL;
610 /* Fill the Surface Attributes */
612 attrib->type = VASurfaceAttribMemoryType;
613 attrib->flags = VA_SURFACE_ATTRIB_SETTABLE;
614 attrib->value.type = VAGenericValueTypeInteger;
615 attrib->value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
617 attrib->type = VASurfaceAttribExternalBufferDescriptor;
618 attrib->flags = VA_SURFACE_ATTRIB_SETTABLE;
619 attrib->value.type = VAGenericValueTypePointer;
620 attrib->value.value.p = &extbuf;
623 va_status = vaCreateSurfaces (gst_msdk_context_get_handle (context),
624 va_chroma, width, height, surface_id, 1, attribs, attrib - attribs);
625 status = gst_msdk_get_mfx_status_from_va_status (va_status);
626 if (status != MFX_ERR_NONE)
627 goto error_create_surface;
631 error_unsupported_format:
633 GST_ERROR ("Unsupported Video format %s, Can't export dmabuf to vaSurface",
634 gst_video_format_to_string (format));
637 error_create_surface:
639 GST_ERROR ("Failed to create the VASurface from DRM_PRIME FD");
645 * gst_msdk_replace_mfx_memid:
646 * This method replace the internal VA Suface in mfxSurface with a new one
648 * Caution: Not a thread-safe routine, this method is here to work around
649 * the dmabuf-import use case with dynamic memID replacement where msdk
650 * originally Inited with fake memIDs.
652 * Don't use anywhere else unless you really know what you are doing!
655 gst_msdk_replace_mfx_memid (GstMsdkContext * context,
656 mfxFrameSurface1 * mfx_surface, VASurfaceID surface_id)
658 GstMsdkMemoryID *msdk_mid = NULL;
660 VASurfaceID *old_surface_id;
662 mfxStatus status = MFX_ERR_NONE;
664 g_return_val_if_fail (mfx_surface != NULL, FALSE);
665 g_return_val_if_fail (context != NULL, FALSE);
667 msdk_mid = (GstMsdkMemoryID *) mfx_surface->Data.MemId;
668 dpy = gst_msdk_context_get_handle (context);
670 /* Destroy the underlined VAImage if already mapped */
671 if (msdk_mid->image.image_id != VA_INVALID_ID
672 && msdk_mid->image.buf != VA_INVALID_ID) {
674 gst_msdk_frame_unlock ((mfxHDL) context, (mfxMemId) msdk_mid, NULL);
675 if (status != MFX_ERR_NONE)
676 goto error_destroy_va_image;
679 /* Destroy the associated VASurface */
680 old_surface_id = msdk_mid->surface;
681 if (*old_surface_id != VA_INVALID_ID) {
682 va_status = vaDestroySurfaces (dpy, old_surface_id, 1);
683 status = gst_msdk_get_mfx_status_from_va_status (va_status);
684 if (status != MFX_ERR_NONE)
685 goto error_destroy_va_surface;
688 *msdk_mid->surface = surface_id;
692 error_destroy_va_image:
694 GST_ERROR ("Failed to Destroy the VAImage");
697 error_destroy_va_surface:
699 GST_ERROR ("Failed to Destroy the VASurfaceID %x", *old_surface_id);