From 6d2cb1995191d4c34c98bf609c299a764ccf0f0e Mon Sep 17 00:00:00 2001 From: blocksmith314 Date: Sat, 21 Nov 2026 18:06:09 +0800 Subject: [PATCH] feat(ui): cache recently decoded frames for instant re-display Add a bounded LRU FrameCache (256 MB default) keyed by frame index. ShowFrame stores the full-resolution image; ShowFrameAt checks the cache first when not playing and displays the cached frame without touching the decoder. Because a cache hit leaves the decoder at an older position, a resync flag forces SeekToFrame() before the next decode so the displayed frame and the decoder cannot diverge. Opening a file clears the cache, and the performance log records cache hits. Adds FrameCache eviction/LRU tests and a GUI test that revisits a cached frame. --- CMakeLists.txt | 3 +++ framecache.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++++ framecache.h | 46 ++++++++++++++++++++++++++++++++++ mainwindow.cpp | 27 +++++++++++++++++++- mainwindow.h | 3 +++ test/test_gui.cpp | 45 +++++++++++++++++++++++++++++++++ 6 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 framecache.cpp create mode 100644 framecache.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 67507f3..3a216c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,6 +57,8 @@ qt_add_executable(DigitalImageAnalyzer mainwindow.cpp nallistmodel.h nallistmodel.cpp + framecache.h + framecache.cpp ${DIA_APP_ICON_SOURCES} @@ -328,6 +330,7 @@ if (BUILD_TEST) add_executable(test_gui test/test_gui.cpp mainwindow.cpp nallistmodel.cpp + framecache.cpp ${DIA_CORE_SOURCES} ffmpeg/decoder.cpp ffmpeg/annexb.cpp diff --git a/framecache.cpp b/framecache.cpp new file mode 100644 index 0000000..1949ded --- /dev/null +++ b/framecache.cpp @@ -0,0 +1,64 @@ +#include "framecache.h" + +#include + +FrameCache::FrameCache(std::size_t limit_bytes) : limit_bytes_(limit_bytes) {} + +void FrameCache::SetLimitBytes(std::size_t limit_bytes) { + limit_bytes_ = limit_bytes; + Evict(); +} + +void FrameCache::Clear() { + entries_.clear(); + lru_.clear(); + bytes_ = 0; +} + +const CachedFrame* FrameCache::Find(int index) { + auto it = entries_.find(index); + if (it == entries_.end()) { + misses_++; + return nullptr; + } + hits_++; + lru_.erase(it->lru); + lru_.push_front(index); + it->lru = lru_.begin(); + return &it->frame; +} + +void FrameCache::Insert(int index, const QImage& image, double pts_sec) { + if (image.isNull()) { + return; + } + const std::size_t image_bytes = (std::size_t)image.sizeInBytes(); + auto it = entries_.find(index); + if (it != entries_.end()) { + bytes_ -= (std::size_t)it->frame.image.sizeInBytes(); + lru_.erase(it->lru); + entries_.erase(it); + } + + Entry entry; + entry.frame.index = index; + entry.frame.image = image; + entry.frame.pts_sec = pts_sec; + lru_.push_front(index); + entry.lru = lru_.begin(); + entries_.insert(index, entry); + bytes_ += image_bytes; + Evict(); +} + +void FrameCache::Evict() { + while (bytes_ > limit_bytes_ && !lru_.empty()) { + const int oldest = lru_.back(); + lru_.pop_back(); + auto it = entries_.find(oldest); + if (it != entries_.end()) { + bytes_ -= (std::size_t)it->frame.image.sizeInBytes(); + entries_.erase(it); + } + } +} diff --git a/framecache.h b/framecache.h new file mode 100644 index 0000000..adc3cd3 --- /dev/null +++ b/framecache.h @@ -0,0 +1,46 @@ +#ifndef DIA_FRAMECACHE_H_ +#define DIA_FRAMECACHE_H_ + +#include +#include +#include +#include + +struct CachedFrame { + int index = 0; + QImage image; + double pts_sec = 0.0; +}; + +// Bounded LRU cache of decoded frames. Insert/Find are cheap; Find updates the +// recency order and returns a pointer valid until the next Insert/Clear. +class FrameCache { + public: + explicit FrameCache(std::size_t limit_bytes = 256ull * 1024 * 1024); + + void SetLimitBytes(std::size_t limit_bytes); + void Clear(); + const CachedFrame* Find(int index); + void Insert(int index, const QImage& image, double pts_sec); + + std::size_t CurrentBytes() const { return bytes_; } + std::size_t HitCount() const { return hits_; } + std::size_t MissCount() const { return misses_; } + + private: + struct Entry { + CachedFrame frame; + std::list::iterator lru; + }; + + void Evict(); + + QHash entries_; + std::list lru_; // front = most recently used + std::size_t limit_bytes_; + std::size_t bytes_ = 0; + std::size_t hits_ = 0; + std::size_t misses_ = 0; +}; + +#endif // DIA_FRAMECACHE_H_ diff --git a/mainwindow.cpp b/mainwindow.cpp index a4865c1..5db35c6 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -715,6 +715,8 @@ void MainWindow::UpdateStats() { } void MainWindow::StartPlayer() { + frame_cache_.Clear(); + display_from_cache_ = false; auto& stats = analyzer_.stats(); player_timer_->stop(); is_playing_ = false; @@ -792,6 +794,7 @@ void MainWindow::ShowFrame(const QImage& image, int frame_idx) { return; } current_image_ = image; + frame_cache_.Insert(frame_idx, image, current_pts_); QSize target = video_size_.isValid() ? video_size_ : video_label_->size(); QImage scaled = image.scaled(target, Qt::KeepAspectRatio, Qt::SmoothTransformation); @@ -860,6 +863,23 @@ void MainWindow::ShowFrameAt(int idx) { return; } + if (!is_playing_) { + if (const CachedFrame* cached = frame_cache_.Find(idx)) { + current_frame_index_ = idx; + current_pts_ = cached->pts_sec; + ShowFrame(cached->image, idx); + display_from_cache_ = true; + if (perf_log_enabled_) { + AppendPerf( + QString("cache %1 frame=%2") + .arg( + QFileInfo(QString::fromStdString(current_path_)).fileName()) + .arg(idx)); + } + return; + } + } + const bool perf = perf_log_enabled_; QElapsedTimer perf_timer; qint64 seek_ms = 0; @@ -869,9 +889,14 @@ void MainWindow::ShowFrameAt(int idx) { } // Decode sequentially when moving forward by one; otherwise seek. + // If the previous frame came from the cache, the decoder may be at a + // different position; force it to resync before decoding anything new. + const bool resync_needed = display_from_cache_; + display_from_cache_ = false; + const bool jump = (idx < current_frame_index_ || idx > current_frame_index_ + 1); - if (jump) { + if (resync_needed || jump) { if (!player_decoder_.SeekToFrame(idx)) { return; } diff --git a/mainwindow.h b/mainwindow.h index 3e217a6..d95db3c 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -20,6 +20,7 @@ #include "bitstreamparser.h" #include "decoder.h" +#include "framecache.h" #include "nalanalyzer.h" class AudioPlayer; @@ -124,6 +125,8 @@ class MainWindow : public QMainWindow { QPlainTextEdit* perf_log_ = nullptr; QAction* perf_action_ = nullptr; bool perf_log_enabled_ = false; + FrameCache frame_cache_; + bool display_from_cache_ = false; ProgressMode progress_mode_ = ProgressMode::kFrames; bool is_playing_ = false; diff --git a/test/test_gui.cpp b/test/test_gui.cpp index 1c79f58..ef9bad6 100644 --- a/test/test_gui.cpp +++ b/test/test_gui.cpp @@ -24,6 +24,7 @@ #include #include "bitstreamparser.h" +#include "framecache.h" #include "mainwindow.h" #include "nalanalyzer.h" #include "nallistmodel.h" @@ -430,3 +431,47 @@ int main(int argc, char** argv) { QApplication app(argc, argv); return RUN_ALL_TESTS(); } + +TEST(FrameCacheTest, EvictsLeastRecentlyUsed) { + FrameCache cache(900); // room for two 10x10 RGB32 frames + const QImage a(10, 10, QImage::Format_RGB32); + const QImage b(10, 10, QImage::Format_RGB32); + const QImage c(10, 10, QImage::Format_RGB32); + + cache.Insert(1, a, 0.1); + cache.Insert(2, b, 0.2); + ASSERT_NE(cache.Find(1), nullptr); // touch 1, now 2 is the oldest + cache.Insert(3, c, 0.3); + + EXPECT_EQ(cache.Find(2), nullptr); + EXPECT_NE(cache.Find(1), nullptr); + EXPECT_NE(cache.Find(3), nullptr); + EXPECT_GT(cache.HitCount(), 0u); + EXPECT_GT(cache.MissCount(), 0u); +} + +TEST(MainWindowTest, CacheServesRevisitedFrames) { + if (!exists(data_path("output.h264"))) { + GTEST_SKIP() << "H.264 fixture missing"; + } + + QSettings().setValue("progressMode", 0); + QSettings().setValue("perfLogVisible", true); + MainWindow window; + window.show(); + ASSERT_TRUE( + window.LoadFile(QString::fromStdString(data_path("output.h264")))); + QApplication::processEvents(); + + QTest::keyClick(&window, Qt::Key_Right); + QApplication::processEvents(); + QTest::keyClick(&window, Qt::Key_Left); + QApplication::processEvents(); + + auto* log = window.findChild("perfLog"); + ASSERT_NE(log, nullptr); + EXPECT_TRUE(log->toPlainText().contains("cache")) + << log->toPlainText().toStdString(); + + QSettings().setValue("perfLogVisible", false); +}