working v4l2 in rust, swapping bytes

This commit is contained in:
devdesk 2024-02-17 17:21:01 +02:00
parent e74fa87103
commit 6bcc541c86
3 changed files with 13 additions and 4 deletions

View File

@ -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 }

View File

@ -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;
}

View File

@ -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