forked from jnohlgard/python-v4l2capture
-
Notifications
You must be signed in to change notification settings - Fork 2
/
v4l2capture.c
1028 lines (908 loc) · 27.6 KB
/
v4l2capture.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
// python-v4l2capture
// Python extension to capture video with video4linux2
//
// 2009, 2010, 2011 Fredrik Portstrom
//
// I, the copyright holder of this file, hereby release it into the
// public domain. This applies worldwide. In case this is not legally
// possible: I grant anyone the right to use this work for any
// purpose, without any conditions, unless such conditions are
// required by law.
#include <Python.h>
#include <fcntl.h>
#include <linux/videodev2.h>
#include <sys/mman.h>
#ifdef USE_LIBV4L
# include <libv4l2.h>
#else
# include <sys/ioctl.h>
# define v4l2_close close
# define v4l2_ioctl ioctl
# define v4l2_mmap mmap
# define v4l2_munmap munmap
# define v4l2_open open
#endif
#ifndef Py_TYPE
# define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
#endif
#if PY_MAJOR_VERSION < 3
# define PyLong_FromLong PyInt_FromLong
#endif
#define ASSERT_OPEN if(self->fd < 0) { \
PyErr_SetString(PyExc_ValueError, \
"I/O operation on closed file"); \
return NULL; \
}
#define CLEAR(x) memset(&(x), 0, sizeof(x))
struct buffer {
void *start;
size_t length;
};
typedef struct {
PyObject_HEAD int fd;
struct buffer *buffers;
int buffer_count;
} Video_device;
struct capability {
int id;
const char *name;
};
static struct capability capabilities[] = {
{V4L2_CAP_ASYNCIO, "asyncio"},
{V4L2_CAP_AUDIO, "audio"},
{V4L2_CAP_HW_FREQ_SEEK, "hw_freq_seek"},
{V4L2_CAP_RADIO, "radio"},
{V4L2_CAP_RDS_CAPTURE, "rds_capture"},
{V4L2_CAP_READWRITE, "readwrite"},
{V4L2_CAP_SLICED_VBI_CAPTURE, "sliced_vbi_capture"},
{V4L2_CAP_SLICED_VBI_OUTPUT, "sliced_vbi_output"},
{V4L2_CAP_STREAMING, "streaming"},
{V4L2_CAP_TUNER, "tuner"},
{V4L2_CAP_VBI_CAPTURE, "vbi_capture"},
{V4L2_CAP_VBI_OUTPUT, "vbi_output"},
{V4L2_CAP_VIDEO_CAPTURE, "video_capture"},
{V4L2_CAP_VIDEO_OUTPUT, "video_output"},
{V4L2_CAP_VIDEO_OUTPUT_OVERLAY, "video_output_overlay"},
{V4L2_CAP_VIDEO_OVERLAY, "video_overlay"}
};
static int my_ioctl(
int fd,
int request,
void *arg) {
// Retry ioctl until it returns without being interrupted.
int result = -1;
while (result < 0) {
result = v4l2_ioctl(fd, request, arg);
if (result < 0 && errno != EINTR) {
PyErr_SetFromErrno(PyExc_IOError);
return 1;
}
}
return 0;
}
static void Video_device_unmap(
Video_device * self) {
int i;
for (i = 0; i < self->buffer_count; i++) {
v4l2_munmap(self->buffers[i].start, self->buffers[i].length);
}
}
static void Video_device_dealloc(
Video_device * self) {
if (self->fd >= 0) {
if (self->buffers) {
Video_device_unmap(self);
}
v4l2_close(self->fd);
}
Py_TYPE(self)->tp_free((PyObject *) self);
}
static int Video_device_init(
Video_device * self,
PyObject * args,
PyObject * kwargs) {
const char *device_path;
if (!PyArg_ParseTuple(args, "s", &device_path)) {
return -1;
}
int fd = v4l2_open(device_path, O_RDWR | O_NONBLOCK);
if (fd < 0) {
PyErr_SetFromErrnoWithFilename(PyExc_IOError, (char *) device_path);
return -1;
}
self->fd = fd;
self->buffers = NULL;
return 0;
}
static PyObject *Video_device_close(
Video_device * self) {
if (self->fd >= 0) {
if (self->buffers) {
Video_device_unmap(self);
}
v4l2_close(self->fd);
self->fd = -1;
}
Py_RETURN_NONE;
}
static PyObject *Video_device_fileno(
Video_device * self) {
ASSERT_OPEN;
return PyLong_FromLong(self->fd);
}
static PyObject *Video_device_get_info(
Video_device * self) {
ASSERT_OPEN;
struct v4l2_capability caps;
if (my_ioctl(self->fd, VIDIOC_QUERYCAP, &caps)) {
return NULL;
}
PyObject *set = PySet_New(NULL);
if (!set) {
return NULL;
}
struct capability *capability = capabilities;
while ((void *) capability < (void *) capabilities + sizeof(capabilities)) {
if (caps.capabilities & capability->id) {
#if PY_MAJOR_VERSION < 3
PyObject *s = PyString_FromString(capability->name);
#else
PyObject *s = PyBytes_FromString(capability->name);
#endif
if (!s) {
Py_DECREF(set);
return NULL;
}
PySet_Add(set, s);
}
capability++;
}
return Py_BuildValue("sssO", caps.driver, caps.card, caps.bus_info, set);
}
static PyObject *Video_device_set_format(
Video_device * self,
PyObject * args,
PyObject * keywds) {
int size_x;
int size_y;
int yuv420 = 0;
int fourcc;
const char *fourcc_str;
int fourcc_len = 0;
static char *kwlist[] = {
"size_x",
"size_y",
"yuv420",
"fourcc",
NULL
};
if (!PyArg_ParseTupleAndKeywords
(args, keywds, "ii|is#", kwlist, &size_x, &size_y, &yuv420, &fourcc_str,
&fourcc_len)) {
return NULL;
}
struct v4l2_format format;
CLEAR(format);
format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
/*
Get the current format
*/
if (my_ioctl(self->fd, VIDIOC_G_FMT, &format)) {
return NULL;
}
#ifdef USE_LIBV4L
format.fmt.pix.pixelformat =
yuv420 ? V4L2_PIX_FMT_YUV420 : V4L2_PIX_FMT_RGB24;
#else
format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
#endif
format.fmt.pix.field = V4L2_FIELD_INTERLACED;
if (fourcc_len == 4) {
fourcc = v4l2_fourcc(fourcc_str[0],
fourcc_str[1], fourcc_str[2], fourcc_str[3]);
format.fmt.pix.pixelformat = fourcc;
format.fmt.pix.field = V4L2_FIELD_ANY;
}
format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
format.fmt.pix.width = size_x;
format.fmt.pix.height = size_y;
format.fmt.pix.bytesperline = 0;
if (my_ioctl(self->fd, VIDIOC_S_FMT, &format)) {
return NULL;
}
return Py_BuildValue("ii", format.fmt.pix.width, format.fmt.pix.height);
}
static PyObject *Video_device_set_fps(
Video_device * self,
PyObject * args) {
int fps;
if (!PyArg_ParseTuple(args, "i", &fps)) {
return NULL;
}
struct v4l2_streamparm setfps;
CLEAR(setfps);
setfps.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
setfps.parm.capture.timeperframe.numerator = 1;
setfps.parm.capture.timeperframe.denominator = fps;
if (my_ioctl(self->fd, VIDIOC_S_PARM, &setfps)) {
return NULL;
}
return Py_BuildValue("i", setfps.parm.capture.timeperframe.denominator);
}
static void get_fourcc_str(
char *fourcc_str,
int fourcc) {
if (fourcc_str == NULL)
return;
fourcc_str[0] = (char) (fourcc & 0xFF);
fourcc_str[1] = (char) ((fourcc >> 8) & 0xFF);
fourcc_str[2] = (char) ((fourcc >> 16) & 0xFF);
fourcc_str[3] = (char) ((fourcc >> 24) & 0xFF);
fourcc_str[4] = 0;
}
static PyObject *Video_device_get_format(
Video_device * self) {
struct v4l2_format format;
CLEAR(format);
format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
/*
Get the current format
*/
if (my_ioctl(self->fd, VIDIOC_G_FMT, &format)) {
return NULL;
}
char current_fourcc[5];
get_fourcc_str(current_fourcc, format.fmt.pix.pixelformat);
return Py_BuildValue("iis", format.fmt.pix.width, format.fmt.pix.height,
current_fourcc);
}
static PyObject *Video_device_get_fourcc(
Video_device * self,
PyObject * args) {
char *fourcc_str;
int size;
int fourcc;
if (!PyArg_ParseTuple(args, "s#", &fourcc_str, &size)) {
return NULL;
}
if (size < 4) {
return NULL;
}
fourcc = v4l2_fourcc(fourcc_str[0],
fourcc_str[1], fourcc_str[2], fourcc_str[3]);
return Py_BuildValue("i", fourcc);
}
static PyObject *Video_device_set_auto_white_balance(
Video_device * self,
PyObject * args) {
#ifdef V4L2_CID_AUTO_WHITE_BALANCE
int autowb;
if (!PyArg_ParseTuple(args, "i", &autowb)) {
return NULL;
}
struct v4l2_control ctrl;
CLEAR(ctrl);
ctrl.id = V4L2_CID_AUTO_WHITE_BALANCE;
ctrl.value = autowb;
if (my_ioctl(self->fd, VIDIOC_S_CTRL, &ctrl)) {
return NULL;
}
return Py_BuildValue("i", ctrl.value);
#else
return NULL;
#endif
}
static PyObject *Video_device_get_auto_white_balance(
Video_device * self) {
#ifdef V4L2_CID_AUTO_WHITE_BALANCE
struct v4l2_control ctrl;
CLEAR(ctrl);
ctrl.id = V4L2_CID_AUTO_WHITE_BALANCE;
if (my_ioctl(self->fd, VIDIOC_G_CTRL, &ctrl)) {
return NULL;
}
return Py_BuildValue("i", ctrl.value);
#else
return NULL;
#endif
}
static PyObject *Video_device_set_white_balance_temperature(
Video_device * self,
PyObject * args) {
#ifdef V4L2_CID_WHITE_BALANCE_TEMPERATURE
int wb;
if (!PyArg_ParseTuple(args, "i", &wb)) {
return NULL;
}
struct v4l2_control ctrl;
CLEAR(ctrl);
ctrl.id = V4L2_CID_WHITE_BALANCE_TEMPERATURE;
ctrl.value = wb;
if (my_ioctl(self->fd, VIDIOC_S_CTRL, &ctrl)) {
return NULL;
}
return Py_BuildValue("i", ctrl.value);
#else
return NULL;
#endif
}
static PyObject *Video_device_get_white_balance_temperature(
Video_device * self) {
#ifdef V4L2_CID_WHITE_BALANCE_TEMPERATURE
struct v4l2_control ctrl;
CLEAR(ctrl);
ctrl.id = V4L2_CID_WHITE_BALANCE_TEMPERATURE;
if (my_ioctl(self->fd, VIDIOC_G_CTRL, &ctrl)) {
return NULL;
}
return Py_BuildValue("i", ctrl.value);
#else
return NULL;
#endif
}
static PyObject *Video_device_set_exposure_absolute(
Video_device * self,
PyObject * args) {
#ifdef V4L2_CID_EXPOSURE_ABSOLUTE
int exposure;
if (!PyArg_ParseTuple(args, "i", &exposure)) {
return NULL;
}
struct v4l2_control ctrl;
CLEAR(ctrl);
ctrl.id = V4L2_CID_EXPOSURE_ABSOLUTE;
ctrl.value = exposure;
if (my_ioctl(self->fd, VIDIOC_S_CTRL, &ctrl)) {
return NULL;
}
return Py_BuildValue("i", ctrl.value);
#else
return NULL;
#endif
}
static PyObject *Video_device_get_exposure_absolute(
Video_device * self) {
#ifdef V4L2_CID_EXPOSURE_ABSOLUTE
struct v4l2_control ctrl;
CLEAR(ctrl);
ctrl.id = V4L2_CID_EXPOSURE_ABSOLUTE;
if (my_ioctl(self->fd, VIDIOC_G_CTRL, &ctrl)) {
return NULL;
}
return Py_BuildValue("i", ctrl.value);
#else
return NULL;
#endif
}
static PyObject *Video_device_set_exposure_auto(
Video_device * self,
PyObject * args) {
#ifdef V4L2_CID_EXPOSURE_AUTO
int autoexposure;
if (!PyArg_ParseTuple(args, "i", &autoexposure)) {
return NULL;
}
struct v4l2_control ctrl;
CLEAR(ctrl);
ctrl.id = V4L2_CID_EXPOSURE_AUTO;
ctrl.value = autoexposure;
if (my_ioctl(self->fd, VIDIOC_S_CTRL, &ctrl)) {
return NULL;
}
return Py_BuildValue("i", ctrl.value);
#else
return NULL;
#endif
}
static PyObject *Video_device_get_exposure_auto(
Video_device * self) {
#ifdef V4L2_CID_EXPOSURE_AUTO
struct v4l2_control ctrl;
CLEAR(ctrl);
ctrl.id = V4L2_CID_EXPOSURE_AUTO;
if (my_ioctl(self->fd, VIDIOC_G_CTRL, &ctrl)) {
return NULL;
}
return Py_BuildValue("i", ctrl.value);
#else
return NULL;
#endif
}
static PyObject *Video_device_get_framesizes(
Video_device * self,
PyObject * args) {
struct v4l2_frmsizeenum frmsize;
CLEAR(frmsize);
char *fourcc_str;
int size;
if (!PyArg_ParseTuple(args, "s#", &fourcc_str, &size)) {
return NULL;
}
if (size != 4) {
return NULL;
}
frmsize.pixel_format = v4l2_fourcc(fourcc_str[0],
fourcc_str[1],
fourcc_str[2], fourcc_str[3]);
frmsize.index = 0;
PyObject *ret = PyList_New(0);
while (!my_ioctl(self->fd, VIDIOC_ENUM_FRAMESIZES, &frmsize)) {
PyObject *cap = PyDict_New();
switch (frmsize.type) {
case V4L2_FRMSIZE_TYPE_DISCRETE:
{
PyDict_SetItemString(cap, "size_x",
PyLong_FromLong(frmsize.discrete.width));
PyDict_SetItemString(cap, "size_y",
PyLong_FromLong(frmsize.discrete.height));
}
break;
case V4L2_FRMSIZE_TYPE_STEPWISE:
{
PyDict_SetItemString(cap, "min_width",
PyLong_FromLong(frmsize.stepwise.min_width));
PyDict_SetItemString(cap, "min_height",
PyLong_FromLong(frmsize.stepwise.min_height));
PyDict_SetItemString(cap, "max_width",
PyLong_FromLong(frmsize.stepwise.max_width));
PyDict_SetItemString(cap, "max_height",
PyLong_FromLong(frmsize.stepwise.max_height));
PyDict_SetItemString(cap, "step_width",
PyLong_FromLong(frmsize.stepwise.step_width));
PyDict_SetItemString(cap, "step_height",
PyLong_FromLong(frmsize.stepwise.step_height));
}
break;
}
PyList_Append(ret, cap);
frmsize.index++;
}
PyErr_Clear();
return ret;
}
static double fract2sec(
const struct v4l2_fract *f) {
return (double) f->numerator / f->denominator;
}
static double fract2fps(
const struct v4l2_fract *f) {
return (double) f->denominator / f->numerator;
}
static PyObject *Video_device_get_frameintervals(
Video_device * self,
PyObject * args) {
struct v4l2_frmivalenum frmival;
CLEAR(frmival);
char *fourcc_str;
int size;
if (!PyArg_ParseTuple
(args, "s#ii", &fourcc_str, &size, &frmival.width, &frmival.height)) {
return NULL;
}
if (size != 4) {
return NULL;
}
frmival.pixel_format = v4l2_fourcc(fourcc_str[0],
fourcc_str[1],
fourcc_str[2], fourcc_str[3]);
frmival.index = 0;
PyObject *ret = PyList_New(0);
while (!my_ioctl(self->fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival)) {
PyObject *cap = PyDict_New();
switch (frmival.type) {
case V4L2_FRMIVAL_TYPE_DISCRETE:
{
PyDict_SetItemString(cap, "interval",
PyFloat_FromDouble(fract2sec(&frmival.discrete)));
PyDict_SetItemString(cap, "fps",
PyFloat_FromDouble(fract2fps(&frmival.discrete)));
}
break;
case V4L2_FRMIVAL_TYPE_CONTINUOUS:
{
PyDict_SetItemString(cap, "interval_min",
PyFloat_FromDouble(fract2sec
(&frmival.stepwise.min)));
PyDict_SetItemString(cap, "interval_max",
PyFloat_FromDouble(fract2sec
(&frmival.stepwise.max)));
PyDict_SetItemString(cap, "fps_max",
PyFloat_FromDouble(fract2fps
(&frmival.stepwise.max)));
PyDict_SetItemString(cap, "fps_min",
PyFloat_FromDouble(fract2fps
(&frmival.stepwise.min)));
}
break;
case V4L2_FRMIVAL_TYPE_STEPWISE:
{
PyDict_SetItemString(cap, "interval_min",
PyFloat_FromDouble(fract2sec
(&frmival.stepwise.min)));
PyDict_SetItemString(cap, "interval_max",
PyFloat_FromDouble(fract2sec
(&frmival.stepwise.max)));
PyDict_SetItemString(cap, "interval_step",
PyFloat_FromDouble(fract2sec
(&frmival.stepwise.step)));
PyDict_SetItemString(cap, "fps_max",
PyFloat_FromDouble(fract2fps
(&frmival.stepwise.max)));
PyDict_SetItemString(cap, "fps_min",
PyFloat_FromDouble(fract2fps
(&frmival.stepwise.min)));
}
break;
}
PyList_Append(ret, cap);
frmival.index++;
}
PyErr_Clear();
return ret;
}
static PyObject *Video_device_set_focus_auto(
Video_device * self,
PyObject * args) {
#ifdef V4L2_CID_FOCUS_AUTO
int autofocus;
if (!PyArg_ParseTuple(args, "i", &autofocus)) {
return NULL;
}
struct v4l2_control ctrl;
CLEAR(ctrl);
ctrl.id = V4L2_CID_FOCUS_AUTO;
ctrl.value = autofocus;
if (my_ioctl(self->fd, VIDIOC_S_CTRL, &ctrl)) {
return NULL;
}
return Py_BuildValue("i", ctrl.value);
#else
return NULL;
#endif
}
static PyObject *Video_device_get_focus_auto(
Video_device * self) {
#ifdef V4L2_CID_FOCUS_AUTO
struct v4l2_control ctrl;
CLEAR(ctrl);
ctrl.id = V4L2_CID_FOCUS_AUTO;
if (my_ioctl(self->fd, VIDIOC_G_CTRL, &ctrl)) {
return NULL;
}
return Py_BuildValue("i", ctrl.value);
#else
return NULL;
#endif
}
static PyObject *Video_device_start(
Video_device * self) {
ASSERT_OPEN;
enum v4l2_buf_type type;
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (my_ioctl(self->fd, VIDIOC_STREAMON, &type)) {
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *Video_device_stop(
Video_device * self) {
ASSERT_OPEN;
enum v4l2_buf_type type;
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (my_ioctl(self->fd, VIDIOC_STREAMOFF, &type)) {
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *Video_device_create_buffers(
Video_device * self,
PyObject * args) {
int buffer_count;
if (!PyArg_ParseTuple(args, "I", &buffer_count)) {
return NULL;
}
ASSERT_OPEN;
if (self->buffers) {
PyErr_SetString(PyExc_ValueError, "Buffers are already created");
return NULL;
}
struct v4l2_requestbuffers reqbuf;
reqbuf.count = buffer_count;
reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
reqbuf.memory = V4L2_MEMORY_MMAP;
if (my_ioctl(self->fd, VIDIOC_REQBUFS, &reqbuf)) {
return NULL;
}
if (!reqbuf.count) {
PyErr_SetString(PyExc_IOError, "Not enough buffer memory");
return NULL;
}
self->buffers = malloc(reqbuf.count * sizeof(struct buffer));
if (!self->buffers) {
PyErr_NoMemory();
return NULL;
}
int i;
for (i = 0; i < reqbuf.count; i++) {
struct v4l2_buffer buffer;
buffer.index = i;
buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buffer.memory = V4L2_MEMORY_MMAP;
if (my_ioctl(self->fd, VIDIOC_QUERYBUF, &buffer)) {
return NULL;
}
self->buffers[i].length = buffer.length;
self->buffers[i].start = v4l2_mmap(NULL, buffer.length,
PROT_READ | PROT_WRITE, MAP_SHARED,
self->fd, buffer.m.offset);
if (self->buffers[i].start == MAP_FAILED) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
}
self->buffer_count = i;
Py_RETURN_NONE;
}
static PyObject *Video_device_queue_all_buffers(
Video_device * self) {
if (!self->buffers) {
ASSERT_OPEN;
PyErr_SetString(PyExc_ValueError, "Buffers have not been created");
return NULL;
}
int i;
int buffer_count = self->buffer_count;
for (i = 0; i < buffer_count; i++) {
struct v4l2_buffer buffer;
buffer.index = i;
buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buffer.memory = V4L2_MEMORY_MMAP;
if (my_ioctl(self->fd, VIDIOC_QBUF, &buffer)) {
return NULL;
}
}
Py_RETURN_NONE;
}
static PyObject *Video_device_read_internal(
Video_device * self,
int queue) {
if (!self->buffers) {
ASSERT_OPEN;
PyErr_SetString(PyExc_ValueError, "Buffers have not been created");
return NULL;
}
struct v4l2_buffer buffer;
buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buffer.memory = V4L2_MEMORY_MMAP;
if (my_ioctl(self->fd, VIDIOC_DQBUF, &buffer)) {
return NULL;
}
#ifdef USE_LIBV4L
# if PY_MAJOR_VERSION < 3
PyObject *result = PyString_FromStringAndSize(
# else
PyObject *result = PyBytes_FromStringAndSize(
# endif
self->buffers[buffer.index].
start, buffer.bytesused);
if (!result) {
return NULL;
}
#else
// Convert buffer from YUYV to RGB.
// For the byte order, see: http://v4l2spec.bytesex.org/spec/r4339.htm
// For the color conversion, see: http://v4l2spec.bytesex.org/spec/x2123.htm
int length = buffer.bytesused * 6 / 4;
# if PY_MAJOR_VERSION < 3
PyObject *result = PyString_FromStringAndSize(NULL, length);
# else
PyObject *result = PyBytes_FromStringAndSize(NULL, length);
# endif
if (!result) {
return NULL;
}
char *rgb = PyString_AS_STRING(result);
char *rgb_max = rgb + length;
unsigned char *yuyv = self->buffers[buffer.index].start;
# define CLAMP(c) ((c) <= 0 ? 0 : (c) >= 65025 ? 255 : (c) >> 8)
while (rgb < rgb_max) {
int u = yuyv[1] - 128;
int v = yuyv[3] - 128;
int uv = 100 * u + 208 * v;
u *= 516;
v *= 409;
int y = 298 * (yuyv[0] - 16);
rgb[0] = CLAMP(y + v);
rgb[1] = CLAMP(y - uv);
rgb[2] = CLAMP(y + u);
y = 298 * (yuyv[2] - 16);
rgb[3] = CLAMP(y + v);
rgb[4] = CLAMP(y - uv);
rgb[5] = CLAMP(y + u);
rgb += 6;
yuyv += 4;
}
# undef CLAMP
#endif
if (queue && my_ioctl(self->fd, VIDIOC_QBUF, &buffer)) {
return NULL;
}
return result;
}
static PyObject *Video_device_read(
Video_device * self) {
return Video_device_read_internal(self, 0);
}
static PyObject *Video_device_read_and_queue(
Video_device * self) {
return Video_device_read_internal(self, 1);
}
static PyMethodDef Video_device_methods[] = {
{"close", (PyCFunction) Video_device_close, METH_NOARGS,
"close()\n\n"
"Close video device. Subsequent calls to other methods will fail."},
{"fileno", (PyCFunction) Video_device_fileno, METH_NOARGS,
"fileno() -> integer \"file descriptor\".\n\n"
"This enables video devices to be passed select.select for waiting "
"until a frame is available for reading."},
{"get_info", (PyCFunction) Video_device_get_info, METH_NOARGS,
"get_info() -> driver, card, bus_info, capabilities\n\n"
"Returns three strings with information about the video device, and one "
"set containing strings identifying the capabilities of the video "
"device."},
{"get_fourcc", (PyCFunction) Video_device_get_fourcc, METH_VARARGS,
"get_fourcc(fourcc_string) -> fourcc_int\n\n"
"Return the fourcc string encoded as int."},
{"get_format", (PyCFunction) Video_device_get_format, METH_NOARGS,
"get_format() -> size_x, size_y, fourcc\n\n"
"Request the current video format."},
{"set_format", (PyCFunction) Video_device_set_format,
METH_VARARGS | METH_KEYWORDS,
"set_format(size_x, size_y, yuv420 = 0, fourcc='MJPEG') -> size_x, size_y\n\n"
"Request the video device to set image size and format. The device may "
"choose another size than requested and will return its choice. The "
"image format will be RGB24 if yuv420 is zero (default) or YUV420 if "
"yuv420 is 1, if fourcc keyword is set that will be the fourcc pixel format used."},
{"set_fps", (PyCFunction) Video_device_set_fps, METH_VARARGS,
"set_fps(fps) -> fps \n\n"
"Request the video device to set frame per seconds.The device may "
"choose another frame rate than requested and will return its choice. "},
{"set_auto_white_balance",
(PyCFunction) Video_device_set_auto_white_balance, METH_VARARGS,
"set_auto_white_balance(autowb) -> autowb \n\n"
"Request the video device to set auto white balance to value. The device may "
"choose another value than requested and will return its choice. "},
{"get_auto_white_balance",
(PyCFunction) Video_device_get_auto_white_balance, METH_NOARGS,
"get_auto_white_balance() -> autowb \n\n"
"Request the video device to get auto white balance value. "},
{"set_white_balance_temperature",
(PyCFunction) Video_device_set_white_balance_temperature, METH_VARARGS,
"set_white_balance_temperature(temp) -> temp \n\n"
"Request the video device to set white balance tempature to value. The device may "
"choose another value than requested and will return its choice. "},
{"get_white_balance_temperature",
(PyCFunction) Video_device_get_white_balance_temperature, METH_NOARGS,
"get_white_balance_temperature() -> temp \n\n"
"Request the video device to get white balance temperature value. "},
{"set_exposure_auto", (PyCFunction) Video_device_set_exposure_auto,
METH_VARARGS,
"set_exposure_auto(autoexp) -> autoexp \n\n"
"Request the video device to set auto exposure to value. The device may "
"choose another value than requested and will return its choice. "},
{"get_exposure_auto", (PyCFunction) Video_device_get_exposure_auto,
METH_NOARGS,
"get_exposure_auto() -> autoexp \n\n"
"Request the video device to get auto exposure value. "},
{"set_exposure_absolute", (PyCFunction) Video_device_set_exposure_absolute,
METH_VARARGS,
"set_exposure_absolute(exptime) -> exptime \n\n"
"Request the video device to set exposure time to value. The device may "
"choose another value than requested and will return its choice. "},
{"get_exposure_absolute", (PyCFunction) Video_device_get_exposure_absolute,
METH_NOARGS,
"get_exposure_absolute() -> exptime \n\n"
"Request the video device to get exposure time value. "},
{"set_focus_auto", (PyCFunction) Video_device_set_focus_auto, METH_VARARGS,
"set_auto_focus_auto(autofocus) -> autofocus \n\n"
"Request the video device to set auto focuse on or off. The device may "
"choose another value than requested and will return its choice. "},
{"get_focus_auto", (PyCFunction) Video_device_get_focus_auto, METH_NOARGS,
"get_focus_auto() -> autofocus \n\n"
"Request the video device to get auto focus value. "},
{"get_framesizes", (PyCFunction) Video_device_get_framesizes, METH_VARARGS,
"get_framesizes() -> framesizes \n\n"
"Request the framesizes suported by the device. "},
{"get_frameintervals", (PyCFunction) Video_device_get_frameintervals,
METH_VARARGS,
"get_frameintervals() -> frameintervals \n\n"
"Request the frameintervals suported by the device. "},
{"start", (PyCFunction) Video_device_start, METH_NOARGS,
"start()\n\n" "Start video capture."},
{"stop", (PyCFunction) Video_device_stop, METH_NOARGS,
"stop()\n\n" "Stop video capture."},
{"create_buffers", (PyCFunction) Video_device_create_buffers, METH_VARARGS,
"create_buffers(count)\n\n"
"Create buffers used for capturing image data. Can only be called once "
"for each video device object."},
{"queue_all_buffers", (PyCFunction) Video_device_queue_all_buffers,
METH_NOARGS,
"queue_all_buffers()\n\n"
"Let the video device fill all buffers created."},
{"read", (PyCFunction) Video_device_read, METH_NOARGS,
"read() -> string\n\n"
"Reads image data from a buffer that has been filled by the video "
"device. The image data is in RGB och YUV420 format as decided by "
"'set_format'. The buffer is removed from the queue. Fails if no buffer "
"is filled. Use select.select to check for filled buffers."},
{"read_and_queue", (PyCFunction) Video_device_read_and_queue, METH_NOARGS,
"read_and_queue()\n\n"
"Same as 'read', but adds the buffer back to the queue so the video "
"device can fill it again."},
{NULL}
};
static PyTypeObject Video_device_type = {
#if PY_MAJOR_VERSION < 3
PyObject_HEAD_INIT(NULL) 0,
#else
PyVarObject_HEAD_INIT(NULL, 0)
#endif
"v4l2capture.Video_device", sizeof(Video_device), 0,
(destructor) Video_device_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, Py_TPFLAGS_DEFAULT, "Video_device(path)\n\nOpens the video device at "
"the given path and returns an object that can capture images. The "
"constructor and all methods except close may raise IOError.", 0, 0, 0,
0, 0, 0, Video_device_methods, 0, 0, 0, 0, 0, 0, 0,
(initproc) Video_device_init
};
static PyMethodDef module_methods[] = {
{NULL}
};
#if PY_MAJOR_VERSION < 3
PyMODINIT_FUNC initv4l2capture(
void)
#else
PyMODINIT_FUNC PyInit_v4l2capture(
void)
#endif
{
Video_device_type.tp_new = PyType_GenericNew;
if (PyType_Ready(&Video_device_type) < 0) {
#if PY_MAJOR_VERSION < 3
return;
#else
return NULL;
#endif
}
PyObject *module;
#if PY_MAJOR_VERSION < 3
module = Py_InitModule3("v4l2capture", module_methods,
"Capture video with video4linux2.");
#else
static struct PyModuleDef moduledef = {