-
Notifications
You must be signed in to change notification settings - Fork 0
/
video.cpp
340 lines (275 loc) · 7.49 KB
/
video.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h> /* low-level i/o */
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <glog/logging.h>
#include <linux/videodev2.h>
#include "video.h"
#define CLEAR(x) memset(&(x), 0, sizeof(x))
struct VideoDev::Buffer {
void *start;
size_t length;
};
namespace {
int xioctl(int fh, int request, void *arg) {
int r;
do {
r = ioctl(fh, request, arg);
} while (-1 == r && EINTR == errno);
return r;
}
bool DequeueBuffer(int fd, struct v4l2_buffer* buf) {
CLEAR(*buf);
buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf->memory = V4L2_MEMORY_MMAP;
while (true) {
// Try and de-queue a buffer.
if (xioctl(fd, VIDIOC_DQBUF, buf) == 0) {
return true;
}
if (errno != EAGAIN) {
perror("DQBUF");
return false;
}
// No buffer. Sleeping awaiting one.
fd_set fds;
struct timeval tv;
int r;
FD_ZERO(&fds);
FD_SET(fd, &fds);
/* Timeout. */
tv.tv_sec = 1;
tv.tv_usec = 0;
r = select(fd + 1, &fds, NULL, NULL, &tv);
if (r == 0) {
fprintf(stderr, "Select timeout\n");
return false;
}
}
}
int open_device(const char* dev_name) {
struct stat st;
if (-1 == stat(dev_name, &st)) {
fprintf(stderr, "Cannot identify '%s': %d, %s\n",
dev_name, errno, strerror(errno));
return -1;
}
if (!S_ISCHR(st.st_mode)) {
fprintf(stderr, "%s is no device\n", dev_name);
return -1;
}
int fd = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
if (-1 == fd) {
fprintf(stderr, "Cannot open '%s': %d, %s\n",
dev_name, errno, strerror(errno));
return -1;
}
struct v4l2_capability cap;
if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) {
if (EINVAL == errno) {
fprintf(stderr, "%s is no V4L2 device\n",
dev_name);
return -1;
}
perror("VIDIOC_QUERYCAP");
return -1;
}
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
fprintf(stderr, "%s is no video capture device\n",
dev_name);
return -1;
}
if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
fprintf(stderr, "%s does not support streaming i/o\n",
dev_name);
return -1;
}
// Disable cropping (allowed to silently fail if cropping
// isn't supported).
struct v4l2_cropcap cropcap;
CLEAR(cropcap);
cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (xioctl(fd, VIDIOC_CROPCAP, &cropcap) == 0) {
struct v4l2_crop crop;
crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
crop.c = cropcap.defrect; /* reset to default */
xioctl(fd, VIDIOC_S_CROP, &crop);
}
// Setup video format.
struct v4l2_format fmt;
CLEAR(fmt);
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = 640;
fmt.fmt.pix.height = 480;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt)) {
perror("VIDIOC_S_FMT");
return -1;
}
// Setup streaming parameters.
struct v4l2_streamparm parm;
CLEAR(parm);
parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
parm.parm.capture.timeperframe.numerator = 1;
parm.parm.capture.timeperframe.denominator = 5;
if (-1 == xioctl(fd, VIDIOC_S_PARM, &parm)) {
perror("VIDIOC_S_FMT");
return -1;
}
/* Buggy driver paranoia. */
unsigned int min = fmt.fmt.pix.width * 2;
if (fmt.fmt.pix.bytesperline < min)
fmt.fmt.pix.bytesperline = min;
min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
if (fmt.fmt.pix.sizeimage < min)
fmt.fmt.pix.sizeimage = min;
return fd;
}
} // namespace
bool VideoDev::GetObservation(int cam, int id, cv::Mat* mat) {
struct v4l2_buffer buf;
if (!DequeueBuffer(fd_, &buf)) {
fprintf(stderr, "Failed to Dequeue buffer\n");
return false;
}
CHECK_LT(buf.index, buffers_.size());
Buffer* b = buffers_[buf.index];
int bytes = buf.bytesused;
// TODO: Actually fill in the Mat. And probably convert from YUYV to RGB?
*mat = cv::Mat(480, 640, CV_8UC3);
#define SAT(c) \
if (c & (~255)) { if (c < 0) c = 0; else c = 255; }
// Convert from YUYV to RGB.
unsigned char * out = mat->ptr(0);
for (int i = 0; i < bytes; i += 4) {
unsigned char *in = ((unsigned char*)b->start) + i;
int y1 = in[0];
int cb = ((in[1] - 128) * 454) >> 8;
int cg = (in[1] - 128) * 88;
int y2 = in[2];
int cr = ((in[3] - 128) * 359) >> 8;
cg = (cg + (in[3] - 128) * 183) >> 8;
int r = y1 + cr;
int b = y1 + cb;
int g = y1 - cg;
SAT(r);
SAT(g);
SAT(b);
*out++ = b;
*out++ = g;
*out++ = r;
r = y2 + cr;
b = y2 + cb;
g = y2 - cg;
SAT(r);
SAT(g);
SAT(b);
*out++ = b;
*out++ = g;
*out++ = r;
}
// Re-queue buffer.
if (xioctl(fd_, VIDIOC_QBUF, &buf) == -1) {
fprintf(stderr, "Failed to re-queue buffer.\n");
return false;
}
return true;
}
VideoDev::~VideoDev() {
enum v4l2_buf_type type;
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (xioctl(fd_, VIDIOC_STREAMOFF, &type) == -1) {
fprintf(stderr, "Failed to call STREAMOFF\n");
}
for (const auto& buffer : buffers_) {
if (munmap(buffer->start, buffer->length) != -1) {
continue;
}
fprintf(stderr, "Failed to unmap buffer\n");
}
for (const auto& buffer : buffers_) {
delete buffer;
}
close(fd_);
}
bool VideoDev::Init() {
fd_ = open_device(device_);
if (fd_ < 0) {
fprintf(stderr, "Failed to open video device\n");
return false;
}
// Request a number of MMAP'ed buffers.
struct v4l2_requestbuffers req;
CLEAR(req);
req.count = num_buf_;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
if (xioctl(fd_, VIDIOC_REQBUFS, &req) == -1) {
if (EINVAL == errno) {
fprintf(stderr, "%s does not support "
"memory mapping\n", device_);
return false;
}
fprintf(stderr, "Failed to request buffers\n");
return false;
}
if (req.count < 2) {
fprintf(stderr, "Insufficient buffer memory on %s\n",
device_);
return false;
}
// mmap the buffers into our address space.
for (unsigned int i = 0; i < req.count; ++i) {
struct v4l2_buffer buf;
CLEAR(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
if (xioctl(fd_, VIDIOC_QUERYBUF, &buf) == -1) {
fprintf(stderr, "QUERYBUF failed\n");
return false;
}
Buffer* buffer = new Buffer;
buffer->length = buf.length;
buffer->start =
mmap(NULL /* start anywhere */,
buf.length,
PROT_READ | PROT_WRITE /* required */,
MAP_SHARED /* recommended */,
fd_, buf.m.offset);
if (buffer->start == MAP_FAILED) {
fprintf(stderr, "Failed to mmap buffer.\n");
return false;
}
buffers_.push_back(buffer);
}
// queue the buffers, ready for receiving video frames.
for (unsigned int i = 0; i < buffers_.size(); ++i) {
struct v4l2_buffer buf;
CLEAR(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
if (xioctl(fd_, VIDIOC_QBUF, &buf) == -1) {
perror("Failed to queue buffer: VIDIOC_QBUF");
return false;
}
printf("Queued %d\n", i);
}
// Turn on video streaming.
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == xioctl(fd_, VIDIOC_STREAMON, &type)) {
perror("Failed to start streaming: VIDIOC_STREAMON");
return false;
}
return true;
}