OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | |
3 * | |
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 | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 #include "webrtc/modules/rtp_rtcp/source/h264_bitstream_parser.h" | |
11 | |
12 #include <vector> | |
13 | |
14 #include "webrtc/base/bitbuffer.h" | |
15 #include "webrtc/base/bytebuffer.h" | |
16 #include "webrtc/base/checks.h" | |
17 #include "webrtc/base/logging.h" | |
18 #include "webrtc/base/scoped_ptr.h" | |
19 | |
20 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::ByteBuffer* ParseRbsp(const uint8_t* bytes, size_t length) { | |
72 // Copied from webrtc::H264SpsParser::Parse. | |
73 rtc::ByteBuffer* rbsp_buffer = new rtc::ByteBuffer; | |
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 | |
87 #define RETURN_FALSE_ON_FAIL(x) \ | |
88 if (!(x)) { \ | |
89 LOG_F(LS_ERROR) << "FAILED: " #x; \ | |
90 return false; \ | |
91 } | |
92 | |
93 H264BitstreamParser::PpsState::PpsState() { | |
94 } | |
95 | |
96 H264BitstreamParser::SpsState::SpsState() { | |
97 } | |
98 | |
99 // These functions are similar to webrtc::H264SpsParser::Parse, and based on the | |
100 // same version of the H.264 standard. You can find it here: | |
101 // http://www.itu.int/rec/T-REC-H.264 | |
102 bool H264BitstreamParser::ParseSpsNalu(const uint8_t* sps, size_t length) { | |
103 // Reset SPS state. | |
104 sps_ = SpsState(); | |
105 // Parse out the SPS RBSP. It should be small, so it's ok that we create a | |
106 // copy. We'll eventually write this back. | |
107 rtc::scoped_ptr<rtc::ByteBuffer> sps_rbsp( | |
108 ParseRbsp(sps + kNaluHeaderAndTypeSize, length - kNaluHeaderAndTypeSize)); | |
109 rtc::BitBuffer sps_parser(reinterpret_cast<const uint8*>(sps_rbsp->Data()), | |
110 sps_rbsp->Length()); | |
111 | |
112 uint8_t byte_tmp; | |
113 uint32_t golomb_tmp; | |
114 uint32_t bits_tmp; | |
115 | |
116 // profile_idc: u(8). | |
117 uint8 profile_idc; | |
118 RETURN_FALSE_ON_FAIL(sps_parser.ReadUInt8(&profile_idc)); | |
119 // constraint_set0_flag through constraint_set5_flag + reserved_zero_2bits | |
120 // 1 bit each for the flags + 2 bits = 8 bits = 1 byte. | |
121 RETURN_FALSE_ON_FAIL(sps_parser.ReadUInt8(&byte_tmp)); | |
122 // level_idc: u(8) | |
123 RETURN_FALSE_ON_FAIL(sps_parser.ReadUInt8(&byte_tmp)); | |
124 // seq_parameter_set_id: ue(v) | |
125 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); | |
126 sps_.separate_colour_plane_flag = 0; | |
127 // See if profile_idc has chroma format information. | |
128 if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 || | |
129 profile_idc == 244 || profile_idc == 44 || profile_idc == 83 || | |
130 profile_idc == 86 || profile_idc == 118 || profile_idc == 128 || | |
131 profile_idc == 138 || profile_idc == 139 || profile_idc == 134) { | |
132 // chroma_format_idc: ue(v) | |
133 uint32 chroma_format_idc; | |
134 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&chroma_format_idc)); | |
135 if (chroma_format_idc == 3) { | |
136 // separate_colour_plane_flag: u(1) | |
137 RETURN_FALSE_ON_FAIL( | |
138 sps_parser.ReadBits(&sps_.separate_colour_plane_flag, 1)); | |
139 } | |
140 // bit_depth_luma_minus8: ue(v) | |
141 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); | |
142 // bit_depth_chroma_minus8: ue(v) | |
143 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); | |
144 // qpprime_y_zero_transform_bypass_flag: u(1) | |
145 RETURN_FALSE_ON_FAIL(sps_parser.ReadBits(&bits_tmp, 1)); | |
146 // seq_scaling_matrix_present_flag: u(1) | |
147 uint32_t seq_scaling_matrix_present_flag; | |
148 RETURN_FALSE_ON_FAIL( | |
149 sps_parser.ReadBits(&seq_scaling_matrix_present_flag, 1)); | |
150 if (seq_scaling_matrix_present_flag) { | |
151 // seq_scaling_list_present_flags. Either 8 or 12, depending on | |
152 // chroma_format_idc. | |
153 uint32_t seq_scaling_list_present_flags; | |
154 if (chroma_format_idc != 3) { | |
155 RETURN_FALSE_ON_FAIL( | |
156 sps_parser.ReadBits(&seq_scaling_list_present_flags, 8)); | |
157 } else { | |
158 RETURN_FALSE_ON_FAIL( | |
159 sps_parser.ReadBits(&seq_scaling_list_present_flags, 12)); | |
160 } | |
161 // TODO(pbos): Support parsing scaling lists if they're seen in practice. | |
162 RTC_CHECK(seq_scaling_list_present_flags == 0) | |
163 << "SPS contains scaling lists, which are unsupported."; | |
164 } | |
165 } | |
166 // log2_max_frame_num_minus4: ue(v) | |
167 RETURN_FALSE_ON_FAIL( | |
168 sps_parser.ReadExponentialGolomb(&sps_.log2_max_frame_num_minus4)); | |
169 // pic_order_cnt_type: ue(v) | |
170 RETURN_FALSE_ON_FAIL( | |
171 sps_parser.ReadExponentialGolomb(&sps_.pic_order_cnt_type)); | |
172 | |
173 if (sps_.pic_order_cnt_type == 0) { | |
174 // log2_max_pic_order_cnt_lsb_minus4: ue(v) | |
175 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb( | |
176 &sps_.log2_max_pic_order_cnt_lsb_minus4)); | |
177 } else if (sps_.pic_order_cnt_type == 1) { | |
178 // delta_pic_order_always_zero_flag: u(1) | |
179 RETURN_FALSE_ON_FAIL( | |
180 sps_parser.ReadBits(&sps_.delta_pic_order_always_zero_flag, 1)); | |
181 // offset_for_non_ref_pic: se(v) | |
182 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); | |
183 // offset_for_top_to_bottom_field: se(v) | |
184 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); | |
185 uint32_t num_ref_frames_in_pic_order_cnt_cycle; | |
186 // num_ref_frames_in_pic_order_cnt_cycle: ue(v) | |
187 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb( | |
188 &num_ref_frames_in_pic_order_cnt_cycle)); | |
189 for (uint32_t i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++) { | |
190 // offset_for_ref_frame[i]: se(v) | |
191 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); | |
192 } | |
193 } | |
194 // max_num_ref_frames: ue(v) | |
195 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); | |
196 // gaps_in_frame_num_value_allowed_flag: u(1) | |
197 RETURN_FALSE_ON_FAIL(sps_parser.ReadBits(&bits_tmp, 1)); | |
198 // pic_width_in_mbs_minus1: ue(v) | |
199 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); | |
200 // pic_height_in_map_units_minus1: ue(v) | |
201 RETURN_FALSE_ON_FAIL(sps_parser.ReadExponentialGolomb(&golomb_tmp)); | |
202 // frame_mbs_only_flag: u(1) | |
203 RETURN_FALSE_ON_FAIL(sps_parser.ReadBits(&sps_.frame_mbs_only_flag, 1)); | |
204 return true; | |
205 } | |
206 | |
207 bool H264BitstreamParser::ParsePpsNalu(const uint8_t* pps, size_t length) { | |
208 // We're starting a new stream, so reset picture type rewriting values. | |
209 pps_ = PpsState(); | |
210 rtc::scoped_ptr<rtc::ByteBuffer> buffer( | |
211 ParseRbsp(pps + kNaluHeaderAndTypeSize, length - kNaluHeaderAndTypeSize)); | |
212 rtc::BitBuffer parser(reinterpret_cast<const uint8*>(buffer->Data()), | |
213 buffer->Length()); | |
214 | |
215 uint32_t bits_tmp; | |
216 uint32_t golomb_ignored; | |
217 // pic_parameter_set_id: ue(v) | |
218 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); | |
219 // seq_parameter_set_id: ue(v) | |
220 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); | |
221 // entropy_coding_mode_flag: u(1) | |
222 uint32_t entropy_coding_mode_flag; | |
223 RETURN_FALSE_ON_FAIL(parser.ReadBits(&entropy_coding_mode_flag, 1)); | |
224 // TODO(pbos): Implement CABAC support if spotted in the wild. | |
225 RTC_CHECK(entropy_coding_mode_flag == 0) | |
226 << "Don't know how to parse CABAC streams."; | |
227 // bottom_field_pic_order_in_frame_present_flag: u(1) | |
228 uint32_t bottom_field_pic_order_in_frame_present_flag; | |
229 RETURN_FALSE_ON_FAIL( | |
230 parser.ReadBits(&bottom_field_pic_order_in_frame_present_flag, 1)); | |
231 pps_.bottom_field_pic_order_in_frame_present_flag = | |
232 bottom_field_pic_order_in_frame_present_flag != 0; | |
233 | |
234 // num_slice_groups_minus1: ue(v) | |
235 uint32_t num_slice_groups_minus1; | |
236 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&num_slice_groups_minus1)); | |
237 if (num_slice_groups_minus1 > 0) { | |
238 uint32_t slice_group_map_type; | |
239 // slice_group_map_type: ue(v) | |
240 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&slice_group_map_type)); | |
241 if (slice_group_map_type == 0) { | |
242 for (uint32_t i_group = 0; i_group <= num_slice_groups_minus1; | |
243 ++i_group) { | |
244 // run_length_minus1[iGroup]: ue(v) | |
245 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); | |
246 } | |
247 } else if (slice_group_map_type == 2) { | |
248 for (uint32_t i_group = 0; i_group <= num_slice_groups_minus1; | |
249 ++i_group) { | |
250 // top_left[iGroup]: ue(v) | |
251 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); | |
252 // bottom_right[iGroup]: ue(v) | |
253 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); | |
254 } | |
255 } else if (slice_group_map_type == 3 || slice_group_map_type == 4 || | |
256 slice_group_map_type == 5) { | |
257 // slice_group_change_direction_flag: u(1) | |
258 RETURN_FALSE_ON_FAIL(parser.ReadBits(&bits_tmp, 1)); | |
259 // slice_group_change_rate_minus1: ue(v) | |
260 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); | |
261 } else if (slice_group_map_type == 6) { | |
262 // pic_size_in_map_units_minus1: ue(v) | |
263 uint32_t pic_size_in_map_units_minus1; | |
264 RETURN_FALSE_ON_FAIL( | |
265 parser.ReadExponentialGolomb(&pic_size_in_map_units_minus1)); | |
266 uint32_t slice_group_id_bits = 0; | |
267 uint32_t num_slice_groups = num_slice_groups_minus1 + 1; | |
268 // If num_slice_groups is not a power of two an additional bit is required | |
269 // to account for the ceil() of log2() below. | |
270 if ((num_slice_groups & (num_slice_groups - 1)) != 0) | |
271 ++slice_group_id_bits; | |
272 while (num_slice_groups > 0) { | |
273 num_slice_groups >>= 1; | |
274 ++slice_group_id_bits; | |
275 } | |
276 for (uint32_t i = 0; i <= pic_size_in_map_units_minus1; i++) { | |
277 // slice_group_id[i]: u(v) | |
278 // Represented by ceil(log2(num_slice_groups_minus1 + 1)) bits. | |
279 RETURN_FALSE_ON_FAIL(parser.ReadBits(&bits_tmp, slice_group_id_bits)); | |
280 } | |
281 } | |
282 } | |
283 // num_ref_idx_l0_default_active_minus1: ue(v) | |
284 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); | |
285 // num_ref_idx_l1_default_active_minus1: ue(v) | |
286 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); | |
287 // weighted_pred_flag: u(1) | |
288 // weighted_bipred_idc: u(2) | |
289 RETURN_FALSE_ON_FAIL(parser.ReadBits(&bits_tmp, 3)); | |
290 | |
291 // pic_init_qp_minus26: se(v) | |
292 RETURN_FALSE_ON_FAIL( | |
293 parser.ReadSignedExponentialGolomb(&pps_.pic_init_qp_minus26)); | |
294 // pic_init_qs_minus26: se(v) | |
295 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); | |
296 // chroma_qp_index_offset: se(v) | |
297 RETURN_FALSE_ON_FAIL(parser.ReadExponentialGolomb(&golomb_ignored)); | |
298 // deblocking_filter_control_present_flag: u(1) | |
noahric
2015/09/22 20:47:19
You can combine these two if you want (doesn't mak
pbos-webrtc
2015/09/23 12:29:43
Done.
| |
299 RETURN_FALSE_ON_FAIL(parser.ReadBits(&bits_tmp, 1)); | |
300 // constrained_intra_pred_flag: u(1) | |
301 RETURN_FALSE_ON_FAIL(parser.ReadBits(&bits_tmp, 1)); | |
302 // redundant_pic_cnt_present_flag: u(1) | |
303 RETURN_FALSE_ON_FAIL( | |
304 parser.ReadBits(&pps_.redundant_pic_cnt_present_flag, 1)); | |
305 | |
306 return true; | |
307 } | |
308 | |
309 bool H264BitstreamParser::ParseNonParameterSetNalu(const uint8_t* source, | |
310 size_t source_length) { | |
311 rtc::scoped_ptr<rtc::ByteBuffer> slice_rbsp(ParseRbsp( | |
312 source + kNaluHeaderAndTypeSize, source_length - kNaluHeaderAndTypeSize)); | |
313 rtc::BitBuffer slice_reader( | |
314 reinterpret_cast<const uint8*>(slice_rbsp->Data()), slice_rbsp->Length()); | |
315 // Check to see if this is an IDR slice, which has an extra field to parse | |
316 // out. | |
317 bool is_idr = (source[kNaluHeaderSize] & 0x0F) == kNaluIdr; | |
318 uint8_t nal_ref_idc = (source[kNaluHeaderSize] & 0x60) >> 5; | |
319 uint32_t golomb_tmp; | |
320 uint32_t bits_tmp; | |
321 | |
322 // first_mb_in_slice: ue(v) | |
323 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); | |
324 // slice_type: ue(v) | |
325 uint32_t slice_type; | |
326 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&slice_type)); | |
327 // slice_type's 5..9 range is used to indicate things.that all slices of a | |
noahric
2015/09/22 20:47:19
I think "things." is probably a mispaste?
pbos-webrtc
2015/09/23 12:29:43
Done.
| |
328 // picture have the same value of slice_type % 5, we don't care about that, so | |
329 // we map to the corresponding 0..4 range. | |
330 slice_type %= 5; | |
331 // pic_parameter_set_id: ue(v) | |
332 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); | |
333 if (sps_.separate_colour_plane_flag == 1) { | |
334 // colour_plane_id | |
335 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 2)); | |
336 } | |
337 // frame_num: u(v) | |
338 // Represented by log2_max_frame_num_minus4 + 4 bits. | |
339 RETURN_FALSE_ON_FAIL( | |
340 slice_reader.ReadBits(&bits_tmp, sps_.log2_max_frame_num_minus4 + 4)); | |
341 uint32 field_pic_flag = 0; | |
342 if (sps_.frame_mbs_only_flag == 0) { | |
343 // field_pic_flag: u(1) | |
344 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&field_pic_flag, 1)); | |
345 if (field_pic_flag != 0) { | |
346 // bottom_field_flag: u(1) | |
347 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 1)); | |
348 } | |
349 } | |
350 if (is_idr) { | |
351 // idr_pic_id: ue(v) | |
352 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); | |
353 } | |
354 // pic_order_cnt_lsb: u(v) | |
355 // Represented by sps_.log2_max_pic_order_cnt_lsb_minus4 + 4 bits. | |
356 if (sps_.pic_order_cnt_type == 0) { | |
357 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits( | |
358 &bits_tmp, sps_.log2_max_pic_order_cnt_lsb_minus4 + 4)); | |
359 if (pps_.bottom_field_pic_order_in_frame_present_flag && | |
360 field_pic_flag == 0) { | |
361 // delta_pic_order_cnt_bottom: se(v) | |
362 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); | |
363 } | |
364 } | |
365 if (sps_.pic_order_cnt_type == 1 && !sps_.delta_pic_order_always_zero_flag) { | |
366 // delta_pic_order_cnt[0]: se(v) | |
367 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); | |
368 if (pps_.bottom_field_pic_order_in_frame_present_flag && !field_pic_flag) { | |
369 // delta_pic_order_cnt[1]: se(v) | |
370 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); | |
371 } | |
372 } | |
373 if (pps_.redundant_pic_cnt_present_flag) { | |
374 // redundant_pic_cnt: ue(v) | |
375 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); | |
376 } | |
377 if (slice_type == kSliceTypeB) { | |
378 // direct_spatial_mv_pred_flag: u(1) | |
379 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 1)); | |
380 } | |
381 if (slice_type == kSliceTypeP || slice_type == kSliceTypeSp || | |
382 slice_type == kSliceTypeB) { | |
383 uint32_t num_ref_idx_active_override_flag; | |
384 // num_ref_idx_active_override_flag: u(1) | |
385 RETURN_FALSE_ON_FAIL( | |
386 slice_reader.ReadBits(&num_ref_idx_active_override_flag, 1)); | |
387 if (num_ref_idx_active_override_flag) { | |
noahric
2015/09/22 20:47:19
!= 0, for consistency (or you could strip the rest
pbos-webrtc
2015/09/23 12:29:43
Done.
| |
388 // num_ref_idx_l0_active_minus1: ue(v) | |
389 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); | |
390 if (slice_type == kSliceTypeB) { | |
391 // num_ref_idx_l1_active_minus1: ue(v) | |
392 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); | |
393 } | |
394 } | |
395 } | |
396 // assume nal_unit_type != 20 && nal_unit_type != 21: | |
397 // if (nal_unit_type == 20 || nal_unit_type == 21) | |
noahric
2015/09/22 20:47:19
Worth adding CHECKS for these? You're right, there
pbos-webrtc
2015/09/23 12:29:43
Done.
| |
398 // ref_pic_list_mvc_modification() | |
399 // else | |
400 { | |
401 // ref_pic_list_modification(): | |
402 // |slice_type| checks here don't use named constants as they aren't named | |
403 // in the spec for this segment. Keeping them consistent makes it easier to | |
404 // verify that they are both the same. | |
noahric
2015/09/22 20:47:19
Heh, yeah, I was just gonna comment on that being
pbos-webrtc
2015/09/23 12:29:43
Acknowledged.
| |
405 if (slice_type % 5 != 2 && slice_type % 5 != 4) { | |
406 // ref_pic_list_modification_flag_l0: u(1) | |
407 uint32_t ref_pic_list_modification_flag_l0; | |
408 RETURN_FALSE_ON_FAIL( | |
409 slice_reader.ReadBits(&ref_pic_list_modification_flag_l0, 1)); | |
410 if (ref_pic_list_modification_flag_l0) { | |
411 uint32_t modification_of_pic_nums_idc; | |
412 do { | |
413 // modification_of_pic_nums_idc: ue(v) | |
414 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb( | |
415 &modification_of_pic_nums_idc)); | |
416 if (modification_of_pic_nums_idc == 0 || | |
417 modification_of_pic_nums_idc == 1) { | |
418 // abs_diff_pic_num_minus1: ue(v) | |
419 RETURN_FALSE_ON_FAIL( | |
420 slice_reader.ReadExponentialGolomb(&golomb_tmp)); | |
421 } else if (modification_of_pic_nums_idc == 2) { | |
422 // long_term_pic_num: ue(v) | |
423 RETURN_FALSE_ON_FAIL( | |
424 slice_reader.ReadExponentialGolomb(&golomb_tmp)); | |
425 } | |
426 } while (modification_of_pic_nums_idc != 3); | |
427 } | |
428 } | |
429 if (slice_type % 5 == 1) { | |
430 // ref_pic_list_modification_flag_l1: u(1) | |
431 uint32_t ref_pic_list_modification_flag_l1; | |
432 RETURN_FALSE_ON_FAIL( | |
433 slice_reader.ReadBits(&ref_pic_list_modification_flag_l1, 1)); | |
434 if (ref_pic_list_modification_flag_l1) { | |
435 uint32_t modification_of_pic_nums_idc; | |
436 do { | |
437 // modification_of_pic_nums_idc: ue(v) | |
438 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb( | |
439 &modification_of_pic_nums_idc)); | |
440 if (modification_of_pic_nums_idc == 0 || | |
441 modification_of_pic_nums_idc == 1) { | |
442 // abs_diff_pic_num_minus1: ue(v) | |
443 RETURN_FALSE_ON_FAIL( | |
444 slice_reader.ReadExponentialGolomb(&golomb_tmp)); | |
445 } else if (modification_of_pic_nums_idc == 2) { | |
446 // long_term_pic_num: ue(v) | |
447 RETURN_FALSE_ON_FAIL( | |
448 slice_reader.ReadExponentialGolomb(&golomb_tmp)); | |
449 } | |
450 } while (modification_of_pic_nums_idc != 3); | |
451 } | |
452 } | |
453 } | |
454 // TODO(pbos): Do we need support for pred_weight_table()? | |
455 // if ((weighted_pred_flag && (slice_type == P || slice_type == SP)) || | |
noahric
2015/09/22 20:47:19
Worth adding CHECKS for these?
pbos-webrtc
2015/09/23 12:29:43
Done.
| |
456 // (weighted_bipred_idc == 1 && slice_type == B)) { | |
457 // pred_weight_table() | |
458 // } | |
459 if (nal_ref_idc) { | |
noahric
2015/09/22 20:47:19
nal_ref_idc != 0
pbos-webrtc
2015/09/23 12:29:43
Done.
| |
460 // dec_ref_pic_marking(): | |
461 if (is_idr) { | |
462 // no_output_of_prior_pics_flag: u(1) | |
463 // long_term_reference_flag: u(1) | |
464 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 2)); | |
465 } else { | |
466 // adaptive_ref_pic_marking_mode_flag: u(1) | |
467 uint32_t adaptive_ref_pic_marking_mode_flag; | |
468 RETURN_FALSE_ON_FAIL( | |
469 slice_reader.ReadBits(&adaptive_ref_pic_marking_mode_flag, 1)); | |
470 if (adaptive_ref_pic_marking_mode_flag) { | |
471 uint32_t memory_management_control_operation; | |
472 do { | |
473 // memory_management_control_operation: ue(v) | |
474 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb( | |
475 &memory_management_control_operation)); | |
476 if (memory_management_control_operation == 1 || | |
477 memory_management_control_operation == 3) { | |
478 // difference_of_pic_nums_minus1: ue(v) | |
479 RETURN_FALSE_ON_FAIL( | |
480 slice_reader.ReadExponentialGolomb(&golomb_tmp)); | |
481 } | |
482 if (memory_management_control_operation == 2) { | |
483 // long_term_pic_num: ue(v) | |
484 RETURN_FALSE_ON_FAIL( | |
485 slice_reader.ReadExponentialGolomb(&golomb_tmp)); | |
486 } | |
487 if (memory_management_control_operation == 3 || | |
488 memory_management_control_operation == 6) { | |
489 // long_term_frame_idx: ue(v) | |
490 RETURN_FALSE_ON_FAIL( | |
491 slice_reader.ReadExponentialGolomb(&golomb_tmp)); | |
492 } | |
493 if (memory_management_control_operation == 4) { | |
494 // max_long_term_frame_idx_plus1: ue(v) | |
495 RETURN_FALSE_ON_FAIL( | |
496 slice_reader.ReadExponentialGolomb(&golomb_tmp)); | |
497 } | |
498 } while (memory_management_control_operation != 0); | |
499 } | |
500 } | |
501 } | |
502 // cabac not supported: entropy_coding_mode_flag == 0 asserted above. | |
503 // if (entropy_coding_mode_flag && slice_type != I && slice_type != SI) | |
504 // cabac_init_idc | |
505 RETURN_FALSE_ON_FAIL( | |
506 slice_reader.ReadSignedExponentialGolomb(&last_slice_qp_delta_)); | |
507 return true; | |
508 } | |
509 | |
510 void H264BitstreamParser::ParseSlice(const uint8_t* slice, size_t length) { | |
511 uint8_t nalu_type = slice[4] & kNaluTypeMask; | |
512 switch (nalu_type) { | |
513 case kNaluSps: | |
514 RTC_CHECK(ParseSpsNalu(slice, length)) | |
515 << "Failed to parse bitstream SPS."; | |
516 break; | |
517 case kNaluPps: | |
518 RTC_CHECK(ParsePpsNalu(slice, length)) | |
519 << "Failed to parse bitstream PPS."; | |
520 break; | |
521 default: | |
522 RTC_CHECK(ParseNonParameterSetNalu(slice, length)) | |
523 << "Failed to parse picture slice."; | |
524 break; | |
525 } | |
526 } | |
527 | |
528 void H264BitstreamParser::ParseBitstream(const uint8_t* bitstream, | |
529 size_t length) { | |
530 RTC_CHECK_GE(length, 4u); | |
531 std::vector<size_t> slice_markers = FindNaluStartSequences(bitstream, length); | |
532 RTC_CHECK(!slice_markers.empty()); | |
533 for (size_t i = 0; i < slice_markers.size() - 1; ++i) { | |
534 ParseSlice(bitstream + slice_markers[i], | |
535 slice_markers[i + 1] - slice_markers[i]); | |
536 } | |
537 // Parse the last slice. | |
538 ParseSlice(bitstream + slice_markers.back(), length - slice_markers.back()); | |
539 } | |
540 | |
541 int H264BitstreamParser::GetLastSliceQp() const { | |
542 return 26 + pps_.pic_init_qp_minus26 + last_slice_qp_delta_; | |
543 } | |
544 | |
545 } // namespace webrtc | |
OLD | NEW |