2013-09-20 13:55:13 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
vaapi: change license to LGPL
Originally mpv vaapi support was based on the MPlayer-vaapi patches.
These were never merged in upstream MPlayer. The license headers
indicated they were GPL-only. Although the actual author agreed to
relicensing, the company employing him to write this code did not, so
the original code is unusable to us.
Fortunately, vaapi support was refactored and rewritten several times,
meaning little code is actually left. The previous commits removed or
moved that to GPL-only code. Namely, vo_vaapi.c remains GPL-only. The
other code went away or became unnecessary mainly because libavcodec
itself gained the ability to manage the hw decoder, and libavutil
provides code to manage vaapi surfaces. We also changed to mainly using
EGL interop, making any of the old rendering code unnecessary.
hwdec_vaglx.c is still GPL. It's possibly relicensable, because much of
it was changed, but I'm not too sure and further investigation would be
required. Also, this has been disabled by default for a while now, so
bothering with this is a waste of time. This commit simply disables it
at compile time as well in LGPL mode.
2017-09-29 16:44:47 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2013-09-20 13:55:13 +00:00
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
vaapi: change license to LGPL
Originally mpv vaapi support was based on the MPlayer-vaapi patches.
These were never merged in upstream MPlayer. The license headers
indicated they were GPL-only. Although the actual author agreed to
relicensing, the company employing him to write this code did not, so
the original code is unusable to us.
Fortunately, vaapi support was refactored and rewritten several times,
meaning little code is actually left. The previous commits removed or
moved that to GPL-only code. Namely, vo_vaapi.c remains GPL-only. The
other code went away or became unnecessary mainly because libavcodec
itself gained the ability to manage the hw decoder, and libavutil
provides code to manage vaapi surfaces. We also changed to mainly using
EGL interop, making any of the old rendering code unnecessary.
hwdec_vaglx.c is still GPL. It's possibly relicensable, because much of
it was changed, but I'm not too sure and further investigation would be
required. Also, this has been disabled by default for a while now, so
bothering with this is a waste of time. This commit simply disables it
at compile time as well in LGPL mode.
2017-09-29 16:44:47 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2013-09-20 13:55:13 +00:00
|
|
|
*
|
vaapi: change license to LGPL
Originally mpv vaapi support was based on the MPlayer-vaapi patches.
These were never merged in upstream MPlayer. The license headers
indicated they were GPL-only. Although the actual author agreed to
relicensing, the company employing him to write this code did not, so
the original code is unusable to us.
Fortunately, vaapi support was refactored and rewritten several times,
meaning little code is actually left. The previous commits removed or
moved that to GPL-only code. Namely, vo_vaapi.c remains GPL-only. The
other code went away or became unnecessary mainly because libavcodec
itself gained the ability to manage the hw decoder, and libavutil
provides code to manage vaapi surfaces. We also changed to mainly using
EGL interop, making any of the old rendering code unnecessary.
hwdec_vaglx.c is still GPL. It's possibly relicensable, because much of
it was changed, but I'm not too sure and further investigation would be
required. Also, this has been disabled by default for a while now, so
bothering with this is a waste of time. This commit simply disables it
at compile time as well in LGPL mode.
2017-09-29 16:44:47 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2013-09-20 13:55:13 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
2017-01-11 17:45:04 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2013-09-20 13:55:13 +00:00
|
|
|
#include "vaapi.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/common.h"
|
2023-11-07 23:03:51 +00:00
|
|
|
#include "common/global.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2014-08-21 20:04:25 +00:00
|
|
|
#include "osdep/threads.h"
|
2013-09-20 13:55:13 +00:00
|
|
|
#include "mp_image.h"
|
|
|
|
#include "img_format.h"
|
2013-09-27 14:48:45 +00:00
|
|
|
#include "mp_image_pool.h"
|
2018-03-29 21:23:27 +00:00
|
|
|
#include "options/m_config.h"
|
2013-09-20 13:55:13 +00:00
|
|
|
|
2023-11-07 23:05:40 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include "osdep/windows_utils.h"
|
|
|
|
#include "out/d3d11/context.h"
|
|
|
|
#include "out/gpu/d3d11_helpers.h"
|
|
|
|
#endif
|
|
|
|
|
2017-01-11 15:09:15 +00:00
|
|
|
#include <libavutil/hwcontext.h>
|
|
|
|
#include <libavutil/hwcontext_vaapi.h>
|
|
|
|
|
2023-11-07 23:05:40 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define DEV_PATH_DEFAULT NULL
|
|
|
|
#define DEV_PATH_VALIDATE mp_dxgi_validate_adapter
|
|
|
|
#else
|
|
|
|
#define DEV_PATH_DEFAULT "/dev/dri/renderD128"
|
|
|
|
#define DEV_PATH_VALIDATE validate_path
|
|
|
|
|
2024-02-26 03:55:00 +00:00
|
|
|
static inline OPT_STRING_VALIDATE_FUNC(validate_path)
|
2023-11-07 23:05:40 +00:00
|
|
|
{
|
|
|
|
return (*value && **value) ? 0 : M_OPT_INVALID;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-03-29 21:23:27 +00:00
|
|
|
struct vaapi_opts {
|
|
|
|
char *path;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define OPT_BASE_STRUCT struct vaapi_opts
|
|
|
|
const struct m_sub_options vaapi_conf = {
|
|
|
|
.opts = (const struct m_option[]) {
|
2023-11-07 23:05:40 +00:00
|
|
|
{"device", OPT_STRING_VALIDATE(path, DEV_PATH_VALIDATE)},
|
2018-03-29 21:23:27 +00:00
|
|
|
{0},
|
|
|
|
},
|
|
|
|
.defaults = &(const struct vaapi_opts) {
|
2023-11-07 23:05:40 +00:00
|
|
|
.path = DEV_PATH_DEFAULT,
|
2018-03-29 21:23:27 +00:00
|
|
|
},
|
|
|
|
.size = sizeof(struct vaapi_opts),
|
|
|
|
};
|
|
|
|
|
2023-11-04 02:55:38 +00:00
|
|
|
int va_get_colorspace_flag(enum pl_color_system csp)
|
2013-09-20 13:55:13 +00:00
|
|
|
{
|
|
|
|
switch (csp) {
|
2023-11-04 02:55:38 +00:00
|
|
|
case PL_COLOR_SYSTEM_BT_601: return VA_SRC_BT601;
|
|
|
|
case PL_COLOR_SYSTEM_BT_709: return VA_SRC_BT709;
|
|
|
|
case PL_COLOR_SYSTEM_SMPTE_240M: return VA_SRC_SMPTE_240;
|
2013-09-20 13:55:13 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-09 19:10:26 +00:00
|
|
|
static void va_message_callback(void *context, const char *msg, int mp_level)
|
|
|
|
{
|
|
|
|
struct mp_vaapi_ctx *res = context;
|
|
|
|
mp_msg(res->log, mp_level, "libva: %s", msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void va_error_callback(void *context, const char *msg)
|
|
|
|
{
|
|
|
|
va_message_callback(context, msg, MSGL_ERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void va_info_callback(void *context, const char *msg)
|
|
|
|
{
|
2023-11-23 23:20:45 +00:00
|
|
|
va_message_callback(context, msg, MSGL_V);
|
2017-10-09 19:10:26 +00:00
|
|
|
}
|
2017-01-08 06:30:20 +00:00
|
|
|
|
2017-12-01 07:01:08 +00:00
|
|
|
static void free_device_ref(struct AVHWDeviceContext *hwctx)
|
2017-01-13 12:35:31 +00:00
|
|
|
{
|
2017-12-01 07:01:08 +00:00
|
|
|
struct mp_vaapi_ctx *ctx = hwctx->user_opaque;
|
2017-01-13 12:35:31 +00:00
|
|
|
|
2017-12-01 07:01:08 +00:00
|
|
|
if (ctx->display)
|
|
|
|
vaTerminate(ctx->display);
|
2017-01-13 12:35:31 +00:00
|
|
|
|
2017-12-01 07:01:08 +00:00
|
|
|
if (ctx->destroy_native_ctx)
|
|
|
|
ctx->destroy_native_ctx(ctx->native_ctx);
|
2017-01-13 12:35:31 +00:00
|
|
|
|
2017-12-01 07:01:08 +00:00
|
|
|
talloc_free(ctx);
|
2017-01-13 12:35:31 +00:00
|
|
|
}
|
|
|
|
|
2015-06-20 20:26:57 +00:00
|
|
|
struct mp_vaapi_ctx *va_initialize(VADisplay *display, struct mp_log *plog,
|
|
|
|
bool probing)
|
2013-09-20 13:55:13 +00:00
|
|
|
{
|
2017-12-01 07:01:08 +00:00
|
|
|
AVBufferRef *avref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
|
|
|
|
if (!avref)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
AVHWDeviceContext *hwctx = (void *)avref->data;
|
|
|
|
AVVAAPIDeviceContext *vactx = hwctx->hwctx;
|
|
|
|
|
2017-01-08 05:57:48 +00:00
|
|
|
struct mp_vaapi_ctx *res = talloc_ptrtype(NULL, res);
|
2013-09-20 13:55:13 +00:00
|
|
|
*res = (struct mp_vaapi_ctx) {
|
2017-01-08 05:57:48 +00:00
|
|
|
.log = mp_log_new(res, plog, "/vaapi"),
|
2013-09-20 13:55:13 +00:00
|
|
|
.display = display,
|
2017-12-01 07:01:08 +00:00
|
|
|
.av_device_ref = avref,
|
2015-01-22 14:32:23 +00:00
|
|
|
.hwctx = {
|
2017-12-01 07:01:08 +00:00
|
|
|
.av_device_ref = avref,
|
2015-01-22 14:32:23 +00:00
|
|
|
},
|
2013-09-20 13:55:13 +00:00
|
|
|
};
|
|
|
|
|
2017-12-01 07:01:08 +00:00
|
|
|
hwctx->free = free_device_ref;
|
|
|
|
hwctx->user_opaque = res;
|
|
|
|
|
2017-10-09 19:10:26 +00:00
|
|
|
vaSetErrorCallback(display, va_error_callback, res);
|
|
|
|
vaSetInfoCallback(display, va_info_callback, res);
|
2017-01-08 06:30:20 +00:00
|
|
|
|
2017-09-29 14:51:30 +00:00
|
|
|
int major, minor;
|
|
|
|
int status = vaInitialize(display, &major, &minor);
|
|
|
|
if (status != VA_STATUS_SUCCESS) {
|
|
|
|
if (!probing)
|
|
|
|
MP_ERR(res, "Failed to initialize VAAPI: %s\n", vaErrorStr(status));
|
2017-01-08 05:57:48 +00:00
|
|
|
goto error;
|
2017-09-29 14:51:30 +00:00
|
|
|
}
|
|
|
|
MP_VERBOSE(res, "Initialized VAAPI: version %d.%d\n", major, minor);
|
2017-01-08 05:57:48 +00:00
|
|
|
|
2017-12-01 07:01:08 +00:00
|
|
|
vactx->display = res->display;
|
|
|
|
|
|
|
|
if (av_hwdevice_ctx_init(res->av_device_ref) < 0)
|
|
|
|
goto error;
|
2017-01-13 12:35:31 +00:00
|
|
|
|
2013-09-20 13:55:13 +00:00
|
|
|
return res;
|
|
|
|
|
|
|
|
error:
|
2017-01-08 05:57:48 +00:00
|
|
|
res->display = NULL; // do not vaTerminate this
|
|
|
|
va_destroy(res);
|
2013-09-20 13:55:13 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Undo va_initialize, and close the VADisplay.
|
|
|
|
void va_destroy(struct mp_vaapi_ctx *ctx)
|
|
|
|
{
|
2017-12-01 07:01:08 +00:00
|
|
|
if (!ctx)
|
|
|
|
return;
|
2017-01-08 06:30:20 +00:00
|
|
|
|
2017-12-01 07:01:08 +00:00
|
|
|
AVBufferRef *ref = ctx->av_device_ref;
|
|
|
|
av_buffer_unref(&ref); // frees ctx as well
|
2013-09-20 13:55:13 +00:00
|
|
|
}
|
|
|
|
|
2014-03-17 17:22:35 +00:00
|
|
|
VASurfaceID va_surface_id(struct mp_image *mpi)
|
|
|
|
{
|
|
|
|
return mpi && mpi->imgfmt == IMGFMT_VAAPI ?
|
|
|
|
(VASurfaceID)(uintptr_t)mpi->planes[3] : VA_INVALID_ID;
|
|
|
|
}
|
|
|
|
|
2017-12-02 03:27:02 +00:00
|
|
|
static bool is_emulated(struct AVBufferRef *hw_device_ctx)
|
2014-05-27 23:37:53 +00:00
|
|
|
{
|
2017-12-02 03:27:02 +00:00
|
|
|
AVHWDeviceContext *hwctx = (void *)hw_device_ctx->data;
|
|
|
|
AVVAAPIDeviceContext *vactx = hwctx->hwctx;
|
|
|
|
|
|
|
|
const char *s = vaQueryVendorString(vactx->display);
|
2014-05-27 23:37:53 +00:00
|
|
|
return s && strstr(s, "VDPAU backend");
|
|
|
|
}
|
2017-01-11 15:07:53 +00:00
|
|
|
|
2017-12-02 03:27:02 +00:00
|
|
|
|
|
|
|
bool va_guess_if_emulated(struct mp_vaapi_ctx *ctx)
|
|
|
|
{
|
|
|
|
return is_emulated(ctx->av_device_ref);
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:07:53 +00:00
|
|
|
struct va_native_display {
|
2023-11-07 23:03:51 +00:00
|
|
|
void (*create)(struct mp_log *log, VADisplay **out_display,
|
|
|
|
void **out_native_ctx, const char *path);
|
2017-01-11 15:07:53 +00:00
|
|
|
void (*destroy)(void *native_ctx);
|
|
|
|
};
|
|
|
|
|
|
|
|
#if HAVE_VAAPI_X11
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <va/va_x11.h>
|
|
|
|
|
|
|
|
static void x11_destroy(void *native_ctx)
|
|
|
|
{
|
|
|
|
XCloseDisplay(native_ctx);
|
|
|
|
}
|
|
|
|
|
2023-11-07 23:03:51 +00:00
|
|
|
static void x11_create(struct mp_log *log, VADisplay **out_display,
|
|
|
|
void **out_native_ctx, const char *path)
|
2017-01-11 15:07:53 +00:00
|
|
|
{
|
|
|
|
void *native_display = XOpenDisplay(NULL);
|
|
|
|
if (!native_display)
|
|
|
|
return;
|
|
|
|
*out_display = vaGetDisplay(native_display);
|
|
|
|
if (*out_display) {
|
|
|
|
*out_native_ctx = native_display;
|
|
|
|
} else {
|
|
|
|
XCloseDisplay(native_display);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct va_native_display disp_x11 = {
|
|
|
|
.create = x11_create,
|
|
|
|
.destroy = x11_destroy,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2023-11-07 23:05:40 +00:00
|
|
|
#if HAVE_VAAPI_WIN32
|
|
|
|
#include <va/va_win32.h>
|
|
|
|
|
|
|
|
static void win32_create(struct mp_log *log, VADisplay **out_display,
|
|
|
|
void **out_native_ctx, const char *path)
|
|
|
|
{
|
|
|
|
LUID *luid = NULL;
|
|
|
|
DXGI_ADAPTER_DESC1 desc = {0};
|
|
|
|
if (path && path[0]) {
|
|
|
|
IDXGIAdapter1 *adapter = mp_get_dxgi_adapter(log, bstr0(path), NULL);
|
|
|
|
if (!adapter || FAILED(IDXGIAdapter1_GetDesc1(adapter, &desc))) {
|
|
|
|
mp_err(log, "Failed to get adapter LUID for name: %s\n", path);
|
|
|
|
} else {
|
|
|
|
luid = &desc.AdapterLuid;
|
|
|
|
}
|
|
|
|
SAFE_RELEASE(adapter);
|
|
|
|
}
|
|
|
|
*out_display = vaGetDisplayWin32(luid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct va_native_display disp_win32 = {
|
|
|
|
.create = win32_create,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2017-01-11 15:07:53 +00:00
|
|
|
#if HAVE_VAAPI_DRM
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <va/va_drm.h>
|
|
|
|
|
|
|
|
struct va_native_display_drm {
|
|
|
|
int drm_fd;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void drm_destroy(void *native_ctx)
|
|
|
|
{
|
|
|
|
struct va_native_display_drm *ctx = native_ctx;
|
|
|
|
close(ctx->drm_fd);
|
|
|
|
talloc_free(ctx);
|
|
|
|
}
|
|
|
|
|
2023-11-07 23:03:51 +00:00
|
|
|
static void drm_create(struct mp_log *log, VADisplay **out_display,
|
|
|
|
void **out_native_ctx, const char *path)
|
2017-01-11 15:07:53 +00:00
|
|
|
{
|
2018-03-29 21:23:27 +00:00
|
|
|
int drm_fd = open(path, O_RDWR);
|
|
|
|
if (drm_fd < 0)
|
|
|
|
return;
|
2017-01-11 15:07:53 +00:00
|
|
|
|
2018-03-29 21:23:27 +00:00
|
|
|
struct va_native_display_drm *ctx = talloc_ptrtype(NULL, ctx);
|
|
|
|
ctx->drm_fd = drm_fd;
|
|
|
|
*out_display = vaGetDisplayDRM(drm_fd);
|
2020-06-03 13:12:08 +00:00
|
|
|
if (*out_display) {
|
2018-03-29 21:23:27 +00:00
|
|
|
*out_native_ctx = ctx;
|
|
|
|
return;
|
2017-01-11 15:07:53 +00:00
|
|
|
}
|
2018-03-29 21:23:27 +00:00
|
|
|
|
|
|
|
close(drm_fd);
|
|
|
|
talloc_free(ctx);
|
2017-01-11 15:07:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct va_native_display disp_drm = {
|
|
|
|
.create = drm_create,
|
|
|
|
.destroy = drm_destroy,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const struct va_native_display *const native_displays[] = {
|
|
|
|
#if HAVE_VAAPI_DRM
|
|
|
|
&disp_drm,
|
|
|
|
#endif
|
2023-11-07 23:05:40 +00:00
|
|
|
#if HAVE_VAAPI_WIN32
|
|
|
|
&disp_win32,
|
|
|
|
#endif
|
2017-01-11 15:07:53 +00:00
|
|
|
#if HAVE_VAAPI_X11
|
|
|
|
&disp_x11,
|
|
|
|
#endif
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2017-12-01 07:01:08 +00:00
|
|
|
static struct AVBufferRef *va_create_standalone(struct mpv_global *global,
|
|
|
|
struct mp_log *log, struct hwcontext_create_dev_params *params)
|
2017-01-11 15:07:53 +00:00
|
|
|
{
|
2018-03-29 21:23:27 +00:00
|
|
|
struct AVBufferRef *ret = NULL;
|
|
|
|
struct vaapi_opts *opts = mp_get_config_group(NULL, global, &vaapi_conf);
|
|
|
|
|
2017-01-11 15:07:53 +00:00
|
|
|
for (int n = 0; native_displays[n]; n++) {
|
|
|
|
VADisplay *display = NULL;
|
|
|
|
void *native_ctx = NULL;
|
2023-11-07 23:03:51 +00:00
|
|
|
native_displays[n]->create(global->log, &display, &native_ctx, opts->path);
|
2017-01-11 15:07:53 +00:00
|
|
|
if (display) {
|
2017-12-01 07:01:08 +00:00
|
|
|
struct mp_vaapi_ctx *ctx =
|
|
|
|
va_initialize(display, log, params->probing);
|
2017-01-11 15:07:53 +00:00
|
|
|
if (!ctx) {
|
|
|
|
vaTerminate(display);
|
2023-11-23 23:08:47 +00:00
|
|
|
if (native_displays[n]->destroy)
|
|
|
|
native_displays[n]->destroy(native_ctx);
|
2018-03-29 21:23:27 +00:00
|
|
|
goto end;
|
2017-01-11 15:07:53 +00:00
|
|
|
}
|
|
|
|
ctx->native_ctx = native_ctx;
|
|
|
|
ctx->destroy_native_ctx = native_displays[n]->destroy;
|
2018-03-29 21:23:27 +00:00
|
|
|
ret = ctx->hwctx.av_device_ref;
|
|
|
|
goto end;
|
2017-01-11 15:07:53 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-29 21:23:27 +00:00
|
|
|
|
|
|
|
end:
|
|
|
|
talloc_free(opts);
|
|
|
|
return ret;
|
2017-01-11 15:07:53 +00:00
|
|
|
}
|
2017-12-01 07:01:08 +00:00
|
|
|
|
|
|
|
const struct hwcontext_fns hwcontext_fns_vaapi = {
|
|
|
|
.av_hwdevice_type = AV_HWDEVICE_TYPE_VAAPI,
|
|
|
|
.create_dev = va_create_standalone,
|
2017-12-02 03:27:02 +00:00
|
|
|
.is_emulated = is_emulated,
|
2017-12-01 07:01:08 +00:00
|
|
|
};
|