Files
lotherk f59563a84a dtchat: build the camera on NetBSD's video(4) (T7.1)
NetBSD's sys/videoio.h speaks the V4L2 API (VIDIOC_*, v4l2_buffer,
v4l2_format, V4L2_PIX_FMT_YUYV), so media_capture.c's backend is shared:
linux/videodev2.h with HAVE_V4L2 on Linux, sys/videoio.h with HAVE_VIDEOIO
on NetBSD. configure checks sys/videoio.h in the NetBSD branch and
linux/videodev2.h otherwise; the guards for the mmap/poll/QBUF code accept
either. A build with neither header still gets the test pattern.

Verified with the cross compiler against build/obj/destdir.amd64:
media_capture.c and test_capture.c compile with -DHAVE_VIDEOIO (exit 0),
and the same sources still compile and run on Linux with -DHAVE_V4L2
(test_capture: v4l2:/dev/video0, all ok) and without either define
(test-pattern, all ok). It has not run on a NetBSD machine - no host is
available - so the release gate still needs that run.
2026-11-17 09:45:53 +00:00

392 lines
9.9 KiB
C

/*
* media_capture.c -- camera frames for dtchat's video (T7.1)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* Two sources behind one call: V4L2 on Linux (linux/videodev2.h) and
* video(4) on NetBSD (sys/videoio.h, which speaks the V4L2 API) -- YUYV,
* memory-mapped buffers, polled; the frame is converted to I420 and scaled
* to the size the call uses when the camera has another one -- and a test
* pattern for a machine without a camera (a moving square on a gradient,
* which also makes the video path testable end to end). A build without
* either header gets the pattern and says so.
*/
#include "dc_internal.h"
#ifdef HAVE_CONFIG_H
#include <cde_config.h> /* HAVE_V4L2 / HAVE_VIDEOIO */
#endif
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#if defined(HAVE_V4L2) || defined(HAVE_VIDEOIO)
#include <sys/ioctl.h>
#include <sys/mman.h>
#ifdef HAVE_V4L2
#include <linux/videodev2.h>
#else
#include <sys/videoio.h>
#endif
#endif
#define V4L2_BUFFERS 4
struct DcCapture {
int fd;
int width; /* what the caller asked for */
int height;
int fps;
unsigned int frame;
char backend[64];
unsigned char *scaled; /* capture size != width/height */
#if defined(HAVE_V4L2) || defined(HAVE_VIDEOIO)
void *map[V4L2_BUFFERS];
size_t maplen[V4L2_BUFFERS];
int cap_w;
int cap_h;
int cap_stride;
#endif
};
/* ------------------------------- test pattern ---------------------------- */
static void
pattern_frame(unsigned char *i420, int w, int h, unsigned int n)
{
int cw = (w + 1) / 2;
int ch = (h + 1) / 2;
int square = h / 6;
int left = (int)((n * 7) % (unsigned)(w - square));
int top = (int)((n * 3) % (unsigned)(h - square));
unsigned char *u = i420 + (size_t)w * h;
unsigned char *v = u + (size_t)cw * ch;
int x, y;
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
int inside = x >= left && x < left + square &&
y >= top && y < top + square;
i420[(size_t)y * w + x] = (unsigned char)(inside ? 235 :
16 + (x * 5 + y * 3 + n * 4) % 220);
}
}
for (y = 0; y < ch; y++) {
for (x = 0; x < cw; x++) {
u[(size_t)y * cw + x] = (unsigned char)(96 +
(x * 3 / 2 + n) % 64);
v[(size_t)y * cw + x] = (unsigned char)(160 -
(y * 3 / 2 + n) % 64);
}
}
}
/* -------------------------------- V4L2 ----------------------------------- */
#if defined(HAVE_V4L2) || defined(HAVE_VIDEOIO)
static int
xioctl(int fd, unsigned long req, void *arg)
{
int rc;
do {
rc = ioctl(fd, req, arg);
} while (rc < 0 && errno == EINTR);
return rc;
}
/* YUYV (Y0 U Y1 V, 4 bytes for two pixels) into I420 */
static void
yuyv_to_i420(const unsigned char *src, int sw, int sh, size_t stride,
unsigned char *dst)
{
int cw = (sw + 1) / 2;
int ch = (sh + 1) / 2;
unsigned char *u = dst + (size_t)sw * sh;
unsigned char *v = u + (size_t)cw * ch;
int x, y;
for (y = 0; y < sh; y++) {
const unsigned char *row = src + (size_t)y * stride;
for (x = 0; x < sw; x += 2) {
dst[(size_t)y * sw + x] = row[x * 2];
if (x + 1 < sw)
dst[(size_t)y * sw + x + 1] = row[x * 2 + 2];
if ((y & 1) == 0) {
u[(size_t)(y / 2) * cw + x / 2] =
row[x * 2 + 1];
v[(size_t)(y / 2) * cw + x / 2] =
row[x * 2 + 3];
}
}
}
}
static int
v4l2_open_camera(DcCapture *cap, const char *device, char *err, size_t errlen)
{
struct v4l2_capability vcap;
struct v4l2_format fmt;
struct v4l2_requestbuffers req;
struct v4l2_streamparm parm;
unsigned int i;
cap->fd = open(device, O_RDWR | O_NONBLOCK | O_CLOEXEC);
if (cap->fd < 0) {
snprintf(err, errlen, "cannot open %s: %s", device,
strerror(errno));
return -1;
}
memset(&vcap, 0, sizeof(vcap));
if (xioctl(cap->fd, VIDIOC_QUERYCAP, &vcap) < 0 ||
!(vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE) ||
!(vcap.capabilities & V4L2_CAP_STREAMING)) {
snprintf(err, errlen, "%s is no streaming camera", device);
goto fail;
}
memset(&fmt, 0, sizeof(fmt));
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = (unsigned)cap->width;
fmt.fmt.pix.height = (unsigned)cap->height;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
fmt.fmt.pix.field = V4L2_FIELD_NONE;
if (xioctl(cap->fd, VIDIOC_S_FMT, &fmt) < 0) {
snprintf(err, errlen, "cannot set the format of %s: %s", device,
strerror(errno));
goto fail;
}
if (fmt.fmt.pix.pixelformat != V4L2_PIX_FMT_YUYV) {
snprintf(err, errlen, "%s does not give YUYV", device);
goto fail;
}
cap->cap_w = (int)fmt.fmt.pix.width;
cap->cap_h = (int)fmt.fmt.pix.height;
cap->cap_stride = (int)fmt.fmt.pix.bytesperline;
if (cap->cap_stride < cap->cap_w * 2)
cap->cap_stride = cap->cap_w * 2;
memset(&parm, 0, sizeof(parm));
parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
parm.parm.capture.timeperframe.numerator = 1;
parm.parm.capture.timeperframe.denominator = (unsigned)cap->fps;
(void)xioctl(cap->fd, VIDIOC_S_PARM, &parm);
memset(&req, 0, sizeof(req));
req.count = V4L2_BUFFERS;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
if (xioctl(cap->fd, VIDIOC_REQBUFS, &req) < 0 || req.count < 2) {
snprintf(err, errlen, "cannot get buffers from %s: %s", device,
strerror(errno));
goto fail;
}
for (i = 0; i < req.count && i < V4L2_BUFFERS; i++) {
struct v4l2_buffer buf;
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
if (xioctl(cap->fd, VIDIOC_QUERYBUF, &buf) < 0)
goto fail;
cap->maplen[i] = buf.length;
cap->map[i] = mmap(NULL, buf.length, PROT_READ | PROT_WRITE,
MAP_SHARED, cap->fd, buf.m.offset);
if (cap->map[i] == MAP_FAILED) {
cap->map[i] = NULL;
snprintf(err, errlen, "cannot map a buffer of %s: %s",
device, strerror(errno));
goto fail;
}
}
for (i = 0; i < req.count && i < V4L2_BUFFERS; i++) {
struct v4l2_buffer buf;
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
if (xioctl(cap->fd, VIDIOC_QBUF, &buf) < 0)
goto fail;
}
{
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (xioctl(cap->fd, VIDIOC_STREAMON, &type) < 0) {
snprintf(err, errlen, "cannot start %s: %s", device,
strerror(errno));
goto fail;
}
}
snprintf(cap->backend, sizeof(cap->backend), "v4l2:%s", device);
return 0;
fail:
if (cap->fd >= 0) {
(void)close(cap->fd);
cap->fd = -1;
}
return -1;
}
#endif /* HAVE_V4L2 || HAVE_VIDEOIO */
/* -------------------------------- API ------------------------------------ */
DcCapture *
dc_capture_open(int width, int height, int fps, const char *device, char *err,
size_t errlen)
{
DcCapture *cap;
if (err != NULL && errlen > 0)
err[0] = '\0';
if (width < 16 || height < 16 || width > 1920 || height > 1080 ||
(width & 1) != 0 || (height & 1) != 0) {
if (err != NULL && errlen > 0)
snprintf(err, errlen, "bad capture size");
return NULL;
}
cap = calloc(1, sizeof(*cap));
if (cap == NULL)
return NULL;
cap->fd = -1;
cap->width = width;
cap->height = height;
cap->fps = fps > 0 ? fps : DC_VIDEO_FPS;
snprintf(cap->backend, sizeof(cap->backend), "test-pattern");
#if defined(HAVE_V4L2) || defined(HAVE_VIDEOIO)
if (device != NULL && *device != '\0' &&
strcmp(device, "pattern") != 0) {
if (v4l2_open_camera(cap, device, err, errlen) == 0) {
if (cap->cap_w != width || cap->cap_h != height) {
cap->scaled = malloc((size_t)width * height *
3 / 2);
if (cap->scaled == NULL) {
dc_capture_close(cap);
return NULL;
}
}
return cap;
}
if (strcmp(device, "auto") != 0) {
free(cap);
return NULL;
}
}
if (device != NULL && strcmp(device, "auto") == 0) {
int i;
for (i = 0; i < 4; i++) {
char path[32];
snprintf(path, sizeof(path), "/dev/video%d", i);
if (v4l2_open_camera(cap, path, err, errlen) == 0) {
if (cap->cap_w != width || cap->cap_h != height) {
cap->scaled = malloc((size_t)width *
height * 3 / 2);
if (cap->scaled == NULL) {
dc_capture_close(cap);
return NULL;
}
}
return cap;
}
}
}
#else
(void)device;
#endif
/* no camera: the pattern still shows whether the path works */
return cap;
}
int
dc_capture_frame(DcCapture *cap, unsigned char *i420, int timeout_ms)
{
if (cap == NULL || i420 == NULL)
return -1;
#if defined(HAVE_V4L2) || defined(HAVE_VIDEOIO)
if (cap->fd >= 0) {
struct pollfd p;
struct v4l2_buffer buf;
int rc;
p.fd = cap->fd;
p.events = POLLIN;
p.revents = 0;
rc = poll(&p, 1, timeout_ms);
if (rc == 0)
return 0;
if (rc < 0) {
if (errno == EINTR)
return 0;
return -1;
}
if (p.revents & (POLLERR | POLLHUP | POLLNVAL))
return -1;
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
if (xioctl(cap->fd, VIDIOC_DQBUF, &buf) < 0) {
if (errno == EAGAIN)
return 0;
return -1;
}
if (buf.index >= V4L2_BUFFERS || cap->map[buf.index] == NULL) {
(void)xioctl(cap->fd, VIDIOC_QBUF, &buf);
return -1;
}
yuyv_to_i420(cap->map[buf.index], cap->cap_w, cap->cap_h,
(size_t)cap->cap_stride, cap->scaled != NULL ?
cap->scaled : i420);
if (cap->scaled != NULL)
dc_i420_scale(cap->scaled, cap->cap_w, cap->cap_h,
i420, cap->width, cap->height);
cap->frame++;
(void)xioctl(cap->fd, VIDIOC_QBUF, &buf);
return 1;
}
#endif
pattern_frame(i420, cap->width, cap->height, cap->frame++);
(void)timeout_ms;
return 1;
}
const char *
dc_capture_backend(const DcCapture *cap)
{
return cap != NULL ? cap->backend : "none";
}
void
dc_capture_close(DcCapture *cap)
{
if (cap == NULL)
return;
#if defined(HAVE_V4L2) || defined(HAVE_VIDEOIO)
if (cap->fd >= 0) {
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
unsigned int i;
(void)xioctl(cap->fd, VIDIOC_STREAMOFF, &type);
for (i = 0; i < V4L2_BUFFERS; i++) {
if (cap->map[i] != NULL)
(void)munmap(cap->map[i], cap->maplen[i]);
}
(void)close(cap->fd);
}
#endif
free(cap->scaled);
free(cap);
}