| 1 |
use std::{mem::size_of, path::Path, slice}; |
| 2 |
|
| 3 |
use anyhow::{bail, ensure}; |
| 4 |
use num_rational::Rational32; |
| 5 |
use v_frame::{ |
| 6 |
frame::Frame, |
| 7 |
pixel::{ChromaSampling, Pixel}, |
| 8 |
}; |
| 9 |
use vapoursynth::{ |
| 10 |
video_info::{Property, VideoInfo}, |
| 11 |
vsscript::{Environment, EvalFlags}, |
| 12 |
}; |
| 13 |
|
| 14 |
use crate::decoder::VideoDetails; |
| 15 |
|
| 16 |
const OUTPUT_INDEX: i32 = 0; |
| 17 |
|
| 18 |
pub struct VapoursynthDecoder { |
| 19 |
env: Environment, |
| 20 |
frames_read: usize, |
| 21 |
total_frames: usize, |
| 22 |
} |
| 23 |
|
| 24 |
impl VapoursynthDecoder { |
| 25 |
/// # Errors |
| 26 |
/// |
| 27 |
/// - If sourcing an invalid Vapoursynth script. |
| 28 |
/// - If using a Vapoursynth script that contains an unsupported video |
| 29 |
/// format. |
| 30 |
#[inline] |
| 31 |
pub fn new(source: &Path) -> anyhow::Result<VapoursynthDecoder> { |
| 32 |
let env = Environment::from_file(source, EvalFlags::SetWorkingDir)?; |
| 33 |
let total_frames = { |
| 34 |
let (node, _) = env.get_output(OUTPUT_INDEX)?; |
| 35 |
get_num_frames(node.info())? |
| 36 |
}; |
| 37 |
Ok(Self { |
| 38 |
env, |
| 39 |
frames_read: 0, |
| 40 |
total_frames, |
| 41 |
}) |
| 42 |
} |
| 43 |
|
| 44 |
/// # Errors |
| 45 |
/// |
| 46 |
/// - If sourcing an invalid Vapoursynth script. |
| 47 |
/// - If using a Vapoursynth script that contains an unsupported video |
| 48 |
/// format. |
| 49 |
#[inline] |
| 50 |
pub fn get_video_details(&self) -> anyhow::Result<VideoDetails> { |
| 51 |
let (node, _) = self.env.get_output(OUTPUT_INDEX)?; |
| 52 |
let info = node.info(); |
| 53 |
let (width, height) = get_resolution(info)?; |
| 54 |
Ok(VideoDetails { |
| 55 |
width, |
| 56 |
height, |
| 57 |
bit_depth: get_bit_depth(info)?, |
| 58 |
chroma_sampling: get_chroma_sampling(info)?, |
| 59 |
time_base: get_time_base(info)?, |
| 60 |
}) |
| 61 |
} |
| 62 |
|
| 63 |
/// # Errors |
| 64 |
/// |
| 65 |
/// - If sourcing an invalid Vapoursynth script. |
| 66 |
/// - If using a Vapoursynth script that contains an unsupported video |
| 67 |
/// format. |
| 68 |
/// - If a frame cannot be read. |
| 69 |
#[allow(clippy::transmute_ptr_to_ptr)] |
| 70 |
#[inline] |
| 71 |
pub fn read_video_frame<T: Pixel>(&mut self, cfg: &VideoDetails) -> anyhow::Result<Frame<T>> { |
| 72 |
const SB_SIZE_LOG2: usize = 6; |
| 73 |
const SB_SIZE: usize = 1 << SB_SIZE_LOG2; |
| 74 |
const SUBPEL_FILTER_SIZE: usize = 8; |
| 75 |
const FRAME_MARGIN: usize = 16 + SUBPEL_FILTER_SIZE; |
| 76 |
const LUMA_PADDING: usize = SB_SIZE + FRAME_MARGIN; |
| 77 |
|
| 78 |
if self.frames_read >= self.total_frames { |
| 79 |
bail!("No frames left"); |
| 80 |
} |
| 81 |
|
| 82 |
let (node, _) = self.env.get_output(OUTPUT_INDEX)?; |
| 83 |
let vs_frame = node.get_frame(self.frames_read)?; |
| 84 |
self.frames_read += 1; |
| 85 |
|
| 86 |
let bytes = size_of::<T>(); |
| 87 |
let mut f: Frame<T> = |
| 88 |
Frame::new_with_padding(cfg.width, cfg.height, cfg.chroma_sampling, LUMA_PADDING); |
| 89 |
|
| 90 |
// SAFETY: We are using the stride to compute the length of the data slice |
| 91 |
unsafe { |
| 92 |
f.planes[0].copy_from_raw_u8( |
| 93 |
slice::from_raw_parts( |
| 94 |
vs_frame.data_ptr(0), |
| 95 |
vs_frame.stride(0) * vs_frame.height(0), |
| 96 |
), |
| 97 |
vs_frame.stride(0), |
| 98 |
bytes, |
| 99 |
); |
| 100 |
f.planes[1].copy_from_raw_u8( |
| 101 |
slice::from_raw_parts( |
| 102 |
vs_frame.data_ptr(1), |
| 103 |
vs_frame.stride(1) * vs_frame.height(1), |
| 104 |
), |
| 105 |
vs_frame.stride(1), |
| 106 |
bytes, |
| 107 |
); |
| 108 |
f.planes[2].copy_from_raw_u8( |
| 109 |
slice::from_raw_parts( |
| 110 |
vs_frame.data_ptr(2), |
| 111 |
vs_frame.stride(2) * vs_frame.height(2), |
| 112 |
), |
| 113 |
vs_frame.stride(2), |
| 114 |
bytes, |
| 115 |
); |
| 116 |
} |
| 117 |
Ok(f) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/// Get the number of frames from a Vapoursynth `VideoInfo` struct. |
| 122 |
fn get_num_frames(info: VideoInfo) -> anyhow::Result<usize> { |
| 123 |
let num_frames = { |
| 124 |
if Property::Variable == info.format { |
| 125 |
bail!("Cannot output clips with varying format"); |
| 126 |
} |
| 127 |
if Property::Variable == info.resolution { |
| 128 |
bail!("Cannot output clips with varying dimensions"); |
| 129 |
} |
| 130 |
if Property::Variable == info.framerate { |
| 131 |
bail!("Cannot output clips with varying framerate"); |
| 132 |
} |
| 133 |
|
| 134 |
info.num_frames |
| 135 |
}; |
| 136 |
|
| 137 |
ensure!(num_frames != 0, "vapoursynth reported 0 frames"); |
| 138 |
|
| 139 |
Ok(num_frames) |
| 140 |
} |
| 141 |
|
| 142 |
/// Get the bit depth from a Vapoursynth `VideoInfo` struct. |
| 143 |
fn get_bit_depth(info: VideoInfo) -> anyhow::Result<usize> { |
| 144 |
let bits_per_sample = { |
| 145 |
match info.format { |
| 146 |
Property::Variable => { |
| 147 |
bail!("Cannot output clips with variable format"); |
| 148 |
} |
| 149 |
Property::Constant(x) => x.bits_per_sample(), |
| 150 |
} |
| 151 |
}; |
| 152 |
|
| 153 |
Ok(bits_per_sample as usize) |
| 154 |
} |
| 155 |
|
| 156 |
/// Get the resolution from a Vapoursynth `VideoInfo` struct. |
| 157 |
fn get_resolution(info: VideoInfo) -> anyhow::Result<(usize, usize)> { |
| 158 |
let resolution = { |
| 159 |
match info.resolution { |
| 160 |
Property::Variable => { |
| 161 |
bail!("Cannot output clips with variable resolution"); |
| 162 |
} |
| 163 |
Property::Constant(x) => x, |
| 164 |
} |
| 165 |
}; |
| 166 |
|
| 167 |
Ok((resolution.width, resolution.height)) |
| 168 |
} |
| 169 |
|
| 170 |
/// Get the time base (inverse of frame rate) from a Vapoursynth `VideoInfo` |
| 171 |
/// struct. |
| 172 |
fn get_time_base(info: VideoInfo) -> anyhow::Result<Rational32> { |
| 173 |
match info.framerate { |
| 174 |
Property::Variable => bail!("Cannot output clips with varying framerate"), |
| 175 |
Property::Constant(fps) => Ok(Rational32::new( |
| 176 |
fps.denominator as i32, |
| 177 |
fps.numerator as i32, |
| 178 |
)), |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/// Get the chroma sampling from a Vapoursynth `VideoInfo` struct. |
| 183 |
fn get_chroma_sampling(info: VideoInfo) -> anyhow::Result<ChromaSampling> { |
| 184 |
match info.format { |
| 185 |
Property::Variable => bail!("Variable pixel format not supported"), |
| 186 |
Property::Constant(x) => match x.color_family() { |
| 187 |
vapoursynth::format::ColorFamily::YUV => { |
| 188 |
let ss = (x.sub_sampling_w(), x.sub_sampling_h()); |
| 189 |
match ss { |
| 190 |
(1, 1) => Ok(ChromaSampling::Cs420), |
| 191 |
(1, 0) => Ok(ChromaSampling::Cs422), |
| 192 |
(0, 0) => Ok(ChromaSampling::Cs444), |
| 193 |
_ => bail!("Unrecognized chroma subsampling"), |
| 194 |
} |
| 195 |
} |
| 196 |
vapoursynth::format::ColorFamily::Gray => Ok(ChromaSampling::Cs400), |
| 197 |
_ => bail!("Currently only YUV input is supported"), |
| 198 |
}, |
| 199 |
} |
| 200 |
} |