Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make frames decoder to build index without file decoding #5809

Merged
merged 3 commits into from
Feb 8, 2025
Merged
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
61 changes: 45 additions & 16 deletions dali/operators/reader/loader/video/frames_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -365,33 +365,62 @@ bool FramesDecoder::IsFormatSeekable() {
}

void FramesDecoder::BuildIndex() {
// TODO(awolant): Optimize this function for:
// - CFR
// - index present in the header

index_ = vector<IndexEntry>();

int last_keyframe = -1;
while (ReadRegularFrame(nullptr, false)) {
while (av_read_frame(av_state_->ctx_, av_state_->packet_) >= 0) {
IndexEntry entry;
entry.is_keyframe = av_state_->frame_->flags & AV_FRAME_FLAG_KEY;
entry.pts = av_state_->frame_->pts;
entry.is_flush_frame = false;
// We want to make sure that we call av_packet_unref in every iteration
auto packet = AVPacketScope(av_state_->packet_, av_packet_unref);

if (packet->stream_index != av_state_->stream_id_) {
continue;
}
if (packet->flags & AV_PKT_FLAG_KEY) {
if (index_->size() == 0) {
entry.is_keyframe = true;
}
auto codec_id = av_state_->ctx_->streams[packet->stream_index]->codecpar->codec_id;
if (codec_id == AV_CODEC_ID_H264) {
if (packet->size >= 5) { // Ensure there's enough data to inspect
uint8_t nal_unit_type = packet->data[4] & 0x1F;
if (nal_unit_type == 5) { // IDR frame for H.264
entry.is_keyframe = true;
}
}
} else if (codec_id == AV_CODEC_ID_HEVC) {
if (packet->size >= 6) { // Ensure there's enough data to inspect
uint8_t nal_unit_type = (packet->data[4] >> 1) & 0x3F;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment suggests that it should be:

Suggested change
uint8_t nal_unit_type = (packet->data[4] >> 1) & 0x3F;
uint8_t nal_unit_type = (packet->data[5] >> 1) & 0x3F;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot, I need to check if the source I get this from has a misleading comment or the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

if (nal_unit_type >= 16 && nal_unit_type <= 21) {
entry.is_keyframe = true;
}
}
} else {
// for other formats it is enough to check (packet->flags & AV_PKT_FLAG_KEY)
entry.is_keyframe = true;
}
} else {
entry.is_keyframe = false;
}
if (entry.is_keyframe) {
last_keyframe = index_->size();
}
if (packet->pts != AV_NOPTS_VALUE) {
entry.pts = packet->pts;
} else {
entry.pts = packet->dts;
}
entry.is_flush_frame = false;
entry.last_keyframe_id = last_keyframe;
index_->push_back(entry);
}
while (ReadFlushFrame(nullptr, false)) {
IndexEntry entry;
entry.is_keyframe = false;
entry.pts = av_state_->frame_->pts;
entry.is_flush_frame = true;
entry.last_keyframe_id = last_keyframe;
index_->push_back(entry);
}
av_packet_unref(av_state_->packet_);
index_->back().is_flush_frame = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have more flush frames? Possibly mapping to more packets?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, flush frame is the last one, so we now there it no more.

// Make sure that the index is sorted by pts as some frames may not be in the presentation
// order in the container
std::sort(index_->begin(), index_->end(), [](const IndexEntry &a, const IndexEntry &b) {
return a.pts < b.pts;
});
Reset();
}

Expand Down
Loading