1use codec::frame::{PixelFormat, StreamInfo};
9
10pub const MIN_RESOLUTION: u32 = 360;
12pub const MIN_FRAME_RATE: f64 = 15.0;
14pub const MAX_DURATION_SECS: f64 = 900.0;
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub enum ValidationErrorKind {
20 ResolutionTooSmall,
21 FrameRateTooSmall,
22 DurationTooLong,
23 UnsupportedPixelFormat,
24}
25
26#[derive(Debug, Clone)]
29pub struct ValidationError {
30 pub kind: ValidationErrorKind,
31 pub message: String,
32}
33
34impl std::fmt::Display for ValidationError {
35 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36 write!(f, "{}", self.message)
37 }
38}
39
40impl std::error::Error for ValidationError {}
41
42pub fn validate_stream(info: &StreamInfo) -> Result<(), ValidationError> {
46 if info.width < MIN_RESOLUTION || info.height < MIN_RESOLUTION {
47 return Err(ValidationError {
48 kind: ValidationErrorKind::ResolutionTooSmall,
49 message: format!(
50 "Video resolution {}x{} is below the minimum {}x{}.",
51 info.width, info.height, MIN_RESOLUTION, MIN_RESOLUTION
52 ),
53 });
54 }
55 if info.frame_rate < MIN_FRAME_RATE {
56 return Err(ValidationError {
57 kind: ValidationErrorKind::FrameRateTooSmall,
58 message: format!(
59 "Video frame rate {:.1} fps is below the minimum {:.0} fps.",
60 info.frame_rate, MIN_FRAME_RATE
61 ),
62 });
63 }
64 if info.duration > MAX_DURATION_SECS {
65 return Err(ValidationError {
66 kind: ValidationErrorKind::DurationTooLong,
67 message: format!(
68 "Video duration {:.0}s exceeds the maximum {}s.",
69 info.duration, MAX_DURATION_SECS
70 ),
71 });
72 }
73 if !matches!(
74 info.pixel_format,
75 PixelFormat::Yuv420p
76 | PixelFormat::Yuv420p10le
77 | PixelFormat::Yuv444p10le
78 | PixelFormat::Yuva444p10le
79 ) {
80 return Err(ValidationError {
81 kind: ValidationErrorKind::UnsupportedPixelFormat,
82 message: format!(
83 "Pixel format {} is not supported.",
84 info.pixel_format.as_ffmpeg_str()
85 ),
86 });
87 }
88 Ok(())
89}
90
91pub fn needs_chroma_downsample(format: PixelFormat) -> bool {
94 matches!(
95 format,
96 PixelFormat::Yuv444p10le | PixelFormat::Yuva444p10le | PixelFormat::Yuv444p
97 )
98}