1use alpha2_media::YuvEncoding;
5
6use super::half;
7use super::image::CpuImage;
8use super::workers::Workers;
9use crate::{ReadbackFailure, RenderError, Yuv420pTargets};
10
11#[derive(Copy, Clone)]
13struct Coding {
14 kr: f32,
15 kg: f32,
16 kb: f32,
17 luma_black: f32,
18 luma_span: f32,
19 chroma_center: f32,
20 chroma_span: f32,
21 cosited: [bool; 2],
22}
23
24impl Coding {
25 fn new(encoding: YuvEncoding) -> Self {
26 let values = crate::readback::yuv_uniform(encoding);
27 Self {
28 kr: values[0],
29 kg: values[1],
30 kb: values[2],
31 luma_black: values[4],
32 luma_span: values[5],
33 chroma_center: values[6],
34 chroma_span: values[7],
35 cosited: [values[8] != 0.0, values[9] != 0.0],
36 }
37 }
38
39 #[inline]
40 fn luma(self, rgb: [f32; 3]) -> f32 {
41 rgb[0] * self.kr + rgb[1] * self.kg + rgb[2] * self.kb
42 }
43
44 #[inline]
45 fn yuv(self, rgb: [f32; 3]) -> [f32; 3] {
46 let luma = self.luma(rgb);
47 [
48 self.luma_black + self.luma_span * luma,
49 self.chroma_center + self.chroma_span * (rgb[2] - luma) / (2.0 * (1.0 - self.kb)),
50 self.chroma_center + self.chroma_span * (rgb[0] - luma) / (2.0 * (1.0 - self.kr)),
51 ]
52 }
53}
54
55fn tap_weight(offset: i64, cosited: bool) -> f32 {
58 if cosited {
59 if offset == 0 { 0.5 } else { 0.25 }
60 } else if offset >= 0 {
61 0.5
62 } else {
63 0.0
64 }
65}
66
67fn chroma_taps(coding: Coding) -> Vec<(usize, usize, f32)> {
70 let mut taps = Vec::with_capacity(9);
71 for dy in -1..=1_i64 {
72 let row_weight = tap_weight(dy, coding.cosited[1]);
73 if row_weight == 0.0 {
74 continue;
75 }
76 for dx in -1..=1_i64 {
77 let column_weight = tap_weight(dx, coding.cosited[0]);
78 if column_weight == 0.0 {
79 continue;
80 }
81 #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
82 taps.push((
83 (dy + 1) as usize,
84 (dx + 1) as usize,
85 row_weight * column_weight,
86 ));
87 }
88 }
89 taps
90}
91
92fn chroma_row(
95 lines: &[Vec<[f32; 3]>; 3],
96 coding: Coding,
97 taps: &[(usize, usize, f32)],
98 u: &mut [u8],
99 v: &mut [u8],
100) {
101 for (column, (u, v)) in u.iter_mut().zip(v.iter_mut()).enumerate() {
102 let x = column * 2;
103 let mut rgb = [0.0_f32; 3];
104 if !coding.cosited[0] && !coding.cosited[1] {
105 for sample in [lines[1][x], lines[1][x + 1], lines[2][x], lines[2][x + 1]] {
106 for channel in 0..3 {
107 rgb[channel] += sample[channel];
108 }
109 }
110 rgb = rgb.map(|value| value * 0.25);
111 } else {
112 for &(line, offset, weight) in taps {
113 let sample = lines[line][(x + offset).saturating_sub(1)];
115 for channel in 0..3 {
116 rgb[channel] += sample[channel] * weight;
117 }
118 }
119 }
120 let yuv = coding.yuv(rgb);
121 *u = half::unorm8(yuv[1]);
122 *v = half::unorm8(yuv[2]);
123 }
124}
125
126pub(crate) fn pack_yuv420p(
131 workers: &Workers,
132 picture: &CpuImage,
133 encoding: YuvEncoding,
134 targets: &mut Yuv420pTargets<'_>,
135) -> Result<(), RenderError> {
136 crate::color::check_packable(encoding)?;
137 let size = picture.size();
138 if !size.width.is_multiple_of(2) || !size.height.is_multiple_of(2) {
139 return Err(RenderError::TextureReadback(
140 ReadbackFailure::UnsupportedTarget,
141 ));
142 }
143 let (width, height) = (size.width as usize, size.height as usize);
144 let (chroma_width, chroma_height) = (width / 2, height / 2);
145 targets.y.fits(width, height)?;
146 targets.u.fits(chroma_width, chroma_height)?;
147 targets.v.fits(chroma_width, chroma_height)?;
148 let coding = Coding::new(encoding);
149 let taps = chroma_taps(coding);
150 let (y_stride, u_stride, v_stride) = (targets.y.stride, targets.u.stride, targets.v.stride);
151 let mut lumas = targets.y.bytes.chunks_mut(y_stride);
153 let mut rows = Vec::with_capacity(chroma_height);
154 for (u, v) in targets
155 .u
156 .bytes
157 .chunks_mut(u_stride)
158 .zip(targets.v.bytes.chunks_mut(v_stride))
159 .take(chroma_height)
160 {
161 let top = lumas.next().expect("the luma plane fits");
162 let bottom = lumas.next().expect("the luma plane fits");
163 rows.push((top, bottom, u, v));
164 }
165 let scratch = || {
166 (
167 [
168 vec![[0.0_f32; 3]; width],
169 vec![[0.0_f32; 3]; width],
170 vec![[0.0_f32; 3]; width],
171 ],
172 vec![[0.0_f32; 4]; width],
173 )
174 };
175 workers.items_with(
177 &mut rows,
178 2 * width,
179 scratch,
180 |(lines, decoded), first, band| {
181 for line in lines.iter_mut() {
183 line.fill([0.0; 3]);
184 }
185 decoded.fill([0.0; 4]);
186 for (index, (top, bottom, u, v)) in band.iter_mut().enumerate() {
187 let row = first + index;
188 let first_line = usize::from(!coding.cosited[1]);
190 for (offset, line) in lines.iter_mut().enumerate().skip(first_line) {
191 let y = (row * 2 + offset).saturating_sub(1).min(height - 1);
192 #[allow(clippy::cast_possible_truncation)]
193 picture.decode_row(y as u32, 0, decoded);
194 for (rgb, texel) in line.iter_mut().zip(decoded.iter()) {
195 *rgb = [
196 texel[0].clamp(0.0, 1.0),
197 texel[1].clamp(0.0, 1.0),
198 texel[2].clamp(0.0, 1.0),
199 ];
200 }
201 }
202 for (luma_row, line) in [(&mut **top, &lines[1]), (&mut **bottom, &lines[2])] {
203 for (out, rgb) in luma_row[..width].iter_mut().zip(line) {
204 *out =
205 half::unorm8(coding.luma_black + coding.luma_span * coding.luma(*rgb));
206 }
207 }
208 chroma_row(
209 lines,
210 coding,
211 &taps,
212 &mut u[..chroma_width],
213 &mut v[..chroma_width],
214 );
215 }
216 },
217 );
218 Ok(())
219}
220
221impl super::CpuRenderer {
222 pub fn pack_image(
231 &self,
232 picture: &CpuImage,
233 encoding: YuvEncoding,
234 layout: alpha2_media::PixelLayout,
235 targets: &mut [crate::PlaneTarget<'_>],
236 ) -> Result<(), RenderError> {
237 let started = web_time::Instant::now();
238 let size = picture.size();
239 let row = |y: usize, out: &mut [[f32; 4]]| {
240 #[allow(clippy::cast_possible_truncation)]
241 picture.decode_row(y as u32, 0, out);
242 };
243 let rows = crate::layout_pack::Picture {
244 width: size.width as usize,
245 height: size.height as usize,
246 row: &row,
247 };
248 let packed = crate::layout_pack::pack_with(&rows, encoding, layout, targets, &self.workers);
249 super::renderer::add_elapsed(&self.stats.pack_micros, started);
250 packed
251 }
252}