gl: Reset video frame size/stride/offset to 0 for GL mapped frames

The memory pointers are actually the GL texture IDs, and accessing them
like raw video memory will read random memory areas.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1173>
This commit is contained in:
Sebastian Dröge 2022-12-22 21:12:35 +02:00 committed by GStreamer Marge Bot
parent d6cc452cf3
commit dcfea9c35a
2 changed files with 20 additions and 10 deletions

View file

@ -18,7 +18,6 @@ rust-version = "1.63"
[dependencies]
bitflags = "1.0"
byteorder = "1"
libc = "0.2"
once_cell = "1.0"
ffi = { package = "gstreamer-gl-sys", path = "sys" }

View file

@ -3,7 +3,6 @@
use glib::translate::{from_glib, ToGlibPtr};
use gst_video::video_frame::Readable;
use byteorder::{NativeEndian, ReadBytesExt};
use std::mem;
pub trait VideoFrameGLExt {
@ -76,7 +75,14 @@ impl<'a> VideoFrameGLExt for gst_video::VideoFrameRef<&'a gst::BufferRef> {
if !res {
Err(buffer)
} else {
Ok(gst_video::VideoFrame::from_glib_full(frame.assume_init()))
let mut frame = frame.assume_init();
// Reset size/stride/offset to 0 as the memory pointers
// are the GL texture ID and accessing them would read
// random memory.
frame.info.size = 0;
frame.info.stride.fill(0);
frame.info.offset.fill(0);
Ok(gst_video::VideoFrame::from_glib_full(frame))
}
}
}
@ -117,9 +123,14 @@ impl<'a> VideoFrameGLExt for gst_video::VideoFrameRef<&'a gst::BufferRef> {
"Failed to fill in the values of GstVideoFrame"
))
} else {
Ok(gst_video::VideoFrameRef::from_glib_full(
frame.assume_init(),
))
let mut frame = frame.assume_init();
// Reset size/stride/offset to 0 as the memory pointers
// are the GL texture ID and accessing them would read
// random memory.
frame.info.size = 0;
frame.info.stride.fill(0);
frame.info.offset.fill(0);
Ok(gst_video::VideoFrameRef::from_glib_full(frame))
}
}
}
@ -136,10 +147,10 @@ impl<'a> VideoFrameGLExt for gst_video::VideoFrameRef<&'a gst::BufferRef> {
return None;
}
let mut data = self.plane_data(idx).ok()?;
let id = &data.read_u32::<NativeEndian>().ok()?;
Some(*id)
unsafe {
let ptr = (*self.as_ptr()).data[idx as usize] as *const u32;
Some(*ptr)
}
}
}