summaryrefslogtreecommitdiff
path: root/snapshot.c
blob: 05b9bd7e34fbffccad09c1c2c1f31cb571b9767e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
 * OMAP3 ISP snapshot test application
 *
 * Copyright (C) 2010-2011 Ideas on board SPRL
 *
 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.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; either version 2 of the License, or (at your option)
 * any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#define _BSD_SOURCE
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>

#include <linux/spi/spidev.h>

#include "isp/list.h"
#include "isp/omap3isp.h"
#include "isp/tools.h"
#include "isp/v4l2.h"
#include "isp/v4l2-pool.h"

#include "events.h"

#define MEDIA_DEVICE		"/dev/media0"

#define VIEWFINDER_WIDTH	1024
#define VIEWFINDER_HEIGHT	768
#define SNAPSHOT_WIDTH		2048
#define SNAPSHOT_HEIGHT		1536

static unsigned int frame_count = 0;

static unsigned int snapshot_interval = 20;
static unsigned int snapshot_count = 0;
static struct timespec snapshot_time;
static bool snapshot_save = false;

static struct events events;

/* -----------------------------------------------------------------------------
 * Events
 */

static void __events_watch_fd(int fd, enum omap3_isp_event_type type,
			      void(*callback)(void *), void *priv)
{
	events_watch_fd(&events, fd, type, callback, priv);
}

static void __events_unwatch_fd(int fd)
{
	events_unwatch_fd(&events, fd);
}

/* -------------------------------------------------------------------------- */

static void sigint_handler(int signal __attribute__((__unused__)))
{
	/* Stop the main loop when the user presses CTRL-C. */
	events_stop(&events);
}

static void snapshot_process(struct omap3_isp_device *isp,
			     struct v4l2_video_buffer *buffer)
{
	static struct timespec ts;
	char name[23];
	int ret;
	int fd;

	if (buffer->error) {
		printf("warning: error in dequeued buffer, skipping\n");
		return;
	}

	ts.tv_sec = buffer->timestamp.tv_sec - snapshot_time.tv_sec;
	ts.tv_nsec = (buffer->timestamp.tv_usec * 1000) - snapshot_time.tv_nsec;
	if (ts.tv_nsec < 0) {
		ts.tv_sec--;
		ts.tv_nsec += 1000000000;
	}

	printf("snapshot captured in %lu.%06lus.\n", ts.tv_sec, ts.tv_nsec / 1000);

	if (snapshot_save) {
		sprintf(name, "snapshot-%06u.bin", snapshot_count);
		fd = open(name, O_WRONLY | O_CREAT,
			  S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
		if (fd == -1)
			goto requeue;

		write(fd, buffer->mem, buffer->bytesused);
		close(fd);
	}

requeue:
	/* Requeue the buffer */
	snapshot_count++;

	ret = omap3_isp_snapshot_put_buffer(isp, buffer);
	if (ret < 0) {
		printf("error: unable to requeue buffer: %s (%d)\n",
			strerror(-ret), ret);
		return;
	}
}

static void viewfinder_process(struct omap3_isp_device *isp,
			       struct v4l2_video_buffer *buffer)
{
	int ret;

	if (buffer->error) {
		printf("warning: error in dequeued buffer, skipping\n");
		return;
	}

	frame_count++;
	printf("viewfinder frame %u captured.\n", frame_count);

	if ((frame_count % snapshot_interval) == 0) {
		clock_gettime(CLOCK_MONOTONIC, &snapshot_time);
		omap3_isp_snapshot_capture(isp);
	}

	/* Requeue the buffer */
	ret = omap3_isp_viewfinder_put_buffer(isp, buffer);
	if (ret < 0) {
		printf("error: unable to requeue buffer: %s (%d)\n",
			strerror(-ret), ret);
		return;
	}
}

/* -------------------------------------------------------------------------- */

static const struct {
	const char *name;
	unsigned int fourcc;
} pixel_formats[] = {
	{ "Y8", V4L2_MBUS_FMT_Y8_1X8 },
	{ "Y10", V4L2_MBUS_FMT_Y10_1X10 },
	{ "Y12", V4L2_MBUS_FMT_Y12_1X12 },
	{ "YUYV", V4L2_MBUS_FMT_YUYV8_2X8 },
	{ "UYVY", V4L2_MBUS_FMT_UYVY8_2X8 },
	{ "SBGGR8", V4L2_MBUS_FMT_SBGGR8_1X8 },
	{ "SGBRG8", V4L2_MBUS_FMT_SGBRG8_1X8 },
	{ "SGRBG8", V4L2_MBUS_FMT_SGRBG8_1X8 },
	{ "SRGGB8", V4L2_MBUS_FMT_SRGGB8_1X8 },
	{ "SBGGR10", V4L2_MBUS_FMT_SBGGR10_1X10 },
	{ "SGBRG10", V4L2_MBUS_FMT_SGBRG10_1X10 },
	{ "SGRBG10", V4L2_MBUS_FMT_SGRBG10_1X10 },
	{ "SRGGB10", V4L2_MBUS_FMT_SRGGB10_1X10 },
	{ "SBGGR12", V4L2_MBUS_FMT_SBGGR12_1X12 },
	{ "SGBRG12", V4L2_MBUS_FMT_SGBRG12_1X12 },
	{ "SGRBG12", V4L2_MBUS_FMT_SGRBG12_1X12 },
	{ "SRGGB12", V4L2_MBUS_FMT_SRGGB12_1X12 },
};

static unsigned int parse_format(const char *name)
{
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(pixel_formats); ++i) {
		if (strcasecmp(pixel_formats[i].name, name) == 0)
			return pixel_formats[i].fourcc;
	}

	return 0;
}

static int parse_crop(const char *p, struct v4l2_rect *crop)
{
	char *end;

	if (*p++ != '(')
		return -EINVAL;

	crop->left = strtoul(p, &end, 10);
	if (*end != ',')
		return -EINVAL;

	p = end + 1;
	crop->top = strtoul(p, &end, 10);
	if (*end++ != ')')
		return -EINVAL;
	if (*end != '/')
		return -EINVAL;

	p = end + 1;
	crop->width = strtoul(p, &end, 10);
	if (*end != 'x')
		return -EINVAL;

	p = end + 1;
	crop->height = strtoul(p, &end, 10);
	if (*end != '\0')
		return -EINVAL;

	return 0;
}

static int parse_size(const char *p, struct v4l2_mbus_framefmt *format)
{
	char *end;

	format->width = strtoul(p, &end, 10);
	if (*end != 'x')
		return -EINVAL;

	p = end + 1;
	format->height = strtoul(p, &end, 10);
	if (*end != '\0')
		return -EINVAL;

	return 0;
}

static void usage(const char *argv0)
{
	printf("Usage: %s [options]\n", argv0);
	printf("Supported options:\n");
	printf("-c, --crop (x,y)/wxh	Set the snapshot capture crop\n");
	printf("-f, --format fmt	Snapshot format\n");
	printf("-h, --help		Show this help screen\n");
	printf("-i, --interval n	Capture a snapshot every n frames\n");
	printf("-S, --save		Save snapshots to disk\n");
	printf("-s, --size wxh		Set the snapshot capture size\n");
}

static struct option opts[] = {
	{ "crop", 1, 0, 'c' },
	{ "format", 1, 0, 'f' },
	{ "help", 0, 0, 'h' },
	{ "interval", 1, 0, 'i' },
	{ "save", 0, 0, 'S' },
	{ "size", 1, 0, 's' },
	{ 0, 0, 0, 0 }
};

int main(int argc __attribute__((__unused__)), char *argv[] __attribute__((__unused__)))
{
	struct v4l2_mbus_framefmt view_format;
	struct v4l2_mbus_framefmt snap_format;
	struct v4l2_rect snap_crop;
	struct v4l2_buffers_pool *pool = NULL;
	struct omap3_isp_device *isp = NULL;
	struct omap3_isp_operations ops;
	struct timespec start, end;
	int exit_code = EXIT_FAILURE;
	bool do_crop = false;
	float fps;
	int ret;
	int c;

	memset(&snap_format, 0, sizeof snap_format);
	snap_format.code = V4L2_MBUS_FMT_SGRBG10_1X10;
	snap_format.width = SNAPSHOT_WIDTH;
	snap_format.height = SNAPSHOT_HEIGHT;

	while ((c = getopt_long(argc, argv, "c:f:hi:Ss:", opts, NULL)) != -1) {
		switch (c) {
		case 'c':
			if (parse_crop(optarg, &snap_crop)) {
				printf("invalid crop rectangle %s.\n", optarg);
				return 1;
			}
			do_crop = true;
			break;
		case 'f':
			snap_format.code = parse_format(optarg);
			if (snap_format.code == 0) {
				printf("invalid format %s.\n", optarg);
				return 1;
			}
			break;
		case 'h':
			usage(argv[0]);
			return 0;
		case 'i':
			snapshot_interval = strtoul(optarg, NULL, 10);
			if (snapshot_interval == 0) {
				printf("snapshot interval value must be >0.\n");
				return 1;
			}
			break;
		case 'S':
			snapshot_save = true;
			break;
		case 's':
			if (parse_size(optarg, &snap_format)) {
				printf("invalid size %s.\n", optarg);
				return 1;
			}
			break;
		default:
			printf("Invalid option -%c\n", c);
			printf("Run %s -h for help.\n", argv[0]);
			return 1;
		}
	}

	events_init(&events);

	/* Register a signal handler for SIGINT, received when the user presses
	 * CTRL-C. This will allow the main loop to be interrupted, and resources
	 * to be freed cleanly.
	 */
	signal(SIGINT, sigint_handler);

	/* Open the OMAP3 ISP device and setup the capture pipeline. */
	memset(&ops, 0, sizeof ops);
	ops.viewfinder_ready = viewfinder_process;
	ops.snapshot_ready = snapshot_process;
	ops.watch_fd = __events_watch_fd;
	ops.unwatch_fd = __events_unwatch_fd;

	isp = omap3_isp_open(MEDIA_DEVICE, &ops);
	if (isp == NULL) {
		printf("error: unable to open media device %s\n", MEDIA_DEVICE);
		goto cleanup;
	}

	memset(&view_format, 0, sizeof view_format);
	view_format.code = V4L2_MBUS_FMT_YUYV8_1X16;
	view_format.width = VIEWFINDER_WIDTH;
	view_format.height = VIEWFINDER_HEIGHT;

	ret = omap3_isp_viewfinder_setup(isp, &view_format);
	if (ret < 0) {
		printf("error: unable to setup pipeline\n");
		goto cleanup;
	}

	printf("viewfinder configured for %04x %ux%u\n",
	       view_format.code, view_format.width, view_format.height);

	ret = omap3_isp_snapshot_setup(isp, do_crop ? &snap_crop : NULL, &snap_format);
	if (ret < 0) {
		printf("error: unable to setup pipeline\n");
		goto cleanup;
	}

	printf("snapshot configured for %04x %ux%u\n",
	       snap_format.code, snap_format.width, snap_format.height);

	/* Allocate a buffers pool and use it for the viewfinder. */
	pool = v4l2_buffers_pool_new(4);
	if (pool == NULL) {
		printf("error: unable to create buffers pool\n");
		goto cleanup;
	}

	ret = v4l2_buffers_pool_alloc(pool, view_format.width * view_format.height * 2, 4096);
	if (ret < 0) {
		printf("error: unable to allocate buffers memory\n");
		goto cleanup;
	}

	ret = omap3_isp_viewfinder_set_pool(isp, pool);
	if (ret < 0) {
		printf("error: unable to set buffers pool\n");
		goto cleanup;
	}

	/* Start the ISP. */
	ret = omap3_isp_viewfinder_start(isp);
	if (ret < 0)
		goto cleanup;

	clock_gettime(CLOCK_MONOTONIC, &start);

	/* Main capture loop. */
	if (events_loop(&events))
		goto cleanup;

	clock_gettime(CLOCK_MONOTONIC, &end);

	/* Stop the ISP. */
	omap3_isp_viewfinder_stop(isp);

	/* Print some statistics. */
	end.tv_sec -= start.tv_sec;
	end.tv_nsec -= start.tv_nsec;
	if (end.tv_nsec < 0) {
		end.tv_sec--;
		end.tv_nsec += 1000000000;
	}

	fps = frame_count / (end.tv_sec + end.tv_nsec / 1000000000.0);

	printf("%u images processed in %lu.%06lu seconds (%f fps)\n", frame_count,
	       end.tv_sec, end.tv_nsec / 1000, fps);

	exit_code = EXIT_SUCCESS;

cleanup:
	/* Cleanup the ISP resources. */
	if (isp)
		omap3_isp_close(isp);
	if (pool)
		v4l2_buffers_pool_delete(pool);

	return exit_code;
}