-
Notifications
You must be signed in to change notification settings - Fork 64
/
videocap.c
281 lines (251 loc) · 6.4 KB
/
videocap.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
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include "videocap.h"
#define N_CAPTURE_BUFFERS 16
typedef struct {
int fd;
unsigned w, h, framesize;
unsigned format;
unsigned curbufidx;
unsigned nbuffers;
struct v4l2_buffer buffers[N_CAPTURE_BUFFERS];
void *pointers[N_CAPTURE_BUFFERS];
} VIDEOCAP;
void *videocap_open(const char *devname)
{
int fd = open(devname, O_RDWR, 0);
if(fd == -1)
return 0;
VIDEOCAP *v = (VIDEOCAP *)calloc(1, sizeof(VIDEOCAP));
v->fd = fd;
return v;
}
int videocap_capabilities(void *v, struct v4l2_capability *cap)
{
if(ioctl(((VIDEOCAP *)v)->fd, VIDIOC_QUERYCAP, cap) == -1)
return VIDEOCAP_ERR_QUERY;
return 0;
}
int videocap_framerates(void *v, int index, int w, int h, unsigned format, double *fps)
{
struct v4l2_frmivalenum arg;
VIDEOCAP *v1 = (VIDEOCAP *)v;
arg.index = index;
arg.pixel_format = format;
arg.width = w;
arg.height = h;
if(ioctl(v1->fd, VIDIOC_ENUM_FRAMEINTERVALS, &arg) == -1)
return VIDEOCAP_ERR_ENUM;
*fps = (double)arg.discrete.denominator / arg.discrete.numerator;
return 0;
}
int videocap_formats(void *v, int index, char *desc, unsigned *pixelformat)
{
struct v4l2_fmtdesc arg;
VIDEOCAP *v1 = (VIDEOCAP *)v;
arg.index = index;
arg.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if(ioctl(v1->fd, VIDIOC_ENUM_FMT, &arg) == -1)
return VIDEOCAP_ERR_ENUM;
strcpy(desc, (const char *)arg.description);
*pixelformat = arg.pixelformat;
return 0;
}
static int set_format(VIDEOCAP *v, int w, int h, unsigned format)
{
struct v4l2_format fmt;
memset(&fmt, 0, sizeof(fmt));
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = w;
fmt.fmt.pix.height = h;
fmt.fmt.pix.pixelformat = format;
if(ioctl(v->fd, VIDIOC_S_FMT, &fmt) == -1)
return -1;
v->framesize = w * h * 2;
v->w = w;
v->h = h;
v->format = format;
return 0;
}
static int set_userpointers(VIDEOCAP *v)
{
struct v4l2_requestbuffers reqbuf;
memset(&reqbuf, 0, sizeof (reqbuf));
reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
reqbuf.memory = V4L2_MEMORY_USERPTR;
if(ioctl(v->fd, VIDIOC_REQBUFS, &reqbuf) == -1)
return -1;
return 0;
}
static int free_buffers(VIDEOCAP *v)
{
int i;
for(i = 0; i < v->nbuffers; i++)
if(v->pointers[i])
{
if(v->buffers[i].memory == V4L2_MEMORY_MMAP)
munmap(v->pointers[i], v->buffers[i].length);
else {
free((void *)v->pointers[i]);
v->pointers[i] = 0;
}
}
return 0;
}
static int create_mmap_buffers(VIDEOCAP *v)
{
struct v4l2_requestbuffers reqbuf;
unsigned i;
memset(&reqbuf, 0, sizeof (reqbuf));
reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
reqbuf.memory = V4L2_MEMORY_MMAP;
reqbuf.count = v->nbuffers;
if(ioctl(v->fd, VIDIOC_REQBUFS, &reqbuf) == -1)
return VIDEOCAP_ERR_REQBUFS;
v->nbuffers = reqbuf.count;
for(i = 0; i < reqbuf.count; i++)
{
memset(&v->buffers[i], 0, sizeof(v->buffers[i]));
v->buffers[i].index = i;
v->buffers[i].type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
v->buffers[i].memory = V4L2_MEMORY_MMAP;
if(ioctl(v->fd, VIDIOC_QUERYBUF, &v->buffers[i]) == -1)
{
free_buffers(v);
return VIDEOCAP_ERR_QUERYBUF;
}
v->pointers[i] = mmap(NULL, v->buffers[i].length, PROT_READ | PROT_WRITE,
MAP_SHARED, v->fd, v->buffers[i].m.offset);
if(v->pointers[i] == (void *)-1)
{
v->pointers[i] = 0;
free_buffers(v);
return VIDEOCAP_ERR_MMAP;
}
}
return 0;
}
static int create_buffers(VIDEOCAP *v)
{
int i;
for(i = 0; i < v->nbuffers; i++)
{
memset(&v->buffers[i], 0, sizeof(v->buffers[i]));
v->buffers[i].index = i;
v->buffers[i].type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
v->buffers[i].memory = V4L2_MEMORY_USERPTR;
v->pointers[i] = malloc(v->framesize);
v->buffers[i].m.userptr = (unsigned long)v->pointers[i];
if(v->buffers[i].m.userptr == 0)
{
free_buffers(v);
return -1;
}
}
return 0;
}
static int enqueue_buffers(VIDEOCAP *v)
{
int i;
for(i = 0; i < v->nbuffers-1; i++)
if(ioctl(v->fd, VIDIOC_QBUF, &v->buffers[i]) == -1)
return -1;
return 0;
}
static int start_streaming(VIDEOCAP *v)
{
int buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if(ioctl(v->fd, VIDIOC_STREAMON, &buf_type))
return -1;
v->curbufidx = 0;
return 0;
}
int stop_streaming(VIDEOCAP *v)
{
int buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if(ioctl(v->fd, VIDIOC_STREAMOFF, &buf_type))
return -1;
return 0;
}
static int set_framerate(VIDEOCAP *v, int rate)
{
struct v4l2_streamparm fps;
memset(&fps, 0, sizeof(fps));
fps.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fps.parm.capture.timeperframe.numerator = 1;
fps.parm.capture.timeperframe.denominator = rate;
if(ioctl(v->fd, VIDIOC_S_PARM, &fps) == -1)
return -1;
return 0;
}
int videocap_startcapture(void *v, int w, int h, unsigned format, int fps, int nbuffers)
{
int rc;
VIDEOCAP *v1 = (VIDEOCAP *)v;
if(nbuffers < 1 || nbuffers > N_CAPTURE_BUFFERS)
return VIDEOCAP_ERR_REQBUFS;
v1->nbuffers = nbuffers;
if(set_format(v1, w, h, format))
return VIDEOCAP_ERR_SET_FORMAT;
if(fps)
{
if(set_framerate(v1, fps))
return VIDEOCAP_ERR_SET_FRAMERATE;
}
rc = create_mmap_buffers(v1);
if(rc)
{
if(set_userpointers(v1))
return rc;
if(create_buffers(v1))
return VIDEOCAP_ERR_MALLOC;
}
if(enqueue_buffers(v1))
{
free_buffers(v1);
return VIDEOCAP_ERR_ENQUEUE_BUFFERS;
}
if(start_streaming(v))
{
free_buffers(v1);
return VIDEOCAP_ERR_START_STREAMING;
}
return 0;
}
static int enqueue_buffer(VIDEOCAP *v, int idx)
{
if(ioctl(v->fd, VIDIOC_QBUF, &v->buffers[idx]) == -1)
return -1;
return 0;
}
static int dequeue_buffer(VIDEOCAP *v, int idx)
{
if(ioctl(v->fd, VIDIOC_DQBUF, &v->buffers[idx]) == -1)
return -1;
return 0;
}
int videocap_getframe(void *v, char **frame, struct timeval *tv)
{
VIDEOCAP *v1 = (VIDEOCAP *)v;
if(enqueue_buffer(v1, (v1->curbufidx + v1->nbuffers - 1) % v1->nbuffers))
return VIDEOCAP_ERR_ENQUEUE_BUFFERS;
if(dequeue_buffer(v1, v1->curbufidx))
return VIDEOCAP_ERR_DEQUEUE_BUFFERS;
*frame = (char *)v1->pointers[v1->curbufidx];
*tv = v1->buffers[v1->curbufidx].timestamp;
v1->curbufidx = (v1->curbufidx + 1) % v1->nbuffers;
return 0;
}
int videocap_close(void *v)
{
VIDEOCAP *v1 = (VIDEOCAP *)v;
stop_streaming(v1);
free_buffers(v1);
close(v1->fd);
free(v1);
return 0;
}