Re-write a v4l2 virtual driver _ 3 and v4l2 virtual _ 3

Source: Internet
Author: User

Re-write a v4l2 virtual driver _ 3 and v4l2 virtual _ 3
Introduction

Because both qcom and linux use the v4l2 framework as the drive framework of camera, the following documents are recorded based on the learning notes. This document is a personal study note for "teacher Wei Dongshan video tutorial Phase 3". Thank you very much for your information. This record is only for learning and communication. If your interest is violated, please contact the blogger to delete it.

Poll/select
In the previous article, we mentioned that the application and driver interact with each other through the select/poll mechanism, so as to achieve constant qbuffer and dequebuffer. Therefore, the driver also needs to implement its poll function:
static unsigned int myvivi_poll(struct file *file, struct poll_table_struct *wait){        return videobuf_poll_stream(file, &myvivi_vb_vidqueue, wait);} static const struct v4l2_file_operations myvivi_fops = {                                                                                 .............    .poll       = myvivi_poll,    .............};
In the videobuf_poll_stream function, if no data exists in the queue myvivi_vb_vidqueue, the process goes to the first buffer of the queue and uses queuebuf-> done to sleep.

Timer simulation data
Finally, the timer is used to simulate data generation. First, add a timer to the driver:
static struct timer_list myvivi_timer; static int myvivi_open(struct file *file){    ............    myvivi_timer.expires = jiffies + 1;    add_timer(&myvivi_timer);    ........}static int myvivi_close(struct file *file){    del_timer(&myvivi_timer);    ...........} static int myvivi_init(void){    ............    init_timer(&myvivi_timer);    myvivi_timer.function  = myvivi_timer_function;}
In addition, we also need a local queue that we mentioned earlier and preparations in myvivi_buffer_prepare:
Static struct list_head myvivi_vb_local_queue; static int myvivi_buffer_prepare (struct videobuf_queue * vq, struct videobuf_buffer * vb, enum v4l2_field field) {/* 0. set videobuf */vb-> size = large; vb-> bytesperline = large; vb-> width = myvivi_format.fmt.pix.width; vb-> height = myvivi_format.fmt.pix.height; vb-> field = field; vb-> state = VIDEOBUF_PREPARED; return 0;} static void myvivi_buffer_queue (struct videobuf_queue * vq, struct videobuf_buffer * vb) {vb-> state = VIDEOBUF_QUEUED; /* put videobuf at the end of a local queue * the timer processing function can be used to retrieve videobuf */list_add_tail (& vb-> queue, & myvivi_vb_local_queue) from the local queue );} static int myvivi_init (void ){............. INIT_LIST_HEAD (& myvivi_vb_local_queue );.............}
In the driver entry function, the timer myvivi_timer and the local queue myvivi_vb_local_queue are initialized. In the open function, add_timer and delete the timer in the release function. Then, in the myvivi_buffer_prepare function, fill in the data size, width, and other information to videobuf, And put videobuf in the local queue myvivi_vb_local_queue.

Timer Function
The last step is to simulate data in the timer function.
Static void myvivi_timer_function (unsigned long data) {struct videobuf_buffer * vb; void * vbuf; struct timeval ts;/* 1. construct Data: retrieve 1st videobuf from the queue header and fill in data * // * 1.1 retrieve 1st videobuf from the local queue */if (list_empty (& myvivi_vb_local_queue) {goto out ;} vb = list_entry (myvivi_vb_local_queue.next, struct videobuf_buffer, queue);/* Nobody is waiting on this buffer, return */if (! Waitqueue_active (& vb-> done) goto out;/* 1.2 fill data */vbuf = videobuf_to_vmalloc (vb);/* get its virtual address */memset (vbuf, 0x88, vb-> size); vb-> field_count ++; do_gettimeofday (& ts); vb-> ts = ts; vb-> state = VIDEOBUF_DONE; /* 1.3 remove videobuf from the local queue */list_del (& vb-> queue);/* 2. wake-up process: Wake up the process on videobuf-> done */wake_up (& vb-> done); out:/* 3. modify the time-out time of timer: 30fps, 30 frames of data in 1 second * one frame of data is generated every 1/30 seconds */mod_timer (& myvivi_timer, jiffies + HZ/30 );}
As mentioned earlier, myvivi_buffer_queue puts queuebuf into the local queue myvivi_vb_local_queue. The timer function extracts the first queuebuf from the local queue. If the local queue is empty, it exits directly. If no process is blocked on vb-> done through poll, it also exits directly. Then, use videobuf_to_vmalloc or the queuebuf virtual address obtained in the local queue, and fill all the data in this frame with the simplest msmset to 0x88, the size is the vb-> size = myvivi_format.fmt.pix.sizeimage filled in by myvivi_buffer_prepare. After that, accumulate the number of generated frames and fill in the current time. The most important thing is to set the current VIDEOBUF_DONE status, otherwise, even if the sleep process is awakened, it does not return a normal result. Delete the current videobuf from the local queue, and then wake up the sleep waiting process on the poll. The last HZ/30 indicates that the timer function runs for about 30 times in one second, that is, the frame rate of analog video is 30fps.

Effect demonstration
Finally, xawtv is used to demonstrate the effect as follows:
 
By modifying the filled data value in memset In the timer function, you can change the color displayed in xawtv.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.