Use macro for different bitdepths

This commit is contained in:
kageru 2020-02-19 20:17:16 +01:00
parent 13ac41ed97
commit a687dd90c4
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
2 changed files with 42 additions and 55 deletions

View File

@ -14,7 +14,7 @@ use vapoursynth::core::CoreRef;
use vapoursynth::format::SampleType; use vapoursynth::format::SampleType;
use vapoursynth::map::Map; use vapoursynth::map::Map;
use vapoursynth::node::Node; use vapoursynth::node::Node;
use vapoursynth::plugins::{Filter, FilterArgument, Metadata }; use vapoursynth::plugins::{Filter, FilterArgument, Metadata};
use vapoursynth::video_info::Property; use vapoursynth::video_info::Property;
pub const PLUGIN_NAME: &str = "adaptivegrain"; pub const PLUGIN_NAME: &str = "adaptivegrain";
@ -28,10 +28,7 @@ make_filter_function! {
clip: Node<'core>, clip: Node<'core>,
luma_scaling: Option<f64> luma_scaling: Option<f64>
) -> Result<Option<Box<dyn Filter<'core> + 'core>>, Error> { ) -> Result<Option<Box<dyn Filter<'core> + 'core>>, Error> {
let luma_scaling = match luma_scaling { let luma_scaling = luma_scaling.unwrap_or(10.0) as f32;
Some(i) => i as f32,
None => 10.0
};
if let Property::Constant(format) = clip.info().format { if let Property::Constant(format) = clip.info().format {
if !(format.sample_type() == SampleType::Float && format.bits_per_sample() != 32) { if !(format.sample_type() == SampleType::Float && format.bits_per_sample() != 32) {
return Ok(Some(Box::new(Mask { return Ok(Some(Box::new(Mask {

View File

@ -1,8 +1,6 @@
use super::PLUGIN_NAME; use super::PLUGIN_NAME;
use failure::Error; use failure::Error;
use num::{NumCast, ToPrimitive};
use std::fmt::Debug; use std::fmt::Debug;
use std::ops::Shl;
use std::ptr; use std::ptr;
use vapoursynth::core::CoreRef; use vapoursynth::core::CoreRef;
use vapoursynth::format::ColorFamily; use vapoursynth::format::ColorFamily;
@ -20,8 +18,8 @@ lazy_static! {
[0f32; 256] [0f32; 256]
.iter() .iter()
.enumerate() .enumerate()
.map(|(i, _f)| (i as f32) * (1.0 / 256.0)) .map(|(i, _f)| (i as f32) / 256.0)
.collect::<Vec<_>>() .collect()
}; };
} }
@ -41,33 +39,34 @@ fn from_property<T: Debug + Clone + Copy + Eq + PartialEq>(prop: Property<T>) ->
} }
} }
fn filter_for_int<T>( macro_rules! int_filter {
frame: &mut FrameRefMut, ($type:ty, $fname:ident) => {
src_frame: FrameRef, fn $fname(
depth: u8, frame: &mut FrameRefMut,
average: f32, src_frame: FrameRef,
luma_scaling: f32, depth: u8,
) where average: f32,
T: Component + NumCast + Clone + Shl<u8> + Debug, luma_scaling: f32,
<T as Shl<u8>>::Output: ToPrimitive, ) {
{ let max = ((1 << depth) - 1) as f32;
let max = ((1 << depth) - 1) as f32; let lut: Vec<$type> = FLOAT_RANGE
let lut: Vec<T> = FLOAT_RANGE .iter()
.iter() .map(|x| (get_mask_value(*x, average, luma_scaling) * max) as $type)
.map(|x| T::from(get_mask_value(*x, average, luma_scaling) * max).unwrap()) .collect();
.collect(); for row in 0..frame.height(0) {
for row in 0..frame.height(0) { for (pixel, src_pixel) in frame
for (pixel, src_pixel) in frame .plane_row_mut::<$type>(0, row)
.plane_row_mut::<T>(0, row) .iter_mut()
.iter_mut() .zip(src_frame.plane_row::<$type>(0, row))
.zip(src_frame.plane_row::<T>(0, row).iter()) {
{ let i = (src_pixel.clone() >> (depth - 8)) as usize;
let i = <usize as NumCast>::from(src_pixel.clone()).unwrap() >> (depth - 8); unsafe {
unsafe { ptr::write(pixel, lut[i].clone());
ptr::write(pixel, lut[i].clone()); }
}
} }
} }
} };
} }
fn filter_for_float(frame: &mut FrameRefMut, src_frame: FrameRef, average: f32, luma_scaling: f32) { fn filter_for_float(frame: &mut FrameRefMut, src_frame: FrameRef, average: f32, luma_scaling: f32) {
@ -156,27 +155,18 @@ impl<'core> Filter<'core> for Mask<'core> {
SampleType::Integer => { SampleType::Integer => {
let depth = from_property(self.source.info().format).bits_per_sample(); let depth = from_property(self.source.info().format).bits_per_sample();
match depth { match depth {
0..=8 => filter_for_int::<u8>( 0..=8 => {
&mut frame, int_filter!(u8, filter_8bit);
src_frame, filter_8bit(&mut frame, src_frame, depth, average, self.luma_scaling)
depth, }
average, 9..=16 => {
self.luma_scaling, int_filter!(u16, filter_16bit);
), filter_16bit(&mut frame, src_frame, depth, average, self.luma_scaling)
9..=16 => filter_for_int::<u16>( }
&mut frame, 17..=32 => {
src_frame, int_filter!(u32, filter_32bit);
depth, filter_32bit(&mut frame, src_frame, depth, average, self.luma_scaling)
average, }
self.luma_scaling,
),
17..=32 => filter_for_int::<u32>(
&mut frame,
src_frame,
depth,
average,
self.luma_scaling,
),
_ => bail!(format!( _ => bail!(format!(
"{}: input depth {} not supported", "{}: input depth {} not supported",
PLUGIN_NAME, depth PLUGIN_NAME, depth