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

fix(capture): arm AVerMedia passthrough and decode AC-3 frames immediately

Veyra Agenta7034d67ab75
diff --git a/include/veyra/sink/BitstreamAudio.h b/include/veyra/sink/BitstreamAudio.h
index d7a092e8..d87e71a3 100644
--- a/include/veyra/sink/BitstreamAudio.h
+++ b/include/veyra/sink/BitstreamAudio.h
@@ -86,8 +86,13 @@ public:
bool open(BitstreamKind kind, bool iec61937 = false);
// Feeds raw bytes from the capture endpoint; decoded interleaved float
// samples are appended to `out`. Returns false only on a hard failure, which
- // is also stored in lastError().
+ // is also stored in lastError(). The AC-3 parser holds a complete frame
+ // until the next frame header arrives, so this path is one frame behind.
bool push(const uint8_t* data, size_t bytes, std::vector<float>& out);
+ // One complete codec frame, already split by the caller (an IEC 61937
+ // payload is one). Sent straight to the decoder, so the frame is not held
+ // for the following header.
+ bool pushFramed(const uint8_t* data, size_t bytes, std::vector<float>& out);
// Drops codec state (seek / discontinuity): the next bytes start a new
// stream, which is what happens when the console switches audio format.
void reset();
diff --git a/include/veyra/source/AverMediaAudioSwitch.h b/include/veyra/source/AverMediaAudioSwitch.h
index 7b6a7df4..368aa272 100644
--- a/include/veyra/source/AverMediaAudioSwitch.h
+++ b/include/veyra/source/AverMediaAudioSwitch.h
@@ -55,6 +55,7 @@ struct AverMediaSwitchStatus {
bool dllsLoaded = false;
bool sdkReady = false;
bool nonPcmActive = false; // what the chip reports *now* (source format)
+ bool passthroughEnabled = false; // VendorSdk::getNonPcmOnOff (the armed mode)
int chipAudioFormat = -1; // raw AT_Audio_Get_HdmiRX_AudioFormat value
std::wstring componentRoot; // folder that owns obs-plugins\64bit
std::string detail; // last result/error, for the log
@@ -95,6 +96,20 @@ public:
// underneath a still-running thread.
void stop() noexcept;
bool active() const;
+ // Re-reads the HDMI format and the passthrough flag. The format lives in
+ // the low byte (measured -236 = 0xFFFFFF14 for a bitstream).
+ bool refreshChipState();
+ // True when that low byte is 20, the non-PCM/Dolby source format.
+ bool sourceIsBitstream();
+ bool passthroughEnabled() const;
+ // VendorSdk::setNonPcmOnOff. Callers arm only when the flag is off:
+ // forcing the call while the chip already reports armed left the pin
+ // silent (2026-10-01). This never disarms.
+ bool setPassthrough(bool enable);
+ // One alignment after apply(), before the vendor graph starts. Arms
+ // passthrough when the flag is off. The OBS plugin makes the same call
+ // on every start; without it a Dolby source stays digital silence.
+ void alignPassthroughOnce();
const AverMediaSwitchStatus& status() const;
private:
diff --git a/src/sink/BitstreamAudio.cpp b/src/sink/BitstreamAudio.cpp
index 3cc18979..55572158 100644
--- a/src/sink/BitstreamAudio.cpp
+++ b/src/sink/BitstreamAudio.cpp
@@ -8,6 +8,7 @@ extern "C" {
#include <libswresample/swresample.h>
}
+#include <climits>
#include <cstring>
#include <format>
@@ -379,6 +380,24 @@ bool BitstreamDecoder::push(const uint8_t* data, size_t bytes, std::vector<float
return true;
}
+bool BitstreamDecoder::pushFramed(const uint8_t* data, size_t bytes, std::vector<float>& out) {
+ auto& p = *p_;
+ if (p.context == nullptr) return false;
+ if (data == nullptr || bytes == 0) return true;
+ if (bytes > size_t(INT_MAX)) {
+ p.lastError = "framed packet is too large";
+ return false;
+ }
+ p.bytesIn += bytes;
+ av_packet_unref(p.packet);
+ if (av_new_packet(p.packet, int(bytes)) < 0) {
+ p.lastError = "packet allocation failed";
+ return false;
+ }
+ std::memcpy(p.packet->data, data, bytes);
+ return p.decodePacket(out);
+}
+
void BitstreamDecoder::reset() {
auto& p = *p_;
p.iecPending.clear();
diff --git a/src/source/AverMediaAudioCarrier.cpp b/src/source/AverMediaAudioCarrier.cpp
index b57c7eb5..2e4bc26e 100644
--- a/src/source/AverMediaAudioCarrier.cpp
+++ b/src/source/AverMediaAudioCarrier.cpp
@@ -54,7 +54,9 @@ bool AverMediaAudioCarrier::emitBurst(std::span<const uint8_t> words,size_t payl
for(size_t i=0;i+1<words.size();i+=2){packet_[i]=words[i+1];packet_[i+1]=words[i];}
packet_.resize(payloadBytes); // Odd byte payloads include a padded carrier word.
pcm_.clear();
- if(!decoder_->push(packet_.data(),packet_.size(),pcm_)){state_.error=decoder_->lastError();return false;}
+ // One IEC payload is one codec frame. push() would keep an AC-3 frame
+ // until the next header, which is another 32 ms on every block.
+ if(!decoder_->pushFramed(packet_.data(),packet_.size(),pcm_)){state_.error=decoder_->lastError();return false;}
if(pcm_.empty()){
if(++undecodedBursts_>=64){state_.error="no decoded audio after 64 carrier bursts";return false;}
}else undecodedBursts_=0;
diff --git a/src/source/AverMediaAudioSwitch.cpp b/src/source/AverMediaAudioSwitch.cpp
index 3b8c6ed3..7cb72138 100644
--- a/src/source/AverMediaAudioSwitch.cpp
+++ b/src/source/AverMediaAudioSwitch.cpp
@@ -97,6 +97,10 @@ constexpr const char* kExportSdkInitialize = "?initialize@VendorSdk@AVerMedia@@Q
constexpr const char* kExportSdkUninitialize = "?uninitialize@VendorSdk@AVerMedia@@QEAAHXZ";
constexpr const char* kExportSdkClosePort = "?closePort@VendorSdk@AVerMedia@@QEAAHXZ";
constexpr const char* kExportSdkGetAudioFormat = "?getAudioFormat@VendorSdk@AVerMedia@@QEAAHPEAH@Z";
+// Passthrough control. Present in 1.0.15 (verified against the installed
+// avt_device_opener.dll); resolved as optional so older components still load.
+constexpr const char* kExportSdkGetNonPcmOnOff = "?getNonPcmOnOff@VendorSdk@AVerMedia@@QEAAHPEAI@Z";
+constexpr const char* kExportSdkSetNonPcmOnOff = "?setNonPcmOnOff@VendorSdk@AVerMedia@@QEAAHH@Z";
constexpr const char* kExportOpenerCtor = "??0DeviceOpener@AVerMedia@@QEAA@XZ";
constexpr const char* kExportOpenerDtor = "??1DeviceOpener@AVerMedia@@QEAA@XZ";
constexpr const char* kExportOpenerSetVendorSdk = "?SetVendorSdk@DeviceOpener@AVerMedia@@QEAAXPEAVVendorSdk@2@@Z";
@@ -254,6 +258,8 @@ struct AverMediaAudioSwitch::Impl {
RawFn uninitialize = nullptr;
RawFn closePort = nullptr;
RawFn getAudioFormat = nullptr;
+ RawFn getNonPcmOnOff = nullptr; // optional
+ RawFn setNonPcmOnOff = nullptr; // optional
RawFn openerCtor = nullptr;
RawFn openerDtor = nullptr;
RawFn setVendorSdk = nullptr;
@@ -300,6 +306,8 @@ struct AverMediaAudioSwitch::Impl {
{kExportSdkUninitialize, &exports.uninitialize},
{kExportSdkClosePort, &exports.closePort},
{kExportSdkGetAudioFormat, &exports.getAudioFormat},
+ {kExportSdkGetNonPcmOnOff, &exports.getNonPcmOnOff},
+ {kExportSdkSetNonPcmOnOff, &exports.setNonPcmOnOff},
{kExportOpenerCtor, &exports.openerCtor},
{kExportOpenerDtor, &exports.openerDtor},
{kExportOpenerSetVendorSdk, &exports.setVendorSdk},
@@ -314,7 +322,9 @@ struct AverMediaAudioSwitch::Impl {
if (address == nullptr) {
// The log sink is optional: keep working with components that
// do not export it, we just lose their internal trace.
- if (entry.target == &exports.setLogHandler) continue;
+ if (entry.target == &exports.setLogHandler ||
+ entry.target == &exports.getNonPcmOnOff ||
+ entry.target == &exports.setNonPcmOnOff) continue;
status.detail = std::format("missing export {}", entry.name);
return false;
}
@@ -599,8 +609,10 @@ bool AverMediaAudioSwitch::apply(const std::wstring& deviceName, const std::wstr
formatCall.result, formatError.empty() ? "" : " ", formatError);
return false;
}
+ // The SDK returns the format sign-extended (measured -236 = 0xFFFFFF14).
+ const int formatByte = chipFormat & 0xFF;
impl.status.detail = std::format("chipAudioFormat={}{}", chipFormat,
- chipFormat == kChipFormatNonPcm ? " (non-PCM/Dolby)" : " (PCM)");
+ formatByte == kChipFormatNonPcm ? " (non-PCM/Dolby)" : " (PCM)");
impl.status.state = AverMediaSwitchState::Switched;
impl.lastAppliedPath = devicePath;
startMonitoring();
@@ -646,6 +658,62 @@ void AverMediaAudioSwitch::stop() noexcept {
// runtime are not safe to unload while other threads may still touch them.
}
+bool AverMediaAudioSwitch::refreshChipState() {
+ if (!p_) return false;
+ auto& impl = *p_;
+ if (!impl.sdkInitialized || impl.exports.getAudioFormat == nullptr) return false;
+ std::string error;
+ int chipFormat = -1;
+ RawCall formatCall{impl.exports.getAudioFormat, impl.sdk(), &chipFormat};
+ const bool formatOk = callVendor(formatCall, error) && formatCall.result == 0;
+ if (formatOk) impl.status.chipAudioFormat = chipFormat;
+ if (impl.exports.getNonPcmOnOff != nullptr) {
+ unsigned onOff = 0;
+ RawCall onOffCall{impl.exports.getNonPcmOnOff, impl.sdk(), &onOff};
+ if (callVendor(onOffCall, error) && onOffCall.result == 0) {
+ impl.status.passthroughEnabled = onOff != 0;
+ }
+ }
+ return formatOk;
+}
+
+bool AverMediaAudioSwitch::sourceIsBitstream() {
+ return refreshChipState() && (p_->status.chipAudioFormat & 0xFF) == kChipFormatNonPcm;
+}
+
+bool AverMediaAudioSwitch::passthroughEnabled() const {
+ return p_ && p_->status.passthroughEnabled;
+}
+
+bool AverMediaAudioSwitch::setPassthrough(bool enable) {
+ if (!p_) return false;
+ auto& impl = *p_;
+ if (!impl.sdkInitialized || impl.exports.setNonPcmOnOff == nullptr) return false;
+ // The raw invoker passes void* arguments; an int parameter travels in the
+ // low 32 bits of the register on x64, which is how the component reads it.
+ RawCall call{impl.exports.setNonPcmOnOff, impl.sdk(),
+ reinterpret_cast<void*>(static_cast<intptr_t>(enable ? 1 : 0))};
+ std::string error;
+ const bool ok = callVendor(call, error) && call.result == 0;
+ log::info("capture-audio-vendor", std::format("setNonPcmOnOff({}) result={}{}",
+ enable ? 1 : 0, call.result, ok ? "" : (error.empty() ? " failed" : " failed: " + error)));
+ if (ok) impl.status.passthroughEnabled = enable;
+ return ok;
+}
+
+void AverMediaAudioSwitch::alignPassthroughOnce() {
+ if (!p_ || !p_->sdkInitialized) return;
+ const bool bitstream = sourceIsBitstream();
+ const bool armed = passthroughEnabled();
+ log::info("capture-audio-vendor", std::format(
+ "HDMI source audio format: chipFormat={} ({}); passthrough reports armed={}",
+ p_->status.chipAudioFormat, bitstream ? "bitstream" : "PCM", armed ? 1 : 0));
+ // Forcing this call when the component already reports armed left the KS
+ // pin silent (2026-10-01). Only top it up when it is actually off, and
+ // never disarm: the monitor thread treats a disarm as something to undo.
+ if (!armed) setPassthrough(true);
+}
+
void AverMediaAudioSwitch::startMonitoring() {
auto& impl = *p_;
if (!impl.sdkInitialized || impl.checking || impl.avtModule == nullptr) return;
diff --git a/src/source/AverMediaVendorAudio.cpp b/src/source/AverMediaVendorAudio.cpp
index 8d6e2b31..4015e26a 100644
--- a/src/source/AverMediaVendorAudio.cpp
+++ b/src/source/AverMediaVendorAudio.cpp
@@ -133,6 +133,7 @@ struct AverMediaVendorAudio::Impl {
// exact card. Keep its potentially slow SDK initialization on our STA.
if(!nonPcmSwitch.apply(chosen.friendlyName,chosen.devicePath))
log::warn("capture-audio-vendor",std::string("non-PCM switch unavailable; PCM capture remains possible: ")+nonPcmSwitch.status().detail);
+ else nonPcmSwitch.alignPassthroughOnce();
if(stopRequested)return false;
if(!call(Reset)){recordError("vendor ResetGraph failed");return false;}
if(!call(Update,const_cast<std::wstring*>(&chosen.friendlyName),const_cast<std::wstring*>(&chosen.devicePath))
diff --git a/src/source/CaptureCardSource.cpp b/src/source/CaptureCardSource.cpp
index 3581879e..d2278de4 100644
--- a/src/source/CaptureCardSource.cpp
+++ b/src/source/CaptureCardSource.cpp
@@ -1515,7 +1515,11 @@ bool CaptureCardSource::connectVendorAudio(const std::wstring& deviceName,const
const bool discontinuity=changed||block.discontinuity;
const double arrival=std::chrono::duration<double,std::milli>(std::chrono::steady_clock::now().time_since_epoch()).count();
const double pts=format->clock.observe(std::nullopt,arrival,1000.0*double(block.frames)/block.rate,discontinuity);
- return target->push(block.samples,block.frames*block.channels*sizeof(float),pts,discontinuity);
+ const size_t bytes=block.frames*block.channels*sizeof(float);
+ if(!target->push(block.samples,bytes,pts,discontinuity)){
+ log::warn("capture-audio-vendor",std::format("audio session rejected one block bytes={} channels={} rate={}; keeping the stream",bytes,block.channels,block.rate));
+ }
+ return true;
},preferName,preferPath);
const auto state=vendor->state();
if(!started){
diff --git a/tools/bitstream_audio_test/main.cpp b/tools/bitstream_audio_test/main.cpp
index 3530f393..e825ad94 100644
--- a/tools/bitstream_audio_test/main.cpp
+++ b/tools/bitstream_audio_test/main.cpp
@@ -39,7 +39,8 @@ constexpr double kAmplitudes[kChannels] = {0.50, 0.40, 0.30, 0.25, 0.20, 0.10};
constexpr double kTones[kChannels] = {440.0, 440.0, 440.0, 60.0, 440.0, 440.0};
// Encodes the test signal with libavcodec; returns the raw bitstream bytes.
-bool encodeTestStream(BitstreamKind kind, std::vector<uint8_t>& out, std::string& why) {
+bool encodeTestStream(BitstreamKind kind, std::vector<uint8_t>& out, std::string& why,
+ std::vector<size_t>* packetSizes = nullptr) {
const AVCodecID codecId = kind == BitstreamKind::Ac3 ? AV_CODEC_ID_AC3 : AV_CODEC_ID_EAC3;
const AVCodec* codec = avcodec_find_encoder(codecId);
if (codec == nullptr) {
@@ -120,6 +121,7 @@ bool encodeTestStream(BitstreamKind kind, std::vector<uint8_t>& out, std::string
ok = false;
break;
}
+ if (packetSizes != nullptr) packetSizes->push_back(size_t(packet->size));
out.insert(out.end(), packet->data, packet->data + packet->size);
av_packet_unref(packet);
}
@@ -129,6 +131,7 @@ bool encodeTestStream(BitstreamKind kind, std::vector<uint8_t>& out, std::string
for (;;) {
const int received = avcodec_receive_packet(context, packet);
if (received < 0) break;
+ if (packetSizes != nullptr) packetSizes->push_back(size_t(packet->size));
out.insert(out.end(), packet->data, packet->data + packet->size);
av_packet_unref(packet);
}
@@ -246,8 +249,9 @@ int main() {
bool ok = true;
for (BitstreamKind kind : {BitstreamKind::Ac3, BitstreamKind::Eac3}) {
std::vector<uint8_t> bitstream;
+ std::vector<size_t> packetSizes;
std::string why;
- if (!encodeTestStream(kind, bitstream, why)) {
+ if (!encodeTestStream(kind, bitstream, why, &packetSizes)) {
std::printf("[bitstream-test] %s encode skipped: %s\n", veyra::sink::bitstreamKindName(kind), why.c_str());
ok = false;
continue;
@@ -261,6 +265,31 @@ int main() {
ok = false;
}
if (kind == BitstreamKind::Ac3) {
+ // The parser holds a finished AC-3 frame until the next header.
+ // pushFramed must emit that frame on the same call.
+ if (packetSizes.size() >= 2 && packetSizes[0] > 0 && packetSizes[1] > 0) {
+ BitstreamDecoder direct;
+ BitstreamDecoder parsed;
+ std::vector<float> directPcm;
+ std::vector<float> parsedPcm;
+ const bool opened = direct.open(kind) && parsed.open(kind);
+ const bool first = opened
+ && direct.pushFramed(bitstream.data(), packetSizes[0], directPcm)
+ && parsed.push(bitstream.data(), packetSizes[0], parsedPcm);
+ const uint64_t parserAfterFirst = parsed.decodedFrames();
+ const bool directImmediate = first && direct.decodedFrames() >= 1536 && parserAfterFirst == 0;
+ const bool parserHeld = directImmediate
+ && parsed.push(bitstream.data() + packetSizes[0], packetSizes[1], parsedPcm)
+ && parsed.decodedFrames() >= 1536;
+ std::printf("[bitstream-test] framed-latency directFrames=%llu parserAfterFirst=%llu parserAfterSecond=%llu\n",
+ static_cast<unsigned long long>(direct.decodedFrames()),
+ static_cast<unsigned long long>(parserAfterFirst),
+ static_cast<unsigned long long>(parsed.decodedFrames()));
+ if (!parserHeld) {
+ std::printf("[bitstream-test] FAIL AC-3 framed path did not beat the parser hold\n");
+ ok = false;
+ }
+ }
const auto framed = decodeStream(toIec61937(bitstream), kind, true, 2);
report("AC-3 (IEC 61937)", framed);
if (!framed.ok || framed.channels != kChannels || !amplitudeOrderingHolds(framed)) {