memory: try caching frameptrs instead of avframes so we have more control

This commit is contained in:
itsmattkc
2020-02-23 21:20:33 +11:00
parent 48cdb3a6dd
commit b1cb8489df
6 changed files with 158 additions and 135 deletions
+52 -24
View File
@@ -3,78 +3,106 @@
#include <QDateTime>
#include <QDebug>
int FFmpegFrameCache::global_frame_count_ = 0;
QMutex FFmpegFrameCache::pool_lock_;
QList<Frame*> FFmpegFrameCache::frame_pool_;
FFmpegFrameCache::FFmpegFrameCache()
Frame *FFmpegFrameCache::Client::append(const VideoRenderingParams& params)
{
}
void FFmpegFrameCache::append(AVFrame *f)
{
global_frame_count_++;
Frame* f = FFmpegFrameCache::Get(params);
frames_.append({f, QDateTime::currentMSecsSinceEpoch()});
return f;
}
void FFmpegFrameCache::clear()
void FFmpegFrameCache::Client::clear()
{
global_frame_count_ -= frames_.size();
foreach (const CachedFrame& cf, frames_) {
FFmpegFrameCache::Release(cf.frame);
}
frames_.clear();
}
bool FFmpegFrameCache::isEmpty() const
bool FFmpegFrameCache::Client::isEmpty() const
{
return frames_.isEmpty();
}
AVFrame *FFmpegFrameCache::first() const
Frame *FFmpegFrameCache::Client::first() const
{
return frames_.first().frame;
}
AVFrame *FFmpegFrameCache::at(int i) const
Frame *FFmpegFrameCache::Client::at(int i) const
{
return frames_.at(i).frame;
}
AVFrame *FFmpegFrameCache::last() const
Frame *FFmpegFrameCache::Client::last() const
{
return frames_.last().frame;
}
int FFmpegFrameCache::size() const
int FFmpegFrameCache::Client::size() const
{
return frames_.size();
}
void FFmpegFrameCache::accessedFirst()
void FFmpegFrameCache::Client::accessedFirst()
{
frames_.first().accessed = QDateTime::currentMSecsSinceEpoch();
}
void FFmpegFrameCache::accessedLast()
void FFmpegFrameCache::Client::accessedLast()
{
frames_.last().accessed = QDateTime::currentMSecsSinceEpoch();
}
void FFmpegFrameCache::accessed(int i)
void FFmpegFrameCache::Client::accessed(int i)
{
frames_[i].accessed = QDateTime::currentMSecsSinceEpoch();
}
void FFmpegFrameCache::remove_old_frames(qint64 older_than)
void FFmpegFrameCache::Client::remove_old_frames(qint64 older_than)
{
int counter = 0;
while (!frames_.isEmpty() && frames_.first().accessed < older_than) {
CachedFrame cf = frames_.takeFirst();
av_frame_free(&cf.frame);
FFmpegFrameCache::Release(frames_.takeFirst().frame);
counter++;
}
qDebug() << " * Removed" << counter << "frames";
global_frame_count_ -= counter;
qDebug() << " * Global frames:" << global_frame_count_;
}
Frame* FFmpegFrameCache::Get(const VideoRenderingParams &params)
{
QMutexLocker locker(&pool_lock_);
// See if we have a frame matching this description in the pool
for (int i=0;i<frame_pool_.size();i++) {
if (frame_pool_.at(i)->width() == params.width()
&& frame_pool_.at(i)->height() == params.height()
&& frame_pool_.at(i)->format() == params.format()) {
return frame_pool_.takeAt(i);
}
}
qDebug() << "Nothing in frame pool existed, have to create new...";
// Otherwise we'll need to create one
Frame* f = new Frame();
f->set_width(params.width());
f->set_height(params.height());
f->set_format(params.format());
f->allocate();
return f;
}
void FFmpegFrameCache::Release(Frame *f)
{
QMutexLocker locker(&pool_lock_);
frame_pool_.append(f);
}