Shaka Packager SDK
h26x_bit_reader.cc
1 // Copyright 2014 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/base/logging.h"
6 #include "packager/media/codecs/h26x_bit_reader.h"
7 
8 namespace shaka {
9 namespace media {
10 namespace {
11 
12 // Check if any bits in the least significant |valid_bits| are set to 1.
13 bool CheckAnyBitsSet(int byte, int valid_bits) {
14  return (byte & ((1 << valid_bits) - 1)) != 0;
15 }
16 
17 } // namespace
18 
19 H26xBitReader::H26xBitReader()
20  : data_(NULL),
21  bytes_left_(0),
22  curr_byte_(0),
23  num_remaining_bits_in_curr_byte_(0),
24  prev_two_bytes_(0),
25  emulation_prevention_bytes_(0) {}
26 
27 H26xBitReader::~H26xBitReader() {}
28 
29 bool H26xBitReader::Initialize(const uint8_t* data, off_t size) {
30  DCHECK(data);
31 
32  if (size < 1)
33  return false;
34 
35  data_ = data;
36  bytes_left_ = size;
37  num_remaining_bits_in_curr_byte_ = 0;
38  // Initially set to 0xffff to accept all initial two-byte sequences.
39  prev_two_bytes_ = 0xffff;
40  emulation_prevention_bytes_ = 0;
41 
42  return true;
43 }
44 
45 bool H26xBitReader::UpdateCurrByte() {
46  if (bytes_left_ < 1)
47  return false;
48 
49  // Emulation prevention three-byte detection.
50  // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03).
51  if (*data_ == 0x03 && (prev_two_bytes_ & 0xffff) == 0) {
52  // Detected 0x000003, skip last byte.
53  ++data_;
54  --bytes_left_;
55  ++emulation_prevention_bytes_;
56  // Need another full three bytes before we can detect the sequence again.
57  prev_two_bytes_ = 0xffff;
58 
59  if (bytes_left_ < 1)
60  return false;
61  }
62 
63  // Load a new byte and advance pointers.
64  curr_byte_ = *data_++ & 0xff;
65  --bytes_left_;
66  num_remaining_bits_in_curr_byte_ = 8;
67 
68  prev_two_bytes_ = (prev_two_bytes_ << 8) | curr_byte_;
69 
70  return true;
71 }
72 
73 // Read |num_bits| (1 to 31 inclusive) from the stream and return them
74 // in |out|, with first bit in the stream as MSB in |out| at position
75 // (|num_bits| - 1).
76 bool H26xBitReader::ReadBits(int num_bits, int* out) {
77  int bits_left = num_bits;
78  *out = 0;
79  DCHECK(num_bits <= 31);
80 
81  while (num_remaining_bits_in_curr_byte_ < bits_left) {
82  // Take all that's left in current byte, shift to make space for the rest.
83  *out |= (curr_byte_ << (bits_left - num_remaining_bits_in_curr_byte_));
84  bits_left -= num_remaining_bits_in_curr_byte_;
85 
86  if (!UpdateCurrByte())
87  return false;
88  }
89 
90  *out |= (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_left));
91  *out &= ((1 << num_bits) - 1);
92  num_remaining_bits_in_curr_byte_ -= bits_left;
93 
94  return true;
95 }
96 
97 bool H26xBitReader::SkipBits(int num_bits) {
98  int bits_left = num_bits;
99  while (num_remaining_bits_in_curr_byte_ < bits_left) {
100  bits_left -= num_remaining_bits_in_curr_byte_;
101  if (!UpdateCurrByte())
102  return false;
103  }
104 
105  num_remaining_bits_in_curr_byte_ -= bits_left;
106  return true;
107 }
108 
109 bool H26xBitReader::ReadUE(int* val) {
110  int num_bits = -1;
111  int bit;
112  int rest;
113 
114  // Count the number of contiguous zero bits.
115  do {
116  if (!ReadBits(1, &bit))
117  return false;
118  num_bits++;
119  } while (bit == 0);
120 
121  if (num_bits > 31)
122  return false;
123 
124  // Calculate exp-Golomb code value of size num_bits.
125  *val = (1 << num_bits) - 1;
126 
127  if (num_bits > 0) {
128  if (!ReadBits(num_bits, &rest))
129  return false;
130  *val += rest;
131  }
132 
133  return true;
134 }
135 
136 bool H26xBitReader::ReadSE(int* val) {
137  int ue;
138 
139  // See Chapter 9 in the spec.
140  if (!ReadUE(&ue))
141  return false;
142 
143  if (ue % 2 == 0)
144  *val = -(ue / 2);
145  else
146  *val = ue / 2 + 1;
147 
148  return true;
149 }
150 
151 off_t H26xBitReader::NumBitsLeft() {
152  return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8);
153 }
154 
155 bool H26xBitReader::HasMoreRBSPData() {
156  // Make sure we have more bits, if we are at 0 bits in current byte and
157  // updating current byte fails, we don't have more data anyway.
158  if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte())
159  return false;
160 
161  // If there is no more RBSP data, then the remaining bits is the stop bit
162  // followed by zero paddings. So if there are 1s in the remaining bits
163  // excluding the current bit, then the current bit is not a stop bit,
164  // regardless of whether it is 1 or not. Therefore there is more data.
165  if (CheckAnyBitsSet(curr_byte_, num_remaining_bits_in_curr_byte_ - 1))
166  return true;
167 
168  // While the spec disallows it (7.4.1: "The last byte of the NAL unit shall
169  // not be equal to 0x00"), some streams have trailing null bytes anyway. We
170  // don't handle emulation prevention sequences because HasMoreRBSPData() is
171  // not used when parsing slices (where cabac_zero_word elements are legal).
172  for (off_t i = 0; i < bytes_left_; i++) {
173  if (data_[i] != 0)
174  return true;
175  }
176 
177  bytes_left_ = 0;
178  return false;
179 }
180 
181 size_t H26xBitReader::NumEmulationPreventionBytesRead() {
182  return emulation_prevention_bytes_;
183 }
184 
185 } // namespace media
186 } // namespace shaka
All the methods that are virtual are virtual for mocking.