forked from umlaeute/v4l2loopback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v4l2loopback.c
1942 lines (1643 loc) · 49.3 KB
/
v4l2loopback.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
/*
* v4l2loopback.c -- video4linux2 loopback driver
*
* Copyright (C) 2005-2009 Vasily Levin ([email protected])
* Copyright (C) 2010-2011 IOhannes m zmoelnig ([email protected])
* Copyright (C) 2011 Stefan Diewald ([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.
*
*/
#include <linux/version.h>
#include <linux/vmalloc.h>
#include <linux/mm.h>
#include <linux/time.h>
#include <linux/module.h>
#include <linux/videodev2.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-common.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,29)
# define v4l2_file_operations file_operations
#endif
#include <linux/sched.h>
#include <linux/slab.h>
#define V4L2LOOPBACK_VERSION_CODE KERNEL_VERSION(0,5,0)
MODULE_DESCRIPTION("V4L2 loopback video device");
MODULE_AUTHOR("Vasily Levin, IOhannes m zmoelnig <[email protected]>, Stefan Diewald <[email protected]>");
MODULE_LICENSE("GPL");
/* helpers */
#define STRINGIFY(s) #s
#define STRINGIFY2(s) STRINGIFY(s)
#define dprintk(fmt, args...) \
do { if (debug > 0) { \
printk(KERN_INFO "v4l2-loopback[" STRINGIFY2(__LINE__) "]: " fmt, ##args); \
} } while(0)
#define MARK() \
do{ if (debug > 1) { \
printk(KERN_INFO "%s:%d[%s]", __FILE__, __LINE__, __FUNCTION__); \
} } while (0)
#define dprintkrw(fmt, args...) \
do { if (debug > 2) { \
printk(KERN_INFO "v4l2-loopback[" STRINGIFY2(__LINE__)"]: " fmt, ##args); \
} } while (0)
/* module constants */
/* module parameters */
static int debug = 0;
module_param(debug, int, S_IRUGO|S_IWUSR );
MODULE_PARM_DESC(debug, "if debug output is enabled, values are 0, 1 or 2");
#define MAX_BUFFERS 32 /* max buffers that can be mapped, actually they
* are all mapped to max_buffers buffers */
static int max_buffers = 8;
module_param(max_buffers, int, S_IRUGO);
MODULE_PARM_DESC(max_buffers, "how many buffers should be allocated");
/* how many times a device can be opened
* the per-module default value can be overridden on a per-device basis using
* the /sys/devices interface
*
* note that max_openers should be at least 2 in order to get a working system:
* one opener for the producer and one opener for the consumer
* however, we leave that to the user
*/
static int max_openers = 10;
module_param(max_openers, int, S_IRUGO|S_IWUSR);
MODULE_PARM_DESC(max_openers, "how many users can open loopback device");
#define MAX_DEVICES 8
static int devices = -1;
module_param(devices, int, 0);
MODULE_PARM_DESC(devices, "how many devices should be created");
static int video_nr[MAX_DEVICES] = { [0 ... (MAX_DEVICES-1)] = -1 };
module_param_array(video_nr, int, NULL, 0444);
MODULE_PARM_DESC(video_nr, "video device numbers (-1=auto, 0=/dev/video0, etc.)");
/* format specifications */
#define V4L2LOOPBACK_SIZE_MIN_WIDTH 48
#define V4L2LOOPBACK_SIZE_MIN_HEIGHT 32
#define V4L2LOOPBACK_SIZE_MAX_WIDTH 8192
#define V4L2LOOPBACK_SIZE_MAX_HEIGHT 8192
#define V4L2LOOPBACK_SIZE_DEFAULT_WIDTH 640
#define V4L2LOOPBACK_SIZE_DEFAULT_HEIGHT 480
/* module structures */
struct v4l2loopback_private {
int devicenr;
};
typedef struct v4l2loopback_private *priv_ptr;
/* TODO(vasaka) use typenames which are common to kernel, but first find out if
* it is needed */
/* struct keeping state and settings of loopback device */
struct v4l2l_buffer {
struct v4l2_buffer buffer;
int use_count;
};
struct v4l2_loopback_device {
struct video_device *vdev;
/* pixel and stream format */
struct v4l2_pix_format pix_format;
struct v4l2_captureparm capture_param;
/* buffers stuff */
u8 *image; /* pointer to actual buffers data */
unsigned long int imagesize; /* size of buffers data */
int buffers_number; /* should not be big, 4 is a good choice */
struct v4l2l_buffer buffers[MAX_BUFFERS]; /* inner driver buffers */
int used_buffers; /* number of the actually used buffers */
int max_openers; /* how many times can this device be opened */
int write_position; /* number of last written frame + 1 */
long buffer_size;
/* sync stuff */
atomic_t open_count;
int ready_for_capture;/* set to true when at least one writer opened
* device and negotiated format */
wait_queue_head_t read_event;
};
/* types of opener shows what opener wants to do with loopback */
enum opener_type {
UNNEGOTIATED = 0,
READER = 1,
WRITER = 2,
};
/* struct keeping state and type of opener */
struct v4l2_loopback_opener {
enum opener_type type;
int vidioc_enum_frameintervals_calls;
int read_position; /* number of last processed frame + 1 or
* write_position - 1 if reader went out of sync */
struct v4l2_buffer *buffers;
int buffers_number; /* should not be big, 4 is a good choice */
};
/* this is heavily inspired by the bttv driver found in the linux kernel */
struct v4l2l_format {
char *name;
int fourcc; /* video4linux 2 */
int depth; /* bit/pixel */
int flags;
};
/* set the v4l2l_format.flags to PLANAR for non-packed formats */
#define FORMAT_FLAGS_PLANAR 0x01
static const struct v4l2l_format formats[] = {
/* here come the packed formats */
{
.name = "32 bpp RGB, le",
.fourcc = V4L2_PIX_FMT_BGR32,
.depth = 32,
.flags = 0,
},{
.name = "32 bpp RGB, be",
.fourcc = V4L2_PIX_FMT_RGB32,
.depth = 32,
.flags = 0,
},{
.name = "24 bpp RGB, le",
.fourcc = V4L2_PIX_FMT_BGR24,
.depth = 24,
.flags = 0,
},{
.name = "24 bpp RGB, be",
.fourcc = V4L2_PIX_FMT_RGB24,
.depth = 24,
.flags = 0,
},{
.name = "4:2:2, packed, YUYV",
.fourcc = V4L2_PIX_FMT_YUYV,
.depth = 16,
.flags = 0,
},{
.name = "4:2:2, packed, YUYV",
.fourcc = V4L2_PIX_FMT_YUYV,
.depth = 16,
.flags = 0,
},{
.name = "4:2:2, packed, UYVY",
.fourcc = V4L2_PIX_FMT_UYVY,
.depth = 16,
.flags = 0,
},{
#ifdef V4L2_PIX_FMT_YVYU
.name = "4:2:2, packed YVYU",
.fourcc = V4L2_PIX_FMT_YVYU,
.depth = 16,
.flags=0,
},{
#endif
#ifdef V4L2_PIX_FMT_VYUY
.name = "4:2:2, packed VYUY",
.fourcc = V4L2_PIX_FMT_VYUY,
.depth = 16,
.flags=0,
},{
#endif
.name = "4:2:2, packed YYUV",
.fourcc = V4L2_PIX_FMT_YYUV,
.depth = 16,
.flags=0,
},{
.name = "YUV-8-8-8-8",
.fourcc = V4L2_PIX_FMT_YUV32,
.depth = 32,
.flags = 0,
},{
.name = "8 bpp, gray",
.fourcc = V4L2_PIX_FMT_GREY,
.depth = 8,
.flags = 0,
},{
.name = "16 Greyscale",
.fourcc = V4L2_PIX_FMT_Y16,
.depth = 16,
.flags = 0,
},
/* here come the planar formats */
{
.name = "4:1:0, planar, Y-Cr-Cb",
.fourcc = V4L2_PIX_FMT_YVU410,
.depth = 9,
.flags = FORMAT_FLAGS_PLANAR,
},{
.name = "4:2:0, planar, Y-Cr-Cb",
.fourcc = V4L2_PIX_FMT_YVU420,
.depth = 12,
.flags = FORMAT_FLAGS_PLANAR,
},{
.name = "4:1:0, planar, Y-Cb-Cr",
.fourcc = V4L2_PIX_FMT_YUV410,
.depth = 9,
.flags = FORMAT_FLAGS_PLANAR,
},{
.name = "4:2:0, planar, Y-Cb-Cr",
.fourcc = V4L2_PIX_FMT_YUV420,
.depth = 12,
.flags = FORMAT_FLAGS_PLANAR,
}
};
static const unsigned int FORMATS = ARRAY_SIZE(formats);
static char*
fourcc2str (unsigned int fourcc,
char buf[4])
{
buf[0]=(fourcc>> 0) & 0xFF;
buf[1]=(fourcc>> 8) & 0xFF;
buf[2]=(fourcc>>16) & 0xFF;
buf[3]=(fourcc>>24) & 0xFF;
return buf;
}
static const struct v4l2l_format*
format_by_fourcc (int fourcc)
{
unsigned int i;
for (i = 0; i < FORMATS; i++) {
if (formats[i].fourcc == fourcc)
return formats+i; }
dprintk("unsupported format '%c%c%c%c'",
(fourcc>> 0) & 0xFF,
(fourcc>> 8) & 0xFF,
(fourcc>>16) & 0xFF,
(fourcc>>24) & 0xFF);
return NULL;
}
static void
pix_format_set_size (struct v4l2_pix_format * f,
const struct v4l2l_format * fmt,
unsigned int width,
unsigned int height)
{
f->width = width;
f->height = height;
if (fmt->flags & FORMAT_FLAGS_PLANAR) {
f->bytesperline = width; /* Y plane */
f->sizeimage = (width * height * fmt->depth) >> 3;
} else {
f->bytesperline = (width * fmt->depth) >> 3;
f->sizeimage = height * f->bytesperline;
}
}
static struct v4l2_loopback_device*v4l2loopback_cd2dev (struct device*cd);
/* device attributes */
/* available via sysfs: /sys/devices/virtual/video4linux/video* */
static ssize_t attr_show_fourcc(struct device *cd,
struct device_attribute *attr,
char *buf)
{
struct v4l2_loopback_device *dev = v4l2loopback_cd2dev(cd);
char buf4cc[5];
if(!dev)return 0;
fourcc2str(dev->pix_format.pixelformat, buf4cc);
return sprintf(buf, "%.*s\n", 4, buf4cc);
}
static DEVICE_ATTR(fourcc, S_IRUGO, attr_show_fourcc, NULL);
static ssize_t attr_show_buffers(struct device *cd,
struct device_attribute *attr,
char *buf)
{
struct v4l2_loopback_device *dev = v4l2loopback_cd2dev(cd);
return sprintf(buf, "%d\n", dev->used_buffers);
}
static DEVICE_ATTR(buffers, S_IRUGO, attr_show_buffers, NULL);
static ssize_t attr_show_maxopeners(struct device *cd,
struct device_attribute *attr,
char *buf)
{
struct v4l2_loopback_device *dev = v4l2loopback_cd2dev(cd);
return sprintf(buf, "%d\n", dev->max_openers);
}
static ssize_t attr_store_maxopeners(struct device* cd,
struct device_attribute *attr,
const char* buf, size_t len)
{
struct v4l2_loopback_device *dev = NULL;
unsigned long curr=0;
if (strict_strtoul(buf, 0, &curr))
return -EINVAL;
dev = v4l2loopback_cd2dev(cd);
if (dev->max_openers == curr)
return len;
if (dev->open_count.counter > curr) {
/* request to limit to less openers as are currently attached to us */
return -EINVAL;
}
dev->max_openers = (int)curr;
return len;
}
static DEVICE_ATTR(max_openers, S_IRUGO | S_IWUSR, attr_show_maxopeners, attr_store_maxopeners);
static void v4l2loopback_remove_sysfs(struct video_device *vdev)
{
#define V4L2_SYSFS_DESTROY(x) device_remove_file(&vdev->dev, &dev_attr_##x)
if (vdev) {
V4L2_SYSFS_DESTROY(fourcc);
V4L2_SYSFS_DESTROY(buffers);
V4L2_SYSFS_DESTROY(max_openers);
/* ... */
}
}
static void v4l2loopback_create_sysfs(struct video_device *vdev)
{
int res=0;
#define V4L2_SYSFS_CREATE(x) res = device_create_file(&vdev->dev, &dev_attr_##x); if (res < 0) break
if (!vdev) return;
do {
V4L2_SYSFS_CREATE(fourcc);
V4L2_SYSFS_CREATE(buffers);
V4L2_SYSFS_CREATE(max_openers);
/* ... */
} while(0);
if (res >= 0)return;
dev_err(&vdev->dev, "%s error: %d\n", __func__, res);
}
/* global module data */
struct v4l2_loopback_device *devs[MAX_DEVICES];
static struct v4l2_loopback_device*
v4l2loopback_cd2dev (struct device*cd)
{
struct video_device *loopdev = to_video_device(cd);
priv_ptr ptr = (priv_ptr)video_get_drvdata(loopdev);
int nr = ptr->devicenr;
if(nr<0 || nr>=devices){printk(KERN_ERR "v4l2-loopback: illegal device %d\n",nr);return NULL;}
return devs[nr];
}
static struct v4l2_loopback_device*
v4l2loopback_getdevice (struct file*f)
{
struct video_device *loopdev = video_devdata(f);
priv_ptr ptr = (priv_ptr)video_get_drvdata(loopdev);
int nr = ptr->devicenr;
if(nr<0 || nr>=devices){printk(KERN_ERR "v4l2-loopback: illegal device %d\n",nr);return NULL;}
return devs[nr];
}
/* forward declarations */
static void init_buffers(struct v4l2_loopback_device *dev);
static int allocate_buffers(struct v4l2_loopback_device *dev);
static int free_buffers(struct v4l2_loopback_device *dev);
static const struct v4l2_file_operations v4l2_loopback_fops;
static const struct v4l2_ioctl_ops v4l2_loopback_ioctl_ops;
/* Queue helpers */
/* next functions sets buffer flags and adjusts counters accordingly */
static inline void
set_done (struct v4l2l_buffer *buffer)
{
buffer->buffer.flags &= ~V4L2_BUF_FLAG_QUEUED;
buffer->buffer.flags |= V4L2_BUF_FLAG_DONE;
}
static inline void
set_queued (struct v4l2l_buffer *buffer)
{
buffer->buffer.flags &= ~V4L2_BUF_FLAG_DONE;
buffer->buffer.flags |= V4L2_BUF_FLAG_QUEUED;
}
static inline void
unset_flags (struct v4l2l_buffer *buffer)
{
buffer->buffer.flags &= ~V4L2_BUF_FLAG_QUEUED;
buffer->buffer.flags &= ~V4L2_BUF_FLAG_DONE;
}
/* V4L2 ioctl caps and params calls */
/* returns device capabilities
* called on VIDIOC_QUERYCAP
*/
static int
vidioc_querycap (struct file *file,
void *priv,
struct v4l2_capability *cap)
{
strlcpy(cap->driver, "v4l2 loopback", sizeof(cap->driver));
strlcpy(cap->card, "Dummy video device", sizeof(cap->card));
cap->bus_info[0]=0;
cap->version = V4L2LOOPBACK_VERSION_CODE;
cap->capabilities =
V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
V4L2_CAP_STREAMING |
V4L2_CAP_READWRITE
;
memset(cap->reserved, 0, sizeof(cap->reserved));
return 0;
}
static int
vidioc_enum_framesizes (struct file *file, void *fh,
struct v4l2_frmsizeenum *argp)
{
struct v4l2_loopback_device *dev;
/* LATER: what does the index really mean?
* if it's about enumerating formats, we can safely ignore it
* (CHECK)
*/
/* there can be only one... */
if (argp->index)
return -EINVAL;
dev=v4l2loopback_getdevice(file);
if (dev->ready_for_capture) {
/* format has already been negotiated
* cannot change during runtime
*/
argp->type=V4L2_FRMSIZE_TYPE_DISCRETE;
argp->discrete.width=dev->pix_format.width;
argp->discrete.height=dev->pix_format.height;
} else {
/* if the format has not been negotiated yet, we accept anything
*/
argp->type=V4L2_FRMSIZE_TYPE_CONTINUOUS;
argp->stepwise.min_width=V4L2LOOPBACK_SIZE_MIN_WIDTH;
argp->stepwise.min_height=V4L2LOOPBACK_SIZE_MIN_HEIGHT;
argp->stepwise.max_width=V4L2LOOPBACK_SIZE_MAX_WIDTH;
argp->stepwise.max_height=V4L2LOOPBACK_SIZE_MAX_HEIGHT;
argp->stepwise.step_width=1;
argp->stepwise.step_height=1;
}
return 0;
}
/* returns frameinterval (fps) for the set resolution
* called on VIDIOC_ENUM_FRAMEINTERVALS
*/
static int
vidioc_enum_frameintervals(struct file *file,
void *fh,
struct v4l2_frmivalenum *argp)
{
struct v4l2_loopback_device *dev = v4l2loopback_getdevice(file);
struct v4l2_loopback_opener *opener = file->private_data;
if (dev->ready_for_capture) {
if (opener->vidioc_enum_frameintervals_calls > 0)
return -EINVAL;
if (argp->width == dev->pix_format.width &&
argp->height== dev->pix_format.height)
{
argp->type = V4L2_FRMIVAL_TYPE_DISCRETE;
argp->discrete = dev->capture_param.timeperframe;
opener->vidioc_enum_frameintervals_calls++;
return 0;
} else {
return -EINVAL;
}
}
return 0;
}
/* ------------------ CAPTURE ----------------------- */
/* returns device formats
* called on VIDIOC_ENUM_FMT, with v4l2_buf_type set to V4L2_BUF_TYPE_VIDEO_CAPTURE
*/
static int
vidioc_enum_fmt_cap (struct file *file,
void *fh,
struct v4l2_fmtdesc *f)
{
struct v4l2_loopback_device *dev;
MARK();
dev=v4l2loopback_getdevice(file);
if (f->index)
return -EINVAL;
if (dev->ready_for_capture) {
const __u32 format = dev->pix_format.pixelformat;
// strlcpy(f->description, "current format", sizeof(f->description));
snprintf(f->description, sizeof(f->description),
"[%c%c%c%c]",
(format>> 0) & 0xFF,
(format>> 8) & 0xFF,
(format>>16) & 0xFF,
(format>>24) & 0xFF);
f->pixelformat = dev->pix_format.pixelformat;
} else {
return -EINVAL;
}
f->flags=0;
MARK();
return 0;
}
/* returns current video format format fmt
* called on VIDIOC_G_FMT, with v4l2_buf_type set to V4L2_BUF_TYPE_VIDEO_CAPTURE
*/
static int
vidioc_g_fmt_cap (struct file *file,
void *priv,
struct v4l2_format *fmt)
{
struct v4l2_loopback_device *dev;
MARK();
dev=v4l2loopback_getdevice(file);
if (!dev->ready_for_capture) {
return -EINVAL;
}
fmt->fmt.pix = dev->pix_format;
MARK();
return 0;
}
/* checks if it is OK to change to format fmt;
* actual check is done by inner_try_fmt_cap
* just checking that pixelformat is OK and set other parameters, app should
* obey this decision
* called on VIDIOC_TRY_FMT, with v4l2_buf_type set to V4L2_BUF_TYPE_VIDEO_CAPTURE
*/
static int
vidioc_try_fmt_cap (struct file *file,
void *priv,
struct v4l2_format *fmt)
{
struct v4l2_loopback_opener *opener;
struct v4l2_loopback_device *dev;
opener = file->private_data;
opener->type = READER;
dev=v4l2loopback_getdevice(file);
if (0 == dev->ready_for_capture) {
dprintk("setting fmt_cap not possible yet\n");
return -EBUSY;
}
if (fmt->fmt.pix.pixelformat != dev->pix_format.pixelformat)
return -EINVAL;
fmt->fmt.pix = dev->pix_format;
do { char buf[5]; buf[4]=0; dprintk("capFOURCC=%s\n", fourcc2str(dev->pix_format.pixelformat, buf)); } while(0);
return 0;
}
/* sets new output format, if possible
* actually format is set by input and we even do not check it, just return
* current one, but it is possible to set subregions of input TODO(vasaka)
* called on VIDIOC_S_FMT, with v4l2_buf_type set to V4L2_BUF_TYPE_VIDEO_CAPTURE
*/
static int
vidioc_s_fmt_cap (struct file *file,
void *priv,
struct v4l2_format *fmt)
{
return vidioc_try_fmt_cap(file, priv, fmt);
}
/* ------------------ OUTPUT ----------------------- */
/* returns device formats;
* LATER: allow all formats
* called on VIDIOC_ENUM_FMT, with v4l2_buf_type set to V4L2_BUF_TYPE_VIDEO_OUTPUT
*/
static int
vidioc_enum_fmt_out (struct file *file,
void *fh,
struct v4l2_fmtdesc *f)
{
struct v4l2_loopback_device *dev;
const struct v4l2l_format * fmt;
dev=v4l2loopback_getdevice(file);
if (dev->ready_for_capture) {
const __u32 format = dev->pix_format.pixelformat;
/* format has been fixed by the writer, so only one single format is supported */
if (f->index)
return -EINVAL;
fmt=format_by_fourcc(format);
if(NULL == fmt)
return -EINVAL;
f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
// f->flags = ??;
snprintf(f->description, sizeof(f->description),
fmt->name);
f->pixelformat = dev->pix_format.pixelformat;
} else {
__u32 format;
/* fill in a dummy format */
if(f->index < 0 ||
f->index >= FORMATS)
return -EINVAL;
fmt=&formats[f->index];
f->pixelformat=fmt->fourcc;
format = f->pixelformat;
// strlcpy(f->description, "dummy OUT format", sizeof(f->description));
snprintf(f->description, sizeof(f->description),
fmt->name);
}
f->flags=0;
return 0;
}
/* returns current video format format fmt */
/* NOTE: this is called from the producer
* so if format has not been negotiated yet,
* it should return ALL of available formats,
* called on VIDIOC_G_FMT, with v4l2_buf_type set to V4L2_BUF_TYPE_VIDEO_OUTPUT
*/
static int
vidioc_g_fmt_out (struct file *file,
void *priv,
struct v4l2_format *fmt)
{
struct v4l2_loopback_device *dev;
struct v4l2_loopback_opener *opener;
MARK();
dev=v4l2loopback_getdevice(file);
opener = file->private_data;
opener->type = WRITER;
/*
* LATER: this should return the currently valid format
* gstreamer doesn't like it, if this returns -EINVAL, as it
* then concludes that there is _no_ valid format
* CHECK whether this assumption is wrong,
* or whether we have to always provide a valid format
*/
if (0 == dev->ready_for_capture) {
/* we are not fixated yet, so return a default format */
const struct v4l2l_format * defaultfmt=&formats[0];
dev->pix_format.width=0; /* V4L2LOOPBACK_SIZE_DEFAULT_WIDTH; */
dev->pix_format.height=0; /* V4L2LOOPBACK_SIZE_DEFAULT_HEIGHT; */
dev->pix_format.pixelformat=defaultfmt->fourcc;
dev->pix_format.colorspace=V4L2_COLORSPACE_SRGB; /* do we need to set this ? */
dev->pix_format.field=V4L2_FIELD_NONE;
pix_format_set_size(&fmt->fmt.pix, defaultfmt,
dev->pix_format.width, dev->pix_format.height);
dev->buffer_size = PAGE_ALIGN(dev->pix_format.sizeimage);
allocate_buffers(dev);
}
fmt->fmt.pix = dev->pix_format;
return 0;
}
/* checks if it is OK to change to format fmt;
* if format is negotiated do not change it
* called on VIDIOC_TRY_FMT with v4l2_buf_type set to V4L2_BUF_TYPE_VIDEO_OUTPUT
*/
static int
vidioc_try_fmt_out (struct file *file,
void *priv,
struct v4l2_format *fmt)
{
struct v4l2_loopback_opener *opener;
struct v4l2_loopback_device *dev;
MARK();
opener = file->private_data;
opener->type = WRITER;
dev=v4l2loopback_getdevice(file);
/* TODO(vasaka) loopback does not care about formats writer want to set,
* maybe it is a good idea to restrict format somehow */
if (dev->ready_for_capture) {
fmt->fmt.pix = dev->pix_format;
} else {
__u32 w=fmt->fmt.pix.width;
__u32 h=fmt->fmt.pix.height;
__u32 pixfmt=fmt->fmt.pix.pixelformat;
const struct v4l2l_format*format=format_by_fourcc(pixfmt);
if(w<1)
w=V4L2LOOPBACK_SIZE_DEFAULT_WIDTH;
if(h<1)
h=V4L2LOOPBACK_SIZE_DEFAULT_HEIGHT;
if(NULL==format) {
format=&formats[0];
}
pix_format_set_size(&fmt->fmt.pix, format, w, h);
fmt->fmt.pix.pixelformat = format->fourcc;
fmt->fmt.pix.colorspace=V4L2_COLORSPACE_SRGB;
if(V4L2_FIELD_ANY == fmt->fmt.pix.field)
fmt->fmt.pix.field=V4L2_FIELD_NONE;
/* FIXXME: try_fmt should never modify the device-state */
dev->pix_format = fmt->fmt.pix;
}
return 0;
}
/* sets new output format, if possible;
* allocate data here because we do not know if it will be streaming or
* read/write IO
* called on VIDIOC_S_FMT with v4l2_buf_type set to V4L2_BUF_TYPE_VIDEO_OUTPUT
*/
static int
vidioc_s_fmt_out (struct file *file,
void *priv, struct v4l2_format *fmt)
{
struct v4l2_loopback_device *dev;
int ret;
MARK();
dev=v4l2loopback_getdevice(file);
ret = vidioc_try_fmt_out(file, priv, fmt);
dprintk("s_fmt_out(%d) %d...%d", ret, dev->ready_for_capture, dev->pix_format.sizeimage);
do {
char buf[5];
buf[4]=0;
dprintk("outFOURCC=%s\n", fourcc2str(dev->pix_format.pixelformat, buf));
} while(0);
if (ret < 0)
return ret;
if (!dev->ready_for_capture) {
dev->buffer_size = PAGE_ALIGN(dev->pix_format.sizeimage);
fmt->fmt.pix.sizeimage = dev->buffer_size;
allocate_buffers(dev);
}
return ret;
}
//#define V4L2L_OVERLAY
#ifdef V4L2L_OVERLAY
/* ------------------ OVERLAY ----------------------- */
/* currently unsupported */
/* GSTreamer's v4l2sink is buggy, as it requires the overlay to work
* while it should only require it, if overlay is requested
* once the gstreamer element is fixed, remove the overlay dummies
*/
#warning OVERLAY dummies
static int
vidioc_g_fmt_overlay(struct file *file,
void *priv,
struct v4l2_format *fmt)
{
return 0;
}
static int
vidioc_s_fmt_overlay(struct file *file,
void *priv,
struct v4l2_format *fmt)
{
return 0;
}
#endif /* V4L2L_OVERLAY */
/* ------------------ PARAMs ----------------------- */
/* get some data flow parameters, only capability, fps and readbuffers has
* effect on this driver
* called on VIDIOC_G_PARM
*/
static int
vidioc_g_parm (struct file *file,
void *priv,
struct v4l2_streamparm *parm)
{
/* do not care about type of opener, hope this enums would always be
* compatible */
struct v4l2_loopback_device *dev;
MARK();
dev=v4l2loopback_getdevice(file);
parm->parm.capture = dev->capture_param;
return 0;
}
/* get some data flow parameters, only capability, fps and readbuffers has
* effect on this driver
* called on VIDIOC_S_PARM
*/
static int
vidioc_s_parm (struct file *file,
void *priv,
struct v4l2_streamparm *parm)
{
struct v4l2_loopback_device *dev;
MARK();
dev=v4l2loopback_getdevice(file);
dprintk("vidioc_s_parm called frate=%d/%d\n",
parm->parm.capture.timeperframe.numerator,
parm->parm.capture.timeperframe.denominator);
switch (parm->type) {
case V4L2_BUF_TYPE_VIDEO_CAPTURE:
dev->capture_param.timeperframe=parm->parm.capture.timeperframe;
break;
case V4L2_BUF_TYPE_VIDEO_OUTPUT:
dev->capture_param.timeperframe=parm->parm.capture.timeperframe;
break;
default:
return -1;
}
parm->parm.capture = dev->capture_param;
return 0;
}
/* sets a tv standard, actually we do not need to handle this any special way
* added to support effecttv
* called on VIDIOC_S_STD
*/
static int
vidioc_s_std (struct file *file,
void *private_data,
v4l2_std_id *_std)
{
v4l2_std_id req_std=0, supported_std=0;
const v4l2_std_id all_std=V4L2_STD_ALL, no_std=0;
if(_std) {
req_std=*_std;
*_std=all_std;
}
/* we support everything in V4L2_STD_ALL, but not more... */
supported_std=(all_std & req_std);
if(no_std == supported_std) {
return -EINVAL;
}
return 0;
}
/* gets a fake video standard
* called on VIDIOC_G_STD
*/
static int
vidioc_g_std (struct file *file,
void *private_data,
v4l2_std_id *norm)
{
if(norm)
*norm=V4L2_STD_ALL;
return 0;
}
/* gets a fake video standard
* called on VIDIOC_QUERYSTD
*/
static int
vidioc_querystd (struct file *file,
void *private_data,
v4l2_std_id *norm)
{
if(norm)
*norm=V4L2_STD_ALL;
return 0;
}
/* dummy queyr control
* called on VIDIOC_QUERYCTRL
*/
static int
vidioc_queryctrl(struct file *file, void *fh,
struct v4l2_queryctrl *a)
{
return -EINVAL;