-
Notifications
You must be signed in to change notification settings - Fork 1
/
v4l2.c
304 lines (248 loc) · 7.58 KB
/
v4l2.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
/*
* Copyright (C) 2011 Texas Instruments
* Author: Rob Clark <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
//#ifdef HAVE_CONFIG_H
#include "config.h"
//#endif
#include <linux/videodev2.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <pthread.h>
#include <omap_drmif.h>
#include "util.h"
#include "v4l2.h"
void
v4l2_usage(void)
{
MSG("V4L2 Capture Options:");
MSG("\t-c WxH@fourcc\tset capture dimensions/format");
MSG("\t-m\t\tdo MCF setup");
}
struct v4l2* v4l2_open(uint32_t fourcc, uint32_t width, uint32_t height)
{
struct v4l2_format format = {
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
};
struct v4l2 *v4l2;
int ret;
char devname[20] = "/dev/video1";
v4l2 = calloc(1, sizeof(*v4l2));
v4l2->fd = open(devname, O_RDWR);
ret = ioctl(v4l2->fd, VIDIOC_G_FMT, &format);
if (ret < 0) {
ERROR("VIDIOC_G_FMT failed: %s (%d)", strerror(errno), ret);
goto fail;
}
format.fmt.pix.pixelformat = fourcc;
format.fmt.pix.width = width;
format.fmt.pix.height = height;
if ((format.fmt.pix.width == 0) || (format.fmt.pix.height == 0) ||
(format.fmt.pix.pixelformat == 0)) {
ERROR("invalid capture settings '%dx%d@%4s'",
format.fmt.pix.width, format.fmt.pix.height,
(char *)&format.fmt.pix.pixelformat);
goto fail;
}
ret = ioctl(v4l2->fd, VIDIOC_S_FMT, &format);
if (ret < 0) {
ERROR("VIDIOC_S_FMT failed: %s (%d)", strerror(errno), ret);
goto fail;
}
return v4l2;
fail:
// XXX cleanup
free(v4l2);
return NULL;
}
void v4l2_close(struct v4l2 * v4l2)
{
close(v4l2->fd);
free(v4l2);
}
int
v4l2_reqbufs(struct v4l2 *v4l2, uint32_t n)
{
#if 1
int ret;
struct v4l2_requestbuffers rqbufs;
memset(&rqbufs, 0, sizeof(rqbufs));
rqbufs.count = n;
rqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
rqbufs.memory = V4L2_MEMORY_DMABUF;
ret = ioctl(v4l2->fd, VIDIOC_REQBUFS, &rqbufs);
if (ret < 0)
ERROR("REQBUFS failed: %s\n", strerror(errno));
DBG("allocated buffers = %d\n", rqbufs.count);
return 0;
#else
struct v4l2_requestbuffers reqbuf = {
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
.memory = V4L2_MEMORY_DMABUF,
.count = n,
};
uint32_t i;
int ret,dmafd;
if (v4l2->v4l2bufs) {
// maybe eventually need to support this?
ERROR("already reqbuf'd");
return -1;
}
ret = ioctl(v4l2->fd, VIDIOC_REQBUFS, &reqbuf);
if (ret < 0) {
ERROR("VIDIOC_REQBUFS failed: %s (%d)", strerror(errno), ret);
return ret;
}
if ((reqbuf.count != n) ||
(reqbuf.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
(reqbuf.memory != V4L2_MEMORY_DMABUF)) {
ERROR("unsupported..");
return -1;
}
#if 0
v4l2->nbufs = reqbuf.count;
v4l2->bufs = bufs;
v4l2->v4l2bufs = calloc(v4l2->nbufs, sizeof(*v4l2->v4l2bufs));
if (!v4l2->v4l2bufs) {
ERROR("allocation failed");
return -1;
}
for (i = 0; i < reqbuf.count; i++) {
assert(bufs[i]->nbo == 1); /* TODO add multi-planar support */
/* Call omap_bo_dmabuf only once, to export only once
* Otherwise, each call will return duplicated fds
* This way, every call to omap_bo_dmabuf will return a new fd
* Which won't match with any previously exported fds
* Instead, store dma fd in buf->fd[] */
dmafd = omap_bo_dmabuf(bufs[i]->bo[0]);
bufs[i]->fd[0] = dmafd;
v4l2->v4l2bufs[i] = (struct v4l2_buffer){
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
.memory = V4L2_MEMORY_DMABUF,
.index = i,
.m.fd = dmafd,
};
MSG("Exported buffer fd = %d\n", dmafd);
ret = ioctl(v4l2->fd, VIDIOC_QUERYBUF, &v4l2->v4l2bufs[i]);
v4l2->v4l2bufs[i].m.fd = dmafd;
if (ret) {
ERROR("VIDIOC_QUERYBUF failed: %s (%d)", strerror(errno), ret);
return ret;
}
}
#endif
#endif
return 0;
}
int
v4l2_streamon(struct v4l2 *v4l2)
{
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
int ret;
ret = ioctl(v4l2->fd, VIDIOC_STREAMON, &type);
if (ret) {
ERROR("VIDIOC_STREAMON failed: %s (%d)", strerror(errno), ret);
}
return ret;
}
int
v4l2_streamoff(struct v4l2 *v4l2)
{
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
int ret;
ret = ioctl(v4l2->fd, VIDIOC_STREAMOFF, &type);
if (ret) {
ERROR("VIDIOC_STREAMOFF failed: %s (%d)", strerror(errno), ret);
}
return ret;
}
int
v4l2_qbuf(struct v4l2 *v4l2, int dmafd, int index)
{
#if 1
int ret;
struct v4l2_buffer buf;
DBG("v4l2 buffer queue\n");
memset(&buf, 0, sizeof buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_DMABUF;
buf.index = index;
buf.m.fd = dmafd; //vpe->input_buf_dmafd[index];
//printf("%d, %d(%d), %d(%d), %d, %d\n",
// vipfd, buf.type, V4L2_BUF_TYPE_VIDEO_CAPTURE, buf.memory, V4L2_MEMORY_DMABUF, buf.index, buf.m.fd);
ret = ioctl(v4l2->fd, VIDIOC_QBUF, &buf);
if (ret < 0)
ERROR("QBUF failed: %s, index = %d\n", strerror(errno), index);
return 0;
#else
struct v4l2_buffer *v4l2buf = NULL;
int i, ret, fd;
struct timeval disp_time;
struct timeval disp_lat;
pthread_t tid;
assert(buf->nbo == 1); /* TODO add multi-planar support */
fd = buf->fd[0];
for (i = 0; i < v4l2->nbufs; i++) {
if (v4l2->v4l2bufs[i].m.fd == fd) {
v4l2buf = &v4l2->v4l2bufs[i];
}
}
tid = pthread_self();
gettimeofday(&disp_time, NULL);
timersub(&disp_time, &v4l2buf->timestamp, &disp_lat);
if(v4l2buf->timestamp.tv_sec)
DBG("%x: DISP latency = %d.%6d", (unsigned int)tid, (int)disp_lat.tv_sec, (int)disp_lat.tv_usec);
if (!v4l2buf) {
ERROR("invalid buffer");
return -1;
}
ret = ioctl(v4l2->fd, VIDIOC_QBUF, v4l2buf);
v4l2buf->m.fd = buf->fd[0];
if (ret) {
ERROR("VIDIOC_QBUF failed: %s (%d)", strerror(errno), ret);
}
return ret;
#endif
}
int
v4l2_dqbuf(struct v4l2 *v4l2, int* field)
{
struct v4l2_buffer v4l2buf = {
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
.memory = V4L2_MEMORY_DMABUF,
};
int ret;
ret = ioctl(v4l2->fd, VIDIOC_DQBUF, &v4l2buf);
if (ret) {
ERROR("VIDIOC_DQBUF failed: %s (%d)", strerror(errno), ret);
}
#if 1
DBG("vip: DQBUF idx = %d, field = %s\n", v4l2buf.index,
v4l2buf.field == V4L2_FIELD_TOP? "Top" : "Bottom");
*field = v4l2buf.field;
return v4l2buf.index;
#else
buf = v4l2->bufs[v4l2buf.index];
v4l2->v4l2bufs[v4l2buf.index].timestamp = v4l2buf.timestamp;
assert(buf->nbo == 1); /* TODO add multi-planar support */
tid = pthread_self();
gettimeofday(&dq_time, NULL);
timersub(&dq_time, &v4l2buf.timestamp, &dq_lat);
DBG("%x: DQ latency = %d.%6d", (unsigned int)tid, (int)dq_lat.tv_sec, (int)dq_lat.tv_usec);
return buf;
#endif
}