1/*
2Copyright (c) 2011 Stanislav Vitvitskiy
3
4Permission is hereby granted, free of charge, to any person obtaining a copy of this
5software and associated documentation files (the "Software"), to deal in the Software
6without restriction, including without limitation the rights to use, copy, modify,
7merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
8permit persons to whom the Software is furnished to do so, subject to the following
9conditions:
10
11The above copyright notice and this permission notice shall be included in all copies or
12substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
17FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
18TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
19OR OTHER DEALINGS IN THE SOFTWARE.
20*/
21package com.googlecode.mp4parser.h264.read;
22
23import com.googlecode.mp4parser.h264.CharCache;
24
25import java.io.IOException;
26import java.io.InputStream;
27
28/**
29 * A dummy implementation of H264 RBSP reading
30 *
31 * @author Stanislav Vitvitskiy
32 */
33public class BitstreamReader {
34    private InputStream is;
35    private int curByte;
36    private int nextByte;
37    int nBit;
38    protected static int bitsRead;
39
40    protected CharCache debugBits = new CharCache(50);
41
42    public BitstreamReader(InputStream is) throws IOException {
43        this.is = is;
44        curByte = is.read();
45        nextByte = is.read();
46    }
47
48    /*
49      * (non-Javadoc)
50      *
51      * @see ua.org.jplayer.javcodec.h264.RBSPInputStream#read1Bit()
52      */
53    public int read1Bit() throws IOException {
54        if (nBit == 8) {
55            advance();
56            if (curByte == -1) {
57                return -1;
58            }
59        }
60        int res = (curByte >> (7 - nBit)) & 1;
61        nBit++;
62
63        debugBits.append(res == 0 ? '0' : '1');
64        ++bitsRead;
65
66        return res;
67    }
68
69    /*
70      * (non-Javadoc)
71      *
72      * @see ua.org.jplayer.javcodec.h264.RBSPInputStream#readNBit(int)
73      */
74    public long readNBit(int n) throws IOException {
75        if (n > 64)
76            throw new IllegalArgumentException("Can not readByte more then 64 bit");
77
78        long val = 0;
79
80        for (int i = 0; i < n; i++) {
81            val <<= 1;
82            val |= read1Bit();
83        }
84
85        return val;
86    }
87
88    private void advance() throws IOException {
89        curByte = nextByte;
90        nextByte = is.read();
91        nBit = 0;
92    }
93
94    /*
95      * (non-Javadoc)
96      *
97      * @see ua.org.jplayer.javcodec.h264.RBSPInputStream#readByte()
98      */
99    public int readByte() throws IOException {
100        if (nBit > 0) {
101            advance();
102        }
103
104        int res = curByte;
105
106        advance();
107
108        return res;
109    }
110
111    /*
112      * (non-Javadoc)
113      *
114      * @see ua.org.jplayer.javcodec.h264.RBSPInputStream#moreRBSPData()
115      */
116    public boolean moreRBSPData() throws IOException {
117        if (nBit == 8) {
118            advance();
119        }
120        int tail = 1 << (8 - nBit - 1);
121        int mask = ((tail << 1) - 1);
122        boolean hasTail = (curByte & mask) == tail;
123
124        return !(curByte == -1 || (nextByte == -1 && hasTail));
125    }
126
127    public long getBitPosition() {
128        return (bitsRead * 8 + (nBit % 8));
129    }
130
131    /*
132      * (non-Javadoc)
133      *
134      * @see ua.org.jplayer.javcodec.h264.RBSPInputStream#readRemainingByte()
135      */
136    public long readRemainingByte() throws IOException {
137        return readNBit(8 - nBit);
138    }
139
140    /*
141      * (non-Javadoc)
142      *
143      * @see ua.org.jplayer.javcodec.h264.RBSPInputStream#next_bits(int)
144      */
145    public int peakNextBits(int n) throws IOException {
146        if (n > 8)
147            throw new IllegalArgumentException("N should be less then 8");
148        if (nBit == 8) {
149            advance();
150            if (curByte == -1) {
151                return -1;
152            }
153        }
154        int[] bits = new int[16 - nBit];
155
156        int cnt = 0;
157        for (int i = nBit; i < 8; i++) {
158            bits[cnt++] = (curByte >> (7 - i)) & 0x1;
159        }
160
161        for (int i = 0; i < 8; i++) {
162            bits[cnt++] = (nextByte >> (7 - i)) & 0x1;
163        }
164
165        int result = 0;
166        for (int i = 0; i < n; i++) {
167            result <<= 1;
168            result |= bits[i];
169        }
170
171        return result;
172    }
173
174    /*
175      * (non-Javadoc)
176      *
177      * @see ua.org.jplayer.javcodec.h264.RBSPInputStream#byte_aligned()
178      */
179    public boolean isByteAligned() {
180        return (nBit % 8) == 0;
181    }
182
183    /*
184      * (non-Javadoc)
185      *
186      * @see ua.org.jplayer.javcodec.h264.RBSPInputStream#close()
187      */
188    public void close() throws IOException {
189    }
190
191    public int getCurBit() {
192        return nBit;
193    }
194}