Shaka Player Embedded
buffer_reader.h
Go to the documentation of this file.
1 // Copyright 2017 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SHAKA_EMBEDDED_UTIL_BUFFER_READER_H_
16 #define SHAKA_EMBEDDED_UTIL_BUFFER_READER_H_
17 
18 #include <stddef.h>
19 #include <stdint.h>
20 
21 namespace shaka {
22 namespace util {
23 
24 enum Endianness {
27 
28 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
30 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
32 #else
33 # error "Invalid byte order"
34 #endif
35 };
36 
41 class BufferReader {
42  public:
43  BufferReader();
44  BufferReader(const uint8_t* data, size_t data_size);
45 
47  const uint8_t* data() const {
48  return data_;
49  }
50 
51  bool empty() const {
52  return size_ == 0;
53  }
54 
56  size_t BytesRemaining() const {
57  return size_;
58  }
59 
61  void SetBuffer(const uint8_t* data, size_t data_size);
62 
69  size_t Read(uint8_t* dest, size_t dest_size);
70 
75  size_t Skip(size_t count) {
76  return SkipBits(count * 8) / 8;
77  }
78 
83  size_t SkipBits(size_t count);
84 
89  uint8_t ReadUint8() {
90  return static_cast<uint8_t>(ReadBits(8, kBigEndian));
91  }
92 
94  uint32_t ReadUint32(Endianness endianness = kBigEndian) {
95  return static_cast<uint32_t>(ReadBits(32, endianness));
96  }
97 
105  uint64_t ReadBits(size_t count, Endianness endianness = kBigEndian);
106 
111  uint64_t ReadExpGolomb();
112 
113  private:
114  const uint8_t* data_;
115  size_t size_;
116  size_t bit_offset_;
117 };
118 
119 } // namespace util
120 } // namespace shaka
121 
122 #endif // SHAKA_EMBEDDED_UTIL_BUFFER_READER_H_
const char * dest
Definition: media_utils.cc:31
size_t Skip(size_t count)
Definition: buffer_reader.h:75
size_t SkipBits(size_t count)
uint32_t ReadUint32(Endianness endianness=kBigEndian)
Definition: buffer_reader.h:94
uint64_t ReadBits(size_t count, Endianness endianness=kBigEndian)
const uint8_t * data() const
Definition: buffer_reader.h:47
void SetBuffer(const uint8_t *data, size_t data_size)
size_t BytesRemaining() const
Definition: buffer_reader.h:56
size_t Read(uint8_t *dest, size_t dest_size)