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