DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
bit_reader.cc
1 // Copyright (c) 2012 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 "packager/media/base/bit_reader.h"
6 
7 #include <algorithm>
8 
9 namespace edash_packager {
10 namespace media {
11 
12 BitReader::BitReader(const uint8_t* data, off_t size)
13  : data_(data),
14  initial_size_(size),
15  bytes_left_(size),
16  num_remaining_bits_in_curr_byte_(0) {
17  DCHECK(data_ != NULL && bytes_left_ > 0);
18 
19  UpdateCurrByte();
20 }
21 
22 BitReader::~BitReader() {}
23 
24 bool BitReader::SkipBits(int num_bits) {
25  DCHECK_GE(num_bits, 0);
26 
27  // Skip any bits in the current byte waiting to be processed, then
28  // process full bytes until less than 8 bits remaining.
29  if (num_bits > num_remaining_bits_in_curr_byte_) {
30  num_bits -= num_remaining_bits_in_curr_byte_;
31  num_remaining_bits_in_curr_byte_ = 0;
32 
33  int num_bytes = num_bits / 8;
34  num_bits %= 8;
35  if (bytes_left_ < num_bytes) {
36  bytes_left_ = 0;
37  return false;
38  }
39  bytes_left_ -= num_bytes;
40  data_ += num_bytes;
41  UpdateCurrByte();
42 
43  // If there is no more data remaining, only return true if we
44  // skipped all that were requested.
45  if (num_remaining_bits_in_curr_byte_ == 0)
46  return (num_bits == 0);
47  }
48 
49  // Less than 8 bits remaining to skip. Use ReadBitsInternal to verify
50  // that the remaining bits we need exist, and adjust them as necessary
51  // for subsequent operations.
52  uint64_t not_needed;
53  return ReadBitsInternal(num_bits, &not_needed);
54 }
55 
56 bool BitReader::SkipBytes(int num_bytes) {
57  if (num_remaining_bits_in_curr_byte_ != 8)
58  return false;
59  if (num_bytes == 0)
60  return true;
61 
62  data_ += num_bytes - 1; // One additional byte in curr_byte_.
63  if (num_bytes > bytes_left_ + 1)
64  return false;
65  bytes_left_ -= num_bytes - 1;
66  num_remaining_bits_in_curr_byte_ = 0;
67  UpdateCurrByte();
68  return true;
69 }
70 
71 bool BitReader::ReadBitsInternal(int num_bits, uint64_t* out) {
72  DCHECK_LE(num_bits, 64);
73 
74  *out = 0;
75 
76  while (num_remaining_bits_in_curr_byte_ != 0 && num_bits != 0) {
77  int bits_to_take = std::min(num_remaining_bits_in_curr_byte_, num_bits);
78 
79  *out <<= bits_to_take;
80  *out += curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_to_take);
81  num_bits -= bits_to_take;
82  num_remaining_bits_in_curr_byte_ -= bits_to_take;
83  curr_byte_ &= (1 << num_remaining_bits_in_curr_byte_) - 1;
84 
85  if (num_remaining_bits_in_curr_byte_ == 0)
86  UpdateCurrByte();
87  }
88 
89  return num_bits == 0;
90 }
91 
92 void BitReader::UpdateCurrByte() {
93  DCHECK_EQ(num_remaining_bits_in_curr_byte_, 0);
94 
95  if (bytes_left_ == 0)
96  return;
97 
98  // Load a new byte and advance pointers.
99  curr_byte_ = *data_;
100  ++data_;
101  --bytes_left_;
102  num_remaining_bits_in_curr_byte_ = 8;
103 }
104 
105 } // namespace media
106 } // namespace edash_packager
BitReader(const uint8_t *data, off_t size)
Definition: bit_reader.cc:12
bool SkipBits(int num_bits)
Definition: bit_reader.cc:24
bool SkipBytes(int num_bytes)
Definition: bit_reader.cc:56