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 class MEDIA_EXPORT BitReader { | |
24 public: | |
25 BitReader(); | |
26 BitReader(const uint8* data, off_t size); | |
27 virtual ~BitReader(); | |
28 | |
29 // Initialize the reader to start reading at |data|, |size| being size | |
30 // of |data| in bytes. | |
31 void Initialize(const uint8* data, off_t size); | |
32 | |
33 // Read |num_bits| next bits from stream and return in |*out|, first bit | |
34 // from the stream starting at |num_bits| position in |*out|. | |
35 // |num_bits| cannot be larger than the bits the type can hold. | |
36 // Return false if the given number of bits cannot be read (not enough | |
37 // bits in the stream), true otherwise. When return false, the stream will | |
38 // enter a state where further ReadBits operations will always return false | |
39 // unless |num_bits| is 0. The type |T| has to be a primitive integer type. | |
40 template<typename T> | |
41 bool ReadBits(int num_bits, T *out) { | |
42 DCHECK(num_bits <= static_cast<int>(sizeof(T) * CHAR_BIT)); | |
43 | |
44 *out = 0; | |
45 position_ += num_bits; | |
46 | |
47 while (num_remaining_bits_in_curr_byte_ != 0 && num_bits != 0) { | |
48 int bits_to_take = std::min(num_remaining_bits_in_curr_byte_, num_bits); | |
49 *out = (*out << bits_to_take) + | |
50 (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_to_take)); | |
51 num_bits -= bits_to_take; | |
52 num_remaining_bits_in_curr_byte_ -= bits_to_take; | |
53 curr_byte_ &= (1 << num_remaining_bits_in_curr_byte_) - 1; | |
Pawel Osciak
2012/07/16 21:27:09
Could you please explain why this more complicated
| |
54 | |
55 if (num_remaining_bits_in_curr_byte_ == 0) | |
56 UpdateCurrByte(); | |
57 } | |
58 | |
59 if (num_bits == 0) | |
60 return true; | |
61 | |
62 *out = 0; | |
63 num_remaining_bits_in_curr_byte_ = 0; | |
64 bytes_left_ = 0; | |
65 | |
66 return false; | |
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 |