Skip to content

Commit

Permalink
Add an ability to rewind at the end of the video (#5676)
Browse files Browse the repository at this point in the history
- in some cases after seeking to the last key frame in the video
  leads to the premature end of decoding as the decoding doesn't start
  and yet DALI waits for frames. This PR adds a rewind to one before
  the last (and so on), frame to make sure we can decode the last frames

Signed-off-by: Janusz Lisiecki <jlisiecki@nvidia.com>
  • Loading branch information
JanuszL authored Oct 23, 2024
1 parent 9a80050 commit 3b4573a
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions dali/operators/reader/loader/video_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -576,29 +576,37 @@ void VideoLoader::read_file() {
int frames_send = 0;
VidReqStatus dec_status = VidReqStatus::REQ_IN_PROGRESS;

while (av_read_frame(file.fmt_ctx_.get(), &raw_pkt) >= 0) {
auto pkt = pkt_ptr(&raw_pkt, av_packet_unref);
int read_frame_ret = 0;
while ((read_frame_ret = av_read_frame(file.fmt_ctx_.get(), &raw_pkt)) >= 0 ||
dec_status == VidReqStatus::REQ_NOT_STARTED) {
pkt_ptr pkt = {nullptr, av_packet_unref};
int64_t frame = 0;

stats_.bytes_read += pkt->size;
stats_.packets_read++;
if (read_frame_ret >= 0) {
pkt = pkt_ptr(&raw_pkt, av_packet_unref);

if (pkt->stream_index != file.vid_stream_idx_) {
continue;
}
stats_.bytes_read += pkt->size;
stats_.packets_read++;

auto frame = av_rescale_q(pkt->pts - file.start_time_,
file.stream_base_,
file.frame_base_);
LOG_LINE << "Frame candidate " << frame << " (for " << req.frame <<" )...\n";
if (pkt->stream_index != file.vid_stream_idx_) {
continue;
}

file.last_frame_ = frame;
key = (pkt->flags & AV_PKT_FLAG_KEY) != 0;
frame = av_rescale_q(pkt->pts - file.start_time_,
file.stream_base_,
file.frame_base_);
LOG_LINE << "Frame candidate " << frame << " (for " << req.frame <<" )...\n";

file.last_frame_ = frame;
key = (pkt->flags & AV_PKT_FLAG_KEY) != 0;
}

// if decoding hasn't produced any frames after providing kStartupFrameThreshold frames,
// or we are at next key frame
if (last_key_frame != -1 &&
((key && last_key_frame != frame && last_key_frame != 0) ||
frames_send > kStartupFrameThreshold) &&
frames_send > kStartupFrameThreshold ||
read_frame_ret < 0) &&
dec_status == VidReqStatus::REQ_NOT_STARTED) {
if (last_key_frame <= 0) {
if (vid_decoder_) {
Expand All @@ -617,7 +625,11 @@ void VideoLoader::read_file() {
--previous_last_key_frame;
}
LOG_LINE << "Decoding not started, seek to preceding key frame, "
<< "current frame " << frame
<< " frames_send vs kStartupFrameThreshold: "
<< frames_send << " / " << kStartupFrameThreshold
<< ", av_read_frame result: "
<< read_frame_ret
<< ", current frame " << frame
<< ", look for a key frame before " << previous_last_key_frame
<< ", is_key " << key << std::endl;
seek(file, previous_last_key_frame);
Expand Down

0 comments on commit 3b4573a

Please sign in to comment.