Changeset 56 in 3DVCSoftware for trunk/source/Lib/TLibCommon/TComBitStream.h


Ignore:
Timestamp:
11 May 2012, 21:20:17 (13 years ago)
Author:
hschwarz
Message:

updated trunk (move to HM6.1)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/source/Lib/TLibCommon/TComBitStream.h

    r42 r56  
    22 * License, included below. This software may be subject to other third party
    33 * and contributor rights, including patent rights, and no such rights are
    4  * granted under this license.
    5  *
    6  * Copyright (c) 2010-2011, ISO/IEC
     4 * granted under this license. 
     5 *
     6 * Copyright (c) 2010-2012, ITU/ISO/IEC
    77 * All rights reserved.
    88 *
     
    1515 *    this list of conditions and the following disclaimer in the documentation
    1616 *    and/or other materials provided with the distribution.
    17  *  * Neither the name of the ISO/IEC nor the names of its contributors may
     17 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
    1818 *    be used to endorse or promote products derived from this software without
    1919 *    specific prior written permission.
     
    3232 */
    3333
    34 
    35 
    3634/** \file     TComBitStream.h
    3735    \brief    class for handling bitstream (header)
     
    4543#endif // _MSC_VER > 1000
    4644
     45#include <stdint.h>
     46#include <vector>
    4747#include <stdio.h>
    4848#include <assert.h>
    4949#include "CommonDef.h"
     50
     51//! \ingroup TLibCommon
     52//! \{
    5053
    5154// ====================================================================================================================
     
    5861public:
    5962  virtual Void        writeAlignOne         () {};
     63  virtual Void        writeAlignZero        () {};
    6064  virtual Void        write                 ( UInt uiBits, UInt uiNumberOfBits )  = 0;
    6165  virtual Void        resetBits             ()                                    = 0;
     
    6468};
    6569
    66 /// class for handling bitstream
    67 class TComBitstream : public TComBitIf
     70/**
     71 * Model of a writable bitstream that accumulates bits to produce a
     72 * bytestream.
     73 */
     74class TComOutputBitstream : public TComBitIf
    6875{
     76  /**
     77   * FIFO for storage of bytes.  Use:
     78   *  - fifo.push_back(x) to append words
     79   *  - fifo.clear() to empty the FIFO
     80   *  - &fifo.front() to get a pointer to the data array.
     81   *    NB, this pointer is only valid until the next push_back()/clear()
     82   */
     83  std::vector<uint8_t> *m_fifo;
     84
     85  unsigned int m_num_held_bits; /// number of bits not flushed to bytestream.
     86  unsigned char m_held_bits; /// the bits held and not flushed to bytestream.
     87                             /// this value is always msb-aligned, bigendian.
     88  UInt m_uiTileMarkerLocationCount;
     89  UInt *m_puiTileMarkerLocation;
     90
     91public:
     92  // create / destroy
     93  TComOutputBitstream();
     94  ~TComOutputBitstream();
     95
     96  // interface for encoding
     97  /**
     98   * append uiNumberOfBits least significant bits of uiBits to
     99   * the current bitstream
     100   */
     101  Void        write           ( UInt uiBits, UInt uiNumberOfBits );
     102
     103  /** insert one bits until the bitstream is byte-aligned */
     104  Void        writeAlignOne   ();
     105
     106  /** insert zero bits until the bitstream is byte-aligned */
     107  Void        writeAlignZero  ();
     108
     109  /** this function should never be called */
     110  void resetBits() { assert(0); }
     111
     112  // utility functions
     113
     114  /**
     115   * Return a pointer to the start of the byte-stream buffer.
     116   * Pointer is valid until the next write/flush/reset call.
     117   * NB, data is arranged such that subsequent bytes in the
     118   * bytestream are stored in ascending addresses.
     119   */
     120  char* getByteStream() const;
     121
     122  /**
     123   * Return the number of valid bytes available from  getByteStream()
     124   */
     125  unsigned int getByteStreamLength();
     126
     127  /**
     128   * Reset all internal state.
     129   */
     130  Void clear();
     131
     132  /**
     133   * returns the number of bits that need to be written to
     134   * achieve byte alignment.
     135   */
     136  Int getNumBitsUntilByteAligned() { return (8 - m_num_held_bits) & 0x7; }
     137
     138  /**
     139   * Return the number of bits that have been written since the last clear()
     140   */
     141  unsigned getNumberOfWrittenBits() const { return unsigned(m_fifo->size()) * 8 + m_num_held_bits; }
     142
     143  void insertAt(const TComOutputBitstream& src, unsigned pos);
     144
     145  /**
     146   * Return a reference to the internal fifo
     147   */
     148  std::vector<uint8_t>& getFIFO() { return *m_fifo; }
     149
     150  UChar getHeldBits  ()          { return m_held_bits;          }
     151
     152  TComOutputBitstream& operator= (const TComOutputBitstream& src);
     153  UInt  getTileMarkerLocationCount   ( )                     { return m_uiTileMarkerLocationCount   ; }
     154  Void  setTileMarkerLocationCount   ( UInt i )              { m_uiTileMarkerLocationCount = i      ; } 
     155  UInt  getTileMarkerLocation        ( UInt i)               { return m_puiTileMarkerLocation[i]    ; }
     156  Void  setTileMarkerLocation        ( UInt i, UInt uiLOC )  { m_puiTileMarkerLocation[i] = uiLOC   ; }
     157  /** Return a reference to the internal fifo */
     158  std::vector<uint8_t>& getFIFO() const { return *m_fifo; }
     159
     160  Void          addSubstream    ( TComOutputBitstream* pcSubstream );
     161};
     162
     163/**
     164 * Model of an input bitstream that extracts bits from a predefined
     165 * bytestream.
     166 */
     167class TComInputBitstream
     168{
     169  std::vector<uint8_t> *m_fifo; /// FIFO for storage of complete bytes
     170
    69171protected:
    70   UInt*       m_apulStreamPacketBegin;
    71   UInt*       m_pulStreamPacket;
    72   UInt        m_uiBufSize;
    73  
    74   UInt        m_uiBitSize;
    75   Int         m_iValidBits;
    76  
    77   UInt        m_ulCurrentBits;
    78   UInt        m_uiBitsWritten;
    79  
    80   UInt        m_uiDWordsLeft;
    81   UInt        m_uiBitsLeft;
    82   UInt        m_uiNextBits;
    83  
    84   UInt        *m_auiSliceByteLocation, m_uiSliceCount;  // used to skip over slice start codes in initParsingConvertPayloadToRBSP()
    85   UInt        m_uiSliceProcessed;
    86 
    87 #if BITSTREAM_EXTRACTION
    88   UInt*       m_apulPacketPayloadBuffer;
    89   UInt        m_uiPacketPayloadSize;
    90 #endif 
    91 
    92   UInt xSwap ( UInt ui )
     172  unsigned int m_fifo_idx; /// Read index into m_fifo
     173
     174  unsigned int m_num_held_bits;
     175  unsigned char m_held_bits;
     176  UInt m_uiTileMarkerLocationCount;
     177  UInt *m_puiTileMarkerLocation;
     178#if TILES_WPP_ENTRY_POINT_SIGNALLING
     179  UInt  m_numBitsRead;
     180#endif
     181
     182public:
     183  /**
     184   * Create a new bitstream reader object that reads from #buf#.  Ownership
     185   * of #buf# remains with the callee, although the constructed object
     186   * will hold a reference to #buf#
     187   */
     188  TComInputBitstream(std::vector<uint8_t>* buf);
     189  ~TComInputBitstream();
     190
     191  // interface for decoding
     192  Void        pseudoRead      ( UInt uiNumberOfBits, UInt& ruiBits );
     193  Void        read            ( UInt uiNumberOfBits, UInt& ruiBits );
     194#if OL_FLUSH && !OL_FLUSH_ALIGN
     195  Void        readByte        ( UInt &ruiBits )
    93196  {
    94     // heiko.schwarz@hhi.fhg.de: support for BSD systems as proposed by Steffen Kamp [kamp@ient.rwth-aachen.de]
    95 #ifdef MSYS_BIG_ENDIAN
    96     return ui;
     197    // More expensive, but reads "bytes" that are not aligned.
     198    read(8, ruiBits);
     199  }
    97200#else
    98     UInt ul2;
    99    
    100     ul2  = ui>>24;
    101     ul2 |= (ui>>8) & 0x0000ff00;
    102     ul2 |= (ui<<8) & 0x00ff0000;
    103     ul2 |= ui<<24;
    104    
    105     return ul2;
    106 #endif
     201  Void        readByte        ( UInt &ruiBits )
     202  {
     203    assert(m_fifo_idx < m_fifo->size());
     204    ruiBits = (*m_fifo)[m_fifo_idx++];
    107205  }
    108  
    109   // read one word
    110   __inline Void xReadNextWord ();
    111  
    112 public:
    113   TComBitstream()             {}
    114   virtual ~TComBitstream()    {}
    115  
    116   // create / destroy
    117   Void        create          ( UInt uiSizeInBytes );
    118   Void        destroy         ();
    119  
    120   // interface for encoding
    121   Void        write           ( UInt uiBits, UInt uiNumberOfBits );
    122   Void        writeAlignOne   ();
    123   Void        writeAlignZero  ();
    124   Void        convertRBSPToPayload( UInt uiStartPos = 0);
    125   // interface for decoding
    126   Void        initParsingConvertPayloadToRBSP( const UInt uiBytesRead );
    127   Void        initParsing     ( UInt uiNumBytes );
    128 #if LCEC_INTRA_MODE || QC_LCEC_INTER_MODE
    129   Void        pseudoRead      ( UInt uiNumberOfBits, UInt& ruiBits );
    130 #endif
    131   Void        read            ( UInt uiNumberOfBits, UInt& ruiBits );
    132   Void        readAlignOne    ();
    133   UInt        getSliceProcessed                ()       { return m_uiSliceProcessed;                }
    134   Void        setSliceProcessed                (UInt u) { m_uiSliceProcessed                = u;    }
    135  
    136   // interface for slice start-code positioning at encoder
    137   UInt        getSliceCount                    ()                            { return m_uiSliceCount;                     }
    138   UInt        getSliceByteLocation             ( UInt uiIdx )                { return m_auiSliceByteLocation[ uiIdx ];    }
    139   Void        setSliceCount                    ( UInt uiCount )              { m_uiSliceCount = uiCount;                  }
    140   Void        setSliceByteLocation             ( UInt uiIdx, UInt uiCount )  { m_auiSliceByteLocation[ uiIdx ] = uiCount; }
    141 
    142   // memory allocation / deallocation interface for "slice location" bookkeeping
    143   Void        allocateMemoryForSliceLocations       ( UInt uiMaxNumOfSlices );
    144   Void        freeMemoryAllocatedForSliceLocations  ();
     206#endif // OL_FLUSH && !OL_FLUSH_ALIGN
     207
     208  Void        readOutTrailingBits ();
     209  UChar getHeldBits  ()          { return m_held_bits;          }
     210  TComOutputBitstream& operator= (const TComOutputBitstream& src);
     211  UInt  getTileMarkerLocationCount   ( )                     { return m_uiTileMarkerLocationCount   ; }
     212  Void  setTileMarkerLocationCount   ( UInt i )              { m_uiTileMarkerLocationCount = i      ; } 
     213  UInt  getTileMarkerLocation        ( UInt i)               { return m_puiTileMarkerLocation[i]    ; }
     214  Void  setTileMarkerLocation        ( UInt i, UInt uiLOC )  { m_puiTileMarkerLocation[i] = uiLOC   ; }
     215  UInt  getByteLocation              ( )                     { return m_fifo_idx                    ; }
    145216
    146217  // Peek at bits in word-storage. Used in determining if we have completed reading of current bitstream and therefore slice in LCEC.
    147   UInt        peekBits (UInt uiBits) { return( m_ulCurrentBits >> (32 - uiBits));  }
    148 
    149   // reset internal status
    150   Void        resetBits       ()
    151   {
    152     m_uiBitSize = 0;
    153     m_iValidBits = 32;
    154     m_ulCurrentBits = 0;
    155     m_uiBitsWritten = 0;
    156   }
    157  
     218  UInt        peekBits (UInt uiBits) { unsigned tmp; pseudoRead(uiBits, tmp); return tmp; }
     219
    158220  // utility functions
    159221  unsigned read(unsigned numberOfBits) { UInt tmp; read(numberOfBits, tmp); return tmp; }
    160   UInt* getStartStream() const { return m_apulStreamPacketBegin; }
    161   UInt*       getBuffer()               { return  m_pulStreamPacket;                    }
    162   Int         getBitsUntilByteAligned() { return m_iValidBits & (0x7);                  }
    163   Void        setModeSbac()             { m_uiBitsLeft = 8*((m_uiBitsLeft+7)/8);        } // stop bit + trailing stuffing bits
    164   Bool        isWordAligned()           { return  (0 == (m_iValidBits & (0x1f)));       }
    165   UInt getNumberOfWrittenBits() const { return  m_uiBitsWritten; }
    166   Void        flushBuffer();
    167   Void        rewindStreamPacket()      { m_pulStreamPacket = m_apulStreamPacketBegin;  }
    168   UInt        getBitsLeft()             { return  m_uiBitsLeft;                         }
    169 
    170   void insertAt(const TComBitstream& src, unsigned pos);
    171 
    172 #if BITSTREAM_EXTRACTION
    173   UInt        reinitParsing();
    174 #endif 
     222  UInt     readByte() { UInt tmp; readByte( tmp ); return tmp; }
     223  unsigned getNumBitsUntilByteAligned() { return m_num_held_bits & (0x7); }
     224  unsigned getNumBitsLeft() { return 8*((unsigned)m_fifo->size() - m_fifo_idx) + m_num_held_bits; }
     225  TComInputBitstream *extractSubstream( UInt uiNumBits ); // Read the nominated number of bits, and return as a bitstream.
     226  Void                deleteFifo(); // Delete internal fifo of bitstream.
     227#if !OL_FLUSH_ALIGN
     228  Void                backupByte() { m_fifo_idx--; }
     229#endif
     230#if TILES_WPP_ENTRY_POINT_SIGNALLING
     231  UInt  getNumBitsRead() { return m_numBitsRead; }
     232#endif
    175233};
    176234
    177 #endif
    178 
     235//! \}
     236
     237#endif
Note: See TracChangeset for help on using the changeset viewer.