Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1339936
  • 博文数量: 198
  • 博客积分: 1629
  • 博客等级: 上尉
  • 技术积分: 2743
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-01 15:41
文章分类
文章存档

2023年(6)

2022年(20)

2021年(8)

2020年(3)

2018年(17)

2017年(3)

2016年(3)

2015年(9)

2014年(13)

2013年(17)

2012年(77)

2011年(22)

分类: LINUX

2015-06-08 16:34:18


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <getopt.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9. #include <sys/stat.h>
  10. #include <sys/types.h>
  11. #include <sys/time.h>
  12. #include <sys/mman.h>
  13. #include <sys/ioctl.h>
  14. #include <asm/types.h>
  15. #include <linux/videodev2.h>
  16. #include <linux/fb.h>
  17. #define CLEAR(x) memset (&(x), 0, sizeof (x))

  18. struct buffer {
  19.     void * start;
  20.     size_t length;
  21. };

  22. static char * dev_name = NULL;
  23. static int fd = -1;
  24. struct buffer * buffers = NULL;
  25. static unsigned int n_buffers = 0;
  26. static int time_in_sec_capture=10;
  27. static int fbfd = -1;
  28. static struct fb_var_screeninfo vinfo;
  29. static struct fb_fix_screeninfo finfo;
  30. static char *fbp=NULL;
  31. static long screensize=0;

  32. static void errno_exit (const char * s)
  33. {
  34.     fprintf (stderr, "%s error %d, %s\n",s, errno, strerror (errno));
  35.     exit (EXIT_FAILURE);
  36. }

  37. static int xioctl (int fd,int request,void * arg)
  38. {
  39.     int r;
  40.     do r = ioctl (fd, request, arg);
  41.     while (-1 == r && EINTR == errno);
  42.     return r;
  43. }

  44. inline int clip(int value, int min, int max) {
  45.     return (value > max ? max : value < min ? min : value);
  46. }

  47. static void process_image (const void * p){


  48.     //ConvertYUVToRGB32

  49.     unsigned char* in=(char*)p;
  50.     int width=640;
  51.     int height=480;
  52.     int istride=1280;
  53.     int x,y,j;
  54.     int y0,u,y1,v,r,g,b;
  55.     long location=0;
  56.     int xpos = (vinfo.xres-width)/2;
  57.     int ypos = (vinfo.yres-height)/2;

  58.     for ( y = ypos; y < height + ypos; ++y) {
  59.         for (j = 0, x=xpos; j < width * 2 ; j += 4,x +=2) {

  60.             location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
  61.                 (y+vinfo.yoffset) * finfo.line_length;

  62.             y0 = in[j];
  63.             u = in[j + 1] - 128;
  64.             y1 = in[j + 2];
  65.             v = in[j + 3] - 128;

  66.             r = (298 * y0 + 409 * v + 128) >> 8;
  67.             g = (298 * y0 - 100 * u - 208 * v + 128) >> 8;
  68.             b = (298 * y0 + 516 * u + 128) >> 8;

  69.             fbp[ location + 0] = clip(b, 0, 255);
  70.             fbp[ location + 1] = clip(g, 0, 255);
  71.             fbp[ location + 2] = clip(r, 0, 255);
  72.             fbp[ location + 3] = 255;

  73.             r = (298 * y1 + 409 * v + 128) >> 8;
  74.             g = (298 * y1 - 100 * u - 208 * v + 128) >> 8;
  75.             b = (298 * y1 + 516 * u + 128) >> 8;

  76.             fbp[ location + 4] = clip(b, 0, 255);
  77.             fbp[ location + 5] = clip(g, 0, 255);
  78.             fbp[ location + 6] = clip(r, 0, 255);
  79.             fbp[ location + 7] = 255;
  80.         }
  81.         in +=istride;
  82.     }

  83. }

  84. static int read_frame (void)
  85. {
  86.     struct v4l2_buffer buf;
  87.     unsigned int i;

  88.     CLEAR (buf);
  89.     buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  90.     buf.memory = V4L2_MEMORY_MMAP;

  91.     if (-1 == xioctl (fd, VIDIOC_DQBUF, &buf)) {
  92.         switch (errno) {
  93.             case EAGAIN:
  94.                 return 0;
  95.             case EIO:

  96.             default:
  97.                 errno_exit ("VIDIOC_DQBUF");
  98.         }
  99.     }

  100.     assert (buf.index < n_buffers);
  101.     assert (buf.field ==V4L2_FIELD_NONE);
  102.     process_image (buffers[buf.index].start);
  103.     if (-1 == xioctl (fd, VIDIOC_QBUF, &buf))
  104.         errno_exit ("VIDIOC_QBUF");

  105.     return 1;
  106. }

  107. static void run (void)
  108. {
  109.     unsigned int count;
  110.     int frames;
  111.     frames = 30 * time_in_sec_capture;

  112.     while (frames-- > 0) {
  113.         for (;;) {
  114.             fd_set fds;
  115.             struct timeval tv;
  116.             int r;
  117.             FD_ZERO (&fds);
  118.             FD_SET (fd, &fds);


  119.             tv.tv_sec = 2;
  120.             tv.tv_usec = 0;

  121.             r = select (fd + 1, &fds, NULL, NULL, &tv);

  122.             if (-1 == r) {
  123.                 if (EINTR == errno)
  124.                     continue;
  125.                 errno_exit ("select");
  126.             }

  127.             if (0 == r) {
  128.                 fprintf (stderr, "select timeout\n");
  129.                 exit (EXIT_FAILURE);
  130.             }

  131.             if (read_frame ())
  132.                 break;

  133.         }

  134.         //if(frames%10 == 0)
  135.         {
  136.             printf("\r %4d ", frames);
  137.             fputc ('.', stdout);
  138.             fflush (stdout);
  139.         }

  140.     }
  141.     printf("\n");
  142. }

  143. static void stop_capturing (void)
  144. {
  145.     enum v4l2_buf_type type;

  146.     type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  147.     if (-1 == xioctl (fd, VIDIOC_STREAMOFF, &type))
  148.         errno_exit ("VIDIOC_STREAMOFF");
  149. }

  150. static void start_capturing (void)
  151. {
  152.     unsigned int i;
  153.     enum v4l2_buf_type type;

  154.     for (i = 0; i < n_buffers; ++i) {
  155.         struct v4l2_buffer buf;
  156.         CLEAR (buf);

  157.         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  158.         buf.memory = V4L2_MEMORY_MMAP;
  159.         buf.index = i;

  160.         if (-1 == xioctl (fd, VIDIOC_QBUF, &buf))
  161.             errno_exit ("VIDIOC_QBUF");
  162.     }

  163.     type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

  164.     if (-1 == xioctl (fd, VIDIOC_STREAMON, &type))
  165.         errno_exit ("VIDIOC_STREAMON");

  166. }

  167. static void uninit_device (void)
  168. {
  169.     unsigned int i;

  170.     for (i = 0; i < n_buffers; ++i)
  171.         if (-1 == munmap (buffers[i].start, buffers[i].length))
  172.             errno_exit ("munmap");

  173.     if (-1 == munmap(fbp, screensize)) {
  174.         printf(" Error: framebuffer device munmap() failed.\n");
  175.         exit (EXIT_FAILURE) ;
  176.     }
  177.     free (buffers);
  178. }


  179. static void init_mmap (void)
  180. {
  181.     struct v4l2_requestbuffers req;


  182.     //mmap framebuffer
  183.     fbp = (char *)mmap(NULL,screensize,PROT_READ | PROT_WRITE,MAP_SHARED ,fbfd, 0);
  184.     if ((int)fbp == -1) {
  185.         printf("Error: failed to map framebuffer device to memory.\n");
  186.         exit (EXIT_FAILURE) ;
  187.     }
  188.     memset(fbp, 0, screensize);
  189.     CLEAR (req);

  190.     req.count = 4;
  191.     req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  192.     req.memory = V4L2_MEMORY_MMAP;

  193.     if (-1 == xioctl (fd, VIDIOC_REQBUFS, &req)) {
  194.         if (EINVAL == errno) {
  195.             fprintf (stderr, "%s does not support memory mapping\n", dev_name);
  196.             exit (EXIT_FAILURE);
  197.         } else {
  198.             errno_exit ("VIDIOC_REQBUFS");
  199.         }
  200.     }

  201.     if (req.count < 4) { //if (req.count < 2)
  202.         fprintf (stderr, "Insufficient buffer memory on %s\n",dev_name);
  203.         exit (EXIT_FAILURE);
  204.     }

  205.     buffers = calloc (req.count, sizeof (*buffers));

  206.     if (!buffers) {
  207.         fprintf (stderr, "Out of memory\n");
  208.         exit (EXIT_FAILURE);
  209.     }

  210.     for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
  211.         struct v4l2_buffer buf;

  212.         CLEAR (buf);

  213.         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  214.         buf.memory = V4L2_MEMORY_MMAP;
  215.         buf.index = n_buffers;

  216.         if (-1 == xioctl (fd, VIDIOC_QUERYBUF, &buf))
  217.             errno_exit ("VIDIOC_QUERYBUF");

  218.         buffers[n_buffers].length = buf.length;
  219.         buffers[n_buffers].start =mmap (NULL,buf.length,PROT_READ | PROT_WRITE ,MAP_SHARED,fd, buf.m.offset);

  220.         if (MAP_FAILED == buffers[n_buffers].start)
  221.             errno_exit ("mmap");
  222.     }

  223. }

  224. static void init_device (void)
  225. {
  226.     struct v4l2_capability cap;
  227.     struct v4l2_cropcap cropcap;
  228.     struct v4l2_crop crop;
  229.     struct v4l2_format fmt;
  230.     unsigned int min;


  231.     // Get fixed screen information
  232.     if (-1==xioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
  233.         printf("Error reading fixed information.\n");
  234.         exit (EXIT_FAILURE);
  235.     }

  236.     // Get variable screen information
  237.     if (-1==xioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
  238.         printf("Error reading variable information.\n");
  239.         exit (EXIT_FAILURE);
  240.     }
  241.     screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;


  242.     if (-1 == xioctl (fd, VIDIOC_QUERYCAP, &cap)) {
  243.         if (EINVAL == errno) {
  244.             fprintf (stderr, "%s is no V4L2 device\n",dev_name);
  245.             exit (EXIT_FAILURE);
  246.         } else {
  247.             errno_exit ("VIDIOC_QUERYCAP");
  248.         }
  249.     }

  250.     if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
  251.         fprintf (stderr, "%s is no video capture device\n",dev_name);
  252.         exit (EXIT_FAILURE);
  253.     }

  254.     if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
  255.         fprintf (stderr, "%s does not support streaming i/o\n",dev_name);
  256.         exit (EXIT_FAILURE);
  257.     }



  258.     CLEAR (cropcap);

  259.     cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

  260.     if (0 == xioctl (fd, VIDIOC_CROPCAP, &cropcap)) {
  261.         crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  262.         crop.c = cropcap.defrect;

  263.         if (-1 == xioctl (fd, VIDIOC_S_CROP, &crop)) {
  264.             switch (errno) {
  265.                 case EINVAL:
  266.                     break;
  267.                 default:
  268.                     break;
  269.             }
  270.         }
  271.     }else { }

  272.     CLEAR (fmt);

  273.     fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  274.     fmt.fmt.pix.width = 640;
  275.     fmt.fmt.pix.height = 480;
  276.     fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  277.     fmt.fmt.pix.field = V4L2_FIELD_NONE;

  278.     if (-1 == xioctl (fd, VIDIOC_S_FMT, &fmt))
  279.         errno_exit ("VIDIOC_S_FMT");

  280.     init_mmap ();

  281. }

  282. static void close_device (void)
  283. {
  284.     if (-1 == close (fd))
  285.         errno_exit ("close");
  286.     fd = -1;
  287.     close(fbfd);
  288. }

  289. static void open_device (void)
  290. {
  291.     struct stat st;

  292.     if (-1 == stat (dev_name, &st)) {
  293.         fprintf (stderr, "Cannot identify '%s': %d, %s\n",dev_name, errno, strerror (errno));
  294.         exit (EXIT_FAILURE);
  295.     }

  296.     if (!S_ISCHR (st.st_mode)) {
  297.         fprintf (stderr, "%s is no device\n", dev_name);
  298.         exit (EXIT_FAILURE);
  299.     }

  300.     //open framebuffer
  301.     fbfd = open("/dev/fb0", O_RDWR);
  302.     if (fbfd==-1) {
  303.         printf("Error: cannot open framebuffer device.\n");
  304.         exit (EXIT_FAILURE);
  305.     }

  306.     //open camera
  307.     fd = open (dev_name, O_RDWR| O_NONBLOCK, 0);

  308.     if (-1 == fd) {
  309.         fprintf (stderr, "Cannot open '%s': %d, %s\n",dev_name, errno, strerror (errno));
  310.         exit (EXIT_FAILURE);
  311.     }
  312. }

  313. static void usage (FILE * fp,int argc,char ** argv)
  314. {
  315.     fprintf (fp,
  316.             "Usage: %s [options]\n\n"
  317.             "Options:\n"
  318.             "-d | --device name Video device name [/dev/video]\n"
  319.             "-h | --help Print this message\n"
  320.             "-t | --how long will display in seconds\n"
  321.             "",
  322.             argv[0]);
  323. }

  324. static const char short_options [] = "d:ht:";
  325. static const struct option long_options [] = {
  326.     { "device", required_argument, NULL, 'd' },
  327.     { "help", no_argument, NULL, 'h' },
  328.     { "time", no_argument, NULL, 't' },
  329.     { 0, 0, 0, 0 }
  330. };

  331. int main (int argc,char ** argv)
  332. {
  333.     dev_name = "/dev/video1";

  334.     for (;;)
  335.     {
  336.         int index;
  337.         int c;

  338.         c = getopt_long (argc, argv,short_options, long_options,&index);
  339.         if (-1 == c)
  340.             break;

  341.         switch (c) {
  342.             case 0:
  343.                 break;

  344.             case 'd':
  345.                 dev_name = optarg;
  346.                 break;

  347.             case 'h':
  348.                 usage (stdout, argc, argv);
  349.                 exit (EXIT_SUCCESS);
  350.             case 't':
  351.                 time_in_sec_capture = atoi(optarg);
  352.                 break;

  353.             default:
  354.                 usage (stderr, argc, argv);
  355.                 exit (EXIT_FAILURE);
  356.         }
  357.     }

  358.     open_device ();

  359.     init_device ();

  360.     start_capturing ();

  361.     run ();

  362.     stop_capturing ();

  363.     uninit_device ();

  364.     close_device ();

  365.     exit (EXIT_SUCCESS);

  366.     return 0;
  367. }

阅读(3234) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~