一個攝像頭監控應用程序的系統調用如下所示:
/* open
?* VIDIOC_QUERYCAP 確定它是否視頻捕捉設備,支持哪種接口(streaming/read,write)
?* VIDIOC_ENUM_FMT 查詢支持哪種格式
?* VIDIOC_S_FMT ? ?設置攝像頭使用哪種格式
?* VIDIOC_REQBUFS ?申請buffer
?對于 streaming:
?* VIDIOC_QUERYBUF 確定每一個buffer的信息 并且 mmap
?* VIDIOC_QBUF ? ? 放入隊列
?* VIDIOC_STREAMON 啟動設備
?* poll ? ? ? ? ? ?等待有數據
?* VIDIOC_DQBUF ? ?從隊列中取出
?* 處理....
?* VIDIOC_QBUF ? ? 放入隊列
?* ....
?* VIDIOC_STREAMOFF 停止設備
?*
?*/
?
本文是作者之前做的一個攝像頭監控項目的筆記,主要目的就是記錄學習的過程,方便以后用到時復習使用,我們結合uvc攝像頭的應用程序和驅動程序來分析uvc攝像頭從硬件產生數據到上層應用程序獲取數據的過程。
?
一:VIDIOC_QUERYCAP
VIDIOC_QUERYCAP函數主要是設置APP傳入的v4l2_capability結構體變量tV4l2Cap(通過指針傳入,本文涉及的基本都是通過指針傳入,為了敘述時的方便,簡單說成傳入變量),然后APP根據設置的值再判斷它是不是一個視頻設備、是不是一個流(stream)設備等等...
APP程序:
struct v4l2_capability tV4l2Cap;
memset(&tV4l2Cap, 0, sizeof(struct v4l2_capability));
iError = ioctl(iFd, VIDIOC_QUERYCAP, &tV4l2Cap);
if (iError) {DBG_PRINTF("Error opening device %s: unable to query device.\n", strDevName);goto err_exit;
}if (!(tV4l2Cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
{DBG_PRINTF("%s is not a video capture device\n", strDevName);goto err_exit;
}if (tV4l2Cap.capabilities & V4L2_CAP_STREAMING) {DBG_PRINTF("%s supports streaming i/o\n", strDevName);
}if (tV4l2Cap.capabilities & V4L2_CAP_READWRITE) {DBG_PRINTF("%s supports read i/o\n", strDevName);
}
驅動程序:
case VIDIOC_QUERYCAP: {struct v4l2_capability *cap = arg; //獲取APP傳入的指針memset(cap, 0, sizeof *cap);strlcpy(cap->driver, "uvcvideo", sizeof cap->driver);strlcpy(cap->card, vdev->name, sizeof cap->card);usb_make_path(stream->dev->udev,cap->bus_info, sizeof(cap->bus_info));cap->version = DRIVER_VERSION_NUMBER; if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) //如果插入的設備是攝像頭cap->capabilities = V4L2_CAP_VIDEO_CAPTURE //設置傳入的結構體變量的信息 | V4L2_CAP_STREAMING;elsecap->capabilities = V4L2_CAP_VIDEO_OUTPUT| V4L2_CAP_STREAMING;break;}
問:底層驅動是根據什么來設置我們傳入的變量的?
答:設備描述符。
問:設備描述符從哪來呢?
答:我們的uvc攝像頭剛插上開發板時,USB總線驅動程序會生成一個usb_device結構體并把它掛到USB總線驅動程序的隊列中,這個usb_device結構體里面就包含有我們的硬件信息,也就是所說的描述符。
?
二:VIDIOC_ENUM_FMT
VIDIOC_ENUM_FMT函數主要是設置APP傳入的v4l2_fmtdesc結構體變量tFmtDesc,然后APP判斷是否支持這種pixelformat,若是支持便把它賦給APP的tVideoDevice(ptVideoDevice是它的指針)->iPixelFormat,無法支持則結束程序。
APP程序:
struct v4l2_fmtdesc tFmtDesc;memset(&tFmtDesc, 0, sizeof(tFmtDesc));tFmtDesc.index = 0;tFmtDesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;//枚舉攝像頭硬件支持的各種格式,若我們的應用程序支持這種格式則跳出循環while ((iError = ioctl(iFd, VIDIOC_ENUM_FMT, &tFmtDesc)) == 0) {if (isSupportThisFormat(tFmtDesc.pixelformat))//這個函數在下面定義了{ptVideoDevice->iPixelFormat = tFmtDesc.pixelformat;break;}tFmtDesc.index++;//index++再傳入驅動即可查看硬件所支持的下一種格式}if (!ptVideoDevice->iPixelFormat)//無法支持{DBG_PRINTF("can not support the format of this device\n");goto err_exit; }
驅動程序:
case VIDIOC_ENUM_FMT:{struct v4l2_fmtdesc *fmt = arg; //獲取APP傳入的指針struct uvc_format *format;enum v4l2_buf_type type = fmt->type;__u32 index = fmt->index;//檢查APP傳入的數據if (fmt->type != stream->type ||fmt->index >= stream->nformats) //類型不對或者超過硬件支持的種數了return -EINVAL;memset(fmt, 0, sizeof(*fmt));fmt->index = index;fmt->type = type;//根據硬件的信息設置APP傳入的變量format = &stream->format[fmt->index];fmt->flags = 0;if (format->flags & UVC_FMT_FLAG_COMPRESSED)fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;strlcpy(fmt->description, format->name,sizeof fmt->description);fmt->description[sizeof fmt->description - 1] = 0;fmt->pixelformat = format->fcc; //設置pixelformat,如YUYV、RGB、MJPEG等等...break;}
?
三:VIDIOC_S_FMT
我們先在APP獲取LCD的分辨率和tVideoDevice->iPixelFormat(如YUYV、MJPEG等),然后通過v4l2_format結構體變量tV4l2Fmt傳入到驅動程序里,傳入的這些值其實還沒真正設置到硬件上,只是暫時保存起來,等streamon的時候才真正發給硬件。
問:為什么要先在APP獲取LCD的分辨率,然后傳入驅動呢
答:我們這個項目是獲取攝像頭采集的數據然后在LCD上顯示,為了增強顯示效果,我們通過傳入的參數設置攝像頭采集的圖片的分辨率和我們的LCD一致(如果攝像頭不支持這種分辨率,那么驅動程序會找到和我們設置的值相近的分辨率)。
int iLcdWidth;int iLcdHeigt;int iLcdBpp;struct v4l2_format tV4l2Fmt;/* set format in */GetDispResolution(&iLcdWidth, &iLcdHeigt, &iLcdBpp);//獲取LCD分辨率memset(&tV4l2Fmt, 0, sizeof(struct v4l2_format));tV4l2Fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;tV4l2Fmt.fmt.pix.pixelformat = ptVideoDevice->iPixelFormat;tV4l2Fmt.fmt.pix.width = iLcdWidth;tV4l2Fmt.fmt.pix.height = iLcdHeigt;tV4l2Fmt.fmt.pix.field = V4L2_FIELD_ANY;/* 如果驅動程序發現無法某些參數(比如分辨率),* 它會調整這些參數, 并且返回給應用程序*/iError = ioctl(iFd, VIDIOC_S_FMT, &tV4l2Fmt); if (iError) {DBG_PRINTF("Unable to set format\n");goto err_exit; }/* 讀出調整后的參數 */ptVideoDevice->iWidth = tV4l2Fmt.fmt.pix.width;ptVideoDevice->iHeight = tV4l2Fmt.fmt.pix.height;
驅動程序:
/* Find the closest image size. The distance between image sizes is* the size in pixels of the non-overlapping regions between the* requested size and the frame-specified size.*///找到和我們傳入的值最接近的分辨率(fmt是我們在APP里傳入的那個變量)rw = fmt->fmt.pix.width;rh = fmt->fmt.pix.height;maxd = (unsigned int)-1;for (i = 0; i < format->nframes; ++i) {__u16 w = format->frame[i].wWidth;__u16 h = format->frame[i].wHeight;d = min(w, rw) * min(h, rh);d = w*h + rw*rh - 2*d;if (d < maxd) {maxd = d;frame = &format->frame[i];}if (maxd == 0)break;} //此處代碼沒貼全(因為太長了)......fmt->fmt.pix.width = frame->wWidth;fmt->fmt.pix.height = frame->wHeight;fmt->fmt.pix.field = V4L2_FIELD_NONE;fmt->fmt.pix.bytesperline = format->bpp * frame->wWidth / 8;fmt->fmt.pix.sizeimage = probe->dwMaxVideoFrameSize;fmt->fmt.pix.colorspace = format->colorspace;fmt->fmt.pix.priv = 0;
?
四:VIDIOC_REQBUFS
VIDIOC_REQBUFS先在APP設置一些值,例如要申請的緩沖區個數,然后通過v4l2_requestbuffers類型的結構體變量tV4l2ReqBuffs傳入到驅動中,驅動程序根據傳入的個數和前面第三步的dwMaxVideoFrameSize(sizeimage)來申請緩沖區,然后設置隊列queue里關于緩沖區的信息(如:各個緩沖區的號數和偏移量),最后得到真正申請到的緩沖區個數回到APP,我們把它保存到ptVideoDevice->iVideoBufCnt中(ptVideoDevice->iVideoBufCnt = tV4l2ReqBuffs.count;)
APP程序:
struct v4l2_requestbuffers tV4l2ReqBuffs;/* request buffers */memset(&tV4l2ReqBuffs, 0, sizeof(struct v4l2_requestbuffers));tV4l2ReqBuffs.count = NB_BUFFER;tV4l2ReqBuffs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;tV4l2ReqBuffs.memory = V4L2_MEMORY_MMAP;iError = ioctl(iFd, VIDIOC_REQBUFS, &tV4l2ReqBuffs);if (iError) {DBG_PRINTF("Unable to allocate buffers.\n");goto err_exit; }/* 申請buffer不一定能成功,真正申請到的buffer個數記錄在tV4l2ReqBuffs.count */ptVideoDevice->iVideoBufCnt = tV4l2ReqBuffs.count;
驅動程序
struct v4l2_requestbuffers *rb = arg;
ret = uvc_alloc_buffers(&stream->queue, rb->count,stream->ctrl.dwMaxVideoFrameSize);
uvc_alloc_buffers的部分代碼如下所示:?
/* Decrement the number of buffers until allocation succeeds. */
//如果分配不成功,則減少要分配的緩沖區個數,然后再嘗試一下for (; nbuffers > 0; --nbuffers) {mem = vmalloc_32(nbuffers * bufsize);if (mem != NULL)break;}
//一個緩沖區都分配不成功,返回錯誤信息if (mem == NULL) {ret = -ENOMEM;goto done;}//設置隊列queue里各個緩沖區的信息,例如偏移量,號數index等等...for (i = 0; i < nbuffers; ++i) {memset(&queue->buffer[i], 0, sizeof queue->buffer[i]);queue->buffer[i].buf.= i;queue->buffer[i].buf.m.offset = i * bufsize;queue->buffer[i].buf.length = buflength;queue->buffer[i].buf.type = queue->type;queue->buffer[i].buf.field = V4L2_FIELD_NONE;queue->buffer[i].buf.memory = V4L2_MEMORY_MMAP;queue->buffer[i].buf.flags = 0;init_waitqueue_head(&queue->buffer[i].wait);}queue->mem = mem; //真正存放圖片數據的地址在這里,隊列里面的buffer數組只是記錄各個緩沖區的偏移量之類的信息queue->count = nbuffers;queue->buf_size = bufsize;ret = nbuffers;done:mutex_unlock(&queue->mutex);return ret;
?
五、for(i = 0; i < ptVideoDevice->iVideoBufCnt; i++){
VIDIOC_QUERYBUF 和 mmap
}
VIDIOC_QUERYBUF 和 mmap,先在APP設置一些值到v4l2_buffer結構體變量tV4l2Buf中,例如:tV4l2Buf.index(表示我們要查詢哪個緩沖區的信息(什么信息?偏移值和頁大小(sizeimage)等)),然后把tV4l2Buf傳入驅動,驅動程序根據傳入的tV4l2Buf.index拷貝隊列queue對應的緩沖區信息到tV4l2Buf,返回到APP,APP把tV4l2Buf.length保存到tVideoDevice->iVideoBufMaxLen中。
然后調用mmap把tV4l2Buf.length,tV4l2Buf.m.offset傳入驅動,驅動程序進行某種對比后找出要mmap的緩沖區(第四步申請的那些緩沖區),把該緩沖區的首地址通過返回值的形式賦給了ptVideoDevice->pucVideBuf[i],一個循環下來,ptVideoDevice->pucVideBuf數組便指向了各個緩沖區的首地址。
APP程序:
memset(&tV4l2Buf, 0, sizeof(struct v4l2_buffer));tV4l2Buf.index = i;tV4l2Buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;tV4l2Buf.memory = V4L2_MEMORY_MMAP;iError = ioctl(iFd, VIDIOC_QUERYBUF, &tV4l2Buf);if (iError) {DBG_PRINTF("Unable to query buffer.\n");goto err_exit;}ptVideoDevice->iVideoBufMaxLen = tV4l2Buf.length;ptVideoDevice->pucVideBuf[i] = mmap(0 /* start anywhere */ ,tV4l2Buf.length, PROT_READ, MAP_SHARED, iFd,tV4l2Buf.m.offset);if (ptVideoDevice->pucVideBuf[i] == MAP_FAILED) {DBG_PRINTF("Unable to map buffer\n");goto err_exit;}
驅動程序:
int uvc_query_buffer(struct uvc_video_queue *queue,struct v4l2_buffer *v4l2_buf)
{int ret = 0;mutex_lock(&queue->mutex);if (v4l2_buf->index >= queue->count) {ret = -EINVAL;goto done;}//根據傳入的v4l2_buf->index決定要查詢哪個緩沖區的信息__uvc_query_buffer(&queue->buffer[v4l2_buf->index], v4l2_buf);//函數定義在下面done:mutex_unlock(&queue->mutex);return ret;
}
static void __uvc_query_buffer(struct uvc_buffer *buf,struct v4l2_buffer *v4l2_buf)
{//把內核里面緩沖區的信息拷貝到APP傳進來的地址memcpy(v4l2_buf, &buf->buf, sizeof *v4l2_buf);if (buf->vma_use_count)v4l2_buf->flags |= V4L2_BUF_FLAG_MAPPED;//設置flagswitch (buf->state) {case UVC_BUF_STATE_ERROR:case UVC_BUF_STATE_DONE:v4l2_buf->flags |= V4L2_BUF_FLAG_DONE;break;case UVC_BUF_STATE_QUEUED:case UVC_BUF_STATE_ACTIVE:case UVC_BUF_STATE_READY:v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;break;case UVC_BUF_STATE_IDLE:default:break;}
}
/** Memory-map a video buffer.** This function implements video buffers memory mapping and is intended to be* used by the device mmap handler.*/
int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
{struct uvc_buffer *uninitialized_var(buffer);struct page *page;unsigned long addr, start, size;unsigned int i;int ret = 0;start = vma->vm_start;size = vma->vm_end - vma->vm_start;mutex_lock(&queue->mutex);/* 應用程序調用mmap函數時, 會傳入offset參數* 根據這個offset找出指定的緩沖區*/for (i = 0; i < queue->count; ++i) {buffer = &queue->buffer[i];if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)//把內核里的緩沖區信息和傳進來的參數進行某種對比,如果符合則證明就是要mmap這個緩沖區break;}if (i == queue->count || PAGE_ALIGN(size) != queue->buf_size) {ret = -EINVAL;goto done;}/** VM_IO marks the area as being an mmaped region for I/O to a* device. It also prevents the region from being core dumped.*/vma->vm_flags |= VM_IO;/* 根據虛擬地址找到緩沖區對應的page構體 */addr = (unsigned long)queue->mem + buffer->buf.m.offset;
#ifdef CONFIG_MMUwhile (size > 0) {page = vmalloc_to_page((void *)addr);/* 把page和APP傳入的虛擬地址掛構 */if ((ret = vm_insert_page(vma, start, page)) < 0)goto done;start += PAGE_SIZE;addr += PAGE_SIZE;size -= PAGE_SIZE;}
#endifvma->vm_ops = &uvc_vm_ops;vma->vm_private_data = buffer;uvc_vm_open(vma);done:mutex_unlock(&queue->mutex);return ret;
}
?
六、for(i = 0; i < ptVideoDevice->iVideoBufCnt; i++){
VIDIOC_QBUF
}
首先清空第五步的變量tV4l2Buf,并設置tV4l2Buf.index傳入驅動,驅動根據tV4l2Buf.index找到各個緩沖區的buffer(看代碼感覺其實里面包含的是緩沖區的信息而已,真正存數據的地方是queme->mem),把它們的stream和queue分別掛到隊列queue的mainqueue和irqqueue形成兩條隊列。
其實是雙向鏈表,上面的圖只是為了容易理解而已。?
APP程序:
memset(&tV4l2Buf, 0, sizeof(struct v4l2_buffer));tV4l2Buf.index = i;tV4l2Buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;tV4l2Buf.memory = V4L2_MEMORY_MMAP;iError = ioctl(iFd, VIDIOC_QBUF, &tV4l2Buf);if (iError){DBG_PRINTF("Unable to queue buffer.\n");goto err_exit;}
驅動程序:?
/** Queue a video buffer. Attempting to queue a buffer that has already been* queued will return -EINVAL.*/
int uvc_queue_buffer(struct uvc_video_queue *queue,struct v4l2_buffer *v4l2_buf)
{struct uvc_buffer *buf;unsigned long flags;int ret = 0;uvc_trace(UVC_TRACE_CAPTURE, "Queuing buffer %u.\n", v4l2_buf->index);//判斷傳入的參數是否正確if (v4l2_buf->type != queue->type ||v4l2_buf->memory != V4L2_MEMORY_MMAP) {uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) ""and/or memory (%u).\n", v4l2_buf->type,v4l2_buf->memory);return -EINVAL;}mutex_lock(&queue->mutex);//判斷傳入的參數是否正確if (v4l2_buf->index >= queue->count) {uvc_trace(UVC_TRACE_CAPTURE, "[E] Out of range index.\n");ret = -EINVAL;goto done;}//找到對應的bufferbuf = &queue->buffer[v4l2_buf->index];if (buf->state != UVC_BUF_STATE_IDLE) {uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state ""(%u).\n", buf->state);ret = -EINVAL;goto done;}if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&v4l2_buf->bytesused > buf->buf.length) {uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");ret = -EINVAL;goto done;}spin_lock_irqsave(&queue->irqlock, flags);if (queue->flags & UVC_QUEUE_DISCONNECTED) {spin_unlock_irqrestore(&queue->irqlock, flags);ret = -ENODEV;goto done;}buf->state = UVC_BUF_STATE_QUEUED;if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)buf->buf.bytesused = 0;elsebuf->buf.bytesused = v4l2_buf->bytesused;//把buffer的stream和queue分別掛到隊列queue的mainqueue和irqqueuelist_add_tail(&buf->stream, &queue->mainqueue);list_add_tail(&buf->queue, &queue->irqqueue);spin_unlock_irqrestore(&queue->irqlock, flags);done:mutex_unlock(&queue->mutex);return ret;
}
?
七:streamon
這一步的工作主要是初始化URB和啟動攝像頭,我們在內核代碼中搜索urb->complete可在Uvc_video.c里找到uvc_video_complete函數,當硬件產生一幀數據給URB之后,便回進入uvc_video_complete函數
uvc_video_complete函數?如下所示:
static void uvc_video_complete(struct urb *urb)
{struct uvc_streaming *stream = urb->context;struct uvc_video_queue *queue = &stream->queue;struct uvc_buffer *buf = NULL;unsigned long flags;int ret;switch (urb->status) {case 0:break;default:uvc_printk(KERN_WARNING, "Non-zero status (%d) in video ""completion handler.\n", urb->status);case -ENOENT: /* usb_kill_urb() called. */if (stream->frozen)return;case -ECONNRESET: /* usb_unlink_urb() called. */case -ESHUTDOWN: /* The endpoint is being disabled. */uvc_queue_cancel(queue, urb->status == -ESHUTDOWN);return;}spin_lock_irqsave(&queue->irqlock, flags);//如果irqqueue隊列不為空,則讓buf指向它第一個節點,準備把數據從URB拷貝到第一個節點if (!list_empty(&queue->irqqueue))buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,queue);spin_unlock_irqrestore(&queue->irqlock, flags);//decode是解碼的意思,其實內部就是拷貝URB上的數據到irqqueue隊列的第一個節點stream->decode(urb, stream, buf);//重新提交URBif ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {uvc_printk(KERN_ERR, "Failed to resubmit video URB (%d).\n",ret);}
}
在內核源碼搜索stream->decode,我們會找到stream->decode = uvc_video_decode_isoc;分析uvc_video_decode_isoc函數的源碼我們看到下面的這一部分,這部分代碼的作用就是把URB上的數據(也就是下面代碼的data)拷貝到irqqueue隊列第一個節點對應的緩沖區,因為調用的過程比較繁瑣,這里長話短說,詳細分析得看源代碼。
/* Copy the video data to the buffer. */maxlen = buf->buf.length - buf->buf.bytesused;mem = queue->mem + buf->buf.m.offset + buf->buf.bytesused;nbytes = min((unsigned int)len, maxlen);memcpy(mem, data, nbytes);buf->buf.bytesused += nbytes;
然后把irqqueue隊列的這個節點刪除掉并喚醒APP進程(等數據時進入休眠)
list_del(&buf->queue);
wake_up(&buf->wait);
?
八:VIDIOC_DQBUF
當應用程序被喚醒后,會調用VIDIOC_DQBUF,APP把v4l2_buffer類型的結構體變量tV4l2Buf傳入到驅動程序,驅動程序把mainqueue隊列的第一個節點的數據拷貝到tV4l2Buf(我們主要用到tV4l2Buf.index),然后把這個節點從mainqueue隊列中刪去。
APP有了這個tV4l2Buf.index便知道了ptVideoDevice->pucVideBuf[i]有數據,把它地址賦給ptVideoBuf->tPixelDatas.aucPixelDatas(用來指向每次有圖片數據的那塊緩存)。到此我們便從硬件得到一幀圖片到APP了。
APP程序:?
/* VIDIOC_DQBUF */memset(&tV4l2Buf, 0, sizeof(struct v4l2_buffer));tV4l2Buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;tV4l2Buf.memory = V4L2_MEMORY_MMAP;iRet = ioctl(ptVideoDevice->iFd, VIDIOC_DQBUF, &tV4l2Buf);if (iRet < 0) {DBG_PRINTF("Unable to dequeue buffer.\n");return -1;}ptVideoDevice->iVideoBufCurIndex = tV4l2Buf.index;ptVideoBuf->iPixelFormat = ptVideoDevice->iPixelFormat;ptVideoBuf->tPixelDatas.iWidth = ptVideoDevice->iWidth;ptVideoBuf->tPixelDatas.iHeight = ptVideoDevice->iHeight;ptVideoBuf->tPixelDatas.iBpp = (ptVideoDevice->iPixelFormat == V4L2_PIX_FMT_YUYV) ? 16 : \(ptVideoDevice->iPixelFormat == V4L2_PIX_FMT_MJPEG) ? 0 : \(ptVideoDevice->iPixelFormat == V4L2_PIX_FMT_RGB565) ? 16 : \0;ptVideoBuf->tPixelDatas.iLineBytes = ptVideoDevice->iWidth * ptVideoBuf->tPixelDatas.iBpp / 8;ptVideoBuf->tPixelDatas.iTotalBytes = tV4l2Buf.bytesused;//獲取圖片緩存的地址ptVideoBuf->tPixelDatas.aucPixelDatas = ptVideoDevice->pucVideBuf[tV4l2Buf.index];
部分驅動程序:
//取出mainqueue隊列的第一個節點buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);//把這個節點從mainqueue隊列中刪除list_del(&buf->stream);//和第五步的VIDIOC_QUERYBUF一樣,它的作用其實就是把buf(也就是mainqueue隊列第一個節點)的數據拷貝到v4l2_buf(我們主要用到v4l2_buf.index)__uvc_query_buffer(buf, v4l2_buf);
?
九:再次VIDIOC_QBUF,為了能夠循環得到圖片,我們還得調用VIDIOC_QBUF把前面從兩個隊列刪除的那個節點插回去。
執行tV4l2Buf.index ?= ptVideoDevice->iVideoBufCurIndex(在第八步記錄了tV4l2Buf.index);找回上次從隊列中刪掉的是哪個buffer,然后把它重新插到隊列上去,然后等待下一幀數據的到來。
struct v4l2_buffer tV4l2Buf;int iError;memset(&tV4l2Buf, 0, sizeof(struct v4l2_buffer));tV4l2Buf.index = ptVideoDevice->iVideoBufCurIndex;tV4l2Buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;tV4l2Buf.memory = V4L2_MEMORY_MMAP;iError = ioctl(ptVideoDevice->iFd, VIDIOC_QBUF, &tV4l2Buf);if (iError) {DBG_PRINTF("Unable to queue buffer.\n");return -1;}return 0;
/** Queue a video buffer. Attempting to queue a buffer that has already been* queued will return -EINVAL.*/
int uvc_queue_buffer(struct uvc_video_queue *queue,struct v4l2_buffer *v4l2_buf)
{struct uvc_buffer *buf;unsigned long flags;int ret = 0;uvc_trace(UVC_TRACE_CAPTURE, "Queuing buffer %u.\n", v4l2_buf->index);//判斷傳入的參數是否正確if (v4l2_buf->type != queue->type ||v4l2_buf->memory != V4L2_MEMORY_MMAP) {uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) ""and/or memory (%u).\n", v4l2_buf->type,v4l2_buf->memory);return -EINVAL;}mutex_lock(&queue->mutex);//判斷傳入的參數是否正確if (v4l2_buf->index >= queue->count) {uvc_trace(UVC_TRACE_CAPTURE, "[E] Out of range index.\n");ret = -EINVAL;goto done;}//找到對應的bufferbuf = &queue->buffer[v4l2_buf->index];if (buf->state != UVC_BUF_STATE_IDLE) {uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state ""(%u).\n", buf->state);ret = -EINVAL;goto done;}if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&v4l2_buf->bytesused > buf->buf.length) {uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");ret = -EINVAL;goto done;}spin_lock_irqsave(&queue->irqlock, flags);if (queue->flags & UVC_QUEUE_DISCONNECTED) {spin_unlock_irqrestore(&queue->irqlock, flags);ret = -ENODEV;goto done;}buf->state = UVC_BUF_STATE_QUEUED;if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)buf->buf.bytesused = 0;elsebuf->buf.bytesused = v4l2_buf->bytesused;//把buffer的stream和queue分別掛到隊列queue的mainqueue和irqqueuelist_add_tail(&buf->stream, &queue->mainqueue);list_add_tail(&buf->queue, &queue->irqqueue);spin_unlock_irqrestore(&queue->irqlock, flags);done:mutex_unlock(&queue->mutex);return ret;
}
?
?
?
?
?
最后再貼幾個上面用到的結構體,方便查看
1.T_VideoBuf
typedef struct VideoBuf {T_PixelDatas tPixelDatas;int iPixelFormat;
}T_VideoBuf, *PT_VideoBuf;/* 圖片的象素數據 */
typedef struct PixelDatas {int iWidth; /* 寬度: 一行有多少個象素 */int iHeight; /* 高度: 一列有多少個象素 */int iBpp; /* 一個象素用多少位來表示 */int iLineBytes; /* 一行數據有多少字節 */int iTotalBytes; /* 所有字節數 */ unsigned char *aucPixelDatas; /* 象素數據存儲的地方,每次都是指向有新數據的緩沖區 */
}T_PixelDatas, *PT_PixelDatas;
2.
struct VideoDevice {int iFd;int iPixelFormat;int iWidth;int iHeight;int iVideoBufCnt;int iVideoBufMaxLen;int iVideoBufCurIndex; unsigned char *pucVideBuf[NB_BUFFER]; //通過mmap函數映射,該數組的每個變量都指向了緩沖區的地址(比如 men + 1 * offset 、men + 2 * offset)/* 函數 */PT_VideoOpr ptOPr;
};
?
3.uvc_video_queue?
struct uvc_video_queue {enum v4l2_buf_type type;void *mem; //正真存放圖片數據的地方unsigned int flags;unsigned int count;unsigned int buf_size;unsigned int buf_used;struct uvc_buffer buffer[UVC_MAX_VIDEO_BUFFERS]; //n個buffer,里面記錄了每個緩沖區的偏移值和號數index等信息struct mutex mutex; /* protects buffers and mainqueue */spinlock_t irqlock; /* protects irqqueue */struct list_head mainqueue; //供APP使用的隊列(頭節點)struct list_head irqqueue; //供驅動使用的隊列(頭節點)
};
?4.uvc_buffer?
struct uvc_buffer {unsigned long vma_use_count;struct list_head stream; //供APP使用的隊列(普通節點)/* Touched by interrupt handler. */struct v4l2_buffer buf;struct list_head queue; //供驅動使用的隊列(普通節點)wait_queue_head_t wait;enum uvc_buffer_state state;unsigned int error;
};
5.v4l2_buffer??
struct v4l2_buffer {__u32 index; //記錄buffer的號數enum v4l2_buf_type type;__u32 bytesused;__u32 flags;enum v4l2_field field;struct timeval timestamp;struct v4l2_timecode timecode;__u32 sequence;/* memory location */enum v4l2_memory memory;union {__u32 offset; //記錄各個緩沖區的偏移值,正真的圖片數據在mem + n * offsetunsigned long userptr;struct v4l2_plane *planes;} m;__u32 length;__u32 input;__u32 reserved;
};
?
?