based on a patch by Mathieu Velten (matmaul gmail com). Originally committed as revision 15324 to svn://svn.ffmpeg.org/ffmpeg/trunk