• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The libgav1 Authors
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 //      http://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 #include "src/utils/raw_bit_reader.h"
16 
17 #include <cassert>
18 #include <limits>
19 
20 #include "src/utils/common.h"
21 #include "src/utils/logging.h"
22 
23 // Note <cinttypes> is only needed when logging is enabled (for the PRI*
24 // macros). It depends on the definition of LIBGAV1_ENABLE_LOGGING from
25 // logging.h, thus the non-standard header ordering.
26 #if LIBGAV1_ENABLE_LOGGING
27 #include <cinttypes>
28 #endif
29 
30 namespace libgav1 {
31 namespace {
32 
33 constexpr int kMaximumLeb128Size = 8;
34 constexpr uint8_t kLeb128ValueByteMask = 0x7f;
35 constexpr uint8_t kLeb128TerminationByteMask = 0x80;
36 
Mod8(size_t n)37 uint8_t Mod8(size_t n) {
38   // Last 3 bits are the value of mod 8.
39   return n & 0x07;
40 }
41 
DivideBy8(size_t n,bool ceil)42 size_t DivideBy8(size_t n, bool ceil) { return (n + (ceil ? 7 : 0)) >> 3; }
43 
44 }  // namespace
45 
RawBitReader(const uint8_t * data,size_t size)46 RawBitReader::RawBitReader(const uint8_t* data, size_t size)
47     : data_(data), bit_offset_(0), size_(size) {
48   assert(data_ != nullptr || size_ == 0);
49 }
50 
ReadBitImpl()51 int RawBitReader::ReadBitImpl() {
52   const size_t byte_offset = DivideBy8(bit_offset_, false);
53   const uint8_t byte = data_[byte_offset];
54   const uint8_t shift = 7 - Mod8(bit_offset_);
55   ++bit_offset_;
56   return static_cast<int>((byte >> shift) & 0x01);
57 }
58 
ReadBit()59 int RawBitReader::ReadBit() {
60   if (Finished()) return -1;
61   return ReadBitImpl();
62 }
63 
ReadLiteral(int num_bits)64 int64_t RawBitReader::ReadLiteral(int num_bits) {
65   assert(num_bits <= 32);
66   if (!CanReadLiteral(num_bits)) return -1;
67   assert(num_bits > 0);
68   uint32_t literal = 0;
69   int bit = num_bits - 1;
70   do {
71     // ARM can combine a shift operation with a constant number of bits with
72     // some other operations, such as the OR operation.
73     // Here is an ARM disassembly example:
74     // orr w1, w0, w1, lsl #1
75     // which left shifts register w1 by 1 bit and OR the shift result with
76     // register w0.
77     // The next 2 lines are equivalent to:
78     // literal |= static_cast<uint32_t>(ReadBitImpl()) << bit;
79     literal <<= 1;
80     literal |= static_cast<uint32_t>(ReadBitImpl());
81   } while (--bit >= 0);
82   return literal;
83 }
84 
ReadInverseSignedLiteral(int num_bits,int * const value)85 bool RawBitReader::ReadInverseSignedLiteral(int num_bits, int* const value) {
86   assert(num_bits + 1 < 32);
87   *value = static_cast<int>(ReadLiteral(num_bits + 1));
88   if (*value == -1) return false;
89   const int sign_bit = 1 << num_bits;
90   if ((*value & sign_bit) != 0) {
91     *value -= 2 * sign_bit;
92   }
93   return true;
94 }
95 
ReadLittleEndian(int num_bytes,size_t * const value)96 bool RawBitReader::ReadLittleEndian(int num_bytes, size_t* const value) {
97   // We must be at a byte boundary.
98   assert(Mod8(bit_offset_) == 0);
99   assert(num_bytes <= 4);
100   static_assert(sizeof(size_t) >= 4, "");
101   if (value == nullptr) return false;
102   size_t byte_offset = DivideBy8(bit_offset_, false);
103   if (Finished() || byte_offset + num_bytes > size_) {
104     LIBGAV1_DLOG(ERROR, "Not enough bits to read Little Endian value.");
105     return false;
106   }
107   *value = 0;
108   for (int i = 0; i < num_bytes; ++i) {
109     const size_t byte = data_[byte_offset];
110     *value |= (byte << (i * 8));
111     ++byte_offset;
112   }
113   bit_offset_ = byte_offset * 8;
114   return true;
115 }
116 
ReadUnsignedLeb128(size_t * const value)117 bool RawBitReader::ReadUnsignedLeb128(size_t* const value) {
118   // We must be at a byte boundary.
119   assert(Mod8(bit_offset_) == 0);
120   if (value == nullptr) return false;
121   uint64_t value64 = 0;
122   for (int i = 0; i < kMaximumLeb128Size; ++i) {
123     if (Finished()) {
124       LIBGAV1_DLOG(ERROR, "Not enough bits to read LEB128 value.");
125       return false;
126     }
127     const size_t byte_offset = DivideBy8(bit_offset_, false);
128     const uint8_t byte = data_[byte_offset];
129     bit_offset_ += 8;
130     value64 |= static_cast<uint64_t>(byte & kLeb128ValueByteMask) << (i * 7);
131     if ((byte & kLeb128TerminationByteMask) == 0) {
132       if (value64 != static_cast<size_t>(value64) ||
133           value64 > std::numeric_limits<uint32_t>::max()) {
134         LIBGAV1_DLOG(
135             ERROR, "LEB128 value (%" PRIu64 ") exceeded uint32_t maximum (%u).",
136             value64, std::numeric_limits<uint32_t>::max());
137         return false;
138       }
139       *value = static_cast<size_t>(value64);
140       return true;
141     }
142   }
143   LIBGAV1_DLOG(
144       ERROR,
145       "Exceeded kMaximumLeb128Size (%d) when trying to read LEB128 value",
146       kMaximumLeb128Size);
147   return false;
148 }
149 
ReadUvlc(uint32_t * const value)150 bool RawBitReader::ReadUvlc(uint32_t* const value) {
151   if (value == nullptr) return false;
152   int leading_zeros = 0;
153   while (true) {
154     const int bit = ReadBit();
155     if (bit == -1) {
156       LIBGAV1_DLOG(ERROR, "Not enough bits to read uvlc value.");
157       return false;
158     }
159     if (bit == 1) break;
160     ++leading_zeros;
161     if (leading_zeros == 32) {
162       LIBGAV1_DLOG(ERROR,
163                    "Exceeded maximum size (32) when trying to read uvlc value");
164       return false;
165     }
166   }
167   int literal;
168   if (leading_zeros != 0) {
169     literal = static_cast<int>(ReadLiteral(leading_zeros));
170     if (literal == -1) {
171       LIBGAV1_DLOG(ERROR, "Not enough bits to read uvlc value.");
172       return false;
173     }
174     literal += (1U << leading_zeros) - 1;
175   } else {
176     literal = 0;
177   }
178   *value = literal;
179   return true;
180 }
181 
AlignToNextByte()182 bool RawBitReader::AlignToNextByte() {
183   while ((bit_offset_ & 7) != 0) {
184     if (ReadBit() != 0) {
185       return false;
186     }
187   }
188   return true;
189 }
190 
VerifyAndSkipTrailingBits(size_t num_bits)191 bool RawBitReader::VerifyAndSkipTrailingBits(size_t num_bits) {
192   if (ReadBit() != 1) return false;
193   for (size_t i = 0; i < num_bits - 1; ++i) {
194     if (ReadBit() != 0) return false;
195   }
196   return true;
197 }
198 
SkipBytes(size_t num_bytes)199 bool RawBitReader::SkipBytes(size_t num_bytes) {
200   // If we are not at a byte boundary, return false.
201   return ((bit_offset_ & 7) != 0) ? false : SkipBits(num_bytes * 8);
202 }
203 
SkipBits(size_t num_bits)204 bool RawBitReader::SkipBits(size_t num_bits) {
205   // If the reader is already finished, return false.
206   if (Finished()) return false;
207   // If skipping |num_bits| runs out of buffer, return false.
208   const size_t bit_offset = bit_offset_ + num_bits - 1;
209   if (DivideBy8(bit_offset, false) >= size_) return false;
210   bit_offset_ += num_bits;
211   return true;
212 }
213 
CanReadLiteral(size_t num_bits) const214 bool RawBitReader::CanReadLiteral(size_t num_bits) const {
215   if (Finished()) return false;
216   const size_t bit_offset = bit_offset_ + num_bits - 1;
217   return DivideBy8(bit_offset, false) < size_;
218 }
219 
Finished() const220 bool RawBitReader::Finished() const {
221   return DivideBy8(bit_offset_, false) >= size_;
222 }
223 
224 }  // namespace libgav1
225