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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
pub mod ciede;
#[cfg(feature = "decode")]
mod decode;
mod pixel;
pub mod psnr;
pub mod psnr_hvs;
pub mod ssim;
use crate::MetricsError;
use std::error::Error;
#[cfg(feature = "decode")]
pub use decode::*;
pub use pixel::*;
#[derive(Clone, Debug)]
pub struct FrameInfo<T: Pixel> {
pub planes: [PlaneData<T>; 3],
pub bit_depth: usize,
pub chroma_sampling: ChromaSampling,
}
impl<T: Pixel> FrameInfo<T> {
pub(crate) fn can_compare(&self, other: &Self) -> Result<(), MetricsError> {
if self.bit_depth != other.bit_depth {
return Err(MetricsError::InputMismatch {
reason: "Bit depths do not match",
});
}
if self.bit_depth > 16 {
return Err(MetricsError::UnsupportedInput {
reason: "Bit depths above 16 are not supported",
});
}
if self.chroma_sampling != other.chroma_sampling {
return Err(MetricsError::InputMismatch {
reason: "Chroma subsampling offsets do not match",
});
}
self.planes[0].can_compare(&other.planes[0])?;
self.planes[1].can_compare(&other.planes[1])?;
self.planes[2].can_compare(&other.planes[2])?;
Ok(())
}
}
#[derive(Clone, Debug)]
pub struct PlaneData<T: Pixel> {
pub width: usize,
pub height: usize,
pub data: Vec<T>,
}
impl<T: Pixel> PlaneData<T> {
pub(crate) fn can_compare(&self, other: &Self) -> Result<(), MetricsError> {
if self.width != other.width || self.height != other.height {
return Err(MetricsError::InputMismatch {
reason: "Video resolution does not match",
});
}
Ok(())
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum ChromaSampling {
Cs420,
Cs422,
Cs444,
Cs400,
}
impl Default for ChromaSampling {
fn default() -> Self {
ChromaSampling::Cs420
}
}
impl ChromaSampling {
pub(crate) fn get_decimation(self) -> Option<(usize, usize)> {
use self::ChromaSampling::*;
match self {
Cs420 => Some((1, 1)),
Cs422 => Some((1, 0)),
Cs444 => Some((0, 0)),
Cs400 => None,
}
}
#[cfg(feature = "decode")]
pub(crate) fn get_chroma_dimensions(
self,
luma_width: usize,
luma_height: usize,
) -> (usize, usize) {
if let Some((ss_x, ss_y)) = self.get_decimation() {
((luma_width + ss_x) >> ss_x, (luma_height + ss_y) >> ss_y)
} else {
(0, 0)
}
}
pub(crate) fn get_chroma_weight(self) -> f64 {
match self {
ChromaSampling::Cs420 => 0.25,
ChromaSampling::Cs422 => 0.5,
ChromaSampling::Cs444 => 1.0,
ChromaSampling::Cs400 => 0.0,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum ChromaSamplePosition {
Unknown,
Vertical,
Colocated,
Bilateral,
Interpolated,
}
impl Default for ChromaSamplePosition {
fn default() -> Self {
ChromaSamplePosition::Unknown
}
}
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct PlanarMetrics {
pub y: f64,
pub u: f64,
pub v: f64,
pub avg: f64,
}
trait VideoMetric {
type FrameResult;
type VideoResult;
#[cfg(feature = "decode")]
fn process_video<D: Decoder>(
&mut self,
decoder1: &mut D,
decoder2: &mut D,
frame_limit: Option<usize>,
) -> Result<Self::VideoResult, Box<dyn Error>> {
if decoder1.get_bit_depth() != decoder2.get_bit_depth() {
return Err(Box::new(MetricsError::InputMismatch {
reason: "Bit depths do not match",
}));
}
let mut metrics = Vec::with_capacity(frame_limit.unwrap_or(0));
let mut frame_no = 0;
while frame_limit.map(|limit| limit > frame_no).unwrap_or(true) {
if decoder1.get_bit_depth() > 8 {
let frame1 = decoder1.read_video_frame::<u16>();
let frame2 = decoder2.read_video_frame::<u16>();
if let Ok(frame1) = frame1 {
if let Ok(frame2) = frame2 {
metrics.push(self.process_frame(&frame1, &frame2)?);
frame_no += 1;
continue;
}
}
} else {
let frame1 = decoder1.read_video_frame::<u8>();
let frame2 = decoder2.read_video_frame::<u8>();
if let Ok(frame1) = frame1 {
if let Ok(frame2) = frame2 {
metrics.push(self.process_frame(&frame1, &frame2)?);
frame_no += 1;
continue;
}
}
}
break;
}
if frame_no == 0 {
return Err(MetricsError::UnsupportedInput {
reason: "No readable frames found in one or more input files",
}
.into());
}
self.aggregate_frame_results(&metrics)
}
fn process_frame<T: Pixel>(
&mut self,
frame1: &FrameInfo<T>,
frame2: &FrameInfo<T>,
) -> Result<Self::FrameResult, Box<dyn Error>>;
#[cfg(feature = "decode")]
fn aggregate_frame_results(
&self,
metrics: &[Self::FrameResult],
) -> Result<Self::VideoResult, Box<dyn Error>>;
}