OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
acolwell GONE FROM CHROMIUM
2014/01/11 00:24:37
s/2013/2014
damienv1
2014/01/13 22:41:06
Done.
| |
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_CORE_H_ | |
6 #define MEDIA_BASE_BIT_READER_CORE_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/logging.h" | |
10 #include "media/base/media_export.h" | |
11 | |
12 namespace media { | |
13 | |
14 class MEDIA_EXPORT BitReaderCore { | |
15 public: | |
16 class ByteStreamProvider { | |
17 public: | |
18 ByteStreamProvider(); | |
19 virtual ~ByteStreamProvider(); | |
20 | |
21 // Request n number of bytes where: | |
22 // - n must be less than or equal to |max_n|. | |
Pawel Osciak
2014/01/11 02:30:49
Perhaps instead something like below would be easi
damienv1
2014/01/13 22:41:06
Done.
BitReaderCore has a maximum capacity in ter
| |
23 // Return the actual number of bytes available in |*array|. | |
24 // Note: |*array| is ensured to be valid until the next call to GetBytes. | |
Pawel Osciak
2014/01/11 02:30:49
s/is ensured to be/has to remain/
damienv1
2014/01/13 22:41:06
Will fix the description.
| |
25 virtual int GetBytes(int max_n, const uint8** array) = 0; | |
26 }; | |
27 | |
28 // Lifetime of |byte_stream_provider| should be longer than BitReaderCore. | |
acolwell GONE FROM CHROMIUM
2014/01/11 00:24:37
nit: s/should/must/
damienv1
2014/01/13 22:41:06
Done.
| |
29 explicit BitReaderCore(ByteStreamProvider* byte_stream_provider); | |
Pawel Osciak
2014/01/11 02:30:49
Why not a scoped_ptr instead? I don't think anyone
damienv1
2014/01/13 22:41:06
Here is one of my previous comment that should ans
| |
30 ~BitReaderCore(); | |
Pawel Osciak
2014/01/11 02:30:49
virtual?
damienv1
2014/01/13 22:41:06
BitReaderCore is not meant to be subclassed so no
| |
31 | |
32 // Read a boolean from the stream and return in |*out|. | |
Pawel Osciak
2014/01/11 02:30:49
To be super correct we should say:
"Read one bit
damienv1
2014/01/13 22:41:06
I'll try to make the description more accurate.
| |
33 // Remark: do not use the template version for reading a bool | |
34 // since it generates some optimization warnings during compilation | |
35 // on windows platforms. | |
Pawel Osciak
2014/01/11 02:30:49
s/windows/Windows/
damienv1
2014/01/13 22:41:06
Will do.
| |
36 bool ReadBits(int num_bits, bool* out) { | |
Pawel Osciak
2014/01/11 02:30:49
Is the remark needed still since it's a specializa
damienv1
2014/01/13 22:41:06
- I think it's less error prone than what we used
| |
37 DCHECK_EQ(num_bits, 1); | |
38 return ReadFlag(out); | |
39 } | |
40 | |
41 // Read |num_bits| next bits from stream and return in |*out|, first bit | |
42 // from the stream starting at |num_bits| position in |*out|. | |
Pawel Osciak
2014/01/11 02:30:49
I feel this could be easier to understand (at leas
damienv1
2014/01/13 22:41:06
This description was the original description of B
| |
43 // |num_bits| cannot be larger than the bits the type can hold. | |
44 // Return false if the given number of bits cannot be read (not enough | |
45 // bits in the stream), true otherwise. When return false, the stream will | |
46 // enter a state where further ReadBits/SkipBits operations will always | |
47 // return false unless |num_bits| is 0. The type |T| has to be a primitive | |
48 // integer type. | |
Pawel Osciak
2014/01/11 02:30:49
I feel there should be a note what happens when T
damienv1
2014/01/13 22:41:06
Added a note in the comment to make it clear that
| |
49 // T can especially not be a boolean since it generates | |
acolwell GONE FROM CHROMIUM
2014/01/11 00:24:37
nit: I believe this comment is no longer necessary
damienv1
2014/01/13 22:41:06
Done.
| |
50 // some optimization warnings during compilation on windows platforms, | |
51 // use ReadFlag instead. | |
52 template<typename T> bool ReadBits(int num_bits, T* out) { | |
Pawel Osciak
2014/01/11 02:30:49
I feel all num_bits should be unsigned in this CL.
damienv1
2014/01/13 22:41:06
From the Chromium coding style, I feel the other w
| |
53 DCHECK_LE(num_bits, static_cast<int>(sizeof(T) * 8)); | |
54 uint64 temp; | |
55 bool ret = ReadBitsInternal(num_bits, &temp); | |
56 *out = static_cast<T>(temp); | |
57 return ret; | |
58 } | |
59 | |
60 // Read a boolean. | |
Pawel Osciak
2014/01/11 02:30:49
I'd suggest "Read one next bit and convert to bool
damienv1
2014/01/13 22:41:06
Done.
| |
61 bool ReadFlag(bool* flag); | |
62 | |
63 // Retrieve some bits without actually consuming them. | |
64 // Bits returned in |*out| are written from MSB to LSB. | |
acolwell GONE FROM CHROMIUM
2014/01/11 00:24:37
nit: I think the from MSB to LSB might be a little
damienv1
2014/01/13 22:41:06
Done.
| |
65 // Return the number of bits written in |out|. | |
66 // Note: if the number of remaining bits is less than |num_bits|, | |
67 // only the remaining bits are returned and in this case, the number | |
68 // of bits returned is less than |num_bits|. | |
69 int PeekBitsMsbAligned(int num_bits, uint64* out); | |
acolwell GONE FROM CHROMIUM
2014/01/11 00:24:37
nit: You might also want to note the guarantees ab
Pawel Osciak
2014/01/11 02:30:49
Nit again, but it feels a bit asymmetric to have R
damienv1
2014/01/13 22:41:06
Rephrased what is the meaning of |num_bits| in thi
damienv1
2014/01/13 22:41:06
It's asymmetric because they don't offer the same
| |
70 | |
71 // Skip |num_bits| next bits from stream. Return false if the given number of | |
72 // bits cannot be skipped (not enough bits in the stream), true otherwise. | |
73 // When return false, the stream will enter a state where further | |
74 // ReadBits/ReadFlag/SkipBits operations | |
75 // will always return false unless |num_bits| is 0. | |
76 bool SkipBits(int num_bits); | |
77 | |
78 // Returns the number of bits read so far. | |
79 int bits_read() const; | |
80 | |
81 // Returns the number of bits buffered in BitReaderCore. | |
Pawel Osciak
2014/01/11 02:30:49
Why is this useful for clients? Isn't this an impl
damienv1
2014/01/13 22:41:06
I rewrote BitReader::bits_available so that it doe
| |
82 int bits_buffered() const; | |
83 | |
84 private: | |
85 // Help function used by ReadBits to avoid inlining the bit reading logic. | |
86 bool ReadBitsInternal(int num_bits, uint64* out); | |
87 | |
88 // Refill bit registers to have at least |min_nbits| bits available. | |
89 // Return true if the mininimum bit count condition is met after the refill. | |
90 bool Refill(int min_nbits); | |
91 | |
92 // Refill the current bit register from the next bit register. | |
93 void RefillCurrentRegister(); | |
94 | |
95 ByteStreamProvider* const byte_stream_provider_; | |
96 | |
97 // Number of bits read so far. | |
98 int bits_read_; | |
99 | |
100 // Number of valid bits left in |reg|. | |
Pawel Osciak
2014/01/11 02:30:49
s/reg/reg_
damienv1
2014/01/13 22:41:06
Done.
| |
101 // Note: bits are consumed from MSB to LSB. | |
102 int nbits_; | |
Pawel Osciak
2014/01/11 02:30:49
Why do we need both bits_read_ and nbits_ ?
damienv1
2014/01/13 22:41:06
|bits_read| is the number of bits read so far (it
| |
103 uint64 reg_; | |
104 | |
105 // Number of valid bits left in |reg_next_|. | |
Pawel Osciak
2014/01/11 02:30:49
s/valid/unconsumed/
damienv1
2014/01/13 22:41:06
Done.
| |
106 // Note: bits are consumed from MSB to LSB. | |
107 int nbits_next_; | |
108 uint64 reg_next_; | |
109 | |
110 DISALLOW_COPY_AND_ASSIGN(BitReaderCore); | |
111 }; | |
112 | |
113 } // namespace media | |
114 | |
115 #endif // MEDIA_BASE_BIT_READER_CORE_H_ | |
OLD | NEW |