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.
33 #include <gst/d3d11/gstd3d11.h>
34 #include "gstmsdkallocator.h"
36 #define GST_MSDK_FRAME_SURFACE gst_msdk_frame_surface_quark_get ()
38 gst_msdk_frame_surface_quark_get (void)
42 if (g_once_init_enter (&g_quark)) {
43 gsize quark = (gsize) g_quark_from_static_string ("GstMsdkFrameSurface");
44 g_once_init_leave (&g_quark, quark);
50 gst_msdk_frame_alloc (mfxHDL pthis, mfxFrameAllocRequest * req,
51 mfxFrameAllocResponse * resp)
57 gst_msdk_frame_free (mfxHDL pthis, mfxFrameAllocResponse * resp)
63 gst_msdk_frame_lock (mfxHDL pthis, mfxMemId mid, mfxFrameData * data)
69 gst_msdk_frame_unlock (mfxHDL pthis, mfxMemId mid, mfxFrameData * ptr)
75 gst_msdk_frame_get_hdl (mfxHDL pthis, mfxMemId mid, mfxHDL * hdl)
77 GstMsdkMemoryID *mem_id;
81 return MFX_ERR_INVALID_HANDLE;
83 mem_id = (GstMsdkMemoryID *) mid;
84 pair = (mfxHDLPair *) hdl;
85 pair->first = (mfxHDL) mem_id->texture;
86 pair->second = (mfxHDL) GUINT_TO_POINTER (mem_id->subresource_index);
92 gst_msdk_import_to_msdk_surface (GstBuffer * buf, GstMsdkContext * msdk_context,
93 GstVideoInfo * vinfo, guint map_flag)
95 GstMemory *mem = NULL;
96 mfxFrameInfo frame_info = { 0, };
97 GstMsdkSurface *msdk_surface = NULL;
98 mfxFrameSurface1 *mfx_surface = NULL;
99 GstMsdkMemoryID *msdk_mid = NULL;
102 mem = gst_buffer_peek_memory (buf, 0);
103 msdk_surface = g_slice_new0 (GstMsdkSurface);
105 if (!gst_is_d3d11_memory (mem) || gst_buffer_n_memory (buf) > 1) {
106 /* d3d11 buffer should hold single memory object */
107 g_slice_free (GstMsdkSurface, msdk_surface);
111 if (!gst_buffer_map (buf, &map_info, map_flag | GST_MAP_D3D11)) {
112 GST_ERROR ("Failed to map buffer");
113 g_slice_free (GstMsdkSurface, msdk_surface);
117 /* If buffer has qdata pointing to mfxFrameSurface1, directly extract it */
118 if ((mfx_surface = gst_mini_object_get_qdata (GST_MINI_OBJECT_CAST (mem),
119 GST_MSDK_FRAME_SURFACE))) {
120 msdk_surface->from_qdata = TRUE;
121 msdk_surface->surface = mfx_surface;
122 gst_buffer_unmap (buf, &map_info);
126 mfx_surface = g_slice_new0 (mfxFrameSurface1);
127 msdk_mid = g_slice_new0 (GstMsdkMemoryID);
128 mfx_surface->Data.MemId = (mfxMemId) msdk_mid;
130 msdk_mid->texture = (ID3D11Texture2D *) (gpointer) map_info.data;
131 msdk_mid->subresource_index = GPOINTER_TO_UINT (map_info.user_data[0]);
133 gst_buffer_unmap (buf, &map_info);
135 gst_msdk_set_mfx_frame_info_from_video_info (&frame_info, vinfo);
136 mfx_surface->Info = frame_info;
138 /* Set mfxFrameSurface1 as qdata in buffer */
139 gst_mini_object_set_qdata (GST_MINI_OBJECT_CAST (mem),
140 GST_MSDK_FRAME_SURFACE, mfx_surface, NULL);
142 msdk_surface->surface = mfx_surface;
148 gst_msdk_set_frame_allocator (GstMsdkContext * context)
150 mfxFrameAllocator gst_msdk_frame_allocator = {
152 .Alloc = gst_msdk_frame_alloc,
153 .Lock = gst_msdk_frame_lock,
154 .Unlock = gst_msdk_frame_unlock,
155 .GetHDL = gst_msdk_frame_get_hdl,
156 .Free = gst_msdk_frame_free,
159 gst_msdk_context_set_frame_allocator (context, &gst_msdk_frame_allocator);