mpegts: Fix memory leaks and related crashes in mpegs_write_header()