From fcb1b0f8a0fa51d98ba38042d64163eeab19658f Mon Sep 17 00:00:00 2001 From: Multihuntr Date: Mon, 25 Jun 2018 18:27:49 +1000 Subject: [PATCH] Minimises number of frames sent to decoder by keeping track of which frames are actually needed, and halting once all frames required have been sent. --- src/VideoLoader.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/VideoLoader.cpp b/src/VideoLoader.cpp index a40db86..80a3ee6 100644 --- a/src/VideoLoader.cpp +++ b/src/VideoLoader.cpp @@ -381,7 +381,9 @@ void VideoLoader::impl::read_file() { seek(file, req.frame); auto nonkey_frame_count = 0; - while (req.count > 0 && av_read_frame(file.fmt_ctx_.get(), &raw_pkt) >= 0) { + bool got_frame[req.count] = {false}; + auto frames_left = req.count; + while (frames_left > 0 && av_read_frame(file.fmt_ctx_.get(), &raw_pkt) >= 0) { auto pkt = pkt_ptr(&raw_pkt, av_packet_unref); stats_.bytes_read += pkt->size; @@ -424,23 +426,21 @@ void VideoLoader::impl::read_file() { continue; } else { req.frame += nonkey_frame_count + 1; - req.count -= nonkey_frame_count + 1; nonkey_frame_count = 0; } final_try = false; } else { nonkey_frame_count++; - // A hueristic so we don't go way over... what should "20" be? - if (frame > req.frame + req.count + 20) { - // This should end the loop - req.frame += nonkey_frame_count; - req.count -= nonkey_frame_count; - nonkey_frame_count = 0; - } } } seek_hack = 1; + auto idx = frame-req.frame; + if (idx >= 0 && idx < req.count && !got_frame[idx]) { + got_frame[idx] = true; + frames_left--; + } + log_.info() << device_id_ << ": Sending " << (key ? " key " : "nonkey") << " frame " << frame << " to the decoder." << " size = " << pkt->size