alpha2_render/cpu/
decoded.rs1use std::sync::Arc;
4
5use alpha2_media::{ColorMetadata, DecodedVideoFrame, PixelFormat};
6
7use crate::{
8 ColorDiagnostic, ColorPolicy, CpuRenderer, CpuSource, RenderSize, RgbaPixels, SourceAlpha,
9 SourcePlane, SourcePresentation, YuvFrame, YuvPlanes,
10};
11
12pub fn refuse_hdr(color: ColorPolicy, frame_color: Option<ColorMetadata>) -> Result<(), String> {
17 if color == ColorPolicy::RejectHdr && frame_color.is_some_and(crate::is_hdr) {
18 return Err(
19 "the project refuses HDR sources (its color pipeline's hdr is \"reject\")".into(),
20 );
21 }
22 Ok(())
23}
24
25#[must_use]
27pub fn presentation(frame: &DecodedVideoFrame, size: RenderSize) -> SourcePresentation {
28 SourcePresentation {
29 output_size: size,
30 visible_rect: frame.visible_rect,
31 orientation: frame.orientation,
32 sample_aspect_ratio: (frame.sample_aspect_ratio.num, frame.sample_aspect_ratio.den),
33 chroma_siting: frame.chroma_location,
34 }
35}
36
37pub fn coded_size(frame: &DecodedVideoFrame) -> Result<RenderSize, String> {
42 let coded = frame
43 .planes
44 .first()
45 .ok_or_else(|| "the decoded frame has no planes".to_owned())?;
46 RenderSize::new(coded.width, coded.height).map_err(|error| error.to_string())
47}
48
49#[must_use]
51pub fn plane(frame: &DecodedVideoFrame, index: usize) -> SourcePlane<'_> {
52 frame.planes.get(index).map_or(
53 SourcePlane {
54 bytes: &[],
55 stride: 0,
56 },
57 |plane| SourcePlane {
58 bytes: &plane.bytes,
59 stride: plane.stride,
60 },
61 )
62}
63
64#[must_use]
66pub fn yuv_source(
67 frame: &DecodedVideoFrame,
68 format: alpha2_media::YuvFormat,
69 size: RenderSize,
70) -> YuvFrame<'_> {
71 let colors = format.color_plane_count();
72 let third = if colors == 3 {
74 plane(frame, 2)
75 } else {
76 SourcePlane {
77 bytes: &[],
78 stride: 0,
79 }
80 };
81 YuvFrame {
82 format,
83 planes: [plane(frame, 0), plane(frame, 1), third],
84 alpha_plane: format.alpha.then(|| plane(frame, colors)),
85 size,
86 alpha: 1.0,
87 color: frame.color,
88 }
89}
90
91#[must_use]
93pub fn skipped_hdr(diagnostics: &[ColorDiagnostic]) -> bool {
94 diagnostics
95 .iter()
96 .any(|diagnostic| matches!(diagnostic, ColorDiagnostic::UnsupportedHdr))
97}
98
99#[derive(Debug)]
100struct LentPlanes {
101 frame: Arc<DecodedVideoFrame>,
102 format: alpha2_media::YuvFormat,
103 size: RenderSize,
104}
105
106impl YuvPlanes for LentPlanes {
107 fn frame(&self) -> YuvFrame<'_> {
108 yuv_source(&self.frame, self.format, self.size)
109 }
110}
111
112#[derive(Debug)]
113struct LentRgba(Arc<DecodedVideoFrame>);
114
115impl RgbaPixels for LentRgba {
116 fn plane(&self) -> SourcePlane<'_> {
117 plane(&self.0, 0)
118 }
119}
120
121pub fn present_decoded(
127 renderer: &CpuRenderer,
128 size: RenderSize,
129 color: ColorPolicy,
130 frame: Arc<DecodedVideoFrame>,
131) -> Result<CpuSource, String> {
132 refuse_hdr(color, frame.color)?;
133 let coded = coded_size(&frame)?;
134 let presentation = presentation(&frame, size);
135 match frame.pixel_format {
136 PixelFormat::Yuv(format) => {
137 if frame.planes.len() != format.plane_count() {
138 return Err(format!(
139 "a YUV frame must contain {} planes",
140 format.plane_count()
141 ));
142 }
143 let planes = Arc::new(LentPlanes {
144 frame,
145 format,
146 size: coded,
147 });
148 let (source, diagnostics) = renderer
149 .present_yuv(planes, presentation, color)
150 .map_err(|error| error.to_string())?;
151 if skipped_hdr(&diagnostics) {
152 return Err("the project skips HDR sources".into());
153 }
154 Ok(source)
155 }
156 PixelFormat::Rgba8 | PixelFormat::LinearRgbaF16 => {
157 if frame.planes.len() != 1 {
158 return Err("an RGBA frame must contain one plane".into());
159 }
160 let stride = frame.planes[0].stride;
161 let linear = frame.pixel_format == PixelFormat::LinearRgbaF16;
162 let pixels = Arc::new(LentRgba(frame));
163 if linear {
164 renderer.present_linear_rgba16f(pixels, coded, stride, presentation)
165 } else {
166 renderer.present_rgba8(pixels, coded, stride, presentation, SourceAlpha::Straight)
167 }
168 .map_err(|error| error.to_string())
169 }
170 other => Err(format!("the decoder returned {other:?}")),
171 }
172}