Skip to main content

ff_decode/video/builder/
format.rs

1//! Output pixel format option for [`VideoDecoderBuilder`].
2
3use ff_format::PixelFormat;
4
5use super::VideoDecoderBuilder;
6
7impl VideoDecoderBuilder {
8    /// Sets the output pixel format for decoded frames.
9    ///
10    /// If not set, frames are returned in the source format. Setting an
11    /// output format enables automatic conversion during decoding.
12    ///
13    /// # Common Formats
14    ///
15    /// - [`PixelFormat::Rgba`] - Best for UI rendering, includes alpha
16    /// - [`PixelFormat::Rgb24`] - RGB without alpha, smaller memory footprint
17    /// - [`PixelFormat::Yuv420p`] - Source format for most H.264/H.265 videos
18    ///
19    /// # Examples
20    ///
21    /// ```ignore
22    /// use ff_decode::VideoDecoder;
23    /// use ff_format::PixelFormat;
24    ///
25    /// let decoder = VideoDecoder::open("video.mp4")?
26    ///     .output_format(PixelFormat::Rgba)
27    ///     .build()?;
28    /// ```
29    #[must_use]
30    pub fn output_format(mut self, format: PixelFormat) -> Self {
31        self.output_format = Some(format);
32        self
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39    use std::path::PathBuf;
40
41    #[test]
42    fn builder_output_format_should_set_pixel_format() {
43        let builder =
44            VideoDecoderBuilder::new(PathBuf::from("test.mp4")).output_format(PixelFormat::Rgba);
45
46        assert_eq!(builder.get_output_format(), Some(PixelFormat::Rgba));
47    }
48
49    #[test]
50    fn builder_output_format_last_setter_wins() {
51        let builder = VideoDecoderBuilder::new(PathBuf::from("test.mp4"))
52            .output_format(PixelFormat::Rgba)
53            .output_format(PixelFormat::Rgb24);
54
55        assert_eq!(builder.get_output_format(), Some(PixelFormat::Rgb24));
56    }
57
58    #[test]
59    fn builder_output_format_yuv420p_should_be_accepted() {
60        let builder =
61            VideoDecoderBuilder::new(PathBuf::from("test.mp4")).output_format(PixelFormat::Yuv420p);
62
63        assert_eq!(builder.get_output_format(), Some(PixelFormat::Yuv420p));
64    }
65}