Video for Linux Two API Specification

Revision 0.24

Michael H Schimek

            
          

Bill Dirks

Hans Verkuil

Martin Rubli

This document is copyrighted © 1999-2008 by Bill Dirks, Michael H. Schimek, Hans Verkuil and Martin Rubli.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the appendix entitled "GNU Free Documentation License".

Programming examples can be used and distributed without restrictions.


Table of Contents
Introduction
1. Common API Elements
1.1. Opening and Closing Devices
1.1.1. Device Naming
1.1.2. Related Devices
1.1.3. Multiple Opens
1.1.4. Shared Data Streams
1.1.5. Functions
1.2. Querying Capabilities
1.3. Application Priority
1.4. Video Inputs and Outputs
1.5. Audio Inputs and Outputs
1.6. Tuners and Modulators
1.6.1. Tuners
1.6.2. Modulators
1.6.3. Radio Frequency
1.6.4. Satellite Receivers
1.7. Video Standards
1.8. User Controls
1.9. Extended Controls
1.9.1. Introduction
1.9.2. The Extended Control API
1.9.3. Enumerating Extended Controls
1.9.4. Creating Control Panels
1.9.5. MPEG Control Reference
1.9.6. Camera Control Reference
1.10. Data Formats
1.10.1. Data Format Negotiation
1.10.2. Image Format Enumeration
1.11. Image Cropping, Insertion and Scaling
1.11.1. Cropping Structures
1.11.2. Scaling Adjustments
1.11.3. Examples
1.12. Streaming Parameters
2. Image Formats
2.1. Standard Image Formats
2.2. Colorspaces
2.3. Indexed Format
2.4. RGB Formats
Packed RGB formats -- Packed RGB formats
V4L2_PIX_FMT_SBGGR8 ('BA81') -- Bayer RGB format
V4L2_PIX_FMT_SBGGR16 ('BA82') -- Bayer RGB format
2.5. YUV Formats
Packed YUV formats -- Packed YUV formats
V4L2_PIX_FMT_GREY ('GREY') -- Grey-scale image
V4L2_PIX_FMT_Y16 ('Y16 ') -- Grey-scale image
V4L2_PIX_FMT_YUYV ('YUYV') -- Packed format with ½ horizontal chroma resolution, also known as YUV 4:2:2
V4L2_PIX_FMT_UYVY ('UYVY') -- Variation of V4L2_PIX_FMT_YUYV with different order of samples in memory
V4L2_PIX_FMT_Y41P ('Y41P') -- Format with ¼ horizontal chroma resolution, also known as YUV 4:1:1
V4L2_PIX_FMT_YVU420 ('YV12'), V4L2_PIX_FMT_YUV420 ('YU12') -- Planar formats with ½ horizontal and vertical chroma resolution, also known as YUV 4:2:0
V4L2_PIX_FMT_YVU410 ('YVU9'), V4L2_PIX_FMT_YUV410 ('YUV9') -- Planar formats with ¼ horizontal and vertical chroma resolution, also known as YUV 4:1:0
V4L2_PIX_FMT_YUV422P ('422P') -- Format with ½ horizontal chroma resolution, also known as YUV 4:2:2. Planar layout as opposed to V4L2_PIX_FMT_YUYV
V4L2_PIX_FMT_YUV411P ('411P') -- Format with ¼ horizontal chroma resolution, also known as YUV 4:1:1. Planar layout as opposed to V4L2_PIX_FMT_Y41P
V4L2_PIX_FMT_NV12 ('NV12'), V4L2_PIX_FMT_NV21 ('NV21') -- Formats with ½ horizontal and vertical chroma resolution, also known as YUV 4:2:0. One luminance and one chrominance plane with alternating chroma samples as opposed to V4L2_PIX_FMT_YVU420
2.6. Compressed Formats
2.7. Reserved Format Identifiers
3. Input/Output
3.1. Read/Write
3.2. Streaming I/O (Memory Mapping)
3.3. Streaming I/O (User Pointers)
3.4. Asynchronous I/O
3.5. Buffers
3.5.1. Timecodes
3.6. Field Order
4. Interfaces
4.1. Video Capture Interface
4.1.1. Querying Capabilities
4.1.2. Supplemental Functions
4.1.3. Image Format Negotiation
4.1.4. Reading Images
4.2. Video Overlay Interface
4.2.1. Querying Capabilities
4.2.2. Supplemental Functions
4.2.3. Setup
4.2.4. Overlay Window
4.2.5. Enabling Overlay
4.3. Video Output Interface
4.3.1. Querying Capabilities
4.3.2. Supplemental Functions
4.3.3. Image Format Negotiation
4.3.4. Writing Images
4.4. Video Output Overlay Interface
4.4.1. Querying Capabilities
4.4.2. Framebuffer
4.4.3. Overlay Window and Scaling
4.4.4. Enabling Overlay
4.5. Codec Interface
4.6. Effect Devices Interface
4.7. Raw VBI Data Interface
4.7.1. Querying Capabilities
4.7.2. Supplemental Functions
4.7.3. Raw VBI Format Negotiation
4.7.4. Reading and writing VBI images
4.8. Sliced VBI Data Interface
4.8.1. Querying Capabilities
4.8.2. Supplemental Functions
4.8.3. Sliced VBI Format Negotiation
4.8.4. Reading and writing sliced VBI data
4.9. Teletext Interface
4.10. Radio Interface
4.10.1. Querying Capabilities
4.10.2. Supplemental Functions
4.10.3. Programming
4.11. RDS Interface
I. Function Reference
V4L2 close() -- Close a V4L2 device
V4L2 ioctl() -- Program a V4L2 device
ioctl VIDIOC_CROPCAP -- Information about the video cropping and scaling abilities
ioctl VIDIOC_DBG_G_REGISTER, VIDIOC_DBG_S_REGISTER -- Read or write hardware registers
ioctl VIDIOC_ENCODER_CMD, VIDIOC_TRY_ENCODER_CMD -- Execute an encoder command
ioctl VIDIOC_ENUMAUDIO -- Enumerate audio inputs
ioctl VIDIOC_ENUMAUDOUT -- Enumerate audio outputs
ioctl VIDIOC_ENUM_FMT -- Enumerate image formats
ioctl VIDIOC_ENUM_FRAMESIZES -- Enumerate frame sizes
ioctl VIDIOC_ENUM_FRAMEINTERVALS -- Enumerate frame intervals
ioctl VIDIOC_ENUMINPUT -- Enumerate video inputs
ioctl VIDIOC_ENUMOUTPUT -- Enumerate video outputs
ioctl VIDIOC_ENUMSTD -- Enumerate supported video standards
ioctl VIDIOC_G_AUDIO, VIDIOC_S_AUDIO -- Query or select the current audio input and its attributes
ioctl VIDIOC_G_AUDOUT, VIDIOC_S_AUDOUT -- Query or select the current audio output
ioctl VIDIOC_G_CHIP_IDENT -- Identify the chips on a TV card
ioctl VIDIOC_G_CROP, VIDIOC_S_CROP -- Get or set the current cropping rectangle
ioctl VIDIOC_G_CTRL, VIDIOC_S_CTRL -- Get or set the value of a control
ioctl VIDIOC_G_ENC_INDEX -- Get meta data about a compressed video stream
ioctl VIDIOC_G_EXT_CTRLS, VIDIOC_S_EXT_CTRLS, VIDIOC_TRY_EXT_CTRLS -- Get or set the value of several controls, try control values
ioctl VIDIOC_G_FBUF, VIDIOC_S_FBUF -- Get or set frame buffer overlay parameters
ioctl VIDIOC_G_FMT, VIDIOC_S_FMT, VIDIOC_TRY_FMT -- Get or set the data format, try a format
ioctl VIDIOC_G_FREQUENCY, VIDIOC_S_FREQUENCY -- Get or set tuner or modulator radio frequency
ioctl VIDIOC_G_INPUT, VIDIOC_S_INPUT -- Query or select the current video input
ioctl VIDIOC_G_JPEGCOMP, VIDIOC_S_JPEGCOMP -- 
ioctl VIDIOC_G_MODULATOR, VIDIOC_S_MODULATOR -- Get or set modulator attributes
ioctl VIDIOC_G_OUTPUT, VIDIOC_S_OUTPUT -- Query or select the current video output
ioctl VIDIOC_G_PARM, VIDIOC_S_PARM -- Get or set streaming parameters
ioctl VIDIOC_G_PRIORITY, VIDIOC_S_PRIORITY -- Query or request the access priority associated with a file descriptor
ioctl VIDIOC_G_SLICED_VBI_CAP -- Query sliced VBI capabilities
ioctl VIDIOC_G_STD, VIDIOC_S_STD -- Query or select the video standard of the current input
ioctl VIDIOC_G_TUNER, VIDIOC_S_TUNER -- Get or set tuner attributes
ioctl VIDIOC_LOG_STATUS -- Log driver status information
ioctl VIDIOC_OVERLAY -- Start or stop video overlay
ioctl VIDIOC_QBUF, VIDIOC_DQBUF -- Exchange a buffer with the driver
ioctl VIDIOC_QUERYBUF -- Query the status of a buffer
ioctl VIDIOC_QUERYCAP -- Query device capabilities
ioctl VIDIOC_QUERYCTRL, VIDIOC_QUERYMENU -- Enumerate controls and menu control items
ioctl VIDIOC_QUERYSTD -- Sense the video standard received by the current input
ioctl VIDIOC_REQBUFS -- Initiate Memory Mapping or User Pointer I/O
ioctl VIDIOC_STREAMON, VIDIOC_STREAMOFF -- Start or stop streaming I/O
V4L2 mmap() -- Map device memory into application address space
V4L2 munmap() -- Unmap device memory
V4L2 open() -- Open a V4L2 device
V4L2 poll() -- Wait for some event on a file descriptor
V4L2 read() -- Read from a V4L2 device
V4L2 select() -- Synchronous I/O multiplexing
V4L2 write() -- Write to a V4L2 device
5. V4L2 Driver Programming
6. Changes
6.1. Differences between V4L and V4L2
6.1.1. Opening and Closing Devices
6.1.2. Querying Capabilities
6.1.3. Video Sources
6.1.4. Tuning
6.1.5. Image Properties
6.1.6. Audio
6.1.7. Frame Buffer Overlay
6.1.8. Cropping
6.1.9. Reading Images, Memory Mapping
6.1.10. Reading Raw VBI Data
6.1.11. Miscellaneous
6.2. Changes of the V4L2 API
6.2.1. Early Versions
6.2.2. V4L2 Version 0.16 1999-01-31
6.2.3. V4L2 Version 0.18 1999-03-16
6.2.4. V4L2 Version 0.19 1999-06-05
6.2.5. V4L2 Version 0.20 (1999-09-10)
6.2.6. V4L2 Version 0.20 incremental changes
6.2.7. V4L2 Version 0.20 2000-11-23
6.2.8. V4L2 Version 0.20 2002-07-25
6.2.9. V4L2 in Linux 2.5.46, 2002-10
6.2.10. V4L2 2003-06-19
6.2.11. V4L2 2003-11-05
6.2.12. V4L2 in Linux 2.6.6, 2004-05-09
6.2.13. V4L2 in Linux 2.6.8
6.2.14. V4L2 spec erratum 2004-08-01
6.2.15. V4L2 in Linux 2.6.14
6.2.16. V4L2 in Linux 2.6.15
6.2.17. V4L2 spec erratum 2005-11-27
6.2.18. V4L2 spec erratum 2006-01-10
6.2.19. V4L2 spec erratum 2006-02-03
6.2.20. V4L2 spec erratum 2006-02-04
6.2.21. V4L2 in Linux 2.6.17
6.2.22. V4L2 spec erratum 2006-09-23 (Draft 0.15)
6.2.23. V4L2 in Linux 2.6.18
6.2.24. V4L2 in Linux 2.6.19
6.2.25. V4L2 spec erratum 2006-10-12 (Draft 0.17)
6.2.26. V4L2 in Linux 2.6.21
6.2.27. V4L2 in Linux 2.6.22
6.2.28. V4L2 in Linux 2.6.24
6.2.29. V4L2 in Linux 2.6.25
6.3. Relation of V4L2 to other Linux multimedia APIs
6.3.1. X Video Extension
6.3.2. Digital Video
6.3.3. Audio Interfaces
6.4. Experimental API Elements
6.5. Obsolete API Elements
A. Video For Linux Two Header File
B. Video Capture Example
C. GNU Free Documentation License
C.1. 0. PREAMBLE
C.2. 1. APPLICABILITY AND DEFINITIONS
C.3. 2. VERBATIM COPYING
C.4. 3. COPYING IN QUANTITY
C.5. 4. MODIFICATIONS
C.6. 5. COMBINING DOCUMENTS
C.7. 6. COLLECTIONS OF DOCUMENTS
C.8. 7. AGGREGATION WITH INDEPENDENT WORKS
C.9. 8. TRANSLATION
C.10. 9. TERMINATION
C.11. 10. FUTURE REVISIONS OF THIS LICENSE
C.12. Addendum
List of Types
References
List of Figures
1-1. Image Cropping, Insertion and Scaling
3-1. Field Order, Top Field First Transmitted
3-2. Field Order, Bottom Field First Transmitted
4-1. Line synchronization
4-2. ITU-R 525 line numbering (M/NTSC and M/PAL)
4-3. ITU-R 625 line numbering
List of Examples
1-1. Information about the current video input
1-2. Switching to the first video input
1-3. Information about the current audio input
1-4. Switching to the first audio input
1-5. Information about the current video standard
1-6. Listing the video standards supported by the current input
1-7. Selecting a new video standard
1-8. Enumerating all controls
1-9. Changing controls
1-10. Resetting the cropping parameters
1-11. Simple downscaling
1-12. Selecting an output area
1-13. Current scaling factor and pixel aspect
2-1. ITU-R Rec. BT.601 color conversion
2-1. V4L2_PIX_FMT_BGR24 4 × 4 pixel image
2-1. V4L2_PIX_FMT_SBGGR8 4 × 4 pixel image
2-1. V4L2_PIX_FMT_SBGGR16 4 × 4 pixel image
2-1. V4L2_PIX_FMT_GREY 4 × 4 pixel image
2-1. V4L2_PIX_FMT_Y16 4 × 4 pixel image
2-1. V4L2_PIX_FMT_YUYV 4 × 4 pixel image
2-1. V4L2_PIX_FMT_UYVY 4 × 4 pixel image
2-1. V4L2_PIX_FMT_Y41P 8 × 4 pixel image
2-1. V4L2_PIX_FMT_YVU420 4 × 4 pixel image
2-1. V4L2_PIX_FMT_YVU410 4 × 4 pixel image
2-1. V4L2_PIX_FMT_YUV422P 4 × 4 pixel image
2-1. V4L2_PIX_FMT_YUV411P 4 × 4 pixel image
2-1. V4L2_PIX_FMT_NV12 4 × 4 pixel image
3-1. Mapping buffers
3-2. Initiating streaming I/O with user pointers
4-1. Finding a framebuffer device for OSD

Introduction

Video For Linux Two is the second version of the Video For Linux API, a kernel interface for analog radio and video capture and output drivers.

Early drivers used ad-hoc interfaces. These were replaced in Linux 2.2 by Alan Cox' V4L API, based on the interface of the bttv driver. In 1999 Bill Dirks started the development of V4L2 to fix some shortcomings of V4L and to support a wider range of devices. The API was revised again in 2002 prior to its inclusion in Linux 2.5/2.6, and work continues on improvements and additions while maintaining compatibility with existing drivers and applications. In 2006/2007 efforts began on FreeBSD drivers with a V4L2 interface.

This book documents the V4L2 API. Intended audience are driver and application writers.

If you have questions or ideas regarding the API, please write to the Video4Linux mailing list: https://listman.redhat.com/mailman/listinfo/video4linux-list. For inquiries about the V4L2 specification contact the maintainer mschimek@gmx.at.

The latest version of this document and the DocBook SGML sources are hosted at http://v4l2spec.bytesex.org, and http://linuxtv.org/downloads/video4linux/API/V4L2_API.


Chapter 1. Common API Elements

Programming a V4L2 device consists of these steps:

In practice most steps are optional and can be executed out of order. It depends on the V4L2 device type, you can read about the details in Chapter 4. In this chapter we will discuss the basic concepts applicable to all devices.


1.1. Opening and Closing Devices

1.1.1. Device Naming

V4L2 drivers are implemented as kernel modules, loaded manually by the system administrator or automatically when a device is first opened. The driver modules plug into the "videodev" kernel module. It provides helper functions and a common application interface specified in this document.

Each driver thus loaded registers one or more device nodes with major number 81 and a minor number between 0 and 255. Assigning minor numbers to V4L2 devices is entirely up to the system administrator, this is primarily intended to solve conflicts between devices.[1] The module options to select minor numbers are named after the device special file with a "_nr" suffix. For example "video_nr" for /dev/video video capture devices. The number is an offset to the base minor number associated with the device type. [2] When the driver supports multiple devices of the same type more than one minor number can be assigned, separated by commas:

> insmod mydriver.o video_nr=0,1 radio_nr=0,1

In /etc/modules.conf this may be written as:

alias char-major-81-0 mydriver
alias char-major-81-1 mydriver
alias char-major-81-64 mydriver              (1)
options mydriver video_nr=0,1 radio_nr=0,1   (2)
          
(1)
When an application attempts to open a device special file with major number 81 and minor number 0, 1, or 64, load "mydriver" (and the "videodev" module it depends upon).
(2)
Register the first two video capture devices with minor number 0 and 1 (base number is 0), the first two radio device with minor number 64 and 65 (base 64).

When no minor number is given as module option the driver supplies a default. Chapter 4 recommends the base minor numbers to be used for the various device types. Obviously minor numbers must be unique. When the number is already in use the offending device will not be registered.

By convention system administrators create various character device special files with these major and minor numbers in the /dev directory. The names recomended for the different V4L2 device types are listed in Chapter 4.

The creation of character special files (with mknod) is a privileged operation and devices cannot be opened by major and minor number. That means applications cannot reliable scan for loaded or installed drivers. The user must enter a device name, or the application can try the conventional device names.

Under the device filesystem (devfs) the minor number options are ignored. V4L2 drivers (or by proxy the "videodev" module) automatically create the required device files in the /dev/v4l directory using the conventional device names above.


1.1.2. Related Devices

Devices can support several related functions. For example video capturing, video overlay and VBI capturing are related because these functions share, amongst other, the same video input and tuner frequency. V4L and earlier versions of V4L2 used the same device name and minor number for video capturing and overlay, but different ones for VBI. Experience showed this approach has several problems[3], and to make things worse the V4L videodev module used to prohibit multiple opens of a device.

As a remedy the present version of the V4L2 API relaxed the concept of device types with specific names and minor numbers. For compatibility with old applications drivers must still register different minor numbers to assign a default function to the device. But if related functions are supported by the driver they must be available under all registered minor numbers. The desired function can be selected after opening the device as described in Chapter 4.

Imagine a driver supporting video capturing, video overlay, raw VBI capturing, and FM radio reception. It registers three devices with minor number 0, 64 and 224 (this numbering scheme is inherited from the V4L API). Regardless if /dev/video (81, 0) or /dev/vbi (81, 224) is opened the application can select any one of the video capturing, overlay or VBI capturing functions. Without programming (e. g. reading from the device with dd or cat) /dev/video captures video images, while /dev/vbi captures raw VBI data. /dev/radio (81, 64) is invariable a radio device, unrelated to the video functions. Being unrelated does not imply the devices can be used at the same time, however. The open() function may very well return an EBUSY error code.

Besides video input or output the hardware may also support audio sampling or playback. If so, these functions are implemented as OSS or ALSA PCM devices and eventually OSS or ALSA audio mixer. The V4L2 API makes no provisions yet to find these related devices. If you have an idea please write to the Video4Linux mailing list: https://listman.redhat.com/mailman/listinfo/video4linux-list.


1.1.3. Multiple Opens

In general, V4L2 devices can be opened more than once. When this is supported by the driver, users can for example start a "panel" application to change controls like brightness or audio volume, while another application captures video and audio. In other words, panel applications are comparable to an OSS or ALSA audio mixer application. When a device supports multiple functions like capturing and overlay simultaneously, multiple opens allow concurrent use of the device by forked processes or specialized applications.

Multiple opens are optional, although drivers should permit at least concurrent accesses without data exchange, i. e. panel applications. This implies open() can return an EBUSY error code when the device is already in use, as well as ioctl() functions initiating data exchange (namely the VIDIOC_S_FMT ioctl), and the read() and write() functions.

Mere opening a V4L2 device does not grant exclusive access.[4] Initiating data exchange however assigns the right to read or write the requested type of data, and to change related properties, to this file descriptor. Applications can request additional access privileges using the priority mechanism described in Section 1.3.


1.1.4. Shared Data Streams

V4L2 drivers should not support multiple applications reading or writing the same data stream on a device by copying buffers, time multiplexing or similar means. This is better handled by a proxy application in user space. When the driver supports stream sharing anyway it must be implemented transparently. The V4L2 API does not specify how conflicts are solved.


1.1.5. Functions

To open and close V4L2 devices applications use the open() and close() function, respectively. Devices are programmed using the ioctl() function as explained in the following sections.


1.2. Querying Capabilities

Because V4L2 covers a wide variety of devices not all aspects of the API are equally applicable to all types of devices. Furthermore devices of the same type have different capabilities and this specification permits the omission of a few complicated and less important parts of the API.

The VIDIOC_QUERYCAP ioctl is available to check if the kernel device is compatible with this specification, and to query the functions and I/O methods supported by the device. Other features can be queried by calling the respective ioctl, for example VIDIOC_ENUMINPUT to learn about the number, types and names of video connectors on the device. Although abstraction is a major objective of this API, the ioctl also allows driver specific applications to reliable identify the driver.

All V4L2 drivers must support VIDIOC_QUERYCAP. Applications should always call this ioctl after opening the device.


1.3. Application Priority

When multiple applications share a device it may be desirable to assign them different priorities. Contrary to the traditional "rm -rf /" school of thought a video recording application could for example block other applications from changing video controls or switching the current TV channel. Another objective is to permit low priority applications working in background, which can be preempted by user controlled applications and automatically regain control of the device at a later time.

Since these features cannot be implemented entirely in user space V4L2 defines the VIDIOC_G_PRIORITY and VIDIOC_S_PRIORITY ioctls to request and query the access priority associate with a file descriptor. Opening a device assigns a medium priority, compatible with earlier versions of V4L2 and drivers not supporting these ioctls. Applications requiring a different priority will usually call VIDIOC_S_PRIORITY after verifying the device with the VIDIOC_QUERYCAP ioctl.

Ioctls changing driver properties, such as VIDIOC_S_INPUT, return an EBUSY error code after another application obtained higher priority. An event mechanism to notify applications about asynchronous property changes has been proposed but not added yet.


1.4. Video Inputs and Outputs

Video inputs and outputs are physical connectors of a device. These can be for example RF connectors (antenna/cable), CVBS a.k.a. Composite Video, S-Video or RGB connectors. Only video and VBI capture devices have inputs, output devices have outputs, at least one each. Radio devices have no video inputs or outputs.

To learn about the number and attributes of the available inputs and outputs applications can enumerate them with the VIDIOC_ENUMINPUT and VIDIOC_ENUMOUTPUT ioctl, respectively. The struct v4l2_input returned by the VIDIOC_ENUMINPUT ioctl also contains signal status information applicable when the current video input is queried.

The VIDIOC_G_INPUT and VIDIOC_G_OUTPUT ioctl return the index of the current video input or output. To select a different input or output applications call the VIDIOC_S_INPUT and VIDIOC_S_OUTPUT ioctl. Drivers must implement all the input ioctls when the device has one or more inputs, all the output ioctls when the device has one or more outputs.

Example 1-1. Information about the current video input

struct v4l2_input input;
int index;

if (-1 == ioctl (fd, VIDIOC_G_INPUT, &index)) {
        perror ("VIDIOC_G_INPUT");
        exit (EXIT_FAILURE);
}

memset (&input, 0, sizeof (input));
input.index = index;

if (-1 == ioctl (fd, VIDIOC_ENUMINPUT, &input)) {
        perror ("VIDIOC_ENUMINPUT");
        exit (EXIT_FAILURE);
}

printf ("Current input: %s\n", input.name);
      

Example 1-2. Switching to the first video input

int index;

index = 0;

if (-1 == ioctl (fd, VIDIOC_S_INPUT, &index)) {
        perror ("VIDIOC_S_INPUT");
        exit (EXIT_FAILURE);
}
      

1.5. Audio Inputs and Outputs

Audio inputs and outputs are physical connectors of a device. Video capture devices have inputs, output devices have outputs, zero or more each. Radio devices have no audio inputs or outputs. They have exactly one tuner which in fact is an audio source, but this API associates tuners with video inputs or outputs only, and radio devices have none of these.[5] A connector on a TV card to loop back the received audio signal to a sound card is not considered an audio output.

Audio and video inputs and outputs are associated. Selecting a video source also selects an audio source. This is most evident when the video and audio source is a tuner. Further audio connectors can combine with more than one video input or output. Assumed two composite video inputs and two audio inputs exist, there may be up to four valid combinations. The relation of video and audio connectors is defined in the audioset field of the respective struct v4l2_input or struct v4l2_output, where each bit represents the index number, starting at zero, of one audio input or output.

To learn about the number and attributes of the available inputs and outputs applications can enumerate them with the VIDIOC_ENUMAUDIO and VIDIOC_ENUMAUDOUT ioctl, respectively. The struct v4l2_audio returned by the VIDIOC_ENUMAUDIO ioctl also contains signal status information applicable when the current audio input is queried.

The VIDIOC_G_AUDIO and VIDIOC_G_AUDOUT ioctl report the current audio input and output, respectively. Note that, unlike VIDIOC_G_INPUT and VIDIOC_G_OUTPUT these ioctls return a structure as VIDIOC_ENUMAUDIO and VIDIOC_ENUMAUDOUT do, not just an index.

To select an audio input and change its properties applications call the VIDIOC_S_AUDIO ioctl. To select an audio output (which presently has no changeable properties) applications call the VIDIOC_S_AUDOUT ioctl.

Drivers must implement all input ioctls when the device has one or more inputs, all output ioctls when the device has one or more outputs. When the device has any audio inputs or outputs the driver must set the V4L2_CAP_AUDIO flag in the struct v4l2_capability returned by the VIDIOC_QUERYCAP ioctl.

Example 1-3. Information about the current audio input

struct v4l2_audio audio;

memset (&audio, 0, sizeof (audio));

if (-1 == ioctl (fd, VIDIOC_G_AUDIO, &audio)) {
        perror ("VIDIOC_G_AUDIO");
        exit (EXIT_FAILURE);
}

printf ("Current input: %s\n", audio.name);
      

Example 1-4. Switching to the first audio input

struct v4l2_audio audio;

memset (&audio, 0, sizeof (audio)); /* clear audio.mode, audio.reserved */

audio.index = 0;

if (-1 == ioctl (fd, VIDIOC_S_AUDIO, &audio)) {
        perror ("VIDIOC_S_AUDIO");
        exit (EXIT_FAILURE);
}
      

1.6. Tuners and Modulators

1.6.1. Tuners

Video input devices can have one or more tuners demodulating a RF signal. Each tuner is associated with one or more video inputs, depending on the number of RF connectors on the tuner. The type field of the respective struct v4l2_input returned by the VIDIOC_ENUMINPUT ioctl is set to V4L2_INPUT_TYPE_TUNER and its tuner field contains the index number of the tuner.

Radio devices have exactly one tuner with index zero, no video inputs.

To query and change tuner properties applications use the VIDIOC_G_TUNER and VIDIOC_S_TUNER ioctl, respectively. The struct v4l2_tuner returned by VIDIOC_G_TUNER also contains signal status information applicable when the tuner of the current video input, or a radio tuner is queried. Note that VIDIOC_S_TUNER does not switch the current tuner, when there is more than one at all. The tuner is solely determined by the current video input. Drivers must support both ioctls and set the V4L2_CAP_TUNER flag in the struct v4l2_capability returned by the VIDIOC_QUERYCAP ioctl when the device has one or more tuners.


1.6.2. Modulators

Video output devices can have one or more modulators, uh, modulating a video signal for radiation or connection to the antenna input of a TV set or video recorder. Each modulator is associated with one or more video outputs, depending on the number of RF connectors on the modulator. The type field of the respective struct v4l2_output returned by the VIDIOC_ENUMOUTPUT ioctl is set to V4L2_OUTPUT_TYPE_MODULATOR and its modulator field contains the index number of the modulator. This specification does not define radio output devices.

To query and change modulator properties applications use the VIDIOC_G_MODULATOR and VIDIOC_S_MODULATOR ioctl. Note that VIDIOC_S_MODULATOR does not switch the current modulator, when there is more than one at all. The modulator is solely determined by the current video output. Drivers must support both ioctls and set the V4L2_CAP_TUNER (sic) flag in the struct v4l2_capability returned by the VIDIOC_QUERYCAP ioctl when the device has one or more modulators.


1.6.3. Radio Frequency

To get and set the tuner or modulator radio frequency applications use the VIDIOC_G_FREQUENCY and VIDIOC_S_FREQUENCY ioctl which both take a pointer to a struct v4l2_frequency. These ioctls are used for TV and radio devices alike. Drivers must support both ioctls when the tuner or modulator ioctls are supported, or when the device is a radio device.


1.6.4. Satellite Receivers

To be discussed. See also proposals by Peter Schlaf, video4linux-list@redhat.com on 23 Oct 2002, subject: "Re: [V4L] Re: v4l2 api".


1.7. Video Standards

Video devices typically support one or more different video standards or variations of standards. Each video input and output may support another set of standards. This set is reported by the std field of struct v4l2_input and struct v4l2_output returned by the VIDIOC_ENUMINPUT and VIDIOC_ENUMOUTPUT ioctl, respectively.

V4L2 defines one bit for each analog video standard currently in use worldwide, and sets aside bits for driver defined standards, e. g. hybrid standards to watch NTSC video tapes on PAL TVs and vice versa. Applications can use the predefined bits to select a particular standard, although presenting the user a menu of supported standards is preferred. To enumerate and query the attributes of the supported standards applications use the VIDIOC_ENUMSTD ioctl.

Many of the defined standards are actually just variations of a few major standards. The hardware may in fact not distinguish between them, or do so internal and switch automatically. Therefore enumerated standards also contain sets of one or more standard bits.

Assume a hypothetic tuner capable of demodulating B/PAL, G/PAL and I/PAL signals. The first enumerated standard is a set of B and G/PAL, switched automatically depending on the selected radio frequency in UHF or VHF band. Enumeration gives a "PAL-B/G" or "PAL-I" choice. Similar a Composite input may collapse standards, enumerating "PAL-B/G/H/I", "NTSC-M" and "SECAM-D/K".[6]

To query and select the standard used by the current video input or output applications call the VIDIOC_G_STD and VIDIOC_S_STD ioctl, respectively. The received standard can be sensed with the VIDIOC_QUERYSTD ioctl. Note parameter of all these ioctls is a pointer to a v4l2_std_id type (a standard set), not an index into the standard enumeration.[7] Drivers must implement all video standard ioctls when the device has one or more video inputs or outputs.

Special rules apply to USB cameras where the notion of video standards makes little sense. More generally any capture device, output devices accordingly, which is

  • incapable of capturing fields or frames at the nominal rate of the video standard, or

  • where timestamps refer to the instant the field or frame was received by the driver, not the capture time, or

  • where sequence numbers refer to the frames received by the driver, not the captured frames.

Here the driver shall set the std field of struct v4l2_input and struct v4l2_output to zero, the VIDIOC_G_STD, VIDIOC_S_STD, VIDIOC_QUERYSTD and VIDIOC_ENUMSTD ioctls shall return the EINVAL error code.[8]

Example 1-5. Information about the current video standard

v4l2_std_id std_id;
struct v4l2_standard standard;

if (-1 == ioctl (fd, VIDIOC_G_STD, &std_id)) {
        /* Note when VIDIOC_ENUMSTD always returns EINVAL this
           is no video device or it falls under the USB exception,
           and VIDIOC_G_STD returning EINVAL is no error. */

        perror ("VIDIOC_G_STD");
        exit (EXIT_FAILURE);
}

memset (&standard, 0, sizeof (standard));
standard.index = 0;

while (0 == ioctl (fd, VIDIOC_ENUMSTD, &standard)) {
        if (standard.id & std_id) {
               printf ("Current video standard: %s\n", standard.name);
               exit (EXIT_SUCCESS);
        }

        standard.index++;
}

/* EINVAL indicates the end of the enumeration, which cannot be
   empty unless this device falls under the USB exception. */

if (errno == EINVAL || standard.index == 0) {
        perror ("VIDIOC_ENUMSTD");
        exit (EXIT_FAILURE);
}
      

Example 1-6. Listing the video standards supported by the current input

struct v4l2_input input;
struct v4l2_standard standard;

memset (&input, 0, sizeof (input));

if (-1 == ioctl (fd, VIDIOC_G_INPUT, &input.index)) {
        perror ("VIDIOC_G_INPUT");
        exit (EXIT_FAILURE);
}

if (-1 == ioctl (fd, VIDIOC_ENUMINPUT, &input)) {
        perror ("VIDIOC_ENUM_INPUT");
        exit (EXIT_FAILURE);
}

printf ("Current input %s supports:\n", input.name);

memset (&standard, 0, sizeof (standard));
standard.index = 0;

while (0 == ioctl (fd, VIDIOC_ENUMSTD, &standard)) {
        if (standard.id & input.std)
                printf ("%s\n", standard.name);

        standard.index++;
}

/* EINVAL indicates the end of the enumeration, which cannot be
   empty unless this device falls under the USB exception. */

if (errno != EINVAL || standard.index == 0) {
        perror ("VIDIOC_ENUMSTD");
        exit (EXIT_FAILURE);
}
      

Example 1-7. Selecting a new video standard

struct v4l2_input input;
v4l2_std_id std_id;

memset (&input, 0, sizeof (input));

if (-1 == ioctl (fd, VIDIOC_G_INPUT, &input.index)) {
        perror ("VIDIOC_G_INPUT");
        exit (EXIT_FAILURE);
}

if (-1 == ioctl (fd, VIDIOC_ENUMINPUT, &input)) {
        perror ("VIDIOC_ENUM_INPUT");
        exit (EXIT_FAILURE);
}

if (0 == (input.std & V4L2_STD_PAL_BG)) {
        fprintf (stderr, "Oops. B/G PAL is not supported.\n");
        exit (EXIT_FAILURE);
}

/* Note this is also supposed to work when only B
   or G/PAL is supported. */

std_id = V4L2_STD_PAL_BG;

if (-1 == ioctl (fd, VIDIOC_S_STD, &std_id)) {
        perror ("VIDIOC_S_STD");
        exit (EXIT_FAILURE);
}
      

1.8. User Controls

Devices typically have a number of user-settable controls such as brightness, saturation and so on, which would be presented to the user on a graphical user interface. But, different devices will have different controls available, and furthermore, the range of possible values, and the default value will vary from device to device. The control ioctls provide the information and a mechanism to create a nice user interface for these controls that will work correctly with any device.

All controls are accessed using an ID value. V4L2 defines several IDs for specific purposes. Drivers can also implement their own custom controls using V4L2_CID_PRIVATE_BASE and higher values. The pre-defined control IDs have the prefix V4L2_CID_, and are listed in Table 1-1. The ID is used when querying the attributes of a control, and when getting or setting the current value.

Generally applications should present controls to the user without assumptions about their purpose. Each control comes with a name string the user is supposed to understand. When the purpose is non-intuitive the driver writer should provide a user manual, a user interface plug-in or a driver specific panel application. Predefined IDs were introduced to change a few controls programmatically, for example to mute a device during a channel switch.

Drivers may enumerate different controls after switching the current video input or output, tuner or modulator, or audio input or output. Different in the sense of other bounds, another default and current value, step size or other menu items. A control with a certain custom ID can also change name and type.[9] Control values are stored globally, they do not change when switching except to stay within the reported bounds. They also do not change e. g. when the device is opened or closed, when the tuner radio frequency is changed or generally never without application request. Since V4L2 specifies no event mechanism, panel applications intended to cooperate with other panel applications (be they built into a larger application, as a TV viewer) may need to regularly poll control values to update their user interface.[10]

Table 1-1. Control IDs

IDTypeDescription
V4L2_CID_BASE First predefined ID, equal to V4L2_CID_BRIGHTNESS.
V4L2_CID_USER_BASE Synonym of V4L2_CID_BASE.
V4L2_CID_BRIGHTNESSintegerPicture brightness, or more precisely, the black level.
V4L2_CID_CONTRASTintegerPicture contrast or luma gain.
V4L2_CID_SATURATIONintegerPicture color saturation or chroma gain.
V4L2_CID_HUEintegerHue or color balance.
V4L2_CID_AUDIO_VOLUMEintegerOverall audio volume. Note some drivers also provide an OSS or ALSA mixer interface.
V4L2_CID_AUDIO_BALANCEintegerAudio stereo balance. Minimum corresponds to all the way left, maximum to right.
V4L2_CID_AUDIO_BASSintegerAudio bass adjustment.
V4L2_CID_AUDIO_TREBLEintegerAudio treble adjustment.
V4L2_CID_AUDIO_MUTEbooleanMute audio, i. e. set the volume to zero, however without affecting V4L2_CID_AUDIO_VOLUME. Like ALSA drivers, V4L2 drivers must mute at load time to avoid excessive noise. Actually the entire device should be reset to a low power consumption state.
V4L2_CID_AUDIO_LOUDNESSbooleanLoudness mode (bass boost).
V4L2_CID_BLACK_LEVELintegerAnother name for brightness (not a synonym of V4L2_CID_BRIGHTNESS). This control is deprecated and should not be used in new drivers and applications.
V4L2_CID_AUTO_WHITE_BALANCEbooleanAutomatic white balance (cameras).
V4L2_CID_DO_WHITE_BALANCEbuttonThis is an action control. When set (the value is ignored), the device will do a white balance and then hold the current setting. Contrast this with the boolean V4L2_CID_AUTO_WHITE_BALANCE, which, when activated, keeps adjusting the white balance.
V4L2_CID_RED_BALANCEintegerRed chroma balance.
V4L2_CID_BLUE_BALANCEintegerBlue chroma balance.
V4L2_CID_GAMMAintegerGamma adjust.
V4L2_CID_WHITENESSintegerWhiteness for grey-scale devices. This is a synonym for V4L2_CID_GAMMA. This control is deprecated and should not be used in new drivers and applications.
V4L2_CID_EXPOSUREintegerExposure (cameras). [Unit?]
V4L2_CID_AUTOGAINbooleanAutomatic gain/exposure control.
V4L2_CID_GAINintegerGain control.
V4L2_CID_HFLIPbooleanMirror the picture horizontally.
V4L2_CID_VFLIPbooleanMirror the picture vertically.
V4L2_CID_HCENTER_DEPRECATED (formerly V4L2_CID_HCENTER)integerHorizontal image centering. This control is deprecated. New drivers and applications should use the Camera class controls V4L2_CID_PAN_ABSOLUTE, V4L2_CID_PAN_RELATIVE and V4L2_CID_PAN_RESET instead.
V4L2_CID_VCENTER_DEPRECATED (formerly V4L2_CID_VCENTER)integerVertical image centering. Centering is intended to physically adjust cameras. For image cropping see Section 1.11, for clipping Section 4.2. This control is deprecated. New drivers and applications should use the Camera class controls V4L2_CID_TILT_ABSOLUTE, V4L2_CID_TILT_RELATIVE and V4L2_CID_TILT_RESET instead.
V4L2_CID_POWER_LINE_FREQUENCYintegerEnables a power line frequency filter to avoid flicker. Possible values are: V4L2_CID_POWER_LINE_FREQUENCY_DISABLED (0), V4L2_CID_POWER_LINE_FREQUENCY_50HZ (1) and V4L2_CID_POWER_LINE_FREQUENCY_60HZ (2).
V4L2_CID_HUE_AUTObooleanEnables automatic hue control by the device. The effect of setting V4L2_CID_HUE while automatic hue control is enabled is undefined, drivers should ignore such request.
V4L2_CID_WHITE_BALANCE_TEMPERATUREintegerThis control specifies the white balance settings as a color temperature in Kelvin. A driver should have a minimum of 2800 (incandescent) to 6500 (daylight). For more information about color temperature see Wikipedia.
V4L2_CID_SHARPNESSintegerAdjusts the sharpness filters in a camera. The minimum value disables the filters, higher values give a sharper picture.
V4L2_CID_BACKLIGHT_COMPENSATIONintegerAdjusts the backlight compensation in a camera. The minimum value disables backlight compensation.
V4L2_CID_LASTP1 End of the predefined control IDs (currently V4L2_CID_BACKLIGHT_COMPENSATION + 1).
V4L2_CID_PRIVATE_BASE ID of the first custom (driver specific) control. Applications depending on particular custom controls should check the driver name and version, see Section 1.2.

Applications can enumerate the available controls with the VIDIOC_QUERYCTRL and VIDIOC_QUERYMENU ioctls, get and set a control value with the VIDIOC_G_CTRL and VIDIOC_S_CTRL ioctls. Drivers must implement VIDIOC_QUERYCTRL, VIDIOC_G_CTRL and VIDIOC_S_CTRL when the device has one or more controls, VIDIOC_QUERYMENU when it has one or more menu type controls.

Example 1-8. Enumerating all controls

struct v4l2_queryctrl queryctrl;
struct v4l2_querymenu querymenu;

static void
enumerate_menu (void)
{
        printf ("  Menu items:\n");

        memset (&querymenu, 0, sizeof (querymenu));
        querymenu.id = queryctrl.id;

        for (querymenu.index = queryctrl.minimum;
             querymenu.index <= queryctrl.maximum;
              querymenu.index++) {
                if (0 == ioctl (fd, VIDIOC_QUERYMENU, &querymenu)) {
                        printf ("  %s\n", querymenu.name);
                } else {
                        perror ("VIDIOC_QUERYMENU");
                        exit (EXIT_FAILURE);
                }
        }
}

memset (&queryctrl, 0, sizeof (queryctrl));

for (queryctrl.id = V4L2_CID_BASE;
     queryctrl.id < V4L2_CID_LASTP1;
     queryctrl.id++) {
        if (0 == ioctl (fd, VIDIOC_QUERYCTRL, &queryctrl)) {
                if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED)
                        continue;

                printf ("Control %s\n", queryctrl.name);

                if (queryctrl.type == V4L2_CTRL_TYPE_MENU)
                        enumerate_menu ();
        } else {
                if (errno == EINVAL)
                        continue;

                perror ("VIDIOC_QUERYCTRL");
                exit (EXIT_FAILURE);
        }
}

for (queryctrl.id = V4L2_CID_PRIVATE_BASE;;
     queryctrl.id++) {
        if (0 == ioctl (fd, VIDIOC_QUERYCTRL, &queryctrl)) {
                if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED)
                        continue;

                printf ("Control %s\n", queryctrl.name);

                if (queryctrl.type == V4L2_CTRL_TYPE_MENU)
                        enumerate_menu ();
        } else {
                if (errno == EINVAL)
                        break;

                perror ("VIDIOC_QUERYCTRL");
                exit (EXIT_FAILURE);
        }
}

Example 1-9. Changing controls

struct v4l2_queryctrl queryctrl;
struct v4l2_control control;

memset (&queryctrl, 0, sizeof (queryctrl));
queryctrl.id = V4L2_CID_BRIGHTNESS;

if (-1 == ioctl (fd, VIDIOC_QUERYCTRL, &queryctrl)) {
        if (errno != EINVAL) {
                perror ("VIDIOC_QUERYCTRL");
                exit (EXIT_FAILURE);
        } else {
                printf ("V4L2_CID_BRIGHTNESS is not supported\n");
        }
} else if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED) {
        printf ("V4L2_CID_BRIGHTNESS is not supported\n");
} else {
        memset (&control, 0, sizeof (control));
        control.id = V4L2_CID_BRIGHTNESS;
        control.value = queryctrl.default_value;

        if (-1 == ioctl (fd, VIDIOC_S_CTRL, &control)) {
                perror ("VIDIOC_S_CTRL");
                exit (EXIT_FAILURE);
        }
}

memset (&control, 0, sizeof (control));
control.id = V4L2_CID_CONTRAST;

if (0 == ioctl (fd, VIDIOC_G_CTRL, &control)) {
        control.value += 1;

        /* The driver may clamp the value or return ERANGE, ignored here */

        if (-1 == ioctl (fd, VIDIOC_S_CTRL, &control)
            && errno != ERANGE) {
                perror ("VIDIOC_S_CTRL");
                exit (EXIT_FAILURE);
        }
/* Ignore if V4L2_CID_CONTRAST is unsupported */
} else if (errno != EINVAL) {
        perror ("VIDIOC_G_CTRL");
        exit (EXIT_FAILURE);
}

control.id = V4L2_CID_AUDIO_MUTE;
control.value = TRUE; /* silence */

/* Errors ignored */
ioctl (fd, VIDIOC_S_CTRL, &control);

1.9. Extended Controls

1.9.1. Introduction

The control mechanism as originally designed was meant to be used for user settings (brightness, saturation, etc). However, it turned out to be a very useful model for implementing more complicated driver APIs where each driver implements only a subset of a larger API.

The MPEG encoding API was the driving force behind designing and implementing this extended control mechanism: the MPEG standard is quite large and the currently supported hardware MPEG encoders each only implement a subset of this standard. Further more, many parameters relating to how the video is encoded into an MPEG stream are specific to the MPEG encoding chip since the MPEG standard only defines the format of the resulting MPEG stream, not how the video is actually encoded into that format.

Unfortunately, the original control API lacked some features needed for these new uses and so it was extended into the (not terribly originally named) extended control API.


1.9.2. The Extended Control API

Three new ioctls are available: VIDIOC_G_EXT_CTRLS, VIDIOC_S_EXT_CTRLS and VIDIOC_TRY_EXT_CTRLS. These ioctls act on arrays of controls (as opposed to the VIDIOC_G_CTRL and VIDIOC_S_CTRL ioctls that act on a single control). This is needed since it is often required to atomically change several controls at once.

Each of the new ioctls expects a pointer to a struct v4l2_ext_controls. This structure contains a pointer to the control array, a count of the number of controls in that array and a control class. Control classes are used to group similar controls into a single class. For example, control class V4L2_CTRL_CLASS_USER contains all user controls (i. e. all controls that can also be set using the old VIDIOC_S_CTRL ioctl). Control class V4L2_CTRL_CLASS_MPEG contains all controls relating to MPEG encoding, etc.

All controls in the control array must belong to the specified control class. An error is returned if this is not the case.

It is also possible to use an empty control array (count == 0) to check whether the specified control class is supported.

The control array is a struct v4l2_ext_control array. The v4l2_ext_control structure is very similar to struct v4l2_control, except for the fact that it also allows for 64-bit values and pointers to be passed (although the latter is not yet used anywhere).

It is important to realize that due to the flexibility of controls it is necessary to check whether the control you want to set actually is supported in the driver and what the valid range of values is. So use the VIDIOC_QUERYCTRL and VIDIOC_QUERYMENU ioctls to check this. Also note that it is possible that some of the menu indices in a control of type V4L2_CTRL_TYPE_MENU may not be supported (VIDIOC_QUERYMENU will return an error). A good example is the list of supported MPEG audio bitrates. Some drivers only support one or two bitrates, others support a wider range.


1.9.3. Enumerating Extended Controls

The recommended way to enumerate over the extended controls is by using VIDIOC_QUERYCTRL in combination with the V4L2_CTRL_FLAG_NEXT_CTRL flag:

struct v4l2_queryctrl qctrl;

qctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
while (0 == ioctl (fd, VIDIOC_QUERYCTRL, &qctrl)) {
        /* ... */
        qctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
}

The initial control ID is set to 0 ORed with the V4L2_CTRL_FLAG_NEXT_CTRL flag. The VIDIOC_QUERYCTRL ioctl will return the first control with a higher ID than the specified one. When no such controls are found an error is returned.

If you want to get all controls within a specific control class, then you can set the initial qctrl.id value to the control class and add an extra check to break out of the loop when a control of another control class is found:

qctrl.id = V4L2_CTRL_CLASS_MPEG | V4L2_CTRL_FLAG_NEXT_CTRL;
while (0 == ioctl (fd, VIDIOC_QUERYCTRL, &qctrl)) {
        if (V4L2_CTRL_ID2CLASS (qctrl.id) != V4L2_CTRL_CLASS_MPEG)
                break;
                /* ... */
                qctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
        }

The 32-bit qctrl.id value is subdivided into three bit ranges: the top 4 bits are reserved for flags (e. g. V4L2_CTRL_FLAG_NEXT_CTRL) and are not actually part of the ID. The remaining 28 bits form the control ID, of which the most significant 12 bits define the control class and the least significant 16 bits identify the control within the control class. It is guaranteed that these last 16 bits are always non-zero for controls. The range of 0x1000 and up are reserved for driver-specific controls. The macro V4L2_CTRL_ID2CLASS(id) returns the control class ID based on a control ID.

If the driver does not support extended controls, then VIDIOC_QUERYCTRL will fail when used in combination with V4L2_CTRL_FLAG_NEXT_CTRL. In that case the old method of enumerating control should be used (see 1.8). But if it is supported, then it is guaranteed to enumerate over all controls, including driver-private controls.


1.9.4. Creating Control Panels

It is possible to create control panels for a graphical user interface where the user can select the various controls. Basically you will have to iterate over all controls using the method described above. Each control class starts with a control of type V4L2_CTRL_TYPE_CTRL_CLASS. VIDIOC_QUERYCTRL will return the name of this control class which can be used as the title of a tab page within a control panel.

The flags field of struct v4l2_queryctrl also contains hints on the behavior of the control. See the VIDIOC_QUERYCTRL documentation for more details.


1.9.5. MPEG Control Reference

Below all controls within the MPEG control class are described. First the generic controls, then controls specific for certain hardware.


1.9.5.1. Generic MPEG Controls

Table 1-2. MPEG Control IDs

IDType 
 Description
    
V4L2_CID_MPEG_CLASS class 
 The MPEG class descriptor. Calling VIDIOC_QUERYCTRL for this control will return a description of this control class. This description can be used as the caption of a Tab page in a GUI, for example.
    
V4L2_CID_MPEG_STREAM_TYPE enum 
 The MPEG-1, -2 or -4 output stream type. One cannot assume anything here. Each hardware MPEG encoder tends to support different subsets of the available MPEG stream types. The currently defined stream types are:
 
V4L2_MPEG_STREAM_TYPE_MPEG2_PS MPEG-2 program stream
V4L2_MPEG_STREAM_TYPE_MPEG2_TS MPEG-2 transport stream
V4L2_MPEG_STREAM_TYPE_MPEG1_SS MPEG-1 system stream
V4L2_MPEG_STREAM_TYPE_MPEG2_DVD MPEG-2 DVD-compatible stream
V4L2_MPEG_STREAM_TYPE_MPEG1_VCD MPEG-1 VCD-compatible stream
V4L2_MPEG_STREAM_TYPE_MPEG2_SVCD MPEG-2 SVCD-compatible stream
    
V4L2_CID_MPEG_STREAM_PID_PMT integer 
 Program Map Table Packet ID for the MPEG transport stream (default 16)
    
V4L2_CID_MPEG_STREAM_PID_AUDIO integer 
 Audio Packet ID for the MPEG transport stream (default 256)
    
V4L2_CID_MPEG_STREAM_PID_VIDEO integer 
 Video Packet ID for the MPEG transport stream (default 260)
    
V4L2_CID_MPEG_STREAM_PID_PCR integer 
 Packet ID for the MPEG transport stream carrying PCR fields (default 259)
    
V4L2_CID_MPEG_STREAM_PES_ID_AUDIO integer 
 Audio ID for MPEG PES
    
V4L2_CID_MPEG_STREAM_PES_ID_VIDEO integer 
 Video ID for MPEG PES
    
V4L2_CID_MPEG_STREAM_VBI_FMT enum 
 Some cards can embed VBI data (e. g. Closed Caption, Teletext) into the MPEG stream. This control selects whether VBI data should be embedded, and if so, what embedding method should be used. The list of possible VBI formats depends on the driver. The currently defined VBI format types are:
 
V4L2_MPEG_STREAM_VBI_FMT_NONE No VBI in the MPEG stream
V4L2_MPEG_STREAM_VBI_FMT_IVTV VBI in private packets, IVTV format (documented in the kernel sources in the file Documentation/video4linux/cx2341x/README.vbi)
    
V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ enum 
 MPEG Audio sampling frequency. Possible values are:
 
V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100 44.1 kHz
V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000 48 kHz
V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000 32 kHz
    
V4L2_CID_MPEG_AUDIO_ENCODING enum 
 MPEG Audio encoding. Possible values are:
 
V4L2_MPEG_AUDIO_ENCODING_LAYER_1 MPEG Layer I encoding
V4L2_MPEG_AUDIO_ENCODING_LAYER_2 MPEG Layer II encoding
V4L2_MPEG_AUDIO_ENCODING_LAYER_3 MPEG Layer III encoding
    
V4L2_CID_MPEG_AUDIO_L1_BITRATE enum 
 Layer I bitrate. Possible values are:
 
V4L2_MPEG_AUDIO_L1_BITRATE_32K 32 kbit/s
V4L2_MPEG_AUDIO_L1_BITRATE_64K 64 kbit/s
V4L2_MPEG_AUDIO_L1_BITRATE_96K 96 kbit/s
V4L2_MPEG_AUDIO_L1_BITRATE_128K 128 kbit/s
V4L2_MPEG_AUDIO_L1_BITRATE_160K 160 kbit/s
V4L2_MPEG_AUDIO_L1_BITRATE_192K 192 kbit/s
V4L2_MPEG_AUDIO_L1_BITRATE_224K 224 kbit/s
V4L2_MPEG_AUDIO_L1_BITRATE_256K 256 kbit/s
V4L2_MPEG_AUDIO_L1_BITRATE_288K 288 kbit/s
V4L2_MPEG_AUDIO_L1_BITRATE_320K 320 kbit/s
V4L2_MPEG_AUDIO_L1_BITRATE_352K 352 kbit/s
V4L2_MPEG_AUDIO_L1_BITRATE_384K 384 kbit/s
V4L2_MPEG_AUDIO_L1_BITRATE_416K 416 kbit/s
V4L2_MPEG_AUDIO_L1_BITRATE_448K 448 kbit/s
    
V4L2_CID_MPEG_AUDIO_L2_BITRATE enum 
 Layer II bitrate. Possible values are:
 
V4L2_MPEG_AUDIO_L2_BITRATE_32K 32 kbit/s
V4L2_MPEG_AUDIO_L2_BITRATE_48K 48 kbit/s
V4L2_MPEG_AUDIO_L2_BITRATE_56K 56 kbit/s
V4L2_MPEG_AUDIO_L2_BITRATE_64K 64 kbit/s
V4L2_MPEG_AUDIO_L2_BITRATE_80K 80 kbit/s
V4L2_MPEG_AUDIO_L2_BITRATE_96K 96 kbit/s
V4L2_MPEG_AUDIO_L2_BITRATE_112K 112 kbit/s
V4L2_MPEG_AUDIO_L2_BITRATE_128K 128 kbit/s
V4L2_MPEG_AUDIO_L2_BITRATE_160K 160 kbit/s
V4L2_MPEG_AUDIO_L2_BITRATE_192K 192 kbit/s
V4L2_MPEG_AUDIO_L2_BITRATE_224K 224 kbit/s
V4L2_MPEG_AUDIO_L2_BITRATE_256K 256 kbit/s
V4L2_MPEG_AUDIO_L2_BITRATE_320K 320 kbit/s
V4L2_MPEG_AUDIO_L2_BITRATE_384K 384 kbit/s
    
V4L2_CID_MPEG_AUDIO_L3_BITRATE enum 
 Layer III bitrate. Possible values are:
 
V4L2_MPEG_AUDIO_L3_BITRATE_32K 32 kbit/s
V4L2_MPEG_AUDIO_L3_BITRATE_40K 40 kbit/s
V4L2_MPEG_AUDIO_L3_BITRATE_48K 48 kbit/s
V4L2_MPEG_AUDIO_L3_BITRATE_56K 56 kbit/s
V4L2_MPEG_AUDIO_L3_BITRATE_64K 64 kbit/s
V4L2_MPEG_AUDIO_L3_BITRATE_80K 80 kbit/s
V4L2_MPEG_AUDIO_L3_BITRATE_96K 96 kbit/s
V4L2_MPEG_AUDIO_L3_BITRATE_112K 112 kbit/s
V4L2_MPEG_AUDIO_L3_BITRATE_128K 128 kbit/s
V4L2_MPEG_AUDIO_L3_BITRATE_160K 160 kbit/s
V4L2_MPEG_AUDIO_L3_BITRATE_192K 192 kbit/s
V4L2_MPEG_AUDIO_L3_BITRATE_224K 224 kbit/s
V4L2_MPEG_AUDIO_L3_BITRATE_256K 256 kbit/s
V4L2_MPEG_AUDIO_L3_BITRATE_320K 320 kbit/s
    
V4L2_CID_MPEG_AUDIO_MODE enum 
 MPEG Audio mode. Possible values are:
 
V4L2_MPEG_AUDIO_MODE_STEREO Stereo
V4L2_MPEG_AUDIO_MODE_JOINT_STEREO Joint Stereo
V4L2_MPEG_AUDIO_MODE_DUAL Bilingual
V4L2_MPEG_AUDIO_MODE_MONO Mono
    
V4L2_CID_MPEG_AUDIO_MODE_EXTENSION enum 
 Joint Stereo audio mode extension. In Layer I and II they indicate which subbands are in intensity stereo. All other subbands are coded in stereo. Layer III is not (yet) supported. Possible values are:
 
V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_4 Subbands 4-31 in intensity stereo
V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_8 Subbands 8-31 in intensity stereo
V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_12 Subbands 12-31 in intensity stereo
V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_16 Subbands 16-31 in intensity stereo
    
V4L2_CID_MPEG_AUDIO_EMPHASIS enum 
 Audio Emphasis. Possible values are:
 
V4L2_MPEG_AUDIO_EMPHASIS_NONE None
V4L2_MPEG_AUDIO_EMPHASIS_50_DIV_15_uS 50/15 microsecond emphasis
V4L2_MPEG_AUDIO_EMPHASIS_CCITT_J17 CCITT J.17
    
V4L2_CID_MPEG_AUDIO_CRC enum 
 CRC method. Possible values are:
 
V4L2_MPEG_AUDIO_CRC_NONE None
V4L2_MPEG_AUDIO_CRC_CRC16 16 bit parity check
    
V4L2_CID_MPEG_AUDIO_MUTE bool 
 Mutes the audio when capturing. This is not done by muting audio hardware, which can still produce a slight hiss, but in the encoder itself, guaranteeing a fixed and reproducable audio bitstream. 0 = unmuted, 1 = muted.
    
V4L2_CID_MPEG_VIDEO_ENCODING enum 
 MPEG Video encoding method. Possible values are:
 
V4L2_MPEG_VIDEO_ENCODING_MPEG_1 MPEG-1 Video encoding
V4L2_MPEG_VIDEO_ENCODING_MPEG_2 MPEG-2 Video encoding
    
V4L2_CID_MPEG_VIDEO_ASPECT enum 
 Video aspect. Possible values are:
 
V4L2_MPEG_VIDEO_ASPECT_1x1  
V4L2_MPEG_VIDEO_ASPECT_4x3  
V4L2_MPEG_VIDEO_ASPECT_16x9  
V4L2_MPEG_VIDEO_ASPECT_221x100  
    
V4L2_CID_MPEG_VIDEO_B_FRAMES integer 
 Number of B-Frames (default 2)
    
V4L2_CID_MPEG_VIDEO_GOP_SIZE integer 
 GOP size (default 12)
    
V4L2_CID_MPEG_VIDEO_GOP_CLOSURE bool 
 GOP closure (default 1)
    
V4L2_CID_MPEG_VIDEO_PULLDOWN bool 
 Enable 3:2 pulldown (default 0)
    
V4L2_CID_MPEG_VIDEO_BITRATE_MODE enum 
 Video bitrate mode. Possible values are:
 
V4L2_MPEG_VIDEO_BITRATE_MODE_VBR Variable bitrate
V4L2_MPEG_VIDEO_BITRATE_MODE_CBR Constant bitrate
    
V4L2_CID_MPEG_VIDEO_BITRATE integer 
 Video bitrate in bits per second.
    
V4L2_CID_MPEG_VIDEO_BITRATE_PEAK integer 
 Peak video bitrate in bits per second. Must be larger or equal to the average video bitrate. It is ignored if the video bitrate mode is set to constant bitrate.
    
V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION integer 
 For every captured frame, skip this many subsequent frames (default 0).
    
V4L2_CID_MPEG_VIDEO_MUTE bool 
 "Mutes" the video to a fixed color when capturing. This is useful for testing, to produce a fixed video bitstream. 0 = unmuted, 1 = muted.
    
V4L2_CID_MPEG_VIDEO_MUTE_YUV integer 
 Sets the "mute" color of the video. The supplied 32-bit integer is interpreted as follows (bit 0 = least significant bit):
 
Bit 0:7V chrominance information
Bit 8:15U chrominance information
Bit 16:23Y luminance information
Bit 24:31Must be zero.

1.9.5.2. CX2341x MPEG Controls

The following MPEG class controls deal with MPEG encoding settings that are specific to the Conexant CX23415 and CX23416 MPEG encoding chips.

Table 1-3. CX2341x Control IDs

IDType 
 Description
    
V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE enum 
 Sets the Spatial Filter mode (default MANUAL). Possible values are:
 
V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_MANUAL Choose the filter manually
V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_AUTO Choose the filter automatically
    
V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER integer (0-15) 
 The setting for the Spatial Filter. 0 = off, 15 = maximum. (Default is 0.)
    
V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE enum 
 Select the algorithm to use for the Luma Spatial Filter (default 1D_HOR). Possible values:
 
V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_OFF No filter
V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_1D_HOR One-dimensional horizontal
V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_1D_VERT One-dimensional vertical
V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_2D_HV_SEPARABLE Two-dimensional separable
V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_2D_SYM_NON_SEPARABLE Two-dimensional symmetrical non-separable
    
V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE enum 
 Select the algorithm for the Chroma Spatial Filter (default 1D_HOR). Possible values are:
 
V4L2_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE_OFF No filter
V4L2_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE_1D_HOR One-dimensional horizontal
    
V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE enum 
 Sets the Temporal Filter mode (default MANUAL). Possible values are:
 
V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_MANUAL Choose the filter manually
V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_AUTO Choose the filter automatically
    
V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER integer (0-31) 
 The setting for the Temporal Filter. 0 = off, 31 = maximum. (Default is 8 for full-scale capturing and 0 for scaled capturing.)
    
V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE enum 
 Median Filter Type (default OFF). Possible values are:
 
V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF No filter
V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_HOR Horizontal filter
V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_VERT Vertical filter
V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_HOR_VERT Horizontal and vertical filter
V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_DIAG Diagonal filter
    
V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM integer (0-255) 
 Threshold above which the luminance median filter is enabled (default 0)
    
V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP integer (0-255) 
 Threshold below which the luminance median filter is enabled (default 255)
    
V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM integer (0-255) 
 Threshold above which the chroma median filter is enabled (default 0)
    
V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP integer (0-255) 
 Threshold below which the chroma median filter is enabled (default 255)
    
V4L2_CID_MPEG_CX2341X_STREAM_INSERT_NAV_PACKETS bool 
 The CX2341X MPEG encoder can insert one empty MPEG-2 PES packet into the stream between every four video frames. The packet size is 2048 bytes, including the packet_start_code_prefix and stream_id fields. The stream_id is 0xBF (private stream 2). The payload consists of 0x00 bytes, to be filled in by the application. 0 = do not insert, 1 = insert packets.

1.9.6. Camera Control Reference

The Camera class includes controls for mechanical (or equivalent digital) features of a device such as controllable lenses or sensors.

Table 1-4. Camera Control IDs

IDType 
 Description
    
V4L2_CID_CAMERA_CLASS class 
 The Camera class descriptor. Calling VIDIOC_QUERYCTRL for this control will return a description of this control class.
    
V4L2_CID_EXPOSURE_AUTO integer 
 Enables automatic adjustments of the exposure time and/or iris aperture. The effect of manual changes of the exposure time or iris aperture while these features are enabled is undefined, drivers should ignore such requests. Possible values are:
 
V4L2_EXPOSURE_AUTO Automatic exposure time, automatic iris aperture.
V4L2_EXPOSURE_MANUAL Manual exposure time, manual iris.
V4L2_EXPOSURE_SHUTTER_PRIORITY Manual exposure time, auto iris.
V4L2_EXPOSURE_APERTURE_PRIORITY Auto exposure time, manual iris.
    
V4L2_CID_EXPOSURE_ABSOLUTE integer 
 Determines the exposure time of the camera sensor. The exposure time is limited by the frame interval. Drivers should interpret the values as 100 µs units, where the value 1 stands for 1/10000th of a second, 10000 for 1 second and 100000 for 10 seconds.
    
V4L2_CID_EXPOSURE_AUTO_PRIORITY boolean 
 When V4L2_CID_EXPOSURE_AUTO is set to AUTO or SHUTTER_PRIORITY, this control determines if the device may dynamically vary the frame rate. By default this feature is disabled (0) and the frame rate must remain constant.
    
V4L2_CID_PAN_RELATIVE integer 
 This control turns the camera horizontally by the specified amount. The unit is undefined. A positive value moves the camera to the right (clockwise when viewed from above), a negative value to the left. A value of zero does not cause motion.
    
V4L2_CID_TILT_RELATIVE integer 
 This control turns the camera vertically by the specified amount. The unit is undefined. A positive value moves the camera up, a negative value down. A value of zero does not cause motion.
    
V4L2_CID_PAN_RESET boolean 
 When this control is set to TRUE (1), the camera moves horizontally to the default position.
    
V4L2_CID_TILT_RESET boolean 
 When this control is set to TRUE (1), the camera moves vertically to the default position.
    
V4L2_CID_PAN_ABSOLUTE integer 
 This control turns the camera horizontally to the specified position. Positive values move the camera to the right (clockwise when viewed from above), negative values to the left. Drivers should interpret the values as arc seconds, with valid values between -180 * 3600 and +180 * 3600 inclusive.
    
V4L2_CID_TILT_ABSOLUTE integer 
 This control turns the camera vertically to the specified position. Positive values move the camera up, negative values down. Drivers should interpret the values as arc seconds, with valid values between -180 * 3600 and +180 * 3600 inclusive.
    
V4L2_CID_FOCUS_ABSOLUTE integer 
 This control sets the focal point of the camera to the specified position. The unit is undefined. Positive values set the focus closer to the camera, negative values towards infinity.
    
V4L2_CID_FOCUS_RELATIVE integer 
 This control moves the focal point of the camera by the specified amount. The unit is undefined. Positive values move the focus closer to the camera, negative values towards infinity.
    
V4L2_CID_FOCUS_AUTO boolean 
 Enables automatic focus adjustments. The effect of manual focus adjustments while this feature is enabled is undefined, drivers should ignore such requests.
    

1.10. Data Formats

1.10.1. Data Format Negotiation

Different devices exchange different kinds of data with applications, for example video images, raw or sliced VBI data, RDS datagrams. Even within one kind many different formats are possible, in particular an abundance of image formats. Although drivers must provide a default and the selection persists across closing and reopening a device, applications should always negotiate a data format before engaging in data exchange. Negotiation means the application asks for a particular format and the driver selects and reports the best the hardware can do to satisfy the request. Of course applications can also just query the current selection.

A single mechanism exists to negotiate all data formats using the aggregate struct v4l2_format and the VIDIOC_G_FMT and VIDIOC_S_FMT ioctls. Additionally the VIDIOC_TRY_FMT ioctl can be used to examine what the hardware could do, without actually selecting a new data format. The data formats supported by the V4L2 API are covered in the respective device section in Chapter 4. For a closer look at image formats see Chapter 2.

The VIDIOC_S_FMT ioctl is a major turning-point in the initialization sequence. Prior to this point multiple panel applications can access the same device concurrently to select the current input, change controls or modify other properties. The first VIDIOC_S_FMT assigns a logical stream (video data, VBI data etc.) exclusively to one file descriptor.

Exclusive means no other application, more precisely no other file descriptor, can grab this stream or change device properties inconsistent with the negotiated parameters. A video standard change for example, when the new standard uses a different number of scan lines, can invalidate the selected image format. Therefore only the file descriptor owning the stream can make invalidating changes. Accordingly multiple file descriptors which grabbed different logical streams prevent each other from interfering with their settings. When for example video overlay is about to start or already in progress, simultaneous video capturing may be restricted to the same cropping and image size.

When applications omit the VIDIOC_S_FMT ioctl its locking side effects are implied by the next step, the selection of an I/O method with the VIDIOC_REQBUFS ioctl or implicit with the first read() or write() call.

Generally only one logical stream can be assigned to a file descriptor, the exception being drivers permitting simultaneous video capturing and overlay using the same file descriptor for compatibility with V4L and earlier versions of V4L2. Switching the logical stream or returning into "panel mode" is possible by closing and reopening the device. Drivers may support a switch using VIDIOC_S_FMT.

All drivers exchanging data with applications must support the VIDIOC_G_FMT and VIDIOC_S_FMT ioctl. Implementation of the VIDIOC_TRY_FMT is highly recommended but optional.


1.10.2. Image Format Enumeration

Apart of the generic format negotiation functions a special ioctl to enumerate all image formats supported by video capture, overlay or output devices is available.[11]

The VIDIOC_ENUM_FMT ioctl must be supported by all drivers exchanging image data with applications.

Important: Drivers are not supposed to convert image formats in kernel space. They must enumerate only formats directly supported by the hardware. If necessary driver writers should publish an example conversion routine or library for integration into applications.


1.11. Image Cropping, Insertion and Scaling

Some video capture devices can sample a subsection of the picture and shrink or enlarge it to an image of arbitrary size. We call these abilities cropping and scaling. Some video output devices can scale an image up or down and insert it at an arbitrary scan line and horizontal offset into a video signal.

Applications can use the following API to select an area in the video signal, query the default area and the hardware limits. Despite their name, the VIDIOC_CROPCAP, VIDIOC_G_CROP and VIDIOC_S_CROP ioctls apply to input as well as output devices.

Scaling requires a source and a target. On a video capture or overlay device the source is the video signal, and the cropping ioctls determine the area actually sampled. The target are images read by the application or overlaid onto the graphics screen. Their size (and position for an overlay) is negotiated with the VIDIOC_G_FMT and VIDIOC_S_FMT ioctls.

On a video output device the source are the images passed in by the application, and their size is again negotiated with the VIDIOC_G/S_FMT ioctls, or may be encoded in a compressed video stream. The target is the video signal, and the cropping ioctls determine the area where the images are inserted.

Source and target rectangles are defined even if the device does not support scaling or the VIDIOC_G/S_CROP ioctls. Their size (and position where applicable) will be fixed in this case. All capture and output device must support the VIDIOC_CROPCAP ioctl such that applications can determine if scaling takes place.


1.11.1. Cropping Structures

Figure 1-1. Image Cropping, Insertion and Scaling

For capture devices the coordinates of the top left corner, width and height of the area which can be sampled is given by the bounds substructure of the struct v4l2_cropcap returned by the VIDIOC_CROPCAP ioctl. To support a wide range of hardware this specification does not define an origin or units. However by convention drivers should horizontally count unscaled samples relative to 0H (the leading edge of the horizontal sync pulse, see Figure 4-1). Vertically ITU-R line numbers of the first field (Figure 4-2, Figure 4-3), multiplied by two if the driver can capture both fields.

The top left corner, width and height of the source rectangle, that is the area actually sampled, is given by struct v4l2_crop using the same coordinate system as struct v4l2_cropcap. Applications can use the VIDIOC_G_CROP and VIDIOC_S_CROP ioctls to get and set this rectangle. It must lie completely within the capture boundaries and the driver may further adjust the requested size and/or position according to hardware limitations.

Each capture device has a default source rectangle, given by the defrect substructure of struct v4l2_cropcap. The center of this rectangle shall align with the center of the active picture area of the video signal, and cover what the driver writer considers the complete picture. Drivers shall reset the source rectangle to the default when the driver is first loaded, but not later.

For output devices these structures and ioctls are used accordingly, defining the target rectangle where the images will be inserted into the video signal.


1.11.2. Scaling Adjustments

Video hardware can have various cropping, insertion and scaling limitations. It may only scale up or down, support only discrete scaling factors, or have different scaling abilities in horizontal and vertical direction. Also it may not support scaling at all. At the same time the struct v4l2_crop rectangle may have to be aligned, and both the source and target rectangles may have arbitrary upper and lower size limits. In particular the maximum width and height in struct v4l2_crop may be smaller than the struct v4l2_cropcap.bounds area. Therefore, as usual, drivers are expected to adjust the requested parameters and return the actual values selected.

Applications can change the source or the target rectangle first, as they may prefer a particular image size or a certain area in the video signal. If the driver has to adjust both to satisfy hardware limitations, the last requested rectangle shall take priority, and the driver should preferably adjust the opposite one. The VIDIOC_TRY_FMT ioctl however shall not change the driver state and therefore only adjust the requested rectangle.

Suppose scaling on a video capture device is restricted to a factor 1:1 or 2:1 in either direction and the target image size must be a multiple of 16 × 16 pixels. The source cropping rectangle is set to defaults, which are also the upper limit in this example, of 640 × 400 pixels at offset 0, 0. An application requests an image size of 300 × 225 pixels, assuming video will be scaled down from the "full picture" accordingly. The driver sets the image size to the closest possible values 304 × 224, then chooses the cropping rectangle closest to the requested size, that is 608 × 224 (224 × 2:1 would exceed the limit 400). The offset 0, 0 is still valid, thus unmodified. Given the default cropping rectangle reported by VIDIOC_CROPCAP the application can easily propose another offset to center the cropping rectangle.

Now the application may insist on covering an area using a picture aspect ratio closer to the original request, so it asks for a cropping rectangle of 608 × 456 pixels. The present scaling factors limit cropping to 640 × 384, so the driver returns the cropping size 608 × 384 and adjusts the image size to closest possible 304 × 192.


1.11.3. Examples

Source and target rectangles shall remain unchanged across closing and reopening a device, such that piping data into or out of a device will work without special preparations. More advanced applications should ensure the parameters are suitable before starting I/O.

Example 1-10. Resetting the cropping parameters

(A video capture device is assumed; change V4L2_BUF_TYPE_VIDEO_CAPTURE for other devices.)

struct v4l2_cropcap cropcap;
struct v4l2_crop crop;

memset (&cropcap, 0, sizeof (cropcap));
cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

if (-1 == ioctl (fd, VIDIOC_CROPCAP, &cropcap)) {
        perror ("VIDIOC_CROPCAP");
        exit (EXIT_FAILURE);
}

memset (&crop, 0, sizeof (crop));
crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
crop.c = cropcap.defrect; 

/* Ignore if cropping is not supported (EINVAL). */

if (-1 == ioctl (fd, VIDIOC_S_CROP, &crop)
    && errno != EINVAL) {
        perror ("VIDIOC_S_CROP");
        exit (EXIT_FAILURE);
}
      

Example 1-11. Simple downscaling

(A video capture device is assumed.)

struct v4l2_cropcap cropcap;
struct v4l2_format format;

reset_cropping_parameters ();

/* Scale down to 1/4 size of full picture. */

memset (&format, 0, sizeof (format)); /* defaults */

format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

format.fmt.pix.width = cropcap.defrect.width >> 1;
format.fmt.pix.height = cropcap.defrect.height >> 1;
format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;

if (-1 == ioctl (fd, VIDIOC_S_FMT, &format)) {
        perror ("VIDIOC_S_FORMAT");
        exit (EXIT_FAILURE);
}

/* We could check the actual image size now, the actual scaling factor
   or if the driver can scale at all. */
        

Example 1-12. Selecting an output area

struct v4l2_cropcap cropcap;
struct v4l2_crop crop;

memset (&cropcap, 0, sizeof (cropcap));
cropcap.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;

if (-1 == ioctl (fd, VIDIOC_CROPCAP, &cropcap)) {
        perror ("VIDIOC_CROPCAP");
        exit (EXIT_FAILURE);
}

memset (&crop, 0, sizeof (crop));

crop.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
crop.c = cropcap.defrect;

/* Scale the width and height to 50 % of their original size
   and center the output. */

crop.c.width /= 2;
crop.c.height /= 2;
crop.c.left += crop.c.width / 2;
crop.c.top += crop.c.height / 2;

/* Ignore if cropping is not supported (EINVAL). */

if (-1 == ioctl (fd, VIDIOC_S_CROP, &crop)
    && errno != EINVAL) {
        perror ("VIDIOC_S_CROP");
        exit (EXIT_FAILURE);
}

Example 1-13. Current scaling factor and pixel aspect

(A video capture device is assumed.)

struct v4l2_cropcap cropcap;
struct v4l2_crop crop;
struct v4l2_format format;
double hscale, vscale;
double aspect;
int dwidth, dheight;

memset (&cropcap, 0, sizeof (cropcap));
cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

if (-1 == ioctl (fd, VIDIOC_CROPCAP, &cropcap)) {
        perror ("VIDIOC_CROPCAP");
        exit (EXIT_FAILURE);
}

memset (&crop, 0, sizeof (crop));
crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

if (-1 == ioctl (fd, VIDIOC_G_CROP, &crop)) {
        if (errno != EINVAL) {
                perror ("VIDIOC_G_CROP");
                exit (EXIT_FAILURE);
        }

        /* Cropping not supported. */
        crop.c = cropcap.defrect;
}

memset (&format, 0, sizeof (format));
format.fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

if (-1 == ioctl (fd, VIDIOC_G_FMT, &format)) {
        perror ("VIDIOC_G_FMT");
        exit (EXIT_FAILURE);
}

/* The scaling applied by the driver. */

hscale = format.fmt.pix.width / (double) crop.c.width;
vscale = format.fmt.pix.height / (double) crop.c.height;

aspect = cropcap.pixelaspect.numerator /
         (double) cropcap.pixelaspect.denominator;
aspect = aspect * hscale / vscale;

/* Devices following ITU-R BT.601 do not capture
   square pixels. For playback on a computer monitor
   we should scale the images to this size. */

dwidth = format.fmt.pix.width / aspect;
dheight = format.fmt.pix.height;
        

1.12. Streaming Parameters

Streaming parameters are intended to optimize the video capture process as well as I/O. Presently applications can request a high quality capture mode with the VIDIOC_S_PARM ioctl.

The current video standard determines a nominal number of frames per second. If less than this number of frames is to be captured or output, applications can request frame skipping or duplicating on the driver side. This is especially useful when using the read() or write(), which are not augmented by timestamps or sequence counters, and to avoid unneccessary data copying.

Finally these ioctls can be used to determine the number of buffers used internally by a driver in read/write mode. For implications see the section discussing the read() function.

To get and set the streaming parameters applications call the VIDIOC_G_PARM and VIDIOC_S_PARM ioctl, respectively. They take a pointer to a struct v4l2_streamparm, which contains a union holding separate parameters for input and output devices.

These ioctls are optional, drivers need not implement them. If so, they return the EINVAL error code.


Chapter 2. Image Formats

The V4L2 API was primarily designed for devices exchanging image data with applications. The v4l2_pix_format structure defines the format and layout of an image in memory. Image formats are negotiated with the VIDIOC_S_FMT ioctl. (The explanations here focus on video capturing and output, for overlay frame buffer formats see also VIDIOC_G_FBUF.)

Table 2-1. struct v4l2_pix_format

__u32widthImage width in pixels.
__u32heightImage height in pixels.
Applications set these fields to request an image size, drivers return the closest possible values. In case of planar formats the width and height applies to the largest plane. To avoid ambiguities drivers must return values rounded up to a multiple of the scale factor of any smaller planes. For example when the image format is YUV 4:2:0, width and height must be multiples of two.
__u32pixelformatThe pixel format or type of compression, set by the application. This is a little endian four character code. V4L2 defines standard RGB formats in Table 2-1, YUV formats in Section 2.5, and reserved codes in Table 2-8
enum v4l2_fieldfieldVideo images are typically interlaced. Applications can request to capture or output only the top or bottom field, or both fields interlaced or sequentially stored in one buffer or alternating in separate buffers. Drivers return the actual field order selected. For details see Section 3.6.
__u32bytesperlineDistance in bytes between the leftmost pixels in two adjacent lines.

Both applications and drivers can set this field to request padding bytes at the end of each line. Drivers however may ignore the value requested by the application, returning width times bytes per pixel or a larger value required by the hardware. That implies applications can just set this field to zero to get a reasonable default.

Video hardware may access padding bytes, therefore they must reside in accessible memory. Consider cases where padding bytes after the last line of an image cross a system page boundary. Input devices may write padding bytes, the value is undefined. Output devices ignore the contents of padding bytes.

When the image format is planar the bytesperline value applies to the largest plane and is divided by the same factor as the width field for any smaller planes. For example the Cb and Cr planes of a YUV 4:2:0 image have half as many padding bytes following each line as the Y plane. To avoid ambiguities drivers must return a bytesperline value rounded up to a multiple of the scale factor.

__u32sizeimageSize in bytes of the buffer to hold a complete image, set by the driver. Usually this is bytesperline times height. When the image consists of variable length compressed data this is the maximum number of bytes required to hold an image.
enum v4l2_colorspacecolorspaceThis information supplements the pixelformat and must be set by the driver, see Section 2.2.
__u32privReserved for custom (driver defined) additional information about formats. When not used drivers and applications must set this field to zero.

2.1. Standard Image Formats

In order to exchange images between drivers and applications, it is necessary to have standard image data formats which both sides will interpret the same way. V4L2 includes several such formats, and this section is intended to be an unambiguous specification of the standard image data formats in V4L2.

V4L2 drivers are not limited to these formats, however. Driver-specific formats are possible. In that case the application may depend on a codec to convert images to one of the standard formats when needed. But the data can still be stored and retrieved in the proprietary format. For example, a device may support a proprietary compressed format. Applications can still capture and save the data in the compressed format, saving much disk space, and later use a codec to convert the images to the X Windows screen format when the video is to be displayed.

Even so, ultimately, some standard formats are needed, so the V4L2 specification would not be complete without well-defined standard formats.

The V4L2 standard formats are mainly uncompressed formats. The pixels are always arranged in memory from left to right, and from top to bottom. The first byte of data in the image buffer is always for the leftmost pixel of the topmost row. Following that is the pixel immediately to its right, and so on until the end of the top row of pixels. Following the rightmost pixel of the row there may be zero or more bytes of padding to guarantee that each row of pixel data has a certain alignment. Following the pad bytes, if any, is data for the leftmost pixel of the second row from the top, and so on. The last row has just as many pad bytes after it as the other rows.

In V4L2 each format has an identifier which looks like PIX_FMT_XXX, defined in the videodev.h header file. These identifiers represent four character codes which are also listed below, however they are not the same as those used in the Windows world.


2.2. Colorspaces

[intro]

Gamma Correction

[to do]

E'R = f(R)

E'G = f(G)

E'B = f(B)

Construction of luminance and color-difference signals

[to do]

E'Y = CoeffR E'R + CoeffG E'G + CoeffB E'B

(E'R - E'Y) = E'R - CoeffR E'R - CoeffG E'G - CoeffB E'B

(E'B - E'Y) = E'B - CoeffR E'R - CoeffG E'G - CoeffB E'B

Re-normalized color-difference signals

The color-difference signals are scaled back to unity range [-0.5;+0.5]:

KB = 0.5 / (1 - CoeffB)

KR = 0.5 / (1 - CoeffR)

PB = KB (E'B - E'Y) = 0.5 (CoeffR / CoeffB) E'R + 0.5 (CoeffG / CoeffB) E'G + 0.5 E'B

PR = KR (E'R - E'Y) = 0.5 E'R + 0.5 (CoeffG / CoeffR) E'G + 0.5 (CoeffB / CoeffR) E'B

Quantization

[to do]

Y' = (Lum. Levels - 1) · E'Y + Lum. Offset

CB = (Chrom. Levels - 1) · PB + Chrom. Offset

CR = (Chrom. Levels - 1) · PR + Chrom. Offset

Rounding to the nearest integer and clamping to the range [0;255] finally yields the digital color components Y'CbCr stored in YUV images.

Example 2-1. ITU-R Rec. BT.601 color conversion

Forward Transformation

int ER, EG, EB;         /* gamma corrected RGB input [0;255] */
int Y1, Cb, Cr;         /* output [0;255] */

double r, g, b;         /* temporaries */
double y1, pb, pr;

int
clamp (double x)
{
        int r = x;      /* round to nearest */

        if (r < 0)         return 0;
        else if (r > 255)  return 255;
        else               return r;
}

r = ER / 255.0;
g = EG / 255.0;
b = EB / 255.0;

y1  =  0.299  * r + 0.587 * g + 0.114  * b;
pb  = -0.169  * r - 0.331 * g + 0.5    * b;
pr  =  0.5    * r - 0.419 * g - 0.081  * b;

Y1 = clamp (219 * y1 + 16);
Cb = clamp (224 * pb + 128);
Cr = clamp (224 * pr + 128);

/* or shorter */

y1 = 0.299 * ER + 0.587 * EG + 0.114 * EB;

Y1 = clamp ( (219 / 255.0)                    *       y1  + 16);
Cb = clamp (((224 / 255.0) / (2 - 2 * 0.114)) * (EB - y1) + 128);
Cr = clamp (((224 / 255.0) / (2 - 2 * 0.299)) * (ER - y1) + 128);
      

Inverse Transformation

int Y1, Cb, Cr;         /* gamma pre-corrected input [0;255] */
int ER, EG, EB;         /* output [0;255] */

double r, g, b;         /* temporaries */
double y1, pb, pr;

int
clamp (double x)
{
        int r = x;      /* round to nearest */

        if (r < 0)         return 0;
        else if (r > 255)  return 255;
        else               return r;
}

y1 = (255 / 219.0) * (Y1 - 16);
pb = (255 / 224.0) * (Cb - 128);
pr = (255 / 224.0) * (Cr - 128);

r = 1.0 * y1 + 0     * pb + 1.402 * pr;
g = 1.0 * y1 - 0.344 * pb - 0.714 * pr;
b = 1.0 * y1 + 1.772 * pb + 0     * pr;

ER = clamp (r * 255); /* [ok? one should prob. limit y1,pb,pr] */
EG = clamp (g * 255);
EB = clamp (b * 255);
      

Table 2-2. enum v4l2_colorspace

IdentifierValueDescriptionChromaticities[a]White PointGamma CorrectionLuminance E'YQuantization
RedGreenBlueY'Cb, Cr
V4L2_COLORSPACE_SMPTE170M1NTSC/PAL according to SMPTE 170M, ITU BT.601x = 0.630, y = 0.340x = 0.310, y = 0.595x = 0.155, y = 0.070x = 0.3127, y = 0.3290, Illuminant D65E' = 4.5 I for I ≤0.018, 1.099 I0.45 - 0.099 for 0.018 < I0.299 E'R + 0.587 E'G + 0.114 E'B219 E'Y + 16224 PB,R + 128
V4L2_COLORSPACE_SMPTE240M21125-Line (US) HDTV, see SMPTE 240Mx = 0.630, y = 0.340x = 0.310, y = 0.595x = 0.155, y = 0.070x = 0.3127, y = 0.3290, Illuminant D65E' = 4 I for I ≤0.0228, 1.1115 I0.45 - 0.1115 for 0.0228 < I0.212 E'R + 0.701 E'G + 0.087 E'B219 E'Y + 16224 PB,R + 128
V4L2_COLORSPACE_REC7093HDTV and modern devices, see ITU BT.709x = 0.640, y = 0.330x = 0.300, y = 0.600x = 0.150, y = 0.060x = 0.3127, y = 0.3290, Illuminant D65E' = 4.5 I for I ≤0.018, 1.099 I0.45 - 0.099 for 0.018 < I0.2125 E'R + 0.7154 E'G + 0.0721 E'B219 E'Y + 16224 PB,R + 128
V4L2_COLORSPACE_BT8784Broken Bt878 extents[b], ITU BT.601?????0.299 E'R + 0.587 E'G + 0.114 E'B237 E'Y + 16224 PB,R + 128 (probably)
V4L2_COLORSPACE_470_SYSTEM_M5M/NTSC[c] according to ITU BT.470, ITU BT.601x = 0.67, y = 0.33x = 0.21, y = 0.71x = 0.14, y = 0.08x = 0.310, y = 0.316, Illuminant C?0.299 E'R + 0.587 E'G + 0.114 E'B219 E'Y + 16224 PB,R + 128
V4L2_COLORSPACE_470_SYSTEM_BG6625-line PAL and SECAM systems according to ITU BT.470, ITU BT.601x = 0.64, y = 0.33x = 0.29, y = 0.60x = 0.15, y = 0.06x = 0.313, y = 0.329, Illuminant D65?0.299 E'R + 0.587 E'G + 0.114 E'B219 E'Y + 16224 PB,R + 128
V4L2_COLORSPACE_JPEG7JPEG Y'CbCr, see JFIF, ITU BT.601?????0.299 E'R + 0.587 E'G + 0.114 E'B256 E'Y + 16[d]256 PB,R + 128
V4L2_COLORSPACE_SRGB8[?]x = 0.640, y = 0.330x = 0.300, y = 0.600x = 0.150, y = 0.060x = 0.3127, y = 0.3290, Illuminant D65E' = 4.5 I for I ≤0.018, 1.099 I0.45 - 0.099 for 0.018 < In/a
Notes:
a. The coordinates of the color primaries are given in the CIE system (1931)
b. The ubiquitous Bt878 video capture chip quantizes E'Y to 238 levels, yielding a range of Y' = 16 … 253, unlike Rec. 601 Y' = 16 … 235. This is not a typo in the Bt878 documentation, it has been implemented in silicon. The chroma extents are unclear.
c. No identifier exists for M/PAL which uses the chromaticities of M/NTSC, the remaining parameters are equal to B and G/PAL.
d. Note JFIF quantizes Y'PBPR in range [0;+1] and [-0.5;+0.5] to 257 levels, however Y'CbCr signals are still clamped to [0;255].

2.3. Indexed Format

In this format each pixel is represented by an 8 bit index into a 256 entry ARGB palette. It is intended for Video Output Overlays only. There are no ioctls to access the palette, this must be done with ioctls of the Linux framebuffer API.

Table 2-3. Indexed Image Format

IdentifierCode Byte 0                          
  Bit76543210                          
V4L2_PIX_FMT_PAL8'PAL8' i7i6i5i4i3i2i1i0                          

2.4. RGB Formats

Table of Contents
Packed RGB formats -- Packed RGB formats
V4L2_PIX_FMT_SBGGR8 ('BA81') -- Bayer RGB format
V4L2_PIX_FMT_SBGGR16 ('BA82') -- Bayer RGB format

Packed RGB formats

Name

Packed RGB formats -- Packed RGB formats

Description

These formats are designed to match the pixel formats of typical PC graphics frame buffers. They occupy 8, 16, 24 or 32 bits per pixel. These are all packed-pixel formats, meaning all the data for a pixel lie next to each other in memory.

When one of these formats is used, drivers shall report the colorspace V4L2_COLORSPACE_SRGB.

Table 2-1. Packed RGB Image Formats

IdentifierCode Byte 0 in memory Byte 1 Byte 2 Byte 3
  Bit76543210 76543210 76543210 76543210
V4L2_PIX_FMT_RGB332'RGB1' b1b0g2g1g0r2r1r0                          
V4L2_PIX_FMT_RGB444'R444' g3g2g1g0b3b2b1b0 a3a2a1a0r3r2r1r0                 
V4L2_PIX_FMT_RGB555'RGBO' g2g1g0r4r3r2r1r0 ab4b3b2b1b0g4g3                 
V4L2_PIX_FMT_RGB565'RGBP' g2g1g0r4r3r2r1r0 b4b3b2b1b0g5g4g3                 
V4L2_PIX_FMT_RGB555X'RGBQ' ab4b3b2b1b0g4g3 g2g1g0r4r3r2r1r0                 
V4L2_PIX_FMT_RGB565X'RGBR' b4b3b2b1b0g5g4g3 g2g1g0r4r3r2r1r0                 
V4L2_PIX_FMT_BGR24'BGR3' b7b6b5b4b3b2b1b0 g7g6g5g4g3g2g1g0 r7r6r5r4r3r2r1r0        
V4L2_PIX_FMT_RGB24'RGB3' r7r6r5r4r3r2r1r0 g7g6g5g4g3g2g1g0 b7b6b5b4b3b2b1b0        
V4L2_PIX_FMT_BGR32'BGR4' b7b6b5b4b3b2b1b0 g7g6g5g4g3g2g1g0 r7r6r5r4r3r2r1r0 a7a6a5a4a3a2a1a0
V4L2_PIX_FMT_RGB32'RGB4' r7r6r5r4r3r2r1r0 g7g6g5g4g3g2g1g0 b7b6b5b4b3b2b1b0 a7a6a5a4a3a2a1a0

Bit 7 is the most significant bit. The value of a = alpha bits is undefined when reading from the driver, ignored when writing to the driver, except when alpha blending has been negotiated for a Video Overlay or Video Output Overlay.

Example 2-1. V4L2_PIX_FMT_BGR24 4 × 4 pixel image

Byte Order. Each cell is one byte.

start + 0:B00G00R00B01G01R01B02G02R02B03G03R03
start + 12:B10G10R10B11G11R11B12G12R12B13G13R13
start + 24:B20G20R20B21G21R21B22G22R22B23G23R23
start + 36:B30G30R30B31G31R31B32G32R32B33G33R33

Important: Drivers may interpret these formats differently.

Some RGB formats above are uncommon and were probably defined in error. Drivers may interpret them as in Table 2-2.

Table 2-2. Packed RGB Image Formats (corrected)

IdentifierCode Byte 0 in memory Byte 1 Byte 2 Byte 3
  Bit76543210 76543210 76543210 76543210
V4L2_PIX_FMT_RGB332'RGB1' r2r1r0g2g1g0b1b0                          
V4L2_PIX_FMT_RGB444'R444' g3g2g1g0b3b2b1b0 a3a2a1a0r3r2r1r0                 
V4L2_PIX_FMT_RGB555'RGBO' g2g1g0b4b3b2b1b0 ar4r3r2r1r0g4g3                 
V4L2_PIX_FMT_RGB565'RGBP' g2g1g0b4b3b2b1b0 r4r3r2r1r0g5g4g3                 
V4L2_PIX_FMT_RGB555X'RGBQ' ar4r3r2r1r0g4g3 g2g1g0b4b3b2b1b0                 
V4L2_PIX_FMT_RGB565X'RGBR' r4r3r2r1r0g5g4g3 g2g1g0b4b3b2b1b0                 
V4L2_PIX_FMT_BGR24'BGR3' b7b6b5b4b3b2b1b0 g7g6g5g4g3g2g1g0 r7r6r5r4r3r2r1r0        
V4L2_PIX_FMT_RGB24'RGB3' r7r6r5r4r3r2r1r0 g7g6g5g4g3g2g1g0 b7b6b5b4b3b2b1b0        
V4L2_PIX_FMT_BGR32'BGR4' b7b6b5b4b3b2b1b0 g7g6g5g4g3g2g1g0 r7r6r5r4r3r2r1r0 a7a6a5a4a3a2a1a0
V4L2_PIX_FMT_RGB32'RGB4' a7a6a5a4a3a2a1a0 r7r6r5r4r3r2r1r0 g7g6g5g4g3g2g1g0 b7b6b5b4b3b2b1b0

A test utility to determine which RGB formats a driver actually supports is available from the LinuxTV v4l-dvb repository. See http://linuxtv.org/repo/ for access instructions.

V4L2_PIX_FMT_SBGGR8 ('BA81')

Name

V4L2_PIX_FMT_SBGGR8 -- Bayer RGB format

Description

This is commonly the native format of digital cameras, reflecting the arrangement of sensors on the CCD device. Only one red, green or blue value is given for each pixel. Missing components must be interpolated from neighbouring pixels. From left to right the first row consists of a blue and green value, the second row of a green and red value. This scheme repeats to the right and down for every two columns and rows.

Example 2-1. V4L2_PIX_FMT_SBGGR8 4 × 4 pixel image

Byte Order. Each cell is one byte.

start + 0:B00G01B02G03
start + 4:G10R11G12R13
start + 8:B20G21B22G23
start + 12:G30R31G32R33

V4L2_PIX_FMT_SBGGR16 ('BA82')

Name

V4L2_PIX_FMT_SBGGR16 -- Bayer RGB format

Description

This format is similar to V4L2_PIX_FMT_SBGGR8, except each pixel has a depth of 16 bits. The least significant byte is stored at lower memory addresses (little-endian). Note the actual sampling precision may be lower than 16 bits, for example 10 bits per pixel with values in range 0 to 1023.

Example 2-1. V4L2_PIX_FMT_SBGGR16 4 × 4 pixel image

Byte Order. Each cell is one byte.

start + 0:B00lowB00highG01lowG01highB02lowB02highG03lowG03high
start + 8:G10lowG10highR11lowR11highG12lowG12highR13lowR13high
start + 16:B20lowB20highG21lowG21highB22lowB22highG23lowG23high
start + 24:G30lowG30highR31lowR31highG32lowG32highR33lowR33high


2.5. YUV Formats

Table of Contents
Packed YUV formats -- Packed YUV formats
V4L2_PIX_FMT_GREY ('GREY') -- Grey-scale image
V4L2_PIX_FMT_Y16 ('Y16 ') -- Grey-scale image
V4L2_PIX_FMT_YUYV ('YUYV') -- Packed format with ½ horizontal chroma resolution, also known as YUV 4:2:2
V4L2_PIX_FMT_UYVY ('UYVY') -- Variation of V4L2_PIX_FMT_YUYV with different order of samples in memory
V4L2_PIX_FMT_Y41P ('Y41P') -- Format with ¼ horizontal chroma resolution, also known as YUV 4:1:1
V4L2_PIX_FMT_YVU420 ('YV12'), V4L2_PIX_FMT_YUV420 ('YU12') -- Planar formats with ½ horizontal and vertical chroma resolution, also known as YUV 4:2:0
V4L2_PIX_FMT_YVU410 ('YVU9'), V4L2_PIX_FMT_YUV410 ('YUV9') -- Planar formats with ¼ horizontal and vertical chroma resolution, also known as YUV 4:1:0
V4L2_PIX_FMT_YUV422P ('422P') -- Format with ½ horizontal chroma resolution, also known as YUV 4:2:2. Planar layout as opposed to V4L2_PIX_FMT_YUYV
V4L2_PIX_FMT_YUV411P ('411P') -- Format with ¼ horizontal chroma resolution, also known as YUV 4:1:1. Planar layout as opposed to V4L2_PIX_FMT_Y41P
V4L2_PIX_FMT_NV12 ('NV12'), V4L2_PIX_FMT_NV21 ('NV21') -- Formats with ½ horizontal and vertical chroma resolution, also known as YUV 4:2:0. One luminance and one chrominance plane with alternating chroma samples as opposed to V4L2_PIX_FMT_YVU420

YUV is the format native to TV broadcast and composite video signals. It separates the brightness information (Y) from the color information (U and V or Cb and Cr). The color information consists of red and blue color difference signals, this way the green component can be reconstructed by subtracting from the brightness component. See Section 2.2 for conversion examples. YUV was chosen because early television would only transmit brightness information. To add color in a way compatible with existing receivers a new signal carrier was added to transmit the color difference signals. Secondary in the YUV format the U and V components usually have lower resolution than the Y component. This is an analog video compression technique taking advantage of a property of the human visual system, being more sensitive to brightness information.

Packed YUV formats

Name

Packed YUV formats -- Packed YUV formats

Description

Similar to the packed RGB formats these formats store the Y, Cb and Cr component of each pixel in one 16 or 32 bit word.

Table 2-1. Packed YUV Image Formats

IdentifierCode Byte 0 in memory Byte 1 Byte 2 Byte 3
  Bit76543210 76543210 76543210 76543210
V4L2_PIX_FMT_YUV444'Y444' Cb3Cb2Cb1Cb0Cr3Cr2Cr1Cr0 a3a2a1a0Y'3Y'2Y'1Y'0                 
V4L2_PIX_FMT_YUV555'YUVO' Cb2Cb1Cb0Cr4Cr3Cr2Cr1Cr0 aY'4Y'3Y'2Y'1Y'0Cb4Cb3                 
V4L2_PIX_FMT_YUV565'YUVP' Cb2Cb1Cb0Cr4Cr3Cr2Cr1Cr0 Y'4Y'3Y'2Y'1Y'0Cb5Cb4Cb3                 
V4L2_PIX_FMT_YUV32'YUV4' a7a6a5a4a3a2a1a0 Y'7Y'6Y'5Y'4Y'3Y'2Y'1Y'0 Cb7Cb6Cb5Cb4Cb3Cb2Cb1Cb0 Cr7Cr6Cr5Cr4Cr3Cr2Cr1Cr0

Bit 7 is the most significant bit. The value of a = alpha bits is undefined when reading from the driver, ignored when writing to the driver, except when alpha blending has been negotiated for a Video Overlay or Video Output Overlay.

V4L2_PIX_FMT_GREY ('GREY')

Name

V4L2_PIX_FMT_GREY -- Grey-scale image

Description

This is a grey-scale image. It is really a degenerate Y'CbCr format which simply contains no Cb or Cr data.

Example 2-1. V4L2_PIX_FMT_GREY 4 × 4 pixel image

Byte Order. Each cell is one byte.

start + 0:Y'00Y'01Y'02Y'03
start + 4:Y'10Y'11Y'12Y'13
start + 8:Y'20Y'21Y'22Y'23
start + 12:Y'30Y'31Y'32Y'33

V4L2_PIX_FMT_Y16 ('Y16 ')

Name

V4L2_PIX_FMT_Y16 -- Grey-scale image

Description

This is a grey-scale image with a depth of 16 bits per pixel. The least significant byte is stored at lower memory addresses (little-endian). Note the actual sampling precision may be lower than 16 bits, for example 10 bits per pixel with values in range 0 to 1023.

Example 2-1. V4L2_PIX_FMT_Y16 4 × 4 pixel image

Byte Order. Each cell is one byte.

start + 0:Y'00lowY'00highY'01lowY'01highY'02lowY'02highY'03lowY'03high
start + 8:Y'10lowY'10highY'11lowY'11highY'12lowY'12highY'13lowY'13high
start + 16:Y'20lowY'20highY'21lowY'21highY'22lowY'22highY'23lowY'23high
start + 24:Y'30lowY'30highY'31lowY'31highY'32lowY'32highY'33lowY'33high

V4L2_PIX_FMT_YUYV ('YUYV')

Name

V4L2_PIX_FMT_YUYV -- Packed format with ½ horizontal chroma resolution, also known as YUV 4:2:2

Description

In this format each four bytes is two pixels. Each four bytes is two Y's, a Cb and a Cr. Each Y goes to one of the pixels, and the Cb and Cr belong to both pixels. As you can see, the Cr and Cb components have half the horizontal resolution of the Y component. V4L2_PIX_FMT_YUYV is known in the Windows environment as YUY2.

Example 2-1. V4L2_PIX_FMT_YUYV 4 × 4 pixel image

Byte Order. Each cell is one byte.

start + 0:Y'00Cb00Y'01Cr00Y'02Cb01Y'03Cr01
start + 8:Y'10Cb10Y'11Cr10Y'12Cb11Y'13Cr11
start + 16:Y'20Cb20Y'21Cr20Y'22Cb21Y'23Cr21
start + 24:Y'30Cb30Y'31Cr30Y'32Cb31Y'33Cr31

Color Sample Location.

 0 1 2 3
0YCY YCY
1YCY YCY
2YCY YCY
3YCY YCY

V4L2_PIX_FMT_UYVY ('UYVY')

Name

V4L2_PIX_FMT_UYVY -- Variation of V4L2_PIX_FMT_YUYV with different order of samples in memory

Description

In this format each four bytes is two pixels. Each four bytes is two Y's, a Cb and a Cr. Each Y goes to one of the pixels, and the Cb and Cr belong to both pixels. As you can see, the Cr and Cb components have half the horizontal resolution of the Y component.

Example 2-1. V4L2_PIX_FMT_UYVY 4 × 4 pixel image

Byte Order. Each cell is one byte.

start + 0:Cb00Y'00Cr00Y'01Cb01Y'02Cr01Y'03
start + 8:Cb10Y'10Cr10Y'11Cb11Y'12Cr11Y'13
start + 16:Cb20Y'20Cr20Y'21Cb21Y'22Cr21Y'23
start + 24:Cb30Y'30Cr30Y'31Cb31Y'32Cr31Y'33

Color Sample Location.

 0 1 2 3
0YCY YCY
1YCY YCY
2YCY YCY
3YCY YCY

V4L2_PIX_FMT_Y41P ('Y41P')

Name

V4L2_PIX_FMT_Y41P -- Format with ¼ horizontal chroma resolution, also known as YUV 4:1:1

Description

In this format each 12 bytes is eight pixels. In the twelve bytes are two CbCr pairs and eight Y's. The first CbCr pair goes with the first four Y's, and the second CbCr pair goes with the other four Y's. The Cb and Cr components have one fourth the horizontal resolution of the Y component.

Do not confuse this format with V4L2_PIX_FMT_YUV411P. Y41P is derived from "YUV 4:1:1 packed", while YUV411P stands for "YUV 4:1:1 planar".

Example 2-1. V4L2_PIX_FMT_Y41P 8 × 4 pixel image

Byte Order. Each cell is one byte.

start + 0:Cb00Y'00Cr00Y'01Cb01Y'02Cr01Y'03Y'04Y'05Y'06Y'07
start + 12:Cb10Y'10Cr10Y'11Cb11Y'12Cr11Y'13Y'14Y'15Y'16Y'17
start + 24:Cb20Y'20Cr20Y'21Cb21Y'22Cr21Y'23Y'24Y'25Y'26Y'27
start + 36:Cb30Y'30Cr30Y'31Cb31Y'32Cr31Y'33Y'34Y'35Y'36Y'37

Color Sample Location.

 0 1 2 3 4 5 6 7
0Y YCY Y Y YCY Y
1Y YCY Y Y YCY Y
2Y YCY Y Y YCY Y
3Y YCY Y Y YCY Y

V4L2_PIX_FMT_YVU420 ('YV12'), V4L2_PIX_FMT_YUV420 ('YU12')

Name

V4L2_PIX_FMT_YVU420, V4L2_PIX_FMT_YUV420 -- Planar formats with ½ horizontal and vertical chroma resolution, also known as YUV 4:2:0

Description

These are planar formats, as opposed to a packed format. The three components are separated into three sub- images or planes. The Y plane is first. The Y plane has one byte per pixel. For V4L2_PIX_FMT_YVU420, the Cr plane immediately follows the Y plane in memory. The Cr plane is half the width and half the height of the Y plane (and of the image). Each Cr belongs to four pixels, a two-by-two square of the image. For example, Cr0 belongs to Y'00, Y'01, Y'10, and Y'11. Following the Cr plane is the Cb plane, just like the Cr plane. V4L2_PIX_FMT_YUV420 is the same except the Cb plane comes first, then the Cr plane.

If the Y plane has pad bytes after each row, then the Cr and Cb planes have half as many pad bytes after their rows. In other words, two Cx rows (including padding) is exactly as long as one Y row (including padding).

Example 2-1. V4L2_PIX_FMT_YVU420 4 × 4 pixel image

Byte Order. Each cell is one byte.

start + 0:Y'00Y'01Y'02Y'03
start + 4:Y'10Y'11Y'12Y'13
start + 8:Y'20Y'21Y'22Y'23
start + 12:Y'30Y'31Y'32Y'33
start + 16:Cr00Cr01  
start + 18:Cr10Cr11  
start + 20:Cb00Cb01  
start + 22:Cb10Cb11  

Color Sample Location.

 0 1 2 3
0Y Y Y Y
  C   C 
1Y Y Y Y
       
2Y Y Y Y
  C   C 
3Y Y Y Y

V4L2_PIX_FMT_YVU410 ('YVU9'), V4L2_PIX_FMT_YUV410 ('YUV9')

Name

V4L2_PIX_FMT_YVU410, V4L2_PIX_FMT_YUV410 -- Planar formats with ¼ horizontal and vertical chroma resolution, also known as YUV 4:1:0

Description

These are planar formats, as opposed to a packed format. The three components are separated into three sub-images or planes. The Y plane is first. The Y plane has one byte per pixel. For V4L2_PIX_FMT_YVU410, the Cr plane immediately follows the Y plane in memory. The Cr plane is ¼ the width and ¼ the height of the Y plane (and of the image). Each Cr belongs to 16 pixels, a four-by-four square of the image. Following the Cr plane is the Cb plane, just like the Cr plane. V4L2_PIX_FMT_YUV410 is the same, except the Cb plane comes first, then the Cr plane.

If the Y plane has pad bytes after each row, then the Cr and Cb planes have ¼ as many pad bytes after their rows. In other words, four Cx rows (including padding) are exactly as long as one Y row (including padding).

Example 2-1. V4L2_PIX_FMT_YVU410 4 × 4 pixel image

Byte Order. Each cell is one byte.

start + 0:Y'00Y'01Y'02Y'03
start + 4:Y'10Y'11Y'12Y'13
start + 8:Y'20Y'21Y'22Y'23
start + 12:Y'30Y'31Y'32Y'33
start + 16:Cr00   
start + 17:Cb00   

Color Sample Location.

 0 1 2 3
0Y Y Y Y
       
1Y Y Y Y
    C   
2Y Y Y Y
       
3Y Y Y Y

V4L2_PIX_FMT_YUV422P ('422P')

Name

V4L2_PIX_FMT_YUV422P -- Format with ½ horizontal chroma resolution, also known as YUV 4:2:2. Planar layout as opposed to V4L2_PIX_FMT_YUYV

Description

This format is not commonly used. This is a planar version of the YUYV format. The three components are separated into three sub-images or planes. The Y plane is first. The Y plane has one byte per pixel. The Cb plane immediately follows the Y plane in memory. The Cb plane is half the width of the Y plane (and of the image). Each Cb belongs to two pixels. For example, Cb0 belongs to Y'00, Y'01. Following the Cb plane is the Cr plane, just like the Cb plane.

If the Y plane has pad bytes after each row, then the Cr and Cb planes have half as many pad bytes after their rows. In other words, two Cx rows (including padding) is exactly as long as one Y row (including padding).

Example 2-1. V4L2_PIX_FMT_YUV422P 4 × 4 pixel image

Byte Order. Each cell is one byte.

start + 0:Y'00Y'01Y'02Y'03
start + 4:Y'10Y'11Y'12Y'13
start + 8:Y'20Y'21Y'22Y'23
start + 12:Y'30Y'31Y'32Y'33
start + 16:Cb00Cb01  
start + 18:Cb10Cb11  
start + 20:Cb20Cb21  
start + 22:Cb30Cb31  
start + 24:Cr00Cr01  
start + 26:Cr10Cr11  
start + 28:Cr20Cr21  
start + 30:Cr30Cr31  

Color Sample Location.

 0 1 2 3
0YCY YCY
1YCY YCY
2YCY YCY
3YCY YCY

V4L2_PIX_FMT_YUV411P ('411P')

Name

V4L2_PIX_FMT_YUV411P -- Format with ¼ horizontal chroma resolution, also known as YUV 4:1:1. Planar layout as opposed to V4L2_PIX_FMT_Y41P

Description

This format is not commonly used. This is a planar format similar to the 4:2:2 planar format except with half as many chroma. The three components are separated into three sub-images or planes. The Y plane is first. The Y plane has one byte per pixel. The Cb plane immediately follows the Y plane in memory. The Cb plane is ¼ the width of the Y plane (and of the image). Each Cb belongs to 4 pixels all on the same row. For example, Cb0 belongs to Y'00, Y'01, Y'02 and Y'03. Following the Cb plane is the Cr plane, just like the Cb plane.

If the Y plane has pad bytes after each row, then the Cr and Cb planes have ¼ as many pad bytes after their rows. In other words, four C x rows (including padding) is exactly as long as one Y row (including padding).

Example 2-1. V4L2_PIX_FMT_YUV411P 4 × 4 pixel image

Byte Order. Each cell is one byte.

start + 0:Y'00Y'01Y'02Y'03
start + 4:Y'10Y'11Y'12Y'13
start + 8:Y'20Y'21Y'22Y'23
start + 12:Y'30Y'31Y'32Y'33
start + 16:Cb00   
start + 17:Cb10   
start + 18:Cb20   
start + 19:Cb30   
start + 20:Cr00   
start + 21:Cr10   
start + 22:Cr20   
start + 23:Cr30   

Color Sample Location.

 0 1 2 3
0Y YCY Y
1Y YCY Y
2Y YCY Y
3Y YCY Y

V4L2_PIX_FMT_NV12 ('NV12'), V4L2_PIX_FMT_NV21 ('NV21')

Name

V4L2_PIX_FMT_NV12, V4L2_PIX_FMT_NV21 -- Formats with ½ horizontal and vertical chroma resolution, also known as YUV 4:2:0. One luminance and one chrominance plane with alternating chroma samples as opposed to V4L2_PIX_FMT_YVU420

Description

These are two-plane versions of the YUV 4:2:0 format. The three components are separated into two sub-images or planes. The Y plane is first. The Y plane has one byte per pixel. For V4L2_PIX_FMT_NV12, a combined CbCr plane immediately follows the Y plane in memory. The CbCr plane is the same width, in bytes, as the Y plane (and of the image), but is half as tall in pixels. Each CbCr pair belongs to four pixels. For example, Cb0/Cr0 belongs to Y'00, Y'01, Y'10, Y'11. V4L2_PIX_FMT_NV21 is the same except the Cb and Cr bytes are swapped, the CrCb plane starts with a Cr byte.

If the Y plane has pad bytes after each row, then the CbCr plane has as many pad bytes after its rows.

Example 2-1. V4L2_PIX_FMT_NV12 4 × 4 pixel image

Byte Order. Each cell is one byte.

start + 0:Y'00Y'01Y'02Y'03
start + 4:Y'10Y'11Y'12Y'13
start + 8:Y'20Y'21Y'22Y'23
start + 12:Y'30Y'31Y'32Y'33
start + 16:Cb00Cr00Cb01Cr01
start + 20:Cb10Cr10Cb11Cr11

Color Sample Location.

 0 1 2 3
0Y Y Y Y
  C   C 
1Y Y Y Y
       
2Y Y Y Y
  C   C 
3Y Y Y Y


2.6. Compressed Formats

Table 2-7. Compressed Image Formats

IdentifierCodeDetails
V4L2_PIX_FMT_JPEG'JPEG'TBD. See also VIDIOC_G_JPEGCOMP, VIDIOC_S_JPEGCOMP.
V4L2_PIX_FMT_MPEG'MPEG'MPEG stream. The actual format is determined by extended control V4L2_CID_MPEG_STREAM_TYPE, see Table 1-2.

2.7. Reserved Format Identifiers

These formats are not defined by this specification, they are just listed for reference and to avoid naming conflicts. If you want to register your own format, send an e-mail to the V4L mailing list https://listman.redhat.com/mailman/listinfo/video4linux-list for inclusion in the videodev.h file. If you want to share your format with other developers add a link to your documentation and send a copy to the maintainer of this document, Michael Schimek , for inclusion in this section. If you think your format should be listed in a standard format section please make a proposal on the V4L mailing list.

Table 2-8. Reserved Image Formats

IdentifierCodeDetails
V4L2_PIX_FMT_DV'dvsd'unknown
V4L2_PIX_FMT_ET61X251'E625'Compressed format of the ET61X251 driver.
V4L2_PIX_FMT_HI240'HI24'

8 bit RGB format used by the BTTV driver, http://bytesex.org/bttv/

V4L2_PIX_FMT_HM12'HM12'

YUV 4:2:0 format used by the IVTV driver, http://www.ivtvdriver.org/

The format is documented in the kernel sources in the file Documentation/video4linux/cx2341x/README.hm12

V4L2_PIX_FMT_MJPEG'MJPG'Compressed format used by the Zoran driver
V4L2_PIX_FMT_PWC1'PWC1'Compressed format of the PWC driver.
V4L2_PIX_FMT_PWC2'PWC2'Compressed format of the PWC driver.
V4L2_PIX_FMT_SN9C10X'S910'Compressed format of the SN9C102 driver.
V4L2_PIX_FMT_WNVA'WNVA'

Used by the Winnov Videum driver, http://www.thedirks.org/winnov/

V4L2_PIX_FMT_YYUV'YYUV'unknown

Chapter 3. Input/Output

The V4L2 API defines several different methods to read from or write to a device. All drivers exchanging data with applications must support at least one of them.

The classic I/O method using the read() and write() function is automatically selected after opening a V4L2 device. When the driver does not support this method attempts to read or write will fail at any time.

Other methods must be negotiated. To select the streaming I/O method with memory mapped or user buffers applications call the VIDIOC_REQBUFS ioctl. The asynchronous I/O method is not defined yet.

Video overlay can be considered another I/O method, although the application does not directly receive the image data. It is selected by initiating video overlay with the VIDIOC_S_FMT ioctl. For more information see Section 4.2.

Generally exactly one I/O method, including overlay, is associated with each file descriptor. The only exceptions are applications not exchanging data with a driver ("panel applications", see Section 1.1) and drivers permitting simultaneous video capturing and overlay using the same file descriptor, for compatibility with V4L and earlier versions of V4L2.

VIDIOC_S_FMT and VIDIOC_REQBUFS would permit this to some degree, but for simplicity drivers need not support switching the I/O method (after first switching away from read/write) other than by closing and reopening the device.

The following sections describe the various I/O methods in more detail.


3.1. Read/Write

Input and output devices support the read() and write() function, respectively, when the V4L2_CAP_READWRITE flag in the capabilities field of struct v4l2_capability returned by the VIDIOC_QUERYCAP ioctl is set.

Drivers may need the CPU to copy the data, but they may also support DMA to or from user memory, so this I/O method is not necessarily less efficient than other methods merely exchanging buffer pointers. It is considered inferior though because no meta-information like frame counters or timestamps are passed. This information is necessary to recognize frame dropping and to synchronize with other data streams. However this is also the simplest I/O method, requiring little or no setup to exchange data. It permits command line stunts like this (the vidctrl tool is fictitious):

> vidctrl /dev/video --input=0 --format=YUYV --size=352x288
> dd if=/dev/video of=myimage.422 bs=202752 count=1

To read from the device applications use the read() function, to write the write() function. Drivers must implement one I/O method if they exchange data with applications, but it need not be this.[12] When reading or writing is supported, the driver must also support the select() and poll() function.[13]


3.2. Streaming I/O (Memory Mapping)

Input and output devices support this I/O method when the V4L2_CAP_STREAMING flag in the capabilities field of struct v4l2_capability returned by the VIDIOC_QUERYCAP ioctl is set. There are two streaming methods, to determine if the memory mapping flavor is supported applications must call the VIDIOC_REQBUFS ioctl.

Streaming is an I/O method where only pointers to buffers are exchanged between application and driver, the data itself is not copied. Memory mapping is primarily intended to map buffers in device memory into the application's address space. Device memory can be for example the video memory on a graphics card with a video capture add-on. However, being the most efficient I/O method available for a long time, many other drivers support streaming as well, allocating buffers in DMA-able main memory.

A driver can support many sets of buffers. Each set is identified by a unique buffer type value. The sets are independent and each set can hold a different type of data. To access different sets at the same time different file descriptors must be used.[14]

To allocate device buffers applications call the VIDIOC_REQBUFS ioctl with the desired number of buffers and buffer type, for example V4L2_BUF_TYPE_VIDEO_CAPTURE. This ioctl can also be used to change the number of buffers or to free the allocated memory, provided none of the buffers are still mapped.

Before applications can access the buffers they must map them into their address space with the mmap() function. The location of the buffers in device memory can be determined with the VIDIOC_QUERYBUF ioctl. The m.offset and length returned in a struct v4l2_buffer are passed as sixth and second parameter to the mmap() function. The offset and length values must not be modified. Remember the buffers are allocated in physical memory, as opposed to virtual memory which can be swapped out to disk. Applications should free the buffers as soon as possible with the munmap() function.

Example 3-1. Mapping buffers

struct v4l2_requestbuffers reqbuf;
struct {
        void *start;
        size_t length;
} *buffers;
unsigned int i;

memset (&reqbuf, 0, sizeof (reqbuf));
reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
reqbuf.memory = V4L2_MEMORY_MMAP;
reqbuf.count = 20;

if (-1 == ioctl (fd, VIDIOC_REQBUFS, &reqbuf)) {
        if (errno == EINVAL)
                printf ("Video capturing or mmap-streaming is not supported\n");
        else
                perror ("VIDIOC_REQBUFS");

        exit (EXIT_FAILURE);
}

/* We want at least five buffers. */

if (reqbuf.count < 5) {
        /* You may need to free the buffers here. */
        printf ("Not enough buffer memory\n");
        exit (EXIT_FAILURE);
}

buffers = calloc (reqbuf.count, sizeof (*buffers));
assert (buffers != NULL);

for (i = 0; i < reqbuf.count; i++) {
        struct v4l2_buffer buffer;

        memset (&buffer, 0, sizeof (buffer));
        buffer.type = reqbuf.type;
	buffer.memory = V4L2_MEMORY_MMAP;
        buffer.index = i;

        if (-1 == ioctl (fd, VIDIOC_QUERYBUF, &buffer)) {
                perror ("VIDIOC_QUERYBUF");
                exit (EXIT_FAILURE);
        }

        buffers[i].length = buffer.length; /* remember for munmap() */

        buffers[i].start = mmap (NULL, buffer.length,
                                 PROT_READ | PROT_WRITE, /* recommended */
                                 MAP_SHARED,             /* recommended */
                                 fd, buffer.m.offset);

        if (MAP_FAILED == buffers[i].start) {
                /* If you do not exit here you should unmap() and free()
                   the buffers mapped so far. */
                perror ("mmap");
                exit (EXIT_FAILURE);
        }
}

/* Cleanup. */

for (i = 0; i < reqbuf.count; i++)
        munmap (buffers[i].start, buffers[i].length);
      

Conceptually streaming drivers maintain two buffer queues, an incoming and an outgoing queue. They separate the synchronous capture or output operation locked to a video clock from the application which is subject to random disk or network delays and preemption by other processes, thereby reducing the probability of data loss. The queues are organized as FIFOs, buffers will be output in the order enqueued in the incoming FIFO, and were captured in the order dequeued from the outgoing FIFO.

The driver may require a minimum number of buffers enqueued at all times to function, apart of this no limit exists on the number of buffers applications can enqueue in advance, or dequeue and process. They can also enqueue in a different order than buffers have been dequeued, and the driver can fill enqueued empty buffers in any order. [15] The index number of a buffer (struct v4l2_buffer index) plays no role here, it only identifies the buffer.

Initially all mapped buffers are in dequeued state, inaccessible by the driver. For capturing applications it is customary to first enqueue all mapped buffers, then to start capturing and enter the read loop. Here the application waits until a filled buffer can be dequeued, and re-enqueues the buffer when the data is no longer needed. Output applications fill and enqueue buffers, when enough buffers are stacked up the output is started with VIDIOC_STREAMON. In the write loop, when the application runs out of free buffers, it must wait until an empty buffer can be dequeued and reused.

To enqueue and dequeue a buffer applications use the VIDIOC_QBUF and VIDIOC_DQBUF ioctl. The status of a buffer being mapped, enqueued, full or empty can be determined at any time using the VIDIOC_QUERYBUF ioctl. Two methods exist to suspend execution of the application until one or more buffers can be dequeued. By default VIDIOC_DQBUF blocks when no buffer is in the outgoing queue. When the O_NONBLOCK flag was given to the open() function, VIDIOC_DQBUF returns immediately with an EAGAIN error code when no buffer is available. The select() or poll() function are always available.

To start and stop capturing or output applications call the VIDIOC_STREAMON and VIDIOC_STREAMOFF ioctl. Note VIDIOC_STREAMOFF removes all buffers from both queues as a side effect. Since there is no notion of doing anything "now" on a multitasking system, if an application needs to synchronize with another event it should examine the struct v4l2_buffer timestamp of captured buffers, or set the field before enqueuing buffers for output.

Drivers implementing memory mapping I/O must support the VIDIOC_REQBUFS, VIDIOC_QUERYBUF, VIDIOC_QBUF, VIDIOC_DQBUF, VIDIOC_STREAMON and VIDIOC_STREAMOFF ioctl, the mmap(), munmap(), select() and poll() function.[16]

[capture example]


3.3. Streaming I/O (User Pointers)

Input and output devices support this I/O method when the V4L2_CAP_STREAMING flag in the capabilities field of struct v4l2_capability returned by the VIDIOC_QUERYCAP ioctl is set. If the particular user pointer method (not only memory mapping) is supported must be determined by calling the VIDIOC_REQBUFS ioctl.

This I/O method combines advantages of the read/write and memory mapping methods. Buffers are allocated by the application itself, and can reside for example in virtual or shared memory. Only pointers to data are exchanged, these pointers and meta-information are passed in struct v4l2_buffer. The driver must be switched into user pointer I/O mode by calling the VIDIOC_REQBUFS with the desired buffer type. No buffers are allocated beforehands, consequently they are not indexed and cannot be queried like mapped buffers with the VIDIOC_QUERYBUF ioctl.

Example 3-2. Initiating streaming I/O with user pointers

struct v4l2_requestbuffers reqbuf;

memset (&reqbuf, 0, sizeof (reqbuf));
reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
reqbuf.memory = V4L2_MEMORY_USERPTR;

if (ioctl (fd, VIDIOC_REQBUFS, &reqbuf) == -1) {
        if (errno == EINVAL)
                printf ("Video capturing or user pointer streaming is not supported\n");
        else
                perror ("VIDIOC_REQBUFS");

        exit (EXIT_FAILURE);
}
      

Buffer addresses and sizes are passed on the fly with the VIDIOC_QBUF ioctl. Although buffers are commonly cycled, applications can pass different addresses and sizes at each VIDIOC_QBUF call. If required by the hardware the driver swaps memory pages within physical memory to create a continuous area of memory. This happens transparently to the application in the virtual memory subsystem of the kernel. When buffer pages have been swapped out to disk they are brought back and finally locked in physical memory for DMA.[17]

Filled or displayed buffers are dequeued with the VIDIOC_DQBUF ioctl. The driver can unlock the memory pages at any time between the completion of the DMA and this ioctl. The memory is also unlocked when VIDIOC_STREAMOFF is called, VIDIOC_REQBUFS, or when the device is closed. Applications must take care not to free buffers without dequeuing. For once, the buffers remain locked until further, wasting physical memory. Second the driver will not be notified when the memory is returned to the application's free list and subsequently reused for other purposes, possibly completing the requested DMA and overwriting valuable data.

For capturing applications it is customary to enqueue a number of empty buffers, to start capturing and enter the read loop. Here the application waits until a filled buffer can be dequeued, and re-enqueues the buffer when the data is no longer needed. Output applications fill and enqueue buffers, when enough buffers are stacked up output is started. In the write loop, when the application runs out of free buffers it must wait until an empty buffer can be dequeued and reused. Two methods exist to suspend execution of the application until one or more buffers can be dequeued. By default VIDIOC_DQBUF blocks when no buffer is in the outgoing queue. When the O_NONBLOCK flag was given to the open() function, VIDIOC_DQBUF returns immediately with an EAGAIN error code when no buffer is available. The select() or poll() function are always available.

To start and stop capturing or output applications call the VIDIOC_STREAMON and VIDIOC_STREAMOFF ioctl. Note VIDIOC_STREAMOFF removes all buffers from both queues and unlocks all buffers as a side effect. Since there is no notion of doing anything "now" on a multitasking system, if an application needs to synchronize with another event it should examine the struct v4l2_buffer timestamp of captured buffers, or set the field before enqueuing buffers for output.

Drivers implementing user pointer I/O must support the VIDIOC_REQBUFS, VIDIOC_QBUF, VIDIOC_DQBUF, VIDIOC_STREAMON and VIDIOC_STREAMOFF ioctl, the select() and poll() function.[18]


3.4. Asynchronous I/O

This method is not defined yet.


3.5. Buffers

A buffer contains data exchanged by application and driver using one of the Streaming I/O methods. Only pointers to buffers are exchanged, the data itself is not copied. These pointers, together with meta-information like timestamps or field parity, are stored in a struct v4l2_buffer, argument to the VIDIOC_QUERYBUF, VIDIOC_QBUF and VIDIOC_DQBUF ioctl.

Nominally timestamps refer to the first data byte transmitted. In practice however the wide range of hardware covered by the V4L2 API limits timestamp accuracy. Often an interrupt routine will sample the system clock shortly after the field or frame was stored completely in memory. So applications must expect a constant difference up to one field or frame period plus a small (few scan lines) random error. The delay and error can be much larger due to compression or transmission over an external bus when the frames are not properly stamped by the sender. This is frequently the case with USB cameras. Here timestamps refer to the instant the field or frame was received by the driver, not the capture time. These devices identify by not enumerating any video standards, see Section 1.7.

Similar limitations apply to output timestamps. Typically the video hardware locks to a clock controlling the video timing, the horizontal and vertical synchronization pulses. At some point in the line sequence, possibly the vertical blanking, an interrupt routine samples the system clock, compares against the timestamp and programs the hardware to repeat the previous field or frame, or to display the buffer contents.

Apart of limitations of the video device and natural inaccuracies of all clocks, it should be noted system time itself is not perfectly stable. It can be affected by power saving cycles, warped to insert leap seconds, or even turned back or forth by the system administrator affecting long term measurements. [19]

Table 3-1. struct v4l2_buffer

__u32index Number of the buffer, set by the application. This field is only used for memory mapping I/O and can range from zero to the number of buffers allocated with the VIDIOC_REQBUFS ioctl (struct v4l2_requestbuffers count) minus one.
enum v4l2_buf_typetype Type of the buffer, same as struct v4l2_format type or struct v4l2_requestbuffers type, set by the application.
__u32bytesused The number of bytes occupied by the data in the buffer. It depends on the negotiated data format and may change with each buffer for compressed variable size data like JPEG images. Drivers must set this field when type refers to an input stream, applications when an output stream.
__u32flags Flags set by the application or driver, see Table 3-3.
enum v4l2_fieldfield Indicates the field order of the image in the buffer, see Table 3-8. This field is not used when the buffer contains VBI data. Drivers must set it when type refers to an input stream, applications when an output stream.
struct timevaltimestamp 

For input streams this is the system time (as returned by the gettimeofday() function) when the first data byte was captured. For output streams the data will not be displayed before this time, secondary to the nominal frame rate determined by the current video standard in enqueued order. Applications can for example zero this field to display frames as soon as possible. The driver stores the time at which the first data byte was actually sent out in the timestamp field. This permits applications to monitor the drift between the video and system clock.

struct v4l2_timecodetimecode When type is V4L2_BUF_TYPE_VIDEO_CAPTURE and the V4L2_BUF_FLAG_TIMECODE flag is set in flags, this structure contains a frame timecode. In V4L2_FIELD_ALTERNATE mode the top and bottom field contain the same timecode. Timecodes are intended to help video editing and are typically recorded on video tapes, but also embedded in compressed formats like MPEG. This field is independent of the timestamp and sequence fields.
__u32sequence Set by the driver, counting the frames in the sequence.

In V4L2_FIELD_ALTERNATE mode the top and bottom field have the same sequence number. The count starts at zero and includes dropped or repeated frames. A dropped frame was received by an input device but could not be stored due to lack of free buffer space. A repeated frame was displayed again by an output device because the application did not pass new data in time.

Note this may count the frames received e.g. over USB, without taking into account the frames dropped by the remote hardware due to limited compression throughput or bus bandwidth. These devices identify by not enumerating any video standards, see Section 1.7.

enum v4l2_memorymemory This field must be set by applications and/or drivers in accordance with the selected I/O method.
unionm  
 __u32offsetWhen memory is V4L2_MEMORY_MMAP this is the offset of the buffer from the start of the device memory. The value is returned by the driver and apart of serving as parameter to the mmap() function not useful for applications. See Section 3.2 for details.
 unsigned longuserptrWhen memory is V4L2_MEMORY_USERPTR this is a pointer to the buffer (casted to unsigned long type) in virtual memory, set by the application. See Section 3.3 for details.
__u32length Size of the buffer (not the payload) in bytes.
__u32input Some video capture drivers support rapid and synchronous video input changes, a function useful for example in video surveillance applications. For this purpose applications set the V4L2_BUF_FLAG_INPUT flag, and this field to the number of a video input as in struct v4l2_input field index.
__u32reserved A place holder for future extensions and custom (driver defined) buffer types V4L2_BUF_TYPE_PRIVATE and higher.

Table 3-2. enum v4l2_buf_type

V4L2_BUF_TYPE_VIDEO_CAPTURE1Buffer of a video capture stream, see Section 4.1.
V4L2_BUF_TYPE_VIDEO_OUTPUT2Buffer of a video output stream, see Section 4.3.
V4L2_BUF_TYPE_VIDEO_OVERLAY3Buffer for video overlay, see Section 4.2.
V4L2_BUF_TYPE_VBI_CAPTURE4Buffer of a raw VBI capture stream, see Section 4.7.
V4L2_BUF_TYPE_VBI_OUTPUT5Buffer of a raw VBI output stream, see Section 4.7.
V4L2_BUF_TYPE_SLICED_VBI_CAPTURE6Buffer of a sliced VBI capture stream, see Section 4.8.
V4L2_BUF_TYPE_SLICED_VBI_OUTPUT7Buffer of a sliced VBI output stream, see Section 4.8.
V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY8Buffer for video output overlay (OSD), see Section 4.4. Status: Experimental.
V4L2_BUF_TYPE_PRIVATE0x80This and higher values are reserved for custom (driver defined) buffer types.

Table 3-3. Buffer Flags

V4L2_BUF_FLAG_MAPPED0x0001The buffer resides in device memory and has been mapped into the application's address space, see Section 3.2 for details. Drivers set or clear this flag when the VIDIOC_QUERYBUF, VIDIOC_QBUF or VIDIOC_DQBUF ioctl is called. Set by the driver.
V4L2_BUF_FLAG_QUEUED0x0002Internally drivers maintain two buffer queues, an incoming and outgoing queue. When this flag is set, the buffer is currently on the incoming queue. It automatically moves to the outgoing queue after the buffer has been filled (capture devices) or displayed (output devices). Drivers set or clear this flag when the VIDIOC_QUERYBUF ioctl is called. After (successful) calling the VIDIOC_QBUF ioctl it is always set and after VIDIOC_DQBUF always cleared.
V4L2_BUF_FLAG_DONE0x0004When this flag is set, the buffer is currently on the outgoing queue, ready to be dequeued from the driver. Drivers set or clear this flag when the VIDIOC_QUERYBUF ioctl is called. After calling the VIDIOC_QBUF or VIDIOC_DQBUF it is always cleared. Of course a buffer cannot be on both queues at the same time, the V4L2_BUF_FLAG_QUEUED and V4L2_BUF_FLAG_DONE flag are mutually exclusive. They can be both cleared however, then the buffer is in "dequeued" state, in the application domain to say so.
V4L2_BUF_FLAG_KEYFRAME0x0008Drivers set or clear this flag when calling the VIDIOC_DQBUF ioctl. It may be set by video capture devices when the buffer contains a compressed image which is a key frame (or field), i. e. can be decompressed on its own.
V4L2_BUF_FLAG_PFRAME0x0010Similar to V4L2_BUF_FLAG_KEYFRAME this flags predicted frames or fields which contain only differences to a previous key frame.
V4L2_BUF_FLAG_BFRAME0x0020Similar to V4L2_BUF_FLAG_PFRAME this is a bidirectional predicted frame or field. [ooc tbd]
V4L2_BUF_FLAG_TIMECODE0x0100The timecode field is valid. Drivers set or clear this flag when the VIDIOC_DQBUF ioctl is called.
V4L2_BUF_FLAG_INPUT0x0200The input field is valid. Applications set or clear this flag before calling the VIDIOC_QBUF ioctl.

Table 3-4. enum v4l2_memory

V4L2_MEMORY_MMAP1The buffer is used for memory mapping I/O.
V4L2_MEMORY_USERPTR2The buffer is used for user pointer I/O.
V4L2_MEMORY_OVERLAY3[to do]

3.5.1. Timecodes

The v4l2_timecode structure is designed to hold a SMPTE 12M or similar timecode. (struct timeval timestamps are stored in struct v4l2_buffer field timestamp.)

Table 3-5. struct v4l2_timecode

__u32typeFrame rate the timecodes are based on, see Table 3-6.
__u32flagsTimecode flags, see Table 3-7.
__u8framesFrame count, 0 ... 23/24/29/49/59, depending on the type of timecode.
__u8secondsSeconds count, 0 ... 59. This is a binary, not BCD number.
__u8minutesMinutes count, 0 ... 59. This is a binary, not BCD number.
__u8hoursHours count, 0 ... 29. This is a binary, not BCD number.
__u8userbits[4]The "user group" bits from the timecode.

Table 3-6. Timecode Types

V4L2_TC_TYPE_24FPS124 frames per second, i. e. film.
V4L2_TC_TYPE_25FPS225 frames per second, i. e. PAL or SECAM video.
V4L2_TC_TYPE_30FPS330 frames per second, i. e. NTSC video.
V4L2_TC_TYPE_50FPS4 
V4L2_TC_TYPE_60FPS5 

Table 3-7. Timecode Flags

V4L2_TC_FLAG_DROPFRAME0x0001Indicates "drop frame" semantics for counting frames in 29.97 fps material. When set, frame numbers 0 and 1 at the start of each minute, except minutes 0, 10, 20, 30, 40, 50 are omitted from the count.
V4L2_TC_FLAG_COLORFRAME0x0002The "color frame" flag.
V4L2_TC_USERBITS_field0x000CField mask for the "binary group flags".
V4L2_TC_USERBITS_USERDEFINED0x0000Unspecified format.
V4L2_TC_USERBITS_8BITCHARS0x00088-bit ISO characters.

3.6. Field Order

We have to distinguish between progressive and interlaced video. Progressive video transmits all lines of a video image sequentially. Interlaced video divides an image into two fields, containing only the odd and even lines of the image, respectively. Alternating the so called odd and even field are transmitted, and due to a small delay between fields a cathode ray TV displays the lines interleaved, yielding the original frame. This curious technique was invented because at refresh rates similar to film the image would fade out too quickly. Transmitting fields reduces the flicker without the necessity of doubling the frame rate and with it the bandwidth required for each channel.

It is important to understand a video camera does not expose one frame at a time, merely transmitting the frames separated into fields. The fields are in fact captured at two different instances in time. An object on screen may well move between one field and the next. For applications analysing motion it is of paramount importance to recognize which field of a frame is older, the temporal order.

When the driver provides or accepts images field by field rather than interleaved, it is also important applications understand how the fields combine to frames. We distinguish between top and bottom fields, the spatial order: The first line of the top field is the first line of an interlaced frame, the first line of the bottom field is the second line of that frame.

However because fields were captured one after the other, arguing whether a frame commences with the top or bottom field is pointless. Any two successive top and bottom, or bottom and top fields yield a valid frame. Only when the source was progressive to begin with, e. g. when transferring film to video, two fields may come from the same frame, creating a natural order.

Counter to intuition the top field is not necessarily the older field. Whether the older field contains the top or bottom lines is a convention determined by the video standard. Hence the distinction between temporal and spatial order of fields. The diagrams below should make this clearer.

All video capture and output devices must report the current field order. Some drivers may permit the selection of a different order, to this end applications initialize the field field of struct v4l2_pix_format before calling the VIDIOC_S_FMT ioctl. If this is not desired it should have the value V4L2_FIELD_ANY (0).

Table 3-8. enum v4l2_field

V4L2_FIELD_ANY0Applications request this field order when any one of the V4L2_FIELD_NONE, V4L2_FIELD_TOP, V4L2_FIELD_BOTTOM, or V4L2_FIELD_INTERLACED formats is acceptable. Drivers choose depending on hardware capabilities or e. g. the requested image size, and return the actual field order. struct v4l2_buffer field can never be V4L2_FIELD_ANY.
V4L2_FIELD_NONE1Images are in progressive format, not interlaced. The driver may also indicate this order when it cannot distinguish between V4L2_FIELD_TOP and V4L2_FIELD_BOTTOM.
V4L2_FIELD_TOP2Images consist of the top field only.
V4L2_FIELD_BOTTOM3Images consist of the bottom field only. Applications may wish to prevent a device from capturing interlaced images because they will have "comb" or "feathering" artefacts around moving objects.
V4L2_FIELD_INTERLACED4Images contain both fields, interleaved line by line. The temporal order of the fields (whether the top or bottom field is first transmitted) depends on the current video standard. M/NTSC transmits the bottom field first, all other standards the top field first.
V4L2_FIELD_SEQ_TB5Images contain both fields, the top field lines are stored first in memory, immediately followed by the bottom field lines. Fields are always stored in temporal order, the older one first in memory. Image sizes refer to the frame, not fields.
V4L2_FIELD_SEQ_BT6Images contain both fields, the bottom field lines are stored first in memory, immediately followed by the top field lines. Fields are always stored in temporal order, the older one first in memory. Image sizes refer to the frame, not fields.
V4L2_FIELD_ALTERNATE7The two fields of a frame are passed in separate buffers, in temporal order, i. e. the older one first. To indicate the field parity (whether the current field is a top or bottom field) the driver or application, depending on data direction, must set struct v4l2_buffer field to V4L2_FIELD_TOP or V4L2_FIELD_BOTTOM. Any two successive fields pair to build a frame. If fields are successive, without any dropped fields between them (fields can drop individually), can be determined from the struct v4l2_buffer sequence field. Image sizes refer to the frame, not fields. This format cannot be selected when using the read/write I/O method.
V4L2_FIELD_INTERLACED_TB8Images contain both fields, interleaved line by line, top field first. The top field is transmitted first.
V4L2_FIELD_INTERLACED_BT9Images contain both fields, interleaved line by line, top field first. The bottom field is transmitted first.

Figure 3-1. Field Order, Top Field First Transmitted

Figure 3-2. Field Order, Bottom Field First Transmitted


Chapter 4. Interfaces

4.1. Video Capture Interface

Video capture devices sample an analog video signal and store the digitized images in memory. Today nearly all devices can capture at full 25 or 30 frames/second. With this interface applications can control the capture process and move images from the driver into user space.

Conventionally V4L2 video capture devices are accessed through character device special files named /dev/video and /dev/video0 to /dev/video63 with major number 81 and minor numbers 0 to 63. /dev/video is typically a symbolic link to the preferred video device. Note the same device files are used for video output devices.


4.1.1. Querying Capabilities

Devices supporting the video capture interface set the V4L2_CAP_VIDEO_CAPTURE flag in the capabilities field of struct v4l2_capability returned by the VIDIOC_QUERYCAP ioctl. As secondary device functions they may also support the video overlay (V4L2_CAP_VIDEO_OVERLAY) and the raw VBI capture (V4L2_CAP_VBI_CAPTURE) interface. At least one of the read/write or streaming I/O methods must be supported. Tuners and audio inputs are optional.


4.1.2. Supplemental Functions

Video capture devices shall support audio input, tuner, controls, cropping and scaling and streaming parameter ioctls as needed. The video input and video standard ioctls must be supported by all video capture devices.


4.1.3. Image Format Negotiation

The result of a capture operation is determined by cropping and image format parameters. The former select an area of the video picture to capture, the latter how images are stored in memory, i. e. in RGB or YUV format, the number of bits per pixel or width and height. Together they also define how images are scaled in the process.

As usual these parameters are not reset at open() time to permit Unix tool chains, programming a device and then reading from it as if it was a plain file. Well written V4L2 applications ensure they really get what they want, including cropping and scaling.

Cropping initialization at minimum requires to reset the parameters to defaults. An example is given in Section 1.11.

To query the current image format applications set the type field of a struct v4l2_format to V4L2_BUF_TYPE_VIDEO_CAPTURE and call the VIDIOC_G_FMT ioctl with a pointer to this structure. Drivers fill the struct v4l2_pix_format pix member of the fmt union.

To request different parameters applications set the type field of a struct v4l2_format as above and initialize all fields of the struct v4l2_pix_format vbi member of the fmt union, or better just modify the results of VIDIOC_G_FMT, and call the VIDIOC_S_FMT ioctl with a pointer to this structure. Drivers may adjust the parameters and finally return the actual parameters as VIDIOC_G_FMT does.

Like VIDIOC_S_FMT the VIDIOC_TRY_FMT ioctl can be used to learn about hardware limitations without disabling I/O or possibly time consuming hardware preparations.

The contents of struct v4l2_pix_format are discussed in Chapter 2. See also the specification of the VIDIOC_G_FMT, VIDIOC_S_FMT and VIDIOC_TRY_FMT ioctls for details. Video capture devices must implement both the VIDIOC_G_FMT and VIDIOC_S_FMT ioctl, even if VIDIOC_S_FMT ignores all requests and always returns default parameters as VIDIOC_G_FMT does. VIDIOC_TRY_FMT is optional.


4.1.4. Reading Images

A video capture device may support the read() function and/or streaming (memory mapping or user pointer) I/O. See Chapter 3 for details.


4.2. Video Overlay Interface

Also known as Framebuffer Overlay or Previewing

Video overlay devices have the ability to genlock (TV-)video into the (VGA-)video signal of a graphics card, or to store captured images directly in video memory of a graphics card, typically with clipping. This can be considerable more efficient than capturing images and displaying them by other means. In the old days when only nuclear power plants needed cooling towers this used to be the only way to put live video into a window.

Video overlay devices are accessed through the same character special files as video capture devices. Note the default function of a /dev/video device is video capturing. The overlay function is only available after calling the VIDIOC_S_FMT ioctl.

The driver may support simultaneous overlay and capturing using the read/write and streaming I/O methods. If so, operation at the nominal frame rate of the video standard is not guaranteed. Frames may be directed away from overlay to capture, or one field may be used for overlay and the other for capture if the capture parameters permit this.

Applications should use different file descriptors for capturing and overlay. This must be supported by all drivers capable of simultaneous capturing and overlay. Optionally these drivers may also permit capturing and overlay with a single file descriptor for compatibility with V4L and earlier versions of V4L2.[20]


4.2.1. Querying Capabilities

Devices supporting the video overlay interface set the V4L2_CAP_VIDEO_OVERLAY flag in the capabilities field of struct v4l2_capability returned by the VIDIOC_QUERYCAP ioctl. The overlay I/O method specified below must be supported. Tuners and audio inputs are optional.


4.2.2. Supplemental Functions

Video overlay devices shall support audio input, tuner, controls, cropping and scaling and streaming parameter ioctls as needed. The video input and video standard ioctls must be supported by all video overlay devices.


4.2.3. Setup

Before overlay can commence applications must program the driver with frame buffer parameters, namely the address and size of the frame buffer and the image format, for example RGB 5:6:5. The VIDIOC_G_FBUF and VIDIOC_S_FBUF ioctls are available to get and set these parameters, respectively. The VIDIOC_S_FBUF ioctl is privileged because it allows to set up DMA into physical memory, bypassing the memory protection mechanisms of the kernel. Only the superuser can change the frame buffer address and size. Users are not supposed to run TV applications as root or with SUID bit set. A small helper application with suitable privileges should query the graphics system and program the V4L2 driver at the appropriate time.

Some devices add the video overlay to the output signal of the graphics card. In this case the frame buffer is not modified by the video device, and the frame buffer address and pixel format are not needed by the driver. The VIDIOC_S_FBUF ioctl is not privileged. An application can check for this type of device by calling the VIDIOC_G_FBUF ioctl.

A driver may support any (or none) of five clipping/blending methods:

  1. Chroma-keying displays the overlaid image only where pixels in the primary graphics surface assume a certain color.

  2. A bitmap can be specified where each bit corresponds to a pixel in the overlaid image. When the bit is set, the corresponding video pixel is displayed, otherwise a pixel of the graphics surface.

  3. A list of clipping rectangles can be specified. In these regions no video is displayed, so the graphics surface can be seen here.

  4. The framebuffer has an alpha channel that can be used to clip or blend the framebuffer with the video.

  5. A global alpha value can be specified to blend the framebuffer contents with video images.

When simultaneous capturing and overlay is supported and the hardware prohibits different image and frame buffer formats, the format requested first takes precedence. The attempt to capture (VIDIOC_S_FMT) or overlay (VIDIOC_S_FBUF) may fail with an EBUSY error code or return accordingly modified parameters..


4.2.4. Overlay Window

The overlaid image is determined by cropping and overlay window parameters. The former select an area of the video picture to capture, the latter how images are overlaid and clipped. Cropping initialization at minimum requires to reset the parameters to defaults. An example is given in Section 1.11.

The overlay window is described by a struct v4l2_window. It defines the size of the image, its position over the graphics surface and the clipping to be applied. To get the current parameters applications set the type field of a struct v4l2_format to V4L2_BUF_TYPE_VIDEO_OVERLAY and call the VIDIOC_G_FMT ioctl. The driver fills the v4l2_window substructure named win. It is not possible to retrieve a previously programmed clipping list or bitmap.

To program the overlay window applications set the type field of a struct v4l2_format to V4L2_BUF_TYPE_VIDEO_OVERLAY, initialize the win substructure and call the VIDIOC_S_FMT ioctl. The driver adjusts the parameters against hardware limits and returns the actual parameters as VIDIOC_G_FMT does. Like VIDIOC_S_FMT, the VIDIOC_TRY_FMT ioctl can be used to learn about driver capabilities without actually changing driver state. Unlike VIDIOC_S_FMT this also works after the overlay has been enabled.

The scaling factor of the overlaid image is implied by the width and height given in struct v4l2_window and the size of the cropping rectangle. For more information see Section 1.11.

When simultaneous capturing and overlay is supported and the hardware prohibits different image and window sizes, the size requested first takes precedence. The attempt to capture or overlay as well (VIDIOC_S_FMT) may fail with an EBUSY error code or return accordingly modified parameters.

Table 4-1. struct v4l2_window

struct v4l2_rectwSize and position of the window relative to the top, left corner of the frame buffer defined with VIDIOC_S_FBUF. The window can extend the frame buffer width and height, the x and y coordinates can be negative, and it can lie completely outside the frame buffer. The driver clips the window accordingly, or if that is not possible, modifies its size and/or position.
enum v4l2_fieldfieldApplications set this field to determine which video field shall be overlaid, typically one of V4L2_FIELD_ANY (0), V4L2_FIELD_TOP, V4L2_FIELD_BOTTOM or V4L2_FIELD_INTERLACED. Drivers may have to choose a different field order and return the actual setting here.
__u32chromakeyWhen chroma-keying has been negotiated with VIDIOC_S_FBUF applications set this field to the desired pixel value for the chroma key. The format is the same as the pixel format of the framebuffer (struct v4l2_framebuffer fmt.pixelformat field), with bytes in host order. E. g. for V4L2_PIX_FMT_BGR24 the value should be 0xRRGGBB on a little endian, 0xBBGGRR on a big endian host.
struct v4l2_clip *clipsWhen chroma-keying has not been negotiated and VIDIOC_G_FBUF indicated this capability, applications can set this field to point to an array of clipping rectangles.
Like the window coordinates w, clipping rectangles are defined relative to the top, left corner of the frame buffer. However clipping rectangles must not extend the frame buffer width and height, and they must not overlap. If possible applications should merge adjacent rectangles. Whether this must create x-y or y-x bands, or the order of rectangles, is not defined. When clip lists are not supported the driver ignores this field. Its contents after calling VIDIOC_S_FMT are undefined.
__u32clipcountWhen the application set the clips field, this field must contain the number of clipping rectangles in the list. When clip lists are not supported the driver ignores this field, its contents after calling VIDIOC_S_FMT are undefined. When clip lists are supported but no clipping is desired this field must be set to zero.
void *bitmapWhen chroma-keying has not been negotiated and VIDIOC_G_FBUF indicated this capability, applications can set this field to point to a clipping bit mask.

It must be of the same size as the window, w.width and w.height. Each bit corresponds to a pixel in the overlaid image, which is displayed only when the bit is set. Pixel coordinates translate to bits like:

((__u8 *) bitmap)[w.width * y + x / 8] & (1 << (x & 7))

where 0 ≤ x < w.width and 0 ≤ y <w.height.a

When a clipping bit mask is not supported the driver ignores this field, its contents after calling VIDIOC_S_FMT are undefined. When a bit mask is supported but no clipping is desired this field must be set to NULL.

Applications need not create a clip list or bit mask. When they pass both, or despite negotiating chroma-keying, the results are undefined. Regardless of the chosen method, the clipping abilities of the hardware may be limited in quantity or quality. The results when these limits are exceeded are undefined.b

__u8global_alpha

The global alpha value used to blend the framebuffer with video images, if global alpha blending has been negotiated (V4L2_FBUF_FLAG_GLOBAL_ALPHA, see VIDIOC_S_FBUF, Table 3).

Note this field was added in Linux 2.6.23, extending the structure. However the VIDIOC_G/S/TRY_FMT ioctls, which take a pointer to a v4l2_format parent structure with padding bytes at the end, are not affected.

Notes:
a. Should we require w.width to be a multiple of eight?
b. When the image is written into frame buffer memory it will be undesirable if the driver clips out less pixels than expected, because the application and graphics system are not aware these regions need to be refreshed. The driver should clip out more pixels or not write the image at all.

Table 4-2. struct v4l2_clip[21]

struct v4l2_rectcCoordinates of the clipping rectangle, relative to the top, left corner of the frame buffer. Only window pixels outside all clipping rectangles are displayed.
struct v4l2_clip *nextPointer to the next clipping rectangle, NULL when this is the last rectangle. Drivers ignore this field, it cannot be used to pass a linked list of clipping rectangles.

Table 4-3. struct v4l2_rect

__s32leftHorizontal offset of the top, left corner of the rectangle, in pixels.
__s32topVertical offset of the top, left corner of the rectangle, in pixels. Offsets increase to the right and down.
__s32widthWidth of the rectangle, in pixels.
__s32heightHeight of the rectangle, in pixels. Width and height cannot be negative, the fields are signed for hysterical reasons.

4.2.5. Enabling Overlay

To start or stop the frame buffer overlay applications call the VIDIOC_OVERLAY ioctl.


4.3. Video Output Interface

Video output devices encode stills or image sequences as analog video signal. With this interface applications can control the encoding process and move images from user space to the driver.

Conventionally V4L2 video output devices are accessed through character device special files named /dev/video and /dev/video0 to /dev/video63 with major number 81 and minor numbers 0 to 63. /dev/video is typically a symbolic link to the preferred video device. Note the same device files are used for video capture devices.


4.3.1. Querying Capabilities

Devices supporting the video output interface set the V4L2_CAP_VIDEO_OUTPUT flag in the capabilities field of struct v4l2_capability returned by the VIDIOC_QUERYCAP ioctl. As secondary device functions they may also support the raw VBI output (V4L2_CAP_VBI_OUTPUT) interface. At least one of the read/write or streaming I/O methods must be supported. Modulators and audio outputs are optional.


4.3.2. Supplemental Functions

Video output devices shall support audio output, modulator, controls, cropping and scaling and streaming parameter ioctls as needed. The video output and video standard ioctls must be supported by all video output devices.


4.3.3. Image Format Negotiation

The output is determined by cropping and image format parameters. The former select an area of the video picture where the image will appear, the latter how images are stored in memory, i. e. in RGB or YUV format, the number of bits per pixel or width and height. Together they also define how images are scaled in the process.

As usual these parameters are not reset at open() time to permit Unix tool chains, programming a device and then writing to it as if it was a plain file. Well written V4L2 applications ensure they really get what they want, including cropping and scaling.

Cropping initialization at minimum requires to reset the parameters to defaults. An example is given in Section 1.11.

To query the current image format applications set the type field of a struct v4l2_format to V4L2_BUF_TYPE_VIDEO_OUTPUT and call the VIDIOC_G_FMT ioctl with a pointer to this structure. Drivers fill the struct v4l2_pix_format pix member of the fmt union.

To request different parameters applications set the type field of a struct v4l2_format as above and initialize all fields of the struct v4l2_pix_format vbi member of the fmt union, or better just modify the results of VIDIOC_G_FMT, and call the VIDIOC_S_FMT ioctl with a pointer to this structure. Drivers may adjust the parameters and finally return the actual parameters as VIDIOC_G_FMT does.

Like VIDIOC_S_FMT the VIDIOC_TRY_FMT ioctl can be used to learn about hardware limitations without disabling I/O or possibly time consuming hardware preparations.

The contents of struct v4l2_pix_format are discussed in Chapter 2. See also the specification of the VIDIOC_G_FMT, VIDIOC_S_FMT and VIDIOC_TRY_FMT ioctls for details. Video output devices must implement both the VIDIOC_G_FMT and VIDIOC_S_FMT ioctl, even if VIDIOC_S_FMT ignores all requests and always returns default parameters as VIDIOC_G_FMT does. VIDIOC_TRY_FMT is optional.


4.3.4. Writing Images

A video output device may support the write() function and/or streaming (memory mapping or user pointer) I/O. See Chapter 3 for details.


4.4. Video Output Overlay Interface

Also known as On-Screen Display (OSD)

Experimental: This is an experimental interface and may change in the future.

Some video output devices can overlay a framebuffer image onto the outgoing video signal. Applications can set up such an overlay using this interface, which borrows structures and ioctls of the Video Overlay interface.

The OSD function is accessible through the same character special file as the Video Output function. Note the default function of such a /dev/video device is video capturing or output. The OSD function is only available after calling the VIDIOC_S_FMT ioctl.


4.4.1. Querying Capabilities

Devices supporting the Video Output Overlay interface set the V4L2_CAP_VIDEO_OUTPUT_OVERLAY flag in the capabilities field of struct v4l2_capability returned by the VIDIOC_QUERYCAP ioctl.


4.4.2. Framebuffer

Contrary to the Video Overlay interface the framebuffer is normally implemented on the TV card and not the graphics card. On Linux it is accessible as a framebuffer device (/dev/fbN). Given a V4L2 device, applications can find the corresponding framebuffer device by calling the VIDIOC_G_FBUF ioctl. It returns, amongst other information, the physical address of the framebuffer in the base field of struct v4l2_framebuffer. The framebuffer device ioctl FBIOGET_FSCREENINFO returns the same address in the smem_start field of struct fb_fix_screeninfo. The FBIOGET_FSCREENINFO ioctl and struct fb_fix_screeninfo are defined in the linux/fb.h header file.

The width and height of the framebuffer depends on the current video standard. A V4L2 driver may reject attempts to change the video standard (or any other ioctl which would imply a framebuffer size change) with an EBUSY error code until all applications closed the framebuffer device.

Example 4-1. Finding a framebuffer device for OSD

#include <linux/fb.h>

struct v4l2_framebuffer fbuf;
unsigned int i;
int fb_fd;

if (-1 == ioctl (fd, VIDIOC_G_FBUF, &fbuf)) {
        perror ("VIDIOC_G_FBUF");
        exit (EXIT_FAILURE);
}

for (i = 0; i < 30; ++i) {
        char dev_name[16];
        struct fb_fix_screeninfo si;

        snprintf (dev_name, sizeof (dev_name), "/dev/fb%u", i);

        fb_fd = open (dev_name, O_RDWR);
        if (-1 == fb_fd) {
                switch (errno) {
                case ENOENT: /* no such file */
                case ENXIO:  /* no driver */
                        continue;

                default:
                        perror ("open");
                        exit (EXIT_FAILURE);
                }
        }

        if (0 == ioctl (fb_fd, FBIOGET_FSCREENINFO, &si)) {
                if (si.smem_start == (unsigned long) fbuf.base)
                        break;
        } else {
                /* Apparently not a framebuffer device. */
        }

        close (fb_fd);
        fb_fd = -1;
}

/* fb_fd is the file descriptor of the framebuffer device
   for the video output overlay, or -1 if no device was found. */

4.4.3. Overlay Window and Scaling

The overlay is controlled by source and target rectangles. The source rectangle selects a subsection of the framebuffer image to be overlaid, the target rectangle an area in the outgoing video signal where the image will appear. Drivers may or may not support scaling, and arbitrary sizes and positions of these rectangles. Further drivers may support any (or none) of the clipping/blending methods defined for the Video Overlay interface.

A struct v4l2_window defines the size of the source rectangle, its position in the framebuffer and the clipping/blending method to be used for the overlay. To get the current parameters applications set the type field of a struct v4l2_format to V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY and call the VIDIOC_G_FMT ioctl. The driver fills the v4l2_window substructure named win. It is not possible to retrieve a previously programmed clipping list or bitmap.

To program the source rectangle applications set the type field of a struct v4l2_format to V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY, initialize the win substructure and call the VIDIOC_S_FMT ioctl. The driver adjusts the parameters against hardware limits and returns the actual parameters as VIDIOC_G_FMT does. Like VIDIOC_S_FMT, the VIDIOC_TRY_FMT ioctl can be used to learn about driver capabilities without actually changing driver state. Unlike VIDIOC_S_FMT this also works after the overlay has been enabled.

A struct v4l2_crop defines the size and position of the target rectangle. The scaling factor of the overlay is implied by the width and height given in struct v4l2_window and struct v4l2_crop. The cropping API applies to Video Output and Video Output Overlay devices in the same way as to Video Capture and Video Overlay devices, merely reversing the direction of the data flow. For more information see Section 1.11.


4.4.4. Enabling Overlay

There is no V4L2 ioctl to enable or disable the overlay, however the framebuffer interface of the driver may support the FBIOBLANK ioctl.


4.5. Codec Interface

Suspended: This interface has been be suspended from the V4L2 API implemented in Linux 2.6 until we have more experience with codec device interfaces.

A V4L2 codec can compress, decompress, transform, or otherwise convert video data from one format into another format, in memory. Applications send data to be converted to the driver through a write() call, and receive the converted data through a read() call. For efficiency a driver may also support streaming I/O.

[to do]


4.6. Effect Devices Interface

Suspended: This interface has been be suspended from the V4L2 API implemented in Linux 2.6 until we have more experience with effect device interfaces.

A V4L2 video effect device can do image effects, filtering, or combine two or more images or image streams. For example video transitions or wipes. Applications send data to be processed and receive the result data either with read() and write() functions, or through the streaming I/O mechanism.

[to do]


4.7. Raw VBI Data Interface

VBI is an abbreviation of Vertical Blanking Interval, a gap in the sequence of lines of an analog video signal. During VBI no picture information is transmitted, allowing some time while the electron beam of a cathode ray tube TV returns to the top of the screen. Using an oscilloscope you will find here the vertical synchronization pulses and short data packages ASK modulated[22] onto the video signal. These are transmissions of services such as Teletext or Closed Caption.

Subject of this interface type is raw VBI data, as sampled off a video signal, or to be added to a signal for output. The data format is similar to uncompressed video images, a number of lines times a number of samples per line, we call this a VBI image.

Conventionally V4L2 VBI devices are accessed through character device special files named /dev/vbi and /dev/vbi0 to /dev/vbi31 with major number 81 and minor numbers 224 to 255. /dev/vbi is typically a symbolic link to the preferred VBI device. This convention applies to both input and output devices.

To address the problems of finding related video and VBI devices VBI capturing and output is also available as device function under /dev/video. To capture or output raw VBI data with these devices applications must call the VIDIOC_S_FMT ioctl. Accessed as /dev/vbi, raw VBI capturing or output is the default device function.


4.7.1. Querying Capabilities

Devices supporting the raw VBI capturing or output API set the V4L2_CAP_VBI_CAPTURE or V4L2_CAP_VBI_OUTPUT flags, respectively, in the capabilities field of struct v4l2_capability returned by the VIDIOC_QUERYCAP ioctl. At least one of the read/write, streaming or asynchronous I/O methods must be supported. VBI devices may or may not have a tuner or modulator.


4.7.2. Supplemental Functions

VBI devices shall support video input or output, tuner or modulator, and controls ioctls as needed. The video standard ioctls provide information vital to program a VBI device, therefore must be supported.


4.7.3. Raw VBI Format Negotiation

Raw VBI sampling abilities can vary, in particular the sampling frequency. To properly interpret the data V4L2 specifies an ioctl to query the sampling parameters. Moreover, to allow for some flexibility applications can also suggest different parameters.

As usual these parameters are not reset at open() time to permit Unix tool chains, programming a device and then reading from it as if it was a plain file. Well written V4L2 applications should always ensure they really get what they want, requesting reasonable parameters and then checking if the actual parameters are suitable.

To query the current raw VBI capture parameters applications set the type field of a struct v4l2_format to V4L2_BUF_TYPE_VBI_CAPTURE or V4L2_BUF_TYPE_VBI_OUTPUT, and call the VIDIOC_G_FMT ioctl with a pointer to this structure. Drivers fill the struct v4l2_vbi_format vbi member of the fmt union.

To request different parameters applications set the type field of a struct v4l2_format as above and initialize all fields of the struct v4l2_vbi_format vbi member of the fmt union, or better just modify the results of VIDIOC_G_FMT, and call the VIDIOC_S_FMT ioctl with a pointer to this structure. Drivers return an EINVAL error code only when the given parameters are ambiguous, otherwise they modify the parameters according to the hardware capabilites and return the actual parameters. When the driver allocates resources at this point, it may return an EBUSY error code to indicate the returned parameters are valid but the required resources are currently not available. That may happen for instance when the video and VBI areas to capture would overlap, or when the driver supports multiple opens and another process already requested VBI capturing or output. Anyway, applications must expect other resource allocation points which may return EBUSY, at the VIDIOC_STREAMON ioctl and the first read(), write() and select() call.

VBI devices must implement both the VIDIOC_G_FMT and VIDIOC_S_FMT ioctl, even if VIDIOC_S_FMT ignores all requests and always returns default parameters as VIDIOC_G_FMT does. VIDIOC_TRY_FMT is optional.

Table 4-4. struct v4l2_vbi_format

__u32sampling_rateSamples per second, i. e. unit 1 Hz.
__u32offset

Horizontal offset of the VBI image, relative to the leading edge of the line synchronization pulse and counted in samples: The first sample in the VBI image will be located offset / sampling_rate seconds following the leading edge. See also Figure 4-1.

__u32samples_per_line 
__u32sample_format

Defines the sample format as in Chapter 2, a four-character-code.a Usually this is V4L2_PIX_FMT_GREY, i. e. each sample consists of 8 bits with lower values oriented towards the black level. Do not assume any other correlation of values with the signal level. For example, the MSB does not necessarily indicate if the signal is 'high' or 'low' because 128 may not be the mean value of the signal. Drivers shall not convert the sample format by software.

__u32start[2]This is the scanning system line number associated with the first line of the VBI image, of the first and the second field respectively. See Figure 4-2 and Figure 4-3 for valid values. VBI input drivers can return start values 0 if the hardware cannot reliable identify scanning lines, VBI acquisition may not require this information.
__u32count[2]The number of lines in the first and second field image, respectively.

Drivers should be as flexibility as possible. For example, it may be possible to extend or move the VBI capture window down to the picture area, implementing a 'full field mode' to capture data service transmissions embedded in the picture.

An application can set the first or second count value to zero if no data is required from the respective field; count[1] if the scanning system is progressive, i. e. not interlaced. The corresponding start value shall be ignored by the application and driver. Anyway, drivers may not support single field capturing and return both count values non-zero.

Both count values set to zero, or line numbers outside the bounds depicted in Figure 4-2 and Figure 4-3, or a field image covering lines of two fields, are invalid and shall not be returned by the driver.

To initialize the start and count fields, applications must first determine the current video standard selection. The v4l2_std_id or the framelines field of struct v4l2_standard can be evaluated for this purpose.

__u32flagsSee Table 4-5 below. Currently only drivers set flags, applications must set this field to zero.
__u32reserved[2]This array is reserved for future extensions. Drivers and applications must set it to zero.
Notes:
a. A few devices may be unable to sample VBI data at all but can extend the video capture window to the VBI region.

Table 4-5. Raw VBI Format Flags

V4L2_VBI_UNSYNC0x0001

This flag indicates hardware which does not properly distinguish between fields. Normally the VBI image stores the first field (lower scanning line numbers) first in memory. This may be a top or bottom field depending on the video standard. When this flag is set the first or second field may be stored first, however the fields are still in correct temporal order with the older field first in memory.a

V4L2_VBI_INTERLACED0x0002By default the two field images will be passed sequentially; all lines of the first field followed by all lines of the second field (compare Section 3.6 V4L2_FIELD_SEQ_TB and V4L2_FIELD_SEQ_BT, whether the top or bottom field is first in memory depends on the video standard). When this flag is set, the two fields are interlaced (cf. V4L2_FIELD_INTERLACED). The first line of the first field followed by the first line of the second field, then the two second lines, and so on. Such a layout may be necessary when the hardware has been programmed to capture or output interlaced video images and is unable to separate the fields for VBI capturing at the same time. For simplicity setting this flag implies that both count values are equal and non-zero.
Notes:
a. Most VBI services transmit on both fields, but some have different semantics depending on the field number. These cannot be reliable decoded or encoded when V4L2_VBI_UNSYNC is set.

Figure 4-1. Line synchronization

Figure 4-2. ITU-R 525 line numbering (M/NTSC and M/PAL)

(1) For the purpose of this specification field 2 starts in line 264 and not 263.5 because half line capturing is not supported.

Figure 4-3. ITU-R 625 line numbering

(1) For the purpose of this specification field 2 starts in line 314 and not 313.5 because half line capturing is not supported.

Remember the VBI image format depends on the selected video standard, therefore the application must choose a new standard or query the current standard first. Attempts to read or write data ahead of format negotiation, or after switching the video standard which may invalidate the negotiated VBI parameters, should be refused by the driver. A format change during active I/O is not permitted.


4.7.4. Reading and writing VBI images

To assure synchronization with the field number and easier implementation, the smallest unit of data passed at a time is one frame, consisting of two fields of VBI images immediately following in memory.

The total size of a frame computes as follows:

(count[0] + count[1]) *
samples_per_line * sample size in bytes

The sample size is most likely always one byte, applications must check the sample_format field though, to function properly with other drivers.

A VBI device may support read/write and/or streaming (memory mapping or user pointer) I/O. The latter bears the possibility of synchronizing video and VBI data by using buffer timestamps.

Remember the VIDIOC_STREAMON ioctl and the first read(), write() and select() call can be resource allocation points returning an EBUSY error code if the required hardware resources are temporarily unavailable, for example the device is already in use by another process.


4.8. Sliced VBI Data Interface

VBI stands for Vertical Blanking Interval, a gap in the sequence of lines of an analog video signal. During VBI no picture information is transmitted, allowing some time while the electron beam of a cathode ray tube TV returns to the top of the screen.

Sliced VBI devices use hardware to demodulate data transmitted in the VBI. V4L2 drivers shall not do this by software, see also the raw VBI interface. The data is passed as short packets of fixed size, covering one scan line each. The number of packets per video frame is variable.

Sliced VBI capture and output devices are accessed through the same character special files as raw VBI devices. When a driver supports both interfaces, the default function of a /dev/vbi device is raw VBI capturing or output, and the sliced VBI function is only available after calling the VIDIOC_S_FMT ioctl as defined below. Likewise a /dev/video device may support the sliced VBI API, however the default function here is video capturing or output. Different file descriptors must be used to pass raw and sliced VBI data simultaneously, if this is supported by the driver.


4.8.1. Querying Capabilities

Devices supporting the sliced VBI capturing or output API set the V4L2_CAP_SLICED_VBI_CAPTURE or V4L2_CAP_SLICED_VBI_OUTPUT flag respectively, in the capabilities field of struct v4l2_capability returned by the VIDIOC_QUERYCAP ioctl. At least one of the read/write, streaming or asynchronous I/O methods must be supported. Sliced VBI devices may have a tuner or modulator.


4.8.2. Supplemental Functions

Sliced VBI devices shall support video input or output and tuner or modulator ioctls if they have these capabilities, and they may support control ioctls. The video standard ioctls provide information vital to program a sliced VBI device, therefore must be supported.


4.8.3. Sliced VBI Format Negotiation

To find out which data services are supported by the hardware applications can call the VIDIOC_G_SLICED_VBI_CAP ioctl. All drivers implementing the sliced VBI interface must support this ioctl. The results may differ from those of the VIDIOC_S_FMT ioctl when the number of VBI lines the hardware can capture or output per frame, or the number of services it can identify on a given line are limited. For example on PAL line 16 the hardware may be able to look for a VPS or Teletext signal, but not both at the same time.

To determine the currently selected services applications set the type field of struct v4l2_format to V4L2_BUF_TYPE_SLICED_VBI_CAPTURE or V4L2_BUF_TYPE_SLICED_VBI_OUTPUT, and the VIDIOC_G_FMT ioctl fills the fmt.sliced member, a struct v4l2_sliced_vbi_format.

Applications can request different parameters by initializing or modifying the fmt.sliced member and calling the VIDIOC_S_FMT ioctl with a pointer to the v4l2_format structure.

The sliced VBI API is more complicated than the raw VBI API because the hardware must be told which VBI service to expect on each scan line. Not all services may be supported by the hardware on all lines (this is especially true for VBI output where Teletext is often unsupported and other services can only be inserted in one specific line). In many cases, however, it is sufficient to just set the service_set field to the required services and let the driver fill the service_lines array according to hardware capabilities. Only if more precise control is needed should the programmer set the service_lines array explicitly.

The VIDIOC_S_FMT ioctl returns an EINVAL error code only when the given parameters are ambiguous, otherwise it modifies the parameters according to hardware capabilities. When the driver allocates resources at this point, it may return an EBUSY error code if the required resources are temporarily unavailable. Other resource allocation points which may return EBUSY can be the VIDIOC_STREAMON ioctl and the first read(), write() and select() call.

Table 4-6. struct v4l2_sliced_vbi_format

__u32service_set

If service_set is non-zero when passed with VIDIOC_S_FMT or VIDIOC_TRY_FMT, the service_lines array will be filled by the driver according to the services specified in this field. For example, if service_set is initialized with V4L2_SLICED_TELETEXT_B | V4L2_SLICED_WSS_625, a driver for the cx25840 video decoder sets lines 7-22 of both fieldsa to V4L2_SLICED_TELETEXT_B and line 23 of the first field to V4L2_SLICED_WSS_625. If service_set is set to zero, then the values of service_lines will be used instead.

On return the driver sets this field to the union of all elements of the returned service_lines array. It may contain less services than requested, perhaps just one, if the hardware cannot handle more services simultaneously. It may be empty (zero) if none of the requested services are supported by the hardware.

__u16service_lines[2][24]

Applications initialize this array with sets of data services the driver shall look for or insert on the respective scan line. Subject to hardware capabilities drivers return the requested set, a subset, which may be just a single service, or an empty set. When the hardware cannot handle multiple services on the same line the driver shall choose one. No assumptions can be made on which service the driver chooses.

Data services are defined in Table 4-7. Array indices map to ITU-R line numbers (see also Figure 4-2 and Figure 4-3) as follows:

  Element525 line systems625 line systems
  service_lines[0][1]11
  service_lines[0][23]2323
  service_lines[1][1]264314
  service_lines[1][23]286336
  Drivers must set service_lines[0][0] and service_lines[1][0] to zero.
__u32io_sizeMaximum number of bytes passed by one read() or write() call, and the buffer size in bytes for the VIDIOC_QBUF and VIDIOC_DQBUF ioctl. Drivers set this field to the size of struct v4l2_sliced_vbi_data times the number of non-zero elements in the returned service_lines array (that is the number of lines potentially carrying data).
__u32reserved[2]This array is reserved for future extensions. Applications and drivers must set it to zero.
Notes:
a. According to ETS 300 706 lines 6-22 of the first field and lines 5-22 of the second field may carry Teletext data.

Table 4-7. Sliced VBI services

SymbolValueReferenceLines, usuallyPayload
V4L2_SLICED_TELETEXT_B (Teletext System B)0x0001ETS 300 706, ITU BT.653PAL/SECAM line 7-22, 320-335 (second field 7-22)Last 42 of the 45 byte Teletext packet, that is without clock run-in and framing code, lsb first transmitted.
V4L2_SLICED_VPS0x0400ETS 300 231PAL line 16Byte number 3 to 15 according to Figure 9 of ETS 300 231, lsb first transmitted.
V4L2_SLICED_CAPTION_5250x1000EIA 608-BNTSC line 21, 284 (second field 21)Two bytes in transmission order, including parity bit, lsb first transmitted.
V4L2_SLICED_WSS_6250x4000ITU BT.1119, EN 300 294PAL/SECAM line 23
Byte         0                 1
      msb         lsb  msb           lsb
 Bit  7 6 5 4 3 2 1 0  x x 13 12 11 10 9
V4L2_SLICED_VBI_5250x1000Set of services applicable to 525 line systems.
V4L2_SLICED_VBI_6250x4401Set of services applicable to 625 line systems.

Drivers may return an EINVAL error code when applications attempt to read or write data without prior format negotiation, after switching the video standard (which may invalidate the negotiated VBI parameters) and after switching the video input (which may change the video standard as a side effect). The VIDIOC_S_FMT ioctl may return an EBUSY error code when applications attempt to change the format while i/o is in progress (between a VIDIOC_STREAMON and VIDIOC_STREAMOFF call, and after the first read() or write() call).


4.8.4. Reading and writing sliced VBI data

A single read() or write() call must pass all data belonging to one video frame. That is an array of v4l2_sliced_vbi_data structures with one or more elements and a total size not exceeding io_size bytes. Likewise in streaming I/O mode one buffer of io_size bytes must contain data of one video frame. The id of unused v4l2_sliced_vbi_data elements must be zero.

Table 4-8. struct v4l2_sliced_vbi_data

__u32idA flag from Table 2 identifying the type of data in this packet. Only a single bit must be set. When the id of a captured packet is zero, the packet is empty and the contents of other fields are undefined. Applications shall ignore empty packets. When the id of a packet for output is zero the contents of the data field are undefined and the driver must no longer insert data on the requested field and line.
__u32fieldThe video field number this data has been captured from, or shall be inserted at. 0 for the first field, 1 for the second field.
__u32lineThe field (as opposed to frame) line number this data has been captured from, or shall be inserted at. See Figure 4-2 and Figure 4-3 for valid values. Sliced VBI capture devices can set the line number of all packets to 0 if the hardware cannot reliably identify scan lines. The field number must always be valid.
__u32reservedThis field is reserved for future extensions. Applications and drivers must set it to zero.
__u8data[48]The packet payload. See Table 2 for the contents and number of bytes passed for each data type. The contents of padding bytes at the end of this array are undefined, drivers and applications shall ignore them.

Packets are always passed in ascending line number order, without duplicate line numbers. The write() function and the VIDIOC_QBUF ioctl must return an EINVAL error code when applications violate this rule. They must also return an EINVAL error code when applications pass an incorrect field or line number, or a combination of field, line and id which has not been negotiated with the VIDIOC_G_FMT or VIDIOC_S_FMT ioctl. When the line numbers are unknown the driver must pass the packets in transmitted order. The driver can insert empty packets with id set to zero anywhere in the packet array.

To assure synchronization and to distinguish from frame dropping, when a captured frame does not carry any of the requested data services drivers must pass one or more empty packets. When an application fails to pass VBI data in time for output, the driver must output the last VPS and WSS packet again, and disable the output of Closed Caption and Teletext data, or output data which is ignored by Closed Caption and Teletext decoders.

A sliced VBI device may support read/write and/or streaming (memory mapping and/or user pointer) I/O. The latter bears the possibility of synchronizing video and VBI data by using buffer timestamps.


4.9. Teletext Interface

This interface aims at devices receiving and demodulating Teletext data [ETS 300 706, ITU BT.653], evaluating the Teletext packages and storing formatted pages in cache memory. Such devices are usually implemented as microcontrollers with serial interface (I2C) and can be found on older TV cards, dedicated Teletext decoding cards and home-brew devices connected to the PC parallel port.

The Teletext API was designed by Martin Buck. It is defined in the kernel header file linux/videotext.h, the specification is available from http://home.pages.de/~videotext/. (Videotext is the name of the German public television Teletext service.) Conventional character device file names are /dev/vtx and /dev/vttuner, with device number 83, 0 and 83, 16 respectively. A similar interface exists for the Philips SAA5249 Teletext decoder [specification?] with character device file names /dev/tlkN, device number 102, N.

Eventually the Teletext API was integrated into the V4L API with character device file names /dev/vtx0 to /dev/vtx31, device major number 81, minor numbers 192 to 223. For reference the V4L Teletext API specification is reproduced here in full: "Teletext interfaces talk the existing VTX API." Teletext devices with major number 83 and 102 will be removed in Linux 2.6.

There are no plans to replace the Teletext API or to integrate it into V4L2. Please write to the Video4Linux mailing list: https://listman.redhat.com/mailman/listinfo/video4linux-list when the need arises.


4.10. Radio Interface

This interface is intended for AM and FM (analog) radio receivers.

Conventionally V4L2 radio devices are accessed through character device special files named /dev/radio and /dev/radio0 to /dev/radio63 with major number 81 and minor numbers 64 to 127.


4.10.1. Querying Capabilities

Devices supporting the radio interface set the V4L2_CAP_RADIO and V4L2_CAP_TUNER flag in the capabilities field of struct v4l2_capability returned by the VIDIOC_QUERYCAP ioctl. Other combinations of capability flags are reserved for future extensions.


4.10.2. Supplemental Functions

Radio devices can support controls, and must support the tuner ioctls.

They do not support the video input or output, audio input or output, video standard, cropping and scaling, compression and streaming parameter, or overlay ioctls. All other ioctls and I/O methods are reserved for future extensions.


4.10.3. Programming

Radio devices may have a couple audio controls (as discussed in Section 1.8) such as a volume control, possibly custom controls. Further all radio devices have one tuner (these are discussed in Section 1.6) with index number zero to select the radio frequency and to determine if a monaural or FM stereo program is received. Drivers switch automatically between AM and FM depending on the selected frequency. The VIDIOC_G_TUNER ioctl reports the supported frequency range.


4.11. RDS Interface

The Radio Data System transmits supplementary information in binary format, for example the station name or travel information, on a inaudible audio subcarrier of a radio program. This interface aims at devices capable of receiving and decoding RDS information.

The V4L API defines its RDS API as follows.

From radio devices supporting it, RDS data can be read with the read() function. The data is packed in groups of three, as follows:

  1. First Octet Least Significant Byte of RDS Block

  2. Second Octet Most Significant Byte of RDS Block

  3. Third Octet Bit 7: Error bit. Indicates that an uncorrectable error occurred during reception of this block. Bit 6: Corrected bit. Indicates that an error was corrected for this data block. Bits 5-3: Received Offset. Indicates the offset received by the sync system. Bits 2-0: Offset Name. Indicates the offset applied to this data.

It was argued the RDS API should be extended before integration into V4L2, no new API has been devised yet. Please write to the Video4Linux mailing list for discussion: https://listman.redhat.com/mailman/listinfo/video4linux-list. Meanwhile no V4L2 driver should set the V4L2_CAP_RDS_CAPTURE capability flag.

I. Function Reference

Table of Contents
V4L2 close() -- Close a V4L2 device
V4L2 ioctl() -- Program a V4L2 device
ioctl VIDIOC_CROPCAP -- Information about the video cropping and scaling abilities
ioctl VIDIOC_DBG_G_REGISTER, VIDIOC_DBG_S_REGISTER -- Read or write hardware registers
ioctl VIDIOC_ENCODER_CMD, VIDIOC_TRY_ENCODER_CMD -- Execute an encoder command
ioctl VIDIOC_ENUMAUDIO -- Enumerate audio inputs
ioctl VIDIOC_ENUMAUDOUT -- Enumerate audio outputs
ioctl VIDIOC_ENUM_FMT -- Enumerate image formats
ioctl VIDIOC_ENUM_FRAMESIZES -- Enumerate frame sizes
ioctl VIDIOC_ENUM_FRAMEINTERVALS -- Enumerate frame intervals
ioctl VIDIOC_ENUMINPUT -- Enumerate video inputs
ioctl VIDIOC_ENUMOUTPUT -- Enumerate video outputs
ioctl VIDIOC_ENUMSTD -- Enumerate supported video standards
ioctl VIDIOC_G_AUDIO, VIDIOC_S_AUDIO -- Query or select the current audio input and its attributes
ioctl VIDIOC_G_AUDOUT, VIDIOC_S_AUDOUT -- Query or select the current audio output
ioctl VIDIOC_G_CHIP_IDENT -- Identify the chips on a TV card
ioctl VIDIOC_G_CROP, VIDIOC_S_CROP -- Get or set the current cropping rectangle
ioctl VIDIOC_G_CTRL, VIDIOC_S_CTRL -- Get or set the value of a control
ioctl VIDIOC_G_ENC_INDEX -- Get meta data about a compressed video stream
ioctl VIDIOC_G_EXT_CTRLS, VIDIOC_S_EXT_CTRLS, VIDIOC_TRY_EXT_CTRLS -- Get or set the value of several controls, try control values
ioctl VIDIOC_G_FBUF, VIDIOC_S_FBUF -- Get or set frame buffer overlay parameters
ioctl VIDIOC_G_FMT, VIDIOC_S_FMT, VIDIOC_TRY_FMT -- Get or set the data format, try a format
ioctl VIDIOC_G_FREQUENCY, VIDIOC_S_FREQUENCY -- Get or set tuner or modulator radio frequency
ioctl VIDIOC_G_INPUT, VIDIOC_S_INPUT -- Query or select the current video input
ioctl VIDIOC_G_JPEGCOMP, VIDIOC_S_JPEGCOMP -- 
ioctl VIDIOC_G_MODULATOR, VIDIOC_S_MODULATOR -- Get or set modulator attributes
ioctl VIDIOC_G_OUTPUT, VIDIOC_S_OUTPUT -- Query or select the current video output
ioctl VIDIOC_G_PARM, VIDIOC_S_PARM -- Get or set streaming parameters
ioctl VIDIOC_G_PRIORITY, VIDIOC_S_PRIORITY -- Query or request the access priority associated with a file descriptor
ioctl VIDIOC_G_SLICED_VBI_CAP -- Query sliced VBI capabilities
ioctl VIDIOC_G_STD, VIDIOC_S_STD -- Query or select the video standard of the current input
ioctl VIDIOC_G_TUNER, VIDIOC_S_TUNER -- Get or set tuner attributes
ioctl VIDIOC_LOG_STATUS -- Log driver status information
ioctl VIDIOC_OVERLAY -- Start or stop video overlay
ioctl VIDIOC_QBUF, VIDIOC_DQBUF -- Exchange a buffer with the driver
ioctl VIDIOC_QUERYBUF -- Query the status of a buffer
ioctl VIDIOC_QUERYCAP -- Query device capabilities
ioctl VIDIOC_QUERYCTRL, VIDIOC_QUERYMENU -- Enumerate controls and menu control items
ioctl VIDIOC_QUERYSTD -- Sense the video standard received by the current input
ioctl VIDIOC_REQBUFS -- Initiate Memory Mapping or User Pointer I/O
ioctl VIDIOC_STREAMON, VIDIOC_STREAMOFF -- Start or stop streaming I/O
V4L2 mmap() -- Map device memory into application address space
V4L2 munmap() -- Unmap device memory
V4L2 open() -- Open a V4L2 device
V4L2 poll() -- Wait for some event on a file descriptor
V4L2 read() -- Read from a V4L2 device
V4L2 select() -- Synchronous I/O multiplexing
V4L2 write() -- Write to a V4L2 device

V4L2 close()

Name

v4l2-close -- Close a V4L2 device

Synopsis

#include <unistd.h>

int close(int fd);

Arguments

fd

File descriptor returned by open().

Description

Closes the device. Any I/O in progress is terminated and resources associated with the file descriptor are freed. However data format parameters, current input or output, control values or other properties remain unchanged.

Return Value

The function returns 0 on success, -1 on failure and the errno is set appropriately. Possible error codes:

EBADF

fd is not a valid open file descriptor.

V4L2 ioctl()

Name

v4l2-ioctl -- Program a V4L2 device

Synopsis

#include <sys/ioctl.h>

int ioctl(int fd, int request, void *argp);

Arguments

fd

File descriptor returned by open().

request

V4L2 ioctl request code as defined in the videodev.h header file, for example VIDIOC_QUERYCAP.

argp

Pointer to a function parameter, usually a structure.

Description

The ioctl() function is used to program V4L2 devices. The argument fd must be an open file descriptor. An ioctl request has encoded in it whether the argument is an input, output or read/write parameter, and the size of the argument argp in bytes. Macros and defines specifying V4L2 ioctl requests are located in the videodev.h header file. Applications should use their own copy, not include the version in the kernel sources on the system they compile on. All V4L2 ioctl requests, their respective function and parameters are specified in Reference I, Function Reference.

Return Value

On success the ioctl() function returns 0 and does not reset the errno variable. On failure -1 is returned, when the ioctl takes an output or read/write parameter it remains unmodified, and the errno variable is set appropriately. See below for possible error codes. Generic errors like EBADF or EFAULT are not listed in the sections discussing individual ioctl requests.

Note ioctls may return undefined error codes. Since errors may have side effects such as a driver reset applications should abort on unexpected errors.

EBADF

fd is not a valid open file descriptor.

EBUSY

The property cannot be changed right now. Typically this error code is returned when I/O is in progress or the driver supports multiple opens and another process locked the property.

EFAULT

argp references an inaccessible memory area.

ENOTTY

fd is not associated with a character special device.

EINVAL

The request or the data pointed to by argp is not valid. This is a very common error code, see the individual ioctl requests listed in Reference I, Function Reference for actual causes.

ENOMEM

Not enough physical or virtual memory was available to complete the request.

ERANGE

The application attempted to set a control with the VIDIOC_S_CTRL ioctl to a value which is out of bounds.

ioctl VIDIOC_CROPCAP

Name

VIDIOC_CROPCAP -- Information about the video cropping and scaling abilities

Synopsis

int ioctl(int fd, int request, struct v4l2_cropcap *argp);

Arguments

fd

File descriptor returned by open().

request

VIDIOC_CROPCAP

argp

Description

Applications use this function to query the cropping limits, the pixel aspect of images and to calculate scale factors. They set the type field of a v4l2_cropcap structure to the respective buffer (stream) type and call the VIDIOC_CROPCAP ioctl with a pointer to this structure. Drivers fill the rest of the structure. The results are constant except when switching the video standard. Remember this switch can occur implicit when switching the video input or output.

Table 1. struct v4l2_cropcap

enum v4l2_buf_typetypeType of the data stream, set by the application. Only these types are valid here: V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_BUF_TYPE_VIDEO_OUTPUT, V4L2_BUF_TYPE_VIDEO_OVERLAY, and custom (driver defined) types with code V4L2_BUF_TYPE_PRIVATE and higher.
struct v4l2_rectboundsDefines the window within capturing or output is possible, this may exclude for example the horizontal and vertical blanking areas. The cropping rectangle cannot exceed these limits. Width and height are defined in pixels, the driver writer is free to choose origin and units of the coordinate system in the analog domain.
struct v4l2_rectdefrectDefault cropping rectangle, it shall cover the "whole picture". Assuming pixel aspect 1/1 this could be for example a 640 × 480 rectangle for NTSC, a 768 × 576 rectangle for PAL and SECAM centered over the active picture area. The same co-ordinate system as for bounds is used.
struct v4l2_fractpixelaspect

This is the pixel aspect (y / x) when no scaling is applied, the ratio of the actual sampling frequency and the frequency required to get square pixels.

When cropping coordinates refer to square pixels, the driver sets pixelaspect to 1/1. Other common values are 54/59 for PAL and SECAM, 11/10 for NTSC sampled according to [ITU BT.601].

Table 2. struct v4l2_rect

__s32leftHorizontal offset of the top, left corner of the rectangle, in pixels.
__s32topVertical offset of the top, left corner of the rectangle, in pixels.
__s32widthWidth of the rectangle, in pixels.
__s32heightHeight of the rectangle, in pixels. Width and height cannot be negative, the fields are signed for hysterical reasons.

Return Value

On success 0 is returned, on error -1 and the errno variable is set appropriately:

EINVAL

The struct v4l2_cropcap type is invalid or the ioctl is not supported. This is not permitted for video capture, output and overlay devices, which must support VIDIOC_CROPCAP.

ioctl VIDIOC_DBG_G_REGISTER, VIDIOC_DBG_S_REGISTER

Name

VIDIOC_DBG_G_REGISTER, VIDIOC_DBG_S_REGISTER -- Read or write hardware registers

Synopsis

int ioctl(int fd, int request, struct v4l2_register *argp);

int ioctl(int fd, int request, const struct v4l2_register *argp);

Arguments

fd

File descriptor returned by open().

request

VIDIOC_DBG_G_REGISTER, VIDIOC_DBG_S_REGISTER

argp

Description

Experimental: This is an experimental interface and may change in the future.

For driver debugging purposes these ioctls allow test applications to access hardware registers directly. Regular applications should not use them.

Since writing or even reading registers can jeopardize the system security, its stability and damage the hardware, both ioctls require superuser privileges. Additionally the Linux kernel must be compiled with the CONFIG_VIDEO_ADV_DEBUG option to enable these ioctls.

To write a register applications must initialize all fields of a struct v4l2_register and call VIDIOC_DBG_S_REGISTER with a pointer to this structure. The match_type and match_chip fields select a chip on the TV card, the reg field specifies a register number and the val field the value to be written into the register.

To read a register applications must initialize the match_type, match_chip and reg fields, and call VIDIOC_DBG_G_REGISTER with a pointer to this structure. On success the driver stores the register value in the val field. On failure the structure remains unchanged.

When match_type is V4L2_CHIP_MATCH_HOST, match_chip selects the nth non-I2C chip on the TV card. Drivers may also interpret match_chip as a random ID, but we recommend against that. The number zero always selects the host chip, e. g. the chip connected to the PCI bus. You can find out which chips are present with the VIDIOC_G_CHIP_IDENT ioctl.

When match_type is V4L2_CHIP_MATCH_I2C_DRIVER, match_chip contains a driver ID as defined in the linux/i2c-id.h header file. For instance I2C_DRIVERID_SAA7127 will match any chip supported by the saa7127 driver, regardless of its I2C bus address. When multiple chips supported by the same driver are present, the effect of these ioctls is undefined. Again with the VIDIOC_G_CHIP_IDENT ioctl you can find out which I2C chips are present.

When match_type is V4L2_CHIP_MATCH_I2C_ADDR, match_chip selects a chip by its 7 bit I2C bus address.

Success not guaranteed: Due to a flaw in the Linux I2C bus driver these ioctls may return successfully without actually reading or writing a register. To catch the most likely failure we recommend a VIDIOC_G_CHIP_IDENT call confirming the presence of the selected I2C chip.

These ioctls are optional, not all drivers may support them. However when a driver supports these ioctls it must also support VIDIOC_G_CHIP_IDENT. Conversely it may support VIDIOC_G_CHIP_IDENT but not these ioctls.

VIDIOC_DBG_G_REGISTER and VIDIOC_DBG_S_REGISTER were introduced in Linux 2.6.21.

We recommended the v4l2-dbg utility over calling these ioctls directly. It is available from the LinuxTV v4l-dvb repository; see http://linuxtv.org/repo/ for access instructions.

Table 1. struct v4l2_register

__u32match_typeSee Table 2 for a list of possible types. 
__u32match_chipMatch a chip by this number, interpreted according to the match_type field. 
__u64regA register number. 
__u64valThe value read from, or to be written into the register. 

Table 2. Chip Match Types

V4L2_CHIP_MATCH_HOST0Match the nth chip on the card, zero for the host chip. Does not match I2C chips.
V4L2_CHIP_MATCH_I2C_DRIVER1Match an I2C chip by its driver ID from the linux/i2c-id.h header file.
V4L2_CHIP_MATCH_I2C_ADDR2Match a chip by its 7 bit I2C bus address.

Return Value

On success 0 is returned, on error -1 and the errno variable is set appropriately:

EINVAL

The driver does not support this ioctl, or the kernel was not compiled with the CONFIG_VIDEO_ADV_DEBUG option, or the match_type is invalid, or the selected chip or register does not exist.

EPERM

Insufficient permissions. Root privileges are required to execute these ioctls.

ioctl VIDIOC_ENCODER_CMD, VIDIOC_TRY_ENCODER_CMD

Name

VIDIOC_ENCODER_CMD, VIDIOC_TRY_ENCODER_CMD -- Execute an encoder command

Synopsis

int ioctl(int fd, int request, struct v4l2_encoder_cmd *argp);

Arguments

fd

File descriptor returned by open().

request

VIDIOC_ENCODER_CMD, VIDIOC_TRY_ENCODER_CMD

argp

Description

Experimental: This is an experimental interface and may change in the future.

These ioctls control an audio/video (usually MPEG-) encoder. VIDIOC_ENCODER_CMD sends a command to the encoder, VIDIOC_TRY_ENCODER_CMD can be used to try a command without actually executing it.

To send a command applications must initialize all fields of a struct v4l2_encoder_cmd and call VIDIOC_ENCODER_CMD or VIDIOC_TRY_ENCODER_CMD with a pointer to this structure.

The cmd field must contain the command code. The flags field is currently only used by the STOP command and contains one bit: If the V4L2_ENC_CMD_STOP_AT_GOP_END flag is set, encoding will continue until the end of the current Group Of Pictures, otherwise it will stop immediately.

A read() call sends a START command to the encoder if it has not been started yet. After a STOP command, read() calls will read the remaining data buffered by the driver. When the buffer is empty, read() will return zero and the next read() call will restart the encoder.

A close() call sends an immediate STOP to the encoder, and all buffered data is discarded.

These ioctls are optional, not all drivers may support them. They were introduced in Linux 2.6.21.

Table 1. struct v4l2_encoder_cmd

__u32cmdThe encoder command, see Table 2.
__u32flagsFlags to go with the command, see Table 3. If no flags are defined for this command, drivers and applications must set this field to zero.
__u32data[8]Reserved for future extensions. Drivers and applications must set the array to zero.

Table 2. Encoder Commands

V4L2_ENC_CMD_START0Start the encoder. When the encoder is already running or paused, this command does nothing. No flags are defined for this command.
V4L2_ENC_CMD_STOP1Stop the encoder. When the V4L2_ENC_CMD_STOP_AT_GOP_END flag is set, encoding will continue until the end of the current Group Of Pictures, otherwise encoding will stop immediately. When the encoder is already stopped, this command does nothing.
V4L2_ENC_CMD_PAUSE2Pause the encoder. When the encoder has not been started yet, the driver will return an EPERM error code. When the encoder is already paused, this command does nothing. No flags are defined for this command.
V4L2_ENC_CMD_RESUME3Resume encoding after a PAUSE command. When the encoder has not been started yet, the driver will return an EPERM error code. When the encoder is already running, this command does nothing. No flags are defined for this command.

Table 3. Encoder Command Flags

V4L2_ENC_CMD_STOP_AT_GOP_END0x0001Stop encoding at the end of the current Group Of Pictures, rather than immediately.

Return Value

On success 0 is returned, on error -1 and the errno variable is set appropriately:

EINVAL

The driver does not support this ioctl, or the cmd field is invalid.

EPERM

The application sent a PAUSE or RESUME command when the encoder was not running.

ioctl VIDIOC_ENUMAUDIO

Name

VIDIOC_ENUMAUDIO -- Enumerate audio inputs

Synopsis

int ioctl(int fd, int request, struct v4l2_audio *argp);

Arguments

fd

File descriptor returned by open().

request

VIDIOC_ENUMAUDIO

argp

Description

To query the attributes of an audio input applications initialize the index field and zero out the reserved array of a struct v4l2_audio and call the VIDIOC_ENUMAUDIO ioctl with a pointer to this structure. Drivers fill the rest of the structure or return an EINVAL error code when the index is out of bounds. To enumerate all audio inputs applications shall begin at index zero, incrementing by one until the driver returns EINVAL.

See ioctl VIDIOC_G_AUDIO, VIDIOC_S_AUDIO(2) for a description of struct v4l2_audio.

Return Value

On success 0 is returned, on error -1 and the errno variable is set appropriately:

EINVAL

The number of the audio input is out of bounds, or there are no audio inputs at all and this ioctl is not supported.

ioctl VIDIOC_ENUMAUDOUT

Name

VIDIOC_ENUMAUDOUT -- Enumerate audio outputs

Synopsis

int ioctl(int fd, int request, struct v4l2_audioout *argp);

Arguments

fd

File descriptor returned by open().

request

VIDIOC_ENUMAUDOUT

argp

Description

To query the attributes of an audio output applications initialize the index field and zero out the reserved array of a struct v4l2_audioout and call the VIDIOC_G_AUDOUT ioctl with a pointer to this structure. Drivers fill the rest of the structure or return an EINVAL error code when the index is out of bounds. To enumerate all audio outputs applications shall begin at index zero, incrementing by one until the driver returns EINVAL.

Note connectors on a TV card to loop back the received audio signal to a sound card are not audio outputs in this sense.

See ioctl VIDIOC_G_AUDOUT, VIDIOC_S_AUDOUT(2) for a description of struct v4l2_audioout.

Return Value

On success 0 is returned, on error -1 and the errno variable is set appropriately:

EINVAL

The number of the audio output is out of bounds, or there are no audio outputs at all and this ioctl is not supported.

ioctl VIDIOC_ENUM_FMT

Name

VIDIOC_ENUM_FMT -- Enumerate image formats

Synopsis

int ioctl(int fd, int request, struct v4l2_fmtdesc *argp);

Arguments

fd

File descriptor returned by open().

request

VIDIOC_ENUM_FMT

argp

Description

To enumerate image formats applications initialize the type and index field of struct v4l2_fmtdesc and call the VIDIOC_ENUM_FMT ioctl with a pointer to this structure. Drivers fill the rest of the structure or return an EINVAL error code. All formats are enumerable by beginning at index zero and incrementing by one until EINVAL is returned.

Table 1. struct v4l2_fmtdesc

__u32indexNumber of the format in the enumeration, set by the application. This is in no way related to the pixelformat field.
enum v4l2_buf_typetypeType of the data stream, set by the application. Only these types are valid here: V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_BUF_TYPE_VIDEO_OUTPUT, V4L2_BUF_TYPE_VIDEO_OVERLAY, and custom (driver defined) types with code V4L2_BUF_TYPE_PRIVATE and higher.
__u32flagsSee Table 2
__u8description[32]Description of the format, a NUL-terminated ASCII string. This information is intended for the user, for example: "YUV 4:2:2".
__u32pixelformatThe image format identifier. This is a four character code as computed by the v4l2_fourcc() macro:

#define v4l2_fourcc(a,b,c,d) (((__u32)(a)<<0)|((__u32)(b)<<8)|((__u32)(c)<<16)|((__u32)(d)<<24))

Several image formats are already defined by this specification in Chapter 2. Note these codes are not the same as those used in the Windows world.

__u32reserved[4]Reserved for future extensions. Drivers must set the array to zero.

Table 2. Image Format Description Flags

V4L2_FMT_FLAG_COMPRESSED0x0001This is a compressed format.

Return Value

On success 0 is returned, on error -1 and the errno variable is set appropriately:

EINVAL

The struct v4l2_fmtdesc type is not supported or the index is out of bounds.

ioctl VIDIOC_ENUM_FRAMESIZES

Name

VIDIOC_ENUM_FRAMESIZES -- Enumerate frame sizes

Synopsis

int ioctl(int fd, int request, struct v4l2_frmsizeenum *argp);

Arguments

fd

File descriptor returned by open().

request

VIDIOC_ENUM_FRAMESIZES

argp

Pointer to a struct v4l2_frmsizeenum that contains an index and pixel format and receives a frame width and height.

Description

Experimental: This is an experimental interface and may change in the future.

This ioctl allows applications to enumerate all frame sizes (i. e. width and height in pixels) that the device supports for the given pixel format.

The supported pixel formats can be obtained by using the VIDIOC_ENUM_FMT function.

The return value and the content of the v4l2_frmsizeenum.type field depend on the type of frame sizes the device supports. Here are the semantics of the function for the different cases:

  • Discrete: The function returns success if the given index value (zero-based) is valid. The application should increase the index by one for each call until EINVAL is returned. The v4l2_frmsizeenum.type field is set to V4L2_FRMSIZE_TYPE_DISCRETE by the driver. Of the union only the discrete member is valid.

  • Step-wise: The function returns success if the given index value is zero and EINVAL for any other index value. The v4l2_frmsizeenum.type field is set to V4L2_FRMSIZE_TYPE_STEPWISE by the driver. Of the union only the stepwise member is valid.

  • Continuous: This is a special case of the step-wise type above. The function returns success if the given index value is zero and EINVAL for any other index value. The v4l2_frmsizeenum.type field is set to V4L2_FRMSIZE_TYPE_CONTINUOUS by the driver. Of the union only the stepwise member is valid and the step_width and step_height values are set to 1.

When the application calls the function with index zero, it must check the type field to determine the type of frame size enumeration the device supports. Only for the V4L2_FRMSIZE_TYPE_DISCRETE type does it make sense to increase the index value to receive more frame sizes.

Note that the order in which the frame sizes are returned has no special meaning. In particular does it not say anything about potential default format sizes.

Applications can assume that the enumeration data does not change without any interaction from the application itself. This means that the enumeration data is consistent if the application does not perform any other ioctl calls while it runs the frame size enumeration.

Structs

In the structs below, IN denotes a value that has to be filled in by the application, OUT denotes values that the driver fills in. The application should zero out all members except for the IN fields.

Table 1. struct v4l2_frmsize_discrete

__u32widthWidth of the frame [pixel].
__u32heightHeight of the frame [pixel].

Table 2. struct v4l2_frmsize_stepwise

__u32min_widthMinimum frame width [pixel].
__u32max_widthMaximum frame width [pixel].
__u32step_widthFrame width step size [pixel].
__u32min_heightMinimum frame height [pixel].
__u32max_heightMaximum frame height [pixel].
__u32step_heightFrame height step size [pixel].

Table 3. struct v4l2_frmsizeenum

__u32index IN: Index of the given frame size in the enumeration.
__u32pixel_format IN: Pixel format for which the frame sizes are enumerated.
__u32type OUT: Frame size type the device supports.
union  OUT: Frame size with the given index.
 struct v4l2_frmsize_discretediscrete 
 struct v4l2_frmsize_stepwisestepwise 
__u32reserved[2] Reserved space for future use.

Enums

Table 4. enum v4l2_frmsizetypes

V4L2_FRMSIZE_TYPE_DISCRETE1Discrete frame size.
V4L2_FRMSIZE_TYPE_CONTINUOUS2Continuous frame size.
V4L2_FRMSIZE_TYPE_STEPWISE3Step-wise defined frame size.

Return Value

On success 0 is returned, on error -1 and the errno variable is set appropriately:

See the description section above for a list of return values that errno can have.

ioctl VIDIOC_ENUM_FRAMEINTERVALS

Name

VIDIOC_ENUM_FRAMEINTERVALS -- Enumerate frame intervals

Synopsis

int ioctl(int fd, int request, struct v4l2_frmivalenum *argp);

Arguments

fd

File descriptor returned by open().

request

VIDIOC_ENUM_FRAMEINTERVALS

argp

Pointer to a struct v4l2_frmivalenum structure that contains a pixel format and size and receives a frame interval.

Description

This ioctl allows applications to enumerate all frame intervals that the device supports for the given pixel format and frame size.

The supported pixel formats and frame sizes can be obtained by using the VIDIOC_ENUM_FMT and VIDIOC_ENUM_FRAMESIZES functions.

The return value and the content of the v4l2_frmivalenum.type field depend on the type of frame intervals the device supports. Here are the semantics of the function for the different cases:

  • Discrete: The function returns success if the given index value (zero-based) is valid. The application should increase the index by one for each call until EINVAL is returned. The `v4l2_frmivalenum.type` field is set to `V4L2_FRMIVAL_TYPE_DISCRETE` by the driver. Of the union only the `discrete` member is valid.

  • Step-wise: The function returns success if the given index value is zero and EINVAL for any other index value. The v4l2_frmivalenum.type field is set to V4L2_FRMIVAL_TYPE_STEPWISE by the driver. Of the union only the stepwise member is valid.

  • Continuous: This is a special case of the step-wise type above. The function returns success if the given index value is zero and EINVAL for any other index value. The v4l2_frmivalenum.type field is set to V4L2_FRMIVAL_TYPE_CONTINUOUS by the driver. Of the union only the stepwise member is valid and the step value is set to 1.

When the application calls the function with index zero, it must check the type field to determine the type of frame interval enumeration the device supports. Only for the V4L2_FRMIVAL_TYPE_DISCRETE type does it make sense to increase the index value to receive more frame intervals.

Note that the order in which the frame intervals are returned has no special meaning. In particular does it not say anything about potential default frame intervals.

Applications can assume that the enumeration data does not change without any interaction from the application itself. This means that the enumeration data is consistent if the application does not perform any other ioctl calls while it runs the frame interval enumeration.

Notes

  • Frame intervals and frame rates: The V4L2 API uses frame intervals instead of frame rates. Given the frame interval the frame rate can be computed as follows:

    frame_rate = 1 / frame_interval

Structs

In the structs below, IN denotes a value that has to be filled in by the application, OUT denotes values that the driver fills in. The application should zero out all members except for the IN fields.

Table 1. struct v4l2_frmival_stepwise

struct v4l2_fractminMinimum frame interval [s].
struct v4l2_fractmaxMaximum frame interval [s].
struct v4l2_fractstepFrame interval step size [s].

Table 2. struct v4l2_frmivalenum

__u32index IN: Index of the given frame interval in the enumeration.
__u32pixel_format IN: Pixel format for which the frame intervals are enumerated.
__u32width IN: Frame width for which the frame intervals are enumerated.
__u32height IN: Frame height for which the frame intervals are enumerated.
__u32type OUT: Frame interval type the device supports.
union  OUT: Frame interval with the given index.
 struct v4l2_fractdiscreteFrame interval [s].
 struct v4l2_frmival_stepwisestepwise 
__u32reserved[2] Reserved space for future use.

Enums

Table 3. enum v4l2_frmivaltypes

V4L2_FRMIVAL_TYPE_DISCRETE1Discrete frame interval.
V4L2_FRMIVAL_TYPE_CONTINUOUS2Continuous frame interval.
V4L2_FRMIVAL_TYPE_STEPWISE3Step-wise defined frame interval.

Return Value

On success 0 is returned, on error -1 and the errno variable is set appropriately:

See the description section above for a list of return values that errno can have.

ioctl VIDIOC_ENUMINPUT

Name

VIDIOC_ENUMINPUT -- Enumerate video inputs

Synopsis

int ioctl(int fd, int request, struct v4l2_input *argp);

Arguments

fd

File descriptor returned by open().

request

VIDIOC_ENUMINPUT

argp

Description

To query the attributes of a video input applications initialize the index field of struct v4l2_input and call the VIDIOC_ENUMINPUT ioctl with a pointer to this structure. Drivers fill the rest of the structure or return an EINVAL error code when the index is out of bounds. To enumerate all inputs applications shall begin at index zero, incrementing by one until the driver returns EINVAL.

Table 1. struct v4l2_input

__u32indexIdentifies the input, set by the application.
__u8name[32]Name of the video input, a NUL-terminated ASCII string, for example: "Vin (Composite 2)". This information is intended for the user, preferably the connector label on the device itself.
__u32typeType of the input, see Table 2.
__u32audioset

Drivers can enumerate up to 32 video and audio inputs. This field shows which audio inputs were selectable as audio source if this was the currently selected video input. It is a bit mask. The LSB corresponds to audio input 0, the MSB to input 31. Any number of bits can be set, or none.

When the driver does not enumerate audio inputs no bits must be set. Applications shall not interpret this as lack of audio support. Some drivers automatically select audio sources and do not enumerate them since there is no choice anyway.

For details on audio inputs and how to select the current input see Section 1.5.

__u32tunerCapture devices can have zero or more tuners (RF demodulators). When the type is set to V4L2_INPUT_TYPE_TUNER this is an RF connector and this field identifies the tuner. It corresponds to struct v4l2_tuner field index. For details on tuners see Section 1.6.
v4l2_std_idstdEvery video input supports one or more different video standards. This field is a set of all supported standards. For details on video standards and how to switch see Section 1.7.
__u32statusThis field provides status information about the input. See Table 3 for flags. status is only valid when this is the current input.
__u32reserved[4]Reserved for future extensions. Drivers must set the array to zero.

Table 2. Input Types

V4L2_INPUT_TYPE_TUNER1This input uses a tuner (RF demodulator).
V4L2_INPUT_TYPE_CAMERA2Analog baseband input, for example CVBS / Composite Video, S-Video, RGB.

Table 3. Input Status Flags

General
V4L2_IN_ST_NO_POWER0x00000001Attached device is off.
V4L2_IN_ST_NO_SIGNAL0x00000002 
V4L2_IN_ST_NO_COLOR0x00000004The hardware supports color decoding, but does not detect color modulation in the signal.
Analog Video
V4L2_IN_ST_NO_H_LOCK0x00000100No horizontal sync lock.
V4L2_IN_ST_COLOR_KILL0x00000200A color killer circuit automatically disables color decoding when it detects no color modulation. When this flag is set the color killer is enabled and has shut off color decoding.
Digital Video
V4L2_IN_ST_NO_SYNC0x00010000No synchronization lock.
V4L2_IN_ST_NO_EQU0x00020000No equalizer lock.
V4L2_IN_ST_NO_CARRIER0x00040000Carrier recovery failed.
VCR and Set-Top Box
V4L2_IN_ST_MACROVISION0x01000000Macrovision is an analog copy prevention system mangling the video signal to confuse video recorders. When this flag is set Macrovision has been detected.
V4L2_IN_ST_NO_ACCESS0x02000000Conditional access denied.
V4L2_IN_ST_VTR0x04000000VTR time constant. [?]

Return Value

On success 0 is returned, on error -1 and the errno variable is set appropriately:

EINVAL

The struct v4l2_input index is out of bounds.

ioctl VIDIOC_ENUMOUTPUT

Name

VIDIOC_ENUMOUTPUT -- Enumerate video outputs

Synopsis

int ioctl(int fd, int request, struct v4l2_output *argp);

Arguments

fd

File descriptor returned by open().

request

VIDIOC_ENUMOUTPUT

argp

Description

To query the attributes of a video outputs applications initialize the index field of struct v4l2_output and call the VIDIOC_ENUMOUTPUT ioctl with a pointer to this structure. Drivers fill the rest of the structure or return an EINVAL error code when the index is out of bounds. To enumerate all outputs applications shall begin at index zero, incrementing by one until the driver returns EINVAL.

Table 1. struct v4l2_output

__u32indexIdentifies the output, set by the application.
__u8name[32]Name of the video output, a NUL-terminated ASCII string, for example: "Vout". This information is intended for the user, preferably the connector label on the device itself.
__u32typeType of the output, see Table 2.
__u32audioset

Drivers can enumerate up to 32 video and audio outputs. This field shows which audio outputs were selectable as the current output if this was the currently selected video output. It is a bit mask. The LSB corresponds to audio output 0, the MSB to output 31. Any number of bits can be set, or none.

When the driver does not enumerate audio outputs no bits must be set. Applications shall not interpret this as lack of audio support. Drivers may automatically select audio outputs without enumerating them.

For details on audio outputs and how to select the current output see Section 1.5.

__u32modulatorOutput devices can have zero or more RF modulators. When the type is V4L2_OUTPUT_TYPE_MODULATOR this is an RF connector and this field identifies the modulator. It corresponds to struct v4l2_modulator field index. For details on modulators see Section 1.6.
v4l2_std_idstdEvery video output supports one or more different video standards. This field is a set of all supported standards. For details on video standards and how to switch see Section 1.7.
__u32reserved[4]Reserved for future extensions. Drivers must set the array to zero.

Table 2. Output Type

V4L2_OUTPUT_TYPE_MODULATOR1This output is an analog TV modulator.
V4L2_OUTPUT_TYPE_ANALOG2Analog baseband output, for example Composite / CVBS, S-Video, RGB.
V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY3[?]

Return Value

On success 0 is returned, on error -1 and the errno variable is set appropriately:

EINVAL

The struct v4l2_output index is out of bounds.

ioctl VIDIOC_ENUMSTD

Name

VIDIOC_ENUMSTD -- Enumerate supported video standards

Synopsis

int ioctl(int fd, int request, struct v4l2_standard *argp);

Arguments

fd

File descriptor returned by open().

request

VIDIOC_ENUMSTD

argp

Description

To query the attributes of a video standard, especially a custom (driver defined) one, applications initialize the index field of struct v4l2_standard and call the VIDIOC_ENUMSTD ioctl with a pointer to this structure. Drivers fill the rest of the structure or return an EINVAL error code when the index is out of bounds. To enumerate all standards applications shall begin at index zero, incrementing by one until the driver returns EINVAL. Drivers may enumerate a different set of standards after switching the video input or output.[23]

Table 1. struct v4l2_standard

__u32indexNumber of the video standard, set by the application.
v4l2_std_ididThe bits in this field identify the standard as one of the common standards listed in Table 3, or if bits 32 to 63 are set as custom standards. Multiple bits can be set if the hardware does not distinguish between these standards, however separate indices do not indicate the opposite. The id must be unique. No other enumerated v4l2_standard structure, for this input or output anyway, can contain the same set of bits.
__u8name[24]Name of the standard, a NUL-terminated ASCII string, for example: "PAL-B/G", "NTSC Japan". This information is intended for the user.
struct v4l2_fractframeperiodThe frame period (not field period) is numerator / denominator. For example M/NTSC has a frame period of 1001 / 30000 seconds.
__u32framelinesTotal lines per frame including blanking, e. g. 625 for B/PAL.
__u32reserved[4]Reserved for future extensions. Drivers must set the array to zero.

Table 2. struct v4l2_fract

__u32numerator 
__u32denominator 

Table 3. typedef v4l2_std_id

__u64v4l2_std_idThis type is a set, each bit representing another video standard as listed below and in Table 4. The 32 most significant bits are reserved for custom (driver defined) video standards.

#define V4L2_STD_PAL_B          ((v4l2_std_id)0x00000001)
#define V4L2_STD_PAL_B1         ((v4l2_std_id)0x00000002)
#define V4L2_STD_PAL_G          ((v4l2_std_id)0x00000004)
#define V4L2_STD_PAL_H          ((v4l2_std_id)0x00000008)
#define V4L2_STD_PAL_I          ((v4l2_std_id)0x00000010)
#define V4L2_STD_PAL_D          ((v4l2_std_id)0x00000020)
#define V4L2_STD_PAL_D1         ((v4l2_std_id)0x00000040)
#define V4L2_STD_PAL_K          ((v4l2_std_id)0x00000080)

#define V4L2_STD_PAL_M          ((v4l2_std_id)0x00000100)
#define V4L2_STD_PAL_N          ((v4l2_std_id)0x00000200)
#define V4L2_STD_PAL_Nc         ((v4l2_std_id)0x00000400)
#define V4L2_STD_PAL_60         ((v4l2_std_id)0x00000800)

V4L2_STD_PAL_60 is a hybrid standard with 525 lines, 60 Hz refresh rate, and PAL color modulation with a 4.43 MHz color subcarrier. Some PAL video recorders can play back NTSC tapes in this mode for display on a 50/60 Hz agnostic PAL TV.

#define V4L2_STD_NTSC_M         ((v4l2_std_id)0x00001000)
#define V4L2_STD_NTSC_M_JP      ((v4l2_std_id)0x00002000)
#define V4L2_STD_NTSC_443       ((v4l2_std_id)0x00004000)

V4L2_STD_NTSC_443 is a hybrid standard with 525 lines, 60 Hz refresh rate, and NTSC color modulation with a 4.43 MHz color subcarrier.

#define V4L2_STD_NTSC_M_KR      ((v4l2_std_id)0x00008000)

#define V4L2_STD_SECAM_B        ((v4l2_std_id)0x00010000)
#define V4L2_STD_SECAM_D        ((v4l2_std_id)0x00020000)
#define V4L2_STD_SECAM_G        ((v4l2_std_id)0x00040000)
#define V4L2_STD_SECAM_H        ((v4l2_std_id)0x00080000)
#define V4L2_STD_SECAM_K        ((v4l2_std_id)0x00100000)
#define V4L2_STD_SECAM_K1       ((v4l2_std_id)0x00200000)
#define V4L2_STD_SECAM_L        ((v4l2_std_id)0x00400000)
#define V4L2_STD_SECAM_LC       ((v4l2_std_id)0x00800000)

/* ATSC/HDTV */
#define V4L2_STD_ATSC_8_VSB     ((v4l2_std_id)0x01000000)
#define V4L2_STD_ATSC_16_VSB    ((v4l2_std_id)0x02000000)

V4L2_STD_ATSC_8_VSB and V4L2_STD_ATSC_16_VSB are U.S. terrestrial digital TV standards. Presently the V4L2 API does not support digital TV. See also the Linux DVB API at http://linuxtv.org.

#define V4L2_STD_PAL_BG         (V4L2_STD_PAL_B         |\
                                 V4L2_STD_PAL_B1        |\
                                 V4L2_STD_PAL_G)
#define V4L2_STD_B              (V4L2_STD_PAL_B         |\
                                 V4L2_STD_PAL_B1        |\
                                 V4L2_STD_SECAM_B)
#define V4L2_STD_GH             (V4L2_STD_PAL_G         |\
                                 V4L2_STD_PAL_H         |\
                                 V4L2_STD_SECAM_G       |\
                                 V4L2_STD_SECAM_H)
#define V4L2_STD_PAL_DK         (V4L2_STD_PAL_D         |\
                                 V4L2_STD_PAL_D1        |\
                                 V4L2_STD_PAL_K)
#define V4L2_STD_PAL            (V4L2_STD_PAL_BG        |\
                                 V4L2_STD_PAL_DK        |\
                                 V4L2_STD_PAL_H         |\
                                 V4L2_STD_PAL_I)
#define V4L2_STD_NTSC           (V4L2_STD_NTSC_M        |\
                                 V4L2_STD_NTSC_M_JP     |\
                                 V4L2_STD_NTSC_M_KR)
#define V4L2_STD_MN             (V4L2_STD_PAL_M         |\
                                 V4L2_STD_PAL_N         |\
                                 V4L2_STD_PAL_Nc        |\
                                 V4L2_STD_NTSC)
#define V4L2_STD_SECAM_DK       (V4L2_STD_SECAM_D       |\
                                 V4L2_STD_SECAM_K       |\
                                 V4L2_STD_SECAM_K1)
#define V4L2_STD_DK             (V4L2_STD_PAL_DK        |\
                                 V4L2_STD_SECAM_DK)

#define V4L2_STD_SECAM          (V4L2_STD_SECAM_B       |\
                                 V4L2_STD_SECAM_G       |\
                                 V4L2_STD_SECAM_H       |\
                                 V4L2_STD_SECAM_DK      |\
                                 V4L2_STD_SECAM_L       |\
                                 V4L2_STD_SECAM_LC)

#define V4L2_STD_525_60         (V4L2_STD_PAL_M         |\
                                 V4L2_STD_PAL_60        |\
                                 V4L2_STD_NTSC          |\
                                 V4L2_STD_NTSC_443)
#define V4L2_STD_625_50         (V4L2_STD_PAL           |\
                                 V4L2_STD_PAL_N         |\
                                 V4L2_STD_PAL_Nc        |\
                                 V4L2_STD_SECAM)

#define V4L2_STD_UNKNOWN        0
#define V4L2_STD_ALL            (V4L2_STD_525_60        |\
                                 V4L2_STD_625_50)

Table 4. Video Standards (based on [ITU BT.470])

Characteristics

M/NTSCa

M/PAL

N/PALb

B, B1, G/PALD, D1, K/PALH/PALI/PALB, G/SECAMD, K/SECAMK1/SECAML/SECAM
Frame lines525625
Frame period (s)1001/300001/25
Chrominance sub-carrier frequency (Hz)3579545 ± 103579611.49 ± 104433618.75 ± 5 (3582056.25 ± 5)4433618.75 ± 54433618.75 ± 1fOR = 4406250 ± 2000, fOB = 4250000 ± 2000
Nominal radio-frequency channel bandwidth (MHz)666B: 7; B1, G: 88888888
Sound carrier relative to vision carrier (MHz)+ 4.5+ 4.5+ 4.5

+ 5.5 ± 0.001 c d e f

+ 6.5 ± 0.001+ 5.5+ 5.9996 ± 0.0005+ 5.5 ± 0.001+ 6.5 ± 0.001+ 6.5

+ 6.5 g

Notes:
a. Japan uses a standard similar to M/NTSC (V4L2_STD_NTSC_M_JP).
b. The values in brackets apply to the combination N/PAL a.k.a. NC used in Argentina (V4L2_STD_PAL_Nc).
c. In the Federal Republic of Germany, Austria, Italy, the Netherlands, Slovakia and Switzerland a system of two sound carriers is used, the frequency of the second carrier being 242.1875 kHz above the frequency of the first sound carrier. For stereophonic sound transmissions a similar system is used in Australia.
d. New Zealand uses a sound carrier displaced 5.4996 ± 0.0005 MHz from the vision carrier.
e. In Denmark, Finland, New Zealand, Sweden and Spain a system of two sound carriers is used. In Iceland, Norway and Poland the same system is being introduced. The second carrier is 5.85 MHz above the vision carrier and is DQPSK modulated with 728 kbit/s sound and data multiplex. (NICAM system)
f. In the United Kingdom, a system of two sound carriers is used. The second sound carrier is 6.552 MHz above the vision carrier and is DQPSK modulated with a 728 kbit/s sound and data multiplex able to carry two sound channels. (NICAM system)
g. In France, a digital carrier 5.85 MHz away from the vision carrier may be used in addition to the main sound carrier. It is modulated in differentially encoded QPSK with a 728 kbit/s sound and data multiplexer capable of carrying two sound channels. (NICAM system)

Return Value

On success 0 is returned, on error -1 and the errno variable is set appropriately:

EINVAL

The struct v4l2_standard index is out of bounds.

ioctl VIDIOC_G_AUDIO, VIDIOC_S_AUDIO

Name

VIDIOC_G_AUDIO, VIDIOC_S_AUDIO -- Query or select the current audio input and its attributes

Synopsis

int ioctl(int fd, int request, struct v4l2_audio *argp);

int ioctl(int fd, int request, const struct v4l2_audio *argp);

Arguments

fd

File descriptor returned by open().

request

VIDIOC_G_AUDIO, VIDIOC_S_AUDIO

argp

Description

To query the current audio input applications zero out the reserved array of a struct v4l2_audio and call the VIDIOC_G_AUDIO ioctl with a pointer to this structure. Drivers fill the rest of the structure or return an EINVAL error code when the device has no audio inputs, or none which combine with the current video input.

Audio inputs have one writable property, the audio mode. To select the current audio input and change the audio mode, applications initialize the index and mode fields, and the reserved array of a v4l2_audio structure and call the VIDIOC_S_AUDIO ioctl. Drivers may switch to a different audio mode if the request cannot be satisfied. However, this is a write-only ioctl, it does not return the actual new audio mode.

Table 1. struct v4l2_audio

__u32indexIdentifies the audio input, set by the driver or application.
__u8name[32]Name of the audio input, a NUL-terminated ASCII string, for example: "Line In". This information is intended for the user, preferably the connector label on the device itself.
__u32capabilityAudio capability flags, see Table 2.
__u32modeAudio mode flags set by drivers and applications (on VIDIOC_S_AUDIO ioctl), see Table 3.
__u32reserved[2]Reserved for future extensions. Drivers and applications must set the array to zero.

Table 2. Audio Capability Flags

V4L2_AUDCAP_STEREO0x00001This is a stereo input. The flag is intended to automatically disable stereo recording etc. when the signal is always monaural. The API provides no means to detect if stereo is received, unless the audio input belongs to a tuner.
V4L2_AUDCAP_AVL0x00002Automatic Volume Level mode is supported.

Table 3. Audio Mode Flags

V4L2_AUDMODE_AVL0x00001AVL mode is on.

Return Value

On success 0 is returned, on error -1 and the errno variable is set appropriately:

EINVAL

No audio inputs combine with the current video input, or the number of the selected audio input is out of bounds or it does not combine, or there are no audio inputs at all and the ioctl is not supported.

EBUSY

I/O is in progress, the input cannot be switched.

ioctl VIDIOC_G_AUDOUT, VIDIOC_S_AUDOUT

Name

VIDIOC_G_AUDOUT, VIDIOC_S_AUDOUT -- Query or select the current audio output

Synopsis

int ioctl(int fd, int request, struct v4l2_audioout *argp);

int ioctl(int fd, int request, const struct v4l2_audioout *argp);

Arguments

fd

File descriptor returned by open().

request

VIDIOC_G_AUDOUT, VIDIOC_S_AUDOUT

argp

Description

To query the current audio output applications zero out the reserved array of a struct v4l2_audioout and call the VIDIOC_G_AUDOUT ioctl with a pointer to this structure. Drivers fill the rest of the structure or return an EINVAL error code when the device has no audio inputs, or none which combine with the current video output.

Audio outputs have no writable properties. Nevertheless, to select the current audio output applications can initialize the index field and reserved array (which in the future may contain writable properties) of a v4l2_audioout structure and call the VIDIOC_S_AUDOUT ioctl. Drivers switch to the requested output or return the EINVAL error code when the index is out of bounds. This is a write-only ioctl, it does not return the current audio output attributes as VIDIOC_G_AUDOUT does.

Note connectors on a TV card to loop back the received audio signal to a sound card are not audio outputs in this sense.

Table 1. struct v4l2_audioout

__u32indexIdentifies the audio output, set by the driver or application.
__u8name[32]Name of the audio output, a NUL-terminated ASCII string, for example: "Line Out". This information is intended for the user, preferably the connector label on the device itself.
__u32capabilityAudio capability flags, none defined yet. Drivers must set this field to zero.
__u32modeAudio mode, none defined yet. Drivers and applications (on VIDIOC_S_AUDOUT) must set this field to zero.
__u32reserved[2]Reserved for future extensions. Drivers and applications must set the array to zero.

Return Value

On success 0 is returned, on error -1 and the errno variable is set appropriately:

EINVAL

No audio outputs combine with the current video output, or the number of the selected audio output is out of bounds or it does not combine, or there are no audio outputs at all and the ioctl is not supported.

EBUSY

I/O is in progress, the output cannot be switched.

ioctl VIDIOC_G_CHIP_IDENT

Name

VIDIOC_G_CHIP_IDENT -- Identify the chips on a TV card

Synopsis

int ioctl(int fd, int request, struct v4l2_chip_ident *argp);

Arguments

fd

File descriptor returned by open().

request

VIDIOC_G_CHIP_IDENT

argp

Description

Experimental: This is an experimental interface and may change in the future.

For driver debugging purposes this ioctl allows test applications to query the driver about the chips present on the TV card. Regular applications should not use it. When you found a chip specific bug, please contact the Video4Linux mailing list (https://listman.redhat.com/mailman/listinfo/video4linux-list) so it can be fixed.

To query the driver applications must initialize the match_type and match_chip fields of a struct v4l2_chip_ident and call VIDIOC_G_CHIP_IDENT with a pointer to this structure. On success the driver stores information about the selected chip in the ident and revision fields. On failure the structure remains unchanged.

When match_type is V4L2_CHIP_MATCH_HOST, match_chip selects the nth non-I2C chip on the TV card. You can enumerate all chips by starting at zero and incrementing match_chip by one until VIDIOC_G_CHIP_IDENT fails with an EINVAL error code. Drivers may also interpret match_chip as a random ID, but we recommend against that. The number zero always selects the host chip, e. g. the chip connected to the PCI bus.

When match_type is V4L2_CHIP_MATCH_I2C_DRIVER, match_chip contains a driver ID as defined in the linux/i2c-id.h header file. For instance I2C_DRIVERID_SAA7127 will match any chip supported by the saa7127 driver, regardless of its I2C bus address. When multiple chips supported by the same driver are present, the ioctl will return V4L2_IDENT_AMBIGUOUS in the ident field.

When match_type is V4L2_CHIP_MATCH_I2C_ADDR, match_chip selects a chip by its 7 bit I2C bus address.

On success, the ident field will contain a chip ID from the Linux media/v4l2-chip-ident.h header file, and the revision field will contain a driver specific value, or zero if no particular revision is associated with this chip.

When the driver could not identify the selected chip, ident will contain V4L2_IDENT_UNKNOWN. When no chip matched match_type and match_chip, the ioctl will succeed but the ident field will contain V4L2_IDENT_NONE. If multiple chips matched, ident will contain V4L2_IDENT_AMBIGUOUS. In all these cases the revision field remains unchanged.

This ioctl is optional, not all drivers may support it. It was introduced in Linux 2.6.21.

We recommended the v4l2-dbg utility over calling this ioctl directly. It is available from the LinuxTV v4l-dvb repository; see http://linuxtv.org/repo/ for access instructions.

Table 1. struct v4l2_chip_ident

__u32match_typeSee Table 2 for a list of possible types.
__u32match_chipMatch a chip by this number, interpreted according to the match_type field.
__u32identA chip identifier as defined in the Linux media/v4l2-chip-ident.h header file, or one of the values from Table 3.
__u32revisionA chip revision, chip and driver specific.

Table 2. Chip Match Types

V4L2_CHIP_MATCH_HOST0Match the nth chip on the card, zero for the host chip. Does not match I2C chips.
V4L2_CHIP_MATCH_I2C_DRIVER1Match an I2C chip by its driver ID from the linux/i2c-id.h header file.
V4L2_CHIP_MATCH_I2C_ADDR2Match a chip by its 7 bit I2C bus address.

Table 3. Chip Identifiers

V4L2_IDENT_NONE0No chip matched.
V4L2_IDENT_AMBIGUOUS1Multiple chips matched.
V4L2_IDENT_UNKNOWN2A chip is present at this address, but the driver could not identify it.

Return Value

On success 0 is returned, on error -1 and the errno variable is set appropriately:

EINVAL

The driver does not support this ioctl, or the match_type is invalid.

ioctl VIDIOC_G_CROP, VIDIOC_S_CROP

Name

VIDIOC_G_CROP, VIDIOC_S_CROP -- Get or set the current cropping rectangle

Synopsis

int ioctl(int fd, int request, struct v4l2_crop *argp);

int ioctl(int fd, int request, const struct v4l2_crop *argp);

Arguments

fd

File descriptor returned by open().

request

VIDIOC_G_CROP, VIDIOC_S_CROP

argp

Description

To query the cropping rectangle size and position applications set the type field of a v4l2_crop structure to the respective buffer (stream) type and call the VIDIOC_G_CROP ioctl with a pointer to this structure. The driver fills the rest of the structure or returns the EINVAL error code if cropping is not supported.

To change the cropping rectangle applications initialize the type and struct v4l2_rect substructure named c of a v4l2_crop structure and call the VIDIOC_S_CROP ioctl with a pointer to this structure.

The driver first adjusts the requested dimensions against hardware limits, i. e. the bounds given by the capture/output window, and it rounds to the closest possible values of horizontal and vertical offset, width and height. In particular the driver must round the vertical offset of the cropping rectangle to frame lines modulo two, such that the field order cannot be confused.

Second the driver adjusts the image size (the opposite rectangle of the scaling process, source or target depending on the data direction) to the closest size possible while maintaining the current horizontal and vertical scaling factor.

Finally the driver programs the hardware with the actual cropping and image parameters. VIDIOC_S_CROP is a write-only ioctl, it does not return the actual parameters. To query them applications must call VIDIOC_G_CROP and VIDIOC_G_FMT. When the parameters are unsuitable the application may modify the cropping or image parameters and repeat the cycle until satisfactory parameters have been negotiated.

When cropping is not supported then no parameters are changed and VIDIOC_S_CROP returns the EINVAL error code.

Table 1. struct v4l2_crop

enum v4l2_buf_typetypeType of the data stream, set by the application. Only these types are valid here: V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_BUF_TYPE_VIDEO_OUTPUT, V4L2_BUF_TYPE_VIDEO_OVERLAY, and custom (driver defined) types with code V4L2_BUF_TYPE_PRIVATE and higher.
struct v4l2_rectcCropping rectangle. The same co-ordinate system as for struct v4l2_cropcap bounds is used.

Return Value

On success 0 is returned, on error -1 and the errno variable is set appropriately:

EINVAL

Cropping is not supported.

ioctl VIDIOC_G_CTRL, VIDIOC_S_CTRL

Name

VIDIOC_G_CTRL, VIDIOC_S_CTRL -- Get or set the value of a control

Synopsis

int ioctl(int fd, int request, struct v4l2_control *argp);

Arguments

fd

File descriptor returned by open().

request

VIDIOC_G_CTRL, VIDIOC_S_CTRL

argp

Description

To get the current value of a control applications initialize the id field of a struct v4l2_control and call the VIDIOC_G_CTRL ioctl with a pointer to this structure. To change the value of a control applications initialize the id and value fields of a struct v4l2_control and call the VIDIOC_S_CTRL ioctl.

When the id is invalid drivers return an EINVAL error code. When the value is out of bounds drivers can choose to take the closest valid value or return an ERANGE error code, whatever seems more appropriate. However, VIDIOC_S_CTRL is a write-only ioctl, it does not return the actual new value.

These ioctls work only with user controls. For other control classes the VIDIOC_G_EXT_CTRLS, VIDIOC_S_EXT_CTRLS or VIDIOC_TRY_EXT_CTRLS must be used.

Table 1. struct v4l2_control

__u32idIdentifies the control, set by the application.
__s32valueNew value or current value.

Return Value

On success 0 is returned, on error -1 and the errno variable is set appropriately:

EINVAL

The struct v4l2_control id is invalid.

ERANGE

The struct v4l2_control value is out of bounds.

EBUSY

The control is temporarily not changeable, possibly because another applications took over control of the device function this control belongs to.

ioctl VIDIOC_G_ENC_INDEX

Name

VIDIOC_G_ENC_INDEX -- Get meta data about a compressed video stream

Synopsis

int ioctl(int fd, int request, struct v4l2_enc_idx *argp);

Arguments

fd

File descriptor returned by open().

request

VIDIOC_G_ENC_INDEX

argp

Description

Experimental: This is an experimental interface and may change in the future.

The VIDIOC_G_ENC_INDEX ioctl provides meta data about a compressed video stream the same or another application currently reads from the driver, which is useful for random access into the stream without decoding it.

To read the data applications must call VIDIOC_G_ENC_INDEX with a pointer to a struct v4l2_enc_idx. On success the driver fills the entry array, stores the number of elements written in the entries field, and initializes the entries_cap field.

Each element of the entry array contains meta data about one picture. A VIDIOC_G_ENC_INDEX call reads up to V4L2_ENC_IDX_ENTRIES entries from a driver buffer, which can hold up to entries_cap entries. This number can be lower or higher than V4L2_ENC_IDX_ENTRIES, but not zero. When the application fails to read the meta data in time the oldest entries will be lost. When the buffer is empty or no capturing/encoding is in progress, entries will be zero.

Currently this ioctl is only defined for MPEG-2 program streams and video elementary streams.

Table 1. struct v4l2_enc_idx

__u32entriesThe number of entries the driver stored in the entry array.
__u32entries_capThe number of entries the driver can buffer. Must be greater than zero.
__u32reserved[4]Reserved for future extensions. Drivers must set the array to zero.
struct v4l2_enc_idx_entryentry[V4L2_ENC_IDX_ENTRIES]Meta data about a compressed video stream. Each element of the array corresponds to one picture, sorted in ascending order by their offset.

Table 2. struct v4l2_enc_idx_entry

__u64offsetThe offset in bytes from the beginning of the compressed video stream to the beginning of this picture, that is a PES packet header as defined in ISO 13818-1 or a picture header as defined in ISO 13818-2. When the encoder is stopped, the driver resets the offset to zero.
__u64ptsThe 33 bit Presentation Time Stamp of this picture as defined in ISO 13818-1.
__u32lengthThe length of this picture in bytes.
__u32flagsFlags containing the coding type of this picture, see Table 3.
__u32reserved[2]Reserved for future extensions. Drivers must set the array to zero.

Table 3. Index Entry Flags

V4L2_ENC_IDX_FRAME_I0x00This is an Intra-coded picture.
V4L2_ENC_IDX_FRAME_P0x01This is a Predictive-coded picture.
V4L2_ENC_IDX_FRAME_B0x02This is a Bidirectionally predictive-coded picture.
V4L2_ENC_IDX_FRAME_MASK0x0FAND the flags field with this mask to obtain the picture coding type.

Return Value

On success 0 is returned, on error -1 and the errno variable is set appropriately:

EINVAL

The driver does not support this ioctl.

ioctl VIDIOC_G_EXT_CTRLS, VIDIOC_S_EXT_CTRLS, VIDIOC_TRY_EXT_CTRLS

Name

VIDIOC_G_EXT_CTRLS, VIDIOC_S_EXT_CTRLS, VIDIOC_TRY_EXT_CTRLS -- Get or set the value of several controls, try control values

Synopsis

int ioctl(int fd, int request, struct v4l2_ext_controls *argp);

Arguments

fd

File descriptor returned by open().

request

VIDIOC_G_EXT_CTRLS, VIDIOC_S_EXT_CTRLS, VIDIOC_TRY_EXT_CTRLS

argp

Description

These ioctls allow the caller to get or set multiple controls atomically. Control IDs are grouped into control classes (see Table 3) and all controls in the control array must belong to the same control class.

Applications must always fill in the count, ctrl_class, controls and reserved fields of struct v4l2_ext_controls, and initialize the struct v4l2_ext_control array pointed to by the controls fields.

To get the current value of a set of controls applications initialize the id field of each struct v4l2_ext_control and call the VIDIOC_G_EXT_CTRLS ioctl.

To change the value of a set of controls applications initialize the id and value fields of a struct v4l2_ext_control and call the VIDIOC_S_EXT_CTRLS ioctl. The controls will only be set if all control values are valid.

To check if the a set of controls have correct values applications initialize the id and value fields of a struct v4l2_ext_control and call the VIDIOC_TRY_EXT_CTRLS ioctl. It is up to the driver whether wrong values are automatically adjusted to a valid value or if an error is returned.

When the id or ctrl_class is invalid drivers return an EINVAL error code. When the value is out of bounds drivers can choose to take the closest valid value or return an ERANGE error code, whatever seems more appropriate. In the first case the new value is set in struct v4l2_ext_control.

The driver will only set/get these controls if all control values are correct. This prevents the situation where only some of the controls were set/get. Only low-level errors (e. g. a failed i2c command) can still cause this situation.

Table 1. struct v4l2_ext_control

__u32id Identifies the control, set by the application.
__u32reserved2[2] Reserved for future extensions. Drivers and applications must set the array to zero.
union(anonymous)  
 __s32valueNew value or current value.
 __s64value64New value or current value.
 void *reservedReserved for future pointer-type controls. Currently unused.

Table 2. struct v4l2_ext_controls

__u32ctrl_classThe control class to which all controls belong, see Table 3.
__u32countThe number of controls in the controls array. May also be zero.
__u32error_idxSet by the driver in case of an error. It is the index of the control causing the error or equal to 'count' when the error is not associated with a particular control. Undefined when the ioctl returns 0 (success).
__u32reserved[2]Reserved for future extensions. Drivers and applications must set the array to zero.
struct v4l2_ext_control *controlsPointer to an array of count v4l2_ext_control structures. Ignored if count equals zero.

Table 3. Control classes

V4L2_CTRL_CLASS_USER0x980000The class containing user controls. These controls are described in Section 1.8. All controls that can be set using the VIDIOC_S_CTRL and VIDIOC_G_CTRL ioctl belong to this class.
V4L2_CTRL_CLASS_MPEG0x990000The class containing MPEG compression controls. These controls are described in section Section 1.9.5.

Return Value

On success 0 is returned, on error -1 and the errno variable is set appropriately:

EINVAL

The struct v4l2_ext_control id is invalid or the struct v4l2_ext_controls ctrl_class is invalid. This error code is also returned by the VIDIOC_S_EXT_CTRLS and VIDIOC_TRY_EXT_CTRLS ioctls if two or more control values are in conflict.

ERANGE

The struct v4l2_ext_control value is out of bounds.

EBUSY

The control is temporarily not changeable, possibly because another applications took over control of the device function this control belongs to.

ioctl VIDIOC_G_FBUF, VIDIOC_S_FBUF

Name

VIDIOC_G_FBUF, VIDIOC_S_FBUF -- Get or set frame buffer overlay parameters

Synopsis

int ioctl(int fd, int request, struct v4l2_framebuffer *argp);

int ioctl(int fd, int request, const struct v4l2_framebuffer *argp);

Arguments

fd

File descriptor returned by open().

request

VIDIOC_G_FBUF, VIDIOC_S_FBUF

argp

Description

Applications can use the VIDIOC_G_FBUF and VIDIOC_S_FBUF ioctl to get and set the framebuffer parameters for a Video Overlay or Video Output Overlay (OSD). The type of overlay is implied by the device type (capture or output device) and can be determined with the VIDIOC_QUERYCAP ioctl. One /dev/videoN device must not support both kinds of overlay.

The V4L2 API distinguishes destructive and non-destructive overlays. A destructive overlay copies captured video images into the video memory of a graphics card. A non-destructive overlay blends video images into a VGA signal or graphics into a video signal. Video Output Overlays are always non-destructive.

To get the current parameters applications call the VIDIOC_G_FBUF ioctl with a pointer to a v4l2_framebuffer structure. The driver fills all fields of the structure or returns an EINVAL error code when overlays are not supported.

To set the parameters for a Video Output Overlay, applications must initialize the flags field of a struct v4l2_framebuffer. Since the framebuffer is implemented on the TV card all other parameters are determined by the driver. When an application calls VIDIOC_S_FBUF with a pointer to this structure, the driver prepares for the overlay and returns the framebuffer parameters as VIDIOC_G_FBUF does, or it returns an error code.

To set the parameters for a non-destructive Video Overlay, applications must initialize the flags field, the fmt substructure, and call VIDIOC_S_FBUF. Again the driver prepares for the overlay and returns the framebuffer parameters as VIDIOC_G_FBUF does, or it returns an error code.

For a destructive Video Overlay applications must additionally provide a base address. Setting up a DMA to a random memory location can jeopardize the system security, its stability or even damage the hardware, therefore only the superuser can set the parameters for a destructive video overlay.

Table 1. struct v4l2_framebuffer

__u32capability Overlay capability flags set by the driver, see Table 2.
__u32flags Overlay control flags set by application and driver, see Table 3
void *base 

Physical base address of the framebuffer, that is the address of the pixel in the top left corner of the framebuffer.a

This field is irrelevant to non-destructive Video Overlays. For destructive Video Overlays applications must provide a base address. The driver may accept only base addresses which are a multiple of two, four or eight bytes. For Video Output Overlays the driver must return a valid base address, so applications can find the corresponding Linux framebuffer device (see Section 4.4).

struct v4l2_pix_formatfmt Layout of the frame buffer. The v4l2_pix_format structure is defined in Chapter 2, for clarification the fields and acceptable values are listed below:
 __u32widthWidth of the frame buffer in pixels.
 __u32heightHeight of the frame buffer in pixels.
 __u32pixelformat

The pixel format of the framebuffer.

For non-destructive Video Overlays this field only defines a format for the struct v4l2_window chromakey field.

For destructive Video Overlays applications must initialize this field. For Video Output Overlays the driver must return a valid format.

Usually this is an RGB format (for example V4L2_PIX_FMT_RGB565) but YUV formats (only packed YUV formats when chroma keying is used, not including V4L2_PIX_FMT_YUYV and V4L2_PIX_FMT_UYVY) and the V4L2_PIX_FMT_PAL8 format are also permitted. The behavior of the driver when an application requests a compressed format is undefined. See Chapter 2 for information on pixel formats.

 enum v4l2_fieldfieldDrivers and applications shall ignore this field. If applicable, the field order is selected with the VIDIOC_S_FMT ioctl, using the field field of struct v4l2_window.
 __u32bytesperlineDistance in bytes between the leftmost pixels in two adjacent lines.

This field is irrelevant to non-destructive Video Overlays.

For destructive Video Overlays both applications and drivers can set this field to request padding bytes at the end of each line. Drivers however may ignore the requested value, returning width times bytes-per-pixel or a larger value required by the hardware. That implies applications can just set this field to zero to get a reasonable default.

For Video Output Overlays the driver must return a valid value.

Video hardware may access padding bytes, therefore they must reside in accessible memory. Consider for example the case where padding bytes after the last line of an image cross a system page boundary. Capture devices may write padding bytes, the value is undefined. Output devices ignore the contents of padding bytes.

When the image format is planar the bytesperline value applies to the largest plane and is divided by the same factor as the width field for any smaller planes. For example the Cb and Cr planes of a YUV 4:2:0 image have half as many padding bytes following each line as the Y plane. To avoid ambiguities drivers must return a bytesperline value rounded up to a multiple of the scale factor.

 __u32sizeimage

This field is irrelevant to non-destructive Video Overlays. For destructive Video Overlays applications must initialize this field. For Video Output Overlays the driver must return a valid format.

Together with base it defines the framebuffer memory accessible by the driver.

 enum v4l2_colorspacecolorspaceThis information supplements the pixelformat and must be set by the driver, see Section 2.2.
 __u32privReserved for additional information about custom (driver defined) formats. When not used drivers and applications must set this field to zero.
Notes:
a. A physical base address may not suit all platforms. GK notes in theory we should pass something like PCI device + memory region + offset instead. If you encounter problems please discuss on the Video4Linux mailing list: https://listman.redhat.com/mailman/listinfo/video4linux-list.

Table 2. Frame Buffer Capability Flags

V4L2_FBUF_CAP_EXTERNOVERLAY0x0001The device is capable of non-destructive overlays. When the driver clears this flag, only destructive overlays are supported. There are no drivers yet which support both destructive and non-destructive overlays.
V4L2_FBUF_CAP_CHROMAKEY0x0002The device supports clipping by chroma-keying the images. That is, image pixels replace pixels in the VGA or video signal only where the latter assume a certain color. Chroma-keying makes no sense for destructive overlays.
V4L2_FBUF_CAP_LIST_CLIPPING0x0004The device supports clipping using a list of clip rectangles.
V4L2_FBUF_CAP_BITMAP_CLIPPING0x0008The device supports clipping using a bit mask.
V4L2_FBUF_CAP_LOCAL_ALPHA0x0010The device supports clipping/blending using the alpha channel of the framebuffer or VGA signal. Alpha blending makes no sense for destructive overlays.
V4L2_FBUF_CAP_GLOBAL_ALPHA0x0020The device supports alpha blending using a global alpha value. Alpha blending makes no sense for destructive overlays.
V4L2_FBUF_CAP_LOCAL_INV_ALPHA0x0040The device supports clipping/blending using the inverted alpha channel of the framebuffer or VGA signal. Alpha blending makes no sense for destructive overlays.

Table 3. Frame Buffer Flags

V4L2_FBUF_FLAG_PRIMARY0x0001The framebuffer is the primary graphics surface. In other words, the overlay is destructive. [?]
V4L2_FBUF_FLAG_OVERLAY0x0002The frame buffer is an overlay surface the same size as the capture. [?]
The purpose of V4L2_FBUF_FLAG_PRIMARY and V4L2_FBUF_FLAG_OVERLAY was never quite clear. Most drivers seem to ignore these flags. For compatibility with the bttv driver applications should set the V4L2_FBUF_FLAG_OVERLAY flag.
V4L2_FBUF_FLAG_CHROMAKEY0x0004Use chroma-keying. The chroma-key color is determined by the chromakey field of struct v4l2_window and negotiated with the VIDIOC_S_FMT ioctl, see Section 4.2 and Section 4.4.
There are no flags to enable clipping using a list of clip rectangles or a bitmap. These methods are negotiated with the VIDIOC_S_FMT ioctl, see Section 4.2 and Section 4.4.
V4L2_FBUF_FLAG_LOCAL_ALPHA0x0008Use the alpha channel of the framebuffer to clip or blend framebuffer pixels with video images. The blend function is: output = framebuffer pixel * alpha + video pixel * (1 - alpha). The actual alpha depth depends on the framebuffer pixel format.
V4L2_FBUF_FLAG_GLOBAL_ALPHA0x0010Use a global alpha value to blend the framebuffer with video images. The blend function is: output = (framebuffer pixel * alpha + video pixel * (255 - alpha)) / 255. The alpha value is determined by the global_alpha field of struct v4l2_window and negotiated with the VIDIOC_S_FMT ioctl, see Section 4.2 and Section 4.4.
V4L2_FBUF_FLAG_LOCAL_INV_ALPHA0x0020Like V4L2_FBUF_FLAG_LOCAL_ALPHA, use the alpha channel of the framebuffer to clip or blend framebuffer pixels with video images, but with an inverted alpha value. The blend function is: output = framebuffer pixel * (1 - alpha) + video pixel * alpha. The actual alpha depth depends on the framebuffer pixel format.

Return Value

On success 0 is returned, on error -1 and the errno variable is set appropriately:

EPERM

VIDIOC_S_FBUF can only be called by a privileged user to negotiate the parameters for a destructive overlay.

EBUSY

The framebuffer parameters cannot be changed at this time because overlay is already enabled, or capturing is enabled and the hardware cannot capture and overlay simultaneously.

EINVAL

The ioctl is not supported or the VIDIOC_S_FBUF parameters are unsuitable.

ioctl VIDIOC_G_FMT, VIDIOC_S_FMT, VIDIOC_TRY_FMT

Name

VIDIOC_G_FMT, VIDIOC_S_FMT, VIDIOC_TRY_FMT -- Get or set the data format, try a format

Synopsis

int ioctl(int fd, int request, struct v4l2_format *argp);

Arguments

fd

File descriptor returned by open().

request

VIDIOC_G_FMT, VIDIOC_S_FMT, VIDIOC_TRY_FMT

argp

Description

These ioctls are used to negotiate the format of data (typically image format) exchanged between driver and application.

To query the current parameters applications set the type field of a struct v4l2_format to the respective buffer (stream) type. For example video capture devices use V4L2_BUF_TYPE_VIDEO_CAPTURE. When the application calls the VIDIOC_G_FMT ioctl with a pointer to this structure the driver fills the respective member of the fmt union. In case of video capture devices that is the struct v4l2_pix_format pix member. When the requested buffer type is not supported drivers return an EINVAL error code.

To change the current format parameters applications initialize the type field and all fields of the respective fmt union member. For details see the documentation of the various devices types in Chapter 4. Good practice is to query the current parameters first, and to modify only those par