styx-codec 0.1.0

Codec registry and MJPEG/raw codecs for the Styx media stack.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
use styx_core::prelude::*;

use crate::decoder::raw::yuv_to_rgb;
#[cfg(feature = "image")]
use crate::decoder::{ImageDecode, process_to_dynamic};
use crate::{Codec, CodecDescriptor, CodecError, CodecKind};
use rayon::prelude::*;
use yuvutils_rs::{
    YuvBiPlanarImage, YuvConversionMode, YuvPackedImage, YuvPlanarImage, YuvRange,
    YuvStandardMatrix,
};

#[inline(always)]
fn map_colorspace(color: ColorSpace) -> (YuvRange, YuvStandardMatrix) {
    match color {
        ColorSpace::Bt709 => (YuvRange::Limited, YuvStandardMatrix::Bt709),
        ColorSpace::Bt2020 => (YuvRange::Limited, YuvStandardMatrix::Bt2020),
        ColorSpace::Srgb => (YuvRange::Full, YuvStandardMatrix::Bt601),
        ColorSpace::Unknown => (YuvRange::Limited, YuvStandardMatrix::Bt709),
    }
}

#[derive(Clone, Copy)]
enum UvOrder {
    Uv,
    Vu,
}

/// NVXX (Y plane + interleaved chroma) → RGB24 decoder.
pub struct NvToRgbDecoder {
    descriptor: CodecDescriptor,
    pool: BufferPool,
    uv_order: UvOrder,
    subsample_h: usize,
    subsample_v: usize,
}

impl NvToRgbDecoder {
    pub fn new(
        input: FourCc,
        impl_name: &'static str,
        subsample_h: usize,
        subsample_v: usize,
        uv_is_uv: bool,
        max_width: u32,
        max_height: u32,
    ) -> Self {
        let bytes = max_width as usize * max_height as usize * 3;
        Self::with_pool(
            input,
            impl_name,
            subsample_h,
            subsample_v,
            uv_is_uv,
            BufferPool::with_limits(2, bytes, 4),
        )
    }

    pub fn with_pool(
        input: FourCc,
        impl_name: &'static str,
        subsample_h: usize,
        subsample_v: usize,
        uv_is_uv: bool,
        pool: BufferPool,
    ) -> Self {
        Self {
            descriptor: CodecDescriptor {
                kind: CodecKind::Decoder,
                input,
                output: FourCc::new(*b"RG24"),
                name: "yuv2rgb",
                impl_name,
            },
            pool,
            uv_order: if uv_is_uv { UvOrder::Uv } else { UvOrder::Vu },
            subsample_h: subsample_h.max(1),
            subsample_v: subsample_v.max(1),
        }
    }

    /// Decode into a caller-provided tightly-packed RGB24 buffer.
    ///
    /// `dst` must be at least `width * height * 3` bytes.
    pub fn decode_into(&self, input: &FrameLease, dst: &mut [u8]) -> Result<FrameMeta, CodecError> {
        let meta = input.meta();
        if meta.format.code != self.descriptor.input {
            return Err(CodecError::FormatMismatch {
                expected: self.descriptor.input,
                actual: meta.format.code,
            });
        }
        let planes = input.planes();
        if planes.len() < 2 {
            return Err(CodecError::Codec("nv frame missing planes".into()));
        }
        let y_plane = &planes[0];
        let uv_plane = &planes[1];

        let width = meta.format.resolution.width.get() as usize;
        let height = meta.format.resolution.height.get() as usize;
        let chroma_width = width.div_ceil(self.subsample_h);
        let chroma_height = height.div_ceil(self.subsample_v);
        let y_stride = y_plane.stride().max(width);
        let uv_stride = uv_plane.stride().max(chroma_width * 2);

        let y_required = y_stride
            .checked_mul(height)
            .ok_or_else(|| CodecError::Codec("nv y stride overflow".into()))?;
        let uv_required = uv_stride
            .checked_mul(chroma_height)
            .ok_or_else(|| CodecError::Codec("nv uv stride overflow".into()))?;
        if y_plane.data().len() < y_required || uv_plane.data().len() < uv_required {
            return Err(CodecError::Codec("nv plane buffer too short".into()));
        }

        let row_bytes = width
            .checked_mul(3)
            .ok_or_else(|| CodecError::Codec("nv output overflow".into()))?;
        let out_len = row_bytes
            .checked_mul(height)
            .ok_or_else(|| CodecError::Codec("nv output overflow".into()))?;
        if dst.len() < out_len {
            return Err(CodecError::Codec("nv dst buffer too short".into()));
        }
        let dst = &mut dst[..out_len];

        let (range, matrix) = map_colorspace(meta.format.color);
        let y_plane_data = &y_plane.data()[..y_required];
        let uv_plane_data = &uv_plane.data()[..uv_required];
        let bi = YuvBiPlanarImage {
            y_plane: y_plane_data,
            y_stride: y_stride as u32,
            uv_plane: uv_plane_data,
            uv_stride: uv_stride as u32,
            width: width as u32,
            height: height as u32,
        };
        let mode = YuvConversionMode::Balanced;
        let input_code = self.descriptor.input.to_u32().to_le_bytes();
        let ok = match &input_code {
            b"NV21" => Some(yuvutils_rs::yuv_nv21_to_rgb(
                &bi,
                dst,
                row_bytes as u32,
                range,
                matrix,
                mode,
            )),
            b"NV16" => Some(yuvutils_rs::yuv_nv16_to_rgb(
                &bi,
                dst,
                row_bytes as u32,
                range,
                matrix,
                mode,
            )),
            b"NV61" => Some(yuvutils_rs::yuv_nv61_to_rgb(
                &bi,
                dst,
                row_bytes as u32,
                range,
                matrix,
                mode,
            )),
            b"NV24" => Some(yuvutils_rs::yuv_nv24_to_rgb(
                &bi,
                dst,
                row_bytes as u32,
                range,
                matrix,
                mode,
            )),
            b"NV42" => Some(yuvutils_rs::yuv_nv42_to_rgb(
                &bi,
                dst,
                row_bytes as u32,
                range,
                matrix,
                mode,
            )),
            _ => None,
        };
        if ok.is_some_and(|r| r.is_ok()) {
            return Ok(FrameMeta::new(
                MediaFormat::new(
                    self.descriptor.output,
                    meta.format.resolution,
                    meta.format.color,
                ),
                meta.timestamp,
            ));
        }

        let color = meta.format.color;
        let uv_order = self.uv_order;
        let subsample_h = self.subsample_h;
        let subsample_v = self.subsample_v;
        dst.par_chunks_mut(row_bytes)
            .enumerate()
            .for_each(|(y, dst_line)| {
                let y_line = &y_plane_data[y * y_stride..][..width];
                let uv_line =
                    &uv_plane_data[(y / subsample_v) * uv_stride..][..chroma_width * 2];
                for x in 0..width {
                    let uv_base = (x / subsample_h) * 2;
                    let (u, v) = unsafe {
                        match uv_order {
                            UvOrder::Uv => (
                                *uv_line.get_unchecked(uv_base) as i32,
                                *uv_line.get_unchecked(uv_base + 1) as i32,
                            ),
                            UvOrder::Vu => (
                                *uv_line.get_unchecked(uv_base + 1) as i32,
                                *uv_line.get_unchecked(uv_base) as i32,
                            ),
                        }
                    };
                    let y_val = unsafe { *y_line.get_unchecked(x) as i32 };
                    let (r, g, b) = yuv_to_rgb(y_val, u, v, color);
                    let di = x * 3;
                    unsafe {
                        *dst_line.get_unchecked_mut(di) = r;
                        *dst_line.get_unchecked_mut(di + 1) = g;
                        *dst_line.get_unchecked_mut(di + 2) = b;
                    }
                }
            });

        Ok(FrameMeta::new(
            MediaFormat::new(
                self.descriptor.output,
                meta.format.resolution,
                meta.format.color,
            ),
            meta.timestamp,
        ))
    }
}

impl Codec for NvToRgbDecoder {
    fn descriptor(&self) -> &CodecDescriptor {
        &self.descriptor
    }

    fn process(&self, input: FrameLease) -> Result<FrameLease, CodecError> {
        let layout = plane_layout_from_dims(
            input.meta().format.resolution.width,
            input.meta().format.resolution.height,
            3,
        );
        let mut buf = self.pool.lease();
        unsafe { buf.resize_uninit(layout.len) };
        let meta = self.decode_into(&input, buf.as_mut_slice())?;
        Ok(unsafe { FrameLease::single_plane_uninit(meta, buf, layout.len, layout.stride) })
    }
}

#[cfg(feature = "image")]
impl ImageDecode for NvToRgbDecoder {
    fn decode_image(&self, frame: FrameLease) -> Result<image::DynamicImage, CodecError> {
        process_to_dynamic(self, frame)
    }
}

/// Planar YUV → RGB24 decoder (Y + separate U/V planes).
pub struct PlanarYuvToRgbDecoder {
    descriptor: CodecDescriptor,
    pool: BufferPool,
    u_first: bool,
    subsample_h: usize,
    subsample_v: usize,
}

impl PlanarYuvToRgbDecoder {
    pub fn new(
        input: FourCc,
        impl_name: &'static str,
        subsample_h: usize,
        subsample_v: usize,
        u_first: bool,
        max_width: u32,
        max_height: u32,
    ) -> Self {
        let bytes = max_width as usize * max_height as usize * 3;
        Self::with_pool(
            input,
            impl_name,
            subsample_h,
            subsample_v,
            u_first,
            BufferPool::with_limits(2, bytes, 4),
        )
    }

    pub fn with_pool(
        input: FourCc,
        impl_name: &'static str,
        subsample_h: usize,
        subsample_v: usize,
        u_first: bool,
        pool: BufferPool,
    ) -> Self {
        Self {
            descriptor: CodecDescriptor {
                kind: CodecKind::Decoder,
                input,
                output: FourCc::new(*b"RG24"),
                name: "yuv2rgb",
                impl_name,
            },
            pool,
            u_first,
            subsample_h: subsample_h.max(1),
            subsample_v: subsample_v.max(1),
        }
    }

    /// Decode into a caller-provided tightly-packed RGB24 buffer.
    ///
    /// `dst` must be at least `width * height * 3` bytes.
    pub fn decode_into(&self, input: &FrameLease, dst: &mut [u8]) -> Result<FrameMeta, CodecError> {
        let meta = input.meta();
        if meta.format.code != self.descriptor.input {
            return Err(CodecError::FormatMismatch {
                expected: self.descriptor.input,
                actual: meta.format.code,
            });
        }
        let planes = input.planes();
        if planes.len() < 3 {
            return Err(CodecError::Codec("planar yuv frame missing planes".into()));
        }

        let y_plane = &planes[0];
        let (u_plane, v_plane) = if self.u_first {
            (&planes[1], &planes[2])
        } else {
            (&planes[2], &planes[1])
        };

        let width = meta.format.resolution.width.get() as usize;
        let height = meta.format.resolution.height.get() as usize;
        let chroma_width = width.div_ceil(self.subsample_h);
        let chroma_height = height.div_ceil(self.subsample_v);
        let y_stride = y_plane.stride().max(width);
        let u_stride = u_plane.stride().max(chroma_width);
        let v_stride = v_plane.stride().max(chroma_width);

        let y_required = y_stride
            .checked_mul(height)
            .ok_or_else(|| CodecError::Codec("planar yuv y stride overflow".into()))?;
        let u_required = u_stride
            .checked_mul(chroma_height)
            .ok_or_else(|| CodecError::Codec("planar yuv u stride overflow".into()))?;
        let v_required = v_stride
            .checked_mul(chroma_height)
            .ok_or_else(|| CodecError::Codec("planar yuv v stride overflow".into()))?;
        if y_plane.data().len() < y_required
            || u_plane.data().len() < u_required
            || v_plane.data().len() < v_required
        {
            return Err(CodecError::Codec(
                "planar yuv plane buffer too short".into(),
            ));
        }

        let row_bytes = width
            .checked_mul(3)
            .ok_or_else(|| CodecError::Codec("planar yuv output overflow".into()))?;
        let out_len = row_bytes
            .checked_mul(height)
            .ok_or_else(|| CodecError::Codec("planar yuv output overflow".into()))?;
        if dst.len() < out_len {
            return Err(CodecError::Codec("planar yuv dst buffer too short".into()));
        }
        let dst = &mut dst[..out_len];

        let (range, matrix) = map_colorspace(meta.format.color);
        let y_plane_data = &y_plane.data()[..y_required];
        let u_plane_data = &u_plane.data()[..u_required];
        let v_plane_data = &v_plane.data()[..v_required];
        let planar = YuvPlanarImage {
            y_plane: y_plane_data,
            y_stride: y_stride as u32,
            u_plane: u_plane_data,
            u_stride: u_stride as u32,
            v_plane: v_plane_data,
            v_stride: v_stride as u32,
            width: width as u32,
            height: height as u32,
        };
        let ok = match (self.subsample_h, self.subsample_v) {
            (2, 2) => Some(yuvutils_rs::yuv420_to_rgb(
                &planar,
                dst,
                row_bytes as u32,
                range,
                matrix,
            )),
            (2, 1) => Some(yuvutils_rs::yuv422_to_rgb(
                &planar,
                dst,
                row_bytes as u32,
                range,
                matrix,
            )),
            (1, 1) => Some(yuvutils_rs::yuv444_to_rgb(
                &planar,
                dst,
                row_bytes as u32,
                range,
                matrix,
            )),
            _ => None,
        };
        if ok.is_some_and(|r| r.is_ok()) {
            return Ok(FrameMeta::new(
                MediaFormat::new(
                    self.descriptor.output,
                    meta.format.resolution,
                    meta.format.color,
                ),
                meta.timestamp,
            ));
        }

        let color = meta.format.color;
        let subsample_h = self.subsample_h;
        let subsample_v = self.subsample_v;
        dst.par_chunks_mut(row_bytes)
            .enumerate()
            .for_each(|(y, dst_line)| {
                let y_line = &y_plane_data[y * y_stride..][..width];
                let u_line =
                    &u_plane_data[(y / subsample_v) * u_stride..][..chroma_width];
                let v_line =
                    &v_plane_data[(y / subsample_v) * v_stride..][..chroma_width];
                for x in 0..width {
                    let chroma_idx = x / subsample_h;
                    let u = unsafe { *u_line.get_unchecked(chroma_idx) as i32 };
                    let v = unsafe { *v_line.get_unchecked(chroma_idx) as i32 };
                    let y_val = unsafe { *y_line.get_unchecked(x) as i32 };
                    let (r, g, b) = yuv_to_rgb(y_val, u, v, color);
                    let di = x * 3;
                    unsafe {
                        *dst_line.get_unchecked_mut(di) = r;
                        *dst_line.get_unchecked_mut(di + 1) = g;
                        *dst_line.get_unchecked_mut(di + 2) = b;
                    }
                }
            });

        Ok(FrameMeta::new(
            MediaFormat::new(
                self.descriptor.output,
                meta.format.resolution,
                meta.format.color,
            ),
            meta.timestamp,
        ))
    }
}

impl Codec for PlanarYuvToRgbDecoder {
    fn descriptor(&self) -> &CodecDescriptor {
        &self.descriptor
    }

    fn process(&self, input: FrameLease) -> Result<FrameLease, CodecError> {
        let layout = plane_layout_from_dims(
            input.meta().format.resolution.width,
            input.meta().format.resolution.height,
            3,
        );
        let mut buf = self.pool.lease();
        unsafe { buf.resize_uninit(layout.len) };
        let meta = self.decode_into(&input, buf.as_mut_slice())?;
        Ok(unsafe { FrameLease::single_plane_uninit(meta, buf, layout.len, layout.stride) })
    }
}

#[cfg(feature = "image")]
impl ImageDecode for PlanarYuvToRgbDecoder {
    fn decode_image(&self, frame: FrameLease) -> Result<image::DynamicImage, CodecError> {
        process_to_dynamic(self, frame)
    }
}

/// Packed YUV422 → RGB24 decoder with configurable byte order.
pub struct Packed422ToRgbDecoder {
    descriptor: CodecDescriptor,
    pool: BufferPool,
    byte_order: [usize; 4],
}

impl Packed422ToRgbDecoder {
    pub fn new(
        input: FourCc,
        impl_name: &'static str,
        byte_order: [usize; 4],
        max_width: u32,
        max_height: u32,
    ) -> Self {
        let bytes = max_width as usize * max_height as usize * 3;
        Self::with_pool(
            input,
            impl_name,
            byte_order,
            BufferPool::with_limits(2, bytes, 4),
        )
    }

    pub fn with_pool(
        input: FourCc,
        impl_name: &'static str,
        byte_order: [usize; 4],
        pool: BufferPool,
    ) -> Self {
        Self {
            descriptor: CodecDescriptor {
                kind: CodecKind::Decoder,
                input,
                output: FourCc::new(*b"RG24"),
                name: "yuv2rgb",
                impl_name,
            },
            pool,
            byte_order,
        }
    }

    /// Decode into a caller-provided tightly-packed RGB24 buffer.
    ///
    /// `dst` must be at least `width * height * 3` bytes.
    pub fn decode_into(&self, input: &FrameLease, dst: &mut [u8]) -> Result<FrameMeta, CodecError> {
        let meta = input.meta();
        if meta.format.code != self.descriptor.input {
            return Err(CodecError::FormatMismatch {
                expected: self.descriptor.input,
                actual: meta.format.code,
            });
        }
        let plane = input
            .planes()
            .into_iter()
            .next()
            .ok_or_else(|| CodecError::Codec("yuv422 frame missing plane".into()))?;

        let width = meta.format.resolution.width.get() as usize;
        let height = meta.format.resolution.height.get() as usize;
        let stride = plane.stride().max(width * 2);
        let required = stride
            .checked_mul(height)
            .ok_or_else(|| CodecError::Codec("yuv422 stride overflow".into()))?;
        if plane.data().len() < required {
            return Err(CodecError::Codec("yuv422 plane buffer too short".into()));
        }

        let row_bytes = width
            .checked_mul(3)
            .ok_or_else(|| CodecError::Codec("yuv422 output overflow".into()))?;
        let out_len = row_bytes
            .checked_mul(height)
            .ok_or_else(|| CodecError::Codec("yuv422 output overflow".into()))?;
        if dst.len() < out_len {
            return Err(CodecError::Codec("yuv422 dst buffer too short".into()));
        }
        let dst = &mut dst[..out_len];

        let src = plane.data();
        let src_required = &src[..required];
        let (range, matrix) = map_colorspace(meta.format.color);
        let packed = YuvPackedImage {
            yuy: src_required,
            yuy_stride: stride as u32,
            width: width as u32,
            height: height as u32,
        };
        let input_code = self.descriptor.input.to_u32().to_le_bytes();
        let ok = match &input_code {
            b"UYVY" => Some(yuvutils_rs::uyvy422_to_rgb(
                &packed,
                dst,
                row_bytes as u32,
                range,
                matrix,
            )),
            b"YUYV" => Some(yuvutils_rs::yuyv422_to_rgb(
                &packed,
                dst,
                row_bytes as u32,
                range,
                matrix,
            )),
            _ => None,
        };
        if ok.is_some_and(|r| r.is_ok()) {
            return Ok(FrameMeta::new(
                MediaFormat::new(
                    self.descriptor.output,
                    meta.format.resolution,
                    meta.format.color,
                ),
                meta.timestamp,
            ));
        }

        let color = meta.format.color;
        let byte_order = self.byte_order;
        dst.par_chunks_mut(row_bytes)
            .enumerate()
            .for_each(|(y, dst_line)| {
                let src_line = &src_required[y * stride..][..width * 2];
                let pair_count = width / 2;
                for pair in 0..pair_count {
                    let si = pair * 4;
                    let di = pair * 6;
                    let y0 = unsafe { *src_line.get_unchecked(si + byte_order[0]) as i32 };
                    let u = unsafe { *src_line.get_unchecked(si + byte_order[1]) as i32 };
                    let y1 = unsafe { *src_line.get_unchecked(si + byte_order[2]) as i32 };
                    let v = unsafe { *src_line.get_unchecked(si + byte_order[3]) as i32 };
                    let (r0, g0, b0) = yuv_to_rgb(y0, u, v, color);
                    let (r1, g1, b1) = yuv_to_rgb(y1, u, v, color);
                    unsafe {
                        *dst_line.get_unchecked_mut(di) = r0;
                        *dst_line.get_unchecked_mut(di + 1) = g0;
                        *dst_line.get_unchecked_mut(di + 2) = b0;
                        *dst_line.get_unchecked_mut(di + 3) = r1;
                        *dst_line.get_unchecked_mut(di + 4) = g1;
                        *dst_line.get_unchecked_mut(di + 5) = b1;
                    }
                }
                if width % 2 == 1 && width >= 1 {
                    let last_x = width - 1;
                    let si = (last_x / 2) * 4;
                    let di = last_x * 3;
                    let y_val = unsafe { *src_line.get_unchecked(si + byte_order[0]) as i32 };
                    let u = unsafe { *src_line.get_unchecked(si + byte_order[1]) as i32 };
                    let v = unsafe { *src_line.get_unchecked(si + byte_order[3]) as i32 };
                    let (r, g, b) = yuv_to_rgb(y_val, u, v, color);
                    unsafe {
                        *dst_line.get_unchecked_mut(di) = r;
                        *dst_line.get_unchecked_mut(di + 1) = g;
                        *dst_line.get_unchecked_mut(di + 2) = b;
                    }
                }
            });

        Ok(FrameMeta::new(
            MediaFormat::new(
                self.descriptor.output,
                meta.format.resolution,
                meta.format.color,
            ),
            meta.timestamp,
        ))
    }
}

impl Codec for Packed422ToRgbDecoder {
    fn descriptor(&self) -> &CodecDescriptor {
        &self.descriptor
    }

    fn process(&self, input: FrameLease) -> Result<FrameLease, CodecError> {
        let layout = plane_layout_from_dims(
            input.meta().format.resolution.width,
            input.meta().format.resolution.height,
            3,
        );
        let mut buf = self.pool.lease();
        unsafe { buf.resize_uninit(layout.len) };
        let meta = self.decode_into(&input, buf.as_mut_slice())?;
        Ok(unsafe { FrameLease::single_plane_uninit(meta, buf, layout.len, layout.stride) })
    }
}

#[cfg(feature = "image")]
impl ImageDecode for Packed422ToRgbDecoder {
    fn decode_image(&self, frame: FrameLease) -> Result<image::DynamicImage, CodecError> {
        process_to_dynamic(self, frame)
    }
}