This code appeared in the Daala project first and was originally committed by Nathan Egge. Change-Id: Iadce416a091929c51b46637ebdec984cddcaf18c