Check existence of Widevince CDM/Adapter on load failure on Win.
[chromium-blink-merge.git] / media / base / bit_reader_core.h
blob6f92d175643e2e0cce1aab9b894d59ede5078768
1 // Copyright 2014 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.
5 #ifndef MEDIA_BASE_BIT_READER_CORE_H_
6 #define MEDIA_BASE_BIT_READER_CORE_H_
8 #include "base/basictypes.h"
9 #include "base/logging.h"
10 #include "media/base/media_export.h"
12 namespace media {
14 class MEDIA_EXPORT BitReaderCore {
15 public:
16 class ByteStreamProvider {
17 public:
18 ByteStreamProvider();
19 virtual ~ByteStreamProvider();
21 // Consume at most the following |max_n| bytes of the stream
22 // and return the number n of bytes actually consumed.
23 // Set |*array| to point to a memory buffer containing those n bytes.
24 // Note: |*array| must be valid until the next call to GetBytes
25 // but there is no guarantee it is valid after.
26 virtual int GetBytes(int max_n, const uint8** array) = 0;
29 // Lifetime of |byte_stream_provider| must be longer than BitReaderCore.
30 explicit BitReaderCore(ByteStreamProvider* byte_stream_provider);
31 ~BitReaderCore();
33 // Read one bit from the stream and return it as a boolean in |*out|.
34 // Remark: we do not use the template version for reading a bool
35 // since it generates some optimization warnings during compilation
36 // on Windows platforms.
37 bool ReadBits(int num_bits, bool* out) {
38 DCHECK_EQ(num_bits, 1);
39 return ReadFlag(out);
42 // Read |num_bits| next bits from stream and return in |*out|, first bit
43 // from the stream starting at |num_bits| position in |*out|,
44 // bits of |*out| whose position is strictly greater than |num_bits|
45 // are all set to zero.
46 // Notes:
47 // - |num_bits| cannot be larger than the bits the type can hold.
48 // - From the above description, passing a signed type in |T| does not
49 // mean the first bit read from the stream gives the sign of the value.
50 // Return false if the given number of bits cannot be read (not enough
51 // bits in the stream), true otherwise. When return false, the stream will
52 // enter a state where further ReadBits/SkipBits operations will always
53 // return false unless |num_bits| is 0. The type |T| has to be a primitive
54 // integer type.
55 template<typename T> bool ReadBits(int num_bits, T* out) {
56 DCHECK_LE(num_bits, static_cast<int>(sizeof(T) * 8));
57 uint64 temp;
58 bool ret = ReadBitsInternal(num_bits, &temp);
59 *out = static_cast<T>(temp);
60 return ret;
63 // Read one bit from the stream and return it as a boolean in |*flag|.
64 bool ReadFlag(bool* flag);
66 // Retrieve some bits without actually consuming them.
67 // Bits returned in |*out| are shifted so the most significant bit contains
68 // the next bit that can be read from the stream.
69 // Return the number of bits actually written in |out|.
70 // Note: |num_bits| is just a suggestion of how many bits the caller
71 // wish to get in |*out| and must be less than 64:
72 // - The number of bits returned can be more than |num_bits|.
73 // - However, it will be strictly less than |num_bits|
74 // if and only if there are not enough bits left in the stream.
75 int PeekBitsMsbAligned(int num_bits, uint64* out);
77 // Skip |num_bits| next bits from stream. Return false if the given number of
78 // bits cannot be skipped (not enough bits in the stream), true otherwise.
79 // When return false, the stream will enter a state where further
80 // ReadBits/ReadFlag/SkipBits operations
81 // will always return false unless |num_bits| is 0.
82 bool SkipBits(int num_bits);
84 // Returns the number of bits read so far.
85 int bits_read() const;
87 private:
88 // Help function used by ReadBits to avoid inlining the bit reading logic.
89 bool ReadBitsInternal(int num_bits, uint64* out);
91 // Refill bit registers to have at least |min_nbits| bits available.
92 // Return true if the mininimum bit count condition is met after the refill.
93 bool Refill(int min_nbits);
95 // Refill the current bit register from the next bit register.
96 void RefillCurrentRegister();
98 ByteStreamProvider* const byte_stream_provider_;
100 // Number of bits read so far.
101 int bits_read_;
103 // Number of bits in |reg_| that have not been consumed yet.
104 // Note: bits are consumed from MSB to LSB.
105 int nbits_;
106 uint64 reg_;
108 // Number of bits in |reg_next_| that have not been consumed yet.
109 // Note: bits are consumed from MSB to LSB.
110 int nbits_next_;
111 uint64 reg_next_;
113 DISALLOW_COPY_AND_ASSIGN(BitReaderCore);
116 } // namespace media
118 #endif // MEDIA_BASE_BIT_READER_CORE_H_