Skip to content

Commit

Permalink
Try to handle out-of-memory when allocating video buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
jhasse committed Sep 2, 2024
1 parent bfc7d9a commit 3253a73
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/theoraplay/theoraplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,22 @@ static void WorkerThread(THEORAPLAY_Decoder* const ctx) {
item->format = ctx->vidfmt;

if (!ctx->ringBuffer) {
ringBufferSize = (ctx->maxframes + 1) * item->width * item->height * 2;
jngl::debug("Allocating ");
jngl::debug(ringBufferSize / 1024 / 1024);
jngl::debugLn(" MB.");
ctx->ringBuffer = std::make_unique<uint8_t[]>(ringBufferSize);
while (true) {
ringBufferSize = static_cast<size_t>(ctx->maxframes + 1) *
item->width * item->height * 2;
jngl::debug("Allocating ");
jngl::debug(ringBufferSize / 1024 / 1024);
jngl::debugLn(" MB.");
try {
ctx->ringBuffer = std::make_unique<uint8_t[]>(ringBufferSize);
break;
} catch (std::bad_alloc&) {
ctx->maxframes /= 2;
if (ctx->maxframes < 1) {
throw;
}
}
}
}
item->pixels = &ctx->ringBuffer[ringBufferPos];
ringBufferPos += item->width * item->height * 2;
Expand Down

0 comments on commit 3253a73

Please sign in to comment.