s5pv210 jpeg硬件编码

本文详细介绍了在QT摄像头程序中遇到的YUV转RGB显示效果不佳的问题,以及如何通过转换为JPEG格式进行显示以提升效果。同时,文章深入探讨了使用libjpeg库进行JPEG编码时遇到的设置问题,并提供了相应的解决策略。通过实验证明,JPEG格式在摄像头程序中显示图像更为流畅和清晰。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

做qt摄像头程序的时候发现yuv转rgb然后显示 效果很不好 所以考虑转jpg后显示 但是使用libjpeg发现格式怎么设置都不对 之前在Android上使用的函数拿过来都不能直接使用 所以还是研究硬编吧 这里使用的是广州斯道ICOOL210开发板 内核版本linux2.6.35

前面的一些流程我就不上代码了标准的v4l2流程 格式设置为V4L2_PIX_FMT_YUYV即yuv422格式 宽 640 高 480 主要是在获取到yuv数据后转成jpg然后保存 摄像头的程序网上有大把的例子 我这里比较乱我只放增加的函数了

首先把内核里的s3c-jpeg.h拷贝过来 这里是我的头文件

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /* 
  2.  * v4l2.h 
  3.  * Author: hclydao 
  4.  */  
  5.   
  6. #ifndef V4L2_H_  
  7. #define V4L2_H_  
  8. #include "s3c-jpeg.h"  
  9. #define CLEAR(x) memset (&(x), 0, sizeof (x))  
  10.   
  11. void errno_exit(const char* s);  
  12. int xioctl(int fd, int request, void* argp);  
  13. void deviceOpen(void);  
  14. int select_input(int input);  
  15. void deviceInit(void);  
  16. void mmapInit(void);  
  17. void captureStart(void);  
  18. void mainLoop(void);  
  19. unsigned char* frameRead(void);  
  20. void imageProcess(const void* p);  
  21. static void deviceUninit(void);  
  22.   
  23. static int jfd;  
  24. unsigned char *ydata;  
  25. static int name = 0;  
  26.   
  27. struct jpg_args mArgs;  
  28.   
  29. unsigned char *pInBuf;  
  30. int jpeg_init(void);  
  31. int test;  
  32. int jpg_enc(const void *p,int len);  
  33. #endif /* V4L2_H_ */</span>  

我还是上代码吧 前面的这些流程完全都是标准的 网上的说明很详细 这里就不说明了

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">/* 
  2.  * v4l2.c 
  3.  * Author: hclydao 
  4.  */  
  5.   
  6. #include <stdio.h>  
  7. #include <stdlib.h>  
  8. #include <string.h>  
  9. #include <assert.h>  
  10. #include <getopt.h>  
  11. #include <fcntl.h>  
  12. #include <unistd.h>  
  13. #include <errno.h>  
  14. #include <malloc.h>  
  15. #include <sys/stat.h>  
  16. #include <sys/types.h>  
  17. #include <sys/time.h>  
  18. #include <sys/mman.h>  
  19. #include <sys/ioctl.h>  
  20. #include <asm/types.h>  
  21. #include <linux/videodev2.h>  
  22. #include <jpeglib.h>  
  23. #include <linux/fb.h>  
  24.   
  25. #include "v4l2.h"  
  26.   
  27. struct buffer {  
  28.         void *                  start;  
  29.         size_t                  length;  
  30. };  
  31.   
  32. int              fd              = -1;  
  33. struct buffer *         buffers         = NULL;  
  34. static unsigned int     n_buffers       = 0;  
  35.   
  36. // global settings  
  37. static unsigned int width = 640;  
  38. static unsigned int height = 480;  
  39. static char* deviceName = "/dev/video0";  
  40.   
  41. static char* jpgdev = "/dev/s3c-jpg";  
  42.   
  43. static int fb_bpp;  
  44. static int cam_fp = -1;  
  45. static int fb_fp = -1;  
  46. static char *fb_addr = NULL;  
  47.   
  48. void deviceOpen(void)  
  49. {  
  50.     struct stat st;  
  51.   
  52.     // stat file  
  53.     if (-1 == stat(deviceName, &st)) {  
  54.         fprintf(stderr, "Cannot identify '%s': %d, %s\n", deviceName, errno, strerror (errno));  
  55.         exit(EXIT_FAILURE);  
  56.     }  
  57.     // check if its device  
  58.     if (!S_ISCHR (st.st_mode)) {  
  59.         fprintf(stderr, "%s is no device\n", deviceName);  
  60.         exit(EXIT_FAILURE);  
  61.     }  
  62.     // open device  
  63.     fd = open(deviceName, O_RDWR /* required */ | O_NONBLOCK, 0);  
  64.     // check if opening was successfull  
  65.     if (-1 == fd) {  
  66.         fprintf(stderr, "Cannot open '%s': %d, %s\n", deviceName, errno, strerror (errno));  
  67.         exit(EXIT_FAILURE);  
  68.     }  
  69. }  
  70.   
  71. int select_input(int input)  
  72. {  
  73.     int ret;  
  74.   
  75.     ret = xioctl(fd, VIDIOC_S_INPUT, &input);  
  76.     if (ret) {  
  77.         printf("xioctl VIDIOC_S_INPUT failed errno(%d)\n", errno);  
  78.     }     
  79.   
  80.     return ret;  
  81. }  
  82. /** 
  83.   initialize device 
  84. */  
  85. void deviceInit(void)  
  86. {  
  87.     struct v4l2_capability cap;  
  88.     struct v4l2_format fmt;  
  89.   
  90.     unsigned int min;  
  91.   
  92.     if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) {  
  93.         if (EINVAL == errno) {  
  94.             fprintf(stderr, "%s is no V4L2 device\n",deviceName);  
  95.             exit(EXIT_FAILURE);  
  96.         } else {  
  97.             errno_exit("VIDIOC_QUERYCAP");  
  98.         }  
  99.     }  
  100.   
  101.     if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))  
  102.     {  
  103.         fprintf(stderr, "%s is no video capture device\n",deviceName);  
  104.         exit(EXIT_FAILURE);  
  105.     }  
  106.   
  107.     if (!(cap.capabilities & V4L2_CAP_STREAMING))  
  108.     {  
  109.         fprintf(stderr, "%s does not support streaming i/o\n",deviceName);  
  110.         exit(EXIT_FAILURE);  
  111.     }  
  112.   
  113.      CLEAR (fmt);  
  114.   
  115.   // v4l2_format  
  116.       fmt.type                = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  117.       fmt.fmt.pix.width       = width;  
  118.       fmt.fmt.pix.height      = height;  
  119.       fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;//V4L2_PIX_FMT_RGB32;  
  120.       fmt.fmt.pix.field = V4L2_FIELD_NONE;//V4L2_FIELD_INTERLACED;//V4L2_FIELD_NONE;  
  121.       fmt.fmt.pix.priv = 1;  
  122.   
  123.       if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt))  
  124.           errno_exit("VIDIOC_S_FMT");  
  125.   
  126.       /* Note VIDIOC_S_FMT may change width and height. */  
  127.       if (width != fmt.fmt.pix.width) {  
  128.           width = fmt.fmt.pix.width;  
  129.           fprintf(stderr,"Image width set to %i by device %s.\n",width,deviceName);  
  130.       }  
  131.       if (height != fmt.fmt.pix.height) {  
  132.           height = fmt.fmt.pix.height;  
  133.           fprintf(stderr,"Image height set to %i by device %s.\n",height,deviceName);  
  134.       }  
  135.   
  136.       /* Buggy driver paranoia. */  
  137.       min = fmt.fmt.pix.width * 2;  
  138.       if (fmt.fmt.pix.bytesperline < min)  
  139.           fmt.fmt.pix.bytesperline = min;  
  140.   
  141.       min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;  
  142.       if (fmt.fmt.pix.sizeimage < min)  
  143.           fmt.fmt.pix.sizeimage = min;  
  144.   
  145.       mmapInit();  
  146. }  
  147.   
  148. void mmapInit(void)  
  149. {  
  150.     struct v4l2_requestbuffers req;  
  151.   
  152.     CLEAR (req);  
  153.   
  154.     req.count               = 4;  
  155.     req.type                = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  156.     req.memory              = V4L2_MEMORY_MMAP;  
  157.   
  158.     if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {  
  159.         if (EINVAL == errno) {  
  160.             fprintf(stderr, "%s does not support memory mapping\n", deviceName);  
  161.             exit(EXIT_FAILURE);  
  162.     } else {  
  163.         errno_exit("VIDIOC_REQBUFS");  
  164.         }  
  165.     }  
  166.   
  167.     if (req.count < 2) {  
  168.         fprintf(stderr, "Insufficient buffer memory on %s\n", deviceName);  
  169.         exit(EXIT_FAILURE);  
  170.     }  
  171.   
  172.     buffers = calloc(req.count, sizeof(*buffers));  
  173.   
  174.     if (!buffers) {  
  175.         fprintf(stderr, "Out of memory\n");  
  176.         exit(EXIT_FAILURE);  
  177.     }  
  178.   
  179.     for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {  
  180.         struct v4l2_buffer buf;  
  181.   
  182.         CLEAR (buf);  
  183.   
  184.         buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  185.         buf.memory      = V4L2_MEMORY_MMAP;  
  186.         buf.index       = n_buffers;  
  187.   
  188.     if (-1 == xioctl(fd, VIDIOC_QUERYBUF, &buf))  
  189.         errno_exit("VIDIOC_QUERYBUF");  
  190.   
  191.     buffers[n_buffers].length = buf.length;  
  192.   
  193.     buffers[n_buffers].start =  
  194.     mmap (NULL /* start anywhere */, buf.length, PROT_READ | PROT_WRITE /* required */, MAP_SHARED /* recommended */, fd, buf.m.offset);  
  195.   
  196.     if (MAP_FAILED == buffers[n_buffers].start)  
  197.         errno_exit("mmap");  
  198.     }  
  199. }  
  200.   
  201. void captureStart(void)  
  202. {  
  203.     unsigned int i;  
  204.     enum v4l2_buf_type type;  
  205.   
  206.     for (i = 0; i < n_buffers; ++i)  
  207.     {  
  208.         struct v4l2_buffer buf;  
  209.   
  210.         CLEAR (buf);  
  211.         buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  212.         buf.memory      = V4L2_MEMORY_MMAP;  
  213.         buf.index       = i;  
  214.   
  215.         if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))  
  216.             errno_exit("VIDIOC_QBUF");  
  217.     }  
  218.   
  219.     type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  220.   
  221.     if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))  
  222.         errno_exit("VIDIOC_STREAMON");  
  223. }  
  224.   
  225. void mainLoop(void)  
  226. {  
  227.     unsigned int count;  
  228.     count = 1;  
  229.   
  230.     while (count-- > 0) {  
  231.         for (;;) {  
  232.             fd_set fds;  
  233.             struct timeval tv;  
  234.             int r;  
  235.   
  236.             FD_ZERO(&fds);  
  237.             FD_SET(fd, &fds);  
  238.   
  239.             /* Timeout. */  
  240.             tv.tv_sec = 2;  
  241.             tv.tv_usec = 0;  
  242.   
  243.             r = select(fd + 1, &fds, NULL, NULL, &tv);  
  244.   
  245.             if (-1 == r) {  
  246.                 if (EINTR == errno)  
  247.                     continue;  
  248.                 errno_exit("select");  
  249.             }  
  250.   
  251.         if (0 == r) {  
  252.             fprintf (stderr, "select timeout\n");  
  253.             exit(EXIT_FAILURE);  
  254.         }  
  255.         if (frameRead())  
  256.             break;  
  257.     }  
  258.   }  
  259. }  
  260.   
  261. unsigned char* frameRead(void)  
  262. {  
  263.   
  264.     struct v4l2_buffer buf;  
  265.   
  266.     CLEAR (buf);  
  267.   
  268.     buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  269.     buf.memory = V4L2_MEMORY_MMAP;  
  270.   
  271.     if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) {  
  272.         switch (errno) {  
  273.             case EAGAIN:  
  274.                 return 0;  
  275.   
  276.             case EIO:  
  277.             default:  
  278.                 errno_exit("VIDIOC_DQBUF");  
  279.         }  
  280.     }  
  281.     assert (buf.index < n_buffers);  
  282.     //imageProcess(buffers[buf.index].start);  
  283.     if(test > 44)//放弃前面的数据  
  284.         jpg_enc(buffers[buf.index].start,buffers[buf.index].length);  
  285.     if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))  
  286.         errno_exit("VIDIOC_QBUF");  
  287.   return (unsigned char*)buffers[buf.index].start;  
  288. }  
  289.   
  290. void imageProcess(const void* p)  
  291. {  
  292.   
  293.     unsigned char* src = (unsigned char*)p;  
  294.       
  295.         memcpy(&fb_addr[0],src,width*height*4);  
  296.   
  297. }  
  298.   
  299. void deviceUninit(void)  
  300. {  
  301.   unsigned int i;  
  302.   
  303.       for (i = 0; i < n_buffers; ++i)  
  304.       {  
  305.       if (-1 == munmap (buffers[i].start, buffers[i].length))  
  306.         errno_exit("munmap");  
  307.       }  
  308.   
  309.   free(buffers);  
  310. }  
  311.   
  312. void errno_exit(const char* s)  
  313. {  
  314.     fprintf(stderr, "%s error %d, %s\n", s, errno, strerror (errno));  
  315.     exit(EXIT_FAILURE);  
  316. }  
  317.   
  318. int xioctl(int fd, int request, void* argp)  
  319. {  
  320.     int r;  
  321.     do r = ioctl(fd, request, argp);  
  322.     while (-1 == r && EINTR == errno);  
  323.   
  324.     return r;  
  325. }  
  326.   
  327. int jpg_enc(const void *p,int len)//编码函数  
  328. {  
  329.     FILE *fp;  
  330.     char tmp[20];  
  331.     int err;  
  332.     enum jpg_return_status ret = JPG_FAIL;  
  333.     unsigned char* src = (unsigned char*)p;  
  334.     memcpy(pInBuf, src, len);  
  335.     ret = (enum jpg_return_status)ioctl(jfd, IOCTL_JPG_ENCODE, &mArgs);  
  336.     if (ret != JPG_SUCCESS) {  
  337.         printf("++++++++IOCTL_JPG_ENCODE failed\n");  
  338.         return -1;  
  339.     }  
  340.     mArgs.out_buf = (char *)ioctl(jfd, IOCTL_JPG_GET_STRBUF, mArgs.mapped_addr);  
  341.     sprintf(tmp, "%d.jpg", name++);  
  342.     fp = fopen(tmp,"wb");  
  343.     if(fp < 0) {  
  344.         printf("+++++++++++=open file %s failed\n",tmp);  
  345.         return -1;  
  346.     }  
  347.     err = fwrite(mArgs.out_buf,sizeof(char),mArgs.enc_param->file_size,fp);  
  348.     if(err < 0) {  
  349.         printf("+++++++++++=write data failed\n");  
  350.         return -1;  
  351.     }  
  352.     fclose(fp);  
  353.     return mArgs.enc_param->file_size;  
  354. }  
  355.   
  356. int jpeg_init(void)//jpg初始化  
  357. {  
  358.     jfd = open(jpgdev,O_RDWR);  
  359.     if(jfd < 0) {  
  360.         printf("++++open jfd failed\n");  
  361.         return -1;  
  362.     }  
  363.   
  364.     mArgs.mapped_addr = mmap(0,640*480*4,PROT_READ | PROT_WRITE,MAP_SHARED, jfd, 0);  
  365.     if(mArgs.mapped_addr == MAP_FAILED) {  
  366.         printf("++++addr_base mmap failed\n");  
  367.         return -1;  
  368.     }  
  369.       
  370.     mArgs.enc_param = (struct jpg_enc_proc_param *)malloc(sizeof(struct jpg_enc_proc_param));  
  371.     if(mArgs.enc_param == NULL) {  
  372.         printf("++++enc_param malloc failed\n");  
  373.         return -1;  
  374.     }  
  375.     memset(mArgs.enc_param,0,sizeof(struct jpg_enc_proc_param));  
  376.   
  377.     mArgs.enc_param->sample_mode = JPG_422;  
  378.     mArgs.enc_param->enc_type = JPG_MAIN;  
  379.     mArgs.enc_param->in_format = JPG_MODESEL_YCBCR;  
  380.     mArgs.enc_param->quality = JPG_QUALITY_LEVEL_1;  
  381.     mArgs.enc_param->width = 640;  
  382.     mArgs.enc_param->height = 480;  
  383.   
  384.     pInBuf = (unsigned char *)ioctl(jfd, IOCTL_JPG_GET_FRMBUF, mArgs.mapped_addr);  
  385.     if (pInBuf == NULL) {  
  386.         printf("+++++++=JPEG input buffer is NULL!!\n");  
  387.         return -1;  
  388.     }  
  389. }  
下面是主函数

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /* 
  2.  * main.c 
  3.  * 
  4.  *  Created on: Aug 4, 2011 
  5.  *      Author: gavin 
  6.  */  
  7.   
  8. #include <stdio.h>  
  9. #include <stdlib.h>  
  10. #include <string.h>  
  11. #include <assert.h>  
  12. #include <getopt.h>  
  13. #include <fcntl.h>  
  14. #include <unistd.h>  
  15. #include <errno.h>  
  16. #include <malloc.h>  
  17. #include <sys/stat.h>  
  18. #include <sys/types.h>  
  19. #include <sys/time.h>  
  20. #include <sys/mman.h>  
  21. #include <sys/ioctl.h>  
  22. #include <asm/types.h>  
  23. #include <linux/videodev2.h>  
  24. #include <jpeglib.h>  
  25. #include <linux/fb.h>  
  26.   
  27. #include "v4l2.h"  
  28.   
  29.   
  30. int main()  
  31. {  
  32.     jpeg_init();  
  33.     deviceOpen();  
  34.     select_input(0);  
  35.     deviceInit();  
  36.     captureStart();  
  37.     for(test=0;test<50;test++)  
  38.     {  
  39.         mainLoop();  
  40.     }  
  41.     return 1;  
  42. }  
不要前面的一些帧 前面的有可能会是全绿的 以下是效果图


测试完成开始修改qt工程

工程下载地址:http://download.youkuaiyun.com/detail/hclydao/8217081


=====================================

同样可参考

http://www.doc88.com/p-590322657192.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值