From 6bcc541c869c4e4280375a77a87e7e4dba9f0d5a Mon Sep 17 00:00:00 2001 From: devdesk Date: Sat, 17 Feb 2024 17:21:01 +0200 Subject: [PATCH] working v4l2 in rust, swapping bytes --- Cargo.toml | 2 +- examples/live.rs | 13 +++++++++++-- run_live.sh | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0556052..7dcd100 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,4 +22,4 @@ pcap-parser = { version = "0.14.1", features = ["data"] } png = "0.17.10" pyo3 = { version = "0.20.0", "features" = ["extension-module"] } serde = { version = "1.0.193", features = ["derive", "serde_derive", "alloc"] } -v4l = "0.14.0" +v4l = { version = "0.14.0", features = ["v4l2"], default-features = false } diff --git a/examples/live.rs b/examples/live.rs index 6f6e311..bd711b1 100644 --- a/examples/live.rs +++ b/examples/live.rs @@ -30,7 +30,9 @@ fn main() -> anyhow::Result<()> { const HEIGHT: usize = 384; let fourcc_repr = [ b'Y', // | 0b10000000 - b'1', b'6', b' ', + b'1', b'6', + b' ', // Note: not using b' ' | 0x80, (V4L2_PIX_FMT_Y16_BE) + // because VID_S_FMT ioctl returns EINVAL, so just swap the bytes here ]; let fourcc = v4l::format::FourCC { repr: fourcc_repr }; let mut out = v4l::Device::with_path(output)?; @@ -67,7 +69,14 @@ fn main() -> anyhow::Result<()> { || (data.len() + len > FRAME_LEN) { if len == FRAME_LEN { - out.write_all(&frame[..])?; + // swap the bytes, we are using LE, not BE, 16 bit grayscale + // possibly limitation of current v4l2loopback or v4l rust wrapper or libv4l2 + let mut swapped = [0u8; FRAME_LEN]; + for i in 0..FRAME_LEN / 2 { + swapped[i * 2] = frame[i * 2 + 1]; + swapped[i * 2 + 1] = frame[i * 2]; + } + out.write_all(&swapped[..])?; } len = 0; } diff --git a/run_live.sh b/run_live.sh index 2008df6..5f6fe36 100755 --- a/run_live.sh +++ b/run_live.sh @@ -9,5 +9,5 @@ cargo build --release --example live TARGET=./target/release/examples/live # setcap does not work yet (EPERM on socket AF_PACKET) # sudo setcap cap_net_raw,cap_net_admin=eip $TARGET -#strace -f -o live.strace $TARGET /dev/video0 +#sudo strace -f -o live.strace $TARGET /dev/video0 sudo $TARGET /dev/video0