Shaka Packager SDK
bit_reader.cc
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 #include <packager/media/base/bit_reader.h>
6 
7 #include <algorithm>
8 
9 #include <absl/log/check.h>
10 
11 namespace shaka {
12 namespace media {
13 
14 BitReader::BitReader(const uint8_t* data, size_t size)
15  : data_(data),
16  initial_size_(size),
17  bytes_left_(size),
18  num_remaining_bits_in_curr_byte_(0) {
19  DCHECK(data_ != NULL && bytes_left_ > 0);
20 
21  UpdateCurrByte();
22 }
23 
24 BitReader::~BitReader() {}
25 
26 bool BitReader::SkipBits(size_t num_bits) {
27  // Skip any bits in the current byte waiting to be processed, then
28  // process full bytes until less than 8 bits remaining.
29  if (num_bits > num_remaining_bits_in_curr_byte_) {
30  num_bits -= num_remaining_bits_in_curr_byte_;
31  num_remaining_bits_in_curr_byte_ = 0;
32 
33  size_t num_bytes = num_bits / 8;
34  num_bits %= 8;
35  if (bytes_left_ < num_bytes) {
36  bytes_left_ = 0;
37  return false;
38  }
39  bytes_left_ -= num_bytes;
40  data_ += num_bytes;
41  UpdateCurrByte();
42 
43  // If there is no more data remaining, only return true if we
44  // skipped all that were requested.
45  if (num_remaining_bits_in_curr_byte_ == 0)
46  return (num_bits == 0);
47  }
48 
49  // Less than 8 bits remaining to skip. Use ReadBitsInternal to verify
50  // that the remaining bits we need exist, and adjust them as necessary
51  // for subsequent operations.
52  uint64_t not_needed;
53  return ReadBitsInternal(num_bits, &not_needed);
54 }
55 
57  // Already aligned.
58  if (num_remaining_bits_in_curr_byte_ == 8)
59  return;
60 
61  num_remaining_bits_in_curr_byte_ = 0;
62  UpdateCurrByte();
63 }
64 
65 bool BitReader::SkipBytes(size_t num_bytes) {
66  if (num_bytes == 0)
67  return true;
68  if (num_remaining_bits_in_curr_byte_ != 8)
69  return false;
70 
71  data_ += num_bytes - 1; // One additional byte in curr_byte_.
72  if (num_bytes > bytes_left_ + 1)
73  return false;
74  bytes_left_ -= num_bytes - 1;
75  num_remaining_bits_in_curr_byte_ = 0;
76  UpdateCurrByte();
77  return true;
78 }
79 
80 bool BitReader::ReadBitsInternal(size_t num_bits, uint64_t* out) {
81  DCHECK_LE(num_bits, 64u);
82 
83  *out = 0;
84 
85  while (num_remaining_bits_in_curr_byte_ != 0 && num_bits != 0) {
86  size_t bits_to_take = std::min(num_remaining_bits_in_curr_byte_, num_bits);
87 
88  *out <<= bits_to_take;
89  *out += curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_to_take);
90  num_bits -= bits_to_take;
91  num_remaining_bits_in_curr_byte_ -= bits_to_take;
92  curr_byte_ &= (1 << num_remaining_bits_in_curr_byte_) - 1;
93 
94  if (num_remaining_bits_in_curr_byte_ == 0)
95  UpdateCurrByte();
96  }
97 
98  return num_bits == 0;
99 }
100 
101 void BitReader::UpdateCurrByte() {
102  DCHECK_EQ(num_remaining_bits_in_curr_byte_, 0u);
103 
104  if (bytes_left_ == 0)
105  return;
106 
107  // Load a new byte and advance pointers.
108  curr_byte_ = *data_;
109  ++data_;
110  --bytes_left_;
111  num_remaining_bits_in_curr_byte_ = 8;
112 }
113 
114 } // namespace media
115 } // namespace shaka
BitReader(const uint8_t *data, size_t size)
Definition: bit_reader.cc:14
bool SkipBits(size_t num_bits)
Definition: bit_reader.cc:26
bool SkipBytes(size_t num_bytes)
Definition: bit_reader.cc:65
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66