Skip to content

Commit 97c6b3f

Browse files
committed
Added resolution validity check to H.264/H.265 parser
1 parent 181ec4b commit 97c6b3f

File tree

2 files changed

+68
-7
lines changed

2 files changed

+68
-7
lines changed

src/projects/modules/bitstream/h264/h264_parser.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,40 @@ bool H264Parser::ParseSPS(const uint8_t *nalu, size_t length, H264SPS &sps)
426426
}
427427
}
428428

429-
sps._width = (pic_width_in_mbs_minus1 + 1) * 16 - 2 * crop_left - 2 * crop_right;
430-
sps._height = ((2 - sps._frame_mbs_only_flag) * (pic_height_in_map_units_minus1 + 1) * 16) - 2 * crop_top - 2 * crop_bottom;
429+
// Calculate the display width and height
430+
// int64_t is used to avoid overflow for extreme cases
431+
int64_t coded_width = (pic_width_in_mbs_minus1 + 1) * 16;
432+
int64_t coded_height = (2 - sps._frame_mbs_only_flag) * (pic_height_in_map_units_minus1 + 1) * 16;
433+
int64_t crop_x = 2 * crop_left + 2 * crop_right;
434+
int64_t crop_y = 2 * crop_top + 2 * crop_bottom;
435+
int64_t display_width = coded_width - crop_x;
436+
int64_t display_height = coded_height - crop_y;
437+
438+
// Validate: Check negative values
439+
if (display_width < 0 || display_height < 0)
440+
{
441+
return false;
442+
}
443+
444+
// Validate: Check maximum width and height (8K, 8192x4320)
445+
if (display_width > 8192 || display_height > 8192)
446+
{
447+
return false;
448+
}
449+
450+
// Validate: Check total pixels
451+
if (display_width * display_height > 8192 * 4320)
452+
{
453+
return false;
454+
}
455+
456+
logtt("Parsed SPS resolution: coded(%lld x %lld), crop(%lld x %lld), display(%lld x %lld)",
457+
coded_width, coded_height,
458+
crop_x, crop_y,
459+
display_width, display_height);
460+
461+
sps._width = static_cast<uint32_t>(display_width);
462+
sps._height = static_cast<uint32_t>(display_height);
431463
}
432464

433465
// [ VUI ]

src/projects/modules/bitstream/h265/h265_parser.cpp

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,7 @@ bool H265Parser::ParseSPS(const uint8_t *nalu, size_t length, H265SPS &sps)
373373
return false;
374374
}
375375

376-
sps._width = pic_width_in_luma_samples;
377-
sps._height = pic_height_in_luma_samples;
378-
376+
uint32_t crop_x = 0, crop_y = 0;
379377
uint32_t conf_win_left_offset, conf_win_right_offset, conf_win_top_offset, conf_win_bottom_offset;
380378
if (conformance_window_flag)
381379
{
@@ -415,10 +413,41 @@ bool H265Parser::ParseSPS(const uint8_t *nalu, size_t length, H265SPS &sps)
415413
sub_height_c = 1;
416414
}
417415

418-
sps._width -= (sub_width_c * (conf_win_left_offset + conf_win_right_offset));
419-
sps._height -= (sub_height_c * (conf_win_top_offset + conf_win_bottom_offset));
416+
crop_x = sub_width_c * (conf_win_left_offset + conf_win_right_offset);
417+
crop_y = sub_height_c * (conf_win_top_offset + conf_win_bottom_offset);
418+
}
419+
420+
int64_t coded_width = pic_width_in_luma_samples;
421+
int64_t coded_height = pic_height_in_luma_samples;
422+
int64_t display_width = pic_width_in_luma_samples - crop_x;
423+
int64_t display_height = pic_height_in_luma_samples - crop_y;
424+
425+
// Validate: Check negative values
426+
if (display_width < 0 || display_height < 0)
427+
{
428+
return false;
420429
}
421430

431+
// Validate: Check maximum width and height (8K, 8192x4320)
432+
if (display_width > 8192 || display_height > 8192)
433+
{
434+
return false;
435+
}
436+
437+
// Validate: Check total pixels
438+
if (display_width * display_height > 8192 * 4320)
439+
{
440+
return false;
441+
}
442+
443+
logtt("Parsed SPS resolution: coded(%lld x %lld), crop(%lld x %lld), display(%lld x %lld)",
444+
coded_width, coded_height,
445+
crop_x, crop_y,
446+
display_width, display_height);
447+
448+
sps._width = static_cast<uint32_t>(display_width);
449+
sps._height = static_cast<uint32_t>(display_height);
450+
422451
uint32_t bit_depth_luma_minus8;
423452
if (parser.ReadUEV(bit_depth_luma_minus8) == false)
424453
{

0 commit comments

Comments
 (0)