• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 The libgav1 Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIBGAV1_SRC_UTILS_RAW_BIT_READER_H_
18 #define LIBGAV1_SRC_UTILS_RAW_BIT_READER_H_
19 
20 #include <cstddef>
21 #include <cstdint>
22 
23 #include "src/utils/bit_reader.h"
24 #include "src/utils/memory.h"
25 
26 namespace libgav1 {
27 
28 class RawBitReader : public BitReader, public Allocable {
29  public:
30   RawBitReader(const uint8_t* data, size_t size);
31   ~RawBitReader() override = default;
32 
33   int ReadBit() override;
34   int64_t ReadLiteral(int num_bits) override;  // f(n) in the spec.
35   bool ReadInverseSignedLiteral(int num_bits,
36                                 int* value);  // su(1+num_bits) in the spec.
37   bool ReadLittleEndian(int num_bytes,
38                         size_t* value);    // le(n) in the spec.
39   bool ReadUnsignedLeb128(size_t* value);  // leb128() in the spec.
40   // Reads a variable length unsigned number and stores it in |*value|. On a
41   // successful return, |*value| is in the range of 0 to UINT32_MAX - 1,
42   // inclusive.
43   bool ReadUvlc(uint32_t* value);  // uvlc() in the spec.
44   bool Finished() const;
bit_offset()45   size_t bit_offset() const { return bit_offset_; }
46   // Return the bytes consumed so far (rounded up).
byte_offset()47   size_t byte_offset() const { return (bit_offset() + 7) >> 3; }
size()48   size_t size() const { return size_; }
49   // Move to the next byte boundary if not already at one. Return false if any
50   // of the bits being skipped over is non-zero. Return true otherwise. If this
51   // function returns false, the reader is left in an undefined state and must
52   // not be used further. section 5.3.5.
53   bool AlignToNextByte();
54   // Make sure that the trailing bits structure is as expected and skip over it.
55   // section 5.3.4.
56   bool VerifyAndSkipTrailingBits(size_t num_bits);
57   // Skip |num_bytes| bytes. This only works if the current position is at a
58   // byte boundary. The function returns false if the current position is not at
59   // a byte boundary or if skipping |num_bytes| causes the reader to run out of
60   // buffer. Returns true otherwise.
61   bool SkipBytes(size_t num_bytes);
62   // Skip |num_bits| bits. The function returns false if skipping |num_bits|
63   // causes the reader to run out of buffer. Returns true otherwise.
64   bool SkipBits(size_t num_bits);
65 
66  private:
67   // Returns true if it is safe to read a literal of size |num_bits|.
68   bool CanReadLiteral(size_t num_bits) const;
69   int ReadBitImpl();
70 
71   const uint8_t* const data_;
72   size_t bit_offset_;
73   const size_t size_;
74 };
75 
76 }  // namespace libgav1
77 
78 #endif  // LIBGAV1_SRC_UTILS_RAW_BIT_READER_H_
79