37#include <linux/videodev2.h>
46#define CLEAR(x) memset(&(x), 0, sizeof (x))
83 printf(
"[v4l2-capture] Select timeout on %s\n",
dev->name);
94 printf(
"[v4l2-capture] Dequeue of buffer failed for %s.\n",
dev->name);
101 dev->buffers[buf.index].timestamp = buf.timestamp;
102 dev->buffers[buf.index].pprz_timestamp =
now_ts;
108 dev->buffers_deq_idx = buf.index;
151 printf(
"[v4l2] Could not get subdevice data format settings of %s\n", subdev_name);
159 sfmt.format.width = size.
w;
160 sfmt.format.height = size.
h;
161 sfmt.format.code = code;
163 sfmt.format.colorspace = 1;
166 printf(
"[v4l2] Could not set subdevice data format settings of %s\n", subdev_name);
222 printf(
"[v4l2] %s isn't capable of streaming (TODO: support reading)\n",
device_name);
244 crp.c.width = crop.
w;
245 crp.c.height = crop.
h;
248 if(crop.
x != 0 || crop.
y != 0 || crop.
w !=
size.w || crop.
h !=
size.h) {
258 fmt.fmt.pix.width =
size.w;
259 fmt.fmt.pix.height =
size.h;
271 req.count = buffers_cnt;
282 if (buffers ==
NULL) {
283 printf(
"[v4l2] Not enough memory for %s to initialize %d MMAP buffers\n",
device_name, req.count);
289 for (i = 0; i < req.count; ++i) {
305 buffers[i].
length = buf.length;
308 printf(
"[v4l2] Mapping buffer %d with length %d from %s failed\n", i, buf.length,
device_name);
315 printf(
"[v4l2] Physical memory %d is not contiguous with length %d from %s\n", i, buf.length,
device_name);
331 dev->buffers_cnt = req.count;
426 buf.index =
img->buf_idx;
428 printf(
"[v4l2] Could not enqueue %d for %s\n",
img->buf_idx,
dev->name);
446 printf(
"[v4l2] There is already a capturing thread running for %s\n",
dev->name);
452 for (i = 0; i <
dev->buffers_cnt; ++i) {
460 printf(
"[v4l2] Could not enqueue buffer %d during start capture for %s\n", i,
dev->name);
475 printf(
"[v4l2] Could not start capturing thread for %s (return code: %d)\n",
dev->name,
rc);
480 printf(
"[v4l2] Could not stop stream of %s\n",
dev->name);
508 printf(
"[v4l2] Already stopped capture for %s\n",
dev->name);
515 printf(
"[v4l2] Could not stop stream of %s\n",
dev->name);
521 printf(
"[v4l2] Could not cancel thread for %s\n",
dev->name);
545 for (i = 0; i <
dev->buffers_cnt; ++i) {
546 if (
munmap(
dev->buffers[i].buf,
dev->buffers[i].length) < 0) {
547 printf(
"[v4l2] Could not unmap buffer %d for %s\n", i,
dev->name);
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
uint16_t h
height of the cropped area
uint16_t w
Width of the cropped area.
uint16_t x
Start position x (horizontal)
uint16_t y
Start position y (vertical)
@ IMAGE_YUV422
UYVY format (uint16 per pixel)
static const struct usb_device_descriptor dev
Architecture independent timing functions.
static void * v4l2_capture_thread(void *data)
The main capturing thread This thread handles the queue and dequeue of buffers, to make sure only the...
bool v4l2_init_subdev(char *subdev_name, uint8_t pad, uint16_t code, struct img_size_t size)
Initialize a V4L2 subdevice.
void v4l2_close(struct v4l2_device *dev)
Close the V4L2 device (Thread safe) This needs to be preformed to clean up all the buffers and close ...
bool v4l2_start_capture(struct v4l2_device *dev)
Start capturing images in streaming mode (Thread safe)
void v4l2_image_get(struct v4l2_device *dev, struct image_t *img)
Get the latest image buffer and lock it (Thread safe, BLOCKING) This functions blocks until image acc...
void v4l2_image_free(struct v4l2_device *dev, struct image_t *img)
Free the image and enqueue the buffer (Thread safe) This must be done after processing the image,...
bool v4l2_stop_capture(struct v4l2_device *dev)
Stop capturing of the image stream (Thread safe) This function is blocking until capturing thread is ...
struct v4l2_device * v4l2_init(char *device_name, struct img_size_t size, struct crop_t crop, uint8_t buffers_cnt, uint32_t _pixelformat)
Initialize a V4L2(Video for Linux 2) device.
bool v4l2_image_get_nonblock(struct v4l2_device *dev, struct image_t *img)
Get the latest image and lock it (Thread safe, NON BLOCKING) This function returns NULL if it can't g...
Capture images from a V4L2 device (Video for Linux 2)
void * buf
Pointer to the memory mapped buffer.
struct v4l2_img_buf * buffers
The memory mapped image buffers.
uint8_t buffers_cnt
The number of image buffers.
#define V4L2_IMG_NONE
There currently no image available.
uint32_t physp
Physical address pointer.
size_t length
The size of the buffer.
int check_contiguity(unsigned long vaddr, pid_t pid, struct physmem *pmem, size_t size)
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.