1e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard/*
2e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** Copyright 2003-2010, VisualOn, Inc.
3e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard **
4e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** Licensed under the Apache License, Version 2.0 (the "License");
5e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** you may not use this file except in compliance with the License.
6e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** You may obtain a copy of the License at
7e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard **
8e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard **     http://www.apache.org/licenses/LICENSE-2.0
9e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard **
10e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** Unless required by applicable law or agreed to in writing, software
11e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** distributed under the License is distributed on an "AS IS" BASIS,
12e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** See the License for the specific language governing permissions and
14e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** limitations under the License.
15e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard */
16e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard/*******************************************************************************
17e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard	File:		bitbuffer.h
18e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
19e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard	Content:	Bit Buffer Management structure and functions
20e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
21956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong*******************************************************************************/
22956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
23956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong#ifndef BITBUFFER_H
24956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong#define BITBUFFER_H
25956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
26956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong#include "typedef.h"
27956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
28956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
29956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dongenum direction
30956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong{
31956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  forwardDirection,
32956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  backwardDirection
33956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong};
34956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
35956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
36956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong/*!
37956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong   The pointer 'pReadNext' points to the next available word, where bits can be read from. The pointer
38956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong   'pWriteNext' points to the next available word, where bits can be written to. The pointer pBitBufBase
39956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong   points to the start of the bitstream buffer and the pointer pBitBufEnd points to the end of the bitstream
40956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong   buffer. The two pointers are used as lower-bound respectively upper-bound address for the modulo addressing
41956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong   mode.
42956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
43956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong   The element cntBits contains the currently available bits in the bit buffer. It will be incremented when
44956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong   bits are written to the bitstream buffer and decremented when bits are read from the bitstream buffer.
45956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong*/
46956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dongstruct BIT_BUF
47956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong{
48956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  UWord8 *pBitBufBase;          /*!< pointer points to first position in bitstream buffer */
49956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  UWord8 *pBitBufEnd;           /*!< pointer points to last position in bitstream buffer */
50956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
51956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  UWord8 *pWriteNext;           /*!< pointer points to next available word in bitstream buffer to write */
52956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
53956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  UWord32 cache;
54956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
55956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  Word16  wBitPos;              /*!< 31<=wBitPos<=0*/
56956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  Word16  cntBits;              /*!< number of available bits in the bitstream buffer
57956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong                                     write bits to bitstream buffer  => increment cntBits
58956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong                                     read bits from bitstream buffer => decrement cntBits */
59956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  Word16  size;                 /*!< size of bitbuffer in bits */
60956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  Word16  isValid;              /*!< indicates whether the instance has been initialized */
61956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong}; /* size Word16: 8 */
62956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
63956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong/*! Define pointer to bit buffer structure */
64956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dongtypedef struct BIT_BUF *HANDLE_BIT_BUF;
65956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
66956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
67956c553ab0ce72f8074ad0fda2ffd66a0305700cJames DongHANDLE_BIT_BUF CreateBitBuffer(HANDLE_BIT_BUF hBitBuf,
68956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong                               UWord8 *pBitBufBase,
69956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong                               Word16  bitBufSize);
70956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
71956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
72956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dongvoid DeleteBitBuffer(HANDLE_BIT_BUF *hBitBuf);
73956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
74956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
75956c553ab0ce72f8074ad0fda2ffd66a0305700cJames DongWord16 GetBitsAvail(HANDLE_BIT_BUF hBitBuf);
76956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
77956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
78956c553ab0ce72f8074ad0fda2ffd66a0305700cJames DongWord16 WriteBits(HANDLE_BIT_BUF hBitBuf,
795e9afe434d8207fb0af6e191cca671ab74cfe878Martin Storsjo                 UWord32 writeValue,
80956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong                 Word16 noBitsToWrite);
81956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
82956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dongvoid ResetBitBuf(HANDLE_BIT_BUF hBitBuf,
83956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong                 UWord8 *pBitBufBase,
84956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong                 Word16  bitBufSize);
85956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
86956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong#define GetNrBitsAvailable(hBitBuf) ( (hBitBuf)->cntBits)
87956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong#define GetNrBitsRead(hBitBuf)       ((hBitBuf)->size-(hBitBuf)->cntBits)
88956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
89956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong#endif /* BITBUFFER_H */
90