source: 3DVCSoftware/branches/0.2-HHI/source/Lib/TLibVideoIO/TVideoIOBits.cpp @ 78

Last change on this file since 78 was 5, checked in by hhi, 13 years ago

Clean version with cfg-files

  • Property svn:eol-style set to native
File size: 6.7 KB
Line 
1/* The copyright in this software is being made available under the BSD
2 * License, included below. This software may be subject to other third party
3 * and contributor rights, including patent rights, and no such rights are
4 * granted under this license.
5 *
6 * Copyright (c) 2010-2011, ISO/IEC
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright notice,
13 *    this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above copyright notice,
15 *    this list of conditions and the following disclaimer in the documentation
16 *    and/or other materials provided with the distribution.
17 *  * Neither the name of the ISO/IEC nor the names of its contributors may
18 *    be used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34
35
36/** \file     TVideoIOBits.cpp
37    \brief    bitstream file I/O class
38*/
39
40#include <cstdlib>
41#include <fcntl.h>
42#include <assert.h>
43#include <sys/stat.h>
44#include <fstream>
45#include <iostream>
46
47#include "TVideoIOBits.h"
48
49using namespace std;
50
51// ====================================================================================================================
52// Public member functions
53// ====================================================================================================================
54
55/**
56 \param pchFile    file name string
57 \param bWriteMode file open mode
58 */
59Void TVideoIOBits::openBits( char* pchFile, Bool bWriteMode )
60{
61  if ( bWriteMode )
62  {
63    m_cHandle.open( pchFile, ios::binary | ios::out );
64   
65    if( m_cHandle.fail() )
66    {
67      printf("\nfailed to write Bitstream file\n");
68      exit(0);
69    }
70  }
71  else
72  {
73    m_cHandle.open( pchFile, ios::binary | ios::in );
74   
75    if( m_cHandle.fail() )
76    {
77      printf("\nfailed to read Bitstream file\n");
78      exit(0);
79    }
80  }
81 
82  return;
83}
84
85Void TVideoIOBits::closeBits()
86{
87  m_cHandle.close();
88}
89
90/**
91 \param  rpcBitstream    bitstream class pointer
92 \retval                 true if EOF is reached
93 */
94Bool TVideoIOBits::readBits( TComBitstream*& rpcBitstream )
95{
96  UInt  uiBytes = 0;
97 
98  rpcBitstream->rewindStreamPacket();
99 
100  // check end-of-file
101  if ( m_cHandle.eof() ) return true;
102 
103  // read 32-bit packet size
104  m_cHandle.read( reinterpret_cast<char*>(&uiBytes), sizeof (Int) );
105 
106  // kolya
107  if ( m_cHandle.eof() ) return true; //additional insertion to avoid over-reading, for <fstream>
108 
109  // read packet data
110  m_cHandle.read(  reinterpret_cast<char*>(rpcBitstream->getBuffer()), uiBytes );
111 
112  // initialize parsing process
113  rpcBitstream->initParsing ( uiBytes );
114 
115  assert (uiBytes >= 4);
116 
117  return false;
118}
119
120/** \param  pcBitstream   bitstream class pointer
121 */
122Void TVideoIOBits::writeBits( TComBitstream* pcBitstream )
123{
124  UInt*  plBuff  = pcBitstream->getStartStream();
125  UInt   uiBytes = pcBitstream->getNumberOfWrittenBits() >> 3;
126 
127  // write 32-bit packet size
128  m_cHandle.write( reinterpret_cast<char*>(&uiBytes ), sizeof(UInt) );
129 
130  // write packet data
131  m_cHandle.write( reinterpret_cast<char*>(plBuff   ), uiBytes      );
132}
133
134////////////////////////////////////////////////////////////////////
135
136Void TVideoIOBitsStartCode::openBits( char* pchFile, Bool bWriteMode )
137{
138  if ( bWriteMode )
139  {
140    m_cHandle.open( pchFile, ios::binary | ios::out );
141   
142    if( m_cHandle.fail() )
143    {
144      printf("\nfailed to write Bitstream file\n");
145      exit(0);
146    }
147  }
148  else
149  {
150    m_cHandle.open( pchFile, ios::binary | ios::in );
151   
152    if( m_cHandle.fail() )
153    {
154      printf("\nfailed to read Bitstream file\n");
155      exit(0);
156    }
157  }
158 
159  return;
160}
161
162Void TVideoIOBitsStartCode::closeBits()
163{
164  m_cHandle.close();
165}
166
167/**
168 \param  rpcBitstream    bitstream class pointer
169 \retval                 true if EOF is reached
170 */
171Bool TVideoIOBitsStartCode::readBits( TComBitstream*& rpcBitstream )
172{
173  rpcBitstream->rewindStreamPacket();
174 
175  // check end-of-file
176  if ( m_cHandle.eof() ) return true;
177 
178  UInt uiPacketSize = 0;
179  if( 0 != xFindNextStartCode( uiPacketSize, reinterpret_cast<UChar*>(rpcBitstream->getBuffer()) ) )
180  {
181    return true;
182  }
183 
184  // initialize parsing process
185  rpcBitstream->initParsingConvertPayloadToRBSP( uiPacketSize );
186  return false;
187}
188
189int TVideoIOBitsStartCode::xFindNextStartCode(UInt& ruiPacketSize, UChar* pucBuffer)
190{
191  UInt uiDummy = 0;
192  m_cHandle.read( reinterpret_cast<char*>(&uiDummy), 3 );
193  if ( m_cHandle.eof() ) return -1;
194  assert( 0 == uiDummy );
195 
196  m_cHandle.read( reinterpret_cast<char*>(&uiDummy), 1 );
197  if ( m_cHandle.eof() ) return -1;
198  assert( 1 == uiDummy );
199 
200  Int iNextStartCodeBytes = 0;
201  Int iBytesRead = 0;
202  UInt uiZeros = 0;
203  while( true )
204  {
205    UChar ucByte = 0;
206    m_cHandle.read( reinterpret_cast<char*>(&ucByte), 1 );
207    if ( m_cHandle.eof() )
208    {
209      iNextStartCodeBytes = 0;
210      break;
211    }
212    pucBuffer[iBytesRead++] = ucByte;
213    if( 1 < ucByte )
214    {
215      uiZeros = 0;
216    }
217    else if( 0 == ucByte )
218    {
219      uiZeros++;
220    }
221    else if( uiZeros > 2)
222    {
223      iNextStartCodeBytes = 3 + 1;
224      break;
225    }
226    else
227    {
228      uiZeros = 0;
229    }
230  }
231 
232  ruiPacketSize = iBytesRead - iNextStartCodeBytes;
233 
234  m_cHandle.seekg( -iNextStartCodeBytes, ios::cur );
235  return 0;
236}
237
238/**
239 \param  pcBitstream   bitstream class pointer
240 */
241Void TVideoIOBitsStartCode::writeBits( TComBitstream* pcBitstream )
242{
243  UInt*  plBuff  = pcBitstream->getStartStream();
244  UInt   uiBytes = pcBitstream->getNumberOfWrittenBits() >> 3;
245  Char   ucZero = 0;
246  Char   ucOne = 1;
247 
248  // write 32-bit packet size
249  m_cHandle.write( &ucZero , sizeof(Char) );
250  m_cHandle.write( &ucZero , sizeof(Char) );
251  m_cHandle.write( &ucZero , sizeof(Char) );
252  m_cHandle.write( &ucOne  , sizeof(Char) );
253 
254  // write packet data
255  m_cHandle.write( reinterpret_cast<char*>(plBuff   ), uiBytes      );
256}
257
258
Note: See TracBrowser for help on using the repository browser.