1 /* V4L2 video picture grabber
2    Copyright (C) 2009 Mauro Carvalho Chehab <mchehab@kernel.org>
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12  */
13 
14 #include <config.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <fcntl.h>
19 #include <errno.h>
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
22 #include <sys/time.h>
23 #include <sys/mman.h>
24 #include <linux/videodev2.h>
25 #include "../../lib/include/libv4l2.h"
26 #include <argp.h>
27 #include <pthread.h>
28 
29 #define CLEAR_P(x,s) memset((x), 0, s)
30 #define CLEAR(x) CLEAR_P(&(x), sizeof(x))
31 
32 static int libv4l = 1;
33 static int ppm_output = 1;
34 
35 struct buffer {
36 	void   *start;
37 	size_t length;
38 };
39 
xioctl(int fh,unsigned long int request,void * arg)40 static void xioctl(int fh, unsigned long int request, void *arg)
41 {
42 	int r;
43 
44 	do {
45 		if (libv4l)
46 			r = v4l2_ioctl(fh, request, arg);
47 		else
48 			r = ioctl(fh, request, arg);
49 
50 	} while (r == -1 && ((errno == EINTR) || (errno == EAGAIN)));
51 
52 	if (r == -1) {
53 		fprintf(stderr, "%s(%lu): error %d, %s\n", __func__,
54 			_IOC_NR(request), errno, strerror(errno));
55 		exit(EXIT_FAILURE);
56 	}
57 }
58 
59 /* Used by the multi thread capture version */
60 struct buffer_queue {
61 	struct v4l2_buffer *buffers;
62 	int buffers_size;
63 
64 	int read_pos;
65 	int write_pos;
66 	int n_frames;
67 
68 	int fd;
69 
70 	pthread_mutex_t mutex;
71 	pthread_cond_t buffer_cond;
72 };
73 
74 /* Gets a buffer and puts it in the buffers list at write position, then
75  * notifies the consumer that a new buffer is ready to be used */
produce_buffer(void * p)76 static void *produce_buffer (void * p)
77 {
78 	struct buffer_queue 		*bq;
79 	fd_set				fds;
80 	struct timeval			tv;
81 	int				i;
82 	struct v4l2_buffer		*buf;
83 	int				r;
84 
85 	bq = p;
86 
87 	for (i = 0; i < bq->n_frames; i++) {
88 		printf ("Prod: %d\n", i);
89 		buf = &bq->buffers[bq->write_pos % bq->buffers_size];
90 		do {
91 			FD_ZERO(&fds);
92 			FD_SET(bq->fd, &fds);
93 
94 			/* Timeout. */
95 			tv.tv_sec = 2;
96 			tv.tv_usec = 0;
97 
98 			r = select(bq->fd + 1, &fds, NULL, NULL, &tv);
99 		} while ((r == -1 && (errno == EINTR)));
100 		if (r == -1) {
101 			perror("select");
102 			pthread_exit (NULL);
103 			return NULL;
104 		}
105 
106 		CLEAR_P(buf, sizeof(struct v4l2_buffer));
107 		buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
108 		buf->memory = V4L2_MEMORY_MMAP;
109 		xioctl(bq->fd, VIDIOC_DQBUF, buf);
110 
111 		pthread_mutex_lock (&bq->mutex);
112 		bq->write_pos++;
113 		printf ("Prod: %d (done)\n", i);
114 		pthread_cond_signal (&bq->buffer_cond);
115 		pthread_mutex_unlock (&bq->mutex);
116 
117 	}
118 
119 	pthread_exit (NULL);
120 }
121 
122 /* will create a separate thread that will produce the buffers and put
123  * into a circular array while this same thread will get the buffers from
124  * this array and 'render' them */
capture_threads(int fd,struct buffer * buffers,int bufpool_size,struct v4l2_format fmt,int n_frames,char * out_dir,int sleep_ms)125 static int capture_threads (int fd, struct buffer *buffers, int bufpool_size,
126 			struct v4l2_format fmt, int n_frames,
127 			char *out_dir, int sleep_ms)
128 {
129 	struct v4l2_buffer		buf;
130 	unsigned int			i;
131 	struct buffer_queue		buf_queue;
132 	pthread_t			producer;
133 	char				out_name[25 + strlen(out_dir)];
134 	FILE				*fout;
135 	struct timespec			sleeptime;
136 
137 	if (sleep_ms) {
138 		sleeptime.tv_sec = sleep_ms / 1000;
139 		sleeptime.tv_nsec = (sleep_ms % 1000) * 1000000;
140 	}
141 
142 	buf_queue.buffers_size = bufpool_size * 2;
143 	buf_queue.buffers = calloc(buf_queue.buffers_size,
144 				   sizeof(struct v4l2_buffer));
145 	buf_queue.fd = fd;
146 	buf_queue.read_pos = 0;
147 	buf_queue.write_pos = 0;
148 	buf_queue.n_frames = n_frames;
149 	pthread_mutex_init (&buf_queue.mutex, NULL);
150 	pthread_cond_init (&buf_queue.buffer_cond, NULL);
151 
152 	pthread_create (&producer, NULL, produce_buffer, &buf_queue);
153 
154 	for (i = 0; i < n_frames; i++) {
155 		printf ("Read: %d\n", i);
156 
157 		/* wait for a buffer to be available in the queue */
158 		pthread_mutex_lock (&buf_queue.mutex);
159 		while (buf_queue.read_pos == buf_queue.write_pos) {
160 			pthread_cond_wait (&buf_queue.buffer_cond,
161 					   &buf_queue.mutex);
162 		}
163 		pthread_mutex_unlock (&buf_queue.mutex);
164 
165 		if (sleep_ms)
166 			nanosleep (&sleeptime, NULL);
167 
168 		if (ppm_output)
169 			sprintf(out_name, "%s/out%03d.ppm", out_dir, i);
170 		else
171 			sprintf(out_name, "%s/out%03d.raw", out_dir, i);
172 
173 		fout = fopen(out_name, "w");
174 		if (!fout) {
175 			perror("Cannot open image");
176 			exit(EXIT_FAILURE);
177 		}
178 		if (ppm_output)
179 			fprintf(fout, "P6\n%d %d 255\n",
180 				fmt.fmt.pix.width, fmt.fmt.pix.height);
181 
182 		buf = buf_queue.buffers[buf_queue.read_pos %
183 					buf_queue.buffers_size];
184 		fwrite(buffers[buf.index].start, buf.bytesused, 1, fout);
185 		fclose(fout);
186 
187 		xioctl(fd, VIDIOC_QBUF, &buf);
188 
189 		pthread_mutex_lock (&buf_queue.mutex);
190 		buf_queue.read_pos++;
191 		printf ("Read: %d (done)\n", i);
192 		pthread_cond_signal (&buf_queue.buffer_cond);
193 		pthread_mutex_unlock (&buf_queue.mutex);
194 	}
195 
196 	pthread_mutex_destroy (&buf_queue.mutex);
197 	pthread_cond_destroy (&buf_queue.buffer_cond);
198 	free (buf_queue.buffers);
199 	return 0;
200 }
201 
capture_loop(int fd,struct buffer * buffers,struct v4l2_format fmt,int n_frames,char * out_dir)202 static int capture_loop (int fd, struct buffer *buffers, struct v4l2_format fmt,
203 			int n_frames, char *out_dir)
204 {
205 	struct v4l2_buffer		buf;
206 	unsigned int			i;
207 	struct timeval			tv;
208 	int				r;
209 	fd_set				fds;
210 	FILE				*fout;
211 	char				out_name[25 + strlen(out_dir)];
212 
213 	for (i = 0; i < n_frames; i++) {
214 		do {
215 			FD_ZERO(&fds);
216 			FD_SET(fd, &fds);
217 
218 			/* Timeout. */
219 			tv.tv_sec = 2;
220 			tv.tv_usec = 0;
221 
222 			r = select(fd + 1, &fds, NULL, NULL, &tv);
223 		} while ((r == -1 && (errno == EINTR)));
224 		if (r == -1) {
225 			perror("select");
226 			return errno;
227 		}
228 
229 		CLEAR(buf);
230 		buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
231 		buf.memory = V4L2_MEMORY_MMAP;
232 		xioctl(fd, VIDIOC_DQBUF, &buf);
233 
234 		if (ppm_output)
235 			sprintf(out_name, "%s/out%03d.ppm", out_dir, i);
236 		else
237 			sprintf(out_name, "%s/out%03d.raw", out_dir, i);
238 
239 		fout = fopen(out_name, "w");
240 		if (!fout) {
241 			perror("Cannot open image");
242 			exit(EXIT_FAILURE);
243 		}
244 		if (ppm_output)
245 			fprintf(fout, "P6\n%d %d 255\n",
246 				fmt.fmt.pix.width, fmt.fmt.pix.height);
247 
248 		fwrite(buffers[buf.index].start, buf.bytesused, 1, fout);
249 		fclose(fout);
250 
251 		xioctl(fd, VIDIOC_QBUF, &buf);
252 	}
253 	return 0;
254 }
255 
capture(char * dev_name,int x_res,int y_res,uint32_t fourcc,int n_frames,char * out_dir,int block,int threads,int sleep_ms)256 static int capture(char *dev_name, int x_res, int y_res, uint32_t fourcc,
257 		   int n_frames, char *out_dir, int block, int threads,
258 		   int sleep_ms)
259 {
260 	struct v4l2_format		fmt;
261 	struct v4l2_buffer		buf;
262 	struct v4l2_requestbuffers	req;
263 	enum v4l2_buf_type		type;
264 	int				fd = -1;
265 	unsigned int			i, n_buffers;
266 	struct buffer			*buffers;
267 
268 	if (libv4l) {
269 		if (block)
270 			fd = v4l2_open(dev_name, O_RDWR, 0);
271 		else
272 			fd = open(dev_name, O_RDWR | O_NONBLOCK, 0);
273 	} else {
274 		if (block)
275 			fd = open(dev_name, O_RDWR, 0);
276 		else
277 			fd = open(dev_name, O_RDWR | O_NONBLOCK, 0);
278 	}
279 	if (fd < 0) {
280 		perror("Cannot open device");
281 		exit(EXIT_FAILURE);
282 	}
283 
284 	CLEAR(fmt);
285 	fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
286 	fmt.fmt.pix.width       = x_res;
287 	fmt.fmt.pix.height      = y_res;
288 	fmt.fmt.pix.pixelformat = fourcc;
289 	fmt.fmt.pix.field       = V4L2_FIELD_INTERLACED;
290 	xioctl(fd, VIDIOC_S_FMT, &fmt);
291 
292 	if (fmt.fmt.pix.pixelformat != V4L2_PIX_FMT_RGB24) {
293 		if (libv4l) {
294 			printf("Libv4l didn't accept RGB24 format. Can't proceed.\n");
295 			exit(EXIT_FAILURE);
296 		} else {
297 			printf("File output won't be in PPM format.\n");
298 			ppm_output = 0;
299 		}
300 	}
301 	if ((fmt.fmt.pix.width != x_res) || (fmt.fmt.pix.height != y_res))
302 		printf("Warning: driver is sending image at %dx%d\n",
303 			fmt.fmt.pix.width, fmt.fmt.pix.height);
304 
305 	CLEAR(req);
306 	req.count = 2;
307 	req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
308 	req.memory = V4L2_MEMORY_MMAP;
309 	xioctl(fd, VIDIOC_REQBUFS, &req);
310 
311 	buffers = calloc(req.count, sizeof(*buffers));
312 	for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
313 		CLEAR(buf);
314 
315 		buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;
316 		buf.memory      = V4L2_MEMORY_MMAP;
317 		buf.index       = n_buffers;
318 
319 		xioctl(fd, VIDIOC_QUERYBUF, &buf);
320 
321 		buffers[n_buffers].length = buf.length;
322 
323 		if (libv4l)
324 			buffers[n_buffers].start = v4l2_mmap(NULL, buf.length,
325 					    PROT_READ | PROT_WRITE, MAP_SHARED,
326 					    fd, buf.m.offset);
327 		else
328 			buffers[n_buffers].start = mmap(NULL, buf.length,
329 					    PROT_READ | PROT_WRITE, MAP_SHARED,
330 					    fd, buf.m.offset);
331 
332 		if (MAP_FAILED == buffers[n_buffers].start) {
333 			perror("mmap");
334 			exit(EXIT_FAILURE);
335 		}
336 	}
337 
338 	for (i = 0; i < n_buffers; ++i) {
339 		CLEAR(buf);
340 		buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
341 		buf.memory = V4L2_MEMORY_MMAP;
342 		buf.index = i;
343 		xioctl(fd, VIDIOC_QBUF, &buf);
344 	}
345 	type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
346 
347 	xioctl(fd, VIDIOC_STREAMON, &type);
348 	if (threads)
349 		capture_threads(fd, buffers, 2, fmt, n_frames, out_dir,
350 				sleep_ms);
351 	else
352 		capture_loop(fd, buffers, fmt, n_frames, out_dir);
353 
354 	type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
355 	xioctl(fd, VIDIOC_STREAMOFF, &type);
356 	for (i = 0; i < n_buffers; ++i) {
357 		if (libv4l)
358 			v4l2_munmap(buffers[i].start, buffers[i].length);
359 		else
360 			munmap(buffers[i].start, buffers[i].length);
361 	}
362 	if (libv4l)
363 		v4l2_close(fd);
364 	else
365 		close(fd);
366 
367 	return 0;
368 }
369 
370 /*
371  * Main routine. Basically, reads parameters via argp.h and passes it to the
372  * capture routine
373  */
374 
375 const char *argp_program_version = "V4L2 grabber version " V4L_UTILS_VERSION;
376 const char *argp_program_bug_address = "Mauro Carvalho Chehab <mchehab@kernel.org>";
377 
378 static const char doc[] = "\nCapture images using libv4l, storing them as ppm files\n";
379 
380 static const struct argp_option options[] = {
381 	{"device",	'd',	"DEV",		0,	"video device (default: /dev/video0)", 0},
382 	{"no-libv4l",	'D',	NULL,		0,	"Don't use libv4l", 0},
383 	{"out-dir",	'o',	"OUTDIR",	0,	"output directory (default: current dir)", 0},
384 	{"xres",	'x',	"XRES",		0,	"horizontal resolution", 0},
385 	{"yres",	'y',	"YRES",		0,	"vertical resolution", 0},
386 	{"fourcc",	'f',	"FOURCC",	0,	"Linux fourcc code", 0},
387 	{"n-frames",	'n',	"NFRAMES",	0,	"number of frames to capture", 0},
388 	{"thread-enable", 't',	"THREADS",	0,	"if different threads should capture and save", 0},
389 	{"blockmode-enable", 'b', "BLOCKMODE",	0,	"if blocking mode should be used", 0},
390 	{"sleep-time",	's',	"SLEEP",	0,	"how long should the consumer thread sleep to simulate the processing of a buffer (in ms)"},
391 	{ 0, 0, 0, 0, 0, 0 }
392 };
393 
394 /* Static vars to store the parameters */
395 static char 	*dev_name = "/dev/video0";
396 static char	*out_dir = ".";
397 static int	x_res = 640;
398 static int	y_res = 480;
399 static int	n_frames = 20;
400 static int	threads = 0;
401 static int	block = 0;
402 static int	sleep_ms = 0;
403 static uint32_t fourcc = V4L2_PIX_FMT_RGB24;
404 
parse_opt(int k,char * arg,struct argp_state * state)405 static int parse_opt(int k, char *arg, struct argp_state *state)
406 {
407 	int val, i, len;
408 	char s[4];
409 
410 	switch (k) {
411 	case 'd':
412 		dev_name = arg;
413 		break;
414 	case 'o':
415 		out_dir = arg;
416 		break;
417 	case 'x':
418 		val = atoi(arg);
419 		if (val)
420 			x_res = val;
421 		break;
422 	case 'y':
423 		val = atoi(arg);
424 		if (val)
425 			y_res = val;
426 		break;
427 	case 'f':
428 		len = strlen(arg);
429 		if (len < 1 || len > 4)
430 			return ARGP_ERR_UNKNOWN;
431 		memcpy(s, arg, len);
432 		for (i = len; i < 4; i++)
433 			s[i] = ' ';
434 		fourcc = v4l2_fourcc(s[0], s[1], s[2], s[3]);
435 		break;
436 	case 'D':
437 		libv4l = 0;
438 		break;
439 	case 'n':
440 		val = atoi(arg);
441 		if (val)
442 			n_frames = val;
443 		break;
444 	case 't':
445 		threads = 1;
446 		break;
447 	case 'b':
448 		block = 1;
449 		break;
450 	case 's':
451 		val = atoi(arg);
452 		if (val)
453 			sleep_ms = val;
454 		break;
455 	default:
456 		return ARGP_ERR_UNKNOWN;
457 	}
458 	return 0;
459 }
460 
461 static struct argp argp = {
462 	.options = options,
463 	.parser = parse_opt,
464 	.doc = doc,
465 };
466 
467 
main(int argc,char ** argv)468 int main(int argc, char **argv)
469 {
470 	argp_parse(&argp, argc, argv, 0, 0, 0);
471 
472 	return capture(dev_name, x_res, y_res, fourcc, n_frames,
473 		       out_dir, block, threads, sleep_ms);
474 }
475