Debian Codegraph @

.cargo/vendor/av1-grain/src/diff/vapoursynth.rs

raw
1 use std::path::Path;
2
3 use anyhow::{bail, ensure, Result};
4 use num_rational::Rational64;
5 use v_frame::{frame::Frame, plane::Plane};
6 use vapoursynth::{
7 format::Format,
8 prelude::{ColorFamily, Environment, EvalFlags, FrameRef, Property},
9 video_info::VideoInfo,
10 };
11
12 use super::{ColorFormat, PixelFormat, VideoSource};
13
14 const OUTPUT_INDEX: i32 = 0i32;
15
16 pub(super) struct VapoursynthSource {
17 env: Environment,
18 }
19
20 impl VapoursynthSource {
21 pub(super) fn open(source: &Path) -> Result<Self> {
22 let mut env = Environment::new()?;
23 env.eval_file(source, EvalFlags::SetWorkingDir)?;
24
25 Ok(VapoursynthSource { env })
26 }
27 }
28
29 impl VideoSource for VapoursynthSource {
30 fn read_frame(&mut self, frameno: usize) -> Result<Option<Frame<u8>>> {
31 let (node, _) = self.env.get_output(OUTPUT_INDEX)?;
32 Ok(node
33 .get_frame(frameno)
34 .map(|frame| Some(vs_frame_to_v_frame(&frame)))?)
35 }
36
37 fn get_frame_count(&mut self) -> Result<usize> {
38 let info = vs_get_clip_info(&mut self.env)?;
39
40 let num_frames = {
41 if Property::Variable == info.format {
42 bail!("Cannot handle clips with varying format");
43 }
44 if Property::Variable == info.resolution {
45 bail!("Cannot handle clips with varying dimensions");
46 }
47 if Property::Variable == info.framerate {
48 bail!("Cannot handle clips with varying framerate");
49 }
50
51 info.num_frames
52 };
53
54 ensure!(num_frames > 0, "vapoursynth reported 0 frames");
55
56 Ok(num_frames)
57 }
58
59 fn get_frame_rate(&mut self) -> Result<Rational64> {
60 let info = vs_get_clip_info(&mut self.env)?;
61
62 match info.framerate {
63 Property::Variable => bail!("Cannot output clips with varying framerate"),
64 Property::Constant(fps) => Ok(Rational64::new(
65 fps.numerator as i64,
66 fps.denominator as i64,
67 )),
68 }
69 }
70
71 fn get_resolution(&mut self) -> Result<(u32, u32)> {
72 let info = vs_get_clip_info(&mut self.env)?;
73
74 let resolution = {
75 match info.resolution {
76 Property::Variable => {
77 bail!("Cannot output clips with variable resolution");
78 }
79 Property::Constant(x) => x,
80 }
81 };
82
83 Ok((resolution.width as u32, resolution.height as u32))
84 }
85
86 fn get_pixel_format(&mut self) -> Result<ColorFormat> {
87 let info = vs_get_clip_info(&mut self.env)?;
88
89 ColorFormat::try_from(info.format)
90 }
91 }
92
93 impl TryFrom<Property<Format<'_>>> for ColorFormat {
94 type Error = anyhow::Error;
95
96 fn try_from(format: Property<Format>) -> Result<Self, Self::Error> {
97 match format {
98 Property::Variable => bail!("Variable pixel format not supported"),
99 Property::Constant(x) => ColorFormat::try_from(x),
100 }
101 }
102 }
103
104 impl TryFrom<Format<'_>> for ColorFormat {
105 type Error = anyhow::Error;
106
107 fn try_from(format: Format) -> Result<Self, Self::Error> {
108 Ok(match format.color_family() {
109 ColorFamily::Gray => ColorFormat {
110 pixel_format: PixelFormat::YUV400,
111 bit_depth: format.bits_per_sample(),
112 },
113 ColorFamily::YUV => ColorFormat {
114 pixel_format: match format.sub_sampling_h() + format.sub_sampling_w() {
115 0 => PixelFormat::YUV444,
116 1 => PixelFormat::YUV422,
117 2 => PixelFormat::YUV420,
118 _ => unreachable!(),
119 },
120 bit_depth: format.bits_per_sample(),
121 },
122 _ => bail!("Only YUV clips are supported"),
123 })
124 }
125 }
126
127 fn vs_get_clip_info(env: &mut Environment) -> Result<VideoInfo> {
128 // Get the output node.
129 let (node, _) = env.get_output(OUTPUT_INDEX)?;
130
131 Ok(node.info())
132 }
133
134 fn vs_frame_to_v_frame(in_frame: &FrameRef) -> Frame<u8> {
135 let mut planes = [
136 Plane::new(0, 0, 0, 0, 0, 0),
137 Plane::new(0, 0, 0, 0, 0, 0),
138 Plane::new(0, 0, 0, 0, 0, 0),
139 ];
140 let format =
141 ColorFormat::try_from(in_frame.format()).expect("Color format has already been checked");
142 for p in 0..format.pixel_format.planes() {
143 let xdec = if p > 0 {
144 format.pixel_format.subsampling().0
145 } else {
146 0
147 };
148 let ydec = if p > 0 {
149 format.pixel_format.subsampling().1
150 } else {
151 0
152 };
153 let mut plane: Plane<u8> = Plane::new(
154 in_frame.width(p) as usize >> xdec,
155 in_frame.height(p) as usize >> ydec,
156 xdec,
157 ydec,
158 0usize,
159 0usize,
160 );
161
162 if format.bit_depth == 8 {
163 let in_data: &[u8] = in_frame.plane(p).unwrap();
164 assert!(plane.data.len() == in_data.len());
165 plane.data_origin_mut().copy_from_slice(in_data);
166 } else {
167 let in_data: &[u16] = in_frame.plane(p).unwrap();
168 assert!(plane.data.len() == in_data.len());
169 in_data
170 .iter()
171 .zip(plane.data_origin_mut().iter_mut())
172 .for_each(|(i, o)| {
173 *o = (i >> (format.bit_depth - 8)) as u8;
174 });
175 }
176
177 planes[p] = plane;
178 }
179 Frame { planes }
180 }