• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BIT_READER_CORE_H_
6 #define BIT_READER_CORE_H_
7 
8 #include <stdint.h>
9 
10 #include "base/logging.h"
11 #include "base/macros.h"
12 
13 namespace media {
14 
15 class BitReaderCore {
16  public:
17   class ByteStreamProvider {
18    public:
19     ByteStreamProvider();
20     virtual ~ByteStreamProvider();
21 
22     // Consume at most the following |max_n| bytes of the stream
23     // and return the number n of bytes actually consumed.
24     // Set |*array| to point to a memory buffer containing those n bytes.
25     // Note: |*array| must be valid until the next call to GetBytes
26     // but there is no guarantee it is valid after.
27     virtual int GetBytes(int max_n, const uint8_t** array) = 0;
28   };
29 
30   // Lifetime of |byte_stream_provider| must be longer than BitReaderCore.
31   explicit BitReaderCore(ByteStreamProvider* byte_stream_provider);
32   ~BitReaderCore();
33 
34   // Read one bit from the stream and return it as a boolean in |*out|.
35   // Remark: we do not use the template version for reading a bool
36   // since it generates some optimization warnings during compilation
37   // on Windows platforms.
ReadBits(int num_bits,bool * out)38   bool ReadBits(int num_bits, bool* out) {
39     DCHECK_EQ(num_bits, 1);
40     return ReadFlag(out);
41   }
42 
43   // Read |num_bits| next bits from stream and return in |*out|, first bit
44   // from the stream starting at |num_bits| position in |*out|,
45   // bits of |*out| whose position is strictly greater than |num_bits|
46   // are all set to zero.
47   // Notes:
48   // - |num_bits| cannot be larger than the bits the type can hold.
49   // - From the above description, passing a signed type in |T| does not
50   //   mean the first bit read from the stream gives the sign of the value.
51   // Return false if the given number of bits cannot be read (not enough
52   // bits in the stream), true otherwise. When return false, the stream will
53   // enter a state where further ReadBits/SkipBits operations will always
54   // return false unless |num_bits| is 0. The type |T| has to be a primitive
55   // integer type.
ReadBits(int num_bits,T * out)56   template<typename T> bool ReadBits(int num_bits, T* out) {
57     DCHECK_LE(num_bits, static_cast<int>(sizeof(T) * 8));
58     uint64_t temp;
59     bool ret = ReadBitsInternal(num_bits, &temp);
60     *out = static_cast<T>(temp);
61     return ret;
62   }
63 
64   // Read one bit from the stream and return it as a boolean in |*flag|.
65   bool ReadFlag(bool* flag);
66 
67   // Retrieve some bits without actually consuming them.
68   // Bits returned in |*out| are shifted so the most significant bit contains
69   // the next bit that can be read from the stream.
70   // Return the number of bits actually written in |out|.
71   // Note: |num_bits| is just a suggestion of how many bits the caller
72   // wish to get in |*out| and must be less than 64:
73   // - The number of bits returned can be more than |num_bits|.
74   // - However, it will be strictly less than |num_bits|
75   //   if and only if there are not enough bits left in the stream.
76   int PeekBitsMsbAligned(int num_bits, uint64_t* out);
77 
78   // Skip |num_bits| next bits from stream. Return false if the given number of
79   // bits cannot be skipped (not enough bits in the stream), true otherwise.
80   // When return false, the stream will enter a state where further
81   // ReadBits/ReadFlag/SkipBits operations
82   // will always return false unless |num_bits| is 0.
83   bool SkipBits(int num_bits);
84 
85   // Returns the number of bits read so far.
86   int bits_read() const;
87 
88  private:
89   // This function can skip any number of bits but is more efficient
90   // for small numbers. Return false if the given number of bits cannot be
91   // skipped (not enough bits in the stream), true otherwise.
92   bool SkipBitsSmall(int num_bits);
93 
94   // Help function used by ReadBits to avoid inlining the bit reading logic.
95   bool ReadBitsInternal(int num_bits, uint64_t* out);
96 
97   // Refill bit registers to have at least |min_nbits| bits available.
98   // Return true if the mininimum bit count condition is met after the refill.
99   bool Refill(int min_nbits);
100 
101   // Refill the current bit register from the next bit register.
102   void RefillCurrentRegister();
103 
104   ByteStreamProvider* const byte_stream_provider_;
105 
106   // Number of bits read so far.
107   int bits_read_;
108 
109   // Number of bits in |reg_| that have not been consumed yet.
110   // Note: bits are consumed from MSB to LSB.
111   int nbits_;
112   uint64_t reg_;
113 
114   // Number of bits in |reg_next_| that have not been consumed yet.
115   // Note: bits are consumed from MSB to LSB.
116   int nbits_next_;
117   uint64_t reg_next_;
118 
119   DISALLOW_COPY_AND_ASSIGN(BitReaderCore);
120 };
121 
122 }  // namespace media
123 
124 #endif  // BIT_READER_CORE_H_
125