diff --git a/Cargo.toml b/Cargo.toml index 0fdd256..59fcc33 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,11 +6,8 @@ edition = "2018" [dependencies] failure = "0.1" -#faster = { git = "https://github.com/AdamNiederer/faster.git" } lazy_static = "1.3.0" num = "0.2" -rand = "0.6" -rand_xorshift = "0.1" vapoursynth = "0.2" vapoursynth-sys = "0.2" diff --git a/src/grain.rs b/src/grain.rs deleted file mode 100644 index a3d026b..0000000 --- a/src/grain.rs +++ /dev/null @@ -1,66 +0,0 @@ -use failure::Error; -use num::Integer; -use rand::distributions::Uniform; -use rand::Rng; -use rand_xorshift::XorShiftRng; -use vapoursynth::api::API; -use vapoursynth::core::CoreRef; -use vapoursynth::frame::{FrameRef, FrameRefMut}; -use vapoursynth::node::Node; -use vapoursynth::plugins::{Filter, FrameContext}; -use vapoursynth::video_info::VideoInfo; - -pub struct Grain<'core> { - pub source: Node<'core>, -} - -#[inline] -fn clip(input: T, lower: T, upper: T) -> T { - input.min(upper).max(lower) -} - -impl<'core> Filter<'core> for Grain<'core> { - fn video_info(&self, _api: API, _core: CoreRef<'core>) -> Vec> { - vec![self.source.info()] - } - - fn get_frame_initial( - &self, - _api: API, - _core: CoreRef<'core>, - context: FrameContext, - n: usize, - ) -> Result>, Error> { - self.source.request_frame_filter(context, n); - Ok(None) - } - - fn get_frame( - &self, - _api: API, - core: CoreRef<'core>, - context: FrameContext, - n: usize, - ) -> Result, Error> { - let frame = self.source.get_frame_filter(context, n).ok_or_else(|| { - format_err!("Could not retrieve source frame. This shouldn’t happen.") - })?; - let var = 20i16; - // these have to be defined explicitly for lifetime reasons - //let mut rng = thread_rng(); - let distribution = Uniform::new_inclusive(-var, var); - let mut rng: XorShiftRng = rand::SeedableRng::seed_from_u64(653334623); - let mut spread = rng.sample_iter(&distribution); - - let mut frame = FrameRefMut::copy_of(core, &frame); - for plane in 0..frame.format().plane_count() { - for row in 0..frame.height(plane) { - for mut_pixel in frame.plane_row_mut::(plane, row) { - let pixel = *mut_pixel as i16 + spread.next().unwrap(); - *mut_pixel = clip(pixel, 0, 255) as u8; - } - } - } - Ok(frame.into()) - } -} diff --git a/src/lib.rs b/src/lib.rs index 1f74814..a620348 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,101 +5,23 @@ extern crate lazy_static; #[macro_use] extern crate vapoursynth; -mod grain; mod mask; -use self::grain::Grain; use self::mask::Mask; use failure::Error; use vapoursynth::api::API; use vapoursynth::core::CoreRef; use vapoursynth::format::SampleType; -use vapoursynth::frame::{FrameRef, FrameRefMut}; use vapoursynth::map::Map; use vapoursynth::node::Node; -use vapoursynth::plugins::{Filter, FilterArgument, FrameContext, Metadata}; -use vapoursynth::video_info::{Property, VideoInfo}; +use vapoursynth::plugins::{Filter, FilterArgument, Metadata }; +use vapoursynth::video_info::Property; pub const PLUGIN_NAME: &str = "adaptivegrain"; pub const PLUGIN_IDENTIFIER: &str = "moe.kageru.adaptivegrain"; -struct AdaptiveGrain<'core> { - source: Node<'core>, -} - -impl<'core> Filter<'core> for AdaptiveGrain<'core> { - fn video_info(&self, _api: API, _core: CoreRef<'core>) -> Vec> { - vec![self.source.info()] - } - - fn get_frame_initial( - &self, - _api: API, - _core: CoreRef<'core>, - context: FrameContext, - n: usize, - ) -> Result>, Error> { - self.source.request_frame_filter(context, n); - Ok(None) - } - - fn get_frame( - &self, - _api: API, - core: CoreRef<'core>, - context: FrameContext, - n: usize, - ) -> Result, Error> { - let frame = self.source.get_frame_filter(context, n).ok_or_else(|| { - format_err!("Could not retrieve source frame. This shouldn’t happen.") - })?; - let average = match frame.props().get::("PlaneStatsAverage") { - Ok(average) => (average * 256.0) as u8, - Err(_) => panic!( - PLUGIN_NAME.to_owned() - + ": You need to run std.PlaneStats on the clip before calling this function." - ), - }; - - let mut frame = FrameRefMut::copy_of(core, &frame); - for plane in 0..frame.format().plane_count() { - for row in 0..frame.height(plane) { - for pixel in frame.plane_row_mut::(plane, row) { - *pixel = average; - } - } - } - Ok(frame.into()) - } -} - -make_filter_function! { - AdaptiveGrainFunction, "AdaptiveGrain" - - fn create_adaptivegrain<'core>( - _api: API, - _core: CoreRef<'core>, - clip: Node<'core>, - ) -> Result + 'core>>, Error> { - Ok(Some(Box::new(AdaptiveGrain { source: clip }))) - } -} - -make_filter_function! { - GrainFunction, "Grain" - - fn create_grain<'core>( - _api: API, - _core: CoreRef<'core>, - clip: Node<'core>, - ) -> Result + 'core>>, Error> { - Ok(Some(Box::new(Grain { source: clip }))) - } -} - make_filter_function! { MaskFunction, "Mask" - fn create_mask<'core>( _api: API, _core: CoreRef<'core>, @@ -131,17 +53,5 @@ export_vapoursynth_plugin! { name: "Adaptive grain", read_only: false, }, - [ - //AdaptiveGrainFunction::new(), - //GrainFunction::new(), - MaskFunction::new() - ] -} - -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - assert_eq!(2 + 2, 4); - } + [ MaskFunction::new() ] }