use std::borrow::Cow;
use std::ffi::{CStr, CString};
use std::marker::PhantomData;
use std::os::raw::{c_char, c_void};
use std::process;
use std::ptr::NonNull;
use std::{mem, panic};
use vapoursynth_sys as ffi;
use crate::api::API;
use crate::frame::FrameRef;
use crate::plugins::FrameContext;
use crate::video_info::VideoInfo;
mod errors;
pub use self::errors::GetFrameError;
#[derive(Debug)]
pub struct Node<'core> {
handle: NonNull<ffi::VSNode>,
_owner: PhantomData<&'core ()>,
}
unsafe impl<'core> Send for Node<'core> {}
unsafe impl<'core> Sync for Node<'core> {}
impl<'core> Drop for Node<'core> {
#[inline]
fn drop(&mut self) {
unsafe {
API::get_cached().free_node(self.handle.as_ptr());
}
}
}
impl<'core> Clone for Node<'core> {
#[inline]
fn clone(&self) -> Self {
let handle = unsafe { API::get_cached().clone_node(self.handle.as_ptr()) };
Self {
handle: unsafe { NonNull::new_unchecked(handle) },
_owner: PhantomData,
}
}
}
impl<'core> Node<'core> {
#[inline]
pub(crate) unsafe fn from_ptr(handle: *mut ffi::VSNode) -> Self {
Self {
handle: NonNull::new_unchecked(handle),
_owner: PhantomData,
}
}
#[inline]
pub(crate) fn ptr(&self) -> *mut ffi::VSNode {
self.handle.as_ptr()
}
#[inline]
pub fn info(&self) -> VideoInfo<'core> {
unsafe {
let ptr = API::get_cached().get_video_info(self.handle.as_ptr());
VideoInfo::from_ptr(ptr)
}
}
pub fn get_frame<'error>(&self, n: usize) -> Result<FrameRef<'core>, GetFrameError<'error>> {
assert!(n <= i32::MAX as usize);
let vi = &self.info();
if n >= vi.num_frames {
let err_cstring = CString::new("Requested frame number beyond the last one").unwrap();
return Err(GetFrameError::new(Cow::Owned(err_cstring)));
}
const ERROR_BUF_CAPACITY: usize = 32 * 1024;
let mut err_buf = vec![0; ERROR_BUF_CAPACITY].into_boxed_slice();
let handle =
unsafe { API::get_cached().get_frame(n as i32, self.handle.as_ptr(), &mut err_buf) };
if handle.is_null() {
let error = unsafe { CStr::from_ptr(err_buf.as_ptr()) }.to_owned();
Err(GetFrameError::new(Cow::Owned(error)))
} else {
Ok(unsafe { FrameRef::from_ptr(handle) })
}
}
pub fn get_frame_async<F>(&self, n: usize, callback: F)
where
F: FnOnce(Result<FrameRef<'core>, GetFrameError>, usize, Node<'core>) + Send + 'core,
{
struct CallbackData<'core> {
callback: Box<dyn CallbackFn<'core> + 'core>,
}
trait CallbackFn<'core> {
fn call(
self: Box<Self>,
frame: Result<FrameRef<'core>, GetFrameError>,
n: usize,
node: Node<'core>,
);
}
impl<'core, F> CallbackFn<'core> for F
where
F: FnOnce(Result<FrameRef<'core>, GetFrameError>, usize, Node<'core>),
{
#[allow(clippy::boxed_local)]
fn call(
self: Box<Self>,
frame: Result<FrameRef<'core>, GetFrameError>,
n: usize,
node: Node<'core>,
) {
(self)(frame, n, node)
}
}
unsafe extern "C" fn c_callback(
user_data: *mut c_void,
frame: *const ffi::VSFrame,
n: i32,
node: *mut ffi::VSNode,
error_msg: *const c_char,
) {
let user_data = Box::from_raw(user_data as *mut CallbackData<'static>);
let closure = panic::AssertUnwindSafe(move || {
let frame = if frame.is_null() {
debug_assert!(!error_msg.is_null());
let error_msg = Cow::Borrowed(CStr::from_ptr(error_msg));
Err(GetFrameError::new(error_msg))
} else {
debug_assert!(error_msg.is_null());
Ok(FrameRef::from_ptr(frame))
};
let node = Node::from_ptr(node);
debug_assert!(n >= 0);
let n = n as usize;
user_data.callback.call(frame, n, node);
});
if panic::catch_unwind(closure).is_err() {
process::abort();
}
}
assert!(n <= i32::MAX as usize);
let n = n as i32;
let user_data = Box::new(CallbackData {
callback: Box::new(callback),
});
let new_node = self.clone();
unsafe {
API::get_cached().get_frame_async(
n,
new_node.handle.as_ptr(),
Some(c_callback),
Box::into_raw(user_data) as *mut c_void,
);
}
mem::forget(new_node);
}
pub fn request_frame_filter(&self, context: FrameContext, n: usize) {
assert!(n <= i32::MAX as usize);
let n = n as i32;
unsafe {
API::get_cached().request_frame_filter(n, self.ptr(), context.ptr());
}
}
pub fn get_frame_filter(&self, context: FrameContext, n: usize) -> Option<FrameRef<'core>> {
assert!(n <= i32::MAX as usize);
let n = n as i32;
let ptr = unsafe { API::get_cached().get_frame_filter(n, self.ptr(), context.ptr()) };
if ptr.is_null() {
None
} else {
Some(unsafe { FrameRef::from_ptr(ptr) })
}
}
}