From 9379098a3fa31bdb7e4dd5e5ad86e46ec4741101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Sat, 3 Oct 2020 17:55:36 +0300 Subject: [PATCH] examples/appsrc: Make use of the VideoFrame API for accessing the data This is not really needed here but useful to have an example for the videoframe API usage. --- examples/src/bin/appsrc.rs | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/examples/src/bin/appsrc.rs b/examples/src/bin/appsrc.rs index b929751f3..a39fa6add 100644 --- a/examples/src/bin/appsrc.rs +++ b/examples/src/bin/appsrc.rs @@ -107,14 +107,31 @@ fn create_pipeline() -> Result { // When we want to access its content, we have to map it while requesting the required // mode of access (read, read/write). // See: https://gstreamer.freedesktop.org/documentation/plugin-development/advanced/allocation.html - let mut data = buffer.map_writable().unwrap(); + let mut vframe = + gst_video::VideoFrameRef::from_buffer_ref_writable(buffer, &video_info) + .unwrap(); - for p in data.as_mut_slice().chunks_mut(4) { - assert_eq!(p.len(), 4); - p[0] = b; - p[1] = g; - p[2] = r; - p[3] = 0; + // Remember some values from the frame for later usage + let width = vframe.width() as usize; + let height = vframe.height() as usize; + + // Each line of the first plane has this many bytes + let stride = vframe.plane_stride()[0] as usize; + + // Iterate over each of the height many lines of length stride + for line in vframe + .plane_data_mut(0) + .unwrap() + .chunks_exact_mut(stride) + .take(height) + { + // Iterate over each pixel of 4 bytes in that line + for pixel in line[..(4 * width)].chunks_exact_mut(4) { + pixel[0] = b; + pixel[1] = g; + pixel[2] = r; + pixel[3] = 0; + } } }