Add encoder lock to prevent concurrent access to encoder state.