@@ -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