-
Notifications
You must be signed in to change notification settings - Fork 3
/
v4l2.py
2260 lines (1821 loc) · 66.4 KB
/
v4l2.py
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
# Python bindings for the v4l2 userspace api
# Copyright (C) 1999-2009 the contributors
# 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.
# Alternatively you can redistribute this file under the terms of the
# BSD license as stated below:
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. The names of its contributors may not be used to endorse or promote
# products derived from this software without specific prior written
# permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
Python bindings for the v4l2 userspace api in Linux 2.6.34
"""
# see linux/videodev2.h
import ctypes
_IOC_NRBITS = 8
_IOC_TYPEBITS = 8
_IOC_SIZEBITS = 14
_IOC_DIRBITS = 2
_IOC_NRSHIFT = 0
_IOC_TYPESHIFT = _IOC_NRSHIFT + _IOC_NRBITS
_IOC_SIZESHIFT = _IOC_TYPESHIFT + _IOC_TYPEBITS
_IOC_DIRSHIFT = _IOC_SIZESHIFT + _IOC_SIZEBITS
_IOC_NONE = 0
_IOC_WRITE = 1
_IOC_READ = 2
def _IOC(dir_, type_, nr, size):
return (
ctypes.c_int32(dir_ << _IOC_DIRSHIFT).value |
ctypes.c_int32(ord(type_) << _IOC_TYPESHIFT).value |
ctypes.c_int32(nr << _IOC_NRSHIFT).value |
ctypes.c_int32(size << _IOC_SIZESHIFT).value)
def _IOC_TYPECHECK(t):
return ctypes.sizeof(t)
def _IO(type_, nr):
return _IOC(_IOC_NONE, type_, nr, 0)
def _IOW(type_, nr, size):
return _IOC(_IOC_WRITE, type_, nr, _IOC_TYPECHECK(size))
def _IOR(type_, nr, size):
return _IOC(_IOC_READ, type_, nr, _IOC_TYPECHECK(size))
def _IOWR(type_, nr, size):
return _IOC(_IOC_READ | _IOC_WRITE, type_, nr, _IOC_TYPECHECK(size))
#
# type alias
#
enum = ctypes.c_uint
c_int = ctypes.c_int
#
# time
#
class timeval(ctypes.Structure):
_fields_ = [
('secs', ctypes.c_long),
('usecs', ctypes.c_long),
]
#
# v4l2
#
VIDEO_MAX_FRAME = 32
VIDEO_MAX_PLANES = 8
VID_TYPE_CAPTURE = 1
VID_TYPE_TUNER = 2
VID_TYPE_TELETEXT = 4
VID_TYPE_OVERLAY = 8
VID_TYPE_CHROMAKEY = 16
VID_TYPE_CLIPPING = 32
VID_TYPE_FRAMERAM = 64
VID_TYPE_SCALES = 128
VID_TYPE_MONOCHROME = 256
VID_TYPE_SUBCAPTURE = 512
VID_TYPE_MPEG_DECODER = 1024
VID_TYPE_MPEG_ENCODER = 2048
VID_TYPE_MJPEG_DECODER = 4096
VID_TYPE_MJPEG_ENCODER = 8192
def v4l2_fourcc(a, b, c, d):
return ord(a) | (ord(b) << 8) | (ord(c) << 16) | (ord(d) << 24)
def v4l2_fourcc2str(fourcc):
a = chr(fourcc & 0xFF)
b = chr((fourcc >> 8) & 0xFF)
c = chr((fourcc >> 16) & 0xFF)
d = chr((fourcc >> 24) & 0xFF)
return ''.join([a, b, c, d])
v4l2_field = enum
(
V4L2_FIELD_ANY,
V4L2_FIELD_NONE,
V4L2_FIELD_TOP,
V4L2_FIELD_BOTTOM,
V4L2_FIELD_INTERLACED,
V4L2_FIELD_SEQ_TB,
V4L2_FIELD_SEQ_BT,
V4L2_FIELD_ALTERNATE,
V4L2_FIELD_INTERLACED_TB,
V4L2_FIELD_INTERLACED_BT,
) = range(10)
def V4L2_FIELD_HAS_TOP(field):
return (
field == V4L2_FIELD_TOP or
field == V4L2_FIELD_INTERLACED or
field == V4L2_FIELD_INTERLACED_TB or
field == V4L2_FIELD_INTERLACED_BT or
field == V4L2_FIELD_SEQ_TB or
field == V4L2_FIELD_SEQ_BT)
def V4L2_FIELD_HAS_BOTTOM(field):
return (
field == V4L2_FIELD_BOTTOM or
field == V4L2_FIELD_INTERLACED or
field == V4L2_FIELD_INTERLACED_TB or
field == V4L2_FIELD_INTERLACED_BT or
field == V4L2_FIELD_SEQ_TB or
field == V4L2_FIELD_SEQ_BT)
def V4L2_FIELD_HAS_BOTH(field):
return (
field == V4L2_FIELD_INTERLACED or
field == V4L2_FIELD_INTERLACED_TB or
field == V4L2_FIELD_INTERLACED_BT or
field == V4L2_FIELD_SEQ_TB or
field == V4L2_FIELD_SEQ_BT)
v4l2_buf_type = enum
( V4L2_BUF_TYPE_VIDEO_CAPTURE,
V4L2_BUF_TYPE_VIDEO_OUTPUT,
V4L2_BUF_TYPE_VIDEO_OVERLAY,
V4L2_BUF_TYPE_VBI_CAPTURE,
V4L2_BUF_TYPE_VBI_OUTPUT,
V4L2_BUF_TYPE_SLICED_VBI_CAPTURE,
V4L2_BUF_TYPE_SLICED_VBI_OUTPUT,
V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY,
V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
V4L2_BUF_TYPE_SDR_CAPTURE,
V4L2_BUF_TYPE_SDR_OUTPUT,
V4L2_BUF_TYPE_META_CAPTURE,
V4L2_BUF_TYPE_PRIVATE, # Deprecated, do not use.
) = list(range(1, 14)) + [0x80]
v4l2_ctrl_type = enum
(
V4L2_CTRL_TYPE_INTEGER,
V4L2_CTRL_TYPE_BOOLEAN,
V4L2_CTRL_TYPE_MENU,
V4L2_CTRL_TYPE_BUTTON,
V4L2_CTRL_TYPE_INTEGER64,
V4L2_CTRL_TYPE_CTRL_CLASS,
V4L2_CTRL_TYPE_STRING,
V4L2_CTRL_TYPE_BITMASK,
V4L2_CTRL_TYPE_INTEGER_MENU,
) = range(1, 10)
# Compound types are >= 0x0100
V4L2_CTRL_COMPOUND_TYPES = 0x0100
V4L2_CTRL_TYPE_U8 = 0x0100
V4L2_CTRL_TYPE_U16 = 0x0101
V4L2_CTRL_TYPE_U32 = 0x0102
v4l2_tuner_type = enum
(
V4L2_TUNER_RADIO,
V4L2_TUNER_ANALOG_TV,
V4L2_TUNER_DIGITAL_TV,
) = range(1, 4)
v4l2_memory = enum
(
V4L2_MEMORY_MMAP,
V4L2_MEMORY_USERPTR,
V4L2_MEMORY_OVERLAY,
V4L2_MEMORY_DMABUF,
) = range(1, 5)
v4l2_colorspace = enum
(
#Default colorspace, i.e. let the driver figure it out.
#Can only be used with video capture.
V4L2_COLORSPACE_DEFAULT,
# SMPTE 170M: used for broadcast NTSC/PAL SDTV
V4L2_COLORSPACE_SMPTE170M,
# Obsolete pre-1998 SMPTE 240M HDTV standard, superseded by Rec 709
V4L2_COLORSPACE_SMPTE240M,
# Rec.709: used for HDTV
V4L2_COLORSPACE_REC709,
#Deprecated, do not use. No driver will ever return this. This was
#based on a misunderstanding of the bt878 datasheet.
V4L2_COLORSPACE_BT878,
#NTSC 1953 colorspace. This only makes sense when dealing with
#really, really old NTSC recordings. Superseded by SMPTE 170M.
V4L2_COLORSPACE_470_SYSTEM_M,
#EBU Tech 3213 PAL/SECAM colorspace. This only makes sense when
#dealing with really old PAL/SECAM recordings. Superseded by
#SMPTE 170M.
V4L2_COLORSPACE_470_SYSTEM_BG,
#Effectively shorthand for V4L2_COLORSPACE_SRGB, V4L2_YCBCR_ENC_601
#and V4L2_QUANTIZATION_FULL_RANGE. To be used for (Motion-)JPEG.
V4L2_COLORSPACE_JPEG,
# For RGB colorspaces such as produces by most webcams.
V4L2_COLORSPACE_SRGB,
# AdobeRGB colorspace
V4L2_COLORSPACE_ADOBERGB,
# BT.2020 colorspace, used for UHDTV.
V4L2_COLORSPACE_BT2020,
# Raw colorspace: for RAW unprocessed images
V4L2_COLORSPACE_RAW,
# DCI-P3 colorspace, used by cinema projectors
V4L2_COLORSPACE_DCI_P3,
) = range(0, 13)
v4l2_priority = enum
(
V4L2_PRIORITY_UNSET,
V4L2_PRIORITY_BACKGROUND,
V4L2_PRIORITY_INTERACTIVE,
V4L2_PRIORITY_RECORD,
V4L2_PRIORITY_DEFAULT,
) = list(range(0, 4)) + [2]
class v4l2_rect(ctypes.Structure):
_fields_ = [
('left', ctypes.c_int32),
('top', ctypes.c_int32),
('width', ctypes.c_int32),
('height', ctypes.c_int32),
]
class v4l2_fract(ctypes.Structure):
_fields_ = [
('numerator', ctypes.c_uint32),
('denominator', ctypes.c_uint32),
]
#
# Driver capabilities
#
class v4l2_capability(ctypes.Structure):
_fields_ = [
('driver', ctypes.c_char * 16),
('card', ctypes.c_char * 32),
('bus_info', ctypes.c_char * 32),
('version', ctypes.c_uint32),
('capabilities', ctypes.c_uint32),
('reserved', ctypes.c_uint32 * 4),
]
#
# Values for 'capabilities' field
#
V4L2_CAP_VIDEO_CAPTURE = 0x00000001 # Is a video capture device
V4L2_CAP_VIDEO_OUTPUT = 0x00000002 # Is a video output device
V4L2_CAP_VIDEO_OVERLAY = 0x00000004 # Can do video overlay
V4L2_CAP_VBI_CAPTURE = 0x00000010 # Is a raw VBI capture device
V4L2_CAP_VBI_OUTPUT = 0x00000020 # Is a raw VBI output device
V4L2_CAP_SLICED_VBI_CAPTURE = 0x00000040 # Is a sliced VBI capture device
V4L2_CAP_SLICED_VBI_OUTPUT = 0x00000080 # Is a sliced VBI output device
V4L2_CAP_RDS_CAPTURE = 0x00000100 # RDS data capture
V4L2_CAP_VIDEO_OUTPUT_OVERLAY = 0x00000200 # Can do video output overlay
V4L2_CAP_HW_FREQ_SEEK = 0x00000400 # Can do hardware frequency seek
V4L2_CAP_RDS_OUTPUT = 0x00000800 # Is an RDS encoder
V4L2_CAP_VIDEO_CAPTURE_MPLANE = 0x00001000 # Is a video capture device that supports multiplanar formats
V4L2_CAP_VIDEO_OUTPUT_MPLANE = 0x00002000 # Is a video output device that supports multiplanar formats
V4L2_CAP_VIDEO_M2M_MPLANE = 0x00004000 # Is a video mem-to-mem device that supports multiplanar formats
V4L2_CAP_VIDEO_M2M = 0x00008000 # Is a video mem-to-mem device
V4L2_CAP_TUNER = 0x00010000 # has a tuner
V4L2_CAP_AUDIO = 0x00020000 # has audio support
V4L2_CAP_RADIO = 0x00040000 # is a radio device
V4L2_CAP_MODULATOR = 0x00080000 # has a modulator
V4L2_CAP_SDR_CAPTURE = 0x00100000 # Is a SDR capture device
V4L2_CAP_EXT_PIX_FORMAT = 0x00200000 # Supports the extended pixel format
V4L2_CAP_SDR_OUTPUT = 0x00400000 # Is a SDR output device
V4L2_CAP_META_CAPTURE = 0x00800000 # Is a metadata capture device
V4L2_CAP_READWRITE = 0x01000000 # read/write systemcalls
V4L2_CAP_ASYNCIO = 0x02000000 # async I/O
V4L2_CAP_STREAMING = 0x04000000 # streaming I/O ioctls
V4L2_CAP_TOUCH = 0x10000000 # Is a touch device
V4L2_CAP_DEVICE_CAPS = 0x80000000 # sets device capabilities field
#
# Video image format
#
class v4l2_pix_format(ctypes.Structure):
_fields_ = [
('width', ctypes.c_uint32),
('height', ctypes.c_uint32),
('pixelformat', ctypes.c_uint32),
('field', v4l2_field),
('bytesperline', ctypes.c_uint32),
('sizeimage', ctypes.c_uint32),
('colorspace', v4l2_colorspace),
('priv', ctypes.c_uint32),
]
# RGB formats
V4L2_PIX_FMT_RGB332 = v4l2_fourcc('R', 'G', 'B', '1')
V4L2_PIX_FMT_RGB444 = v4l2_fourcc('R', '4', '4', '4')
V4L2_PIX_FMT_RGB555 = v4l2_fourcc('R', 'G', 'B', 'O')
V4L2_PIX_FMT_RGB565 = v4l2_fourcc('R', 'G', 'B', 'P')
V4L2_PIX_FMT_RGB555X = v4l2_fourcc('R', 'G', 'B', 'Q')
V4L2_PIX_FMT_RGB565X = v4l2_fourcc('R', 'G', 'B', 'R')
V4L2_PIX_FMT_BGR24 = v4l2_fourcc('B', 'G', 'R', '3')
V4L2_PIX_FMT_RGB24 = v4l2_fourcc('R', 'G', 'B', '3')
V4L2_PIX_FMT_BGR32 = v4l2_fourcc('B', 'G', 'R', '4')
V4L2_PIX_FMT_RGB32 = v4l2_fourcc('R', 'G', 'B', '4')
V4L2_PIX_FMT_RGBX32 = v4l2_fourcc('X', 'B', '2', '4')
V4L2_PIX_FMT_XRGB32 = v4l2_fourcc('B', 'X', '2', '4')
V4L2_PIX_FMT_RGBA32 = v4l2_fourcc('A', 'B', '2', '4')
# Grey formats
V4L2_PIX_FMT_GREY = v4l2_fourcc('G', 'R', 'E', 'Y')
V4L2_PIX_FMT_Y10 = v4l2_fourcc('Y', '1', '0', ' ')
V4L2_PIX_FMT_Y16 = v4l2_fourcc('Y', '1', '6', ' ')
# Palette formats
V4L2_PIX_FMT_PAL8 = v4l2_fourcc('P', 'A', 'L', '8')
# Luminance+Chrominance formats
V4L2_PIX_FMT_YVU410 = v4l2_fourcc('Y', 'V', 'U', '9')
V4L2_PIX_FMT_YVU420 = v4l2_fourcc('Y', 'V', '1', '2')
V4L2_PIX_FMT_YUYV = v4l2_fourcc('Y', 'U', 'Y', 'V')
V4L2_PIX_FMT_YYUV = v4l2_fourcc('Y', 'Y', 'U', 'V')
V4L2_PIX_FMT_YVYU = v4l2_fourcc('Y', 'V', 'Y', 'U')
V4L2_PIX_FMT_UYVY = v4l2_fourcc('U', 'Y', 'V', 'Y')
V4L2_PIX_FMT_VYUY = v4l2_fourcc('V', 'Y', 'U', 'Y')
V4L2_PIX_FMT_YUV422P = v4l2_fourcc('4', '2', '2', 'P')
V4L2_PIX_FMT_YUV411P = v4l2_fourcc('4', '1', '1', 'P')
V4L2_PIX_FMT_Y41P = v4l2_fourcc('Y', '4', '1', 'P')
V4L2_PIX_FMT_YUV444 = v4l2_fourcc('Y', '4', '4', '4')
V4L2_PIX_FMT_YUV555 = v4l2_fourcc('Y', 'U', 'V', 'O')
V4L2_PIX_FMT_YUV565 = v4l2_fourcc('Y', 'U', 'V', 'P')
V4L2_PIX_FMT_YUV32 = v4l2_fourcc('Y', 'U', 'V', '4')
V4L2_PIX_FMT_YUV410 = v4l2_fourcc('Y', 'U', 'V', '9')
V4L2_PIX_FMT_YUV420 = v4l2_fourcc('Y', 'U', '1', '2')
V4L2_PIX_FMT_HI240 = v4l2_fourcc('H', 'I', '2', '4')
V4L2_PIX_FMT_HM12 = v4l2_fourcc('H', 'M', '1', '2')
# two planes -- one Y, one Cr + Cb interleaved
V4L2_PIX_FMT_NV12 = v4l2_fourcc('N', 'V', '1', '2')
V4L2_PIX_FMT_NV21 = v4l2_fourcc('N', 'V', '2', '1')
V4L2_PIX_FMT_NV16 = v4l2_fourcc('N', 'V', '1', '6')
V4L2_PIX_FMT_NV61 = v4l2_fourcc('N', 'V', '6', '1')
# Bayer formats - see http://www.siliconimaging.com/RGB%20Bayer.htm
V4L2_PIX_FMT_SBGGR8 = v4l2_fourcc('B', 'A', '8', '1')
V4L2_PIX_FMT_SGBRG8 = v4l2_fourcc('G', 'B', 'R', 'G')
V4L2_PIX_FMT_SGRBG8 = v4l2_fourcc('G', 'R', 'B', 'G')
V4L2_PIX_FMT_SRGGB8 = v4l2_fourcc('R', 'G', 'G', 'B')
V4L2_PIX_FMT_SBGGR10 = v4l2_fourcc('B', 'G', '1', '0')
V4L2_PIX_FMT_SGBRG10 = v4l2_fourcc('G', 'B', '1', '0')
V4L2_PIX_FMT_SGRBG10 = v4l2_fourcc('B', 'A', '1', '0')
V4L2_PIX_FMT_SRGGB10 = v4l2_fourcc('R', 'G', '1', '0')
V4L2_PIX_FMT_SGRBG10DPCM8 = v4l2_fourcc('B', 'D', '1', '0')
V4L2_PIX_FMT_SBGGR16 = v4l2_fourcc('B', 'Y', 'R', '2')
# compressed formats
V4L2_PIX_FMT_MJPEG = v4l2_fourcc('M', 'J', 'P', 'G')
V4L2_PIX_FMT_JPEG = v4l2_fourcc('J', 'P', 'E', 'G')
V4L2_PIX_FMT_DV = v4l2_fourcc('d', 'v', 's', 'd')
V4L2_PIX_FMT_MPEG = v4l2_fourcc('M', 'P', 'E', 'G')
V4L2_PIX_FMT_H264 = v4l2_fourcc('H', '2', '6', '4')
# Vendor-specific formats
V4L2_PIX_FMT_CPIA1 = v4l2_fourcc('C', 'P', 'I', 'A')
V4L2_PIX_FMT_WNVA = v4l2_fourcc('W', 'N', 'V', 'A')
V4L2_PIX_FMT_SN9C10X = v4l2_fourcc('S', '9', '1', '0')
V4L2_PIX_FMT_SN9C20X_I420 = v4l2_fourcc('S', '9', '2', '0')
V4L2_PIX_FMT_PWC1 = v4l2_fourcc('P', 'W', 'C', '1')
V4L2_PIX_FMT_PWC2 = v4l2_fourcc('P', 'W', 'C', '2')
V4L2_PIX_FMT_ET61X251 = v4l2_fourcc('E', '6', '2', '5')
V4L2_PIX_FMT_SPCA501 = v4l2_fourcc('S', '5', '0', '1')
V4L2_PIX_FMT_SPCA505 = v4l2_fourcc('S', '5', '0', '5')
V4L2_PIX_FMT_SPCA508 = v4l2_fourcc('S', '5', '0', '8')
V4L2_PIX_FMT_SPCA561 = v4l2_fourcc('S', '5', '6', '1')
V4L2_PIX_FMT_PAC207 = v4l2_fourcc('P', '2', '0', '7')
V4L2_PIX_FMT_MR97310A = v4l2_fourcc('M', '3', '1', '0')
V4L2_PIX_FMT_SN9C2028 = v4l2_fourcc('S', 'O', 'N', 'X')
V4L2_PIX_FMT_SQ905C = v4l2_fourcc('9', '0', '5', 'C')
V4L2_PIX_FMT_PJPG = v4l2_fourcc('P', 'J', 'P', 'G')
V4L2_PIX_FMT_OV511 = v4l2_fourcc('O', '5', '1', '1')
V4L2_PIX_FMT_OV518 = v4l2_fourcc('O', '5', '1', '8')
V4L2_PIX_FMT_STV0680 = v4l2_fourcc('S', '6', '8', '0')
#
# Format enumeration
#
class v4l2_fmtdesc(ctypes.Structure):
_fields_ = [
('index', ctypes.c_uint32),
('type', ctypes.c_int),
('flags', ctypes.c_uint32),
('description', ctypes.c_char * 32),
('pixelformat', ctypes.c_uint32),
('reserved', ctypes.c_uint32 * 4),
]
V4L2_FMT_FLAG_COMPRESSED = 0x0001
V4L2_FMT_FLAG_EMULATED = 0x0002
#
# Experimental frame size and frame rate enumeration
#
v4l2_frmsizetypes = enum
(
V4L2_FRMSIZE_TYPE_DISCRETE,
V4L2_FRMSIZE_TYPE_CONTINUOUS,
V4L2_FRMSIZE_TYPE_STEPWISE,
) = range(1, 4)
class v4l2_frmsize_discrete(ctypes.Structure):
_fields_ = [
('width', ctypes.c_uint32),
('height', ctypes.c_uint32),
]
class v4l2_frmsize_stepwise(ctypes.Structure):
_fields_ = [
('min_width', ctypes.c_uint32),
('min_height', ctypes.c_uint32),
('step_width', ctypes.c_uint32),
('min_height', ctypes.c_uint32),
('max_height', ctypes.c_uint32),
('step_height', ctypes.c_uint32),
]
class v4l2_frmsizeenum(ctypes.Structure):
class _u(ctypes.Union):
_fields_ = [
('discrete', v4l2_frmsize_discrete),
('stepwise', v4l2_frmsize_stepwise),
]
_fields_ = [
('index', ctypes.c_uint32),
('pixel_format', ctypes.c_uint32),
('type', ctypes.c_uint32),
('_u', _u),
('reserved', ctypes.c_uint32 * 2)
]
_anonymous_ = ('_u',)
#
# Frame rate enumeration
#
v4l2_frmivaltypes = enum
(
V4L2_FRMIVAL_TYPE_DISCRETE,
V4L2_FRMIVAL_TYPE_CONTINUOUS,
V4L2_FRMIVAL_TYPE_STEPWISE,
) = range(1, 4)
class v4l2_frmival_stepwise(ctypes.Structure):
_fields_ = [
('min', v4l2_fract),
('max', v4l2_fract),
('step', v4l2_fract),
]
class v4l2_frmivalenum(ctypes.Structure):
class _u(ctypes.Union):
_fields_ = [
('discrete', v4l2_fract),
('stepwise', v4l2_frmival_stepwise),
]
_fields_ = [
('index', ctypes.c_uint32),
('pixel_format', ctypes.c_uint32),
('width', ctypes.c_uint32),
('height', ctypes.c_uint32),
('type', ctypes.c_uint32),
('_u', _u),
('reserved', ctypes.c_uint32 * 2),
]
_anonymous_ = ('_u',)
#
# Timecode
#
class v4l2_timecode(ctypes.Structure):
_fields_ = [
('type', ctypes.c_uint32),
('flags', ctypes.c_uint32),
('frames', ctypes.c_uint8),
('seconds', ctypes.c_uint8),
('minutes', ctypes.c_uint8),
('hours', ctypes.c_uint8),
('userbits', ctypes.c_uint8 * 4),
]
V4L2_TC_TYPE_24FPS = 1
V4L2_TC_TYPE_25FPS = 2
V4L2_TC_TYPE_30FPS = 3
V4L2_TC_TYPE_50FPS = 4
V4L2_TC_TYPE_60FPS = 5
V4L2_TC_FLAG_DROPFRAME = 0x0001
V4L2_TC_FLAG_COLORFRAME = 0x0002
V4L2_TC_USERBITS_field = 0x000C
V4L2_TC_USERBITS_USERDEFINED = 0x0000
V4L2_TC_USERBITS_8BITCHARS = 0x0008
class v4l2_jpegcompression(ctypes.Structure):
_fields_ = [
('quality', ctypes.c_int),
('APPn', ctypes.c_int),
('APP_len', ctypes.c_int),
('APP_data', ctypes.c_char * 60),
('COM_len', ctypes.c_int),
('COM_data', ctypes.c_char * 60),
('jpeg_markers', ctypes.c_uint32),
]
V4L2_JPEG_MARKER_DHT = 1 << 3
V4L2_JPEG_MARKER_DQT = 1 << 4
V4L2_JPEG_MARKER_DRI = 1 << 5
V4L2_JPEG_MARKER_COM = 1 << 6
V4L2_JPEG_MARKER_APP = 1 << 7
#
# Memory-mapping buffers
#
# https://www.kernel.org/doc/html/v5.10/userspace-api/media/v4l/buffer.html#struct-v4l2-plane
class v4l2_plane(ctypes.Structure):
class _u(ctypes.Union):
_fields_ = [("mem_offset", ctypes.c_uint32),
("userptr", ctypes.c_ulong),
("fd", ctypes.c_int32)]
_fields_ = [
('bytesused', ctypes.c_uint32),
('length', ctypes.c_uint32),
('m', _u),
('data_offset', ctypes.c_uint32),
('reserved', ctypes.c_uint32 * 11)
]
class v4l2_requestbuffers(ctypes.Structure):
_fields_ = [
('count', ctypes.c_uint32),
('type', v4l2_buf_type),
('memory', v4l2_memory),
('reserved', ctypes.c_uint32 * 2),
]
class v4l2_buffer(ctypes.Structure):
class _u(ctypes.Union):
_fields_ = [
('offset', ctypes.c_uint32),
('userptr', ctypes.c_ulong),
('planes', ctypes.POINTER(v4l2_plane)),
('fd', ctypes.c_int32)
]
_fields_ = [
('index', ctypes.c_uint32),
('type', v4l2_buf_type),
('bytesused', ctypes.c_uint32),
('flags', ctypes.c_uint32),
('field', v4l2_field),
('timestamp', timeval),
('timecode', v4l2_timecode),
('sequence', ctypes.c_uint32),
('memory', v4l2_memory),
('m', _u),
('length', ctypes.c_uint32),
('input', ctypes.c_uint32),
('reserved', ctypes.c_uint32),
]
V4L2_BUF_FLAG_MAPPED = 0x0001
V4L2_BUF_FLAG_QUEUED = 0x0002
V4L2_BUF_FLAG_DONE = 0x0004
V4L2_BUF_FLAG_KEYFRAME = 0x0008
V4L2_BUF_FLAG_PFRAME = 0x0010
V4L2_BUF_FLAG_BFRAME = 0x0020
V4L2_BUF_FLAG_TIMECODE = 0x0100
V4L2_BUF_FLAG_INPUT = 0x0200
#
# Overlay preview
#
class v4l2_framebuffer(ctypes.Structure):
_fields_ = [
('capability', ctypes.c_uint32),
('flags', ctypes.c_uint32),
('base', ctypes.c_void_p),
('fmt', v4l2_pix_format),
]
V4L2_FBUF_CAP_EXTERNOVERLAY = 0x0001
V4L2_FBUF_CAP_CHROMAKEY = 0x0002
V4L2_FBUF_CAP_LIST_CLIPPING = 0x0004
V4L2_FBUF_CAP_BITMAP_CLIPPING = 0x0008
V4L2_FBUF_CAP_LOCAL_ALPHA = 0x0010
V4L2_FBUF_CAP_GLOBAL_ALPHA = 0x0020
V4L2_FBUF_CAP_LOCAL_INV_ALPHA = 0x0040
V4L2_FBUF_CAP_SRC_CHROMAKEY = 0x0080
V4L2_FBUF_FLAG_PRIMARY = 0x0001
V4L2_FBUF_FLAG_OVERLAY = 0x0002
V4L2_FBUF_FLAG_CHROMAKEY = 0x0004
V4L2_FBUF_FLAG_LOCAL_ALPHA = 0x0008
V4L2_FBUF_FLAG_GLOBAL_ALPHA = 0x0010
V4L2_FBUF_FLAG_LOCAL_INV_ALPHA = 0x0020
V4L2_FBUF_FLAG_SRC_CHROMAKEY = 0x0040
class v4l2_clip(ctypes.Structure):
pass
v4l2_clip._fields_ = [
('c', v4l2_rect),
('next', ctypes.POINTER(v4l2_clip)),
]
class v4l2_window(ctypes.Structure):
_fields_ = [
('w', v4l2_rect),
('field', v4l2_field),
('chromakey', ctypes.c_uint32),
('clips', ctypes.POINTER(v4l2_clip)),
('clipcount', ctypes.c_uint32),
('bitmap', ctypes.c_void_p),
('global_alpha', ctypes.c_uint8),
]
#
# Capture parameters
#
class v4l2_captureparm(ctypes.Structure):
_fields_ = [
('capability', ctypes.c_uint32),
('capturemode', ctypes.c_uint32),
('timeperframe', v4l2_fract),
('extendedmode', ctypes.c_uint32),
('readbuffers', ctypes.c_uint32),
('reserved', ctypes.c_uint32 * 4),
]
V4L2_MODE_HIGHQUALITY = 0x0001
V4L2_CAP_TIMEPERFRAME = 0x1000
class v4l2_outputparm(ctypes.Structure):
_fields_ = [
('capability', ctypes.c_uint32),
('outputmode', ctypes.c_uint32),
('timeperframe', v4l2_fract),
('extendedmode', ctypes.c_uint32),
('writebuffers', ctypes.c_uint32),
('reserved', ctypes.c_uint32 * 4),
]
#
# Input image cropping
#
class v4l2_cropcap(ctypes.Structure):
_fields_ = [
('type', v4l2_buf_type),
('bounds', v4l2_rect),
('defrect', v4l2_rect),
('pixelaspect', v4l2_fract),
]
class v4l2_crop(ctypes.Structure):
_fields_ = [
('type', ctypes.c_int),
('c', v4l2_rect),
]
#
# Analog video standard
#
v4l2_std_id = ctypes.c_uint64
V4L2_STD_PAL_B = 0x00000001
V4L2_STD_PAL_B1 = 0x00000002
V4L2_STD_PAL_G = 0x00000004
V4L2_STD_PAL_H = 0x00000008
V4L2_STD_PAL_I = 0x00000010
V4L2_STD_PAL_D = 0x00000020
V4L2_STD_PAL_D1 = 0x00000040
V4L2_STD_PAL_K = 0x00000080
V4L2_STD_PAL_M = 0x00000100
V4L2_STD_PAL_N = 0x00000200
V4L2_STD_PAL_Nc = 0x00000400
V4L2_STD_PAL_60 = 0x00000800
V4L2_STD_NTSC_M = 0x00001000
V4L2_STD_NTSC_M_JP = 0x00002000
V4L2_STD_NTSC_443 = 0x00004000
V4L2_STD_NTSC_M_KR = 0x00008000
V4L2_STD_SECAM_B = 0x00010000
V4L2_STD_SECAM_D = 0x00020000
V4L2_STD_SECAM_G = 0x00040000
V4L2_STD_SECAM_H = 0x00080000
V4L2_STD_SECAM_K = 0x00100000
V4L2_STD_SECAM_K1 = 0x00200000
V4L2_STD_SECAM_L = 0x00400000
V4L2_STD_SECAM_LC = 0x00800000
V4L2_STD_ATSC_8_VSB = 0x01000000
V4L2_STD_ATSC_16_VSB = 0x02000000
# some common needed stuff
V4L2_STD_PAL_BG = (V4L2_STD_PAL_B | V4L2_STD_PAL_B1 | V4L2_STD_PAL_G)
V4L2_STD_PAL_DK = (V4L2_STD_PAL_D | V4L2_STD_PAL_D1 | V4L2_STD_PAL_K)
V4L2_STD_PAL = (V4L2_STD_PAL_BG | V4L2_STD_PAL_DK | V4L2_STD_PAL_H | V4L2_STD_PAL_I)
V4L2_STD_NTSC = (V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_JP | V4L2_STD_NTSC_M_KR)
V4L2_STD_SECAM_DK = (V4L2_STD_SECAM_D | V4L2_STD_SECAM_K | V4L2_STD_SECAM_K1)
V4L2_STD_SECAM = (V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H | V4L2_STD_SECAM_DK | V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC)
V4L2_STD_525_60 = (V4L2_STD_PAL_M | V4L2_STD_PAL_60 | V4L2_STD_NTSC | V4L2_STD_NTSC_443)
V4L2_STD_625_50 = (V4L2_STD_PAL | V4L2_STD_PAL_N | V4L2_STD_PAL_Nc | V4L2_STD_SECAM)
V4L2_STD_ATSC = (V4L2_STD_ATSC_8_VSB | V4L2_STD_ATSC_16_VSB)
V4L2_STD_UNKNOWN = 0
V4L2_STD_ALL = (V4L2_STD_525_60 | V4L2_STD_625_50)
# some merged standards
V4L2_STD_MN = (V4L2_STD_PAL_M | V4L2_STD_PAL_N | V4L2_STD_PAL_Nc | V4L2_STD_NTSC)
V4L2_STD_B = (V4L2_STD_PAL_B | V4L2_STD_PAL_B1 | V4L2_STD_SECAM_B)
V4L2_STD_GH = (V4L2_STD_PAL_G | V4L2_STD_PAL_H|V4L2_STD_SECAM_G | V4L2_STD_SECAM_H)
V4L2_STD_DK = (V4L2_STD_PAL_DK | V4L2_STD_SECAM_DK)
class v4l2_standard(ctypes.Structure):
_fields_ = [
('index', ctypes.c_uint32),
('id', v4l2_std_id),
('name', ctypes.c_char * 24),
('frameperiod', v4l2_fract),
('framelines', ctypes.c_uint32),
('reserved', ctypes.c_uint32 * 4),
]
#
# Video timings dv preset
#
class v4l2_dv_preset(ctypes.Structure):
_fields_ = [
('preset', ctypes.c_uint32),
('reserved', ctypes.c_uint32 * 4)
]
#
# DV preset enumeration
#
class v4l2_dv_enum_preset(ctypes.Structure):
_fields_ = [
('index', ctypes.c_uint32),
('preset', ctypes.c_uint32),
('name', ctypes.c_char * 32),
('width', ctypes.c_uint32),
('height', ctypes.c_uint32),
('reserved', ctypes.c_uint32 * 4),
]
#
# DV preset values
#
V4L2_DV_INVALID = 0
V4L2_DV_480P59_94 = 1
V4L2_DV_576P50 = 2
V4L2_DV_720P24 = 3
V4L2_DV_720P25 = 4
V4L2_DV_720P30 = 5
V4L2_DV_720P50 = 6
V4L2_DV_720P59_94 = 7
V4L2_DV_720P60 = 8
V4L2_DV_1080I29_97 = 9
V4L2_DV_1080I30 = 10
V4L2_DV_1080I25 = 11
V4L2_DV_1080I50 = 12
V4L2_DV_1080I60 = 13
V4L2_DV_1080P24 = 14
V4L2_DV_1080P25 = 15
V4L2_DV_1080P30 = 16
V4L2_DV_1080P50 = 17
V4L2_DV_1080P60 = 18
#
# DV BT timings
#
class v4l2_bt_timings(ctypes.Structure):
_fields_ = [
('width', ctypes.c_uint32),
('height', ctypes.c_uint32),
('interlaced', ctypes.c_uint32),
('polarities', ctypes.c_uint32),
('pixelclock', ctypes.c_uint64),
('hfrontporch', ctypes.c_uint32),
('hsync', ctypes.c_uint32),
('hbackporch', ctypes.c_uint32),
('vfrontporch', ctypes.c_uint32),
('vsync', ctypes.c_uint32),
('vbackporch', ctypes.c_uint32),
('il_vfrontporch', ctypes.c_uint32),
('il_vsync', ctypes.c_uint32),
('il_vbackporch', ctypes.c_uint32),
('reserved', ctypes.c_uint32 * 16),
]
_pack_ = True
# Interlaced or progressive format
V4L2_DV_PROGRESSIVE = 0
V4L2_DV_INTERLACED = 1
# Polarities. If bit is not set, it is assumed to be negative polarity
V4L2_DV_VSYNC_POS_POL = 0x00000001
V4L2_DV_HSYNC_POS_POL = 0x00000002
class v4l2_dv_timings(ctypes.Structure):
class _u(ctypes.Union):
_fields_ = [
('bt', v4l2_bt_timings),
('reserved', ctypes.c_uint32 * 32),
]
_fields_ = [
('type', ctypes.c_uint32),
('_u', _u),
]
_anonymous_ = ('_u',)
_pack_ = True
# Values for the type field
V4L2_DV_BT_656_1120 = 0
#
# Video inputs
#
class v4l2_input(ctypes.Structure):
_fields_ = [
('index', ctypes.c_uint32),
('name', ctypes.c_char * 32),
('type', ctypes.c_uint32),
('audioset', ctypes.c_uint32),
('tuner', ctypes.c_uint32),
('std', v4l2_std_id),
('status', ctypes.c_uint32),
('reserved', ctypes.c_uint32 * 4),
]
V4L2_INPUT_TYPE_TUNER = 1
V4L2_INPUT_TYPE_CAMERA = 2
V4L2_IN_ST_NO_POWER = 0x00000001
V4L2_IN_ST_NO_SIGNAL = 0x00000002
V4L2_IN_ST_NO_COLOR = 0x00000004
V4L2_IN_ST_HFLIP = 0x00000010
V4L2_IN_ST_VFLIP = 0x00000020
V4L2_IN_ST_NO_H_LOCK = 0x00000100
V4L2_IN_ST_COLOR_KILL = 0x00000200
V4L2_IN_ST_NO_SYNC = 0x00010000
V4L2_IN_ST_NO_EQU = 0x00020000
V4L2_IN_ST_NO_CARRIER = 0x00040000
V4L2_IN_ST_MACROVISION = 0x01000000
V4L2_IN_ST_NO_ACCESS = 0x02000000
V4L2_IN_ST_VTR = 0x04000000
V4L2_IN_CAP_PRESETS = 0x00000001
V4L2_IN_CAP_CUSTOM_TIMINGS = 0x00000002