Dirac - A Video Codec

Created by the British Broadcasting Corporation.


byteio.h

Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002 *
00003 * $Id: byteio.h,v 1.7 2008/01/22 07:38:37 asuraparaju Exp $ $Name: Dirac_0_9_0 $
00004 *
00005 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00006 *
00007 * The contents of this file are subject to the Mozilla Public License
00008 * Version 1.1 (the "License"); you may not use this file except in compliance
00009 * with the License. You may obtain a copy of the License at
00010 * http://www.mozilla.org/MPL/
00011 *
00012 * Software distributed under the License is distributed on an "AS IS" basis,
00013 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
00014 * the specific language governing rights and limitations under the License.
00015 *
00016 * The Original Code is BBC Research and Development code.
00017 *
00018 * The Initial Developer of the Original Code is the British Broadcasting
00019 * Corporation.
00020 * Portions created by the Initial Developer are Copyright (C) 2004.
00021 * All Rights Reserved.
00022 *
00023 * Contributor(s): Andrew Kennedy (Original Author),
00024 *                 Anuradha Suraparaju
00025 *
00026 * Alternatively, the contents of this file may be used under the terms of
00027 * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser
00028 * Public License Version 2.1 (the "LGPL"), in which case the provisions of
00029 * the GPL or the LGPL are applicable instead of those above. If you wish to
00030 * allow use of your version of this file only under the terms of the either
00031 * the GPL or LGPL and not to allow others to use your version of this file
00032 * under the MPL, indicate your decision by deleting the provisions above
00033 * and replace them with the notice and other provisions required by the GPL
00034 * or LGPL. If you do not delete the provisions above, a recipient may use
00035 * your version of this file under the terms of any one of the MPL, the GPL
00036 * or the LGPL.
00037 * ***** END LICENSE BLOCK ***** */
00038 
00042 #ifndef byteio_h
00043 #define byteio_h
00044 
00045 // SYSTEM INCLUDES
00046 #include <iostream>             // IO classes
00047 #include <sstream>              // IO classes
00048 #include <iomanip>              // setw
00049 
00050 //LOCAL INCLUDEs
00051 #include <libdirac_byteio/dirac_byte_stats.h>   // stores stats
00052 
00053 namespace dirac
00054 {
00055     
00056     // BIT DEFS
00057     #define BIT_ZERO 0
00058     #define BIT_ONE 1
00059 
00060     // most significant bit in a character 
00061     #define MS_BIT                (1 << (CHAR_BIT - 1))
00062 
00063     /* array index for character containing bit */
00064     //#define BIT_IN_CHAR(bit)      (1 << (CHAR_BIT-1-bit))
00065     #define BIT_IN_CHAR(bit)      (1 << bit)
00066  
00067 
00071    class ByteIO
00072    {
00073    public:
00074        
00079        ByteIO(bool new_stream=true);
00080 
00085        ByteIO(const ByteIO& stream_data);
00086 
00090        virtual ~ByteIO();
00091 
00096        virtual void CollateByteStats(DiracByteStats& dirac_byte_stats) 
00097        { dirac_byte_stats.Clear(); }
00098 
00102         virtual const std::string GetBytes();
00103 
00107         int GetReadBytePosition() const { return mp_stream->tellg();};
00108 
00109 
00113         virtual int GetSize() const;
00114 
00119         void SetByteParams(const ByteIO& byte_io);
00120 
00124         void ByteAlignOutput();
00125 
00130         //void OutputVarLengthUint(const unsigned int& value);
00131         void WriteUint(const unsigned int& value);
00132 
00136          void SetBitsLeft(int left_bits) { m_bits_left = left_bits; }
00137 
00141          int BitsLeft(void) { return m_bits_left; }
00142 
00143     protected:
00144 
00145         inline bool CanRead() const { return(!mp_stream->eof()); }
00146 
00147         inline bool GetBit(unsigned char& c, int pos) const { return (c & BIT_IN_CHAR(pos)); }
00148 
00149         inline void SetBit(unsigned char& c, int pos) const { c |= BIT_IN_CHAR(pos); }
00150 
00151         inline void SetBits(unsigned char& c, unsigned char bits) const { c |= bits; }
00152 
00156         void ByteAlignInput();
00157 
00158 
00162         bool ReadBool();
00163 
00167         bool ReadBoolB();
00168 
00172         int ReadBit();
00173 
00177         int ReadBitB();
00178 
00184         unsigned int ReadNBits(int count);
00185 
00191         void InputBytes(char* data, int count)
00192         {
00193             //int j=mp_stream->tellg();
00194             mp_stream->read(data, count);
00195 
00196             //int h=mp_stream->tellg();
00197         }
00198 
00202         void FlushInputB();
00203 
00208         //int InputVarLengthInt();
00209         int ReadSint();
00210 
00215         int ReadSintB();
00216 
00221         //unsigned int InputVarLengthUint();
00222         unsigned int ReadUint();
00223 
00228         //unsigned int InputVarLengthUint();
00229         unsigned int ReadUintB();
00230 
00236         //inline unsigned int InputFixedLengthUint(const int byte_size) { 
00237         inline unsigned int ReadUintLit(const int byte_size) { 
00238            unsigned int val=0; 
00239            for(int i=0; i < byte_size; ++i)
00240            {
00241                val <<= 8;
00242                val += (unsigned char)mp_stream->get();
00243            }
00244            m_num_bytes+=byte_size;
00245            return val;
00246         } 
00247 
00251         inline unsigned char InputUnByte() {m_num_bytes++ ; return mp_stream->get(); }
00252 
00256         inline std::string InputUnString(const int count) 
00257         {
00258             std::string str;
00259             for(int index=0; index < count; ++index)
00260                 str.push_back(InputUnByte());
00261             return str;
00262         }
00263 
00268         void WriteBit(const bool& bit);
00269 
00275         int WriteNBits(unsigned int val);
00276 
00282         void WriteNBits(unsigned int val, int count);
00283 
00284 
00285 
00289         void OutputBytes(const std::string& bytes) {
00290            int cur_pos = mp_stream->tellg();
00291           mp_stream->str(mp_stream->str()+bytes);
00292            m_num_bytes+=bytes.size();
00293         //   *mp_stream << bytes;
00294            mp_stream->seekg(std::max(cur_pos,0), std::ios_base::beg);
00295         }
00296 
00300         inline void OutputCurrentByte()
00301         {
00302             if (m_current_pos)
00303             {
00304                 *mp_stream << (m_current_byte);
00305                 ++m_num_bytes;
00306                 m_current_pos = 0;
00307                 m_current_byte = 0;
00308             }
00309         };
00310 
00315        //void OutputVarLengthInt(const int val);
00316        void WriteSint(const int val);
00317 
00323        //inline void OutputFixedLengthUint(const unsigned int& value, const int& length)
00324        inline void WriteUintLit(const unsigned int& value, const int& length)
00325        {
00326            for(int i=length-1; i >=0 ; --i)
00327            {
00328               unsigned char cp = (value>>(i*8))&0xff; 
00329                *mp_stream << cp;
00330            }
00331            m_num_bytes+=length;
00332        }
00333        
00338        void RemoveRedundantBytes(const int count);
00339 
00340        inline void SeekGet(const int offset, std::ios_base::seekdir dir) 
00341        {
00342            mp_stream->seekg(offset, dir);
00343        }
00344 
00348        std::stringstream*    mp_stream;
00349 
00350 
00351    private:
00352        
00356        friend class ArithCodecBase;
00357 
00361        friend class BandVLC;
00362 
00366        unsigned char m_current_byte;
00367             
00371        int m_current_pos;
00372 
00376        int m_num_bytes;
00377        
00381        bool m_new_stream;
00382         
00386        int m_bits_left;
00387    protected:
00388 
00389         
00390    };
00391 
00392 
00393 
00394 } // namespace dirac
00395 
00396 #endif

© 2004 British Broadcasting Corporation. Dirac code licensed under the Mozilla Public License (MPL) Version 1.1.
HTML documentation generated by Dimitri van Heesch's excellent Doxygen tool.