DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs 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/filters/h26x_bit_reader.h"
7 
8 namespace edash_packager {
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::ReadUE(int* val) {
90  int num_bits = -1;
91  int bit;
92  int rest;
93 
94  // Count the number of contiguous zero bits.
95  do {
96  if (!ReadBits(1, &bit))
97  return false;
98  num_bits++;
99  } while (bit == 0);
100 
101  if (num_bits > 31)
102  return false;
103 
104  // Calculate exp-Golomb code value of size num_bits.
105  *val = (1 << num_bits) - 1;
106 
107  if (num_bits > 0) {
108  if (!ReadBits(num_bits, &rest))
109  return false;
110  *val += rest;
111  }
112 
113  return true;
114 }
115 
116 bool H26xBitReader::ReadSE(int* val) {
117  int ue;
118 
119  // See Chapter 9 in the spec.
120  if (!ReadUE(&ue))
121  return false;
122 
123  if (ue % 2 == 0)
124  *val = -(ue / 2);
125  else
126  *val = ue / 2 + 1;
127 
128  return true;
129 }
130 
131 off_t H26xBitReader::NumBitsLeft() {
132  return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8);
133 }
134 
135 bool H26xBitReader::HasMoreRBSPData() {
136  // Make sure we have more bits, if we are at 0 bits in current byte
137  // and updating current byte fails, we don't have more data anyway.
138  if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte())
139  return false;
140 
141  // On last byte?
142  if (bytes_left_)
143  return true;
144 
145  // Last byte, look for stop bit;
146  // We have more RBSP data if the last non-zero bit we find is not the
147  // first available bit.
148  return (curr_byte_ &
149  ((1 << (num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0;
150 }
151 
152 size_t H26xBitReader::NumEmulationPreventionBytesRead() {
153  return emulation_prevention_bytes_;
154 }
155 
156 } // namespace media
157 } // namespace edash_packager