Skip to main content

threers_codec/decode/
picture.rs

1//! Sample planes and the decoded frame the decoders hand back.
2
3/// One plane of samples. Stored as `u16` whatever the bit depth, so 8-bit and
4/// 10-bit streams share every reconstruction path.
5#[derive(Clone, Debug)]
6pub struct Plane {
7    /// Samples, row by row, `width * height` of them.
8    pub data: Vec<u16>,
9    /// Width in samples.
10    pub width: usize,
11    /// Height in samples.
12    pub height: usize,
13}
14
15/// Refuse a picture too large to decode, as an error. A damaged header can
16/// claim any size, and allocation failure aborts: on wasm, with memory capped,
17/// that takes the whole instance down. No level of AV1, H.264, HEVC or VP9
18/// allows more luma samples than this, and the memory for a few such pictures
19/// has to be there before anything is sized from the header.
20pub(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    // Four 4:4:4 pictures of 16-bit samples.
27    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    /// A `width × height` plane with every sample set to `fill`.
35    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    /// The sample at `(x, y)`.
44    #[inline(always)]
45    pub fn at(&self, x: usize, y: usize) -> u16 {
46        self.data[y * self.width + x]
47    }
48
49    /// Set the sample at `(x, y)`.
50    #[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    /// The sample at `(x, y)` with the coordinates clamped into the plane —
56    /// what motion compensation reads when a vector points off the edge.
57    #[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/// Chroma subsampling of a decoded frame.
66#[derive(Debug, Clone, Copy, PartialEq, Eq)]
67pub enum ChromaFormat {
68    /// Luma only.
69    Monochrome,
70    /// Chroma halved both ways.
71    Yuv420,
72    /// Chroma halved horizontally.
73    Yuv422,
74    /// Full-resolution chroma.
75    Yuv444,
76    /// Chroma halved vertically only (VP9 profile 1).
77    Yuv440,
78}
79
80impl ChromaFormat {
81    /// From H.264/HEVC `chroma_format_idc`, 0 to 3.
82    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    /// `(SubWidthC, SubHeightC)`.
93    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/// The YCbCr → RGB matrix a stream says it was coded with.
104#[derive(Debug, Clone, Copy, PartialEq, Eq)]
105pub enum Matrix {
106    /// ITU-R BT.601 (SD).
107    Bt601,
108    /// ITU-R BT.709 (HD).
109    Bt709,
110    /// ITU-R BT.2020 (UHD, non-constant luminance).
111    Bt2020,
112}
113
114/// What the VUI says about colour, reduced to what conversion needs.
115#[derive(Debug, Clone, Copy, PartialEq, Eq)]
116pub struct ColorInfo {
117    /// The YCbCr → RGB matrix.
118    pub matrix: Matrix,
119    /// Full-range (0–255 at 8 bits) rather than video range (16–235).
120    pub full_range: bool,
121    /// The VUI's `matrix_coefficients` as coded (`None` without a colour
122    /// description) — what ffmpeg's swscale keys its tables on.
123    pub vui_matrix: Option<u32>,
124}
125
126impl ColorInfo {
127    /// From the VUI's `matrix_coefficients` and `video_full_range_flag`.
128    ///
129    /// An unspecified matrix (2, or no VUI at all) is a guess either way;
130    /// this makes the same one players do — BT.709 for HD, BT.601 below it.
131    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/// A 4:2:0 picture as hardware decoders keep it: a luma plane and one plane
148/// of Cb and Cr interleaved (NV12; P010 above 8 bits, two little-endian bytes
149/// a sample with the value in the high bits). VideoToolbox, NVDEC, VAAPI and
150/// the browser's WebCodecs all decode to this, so handing it on as it is, to
151/// a GPU that converts it, costs no pass over the pixels.
152#[derive(Debug, Clone)]
153pub struct PackedFrame {
154    /// Display width in pixels.
155    pub width: u32,
156    /// Display height in pixels.
157    pub height: u32,
158    /// Bits a sample: 8 (NV12), or 10 to 16 (P010, P016).
159    pub bit_depth: u8,
160    /// Luma, `width * height` samples, tightly packed.
161    pub y: Vec<u8>,
162    /// Cb, Cr, Cb, Cr…: `ceil(width / 2) * ceil(height / 2)` pairs.
163    pub uv: Vec<u8>,
164    /// What the stream says about its colour.
165    pub color: ColorInfo,
166}
167
168impl PackedFrame {
169    /// Bytes a sample: 1 for NV12, 2 above 8 bits.
170    pub fn bytes_per_sample(&self) -> usize {
171        if self.bit_depth > 8 {
172            2
173        } else {
174            1
175        }
176    }
177
178    /// As planes of `u16` samples, the form the native decoders give: one
179    /// pass over the picture, for callers that want [`DecodedFrame`].
180    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/// A decoded picture: planes from the native decoders, or packed as the
206/// hardware ones give it.
207#[derive(Debug, Clone)]
208pub enum Picture {
209    /// Separate planes, `u16` a sample.
210    Planes(DecodedFrame),
211    /// NV12 or P010.
212    Packed(PackedFrame),
213}
214
215impl Picture {
216    /// Its size in pixels.
217    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    /// As planes, converting a packed picture.
225    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/// A decoded picture, cropped to its display size.
234#[derive(Debug, Clone)]
235pub struct DecodedFrame {
236    /// Display width in pixels.
237    pub width: u32,
238    /// Display height in pixels.
239    pub height: u32,
240    /// Bits per sample, 8 to 12.
241    pub bit_depth: u8,
242    /// Chroma subsampling.
243    pub chroma: ChromaFormat,
244    /// Luma, `width * height`, tightly packed.
245    pub y: Vec<u16>,
246    /// Chroma planes, [`chroma_size`](Self::chroma_size) each. Empty for
247    /// monochrome.
248    pub u: Vec<u16>,
249    /// Cr, laid out like `u`.
250    pub v: Vec<u16>,
251    /// What the stream says about its colour.
252    pub color: ColorInfo,
253}
254
255impl DecodedFrame {
256    /// Size of each chroma plane.
257    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    /// Crop planes out of the coded picture.
269    #[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    /// The planes as ffmpeg's `rawvideo` writes them for the stream's native
311    /// pixel format: one byte a sample at 8 bits, two (little-endian) above.
312    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    /// Convert to tightly packed 8-bit BGRA, alpha 255, with the stream's
327    /// matrix and range ([`ColorInfo`]) and bilinear chroma upsampling.
328    /// Large pictures are converted on several threads with `parallel`.
329    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    /// [`to_bgra`](Self::to_bgra), in RGBA order.
336    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    /// [`to_rgba`](Self::to_rgba) into `out`, replacing what it holds. Reusing
343    /// one buffer across frames saves allocating and clearing a whole picture
344    /// each time.
345    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    /// Rows `rows` of the picture into `out`, which holds exactly those rows.
369    ///
370    /// Chroma is upsampled bilinearly from where the samples sit by default
371    /// in H.264 and HEVC (`chroma_sample_loc_type` 0): level with the even
372    /// luma columns, and halfway between luma rows for 4:2:0. Each output
373    /// pixel's chroma is a weighted sum scaled by 8 — vertical weights 3:1
374    /// (or 4), horizontal 1:1 at odd columns (or 2) — so the arithmetic stays
375    /// in integers.
376    ///
377    /// A row's chroma is upsampled first, so the pixel loop has no branches
378    /// and vectorises; on wasm, where this runs on one thread beside the
379    /// decoder, it is otherwise the slowest stage of decoding to RGBA.
380    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        // One chroma row, interpolated vertically, scaled by 4, centred on 0.
393        let mut ru = vec![0i32; cw];
394        let mut rv = vec![0i32; cw];
395        // The same across the picture's width, scaled by 8.
396        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
450/// `row`, sited on even columns, across twice the width: each even column as
451/// it is and doubled, each odd one the sum of its neighbours. The picture's
452/// last column may be even and past the last pair.
453fn 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
463/// YCbCr → 8-bit RGB in 16.16 fixed point. Chroma coefficients include the
464/// ÷8 of [`DecodedFrame::convert_rows`]' interpolation weights.
465struct 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        // Y in [0, 1] and C in [-0.5, 0.5], then to 0..255.
480        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
505/// Rows to convert at a time on the thread pool: enough that a band outweighs
506/// handing it over, and all of a small picture.
507fn 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        // BT.601 limited: (81, 90, 240) is pure red, (16, 128, 128) black,
545        // (235, 128, 128) white.
546        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        // Cb steps from 128 to 228 between chroma columns 0 and 1 (luma 0–1
574        // and 2–3). Column 1 sits halfway: its blue is between its
575        // neighbours', not equal to one of them.
576        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    /// The conversion pixel by pixel, as it reads in the definition: the
606    /// reference for the vectorised rows.
607    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}