Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef MEDIA_BASE_BIT_READER_H_ |
michael@0 | 6 | #define MEDIA_BASE_BIT_READER_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include <sys/types.h> |
michael@0 | 9 | |
michael@0 | 10 | #include "mp4_demuxer/basictypes.h" |
michael@0 | 11 | |
michael@0 | 12 | namespace mp4_demuxer { |
michael@0 | 13 | |
michael@0 | 14 | // A class to read bit streams. |
michael@0 | 15 | class BitReader { |
michael@0 | 16 | public: |
michael@0 | 17 | // Initialize the reader to start reading at |data|, |size| being size |
michael@0 | 18 | // of |data| in bytes. |
michael@0 | 19 | BitReader(const uint8_t* data, off_t size); |
michael@0 | 20 | ~BitReader(); |
michael@0 | 21 | |
michael@0 | 22 | // Read |num_bits| next bits from stream and return in |*out|, first bit |
michael@0 | 23 | // from the stream starting at |num_bits| position in |*out|. |
michael@0 | 24 | // |num_bits| cannot be larger than the bits the type can hold. |
michael@0 | 25 | // Return false if the given number of bits cannot be read (not enough |
michael@0 | 26 | // bits in the stream), true otherwise. When return false, the stream will |
michael@0 | 27 | // enter a state where further ReadBits/SkipBits operations will always |
michael@0 | 28 | // return false unless |num_bits| is 0. The type |T| has to be a primitive |
michael@0 | 29 | // integer type. |
michael@0 | 30 | template<typename T> bool ReadBits(int num_bits, T *out) { |
michael@0 | 31 | DCHECK_LE(num_bits, static_cast<int>(sizeof(T) * 8)); |
michael@0 | 32 | uint64_t temp; |
michael@0 | 33 | bool ret = ReadBitsInternal(num_bits, &temp); |
michael@0 | 34 | *out = static_cast<T>(temp); |
michael@0 | 35 | return ret; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | // Returns the number of bits available for reading. |
michael@0 | 39 | int bits_available() const; |
michael@0 | 40 | |
michael@0 | 41 | private: |
michael@0 | 42 | // Help function used by ReadBits to avoid inlining the bit reading logic. |
michael@0 | 43 | bool ReadBitsInternal(int num_bits, uint64_t* out); |
michael@0 | 44 | |
michael@0 | 45 | // Advance to the next byte, loading it into curr_byte_. |
michael@0 | 46 | // If the num_remaining_bits_in_curr_byte_ is 0 after this function returns, |
michael@0 | 47 | // the stream has reached the end. |
michael@0 | 48 | void UpdateCurrByte(); |
michael@0 | 49 | |
michael@0 | 50 | // Pointer to the next unread (not in curr_byte_) byte in the stream. |
michael@0 | 51 | const uint8_t* data_; |
michael@0 | 52 | |
michael@0 | 53 | // Bytes left in the stream (without the curr_byte_). |
michael@0 | 54 | off_t bytes_left_; |
michael@0 | 55 | |
michael@0 | 56 | // Contents of the current byte; first unread bit starting at position |
michael@0 | 57 | // 8 - num_remaining_bits_in_curr_byte_ from MSB. |
michael@0 | 58 | uint8_t curr_byte_; |
michael@0 | 59 | |
michael@0 | 60 | // Number of bits remaining in curr_byte_ |
michael@0 | 61 | int num_remaining_bits_in_curr_byte_; |
michael@0 | 62 | |
michael@0 | 63 | private: |
michael@0 | 64 | DISALLOW_COPY_AND_ASSIGN(BitReader); |
michael@0 | 65 | }; |
michael@0 | 66 | |
michael@0 | 67 | } // namespace mp4_demuxer |
michael@0 | 68 | |
michael@0 | 69 | #endif // MEDIA_BASE_BIT_READER_H_ |