1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use std::num::{NonZeroU8, NonZeroUsize};
use anyhow::{Result, anyhow, bail};
use av1_grain::v_frame::frame::Frame;
use av1_grain::v_frame::pixel::Pixel;
use video_resize::algorithms::{
BicubicCatmullRom, BicubicHermite, BicubicMitchell, Lanczos3, Spline36,
};
use video_resize::{CropDimensions, ResizeDimensions, crop, resize};
pub struct FilterChain {
filters: Vec<Filter>,
}
impl FilterChain {
pub fn new(filters: &str) -> Result<Self> {
if filters.is_empty() {
return Ok(Self {
filters: Vec::new(),
});
}
let mut parsed = Vec::new();
for filter in filters.split(';') {
let (filter, args) = filter
.split_once(':')
.ok_or_else(|| anyhow!("Invalid filter syntax in \"{filter}\""))?;
let args = args.split(',');
match filter {
"crop" => {
let (mut top, mut bottom, mut left, mut right) = (0, 0, 0, 0);
for arg in args {
let (arg, value) = arg
.split_once('=')
.ok_or_else(|| anyhow!("Invalid filter syntax in \"{arg}\""))?;
match arg {
"top" => {
top = value.parse()?;
}
"bottom" => {
bottom = value.parse()?;
}
"left" => {
left = value.parse()?;
}
"right" => {
right = value.parse()?;
}
arg => bail!("Unrecognized crop arg \"{arg}\""),
}
}
parsed.push(Filter::Crop {
top,
bottom,
left,
right,
});
}
"resize" => {
let (mut width, mut height, mut alg) = (0, 0, "catmullrom");
for arg in args {
let (arg, value) = arg
.split_once('=')
.ok_or_else(|| anyhow!("Invalid filter syntax in \"{arg}\""))?;
match arg {
"width" => {
width = value.parse()?;
}
"height" => {
height = value.parse()?;
}
"alg" => match value {
"hermite" => {
alg = "hermite";
}
"catmullrom" => {
alg = "catmullrom";
}
"mitchell" => {
alg = "mitchell";
}
"lanczos" => {
alg = "lanczos";
}
"spline36" => {
alg = "spline36";
}
alg => bail!("Unrecognized resize algorithm \"{alg}\""),
},
arg => bail!("Unrecognized resize arg \"{arg}\""),
}
}
if width == 0 || height == 0 {
bail!("Both width and height must be provided to resize filter");
}
// SAFETY: checked above
unsafe {
parsed.push(Filter::Resize {
width: NonZeroUsize::new_unchecked(width),
height: NonZeroUsize::new_unchecked(height),
alg,
});
}
}
f => bail!("Unrecognized filter \"{f}\""),
}
}
Ok(Self { filters: parsed })
}
pub fn apply<T: Pixel>(&self, frame: Frame<T>, source_bd: NonZeroU8) -> Frame<T> {
self.filters
.iter()
.fold(frame, |prev, f| f.apply(&prev, source_bd))
}
}
enum Filter {
Crop {
top: usize,
bottom: usize,
left: usize,
right: usize,
},
Resize {
width: NonZeroUsize,
height: NonZeroUsize,
alg: &'static str,
},
}
impl Filter {
pub fn apply<T: Pixel>(&self, frame: &Frame<T>, source_bd: NonZeroU8) -> Frame<T> {
match *self {
Filter::Crop {
top,
bottom,
left,
right,
} => crop(
frame,
CropDimensions {
top,
bottom,
left,
right,
},
)
.unwrap(),
Filter::Resize { width, height, alg } => match alg {
"hermite" => resize::<T, BicubicHermite>(
frame,
ResizeDimensions { width, height },
source_bd,
)
.unwrap(),
"catmullrom" => resize::<T, BicubicCatmullRom>(
frame,
ResizeDimensions { width, height },
source_bd,
)
.unwrap(),
"mitchell" => resize::<T, BicubicMitchell>(
frame,
ResizeDimensions { width, height },
source_bd,
)
.unwrap(),
"lanczos" => {
resize::<T, Lanczos3>(frame, ResizeDimensions { width, height }, source_bd)
.unwrap()
}
"spline36" => {
resize::<T, Spline36>(frame, ResizeDimensions { width, height }, source_bd)
.unwrap()
}
_ => unreachable!(),
},
}
}
}