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.
|
|
|
|
|
|
|
|
#ifndef MEDIA_BASE_BIT_READER_H_
|
|
|
|
#define MEDIA_BASE_BIT_READER_H_
|
|
|
|
|
2014-09-30 23:52:58 +00:00
|
|
|
#include <stdint.h>
|
2013-09-24 01:35:40 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/logging.h"
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace edash_packager {
|
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.
|
2014-09-30 21:52:21 +00:00
|
|
|
BitReader(const uint8_t* data, off_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>
|
|
|
|
bool ReadBits(int num_bits, T* out) {
|
2013-09-24 01:35:40 +00:00
|
|
|
DCHECK_LE(num_bits, static_cast<int>(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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
/// stream will enter a state where further ReadBits/SkipBits
|
|
|
|
/// operations will always return false unless |num_bits| is 0.
|
2013-09-24 01:35:40 +00:00
|
|
|
bool SkipBits(int num_bits);
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @return The number of bits available for reading.
|
2013-09-24 01:35:40 +00:00
|
|
|
int bits_available() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Help function used by ReadBits to avoid inlining the bit reading logic.
|
2014-09-30 21:52:21 +00:00
|
|
|
bool ReadBitsInternal(int 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
|
|
|
|
|
|
|
// Bytes left in the stream (without the curr_byte_).
|
|
|
|
off_t bytes_left_;
|
|
|
|
|
|
|
|
// 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_
|
|
|
|
int num_remaining_bits_in_curr_byte_;
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(BitReader);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace media
|
2014-09-19 20:41:13 +00:00
|
|
|
} // namespace edash_packager
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
#endif // MEDIA_BASE_BIT_READER_H_
|