<title>Libv4l Userspace Library</title>
<section id="libv4l-introduction">
	<title>Introduction</title>

	<para>libv4l is a collection of libraries which adds a thin abstraction
layer on top of video4linux2 devices. The purpose of this (thin) layer
is to make it easy for application writers to support a wide variety of
devices without having to write separate code for different devices in the
same class.</para>
<para>An example of using libv4l is provided by
<link linkend='v4l2grab-example'>v4l2grab</link>.
</para>

	<para>libv4l consists of 3 different libraries:</para>
	<section>
		<title>libv4lconvert</title>

		<para>libv4lconvert is a library that converts several
different pixelformats found in V4L2 drivers into a few common RGB and
YUY formats.</para>
		<para>It currently accepts the following V4L2 driver formats:
<link linkend="V4L2-PIX-FMT-BGR24"><constant>V4L2_PIX_FMT_BGR24</constant></link>,
<link linkend="V4L2-PIX-FMT-HM12"><constant>V4L2_PIX_FMT_HM12</constant></link>,
<link linkend="V4L2-PIX-FMT-JPEG"><constant>V4L2_PIX_FMT_JPEG</constant></link>,
<link linkend="V4L2-PIX-FMT-MJPEG"><constant>V4L2_PIX_FMT_MJPEG</constant></link>,
<link linkend="V4L2-PIX-FMT-MR97310A"><constant>V4L2_PIX_FMT_MR97310A</constant></link>,
<link linkend="V4L2-PIX-FMT-OV511"><constant>V4L2_PIX_FMT_OV511</constant></link>,
<link linkend="V4L2-PIX-FMT-OV518"><constant>V4L2_PIX_FMT_OV518</constant></link>,
<link linkend="V4L2-PIX-FMT-PAC207"><constant>V4L2_PIX_FMT_PAC207</constant></link>,
<link linkend="V4L2-PIX-FMT-PJPG"><constant>V4L2_PIX_FMT_PJPG</constant></link>,
<link linkend="V4L2-PIX-FMT-RGB24"><constant>V4L2_PIX_FMT_RGB24</constant></link>,
<link linkend="V4L2-PIX-FMT-SBGGR8"><constant>V4L2_PIX_FMT_SBGGR8</constant></link>,
<link linkend="V4L2-PIX-FMT-SGBRG8"><constant>V4L2_PIX_FMT_SGBRG8</constant></link>,
<link linkend="V4L2-PIX-FMT-SGRBG8"><constant>V4L2_PIX_FMT_SGRBG8</constant></link>,
<link linkend="V4L2-PIX-FMT-SN9C10X"><constant>V4L2_PIX_FMT_SN9C10X</constant></link>,
<link linkend="V4L2-PIX-FMT-SN9C20X-I420"><constant>V4L2_PIX_FMT_SN9C20X_I420</constant></link>,
<link linkend="V4L2-PIX-FMT-SPCA501"><constant>V4L2_PIX_FMT_SPCA501</constant></link>,
<link linkend="V4L2-PIX-FMT-SPCA505"><constant>V4L2_PIX_FMT_SPCA505</constant></link>,
<link linkend="V4L2-PIX-FMT-SPCA508"><constant>V4L2_PIX_FMT_SPCA508</constant></link>,
<link linkend="V4L2-PIX-FMT-SPCA561"><constant>V4L2_PIX_FMT_SPCA561</constant></link>,
<link linkend="V4L2-PIX-FMT-SQ905C"><constant>V4L2_PIX_FMT_SQ905C</constant></link>,
<constant>V4L2_PIX_FMT_SRGGB8</constant>,
<link linkend="V4L2-PIX-FMT-UYVY"><constant>V4L2_PIX_FMT_UYVY</constant></link>,
<link linkend="V4L2-PIX-FMT-YUV420"><constant>V4L2_PIX_FMT_YUV420</constant></link>,
<link linkend="V4L2-PIX-FMT-YUYV"><constant>V4L2_PIX_FMT_YUYV</constant></link>,
<link linkend="V4L2-PIX-FMT-YVU420"><constant>V4L2_PIX_FMT_YVU420</constant></link>,
and <link linkend="V4L2-PIX-FMT-YVYU"><constant>V4L2_PIX_FMT_YVYU</constant></link>.
</para>
		<para>Later on libv4lconvert was expanded to also be able to do
various	video processing functions to improve webcam video quality.
The video processing is split in to 2 parts: libv4lconvert/control and
libv4lconvert/processing.</para>

		<para>The control part is used to offer video controls which can
be used	to control the video processing functions made available by
	libv4lconvert/processing. These controls are stored application wide
(until reboot) by using a persistent shared memory object.</para>

		<para>libv4lconvert/processing offers the actual video
processing functionality.</para>
	</section>
	<section>
		<title>libv4l1</title>
		<para>This library offers functions that can be used to quickly
make v4l1 applications work with v4l2 devices. These functions work exactly
like the normal open/close/etc, except that libv4l1 does full emulation of
the v4l1 api on top of v4l2 drivers, in case of v4l1 drivers it
will just pass calls through.</para>
		<para>Since those functions are emulations of the old V4L1 API,
it shouldn't be used for new applications.</para>
	</section>
	<section>
		<title>libv4l2</title>
		<para>This library should be used for all modern V4L2
applications.</para>
		<para>It provides handles to call V4L2 open/ioctl/close/poll
methods. Instead of just providing the raw output of the device, it enhances
the calls in the sense that it will use libv4lconvert to provide more video
formats and to enhance the image quality.</para>
		<para>In most cases, libv4l2 just passes the calls directly
through to the v4l2 driver, intercepting the calls to
<link linkend='vidioc-g-fmt'><constant>VIDIOC_TRY_FMT</constant></link>,
<link linkend='vidioc-g-fmt'><constant>VIDIOC_G_FMT</constant></link>
<link linkend='vidioc-g-fmt'><constant>VIDIOC_S_FMT</constant></link>
<link linkend='vidioc-enum-framesizes'><constant>VIDIOC_ENUM_FRAMESIZES</constant></link>
and <link linkend='vidioc-enum-frameintervals'><constant>VIDIOC_ENUM_FRAMEINTERVALS</constant></link>
in order to emulate the formats
<link linkend="V4L2-PIX-FMT-BGR24"><constant>V4L2_PIX_FMT_BGR24</constant></link>,
<link linkend="V4L2-PIX-FMT-RGB24"><constant>V4L2_PIX_FMT_RGB24</constant></link>,
<link linkend="V4L2-PIX-FMT-YUV420"><constant>V4L2_PIX_FMT_YUV420</constant></link>,
and <link linkend="V4L2-PIX-FMT-YVU420"><constant>V4L2_PIX_FMT_YVU420</constant></link>,
if they aren't available in the driver.
<link linkend='vidioc-enum-fmt'><constant>VIDIOC_ENUM_FMT</constant></link>
keeps enumerating the hardware supported formats, plus the emulated formats
offered by libv4l at the end.
</para>
		<section id="libv4l-ops">
			<title>Libv4l device control functions</title>
			<para>The common file operation methods are provided by
libv4l.</para>
			<para>Those functions operate just like glibc
open/close/dup/ioctl/read/mmap/munmap:</para>
<itemizedlist><listitem>
			<para>int v4l2_open(const char *file, int oflag,
...) -
operates like the standard <link linkend='func-open'>open()</link> function.
</para></listitem><listitem>
			<para>int v4l2_close(int fd) -
operates like the standard <link linkend='func-close'>close()</link> function.
</para></listitem><listitem>
			<para>int v4l2_dup(int fd) -
operates like the standard dup() function, duplicating a file handler.
</para></listitem><listitem>
			<para>int v4l2_ioctl (int fd, unsigned long int request, ...) -
operates like the standard <link linkend='func-ioctl'>ioctl()</link> function.
</para></listitem><listitem>
			<para>int v4l2_read (int fd, void* buffer, size_t n) -
operates like the standard <link linkend='func-read'>read()</link> function.
</para></listitem><listitem>
			<para>void v4l2_mmap(void *start, size_t length, int prot, int flags, int fd, int64_t offset); -
operates like the standard <link linkend='func-mmap'>mmap()</link> function.
</para></listitem><listitem>
			<para>int v4l2_munmap(void *_start, size_t length); -
operates like the standard <link linkend='func-munmap'>munmap()</link> function.
</para></listitem>
</itemizedlist>
			<para>Those functions provide additional control:</para>
<itemizedlist><listitem>
			<para>int v4l2_fd_open(int fd, int v4l2_flags) -
opens an already opened fd for further use through v4l2lib and possibly
modify libv4l2's default behavior through the v4l2_flags argument.
Currently, v4l2_flags can be <constant>V4L2_DISABLE_CONVERSION</constant>,
to disable format conversion.
</para></listitem><listitem>
			<para>int v4l2_set_control(int fd, int cid, int value) -
This function takes a value of 0 - 65535, and then scales that range to
the actual range of the given v4l control id, and then if the cid exists
and is not locked sets the cid to the scaled value.
</para></listitem><listitem>
			<para>int v4l2_get_control(int fd, int cid) -
This function returns a value of 0 - 65535, scaled to from the actual range
of the given v4l control id. when the cid does not exist, could not be
accessed for some reason, or some error occurred 0 is returned.
</para></listitem>
</itemizedlist>
		</section>
	</section>
	<section>

		<title>v4l1compat.so wrapper library</title>

		<para>This library intercepts calls to
open/close/ioctl/mmap/mmunmap operations and redirects them to the libv4l
counterparts, by using LD_PRELOAD=/usr/lib/v4l1compat.so. It also
emulates V4L1 calls via V4L2 API.</para>
		<para>It allows usage of binary legacy applications that
still don't use libv4l.</para>
	</section>

</section>
