-
Notifications
You must be signed in to change notification settings - Fork 0
/
yavta.c
2768 lines (2346 loc) · 65.8 KB
/
yavta.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
/*
* yavta -- Yet Another V4L2 Test Application
*
* Copyright (C) 2005-2010 Laurent Pinchart <[email protected]>
*
* 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.,
*/
#define __STDC_FORMAT_MACROS
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <inttypes.h>
#include <unistd.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>
#include <sched.h>
#include <termios.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <linux/videodev2.h>
#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
enum buffer_fill_mode
{
BUFFER_FILL_NONE = 0,
BUFFER_FILL_FRAME = 1 << 0,
BUFFER_FILL_PADDING = 1 << 1,
};
struct buffer
{
unsigned int idx;
unsigned int padding[VIDEO_MAX_PLANES];
unsigned int size[VIDEO_MAX_PLANES];
void *mem[VIDEO_MAX_PLANES];
};
struct device
{
int fd;
int opened;
enum v4l2_buf_type type;
enum v4l2_memory memtype;
unsigned int nbufs;
struct buffer *buffers;
unsigned int width;
unsigned int height;
uint32_t buffer_output_flags;
uint32_t timestamp_type;
unsigned char num_planes;
struct v4l2_plane_pix_format plane_fmt[VIDEO_MAX_PLANES];
void *pattern[VIDEO_MAX_PLANES];
unsigned int patternsize[VIDEO_MAX_PLANES];
bool write_data_prefix;
};
/* -----------------------------------------------------------------------------
* Pause Handling
*/
static bool pause_resume;
static struct termios pause_term;
static bool pause_no_term;
static bool pause_term_configured;
static char pause_filename[23];
static void pause_signal_handler(int signal __attribute__((__unused__)))
{
pause_resume = true;
}
static void pause_wait(void)
{
int ret;
int fd;
fd = open(pause_filename, O_CREAT, 0);
if (fd != -1)
close(fd);
if (pause_no_term) {
printf("Paused waiting for SIGUSR1\n");
while (!pause_resume)
pause();
goto done;
}
printf("Paused waiting for key press or SIGUSR1\n");
pause_resume = false;
while (!pause_resume) {
fd_set rfds;
char c;
FD_ZERO(&rfds);
FD_SET(0, &rfds);
ret = select(1, &rfds, NULL, NULL, NULL);
if (ret < 0 && errno != EINTR)
break;
if (ret == 1) {
ret = read(0, &c, 1);
break;
}
}
done:
unlink(pause_filename);
}
static void pause_cleanup(void)
{
if (pause_term_configured)
tcsetattr(0, TCSANOW, &pause_term);
unlink(pause_filename);
}
static int pause_init(void)
{
struct sigaction sig_usr1;
struct termios term;
int ret;
sprintf(pause_filename, ".yavta.wait.%u", getpid());
memset(&sig_usr1, 0, sizeof(sig_usr1));
sig_usr1.sa_handler = pause_signal_handler;
ret = sigaction(SIGUSR1, &sig_usr1, NULL);
if (ret < 0) {
printf("Unable to install SIGUSR1 handler: %s (%d)\n",
strerror(errno), errno);
return -errno;
}
ret = tcgetattr(0, &term);
if (ret < 0) {
if (errno == ENOTTY) {
pause_no_term = true;
return 0;
}
printf("Unable to retrieve terminal attributes: %s (%d)\n",
strerror(errno), errno);
return -errno;
}
pause_term = term;
pause_term_configured = true;
atexit(pause_cleanup);
term.c_lflag &= ~ICANON;
term.c_lflag &= ~ECHO;
term.c_cc[VMIN] = 0;
term.c_cc[VTIME] = 0;
ret = tcsetattr(0, TCSANOW, &term);
if (ret < 0) {
printf("Unable to set terminal attributes: %s (%d)\n",
strerror(errno), errno);
return -errno;
}
return 0;
}
/* -----------------------------------------------------------------------------
* Pause Handling
*/
static bool video_is_mplane(struct device *dev)
{
return dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
dev->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
}
static bool video_is_meta(struct device *dev)
{
return dev->type == V4L2_BUF_TYPE_META_CAPTURE ||
dev->type == V4L2_BUF_TYPE_META_OUTPUT;
}
static bool video_is_capture(struct device *dev)
{
return dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
dev->type == V4L2_BUF_TYPE_META_CAPTURE;
}
static bool video_is_output(struct device *dev)
{
return dev->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ||
dev->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
dev->type == V4L2_BUF_TYPE_META_OUTPUT;
}
static struct {
enum v4l2_buf_type type;
bool supported;
const char *name;
const char *string;
} buf_types[] = {
{ V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, 1, "Video capture mplanes", "capture-mplane", },
{ V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 1, "Video output", "output-mplane", },
{ V4L2_BUF_TYPE_VIDEO_CAPTURE, 1, "Video capture", "capture", },
{ V4L2_BUF_TYPE_VIDEO_OUTPUT, 1, "Video output mplanes", "output", },
{ V4L2_BUF_TYPE_VIDEO_OVERLAY, 0, "Video overlay", "overlay" },
{ V4L2_BUF_TYPE_META_CAPTURE, 1, "Meta-data capture", "meta-capture", },
{ V4L2_BUF_TYPE_META_OUTPUT, 1, "Meta-data output", "meta-output", },
};
static int v4l2_buf_type_from_string(const char *str)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(buf_types); i++) {
if (!buf_types[i].supported)
continue;
if (strcmp(buf_types[i].string, str))
continue;
return buf_types[i].type;
}
return -1;
}
static const char *v4l2_buf_type_name(enum v4l2_buf_type type)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(buf_types); ++i) {
if (buf_types[i].type == type)
return buf_types[i].name;
}
if (type & V4L2_BUF_TYPE_PRIVATE)
return "Private";
else
return "Unknown";
}
static struct v4l2_format_info {
const char *name;
unsigned int fourcc;
unsigned char n_planes;
} pixel_formats[] = {
{ "RGB332", V4L2_PIX_FMT_RGB332, 1 },
{ "RGB444", V4L2_PIX_FMT_RGB444, 1 },
{ "ARGB444", V4L2_PIX_FMT_ARGB444, 1 },
{ "XRGB444", V4L2_PIX_FMT_XRGB444, 1 },
{ "RGB555", V4L2_PIX_FMT_RGB555, 1 },
{ "ARGB555", V4L2_PIX_FMT_ARGB555, 1 },
{ "XRGB555", V4L2_PIX_FMT_XRGB555, 1 },
{ "RGB565", V4L2_PIX_FMT_RGB565, 1 },
{ "RGB555X", V4L2_PIX_FMT_RGB555X, 1 },
{ "RGB565X", V4L2_PIX_FMT_RGB565X, 1 },
{ "BGR666", V4L2_PIX_FMT_BGR666, 1 },
{ "BGR24", V4L2_PIX_FMT_BGR24, 1 },
{ "RGB24", V4L2_PIX_FMT_RGB24, 1 },
{ "BGR32", V4L2_PIX_FMT_BGR32, 1 },
{ "ABGR32", V4L2_PIX_FMT_ABGR32, 1 },
{ "XBGR32", V4L2_PIX_FMT_XBGR32, 1 },
{ "RGB32", V4L2_PIX_FMT_RGB32, 1 },
{ "ARGB32", V4L2_PIX_FMT_ARGB32, 1 },
{ "XRGB32", V4L2_PIX_FMT_XRGB32, 1 },
{ "HSV24", V4L2_PIX_FMT_HSV24, 1 },
{ "HSV32", V4L2_PIX_FMT_HSV32, 1 },
{ "Y8", V4L2_PIX_FMT_GREY, 1 },
{ "Y10", V4L2_PIX_FMT_Y10, 1 },
{ "Y12", V4L2_PIX_FMT_Y12, 1 },
{ "Y16", V4L2_PIX_FMT_Y16, 1 },
//xumm add
{ "XY10", V4L2_PIX_FMT_XAVIER_Y10, 1 },
{ "XY12", V4L2_PIX_FMT_XAVIER_Y12, 1 },
{ "TY10", V4L2_PIX_FMT_TX2_Y10, 1 },
{ "TY12", V4L2_PIX_FMT_TX2_Y12, 1 },
//end
{ "UYVY", V4L2_PIX_FMT_UYVY, 1 },
{ "VYUY", V4L2_PIX_FMT_VYUY, 1 },
{ "YUYV", V4L2_PIX_FMT_YUYV, 1 },
{ "YVYU", V4L2_PIX_FMT_YVYU, 1 },
{ "YUV32", V4L2_PIX_FMT_YUV32, 1 },
{ "AYUV32", V4L2_PIX_FMT_AYUV32, 1 },
{ "XYUV32", V4L2_PIX_FMT_XYUV32, 1 },
{ "VUYA32", V4L2_PIX_FMT_VUYA32, 1 },
{ "VUYX32", V4L2_PIX_FMT_VUYX32, 1 },
{ "NV12", V4L2_PIX_FMT_NV12, 1 },
{ "NV12M", V4L2_PIX_FMT_NV12M, 2 },
{ "NV21", V4L2_PIX_FMT_NV21, 1 },
{ "NV21M", V4L2_PIX_FMT_NV21M, 2 },
{ "NV16", V4L2_PIX_FMT_NV16, 1 },
{ "NV16M", V4L2_PIX_FMT_NV16M, 2 },
{ "NV61", V4L2_PIX_FMT_NV61, 1 },
{ "NV61M", V4L2_PIX_FMT_NV61M, 2 },
{ "NV24", V4L2_PIX_FMT_NV24, 1 },
{ "NV42", V4L2_PIX_FMT_NV42, 1 },
{ "YUV420M", V4L2_PIX_FMT_YUV420M, 3 },
{ "YUV422M", V4L2_PIX_FMT_YUV422M, 3 },
{ "YUV444M", V4L2_PIX_FMT_YUV444M, 3 },
{ "YVU420M", V4L2_PIX_FMT_YVU420M, 3 },
{ "YVU422M", V4L2_PIX_FMT_YVU422M, 3 },
{ "YVU444M", V4L2_PIX_FMT_YVU444M, 3 },
{ "SBGGR8", V4L2_PIX_FMT_SBGGR8, 1 },
{ "SGBRG8", V4L2_PIX_FMT_SGBRG8, 1 },
{ "SGRBG8", V4L2_PIX_FMT_SGRBG8, 1 },
{ "SRGGB8", V4L2_PIX_FMT_SRGGB8, 1 },
{ "SBGGR10_DPCM8", V4L2_PIX_FMT_SBGGR10DPCM8, 1 },
{ "SGBRG10_DPCM8", V4L2_PIX_FMT_SGBRG10DPCM8, 1 },
{ "SGRBG10_DPCM8", V4L2_PIX_FMT_SGRBG10DPCM8, 1 },
{ "SRGGB10_DPCM8", V4L2_PIX_FMT_SRGGB10DPCM8, 1 },
{ "SBGGR10", V4L2_PIX_FMT_SBGGR10, 1 },
{ "SGBRG10", V4L2_PIX_FMT_SGBRG10, 1 },
{ "SGRBG10", V4L2_PIX_FMT_SGRBG10, 1 },
{ "SRGGB10", V4L2_PIX_FMT_SRGGB10, 1 },
{ "SBGGR10P", V4L2_PIX_FMT_SBGGR10P, 1 },
{ "SGBRG10P", V4L2_PIX_FMT_SGBRG10P, 1 },
{ "SGRBG10P", V4L2_PIX_FMT_SGRBG10P, 1 },
{ "SRGGB10P", V4L2_PIX_FMT_SRGGB10P, 1 },
{ "SBGGR12", V4L2_PIX_FMT_SBGGR12, 1 },
{ "SGBRG12", V4L2_PIX_FMT_SGBRG12, 1 },
{ "SGRBG12", V4L2_PIX_FMT_SGRBG12, 1 },
{ "SRGGB12", V4L2_PIX_FMT_SRGGB12, 1 },
{ "SBGGR16", V4L2_PIX_FMT_SBGGR16, 1 },
{ "SGBRG16", V4L2_PIX_FMT_SGBRG16, 1 },
{ "SGRBG16", V4L2_PIX_FMT_SGRBG16, 1 },
{ "SRGGB16", V4L2_PIX_FMT_SRGGB16, 1 },
{ "IPU3_SBGGR10", V4L2_PIX_FMT_IPU3_SBGGR10, 1 },
{ "IPU3_SGBRG10", V4L2_PIX_FMT_IPU3_SGBRG10, 1 },
{ "IPU3_SGRBG10", V4L2_PIX_FMT_IPU3_SGRBG10, 1 },
{ "IPU3_SRGGB10", V4L2_PIX_FMT_IPU3_SRGGB10, 1 },
{ "DV", V4L2_PIX_FMT_DV, 1 },
{ "MJPEG", V4L2_PIX_FMT_MJPEG, 1 },
{ "MPEG", V4L2_PIX_FMT_MPEG, 1 },
};
static void list_formats(void)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(pixel_formats); i++)
printf("%s (\"%c%c%c%c\", %u planes)\n",
pixel_formats[i].name,
pixel_formats[i].fourcc & 0xff,
(pixel_formats[i].fourcc >> 8) & 0xff,
(pixel_formats[i].fourcc >> 16) & 0xff,
(pixel_formats[i].fourcc >> 24) & 0xff,
pixel_formats[i].n_planes);
}
static const struct v4l2_format_info *v4l2_format_by_fourcc(unsigned int fourcc)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(pixel_formats); ++i) {
if (pixel_formats[i].fourcc == fourcc)
return &pixel_formats[i];
}
return NULL;
}
static const struct v4l2_format_info *v4l2_format_by_name(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];
}
return NULL;
}
static const char *v4l2_format_name(unsigned int fourcc)
{
const struct v4l2_format_info *info;
static char name[5];
unsigned int i;
info = v4l2_format_by_fourcc(fourcc);
if (info)
return info->name;
for (i = 0; i < 4; ++i) {
name[i] = fourcc & 0xff;
fourcc >>= 8;
}
name[4] = '\0';
return name;
}
static const struct {
const char *name;
enum v4l2_field field;
} fields[] = {
{ "any", V4L2_FIELD_ANY },
{ "none", V4L2_FIELD_NONE },
{ "top", V4L2_FIELD_TOP },
{ "bottom", V4L2_FIELD_BOTTOM },
{ "interlaced", V4L2_FIELD_INTERLACED },
{ "seq-tb", V4L2_FIELD_SEQ_TB },
{ "seq-bt", V4L2_FIELD_SEQ_BT },
{ "alternate", V4L2_FIELD_ALTERNATE },
{ "interlaced-tb", V4L2_FIELD_INTERLACED_TB },
{ "interlaced-bt", V4L2_FIELD_INTERLACED_BT },
};
static enum v4l2_field v4l2_field_from_string(const char *name)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(fields); ++i) {
if (strcasecmp(fields[i].name, name) == 0)
return fields[i].field;
}
return -1;
}
static const char *v4l2_field_name(enum v4l2_field field)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(fields); ++i) {
if (fields[i].field == field)
return fields[i].name;
}
return "unknown";
}
static void video_set_buf_type(struct device *dev, enum v4l2_buf_type type)
{
dev->type = type;
}
static bool video_has_valid_buf_type(struct device *dev)
{
return (int)dev->type != -1;
}
static void video_init(struct device *dev)
{
dev->fd = -1;
dev->memtype = V4L2_MEMORY_MMAP;
dev->buffers = NULL;
dev->type = (enum v4l2_buf_type)-1;
}
static bool video_has_fd(struct device *dev)
{
return dev->fd != -1;
}
static int video_set_fd(struct device *dev, int fd)
{
if (video_has_fd(dev)) {
printf("Can't set fd (already open).\n");
return -1;
}
dev->fd = fd;
return 0;
}
static int video_open(struct device *dev, const char *devname)
{
if (video_has_fd(dev)) {
printf("Can't open device (already open).\n");
return -1;
}
dev->fd = open(devname, O_RDWR);
if (dev->fd < 0) {
printf("Error opening device %s: %s (%d).\n", devname,
strerror(errno), errno);
return dev->fd;
}
printf("Device %s opened.\n", devname);
dev->opened = 1;
return 0;
}
static int video_querycap(struct device *dev, unsigned int *capabilities)
{
struct v4l2_capability cap;
unsigned int caps;
bool has_video;
bool has_meta;
bool has_capture;
bool has_output;
bool has_mplane;
int ret;
memset(&cap, 0, sizeof cap);
ret = ioctl(dev->fd, VIDIOC_QUERYCAP, &cap);
if (ret < 0)
return 0;
caps = cap.capabilities & V4L2_CAP_DEVICE_CAPS
? cap.device_caps : cap.capabilities;
has_video = caps & (V4L2_CAP_VIDEO_CAPTURE_MPLANE |
V4L2_CAP_VIDEO_CAPTURE |
V4L2_CAP_VIDEO_OUTPUT_MPLANE |
V4L2_CAP_VIDEO_OUTPUT);
has_meta = caps & (V4L2_CAP_META_CAPTURE |
V4L2_CAP_META_OUTPUT);
has_capture = caps & (V4L2_CAP_VIDEO_CAPTURE_MPLANE |
V4L2_CAP_VIDEO_CAPTURE |
V4L2_CAP_META_CAPTURE);
has_output = caps & (V4L2_CAP_VIDEO_OUTPUT_MPLANE |
V4L2_CAP_VIDEO_OUTPUT |
V4L2_CAP_META_OUTPUT);
has_mplane = caps & (V4L2_CAP_VIDEO_CAPTURE_MPLANE |
V4L2_CAP_VIDEO_OUTPUT_MPLANE);
printf("Device `%s' on `%s' (driver '%s') supports%s%s%s%s %s mplanes.\n",
cap.card, cap.bus_info, cap.driver,
has_video ? " video," : "",
has_meta ? " meta-data," : "",
has_capture ? " capture," : "",
has_output ? " output," : "",
has_mplane ? "with" : "without");
*capabilities = caps;
return 0;
}
static int cap_get_buf_type(unsigned int capabilities)
{
if (capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE) {
return V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
} else if (capabilities & V4L2_CAP_VIDEO_OUTPUT_MPLANE) {
return V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
} else if (capabilities & V4L2_CAP_VIDEO_CAPTURE) {
return V4L2_BUF_TYPE_VIDEO_CAPTURE;
} else if (capabilities & V4L2_CAP_VIDEO_OUTPUT) {
return V4L2_BUF_TYPE_VIDEO_OUTPUT;
} else if (capabilities & V4L2_CAP_META_CAPTURE) {
return V4L2_BUF_TYPE_META_CAPTURE;
} else if (capabilities & V4L2_CAP_META_OUTPUT) {
return V4L2_BUF_TYPE_META_OUTPUT;
} else {
printf("Device supports neither capture nor output.\n");
return -EINVAL;
}
return 0;
}
static void video_close(struct device *dev)
{
unsigned int i;
for (i = 0; i < dev->num_planes; i++)
free(dev->pattern[i]);
free(dev->buffers);
if (dev->opened)
close(dev->fd);
}
static void video_log_status(struct device *dev)
{
ioctl(dev->fd, VIDIOC_LOG_STATUS);
}
static int query_control(struct device *dev, unsigned int id,
struct v4l2_query_ext_ctrl *query)
{
struct v4l2_queryctrl q;
int ret;
memset(query, 0, sizeof(*query));
query->id = id;
ret = ioctl(dev->fd, VIDIOC_QUERY_EXT_CTRL, query);
if (ret < 0)
ret = -errno;
if (!ret || ret == -EINVAL)
return ret;
if (ret != -ENOTTY) {
printf("unable to query control 0x%8.8x: %s (%d).\n",
id, strerror(-ret), -ret);
return ret;
}
/*
* If VIDIOC_QUERY_EXT_CTRL isn't available emulate it using
* VIDIOC_QUERYCTRL.
*/
memset(&q, 0, sizeof(q));
q.id = id;
ret = ioctl(dev->fd, VIDIOC_QUERYCTRL, &q);
if (ret < 0) {
ret = -errno;
printf("unable to query control 0x%8.8x: %s (%d).\n",
id, strerror(-ret), -ret);
return ret;
}
memset(query, 0, sizeof(*query));
query->id = q.id;
query->type = q.type;
memcpy(query->name, q.name, sizeof(query->name));
query->minimum = q.minimum;
query->maximum = q.maximum;
query->step = q.step;
query->default_value = q.default_value;
query->flags = q.flags;
if (q.type == V4L2_CTRL_TYPE_STRING &&
!(q.flags & V4L2_CTRL_FLAG_HAS_PAYLOAD)) {
query->elem_size = q.maximum + 1;
query->elems = 1;
}
return 0;
}
static int get_control(struct device *dev,
const struct v4l2_query_ext_ctrl *query,
struct v4l2_ext_control *ctrl,
unsigned int which)
{
struct v4l2_ext_controls ctrls;
struct v4l2_control old;
int ret;
memset(&ctrls, 0, sizeof(ctrls));
memset(ctrl, 0, sizeof(*ctrl));
ctrls.which = which;
ctrls.count = 1;
ctrls.controls = ctrl;
ctrl->id = query->id;
if (query->flags & V4L2_CTRL_FLAG_HAS_PAYLOAD) {
ctrl->size = query->elems * query->elem_size;
ctrl->ptr = malloc(ctrl->size);
if (ctrl->ptr == NULL)
return -ENOMEM;
}
ret = ioctl(dev->fd, VIDIOC_G_EXT_CTRLS, &ctrls);
if (ret != -1)
return 0;
if (query->flags & V4L2_CTRL_FLAG_HAS_PAYLOAD)
free(ctrl->ptr);
if (query->flags & V4L2_CTRL_FLAG_HAS_PAYLOAD ||
query->type == V4L2_CTRL_TYPE_INTEGER64 ||
(errno != EINVAL && errno != ENOTTY))
return -errno;
old.id = query->id;
ret = ioctl(dev->fd, VIDIOC_G_CTRL, &old);
if (ret < 0)
return -errno;
ctrl->value = old.value;
return 0;
}
static int set_control(struct device *dev,
const struct v4l2_query_ext_ctrl *query,
struct v4l2_ext_control *ctrl)
{
struct v4l2_ext_controls ctrls;
struct v4l2_control old;
int ret;
memset(&ctrls, 0, sizeof(ctrls));
ctrls.ctrl_class = V4L2_CTRL_ID2CLASS(ctrl->id);
ctrls.count = 1;
ctrls.controls = ctrl;
ctrl->id = query->id;
ret = ioctl(dev->fd, VIDIOC_S_EXT_CTRLS, &ctrls);
if (ret != -1)
return 0;
if (query->flags & V4L2_CTRL_FLAG_HAS_PAYLOAD ||
query->type == V4L2_CTRL_TYPE_INTEGER64 ||
(errno != EINVAL && errno != ENOTTY))
return -1;
old.id = ctrl->id;
old.value = ctrl->value;
ret = ioctl(dev->fd, VIDIOC_S_CTRL, &old);
if (ret != -1)
ctrl->value = old.value;
return ret;
}
static int video_get_format(struct device *dev)
{
struct v4l2_format fmt;
unsigned int i;
int ret;
memset(&fmt, 0, sizeof fmt);
fmt.type = dev->type;
ret = ioctl(dev->fd, VIDIOC_G_FMT, &fmt);
if (ret < 0) {
printf("Unable to get format: %s (%d).\n", strerror(errno),
errno);
return ret;
}
if (video_is_mplane(dev)) {
dev->width = fmt.fmt.pix_mp.width;
dev->height = fmt.fmt.pix_mp.height;
dev->num_planes = fmt.fmt.pix_mp.num_planes;
printf("Video format: %s (%08x) %ux%u field %s, %u planes: \n",
v4l2_format_name(fmt.fmt.pix_mp.pixelformat), fmt.fmt.pix_mp.pixelformat,
fmt.fmt.pix_mp.width, fmt.fmt.pix_mp.height,
v4l2_field_name(fmt.fmt.pix_mp.field),
fmt.fmt.pix_mp.num_planes);
for (i = 0; i < fmt.fmt.pix_mp.num_planes; i++) {
dev->plane_fmt[i].bytesperline =
fmt.fmt.pix_mp.plane_fmt[i].bytesperline;
dev->plane_fmt[i].sizeimage =
fmt.fmt.pix_mp.plane_fmt[i].bytesperline ?
fmt.fmt.pix_mp.plane_fmt[i].sizeimage : 0;
printf(" * Stride %u, buffer size %u\n",
fmt.fmt.pix_mp.plane_fmt[i].bytesperline,
fmt.fmt.pix_mp.plane_fmt[i].sizeimage);
}
} else if (video_is_meta(dev)) {
dev->width = 0;
dev->height = 0;
dev->num_planes = 1;
printf("Meta-data format: %s (%08x) buffer size %u\n",
v4l2_format_name(fmt.fmt.meta.dataformat), fmt.fmt.meta.dataformat,
fmt.fmt.meta.buffersize);
} else {
dev->width = fmt.fmt.pix.width;
dev->height = fmt.fmt.pix.height;
dev->num_planes = 1;
dev->plane_fmt[0].bytesperline = fmt.fmt.pix.bytesperline;
dev->plane_fmt[0].sizeimage = fmt.fmt.pix.bytesperline ? fmt.fmt.pix.sizeimage : 0;
printf("Video format: %s (%08x) %ux%u (stride %u) field %s buffer size %u\n",
v4l2_format_name(fmt.fmt.pix.pixelformat), fmt.fmt.pix.pixelformat,
fmt.fmt.pix.width, fmt.fmt.pix.height, fmt.fmt.pix.bytesperline,
v4l2_field_name(fmt.fmt.pix_mp.field),
fmt.fmt.pix.sizeimage);
}
return 0;
}
static int video_set_format(struct device *dev, unsigned int w, unsigned int h,
unsigned int format, unsigned int stride,
unsigned int buffer_size, enum v4l2_field field,
unsigned int flags)
{
struct v4l2_format fmt;
unsigned int i;
int ret;
memset(&fmt, 0, sizeof fmt);
fmt.type = dev->type;
if (video_is_mplane(dev)) {
const struct v4l2_format_info *info = v4l2_format_by_fourcc(format);
fmt.fmt.pix_mp.width = w;
fmt.fmt.pix_mp.height = h;
fmt.fmt.pix_mp.pixelformat = format;
fmt.fmt.pix_mp.field = field;
fmt.fmt.pix_mp.num_planes = info->n_planes;
fmt.fmt.pix_mp.flags = flags;
for (i = 0; i < fmt.fmt.pix_mp.num_planes; i++) {
fmt.fmt.pix_mp.plane_fmt[i].bytesperline = stride;
fmt.fmt.pix_mp.plane_fmt[i].sizeimage = buffer_size;
}
} else if (video_is_meta(dev)) {
fmt.fmt.meta.dataformat = format;
fmt.fmt.meta.buffersize = buffer_size;
} else {
fmt.fmt.pix.width = w;
fmt.fmt.pix.height = h;
fmt.fmt.pix.pixelformat = format;
fmt.fmt.pix.field = field;
fmt.fmt.pix.bytesperline = stride;
fmt.fmt.pix.sizeimage = buffer_size;
fmt.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
fmt.fmt.pix.flags = flags;
}
ret = ioctl(dev->fd, VIDIOC_S_FMT, &fmt);
if (ret < 0) {
printf("Unable to set format: %s (%d).\n", strerror(errno),
errno);
return ret;
}
if (video_is_mplane(dev)) {
printf("Video format set: %s (%08x) %ux%u field %s, %u planes: \n",
v4l2_format_name(fmt.fmt.pix_mp.pixelformat), fmt.fmt.pix_mp.pixelformat,
fmt.fmt.pix_mp.width, fmt.fmt.pix_mp.height,
v4l2_field_name(fmt.fmt.pix_mp.field),
fmt.fmt.pix_mp.num_planes);
for (i = 0; i < fmt.fmt.pix_mp.num_planes; i++) {
printf(" * Stride %u, buffer size %u\n",
fmt.fmt.pix_mp.plane_fmt[i].bytesperline,
fmt.fmt.pix_mp.plane_fmt[i].sizeimage);
}
} else if (video_is_meta(dev)) {
printf("Meta-data format: %s (%08x) buffer size %u\n",
v4l2_format_name(fmt.fmt.meta.dataformat), fmt.fmt.meta.dataformat,
fmt.fmt.meta.buffersize);
} else {
printf("Video format set: %s (%08x) %ux%u (stride %u) field %s buffer size %u\n",
v4l2_format_name(fmt.fmt.pix.pixelformat), fmt.fmt.pix.pixelformat,
fmt.fmt.pix.width, fmt.fmt.pix.height, fmt.fmt.pix.bytesperline,
v4l2_field_name(fmt.fmt.pix.field),
fmt.fmt.pix.sizeimage);
}
return 0;
}
static int video_set_framerate(struct device *dev, struct v4l2_fract *time_per_frame)
{
struct v4l2_streamparm parm;
int ret;
memset(&parm, 0, sizeof parm);
parm.type = dev->type;
ret = ioctl(dev->fd, VIDIOC_G_PARM, &parm);
if (ret < 0) {
printf("Unable to get frame rate: %s (%d).\n",
strerror(errno), errno);
return ret;
}
printf("Current frame rate: %u/%u\n",
parm.parm.capture.timeperframe.numerator,
parm.parm.capture.timeperframe.denominator);
printf("Setting frame rate to: %u/%u\n",
time_per_frame->numerator,
time_per_frame->denominator);
parm.parm.capture.timeperframe.numerator = time_per_frame->numerator;
parm.parm.capture.timeperframe.denominator = time_per_frame->denominator;
ret = ioctl(dev->fd, VIDIOC_S_PARM, &parm);
if (ret < 0) {
printf("Unable to set frame rate: %s (%d).\n", strerror(errno),
errno);
return ret;
}
ret = ioctl(dev->fd, VIDIOC_G_PARM, &parm);
if (ret < 0) {
printf("Unable to get frame rate: %s (%d).\n", strerror(errno),
errno);
return ret;
}
printf("Frame rate set: %u/%u\n",
parm.parm.capture.timeperframe.numerator,
parm.parm.capture.timeperframe.denominator);
return 0;
}
static int video_buffer_mmap(struct device *dev, struct buffer *buffer,
struct v4l2_buffer *v4l2buf)
{
unsigned int length;
unsigned int offset;
unsigned int i;
for (i = 0; i < dev->num_planes; i++) {
if (video_is_mplane(dev)) {
length = v4l2buf->m.planes[i].length;
offset = v4l2buf->m.planes[i].m.mem_offset;
} else {
length = v4l2buf->length;
offset = v4l2buf->m.offset;
}
buffer->mem[i] = mmap(0, length, PROT_READ | PROT_WRITE, MAP_SHARED,
dev->fd, offset);
if (buffer->mem[i] == MAP_FAILED) {
printf("Unable to map buffer %u/%u: %s (%d)\n",
buffer->idx, i, strerror(errno), errno);
return -1;
}
buffer->size[i] = length;
buffer->padding[i] = 0;
printf("Buffer %u/%u mapped at address %p.\n",
buffer->idx, i, buffer->mem[i]);
}
return 0;
}
static int video_buffer_munmap(struct device *dev, struct buffer *buffer)
{
unsigned int i;
int ret;
for (i = 0; i < dev->num_planes; i++) {
ret = munmap(buffer->mem[i], buffer->size[i]);
if (ret < 0) {
printf("Unable to unmap buffer %u/%u: %s (%d)\n",
buffer->idx, i, strerror(errno), errno);
}
buffer->mem[i] = NULL;
}
return 0;
}
static int video_buffer_alloc_userptr(struct device *dev, struct buffer *buffer,
struct v4l2_buffer *v4l2buf,
unsigned int offset, unsigned int padding)
{
int page_size = getpagesize();
unsigned int length;
unsigned int i;
int ret;
for (i = 0; i < dev->num_planes; i++) {
if (video_is_mplane(dev))
length = v4l2buf->m.planes[i].length;
else
length = v4l2buf->length;
ret = posix_memalign(&buffer->mem[i], page_size,
length + offset + padding);
if (ret < 0) {
printf("Unable to allocate buffer %u/%u (%d)\n",
buffer->idx, i, ret);
return -ENOMEM;