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
42 changes: 39 additions & 3 deletions ffmpeg/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct FFmpegDecoder::Impl {
std::vector<FrameEntry> frame_index; // presentation order
std::vector<int> keyframes; // indices into frame_index
bool frame_index_tried = false;
std::vector<RawKeyframe> raw_keyframes;
};

FFmpegDecoder::FFmpegDecoder() : impl_(new Impl()) {}
Expand Down Expand Up @@ -162,6 +163,7 @@ bool FFmpegDecoder::OpenFile(const std::string& path, CodecType codec) {
impl_->frame_index.clear();
impl_->keyframes.clear();
impl_->frame_index_tried = false;
impl_->raw_keyframes.clear();

path_ = path;
codec_ = codec;
Expand Down Expand Up @@ -342,6 +344,12 @@ void FFmpegDecoder::reset() {
}
}

void FFmpegDecoder::SetRawKeyframeIndex(std::vector<RawKeyframe> keyframes) {
if (impl_) {
impl_->raw_keyframes = std::move(keyframes);
}
}

bool FFmpegDecoder::SeekToFrame(int target_index) {
last_seek_stats_ = SeekStats();
if (!impl_ || !impl_->codecCtx || !impl_->fmtCtx) {
Expand All @@ -363,6 +371,37 @@ bool FFmpegDecoder::SeekToFrame(int target_index) {
bool seek_attempted = false;
bool index_rebuilt = false;

if (impl_->is_raw && !impl_->raw_keyframes.empty()) {
const RawKeyframe* keyframe = nullptr;
for (const RawKeyframe& entry : impl_->raw_keyframes) {
if (entry.frame_index <= target_index) {
keyframe = &entry;
} else {
break;
}
}
const int cost_forward = gap > 0 ? gap : INT_MAX;
const int cost_seek = keyframe != nullptr
? target_index - keyframe->frame_index + 2
: INT_MAX;
if (cost_forward > cost_seek) {
if (keyframe != nullptr &&
av_seek_frame(impl_->fmtCtx, -1, keyframe->byte_offset,
AVSEEK_FLAG_BYTE) >= 0) {
avcodec_flush_buffers(impl_->codecCtx);
AVFrame* stale = av_frame_alloc();
while (avcodec_receive_frame(impl_->codecCtx, stale) >= 0) {
av_frame_unref(stale);
}
av_frame_free(&stale);
current_frame_ = keyframe->frame_index;
last_seek_stats_.used_raw_keyframe = true;
} else {
seek_attempted = true;
}
}
}

if (!impl_->is_raw) {
if (impl_->frame_index.empty() && !impl_->frame_index_tried) {
// Building the index demuxes the whole file and resets the decoder, so
Expand Down Expand Up @@ -427,9 +466,6 @@ bool FFmpegDecoder::SeekToFrame(int target_index) {
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start_time)
.count();
if (gap > 0) {
last_seek_stats_.frames_decoded = gap;
}
return true;
}

Expand Down
7 changes: 7 additions & 0 deletions ffmpeg/decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,21 @@ class FFmpegDecoder {

bool DecodeKeyframes(std::vector<DecodedFrame>& out, int max_frames = 10);

struct RawKeyframe {
int frame_index = 0;
int64_t byte_offset = 0;
};

struct SeekStats {
bool used_container = false;
bool used_raw_keyframe = false;
int frames_decoded = 0;
qint64 seek_io_ms = 0;
qint64 decode_ms = 0;
};

const SeekStats& LastSeekStats() const { return last_seek_stats_; }
void SetRawKeyframeIndex(std::vector<RawKeyframe> keyframes);

bool HasTimestamps() const;
int FrameCount() const { return frame_count_; }
Expand Down
20 changes: 20 additions & 0 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,26 @@ void MainWindow::StartPlayer() {
return;
}

if (!player_decoder_.HasTimestamps()) {
// Raw Annex B has no container index: hand the decoder the IDR offsets so
// backward seeks can jump straight to the previous keyframe.
std::vector<FFmpegDecoder::RawKeyframe> keyframes;
int64_t parameter_offset = -1;
for (const NALUnit& nal : parser_.NalUnits()) {
if (nal.nal_type == 7 || nal.nal_type == 8) {
if (parameter_offset < 0) {
parameter_offset = (int64_t)nal.offset;
}
} else if (nal.nal_type == 5 && nal.frame_index >= 0) {
keyframes.push_back({nal.frame_index, parameter_offset >= 0
? parameter_offset
: (int64_t)nal.offset});
parameter_offset = -1;
}
}
player_decoder_.SetRawKeyframeIndex(std::move(keyframes));
}

// No upfront decode: frames are decoded on demand while playing/seeking.
total_frames_ = (int)stats.FrameCount;
frame_rate_ = player_decoder_.FrameRate();
Expand Down
49 changes: 49 additions & 0 deletions test/test_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include <fstream>
#include <string>

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

namespace {

Expand Down Expand Up @@ -126,3 +128,50 @@ TEST(DecoderTest, BackwardSeekUsesContainerSeek) {
ASSERT_TRUE(decoder.DecodeNextFrame(frame));
EXPECT_NEAR(frame.pts_sec, 0.2, 0.01);
}

TEST(DecoderTest, RawSeekJumpsToPreviousIdr) {
const std::string path = data_path("output.h264");
if (!exists(path)) {
GTEST_SKIP() << "raw fixture missing";
}

BitstreamParser parser(CodecType::kH264);
ASSERT_TRUE(parser.ParseFile(path)) << parser.LastError();
NALAnalyzer analyzer;
ASSERT_TRUE(analyzer.AnalyzeAll(parser));

std::vector<FFmpegDecoder::RawKeyframe> keyframes;
int64_t parameter_offset = -1;
for (const NALUnit& nal : parser.NalUnits()) {
if (nal.nal_type == 7 || nal.nal_type == 8) {
if (parameter_offset < 0) {
parameter_offset = (int64_t)nal.offset;
}
} else if (nal.nal_type == 5 && nal.frame_index >= 0) {
keyframes.push_back({nal.frame_index, parameter_offset >= 0
? parameter_offset
: (int64_t)nal.offset});
parameter_offset = -1;
}
}
ASSERT_FALSE(keyframes.empty());

FFmpegDecoder decoder;
ASSERT_TRUE(decoder.OpenFile(path, CodecType::kH264)) << decoder.LastError();
decoder.SetRawKeyframeIndex(keyframes);

DecodedFrame frame;
QImage expected;
for (int i = 0; i <= 50; i++) {
ASSERT_TRUE(decoder.DecodeNextFrame(frame));
if (i == 22) {
expected = frame.image;
}
}
ASSERT_FALSE(expected.isNull());

ASSERT_TRUE(decoder.SeekToFrame(22));
EXPECT_TRUE(decoder.LastSeekStats().used_raw_keyframe);
ASSERT_TRUE(decoder.DecodeNextFrame(frame));
EXPECT_EQ(frame.image, expected);
}
Loading