staticuint8_th264_get_max_dpb_frames(consth264_sequence_parameter_set_t*p_sps){consth264_level_limits_t*limits=h264_get_level_limits(p_sps);if(limits){unsignedi_frame_height_in_mbs=(p_sps->pic_height_in_map_units_minus1+1)*(2-p_sps->frame_mbs_only_flag);unsignedi_den=(p_sps->pic_width_in_mbs_minus1+1)*i_frame_height_in_mbs;uint8_ti_max_dpb_frames=limits->i_max_dpb_mbs/i_den;if(i_max_dpb_frames<16)returni_max_dpb_frames;}return16;}boolh264_get_dpb_values(consth264_sequence_parameter_set_t*p_sps,uint8_t*pi_depth,unsigned*pi_delay){uint8_ti_max_num_reorder_frames=p_sps->vui.i_max_num_reorder_frames;if(!p_sps->vui.b_bitstream_restriction_flag){switch(p_sps->i_profile)/* E-2.1 */{casePROFILE_H264_BASELINE:i_max_num_reorder_frames=0;/* only I & P */break;casePROFILE_H264_CAVLC_INTRA:casePROFILE_H264_SVC_HIGH:casePROFILE_H264_HIGH:casePROFILE_H264_HIGH_10:casePROFILE_H264_HIGH_422:casePROFILE_H264_HIGH_444_PREDICTIVE:if(p_sps->i_constraint_set_flags&H264_CONSTRAINT_SET_FLAG(3)){i_max_num_reorder_frames=0;/* all IDR */break;}/* fallthrough */default:i_max_num_reorder_frames=h264_get_max_dpb_frames(p_sps);break;}}*pi_depth=i_max_num_reorder_frames;*pi_delay=0;returntrue;}
boolH264Decoder::ProcessSPS(intsps_id,bool*need_new_buffers){DVLOG(4)<<"Processing SPS id:"<<sps_id;constH264SPS*sps=parser_.GetSPS(sps_id);if(!sps)returnfalse;*need_new_buffers=false;if(sps->frame_mbs_only_flag==0){DVLOG(1)<<"frame_mbs_only_flag != 1 not supported";returnfalse;}Sizenew_pic_size=sps->GetCodedSize().value_or(Size());if(new_pic_size.IsEmpty()){DVLOG(1)<<"Invalid picture size";returnfalse;}intwidth_mb=new_pic_size.width()/16;intheight_mb=new_pic_size.height()/16;// Verify that the values are not too large before multiplying.if(std::numeric_limits<int>::max()/width_mb<height_mb){DVLOG(1)<<"Picture size is too big: "<<new_pic_size.ToString();returnfalse;}intlevel=sps->level_idc;intmax_dpb_mbs=LevelToMaxDpbMbs(level);if(max_dpb_mbs==0)returnfalse;// MaxDpbFrames from level limits per spec.size_tmax_dpb_frames=std::min(max_dpb_mbs/(width_mb*height_mb),static_cast<int>(H264DPB::kDPBMaxSize));DVLOG(1)<<"MaxDpbFrames: "<<max_dpb_frames<<", max_num_ref_frames: "<<sps->max_num_ref_frames<<", max_dec_frame_buffering: "<<sps->max_dec_frame_buffering;// Set DPB size to at least the level limit, or what the stream requires.size_tmax_dpb_size=std::max(static_cast<int>(max_dpb_frames),std::max(sps->max_num_ref_frames,sps->max_dec_frame_buffering));// Some non-conforming streams specify more frames are needed than the current// level limit. Allow this, but only up to the maximum number of reference// frames allowed per spec.DVLOG_IF(1,max_dpb_size>max_dpb_frames)<<"Invalid stream, DPB size > MaxDpbFrames";if(max_dpb_size==0||max_dpb_size>H264DPB::kDPBMaxSize){DVLOG(1)<<"Invalid DPB size: "<<max_dpb_size;returnfalse;}if((pic_size_!=new_pic_size)||(dpb_.max_num_pics()!=max_dpb_size)){if(!Flush())returnfalse;DVLOG(1)<<"Codec level: "<<level<<", DPB size: "<<max_dpb_size<<", Picture size: "<<new_pic_size.ToString();*need_new_buffers=true;pic_size_=new_pic_size;dpb_.set_max_num_pics(max_dpb_size);}Rectnew_visible_rect=sps->GetVisibleRect().value_or(Rect());if(visible_rect_!=new_visible_rect){DVLOG(2)<<"New visible rect: "<<new_visible_rect.ToString();visible_rect_=new_visible_rect;}if(!UpdateMaxNumReorderFrames(sps))returnfalse;DVLOG(1)<<"max_num_reorder_frames: "<<max_num_reorder_frames_;returntrue;}boolH264Decoder::UpdateMaxNumReorderFrames(constH264SPS*sps){if(sps->vui_parameters_present_flag&&sps->bitstream_restriction_flag){max_num_reorder_frames_=base::checked_cast<size_t>(sps->max_num_reorder_frames);if(max_num_reorder_frames_>dpb_.max_num_pics()){DVLOG(1)<<"max_num_reorder_frames present, but larger than MaxDpbFrames ("<<max_num_reorder_frames_<<" > "<<dpb_.max_num_pics()<<")";max_num_reorder_frames_=0;returnfalse;}returntrue;}// max_num_reorder_frames not present, infer from profile/constraints// (see VUI semantics in spec).if(sps->constraint_set3_flag){switch(sps->profile_idc){case44:case86:case100:case110:case122:case244:max_num_reorder_frames_=0;break;default:max_num_reorder_frames_=dpb_.max_num_pics();break;}}else{max_num_reorder_frames_=dpb_.max_num_pics();}returntrue;}