From 532162e2dab44a648593ffe89af4879adb67c0ed Mon Sep 17 00:00:00 2001 From: kieran Date: Mon, 18 Nov 2024 12:19:14 +0000 Subject: [PATCH] fix: memory leaks --- src/decode.rs | 18 ++++++++---------- src/demux.rs | 3 ++- src/encode.rs | 8 +++++++- src/mux.rs | 3 +-- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/decode.rs b/src/decode.rs index 63b18a1..4a11748 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -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) diff --git a/src/demux.rs b/src/demux.rs index 4b91441..8935689 100644 --- a/src/demux.rs +++ b/src/demux.rs @@ -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); diff --git a/src/encode.rs b/src/encode.rs index 6c4e542..e08b708 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -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 { diff --git a/src/mux.rs b/src/mux.rs index a7016dd..c5faf4c 100644 --- a/src/mux.rs +++ b/src/mux.rs @@ -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) }