aboutsummaryrefslogtreecommitdiff
path: root/Src/replicant/nsmp3dec/bitstream.h
blob: c72e43580dde029aca2222d3518a1e3a58f3c9bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*************************************************************************** *
*                    MPEG Layer3-Audio Decoder
*                  © 1997-2006 by Fraunhofer IIS
 *                        All Rights Reserved
 *
 *   filename: bitstream.h
 *   project : MPEG Decoder
 *   author  : Martin Sieler
 *   date    : 1997-12-05
 *   contents/description: generic bitbuffer - HEADER
 *
 *
\***************************************************************************/

/*
 * $Date: 2011/01/18 18:22:02 $
 * $Id: bitstream.h,v 1.4 2011/01/18 18:22:02 audiodsp Exp $
 */

#ifndef __BITSTREAM_H__
#define __BITSTREAM_H__

/* ------------------------ includes --------------------------------------*/

/*-------------------------- defines --------------------------------------*/

class CGioBase;

/*-------------------------------------------------------------------------*/

//
// Bitstream input class.
//
//    This class defines the interface that the mp3 decoder object will
//    read all of its bitstream input data from.
//

class CBitStream
{
public:

  CBitStream(int cbSize);
  CBitStream(unsigned char *pBuf, int cbSize, bool fDataValid = false);
  virtual ~CBitStream();

  virtual void   Reset();
	bool ByteAligned() const { return !(m_BitNdx & 7); }
  bool           ResetOccurred()           { return m_ResetOccurred;  }
  void           SetResetState(bool state) { m_ResetOccurred = state; }

  void           Connect(CGioBase *pGB);

  void           ResetBitCnt()     { m_BitCnt = 0;    }
  int            GetBitCnt() const { return m_BitCnt; }

  unsigned int   GetBits(unsigned int nBits); // gets 16 bits or less
	unsigned int   GetBits8(unsigned int nBits); // gets 8 bits or less
  unsigned int   Get1Bit();
  unsigned long  Get32Bits();

  bool           Ff(int nBits)     { return ( (nBits > 0) ? Seek(nBits)  : false); }
  bool           Rewind(int nBits) { return ( (nBits > 0) ? Seek(-nBits) : false); }
  bool           Seek(int nBits)
    {
    m_BitCnt    += nBits;
    m_ValidBits -= nBits;
    m_BitNdx     = (m_BitNdx+nBits) & m_bitMask;
    return true;
    }

  int            GetValidBits() const { return m_ValidBits; }
  int            GetFree()      const;

  void           SetEof();
  int            Fill(const unsigned char *pBuf, int cbSize);
  int            Fill(CBitStream &Bs, int cbSize);

protected:

  int            Refill();
  bool           IsEof()       const;
  bool           IsConnected() const;

private:

  CGioBase      *m_pGB;           // I/O object
  int            m_nBytes;        // size of buffer in bytes
	int						 m_mask;
  int            m_nBits;         // size of buffer in bits
	int					   m_bitMask;
  int            m_ValidBits;     // valid bits in buffer
  int            m_ReadOffset;    // where to write next
  int            m_BitCnt;        // bit counter
  int            m_BitNdx;        // position of next bit in byte
  bool           m_fEof;          // indication of input eof
  unsigned char *m_Buf;           // the buffer
  bool           m_fBufferIntern; // did we allocate the buffer ourselves
  bool           m_ResetOccurred; // reset just occurred, only for dynamic buffer used
};

/*-------------------------------------------------------------------------*/
#endif