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 
54 bool BitReader::SkipBytes(size_t num_bytes) {
55  if (num_remaining_bits_in_curr_byte_ != 8)
56  return false;
57  if (num_bytes == 0)
58  return true;
59 
60  data_ += num_bytes - 1; // One additional byte in curr_byte_.
61  if (num_bytes > bytes_left_ + 1)
62  return false;
63  bytes_left_ -= num_bytes - 1;
64  num_remaining_bits_in_curr_byte_ = 0;
65  UpdateCurrByte();
66  return true;
67 }
68 
69 bool BitReader::ReadBitsInternal(size_t num_bits, uint64_t* out) {
70  DCHECK_LE(num_bits, 64u);
71 
72  *out = 0;
73 
74  while (num_remaining_bits_in_curr_byte_ != 0 && num_bits != 0) {
75  size_t bits_to_take = std::min(num_remaining_bits_in_curr_byte_, num_bits);
76 
77  *out <<= bits_to_take;
78  *out += curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_to_take);
79  num_bits -= bits_to_take;
80  num_remaining_bits_in_curr_byte_ -= bits_to_take;
81  curr_byte_ &= (1 << num_remaining_bits_in_curr_byte_) - 1;
82 
83  if (num_remaining_bits_in_curr_byte_ == 0)
84  UpdateCurrByte();
85  }
86 
87  return num_bits == 0;
88 }
89 
90 void BitReader::UpdateCurrByte() {
91  DCHECK_EQ(num_remaining_bits_in_curr_byte_, 0u);
92 
93  if (bytes_left_ == 0)
94  return;
95 
96  // Load a new byte and advance pointers.
97  curr_byte_ = *data_;
98  ++data_;
99  --bytes_left_;
100  num_remaining_bits_in_curr_byte_ = 8;
101 }
102 
103 } // namespace media
104 } // namespace shaka
All the methods that are virtual are virtual for mocking.
bool SkipBits(size_t num_bits)
Definition: bit_reader.cc:24
BitReader(const uint8_t *data, size_t size)
Definition: bit_reader.cc:12
bool SkipBytes(size_t num_bytes)
Definition: bit_reader.cc:54