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.4 2006/06/19 11:27:19 asuraparaju Exp $ $Name:  $
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 
00101         virtual const std::string GetBytes();
00102 
00106         int GetReadBytePosition() const { return mp_stream->tellg();};
00107 
00108 
00112         virtual int GetSize() const;
00113 
00118         void SetByteParams(const ByteIO& byte_io);
00119 
00123         void ByteAlignOutput();
00124 
00129         void OutputVarLengthUint(const unsigned int& value);
00130 
00131     protected:
00132 
00133         inline bool CanRead() const { return(!mp_stream->eof()); }
00134 
00135         inline bool GetBit(unsigned char& c, int pos) const { return (c & BIT_IN_CHAR(pos)); }
00136 
00137         inline void SetBit(unsigned char& c, int pos) const { c |= BIT_IN_CHAR(pos); }
00138 
00139         inline void SetBits(unsigned char& c, unsigned char bits) const { c |= bits; }
00140 
00144         void ByteAlignInput();
00145 
00146 
00150         bool InputBit();
00151 
00157         void InputBytes(char* data, int count)
00158         {
00159             //int j=mp_stream->tellg();
00160             mp_stream->read(data, count);
00161 
00162             //int h=mp_stream->tellg();
00163         }
00164 
00169         int InputVarLengthInt();
00170 
00175         unsigned int InputVarLengthUint();
00176 
00182         inline unsigned int InputFixedLengthUint(const int byte_size) { 
00183            unsigned int val=0; 
00184            for(int i=0; i < byte_size; ++i)
00185            {
00186                val <<= 8;
00187                val += (unsigned char)mp_stream->get();
00188            }
00189            m_num_bytes+=byte_size;
00190            return val;
00191         } 
00192 
00196         inline unsigned char InputUnByte() {m_num_bytes++ ; return mp_stream->get(); }
00197 
00201         inline std::string InputUnString(const int count) 
00202         {
00203             std::string str;
00204             for(int index=0; index < count; ++index)
00205                 str.push_back(InputUnByte());
00206             return str;
00207         }
00208 
00213         void OutputBit(const bool& bit);
00214 
00215 
00219         void OutputBytes(const std::string& bytes) {
00220            int cur_pos = mp_stream->tellg();
00221           mp_stream->str(mp_stream->str()+bytes);
00222            m_num_bytes+=bytes.size();
00223         //   *mp_stream << bytes;
00224            mp_stream->seekg(std::max(cur_pos,0), std::ios_base::beg);
00225         }
00226 
00230         inline void OutputCurrentByte()
00231         {
00232             if (m_current_pos)
00233             {
00234                 *mp_stream << (m_current_byte);
00235                 ++m_num_bytes;
00236                 m_current_pos = 0;
00237                 m_current_byte = 0;
00238             }
00239         };
00240 
00245        void OutputVarLengthInt(const int val);
00246 
00252        inline void OutputFixedLengthUint(const unsigned int& value, const int& length)
00253        {
00254            for(int i=length-1; i >=0 ; --i)
00255            {
00256               unsigned char cp = (value>>(i*8))&0xff; 
00257                *mp_stream << cp;
00258            }
00259            m_num_bytes+=length;
00260        }
00261        
00266        void RemoveRedundantBytes(const int count);
00267 
00268        inline void SeekGet(const int offset, std::ios_base::seekdir dir) 
00269        {
00270            mp_stream->seekg(offset, dir);
00271        }
00272 
00276        std::stringstream*    mp_stream;
00277 
00278 
00279    private:
00280        
00284        friend class ArithCodecBase;
00285 
00289        unsigned char m_current_byte;
00290             
00294        int m_current_pos;
00295 
00299        int m_num_bytes;
00300        
00304        bool m_new_stream;
00305         
00306         
00307    protected:
00308 
00309         
00310    };
00311 
00312 
00313 
00314 } // namespace dirac
00315 
00316 #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.