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