Skip to content
Snippets Groups Projects
Commit a33d733f authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Improve safety yet again

parent 504f4e94
Branches
No related tags found
No related merge requests found
...@@ -6,17 +6,6 @@ use fraction::Fraction; ...@@ -6,17 +6,6 @@ use fraction::Fraction;
use crate::ffmpeg_api::enums::*; use crate::ffmpeg_api::enums::*;
// TODO: Use proper errors (with struct etc) for this
enum_from_primitive! {
#[derive(Debug, Copy, Clone, PartialEq)]
#[repr(i32)]
pub enum AVErrorKind {
Unknown = ffi::AVERROR_EXPERIMENTAL,
InputChanged = ffi::AVERROR_INPUT_CHANGED,
OutputChanged = ffi::AVERROR_OUTPUT_CHANGED
}
}
pub struct AVFormatContext { pub struct AVFormatContext {
base: *mut ffi::AVFormatContext, base: *mut ffi::AVFormatContext,
} }
...@@ -30,11 +19,6 @@ impl<'a> AVFormatContext { ...@@ -30,11 +19,6 @@ impl<'a> AVFormatContext {
Ok(AVFormatContext { base }) Ok(AVFormatContext { base })
} }
// TODO: Just for testing
pub unsafe fn raw(&self) -> *mut ffi::AVFormatContext {
self.base
}
pub fn open_input(&mut self, path: &str) -> Result<(), failure::Error> { pub fn open_input(&mut self, path: &str) -> Result<(), failure::Error> {
match unsafe { match unsafe {
ffi::avformat_open_input( ffi::avformat_open_input(
...@@ -64,6 +48,13 @@ impl<'a> AVFormatContext { ...@@ -64,6 +48,13 @@ impl<'a> AVFormatContext {
}) })
.collect(); .collect();
} }
pub fn read_frame(&/*TODO:mut*/ self, packet: &mut AVPacket) -> Result<(), failure::Error> {
match unsafe { ffi::av_read_frame(self.base, packet.base) } {
0 => Ok(()),
errno => Err(failure::format_err!("Error while decoding frame: {}", errno))
}
}
} }
impl Drop for AVFormatContext { impl Drop for AVFormatContext {
...@@ -116,11 +107,6 @@ impl AVPacket { ...@@ -116,11 +107,6 @@ impl AVPacket {
Ok(AVPacket { base }) Ok(AVPacket { base })
} }
// TODO: Just for testing
pub unsafe fn as_mut(&mut self) -> &mut ffi::AVPacket {
self.base.as_mut().expect("not null")
}
pub fn pts(&self) -> i64 { pub fn pts(&self) -> i64 {
let base = unsafe { self.base.as_ref() }.expect("not null"); let base = unsafe { self.base.as_ref() }.expect("not null");
...@@ -160,11 +146,6 @@ impl AVFrame { ...@@ -160,11 +146,6 @@ impl AVFrame {
Ok(AVFrame { base, buffer: AVBuffer::empty() }) Ok(AVFrame { base, buffer: AVBuffer::empty() })
} }
// TODO: Just for testing
pub unsafe fn as_mut(&mut self) -> &mut ffi::AVFrame {
self.base.as_mut().expect("not null")
}
pub fn init(&mut self, width: i32, height: i32, format: AVPixelFormat) -> Result<(), failure::Error> { pub fn init(&mut self, width: i32, height: i32, format: AVPixelFormat) -> Result<(), failure::Error> {
let mut base = unsafe { self.base.as_mut() }.expect("not null"); let mut base = unsafe { self.base.as_mut() }.expect("not null");
...@@ -345,11 +326,6 @@ impl<'a> AVCodecParameters<'a> { ...@@ -345,11 +326,6 @@ impl<'a> AVCodecParameters<'a> {
return AVCodecParameters { base, phantom: PhantomData }; return AVCodecParameters { base, phantom: PhantomData };
} }
// TODO: Just for testing
pub unsafe fn as_ref(&self) -> &ffi::AVCodecParameters {
self.base
}
pub fn codec_type(&self) -> AVMediaType { pub fn codec_type(&self) -> AVMediaType {
AVMediaType::from_i32(self.base.codec_type).unwrap_or(AVMediaType::Unknown) AVMediaType::from_i32(self.base.codec_type).unwrap_or(AVMediaType::Unknown)
} }
...@@ -376,11 +352,6 @@ impl<'a> AVCodec<'a> { ...@@ -376,11 +352,6 @@ impl<'a> AVCodec<'a> {
return AVCodec { base, phantom: PhantomData }; return AVCodec { base, phantom: PhantomData };
} }
// TODO: Just for testing
pub unsafe fn as_ref(&self) -> &ffi::AVCodec {
self.base
}
pub fn name(self: &AVCodec<'a>) -> std::string::String { pub fn name(self: &AVCodec<'a>) -> std::string::String {
String::from(unsafe { std::ffi::CStr::from_ptr(self.base.name) }.to_str().unwrap()) String::from(unsafe { std::ffi::CStr::from_ptr(self.base.name) }.to_str().unwrap())
} }
...@@ -399,9 +370,18 @@ impl AVCodecContext { ...@@ -399,9 +370,18 @@ impl AVCodecContext {
Ok(AVCodecContext { base }) Ok(AVCodecContext { base })
} }
// TODO: Just for testing pub fn in_packet(&mut self, packet: &mut AVPacket) -> Result<(), failure::Error> {
pub unsafe fn raw(&self) -> *mut ffi::AVCodecContext { match unsafe { ffi::avcodec_send_packet(self.base, packet.base) } {
self.base 0 => Ok(()),
errno => Err(failure::format_err!("Error while loading paclet: {}", errno))
}
}
pub fn out_frame(&mut self, frame: &mut AVFrame) -> Result<(), failure::Error> {
match unsafe { ffi::avcodec_receive_frame(self.base, frame.base) } {
0 => Ok(()),
errno => Err(failure::format_err!("Error while decoding frame: {}", errno))
}
} }
pub fn skip_loop_filter(&self) -> Option<AVDiscard> { pub fn skip_loop_filter(&self) -> Option<AVDiscard> {
......
use ffmpeg_dev::sys as ffi;
pub(crate) mod ffmpeg_api; pub(crate) mod ffmpeg_api;
use crate::ffmpeg_api::enums::*; use crate::ffmpeg_api::enums::*;
...@@ -63,17 +61,12 @@ fn main() -> Result<(), std::io::Error> { ...@@ -63,17 +61,12 @@ fn main() -> Result<(), std::io::Error> {
let mut scale_context = SwsContext::new(); let mut scale_context = SwsContext::new();
//TODO: HERE BE DRAGONS while avformat_context.read_frame(&mut packet).is_ok() && i < 16 {
while unsafe { ffi::av_read_frame(avformat_context.raw(), packet.as_mut()) } >= 0 && i < 16 {
// TODO: END DRAGONS
if packet.stream_index() == stream.index() { if packet.stream_index() == stream.index() {
codec_context.in_packet(&mut packet).unwrap_or_else(|error| {
//TODO: HERE BE DRAGONS panic!("Could not load packet: {:?}", error)
unsafe { ffi::avcodec_send_packet(codec_context.raw(), packet.as_mut()) }; });
while unsafe { ffi::avcodec_receive_frame(codec_context.raw(), frame.as_mut()) } >= 0 { while codec_context.out_frame(&mut frame).is_ok() {
// TODO: END DRAGONS
println!( println!(
"Frame {}: {:?} @ {}", "Frame {}: {:?} @ {}",
frame.coded_picture_number(), frame.coded_picture_number(),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment