Skip to content

Commit

Permalink
Fix cache size smaller than frame size crash
Browse files Browse the repository at this point in the history
  • Loading branch information
myrsloik committed Mar 3, 2024
1 parent 9fe6ecf commit c6ca7e1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/vapoursynth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ static void VS_CC CreateBestVideoSource(const VSMap *In, VSMap *Out, void *, VSC
}

int64_t CacheSize = vsapi->mapGetInt(In, "cachesize", 0, &err);
if (!err && CacheSize > 0)
if (!err && CacheSize >= 0)
D->V->SetMaxCacheSize(CacheSize * 1024 * 1024);

vsapi->createVideoFilter(Out, "VideoSource", &D->VI, BestVideoSourceGetFrame, BestVideoSourceFree, fmUnordered, nullptr, 0, D, Core);
Expand Down
12 changes: 5 additions & 7 deletions src/videosource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ BestVideoFrame *BestVideoSource::GetFrame(int64_t N) {
LWVideoDecoder *Decoder = Decoders[Index];
DecoderLastUse[Index] = DecoderSequenceNum++;

AVFrame *RetFrame = nullptr;
BestVideoFrame *RetFrame = nullptr;

while (Decoder && Decoder->GetFrameNumber() <= N && Decoder->HasMoreFrames()) {
int64_t FrameNumber = Decoder->GetFrameNumber();
Expand All @@ -798,16 +798,16 @@ BestVideoFrame *BestVideoSource::GetFrame(int64_t N) {
}
}

if (FrameNumber == N)
RetFrame = new BestVideoFrame(Frame);

Cache.emplace_front(FrameNumber, Frame);
CacheSize += Cache.front().Size;

while (CacheSize > MaxSize) {
CacheSize -= Cache.back().Size;
Cache.pop_back();
}

if (FrameNumber == N)
RetFrame = Frame;
} else if (FrameNumber < N) {
Decoder->SkipAVFrames(N - PreRoll - FrameNumber);
}
Expand All @@ -827,9 +827,7 @@ BestVideoFrame *BestVideoSource::GetFrame(int64_t N) {
}
}

if (RetFrame)
return new BestVideoFrame(RetFrame);
return nullptr;
return RetFrame;
}

BestVideoFrame *BestVideoSource::GetFrameExtendLast(int64_t N) {
Expand Down

0 comments on commit c6ca7e1

Please sign in to comment.