Originally committed as revision 22404 to svn://svn.ffmpeg.org/ffmpeg/trunk
Patch by Kostya, minor fixes by me. Originally committed as revision 21958 to svn://svn.ffmpeg.org/ffmpeg/trunk