基於Video4linux的視頻採集,用SDL顯示

本程序的開發環境是(我當前的系統):操作系統:RedHat 9.0 及 CG300的視頻採集卡

Video4linux 簡介
Video4Linux是爲市場現在常見的電視捕獲卡和並口及USB口的攝像頭提供統一的編程接口。同時也提供無線電通信和文字電視廣播解碼和垂直消隱的數據接口。本文主要針對USB攝像頭設備文件/dev/video0,進行視頻圖像採集方面的程序設計。

Video4linux 編程指南
1.視頻編程的流程
(1)打開視頻設備:
(2)讀取設備信息
(3)更改設備當前設置(可以不做)
(4)進行視頻採集,兩種方法:
 a.內存映射
 b.直接從設備讀取
(5)對採集的視頻進行處理( 本程序沒做,下次再show給大家)
(6)關閉視頻設備

  1. //下面我對這些操作做了個簡單的函數封裝 (o(∩_∩)o...哈哈)
  2. #ifndef _V4L_H
  3. #define _V4L_H
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <error.h>
  8. #include <assert.h>
  9. #include <fcntl.h>
  10. #include <sys/ioctl.h>
  11. #include <sys/types.h>
  12. #include <sys/mman.h>
  13. #include <linux/videodev.h>
  14. #include <sys/types.h>
  15. #include <string.h>
  16. /*採集的圖像的最大長和寬*/
  17. #define MAX_WIDTH 400
  18. #define MAX_HEIGHT 300
  19. /*設備文件*/
  20. #define DEFAULT_DEVICE "/dev/video0"
  21. /*自定義數據結構,包含v4l 中用到的數據結構*/
  22. typedef struct v4l_struct
  23. {
  24.     int fd;/*設備號*/
  25.     struct video_capability capability; //包含設備的基本信息(設備名稱、支持的最大最小分辨率、信號源信息等)
  26.     struct video_channel channel[8];//信號源個數
  27.     struct video_picture picture;//設備採集的圖象的各種屬性
  28.     struct video_mmap mmap;//用於mmap         
  29.     struct video_mbuf mbuf;//利用mmap進行映射的幀的信息            
  30.     unsigned char *buffer ;/*圖像數據存放區*/
  31.     unsigned char *map;/*mmap方式獲取數據時,數據的首地址*/
  32.     int frame_current;
  33.     int frame_using[2]; /*這個幀的狀態0 表示可用,1表示不可用*/
  34. }v4l_device;
  35. /**************************************************************
  36. * 函數名:v4l_open
  37. * 功  能: 打開設備
  38. * 輸  入: dev,vd
  39. * 輸  出: 無
  40. * 返  回:  -1----失敗  0----成功
  41. **************************************************************/
  42. int v4l_open( char *dev, v4l_device *vd )
  43. {
  44.     if( !dev )
  45.     {
  46.         dev=DEFAULT_DEVICE ;
  47.     }
  48.     if( ( vd->fd = open( dev, O_RDWR ) )  < 0 )
  49.     {
  50.         perror( "v4l_open error" );
  51.         return -1;
  52.     }
  53.     return 0;
  54. }
  55. /**************************************************************
  56. * 函數名: v4l_get_capability
  57. * 功  能: 獲取設備屬性
  58. * 輸  入: vd
  59. * 輸  出: 無
  60. * 返  回:  -1----失敗 0----成功
  61. **************************************************************/
  62. int v4l_get_capability( v4l_device *vd )
  63. {
  64.     if( ioctl( vd->fd, VIDIOCGCAP, &( vd->capability ) ) <0 )
  65.     {
  66.         perror( "v4l_get_capability" );
  67.         return -1 ;
  68.     }
  69.     return 0; 
  70. }
  71. /***************************************************************
  72. * 函數名:v4l_get_picture
  73. * 功  能:獲取圖片屬性
  74. * 輸  入: vd
  75. * 輸  出: 無
  76. * 返  回:  -1----失敗  0----成功
  77. ***************************************************************/
  78. int v4l_get_picture( v4l_device *vd )
  79. {
  80.     if( ioctl( vd->fd,VIDIOCGPICT,&( vd->picture ) ) < 0 )
  81.     {
  82.         return -1;
  83.     }
  84.     return 0;
  85. }
  86. /**************************************************************
  87. * 函數名: v4l_set_picture
  88. * 功  能: 設置圖片屬性
  89. * 輸  入: vd
  90. * 輸  出: 無
  91. * 返  回: -1----失敗 0----成功
  92. **************************************************************/
  93. int v4l_set_picture( v4l_device *vd ) 
  94. {
  95.     if( ioctl( vd->fd, VIDIOCSPICT, &( vd->picture ) ) < 0 )
  96.     {
  97.         return -1;
  98.     }
  99.     return 0;
  100. }
  101. /*************************************************************
  102. * 函數名:v4l_get_channels
  103. * 功  能:獲取通道信息
  104. * 輸  入: vd
  105. * 輸  出: 無
  106. * 返  回:  -1----失敗 0----成功
  107. *************************************************************/
  108. int v4l_get_channels( v4l_device *vd )
  109. {
  110.     int i;
  111.     for( i=0;i < vd->capability.channels ; i++ )
  112.     {
  113.         vd->channel[i].channel = i;               //確定通道
  114.         if( ioctl( vd->fd , VIDIOCGCHAN, &( vd->channel[i] ) ) <0 )
  115.         {
  116.             perror( "v4l_get_channel" );
  117.             return -1;
  118.         }
  119.     }
  120.     return 0;
  121. }
  122. /*************************************************************
  123. * 函數名: v4l_get_mbuf
  124. * 功  能: 獲取內存映射信息
  125. * 輸  入: vd
  126. * 輸  出: 無
  127. * 返  回:  -1----失敗 0----成功
  128. **************************************************************/
  129. int v4l_get_mbuf( v4l_device *vd )
  130. {
  131.     if( ioctl ( vd->fd,VIDIOCGMBUF,&( vd->mbuf ) ) <0 )
  132.     {
  133.         perror( "get_mbuf:" );
  134.         return -1;
  135.     }
  136.     if ( ( vd->map = ( unsigned char * )mmap( 0, vd->mbuf.size, PROT_READ | PROT_WRITE, MAP_SHARED, vd->fd, 0 ) ) < 0 )
  137.     {
  138.         perror("v4l_mmap_init:mmap");
  139.         return -1;
  140.     }
  141.     return 0 ;
  142. }
  143. /*************************************************************
  144. * 函數名: v4l_init_mbuff
  145. * 功  能: 初始化內存映射信息
  146. * 輸  入: vd
  147. * 輸  出: 無
  148. * 返  回: 0----成功
  149. **************************************************************/
  150. int v4l_init_mbuf(v4l_device *vd)
  151. {
  152.     //vd->mmap.frame = 10 ; //不懂雙幀是怎樣設置的這個frame 該是當前幀的可mbuf 以又沒有設置怎麼確定是雙幀不是單幀還是更多
  153.     vd->mmap.width = MAX_WIDTH;
  154.     vd->mmap.height = MAX_HEIGHT;
  155.     vd->mmap.format = vd->picture.palette;
  156.     vd->frame_current = 0;
  157.     vd->frame_using[0] = 0;
  158.     vd->frame_using[1] = 0;
  159.     return 0;
  160. }
  161. /**************************************************************
  162. * 函數名: v4l_get_address
  163. * 功  能: 獲取數據在圖像的地址
  164. ***************************************************************/
  165. unsigned char *v4l_get_address(v4l_device *vd)
  166. {
  167.     return (vd->map + vd->mbuf.offsets[vd->frame_current]);
  168. }
  169. /*************************************************************
  170. * 函數名: v4l_grab_frame
  171. * 功  能: 捕獲幀
  172. **************************************************************/
  173. int v4l_grab_frame(v4l_device *vd, int frame)
  174. {
  175.     if (vd->frame_using[frame]) 
  176.     {
  177.         fprintf(stderr, "v4l_grab_frame: frame %d is already used./n", frame);
  178.         return -1;
  179.     }
  180.     vd->mmap.frame = frame;
  181.     if ( ioctl(vd->fd, VIDIOCMCAPTURE, &(vd->mmap ) ) < 0 ) 
  182.     {
  183.         perror( "v4l_grab_frame" );
  184.         return -1;
  185.     }
  186.     vd->frame_using[frame] = 1;
  187.     vd->frame_current = frame;
  188.     return 0;    
  189. }
  190. /**************************************************************
  191. * 函數名: v4l_grab_sync
  192. * 功  能:與內存映射捕獲一致
  193. **************************************************************/
  194. int v4l_grab_sync(v4l_device *vd)
  195. {  
  196.     if (ioctl(vd->fd, VIDIOCSYNC, &(vd->frame_current)) < 0) 
  197.     {
  198.         perror("v4l_grab_sync");
  199.     }
  200.     vd->frame_using[vd->frame_current] = 0;
  201.     return 0;
  202. }
  203. /***************************************************************
  204. * 函數名: v4l_munmap
  205. * 功  能:停止內存映射
  206. ***************************************************************/
  207. int v4l_munmap( v4l_device *vd )
  208. {
  209.     if ( munmap( vd->map, vd->mbuf.size ) < 0 ) 
  210.     {
  211.         perror( "v4lmunmap:munmap" );
  212.         return -1;
  213.     }
  214.     return 0;
  215. }
  216. /***************************************************************
  217. * 函數名: v4l_close
  218. * 功  能:關閉設備
  219. ***************************************************************/
  220. int v4l_close(v4l_device *vd)
  221. {
  222.     close(vd->fd);
  223.     return 0;
  224. }
  225. #endif
  226. //簡單的封裝了關於SDL的相關操作"ScreenSurface.h"
  227. #ifndef SCREEN_SURFACE_H
  228. #define SCREEN_SURFACE_H
  229. #include <stdio.h>
  230. #include <stdlib.h>
  231. #include <SDL/SDL.h>
  232. class ScreenSurface 
  233. {
  234. public:
  235.     ScreenSurface();
  236.     bool screen_init(int w, int h, int b = 0, Uint32 f = 0);//初始化
  237.     ~ScreenSurface();
  238.     SDL_Surface* point() const;
  239.     int screen_lock();
  240.     void screen_unlock();
  241.     void screen_quit();
  242.     void screen_set_caption( const char *str );//設置標題
  243.     bool flip( unsigned char * src) ;//顯示
  244.     int startTV();//開始採集
  245. private:
  246.     static int screenNum;
  247.     int width;
  248.     int height;
  249.     int bpp;
  250.     Uint32 flags;
  251.     SDL_Surface* pScreen;
  252. };
  253. #endif
  254. //ScreenSurface.cpp
  255. #include "ScreenSurface.h"
  256. #include "qt_v4l.h"
  257. v4l_device  v4l_dev;
  258. /**************************************************************
  259. * 函數名: v4l_grab_movie
  260. * 功  能:捕獲連續圖像
  261. **************************************************************/
  262. void  v4l_grab_movie()
  263. {
  264.     v4l_grab_frame(&v4l_dev, v4l_dev.frame_current);/*獲取下一 幀*/
  265.     v4l_grab_sync(&v4l_dev);/*等待傳完一 幀*/
  266.     v4l_dev.buffer = v4l_get_address(&v4l_dev);/*得到這一幀的地址*/
  267.     v4l_dev.frame_current = (v4l_dev.frame_current+1)%2; /* 下一幀的frame*/
  268. }
  269. //構造函數。如果創建1個以上的screen surface,則會拋出異常
  270. ScreenSurface::ScreenSurface():width(640), height(480), bpp(32), flags(0)
  271.     pScreen = 0;
  272.     v4l_open(DEFAULT_DEVICE, &v4l_dev);/*打開設備*/
  273.     v4l_get_capability(&v4l_dev);
  274.     v4l_get_picture(&v4l_dev);
  275.     v4l_init_mbuf(&v4l_dev);/*初始化設備*/
  276.     v4l_get_mbuf(&v4l_dev);/*內存映射*/
  277. }
  278. bool ScreenSurface::screen_init(int w, int h, int b, Uint32 f)
  279. {
  280.     width = w ;
  281.     height = h ;
  282.     bpp = b ;
  283.     flags = f ;
  284.     
  285.     if(SDL_Init(SDL_INIT_VIDEO) < 0)
  286.     {
  287.         printf("SDL_Init Failed!/n");
  288.         return false;
  289.     }
  290.     //設置圖象模式(寬*高 位數 標誌 SDL_SWSURFACE | SDL_DOUBLEBUF)
  291.     pScreen = SDL_SetVideoMode(width, height, bpp, flags);
  292.     if ( pScreen == 0 )
  293.     {
  294.         printf("Could't set display mode /n");
  295.         SDL_Quit();
  296.         return false;
  297.     }
  298.     SDL_ShowCursor(SDL_DISABLE);
  299.     return true;
  300. }
  301. //析構函數。在對象消亡時,退出SDL系統。
  302. ScreenSurface::~ScreenSurface()
  303. {
  304.     
  305. }
  306. //返回screen surface中SDL_Surface結構的指針,主要提供給SDL的函數調用
  307. SDL_Surface* ScreenSurface::point() const
  308. {
  309.     return pScreen;
  310. }
  311. int ScreenSurface::screen_lock()
  312. {
  313.     if ( SDL_MUSTLOCK(pScreen))
  314.         return SDL_LockSurface(pScreen);
  315.     return 0;
  316. }
  317. void ScreenSurface::screen_unlock()
  318. {
  319.     if ( SDL_MUSTLOCK(pScreen))
  320.         SDL_UnlockSurface(pScreen); 
  321. }
  322. void ScreenSurface::screen_quit()
  323. {
  324.     SDL_Quit();
  325.     v4l_munmap(&v4l_dev) ;
  326.     v4l_close(&v4l_dev); 
  327. }
  328. void ScreenSurface::screen_set_caption( const char *str )
  329. {
  330.     SDL_WM_SetCaption( str, 0 );
  331. }
  332. //顯示(彈出flip)screen surface到屏幕上
  333. bool ScreenSurface::flip( unsigned char * src )
  334. {
  335.     if ( screen_lock() < 0)
  336.         return false;
  337.     unsigned char  *dest;
  338.     dest = ( unsigned char * )pScreen->pixels;
  339.     memcpy( dest , src , width * height * 4 );
  340.     screen_unlock();
  341.     
  342.     if ( SDL_Flip(pScreen) < 0 )
  343.         return false;
  344.     else 
  345.         return true;   
  346. }
  347. int ScreenSurface::startTV()
  348. {
  349.     bool bFlag = true;
  350.     while(bFlag)
  351.     {
  352.         v4l_grab_movie(); 
  353.         unsigned char *buf= v4l_dev.buffer;
  354.         if (buf != NULL)
  355.         {
  356.             flip(buf);
  357.         }
  358.         SDL_Event event;
  359.         while(SDL_PollEvent(event))
  360.         {
  361.             if (event.type == SDL_QUIT)
  362.             {
  363.                 //bFlag = false;
  364.                 screen_quit();
  365.             }
  366.         }
  367.     }
  368. }
  369. //main.cpp
  370. #include "ScreenSurface.h"
  371. void main()
  372. {
  373.     ScreenSurface *m_pScreen;
  374.     m_pScreen = new ScreenSurface( );
  375.     m_pScreen->screen_init(400 , 300 , 32 , SDL_SWSURFACE | SDL_ANYFORMAT);
  376.     m_pScreen->screen_set_caption("DemoTV");   
  377.     m_pScreen->startTV();
  378. }

代碼SHOW完了,下面再附點小信息供大家看看,剛玩LINUX不久,有錯誤的地方還請大俠們指出。

發佈了2 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章