video-frame: Store buffer directly as part of the GstVideoFrame

There's no need to store it again separately, and this allows dropping
the usage of GST_VIDEO_FRAME_MAP_FLAG_NO_REF too.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1712>
This commit is contained in:
Sebastian Dröge 2025-04-10 15:49:53 +03:00
parent 8460ec94f4
commit e09b0f4e22

View file

@ -50,7 +50,6 @@ fn plane_buffer_info<T: IsVideoFrame>(
pub struct VideoFrame<T> { pub struct VideoFrame<T> {
frame: ffi::GstVideoFrame, frame: ffi::GstVideoFrame,
buffer: gst::Buffer,
phantom: PhantomData<T>, phantom: PhantomData<T>,
} }
@ -245,7 +244,7 @@ impl<T> VideoFrame<T> {
pub fn into_buffer(self) -> gst::Buffer { pub fn into_buffer(self) -> gst::Buffer {
unsafe { unsafe {
let mut s = mem::ManuallyDrop::new(self); let mut s = mem::ManuallyDrop::new(self);
let buffer = ptr::read(&s.buffer); let buffer = from_glib_none(s.frame.buffer);
ffi::gst_video_frame_unmap(&mut s.frame); ffi::gst_video_frame_unmap(&mut s.frame);
buffer buffer
} }
@ -326,10 +325,8 @@ impl<T> VideoFrame<T> {
#[inline] #[inline]
pub unsafe fn from_glib_full(frame: ffi::GstVideoFrame) -> Self { pub unsafe fn from_glib_full(frame: ffi::GstVideoFrame) -> Self {
let buffer = gst::Buffer::from_glib_none(frame.buffer);
Self { Self {
frame, frame,
buffer,
phantom: PhantomData, phantom: PhantomData,
} }
} }
@ -346,11 +343,8 @@ impl<T> VideoFrame<T> {
#[inline] #[inline]
pub fn into_raw(self) -> ffi::GstVideoFrame { pub fn into_raw(self) -> ffi::GstVideoFrame {
unsafe { let s = mem::ManuallyDrop::new(self);
let mut s = mem::ManuallyDrop::new(self); s.frame
ptr::drop_in_place(&mut s.buffer);
s.frame
}
} }
} }
@ -375,11 +369,15 @@ impl VideoFrame<Readable> {
unsafe { unsafe {
let mut frame = mem::MaybeUninit::uninit(); let mut frame = mem::MaybeUninit::uninit();
// Takes another reference of the buffer but only
// when successful, so we can safely return the buffer
// on failure and on success drop the additional
// reference.
let res: bool = from_glib(ffi::gst_video_frame_map( let res: bool = from_glib(ffi::gst_video_frame_map(
frame.as_mut_ptr(), frame.as_mut_ptr(),
info.to_glib_none().0 as *mut _, info.to_glib_none().0 as *mut _,
buffer.to_glib_none().0, buffer.to_glib_none().0,
ffi::GST_VIDEO_FRAME_MAP_FLAG_NO_REF | gst::ffi::GST_MAP_READ, gst::ffi::GST_MAP_READ,
)); ));
if !res { if !res {
@ -388,7 +386,6 @@ impl VideoFrame<Readable> {
let frame = frame.assume_init(); let frame = frame.assume_init();
Ok(Self { Ok(Self {
frame, frame,
buffer,
phantom: PhantomData, phantom: PhantomData,
}) })
} }
@ -407,12 +404,16 @@ impl VideoFrame<Readable> {
unsafe { unsafe {
let mut frame = mem::MaybeUninit::uninit(); let mut frame = mem::MaybeUninit::uninit();
// Takes another reference of the buffer but only
// when successful, so we can safely return the buffer
// on failure and on success drop the additional
// reference.
let res: bool = from_glib(ffi::gst_video_frame_map_id( let res: bool = from_glib(ffi::gst_video_frame_map_id(
frame.as_mut_ptr(), frame.as_mut_ptr(),
info.to_glib_none().0 as *mut _, info.to_glib_none().0 as *mut _,
buffer.to_glib_none().0, buffer.to_glib_none().0,
id, id,
ffi::GST_VIDEO_FRAME_MAP_FLAG_NO_REF | gst::ffi::GST_MAP_READ, gst::ffi::GST_MAP_READ,
)); ));
if !res { if !res {
@ -421,7 +422,6 @@ impl VideoFrame<Readable> {
let frame = frame.assume_init(); let frame = frame.assume_init();
Ok(Self { Ok(Self {
frame, frame,
buffer,
phantom: PhantomData, phantom: PhantomData,
}) })
} }
@ -446,13 +446,15 @@ impl VideoFrame<Writable> {
unsafe { unsafe {
let mut frame = mem::MaybeUninit::uninit(); let mut frame = mem::MaybeUninit::uninit();
// Takes another reference of the buffer but only
// when successful, so we can safely return the buffer
// on failure and on success drop the additional
// reference.
let res: bool = from_glib(ffi::gst_video_frame_map( let res: bool = from_glib(ffi::gst_video_frame_map(
frame.as_mut_ptr(), frame.as_mut_ptr(),
info.to_glib_none().0 as *mut _, info.to_glib_none().0 as *mut _,
buffer.to_glib_none().0, buffer.to_glib_none().0,
ffi::GST_VIDEO_FRAME_MAP_FLAG_NO_REF gst::ffi::GST_MAP_READ | gst::ffi::GST_MAP_WRITE,
| gst::ffi::GST_MAP_READ
| gst::ffi::GST_MAP_WRITE,
)); ));
if !res { if !res {
@ -461,7 +463,6 @@ impl VideoFrame<Writable> {
let frame = frame.assume_init(); let frame = frame.assume_init();
Ok(Self { Ok(Self {
frame, frame,
buffer,
phantom: PhantomData, phantom: PhantomData,
}) })
} }
@ -480,14 +481,16 @@ impl VideoFrame<Writable> {
unsafe { unsafe {
let mut frame = mem::MaybeUninit::uninit(); let mut frame = mem::MaybeUninit::uninit();
// Takes another reference of the buffer but only
// when successful, so we can safely return the buffer
// on failure and on success drop the additional
// reference.
let res: bool = from_glib(ffi::gst_video_frame_map_id( let res: bool = from_glib(ffi::gst_video_frame_map_id(
frame.as_mut_ptr(), frame.as_mut_ptr(),
info.to_glib_none().0 as *mut _, info.to_glib_none().0 as *mut _,
buffer.to_glib_none().0, buffer.to_glib_none().0,
id, id,
ffi::GST_VIDEO_FRAME_MAP_FLAG_NO_REF gst::ffi::GST_MAP_READ | gst::ffi::GST_MAP_WRITE,
| gst::ffi::GST_MAP_READ
| gst::ffi::GST_MAP_WRITE,
)); ));
if !res { if !res {
@ -496,7 +499,6 @@ impl VideoFrame<Writable> {
let frame = frame.assume_init(); let frame = frame.assume_init();
Ok(Self { Ok(Self {
frame, frame,
buffer,
phantom: PhantomData, phantom: PhantomData,
}) })
} }