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.
FindNaluStartSequences(const uint8_t * buffer,size_t buffer_size)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.
ParseRbsp(const uint8_t * bytes,size_t length)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 
PpsState()93 H264BitstreamParser::PpsState::PpsState() {}
94 
SpsState()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
ParseSpsNalu(const uint8_t * sps,size_t length)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   rtc::scoped_ptr<rtc::ByteBuffer> 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 
ParsePpsNalu(const uint8_t * pps,size_t length)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   rtc::scoped_ptr<rtc::ByteBuffer> 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 
ParseNonParameterSetNalu(const uint8_t * source,size_t source_length,uint8_t nalu_type)314 bool H264BitstreamParser::ParseNonParameterSetNalu(const uint8_t* source,
315                                                    size_t source_length,
316                                                    uint8_t nalu_type) {
317   RTC_CHECK(sps_parsed_);
318   RTC_CHECK(pps_parsed_);
319   last_slice_qp_delta_parsed_ = false;
320   rtc::scoped_ptr<rtc::ByteBuffer> slice_rbsp(ParseRbsp(
321       source + kNaluHeaderAndTypeSize, source_length - kNaluHeaderAndTypeSize));
322   rtc::BitBuffer slice_reader(
323       reinterpret_cast<const uint8_t*>(slice_rbsp->Data()),
324       slice_rbsp->Length());
325   // Check to see if this is an IDR slice, which has an extra field to parse
326   // out.
327   bool is_idr = (source[kNaluHeaderSize] & 0x0F) == kNaluIdr;
328   uint8_t nal_ref_idc = (source[kNaluHeaderSize] & 0x60) >> 5;
329   uint32_t golomb_tmp;
330   uint32_t bits_tmp;
331 
332   // first_mb_in_slice: ue(v)
333   RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
334   // slice_type: ue(v)
335   uint32_t slice_type;
336   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
338   // have the same value of slice_type % 5, we don't care about that, so we map
339   // to the corresponding 0..4 range.
340   slice_type %= 5;
341   // pic_parameter_set_id: ue(v)
342   RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
343   if (sps_.separate_colour_plane_flag == 1) {
344     // colour_plane_id
345     RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 2));
346   }
347   // frame_num: u(v)
348   // Represented by log2_max_frame_num_minus4 + 4 bits.
349   RETURN_FALSE_ON_FAIL(
350       slice_reader.ReadBits(&bits_tmp, sps_.log2_max_frame_num_minus4 + 4));
351   uint32_t field_pic_flag = 0;
352   if (sps_.frame_mbs_only_flag == 0) {
353     // field_pic_flag: u(1)
354     RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&field_pic_flag, 1));
355     if (field_pic_flag != 0) {
356       // bottom_field_flag: u(1)
357       RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 1));
358     }
359   }
360   if (is_idr) {
361     // idr_pic_id: ue(v)
362     RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
363   }
364   // pic_order_cnt_lsb: u(v)
365   // Represented by sps_.log2_max_pic_order_cnt_lsb_minus4 + 4 bits.
366   if (sps_.pic_order_cnt_type == 0) {
367     RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(
368         &bits_tmp, sps_.log2_max_pic_order_cnt_lsb_minus4 + 4));
369     if (pps_.bottom_field_pic_order_in_frame_present_flag &&
370         field_pic_flag == 0) {
371       // delta_pic_order_cnt_bottom: se(v)
372       RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
373     }
374   }
375   if (sps_.pic_order_cnt_type == 1 && !sps_.delta_pic_order_always_zero_flag) {
376     // delta_pic_order_cnt[0]: se(v)
377     RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
378     if (pps_.bottom_field_pic_order_in_frame_present_flag && !field_pic_flag) {
379       // delta_pic_order_cnt[1]: se(v)
380       RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
381     }
382   }
383   if (pps_.redundant_pic_cnt_present_flag) {
384     // redundant_pic_cnt: ue(v)
385     RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
386   }
387   if (slice_type == kSliceTypeB) {
388     // direct_spatial_mv_pred_flag: u(1)
389     RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 1));
390   }
391   if (slice_type == kSliceTypeP || slice_type == kSliceTypeSp ||
392       slice_type == kSliceTypeB) {
393     uint32_t num_ref_idx_active_override_flag;
394     // num_ref_idx_active_override_flag: u(1)
395     RETURN_FALSE_ON_FAIL(
396         slice_reader.ReadBits(&num_ref_idx_active_override_flag, 1));
397     if (num_ref_idx_active_override_flag != 0) {
398       // num_ref_idx_l0_active_minus1: ue(v)
399       RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
400       if (slice_type == kSliceTypeB) {
401         // num_ref_idx_l1_active_minus1: ue(v)
402         RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
403       }
404     }
405   }
406   // assume nal_unit_type != 20 && nal_unit_type != 21:
407   RTC_CHECK_NE(nalu_type, 20);
408   RTC_CHECK_NE(nalu_type, 21);
409   // if (nal_unit_type == 20 || nal_unit_type == 21)
410   //   ref_pic_list_mvc_modification()
411   // else
412   {
413     // ref_pic_list_modification():
414     // |slice_type| checks here don't use named constants as they aren't named
415     // in the spec for this segment. Keeping them consistent makes it easier to
416     // verify that they are both the same.
417     if (slice_type % 5 != 2 && slice_type % 5 != 4) {
418       // ref_pic_list_modification_flag_l0: u(1)
419       uint32_t ref_pic_list_modification_flag_l0;
420       RETURN_FALSE_ON_FAIL(
421           slice_reader.ReadBits(&ref_pic_list_modification_flag_l0, 1));
422       if (ref_pic_list_modification_flag_l0) {
423         uint32_t modification_of_pic_nums_idc;
424         do {
425           // modification_of_pic_nums_idc: ue(v)
426           RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(
427               &modification_of_pic_nums_idc));
428           if (modification_of_pic_nums_idc == 0 ||
429               modification_of_pic_nums_idc == 1) {
430             // abs_diff_pic_num_minus1: ue(v)
431             RETURN_FALSE_ON_FAIL(
432                 slice_reader.ReadExponentialGolomb(&golomb_tmp));
433           } else if (modification_of_pic_nums_idc == 2) {
434             // long_term_pic_num: ue(v)
435             RETURN_FALSE_ON_FAIL(
436                 slice_reader.ReadExponentialGolomb(&golomb_tmp));
437           }
438         } while (modification_of_pic_nums_idc != 3);
439       }
440     }
441     if (slice_type % 5 == 1) {
442       // ref_pic_list_modification_flag_l1: u(1)
443       uint32_t ref_pic_list_modification_flag_l1;
444       RETURN_FALSE_ON_FAIL(
445           slice_reader.ReadBits(&ref_pic_list_modification_flag_l1, 1));
446       if (ref_pic_list_modification_flag_l1) {
447         uint32_t modification_of_pic_nums_idc;
448         do {
449           // modification_of_pic_nums_idc: ue(v)
450           RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(
451               &modification_of_pic_nums_idc));
452           if (modification_of_pic_nums_idc == 0 ||
453               modification_of_pic_nums_idc == 1) {
454             // abs_diff_pic_num_minus1: ue(v)
455             RETURN_FALSE_ON_FAIL(
456                 slice_reader.ReadExponentialGolomb(&golomb_tmp));
457           } else if (modification_of_pic_nums_idc == 2) {
458             // long_term_pic_num: ue(v)
459             RETURN_FALSE_ON_FAIL(
460                 slice_reader.ReadExponentialGolomb(&golomb_tmp));
461           }
462         } while (modification_of_pic_nums_idc != 3);
463       }
464     }
465   }
466   // TODO(pbos): Do we need support for pred_weight_table()?
467   RTC_CHECK(!((pps_.weighted_pred_flag &&
468                (slice_type == kSliceTypeP || slice_type == kSliceTypeSp)) ||
469               (pps_.weighted_bipred_idc != 0 && slice_type == kSliceTypeB)))
470       << "Missing support for pred_weight_table().";
471   // if ((weighted_pred_flag && (slice_type == P || slice_type == SP)) ||
472   //    (weighted_bipred_idc == 1 && slice_type == B)) {
473   //  pred_weight_table()
474   // }
475   if (nal_ref_idc != 0) {
476     // dec_ref_pic_marking():
477     if (is_idr) {
478       // no_output_of_prior_pics_flag: u(1)
479       // long_term_reference_flag: u(1)
480       RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 2));
481     } else {
482       // adaptive_ref_pic_marking_mode_flag: u(1)
483       uint32_t adaptive_ref_pic_marking_mode_flag;
484       RETURN_FALSE_ON_FAIL(
485           slice_reader.ReadBits(&adaptive_ref_pic_marking_mode_flag, 1));
486       if (adaptive_ref_pic_marking_mode_flag) {
487         uint32_t memory_management_control_operation;
488         do {
489           // memory_management_control_operation: ue(v)
490           RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(
491               &memory_management_control_operation));
492           if (memory_management_control_operation == 1 ||
493               memory_management_control_operation == 3) {
494             // difference_of_pic_nums_minus1: ue(v)
495             RETURN_FALSE_ON_FAIL(
496                 slice_reader.ReadExponentialGolomb(&golomb_tmp));
497           }
498           if (memory_management_control_operation == 2) {
499             // long_term_pic_num: ue(v)
500             RETURN_FALSE_ON_FAIL(
501                 slice_reader.ReadExponentialGolomb(&golomb_tmp));
502           }
503           if (memory_management_control_operation == 3 ||
504               memory_management_control_operation == 6) {
505             // long_term_frame_idx: ue(v)
506             RETURN_FALSE_ON_FAIL(
507                 slice_reader.ReadExponentialGolomb(&golomb_tmp));
508           }
509           if (memory_management_control_operation == 4) {
510             // max_long_term_frame_idx_plus1: ue(v)
511             RETURN_FALSE_ON_FAIL(
512                 slice_reader.ReadExponentialGolomb(&golomb_tmp));
513           }
514         } while (memory_management_control_operation != 0);
515       }
516     }
517   }
518   // cabac not supported: entropy_coding_mode_flag == 0 asserted above.
519   // if (entropy_coding_mode_flag && slice_type != I && slice_type != SI)
520   //   cabac_init_idc
521   RETURN_FALSE_ON_FAIL(
522       slice_reader.ReadSignedExponentialGolomb(&last_slice_qp_delta_));
523   last_slice_qp_delta_parsed_ = true;
524   return true;
525 }
526 
ParseSlice(const uint8_t * slice,size_t length)527 void H264BitstreamParser::ParseSlice(const uint8_t* slice, size_t length) {
528   uint8_t nalu_type = slice[4] & kNaluTypeMask;
529   switch (nalu_type) {
530     case kNaluSps:
531       RTC_CHECK(ParseSpsNalu(slice, length))
532           << "Failed to parse bitstream SPS.";
533       break;
534     case kNaluPps:
535       RTC_CHECK(ParsePpsNalu(slice, length))
536           << "Failed to parse bitstream PPS.";
537       break;
538     default:
539       RTC_CHECK(ParseNonParameterSetNalu(slice, length, nalu_type))
540           << "Failed to parse picture slice.";
541       break;
542   }
543 }
544 
ParseBitstream(const uint8_t * bitstream,size_t length)545 void H264BitstreamParser::ParseBitstream(const uint8_t* bitstream,
546                                          size_t length) {
547   RTC_CHECK_GE(length, 4u);
548   std::vector<size_t> slice_markers = FindNaluStartSequences(bitstream, length);
549   RTC_CHECK(!slice_markers.empty());
550   for (size_t i = 0; i < slice_markers.size() - 1; ++i) {
551     ParseSlice(bitstream + slice_markers[i],
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 }
557 
GetLastSliceQp(int * qp) const558 bool H264BitstreamParser::GetLastSliceQp(int* qp) const {
559   if (!last_slice_qp_delta_parsed_)
560     return false;
561   *qp = 26 + pps_.pic_init_qp_minus26 + last_slice_qp_delta_;
562   return true;
563 }
564 
565 }  // namespace webrtc
566