Some refactoring to allow supporting multiple encoders in the encode thread (far from being complete though).