18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later 28c2ecf20Sopenharmony_ci.. c:namespace:: V4L 38c2ecf20Sopenharmony_ci 48c2ecf20Sopenharmony_ci.. _libv4l-introduction: 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci************ 78c2ecf20Sopenharmony_ciIntroduction 88c2ecf20Sopenharmony_ci************ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_cilibv4l is a collection of libraries which adds a thin abstraction layer 118c2ecf20Sopenharmony_cion top of video4linux2 devices. The purpose of this (thin) layer is to 128c2ecf20Sopenharmony_cimake it easy for application writers to support a wide variety of 138c2ecf20Sopenharmony_cidevices without having to write separate code for different devices in 148c2ecf20Sopenharmony_cithe same class. 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ciAn example of using libv4l is provided by 178c2ecf20Sopenharmony_ci:ref:`v4l2grab <v4l2grab-example>`. 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_cilibv4l consists of 3 different libraries: 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cilibv4lconvert 228c2ecf20Sopenharmony_ci============= 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_cilibv4lconvert is a library that converts several different pixelformats 258c2ecf20Sopenharmony_cifound in V4L2 drivers into a few common RGB and YUY formats. 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ciIt currently accepts the following V4L2 driver formats: 288c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_BGR24 <V4L2-PIX-FMT-BGR24>`, 298c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_HM12 <V4L2-PIX-FMT-HM12>`, 308c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_JPEG <V4L2-PIX-FMT-JPEG>`, 318c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_MJPEG <V4L2-PIX-FMT-MJPEG>`, 328c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_MR97310A <V4L2-PIX-FMT-MR97310A>`, 338c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_OV511 <V4L2-PIX-FMT-OV511>`, 348c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_OV518 <V4L2-PIX-FMT-OV518>`, 358c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_PAC207 <V4L2-PIX-FMT-PAC207>`, 368c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_PJPG <V4L2-PIX-FMT-PJPG>`, 378c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_RGB24 <V4L2-PIX-FMT-RGB24>`, 388c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_SBGGR8 <V4L2-PIX-FMT-SBGGR8>`, 398c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_SGBRG8 <V4L2-PIX-FMT-SGBRG8>`, 408c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_SGRBG8 <V4L2-PIX-FMT-SGRBG8>`, 418c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_SN9C10X <V4L2-PIX-FMT-SN9C10X>`, 428c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_SN9C20X_I420 <V4L2-PIX-FMT-SN9C20X-I420>`, 438c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_SPCA501 <V4L2-PIX-FMT-SPCA501>`, 448c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_SPCA505 <V4L2-PIX-FMT-SPCA505>`, 458c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_SPCA508 <V4L2-PIX-FMT-SPCA508>`, 468c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_SPCA561 <V4L2-PIX-FMT-SPCA561>`, 478c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_SQ905C <V4L2-PIX-FMT-SQ905C>`, 488c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_SRGGB8 <V4L2-PIX-FMT-SRGGB8>`, 498c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_UYVY <V4L2-PIX-FMT-UYVY>`, 508c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_YUV420 <V4L2-PIX-FMT-YUV420>`, 518c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_YUYV <V4L2-PIX-FMT-YUYV>`, 528c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_YVU420 <V4L2-PIX-FMT-YVU420>`, and 538c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_YVYU <V4L2-PIX-FMT-YVYU>`. 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ciLater on libv4lconvert was expanded to also be able to do various video 568c2ecf20Sopenharmony_ciprocessing functions to improve webcam video quality. The video 578c2ecf20Sopenharmony_ciprocessing is split in to 2 parts: libv4lconvert/control and 588c2ecf20Sopenharmony_cilibv4lconvert/processing. 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ciThe control part is used to offer video controls which can be used to 618c2ecf20Sopenharmony_cicontrol the video processing functions made available by 628c2ecf20Sopenharmony_cilibv4lconvert/processing. These controls are stored application wide 638c2ecf20Sopenharmony_ci(until reboot) by using a persistent shared memory object. 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_cilibv4lconvert/processing offers the actual video processing 668c2ecf20Sopenharmony_cifunctionality. 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_cilibv4l1 698c2ecf20Sopenharmony_ci======= 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ciThis library offers functions that can be used to quickly make v4l1 728c2ecf20Sopenharmony_ciapplications work with v4l2 devices. These functions work exactly like 738c2ecf20Sopenharmony_cithe normal open/close/etc, except that libv4l1 does full emulation of 748c2ecf20Sopenharmony_cithe v4l1 api on top of v4l2 drivers, in case of v4l1 drivers it will 758c2ecf20Sopenharmony_cijust pass calls through. 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ciSince those functions are emulations of the old V4L1 API, it shouldn't 788c2ecf20Sopenharmony_cibe used for new applications. 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_cilibv4l2 818c2ecf20Sopenharmony_ci======= 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ciThis library should be used for all modern V4L2 applications. 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ciIt provides handles to call V4L2 open/ioctl/close/poll methods. Instead 868c2ecf20Sopenharmony_ciof just providing the raw output of the device, it enhances the calls in 878c2ecf20Sopenharmony_cithe sense that it will use libv4lconvert to provide more video formats 888c2ecf20Sopenharmony_ciand to enhance the image quality. 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ciIn most cases, libv4l2 just passes the calls directly through to the 918c2ecf20Sopenharmony_civ4l2 driver, intercepting the calls to 928c2ecf20Sopenharmony_ci:ref:`VIDIOC_TRY_FMT <VIDIOC_G_FMT>`, 938c2ecf20Sopenharmony_ci:ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>`, 948c2ecf20Sopenharmony_ci:ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>`, 958c2ecf20Sopenharmony_ci:ref:`VIDIOC_ENUM_FRAMESIZES <VIDIOC_ENUM_FRAMESIZES>` and 968c2ecf20Sopenharmony_ci:ref:`VIDIOC_ENUM_FRAMEINTERVALS <VIDIOC_ENUM_FRAMEINTERVALS>` in 978c2ecf20Sopenharmony_ciorder to emulate the formats 988c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_BGR24 <V4L2-PIX-FMT-BGR24>`, 998c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_RGB24 <V4L2-PIX-FMT-RGB24>`, 1008c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_YUV420 <V4L2-PIX-FMT-YUV420>`, and 1018c2ecf20Sopenharmony_ci:ref:`V4L2_PIX_FMT_YVU420 <V4L2-PIX-FMT-YVU420>`, if they aren't 1028c2ecf20Sopenharmony_ciavailable in the driver. :ref:`VIDIOC_ENUM_FMT <VIDIOC_ENUM_FMT>` 1038c2ecf20Sopenharmony_cikeeps enumerating the hardware supported formats, plus the emulated 1048c2ecf20Sopenharmony_ciformats offered by libv4l at the end. 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci.. _libv4l-ops: 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ciLibv4l device control functions 1098c2ecf20Sopenharmony_ci------------------------------- 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ciThe common file operation methods are provided by libv4l. 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ciThose functions operate just like the gcc function ``dup()`` and 1148c2ecf20Sopenharmony_ciV4L2 functions 1158c2ecf20Sopenharmony_ci:c:func:`open()`, :c:func:`close()`, 1168c2ecf20Sopenharmony_ci:c:func:`ioctl()`, :c:func:`read()`, 1178c2ecf20Sopenharmony_ci:c:func:`mmap()` and :c:func:`munmap()`: 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci.. c:function:: int v4l2_open(const char *file, int oflag, ...) 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci operates like the :c:func:`open()` function. 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci.. c:function:: int v4l2_close(int fd) 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci operates like the :c:func:`close()` function. 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci.. c:function:: int v4l2_dup(int fd) 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci operates like the libc ``dup()`` function, duplicating a file handler. 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci.. c:function:: int v4l2_ioctl (int fd, unsigned long int request, ...) 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci operates like the :c:func:`ioctl()` function. 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci.. c:function:: int v4l2_read (int fd, void* buffer, size_t n) 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci operates like the :c:func:`read()` function. 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci.. c:function:: void v4l2_mmap(void *start, size_t length, int prot, int flags, int fd, int64_t offset); 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci operates like the :c:func:`munmap()` function. 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci.. c:function:: int v4l2_munmap(void *_start, size_t length); 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci operates like the :c:func:`munmap()` function. 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ciThose functions provide additional control: 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci.. c:function:: int v4l2_fd_open(int fd, int v4l2_flags) 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci opens an already opened fd for further use through v4l2lib and possibly 1528c2ecf20Sopenharmony_ci modify libv4l2's default behavior through the ``v4l2_flags`` argument. 1538c2ecf20Sopenharmony_ci Currently, ``v4l2_flags`` can be ``V4L2_DISABLE_CONVERSION``, to disable 1548c2ecf20Sopenharmony_ci format conversion. 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci.. c:function:: int v4l2_set_control(int fd, int cid, int value) 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci This function takes a value of 0 - 65535, and then scales that range to the 1598c2ecf20Sopenharmony_ci actual range of the given v4l control id, and then if the cid exists and is 1608c2ecf20Sopenharmony_ci not locked sets the cid to the scaled value. 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci.. c:function:: int v4l2_get_control(int fd, int cid) 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci This function returns a value of 0 - 65535, scaled to from the actual range 1658c2ecf20Sopenharmony_ci of the given v4l control id. when the cid does not exist, could not be 1668c2ecf20Sopenharmony_ci accessed for some reason, or some error occurred 0 is returned. 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_civ4l1compat.so wrapper library 1698c2ecf20Sopenharmony_ci============================= 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ciThis library intercepts calls to 1728c2ecf20Sopenharmony_ci:c:func:`open()`, :c:func:`close()`, 1738c2ecf20Sopenharmony_ci:c:func:`ioctl()`, :c:func:`mmap()` and 1748c2ecf20Sopenharmony_ci:c:func:`munmap()` 1758c2ecf20Sopenharmony_cioperations and redirects them to the libv4l counterparts, by using 1768c2ecf20Sopenharmony_ci``LD_PRELOAD=/usr/lib/v4l1compat.so``. It also emulates V4L1 calls via V4L2 1778c2ecf20Sopenharmony_ciAPI. 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ciIt allows usage of binary legacy applications that still don't use 1808c2ecf20Sopenharmony_cilibv4l. 181