You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.0 KiB
Rust

use crate::av::as_ptr::{AsMutPtr, AsPtr};
use crate::av::codec_parameters::CodecParameters;
use crate::av::decoder::Decoder;
use crate::av::decoder_selector::DecoderSelector;
use crate::av::encoder::Encoder;
use crate::av::encoder_selector::EncoderSelector;
use crate::av::format::Format;
use crate::av::xcoder::XCoder;
use crate::av::{get_best_decoder, get_best_encoder, Rational};
use ffmpeg_sys_next::{av_guess_frame_rate, AVCodecID, AVRational, AVStream};
use num_rational::Ratio;
use std::ptr::null_mut;
pub struct Stream(pub *mut AVStream);
impl Stream {
pub fn as_ptr(&self) -> *const AVStream {
self.0
}
pub fn decoder(&self, hw_accel: Option<bool>) -> Option<Decoder> {
get_best_decoder(self.codec(), hw_accel)
.and_then(|decoder_name| Decoder::from_name(&decoder_name))
}
pub fn decoder_select(
&self,
hw_accel: Option<bool>,
select_fn: impl Fn(&Decoder) -> Result<(), String>,
) -> Result<Decoder, String> {
DecoderSelector::for_codec_id(self.codec()).select(hw_accel, select_fn)
}
pub fn encoder(&self, hw_accel: Option<bool>) -> Option<Encoder> {
let encoder = get_best_encoder(self.codec(), hw_accel);
encoder.and_then(|encoder_name| Encoder::from_name(&encoder_name))
}
pub fn encoder_select(
&self,
hw_accel: Option<bool>,
select_fn: impl Fn(&Encoder) -> Result<(), String>,
) -> Result<Encoder, String> {
EncoderSelector::for_codec_id(self.codec()).select(hw_accel, select_fn)
}
#[inline]
pub fn index(&self) -> i32 {
unsafe { (*self.0).index }
}
#[inline]
pub fn set_index(&self, index: i32) {
unsafe { (*self.0).index = index }
}
pub fn container_id(&self) -> i32 {
unsafe { (*self.0).id }
}
#[inline]
pub fn codec(&self) -> AVCodecID {
unsafe { (*(*self.0).codec).codec_id }
}
pub fn params(&self) -> CodecParameters {
unsafe { CodecParameters((*self.0).codecpar) }
}
#[inline]
pub fn time_base(&self) -> Ratio<i32> {
unsafe { (*self.0).time_base.to_num() }
}
#[inline]
pub fn set_time_base<R: Rational>(&self, time_base: R) {
unsafe { (*self.0).time_base = time_base.to_av() }
}
#[inline]
pub fn sample_aspect_ratio(&self) -> AVRational {
unsafe { (*self.as_ptr()).sample_aspect_ratio }
}
#[inline]
pub fn set_sample_aspect_ratio(&self, sample_aspect_ratio: impl Rational) {
unsafe { (*self.as_mut_ptr()).sample_aspect_ratio = sample_aspect_ratio.to_av() }
}
pub fn avg_frame_rate<T>(&self, fmt: &Format<T>) -> Option<impl Rational> {
Some(unsafe { av_guess_frame_rate(fmt.as_mut_ptr(), self.as_mut_ptr(), null_mut()) })
}
}
impl AsMutPtr<AVStream> for Stream {
#[inline]
fn as_mut_ptr(&self) -> *mut AVStream {
self.0
}
}
impl AsPtr<AVStream> for Stream {
#[inline]
fn as_ptr(&self) -> *const AVStream {
self.0
}
}