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 "base/logging.h"
6 #include "h264_bit_reader.h"
7
8 namespace media {
9
H264BitReader()10 H264BitReader::H264BitReader()
11 : data_(NULL),
12 bytes_left_(0),
13 curr_byte_(0),
14 num_remaining_bits_in_curr_byte_(0),
15 prev_two_bytes_(0),
16 emulation_prevention_bytes_(0) {}
17
~H264BitReader()18 H264BitReader::~H264BitReader() {}
19
Initialize(const uint8_t * data,off_t size)20 bool H264BitReader::Initialize(const uint8_t* data, off_t size) {
21 DCHECK(data);
22
23 if (size < 1)
24 return false;
25
26 data_ = data;
27 bytes_left_ = size;
28 num_remaining_bits_in_curr_byte_ = 0;
29 // Initially set to 0xffff to accept all initial two-byte sequences.
30 prev_two_bytes_ = 0xffff;
31 emulation_prevention_bytes_ = 0;
32
33 return true;
34 }
35
UpdateCurrByte()36 bool H264BitReader::UpdateCurrByte() {
37 if (bytes_left_ < 1)
38 return false;
39
40 // Emulation prevention three-byte detection.
41 // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03).
42 if (*data_ == 0x03 && (prev_two_bytes_ & 0xffff) == 0) {
43 // Detected 0x000003, skip last byte.
44 ++data_;
45 --bytes_left_;
46 ++emulation_prevention_bytes_;
47 // Need another full three bytes before we can detect the sequence again.
48 prev_two_bytes_ = 0xffff;
49
50 if (bytes_left_ < 1)
51 return false;
52 }
53
54 // Load a new byte and advance pointers.
55 curr_byte_ = *data_++ & 0xff;
56 --bytes_left_;
57 num_remaining_bits_in_curr_byte_ = 8;
58
59 prev_two_bytes_ = ((prev_two_bytes_ & 0xff) << 8) | curr_byte_;
60
61 return true;
62 }
63
64 // Read |num_bits| (1 to 31 inclusive) from the stream and return them
65 // in |out|, with first bit in the stream as MSB in |out| at position
66 // (|num_bits| - 1).
ReadBits(int num_bits,int * out)67 bool H264BitReader::ReadBits(int num_bits, int* out) {
68 int bits_left = num_bits;
69 *out = 0;
70 DCHECK(num_bits <= 31);
71
72 while (num_remaining_bits_in_curr_byte_ < bits_left) {
73 // Take all that's left in current byte, shift to make space for the rest.
74 *out |= (curr_byte_ << (bits_left - num_remaining_bits_in_curr_byte_));
75 bits_left -= num_remaining_bits_in_curr_byte_;
76
77 if (!UpdateCurrByte())
78 return false;
79 }
80
81 *out |= (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_left));
82 *out &= ((1u << num_bits) - 1u);
83 num_remaining_bits_in_curr_byte_ -= bits_left;
84
85 return true;
86 }
87
NumBitsLeft()88 off_t H264BitReader::NumBitsLeft() {
89 return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8);
90 }
91
HasMoreRBSPData()92 bool H264BitReader::HasMoreRBSPData() {
93 // Make sure we have more bits, if we are at 0 bits in current byte and
94 // updating current byte fails, we don't have more data anyway.
95 if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte())
96 return false;
97
98 // If there is no more RBSP data, then |curr_byte_| contains the stop bit and
99 // zero padding. Check to see if there is other data instead.
100 // (We don't actually check for the stop bit itself, instead treating the
101 // invalid case of all trailing zeros identically).
102 if ((curr_byte_ & ((1 << (num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0)
103 return true;
104
105 // While the spec disallows it (7.4.1: "The last byte of the NAL unit shall
106 // not be equal to 0x00"), some streams have trailing null bytes anyway. We
107 // don't handle emulation prevention sequences because HasMoreRBSPData() is
108 // not used when parsing slices (where cabac_zero_word elements are legal).
109 for (off_t i = 0; i < bytes_left_; i++) {
110 if (data_[i] != 0)
111 return true;
112 }
113
114 bytes_left_ = 0;
115 return false;
116 }
117
NumEmulationPreventionBytesRead()118 size_t H264BitReader::NumEmulationPreventionBytesRead() {
119 return emulation_prevention_bytes_;
120 }
121
122 } // namespace media
123