Debian Codegraph @

.cargo/vendor/image/src/codecs/avif/yuv.rs

raw
1 use crate::error::DecodingError;
2 use crate::{ImageError, ImageFormat};
3 use num_traits::AsPrimitive;
4 use std::fmt::{Display, Formatter};
5 use std::mem::size_of;
6
7 #[derive(Debug, Copy, Clone)]
8 /// Representation of inversion matrix
9 pub(crate) struct CbCrInverseTransform<T> {
10 y_coef: T,
11 cr_coef: T,
12 cb_coef: T,
13 g_coeff_1: T,
14 g_coeff_2: T,
15 }
16
17 impl CbCrInverseTransform<f32> {
18 fn to_integers(self, precision: u32) -> CbCrInverseTransform<i32> {
19 let precision_scale: i32 = 1i32 << (precision as i32);
20 let cr_coef = (self.cr_coef * precision_scale as f32) as i32;
21 let cb_coef = (self.cb_coef * precision_scale as f32) as i32;
22 let y_coef = (self.y_coef * precision_scale as f32) as i32;
23 let g_coef_1 = (self.g_coeff_1 * precision_scale as f32) as i32;
24 let g_coef_2 = (self.g_coeff_2 * precision_scale as f32) as i32;
25 CbCrInverseTransform::<i32> {
26 y_coef,
27 cr_coef,
28 cb_coef,
29 g_coeff_1: g_coef_1,
30 g_coeff_2: g_coef_2,
31 }
32 }
33 }
34
35 #[derive(Copy, Clone, Debug)]
36 struct ErrorSize {
37 expected: usize,
38 received: usize,
39 }
40
41 #[derive(Copy, Clone, Debug)]
42 pub(crate) enum PlaneDefinition {
43 Y,
44 U,
45 V,
46 }
47
48 impl Display for PlaneDefinition {
49 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
50 match self {
51 PlaneDefinition::Y => f.write_str("Luma"),
52 PlaneDefinition::U => f.write_str("U chroma"),
53 PlaneDefinition::V => f.write_str("V chroma"),
54 }
55 }
56 }
57
58 #[derive(Debug, Clone, Copy)]
59 enum YuvConversionError {
60 YuvPlaneSizeMismatch(PlaneDefinition, ErrorSize),
61 RgbDestinationSizeMismatch(ErrorSize),
62 }
63
64 impl Display for YuvConversionError {
65 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
66 match self {
67 YuvConversionError::YuvPlaneSizeMismatch(plane, error_size) => {
68 f.write_fmt(format_args!(
69 "For plane {} expected size is {} but was received {}",
70 plane, error_size.received, error_size.expected,
71 ))
72 }
73 YuvConversionError::RgbDestinationSizeMismatch(error_size) => {
74 f.write_fmt(format_args!(
75 "For RGB destination expected size is {} but was received {}",
76 error_size.received, error_size.expected,
77 ))
78 }
79 }
80 }
81 }
82
83 impl std::error::Error for YuvConversionError {}
84
85 #[inline]
86 pub(crate) fn check_yuv_plane_preconditions<V>(
87 plane: &[V],
88 plane_definition: PlaneDefinition,
89 stride: usize,
90 height: usize,
91 ) -> Result<(), ImageError> {
92 if plane.len() != stride * height {
93 return Err(ImageError::Decoding(DecodingError::new(
94 ImageFormat::Avif.into(),
95 YuvConversionError::YuvPlaneSizeMismatch(
96 plane_definition,
97 ErrorSize {
98 expected: stride * height,
99 received: plane.len(),
100 },
101 ),
102 )));
103 }
104 Ok(())
105 }
106
107 #[inline]
108 pub(crate) fn check_rgb_preconditions<V>(
109 rgb_data: &[V],
110 stride: usize,
111 height: usize,
112 ) -> Result<(), ImageError> {
113 if rgb_data.len() != stride * height {
114 return Err(ImageError::Decoding(DecodingError::new(
115 ImageFormat::Avif.into(),
116 YuvConversionError::RgbDestinationSizeMismatch(ErrorSize {
117 expected: stride * height,
118 received: rgb_data.len(),
119 }),
120 )));
121 }
122 Ok(())
123 }
124
125 /// Transformation YUV to RGB with coefficients as specified in [ITU-R](https://www.itu.int/rec/T-REC-H.273/en)
126 fn get_inverse_transform(
127 range_bgra: u32,
128 range_y: u32,
129 range_uv: u32,
130 kr: f32,
131 kb: f32,
132 precision: u32,
133 ) -> CbCrInverseTransform<i32> {
134 let range_uv = range_bgra as f32 / range_uv as f32;
135 let y_coef = range_bgra as f32 / range_y as f32;
136 let cr_coeff = (2f32 * (1f32 - kr)) * range_uv;
137 let cb_coeff = (2f32 * (1f32 - kb)) * range_uv;
138 let kg = 1.0f32 - kr - kb;
139 assert_ne!(kg, 0., "1.0f - kr - kg must not be 0");
140 let g_coeff_1 = (2f32 * ((1f32 - kr) * kr / kg)) * range_uv;
141 let g_coeff_2 = (2f32 * ((1f32 - kb) * kb / kg)) * range_uv;
142 let exact_transform = CbCrInverseTransform {
143 y_coef,
144 cr_coef: cr_coeff,
145 cb_coef: cb_coeff,
146 g_coeff_1,
147 g_coeff_2,
148 };
149 exact_transform.to_integers(precision)
150 }
151
152 #[derive(Debug, Copy, Clone, PartialOrd, PartialEq)]
153 /// Declares YUV range TV (limited) or PC (full),
154 /// more info [ITU-R](https://www.itu.int/rec/T-REC-H.273/en)
155 pub(crate) enum YuvIntensityRange {
156 /// Limited range Y ∈ [16 << (depth - 8), 16 << (depth - 8) + 224 << (depth - 8)],
157 /// UV ∈ [-1 << (depth - 1), -1 << (depth - 1) + 1 << (depth - 1)]
158 Tv,
159 /// Full range Y ∈ [0, 2^bit_depth - 1],
160 /// UV ∈ [-1 << (depth - 1), -1 << (depth - 1) + 2^bit_depth - 1]
161 Pc,
162 }
163
164 #[derive(Debug, Copy, Clone, PartialOrd, PartialEq)]
165 pub(crate) struct YuvChromaRange {
166 pub(crate) bias_y: u32,
167 pub(crate) bias_uv: u32,
168 pub(crate) range_y: u32,
169 pub(crate) range_uv: u32,
170 pub(crate) range: YuvIntensityRange,
171 }
172
173 impl YuvIntensityRange {
174 pub(crate) const fn get_yuv_range(self, depth: u32) -> YuvChromaRange {
175 match self {
176 YuvIntensityRange::Tv => YuvChromaRange {
177 bias_y: 16 << (depth - 8),
178 bias_uv: 1 << (depth - 1),
179 range_y: 219 << (depth - 8),
180 range_uv: 224 << (depth - 8),
181 range: self,
182 },
183 YuvIntensityRange::Pc => YuvChromaRange {
184 bias_y: 0,
185 bias_uv: 1 << (depth - 1),
186 range_uv: (1 << depth) - 1,
187 range_y: (1 << depth) - 1,
188 range: self,
189 },
190 }
191 }
192 }
193
194 #[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Eq)]
195 /// Declares standard prebuilt YUV conversion matrices,
196 /// check [ITU-R](https://www.itu.int/rec/T-REC-H.273/en) information for more info
197 pub(crate) enum YuvStandardMatrix {
198 Bt601,
199 Bt709,
200 Bt2020,
201 Smpte240,
202 Bt470_6,
203 }
204
205 #[derive(Debug, Copy, Clone, PartialOrd, PartialEq)]
206 struct YuvBias {
207 kr: f32,
208 kb: f32,
209 }
210
211 impl YuvStandardMatrix {
212 const fn get_kr_kb(self) -> YuvBias {
213 match self {
214 YuvStandardMatrix::Bt601 => YuvBias {
215 kr: 0.299f32,
216 kb: 0.114f32,
217 },
218 YuvStandardMatrix::Bt709 => YuvBias {
219 kr: 0.2126f32,
220 kb: 0.0722f32,
221 },
222 YuvStandardMatrix::Bt2020 => YuvBias {
223 kr: 0.2627f32,
224 kb: 0.0593f32,
225 },
226 YuvStandardMatrix::Smpte240 => YuvBias {
227 kr: 0.087f32,
228 kb: 0.212f32,
229 },
230 YuvStandardMatrix::Bt470_6 => YuvBias {
231 kr: 0.2220f32,
232 kb: 0.0713f32,
233 },
234 }
235 }
236 }
237
238 pub(crate) struct YuvPlanarImage<'a, T> {
239 pub(crate) y_plane: &'a [T],
240 pub(crate) y_stride: usize,
241 pub(crate) u_plane: &'a [T],
242 pub(crate) u_stride: usize,
243 pub(crate) v_plane: &'a [T],
244 pub(crate) v_stride: usize,
245 pub(crate) width: usize,
246 pub(crate) height: usize,
247 }
248
249 #[inline(always)]
250 /// Saturating rounding shift right against bit depth
251 pub(crate) fn qrshr<const PRECISION: i32, const BIT_DEPTH: usize>(val: i32) -> i32 {
252 let rounding: i32 = 1 << (PRECISION - 1);
253 let max_value: i32 = (1 << BIT_DEPTH) - 1;
254 ((val + rounding) >> PRECISION).clamp(0, max_value)
255 }
256
257 /// Converts Yuv 400 planar format 8 bit to Rgba 8 bit
258 ///
259 /// # Arguments
260 ///
261 /// * `image`: see [YuvGrayImage]
262 /// * `rgba`: RGBA image layout
263 /// * `range`: see [YuvIntensityRange]
264 /// * `matrix`: see [YuvStandardMatrix]
265 ///
266 pub(crate) fn yuv400_to_rgba8(
267 image: YuvPlanarImage<u8>,
268 rgba: &mut [u8],
269 range: YuvIntensityRange,
270 matrix: YuvStandardMatrix,
271 ) -> Result<(), ImageError> {
272 yuv400_to_rgbx_impl::<u8, 4, 8>(image, rgba, range, matrix)
273 }
274
275 /// Converts Yuv 400 planar format 10 bit to Rgba 10 bit
276 ///
277 /// # Arguments
278 ///
279 /// * `image`: see [YuvGrayImage]
280 /// * `rgba`: RGBA image layout
281 /// * `range`: see [YuvIntensityRange]
282 /// * `matrix`: see [YuvStandardMatrix]
283 ///
284 pub(crate) fn yuv400_to_rgba10(
285 image: YuvPlanarImage<u16>,
286 rgba: &mut [u16],
287 range: YuvIntensityRange,
288 matrix: YuvStandardMatrix,
289 ) -> Result<(), ImageError> {
290 yuv400_to_rgbx_impl::<u16, 4, 10>(image, rgba, range, matrix)
291 }
292
293 /// Converts Yuv 400 planar format 12 bit to Rgba 12 bit
294 ///
295 /// # Arguments
296 ///
297 /// * `image`: see [YuvGrayImage]
298 /// * `rgba`: RGBA image layout
299 /// * `range`: see [YuvIntensityRange]
300 /// * `matrix`: see [YuvStandardMatrix]
301 ///
302 pub(crate) fn yuv400_to_rgba12(
303 image: YuvPlanarImage<u16>,
304 rgba: &mut [u16],
305 range: YuvIntensityRange,
306 matrix: YuvStandardMatrix,
307 ) -> Result<(), ImageError> {
308 yuv400_to_rgbx_impl::<u16, 4, 12>(image, rgba, range, matrix)
309 }
310
311 /// Converts Yuv 400 planar format to Rgba
312 ///
313 /// # Arguments
314 ///
315 /// * `image`: see [YuvGrayImage]
316 /// * `rgba`: RGBA image layout
317 /// * `range`: see [YuvIntensityRange]
318 /// * `matrix`: see [YuvStandardMatrix]
319 ///
320 #[inline]
321 fn yuv400_to_rgbx_impl<
322 V: Copy + AsPrimitive<i32> + 'static + Sized,
323 const CHANNELS: usize,
324 const BIT_DEPTH: usize,
325 >(
326 image: YuvPlanarImage<V>,
327 rgba: &mut [V],
328 range: YuvIntensityRange,
329 matrix: YuvStandardMatrix,
330 ) -> Result<(), ImageError>
331 where
332 i32: AsPrimitive<V>,
333 {
334 assert!(
335 CHANNELS == 3 || CHANNELS == 4,
336 "YUV 4:0:0 -> RGB is implemented only on 3 and 4 channels"
337 );
338 assert!(
339 (8..=16).contains(&BIT_DEPTH),
340 "Invalid bit depth is provided"
341 );
342 assert!(
343 if BIT_DEPTH > 8 {
344 size_of::<V>() == 2
345 } else {
346 size_of::<V>() == 1
347 },
348 "Unsupported bit depth and data type combination"
349 );
350
351 let y_plane = image.y_plane;
352 let y_stride = image.y_stride;
353 let height = image.height;
354 let width = image.width;
355
356 check_yuv_plane_preconditions(y_plane, PlaneDefinition::Y, y_stride, height)?;
357 check_rgb_preconditions(rgba, width * CHANNELS, height)?;
358
359 let rgba_stride = width * CHANNELS;
360
361 let max_value = (1 << BIT_DEPTH) - 1;
362
363 // If luma plane is in full range it can be just redistributed across the image
364 if range == YuvIntensityRange::Pc {
365 let y_iter = y_plane.chunks_exact(y_stride);
366 let rgb_iter = rgba.chunks_exact_mut(rgba_stride);
367
368 // All branches on generic const will be optimized out.
369 for (y_src, rgb) in y_iter.zip(rgb_iter) {
370 let rgb_chunks = rgb.chunks_exact_mut(CHANNELS);
371
372 for (y_src, rgb_dst) in y_src.iter().zip(rgb_chunks) {
373 let r = *y_src;
374 rgb_dst[0] = r;
375 rgb_dst[1] = r;
376 rgb_dst[2] = r;
377 if CHANNELS == 4 {
378 rgb_dst[3] = max_value.as_();
379 }
380 }
381 }
382 return Ok(());
383 }
384
385 let range = range.get_yuv_range(BIT_DEPTH as u32);
386 let kr_kb = matrix.get_kr_kb();
387 const PRECISION: i32 = 11;
388
389 let inverse_transform = get_inverse_transform(
390 (1 << BIT_DEPTH) - 1,
391 range.range_y,
392 range.range_uv,
393 kr_kb.kr,
394 kr_kb.kb,
395 PRECISION as u32,
396 );
397 let y_coef = inverse_transform.y_coef;
398
399 let bias_y = range.bias_y as i32;
400
401 let y_iter = y_plane.chunks_exact(y_stride);
402 let rgb_iter = rgba.chunks_exact_mut(rgba_stride);
403
404 // All branches on generic const will be optimized out.
405 for (y_src, rgb) in y_iter.zip(rgb_iter) {
406 let rgb_chunks = rgb.chunks_exact_mut(CHANNELS);
407
408 for (y_src, rgb_dst) in y_src.iter().zip(rgb_chunks) {
409 let y_value = (y_src.as_() - bias_y) * y_coef;
410
411 let r = qrshr::<PRECISION, BIT_DEPTH>(y_value);
412 rgb_dst[0] = r.as_();
413 rgb_dst[1] = r.as_();
414 rgb_dst[2] = r.as_();
415 if CHANNELS == 4 {
416 rgb_dst[3] = max_value.as_();
417 }
418 }
419 }
420
421 Ok(())
422 }
423
424 /// Converts YUV420 8 bit-depth to Rgba 8 bit
425 ///
426 /// # Arguments
427 ///
428 /// * `image`: see [YuvPlanarImage]
429 /// * `rgb`: RGB image layout
430 /// * `range`: see [YuvIntensityRange]
431 /// * `matrix`: see [YuvStandardMatrix]
432 ///
433 pub(crate) fn yuv420_to_rgba8(
434 image: YuvPlanarImage<u8>,
435 rgb: &mut [u8],
436 range: YuvIntensityRange,
437 matrix: YuvStandardMatrix,
438 ) -> Result<(), ImageError> {
439 const P: i32 = 13;
440 yuv420_to_rgbx_invoker::<u8, HalvedRowHandler<u8>, P, 4, 8>(
441 image,
442 rgb,
443 range,
444 matrix,
445 process_halved_chroma_row_cbcr::<u8, P, 4, 8>,
446 )
447 }
448
449 /// Converts YUV420 10 bit-depth to Rgba 10 bit-depth
450 ///
451 /// # Arguments
452 ///
453 /// * `image`: see [YuvPlanarImage]
454 /// * `rgb`: RGB image layout
455 /// * `range`: see [YuvIntensityRange]
456 /// * `matrix`: see [YuvStandardMatrix]
457 ///
458 pub(crate) fn yuv420_to_rgba10(
459 image: YuvPlanarImage<u16>,
460 rgb: &mut [u16],
461 range: YuvIntensityRange,
462 matrix: YuvStandardMatrix,
463 ) -> Result<(), ImageError> {
464 const P: i32 = 13;
465 yuv420_to_rgbx_invoker::<u16, HalvedRowHandler<u16>, P, 4, 10>(
466 image,
467 rgb,
468 range,
469 matrix,
470 process_halved_chroma_row_cbcr::<u16, P, 4, 10>,
471 )
472 }
473
474 pub(crate) type HalvedRowHandler<V> =
475 fn(YuvPlanarImage<V>, &mut [V], &CbCrInverseTransform<i32>, &YuvChromaRange);
476
477 /// Converts YUV420 12 bit-depth to Rgba 12 bit-depth
478 ///
479 /// # Arguments
480 ///
481 /// * `image`: see [YuvPlanarImage]
482 /// * `rgb`: RGB image layout
483 /// * `range`: see [YuvIntensityRange]
484 /// * `matrix`: see [YuvStandardMatrix]
485 ///
486 pub(crate) fn yuv420_to_rgba12(
487 image: YuvPlanarImage<u16>,
488 rgb: &mut [u16],
489 range: YuvIntensityRange,
490 matrix: YuvStandardMatrix,
491 ) -> Result<(), ImageError> {
492 const P: i32 = 13;
493 yuv420_to_rgbx_invoker::<u16, HalvedRowHandler<u16>, P, 4, 12>(
494 image,
495 rgb,
496 range,
497 matrix,
498 process_halved_chroma_row_cbcr::<u16, P, 4, 12>,
499 )
500 }
501
502 /// Computes YCbCr inverse
503 /// # Arguments
504 /// - `dst` - dest buffer
505 /// - `y_value` - Y value with subtracted bias
506 /// - `cb` - Cb value with subtracted bias
507 /// - `cr` - Cr value with subtracted bias
508 #[inline(always)]
509 fn ycbcr_execute<
510 V: Copy + AsPrimitive<i32> + 'static + Sized,
511 const PRECISION: i32,
512 const CHANNELS: usize,
513 const BIT_DEPTH: usize,
514 >(
515 dst: &mut [V; CHANNELS],
516 y_value: i32,
517 cb: i32,
518 cr: i32,
519 t: &CbCrInverseTransform<i32>,
520 ) where
521 i32: AsPrimitive<V>,
522 {
523 let y_scaled = y_value * t.y_coef;
524 let r = qrshr::<PRECISION, BIT_DEPTH>(y_scaled + t.cr_coef * cr);
525 let b = qrshr::<PRECISION, BIT_DEPTH>(y_scaled + t.cb_coef * cb);
526 let g = qrshr::<PRECISION, BIT_DEPTH>(y_scaled - t.g_coeff_1 * cr - t.g_coeff_2 * cb);
527
528 if CHANNELS == 4 {
529 dst[0] = r.as_();
530 dst[1] = g.as_();
531 dst[2] = b.as_();
532 dst[3] = ((1i32 << BIT_DEPTH) - 1).as_();
533 } else if CHANNELS == 3 {
534 dst[0] = r.as_();
535 dst[1] = g.as_();
536 dst[2] = b.as_();
537 } else {
538 unreachable!();
539 }
540 }
541
542 #[inline]
543 fn process_halved_chroma_row_cbcr<
544 V: Copy + AsPrimitive<i32> + 'static + Sized,
545 const PRECISION: i32,
546 const CHANNELS: usize,
547 const BIT_DEPTH: usize,
548 >(
549 image: YuvPlanarImage<V>,
550 rgba: &mut [V],
551 transform: &CbCrInverseTransform<i32>,
552 range: &YuvChromaRange,
553 ) where
554 i32: AsPrimitive<V>,
555 {
556 // If the stride is larger than the plane size,
557 // it might contain junk data beyond the actual valid region.
558 // To avoid processing artifacts when working with odd-sized images,
559 // the buffer is reshaped to its actual size,
560 // preventing accidental use of invalid values from the trailing region.
561
562 let y_plane = &image.y_plane[0..image.width];
563 let chroma_size = image.width.div_ceil(2);
564 let u_plane = &image.u_plane[0..chroma_size];
565 let v_plane = &image.v_plane[0..chroma_size];
566 let rgba = &mut rgba[0..image.width * CHANNELS];
567
568 let bias_y = range.bias_y as i32;
569 let bias_uv = range.bias_uv as i32;
570 let y_iter = y_plane.chunks_exact(2);
571 let rgb_chunks = rgba.chunks_exact_mut(CHANNELS * 2);
572 for (((y_src, &u_src), &v_src), rgb_dst) in y_iter.zip(u_plane).zip(v_plane).zip(rgb_chunks) {
573 let y_value0: i32 = y_src[0].as_() - bias_y;
574 let cb_value: i32 = u_src.as_() - bias_uv;
575 let cr_value: i32 = v_src.as_() - bias_uv;
576
577 let dst0 = &mut rgb_dst[..CHANNELS];
578
579 ycbcr_execute::<V, PRECISION, CHANNELS, BIT_DEPTH>(
580 dst0.try_into().unwrap(),
581 y_value0,
582 cb_value,
583 cr_value,
584 transform,
585 );
586
587 let y_value1 = y_src[1].as_() - bias_y;
588
589 let dst1 = &mut rgb_dst[CHANNELS..2 * CHANNELS];
590
591 ycbcr_execute::<V, PRECISION, CHANNELS, BIT_DEPTH>(
592 dst1.try_into().unwrap(),
593 y_value1,
594 cb_value,
595 cr_value,
596 transform,
597 );
598 }
599
600 // Process remainder if width is odd.
601 if image.width & 1 != 0 {
602 let y_left = y_plane.chunks_exact(2).remainder();
603 let rgb_chunks = rgba
604 .chunks_exact_mut(CHANNELS * 2)
605 .into_remainder()
606 .chunks_exact_mut(CHANNELS);
607 let u_iter = u_plane.iter().rev();
608 let v_iter = v_plane.iter().rev();
609
610 for (((y_src, u_src), v_src), rgb_dst) in
611 y_left.iter().zip(u_iter).zip(v_iter).zip(rgb_chunks)
612 {
613 let y_value = y_src.as_() - bias_y;
614 let cb_value = u_src.as_() - bias_uv;
615 let cr_value = v_src.as_() - bias_uv;
616
617 ycbcr_execute::<V, PRECISION, CHANNELS, BIT_DEPTH>(
618 rgb_dst.try_into().unwrap(),
619 y_value,
620 cb_value,
621 cr_value,
622 transform,
623 );
624 }
625 }
626 }
627
628 /// Converts YUV420 to Rgba
629 ///
630 /// # Arguments
631 ///
632 /// * `image`: see [YuvPlanarImage]
633 /// * `rgb`: RGB image layout
634 /// * `range`: see [YuvIntensityRange]
635 /// * `matrix`: see [YuvStandardMatrix]
636 ///
637 pub(crate) fn yuv420_to_rgbx_invoker<
638 V: Copy + AsPrimitive<i32> + 'static + Sized,
639 W: Fn(YuvPlanarImage<V>, &mut [V], &CbCrInverseTransform<i32>, &YuvChromaRange),
640 const PRECISION: i32,
641 const CHANNELS: usize,
642 const BIT_DEPTH: usize,
643 >(
644 image: YuvPlanarImage<V>,
645 rgb: &mut [V],
646 range: YuvIntensityRange,
647 matrix: YuvStandardMatrix,
648 worker: W,
649 ) -> Result<(), ImageError>
650 where
651 i32: AsPrimitive<V>,
652 {
653 assert!(
654 CHANNELS == 3 || CHANNELS == 4,
655 "YUV 4:2:0 -> RGB is implemented only on 3 and 4 channels"
656 );
657 assert!(
658 (8..=16).contains(&BIT_DEPTH),
659 "Invalid bit depth is provided"
660 );
661 assert!(
662 if BIT_DEPTH > 8 {
663 size_of::<V>() == 2
664 } else {
665 size_of::<V>() == 1
666 },
667 "Unsupported bit depth and data type combination"
668 );
669 let y_plane = image.y_plane;
670 let u_plane = image.u_plane;
671 let v_plane = image.v_plane;
672 let y_stride = image.y_stride;
673 let u_stride = image.u_stride;
674 let v_stride = image.v_stride;
675 let chroma_height = image.height.div_ceil(2);
676
677 check_yuv_plane_preconditions(y_plane, PlaneDefinition::Y, y_stride, image.height)?;
678 check_yuv_plane_preconditions(u_plane, PlaneDefinition::U, u_stride, chroma_height)?;
679 check_yuv_plane_preconditions(v_plane, PlaneDefinition::V, v_stride, chroma_height)?;
680
681 check_rgb_preconditions(rgb, image.width * CHANNELS, image.height)?;
682
683 let range = range.get_yuv_range(BIT_DEPTH as u32);
684 let kr_kb = matrix.get_kr_kb();
685 let inverse_transform = get_inverse_transform(
686 (1 << BIT_DEPTH) - 1,
687 range.range_y,
688 range.range_uv,
689 kr_kb.kr,
690 kr_kb.kb,
691 PRECISION as u32,
692 );
693
694 let rgb_stride = image.width * CHANNELS;
695
696 let y_iter = y_plane.chunks_exact(y_stride * 2);
697 let rgb_iter = rgb.chunks_exact_mut(rgb_stride * 2);
698 let u_iter = u_plane.chunks_exact(u_stride);
699 let v_iter = v_plane.chunks_exact(v_stride);
700
701 /*
702 Sample 4x4 YUV420 planar image
703 start_y + 0: Y00 Y01 Y02 Y03
704 start_y + 4: Y04 Y05 Y06 Y07
705 start_y + 8: Y08 Y09 Y10 Y11
706 start_y + 12: Y12 Y13 Y14 Y15
707 start_cb + 0: Cb00 Cb01
708 start_cb + 2: Cb02 Cb03
709 start_cr + 0: Cr00 Cr01
710 start_cr + 2: Cr02 Cr03
711
712 For 4 luma components (2x2 on rows and cols) there are 1 chroma Cb/Cr components.
713 Luma channel must have always exact size as RGB target layout, but chroma is not.
714
715 We're sectioning an image by pair of rows, then for each pair of luma and RGB row,
716 there is one chroma row.
717
718 As chroma is shrunk by factor of 2 then we're processing by pairs of RGB and luma,
719 for each RGB and luma pair there is one chroma component.
720
721 If image have odd width then luma channel must be exact, and we're replicating last
722 chroma component.
723
724 If image have odd height then luma channel is exact, and we're replicating last chroma rows.
725 */
726
727 // All branches on generic const will be optimized out.
728 for (((y_src, u_src), v_src), rgb) in y_iter.zip(u_iter).zip(v_iter).zip(rgb_iter) {
729 // Since we're processing two rows in one loop we need to re-slice once more
730 let y_iter = y_src.chunks_exact(y_stride);
731 let rgb_iter = rgb.chunks_exact_mut(rgb_stride);
732 for (y_src, rgba) in y_iter.zip(rgb_iter) {
733 let image = YuvPlanarImage {
734 y_plane: y_src,
735 y_stride: 0,
736 u_plane: u_src,
737 u_stride: 0,
738 v_plane: v_src,
739 v_stride: 0,
740 width: image.width,
741 height: image.height,
742 };
743 worker(image, rgba, &inverse_transform, &range);
744 }
745 }
746
747 // Process remainder if height is odd
748
749 let y_iter = y_plane
750 .chunks_exact(y_stride * 2)
751 .remainder()
752 .chunks_exact(y_stride);
753 let rgb_iter = rgb.chunks_exact_mut(rgb_stride).rev();
754 let u_iter = u_plane.chunks_exact(u_stride).rev();
755 let v_iter = v_plane.chunks_exact(v_stride).rev();
756
757 for (((y_src, u_src), v_src), rgba) in y_iter.zip(u_iter).zip(v_iter).zip(rgb_iter) {
758 let image = YuvPlanarImage {
759 y_plane: y_src,
760 y_stride: 0,
761 u_plane: u_src,
762 u_stride: 0,
763 v_plane: v_src,
764 v_stride: 0,
765 width: image.width,
766 height: image.height,
767 };
768 worker(image, rgba, &inverse_transform, &range);
769 }
770
771 Ok(())
772 }
773
774 /// Converts Yuv 422 8-bit planar format to Rgba 8-bit
775 ///
776 /// # Arguments
777 ///
778 /// * `image`: see [YuvPlanarImage]
779 /// * `rgb`: RGB image layout
780 /// * `range`: see [YuvIntensityRange]
781 /// * `matrix`: see [YuvStandardMatrix]
782 ///
783 pub(crate) fn yuv422_to_rgba8(
784 image: YuvPlanarImage<u8>,
785 rgb: &mut [u8],
786 range: YuvIntensityRange,
787 matrix: YuvStandardMatrix,
788 ) -> Result<(), ImageError> {
789 const P: i32 = 13;
790 yuv422_to_rgbx_invoker::<u8, HalvedRowHandler<u8>, P, 4, 8>(
791 image,
792 rgb,
793 range,
794 matrix,
795 process_halved_chroma_row_cbcr::<u8, P, 4, 8>,
796 )
797 }
798
799 /// Converts Yuv 422 10-bit planar format to Rgba 10-bit
800 ///
801 /// # Arguments
802 ///
803 /// * `image`: see [YuvPlanarImage]
804 /// * `rgb`: RGB image layout
805 /// * `range`: see [YuvIntensityRange]
806 /// * `matrix`: see [YuvStandardMatrix]
807 ///
808 pub(crate) fn yuv422_to_rgba10(
809 image: YuvPlanarImage<u16>,
810 rgb: &mut [u16],
811 range: YuvIntensityRange,
812 matrix: YuvStandardMatrix,
813 ) -> Result<(), ImageError> {
814 const P: i32 = 13;
815 yuv422_to_rgbx_invoker::<u16, HalvedRowHandler<u16>, P, 4, 10>(
816 image,
817 rgb,
818 range,
819 matrix,
820 process_halved_chroma_row_cbcr::<u16, P, 4, 10>,
821 )
822 }
823
824 /// Converts Yuv 422 12-bit planar format to Rgba 12-bit
825 ///
826 /// # Arguments
827 ///
828 /// * `image`: see [YuvPlanarImage]
829 /// * `rgb`: RGB image layout
830 /// * `range`: see [YuvIntensityRange]
831 /// * `matrix`: see [YuvStandardMatrix]
832 ///
833 pub(crate) fn yuv422_to_rgba12(
834 image: YuvPlanarImage<u16>,
835 rgb: &mut [u16],
836 range: YuvIntensityRange,
837 matrix: YuvStandardMatrix,
838 ) -> Result<(), ImageError> {
839 const P: i32 = 13;
840 yuv422_to_rgbx_invoker::<u16, HalvedRowHandler<u16>, P, 4, 12>(
841 image,
842 rgb,
843 range,
844 matrix,
845 process_halved_chroma_row_cbcr::<u16, P, 4, 12>,
846 )
847 }
848
849 /// Converts Yuv 422 planar format to Rgba
850 ///
851 /// # Arguments
852 ///
853 /// * `image`: see [YuvPlanarImage]
854 /// * `rgb`: RGB image layout
855 /// * `range`: see [YuvIntensityRange]
856 /// * `matrix`: see [YuvStandardMatrix]
857 ///
858 pub(crate) fn yuv422_to_rgbx_invoker<
859 V: Copy + AsPrimitive<i32> + 'static + Sized,
860 W: Fn(YuvPlanarImage<V>, &mut [V], &CbCrInverseTransform<i32>, &YuvChromaRange),
861 const PRECISION: i32,
862 const CHANNELS: usize,
863 const BIT_DEPTH: usize,
864 >(
865 image: YuvPlanarImage<V>,
866 rgb: &mut [V],
867 range: YuvIntensityRange,
868 matrix: YuvStandardMatrix,
869 worker: W,
870 ) -> Result<(), ImageError>
871 where
872 i32: AsPrimitive<V>,
873 {
874 assert!(
875 CHANNELS == 3 || CHANNELS == 4,
876 "YUV 4:2:2 -> RGB is implemented only on 3 and 4 channels"
877 );
878 assert!(
879 (8..=16).contains(&BIT_DEPTH),
880 "Invalid bit depth is provided"
881 );
882 assert!(PRECISION < 16);
883 assert!(
884 if BIT_DEPTH > 8 {
885 size_of::<V>() == 2
886 } else {
887 size_of::<V>() == 1
888 },
889 "Unsupported bit depth and data type combination"
890 );
891 let y_plane = image.y_plane;
892 let u_plane = image.u_plane;
893 let v_plane = image.v_plane;
894 let y_stride = image.y_stride;
895 let u_stride = image.u_stride;
896 let v_stride = image.v_stride;
897 let width = image.width;
898
899 check_yuv_plane_preconditions(y_plane, PlaneDefinition::Y, y_stride, image.height)?;
900 check_yuv_plane_preconditions(u_plane, PlaneDefinition::U, u_stride, image.height)?;
901 check_yuv_plane_preconditions(v_plane, PlaneDefinition::V, v_stride, image.height)?;
902
903 check_rgb_preconditions(rgb, image.width * CHANNELS, image.height)?;
904
905 let range = range.get_yuv_range(BIT_DEPTH as u32);
906 let kr_kb = matrix.get_kr_kb();
907
908 let inverse_transform = get_inverse_transform(
909 (1 << BIT_DEPTH) - 1,
910 range.range_y,
911 range.range_uv,
912 kr_kb.kr,
913 kr_kb.kb,
914 PRECISION as u32,
915 );
916
917 /*
918 Sample 4x4 YUV422 planar image
919 start_y + 0: Y00 Y01 Y02 Y03
920 start_y + 4: Y04 Y05 Y06 Y07
921 start_y + 8: Y08 Y09 Y10 Y11
922 start_y + 12: Y12 Y13 Y14 Y15
923 start_cb + 0: Cb00 Cb01
924 start_cb + 2: Cb02 Cb03
925 start_cb + 4: Cb04 Cb05
926 start_cb + 6: Cb06 Cb07
927 start_cr + 0: Cr00 Cr01
928 start_cr + 2: Cr02 Cr03
929 start_cr + 4: Cr04 Cr05
930 start_cr + 6: Cr06 Cr07
931
932 For 2 luma components there are 1 chroma Cb/Cr components.
933 Luma channel must have always exact size as RGB target layout, but chroma is not.
934
935 As chroma is shrunk by factor of 2 then we're processing by pairs of RGB and luma,
936 for each RGB and luma pair there is one chroma component.
937
938 If image have odd width then luma channel must be exact, and we're replicating last
939 chroma component.
940 */
941
942 let rgb_stride = width * CHANNELS;
943
944 let y_iter = y_plane.chunks_exact(y_stride);
945 let rgb_iter = rgb.chunks_exact_mut(rgb_stride);
946 let u_iter = u_plane.chunks_exact(u_stride);
947 let v_iter = v_plane.chunks_exact(v_stride);
948
949 // All branches on generic const will be optimized out.
950 for (((y_src, u_src), v_src), rgba) in y_iter.zip(u_iter).zip(v_iter).zip(rgb_iter) {
951 let image = YuvPlanarImage {
952 y_plane: y_src,
953 y_stride: 0,
954 u_plane: u_src,
955 u_stride: 0,
956 v_plane: v_src,
957 v_stride: 0,
958 width: image.width,
959 height: image.height,
960 };
961 worker(image, rgba, &inverse_transform, &range);
962 }
963
964 Ok(())
965 }
966
967 /// Converts Yuv 444 planar format 8 bit-depth to Rgba 8 bit
968 ///
969 /// # Arguments
970 ///
971 /// * `image`: see [YuvPlanarImage]
972 /// * `rgba`: RGB image layout
973 /// * `range`: see [YuvIntensityRange]
974 /// * `matrix`: see [YuvStandardMatrix]
975 ///
976 pub(crate) fn yuv444_to_rgba8(
977 image: YuvPlanarImage<u8>,
978 rgba: &mut [u8],
979 range: YuvIntensityRange,
980 matrix: YuvStandardMatrix,
981 ) -> Result<(), ImageError> {
982 yuv444_to_rgbx_impl::<u8, 4, 8>(image, rgba, range, matrix)
983 }
984
985 /// Converts Yuv 444 planar format 10 bit-depth to Rgba 10 bit
986 ///
987 /// # Arguments
988 ///
989 /// * `image`: see [YuvPlanarImage]
990 /// * `rgba`: RGB image layout
991 /// * `range`: see [YuvIntensityRange]
992 /// * `matrix`: see [YuvStandardMatrix]
993 ///
994 pub(super) fn yuv444_to_rgba10(
995 image: YuvPlanarImage<u16>,
996 rgba: &mut [u16],
997 range: YuvIntensityRange,
998 matrix: YuvStandardMatrix,
999 ) -> Result<(), ImageError> {
1000 yuv444_to_rgbx_impl::<u16, 4, 10>(image, rgba, range, matrix)
1001 }
1002
1003 /// Converts Yuv 444 planar format 12 bit-depth to Rgba 12 bit
1004 ///
1005 /// # Arguments
1006 ///
1007 /// * `image`: see [YuvPlanarImage]
1008 /// * `rgba`: RGB image layout
1009 /// * `range`: see [YuvIntensityRange]
1010 /// * `matrix`: see [YuvStandardMatrix]
1011 ///
1012 pub(super) fn yuv444_to_rgba12(
1013 image: YuvPlanarImage<u16>,
1014 rgba: &mut [u16],
1015 range: YuvIntensityRange,
1016 matrix: YuvStandardMatrix,
1017 ) -> Result<(), ImageError> {
1018 yuv444_to_rgbx_impl::<u16, 4, 12>(image, rgba, range, matrix)
1019 }
1020
1021 /// Converts Yuv 444 planar format to Rgba
1022 ///
1023 /// # Arguments
1024 ///
1025 /// * `image`: see [YuvPlanarImage]
1026 /// * `rgba`: RGB image layout
1027 /// * `range`: see [YuvIntensityRange]
1028 /// * `matrix`: see [YuvStandardMatrix]
1029 ///
1030 #[inline]
1031 fn yuv444_to_rgbx_impl<
1032 V: Copy + AsPrimitive<i32> + 'static + Sized,
1033 const CHANNELS: usize,
1034 const BIT_DEPTH: usize,
1035 >(
1036 image: YuvPlanarImage<V>,
1037 rgba: &mut [V],
1038 range: YuvIntensityRange,
1039 matrix: YuvStandardMatrix,
1040 ) -> Result<(), ImageError>
1041 where
1042 i32: AsPrimitive<V>,
1043 {
1044 assert!(
1045 CHANNELS == 3 || CHANNELS == 4,
1046 "YUV 4:4:4 -> RGB is implemented only on 3 and 4 channels"
1047 );
1048 assert!(
1049 (8..=16).contains(&BIT_DEPTH),
1050 "Invalid bit depth is provided"
1051 );
1052 assert!(
1053 if BIT_DEPTH > 8 {
1054 size_of::<V>() == 2
1055 } else {
1056 size_of::<V>() == 1
1057 },
1058 "Unsupported bit depth and data type combination"
1059 );
1060
1061 let y_plane = image.y_plane;
1062 let u_plane = image.u_plane;
1063 let v_plane = image.v_plane;
1064 let y_stride = image.y_stride;
1065 let u_stride = image.u_stride;
1066 let v_stride = image.v_stride;
1067 let height = image.height;
1068 let width = image.width;
1069
1070 check_yuv_plane_preconditions(y_plane, PlaneDefinition::Y, y_stride, height)?;
1071 check_yuv_plane_preconditions(u_plane, PlaneDefinition::U, u_stride, height)?;
1072 check_yuv_plane_preconditions(v_plane, PlaneDefinition::V, v_stride, height)?;
1073
1074 check_rgb_preconditions(rgba, image.width * CHANNELS, height)?;
1075
1076 let range = range.get_yuv_range(BIT_DEPTH as u32);
1077 let kr_kb = matrix.get_kr_kb();
1078 const PRECISION: i32 = 13;
1079
1080 let inverse_transform = get_inverse_transform(
1081 (1 << BIT_DEPTH) - 1,
1082 range.range_y,
1083 range.range_uv,
1084 kr_kb.kr,
1085 kr_kb.kb,
1086 PRECISION as u32,
1087 );
1088
1089 let bias_y = range.bias_y as i32;
1090 let bias_uv = range.bias_uv as i32;
1091
1092 let rgb_stride = width * CHANNELS;
1093
1094 let y_iter = y_plane.chunks_exact(y_stride);
1095 let rgb_iter = rgba.chunks_exact_mut(rgb_stride);
1096 let u_iter = u_plane.chunks_exact(u_stride);
1097 let v_iter = v_plane.chunks_exact(v_stride);
1098
1099 // All branches on generic const will be optimized out.
1100 for (((y_src, u_src), v_src), rgb) in y_iter.zip(u_iter).zip(v_iter).zip(rgb_iter) {
1101 let rgb_chunks = rgb.chunks_exact_mut(CHANNELS);
1102
1103 for (((y_src, u_src), v_src), rgb_dst) in y_src.iter().zip(u_src).zip(v_src).zip(rgb_chunks)
1104 {
1105 let y_value = y_src.as_() - bias_y;
1106 let cb_value = u_src.as_() - bias_uv;
1107 let cr_value = v_src.as_() - bias_uv;
1108
1109 ycbcr_execute::<V, PRECISION, CHANNELS, BIT_DEPTH>(
1110 rgb_dst.try_into().unwrap(),
1111 y_value,
1112 cb_value,
1113 cr_value,
1114 &inverse_transform,
1115 );
1116 }
1117 }
1118
1119 Ok(())
1120 }
1121
1122 /// Converts Gbr 8 bit planar format to Rgba 8 bit-depth
1123 ///
1124 /// # Arguments
1125 ///
1126 /// * `image`: see [YuvPlanarImage]
1127 /// * `rgb`: RGB image layout
1128 /// * `range`: see [YuvIntensityRange]
1129 ///
1130 pub(crate) fn gbr_to_rgba8(
1131 image: YuvPlanarImage<u8>,
1132 rgb: &mut [u8],
1133 range: YuvIntensityRange,
1134 ) -> Result<(), ImageError> {
1135 gbr_to_rgbx_impl::<u8, 4, 8>(image, rgb, range)
1136 }
1137
1138 /// Converts Gbr 10 bit planar format to Rgba 10 bit-depth
1139 ///
1140 ///
1141 /// # Arguments
1142 ///
1143 /// * `image`: see [YuvPlanarImage]
1144 /// * `rgba`: RGBx image layout
1145 /// * `range`: see [YuvIntensityRange]
1146 ///
1147 pub(crate) fn gbr_to_rgba10(
1148 image: YuvPlanarImage<u16>,
1149 rgba: &mut [u16],
1150 range: YuvIntensityRange,
1151 ) -> Result<(), ImageError> {
1152 gbr_to_rgbx_impl::<u16, 4, 10>(image, rgba, range)
1153 }
1154
1155 /// Converts Gbr 12 bit planar format to Rgba 12 bit-depth
1156 ///
1157 /// # Arguments
1158 ///
1159 /// * `image`: see [YuvPlanarImage]
1160 /// * `rgba`: RGBx image layout
1161 /// * `range`: see [YuvIntensityRange]
1162 ///
1163 pub(crate) fn gbr_to_rgba12(
1164 image: YuvPlanarImage<u16>,
1165 rgba: &mut [u16],
1166 range: YuvIntensityRange,
1167 ) -> Result<(), ImageError> {
1168 gbr_to_rgbx_impl::<u16, 4, 12>(image, rgba, range)
1169 }
1170
1171 /// Converts Gbr planar format to Rgba
1172 ///
1173 /// # Arguments
1174 ///
1175 /// * `image`: see [YuvPlanarImage]
1176 /// * `rgb`: RGB image layout
1177 /// * `range`: see [YuvIntensityRange]
1178 ///
1179 #[inline]
1180 fn gbr_to_rgbx_impl<
1181 V: Copy + AsPrimitive<i32> + 'static + Sized,
1182 const CHANNELS: usize,
1183 const BIT_DEPTH: usize,
1184 >(
1185 image: YuvPlanarImage<V>,
1186 rgba: &mut [V],
1187 yuv_range: YuvIntensityRange,
1188 ) -> Result<(), ImageError>
1189 where
1190 i32: AsPrimitive<V>,
1191 {
1192 assert!(
1193 CHANNELS == 3 || CHANNELS == 4,
1194 "GBR -> RGB is implemented only on 3 and 4 channels"
1195 );
1196 assert!(
1197 (8..=16).contains(&BIT_DEPTH),
1198 "Invalid bit depth is provided"
1199 );
1200 assert!(
1201 if BIT_DEPTH > 8 {
1202 size_of::<V>() == 2
1203 } else {
1204 size_of::<V>() == 1
1205 },
1206 "Unsupported bit depth and data type combination"
1207 );
1208 let y_plane = image.y_plane;
1209 let u_plane = image.u_plane;
1210 let v_plane = image.v_plane;
1211 let y_stride = image.y_stride;
1212 let u_stride = image.u_stride;
1213 let v_stride = image.v_stride;
1214 let height = image.height;
1215 let width = image.width;
1216
1217 check_yuv_plane_preconditions(y_plane, PlaneDefinition::Y, y_stride, height)?;
1218 check_yuv_plane_preconditions(u_plane, PlaneDefinition::U, u_stride, height)?;
1219 check_yuv_plane_preconditions(v_plane, PlaneDefinition::V, v_stride, height)?;
1220
1221 check_rgb_preconditions(rgba, width * CHANNELS, height)?;
1222
1223 let max_value = (1 << BIT_DEPTH) - 1;
1224
1225 let rgb_stride = width * CHANNELS;
1226
1227 let y_iter = y_plane.chunks_exact(y_stride);
1228 let rgb_iter = rgba.chunks_exact_mut(rgb_stride);
1229 let u_iter = u_plane.chunks_exact(u_stride);
1230 let v_iter = v_plane.chunks_exact(v_stride);
1231
1232 match yuv_range {
1233 YuvIntensityRange::Tv => {
1234 const PRECISION: i32 = 11;
1235 // All channels on identity should use Y range
1236 let range = yuv_range.get_yuv_range(BIT_DEPTH as u32);
1237 let range_rgba = (1 << BIT_DEPTH) - 1;
1238 let y_coef =
1239 ((range_rgba as f32 / range.range_y as f32) * (1 << PRECISION) as f32) as i32;
1240 let y_bias = range.bias_y as i32;
1241
1242 for (((y_src, u_src), v_src), rgb) in y_iter.zip(u_iter).zip(v_iter).zip(rgb_iter) {
1243 let rgb_chunks = rgb.chunks_exact_mut(CHANNELS);
1244
1245 for (((&y_src, &u_src), &v_src), rgb_dst) in
1246 y_src.iter().zip(u_src).zip(v_src).zip(rgb_chunks)
1247 {
1248 rgb_dst[0] =
1249 qrshr::<PRECISION, BIT_DEPTH>((v_src.as_() - y_bias) * y_coef).as_();
1250 rgb_dst[1] =
1251 qrshr::<PRECISION, BIT_DEPTH>((y_src.as_() - y_bias) * y_coef).as_();
1252 rgb_dst[2] =
1253 qrshr::<PRECISION, BIT_DEPTH>((u_src.as_() - y_bias) * y_coef).as_();
1254 if CHANNELS == 4 {
1255 rgb_dst[3] = max_value.as_();
1256 }
1257 }
1258 }
1259 }
1260 YuvIntensityRange::Pc => {
1261 for (((y_src, u_src), v_src), rgb) in y_iter.zip(u_iter).zip(v_iter).zip(rgb_iter) {
1262 let rgb_chunks = rgb.chunks_exact_mut(CHANNELS);
1263
1264 for (((&y_src, &u_src), &v_src), rgb_dst) in
1265 y_src.iter().zip(u_src).zip(v_src).zip(rgb_chunks)
1266 {
1267 rgb_dst[0] = v_src;
1268 rgb_dst[1] = y_src;
1269 rgb_dst[2] = u_src;
1270 if CHANNELS == 4 {
1271 rgb_dst[3] = max_value.as_();
1272 }
1273 }
1274 }
1275 }
1276 }
1277
1278 Ok(())
1279 }