forked from twam/v4l2grab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v4l2grab.c
1001 lines (814 loc) · 21.8 KB
/
v4l2grab.c
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* v4l2grab Version 0.3 *
* Copyright (C) 2012 by Tobias Müller *
* Tobias_Mueller@twam.info *
* *
* based on V4L2 Specification, Appendix B: Video Capture Example *
* (http://v4l2spec.bytesex.org/spec/capture-example.html) *
* *
* 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., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/**************************************************************************
* Modification History *
* *
* Matthew Witherwax 21AUG2013 *
* Added ability to change frame interval (ie. frame rate/fps) *
* Martin Savc 7JUL2015
* Added support for continuous capture using SIGINT to stop.
***************************************************************************/
// compile with all three access methods
#if !defined(IO_READ) && !defined(IO_MMAP) && !defined(IO_USERPTR)
#define IO_READ
#define IO_MMAP
#define IO_USERPTR
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <getopt.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <asm/types.h>
#include <linux/videodev2.h>
#include <jpeglib.h>
#include <libv4l2.h>
#include <signal.h>
#include <stdint.h>
#include <inttypes.h>
#include "config.h"
#include "yuv.h"
#define CLEAR(x) memset (&(x), 0, sizeof (x))
#ifndef VERSION
#define VERSION "unknown"
#endif
#if defined(IO_MMAP) || defined(IO_USERPTR)
// minimum number of buffers to request in VIDIOC_REQBUFS call
#define VIDIOC_REQBUFS_COUNT 2
#endif
typedef enum {
#ifdef IO_READ
IO_METHOD_READ,
#endif
#ifdef IO_MMAP
IO_METHOD_MMAP,
#endif
#ifdef IO_USERPTR
IO_METHOD_USERPTR,
#endif
} io_method;
struct buffer {
void * start;
size_t length;
};
static io_method io = IO_METHOD_MMAP;
static int fd = -1;
struct buffer * buffers = NULL;
static unsigned int n_buffers = 0;
// global settings
static unsigned int width = 640;
static unsigned int height = 480;
static unsigned int fps = 30;
static int continuous = 0;
static unsigned char jpegQuality = 70;
static char* jpegFilename = NULL;
static char* jpegFilenamePart = NULL;
static char* deviceName = "/dev/video0";
static const char* const continuousFilenameFmt = "%s_%010"PRIu32"_%"PRId64".jpg";
/**
SIGINT interput handler
*/
void StopContCapture(int sig_id) {
printf("stoping continuous capture\n");
continuous = 0;
}
void InstallSIGINTHandler() {
struct sigaction sa;
CLEAR(sa);
sa.sa_handler = StopContCapture;
if(sigaction(SIGINT, &sa, 0) != 0)
{
fprintf(stderr,"could not install SIGINT handler, continuous capture disabled");
continuous = 0;
}
}
/**
Print error message and terminate programm with EXIT_FAILURE return code.
\param s error message to print
*/
static void errno_exit(const char* s)
{
fprintf(stderr, "%s error %d, %s\n", s, errno, strerror(errno));
exit(EXIT_FAILURE);
}
/**
Do ioctl and retry if error was EINTR ("A signal was caught during the ioctl() operation."). Parameters are the same as on ioctl.
\param fd file descriptor
\param request request
\param argp argument
\returns result from ioctl
*/
static int xioctl(int fd, int request, void* argp)
{
int r;
do r = v4l2_ioctl(fd, request, argp);
while (-1 == r && EINTR == errno);
return r;
}
/**
Write image to jpeg file.
\param img image to write
*/
static void jpegWrite(unsigned char* img, char* jpegFilename)
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPROW row_pointer[1];
FILE *outfile = fopen( jpegFilename, "wb" );
// try to open file for saving
if (!outfile) {
errno_exit("jpeg");
}
// create jpeg data
cinfo.err = jpeg_std_error( &jerr );
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, outfile);
// set image parameters
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_YCbCr;
// set jpeg compression parameters to default
jpeg_set_defaults(&cinfo);
// and then adjust quality setting
jpeg_set_quality(&cinfo, jpegQuality, TRUE);
// start compress
jpeg_start_compress(&cinfo, TRUE);
// feed data
while (cinfo.next_scanline < cinfo.image_height) {
row_pointer[0] = &img[cinfo.next_scanline * cinfo.image_width * cinfo.input_components];
jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
// finish compression
jpeg_finish_compress(&cinfo);
// destroy jpeg data
jpeg_destroy_compress(&cinfo);
// close output file
fclose(outfile);
}
/**
process image read
*/
static void imageProcess(const void* p, struct timeval timestamp)
{
//timestamp.tv_sec
//timestamp.tv_usec
unsigned char* src = (unsigned char*)p;
unsigned char* dst = malloc(width*height*3*sizeof(char));
YUV420toYUV444(width, height, src, dst);
if(continuous==1) {
static uint32_t img_ind = 0;
int64_t timestamp_long;
timestamp_long = timestamp.tv_sec*1e6 + timestamp.tv_usec;
sprintf(jpegFilename,continuousFilenameFmt,jpegFilenamePart,img_ind++,timestamp_long);
}
// write jpeg
jpegWrite(dst,jpegFilename);
// free temporary image
free(dst);
}
/**
read single frame
*/
static int frameRead(void)
{
struct v4l2_buffer buf;
#ifdef IO_USERPTR
unsigned int i;
#endif
switch (io) {
#ifdef IO_READ
case IO_METHOD_READ:
if (-1 == v4l2_read(fd, buffers[0].start, buffers[0].length)) {
switch (errno) {
case EAGAIN:
return 0;
case EIO:
// Could ignore EIO, see spec.
// fall through
default:
errno_exit("read");
}
}
struct timespec ts;
struct timeval timestamp;
clock_gettime(CLOCK_MONOTONIC,&ts);
timestamp.tv_sec = ts.tv_sec;
timestamp.tv_usec = ts.tv_nsec/1000;
imageProcess(buffers[0].start,timestamp);
break;
#endif
#ifdef IO_MMAP
case IO_METHOD_MMAP:
CLEAR(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) {
switch (errno) {
case EAGAIN:
return 0;
case EIO:
// Could ignore EIO, see spec
// fall through
default:
errno_exit("VIDIOC_DQBUF");
}
}
assert(buf.index < n_buffers);
imageProcess(buffers[buf.index].start,buf.timestamp);
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
errno_exit("VIDIOC_QBUF");
break;
#endif
#ifdef IO_USERPTR
case IO_METHOD_USERPTR:
CLEAR (buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_USERPTR;
if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) {
switch (errno) {
case EAGAIN:
return 0;
case EIO:
// Could ignore EIO, see spec.
// fall through
default:
errno_exit("VIDIOC_DQBUF");
}
}
for (i = 0; i < n_buffers; ++i)
if (buf.m.userptr == (unsigned long)buffers[i].start && buf.length == buffers[i].length)
break;
assert (i < n_buffers);
imageProcess((void *)buf.m.userptr,buf.timestamp);
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
errno_exit("VIDIOC_QBUF");
break;
#endif
}
return 1;
}
/**
mainloop: read frames and process them
*/
static void mainLoop(void)
{
int count;
unsigned int numberOfTimeouts;
numberOfTimeouts = 0;
count = 3;
while (count-- > 0) {
for (;;) {
fd_set fds;
struct timeval tv;
int r;
FD_ZERO(&fds);
FD_SET(fd, &fds);
/* Timeout. */
tv.tv_sec = 1;
tv.tv_usec = 0;
r = select(fd + 1, &fds, NULL, NULL, &tv);
if (-1 == r) {
if (EINTR == errno)
continue;
errno_exit("select");
}
if (0 == r) {
if (numberOfTimeouts <= 0) {
count++;
} else {
fprintf(stderr, "select timeout\n");
exit(EXIT_FAILURE);
}
}
if(continuous == 1) {
count = 3;
}
if (frameRead())
break;
/* EAGAIN - continue select loop. */
}
}
}
/**
stop capturing
*/
static void captureStop(void)
{
enum v4l2_buf_type type;
switch (io) {
#ifdef IO_READ
case IO_METHOD_READ:
/* Nothing to do. */
break;
#endif
#ifdef IO_MMAP
case IO_METHOD_MMAP:
#endif
#ifdef IO_USERPTR
case IO_METHOD_USERPTR:
#endif
#if defined(IO_MMAP) || defined(IO_USERPTR)
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == xioctl(fd, VIDIOC_STREAMOFF, &type))
errno_exit("VIDIOC_STREAMOFF");
break;
#endif
}
}
/**
start capturing
*/
static void captureStart(void)
{
unsigned int i;
enum v4l2_buf_type type;
switch (io) {
#ifdef IO_READ
case IO_METHOD_READ:
/* Nothing to do. */
break;
#endif
#ifdef IO_MMAP
case IO_METHOD_MMAP:
for (i = 0; i < n_buffers; ++i) {
struct v4l2_buffer buf;
CLEAR(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
errno_exit("VIDIOC_QBUF");
}
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
errno_exit("VIDIOC_STREAMON");
break;
#endif
#ifdef IO_USERPTR
case IO_METHOD_USERPTR:
for (i = 0; i < n_buffers; ++i) {
struct v4l2_buffer buf;
CLEAR (buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_USERPTR;
buf.index = i;
buf.m.userptr = (unsigned long) buffers[i].start;
buf.length = buffers[i].length;
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
errno_exit("VIDIOC_QBUF");
}
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
errno_exit("VIDIOC_STREAMON");
break;
#endif
}
}
static void deviceUninit(void)
{
unsigned int i;
switch (io) {
#ifdef IO_READ
case IO_METHOD_READ:
free(buffers[0].start);
break;
#endif
#ifdef IO_MMAP
case IO_METHOD_MMAP:
for (i = 0; i < n_buffers; ++i)
if (-1 == v4l2_munmap(buffers[i].start, buffers[i].length))
errno_exit("munmap");
break;
#endif
#ifdef IO_USERPTR
case IO_METHOD_USERPTR:
for (i = 0; i < n_buffers; ++i)
free(buffers[i].start);
break;
#endif
}
free(buffers);
}
#ifdef IO_READ
static void readInit(unsigned int buffer_size)
{
buffers = calloc(1, sizeof(*buffers));
if (!buffers) {
fprintf(stderr, "Out of memory\n");
exit(EXIT_FAILURE);
}
buffers[0].length = buffer_size;
buffers[0].start = malloc(buffer_size);
if (!buffers[0].start) {
fprintf (stderr, "Out of memory\n");
exit(EXIT_FAILURE);
}
}
#endif
#ifdef IO_MMAP
static void mmapInit(void)
{
struct v4l2_requestbuffers req;
CLEAR(req);
req.count = VIDIOC_REQBUFS_COUNT;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {
if (EINVAL == errno) {
fprintf(stderr, "%s does not support memory mapping\n", deviceName);
exit(EXIT_FAILURE);
} else {
errno_exit("VIDIOC_REQBUFS");
}
}
if (req.count < 2) {
fprintf(stderr, "Insufficient buffer memory on %s\n", deviceName);
exit(EXIT_FAILURE);
}
buffers = calloc(req.count, sizeof(*buffers));
if (!buffers) {
fprintf(stderr, "Out of memory\n");
exit(EXIT_FAILURE);
}
for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
struct v4l2_buffer buf;
CLEAR(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = n_buffers;
if (-1 == xioctl(fd, VIDIOC_QUERYBUF, &buf))
errno_exit("VIDIOC_QUERYBUF");
buffers[n_buffers].length = buf.length;
buffers[n_buffers].start = v4l2_mmap(NULL /* start anywhere */, buf.length, PROT_READ | PROT_WRITE /* required */, MAP_SHARED /* recommended */, fd, buf.m.offset);
if (MAP_FAILED == buffers[n_buffers].start)
errno_exit("mmap");
}
}
#endif
#ifdef IO_USERPTR
static void userptrInit(unsigned int buffer_size)
{
struct v4l2_requestbuffers req;
unsigned int page_size;
page_size = getpagesize();
buffer_size = (buffer_size + page_size - 1) & ~(page_size - 1);
CLEAR(req);
req.count = VIDIOC_REQBUFS_COUNT;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_USERPTR;
if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {
if (EINVAL == errno) {
fprintf(stderr, "%s does not support user pointer i/o\n", deviceName);
exit(EXIT_FAILURE);
} else {
errno_exit("VIDIOC_REQBUFS");
}
}
buffers = calloc(4, sizeof(*buffers));
if (!buffers) {
fprintf(stderr, "Out of memory\n");
exit(EXIT_FAILURE);
}
for (n_buffers = 0; n_buffers < 4; ++n_buffers) {
buffers[n_buffers].length = buffer_size;
buffers[n_buffers].start = memalign(/* boundary */ page_size, buffer_size);
if (!buffers[n_buffers].start) {
fprintf(stderr, "Out of memory\n");
exit(EXIT_FAILURE);
}
}
}
#endif
/**
initialize device
*/
static void deviceInit(void)
{
struct v4l2_capability cap;
struct v4l2_cropcap cropcap;
struct v4l2_crop crop;
struct v4l2_format fmt;
struct v4l2_streamparm frameint;
unsigned int min;
if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) {
if (EINVAL == errno) {
fprintf(stderr, "%s is no V4L2 device\n",deviceName);
exit(EXIT_FAILURE);
} else {
errno_exit("VIDIOC_QUERYCAP");
}
}
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
fprintf(stderr, "%s is no video capture device\n",deviceName);
exit(EXIT_FAILURE);
}
switch (io) {
#ifdef IO_READ
case IO_METHOD_READ:
if (!(cap.capabilities & V4L2_CAP_READWRITE)) {
fprintf(stderr, "%s does not support read i/o\n",deviceName);
exit(EXIT_FAILURE);
}
break;
#endif
#ifdef IO_MMAP
case IO_METHOD_MMAP:
#endif
#ifdef IO_USERPTR
case IO_METHOD_USERPTR:
#endif
#if defined(IO_MMAP) || defined(IO_USERPTR)
if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
fprintf(stderr, "%s does not support streaming i/o\n",deviceName);
exit(EXIT_FAILURE);
}
break;
#endif
}
/* Select video input, video standard and tune here. */
CLEAR(cropcap);
cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (0 == xioctl(fd, VIDIOC_CROPCAP, &cropcap)) {
crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
crop.c = cropcap.defrect; /* reset to default */
if (-1 == xioctl(fd, VIDIOC_S_CROP, &crop)) {
switch (errno) {
case EINVAL:
/* Cropping not supported. */
break;
default:
/* Errors ignored. */
break;
}
}
} else {
/* Errors ignored. */
}
CLEAR(fmt);
// v4l2_format
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = width;
fmt.fmt.pix.height = height;
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt))
errno_exit("VIDIOC_S_FMT");
if (fmt.fmt.pix.pixelformat != V4L2_PIX_FMT_YUV420) {
fprintf(stderr,"Libv4l didn't accept YUV420 format. Can't proceed.\n");
exit(EXIT_FAILURE);
}
/* Note VIDIOC_S_FMT may change width and height. */
if (width != fmt.fmt.pix.width) {
width = fmt.fmt.pix.width;
fprintf(stderr,"Image width set to %i by device %s.\n", width, deviceName);
}
if (height != fmt.fmt.pix.height) {
height = fmt.fmt.pix.height;
fprintf(stderr,"Image height set to %i by device %s.\n", height, deviceName);
}
/* If the user has set the fps to -1, don't try to set the frame interval */
if (fps != -1)
{
CLEAR(frameint);
/* Attempt to set the frame interval. */
frameint.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
frameint.parm.capture.timeperframe.numerator = 1;
frameint.parm.capture.timeperframe.denominator = fps;
if (-1 == xioctl(fd, VIDIOC_S_PARM, &frameint))
fprintf(stderr,"Unable to set frame interval.\n");
}
/* Buggy driver paranoia. */
min = fmt.fmt.pix.width * 2;
if (fmt.fmt.pix.bytesperline < min)
fmt.fmt.pix.bytesperline = min;
min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
if (fmt.fmt.pix.sizeimage < min)
fmt.fmt.pix.sizeimage = min;
switch (io) {
#ifdef IO_READ
case IO_METHOD_READ:
readInit(fmt.fmt.pix.sizeimage);
break;
#endif
#ifdef IO_MMAP
case IO_METHOD_MMAP:
mmapInit();
break;
#endif
#ifdef IO_USERPTR
case IO_METHOD_USERPTR:
userptrInit(fmt.fmt.pix.sizeimage);
break;
#endif
}
}
/**
close device
*/
static void deviceClose(void)
{
if (-1 == v4l2_close(fd))
errno_exit("close");
fd = -1;
}
/**
open device
*/
static void deviceOpen(void)
{
struct stat st;
// stat file
if (-1 == stat(deviceName, &st)) {
fprintf(stderr, "Cannot identify '%s': %d, %s\n", deviceName, errno, strerror(errno));
exit(EXIT_FAILURE);
}
// check if its device
if (!S_ISCHR(st.st_mode)) {
fprintf(stderr, "%s is no device\n", deviceName);
exit(EXIT_FAILURE);
}
// open device
fd = v4l2_open(deviceName, O_RDWR /* required */ | O_NONBLOCK, 0);
// check if opening was successfull
if (-1 == fd) {
fprintf(stderr, "Cannot open '%s': %d, %s\n", deviceName, errno, strerror(errno));
exit(EXIT_FAILURE);
}
}
/**
print usage information
*/
static void usage(FILE* fp, int argc, char** argv)
{
fprintf(fp,
"Usage: %s [options]\n\n"
"Options:\n"
"-d | --device name Video device name [/dev/video0]\n"
"-h | --help Print this message\n"
"-o | --output Set JPEG output filename\n"
"-q | --quality Set JPEG quality (0-100)\n"
"-m | --mmap Use memory mapped buffers\n"
"-r | --read Use read() calls\n"
"-u | --userptr Use application allocated buffers\n"
"-W | --width Set image width\n"
"-H | --height Set image height\n"
"-I | --interval Set frame interval (fps) (-1 to skip)\n"
"-c | --continuous Do continous capture, stop with SIGINT.\n"
"-v | --version Print version\n"
"",
argv[0]);
}
static const char short_options [] = "d:ho:q:mruW:H:I:vc";
static const struct option
long_options [] = {
{ "device", required_argument, NULL, 'd' },
{ "help", no_argument, NULL, 'h' },
{ "output", required_argument, NULL, 'o' },
{ "quality", required_argument, NULL, 'q' },
{ "mmap", no_argument, NULL, 'm' },
{ "read", no_argument, NULL, 'r' },
{ "userptr", no_argument, NULL, 'u' },
{ "width", required_argument, NULL, 'W' },
{ "height", required_argument, NULL, 'H' },
{ "interval", required_argument, NULL, 'I' },
{ "version", no_argument, NULL, 'v' },
{ "continuous", no_argument, NULL, 'c' },
{ 0, 0, 0, 0 }
};
int main(int argc, char **argv)
{
for (;;) {
int index, c = 0;
c = getopt_long(argc, argv, short_options, long_options, &index);
if (-1 == c)
break;
switch (c) {
case 0: /* getopt_long() flag */
break;
case 'd':
deviceName = optarg;
break;
case 'h':
// print help
usage(stdout, argc, argv);
exit(EXIT_SUCCESS);
case 'o':
// set jpeg filename
jpegFilename = optarg;
break;
case 'q':
// set jpeg quality
jpegQuality = atoi(optarg);
break;
case 'm':
#ifdef IO_MMAP
io = IO_METHOD_MMAP;
#else
fprintf(stderr, "You didn't compile for mmap support.\n");
exit(EXIT_FAILURE);
#endif
break;
case 'r':
#ifdef IO_READ
io = IO_METHOD_READ;
#else
fprintf(stderr, "You didn't compile for read support.\n");
exit(EXIT_FAILURE);
#endif
break;
case 'u':
#ifdef IO_USERPTR
io = IO_METHOD_USERPTR;
#else
fprintf(stderr, "You didn't compile for userptr support.\n");
exit(EXIT_FAILURE);
#endif
break;
case 'W':
// set width
width = atoi(optarg);
break;
case 'H':
// set height
height = atoi(optarg);
break;
case 'I':
// set fps
fps = atoi(optarg);
break;
case 'c':
// set flag for continuous capture, interuptible by sigint
continuous = 1;
InstallSIGINTHandler();
break;
case 'v':
printf("Version: %s\n", VERSION);
exit(EXIT_SUCCESS);
break;
default:
usage(stderr, argc, argv);
exit(EXIT_FAILURE);
}
}
// check for need parameters
if (!jpegFilename) {
fprintf(stderr, "You have to specify JPEG output filename!\n\n");
usage(stdout, argc, argv);
exit(EXIT_FAILURE);
}
if(continuous == 1) {
int max_name_len = snprintf(NULL,0,continuousFilenameFmt,jpegFilename,UINT32_MAX,INT64_MAX);
jpegFilenamePart = jpegFilename;
jpegFilename = calloc(max_name_len+1,sizeof(char));
strcpy(jpegFilename,jpegFilenamePart);
}
// open and initialize device
deviceOpen();
deviceInit();
// start capturing
captureStart();
// process frames
mainLoop();
// stop capturing
captureStop();
// close device
deviceUninit();
deviceClose();
if(jpegFilenamePart != 0){
free(jpegFilename);
}
exit(EXIT_SUCCESS);
return 0;