#![allow(clippy::missing_safety_doc)]
use crate::c;
use corevm_types::{flags::ReadVideoFrame, InputVideoFrameFormat, VideoMode};
#[inline]
pub fn video_input_mode() -> Option<VideoMode> {
let regs = unsafe { c::video_input_mode() };
VideoMode::from_regs(regs).ok()
}
#[cfg(feature = "alloc")]
pub fn try_read_video_frame(format: InputVideoFrameFormat) -> Option<alloc::vec::Vec<u8>> {
let len =
unsafe { c::read_video_frame(0, 0, format as u64, ReadVideoFrame::NON_BLOCKING.bits()) };
if len == u64::MAX {
return None;
}
let mut buf = alloc::vec::Vec::with_capacity(len as usize);
unsafe {
c::read_video_frame(
buf.as_mut_ptr() as u64,
len,
format as u64,
ReadVideoFrame::NON_BLOCKING.bits(),
)
};
unsafe { buf.set_len(len as usize) };
Some(buf)
}
#[inline]
pub fn try_read_video_frame_into(
buf: &mut [u8],
format: InputVideoFrameFormat,
) -> Option<Result<usize, usize>> {
let len = unsafe {
c::read_video_frame(
buf.as_mut_ptr() as u64,
buf.len() as u64,
format as u64,
ReadVideoFrame::NON_BLOCKING.bits(),
)
};
if len == u64::MAX {
return None;
}
if len > buf.len() as u64 {
return Some(Err(len as usize));
}
Some(Ok(len as usize))
}
#[cfg(feature = "alloc")]
pub fn read_video_frame(format: InputVideoFrameFormat) -> alloc::vec::Vec<u8> {
let len = unsafe { c::read_video_frame(0, 0, 0, 0) };
let mut buf = alloc::vec::Vec::with_capacity(len as usize);
unsafe { c::read_video_frame(buf.as_mut_ptr() as u64, len, format as u64, 0) };
unsafe { buf.set_len(len as usize) };
buf
}
#[inline]
pub fn read_video_frame_into(
buf: &mut [u8],
format: InputVideoFrameFormat,
) -> Result<usize, usize> {
let len =
unsafe { c::read_video_frame(buf.as_mut_ptr() as u64, buf.len() as u64, format as u64, 0) };
if len > buf.len() as u64 {
return Err(len as usize);
}
Ok(len as usize)
}