This repository has been archived by the owner on Oct 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v4l.c
209 lines (171 loc) · 4.55 KB
/
v4l.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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#ifdef __NetBSD__
#include <sys/videoio.h>
#elif defined(__linux__)
#include <linux/videodev2.h>
#else
#error What OS is this? Where am I? Are those my feet?
#endif
#include <sys/mman.h>
#include "hueblobs.h"
#include "v4l.h"
#define DEBUG
#ifdef DEBUG
#define DFPRINTF(x) fprintf x
#else
#define DFPRINTF(x)
#endif
#define BUFFERS 1
static int fd;
struct {
uint8_t *start;
size_t length;
} *buffers;
int
open_webcam(unsigned int desired_width, unsigned int desired_height)
{
unsigned int i;
int ret, type;
struct v4l2_format format;
struct v4l2_capability cap;
struct v4l2_requestbuffers reqbuf;
struct v4l2_buffer buffer;
struct v4l2_control control;
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fd = open("/dev/video0", O_RDWR, 0);
if (fd == -1) {
perror("Couldn't open /dev/video0");
return 1;
}
memset(&format, 0, sizeof(format));
format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
ret = ioctl(fd, VIDIOC_G_FMT, &format);
if (ret < 0) {
perror("Couldn't get format");
return 1;
}
DFPRINTF((stderr, "buf type %d\n", format.type));
DFPRINTF((stderr, "pix data %d %d\n", format.fmt.pix.width,
format.fmt.pix.height));
#define pix format.fmt.pix.pixelformat
DFPRINTF((stderr, "%c%c%c%c\n", pix & 0xFF, (pix >> 8) & 0xFF,
(pix >> 16) & 0xFF, pix >> 24));
#undef pix
DFPRINTF((stderr, "field is %d\n", format.fmt.pix.field));
DFPRINTF((stderr, "bytesperline is %d\n", format.fmt.pix.bytesperline));
memset(&format, 0, sizeof(format));
format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
format.fmt.pix.width = desired_width;
format.fmt.pix.height = desired_height;
format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
format.fmt.pix.bytesperline = 2 * desired_width;
format.fmt.pix.sizeimage = 2 * desired_width * desired_height;
ret = ioctl(fd, VIDIOC_S_FMT, &format);
if (ret < 0) {
perror("Couldn't set format");
return 1;
}
memset(&cap, 0, sizeof(cap));
ioctl(fd, VIDIOC_QUERYCAP, &cap);
DFPRINTF((stderr,
"Capability driver is %s, card %s, version %X, cap %X\n",
cap.driver, cap.card, cap.version, cap.capabilities));
if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
fprintf(stderr, "Oh noes, no streaming capability\n");
return 1;
}
memset(&control, 0, sizeof(control));
control.id = V4L2_CID_AUTO_WHITE_BALANCE;
control.value = 1;
if (ioctl(fd, VIDIOC_S_CTRL, &control) < 0) {
perror("Couldn't set whitebalancing on");
}
memset(&reqbuf, 0, sizeof(reqbuf));
reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
reqbuf.memory = V4L2_MEMORY_MMAP;
reqbuf.count = BUFFERS;
ret = ioctl(fd, VIDIOC_REQBUFS, &reqbuf);
if (ret < 0) {
perror("Couldn't reqbufs");
return 1;
}
DFPRINTF((stderr, "%d buffers aquired\n", reqbuf.count));
buffers = calloc(reqbuf.count, sizeof(*buffers));
if (buffers == NULL) {
printf("Couldn't allocate buffer ptrs\n");
return 1;
}
for (i = 0; i < reqbuf.count; i++) {
memset(&buffer, 0, sizeof(buffer));
buffer.type = reqbuf.type;
buffer.memory = V4L2_MEMORY_MMAP;
buffer.index = i;
ret = ioctl(fd, VIDIOC_QUERYBUF, &buffer);
if (ret < 0) {
perror("Vidioc querybuf req failure\n");
return 1;
}
buffers[i].length = buffer.length;
buffers[i].start = mmap(NULL, buffer.length,
PROT_READ | PROT_WRITE, MAP_SHARED,
fd, buffer.m.offset);
if (buffers[i].start == MAP_FAILED) {
perror("mmap failed");
return 1;
}
memset(buffers[i].start, 0x41, buffers[i].length);
}
ret = ioctl(fd, VIDIOC_STREAMON, &type);
if (ret < 0) {
perror("Starting stream failed");
return 1;
}
return 0;
}
uint8_t *
get_v4l_frame()
{
struct v4l2_buffer buffer;
int ret;
memset(&buffer, 0, sizeof(buffer));
buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buffer.memory = V4L2_MEMORY_MMAP;
buffer.index = 0;
/* Queue it */
ret = ioctl(fd, VIDIOC_QBUF, &buffer);
if (ret < 0) {
perror("queueing buffer failed");
return NULL;
}
/* Now dequeue it */
ret = ioctl(fd, VIDIOC_DQBUF, &buffer);
if (ret < 0) {
perror("couldn't dequeue buffer");
return NULL;
}
DFPRINTF((stderr, "dequeued buffer is size %d?\n", buffer.length));
return buffers[0].start;
}
void
close_webcam()
{
int i, type, ret;
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
ret = ioctl(fd, VIDIOC_STREAMOFF, &type);
if (ret < 0) {
perror("Stopping stream failed.");
}
for (i = 0; i < BUFFERS; i++)
munmap(buffers[i].start, buffers[i].length);
free(buffers);
close(fd);
/* As far as I can tell, we don't free device buffers, as they don't
* belong to us, they belong to the device */
return;
}