Horus Doc || C++ Reference || Class Overview   Pixels   Images   Detector   Geometry   Registry || Doxygen's quick Index  

cobitbuf.h

00001 // cobitbuf.h : interface of bidirectional bit buffer management
00002 //
00003 // This class provides a set of bit-based and byte-based functions
00004 // to read information from buffer in two direction and manage buffer.
00005 //
00006 //*****************************************************************************
00007 // 
00008 //                  MPEG Developing Classes
00009 //
00010 // Copyright (C) 1998, Vision and Neural Networks Laboratory, Computer Science 
00011 // Department, Wayne State University, All Rights Reserved; 
00012 //
00013 //                  Disclaimer of Warranty
00014 //
00015 // MPEG Developing Classes, both binary and source (hereafter, Software)
00016 // is copyrighted by Vision and Neural Networks Laboratory, Computer Science 
00017 // Department, Wayne State University (WSU), and ownership remains with WSU.
00018 //
00019 // Permission is hereby granted, free of charge, to any person obtaining
00020 // a copy of this software and associated documentation files to use, copy,
00021 // and distribute the software, provided that no charge is associated with 
00022 // such copies and that the copyright notice and this statement appears on 
00023 // all copies.
00024 //
00025 // THE SOFTWARE IS AVAILABLE TO THE USER ON AN "AS IS" BASIS, WITHOUT WARRANTY
00026 // OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, THE 
00027 // IMPLIED WARRANTIES OR MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
00028 // IN NO EVENT SHALL THE COPYRIGHT-HOLDER BE LIABLE FOR ANY CLAIMS, DAMAGES, OR
00029 // OTHER LIABILITIES OF ANY KIND WHATSOEVER ARISING FROM, OUT OF OR IN CONNECTION
00030 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. THIS 
00031 // DISCLAIMAER OF WARRANTY EXTENDS TO THE USER OF THIS SOFTWARE. ALSO THE WSU 
00032 // DOES NOT REPRESENT OR WARRANT THAT THE SOFTWARE FURNISHED HEREUNDER ARE FREE OF
00033 // INFRINGMENT OF ANY THIRD-PARTY PATENTS.
00034 //
00035 // Many patents of MPEG-1 and MPEG-2 are general enough such that commercial
00036 // implementations, including shareware, are unavoidable subject to royalty fees to
00037 // patent holders, regardless of implementation design.
00038 //
00039 //
00040 // Vision and Neural Networks Laboratory,
00041 // Computer Science Department,
00042 // Wayne State University,
00043 // Detroit, MI 48202.
00044 // Dongge Li(dil@cs.wayne.edu) and Ishwar K. Sethi(sethi@cs.wayne.edu).
00045 //
00047 
00048 #if !defined COBITBUF_H
00049 #define COBITBUF_H
00050 
00051 #include <stdio.h>
00052 #include "datatype.h"
00053 
00054 const long MIN_SIZE_LIMIT=sizeof(DWORD)+1; // minimum number of data remained in the buffer, must larger than 5(DWORD+BitOffset);
00055 
00056 
00057 enum DIRECTION {MDC_FORWARD, MDC_BACKWARD};
00058 typedef struct bufpos_tag
00059 {
00060     long lCurBytePos;
00061     BYTE nCurBitOffset;
00062     long lStartPos; // data start position in the buffer;
00063     long lEndPos; // data end position in the buffer;
00064     long lTotalItemNum;
00065 } BUFPOS;
00066 
00067 class COBitBuf
00068 {
00069 // constructor and destructor
00070 public:
00071     COBitBuf();
00072     ~COBitBuf();
00073 
00074 // attributes
00075 protected:
00076     BYTE *pnBuf;
00077     DWORD dwBufSize;
00078     long lActualBufSize;  // just for speedup, it equals to dwBufSize-1;
00079     BUFPOS BufPos;
00080     long lFwdLoadBlkSize, lBackLoadBlkSize;
00081     int iStatus;
00082 
00083 // operation
00084 public:
00085     int InitBuffer(DWORD dwBufferSize);
00086     void ResetBuffer(void);
00087     DWORD GetBufferSize(void) { return dwBufSize; };
00088     BYTE GetCurBitOffset() { return BufPos.nCurBitOffset; };
00089     bool ByteAligned() { return (BufPos.nCurBitOffset==0)? true : false; };
00090     int SetLoadSize(long lNewLoadSize, DIRECTION eDirection);
00091     int SetLoadSize(long lFwdLoadSize, long lBackLoadSize);
00092     long GetLoadSize(DIRECTION eDirection);
00093     int FwdJumpBytes(DWORD dwOffset);
00094     int BackJumpBytes(DWORD dwOffset);
00095     BYTE ShowByte(int *piException);
00096     BYTE ShowByte(void);
00097     BYTE GetByte(int *piException);
00098     BYTE GetByte(void);
00099     WORD ShowWord(int *piException);
00100     WORD ShowWord(void);
00101     WORD GetWord(int *piException);
00102     WORD GetWord(void);
00103     DWORD ShowDWord(int *piException);
00104     DWORD ShowDWord(void);
00105     DWORD GetDWord(int *piException);
00106     DWORD GetDWord(void);
00107     inline int FwdJumpBits(DWORD dwOffset);
00108     inline int BackJumpBits(DWORD dwOffset);
00109     DWORD ShowBits(BYTE nBitNum, int *piException);
00110     DWORD ShowBits(BYTE nBitNum);
00111     inline DWORD GetBits(BYTE nBitNum, int *piException);
00112     inline DWORD GetBits(BYTE nBitNum);
00113     int GetStatus(void) {return iStatus;};
00114     void ClearStatus(void) {iStatus=MDC_SUCCESS;};
00115     //just for advance user to slightly improve performance;
00116     inline BYTE ShowByteBits(BYTE nBitNum, int *piException);
00117     inline BYTE ShowByteBits(BYTE nBitNum);
00118     inline BYTE GetByteBits(BYTE nBitNum);
00119 
00120 
00121 protected:
00122     void SetBufPos(const BUFPOS &dNewBufPos) {BufPos=dNewBufPos;};
00123 
00124 // virtual methods;
00125 protected: 
00126     //Unsuccess represents by 0; and fatal errors <0;
00127     virtual long FwdLoadData(long lLoadStartPos, DWORD dwItemNum)=0;// used by other functions
00128     //Unsuccess represents by 0; and fatal errors <0;
00129     virtual long BackLoadData(long lLoadEndPos, DWORD dwItemNum)=0; 
00130     //Unsuccess represents by values smaller than 0; No fatal error;
00131     virtual long FlushBuf(long lOffset, BUFPOS *pBufPos)=0;
00132 };
00133 
00134 int COBitBuf::FwdJumpBits(DWORD dwOffset)
00135 {
00136     DWORD dwBitOffset;
00137     int iStatus;
00138 
00139     dwBitOffset=dwOffset+BufPos.nCurBitOffset;
00140     if (dwBitOffset>=8)
00141     {
00142         iStatus=FwdJumpBytes(dwBitOffset>>3);
00143         if (iStatus!=MDC_SUCCESS)
00144             return iStatus;
00145         BufPos.nCurBitOffset=(BYTE)dwBitOffset&7;  // module 8
00146     }
00147     else
00148         BufPos.nCurBitOffset=(BYTE)dwBitOffset;
00149     return MDC_SUCCESS;
00150 }
00151 
00152 int COBitBuf::BackJumpBits(DWORD dwOffset)
00153 {
00154     long lBitOffset;
00155     int iStatus;
00156 
00157     lBitOffset=BufPos.nCurBitOffset-dwOffset;
00158     if (lBitOffset>=0)   // still the same byte;
00159     {
00160         BufPos.nCurBitOffset=(BYTE)lBitOffset;
00161     }
00162     else   // backward
00163     {
00164         iStatus=BackJumpBytes(-(lBitOffset+1)/8+1); // Bit -1 to -8 is Byte -1 position.
00165         if (iStatus!=MDC_SUCCESS)
00166             return iStatus;
00167         BufPos.nCurBitOffset=7+(lBitOffset+1)%8; 
00168     }
00169     return MDC_SUCCESS;
00170 }
00171 
00172 DWORD COBitBuf::GetBits(BYTE nBitNum, int *piException)
00173 {
00174     DWORD dwResult;
00175     BYTE nBitEnd;
00176 
00177     dwResult=ShowBits(nBitNum,piException);
00178     if (*piException!=MDC_SUCCESS)
00179         return 0;
00180     // the data has actually be loaded in ShowBits successfully
00181     nBitEnd=nBitNum+BufPos.nCurBitOffset;
00182     BufPos.lCurBytePos+=(nBitEnd>>3);  // divided by 8
00183     if (BufPos.lCurBytePos>=(long)dwBufSize)
00184         BufPos.lCurBytePos-=dwBufSize;
00185     BufPos.nCurBitOffset=nBitEnd&7;  // module 8
00186     return dwResult;
00187 }
00188 
00189 DWORD COBitBuf::GetBits(BYTE nBitNum)
00190 {
00191     DWORD dwResult;
00192     BYTE nBitEnd;
00193     int iException;
00194 
00195     dwResult=ShowBits(nBitNum,&iException);
00196     if (iException!=MDC_SUCCESS)
00197         return 0;
00198     // the data has actually be loaded in ShowBits successfully
00199     nBitEnd=nBitNum+BufPos.nCurBitOffset;
00200     BufPos.lCurBytePos+=(nBitEnd>>3);  // divided by 8
00201     if (BufPos.lCurBytePos>=(long)dwBufSize)
00202         BufPos.lCurBytePos-=dwBufSize;
00203     BufPos.nCurBitOffset=nBitEnd&7;  // module 8
00204     return dwResult;
00205 }
00206 
00207 BYTE COBitBuf::ShowByteBits(BYTE nBitNum, int *piException)
00208 {
00209 #ifdef DEBUG
00210     if (nBitNum>8)
00211     {
00212         if (iStatus>0)
00213             iStatus=UNSUCCESS;
00214         *piException=UNSUCCESS;
00215         return 0;
00216     }
00217 #endif
00218     if (nBitNum+BufPos.nCurBitOffset<=8)
00219         return ( (BYTE)(ShowByte(piException)<<BufPos.nCurBitOffset) )>>( 8-nBitNum );
00220     else
00221         return (BYTE)(( (WORD)(ShowWord(piException)<<BufPos.nCurBitOffset) )>>( 16-nBitNum ));
00222 }
00223 
00224 BYTE COBitBuf::ShowByteBits(BYTE nBitNum)
00225 {
00226 #ifdef DEBUG
00227     if (nBitNum>8)
00228     {
00229         if (iStatus>0)
00230             iStatus=UNSUCCESS;
00231         return 0;
00232     }
00233 #endif
00234     if (nBitNum+BufPos.nCurBitOffset<=8)
00235         return ( (BYTE)(ShowByte()<<BufPos.nCurBitOffset) )>>( 8-nBitNum );
00236     else
00237         return (BYTE)(( (WORD)(ShowWord()<<BufPos.nCurBitOffset) )>>( 16-nBitNum ));
00238 }
00239 
00240 BYTE COBitBuf::GetByteBits(BYTE nBitNum)
00241 {
00242     BYTE nResult;
00243     BYTE nBitEnd;
00244     int iException;
00245 
00246     nResult=ShowByteBits(nBitNum,&iException);
00247     if (iException!=MDC_SUCCESS)
00248         return 0;
00249     // the data has actually be loaded in ShowBits successfully
00250     nBitEnd=nBitNum+BufPos.nCurBitOffset;
00251     BufPos.lCurBytePos+=(nBitEnd>>3);  // divided by 8
00252     if (BufPos.lCurBytePos>=(long)dwBufSize)
00253         BufPos.lCurBytePos-=dwBufSize;
00254     BufPos.nCurBitOffset=nBitEnd&7;  // module 8
00255     return nResult;
00256 }
00257 
00258 #endif // COBITBUF_H

Generated on Mon Jan 27 15:48:40 2003 for C++Reference by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001