Bring PS5 GTA6 to 120FPS with DLSS 5. An advanced Windows video player and capture enhancement tool featuring real-time preview for videos, images, and capture card sources. Combine AI-powered Super Resolution, DLSS5 image enhancement, and Frame Generation to deliver a next-generation visual experience.
C++
fix(capture): restore stable format identity and verify negotiation
Veyra Goal Agent7fe6201e4b02
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 531155cb..b368b367 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -590,6 +590,9 @@ if(VEYRA_FFMPEG_ROOT AND EXISTS "${VEYRA_FFMPEG_ROOT}/include/libavformat/avformtarget_compile_definitions(veyra_fsr_upscale_probe PRIVATE NOMINMAX WIN32_LEAN_AND_MEAN UNICODE _UNICODE)target_link_libraries(veyra_fsr_upscale_probe PRIVATE d3d12 dxgi dxguid)add_executable(veyra_capture_color_tests tests/unit/CaptureColorContractTests.cpp)+ add_executable(veyra_capture_format_selection_tests tests/unit/CaptureFormatSelectionTests.cpp)+ target_compile_features(veyra_capture_format_selection_tests PRIVATE cxx_std_20)+ target_include_directories(veyra_capture_format_selection_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)add_executable(veyra_presentation_worker_tests tests/unit/LiveGpuSchedulerTests.cpp)target_compile_features(veyra_presentation_worker_tests PRIVATE cxx_std_20)target_compile_definitions(veyra_presentation_worker_tests PRIVATE NOMINMAX WIN32_LEAN_AND_MEAN)diff --git a/apps/veyra/ui/CapturePanel.cpp b/apps/veyra/ui/CapturePanel.cppindex 7ce1e310..3be2e076 100644--- a/apps/veyra/ui/CapturePanel.cpp+++ b/apps/veyra/ui/CapturePanel.cpp@@ -162,7 +162,7 @@ case WM_COMMAND:const bool audioIndexValid=SendDlgItemMessageW(h,3,CB_GETCURSEL,0,0)!=CB_ERR&&(audio<0||size_t(audio)<audioDevices.size());if(!busy&&device==queriedDevice&&format>=0&&size_t(format)<formats.size()&&device>=0&&size_t(device)<videoDevices.size()&&audioIndexValid){const auto* audioDevice=audio>=0?&audioDevices[size_t(audio)]:nullptr;- const auto path=source::CaptureCardSource::makeCapturePath(unsigned(device),videoDevices[size_t(device)],formats[size_t(format)].index,audio,audioDevice,selectedColor(h),requestedFps);+ const auto path=source::CaptureCardSource::makeCapturePath(unsigned(device),videoDevices[size_t(device)],formats[size_t(format)].index,audio,audioDevice,selectedColor(h),requestedFps,formats[size_t(format)].key);if(!path.empty()){auto colors=remembered.deviceColors;colors[videoDevices[size_t(device)].path]=selectedColor(h);remembered={videoDevices[size_t(device)].path,formats[size_t(format)].key,audioDevice?audioDevice->path:L"",audioDevice?(audioDevice->wasapi?source::kCaptureAudioWasapi:0):audio,selectedColor(h),remembered.formatHintDismissed,requestedFps,std::move(colors)};diff --git a/include/veyra/source/CaptureCardSource.h b/include/veyra/source/CaptureCardSource.hindex c2a76b72..3e6f2985 100644--- a/include/veyra/source/CaptureCardSource.h+++ b/include/veyra/source/CaptureCardSource.h@@ -1,13 +1,13 @@#pragma once#include "veyra/source/IFrameSource.h"#include "veyra/source/CaptureColorOverride.h"+#include "veyra/source/CaptureFormatSelection.h"#include <memory>#include <string_view>#include <vector>#include "veyra/sink/CaptureAudioSession.h"#include "veyra/source/AudioInputRecovery.h"namespace veyra::source {-struct CaptureFormat {int index=0;unsigned width=0,height=0;double fps=0;std::wstring label;std::wstring key;int rank=0;int tier=0;};struct CaptureDevice {std::wstring name;// DirectShow moniker DevicePath/display name. This is stable across a@@ -31,7 +31,7 @@ public:static std::vector<CaptureDevice> deviceDetails(bool audio=false);static std::vector<std::wstring> devices(bool audio=false);static std::wstring makeCapturePath(unsigned videoIndex,const CaptureDevice& video,- int format,int audioMode,const CaptureDevice* audio,unsigned colorOverride=0,double requestedFps=0);+ int format,int audioMode,const CaptureDevice* audio,unsigned colorOverride=0,double requestedFps=0,std::wstring_view formatKey={});static std::vector<CaptureFormat> formats(unsigned device);static std::vector<CaptureFormat> formatsByPath(std::wstring_view devicePath);bool open(const SourceOpenDesc&)override;diff --git a/include/veyra/source/CaptureFormatSelection.h b/include/veyra/source/CaptureFormatSelection.hnew file mode 100644index 00000000..5ef4cc43--- /dev/null+++ b/include/veyra/source/CaptureFormatSelection.h@@ -0,0 +1,41 @@+#pragma once+#include "veyra/source/CaptureFrameRate.h"+#include <format>+#include <span>++namespace veyra::source {+struct CaptureFormat {int index=0;unsigned width=0,height=0;double fps=0;std::wstring label;std::wstring key;int rank=0;int tier=0;};++inline const CaptureFormat* selectCaptureFormat(std::span<const CaptureFormat> formats,int legacyIndex,std::wstring_view key){+ for(const auto& format:formats)+ if(key.empty()?format.index==legacyIndex:format.key==key)return &format;+ return nullptr;+}++inline std::wstring capturePathOptions(double fps,std::wstring_view encodedKey){+ std::wstring result;+ if(fps>0)result=std::format(L"?fps={:.6f}",fps);+ if(!encodedKey.empty())result+=std::format(L"{}format={}",result.empty()?L"?":L"&",encodedKey);+ return result;+}++inline bool parseCapturePathOptions(std::wstring_view query,double& fps,std::wstring& encodedKey){+ fps=0;encodedKey.clear();bool haveFps=false,haveKey=false;+ if(query.empty())return false;+ while(!query.empty()){+ const auto end=query.find(L'&');const auto option=query.substr(0,end);+ if(option.starts_with(L"fps=")){+ if(haveFps||!parseCaptureFrameRate(option.substr(4),fps))return false;+ haveFps=true;+ }else if(option.starts_with(L"format=")){+ const auto key=option.substr(7);+ if(haveKey||key.empty()||key.size()>4096||key.size()%4||key.find_first_not_of(L"0123456789abcdefABCDEF")!=std::wstring_view::npos)return false;+ haveKey=true;encodedKey=key;+ }else return false;+ if(end==std::wstring_view::npos)break;+ query.remove_prefix(end+1);+ if(query.empty())return false;+ }+ return true;+}+}diff --git a/src/source/CaptureCardSource.cpp b/src/source/CaptureCardSource.cppindex 03cf39b6..a15a2f40 100644--- a/src/source/CaptureCardSource.cpp+++ b/src/source/CaptureCardSource.cpp@@ -199,12 +199,12 @@ bool parseInt(std::wstring_view text,int& value){if(text.empty())return false;const std::wstring copy(text);size_t consumed=0;try{value=std::stoi(copy,&consumed);}catch(...){return false;}return consumed==copy.size();}bool parseUnsigned(std::wstring_view text,unsigned& value){int parsed=0;if(!parseInt(text,parsed)||parsed<0)return false;value=static_cast<unsigned>(parsed);return true;}-struct CaptureSelection {unsigned videoIndex=0;int format=0;int audio=kCaptureAudioDisabled;unsigned colorOverride=0;double requestedFps=0;bool stable=false;std::wstring videoPath,audioPath;};+struct CaptureSelection {unsigned videoIndex=0;int format=0;int audio=kCaptureAudioDisabled;unsigned colorOverride=0;double requestedFps=0;bool stable=false;std::wstring videoPath,audioPath,formatKey;};bool parseCapturePath(std::wstring_view path,CaptureSelection& selection){selection={};if(const auto query=path.find(L'?');query!=std::wstring_view::npos){- const auto value=path.substr(query+1);- if(!value.starts_with(L"fps=")||!parseCaptureFrameRate(value.substr(4),selection.requestedFps))return false;+ std::wstring encodedKey;+ if(!parseCapturePathOptions(path.substr(query+1),selection.requestedFps,encodedKey)||!decodePath(encodedKey,selection.formatKey))return false;path=path.substr(0,query);}constexpr std::wstring_view prefix=L"capture2:";@@ -481,9 +481,9 @@ std::vector<CaptureDevice> CaptureCardSource::deviceDetails(bool audio){return result;}std::vector<std::wstring> CaptureCardSource::devices(bool audio){std::vector<std::wstring> result;for(auto& device:deviceDetails(audio))result.push_back(std::move(device.name));return result;}-std::wstring CaptureCardSource::makeCapturePath(unsigned videoIndex,const CaptureDevice& video,int format,int audioMode,const CaptureDevice* audio,unsigned colorOverride,double requestedFps){+std::wstring CaptureCardSource::makeCapturePath(unsigned videoIndex,const CaptureDevice& video,int format,int audioMode,const CaptureDevice* audio,unsigned colorOverride,double requestedFps,std::wstring_view formatKey){if(!validCaptureFrameRate(requestedFps)||!validCaptureColorOverride(colorOverride))return {};- const auto suffix=requestedFps>0?std::format(L"?fps={:.6f}",requestedFps):L"";+ const auto suffix=capturePathOptions(requestedFps,encodePath(formatKey));if(audio&&audio->wasapi){if(video.path.empty()||audio->path.empty())return {};return std::format(L"capture2:{}:{}:{}:{}:{}{}",encodePath(video.path),format,kCaptureAudioWasapi,encodePath(audio->path),colorOverride,suffix);@@ -562,8 +562,16 @@ void CaptureCardSource::videoReset(bool resetAudio){if(p_->wasapi)p_->wasapi->vivoid CaptureCardSource::setAudioSync(unsigned mode,int offset){if(p_->wasapi)p_->wasapi->setSync(mode,offset);else if(p_->audioSession)p_->audioSession->setSync(mode,offset);}sink::CaptureAudioState CaptureCardSource::audioState()const{auto state=p_->wasapi?p_->wasapi->snapshot():p_->audioSession?p_->audioSession->snapshot():sink::CaptureAudioState{};if(!p_->audioError.empty())state.error=p_->audioError;return state;}bool CaptureCardSource::open(const SourceOpenDesc& desc){return configure(desc)&&start();}-bool CaptureCardSource::configure(const SourceOpenDesc& desc){close();error_.clear();p_->lastAudioGain=-1;auto& p=*p_;CaptureSelection selection;if(!parseCapturePath(desc.path,selection))return false;const unsigned index=selection.videoIndex;const int format=selection.format;const int audio=selection.audio;+bool CaptureCardSource::configure(const SourceOpenDesc& desc){close();error_.clear();p_->lastAudioGain=-1;auto& p=*p_;CaptureSelection selection;if(!parseCapturePath(desc.path,selection))return false;const unsigned index=selection.videoIndex;int format=selection.format;const int audio=selection.audio;if(selection.stable?!configuration(selection.videoPath,p.graph,p.builder,p.device,p.config):!configuration(index,p.graph,p.builder,p.device,p.config))return false;+ const auto available=enumerateFormats(p.config.Get());+ const auto* chosen=selectCaptureFormat(available,format,selection.formatKey);+ if(!selection.formatKey.empty()&&!chosen){error_=L"保存的采集格式已不可用,请重新选择分辨率、帧率和像素格式。";return false;}+ if(chosen)format=chosen->index;+ // Capture the advertised identity before SetFormat: some drivers mutate+ // their capability list to reflect the last negotiated rate/orientation.+ const std::wstring selectedKey=chosen?chosen->key:L"";+ const double expectedFps=selection.requestedFps>0?selection.requestedFps:chosen?chosen->fps:0;int count=0,size=0;if(FAILED(p.config->GetNumberOfCapabilities(&count,&size))||format<0||format>=count||size<=0||size>65536)return false;std::vector<BYTE> caps(size);AM_MEDIA_TYPE* native=nullptr;if(FAILED(p.config->GetStreamCaps(format,&native,caps.data())))return false;if(selection.requestedFps>0){@@ -604,6 +612,11 @@ bool CaptureCardSource::configure(const SourceOpenDesc& desc){close();error_.cle}}}+ if(native->subtype!=requestedSubtype){+ log::error("capture",std::format("Driver changed requested subtype 0x{:08X} to 0x{:08X}",requestedSubtype.Data1,native->subtype.Data1));+ error_=L"采集卡返回的像素格式与所选格式不符,请重新选择采集格式。";+ freeType(native);return false;+ }const bool direct=nativeSupported&&!desc.legacyCaptureRgbForDiagnostic;const CaptureCodec codec=captureCodecOf(native->subtype);unsigned compressedWidth=0,compressedHeight=0;int64_t compressedDuration=0;@@ -725,10 +738,13 @@ bool CaptureCardSource::configure(const SourceOpenDesc& desc){close();error_.clelog::info("capture-color",std::format("manual space={} range={} effective transfer={} matrix={} primaries={} range={} (0=auto, space 1=PQ 2=HLG 3=709, range 1=limited 2=full)",space,captureColorRange(colorOverride),int(p.layout.color.transfer),int(p.layout.color.matrix),int(p.layout.color.primaries),int(p.layout.color.range)));}p.info={};p.info.kind=pipeline::SourceKind::CaptureCard;p.info.width=p.layout.width;p.info.height=p.layout.height;p.info.averageFps=p.layout.duration>0?1e7/p.layout.duration:0;p.info.duration=pipeline::Rational::unknown();p.info.color=p.layout.color;- if(selection.requestedFps>0){- const bool accepted=captureFrameRateMatches(selection.requestedFps,p.layout.duration);- log::info("capture-rate",std::format("requestedFps={:.6f} connectedFps={:.6f} accepted={} softwareLimiter=0",selection.requestedFps,p.info.averageFps,accepted));- if(!accepted){error_=std::format(L"请求 {:.3f} FPS,但采集卡返回 {:.3f} FPS;请改用支持的帧率,或填 0。",selection.requestedFps,p.info.averageFps);return false;}+ if(expectedFps>0){+ const bool accepted=captureFrameRateMatches(expectedFps,p.layout.duration);+ log::info("capture-rate",std::format("requestedFps={:.6f} connectedFps={:.6f} accepted={} softwareLimiter=0",expectedFps,p.info.averageFps,accepted));+ if(!accepted){error_=std::format(L"所选格式为 {:.3f} FPS,但采集卡返回 {:.3f} FPS;请重新选择设备支持的格式。",expectedFps,p.info.averageFps);return false;}+ }+ if(chosen&&(p.info.width!=chosen->width||p.info.height!=chosen->height)){+ error_=L"采集卡返回的分辨率与所选格式不符,请重新选择采集格式。";return false;}if(!compressedPath&&!colorOverride&&(p.layout.format==AV_PIX_FMT_P010||p.layout.format==AV_PIX_FMT_P016)&&p.info.color.transferAssumed)log::warn("capture-color","No explicit HDR transfer from driver; bit depth does not identify HDR. Keeping SDR fallback; manual PQ/HLG remains available.");@@ -794,8 +810,7 @@ bool CaptureCardSource::configure(const SourceOpenDesc& desc){close();error_.cle}p.elgatoHdr=std::move(elgatoHdr);p.configured=true;- reconnectDesc_=desc;reconnectInfo_=p.info;reconnectFormat_.clear();- for(const auto& candidate:enumerateFormats(p.config.Get()))if(candidate.index==format){reconnectFormat_=candidate.key;break;}+ reconnectDesc_=desc;reconnectInfo_=p.info;reconnectFormat_=selectedKey;// Log the upstream type after DirectShow has finished negotiation. The// RGB32 output's nominal FPS alone is not proof of actual callback cadence.AM_MEDIA_TYPE* actual=nullptr;const auto formatHr=p.config->GetFormat(&actual);@@ -1161,10 +1176,10 @@ bool CaptureCardSource::reconnect(float gain,unsigned syncMode,int offsetMs){int format=-1;for(const auto& candidate:formatsByPath(selection.videoPath))if(candidate.key==key){format=candidate.index;break;}if(format<0)return false;auto reopen=desc;reopen.path=std::format(L"capture2:{}:{}:{}:{}:{}",encodePath(selection.videoPath),format,selection.audio,encodePath(selection.audioPath),selection.colorOverride);- if(selection.requestedFps>0)reopen.path+=std::format(L"?fps={:.6f}",selection.requestedFps);+ reopen.path+=capturePathOptions(selection.requestedFps,encodePath(key));bool ok=configure(reopen);if(ok){const auto& current=p_->info;- ok=current.width==expected.width&¤t.height==expected.height&¤t.color.pixelFormat==expected.color.pixelFormat&&+ ok=current.width==expected.width&¤t.height==expected.height&&captureFrameRateMatches(expected.averageFps,p_->layout.duration)&¤t.color.pixelFormat==expected.color.pixelFormat&¤t.color.transfer==expected.color.transfer&¤t.color.matrix==expected.color.matrix&¤t.color.primaries==expected.color.primaries&¤t.color.range==expected.color.range&¤t.color.displayReferred709==expected.color.displayReferred709&¤t.color.preserveSdrCodeValues==expected.color.preserveSdrCodeValues;if(!ok)log::warn("capture-reconnect","Negotiated input contract changed; explicit reselection required");diff --git a/tests/integration/CaptureSourceTests.cpp b/tests/integration/CaptureSourceTests.cppindex 1c0b0efc..8ab30797 100644--- a/tests/integration/CaptureSourceTests.cpp+++ b/tests/integration/CaptureSourceTests.cpp@@ -22,7 +22,14 @@ int wmain(int argc,wchar_t** argv){unsigned device=0;int format=0,audio=0;if(swscanf_s(argv[2],L"capture:%u:%d:%d",&device,&format,&audio)!=3||audio!=-1)return 2;const auto devices=veyra::source::CaptureCardSource::deviceDetails();if(device>=devices.size())return 2;- desc.path=veyra::source::CaptureCardSource::makeCapturePath(device,devices[device],format,audio,nullptr,0,expected);+ const auto formats=veyra::source::CaptureCardSource::formatsByPath(devices[device].path);+ const auto* selected=veyra::source::selectCaptureFormat(formats,format,L"");+ if(!selected)return 2;+ // An obsolete ordinal must not override the persisted format identity.+ desc.path=veyra::source::CaptureCardSource::makeCapturePath(device,devices[device],999999,audio,nullptr,0,expected,selected->key);+ auto missing=desc;+ missing.path=veyra::source::CaptureCardSource::makeCapturePath(device,devices[device],format,audio,nullptr,0,expected,L"missing-test-format");+ if(source.configure(missing)){printf("FAIL missing format silently accepted\n");return 1;}}if(!source.configure(desc)){wprintf(L"RATE_REJECTED %ls\n",source.errorMessage().c_str());return 3;}if(!source.start())return 4;diff --git a/tests/unit/CaptureFormatSelectionTests.cpp b/tests/unit/CaptureFormatSelectionTests.cppnew file mode 100644index 00000000..0b63b198--- /dev/null+++ b/tests/unit/CaptureFormatSelectionTests.cpp@@ -0,0 +1,27 @@+#include "veyra/source/CaptureFormatSelection.h"+#include <array>+#include <iostream>++int main(){+ using namespace veyra::source;+ int failures=0;+ auto check=[&](bool ok,const char* name){if(!ok){++failures;std::cerr<<"FAIL "<<name<<'\n';}};+ std::array<CaptureFormat,3> formats{{{7,3840,2160,60,L"",L"4k60-p010"},{2,1920,1080,30,L"",L"1080p30-nv12"},{0,3840,2160,30,L"",L"4k30-p010"}}};+ check(selectCaptureFormat(formats,2,L"4k60-p010")==&formats[0],"stable key overrides stale index");+ check(!selectCaptureFormat(formats,2,L"missing"),"missing key never falls back to another format");+ check(selectCaptureFormat(formats,2,L"")==&formats[1],"legacy request retains index semantics");+ check(!selectCaptureFormat(formats,9,L""),"invalid legacy index rejected");+ double fps=0;std::wstring key;+ for(double requested:{0.,29.97,40.,60.}){+ const auto query=capturePathOptions(requested,L"0034004B");+ check(parseCapturePathOptions(std::wstring_view(query).substr(1),fps,key)&&fps==requested&&key==L"0034004B","options roundtrip");+ }+ check(parseCapturePathOptions(L"fps=50",fps,key)&&fps==50&&key.empty(),"old fps-only URI supported");+ check(parseCapturePathOptions(L"format=0041&fps=30",fps,key)&&fps==30,"option order independent");+ for(auto bad:{L"",L"format=",L"format=XYZW",L"format=000",L"fps=30&fps=60",L"format=0041&format=0042",L"fps=60&",L"unknown=1",L"fps=nan"})+ check(!parseCapturePathOptions(bad,fps,key),"malformed options rejected");+ check(captureFrameRateMatches(60,166833),"nominal 59.94 allowed");+ check(!captureFrameRateMatches(60,200000),"60 to 50 renegotiation rejected");+ check(!captureFrameRateMatches(60,0),"unknown negotiated rate rejected");+ std::cout<<"capture format selection failures="<<failures<<'\n';return failures?1:0;+}