All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jcodec.codecs.common.biari.VPxBooleanEncoder Maven / Gradle / Ivy

There is a newer version: 0.2.5
Show newest version
package org.jcodec.codecs.common.biari;

import java.nio.ByteBuffer;

import org.jcodec.codecs.vpx.VPXConst;

/**
 * This class is part of JCodec ( www.jcodec.org ) This software is distributed
 * under FreeBSD License
 * 
 * Containes boolean encoder from VPx codecs
 * 
 * @author The JCodec project
 * 
 */
public class VPxBooleanEncoder {
    private ByteBuffer out;
    private int lowvalue;
    private int range;
    private int count;

    public VPxBooleanEncoder(ByteBuffer out) {
        this.out = out;
        lowvalue = 0;
        range = 255;
        count = -24;
    }

    public void writeBit(int prob, int bb) {
        int split = 1 + (((range - 1) * prob) >> 8);

        if (bb != 0) {
            lowvalue += split;
            range = range - split;
        } else {
            range = split;
        }

        int shift = VPXConst.vp8Norm[range];
        range <<= shift;
        count += shift;

        if (count >= 0) {
            int offset = shift - count;

            if (((lowvalue << (offset - 1)) & 0x80000000) != 0) {
                int x = out.position() - 1;

                while (x >= 0 && out.get(x) == -1) {
                    out.put(x, (byte) 0);
                    x--;
                }

                out.put(x, (byte) ((out.get(x) & 0xff) + 1));
            }

            out.put((byte) (lowvalue >> (24 - offset)));
            lowvalue <<= offset;
            shift = count;
            lowvalue &= 0xffffff;
            count -= 8;
        }

        lowvalue <<= shift;
    }

    public void stop() {
        int i;

        for (i = 0; i < 32; i++)
            writeBit(128, 0);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy