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

Side by Side Diff: webrtc/common_video/h264/pps_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
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2016 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 10
11 #include "webrtc/common_video/h264/pps_parser.h" 11 #include "webrtc/common_video/h264/pps_parser.h"
12 12
13 #include <memory> 13 #include <memory>
14 14
15 #include "webrtc/common_video/h264/h264_common.h" 15 #include "webrtc/common_video/h264/h264_common.h"
16 #include "webrtc/base/bitbuffer.h" 16 #include "webrtc/base/bitbuffer.h"
17 #include "webrtc/base/buffer.h" 17 #include "webrtc/base/buffer.h"
18 #include "webrtc/base/logging.h" 18 #include "webrtc/base/logging.h"
19 19
20 #define RETURN_EMPTY_ON_FAIL(x) \ 20 #define RETURN_EMPTY_ON_FAIL(x) \
21 if (!(x)) { \ 21 if (!(x)) { \
22 return rtc::Optional<PpsParser::PpsState>(); \ 22 return rtc::Optional<PpsParser::PpsState>(); \
23 } 23 }
24 24
25 namespace {
26 const int kMaxPicInitQpDeltaValue = 25;
27 const int kMinPicInitQpDeltaValue = -26;
28 }
29
25 namespace webrtc { 30 namespace webrtc {
26 31
27 // General note: this is based off the 02/2014 version of the H.264 standard. 32 // General note: this is based off the 02/2014 version of the H.264 standard.
28 // You can find it on this page: 33 // You can find it on this page:
29 // http://www.itu.int/rec/T-REC-H.264 34 // http://www.itu.int/rec/T-REC-H.264
30 35
31 rtc::Optional<PpsParser::PpsState> PpsParser::ParsePps(const uint8_t* data, 36 rtc::Optional<PpsParser::PpsState> PpsParser::ParsePps(const uint8_t* data,
32 size_t length) { 37 size_t length) {
33 // First, parse out rbsp, which is basically the source buffer minus emulation 38 // First, parse out rbsp, which is basically the source buffer minus emulation
34 // bytes (the last byte of a 0x00 0x00 0x03 sequence). RBSP is defined in 39 // bytes (the last byte of a 0x00 0x00 0x03 sequence). RBSP is defined in
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 // weighted_pred_flag: u(1) 160 // weighted_pred_flag: u(1)
156 uint32_t weighted_pred_flag; 161 uint32_t weighted_pred_flag;
157 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&weighted_pred_flag, 1)); 162 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&weighted_pred_flag, 1));
158 pps.weighted_pred_flag = weighted_pred_flag != 0; 163 pps.weighted_pred_flag = weighted_pred_flag != 0;
159 // weighted_bipred_idc: u(2) 164 // weighted_bipred_idc: u(2)
160 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&pps.weighted_bipred_idc, 2)); 165 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&pps.weighted_bipred_idc, 2));
161 166
162 // pic_init_qp_minus26: se(v) 167 // pic_init_qp_minus26: se(v)
163 RETURN_EMPTY_ON_FAIL( 168 RETURN_EMPTY_ON_FAIL(
164 bit_buffer->ReadSignedExponentialGolomb(&pps.pic_init_qp_minus26)); 169 bit_buffer->ReadSignedExponentialGolomb(&pps.pic_init_qp_minus26));
170 // Sanity-check parsed value
171 if (pps.pic_init_qp_minus26 > kMaxPicInitQpDeltaValue ||
172 pps.pic_init_qp_minus26 < kMinPicInitQpDeltaValue) {
173 RETURN_EMPTY_ON_FAIL(false);
174 }
165 // pic_init_qs_minus26: se(v) 175 // pic_init_qs_minus26: se(v)
166 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored)); 176 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
167 // chroma_qp_index_offset: se(v) 177 // chroma_qp_index_offset: se(v)
168 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored)); 178 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadExponentialGolomb(&golomb_ignored));
169 // deblocking_filter_control_present_flag: u(1) 179 // deblocking_filter_control_present_flag: u(1)
170 // constrained_intra_pred_flag: u(1) 180 // constrained_intra_pred_flag: u(1)
171 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&bits_tmp, 2)); 181 RETURN_EMPTY_ON_FAIL(bit_buffer->ReadBits(&bits_tmp, 2));
172 // redundant_pic_cnt_present_flag: u(1) 182 // redundant_pic_cnt_present_flag: u(1)
173 RETURN_EMPTY_ON_FAIL( 183 RETURN_EMPTY_ON_FAIL(
174 bit_buffer->ReadBits(&pps.redundant_pic_cnt_present_flag, 1)); 184 bit_buffer->ReadBits(&pps.redundant_pic_cnt_present_flag, 1));
175 185
176 return rtc::Optional<PpsParser::PpsState>(pps); 186 return rtc::Optional<PpsParser::PpsState>(pps);
177 } 187 }
178 188
179 bool PpsParser::ParsePpsIdsInternal(rtc::BitBuffer* bit_buffer, 189 bool PpsParser::ParsePpsIdsInternal(rtc::BitBuffer* bit_buffer,
180 uint32_t* pps_id, 190 uint32_t* pps_id,
181 uint32_t* sps_id) { 191 uint32_t* sps_id) {
182 // pic_parameter_set_id: ue(v) 192 // pic_parameter_set_id: ue(v)
183 if (!bit_buffer->ReadExponentialGolomb(pps_id)) 193 if (!bit_buffer->ReadExponentialGolomb(pps_id))
184 return false; 194 return false;
185 // seq_parameter_set_id: ue(v) 195 // seq_parameter_set_id: ue(v)
186 if (!bit_buffer->ReadExponentialGolomb(sps_id)) 196 if (!bit_buffer->ReadExponentialGolomb(sps_id))
187 return false; 197 return false;
188 return true; 198 return true;
189 } 199 }
190 200
191 } // namespace webrtc 201 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/common_video/h264/h264_bitstream_parser.cc ('k') | webrtc/common_video/h264/pps_parser_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698