media-core 0.9.1

Define media types and provide basic media utilities
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
use std::{fmt::Debug, num::NonZeroU32, sync::LazyLock};

use bytemuck::{self, Pod};
use strum::EnumCount;
use yuv::{
    self, BufferStoreMut, Rgb30ByteOrder::Network, YuvBiPlanarImage, YuvBiPlanarImageMut, YuvConversionMode, YuvConversionMode::Fast, YuvPackedImage,
    YuvPackedImageMut, YuvPlanarImage, YuvPlanarImageMut, YuvRange, YuvStandardMatrix,
};

use super::{
    frame::VideoFrame,
    video::{ColorMatrix, ColorRange, PixelFormat, VideoFrameDescriptor},
};
use crate::{
    frame::{DataMappable, Frame, FrameData, MappedPlanes},
    invalid_error, unsupported_error, FrameDescriptor, Result,
};

fn into_yuv_planar_image<'a, T>(src: &'a MappedPlanes, width: NonZeroU32, height: NonZeroU32) -> Result<YuvPlanarImage<'a, T>>
where
    T: Debug + Pod,
{
    if src.planes.len() != 3 {
        return Err(invalid_error!("invalid plane count"));
    }

    let planes = &src.planes;
    let size = size_of::<T>();

    Ok(YuvPlanarImage::<T> {
        y_plane: bytemuck::cast_slice(planes[0].data().unwrap()),
        y_stride: (planes[0].stride().unwrap() / size) as u32,
        u_plane: bytemuck::cast_slice(planes[1].data().unwrap()),
        u_stride: (planes[1].stride().unwrap() / size) as u32,
        v_plane: bytemuck::cast_slice(planes[2].data().unwrap()),
        v_stride: (planes[2].stride().unwrap() / size) as u32,
        width: width.get(),
        height: height.get(),
    })
}

fn into_yuv_planar_image_mut<'a, T>(dst: &'a mut MappedPlanes, width: NonZeroU32, height: NonZeroU32) -> Result<YuvPlanarImageMut<'a, T>>
where
    T: Debug + Pod,
{
    if dst.planes.len() != 3 {
        return Err(invalid_error!("invalid plane count"));
    }

    let planes = dst.planes.as_mut_slice();
    let size = size_of::<T>();

    let y_stride = (planes[0].stride().unwrap() / size) as u32;
    let u_stride = (planes[1].stride().unwrap() / size) as u32;
    let v_stride = (planes[2].stride().unwrap() / size) as u32;

    let (y_plane, rest) = planes.split_at_mut(1);
    let (u_plane, v_plane) = rest.split_at_mut(1);

    Ok(YuvPlanarImageMut::<T> {
        y_plane: BufferStoreMut::Borrowed(bytemuck::cast_slice_mut(y_plane[0].data_mut().unwrap())),
        y_stride,
        u_plane: BufferStoreMut::Borrowed(bytemuck::cast_slice_mut(u_plane[0].data_mut().unwrap())),
        u_stride,
        v_plane: BufferStoreMut::Borrowed(bytemuck::cast_slice_mut(v_plane[0].data_mut().unwrap())),
        v_stride,
        width: width.get(),
        height: height.get(),
    })
}

fn into_yuv_bi_planar_image<'a, T>(src: &'a MappedPlanes, width: NonZeroU32, height: NonZeroU32) -> Result<YuvBiPlanarImage<'a, T>>
where
    T: Debug + Pod,
{
    if src.planes.len() != 2 {
        return Err(invalid_error!("invalid plane count"));
    }

    let planes = &src.planes;
    let size = size_of::<T>();

    Ok(YuvBiPlanarImage::<T> {
        y_plane: bytemuck::cast_slice(planes[0].data().unwrap()),
        y_stride: (planes[0].stride().unwrap() / size) as u32,
        uv_plane: bytemuck::cast_slice(planes[1].data().unwrap()),
        uv_stride: (planes[1].stride().unwrap() / size) as u32,
        width: width.get(),
        height: height.get(),
    })
}

fn into_yuv_bi_planar_image_mut<'a, T>(dst: &'a mut MappedPlanes, width: NonZeroU32, height: NonZeroU32) -> Result<YuvBiPlanarImageMut<'a, T>>
where
    T: Debug + Pod,
{
    if dst.planes.len() != 2 {
        return Err(invalid_error!("invalid plane count"));
    }

    let planes = dst.planes.as_mut_slice();
    let size = size_of::<T>();

    let y_stride = (planes[0].stride().unwrap() / size) as u32;
    let uv_stride = (planes[1].stride().unwrap() / size) as u32;

    let (y_plane, uv_plane) = planes.split_at_mut(1);

    Ok(YuvBiPlanarImageMut::<T> {
        y_plane: BufferStoreMut::Borrowed(bytemuck::cast_slice_mut(y_plane[0].data_mut().unwrap())),
        y_stride,
        uv_plane: BufferStoreMut::Borrowed(bytemuck::cast_slice_mut(uv_plane[0].data_mut().unwrap())),
        uv_stride,
        width: width.get(),
        height: height.get(),
    })
}

fn into_yuv_packed_image<'a, T>(src: &'a MappedPlanes, width: NonZeroU32, height: NonZeroU32) -> Result<YuvPackedImage<'a, T>>
where
    T: Debug + Pod,
{
    if src.planes.len() != 1 {
        return Err(invalid_error!("invalid plane count"));
    }

    let planes = &src.planes;
    let size = size_of::<T>();

    Ok(YuvPackedImage::<T> {
        yuy: bytemuck::cast_slice(planes[0].data().unwrap()),
        yuy_stride: (planes[0].stride().unwrap() / size) as u32,
        width: width.get(),
        height: height.get(),
    })
}

fn into_yuv_packed_image_mut<'a, T>(dst: &'a mut MappedPlanes, width: NonZeroU32, height: NonZeroU32) -> Result<YuvPackedImageMut<'a, T>>
where
    T: Debug + Pod,
{
    if dst.planes.len() != 1 {
        return Err(invalid_error!("invalid plane count"));
    }

    let planes = dst.planes.as_mut();
    let size = size_of::<T>();

    let yuy_stride = (planes[0].stride().unwrap() / size) as u32;

    Ok(YuvPackedImageMut::<T> {
        yuy: BufferStoreMut::Borrowed(bytemuck::cast_slice_mut(planes[0].data_mut().unwrap())),
        yuy_stride,
        width: width.get(),
        height: height.get(),
    })
}

impl From<ColorRange> for YuvRange {
    fn from(range: ColorRange) -> Self {
        match range {
            ColorRange::Video => YuvRange::Limited,
            ColorRange::Full => YuvRange::Full,
            _ => YuvRange::Limited,
        }
    }
}

impl From<ColorMatrix> for YuvStandardMatrix {
    fn from(color_matrix: ColorMatrix) -> Self {
        match color_matrix {
            ColorMatrix::BT709 => YuvStandardMatrix::Bt709,
            ColorMatrix::FCC => YuvStandardMatrix::Fcc,
            ColorMatrix::BT470BG | ColorMatrix::SMPTE170M => YuvStandardMatrix::Bt601,
            ColorMatrix::SMPTE240M => YuvStandardMatrix::Custom(0.212, 0.087),
            ColorMatrix::YCgCo => YuvStandardMatrix::Custom(0.25, 0.25),
            ColorMatrix::BT2020NCL | ColorMatrix::BT2020CL => YuvStandardMatrix::Bt2020,
            _ => YuvStandardMatrix::Bt601,
        }
    }
}

macro_rules! impl_rgb_to_rgb {
    ($func_name:ident, $convert_func:ident) => {
        fn $func_name(
            src: &MappedPlanes,
            dst: &mut MappedPlanes,
            _color_range: ColorRange,
            _color_matrix: ColorMatrix,
            width: NonZeroU32,
            height: NonZeroU32,
        ) -> Result<()> {
            let dst_stride = dst.plane_stride(0).unwrap() as u32;

            yuv::$convert_func(
                src.plane_data(0).unwrap(),
                src.plane_stride(0).unwrap() as u32,
                dst.plane_data_mut(0).unwrap(),
                dst_stride,
                width.get(),
                height.get(),
            )
            .map_err(|e| invalid_error!(e.to_string()))?;

            Ok(())
        }
    };
}

macro_rules! impl_rgb_to_yuv {
    ($func_name:ident, $convert_func:ident, $into_image_func:ident) => {
        fn $func_name(
            src: &MappedPlanes,
            dst: &mut MappedPlanes,
            color_range: ColorRange,
            color_matrix: ColorMatrix,
            width: NonZeroU32,
            height: NonZeroU32,
        ) -> Result<()> {
            let mut yuv_image = $into_image_func(dst, width, height)?;

            yuv::$convert_func(
                &mut yuv_image,
                src.plane_data(0).unwrap(),
                src.plane_stride(0).unwrap() as u32,
                color_range.into(),
                color_matrix.into(),
                YuvConversionMode::Fast,
            )
            .map_err(|e| invalid_error!(e.to_string()))?;

            Ok(())
        }
    };
}

macro_rules! impl_yuv_to_rgb {
    ($func_name:ident, $convert_func:ident, $into_image_func:ident) => {
        fn $func_name(
            src: &MappedPlanes,
            dst: &mut MappedPlanes,
            color_range: ColorRange,
            color_matrix: ColorMatrix,
            width: NonZeroU32,
            height: NonZeroU32,
        ) -> Result<()> {
            let yuv_image = $into_image_func(src, width, height)?;
            let dst_stride = dst.plane_stride(0).unwrap() as u32;

            yuv::$convert_func(&yuv_image, dst.plane_data_mut(0).unwrap(), dst_stride, color_range.into(), color_matrix.into())
                .map_err(|e| invalid_error!(e.to_string()))?;

            Ok(())
        }
    };
}

macro_rules! impl_yuv_to_rgb_with_conversion_mode {
    ($func_name:ident, $convert_func:ident, $into_image_func:ident, $conversion_mode:ident) => {
        fn $func_name(
            src: &MappedPlanes,
            dst: &mut MappedPlanes,
            color_range: ColorRange,
            color_matrix: ColorMatrix,
            width: NonZeroU32,
            height: NonZeroU32,
        ) -> Result<()> {
            let yuv_image = $into_image_func(src, width, height)?;
            let dst_stride = dst.plane_stride(0).unwrap() as u32;

            yuv::$convert_func(&yuv_image, dst.plane_data_mut(0).unwrap(), dst_stride, color_range.into(), color_matrix.into(), $conversion_mode)
                .map_err(|e| invalid_error!(e.to_string()))?;

            Ok(())
        }
    };
}

macro_rules! impl_yuv_to_rgb_with_byte_order {
    ($func_name:ident, $convert_func:ident, $into_image_func:ident, $byte_order:ident) => {
        fn $func_name(
            src: &MappedPlanes,
            dst: &mut MappedPlanes,
            color_range: ColorRange,
            color_matrix: ColorMatrix,
            width: NonZeroU32,
            height: NonZeroU32,
        ) -> Result<()> {
            let yuv_image = $into_image_func(src, width, height)?;
            let dst_stride = dst.plane_stride(0).unwrap() as u32;

            yuv::$convert_func(&yuv_image, dst.plane_data_mut(0).unwrap(), dst_stride, $byte_order, color_range.into(), color_matrix.into())
                .map_err(|e| invalid_error!(e.to_string()))?;

            Ok(())
        }
    };
}

macro_rules! impl_yuv_to_yuv {
    ($func_name:ident, $convert_func:ident, $into_src_image_func:ident, $into_dst_image_func:ident) => {
        fn $func_name(
            src: &MappedPlanes,
            dst: &mut MappedPlanes,
            _color_range: ColorRange,
            _color_matrix: ColorMatrix,
            width: NonZeroU32,
            height: NonZeroU32,
        ) -> Result<()> {
            let src_image = $into_src_image_func(src, width, height)?;
            let mut dst_image = $into_dst_image_func(dst, width, height)?;

            yuv::$convert_func(&mut dst_image, &src_image).map_err(|e| invalid_error!(e.to_string()))?;

            Ok(())
        }
    };
}

impl_rgb_to_rgb!(bgra32_to_rgba32, bgra_to_rgba);

impl_rgb_to_yuv!(bgra32_to_i420, bgra_to_yuv420, into_yuv_planar_image_mut);
impl_rgb_to_yuv!(bgra32_to_i422, bgra_to_yuv422, into_yuv_planar_image_mut);
impl_rgb_to_yuv!(bgra32_to_i444, bgra_to_yuv444, into_yuv_planar_image_mut);
impl_rgb_to_yuv!(bgra32_to_nv12, bgra_to_yuv_nv12, into_yuv_bi_planar_image_mut);
impl_rgb_to_yuv!(bgra32_to_nv16, bgra_to_yuv_nv16, into_yuv_bi_planar_image_mut);
impl_rgb_to_yuv!(bgra32_to_nv24, bgra_to_yuv_nv24, into_yuv_bi_planar_image_mut);
impl_rgb_to_yuv!(bgra32_to_nv21, bgra_to_yuv_nv21, into_yuv_bi_planar_image_mut);
impl_rgb_to_yuv!(bgra32_to_nv61, bgra_to_yuv_nv61, into_yuv_bi_planar_image_mut);
impl_rgb_to_yuv!(bgra32_to_nv42, bgra_to_yuv_nv42, into_yuv_bi_planar_image_mut);

impl_rgb_to_rgb!(rgba32_to_bgra32, rgba_to_bgra);

impl_rgb_to_yuv!(rgba32_to_i420, rgba_to_yuv420, into_yuv_planar_image_mut);
impl_rgb_to_yuv!(rgba32_to_i422, rgba_to_yuv422, into_yuv_planar_image_mut);
impl_rgb_to_yuv!(rgba32_to_i444, rgba_to_yuv444, into_yuv_planar_image_mut);
impl_rgb_to_yuv!(rgba32_to_nv12, rgba_to_yuv_nv12, into_yuv_bi_planar_image_mut);
impl_rgb_to_yuv!(rgba32_to_nv16, rgba_to_yuv_nv16, into_yuv_bi_planar_image_mut);
impl_rgb_to_yuv!(rgba32_to_nv24, rgba_to_yuv_nv24, into_yuv_bi_planar_image_mut);
impl_rgb_to_yuv!(rgba32_to_nv21, rgba_to_yuv_nv21, into_yuv_bi_planar_image_mut);
impl_rgb_to_yuv!(rgba32_to_nv61, rgba_to_yuv_nv61, into_yuv_bi_planar_image_mut);
impl_rgb_to_yuv!(rgba32_to_nv42, rgba_to_yuv_nv42, into_yuv_bi_planar_image_mut);

impl_yuv_to_rgb!(i420_to_bgra32, yuv420_to_bgra, into_yuv_planar_image);
impl_yuv_to_rgb!(i420_to_rgba32, yuv420_to_rgba, into_yuv_planar_image);
impl_yuv_to_rgb!(i420_to_bgr24, yuv420_to_bgr, into_yuv_planar_image);
impl_yuv_to_rgb!(i420_to_rgb24, yuv420_to_rgb, into_yuv_planar_image);

impl_yuv_to_yuv!(i420_to_yuyv, yuv420_to_yuyv422, into_yuv_planar_image, into_yuv_packed_image_mut);
impl_yuv_to_yuv!(i420_to_yvyu, yuv420_to_yvyu422, into_yuv_planar_image, into_yuv_packed_image_mut);
impl_yuv_to_yuv!(i420_to_uyvy, yuv420_to_uyvy422, into_yuv_planar_image, into_yuv_packed_image_mut);
impl_yuv_to_yuv!(i420_to_vyuy, yuv420_to_vyuy422, into_yuv_planar_image, into_yuv_packed_image_mut);

impl_yuv_to_rgb!(i422_to_bgra32, yuv422_to_bgra, into_yuv_planar_image);
impl_yuv_to_rgb!(i422_to_rgba32, yuv422_to_rgba, into_yuv_planar_image);
impl_yuv_to_rgb!(i422_to_bgr24, yuv422_to_bgr, into_yuv_planar_image);
impl_yuv_to_rgb!(i422_to_rgb24, yuv422_to_rgb, into_yuv_planar_image);

impl_yuv_to_yuv!(i422_to_yuyv, yuv422_to_yuyv422, into_yuv_planar_image, into_yuv_packed_image_mut);
impl_yuv_to_yuv!(i422_to_yvyu, yuv422_to_yvyu422, into_yuv_planar_image, into_yuv_packed_image_mut);
impl_yuv_to_yuv!(i422_to_uyvy, yuv422_to_uyvy422, into_yuv_planar_image, into_yuv_packed_image_mut);
impl_yuv_to_yuv!(i422_to_vyuy, yuv422_to_vyuy422, into_yuv_planar_image, into_yuv_packed_image_mut);

impl_yuv_to_rgb!(i444_to_bgra32, yuv444_to_bgra, into_yuv_planar_image);
impl_yuv_to_rgb!(i444_to_rgba32, yuv444_to_rgba, into_yuv_planar_image);
impl_yuv_to_rgb!(i444_to_bgr24, yuv444_to_bgr, into_yuv_planar_image);
impl_yuv_to_rgb!(i444_to_rgb24, yuv444_to_rgb, into_yuv_planar_image);

impl_yuv_to_yuv!(i444_to_yuyv, yuv444_to_yuyv422, into_yuv_planar_image, into_yuv_packed_image_mut);
impl_yuv_to_yuv!(i444_to_yvyu, yuv444_to_yvyu422, into_yuv_planar_image, into_yuv_packed_image_mut);
impl_yuv_to_yuv!(i444_to_uyvy, yuv444_to_uyvy422, into_yuv_planar_image, into_yuv_packed_image_mut);
impl_yuv_to_yuv!(i444_to_vyuy, yuv444_to_vyuy422, into_yuv_planar_image, into_yuv_packed_image_mut);

impl_yuv_to_rgb_with_conversion_mode!(nv12_to_bgra32, yuv_nv12_to_bgra, into_yuv_bi_planar_image, Fast);
impl_yuv_to_rgb_with_conversion_mode!(nv12_to_rgba32, yuv_nv12_to_rgba, into_yuv_bi_planar_image, Fast);
impl_yuv_to_rgb_with_conversion_mode!(nv12_to_bgr24, yuv_nv12_to_bgr, into_yuv_bi_planar_image, Fast);
impl_yuv_to_rgb_with_conversion_mode!(nv12_to_rgb24, yuv_nv12_to_rgb, into_yuv_bi_planar_image, Fast);

impl_yuv_to_rgb_with_conversion_mode!(nv16_to_bgra32, yuv_nv16_to_bgra, into_yuv_bi_planar_image, Fast);
impl_yuv_to_rgb_with_conversion_mode!(nv16_to_rgba32, yuv_nv16_to_rgba, into_yuv_bi_planar_image, Fast);
impl_yuv_to_rgb_with_conversion_mode!(nv16_to_bgr24, yuv_nv16_to_bgr, into_yuv_bi_planar_image, Fast);
impl_yuv_to_rgb_with_conversion_mode!(nv16_to_rgb24, yuv_nv16_to_rgb, into_yuv_bi_planar_image, Fast);

impl_yuv_to_rgb_with_conversion_mode!(nv24_to_bgra32, yuv_nv24_to_bgra, into_yuv_bi_planar_image, Fast);
impl_yuv_to_rgb_with_conversion_mode!(nv24_to_rgba32, yuv_nv24_to_rgba, into_yuv_bi_planar_image, Fast);
impl_yuv_to_rgb_with_conversion_mode!(nv24_to_bgr24, yuv_nv24_to_bgr, into_yuv_bi_planar_image, Fast);
impl_yuv_to_rgb_with_conversion_mode!(nv24_to_rgb24, yuv_nv24_to_rgb, into_yuv_bi_planar_image, Fast);

impl_yuv_to_rgb_with_conversion_mode!(nv21_to_bgra32, yuv_nv21_to_bgra, into_yuv_bi_planar_image, Fast);
impl_yuv_to_rgb_with_conversion_mode!(nv21_to_rgba32, yuv_nv21_to_rgba, into_yuv_bi_planar_image, Fast);
impl_yuv_to_rgb_with_conversion_mode!(nv21_to_bgr24, yuv_nv21_to_bgr, into_yuv_bi_planar_image, Fast);
impl_yuv_to_rgb_with_conversion_mode!(nv21_to_rgb24, yuv_nv21_to_rgb, into_yuv_bi_planar_image, Fast);

impl_yuv_to_rgb_with_conversion_mode!(nv61_to_bgra32, yuv_nv61_to_bgra, into_yuv_bi_planar_image, Fast);
impl_yuv_to_rgb_with_conversion_mode!(nv61_to_rgba32, yuv_nv61_to_rgba, into_yuv_bi_planar_image, Fast);
impl_yuv_to_rgb_with_conversion_mode!(nv61_to_bgr24, yuv_nv61_to_bgr, into_yuv_bi_planar_image, Fast);
impl_yuv_to_rgb_with_conversion_mode!(nv61_to_rgb24, yuv_nv61_to_rgb, into_yuv_bi_planar_image, Fast);

impl_yuv_to_rgb_with_conversion_mode!(nv42_to_bgra32, yuv_nv42_to_bgra, into_yuv_bi_planar_image, Fast);
impl_yuv_to_rgb_with_conversion_mode!(nv42_to_rgba32, yuv_nv42_to_rgba, into_yuv_bi_planar_image, Fast);
impl_yuv_to_rgb_with_conversion_mode!(nv42_to_bgr24, yuv_nv42_to_bgr, into_yuv_bi_planar_image, Fast);
impl_yuv_to_rgb_with_conversion_mode!(nv42_to_rgb24, yuv_nv42_to_rgb, into_yuv_bi_planar_image, Fast);

impl_yuv_to_rgb!(yuyv_to_bgra32, yuyv422_to_bgra, into_yuv_packed_image);
impl_yuv_to_rgb!(yuyv_to_rgba32, yuyv422_to_rgba, into_yuv_packed_image);
impl_yuv_to_rgb!(yuyv_to_bgr24, yuyv422_to_bgr, into_yuv_packed_image);
impl_yuv_to_rgb!(yuyv_to_rgb24, yuyv422_to_rgb, into_yuv_packed_image);

impl_yuv_to_yuv!(yuyv_to_i420, yuyv422_to_yuv420, into_yuv_packed_image, into_yuv_planar_image_mut);
impl_yuv_to_yuv!(yuyv_to_i422, yuyv422_to_yuv422, into_yuv_packed_image, into_yuv_planar_image_mut);
impl_yuv_to_yuv!(yuyv_to_i444, yuyv422_to_yuv444, into_yuv_packed_image, into_yuv_planar_image_mut);

impl_yuv_to_rgb!(yvyu_to_bgra32, yvyu422_to_bgra, into_yuv_packed_image);
impl_yuv_to_rgb!(yvyu_to_rgba32, yvyu422_to_rgba, into_yuv_packed_image);
impl_yuv_to_rgb!(yvyu_to_bgr24, yvyu422_to_bgr, into_yuv_packed_image);
impl_yuv_to_rgb!(yvyu_to_rgb24, yvyu422_to_rgb, into_yuv_packed_image);

impl_yuv_to_yuv!(yvyu_to_i420, yvyu422_to_yuv420, into_yuv_packed_image, into_yuv_planar_image_mut);
impl_yuv_to_yuv!(yvyu_to_i422, yvyu422_to_yuv422, into_yuv_packed_image, into_yuv_planar_image_mut);
impl_yuv_to_yuv!(yvyu_to_i444, yvyu422_to_yuv444, into_yuv_packed_image, into_yuv_planar_image_mut);

impl_yuv_to_rgb!(uyvy_to_bgra32, uyvy422_to_bgra, into_yuv_packed_image);
impl_yuv_to_rgb!(uyvy_to_rgba32, uyvy422_to_rgba, into_yuv_packed_image);
impl_yuv_to_rgb!(uyvy_to_bgr24, uyvy422_to_bgr, into_yuv_packed_image);
impl_yuv_to_rgb!(uyvy_to_rgb24, uyvy422_to_rgb, into_yuv_packed_image);

impl_yuv_to_yuv!(uyvy_to_i420, uyvy422_to_yuv420, into_yuv_packed_image, into_yuv_planar_image_mut);
impl_yuv_to_yuv!(uyvy_to_i422, uyvy422_to_yuv422, into_yuv_packed_image, into_yuv_planar_image_mut);
impl_yuv_to_yuv!(uyvy_to_i444, uyvy422_to_yuv444, into_yuv_packed_image, into_yuv_planar_image_mut);

impl_yuv_to_rgb!(vyuy_to_bgra32, vyuy422_to_bgra, into_yuv_packed_image);
impl_yuv_to_rgb!(vyuy_to_rgba32, vyuy422_to_rgba, into_yuv_packed_image);
impl_yuv_to_rgb!(vyuy_to_bgr24, vyuy422_to_bgr, into_yuv_packed_image);
impl_yuv_to_rgb!(vyuy_to_rgb24, vyuy422_to_rgb, into_yuv_packed_image);

impl_yuv_to_yuv!(vyuy_to_i420, vyuy422_to_yuv420, into_yuv_packed_image, into_yuv_planar_image_mut);
impl_yuv_to_yuv!(vyuy_to_i422, vyuy422_to_yuv422, into_yuv_packed_image, into_yuv_planar_image_mut);
impl_yuv_to_yuv!(vyuy_to_i444, vyuy422_to_yuv444, into_yuv_packed_image, into_yuv_planar_image_mut);

impl_yuv_to_rgb_with_byte_order!(i010_to_rgb30, i010_to_ra30, into_yuv_planar_image, Network);
impl_yuv_to_rgb_with_byte_order!(i210_to_rgb30, i210_to_ra30, into_yuv_planar_image, Network);
impl_yuv_to_rgb_with_byte_order!(i410_to_rgb30, i410_to_ra30, into_yuv_planar_image, Network);

impl_yuv_to_rgb_with_byte_order!(p010_to_rgb30, p010_to_ra30, into_yuv_bi_planar_image, Network);
impl_yuv_to_rgb_with_byte_order!(p210_to_rgb30, p210_to_ra30, into_yuv_bi_planar_image, Network);

type VideoFormatConvertFunc = fn(&MappedPlanes, &mut MappedPlanes, ColorRange, ColorMatrix, NonZeroU32, NonZeroU32) -> Result<()>;

const PIXEL_FORMAT_MAX: usize = PixelFormat::COUNT;

static VIDEO_FORMAT_CONVERT_FUNCS: LazyLock<[[Option<VideoFormatConvertFunc>; PIXEL_FORMAT_MAX]; PIXEL_FORMAT_MAX]> = LazyLock::new(|| {
    let mut funcs: [[Option<VideoFormatConvertFunc>; PIXEL_FORMAT_MAX]; PIXEL_FORMAT_MAX] = [[None; PIXEL_FORMAT_MAX]; PIXEL_FORMAT_MAX];
    funcs[PixelFormat::BGRA32 as usize][PixelFormat::RGBA32 as usize] = Some(bgra32_to_rgba32);
    funcs[PixelFormat::BGRA32 as usize][PixelFormat::I420 as usize] = Some(bgra32_to_i420);
    funcs[PixelFormat::BGRA32 as usize][PixelFormat::I422 as usize] = Some(bgra32_to_i422);
    funcs[PixelFormat::BGRA32 as usize][PixelFormat::I444 as usize] = Some(bgra32_to_i444);
    funcs[PixelFormat::BGRA32 as usize][PixelFormat::NV12 as usize] = Some(bgra32_to_nv12);
    funcs[PixelFormat::BGRA32 as usize][PixelFormat::NV16 as usize] = Some(bgra32_to_nv16);
    funcs[PixelFormat::BGRA32 as usize][PixelFormat::NV24 as usize] = Some(bgra32_to_nv24);
    funcs[PixelFormat::BGRA32 as usize][PixelFormat::NV21 as usize] = Some(bgra32_to_nv21);
    funcs[PixelFormat::BGRA32 as usize][PixelFormat::NV61 as usize] = Some(bgra32_to_nv61);
    funcs[PixelFormat::BGRA32 as usize][PixelFormat::NV42 as usize] = Some(bgra32_to_nv42);
    funcs[PixelFormat::RGBA32 as usize][PixelFormat::BGRA32 as usize] = Some(rgba32_to_bgra32);
    funcs[PixelFormat::RGBA32 as usize][PixelFormat::I420 as usize] = Some(rgba32_to_i420);
    funcs[PixelFormat::RGBA32 as usize][PixelFormat::I422 as usize] = Some(rgba32_to_i422);
    funcs[PixelFormat::RGBA32 as usize][PixelFormat::I444 as usize] = Some(rgba32_to_i444);
    funcs[PixelFormat::RGBA32 as usize][PixelFormat::NV12 as usize] = Some(rgba32_to_nv12);
    funcs[PixelFormat::RGBA32 as usize][PixelFormat::NV16 as usize] = Some(rgba32_to_nv16);
    funcs[PixelFormat::RGBA32 as usize][PixelFormat::NV24 as usize] = Some(rgba32_to_nv24);
    funcs[PixelFormat::RGBA32 as usize][PixelFormat::NV21 as usize] = Some(rgba32_to_nv21);
    funcs[PixelFormat::RGBA32 as usize][PixelFormat::NV61 as usize] = Some(rgba32_to_nv61);
    funcs[PixelFormat::RGBA32 as usize][PixelFormat::NV42 as usize] = Some(rgba32_to_nv42);
    funcs[PixelFormat::I420 as usize][PixelFormat::BGRA32 as usize] = Some(i420_to_bgra32);
    funcs[PixelFormat::I420 as usize][PixelFormat::RGBA32 as usize] = Some(i420_to_rgba32);
    funcs[PixelFormat::I420 as usize][PixelFormat::BGR24 as usize] = Some(i420_to_bgr24);
    funcs[PixelFormat::I420 as usize][PixelFormat::RGB24 as usize] = Some(i420_to_rgb24);
    funcs[PixelFormat::I420 as usize][PixelFormat::YUYV as usize] = Some(i420_to_yuyv);
    funcs[PixelFormat::I420 as usize][PixelFormat::YVYU as usize] = Some(i420_to_yvyu);
    funcs[PixelFormat::I420 as usize][PixelFormat::UYVY as usize] = Some(i420_to_uyvy);
    funcs[PixelFormat::I420 as usize][PixelFormat::VYUY as usize] = Some(i420_to_vyuy);
    funcs[PixelFormat::I422 as usize][PixelFormat::BGRA32 as usize] = Some(i422_to_bgra32);
    funcs[PixelFormat::I422 as usize][PixelFormat::RGBA32 as usize] = Some(i422_to_rgba32);
    funcs[PixelFormat::I422 as usize][PixelFormat::BGR24 as usize] = Some(i422_to_bgr24);
    funcs[PixelFormat::I422 as usize][PixelFormat::RGB24 as usize] = Some(i422_to_rgb24);
    funcs[PixelFormat::I422 as usize][PixelFormat::YUYV as usize] = Some(i422_to_yuyv);
    funcs[PixelFormat::I422 as usize][PixelFormat::YVYU as usize] = Some(i422_to_yvyu);
    funcs[PixelFormat::I422 as usize][PixelFormat::UYVY as usize] = Some(i422_to_uyvy);
    funcs[PixelFormat::I422 as usize][PixelFormat::VYUY as usize] = Some(i422_to_vyuy);
    funcs[PixelFormat::I444 as usize][PixelFormat::BGRA32 as usize] = Some(i444_to_bgra32);
    funcs[PixelFormat::I444 as usize][PixelFormat::RGBA32 as usize] = Some(i444_to_rgba32);
    funcs[PixelFormat::I444 as usize][PixelFormat::BGR24 as usize] = Some(i444_to_bgr24);
    funcs[PixelFormat::I444 as usize][PixelFormat::RGB24 as usize] = Some(i444_to_rgb24);
    funcs[PixelFormat::I444 as usize][PixelFormat::YUYV as usize] = Some(i444_to_yuyv);
    funcs[PixelFormat::I444 as usize][PixelFormat::YVYU as usize] = Some(i444_to_yvyu);
    funcs[PixelFormat::I444 as usize][PixelFormat::UYVY as usize] = Some(i444_to_uyvy);
    funcs[PixelFormat::I444 as usize][PixelFormat::VYUY as usize] = Some(i444_to_vyuy);
    funcs[PixelFormat::NV12 as usize][PixelFormat::BGRA32 as usize] = Some(nv12_to_bgra32);
    funcs[PixelFormat::NV12 as usize][PixelFormat::RGBA32 as usize] = Some(nv12_to_rgba32);
    funcs[PixelFormat::NV12 as usize][PixelFormat::BGR24 as usize] = Some(nv12_to_bgr24);
    funcs[PixelFormat::NV12 as usize][PixelFormat::RGB24 as usize] = Some(nv12_to_rgb24);
    funcs[PixelFormat::NV16 as usize][PixelFormat::BGRA32 as usize] = Some(nv16_to_bgra32);
    funcs[PixelFormat::NV16 as usize][PixelFormat::RGBA32 as usize] = Some(nv16_to_rgba32);
    funcs[PixelFormat::NV16 as usize][PixelFormat::BGR24 as usize] = Some(nv16_to_bgr24);
    funcs[PixelFormat::NV16 as usize][PixelFormat::RGB24 as usize] = Some(nv16_to_rgb24);
    funcs[PixelFormat::NV24 as usize][PixelFormat::BGRA32 as usize] = Some(nv24_to_bgra32);
    funcs[PixelFormat::NV24 as usize][PixelFormat::RGBA32 as usize] = Some(nv24_to_rgba32);
    funcs[PixelFormat::NV24 as usize][PixelFormat::BGR24 as usize] = Some(nv24_to_bgr24);
    funcs[PixelFormat::NV24 as usize][PixelFormat::RGB24 as usize] = Some(nv24_to_rgb24);
    funcs[PixelFormat::NV21 as usize][PixelFormat::BGRA32 as usize] = Some(nv21_to_bgra32);
    funcs[PixelFormat::NV21 as usize][PixelFormat::RGBA32 as usize] = Some(nv21_to_rgba32);
    funcs[PixelFormat::NV21 as usize][PixelFormat::BGR24 as usize] = Some(nv21_to_bgr24);
    funcs[PixelFormat::NV21 as usize][PixelFormat::RGB24 as usize] = Some(nv21_to_rgb24);
    funcs[PixelFormat::NV61 as usize][PixelFormat::BGRA32 as usize] = Some(nv61_to_bgra32);
    funcs[PixelFormat::NV61 as usize][PixelFormat::RGBA32 as usize] = Some(nv61_to_rgba32);
    funcs[PixelFormat::NV61 as usize][PixelFormat::BGR24 as usize] = Some(nv61_to_bgr24);
    funcs[PixelFormat::NV61 as usize][PixelFormat::RGB24 as usize] = Some(nv61_to_rgb24);
    funcs[PixelFormat::NV42 as usize][PixelFormat::BGRA32 as usize] = Some(nv42_to_bgra32);
    funcs[PixelFormat::NV42 as usize][PixelFormat::RGBA32 as usize] = Some(nv42_to_rgba32);
    funcs[PixelFormat::NV42 as usize][PixelFormat::BGR24 as usize] = Some(nv42_to_bgr24);
    funcs[PixelFormat::NV42 as usize][PixelFormat::RGB24 as usize] = Some(nv42_to_rgb24);
    funcs[PixelFormat::YUYV as usize][PixelFormat::BGRA32 as usize] = Some(yuyv_to_bgra32);
    funcs[PixelFormat::YUYV as usize][PixelFormat::RGBA32 as usize] = Some(yuyv_to_rgba32);
    funcs[PixelFormat::YUYV as usize][PixelFormat::BGR24 as usize] = Some(yuyv_to_bgr24);
    funcs[PixelFormat::YUYV as usize][PixelFormat::RGB24 as usize] = Some(yuyv_to_rgb24);
    funcs[PixelFormat::YUYV as usize][PixelFormat::I420 as usize] = Some(yuyv_to_i420);
    funcs[PixelFormat::YUYV as usize][PixelFormat::I422 as usize] = Some(yuyv_to_i422);
    funcs[PixelFormat::YUYV as usize][PixelFormat::I444 as usize] = Some(yuyv_to_i444);
    funcs[PixelFormat::YVYU as usize][PixelFormat::BGRA32 as usize] = Some(yvyu_to_bgra32);
    funcs[PixelFormat::YVYU as usize][PixelFormat::RGBA32 as usize] = Some(yvyu_to_rgba32);
    funcs[PixelFormat::YVYU as usize][PixelFormat::BGR24 as usize] = Some(yvyu_to_bgr24);
    funcs[PixelFormat::YVYU as usize][PixelFormat::RGB24 as usize] = Some(yvyu_to_rgb24);
    funcs[PixelFormat::YVYU as usize][PixelFormat::I420 as usize] = Some(yvyu_to_i420);
    funcs[PixelFormat::YVYU as usize][PixelFormat::I422 as usize] = Some(yvyu_to_i422);
    funcs[PixelFormat::YVYU as usize][PixelFormat::I444 as usize] = Some(yvyu_to_i444);
    funcs[PixelFormat::UYVY as usize][PixelFormat::BGRA32 as usize] = Some(uyvy_to_bgra32);
    funcs[PixelFormat::UYVY as usize][PixelFormat::RGBA32 as usize] = Some(uyvy_to_rgba32);
    funcs[PixelFormat::UYVY as usize][PixelFormat::BGR24 as usize] = Some(uyvy_to_bgr24);
    funcs[PixelFormat::UYVY as usize][PixelFormat::RGB24 as usize] = Some(uyvy_to_rgb24);
    funcs[PixelFormat::UYVY as usize][PixelFormat::I420 as usize] = Some(uyvy_to_i420);
    funcs[PixelFormat::UYVY as usize][PixelFormat::I422 as usize] = Some(uyvy_to_i422);
    funcs[PixelFormat::UYVY as usize][PixelFormat::I444 as usize] = Some(uyvy_to_i444);
    funcs[PixelFormat::VYUY as usize][PixelFormat::BGRA32 as usize] = Some(vyuy_to_bgra32);
    funcs[PixelFormat::VYUY as usize][PixelFormat::RGBA32 as usize] = Some(vyuy_to_rgba32);
    funcs[PixelFormat::VYUY as usize][PixelFormat::BGR24 as usize] = Some(vyuy_to_bgr24);
    funcs[PixelFormat::VYUY as usize][PixelFormat::RGB24 as usize] = Some(vyuy_to_rgb24);
    funcs[PixelFormat::VYUY as usize][PixelFormat::I420 as usize] = Some(vyuy_to_i420);
    funcs[PixelFormat::VYUY as usize][PixelFormat::I422 as usize] = Some(vyuy_to_i422);
    funcs[PixelFormat::VYUY as usize][PixelFormat::I444 as usize] = Some(vyuy_to_i444);
    funcs[PixelFormat::I010 as usize][PixelFormat::RGB30 as usize] = Some(i010_to_rgb30);
    funcs[PixelFormat::I210 as usize][PixelFormat::RGB30 as usize] = Some(i210_to_rgb30);
    funcs[PixelFormat::I410 as usize][PixelFormat::RGB30 as usize] = Some(i410_to_rgb30);
    funcs[PixelFormat::P010 as usize][PixelFormat::RGB30 as usize] = Some(p010_to_rgb30);
    funcs[PixelFormat::P210 as usize][PixelFormat::RGB30 as usize] = Some(p210_to_rgb30);
    funcs
});

fn data_copy(src: &MappedPlanes, dst: &mut MappedPlanes, format: PixelFormat, width: NonZeroU32, height: NonZeroU32) -> Result<()> {
    if src.planes.len() != dst.planes.len() {
        return Err(invalid_error!("planes size mismatch"));
    }

    for (plane_index, (src_plane, dst_plane)) in src.planes.iter().zip(&mut dst.planes).enumerate() {
        let plane_row_bytes = format.calc_plane_row_bytes(plane_index, width.get()) as usize;
        let plane_height = format.calc_plane_height(plane_index, height.get());
        if let (Some(src_stride), Some(dst_stride)) = (src_plane.stride(), dst_plane.stride()) {
            if let (Some(src_data), Some(dst_data)) = (src_plane.data(), dst_plane.data_mut()) {
                for row in 0..plane_height {
                    let src_start = row as usize * src_stride;
                    let dst_start = row as usize * dst_stride;
                    dst_data[dst_start..dst_start + plane_row_bytes].copy_from_slice(&src_data[src_start..src_start + plane_row_bytes]);
                }
            }
        }
    }

    Ok(())
}

impl Frame<'_> {
    pub fn convert_video_to(&self, dst: &mut Frame) -> Result<()> {
        let (FrameDescriptor::Video(src_desc), FrameDescriptor::Video(dst_desc)) = (&self.desc, &dst.desc) else {
            return Err(invalid_error!("not video frame"));
        };

        VideoFrame::convert_video_to_internal(src_desc, &self.data, dst_desc, &mut dst.data)
    }
}

impl VideoFrame<'_> {
    fn convert_video_to_internal(
        src_desc: &VideoFrameDescriptor,
        src_data: &FrameData,
        dst_desc: &VideoFrameDescriptor,
        dst_data: &mut FrameData,
    ) -> Result<()> {
        if src_desc.dimensions != dst_desc.dimensions {
            return Err(invalid_error!("video frame dimensions mismatch"));
        }

        let guard = src_data.map()?;
        let mut dst_guard = dst_data.map_mut()?;
        let src_planes = guard.planes().unwrap();
        let mut dst_planes = dst_guard.planes_mut().unwrap();

        if src_desc.format == dst_desc.format {
            return data_copy(&src_planes, &mut dst_planes, src_desc.format, src_desc.width(), src_desc.height());
        }

        let convert = VIDEO_FORMAT_CONVERT_FUNCS[src_desc.format as usize][dst_desc.format as usize]
            .ok_or_else(|| unsupported_error!("video format conversion"))?;

        convert(&src_planes, &mut dst_planes, src_desc.color_range, src_desc.color_matrix, src_desc.width(), src_desc.height())
    }

    pub fn convert_to(&self, dst: &mut VideoFrame) -> Result<()> {
        Self::convert_video_to_internal(&self.desc, &self.data, &dst.desc, &mut dst.data)
    }
}