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
use std::convert::TryFrom;
use std::fmt;

use crate::fourcc::FourCC;
use crate::v4l_sys;
use crate::v4l_sys::*;

#[derive(Debug)]
/// Format description as returned by VIDIOC_ENUM_FRAMESIZES
pub struct FrameSize {
    pub index: u32,
    pub fourcc: FourCC,
    pub typ: u32,
    pub size: FrameSizeEnum,
}

impl fmt::Display for FrameSize {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "index  : {}", self.index)?;
        writeln!(f, "type   : {}", self.typ)?;
        writeln!(f, "fourcc : {}", self.fourcc)?;
        writeln!(f, "size   : {}", self.size)?;
        Ok(())
    }
}

#[derive(Debug)]
pub enum FrameSizeEnum {
    Discrete(Discrete),
    Stepwise(Stepwise),
}

impl fmt::Display for FrameSizeEnum {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            FrameSizeEnum::Discrete(val) => write!(f, "Discrete({})", val)?,
            FrameSizeEnum::Stepwise(val) => write!(f, "Stepwise({})", val)?,
        }

        Ok(())
    }
}

impl TryFrom<v4l2_frmsizeenum> for FrameSizeEnum {
    type Error = String;

    fn try_from(desc: v4l2_frmsizeenum) -> Result<Self, Self::Error> {
        unsafe {
            // Unsafe because of access to union __bindgen_anon_1
            match desc.type_ {
                v4l_sys::v4l2_frmsizetypes_V4L2_FRMSIZE_TYPE_DISCRETE => Ok({
                    FrameSizeEnum::Discrete(Discrete {
                        width: desc.__bindgen_anon_1.discrete.width,
                        height: desc.__bindgen_anon_1.discrete.height,
                    })
                }),
                v4l_sys::v4l2_frmsizetypes_V4L2_FRMSIZE_TYPE_STEPWISE
                | v4l_sys::v4l2_frmsizetypes_V4L2_FRMSIZE_TYPE_CONTINUOUS => Ok({
                    FrameSizeEnum::Stepwise(Stepwise {
                        min_width: desc.__bindgen_anon_1.stepwise.min_width,
                        max_width: desc.__bindgen_anon_1.stepwise.max_width,
                        step_width: desc.__bindgen_anon_1.stepwise.step_width,
                        min_height: desc.__bindgen_anon_1.stepwise.min_height,
                        max_height: desc.__bindgen_anon_1.stepwise.max_height,
                        step_height: desc.__bindgen_anon_1.stepwise.step_height,
                    })
                }),
                typ => Err(format!("Unknown frame size type: {}", typ)),
            }
        }
    }
}

#[derive(Debug)]
pub struct Discrete {
    /// Width of the frame [pixel].
    pub width: u32,
    /// Height of the frame [pixel].
    pub height: u32,
}

impl fmt::Display for Discrete {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}x{}", self.width, self.height)?;
        Ok(())
    }
}

#[derive(Debug)]
pub struct Stepwise {
    /// Minimum frame width [pixel].
    pub min_width: u32,
    /// Maximum frame width [pixel].
    pub max_width: u32,
    /// Frame width step size [pixel].
    pub step_width: u32,
    /// Minimum frame height [pixel].
    pub min_height: u32,
    /// Maximum frame height [pixel].
    pub max_height: u32,
    /// Frame height step size [pixel].
    pub step_height: u32,
}

impl fmt::Display for Stepwise {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}x{} - {}x{} with step {}/{}",
            self.min_width,
            self.min_height,
            self.max_width,
            self.max_height,
            self.step_width,
            self.step_height,
        )?;
        Ok(())
    }
}

impl TryFrom<v4l2_frmsizeenum> for FrameSize {
    type Error = String;

    fn try_from(desc: v4l2_frmsizeenum) -> Result<Self, Self::Error> {
        Ok(FrameSize {
            index: desc.index,
            typ: desc.type_,
            fourcc: FourCC::from(desc.pixel_format),
            size: FrameSizeEnum::try_from(desc)?,
        })
    }
}