2 * Copyright (C) 2021 Seungha Yang <seungha@centricular.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
26 #include "gstqsvutils.h"
27 #include "gstqsvh264dec.h"
28 #include "gstqsvh264enc.h"
29 #include "gstqsvh265enc.h"
30 #include "gstqsvjpegenc.h"
31 #include "gstqsvvp9enc.h"
35 #define WIN32_LEAN_AND_MEAN
37 #include <versionhelpers.h>
38 #include <gst/d3d11/gstd3d11.h>
40 #include <gst/va/gstva.h>
43 GST_DEBUG_CATEGORY (gst_qsv_debug);
44 GST_DEBUG_CATEGORY (gst_qsv_allocator_debug);
46 #define GST_CAT_DEFAULT gst_qsv_debug
49 #define MFX_ACCEL_MODE MFX_ACCEL_MODE_VIA_D3D11
51 #define MFX_ACCEL_MODE MFX_ACCEL_MODE_VIA_VAAPI
56 create_session_with_platform_device (mfxLoader loader,
57 mfxImplDescription * desc, guint impl_index, GstObject ** d3d11_device,
60 mfxSession session = nullptr;
62 GstD3D11Device *selected = nullptr;
63 GList *list = *devices;
67 *d3d11_device = nullptr;
69 status = MFXCreateSession (loader, impl_index, &session);
70 if (status != MFX_ERR_NONE) {
71 GST_WARNING ("Failed to create session with index %d, %d (%s)",
72 impl_index, QSV_STATUS_ARGS (status));
76 if (desc->ApiVersion.Major >= 2 ||
77 (desc->ApiVersion.Major == 1 && desc->ApiVersion.Minor >= 19)) {
80 memset (&platform, 0, sizeof (mfxPlatform));
82 if (MFXVideoCORE_QueryPlatform (session, &platform) == MFX_ERR_NONE) {
83 device_id = platform.DeviceId;
85 /* XXX: re-create session, MFXVideoCORE_QueryPlatform() may cause
86 * later MFXVideoCORE_SetHandle() call failed with
87 * MFX_ERR_UNDEFINED_BEHAVIOR error */
88 g_clear_pointer (&session, MFXClose);
90 status = MFXCreateSession (loader, impl_index, &session);
91 if (status != MFX_ERR_NONE) {
92 GST_WARNING ("Failed to re-create session with index %d, %d (%s)",
93 impl_index, QSV_STATUS_ARGS (status));
100 for (iter = list; iter; iter = g_list_next (iter)) {
101 GstD3D11Device *dev = GST_D3D11_DEVICE (iter->data);
104 g_object_get (dev, "device-id", &dev_id, nullptr);
105 if (dev_id == (guint) device_id) {
107 list = g_list_delete_link (list, iter);
114 /* Unknown device id, pick the first device */
115 selected = GST_D3D11_DEVICE (list->data);
116 list = g_list_delete_link (list, list);
121 status = MFXVideoCORE_SetHandle (session, MFX_HANDLE_D3D11_DEVICE,
122 gst_d3d11_device_get_device_handle (selected));
123 if (status != MFX_ERR_NONE) {
124 GST_WARNING ("Failed to set d3d11 device handle, %d (%s)",
125 QSV_STATUS_ARGS (status));
126 gst_object_unref (selected);
132 *d3d11_device = GST_OBJECT (selected);
138 create_session_with_platform_device (mfxLoader loader,
139 mfxImplDescription * desc, guint impl_index, GstObject ** va_display,
142 mfxSession session = nullptr;
144 GstVaDisplay *selected;
145 GList *list = *devices;
147 *va_display = nullptr;
149 status = MFXCreateSession (loader, impl_index, &session);
150 if (status != MFX_ERR_NONE) {
151 GST_WARNING ("Failed to create session with index %d, %d (%s)",
152 impl_index, QSV_STATUS_ARGS (status));
156 /* XXX: what's the relation between implementation index and VA display ?
157 * Pick the first available device for now */
158 selected = GST_VA_DISPLAY (list->data);
159 list = g_list_delete_link (list, list);
162 status = MFXVideoCORE_SetHandle (session, MFX_HANDLE_VA_DISPLAY,
163 gst_va_display_get_va_dpy (selected));
164 if (status != MFX_ERR_NONE) {
165 GST_WARNING ("Failed to set display handle, %d (%s)",
166 QSV_STATUS_ARGS (status));
167 gst_object_unref (selected);
173 *va_display = GST_OBJECT (selected);
180 plugin_deinit (gpointer data)
186 plugin_init (GstPlugin * plugin)
190 GList *platform_devices = nullptr;
193 /* D3D11 Video API is supported since Windows 8.
194 * Do we want to support old OS (Windows 7 for example) with D3D9 ?? */
195 if (!IsWindows8OrGreater ())
199 GST_DEBUG_CATEGORY_INIT (gst_qsv_debug, "qsv", 0, "Intel Quick Sync Video");
200 GST_DEBUG_CATEGORY_INIT (gst_qsv_allocator_debug,
201 "qsvallocator", 0, "qsvallocator");
203 loader = gst_qsv_get_loader ();
207 platform_devices = gst_qsv_get_platform_devices ();
208 if (!platform_devices) {
213 GST_INFO ("Found %d platform devices", g_list_length (platform_devices));
216 mfxStatus status = MFX_ERR_NONE;
217 mfxSession session = nullptr;
218 mfxImplDescription *desc = nullptr;
219 GstObject *device = nullptr;
221 status = MFXEnumImplementations (loader,
222 i, MFX_IMPLCAPS_IMPLDESCSTRUCTURE, (mfxHDL *) & desc);
224 if (status != MFX_ERR_NONE)
227 if ((desc->Impl & MFX_IMPL_TYPE_HARDWARE) == 0)
230 if ((desc->AccelerationMode & MFX_ACCEL_MODE) == 0)
233 session = create_session_with_platform_device (loader, desc, i, &device,
238 gst_qsv_h264_dec_register (plugin, GST_RANK_MARGINAL, i, device, session);
240 gst_qsv_h264_enc_register (plugin, GST_RANK_NONE, i, device, session);
241 gst_qsv_h265_enc_register (plugin, GST_RANK_NONE, i, device, session);
242 gst_qsv_jpeg_enc_register (plugin, GST_RANK_NONE, i, device, session);
243 gst_qsv_vp9_enc_register (plugin, GST_RANK_NONE, i, device, session);
246 MFXDispReleaseImplDescription (loader, desc);
247 g_clear_pointer (&session, MFXClose);
248 gst_clear_object (&device);
251 /* What's the possible maximum number of impl/device ? */
252 } while (i < 16 && platform_devices != nullptr);
254 if (platform_devices)
255 g_list_free_full (platform_devices, (GDestroyNotify) gst_object_unref);
257 g_object_set_data_full (G_OBJECT (plugin), "plugin-qsv-shutdown",
258 (gpointer) "shutdown-data", (GDestroyNotify) plugin_deinit);
263 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
266 "Intel Quick Sync Video plugin",
267 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)