1#[derive(Clone, Debug)]
6pub struct Plane {
7 pub data: Vec<u16>,
9 pub width: usize,
11 pub height: usize,
13}
14
15pub(crate) fn check_picture_size(width: u32, height: u32) -> Result<(), String> {
21 const MAX_LUMA_SAMPLES: u64 = 35_651_584;
22 let luma = width as u64 * height as u64;
23 if luma > MAX_LUMA_SAMPLES {
24 return Err(format!("{width}x{height} is larger than any level allows"));
25 }
26 let mut probe = Vec::<u16>::new();
28 probe
29 .try_reserve_exact(luma as usize * 3 * 4)
30 .map_err(|_| format!("not enough memory for {width}x{height} pictures"))
31}
32
33impl Plane {
34 pub fn new(width: usize, height: usize, fill: u16) -> Self {
36 Plane {
37 data: vec![fill; width * height],
38 width,
39 height,
40 }
41 }
42
43 #[inline(always)]
45 pub fn at(&self, x: usize, y: usize) -> u16 {
46 self.data[y * self.width + x]
47 }
48
49 #[inline(always)]
51 pub fn set(&mut self, x: usize, y: usize, v: u16) {
52 self.data[y * self.width + x] = v;
53 }
54
55 #[inline(always)]
58 pub fn clamped(&self, x: isize, y: isize) -> u16 {
59 let x = x.clamp(0, self.width as isize - 1) as usize;
60 let y = y.clamp(0, self.height as isize - 1) as usize;
61 self.data[y * self.width + x]
62 }
63}
64
65#[derive(Debug, Clone, Copy, PartialEq, Eq)]
67pub enum ChromaFormat {
68 Monochrome,
70 Yuv420,
72 Yuv422,
74 Yuv444,
76 Yuv440,
78}
79
80impl ChromaFormat {
81 pub fn from_idc(idc: u32) -> Option<Self> {
83 Some(match idc {
84 0 => ChromaFormat::Monochrome,
85 1 => ChromaFormat::Yuv420,
86 2 => ChromaFormat::Yuv422,
87 3 => ChromaFormat::Yuv444,
88 _ => return None,
89 })
90 }
91
92 pub fn subsampling(self) -> (usize, usize) {
94 match self {
95 ChromaFormat::Yuv420 => (2, 2),
96 ChromaFormat::Yuv422 => (2, 1),
97 ChromaFormat::Yuv440 => (1, 2),
98 _ => (1, 1),
99 }
100 }
101}
102
103#[derive(Debug, Clone, Copy, PartialEq, Eq)]
105pub enum Matrix {
106 Bt601,
108 Bt709,
110 Bt2020,
112}
113
114#[derive(Debug, Clone, Copy, PartialEq, Eq)]
116pub struct ColorInfo {
117 pub matrix: Matrix,
119 pub full_range: bool,
121 pub vui_matrix: Option<u32>,
124}
125
126impl ColorInfo {
127 pub fn from_vui(matrix_coefficients: Option<u32>, full_range: bool, height: u32) -> Self {
132 let matrix = match matrix_coefficients {
133 Some(1) => Matrix::Bt709,
134 Some(5) | Some(6) => Matrix::Bt601,
135 Some(9) | Some(10) => Matrix::Bt2020,
136 _ if height >= 720 => Matrix::Bt709,
137 _ => Matrix::Bt601,
138 };
139 ColorInfo {
140 matrix,
141 full_range,
142 vui_matrix: matrix_coefficients,
143 }
144 }
145}
146
147#[derive(Debug, Clone)]
153pub struct PackedFrame {
154 pub width: u32,
156 pub height: u32,
158 pub bit_depth: u8,
160 pub y: Vec<u8>,
162 pub uv: Vec<u8>,
164 pub color: ColorInfo,
166}
167
168impl PackedFrame {
169 pub fn bytes_per_sample(&self) -> usize {
171 if self.bit_depth > 8 {
172 2
173 } else {
174 1
175 }
176 }
177
178 pub fn to_decoded(&self) -> DecodedFrame {
181 let deep = self.bit_depth > 8;
182 let shift = 16 - self.bit_depth.min(16) as u32;
183 let at = |b: &[u8], i: usize| -> u16 {
184 if deep {
185 u16::from_le_bytes([b[2 * i], b[2 * i + 1]]) >> shift
186 } else {
187 b[i] as u16
188 }
189 };
190 let (w, h) = (self.width as usize, self.height as usize);
191 let pairs = w.div_ceil(2) * h.div_ceil(2);
192 DecodedFrame {
193 width: self.width,
194 height: self.height,
195 bit_depth: self.bit_depth,
196 chroma: ChromaFormat::Yuv420,
197 y: (0..w * h).map(|i| at(&self.y, i)).collect(),
198 u: (0..pairs).map(|i| at(&self.uv, 2 * i)).collect(),
199 v: (0..pairs).map(|i| at(&self.uv, 2 * i + 1)).collect(),
200 color: self.color,
201 }
202 }
203}
204
205#[derive(Debug, Clone)]
208pub enum Picture {
209 Planes(DecodedFrame),
211 Packed(PackedFrame),
213}
214
215impl Picture {
216 pub fn size(&self) -> (u32, u32) {
218 match self {
219 Picture::Planes(f) => (f.width, f.height),
220 Picture::Packed(f) => (f.width, f.height),
221 }
222 }
223
224 pub fn into_decoded(self) -> DecodedFrame {
226 match self {
227 Picture::Planes(f) => f,
228 Picture::Packed(f) => f.to_decoded(),
229 }
230 }
231}
232
233#[derive(Debug, Clone)]
235pub struct DecodedFrame {
236 pub width: u32,
238 pub height: u32,
240 pub bit_depth: u8,
242 pub chroma: ChromaFormat,
244 pub y: Vec<u16>,
246 pub u: Vec<u16>,
249 pub v: Vec<u16>,
251 pub color: ColorInfo,
253}
254
255impl DecodedFrame {
256 pub fn chroma_size(&self) -> (u32, u32) {
258 if self.chroma == ChromaFormat::Monochrome {
259 return (0, 0);
260 }
261 let (sw, sh) = self.chroma.subsampling();
262 (
263 self.width.div_ceil(sw as u32),
264 self.height.div_ceil(sh as u32),
265 )
266 }
267
268 #[cfg(feature = "decode")]
270 pub(crate) fn from_planes(
271 planes: &[Plane; 3],
272 chroma: ChromaFormat,
273 bit_depth: u8,
274 crop: [usize; 4],
275 color: ColorInfo,
276 ) -> Self {
277 let [left, right, top, bottom] = crop;
278 let w = planes[0].width.saturating_sub(left + right);
279 let h = planes[0].height.saturating_sub(top + bottom);
280 let cut = |p: &Plane, x0: usize, y0: usize, w: usize, h: usize| {
281 let mut out = Vec::with_capacity(w * h);
282 for y in y0..y0 + h {
283 out.extend_from_slice(&p.data[y * p.width + x0..y * p.width + x0 + w]);
284 }
285 out
286 };
287 let y = cut(&planes[0], left, top, w, h);
288 let (u, v) = if chroma == ChromaFormat::Monochrome {
289 (Vec::new(), Vec::new())
290 } else {
291 let (sw, sh) = chroma.subsampling();
292 let (cw, ch) = (w.div_ceil(sw), h.div_ceil(sh));
293 (
294 cut(&planes[1], left / sw, top / sh, cw, ch),
295 cut(&planes[2], left / sw, top / sh, cw, ch),
296 )
297 };
298 DecodedFrame {
299 width: w as u32,
300 height: h as u32,
301 bit_depth,
302 chroma,
303 y,
304 u,
305 v,
306 color,
307 }
308 }
309
310 pub fn to_raw_yuv(&self) -> Vec<u8> {
313 let mut out = Vec::new();
314 for plane in [&self.y, &self.u, &self.v] {
315 if self.bit_depth <= 8 {
316 out.extend(plane.iter().map(|&s| s as u8));
317 } else {
318 for &s in plane.iter() {
319 out.extend_from_slice(&s.to_le_bytes());
320 }
321 }
322 }
323 out
324 }
325
326 pub fn to_bgra(&self) -> Vec<u8> {
330 let mut out = Vec::new();
331 self.convert_into::<true>(&mut out);
332 out
333 }
334
335 pub fn to_rgba(&self) -> Vec<u8> {
337 let mut out = Vec::new();
338 self.to_rgba_into(&mut out);
339 out
340 }
341
342 pub fn to_rgba_into(&self, out: &mut Vec<u8>) {
346 self.convert_into::<false>(out);
347 }
348
349 fn convert_into<const BGR: bool>(&self, out: &mut Vec<u8>) {
350 let (w, h) = (self.width as usize, self.height as usize);
351 out.resize(w * h * 4, 0);
352 if w == 0 || h == 0 {
353 return;
354 }
355 let k = Coefficients::new(self);
356 let rows_each = rows_per_band(w, h);
357 if rows_each >= h {
358 self.convert_rows::<BGR>(0..h, out, &k);
359 return;
360 }
361 let bands: Vec<_> = out.chunks_mut(rows_each * w * 4).enumerate().collect();
362 crate::for_each_owned(bands, |(n, band)| {
363 let start = n * rows_each;
364 self.convert_rows::<BGR>(start..(start + rows_each).min(h), band, &k);
365 });
366 }
367
368 fn convert_rows<const BGR: bool>(
381 &self,
382 rows: std::ops::Range<usize>,
383 out: &mut [u8],
384 k: &Coefficients,
385 ) {
386 let w = self.width as usize;
387 let mono = self.chroma == ChromaFormat::Monochrome;
388 let (sw, sh) = self.chroma.subsampling();
389 let (cw, ch) = self.chroma_size();
390 let (cw, ch) = (cw as usize, ch as usize);
391 let mid = k.c_mid;
392 let mut ru = vec![0i32; cw];
394 let mut rv = vec![0i32; cw];
395 let mut cb = vec![0i32; w];
397 let mut cr = vec![0i32; w];
398 for (row, y) in rows.enumerate() {
399 if !mono {
400 let (a, b) = if sh == 2 {
401 let c = (y / 2).min(ch - 1);
402 let other = if y % 2 == 0 {
403 c.saturating_sub(1)
404 } else {
405 (c + 1).min(ch - 1)
406 };
407 (c, other)
408 } else {
409 (y.min(ch - 1), y.min(ch - 1))
410 };
411 let (wa, wb) = if sh == 2 { (3, 1) } else { (4, 0) };
412 let (ua, ub) = (&self.u[a * cw..][..cw], &self.u[b * cw..][..cw]);
413 let (va, vb) = (&self.v[a * cw..][..cw], &self.v[b * cw..][..cw]);
414 for i in 0..cw {
415 ru[i] = wa * ua[i] as i32 + wb * ub[i] as i32 - 4 * mid;
416 rv[i] = wa * va[i] as i32 + wb * vb[i] as i32 - 4 * mid;
417 }
418 if sw == 2 {
419 upsample_row(&ru, &mut cb);
420 upsample_row(&rv, &mut cr);
421 } else {
422 for x in 0..w {
423 cb[x] = 2 * ru[x];
424 cr[x] = 2 * rv[x];
425 }
426 }
427 }
428 let luma = &self.y[y * w..][..w];
429 let line = &mut out[row * w * 4..][..w * 4];
430 let to8 = |v: i32| ((v + (1 << 15)) >> 16).clamp(0, 255) as u32;
431 for (((px, &l), &cb), &cr) in line
432 .as_chunks_mut::<4>()
433 .0
434 .iter_mut()
435 .zip(luma)
436 .zip(&cb)
437 .zip(&cr)
438 {
439 let yv = k.y * (l as i32 - k.y_off);
440 let r = to8(yv + k.rv * cr);
441 let g = to8(yv - k.gu * cb - k.gv * cr);
442 let b = to8(yv + k.bu * cb);
443 let (first, last) = if BGR { (b, r) } else { (r, b) };
444 px.copy_from_slice(&(first | g << 8 | last << 16 | 255 << 24).to_le_bytes());
445 }
446 }
447 }
448}
449
450fn upsample_row(row: &[i32], out: &mut [i32]) {
454 let last = row.len() - 1;
455 for (j, pair) in out.chunks_mut(2).enumerate() {
456 pair[0] = 2 * row[j];
457 if let Some(odd) = pair.get_mut(1) {
458 *odd = row[j] + row[(j + 1).min(last)];
459 }
460 }
461}
462
463struct Coefficients {
466 y: i32,
467 y_off: i32,
468 c_mid: i32,
469 rv: i32,
470 gu: i32,
471 gv: i32,
472 bu: i32,
473}
474
475impl Coefficients {
476 fn new(f: &DecodedFrame) -> Self {
477 let max = ((1u32 << f.bit_depth) - 1) as f64;
478 let s = (1u32 << (f.bit_depth - 8)) as f64;
479 let (y_off, y_scale, c_scale) = if f.color.full_range {
481 (0.0, 1.0 / max, 1.0 / max)
482 } else {
483 (16.0 * s, 1.0 / (219.0 * s), 1.0 / (224.0 * s))
484 };
485 let (kr, kb) = match f.color.matrix {
486 Matrix::Bt601 => (0.299, 0.114),
487 Matrix::Bt709 => (0.2126, 0.0722),
488 Matrix::Bt2020 => (0.2627, 0.0593),
489 };
490 let kg = 1.0 - kr - kb;
491 let fixed = |v: f64| (v * 65536.0).round() as i32;
492 let c = 255.0 * c_scale / 8.0;
493 Coefficients {
494 y: fixed(255.0 * y_scale),
495 y_off: y_off as i32,
496 c_mid: 1 << (f.bit_depth - 1),
497 rv: fixed(2.0 * (1.0 - kr) * c),
498 gu: fixed(2.0 * kb * (1.0 - kb) / kg * c),
499 gv: fixed(2.0 * kr * (1.0 - kr) / kg * c),
500 bu: fixed(2.0 * (1.0 - kb) * c),
501 }
502 }
503}
504
505fn rows_per_band(w: usize, h: usize) -> usize {
508 if !crate::threaded() || w * h < 1 << 17 {
509 return h;
510 }
511 ((1 << 16) / w).max(8)
512}
513
514#[cfg(test)]
515mod tests {
516 use super::*;
517
518 fn frame(
519 w: u32,
520 h: u32,
521 y: Vec<u16>,
522 u: Vec<u16>,
523 v: Vec<u16>,
524 matrix: Matrix,
525 ) -> DecodedFrame {
526 DecodedFrame {
527 width: w,
528 height: h,
529 bit_depth: 8,
530 chroma: ChromaFormat::Yuv420,
531 y,
532 u,
533 v,
534 color: ColorInfo {
535 matrix,
536 full_range: false,
537 vui_matrix: None,
538 },
539 }
540 }
541
542 #[test]
543 fn flat_colours_convert_like_the_formula() {
544 for (yuv, rgb) in [
547 ((81, 90, 240), [255, 0, 0]),
548 ((16, 128, 128), [0, 0, 0]),
549 ((235, 128, 128), [255, 255, 255]),
550 ] {
551 let f = frame(
552 4,
553 4,
554 vec![yuv.0; 16],
555 vec![yuv.1; 4],
556 vec![yuv.2; 4],
557 Matrix::Bt601,
558 );
559 for px in f.to_rgba().as_chunks::<4>().0 {
560 for c in 0..3 {
561 assert!(
562 (px[c] as i32 - rgb[c]).abs() <= 1,
563 "{yuv:?} → {px:?}, want {rgb:?}"
564 );
565 }
566 assert_eq!(px[3], 255);
567 }
568 }
569 }
570
571 #[test]
572 fn chroma_is_interpolated_between_samples() {
573 let (w, h) = (4u32, 2u32);
577 let f = frame(
578 w,
579 h,
580 vec![128; 8],
581 vec![128, 228],
582 vec![128, 128],
583 Matrix::Bt709,
584 );
585 let bgra = f.to_bgra();
586 let blue: Vec<u8> = (0..w as usize).map(|x| bgra[x * 4]).collect();
587 assert!(blue[0] < blue[1] && blue[1] < blue[2], "{blue:?}");
588 assert_eq!(blue[2], blue[3], "past the last sample, held: {blue:?}");
589 }
590
591 #[test]
592 fn threaded_conversion_matches_one_thread() {
593 let (w, h) = (640u32, 480u32);
594 let n = (w * h) as usize;
595 let y: Vec<u16> = (0..n).map(|i| (i * 7 % 220 + 16) as u16).collect();
596 let u: Vec<u16> = (0..n / 4).map(|i| (i * 13 % 224 + 16) as u16).collect();
597 let v: Vec<u16> = (0..n / 4).map(|i| (i * 29 % 224 + 16) as u16).collect();
598 let f = frame(w, h, y, u, v, Matrix::Bt709);
599 assert!(rows_per_band(w as usize, h as usize) < h as usize || !crate::threaded());
600 let mut single = vec![255u8; n * 4];
601 f.convert_rows::<true>(0..h as usize, &mut single, &Coefficients::new(&f));
602 assert!(f.to_bgra() == single);
603 }
604
605 fn reference_rgba(f: &DecodedFrame) -> Vec<u8> {
608 let k = Coefficients::new(f);
609 let (w, h) = (f.width as usize, f.height as usize);
610 let mono = f.chroma == ChromaFormat::Monochrome;
611 let (sw, sh) = f.chroma.subsampling();
612 let (cw, ch) = f.chroma_size();
613 let (cw, ch) = (cw as usize, ch as usize);
614 let to8 = |v: i32| ((v + (1 << 15)) >> 16).clamp(0, 255) as u8;
615 let mut out = vec![255u8; w * h * 4];
616 for y in 0..h {
617 for x in 0..w {
618 let (mut cb, mut cr) = (0, 0);
619 if !mono {
620 let (a, b) = if sh == 2 {
621 let c = (y / 2).min(ch - 1);
622 (
623 c,
624 if y % 2 == 0 {
625 c.saturating_sub(1)
626 } else {
627 (c + 1).min(ch - 1)
628 },
629 )
630 } else {
631 (y.min(ch - 1), y.min(ch - 1))
632 };
633 let (wa, wb) = if sh == 2 { (3, 1) } else { (4, 0) };
634 let vert = |p: &[u16], i: usize| {
635 wa * p[a * cw + i] as i32 + wb * p[b * cw + i] as i32 - 4 * k.c_mid
636 };
637 (cb, cr) = if sw == 2 && x % 2 == 1 {
638 let (j, n) = (x / 2, (x / 2 + 1).min(cw - 1));
639 (vert(&f.u, j) + vert(&f.u, n), vert(&f.v, j) + vert(&f.v, n))
640 } else {
641 let j = if sw == 2 { x / 2 } else { x };
642 (2 * vert(&f.u, j), 2 * vert(&f.v, j))
643 };
644 }
645 let yv = k.y * (f.y[y * w + x] as i32 - k.y_off);
646 let px = &mut out[(y * w + x) * 4..][..3];
647 px[0] = to8(yv + k.rv * cr);
648 px[1] = to8(yv - k.gu * cb - k.gv * cr);
649 px[2] = to8(yv + k.bu * cb);
650 }
651 }
652 out
653 }
654
655 #[test]
656 fn rows_match_the_per_pixel_definition() {
657 let mut x = 0x9e37_79b9u32;
658 let mut rnd = move |n: u32| {
659 x ^= x << 13;
660 x ^= x >> 17;
661 x ^= x << 5;
662 x % n
663 };
664 for chroma in [
665 ChromaFormat::Yuv420,
666 ChromaFormat::Yuv422,
667 ChromaFormat::Yuv444,
668 ChromaFormat::Monochrome,
669 ] {
670 for bit_depth in [8u8, 10, 12] {
671 for (w, h) in [(1, 1), (2, 2), (3, 5), (17, 9), (64, 33), (333, 21)] {
672 let mut f = frame(w, h, Vec::new(), Vec::new(), Vec::new(), Matrix::Bt709);
673 (f.chroma, f.bit_depth) = (chroma, bit_depth);
674 let (cw, ch) = f.chroma_size();
675 let max = 1u32 << bit_depth;
676 f.y = (0..w * h).map(|_| rnd(max) as u16).collect();
677 f.u = (0..cw * ch).map(|_| rnd(max) as u16).collect();
678 f.v = (0..cw * ch).map(|_| rnd(max) as u16).collect();
679 let want = reference_rgba(&f);
680 assert!(f.to_rgba() == want, "{chroma:?} {bit_depth}-bit {w}x{h}");
681 let bgr: Vec<u8> = want
682 .chunks(4)
683 .flat_map(|p| [p[2], p[1], p[0], p[3]])
684 .collect();
685 assert!(
686 f.to_bgra() == bgr,
687 "{chroma:?} {bit_depth}-bit {w}x{h} BGRA"
688 );
689 }
690 }
691 }
692 }
693}