Debian Codegraph @

debian/missing-sources/image/src/codecs/avif/ycgco.rs

raw
1 use crate::codecs::avif::yuv::{
2 check_rgb_preconditions, check_yuv_plane_preconditions, qrshr, yuv420_to_rgbx_invoker,
3 yuv422_to_rgbx_invoker, CbCrInverseTransform, HalvedRowHandler, PlaneDefinition,
4 YuvChromaRange, YuvIntensityRange, YuvPlanarImage, YuvStandardMatrix,
5 };
6 use crate::ImageError;
7 use num_traits::AsPrimitive;
8
9 /// Computes YCgCo inverse in limited range
10 /// # Arguments
11 /// - `dst` - dest buffer
12 /// - `y_value` - Y value with subtracted bias
13 /// - `cb` - Cb value with subtracted bias
14 /// - `cr` - Cr value with subtracted bias
15 #[inline(always)]
16 fn ycgco_execute_limited<
17 V: Copy + AsPrimitive<i32> + 'static + Sized,
18 const PRECISION: i32,
19 const CHANNELS: usize,
20 const BIT_DEPTH: usize,
21 >(
22 dst: &mut [V; CHANNELS],
23 y_value: i32,
24 cg: i32,
25 co: i32,
26 scale: i32,
27 ) where
28 i32: AsPrimitive<V>,
29 {
30 let t0 = y_value - cg;
31
32 let r = qrshr::<PRECISION, BIT_DEPTH>((t0 + co) * scale);
33 let b = qrshr::<PRECISION, BIT_DEPTH>((t0 - co) * scale);
34 let g = qrshr::<PRECISION, BIT_DEPTH>((y_value + cg) * scale);
35
36 if CHANNELS == 4 {
37 dst[0] = r.as_();
38 dst[1] = g.as_();
39 dst[2] = b.as_();
40 dst[3] = ((1i32 << BIT_DEPTH) - 1).as_();
41 } else if CHANNELS == 3 {
42 dst[0] = r.as_();
43 dst[1] = g.as_();
44 dst[2] = b.as_();
45 } else {
46 unreachable!();
47 }
48 }
49
50 /// Computes YCgCo inverse in full range
51 /// # Arguments
52 /// - `dst` - dest buffer
53 /// - `y_value` - Y value with subtracted bias
54 /// - `cb` - Cb value with subtracted bias
55 /// - `cr` - Cr value with subtracted bias
56 #[inline(always)]
57 fn ycgco_execute_full<
58 V: Copy + AsPrimitive<i32> + 'static + Sized,
59 const PRECISION: i32,
60 const CHANNELS: usize,
61 const BIT_DEPTH: usize,
62 >(
63 dst: &mut [V; CHANNELS],
64 y_value: i32,
65 cg: i32,
66 co: i32,
67 ) where
68 i32: AsPrimitive<V>,
69 {
70 let t0 = y_value - cg;
71
72 let max_value = (1i32 << BIT_DEPTH) - 1;
73
74 let r = (t0 + co).clamp(0, max_value);
75 let b = (t0 - co).clamp(0, max_value);
76 let g = (y_value + cg).clamp(0, max_value);
77
78 if CHANNELS == 4 {
79 dst[0] = r.as_();
80 dst[1] = g.as_();
81 dst[2] = b.as_();
82 dst[3] = max_value.as_();
83 } else if CHANNELS == 3 {
84 dst[0] = r.as_();
85 dst[1] = g.as_();
86 dst[2] = b.as_();
87 } else {
88 unreachable!();
89 }
90 }
91
92 #[inline(always)]
93 fn process_halved_chroma_row_cgco<
94 V: Copy + AsPrimitive<i32> + 'static + Sized,
95 const PRECISION: i32,
96 const CHANNELS: usize,
97 const BIT_DEPTH: usize,
98 >(
99 image: YuvPlanarImage<V>,
100 rgba: &mut [V],
101 _: &CbCrInverseTransform<i32>,
102 range: &YuvChromaRange,
103 ) where
104 i32: AsPrimitive<V>,
105 {
106 let max_value = (1i32 << BIT_DEPTH) - 1;
107
108 // If the stride is larger than the plane size,
109 // it might contain junk data beyond the actual valid region.
110 // To avoid processing artifacts when working with odd-sized images,
111 // the buffer is reshaped to its actual size,
112 // preventing accidental use of invalid values from the trailing region.
113
114 let y_plane = &image.y_plane[..image.width];
115 let chroma_size = image.width.div_ceil(2);
116 let u_plane = &image.u_plane[..chroma_size];
117 let v_plane = &image.v_plane[..chroma_size];
118 let rgba = &mut rgba[..image.width * CHANNELS];
119
120 let bias_y = range.bias_y as i32;
121 let bias_uv = range.bias_uv as i32;
122 let (y_iter, y_left) = y_plane.as_chunks::<2>();
123 let mut rgb_chunks = rgba.chunks_exact_mut(CHANNELS * 2);
124
125 let scale_coef = ((max_value as f32 / range.range_y as f32) * (1 << PRECISION) as f32) as i32;
126
127 for (((y_src, &u_src), &v_src), rgb_dst) in
128 y_iter.iter().zip(u_plane).zip(v_plane).zip(&mut rgb_chunks)
129 {
130 let y_value0: i32 = y_src[0].as_() - bias_y;
131 let cg_value: i32 = u_src.as_() - bias_uv;
132 let co_value: i32 = v_src.as_() - bias_uv;
133
134 let dst0 = &mut rgb_dst[..CHANNELS];
135
136 ycgco_execute_limited::<V, PRECISION, CHANNELS, BIT_DEPTH>(
137 dst0.try_into().unwrap(),
138 y_value0,
139 cg_value,
140 co_value,
141 scale_coef,
142 );
143
144 let y_value1 = y_src[1].as_() - bias_y;
145
146 let dst1 = &mut rgb_dst[CHANNELS..2 * CHANNELS];
147
148 ycgco_execute_limited::<V, PRECISION, CHANNELS, BIT_DEPTH>(
149 dst1.try_into().unwrap(),
150 y_value1,
151 cg_value,
152 co_value,
153 scale_coef,
154 );
155 }
156
157 // Process remainder if width is odd.
158 if image.width & 1 != 0 {
159 let rgb_chunks = rgb_chunks
160 .into_remainder()
161 .as_chunks_mut::<CHANNELS>()
162 .0
163 .iter_mut();
164 let u_iter = u_plane.iter().rev();
165 let v_iter = v_plane.iter().rev();
166
167 for (((y_src, u_src), v_src), rgb_dst) in
168 y_left.iter().zip(u_iter).zip(v_iter).zip(rgb_chunks)
169 {
170 let y_value = y_src.as_() - bias_y;
171 let cg_value = u_src.as_() - bias_uv;
172 let co_value = v_src.as_() - bias_uv;
173
174 ycgco_execute_limited::<V, PRECISION, CHANNELS, BIT_DEPTH>(
175 rgb_dst, y_value, cg_value, co_value, scale_coef,
176 );
177 }
178 }
179 }
180
181 /// Converts YCgCo 444 planar format to Rgba
182 ///
183 /// # Arguments
184 ///
185 /// * `image`: see [YuvPlanarImage]
186 /// * `rgba`: RGB image layout
187 /// * `range`: see [YuvIntensityRange]
188 ///
189 fn ycgco444_to_rgbx_impl<
190 V: Copy + AsPrimitive<i32> + 'static + Sized,
191 const CHANNELS: usize,
192 const BIT_DEPTH: usize,
193 >(
194 image: YuvPlanarImage<V>,
195 rgba: &mut [V],
196 yuv_range: YuvIntensityRange,
197 ) -> Result<(), ImageError>
198 where
199 i32: AsPrimitive<V>,
200 {
201 assert!(
202 CHANNELS == 3 || CHANNELS == 4,
203 "YUV 4:4:4 -> RGB is implemented only on 3 and 4 channels"
204 );
205 assert!(
206 (8..=16).contains(&BIT_DEPTH),
207 "Invalid bit depth is provided"
208 );
209 assert!(
210 if BIT_DEPTH > 8 {
211 size_of::<V>() == 2
212 } else {
213 size_of::<V>() == 1
214 },
215 "Unsupported bit depth and data type combination"
216 );
217
218 let y_plane = image.y_plane;
219 let u_plane = image.u_plane;
220 let v_plane = image.v_plane;
221 let y_stride = image.y_stride;
222 let u_stride = image.u_stride;
223 let v_stride = image.v_stride;
224 let height = image.height;
225 let width = image.width;
226
227 check_yuv_plane_preconditions(y_plane, PlaneDefinition::Y, y_stride, height)?;
228 check_yuv_plane_preconditions(u_plane, PlaneDefinition::U, u_stride, height)?;
229 check_yuv_plane_preconditions(v_plane, PlaneDefinition::V, v_stride, height)?;
230
231 check_rgb_preconditions(rgba, image.width * CHANNELS, height)?;
232
233 let range = yuv_range.get_yuv_range(BIT_DEPTH as u32);
234 const PRECISION: i32 = 13;
235
236 let bias_y = range.bias_y as i32;
237 let bias_uv = range.bias_uv as i32;
238
239 let rgb_stride = width * CHANNELS;
240
241 let y_iter = y_plane.chunks_exact(y_stride);
242 let rgb_iter = rgba.chunks_exact_mut(rgb_stride);
243 let u_iter = u_plane.chunks_exact(u_stride);
244 let v_iter = v_plane.chunks_exact(v_stride);
245
246 let max_value: i32 = (1 << BIT_DEPTH) - 1;
247
248 // All branches on generic const will be optimized out.
249 for (((y_src, u_src), v_src), rgb) in y_iter.zip(u_iter).zip(v_iter).zip(rgb_iter) {
250 let rgb_chunks = rgb.as_chunks_mut::<CHANNELS>().0.iter_mut();
251 match yuv_range {
252 YuvIntensityRange::Tv => {
253 let y_coef =
254 ((max_value as f32 / range.range_y as f32) * (1 << PRECISION) as f32) as i32;
255 for (((y_src, u_src), v_src), rgb_dst) in
256 y_src.iter().zip(u_src).zip(v_src).zip(rgb_chunks)
257 {
258 let y_value = y_src.as_() - bias_y;
259 let cg_value = u_src.as_() - bias_uv;
260 let co_value = v_src.as_() - bias_uv;
261
262 ycgco_execute_limited::<V, PRECISION, CHANNELS, BIT_DEPTH>(
263 rgb_dst, y_value, cg_value, co_value, y_coef,
264 );
265 }
266 }
267 YuvIntensityRange::Pc => {
268 for (((y_src, u_src), v_src), rgb_dst) in
269 y_src.iter().zip(u_src).zip(v_src).zip(rgb_chunks)
270 {
271 let y_value = y_src.as_() - bias_y;
272 let cg_value = u_src.as_() - bias_uv;
273 let co_value = v_src.as_() - bias_uv;
274
275 ycgco_execute_full::<V, PRECISION, CHANNELS, BIT_DEPTH>(
276 rgb_dst, y_value, cg_value, co_value,
277 );
278 }
279 }
280 }
281 }
282
283 Ok(())
284 }
285
286 macro_rules! define_ycgco_half_chroma {
287 ($name: ident, $invoker: ident, $storage: ident, $cn: expr, $bp: expr, $description: expr) => {
288 #[doc = concat!($description, "
289
290 # Arguments
291
292 * `image`: see [YuvPlanarImage]
293 * `rgb`: RGB image layout
294 * `range`: see [YuvIntensityRange]
295 * `matrix`: see [YuvStandardMatrix]")]
296 pub(crate) fn $name(
297 image: YuvPlanarImage<$storage>,
298 rgb: &mut [$storage],
299 range: YuvIntensityRange,
300 ) -> Result<(), ImageError> {
301 const P: i32 = 13;
302 $invoker::<$storage, HalvedRowHandler<$storage>, P, $cn, $bp>(
303 image,
304 rgb,
305 range,
306 YuvStandardMatrix::Bt709,
307 process_halved_chroma_row_cgco::<$storage, P, $cn, $bp>,
308 )
309 }
310 };
311 }
312
313 const RGBA_CN: usize = 4;
314
315 define_ycgco_half_chroma!(
316 ycgco420_to_rgba8,
317 yuv420_to_rgbx_invoker,
318 u8,
319 RGBA_CN,
320 8,
321 "Converts YCgCo 420 8-bit planar format to Rgba 8-bit"
322 );
323
324 define_ycgco_half_chroma!(
325 ycgco422_to_rgba8,
326 yuv422_to_rgbx_invoker,
327 u8,
328 RGBA_CN,
329 8,
330 "Converts YCgCo 420 8-bit planar format to Rgba 8-bit"
331 );
332
333 define_ycgco_half_chroma!(
334 ycgco420_to_rgba10,
335 yuv420_to_rgbx_invoker,
336 u16,
337 RGBA_CN,
338 10,
339 "Converts YCgCo 420 10-bit planar format to Rgba 10-bit"
340 );
341
342 define_ycgco_half_chroma!(
343 ycgco422_to_rgba10,
344 yuv422_to_rgbx_invoker,
345 u16,
346 RGBA_CN,
347 10,
348 "Converts YCgCo 422 10-bit planar format to Rgba 10-bit"
349 );
350
351 define_ycgco_half_chroma!(
352 ycgco420_to_rgba12,
353 yuv420_to_rgbx_invoker,
354 u16,
355 RGBA_CN,
356 12,
357 "Converts YCgCo 420 12-bit planar format to Rgba 12-bit"
358 );
359
360 define_ycgco_half_chroma!(
361 ycgco422_to_rgba12,
362 yuv422_to_rgbx_invoker,
363 u16,
364 RGBA_CN,
365 12,
366 "Converts YCgCo 422 12-bit planar format to Rgba 12-bit"
367 );
368
369 macro_rules! define_ycgcg_full_chroma {
370 ($name: ident, $storage: ident, $cn: expr, $bp: expr, $description: expr) => {
371 #[doc = concat!($description, "
372
373 # Arguments
374
375 * `image`: see [YuvPlanarImage]
376 * `rgba`: RGB image layout
377 * `range`: see [YuvIntensityRange]
378 * `matrix`: see [YuvStandardMatrix]
379 ")]
380 pub(crate) fn $name(
381 image: YuvPlanarImage<$storage>,
382 rgba: &mut [$storage],
383 range: YuvIntensityRange,
384 ) -> Result<(), ImageError> {
385 ycgco444_to_rgbx_impl::<$storage, $cn, $bp>(image, rgba, range)
386 }
387 };
388 }
389
390 define_ycgcg_full_chroma!(
391 ycgco444_to_rgba8,
392 u8,
393 RGBA_CN,
394 8,
395 "Converts YCgCo 444 planar format 8 bit-depth to Rgba 8 bit"
396 );
397 define_ycgcg_full_chroma!(
398 ycgco444_to_rgba10,
399 u16,
400 RGBA_CN,
401 10,
402 "Converts YCgCo 444 planar format 10 bit-depth to Rgba 10 bit"
403 );
404 define_ycgcg_full_chroma!(
405 ycgco444_to_rgba12,
406 u16,
407 RGBA_CN,
408 12,
409 "Converts YCgCo 444 planar format 12 bit-depth to Rgba 12 bit"
410 );