-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathv4l2-mmal-uvc.c
3572 lines (3033 loc) · 94.9 KB
/
v4l2-mmal-uvc.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
/*
* v4l2-mmal-uvc application
*
* Copyright (C) 2020 Kin Wei Lee
*
* based on uvc-gadget [email protected]
* based on v4l2_mmal by 6by9 Raspberry Pi (Trading) Ltd.
*
* 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 <sys/time.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <time.h>
#include <sched.h>
#include <linux/usb/ch9.h>
#include <linux/usb/video.h>
#include <linux/videodev2.h>
#include "uvc.h"
#include "interface/mmal/mmal.h"
#include "interface/mmal/mmal_buffer.h"
#include "interface/mmal/util/mmal_connection.h"
#include "interface/mmal/util/mmal_util.h"
#include "interface/mmal/util/mmal_util_params.h"
#include "bcm_host.h"
#include "interface/vcsm/user-vcsm.h"
#include "v4l2-mmal-uvc.h"
#ifndef V4L2_BUF_FLAG_ERROR
#define V4L2_BUF_FLAG_ERROR 0x0040
#endif
//output image size fixed to 1280x720
/*#define WIDTH 1280
#define HEIGHT 720*/
#define MAX_PLANES 8
int debug = 0;
#define print(...) do { if (debug) printf(__VA_ARGS__); } while (0)
/* Enable debug prints. */
#undef ENABLE_BUFFER_DEBUG
//#define ENABLE_BUFFER_DEBUG
#undef ENABLE_USB_REQUEST_DEBUG
#define CLEAR(x) memset (&(x), 0, sizeof (x))
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define clamp(val, min, max) ({ \
typeof(val) __val = (val); \
typeof(min) __min = (min); \
typeof(max) __max = (max); \
(void) (&__val == &__min); \
(void) (&__val == &__max); \
__val = __val < __min ? __min: __val; \
__val > __max ? __max: __val; })
//#define ARRAY_SIZE(a) ((sizeof(a) / sizeof(a[0])))
#define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, \
((x) >> 24) & 0xff
/*
* The UVC webcam gadget kernel driver (g_webcam.ko) supports changing
* the Brightness attribute of the Processing Unit (PU). by default. If
* the underlying video capture device supports changing the Brightness
* attribute of the image being acquired (like the Virtual Video, VIVI
* driver), then we should route this UVC request to the respective
* video capture device.
*
* Incase, there is no actual video capture device associated with the
* UVC gadget and we wish to use this application as the final
* destination of the UVC specific requests then we should return
* pre-cooked (static) responses to GET_CUR(BRIGHTNESS) and
* SET_CUR(BRIGHTNESS) commands to keep command verifier test tools like
* UVC class specific test suite of USBCV, happy.
*
* Note that the values taken below are in sync with the VIVI driver and
* must be changed for your specific video capture device. These values
* also work well in case there in no actual video capture device.
*/
#define PU_BRIGHTNESS_MIN_VAL 0
#define PU_BRIGHTNESS_MAX_VAL 255
#define PU_BRIGHTNESS_STEP_SIZE 1
#define PU_BRIGHTNESS_DEFAULT_VAL 7
/* declarations */
int default_format = 0; /* V4L2_PIX_FMT_YUYV */
static int video_queue_buffer (struct v4l2_device *dev, int index);
/* ---------------------------------------------------------------------------
* UVC specific stuff
*/
struct uvc_frame_info
{
unsigned int width;
unsigned int height;
unsigned int intervals[8];
};
struct uvc_format_info
{
unsigned int fcc;
const struct uvc_frame_info *frames;
};
static const struct uvc_frame_info uvc_frames_yuyv[] = {
// {640, 360, {66666, 100000, 500000, 0},},
// {736, 480, {500000, 0},},
{1280, 720, {400000, 0},},
{0, 0, {0,},},
};
static const struct uvc_frame_info uvc_frames_mjpeg[] = {
// {640, 360, {66666, 100000, 500000, 0},},
{1280, 720, {500000},},
{0, 0, {0,},},
};
static const struct uvc_format_info uvc_formats[] = {
// {V4L2_PIX_FMT_YUYV, uvc_frames_yuyv},
{V4L2_PIX_FMT_MJPEG, uvc_frames_mjpeg},
};
/* ---------------------------------------------------------------------------
* V4L2 and UVC device instances
*/
static void
time_me (char *fn_name, struct v4l2_device *dev)
{
struct timespec stt;
static int i = 0;
static float ft = 0;
clock_gettime (CLOCK_MONOTONIC, &stt);
if (i)
ft +=
((float) (stt.tv_nsec - dev->last.tv_nsec) / 1e9 +
(float) (stt.tv_sec - dev->last.tv_sec));
if (i % 100 == 0)
printf ("%s %0.2ffps\n", fn_name, i / ft );
dev->last = stt;
i++;
}
/* forward declarations */
static int uvc_video_stream (struct uvc_device *dev, int enable);
// print out some structure info
void
v4l2_dump (char *txt, struct v4l2_device *dev)
{
print ("%s\n", txt);
print ("dev->v4l2_fd %d\n", dev->v4l2_fd);
print ("dev->type %d\n", dev->type);
print ("dev->memtype %d\n", dev->memtype);
}
static bool
video_is_mplane (struct v4l2_device *dev)
{
return dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE || dev->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
}
static bool
video_is_capture (struct v4l2_device *dev)
{
return dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE || dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE;
}
static bool
video_is_output (struct v4l2_device *dev)
{
return dev->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE || dev->type == V4L2_BUF_TYPE_VIDEO_OUTPUT;
}
static bool
video_has_fd (struct v4l2_device *dev)
{
return dev->v4l2_fd != -1;
}
static int
video_set_fd (struct v4l2_device *dev, int fd)
{
if (video_has_fd (dev)) {
print ("Can't set fd (already open).\n");
return -1;
}
dev->v4l2_fd = fd;
return 0;
}
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 v4l2_device *dev, enum v4l2_buf_type type)
{
dev->type = type;
}
int
video_querycap (struct v4l2_device *dev, unsigned int *capabilities)
{
struct v4l2_capability cap;
unsigned int caps;
int ret;
memset (&cap, 0, sizeof cap);
ret = ioctl (dev->v4l2_fd, VIDIOC_QUERYCAP, &cap);
if (ret < 0)
return 0;
caps = cap.capabilities & V4L2_CAP_DEVICE_CAPS ? cap.device_caps : cap.capabilities;
print
("Device `%s' on `%s' (driver '%s') is a video %s (%s mplanes) device.\n",
cap.card, cap.bus_info, cap.driver,
caps & (V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_CAPTURE) ?
"capture" : "output",
caps & (V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_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 {
print ("Device supports neither capture nor output.\n");
return -EINVAL;
}
return 0;
}
static int
video_open (struct v4l2_device *dev, const char *devname)
{
if (video_has_fd (dev)) {
print ("Can't open device (already open).\n");
return -1;
}
dev->v4l2_fd = open (devname, O_RDWR);
if (dev->v4l2_fd < 0) {
print ("Error opening device %s: %s (%d).\n", devname,
strerror (errno), errno);
return dev->v4l2_fd;
}
print ("Device %s opened.\n", devname);
dev->opened = 1;
return 0;
}
static void
video_close (struct v4l2_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->v4l2_fd);
}
static int
video_get_format (struct v4l2_device *dev)
{
struct v4l2_format fmt;
unsigned int i;
int ret;
memset (&fmt, 0, sizeof fmt);
fmt.type = dev->type;
ret = ioctl (dev->v4l2_fd, VIDIOC_G_FMT, &fmt);
if (ret < 0) {
print ("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;
print ("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;
print (" * Stride %u, buffer size %u\n",
fmt.fmt.pix_mp.plane_fmt[i].bytesperline,
fmt.fmt.pix_mp.plane_fmt[i].sizeimage);
}
}
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;
print
("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
format_bpp (__u32 pixfmt)
{
switch (pixfmt) {
case V4L2_PIX_FMT_BGR24:
case V4L2_PIX_FMT_RGB24:
return 4;
case V4L2_PIX_FMT_YUYV:
case V4L2_PIX_FMT_YVYU:
case V4L2_PIX_FMT_UYVY:
case V4L2_PIX_FMT_VYUY:
return 2;
case V4L2_PIX_FMT_SRGGB8:
case V4L2_PIX_FMT_SBGGR8:
case V4L2_PIX_FMT_SGRBG8:
case V4L2_PIX_FMT_SGBRG8:
return 1;
default:
return 1;
}
}
/*static int
video_set_quality (struct v4l2_device *dev, unsigned int quality)
{
struct v4l2_jpegcompression jpeg;
int ret;
if (quality == (unsigned int) -1)
return 0;
memset (&jpeg, 0, sizeof jpeg);
jpeg.quality = quality;
ret = ioctl (dev->v4l2_fd, VIDIOC_S_JPEGCOMP, &jpeg);
if (ret < 0) {
print ("Unable to set quality to %u: %s (%d).\n", quality,
strerror (errno), errno);
return ret;
}
ret = ioctl (dev->v4l2_fd, VIDIOC_G_JPEGCOMP, &jpeg);
if (ret >= 0)
print ("Quality set to %u\n", jpeg.quality);
return 0;
}*/
static int
video_set_format (struct v4l2_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;
print ("video_set_format width %d, height %d\n", w, h);
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 {
fmt.fmt.pix.width = w;
fmt.fmt.pix.height = h;
fmt.fmt.pix.pixelformat = format;
fmt.fmt.pix.field = field;
print ("stride is %d\n", stride);
if (!stride)
stride = ((w + 31) & ~31) * format_bpp (format);
print ("stride is now %d\n", stride);
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->v4l2_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 {
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;
}
// create dma buffer using vcm and link it to v4l2 buffer
static int
buffer_export (int v4l2fd, enum v4l2_buf_type bt, int index, int *dmafd,
unsigned int *vcsm_hdl)
{
struct v4l2_exportbuffer expbuf;
unsigned int vcsm_handle;
memset (&expbuf, 0, sizeof (expbuf));
expbuf.type = bt;
expbuf.index = index;
if (ioctl (v4l2fd, VIDIOC_EXPBUF, &expbuf)) {
print ("Failed to EXPBUF\n");
return -1;
}
*dmafd = expbuf.fd;
print ("Importing DMABUF %d into VCSM...\n", expbuf.fd);
vcsm_handle = vcsm_import_dmabuf (expbuf.fd, "V4L2 buf");
if (vcsm_handle)
print ("...done. vcsm_handle %u\n", vcsm_handle);
else
print ("...done. Failed\n");
*vcsm_hdl = vcsm_handle;
return vcsm_handle ? 0 : -1;
}
static int
video_buffer_mmap (struct v4l2_device *dev, struct mmal_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->v4l2_fd, offset);
if (buffer->mem[i] == MAP_FAILED) {
print ("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;
print ("Buffer %u/%u mapped at address %p.\n", buffer->idx, i, buffer->mem[i]);
}
return 0;
}
static int
video_buffer_munmap (struct v4l2_device *dev, struct mmal_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) {
print ("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 v4l2_device *dev,
struct mmal_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) {
print ("Unable to allocate buffer %u/%u (%d)\n", buffer->idx, i, ret);
return -ENOMEM;
}
buffer->mem[i] += offset;
buffer->size[i] = length;
buffer->padding[i] = padding;
print ("Buffer %u/%u allocated at address %p.\n",
buffer->idx, i, buffer->mem[i]);
}
return 0;
}
*/
static void
video_buffer_free_userptr (struct v4l2_device *dev,
struct mmal_buffer *buffer)
{
unsigned int i;
for (i = 0; i < dev->num_planes; i++) {
free (buffer->mem[i]);
buffer->mem[i] = NULL;
}
}
static void
get_ts_flags (uint32_t flags, const char **ts_type, const char **ts_source)
{
switch (flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) {
case V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN:
*ts_type = "unk";
break;
case V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC:
*ts_type = "mono";
break;
case V4L2_BUF_FLAG_TIMESTAMP_COPY:
*ts_type = "copy";
break;
default:
*ts_type = "inv";
}
switch (flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK) {
case V4L2_BUF_FLAG_TSTAMP_SRC_EOF:
*ts_source = "EoF";
break;
case V4L2_BUF_FLAG_TSTAMP_SRC_SOE:
*ts_source = "SoE";
break;
default:
*ts_source = "inv";
}
}
static int
video_alloc_buffers (struct v4l2_device *dev, int nbufs)
{
struct v4l2_plane planes[MAX_PLANES];
struct v4l2_requestbuffers rb;
struct v4l2_buffer buf;
struct mmal_buffer *buffers;
unsigned int i;
int ret;
memset (&rb, 0, sizeof rb);
rb.count = nbufs;
rb.type = dev->type;
rb.memory = dev->memtype;
ret = ioctl (dev->v4l2_fd, VIDIOC_REQBUFS, &rb);
if (ret < 0) {
print ("V4L2: Unable to request buffers: %s (%d).\n", strerror (errno), errno);
return ret;
}
//printf ("video_alloc_buffers fd%d %d %d\n", dev->v4l2_fd, dev->type, ret);
printf ("%u buffers requested, V4L2 returned %u bufs.\n", nbufs, rb.count);
buffers = malloc (rb.count * sizeof buffers[0]);
if (buffers == NULL)
return -ENOMEM;
/* Map the buffers. */
for (i = 0; i < rb.count; ++i) {
const char *ts_type, *ts_source;
memset (&buf, 0, sizeof buf);
memset (planes, 0, sizeof planes);
buf.index = i;
buf.type = dev->type;
buf.memory = dev->memtype;
buf.length = MAX_PLANES;
buf.m.planes = planes;
ret = ioctl (dev->v4l2_fd, VIDIOC_QUERYBUF, &buf);
if (ret < 0) {
print ("Unable to query buffer %u: %s (%d).\n", i, strerror (errno), errno);
return ret;
}
get_ts_flags (buf.flags, &ts_type, &ts_source);
print ("length: %u offset: %u timestamp type/source: %s/%s\n", buf.length, buf.m.offset, ts_type, ts_source);
buffers[i].idx = i;
switch (dev->memtype) {
case V4L2_MEMORY_MMAP:
ret = video_buffer_mmap (dev, &buffers[i], &buf);
break;
/*case V4L2_MEMORY_USERPTR:
ret = video_buffer_alloc_userptr (dev, &buffers[i], &buf, offset, padding);
break;*/
default:
ret = -1;
break;
}
if (ret < 0)
return ret;
if (!buffer_export(dev->v4l2_fd, dev->type, i, &buffers[i].dma_fd, &buffers[i].vcsm_handle)) {
dev->can_zero_copy = MMAL_TRUE;
print ("Exported buffer %d to dmabuf %d, vcsm handle %u\n", i,
buffers[i].dma_fd, buffers[i].vcsm_handle);
}
else {
if (dev->can_zero_copy) {
print ("Some buffer exported whilst others not. HELP!\n");
dev->can_zero_copy = MMAL_FALSE;
}
}
if (dev->mmal_pool) {
MMAL_BUFFER_HEADER_T *mmal_buf;
mmal_buf = mmal_queue_get (dev->mmal_pool->queue);
if (!mmal_buf) {
print
("Failed to get a buffer from the pool. Queue length %d\n", mmal_queue_length (dev->mmal_pool->queue));
return -1;
}
mmal_buf->user_data = &buffers[i];
if (dev->can_zero_copy)
mmal_buf->data = (uint8_t *) vcsm_vc_hdl_from_hdl (buffers[i].vcsm_handle);
else
mmal_buf->data = buffers[i].mem[0];
mmal_buf->alloc_size = buf.length;
buffers[i].mmal = mmal_buf;
print ("Linking V4L2 buffer index %d ptr %p to MMAL header %p. mmal->data 0x%X\n",
i, &buffers[i], mmal_buf, (uint32_t) mmal_buf->data);
/* Put buffer back in the pool */
mmal_buffer_header_release (mmal_buf);
}
}
dev->timestamp_type = buf.flags & V4L2_BUF_FLAG_TIMESTAMP_MASK;
dev->buffers = buffers;
dev->nbufs = rb.count;
return 0;
}
static int
video_queue_all_buffers (struct v4l2_device *dev)
{
unsigned int i;
int ret;
print ("V4L2: video_queue_all_buffers try %d \n", dev->nbufs);
/* Queue the buffers. */
for (i = 0; i < dev->nbufs; ++i) {
ret = video_queue_buffer (dev, i);//, fill);
if (ret < 0)
return ret;
}
print ("V4L2: video_queue_all_buffers ok %d %lld\n", dev->nbufs,
dev->qbuf_count);
return 0;
}
static void
video_verify_buffer (struct v4l2_device *dev, struct v4l2_buffer *buf)
{
struct mmal_buffer *buffer = &dev->buffers[buf->index];
unsigned int plane;
unsigned int i;
for (plane = 0; plane < dev->num_planes; ++plane) {
const uint8_t *data = buffer->mem[plane] + buffer->size[plane];
unsigned int errors = 0;
unsigned int dirty = 0;
if (buffer->padding[plane] == 0)
continue;
for (i = 0; i < buffer->padding[plane]; ++i) {
if (data[i] != 0x55) {
errors++;
dirty = i + 1;
}
}
if (errors) {
print
("Warning: %u bytes overwritten among %u first padding bytes for plane %u\n",
errors, dirty, plane);
dirty = (dirty + 15) & ~15;
dirty = dirty > 32 ? 32 : dirty;
for (i = 0; i < dirty; ++i) {
print ("%02x ", data[i]);
if (i % 16 == 15)
print ("\n");
}
}
}
}
static int
video_free_buffers (struct v4l2_device *dev)
{
struct v4l2_requestbuffers rb;
unsigned int i;
int ret;
if (dev->nbufs == 0)
return 0;
for (i = 0; i < dev->nbufs; ++i) {
switch (dev->memtype) {
case V4L2_MEMORY_MMAP:
if (dev->buffers[i].vcsm_handle) {
print ("Releasing vcsm handle %u\n", dev->buffers[i].vcsm_handle);
vcsm_free (dev->buffers[i].vcsm_handle);
}
if (dev->buffers[i].dma_fd) {
print ("Closing dma_buf %d\n", dev->buffers[i].dma_fd);
close (dev->buffers[i].dma_fd);
}
ret = video_buffer_munmap (dev, &dev->buffers[i]);
if (ret < 0)
return ret;
break;
case V4L2_MEMORY_USERPTR:
video_buffer_free_userptr (dev, &dev->buffers[i]);
break;
default:
break;
}
}
memset (&rb, 0, sizeof rb);
rb.count = 0;
rb.type = dev->type;
rb.memory = dev->memtype;
ret = ioctl (dev->v4l2_fd, VIDIOC_REQBUFS, &rb);
if (ret < 0) {
print ("Unable to release buffers: %s (%d).\n", strerror (errno), errno);
return ret;
}
print ("%u buffers released.\n", dev->nbufs);
free (dev->buffers);
dev->nbufs = 0;
dev->buffers = NULL;
return 0;
}
static int
video_queue_buffer (struct v4l2_device *dev, int index)
{
struct v4l2_buffer buf;
struct v4l2_plane planes[MAX_PLANES];
int ret;
unsigned int i;
memset (&buf, 0, sizeof buf);
memset (&planes, 0, sizeof planes);
buf.index = index;
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; //dev->type;
buf.memory = V4L2_MEMORY_MMAP; //dev->memtype;
//print (" video_queue_buf \n");
/*if (video_is_output (dev))
{
buf.flags = dev->buffer_output_flags;
if (dev->timestamp_type == V4L2_BUF_FLAG_TIMESTAMP_COPY)
{
struct timespec ts;
clock_gettime (CLOCK_MONOTONIC, &ts);
buf.timestamp.tv_sec = ts.tv_sec;
buf.timestamp.tv_usec = ts.tv_nsec / 1000;
}
}
if (video_is_mplane (dev))
{
buf.m.planes = planes;
buf.length = dev->num_planes;
}
if (dev->memtype == V4L2_MEMORY_USERPTR)
{
print ("v4l2 memtype is V4L2_MEMORY_USERPTR\n");