Merge pull request #909 from OpenShot/memory-leak-mar-20-2023

Fix Memory leaks in FFmpegReader, Clip, and FrameMapper
This commit is contained in:
Jonathan Thomas
2023-03-20 17:31:29 -05:00
committed by GitHub
3 changed files with 15 additions and 8 deletions

View File

@@ -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
@@ -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);
}
}

View File

@@ -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;
}
@@ -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;
@@ -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
@@ -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
@@ -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)

View File

@@ -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)