ZenLib
BitStream_Fast.h
Go to the documentation of this file.
1 /* Copyright (c) MediaArea.net SARL. All Rights Reserved.
2  *
3  * Use of this source code is governed by a zlib-style license that can
4  * be found in the License.txt file in the root of the source tree.
5  */
6 
7 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 //
9 // Read a stream bit per bit
10 // Can read up to 32 bits at once (64 bits with Get8 and Peek8)
11 //
12 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13 
14 //---------------------------------------------------------------------------
15 #ifndef ZenBitStream_FastH
16 #define ZenBitStream_FastH
17 //---------------------------------------------------------------------------
18 
19 //---------------------------------------------------------------------------
20 #include "ZenLib/Conf.h"
21 //---------------------------------------------------------------------------
22 
23 #include <cstddef>
24 #include <cstring>
25 #if defined(_MSC_VER)
26  #include <stdlib.h>
27  #include <intrin.h>
28 #endif
29 
30 namespace ZenLib
31 {
32 
33 class BitStream_Fast_Local;
34 class BitStream_Fast_EmulationPrevention_Local;
35 class BitStream_Fast_Mixed_Local;
36 
37 #ifndef MIN
38  #define MIN(a, b) (((a) < (b)) ? (a) : (b))
39 #endif
40 
41 #if defined(_MSC_VER)
42  #define ZENLIB_BSF_INLINE __forceinline
43  #define ZENLIB_BSF_NOINLINE __declspec(noinline)
44 #elif defined(__GNUC__) || defined(__clang__)
45  #define ZENLIB_BSF_INLINE inline __attribute__((always_inline))
46  #define ZENLIB_BSF_NOINLINE __attribute__((noinline))
47 #else
48  #define ZENLIB_BSF_INLINE inline
49  #define ZENLIB_BSF_NOINLINE
50 #endif
51 
52 //***************************************************************************
53 // BitStream_Fast
54 //***************************************************************************
55 //
56 // Design: the position is the count of remaining bits, Buffer_Size. Window
57 // holds one native big-endian word of the stream, loaded by Attach(),
58 // Resize() and Refresh(), then when a read goes past its usable bits. A read
59 // checks that its bits are in Window, decrements Buffer_Size and extracts
60 // them with one rotation and a mask: the position is the only state written
61 // by reads, and Skip() is a subtraction.
62 //
63 // Window is rotated at load time so that the bit read when Buffer_Size is B
64 // is its bit (B-1)%WordBits: after a read of n bits decremented Buffer_Size,
65 // the bits are the n low bits of Window rotated right by Buffer_Size. Reads
66 // only need, in addition to Buffer_Size:
67 // - WindowEnd: Buffer_Size value after the last usable bit of Window, never
68 // beyond the logical end. A read of n bits can use Window iff
69 // n+WindowEnd<=Buffer_Size, which also implies n<=Remain().
70 // - The position is never before Window: Window is loaded at or before the
71 // position, so the usable bits never wrap around. A position beyond Window
72 // (after Skip()) just fails the usability check.
73 // A reload at byte Position/8 gives at least WordBits-7 usable bits, or all
74 // the remaining bits when the word is moved back to end at the last readable
75 // byte. While Buffer_Size is 0, every read fails the usability check.
76 // In the normal mode, Get8(), Get8_Unchecked() and Peek8() parts not in Window,
77 // and peeks wider than a fresh Window (32-bit words), use their own word load
78 // at the position, Window unchanged.
79 //
80 // Underrun: Remain() becomes 0 and the logical end moves to the position,
81 // which is preserved for a growing Resize(); BufferUnderRun() thus implies
82 // Remain()==0.
83 //
84 // Memory safety: loads never go before Buffer or beyond the logical end plus
85 // the declared ExtraBytesAvailable_ reserve, the reserve keeping its Attach()
86 // time end. Near the end, the word is loaded so that it ends at the last
87 // readable byte. In the normal mode, if fewer bytes than a word are readable
88 // (logical size plus reserve), they are copied entirely into Window, zero
89 // padded, and never need another load: WindowEnd is then 0 and Reload() is
90 // never called.
91 //
92 // Emulation prevention (H.264, H.265 and H.266 NAL units): the reads skip each
93 // emulation_prevention_three_byte (0x03 after two 0x00 in the attached bytes),
94 // without scanning nor copying the buffer first. Three readers from the same code:
95 // BitStream_Fast (normal reads only, without the code nor the state of this mode),
96 // BitStream_Fast_EmulationPrevention (always in this mode) and BitStream_Fast_Mixed
97 // (Attach() for the normal mode, Attach_EmulationPrevention() for this mode).
98 // A load ends the usable bits of Window before the next such byte, the next load
99 // moves the position after it, and a load first counts the ones a Skip() went
100 // over. Remain() and Offset_Get() count the attached bytes, these bytes included:
101 // Remain() is the count of readable bits plus 8 per such byte not passed yet, so
102 // Skip(Remain()) goes to the end. Skip() checks its count against Remain() and the
103 // bytes it goes over are counted at the next load: until then, Offset_Get() is one
104 // byte low per such byte, and a Skip() beyond the readable bits (not up to the end)
105 // underruns at the next read or Sync(); Sync() counts them at once and passes the
106 // one at the position, so that Offset_Get() is exact. Remain_Exact() gives the
107 // count of readable bits, from a scan of the rest of the buffer. A peek does not
108 // change Remain(). An attach in the middle of a unit gives the count of 0x00 bytes
109 // right before it, for such a byte at the first two offsets. With fewer bytes
110 // than a word readable, the usable bits of Window may end before such a byte, so
111 // that Window is loaded again; the parts of Get8() not in Window load it too.
112 // The code of this mode is in the slow paths, called through
113 // BitStream_Fast_EmulationPrevention_Calls.
114 //
115 
116 // Reader kind, see the class comment
118 {
120  BitStream_Fast_EmulationPrevention_Yes, // BitStream_Fast_EmulationPrevention
122 };
123 
124 // State of the emulation prevention mode: none for BitStream_Fast (empty base, no size)
125 template<bitstream_fast_emulationprevention Kind>
127 {
128 };
129 template<>
131 {
132  size_t Readable_; // Readable bytes since Buffer
133  size_t Checked; // The bytes before are examined
134  int8u ZerosBefore; // 0x00 bytes of the unit right before Buffer
135 
136  bool EmulationPrevention_Mode() const { return true; }
138 };
139 // The same members and the mode, in their padding (members of a derived struct
140 // would come after the padding)
141 template<>
143 {
144  size_t Readable_; // Readable bytes since Buffer
145  size_t Checked; // The bytes before are examined
146  int8u ZerosBefore; // 0x00 bytes of the unit right before Buffer
147  bool Emulation; // Emulation prevention mode
148 
149  bool EmulationPrevention_Mode() const { return Emulation; }
150  void EmulationPrevention_Mode(bool Value) { Emulation=Value; }
151 };
152 
153 template<bitstream_fast_emulationprevention Kind>
155 
156 // Calls of the emulation prevention mode from the common code, by reader kind,
157 // for the reader only. BitStream_Fast: none, and Present is 0, first in each test
158 // of the mode, so that the compiler drops this code before anything else (with a
159 // test known only after inlining, some normal reads compile differently). The
160 // other readers: the functions of this mode, only instantiated for them.
161 template<bitstream_fast_emulationprevention Kind>
163 {
164 private:
165  friend class BitStream_Fast_Template<Kind>;
166  enum { Present=1 };
167  template<class R> static ZENLIB_BSF_INLINE bool Active(const R& Reader) { return Reader.EmulationPrevention_Mode(); }
168  template<class R> static ZENLIB_BSF_INLINE void Attach(R& Reader, bool Mode, int8u Zeros) { Reader.Attach_State_EmulationPrevention(Mode, Zeros); }
169  template<class R> static ZENLIB_BSF_INLINE void Fill(R& Reader, size_t Readable) { Reader.Fill_EmulationPrevention(Readable); }
170  template<class R> static ZENLIB_BSF_INLINE void Reload(R& Reader) { Reader.Reload_EmulationPrevention(); }
171  template<class R> static ZENLIB_BSF_INLINE void Sync(R& Reader) { Reader.Sync_EmulationPrevention(); }
172  template<class R> static ZENLIB_BSF_INLINE int64u Read(R& Reader, unsigned HowMany) { return Reader.Read_EmulationPrevention(HowMany); }
173  template<class R> static ZENLIB_BSF_INLINE int64u Peek(R& Reader, unsigned HowMany) { return Reader.Peek_EmulationPrevention(HowMany); }
174  template<class R> static ZENLIB_BSF_INLINE int32u GetUE(R& Reader) { return Reader.GetUE_EmulationPrevention(); }
175  template<class R> static ZENLIB_BSF_INLINE size_t Remain_Exact(R& Reader) { return Reader.Remain_Exact_EmulationPrevention(); }
176  template<class R> static ZENLIB_BSF_INLINE void Resize(R& Reader) { Reader.Resize_EmulationPrevention(); }
177  template<class R> static ZENLIB_BSF_INLINE void Refresh(R& Reader) { Reader.Refresh_EmulationPrevention(); }
178 };
179 template<>
181 {
182 private:
184  enum { Present=0 };
185  template<class R> static ZENLIB_BSF_INLINE bool Active(const R&) { return false; }
186  template<class R> static ZENLIB_BSF_INLINE void Attach(R&, bool, int8u) {}
187  template<class R> static ZENLIB_BSF_INLINE void Fill(R&, size_t) {}
188  template<class R> static ZENLIB_BSF_INLINE void Reload(R&) {}
189  template<class R> static ZENLIB_BSF_INLINE void Sync(R&) {}
190  template<class R> static ZENLIB_BSF_INLINE int64u Read(R&, unsigned) { return 0; }
191  template<class R> static ZENLIB_BSF_INLINE int64u Peek(R&, unsigned) { return 0; }
192  template<class R> static ZENLIB_BSF_INLINE int32u GetUE(R&) { return 0; }
193  template<class R> static ZENLIB_BSF_INLINE size_t Remain_Exact(R&) { return 0; }
194  template<class R> static ZENLIB_BSF_INLINE void Resize(R&) {}
195  template<class R> static ZENLIB_BSF_INLINE void Refresh(R&) {}
196 };
197 
198 template<bitstream_fast_emulationprevention Kind>
199 class BitStream_Fast_Template : private BitStream_Fast_EmulationPrevention_State<Kind>
200 {
201 public:
203  // ExtraBytesAvailable_ is the number of initialized, readable bytes after
204  // Buffer_+Size_, excluded from Size_ and Remain(); readable bytes must not
205  // be modified while attached, except bytes not read yet followed by a call
206  // to Refresh(). Zero requires no extra storage beyond the
207  // logical buffer. The reserve is not moved by Resize(): loads stay before
208  // Buffer_+Size_+ExtraBytesAvailable_, or before the logical end if a
209  // growing Resize() moved it further.
210  BitStream_Fast_Template (const int8u* Buffer_, size_t Size_, size_t ExtraBytesAvailable_=0) { Attach(Buffer_, Size_, ExtraBytesAvailable_); }
212 
213  // Same extra-byte contract as the constructor; omitted means zero.
214  // In the emulation prevention mode for BitStream_Fast_EmulationPrevention, else in the normal mode.
215  ZENLIB_BSF_INLINE void Attach(const int8u* Buffer_, size_t Size_, size_t ExtraBytesAvailable_=0)
216  {
217  Calls::Attach(*this, false, 0);
218  Attach_Common(Buffer_, Size_, ExtraBytesAvailable_);
219  }
220 
221  // Same as Attach(), the reads skipping the emulation prevention bytes: see the class comment.
222  // ZerosBefore_ (0 to 2) is the count of 0x00 bytes of the same unit right before Buffer_.
223  // Not for BitStream_Fast.
224  ZENLIB_BSF_INLINE void Attach_EmulationPrevention(const int8u* Buffer_, size_t Size_, size_t ExtraBytesAvailable_=0, int8u ZerosBefore_=0)
225  {
226  static_assert(Kind!=BitStream_Fast_EmulationPrevention_No, "Attach_EmulationPrevention() is not for BitStream_Fast");
227  Calls::Attach(*this, true, ZerosBefore_);
228  Attach_Common(Buffer_, Size_, ExtraBytesAvailable_);
229  }
230 
231  // Emulation prevention mode is active
232  bool EmulationPrevention_Get() const { return Calls::Present && Calls::Active(*this); }
233 
234  //***********************************************************************
235  // Reads
236  //***********************************************************************
237 
239  {
240  if (Buffer_Size<=WindowEnd)
241  {
242  if (Calls::Present && Calls::Active(*this))
243  return Calls::Read(*this, 1)!=0;
244  if (!Buffer_Size)
245  {
246  UnderRun();
247  return false;
248  }
249  Reload();
250  }
251  Buffer_Size--;
252  return TestBit(Window, Buffer_Size);
253  }
254 
255  ZENLIB_BSF_INLINE int8u Get1(int8u HowMany)
256  {
257  return (int8u)Get<8>(HowMany);
258  }
259 
260  ZENLIB_BSF_INLINE int16u Get2(int8u HowMany)
261  {
262  return (int16u)Get<16>(HowMany);
263  }
264 
265  ZENLIB_BSF_INLINE int32u Get4(int8u HowMany)
266  {
267  return (int32u)Get<32>(HowMany);
268  }
269 
270  ZENLIB_BSF_INLINE int64u Get8(int8u HowMany)
271  {
272  if (HowMany<=32)
273  return Get<32>(HowMany);
274  if (HowMany>64)
275  return 0;
276  if (HowMany>Buffer_Size)
277  {
278  // Historical behavior: the high part is consumed if it is available
279  if ((size_t)(HowMany-32)<=Buffer_Size)
280  Buffer_Size-=HowMany-32;
281  UnderRun();
282  return 0;
283  }
284  return Read64(HowMany);
285  }
286 
287  // Preconditions: 1 <= HowMany <= 32 and Remain() >= HowMany.
288  ZENLIB_BSF_INLINE int32u Get4_Unchecked(int8u HowMany)
289  {
290  return (int32u)Read<32>(HowMany);
291  }
292 
293  // Preconditions: 1 <= HowMany <= 64 and Remain() >= HowMany.
294  ZENLIB_BSF_INLINE int64u Get8_Unchecked(int8u HowMany)
295  {
296  return Read64(HowMany);
297  }
298 
299  // Exp-Golomb ue(v), up to 31 leading zero bits (values up to 2^32-2).
300  // Returns 0 if the code is longer or truncated; the buffer is then
301  // considered as exhausted (Remain() is 0 and BufferUnderRun() is set), so
302  // BufferUnderRun() right after the call tells an error from a valid 0.
304  {
305  Word Bits;
306  unsigned Size;
307  if (!PeekUE(Bits, Size))
308  return GetUE_Slow();
309  Buffer_Size-=Size;
310  return (int32u)(Bits>>(WordBits-Size))-1;
311  }
312 
313  // Exp-Golomb se(v). Returns 0 on error, see GetUE().
315  {
316  const int32u CodeNum=GetUE();
317  return (int32s)((CodeNum&1)?(CodeNum>>1)+1:0u-(CodeNum>>1));
318  }
319 
320  // Sticky error state; cleared by Attach() or by growing Resize().
321  bool BufferUnderRun() const { return BufferUnderRun_; }
322 
323  ZENLIB_BSF_INLINE void Skip(size_t HowMany)
324  {
325  if (HowMany>Buffer_Size)
326  {
327  UnderRun();
328  return;
329  }
330  Buffer_Size-=HowMany;
331  }
332 
334  {
335  if (Buffer_Size<=WindowEnd)
336  {
337  if (Calls::Present && Calls::Active(*this))
338  return Calls::Peek(*this, 1)!=0;
339  if (!Buffer_Size)
340  {
341  UnderRun();
342  return false;
343  }
344  Reload();
345  }
346  return TestBit(Window, Buffer_Size-1);
347  }
348 
349  ZENLIB_BSF_INLINE int8u Peek1(int8u HowMany)
350  {
351  return (int8u)Peek<8>(HowMany);
352  }
353 
354  ZENLIB_BSF_INLINE int16u Peek2(int8u HowMany)
355  {
356  return (int16u)Peek<16>(HowMany);
357  }
358 
359  ZENLIB_BSF_INLINE int32u Peek4(int8u HowMany)
360  {
361  return (int32u)Peek<32>(HowMany);
362  }
363 
364  int64u Peek8(int8u HowMany)
365  {
366  if (HowMany<=32)
367  return Peek<32>(HowMany);
368  if (HowMany>64)
369  return 0;
370  if (HowMany>Buffer_Size)
371  {
372  UnderRun();
373  return 0;
374  }
375  if (Calls::Present && Calls::Active(*this) && HowMany+WindowEnd>Buffer_Size)
376  return Calls::Peek(*this, HowMany);
377  if (HowMany<=WordBits)
378  return PeekWide(Buffer_Size, HowMany);
379  // 32-bit words: high part, then the 32 bits after it
380  const int64u High=PeekWide(Buffer_Size, HowMany-32);
381  return (High<<32)|PeekWide(Buffer_Size-(HowMany-32), 32);
382  }
383 
384  inline size_t Remain() const
385  {
386  return Buffer_Size;
387  }
388 
389  // Emulation prevention mode: counts the emulation prevention bytes a Skip() went over and passes
390  // the one at the position, so that Remain() and Offset_Get() are those of the position; a Skip()
391  // beyond the readable bits (not up to the end) underruns here. No effect in the normal mode.
392  inline void Sync()
393  {
394  if (Calls::Present && Calls::Active(*this) && Buffer_Size)
395  Calls::Sync(*this);
396  }
397 
398  // Count of readable bits: Remain() less 8 per emulation prevention byte not passed yet, from a scan
399  // of the rest of the buffer (for small units). Same as Remain() in the normal mode.
400  size_t Remain_Exact()
401  {
402  if (!Calls::Present || !Calls::Active(*this) || !Buffer_Size)
403  return Buffer_Size;
404  return Calls::Remain_Exact(*this);
405  }
406 
407  inline void Byte_Align()
408  {
409  Buffer_Size&=~(size_t)7;
410  }
411 
412  inline size_t Offset_Get() const
413  {
414  return (Buffer_Size_Init-Buffer_Size)/8;
415  }
416 
417  inline size_t BitOffset_Get() const
418  {
419  return Buffer_Size%8;
420  }
421 
422  inline size_t OffsetBeforeLastCall_Get() const //No more valid
423  {
424  return Buffer_Size%8;
425  }
426 
427  // Size_ is the new count of remaining bits: the position is kept, only the logical end
428  // moves, to the position plus Size_ bits (this sum must fit in a size_t). Beyond the
429  // attached size, the bytes up to the new logical end must be readable, see Attach().
430  inline void Resize(size_t Size_)
431  {
432  if (BufferUnderRun_ && Size_>Buffer_Size)
433  BufferUnderRun_=false;
434  if (Calls::Present && Calls::Active(*this))
435  Calls::Resize(*this);
436  BitEnd=BitEnd-Buffer_Size+Size_;
437  Buffer_Size=Size_;
438  Fill();
439  }
440 
441  // Loads Window again from the attached bytes at the position: needed after
442  // modifying bytes not read yet (for example a descrambling in place), as
443  // Window may already contain them. The position and the state are kept.
444  inline void Refresh()
445  {
446  if (Calls::Present && Calls::Active(*this))
447  Calls::Refresh(*this);
448  Fill();
449  }
450 
451 private:
452  typedef size_t Word;
453  enum { WordBits=sizeof(Word)*8 };
454 
455  //***********************************************************************
456  // Word helpers
457  //***********************************************************************
458 
459  static ZENLIB_BSF_INLINE Word LoadWord(const int8u* Source)
460  {
461  Word Value;
462  std::memcpy(&Value, Source, sizeof(Value));
463  #if defined(__BYTE_ORDER__) && __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
464  return Value;
465  #elif defined(_MSC_VER)
466  #if defined(_WIN64)
467  return _byteswap_uint64(Value);
468  #else
469  return _byteswap_ulong(Value);
470  #endif
471  #elif (defined(__GNUC__) || defined(__clang__)) && defined(__BYTE_ORDER__) && __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
472  return sizeof(Word)==8?(Word)__builtin_bswap64((int64u)Value):(Word)__builtin_bswap32((int32u)Value);
473  #else
474  // Unknown byte order
475  Word Result=0;
476  for (size_t i=0; i<sizeof(Word); i++)
477  Result=(Result<<8)|Source[i];
478  return Result;
479  #endif
480  }
481 
482  // Big-endian value of Size bytes (Size<sizeof(Word)), left aligned, zero padded
483  static ZENLIB_BSF_NOINLINE Word LoadPartial(const int8u* Source, size_t Size)
484  {
485  Word Value=0;
486  for (size_t i=0; i<Size; i++)
487  Value|=(Word)Source[i]<<(WordBits-8-8*i);
488  return Value;
489  }
490 
491  static ZENLIB_BSF_INLINE unsigned CountLeadingZeros(Word Value) //Value!=0
492  {
493  #if defined(_MSC_VER)
494  unsigned long Index;
495  #if defined(_WIN64)
496  _BitScanReverse64(&Index, Value);
497  #else
498  _BitScanReverse(&Index, Value);
499  #endif
500  return (unsigned)(WordBits-1-Index);
501  #elif defined(__GNUC__) || defined(__clang__)
502  return sizeof(Word)==8?(unsigned)__builtin_clzll((unsigned long long)Value):(unsigned)__builtin_clz((unsigned)Value);
503  #else
504  unsigned Count=0;
505  while (!(Value>>(WordBits-1))) { Value<<=1; Count++; }
506  return Count;
507  #endif
508  }
509 
510  // Mask of the HowMany low bits, 0 <= HowMany <= WordBits
511  static ZENLIB_BSF_INLINE Word Mask(unsigned HowMany)
512  {
513  // A table avoids a variable shift when HowMany is not a constant.
514  // 64-bit entries for any word size: with 32-bit words, only the first 33 are used.
515  #define ZENLIB_BSF_MASK4(N) ((int64u)1<<(N))-1, ((int64u)2<<(N))-1, ((int64u)4<<(N))-1, ((int64u)8<<(N))-1
516  static const int64u Masks[65]=
517  {
522  (int64u)-1,
523  };
524  #undef ZENLIB_BSF_MASK4
525  return (Word)Masks[HowMany];
526  }
527 
528  // Value rotated right by Count%WordBits bits
529  static ZENLIB_BSF_INLINE Word RotateRight(Word Value, size_t Count)
530  {
531  #if defined(_MSC_VER) && !defined(__clang__)
532  // The instruction masks the count
533  #if defined(_WIN64)
534  return _rotr64(Value, (int)Count);
535  #else
536  return _rotr(Value, (int)Count);
537  #endif
538  #else
539  const unsigned Shift=(unsigned)(Count&(WordBits-1));
540  return (Value>>Shift)|(Value<<((WordBits-Shift)&(WordBits-1))); // Recognized as a rotation
541  #endif
542  }
543 
544  // Bit Index%WordBits of Value, 0 being the least significant bit
545  static ZENLIB_BSF_INLINE bool TestBit(Word Value, size_t Index)
546  {
547  // Both forms let the instruction mask the index: a rotation with MSVC,
548  // a bit test or a shift with GCC and Clang
549  #if defined(_MSC_VER) && !defined(__clang__)
550  return (RotateRight(Value, Index)&1)!=0;
551  #else
552  return ((Value>>(Index&(WordBits-1)))&1)!=0;
553  #endif
554  }
555 
556  ZENLIB_BSF_INLINE void Attach_Common(const int8u* Buffer_, size_t Size_, size_t ExtraBytesAvailable_)
557  {
558  Buffer=Buffer_;
559  Buffer_Size=Buffer_Size_Init=BitEnd=Size_*8;
560  // A reserve of one word minus one byte is enough for any load
561  ExtraBytes=(int8u)MIN(ExtraBytesAvailable_, sizeof(Word)-1);
562  BufferUnderRun_=false;
563  Fill();
564  }
565 
566  //***********************************************************************
567  // Window management
568  //***********************************************************************
569 
570  // Loads Window at byte Position/8, or at the last loadable byte offset.
571  // Precondition in the normal mode: a full word is readable (LastLoad is valid), which is the case
572  // if WindowEnd>0. The emulation prevention mode loads any buffer size.
573  ZENLIB_BSF_INLINE void Reload()
574  {
575  if (Calls::Present && Calls::Active(*this))
576  {
577  Calls::Reload(*this);
578  return;
579  }
580  size_t Load=(BitEnd-Buffer_Size)/8;
581  if (Load>LastLoad)
582  Load=LastLoad; // Near the end, the word ends at the last readable byte
583  // Bits from the word start to the logical end, then Buffer_Size value at the least
584  // significant bit of the word (wraps around if the word ends after the logical end).
585  // Unsigned comparison: with 32-bit words, Avail may be 2^31 or more.
586  const size_t Avail=BitEnd-Load*8;
587  const size_t WindowLow=Avail-WordBits;
588  Window=RotateRight(LoadWord(Buffer+Load), 0-WindowLow);
589  WindowEnd=Avail>WordBits?WindowLow:0;
590  }
591 
592  // Computes the load limit for the current logical end then loads Window, any buffer size
593  ZENLIB_BSF_INLINE void Fill()
594  {
595  // Readable bytes since Buffer: the logical ones, then the reserve, trusted
596  // only up to its end at Attach() time, as a growing Resize() may move the
597  // logical end beyond the attached size
598  const size_t Logical=BitEnd/8+(BitEnd%8!=0); // No overflow after any Resize()
599  const size_t Attached=Buffer_Size_Init/8+ExtraBytes;
600  size_t Readable=Logical+ExtraBytes;
601  if (Readable>Attached)
602  Readable=Logical>Attached?Logical:Attached;
603  Calls::Fill(*this, Readable);
604  if (Readable>=sizeof(Word))
605  {
606  LastLoad=Readable-sizeof(Word);
607  if (Buffer_Size)
608  {
609  Reload();
610  return;
611  }
612  }
613  else
614  {
615  LastLoad=0; // Never used: Window contains the whole buffer
616  if (Buffer_Size)
617  {
618  if (Calls::Present && Calls::Active(*this))
619  {
620  Calls::Reload(*this);
621  return;
622  }
623  Window=RotateRight(LoadPartial(Buffer, Readable), WordBits-BitEnd);
624  WindowEnd=0;
625  return;
626  }
627  }
628 
629  // Nothing to read, nothing to load: every read fails the usability check
630  Window=0;
631  WindowEnd=0;
632  }
633 
634  ZENLIB_BSF_INLINE void UnderRun()
635  {
636  // The logical end moves to the position, which is kept for a growing Resize()
637  BitEnd-=Buffer_Size;
638  Buffer_Size=0;
639  WindowEnd=0;
640  BufferUnderRun_=true;
641  }
642 
643  //***********************************************************************
644  // Emulation prevention mode
645  //***********************************************************************
646 
647  // Byte at offset Pos is an emulation prevention byte: 0x03 after two 0x00 (before Buffer: this->ZerosBefore),
648  // all before the logical end
649  ZENLIB_BSF_INLINE bool IsEmulationPrevention(size_t Pos) const
650  {
651  if (Pos*8+8>BitEnd || Buffer[Pos]!=0x03)
652  return false;
653  if (Pos>=2)
654  return !Buffer[Pos-1] && !Buffer[Pos-2];
655  if (Pos)
656  return !Buffer[0] && this->ZerosBefore;
657  return this->ZerosBefore>=2;
658  }
659 
660  // Moves the position after the emulation prevention bytes up to it: the one at the position,
661  // and the ones a Skip() went over. Bytes before this->Checked were already examined. A Skip() beyond
662  // the readable bits underruns here: fewer bits left than such bytes, or such bytes left once the
663  // end is reached.
664  ZENLIB_BSF_NOINLINE void Sync_EmulationPrevention()
665  {
666  size_t Pos=(BitEnd-Buffer_Size)/8;
667  while (this->Checked<=Pos && Buffer_Size)
668  {
669  if (IsEmulationPrevention(this->Checked))
670  {
671  if (Buffer_Size<8)
672  {
673  UnderRun();
674  return;
675  }
676  Buffer_Size-=8;
677  if (!Buffer_Size)
678  {
679  while (++this->Checked*8<BitEnd)
680  if (IsEmulationPrevention(this->Checked))
681  {
682  UnderRun();
683  return;
684  }
685  return;
686  }
687  Pos=(BitEnd-Buffer_Size)/8;
688  }
689  this->Checked++;
690  }
691  }
692 
693  static ZENLIB_BSF_INLINE bool HasZeroByte(Word Value)
694  {
695  const Word Ones=(Word)-1/255; // 0x01 in each byte
696  return ((Value-Ones)&~Value&(Ones<<7))!=0;
697  }
698 
699  // Loads Window at the position, any buffer size: first moves the position after the emulation
700  // prevention bytes up to it, then ends the usable bits before the next one.
701  ZENLIB_BSF_NOINLINE void Reload_EmulationPrevention()
702  {
703  Sync_EmulationPrevention();
704  const size_t Pos=(BitEnd-Buffer_Size)/8;
705  if (!Buffer_Size)
706  {
707  WindowEnd=0;
708  return;
709  }
710  size_t Load;
711  Word Value;
712  if (this->Readable_>=sizeof(Word))
713  {
714  Load=Pos<LastLoad?Pos:LastLoad; // Near the end, the word ends at the last readable byte
715  Value=LoadWord(Buffer+Load);
716  }
717  else
718  {
719  Load=0;
720  Value=LoadPartial(Buffer, this->Readable_);
721  }
722  const size_t Avail=BitEnd-Load*8;
723  const size_t WindowLow=Avail-WordBits;
724  Window=RotateRight(Value, 0-WindowLow);
725  WindowEnd=Avail>WordBits?WindowLow:0;
726  // Usable bytes after the position: an emulation prevention byte among them needs a 0x00 in the word
727  size_t End=BitEnd/8;
728  if (End>Load+sizeof(Word))
729  End=Load+sizeof(Word);
730  if (this->Readable_<sizeof(Word) || HasZeroByte(Value))
731  for (size_t i=this->Checked; i<End; i++)
732  if (IsEmulationPrevention(i))
733  {
734  WindowEnd=BitEnd-i*8;
735  this->Checked=i;
736  return;
737  }
738  if (this->Checked<End)
739  this->Checked=End;
740  }
741 
742  // HowMany bits (1 to 64) across emulation prevention bytes, Window reloaded as needed
743  ZENLIB_BSF_NOINLINE int64u Read_EmulationPrevention(unsigned HowMany)
744  {
745  const size_t Start_Size=Buffer_Size, Start_End=BitEnd, Start_Checked=this->Checked;
746  int64u Value=0;
747  for (;;)
748  {
749  if (Buffer_Size<=WindowEnd)
750  {
751  if (Buffer_Size)
752  Reload_EmulationPrevention();
753  if (Buffer_Size<=WindowEnd)
754  {
755  UnderRun_At(Start_Size, Start_End, Start_Checked);
756  return 0;
757  }
758  }
759  size_t Usable=Buffer_Size-WindowEnd;
760  if (Usable>HowMany)
761  Usable=HowMany;
762  const unsigned Part=(unsigned)Usable;
763  Buffer_Size-=Part;
764  Value=(Part<64?(Value<<Part):0)|(RotateRight(Window, Buffer_Size)&Mask(Part));
765  HowMany-=Part;
766  if (!HowMany)
767  return Value;
768  }
769  }
770 
771  // Underrun at a previous position (a read in several parts)
772  ZENLIB_BSF_NOINLINE void UnderRun_At(size_t Size, size_t End, size_t Checked_)
773  {
774  Buffer_Size=Size;
775  BitEnd=End;
776  this->Checked=Checked_;
777  UnderRun();
778  }
779 
780  // Same, without moving the position; an underrun is that of the read
781  ZENLIB_BSF_NOINLINE int64u Peek_EmulationPrevention(unsigned HowMany)
782  {
783  BitStream_Fast_Template Copy(*this);
784  const int64u Value=Copy.Read_EmulationPrevention(HowMany);
785  if (Copy.BufferUnderRun_)
786  {
787  UnderRun();
788  return 0;
789  }
790  return Value;
791  }
792 
793  // Exp-Golomb code across emulation prevention bytes, same results as GetUE()
794  ZENLIB_BSF_NOINLINE int32u GetUE_EmulationPrevention()
795  {
796  const size_t Start_Size=Buffer_Size, Start_End=BitEnd, Start_Checked=this->Checked;
797  unsigned LeadingZeroBits=0;
798  for (;;)
799  {
800  if (!Buffer_Size) // Also after an underrun of GetB()
801  {
802  UnderRun_At(Start_Size, Start_End, Start_Checked);
803  return 0;
804  }
805  if (GetB())
806  break;
807  if (++LeadingZeroBits>31)
808  {
809  UnderRun_At(Start_Size, Start_End, Start_Checked);
810  return 0;
811  }
812  }
813  if (!LeadingZeroBits)
814  return 0;
815  const int64u Info=Read_EmulationPrevention(LeadingZeroBits);
816  if (BufferUnderRun_)
817  {
818  UnderRun_At(Start_Size, Start_End, Start_Checked);
819  return 0;
820  }
821  return (int32u)((((int64u)1<<LeadingZeroBits)|Info)-1);
822  }
823 
824  // Attach(), Attach_EmulationPrevention(): mode, then state for the attached bytes
825  ZENLIB_BSF_INLINE void Attach_State_EmulationPrevention(bool Mode, int8u ZerosBefore_)
826  {
827  this->EmulationPrevention_Mode(Mode);
828  this->ZerosBefore=ZerosBefore_;
829  this->Checked=0;
830  }
831 
832  // Fill()
833  ZENLIB_BSF_INLINE void Fill_EmulationPrevention(size_t Readable)
834  {
835  this->Readable_=Readable;
836  }
837 
838  // Remain_Exact(), some bits left
839  size_t Remain_Exact_EmulationPrevention()
840  {
841  Sync_EmulationPrevention();
842  if (!Buffer_Size)
843  return 0;
844  size_t Count=0;
845  const size_t End=BitEnd/8;
846  for (size_t i=this->Checked; i<End; i++)
847  if (IsEmulationPrevention(i))
848  Count++;
849  return Buffer_Size-Count*8;
850  }
851 
852  // Resize(), before the change of the logical end
853  ZENLIB_BSF_INLINE void Resize_EmulationPrevention()
854  {
855  const size_t Pos=(BitEnd-Buffer_Size)/8;
856  if (this->Checked>Pos || !Buffer_Size)
857  this->Checked=Pos;
858  }
859 
860  // Refresh(): the bytes after the position are examined again
861  ZENLIB_BSF_INLINE void Refresh_EmulationPrevention()
862  {
863  if (this->Checked>(BitEnd-Buffer_Size)/8)
864  this->Checked=(BitEnd-Buffer_Size)/8;
865  }
866 
867  //***********************************************************************
868  // Reads
869  //***********************************************************************
870 
871  template <int Max>
872  ZENLIB_BSF_INLINE Word Get(int8u HowMany)
873  {
874  // Rejects 0 and HowMany>Max with a single comparison
875  if ((unsigned)(HowMany-1)>=(unsigned)Max)
876  return 0;
877  return Read<Max>(HowMany);
878  }
879 
880  template <int Max>
881  ZENLIB_BSF_INLINE Word Peek(int8u HowMany)
882  {
883  if ((unsigned)(HowMany-1)>=(unsigned)Max)
884  return 0;
885  if (HowMany+WindowEnd>Buffer_Size)
886  {
887  if (Calls::Present && Calls::Active(*this))
888  return (Word)Calls::Peek(*this, HowMany);
889  if (HowMany>Buffer_Size)
890  {
891  UnderRun();
892  return 0;
893  }
894  Reload();
895  // A fresh Window has at least WordBits-7 usable bits
896  if (Max>WordBits-7 && HowMany+WindowEnd>Buffer_Size)
897  return PeekDirect(Buffer_Size, HowMany);
898  }
899  return RotateRight(Window, Buffer_Size-HowMany)&Mask(HowMany);
900  }
901 
902  // Preconditions: 1 <= HowMany <= Max <= WordBits (if HowMany>Remain(): underrun)
903  template <int Max>
904  ZENLIB_BSF_INLINE Word Read(unsigned HowMany)
905  {
906  if (HowMany+WindowEnd>Buffer_Size)
907  {
908  if (Calls::Present && Calls::Active(*this))
909  return (Word)Calls::Read(*this, HowMany);
910  if (HowMany>Buffer_Size)
911  {
912  UnderRun();
913  return 0;
914  }
915  Reload();
916  // A fresh Window has at least WordBits-7 usable bits
917  if (Max>WordBits-7 && HowMany+WindowEnd>Buffer_Size)
918  return Read_Split(HowMany);
919  }
920  Buffer_Size-=HowMany;
921  return RotateRight(Window, Buffer_Size)&Mask(HowMany);
922  }
923 
924  // Read wider than the usable bits of a fresh Window (32-bit words only): the high
925  // part from Window, which is then reloaded at the position of the low part
926  ZENLIB_BSF_NOINLINE Word Read_Split(unsigned HowMany)
927  {
928  const unsigned High_Size=(unsigned)(Buffer_Size-WindowEnd);
929  Buffer_Size-=High_Size;
930  const Word High=RotateRight(Window, Buffer_Size)&Mask(High_Size);
931  Reload();
932  const unsigned Low_Size=HowMany-High_Size;
933  Buffer_Size-=Low_Size;
934  return (High<<Low_Size)|(RotateRight(Window, Buffer_Size)&Mask(Low_Size));
935  }
936 
937  // Preconditions: 1 <= HowMany <= 64 (if HowMany>Remain(): underrun)
938  ZENLIB_BSF_INLINE int64u Read64(unsigned HowMany)
939  {
940  if (HowMany<=WordBits)
941  return ReadWide(HowMany);
942  if (Calls::Present && Calls::Active(*this))
943  return Calls::Read(*this, HowMany);
944  const int64u High=ReadWide(HowMany-32);
945  return (High<<32)|ReadWide(32);
946  }
947 
948  // A wide read would consume most of a reloaded Window: it uses its own load instead.
949  // Preconditions: 1 <= HowMany <= WordBits (if HowMany>Remain(): underrun)
950  ZENLIB_BSF_INLINE Word ReadWide(unsigned HowMany)
951  {
952  Word Value;
953  if (HowMany+WindowEnd<=Buffer_Size)
954  Value=RotateRight(Window, Buffer_Size-HowMany)&Mask(HowMany);
955  else if (Calls::Present && Calls::Active(*this))
956  return (Word)Calls::Read(*this, HowMany);
957  else if (HowMany<=Buffer_Size)
958  Value=PeekDirect(Buffer_Size, HowMany);
959  else
960  {
961  UnderRun();
962  return 0;
963  }
964  Buffer_Size-=HowMany;
965  return Value;
966  }
967 
968  // HowMany bits at the position where Remain() would be Remaining, no state change.
969  // Preconditions: 1 <= HowMany <= WordBits and HowMany <= Remaining <= Remain(),
970  // so that the position is not before Window.
971  ZENLIB_BSF_INLINE Word PeekWide(size_t Remaining, unsigned HowMany) const
972  {
973  if (HowMany+WindowEnd<=Remaining)
974  return RotateRight(Window, Remaining-HowMany)&Mask(HowMany);
975  return PeekDirect(Remaining, HowMany);
976  }
977 
978  // HowMany bits at the position where Remain() would be Remaining, from their own
979  // word load, Window unchanged.
980  // Preconditions: 1 <= HowMany <= WordBits, HowMany <= Remaining <= Remain() and the
981  // bits are not all in Window (so a full word is readable).
982  ZENLIB_BSF_INLINE Word PeekDirect(size_t Remaining, unsigned HowMany) const
983  {
984  const size_t BitPos=BitEnd-Remaining;
985  size_t Load=BitPos/8;
986  if (Load>LastLoad)
987  Load=LastLoad; // The word then contains all the remaining bits
988  const unsigned Shift=(unsigned)(BitPos-Load*8);
989  Word Value=LoadWord(Buffer+Load)<<Shift;
990  if (Shift+HowMany>WordBits)
991  Value|=(Word)(Buffer[Load+sizeof(Word)]>>(8-Shift)); // Not clamped, before the logical end
992  return Value>>(WordBits-HowMany);
993  }
994 
995  //***********************************************************************
996  // Exp-Golomb
997  //***********************************************************************
998 
999  // Exp-Golomb code inside Window: bits from the position, left aligned, and code size
1000  ZENLIB_BSF_INLINE bool PeekUE(Word& Bits, unsigned& Size) const
1001  {
1002  if (Buffer_Size<=WindowEnd)
1003  return false;
1004  Bits=RotateRight(Window, Buffer_Size); // Next bit at the top, then the usable bits
1005  if (!Bits)
1006  return false;
1007  // The whole code must be in the usable bits, so it is valid even if the
1008  // leading one was found in the bits wrapped around after them.
1009  // Size<=WordBits-1, so at most 31 leading zero bits on 64-bit.
1010  Size=CountLeadingZeros(Bits)*2+1;
1011  return Size<=Buffer_Size-WindowEnd; // Size+WindowEnd could overflow
1012  }
1013 
1014  // Exp-Golomb code not inside Window
1015  ZENLIB_BSF_NOINLINE int32u GetUE_Slow()
1016  {
1017  // Reload if bits after Window are readable, else the code is long, truncated or invalid
1018  Word Bits;
1019  unsigned Size;
1020  if (WindowEnd)
1021  {
1022  Reload();
1023  if (PeekUE(Bits, Size))
1024  {
1025  Buffer_Size-=Size;
1026  return (int32u)(Bits>>(WordBits-Size))-1;
1027  }
1028  }
1029  if (Calls::Present && Calls::Active(*this))
1030  return Calls::GetUE(*this);
1031  if (Buffer_Size)
1032  {
1033  const int64u Result=GetUE_Bytes(Buffer, BitEnd-Buffer_Size, Buffer_Size);
1034  const size_t Code_Size=(size_t)(Result>>32);
1035  if (Code_Size)
1036  {
1037  Buffer_Size-=Code_Size; // The next read reloads Window if needed
1038  return (int32u)Result;
1039  }
1040  }
1041  UnderRun();
1042  return 0;
1043  }
1044 
1045  // Exp-Golomb code read byte per byte, for codes not fitting in a fresh Window.
1046  // Returns the code size (0 on error) in the high 32 bits, the value in the low 32 bits.
1047  static ZENLIB_BSF_NOINLINE int64u GetUE_Bytes(const int8u* Buffer, size_t BitPos, size_t Remain)
1048  {
1049  const unsigned Peeked=(unsigned)MIN(Remain, (size_t)32);
1050  const int32u Prefix=ReadBytes(Buffer, BitPos, Peeked)<<(32-Peeked);
1051  if (!Prefix)
1052  return 0; // More than 31 leading zero bits, or truncated
1053  unsigned LeadingZeroBits=0;
1054  while (!(Prefix>>(31-LeadingZeroBits)))
1055  LeadingZeroBits++;
1056  if (LeadingZeroBits*2+1>Remain)
1057  return 0; // Truncated
1058  const int32u Info=LeadingZeroBits?ReadBytes(Buffer, BitPos+LeadingZeroBits+1, LeadingZeroBits):0;
1059  return ((int64u)(LeadingZeroBits*2+1)<<32)|((((int32u)1<<LeadingZeroBits)|Info)-1);
1060  }
1061 
1062  // HowMany bits (1 to 32) at BitPos, reading only the bytes containing them
1063  static int32u ReadBytes(const int8u* Buffer, size_t BitPos, unsigned HowMany)
1064  {
1065  const int8u* Source=Buffer+BitPos/8;
1066  const unsigned Skipped=(unsigned)(BitPos%8);
1067  const unsigned Bytes=(Skipped+HowMany+7)/8;
1068  int64u Value=0;
1069  for (unsigned i=0; i<Bytes; i++)
1070  Value=(Value<<8)|Source[i];
1071  return (int32u)((Value>>(Bytes*8-Skipped-HowMany))&(((int64u)1<<HowMany)-1));
1072  }
1073 
1074  //***********************************************************************
1075  // Calls of the emulation prevention mode, see BitStream_Fast_EmulationPrevention_Calls
1076  //***********************************************************************
1077 
1080 
1081  // Hot state, see the class comment
1082  Word Window;
1083  size_t Buffer_Size; // Remaining bits
1084  size_t WindowEnd;
1085  // Cold state, used when Window is (re)loaded
1086  const int8u* Buffer;
1087  size_t BitEnd; // Logical end, in bits since Buffer
1088  size_t LastLoad; // Last byte offset where a full word can be loaded
1089  size_t Buffer_Size_Init;
1090  int8u ExtraBytes;
1091  bool BufferUnderRun_;
1092 };
1093 
1094 //***************************************************************************
1095 // Readers
1096 //***************************************************************************
1097 
1098 // Normal reads only. The members are those of the base, also in the type of a
1099 // member pointer (&BitStream_Fast::Get4).
1100 class BitStream_Fast : public BitStream_Fast_Template<BitStream_Fast_EmulationPrevention_No>
1101 {
1102 public:
1104  BitStream_Fast (const int8u* Buffer_, size_t Size_, size_t ExtraBytesAvailable_=0)
1105  : BitStream_Fast_Template<BitStream_Fast_EmulationPrevention_No>(Buffer_, Size_, ExtraBytesAvailable_) {}
1106 
1108 };
1109 
1110 // Every read skips the emulation prevention bytes (Attach() and the constructor too), see the class comment
1111 class BitStream_Fast_EmulationPrevention : public BitStream_Fast_Template<BitStream_Fast_EmulationPrevention_Yes>
1112 {
1113 public:
1115  BitStream_Fast_EmulationPrevention (const int8u* Buffer_, size_t Size_, size_t ExtraBytesAvailable_=0)
1116  : BitStream_Fast_Template<BitStream_Fast_EmulationPrevention_Yes>(Buffer_, Size_, ExtraBytesAvailable_) {}
1117 
1119 };
1120 
1121 // Mode chosen at each attach: Attach() for the normal mode, Attach_EmulationPrevention() for the emulation
1122 // prevention mode, see the class comment
1123 class BitStream_Fast_Mixed : public BitStream_Fast_Template<BitStream_Fast_EmulationPrevention_Mixed>
1124 {
1125 public:
1127  BitStream_Fast_Mixed (const int8u* Buffer_, size_t Size_, size_t ExtraBytesAvailable_=0)
1128  : BitStream_Fast_Template<BitStream_Fast_EmulationPrevention_Mixed>(Buffer_, Size_, ExtraBytesAvailable_) {}
1129 
1131 };
1132 
1133 // Independent parsing state: changes become visible in the owner only at Commit().
1134 // The owner must outlive the session and must not be modified before Commit().
1135 template<class Reader>
1136 class BitStream_Fast_Local_Template : public Reader
1137 {
1138 public:
1139  explicit BitStream_Fast_Local_Template(Reader& Owner_)
1140  : Reader(Owner_), Owner(&Owner_) {}
1142  : Reader(Other), Owner(Other.Owner) { Other.Owner=NULL; }
1145 
1146  // Explicit commit permits speculative parsing; destruction discards changes.
1147  void Commit()
1148  {
1149  if (Owner)
1150  *Owner=static_cast<const Reader&>(*this);
1151  }
1152 
1153 private:
1154  Reader* Owner;
1155 };
1156 
1158 {
1159 public:
1162 };
1163 
1164 class BitStream_Fast_EmulationPrevention_Local : public BitStream_Fast_Local_Template<BitStream_Fast_EmulationPrevention>
1165 {
1166 public:
1169 };
1170 
1171 class BitStream_Fast_Mixed_Local : public BitStream_Fast_Local_Template<BitStream_Fast_Mixed>
1172 {
1173 public:
1176 };
1177 
1179 {
1180  return BitStream_Fast_Local(*this);
1181 }
1182 
1184 {
1186 }
1187 
1189 {
1190  return BitStream_Fast_Mixed_Local(*this);
1191 }
1192 
1193 #undef ZENLIB_BSF_INLINE
1194 #undef ZENLIB_BSF_NOINLINE
1195 
1196 } //namespace ZenLib
1197 #endif
ZENLIB_BSF_INLINE bool GetB()
Definition: BitStream_Fast.h:238
#define ZENLIB_BSF_NOINLINE
Definition: BitStream_Fast.h:49
ZENLIB_BSF_INLINE int64u Get8(int8u HowMany)
Definition: BitStream_Fast.h:270
ZENLIB_BSF_INLINE int16u Get2(int8u HowMany)
Definition: BitStream_Fast.h:260
#define ZENLIB_BSF_MASK4(N)
ZENLIB_BSF_INLINE int8u Peek1(int8u HowMany)
Definition: BitStream_Fast.h:349
BitStream_Fast(const int8u *Buffer_, size_t Size_, size_t ExtraBytesAvailable_=0)
Definition: BitStream_Fast.h:1104
#define MIN(a, b)
Definition: BitStream_Fast.h:38
bool BufferUnderRun() const
Definition: BitStream_Fast.h:321
ZENLIB_BSF_INLINE void Skip(size_t HowMany)
Definition: BitStream_Fast.h:323
BitStream_Fast_Local LocalReader()
Definition: BitStream_Fast.h:1178
ZENLIB_BSF_INLINE int32u Get4(int8u HowMany)
Definition: BitStream_Fast.h:265
Definition: BitStream_Fast.h:1136
BitStream_Fast_Mixed_Local LocalReader()
Definition: BitStream_Fast.h:1188
#define NULL
Definition: HTTPClientWrapper.h:98
Definition: BitStream_Fast.h:120
ZENLIB_BSF_INLINE int32s GetSE()
Definition: BitStream_Fast.h:314
Definition: BitStream.h:23
bool EmulationPrevention_Get() const
Definition: BitStream_Fast.h:232
Definition: BitStream_Fast.h:162
size_t Remain_Exact()
Definition: BitStream_Fast.h:400
int64u Peek8(int8u HowMany)
Definition: BitStream_Fast.h:364
size_t Remain() const
Definition: BitStream_Fast.h:384
BitStream_Fast_Local(BitStream_Fast &Owner_)
Definition: BitStream_Fast.h:1160
friend struct BitStream_Fast_EmulationPrevention_Calls< Kind >
Definition: BitStream_Fast.h:1079
BitStream_Fast()
Definition: BitStream_Fast.h:1103
ZENLIB_BSF_INLINE int64u Get8_Unchecked(int8u HowMany)
Definition: BitStream_Fast.h:294
size_t BitOffset_Get() const
Definition: BitStream_Fast.h:417
Definition: BitStream_Fast.h:154
BitStream_Fast_Local_Template & operator=(const BitStream_Fast_Local_Template &)=delete
Definition: BitStream_Fast.h:1157
size_t Offset_Get() const
Definition: BitStream_Fast.h:412
ZENLIB_BSF_INLINE int32u Get4_Unchecked(int8u HowMany)
Definition: BitStream_Fast.h:288
BitStream_Fast_EmulationPrevention_Local LocalReader()
Definition: BitStream_Fast.h:1183
~BitStream_Fast_Template()
Definition: BitStream_Fast.h:211
BitStream_Fast_EmulationPrevention(const int8u *Buffer_, size_t Size_, size_t ExtraBytesAvailable_=0)
Definition: BitStream_Fast.h:1115
Definition: BitStream_Fast.h:1100
void Sync()
Definition: BitStream_Fast.h:392
Definition: BitStream_Fast.h:1171
bitstream_fast_emulationprevention
Definition: BitStream_Fast.h:117
BitStream_Fast_Template()
Definition: BitStream_Fast.h:202
size_t OffsetBeforeLastCall_Get() const
Definition: BitStream_Fast.h:422
BitStream_Fast_EmulationPrevention()
Definition: BitStream_Fast.h:1114
BitStream_Fast_Mixed(const int8u *Buffer_, size_t Size_, size_t ExtraBytesAvailable_=0)
Definition: BitStream_Fast.h:1127
Definition: BitStream_Fast.h:1123
Definition: BitStream_Fast.h:119
ZENLIB_BSF_INLINE void Attach_EmulationPrevention(const int8u *Buffer_, size_t Size_, size_t ExtraBytesAvailable_=0, int8u ZerosBefore_=0)
Definition: BitStream_Fast.h:224
ZENLIB_BSF_INLINE int16u Peek2(int8u HowMany)
Definition: BitStream_Fast.h:354
ZENLIB_BSF_INLINE void Attach(const int8u *Buffer_, size_t Size_, size_t ExtraBytesAvailable_=0)
Definition: BitStream_Fast.h:215
BitStream_Fast_Local_Template(Reader &Owner_)
Definition: BitStream_Fast.h:1139
void Resize(size_t Size_)
Definition: BitStream_Fast.h:430
Definition: BitStream_Fast.h:121
BitStream_Fast_Template(const int8u *Buffer_, size_t Size_, size_t ExtraBytesAvailable_=0)
Definition: BitStream_Fast.h:210
ZENLIB_BSF_INLINE int32u Peek4(int8u HowMany)
Definition: BitStream_Fast.h:359
void Refresh()
Definition: BitStream_Fast.h:444
void Commit()
Definition: BitStream_Fast.h:1147
ZENLIB_BSF_INLINE int32u GetUE()
Definition: BitStream_Fast.h:303
Definition: BitStream_Fast.h:126
void Byte_Align()
Definition: BitStream_Fast.h:407
ZENLIB_BSF_INLINE bool PeekB()
Definition: BitStream_Fast.h:333
ZENLIB_BSF_INLINE int8u Get1(int8u HowMany)
Definition: BitStream_Fast.h:255
BitStream_Fast_Mixed_Local(BitStream_Fast_Mixed &Owner_)
Definition: BitStream_Fast.h:1174
BitStream_Fast_EmulationPrevention_Local(BitStream_Fast_EmulationPrevention &Owner_)
Definition: BitStream_Fast.h:1167
Definition: BitStream_Fast.h:1164
BitStream_Fast_Local_Template(BitStream_Fast_Local_Template &&Other)
Definition: BitStream_Fast.h:1141
Definition: BitStream_Fast.h:1111
#define ZENLIB_BSF_INLINE
Definition: BitStream_Fast.h:48
BitStream_Fast_Mixed()
Definition: BitStream_Fast.h:1126