diff --git a/app/codec/ffmpeg/ffmpegdecoder.cpp b/app/codec/ffmpeg/ffmpegdecoder.cpp index ecccf03b3..fbbb869ee 100644 --- a/app/codec/ffmpeg/ffmpegdecoder.cpp +++ b/app/codec/ffmpeg/ffmpegdecoder.cpp @@ -47,13 +47,13 @@ OLIVE_NAMESPACE_ENTER QHash< Stream*, QList > FFmpegDecoder::instances_; QMutex FFmpegDecoder::instance_lock_; +// FIXME: Hardcoded, ideally this value is dynamically chosen based on memory restraints const int FFmpegDecoder::kMaxFrameLife = 5000; FFmpegDecoder::FFmpegDecoder() : scale_ctx_(nullptr), scale_divider_(-1) { - // FIXME: Hardcoded, ideally this value is dynamically chosen based on memory restraints clear_timer_.setInterval(kMaxFrameLife); clear_timer_.moveToThread(qApp->thread()); connect(&clear_timer_, &QTimer::timeout, this, &FFmpegDecoder::ClearTimerEvent); @@ -182,10 +182,7 @@ FramePtr FFmpegDecoder::RetrieveVideo(const rational &timecode, const int &divid foreach (FFmpegDecoderInstance* i, instances) { - { - TIME_THIS_FUNCTION; - i->cache_lock()->lock(); - } + i->cache_lock()->lock(); if (i->CacheContainsTime(target_ts)) { @@ -1100,7 +1097,9 @@ AVFramePtr FFmpegDecoderInstance::GetFrameFromCache(const int64_t &t) const void FFmpegDecoderInstance::RemoveFramesBefore(const qint64 &t) { - cached_frames_.remove_old_frames(t); + if (cached_frames_.remove_old_frames(t)) { + cache_at_zero_ = false; + } } rational FFmpegDecoderInstance::sample_aspect_ratio() const diff --git a/app/codec/ffmpeg/ffmpegframecache.cpp b/app/codec/ffmpeg/ffmpegframecache.cpp index bd21411fb..d64ac8c37 100644 --- a/app/codec/ffmpeg/ffmpegframecache.cpp +++ b/app/codec/ffmpeg/ffmpegframecache.cpp @@ -95,11 +95,16 @@ void FFmpegFrameCache::Client::accessed(int i) frames_[i]->access(); } -void FFmpegFrameCache::Client::remove_old_frames(qint64 older_than) +int FFmpegFrameCache::Client::remove_old_frames(qint64 older_than) { - while (!frames_.isEmpty() && frames_.first()->last_accessed() < older_than) { + int counter = 0; + + while (frames_.size() > 1 && frames_.first()->last_accessed() < older_than) { FFmpegFrameCache::Release(frames_.takeFirst()); + counter++; } + + return counter; } AVFramePtr FFmpegFrameCache::Get(int width, int height, int format) diff --git a/app/codec/ffmpeg/ffmpegframecache.h b/app/codec/ffmpeg/ffmpegframecache.h index bb38651be..084b5e869 100644 --- a/app/codec/ffmpeg/ffmpegframecache.h +++ b/app/codec/ffmpeg/ffmpegframecache.h @@ -57,7 +57,7 @@ public: void accessedLast(); void accessed(int i); - void remove_old_frames(qint64 older_than); + int remove_old_frames(qint64 older_than); private: QList frames_;