| 1 |
/* |
| 2 |
* Copyright (C) 2005-2023 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 |
#include "DXVAEnumeratorHD.h" |
| 10 |
|
| 11 |
#include "rendering/dx/RenderContext.h" |
| 12 |
#include "utils/log.h" |
| 13 |
|
| 14 |
#include <mutex> |
| 15 |
|
| 16 |
#include <Windows.h> |
| 17 |
#include <d3d11_4.h> |
| 18 |
#include <dxgi1_5.h> |
| 19 |
|
| 20 |
using namespace DXVA; |
| 21 |
using namespace Microsoft::WRL; |
| 22 |
|
| 23 |
const std::string ProcessorConversion::ToString() const |
| 24 |
{ |
| 25 |
return StringUtils::Format("{} / {} to {} / {}", DX::DXGIFormatToString(m_inputFormat), |
| 26 |
DX::DXGIColorSpaceTypeToString(m_inputCS), |
| 27 |
DX::DXGIFormatToString(m_outputFormat), |
| 28 |
DX::DXGIColorSpaceTypeToString(m_outputCS)); |
| 29 |
} |
| 30 |
|
| 31 |
CEnumeratorHD::CEnumeratorHD() |
| 32 |
{ |
| 33 |
DX::Windowing()->Register(this); |
| 34 |
} |
| 35 |
|
| 36 |
CEnumeratorHD::~CEnumeratorHD() |
| 37 |
{ |
| 38 |
std::unique_lock<CCriticalSection> lock(m_section); |
| 39 |
DX::Windowing()->Unregister(this); |
| 40 |
UnInit(); |
| 41 |
} |
| 42 |
|
| 43 |
void CEnumeratorHD::UnInit() |
| 44 |
{ |
| 45 |
Close(); |
| 46 |
} |
| 47 |
|
| 48 |
void CEnumeratorHD::Close() |
| 49 |
{ |
| 50 |
std::unique_lock<CCriticalSection> lock(m_section); |
| 51 |
m_pEnumerator1 = nullptr; |
| 52 |
m_pEnumerator = nullptr; |
| 53 |
m_pVideoDevice = nullptr; |
| 54 |
} |
| 55 |
|
| 56 |
bool CEnumeratorHD::Open(unsigned int width, unsigned int height, DXGI_FORMAT input_dxgi_format) |
| 57 |
{ |
| 58 |
Close(); |
| 59 |
|
| 60 |
std::unique_lock<CCriticalSection> lock(m_section); |
| 61 |
m_width = width; |
| 62 |
m_height = height; |
| 63 |
m_input_dxgi_format = input_dxgi_format; |
| 64 |
|
| 65 |
return OpenEnumerator(); |
| 66 |
} |
| 67 |
|
| 68 |
bool CEnumeratorHD::OpenEnumerator() |
| 69 |
{ |
| 70 |
HRESULT hr{}; |
| 71 |
ComPtr<ID3D11Device> pD3DDevice = DX::DeviceResources::Get()->GetD3DDevice(); |
| 72 |
|
| 73 |
if (FAILED(hr = pD3DDevice.As(&m_pVideoDevice))) |
| 74 |
{ |
| 75 |
CLog::LogF(LOGWARNING, "video device initialization is failed. Error {}", |
| 76 |
DX::GetErrorDescription(hr)); |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
CLog::LogF(LOGDEBUG, "initializing video enumerator with params: {}x{}.", m_width, m_height); |
| 81 |
|
| 82 |
D3D11_VIDEO_PROCESSOR_CONTENT_DESC contentDesc = {}; |
| 83 |
contentDesc.InputFrameFormat = D3D11_VIDEO_FRAME_FORMAT_INTERLACED_TOP_FIELD_FIRST; |
| 84 |
contentDesc.InputWidth = m_width; |
| 85 |
contentDesc.InputHeight = m_height; |
| 86 |
contentDesc.OutputWidth = m_width; |
| 87 |
contentDesc.OutputHeight = m_height; |
| 88 |
contentDesc.Usage = D3D11_VIDEO_USAGE_PLAYBACK_NORMAL; |
| 89 |
|
| 90 |
if (FAILED(hr = m_pVideoDevice->CreateVideoProcessorEnumerator( |
| 91 |
&contentDesc, m_pEnumerator.ReleaseAndGetAddressOf()))) |
| 92 |
{ |
| 93 |
CLog::LogF(LOGWARNING, "failed to init video enumerator with params: {}x{}. Error {}", m_width, |
| 94 |
m_height, DX::GetErrorDescription(hr)); |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
if (FAILED(hr = m_pEnumerator.As(&m_pEnumerator1))) |
| 99 |
{ |
| 100 |
CLog::LogF(LOGDEBUG, "ID3D11VideoProcessorEnumerator1 not available on this system. Message {}", |
| 101 |
DX::GetErrorDescription(hr)); |
| 102 |
} |
| 103 |
|
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
ProcessorCapabilities CEnumeratorHD::ProbeProcessorCaps() |
| 108 |
{ |
| 109 |
std::unique_lock<CCriticalSection> lock(m_section); |
| 110 |
|
| 111 |
if (!m_pEnumerator) |
| 112 |
return {}; |
| 113 |
|
| 114 |
HRESULT hr{}; |
| 115 |
ProcessorCapabilities result{}; |
| 116 |
|
| 117 |
if (CServiceBroker::GetLogging().IsLogLevelLogged(LOGDEBUG) && |
| 118 |
CServiceBroker::GetLogging().CanLogComponent(LOGVIDEO)) |
| 119 |
{ |
| 120 |
ProcessorFormats formats = GetProcessorFormats(true, true); |
| 121 |
|
| 122 |
if (formats.m_valid) |
| 123 |
{ |
| 124 |
CLog::LogFC(LOGDEBUG, LOGVIDEO, "Supported input formats:"); |
| 125 |
|
| 126 |
for (const auto& it : formats.m_input) |
| 127 |
CLog::LogFC(LOGDEBUG, LOGVIDEO, "{}", DX::DXGIFormatToString(it)); |
| 128 |
|
| 129 |
CLog::LogFC(LOGDEBUG, LOGVIDEO, "Supported output formats:"); |
| 130 |
|
| 131 |
for (const auto& it : formats.m_output) |
| 132 |
CLog::LogFC(LOGDEBUG, LOGVIDEO, "{}", DX::DXGIFormatToString(it)); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
if (FAILED(hr = m_pEnumerator->GetVideoProcessorCaps(&result.m_vcaps))) |
| 137 |
{ |
| 138 |
CLog::LogF(LOGWARNING, "failed to get processor caps. Error {}", DX::GetErrorDescription(hr)); |
| 139 |
return {}; |
| 140 |
} |
| 141 |
|
| 142 |
CLog::LogF(LOGDEBUG, "video processor has {} rate conversion.", |
| 143 |
result.m_vcaps.RateConversionCapsCount); |
| 144 |
CLog::LogF(LOGDEBUG, "video processor has {:#x} feature caps.", result.m_vcaps.FeatureCaps); |
| 145 |
CLog::LogF(LOGDEBUG, "video processor has {:#x} device caps.", result.m_vcaps.DeviceCaps); |
| 146 |
CLog::LogF(LOGDEBUG, "video processor has {:#x} input format caps.", |
| 147 |
result.m_vcaps.InputFormatCaps); |
| 148 |
CLog::LogF(LOGDEBUG, "video processor has {:#x} auto stream caps.", |
| 149 |
result.m_vcaps.AutoStreamCaps); |
| 150 |
CLog::LogF(LOGDEBUG, "video processor has {:#x} stereo caps.", result.m_vcaps.StereoCaps); |
| 151 |
CLog::LogF(LOGDEBUG, "video processor has {} max input streams.", result.m_vcaps.MaxInputStreams); |
| 152 |
CLog::LogF(LOGDEBUG, "video processor has {} max stream states.", result.m_vcaps.MaxStreamStates); |
| 153 |
if (result.m_vcaps.FeatureCaps & D3D11_VIDEO_PROCESSOR_FEATURE_CAPS_METADATA_HDR10) |
| 154 |
{ |
| 155 |
CLog::LogF(LOGDEBUG, "video processor supports HDR10 metadata."); |
| 156 |
result.m_hasMetadataHDR10Support = true; |
| 157 |
} |
| 158 |
|
| 159 |
if (0 != (result.m_vcaps.FeatureCaps & D3D11_VIDEO_PROCESSOR_FEATURE_CAPS_LEGACY)) |
| 160 |
CLog::LogF(LOGWARNING, "the video driver does not support full video processing capabilities."); |
| 161 |
|
| 162 |
if (result.m_vcaps.FeatureCaps & D3D11_VIDEO_PROCESSOR_FEATURE_CAPS_STEREO) |
| 163 |
CLog::LogF(LOGDEBUG, "video processor supports stereo."); |
| 164 |
|
| 165 |
if (result.m_vcaps.FeatureCaps & D3D11_VIDEO_PROCESSOR_FEATURE_CAPS_ROTATION) |
| 166 |
CLog::LogF(LOGDEBUG, "video processor supports rotation."); |
| 167 |
|
| 168 |
if (result.m_vcaps.FeatureCaps & D3D11_VIDEO_PROCESSOR_FEATURE_CAPS_SHADER_USAGE) |
| 169 |
CLog::LogF(LOGDEBUG, "video processor supports shader usage."); |
| 170 |
|
| 171 |
const UINT deinterlacingCaps = |
| 172 |
D3D11_VIDEO_PROCESSOR_PROCESSOR_CAPS_DEINTERLACE_BLEND | |
| 173 |
D3D11_VIDEO_PROCESSOR_PROCESSOR_CAPS_DEINTERLACE_BOB | |
| 174 |
D3D11_VIDEO_PROCESSOR_PROCESSOR_CAPS_DEINTERLACE_ADAPTIVE | |
| 175 |
D3D11_VIDEO_PROCESSOR_PROCESSOR_CAPS_DEINTERLACE_MOTION_COMPENSATION; |
| 176 |
unsigned maxProcCaps = 0; |
| 177 |
// try to find best processor |
| 178 |
for (unsigned int i = 0; i < result.m_vcaps.RateConversionCapsCount; i++) |
| 179 |
{ |
| 180 |
D3D11_VIDEO_PROCESSOR_RATE_CONVERSION_CAPS convCaps; |
| 181 |
if (FAILED(hr = m_pEnumerator->GetVideoProcessorRateConversionCaps(i, &convCaps))) |
| 182 |
{ |
| 183 |
CLog::LogF(LOGWARNING, "unable to retrieve processor rate conversion caps {}. Error {}", i, |
| 184 |
DX::GetErrorDescription(hr)); |
| 185 |
continue; |
| 186 |
} |
| 187 |
|
| 188 |
// check only deinterlace caps |
| 189 |
if ((convCaps.ProcessorCaps & deinterlacingCaps) > maxProcCaps) |
| 190 |
{ |
| 191 |
result.m_procIndex = i; |
| 192 |
maxProcCaps = convCaps.ProcessorCaps & deinterlacingCaps; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
CLog::LogF(LOGDEBUG, "selected video processor index: {}.", result.m_procIndex); |
| 197 |
|
| 198 |
if (SUCCEEDED(hr = m_pEnumerator->GetVideoProcessorRateConversionCaps(result.m_procIndex, |
| 199 |
&result.m_rateCaps))) |
| 200 |
{ |
| 201 |
CLog::LogF(LOGINFO, "supported deinterlace methods: blend:{}, bob:{}, adaptive:{}, mocomp:{}.", |
| 202 |
(result.m_rateCaps.ProcessorCaps & 0x1) != 0 ? "yes" : "no", // BLEND |
| 203 |
(result.m_rateCaps.ProcessorCaps & 0x2) != 0 ? "yes" : "no", // BOB |
| 204 |
(result.m_rateCaps.ProcessorCaps & 0x4) != 0 ? "yes" : "no", // ADAPTIVE |
| 205 |
(result.m_rateCaps.ProcessorCaps & 0x8) != 0 ? "yes" : "no" // MOTION_COMPENSATION |
| 206 |
); |
| 207 |
} |
| 208 |
else |
| 209 |
{ |
| 210 |
CLog::LogF(LOGWARNING, |
| 211 |
"unable to retrieve processor rate conversion caps {}, the deinterlacing " |
| 212 |
"capabilities are unknown. Error {}", |
| 213 |
result.m_procIndex, DX::GetErrorDescription(hr)); |
| 214 |
} |
| 215 |
|
| 216 |
CLog::LogF( |
| 217 |
LOGDEBUG, |
| 218 |
"selected video processor recommends {} future frames and {} past frames for best results.", |
| 219 |
result.m_rateCaps.FutureFrames, result.m_rateCaps.PastFrames); |
| 220 |
|
| 221 |
// Get the image filtering capabilities. |
| 222 |
for (size_t i = 0; i < NUM_FILTERS; i++) |
| 223 |
{ |
| 224 |
if (result.m_vcaps.FilterCaps & PROCAMP_FILTERS[i].cap) |
| 225 |
{ |
| 226 |
result.m_Filters[i].Range = {}; |
| 227 |
result.m_Filters[i].bSupported = SUCCEEDED(m_pEnumerator->GetVideoProcessorFilterRange( |
| 228 |
PROCAMP_FILTERS[i].filter, &result.m_Filters[i].Range)); |
| 229 |
|
| 230 |
if (result.m_Filters[i].bSupported) |
| 231 |
{ |
| 232 |
CLog::LogF(LOGDEBUG, "filter {} has following params - max: {}, min: {}, default: {}", |
| 233 |
PROCAMP_FILTERS[i].name, result.m_Filters[i].Range.Maximum, |
| 234 |
result.m_Filters[i].Range.Minimum, result.m_Filters[i].Range.Default); |
| 235 |
} |
| 236 |
} |
| 237 |
else |
| 238 |
{ |
| 239 |
CLog::LogF(LOGDEBUG, "filter {} not supported by processor.", PROCAMP_FILTERS[i].name); |
| 240 |
result.m_Filters[i].bSupported = false; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
result.m_valid = true; |
| 245 |
|
| 246 |
return result; |
| 247 |
} |
| 248 |
|
| 249 |
ProcessorFormats CEnumeratorHD::GetProcessorFormats(bool inputFormats, bool outputFormats) const |
| 250 |
{ |
| 251 |
// Not initialized yet |
| 252 |
if (!m_pEnumerator) |
| 253 |
return {}; |
| 254 |
|
| 255 |
ProcessorFormats formats; |
| 256 |
HRESULT hr{}; |
| 257 |
UINT uiFlags{0}; |
| 258 |
for (int fmt = DXGI_FORMAT_UNKNOWN; fmt <= DXGI_FORMAT_V408; fmt++) |
| 259 |
{ |
| 260 |
const DXGI_FORMAT dxgiFormat = static_cast<DXGI_FORMAT>(fmt); |
| 261 |
if (S_OK == (hr = m_pEnumerator->CheckVideoProcessorFormat(dxgiFormat, &uiFlags))) |
| 262 |
{ |
| 263 |
if ((uiFlags & D3D11_VIDEO_PROCESSOR_FORMAT_SUPPORT_INPUT) && inputFormats) |
| 264 |
formats.m_input.push_back(dxgiFormat); |
| 265 |
if ((uiFlags & D3D11_VIDEO_PROCESSOR_FORMAT_SUPPORT_OUTPUT) && outputFormats) |
| 266 |
formats.m_output.push_back(dxgiFormat); |
| 267 |
} |
| 268 |
else |
| 269 |
{ |
| 270 |
CLog::LogF(LOGWARNING, |
| 271 |
"Unable to retrieve support of the dxva processor for format {}, error {}", |
| 272 |
DX::DXGIFormatToString(dxgiFormat), DX::GetErrorDescription(hr)); |
| 273 |
return formats; |
| 274 |
} |
| 275 |
} |
| 276 |
formats.m_valid = true; |
| 277 |
|
| 278 |
return formats; |
| 279 |
} |
| 280 |
|
| 281 |
std::vector<DXGI_FORMAT> CEnumeratorHD::GetProcessorRGBOutputFormats() const |
| 282 |
{ |
| 283 |
ProcessorFormats formats = GetProcessorFormats(false, true); |
| 284 |
if (!formats.m_valid) |
| 285 |
return {}; |
| 286 |
|
| 287 |
std::vector<DXGI_FORMAT> result; |
| 288 |
result.reserve(formats.m_output.size()); |
| 289 |
for (const auto& format : formats.m_output) |
| 290 |
{ |
| 291 |
const std::string name = DX::DXGIFormatToString(format); |
| 292 |
|
| 293 |
if (name.find('R') != std::string::npos && name.find('G') != std::string::npos && |
| 294 |
name.find('B') != std::string::npos) |
| 295 |
{ |
| 296 |
result.emplace_back(format); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
return result; |
| 301 |
} |
| 302 |
|
| 303 |
bool CEnumeratorHD::CheckConversion(DXGI_FORMAT inputFormat, |
| 304 |
DXGI_COLOR_SPACE_TYPE inputCS, |
| 305 |
DXGI_FORMAT outputFormat, |
| 306 |
DXGI_COLOR_SPACE_TYPE outputCS) |
| 307 |
{ |
| 308 |
std::unique_lock<CCriticalSection> lock(m_section); |
| 309 |
return CheckConversionInternal(inputFormat, inputCS, outputFormat, outputCS); |
| 310 |
} |
| 311 |
|
| 312 |
bool CEnumeratorHD::CheckConversionInternal(DXGI_FORMAT inputFormat, |
| 313 |
DXGI_COLOR_SPACE_TYPE inputCS, |
| 314 |
DXGI_FORMAT outputFormat, |
| 315 |
DXGI_COLOR_SPACE_TYPE outputCS) const |
| 316 |
{ |
| 317 |
// Not initialized yet or OS < Windows 10 |
| 318 |
if (!m_pEnumerator || !m_pEnumerator1) |
| 319 |
return false; |
| 320 |
|
| 321 |
HRESULT hr; |
| 322 |
BOOL supported; |
| 323 |
|
| 324 |
if (SUCCEEDED(hr = m_pEnumerator1->CheckVideoProcessorFormatConversion( |
| 325 |
inputFormat, inputCS, outputFormat, outputCS, &supported))) |
| 326 |
{ |
| 327 |
return (supported == TRUE); |
| 328 |
} |
| 329 |
else |
| 330 |
{ |
| 331 |
CLog::LogF(LOGERROR, "unable to validate the format conversion, error {}", |
| 332 |
DX::GetErrorDescription(hr)); |
| 333 |
return false; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
ProcessorConversions CEnumeratorHD::ListConversions( |
| 338 |
DXGI_FORMAT inputFormat, |
| 339 |
const std::vector<DXGI_COLOR_SPACE_TYPE>& inputColorSpaces, |
| 340 |
const std::vector<DXGI_FORMAT>& outputFormats, |
| 341 |
const std::vector<DXGI_COLOR_SPACE_TYPE>& outputColorSpaces) const |
| 342 |
{ |
| 343 |
// Not initialized yet or OS < Windows 10 |
| 344 |
if (!m_pEnumerator || !m_pEnumerator1) |
| 345 |
return {}; |
| 346 |
|
| 347 |
ProcessorConversions result; |
| 348 |
|
| 349 |
for (const DXGI_COLOR_SPACE_TYPE& inputCS : inputColorSpaces) |
| 350 |
{ |
| 351 |
for (const DXGI_FORMAT& outputFormat : outputFormats) |
| 352 |
{ |
| 353 |
for (const DXGI_COLOR_SPACE_TYPE& outputCS : outputColorSpaces) |
| 354 |
{ |
| 355 |
if (CheckConversionInternal(inputFormat, inputCS, outputFormat, outputCS)) |
| 356 |
{ |
| 357 |
result.push_back(ProcessorConversion{inputFormat, inputCS, outputFormat, outputCS}); |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
return result; |
| 363 |
} |
| 364 |
|
| 365 |
void CEnumeratorHD::LogSupportedConversions(const DXGI_FORMAT inputFormat, |
| 366 |
const DXGI_COLOR_SPACE_TYPE inputNativeCS) |
| 367 |
{ |
| 368 |
std::unique_lock<CCriticalSection> lock(m_section); |
| 369 |
|
| 370 |
if (!m_pEnumerator) |
| 371 |
return; |
| 372 |
|
| 373 |
// Windows 8 and above compatible code |
| 374 |
|
| 375 |
HRESULT hr; |
| 376 |
UINT uiFlags; |
| 377 |
|
| 378 |
if (FAILED(hr = m_pEnumerator->CheckVideoProcessorFormat(inputFormat, &uiFlags))) |
| 379 |
{ |
| 380 |
CLog::LogFC(LOGDEBUG, LOGVIDEO, |
| 381 |
"unable to retrieve processor support of input format {}. Error {}", |
| 382 |
DX::DXGIFormatToString(inputFormat), DX::GetErrorDescription(hr)); |
| 383 |
return; |
| 384 |
} |
| 385 |
else if (!(uiFlags & D3D11_VIDEO_PROCESSOR_FORMAT_SUPPORT_INPUT)) |
| 386 |
{ |
| 387 |
CLog::LogFC(LOGDEBUG, LOGVIDEO, |
| 388 |
"input format {} not supported by the processor. No conversion possible.", |
| 389 |
DX::DXGIFormatToString(inputFormat)); |
| 390 |
return; |
| 391 |
} |
| 392 |
|
| 393 |
// Windows 10 and above from this point on |
| 394 |
if (!m_pEnumerator1) |
| 395 |
return; |
| 396 |
|
| 397 |
CLog::LogFC(LOGDEBUG, LOGVIDEO, "The source is {} / {}", DX::DXGIFormatToString(inputFormat), |
| 398 |
DX::DXGIColorSpaceTypeToString(inputNativeCS)); |
| 399 |
|
| 400 |
// Possible input color spaces: YCbCr only |
| 401 |
std::vector<DXGI_COLOR_SPACE_TYPE> ycbcrColorSpaces; |
| 402 |
// Possible output color spaces: RGB only |
| 403 |
std::vector<DXGI_COLOR_SPACE_TYPE> rgbColorSpaces; |
| 404 |
|
| 405 |
for (UINT colorSpace = DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709; |
| 406 |
colorSpace < DXGI_COLOR_SPACE_YCBCR_STUDIO_G24_TOPLEFT_P2020; ++colorSpace) |
| 407 |
{ |
| 408 |
const DXGI_COLOR_SPACE_TYPE cs = static_cast<DXGI_COLOR_SPACE_TYPE>(colorSpace); |
| 409 |
|
| 410 |
constexpr std::string_view rgb("RGB_"); |
| 411 |
if (DX::DXGIColorSpaceTypeToString(cs).compare(0, rgb.size(), rgb) == 0) |
| 412 |
rgbColorSpaces.push_back(cs); |
| 413 |
|
| 414 |
constexpr std::string_view ycbcr("YCBCR_"); |
| 415 |
if (DX::DXGIColorSpaceTypeToString(cs).compare(0, ycbcr.size(), ycbcr) == 0) |
| 416 |
ycbcrColorSpaces.push_back(cs); |
| 417 |
} |
| 418 |
|
| 419 |
// Only probe the output formats of RGB/BGR type supported by the processor. |
| 420 |
const std::vector<DXGI_FORMAT> outputFormats = GetProcessorRGBOutputFormats(); |
| 421 |
|
| 422 |
// Color spaces supported directly by the swap chain - as a set for easy lookup |
| 423 |
const std::vector<DXGI_COLOR_SPACE_TYPE> bbcs = |
| 424 |
DX::DeviceResources::Get()->GetSwapChainColorSpaces(); |
| 425 |
const std::set<DXGI_COLOR_SPACE_TYPE> backbufferColorSpaces(bbcs.begin(), bbcs.end()); |
| 426 |
|
| 427 |
// The input format cannot be worked around and is fixed. |
| 428 |
// Restrieve all combinations of the allowed |
| 429 |
// - input color spaces |
| 430 |
// - output formats |
| 431 |
// - output color spaces |
| 432 |
|
| 433 |
const ProcessorConversions conversions = |
| 434 |
ListConversions(inputFormat, ycbcrColorSpaces, outputFormats, rgbColorSpaces); |
| 435 |
|
| 436 |
std::string conversionsString; |
| 437 |
|
| 438 |
for (const ProcessorConversion& c : conversions) |
| 439 |
{ |
| 440 |
conversionsString.append("\n"); |
| 441 |
conversionsString.append(StringUtils::Format( |
| 442 |
"{} / {} {:<{}} to {:<{}} / {} {:<{}}", DX::DXGIFormatToString(c.m_inputFormat), |
| 443 |
(c.m_inputCS == inputNativeCS) ? "N" : " ", DX::DXGIColorSpaceTypeToString(c.m_inputCS), 32, |
| 444 |
DX::DXGIFormatToString(c.m_outputFormat), 26, |
| 445 |
(backbufferColorSpaces.find(c.m_outputCS) != backbufferColorSpaces.end()) ? "bb" : " ", |
| 446 |
DX::DXGIColorSpaceTypeToString(c.m_outputCS), 32)); |
| 447 |
} |
| 448 |
|
| 449 |
CLog::LogFC(LOGDEBUG, LOGVIDEO, |
| 450 |
"Supported conversions from format {}\n(N native input color space, bb supported as " |
| 451 |
"swap chain backbuffer){}", |
| 452 |
DX::DXGIFormatToString(inputFormat), conversionsString); |
| 453 |
} |
| 454 |
|
| 455 |
bool CEnumeratorHD::IsFormatSupportedInput(DXGI_FORMAT format) |
| 456 |
{ |
| 457 |
std::unique_lock<CCriticalSection> lock(m_section); |
| 458 |
return IsFormatSupportedInternal(format, D3D11_VIDEO_PROCESSOR_FORMAT_SUPPORT_INPUT); |
| 459 |
} |
| 460 |
bool CEnumeratorHD::IsFormatSupportedOutput(DXGI_FORMAT format) |
| 461 |
{ |
| 462 |
std::unique_lock<CCriticalSection> lock(m_section); |
| 463 |
return IsFormatSupportedInternal(format, D3D11_VIDEO_PROCESSOR_FORMAT_SUPPORT_OUTPUT); |
| 464 |
} |
| 465 |
|
| 466 |
bool CEnumeratorHD::IsFormatSupportedInternal(DXGI_FORMAT format, |
| 467 |
D3D11_VIDEO_PROCESSOR_FORMAT_SUPPORT support) const |
| 468 |
{ |
| 469 |
// Not initialized yet |
| 470 |
if (!m_pEnumerator) |
| 471 |
return false; |
| 472 |
|
| 473 |
UINT uiFlags; |
| 474 |
if (S_OK == m_pEnumerator->CheckVideoProcessorFormat(format, &uiFlags)) |
| 475 |
{ |
| 476 |
if (uiFlags & support) |
| 477 |
return true; |
| 478 |
} |
| 479 |
|
| 480 |
CLog::LogF(LOGERROR, "unsupported format {} for {}.", DX::DXGIFormatToString(format), |
| 481 |
DX::D3D11VideoProcessorFormatSupportToString(support)); |
| 482 |
return false; |
| 483 |
} |
| 484 |
|
| 485 |
ComPtr<ID3D11VideoProcessor> CEnumeratorHD::CreateVideoProcessor(UINT RateConversionIndex) |
| 486 |
{ |
| 487 |
std::unique_lock<CCriticalSection> lock(m_section); |
| 488 |
|
| 489 |
// Not initialized yet |
| 490 |
if (!m_pEnumerator) |
| 491 |
return {}; |
| 492 |
|
| 493 |
ComPtr<ID3D11VideoProcessor> videoProcessor; |
| 494 |
|
| 495 |
// create processor |
| 496 |
HRESULT hr = m_pVideoDevice->CreateVideoProcessor(m_pEnumerator.Get(), RateConversionIndex, |
| 497 |
videoProcessor.ReleaseAndGetAddressOf()); |
| 498 |
if (FAILED(hr)) |
| 499 |
{ |
| 500 |
CLog::LogF(LOGDEBUG, "failed creating video processor with error {}.", |
| 501 |
DX::GetErrorDescription(hr)); |
| 502 |
return {}; |
| 503 |
} |
| 504 |
|
| 505 |
return videoProcessor; |
| 506 |
} |
| 507 |
|
| 508 |
ComPtr<ID3D11VideoProcessorInputView> CEnumeratorHD::CreateVideoProcessorInputView( |
| 509 |
ID3D11Resource* pResource, const D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC* pDesc) |
| 510 |
{ |
| 511 |
std::unique_lock<CCriticalSection> lock(m_section); |
| 512 |
|
| 513 |
// Not initialized yet |
| 514 |
if (!m_pEnumerator) |
| 515 |
return {}; |
| 516 |
|
| 517 |
ComPtr<ID3D11VideoProcessorInputView> inputView; |
| 518 |
|
| 519 |
HRESULT hr = m_pVideoDevice->CreateVideoProcessorInputView(pResource, m_pEnumerator.Get(), pDesc, |
| 520 |
inputView.GetAddressOf()); |
| 521 |
|
| 522 |
if (S_OK != hr) |
| 523 |
CLog::LogF(FAILED(hr) ? LOGERROR : LOGWARNING, "CreateVideoProcessorInputView returned {}.", |
| 524 |
DX::GetErrorDescription(hr)); |
| 525 |
|
| 526 |
return inputView; |
| 527 |
} |
| 528 |
|
| 529 |
ComPtr<ID3D11VideoProcessorOutputView> CEnumeratorHD::CreateVideoProcessorOutputView( |
| 530 |
ID3D11Resource* pResource, const D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC* pDesc) |
| 531 |
{ |
| 532 |
std::unique_lock<CCriticalSection> lock(m_section); |
| 533 |
|
| 534 |
// Not initialized yet |
| 535 |
if (!m_pEnumerator) |
| 536 |
return {}; |
| 537 |
|
| 538 |
ComPtr<ID3D11VideoProcessorOutputView> outputView; |
| 539 |
|
| 540 |
HRESULT hr = m_pVideoDevice->CreateVideoProcessorOutputView(pResource, m_pEnumerator.Get(), pDesc, |
| 541 |
outputView.GetAddressOf()); |
| 542 |
if (S_OK != hr) |
| 543 |
CLog::LogF(FAILED(hr) ? LOGERROR : LOGWARNING, "CreateVideoProcessorOutputView returned {}.", |
| 544 |
DX::GetErrorDescription(hr)); |
| 545 |
|
| 546 |
return outputView; |
| 547 |
} |
| 548 |
|
| 549 |
ProcessorConversions CEnumeratorHD::SupportedConversions(const SupportedConversionsArgs& args) |
| 550 |
{ |
| 551 |
std::unique_lock<CCriticalSection> lock(m_section); |
| 552 |
|
| 553 |
// Not initialized yet |
| 554 |
if (!m_pEnumerator) |
| 555 |
return {}; |
| 556 |
|
| 557 |
const char* pr = av_color_primaries_name(args.m_colorPrimaries); |
| 558 |
const char* cs = av_color_space_name(args.m_colorSpace); |
| 559 |
const char* tr = av_color_transfer_name(args.m_colorTransfer); |
| 560 |
|
| 561 |
const std::string_view prim = pr ? pr : "unknown"; |
| 562 |
const std::string_view colspace = cs ? cs : "unknown"; |
| 563 |
const std::string_view trans = tr ? tr : "unknown"; |
| 564 |
|
| 565 |
CLog::LogF(LOGDEBUG, "source: primaries {}, color space {}, transfer {}, {} range, dest: {}", |
| 566 |
prim, colspace, tr, args.m_fullRange ? "full" : "limited", |
| 567 |
args.m_hdrOutput ? "HDR" : "SDR"); |
| 568 |
|
| 569 |
DXGIColorSpaceArgs inputArgs(args.m_colorPrimaries, args.m_colorSpace, args.m_colorTransfer, |
| 570 |
args.m_fullRange, DefaultChromaSiting(args)); |
| 571 |
|
| 572 |
DXGIColorSpaceArgs outputArgs(args.m_hdrOutput ? AVCOL_PRI_BT2020 : AVCOL_PRI_BT709, |
| 573 |
AVCOL_SPC_RGB, |
| 574 |
args.m_hdrOutput ? AVCOL_TRC_SMPTE2084 : AVCOL_TRC_BT709, |
| 575 |
!DX::Windowing()->UseLimitedColor(), AVCHROMA_LOC_UNSPECIFIED); |
| 576 |
|
| 577 |
const DXGI_COLOR_SPACE_TYPE inputCS = AvToDxgiColorSpace(inputArgs); |
| 578 |
const DXGI_COLOR_SPACE_TYPE outputCS = AvToDxgiColorSpace(outputArgs); |
| 579 |
|
| 580 |
// Enumerator1 interface and validation API not available? (Windows 8) |
| 581 |
// SDR has been supported since Windows 7, return a conversion. |
| 582 |
// It doesn't matter if the conversion characteristics don't always match perfectly the source |
| 583 |
// and destination attributes as the dxva processor render path with Windows < 10 ignores the |
| 584 |
// conversion and uses an older API to specify input and output attributes. |
| 585 |
if (!m_pEnumerator1) |
| 586 |
{ |
| 587 |
if (args.m_colorPrimaries == AVCOL_PRI_BT2020 && |
| 588 |
(args.m_colorTransfer == AVCOL_TRC_SMPTE2084 || |
| 589 |
args.m_colorTransfer == AVCOL_TRC_ARIB_STD_B67)) |
| 590 |
{ |
| 591 |
CLog::LogF(LOGWARNING, "ID3D11VideoProcessorEnumerator1 not available on legacy OS, HDR is " |
| 592 |
"not available."); |
| 593 |
return {}; |
| 594 |
} |
| 595 |
else |
| 596 |
{ |
| 597 |
CLog::LogF(LOGWARNING, "ID3D11VideoProcessorEnumerator1 not available on legacy OS, SDR is " |
| 598 |
"assumed to be available."); |
| 599 |
|
| 600 |
return { |
| 601 |
ProcessorConversion(m_input_dxgi_format, inputCS, DXGI_FORMAT_B8G8R8A8_UNORM, outputCS)}; |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
CLog::LogF(LOGDEBUG, "natural dxgi color space mapping of source: {}, dest: {}", |
| 606 |
inputCS == DXGI_COLOR_SPACE_CUSTOM ? "NA" : DX::DXGIColorSpaceTypeToString(inputCS), |
| 607 |
outputCS == DXGI_COLOR_SPACE_CUSTOM ? "NA" : DX::DXGIColorSpaceTypeToString(outputCS)); |
| 608 |
|
| 609 |
// Exceptions to the straight mapping before conversion to DXGI color space |
| 610 |
|
| 611 |
// Bypass dxva processor tonemapping to perform own tone-mapping |
| 612 |
if (args.m_colorPrimaries == AVCOL_PRI_BT2020 && args.m_colorTransfer == AVCOL_TRC_SMPTE2084 && |
| 613 |
!args.m_hdrOutput) |
| 614 |
{ |
| 615 |
CLog::LogF(LOGDEBUG, "mapping exception to perform own HDR > SDR tone mapping"); |
| 616 |
inputArgs.color_transfer = AVCOL_TRC_BT709; |
| 617 |
} |
| 618 |
|
| 619 |
// HLG output not supported by Windows. |
| 620 |
// HDR output: HLG transfer will be converted to PQ |
| 621 |
// SDR output: transfer function is compatible with SDR |
| 622 |
if (args.m_colorPrimaries == AVCOL_PRI_BT2020 && args.m_colorTransfer == AVCOL_TRC_ARIB_STD_B67) |
| 623 |
{ |
| 624 |
if (args.m_hdrOutput) |
| 625 |
{ |
| 626 |
CLog::LogF(LOGDEBUG, "mapping exception for HLG > HDR"); |
| 627 |
inputArgs.color_transfer = AVCOL_TRC_SMPTE2084; |
| 628 |
} |
| 629 |
else |
| 630 |
{ |
| 631 |
CLog::LogF(LOGDEBUG, "mapping exception for HLG > SDR"); |
| 632 |
inputArgs.color_transfer = AVCOL_TRC_BT709; |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
ProcessorConversions conversions = LogAndListConversions(inputArgs, outputArgs); |
| 637 |
|
| 638 |
if (conversions.empty() && args.m_colorPrimaries == AVCOL_PRI_BT2020) |
| 639 |
{ |
| 640 |
inputArgs.chroma_location = AlternativeChromaSiting(inputArgs.chroma_location); |
| 641 |
|
| 642 |
const char* loc = av_chroma_location_name(inputArgs.chroma_location); |
| 643 |
const std::string_view location = loc ? loc : "unknown"; |
| 644 |
|
| 645 |
CLog::LogF(LOGDEBUG, |
| 646 |
"no supported conversions. Trying alternative chroma siting {} for HDR source.", |
| 647 |
loc); |
| 648 |
|
| 649 |
conversions = LogAndListConversions(inputArgs, outputArgs); |
| 650 |
} |
| 651 |
|
| 652 |
if (conversions.empty()) |
| 653 |
{ |
| 654 |
CLog::LogF(LOGWARNING, "no conversions supported."); |
| 655 |
} |
| 656 |
else |
| 657 |
{ |
| 658 |
if (CServiceBroker::GetLogging().IsLogLevelLogged(LOGDEBUG) && |
| 659 |
CServiceBroker::GetLogging().CanLogComponent(LOGVIDEO)) |
| 660 |
{ |
| 661 |
for (const auto& c : conversions) |
| 662 |
CLog::LogFC(LOGDEBUG, LOGVIDEO, "supported conversion: {}", c.ToString()); |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
return conversions; |
| 667 |
} |
| 668 |
|
| 669 |
ProcessorConversions CEnumeratorHD::LogAndListConversions( |
| 670 |
const DXGIColorSpaceArgs& inputArgs, const DXGIColorSpaceArgs& outputArgs) const |
| 671 |
{ |
| 672 |
const DXGI_COLOR_SPACE_TYPE inputCS = AvToDxgiColorSpace(inputArgs); |
| 673 |
const DXGI_COLOR_SPACE_TYPE outputCS = AvToDxgiColorSpace(outputArgs); |
| 674 |
|
| 675 |
CLog::LogF(LOGDEBUG, "dxgi color space mapping of source: {}, dest color space: {}", |
| 676 |
inputCS == DXGI_COLOR_SPACE_CUSTOM ? "NA" : DX::DXGIColorSpaceTypeToString(inputCS), |
| 677 |
outputCS == DXGI_COLOR_SPACE_CUSTOM ? "NA" : DX::DXGIColorSpaceTypeToString(outputCS)); |
| 678 |
|
| 679 |
if (inputCS == DXGI_COLOR_SPACE_CUSTOM || outputCS == DXGI_COLOR_SPACE_CUSTOM) |
| 680 |
{ |
| 681 |
CLog::LogF(LOGDEBUG, "conversion is not possible because of source or destination without " |
| 682 |
"mapping to a DXGI color space value."); |
| 683 |
return {}; |
| 684 |
} |
| 685 |
|
| 686 |
std::vector<DXGI_FORMAT> RenderingOutputFormats = {DXGI_FORMAT_B8G8R8A8_UNORM}; |
| 687 |
|
| 688 |
// The AMD dxva processor of the GCN architecture has low chroma upsampling quality |
| 689 |
// (nearest neighbor) from YCbCr SDR color primaries to 10 bit RGB. Transfer function doesn't matter. |
| 690 |
// > Blacklist 10 bit output. Float output (not used at this point) has the same problem. |
| 691 |
|
| 692 |
if (!DX::DeviceResources::Get()->IsGCNOrOlder() || inputArgs.primaries == AVCOL_PRI_BT2020) |
| 693 |
RenderingOutputFormats.push_back(DXGI_FORMAT_R10G10B10A2_UNORM); |
| 694 |
|
| 695 |
return ListConversions(m_input_dxgi_format, std::vector<DXGI_COLOR_SPACE_TYPE>{inputCS}, |
| 696 |
RenderingOutputFormats, std::vector<DXGI_COLOR_SPACE_TYPE>{outputCS}); |
| 697 |
} |
| 698 |
|
| 699 |
DXGI_COLOR_SPACE_TYPE CEnumeratorHD::AvToDxgiColorSpace(const DXGIColorSpaceArgs& csArgs) |
| 700 |
{ |
| 701 |
// RGB |
| 702 |
if (csArgs.color_space == AVCOL_SPC_RGB) |
| 703 |
{ |
| 704 |
if (!csArgs.full_range) |
| 705 |
{ |
| 706 |
if (csArgs.primaries == AVCOL_PRI_BT2020) |
| 707 |
{ |
| 708 |
if (csArgs.color_transfer == AVCOL_TRC_SMPTEST2084) |
| 709 |
return DXGI_COLOR_SPACE_RGB_STUDIO_G2084_NONE_P2020; |
| 710 |
|
| 711 |
return DXGI_COLOR_SPACE_RGB_STUDIO_G22_NONE_P2020; |
| 712 |
} |
| 713 |
return DXGI_COLOR_SPACE_RGB_STUDIO_G22_NONE_P709; |
| 714 |
} |
| 715 |
|
| 716 |
if (csArgs.primaries == AVCOL_PRI_BT2020) |
| 717 |
{ |
| 718 |
if (csArgs.color_transfer == AVCOL_TRC_SMPTEST2084) |
| 719 |
return DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020; |
| 720 |
|
| 721 |
return DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P2020; |
| 722 |
} |
| 723 |
if (csArgs.color_transfer == AVCOL_TRC_LINEAR || csArgs.color_transfer == AVCOL_TRC_LOG) |
| 724 |
return DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709; |
| 725 |
|
| 726 |
return DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709; |
| 727 |
} |
| 728 |
// UHDTV |
| 729 |
if (csArgs.primaries == AVCOL_PRI_BT2020) |
| 730 |
{ |
| 731 |
if (csArgs.color_transfer == AVCOL_TRC_SMPTEST2084) |
| 732 |
{ |
| 733 |
// Full range DXGI_COLOR_SPACE_YCBCR_FULL_G2084_TOPLEFT_P2020/LEFT do not exist at this time. |
| 734 |
// > return no match |
| 735 |
// Chroma siting: top left is default because it is used in UHD Blu-Rays and ffmpeg flagging |
| 736 |
// may not always be provided. |
| 737 |
if (csArgs.full_range) |
| 738 |
return DXGI_COLOR_SPACE_CUSTOM; |
| 739 |
else |
| 740 |
{ |
| 741 |
switch (csArgs.chroma_location) |
| 742 |
{ |
| 743 |
case AVCHROMA_LOC_LEFT: |
| 744 |
return DXGI_COLOR_SPACE_YCBCR_STUDIO_G2084_LEFT_P2020; |
| 745 |
case AVCHROMA_LOC_TOPLEFT: |
| 746 |
// fallthrough on purpose |
| 747 |
case AVCHROMA_LOC_UNSPECIFIED: |
| 748 |
return DXGI_COLOR_SPACE_YCBCR_STUDIO_G2084_TOPLEFT_P2020; |
| 749 |
default: |
| 750 |
return DXGI_COLOR_SPACE_CUSTOM; |
| 751 |
} |
| 752 |
} |
| 753 |
} |
| 754 |
// HLG transfer can be used for HLG source in SDR display if is supported |
| 755 |
if (csArgs.color_transfer == AVCOL_TRC_ARIB_STD_B67) |
| 756 |
{ |
| 757 |
if (csArgs.full_range) |
| 758 |
return DXGI_COLOR_SPACE_YCBCR_FULL_GHLG_TOPLEFT_P2020; |
| 759 |
else |
| 760 |
return DXGI_COLOR_SPACE_YCBCR_STUDIO_GHLG_TOPLEFT_P2020; |
| 761 |
} |
| 762 |
|
| 763 |
// SDR transfer function with HDR primaries: not standard, used to avoid tonemapping by dxva |
| 764 |
// Prefer top left for UNSPECIFIED because this will be used mostly for UHD-BD derived video |
| 765 |
switch (csArgs.chroma_location) |
| 766 |
{ |
| 767 |
case AVCHROMA_LOC_LEFT: |
| 768 |
return csArgs.full_range ? DXGI_COLOR_SPACE_YCBCR_FULL_G22_LEFT_P2020 |
| 769 |
: DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P2020; |
| 770 |
case AVCHROMA_LOC_TOPLEFT: |
| 771 |
// fallthrough on purpose |
| 772 |
case AVCHROMA_LOC_UNSPECIFIED: |
| 773 |
return csArgs.full_range ? DXGI_COLOR_SPACE_CUSTOM |
| 774 |
: DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_TOPLEFT_P2020; |
| 775 |
} |
| 776 |
} |
| 777 |
// SDTV |
| 778 |
if (csArgs.primaries == AVCOL_PRI_BT470BG || csArgs.primaries == AVCOL_PRI_SMPTE170M) |
| 779 |
{ |
| 780 |
if (csArgs.full_range) |
| 781 |
return DXGI_COLOR_SPACE_YCBCR_FULL_G22_LEFT_P601; |
| 782 |
else |
| 783 |
return DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P601; |
| 784 |
} |
| 785 |
// JPG |
| 786 |
if (csArgs.color_transfer == AVCOL_TRC_SMPTE170M) |
| 787 |
{ |
| 788 |
if (csArgs.full_range) |
| 789 |
return DXGI_COLOR_SPACE_YCBCR_FULL_G22_NONE_P709_X601; |
| 790 |
else |
| 791 |
return DXGI_COLOR_SPACE_CUSTOM; |
| 792 |
} |
| 793 |
// HDTV - fallback |
| 794 |
if (csArgs.full_range) |
| 795 |
return DXGI_COLOR_SPACE_YCBCR_FULL_G22_LEFT_P709; |
| 796 |
else |
| 797 |
return DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P709; |
| 798 |
} |
| 799 |
|
| 800 |
AVChromaLocation CEnumeratorHD::DefaultChromaSiting(const SupportedConversionsArgs& args) const |
| 801 |
{ |
| 802 |
// Chroma siting does not apply to RGB |
| 803 |
if (args.m_colorSpace == AVCOL_SPC_RGB) |
| 804 |
return AVCHROMA_LOC_UNSPECIFIED; |
| 805 |
|
| 806 |
// UHD-BD and HLG standards use top left chroma siting |
| 807 |
if (args.m_colorPrimaries == AVCOL_PRI_BT2020 && (args.m_colorTransfer == AVCOL_TRC_SMPTE2084 || |
| 808 |
args.m_colorTransfer == AVCOL_TRC_ARIB_STD_B67)) |
| 809 |
return AVCHROMA_LOC_TOPLEFT; |
| 810 |
|
| 811 |
return AVCHROMA_LOC_LEFT; |
| 812 |
} |
| 813 |
|
| 814 |
AVChromaLocation CEnumeratorHD::AlternativeChromaSiting(const AVChromaLocation& location) const |
| 815 |
{ |
| 816 |
// TOPLEFT and LEFT are the only sitings supported by DXGI at this time. |
| 817 |
switch (location) |
| 818 |
{ |
| 819 |
case AVCHROMA_LOC_TOPLEFT: |
| 820 |
return AVCHROMA_LOC_LEFT; |
| 821 |
case AVCHROMA_LOC_LEFT: |
| 822 |
return AVCHROMA_LOC_TOPLEFT; |
| 823 |
default: |
| 824 |
return AVCHROMA_LOC_UNSPECIFIED; |
| 825 |
} |
| 826 |
} |