?? video_cap.cpp
字號:
///////////////////////////////////////////////////////// FileName: video_cap.cpp// Author: b1gm0use// Project: myvideo#include <iostream>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/mman.h>#include <fcntl.h>#include <sys/ioctl.h>#include <qsemaphore.h>#include "video_cap.h"#include "video_player.h"#include "v4l.h"#include "video.h"#include "avi.h"using namespace std;///////////////////////////////////////////////////////// Functions Declaration///////////////////////////////////////////////////////extern int jpeg_compress( char *dst, char *src, int width, int height, int dstsize );///////////////////////////////////////////////////////// Public Functions///////////////////////////////////////////////////////// 構造函數video_cap::video_cap ( avi * avi_ptr_in ) // {{{{ verbose_output( 2, "create video_cap." ); vid_cap = new video_capability (); jpeg_image = NULL; dev_id = 0; img_buff = NULL; frame = 0; avi_ptr = avi_ptr_in;} // }}}// 析構函數video_cap::~video_cap ( void ) // {{{{ verbose_output( 2, "delete video_cap." ); delete vid_cap; delete [] jpeg_image; delete [] img_buff;} // }}}// 初始化函數// img 是圖像的參數,video_dev 是設備名稱int video_cap::init ( void ) // {{{{ verbose_output( 2, "init video_cap." ); if ( NULL == avi_ptr->video_dev ) { cerr << "video_dev Can't be NULL" << endl; exit( 1 ); } // 打開設備 dev_id = open ( avi_ptr->video_dev, O_RDWR); if ( -1 == dev_id ) { cerr << "Can't open video_dev:\t" << avi_ptr->video_dev << endl; exit( 1 ); } if ( ioctl( dev_id, VIDIOCGCAP, vid_cap ) == -1 ) { cerr << "Can't get video video_dev capability!" << endl; exit( 1 ); } jpeg_image = new BUFF [ avi_ptr->video_opt.min_width * avi_ptr->video_opt.factor * avi_ptr->video_opt.min_height * avi_ptr->video_opt.factor * 3 ]; img_buff = new BUFF [ avi_ptr->video_opt.min_width * avi_ptr->video_opt.factor * avi_ptr->video_opt.min_height * avi_ptr->video_opt.factor * 3 ]; return 0;} // }}}// 取得一幀圖像// image 為傳出參數,指向取得的圖像// size 為傳出參數,表示圖像數據大小int video_cap::get_image ( BUFF * &image, int *size) // {{{{ struct video_mbuf vid_buf; struct video_mmap vid_mmap; verbose_output( 3, "get new capture image" ); if (ioctl (dev_id, VIDIOCGMBUF, &vid_buf) == -1) { cerr << "ioctl(VIDIOCGMBUF)" << endl; image = NULL; return -1; } verbose_output( 3, "map video_dev to memory." ); // 開辟設備映射 image = static_cast<BUFF *> ( mmap ( 0, vid_buf.size, PROT_READ|PROT_WRITE,MAP_PRIVATE, dev_id, 0 ) ); if ((unsigned char *)-1 == (unsigned char *)image) { cerr << "Error in mmap()!" << endl; image = NULL; exit( 1 ); } vid_mmap.format = avi_ptr->video_opt.palette; vid_mmap.frame = 0; vid_mmap.width = avi_ptr->video_opt.min_width * avi_ptr->video_opt.factor; vid_mmap.height = avi_ptr->video_opt.min_height * avi_ptr->video_opt.factor; verbose_output( 4, "get new frame." ); // 取得一幀 if ( ioctl (dev_id, VIDIOCMCAPTURE, &vid_mmap ) == -1 ) { cerr << "Error in VIDIOCMCAPTURE!" << " args: width=" << vid_mmap.width << " height=" << vid_mmap.height << " palette=" << vid_mmap.format << endl; munmap (image, vid_buf.size); image = NULL; return -1; } verbose_output( 4, "get frame sync." ); // 幀同步 if (ioctl (dev_id, VIDIOCSYNC, &vid_mmap.frame) == -1) { cerr << "Error in VIDIOCSYNC!" << endl; munmap (image, vid_buf.size); image = NULL; return -1; } *size = vid_buf.size; verbose_output( 4, "copy frame to buffer" ); if ( avi_ptr->video_opt.palette == VIDEO_PALETTE_RGB24 ) { memcpy( img_buff, image, avi_ptr->video_opt.min_width * avi_ptr->video_opt.factor * avi_ptr->video_opt.min_height * avi_ptr->video_opt.factor * 3 ); } else { if ( avi_ptr->video_opt.palette == VIDEO_PALETTE_YUV420) { verbose_output( 4, "converting from YUV to RGB"); v4l_yuv420p2rgb ( ( unsigned char * ) img_buff, ( unsigned char * ) image, avi_ptr->video_opt.min_width * avi_ptr->video_opt.factor, avi_ptr->video_opt.min_height * avi_ptr->video_opt.factor, 24 ); } } // 刪除設備映射 munmap (image, vid_buf.size); // 壓縮為JPEG *size = jpeg_compress((char*)jpeg_image, (char*)img_buff, avi_ptr->video_opt.min_width * avi_ptr->video_opt.factor, avi_ptr->video_opt.min_height * avi_ptr->video_opt.factor, avi_ptr->video_opt.min_width * avi_ptr->video_opt.factor * avi_ptr->video_opt.min_height * avi_ptr->video_opt.factor * 3 ); if ( *size == -1 ) { cerr << "Warning! Compress too few data" << endl; return -1; } image = jpeg_image; return 0;} // }}}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -