Detect input encoding and convert to utf-8 if needed