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
53 changes: 0 additions & 53 deletions decoderservice.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "decoderservice.h"

#include <cstdlib>
#include <vector>

DecoderService::DecoderService(QObject* parent) : QObject(parent) {}
Expand Down Expand Up @@ -76,57 +75,5 @@ QVariantMap DecoderService::DecodeNext() {
result["ok"] = true;
result["image"] = frame.image;
result["pts"] = frame.pts_sec;
cache_.Insert(decoder_->CurrentFrameIndex() - 1, frame.image, frame.pts_sec);
return result;
}

void DecoderService::RequestSeek(int frame, quint64 generation) {
QVariantMap result;
result["generation"] = generation;
result["target"] = frame;
if (!decoder_) {
result["ok"] = false;
emit SeekFailed(result);
return;
}

if (const CachedFrame* hit = cache_.Find(frame)) {
result["ok"] = true;
result["from_cache"] = true;
result["image"] = hit->image;
result["pts"] = hit->pts_sec;
emit FrameReady(result);
return;
}

if (const CachedFrame* nearest = cache_.Nearest(frame)) {
QVariantMap preview;
preview["generation"] = generation;
preview["target"] = frame;
preview["image"] = nearest->image;
preview["from_frame"] = nearest->index;
emit PreviewReady(preview);
}

if (!decoder_->SeekToFrame(frame)) {
result["ok"] = false;
emit SeekFailed(result);
return;
}
DecodedFrame decoded;
if (!decoder_->DecodeNextFrame(decoded)) {
result["ok"] = false;
emit SeekFailed(result);
return;
}
const FFmpegDecoder::SeekStats stats = decoder_->LastSeekStats();
cache_.Insert(frame, decoded.image, decoded.pts_sec);
result["ok"] = true;
result["from_cache"] = false;
result["image"] = decoded.image;
result["pts"] = decoded.pts_sec;
result["frames_decoded"] = stats.frames_decoded;
result["seek_io_ms"] = (qint64)stats.seek_io_ms;
result["decode_ms"] = (qint64)stats.decode_ms;
emit FrameReady(result);
}
8 changes: 0 additions & 8 deletions decoderservice.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <memory>

#include "decoder.h"
#include "framecache.h"

// Owns the FFmpegDecoder and lives in a worker thread. Callers use blocking
// invocations for now; asynchronous seek/preview will be layered on top.
Expand All @@ -22,16 +21,9 @@ class DecoderService : public QObject {
QVariantMap Close();
QVariantMap Seek(int frame);
QVariantMap DecodeNext();
void RequestSeek(int frame, quint64 generation);

signals:
void PreviewReady(QVariantMap result);
void FrameReady(QVariantMap result);
void SeekFailed(QVariantMap result);

private:
std::unique_ptr<FFmpegDecoder> decoder_;
FrameCache cache_;
};

#endif // DIA_DECODERSERVICE_H_
14 changes: 0 additions & 14 deletions framecache.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "framecache.h"

#include <cstdlib>
#include <utility>

FrameCache::FrameCache(std::size_t limit_bytes) : limit_bytes_(limit_bytes) {}
Expand All @@ -16,19 +15,6 @@ void FrameCache::Clear() {
bytes_ = 0;
}

const CachedFrame* FrameCache::Nearest(int index) const {
const CachedFrame* best = nullptr;
int best_distance = 0;
for (auto it = entries_.begin(); it != entries_.end(); ++it) {
const int distance = std::abs(it.key() - index);
if (best == nullptr || distance < best_distance) {
best = &it->frame;
best_distance = distance;
}
}
return best;
}

const CachedFrame* FrameCache::Find(int index) {
auto it = entries_.find(index);
if (it == entries_.end()) {
Expand Down
1 change: 0 additions & 1 deletion framecache.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class FrameCache {
void SetLimitBytes(std::size_t limit_bytes);
void Clear();
const CachedFrame* Find(int index);
const CachedFrame* Nearest(int index) const;
void Insert(int index, const QImage& image, double pts_sec);

std::size_t CurrentBytes() const { return bytes_; }
Expand Down
167 changes: 76 additions & 91 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,6 @@ MainWindow::MainWindow(QWidget* parent)
connect(&decode_thread_, &QThread::finished, decoder_service_,
&QObject::deleteLater);
decode_thread_.start();
connect(decoder_service_, &DecoderService::PreviewReady, this,
&MainWindow::OnSeekPreview, Qt::QueuedConnection);
connect(decoder_service_, &DecoderService::FrameReady, this,
&MainWindow::OnSeekFrameReady, Qt::QueuedConnection);
connect(decoder_service_, &DecoderService::SeekFailed, this,
&MainWindow::OnSeekFailed, Qt::QueuedConnection);
SetupUi();
}

Expand Down Expand Up @@ -763,6 +757,8 @@ QVariantMap MainWindow::DecodeFrame() {
}

void MainWindow::StartPlayer() {
frame_cache_.Clear();
display_from_cache_ = false;
auto& stats = analyzer_.stats();
player_timer_->stop();
is_playing_ = false;
Expand Down Expand Up @@ -833,14 +829,15 @@ void MainWindow::StartPlayer() {

video_size_ = video_label_->size();
current_frame_index_ = -1;
ShowFirstFrameBlocking();
ShowFrameAt(0);
}

void MainWindow::ShowFrame(const QImage& image, int frame_idx) {
if (image.isNull()) {
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 @@ -909,89 +906,90 @@ void MainWindow::ShowFrameAt(int idx) {
return;
}

pending_seek_frame_ = idx;
pending_seek_jump_ =
(idx < current_frame_index_ || idx > current_frame_index_ + 1);
seek_pending_ = true;
const quint64 generation = ++seek_generation_;
if (perf_log_enabled_) {
seek_request_timer_.start();
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;
}
}
QMetaObject::invokeMethod(decoder_service_, "RequestSeek",
Qt::QueuedConnection, Q_ARG(int, idx),
Q_ARG(quint64, generation));
}

void MainWindow::ShowFirstFrameBlocking() {
if (total_frames_ <= 0 || !decoder_service_) {
return;
}
const QVariantMap seek_result = SeekDecoder(0);
if (!seek_result.value("ok").toBool()) {
return;
}
const QVariantMap decode_result = DecodeFrame();
if (!decode_result.value("ok").toBool()) {
return;
const bool perf = perf_log_enabled_;
QElapsedTimer perf_timer;
qint64 seek_ms = 0;
qint64 decode_ms = 0;
if (perf) {
perf_timer.start();
}
current_frame_index_ = 0;
current_pts_ = decode_result.value("pts").toDouble();
ShowFrame(decode_result.value("image").value<QImage>(), 0);
}

void MainWindow::OnSeekPreview(const QVariantMap& result) {
if (result.value("generation").toULongLong() != seek_generation_) {
return;
}
const QImage image = result.value("image").value<QImage>();
if (image.isNull() || !video_label_) {
return;
// 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 (resync_needed || jump) {
const QVariantMap seek_result = SeekDecoder(idx);
if (!seek_result.value("ok").toBool()) {
return;
}
last_seek_stats_.used_container =
seek_result.value("used_container").toBool();
last_seek_stats_.used_raw_keyframe =
seek_result.value("used_raw_keyframe").toBool();
last_seek_stats_.frames_decoded =
seek_result.value("frames_decoded").toInt();
last_seek_stats_.seek_io_ms = seek_result.value("seek_io_ms").toLongLong();
last_seek_stats_.decode_ms = seek_result.value("decode_ms").toLongLong();
if (perf) {
seek_ms = perf_timer.restart();
}
}
QSize target = video_size_.isValid() ? video_size_ : video_label_->size();
video_label_->setPixmap(QPixmap::fromImage(
image.scaled(target, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
}

void MainWindow::OnSeekFrameReady(const QVariantMap& result) {
if (result.value("generation").toULongLong() != seek_generation_) {
const QVariantMap decode_result = DecodeFrame();
if (!decode_result.value("ok").toBool()) {
// End of decodable frames (container may have fewer than the NAL count).
player_timer_->stop();
is_playing_ = false;
play_btn_->setText("Play");
return;
}
const int idx = result.value("target").toInt();
const QImage image = result.value("image").value<QImage>();
if (image.isNull()) {
seek_pending_ = false;
return;
if (perf) {
decode_ms = perf_timer.restart();
}

const bool from_cache = result.value("from_cache").toBool();
const qint64 total_ms =
seek_request_timer_.isValid() ? seek_request_timer_.elapsed() : 0;
seek_pending_ = false;
current_frame_index_ = idx;
current_pts_ = result.value("pts").toDouble();
ShowFrame(image, idx);

if (perf_log_enabled_) {
if (from_cache) {
AppendPerf(
QString("cache %1 frame=%2")
.arg(QFileInfo(QString::fromStdString(current_path_)).fileName())
.arg(idx));
} else {
AppendPerf(
QString("%1 %2 frame=%3 total=%4ms seek_io=%5ms seek_decode=%6ms "
"seek_frames=%7")
.arg(pending_seek_jump_ ? "seek " : "step ")
.arg(QFileInfo(QString::fromStdString(current_path_)).fileName())
.arg(idx)
.arg(total_ms)
.arg(result.value("seek_io_ms").toLongLong())
.arg(result.value("decode_ms").toLongLong())
.arg(result.value("frames_decoded").toInt()));
}
current_pts_ = decode_result.value("pts").toDouble();
ShowFrame(decode_result.value("image").value<QImage>(), idx);
if (perf) {
const qint64 display_ms = perf_timer.elapsed();
const auto seek_stats =
jump ? last_seek_stats_ : FFmpegDecoder::SeekStats{};
AppendPerf(
QString("%1 %2 frame=%3 total=%4ms seek=%5 decode=%6 display=%7 "
"seek_frames=%8 seek_io=%9ms")
.arg(jump ? "seek " : "step ")
.arg(QFileInfo(QString::fromStdString(current_path_)).fileName())
.arg(idx)
.arg(seek_ms + decode_ms + display_ms)
.arg(seek_ms)
.arg(decode_ms)
.arg(display_ms)
.arg(seek_stats.frames_decoded)
.arg(seek_stats.seek_io_ms));
}

if (pending_seek_jump_) {
if (jump) {
has_pending_ = false;
clock_start_pts_ = current_pts_;
play_clock_.restart();
Expand All @@ -1005,16 +1003,6 @@ void MainWindow::OnSeekFrameReady(const QVariantMap& result) {
}
}

void MainWindow::OnSeekFailed(const QVariantMap& result) {
if (result.value("generation").toULongLong() != seek_generation_) {
return;
}
seek_pending_ = false;
if (is_playing_) {
StopPlayback();
}
}

void MainWindow::HighlightFrame(int frame_idx) {
nal_model_->SetHighlightedFrame(frame_idx);
if (frame_idx < 0) {
Expand Down Expand Up @@ -1136,9 +1124,6 @@ void MainWindow::OnPlayerTick() {

// Raw streams have no container timestamps: fall back to the fixed timer.
if (!has_timestamps_) {
if (seek_pending_) {
return;
}
if (current_frame_index_ + 1 >= total_frames_) {
StopPlayback();
return;
Expand Down
12 changes: 3 additions & 9 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@ class MainWindow : public QMainWindow {
void ShowAbout();
void ShowShortcuts();
void UpdatePositionLabel();
void ShowFirstFrameBlocking();
void SetPerfLogVisible(bool visible);
void OnSeekPreview(const QVariantMap& result);
void OnSeekFrameReady(const QVariantMap& result);
void OnSeekFailed(const QVariantMap& result);

private:
enum class ProgressMode { kFrames, kTime };
Expand Down Expand Up @@ -107,6 +103,7 @@ class MainWindow : public QMainWindow {
QThread decode_thread_;
bool has_timestamps_ = false;
double duration_sec_ = 0.0;
FFmpegDecoder::SeekStats last_seek_stats_;
AudioPlayer* audio_ = nullptr;
std::string current_path_;

Expand Down Expand Up @@ -140,11 +137,8 @@ class MainWindow : public QMainWindow {
QPlainTextEdit* perf_log_ = nullptr;
QAction* perf_action_ = nullptr;
bool perf_log_enabled_ = false;
quint64 seek_generation_ = 0;
int pending_seek_frame_ = -1;
bool pending_seek_jump_ = false;
bool seek_pending_ = false;
QElapsedTimer seek_request_timer_;
FrameCache frame_cache_;
bool display_from_cache_ = false;
ProgressMode progress_mode_ = ProgressMode::kFrames;

bool is_playing_ = false;
Expand Down
Loading
Loading