//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / pf
//
//File: bithandler.h
//
//Version: $Revision: 1.3 $
//
//Date: $Date: 1998/10/19 08:46:02 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications and Multimedia
//
//Author:
//      Timo Pärnänen
//
//Description:
//      Provide functions to use in encoding and decoding functions.
//      Interface has functions to set/get bits to/from frame and
//      it provide also some method to handle more complex mechanism
//      like extension mechanism.
//
//    Class for simplifying coding functions. The class have methods
//    to get/put bits from/to frame. The amount of bits need not to be
//    byte multiples. There is also methods for extension mechanism and
//    some other special methods for coding functions to use.
//
//Copyright:
//      Copyright 1999 Helsinki University of Technology
//      ALL RIGHTS RESERVED BETWEEN JANUARY 1996 AND JUNE 1999.
//
//Licence:
//
//
//History: 
//
//

#ifndef __PF_BITHANDLER_H__
#define __PF_BITHANDLER_H__

#include "pf/types.h"
#include "pf/frame.h"

class pfBitHandler
{
    public:
        virtual ~pfBitHandler(void);
        pfBitHandler(void);
        
        void start(pfFrame &frame_);
        pfFrame getFrame(void);
        pfLong frameLength(void);
        
        pfByte read(int offSet_);
        
        pfLong getBits(int amount_);
        void putBits(int amount_, pfLong field_);

        void skip(int amount_);
        void putSpare(int amount_);

        void getExtension(void);
        void putExtension(pfLong value_);
        pfLong getBitsInExtension(int amount_);
        void skipInExtension(int amount_);
        bool isInExtension(void);
        void endExtension(void);
        void getEvenOdd(void);
        void putEvenOdd(int value_);

        bool isValid(pfLong value_, pfLong invalid_);
        void setDefault(pfLong &value_, pfLong invalid_, pfLong default_);
        
        // bcd_table will be added

    protected:
        pfFrame _frame;

    private:
        void putBitsToByte(int amount_, pfLong field_);
        void getBitsFromByte(int amount_, pfLong &field_);

        static const int MAXBIT = 8; // The highest coding position
                                     // and length of byte
        
        pfByte _currentByte;
        int _encPosition;
        int _decPosition;
        pfLong _extBit;
        pfLong _currentExt;
        pfLong _evenOdd;
};

#endif // __PF_BITHANDLER_H__

