Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support mplane capture and mmap #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
**/target
**/Cargo.lock
/.idea/
60 changes: 60 additions & 0 deletions examples/stream_capture_mplane_mmap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::io;
use std::time::Instant;

use v4l::buffer::Type;
use v4l::io::traits::CaptureStream;
use v4l::prelude::*;
use v4l::video::{Capture, CaptureMplane};

fn main() -> io::Result<()> {
let path = "/dev/video0";
println!("Using device: {}\n", path);

// Capture 4 frames by default
let count = 4;

// Allocate 4 buffers by default
let buffer_count = 4;

let dev = Device::with_path(path)?;
let format = CaptureMplane::format(&dev)?;
let params = dev.params()?;
println!("Active format:\n{}", format);
println!("Active parameters:\n{}", params);

// Setup a buffer stream and grab a frame, then print its data
let mut stream = MmapStream::with_buffers(&dev, Type::VideoCaptureMplane, buffer_count)?;

// warmup
stream.next()?;

let start = Instant::now();
let mut megabytes_ps: f64 = 0.0;
for i in 0..count {
let t0 = Instant::now();
let (buf, meta) = stream.next()?;
let duration_us = t0.elapsed().as_micros();

let cur = buf.len() as f64 / 1_048_576.0 * 1_000_000.0 / duration_us as f64;
if i == 0 {
megabytes_ps = cur;
} else {
// ignore the first measurement
let prev = megabytes_ps * (i as f64 / (i + 1) as f64);
let now = cur * (1.0 / (i + 1) as f64);
megabytes_ps = prev + now;
}

println!("Buffer");
println!(" sequence : {}", meta.sequence);
println!(" timestamp : {}", meta.timestamp);
println!(" flags : {}", meta.flags);
println!(" length : {}", buf.len());
}

println!();
println!("FPS: {}", count as f64 / start.elapsed().as_secs_f64());
println!("MB/s: {}", megabytes_ps);

Ok(())
}
147 changes: 147 additions & 0 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,22 @@ impl From<u32> for Flags {
}
}

impl From<u8> for Flags {
fn from(flags: u8) -> Self {
Self::from_bits_retain(flags as u32)
}
}

impl From<Flags> for u32 {
fn from(flags: Flags) -> Self {
flags.bits()
}
}

impl From<Flags> for u8 {
fn from(flags: Flags) -> Self { flags.bits() as u8 }
}

impl fmt::Display for Flags {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self, f)
Expand Down Expand Up @@ -153,3 +163,140 @@ impl From<Format> for v4l2_pix_format {
}
}
}

/// streaming format (multi-planar)
#[derive(Debug, Copy, Clone)]
pub struct FormatMplane {
/// width in pixels
pub width: u32,
/// height in pixels
pub height: u32,
/// pixelformat code
pub fourcc: FourCC,
/// field order for interlacing
pub field_order: FieldOrder,

pub plane_fmt: [FormatPlanePixItem; 8usize],

pub num_planes: u8,

/// flags set by the application or driver
pub flags: Flags,

/// supplements the pixelformat (fourcc) information
pub colorspace: Colorspace,
/// the way colors are mapped
pub quantization: Quantization,
/// the transfer function for the colorspace
pub transfer: TransferFunction,
}

impl FormatMplane {
pub const fn new(width: u32, height: u32, fourcc: FourCC) -> Self {
FormatMplane {
width,
height,
fourcc,
field_order: FieldOrder::Any,
plane_fmt: [FormatPlanePixItem { stride: 0, size: 0 }; 8usize],
num_planes: 0,
flags: Flags::empty(),
colorspace: Colorspace::Default,
quantization: Quantization::Default,
transfer: TransferFunction::Default,
}
}
}

impl fmt::Display for FormatMplane {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "width : {}", self.width)?;
writeln!(f, "height : {}", self.height)?;
writeln!(f, "fourcc : {}", self.fourcc)?;
writeln!(f, "field_order : {}", self.field_order)?;
for i in 0..self.num_planes as usize {
writeln!(f, "plane_fmt[{}]", i)?;
write!(f, "{}", self.plane_fmt[i])?;
}
writeln!(f, "flags : {}", self.flags)?;
writeln!(f, "colorspace : {}", self.colorspace)?;
writeln!(f, "quantization : {}", self.quantization)?;
writeln!(f, "transfer : {}", self.transfer)?;
Ok(())
}
}

impl From<v4l2_pix_format_mplane> for FormatMplane {
fn from(fmt: v4l2_pix_format_mplane) -> Self {
let mut plane_fmt = [FormatPlanePixItem {
stride: fmt.plane_fmt[0].bytesperline,
size: fmt.plane_fmt[0].sizeimage,
}; 8usize];
for i in 1..fmt.num_planes as usize {
plane_fmt[i] = FormatPlanePixItem {
stride: fmt.plane_fmt[i].bytesperline,
size: fmt.plane_fmt[i].sizeimage,
};
}

FormatMplane {
width: fmt.width,
height: fmt.height,
fourcc: FourCC::from(fmt.pixelformat),
field_order: FieldOrder::try_from(fmt.field).expect("Invalid field order"),
flags: Flags::from(fmt.flags),
colorspace: Colorspace::try_from(fmt.colorspace).expect("Invalid colorspace"),
plane_fmt,
num_planes: fmt.num_planes,
quantization: Quantization::try_from(fmt.quantization).expect("Invalid quantization"),
transfer: TransferFunction::try_from(fmt.xfer_func).expect("Invalid transfer function"),
}
}
}
impl From<FormatMplane> for v4l2_pix_format_mplane {
fn from(format: FormatMplane) -> Self {
let mut plane_fmt = [v4l2_plane_pix_format {
bytesperline: format.plane_fmt[0].stride,
sizeimage: format.plane_fmt[0].size,
reserved: [0; 6usize],
}; 8usize];
for i in 1..format.num_planes as usize {
plane_fmt[i] = v4l2_plane_pix_format {
bytesperline: format.plane_fmt[i].stride,
sizeimage: format.plane_fmt[i].size,
reserved: [0; 6usize],
};
}

v4l2_pix_format_mplane {
width: format.width,
height: format.height,
pixelformat: format.fourcc.into(),
field: format.field_order as u32,
colorspace: format.colorspace as u32,
plane_fmt,
num_planes: format.plane_fmt.len() as u8,
flags: format.flags.into(),
quantization: format.quantization as u8,
xfer_func: format.transfer as u8,
..unsafe { std::mem::zeroed() }
}
}
}


#[derive(Debug, Copy, Clone)]
pub struct FormatPlanePixItem {
/// bytes per line
pub stride: u32,
/// maximum number of bytes required to store an image
pub size: u32,
}

impl fmt::Display for FormatPlanePixItem {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, " stride : {}", self.stride)?;
writeln!(f, " size : {}", self.size)?;
Ok(())
}
}
28 changes: 18 additions & 10 deletions src/format/quantization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,23 @@ impl fmt::Display for Quantization {
}
}

impl TryFrom<u32> for Quantization {
type Error = ();
macro_rules! imp_try_from {
($($t:ty),*) => {
$(
impl TryFrom<$t> for Quantization {
type Error = ();

fn try_from(code: u32) -> Result<Self, Self::Error> {
match code {
0 => Ok(Self::Default),
1 => Ok(Self::FullRange),
2 => Ok(Self::LimitedRange),
_ => Err(()),
}
}
fn try_from(code: $t) -> Result<Self, Self::Error> {
match code {
0 => Ok(Self::Default),
1 => Ok(Self::FullRange),
2 => Ok(Self::LimitedRange),
_ => Err(()),
}
}
}
)*
};
}

imp_try_from!(u32, u8);
39 changes: 24 additions & 15 deletions src/format/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,29 @@ impl fmt::Display for TransferFunction {
}
}

impl TryFrom<u32> for TransferFunction {
type Error = ();
macro_rules! impl_try_from_transfer_function {
($($t:ty),*) => {
$(
impl TryFrom<$t> for TransferFunction {
type Error = ();

fn try_from(colorspace_code: u32) -> Result<Self, Self::Error> {
match colorspace_code {
0 => Ok(Self::Default),
1 => Ok(Self::Rec709),
2 => Ok(Self::SRGB),
3 => Ok(Self::OPRGB),
4 => Ok(Self::SMPTE240M),
5 => Ok(Self::None),
6 => Ok(Self::DCIP3),
7 => Ok(Self::SMPTE2084),
_ => Err(()),
}
}
fn try_from(colorspace_code: $t) -> Result<Self, Self::Error> {
match colorspace_code {
0 => Ok(Self::Default),
1 => Ok(Self::Rec709),
2 => Ok(Self::SRGB),
3 => Ok(Self::OPRGB),
4 => Ok(Self::SMPTE240M),
5 => Ok(Self::None),
6 => Ok(Self::DCIP3),
7 => Ok(Self::SMPTE2084),
_ => Err(()),
}
}
}
)*
};
}

impl_try_from_transfer_function!(u8, u32);

Loading