Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn main() {

let mut first_audio_stream = None;
for i in 0..format_context.get_nb_streams() {
let stream_type = format_context.get_stream_type(i as isize);
let stream_type = unsafe { format_context.get_stream_type(i as isize) };
log::info!("Stream {}: {:?}", i, stream_type);

if stream_type == AVMediaType::AVMEDIA_TYPE_AUDIO {
Expand Down
28 changes: 20 additions & 8 deletions src/format_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,11 @@ impl FormatContext {
Ok(())
}

pub fn get_stream(&self, stream_index: isize) -> *mut AVStream {
unsafe { *(*self.format_context).streams.offset(stream_index) }
/// # Safety
/// The caller must ensure that `stream_index` is a valid index, i.e., is in [0, (*self.format_context).nb_streams].
/// You can use the `get_nb_streams()` function to determine the valid range.
pub unsafe fn get_stream(&self, stream_index: isize) -> *mut AVStream {
*(*self.format_context).streams.offset(stream_index)
}

pub fn get_nb_streams(&self) -> u32 {
Expand Down Expand Up @@ -181,16 +184,25 @@ impl FormatContext {
unsafe { (*self.format_context).packet_size }
}

pub fn get_stream_type(&self, stream_index: isize) -> AVMediaType {
unsafe { (*(**(*self.format_context).streams.offset(stream_index)).codecpar).codec_type }
/// # Safety
/// The caller must ensure that `stream_index` is a valid index, i.e., is in [0, (*self.format_context).nb_streams].
/// You can use the `get_nb_streams()` function to determine the valid range.
pub unsafe fn get_stream_type(&self, stream_index: isize) -> AVMediaType {
(*(**(*self.format_context).streams.offset(stream_index)).codecpar).codec_type
}

pub fn get_stream_type_name(&self, stream_index: isize) -> String {
unsafe { tools::to_string(av_get_media_type_string(self.get_stream_type(stream_index))) }
/// # Safety
/// The caller must ensure that `stream_index` is a valid index, i.e., is in [0, (*self.format_context).nb_streams].
/// You can use the `get_nb_streams()` function to determine the valid range.
pub unsafe fn get_stream_type_name(&self, stream_index: isize) -> String {
tools::to_string(av_get_media_type_string(self.get_stream_type(stream_index)))
}

pub fn get_codec_id(&self, stream_index: isize) -> AVCodecID {
unsafe { (*(**(*self.format_context).streams.offset(stream_index)).codecpar).codec_id }
/// # Safety
/// The caller must ensure that `stream_index` is a valid index, i.e., is in [0, (*self.format_context).nb_streams].
/// You can use the `get_nb_streams()` function to determine the valid range.
pub unsafe fn get_codec_id(&self, stream_index: isize) -> AVCodecID {
(*(**(*self.format_context).streams.offset(stream_index)).codecpar).codec_id
}

pub fn get_metadata(&self) -> BTreeMap<String, String> {
Expand Down
38 changes: 20 additions & 18 deletions src/order/decoder_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,27 @@ impl DecoderFormat {
tools::random_string(8)
};

match context.get_stream_type(stream.index as isize) {
AVMediaType::AVMEDIA_TYPE_VIDEO => {
let video_decoder =
VideoDecoder::new(identifier.clone(), &context, stream.index as isize)?;
graph.add_input_from_video_decoder(&identifier, &video_decoder)?;
video_decoders.push(video_decoder);
unsafe {
match context.get_stream_type(stream.index as isize) {
AVMediaType::AVMEDIA_TYPE_VIDEO => {
let video_decoder =
VideoDecoder::new(identifier.clone(), &context, stream.index as isize)?;
graph.add_input_from_video_decoder(&identifier, &video_decoder)?;
video_decoders.push(video_decoder);
}
AVMediaType::AVMEDIA_TYPE_AUDIO => {
let audio_decoder =
AudioDecoder::new(identifier.clone(), &context, stream.index as isize)?;
graph.add_input_from_audio_decoder(&identifier, &audio_decoder)?;
audio_decoders.push(audio_decoder);
}
AVMediaType::AVMEDIA_TYPE_SUBTITLE => {
let subtitle_decoder =
SubtitleDecoder::new(identifier.clone(), &context, stream.index as isize)?;
subtitle_decoders.push(subtitle_decoder);
}
_ => {}
}
AVMediaType::AVMEDIA_TYPE_AUDIO => {
let audio_decoder =
AudioDecoder::new(identifier.clone(), &context, stream.index as isize)?;
graph.add_input_from_audio_decoder(&identifier, &audio_decoder)?;
audio_decoders.push(audio_decoder);
}
AVMediaType::AVMEDIA_TYPE_SUBTITLE => {
let subtitle_decoder =
SubtitleDecoder::new(identifier.clone(), &context, stream.index as isize)?;
subtitle_decoders.push(subtitle_decoder);
}
_ => {}
}
}

Expand Down
83 changes: 44 additions & 39 deletions src/probe/deep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,45 +443,48 @@ impl DeepProbe {

for stream_index in 0..context.get_nb_streams() {
let mut input_id = format!("unknown_input_{}", stream_index);
if context.get_stream_type(stream_index as isize) == AVMediaType::AVMEDIA_TYPE_VIDEO {
deep_orders.video_indexes.push(stream_index);
input_id = format!("video_input_{}", stream_index);
if let Ok(stream) = Stream::new(context.get_stream(stream_index as isize)) {
deep_orders.streams[stream_index as usize].color_space = stream.get_color_space();
deep_orders.streams[stream_index as usize].color_range = stream.get_color_range();
deep_orders.streams[stream_index as usize].color_primaries = stream.get_color_primaries();
deep_orders.streams[stream_index as usize].color_trc = stream.get_color_trc();
deep_orders.streams[stream_index as usize].color_matrix = stream.get_color_matrix();
deep_orders.video_details.frame_duration = stream.get_frame_rate().invert().to_float();
deep_orders.video_details.frame_rate = stream.get_frame_rate().to_float();
deep_orders.video_details.time_base = stream.get_time_base().to_float();
deep_orders.video_details.stream_duration = stream.get_duration();
deep_orders.video_details.stream_frames = stream.get_nb_frames();
deep_orders.video_details.bits_raw_sample = stream.get_bits_per_raw_sample();
deep_orders.video_details.metadata_width = stream.get_width();
deep_orders.video_details.metadata_height = stream.get_height();
deep_orders.video_details.aspect_ratio = stream.get_picture_aspect_ratio();
unsafe {
if context.get_stream_type(stream_index as isize) == AVMediaType::AVMEDIA_TYPE_VIDEO {
deep_orders.video_indexes.push(stream_index);
input_id = format!("video_input_{}", stream_index);
if let Ok(stream) = Stream::new(context.get_stream(stream_index as isize)) {
deep_orders.streams[stream_index as usize].color_space = stream.get_color_space();
deep_orders.streams[stream_index as usize].color_range = stream.get_color_range();
deep_orders.streams[stream_index as usize].color_primaries =
stream.get_color_primaries();
deep_orders.streams[stream_index as usize].color_trc = stream.get_color_trc();
deep_orders.streams[stream_index as usize].color_matrix = stream.get_color_matrix();
deep_orders.video_details.frame_duration = stream.get_frame_rate().invert().to_float();
deep_orders.video_details.frame_rate = stream.get_frame_rate().to_float();
deep_orders.video_details.time_base = stream.get_time_base().to_float();
deep_orders.video_details.stream_duration = stream.get_duration();
deep_orders.video_details.stream_frames = stream.get_nb_frames();
deep_orders.video_details.bits_raw_sample = stream.get_bits_per_raw_sample();
deep_orders.video_details.metadata_width = stream.get_width();
deep_orders.video_details.metadata_height = stream.get_height();
deep_orders.video_details.aspect_ratio = stream.get_picture_aspect_ratio();
}
}
}
if context.get_stream_type(stream_index as isize) == AVMediaType::AVMEDIA_TYPE_AUDIO {
deep_orders.audio_indexes.push(stream_index);
input_id = format!("audio_input_{}", stream_index);
if let Ok(stream) = Stream::new(context.get_stream(stream_index as isize)) {
let avg_pkt_duration = (deep_orders.streams[stream_index as usize].total_packets_duration
as f64
/ deep_orders.streams[stream_index as usize].count_packets as f64)
.ceil();
let audio_stream_details: AudioDetails = AudioDetails {
stream_index: stream_index as i32,
stream_duration: stream.get_duration(),
sample_rate: stream.get_sample_rate(),
samples_per_frame: avg_pkt_duration as i32,
};
deep_orders.audio_details.push(audio_stream_details);
if context.get_stream_type(stream_index as isize) == AVMediaType::AVMEDIA_TYPE_AUDIO {
deep_orders.audio_indexes.push(stream_index);
input_id = format!("audio_input_{}", stream_index);
if let Ok(stream) = Stream::new(context.get_stream(stream_index as isize)) {
let avg_pkt_duration = (deep_orders.streams[stream_index as usize]
.total_packets_duration as f64
/ deep_orders.streams[stream_index as usize].count_packets as f64)
.ceil();
let audio_stream_details: AudioDetails = AudioDetails {
stream_index: stream_index as i32,
stream_duration: stream.get_duration(),
sample_rate: stream.get_sample_rate(),
samples_per_frame: avg_pkt_duration as i32,
};
deep_orders.audio_details.push(audio_stream_details);
}
}
if context.get_stream_type(stream_index as isize) == AVMediaType::AVMEDIA_TYPE_SUBTITLE {
input_id = format!("subtitle_input_{}", stream_index);
}
}
if context.get_stream_type(stream_index as isize) == AVMediaType::AVMEDIA_TYPE_SUBTITLE {
input_id = format!("subtitle_input_{}", stream_index);
}
let input_streams = vec![StreamOrder {
index: stream_index,
Expand Down Expand Up @@ -705,8 +708,10 @@ impl DeepProbe {
}
}
for index in 0..context.get_nb_streams() {
if let Ok(stream) = Stream::new(context.get_stream(index as isize)) {
deep_orders.streams[(index) as usize].detected_bitrate = stream.get_bit_rate();
unsafe {
if let Ok(stream) = Stream::new(context.get_stream(index as isize)) {
deep_orders.streams[(index) as usize].detected_bitrate = stream.get_bit_rate();
}
}
}

Expand Down
130 changes: 66 additions & 64 deletions src/probe/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,76 +178,78 @@ impl Probe {
let mut streams = vec![];

for index in 0..context.get_nb_streams() {
if let Ok(stream) = Stream::new(context.get_stream(index as isize)) {
let stream_type = context.get_stream_type_name(index as isize);
let codec_name = stream.get_codec_name();
let codec_long_name = stream.get_codec_long_name();
let codec_tag = stream.get_codec_tag();
let duration = stream.get_duration();
let start_time = stream.get_start_time();
let bit_rate = stream.get_bit_rate();
let mut vp = None;
let mut ap = None;
let stream_metadata = stream.get_stream_metadata();
unsafe {
if let Ok(stream) = Stream::new(context.get_stream(index as isize)) {
let stream_type = context.get_stream_type_name(index as isize);
let codec_name = stream.get_codec_name();
let codec_long_name = stream.get_codec_long_name();
let codec_tag = stream.get_codec_tag();
let duration = stream.get_duration();
let start_time = stream.get_start_time();
let bit_rate = stream.get_bit_rate();
let mut vp = None;
let mut ap = None;
let stream_metadata = stream.get_stream_metadata();

match context.get_stream_type(index as isize) {
AVMediaType::AVMEDIA_TYPE_VIDEO => {
let width = stream.get_width();
let height = stream.get_height();
let display_aspect_ratio = stream.get_display_aspect_ratio();
let frame_rate = stream.get_frame_rate();
let scanning_type = stream.get_scanning_type();
let chroma_subsampling = stream.get_chroma_sub_sample();
let level = stream.get_level();
let profile = stream.get_profile();
let timecode = stream.get_timecode();
let pix_fmt = stream.get_pix_fmt_name();
let nb_frames = stream.get_nb_frames();
match context.get_stream_type(index as isize) {
AVMediaType::AVMEDIA_TYPE_VIDEO => {
let width = stream.get_width();
let height = stream.get_height();
let display_aspect_ratio = stream.get_display_aspect_ratio();
let frame_rate = stream.get_frame_rate();
let scanning_type = stream.get_scanning_type();
let chroma_subsampling = stream.get_chroma_sub_sample();
let level = stream.get_level();
let profile = stream.get_profile();
let timecode = stream.get_timecode();
let pix_fmt = stream.get_pix_fmt_name();
let nb_frames = stream.get_nb_frames();

vp = Some(VideoProperties {
width,
height,
display_aspect_ratio,
frame_rate,
level,
profile,
scanning_type,
chroma_subsampling,
timecode,
pix_fmt,
nb_frames,
});
}
AVMediaType::AVMEDIA_TYPE_AUDIO => {
let channels = stream.get_channels();
vp = Some(VideoProperties {
width,
height,
display_aspect_ratio,
frame_rate,
level,
profile,
scanning_type,
chroma_subsampling,
timecode,
pix_fmt,
nb_frames,
});
}
AVMediaType::AVMEDIA_TYPE_AUDIO => {
let channels = stream.get_channels();

let bits_per_sample = stream.get_bits_per_sample();
let sample_fmt = stream.get_sample_fmt();
let sample_rate = stream.get_sample_rate();
let bits_per_sample = stream.get_bits_per_sample();
let sample_fmt = stream.get_sample_fmt();
let sample_rate = stream.get_sample_rate();

ap = Some(AudioProperties {
channels,
sample_rate,
sample_fmt,
bits_per_sample,
});
ap = Some(AudioProperties {
channels,
sample_rate,
sample_fmt,
bits_per_sample,
});
}
_ => {}
}
_ => {}
}

streams.push(StreamDescriptor {
index,
stream_type,
codec_name,
codec_long_name,
codec_tag,
start_time,
duration,
bit_rate,
stream_metadata,
video_properties: vp,
audio_properties: ap,
})
streams.push(StreamDescriptor {
index,
stream_type,
codec_name,
codec_long_name,
codec_tag,
start_time,
duration,
bit_rate,
stream_metadata,
video_properties: vp,
audio_properties: ap,
})
}
}
}

Expand Down
20 changes: 11 additions & 9 deletions src/probe/sine_detect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,17 @@ pub fn detect_sine(
}
let detected_sine = streams[index as usize].detected_sine.as_mut().unwrap();

if let Ok(stream) = ContextStream::new(context.get_stream(index as isize)) {
if let AVMediaType::AVMEDIA_TYPE_AUDIO = context.get_stream_type(index as isize) {
let sample_fmt = stream.get_sample_fmt();
range_value = match sample_fmt.as_str() {
"s16" => i16::MAX as f64,
"s32" => i32::MAX as f64,
"s64" => i64::MAX as f64,
_ => 1.0,
};
unsafe {
if let Ok(stream) = ContextStream::new(context.get_stream(index as isize)) {
if let AVMediaType::AVMEDIA_TYPE_AUDIO = context.get_stream_type(index as isize) {
let sample_fmt = stream.get_sample_fmt();
range_value = match sample_fmt.as_str() {
"s16" => i16::MAX as f64,
"s32" => i32::MAX as f64,
"s64" => i64::MAX as f64,
_ => 1.0,
};
}
}
}

Expand Down
Loading