Decoder mode

This fix #952 and #864 

@trustin please review