Skip to main content

v4l/format/
description.rs

1use bitflags::bitflags;
2use std::{fmt, str};
3
4use crate::format::FourCC;
5use crate::v4l_sys::*;
6
7bitflags! {
8    #[allow(clippy::unreadable_literal)]
9    pub struct Flags : u32 {
10        const COMPRESSED            = 0x0001;
11        const EMULATED              = 0x0002;
12        const CONTINUOUS_BITSTREAM  = 0x0004;
13        const DYN_RESOLUTION        = 0x0008;
14    }
15}
16
17impl From<u32> for Flags {
18    fn from(flags: u32) -> Self {
19        Self::from_bits_truncate(flags)
20    }
21}
22
23impl From<Flags> for u32 {
24    fn from(flags: Flags) -> Self {
25        flags.bits()
26    }
27}
28
29impl fmt::Display for Flags {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        fmt::Debug::fmt(self, f)
32    }
33}
34
35#[derive(Debug)]
36/// Format description as returned by [`crate::v4l2::vidioc::VIDIOC_ENUM_FMT`]
37pub struct Description {
38    pub index: u32,
39    pub typ: u32,
40    pub flags: Flags,
41    pub description: String,
42    pub fourcc: FourCC,
43}
44
45impl fmt::Display for Description {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        writeln!(f, "index       : {}", self.index)?;
48        writeln!(f, "type:       : {}", self.typ)?;
49        writeln!(f, "flags:      : {}", self.flags)?;
50        writeln!(f, "description : {}", self.description)?;
51        writeln!(f, "fourcc      : {}", self.fourcc)?;
52        Ok(())
53    }
54}
55
56impl From<v4l2_fmtdesc> for Description {
57    fn from(desc: v4l2_fmtdesc) -> Self {
58        Self {
59            index: desc.index,
60            typ: desc.type_,
61            flags: Flags::from(desc.flags),
62            description: str::from_utf8(&desc.description)
63                .unwrap()
64                .trim_matches(char::from(0))
65                .to_string(),
66            fourcc: FourCC::from(desc.pixelformat),
67        }
68    }
69}