Skip to content

Commit

Permalink
manually call drop function
Browse files Browse the repository at this point in the history
  • Loading branch information
Bramart committed Oct 28, 2024
1 parent 5705508 commit 045292a
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/audio_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ impl AudioDecoder {
})
}
}

pub fn close(&mut self) {
unsafe { avcodec_free_context(&mut self.codec_context) };
}
}

impl Drop for AudioDecoder {
Expand Down
4 changes: 4 additions & 0 deletions src/audio_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ impl AudioEncoder {
}
}
}

pub fn close(&mut self) {
unsafe { avcodec_free_context(&mut self.codec_context) };
}
}

impl Drop for AudioEncoder {
Expand Down
7 changes: 7 additions & 0 deletions src/filter_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,13 @@ impl FilterGraph {

Ok((output_audio_frames, output_video_frames))
}

pub fn close(&mut self) -> Result<(), String> {
if !self.graph.is_null() {
unsafe { avfilter_graph_free(&mut self.graph) };
}
Ok(())
}
}

impl Drop for FilterGraph {
Expand Down
5 changes: 5 additions & 0 deletions src/format_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ impl FormatContext {
Ok(())
}

pub fn close_output(&self) -> Result<(), String> {
unsafe { avformat_free_context(self.format_context) };
Ok(())
}

pub fn add_video_stream(&mut self, encoder: &VideoEncoder) -> Result<(), String> {
unsafe {
let av_stream = avformat_new_stream(self.format_context, null_mut());
Expand Down
28 changes: 28 additions & 0 deletions src/order/decoder_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,32 @@ impl DecoderFormat {
}
}
}

pub fn close(&mut self, input: &Input) {
match input {
Input::VideoFrames { .. } => {}
Input::Streams { streams, .. } => {
for stream in streams {
match self.context.get_stream_type(stream.index as isize) {
AVMediaType::AVMEDIA_TYPE_VIDEO => {
for video_decoder in &mut self.video_decoders {
video_decoder.close();
}
}
AVMediaType::AVMEDIA_TYPE_AUDIO => {
for audio_decoder in &mut self.audio_decoders {
audio_decoder.close();
}
}
AVMediaType::AVMEDIA_TYPE_SUBTITLE => {
for subtitle_decoder in &mut self.subtitle_decoders {
subtitle_decoder.close();
}
}
_ => {}
}
}
}
}
}
}
17 changes: 17 additions & 0 deletions src/order/encoder_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,21 @@ impl EncoderFormat {

Ok(r_packet)
}

pub fn close(&mut self, output: &Output) {
match output.kind {
Some(OutputKind::File) | Some(OutputKind::Packet) => {}
Some(OutputKind::AudioMetadata) => {
for audio_encoder in &mut self.audio_encoders {
audio_encoder.close();
}
}
Some(OutputKind::VideoMetadata) => {
for video_encoder in &mut self.video_encoders {
video_encoder.close();
}
}
None => {}
}
}
}
59 changes: 55 additions & 4 deletions src/order/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use ffmpeg_sys_next::{av_frame_free, av_packet_free};

use crate::filter_graph::FilterGraph;
use std::collections::HashMap;

Expand Down Expand Up @@ -75,12 +77,24 @@ impl Order {
Ok(())
}

pub fn close(&mut self) -> Result<(), String> {
warn!("Close inputs");
self.close_input_format()?;
warn!("Close outputs");
self.close_output_format()?;
warn!("Close graph");
self.close_graph()?;

Ok(())
}

pub fn process(&mut self) -> Result<Vec<OutputResult>, String> {
let mut results: Vec<OutputResult> = vec![];
let mut decode_end = false;

while !decode_end {
let (in_audio_frames, in_video_frames, in_subtitle_packets, end) = self.process_input();
let (mut in_audio_frames, mut in_video_frames, mut in_subtitle_packets, end) =
self.process_input();
if end == self.total_streams {
decode_end = true;
}
Expand All @@ -93,6 +107,16 @@ impl Order {
error!("Error while filtering : {msg}");
}
}

for frame in &mut in_audio_frames {
unsafe { av_frame_free(&mut frame.frame) };
}
for frame in &mut in_video_frames {
unsafe { av_frame_free(&mut frame.frame) };
}
for packet in &mut in_subtitle_packets {
unsafe { av_packet_free(&mut packet.packet) };
}
}

Ok(results)
Expand Down Expand Up @@ -162,10 +186,10 @@ impl Order {
) -> Result<Vec<OutputResult>, String> {
let mut results = vec![];

let (output_audio_frames, output_video_frames) = self
let (mut output_audio_frames, mut output_video_frames) = self
.filter_graph
.process(in_audio_frames, in_video_frames)?;
for output_frame in output_audio_frames {
for output_frame in &mut output_audio_frames {
for output in &self.outputs {
if output.stream == output_frame.name {
if let Some(OutputKind::AudioMetadata) = output.kind {
Expand All @@ -191,13 +215,14 @@ impl Order {
results.push(OutputResult::Packet(packet));
};
}
unsafe { av_frame_free(&mut output_frame.frame) };
}
for output_packet in in_subtitle_packets {
for output in &mut self.output_formats {
output.wrap(output_packet)?;
}
}
for output_frame in output_video_frames {
for output_frame in &mut output_video_frames {
for output in &self.outputs {
if let Some(OutputKind::VideoMetadata) = output.kind {
let mut entry = HashMap::new();
Expand All @@ -218,6 +243,7 @@ impl Order {
results.push(OutputResult::Packet(packet));
};
}
unsafe { av_frame_free(&mut output_frame.frame) };
}

Ok(results)
Expand Down Expand Up @@ -314,6 +340,31 @@ impl Order {

Ok(filters)
}

pub fn close_input_format(&mut self) -> Result<(), String> {
for input_format in &mut self.input_formats {
for input in &self.inputs {
input_format.close(input);
}
input_format.context.close_input();
}
Ok(())
}

fn close_output_format(&mut self) -> Result<(), String> {
for output_format in &mut self.output_formats {
for output in &self.outputs {
output_format.close(output);
}
output_format.context.close_input();
}
Ok(())
}

pub fn close_graph(&mut self) -> Result<(), String> {
self.filter_graph.close()?;
Ok(())
}
}

#[test]
Expand Down
10 changes: 9 additions & 1 deletion src/probe/deep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,8 +681,8 @@ impl DeepProbe {
CheckName::Tone => {
if let Some(params) = deep_orders.check.sine_detect.clone() {
detect_sine(
context,
&deep_orders.output_results,
&self.filename,
&mut deep_orders.streams,
deep_orders.audio_indexes.clone(),
params,
Expand Down Expand Up @@ -770,6 +770,9 @@ impl DeepProbe {
for mut frame in in_video_frames {
unsafe { av_frame_free(&mut frame.frame) };
}
for mut packet in in_subtitle_packets {
unsafe { av_packet_free(&mut packet.packet) };
}
}

if let Err(msg) = self.get_results(&context, &mut deep_orders) {
Expand All @@ -784,7 +787,12 @@ impl DeepProbe {
format,
});

for order in &mut deep_orders.orders {
let _ = order.1.close();
}
let _ = order_src.close();
context.close_input();

Ok(())
}
}
Expand Down
8 changes: 1 addition & 7 deletions src/probe/sine_detect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ pub fn create_graph(
}

pub fn detect_sine(
context: &FormatContext,
output_results: &BTreeMap<CheckName, Vec<OutputResult>>,
filename: &str,
streams: &mut [StreamProbeResult],
audio_indexes: Vec<u32>,
params: HashMap<String, CheckParameterValue>,
Expand Down Expand Up @@ -141,12 +141,6 @@ pub fn detect_sine(
None => return warn!("No input message for the 1000Hz analysis (audio qualification)"),
}

let mut context = FormatContext::new(filename).unwrap();
if let Err(msg) = context.open_input() {
context.close_input();
error!("{:?}", msg);
return;
}
for result in results {
if let Entry(entry_map) = result {
if let Some(stream_id) = entry_map.get("stream_id") {
Expand Down
4 changes: 4 additions & 0 deletions src/subtitle_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ impl SubtitleDecoder {
})
}
}

pub fn close(&mut self) {
unsafe { avcodec_free_context(&mut self.codec_context) };
}
}

impl Drop for SubtitleDecoder {
Expand Down
6 changes: 6 additions & 0 deletions src/video_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ impl VideoDecoder {
})
}
}

pub fn close(&mut self) {
if !self.codec_context.is_null() {
unsafe { avcodec_free_context(&mut self.codec_context) };
}
}
}

impl Drop for VideoDecoder {
Expand Down
4 changes: 4 additions & 0 deletions src/video_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ impl VideoEncoder {
Ok(true)
}
}

pub fn close(&mut self) {
unsafe { avcodec_free_context(&mut self.codec_context) };
}
}

impl Drop for VideoEncoder {
Expand Down

0 comments on commit 045292a

Please sign in to comment.