diff --git a/ffmpeg/decoder.cpp b/ffmpeg/decoder.cpp index 45a502c..79fcbe3 100644 --- a/ffmpeg/decoder.cpp +++ b/ffmpeg/decoder.cpp @@ -36,6 +36,7 @@ struct FFmpegDecoder::Impl { std::vector frame_index; // presentation order std::vector keyframes; // indices into frame_index bool frame_index_tried = false; + std::vector raw_keyframes; }; FFmpegDecoder::FFmpegDecoder() : impl_(new Impl()) {} @@ -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; @@ -342,6 +344,12 @@ void FFmpegDecoder::reset() { } } +void FFmpegDecoder::SetRawKeyframeIndex(std::vector 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) { @@ -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 @@ -427,9 +466,6 @@ bool FFmpegDecoder::SeekToFrame(int target_index) { std::chrono::duration_cast( std::chrono::steady_clock::now() - start_time) .count(); - if (gap > 0) { - last_seek_stats_.frames_decoded = gap; - } return true; } diff --git a/ffmpeg/decoder.h b/ffmpeg/decoder.h index b1ba869..b833d20 100644 --- a/ffmpeg/decoder.h +++ b/ffmpeg/decoder.h @@ -39,14 +39,21 @@ class FFmpegDecoder { bool DecodeKeyframes(std::vector& 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 keyframes); bool HasTimestamps() const; int FrameCount() const { return frame_count_; } diff --git a/mainwindow.cpp b/mainwindow.cpp index c74ef09..a4865c1 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -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 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(); diff --git a/test/test_decoder.cpp b/test/test_decoder.cpp index af0a3b1..fa9d702 100644 --- a/test/test_decoder.cpp +++ b/test/test_decoder.cpp @@ -3,7 +3,9 @@ #include #include +#include "bitstreamparser.h" #include "decoder.h" +#include "nalanalyzer.h" namespace { @@ -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 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); +}