V4L2 API Detailed Buffer preparation and data read

1. Initialize Memory Mapping or User Pointer I / O.
int ioctl(int fd, int requestbuf, struct v4l2_requestbuffers * argp);
A parameter: open handle is produced ().
Parameter Two: VIDIOC_REQBUFS
Three parameters: in / out structure.
struct v4l2_requestbuffers
{
 __u32 count;
 enum v4l2_buf_type type;
 enum v4l2_memory memory; //Applications set this field to V4L2_MEMORY_MMAP or V4L2_MEMORY_USERPTR
 __u32 reserved[2];
};
 
Note that there are two ways of I / O. Memory Mapping and User Pointer.
Driver's Buffer Memory Mapping the continuous application is a physical memory space (Kernel space). It was assigned at the time of this ioctl call, earlier than required mmap () action to map them to the user space.
 
1.1: Memory Mapping mode Detailed:
When using the Memory Mapping mode, three parameters of each field in the structure of the body need to be set.
 
 
 __u32 count; // time when memory = V4L2_MEMORY_MMAP, here is valid. Show that the number of buffer to the application.
 enum v4l2_buf_type type; // type or Stream Buffer. Here certainly is V4L2_BUF_TYPE_VIDEO_CAPTURE
 enum v4l2_memory memory; // Memory Mapping mode since it is, is set here: V4L2_MEMORY_MMAP


Note: count is input and output functions. Because the number of Buffer you are applying to is not necessarily the Number you entered. So after ioctl execution, driver will apply to the real number of buffer fill this field. This number may be larger than you want to apply, may also be small and may even be 0.
An application can call ioctl again - VIDIOC_REQBUFS to modify the number of buffer. But the premise is already mapped must first release the buffer, you can first munmap, then set the parameter count to zero to release all of the buffer.
 
 
Premise support Memory Mapping I / O approach is: v4l2_capability    support V4L2_CAP_STREAMING.
In this mode, the data itself is not Copy, but the exchange between the user and Kernel mode. Before application wants access to these data, it must call mmap () mapped to user mode.
 
Also note that the ioctl application memory, physical memory, and can not be exchanged into Disk, so be sure to release: munmap () .
 
 
 
1.2: User Pointer mode:
When User Pointer mode, the application to achieve application.
Just filled Type = V4L2_BUF_TYPE_VIDEO_CAPTURE, memory = V4L2_MEMORY_USERPTR
 
 
 
 
2. Ask Buffer state:
int ioctl(int fd, int request, struct v4l2_buffer* argp);
A parameter: open handle is produced ().
Parameter Two: VIDIOC_QUERYBUF
Three parameters: v4l2_buffer structure. (IN / OUT parameter)
 
Note that this is one of ioctl Memory Mapping of I / O methods. User Pointer mode does not require. In Buffer in ioctl- VIDIOC_REQBUFS After creating execution, you can always call this Ioctl get buffer information.
 
We first look at the parameters by v4l2_buffer structure of the three input and output parameters need to enter something, and be able to get any information.
 
struct v4l2_buffer
{
 __u32 index;
 enum v4l2_buf_type type;
 __u32 bytesused;
 __u32 flags;
 enum v4l2_field field;
 struct timeval timestamp;
 struct v4l2_timecode timecode;
 __u32 sequence;
 
 enum v4l2_memory memory;
 union {
 __u32 offset;
 unsigned long userptr;
 } m;
 __u32 length;
 __u32 input;
 __u32 reserved;
};
 
In calling ioctl-- VIDIOC_QUERYBUF , the project has to be written:
enum v4l2_buf_type type; // V4L2_BUF_TYPE_VIDEO_CAPTURE
__u32 index; // here need to explain, because the call ioctl- VIDIOC_REQBUFS when established to count Buffer. Therefore, the effective range is where the index: 0 to count-1.
 
 
In calling ioctl- VIDIOC_QUERYBUF after filling all the information Driver v4l2_buffer structures in the body will be available to users.
If some of the normal:
1. flags 中:V4L2_BUF_FLAG_MAPPED, V4L2_BUF_FLAG_QUEUED and V4L2_BUF_FLAG_DONE被设置。
2. memory medium, V4L2_MEMORY_MMAP is provided.
3.  m.offset , a mapping from the device memory to be head-to-head offset data.
4. length, the length of filling the current Buffer.
5. Other Field is possible to set, there may not be set.
 
 
In this way, mmap () wants some information on a whole. And mmap () after, Device Driver Device Memory or application can be mapped to the user space. Application data can be used. This is ioctl- VIDIOC_QUERYBUF key role.
 
 
 
3. Driver and exchange buffer: 
Camera for capturing apparatus is such, Device Buffer data into the user data obtained. Device data again into the Buffer.
So how do you know which Device Driver Buffer can store the data it? It is currently used in two ioctl-VIDIOC_QBUF, ioctl-VIDIOC_DQBUF.
 
ioctl-VIDIOC_QBUF: Buffer into the specified input queue, ie Device Buffer can be stored indicating that this stuff.
ioctl-VIDIOC_DQBUF: the data extraction output queue buffer.
 
Internal driver managing a two buffer queues, an input queue, an output queue. For capture device, when the input queue after the buffer is filled with data automatically changes the output queue, waiting for later recall VIDIOC_QBUF VIDIOC_DQBUF the call processing data into the input queue buffer again.
 
 
usage:
ioctl--VIDIOC_QBUF:
int ioctl(int fd, int request, struct v4l2_buffer* argp);
A parameter: open handle is produced ().
Parameter Two: VIDIOC_QBUF
Three parameters: v4l2_buffer structure. (IN / OUT parameter)
 
Third parameter IN / OUT parameter. Need to fill
enum v4l2_buf_type type; // V4L2_BUF_TYPE_VIDEO_CAPTURE
__u32 index; // here need to explain, because the call ioctl- VIDIOC_REQBUFS when established to count Buffer. Therefore, the effective range is where the index: 0 to count-1. 
memory: V4L2_MEMORY_MMAP.
 
This structure is specified in the buffer is sent to the output queue, indicating Buffer device can be filled with data.
 
usage:
ioctl--VIDIOC_DQBUF:
int ioctl(int fd, int request, struct v4l2_buffer* argp);
A parameter: open handle is produced ().
Parameter Two: VIDIOC_DQBUF
Three parameters: v4l2_buffer structure. (IN / OUT parameter)
 
Removed from the output queue of a data Buffer. After the data is processed in the Buffer, the Buffer by ioctl- VIDIOC_QBUF into the input queue to again.
 
 
 
 
4. Start and end of capture:
ioctl--VIDIOC_STREAMON. ioctl--VIDIOC_STREAMOFF
 
Very simple call. It is the beginning and the end.

Guess you like

Origin www.cnblogs.com/cyyljw/p/11269859.html