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

Side by Side Diff: webrtc/common_video/h264/h264_bitstream_parser.cc

Issue 2532973002: Sanity check parsed QP values from H264 bitstream (Closed)
Patch Set: add one last range check Created 4 years 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
« no previous file with comments | « no previous file | webrtc/common_video/h264/pps_parser.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/common_video/h264/h264_bitstream_parser.h" 10 #include "webrtc/common_video/h264/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 18
19 #include "webrtc/common_video/h264/h264_common.h" 19 #include "webrtc/common_video/h264/h264_common.h"
20 #include "webrtc/base/logging.h" 20 #include "webrtc/base/logging.h"
21 21
22 namespace {
23 const int kMaxAbsQpDeltaValue = 51;
24 const int kMinQpValue = 0;
25 const int kMaxQpValue = 51;
26 }
27
22 namespace webrtc { 28 namespace webrtc {
23 29
24 #define RETURN_ON_FAIL(x, res) \ 30 #define RETURN_ON_FAIL(x, res) \
25 if (!(x)) { \ 31 if (!(x)) { \
26 LOG_F(LS_ERROR) << "FAILED: " #x; \ 32 LOG_F(LS_ERROR) << "FAILED: " #x; \
27 return res; \ 33 return res; \
28 } 34 }
29 35
30 #define RETURN_INV_ON_FAIL(x) RETURN_ON_FAIL(x, kInvalidStream) 36 #define RETURN_INV_ON_FAIL(x) RETURN_ON_FAIL(x, kInvalidStream)
31 37
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 } 250 }
245 if (pps_->entropy_coding_mode_flag && 251 if (pps_->entropy_coding_mode_flag &&
246 slice_type != H264::SliceType::kI && slice_type != H264::SliceType::kSi) { 252 slice_type != H264::SliceType::kI && slice_type != H264::SliceType::kSi) {
247 // cabac_init_idc: ue(v) 253 // cabac_init_idc: ue(v)
248 RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp)); 254 RETURN_INV_ON_FAIL(slice_reader.ReadExponentialGolomb(&golomb_tmp));
249 } 255 }
250 256
251 int32_t last_slice_qp_delta; 257 int32_t last_slice_qp_delta;
252 RETURN_INV_ON_FAIL( 258 RETURN_INV_ON_FAIL(
253 slice_reader.ReadSignedExponentialGolomb(&last_slice_qp_delta)); 259 slice_reader.ReadSignedExponentialGolomb(&last_slice_qp_delta));
260 if (abs(last_slice_qp_delta) > kMaxAbsQpDeltaValue) {
261 // Something has gone wrong, and the parsed value is invalid.
262 LOG(LS_WARNING) << "Parsed QP value out of range.";
263 return kInvalidStream;
264 }
265
254 last_slice_qp_delta_ = rtc::Optional<int32_t>(last_slice_qp_delta); 266 last_slice_qp_delta_ = rtc::Optional<int32_t>(last_slice_qp_delta);
255 return kOk; 267 return kOk;
256 } 268 }
257 269
258 void H264BitstreamParser::ParseSlice(const uint8_t* slice, size_t length) { 270 void H264BitstreamParser::ParseSlice(const uint8_t* slice, size_t length) {
259 H264::NaluType nalu_type = H264::ParseNaluType(slice[0]); 271 H264::NaluType nalu_type = H264::ParseNaluType(slice[0]);
260 switch (nalu_type) { 272 switch (nalu_type) {
261 case H264::NaluType::kSps: { 273 case H264::NaluType::kSps: {
262 sps_ = SpsParser::ParseSps(slice + H264::kNaluTypeSize, 274 sps_ = SpsParser::ParseSps(slice + H264::kNaluTypeSize,
263 length - H264::kNaluTypeSize); 275 length - H264::kNaluTypeSize);
(...skipping 20 matching lines...) Expand all
284 size_t length) { 296 size_t length) {
285 std::vector<H264::NaluIndex> nalu_indices = 297 std::vector<H264::NaluIndex> nalu_indices =
286 H264::FindNaluIndices(bitstream, length); 298 H264::FindNaluIndices(bitstream, length);
287 for (const H264::NaluIndex& index : nalu_indices) 299 for (const H264::NaluIndex& index : nalu_indices)
288 ParseSlice(&bitstream[index.payload_start_offset], index.payload_size); 300 ParseSlice(&bitstream[index.payload_start_offset], index.payload_size);
289 } 301 }
290 302
291 bool H264BitstreamParser::GetLastSliceQp(int* qp) const { 303 bool H264BitstreamParser::GetLastSliceQp(int* qp) const {
292 if (!last_slice_qp_delta_ || !pps_) 304 if (!last_slice_qp_delta_ || !pps_)
293 return false; 305 return false;
294 *qp = 26 + pps_->pic_init_qp_minus26 + *last_slice_qp_delta_; 306 const int parsed_qp = 26 + pps_->pic_init_qp_minus26 + *last_slice_qp_delta_;
307 if (parsed_qp < kMinQpValue || parsed_qp > kMaxQpValue) {
308 LOG(LS_ERROR) << "Parsed invalid QP from bitstream.";
309 return false;
310 }
311 *qp = parsed_qp;
295 return true; 312 return true;
296 } 313 }
297 314
298 } // namespace webrtc 315 } // namespace webrtc
OLDNEW
« no previous file with comments | « no previous file | webrtc/common_video/h264/pps_parser.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698