我们都有一个很直观的感觉:在Linux系统下面,登录优酷这种视频网站,会导致CPU消耗很大,甚至出现跳帧的现象。那究竟是什么原因导致的呢?这里面有一个很重要的原因就是对于视频播放的显卡加速问题。
我们可以将Firefox的视频插件分为两类:
以mplayer plugin(现在为gecko media
player)为代表的视频插件
以Flash和Silverlight(Linux下为Moonlight)为代表的插件
这两种方式实现差别很大。第一种插件使用插件创建一个GTK的窗口,接受键盘的操作并传递给后台的播放程序。第二种方式实现比较复杂。特别是在实现无窗口模式的时候(Windowless
mode)。无窗口模式工作时,所有的键盘操作插件都需要从Firefox获取。同时,所有的显示(Rendering)都需要使用Firefox来完成。而现在很多Flash视频网站工作在这种模式之下。我们主要来谈一下这种工作模式的实现困境。
在开源Flash的实现Swfdec中,有一段描述为什么性能低的说明:
http://swfdec.freedesktop.org/wiki/FAQ
What do I have to do to make video
playback in Swfdec as fast as with mplayer?
The short answer: It’s hard. Here’s the problem: As you might know,
hardware has a dedicated method to display video, called the video
overlay. That is what xv and in turn mplayer and ffmpeg use. It has
the following features: * reserve a rectangular region on the
screen for video display * move a memory rectangular image of YUV
video data to that region and scale it to fit. That’s not a lot and
works well enough for video, but not for Flash. Flash allows
rendering stuff on top of the video (the end screen on Youtube for
example has the last video image shine through) and it allows
translucent videos and drawing non-rectangular parts of videos. All
of this is not supported by xv, which is why we decided to not go
through the pain to use it. The unfortunate side effect is that
currently a lot more horsepower is required to display a video via
Swfdec.
The end goal is to use OpenGL and its video extensions to speed up
Flash video. That should make it as fast as xv for graphic cards
that provide these features (almost all current graphic cards do).
But then, there is currently no OpenGL cairo backend, even though
there’s constantly talk about doing one.
这里面说了两个问题:不能使用YUV直接显示视频帧和没有使用显卡加速。
现在很多视频采用YUV颜色空间。但是现在Firefox在做显示时使用的RGB颜色空间。实际上这是由于Cairo不支持YUV颜色空间。因此,插件在做显示前都需要将视频帧的YUV转为RGB。这是一个很消耗CPU的操作:要对每一帧的每一个像素进行这种YUV->RGB的转换。很显然,这将消耗太多的CPU资源。
在Linux系统下Mplayer使用xv输出时,效率是比较好的。但是在编写Firefox插件时,这种方式是不能使用的(我不知道怎么用,哪位如果知道,望不吝赐教)。因此,在对视频帧进行缩放时,也将遇到效率问题。幸运的是,cairo在这方面性能是能够保证的。
以上问题可能的解决办法就是与OpenGL的结合。现在webgl已经引入了Firefox,但是在Linux系统下性能如何,接口如何还需要观察。目前来看,cairo直接和OpenGL结合使用,没有带来太多的性能改进。这几个测试程序运行起来的效率依旧是不高的。
与Flash类似,Moonlight的效率也很低。
Moonlight播放视频可以达到上面的视频播放效果,在视频的上面有一张半透明的图片。这得益于插件的无窗口模式,然而这种效果消耗的CPU却是很多的。使用Vtune对Moonlight进行了分析,得到了下面的结果:
Moonlight的Vtune分析结果
可以看出YUV->RGB的Covert占用了近80%的CPU时间。这也就是为什么在Linux系统下视频插件效率低的共同问题根源。
有什么办法可以解决这个问题吗?
我也还在想……