2016-03-18 06:28:29 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-05-17 09:57:28 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
|
2017-09-26 16:12:19 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2016-03-18 06:28:29 +00:00
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
|
2017-09-26 16:12:19 +00:00
|
|
|
#include <libavutil/hwcontext.h>
|
|
|
|
#include <libavutil/hwcontext_d3d11va.h>
|
|
|
|
|
|
|
|
#if HAVE_D3D9_HWACCEL
|
|
|
|
#include <libavutil/hwcontext_dxva2.h>
|
|
|
|
#endif
|
2017-06-08 19:16:11 +00:00
|
|
|
|
2016-03-18 06:28:29 +00:00
|
|
|
#include "lavc.h"
|
|
|
|
#include "common/common.h"
|
|
|
|
#include "common/av_common.h"
|
|
|
|
#include "video/fmt-conversion.h"
|
|
|
|
#include "video/mp_image.h"
|
2016-06-28 18:36:00 +00:00
|
|
|
#include "video/mp_image_pool.h"
|
2016-03-18 06:28:29 +00:00
|
|
|
#include "osdep/windows_utils.h"
|
|
|
|
|
|
|
|
#include "d3d.h"
|
|
|
|
|
2016-05-17 09:57:28 +00:00
|
|
|
HMODULE d3d11_dll, d3d9_dll, dxva2_dll;
|
2017-06-30 10:46:12 +00:00
|
|
|
PFN_D3D11_CREATE_DEVICE d3d11_D3D11CreateDevice;
|
2016-05-17 09:57:28 +00:00
|
|
|
|
|
|
|
static pthread_once_t d3d_load_once = PTHREAD_ONCE_INIT;
|
|
|
|
|
2017-06-30 10:46:12 +00:00
|
|
|
#if !HAVE_UWP
|
2016-05-17 09:57:28 +00:00
|
|
|
static void d3d_do_load(void)
|
|
|
|
{
|
|
|
|
d3d11_dll = LoadLibrary(L"d3d11.dll");
|
|
|
|
d3d9_dll = LoadLibrary(L"d3d9.dll");
|
|
|
|
dxva2_dll = LoadLibrary(L"dxva2.dll");
|
2017-06-30 10:46:12 +00:00
|
|
|
|
|
|
|
if (d3d11_dll) {
|
|
|
|
d3d11_D3D11CreateDevice =
|
|
|
|
(void *)GetProcAddress(d3d11_dll, "D3D11CreateDevice");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static void d3d_do_load(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
d3d11_D3D11CreateDevice = D3D11CreateDevice;
|
2016-05-17 09:57:28 +00:00
|
|
|
}
|
2017-06-30 10:46:12 +00:00
|
|
|
#endif
|
2016-05-17 09:57:28 +00:00
|
|
|
|
|
|
|
void d3d_load_dlls(void)
|
|
|
|
{
|
|
|
|
pthread_once(&d3d_load_once, d3d_do_load);
|
|
|
|
}
|
|
|
|
|
2017-06-08 19:16:11 +00:00
|
|
|
// Test if Direct3D11 can be used by us. Basically, this prevents trying to use
|
|
|
|
// D3D11 on Win7, and then failing somewhere in the process.
|
|
|
|
bool d3d11_check_decoding(ID3D11Device *dev)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
// We assume that NV12 is always supported, if hw decoding is supported at
|
|
|
|
// all.
|
|
|
|
UINT supported = 0;
|
|
|
|
hr = ID3D11Device_CheckFormatSupport(dev, DXGI_FORMAT_NV12, &supported);
|
|
|
|
return !FAILED(hr) && (supported & D3D11_BIND_DECODER);
|
|
|
|
}
|
|
|
|
|
2017-10-27 15:22:32 +00:00
|
|
|
void d3d_hwframes_refine(struct lavc_ctx *ctx, AVBufferRef *hw_frames_ctx)
|
|
|
|
{
|
|
|
|
AVHWFramesContext *fctx = (void *)hw_frames_ctx->data;
|
|
|
|
|
|
|
|
if (fctx->format == AV_PIX_FMT_D3D11) {
|
|
|
|
AVD3D11VAFramesContext *hwctx = fctx->hwctx;
|
|
|
|
|
2017-11-02 14:53:22 +00:00
|
|
|
// According to hwcontex_d3d11va.h, yuv420p means DXGI_FORMAT_420_OPAQUE,
|
|
|
|
// which has no shader support.
|
|
|
|
if (fctx->sw_format != AV_PIX_FMT_YUV420P)
|
|
|
|
hwctx->BindFlags |= D3D11_BIND_SHADER_RESOURCE;
|
2017-10-27 15:22:32 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-08 19:16:11 +00:00
|
|
|
|
|
|
|
AVBufferRef *d3d11_wrap_device_ref(ID3D11Device *device)
|
|
|
|
{
|
|
|
|
AVBufferRef *device_ref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_D3D11VA);
|
|
|
|
if (!device_ref)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
AVHWDeviceContext *ctx = (void *)device_ref->data;
|
|
|
|
AVD3D11VADeviceContext *hwctx = ctx->hwctx;
|
|
|
|
|
|
|
|
ID3D11Device_AddRef(device);
|
|
|
|
hwctx->device = device;
|
|
|
|
|
|
|
|
if (av_hwdevice_ctx_init(device_ref) < 0)
|
|
|
|
av_buffer_unref(&device_ref);
|
|
|
|
|
|
|
|
return device_ref;
|
|
|
|
}
|
video: add mp_image_params.hw_flags and add an example
It seems this will be useful for Rokchip DRM hwcontext integration.
DRM hwcontexts have additional internal structure which can be different
depending on the decoder, and which is not part of the generic hwcontext
API. Rockchip has 1 layer, which EGL interop happens to translate to a
RGB texture, while VAAPI (mapped as DRM hwcontext) will use multiple
layers. Both will use sw_format=nv12, and thus are indistinguishable on
the mp_image_params level. But this is needed to initialize the EGL
mapping and the vo_gpu video renderer correctly.
We hope that the layer count is enough to tell whether EGL will
translate the data to a RGB texture (vs. 2 texture resembling raw nv12
data). For that we introduce MP_IMAGE_HW_FLAG_OPAQUE.
This commit adds the flag, infrastructure to set it, and an "example"
for D3D11.
The D3D11 addition is quite useless at this point. But later we want to
get rid of d3d11_update_image_attribs() anyway, while we still need a
way to force d3d11vpp filter insertion, so maybe it has some
justification (who knows). In any case it makes testing this easier.
Obviously it also adds some basic support for triggering the opaque
format for decoding, which will use a driver-specific format, but which
is not supported in shaders. The opaque flag is not used to determine
whether d3d11vpp needs to be inserted, though.
2017-10-16 12:44:59 +00:00
|
|
|
|
2017-10-16 14:56:24 +00:00
|
|
|
static void d3d11_complete_image_params(struct mp_image *img)
|
video: add mp_image_params.hw_flags and add an example
It seems this will be useful for Rokchip DRM hwcontext integration.
DRM hwcontexts have additional internal structure which can be different
depending on the decoder, and which is not part of the generic hwcontext
API. Rockchip has 1 layer, which EGL interop happens to translate to a
RGB texture, while VAAPI (mapped as DRM hwcontext) will use multiple
layers. Both will use sw_format=nv12, and thus are indistinguishable on
the mp_image_params level. But this is needed to initialize the EGL
mapping and the vo_gpu video renderer correctly.
We hope that the layer count is enough to tell whether EGL will
translate the data to a RGB texture (vs. 2 texture resembling raw nv12
data). For that we introduce MP_IMAGE_HW_FLAG_OPAQUE.
This commit adds the flag, infrastructure to set it, and an "example"
for D3D11.
The D3D11 addition is quite useless at this point. But later we want to
get rid of d3d11_update_image_attribs() anyway, while we still need a
way to force d3d11vpp filter insertion, so maybe it has some
justification (who knows). In any case it makes testing this easier.
Obviously it also adds some basic support for triggering the opaque
format for decoding, which will use a driver-specific format, but which
is not supported in shaders. The opaque flag is not used to determine
whether d3d11vpp needs to be inserted, though.
2017-10-16 12:44:59 +00:00
|
|
|
{
|
2017-10-16 14:56:24 +00:00
|
|
|
AVHWFramesContext *hw_frames = (void *)img->hwctx->data;
|
|
|
|
|
video: add mp_image_params.hw_flags and add an example
It seems this will be useful for Rokchip DRM hwcontext integration.
DRM hwcontexts have additional internal structure which can be different
depending on the decoder, and which is not part of the generic hwcontext
API. Rockchip has 1 layer, which EGL interop happens to translate to a
RGB texture, while VAAPI (mapped as DRM hwcontext) will use multiple
layers. Both will use sw_format=nv12, and thus are indistinguishable on
the mp_image_params level. But this is needed to initialize the EGL
mapping and the vo_gpu video renderer correctly.
We hope that the layer count is enough to tell whether EGL will
translate the data to a RGB texture (vs. 2 texture resembling raw nv12
data). For that we introduce MP_IMAGE_HW_FLAG_OPAQUE.
This commit adds the flag, infrastructure to set it, and an "example"
for D3D11.
The D3D11 addition is quite useless at this point. But later we want to
get rid of d3d11_update_image_attribs() anyway, while we still need a
way to force d3d11vpp filter insertion, so maybe it has some
justification (who knows). In any case it makes testing this easier.
Obviously it also adds some basic support for triggering the opaque
format for decoding, which will use a driver-specific format, but which
is not supported in shaders. The opaque flag is not used to determine
whether d3d11vpp needs to be inserted, though.
2017-10-16 12:44:59 +00:00
|
|
|
// According to hwcontex_d3d11va.h, this means DXGI_FORMAT_420_OPAQUE.
|
2017-10-16 14:56:24 +00:00
|
|
|
img->params.hw_flags = hw_frames->sw_format == AV_PIX_FMT_YUV420P
|
|
|
|
? MP_IMAGE_HW_FLAG_OPAQUE : 0;
|
video: add mp_image_params.hw_flags and add an example
It seems this will be useful for Rokchip DRM hwcontext integration.
DRM hwcontexts have additional internal structure which can be different
depending on the decoder, and which is not part of the generic hwcontext
API. Rockchip has 1 layer, which EGL interop happens to translate to a
RGB texture, while VAAPI (mapped as DRM hwcontext) will use multiple
layers. Both will use sw_format=nv12, and thus are indistinguishable on
the mp_image_params level. But this is needed to initialize the EGL
mapping and the vo_gpu video renderer correctly.
We hope that the layer count is enough to tell whether EGL will
translate the data to a RGB texture (vs. 2 texture resembling raw nv12
data). For that we introduce MP_IMAGE_HW_FLAG_OPAQUE.
This commit adds the flag, infrastructure to set it, and an "example"
for D3D11.
The D3D11 addition is quite useless at this point. But later we want to
get rid of d3d11_update_image_attribs() anyway, while we still need a
way to force d3d11vpp filter insertion, so maybe it has some
justification (who knows). In any case it makes testing this easier.
Obviously it also adds some basic support for triggering the opaque
format for decoding, which will use a driver-specific format, but which
is not supported in shaders. The opaque flag is not used to determine
whether d3d11vpp needs to be inserted, though.
2017-10-16 12:44:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct hwcontext_fns hwcontext_fns_d3d11 = {
|
|
|
|
.av_hwdevice_type = AV_HWDEVICE_TYPE_D3D11VA,
|
|
|
|
.complete_image_params = d3d11_complete_image_params,
|
|
|
|
};
|