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