- 论坛徽章:
- 0
|
我写的代码感觉没有错,但是为什么会在ioctl(vfd,VIDIOC_S_CROP,&crop)和ioctl(vfd,VIDIOC_S_FMT,&fmt)时候出现s3c-fimc: invalid target size有谁出现过类似的问题么?怎么解决的?- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/ioctl.h>
- #include <sys/mman.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <linux/videodev2.h>
- #include <linux/fb.h>
- #define WIDTH 640
- #define HEIGHT 480
- #define BUFFER_NUM 3
- typedef struct _FRAME_BUF{
- unsigned char *start;
- size_t offset;
- unsigned int length;
- }FRAME_BUF;
- int main(int argc,char* argv[])
- {
-
- int vfd = open("/dev/video0",O_RDWR|O_NONBLOCK);
- // int vfd = open("/dev/video1",O_RDWR);
-
- if(vfd < 0){
- printf("failed in open\n");
- return -1;
- }
- struct v4l2_crop crop;
- memset(&crop,0,sizeof(crop));
- crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- crop.c.left = 0;
- crop.c.top = 0;
- crop.c.width = 320;
- crop.c.height = 240;
- if(ioctl(vfd,VIDIOC_S_CROP,&crop)<0){
- printf("VIDIOC_S_CROP failed\n");
- }
-
- struct v4l2_format fmt;
- fmt.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
- /*
- if(ioctl(vfd,VIDIOC_G_FMT,&fmt) < 0){
- printf("failed G_fmt1 \n");
- return -1;
- }
- printf("fmt.fmt.pix.priv %d\n",fmt.fmt.pix.priv);
-
- */
- memset(&fmt,0,sizeof(fmt));
- fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
- fmt.fmt.pix.width =WIDTH;
- fmt.fmt.pix.height =HEIGHT;
- fmt.fmt.pix.bytesperline = WIDTH;
- fmt.fmt.pix.sizeimage = WIDTH*HEIGHT*3/2;
- fmt.fmt.pix.priv = 0;
- fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
-
- if(ioctl(vfd,VIDIOC_S_FMT,&fmt) < 0) {
- printf("failed in S_fmt \n");
- return -1;
- }
- memset(&fmt,0,sizeof(fmt));
- fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- if(ioctl(vfd,VIDIOC_G_FMT,&fmt) < 0) {
- printf("failed in g_fmt\n");
- return -1;
- }
- printf("width %d, height %d ,sizeimage %d",
- fmt.fmt.pix.width, fmt.fmt.pix.height,
- fmt.fmt.pix.sizeimage);
- char k = getchar();
- printf("d");
-
- struct v4l2_streamparm parm;
- struct v4l2_control ctrl;
-
-
- parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- parm.parm.capture.timeperframe.numerator = 1;
- parm.parm.capture.timeperframe.denominator = 3;
- parm.parm.capture.capturemode = 0;
- if(ioctl(vfd,VIDIOC_S_PARM,&parm)<0){
- printf("failed S_PARM\n");
- return -1;
- }
- getchar();
- printf("s");
-
- ctrl.id = V4L2_CID_PRIVATE_BASE + 0;
- ctrl.value = 0;
- if(ioctl(vfd,VIDIOC_S_CTRL,&ctrl) < 0){
- printf("set ctrl failed\n");
- return -1;
- }
- getchar();
- struct v4l2_requestbuffers req;
- memset(&req,0,sizeof(req));
- req.count = BUFFER_NUM;
- req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- req.memory = V4L2_MEMORY_MMAP;
- if(ioctl(vfd,VIDIOC_REQBUFS,&req) <0){
- printf("VIDIOC_REQBUFS failed\n");
- return -1;
- }
- getchar();
- int i;
- struct v4l2_buffer buf;
- enum v4l2_buf_type type;
- FRAME_BUF bufs[BUFFER_NUM];
-
- for(i=0; i< BUFFER_NUM; i++)
- {
- memset(&buf,0,sizeof(buf));
- buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- buf.index = i;
- if(ioctl(vfd,VIDIOC_QUERYBUF,&buf)<0){
- printf("VIDIOC_QUERYBUF failed\n");
- return -1;
- }
- bufs[i].length = buf.length;
- bufs[i].offset = (size_t) buf.m.offset;
- bufs[i].start = mmap(NULL,bufs[i].length,
- PROT_READ|PROT_WRITE,MAP_SHARED,
- vfd,bufs[i].offset);
- memset(bufs[i].start,0,bufs[i].length);
- printf("buf%d length:%d offset:%d start %d\n",i,bufs[i].length,
- bufs[i].offset,bufs[i].start);
- getchar();
- }
- for(i=0; i< BUFFER_NUM; i++)
- {
- memset(&buf,0,sizeof(buf));
- buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- buf.memory = V4L2_MEMORY_MMAP;
- buf.index = i;
- buf.m.offset = bufs[i].offset;
-
- if(ioctl(vfd,VIDIOC_QBUF,&buf) < 0){
- printf("VIDIOC_QBUF failed\n");
- return -1;
- }
- }
- type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- if(ioctl(vfd,VIDIOC_STREAMON,&type)<0){
- printf("VIDIOC_STREAMON failed\n");
- return -1;
- }
- fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- if(ioctl(vfd,VIDIOC_G_FMT,&fmt) < 0){
- printf("G_FMT failed\n");
- return -1;
- }else{
- printf("wdith %d\n",fmt.fmt.pix.width);
- printf("height %d\n",fmt.fmt.pix.height);
- printf("Image size = %d\n",fmt.fmt.pix.sizeimage);
- printf("pixelformate = %x\n",fmt.fmt.pix.pixelformat);
- }
- getchar();
-
- unsigned char count = 1;
- memset(&buf,0,sizeof(buf));
- buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- buf.memory = V4L2_MEMORY_MMAP;
- FILE * fyuv = fopen("./yuvfile","wb");
- if(fyuv < 0){
- printf("failed in create yuv file\n");
- return -1;
- }
- while(count++ > 0) {
- if(ioctl(vfd, VIDIOC_DQBUF,&buf)<0){
- printf("VIDIOC_DQBUF failed\n");
- continue;
- }
- fwrite(bufs[buf.index].start,fmt.fmt.pix.sizeimage,1,fyuv);
- }
- type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- ioctl(vfd,VIDIOC_STREAMOFF,&type);
-
- for(i=0; i< BUFFER_NUM; i++){
- munmap(bufs[i].start,bufs[i].length);
- }
- fclose(fyuv);
- close(vfd);
- return 0;
- }
复制代码 |
|