From f2e986a4726fbcceab33d093874094ec8b11987c Mon Sep 17 00:00:00 2001 From: meh Date: Tue, 30 Nov 2021 16:30:27 +0100 Subject: [PATCH] feat: add basic imgutils support --- src/util/frame/video.rs | 4 ---- src/util/image.rs | 42 +++++++++++++++++++++++++++++++++++++++++ src/util/mod.rs | 1 + 3 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 src/util/image.rs diff --git a/src/util/frame/video.rs b/src/util/frame/video.rs index c968036..f568585 100644 --- a/src/util/frame/video.rs +++ b/src/util/frame/video.rs @@ -318,10 +318,6 @@ impl Video { ) } } - - pub fn as_image(&self) -> crate::util::Image { - crate::util::Image(self) - } } impl Deref for Video { diff --git a/src/util/image.rs b/src/util/image.rs new file mode 100644 index 0000000..82c7714 --- /dev/null +++ b/src/util/image.rs @@ -0,0 +1,42 @@ +use crate::{format::Pixel, sys::*, util::frame, Error}; + +pub fn buffer_size(format: Pixel, width: u32, height: u32, align: u32) -> crate::Result { + unsafe { + match av_image_get_buffer_size(format.into(), width as i32, height as i32, align as i32) { + e if e < 0 => Err(Error::from(e)), + size => Ok(size as usize), + } + } +} + +pub fn to_buffer(source: &frame::Video) -> crate::Result> { + let mut buffer = Vec::new(); + into_buffer(source, &mut buffer)?; + + Ok(buffer) +} + +pub fn into_buffer(source: &frame::Video, buffer: &mut Vec) -> crate::Result<()> { + let size = buffer_size(source.format(), source.width(), source.height(), 1)?; + buffer.resize(size, 0); + + let written = unsafe { + match av_image_copy_to_buffer( + buffer.as_mut_ptr(), + size as i32, + (*source.as_ptr()).data.as_ptr() as *mut _, + (*source.as_ptr()).linesize.as_ptr() as *mut _, + source.format().into(), + source.width() as i32, + source.height() as i32, + 1, + ) { + e if e < 0 => Err(Error::from(e)), + size => Ok(size), + } + }?; + + buffer.shrink_to(written as usize); + + Ok(()) +} diff --git a/src/util/mod.rs b/src/util/mod.rs index 228b925..1f41c13 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -14,6 +14,7 @@ pub mod picture; pub mod range; pub mod rational; pub mod time; +pub mod image; #[cfg(feature = "log")] pub mod log;