Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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 | |
| 5 #ifndef MEDIA_BASE_BIT_READER_H_ | |
| 6 #define MEDIA_BASE_BIT_READER_H_ | |
| 7 | |
| 8 #include <sys/types.h> | |
| 9 #include <algorithm> | |
| 10 #include <climits> | |
| 11 | |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "media/base/media_export.h" | |
| 15 | |
| 16 | |
| 17 namespace media { | |
| 18 | |
| 19 // A class to read bit streams. | |
| 20 // Classes inherited this class can override its UpdateCurrByte function | |
| 21 // to support specific escape mechanism, which is widely used by streaming | |
| 22 // formats like H.264 Annex B. | |
| 23 // TODO(posciak): need separate unittests for this class. | |
|
acolwell GONE FROM CHROMIUM
2012/07/03 01:00:12
nit: Can we remove this TODO since you have create
| |
| 24 class MEDIA_EXPORT BitReader { | |
| 25 public: | |
| 26 BitReader(); | |
| 27 BitReader(const uint8* data, off_t size); | |
| 28 virtual ~BitReader(); | |
| 29 | |
| 30 // Initialize the reader to start reading at |data|, |size| being size | |
| 31 // of |data| in bytes. | |
| 32 void Initialize(const uint8* data, off_t size); | |
| 33 | |
| 34 // Read |num_bits| next bits from stream and return in |*out|, first bit | |
| 35 // from the stream starting at |num_bits| position in |*out|. | |
| 36 // |num_bits| cannot be larger than the bits the type can hold. | |
| 37 // Return false if the given number of bits cannot be read (not enough | |
| 38 // bits in the stream), true otherwise. When return false, the stream will | |
| 39 // enter a status that further ReadBits operations will always return false | |
|
acolwell GONE FROM CHROMIUM
2012/07/03 01:00:12
nit: status that -> state where
| |
| 40 // unless |num_bits| is 0. The type |T| has to be a primitive integer type. | |
| 41 template<typename T> | |
| 42 bool ReadBits(int num_bits, T *out) { | |
| 43 DCHECK(num_bits <= static_cast<int>(sizeof(T) * CHAR_BIT)); | |
| 44 | |
| 45 *out = 0; | |
| 46 position_ += num_bits; | |
| 47 | |
| 48 while (num_remaining_bits_in_curr_byte_ != 0 && num_bits != 0) { | |
| 49 int bits_to_take = std::min(num_remaining_bits_in_curr_byte_, num_bits); | |
| 50 *out = (*out << bits_to_take) + | |
| 51 (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_to_take)); | |
| 52 num_bits -= bits_to_take; | |
| 53 num_remaining_bits_in_curr_byte_ -= bits_to_take; | |
| 54 curr_byte_ &= (1 << num_remaining_bits_in_curr_byte_) - 1; | |
| 55 | |
| 56 if (num_remaining_bits_in_curr_byte_ == 0) | |
| 57 UpdateCurrByte(); | |
| 58 } | |
| 59 | |
| 60 if (num_bits != 0) { | |
|
acolwell GONE FROM CHROMIUM
2012/07/03 01:00:12
nit: Reverse check and return early
| |
| 61 *out = 0; | |
| 62 num_remaining_bits_in_curr_byte_ = 0; | |
| 63 bytes_left_ = 0; | |
| 64 } | |
| 65 | |
| 66 return num_bits == 0; | |
| 67 } | |
| 68 | |
| 69 bool SkipBits(int num_bits); | |
| 70 | |
| 71 // Return the current position in the stream in unit of bit. | |
| 72 // This includes the skipped escape bytes if there are any. | |
| 73 off_t Tell() const; | |
| 74 | |
| 75 // Return the number of bits left in the stream. | |
| 76 // This doesn't take any escape sequence into account. | |
| 77 off_t NumBitsLeft() const; | |
| 78 | |
| 79 bool HasMoreData() const; | |
| 80 | |
| 81 protected: | |
| 82 // Advance to the next byte, loading it into curr_byte_. | |
| 83 // This function can be overridden to support specific escape mechanism. | |
| 84 // If the num_remaining_bits_in_curr_byte_ is 0 after this function returns, | |
| 85 // the stream has reached the end. | |
| 86 virtual void UpdateCurrByte(); | |
| 87 | |
| 88 // Pointer to the next unread (not in curr_byte_) byte in the stream. | |
| 89 const uint8* data_; | |
| 90 | |
| 91 // Bytes left in the stream (without the curr_byte_). | |
| 92 off_t bytes_left_; | |
| 93 | |
| 94 // Current position in bits. | |
| 95 off_t position_; | |
| 96 | |
| 97 // Contents of the current byte; first unread bit starting at position | |
| 98 // 8 - num_remaining_bits_in_curr_byte_ from MSB. | |
| 99 uint8 curr_byte_; | |
| 100 | |
| 101 // Number of bits remaining in curr_byte_ | |
| 102 int num_remaining_bits_in_curr_byte_; | |
| 103 | |
| 104 private: | |
| 105 DISALLOW_COPY_AND_ASSIGN(BitReader); | |
| 106 }; | |
| 107 | |
| 108 } // namespace media | |
| 109 | |
| 110 #endif // MEDIA_BASE_BIT_READER_H_ | |
| OLD | NEW |