diff --git a/src/codec/packet/packet.rs b/src/codec/packet/packet.rs index f1a10843..afd1eed4 100644 --- a/src/codec/packet/packet.rs +++ b/src/codec/packet/packet.rs @@ -213,13 +213,22 @@ impl Packet { } } - #[inline] - pub fn read(&mut self, format: &mut format::context::Input) -> Result<(), Error> { - unsafe { - match av_read_frame(format.as_mut_ptr(), self.as_mut_ptr()) { - 0 => Ok(()), - e => Err(Error::from(e)), - } + /// Read a packet from the input format context. + /// + /// # Safety + /// this is unsafe because you can potentially "read" a packet twice like so: + /// ```rust,ignore + /// let mut packet = Packet::empty(); + /// packet.read(&mut ictx)?; + /// packet.read(&mut ictx)?; + /// + /// ``` + /// it is recommended to use the input packet read api's + #[inline] + pub unsafe fn read(&mut self, format: &mut format::context::Input) -> Result<(), Error> { + match av_read_frame(format.as_mut_ptr(), self.as_mut_ptr()) { + 0 => Ok(()), + e => Err(Error::from(e)), } } diff --git a/src/format/context/input.rs b/src/format/context/input.rs index c73f985e..9b8b6d90 100644 --- a/src/format/context/input.rs +++ b/src/format/context/input.rs @@ -2,6 +2,8 @@ use std::ffi::CString; use std::mem; use std::ops::{Deref, DerefMut}; +use crate::packet::Mut; + use super::common::Context; use super::destructor; use ffi::*; @@ -136,6 +138,17 @@ impl Input { } } } + + #[inline] + pub fn next_packet(&mut self) -> Result { + let mut packet = Packet::empty(); + unsafe { + match av_read_frame(self.as_mut_ptr(), packet.as_mut_ptr()) { + 0 => Ok(packet), + e => Err(Error::from(e)), + } + } + } } impl Deref for Input { @@ -169,7 +182,7 @@ impl<'a> Iterator for PacketIter<'a> { let mut packet = Packet::empty(); loop { - match packet.read(self.context) { + match unsafe { packet.read(self.context) } { Ok(..) => unsafe { return Some(( Stream::wrap(mem::transmute_copy(&self.context), packet.stream()),