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.5 2007/03/19 16:18:59 asuraparaju Exp $ $Name: Dirac_0_7_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 
00132     protected:
00133 
00134         inline bool CanRead() const { return(!mp_stream->eof()); }
00135 
00136         inline bool GetBit(unsigned char& c, int pos) const { return (c & BIT_IN_CHAR(pos)); }
00137 
00138         inline void SetBit(unsigned char& c, int pos) const { c |= BIT_IN_CHAR(pos); }
00139 
00140         inline void SetBits(unsigned char& c, unsigned char bits) const { c |= bits; }
00141 
00145         void ByteAlignInput();
00146 
00147 
00151         bool InputBit();
00152 
00158         void InputBytes(char* data, int count)
00159         {
00160             //int j=mp_stream->tellg();
00161             mp_stream->read(data, count);
00162 
00163             //int h=mp_stream->tellg();
00164         }
00165 
00170         int InputVarLengthInt();
00171 
00176         unsigned int InputVarLengthUint();
00177 
00183         inline unsigned int InputFixedLengthUint(const int byte_size) { 
00184            unsigned int val=0; 
00185            for(int i=0; i < byte_size; ++i)
00186            {
00187                val <<= 8;
00188                val += (unsigned char)mp_stream->get();
00189            }
00190            m_num_bytes+=byte_size;
00191            return val;
00192         } 
00193 
00197         inline unsigned char InputUnByte() {m_num_bytes++ ; return mp_stream->get(); }
00198 
00202         inline std::string InputUnString(const int count) 
00203         {
00204             std::string str;
00205             for(int index=0; index < count; ++index)
00206                 str.push_back(InputUnByte());
00207             return str;
00208         }
00209 
00214         void OutputBit(const bool& bit);
00215 
00216 
00220         void OutputBytes(const std::string& bytes) {
00221            int cur_pos = mp_stream->tellg();
00222           mp_stream->str(mp_stream->str()+bytes);
00223            m_num_bytes+=bytes.size();
00224         //   *mp_stream << bytes;
00225            mp_stream->seekg(std::max(cur_pos,0), std::ios_base::beg);
00226         }
00227 
00231         inline void OutputCurrentByte()
00232         {
00233             if (m_current_pos)
00234             {
00235                 *mp_stream << (m_current_byte);
00236                 ++m_num_bytes;
00237                 m_current_pos = 0;
00238                 m_current_byte = 0;
00239             }
00240         };
00241 
00246        void OutputVarLengthInt(const int val);
00247 
00253        inline void OutputFixedLengthUint(const unsigned int& value, const int& length)
00254        {
00255            for(int i=length-1; i >=0 ; --i)
00256            {
00257               unsigned char cp = (value>>(i*8))&0xff; 
00258                *mp_stream << cp;
00259            }
00260            m_num_bytes+=length;
00261        }
00262        
00267        void RemoveRedundantBytes(const int count);
00268 
00269        inline void SeekGet(const int offset, std::ios_base::seekdir dir) 
00270        {
00271            mp_stream->seekg(offset, dir);
00272        }
00273 
00277        std::stringstream*    mp_stream;
00278 
00279 
00280    private:
00281        
00285        friend class ArithCodecBase;
00286 
00290        unsigned char m_current_byte;
00291             
00295        int m_current_pos;
00296 
00300        int m_num_bytes;
00301        
00305        bool m_new_stream;
00306         
00307         
00308    protected:
00309 
00310         
00311    };
00312 
00313 
00314 
00315 } // namespace dirac
00316 
00317 #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.