| 1 |
/* |
| 2 |
* Copyright (C) 2005-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
// setting that here because otherwise SampleFormat is defined to AVSampleFormat |
| 10 |
// which we don't use here |
| 11 |
#define FF_API_OLD_SAMPLE_FMT 0 |
| 12 |
#define DEFAULT_STREAM_INDEX (0) |
| 13 |
|
| 14 |
#include "DXVAHD.h" |
| 15 |
|
| 16 |
#include "VideoRenderers/RenderFlags.h" |
| 17 |
#include "VideoRenderers/RenderManager.h" |
| 18 |
#include "VideoRenderers/windows/RendererBase.h" |
| 19 |
#include "VideoRenderers/windows/RendererDXVA.h" |
| 20 |
#include "rendering/dx/RenderContext.h" |
| 21 |
#include "utils/StringUtils.h" |
| 22 |
#include "utils/log.h" |
| 23 |
|
| 24 |
#include <mutex> |
| 25 |
|
| 26 |
#include <Windows.h> |
| 27 |
#include <d3d11_4.h> |
| 28 |
#include <dxgi1_5.h> |
| 29 |
|
| 30 |
using namespace DXVA; |
| 31 |
using namespace Microsoft::WRL; |
| 32 |
|
| 33 |
namespace |
| 34 |
{ |
| 35 |
// magic constants taken from Chromium: |
| 36 |
// https://chromium.googlesource.com/chromium/src/+/refs/heads/main/ui/gl/swap_chain_presenter.cc#180 |
| 37 |
constexpr GUID GUID_INTEL_VPE_INTERFACE = { |
| 38 |
0xedd1d4b9, 0x8659, 0x4cbc, {0xa4, 0xd6, 0x98, 0x31, 0xa2, 0x16, 0x3a, 0xc3}}; |
| 39 |
|
| 40 |
constexpr UINT kIntelVpeFnVersion = 0x01; |
| 41 |
constexpr UINT kIntelVpeFnMode = 0x20; |
| 42 |
constexpr UINT kIntelVpeFnScaling = 0x37; |
| 43 |
constexpr UINT kIntelVpeVersion3 = 0x0003; |
| 44 |
constexpr UINT kIntelVpeModePreproc = 0x01; |
| 45 |
constexpr UINT kIntelVpeScalingSuperResolution = 0x2; |
| 46 |
|
| 47 |
constexpr GUID GUID_NVIDIA_PPE_INTERFACE = { |
| 48 |
0xd43ce1b3, 0x1f4b, 0x48ac, {0xba, 0xee, 0xc3, 0xc2, 0x53, 0x75, 0xe6, 0xf7}}; |
| 49 |
|
| 50 |
constexpr UINT kStreamExtensionVersionV1 = 0x1; |
| 51 |
constexpr UINT kStreamExtensionMethodSuperResolution = 0x2; |
| 52 |
} // unnamed namespace |
| 53 |
|
| 54 |
struct DXVA::ProcColorSpaces |
| 55 |
{ |
| 56 |
DXGI_COLOR_SPACE_TYPE inputColorSpace; |
| 57 |
DXGI_COLOR_SPACE_TYPE outputColorSpace; |
| 58 |
}; |
| 59 |
|
| 60 |
CProcessorHD::CProcessorHD() |
| 61 |
{ |
| 62 |
DX::Windowing()->Register(this); |
| 63 |
} |
| 64 |
|
| 65 |
CProcessorHD::~CProcessorHD() |
| 66 |
{ |
| 67 |
DX::Windowing()->Unregister(this); |
| 68 |
UnInit(); |
| 69 |
} |
| 70 |
|
| 71 |
void CProcessorHD::UnInit() |
| 72 |
{ |
| 73 |
std::unique_lock<CCriticalSection> lock(m_section); |
| 74 |
m_enumerator = nullptr; |
| 75 |
Close(); |
| 76 |
} |
| 77 |
|
| 78 |
void CProcessorHD::Close() |
| 79 |
{ |
| 80 |
std::unique_lock<CCriticalSection> lock(m_section); |
| 81 |
m_pVideoProcessor = nullptr; |
| 82 |
m_pVideoContext = nullptr; |
| 83 |
m_pVideoDevice = nullptr; |
| 84 |
m_superResolutionEnabled = false; |
| 85 |
m_configured = false; |
| 86 |
} |
| 87 |
|
| 88 |
bool CProcessorHD::InitProcessor() |
| 89 |
{ |
| 90 |
HRESULT hr{}; |
| 91 |
m_pVideoDevice = nullptr; |
| 92 |
m_pVideoContext = nullptr; |
| 93 |
|
| 94 |
ComPtr<ID3D11DeviceContext1> pD3DDeviceContext = DX::DeviceResources::Get()->GetImmediateContext(); |
| 95 |
ComPtr<ID3D11Device> pD3DDevice = DX::DeviceResources::Get()->GetD3DDevice(); |
| 96 |
|
| 97 |
if (FAILED(hr = pD3DDeviceContext.As(&m_pVideoContext))) |
| 98 |
{ |
| 99 |
CLog::LogF(LOGWARNING, "video context initialization is failed. Error {}", |
| 100 |
DX::GetErrorDescription(hr)); |
| 101 |
return false; |
| 102 |
} |
| 103 |
if (FAILED(hr = pD3DDevice.As(&m_pVideoDevice))) |
| 104 |
{ |
| 105 |
CLog::LogF(LOGWARNING, "video device initialization is failed. Error {}", |
| 106 |
DX::GetErrorDescription(hr)); |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
if (!m_enumerator) |
| 111 |
return false; |
| 112 |
|
| 113 |
m_procCaps = m_enumerator->ProbeProcessorCaps(); |
| 114 |
if (!m_procCaps.m_valid) |
| 115 |
return false; |
| 116 |
|
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
bool CProcessorHD::CheckFormats() const |
| 121 |
{ |
| 122 |
if (!m_isValidConversion) |
| 123 |
return true; |
| 124 |
|
| 125 |
// check default output format (as render target) |
| 126 |
return m_enumerator && m_enumerator->IsFormatSupportedOutput(m_conversion.m_outputFormat); |
| 127 |
} |
| 128 |
|
| 129 |
bool CProcessorHD::Open(const VideoPicture& picture, |
| 130 |
std::shared_ptr<DXVA::CEnumeratorHD> enumerator) |
| 131 |
{ |
| 132 |
Close(); |
| 133 |
|
| 134 |
std::unique_lock<CCriticalSection> lock(m_section); |
| 135 |
|
| 136 |
m_color_primaries = picture.color_primaries; |
| 137 |
m_color_transfer = picture.color_transfer; |
| 138 |
m_enumerator = enumerator; |
| 139 |
|
| 140 |
if (!InitProcessor()) |
| 141 |
return false; |
| 142 |
|
| 143 |
return OpenProcessor(); |
| 144 |
} |
| 145 |
|
| 146 |
bool CProcessorHD::ReInit() |
| 147 |
{ |
| 148 |
std::unique_lock<CCriticalSection> lock(m_section); |
| 149 |
Close(); |
| 150 |
|
| 151 |
if (!InitProcessor()) |
| 152 |
return false; |
| 153 |
|
| 154 |
if (!CheckFormats()) |
| 155 |
return false; |
| 156 |
|
| 157 |
return true; |
| 158 |
} |
| 159 |
|
| 160 |
bool CProcessorHD::OpenProcessor() |
| 161 |
{ |
| 162 |
std::unique_lock<CCriticalSection> lock(m_section); |
| 163 |
|
| 164 |
if ((!m_pVideoDevice || !m_pVideoContext || !m_enumerator || !m_procCaps.m_valid) && !ReInit()) |
| 165 |
{ |
| 166 |
CLog::LogF(LOGDEBUG, "invalid state, failed to re-initialize."); |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
CLog::LogF(LOGDEBUG, "creating processor."); |
| 171 |
|
| 172 |
// create processor |
| 173 |
m_pVideoProcessor = m_enumerator->CreateVideoProcessor(m_procCaps.m_procIndex); |
| 174 |
|
| 175 |
if (!m_pVideoProcessor) |
| 176 |
{ |
| 177 |
CLog::LogF(LOGDEBUG, "failed creating video processor."); |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
m_pVideoContext->VideoProcessorSetStreamAutoProcessingMode(m_pVideoProcessor.Get(), 0, FALSE); |
| 182 |
m_pVideoContext->VideoProcessorSetStreamOutputRate( |
| 183 |
m_pVideoProcessor.Get(), 0, D3D11_VIDEO_PROCESSOR_OUTPUT_RATE_NORMAL, FALSE, 0); |
| 184 |
|
| 185 |
ComPtr<ID3D11VideoContext1> videoCtx1; |
| 186 |
if (SUCCEEDED(m_pVideoContext.As(&videoCtx1))) |
| 187 |
{ |
| 188 |
videoCtx1->VideoProcessorSetOutputShaderUsage(m_pVideoProcessor.Get(), 1); |
| 189 |
} |
| 190 |
else |
| 191 |
{ |
| 192 |
CLog::LogF(LOGWARNING, "unable to retrieve ID3D11VideoContext1 to allow usage of shaders on " |
| 193 |
"video processor output surfaces."); |
| 194 |
} |
| 195 |
|
| 196 |
// Output background color (black) |
| 197 |
D3D11_VIDEO_COLOR color; |
| 198 |
color.YCbCr = { 0.0625f, 0.5f, 0.5f, 1.0f }; // black color |
| 199 |
m_pVideoContext->VideoProcessorSetOutputBackgroundColor(m_pVideoProcessor.Get(), TRUE, &color); |
| 200 |
|
| 201 |
// AMD/HDR (as of 2023-06-16): processor tone maps by default and modifies high code values |
| 202 |
// We want "passthrough" of the signal and to do our own tone mapping when needed. |
| 203 |
// Disable the functionality by pretending that the display supports all PQ levels (0-10000) |
| 204 |
const DXGI_ADAPTER_DESC ad = DX::DeviceResources::Get()->GetAdapterDesc(); |
| 205 |
bool streamIsHDR = |
| 206 |
(m_color_primaries == AVCOL_PRI_BT2020) && |
| 207 |
(m_color_transfer == AVCOL_TRC_SMPTE2084 || m_color_transfer == AVCOL_TRC_ARIB_STD_B67); |
| 208 |
|
| 209 |
if (m_procCaps.m_hasMetadataHDR10Support && ad.VendorId == PCIV_AMD && streamIsHDR) |
| 210 |
{ |
| 211 |
ComPtr<ID3D11VideoContext2> videoCtx2; |
| 212 |
if (SUCCEEDED(m_pVideoContext.As(&videoCtx2))) |
| 213 |
{ |
| 214 |
DXGI_HDR_METADATA_HDR10 hdr10{}; |
| 215 |
hdr10.MaxMasteringLuminance = 10000; |
| 216 |
hdr10.MinMasteringLuminance = 0; |
| 217 |
|
| 218 |
videoCtx2->VideoProcessorSetOutputHDRMetaData(m_pVideoProcessor.Get(), |
| 219 |
DXGI_HDR_METADATA_TYPE_HDR10, |
| 220 |
sizeof(DXGI_HDR_METADATA_HDR10), &hdr10); |
| 221 |
|
| 222 |
CLog::LogF(LOGDEBUG, "video processor tone mapping disabled."); |
| 223 |
} |
| 224 |
else |
| 225 |
{ |
| 226 |
CLog::LogF(LOGDEBUG, |
| 227 |
"unable to retrieve ID3D11VideoContext2 to disable video processor tone mapping."); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
return true; |
| 232 |
} |
| 233 |
|
| 234 |
void CProcessorHD::ApplyFilter(D3D11_VIDEO_PROCESSOR_FILTER filter, int value, int min, int max, int def) const |
| 235 |
{ |
| 236 |
if (filter >= static_cast<D3D11_VIDEO_PROCESSOR_FILTER>(NUM_FILTERS)) |
| 237 |
return; |
| 238 |
|
| 239 |
// Unsupported filter. Ignore. |
| 240 |
if (!m_procCaps.m_Filters[filter].bSupported) |
| 241 |
return; |
| 242 |
|
| 243 |
D3D11_VIDEO_PROCESSOR_FILTER_RANGE range = m_procCaps.m_Filters[filter].Range; |
| 244 |
int val; |
| 245 |
|
| 246 |
if(value > def) |
| 247 |
val = range.Default + (range.Maximum - range.Default) * (value - def) / (max - def); |
| 248 |
else if(value < def) |
| 249 |
val = range.Default + (range.Minimum - range.Default) * (value - def) / (min - def); |
| 250 |
else |
| 251 |
val = range.Default; |
| 252 |
|
| 253 |
m_pVideoContext->VideoProcessorSetStreamFilter(m_pVideoProcessor.Get(), DEFAULT_STREAM_INDEX, filter, val != range.Default, val); |
| 254 |
} |
| 255 |
|
| 256 |
ComPtr<ID3D11VideoProcessorInputView> CProcessorHD::GetInputView(CRenderBuffer* view) const |
| 257 |
{ |
| 258 |
if (m_enumerator) |
| 259 |
{ |
| 260 |
D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC vpivd = {0, D3D11_VPIV_DIMENSION_TEXTURE2D, {0, 0}}; |
| 261 |
|
| 262 |
ComPtr<ID3D11Resource> resource; |
| 263 |
unsigned arrayIdx = 0; |
| 264 |
HRESULT hr = view->GetResource(resource.GetAddressOf(), &arrayIdx); |
| 265 |
if (SUCCEEDED(hr)) |
| 266 |
{ |
| 267 |
vpivd.Texture2D.ArraySlice = arrayIdx; |
| 268 |
return m_enumerator->CreateVideoProcessorInputView(resource.Get(), &vpivd); |
| 269 |
} |
| 270 |
} |
| 271 |
return {}; |
| 272 |
} |
| 273 |
|
| 274 |
bool CProcessorHD::CheckVideoParameters(const CRect& src, |
| 275 |
const CRect& dst, |
| 276 |
const UINT& rotation, |
| 277 |
const float& contrast, |
| 278 |
const float& brightness, |
| 279 |
const CRenderBuffer& rb) |
| 280 |
{ |
| 281 |
bool updatedParameter{false}; |
| 282 |
|
| 283 |
if (!m_configured || m_lastSrc != src) |
| 284 |
{ |
| 285 |
const RECT sourceRECT = {static_cast<LONG>(src.x1), static_cast<LONG>(src.y1), |
| 286 |
static_cast<LONG>(src.x2), static_cast<LONG>(src.y2)}; |
| 287 |
|
| 288 |
m_pVideoContext->VideoProcessorSetStreamSourceRect(m_pVideoProcessor.Get(), |
| 289 |
DEFAULT_STREAM_INDEX, TRUE, &sourceRECT); |
| 290 |
m_lastSrc = src; |
| 291 |
updatedParameter = true; |
| 292 |
} |
| 293 |
|
| 294 |
if (!m_configured || m_lastDst != dst) |
| 295 |
{ |
| 296 |
const RECT dstRECT = {static_cast<LONG>(dst.x1), static_cast<LONG>(dst.y1), |
| 297 |
static_cast<LONG>(dst.x2), static_cast<LONG>(dst.y2)}; |
| 298 |
|
| 299 |
m_pVideoContext->VideoProcessorSetStreamDestRect(m_pVideoProcessor.Get(), DEFAULT_STREAM_INDEX, |
| 300 |
TRUE, &dstRECT); |
| 301 |
m_pVideoContext->VideoProcessorSetOutputTargetRect(m_pVideoProcessor.Get(), TRUE, &dstRECT); |
| 302 |
|
| 303 |
m_lastDst = dst; |
| 304 |
updatedParameter = true; |
| 305 |
} |
| 306 |
|
| 307 |
if (!m_configured || m_lastBrightness != brightness) |
| 308 |
{ |
| 309 |
ApplyFilter(D3D11_VIDEO_PROCESSOR_FILTER_BRIGHTNESS, static_cast<int>(brightness), 0, 100, 50); |
| 310 |
|
| 311 |
m_lastBrightness = brightness; |
| 312 |
updatedParameter = true; |
| 313 |
} |
| 314 |
|
| 315 |
if (!m_configured || m_lastContrast != contrast) |
| 316 |
{ |
| 317 |
ApplyFilter(D3D11_VIDEO_PROCESSOR_FILTER_CONTRAST, static_cast<int>(contrast), 0, 100, 50); |
| 318 |
|
| 319 |
m_lastContrast = contrast; |
| 320 |
updatedParameter = true; |
| 321 |
} |
| 322 |
|
| 323 |
// unused filters - set once and forget |
| 324 |
if (!m_configured) |
| 325 |
{ |
| 326 |
ApplyFilter(D3D11_VIDEO_PROCESSOR_FILTER_HUE, 50, 0, 100, 50); |
| 327 |
ApplyFilter(D3D11_VIDEO_PROCESSOR_FILTER_SATURATION, 50, 0, 100, 50); |
| 328 |
} |
| 329 |
|
| 330 |
if (!m_configured || m_lastRotation != rotation) |
| 331 |
{ |
| 332 |
m_pVideoContext->VideoProcessorSetStreamRotation( |
| 333 |
m_pVideoProcessor.Get(), DEFAULT_STREAM_INDEX, rotation != 0, |
| 334 |
static_cast<D3D11_VIDEO_PROCESSOR_ROTATION>(rotation / 90)); |
| 335 |
|
| 336 |
m_lastRotation = rotation; |
| 337 |
updatedParameter = true; |
| 338 |
} |
| 339 |
|
| 340 |
ComPtr<ID3D11VideoContext1> videoCtx1; |
| 341 |
if (SUCCEEDED(m_pVideoContext.As(&videoCtx1))) |
| 342 |
{ |
| 343 |
if (!m_configured || m_lastConversion != m_conversion) |
| 344 |
{ |
| 345 |
videoCtx1->VideoProcessorSetStreamColorSpace1(m_pVideoProcessor.Get(), DEFAULT_STREAM_INDEX, |
| 346 |
m_conversion.m_inputCS); |
| 347 |
videoCtx1->VideoProcessorSetOutputColorSpace1(m_pVideoProcessor.Get(), |
| 348 |
m_conversion.m_outputCS); |
| 349 |
|
| 350 |
m_lastConversion = m_conversion; |
| 351 |
updatedParameter = true; |
| 352 |
} |
| 353 |
} |
| 354 |
else if (!m_configured || m_lastColorSpace != rb.color_space || m_lastFullRange != rb.full_range) |
| 355 |
{ |
| 356 |
// input colorspace |
| 357 |
bool isBT601 = rb.color_space == AVCOL_SPC_BT470BG || rb.color_space == AVCOL_SPC_SMPTE170M; |
| 358 |
// clang-format off |
| 359 |
D3D11_VIDEO_PROCESSOR_COLOR_SPACE colorSpace |
| 360 |
{ |
| 361 |
0u, // 0 - Playback, 1 - Processing |
| 362 |
rb.full_range ? 0u : 1u, // 0 - Full (0-255), 1 - Limited (16-235) (RGB) |
| 363 |
isBT601 ? 1u : 0u, // 0 - BT.601, 1 - BT.709 |
| 364 |
0u, // 0 - Conventional YCbCr, 1 - xvYCC |
| 365 |
rb.full_range ? 2u : 1u // 0 - driver defaults, 2 - Full range [0-255], 1 - Studio range [16-235] (YUV) |
| 366 |
}; |
| 367 |
// clang-format on |
| 368 |
m_pVideoContext->VideoProcessorSetStreamColorSpace(m_pVideoProcessor.Get(), |
| 369 |
DEFAULT_STREAM_INDEX, &colorSpace); |
| 370 |
// Output color space |
| 371 |
// don't apply any color range conversion, this will be fixed at later stage. |
| 372 |
colorSpace.Usage = 0; // 0 - playback, 1 - video processing |
| 373 |
colorSpace.RGB_Range = DX::Windowing()->UseLimitedColor() ? 1 : 0; // 0 - 0-255, 1 - 16-235 |
| 374 |
colorSpace.YCbCr_Matrix = 1; // 0 - BT.601, 1 = BT.709 |
| 375 |
colorSpace.YCbCr_xvYCC = 1; // 0 - Conventional YCbCr, 1 - xvYCC |
| 376 |
colorSpace.Nominal_Range = 0; // 2 - 0-255, 1 = 16-235, 0 - undefined |
| 377 |
m_pVideoContext->VideoProcessorSetOutputColorSpace(m_pVideoProcessor.Get(), &colorSpace); |
| 378 |
|
| 379 |
m_lastColorSpace = rb.color_space; |
| 380 |
m_lastFullRange = rb.full_range; |
| 381 |
updatedParameter = true; |
| 382 |
} |
| 383 |
|
| 384 |
return updatedParameter; |
| 385 |
} |
| 386 |
|
| 387 |
bool CProcessorHD::Render(CRect src, CRect dst, ID3D11Resource* target, CRenderBuffer** views, DWORD flags, UINT frameIdx, UINT rotation, float contrast, float brightness) |
| 388 |
{ |
| 389 |
std::unique_lock<CCriticalSection> lock(m_section); |
| 390 |
|
| 391 |
// restore processor if it was lost |
| 392 |
if (!m_pVideoProcessor && !OpenProcessor()) |
| 393 |
return false; |
| 394 |
|
| 395 |
if (!views[2]) |
| 396 |
return false; |
| 397 |
|
| 398 |
const bool updatedParam = |
| 399 |
CheckVideoParameters(src, dst, rotation, contrast, brightness, *views[2]); |
| 400 |
|
| 401 |
D3D11_VIDEO_FRAME_FORMAT dxvaFrameFormat = D3D11_VIDEO_FRAME_FORMAT_PROGRESSIVE; |
| 402 |
|
| 403 |
if ((flags & RENDER_FLAG_FIELD0) && (flags & RENDER_FLAG_TOP)) |
| 404 |
dxvaFrameFormat = D3D11_VIDEO_FRAME_FORMAT_INTERLACED_TOP_FIELD_FIRST; |
| 405 |
else if ((flags & RENDER_FLAG_FIELD1) && (flags & RENDER_FLAG_BOT)) |
| 406 |
dxvaFrameFormat = D3D11_VIDEO_FRAME_FORMAT_INTERLACED_TOP_FIELD_FIRST; |
| 407 |
else if ((flags & RENDER_FLAG_FIELD0) && (flags & RENDER_FLAG_BOT)) |
| 408 |
dxvaFrameFormat = D3D11_VIDEO_FRAME_FORMAT_INTERLACED_BOTTOM_FIELD_FIRST; |
| 409 |
else if ((flags & RENDER_FLAG_FIELD1) && (flags & RENDER_FLAG_TOP)) |
| 410 |
dxvaFrameFormat = D3D11_VIDEO_FRAME_FORMAT_INTERLACED_BOTTOM_FIELD_FIRST; |
| 411 |
|
| 412 |
m_pVideoContext->VideoProcessorSetStreamFrameFormat(m_pVideoProcessor.Get(), DEFAULT_STREAM_INDEX, |
| 413 |
dxvaFrameFormat); |
| 414 |
|
| 415 |
D3D11_VIDEO_PROCESSOR_STREAM stream_data = {}; |
| 416 |
stream_data.Enable = TRUE; |
| 417 |
|
| 418 |
const bool frameProgressive = dxvaFrameFormat == D3D11_VIDEO_FRAME_FORMAT_PROGRESSIVE; |
| 419 |
|
| 420 |
// Progressive or Interlaced video at normal rate. |
| 421 |
const bool secondField = ((flags & RENDER_FLAG_FIELD1) && !frameProgressive) ? 1 : 0; |
| 422 |
stream_data.InputFrameOrField = frameIdx + (secondField ? 1 : 0); |
| 423 |
stream_data.OutputIndex = secondField; |
| 424 |
|
| 425 |
// Render() gets called once for each displayed frame, following the pattern necessary to adapt |
| 426 |
// the source fps to the display fps, with repetitions as needed (ex. 3:2 for 23.98fps at 59Hz) |
| 427 |
// However there is no need to render the same frame more than once, the intermediate target is |
| 428 |
// not cleared and the output of the previous processing is still there. |
| 429 |
// For nVidia deinterlacing it's more than an optimization. The processor must see each field |
| 430 |
// only once or it won't switch from bob to a more advanced algorithm. |
| 431 |
// for ex. when playing 25i at 60fps, decoded frames A B => output A0 A1 B0 B1 B1 |
| 432 |
// B1 field is repeated and the second B1 must be skipped. |
| 433 |
// Exception: always process when a parameter changes to provide immediate feedback to the user |
| 434 |
|
| 435 |
if (m_configured && m_lastInputFrameOrField == stream_data.InputFrameOrField && |
| 436 |
m_lastOutputIndex == stream_data.OutputIndex && !updatedParam) |
| 437 |
return true; |
| 438 |
|
| 439 |
m_lastInputFrameOrField = stream_data.InputFrameOrField; |
| 440 |
m_lastOutputIndex = stream_data.OutputIndex; |
| 441 |
|
| 442 |
unsigned int providedPast = 0; |
| 443 |
for (int i = 3; i < 8; i++) |
| 444 |
{ |
| 445 |
if (views[i]) |
| 446 |
providedPast++; |
| 447 |
} |
| 448 |
unsigned int providedFuture = 0; |
| 449 |
for (int i = 1; i >= 0; i--) |
| 450 |
{ |
| 451 |
if (views[i]) |
| 452 |
providedFuture++; |
| 453 |
} |
| 454 |
const int futureFrames = std::min(providedFuture, m_procCaps.m_rateCaps.FutureFrames); |
| 455 |
const int pastFrames = std::min(providedPast, m_procCaps.m_rateCaps.PastFrames); |
| 456 |
std::vector<ID3D11VideoProcessorInputView*> pastViews(pastFrames, nullptr); |
| 457 |
std::vector<ID3D11VideoProcessorInputView*> futureViews(futureFrames, nullptr); |
| 458 |
|
| 459 |
stream_data.PastFrames = pastFrames; |
| 460 |
stream_data.FutureFrames = futureFrames; |
| 461 |
stream_data.ppPastSurfaces = pastViews.data(); |
| 462 |
stream_data.ppFutureSurfaces = futureViews.data(); |
| 463 |
|
| 464 |
std::vector<ComPtr<ID3D11VideoProcessorInputView>> all_views; |
| 465 |
const int start = 2 - futureFrames; |
| 466 |
const int end = 2 + pastFrames; |
| 467 |
int count = 0; |
| 468 |
|
| 469 |
for (int i = start; i <= end; i++) |
| 470 |
{ |
| 471 |
if (!views[i]) |
| 472 |
continue; |
| 473 |
|
| 474 |
ComPtr<ID3D11VideoProcessorInputView> view = GetInputView(views[i]); |
| 475 |
|
| 476 |
if (i > 2) |
| 477 |
{ |
| 478 |
// frames order should be { ?, T-3, T-2, T-1 } |
| 479 |
pastViews[2 + pastFrames - i] = view.Get(); |
| 480 |
} |
| 481 |
else if (i == 2) |
| 482 |
{ |
| 483 |
stream_data.pInputSurface = view.Get(); |
| 484 |
} |
| 485 |
else if (i < 2) |
| 486 |
{ |
| 487 |
// frames order should be { T+1, T+2, T+3, .. } |
| 488 |
futureViews[1 - i] = view.Get(); |
| 489 |
} |
| 490 |
if (view) |
| 491 |
{ |
| 492 |
count++; |
| 493 |
all_views.push_back(view); |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
if (count != pastFrames + futureFrames + 1) |
| 498 |
{ |
| 499 |
CLog::LogF(LOGERROR, "incomplete views set."); |
| 500 |
return false; |
| 501 |
} |
| 502 |
|
| 503 |
// create output view for surface. |
| 504 |
const D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC outputViewDesc = {D3D11_VPOV_DIMENSION_TEXTURE2D, |
| 505 |
{0}}; |
| 506 |
ComPtr<ID3D11VideoProcessorOutputView> pOutputView = |
| 507 |
m_enumerator->CreateVideoProcessorOutputView(target, &outputViewDesc); |
| 508 |
|
| 509 |
HRESULT hr{}; |
| 510 |
if (pOutputView) |
| 511 |
{ |
| 512 |
hr = m_pVideoContext->VideoProcessorBlt(m_pVideoProcessor.Get(), pOutputView.Get(), 0, 1, |
| 513 |
&stream_data); |
| 514 |
if (S_OK != hr) |
| 515 |
{ |
| 516 |
CLog::LogF(FAILED(hr) ? LOGERROR : LOGWARNING, |
| 517 |
"VideoProcessorBlt returned {} while VideoProcessorBlt execution.", |
| 518 |
DX::GetErrorDescription(hr)); |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
if (!m_configured) |
| 523 |
m_configured = true; |
| 524 |
|
| 525 |
return pOutputView && !FAILED(hr); |
| 526 |
} |
| 527 |
|
| 528 |
bool CProcessorHD::IsSuperResolutionSuitable(const VideoPicture& picture) |
| 529 |
{ |
| 530 |
if (picture.iWidth > 1920) |
| 531 |
return false; |
| 532 |
|
| 533 |
const UINT outputWidth = DX::Windowing()->GetBackBuffer().GetWidth(); |
| 534 |
|
| 535 |
if (outputWidth <= picture.iWidth) |
| 536 |
return false; |
| 537 |
|
| 538 |
if (picture.color_primaries == AVCOL_PRI_BT2020 || |
| 539 |
picture.color_transfer == AVCOL_TRC_SMPTE2084 || |
| 540 |
picture.color_transfer == AVCOL_TRC_ARIB_STD_B67) |
| 541 |
return false; |
| 542 |
|
| 543 |
return true; |
| 544 |
} |
| 545 |
|
| 546 |
void CProcessorHD::TryEnableVideoSuperResolution() |
| 547 |
{ |
| 548 |
if (!m_pVideoContext || !m_pVideoProcessor) |
| 549 |
return; |
| 550 |
|
| 551 |
const DXGI_ADAPTER_DESC ad = DX::DeviceResources::Get()->GetAdapterDesc(); |
| 552 |
|
| 553 |
if (ad.VendorId == PCIV_Intel) |
| 554 |
{ |
| 555 |
EnableIntelVideoSuperResolution(); |
| 556 |
} |
| 557 |
else if (ad.VendorId == PCIV_NVIDIA) |
| 558 |
{ |
| 559 |
EnableNvidiaRTXVideoSuperResolution(); |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
void CProcessorHD::EnableIntelVideoSuperResolution() |
| 564 |
{ |
| 565 |
UINT param = 0; |
| 566 |
|
| 567 |
struct IntelVpeExt |
| 568 |
{ |
| 569 |
UINT function; |
| 570 |
void* param; |
| 571 |
}; |
| 572 |
|
| 573 |
IntelVpeExt ext{0, ¶m}; |
| 574 |
|
| 575 |
ext.function = kIntelVpeFnVersion; |
| 576 |
param = kIntelVpeVersion3; |
| 577 |
|
| 578 |
HRESULT hr = m_pVideoContext->VideoProcessorSetOutputExtension( |
| 579 |
m_pVideoProcessor.Get(), &GUID_INTEL_VPE_INTERFACE, sizeof(ext), &ext); |
| 580 |
if (FAILED(hr)) |
| 581 |
{ |
| 582 |
CLog::LogF(LOGWARNING, "Failed to set the Intel VPE version with error {}.", |
| 583 |
DX::GetErrorDescription(hr)); |
| 584 |
return; |
| 585 |
} |
| 586 |
|
| 587 |
ext.function = kIntelVpeFnMode; |
| 588 |
param = kIntelVpeModePreproc; |
| 589 |
|
| 590 |
hr = m_pVideoContext->VideoProcessorSetOutputExtension( |
| 591 |
m_pVideoProcessor.Get(), &GUID_INTEL_VPE_INTERFACE, sizeof(ext), &ext); |
| 592 |
if (FAILED(hr)) |
| 593 |
{ |
| 594 |
CLog::LogF(LOGWARNING, "Failed to set the Intel VPE mode with error {}.", |
| 595 |
DX::GetErrorDescription(hr)); |
| 596 |
return; |
| 597 |
} |
| 598 |
|
| 599 |
ext.function = kIntelVpeFnScaling; |
| 600 |
param = kIntelVpeScalingSuperResolution; |
| 601 |
|
| 602 |
hr = m_pVideoContext->VideoProcessorSetStreamExtension( |
| 603 |
m_pVideoProcessor.Get(), 0, &GUID_INTEL_VPE_INTERFACE, sizeof(ext), &ext); |
| 604 |
if (FAILED(hr)) |
| 605 |
{ |
| 606 |
CLog::LogF(LOGWARNING, "Failed to set the Intel VPE scaling type with error {}.", |
| 607 |
DX::GetErrorDescription(hr)); |
| 608 |
return; |
| 609 |
} |
| 610 |
|
| 611 |
CLog::LogF(LOGINFO, "Intel Video Super Resolution request enable successfully"); |
| 612 |
m_superResolutionEnabled = true; |
| 613 |
} |
| 614 |
|
| 615 |
void CProcessorHD::EnableNvidiaRTXVideoSuperResolution() |
| 616 |
{ |
| 617 |
struct NvidiaStreamExt |
| 618 |
{ |
| 619 |
UINT version; |
| 620 |
UINT method; |
| 621 |
UINT enable; |
| 622 |
}; |
| 623 |
|
| 624 |
NvidiaStreamExt ext = {kStreamExtensionVersionV1, kStreamExtensionMethodSuperResolution, 1u}; |
| 625 |
|
| 626 |
HRESULT hr = m_pVideoContext->VideoProcessorSetStreamExtension( |
| 627 |
m_pVideoProcessor.Get(), 0, &GUID_NVIDIA_PPE_INTERFACE, sizeof(ext), &ext); |
| 628 |
if (FAILED(hr)) |
| 629 |
{ |
| 630 |
CLog::LogF(LOGWARNING, "Failed to set the NVIDIA video process stream extension with error {}.", |
| 631 |
DX::GetErrorDescription(hr)); |
| 632 |
return; |
| 633 |
} |
| 634 |
|
| 635 |
CLog::LogF(LOGINFO, "RTX Video Super Resolution request enable successfully"); |
| 636 |
m_superResolutionEnabled = true; |
| 637 |
} |
| 638 |
|
| 639 |
bool CProcessorHD::SetConversion(const ProcessorConversion& conversion) |
| 640 |
{ |
| 641 |
std::unique_lock<CCriticalSection> lock(m_section); |
| 642 |
|
| 643 |
if (!m_enumerator) |
| 644 |
return false; |
| 645 |
|
| 646 |
if (!m_enumerator || !m_enumerator->IsFormatSupportedInput(conversion.m_inputFormat) || |
| 647 |
!m_enumerator->IsFormatSupportedOutput(conversion.m_outputFormat)) |
| 648 |
return false; |
| 649 |
|
| 650 |
if (m_enumerator->IsEnumerator1Available() && |
| 651 |
!m_enumerator->CheckConversion(conversion.m_inputFormat, conversion.m_inputCS, |
| 652 |
conversion.m_outputFormat, conversion.m_outputCS)) |
| 653 |
{ |
| 654 |
CLog::LogF(LOGERROR, "Conversion {} is not supported", conversion.ToString()); |
| 655 |
return false; |
| 656 |
} |
| 657 |
|
| 658 |
m_conversion = conversion; |
| 659 |
m_isValidConversion = true; |
| 660 |
|
| 661 |
return true; |
| 662 |
} |
| 663 |
|
| 664 |
bool CProcessorHD::Supports(ERENDERFEATURE feature) const |
| 665 |
{ |
| 666 |
switch (feature) |
| 667 |
{ |
| 668 |
case RENDERFEATURE_BRIGHTNESS: |
| 669 |
return m_procCaps.m_Filters[D3D11_VIDEO_PROCESSOR_FILTER_BRIGHTNESS].bSupported; |
| 670 |
case RENDERFEATURE_CONTRAST: |
| 671 |
return m_procCaps.m_Filters[D3D11_VIDEO_PROCESSOR_FILTER_CONTRAST].bSupported; |
| 672 |
case RENDERFEATURE_ROTATION: |
| 673 |
return (m_procCaps.m_vcaps.FeatureCaps & D3D11_VIDEO_PROCESSOR_FEATURE_CAPS_ROTATION); |
| 674 |
default: |
| 675 |
return false; |
| 676 |
} |
| 677 |
} |