2018-02-13 21:43:42 +00:00
|
|
|
/* GStreamer Intel MSDK plugin
|
|
|
|
* Copyright (c) 2018, Intel Corporation
|
|
|
|
* Copyright (c) 2018, Igalia S.L.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* 3. Neither the name of the copyright holder nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGDECE
|
|
|
|
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <va/va.h>
|
2018-03-30 19:03:17 +00:00
|
|
|
#include <va/va_drmcommon.h>
|
2018-02-13 21:43:42 +00:00
|
|
|
#include "gstmsdkallocator.h"
|
2018-03-30 19:05:16 +00:00
|
|
|
#include "gstmsdkallocator_libva.h"
|
2018-02-13 21:43:42 +00:00
|
|
|
#include "msdk_libva.h"
|
|
|
|
|
|
|
|
mfxStatus
|
|
|
|
gst_msdk_frame_alloc (mfxHDL pthis, mfxFrameAllocRequest * req,
|
|
|
|
mfxFrameAllocResponse * resp)
|
|
|
|
{
|
|
|
|
VAStatus va_status;
|
|
|
|
mfxStatus status = MFX_ERR_NONE;
|
|
|
|
gint i;
|
|
|
|
guint format;
|
|
|
|
guint va_fourcc = 0;
|
|
|
|
VASurfaceID *surfaces = NULL;
|
2019-10-09 05:41:09 +00:00
|
|
|
VASurfaceAttrib attribs[2];
|
|
|
|
guint num_attribs = 0;
|
2018-02-13 21:43:42 +00:00
|
|
|
mfxMemId *mids = NULL;
|
|
|
|
GstMsdkContext *context = (GstMsdkContext *) pthis;
|
|
|
|
GstMsdkMemoryID *msdk_mids = NULL;
|
|
|
|
GstMsdkAllocResponse *msdk_resp = NULL;
|
|
|
|
mfxU32 fourcc = req->Info.FourCC;
|
|
|
|
mfxU16 surfaces_num = req->NumFrameSuggested;
|
|
|
|
|
2019-04-28 08:10:13 +00:00
|
|
|
/* MFX_MAKEFOURCC('V','P','8','S') is used for MFX_FOURCC_VP9_SEGMAP surface
|
|
|
|
* in MSDK and this surface is an internal surface. The external allocator
|
|
|
|
* shouldn't be used for this surface allocation
|
|
|
|
*
|
|
|
|
* See https://github.com/Intel-Media-SDK/MediaSDK/issues/762
|
|
|
|
*/
|
|
|
|
if (req->Type & MFX_MEMTYPE_INTERNAL_FRAME
|
|
|
|
&& fourcc == MFX_MAKEFOURCC ('V', 'P', '8', 'S'))
|
|
|
|
return MFX_ERR_UNSUPPORTED;
|
|
|
|
|
2018-02-13 21:43:42 +00:00
|
|
|
if (req->Type & MFX_MEMTYPE_EXTERNAL_FRAME) {
|
|
|
|
GstMsdkAllocResponse *cached =
|
|
|
|
gst_msdk_context_get_cached_alloc_responses_by_request (context, req);
|
|
|
|
if (cached) {
|
2018-03-30 19:02:26 +00:00
|
|
|
/* check if enough frames were allocated */
|
2019-09-11 02:49:10 +00:00
|
|
|
if (req->NumFrameSuggested > cached->response.NumFrameActual)
|
2018-03-30 19:02:26 +00:00
|
|
|
return MFX_ERR_MEMORY_ALLOC;
|
|
|
|
|
2019-09-11 02:49:10 +00:00
|
|
|
*resp = cached->response;
|
2019-09-09 05:40:53 +00:00
|
|
|
g_atomic_int_inc (&cached->refcount);
|
2018-02-13 21:43:42 +00:00
|
|
|
return MFX_ERR_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The VA API does not define any surface types and the application can use either
|
|
|
|
* MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET or
|
|
|
|
* MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET to indicate data in video memory.
|
|
|
|
*/
|
|
|
|
if (!(req->Type & (MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET |
|
|
|
|
MFX_MEMTYPE_VIDEO_MEMORY_PROCESSOR_TARGET)))
|
|
|
|
return MFX_ERR_UNSUPPORTED;
|
|
|
|
|
|
|
|
va_fourcc = gst_msdk_get_va_fourcc_from_mfx_fourcc (fourcc);
|
|
|
|
|
|
|
|
msdk_mids =
|
|
|
|
(GstMsdkMemoryID *) g_slice_alloc0 (surfaces_num *
|
|
|
|
sizeof (GstMsdkMemoryID));
|
|
|
|
mids = (mfxMemId *) g_slice_alloc0 (surfaces_num * sizeof (mfxMemId));
|
|
|
|
surfaces =
|
|
|
|
(VASurfaceID *) g_slice_alloc0 (surfaces_num * sizeof (VASurfaceID));
|
|
|
|
msdk_resp =
|
|
|
|
(GstMsdkAllocResponse *) g_slice_alloc0 (sizeof (GstMsdkAllocResponse));
|
|
|
|
|
|
|
|
if (va_fourcc != VA_FOURCC_P208) {
|
2019-10-09 05:41:09 +00:00
|
|
|
attribs[0].type = VASurfaceAttribPixelFormat;
|
|
|
|
attribs[0].flags = VA_SURFACE_ATTRIB_SETTABLE;
|
|
|
|
attribs[0].value.type = VAGenericValueTypeInteger;
|
|
|
|
attribs[0].value.value.i = va_fourcc;
|
|
|
|
num_attribs = 1;
|
|
|
|
|
|
|
|
/* set VA_SURFACE_ATTRIB_USAGE_HINT_ENCODER flag for encoding */
|
2020-02-12 02:24:55 +00:00
|
|
|
#if (MFX_VERSION >= 1025)
|
2019-10-09 05:41:09 +00:00
|
|
|
if ((req->Type & MFX_MEMTYPE_VIDEO_MEMORY_ENCODER_TARGET) &&
|
|
|
|
(req->Type & MFX_MEMTYPE_FROM_ENCODE)) {
|
|
|
|
attribs[1].type = VASurfaceAttribUsageHint;
|
|
|
|
attribs[1].flags = VA_SURFACE_ATTRIB_SETTABLE;
|
|
|
|
attribs[1].value.type = VAGenericValueTypeInteger;
|
|
|
|
attribs[1].value.value.i = VA_SURFACE_ATTRIB_USAGE_HINT_ENCODER;
|
|
|
|
num_attribs = 2;
|
|
|
|
}
|
2020-02-12 02:24:55 +00:00
|
|
|
#endif
|
2018-02-13 21:43:42 +00:00
|
|
|
|
|
|
|
format =
|
|
|
|
gst_msdk_get_va_rt_format_from_mfx_rt_format (req->Info.ChromaFormat);
|
|
|
|
|
2018-11-16 12:01:52 +00:00
|
|
|
if (format == VA_RT_FORMAT_YUV420 && va_fourcc == VA_FOURCC_P010)
|
2019-08-24 03:18:20 +00:00
|
|
|
#if VA_CHECK_VERSION(1, 2, 0)
|
2018-11-16 12:01:52 +00:00
|
|
|
format = VA_RT_FORMAT_YUV420_10;
|
2019-08-24 03:18:20 +00:00
|
|
|
#else
|
|
|
|
format = VA_RT_FORMAT_YUV420_10BPP;
|
|
|
|
#endif
|
2018-11-16 12:01:52 +00:00
|
|
|
|
2019-04-17 01:12:52 +00:00
|
|
|
#if VA_CHECK_VERSION(1, 4, 1)
|
2019-03-21 07:34:36 +00:00
|
|
|
if (format == VA_RT_FORMAT_YUV444 && va_fourcc == VA_FOURCC_A2R10G10B10)
|
|
|
|
format = VA_RT_FORMAT_RGB32_10;
|
2019-04-17 01:12:52 +00:00
|
|
|
#endif
|
2019-03-21 07:34:36 +00:00
|
|
|
|
2019-05-30 03:02:08 +00:00
|
|
|
#if ((MFX_VERSION >= 1027) && VA_CHECK_VERSION(1, 2, 0))
|
|
|
|
if (format == VA_RT_FORMAT_YUV422 && va_fourcc == VA_FOURCC_Y210)
|
|
|
|
format = VA_RT_FORMAT_YUV422_10;
|
2019-05-31 07:26:50 +00:00
|
|
|
else if (format == VA_RT_FORMAT_YUV444 && va_fourcc == VA_FOURCC_Y410)
|
|
|
|
format = VA_RT_FORMAT_YUV444_10;
|
2019-05-30 03:02:08 +00:00
|
|
|
#endif
|
|
|
|
|
2020-03-10 07:41:50 +00:00
|
|
|
#if ((MFX_VERSION >= 1031) && VA_CHECK_VERSION(1, 2, 0))
|
|
|
|
if (format == VA_RT_FORMAT_YUV420 && va_fourcc == VA_FOURCC_P016)
|
|
|
|
format = VA_RT_FORMAT_YUV420_12;
|
2020-03-11 06:09:11 +00:00
|
|
|
|
|
|
|
if (format == VA_RT_FORMAT_YUV422 && va_fourcc == VA_FOURCC_Y216)
|
|
|
|
format = VA_RT_FORMAT_YUV422_12;
|
2020-03-12 05:12:33 +00:00
|
|
|
|
|
|
|
if (format == VA_RT_FORMAT_YUV444 && va_fourcc == VA_FOURCC_Y416)
|
|
|
|
format = VA_RT_FORMAT_YUV444_12;
|
2020-03-10 07:41:50 +00:00
|
|
|
#endif
|
|
|
|
|
2018-02-13 21:43:42 +00:00
|
|
|
va_status = vaCreateSurfaces (gst_msdk_context_get_handle (context),
|
|
|
|
format,
|
2019-10-09 05:41:09 +00:00
|
|
|
req->Info.Width, req->Info.Height, surfaces, surfaces_num, attribs,
|
|
|
|
num_attribs);
|
2018-02-13 21:43:42 +00:00
|
|
|
|
|
|
|
status = gst_msdk_get_mfx_status_from_va_status (va_status);
|
|
|
|
if (status != MFX_ERR_NONE) {
|
|
|
|
GST_WARNING ("failed to create VA surface");
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < surfaces_num; i++) {
|
2018-03-30 19:03:17 +00:00
|
|
|
/* Get dmabuf handle if MFX_MEMTYPE_EXPORT_FRAME */
|
|
|
|
if (req->Type & MFX_MEMTYPE_EXPORT_FRAME) {
|
|
|
|
msdk_mids[i].info.mem_type = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
|
|
|
|
va_status =
|
|
|
|
vaDeriveImage (gst_msdk_context_get_handle (context), surfaces[i],
|
|
|
|
&msdk_mids[i].image);
|
|
|
|
status = gst_msdk_get_mfx_status_from_va_status (va_status);
|
|
|
|
|
|
|
|
if (MFX_ERR_NONE != status) {
|
|
|
|
GST_ERROR ("failed to derive image");
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_status =
|
|
|
|
vaAcquireBufferHandle (gst_msdk_context_get_handle (context),
|
|
|
|
msdk_mids[i].image.buf, &msdk_mids[i].info);
|
|
|
|
status = gst_msdk_get_mfx_status_from_va_status (va_status);
|
|
|
|
|
|
|
|
if (MFX_ERR_NONE != status) {
|
|
|
|
GST_ERROR ("failed to get dmabuf handle");
|
2019-01-18 07:14:59 +00:00
|
|
|
va_status = vaDestroyImage (gst_msdk_context_get_handle (context),
|
2018-03-30 19:03:17 +00:00
|
|
|
msdk_mids[i].image.image_id);
|
2019-01-18 07:14:59 +00:00
|
|
|
if (va_status == VA_STATUS_SUCCESS) {
|
|
|
|
msdk_mids[i].image.image_id = VA_INVALID_ID;
|
|
|
|
msdk_mids[i].image.buf = VA_INVALID_ID;
|
|
|
|
}
|
2018-03-30 19:03:17 +00:00
|
|
|
}
|
2018-05-31 00:23:44 +00:00
|
|
|
} else {
|
|
|
|
/* useful to check the image mapping state later */
|
|
|
|
msdk_mids[i].image.image_id = VA_INVALID_ID;
|
|
|
|
msdk_mids[i].image.buf = VA_INVALID_ID;
|
2018-03-30 19:03:17 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 21:43:42 +00:00
|
|
|
msdk_mids[i].surface = &surfaces[i];
|
|
|
|
mids[i] = (mfxMemId *) & msdk_mids[i];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* This is requested from the driver when h265 encoding.
|
|
|
|
* These buffers will be used inside the driver and released by
|
|
|
|
* gst_msdk_frame_free functions. Application doesn't need to handle these buffers.
|
|
|
|
*
|
|
|
|
* See https://github.com/Intel-Media-SDK/samples/issues/13 for more details.
|
|
|
|
*/
|
|
|
|
VAContextID context_id = req->AllocId;
|
|
|
|
gint width32 = 32 * ((req->Info.Width + 31) >> 5);
|
|
|
|
gint height32 = 32 * ((req->Info.Height + 31) >> 5);
|
|
|
|
guint64 codedbuf_size = (width32 * height32) * 400LL / (16 * 16);
|
|
|
|
|
|
|
|
for (i = 0; i < surfaces_num; i++) {
|
|
|
|
VABufferID coded_buf;
|
|
|
|
|
|
|
|
va_status = vaCreateBuffer (gst_msdk_context_get_handle (context),
|
|
|
|
context_id, VAEncCodedBufferType, codedbuf_size, 1, NULL, &coded_buf);
|
|
|
|
|
|
|
|
status = gst_msdk_get_mfx_status_from_va_status (va_status);
|
|
|
|
if (status < MFX_ERR_NONE) {
|
|
|
|
GST_ERROR ("failed to create buffer");
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
surfaces[i] = coded_buf;
|
|
|
|
msdk_mids[i].surface = &surfaces[i];
|
|
|
|
msdk_mids[i].fourcc = fourcc;
|
|
|
|
mids[i] = (mfxMemId *) & msdk_mids[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp->mids = mids;
|
|
|
|
resp->NumFrameActual = surfaces_num;
|
|
|
|
|
2019-09-11 02:49:10 +00:00
|
|
|
msdk_resp->response = *resp;
|
2018-02-13 21:43:42 +00:00
|
|
|
msdk_resp->request = *req;
|
2019-09-09 05:40:53 +00:00
|
|
|
msdk_resp->refcount = 1;
|
2018-02-13 21:43:42 +00:00
|
|
|
|
|
|
|
gst_msdk_context_add_alloc_response (context, msdk_resp);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
mfxStatus
|
|
|
|
gst_msdk_frame_free (mfxHDL pthis, mfxFrameAllocResponse * resp)
|
|
|
|
{
|
|
|
|
GstMsdkContext *context = (GstMsdkContext *) pthis;
|
|
|
|
VAStatus va_status = VA_STATUS_SUCCESS;
|
|
|
|
mfxStatus status;
|
|
|
|
GstMsdkMemoryID *mem_id;
|
|
|
|
VADisplay dpy;
|
|
|
|
gint i;
|
2019-09-09 05:40:53 +00:00
|
|
|
GstMsdkAllocResponse *cached = NULL;
|
|
|
|
|
|
|
|
cached = gst_msdk_context_get_cached_alloc_responses (context, resp);
|
|
|
|
|
|
|
|
if (cached) {
|
|
|
|
if (!g_atomic_int_dec_and_test (&cached->refcount))
|
|
|
|
return MFX_ERR_NONE;
|
|
|
|
} else
|
|
|
|
return MFX_ERR_NONE;
|
2018-02-13 21:43:42 +00:00
|
|
|
|
|
|
|
if (!gst_msdk_context_remove_alloc_response (context, resp))
|
|
|
|
return MFX_ERR_NONE;
|
|
|
|
|
|
|
|
mem_id = resp->mids[0];
|
|
|
|
dpy = gst_msdk_context_get_handle (context);
|
|
|
|
|
|
|
|
if (mem_id->fourcc != MFX_FOURCC_P8) {
|
|
|
|
/* Make sure that all the vaImages are destroyed */
|
|
|
|
for (i = 0; i < resp->NumFrameActual; i++) {
|
|
|
|
GstMsdkMemoryID *mem = resp->mids[i];
|
2018-03-30 19:03:17 +00:00
|
|
|
|
|
|
|
/* Release dmabuf handle if used */
|
|
|
|
if (mem->info.mem_type == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME)
|
|
|
|
vaReleaseBufferHandle (dpy, mem->image.buf);
|
|
|
|
|
2019-01-18 07:14:59 +00:00
|
|
|
if (mem->image.image_id != VA_INVALID_ID &&
|
|
|
|
vaDestroyImage (dpy, mem->image.image_id) == VA_STATUS_SUCCESS) {
|
|
|
|
mem_id->image.image_id = VA_INVALID_ID;
|
|
|
|
mem_id->image.buf = VA_INVALID_ID;
|
|
|
|
}
|
2018-02-13 21:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
va_status =
|
|
|
|
vaDestroySurfaces (dpy, (VASurfaceID *) mem_id->surface,
|
|
|
|
resp->NumFrameActual);
|
|
|
|
} else {
|
|
|
|
VASurfaceID *surfaces = mem_id->surface;
|
|
|
|
|
|
|
|
for (i = 0; i < resp->NumFrameActual; i++) {
|
|
|
|
va_status = vaDestroyBuffer (dpy, surfaces[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_slice_free1 (resp->NumFrameActual * sizeof (VASurfaceID), mem_id->surface);
|
|
|
|
g_slice_free1 (resp->NumFrameActual * sizeof (GstMsdkMemoryID), mem_id);
|
|
|
|
g_slice_free1 (resp->NumFrameActual * sizeof (mfxMemId), resp->mids);
|
|
|
|
|
|
|
|
status = gst_msdk_get_mfx_status_from_va_status (va_status);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
mfxStatus
|
|
|
|
gst_msdk_frame_lock (mfxHDL pthis, mfxMemId mid, mfxFrameData * data)
|
|
|
|
{
|
|
|
|
GstMsdkContext *context = (GstMsdkContext *) pthis;
|
|
|
|
VAStatus va_status;
|
|
|
|
mfxStatus status;
|
|
|
|
mfxU8 *buf = NULL;
|
|
|
|
VASurfaceID *va_surface;
|
|
|
|
VADisplay dpy;
|
|
|
|
GstMsdkMemoryID *mem_id;
|
|
|
|
|
|
|
|
mem_id = (GstMsdkMemoryID *) mid;
|
|
|
|
va_surface = mem_id->surface;
|
|
|
|
dpy = gst_msdk_context_get_handle (context);
|
|
|
|
|
2018-04-02 23:49:32 +00:00
|
|
|
if (mem_id->info.mem_type == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME) {
|
|
|
|
GST_WARNING ("Couldn't map the buffer since dmabuf is already in use");
|
|
|
|
return MFX_ERR_LOCK_MEMORY;
|
|
|
|
}
|
|
|
|
|
2018-02-13 21:43:42 +00:00
|
|
|
if (mem_id->fourcc != MFX_FOURCC_P8) {
|
|
|
|
va_status = vaDeriveImage (dpy, *va_surface, &mem_id->image);
|
|
|
|
status = gst_msdk_get_mfx_status_from_va_status (va_status);
|
|
|
|
|
|
|
|
if (status != MFX_ERR_NONE) {
|
|
|
|
GST_WARNING ("failed to derive image");
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_status = vaMapBuffer (dpy, mem_id->image.buf, (void **) &buf);
|
|
|
|
status = gst_msdk_get_mfx_status_from_va_status (va_status);
|
|
|
|
|
|
|
|
if (status != MFX_ERR_NONE) {
|
|
|
|
GST_WARNING ("failed to map");
|
2019-01-18 07:14:59 +00:00
|
|
|
if (vaDestroyImage (dpy, mem_id->image.image_id) == VA_STATUS_SUCCESS) {
|
|
|
|
mem_id->image.image_id = VA_INVALID_ID;
|
|
|
|
mem_id->image.buf = VA_INVALID_ID;
|
|
|
|
}
|
2018-02-13 21:43:42 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (mem_id->image.format.fourcc) {
|
|
|
|
case VA_FOURCC_NV12:
|
2018-11-16 06:32:29 +00:00
|
|
|
case VA_FOURCC_P010:
|
2020-03-10 07:41:50 +00:00
|
|
|
case VA_FOURCC_P016:
|
2018-02-13 21:43:42 +00:00
|
|
|
data->Pitch = mem_id->image.pitches[0];
|
|
|
|
data->Y = buf + mem_id->image.offsets[0];
|
|
|
|
data->UV = buf + mem_id->image.offsets[1];
|
|
|
|
break;
|
|
|
|
case VA_FOURCC_YV12:
|
|
|
|
data->Pitch = mem_id->image.pitches[0];
|
|
|
|
data->Y = buf + mem_id->image.offsets[0];
|
|
|
|
data->U = buf + mem_id->image.offsets[2];
|
|
|
|
data->V = buf + mem_id->image.offsets[1];
|
|
|
|
break;
|
|
|
|
case VA_FOURCC_YUY2:
|
|
|
|
data->Pitch = mem_id->image.pitches[0];
|
|
|
|
data->Y = buf + mem_id->image.offsets[0];
|
|
|
|
data->U = data->Y + 1;
|
|
|
|
data->V = data->Y + 3;
|
|
|
|
break;
|
|
|
|
case VA_FOURCC_UYVY:
|
|
|
|
data->Pitch = mem_id->image.pitches[0];
|
|
|
|
data->Y = buf + mem_id->image.offsets[0];
|
|
|
|
data->U = data->U + 1;
|
|
|
|
data->V = data->U + 2;
|
|
|
|
break;
|
|
|
|
case VA_FOURCC_ARGB:
|
|
|
|
data->Pitch = mem_id->image.pitches[0];
|
2019-10-09 06:00:01 +00:00
|
|
|
data->B = buf + mem_id->image.offsets[0];
|
|
|
|
data->G = data->B + 1;
|
|
|
|
data->R = data->B + 2;
|
|
|
|
data->A = data->B + 3;
|
2018-02-13 21:43:42 +00:00
|
|
|
break;
|
2019-02-28 12:37:49 +00:00
|
|
|
#if (MFX_VERSION >= 1028)
|
2019-02-25 04:18:05 +00:00
|
|
|
case VA_FOURCC_RGB565:
|
|
|
|
data->Pitch = mem_id->image.pitches[0];
|
|
|
|
data->R = buf + mem_id->image.offsets[0];
|
|
|
|
data->G = data->R;
|
|
|
|
data->B = data->R;
|
|
|
|
break;
|
2019-02-28 12:37:49 +00:00
|
|
|
#endif
|
2019-03-08 04:20:56 +00:00
|
|
|
case VA_FOURCC_AYUV:
|
|
|
|
data->PitchHigh = (mfxU16) (mem_id->image.pitches[0] / (1 << 16));
|
|
|
|
data->PitchLow = (mfxU16) (mem_id->image.pitches[0] % (1 << 16));
|
|
|
|
data->V = buf + mem_id->image.offsets[0];
|
|
|
|
data->U = data->V + 1;
|
|
|
|
data->Y = data->V + 2;
|
|
|
|
data->A = data->V + 3;
|
|
|
|
break;
|
2019-04-17 01:12:52 +00:00
|
|
|
#if VA_CHECK_VERSION(1, 4, 1)
|
2019-03-21 08:14:47 +00:00
|
|
|
case VA_FOURCC_A2R10G10B10:
|
|
|
|
data->Pitch = mem_id->image.pitches[0];
|
|
|
|
data->R = buf + mem_id->image.offsets[0];
|
|
|
|
data->G = data->R;
|
|
|
|
data->B = data->R;
|
|
|
|
data->A = data->R;
|
|
|
|
break;
|
2019-04-17 01:12:52 +00:00
|
|
|
#endif
|
2019-05-30 03:10:58 +00:00
|
|
|
#if VA_CHECK_VERSION(1, 2, 0)
|
|
|
|
case VA_FOURCC_Y210:
|
2020-03-11 06:09:11 +00:00
|
|
|
case VA_FOURCC_Y216:
|
2019-05-30 03:10:58 +00:00
|
|
|
data->Pitch = mem_id->image.pitches[0];
|
|
|
|
data->Y = buf + mem_id->image.offsets[0];
|
|
|
|
data->U = data->Y + 2;
|
|
|
|
data->V = data->Y + 6;
|
|
|
|
break;
|
2019-05-31 07:37:51 +00:00
|
|
|
case VA_FOURCC_Y410:
|
|
|
|
data->Pitch = mem_id->image.pitches[0];
|
|
|
|
data->U = buf + mem_id->image.offsets[0]; /* data->Y410 */
|
|
|
|
break;
|
2020-03-12 05:12:33 +00:00
|
|
|
case VA_FOURCC_Y416:
|
|
|
|
data->Pitch = mem_id->image.pitches[0];
|
|
|
|
data->U = buf + mem_id->image.offsets[0];
|
|
|
|
data->Y = data->U + 2;
|
|
|
|
data->V = data->U + 4;
|
|
|
|
data->A = data->U + 6;
|
|
|
|
break;
|
2019-05-30 03:10:58 +00:00
|
|
|
#endif
|
2019-10-09 05:41:09 +00:00
|
|
|
case VA_FOURCC_ABGR:
|
|
|
|
data->Pitch = mem_id->image.pitches[0];
|
|
|
|
data->R = buf + mem_id->image.offsets[0];
|
|
|
|
data->G = data->R + 1;
|
|
|
|
data->B = data->R + 2;
|
|
|
|
data->A = data->R + 3;
|
|
|
|
break;
|
2019-05-30 03:10:58 +00:00
|
|
|
|
2018-11-16 06:00:02 +00:00
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
2018-02-13 21:43:42 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
VACodedBufferSegment *coded_buffer_segment;
|
|
|
|
va_status =
|
|
|
|
vaMapBuffer (dpy, *va_surface, (void **) (&coded_buffer_segment));
|
|
|
|
status = gst_msdk_get_mfx_status_from_va_status (va_status);
|
|
|
|
if (MFX_ERR_NONE == status)
|
|
|
|
data->Y = (mfxU8 *) coded_buffer_segment->buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
mfxStatus
|
|
|
|
gst_msdk_frame_unlock (mfxHDL pthis, mfxMemId mid, mfxFrameData * ptr)
|
|
|
|
{
|
|
|
|
GstMsdkContext *context = (GstMsdkContext *) pthis;
|
|
|
|
VAStatus va_status;
|
|
|
|
mfxStatus status;
|
|
|
|
VADisplay dpy;
|
|
|
|
GstMsdkMemoryID *mem_id;
|
|
|
|
|
|
|
|
mem_id = (GstMsdkMemoryID *) mid;
|
|
|
|
dpy = gst_msdk_context_get_handle (context);
|
|
|
|
|
|
|
|
if (mem_id->fourcc != MFX_FOURCC_P8) {
|
|
|
|
vaUnmapBuffer (dpy, mem_id->image.buf);
|
|
|
|
va_status = vaDestroyImage (dpy, mem_id->image.image_id);
|
2019-01-18 07:14:59 +00:00
|
|
|
|
|
|
|
if (va_status == VA_STATUS_SUCCESS) {
|
|
|
|
mem_id->image.image_id = VA_INVALID_ID;
|
|
|
|
mem_id->image.buf = VA_INVALID_ID;
|
|
|
|
}
|
2018-02-13 21:43:42 +00:00
|
|
|
} else {
|
|
|
|
va_status = vaUnmapBuffer (dpy, *(mem_id->surface));
|
|
|
|
}
|
|
|
|
|
|
|
|
status = gst_msdk_get_mfx_status_from_va_status (va_status);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
mfxStatus
|
|
|
|
gst_msdk_frame_get_hdl (mfxHDL pthis, mfxMemId mid, mfxHDL * hdl)
|
|
|
|
{
|
|
|
|
GstMsdkMemoryID *mem_id;
|
|
|
|
|
|
|
|
if (!hdl || !mid)
|
|
|
|
return MFX_ERR_INVALID_HANDLE;
|
|
|
|
|
|
|
|
mem_id = mid;
|
|
|
|
*hdl = mem_id->surface;
|
|
|
|
|
|
|
|
return MFX_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_msdk_set_frame_allocator (GstMsdkContext * context)
|
|
|
|
{
|
|
|
|
mfxFrameAllocator gst_msdk_frame_allocator = {
|
|
|
|
.pthis = context,
|
|
|
|
.Alloc = gst_msdk_frame_alloc,
|
|
|
|
.Lock = gst_msdk_frame_lock,
|
|
|
|
.Unlock = gst_msdk_frame_unlock,
|
|
|
|
.GetHDL = gst_msdk_frame_get_hdl,
|
|
|
|
.Free = gst_msdk_frame_free,
|
|
|
|
};
|
|
|
|
|
2018-12-29 05:56:49 +00:00
|
|
|
gst_msdk_context_set_frame_allocator (context, &gst_msdk_frame_allocator);
|
2018-02-13 21:43:42 +00:00
|
|
|
}
|
2018-03-30 19:05:16 +00:00
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_msdk_get_dmabuf_info_from_surface (mfxFrameSurface1 * surface,
|
|
|
|
gint * handle, gsize * size)
|
|
|
|
{
|
|
|
|
GstMsdkMemoryID *mem_id;
|
|
|
|
g_return_val_if_fail (surface, FALSE);
|
|
|
|
|
|
|
|
mem_id = (GstMsdkMemoryID *) surface->Data.MemId;
|
|
|
|
if (handle)
|
|
|
|
*handle = mem_id->info.handle;
|
|
|
|
if (size)
|
|
|
|
*size = mem_id->info.mem_size;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2018-05-31 00:22:49 +00:00
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_msdk_export_dmabuf_to_vasurface (GstMsdkContext * context,
|
|
|
|
GstVideoInfo * vinfo, gint fd, VASurfaceID * surface_id)
|
|
|
|
{
|
|
|
|
GstVideoFormat format;
|
|
|
|
guint width, height, size, i;
|
|
|
|
unsigned long extbuf_handle;
|
|
|
|
guint va_fourcc = 0, va_chroma = 0;
|
|
|
|
VASurfaceAttrib attribs[2], *attrib;
|
|
|
|
VASurfaceAttribExternalBuffers extbuf;
|
|
|
|
VAStatus va_status;
|
|
|
|
mfxStatus status = MFX_ERR_NONE;
|
|
|
|
|
|
|
|
g_return_val_if_fail (context != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (vinfo != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (fd >= 0, FALSE);
|
|
|
|
|
|
|
|
extbuf_handle = (guintptr) (fd);
|
|
|
|
|
|
|
|
format = GST_VIDEO_INFO_FORMAT (vinfo);
|
|
|
|
width = GST_VIDEO_INFO_WIDTH (vinfo);
|
|
|
|
height = GST_VIDEO_INFO_HEIGHT (vinfo);
|
|
|
|
size = GST_VIDEO_INFO_SIZE (vinfo);
|
|
|
|
|
|
|
|
/* Fixme: Move to common format handling util */
|
|
|
|
switch (format) {
|
|
|
|
case GST_VIDEO_FORMAT_NV12:
|
|
|
|
va_chroma = VA_RT_FORMAT_YUV420;
|
|
|
|
va_fourcc = VA_FOURCC_NV12;
|
|
|
|
break;
|
|
|
|
case GST_VIDEO_FORMAT_BGRA:
|
|
|
|
va_chroma = VA_RT_FORMAT_YUV444;
|
|
|
|
va_fourcc = VA_FOURCC_BGRA;
|
|
|
|
break;
|
|
|
|
case GST_VIDEO_FORMAT_YUY2:
|
|
|
|
va_chroma = VA_RT_FORMAT_YUV422;
|
|
|
|
va_fourcc = VA_FOURCC_YUY2;
|
|
|
|
break;
|
2018-11-16 06:36:31 +00:00
|
|
|
case GST_VIDEO_FORMAT_P010_10LE:
|
2019-08-24 03:18:20 +00:00
|
|
|
#if VA_CHECK_VERSION(1, 2, 0)
|
2018-11-16 06:36:31 +00:00
|
|
|
va_chroma = VA_RT_FORMAT_YUV420_10;
|
2019-08-24 03:18:20 +00:00
|
|
|
#else
|
|
|
|
va_chroma = VA_RT_FORMAT_YUV420_10BPP;
|
|
|
|
#endif
|
2018-11-16 06:36:31 +00:00
|
|
|
va_fourcc = VA_FOURCC_P010;
|
|
|
|
break;
|
2019-01-02 11:48:34 +00:00
|
|
|
case GST_VIDEO_FORMAT_UYVY:
|
|
|
|
va_chroma = VA_RT_FORMAT_YUV422;
|
|
|
|
va_fourcc = VA_FOURCC_UYVY;
|
|
|
|
break;
|
2019-02-28 12:37:49 +00:00
|
|
|
#if (MFX_VERSION >= 1028)
|
2019-02-25 04:29:25 +00:00
|
|
|
case GST_VIDEO_FORMAT_RGB16:
|
|
|
|
va_chroma = VA_RT_FORMAT_RGB16;
|
|
|
|
va_fourcc = VA_FOURCC_RGB565;
|
|
|
|
break;
|
2019-02-28 12:37:49 +00:00
|
|
|
#endif
|
2019-03-08 04:26:37 +00:00
|
|
|
case GST_VIDEO_FORMAT_VUYA:
|
|
|
|
va_chroma = VA_RT_FORMAT_YUV444;
|
|
|
|
va_fourcc = VA_FOURCC_AYUV;
|
|
|
|
break;
|
2019-04-17 01:12:52 +00:00
|
|
|
#if VA_CHECK_VERSION(1, 4, 1)
|
2019-03-21 07:29:15 +00:00
|
|
|
case GST_VIDEO_FORMAT_BGR10A2_LE:
|
|
|
|
va_chroma = VA_RT_FORMAT_RGB32_10;
|
|
|
|
va_fourcc = VA_FOURCC_A2R10G10B10;
|
|
|
|
break;
|
2019-05-30 01:56:43 +00:00
|
|
|
#endif
|
|
|
|
#if VA_CHECK_VERSION(1, 2, 0)
|
|
|
|
case GST_VIDEO_FORMAT_Y210:
|
|
|
|
va_chroma = VA_RT_FORMAT_YUV422_10;
|
|
|
|
va_fourcc = VA_FOURCC_Y210;
|
|
|
|
break;
|
2019-05-31 07:21:05 +00:00
|
|
|
case GST_VIDEO_FORMAT_Y410:
|
|
|
|
va_chroma = VA_RT_FORMAT_YUV444_10;
|
|
|
|
va_fourcc = VA_FOURCC_Y410;
|
|
|
|
break;
|
2020-03-10 08:03:55 +00:00
|
|
|
case GST_VIDEO_FORMAT_P012_LE:
|
|
|
|
va_chroma = VA_RT_FORMAT_YUV420_12;
|
|
|
|
va_fourcc = VA_FOURCC_P016;
|
|
|
|
break;
|
2020-03-11 06:35:55 +00:00
|
|
|
case GST_VIDEO_FORMAT_Y212_LE:
|
|
|
|
va_chroma = VA_RT_FORMAT_YUV422_12;
|
|
|
|
va_fourcc = VA_FOURCC_Y216;
|
|
|
|
break;
|
2020-03-12 05:21:02 +00:00
|
|
|
case GST_VIDEO_FORMAT_Y412_LE:
|
|
|
|
va_chroma = VA_RT_FORMAT_YUV444_12;
|
|
|
|
va_fourcc = VA_FOURCC_Y416;
|
|
|
|
break;
|
2019-04-17 01:12:52 +00:00
|
|
|
#endif
|
2018-05-31 00:22:49 +00:00
|
|
|
default:
|
|
|
|
goto error_unsupported_format;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill the VASurfaceAttribExternalBuffers */
|
|
|
|
extbuf.pixel_format = va_fourcc;
|
|
|
|
extbuf.width = width;
|
|
|
|
extbuf.height = height;
|
|
|
|
extbuf.data_size = size;
|
|
|
|
extbuf.num_planes = GST_VIDEO_INFO_N_PLANES (vinfo);
|
|
|
|
for (i = 0; i < extbuf.num_planes; i++) {
|
|
|
|
extbuf.pitches[i] = GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i);
|
|
|
|
extbuf.offsets[i] = GST_VIDEO_INFO_PLANE_OFFSET (vinfo, i);
|
|
|
|
}
|
|
|
|
extbuf.buffers = (uintptr_t *) & extbuf_handle;
|
|
|
|
extbuf.num_buffers = 1;
|
|
|
|
extbuf.flags = 0;
|
|
|
|
extbuf.private_data = NULL;
|
|
|
|
|
|
|
|
/* Fill the Surface Attributes */
|
|
|
|
attrib = attribs;
|
|
|
|
attrib->type = VASurfaceAttribMemoryType;
|
|
|
|
attrib->flags = VA_SURFACE_ATTRIB_SETTABLE;
|
|
|
|
attrib->value.type = VAGenericValueTypeInteger;
|
|
|
|
attrib->value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
|
|
|
|
attrib++;
|
|
|
|
attrib->type = VASurfaceAttribExternalBufferDescriptor;
|
|
|
|
attrib->flags = VA_SURFACE_ATTRIB_SETTABLE;
|
|
|
|
attrib->value.type = VAGenericValueTypePointer;
|
|
|
|
attrib->value.value.p = &extbuf;
|
|
|
|
attrib++;
|
|
|
|
|
|
|
|
va_status = vaCreateSurfaces (gst_msdk_context_get_handle (context),
|
|
|
|
va_chroma, width, height, surface_id, 1, attribs, attrib - attribs);
|
|
|
|
status = gst_msdk_get_mfx_status_from_va_status (va_status);
|
|
|
|
if (status != MFX_ERR_NONE)
|
|
|
|
goto error_create_surface;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
error_unsupported_format:
|
|
|
|
{
|
|
|
|
GST_ERROR ("Unsupported Video format %s, Can't export dmabuf to vaSurface",
|
|
|
|
gst_video_format_to_string (format));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
error_create_surface:
|
|
|
|
{
|
|
|
|
GST_ERROR ("Failed to create the VASurface from DRM_PRIME FD");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2018-05-31 00:23:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_msdk_replace_mfx_memid:
|
|
|
|
* This method replace the internal VA Suface in mfxSurface with a new one
|
|
|
|
*
|
|
|
|
* Caution: Not a thread-safe routine, this method is here to work around
|
|
|
|
* the dmabuf-import use case with dynamic memID replacement where msdk
|
|
|
|
* originally Inited with fake memIDs.
|
|
|
|
*
|
|
|
|
* Don't use anywhere else unless you really know what you are doing!
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gst_msdk_replace_mfx_memid (GstMsdkContext * context,
|
|
|
|
mfxFrameSurface1 * mfx_surface, VASurfaceID surface_id)
|
|
|
|
{
|
|
|
|
GstMsdkMemoryID *msdk_mid = NULL;
|
|
|
|
VADisplay dpy;
|
|
|
|
VASurfaceID *old_surface_id;
|
|
|
|
VAStatus va_status;
|
|
|
|
mfxStatus status = MFX_ERR_NONE;
|
|
|
|
|
|
|
|
g_return_val_if_fail (mfx_surface != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (context != NULL, FALSE);
|
|
|
|
|
|
|
|
msdk_mid = (GstMsdkMemoryID *) mfx_surface->Data.MemId;
|
|
|
|
dpy = gst_msdk_context_get_handle (context);
|
|
|
|
|
2019-09-02 19:08:44 +00:00
|
|
|
/* Destroy the underlined VAImage if already mapped */
|
2018-05-31 00:23:44 +00:00
|
|
|
if (msdk_mid->image.image_id != VA_INVALID_ID
|
|
|
|
&& msdk_mid->image.buf != VA_INVALID_ID) {
|
|
|
|
status =
|
|
|
|
gst_msdk_frame_unlock ((mfxHDL) context, (mfxMemId) msdk_mid, NULL);
|
|
|
|
if (status != MFX_ERR_NONE)
|
|
|
|
goto error_destroy_va_image;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Destroy the associated VASurface */
|
|
|
|
old_surface_id = msdk_mid->surface;
|
|
|
|
if (*old_surface_id != VA_INVALID_ID) {
|
|
|
|
va_status = vaDestroySurfaces (dpy, old_surface_id, 1);
|
|
|
|
status = gst_msdk_get_mfx_status_from_va_status (va_status);
|
|
|
|
if (status != MFX_ERR_NONE)
|
|
|
|
goto error_destroy_va_surface;
|
|
|
|
}
|
|
|
|
|
|
|
|
*msdk_mid->surface = surface_id;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
error_destroy_va_image:
|
|
|
|
{
|
|
|
|
GST_ERROR ("Failed to Destroy the VAImage");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
error_destroy_va_surface:
|
|
|
|
{
|
|
|
|
GST_ERROR ("Failed to Destroy the VASurfaceID %x", *old_surface_id);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|