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