gusucode.com > 嵌入式linux系统的网络编程源码程序 > 嵌入式linux系统的网络编程源码程序/视频会议源码/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; } // }}}