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