Patch by James Zern for Google, Inc., jzern google com backportd r23191,23303,23307-23308 by conrad, cehoyos and mstorsjo Originally committed as revision 23374 to svn://svn.ffmpeg.org/ffmpeg/branches/0.6