1 // Copyright 2015 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 "vp9_raw_bits_reader.h" 6 7 #include <limits.h> 8 9 #include "base/logging.h" 10 #include "bit_reader.h" 11 12 namespace media { 13 14 Vp9RawBitsReader::Vp9RawBitsReader() : valid_(true) {} 15 16 Vp9RawBitsReader::~Vp9RawBitsReader() {} 17 18 void Vp9RawBitsReader::Initialize(const uint8_t* data, size_t size) { 19 DCHECK(data); 20 reader_.reset(new BitReader(data, size)); 21 valid_ = true; 22 } 23 24 bool Vp9RawBitsReader::ReadBool() { 25 DCHECK(reader_); 26 if (!valid_) 27 return false; 28 29 int value = 0; 30 valid_ = reader_->ReadBits(1, &value); 31 return valid_ ? value == 1 : false; 32 } 33 34 int Vp9RawBitsReader::ReadLiteral(int bits) { 35 DCHECK(reader_); 36 if (!valid_) 37 return 0; 38 39 int value = 0; 40 DCHECK_LT(static_cast<size_t>(bits), sizeof(value) * 8); 41 valid_ = reader_->ReadBits(bits, &value); 42 return valid_ ? value : 0; 43 } 44 45 int Vp9RawBitsReader::ReadSignedLiteral(int bits) { 46 int value = ReadLiteral(bits); 47 return ReadBool() ? -value : value; 48 } 49 50 size_t Vp9RawBitsReader::GetBytesRead() const { 51 DCHECK(reader_); 52 return (reader_->bits_read() + 7) / 8; 53 } 54 55 bool Vp9RawBitsReader::ConsumeTrailingBits() { 56 DCHECK(reader_); 57 int bits_left = GetBytesRead() * 8 - reader_->bits_read(); 58 return ReadLiteral(bits_left) == 0; 59 } 60 61 } // namespace media 62