# 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.11 2009/01/21 05:18:09 asuraparaju Exp $ $Name: Dirac_1_0_2 $
    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 #include <climits>              // CHAR_BIT
    00050
    00051 //LOCAL INCLUDEs
    00052 #include <libdirac_byteio/dirac_byte_stats.h>   // stores stats
    00053
    00054 namespace dirac
    00055 {
    00056
    00057     // BIT DEFS
    00058     #define BIT_ZERO 0
    00059     #define BIT_ONE 1
    00060
    00061     // most significant bit in a character
    00062     #define MS_BIT                (1 << (CHAR_BIT - 1))
    00063
    00064     /* array index for character containing bit */
    00065     //#define BIT_IN_CHAR(bit)      (1 << (CHAR_BIT-1-bit))
    00066     #define BIT_IN_CHAR(bit)      (1 << bit)
    00067
    00068
    00072    class ByteIO
    00073    {
    00074    public:
    00075
    00080        ByteIO(bool new_stream=true);
    00081
    00086        ByteIO(const ByteIO& stream_data);
    00087
    00091        virtual ~ByteIO();
    00092
    00097        virtual void CollateByteStats(DiracByteStats& dirac_byte_stats)
    00098        { dirac_byte_stats.Clear(); }
    00099
    00103         virtual const std::string GetBytes();
    00104
    00108         int GetReadBytePosition() const { return mp_stream->tellg();};
    00109
    00110
    00114         virtual int GetSize() const;
    00115
    00120         void SetByteParams(const ByteIO& byte_io);
    00121
    00125         void ByteAlignOutput();
    00126
    00131         //void OutputVarLengthUint(const unsigned int& value);
    00132         void WriteUint(unsigned int value);
    00133
    00137          void SetBitsLeft(int left_bits) { m_bits_left = left_bits; }
    00138
    00142          int BitsLeft(void) { return m_bits_left; }
    00143
    00144     protected:
    00145
    00146         inline bool CanRead() const { return(!mp_stream->eof()); }
    00147
    00148         inline bool GetBit(unsigned char& c, int pos) const { return (c & BIT_IN_CHAR(pos)); }
    00149
    00150         inline void SetBit(unsigned char& c, int pos) const { c |= BIT_IN_CHAR(pos); }
    00151
    00152         inline void SetBits(unsigned char& c, unsigned char bits) const { c |= bits; }
    00153
    00157         void ByteAlignInput();
    00158
    00159
    00163         bool ReadBool();
    00164
    00168         bool ReadBoolB();
    00169
    00173         int ReadBit();
    00174
    00178         int ReadBitB();
    00179
    00185         unsigned int ReadNBits(int count);
    00186
    00192         void InputBytes(char* data, int count)
    00193         {
    00194             //int j=mp_stream->tellg();
    00195             mp_stream->read(data, count);
    00196
    00197             //int h=mp_stream->tellg();
    00198         }
    00199
    00203         void FlushInputB();
    00204
    00209         //int InputVarLengthInt();
    00210         int ReadSint();
    00211
    00216         int ReadSintB();
    00217
    00222         //unsigned int InputVarLengthUint();
    00223         unsigned int ReadUint();
    00224
    00229         //unsigned int InputVarLengthUint();
    00230         unsigned int ReadUintB();
    00231
    00237         //inline unsigned int InputFixedLengthUint(const int byte_size) {
    00238         inline unsigned int ReadUintLit(const int byte_size) {
    00239            unsigned int val=0;
    00240            for(int i=0; i < byte_size; ++i)
    00241            {
    00242                val <<= 8;
    00243                val += (unsigned char)mp_stream->get();
    00244            }
    00245            m_num_bytes+=byte_size;
    00246            return val;
    00247         }
    00248
    00252         inline unsigned char InputUnByte() {m_num_bytes++ ; return mp_stream->get(); }
    00253
    00257         inline std::string InputUnString(const int count)
    00258         {
    00259             std::string str;
    00260             for(int index=0; index < count; ++index)
    00261                 str.push_back(InputUnByte());
    00262             return str;
    00263         }
    00264
    00269         void WriteBit(const bool& bit);
    00270
    00276         int WriteNBits(unsigned int val);
    00277
    00283         void WriteNBits(unsigned int val, int count);
    00284
    00285
    00286
    00290         void OutputBytes(const std::string& bytes) {
    00291            int cur_pos = mp_stream->tellg();
    00292           mp_stream->str(mp_stream->str()+bytes);
    00293            m_num_bytes+=bytes.size();
    00294         //   *mp_stream << bytes;
    00295            mp_stream->seekg(std::max(cur_pos,0), std::ios_base::beg);
    00296         }
    00297
    00301         inline void OutputCurrentByte()
    00302         {
    00303             if (m_current_pos)
    00304             {
    00305                 *mp_stream << (m_current_byte);
    00306                 ++m_num_bytes;
    00307                 m_current_pos = 0;
    00308                 m_current_byte = 0;
    00309             }
    00310         };
    00311
    00316        //void OutputVarLengthInt(const int val);
    00317        void WriteSint(int val);
    00318
    00324        //inline void OutputFixedLengthUint(const unsigned int& value, const int& length)
    00325        inline void WriteUintLit(const unsigned int& value, const int& length)
    00326        {
    00327            for(int i=length-1; i >=0 ; --i)
    00328            {
    00329               unsigned char cp = (value>>(i*8))&0xff;
    00330                *mp_stream << cp;
    00331            }
    00332            m_num_bytes+=length;
    00333        }
    00334
    00339        void RemoveRedundantBytes(const int count);
    00340
    00341        inline void SeekGet(const int offset, std::ios_base::seekdir dir)
    00342        {
    00343            mp_stream->seekg(offset, dir);
    00344        }
    00345
    00349        std::stringstream*    mp_stream;
    00350
    00351
    00352    private:
    00353
    00357        friend class ArithCodecBase;
    00358
    00362        friend class ArithCodecToVLCAdapter;
    00363
    00367        unsigned char m_current_byte;
    00368
    00372        int m_current_pos;
    00373
    00377        int m_num_bytes;
    00378
    00382        bool m_new_stream;
    00383
    00387        int m_bits_left;
    00388    protected:
    00389
    00390
    00391    };
    00392
    00393
    00394
    00395 } // namespace dirac
    00396
    00397 #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.