Skip to main content

moq_v4l/format/
mod.rs

1use bitflags::bitflags;
2use std::{convert::TryFrom, fmt, mem};
3
4use crate::sys::*;
5
6pub mod colorspace;
7pub use colorspace::Colorspace;
8
9pub mod description;
10pub use description::Description;
11
12pub mod field;
13pub use field::FieldOrder;
14
15pub mod fourcc;
16pub use fourcc::FourCC;
17
18pub mod quantization;
19pub use quantization::Quantization;
20
21pub mod transfer;
22pub use transfer::TransferFunction;
23
24bitflags! {
25	#[allow(clippy::unreadable_literal)]
26	#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
27	pub struct Flags : u32 {
28		const PREMUL_ALPHA  = 0x00000001;
29	}
30}
31
32impl From<u32> for Flags {
33	fn from(flags: u32) -> Self {
34		Self::from_bits_truncate(flags)
35	}
36}
37
38impl From<Flags> for u32 {
39	fn from(flags: Flags) -> Self {
40		flags.bits()
41	}
42}
43
44impl fmt::Display for Flags {
45	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46		fmt::Debug::fmt(self, f)
47	}
48}
49
50#[derive(Debug, Copy, Clone)]
51/// Streaming format (single-planar)
52pub struct Format {
53	/// width in pixels
54	pub width: u32,
55	/// height in pixels
56	pub height: u32,
57	/// pixelformat code
58	pub fourcc: FourCC,
59	/// field order for interlacing
60	pub field_order: FieldOrder,
61
62	/// bytes per line
63	pub stride: u32,
64	/// maximum number of bytes required to store an image
65	pub size: u32,
66
67	/// flags set by the application or driver
68	pub flags: Flags,
69
70	/// supplements the pixelformat (fourcc) information
71	pub colorspace: Colorspace,
72	/// the way colors are mapped
73	pub quantization: Quantization,
74	/// the transfer function for the colorspace
75	pub transfer: TransferFunction,
76}
77
78impl Format {
79	/// Returns a capture format
80	///
81	/// # Arguments
82	///
83	/// * `width` - Width in pixels
84	/// * `height` - Height in pixels
85	/// * `fourcc` - Four character code (pixelformat)
86	///
87	/// # Example
88	///
89	/// ```
90	/// use moq_v4l::{Format, FourCC};
91	/// let fmt = Format::new(640, 480, FourCC::new(b"YUYV"));
92	/// ```
93	pub const fn new(width: u32, height: u32, fourcc: FourCC) -> Self {
94		Format {
95			width,
96			height,
97			fourcc,
98			field_order: FieldOrder::Any,
99			stride: 0,
100			size: 0,
101			flags: Flags::empty(),
102			colorspace: Colorspace::Default,
103			quantization: Quantization::Default,
104			transfer: TransferFunction::Default,
105		}
106	}
107}
108
109impl fmt::Display for Format {
110	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111		writeln!(f, "width          : {}", self.width)?;
112		writeln!(f, "height         : {}", self.height)?;
113		writeln!(f, "fourcc         : {}", self.fourcc)?;
114		writeln!(f, "field          : {}", self.field_order)?;
115		writeln!(f, "stride         : {}", self.stride)?;
116		writeln!(f, "size           : {}", self.size)?;
117		writeln!(f, "colorspace     : {}", self.colorspace)?;
118		writeln!(f, "quantization   : {}", self.quantization)?;
119		writeln!(f, "transfer       : {}", self.transfer)?;
120		Ok(())
121	}
122}
123
124impl From<v4l2_pix_format> for Format {
125	fn from(fmt: v4l2_pix_format) -> Self {
126		Self {
127			width: fmt.width,
128			height: fmt.height,
129			fourcc: FourCC::from(fmt.pixelformat),
130			field_order: FieldOrder::try_from(fmt.field).expect("Invalid field order"),
131			stride: fmt.bytesperline,
132			size: fmt.sizeimage,
133			flags: Flags::from(fmt.flags),
134			colorspace: Colorspace::try_from(fmt.colorspace).expect("Invalid colorspace"),
135			quantization: Quantization::try_from(fmt.quantization).expect("Invalid quantization"),
136			transfer: TransferFunction::try_from(fmt.xfer_func).expect("Invalid transfer function"),
137		}
138	}
139}
140
141impl From<Format> for v4l2_pix_format {
142	fn from(format: Format) -> Self {
143		Self {
144			width: format.width,
145			height: format.height,
146			pixelformat: format.fourcc.into(),
147			field: format.field_order as u32,
148			bytesperline: format.stride,
149			sizeimage: format.size,
150			colorspace: format.colorspace as u32,
151			flags: format.flags.into(),
152			quantization: format.quantization as u32,
153			xfer_func: format.transfer as u32,
154			..unsafe { mem::zeroed() }
155		}
156	}
157}