OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 #include "webrtc/modules/video_coding/utility/h264_bitstream_parser.h" | 10 #include "webrtc/modules/video_coding/utility/h264_bitstream_parser.h" |
11 | 11 |
12 #include <memory> | 12 #include <memory> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "webrtc/base/bitbuffer.h" | 15 #include "webrtc/base/bitbuffer.h" |
16 #include "webrtc/base/bytebuffer.h" | 16 #include "webrtc/base/bytebuffer.h" |
17 #include "webrtc/base/checks.h" | 17 #include "webrtc/base/checks.h" |
| 18 |
| 19 #include "webrtc/common_video/h264/h264_common.h" |
18 #include "webrtc/base/logging.h" | 20 #include "webrtc/base/logging.h" |
19 | 21 |
20 namespace webrtc { | 22 namespace webrtc { |
21 namespace { | |
22 // The size of a NALU header {0 0 0 1}. | |
23 static const size_t kNaluHeaderSize = 4; | |
24 | |
25 // The size of a NALU header plus the type byte. | |
26 static const size_t kNaluHeaderAndTypeSize = kNaluHeaderSize + 1; | |
27 | |
28 // The NALU type. | |
29 static const uint8_t kNaluSps = 0x7; | |
30 static const uint8_t kNaluPps = 0x8; | |
31 static const uint8_t kNaluIdr = 0x5; | |
32 static const uint8_t kNaluTypeMask = 0x1F; | |
33 | |
34 static const uint8_t kSliceTypeP = 0x0; | |
35 static const uint8_t kSliceTypeB = 0x1; | |
36 static const uint8_t kSliceTypeSp = 0x3; | |
37 | |
38 // Returns a vector of the NALU start sequences (0 0 0 1) in the given buffer. | |
39 std::vector<size_t> FindNaluStartSequences(const uint8_t* buffer, | |
40 size_t buffer_size) { | |
41 std::vector<size_t> sequences; | |
42 // This is sorta like Boyer-Moore, but with only the first optimization step: | |
43 // given a 4-byte sequence we're looking at, if the 4th byte isn't 1 or 0, | |
44 // skip ahead to the next 4-byte sequence. 0s and 1s are relatively rare, so | |
45 // this will skip the majority of reads/checks. | |
46 const uint8_t* end = buffer + buffer_size - 4; | |
47 for (const uint8_t* head = buffer; head < end;) { | |
48 if (head[3] > 1) { | |
49 head += 4; | |
50 } else if (head[3] == 1 && head[2] == 0 && head[1] == 0 && head[0] == 0) { | |
51 sequences.push_back(static_cast<size_t>(head - buffer)); | |
52 head += 4; | |
53 } else { | |
54 head++; | |
55 } | |
56 } | |
57 | |
58 return sequences; | |
59 } | |
60 } // namespace | |
61 | |
62 // Parses RBSP from source bytes. Removes emulation bytes, but leaves the | |
63 // rbsp_trailing_bits() in the stream, since none of the parsing reads all the | |
64 // way to the end of a parsed RBSP sequence. When writing, that means the | |
65 // rbsp_trailing_bits() should be preserved and don't need to be restored (i.e. | |
66 // the rbsp_stop_one_bit, which is just a 1, then zero padded), and alignment | |
67 // should "just work". | |
68 // TODO(pbos): Make parsing RBSP something that can be integrated into BitBuffer | |
69 // so we don't have to copy the entire frames when only interested in the | |
70 // headers. | |
71 rtc::ByteBufferWriter* ParseRbsp(const uint8_t* bytes, size_t length) { | |
72 // Copied from webrtc::H264SpsParser::Parse. | |
73 rtc::ByteBufferWriter* rbsp_buffer = new rtc::ByteBufferWriter(); | |
74 for (size_t i = 0; i < length;) { | |
75 if (length - i >= 3 && bytes[i] == 0 && bytes[i + 1] == 0 && | |
76 bytes[i + 2] == 3) { | |
77 rbsp_buffer->WriteBytes(reinterpret_cast<const char*>(bytes) + i, 2); | |
78 i += 3; | |
79 } else { | |
80 rbsp_buffer->WriteBytes(reinterpret_cast<const char*>(bytes) + i, 1); | |
81 i++; | |
82 } | |
83 } | |
84 return rbsp_buffer; | |
85 } | |
86 | 23 |
87 #define RETURN_FALSE_ON_FAIL(x) \ | 24 #define RETURN_FALSE_ON_FAIL(x) \ |
88 if (!(x)) { \ | 25 if (!(x)) { \ |
89 LOG_F(LS_ERROR) << "FAILED: " #x; \ | 26 LOG_F(LS_ERROR) << "FAILED: " #x; \ |
90 return false; \ | 27 return false; \ |
91 } | 28 } |
92 | 29 |
93 H264BitstreamParser::PpsState::PpsState() {} | 30 H264BitstreamParser::H264BitstreamParser() {} |
94 | 31 H264BitstreamParser::~H264BitstreamParser() {} |
95 H264BitstreamParser::SpsState::SpsState() {} | |
96 | |
97 // These functions are similar to webrtc::H264SpsParser::Parse, and based on the | |
98 // same version of the H.264 standard. You can find it here: | |
99 // http://www.itu.int/rec/T-REC-H.264 | |
100 bool H264BitstreamParser::ParseSpsNalu(const uint8_t* sps, size_t length) { | |
101 // Reset SPS state. | |
102 sps_ = SpsState(); | |
103 sps_parsed_ = false; | |
104 // Parse out the SPS RBSP. It should be small, so it's ok that we create a | |
105 // copy. We'll eventually write this back. | |
106 std::unique_ptr<rtc::ByteBufferWriter> sps_rbsp( | |
107 ParseRbsp(sps + kNaluHeaderAndTypeSize, length - kNaluHeaderAndTypeSize)); | |
108 rtc::BitBuffer sps_parser(reinterpret_cast<const uint8_t*>(sps_rbsp->Data()), | |
109 sps_rbsp->Length()); | |
110 | |
111 uint8_t byte_tmp; | |
112 uint32_t golomb_tmp; | |
113 uint32_t bits_tmp; | |
114 | |
115 // profile_idc: u(8). | |
116 uint8_t profile_idc; | |
117 RETURN_FALSE_ON_FAIL(sps_parser.ReadUInt8(&profile_idc)); | |
118 // constraint_set0_flag through constraint_set5_flag + reserved_zero_2bits | |
119 // 1 bit each for the flags + 2 bits = 8 bits = 1 byte. | |
120 RETURN_FALSE_ON_FAIL(sps_parser.ReadUInt8(&byte_tmp)); | |
121 // level_idc: u(8) | |
122 RETURN_FALSE_ON_FAIL(sps_parser.ReadUInt8(&byte_tmp)); | |
123 // seq_parameter_set_id: ue(v) | |
124 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); | |
125 sps_.separate_colour_plane_flag = 0; | |
126 // See if profile_idc has chroma format information. | |
127 if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 || | |
128 profile_idc == 244 || profile_idc == 44 || profile_idc == 83 || | |
129 profile_idc == 86 || profile_idc == 118 || profile_idc == 128 || | |
130 profile_idc == 138 || profile_idc == 139 || profile_idc == 134) { | |
131 // chroma_format_idc: ue(v) | |
132 uint32_t chroma_format_idc; | |
133 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&chroma_format_idc)); | |
134 if (chroma_format_idc == 3) { | |
135 // separate_colour_plane_flag: u(1) | |
136 RETURN_FALSE_ON_FAIL( | |
137 sps_parser.ReadBits(&sps_.separate_colour_plane_flag, 1)); | |
138 } | |
139 // bit_depth_luma_minus8: ue(v) | |
140 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); | |
141 // bit_depth_chroma_minus8: ue(v) | |
142 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); | |
143 // qpprime_y_zero_transform_bypass_flag: u(1) | |
144 RETURN_FALSE_ON_FAIL(sps_parser.ReadBits(&bits_tmp, 1)); | |
145 // seq_scaling_matrix_present_flag: u(1) | |
146 uint32_t seq_scaling_matrix_present_flag; | |
147 RETURN_FALSE_ON_FAIL( | |
148 sps_parser.ReadBits(&seq_scaling_matrix_present_flag, 1)); | |
149 if (seq_scaling_matrix_present_flag) { | |
150 // seq_scaling_list_present_flags. Either 8 or 12, depending on | |
151 // chroma_format_idc. | |
152 uint32_t seq_scaling_list_present_flags; | |
153 if (chroma_format_idc != 3) { | |
154 RETURN_FALSE_ON_FAIL( | |
155 sps_parser.ReadBits(&seq_scaling_list_present_flags, 8)); | |
156 } else { | |
157 RETURN_FALSE_ON_FAIL( | |
158 sps_parser.ReadBits(&seq_scaling_list_present_flags, 12)); | |
159 } | |
160 // TODO(pbos): Support parsing scaling lists if they're seen in practice. | |
161 RTC_CHECK(seq_scaling_list_present_flags == 0) | |
162 << "SPS contains scaling lists, which are unsupported."; | |
163 } | |
164 } | |
165 // log2_max_frame_num_minus4: ue(v) | |
166 RETURN_FALSE_ON_FAIL( | |
167 sps_parser.ReadExponentialGolomb(&sps_.log2_max_frame_num_minus4)); | |
168 // pic_order_cnt_type: ue(v) | |
169 RETURN_FALSE_ON_FAIL( | |
170 sps_parser.ReadExponentialGolomb(&sps_.pic_order_cnt_type)); | |
171 | |
172 if (sps_.pic_order_cnt_type == 0) { | |
173 // log2_max_pic_order_cnt_lsb_minus4: ue(v) | |
174 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb( | |
175 &sps_.log2_max_pic_order_cnt_lsb_minus4)); | |
176 } else if (sps_.pic_order_cnt_type == 1) { | |
177 // delta_pic_order_always_zero_flag: u(1) | |
178 RETURN_FALSE_ON_FAIL( | |
179 sps_parser.ReadBits(&sps_.delta_pic_order_always_zero_flag, 1)); | |
180 // offset_for_non_ref_pic: se(v) | |
181 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); | |
182 // offset_for_top_to_bottom_field: se(v) | |
183 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); | |
184 uint32_t num_ref_frames_in_pic_order_cnt_cycle; | |
185 // num_ref_frames_in_pic_order_cnt_cycle: ue(v) | |
186 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb( | |
187 &num_ref_frames_in_pic_order_cnt_cycle)); | |
188 for (uint32_t i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++) { | |
189 // offset_for_ref_frame[i]: se(v) | |
190 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); | |
191 } | |
192 } | |
193 // max_num_ref_frames: ue(v) | |
194 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); | |
195 // gaps_in_frame_num_value_allowed_flag: u(1) | |
196 RETURN_FALSE_ON_FAIL(sps_parser.ReadBits(&bits_tmp, 1)); | |
197 // pic_width_in_mbs_minus1: ue(v) | |
198 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); | |
199 // pic_height_in_map_units_minus1: ue(v) | |
200 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); | |
201 // frame_mbs_only_flag: u(1) | |
202 RETURN_FALSE_ON_FAIL(sps_parser.ReadBits(&sps_.frame_mbs_only_flag, 1)); | |
203 sps_parsed_ = true; | |
204 return true; | |
205 } | |
206 | |
207 bool H264BitstreamParser::ParsePpsNalu(const uint8_t* pps, size_t length) { | |
208 RTC_CHECK(sps_parsed_); | |
209 // We're starting a new stream, so reset picture type rewriting values. | |
210 pps_ = PpsState(); | |
211 pps_parsed_ = false; | |
212 std::unique_ptr<rtc::ByteBufferWriter> buffer( | |
213 ParseRbsp(pps + kNaluHeaderAndTypeSize, length - kNaluHeaderAndTypeSize)); | |
214 rtc::BitBuffer parser(reinterpret_cast<const uint8_t*>(buffer->Data()), | |
215 buffer->Length()); | |
216 | |
217 uint32_t bits_tmp; | |
218 uint32_t golomb_ignored; | |
219 // pic_parameter_set_id: ue(v) | |
220 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); | |
221 // seq_parameter_set_id: ue(v) | |
222 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); | |
223 // entropy_coding_mode_flag: u(1) | |
224 uint32_t entropy_coding_mode_flag; | |
225 RETURN_FALSE_ON_FAIL(parser.ReadBits(&entropy_coding_mode_flag, 1)); | |
226 // TODO(pbos): Implement CABAC support if spotted in the wild. | |
227 RTC_CHECK(entropy_coding_mode_flag == 0) | |
228 << "Don't know how to parse CABAC streams."; | |
229 // bottom_field_pic_order_in_frame_present_flag: u(1) | |
230 uint32_t bottom_field_pic_order_in_frame_present_flag; | |
231 RETURN_FALSE_ON_FAIL( | |
232 parser.ReadBits(&bottom_field_pic_order_in_frame_present_flag, 1)); | |
233 pps_.bottom_field_pic_order_in_frame_present_flag = | |
234 bottom_field_pic_order_in_frame_present_flag != 0; | |
235 | |
236 // num_slice_groups_minus1: ue(v) | |
237 uint32_t num_slice_groups_minus1; | |
238 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&num_slice_groups_minus1)); | |
239 if (num_slice_groups_minus1 > 0) { | |
240 uint32_t slice_group_map_type; | |
241 // slice_group_map_type: ue(v) | |
242 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&slice_group_map_type)); | |
243 if (slice_group_map_type == 0) { | |
244 for (uint32_t i_group = 0; i_group <= num_slice_groups_minus1; | |
245 ++i_group) { | |
246 // run_length_minus1[iGroup]: ue(v) | |
247 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); | |
248 } | |
249 } else if (slice_group_map_type == 2) { | |
250 for (uint32_t i_group = 0; i_group <= num_slice_groups_minus1; | |
251 ++i_group) { | |
252 // top_left[iGroup]: ue(v) | |
253 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); | |
254 // bottom_right[iGroup]: ue(v) | |
255 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); | |
256 } | |
257 } else if (slice_group_map_type == 3 || slice_group_map_type == 4 || | |
258 slice_group_map_type == 5) { | |
259 // slice_group_change_direction_flag: u(1) | |
260 RETURN_FALSE_ON_FAIL(parser.ReadBits(&bits_tmp, 1)); | |
261 // slice_group_change_rate_minus1: ue(v) | |
262 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); | |
263 } else if (slice_group_map_type == 6) { | |
264 // pic_size_in_map_units_minus1: ue(v) | |
265 uint32_t pic_size_in_map_units_minus1; | |
266 RETURN_FALSE_ON_FAIL( | |
267 parser.ReadExponentialGolomb(&pic_size_in_map_units_minus1)); | |
268 uint32_t slice_group_id_bits = 0; | |
269 uint32_t num_slice_groups = num_slice_groups_minus1 + 1; | |
270 // If num_slice_groups is not a power of two an additional bit is required | |
271 // to account for the ceil() of log2() below. | |
272 if ((num_slice_groups & (num_slice_groups - 1)) != 0) | |
273 ++slice_group_id_bits; | |
274 while (num_slice_groups > 0) { | |
275 num_slice_groups >>= 1; | |
276 ++slice_group_id_bits; | |
277 } | |
278 for (uint32_t i = 0; i <= pic_size_in_map_units_minus1; i++) { | |
279 // slice_group_id[i]: u(v) | |
280 // Represented by ceil(log2(num_slice_groups_minus1 + 1)) bits. | |
281 RETURN_FALSE_ON_FAIL(parser.ReadBits(&bits_tmp, slice_group_id_bits)); | |
282 } | |
283 } | |
284 } | |
285 // num_ref_idx_l0_default_active_minus1: ue(v) | |
286 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); | |
287 // num_ref_idx_l1_default_active_minus1: ue(v) | |
288 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); | |
289 // weighted_pred_flag: u(1) | |
290 uint32_t weighted_pred_flag; | |
291 RETURN_FALSE_ON_FAIL(parser.ReadBits(&weighted_pred_flag, 1)); | |
292 pps_.weighted_pred_flag = weighted_pred_flag != 0; | |
293 // weighted_bipred_idc: u(2) | |
294 RETURN_FALSE_ON_FAIL(parser.ReadBits(&pps_.weighted_bipred_idc, 2)); | |
295 | |
296 // pic_init_qp_minus26: se(v) | |
297 RETURN_FALSE_ON_FAIL( | |
298 parser.ReadSignedExponentialGolomb(&pps_.pic_init_qp_minus26)); | |
299 // pic_init_qs_minus26: se(v) | |
300 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); | |
301 // chroma_qp_index_offset: se(v) | |
302 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); | |
303 // deblocking_filter_control_present_flag: u(1) | |
304 // constrained_intra_pred_flag: u(1) | |
305 RETURN_FALSE_ON_FAIL(parser.ReadBits(&bits_tmp, 2)); | |
306 // redundant_pic_cnt_present_flag: u(1) | |
307 RETURN_FALSE_ON_FAIL( | |
308 parser.ReadBits(&pps_.redundant_pic_cnt_present_flag, 1)); | |
309 | |
310 pps_parsed_ = true; | |
311 return true; | |
312 } | |
313 | 32 |
314 bool H264BitstreamParser::ParseNonParameterSetNalu(const uint8_t* source, | 33 bool H264BitstreamParser::ParseNonParameterSetNalu(const uint8_t* source, |
315 size_t source_length, | 34 size_t source_length, |
316 uint8_t nalu_type) { | 35 uint8_t nalu_type) { |
317 RTC_CHECK(sps_parsed_); | 36 RTC_CHECK(sps_); |
318 RTC_CHECK(pps_parsed_); | 37 RTC_CHECK(pps_); |
319 last_slice_qp_delta_parsed_ = false; | 38 last_slice_qp_delta_ = rtc::Optional<int32_t>(); |
320 std::unique_ptr<rtc::ByteBufferWriter> slice_rbsp(ParseRbsp( | 39 std::unique_ptr<rtc::Buffer> slice_rbsp( |
321 source + kNaluHeaderAndTypeSize, source_length - kNaluHeaderAndTypeSize)); | 40 H264::ParseRbsp(source, source_length)); |
322 rtc::BitBuffer slice_reader( | 41 rtc::BitBuffer slice_reader(slice_rbsp->data() + H264::kNaluTypeSize, |
323 reinterpret_cast<const uint8_t*>(slice_rbsp->Data()), | 42 slice_rbsp->size() - H264::kNaluTypeSize); |
324 slice_rbsp->Length()); | |
325 // Check to see if this is an IDR slice, which has an extra field to parse | 43 // Check to see if this is an IDR slice, which has an extra field to parse |
326 // out. | 44 // out. |
327 bool is_idr = (source[kNaluHeaderSize] & 0x0F) == kNaluIdr; | 45 bool is_idr = (source[0] & 0x0F) == H264::NaluType::kIdr; |
328 uint8_t nal_ref_idc = (source[kNaluHeaderSize] & 0x60) >> 5; | 46 uint8_t nal_ref_idc = (source[0] & 0x60) >> 5; |
329 uint32_t golomb_tmp; | 47 uint32_t golomb_tmp; |
330 uint32_t bits_tmp; | 48 uint32_t bits_tmp; |
331 | 49 |
332 // first_mb_in_slice: ue(v) | 50 // first_mb_in_slice: ue(v) |
333 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); | 51 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); |
334 // slice_type: ue(v) | 52 // slice_type: ue(v) |
335 uint32_t slice_type; | 53 uint32_t slice_type; |
336 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&slice_type)); | 54 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&slice_type)); |
337 // slice_type's 5..9 range is used to indicate that all slices of a picture | 55 // slice_type's 5..9 range is used to indicate that all slices of a picture |
338 // have the same value of slice_type % 5, we don't care about that, so we map | 56 // have the same value of slice_type % 5, we don't care about that, so we map |
339 // to the corresponding 0..4 range. | 57 // to the corresponding 0..4 range. |
340 slice_type %= 5; | 58 slice_type %= 5; |
341 // pic_parameter_set_id: ue(v) | 59 // pic_parameter_set_id: ue(v) |
342 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); | 60 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); |
343 if (sps_.separate_colour_plane_flag == 1) { | 61 if (sps_->separate_colour_plane_flag == 1) { |
344 // colour_plane_id | 62 // colour_plane_id |
345 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 2)); | 63 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 2)); |
346 } | 64 } |
347 // frame_num: u(v) | 65 // frame_num: u(v) |
348 // Represented by log2_max_frame_num_minus4 + 4 bits. | 66 // Represented by log2_max_frame_num_minus4 + 4 bits. |
349 RETURN_FALSE_ON_FAIL( | 67 RETURN_FALSE_ON_FAIL( |
350 slice_reader.ReadBits(&bits_tmp, sps_.log2_max_frame_num_minus4 + 4)); | 68 slice_reader.ReadBits(&bits_tmp, sps_->log2_max_frame_num_minus4 + 4)); |
351 uint32_t field_pic_flag = 0; | 69 uint32_t field_pic_flag = 0; |
352 if (sps_.frame_mbs_only_flag == 0) { | 70 if (sps_->frame_mbs_only_flag == 0) { |
353 // field_pic_flag: u(1) | 71 // field_pic_flag: u(1) |
354 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&field_pic_flag, 1)); | 72 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&field_pic_flag, 1)); |
355 if (field_pic_flag != 0) { | 73 if (field_pic_flag != 0) { |
356 // bottom_field_flag: u(1) | 74 // bottom_field_flag: u(1) |
357 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 1)); | 75 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 1)); |
358 } | 76 } |
359 } | 77 } |
360 if (is_idr) { | 78 if (is_idr) { |
361 // idr_pic_id: ue(v) | 79 // idr_pic_id: ue(v) |
362 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); | 80 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); |
363 } | 81 } |
364 // pic_order_cnt_lsb: u(v) | 82 // pic_order_cnt_lsb: u(v) |
365 // Represented by sps_.log2_max_pic_order_cnt_lsb_minus4 + 4 bits. | 83 // Represented by sps_.log2_max_pic_order_cnt_lsb_minus4 + 4 bits. |
366 if (sps_.pic_order_cnt_type == 0) { | 84 if (sps_->pic_order_cnt_type == 0) { |
367 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits( | 85 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits( |
368 &bits_tmp, sps_.log2_max_pic_order_cnt_lsb_minus4 + 4)); | 86 &bits_tmp, sps_->log2_max_pic_order_cnt_lsb_minus4 + 4)); |
369 if (pps_.bottom_field_pic_order_in_frame_present_flag && | 87 if (pps_->bottom_field_pic_order_in_frame_present_flag && |
370 field_pic_flag == 0) { | 88 field_pic_flag == 0) { |
371 // delta_pic_order_cnt_bottom: se(v) | 89 // delta_pic_order_cnt_bottom: se(v) |
372 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); | 90 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); |
373 } | 91 } |
374 } | 92 } |
375 if (sps_.pic_order_cnt_type == 1 && !sps_.delta_pic_order_always_zero_flag) { | 93 if (sps_->pic_order_cnt_type == 1 && |
| 94 !sps_->delta_pic_order_always_zero_flag) { |
376 // delta_pic_order_cnt[0]: se(v) | 95 // delta_pic_order_cnt[0]: se(v) |
377 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); | 96 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); |
378 if (pps_.bottom_field_pic_order_in_frame_present_flag && !field_pic_flag) { | 97 if (pps_->bottom_field_pic_order_in_frame_present_flag && !field_pic_flag) { |
379 // delta_pic_order_cnt[1]: se(v) | 98 // delta_pic_order_cnt[1]: se(v) |
380 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); | 99 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); |
381 } | 100 } |
382 } | 101 } |
383 if (pps_.redundant_pic_cnt_present_flag) { | 102 if (pps_->redundant_pic_cnt_present_flag) { |
384 // redundant_pic_cnt: ue(v) | 103 // redundant_pic_cnt: ue(v) |
385 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); | 104 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); |
386 } | 105 } |
387 if (slice_type == kSliceTypeB) { | 106 if (slice_type == H264::SliceType::kB) { |
388 // direct_spatial_mv_pred_flag: u(1) | 107 // direct_spatial_mv_pred_flag: u(1) |
389 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 1)); | 108 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 1)); |
390 } | 109 } |
391 if (slice_type == kSliceTypeP || slice_type == kSliceTypeSp || | 110 switch (slice_type) { |
392 slice_type == kSliceTypeB) { | 111 case H264::SliceType::kP: |
393 uint32_t num_ref_idx_active_override_flag; | 112 case H264::SliceType::kB: |
394 // num_ref_idx_active_override_flag: u(1) | 113 case H264::SliceType::kSp: |
395 RETURN_FALSE_ON_FAIL( | 114 uint32_t num_ref_idx_active_override_flag; |
396 slice_reader.ReadBits(&num_ref_idx_active_override_flag, 1)); | 115 // num_ref_idx_active_override_flag: u(1) |
397 if (num_ref_idx_active_override_flag != 0) { | 116 RETURN_FALSE_ON_FAIL( |
398 // num_ref_idx_l0_active_minus1: ue(v) | 117 slice_reader.ReadBits(&num_ref_idx_active_override_flag, 1)); |
399 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); | 118 if (num_ref_idx_active_override_flag != 0) { |
400 if (slice_type == kSliceTypeB) { | 119 // num_ref_idx_l0_active_minus1: ue(v) |
401 // num_ref_idx_l1_active_minus1: ue(v) | |
402 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); | 120 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); |
| 121 if (slice_type == H264::SliceType::kB) { |
| 122 // num_ref_idx_l1_active_minus1: ue(v) |
| 123 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); |
| 124 } |
403 } | 125 } |
404 } | 126 break; |
| 127 default: |
| 128 break; |
405 } | 129 } |
406 // assume nal_unit_type != 20 && nal_unit_type != 21: | 130 // assume nal_unit_type != 20 && nal_unit_type != 21: |
407 RTC_CHECK_NE(nalu_type, 20); | 131 RTC_CHECK_NE(nalu_type, 20); |
408 RTC_CHECK_NE(nalu_type, 21); | 132 RTC_CHECK_NE(nalu_type, 21); |
409 // if (nal_unit_type == 20 || nal_unit_type == 21) | 133 // if (nal_unit_type == 20 || nal_unit_type == 21) |
410 // ref_pic_list_mvc_modification() | 134 // ref_pic_list_mvc_modification() |
411 // else | 135 // else |
412 { | 136 { |
413 // ref_pic_list_modification(): | 137 // ref_pic_list_modification(): |
414 // |slice_type| checks here don't use named constants as they aren't named | 138 // |slice_type| checks here don't use named constants as they aren't named |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 } else if (modification_of_pic_nums_idc == 2) { | 181 } else if (modification_of_pic_nums_idc == 2) { |
458 // long_term_pic_num: ue(v) | 182 // long_term_pic_num: ue(v) |
459 RETURN_FALSE_ON_FAIL( | 183 RETURN_FALSE_ON_FAIL( |
460 slice_reader.ReadExponentialGolomb(&golomb_tmp)); | 184 slice_reader.ReadExponentialGolomb(&golomb_tmp)); |
461 } | 185 } |
462 } while (modification_of_pic_nums_idc != 3); | 186 } while (modification_of_pic_nums_idc != 3); |
463 } | 187 } |
464 } | 188 } |
465 } | 189 } |
466 // TODO(pbos): Do we need support for pred_weight_table()? | 190 // TODO(pbos): Do we need support for pred_weight_table()? |
467 RTC_CHECK(!((pps_.weighted_pred_flag && | 191 RTC_CHECK( |
468 (slice_type == kSliceTypeP || slice_type == kSliceTypeSp)) || | 192 !((pps_->weighted_pred_flag && (slice_type == H264::SliceType::kP || |
469 (pps_.weighted_bipred_idc != 0 && slice_type == kSliceTypeB))) | 193 slice_type == H264::SliceType::kSp)) || |
| 194 (pps_->weighted_bipred_idc != 0 && slice_type == H264::SliceType::kB))) |
470 << "Missing support for pred_weight_table()."; | 195 << "Missing support for pred_weight_table()."; |
471 // if ((weighted_pred_flag && (slice_type == P || slice_type == SP)) || | 196 // if ((weighted_pred_flag && (slice_type == P || slice_type == SP)) || |
472 // (weighted_bipred_idc == 1 && slice_type == B)) { | 197 // (weighted_bipred_idc == 1 && slice_type == B)) { |
473 // pred_weight_table() | 198 // pred_weight_table() |
474 // } | 199 // } |
475 if (nal_ref_idc != 0) { | 200 if (nal_ref_idc != 0) { |
476 // dec_ref_pic_marking(): | 201 // dec_ref_pic_marking(): |
477 if (is_idr) { | 202 if (is_idr) { |
478 // no_output_of_prior_pics_flag: u(1) | 203 // no_output_of_prior_pics_flag: u(1) |
479 // long_term_reference_flag: u(1) | 204 // long_term_reference_flag: u(1) |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
511 RETURN_FALSE_ON_FAIL( | 236 RETURN_FALSE_ON_FAIL( |
512 slice_reader.ReadExponentialGolomb(&golomb_tmp)); | 237 slice_reader.ReadExponentialGolomb(&golomb_tmp)); |
513 } | 238 } |
514 } while (memory_management_control_operation != 0); | 239 } while (memory_management_control_operation != 0); |
515 } | 240 } |
516 } | 241 } |
517 } | 242 } |
518 // cabac not supported: entropy_coding_mode_flag == 0 asserted above. | 243 // cabac not supported: entropy_coding_mode_flag == 0 asserted above. |
519 // if (entropy_coding_mode_flag && slice_type != I && slice_type != SI) | 244 // if (entropy_coding_mode_flag && slice_type != I && slice_type != SI) |
520 // cabac_init_idc | 245 // cabac_init_idc |
| 246 int32_t last_slice_qp_delta; |
521 RETURN_FALSE_ON_FAIL( | 247 RETURN_FALSE_ON_FAIL( |
522 slice_reader.ReadSignedExponentialGolomb(&last_slice_qp_delta_)); | 248 slice_reader.ReadSignedExponentialGolomb(&last_slice_qp_delta)); |
523 last_slice_qp_delta_parsed_ = true; | 249 last_slice_qp_delta_ = rtc::Optional<int32_t>(last_slice_qp_delta); |
524 return true; | 250 return true; |
525 } | 251 } |
526 | 252 |
527 void H264BitstreamParser::ParseSlice(const uint8_t* slice, size_t length) { | 253 void H264BitstreamParser::ParseSlice(const uint8_t* slice, size_t length) { |
528 uint8_t nalu_type = slice[4] & kNaluTypeMask; | 254 H264::NaluType nalu_type = H264::ParseNaluType(slice[0]); |
529 switch (nalu_type) { | 255 switch (nalu_type) { |
530 case kNaluSps: | 256 case H264::NaluType::kSps: { |
531 RTC_CHECK(ParseSpsNalu(slice, length)) | 257 sps_ = SpsParser::ParseSps(slice + H264::kNaluTypeSize, |
532 << "Failed to parse bitstream SPS."; | 258 length - H264::kNaluTypeSize); |
| 259 if (!sps_) |
| 260 FATAL() << "Unable to parse SPS from H264 bitstream."; |
533 break; | 261 break; |
534 case kNaluPps: | 262 } |
535 RTC_CHECK(ParsePpsNalu(slice, length)) | 263 case H264::NaluType::kPps: { |
536 << "Failed to parse bitstream PPS."; | 264 pps_ = PpsParser::ParsePps(slice + H264::kNaluTypeSize, |
| 265 length - H264::kNaluTypeSize); |
| 266 if (!pps_) |
| 267 FATAL() << "Unable to parse PPS from H264 bitstream."; |
537 break; | 268 break; |
| 269 } |
538 default: | 270 default: |
539 RTC_CHECK(ParseNonParameterSetNalu(slice, length, nalu_type)) | 271 RTC_CHECK(ParseNonParameterSetNalu(slice, length, nalu_type)) |
540 << "Failed to parse picture slice."; | 272 << "Failed to parse picture slice."; |
541 break; | 273 break; |
542 } | 274 } |
543 } | 275 } |
544 | 276 |
545 void H264BitstreamParser::ParseBitstream(const uint8_t* bitstream, | 277 void H264BitstreamParser::ParseBitstream(const uint8_t* bitstream, |
546 size_t length) { | 278 size_t length) { |
547 RTC_CHECK_GE(length, 4u); | 279 std::vector<H264::NaluIndex> nalu_indices = |
548 std::vector<size_t> slice_markers = FindNaluStartSequences(bitstream, length); | 280 H264::FindNaluIndices(bitstream, length); |
549 RTC_CHECK(!slice_markers.empty()); | 281 RTC_CHECK(!nalu_indices.empty()); |
550 for (size_t i = 0; i < slice_markers.size() - 1; ++i) { | 282 for (const H264::NaluIndex& index : nalu_indices) |
551 ParseSlice(bitstream + slice_markers[i], | 283 ParseSlice(&bitstream[index.payload_start_offset], index.payload_size); |
552 slice_markers[i + 1] - slice_markers[i]); | |
553 } | |
554 // Parse the last slice. | |
555 ParseSlice(bitstream + slice_markers.back(), length - slice_markers.back()); | |
556 } | 284 } |
557 | 285 |
558 bool H264BitstreamParser::GetLastSliceQp(int* qp) const { | 286 bool H264BitstreamParser::GetLastSliceQp(int* qp) const { |
559 if (!last_slice_qp_delta_parsed_) | 287 if (!last_slice_qp_delta_ || !pps_) |
560 return false; | 288 return false; |
561 *qp = 26 + pps_.pic_init_qp_minus26 + last_slice_qp_delta_; | 289 *qp = 26 + pps_->pic_init_qp_minus26 + *last_slice_qp_delta_; |
562 return true; | 290 return true; |
563 } | 291 } |
564 | 292 |
565 } // namespace webrtc | 293 } // namespace webrtc |
OLD | NEW |