use crate::av::{verify_response, Rational}; use ffmpeg_sys_next::{ av_malloc, avcodec_parameters_copy, AVCodecID, AVCodecParameters, AVPixelFormat, AVRational, }; use std::mem; pub struct CodecParameters(pub *mut AVCodecParameters); impl CodecParameters { #[inline] pub fn as_ptr(&self) -> *const AVCodecParameters { self.0 } #[inline] pub fn as_mut_ptr(&self) -> *mut AVCodecParameters { self.0 } pub fn codec(&self) -> AVCodecID { unsafe { (*self.as_ptr()).codec_id } } #[inline] pub fn width(&self) -> i32 { unsafe { (*self.as_ptr()).width } } #[inline] pub fn set_width(&self, width: i32) { unsafe { (*self.as_mut_ptr()).width = width } } #[inline] pub fn height(&self) -> i32 { unsafe { (*self.as_ptr()).height } } #[inline] pub fn set_height(&self, height: i32) { unsafe { (*self.as_mut_ptr()).height = height } } #[inline] pub fn pixel_format(&self) -> AVPixelFormat { unsafe { mem::transmute((*self.as_ptr()).format) } } #[inline] pub fn set_pixel_format(&self, pixel_format: AVPixelFormat) { unsafe { (*self.as_mut_ptr()).format = mem::transmute(pixel_format) } } #[inline] pub fn extra_data(&self) -> Vec { unsafe { let extra_data_size = (*self.as_ptr()).extradata_size as usize; let mut extra_data = vec![0u8; extra_data_size]; (*self.as_ptr()) .extradata .copy_to(extra_data.as_mut_ptr(), extra_data_size); extra_data } } #[inline] pub fn set_extra_data(&self, extra_data: &Vec) { unsafe { (*self.as_mut_ptr()).extradata_size = 0; let new_extra_data: *mut u8 = av_malloc(extra_data.len() + 64).cast(); new_extra_data.copy_from(extra_data.as_ptr(), extra_data.len()); (*self.as_mut_ptr()).extradata = new_extra_data; (*self.as_mut_ptr()).extradata_size = extra_data.len() as i32; } } #[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 copy_to(&self, to: &CodecParameters) -> Result<(), String> { Self::copy(&self, to) } pub fn copy_from(&self, from: &CodecParameters) -> Result<(), String> { Self::copy(from, &self) } pub fn copy(from: &CodecParameters, to: &CodecParameters) -> Result<(), String> { verify_response("Failed to copy codec parameters", unsafe { avcodec_parameters_copy(to.as_mut_ptr(), from.as_ptr()) })?; Ok(()) } }