Support encode from buffer and surface

The function should be debugged