bit_reader_core.h 4.7 KB

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