Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Memory leaks in FFmpegReader, Clip, and FrameMapper #909

Merged
merged 2 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@ void Clip::init_settings()

// Init reader info struct
init_reader_settings();

// Init cache
final_cache.SetMaxBytesFromInfo(10, info.width, info.height, info.sample_rate, info.channels);
}

// Init reader info details
Expand All @@ -108,6 +105,9 @@ void Clip::init_reader_settings() {

// Initialize info struct
info = reader->info;

// Init cache
final_cache.SetMaxBytesFromInfo(8, info.width, info.height, info.sample_rate, info.channels);
}
}

Expand Down
14 changes: 10 additions & 4 deletions src/FFmpegReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,9 @@ void FFmpegReader::ProcessVideoPacket(int64_t requested_frame) {
// Check if the AVFrame is finished and set it
if (!frame_finished) {
// No AVFrame decoded yet, bail out
if (pFrame) {
RemoveAVFrame(pFrame);
}
return;
}

Expand All @@ -1383,8 +1386,6 @@ void FFmpegReader::ProcessVideoPacket(int64_t requested_frame) {
int height = info.height;
int width = info.width;
int64_t video_length = info.video_length;
AVFrame *my_frame = pFrame;
pFrame = NULL;

// Create variables for a RGB Frame (since most videos are not in RGB, we must convert it)
AVFrame *pFrameRGB = nullptr;
Expand Down Expand Up @@ -1485,7 +1486,7 @@ void FFmpegReader::ProcessVideoPacket(int64_t requested_frame) {
height, PIX_FMT_RGBA, scale_mode, NULL, NULL, NULL);

// Resize / Convert to RGB
sws_scale(img_convert_ctx, my_frame->data, my_frame->linesize, 0,
sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize, 0,
original_height, pFrameRGB->data, pFrameRGB->linesize);

// Create or get the existing frame object
Expand All @@ -1510,7 +1511,7 @@ void FFmpegReader::ProcessVideoPacket(int64_t requested_frame) {
AV_FREE_FRAME(&pFrameRGB);

// Remove frame and packet
RemoveAVFrame(my_frame);
RemoveAVFrame(pFrame);
sws_freeContext(img_convert_ctx);

// Get video PTS in seconds
Expand Down Expand Up @@ -1599,6 +1600,11 @@ void FFmpegReader::ProcessAudioPacket(int64_t requested_frame) {

// Calculate total number of samples
packet_samples = audio_frame->nb_samples * AV_GET_CODEC_ATTRIBUTES(aStream, aCodecCtx)->channels;
} else {
if (audio_frame) {
// Free audio frame
AV_FREE_FRAME(&audio_frame);
}
}

// Estimate the # of samples and the end of this packet's location (to prevent GAPS for the next timestamp)
Expand Down
3 changes: 2 additions & 1 deletion src/FrameMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,8 @@ std::shared_ptr<Frame> FrameMapper::GetFrame(int64_t requested_frame)
if (info.sample_rate == mapped_frame->SampleRate() &&
info.channels == mapped_frame->GetAudioChannelsCount() &&
info.channel_layout == mapped_frame->ChannelsLayout() &&
mapped.Samples.total == mapped_frame->GetAudioSamplesCount() == samples_in_frame && is_increasing &&
mapped.Samples.total == mapped_frame->GetAudioSamplesCount() &&
mapped.Samples.total == samples_in_frame && is_increasing &&
mapped.Samples.frame_start == mapped.Odd.Frame &&
mapped.Samples.sample_start == 0 &&
mapped_frame->number == frame_number &&// in some conditions (e.g. end of stream)
Expand Down