DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
h264_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/h264_bit_reader.h"
7 
8 namespace edash_packager {
9 namespace media {
10 
11 H264BitReader::H264BitReader()
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 H264BitReader::~H264BitReader() {}
20 
21 bool H264BitReader::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 H264BitReader::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 H264BitReader::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 off_t H264BitReader::NumBitsLeft() {
90  return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8);
91 }
92 
93 bool H264BitReader::HasMoreRBSPData() {
94  // Make sure we have more bits, if we are at 0 bits in current byte
95  // and updating current byte fails, we don't have more data anyway.
96  if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte())
97  return false;
98 
99  // On last byte?
100  if (bytes_left_)
101  return true;
102 
103  // Last byte, look for stop bit;
104  // We have more RBSP data if the last non-zero bit we find is not the
105  // first available bit.
106  return (curr_byte_ &
107  ((1 << (num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0;
108 }
109 
110 size_t H264BitReader::NumEmulationPreventionBytesRead() {
111  return emulation_prevention_bytes_;
112 }
113 
114 } // namespace media
115 } // namespace edash_packager