Shaka Packager SDK
Loading...
Searching...
No Matches
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/media/codecs/h26x_bit_reader.h>
6
7#include <absl/log/check.h>
8#include <absl/log/log.h>
9
10namespace shaka {
11namespace media {
12namespace {
13
14// Check if any bits in the least significant |valid_bits| are set to 1.
15bool CheckAnyBitsSet(int byte, int valid_bits) {
16 return (byte & ((1 << valid_bits) - 1)) != 0;
17}
18
19} // namespace
20
21H26xBitReader::H26xBitReader()
22 : data_(NULL),
23 bytes_left_(0),
24 curr_byte_(0),
25 num_remaining_bits_in_curr_byte_(0),
26 prev_two_bytes_(0),
27 emulation_prevention_bytes_(0) {}
28
29H26xBitReader::~H26xBitReader() {}
30
31bool H26xBitReader::Initialize(const uint8_t* data, off_t size) {
32 DCHECK(data);
33
34 if (size < 1)
35 return false;
36
37 data_ = data;
38 bytes_left_ = size;
39 num_remaining_bits_in_curr_byte_ = 0;
40 // Initially set to 0xffff to accept all initial two-byte sequences.
41 prev_two_bytes_ = 0xffff;
42 emulation_prevention_bytes_ = 0;
43
44 return true;
45}
46
47bool H26xBitReader::UpdateCurrByte() {
48 if (bytes_left_ < 1)
49 return false;
50
51 // Emulation prevention three-byte detection.
52 // If a sequence of 0x000003 is found, skip (ignore) the last byte (0x03).
53 if (*data_ == 0x03 && (prev_two_bytes_ & 0xffff) == 0) {
54 // Detected 0x000003, skip last byte.
55 ++data_;
56 --bytes_left_;
57 ++emulation_prevention_bytes_;
58 // Need another full three bytes before we can detect the sequence again.
59 prev_two_bytes_ = 0xffff;
60
61 if (bytes_left_ < 1)
62 return false;
63 }
64
65 // Load a new byte and advance pointers.
66 curr_byte_ = *data_++ & 0xff;
67 --bytes_left_;
68 num_remaining_bits_in_curr_byte_ = 8;
69
70 prev_two_bytes_ = (prev_two_bytes_ << 8) | curr_byte_;
71
72 return true;
73}
74
75// Read |num_bits| (1 to 31 inclusive) from the stream and return them
76// in |out|, with first bit in the stream as MSB in |out| at position
77// (|num_bits| - 1).
78bool H26xBitReader::ReadBits(int num_bits, int* out) {
79 int bits_left = num_bits;
80 *out = 0;
81 DCHECK(num_bits <= 31);
82
83 while (num_remaining_bits_in_curr_byte_ < bits_left) {
84 // Take all that's left in current byte, shift to make space for the rest.
85 *out |= (curr_byte_ << (bits_left - num_remaining_bits_in_curr_byte_));
86 bits_left -= num_remaining_bits_in_curr_byte_;
87
88 if (!UpdateCurrByte())
89 return false;
90 }
91
92 *out |= (curr_byte_ >> (num_remaining_bits_in_curr_byte_ - bits_left));
93 *out &= ((1 << num_bits) - 1);
94 num_remaining_bits_in_curr_byte_ -= bits_left;
95
96 return true;
97}
98
99bool H26xBitReader::SkipBits(int num_bits) {
100 int bits_left = num_bits;
101 while (num_remaining_bits_in_curr_byte_ < bits_left) {
102 bits_left -= num_remaining_bits_in_curr_byte_;
103 if (!UpdateCurrByte())
104 return false;
105 }
106
107 num_remaining_bits_in_curr_byte_ -= bits_left;
108 return true;
109}
110
111bool H26xBitReader::ReadUE(int* val) {
112 int num_bits = -1;
113 int bit;
114 int rest;
115
116 // Count the number of contiguous zero bits.
117 do {
118 if (!ReadBits(1, &bit))
119 return false;
120 num_bits++;
121 } while (bit == 0);
122
123 if (num_bits > 31)
124 return false;
125
126 // Calculate exp-Golomb code value of size num_bits.
127 *val = (1 << num_bits) - 1;
128
129 if (num_bits > 0) {
130 if (!ReadBits(num_bits, &rest))
131 return false;
132 *val += rest;
133 }
134
135 return true;
136}
137
138bool H26xBitReader::ReadSE(int* val) {
139 int ue;
140
141 // See Chapter 9 in the spec.
142 if (!ReadUE(&ue))
143 return false;
144
145 if (ue % 2 == 0)
146 *val = -(ue / 2);
147 else
148 *val = ue / 2 + 1;
149
150 return true;
151}
152
153off_t H26xBitReader::NumBitsLeft() {
154 return (num_remaining_bits_in_curr_byte_ + bytes_left_ * 8);
155}
156
157bool H26xBitReader::HasMoreRBSPData() {
158 // Make sure we have more bits, if we are at 0 bits in current byte and
159 // updating current byte fails, we don't have more data anyway.
160 if (num_remaining_bits_in_curr_byte_ == 0 && !UpdateCurrByte())
161 return false;
162
163 // If there is no more RBSP data, then the remaining bits is the stop bit
164 // followed by zero paddings. So if there are 1s in the remaining bits
165 // excluding the current bit, then the current bit is not a stop bit,
166 // regardless of whether it is 1 or not. Therefore there is more data.
167 if (CheckAnyBitsSet(curr_byte_, num_remaining_bits_in_curr_byte_ - 1))
168 return true;
169
170 // While the spec disallows it (7.4.1: "The last byte of the NAL unit shall
171 // not be equal to 0x00"), some streams have trailing null bytes anyway. We
172 // don't handle emulation prevention sequences because HasMoreRBSPData() is
173 // not used when parsing slices (where cabac_zero_word elements are legal).
174 for (off_t i = 0; i < bytes_left_; i++) {
175 if (data_[i] != 0)
176 return true;
177 }
178
179 bytes_left_ = 0;
180 return false;
181}
182
183size_t H26xBitReader::NumEmulationPreventionBytesRead() {
184 return emulation_prevention_bytes_;
185}
186
187} // namespace media
188} // namespace shaka
All the methods that are virtual are virtual for mocking.