• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 Copyright (c) 2011 Stanislav Vitvitskiy
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy of this
5 software and associated documentation files (the "Software"), to deal in the Software
6 without restriction, including without limitation the rights to use, copy, modify,
7 merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
8 permit persons to whom the Software is furnished to do so, subject to the following
9 conditions:
10 
11 The above copyright notice and this permission notice shall be included in all copies or
12 substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
17 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
18 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
19 OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21 package com.googlecode.mp4parser.h264.read;
22 
23 
24 import com.googlecode.mp4parser.h264.BTree;
25 
26 import java.io.IOException;
27 import java.io.InputStream;
28 
29 import static com.googlecode.mp4parser.h264.Debug.println;
30 
31 
32 public class CAVLCReader extends BitstreamReader {
33 
CAVLCReader(InputStream is)34     public CAVLCReader(InputStream is) throws IOException {
35         super(is);
36     }
37 
readNBit(int n, String message)38     public long readNBit(int n, String message) throws IOException {
39         long val = readNBit(n);
40 
41         trace(message, String.valueOf(val));
42 
43         return val;
44     }
45 
46     /**
47      * Read unsigned exp-golomb code
48      *
49      * @return
50      * @throws java.io.IOException
51      * @throws java.io.IOException
52      */
readUE()53     private int readUE() throws IOException {
54         int cnt = 0;
55         while (read1Bit() == 0)
56             cnt++;
57 
58         int res = 0;
59         if (cnt > 0) {
60             long val = readNBit(cnt);
61 
62             res = (int) ((1 << cnt) - 1 + val);
63         }
64 
65         return res;
66     }
67 
68     /*
69       * (non-Javadoc)
70       *
71       * @see
72       * ua.org.jplayer.javcodec.h264.H264BitInputStream#readUE(java.lang.String)
73       */
readUE(String message)74     public int readUE(String message) throws IOException {
75         int res = readUE();
76 
77         trace(message, String.valueOf(res));
78 
79         return res;
80     }
81 
readSE(String message)82     public int readSE(String message) throws IOException {
83         int val = readUE();
84 
85         int sign = ((val & 0x1) << 1) - 1;
86         val = ((val >> 1) + (val & 0x1)) * sign;
87 
88         trace(message, String.valueOf(val));
89 
90         return val;
91     }
92 
readBool(String message)93     public boolean readBool(String message) throws IOException {
94 
95         boolean res = read1Bit() == 0 ? false : true;
96 
97         trace(message, res ? "1" : "0");
98 
99         return res;
100     }
101 
readU(int i, String string)102     public int readU(int i, String string) throws IOException {
103         return (int) readNBit(i, string);
104     }
105 
read(int payloadSize)106     public byte[] read(int payloadSize) throws IOException {
107         byte[] result = new byte[payloadSize];
108         for (int i = 0; i < payloadSize; i++) {
109             result[i] = (byte) readByte();
110         }
111         return result;
112     }
113 
readAE()114     public boolean readAE() {
115         // TODO: do it!!
116         throw new UnsupportedOperationException("Stan");
117     }
118 
readTE(int max)119     public int readTE(int max) throws IOException {
120         if (max > 1)
121             return readUE();
122         return ~read1Bit() & 0x1;
123     }
124 
readAEI()125     public int readAEI() {
126         // TODO: do it!!
127         throw new UnsupportedOperationException("Stan");
128     }
129 
readME(String string)130     public int readME(String string) throws IOException {
131         return readUE(string);
132     }
133 
readCE(BTree bt, String message)134     public Object readCE(BTree bt, String message) throws IOException {
135         while (true) {
136             int bit = read1Bit();
137             bt = bt.down(bit);
138             if (bt == null) {
139                 throw new RuntimeException("Illegal code");
140             }
141             Object i = bt.getValue();
142             if (i != null) {
143                 trace(message, i.toString());
144                 return i;
145             }
146         }
147     }
148 
readZeroBitCount(String message)149     public int readZeroBitCount(String message) throws IOException {
150         int count = 0;
151         while (read1Bit() == 0)
152             count++;
153 
154         trace(message, String.valueOf(count));
155 
156         return count;
157     }
158 
readTrailingBits()159     public void readTrailingBits() throws IOException {
160         read1Bit();
161         readRemainingByte();
162     }
163 
trace(String message, String val)164     private void trace(String message, String val) {
165         StringBuilder traceBuilder = new StringBuilder();
166         int spaces;
167         String pos = String.valueOf(bitsRead - debugBits.length());
168         spaces = 8 - pos.length();
169 
170         traceBuilder.append("@" + pos);
171 
172         for (int i = 0; i < spaces; i++)
173             traceBuilder.append(' ');
174 
175         traceBuilder.append(message);
176         spaces = 100 - traceBuilder.length() - debugBits.length();
177         for (int i = 0; i < spaces; i++)
178             traceBuilder.append(' ');
179         traceBuilder.append(debugBits);
180         traceBuilder.append(" (" + val + ")");
181         debugBits.clear();
182 
183         println(traceBuilder.toString());
184     }
185 }