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

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