Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(102)

Side by Side Diff: webrtc/modules/rtp_rtcp/source/h264/bitstream_parser.cc

Issue 1979443004: Add H264 bitstream rewriting to limit frame reordering marker in header (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Fixed compiler warning on win Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 <memory>
13 #include <vector>
14
15 #include "webrtc/base/bitbuffer.h"
16 #include "webrtc/base/bytebuffer.h"
17 #include "webrtc/base/checks.h"
18
19 #include "webrtc/modules/rtp_rtcp/source/h264/h264_common.h"
20 #include "webrtc/base/logging.h"
21
22 namespace webrtc {
23
24 #define RETURN_FALSE_ON_FAIL(x) \
25 if (!(x)) { \
26 LOG_F(LS_ERROR) << "FAILED: " #x; \
27 return false; \
28 }
29
30 H264BitstreamParser::H264BitstreamParser() {}
31 H264BitstreamParser::~H264BitstreamParser() {}
32
33 bool H264BitstreamParser::ParseNonParameterSetNalu(const uint8_t* source,
34 size_t source_length,
35 uint8_t nalu_type) {
36 RTC_CHECK(sps_);
37 RTC_CHECK(pps_);
38 last_slice_qp_delta_ = rtc::Optional<int32_t>();
39 std::unique_ptr<rtc::Buffer> slice_rbsp(
40 H264Common::ParseRbsp(source, source_length));
41 rtc::BitBuffer slice_reader(slice_rbsp->data() + H264Common::kNaluTypeSize,
42 slice_rbsp->size() - H264Common::kNaluTypeSize);
43 // Check to see if this is an IDR slice, which has an extra field to parse
44 // out.
45 bool is_idr = (source[0] & 0x0F) == H264Common::NaluType::kIdr;
46 uint8_t nal_ref_idc = (source[0] & 0x60) >> 5;
47 uint32_t golomb_tmp;
48 uint32_t bits_tmp;
49
50 // first_mb_in_slice: ue(v)
51 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
52 // slice_type: ue(v)
53 uint32_t slice_type;
54 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&slice_type));
55 // slice_type's 5..9 range is used to indicate that all slices of a picture
56 // have the same value of slice_type % 5, we don't care about that, so we map
57 // to the corresponding 0..4 range.
58 slice_type %= 5;
59 // pic_parameter_set_id: ue(v)
60 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
61 if (sps_->separate_colour_plane_flag == 1) {
62 // colour_plane_id
63 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 2));
64 }
65 // frame_num: u(v)
66 // Represented by log2_max_frame_num_minus4 + 4 bits.
67 RETURN_FALSE_ON_FAIL(
68 slice_reader.ReadBits(&bits_tmp, sps_->log2_max_frame_num_minus4 + 4));
69 uint32_t field_pic_flag = 0;
70 if (sps_->frame_mbs_only_flag == 0) {
71 // field_pic_flag: u(1)
72 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&field_pic_flag, 1));
73 if (field_pic_flag != 0) {
74 // bottom_field_flag: u(1)
75 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 1));
76 }
77 }
78 if (is_idr) {
79 // idr_pic_id: ue(v)
80 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
81 }
82 // pic_order_cnt_lsb: u(v)
83 // Represented by sps_.log2_max_pic_order_cnt_lsb_minus4 + 4 bits.
84 if (sps_->pic_order_cnt_type == 0) {
85 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(
86 &bits_tmp, sps_->log2_max_pic_order_cnt_lsb_minus4 + 4));
87 if (pps_->bottom_field_pic_order_in_frame_present_flag &&
88 field_pic_flag == 0) {
89 // delta_pic_order_cnt_bottom: se(v)
90 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
91 }
92 }
93 if (sps_->pic_order_cnt_type == 1 &&
94 !sps_->delta_pic_order_always_zero_flag) {
95 // delta_pic_order_cnt[0]: se(v)
96 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
97 if (pps_->bottom_field_pic_order_in_frame_present_flag && !field_pic_flag) {
98 // delta_pic_order_cnt[1]: se(v)
99 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
100 }
101 }
102 if (pps_->redundant_pic_cnt_present_flag) {
103 // redundant_pic_cnt: ue(v)
104 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
105 }
106 if (slice_type == H264Common::SliceType::B) {
107 // direct_spatial_mv_pred_flag: u(1)
108 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 1));
109 }
110 switch (slice_type) {
111 case H264Common::SliceType::P:
112 case H264Common::SliceType::B:
113 case H264Common::SliceType::Sp:
114 uint32_t num_ref_idx_active_override_flag;
115 // num_ref_idx_active_override_flag: u(1)
116 RETURN_FALSE_ON_FAIL(
117 slice_reader.ReadBits(&num_ref_idx_active_override_flag, 1));
118 if (num_ref_idx_active_override_flag != 0) {
119 // num_ref_idx_l0_active_minus1: ue(v)
120 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
121 if (slice_type == H264Common::SliceType::B) {
122 // num_ref_idx_l1_active_minus1: ue(v)
123 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
124 }
125 }
126 break;
127 default:
128 break;
129 }
130 // assume nal_unit_type != 20 && nal_unit_type != 21:
131 RTC_CHECK_NE(nalu_type, 20);
132 RTC_CHECK_NE(nalu_type, 21);
133 // if (nal_unit_type == 20 || nal_unit_type == 21)
134 // ref_pic_list_mvc_modification()
135 // else
136 {
137 // ref_pic_list_modification():
138 // |slice_type| checks here don't use named constants as they aren't named
139 // in the spec for this segment. Keeping them consistent makes it easier to
140 // verify that they are both the same.
141 if (slice_type % 5 != 2 && slice_type % 5 != 4) {
142 // ref_pic_list_modification_flag_l0: u(1)
143 uint32_t ref_pic_list_modification_flag_l0;
144 RETURN_FALSE_ON_FAIL(
145 slice_reader.ReadBits(&ref_pic_list_modification_flag_l0, 1));
146 if (ref_pic_list_modification_flag_l0) {
147 uint32_t modification_of_pic_nums_idc;
148 do {
149 // modification_of_pic_nums_idc: ue(v)
150 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(
151 &modification_of_pic_nums_idc));
152 if (modification_of_pic_nums_idc == 0 ||
153 modification_of_pic_nums_idc == 1) {
154 // abs_diff_pic_num_minus1: ue(v)
155 RETURN_FALSE_ON_FAIL(
156 slice_reader.ReadExponentialGolomb(&golomb_tmp));
157 } else if (modification_of_pic_nums_idc == 2) {
158 // long_term_pic_num: ue(v)
159 RETURN_FALSE_ON_FAIL(
160 slice_reader.ReadExponentialGolomb(&golomb_tmp));
161 }
162 } while (modification_of_pic_nums_idc != 3);
163 }
164 }
165 if (slice_type % 5 == 1) {
166 // ref_pic_list_modification_flag_l1: u(1)
167 uint32_t ref_pic_list_modification_flag_l1;
168 RETURN_FALSE_ON_FAIL(
169 slice_reader.ReadBits(&ref_pic_list_modification_flag_l1, 1));
170 if (ref_pic_list_modification_flag_l1) {
171 uint32_t modification_of_pic_nums_idc;
172 do {
173 // modification_of_pic_nums_idc: ue(v)
174 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(
175 &modification_of_pic_nums_idc));
176 if (modification_of_pic_nums_idc == 0 ||
177 modification_of_pic_nums_idc == 1) {
178 // abs_diff_pic_num_minus1: ue(v)
179 RETURN_FALSE_ON_FAIL(
180 slice_reader.ReadExponentialGolomb(&golomb_tmp));
181 } else if (modification_of_pic_nums_idc == 2) {
182 // long_term_pic_num: ue(v)
183 RETURN_FALSE_ON_FAIL(
184 slice_reader.ReadExponentialGolomb(&golomb_tmp));
185 }
186 } while (modification_of_pic_nums_idc != 3);
187 }
188 }
189 }
190 // TODO(pbos): Do we need support for pred_weight_table()?
191 RTC_CHECK(!(
192 (pps_->weighted_pred_flag && (slice_type == H264Common::SliceType::P ||
193 slice_type == H264Common::SliceType::Sp)) ||
194 (pps_->weighted_bipred_idc != 0 &&
195 slice_type == H264Common::SliceType::B)))
196 << "Missing support for pred_weight_table().";
197 // if ((weighted_pred_flag && (slice_type == P || slice_type == SP)) ||
198 // (weighted_bipred_idc == 1 && slice_type == B)) {
199 // pred_weight_table()
200 // }
201 if (nal_ref_idc != 0) {
202 // dec_ref_pic_marking():
203 if (is_idr) {
204 // no_output_of_prior_pics_flag: u(1)
205 // long_term_reference_flag: u(1)
206 RETURN_FALSE_ON_FAIL(slice_reader.ReadBits(&bits_tmp, 2));
207 } else {
208 // adaptive_ref_pic_marking_mode_flag: u(1)
209 uint32_t adaptive_ref_pic_marking_mode_flag;
210 RETURN_FALSE_ON_FAIL(
211 slice_reader.ReadBits(&adaptive_ref_pic_marking_mode_flag, 1));
212 if (adaptive_ref_pic_marking_mode_flag) {
213 uint32_t memory_management_control_operation;
214 do {
215 // memory_management_control_operation: ue(v)
216 RETURN_FALSE_ON_FAIL(slice_reader.ReadExponentialGolomb(
217 &memory_management_control_operation));
218 if (memory_management_control_operation == 1 ||
219 memory_management_control_operation == 3) {
220 // difference_of_pic_nums_minus1: ue(v)
221 RETURN_FALSE_ON_FAIL(
222 slice_reader.ReadExponentialGolomb(&golomb_tmp));
223 }
224 if (memory_management_control_operation == 2) {
225 // long_term_pic_num: ue(v)
226 RETURN_FALSE_ON_FAIL(
227 slice_reader.ReadExponentialGolomb(&golomb_tmp));
228 }
229 if (memory_management_control_operation == 3 ||
230 memory_management_control_operation == 6) {
231 // long_term_frame_idx: ue(v)
232 RETURN_FALSE_ON_FAIL(
233 slice_reader.ReadExponentialGolomb(&golomb_tmp));
234 }
235 if (memory_management_control_operation == 4) {
236 // max_long_term_frame_idx_plus1: ue(v)
237 RETURN_FALSE_ON_FAIL(
238 slice_reader.ReadExponentialGolomb(&golomb_tmp));
239 }
240 } while (memory_management_control_operation != 0);
241 }
242 }
243 }
244 // cabac not supported: entropy_coding_mode_flag == 0 asserted above.
245 // if (entropy_coding_mode_flag && slice_type != I && slice_type != SI)
246 // cabac_init_idc
247 int32_t last_slice_qp_delta;
248 RETURN_FALSE_ON_FAIL(
249 slice_reader.ReadSignedExponentialGolomb(&last_slice_qp_delta));
250 last_slice_qp_delta_ = rtc::Optional<int32_t>(last_slice_qp_delta);
251 return true;
252 }
253
254 void H264BitstreamParser::ParseSlice(const uint8_t* slice, size_t length) {
255 H264Common::NaluType nalu_type = H264Common::ParseNaluType(slice[0]);
256 switch (nalu_type) {
257 case H264Common::NaluType::kSps: {
258 sps_ = SpsParser::ParseSps(slice + H264Common::kNaluTypeSize,
259 length - H264Common::kNaluTypeSize);
260 if (!sps_)
261 FATAL() << "Unable to parse SPS from H264 bitstream.";
262 break;
263 }
264 case H264Common::NaluType::kPps: {
265 pps_ = PpsParser::ParsePps(slice + H264Common::kNaluTypeSize,
266 length - H264Common::kNaluTypeSize);
267 if (!pps_)
268 FATAL() << "Unable to parse PPS from H264 bitstream.";
269 break;
270 }
271 default:
272 RTC_CHECK(ParseNonParameterSetNalu(slice, length, nalu_type))
273 << "Failed to parse picture slice.";
274 break;
275 }
276 }
277
278 void H264BitstreamParser::ParseBitstream(const uint8_t* bitstream,
279 size_t length) {
280 std::vector<H264Common::NaluIndex> nalu_indices =
281 H264Common::FindNaluIndices(bitstream, length);
282 RTC_CHECK(!nalu_indices.empty());
283 for (const H264Common::NaluIndex& index : nalu_indices)
284 ParseSlice(&bitstream[index.payload_start_offset], index.payload_size);
285 }
286
287 bool H264BitstreamParser::GetLastSliceQp(int* qp) const {
288 if (!last_slice_qp_delta_ || !pps_)
289 return false;
290 *qp = 26 + pps_->pic_init_qp_minus26 + *last_slice_qp_delta_;
291 return true;
292 }
293
294 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698