summaryrefslogtreecommitdiffstats
path: root/media/base/bit_reader.cc
blob: e4106fa24a8dab44ddcda11bb8eb555e113b485f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/base/bit_reader.h"

namespace media {

BitReader::BitReader()
    : data_(NULL),
      bytes_left_(0),
      position_(0),
      curr_byte_(0),
      num_remaining_bits_in_curr_byte_(0) {
}

BitReader::BitReader(const uint8* data, off_t size) {
  Initialize(data, size);
}

BitReader::~BitReader() {}

void BitReader::Initialize(const uint8* data, off_t size) {
  DCHECK(data != NULL || size == 0);  // Data cannot be NULL if size is not 0.

  data_ = data;
  bytes_left_ = size;
  position_ = 0;
  num_remaining_bits_in_curr_byte_ = 0;

  UpdateCurrByte();
}

void BitReader::UpdateCurrByte() {
  DCHECK_EQ(num_remaining_bits_in_curr_byte_, 0);

  if (bytes_left_ < 1)
    return;

  // Load a new byte and advance pointers.
  curr_byte_ = *data_;
  ++data_;
  --bytes_left_;
  num_remaining_bits_in_curr_byte_ = 8;
}

bool BitReader::SkipBits(int num_bits) {
  int dummy;
  const int kDummySize = static_cast<int>(sizeof(dummy)) * 8;

  while (num_bits > kDummySize) {
    if (ReadBits(kDummySize, &dummy)) {
      num_bits -= kDummySize;
    } else {
      return false;
    }
  }

  return ReadBits(num_bits, &dummy);
}

off_t BitReader::Tell() const {
  return position_;
}

off_t BitReader::NumBitsLeft() const {
  return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8);
}

bool BitReader::HasMoreData() const {
  return num_remaining_bits_in_curr_byte_ != 0;
}

}  // namespace media