Skip to main content

camera/
conversion.rs

1//! Reusable conversion into caller-owned RGB storage; no implicit frame copies.
2#[cfg(feature = "runtime-tokio")]
3use crate::CapturedFrame;
4use crate::{
5    utils::color_convert::{self, Yuv420Sp, YuvPlane},
6    CameraError, CameraResult, ColorInfo, FrameLayout, PixelFormat,
7};
8
9/// Describes the RGB image a consumer actually needs.
10///
11/// A smaller target is produced directly from raw frames and uses TurboJPEG's
12/// DCT scaling for MJPEG when a supported factor can reduce the decode work.
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14pub struct ConversionRequest {
15    /// Frame width in pixels.
16    pub width: u32,
17    /// Frame height in pixels.
18    pub height: u32,
19    /// The color override value.
20    pub color_override: Option<ColorInfo>,
21}
22impl ConversionRequest {
23    /// Performs the `new` operation.
24    pub fn new(width: u32, height: u32) -> CameraResult<Self> {
25        let request = Self {
26            width,
27            height,
28            color_override: None,
29        };
30        request.output_len()?;
31        Ok(request)
32    }
33    /// Performs the `for_layout` operation.
34    pub fn for_layout(layout: &FrameLayout) -> Self {
35        Self {
36            width: layout.width,
37            height: layout.height,
38            color_override: None,
39        }
40    }
41    /// Performs the `with_color` operation.
42    pub fn with_color(mut self, color: ColorInfo) -> Self {
43        self.color_override = Some(color);
44        self
45    }
46    /// Performs the `output_len` operation.
47    pub fn output_len(self) -> CameraResult<usize> {
48        (self.width as usize)
49            .checked_mul(self.height as usize)
50            .and_then(|pixels| pixels.checked_mul(3))
51            .filter(|&bytes| bytes > 0)
52            .ok_or_else(|| CameraError::invalid_frame("Invalid RGB output dimensions".into()))
53    }
54}
55
56fn is_exact_half(layout: &FrameLayout, request: ConversionRequest) -> bool {
57    let (width, height) = (layout.width as usize, layout.height as usize);
58    !layout.bottom_up
59        && width.is_multiple_of(2)
60        && height.is_multiple_of(2)
61        && (request.width as usize, request.height as usize) == (width / 2, height / 2)
62}
63
64fn is_exact_two_thirds(layout: &FrameLayout, request: ConversionRequest) -> bool {
65    u64::from(request.width) * 3 == u64::from(layout.width) * 2
66        && u64::from(request.height) * 3 == u64::from(layout.height) * 2
67}
68
69fn is_exact_three_quarters(layout: &FrameLayout, request: ConversionRequest) -> bool {
70    u64::from(request.width) * 4 == u64::from(layout.width) * 3
71        && u64::from(request.height) * 4 == u64::from(layout.height) * 3
72}
73
74fn direct_half_path(layout: &FrameLayout, request: ConversionRequest) -> Option<&'static str> {
75    if !is_exact_half(layout, request) {
76        return None;
77    }
78    match layout.format {
79        PixelFormat::Nv12 | PixelFormat::Nv21
80            if layout.planes.len() == 2
81                && layout.planes[0].pixel_stride == 1
82                && layout.planes[1].pixel_stride == 2 =>
83        {
84            Some("nv12-direct-half")
85        }
86        PixelFormat::Yuyv | PixelFormat::Uyvy
87            if layout.planes.len() == 1 && layout.planes[0].pixel_stride == 2 =>
88        {
89            Some("yuv422-direct-half")
90        }
91        PixelFormat::Yuv420p
92            if layout.planes.len() == 3
93                && layout.planes.iter().all(|plane| plane.pixel_stride == 1) =>
94        {
95            Some("yuv420p-direct-half")
96        }
97        _ => None,
98    }
99}
100
101fn supports_yuv_row_conversion(layout: &FrameLayout) -> bool {
102    match layout.format {
103        PixelFormat::Yuyv | PixelFormat::Uyvy => true,
104        PixelFormat::Nv12 | PixelFormat::Nv21 => {
105            layout.planes.len() == 2
106                && layout.planes[0].pixel_stride == 1
107                && layout.planes[1].pixel_stride == 2
108        }
109        PixelFormat::Yuv420p => {
110            layout.planes.len() == 3 && layout.planes.iter().all(|plane| plane.pixel_stride == 1)
111        }
112        _ => false,
113    }
114}
115
116/// Reports the production conversion strategy used for a valid layout/request pair.
117/// Intended for diagnostics and benchmark output, so benchmark labels cannot drift
118/// from the real dispatch conditions.
119pub fn selected_conversion_path(layout: &FrameLayout, request: ConversionRequest) -> &'static str {
120    if layout.format == PixelFormat::Mjpeg {
121        return "jpeg-adaptive-decode-scale";
122    }
123    if layout.format == PixelFormat::H264 {
124        return "unsupported-h264";
125    }
126    if let Some(path) = direct_half_path(layout, request) {
127        return path;
128    }
129    if (request.width, request.height) == (layout.width, layout.height) && !layout.bottom_up {
130        return "direct-full-size";
131    }
132    if layout.format == PixelFormat::Rgb8
133        && layout
134            .planes
135            .first()
136            .is_some_and(|plane| plane.pixel_stride == 3)
137    {
138        if is_exact_two_thirds(layout, request) {
139            return "rgb-row-nearest-2x3";
140        }
141        if is_exact_three_quarters(layout, request) {
142            return "rgb-row-nearest-3x4";
143        }
144    }
145    let yuv = matches!(
146        layout.format,
147        PixelFormat::Yuyv
148            | PixelFormat::Uyvy
149            | PixelFormat::Nv12
150            | PixelFormat::Nv21
151            | PixelFormat::Yuv420p
152    );
153    if yuv
154        && supports_yuv_row_conversion(layout)
155        && (request.width as usize).saturating_mul(4) >= layout.width as usize
156    {
157        if is_exact_two_thirds(layout, request) {
158            "yuv-row-convert-nearest-2x3"
159        } else if is_exact_three_quarters(layout, request) {
160            "yuv-row-convert-nearest-3x4"
161        } else {
162            "yuv-row-convert-nearest"
163        }
164    } else {
165        "mapped-nearest"
166    }
167}
168
169#[derive(Default)]
170struct NearestMap {
171    dimensions: (usize, usize, usize, usize, bool),
172    x: Vec<usize>,
173    y: Vec<usize>,
174}
175
176impl NearestMap {
177    fn update(
178        &mut self,
179        source_width: usize,
180        source_height: usize,
181        destination_width: usize,
182        destination_height: usize,
183        bottom_up: bool,
184    ) {
185        let dimensions = (
186            source_width,
187            source_height,
188            destination_width,
189            destination_height,
190            bottom_up,
191        );
192        if self.dimensions == dimensions {
193            return;
194        }
195        self.dimensions = dimensions;
196        self.x.resize(destination_width, 0);
197        self.y.resize(destination_height, 0);
198        for (destination, source) in self.x.iter_mut().enumerate() {
199            *source = destination * source_width / destination_width;
200        }
201        for (destination, source) in self.y.iter_mut().enumerate() {
202            let row = destination * source_height / destination_height;
203            *source = if bottom_up {
204                source_height - row - 1
205            } else {
206                row
207            };
208        }
209    }
210}
211
212#[derive(Default)]
213/// Values for `RgbConverter`.
214pub struct RgbConverter {
215    #[cfg(feature = "decode-mjpeg")]
216    jpeg: Option<turbojpeg::Decompressor>,
217    #[cfg(feature = "decode-mjpeg")]
218    jpeg_scratch: Vec<u8>,
219    nearest: NearestMap,
220    rgb_row_scratch: Vec<u8>,
221}
222impl RgbConverter {
223    /// Performs the `new` operation.
224    pub fn new() -> Self {
225        Self::default()
226    }
227    /// Convert without applying orientation/mirroring. Unknown YUV colorimetry
228    /// requires an explicit override instead of silently assuming a matrix/range.
229    #[cfg(feature = "runtime-tokio")]
230    pub fn convert_into(
231        &mut self,
232        frame: &CapturedFrame,
233        request: ConversionRequest,
234        output: &mut [u8],
235    ) -> CameraResult<()> {
236        self.convert_layout_into(frame.layout(), frame.bytes(), request, output)
237    }
238    /// Performs the `convert_layout_into` operation.
239    pub fn convert_layout_into(
240        &mut self,
241        layout: &FrameLayout,
242        data: &[u8],
243        request: ConversionRequest,
244        out: &mut [u8],
245    ) -> CameraResult<()> {
246        layout.validate(data.len())?;
247        let (w, h) = (layout.width as usize, layout.height as usize);
248        let (out_w, out_h) = (request.width as usize, request.height as usize);
249        if request.output_len()? != out.len() {
250            return Err(CameraError::invalid_frame(
251                "RGB destination size mismatch".into(),
252            ));
253        }
254        if layout.format == PixelFormat::Mjpeg {
255            #[cfg(feature = "decode-mjpeg")]
256            {
257                let (decoder, scratch, nearest) =
258                    (&mut self.jpeg, &mut self.jpeg_scratch, &mut self.nearest);
259                return crate::mjpeg::decode_with(decoder, data, |decoder, data| {
260                    let header = decoder
261                        .read_header(data)
262                        .map_err(|e| CameraError::invalid_frame(e.to_string()))?;
263                    if (header.width, header.height) != (w, h) {
264                        return Err(CameraError::invalid_frame(
265                            "JPEG dimensions differ from layout".into(),
266                        ));
267                    }
268                    let scaling = jpeg_scaling(&header, out_w, out_h);
269                    decoder
270                        .set_scaling_factor(scaling)
271                        .map_err(|e| CameraError::invalid_frame(e.to_string()))?;
272                    let scaled = header.scaled(scaling);
273                    if (scaled.width, scaled.height) == (out_w, out_h) {
274                        return decoder
275                            .decompress(
276                                data,
277                                turbojpeg::Image {
278                                    pixels: out,
279                                    width: out_w,
280                                    height: out_h,
281                                    pitch: out_w * 3,
282                                    format: turbojpeg::PixelFormat::RGB,
283                                },
284                            )
285                            .map_err(|e| CameraError::invalid_frame(e.to_string()));
286                    }
287                    let scratch_len = scaled
288                        .width
289                        .checked_mul(scaled.height)
290                        .and_then(|pixels| pixels.checked_mul(3))
291                        .ok_or_else(|| {
292                            CameraError::invalid_frame("JPEG output size overflow".into())
293                        })?;
294                    scratch.resize(scratch_len, 0);
295                    decoder
296                        .decompress(
297                            data,
298                            turbojpeg::Image {
299                                pixels: scratch,
300                                width: scaled.width,
301                                height: scaled.height,
302                                pitch: scaled.width * 3,
303                                format: turbojpeg::PixelFormat::RGB,
304                            },
305                        )
306                        .map_err(|e| CameraError::invalid_frame(e.to_string()))?;
307                    nearest.update(scaled.width, scaled.height, out_w, out_h, false);
308                    resize_rgb_nearest(scratch, scaled.width, out, out_w, &nearest.x, &nearest.y);
309                    Ok(())
310                });
311            }
312            #[cfg(not(feature = "decode-mjpeg"))]
313            return Err(CameraError::unsupported_format(
314                "Enable jpeg for MJPEG decoding".into(),
315            ));
316        }
317        if layout.format == PixelFormat::H264 {
318            return Err(CameraError::unsupported_format(
319                "H264 decoding requires an external video decoder".into(),
320            ));
321        }
322        let yuv = matches!(
323            layout.format,
324            PixelFormat::Yuyv
325                | PixelFormat::Uyvy
326                | PixelFormat::Nv12
327                | PixelFormat::Nv21
328                | PixelFormat::Yuv420p
329        );
330        let color = request.color_override.unwrap_or(layout.color);
331        let coeff = if yuv {
332            Some(color_convert::color_coefficients(color)?)
333        } else {
334            None
335        };
336        if direct_half_path(layout, request).is_some() {
337            let p = |index: usize| {
338                let plane = &layout.planes[index];
339                YuvPlane {
340                    data: &data[plane.offset..plane.offset + plane.length],
341                    row_stride: plane.row_stride,
342                    pixel_stride: plane.pixel_stride,
343                }
344            };
345            return match layout.format {
346                PixelFormat::Nv12 | PixelFormat::Nv21 => {
347                    let y = &layout.planes[0];
348                    let uv = &layout.planes[1];
349                    color_convert::yuv420sp_to_rgb_half_with_coefficients_into(
350                        Yuv420Sp {
351                            y: &data[y.offset..y.offset + y.length],
352                            uv: &data[uv.offset..uv.offset + uv.length],
353                            y_stride: y.row_stride,
354                            uv_stride: uv.row_stride,
355                            vu_order: layout.format == PixelFormat::Nv21,
356                        },
357                        w,
358                        h,
359                        coeff.unwrap(),
360                        out,
361                    )
362                }
363                PixelFormat::Yuyv | PixelFormat::Uyvy => {
364                    let plane = &layout.planes[0];
365                    color_convert::yuv422_to_rgb_half_with_coefficients_into(
366                        &data[plane.offset..plane.offset + plane.length],
367                        w,
368                        h,
369                        plane.row_stride,
370                        layout.format == PixelFormat::Uyvy,
371                        coeff.unwrap(),
372                        out,
373                    )
374                }
375                PixelFormat::Yuv420p => color_convert::yuv420_to_rgb_half_with_coefficients_into(
376                    p(0),
377                    p(1),
378                    p(2),
379                    w,
380                    h,
381                    coeff.unwrap(),
382                    out,
383                ),
384                _ => unreachable!(),
385            };
386        }
387        // Same-size top-down frames use checked vectorized kernels. Scaling and
388        // bottom-up layouts keep a direct scalar path without an intermediate image.
389        if (out_w, out_h) == (w, h) && !layout.bottom_up {
390            let plane = &layout.planes[0];
391            let source = &data[plane.offset..plane.offset + plane.length];
392            if layout.format == PixelFormat::Rgb8 && plane.pixel_stride == 3 {
393                for y in 0..h {
394                    out[y * w * 3..(y + 1) * w * 3].copy_from_slice(
395                        &source[y * plane.row_stride..y * plane.row_stride + w * 3],
396                    );
397                }
398                return Ok(());
399            }
400            if layout.format == PixelFormat::Rgba8 && plane.pixel_stride == 4 {
401                return crate::utils::color_convert::rgba8888_to_rgb_into(
402                    source,
403                    w,
404                    h,
405                    plane.row_stride,
406                    out,
407                );
408            }
409            if layout.format == PixelFormat::Bgra8 && plane.pixel_stride == 4 {
410                return crate::utils::color_convert::bgra8888_to_rgb_into(
411                    source,
412                    w,
413                    h,
414                    plane.row_stride,
415                    out,
416                );
417            }
418            if layout.format == PixelFormat::Argb8 && plane.pixel_stride == 4 {
419                return crate::utils::color_convert::argb8888_to_rgb_into(
420                    source,
421                    w,
422                    h,
423                    plane.row_stride,
424                    out,
425                );
426            }
427            let p = |i: usize, shift: usize| {
428                let plane = &layout.planes[i];
429                YuvPlane {
430                    data: &data[plane.offset + shift..plane.offset + plane.length],
431                    row_stride: plane.row_stride,
432                    pixel_stride: plane.pixel_stride,
433                }
434            };
435            match layout.format {
436                PixelFormat::Yuv420p => {
437                    return color_convert::yuv420_to_rgb_with_coefficients_into(
438                        p(0, 0),
439                        p(1, 0),
440                        p(2, 0),
441                        w,
442                        h,
443                        coeff.unwrap(),
444                        out,
445                    );
446                }
447                PixelFormat::Nv12 | PixelFormat::Nv21 => {
448                    let y = &layout.planes[0];
449                    let uv = &layout.planes[1];
450                    if y.pixel_stride == 1 && uv.pixel_stride == 2 {
451                        return color_convert::yuv420sp_to_rgb_with_coefficients_into(
452                            Yuv420Sp {
453                                y: &data[y.offset..y.offset + y.length],
454                                uv: &data[uv.offset..uv.offset + uv.length],
455                                y_stride: y.row_stride,
456                                uv_stride: uv.row_stride,
457                                vu_order: layout.format == PixelFormat::Nv21,
458                            },
459                            w,
460                            h,
461                            coeff.unwrap(),
462                            out,
463                        );
464                    }
465                    let (u, v) = if layout.format == PixelFormat::Nv12 {
466                        (p(1, 0), p(1, 1))
467                    } else {
468                        (p(1, 1), p(1, 0))
469                    };
470                    return color_convert::yuv420_to_rgb_with_coefficients_into(
471                        p(0, 0),
472                        u,
473                        v,
474                        w,
475                        h,
476                        coeff.unwrap(),
477                        out,
478                    );
479                }
480                PixelFormat::Yuyv | PixelFormat::Uyvy => {
481                    return color_convert::yuv422_to_rgb_with_coefficients_into(
482                        source,
483                        w,
484                        h,
485                        plane.row_stride,
486                        layout.format == PixelFormat::Uyvy,
487                        coeff.unwrap(),
488                        out,
489                    );
490                }
491                _ => {}
492            }
493        }
494        self.nearest.update(w, h, out_w, out_h, layout.bottom_up);
495        let yuv_row_fast = supports_yuv_row_conversion(layout);
496        if yuv && yuv_row_fast && out_w.saturating_mul(4) >= w {
497            self.rgb_row_scratch.resize(w * 6, 0);
498            return scale_yuv_nearest(
499                layout,
500                data,
501                out,
502                &self.nearest.x,
503                &self.nearest.y,
504                coeff.unwrap(),
505                &mut self.rgb_row_scratch,
506            );
507        }
508        if yuv {
509            return scale_yuv_scalar(
510                layout,
511                data,
512                out,
513                out_w,
514                &self.nearest.x,
515                &self.nearest.y,
516                coeff.unwrap(),
517            );
518        }
519        scale_nearest(layout, data, out, out_w, &self.nearest.x, &self.nearest.y)
520    }
521}
522
523#[cfg(feature = "decode-mjpeg")]
524fn jpeg_scaling(
525    header: &turbojpeg::DecompressHeader,
526    target_width: usize,
527    target_height: usize,
528) -> turbojpeg::ScalingFactor {
529    if header.is_lossless || target_width >= header.width || target_height >= header.height {
530        return turbojpeg::ScalingFactor::ONE;
531    }
532    turbojpeg::Decompressor::supported_scaling_factors()
533        .into_iter()
534        .filter(|factor| factor.num() <= factor.denom())
535        // libjpeg-turbo's fractional IDCTs above 1/2 (notably 3/4) are
536        // substantially slower than its full-size SIMD IDCT on tested ARM64
537        // and x86 builds. Decode full-size and use the cached nearest map.
538        .filter(|factor| factor.num() * 2 <= factor.denom() || factor.num() == factor.denom())
539        .filter(|factor| {
540            factor.scale(header.width) >= target_width
541                && factor.scale(header.height) >= target_height
542        })
543        .min_by_key(|factor| {
544            factor
545                .scale(header.width)
546                .saturating_mul(factor.scale(header.height))
547        })
548        .unwrap_or(turbojpeg::ScalingFactor::ONE)
549}
550
551#[cfg(feature = "decode-mjpeg")]
552fn resize_rgb_nearest(
553    source: &[u8],
554    source_width: usize,
555    destination: &mut [u8],
556    destination_width: usize,
557    x_map: &[usize],
558    y_map: &[usize],
559) {
560    for (destination_row, &source_y) in destination
561        .chunks_exact_mut(destination_width * 3)
562        .zip(y_map)
563    {
564        let source_row = &source[source_y * source_width * 3..][..source_width * 3];
565        resize_rgb_row_nearest(source_row, destination_row, x_map);
566    }
567}
568
569fn scale_nearest(
570    layout: &FrameLayout,
571    data: &[u8],
572    destination: &mut [u8],
573    destination_width: usize,
574    x_map: &[usize],
575    y_map: &[usize],
576) -> CameraResult<()> {
577    let packed = &layout.planes[0];
578    match layout.format {
579        PixelFormat::Rgb8 if packed.pixel_stride == 3 => {
580            let source_width = layout.width as usize;
581            for (destination_row, &source_y) in destination
582                .chunks_exact_mut(destination_width * 3)
583                .zip(y_map)
584            {
585                let row = packed.offset + source_y * packed.row_stride;
586                resize_rgb_row_nearest(&data[row..row + source_width * 3], destination_row, x_map);
587            }
588        }
589        PixelFormat::Rgb8 | PixelFormat::Rgba8 => {
590            for (destination_row, &source_y) in destination
591                .chunks_exact_mut(destination_width * 3)
592                .zip(y_map)
593            {
594                let row = packed.offset + source_y * packed.row_stride;
595                for (pixel, &source_x) in
596                    destination_row.as_chunks_mut::<3>().0.iter_mut().zip(x_map)
597                {
598                    let source = row + source_x * packed.pixel_stride;
599                    pixel.copy_from_slice(&data[source..source + 3]);
600                }
601            }
602        }
603        PixelFormat::Bgr8 | PixelFormat::Bgra8 => {
604            for (destination_row, &source_y) in destination
605                .chunks_exact_mut(destination_width * 3)
606                .zip(y_map)
607            {
608                let row = packed.offset + source_y * packed.row_stride;
609                for (pixel, &source_x) in
610                    destination_row.as_chunks_mut::<3>().0.iter_mut().zip(x_map)
611                {
612                    let source = row + source_x * packed.pixel_stride;
613                    pixel[0] = data[source + 2];
614                    pixel[1] = data[source + 1];
615                    pixel[2] = data[source];
616                }
617            }
618        }
619        PixelFormat::Argb8 => {
620            for (destination_row, &source_y) in destination
621                .chunks_exact_mut(destination_width * 3)
622                .zip(y_map)
623            {
624                let row = packed.offset + source_y * packed.row_stride;
625                for (pixel, &source_x) in
626                    destination_row.as_chunks_mut::<3>().0.iter_mut().zip(x_map)
627                {
628                    let source = row + source_x * packed.pixel_stride + 1;
629                    pixel.copy_from_slice(&data[source..source + 3]);
630                }
631            }
632        }
633        PixelFormat::Gray8 => {
634            for (destination_row, &source_y) in destination
635                .chunks_exact_mut(destination_width * 3)
636                .zip(y_map)
637            {
638                let row = packed.offset + source_y * packed.row_stride;
639                for (pixel, &source_x) in
640                    destination_row.as_chunks_mut::<3>().0.iter_mut().zip(x_map)
641                {
642                    pixel.fill(data[row + source_x * packed.pixel_stride]);
643                }
644            }
645        }
646        PixelFormat::Yuyv
647        | PixelFormat::Uyvy
648        | PixelFormat::Nv12
649        | PixelFormat::Nv21
650        | PixelFormat::Yuv420p => unreachable!("YUV scaling uses scale_yuv_nearest"),
651        PixelFormat::Mjpeg | PixelFormat::H264 => unreachable!(),
652    }
653    Ok(())
654}
655
656fn scale_yuv_scalar(
657    layout: &FrameLayout,
658    data: &[u8],
659    destination: &mut [u8],
660    destination_width: usize,
661    x_map: &[usize],
662    y_map: &[usize],
663    coefficients: [i32; 6],
664) -> CameraResult<()> {
665    let y_plane = &layout.planes[0];
666    for (destination_row, &source_y) in destination
667        .chunks_exact_mut(destination_width * 3)
668        .zip(y_map)
669    {
670        let y_row = y_plane.offset + source_y * y_plane.row_stride;
671        for (pixel, &source_x) in destination_row.as_chunks_mut::<3>().0.iter_mut().zip(x_map) {
672            let y_offset = y_row + source_x * y_plane.pixel_stride;
673            let (y, u, v) = match layout.format {
674                PixelFormat::Yuyv | PixelFormat::Uyvy => {
675                    let pair = y_row + (source_x & !1) * 2;
676                    if layout.format == PixelFormat::Uyvy {
677                        (data[y_offset + 1], data[pair], data[pair + 2])
678                    } else {
679                        (data[y_offset], data[pair + 1], data[pair + 3])
680                    }
681                }
682                PixelFormat::Nv12 | PixelFormat::Nv21 => {
683                    let chroma = &layout.planes[1];
684                    let uv = chroma.offset
685                        + (source_y / 2) * chroma.row_stride
686                        + (source_x / 2) * chroma.pixel_stride;
687                    if layout.format == PixelFormat::Nv12 {
688                        (data[y_offset], data[uv], data[uv + 1])
689                    } else {
690                        (data[y_offset], data[uv + 1], data[uv])
691                    }
692                }
693                PixelFormat::Yuv420p => {
694                    let u = &layout.planes[1];
695                    let v = &layout.planes[2];
696                    (
697                        data[y_offset],
698                        data[u.offset
699                            + (source_y / 2) * u.row_stride
700                            + (source_x / 2) * u.pixel_stride],
701                        data[v.offset
702                            + (source_y / 2) * v.row_stride
703                            + (source_x / 2) * v.pixel_stride],
704                    )
705                }
706                _ => unreachable!(),
707            };
708            color_convert::yuv_pixel(coefficients, y, u, v, pixel);
709        }
710    }
711    Ok(())
712}
713
714fn scale_yuv_nearest(
715    layout: &FrameLayout,
716    data: &[u8],
717    destination: &mut [u8],
718    x_map: &[usize],
719    y_map: &[usize],
720    coefficients: [i32; 6],
721    rgb_row: &mut [u8],
722) -> CameraResult<()> {
723    let width = layout.width as usize;
724    let source_row_bytes = width * 3;
725    let destination_row_bytes = x_map.len() * 3;
726    let pairable = matches!(
727        layout.format,
728        PixelFormat::Nv12 | PixelFormat::Nv21 | PixelFormat::Yuv420p
729    );
730    let mut destination_rows = destination.chunks_exact_mut(destination_row_bytes);
731    let mut index = 0;
732    while let Some(destination_row) = destination_rows.next() {
733        let source_y = y_map[index];
734        let paired =
735            pairable && source_y.is_multiple_of(2) && y_map.get(index + 1) == Some(&(source_y + 1));
736        if paired {
737            let next_destination = destination_rows
738                .next()
739                .expect("paired source rows require paired destinations");
740            convert_yuv_rows(
741                layout,
742                data,
743                source_y,
744                2,
745                coefficients,
746                &mut rgb_row[..source_row_bytes * 2],
747            )?;
748            let (first_source, second_source) = rgb_row.split_at(source_row_bytes);
749            resize_rgb_row_nearest(first_source, destination_row, x_map);
750            resize_rgb_row_nearest(&second_source[..source_row_bytes], next_destination, x_map);
751            index += 2;
752        } else {
753            convert_yuv_rows(
754                layout,
755                data,
756                source_y,
757                1,
758                coefficients,
759                &mut rgb_row[..source_row_bytes],
760            )?;
761            resize_rgb_row_nearest(&rgb_row[..source_row_bytes], destination_row, x_map);
762            index += 1;
763        }
764    }
765    Ok(())
766}
767
768fn convert_yuv_rows(
769    layout: &FrameLayout,
770    data: &[u8],
771    source_y: usize,
772    rows: usize,
773    coefficients: [i32; 6],
774    rgb: &mut [u8],
775) -> CameraResult<()> {
776    let width = layout.width as usize;
777    let y_plane = &layout.planes[0];
778    let y_start = y_plane.offset + source_y * y_plane.row_stride;
779    match layout.format {
780        PixelFormat::Yuyv | PixelFormat::Uyvy => {
781            let row_bytes = width * 2;
782            let length = (rows - 1) * y_plane.row_stride + row_bytes;
783            color_convert::yuv422_to_rgb_with_coefficients_into(
784                &data[y_start..y_start + length],
785                width,
786                rows,
787                y_plane.row_stride,
788                layout.format == PixelFormat::Uyvy,
789                coefficients,
790                rgb,
791            )
792        }
793        PixelFormat::Nv12 | PixelFormat::Nv21 => {
794            let uv_plane = &layout.planes[1];
795            let uv_start = uv_plane.offset + (source_y / 2) * uv_plane.row_stride;
796            let chroma_width = width.div_ceil(2) * 2;
797            let y_length = (rows - 1) * y_plane.row_stride + width;
798            color_convert::yuv420sp_to_rgb_with_coefficients_into(
799                Yuv420Sp {
800                    y: &data[y_start..y_start + y_length],
801                    uv: &data[uv_start..uv_start + chroma_width],
802                    y_stride: y_plane.row_stride,
803                    uv_stride: uv_plane.row_stride,
804                    vu_order: layout.format == PixelFormat::Nv21,
805                },
806                width,
807                rows,
808                coefficients,
809                rgb,
810            )
811        }
812        PixelFormat::Yuv420p => {
813            let u_plane = &layout.planes[1];
814            let v_plane = &layout.planes[2];
815            let u_start = u_plane.offset + (source_y / 2) * u_plane.row_stride;
816            let v_start = v_plane.offset + (source_y / 2) * v_plane.row_stride;
817            let chroma_width = width.div_ceil(2);
818            let y_length = (rows - 1) * y_plane.row_stride + width;
819            color_convert::yuv420_to_rgb_with_coefficients_into(
820                YuvPlane {
821                    data: &data[y_start..y_start + y_length],
822                    row_stride: y_plane.row_stride,
823                    pixel_stride: y_plane.pixel_stride,
824                },
825                YuvPlane {
826                    data: &data[u_start..u_start + chroma_width],
827                    row_stride: u_plane.row_stride,
828                    pixel_stride: u_plane.pixel_stride,
829                },
830                YuvPlane {
831                    data: &data[v_start..v_start + chroma_width],
832                    row_stride: v_plane.row_stride,
833                    pixel_stride: v_plane.pixel_stride,
834                },
835                width,
836                rows,
837                coefficients,
838                rgb,
839            )
840        }
841        _ => unreachable!(),
842    }
843}
844
845fn resize_rgb_row_nearest(source_row: &[u8], destination: &mut [u8], x_map: &[usize]) {
846    let source_width = source_row.len() / 3;
847    let destination_width = destination.len() / 3;
848    if source_width.is_multiple_of(3)
849        && destination_width * 3 == source_width * 2
850        && x_map.len() == destination_width
851    {
852        let (source_groups, source_tail) = source_row.as_chunks::<9>();
853        let (destination_groups, destination_tail) = destination.as_chunks_mut::<6>();
854        debug_assert!(source_tail.is_empty() && destination_tail.is_empty());
855        for (source, destination) in source_groups.iter().zip(destination_groups) {
856            destination.copy_from_slice(&source[..6]);
857        }
858        return;
859    }
860    if source_width.is_multiple_of(4)
861        && destination_width * 4 == source_width * 3
862        && x_map.len() == destination_width
863    {
864        let (source_groups, source_tail) = source_row.as_chunks::<12>();
865        let (destination_groups, destination_tail) = destination.as_chunks_mut::<9>();
866        debug_assert!(source_tail.is_empty() && destination_tail.is_empty());
867        for (source, destination) in source_groups.iter().zip(destination_groups) {
868            destination.copy_from_slice(&source[..9]);
869        }
870        return;
871    }
872    for (pixel, &source_x) in destination.as_chunks_mut::<3>().0.iter_mut().zip(x_map) {
873        let source = source_x * 3;
874        pixel.copy_from_slice(&source_row[source..source + 3]);
875    }
876}