Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ qt_add_executable(DigitalImageAnalyzer
mainwindow.cpp
nallistmodel.h
nallistmodel.cpp
framecache.h
framecache.cpp

${DIA_APP_ICON_SOURCES}

Expand Down Expand Up @@ -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
Expand Down
64 changes: 64 additions & 0 deletions framecache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "framecache.h"

#include <utility>

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);
}
}
}
46 changes: 46 additions & 0 deletions framecache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef DIA_FRAMECACHE_H_
#define DIA_FRAMECACHE_H_

#include <QHash>
#include <QImage>
#include <cstddef>
#include <list>

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<int>::iterator lru;
};

void Evict();

QHash<int, Entry> entries_;
std::list<int> 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_
27 changes: 26 additions & 1 deletion mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "bitstreamparser.h"
#include "decoder.h"
#include "framecache.h"
#include "nalanalyzer.h"

class AudioPlayer;
Expand Down Expand Up @@ -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;
Expand Down
45 changes: 45 additions & 0 deletions test/test_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <string>

#include "bitstreamparser.h"
#include "framecache.h"
#include "mainwindow.h"
#include "nalanalyzer.h"
#include "nallistmodel.h"
Expand Down Expand Up @@ -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<QPlainTextEdit*>("perfLog");
ASSERT_NE(log, nullptr);
EXPECT_TRUE(log->toPlainText().contains("cache"))
<< log->toPlainText().toStdString();

QSettings().setValue("perfLogVisible", false);
}
Loading