decode: refactor
state: check ref_img allocation