162306a36Sopenharmony_ci.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later 262306a36Sopenharmony_ci.. c:namespace:: V4L 362306a36Sopenharmony_ci 462306a36Sopenharmony_ci.. _libv4l-introduction: 562306a36Sopenharmony_ci 662306a36Sopenharmony_ci************ 762306a36Sopenharmony_ciIntroduction 862306a36Sopenharmony_ci************ 962306a36Sopenharmony_ci 1062306a36Sopenharmony_cilibv4l is a collection of libraries which adds a thin abstraction layer 1162306a36Sopenharmony_cion top of video4linux2 devices. The purpose of this (thin) layer is to 1262306a36Sopenharmony_cimake it easy for application writers to support a wide variety of 1362306a36Sopenharmony_cidevices without having to write separate code for different devices in 1462306a36Sopenharmony_cithe same class. 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ciAn example of using libv4l is provided by 1762306a36Sopenharmony_ci:ref:`v4l2grab <v4l2grab-example>`. 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_cilibv4l consists of 3 different libraries: 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_cilibv4lconvert 2262306a36Sopenharmony_ci============= 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_cilibv4lconvert is a library that converts several different pixelformats 2562306a36Sopenharmony_cifound in V4L2 drivers into a few common RGB and YUY formats. 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ciIt currently accepts the following V4L2 driver formats: 2862306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_BGR24 <V4L2-PIX-FMT-BGR24>`, 2962306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_NV12_16L16 <V4L2-PIX-FMT-NV12-16L16>`, 3062306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_JPEG <V4L2-PIX-FMT-JPEG>`, 3162306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_MJPEG <V4L2-PIX-FMT-MJPEG>`, 3262306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_MR97310A <V4L2-PIX-FMT-MR97310A>`, 3362306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_OV511 <V4L2-PIX-FMT-OV511>`, 3462306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_OV518 <V4L2-PIX-FMT-OV518>`, 3562306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_PAC207 <V4L2-PIX-FMT-PAC207>`, 3662306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_PJPG <V4L2-PIX-FMT-PJPG>`, 3762306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_RGB24 <V4L2-PIX-FMT-RGB24>`, 3862306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_SBGGR8 <V4L2-PIX-FMT-SBGGR8>`, 3962306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_SGBRG8 <V4L2-PIX-FMT-SGBRG8>`, 4062306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_SGRBG8 <V4L2-PIX-FMT-SGRBG8>`, 4162306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_SN9C10X <V4L2-PIX-FMT-SN9C10X>`, 4262306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_SN9C20X_I420 <V4L2-PIX-FMT-SN9C20X-I420>`, 4362306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_SPCA501 <V4L2-PIX-FMT-SPCA501>`, 4462306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_SPCA505 <V4L2-PIX-FMT-SPCA505>`, 4562306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_SPCA508 <V4L2-PIX-FMT-SPCA508>`, 4662306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_SPCA561 <V4L2-PIX-FMT-SPCA561>`, 4762306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_SQ905C <V4L2-PIX-FMT-SQ905C>`, 4862306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_SRGGB8 <V4L2-PIX-FMT-SRGGB8>`, 4962306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_UYVY <V4L2-PIX-FMT-UYVY>`, 5062306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_YUV420 <V4L2-PIX-FMT-YUV420>`, 5162306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_YUYV <V4L2-PIX-FMT-YUYV>`, 5262306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_YVU420 <V4L2-PIX-FMT-YVU420>`, and 5362306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_YVYU <V4L2-PIX-FMT-YVYU>`. 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ciLater on libv4lconvert was expanded to also be able to do various video 5662306a36Sopenharmony_ciprocessing functions to improve webcam video quality. The video 5762306a36Sopenharmony_ciprocessing is split in to 2 parts: libv4lconvert/control and 5862306a36Sopenharmony_cilibv4lconvert/processing. 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ciThe control part is used to offer video controls which can be used to 6162306a36Sopenharmony_cicontrol the video processing functions made available by 6262306a36Sopenharmony_cilibv4lconvert/processing. These controls are stored application wide 6362306a36Sopenharmony_ci(until reboot) by using a persistent shared memory object. 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_cilibv4lconvert/processing offers the actual video processing 6662306a36Sopenharmony_cifunctionality. 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_cilibv4l1 6962306a36Sopenharmony_ci======= 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ciThis library offers functions that can be used to quickly make v4l1 7262306a36Sopenharmony_ciapplications work with v4l2 devices. These functions work exactly like 7362306a36Sopenharmony_cithe normal open/close/etc, except that libv4l1 does full emulation of 7462306a36Sopenharmony_cithe v4l1 api on top of v4l2 drivers, in case of v4l1 drivers it will 7562306a36Sopenharmony_cijust pass calls through. 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ciSince those functions are emulations of the old V4L1 API, it shouldn't 7862306a36Sopenharmony_cibe used for new applications. 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_cilibv4l2 8162306a36Sopenharmony_ci======= 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ciThis library should be used for all modern V4L2 applications. 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ciIt provides handles to call V4L2 open/ioctl/close/poll methods. Instead 8662306a36Sopenharmony_ciof just providing the raw output of the device, it enhances the calls in 8762306a36Sopenharmony_cithe sense that it will use libv4lconvert to provide more video formats 8862306a36Sopenharmony_ciand to enhance the image quality. 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ciIn most cases, libv4l2 just passes the calls directly through to the 9162306a36Sopenharmony_civ4l2 driver, intercepting the calls to 9262306a36Sopenharmony_ci:ref:`VIDIOC_TRY_FMT <VIDIOC_G_FMT>`, 9362306a36Sopenharmony_ci:ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>`, 9462306a36Sopenharmony_ci:ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>`, 9562306a36Sopenharmony_ci:ref:`VIDIOC_ENUM_FRAMESIZES <VIDIOC_ENUM_FRAMESIZES>` and 9662306a36Sopenharmony_ci:ref:`VIDIOC_ENUM_FRAMEINTERVALS <VIDIOC_ENUM_FRAMEINTERVALS>` in 9762306a36Sopenharmony_ciorder to emulate the formats 9862306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_BGR24 <V4L2-PIX-FMT-BGR24>`, 9962306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_RGB24 <V4L2-PIX-FMT-RGB24>`, 10062306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_YUV420 <V4L2-PIX-FMT-YUV420>`, and 10162306a36Sopenharmony_ci:ref:`V4L2_PIX_FMT_YVU420 <V4L2-PIX-FMT-YVU420>`, if they aren't 10262306a36Sopenharmony_ciavailable in the driver. :ref:`VIDIOC_ENUM_FMT <VIDIOC_ENUM_FMT>` 10362306a36Sopenharmony_cikeeps enumerating the hardware supported formats, plus the emulated 10462306a36Sopenharmony_ciformats offered by libv4l at the end. 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci.. _libv4l-ops: 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ciLibv4l device control functions 10962306a36Sopenharmony_ci------------------------------- 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ciThe common file operation methods are provided by libv4l. 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ciThose functions operate just like the gcc function ``dup()`` and 11462306a36Sopenharmony_ciV4L2 functions 11562306a36Sopenharmony_ci:c:func:`open()`, :c:func:`close()`, 11662306a36Sopenharmony_ci:c:func:`ioctl()`, :c:func:`read()`, 11762306a36Sopenharmony_ci:c:func:`mmap()` and :c:func:`munmap()`: 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_ci.. c:function:: int v4l2_open(const char *file, int oflag, ...) 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci operates like the :c:func:`open()` function. 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ci.. c:function:: int v4l2_close(int fd) 12462306a36Sopenharmony_ci 12562306a36Sopenharmony_ci operates like the :c:func:`close()` function. 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci.. c:function:: int v4l2_dup(int fd) 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_ci operates like the libc ``dup()`` function, duplicating a file handler. 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci.. c:function:: int v4l2_ioctl (int fd, unsigned long int request, ...) 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_ci operates like the :c:func:`ioctl()` function. 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci.. c:function:: int v4l2_read (int fd, void* buffer, size_t n) 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ci operates like the :c:func:`read()` function. 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci.. c:function:: void *v4l2_mmap(void *start, size_t length, int prot, int flags, int fd, int64_t offset); 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ci operates like the :c:func:`mmap()` function. 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_ci.. c:function:: int v4l2_munmap(void *_start, size_t length); 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ci operates like the :c:func:`munmap()` function. 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_ciThose functions provide additional control: 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci.. c:function:: int v4l2_fd_open(int fd, int v4l2_flags) 15062306a36Sopenharmony_ci 15162306a36Sopenharmony_ci opens an already opened fd for further use through v4l2lib and possibly 15262306a36Sopenharmony_ci modify libv4l2's default behavior through the ``v4l2_flags`` argument. 15362306a36Sopenharmony_ci Currently, ``v4l2_flags`` can be ``V4L2_DISABLE_CONVERSION``, to disable 15462306a36Sopenharmony_ci format conversion. 15562306a36Sopenharmony_ci 15662306a36Sopenharmony_ci.. c:function:: int v4l2_set_control(int fd, int cid, int value) 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ci This function takes a value of 0 - 65535, and then scales that range to the 15962306a36Sopenharmony_ci actual range of the given v4l control id, and then if the cid exists and is 16062306a36Sopenharmony_ci not locked sets the cid to the scaled value. 16162306a36Sopenharmony_ci 16262306a36Sopenharmony_ci.. c:function:: int v4l2_get_control(int fd, int cid) 16362306a36Sopenharmony_ci 16462306a36Sopenharmony_ci This function returns a value of 0 - 65535, scaled to from the actual range 16562306a36Sopenharmony_ci of the given v4l control id. when the cid does not exist, could not be 16662306a36Sopenharmony_ci accessed for some reason, or some error occurred 0 is returned. 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_civ4l1compat.so wrapper library 16962306a36Sopenharmony_ci============================= 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_ciThis library intercepts calls to 17262306a36Sopenharmony_ci:c:func:`open()`, :c:func:`close()`, 17362306a36Sopenharmony_ci:c:func:`ioctl()`, :c:func:`mmap()` and 17462306a36Sopenharmony_ci:c:func:`munmap()` 17562306a36Sopenharmony_cioperations and redirects them to the libv4l counterparts, by using 17662306a36Sopenharmony_ci``LD_PRELOAD=/usr/lib/v4l1compat.so``. It also emulates V4L1 calls via V4L2 17762306a36Sopenharmony_ciAPI. 17862306a36Sopenharmony_ci 17962306a36Sopenharmony_ciIt allows usage of binary legacy applications that still don't use 18062306a36Sopenharmony_cilibv4l. 181