1 | |
---|
2 | |
---|
3 | /** \file TVideoIOBits.cpp |
---|
4 | \brief bitstream file I/O class |
---|
5 | */ |
---|
6 | |
---|
7 | #include <cstdlib> |
---|
8 | #include <fcntl.h> |
---|
9 | #include <assert.h> |
---|
10 | #include <sys/stat.h> |
---|
11 | #include <fstream> |
---|
12 | #include <iostream> |
---|
13 | |
---|
14 | #include "TVideoIOBits.h" |
---|
15 | |
---|
16 | using namespace std; |
---|
17 | |
---|
18 | // ==================================================================================================================== |
---|
19 | // Public member functions |
---|
20 | // ==================================================================================================================== |
---|
21 | |
---|
22 | /** |
---|
23 | \param pchFile file name string |
---|
24 | \param bWriteMode file open mode |
---|
25 | */ |
---|
26 | Void TVideoIOBits::openBits( char* pchFile, Bool bWriteMode ) |
---|
27 | { |
---|
28 | if ( bWriteMode ) |
---|
29 | { |
---|
30 | m_cHandle.open( pchFile, ios::binary | ios::out ); |
---|
31 | |
---|
32 | if( m_cHandle.fail() ) |
---|
33 | { |
---|
34 | printf("\nfailed to write Bitstream file\n"); |
---|
35 | exit(0); |
---|
36 | } |
---|
37 | } |
---|
38 | else |
---|
39 | { |
---|
40 | m_cHandle.open( pchFile, ios::binary | ios::in ); |
---|
41 | |
---|
42 | if( m_cHandle.fail() ) |
---|
43 | { |
---|
44 | printf("\nfailed to read Bitstream file\n"); |
---|
45 | exit(0); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | return; |
---|
50 | } |
---|
51 | |
---|
52 | Void TVideoIOBits::closeBits() |
---|
53 | { |
---|
54 | m_cHandle.close(); |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | \param rpcBitstream bitstream class pointer |
---|
59 | \retval true if EOF is reached |
---|
60 | */ |
---|
61 | Bool TVideoIOBits::readBits( TComBitstream*& rpcBitstream ) |
---|
62 | { |
---|
63 | UInt uiBytes = 0; |
---|
64 | |
---|
65 | rpcBitstream->rewindStreamPacket(); |
---|
66 | |
---|
67 | // check end-of-file |
---|
68 | if ( m_cHandle.eof() ) return true; |
---|
69 | |
---|
70 | // read 32-bit packet size |
---|
71 | m_cHandle.read( reinterpret_cast<char*>(&uiBytes), sizeof (Int) ); |
---|
72 | |
---|
73 | // kolya |
---|
74 | if ( m_cHandle.eof() ) return true; //additional insertion to avoid over-reading, for <fstream> |
---|
75 | |
---|
76 | // read packet data |
---|
77 | m_cHandle.read( reinterpret_cast<char*>(rpcBitstream->getBuffer()), uiBytes ); |
---|
78 | |
---|
79 | // initialize parsing process |
---|
80 | rpcBitstream->initParsing ( uiBytes ); |
---|
81 | |
---|
82 | assert (uiBytes >= 4); |
---|
83 | |
---|
84 | return false; |
---|
85 | } |
---|
86 | |
---|
87 | /** \param pcBitstream bitstream class pointer |
---|
88 | */ |
---|
89 | Void TVideoIOBits::writeBits( TComBitstream* pcBitstream ) |
---|
90 | { |
---|
91 | UInt* plBuff = pcBitstream->getStartStream(); |
---|
92 | UInt uiBytes = pcBitstream->getNumberOfWrittenBits() >> 3; |
---|
93 | |
---|
94 | // write 32-bit packet size |
---|
95 | m_cHandle.write( reinterpret_cast<char*>(&uiBytes ), sizeof(UInt) ); |
---|
96 | |
---|
97 | // write packet data |
---|
98 | m_cHandle.write( reinterpret_cast<char*>(plBuff ), uiBytes ); |
---|
99 | } |
---|
100 | |
---|
101 | //////////////////////////////////////////////////////////////////// |
---|
102 | |
---|
103 | Void TVideoIOBitsStartCode::openBits( char* pchFile, Bool bWriteMode ) |
---|
104 | { |
---|
105 | if ( bWriteMode ) |
---|
106 | { |
---|
107 | m_cHandle.open( pchFile, ios::binary | ios::out ); |
---|
108 | |
---|
109 | if( m_cHandle.fail() ) |
---|
110 | { |
---|
111 | printf("\nfailed to write Bitstream file\n"); |
---|
112 | exit(0); |
---|
113 | } |
---|
114 | } |
---|
115 | else |
---|
116 | { |
---|
117 | m_cHandle.open( pchFile, ios::binary | ios::in ); |
---|
118 | |
---|
119 | if( m_cHandle.fail() ) |
---|
120 | { |
---|
121 | printf("\nfailed to read Bitstream file\n"); |
---|
122 | exit(0); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | return; |
---|
127 | } |
---|
128 | |
---|
129 | Void TVideoIOBitsStartCode::closeBits() |
---|
130 | { |
---|
131 | m_cHandle.close(); |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | \param rpcBitstream bitstream class pointer |
---|
136 | \retval true if EOF is reached |
---|
137 | */ |
---|
138 | Bool TVideoIOBitsStartCode::readBits( TComBitstream*& rpcBitstream ) |
---|
139 | { |
---|
140 | rpcBitstream->rewindStreamPacket(); |
---|
141 | |
---|
142 | // check end-of-file |
---|
143 | if ( m_cHandle.eof() ) return true; |
---|
144 | |
---|
145 | UInt uiPacketSize = 0; |
---|
146 | if( 0 != xFindNextStartCode( uiPacketSize, reinterpret_cast<UChar*>(rpcBitstream->getBuffer()) ) ) |
---|
147 | { |
---|
148 | return true; |
---|
149 | } |
---|
150 | |
---|
151 | // initialize parsing process |
---|
152 | rpcBitstream->initParsingConvertPayloadToRBSP( uiPacketSize ); |
---|
153 | return false; |
---|
154 | } |
---|
155 | |
---|
156 | int TVideoIOBitsStartCode::xFindNextStartCode(UInt& ruiPacketSize, UChar* pucBuffer) |
---|
157 | { |
---|
158 | UInt uiDummy = 0; |
---|
159 | m_cHandle.read( reinterpret_cast<char*>(&uiDummy), 3 ); |
---|
160 | if ( m_cHandle.eof() ) return -1; |
---|
161 | assert( 0 == uiDummy ); |
---|
162 | |
---|
163 | m_cHandle.read( reinterpret_cast<char*>(&uiDummy), 1 ); |
---|
164 | if ( m_cHandle.eof() ) return -1; |
---|
165 | assert( 1 == uiDummy ); |
---|
166 | |
---|
167 | Int iNextStartCodeBytes = 0; |
---|
168 | Int iBytesRead = 0; |
---|
169 | UInt uiZeros = 0; |
---|
170 | while( true ) |
---|
171 | { |
---|
172 | UChar ucByte = 0; |
---|
173 | m_cHandle.read( reinterpret_cast<char*>(&ucByte), 1 ); |
---|
174 | if ( m_cHandle.eof() ) |
---|
175 | { |
---|
176 | iNextStartCodeBytes = 0; |
---|
177 | break; |
---|
178 | } |
---|
179 | pucBuffer[iBytesRead++] = ucByte; |
---|
180 | if( 1 < ucByte ) |
---|
181 | { |
---|
182 | uiZeros = 0; |
---|
183 | } |
---|
184 | else if( 0 == ucByte ) |
---|
185 | { |
---|
186 | uiZeros++; |
---|
187 | } |
---|
188 | else if( uiZeros > 2) |
---|
189 | { |
---|
190 | iNextStartCodeBytes = 3 + 1; |
---|
191 | break; |
---|
192 | } |
---|
193 | else |
---|
194 | { |
---|
195 | uiZeros = 0; |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | ruiPacketSize = iBytesRead - iNextStartCodeBytes; |
---|
200 | |
---|
201 | m_cHandle.seekg( -iNextStartCodeBytes, ios::cur ); |
---|
202 | return 0; |
---|
203 | } |
---|
204 | |
---|
205 | /** |
---|
206 | \param pcBitstream bitstream class pointer |
---|
207 | */ |
---|
208 | Void TVideoIOBitsStartCode::writeBits( TComBitstream* pcBitstream ) |
---|
209 | { |
---|
210 | UInt* plBuff = pcBitstream->getStartStream(); |
---|
211 | UInt uiBytes = pcBitstream->getNumberOfWrittenBits() >> 3; |
---|
212 | Char ucZero = 0; |
---|
213 | Char ucOne = 1; |
---|
214 | |
---|
215 | // write 32-bit packet size |
---|
216 | m_cHandle.write( &ucZero , sizeof(Char) ); |
---|
217 | m_cHandle.write( &ucZero , sizeof(Char) ); |
---|
218 | m_cHandle.write( &ucZero , sizeof(Char) ); |
---|
219 | m_cHandle.write( &ucOne , sizeof(Char) ); |
---|
220 | |
---|
221 | // write packet data |
---|
222 | m_cHandle.write( reinterpret_cast<char*>(plBuff ), uiBytes ); |
---|
223 | } |
---|
224 | |
---|
225 | |
---|