| 1 |
/* |
| 2 |
# (C) 2008 Hans de Goede <[email protected]> |
| 3 |
|
| 4 |
# This program is free software; you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU Lesser General Public License as published by |
| 6 |
# the Free Software Foundation; either version 2.1 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
# Lesser General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU Lesser General Public License |
| 15 |
# along with this program; if not, write to the Free Software |
| 16 |
# Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA |
| 17 |
*/ |
| 18 |
|
| 19 |
/* MAKING CHANGES TO THIS FILE?? READ THIS FIRST!!! |
| 20 |
|
| 21 |
This file implements libv4l2, which offers v4l2_ prefixed versions of |
| 22 |
open/close/etc. The API is 100% the same as directly opening /dev/videoX |
| 23 |
using regular open/close/etc, the big difference is that format conversion |
| 24 |
is done if necessary when capturing. That is if you (try to) set a capture |
| 25 |
format which is not supported by the cam, but is supported by libv4lconvert, |
| 26 |
then the try_fmt / set_fmt will succeed as if the cam supports the format |
| 27 |
and on dqbuf / read the data will be converted for you and returned in |
| 28 |
the request format. |
| 29 |
|
| 30 |
Important note to people making changes to this file: All functions |
| 31 |
(v4l2_close, v4l2_ioctl, etc.) are designed to function as their regular |
| 32 |
counterpart when they get passed a fd that is not "registered" by libv4l2, |
| 33 |
there are 2 reasons for this: |
| 34 |
1) This allows us to get completely out of the way when dealing with non |
| 35 |
capture devices. |
| 36 |
2) libv4l2 is the base of the v4l2convert.so wrapper lib, which is a .so |
| 37 |
which can be LD_PRELOAD-ed and the overrules the libc's open/close/etc, |
| 38 |
and when opening /dev/videoX or /dev/v4l/ calls v4l2_open. Because we |
| 39 |
behave as the regular counterpart when the fd is not known (instead of say |
| 40 |
throwing an error), v4l2convert.so can simply call the v4l2_ prefixed |
| 41 |
function for all wrapped functions (except for v4l2_open which will fail |
| 42 |
when not called on a v4l2 device). This way the wrapper does not have to |
| 43 |
keep track of which fd's are being handled by libv4l2, as libv4l2 already |
| 44 |
keeps track of this itself. |
| 45 |
|
| 46 |
This also means that libv4l2 may not use any of the regular functions |
| 47 |
it mimics, as for example open could be a symbol in v4l2convert.so, which |
| 48 |
in turn will call v4l2_open, so therefore v4l2_open (for example) may not |
| 49 |
use the regular open()! |
| 50 |
|
| 51 |
Another important note: libv4l2 does conversion for capture usage only, if |
| 52 |
any calls are made which are passed a v4l2_buffer or v4l2_format with a |
| 53 |
v4l2_buf_type which is different from V4L2_BUF_TYPE_VIDEO_CAPTURE, then |
| 54 |
the v4l2_ methods behave exactly the same as their regular counterparts. |
| 55 |
When modifications are made, one should be careful that this behavior is |
| 56 |
preserved. |
| 57 |
*/ |
| 58 |
#include <errno.h> |
| 59 |
#include <stdarg.h> |
| 60 |
#include <stdio.h> |
| 61 |
#include <stdlib.h> |
| 62 |
#include <fcntl.h> |
| 63 |
#include <string.h> |
| 64 |
#include <unistd.h> |
| 65 |
#include <sys/types.h> |
| 66 |
#include <sys/mman.h> |
| 67 |
#include <sys/stat.h> |
| 68 |
#include "libv4l2.h" |
| 69 |
#include "libv4l2-priv.h" |
| 70 |
#include "libv4l-plugin.h" |
| 71 |
|
| 72 |
/* Note these flags are stored together with the flags passed to v4l2_fd_open() |
| 73 |
in v4l2_dev_info's flags member, so care should be taken that the do not |
| 74 |
use the same bits! */ |
| 75 |
#define V4L2_STREAMON 0x0100 |
| 76 |
#define V4L2_BUFFERS_REQUESTED_BY_READ 0x0200 |
| 77 |
#define V4L2_STREAM_CONTROLLED_BY_READ 0x0400 |
| 78 |
#define V4L2_SUPPORTS_READ 0x0800 |
| 79 |
#define V4L2_STREAM_TOUCHED 0x1000 |
| 80 |
#define V4L2_USE_READ_FOR_READ 0x2000 |
| 81 |
#define V4L2_SUPPORTS_TIMEPERFRAME 0x4000 |
| 82 |
|
| 83 |
#define V4L2_MMAP_OFFSET_MAGIC 0xABCDEF00u |
| 84 |
|
| 85 |
static void v4l2_adjust_src_fmt_to_fps(int index, int fps); |
| 86 |
static void v4l2_set_src_and_dest_format(int index, |
| 87 |
struct v4l2_format *src_fmt, struct v4l2_format *dest_fmt); |
| 88 |
|
| 89 |
static pthread_mutex_t v4l2_open_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 90 |
static struct v4l2_dev_info devices[V4L2_MAX_DEVICES] = { |
| 91 |
{ .fd = -1 }, |
| 92 |
{ .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, |
| 93 |
{ .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, |
| 94 |
{ .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 } |
| 95 |
}; |
| 96 |
static int devices_used; |
| 97 |
|
| 98 |
static int v4l2_ensure_convert_mmap_buf(int index) |
| 99 |
{ |
| 100 |
if (devices[index].convert_mmap_buf != MAP_FAILED) { |
| 101 |
return 0; |
| 102 |
} |
| 103 |
|
| 104 |
devices[index].convert_mmap_buf_size = |
| 105 |
devices[index].convert_mmap_frame_size * devices[index].no_frames; |
| 106 |
|
| 107 |
devices[index].convert_mmap_buf = (void *)SYS_MMAP(NULL, |
| 108 |
devices[index].convert_mmap_buf_size, |
| 109 |
PROT_READ | PROT_WRITE, |
| 110 |
MAP_ANONYMOUS | MAP_PRIVATE, |
| 111 |
-1, 0); |
| 112 |
|
| 113 |
if (devices[index].convert_mmap_buf == MAP_FAILED) { |
| 114 |
devices[index].convert_mmap_buf_size = 0; |
| 115 |
|
| 116 |
int saved_err = errno; |
| 117 |
V4L2_LOG_ERR("allocating conversion buffer\n"); |
| 118 |
errno = saved_err; |
| 119 |
return -1; |
| 120 |
} |
| 121 |
|
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
static int v4l2_request_read_buffers(int index) |
| 126 |
{ |
| 127 |
int result; |
| 128 |
struct v4l2_requestbuffers req; |
| 129 |
|
| 130 |
/* Note we re-request the buffers if they are already requested as the format |
| 131 |
and thus the needed buffer size may have changed. */ |
| 132 |
req.count = (devices[index].no_frames) ? devices[index].no_frames : |
| 133 |
devices[index].nreadbuffers; |
| 134 |
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 135 |
req.memory = V4L2_MEMORY_MMAP; |
| 136 |
result = devices[index].dev_ops->ioctl(devices[index].dev_ops_priv, |
| 137 |
devices[index].fd, VIDIOC_REQBUFS, &req); |
| 138 |
if (result < 0) { |
| 139 |
int saved_err = errno; |
| 140 |
|
| 141 |
V4L2_LOG("warning reqbuf (%u) failed: %s\n", req.count, strerror(errno)); |
| 142 |
errno = saved_err; |
| 143 |
return result; |
| 144 |
} |
| 145 |
|
| 146 |
if (!devices[index].no_frames && req.count) |
| 147 |
devices[index].flags |= V4L2_BUFFERS_REQUESTED_BY_READ; |
| 148 |
|
| 149 |
devices[index].no_frames = MIN(req.count, V4L2_MAX_NO_FRAMES); |
| 150 |
return 0; |
| 151 |
} |
| 152 |
|
| 153 |
static void v4l2_unrequest_read_buffers(int index) |
| 154 |
{ |
| 155 |
struct v4l2_requestbuffers req; |
| 156 |
|
| 157 |
if (!(devices[index].flags & V4L2_BUFFERS_REQUESTED_BY_READ) || |
| 158 |
devices[index].no_frames == 0) |
| 159 |
return; |
| 160 |
|
| 161 |
/* (Un)Request buffers, note not all driver support this, and those |
| 162 |
who do not support it don't need it. */ |
| 163 |
req.count = 0; |
| 164 |
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 165 |
req.memory = V4L2_MEMORY_MMAP; |
| 166 |
if (devices[index].dev_ops->ioctl(devices[index].dev_ops_priv, |
| 167 |
devices[index].fd, VIDIOC_REQBUFS, &req) < 0) |
| 168 |
return; |
| 169 |
|
| 170 |
devices[index].no_frames = MIN(req.count, V4L2_MAX_NO_FRAMES); |
| 171 |
if (devices[index].no_frames == 0) |
| 172 |
devices[index].flags &= ~V4L2_BUFFERS_REQUESTED_BY_READ; |
| 173 |
} |
| 174 |
|
| 175 |
static int v4l2_map_buffers(int index) |
| 176 |
{ |
| 177 |
int result = 0; |
| 178 |
unsigned int i; |
| 179 |
struct v4l2_buffer buf; |
| 180 |
|
| 181 |
for (i = 0; i < devices[index].no_frames; i++) { |
| 182 |
if (devices[index].frame_pointers[i] != MAP_FAILED) |
| 183 |
continue; |
| 184 |
|
| 185 |
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 186 |
buf.memory = V4L2_MEMORY_MMAP; |
| 187 |
buf.index = i; |
| 188 |
buf.reserved = buf.reserved2 = 0; |
| 189 |
result = devices[index].dev_ops->ioctl( |
| 190 |
devices[index].dev_ops_priv, |
| 191 |
devices[index].fd, VIDIOC_QUERYBUF, &buf); |
| 192 |
if (result) { |
| 193 |
int saved_err = errno; |
| 194 |
|
| 195 |
V4L2_PERROR("querying buffer %u", i); |
| 196 |
errno = saved_err; |
| 197 |
break; |
| 198 |
} |
| 199 |
|
| 200 |
devices[index].frame_pointers[i] = (void *)SYS_MMAP(NULL, |
| 201 |
(size_t)buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, devices[index].fd, |
| 202 |
buf.m.offset); |
| 203 |
if (devices[index].frame_pointers[i] == MAP_FAILED) { |
| 204 |
int saved_err = errno; |
| 205 |
|
| 206 |
V4L2_PERROR("mmapping buffer %u", i); |
| 207 |
errno = saved_err; |
| 208 |
result = -1; |
| 209 |
break; |
| 210 |
} |
| 211 |
V4L2_LOG("mapped buffer %u at %p\n", i, |
| 212 |
devices[index].frame_pointers[i]); |
| 213 |
|
| 214 |
devices[index].frame_sizes[i] = buf.length; |
| 215 |
} |
| 216 |
|
| 217 |
return result; |
| 218 |
} |
| 219 |
|
| 220 |
static void v4l2_unmap_buffers(int index) |
| 221 |
{ |
| 222 |
unsigned int i; |
| 223 |
|
| 224 |
/* unmap the buffers */ |
| 225 |
for (i = 0; i < devices[index].no_frames; i++) { |
| 226 |
if (devices[index].frame_pointers[i] != MAP_FAILED) { |
| 227 |
SYS_MUNMAP(devices[index].frame_pointers[i], |
| 228 |
devices[index].frame_sizes[i]); |
| 229 |
devices[index].frame_pointers[i] = MAP_FAILED; |
| 230 |
V4L2_LOG("unmapped buffer %u\n", i); |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
static int v4l2_streamon(int index) |
| 236 |
{ |
| 237 |
int result; |
| 238 |
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 239 |
|
| 240 |
if (!(devices[index].flags & V4L2_STREAMON)) { |
| 241 |
result = devices[index].dev_ops->ioctl( |
| 242 |
devices[index].dev_ops_priv, |
| 243 |
devices[index].fd, VIDIOC_STREAMON, &type); |
| 244 |
if (result) { |
| 245 |
int saved_err = errno; |
| 246 |
|
| 247 |
V4L2_PERROR("turning on stream"); |
| 248 |
errno = saved_err; |
| 249 |
return result; |
| 250 |
} |
| 251 |
devices[index].flags |= V4L2_STREAMON; |
| 252 |
devices[index].first_frame = V4L2_IGNORE_FIRST_FRAME_ERRORS; |
| 253 |
} |
| 254 |
|
| 255 |
return 0; |
| 256 |
} |
| 257 |
|
| 258 |
static int v4l2_streamoff(int index) |
| 259 |
{ |
| 260 |
int result; |
| 261 |
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 262 |
|
| 263 |
if (devices[index].flags & V4L2_STREAMON) { |
| 264 |
result = devices[index].dev_ops->ioctl( |
| 265 |
devices[index].dev_ops_priv, |
| 266 |
devices[index].fd, VIDIOC_STREAMOFF, &type); |
| 267 |
if (result) { |
| 268 |
int saved_err = errno; |
| 269 |
|
| 270 |
V4L2_PERROR("turning off stream"); |
| 271 |
errno = saved_err; |
| 272 |
return result; |
| 273 |
} |
| 274 |
devices[index].flags &= ~V4L2_STREAMON; |
| 275 |
|
| 276 |
/* Stream off also dequeues all our buffers! */ |
| 277 |
devices[index].frame_queued = 0; |
| 278 |
} |
| 279 |
|
| 280 |
return 0; |
| 281 |
} |
| 282 |
|
| 283 |
static int v4l2_queue_read_buffer(int index, int buffer_index) |
| 284 |
{ |
| 285 |
int result; |
| 286 |
struct v4l2_buffer buf; |
| 287 |
|
| 288 |
if (devices[index].frame_queued & (1 << buffer_index)) |
| 289 |
return 0; |
| 290 |
|
| 291 |
memset(&buf, 0, sizeof(buf)); |
| 292 |
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 293 |
buf.memory = V4L2_MEMORY_MMAP; |
| 294 |
buf.index = buffer_index; |
| 295 |
result = devices[index].dev_ops->ioctl(devices[index].dev_ops_priv, |
| 296 |
devices[index].fd, VIDIOC_QBUF, &buf); |
| 297 |
if (result) { |
| 298 |
int saved_err = errno; |
| 299 |
|
| 300 |
V4L2_PERROR("queuing buf %d", buffer_index); |
| 301 |
errno = saved_err; |
| 302 |
return result; |
| 303 |
} |
| 304 |
|
| 305 |
devices[index].frame_queued |= 1 << buffer_index; |
| 306 |
return 0; |
| 307 |
} |
| 308 |
|
| 309 |
static int v4l2_dequeue_and_convert(int index, struct v4l2_buffer *buf, |
| 310 |
unsigned char *dest, int dest_size) |
| 311 |
{ |
| 312 |
const int max_tries = V4L2_IGNORE_FIRST_FRAME_ERRORS + 1; |
| 313 |
int result, tries = max_tries, frame_info_gen; |
| 314 |
|
| 315 |
/* Make sure we have the real v4l2 buffers mapped */ |
| 316 |
result = v4l2_map_buffers(index); |
| 317 |
if (result) |
| 318 |
return result; |
| 319 |
|
| 320 |
do { |
| 321 |
frame_info_gen = devices[index].frame_info_generation; |
| 322 |
pthread_mutex_unlock(&devices[index].stream_lock); |
| 323 |
result = devices[index].dev_ops->ioctl( |
| 324 |
devices[index].dev_ops_priv, |
| 325 |
devices[index].fd, VIDIOC_DQBUF, buf); |
| 326 |
pthread_mutex_lock(&devices[index].stream_lock); |
| 327 |
if (result) { |
| 328 |
if (errno != EAGAIN) { |
| 329 |
int saved_err = errno; |
| 330 |
|
| 331 |
V4L2_PERROR("dequeuing buf"); |
| 332 |
errno = saved_err; |
| 333 |
} |
| 334 |
return result; |
| 335 |
} |
| 336 |
|
| 337 |
devices[index].frame_queued &= ~(1 << buf->index); |
| 338 |
|
| 339 |
if (frame_info_gen != devices[index].frame_info_generation) { |
| 340 |
errno = -EINVAL; |
| 341 |
return -1; |
| 342 |
} |
| 343 |
|
| 344 |
result = v4lconvert_convert(devices[index].convert, |
| 345 |
&devices[index].src_fmt, &devices[index].dest_fmt, |
| 346 |
devices[index].frame_pointers[buf->index], |
| 347 |
buf->bytesused, dest ? dest : (devices[index].convert_mmap_buf + |
| 348 |
buf->index * devices[index].convert_mmap_frame_size), |
| 349 |
dest_size); |
| 350 |
|
| 351 |
if (devices[index].first_frame) { |
| 352 |
/* Always treat convert errors as EAGAIN during the first few frames, as |
| 353 |
some cams produce bad frames at the start of the stream |
| 354 |
(hsync and vsync still syncing ??). */ |
| 355 |
if (result < 0) |
| 356 |
errno = EAGAIN; |
| 357 |
devices[index].first_frame--; |
| 358 |
} |
| 359 |
|
| 360 |
if (result < 0) { |
| 361 |
int saved_err = errno; |
| 362 |
|
| 363 |
if (errno == EAGAIN || errno == EPIPE) |
| 364 |
V4L2_LOG("warning error while converting frame data: %s", |
| 365 |
v4lconvert_get_error_message(devices[index].convert)); |
| 366 |
else |
| 367 |
V4L2_LOG_ERR("converting / decoding frame data: %s", |
| 368 |
v4lconvert_get_error_message(devices[index].convert)); |
| 369 |
|
| 370 |
/* |
| 371 |
* If this is the last try, and the frame is short |
| 372 |
* we will return the (short) buffer to the caller, |
| 373 |
* so we must not re-queue it then! |
| 374 |
*/ |
| 375 |
if (!(tries == 1 && errno == EPIPE)) |
| 376 |
v4l2_queue_read_buffer(index, buf->index); |
| 377 |
errno = saved_err; |
| 378 |
} |
| 379 |
tries--; |
| 380 |
} while (result < 0 && (errno == EAGAIN || errno == EPIPE) && tries); |
| 381 |
|
| 382 |
if (result < 0 && errno == EAGAIN) { |
| 383 |
V4L2_LOG_ERR("got %d consecutive frame decode errors, last error: %s", |
| 384 |
max_tries, v4lconvert_get_error_message(devices[index].convert)); |
| 385 |
errno = EIO; |
| 386 |
} |
| 387 |
|
| 388 |
if (result < 0 && errno == EPIPE) { |
| 389 |
V4L2_LOG("got %d consecutive short frame errors, " |
| 390 |
"returning short frame", max_tries); |
| 391 |
result = devices[index].dest_fmt.fmt.pix.sizeimage; |
| 392 |
errno = 0; |
| 393 |
} |
| 394 |
|
| 395 |
return result; |
| 396 |
} |
| 397 |
|
| 398 |
static int v4l2_read_and_convert(int index, unsigned char *dest, int dest_size) |
| 399 |
{ |
| 400 |
const int max_tries = V4L2_IGNORE_FIRST_FRAME_ERRORS + 1; |
| 401 |
int result, buf_size, tries = max_tries; |
| 402 |
|
| 403 |
buf_size = devices[index].dest_fmt.fmt.pix.sizeimage; |
| 404 |
|
| 405 |
if (devices[index].readbuf_size < buf_size) { |
| 406 |
unsigned char *new_buf; |
| 407 |
|
| 408 |
new_buf = realloc(devices[index].readbuf, buf_size); |
| 409 |
if (!new_buf) |
| 410 |
return -1; |
| 411 |
|
| 412 |
devices[index].readbuf = new_buf; |
| 413 |
devices[index].readbuf_size = buf_size; |
| 414 |
} |
| 415 |
|
| 416 |
do { |
| 417 |
result = devices[index].dev_ops->read( |
| 418 |
devices[index].dev_ops_priv, |
| 419 |
devices[index].fd, devices[index].readbuf, |
| 420 |
buf_size); |
| 421 |
if (result <= 0) { |
| 422 |
if (result && errno != EAGAIN) { |
| 423 |
int saved_err = errno; |
| 424 |
|
| 425 |
V4L2_PERROR("reading"); |
| 426 |
errno = saved_err; |
| 427 |
} |
| 428 |
return result; |
| 429 |
} |
| 430 |
|
| 431 |
result = v4lconvert_convert(devices[index].convert, |
| 432 |
&devices[index].src_fmt, &devices[index].dest_fmt, |
| 433 |
devices[index].readbuf, result, dest, dest_size); |
| 434 |
|
| 435 |
if (devices[index].first_frame) { |
| 436 |
/* Always treat convert errors as EAGAIN during the first few frames, as |
| 437 |
some cams produce bad frames at the start of the stream |
| 438 |
(hsync and vsync still syncing ??). */ |
| 439 |
if (result < 0) |
| 440 |
errno = EAGAIN; |
| 441 |
devices[index].first_frame--; |
| 442 |
} |
| 443 |
|
| 444 |
if (result < 0) { |
| 445 |
int saved_err = errno; |
| 446 |
|
| 447 |
if (errno == EAGAIN || errno == EPIPE) |
| 448 |
V4L2_LOG("warning error while converting frame data: %s", |
| 449 |
v4lconvert_get_error_message(devices[index].convert)); |
| 450 |
else |
| 451 |
V4L2_LOG_ERR("converting / decoding frame data: %s", |
| 452 |
v4lconvert_get_error_message(devices[index].convert)); |
| 453 |
|
| 454 |
errno = saved_err; |
| 455 |
} |
| 456 |
tries--; |
| 457 |
} while (result < 0 && (errno == EAGAIN || errno == EPIPE) && tries); |
| 458 |
|
| 459 |
if (result < 0 && errno == EAGAIN) { |
| 460 |
V4L2_LOG_ERR("got %d consecutive frame decode errors, last error: %s", |
| 461 |
max_tries, v4lconvert_get_error_message(devices[index].convert)); |
| 462 |
errno = EIO; |
| 463 |
} |
| 464 |
|
| 465 |
if (result < 0 && errno == EPIPE) { |
| 466 |
V4L2_LOG("got %d consecutive short frame errors, " |
| 467 |
"returning short frame", max_tries); |
| 468 |
result = devices[index].dest_fmt.fmt.pix.sizeimage; |
| 469 |
errno = 0; |
| 470 |
} |
| 471 |
|
| 472 |
return result; |
| 473 |
} |
| 474 |
|
| 475 |
static int v4l2_queue_read_buffers(int index) |
| 476 |
{ |
| 477 |
unsigned int i; |
| 478 |
int last_error = EIO, queued = 0; |
| 479 |
|
| 480 |
for (i = 0; i < devices[index].no_frames; i++) { |
| 481 |
/* Don't queue unmapped buffers (should never happen) */ |
| 482 |
if (devices[index].frame_pointers[i] != MAP_FAILED) { |
| 483 |
if (v4l2_queue_read_buffer(index, i)) { |
| 484 |
last_error = errno; |
| 485 |
continue; |
| 486 |
} |
| 487 |
queued++; |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
if (!queued) { |
| 492 |
errno = last_error; |
| 493 |
return -1; |
| 494 |
} |
| 495 |
return 0; |
| 496 |
} |
| 497 |
|
| 498 |
static int v4l2_activate_read_stream(int index) |
| 499 |
{ |
| 500 |
int result; |
| 501 |
|
| 502 |
if ((devices[index].flags & V4L2_STREAMON) || devices[index].frame_queued) { |
| 503 |
errno = EBUSY; |
| 504 |
return -1; |
| 505 |
} |
| 506 |
|
| 507 |
result = v4l2_request_read_buffers(index); |
| 508 |
if (!result) |
| 509 |
result = v4l2_map_buffers(index); |
| 510 |
if (!result) |
| 511 |
result = v4l2_queue_read_buffers(index); |
| 512 |
if (result) |
| 513 |
return result; |
| 514 |
|
| 515 |
devices[index].flags |= V4L2_STREAM_CONTROLLED_BY_READ; |
| 516 |
|
| 517 |
return v4l2_streamon(index); |
| 518 |
} |
| 519 |
|
| 520 |
static int v4l2_deactivate_read_stream(int index) |
| 521 |
{ |
| 522 |
int result; |
| 523 |
|
| 524 |
result = v4l2_streamoff(index); |
| 525 |
if (result) |
| 526 |
return result; |
| 527 |
|
| 528 |
/* No need to dequeue our buffers, streamoff does that for us */ |
| 529 |
|
| 530 |
v4l2_unmap_buffers(index); |
| 531 |
|
| 532 |
v4l2_unrequest_read_buffers(index); |
| 533 |
|
| 534 |
devices[index].flags &= ~V4L2_STREAM_CONTROLLED_BY_READ; |
| 535 |
|
| 536 |
return 0; |
| 537 |
} |
| 538 |
|
| 539 |
static int v4l2_needs_conversion(int index) |
| 540 |
{ |
| 541 |
if (devices[index].convert == NULL) |
| 542 |
return 0; |
| 543 |
|
| 544 |
return v4lconvert_needs_conversion(devices[index].convert, |
| 545 |
&devices[index].src_fmt, &devices[index].dest_fmt); |
| 546 |
} |
| 547 |
|
| 548 |
static void v4l2_set_conversion_buf_params(int index, struct v4l2_buffer *buf) |
| 549 |
{ |
| 550 |
if (!v4l2_needs_conversion(index)) |
| 551 |
return; |
| 552 |
|
| 553 |
/* This may happen if the ioctl failed */ |
| 554 |
if (buf->index >= devices[index].no_frames) |
| 555 |
buf->index = 0; |
| 556 |
|
| 557 |
buf->m.offset = V4L2_MMAP_OFFSET_MAGIC | buf->index; |
| 558 |
buf->length = devices[index].convert_mmap_frame_size; |
| 559 |
if (devices[index].frame_map_count[buf->index]) |
| 560 |
buf->flags |= V4L2_BUF_FLAG_MAPPED; |
| 561 |
else |
| 562 |
buf->flags &= ~V4L2_BUF_FLAG_MAPPED; |
| 563 |
} |
| 564 |
|
| 565 |
static int v4l2_buffers_mapped(int index) |
| 566 |
{ |
| 567 |
unsigned int i; |
| 568 |
|
| 569 |
if (!v4l2_needs_conversion(index)) { |
| 570 |
/* Normal (no conversion) mode */ |
| 571 |
struct v4l2_buffer buf; |
| 572 |
|
| 573 |
for (i = 0; i < devices[index].no_frames; i++) { |
| 574 |
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 575 |
buf.memory = V4L2_MEMORY_MMAP; |
| 576 |
buf.index = i; |
| 577 |
buf.reserved = buf.reserved2 = 0; |
| 578 |
if (devices[index].dev_ops->ioctl( |
| 579 |
devices[index].dev_ops_priv, |
| 580 |
devices[index].fd, VIDIOC_QUERYBUF, |
| 581 |
&buf)) { |
| 582 |
int saved_err = errno; |
| 583 |
|
| 584 |
V4L2_PERROR("querying buffer %u", i); |
| 585 |
errno = saved_err; |
| 586 |
break; |
| 587 |
} |
| 588 |
if (buf.flags & V4L2_BUF_FLAG_MAPPED) |
| 589 |
break; |
| 590 |
} |
| 591 |
} else { |
| 592 |
/* Conversion mode */ |
| 593 |
for (i = 0; i < devices[index].no_frames; i++) |
| 594 |
if (devices[index].frame_map_count[i]) |
| 595 |
break; |
| 596 |
} |
| 597 |
|
| 598 |
if (i != devices[index].no_frames) |
| 599 |
V4L2_LOG("v4l2_buffers_mapped(): buffers still mapped\n"); |
| 600 |
|
| 601 |
return i != devices[index].no_frames; |
| 602 |
} |
| 603 |
|
| 604 |
static void v4l2_update_fps(int index, struct v4l2_streamparm *parm) |
| 605 |
{ |
| 606 |
if ((devices[index].flags & V4L2_SUPPORTS_TIMEPERFRAME) && |
| 607 |
parm->parm.capture.timeperframe.numerator != 0) { |
| 608 |
int fps = parm->parm.capture.timeperframe.denominator; |
| 609 |
fps += parm->parm.capture.timeperframe.numerator - 1; |
| 610 |
fps /= parm->parm.capture.timeperframe.numerator; |
| 611 |
devices[index].fps = fps; |
| 612 |
} else |
| 613 |
devices[index].fps = 0; |
| 614 |
} |
| 615 |
|
| 616 |
int v4l2_open(const char *file, int oflag, ...) |
| 617 |
{ |
| 618 |
int fd; |
| 619 |
|
| 620 |
/* original open code */ |
| 621 |
if (oflag & O_CREAT) { |
| 622 |
va_list ap; |
| 623 |
mode_t mode; |
| 624 |
|
| 625 |
va_start(ap, oflag); |
| 626 |
mode = va_arg(ap, PROMOTED_MODE_T); |
| 627 |
|
| 628 |
fd = SYS_OPEN(file, oflag, mode); |
| 629 |
|
| 630 |
va_end(ap); |
| 631 |
} else { |
| 632 |
fd = SYS_OPEN(file, oflag, 0); |
| 633 |
} |
| 634 |
/* end of original open code */ |
| 635 |
|
| 636 |
if (fd == -1) |
| 637 |
return fd; |
| 638 |
|
| 639 |
if (v4l2_fd_open(fd, 0) == -1) { |
| 640 |
int saved_err = errno; |
| 641 |
|
| 642 |
SYS_CLOSE(fd); |
| 643 |
errno = saved_err; |
| 644 |
return -1; |
| 645 |
} |
| 646 |
|
| 647 |
return fd; |
| 648 |
} |
| 649 |
|
| 650 |
int v4l2_fd_open(int fd, int v4l2_flags) |
| 651 |
{ |
| 652 |
int i, index; |
| 653 |
char *lfname; |
| 654 |
struct v4l2_capability cap; |
| 655 |
struct v4l2_format fmt = { 0, }; |
| 656 |
struct v4l2_streamparm parm = { 0, }; |
| 657 |
struct v4lconvert_data *convert = NULL; |
| 658 |
void *plugin_library; |
| 659 |
void *dev_ops_priv; |
| 660 |
const struct libv4l_dev_ops *dev_ops; |
| 661 |
long page_size; |
| 662 |
|
| 663 |
v4l2_plugin_init(fd, &plugin_library, &dev_ops_priv, &dev_ops); |
| 664 |
|
| 665 |
/* If no log file was set by the app, see if one was specified through the |
| 666 |
environment */ |
| 667 |
if (!v4l2_log_file) { |
| 668 |
lfname = getenv("LIBV4L2_LOG_FILENAME"); |
| 669 |
if (lfname) |
| 670 |
v4l2_log_file = fopen(lfname, "w"); |
| 671 |
} |
| 672 |
|
| 673 |
/* Get page_size (for mmap emulation) */ |
| 674 |
page_size = sysconf(_SC_PAGESIZE); |
| 675 |
if (page_size < 0) { |
| 676 |
int saved_err = errno; |
| 677 |
V4L2_LOG_ERR("unable to retrieve page size: %s\n", |
| 678 |
strerror(errno)); |
| 679 |
v4l2_plugin_cleanup(plugin_library, dev_ops_priv, dev_ops); |
| 680 |
errno = saved_err; |
| 681 |
return -1; |
| 682 |
} |
| 683 |
|
| 684 |
/* check that this is a v4l2 device */ |
| 685 |
if (dev_ops->ioctl(dev_ops_priv, fd, VIDIOC_QUERYCAP, &cap)) { |
| 686 |
int saved_err = errno; |
| 687 |
V4L2_LOG_ERR("getting capabilities: %s\n", strerror(errno)); |
| 688 |
v4l2_plugin_cleanup(plugin_library, dev_ops_priv, dev_ops); |
| 689 |
errno = saved_err; |
| 690 |
return -1; |
| 691 |
} |
| 692 |
|
| 693 |
if (cap.capabilities & V4L2_CAP_DEVICE_CAPS) |
| 694 |
cap.capabilities = cap.device_caps; |
| 695 |
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) || |
| 696 |
!(cap.capabilities & (V4L2_CAP_STREAMING | V4L2_CAP_READWRITE))) |
| 697 |
goto no_capture; |
| 698 |
|
| 699 |
/* Get current cam format */ |
| 700 |
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 701 |
if (dev_ops->ioctl(dev_ops_priv, fd, VIDIOC_G_FMT, &fmt)) { |
| 702 |
int saved_err = errno; |
| 703 |
V4L2_LOG_ERR("getting pixformat: %s\n", strerror(errno)); |
| 704 |
v4l2_plugin_cleanup(plugin_library, dev_ops_priv, dev_ops); |
| 705 |
errno = saved_err; |
| 706 |
return -1; |
| 707 |
} |
| 708 |
|
| 709 |
/* Check for frame rate setting support */ |
| 710 |
parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 711 |
if (dev_ops->ioctl(dev_ops_priv, fd, VIDIOC_G_PARM, &parm)) |
| 712 |
parm.type = 0; |
| 713 |
|
| 714 |
/* init libv4lconvert */ |
| 715 |
if (!(v4l2_flags & V4L2_DISABLE_CONVERSION)) { |
| 716 |
convert = v4lconvert_create_with_dev_ops(fd, dev_ops_priv, dev_ops); |
| 717 |
if (!convert) { |
| 718 |
int saved_err = errno; |
| 719 |
v4l2_plugin_cleanup(plugin_library, dev_ops_priv, |
| 720 |
dev_ops); |
| 721 |
errno = saved_err; |
| 722 |
return -1; |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
no_capture: |
| 727 |
/* So we have a v4l2 capture device, register it in our devices array */ |
| 728 |
pthread_mutex_lock(&v4l2_open_mutex); |
| 729 |
for (index = 0; index < V4L2_MAX_DEVICES; index++) { |
| 730 |
if (devices[index].fd == -1) { |
| 731 |
devices[index].fd = fd; |
| 732 |
devices[index].plugin_library = plugin_library; |
| 733 |
devices[index].dev_ops_priv = dev_ops_priv; |
| 734 |
devices[index].dev_ops = dev_ops; |
| 735 |
break; |
| 736 |
} |
| 737 |
} |
| 738 |
pthread_mutex_unlock(&v4l2_open_mutex); |
| 739 |
|
| 740 |
if (index == V4L2_MAX_DEVICES) { |
| 741 |
V4L2_LOG_ERR("attempting to open more than %d video devices\n", |
| 742 |
V4L2_MAX_DEVICES); |
| 743 |
v4l2_plugin_cleanup(plugin_library, dev_ops_priv, dev_ops); |
| 744 |
errno = EBUSY; |
| 745 |
return -1; |
| 746 |
} |
| 747 |
|
| 748 |
devices[index].flags = v4l2_flags; |
| 749 |
if (cap.capabilities & V4L2_CAP_READWRITE) |
| 750 |
devices[index].flags |= V4L2_SUPPORTS_READ; |
| 751 |
if (!(cap.capabilities & V4L2_CAP_STREAMING)) { |
| 752 |
devices[index].flags |= V4L2_USE_READ_FOR_READ; |
| 753 |
/* This device only supports read so the stream gets started by the |
| 754 |
driver on the first read */ |
| 755 |
devices[index].first_frame = V4L2_IGNORE_FIRST_FRAME_ERRORS; |
| 756 |
} |
| 757 |
if ((parm.type == V4L2_BUF_TYPE_VIDEO_CAPTURE) && |
| 758 |
(parm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME)) |
| 759 |
devices[index].flags |= V4L2_SUPPORTS_TIMEPERFRAME; |
| 760 |
devices[index].open_count = 1; |
| 761 |
devices[index].page_size = page_size; |
| 762 |
devices[index].src_fmt = fmt; |
| 763 |
devices[index].dest_fmt = fmt; |
| 764 |
v4l2_set_src_and_dest_format(index, &devices[index].src_fmt, |
| 765 |
&devices[index].dest_fmt); |
| 766 |
|
| 767 |
pthread_mutex_init(&devices[index].stream_lock, NULL); |
| 768 |
|
| 769 |
devices[index].no_frames = 0; |
| 770 |
devices[index].nreadbuffers = V4L2_DEFAULT_NREADBUFFERS; |
| 771 |
devices[index].convert = convert; |
| 772 |
devices[index].convert_mmap_buf = MAP_FAILED; |
| 773 |
devices[index].convert_mmap_buf_size = 0; |
| 774 |
for (i = 0; i < V4L2_MAX_NO_FRAMES; i++) { |
| 775 |
devices[index].frame_pointers[i] = MAP_FAILED; |
| 776 |
devices[index].frame_map_count[i] = 0; |
| 777 |
} |
| 778 |
devices[index].frame_queued = 0; |
| 779 |
devices[index].readbuf = NULL; |
| 780 |
devices[index].readbuf_size = 0; |
| 781 |
|
| 782 |
if (index >= devices_used) |
| 783 |
devices_used = index + 1; |
| 784 |
|
| 785 |
/* Note we always tell v4lconvert to optimize src fmt selection for |
| 786 |
our default fps, the only exception is the app explicitly selecting |
| 787 |
a frame rate using the S_PARM ioctl after a S_FMT */ |
| 788 |
if (devices[index].convert) |
| 789 |
v4lconvert_set_fps(devices[index].convert, V4L2_DEFAULT_FPS); |
| 790 |
v4l2_update_fps(index, &parm); |
| 791 |
|
| 792 |
V4L2_LOG("open: %d\n", fd); |
| 793 |
|
| 794 |
return fd; |
| 795 |
} |
| 796 |
|
| 797 |
/* Is this an fd for which we are emulating v4l1 ? */ |
| 798 |
static int v4l2_get_index(int fd) |
| 799 |
{ |
| 800 |
int index; |
| 801 |
|
| 802 |
/* We never handle fd -1 */ |
| 803 |
if (fd == -1) |
| 804 |
return -1; |
| 805 |
|
| 806 |
for (index = 0; index < devices_used; index++) |
| 807 |
if (devices[index].fd == fd) |
| 808 |
break; |
| 809 |
|
| 810 |
if (index == devices_used) |
| 811 |
return -1; |
| 812 |
|
| 813 |
return index; |
| 814 |
} |
| 815 |
|
| 816 |
|
| 817 |
int v4l2_close(int fd) |
| 818 |
{ |
| 819 |
int index, result; |
| 820 |
|
| 821 |
index = v4l2_get_index(fd); |
| 822 |
if (index == -1) |
| 823 |
return SYS_CLOSE(fd); |
| 824 |
|
| 825 |
/* Abuse stream_lock to stop 2 closes from racing and trying to free |
| 826 |
the resources twice */ |
| 827 |
pthread_mutex_lock(&devices[index].stream_lock); |
| 828 |
devices[index].open_count--; |
| 829 |
result = devices[index].open_count != 0; |
| 830 |
pthread_mutex_unlock(&devices[index].stream_lock); |
| 831 |
|
| 832 |
if (result) |
| 833 |
return 0; |
| 834 |
|
| 835 |
v4l2_plugin_cleanup(devices[index].plugin_library, |
| 836 |
devices[index].dev_ops_priv, |
| 837 |
devices[index].dev_ops); |
| 838 |
|
| 839 |
/* Free resources */ |
| 840 |
v4l2_unmap_buffers(index); |
| 841 |
if (devices[index].convert_mmap_buf != MAP_FAILED) { |
| 842 |
if (v4l2_buffers_mapped(index)) { |
| 843 |
if (!devices[index].gone) |
| 844 |
V4L2_LOG_WARN("v4l2 mmap buffers still mapped on close()\n"); |
| 845 |
} else { |
| 846 |
SYS_MUNMAP(devices[index].convert_mmap_buf, |
| 847 |
devices[index].convert_mmap_buf_size); |
| 848 |
} |
| 849 |
devices[index].convert_mmap_buf = MAP_FAILED; |
| 850 |
devices[index].convert_mmap_buf_size = 0; |
| 851 |
} |
| 852 |
v4lconvert_destroy(devices[index].convert); |
| 853 |
free(devices[index].readbuf); |
| 854 |
devices[index].readbuf = NULL; |
| 855 |
devices[index].readbuf_size = 0; |
| 856 |
|
| 857 |
/* Remove the fd from our list of managed fds before closing it, because as |
| 858 |
soon as we've done the actual close, the fd maybe returned by an open() in |
| 859 |
another thread and we don't want to intercept calls to this new fd. */ |
| 860 |
devices[index].fd = -1; |
| 861 |
|
| 862 |
/* Since we've marked the fd as no longer used, and freed the resources, |
| 863 |
redo the close in case it was interrupted */ |
| 864 |
do { |
| 865 |
result = SYS_CLOSE(fd); |
| 866 |
} while (result == -1 && errno == EINTR); |
| 867 |
|
| 868 |
V4L2_LOG("close: %d\n", fd); |
| 869 |
|
| 870 |
return result; |
| 871 |
} |
| 872 |
|
| 873 |
int v4l2_dup(int fd) |
| 874 |
{ |
| 875 |
int index = v4l2_get_index(fd); |
| 876 |
|
| 877 |
if (index == -1) |
| 878 |
return syscall(SYS_dup, fd); |
| 879 |
|
| 880 |
devices[index].open_count++; |
| 881 |
|
| 882 |
return fd; |
| 883 |
} |
| 884 |
|
| 885 |
static int v4l2_check_buffer_change_ok(int index) |
| 886 |
{ |
| 887 |
devices[index].frame_info_generation++; |
| 888 |
v4l2_unmap_buffers(index); |
| 889 |
|
| 890 |
/* Check if the app itself still is using the stream */ |
| 891 |
if (v4l2_buffers_mapped(index) || |
| 892 |
(!(devices[index].flags & V4L2_STREAM_CONTROLLED_BY_READ) && |
| 893 |
((devices[index].flags & V4L2_STREAMON) || |
| 894 |
devices[index].frame_queued))) { |
| 895 |
V4L2_LOG("v4l2_check_buffer_change_ok(): stream busy\n"); |
| 896 |
errno = EBUSY; |
| 897 |
return -1; |
| 898 |
} |
| 899 |
|
| 900 |
/* We may change from convert to non conversion mode and |
| 901 |
v4l2_unrequest_read_buffers may change the no_frames, so free the |
| 902 |
convert mmap buffer */ |
| 903 |
SYS_MUNMAP(devices[index].convert_mmap_buf, |
| 904 |
devices[index].convert_mmap_buf_size); |
| 905 |
devices[index].convert_mmap_buf = MAP_FAILED; |
| 906 |
devices[index].convert_mmap_buf_size = 0; |
| 907 |
|
| 908 |
if (devices[index].flags & V4L2_STREAM_CONTROLLED_BY_READ) { |
| 909 |
V4L2_LOG("deactivating read-stream for settings change\n"); |
| 910 |
return v4l2_deactivate_read_stream(index); |
| 911 |
} |
| 912 |
|
| 913 |
return 0; |
| 914 |
} |
| 915 |
|
| 916 |
static int v4l2_pix_fmt_compat(struct v4l2_format *a, struct v4l2_format *b) |
| 917 |
{ |
| 918 |
if (a->fmt.pix.width == b->fmt.pix.width && |
| 919 |
a->fmt.pix.height == b->fmt.pix.height && |
| 920 |
a->fmt.pix.pixelformat == b->fmt.pix.pixelformat && |
| 921 |
a->fmt.pix.field == b->fmt.pix.field) |
| 922 |
return 1; |
| 923 |
|
| 924 |
return 0; |
| 925 |
} |
| 926 |
|
| 927 |
static int v4l2_pix_fmt_identical(struct v4l2_format *a, struct v4l2_format *b) |
| 928 |
{ |
| 929 |
if (v4l2_pix_fmt_compat(a, b) && |
| 930 |
a->fmt.pix.bytesperline == b->fmt.pix.bytesperline && |
| 931 |
a->fmt.pix.sizeimage == b->fmt.pix.sizeimage) |
| 932 |
return 1; |
| 933 |
|
| 934 |
return 0; |
| 935 |
} |
| 936 |
|
| 937 |
static void v4l2_set_src_and_dest_format(int index, |
| 938 |
struct v4l2_format *src_fmt, struct v4l2_format *dest_fmt) |
| 939 |
{ |
| 940 |
/* |
| 941 |
* When a user does a try_fmt with the current dest_fmt and the |
| 942 |
* dest_fmt is a supported one we will align the resolution (see |
| 943 |
* libv4lconvert_try_fmt). We do this here too, in case dest_fmt gets |
| 944 |
* set without having gone through libv4lconvert_try_fmt, so that a |
| 945 |
* try_fmt on the result of a get_fmt always returns the same result. |
| 946 |
*/ |
| 947 |
if (v4lconvert_supported_dst_format(dest_fmt->fmt.pix.pixelformat)) { |
| 948 |
dest_fmt->fmt.pix.width &= ~7; |
| 949 |
dest_fmt->fmt.pix.height &= ~1; |
| 950 |
} |
| 951 |
|
| 952 |
/* Sigh some drivers (pwc) do not properly reflect what one really gets |
| 953 |
after a s_fmt in their try_fmt answer. So update dest format (which we |
| 954 |
report as result from s_fmt / g_fmt to the app) with all info from the src |
| 955 |
format not changed by conversion */ |
| 956 |
dest_fmt->fmt.pix.field = src_fmt->fmt.pix.field; |
| 957 |
dest_fmt->fmt.pix.colorspace = src_fmt->fmt.pix.colorspace; |
| 958 |
dest_fmt->fmt.pix.xfer_func = src_fmt->fmt.pix.xfer_func; |
| 959 |
dest_fmt->fmt.pix.ycbcr_enc = src_fmt->fmt.pix.ycbcr_enc; |
| 960 |
dest_fmt->fmt.pix.quantization = src_fmt->fmt.pix.quantization; |
| 961 |
/* When we're not converting use bytesperline and imagesize from src_fmt */ |
| 962 |
if (v4l2_pix_fmt_compat(src_fmt, dest_fmt)) { |
| 963 |
dest_fmt->fmt.pix.bytesperline = src_fmt->fmt.pix.bytesperline; |
| 964 |
dest_fmt->fmt.pix.sizeimage = src_fmt->fmt.pix.sizeimage; |
| 965 |
} else |
| 966 |
v4lconvert_fixup_fmt(dest_fmt); |
| 967 |
|
| 968 |
devices[index].src_fmt = *src_fmt; |
| 969 |
devices[index].dest_fmt = *dest_fmt; |
| 970 |
/* round up to full page size */ |
| 971 |
devices[index].convert_mmap_frame_size = |
| 972 |
(((dest_fmt->fmt.pix.sizeimage + devices[index].page_size - 1) |
| 973 |
/ devices[index].page_size) * devices[index].page_size); |
| 974 |
} |
| 975 |
|
| 976 |
static int v4l2_s_fmt(int index, struct v4l2_format *dest_fmt) |
| 977 |
{ |
| 978 |
struct v4l2_format src_fmt; |
| 979 |
struct v4l2_pix_format req_pix_fmt; |
| 980 |
int result; |
| 981 |
|
| 982 |
if (v4l2_log_file) { |
| 983 |
int pixfmt = dest_fmt->fmt.pix.pixelformat; |
| 984 |
|
| 985 |
fprintf(v4l2_log_file, "VIDIOC_S_FMT app requesting: %c%c%c%c\n", |
| 986 |
pixfmt & 0xff, |
| 987 |
(pixfmt >> 8) & 0xff, |
| 988 |
(pixfmt >> 16) & 0xff, |
| 989 |
pixfmt >> 24); |
| 990 |
} |
| 991 |
|
| 992 |
result = v4lconvert_try_format(devices[index].convert, |
| 993 |
dest_fmt, &src_fmt); |
| 994 |
if (result) { |
| 995 |
int saved_err = errno; |
| 996 |
V4L2_LOG("S_FMT error trying format: %s\n", strerror(errno)); |
| 997 |
errno = saved_err; |
| 998 |
return result; |
| 999 |
} |
| 1000 |
|
| 1001 |
if (src_fmt.fmt.pix.pixelformat != dest_fmt->fmt.pix.pixelformat && |
| 1002 |
v4l2_log_file) { |
| 1003 |
int pixfmt = src_fmt.fmt.pix.pixelformat; |
| 1004 |
|
| 1005 |
fprintf(v4l2_log_file, |
| 1006 |
"VIDIOC_S_FMT converting from: %c%c%c%c\n", |
| 1007 |
pixfmt & 0xff, (pixfmt >> 8) & 0xff, |
| 1008 |
(pixfmt >> 16) & 0xff, pixfmt >> 24); |
| 1009 |
} |
| 1010 |
|
| 1011 |
result = v4l2_check_buffer_change_ok(index); |
| 1012 |
if (result) |
| 1013 |
return result; |
| 1014 |
|
| 1015 |
req_pix_fmt = src_fmt.fmt.pix; |
| 1016 |
result = devices[index].dev_ops->ioctl(devices[index].dev_ops_priv, |
| 1017 |
devices[index].fd, |
| 1018 |
VIDIOC_S_FMT, &src_fmt); |
| 1019 |
if (result) { |
| 1020 |
int saved_err = errno; |
| 1021 |
V4L2_PERROR("setting pixformat"); |
| 1022 |
/* Report to the app dest_fmt has not changed */ |
| 1023 |
*dest_fmt = devices[index].dest_fmt; |
| 1024 |
errno = saved_err; |
| 1025 |
return result; |
| 1026 |
} |
| 1027 |
|
| 1028 |
/* See if we've gotten what try_fmt promised us |
| 1029 |
(this check should never fail) */ |
| 1030 |
if (src_fmt.fmt.pix.width != req_pix_fmt.width || |
| 1031 |
src_fmt.fmt.pix.height != req_pix_fmt.height || |
| 1032 |
src_fmt.fmt.pix.pixelformat != req_pix_fmt.pixelformat) { |
| 1033 |
V4L2_LOG_ERR("set_fmt gave us a different result than try_fmt!\n"); |
| 1034 |
/* Not what we expected / wanted, disable conversion */ |
| 1035 |
*dest_fmt = src_fmt; |
| 1036 |
} |
| 1037 |
|
| 1038 |
v4l2_set_src_and_dest_format(index, &src_fmt, dest_fmt); |
| 1039 |
|
| 1040 |
if (devices[index].flags & V4L2_SUPPORTS_TIMEPERFRAME) { |
| 1041 |
struct v4l2_streamparm parm = { |
| 1042 |
.type = V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 1043 |
}; |
| 1044 |
if (devices[index].dev_ops->ioctl(devices[index].dev_ops_priv, |
| 1045 |
devices[index].fd, |
| 1046 |
VIDIOC_G_PARM, &parm)) |
| 1047 |
return 0; |
| 1048 |
v4l2_update_fps(index, &parm); |
| 1049 |
} |
| 1050 |
|
| 1051 |
return 0; |
| 1052 |
} |
| 1053 |
|
| 1054 |
#ifdef LIBV4L_HAVE_POSIX_IOCTL |
| 1055 |
int v4l2_ioctl(int fd, int request, ...) |
| 1056 |
#else |
| 1057 |
int v4l2_ioctl(int fd, unsigned long int request, ...) |
| 1058 |
#endif |
| 1059 |
{ |
| 1060 |
void *arg; |
| 1061 |
va_list ap; |
| 1062 |
int result, index, saved_err; |
| 1063 |
int is_capture_request = 0, stream_needs_locking = 0; |
| 1064 |
|
| 1065 |
va_start(ap, request); |
| 1066 |
arg = va_arg(ap, void *); |
| 1067 |
va_end(ap); |
| 1068 |
|
| 1069 |
index = v4l2_get_index(fd); |
| 1070 |
if (index == -1) |
| 1071 |
return SYS_IOCTL(fd, request, arg); |
| 1072 |
|
| 1073 |
/* Apparently the kernel and / or glibc ignore the 32 most significant bits |
| 1074 |
when long = 64 bits, and some applications pass an int holding the req to |
| 1075 |
ioctl, causing it to get sign extended, depending upon this behavior */ |
| 1076 |
request = (unsigned int)request; |
| 1077 |
|
| 1078 |
if (devices[index].convert == NULL) |
| 1079 |
goto no_capture_request; |
| 1080 |
|
| 1081 |
/* Is this a capture request and do we need to take the stream lock? */ |
| 1082 |
switch (request) { |
| 1083 |
case VIDIOC_QUERYCAP: |
| 1084 |
case VIDIOC_QUERYCTRL: |
| 1085 |
case VIDIOC_G_CTRL: |
| 1086 |
case VIDIOC_S_CTRL: |
| 1087 |
case VIDIOC_G_EXT_CTRLS: |
| 1088 |
case VIDIOC_TRY_EXT_CTRLS: |
| 1089 |
case VIDIOC_S_EXT_CTRLS: |
| 1090 |
case VIDIOC_ENUM_FRAMESIZES: |
| 1091 |
case VIDIOC_ENUM_FRAMEINTERVALS: |
| 1092 |
is_capture_request = 1; |
| 1093 |
break; |
| 1094 |
case VIDIOC_ENUM_FMT: |
| 1095 |
if (((struct v4l2_fmtdesc *)arg)->type == |
| 1096 |
V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1097 |
is_capture_request = 1; |
| 1098 |
break; |
| 1099 |
case VIDIOC_TRY_FMT: |
| 1100 |
if (((struct v4l2_format *)arg)->type == |
| 1101 |
V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1102 |
is_capture_request = 1; |
| 1103 |
break; |
| 1104 |
case VIDIOC_S_FMT: |
| 1105 |
case VIDIOC_G_FMT: |
| 1106 |
if (((struct v4l2_format *)arg)->type == |
| 1107 |
V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
| 1108 |
is_capture_request = 1; |
| 1109 |
stream_needs_locking = 1; |
| 1110 |
} |
| 1111 |
break; |
| 1112 |
case VIDIOC_REQBUFS: |
| 1113 |
if (((struct v4l2_requestbuffers *)arg)->type == |
| 1114 |
V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
| 1115 |
is_capture_request = 1; |
| 1116 |
stream_needs_locking = 1; |
| 1117 |
} |
| 1118 |
break; |
| 1119 |
case VIDIOC_QUERYBUF: |
| 1120 |
case VIDIOC_QBUF: |
| 1121 |
case VIDIOC_DQBUF: |
| 1122 |
if (((struct v4l2_buffer *)arg)->type == |
| 1123 |
V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
| 1124 |
is_capture_request = 1; |
| 1125 |
stream_needs_locking = 1; |
| 1126 |
} |
| 1127 |
break; |
| 1128 |
case VIDIOC_STREAMON: |
| 1129 |
case VIDIOC_STREAMOFF: |
| 1130 |
if (*((enum v4l2_buf_type *)arg) == |
| 1131 |
V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
| 1132 |
is_capture_request = 1; |
| 1133 |
stream_needs_locking = 1; |
| 1134 |
} |
| 1135 |
break; |
| 1136 |
case VIDIOC_S_PARM: |
| 1137 |
if (((struct v4l2_streamparm *)arg)->type == |
| 1138 |
V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
| 1139 |
is_capture_request = 1; |
| 1140 |
if (devices[index].flags & V4L2_SUPPORTS_TIMEPERFRAME) |
| 1141 |
stream_needs_locking = 1; |
| 1142 |
} |
| 1143 |
break; |
| 1144 |
case VIDIOC_S_STD: |
| 1145 |
case VIDIOC_S_INPUT: |
| 1146 |
case VIDIOC_S_DV_TIMINGS: |
| 1147 |
is_capture_request = 1; |
| 1148 |
stream_needs_locking = 1; |
| 1149 |
break; |
| 1150 |
} |
| 1151 |
|
| 1152 |
if (!is_capture_request) { |
| 1153 |
no_capture_request: |
| 1154 |
result = devices[index].dev_ops->ioctl( |
| 1155 |
devices[index].dev_ops_priv, |
| 1156 |
fd, request, arg); |
| 1157 |
saved_err = errno; |
| 1158 |
v4l2_log_ioctl(request, arg, result); |
| 1159 |
errno = saved_err; |
| 1160 |
return result; |
| 1161 |
} |
| 1162 |
|
| 1163 |
|
| 1164 |
if (stream_needs_locking) { |
| 1165 |
pthread_mutex_lock(&devices[index].stream_lock); |
| 1166 |
/* If this is the first stream-related ioctl, and we should only allow |
| 1167 |
libv4lconvert supported destination formats (so that it can do flipping, |
| 1168 |
processing, etc.) and the current destination format is not supported, |
| 1169 |
try setting the format to RGB24 (which is a supported dest. format). */ |
| 1170 |
if (!(devices[index].flags & V4L2_STREAM_TOUCHED) && |
| 1171 |
v4lconvert_supported_dst_fmt_only(devices[index].convert) && |
| 1172 |
!v4lconvert_supported_dst_format( |
| 1173 |
devices[index].dest_fmt.fmt.pix.pixelformat)) { |
| 1174 |
struct v4l2_format fmt = devices[index].dest_fmt; |
| 1175 |
|
| 1176 |
V4L2_LOG("Setting pixelformat to RGB24 (supported_dst_fmt_only)"); |
| 1177 |
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24; |
| 1178 |
v4l2_s_fmt(index, &fmt); |
| 1179 |
V4L2_LOG("Done setting pixelformat (supported_dst_fmt_only)"); |
| 1180 |
} |
| 1181 |
devices[index].flags |= V4L2_STREAM_TOUCHED; |
| 1182 |
} |
| 1183 |
|
| 1184 |
switch (request) { |
| 1185 |
case VIDIOC_QUERYCTRL: |
| 1186 |
result = v4lconvert_vidioc_queryctrl(devices[index].convert, arg); |
| 1187 |
break; |
| 1188 |
|
| 1189 |
case VIDIOC_G_CTRL: |
| 1190 |
result = v4lconvert_vidioc_g_ctrl(devices[index].convert, arg); |
| 1191 |
break; |
| 1192 |
|
| 1193 |
case VIDIOC_S_CTRL: |
| 1194 |
result = v4lconvert_vidioc_s_ctrl(devices[index].convert, arg); |
| 1195 |
break; |
| 1196 |
|
| 1197 |
case VIDIOC_G_EXT_CTRLS: |
| 1198 |
result = v4lconvert_vidioc_g_ext_ctrls(devices[index].convert, arg); |
| 1199 |
break; |
| 1200 |
|
| 1201 |
case VIDIOC_TRY_EXT_CTRLS: |
| 1202 |
result = v4lconvert_vidioc_try_ext_ctrls(devices[index].convert, arg); |
| 1203 |
break; |
| 1204 |
|
| 1205 |
case VIDIOC_S_EXT_CTRLS: |
| 1206 |
result = v4lconvert_vidioc_s_ext_ctrls(devices[index].convert, arg); |
| 1207 |
break; |
| 1208 |
|
| 1209 |
case VIDIOC_QUERYCAP: { |
| 1210 |
struct v4l2_capability *cap = arg; |
| 1211 |
|
| 1212 |
result = devices[index].dev_ops->ioctl( |
| 1213 |
devices[index].dev_ops_priv, |
| 1214 |
fd, VIDIOC_QUERYCAP, cap); |
| 1215 |
if (result == 0) { |
| 1216 |
/* We always support read() as we fake it using mmap mode */ |
| 1217 |
cap->capabilities |= V4L2_CAP_READWRITE; |
| 1218 |
cap->device_caps |= V4L2_CAP_READWRITE; |
| 1219 |
} |
| 1220 |
break; |
| 1221 |
} |
| 1222 |
|
| 1223 |
case VIDIOC_ENUM_FMT: |
| 1224 |
result = v4lconvert_enum_fmt(devices[index].convert, arg); |
| 1225 |
break; |
| 1226 |
|
| 1227 |
case VIDIOC_ENUM_FRAMESIZES: |
| 1228 |
result = v4lconvert_enum_framesizes(devices[index].convert, arg); |
| 1229 |
break; |
| 1230 |
|
| 1231 |
case VIDIOC_ENUM_FRAMEINTERVALS: |
| 1232 |
result = v4lconvert_enum_frameintervals(devices[index].convert, arg); |
| 1233 |
if (result) |
| 1234 |
V4L2_LOG("ENUM_FRAMEINTERVALS Error: %s", |
| 1235 |
v4lconvert_get_error_message(devices[index].convert)); |
| 1236 |
break; |
| 1237 |
|
| 1238 |
case VIDIOC_TRY_FMT: |
| 1239 |
result = v4lconvert_try_format(devices[index].convert, |
| 1240 |
arg, NULL); |
| 1241 |
break; |
| 1242 |
|
| 1243 |
case VIDIOC_S_FMT: |
| 1244 |
result = v4l2_s_fmt(index, arg); |
| 1245 |
break; |
| 1246 |
|
| 1247 |
case VIDIOC_G_FMT: { |
| 1248 |
struct v4l2_format *fmt = arg; |
| 1249 |
|
| 1250 |
*fmt = devices[index].dest_fmt; |
| 1251 |
result = 0; |
| 1252 |
break; |
| 1253 |
} |
| 1254 |
|
| 1255 |
case VIDIOC_S_STD: |
| 1256 |
case VIDIOC_S_INPUT: |
| 1257 |
case VIDIOC_S_DV_TIMINGS: { |
| 1258 |
struct v4l2_format src_fmt = { 0 }; |
| 1259 |
unsigned int orig_dest_pixelformat = |
| 1260 |
devices[index].dest_fmt.fmt.pix.pixelformat; |
| 1261 |
|
| 1262 |
result = devices[index].dev_ops->ioctl( |
| 1263 |
devices[index].dev_ops_priv, |
| 1264 |
fd, request, arg); |
| 1265 |
if (result) |
| 1266 |
break; |
| 1267 |
|
| 1268 |
/* These ioctls may have changed the device's fmt */ |
| 1269 |
src_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1270 |
result = devices[index].dev_ops->ioctl( |
| 1271 |
devices[index].dev_ops_priv, |
| 1272 |
fd, VIDIOC_G_FMT, &src_fmt); |
| 1273 |
if (result) { |
| 1274 |
V4L2_PERROR("getting pixformat after %s", |
| 1275 |
v4l2_ioctls[_IOC_NR(request)]); |
| 1276 |
result = 0; /* The original command did succeed */ |
| 1277 |
break; |
| 1278 |
} |
| 1279 |
|
| 1280 |
if (v4l2_pix_fmt_compat(&devices[index].src_fmt, &src_fmt)) { |
| 1281 |
v4l2_set_src_and_dest_format(index, &src_fmt, |
| 1282 |
&devices[index].dest_fmt); |
| 1283 |
break; |
| 1284 |
} |
| 1285 |
|
| 1286 |
/* The fmt has been changed, remember the new format ... */ |
| 1287 |
devices[index].src_fmt = src_fmt; |
| 1288 |
devices[index].dest_fmt = src_fmt; |
| 1289 |
v4l2_set_src_and_dest_format(index, &devices[index].src_fmt, |
| 1290 |
&devices[index].dest_fmt); |
| 1291 |
/* and try to restore the last set destination pixelformat. */ |
| 1292 |
src_fmt.fmt.pix.pixelformat = orig_dest_pixelformat; |
| 1293 |
result = v4l2_s_fmt(index, &src_fmt); |
| 1294 |
if (result) { |
| 1295 |
V4L2_LOG_WARN("restoring destination pixelformat after %s failed\n", |
| 1296 |
v4l2_ioctls[_IOC_NR(request)]); |
| 1297 |
result = 0; /* The original command did succeed */ |
| 1298 |
} |
| 1299 |
|
| 1300 |
break; |
| 1301 |
} |
| 1302 |
|
| 1303 |
case VIDIOC_REQBUFS: { |
| 1304 |
struct v4l2_requestbuffers *req = arg; |
| 1305 |
|
| 1306 |
/* IMPROVEME (maybe?) add support for userptr's? */ |
| 1307 |
if (req->memory != V4L2_MEMORY_MMAP) { |
| 1308 |
errno = EINVAL; |
| 1309 |
result = -1; |
| 1310 |
break; |
| 1311 |
} |
| 1312 |
|
| 1313 |
result = v4l2_check_buffer_change_ok(index); |
| 1314 |
if (result) |
| 1315 |
break; |
| 1316 |
|
| 1317 |
/* No more buffers than we can manage please */ |
| 1318 |
if (req->count > V4L2_MAX_NO_FRAMES) |
| 1319 |
req->count = V4L2_MAX_NO_FRAMES; |
| 1320 |
|
| 1321 |
result = devices[index].dev_ops->ioctl( |
| 1322 |
devices[index].dev_ops_priv, |
| 1323 |
fd, VIDIOC_REQBUFS, req); |
| 1324 |
if (result < 0) |
| 1325 |
break; |
| 1326 |
result = 0; /* some drivers return the number of buffers on success */ |
| 1327 |
|
| 1328 |
devices[index].no_frames = MIN(req->count, V4L2_MAX_NO_FRAMES); |
| 1329 |
devices[index].flags &= ~V4L2_BUFFERS_REQUESTED_BY_READ; |
| 1330 |
break; |
| 1331 |
} |
| 1332 |
|
| 1333 |
case VIDIOC_QUERYBUF: { |
| 1334 |
struct v4l2_buffer *buf = arg; |
| 1335 |
|
| 1336 |
if (devices[index].flags & V4L2_STREAM_CONTROLLED_BY_READ) { |
| 1337 |
result = v4l2_deactivate_read_stream(index); |
| 1338 |
if (result) |
| 1339 |
break; |
| 1340 |
} |
| 1341 |
|
| 1342 |
/* Do a real query even when converting to let the driver fill in |
| 1343 |
things like buf->field */ |
| 1344 |
result = devices[index].dev_ops->ioctl( |
| 1345 |
devices[index].dev_ops_priv, |
| 1346 |
fd, VIDIOC_QUERYBUF, buf); |
| 1347 |
|
| 1348 |
v4l2_set_conversion_buf_params(index, buf); |
| 1349 |
break; |
| 1350 |
} |
| 1351 |
|
| 1352 |
case VIDIOC_QBUF: { |
| 1353 |
struct v4l2_buffer *buf = arg; |
| 1354 |
|
| 1355 |
if (devices[index].flags & V4L2_STREAM_CONTROLLED_BY_READ) { |
| 1356 |
result = v4l2_deactivate_read_stream(index); |
| 1357 |
if (result) |
| 1358 |
break; |
| 1359 |
} |
| 1360 |
|
| 1361 |
/* With some drivers the buffers must be mapped before queuing */ |
| 1362 |
if (v4l2_needs_conversion(index)) { |
| 1363 |
result = v4l2_map_buffers(index); |
| 1364 |
if (result) |
| 1365 |
break; |
| 1366 |
} |
| 1367 |
|
| 1368 |
result = devices[index].dev_ops->ioctl( |
| 1369 |
devices[index].dev_ops_priv, |
| 1370 |
fd, VIDIOC_QBUF, arg); |
| 1371 |
|
| 1372 |
v4l2_set_conversion_buf_params(index, buf); |
| 1373 |
break; |
| 1374 |
} |
| 1375 |
|
| 1376 |
case VIDIOC_DQBUF: { |
| 1377 |
struct v4l2_buffer *buf = arg; |
| 1378 |
|
| 1379 |
if (devices[index].flags & V4L2_STREAM_CONTROLLED_BY_READ) { |
| 1380 |
result = v4l2_deactivate_read_stream(index); |
| 1381 |
if (result) |
| 1382 |
break; |
| 1383 |
} |
| 1384 |
|
| 1385 |
if (!v4l2_needs_conversion(index)) { |
| 1386 |
pthread_mutex_unlock(&devices[index].stream_lock); |
| 1387 |
result = devices[index].dev_ops->ioctl( |
| 1388 |
devices[index].dev_ops_priv, |
| 1389 |
fd, VIDIOC_DQBUF, buf); |
| 1390 |
pthread_mutex_lock(&devices[index].stream_lock); |
| 1391 |
if (result) { |
| 1392 |
saved_err = errno; |
| 1393 |
V4L2_PERROR("dequeuing buf"); |
| 1394 |
errno = saved_err; |
| 1395 |
} |
| 1396 |
break; |
| 1397 |
} |
| 1398 |
|
| 1399 |
/* An application can do a DQBUF before mmap-ing in the buffer, |
| 1400 |
but we need the buffer _now_ to write our converted data |
| 1401 |
to it! */ |
| 1402 |
result = v4l2_ensure_convert_mmap_buf(index); |
| 1403 |
if (result) |
| 1404 |
break; |
| 1405 |
|
| 1406 |
result = v4l2_dequeue_and_convert(index, buf, 0, |
| 1407 |
devices[index].convert_mmap_frame_size); |
| 1408 |
if (result >= 0) { |
| 1409 |
buf->bytesused = result; |
| 1410 |
result = 0; |
| 1411 |
} |
| 1412 |
|
| 1413 |
v4l2_set_conversion_buf_params(index, buf); |
| 1414 |
break; |
| 1415 |
} |
| 1416 |
|
| 1417 |
case VIDIOC_STREAMON: |
| 1418 |
case VIDIOC_STREAMOFF: |
| 1419 |
if (devices[index].flags & V4L2_STREAM_CONTROLLED_BY_READ) { |
| 1420 |
result = v4l2_deactivate_read_stream(index); |
| 1421 |
if (result) |
| 1422 |
break; |
| 1423 |
} |
| 1424 |
|
| 1425 |
if (request == VIDIOC_STREAMON) |
| 1426 |
result = v4l2_streamon(index); |
| 1427 |
else |
| 1428 |
result = v4l2_streamoff(index); |
| 1429 |
break; |
| 1430 |
|
| 1431 |
case VIDIOC_S_PARM: { |
| 1432 |
struct v4l2_streamparm *parm = arg; |
| 1433 |
|
| 1434 |
/* See if libv4lconvert wishes to use a different src_fmt |
| 1435 |
for the new frame rate and set that first */ |
| 1436 |
if ((devices[index].flags & V4L2_SUPPORTS_TIMEPERFRAME) && |
| 1437 |
parm->parm.capture.timeperframe.numerator != 0) { |
| 1438 |
int fps = parm->parm.capture.timeperframe.denominator; |
| 1439 |
fps += parm->parm.capture.timeperframe.numerator - 1; |
| 1440 |
fps /= parm->parm.capture.timeperframe.numerator; |
| 1441 |
v4l2_adjust_src_fmt_to_fps(index, fps); |
| 1442 |
} |
| 1443 |
|
| 1444 |
result = devices[index].dev_ops->ioctl( |
| 1445 |
devices[index].dev_ops_priv, |
| 1446 |
fd, VIDIOC_S_PARM, parm); |
| 1447 |
if (result) |
| 1448 |
break; |
| 1449 |
|
| 1450 |
v4l2_update_fps(index, parm); |
| 1451 |
break; |
| 1452 |
} |
| 1453 |
|
| 1454 |
default: |
| 1455 |
result = devices[index].dev_ops->ioctl( |
| 1456 |
devices[index].dev_ops_priv, |
| 1457 |
fd, request, arg); |
| 1458 |
break; |
| 1459 |
} |
| 1460 |
|
| 1461 |
if (stream_needs_locking) |
| 1462 |
pthread_mutex_unlock(&devices[index].stream_lock); |
| 1463 |
|
| 1464 |
saved_err = errno; |
| 1465 |
v4l2_log_ioctl(request, arg, result); |
| 1466 |
errno = saved_err; |
| 1467 |
|
| 1468 |
return result; |
| 1469 |
} |
| 1470 |
|
| 1471 |
static void v4l2_adjust_src_fmt_to_fps(int index, int fps) |
| 1472 |
{ |
| 1473 |
struct v4l2_pix_format req_pix_fmt; |
| 1474 |
struct v4l2_format src_fmt; |
| 1475 |
struct v4l2_format dest_fmt = devices[index].dest_fmt; |
| 1476 |
struct v4l2_format orig_src_fmt = devices[index].src_fmt; |
| 1477 |
struct v4l2_format orig_dest_fmt = devices[index].dest_fmt; |
| 1478 |
int r; |
| 1479 |
|
| 1480 |
if (fps == devices[index].fps) |
| 1481 |
return; |
| 1482 |
|
| 1483 |
if (v4l2_check_buffer_change_ok(index)) |
| 1484 |
return; |
| 1485 |
|
| 1486 |
v4lconvert_set_fps(devices[index].convert, fps); |
| 1487 |
r = v4lconvert_try_format(devices[index].convert, &dest_fmt, &src_fmt); |
| 1488 |
v4lconvert_set_fps(devices[index].convert, V4L2_DEFAULT_FPS); |
| 1489 |
if (r) |
| 1490 |
return; |
| 1491 |
|
| 1492 |
if (orig_src_fmt.fmt.pix.pixelformat == src_fmt.fmt.pix.pixelformat || |
| 1493 |
!v4l2_pix_fmt_compat(&orig_dest_fmt, &dest_fmt)) |
| 1494 |
return; |
| 1495 |
|
| 1496 |
req_pix_fmt = src_fmt.fmt.pix; |
| 1497 |
if (devices[index].dev_ops->ioctl(devices[index].dev_ops_priv, |
| 1498 |
devices[index].fd, VIDIOC_S_FMT, &src_fmt)) |
| 1499 |
return; |
| 1500 |
|
| 1501 |
v4l2_set_src_and_dest_format(index, &src_fmt, &dest_fmt); |
| 1502 |
|
| 1503 |
/* Check we've gotten what try_fmt promised us and that the |
| 1504 |
new dest fmt matches the original, if this is true we're done. */ |
| 1505 |
if (src_fmt.fmt.pix.width == req_pix_fmt.width && |
| 1506 |
src_fmt.fmt.pix.height == req_pix_fmt.height && |
| 1507 |
src_fmt.fmt.pix.pixelformat == req_pix_fmt.pixelformat && |
| 1508 |
v4l2_pix_fmt_identical(&orig_dest_fmt, &dest_fmt)) { |
| 1509 |
if (v4l2_log_file) { |
| 1510 |
int pixfmt = src_fmt.fmt.pix.pixelformat; |
| 1511 |
fprintf(v4l2_log_file, |
| 1512 |
"new src fmt for fps change: %c%c%c%c\n", |
| 1513 |
pixfmt & 0xff, (pixfmt >> 8) & 0xff, |
| 1514 |
(pixfmt >> 16) & 0xff, pixfmt >> 24); |
| 1515 |
} |
| 1516 |
return; |
| 1517 |
} |
| 1518 |
|
| 1519 |
/* Not identical!! */ |
| 1520 |
V4L2_LOG_WARN("dest fmt changed after adjusting src fmt for fps " |
| 1521 |
"change, restoring original src fmt"); |
| 1522 |
src_fmt = orig_src_fmt; |
| 1523 |
dest_fmt = orig_dest_fmt; |
| 1524 |
req_pix_fmt = src_fmt.fmt.pix; |
| 1525 |
if (devices[index].dev_ops->ioctl(devices[index].dev_ops_priv, |
| 1526 |
devices[index].fd, VIDIOC_S_FMT, &src_fmt)) { |
| 1527 |
V4L2_PERROR("restoring src fmt"); |
| 1528 |
return; |
| 1529 |
} |
| 1530 |
v4l2_set_src_and_dest_format(index, &src_fmt, &dest_fmt); |
| 1531 |
if (src_fmt.fmt.pix.width != req_pix_fmt.width || |
| 1532 |
src_fmt.fmt.pix.height != req_pix_fmt.height || |
| 1533 |
src_fmt.fmt.pix.pixelformat != req_pix_fmt.pixelformat || |
| 1534 |
!v4l2_pix_fmt_identical(&orig_dest_fmt, &dest_fmt)) |
| 1535 |
V4L2_LOG_ERR("dest fmt different after restoring src fmt"); |
| 1536 |
} |
| 1537 |
|
| 1538 |
ssize_t v4l2_read(int fd, void *dest, size_t n) |
| 1539 |
{ |
| 1540 |
ssize_t result; |
| 1541 |
int saved_errno; |
| 1542 |
int index = v4l2_get_index(fd); |
| 1543 |
|
| 1544 |
if (index == -1) |
| 1545 |
return SYS_READ(fd, dest, n); |
| 1546 |
|
| 1547 |
if (!devices[index].dev_ops->read) { |
| 1548 |
errno = EINVAL; |
| 1549 |
return -1; |
| 1550 |
} |
| 1551 |
|
| 1552 |
pthread_mutex_lock(&devices[index].stream_lock); |
| 1553 |
|
| 1554 |
/* When not converting and the device supports read(), let the kernel handle |
| 1555 |
it */ |
| 1556 |
if (devices[index].convert == NULL || |
| 1557 |
((devices[index].flags & V4L2_SUPPORTS_READ) && |
| 1558 |
!v4l2_needs_conversion(index))) { |
| 1559 |
result = devices[index].dev_ops->read( |
| 1560 |
devices[index].dev_ops_priv, |
| 1561 |
fd, dest, n); |
| 1562 |
goto leave; |
| 1563 |
} |
| 1564 |
|
| 1565 |
/* Since we need to do conversion try to use mmap (streaming) mode under |
| 1566 |
the hood as that safes a memcpy for each frame read. |
| 1567 |
|
| 1568 |
Note sometimes this will fail as some drivers (at least gspca) do not allow |
| 1569 |
switching from read mode to mmap mode and they assume read() mode if a |
| 1570 |
select or poll() is done before any buffers are requested. So using mmap |
| 1571 |
mode under the hood will fail if a select() or poll() is done before the |
| 1572 |
first emulated read() call. */ |
| 1573 |
if (!(devices[index].flags & V4L2_STREAM_CONTROLLED_BY_READ) && |
| 1574 |
!(devices[index].flags & V4L2_USE_READ_FOR_READ)) { |
| 1575 |
result = v4l2_activate_read_stream(index); |
| 1576 |
if (result) { |
| 1577 |
/* Activating mmap mode failed, use read() instead */ |
| 1578 |
devices[index].flags |= V4L2_USE_READ_FOR_READ; |
| 1579 |
/* The read call done by v4l2_read_and_convert will start the stream */ |
| 1580 |
devices[index].first_frame = V4L2_IGNORE_FIRST_FRAME_ERRORS; |
| 1581 |
} |
| 1582 |
} |
| 1583 |
|
| 1584 |
if (devices[index].flags & V4L2_USE_READ_FOR_READ) { |
| 1585 |
result = v4l2_read_and_convert(index, dest, n); |
| 1586 |
} else { |
| 1587 |
struct v4l2_buffer buf = {}; |
| 1588 |
|
| 1589 |
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1590 |
buf.memory = V4L2_MEMORY_MMAP; |
| 1591 |
result = v4l2_dequeue_and_convert(index, &buf, dest, n); |
| 1592 |
|
| 1593 |
if (result >= 0) |
| 1594 |
v4l2_queue_read_buffer(index, buf.index); |
| 1595 |
} |
| 1596 |
|
| 1597 |
leave: |
| 1598 |
saved_errno = errno; |
| 1599 |
pthread_mutex_unlock(&devices[index].stream_lock); |
| 1600 |
errno = saved_errno; |
| 1601 |
|
| 1602 |
return result; |
| 1603 |
} |
| 1604 |
|
| 1605 |
ssize_t v4l2_write(int fd, const void *buffer, size_t n) |
| 1606 |
{ |
| 1607 |
int index = v4l2_get_index(fd); |
| 1608 |
|
| 1609 |
if (index == -1) |
| 1610 |
return SYS_WRITE(fd, buffer, n); |
| 1611 |
|
| 1612 |
if (!devices[index].dev_ops->write) { |
| 1613 |
errno = EINVAL; |
| 1614 |
return -1; |
| 1615 |
} |
| 1616 |
|
| 1617 |
return devices[index].dev_ops->write( |
| 1618 |
devices[index].dev_ops_priv, fd, buffer, n); |
| 1619 |
} |
| 1620 |
|
| 1621 |
void *v4l2_mmap(void *start, size_t length, int prot, int flags, int fd, |
| 1622 |
int64_t offset) |
| 1623 |
{ |
| 1624 |
int index; |
| 1625 |
unsigned int buffer_index; |
| 1626 |
void *result; |
| 1627 |
|
| 1628 |
index = v4l2_get_index(fd); |
| 1629 |
if (index == -1 || |
| 1630 |
/* Check if the mmap data matches our answer to QUERY_BUF. If it doesn't, |
| 1631 |
let the kernel handle it (to allow for mmap-based non capture use) */ |
| 1632 |
start || length != devices[index].convert_mmap_frame_size || |
| 1633 |
((unsigned int)offset & ~0xFFu) != V4L2_MMAP_OFFSET_MAGIC) { |
| 1634 |
if (index != -1) |
| 1635 |
V4L2_LOG("Passing mmap(%p, %d, ..., %x, through to the driver\n", |
| 1636 |
start, (int)length, (int)offset); |
| 1637 |
|
| 1638 |
if (offset & ((1 << MMAP2_PAGE_SHIFT) - 1)) { |
| 1639 |
errno = EINVAL; |
| 1640 |
return MAP_FAILED; |
| 1641 |
} |
| 1642 |
|
| 1643 |
return (void *)SYS_MMAP(start, length, prot, flags, fd, offset); |
| 1644 |
} |
| 1645 |
|
| 1646 |
pthread_mutex_lock(&devices[index].stream_lock); |
| 1647 |
|
| 1648 |
buffer_index = offset & 0xff; |
| 1649 |
if (buffer_index >= devices[index].no_frames || |
| 1650 |
/* Got magic offset and not converting ?? */ |
| 1651 |
!v4l2_needs_conversion(index)) { |
| 1652 |
errno = EINVAL; |
| 1653 |
result = MAP_FAILED; |
| 1654 |
goto leave; |
| 1655 |
} |
| 1656 |
|
| 1657 |
if (v4l2_ensure_convert_mmap_buf(index)) { |
| 1658 |
errno = EINVAL; |
| 1659 |
result = MAP_FAILED; |
| 1660 |
goto leave; |
| 1661 |
} |
| 1662 |
|
| 1663 |
devices[index].frame_map_count[buffer_index]++; |
| 1664 |
|
| 1665 |
result = devices[index].convert_mmap_buf + |
| 1666 |
buffer_index * devices[index].convert_mmap_frame_size; |
| 1667 |
|
| 1668 |
V4L2_LOG("Fake (conversion) mmap buf %u, seen by app at: %p\n", |
| 1669 |
buffer_index, result); |
| 1670 |
|
| 1671 |
leave: |
| 1672 |
pthread_mutex_unlock(&devices[index].stream_lock); |
| 1673 |
|
| 1674 |
return result; |
| 1675 |
} |
| 1676 |
|
| 1677 |
int v4l2_munmap(void *_start, size_t length) |
| 1678 |
{ |
| 1679 |
int index; |
| 1680 |
unsigned int buffer_index; |
| 1681 |
unsigned char *start = _start; |
| 1682 |
|
| 1683 |
/* Is this memory ours? */ |
| 1684 |
if (start != MAP_FAILED) { |
| 1685 |
for (index = 0; index < devices_used; index++) |
| 1686 |
if (devices[index].fd != -1 && |
| 1687 |
devices[index].convert_mmap_buf != MAP_FAILED && |
| 1688 |
length == devices[index].convert_mmap_frame_size && |
| 1689 |
start >= devices[index].convert_mmap_buf && |
| 1690 |
(start - devices[index].convert_mmap_buf) % length == 0) |
| 1691 |
break; |
| 1692 |
|
| 1693 |
if (index != devices_used) { |
| 1694 |
int unmapped = 0; |
| 1695 |
|
| 1696 |
pthread_mutex_lock(&devices[index].stream_lock); |
| 1697 |
|
| 1698 |
buffer_index = (start - devices[index].convert_mmap_buf) / length; |
| 1699 |
|
| 1700 |
/* Re-do our checks now that we have the lock, things may have changed */ |
| 1701 |
if (devices[index].convert_mmap_buf != MAP_FAILED && |
| 1702 |
length == devices[index].convert_mmap_frame_size && |
| 1703 |
start >= devices[index].convert_mmap_buf && |
| 1704 |
(start - devices[index].convert_mmap_buf) % length == 0 && |
| 1705 |
buffer_index < devices[index].no_frames) { |
| 1706 |
if (devices[index].frame_map_count[buffer_index] > 0) |
| 1707 |
devices[index].frame_map_count[buffer_index]--; |
| 1708 |
unmapped = 1; |
| 1709 |
} |
| 1710 |
|
| 1711 |
pthread_mutex_unlock(&devices[index].stream_lock); |
| 1712 |
|
| 1713 |
if (unmapped) { |
| 1714 |
V4L2_LOG("v4l2 fake buffer munmap %p, %d\n", start, (int)length); |
| 1715 |
return 0; |
| 1716 |
} |
| 1717 |
} |
| 1718 |
} |
| 1719 |
|
| 1720 |
V4L2_LOG("v4l2 unknown munmap %p, %d\n", start, (int)length); |
| 1721 |
|
| 1722 |
return SYS_MUNMAP(_start, length); |
| 1723 |
} |
| 1724 |
|
| 1725 |
/* Misc utility functions */ |
| 1726 |
int v4l2_set_control(int fd, int cid, int value) |
| 1727 |
{ |
| 1728 |
struct v4l2_queryctrl qctrl = { .id = cid }; |
| 1729 |
struct v4l2_control ctrl = { .id = cid }; |
| 1730 |
int index, result; |
| 1731 |
|
| 1732 |
index = v4l2_get_index(fd); |
| 1733 |
if (index == -1 || devices[index].convert == NULL) { |
| 1734 |
V4L2_LOG_ERR("v4l2_set_control called with invalid fd: %d\n", fd); |
| 1735 |
errno = EBADF; |
| 1736 |
return -1; |
| 1737 |
} |
| 1738 |
|
| 1739 |
result = v4lconvert_vidioc_queryctrl(devices[index].convert, &qctrl); |
| 1740 |
if (result) |
| 1741 |
return result; |
| 1742 |
|
| 1743 |
if (!(qctrl.flags & V4L2_CTRL_FLAG_DISABLED) && |
| 1744 |
!(qctrl.flags & V4L2_CTRL_FLAG_GRABBED)) { |
| 1745 |
if (qctrl.type == V4L2_CTRL_TYPE_BOOLEAN) |
| 1746 |
ctrl.value = value ? 1 : 0; |
| 1747 |
else |
| 1748 |
ctrl.value = ((long long) value * (qctrl.maximum - qctrl.minimum) + 32767) / 65535 + |
| 1749 |
qctrl.minimum; |
| 1750 |
|
| 1751 |
result = v4lconvert_vidioc_s_ctrl(devices[index].convert, &ctrl); |
| 1752 |
} |
| 1753 |
|
| 1754 |
return result; |
| 1755 |
} |
| 1756 |
|
| 1757 |
int v4l2_get_control(int fd, int cid) |
| 1758 |
{ |
| 1759 |
struct v4l2_queryctrl qctrl = { .id = cid }; |
| 1760 |
struct v4l2_control ctrl = { .id = cid }; |
| 1761 |
int index = v4l2_get_index(fd); |
| 1762 |
|
| 1763 |
if (index == -1 || devices[index].convert == NULL) { |
| 1764 |
V4L2_LOG_ERR("v4l2_set_control called with invalid fd: %d\n", fd); |
| 1765 |
errno = EBADF; |
| 1766 |
return -1; |
| 1767 |
} |
| 1768 |
|
| 1769 |
if (v4lconvert_vidioc_queryctrl(devices[index].convert, &qctrl)) |
| 1770 |
return -1; |
| 1771 |
|
| 1772 |
if (qctrl.flags & V4L2_CTRL_FLAG_DISABLED) { |
| 1773 |
errno = EINVAL; |
| 1774 |
return -1; |
| 1775 |
} |
| 1776 |
|
| 1777 |
if (v4lconvert_vidioc_g_ctrl(devices[index].convert, &ctrl)) |
| 1778 |
return -1; |
| 1779 |
|
| 1780 |
return (((long long) ctrl.value - qctrl.minimum) * 65535 + |
| 1781 |
(qctrl.maximum - qctrl.minimum) / 2) / |
| 1782 |
(qctrl.maximum - qctrl.minimum); |
| 1783 |
} |