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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
/*****************************************************************
|
| AP4 - Bitstream Utility
|
| Copyright 2002-2008 Axiomatic Systems, LLC
|
|
| This file is part of Bento4/AP4 (MP4 Atom Processing Library).
|
| Unless you have obtained Bento4 under a difference license,
| this version of Bento4 is Bento4|GPL.
| Bento4|GPL is free software; you can redistribute it and/or modify
| it under the terms of the GNU General Public License as published by
| the Free Software Foundation; either version 2, or (at your option)
| any later version.
|
| Bento4|GPL is distributed in the hope that it will be useful,
| but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
| GNU General Public License for more details.
|
| You should have received a copy of the GNU General Public License
| along with Bento4|GPL; see the file COPYING. If not, write to the
| Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
| 02111-1307, USA.
|
****************************************************************/
#ifndef _AP4_BIT_STREAM_H_
#define _AP4_BIT_STREAM_H_
/*----------------------------------------------------------------------
| includes
+---------------------------------------------------------------------*/
#include "Ap4Types.h"
#include "Ap4Results.h"
/*----------------------------------------------------------------------
| constants
+---------------------------------------------------------------------*/
const int AP4_ERROR_BASE_BITSTREAM = -10000;
// the max frame size we can handle
const unsigned int AP4_BITSTREAM_BUFFER_SIZE = 8192;
// flags
#define AP4_BITSTREAM_FLAG_EOS 0x01
// error codes
const int AP4_ERROR_CORRUPTED_BITSTREAM = (AP4_ERROR_BASE_BITSTREAM - 0);
const int AP4_ERROR_NOT_ENOUGH_FREE_BUFFER = (AP4_ERROR_BASE_BITSTREAM - 1);
/*----------------------------------------------------------------------
| types helpers
+---------------------------------------------------------------------*/
/* use long by default */
typedef unsigned int AP4_BitsWord;
#define AP4_WORD_BITS 32
#define AP4_WORD_BYTES 4
/*----------------------------------------------------------------------
| types
+---------------------------------------------------------------------*/
class AP4_BitStream
{
public:
// constructor and destructor
AP4_BitStream();
~AP4_BitStream();
// methods
AP4_Result Reset();
AP4_Size GetContiguousBytesFree();
AP4_Size GetBytesFree();
AP4_Result WriteBytes(const AP4_UI08* bytes, AP4_Size byte_count);
AP4_Size GetContiguousBytesAvailable();
AP4_Size GetBytesAvailable();
AP4_UI08 ReadByte();
AP4_Result ReadBytes(AP4_UI08* bytes, AP4_Size byte_count);
AP4_UI08 PeekByte();
AP4_Result PeekBytes(AP4_UI08* bytes, AP4_Size byte_count);
int ReadBit();
AP4_UI32 ReadBits(unsigned int bit_count);
int PeekBit();
AP4_UI32 PeekBits(unsigned int bit_count);
AP4_Result SkipBytes(AP4_Size byte_count);
void SkipBit();
void SkipBits(unsigned int bit_count);
AP4_Result ByteAlign();
// members
AP4_UI08* m_Buffer;
unsigned int m_In;
unsigned int m_Out;
AP4_BitsWord m_Cache;
unsigned int m_BitsCached;
unsigned int m_Flags;
private:
// methods
AP4_BitsWord ReadCache() const;
};
/*----------------------------------------------------------------------
| macros
+---------------------------------------------------------------------*/
#define AP4_BIT_MASK(_n) ((1<<(_n))-1)
#define AP4_BITSTREAM_POINTER_VAL(offset) \
((offset)&(AP4_BITSTREAM_BUFFER_SIZE-1))
#define AP4_BITSTREAM_POINTER_OFFSET(pointer, offset) \
(AP4_BITSTREAM_POINTER_VAL((pointer)+(offset)))
#define AP4_BITSTREAM_POINTER_ADD(pointer, offset) \
((pointer) = AP4_BITSTREAM_POINTER_OFFSET(pointer, offset))
/*----------------------------------------------------------------------
| AP4_BitStream::ReadCache
+---------------------------------------------------------------------*/
inline AP4_BitsWord
AP4_BitStream::ReadCache() const
{
unsigned int pos = m_Out;
AP4_BitsWord cache;
#if AP4_WORD_BITS != 32
#error unsupported word size /* 64 and other word size not yet implemented */
#endif
if (pos <= AP4_BITSTREAM_BUFFER_SIZE - AP4_WORD_BYTES) {
unsigned char* out_ptr = &m_Buffer[pos];
cache = (((AP4_BitsWord) out_ptr[0]) << 24)
| (((AP4_BitsWord) out_ptr[1]) << 16)
| (((AP4_BitsWord) out_ptr[2]) << 8)
| (((AP4_BitsWord) out_ptr[3]) );
} else {
unsigned char* buf_ptr = m_Buffer;
cache = (((AP4_BitsWord) buf_ptr[ pos ]) << 24)
| (((AP4_BitsWord) buf_ptr[AP4_BITSTREAM_POINTER_OFFSET (pos, 1)]) << 16)
| (((AP4_BitsWord) buf_ptr[AP4_BITSTREAM_POINTER_OFFSET (pos, 2)]) << 8)
| (((AP4_BitsWord) buf_ptr[AP4_BITSTREAM_POINTER_OFFSET (pos, 3)]) );
}
return cache;
}
/*----------------------------------------------------------------------
| AP4_BitStream::ReadBits
+---------------------------------------------------------------------*/
inline AP4_UI32
AP4_BitStream::ReadBits(unsigned int n)
{
AP4_BitsWord result;
if (m_BitsCached >= n) {
/* we have enough bits in the cache to satisfy the request */
m_BitsCached -= n;
result = (m_Cache >> m_BitsCached) & AP4_BIT_MASK(n);
} else {
/* not enough bits in the cache */
AP4_BitsWord word;
/* read the next word */
{
word = ReadCache();
m_Out = AP4_BITSTREAM_POINTER_OFFSET(m_Out, AP4_WORD_BYTES);
}
/* combine the new word and the cache, and update the state */
{
AP4_BitsWord cache = m_Cache & AP4_BIT_MASK(m_BitsCached);
n -= m_BitsCached;
m_BitsCached = AP4_WORD_BITS - n;
result = (word >> m_BitsCached) | (cache << n);
m_Cache = word;
}
}
return result;
}
/*----------------------------------------------------------------------
| AP4_BitStream::ReadBit
+---------------------------------------------------------------------*/
inline int
AP4_BitStream::ReadBit()
{
AP4_BitsWord result;
if (m_BitsCached == 0) {
/* the cache is empty */
/* read the next word into the cache */
m_Cache = ReadCache();
m_Out = AP4_BITSTREAM_POINTER_OFFSET(m_Out, AP4_WORD_BYTES);
m_BitsCached = AP4_WORD_BITS - 1;
/* return the first bit */
result = m_Cache >> (AP4_WORD_BITS - 1);
} else {
/* get the bit from the cache */
result = (m_Cache >> (--m_BitsCached)) & 1;
}
return result;
}
/*----------------------------------------------------------------------
| AP4_BitStream::PeekBits
+---------------------------------------------------------------------*/
inline AP4_UI32
AP4_BitStream::PeekBits(unsigned int n)
{
/* we have enough bits in the cache to satisfy the request */
if (m_BitsCached >= n) {
return (m_Cache >> (m_BitsCached - n)) & AP4_BIT_MASK(n);
} else {
/* not enough bits in the cache, read the next word */
AP4_BitsWord word = ReadCache();
/* combine the new word and the cache, and update the state */
AP4_BitsWord cache = m_Cache & AP4_BIT_MASK(m_BitsCached);
n -= m_BitsCached;
return (word >> (AP4_WORD_BITS - n)) | (cache << n);
}
}
/*----------------------------------------------------------------------
| AP4_BitStream::PeekBit
+---------------------------------------------------------------------*/
inline int
AP4_BitStream::PeekBit()
{
/* the cache is empty */
if (m_BitsCached == 0) {
/* read the next word into the cache */
AP4_BitsWord cache = ReadCache();
/* return the first bit */
return cache >> (AP4_WORD_BITS - 1);
} else {
/* get the bit from the cache */
return (m_Cache >> (m_BitsCached-1)) & 1;
}
}
/*----------------------------------------------------------------------
| AP4_BitStream::SkipBits
+---------------------------------------------------------------------*/
inline void
AP4_BitStream::SkipBits(unsigned int n)
{
if (n <= m_BitsCached) {
m_BitsCached -= n;
} else {
n -= m_BitsCached;
while (n >= AP4_WORD_BITS) {
m_Out = AP4_BITSTREAM_POINTER_OFFSET(m_Out, AP4_WORD_BYTES);
n -= AP4_WORD_BITS;
}
if (n) {
m_Cache = ReadCache();
m_BitsCached = AP4_WORD_BITS-n;
m_Out = AP4_BITSTREAM_POINTER_OFFSET(m_Out, AP4_WORD_BYTES);
} else {
m_BitsCached = 0;
m_Cache = 0;
}
}
}
/*----------------------------------------------------------------------
| AP4_BitStream::SkipBit
+---------------------------------------------------------------------*/
inline void
AP4_BitStream::SkipBit()
{
if (m_BitsCached == 0) {
m_Cache = ReadCache();
m_Out = AP4_BITSTREAM_POINTER_OFFSET(m_Out, AP4_WORD_BYTES);
m_BitsCached = AP4_WORD_BITS - 1;
} else {
--m_BitsCached;
}
}
/*----------------------------------------------------------------------
| AP4_BitStream::ReadByte
+---------------------------------------------------------------------*/
inline AP4_UI08
AP4_BitStream::ReadByte()
{
SkipBits(m_BitsCached & 7);
return (AP4_UI08)ReadBits(8);
}
/*----------------------------------------------------------------------
| AP4_BitStream::PeekByte
+---------------------------------------------------------------------*/
inline AP4_UI08
AP4_BitStream::PeekByte()
{
unsigned int extra_bits = m_BitsCached & 7;
return (AP4_UI08)(PeekBits(extra_bits + 8)&0xFF);
}
#endif // _AP4_BIT_STREAM_H_
|