Replace SSIM algorithm with code from x264

Alas, I have no time to debug it.

Also, fix the main reading code to read the last frames of the file.