ff-format 0.13.1

Common types for video/audio processing - the Rust way
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
//! Video frame type.
//!
//! This module provides [`VideoFrame`] for working with decoded video frames.
//!
//! # Examples
//!
//! ```
//! use ff_format::{PixelFormat, PooledBuffer, Rational, Timestamp, VideoFrame};
//!
//! // Create a simple 1920x1080 RGBA frame
//! let width = 1920u32;
//! let height = 1080u32;
//! let bytes_per_pixel = 4; // RGBA
//! let stride = width as usize * bytes_per_pixel;
//! let data = vec![0u8; stride * height as usize];
//!
//! let frame = VideoFrame::new(
//!     vec![PooledBuffer::standalone(data)],
//!     vec![stride],
//!     width,
//!     height,
//!     PixelFormat::Rgba,
//!     Timestamp::default(),
//!     true,
//! ).unwrap();
//!
//! assert_eq!(frame.width(), 1920);
//! assert_eq!(frame.height(), 1080);
//! assert!(frame.is_key_frame());
//! assert_eq!(frame.num_planes(), 1);
//! ```

use std::fmt;

use crate::error::FrameError;
use crate::{PixelFormat, PooledBuffer, Timestamp};

mod planar;

/// A decoded video frame.
///
/// This structure holds the pixel data and metadata for a single video frame.
/// It supports both packed formats (like RGBA) where all data is in a single
/// plane, and planar formats (like YUV420P) where each color component is
/// stored in a separate plane.
///
/// # Memory Layout
///
/// For packed formats (RGB, RGBA, BGR, BGRA):
/// - Single plane containing all pixel data
/// - Stride equals width × `bytes_per_pixel` (plus optional padding)
///
/// For planar YUV formats (YUV420P, YUV422P, YUV444P):
/// - Plane 0: Y (luma) - full resolution
/// - Plane 1: U (Cb) - may be subsampled
/// - Plane 2: V (Cr) - may be subsampled
///
/// For semi-planar formats (NV12, NV21):
/// - Plane 0: Y (luma) - full resolution
/// - Plane 1: UV interleaved - half height
///
/// # Strides
///
/// Each plane has an associated stride (also called line size or pitch),
/// which is the number of bytes from the start of one row to the start
/// of the next. This may be larger than the actual data width due to
/// alignment requirements.
#[derive(Clone)]
pub struct VideoFrame {
    /// Pixel data for each plane
    planes: Vec<PooledBuffer>,
    /// Stride (bytes per row) for each plane
    strides: Vec<usize>,
    /// Frame width in pixels
    width: u32,
    /// Frame height in pixels
    height: u32,
    /// Pixel format
    format: PixelFormat,
    /// Presentation timestamp
    timestamp: Timestamp,
    /// Whether this is a key frame (I-frame)
    key_frame: bool,
}

impl VideoFrame {
    /// Creates a new video frame with the specified parameters.
    ///
    /// # Arguments
    ///
    /// * `planes` - Pixel data for each plane
    /// * `strides` - Stride (bytes per row) for each plane
    /// * `width` - Frame width in pixels
    /// * `height` - Frame height in pixels
    /// * `format` - Pixel format
    /// * `timestamp` - Presentation timestamp
    /// * `key_frame` - Whether this is a key frame
    ///
    /// # Errors
    ///
    /// Returns [`FrameError::MismatchedPlaneStride`] if `planes.len() != strides.len()`.
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_format::{PixelFormat, PooledBuffer, Rational, Timestamp, VideoFrame};
    ///
    /// // Create a 640x480 YUV420P frame
    /// let width = 640u32;
    /// let height = 480u32;
    ///
    /// // Y plane: full resolution
    /// let y_stride = width as usize;
    /// let y_data = vec![128u8; y_stride * height as usize];
    ///
    /// // U/V planes: half resolution in both dimensions
    /// let uv_stride = (width / 2) as usize;
    /// let uv_height = (height / 2) as usize;
    /// let u_data = vec![128u8; uv_stride * uv_height];
    /// let v_data = vec![128u8; uv_stride * uv_height];
    ///
    /// let frame = VideoFrame::new(
    ///     vec![
    ///         PooledBuffer::standalone(y_data),
    ///         PooledBuffer::standalone(u_data),
    ///         PooledBuffer::standalone(v_data),
    ///     ],
    ///     vec![y_stride, uv_stride, uv_stride],
    ///     width,
    ///     height,
    ///     PixelFormat::Yuv420p,
    ///     Timestamp::default(),
    ///     true,
    /// ).unwrap();
    ///
    /// assert_eq!(frame.num_planes(), 3);
    /// ```
    pub fn new(
        planes: Vec<PooledBuffer>,
        strides: Vec<usize>,
        width: u32,
        height: u32,
        format: PixelFormat,
        timestamp: Timestamp,
        key_frame: bool,
    ) -> Result<Self, FrameError> {
        if planes.len() != strides.len() {
            return Err(FrameError::MismatchedPlaneStride {
                planes: planes.len(),
                strides: strides.len(),
            });
        }
        Ok(Self {
            planes,
            strides,
            width,
            height,
            format,
            timestamp,
            key_frame,
        })
    }

    /// Creates an empty video frame with the specified dimensions and format.
    ///
    /// The frame will have properly sized planes filled with zeros based
    /// on the pixel format.
    ///
    /// # Arguments
    ///
    /// * `width` - Frame width in pixels
    /// * `height` - Frame height in pixels
    /// * `format` - Pixel format
    ///
    /// # Errors
    ///
    /// Returns [`FrameError::UnsupportedPixelFormat`] if the format is
    /// [`PixelFormat::Other`], as the memory layout cannot be determined.
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_format::{PixelFormat, VideoFrame};
    ///
    /// let frame = VideoFrame::empty(1920, 1080, PixelFormat::Rgba).unwrap();
    /// assert_eq!(frame.width(), 1920);
    /// assert_eq!(frame.height(), 1080);
    /// assert_eq!(frame.num_planes(), 1);
    /// ```
    pub fn empty(width: u32, height: u32, format: PixelFormat) -> Result<Self, FrameError> {
        let (planes, strides) = Self::allocate_planes(width, height, format)?;
        Ok(Self {
            planes,
            strides,
            width,
            height,
            format,
            timestamp: Timestamp::default(),
            key_frame: false,
        })
    }

    /// Creates a black YUV420P video frame.
    ///
    /// The Y plane is filled with `0x00`; U and V planes are filled with `0x80`
    /// (neutral chroma). `pts_ms` is the presentation timestamp in milliseconds.
    ///
    /// The `format` parameter is accepted for call-site clarity; always pass
    /// [`PixelFormat::Yuv420p`].
    #[doc(hidden)]
    #[must_use]
    pub fn new_black(width: u32, height: u32, format: PixelFormat, pts_ms: i64) -> Self {
        let y_w = width as usize;
        let y_h = height as usize;
        let uv_w = (width as usize).div_ceil(2);
        let uv_h = (height as usize).div_ceil(2);
        let timestamp = Timestamp::from_millis(pts_ms, crate::Rational::new(1, 1000));
        Self {
            planes: vec![
                PooledBuffer::standalone(vec![0u8; y_w * y_h]),
                PooledBuffer::standalone(vec![0x80u8; uv_w * uv_h]),
                PooledBuffer::standalone(vec![0x80u8; uv_w * uv_h]),
            ],
            strides: vec![y_w, uv_w, uv_w],
            width,
            height,
            format,
            timestamp,
            key_frame: true,
        }
    }

    // ==========================================================================
    // Metadata Accessors
    // ==========================================================================

    /// Returns the frame width in pixels.
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_format::{PixelFormat, VideoFrame};
    ///
    /// let frame = VideoFrame::empty(1920, 1080, PixelFormat::Rgba).unwrap();
    /// assert_eq!(frame.width(), 1920);
    /// ```
    #[must_use]
    #[inline]
    pub const fn width(&self) -> u32 {
        self.width
    }

    /// Returns the frame height in pixels.
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_format::{PixelFormat, VideoFrame};
    ///
    /// let frame = VideoFrame::empty(1920, 1080, PixelFormat::Rgba).unwrap();
    /// assert_eq!(frame.height(), 1080);
    /// ```
    #[must_use]
    #[inline]
    pub const fn height(&self) -> u32 {
        self.height
    }

    /// Returns the pixel format of this frame.
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_format::{PixelFormat, VideoFrame};
    ///
    /// let frame = VideoFrame::empty(1920, 1080, PixelFormat::Yuv420p).unwrap();
    /// assert_eq!(frame.format(), PixelFormat::Yuv420p);
    /// ```
    #[must_use]
    #[inline]
    pub const fn format(&self) -> PixelFormat {
        self.format
    }

    /// Returns the presentation timestamp of this frame.
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_format::{PixelFormat, PooledBuffer, Rational, Timestamp, VideoFrame};
    ///
    /// let ts = Timestamp::new(90000, Rational::new(1, 90000));
    /// let frame = VideoFrame::new(
    ///     vec![PooledBuffer::standalone(vec![0u8; 1920 * 1080 * 4])],
    ///     vec![1920 * 4],
    ///     1920,
    ///     1080,
    ///     PixelFormat::Rgba,
    ///     ts,
    ///     true,
    /// ).unwrap();
    /// assert_eq!(frame.timestamp(), ts);
    /// ```
    #[must_use]
    #[inline]
    pub const fn timestamp(&self) -> Timestamp {
        self.timestamp
    }

    /// Returns whether this frame is a key frame (I-frame).
    ///
    /// Key frames are complete frames that don't depend on any other frames
    /// for decoding. They are used as reference points for seeking.
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_format::{PixelFormat, VideoFrame};
    ///
    /// let mut frame = VideoFrame::empty(1920, 1080, PixelFormat::Rgba).unwrap();
    /// assert!(!frame.is_key_frame());
    ///
    /// frame.set_key_frame(true);
    /// assert!(frame.is_key_frame());
    /// ```
    #[must_use]
    #[inline]
    pub const fn is_key_frame(&self) -> bool {
        self.key_frame
    }

    /// Sets whether this frame is a key frame.
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_format::{PixelFormat, VideoFrame};
    ///
    /// let mut frame = VideoFrame::empty(640, 480, PixelFormat::Rgba).unwrap();
    /// frame.set_key_frame(true);
    /// assert!(frame.is_key_frame());
    /// ```
    #[inline]
    pub fn set_key_frame(&mut self, key_frame: bool) {
        self.key_frame = key_frame;
    }

    /// Sets the timestamp of this frame.
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_format::{PixelFormat, Rational, Timestamp, VideoFrame};
    ///
    /// let mut frame = VideoFrame::empty(640, 480, PixelFormat::Rgba).unwrap();
    /// let ts = Timestamp::new(3000, Rational::new(1, 90000));
    /// frame.set_timestamp(ts);
    /// assert_eq!(frame.timestamp(), ts);
    /// ```
    #[inline]
    pub fn set_timestamp(&mut self, timestamp: Timestamp) {
        self.timestamp = timestamp;
    }

    // ==========================================================================
    // Plane Data Access
    // ==========================================================================

    /// Returns the number of planes in this frame.
    ///
    /// - Packed formats (RGBA, RGB24, etc.): 1 plane
    /// - Planar YUV (YUV420P, YUV422P, YUV444P): 3 planes
    /// - Semi-planar (NV12, NV21): 2 planes
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_format::{PixelFormat, VideoFrame};
    ///
    /// let rgba = VideoFrame::empty(640, 480, PixelFormat::Rgba).unwrap();
    /// assert_eq!(rgba.num_planes(), 1);
    ///
    /// let yuv = VideoFrame::empty(640, 480, PixelFormat::Yuv420p).unwrap();
    /// assert_eq!(yuv.num_planes(), 3);
    ///
    /// let nv12 = VideoFrame::empty(640, 480, PixelFormat::Nv12).unwrap();
    /// assert_eq!(nv12.num_planes(), 2);
    /// ```
    #[must_use]
    #[inline]
    pub fn num_planes(&self) -> usize {
        self.planes.len()
    }

    /// Returns a slice of all plane buffers.
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_format::{PixelFormat, VideoFrame};
    ///
    /// let frame = VideoFrame::empty(640, 480, PixelFormat::Yuv420p).unwrap();
    /// let planes = frame.planes();
    /// assert_eq!(planes.len(), 3);
    /// ```
    #[must_use]
    #[inline]
    pub fn planes(&self) -> &[PooledBuffer] {
        &self.planes
    }

    /// Returns the data for a specific plane, or `None` if the index is out of bounds.
    ///
    /// # Arguments
    ///
    /// * `index` - The plane index (0 for Y/RGB, 1 for U/UV, 2 for V)
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_format::{PixelFormat, VideoFrame};
    ///
    /// let frame = VideoFrame::empty(640, 480, PixelFormat::Yuv420p).unwrap();
    ///
    /// // Y plane exists
    /// assert!(frame.plane(0).is_some());
    ///
    /// // U and V planes exist
    /// assert!(frame.plane(1).is_some());
    /// assert!(frame.plane(2).is_some());
    ///
    /// // No fourth plane
    /// assert!(frame.plane(3).is_none());
    /// ```
    #[must_use]
    #[inline]
    pub fn plane(&self, index: usize) -> Option<&[u8]> {
        self.planes.get(index).map(std::convert::AsRef::as_ref)
    }

    /// Returns mutable access to a specific plane's data.
    ///
    /// # Arguments
    ///
    /// * `index` - The plane index
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_format::{PixelFormat, VideoFrame};
    ///
    /// let mut frame = VideoFrame::empty(640, 480, PixelFormat::Rgba).unwrap();
    /// if let Some(data) = frame.plane_mut(0) {
    ///     // Fill with red (RGBA)
    ///     for chunk in data.chunks_exact_mut(4) {
    ///         chunk[0] = 255; // R
    ///         chunk[1] = 0;   // G
    ///         chunk[2] = 0;   // B
    ///         chunk[3] = 255; // A
    ///     }
    /// }
    /// ```
    #[must_use]
    #[inline]
    pub fn plane_mut(&mut self, index: usize) -> Option<&mut [u8]> {
        self.planes.get_mut(index).map(std::convert::AsMut::as_mut)
    }

    /// Returns a slice of all stride values.
    ///
    /// Strides indicate the number of bytes between the start of consecutive
    /// rows in each plane.
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_format::{PixelFormat, VideoFrame};
    ///
    /// let frame = VideoFrame::empty(1920, 1080, PixelFormat::Rgba).unwrap();
    /// let strides = frame.strides();
    /// assert_eq!(strides[0], 1920 * 4); // RGBA = 4 bytes per pixel
    /// ```
    #[must_use]
    #[inline]
    pub fn strides(&self) -> &[usize] {
        &self.strides
    }

    /// Returns the stride for a specific plane, or `None` if the index is out of bounds.
    ///
    /// # Arguments
    ///
    /// * `plane` - The plane index
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_format::{PixelFormat, VideoFrame};
    ///
    /// let frame = VideoFrame::empty(640, 480, PixelFormat::Yuv420p).unwrap();
    ///
    /// // Y plane stride = width
    /// assert_eq!(frame.stride(0), Some(640));
    ///
    /// // U/V plane stride = width / 2
    /// assert_eq!(frame.stride(1), Some(320));
    /// assert_eq!(frame.stride(2), Some(320));
    /// ```
    #[must_use]
    #[inline]
    pub fn stride(&self, plane: usize) -> Option<usize> {
        self.strides.get(plane).copied()
    }

    // ==========================================================================
    // Contiguous Data Access
    // ==========================================================================

    /// Returns the frame data as a contiguous byte vector.
    ///
    /// For packed formats with a single plane, this returns a copy of the plane data.
    /// For planar formats, this concatenates all planes into a single buffer.
    ///
    /// # Note
    ///
    /// This method allocates a new vector and copies the data. For zero-copy
    /// access, use [`plane()`](Self::plane) or [`planes()`](Self::planes) instead.
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_format::{PixelFormat, VideoFrame};
    ///
    /// let frame = VideoFrame::empty(4, 4, PixelFormat::Rgba).unwrap();
    /// let data = frame.data();
    /// assert_eq!(data.len(), 4 * 4 * 4); // 4x4 pixels, 4 bytes each
    /// ```
    #[must_use]
    pub fn data(&self) -> Vec<u8> {
        let total_size: usize = self.planes.iter().map(PooledBuffer::len).sum();
        let mut result = Vec::with_capacity(total_size);
        for plane in &self.planes {
            result.extend_from_slice(plane.as_ref());
        }
        result
    }

    /// Returns a reference to the first plane's data as a contiguous slice.
    ///
    /// This is only meaningful for packed formats (RGBA, RGB24, etc.) where
    /// all data is in a single plane. Returns `None` if the format is planar
    /// or if no planes exist.
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_format::{PixelFormat, VideoFrame};
    ///
    /// // Packed format - returns data
    /// let rgba = VideoFrame::empty(640, 480, PixelFormat::Rgba).unwrap();
    /// assert!(rgba.data_ref().is_some());
    ///
    /// // Planar format - returns None
    /// let yuv = VideoFrame::empty(640, 480, PixelFormat::Yuv420p).unwrap();
    /// assert!(yuv.data_ref().is_none());
    /// ```
    #[must_use]
    #[inline]
    pub fn data_ref(&self) -> Option<&[u8]> {
        if self.format.is_packed() && self.planes.len() == 1 {
            Some(self.planes[0].as_ref())
        } else {
            None
        }
    }

    /// Returns a mutable reference to the first plane's data.
    ///
    /// This is only meaningful for packed formats where all data is in a
    /// single plane. Returns `None` if the format is planar.
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_format::{PixelFormat, VideoFrame};
    ///
    /// let mut frame = VideoFrame::empty(4, 4, PixelFormat::Rgba).unwrap();
    /// if let Some(data) = frame.data_mut() {
    ///     data[0] = 255; // Modify first byte
    /// }
    /// ```
    #[must_use]
    #[inline]
    pub fn data_mut(&mut self) -> Option<&mut [u8]> {
        if self.format.is_packed() && self.planes.len() == 1 {
            Some(self.planes[0].as_mut())
        } else {
            None
        }
    }

    // ==========================================================================
    // Utility Methods
    // ==========================================================================

    /// Returns the total size in bytes of all plane data.
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_format::{PixelFormat, VideoFrame};
    ///
    /// let frame = VideoFrame::empty(1920, 1080, PixelFormat::Rgba).unwrap();
    /// assert_eq!(frame.total_size(), 1920 * 1080 * 4);
    /// ```
    #[must_use]
    pub fn total_size(&self) -> usize {
        self.planes.iter().map(PooledBuffer::len).sum()
    }

    /// Returns the resolution as a (width, height) tuple.
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_format::{PixelFormat, VideoFrame};
    ///
    /// let frame = VideoFrame::empty(1920, 1080, PixelFormat::Rgba).unwrap();
    /// assert_eq!(frame.resolution(), (1920, 1080));
    /// ```
    #[must_use]
    #[inline]
    pub const fn resolution(&self) -> (u32, u32) {
        (self.width, self.height)
    }

    /// Returns the aspect ratio as a floating-point value.
    ///
    /// # Examples
    ///
    /// ```
    /// use ff_format::{PixelFormat, VideoFrame};
    ///
    /// let frame = VideoFrame::empty(1920, 1080, PixelFormat::Rgba).unwrap();
    /// let aspect = frame.aspect_ratio();
    /// assert!((aspect - 16.0 / 9.0).abs() < 0.01);
    /// ```
    #[must_use]
    #[inline]
    pub fn aspect_ratio(&self) -> f64 {
        if self.height == 0 {
            log::warn!(
                "aspect_ratio unavailable, height is 0, returning 0.0 \
                 width={} height=0 fallback=0.0",
                self.width
            );
            0.0
        } else {
            f64::from(self.width) / f64::from(self.height)
        }
    }
}

impl fmt::Debug for VideoFrame {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("VideoFrame")
            .field("width", &self.width)
            .field("height", &self.height)
            .field("format", &self.format)
            .field("timestamp", &self.timestamp)
            .field("key_frame", &self.key_frame)
            .field("num_planes", &self.planes.len())
            .field(
                "plane_sizes",
                &self
                    .planes
                    .iter()
                    .map(PooledBuffer::len)
                    .collect::<Vec<_>>(),
            )
            .field("strides", &self.strides)
            .finish()
    }
}

impl fmt::Display for VideoFrame {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "VideoFrame({}x{} {} @ {}{})",
            self.width,
            self.height,
            self.format,
            self.timestamp,
            if self.key_frame { " [KEY]" } else { "" }
        )
    }
}

impl Default for VideoFrame {
    /// Returns a default empty 1x1 YUV420P frame.
    ///
    /// This constructs a minimal valid frame directly.
    fn default() -> Self {
        // Construct a minimal 1x1 YUV420P frame directly
        // Y plane: 1 byte, U plane: 1 byte, V plane: 1 byte
        Self {
            planes: vec![
                PooledBuffer::standalone(vec![0u8; 1]),
                PooledBuffer::standalone(vec![0u8; 1]),
                PooledBuffer::standalone(vec![0u8; 1]),
            ],
            strides: vec![1, 1, 1],
            width: 1,
            height: 1,
            format: PixelFormat::Yuv420p,
            timestamp: Timestamp::default(),
            key_frame: false,
        }
    }
}

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::redundant_closure_for_method_calls,
    clippy::float_cmp
)]
mod tests {
    use super::*;
    use crate::Rational;

    // ==========================================================================
    // Construction Tests
    // ==========================================================================

    #[test]
    fn test_new_rgba_frame() {
        let width = 640u32;
        let height = 480u32;
        let stride = width as usize * 4;
        let data = vec![0u8; stride * height as usize];
        let ts = Timestamp::new(1000, Rational::new(1, 1000));

        let frame = VideoFrame::new(
            vec![PooledBuffer::standalone(data)],
            vec![stride],
            width,
            height,
            PixelFormat::Rgba,
            ts,
            true,
        )
        .unwrap();

        assert_eq!(frame.width(), 640);
        assert_eq!(frame.height(), 480);
        assert_eq!(frame.format(), PixelFormat::Rgba);
        assert_eq!(frame.timestamp(), ts);
        assert!(frame.is_key_frame());
        assert_eq!(frame.num_planes(), 1);
        assert_eq!(frame.stride(0), Some(640 * 4));
    }

    #[test]
    fn test_new_yuv420p_frame() {
        let width = 640u32;
        let height = 480u32;

        let y_stride = width as usize;
        let uv_stride = (width / 2) as usize;
        let uv_height = (height / 2) as usize;

        let y_data = vec![128u8; y_stride * height as usize];
        let u_data = vec![128u8; uv_stride * uv_height];
        let v_data = vec![128u8; uv_stride * uv_height];

        let frame = VideoFrame::new(
            vec![
                PooledBuffer::standalone(y_data),
                PooledBuffer::standalone(u_data),
                PooledBuffer::standalone(v_data),
            ],
            vec![y_stride, uv_stride, uv_stride],
            width,
            height,
            PixelFormat::Yuv420p,
            Timestamp::default(),
            false,
        )
        .unwrap();

        assert_eq!(frame.width(), 640);
        assert_eq!(frame.height(), 480);
        assert_eq!(frame.format(), PixelFormat::Yuv420p);
        assert!(!frame.is_key_frame());
        assert_eq!(frame.num_planes(), 3);
        assert_eq!(frame.stride(0), Some(640));
        assert_eq!(frame.stride(1), Some(320));
        assert_eq!(frame.stride(2), Some(320));
    }

    #[test]
    fn test_new_mismatched_planes_strides() {
        let result = VideoFrame::new(
            vec![PooledBuffer::standalone(vec![0u8; 100])],
            vec![10, 10], // Mismatched length
            10,
            10,
            PixelFormat::Rgba,
            Timestamp::default(),
            false,
        );

        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err(),
            FrameError::MismatchedPlaneStride {
                planes: 1,
                strides: 2
            }
        );
    }

    // ==========================================================================
    // Metadata Tests
    // ==========================================================================

    #[test]
    fn test_resolution() {
        let frame = VideoFrame::empty(1920, 1080, PixelFormat::Rgba).unwrap();
        assert_eq!(frame.resolution(), (1920, 1080));
    }

    #[test]
    fn test_aspect_ratio() {
        let frame = VideoFrame::empty(1920, 1080, PixelFormat::Rgba).unwrap();
        let aspect = frame.aspect_ratio();
        assert!((aspect - 16.0 / 9.0).abs() < 0.001);

        let frame_4_3 = VideoFrame::empty(640, 480, PixelFormat::Rgba).unwrap();
        let aspect_4_3 = frame_4_3.aspect_ratio();
        assert!((aspect_4_3 - 4.0 / 3.0).abs() < 0.001);
    }

    #[test]
    fn test_aspect_ratio_zero_height() {
        let frame = VideoFrame::new(
            vec![PooledBuffer::standalone(vec![])],
            vec![0],
            100,
            0,
            PixelFormat::Rgba,
            Timestamp::default(),
            false,
        )
        .unwrap();
        assert_eq!(frame.aspect_ratio(), 0.0);
    }

    #[test]
    fn test_total_size_rgba() {
        let frame = VideoFrame::empty(1920, 1080, PixelFormat::Rgba).unwrap();
        assert_eq!(frame.total_size(), 1920 * 1080 * 4);
    }

    #[test]
    fn test_total_size_yuv420p() {
        let frame = VideoFrame::empty(640, 480, PixelFormat::Yuv420p).unwrap();
        // Y: 640*480, U: 320*240, V: 320*240
        let expected = 640 * 480 + 320 * 240 * 2;
        assert_eq!(frame.total_size(), expected);
    }

    #[test]
    fn test_set_key_frame() {
        let mut frame = VideoFrame::empty(640, 480, PixelFormat::Rgba).unwrap();
        assert!(!frame.is_key_frame());

        frame.set_key_frame(true);
        assert!(frame.is_key_frame());

        frame.set_key_frame(false);
        assert!(!frame.is_key_frame());
    }

    #[test]
    fn test_set_timestamp() {
        let mut frame = VideoFrame::empty(640, 480, PixelFormat::Rgba).unwrap();
        let ts = Timestamp::new(90000, Rational::new(1, 90000));

        frame.set_timestamp(ts);
        assert_eq!(frame.timestamp(), ts);
    }

    // ==========================================================================
    // Plane Access Tests
    // ==========================================================================

    #[test]
    fn test_plane_access() {
        let frame = VideoFrame::empty(640, 480, PixelFormat::Yuv420p).unwrap();

        assert!(frame.plane(0).is_some());
        assert!(frame.plane(1).is_some());
        assert!(frame.plane(2).is_some());
        assert!(frame.plane(3).is_none());
    }

    #[test]
    fn test_plane_mut_access() {
        let mut frame = VideoFrame::empty(4, 4, PixelFormat::Rgba).unwrap();

        if let Some(data) = frame.plane_mut(0) {
            // Fill with red
            for chunk in data.chunks_exact_mut(4) {
                chunk[0] = 255;
                chunk[1] = 0;
                chunk[2] = 0;
                chunk[3] = 255;
            }
        }

        let plane = frame.plane(0).unwrap();
        assert_eq!(plane[0], 255); // R
        assert_eq!(plane[1], 0); // G
        assert_eq!(plane[2], 0); // B
        assert_eq!(plane[3], 255); // A
    }

    #[test]
    fn test_planes_slice() {
        let frame = VideoFrame::empty(640, 480, PixelFormat::Yuv420p).unwrap();
        let planes = frame.planes();
        assert_eq!(planes.len(), 3);
    }

    #[test]
    fn test_strides_slice() {
        let frame = VideoFrame::empty(640, 480, PixelFormat::Yuv420p).unwrap();
        let strides = frame.strides();
        assert_eq!(strides.len(), 3);
        assert_eq!(strides[0], 640);
        assert_eq!(strides[1], 320);
        assert_eq!(strides[2], 320);
    }

    #[test]
    fn test_stride_out_of_bounds() {
        let frame = VideoFrame::empty(640, 480, PixelFormat::Rgba).unwrap();
        assert!(frame.stride(0).is_some());
        assert!(frame.stride(1).is_none());
    }

    // ==========================================================================
    // Data Access Tests
    // ==========================================================================

    #[test]
    fn test_data_contiguous() {
        let frame = VideoFrame::empty(4, 4, PixelFormat::Rgba).unwrap();
        let data = frame.data();
        assert_eq!(data.len(), 4 * 4 * 4);
    }

    #[test]
    fn test_data_yuv420p_concatenation() {
        let frame = VideoFrame::empty(4, 4, PixelFormat::Yuv420p).unwrap();
        let data = frame.data();
        // Y: 4*4 + U: 2*2 + V: 2*2 = 16 + 4 + 4 = 24
        assert_eq!(data.len(), 24);
    }

    #[test]
    fn test_data_ref_packed() {
        let frame = VideoFrame::empty(640, 480, PixelFormat::Rgba).unwrap();
        assert!(frame.data_ref().is_some());
        assert_eq!(frame.data_ref().map(|d| d.len()), Some(640 * 480 * 4));
    }

    #[test]
    fn test_data_ref_planar() {
        let frame = VideoFrame::empty(640, 480, PixelFormat::Yuv420p).unwrap();
        assert!(frame.data_ref().is_none());
    }

    #[test]
    fn test_data_mut_packed() {
        let mut frame = VideoFrame::empty(4, 4, PixelFormat::Rgba).unwrap();
        assert!(frame.data_mut().is_some());

        if let Some(data) = frame.data_mut() {
            data[0] = 123;
        }

        assert_eq!(frame.plane(0).unwrap()[0], 123);
    }

    #[test]
    fn test_data_mut_planar() {
        let mut frame = VideoFrame::empty(640, 480, PixelFormat::Yuv420p).unwrap();
        assert!(frame.data_mut().is_none());
    }

    // ==========================================================================
    // Clone Tests
    // ==========================================================================

    #[test]
    fn test_clone() {
        let mut original = VideoFrame::empty(640, 480, PixelFormat::Rgba).unwrap();
        original.set_key_frame(true);
        original.set_timestamp(Timestamp::new(1000, Rational::new(1, 1000)));

        // Modify some data
        if let Some(data) = original.plane_mut(0) {
            data[0] = 42;
        }

        let cloned = original.clone();

        // Verify metadata matches
        assert_eq!(cloned.width(), original.width());
        assert_eq!(cloned.height(), original.height());
        assert_eq!(cloned.format(), original.format());
        assert_eq!(cloned.timestamp(), original.timestamp());
        assert_eq!(cloned.is_key_frame(), original.is_key_frame());

        // Verify data was cloned
        assert_eq!(cloned.plane(0).unwrap()[0], 42);

        // Verify it's a deep clone (modifying clone doesn't affect original)
        let mut cloned = cloned;
        if let Some(data) = cloned.plane_mut(0) {
            data[0] = 99;
        }
        assert_eq!(original.plane(0).unwrap()[0], 42);
        assert_eq!(cloned.plane(0).unwrap()[0], 99);
    }

    #[test]
    fn video_frame_clone_should_have_identical_data() {
        let width = 320u32;
        let height = 240u32;
        let stride = width as usize;
        let y_data = vec![42u8; stride * height as usize];
        let uv_stride = (width / 2) as usize;
        let uv_data = vec![128u8; uv_stride * (height / 2) as usize];
        let ts = Timestamp::new(1000, Rational::new(1, 1000));

        let original = VideoFrame::new(
            vec![
                PooledBuffer::standalone(y_data.clone()),
                PooledBuffer::standalone(uv_data.clone()),
                PooledBuffer::standalone(uv_data.clone()),
            ],
            vec![stride, uv_stride, uv_stride],
            width,
            height,
            PixelFormat::Yuv420p,
            ts,
            false,
        )
        .unwrap();

        let clone = original.clone();

        assert_eq!(clone.width(), original.width());
        assert_eq!(clone.height(), original.height());
        assert_eq!(clone.format(), original.format());
        assert_eq!(clone.timestamp(), original.timestamp());
        assert_eq!(clone.is_key_frame(), original.is_key_frame());
        assert_eq!(clone.num_planes(), original.num_planes());
        assert_eq!(clone.plane(0), original.plane(0));
    }

    // ==========================================================================
    // Display/Debug Tests
    // ==========================================================================

    #[test]
    fn test_debug() {
        let frame = VideoFrame::empty(640, 480, PixelFormat::Rgba).unwrap();
        let debug = format!("{frame:?}");
        assert!(debug.contains("VideoFrame"));
        assert!(debug.contains("640"));
        assert!(debug.contains("480"));
        assert!(debug.contains("Rgba"));
    }

    #[test]
    fn test_display() {
        let mut frame = VideoFrame::empty(1920, 1080, PixelFormat::Yuv420p).unwrap();
        frame.set_key_frame(true);

        let display = format!("{frame}");
        assert!(display.contains("1920x1080"));
        assert!(display.contains("yuv420p"));
        assert!(display.contains("[KEY]"));
    }

    #[test]
    fn test_display_non_keyframe() {
        let frame = VideoFrame::empty(1920, 1080, PixelFormat::Rgba).unwrap();
        let display = format!("{frame}");
        assert!(!display.contains("[KEY]"));
    }
}