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