DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
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 
11 H26xBitReader::H26xBitReader()
12  : data_(NULL),
13  bytes_left_(0),
14  curr_byte_(0),
15  num_remaining_bits_in_curr_byte_(0),
16  prev_two_bytes_(0),
17  emulation_prevention_bytes_(0) {}
18 
19 H26xBitReader::~H26xBitReader() {}
20 
21 bool H26xBitReader::Initialize(const uint8_t* data, off_t size) {
22  DCHECK(data);
23 
24  if (size < 1)
25  return false;
26 
27  data_ = data;
28  bytes_left_ = size;
29  num_remaining_bits_in_curr_byte_ = 0;
30  // Initially set to 0xffff to accept all initial two-byte sequences.
31  prev_two_bytes_ = 0xffff;
32  emulation_prevention_bytes_ = 0;
33 
34  return true;
35 }
36 
37 bool H26xBitReader::UpdateCurrByte() {
38  if (bytes_left_ < 1)
39  return false;
40 
41  // Emulation prevention three-byte detection.
42  // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03).
43  if (*data_ == 0x03 && (prev_two_bytes_ & 0xffff) == 0) {
44  // Detected 0x000003, skip last byte.
45  ++data_;
46  --bytes_left_;
47  ++emulation_prevention_bytes_;
48  // Need another full three bytes before we can detect the sequence again.
49  prev_two_bytes_ = 0xffff;
50 
51  if (bytes_left_ < 1)
52  return false;
53  }
54 
55  // Load a new byte and advance pointers.
56  curr_byte_ = *data_++ & 0xff;
57  --bytes_left_;
58  num_remaining_bits_in_curr_byte_ = 8;
59 
60  prev_two_bytes_ = (prev_two_bytes_ << 8) | curr_byte_;
61 
62  return true;
63 }
64 
65 // Read |num_bits| (1 to 31 inclusive) from the stream and return them
66 // in |out|, with first bit in the stream as MSB in |out| at position
67 // (|num_bits| - 1).
68 bool H26xBitReader::ReadBits(int num_bits, int* out) {
69  int bits_left = num_bits;
70  *out = 0;
71  DCHECK(num_bits <= 31);
72 
73  while (num_remaining_bits_in_curr_byte_ < bits_left) {
74  // Take all that's left in current byte, shift to make space for the rest.
75  *out |= (curr_byte_ << (bits_left - num_remaining_bits_in_curr_byte_));
76  bits_left -= num_remaining_bits_in_curr_byte_;
77 
78  if (!UpdateCurrByte())
79  return false;
80  }
81 
82  *out |= (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_left));
83  *out &= ((1 << num_bits) - 1);
84  num_remaining_bits_in_curr_byte_ -= bits_left;
85 
86  return true;
87 }
88 
89 bool H26xBitReader::SkipBits(int num_bits) {
90  int bits_left = num_bits;
91  while (num_remaining_bits_in_curr_byte_ < bits_left) {
92  bits_left -= num_remaining_bits_in_curr_byte_;
93  if (!UpdateCurrByte())
94  return false;
95  }
96 
97  num_remaining_bits_in_curr_byte_ -= bits_left;
98  return true;
99 }
100 
101 bool H26xBitReader::ReadUE(int* val) {
102  int num_bits = -1;
103  int bit;
104  int rest;
105 
106  // Count the number of contiguous zero bits.
107  do {
108  if (!ReadBits(1, &bit))
109  return false;
110  num_bits++;
111  } while (bit == 0);
112 
113  if (num_bits > 31)
114  return false;
115 
116  // Calculate exp-Golomb code value of size num_bits.
117  *val = (1 << num_bits) - 1;
118 
119  if (num_bits > 0) {
120  if (!ReadBits(num_bits, &rest))
121  return false;
122  *val += rest;
123  }
124 
125  return true;
126 }
127 
128 bool H26xBitReader::ReadSE(int* val) {
129  int ue;
130 
131  // See Chapter 9 in the spec.
132  if (!ReadUE(&ue))
133  return false;
134 
135  if (ue % 2 == 0)
136  *val = -(ue / 2);
137  else
138  *val = ue / 2 + 1;
139 
140  return true;
141 }
142 
143 off_t H26xBitReader::NumBitsLeft() {
144  return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8);
145 }
146 
147 bool H26xBitReader::HasMoreRBSPData() {
148  // Make sure we have more bits, if we are at 0 bits in current byte
149  // and updating current byte fails, we don't have more data anyway.
150  if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte())
151  return false;
152 
153  // On last byte?
154  if (bytes_left_)
155  return true;
156 
157  // Last byte, look for stop bit;
158  // We have more RBSP data if the last non-zero bit we find is not the
159  // first available bit.
160  return (curr_byte_ &
161  ((1 << (num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0;
162 }
163 
164 size_t H26xBitReader::NumEmulationPreventionBytesRead() {
165  return emulation_prevention_bytes_;
166 }
167 
168 } // namespace media
169 } // namespace shaka