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 ((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;
119 gst_msdk_get_va_rt_format_from_mfx_rt_format (req->Info.ChromaFormat);
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;
125 format = VA_RT_FORMAT_YUV420_10BPP;
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;
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;
140 va_status = vaCreateSurfaces (gst_msdk_context_get_handle (context),
142 req->Info.Width, req->Info.Height, surfaces, surfaces_num, attribs,
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");
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;
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);
160 if (MFX_ERR_NONE != status) {
161 GST_ERROR ("failed to derive image");
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);
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;
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;
185 msdk_mids[i].surface = &surfaces[i];
186 mids[i] = (mfxMemId *) & msdk_mids[i];
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.
193 * See https://github.com/Intel-Media-SDK/samples/issues/13 for more details.
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);
200 for (i = 0; i < surfaces_num; i++) {
201 VABufferID coded_buf;
203 va_status = vaCreateBuffer (gst_msdk_context_get_handle (context),
204 context_id, VAEncCodedBufferType, codedbuf_size, 1, NULL, &coded_buf);
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");
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];
220 resp->NumFrameActual = surfaces_num;
222 msdk_resp->response = *resp;
223 msdk_resp->request = *req;
224 msdk_resp->refcount = 1;
226 gst_msdk_context_add_alloc_response (context, msdk_resp);
232 gst_msdk_frame_free (mfxHDL pthis, mfxFrameAllocResponse * resp)
234 GstMsdkContext *context = (GstMsdkContext *) pthis;
235 VAStatus va_status = VA_STATUS_SUCCESS;
237 GstMsdkMemoryID *mem_id;
240 GstMsdkAllocResponse *cached = NULL;
242 cached = gst_msdk_context_get_cached_alloc_responses (context, resp);
245 if (!g_atomic_int_dec_and_test (&cached->refcount))
250 if (!gst_msdk_context_remove_alloc_response (context, resp))
253 mem_id = resp->mids[0];
254 dpy = gst_msdk_context_get_handle (context);
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];
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);
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;
273 vaDestroySurfaces (dpy, (VASurfaceID *) mem_id->surface,
274 resp->NumFrameActual);
276 VASurfaceID *surfaces = mem_id->surface;
278 for (i = 0; i < resp->NumFrameActual; i++) {
279 va_status = vaDestroyBuffer (dpy, surfaces[i]);
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);
287 status = gst_msdk_get_mfx_status_from_va_status (va_status);
292 gst_msdk_frame_lock (mfxHDL pthis, mfxMemId mid, mfxFrameData * data)
294 GstMsdkContext *context = (GstMsdkContext *) pthis;
298 VASurfaceID *va_surface;
300 GstMsdkMemoryID *mem_id;
302 mem_id = (GstMsdkMemoryID *) mid;
303 va_surface = mem_id->surface;
304 dpy = gst_msdk_context_get_handle (context);
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;
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);
315 if (status != MFX_ERR_NONE) {
316 GST_WARNING ("failed to derive image");
320 va_status = vaMapBuffer (dpy, mem_id->image.buf, (void **) &buf);
321 status = gst_msdk_get_mfx_status_from_va_status (va_status);
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;
332 switch (mem_id->image.format.fourcc) {
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];
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];
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;
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;
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;
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];
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;
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];
389 #if VA_CHECK_VERSION(1, 2, 0)
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;
397 data->Pitch = mem_id->image.pitches[0];
398 data->U = buf + mem_id->image.offsets[0]; /* data->Y410 */
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;
410 g_assert_not_reached ();
414 VACodedBufferSegment *coded_buffer_segment;
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;
426 gst_msdk_frame_unlock (mfxHDL pthis, mfxMemId mid, mfxFrameData * ptr)
428 GstMsdkContext *context = (GstMsdkContext *) pthis;
432 GstMsdkMemoryID *mem_id;
434 mem_id = (GstMsdkMemoryID *) mid;
435 dpy = gst_msdk_context_get_handle (context);
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);
441 if (va_status == VA_STATUS_SUCCESS) {
442 mem_id->image.image_id = VA_INVALID_ID;
443 mem_id->image.buf = VA_INVALID_ID;
446 va_status = vaUnmapBuffer (dpy, *(mem_id->surface));
449 status = gst_msdk_get_mfx_status_from_va_status (va_status);
455 gst_msdk_frame_get_hdl (mfxHDL pthis, mfxMemId mid, mfxHDL * hdl)
457 GstMsdkMemoryID *mem_id;
460 return MFX_ERR_INVALID_HANDLE;
463 *hdl = mem_id->surface;
469 gst_msdk_set_frame_allocator (GstMsdkContext * context)
471 mfxFrameAllocator gst_msdk_frame_allocator = {
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,
480 gst_msdk_context_set_frame_allocator (context, &gst_msdk_frame_allocator);
484 gst_msdk_get_dmabuf_info_from_surface (mfxFrameSurface1 * surface,
485 gint * handle, gsize * size)
487 GstMsdkMemoryID *mem_id;
488 g_return_val_if_fail (surface, FALSE);
490 mem_id = (GstMsdkMemoryID *) surface->Data.MemId;
492 *handle = mem_id->info.handle;
494 *size = mem_id->info.mem_size;
500 gst_msdk_export_dmabuf_to_vasurface (GstMsdkContext * context,
501 GstVideoInfo * vinfo, gint fd, VASurfaceID * surface_id)
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;
510 mfxStatus status = MFX_ERR_NONE;
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);
516 extbuf_handle = (guintptr) (fd);
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);
523 /* Fixme: Move to common format handling util */
525 case GST_VIDEO_FORMAT_NV12:
526 va_chroma = VA_RT_FORMAT_YUV420;
527 va_fourcc = VA_FOURCC_NV12;
529 case GST_VIDEO_FORMAT_BGRA:
530 va_chroma = VA_RT_FORMAT_YUV444;
531 va_fourcc = VA_FOURCC_BGRA;
533 case GST_VIDEO_FORMAT_YUY2:
534 va_chroma = VA_RT_FORMAT_YUV422;
535 va_fourcc = VA_FOURCC_YUY2;
537 case GST_VIDEO_FORMAT_P010_10LE:
538 #if VA_CHECK_VERSION(1, 2, 0)
539 va_chroma = VA_RT_FORMAT_YUV420_10;
541 va_chroma = VA_RT_FORMAT_YUV420_10BPP;
543 va_fourcc = VA_FOURCC_P010;
545 case GST_VIDEO_FORMAT_UYVY:
546 va_chroma = VA_RT_FORMAT_YUV422;
547 va_fourcc = VA_FOURCC_UYVY;
549 #if (MFX_VERSION >= 1028)
550 case GST_VIDEO_FORMAT_RGB16:
551 va_chroma = VA_RT_FORMAT_RGB16;
552 va_fourcc = VA_FOURCC_RGB565;
555 case GST_VIDEO_FORMAT_VUYA:
556 va_chroma = VA_RT_FORMAT_YUV444;
557 va_fourcc = VA_FOURCC_AYUV;
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;
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;
570 case GST_VIDEO_FORMAT_Y410:
571 va_chroma = VA_RT_FORMAT_YUV444_10;
572 va_fourcc = VA_FOURCC_Y410;
576 goto error_unsupported_format;
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);
589 extbuf.buffers = (uintptr_t *) & extbuf_handle;
590 extbuf.num_buffers = 1;
592 extbuf.private_data = NULL;
594 /* Fill the Surface Attributes */
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;
601 attrib->type = VASurfaceAttribExternalBufferDescriptor;
602 attrib->flags = VA_SURFACE_ATTRIB_SETTABLE;
603 attrib->value.type = VAGenericValueTypePointer;
604 attrib->value.value.p = &extbuf;
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;
615 error_unsupported_format:
617 GST_ERROR ("Unsupported Video format %s, Can't export dmabuf to vaSurface",
618 gst_video_format_to_string (format));
621 error_create_surface:
623 GST_ERROR ("Failed to create the VASurface from DRM_PRIME FD");
629 * gst_msdk_replace_mfx_memid:
630 * This method replace the internal VA Suface in mfxSurface with a new one
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.
636 * Don't use anywhere else unless you really know what you are doing!
639 gst_msdk_replace_mfx_memid (GstMsdkContext * context,
640 mfxFrameSurface1 * mfx_surface, VASurfaceID surface_id)
642 GstMsdkMemoryID *msdk_mid = NULL;
644 VASurfaceID *old_surface_id;
646 mfxStatus status = MFX_ERR_NONE;
648 g_return_val_if_fail (mfx_surface != NULL, FALSE);
649 g_return_val_if_fail (context != NULL, FALSE);
651 msdk_mid = (GstMsdkMemoryID *) mfx_surface->Data.MemId;
652 dpy = gst_msdk_context_get_handle (context);
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) {
658 gst_msdk_frame_unlock ((mfxHDL) context, (mfxMemId) msdk_mid, NULL);
659 if (status != MFX_ERR_NONE)
660 goto error_destroy_va_image;
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;
672 *msdk_mid->surface = surface_id;
676 error_destroy_va_image:
678 GST_ERROR ("Failed to Destroy the VAImage");
681 error_destroy_va_surface:
683 GST_ERROR ("Failed to Destroy the VASurfaceID %x", *old_surface_id);