Skip to content

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++

feat(capture-audio): decode Dolby/DTS carried inside a stereo PCM pin

Veyra Goal Agent75444b35ff80
diff --git a/include/veyra/source/AverMediaAudioCarrier.h b/include/veyra/source/AverMediaAudioCarrier.h
index f0243636..ebd9c2f4 100644
--- a/include/veyra/source/AverMediaAudioCarrier.h
+++ b/include/veyra/source/AverMediaAudioCarrier.h
@@ -33,6 +33,8 @@ public:
bool feed(std::span<const uint8_t>,avermedia::SampleInfo,double now);
void reset();
const VendorCarrierState& state()const{return state_;}
+ // An IEC 61937 sync was seen and the carrier has not returned to PCM.
+ bool compressed()const{return compressed_;}
private:
bool emitPcm(std::span<const uint8_t>,avermedia::SampleInfo);
bool emitBurst(std::span<const uint8_t>,size_t payloadBytes,sink::BitstreamKind);
@@ -48,4 +50,33 @@ private:
uint64_t revision_=0;
avermedia::SampleInfo input_{};
};
+// Any capture card whose audio pin is 16-bit stereo PCM. A PS5 set to Bitstream
+// (Dolby/DTS) behind a card whose USB audio has only two channels arrives as
+// IEC 61937 inside "PCM": it is decoded through the carrier above instead of
+// being played as noise, and the session returns to the card's PCM format once
+// the carrier does (4 s after the last burst). Plain PCM stays on the caller's
+// unchanged path; a sync-like pattern inside real PCM that brings no second
+// burst within kConfirmSeconds is released to the speakers again.
+class PcmCarrierRouter {
+public:
+ static constexpr double kConfirmSeconds=0.3; // E-AC-3 repeats every 128 ms
+ enum class Route { Pcm, Withheld };
+ struct Result { Route route=Route::Pcm; bool restorePcm=false; };
+ PcmCarrierRouter(uint32_t rate,AverMediaAudioCarrier::Callback decoded);
+ PcmCarrierRouter(const PcmCarrierRouter&)=delete;
+ PcmCarrierRouter& operator=(const PcmCarrierRouter&)=delete;
+ // Pcm: push these original bytes. Withheld: push nothing; decoded blocks
+ // went to the callback. restorePcm: put the session back on the PCM format
+ // before pushing. Thread-affine like the carrier.
+ Result feed(std::span<const uint8_t>,double now);
+ bool decoding()const{return decoding_;}
+ uint64_t falseStarts()const{return falseStarts_;}
+ const VendorCarrierState& state()const{return carrier_.state();}
+private:
+ AverMediaAudioCarrier carrier_;
+ avermedia::SampleInfo info_{};
+ double compressedSince_=-1;
+ uint64_t burstsAtEntry_=0,falseStarts_=0;
+ bool decoding_=false;
+};
} // namespace veyra::source
diff --git a/src/source/AverMediaAudioCarrier.cpp b/src/source/AverMediaAudioCarrier.cpp
index dcd55781..535c428a 100644
--- a/src/source/AverMediaAudioCarrier.cpp
+++ b/src/source/AverMediaAudioCarrier.cpp
@@ -1,7 +1,9 @@
#include "veyra/source/AverMediaAudioCarrier.h"
+#include "veyra/Log.h"
#include <algorithm>
#include <cmath>
#include <cstring>
+#include <format>
namespace veyra::source {
namespace {
@@ -133,4 +135,28 @@ bool AverMediaAudioCarrier::feed(std::span<const uint8_t> bytes,avermedia::Sampl
}
state_.retainedBytes=buffer_.size();return true;
}
+PcmCarrierRouter::PcmCarrierRouter(uint32_t rate,AverMediaAudioCarrier::Callback decoded)
+ :carrier_([this,decoded=std::move(decoded)](const VendorPcmBlock& block){
+ // PCM blocks are the caller's own bytes, pushed unchanged by the caller. A
+ // decoded block the session rejects is the session's business and must not
+ // reset decoding (that would let the carrier through as noise).
+ if(block.codec.empty())return true;
+ decoding_=true;if(decoded)decoded(block);return true;
+ }),info_{2,16,rate}{}
+PcmCarrierRouter::Result PcmCarrierRouter::feed(std::span<const uint8_t> bytes,double now){
+ Result result;
+ if(!carrier_.feed(bytes,info_,now)){
+ log::warn("capture-audio-carrier",std::format("carrier reset: {}",carrier_.state().error));
+ carrier_.reset();compressedSince_=-1;
+ }
+ if(carrier_.compressed()){
+ if(compressedSince_<0){compressedSince_=now;burstsAtEntry_=carrier_.state().bursts;}
+ if(carrier_.state().bursts>=burstsAtEntry_+2||now-compressedSince_<kConfirmSeconds){result.route=Route::Withheld;return result;}
+ // A sync-like pattern inside real PCM: no second burst followed it.
+ ++falseStarts_;carrier_.reset();compressedSince_=-1;
+ log::info("capture-audio-carrier",std::format("IEC 61937 sync without a following burst; playing PCM again falseStarts={}",falseStarts_));
+ }else compressedSince_=-1;
+ if(decoding_){decoding_=false;result.restorePcm=true;}
+ return result;
+}
} // namespace veyra::source
diff --git a/src/source/CaptureCardSource.cpp b/src/source/CaptureCardSource.cpp
index ce1372d2..20f3bb32 100644
--- a/src/source/CaptureCardSource.cpp
+++ b/src/source/CaptureCardSource.cpp
@@ -1633,7 +1633,40 @@ if(audioTypes.empty()&&(!allowBitstream||bitstreamTypes.empty())){log::warn("cap
if(type->formattype==FORMAT_WaveFormatEx&&type->pbFormat&&type->cbFormat>=sizeof(WAVEFORMATEX)&&sink::parseWavePcm(type->pbFormat,type->cbFormat,parsed)&&session->configure(parsed)){
auto* target=session.get();
auto probe=std::make_shared<sink::Iec61937Probe>();
- hr=createNativeAudioSink(*type,[target,probe,clock=CaptureAudioClock{},rate=parsed.wave.nAvgBytesPerSec,warned=false](IMediaSample* sample) mutable {
+ // A 16-bit stereo pin can carry Dolby/DTS as IEC 61937: a PS5 set to
+ // Bitstream behind a card whose USB audio has two channels only. That
+ // used to be played as noise; decode it (5.1 keeps the centre, where
+ // dialogue lives). PCM-only mode leaves every byte as it is.
+ struct DecodedFormat {unsigned channels=0;uint32_t rate=0,mask=0;CaptureAudioClock clock;uint64_t rejected=0;};
+ auto decodedFormat=std::make_shared<DecodedFormat>();
+ std::shared_ptr<PcmCarrierRouter> router;
+ if(allowBitstream&&parsed.layout.channels==2&&parsed.wave.wBitsPerSample==16&&!parsed.floating)
+ router=std::make_shared<PcmCarrierRouter>(parsed.wave.nSamplesPerSec,[target,decodedFormat](const VendorPcmBlock& block){
+ auto& format=*decodedFormat;
+ const bool changed=format.channels!=block.channels||format.rate!=block.rate||format.mask!=block.mask;
+ if(changed){
+ target->stop();
+ WAVEFORMATEXTENSIBLE wfx{};
+ wfx.Format.wFormatTag=WAVE_FORMAT_EXTENSIBLE;wfx.Format.nChannels=WORD(block.channels);
+ wfx.Format.nSamplesPerSec=block.rate;wfx.Format.wBitsPerSample=32;
+ wfx.Format.nBlockAlign=WORD(block.channels*sizeof(float));
+ wfx.Format.nAvgBytesPerSec=block.rate*wfx.Format.nBlockAlign;
+ wfx.Format.cbSize=sizeof(wfx)-sizeof(WAVEFORMATEX);wfx.Samples.wValidBitsPerSample=32;
+ wfx.dwChannelMask=block.mask;wfx.SubFormat=KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
+ const bool ok=target->configure(wfx.Format,sizeof(wfx))&&target->start();
+ log::info("capture-audio-carrier",std::format("decoding {} carried in PCM: channels={} mask=0x{:X} rate={} ok={}",block.codec,block.channels,block.mask,block.rate,ok));
+ if(!ok){format.channels=0;return false;}
+ format.channels=block.channels;format.rate=block.rate;format.mask=block.mask;
+ }
+ target->setInputBitstream(std::wstring(block.codec.begin(),block.codec.end()));
+ const double arrival=std::chrono::duration<double,std::milli>(std::chrono::steady_clock::now().time_since_epoch()).count();
+ const bool discontinuity=changed||block.discontinuity;
+ const double pts=format.clock.observe(std::nullopt,arrival,1000.0*double(block.frames)/block.rate,discontinuity);
+ if(target->push(block.samples,block.frames*block.channels*sizeof(float),pts,discontinuity))return true;
+ if((++format.rejected&(format.rejected-1))==0)log::warn("capture-audio-carrier",std::format("audio session rejected decoded block channels={} rejected={}",block.channels,format.rejected));
+ return false;
+ });
+ hr=createNativeAudioSink(*type,[target,probe,router,decodedFormat,pcm=parsed,clock=CaptureAudioClock{},rate=parsed.wave.nAvgBytesPerSec,warned=false](IMediaSample* sample) mutable {
BYTE* bytes=nullptr;REFERENCE_TIME begin=0,end=0;
const auto pointerHr=sample->GetPointer(&bytes);if(FAILED(pointerHr))return pointerHr;
const auto timeHr=sample->GetTime(&begin,&end);const bool timed=SUCCEEDED(timeHr);
@@ -1641,6 +1674,16 @@ if(audioTypes.empty()&&(!allowBitstream||bitstreamTypes.empty())){log::warn("cap
const double arrival=std::chrono::duration<double,std::milli>(std::chrono::steady_clock::now().time_since_epoch()).count();
const double pts=clock.observe(timed?std::optional<double>(double(begin)/10000):std::nullopt,arrival,1000.0*sample->GetActualDataLength()/rate,sample->IsDiscontinuity()==S_OK);
probe->feed(bytes,size_t(sample->GetActualDataLength()));
+ if(router){
+ const auto routed=router->feed(std::span<const uint8_t>(bytes,size_t(sample->GetActualDataLength())),arrival/1000);
+ if(routed.restorePcm){
+ target->stop();decodedFormat->channels=0;target->setInputBitstream({});
+ const bool ok=target->configure(pcm)&&target->start();
+ log::info("capture-audio-carrier",std::format("carried bitstream ended; session back on PCM ok={}",ok));
+ if(!ok)return E_FAIL;
+ }
+ if(routed.route==PcmCarrierRouter::Route::Withheld)return S_OK;
+ }
return target->push(bytes,size_t(sample->GetActualDataLength()),pts,sample->IsDiscontinuity()==S_OK)?S_OK:E_FAIL;
},candidate,terminal);
if(SUCCEEDED(hr))hr=p.graph->AddFilter(candidate.Get(),L"Veyra audio PCM");
diff --git a/tests/unit/AverMediaCarrierTests.cpp b/tests/unit/AverMediaCarrierTests.cpp
index bb212de5..a103ea91 100644
--- a/tests/unit/AverMediaCarrierTests.cpp
+++ b/tests/unit/AverMediaCarrierTests.cpp
@@ -152,5 +152,40 @@ int main(){try{
require(!progress&&outputs.empty(),"continuous undecodable carrier has a bounded recovery trigger");
carrier.reset();outputs.clear();
for(unsigned n=0;n<2000;++n){std::vector<uint8_t> noise(511,0);require(carrier.feed(noise,stereo,n*.01),"continuous quiet PCM");require(carrier.state().retainedBytes<=12,"bounded persistent PCM buffer");}
- std::cout<<"PASS "<<checks<<" checks: identity, real AC3/EAC3, fragmented carriers, 6/2ch and rate changes, PCM transitions, malformed input\n";return 0;
+ {
+ // PcmCarrierRouter: any card's 16-bit stereo DirectShow pin (PS5 Bitstream
+ // behind a card with two-channel USB audio, user log 68).
+ std::vector<Output> decoded;unsigned restores=0;size_t raw=0,held=0;
+ source::PcmCarrierRouter router(48000,[&](const source::VendorPcmBlock& b){
+ decoded.push_back({b.channels,b.rate,b.mask,b.discontinuity,std::string(b.codec),{b.samples,b.samples+b.frames*b.channels}});return true;});
+ auto route=[&](std::span<const uint8_t> bytes,double now){
+ const auto r=router.feed(bytes,now);restores+=r.restorePcm;(r.route==source::PcmCarrierRouter::Route::Pcm?raw:held)+=bytes.size();};
+ double now=0;
+ for(int n=0;n<50;++n,now+=.04)route(pcm,now);
+ require(raw==50*pcm.size()&&held==0&&decoded.empty()&&!router.decoding()&&!restores,"router: plain PCM stays on the caller's path untouched");
+ raw=0;
+ for(const auto& packet:encode(AV_CODEC_ID_AC3,0x60f,48000)){
+ const auto wire=burst(packet,1);
+ for(size_t pos=0;pos<wire.size();pos+=1920){route(std::span(wire).subspan(pos,std::min<size_t>(1920,wire.size()-pos)),now);now+=.01;}
+ }
+ require(raw==0&&held>0,"router: no carried Dolby byte reaches the speakers as PCM");
+ require(!decoded.empty()&&router.decoding(),"router: carried AC3 decoded");
+ const auto& block=decoded.back();
+ require(block.channels==6&&(block.mask&0x4)&&!block.codec.empty(),"router: 5.1 decoded with a front centre channel");
+ const unsigned centre=unsigned(std::popcount(block.mask&0x3u)); // channels precede in speaker-mask order
+ double centreEnergy=0;for(size_t i=centre;i<block.pcm.size();i+=block.channels)centreEnergy+=block.pcm[i]*block.pcm[i];
+ require(centreEnergy>0.001,"router: the centre channel (dialogue) carries its signal");
+ held=0;size_t restoredAt=0;
+ for(int n=0;n<150;++n,now+=.04){route(pcm,now);if(restores&&!restoredAt)restoredAt=size_t(n)+1;}
+ require(restores==1&&!router.decoding(),"router: session returns to PCM once");
+ std::cout<<"ROUTER decodedBlocks="<<decoded.size()<<" mask=0x"<<std::hex<<block.mask<<std::dec<<" pcmReturnAfter="<<restoredAt*0.04<<"s\n";
+ require(restoredAt*0.04>=4.0&&restoredAt*0.04<4.5,"router: PCM returns about 4 s after the last burst");
+ const size_t decodedBefore=decoded.size();raw=0;held=0;
+ std::vector<uint8_t> fake=pcm;fake[64]=0x72;fake[65]=0xf8;fake[66]=0x1f;fake[67]=0x4e;fake[68]=1;fake[69]=0;fake[70]=64;fake[71]=0;
+ route(fake,now);now+=.04;
+ for(int n=0;n<20;++n,now+=.04)route(pcm,now);
+ require(router.falseStarts()==1&&decoded.size()==decodedBefore&&restores==1,"router: a sync-like pattern inside PCM is released");
+ require(held<=std::max<size_t>(8,size_t(source::PcmCarrierRouter::kConfirmSeconds/.04+1))*pcm.size()&&raw>=10*pcm.size(),"router: at most the confirmation window is withheld");
+ }
+ std::cout<<"PASS "<<checks<<" checks: identity, real AC3/EAC3, fragmented carriers, 6/2ch and rate changes, PCM transitions, malformed input, generic PCM router\n";return 0;
}catch(const std::exception& e){std::cerr<<"FAIL "<<e.what()<<" after "<<checks<<" checks\n";return 1;}}