Skip to content

Commit

Permalink
remove getting stream data from detect functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Romanelaf committed Nov 20, 2023
1 parent a90507c commit eaeec0a
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 185 deletions.
33 changes: 8 additions & 25 deletions src/probe/black_detect.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use crate::{
format_context::FormatContext,
order::{
filter_input::FilterInput, filter_output::FilterOutput, input::Input, input_kind::InputKind,
output::Output, output_kind::OutputKind, stream::Stream, Filter, Order, OutputResult::Entry,
ParameterValue,
},
probe::deep::{BlackResult, CheckParameterValue, StreamProbeResult},
stream::Stream as ContextStream,
};
use ffmpeg_sys_next::AVMediaType;
use std::collections::HashMap;

pub fn create_graph(
Expand Down Expand Up @@ -82,6 +79,9 @@ pub fn detect_black_frames(
streams: &mut [StreamProbeResult],
video_indexes: Vec<u32>,
params: HashMap<String, CheckParameterValue>,
frame_rate: f32,
frame_duration: f32,
stream_duration: Option<f32>,
) {
let mut order = create_graph(filename, video_indexes.clone(), params.clone()).unwrap();
if let Err(msg) = order.setup() {
Expand All @@ -96,34 +96,17 @@ pub fn detect_black_frames(
Ok(results) => {
info!("END OF PROCESS");
info!("-> {:?} frames processed", results.len());
let mut end_from_duration = 0;
let end_from_duration = match stream_duration {
Some(duration) => ((duration - frame_duration) * 1000.0).round() as i64,
None => ((results.len() as f32 - 1.0) / frame_rate * 1000.0).round() as i64,
};
let mut max_duration = None;
let mut min_duration = None;
let mut frame_duration = 0.0;
if let Some(duration) = params.get("duration") {
max_duration = duration.max;
min_duration = duration.min;
}
let mut context = FormatContext::new(filename).unwrap();
if let Err(msg) = context.open_input() {
context.close_input();
error!("{:?}", msg);
return;
}
for index in 0..context.get_nb_streams() {
if let Ok(stream) = ContextStream::new(context.get_stream(index as isize)) {
if let AVMediaType::AVMEDIA_TYPE_VIDEO = context.get_stream_type(index as isize) {
let frame_rate = stream.get_frame_rate().to_float();
frame_duration = stream.get_frame_rate().invert().to_float();
if let Some(stream_duration) = stream.get_duration() {
end_from_duration = ((stream_duration - frame_duration) * 1000.0).round() as i64;
} else {
end_from_duration =
((results.len() as f32 - 1.0) / frame_rate * 1000.0).round() as i64;
}
}
}
}

for result in results {
if let Entry(entry_map) = result {
if let Some(stream_id) = entry_map.get("stream_id") {
Expand Down
62 changes: 16 additions & 46 deletions src/probe/crop_detect.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use crate::format_context::FormatContext;
use crate::order::{
filter_input::FilterInput, filter_output::FilterOutput, input::Input, input_kind::InputKind,
output::Output, output_kind::OutputKind, stream::Stream,
};
use crate::order::{Filter, Order, OutputResult::Entry, ParameterValue};
use crate::probe::deep::{CheckParameterValue, CropResult, StreamProbeResult};
use crate::stream::Stream as ContextStream;
use crate::tools::rational::Rational;
use ffmpeg_sys_next::AVMediaType;
use std::collections::HashMap;

pub fn create_graph(
Expand Down Expand Up @@ -91,31 +88,20 @@ pub fn detect_black_borders(
streams: &mut [StreamProbeResult],
video_indexes: Vec<u32>,
params: HashMap<String, CheckParameterValue>,
stream_frames: Option<i64>,
bits_raw_sample: Option<i32>,
time_base: f32,
metadata_width: i32,
metadata_height: i32,
aspect_ratio: Rational,
) {
let mut context = FormatContext::new(filename).unwrap();
if let Err(msg) = context.open_input() {
context.close_input();
error!("{:?}", msg);
return;
}

let mut nb_frames = 0;
let mut limit = 0;
for index in 0..context.get_nb_streams() {
if let Ok(stream) = ContextStream::new(context.get_stream(index as isize)) {
if let AVMediaType::AVMEDIA_TYPE_VIDEO = context.get_stream_type(index as isize) {
if let Some(frames) = stream.get_nb_frames() {
nb_frames = frames;
}
// black threshold : 16 pour 8bits / 64 pour 10bits / 256 pour 12bits
limit = match stream.get_bits_per_raw_sample() {
Some(10) => 64,
Some(12) => 256,
_ => 16,
}
}
}
}
let nb_frames = stream_frames.unwrap_or(0);
// black threshold : 16 pour 8bits / 64 pour 10bits / 256 pour 12bits
let limit = match bits_raw_sample {
Some(10) => 64,
Some(12) => 256,
_ => 16,
};
let mut order = create_graph(filename, video_indexes.clone(), params, nb_frames, limit).unwrap();
if let Err(msg) = order.setup() {
error!("{:?}", msg);
Expand All @@ -129,27 +115,11 @@ pub fn detect_black_borders(
Ok(results) => {
info!("END OF PROCESS");
info!("-> {:?} frames processed", results.len());
let mut time_base = 1.0;
let mut metadata_width = 0;
let mut metadata_height = 0;
let mut real_width = 0;
let mut real_height = 0;
let mut w_changed = false;
let mut h_changed = false;
let mut pict_size = Rational::new(1, 1);
let mut real_width = metadata_width;
let mut real_height = metadata_height;

for index in 0..context.get_nb_streams() {
if let Ok(stream) = ContextStream::new(context.get_stream(index as isize)) {
if let AVMediaType::AVMEDIA_TYPE_VIDEO = context.get_stream_type(index as isize) {
time_base = stream.get_time_base().to_float();
metadata_width = stream.get_width();
metadata_height = stream.get_height();
pict_size = stream.get_picture_aspect_ratio();
real_width = metadata_width;
real_height = metadata_height;
}
}
}
for result in results {
if let Entry(entry_map) = result {
if let Some(stream_id) = entry_map.get("stream_id") {
Expand Down Expand Up @@ -190,7 +160,7 @@ pub fn detect_black_borders(
crop.height = real_height;
crop.pts = (pts.parse::<f32>().unwrap() * time_base * 1000.0).round() as i64;
let real_aspect =
(real_width * pict_size.num) as f32 / (real_height * pict_size.den) as f32;
(real_width * aspect_ratio.num) as f32 / (real_height * aspect_ratio.den) as f32;
crop.aspect_ratio = real_aspect;
detected_crop.push(crop);
w_changed = false;
Expand Down
41 changes: 40 additions & 1 deletion src/probe/deep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::probe::scene_detect::detect_scene;
use crate::probe::silence_detect::detect_silence;
use crate::probe::sine_detect::detect_sine;
use crate::stream::Stream;
use crate::tools::rational::Rational;
use ffmpeg_sys_next::*;
use log::LevelFilter;
use std::{cmp, collections::HashMap, fmt};
Expand Down Expand Up @@ -331,6 +332,14 @@ impl DeepProbe {
}

let mut frame_duration = 0.0;
let mut frame_rate = 1.0;
let mut time_base = 1.0;
let mut metadata_width = 0;
let mut metadata_height = 0;
let mut stream_duration = None;
let mut stream_frames = None;
let mut bits_raw_sample = None;
let mut aspect_ratio = Rational::new(1, 1);
let mut streams = vec![];
streams.resize(context.get_nb_streams() as usize, StreamProbeResult::new());
while let Ok(packet) = context.next_packet() {
Expand All @@ -353,6 +362,14 @@ impl DeepProbe {
streams[stream_index].color_trc = stream.get_color_trc();
streams[stream_index].color_matrix = stream.get_color_matrix();
frame_duration = stream.get_frame_rate().invert().to_float();
frame_rate = stream.get_frame_rate().to_float();
time_base = stream.get_time_base().to_float();
stream_duration = stream.get_duration();
stream_frames = stream.get_nb_frames();
bits_raw_sample = stream.get_bits_per_raw_sample();
metadata_width = stream.get_width();
metadata_height = stream.get_height();
aspect_ratio = stream.get_picture_aspect_ratio();
}
}
}
Expand All @@ -375,6 +392,8 @@ impl DeepProbe {
&mut streams,
audio_indexes.clone(),
silence_parameters,
frame_rate,
frame_duration,
);
}

Expand All @@ -384,6 +403,9 @@ impl DeepProbe {
&mut streams,
video_indexes.clone(),
black_parameters,
frame_rate,
frame_duration,
stream_duration,
);
}

Expand All @@ -405,6 +427,12 @@ impl DeepProbe {
&mut streams,
video_indexes.clone(),
crop_parameters,
stream_frames,
bits_raw_sample,
time_base,
metadata_width,
metadata_height,
aspect_ratio,
);
}

Expand All @@ -414,6 +442,7 @@ impl DeepProbe {
&mut streams,
video_indexes.clone(),
scene_parameters,
frame_rate,
);
}

Expand All @@ -423,6 +452,8 @@ impl DeepProbe {
&mut streams,
video_indexes.clone(),
ocr_parameters,
frame_rate,
stream_frames,
);
}

Expand All @@ -447,11 +478,19 @@ impl DeepProbe {
&mut streams,
audio_indexes.clone(),
dualmono_parameters,
frame_rate,
frame_duration,
);
}

if let Some(sine_parameters) = check.sine_detect {
detect_sine(&self.filename, &mut streams, audio_indexes, sine_parameters);
detect_sine(
&self.filename,
&mut streams,
audio_indexes,
sine_parameters,
frame_rate,
);
}

let mut format = FormatProbeResult::new();
Expand Down
31 changes: 7 additions & 24 deletions src/probe/dualmono_detect.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use crate::format_context::FormatContext;
use crate::{
order::{
filter_input::FilterInput, filter_output::FilterOutput, input::Input, input_kind::InputKind,
output::Output, output_kind::OutputKind, stream::Stream, Filter, Order, OutputResult::Entry,
ParameterValue,
},
probe::deep::{CheckParameterValue, DualMonoResult, StreamProbeResult},
stream::Stream as ContextStream,
};
use ffmpeg_sys_next::AVMediaType;
use std::collections::HashMap;

pub fn create_graph<S: ::std::hash::BuildHasher>(
Expand Down Expand Up @@ -119,10 +116,11 @@ pub fn detect_dualmono<S: ::std::hash::BuildHasher>(
streams: &mut [StreamProbeResult],
audio_indexes: Vec<u32>,
params: HashMap<String, CheckParameterValue, S>,
frame_rate: f32,
frame_duration: f32,
) {
let mut order = create_graph(filename, &params).unwrap();
let mut max_duration = None;
let mut frame_duration = 0.0;
if let Some(duration) = params.get("duration") {
max_duration = duration.max;
}
Expand All @@ -138,7 +136,6 @@ pub fn detect_dualmono<S: ::std::hash::BuildHasher>(
Ok(results) => {
info!("END OF PROCESS");
info!("-> {:?} frames processed", results.len());
let mut end_from_duration = 0;

let mut audio_stream_qualif_number = 0;
match params.get("pairing_list") {
Expand All @@ -157,24 +154,10 @@ pub fn detect_dualmono<S: ::std::hash::BuildHasher>(
None => warn!("No input message for the dualmono analysis (list of indexes to merge)"),
}

let mut context = FormatContext::new(filename).unwrap();
if let Err(msg) = context.open_input() {
context.close_input();
error!("{:?}", msg);
return;
}
for index in 0..context.get_nb_streams() {
if let Ok(stream) = ContextStream::new(context.get_stream(index as isize)) {
if let AVMediaType::AVMEDIA_TYPE_VIDEO = context.get_stream_type(index as isize) {
let frame_rate = stream.get_frame_rate().to_float() as f64;
frame_duration = stream.get_frame_rate().invert().to_float() as f64;
end_from_duration = (((results.len() as f64 / audio_stream_qualif_number as f64) - 1.0)
/ frame_rate
* 1000.0)
.round() as i64;
}
}
}
let end_from_duration = (((results.len() as f64 / audio_stream_qualif_number as f64) - 1.0)
/ frame_rate as f64
* 1000.0)
.round() as i64;
for result in results {
if let Entry(entry_map) = result {
if let Some(stream_id) = entry_map.get("stream_id") {
Expand All @@ -198,7 +181,7 @@ pub fn detect_dualmono<S: ::std::hash::BuildHasher>(
if let Some(value) = entry_map.get("lavfi.aphasemeter.mono_end") {
if let Some(last_detect) = detected_dualmono.last_mut() {
last_detect.end =
((value.parse::<f64>().unwrap() - frame_duration) * 1000.0).round() as i64;
((value.parse::<f64>().unwrap() - frame_duration as f64) * 1000.0).round() as i64;
}
}
if let Some(value) = entry_map.get("lavfi.aphasemeter.mono_duration") {
Expand Down
7 changes: 0 additions & 7 deletions src/probe/loudness_detect.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::format_context::FormatContext;
use crate::order::{
filter_input::FilterInput, filter_output::FilterOutput, input::Input, input_kind::InputKind,
output::Output, output_kind::OutputKind, stream::Stream, Filter, Order, OutputResult::Entry,
Expand Down Expand Up @@ -126,12 +125,6 @@ pub fn detect_loudness<S: ::std::hash::BuildHasher>(
Ok(results) => {
info!("END OF PROCESS");
info!("-> {:?} frames processed", results.len());
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
Loading

0 comments on commit eaeec0a

Please sign in to comment.