From 08d319ded7d8860235bf8a3e5be5b2ce083df45d Mon Sep 17 00:00:00 2001 From: nir benlulu Date: Wed, 4 Feb 2026 09:16:52 +0200 Subject: [PATCH 1/3] make packet.read unsafe and add a safe api in input context. --- src/codec/packet/packet.rs | 23 ++++++++++++++++------- src/format/context/input.rs | 15 ++++++++++++++- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/codec/packet/packet.rs b/src/codec/packet/packet.rs index f1a10843..6b4cd8fa 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: + /// ``` + /// let mut packet = Packet::new(); + /// 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()), From 446f10a10797760e4d60c7dfe6f38b518742b565 Mon Sep 17 00:00:00 2001 From: nir benlulu Date: Wed, 4 Feb 2026 09:23:02 +0200 Subject: [PATCH 2/3] ignore doctest --- src/codec/packet/packet.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codec/packet/packet.rs b/src/codec/packet/packet.rs index 6b4cd8fa..2c1b1031 100644 --- a/src/codec/packet/packet.rs +++ b/src/codec/packet/packet.rs @@ -217,7 +217,7 @@ impl Packet { /// /// # Safety /// this is unsafe because you can potentially "read" a packet twice like so: - /// ``` + /// ```rust,ignore /// let mut packet = Packet::new(); /// packet.read(&mut ictx)?; /// packet.read(&mut ictx)?; From 18c35b2a02a4dbf0646cee27f5f4bc327202fc0a Mon Sep 17 00:00:00 2001 From: nir benlulu Date: Wed, 4 Feb 2026 09:24:08 +0200 Subject: [PATCH 3/3] doc --- src/codec/packet/packet.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codec/packet/packet.rs b/src/codec/packet/packet.rs index 2c1b1031..afd1eed4 100644 --- a/src/codec/packet/packet.rs +++ b/src/codec/packet/packet.rs @@ -218,7 +218,7 @@ impl Packet { /// # Safety /// this is unsafe because you can potentially "read" a packet twice like so: /// ```rust,ignore - /// let mut packet = Packet::new(); + /// let mut packet = Packet::empty(); /// packet.read(&mut ictx)?; /// packet.read(&mut ictx)?; ///