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 // Note: ported from Chromium commit head: 2de6929
5
6 #include "base/logging.h"
7 #include "h264_bit_reader.h"
8
9 namespace media {
10
H264BitReader()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() = default;
20
Initialize(const uint8_t * data,off_t size)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
UpdateCurrByte()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_ & 0xff) << 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).
ReadBits(int num_bits,int * out)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 &= ((1u << num_bits) - 1u);
84 num_remaining_bits_in_curr_byte_ -= bits_left;
85
86 return true;
87 }
88
NumBitsLeft()89 off_t H264BitReader::NumBitsLeft() {
90 return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8);
91 }
92
HasMoreRBSPData()93 bool H264BitReader::HasMoreRBSPData() {
94 // Make sure we have more bits, if we are at 0 bits in current byte and
95 // 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 // If there is no more RBSP data, then |curr_byte_| contains the stop bit and
100 // zero padding. Check to see if there is other data instead.
101 // (We don't actually check for the stop bit itself, instead treating the
102 // invalid case of all trailing zeros identically).
103 if ((curr_byte_ & ((1 << (num_remaining_bits_in_curr_byte_ - 1)) - 1)) != 0)
104 return true;
105
106 // While the spec disallows it (7.4.1: "The last byte of the NAL unit shall
107 // not be equal to 0x00"), some streams have trailing null bytes anyway. We
108 // don't handle emulation prevention sequences because HasMoreRBSPData() is
109 // not used when parsing slices (where cabac_zero_word elements are legal).
110 for (off_t i = 0; i < bytes_left_; i++) {
111 if (data_[i] != 0)
112 return true;
113 }
114
115 bytes_left_ = 0;
116 return false;
117 }
118
NumEmulationPreventionBytesRead()119 size_t H264BitReader::NumEmulationPreventionBytesRead() {
120 return emulation_prevention_bytes_;
121 }
122
123 } // namespace media
124