USB攝像頭視頻監控項目學習筆記

一個攝像頭監控應用程序的系統調用如下所示:

/* 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;
};

?

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/384499.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/384499.shtml
英文地址,請注明出處:http://en.pswp.cn/news/384499.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

圖片縮放算法

項目背景&#xff1a;博主之前做過一個攝像頭采集數據&#xff0c;然后在LCD上顯示視頻數據的項目&#xff0c;假如我們攝像頭采集的一幀數據的分辨率比我們的LCD的分辨率要大&#xff0c;那么LCD則無法顯示整個圖像&#xff0c;這時候我們就要把這么一幀圖片進行縮放&#xff…

數碼相框項目之顯示一張可放大、縮小、拖拽的圖片

之前我做過一個電子相框的項目&#xff0c;涉及到的重難點主要為&#xff1a;在LCD上放大、縮小、移動圖片。 首先我們得明白的一點是&#xff1a;無論是放大或縮小&#xff0c;實際上都是對原圖進行等比例的縮小&#xff0c;然后在LCD上面顯示&#xff0c;只不過縮小的程度不…

TCP協議-如何保證傳輸可靠性

TCP協議傳輸的特點主要就是面向字節流、傳輸可靠、面向連接。這篇博客&#xff0c;我們就重點討論一下TCP協議如何確保傳輸的可靠性的。 確保傳輸可靠性的方式 TCP協議保證數據傳輸可靠性的方式主要有&#xff1a; 校驗和序列號確認應答超時重傳連接管理流量控制擁塞控制 校…

TCP協議-握手與揮手

認識TCP協議 TCP全稱為“傳輸控制協議”&#xff0c;這是傳輸層的一個協議&#xff0c;對數據的傳輸進行一個詳細的控制。 特點&#xff1a; 面向字節流安全可靠面向連接 TCP協議段格式 源端口號與目的端口號&#xff1a;這里與UDP的一樣&#xff0c;每個數據都要知道從哪個…

ASOC注冊過程

一、什么是ASOC 在嵌入式系統里面的聲卡驅動為ASOC&#xff08;ALSA System on Chip&#xff09; &#xff0c;它是在ALSA 驅動程序上封裝的一層&#xff0c;分為3大部分&#xff0c;Machine&#xff0c;Platform和Codec ,三部分的關系如下圖所示&#xff1a;其中Machine是指我…

ASOC調用過程

上一篇文章我們將了嵌入式系統注冊聲卡的過程&#xff1a;https://blog.csdn.net/qq_37659294/article/details/104748747 這篇文章我們以打開一個聲卡的播放節點為例&#xff0c;講解一下在APP調用open時&#xff0c;最終會如何調用到硬件相關的函數。 在上一篇文章最后我們說…

編寫聲卡驅動(框架)

在前面兩篇文章中&#xff0c;我們分別講了嵌入式Linux系統聲卡注冊的過程和調用的過程&#xff1a; https://blog.csdn.net/qq_37659294/article/details/104748747 https://blog.csdn.net/qq_37659294/article/details/104802868 講了那么多&#xff0c;我們最終的目的無非…

聲卡學習筆記

分享幾篇關于韋東山聲卡驅動的學習筆記&#xff0c;作者寫得非常詳細。 ALSA驅動框架&#xff1a;https://blog.csdn.net/qingkongyeyue/article/details/52328991 ASoC驅動框架&#xff1a;https://blog.csdn.net/qingkongyeyue/article/details/52349120 ASoC驅動重要結構…

路由器、交換機、集線器的區別

https://blog.csdn.net/weibo1230123/article/details/82779040

$PATH環境變量的作用

echo $PATH 顯示當前PATH環境變量&#xff0c;該變量的值由一系列以冒號分隔的目錄名組成&#xff0c;如&#xff1a;/usr/local/bin:/bin:/usr/bin。(冒號:是路徑分隔符) 在執行一個程序的時候如果沒有PATH的話&#xff0c;就需要寫出路徑名&#xff08;絕對或者相對&#xf…

dmesg

https://blog.csdn.net/zm_21/article/details/31760569

進程上下文與中斷上下文的理解

一.什么是內核態和用戶態 內核態&#xff1a;在內核空間執行&#xff0c;通常是驅動程序&#xff0c;中斷相關程序&#xff0c;內核調度程序&#xff0c;內存管理及其操作程序。 用戶態&#xff1a;用戶程序運行空間。 二.什么是進程上下文與中斷上下文 1.進程上下文&#xf…

GDB調試教程:1小時玩轉Linux gdb命令

原文鏈接&#xff1a;http://c.biancheng.net/gdb/ GDB 入門教程 本教程以下面的代碼為例&#xff0c;在 Linux 系統下來講解 GBD 的調試流程&#xff1a; int main (void) {unsigned long long int n, sum;n 1;sum 0;while (n < 100){sum sum n;n n 1;}return 0; …

shell將命令執行的結果賦值給 變量

https://blog.csdn.net/lemontree1945/article/details/79126819/

Linux下shell腳本指定程序運行時長

https://www.cnblogs.com/yychuyu/p/12626798.html

vim編輯器如何刪除一行或者多行內容

http://blog.itpub.net/69955379/viewspace-2681334/

C++經典問題:如果對象A中有對象成員B,對象B沒有默認構造函數,那么對象A必須在初始化列表中初始化對象B?

對象成員特點總結&#xff1a; &#xff08;1&#xff09;實例化對象A時&#xff0c;如果對象A有對象成員B,那么先執行對象B的構造函數&#xff0c;再執行A的構造函數。 &#xff08;2&#xff09;如果對象A中有對象成員B,那么銷毀對象A時&#xff0c;先執行對象A的析構函數&…

JZ2440用U-Boot給Nand-Flash燒寫程序時報錯:NAND write: incorrect device type in bootloader ‘bootloader‘ is not

JZ2440開發板使用問題&#xff0c;U-Boot燒寫程序到Nand Flash時報錯&#xff1a;NAND write: incorrect device type in bootloader bootloader is not a number 這是因為分區名中u-boot&#xff0c;不是bootloader&#xff0c;而cmd_menu.c里用的是bootloader 可以執行&#…

韋東山銜接班——4.4_構建根文件系統之構建根文件系統

文章地址&#xff1a; https://blog.csdn.net/gongweidi/article/details/100086289?biz_id102&utm_term%E9%9F%A6%E4%B8%9C%E5%B1%B1%E8%A1%94%E6%8E%A5%E7%8F%AD&utm_mediumdistribute.pc_search_result.none-task-blog-2~blog~sobaiduweb~default-5-100086289&…

C++中const char *p和char const *p

const char *p;他的意思是p指向的目標空間的內容不可變化 例如定義char cA; p&c;則c的內容不可以變化.如cB;等一些企圖改變變量c的值的做法都不行. 然而p仍然是動態的,就是它還可以指向別的空間,被賦予新的地址值,只是被他指向的目標空間的內容不可變化,如上面的c值始終為A…