Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(657)

Unified Diff: media/base/bit_reader_core.h

Issue 112343011: Split the bit reader functionalities from the byte stream provider. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Modify ReadUE function prototype. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: media/base/bit_reader_core.h
diff --git a/media/base/bit_reader_core.h b/media/base/bit_reader_core.h
new file mode 100644
index 0000000000000000000000000000000000000000..1fa100eb589cc69da7f3a45226b36c8f6033c4ab
--- /dev/null
+++ b/media/base/bit_reader_core.h
@@ -0,0 +1,115 @@
+// 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.
+// 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_CORE_H_
+#define MEDIA_BASE_BIT_READER_CORE_H_
+
+#include "base/basictypes.h"
+#include "base/logging.h"
+#include "media/base/media_export.h"
+
+namespace media {
+
+class MEDIA_EXPORT BitReaderCore {
+ public:
+ class ByteStreamProvider {
+ public:
+ ByteStreamProvider();
+ virtual ~ByteStreamProvider();
+
+ // Request n number of bytes where:
+ // - 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
+ // Return the actual number of bytes available in |*array|.
+ // 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.
+ virtual int GetBytes(int max_n, const uint8** array) = 0;
+ };
+
+ // 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.
+ 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
+ ~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
+
+ // 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.
+ // Remark: do not use the template version for reading a bool
+ // since it generates some optimization warnings during compilation
+ // on windows platforms.
Pawel Osciak 2014/01/11 02:30:49 s/windows/Windows/
damienv1 2014/01/13 22:41:06 Will do.
+ 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
+ DCHECK_EQ(num_bits, 1);
+ return ReadFlag(out);
+ }
+
+ // Read |num_bits| next bits from stream and return in |*out|, first bit
+ // 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
+ // |num_bits| cannot be larger than the bits the type can hold.
+ // Return false if the given number of bits cannot be read (not enough
+ // bits in the stream), true otherwise. When return false, the stream will
+ // enter a state where further ReadBits/SkipBits operations will always
+ // return false unless |num_bits| is 0. The type |T| has to be a primitive
+ // 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
+ // 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.
+ // some optimization warnings during compilation on windows platforms,
+ // use ReadFlag instead.
+ 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
+ DCHECK_LE(num_bits, static_cast<int>(sizeof(T) * 8));
+ uint64 temp;
+ bool ret = ReadBitsInternal(num_bits, &temp);
+ *out = static_cast<T>(temp);
+ return ret;
+ }
+
+ // 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.
+ bool ReadFlag(bool* flag);
+
+ // Retrieve some bits without actually consuming them.
+ // 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.
+ // Return the number of bits written in |out|.
+ // Note: if the number of remaining bits is less than |num_bits|,
+ // only the remaining bits are returned and in this case, the number
+ // of bits returned is less than |num_bits|.
+ 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
+
+ // Skip |num_bits| next bits from stream. Return false if the given number of
+ // bits cannot be skipped (not enough bits in the stream), true otherwise.
+ // When return false, the stream will enter a state where further
+ // ReadBits/ReadFlag/SkipBits operations
+ // will always return false unless |num_bits| is 0.
+ bool SkipBits(int num_bits);
+
+ // Returns the number of bits read so far.
+ int bits_read() const;
+
+ // 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
+ int bits_buffered() const;
+
+ private:
+ // Help function used by ReadBits to avoid inlining the bit reading logic.
+ bool ReadBitsInternal(int num_bits, uint64* out);
+
+ // Refill bit registers to have at least |min_nbits| bits available.
+ // Return true if the mininimum bit count condition is met after the refill.
+ bool Refill(int min_nbits);
+
+ // Refill the current bit register from the next bit register.
+ void RefillCurrentRegister();
+
+ ByteStreamProvider* const byte_stream_provider_;
+
+ // Number of bits read so far.
+ int bits_read_;
+
+ // 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.
+ // Note: bits are consumed from MSB to LSB.
+ 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
+ uint64 reg_;
+
+ // 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.
+ // Note: bits are consumed from MSB to LSB.
+ int nbits_next_;
+ uint64 reg_next_;
+
+ DISALLOW_COPY_AND_ASSIGN(BitReaderCore);
+};
+
+} // namespace media
+
+#endif // MEDIA_BASE_BIT_READER_CORE_H_

Powered by Google App Engine
This is Rietveld 408576698