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++
7.7 KB
Raw Blame
// MediaFileSource integration tests - driven by the deterministic corpus
// (loop/local/fixed_clips/corpus/translation_1080p60.mp4, 600 frames, 60fps).
// Verifies the contract translation layer: open metadata, monotonic PTS in
// the real stream time base, honest color defaults, atomic seek protocol,
// and EOS drain. Requires the corpus (generated by veyra_clip_gen).
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
extern "C" {
#include <libavutil/frame.h>
#include <libavutil/adler32.h>
}

#include "veyra/source/MediaFileSource.h"

using namespace veyra;
using namespace veyra::source;

namespace {

int g_checks = 0;
int g_failures = 0;

void check(const char* name, bool ok) {
    ++g_checks;
    if (!ok) { ++g_failures; std::printf("  [FAIL] %s\n", name); }
    else { std::printf("  [PASS] %s\n", name); }
}

} // namespace

// A short 4K B-frame fixture exercises codec lookahead, EOS drain and a seek
// after EOS. Compare every pixel (excluding row padding) and PTS against the
// existing single-thread decoder; counts alone would miss stale/reordered data.
int threadedFileTest(const std::wstring& clip) {
    struct Signature { int64_t pts; uint32_t pixels; };
    auto signature=[](const AVFrame* f) {
        uint32_t crc=1;
        for(int plane=0;plane<3;++plane) {
            const int w=plane?f->width/2:f->width,h=plane?f->height/2:f->height;
            for(int y=0;y<h;++y) crc=av_adler32_update(crc,f->data[plane]+ptrdiff_t(y)*f->linesize[plane],w);
        }
        return Signature{f->pts,crc};
    };
    media::FFmpegDemuxer demux;
    media::FFmpegVideoDecoder decoder;
    if(!demux.open(clip)||!decoder.openSoftware(demux.videoCodecParameters(),demux.videoTimeBaseNum(),demux.videoTimeBaseDen(),1)) return 1;
    std::vector<Signature> reference;
    bool draining=false;
    for(int guard=0;guard<1000;++guard) {
        if(const AVFrame* f=decoder.receiveFrame()) {
            if(f->format!=AV_PIX_FMT_YUV420P||f->width!=3840||f->height!=2160) return 1;
            reference.push_back(signature(f));
        } else if(draining) break;
        else { bool eof=false; if(demux.readVideoPacket(eof)) { if(!decoder.sendPacket(demux.currentPacket())) return 1; }
            else { if(!eof||!decoder.sendPacket(nullptr)) return 1; draining=true; } }
    }
    check("threaded:reference-drained-24",draining&&reference.size()==24);
    if(reference.size()!=24) return 1;
    MediaFileSource src;SourceOpenDesc desc;desc.path=clip;desc.preferHardwareDecode=false;
    if(!src.open(desc)) return 1;
    for(int pass=0;pass<2;++pass) {
        if(pass) check("threaded:seek-after-EOS",src.seek(pipeline::Rational{0,1}));
        size_t count=0;bool matches=true,monotonic=true;SourceReadStatus status=SourceReadStatus::Error;
        pipeline::FramePacket packet;const AVFrame* frame=nullptr;
        while(count<100&&(status=src.read(packet,&frame))==SourceReadStatus::Frame) {
            const auto actual=signature(frame);
            matches=matches&&count<reference.size()&&actual.pts==reference[count].pts&&actual.pixels==reference[count].pixels;
            if(count&&count<reference.size()) monotonic=monotonic&&actual.pts>reference[count-1].pts;
            if(pass&&count==0) check("threaded:seek-epoch-flag",packet.sourceEpoch==src.epoch()&&(packet.flags&static_cast<uint32_t>(pipeline::FrameFlagBits::Seek))!=0);
            ++count;
        }
        check("threaded:EOS-exact-count",status==SourceReadStatus::Eos&&count==reference.size());
        check("threaded:every-pixel-and-PTS-equal",matches);
        check("threaded:PTS-monotonic",monotonic);
    }
    std::printf("threaded-file: %d checks, %d failures\n",g_checks,g_failures);
    return g_failures?1:0;
}

int main(int argc, char** argv) {
    std::wstring clip = L"loop/local/fixed_clips/corpus/translation_1080p60.mp4";
    if (argc > 1) {
        clip.assign(argv[1], argv[1] + std::strlen(argv[1]));
    }
    if(argc>2&&std::strcmp(argv[2],"--threaded")==0) return threadedFileTest(clip);

    std::printf("source-file: open + metadata\n");
    MediaFileSource src;
    SourceOpenDesc desc;
    desc.path = clip;
    desc.preferHardwareDecode = false; // contract test: software decode, no GPU needed
    if (!src.open(desc)) {
        std::printf("source-file: open FAILED (corpus missing? run veyra_clip_gen --make-corpus)\n");
        return 1;
    }
    const SourceInfo& info = src.info();
    check("open:extent", info.width == 1920 && info.height == 1080);
    check("open:kind", info.kind == pipeline::SourceKind::File);
    check("open:duration", info.duration.known && info.duration.toDouble() > 9.0 && info.duration.toDouble() < 11.0);
    check("open:fps-nominal-60", info.averageFps > 59.0 && info.averageFps < 61.0);
    // 1080p SDR H.264 with no color metadata: BT.709 limited assumed (Playbook 9).
    check("open:color-matrix-709-assumed",
        info.color.matrix == pipeline::YuvMatrix::BT709 && info.color.matrixAssumed);
    check("open:color-range-limited-assumed",
        info.color.range == pipeline::ColorRange::Limited && info.color.rangeAssumed);
    check("open:not-hdr", !info.color.isHdrPath());

    std::printf("source-file: read 90 frames (PTS monotonic, flags)\n");
    pipeline::FramePacket p;
    const AVFrame* frame = nullptr;
    bool first = true;
    double lastPts = -1.0;
    int monotonicViolations = 0;
    int frames = 0;
    for (int i = 0; i < 90; ++i) {
        const SourceReadStatus st = src.read(p, &frame);
        if (st != SourceReadStatus::Frame) { break; }
        ++frames;
        if (first) {
            check("read:first-open-flag", (p.flags & static_cast<uint32_t>(pipeline::FrameFlagBits::Open)) != 0);
            check("read:first-frame-nonnull", frame != nullptr);
            first = false;
        }
        if (p.pts.known && p.pts.toDouble() <= lastPts) { ++monotonicViolations; }
        if (p.pts.known) { lastPts = p.pts.toDouble(); }
        if (!p.duration.known) { break; }
    }
    check("read:90-frames", frames == 90);
    check("read:pts-monotonic", monotonicViolations == 0);
    check("read:duration-known", p.duration.known && p.duration.toDouble() > 0.0);
    check("read:sequence-monotonic", p.sequence == 90);

    std::printf("source-file: atomic seek to 5s\n");
    const uint64_t epochBefore = src.epoch();
    check("seek:accepted", src.seek(pipeline::Rational{5, 1}));
    const SourceReadStatus st = src.read(p, &frame);
    check("seek:frame-after", st == SourceReadStatus::Frame);
    check("seek:flag-set", (p.flags & static_cast<uint32_t>(pipeline::FrameFlagBits::Seek)) != 0);
    check("seek:epoch-advanced", src.epoch() == epochBefore + 1 && p.sourceEpoch == epochBefore + 1);
    check("seek:pts-at-or-after-target", p.pts.known && p.pts.toDouble() >= 4.9 && p.pts.toDouble() < 6.0);
    SourceReadStatus st2 = src.read(p, &frame);
    check("seek:next-frame-no-flag", st2 == SourceReadStatus::Frame
        && (p.flags & static_cast<uint32_t>(pipeline::FrameFlagBits::Seek)) == 0);

    std::printf("source-file: drain to EOS\n");
    int drained = 0;
    for (;;) {
        const SourceReadStatus st3 = src.read(p, &frame);
        if (st3 == SourceReadStatus::Eos) { break; }
        if (st3 == SourceReadStatus::Error) { break; }
        ++drained;
        if (drained > 1000) { break; } // safety
    }
    check("eos:reached", drained < 1000);
    // 90 initial + 2 post-seek + drained remainder; seek to 5s of a 10s clip
    // leaves roughly half the frames to drain.
    const uint64_t expectedTotal = 90 + 2 + static_cast<uint64_t>(drained);
    check("eos:total-accounted", src.framesRead() == expectedTotal);
    check("eos:drained-roughly-half", drained >= 200 && drained <= 500);

    src.close();
    check("close:info-cleared", !src.info().opened);

    std::printf("source-file: %d checks, %d failures\n", g_checks, g_failures);
    return g_failures == 0 ? 0 : 1;
}