FileDocCategorySizeDatePackage
BitstreamReader.javaAPI Docmp4parser 1.0-RC-174982Wed Dec 19 20:10:37 GMT 2012com.googlecode.mp4parser.h264.read

BitstreamReader

public class BitstreamReader extends Object
A dummy implementation of H264 RBSP reading
author
Stanislav Vitvitskiy

Fields Summary
private InputStream
is
private int
curByte
private int
nextByte
int
nBit
protected static int
bitsRead
protected com.googlecode.mp4parser.h264.CharCache
debugBits
Constructors Summary
public BitstreamReader(InputStream is)


         
        this.is = is;
        curByte = is.read();
        nextByte = is.read();
    
Methods Summary
private voidadvance()

        curByte = nextByte;
        nextByte = is.read();
        nBit = 0;
    
public voidclose()

    
public longgetBitPosition()

        return (bitsRead * 8 + (nBit % 8));
    
public intgetCurBit()

        return nBit;
    
public booleanisByteAligned()

        return (nBit % 8) == 0;
    
public booleanmoreRBSPData()

        if (nBit == 8) {
            advance();
        }
        int tail = 1 << (8 - nBit - 1);
        int mask = ((tail << 1) - 1);
        boolean hasTail = (curByte & mask) == tail;

        return !(curByte == -1 || (nextByte == -1 && hasTail));
    
public intpeakNextBits(int n)

        if (n > 8)
            throw new IllegalArgumentException("N should be less then 8");
        if (nBit == 8) {
            advance();
            if (curByte == -1) {
                return -1;
            }
        }
        int[] bits = new int[16 - nBit];

        int cnt = 0;
        for (int i = nBit; i < 8; i++) {
            bits[cnt++] = (curByte >> (7 - i)) & 0x1;
        }

        for (int i = 0; i < 8; i++) {
            bits[cnt++] = (nextByte >> (7 - i)) & 0x1;
        }

        int result = 0;
        for (int i = 0; i < n; i++) {
            result <<= 1;
            result |= bits[i];
        }

        return result;
    
public intread1Bit()

        if (nBit == 8) {
            advance();
            if (curByte == -1) {
                return -1;
            }
        }
        int res = (curByte >> (7 - nBit)) & 1;
        nBit++;

        debugBits.append(res == 0 ? '0" : '1");
        ++bitsRead;

        return res;
    
public intreadByte()

        if (nBit > 0) {
            advance();
        }

        int res = curByte;

        advance();

        return res;
    
public longreadNBit(int n)

        if (n > 64)
            throw new IllegalArgumentException("Can not readByte more then 64 bit");

        long val = 0;

        for (int i = 0; i < n; i++) {
            val <<= 1;
            val |= read1Bit();
        }

        return val;
    
public longreadRemainingByte()

        return readNBit(8 - nBit);