forked from raspberrypi/libpisp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv4l2_device.cpp
More file actions
276 lines (222 loc) · 6.58 KB
/
v4l2_device.cpp
File metadata and controls
276 lines (222 loc) · 6.58 KB
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
/* SPDX-License-Identifier: BSD-2-Clause */
/*
* Copyright (C) 2424 Raspberry Pi Ltd
*
* v4l2_device.cpp - PiSP V4L2 device helper
*/
#include <algorithm>
#include <assert.h>
#include <fcntl.h>
#include <map>
#include <poll.h>
#include <stdexcept>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <time.h>
#include "v4l2_device.hpp"
using namespace libpisp::helpers;
struct FormatInfo
{
unsigned int v4l2_pixfmt;
unsigned int num_memory_planes;
};
static FormatInfo get_v4l2_format(const std::string &format)
{
std::map<std::string, FormatInfo> formats {
{ "RGB888", { V4L2_PIX_FMT_RGB24, 1 } },
{ "RGBX8888", { V4L2_PIX_FMT_RGBX32, 1 } },
{ "YUV420P", { V4L2_PIX_FMT_YUV420, 1 } },
{ "YUV422P", { V4L2_PIX_FMT_YUV422P, 1 } },
{ "YUV444P", { V4L2_PIX_FMT_YUV444M, 3 } },
{ "YUYV", { V4L2_PIX_FMT_YUYV, 1 } },
{ "UYVY", { V4L2_PIX_FMT_UYVY, 1 } },
};
auto it = formats.find(format);
if (it == formats.end())
return { 0, {} };
return it->second;
}
V4l2Device::V4l2Device(const std::string &device)
: fd_(device, O_RDWR | O_NONBLOCK | O_CLOEXEC), num_memory_planes_(1)
{
struct v4l2_capability caps;
int ret = ioctl(fd_.Get(), VIDIOC_QUERYCAP, &caps);
if (ret < 0)
throw std::runtime_error("Cannot query device caps");
if (caps.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
buf_type_ = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
else if (caps.capabilities & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
buf_type_ = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
else if (caps.capabilities & V4L2_CAP_META_OUTPUT)
buf_type_ = V4L2_BUF_TYPE_META_OUTPUT;
else
throw std::runtime_error("Invalid buffer_type - caps: " + std::to_string(caps.capabilities));
}
V4l2Device::~V4l2Device()
{
ReleaseBuffers();
Close();
}
int V4l2Device::RequestBuffers(unsigned int count)
{
int ret;
ReleaseBuffers();
v4l2_requestbuffers req_bufs {};
req_bufs.count = count;
req_bufs.type = buf_type_;
req_bufs.memory = V4L2_MEMORY_MMAP;
ret = ioctl(fd_.Get(), VIDIOC_REQBUFS, &req_bufs);
if (ret < 0)
throw std::runtime_error("VIDIOC_REQBUFS failed: " + std::to_string(ret));
for (unsigned int i = 0; i < req_bufs.count; i++)
{
v4l2_plane planes[VIDEO_MAX_PLANES] = {};
v4l2_buffer buffer {};
buffer.index = i;
buffer.type = buf_type_;
buffer.memory = V4L2_MEMORY_MMAP;
if (!isMeta())
{
buffer.m.planes = planes;
buffer.length = num_memory_planes_;
}
ret = ioctl(fd_.Get(), VIDIOC_QUERYBUF, &buffer);
if (ret < 0)
throw std::runtime_error("VIDIOC_QUERYBUF failed: " + std::to_string(ret));
// Don't keep this pointer dangling when putting into v4l2_buffers_.
buffer.m.planes = NULL;
v4l2_buffers_.emplace_back(buffer);
available_buffers_.push(i);
for (unsigned int p = 0; p < num_memory_planes_; p++)
{
size_t size = !isMeta() ? planes[p].length : buffer.length;
unsigned int offset = !isMeta() ? planes[p].m.mem_offset : buffer.m.offset;
void *mem = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_.Get(), offset);
if (mem == MAP_FAILED)
throw std::runtime_error("Unable to mmap buffer");
v4l2_buffers_.back().size[p] = size;
v4l2_buffers_.back().mem[p] = (uint8_t *)mem;
}
}
return v4l2_buffers_.size();
}
void V4l2Device::ReleaseBuffers()
{
v4l2_requestbuffers req_bufs {};
if (!v4l2_buffers_.size())
return;
for (auto const &b : v4l2_buffers_)
{
for (unsigned int p = 0; p < num_memory_planes_; p++)
munmap(b.mem[p], b.size[p]);
}
req_bufs.type = buf_type_;
req_bufs.count = 0;
req_bufs.memory = V4L2_MEMORY_MMAP;
ioctl(fd_.Get(), VIDIOC_REQBUFS, &req_bufs);
v4l2_buffers_.clear();
}
std::optional<V4l2Device::Buffer> V4l2Device::GetBuffer()
{
if (available_buffers_.empty())
return {};
unsigned int index = available_buffers_.front();
available_buffers_.pop();
return findBuffer(index);
}
int V4l2Device::QueueBuffer(unsigned int index)
{
std::optional<Buffer> buf = findBuffer(index);
if (!buf)
return -1;
v4l2_plane planes[VIDEO_MAX_PLANES];
if (!isMeta())
{
buf->buffer.m.planes = planes;
buf->buffer.length = num_memory_planes_;
for (unsigned int p = 0; p < num_memory_planes_; p++)
{
buf->buffer.m.planes[p].bytesused = buf->size[p];
buf->buffer.m.planes[p].length = buf->size[p];
}
}
else
buf->buffer.bytesused = buf->size[0];
buf->buffer.timestamp.tv_sec = time(NULL);
buf->buffer.field = V4L2_FIELD_NONE;
buf->buffer.flags = 0;
int ret = ioctl(fd_.Get(), VIDIOC_QBUF, &buf->buffer);
if (ret < 0)
throw std::runtime_error("Unable to queue buffer: " + std::string(strerror(errno)));
return ret;
}
int V4l2Device::DequeueBuffer(unsigned int timeout_ms)
{
short int poll_event = isOutput() ? POLLOUT : POLLIN;
pollfd p = { fd_.Get(), poll_event, 0 };
int ret = poll(&p, 1, timeout_ms);
if (ret <= 0)
return -1;
if (!(p.revents & poll_event))
return -1;
v4l2_buffer buf = {};
v4l2_plane planes[VIDEO_MAX_PLANES] = {};
buf.type = buf_type_;
buf.memory = V4L2_MEMORY_MMAP;
if (!isMeta())
{
buf.m.planes = planes;
buf.length = VIDEO_MAX_PLANES;
}
ret = ioctl(fd_.Get(), VIDIOC_DQBUF, &buf);
if (ret)
return -1;
available_buffers_.push(buf.index);
return buf.index;
}
void V4l2Device::SetFormat(unsigned int width, unsigned int height, unsigned int stride, unsigned int stride2,
const std::string &format)
{
struct v4l2_format f = {};
FormatInfo info = get_v4l2_format(format);
assert(info.v4l2_pixfmt);
num_memory_planes_ = info.num_memory_planes;
f.type = buf_type_;
f.fmt.pix_mp.width = width;
f.fmt.pix_mp.height = height;
f.fmt.pix_mp.pixelformat = info.v4l2_pixfmt;
f.fmt.pix_mp.field = V4L2_FIELD_NONE;
f.fmt.pix_mp.num_planes = num_memory_planes_;
for (unsigned int p = 0; p < num_memory_planes_; p++)
{
f.fmt.pix_mp.plane_fmt[p].bytesperline = p == 0 ? stride : stride2;
f.fmt.pix_mp.plane_fmt[p].sizeimage = 0;
}
int ret = ioctl(fd_.Get(), VIDIOC_S_FMT, &f);
if (ret)
throw std::runtime_error("Unable to set format: " + std::string(strerror(errno)));
}
void V4l2Device::StreamOn()
{
int ret = ioctl(fd_.Get(), VIDIOC_STREAMON, &buf_type_);
if (ret < 0)
throw std::runtime_error("Stream on failed: " + std::string(strerror(errno)));
}
void V4l2Device::StreamOff()
{
int ret = ioctl(fd_.Get(), VIDIOC_STREAMOFF, &buf_type_);
if (ret < 0)
throw std::runtime_error("Stream off failed: " + std::string(strerror(errno)));
}
std::optional<V4l2Device::Buffer> V4l2Device::findBuffer(unsigned int index) const
{
auto it = std::find_if(v4l2_buffers_.begin(), v4l2_buffers_.end(),
[index](auto const &b) { return b.buffer.index == index; });
if (it == v4l2_buffers_.end())
{
throw std::runtime_error("find buffers failed");
return {};
}
return *it;
}