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