mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 23:50:19 +00:00
LibVideo: Implement Matroska keyframe search for when there are no Cues
This just searches sequentially through each block in a SampleIterator until it finds a block after the specified seek timestamp. Once it finds one, it will try to set the input/output iterator to the most recent keyframe. If the iterator's original position is closer to the target, however, it leaves it at that original position, allowing callers to continue decoding from that position until they reach the target timestamp.
This commit is contained in:
parent
9040194d54
commit
eef8867d9e
Notes:
sideshowbarker
2024-07-17 16:42:19 +09:00
Author: https://github.com/Zaggy1024 Commit: https://github.com/SerenityOS/serenity/commit/eef8867d9e Pull-request: https://github.com/SerenityOS/serenity/pull/16063
3 changed files with 67 additions and 9 deletions
|
@ -53,13 +53,16 @@ DecoderErrorOr<MatroskaDemuxer::TrackStatus*> MatroskaDemuxer::get_track_status(
|
||||||
return &m_track_statuses.get(track).release_value();
|
return &m_track_statuses.get(track).release_value();
|
||||||
}
|
}
|
||||||
|
|
||||||
DecoderErrorOr<void> MatroskaDemuxer::seek_to_most_recent_keyframe(Track track, Time)
|
DecoderErrorOr<void> MatroskaDemuxer::seek_to_most_recent_keyframe(Track track, Time timestamp)
|
||||||
{
|
{
|
||||||
// Removing the track status will cause us to start from the beginning.
|
// Removing the track status will cause us to start from the beginning.
|
||||||
// FIXME: We just go back to the beginning always, so that the PlaybackManager seeking
|
if (timestamp.is_zero()) {
|
||||||
// technology can be tested.
|
m_track_statuses.remove(track);
|
||||||
m_track_statuses.remove(track);
|
return {};
|
||||||
return {};
|
}
|
||||||
|
|
||||||
|
auto& track_status = *TRY(get_track_status(track));
|
||||||
|
return m_reader.seek_to_random_access_point(track_status.iterator, timestamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
DecoderErrorOr<NonnullOwnPtr<Sample>> MatroskaDemuxer::get_next_sample_for_track(Track track)
|
DecoderErrorOr<NonnullOwnPtr<Sample>> MatroskaDemuxer::get_next_sample_for_track(Track track)
|
||||||
|
|
|
@ -599,11 +599,62 @@ DecoderErrorOr<SampleIterator> Reader::create_sample_iterator(u64 track_number)
|
||||||
return SampleIterator(this->m_mapped_file, segment_view, track_number, position, TRY(segment_information()).timestamp_scale());
|
return SampleIterator(this->m_mapped_file, segment_view, track_number, position, TRY(segment_information()).timestamp_scale());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static DecoderErrorOr<bool> find_keyframe_before_timestamp(SampleIterator& iterator, Time const& timestamp)
|
||||||
|
{
|
||||||
|
#if MATROSKA_DEBUG
|
||||||
|
size_t inter_frames_count;
|
||||||
|
#endif
|
||||||
|
Optional<SampleIterator> last_keyframe;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
SampleIterator rewind_iterator = iterator;
|
||||||
|
auto block = TRY(iterator.next_block());
|
||||||
|
|
||||||
|
if (block.only_keyframes()) {
|
||||||
|
last_keyframe.emplace(rewind_iterator);
|
||||||
|
#if MATROSKA_DEBUG
|
||||||
|
inter_frames_count = 0;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
if (block.timestamp() > timestamp)
|
||||||
|
break;
|
||||||
|
|
||||||
|
#if MATROSKA_DEBUG
|
||||||
|
inter_frames_count++;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
if (last_keyframe.has_value()) {
|
||||||
|
#if MATROSKA_DEBUG
|
||||||
|
dbgln("Seeked to a keyframe with {} inter frames to skip", inter_frames_count);
|
||||||
|
#endif
|
||||||
|
iterator = last_keyframe.release_value();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
DecoderErrorOr<void> Reader::seek_to_random_access_point(SampleIterator& iterator, Time timestamp)
|
DecoderErrorOr<void> Reader::seek_to_random_access_point(SampleIterator& iterator, Time timestamp)
|
||||||
{
|
{
|
||||||
(void)iterator;
|
// FIXME: Use Cues to look these up if the element is present.
|
||||||
(void)timestamp;
|
|
||||||
return DecoderError::not_implemented();
|
// FIXME: This could cache the keyframes it finds. Is it worth doing? Probably not, most files will have Cues :^)
|
||||||
|
if (timestamp < iterator.last_timestamp() || iterator.last_timestamp().is_negative()) {
|
||||||
|
// If the timestamp is before the iterator's current position, then we need to start from the beginning of the Segment.
|
||||||
|
iterator = TRY(create_sample_iterator(iterator.m_track_id));
|
||||||
|
if (!TRY(find_keyframe_before_timestamp(iterator, timestamp)))
|
||||||
|
return DecoderError::corrupted("No random access points found"sv);
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
auto seeked_iterator = iterator;
|
||||||
|
if (TRY(find_keyframe_before_timestamp(seeked_iterator, timestamp)))
|
||||||
|
iterator = seeked_iterator;
|
||||||
|
VERIFY(iterator.last_timestamp() <= timestamp);
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
DecoderErrorOr<Block> SampleIterator::next_block()
|
DecoderErrorOr<Block> SampleIterator::next_block()
|
||||||
|
@ -639,8 +690,10 @@ DecoderErrorOr<Block> SampleIterator::next_block()
|
||||||
}
|
}
|
||||||
|
|
||||||
m_position = streamer.position();
|
m_position = streamer.position();
|
||||||
if (block.has_value())
|
if (block.has_value()) {
|
||||||
|
m_last_timestamp = block->timestamp();
|
||||||
return block.release_value();
|
return block.release_value();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_current_cluster.clear();
|
m_current_cluster.clear();
|
||||||
|
|
|
@ -75,6 +75,7 @@ class SampleIterator {
|
||||||
public:
|
public:
|
||||||
DecoderErrorOr<Block> next_block();
|
DecoderErrorOr<Block> next_block();
|
||||||
Cluster const& current_cluster() { return *m_current_cluster; }
|
Cluster const& current_cluster() { return *m_current_cluster; }
|
||||||
|
Time const& last_timestamp() { return m_last_timestamp; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class Reader;
|
friend class Reader;
|
||||||
|
@ -98,6 +99,7 @@ private:
|
||||||
size_t m_position { 0 };
|
size_t m_position { 0 };
|
||||||
|
|
||||||
u64 m_timestamp_scale { 0 };
|
u64 m_timestamp_scale { 0 };
|
||||||
|
Time m_last_timestamp { Time::min() };
|
||||||
|
|
||||||
Optional<Cluster> m_current_cluster;
|
Optional<Cluster> m_current_cluster;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue