毕业论文(设计)基于VFW 的视频应用程序开发38113.doc
基于VFW的视频应用程序开发刘肃亮,周明全,韦智勇(西北大学 可视化研究所,陕西 西安 710069)摘 要:介绍了微软软件开发包VFW的体系结构,阐明了利用VFW软件包进行视频应用程序开发的基本原理,设计了在Windows平台上实现视频的实时捕获、编辑和播放的方案,给出了主要代码。关 键 字: 视频捕获;SDK;VFW;AVI;MCI中图分类号:TP391 文献标识码:A 文章编号:1000-274X(2003)0037-05当前,在Windows 平台下开发视频应用程序一般采用两种方式:一种是基于视频采集卡所附带的二次软件开发包SDK(Software development kit)进行。这种方式的优点是应用方便,容易上手,缺点是对硬件的依赖性较强,灵活性差,且功能参差不齐,不能充分满足各种视频应用程序的开发需要;另一种方式是基于VFW(Video for Windows)进行的。VFW 是Microsoft公司为开发Windows平台下的视频应用程序提供的软件工具包,提供了一系列应用程序编程接口(API),用户可以通过它们很方便地实现视频捕获1、视频编辑及视频播放等通用功能,还可利用回调函数开发更复杂的视频应用程序。它的特点是播放视频时不需要专用的硬件设备,而且应用灵活,可以满足视频应用程序开发的需要。Windows操作系统自身就携带了VFW,系统安装时,会自动安装VFW的相关组件。VC+自4.0以来就支持VFW,大大简化了视频应用程序的开发。目前,PC机上多媒体应用程序的视频部分,大都是利用VFW API开发的。1 VFW 的体系结构VFW以消息驱动方式对视频设备进行存取,可以很方便地控制设备数据流的工作过程。目前,大多数的视频采集卡驱动程序都支持VFW接口,它主要包括多个动态连接库,通过这些组件间的协调合作,来完成视频的捕获、视频压缩及播放功能。VFW体系结构如图1所示。1)VICAP.DLL:主要实现视频捕获功能,包含了用于视频捕获的函数,为音像交错AVI (Audio video interleaved)格式文件和视频、音频设备程序提供一个高级接口。2)MSVIDEO.DLL:能够将视频捕获窗口与获驱动设备连接起来,支持ICM视频编码服务。3)MCIAVI.DRV:包含MCI(Media control interface)命令解释器,实现回放功能。4)AVIFILE.DLL:提供对AVI文件的读写操作等文件管理功能。5)ICM ( Installable compression manager ):即压缩管理器,提供对存储在AVI文件中视频图像数据的压缩、解压缩服务。6)ACM ( Audio Compression Manager ):即音频压缩管理器,提供实时音频压缩及解压缩功能。Capture ApplicationPlayback ApplicationEdit ApplicationAVICAP.DLL AVICAPMSVIDEO.DLL MCIWnd ACMMCIAVI.DRVMCI CommandInterpreterMSVIDEO.DLLVideo in ICMChannelAVIFILE.DLLFile / StreamHandlerMSVIDEO.DLLDrawdib ICM Capture ApplicationPlayback ApplicationEdit ApplicationAVICAP.DLL AVICAPMSVIDEO.DLL MCIWnd ACMMCIAVI.DRVMCI CommandInterpreterMSVIDEO.DLLVideo in ICMChannelAVIFILE.DLLFile / StreamHandlerMSVIDEO.DLLDrawdib ICM Capture ApplicationPlayback ApplicationEdit ApplicationAVICAP.DLL AVICAPMSVIDEO.DLL MCIWnd ACMMCIAVI.DRVMCI CommandInterpreterMSVIDEO.DLLVideo in ICMChannelAVIFILE.DLLFile / StreamHandlerMSVIDEO.DLLDrawdib ICM 图 1VFW的体系结构 Fig.1Architecture of VFW2 视频捕获视频数据的实时采集,主要通过AVICAP模块中的消息、宏函数、结构以及回调函数来完成。视频捕获的一般过程如下:2.1建立捕获窗口利用AVICAP 组件函数 capCreateCaptureWindow() 建立视频捕获窗口,它是所有捕获工作及设置的基础,其主要功能包括: 动态地同视频和音频输入器连接或断开; 设置视频捕获速率; 提供视频源、视频格式以及是否采用视频压缩的对话框; 设置视频采集的显示模式为Overlay或为Preview; 实时获取每一帧视频数据; 将一视频流和音频流捕获并保存到一个AVI文件中; 捕获某一帧数字视频数据,并将单帧图像以DIB格式保存; 指定捕获数据的文件名,并能将捕获的内容拷贝到另一文件。2.2登记回调函数2登记回调函数用来实现用户的一些特殊需要。在以一些实时监控系统或视频会议系统中,需要将数据流在写入磁盘以前就必须加以处理,达到实时功效。应用程序可用捕获窗来登记回调函数,以便及时处理以下情况:捕获窗状态改变、出错、使用视频或音频缓存、放弃控制权等,相应的回调函数分别为 capStatusCallback(), capErrorCallback(), capVideoStreamCallback(), capWaveStreamCallback(),capYieldCallback()。2.3获取捕获窗口的缺省设置通过宏capCaptureGetSetup(hWndCap,&m_Parms,sizeof(m_Parms)来完成。2.4设置捕获窗口的相关参数通过宏capCaptureSetSetup(hWndCap,&m_Parms,sizeof(m_Parms)来完成。2.5连接捕获窗口与视频捕获卡通过宏capDriveConnect(hWndCap,0)来完成。2.6获取采集设备的功能和状态通过宏capDriverGetCaps(hWndCap,&m_CapDrvCap,sizeof(CAPDRIVERCAPS)来获取视频设备的能力,通过宏capGetStatus(hWndCap,&m_CapStatus,sizeof(m_CapStatus)来获取视频设备的状态。2.7设置捕获窗口显示模式视频显示有Overlay(叠加)和Preview(预览)两种模式。在叠加模式下,捕获视频数据布展系统资源,显示速度快,视频采集格式为YUV格式,可通过capOverlay(hWndCap,TRUE)来设置;预览模式下要占用系统资源,视频由系统调用GDI函数在捕获窗显示,显示速度慢,它支持RGB视频格式。2.8捕获图像到缓存或文件并作相应处理若要对采集数据进行实时处理,则应利用回调机制,由capSetCallbackOnFrame(hWndCap, FrameCall-backProc)完成单帧视频采集;由capSetCallbackOnVideoStream(hWndCap, VideoCallbackProc)完成视频流采集。如果要保存采集数据,则可调用capCaptureSequence(hWnd);要指定文件名,可调用capFileSetCap-ture(hwnd, Filename)。2.9终止视频捕获断开与视频采集设备的连接调用capCatureStop(hWndCap)停止采集,调用capDriverDisconnect(hWndCap), 断开视频窗口与捕获驱动程序的连接。3 视频编辑和播放利用VFW,不仅可以实现视频流的实时采集,还提供了编辑和播放功能,主要通过AVIFILE、ICM、ACM、MCIWnd 等组件之间的协作来完成。3.1 编辑处理AVI3视频文件的一般流程(图2)断开视频流打开文件连接视频流编辑操作初 始 化释放文件图2编辑视频文件的一般流程图Fig.2General flow chart of editing video files1) AVIFileInit();/初始化;2) AVIFileOpen(); /打开一个AVI文件并获文件的句柄;3) AVIFileInfo(); /获取文件的相关信息,如图像的Width和Height等;4) AVIFileGetStream(); /建立一个指向需要访问的数据流的指针;5) AVIStreamInfo(); /获取存储数据流信息的AVISTREAMINFO结构;6) AVIStreamRead(); /读取数据流中的原始数据, 对AVI文件进行所需的编辑处理;7) AVIStreamRelease(); /释放指向视频流的指针;8) AVIFileRelease();AVIFileExit(); /释放AVI文件。若数据是压缩过的,则用AVIStreamGetFrameOpen(),AVIStreamGetFrame()和AVIStreamGetFrameClose()来操作,可以完成对视频流的逐帧分解。3.2 视频播放对于实现视频流的播放,VFW提供了MCIWnd窗口类4,主要用于创建视频播放区,控制并修改MCI窗口当前加载媒体的属性。一个由函数、消息和宏组成的库与MCIWnd相关联,通过它们可以进行AVI文件操作,很方便地使应用程序完成视频播放功能。待添加的隐藏文字内容31)MCIWndCreate(); /注册MCIWnd窗口类,创建MCIWnd窗口,并指定窗口风格;2)AVIFileInit(); /初始化;3) AVIFileOpen(); /打开AVI文件;4) AVIFileGetStream(); /获得视频流;5)运用相关函数进行各种播放任务:MCIWndPlay()正向播放AVI文件内容,MCIWndPlayReverse()反向播放,MCIWndResume() 恢复播放,MCIWndPlayPause()暂停播放,MCIWndStop()停止播放等等。6) AVIStreamRelease(); /释放视频流;7)AVIFileRease();AVIFileExit(); /断开与AVI文件的连接,释放视频源。由以上步骤可以看出,视频播放是视频编辑其中的一种操作。4 结束语随着社会的发展,人们对多媒体技术的需求也越来越高,已经不满足单一的通信功能,要求影像与声音的同步服务,如可视电话、视频会议、各种实时监控系统、报警系统等,而数字视频技术则是多媒体技术的核心之一。微软推出的数字视频处理软件开发包Video for Windows,将开发者从硬件的底层束缚中解放出来,大大简化了数字视频捕获这一原本很复杂的工作,利用它可以很方便的将模拟视频信号数字化,并支持进一步处理和播放,为开发实时视频应用程序开辟了道路。参考文献:1 周长发. VC+多媒体编程技术与实例M. 北京:电子工业出版社, 1999.2 张星明. 视频图像捕获及运动检测技术的实现J. 计算机工程, 2002, 28(8):130-132. 3 肖永隆,王 理. 利用VFW库函数快速分解AVI数据流J. 信息工程大学学报, 2001, 2(3):39-42.4 喻其炳. 多媒体信息处理在Windows中的应用J. 重庆工商大学学报, 2003, 20(1):64-66.(编辑 曹大刚)Development of video applications based upon VFWLIU Su-liang,ZHOU Ming-quan,WEI Zhi-yong (Institute of Visualization Technology, Northwest University, Xian 710069, China) Abstract: The architecture of Microsoft software developing kit VFW is introduced, the fundamental principle of developing video applications by means of VFW is illustrated; a scheme to real-time capture, editing and display of video pictures is designed, and the related core codes are also offered.Key words: video capture; SDK; VFW; AVI; MCI作 者 简 介刘肃亮,男,河南商丘人,生于1972年10月。1995年毕业于河南大学数学系数学专业,获理学学士学位;1995年7月至2001年9月任教于商丘师范学院计算机系;2001年考入西北大学计算机系计算机软件与理论专业攻读硕士学位,师从可视化研究所周明全教授。研究方向为图像处理、计算机视觉与智能信息系统,参与了“城市区域交通可视化平台技术研究”及“医学影像数据库基于内容图像检索技术研究”等项目的开发,在西北大学学报(自然科学版)2003年专辑上发表有“Windows下的实时视频采集”。Editor's note: Judson Jones is a meteorologist, journalist and photographer. He has freelanced with CNN for four years, covering severe weather from tornadoes to typhoons. Follow him on Twitter: jnjonesjr (CNN) - I will always wonder what it was like to huddle around a shortwave radio and through the crackling static from space hear the faint beeps of the world's first satellite - Sputnik. I also missed watching Neil Armstrong step foot on the moon and the first space shuttle take off for the stars. Those events were way before my time.As a kid, I was fascinated with what goes on in the sky, and when NASA pulled the plug on the shuttle program I was heartbroken. Yet the privatized space race has renewed my childhood dreams to reach for the stars.As a meteorologist, I've still seen many important weather and space events, but right now, if you were sitting next to me, you'd hear my foot tapping rapidly under my desk. I'm anxious for the next one: a space capsule hanging from a crane in the New Mexico desert.It's like the set for a George Lucas movie floating to the edge of space.You and I will have the chance to watch a man take a leap into an unimaginable free fall from the edge of space - live.The (lack of) air up there Watch man jump from 96,000 feet Tuesday, I sat at work glued to the live stream of the Red Bull Stratos Mission. I watched the balloons positioned at different altitudes in the sky to test the winds, knowing that if they would just line up in a vertical straight line "we" would be go for launch.I feel this mission was created for me because I am also a journalist and a photographer, but above all I live for taking a leap of faith - the feeling of pushing the envelope into uncharted territory.The guy who is going to do this, Felix Baumgartner, must have that same feeling, at a level I will never reach. However, it did not stop me from feeling his pain when a gust of swirling wind kicked up and twisted the partially filled balloon that would take him to the upper end of our atmosphere. As soon as the 40-acre balloon, with skin no thicker than a dry cleaning bag, scraped the ground I knew it was over.How claustrophobia almost grounded supersonic skydiverWith each twist, you could see the wrinkles of disappointment on the face of the current record holder and "capcom" (capsule communications), Col. Joe Kittinger. He hung his head low in mission control as he told Baumgartner the disappointing news: Mission aborted.The supersonic descent could happen as early as Sunday.The weather plays an important role in this mission. Starting at the ground, conditions have to be very calm - winds less than 2 mph, with no precipitation or humidity and limited cloud cover. The balloon, with capsule attached, will move through the lower level of the atmosphere (the troposphere) where our day-to-day weather lives. It will climb higher than the tip of Mount Everest (5.5 miles/8.85 kilometers), drifting even higher than the cruising altitude of commercial airliners (5.6 miles/9.17 kilometers) and into the stratosphere. As he crosses the boundary layer (called the tropopause), he can expect a lot of turbulence.The balloon will slowly drift to the edge of space at 120,000 feet (22.7 miles/36.53 kilometers). Here, "Fearless Felix" will unclip. He will roll back the door.Then, I would assume, he will slowly step out onto something resembling an Olympic diving platform.Below, the Earth becomes the concrete bottom of a swimming pool that he wants to land on, but not too hard. Still, he'll be traveling fast, so despite the distance, it will not be like diving into the deep end of a pool. It will be like he is diving into the shallow end.Skydiver preps for the big jumpWhen he jumps, he is expected to reach the speed of sound - 690 mph (1,110 kph) - in less than 40 seconds. Like hitting the top of the water, he will begin to slow as he approaches the more dense air closer to Earth. But this will not be enough to stop him completely.If he goes too fast or spins out of control, he has a stabilization parachute that can be deployed to slow him down. His team hopes it's not needed. Instead, he plans to deploy his 270-square-foot (25-square-meter) main chute at an altitude of around 5,000 feet (1,524 meters).In order to deploy this chute successfully, he will have to slow to 172 mph (277 kph). He will have a reserve parachute that will open automatically if he loses consciousness at mach speeds.Even if everything goes as planned, it won't. Baumgartner still will free fall at a speed that would cause you and me to pass out, and no parachute is guaranteed to work higher than 25,000 feet (7,620 meters).It might not be the moon, but Kittinger free fell from 102,800 feet in 1960 - at the dawn of an infamous space race that captured the hearts of many. Baumgartner will attempt to break that record, a feat that boggles the mind. This is one of those monumental moments I will always remember, because there is no way I'd miss this.