Improve Matroska file handling

I've started to move file validation as the very first thing, and create a file stream in the process, because it seems that both things are always made anyway.

Also fixed some minor memory leaks in the process, and I have plans to improve a little bit here.

Don't merge this yet.