Fixes issue562 and issue621. Originally committed as revision 15183 to svn://svn.ffmpeg.org/ffmpeg/trunk