Linux V4L2 notes(2)

Data input/output methods

  • Read/write (by default): data is copied between application and driver.
    • pros: simplest I/O method, requiring little or no setup to exchange data.
    • cons:
      • no meta-information, fram counters, timestamps, are passed. Therefore, there is no way to recognize frame dropping, etc.
      • less efficient, even using DMA to copy, compare to other input/output methods, which merely exchange buffer pointers.
  • Streaming I/O: only pointers to the buffers are exchanged between application and driver, the data itself is not copied.
    • Indicated in capabilities field (V4L2_CAP_STRAMING)of struct v4l2_capability, returned by VIDIOC_QUERYCAP.
    • For user pointers, it has be to determined further, by calling the VIDIOC_REQBUFS ioctl.
    • Two methods
      • Memory mapping: map buffers in device memory, or DMA-able main memory, into the application’s address space.
      • User pointers: buffers are allocated by the application itself, and can reside in virtual or shared memory.
  • Video overlay: Only used if the hardware supports storing captured images directly in video memory of a graphics card.


For memory mapping streaming I/O,


  • ioctl(handle, VIDIOC_REQBUFS, &reqbuf);
    • Ask the driver to allocate buffers, with the number of buffers and type: reqbuf.memory = V4L2_MEMORY_MAP;
    • Can also be used to change the number of buffers, or free the allocated memory.
  • Map the buffers into the application’s address space: mmap();
  • Enqueue all mapped buffers via ioctl VIDIOC_QBUF
  • Start capturing: ioctl VIDIOC_STREAMON
  • enter the read loop: ioctl VIDIOC_DQBUF blocks when no buffer is in the outgoing queue.
    • If O_NONBLOCK is given to the open() function, VIDIOC_DQBUF returns immediately with an EAGAIN error code if no buffer is avialble.
  • Can use VIDIOC_QUERYBUF to query the buffer status: mapped, enqueued, full, empty.
  • Re-enque the buffer when no needed: VIDIOC_QBUF;

The driver:

  • Maintain two buffer queues: incoming and outgoing, organized as FIFOs.
  • Initially, all mapped buffers are in dqueued state, inaccessible by the driver.


For user pointers,


    • Ask the driver to switch into user pointer I/O mode, with reqbuf.memory = V4L2_MEMORY_USERPTR;
  • Allocate the buffers, and pass them to the driver: VIDIOC_QBUF;


V4L2 Buffers

struct v4L2_buffer {

               _u32 index;


               _u32 bytesused;  // for capture, the driver must set this field.

               _u32 flags;   




               _u32 sequence;


               Union m;

                          _u32 offset; // for MMAP streaming;

                                    Userptr; // for USRPTR streaming;

               _u32 length; // size of the buffer (not the payload) in bytes

               _u32 input;



  1. MAPPED;
  2. QUEUED (when this flag is set, the buffer is currently on the incoming queue, and it automatically moves to the outgoing queue after the buffer has been filled(for capture))
  3. DONE: on the outgoing queue.
  4. KEYFRAME, FRAME, BFRAME, TIMECODE (the timecode field is valid)
  5. INPUT

Timecode contains information like 24FPS/30FPS,  etc



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s