fix: memory leaks

This commit is contained in:
kieran 2024-11-18 12:19:14 +00:00
parent db0b57e122
commit 532162e2da
No known key found for this signature in database
GPG Key ID: DE71CEB3925BE941
4 changed files with 18 additions and 14 deletions

View File

@ -5,14 +5,13 @@ use std::fmt::{Display, Formatter};
use std::ptr;
use anyhow::Error;
use ffmpeg_sys_the_third::AVPictureType::AV_PICTURE_TYPE_NONE;
use ffmpeg_sys_the_third::{
av_buffer_ref, av_frame_alloc, av_hwdevice_ctx_create, av_hwdevice_get_type_name,
av_hwdevice_iterate_types, avcodec_alloc_context3, avcodec_find_decoder, avcodec_free_context,
avcodec_get_hw_config, avcodec_get_name, avcodec_open2, avcodec_parameters_to_context,
avcodec_receive_frame, avcodec_send_packet, AVCodec, AVCodecContext, AVCodecHWConfig, AVFrame,
AVHWDeviceType, AVPacket, AVStream, AVERROR, AVERROR_EOF,
AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX,
av_buffer_ref, av_frame_alloc, av_frame_free, av_hwdevice_ctx_create,
av_hwdevice_get_type_name, av_hwdevice_iterate_types, avcodec_alloc_context3,
avcodec_find_decoder, avcodec_free_context, avcodec_get_hw_config, avcodec_get_name,
avcodec_open2, avcodec_parameters_to_context, avcodec_receive_frame, avcodec_send_packet,
AVCodec, AVCodecContext, AVCodecHWConfig, AVFrame, AVHWDeviceType, AVPacket, AVStream, AVERROR,
AVERROR_EOF, AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX,
};
use log::trace;
@ -255,16 +254,15 @@ impl Decoder {
let mut pkgs = Vec::new();
while ret >= 0 {
let frame = av_frame_alloc();
let mut frame = av_frame_alloc();
ret = avcodec_receive_frame(ctx, frame);
if ret < 0 {
av_frame_free(&mut frame);
if ret == AVERROR_EOF || ret == AVERROR(libc::EAGAIN) {
break;
}
return Err(Error::msg(format!("Failed to decode {}", ret)));
}
(*frame).pict_type = AV_PICTURE_TYPE_NONE; // encoder prints warnings
pkgs.push(frame);
}
Ok(pkgs)

View File

@ -222,9 +222,10 @@ impl Demuxer {
}
pub unsafe fn get_packet(&mut self) -> Result<(*mut AVPacket, *mut AVStream), Error> {
let pkt: *mut AVPacket = av_packet_alloc();
let mut pkt = av_packet_alloc();
let ret = av_read_frame(self.ctx, pkt);
if ret == AVERROR_EOF {
av_packet_free(&mut pkt);
return Ok((ptr::null_mut(), ptr::null_mut()));
}
bail_ffmpeg!(ret);

View File

@ -3,7 +3,13 @@ use std::{ptr, slice};
use crate::{bail_ffmpeg, cstr, get_ffmpeg_error_msg, options_to_dict};
use anyhow::{bail, Error, Result};
use ffmpeg_sys_the_third::{av_channel_layout_default, av_d2q, av_inv_q, av_packet_alloc, av_packet_free, avcodec_alloc_context3, avcodec_find_encoder, avcodec_find_encoder_by_name, avcodec_free_context, avcodec_get_supported_config, avcodec_open2, avcodec_receive_packet, avcodec_send_frame, AVChannelLayout, AVCodec, AVCodecConfig, AVCodecContext, AVCodecID, AVFrame, AVPacket, AVPixelFormat, AVRational, AVSampleFormat, AVERROR, AVERROR_EOF};
use ffmpeg_sys_the_third::{
av_channel_layout_default, av_d2q, av_inv_q, av_packet_alloc, av_packet_free,
avcodec_alloc_context3, avcodec_find_encoder, avcodec_find_encoder_by_name,
avcodec_free_context, avcodec_get_supported_config, avcodec_open2, avcodec_receive_packet,
avcodec_send_frame, AVChannelLayout, AVCodec, AVCodecConfig, AVCodecContext, AVCodecID,
AVFrame, AVPacket, AVPixelFormat, AVRational, AVSampleFormat, AVERROR, AVERROR_EOF,
};
use libc::EAGAIN;
pub struct Encoder {

View File

@ -240,9 +240,8 @@ impl MuxerBuilder {
// setup other stream params
let encoder_ctx = encoder.codec_context();
(*stream).sample_aspect_ratio = (*encoder_ctx).sample_aspect_ratio;
(*stream).time_base = (*encoder_ctx).time_base;
(*stream).avg_frame_rate = (*encoder_ctx).framerate;
(*stream).r_frame_rate = (*encoder_ctx).framerate;
Ok(stream)
}