libv4l-introduction.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. .. -*- coding: utf-8; mode: rst -*-
  2. .. _libv4l-introduction:
  3. ************
  4. Introduction
  5. ************
  6. libv4l is a collection of libraries which adds a thin abstraction layer
  7. on top of video4linux2 devices. The purpose of this (thin) layer is to
  8. make it easy for application writers to support a wide variety of
  9. devices without having to write separate code for different devices in
  10. the same class.
  11. An example of using libv4l is provided by
  12. :ref:`v4l2grab <v4l2grab-example>`.
  13. libv4l consists of 3 different libraries:
  14. libv4lconvert
  15. =============
  16. libv4lconvert is a library that converts several different pixelformats
  17. found in V4L2 drivers into a few common RGB and YUY formats.
  18. It currently accepts the following V4L2 driver formats:
  19. :ref:`V4L2_PIX_FMT_BGR24 <V4L2-PIX-FMT-BGR24>`,
  20. :ref:`V4L2_PIX_FMT_HM12 <V4L2-PIX-FMT-HM12>`,
  21. :ref:`V4L2_PIX_FMT_JPEG <V4L2-PIX-FMT-JPEG>`,
  22. :ref:`V4L2_PIX_FMT_MJPEG <V4L2-PIX-FMT-MJPEG>`,
  23. :ref:`V4L2_PIX_FMT_MR97310A <V4L2-PIX-FMT-MR97310A>`,
  24. :ref:`V4L2_PIX_FMT_OV511 <V4L2-PIX-FMT-OV511>`,
  25. :ref:`V4L2_PIX_FMT_OV518 <V4L2-PIX-FMT-OV518>`,
  26. :ref:`V4L2_PIX_FMT_PAC207 <V4L2-PIX-FMT-PAC207>`,
  27. :ref:`V4L2_PIX_FMT_PJPG <V4L2-PIX-FMT-PJPG>`,
  28. :ref:`V4L2_PIX_FMT_RGB24 <V4L2-PIX-FMT-RGB24>`,
  29. :ref:`V4L2_PIX_FMT_SBGGR8 <V4L2-PIX-FMT-SBGGR8>`,
  30. :ref:`V4L2_PIX_FMT_SGBRG8 <V4L2-PIX-FMT-SGBRG8>`,
  31. :ref:`V4L2_PIX_FMT_SGRBG8 <V4L2-PIX-FMT-SGRBG8>`,
  32. :ref:`V4L2_PIX_FMT_SN9C10X <V4L2-PIX-FMT-SN9C10X>`,
  33. :ref:`V4L2_PIX_FMT_SN9C20X_I420 <V4L2-PIX-FMT-SN9C20X-I420>`,
  34. :ref:`V4L2_PIX_FMT_SPCA501 <V4L2-PIX-FMT-SPCA501>`,
  35. :ref:`V4L2_PIX_FMT_SPCA505 <V4L2-PIX-FMT-SPCA505>`,
  36. :ref:`V4L2_PIX_FMT_SPCA508 <V4L2-PIX-FMT-SPCA508>`,
  37. :ref:`V4L2_PIX_FMT_SPCA561 <V4L2-PIX-FMT-SPCA561>`,
  38. :ref:`V4L2_PIX_FMT_SQ905C <V4L2-PIX-FMT-SQ905C>`,
  39. :ref:`V4L2_PIX_FMT_SRGGB8 <V4L2-PIX-FMT-SRGGB8>`,
  40. :ref:`V4L2_PIX_FMT_UYVY <V4L2-PIX-FMT-UYVY>`,
  41. :ref:`V4L2_PIX_FMT_YUV420 <V4L2-PIX-FMT-YUV420>`,
  42. :ref:`V4L2_PIX_FMT_YUYV <V4L2-PIX-FMT-YUYV>`,
  43. :ref:`V4L2_PIX_FMT_YVU420 <V4L2-PIX-FMT-YVU420>`, and
  44. :ref:`V4L2_PIX_FMT_YVYU <V4L2-PIX-FMT-YVYU>`.
  45. Later on libv4lconvert was expanded to also be able to do various video
  46. processing functions to improve webcam video quality. The video
  47. processing is split in to 2 parts: libv4lconvert/control and
  48. libv4lconvert/processing.
  49. The control part is used to offer video controls which can be used to
  50. control the video processing functions made available by
  51. libv4lconvert/processing. These controls are stored application wide
  52. (until reboot) by using a persistent shared memory object.
  53. libv4lconvert/processing offers the actual video processing
  54. functionality.
  55. libv4l1
  56. =======
  57. This library offers functions that can be used to quickly make v4l1
  58. applications work with v4l2 devices. These functions work exactly like
  59. the normal open/close/etc, except that libv4l1 does full emulation of
  60. the v4l1 api on top of v4l2 drivers, in case of v4l1 drivers it will
  61. just pass calls through.
  62. Since those functions are emulations of the old V4L1 API, it shouldn't
  63. be used for new applications.
  64. libv4l2
  65. =======
  66. This library should be used for all modern V4L2 applications.
  67. It provides handles to call V4L2 open/ioctl/close/poll methods. Instead
  68. of just providing the raw output of the device, it enhances the calls in
  69. the sense that it will use libv4lconvert to provide more video formats
  70. and to enhance the image quality.
  71. In most cases, libv4l2 just passes the calls directly through to the
  72. v4l2 driver, intercepting the calls to
  73. :ref:`VIDIOC_TRY_FMT <VIDIOC_G_FMT>`,
  74. :ref:`VIDIOC_G_FMT <VIDIOC_G_FMT>`,
  75. :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>`,
  76. :ref:`VIDIOC_ENUM_FRAMESIZES <VIDIOC_ENUM_FRAMESIZES>` and
  77. :ref:`VIDIOC_ENUM_FRAMEINTERVALS <VIDIOC_ENUM_FRAMEINTERVALS>` in
  78. order to emulate the formats
  79. :ref:`V4L2_PIX_FMT_BGR24 <V4L2-PIX-FMT-BGR24>`,
  80. :ref:`V4L2_PIX_FMT_RGB24 <V4L2-PIX-FMT-RGB24>`,
  81. :ref:`V4L2_PIX_FMT_YUV420 <V4L2-PIX-FMT-YUV420>`, and
  82. :ref:`V4L2_PIX_FMT_YVU420 <V4L2-PIX-FMT-YVU420>`, if they aren't
  83. available in the driver. :ref:`VIDIOC_ENUM_FMT <VIDIOC_ENUM_FMT>`
  84. keeps enumerating the hardware supported formats, plus the emulated
  85. formats offered by libv4l at the end.
  86. .. _libv4l-ops:
  87. Libv4l device control functions
  88. -------------------------------
  89. The common file operation methods are provided by libv4l.
  90. Those functions operate just like the gcc function ``dup()`` and
  91. V4L2 functions
  92. :c:func:`open() <v4l2-open>`, :c:func:`close() <v4l2-close>`,
  93. :c:func:`ioctl() <v4l2-ioctl>`, :c:func:`read() <v4l2-read>`,
  94. :c:func:`mmap() <v4l2-mmap>` and :c:func:`munmap() <v4l2-munmap>`:
  95. .. c:function:: int v4l2_open(const char *file, int oflag, ...)
  96. operates like the :c:func:`open() <v4l2-open>` function.
  97. .. c:function:: int v4l2_close(int fd)
  98. operates like the :c:func:`close() <v4l2-close>` function.
  99. .. c:function:: int v4l2_dup(int fd)
  100. operates like the libc ``dup()`` function, duplicating a file handler.
  101. .. c:function:: int v4l2_ioctl (int fd, unsigned long int request, ...)
  102. operates like the :c:func:`ioctl() <v4l2-ioctl>` function.
  103. .. c:function:: int v4l2_read (int fd, void* buffer, size_t n)
  104. operates like the :c:func:`read() <v4l2-read>` function.
  105. .. c:function:: void v4l2_mmap(void *start, size_t length, int prot, int flags, int fd, int64_t offset);
  106. operates like the :c:func:`munmap() <v4l2-munmap>` function.
  107. .. c:function:: int v4l2_munmap(void *_start, size_t length);
  108. operates like the :c:func:`munmap() <v4l2-munmap>` function.
  109. Those functions provide additional control:
  110. .. c:function:: int v4l2_fd_open(int fd, int v4l2_flags)
  111. opens an already opened fd for further use through v4l2lib and possibly
  112. modify libv4l2's default behavior through the ``v4l2_flags`` argument.
  113. Currently, ``v4l2_flags`` can be ``V4L2_DISABLE_CONVERSION``, to disable
  114. format conversion.
  115. .. c:function:: int v4l2_set_control(int fd, int cid, int value)
  116. This function takes a value of 0 - 65535, and then scales that range to the
  117. actual range of the given v4l control id, and then if the cid exists and is
  118. not locked sets the cid to the scaled value.
  119. .. c:function:: int v4l2_get_control(int fd, int cid)
  120. This function returns a value of 0 - 65535, scaled to from the actual range
  121. of the given v4l control id. when the cid does not exist, could not be
  122. accessed for some reason, or some error occurred 0 is returned.
  123. v4l1compat.so wrapper library
  124. =============================
  125. This library intercepts calls to
  126. :c:func:`open() <v4l2-open>`, :c:func:`close() <v4l2-close>`,
  127. :c:func:`ioctl() <v4l2-ioctl>`, :c:func:`mmap() <v4l2-mmap>` and
  128. :c:func:`munmap() <v4l2-munmap>`
  129. operations and redirects them to the libv4l counterparts, by using
  130. ``LD_PRELOAD=/usr/lib/v4l1compat.so``. It also emulates V4L1 calls via V4L2
  131. API.
  132. It allows usage of binary legacy applications that still don't use
  133. libv4l.