📄 libv4l1.c
字号:
/*# libv4l1 userspace v4l1 api emulation for v4l2 devices# (C) 2008 Hans de Goede <j.w.r.degoede@hhs.nl># This program is free software; you can redistribute it and/or modify# it under the terms of the GNU Lesser General Public License as published by# the Free Software Foundation; either version 2.1 of the License, or# (at your option) any later version.## 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 Lesser General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*//* MAKING CHANGES TO THIS FILE?? READ THIS FIRST!!! Important note to people making changes to this file: All functions (v4l1_close, v4l1_ioctl, etc.) are designed to function as their regular counterpart when they get passed a fd that is not "registered" by libv4l1, there are 2 reasons for this: 1) This allows us to get completely out of the way when dealing with non capture only devices, or non v4l2 devices. 2) libv4l1 is the base of the v4l1compat.so wrapper lib, which is a .so which can be LD_PRELOAD-ed and the overrules the libc's open/close/etc, and when opening /dev/videoX or /dev/v4l/ calls v4l1_open. Because we behave as the regular counterpart when the fd is not known (instead of say throwing an error), v4l1compat.so can simply call the v4l1_ prefixed function for all wrapped functions. This way the wrapper does not have to keep track of which fd's are being handled by libv4l1, as libv4l1 already keeps track of this itself. This also means that libv4l1 may not use any of the regular functions it mimics, as for example open could be a symbol in v4l1compat.so, which in turn will call v4l1_open, so therefor v4l1_open (for example) may not use the regular open()!*/#include <errno.h>#include <stdarg.h>#include <stdio.h>#include <stdlib.h>#include <syscall.h>#include <fcntl.h>#include <string.h>#include <unistd.h>#include <sys/types.h>#include <sys/mman.h>/* These headers are not needed by us, but by linux/videodev2.h, which is broken on some systems and doesn't include them itself :( */#include <sys/time.h>#include <asm/types.h>#include <linux/ioctl.h>/* end broken header workaround includes */#include <linux/videodev.h>#include <linux/videodev2.h>#include <libv4l2.h>#include "libv4l1.h"#include "libv4l1-priv.h"#define V4L1_SUPPORTS_ENUMINPUT 0x01#define V4L1_SUPPORTS_ENUMSTD 0x02#define V4L1_PIX_FMT_TOUCHED 0x04#define V4L1_PIX_SIZE_TOUCHED 0x08static pthread_mutex_t v4l1_open_mutex = PTHREAD_MUTEX_INITIALIZER;static struct v4l1_dev_info devices[V4L1_MAX_DEVICES] = { { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }, { .fd = -1 }};static int devices_used = 0;static unsigned int palette_to_pixelformat(unsigned int palette){ switch (palette) { case VIDEO_PALETTE_GREY: return V4L2_PIX_FMT_GREY; case VIDEO_PALETTE_RGB555: return V4L2_PIX_FMT_RGB555; case VIDEO_PALETTE_RGB565: return V4L2_PIX_FMT_RGB565; case VIDEO_PALETTE_RGB24: return V4L2_PIX_FMT_BGR24; case VIDEO_PALETTE_RGB32: return V4L2_PIX_FMT_BGR32; case VIDEO_PALETTE_YUYV: return V4L2_PIX_FMT_YUYV; case VIDEO_PALETTE_YUV422: return V4L2_PIX_FMT_YUYV; case VIDEO_PALETTE_UYVY: return V4L2_PIX_FMT_UYVY; case VIDEO_PALETTE_YUV410P: return V4L2_PIX_FMT_YUV410; case VIDEO_PALETTE_YUV420: case VIDEO_PALETTE_YUV420P: return V4L2_PIX_FMT_YUV420; case VIDEO_PALETTE_YUV411P: return V4L2_PIX_FMT_YUV411P; case VIDEO_PALETTE_YUV422P: return V4L2_PIX_FMT_YUV422P; } return 0;}static unsigned int pixelformat_to_palette(unsigned int pixelformat){ switch (pixelformat) { case V4L2_PIX_FMT_GREY: return VIDEO_PALETTE_GREY; case V4L2_PIX_FMT_RGB555: return VIDEO_PALETTE_RGB555; case V4L2_PIX_FMT_RGB565: return VIDEO_PALETTE_RGB565; case V4L2_PIX_FMT_BGR24: return VIDEO_PALETTE_RGB24; case V4L2_PIX_FMT_BGR32: return VIDEO_PALETTE_RGB32; case V4L2_PIX_FMT_YUYV: return VIDEO_PALETTE_YUYV; case V4L2_PIX_FMT_UYVY: return VIDEO_PALETTE_UYVY; case V4L2_PIX_FMT_YUV410: case V4L2_PIX_FMT_YUV420: return VIDEO_PALETTE_YUV420P; case V4L2_PIX_FMT_YUV411P: return VIDEO_PALETTE_YUV411P; case V4L2_PIX_FMT_YUV422P: return VIDEO_PALETTE_YUV422P; } return 0;}static int v4l1_set_format(int index, unsigned int width, unsigned int height, int v4l1_pal, int width_height_may_differ){ int result; unsigned int v4l2_pixfmt; struct v4l2_format fmt2 = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE }; if (v4l1_pal != -1) { v4l2_pixfmt = palette_to_pixelformat(v4l1_pal); if (!v4l2_pixfmt) { V4L1_LOG("Unknown v4l1 palette number: %d\n", v4l1_pal); errno = EINVAL; return -1; } } else { v4l2_pixfmt = devices[index].v4l2_pixfmt; v4l1_pal = devices[index].v4l1_pal; } /* Do we need to change the resolution / format ? */ if (width == devices[index].width && height == devices[index].height && v4l2_pixfmt == devices[index].v4l2_pixfmt) return 0; /* Get current settings, apply our changes and try the new setting */ if ((result = v4l2_ioctl(devices[index].fd, VIDIOC_G_FMT, &fmt2))) { int saved_err = errno; V4L1_LOG_ERR("getting pixformat: %s\n", strerror(errno)); errno = saved_err; return result; } fmt2.fmt.pix.pixelformat = v4l2_pixfmt; fmt2.fmt.pix.width = width; fmt2.fmt.pix.height = height; if ((result = v4l2_ioctl(devices[index].fd, VIDIOC_TRY_FMT, &fmt2))) { int saved_err = errno; V4L1_LOG("error trying pixformat: %s\n", strerror(errno)); errno = saved_err; return result; } /* Check if we get what we asked for */ if (fmt2.fmt.pix.pixelformat != v4l2_pixfmt || (!width_height_may_differ && (fmt2.fmt.pix.width != width || fmt2.fmt.pix.height != height))) { V4L1_LOG("requested fmt, width, height combo not available\n"); errno = EINVAL; return -1; } /* Maybe after the TRY_FMT things haven't changed after all ? */ if (fmt2.fmt.pix.width == devices[index].width && fmt2.fmt.pix.height == devices[index].height && fmt2.fmt.pix.pixelformat == devices[index].v4l2_pixfmt) { devices[index].v4l1_pal = v4l1_pal; return 0; } if ((result = v4l2_ioctl(devices[index].fd, VIDIOC_S_FMT, &fmt2))) { int saved_err = errno; V4L1_LOG_ERR("setting pixformat: %s\n", strerror(errno)); errno = saved_err; return result; } devices[index].width = fmt2.fmt.pix.width; devices[index].height = fmt2.fmt.pix.height; devices[index].v4l2_pixfmt = v4l2_pixfmt; devices[index].v4l1_pal = v4l1_pal; devices[index].depth = ((fmt2.fmt.pix.bytesperline << 3) + (fmt2.fmt.pix.width - 1)) / fmt2.fmt.pix.width; return result;}static void v4l1_find_min_and_max_size(int index, struct v4l2_format *fmt2){ int i; struct v4l2_fmtdesc fmtdesc2 = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE }; devices[index].min_width = -1; devices[index].min_height = -1; devices[index].max_width = 0; devices[index].max_height = 0; for (i = 0; ; i++) { fmtdesc2.index = i; if (syscall(SYS_ioctl, devices[index].fd, VIDIOC_ENUM_FMT, &fmtdesc2)) break; fmt2->fmt.pix.pixelformat = fmtdesc2.pixelformat; fmt2->fmt.pix.width = 48; fmt2->fmt.pix.height = 32; if (syscall(SYS_ioctl, devices[index].fd, VIDIOC_TRY_FMT, fmt2) == 0) { if (fmt2->fmt.pix.width < devices[index].min_width) devices[index].min_width = fmt2->fmt.pix.width; if (fmt2->fmt.pix.height < devices[index].min_height) devices[index].min_height = fmt2->fmt.pix.height; } fmt2->fmt.pix.pixelformat = fmtdesc2.pixelformat; fmt2->fmt.pix.width = 100000; fmt2->fmt.pix.height = 100000; if (syscall(SYS_ioctl, devices[index].fd, VIDIOC_TRY_FMT, fmt2) == 0) { if (fmt2->fmt.pix.width > devices[index].max_width) devices[index].max_width = fmt2->fmt.pix.width; if (fmt2->fmt.pix.height > devices[index].max_height) devices[index].max_height = fmt2->fmt.pix.height; } }}int v4l1_open (const char *file, int oflag, ...){ int index, fd; char *lfname; struct v4l2_capability cap2; struct v4l2_format fmt2; struct v4l2_input input2; struct v4l2_standard standard2; int v4l_device = 0; /* check if we're opening a video4linux2 device */ if (!strncmp(file, "/dev/video", 10) || !strncmp(file, "/dev/v4l/", 9)) { /* Some apps open the device read only, but we need rw rights as the buffers *MUST* be mapped rw */ oflag = (oflag & ~O_ACCMODE) | O_RDWR; v4l_device = 1; } /* original open code */ if (oflag & O_CREAT) { va_list ap; mode_t mode; va_start (ap, oflag); mode = va_arg (ap, mode_t); fd = syscall(SYS_open, file, oflag, mode); va_end(ap); } else fd = syscall(SYS_open, file, oflag); /* end of original open code */ if (fd == -1 || !v4l_device) return fd; /* check that this is an v4l2 device, no need to emulate v4l1 on a v4l1 device */ if (syscall(SYS_ioctl, fd, VIDIOC_QUERYCAP, &cap2)) return fd; /* IMPROVEME */ /* we only support simple video capture devices which do not do overlay */ if ((cap2.capabilities & 0x0F) != V4L2_CAP_VIDEO_CAPTURE) return fd; /* If no log file was set by the app, see if one was specified through the environment */ if (!v4l1_log_file && (lfname = getenv("LIBV4L1_LOG_FILENAME"))) v4l1_log_file = fopen(lfname, "w"); /* redirect libv4l2 log messages to our logfile if no libv4l2 logfile is specified */ if (!v4l2_log_file) v4l2_log_file = v4l1_log_file; /* Get initial width, height and pixelformat */ fmt2.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if (syscall(SYS_ioctl, fd, VIDIOC_G_FMT, &fmt2)) { int saved_err = errno; V4L1_LOG_ERR("getting pixformat: %s\n", strerror(errno)); syscall(SYS_close, fd); errno = saved_err; return -1; } /* Register with libv4l2, as we use that todo format conversion and read() emulation for us */ if (v4l2_fd_open(fd, V4L2_ENABLE_ENUM_FMT_EMULATION) == -1) { int saved_err = errno; syscall(SYS_close, fd); errno = saved_err; return -1; } /* So we have a device on which we can (and want to) emulate v4l1, register it in our devices array */ pthread_mutex_lock(&v4l1_open_mutex); for (index = 0; index < V4L1_MAX_DEVICES; index++) if(devices[index].fd == -1) { devices[index].fd = fd; break; } pthread_mutex_unlock(&v4l1_open_mutex); if (index == V4L1_MAX_DEVICES) { V4L1_LOG_ERR("attempting to open more then %d video devices\n", V4L1_MAX_DEVICES); v4l2_close(fd); errno = EBUSY; return -1; } if (index >= devices_used) devices_used = index + 1; devices[index].flags = 0; devices[index].open_count = 1; devices[index].v4l1_frame_buf_map_count = 0; devices[index].v4l1_frame_pointer = MAP_FAILED; devices[index].width = fmt2.fmt.pix.width; devices[index].height = fmt2.fmt.pix.height; devices[index].v4l2_pixfmt = fmt2.fmt.pix.pixelformat; devices[index].v4l1_pal = pixelformat_to_palette(fmt2.fmt.pix.pixelformat); devices[index].depth = ((fmt2.fmt.pix.bytesperline << 3) + (fmt2.fmt.pix.width - 1)) / fmt2.fmt.pix.width; v4l1_find_min_and_max_size(index, &fmt2); /* Check ENUM_INPUT and ENUM_STD support */ input2.index = 0; if (v4l2_ioctl(fd, VIDIOC_ENUMINPUT, &input2) == 0) devices[index].flags |= V4L1_SUPPORTS_ENUMINPUT; standard2.index = 0; if (v4l2_ioctl(fd, VIDIOC_ENUMSTD, &input2) == 0) devices[index].flags |= V4L1_SUPPORTS_ENUMSTD; V4L1_LOG("open: %d\n", fd); return fd;}/* Is this an fd for which we are emulating v4l1 ? */static int v4l1_get_index(int fd){ int index; /* We never handle fd -1 */ if (fd == -1) return -1; for (index = 0; index < devices_used; index++) if (devices[index].fd == fd) break; if (index == devices_used) return -1; return index;}int v4l1_close(int fd) { int index, result; if ((index = v4l1_get_index(fd)) == -1) return syscall(SYS_close, fd); /* Abuse stream_lock to stop 2 closes from racing and trying to free the resources twice */ pthread_mutex_lock(&devices[index].stream_lock); devices[index].open_count--; result = devices[index].open_count != 0; pthread_mutex_unlock(&devices[index].stream_lock); if (result) return v4l2_close(fd); /* Free resources */ if (devices[index].v4l1_frame_pointer != MAP_FAILED) { if (devices[index].v4l1_frame_buf_map_count) V4L1_LOG("v4l1 capture buffer still mapped: %d times on close()\n", devices[index].v4l1_frame_buf_map_count); else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -