2013-09-24 01:35:40 +00:00
|
|
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#ifndef PACKAGER_MEDIA_BASE_BIT_READER_H_
|
|
|
|
#define PACKAGER_MEDIA_BASE_BIT_READER_H_
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-09-30 23:52:58 +00:00
|
|
|
#include <stdint.h>
|
2013-09-24 01:35:40 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
#include "glog/logging.h"
|
|
|
|
#include "packager/macros.h"
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-09-24 01:35:40 +00:00
|
|
|
namespace media {
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// A class to read bit streams.
|
2013-09-24 04:17:12 +00:00
|
|
|
class BitReader {
|
2013-09-24 01:35:40 +00:00
|
|
|
public:
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Initialize the BitReader object to read a data buffer.
|
|
|
|
/// @param data points to the beginning of the buffer.
|
|
|
|
/// @param size is the buffer size in bytes.
|
2016-11-09 02:11:13 +00:00
|
|
|
BitReader(const uint8_t* data, size_t size);
|
2013-09-24 01:35:40 +00:00
|
|
|
~BitReader();
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Read a number of bits from stream.
|
|
|
|
/// @param num_bits specifies the number of bits to read. It cannot be larger
|
|
|
|
/// than the number of bits the type can hold.
|
|
|
|
/// @param[out] out stores the output. The type @b T has to be a primitive
|
|
|
|
/// integer type.
|
|
|
|
/// @return false if the given number of bits cannot be read (not enough
|
|
|
|
/// bits in the stream), true otherwise. When false is returned, the
|
|
|
|
/// stream will enter a state where further ReadBits/SkipBits
|
|
|
|
/// operations will always return false unless @a num_bits is 0.
|
2014-09-30 23:52:58 +00:00
|
|
|
template <typename T>
|
2016-11-09 02:11:13 +00:00
|
|
|
bool ReadBits(size_t num_bits, T* out) {
|
|
|
|
DCHECK_LE(num_bits, sizeof(T) * 8);
|
2014-09-30 21:52:21 +00:00
|
|
|
uint64_t temp;
|
2013-09-24 01:35:40 +00:00
|
|
|
bool ret = ReadBitsInternal(num_bits, &temp);
|
|
|
|
*out = static_cast<T>(temp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-08-14 22:28:21 +00:00
|
|
|
// Explicit T=bool overload to make MSVC happy.
|
2016-11-09 02:11:13 +00:00
|
|
|
bool ReadBits(size_t num_bits, bool* out) {
|
|
|
|
DCHECK_EQ(num_bits, 1u);
|
2016-08-14 22:28:21 +00:00
|
|
|
uint64_t temp;
|
|
|
|
bool ret = ReadBitsInternal(num_bits, &temp);
|
|
|
|
*out = temp != 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Skip a number of bits from stream.
|
|
|
|
/// @param num_bits specifies the number of bits to be skipped.
|
|
|
|
/// @return false if the given number of bits cannot be skipped (not enough
|
|
|
|
/// bits in the stream), true otherwise. When false is returned, the
|
2015-11-20 19:28:45 +00:00
|
|
|
/// stream will enter a state where further ReadXXX/SkipXXX
|
|
|
|
/// operations will always return false unless |num_bits/bytes| is 0.
|
2016-11-09 02:11:13 +00:00
|
|
|
bool SkipBits(size_t num_bits);
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2015-11-20 19:28:45 +00:00
|
|
|
/// Read one bit then skip the number of bits specified if that bit matches @a
|
|
|
|
/// condition.
|
|
|
|
/// @param condition indicates when the number of bits should be skipped.
|
|
|
|
/// @param num_bits specifies the number of bits to be skipped.
|
|
|
|
/// @return false if the one bit cannot be read (not enough bits in the
|
|
|
|
/// stream) or if the bit is set but the given number of bits cannot
|
|
|
|
/// be skipped (not enough bits in the stream), true otherwise. When
|
|
|
|
/// false is returned, the stream will enter a state where further
|
|
|
|
/// ReadXXX/SkipXXX operations will always return false.
|
2016-11-09 02:11:13 +00:00
|
|
|
bool SkipBitsConditional(bool condition, size_t num_bits) {
|
2015-11-20 19:28:45 +00:00
|
|
|
bool condition_read = true;
|
|
|
|
if (!ReadBits(1, &condition_read))
|
|
|
|
return false;
|
|
|
|
return condition_read == condition ? SkipBits(num_bits) : true;
|
|
|
|
}
|
|
|
|
|
2018-04-27 22:15:50 +00:00
|
|
|
/// Skip a number of bits so the stream is byte aligned to the initial data.
|
|
|
|
/// There could be 0 to 7 bits skipped.
|
|
|
|
void SkipToNextByte();
|
|
|
|
|
2015-11-20 19:28:45 +00:00
|
|
|
/// Skip a number of bytes from stream. The current posision should be byte
|
|
|
|
/// aligned, otherwise a false is returned and bytes are not skipped.
|
|
|
|
/// @param num_bytes specifies the number of bytes to be skipped.
|
|
|
|
/// @return false if the current position is not byte aligned or if the given
|
|
|
|
/// number of bytes cannot be skipped (not enough bytes in the
|
|
|
|
/// stream), true otherwise.
|
2016-11-09 02:11:13 +00:00
|
|
|
bool SkipBytes(size_t num_bytes);
|
2015-11-20 19:28:45 +00:00
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @return The number of bits available for reading.
|
2016-11-09 02:11:13 +00:00
|
|
|
size_t bits_available() const {
|
2015-11-18 19:51:15 +00:00
|
|
|
return 8 * bytes_left_ + num_remaining_bits_in_curr_byte_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @return The current bit position.
|
2016-11-09 02:11:13 +00:00
|
|
|
size_t bit_position() const { return 8 * initial_size_ - bits_available(); }
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2020-12-11 20:58:26 +00:00
|
|
|
/// @return A pointer to the current byte.
|
|
|
|
const uint8_t* current_byte_ptr() const { return data_ - 1; }
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
private:
|
|
|
|
// Help function used by ReadBits to avoid inlining the bit reading logic.
|
2016-11-09 02:11:13 +00:00
|
|
|
bool ReadBitsInternal(size_t num_bits, uint64_t* out);
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
// Advance to the next byte, loading it into curr_byte_.
|
|
|
|
// If the num_remaining_bits_in_curr_byte_ is 0 after this function returns,
|
|
|
|
// the stream has reached the end.
|
|
|
|
void UpdateCurrByte();
|
|
|
|
|
|
|
|
// Pointer to the next unread (not in curr_byte_) byte in the stream.
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t* data_;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2015-11-18 19:51:15 +00:00
|
|
|
// Initial size of the input data.
|
2016-11-09 02:11:13 +00:00
|
|
|
size_t initial_size_;
|
2015-11-18 19:51:15 +00:00
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
// Bytes left in the stream (without the curr_byte_).
|
2016-11-09 02:11:13 +00:00
|
|
|
size_t bytes_left_;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
// Contents of the current byte; first unread bit starting at position
|
|
|
|
// 8 - num_remaining_bits_in_curr_byte_ from MSB.
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t curr_byte_;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
// Number of bits remaining in curr_byte_
|
2016-11-09 02:11:13 +00:00
|
|
|
size_t num_remaining_bits_in_curr_byte_;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(BitReader);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#endif // PACKAGER_MEDIA_BASE_BIT_READER_H_
|