Commit 08bfb727 authored by André Almeida's avatar André Almeida

Initial commit

parent abd2c584
all:
gcc grab.c -o grab
all_debug:
gcc grab.c -g -D DBG_PRINT -o grab
run: all
./grab
run_debug: all_debug
./grab
run_trace:
strace -o grab.trace ./grab
show_image:
gst-launch-1.0 filesrc location=out000.yuv ! rawvideoparse use-sink-caps=false width=640 height=360 format=yv12 ! imagefreeze ! videoconvert ! ximagesink
clean:
rm grab out000.yuv grab.trace
# v4l-grab
This is an example on how to use the V4L2 multi-planar API to get frames from a device
\ No newline at end of file
This code grabs a frame from vivid driver using V4L2 API for studying proposes.
Before run, make sure you are using vivid with the multi-planar API set, using
this kernel parameter: `vivid.multiplanar=2`.
After the run, the program will create a `out000.yuv` file with color bars. The
V4L2 pixel format is YVU420M
The Makefile contains compiling, run and testing instructions:
- all: normal compilation
- all_debug: compiles with debug prints
- run: normal compile and run
- run_debug: compile and run with debug prints
For bellow options, make sure you have run at least one of the command above:
- run_trace: run and generates a strace output file (grab.trace)
- show_image: uses gstreamer to show the frame in `out000.yuv`
- clean: remove the strace file, image and the binary
/* V4L2 video picture grabber
Copyright (C) 2009 Mauro Carvalho Chehab <mchehab@kernel.org>
Copyright (C) 2018 Andre Almeida <andre.almeida@collabora.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <linux/videodev2.h>
// clear memory
#define CLEAR(x) memset(&(x), 0, sizeof(x))
// num of planes of YVU420M
#define NUM_PLANES 3
// plane buffer info
struct buffer {
void *start[NUM_PLANES];
size_t length[NUM_PLANES];
};
// common data about the image
struct context {
__u32 pix_format;
__u32 buf_type;
__u32 mem_type;
__u8 num_planes;
__u8 num_buffers;
__u16 img_height;
__u16 img_width;
__u8 size_image;
};
static void xioctl(int fh, int request, void *arg)
{
int r;
do {
r = ioctl(fh, request, arg);
} while (r == -1 && ((errno == EINTR) || (errno == EAGAIN)));
if (r == -1) {
fprintf(stderr, "error %d, %s\n", errno, strerror(errno));
exit(EXIT_FAILURE);
}
}
int main(int argc, char **argv)
{
/* define context variables for this kind of capture */
struct context ctx;
CLEAR(ctx);
ctx.pix_format = V4L2_PIX_FMT_YVU420M;
ctx.buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
ctx.mem_type = V4L2_MEMORY_MMAP;
ctx.num_planes = NUM_PLANES;
ctx.num_buffers = 5;
ctx.img_height = 360;
ctx.img_width = 640;
ctx.size_image = ctx.img_height * ctx.img_width;
struct v4l2_format fmt;
struct v4l2_buffer buf;
struct v4l2_requestbuffers req;
struct v4l2_plane planes[ctx.num_planes];
struct v4l2_capability caps;
enum v4l2_buf_type type;
fd_set fds;
struct timeval tv;
int r, fd = -1;
unsigned int i, j;
char *dev_name = "/dev/video0";
char out_name[256];
FILE *fout;
struct buffer *buffers;
/* Opening the device */
fd = open(dev_name, O_RDWR | O_NONBLOCK, 0);
if (fd < 0) {
perror("Cannot open device");
exit(EXIT_FAILURE);
}
/* Querying device capabilities */
//xioctl(fd, VIDIOC_QUERYCAP, &caps);
/* Setting image format */
CLEAR(fmt);
fmt.type = ctx.buf_type;
fmt.fmt.pix_mp.width = ctx.img_width;
fmt.fmt.pix_mp.height = ctx.img_height;
fmt.fmt.pix_mp.pixelformat = ctx.pix_format;
fmt.fmt.pix_mp.num_planes = ctx.num_planes;
//fmt.fmt.pix_mp.field = V4L2_FIELD_NONE;
//fmt.fmt.pix_mp.colorspace = V4L2_COLORSPACE_SRGB;
/* plane definitions
fmt.fmt.pix_mp.plane_fmt[0].sizeimage = ctx.size_image;
fmt.fmt.pix_mp.plane_fmt[0].bytesperline = ctx.img_width;
fmt.fmt.pix_mp.plane_fmt[1].sizeimage = ctx.size_image / 2;
fmt.fmt.pix_mp.plane_fmt[1].bytesperline = ctx.img_width / 2;
fmt.fmt.pix_mp.plane_fmt[2].sizeimage = ctx.size_image / 2;
fmt.fmt.pix_mp.plane_fmt[2].bytesperline = ctx.img_width / 2;
*/
xioctl(fd, VIDIOC_S_FMT, &fmt);
/* Comparing fmt accepted by the driver */
if (fmt.fmt.pix.pixelformat != ctx.pix_format) {
printf("Device didn't accept YUV420M format (got %x). Can't proceed.\n",
fmt.fmt.pix.pixelformat);
exit(EXIT_FAILURE);
}
if ((fmt.fmt.pix.width != ctx.img_width) || (fmt.fmt.pix.height != ctx.img_height))
printf("Warning: driver is sending image at %dx%d\n",
fmt.fmt.pix.width, fmt.fmt.pix.height);
/* Requiring buffers */
CLEAR(req);
req.count = ctx.num_buffers;
req.type = ctx.buf_type;
req.memory = ctx.mem_type;
xioctl(fd, VIDIOC_REQBUFS, &req);
if (req.count < ctx.num_buffers){
printf("Warning: couldn't allocate all requested buffers");
ctx.num_buffers = req.count;
}
/* Buffer memory allocation */
buffers = calloc(ctx.num_planes * ctx.num_buffers, sizeof(*buffers));
/* for each buffer, allocate and set the planes */
for (i = 0; i < ctx.num_buffers; i++){
/* query the requested buffers */
CLEAR(buf);
CLEAR(planes);
buf.type = ctx.buf_type;
buf.memory = ctx.mem_type;
buf.index = i;
buf.m.planes = planes;
buf.length = ctx.num_planes;
xioctl(fd, VIDIOC_QUERYBUF, &buf);
/* map planes offsets to buffers pointers */
for(j = 0; j < ctx.num_planes; j++){
#ifdef DBG_PRINT
printf("mem offset[%d][%d]: %p\n", i, j,
buf.m.planes[j].m.mem_offset);
#endif
buffers[i].length[j] = buf.m.planes[j].length;
buffers[i].start[j] = mmap(NULL, buffers[i].length[j],
PROT_READ | PROT_WRITE, MAP_SHARED,
fd, buf.m.planes[j].m.mem_offset);
if (MAP_FAILED == buffers[i].start[j]) {
perror("mmap");
exit(EXIT_FAILURE);
}
}
}
#ifdef DBG_PRINT
for (i = 0; i < ctx.num_buffers; i++)
for (j = 0; j < ctx.num_planes; j++)
printf("buffer[%d][%d](size: %d) start: %p\n",
i, j, buffers[i].length[j], buffers[i].start[j]);
#endif
/* enqueue the buffers */
for (i = 0; i < ctx.num_buffers; i++) {
CLEAR(buf);
CLEAR(planes);
buf.type = ctx.buf_type;
buf.memory = ctx.mem_type;
buf.m.planes = planes;
buf.length = ctx.num_planes;
buf.index = i;
xioctl(fd, VIDIOC_QBUF, &buf);
}
/* start the streaming */
type = ctx.buf_type;
xioctl(fd, VIDIOC_STREAMON, &type);
/* dequeue the buffers and capture the frames info */
// for now it's only show one frame
for (i = 0; i < 1; i++) {
/* timestamp */
do {
FD_ZERO(&fds);
FD_SET(fd, &fds);
/* Timeout. */
tv.tv_sec = 2;
tv.tv_usec = 0;
r = select(fd + 1, &fds, NULL, NULL, &tv);
} while ((r == -1 && (errno = EINTR)));
if (r == -1) {
perror("select");
return errno;
}
/* create the output file */
sprintf(out_name, "out%03d.yuv", i);
fout = fopen(out_name, "w");
if (!fout) {
perror("Cannot open image");
exit(EXIT_FAILURE);
}
/* dequeue buffer */
CLEAR(buf);
CLEAR(planes);
buf.type = ctx.buf_type;
buf.memory = ctx.mem_type;
buf.m.planes = planes;
buf.length = ctx.num_planes;
xioctl(fd, VIDIOC_DQBUF, &buf);
/* write each plane in the file */
for(j = 0; j < ctx.num_planes; j++){
#ifdef DBG_PRINT
printf("buf addres[%d][%d]: %p\n", buf.index, j, buffers[buf.index].start[j]);
printf("mem offset[%d][%d]: %p\n", buf.index, j, buf.m.planes[j].m.mem_offset);
printf("bytesused: %d\n", buf.m.planes[j].bytesused);
#endif
fwrite(buffers[buf.index].start[j],
buf.m.planes[j].bytesused, 1, fout);
}
/* reinsert the buffer on queue */
xioctl(fd, VIDIOC_QBUF, &buf);
fclose(fout);
}
/* stop the streaming and free the memory */
type = ctx.buf_type;
xioctl(fd, VIDIOC_STREAMOFF, &type);
for (i = 0; i < ctx.num_buffers; i++)
for (j = 0; j < ctx.num_planes; j++)
munmap(buffers[i].start[j], buffers[i].length[j]);
close(fd);
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment