• 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.write;
22 
23 import com.googlecode.mp4parser.h264.Debug;
24 
25 import java.io.IOException;
26 import java.io.OutputStream;
27 
28 /**
29  * A dummy implementation of H264 RBSP output stream
30  *
31  * @author Stanislav Vitvitskiy
32  */
33 public class BitstreamWriter {
34 
35     private final OutputStream os;
36     private int[] curByte = new int[8];
37     private int curBit;
38 
BitstreamWriter(OutputStream out)39     public BitstreamWriter(OutputStream out) {
40         this.os = out;
41     }
42 
43     /*
44      * (non-Javadoc)
45      *
46      * @see ua.org.jplayer.javcodec.h264.H264BitOutputStream#flush()
47      */
flush()48     public void flush() throws IOException {
49         for (int i = curBit; i < 8; i++) {
50             curByte[i] = 0;
51         }
52         curBit = 0;
53         writeCurByte();
54     }
55 
writeCurByte()56     private void writeCurByte() throws IOException {
57         int toWrite = (curByte[0] << 7) | (curByte[1] << 6) | (curByte[2] << 5)
58                 | (curByte[3] << 4) | (curByte[4] << 3) | (curByte[5] << 2)
59                 | (curByte[6] << 1) | curByte[7];
60         os.write(toWrite);
61     }
62 
63     /*
64      * (non-Javadoc)
65      *
66      * @see ua.org.jplayer.javcodec.h264.H264BitOutputStream#write1Bit(int)
67      */
write1Bit(int value)68     public void write1Bit(int value) throws IOException {
69         Debug.print(value);
70         if (curBit == 8) {
71             curBit = 0;
72             writeCurByte();
73         }
74         curByte[curBit++] = value;
75     }
76 
77     /*
78      * (non-Javadoc)
79      *
80      * @see ua.org.jplayer.javcodec.h264.H264BitOutputStream#writeNBit(long,
81      * int)
82      */
writeNBit(long value, int n)83     public void writeNBit(long value, int n) throws IOException {
84         for (int i = 0; i < n; i++) {
85             write1Bit((int) (value >> (n - i - 1)) & 0x1);
86         }
87     }
88 
89     /*
90      * (non-Javadoc)
91      *
92      * @see
93      * ua.org.jplayer.javcodec.h264.H264BitOutputStream#writeRemainingZero()
94      */
writeRemainingZero()95     public void writeRemainingZero() throws IOException {
96         writeNBit(0, 8 - curBit);
97     }
98 
99     /*
100      * (non-Javadoc)
101      *
102      * @see ua.org.jplayer.javcodec.h264.H264BitOutputStream#writeByte(int)
103      */
writeByte(int b)104     public void writeByte(int b) throws IOException {
105         os.write(b);
106 
107     }
108 }