| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <algorithm> | |
| 6 #include <limits> | |
| 7 | |
| 8 #include "base/bind.h" | |
| 9 #include "base/bind_helpers.h" | |
| 10 #include "base/numerics/safe_conversions.h" | |
| 11 #include "base/stl_util.h" | |
| 12 #include "content/common/gpu/media/vaapi_h264_decoder.h" | |
| 13 | |
| 14 namespace content { | |
| 15 | |
| 16 // Decode surface, used for decoding and reference. input_id comes from client | |
| 17 // and is associated with the surface that was produced as the result | |
| 18 // of decoding a bitstream buffer with that id. | |
| 19 class VaapiH264Decoder::DecodeSurface { | |
| 20 public: | |
| 21 DecodeSurface(int poc, | |
| 22 int32 input_id, | |
| 23 const scoped_refptr<VASurface>& va_surface); | |
| 24 DecodeSurface(int poc, const scoped_refptr<DecodeSurface>& dec_surface); | |
| 25 ~DecodeSurface(); | |
| 26 | |
| 27 int poc() { | |
| 28 return poc_; | |
| 29 } | |
| 30 | |
| 31 scoped_refptr<VASurface> va_surface() { | |
| 32 return va_surface_; | |
| 33 } | |
| 34 | |
| 35 int32 input_id() { | |
| 36 return input_id_; | |
| 37 } | |
| 38 | |
| 39 private: | |
| 40 int poc_; | |
| 41 int32 input_id_; | |
| 42 scoped_refptr<VASurface> va_surface_; | |
| 43 }; | |
| 44 | |
| 45 VaapiH264Decoder::DecodeSurface::DecodeSurface( | |
| 46 int poc, | |
| 47 int32 input_id, | |
| 48 const scoped_refptr<VASurface>& va_surface) | |
| 49 : poc_(poc), | |
| 50 input_id_(input_id), | |
| 51 va_surface_(va_surface) { | |
| 52 DCHECK(va_surface_.get()); | |
| 53 } | |
| 54 | |
| 55 VaapiH264Decoder::DecodeSurface::~DecodeSurface() { | |
| 56 } | |
| 57 | |
| 58 VaapiH264Decoder::VaapiH264Decoder( | |
| 59 VaapiWrapper* vaapi_wrapper, | |
| 60 const OutputPicCB& output_pic_cb, | |
| 61 const ReportErrorToUmaCB& report_error_to_uma_cb) | |
| 62 : max_pic_order_cnt_lsb_(0), | |
| 63 max_frame_num_(0), | |
| 64 max_pic_num_(0), | |
| 65 max_long_term_frame_idx_(0), | |
| 66 max_num_reorder_frames_(0), | |
| 67 curr_sps_id_(-1), | |
| 68 curr_pps_id_(-1), | |
| 69 vaapi_wrapper_(vaapi_wrapper), | |
| 70 output_pic_cb_(output_pic_cb), | |
| 71 report_error_to_uma_cb_(report_error_to_uma_cb) { | |
| 72 Reset(); | |
| 73 state_ = kNeedStreamMetadata; | |
| 74 } | |
| 75 | |
| 76 VaapiH264Decoder::~VaapiH264Decoder() { | |
| 77 } | |
| 78 | |
| 79 void VaapiH264Decoder::Reset() { | |
| 80 curr_pic_.reset(); | |
| 81 | |
| 82 curr_input_id_ = -1; | |
| 83 frame_num_ = 0; | |
| 84 prev_frame_num_ = -1; | |
| 85 prev_frame_num_offset_ = -1; | |
| 86 | |
| 87 prev_ref_has_memmgmnt5_ = false; | |
| 88 prev_ref_top_field_order_cnt_ = -1; | |
| 89 prev_ref_pic_order_cnt_msb_ = -1; | |
| 90 prev_ref_pic_order_cnt_lsb_ = -1; | |
| 91 prev_ref_field_ = VaapiH264Picture::FIELD_NONE; | |
| 92 | |
| 93 vaapi_wrapper_->DestroyPendingBuffers(); | |
| 94 | |
| 95 ref_pic_list0_.clear(); | |
| 96 ref_pic_list1_.clear(); | |
| 97 | |
| 98 for (DecSurfacesInUse::iterator it = decode_surfaces_in_use_.begin(); | |
| 99 it != decode_surfaces_in_use_.end(); ) { | |
| 100 int poc = it->second->poc(); | |
| 101 // Must be incremented before UnassignSurfaceFromPoC as this call | |
| 102 // invalidates |it|. | |
| 103 ++it; | |
| 104 UnassignSurfaceFromPoC(poc); | |
| 105 } | |
| 106 DCHECK(decode_surfaces_in_use_.empty()); | |
| 107 | |
| 108 dpb_.Clear(); | |
| 109 parser_.Reset(); | |
| 110 last_output_poc_ = std::numeric_limits<int>::min(); | |
| 111 | |
| 112 // If we are in kDecoding, we can resume without processing an SPS. | |
| 113 if (state_ == kDecoding) | |
| 114 state_ = kAfterReset; | |
| 115 } | |
| 116 | |
| 117 void VaapiH264Decoder::ReuseSurface( | |
| 118 const scoped_refptr<VASurface>& va_surface) { | |
| 119 available_va_surfaces_.push_back(va_surface); | |
| 120 } | |
| 121 | |
| 122 // Fill |va_pic| with default/neutral values. | |
| 123 static void InitVAPicture(VAPictureH264* va_pic) { | |
| 124 memset(va_pic, 0, sizeof(*va_pic)); | |
| 125 va_pic->picture_id = VA_INVALID_ID; | |
| 126 va_pic->flags = VA_PICTURE_H264_INVALID; | |
| 127 } | |
| 128 | |
| 129 void VaapiH264Decoder::FillVAPicture(VAPictureH264* va_pic, | |
| 130 VaapiH264Picture* pic) { | |
| 131 DCHECK(pic); | |
| 132 | |
| 133 DecodeSurface* dec_surface = DecodeSurfaceByPoC(pic->pic_order_cnt); | |
| 134 if (!dec_surface) { | |
| 135 // Cannot provide a ref picture, will corrupt output, but may be able | |
| 136 // to recover. | |
| 137 InitVAPicture(va_pic); | |
| 138 return; | |
| 139 } | |
| 140 | |
| 141 va_pic->picture_id = dec_surface->va_surface()->id(); | |
| 142 va_pic->frame_idx = pic->frame_num; | |
| 143 va_pic->flags = 0; | |
| 144 | |
| 145 switch (pic->field) { | |
| 146 case VaapiH264Picture::FIELD_NONE: | |
| 147 break; | |
| 148 case VaapiH264Picture::FIELD_TOP: | |
| 149 va_pic->flags |= VA_PICTURE_H264_TOP_FIELD; | |
| 150 break; | |
| 151 case VaapiH264Picture::FIELD_BOTTOM: | |
| 152 va_pic->flags |= VA_PICTURE_H264_BOTTOM_FIELD; | |
| 153 break; | |
| 154 } | |
| 155 | |
| 156 if (pic->ref) { | |
| 157 va_pic->flags |= pic->long_term ? VA_PICTURE_H264_LONG_TERM_REFERENCE | |
| 158 : VA_PICTURE_H264_SHORT_TERM_REFERENCE; | |
| 159 } | |
| 160 | |
| 161 va_pic->TopFieldOrderCnt = pic->top_field_order_cnt; | |
| 162 va_pic->BottomFieldOrderCnt = pic->bottom_field_order_cnt; | |
| 163 } | |
| 164 | |
| 165 int VaapiH264Decoder::FillVARefFramesFromDPB(VAPictureH264 *va_pics, | |
| 166 int num_pics) { | |
| 167 VaapiH264DPB::Pictures::reverse_iterator rit; | |
| 168 int i; | |
| 169 | |
| 170 // Return reference frames in reverse order of insertion. | |
| 171 // Libva does not document this, but other implementations (e.g. mplayer) | |
| 172 // do it this way as well. | |
| 173 for (rit = dpb_.rbegin(), i = 0; rit != dpb_.rend() && i < num_pics; ++rit) { | |
| 174 if ((*rit)->ref) | |
| 175 FillVAPicture(&va_pics[i++], *rit); | |
| 176 } | |
| 177 | |
| 178 return i; | |
| 179 } | |
| 180 | |
| 181 VaapiH264Decoder::DecodeSurface* VaapiH264Decoder::DecodeSurfaceByPoC(int poc) { | |
| 182 DecSurfacesInUse::iterator iter = decode_surfaces_in_use_.find(poc); | |
| 183 if (iter == decode_surfaces_in_use_.end()) { | |
| 184 DVLOG(1) << "Could not find surface assigned to POC: " << poc; | |
| 185 return NULL; | |
| 186 } | |
| 187 | |
| 188 return iter->second.get(); | |
| 189 } | |
| 190 | |
| 191 bool VaapiH264Decoder::AssignSurfaceToPoC(int32 input_id, int poc) { | |
| 192 if (available_va_surfaces_.empty()) { | |
| 193 DVLOG(1) << "No VA Surfaces available"; | |
| 194 return false; | |
| 195 } | |
| 196 | |
| 197 linked_ptr<DecodeSurface> dec_surface(new DecodeSurface( | |
| 198 poc, input_id, available_va_surfaces_.back())); | |
| 199 available_va_surfaces_.pop_back(); | |
| 200 | |
| 201 DVLOG(4) << "POC " << poc | |
| 202 << " will use surface " << dec_surface->va_surface()->id(); | |
| 203 | |
| 204 bool inserted = decode_surfaces_in_use_.insert( | |
| 205 std::make_pair(poc, dec_surface)).second; | |
| 206 DCHECK(inserted); | |
| 207 | |
| 208 return true; | |
| 209 } | |
| 210 | |
| 211 void VaapiH264Decoder::UnassignSurfaceFromPoC(int poc) { | |
| 212 DecSurfacesInUse::iterator it = decode_surfaces_in_use_.find(poc); | |
| 213 if (it == decode_surfaces_in_use_.end()) { | |
| 214 DVLOG(1) << "Asked to unassign an unassigned POC " << poc; | |
| 215 return; | |
| 216 } | |
| 217 | |
| 218 DVLOG(4) << "POC " << poc << " no longer using VA surface " | |
| 219 << it->second->va_surface()->id(); | |
| 220 | |
| 221 decode_surfaces_in_use_.erase(it); | |
| 222 } | |
| 223 | |
| 224 bool VaapiH264Decoder::SendPPS() { | |
| 225 const media::H264PPS* pps = parser_.GetPPS(curr_pps_id_); | |
| 226 DCHECK(pps); | |
| 227 | |
| 228 const media::H264SPS* sps = parser_.GetSPS(pps->seq_parameter_set_id); | |
| 229 DCHECK(sps); | |
| 230 | |
| 231 DCHECK(curr_pic_.get()); | |
| 232 | |
| 233 VAPictureParameterBufferH264 pic_param; | |
| 234 memset(&pic_param, 0, sizeof(VAPictureParameterBufferH264)); | |
| 235 | |
| 236 #define FROM_SPS_TO_PP(a) pic_param.a = sps->a; | |
| 237 #define FROM_SPS_TO_PP2(a, b) pic_param.b = sps->a; | |
| 238 FROM_SPS_TO_PP2(pic_width_in_mbs_minus1, picture_width_in_mbs_minus1); | |
| 239 // This assumes non-interlaced video | |
| 240 FROM_SPS_TO_PP2(pic_height_in_map_units_minus1, | |
| 241 picture_height_in_mbs_minus1); | |
| 242 FROM_SPS_TO_PP(bit_depth_luma_minus8); | |
| 243 FROM_SPS_TO_PP(bit_depth_chroma_minus8); | |
| 244 #undef FROM_SPS_TO_PP | |
| 245 #undef FROM_SPS_TO_PP2 | |
| 246 | |
| 247 #define FROM_SPS_TO_PP_SF(a) pic_param.seq_fields.bits.a = sps->a; | |
| 248 #define FROM_SPS_TO_PP_SF2(a, b) pic_param.seq_fields.bits.b = sps->a; | |
| 249 FROM_SPS_TO_PP_SF(chroma_format_idc); | |
| 250 FROM_SPS_TO_PP_SF2(separate_colour_plane_flag, | |
| 251 residual_colour_transform_flag); | |
| 252 FROM_SPS_TO_PP_SF(gaps_in_frame_num_value_allowed_flag); | |
| 253 FROM_SPS_TO_PP_SF(frame_mbs_only_flag); | |
| 254 FROM_SPS_TO_PP_SF(mb_adaptive_frame_field_flag); | |
| 255 FROM_SPS_TO_PP_SF(direct_8x8_inference_flag); | |
| 256 pic_param.seq_fields.bits.MinLumaBiPredSize8x8 = (sps->level_idc >= 31); | |
| 257 FROM_SPS_TO_PP_SF(log2_max_frame_num_minus4); | |
| 258 FROM_SPS_TO_PP_SF(pic_order_cnt_type); | |
| 259 FROM_SPS_TO_PP_SF(log2_max_pic_order_cnt_lsb_minus4); | |
| 260 FROM_SPS_TO_PP_SF(delta_pic_order_always_zero_flag); | |
| 261 #undef FROM_SPS_TO_PP_SF | |
| 262 #undef FROM_SPS_TO_PP_SF2 | |
| 263 | |
| 264 #define FROM_PPS_TO_PP(a) pic_param.a = pps->a; | |
| 265 FROM_PPS_TO_PP(num_slice_groups_minus1); | |
| 266 pic_param.slice_group_map_type = 0; | |
| 267 pic_param.slice_group_change_rate_minus1 = 0; | |
| 268 FROM_PPS_TO_PP(pic_init_qp_minus26); | |
| 269 FROM_PPS_TO_PP(pic_init_qs_minus26); | |
| 270 FROM_PPS_TO_PP(chroma_qp_index_offset); | |
| 271 FROM_PPS_TO_PP(second_chroma_qp_index_offset); | |
| 272 #undef FROM_PPS_TO_PP | |
| 273 | |
| 274 #define FROM_PPS_TO_PP_PF(a) pic_param.pic_fields.bits.a = pps->a; | |
| 275 #define FROM_PPS_TO_PP_PF2(a, b) pic_param.pic_fields.bits.b = pps->a; | |
| 276 FROM_PPS_TO_PP_PF(entropy_coding_mode_flag); | |
| 277 FROM_PPS_TO_PP_PF(weighted_pred_flag); | |
| 278 FROM_PPS_TO_PP_PF(weighted_bipred_idc); | |
| 279 FROM_PPS_TO_PP_PF(transform_8x8_mode_flag); | |
| 280 | |
| 281 pic_param.pic_fields.bits.field_pic_flag = 0; | |
| 282 FROM_PPS_TO_PP_PF(constrained_intra_pred_flag); | |
| 283 FROM_PPS_TO_PP_PF2(bottom_field_pic_order_in_frame_present_flag, | |
| 284 pic_order_present_flag); | |
| 285 FROM_PPS_TO_PP_PF(deblocking_filter_control_present_flag); | |
| 286 FROM_PPS_TO_PP_PF(redundant_pic_cnt_present_flag); | |
| 287 pic_param.pic_fields.bits.reference_pic_flag = curr_pic_->ref; | |
| 288 #undef FROM_PPS_TO_PP_PF | |
| 289 #undef FROM_PPS_TO_PP_PF2 | |
| 290 | |
| 291 pic_param.frame_num = curr_pic_->frame_num; | |
| 292 | |
| 293 InitVAPicture(&pic_param.CurrPic); | |
| 294 FillVAPicture(&pic_param.CurrPic, curr_pic_.get()); | |
| 295 | |
| 296 // Init reference pictures' array. | |
| 297 for (int i = 0; i < 16; ++i) | |
| 298 InitVAPicture(&pic_param.ReferenceFrames[i]); | |
| 299 | |
| 300 // And fill it with picture info from DPB. | |
| 301 FillVARefFramesFromDPB(pic_param.ReferenceFrames, | |
| 302 arraysize(pic_param.ReferenceFrames)); | |
| 303 | |
| 304 pic_param.num_ref_frames = sps->max_num_ref_frames; | |
| 305 | |
| 306 return vaapi_wrapper_->SubmitBuffer(VAPictureParameterBufferType, | |
| 307 sizeof(VAPictureParameterBufferH264), | |
| 308 &pic_param); | |
| 309 } | |
| 310 | |
| 311 bool VaapiH264Decoder::SendIQMatrix() { | |
| 312 const media::H264PPS* pps = parser_.GetPPS(curr_pps_id_); | |
| 313 DCHECK(pps); | |
| 314 | |
| 315 VAIQMatrixBufferH264 iq_matrix_buf; | |
| 316 memset(&iq_matrix_buf, 0, sizeof(VAIQMatrixBufferH264)); | |
| 317 | |
| 318 if (pps->pic_scaling_matrix_present_flag) { | |
| 319 for (int i = 0; i < 6; ++i) { | |
| 320 for (int j = 0; j < 16; ++j) | |
| 321 iq_matrix_buf.ScalingList4x4[i][j] = pps->scaling_list4x4[i][j]; | |
| 322 } | |
| 323 | |
| 324 for (int i = 0; i < 2; ++i) { | |
| 325 for (int j = 0; j < 64; ++j) | |
| 326 iq_matrix_buf.ScalingList8x8[i][j] = pps->scaling_list8x8[i][j]; | |
| 327 } | |
| 328 } else { | |
| 329 const media::H264SPS* sps = parser_.GetSPS(pps->seq_parameter_set_id); | |
| 330 DCHECK(sps); | |
| 331 for (int i = 0; i < 6; ++i) { | |
| 332 for (int j = 0; j < 16; ++j) | |
| 333 iq_matrix_buf.ScalingList4x4[i][j] = sps->scaling_list4x4[i][j]; | |
| 334 } | |
| 335 | |
| 336 for (int i = 0; i < 2; ++i) { | |
| 337 for (int j = 0; j < 64; ++j) | |
| 338 iq_matrix_buf.ScalingList8x8[i][j] = sps->scaling_list8x8[i][j]; | |
| 339 } | |
| 340 } | |
| 341 | |
| 342 return vaapi_wrapper_->SubmitBuffer(VAIQMatrixBufferType, | |
| 343 sizeof(VAIQMatrixBufferH264), | |
| 344 &iq_matrix_buf); | |
| 345 } | |
| 346 | |
| 347 bool VaapiH264Decoder::SendVASliceParam(media::H264SliceHeader* slice_hdr) { | |
| 348 const media::H264PPS* pps = parser_.GetPPS(slice_hdr->pic_parameter_set_id); | |
| 349 DCHECK(pps); | |
| 350 | |
| 351 const media::H264SPS* sps = parser_.GetSPS(pps->seq_parameter_set_id); | |
| 352 DCHECK(sps); | |
| 353 | |
| 354 VASliceParameterBufferH264 slice_param; | |
| 355 memset(&slice_param, 0, sizeof(VASliceParameterBufferH264)); | |
| 356 | |
| 357 slice_param.slice_data_size = slice_hdr->nalu_size; | |
| 358 slice_param.slice_data_offset = 0; | |
| 359 slice_param.slice_data_flag = VA_SLICE_DATA_FLAG_ALL; | |
| 360 slice_param.slice_data_bit_offset = slice_hdr->header_bit_size; | |
| 361 | |
| 362 #define SHDRToSP(a) slice_param.a = slice_hdr->a; | |
| 363 SHDRToSP(first_mb_in_slice); | |
| 364 slice_param.slice_type = slice_hdr->slice_type % 5; | |
| 365 SHDRToSP(direct_spatial_mv_pred_flag); | |
| 366 | |
| 367 // TODO posciak: make sure parser sets those even when override flags | |
| 368 // in slice header is off. | |
| 369 SHDRToSP(num_ref_idx_l0_active_minus1); | |
| 370 SHDRToSP(num_ref_idx_l1_active_minus1); | |
| 371 SHDRToSP(cabac_init_idc); | |
| 372 SHDRToSP(slice_qp_delta); | |
| 373 SHDRToSP(disable_deblocking_filter_idc); | |
| 374 SHDRToSP(slice_alpha_c0_offset_div2); | |
| 375 SHDRToSP(slice_beta_offset_div2); | |
| 376 | |
| 377 if (((slice_hdr->IsPSlice() || slice_hdr->IsSPSlice()) && | |
| 378 pps->weighted_pred_flag) || | |
| 379 (slice_hdr->IsBSlice() && pps->weighted_bipred_idc == 1)) { | |
| 380 SHDRToSP(luma_log2_weight_denom); | |
| 381 SHDRToSP(chroma_log2_weight_denom); | |
| 382 | |
| 383 SHDRToSP(luma_weight_l0_flag); | |
| 384 SHDRToSP(luma_weight_l1_flag); | |
| 385 | |
| 386 SHDRToSP(chroma_weight_l0_flag); | |
| 387 SHDRToSP(chroma_weight_l1_flag); | |
| 388 | |
| 389 for (int i = 0; i <= slice_param.num_ref_idx_l0_active_minus1; ++i) { | |
| 390 slice_param.luma_weight_l0[i] = | |
| 391 slice_hdr->pred_weight_table_l0.luma_weight[i]; | |
| 392 slice_param.luma_offset_l0[i] = | |
| 393 slice_hdr->pred_weight_table_l0.luma_offset[i]; | |
| 394 | |
| 395 for (int j = 0; j < 2; ++j) { | |
| 396 slice_param.chroma_weight_l0[i][j] = | |
| 397 slice_hdr->pred_weight_table_l0.chroma_weight[i][j]; | |
| 398 slice_param.chroma_offset_l0[i][j] = | |
| 399 slice_hdr->pred_weight_table_l0.chroma_offset[i][j]; | |
| 400 } | |
| 401 } | |
| 402 | |
| 403 if (slice_hdr->IsBSlice()) { | |
| 404 for (int i = 0; i <= slice_param.num_ref_idx_l1_active_minus1; ++i) { | |
| 405 slice_param.luma_weight_l1[i] = | |
| 406 slice_hdr->pred_weight_table_l1.luma_weight[i]; | |
| 407 slice_param.luma_offset_l1[i] = | |
| 408 slice_hdr->pred_weight_table_l1.luma_offset[i]; | |
| 409 | |
| 410 for (int j = 0; j < 2; ++j) { | |
| 411 slice_param.chroma_weight_l1[i][j] = | |
| 412 slice_hdr->pred_weight_table_l1.chroma_weight[i][j]; | |
| 413 slice_param.chroma_offset_l1[i][j] = | |
| 414 slice_hdr->pred_weight_table_l1.chroma_offset[i][j]; | |
| 415 } | |
| 416 } | |
| 417 } | |
| 418 } | |
| 419 | |
| 420 for (int i = 0; i < 32; ++i) { | |
| 421 InitVAPicture(&slice_param.RefPicList0[i]); | |
| 422 InitVAPicture(&slice_param.RefPicList1[i]); | |
| 423 } | |
| 424 | |
| 425 int i; | |
| 426 VaapiH264Picture::PtrVector::iterator it; | |
| 427 for (it = ref_pic_list0_.begin(), i = 0; it != ref_pic_list0_.end() && *it; | |
| 428 ++it, ++i) | |
| 429 FillVAPicture(&slice_param.RefPicList0[i], *it); | |
| 430 for (it = ref_pic_list1_.begin(), i = 0; it != ref_pic_list1_.end() && *it; | |
| 431 ++it, ++i) | |
| 432 FillVAPicture(&slice_param.RefPicList1[i], *it); | |
| 433 | |
| 434 return vaapi_wrapper_->SubmitBuffer(VASliceParameterBufferType, | |
| 435 sizeof(VASliceParameterBufferH264), | |
| 436 &slice_param); | |
| 437 } | |
| 438 | |
| 439 bool VaapiH264Decoder::SendSliceData(const uint8* ptr, size_t size) { | |
| 440 // Can't help it, blame libva... | |
| 441 void* non_const_ptr = const_cast<uint8*>(ptr); | |
| 442 return vaapi_wrapper_->SubmitBuffer(VASliceDataBufferType, size, | |
| 443 non_const_ptr); | |
| 444 } | |
| 445 | |
| 446 bool VaapiH264Decoder::PrepareRefPicLists(media::H264SliceHeader* slice_hdr) { | |
| 447 ref_pic_list0_.clear(); | |
| 448 ref_pic_list1_.clear(); | |
| 449 | |
| 450 // Fill reference picture lists for B and S/SP slices. | |
| 451 if (slice_hdr->IsPSlice() || slice_hdr->IsSPSlice()) { | |
| 452 ConstructReferencePicListsP(slice_hdr); | |
| 453 return ModifyReferencePicList(slice_hdr, 0); | |
| 454 } | |
| 455 | |
| 456 if (slice_hdr->IsBSlice()) { | |
| 457 ConstructReferencePicListsB(slice_hdr); | |
| 458 return ModifyReferencePicList(slice_hdr, 0) && | |
| 459 ModifyReferencePicList(slice_hdr, 1); | |
| 460 } | |
| 461 | |
| 462 return true; | |
| 463 } | |
| 464 | |
| 465 bool VaapiH264Decoder::QueueSlice(media::H264SliceHeader* slice_hdr) { | |
| 466 DCHECK(curr_pic_.get()); | |
| 467 | |
| 468 if (!PrepareRefPicLists(slice_hdr)) | |
| 469 return false; | |
| 470 | |
| 471 if (!SendVASliceParam(slice_hdr)) | |
| 472 return false; | |
| 473 | |
| 474 if (!SendSliceData(slice_hdr->nalu_data, slice_hdr->nalu_size)) | |
| 475 return false; | |
| 476 | |
| 477 return true; | |
| 478 } | |
| 479 | |
| 480 // TODO(posciak) start using vaMapBuffer instead of vaCreateBuffer wherever | |
| 481 // possible. | |
| 482 bool VaapiH264Decoder::DecodePicture() { | |
| 483 DCHECK(curr_pic_.get()); | |
| 484 | |
| 485 DVLOG(4) << "Decoding POC " << curr_pic_->pic_order_cnt; | |
| 486 DecodeSurface* dec_surface = DecodeSurfaceByPoC(curr_pic_->pic_order_cnt); | |
| 487 if (!dec_surface) { | |
| 488 DVLOG(1) << "Asked to decode an invalid POC " << curr_pic_->pic_order_cnt; | |
| 489 return false; | |
| 490 } | |
| 491 | |
| 492 if (!vaapi_wrapper_->ExecuteAndDestroyPendingBuffers( | |
| 493 dec_surface->va_surface()->id())) { | |
| 494 DVLOG(1) << "Failed decoding picture"; | |
| 495 return false; | |
| 496 } | |
| 497 | |
| 498 return true; | |
| 499 } | |
| 500 | |
| 501 bool VaapiH264Decoder::InitCurrPicture(media::H264SliceHeader* slice_hdr) { | |
| 502 DCHECK(curr_pic_.get()); | |
| 503 | |
| 504 memset(curr_pic_.get(), 0, sizeof(VaapiH264Picture)); | |
| 505 | |
| 506 curr_pic_->idr = slice_hdr->idr_pic_flag; | |
| 507 | |
| 508 if (slice_hdr->field_pic_flag) { | |
| 509 curr_pic_->field = slice_hdr->bottom_field_flag | |
| 510 ? VaapiH264Picture::FIELD_BOTTOM | |
| 511 : VaapiH264Picture::FIELD_TOP; | |
| 512 } else { | |
| 513 curr_pic_->field = VaapiH264Picture::FIELD_NONE; | |
| 514 } | |
| 515 | |
| 516 curr_pic_->ref = slice_hdr->nal_ref_idc != 0; | |
| 517 // This assumes non-interlaced stream. | |
| 518 curr_pic_->frame_num = curr_pic_->pic_num = slice_hdr->frame_num; | |
| 519 | |
| 520 if (!CalculatePicOrderCounts(slice_hdr)) | |
| 521 return false; | |
| 522 | |
| 523 // Try to get an empty surface to decode this picture to. | |
| 524 if (!AssignSurfaceToPoC(curr_input_id_, curr_pic_->pic_order_cnt)) { | |
| 525 DVLOG(1) << "Failed getting a free surface for a picture"; | |
| 526 return false; | |
| 527 } | |
| 528 | |
| 529 curr_pic_->long_term_reference_flag = slice_hdr->long_term_reference_flag; | |
| 530 curr_pic_->adaptive_ref_pic_marking_mode_flag = | |
| 531 slice_hdr->adaptive_ref_pic_marking_mode_flag; | |
| 532 | |
| 533 // If the slice header indicates we will have to perform reference marking | |
| 534 // process after this picture is decoded, store required data for that | |
| 535 // purpose. | |
| 536 if (slice_hdr->adaptive_ref_pic_marking_mode_flag) { | |
| 537 static_assert(sizeof(curr_pic_->ref_pic_marking) == | |
| 538 sizeof(slice_hdr->ref_pic_marking), | |
| 539 "ref_pic_marking array sizes do not match"); | |
| 540 memcpy(curr_pic_->ref_pic_marking, slice_hdr->ref_pic_marking, | |
| 541 sizeof(curr_pic_->ref_pic_marking)); | |
| 542 } | |
| 543 | |
| 544 return true; | |
| 545 } | |
| 546 | |
| 547 bool VaapiH264Decoder::CalculatePicOrderCounts( | |
| 548 media::H264SliceHeader* slice_hdr) { | |
| 549 DCHECK_NE(curr_sps_id_, -1); | |
| 550 const media::H264SPS* sps = parser_.GetSPS(curr_sps_id_); | |
| 551 | |
| 552 int pic_order_cnt_lsb = slice_hdr->pic_order_cnt_lsb; | |
| 553 curr_pic_->pic_order_cnt_lsb = pic_order_cnt_lsb; | |
| 554 | |
| 555 switch (sps->pic_order_cnt_type) { | |
| 556 case 0: | |
| 557 // See spec 8.2.1.1. | |
| 558 int prev_pic_order_cnt_msb, prev_pic_order_cnt_lsb; | |
| 559 if (slice_hdr->idr_pic_flag) { | |
| 560 prev_pic_order_cnt_msb = prev_pic_order_cnt_lsb = 0; | |
| 561 } else { | |
| 562 if (prev_ref_has_memmgmnt5_) { | |
| 563 if (prev_ref_field_ != VaapiH264Picture::FIELD_BOTTOM) { | |
| 564 prev_pic_order_cnt_msb = 0; | |
| 565 prev_pic_order_cnt_lsb = prev_ref_top_field_order_cnt_; | |
| 566 } else { | |
| 567 prev_pic_order_cnt_msb = 0; | |
| 568 prev_pic_order_cnt_lsb = 0; | |
| 569 } | |
| 570 } else { | |
| 571 prev_pic_order_cnt_msb = prev_ref_pic_order_cnt_msb_; | |
| 572 prev_pic_order_cnt_lsb = prev_ref_pic_order_cnt_lsb_; | |
| 573 } | |
| 574 } | |
| 575 | |
| 576 DCHECK_NE(max_pic_order_cnt_lsb_, 0); | |
| 577 if ((pic_order_cnt_lsb < prev_pic_order_cnt_lsb) && | |
| 578 (prev_pic_order_cnt_lsb - pic_order_cnt_lsb >= | |
| 579 max_pic_order_cnt_lsb_ / 2)) { | |
| 580 curr_pic_->pic_order_cnt_msb = prev_pic_order_cnt_msb + | |
| 581 max_pic_order_cnt_lsb_; | |
| 582 } else if ((pic_order_cnt_lsb > prev_pic_order_cnt_lsb) && | |
| 583 (pic_order_cnt_lsb - prev_pic_order_cnt_lsb > | |
| 584 max_pic_order_cnt_lsb_ / 2)) { | |
| 585 curr_pic_->pic_order_cnt_msb = prev_pic_order_cnt_msb - | |
| 586 max_pic_order_cnt_lsb_; | |
| 587 } else { | |
| 588 curr_pic_->pic_order_cnt_msb = prev_pic_order_cnt_msb; | |
| 589 } | |
| 590 | |
| 591 if (curr_pic_->field != VaapiH264Picture::FIELD_BOTTOM) { | |
| 592 curr_pic_->top_field_order_cnt = curr_pic_->pic_order_cnt_msb + | |
| 593 pic_order_cnt_lsb; | |
| 594 } | |
| 595 | |
| 596 if (curr_pic_->field != VaapiH264Picture::FIELD_TOP) { | |
| 597 // TODO posciak: perhaps replace with pic->field? | |
| 598 if (!slice_hdr->field_pic_flag) { | |
| 599 curr_pic_->bottom_field_order_cnt = curr_pic_->top_field_order_cnt + | |
| 600 slice_hdr->delta_pic_order_cnt_bottom; | |
| 601 } else { | |
| 602 curr_pic_->bottom_field_order_cnt = curr_pic_->pic_order_cnt_msb + | |
| 603 pic_order_cnt_lsb; | |
| 604 } | |
| 605 } | |
| 606 break; | |
| 607 | |
| 608 case 1: { | |
| 609 // See spec 8.2.1.2. | |
| 610 if (prev_has_memmgmnt5_) | |
| 611 prev_frame_num_offset_ = 0; | |
| 612 | |
| 613 if (slice_hdr->idr_pic_flag) | |
| 614 curr_pic_->frame_num_offset = 0; | |
| 615 else if (prev_frame_num_ > slice_hdr->frame_num) | |
| 616 curr_pic_->frame_num_offset = prev_frame_num_offset_ + max_frame_num_; | |
| 617 else | |
| 618 curr_pic_->frame_num_offset = prev_frame_num_offset_; | |
| 619 | |
| 620 int abs_frame_num = 0; | |
| 621 if (sps->num_ref_frames_in_pic_order_cnt_cycle != 0) | |
| 622 abs_frame_num = curr_pic_->frame_num_offset + slice_hdr->frame_num; | |
| 623 else | |
| 624 abs_frame_num = 0; | |
| 625 | |
| 626 if (slice_hdr->nal_ref_idc == 0 && abs_frame_num > 0) | |
| 627 --abs_frame_num; | |
| 628 | |
| 629 int expected_pic_order_cnt = 0; | |
| 630 if (abs_frame_num > 0) { | |
| 631 if (sps->num_ref_frames_in_pic_order_cnt_cycle == 0) { | |
| 632 DVLOG(1) << "Invalid num_ref_frames_in_pic_order_cnt_cycle " | |
| 633 << "in stream"; | |
| 634 return false; | |
| 635 } | |
| 636 | |
| 637 int pic_order_cnt_cycle_cnt = (abs_frame_num - 1) / | |
| 638 sps->num_ref_frames_in_pic_order_cnt_cycle; | |
| 639 int frame_num_in_pic_order_cnt_cycle = (abs_frame_num - 1) % | |
| 640 sps->num_ref_frames_in_pic_order_cnt_cycle; | |
| 641 | |
| 642 expected_pic_order_cnt = pic_order_cnt_cycle_cnt * | |
| 643 sps->expected_delta_per_pic_order_cnt_cycle; | |
| 644 // frame_num_in_pic_order_cnt_cycle is verified < 255 in parser | |
| 645 for (int i = 0; i <= frame_num_in_pic_order_cnt_cycle; ++i) | |
| 646 expected_pic_order_cnt += sps->offset_for_ref_frame[i]; | |
| 647 } | |
| 648 | |
| 649 if (!slice_hdr->nal_ref_idc) | |
| 650 expected_pic_order_cnt += sps->offset_for_non_ref_pic; | |
| 651 | |
| 652 if (!slice_hdr->field_pic_flag) { | |
| 653 curr_pic_->top_field_order_cnt = expected_pic_order_cnt + | |
| 654 slice_hdr->delta_pic_order_cnt0; | |
| 655 curr_pic_->bottom_field_order_cnt = curr_pic_->top_field_order_cnt + | |
| 656 sps->offset_for_top_to_bottom_field + | |
| 657 slice_hdr->delta_pic_order_cnt1; | |
| 658 } else if (!slice_hdr->bottom_field_flag) { | |
| 659 curr_pic_->top_field_order_cnt = expected_pic_order_cnt + | |
| 660 slice_hdr->delta_pic_order_cnt0; | |
| 661 } else { | |
| 662 curr_pic_->bottom_field_order_cnt = expected_pic_order_cnt + | |
| 663 sps->offset_for_top_to_bottom_field + | |
| 664 slice_hdr->delta_pic_order_cnt0; | |
| 665 } | |
| 666 break; | |
| 667 } | |
| 668 | |
| 669 case 2: | |
| 670 // See spec 8.2.1.3. | |
| 671 if (prev_has_memmgmnt5_) | |
| 672 prev_frame_num_offset_ = 0; | |
| 673 | |
| 674 if (slice_hdr->idr_pic_flag) | |
| 675 curr_pic_->frame_num_offset = 0; | |
| 676 else if (prev_frame_num_ > slice_hdr->frame_num) | |
| 677 curr_pic_->frame_num_offset = prev_frame_num_offset_ + max_frame_num_; | |
| 678 else | |
| 679 curr_pic_->frame_num_offset = prev_frame_num_offset_; | |
| 680 | |
| 681 int temp_pic_order_cnt; | |
| 682 if (slice_hdr->idr_pic_flag) { | |
| 683 temp_pic_order_cnt = 0; | |
| 684 } else if (!slice_hdr->nal_ref_idc) { | |
| 685 temp_pic_order_cnt = | |
| 686 2 * (curr_pic_->frame_num_offset + slice_hdr->frame_num) - 1; | |
| 687 } else { | |
| 688 temp_pic_order_cnt = 2 * (curr_pic_->frame_num_offset + | |
| 689 slice_hdr->frame_num); | |
| 690 } | |
| 691 | |
| 692 if (!slice_hdr->field_pic_flag) { | |
| 693 curr_pic_->top_field_order_cnt = temp_pic_order_cnt; | |
| 694 curr_pic_->bottom_field_order_cnt = temp_pic_order_cnt; | |
| 695 } else if (slice_hdr->bottom_field_flag) { | |
| 696 curr_pic_->bottom_field_order_cnt = temp_pic_order_cnt; | |
| 697 } else { | |
| 698 curr_pic_->top_field_order_cnt = temp_pic_order_cnt; | |
| 699 } | |
| 700 break; | |
| 701 | |
| 702 default: | |
| 703 DVLOG(1) << "Invalid pic_order_cnt_type: " << sps->pic_order_cnt_type; | |
| 704 return false; | |
| 705 } | |
| 706 | |
| 707 switch (curr_pic_->field) { | |
| 708 case VaapiH264Picture::FIELD_NONE: | |
| 709 curr_pic_->pic_order_cnt = std::min(curr_pic_->top_field_order_cnt, | |
| 710 curr_pic_->bottom_field_order_cnt); | |
| 711 break; | |
| 712 case VaapiH264Picture::FIELD_TOP: | |
| 713 curr_pic_->pic_order_cnt = curr_pic_->top_field_order_cnt; | |
| 714 break; | |
| 715 case VaapiH264Picture::FIELD_BOTTOM: | |
| 716 curr_pic_->pic_order_cnt = curr_pic_->bottom_field_order_cnt; | |
| 717 break; | |
| 718 } | |
| 719 | |
| 720 return true; | |
| 721 } | |
| 722 | |
| 723 void VaapiH264Decoder::UpdatePicNums() { | |
| 724 for (VaapiH264DPB::Pictures::iterator it = dpb_.begin(); it != dpb_.end(); | |
| 725 ++it) { | |
| 726 VaapiH264Picture* pic = *it; | |
| 727 DCHECK(pic); | |
| 728 if (!pic->ref) | |
| 729 continue; | |
| 730 | |
| 731 // Below assumes non-interlaced stream. | |
| 732 DCHECK_EQ(pic->field, VaapiH264Picture::FIELD_NONE); | |
| 733 if (pic->long_term) { | |
| 734 pic->long_term_pic_num = pic->long_term_frame_idx; | |
| 735 } else { | |
| 736 if (pic->frame_num > frame_num_) | |
| 737 pic->frame_num_wrap = pic->frame_num - max_frame_num_; | |
| 738 else | |
| 739 pic->frame_num_wrap = pic->frame_num; | |
| 740 | |
| 741 pic->pic_num = pic->frame_num_wrap; | |
| 742 } | |
| 743 } | |
| 744 } | |
| 745 | |
| 746 struct PicNumDescCompare { | |
| 747 bool operator()(const VaapiH264Picture* a, const VaapiH264Picture* b) const { | |
| 748 return a->pic_num > b->pic_num; | |
| 749 } | |
| 750 }; | |
| 751 | |
| 752 struct LongTermPicNumAscCompare { | |
| 753 bool operator()(const VaapiH264Picture* a, const VaapiH264Picture* b) const { | |
| 754 return a->long_term_pic_num < b->long_term_pic_num; | |
| 755 } | |
| 756 }; | |
| 757 | |
| 758 void VaapiH264Decoder::ConstructReferencePicListsP( | |
| 759 media::H264SliceHeader* slice_hdr) { | |
| 760 // RefPicList0 (8.2.4.2.1) [[1] [2]], where: | |
| 761 // [1] shortterm ref pics sorted by descending pic_num, | |
| 762 // [2] longterm ref pics by ascending long_term_pic_num. | |
| 763 DCHECK(ref_pic_list0_.empty() && ref_pic_list1_.empty()); | |
| 764 // First get the short ref pics... | |
| 765 dpb_.GetShortTermRefPicsAppending(ref_pic_list0_); | |
| 766 size_t num_short_refs = ref_pic_list0_.size(); | |
| 767 | |
| 768 // and sort them to get [1]. | |
| 769 std::sort(ref_pic_list0_.begin(), ref_pic_list0_.end(), PicNumDescCompare()); | |
| 770 | |
| 771 // Now get long term pics and sort them by long_term_pic_num to get [2]. | |
| 772 dpb_.GetLongTermRefPicsAppending(ref_pic_list0_); | |
| 773 std::sort(ref_pic_list0_.begin() + num_short_refs, ref_pic_list0_.end(), | |
| 774 LongTermPicNumAscCompare()); | |
| 775 | |
| 776 // Cut off if we have more than requested in slice header. | |
| 777 ref_pic_list0_.resize(slice_hdr->num_ref_idx_l0_active_minus1 + 1); | |
| 778 } | |
| 779 | |
| 780 struct POCAscCompare { | |
| 781 bool operator()(const VaapiH264Picture* a, const VaapiH264Picture* b) const { | |
| 782 return a->pic_order_cnt < b->pic_order_cnt; | |
| 783 } | |
| 784 }; | |
| 785 | |
| 786 struct POCDescCompare { | |
| 787 bool operator()(const VaapiH264Picture* a, const VaapiH264Picture* b) const { | |
| 788 return a->pic_order_cnt > b->pic_order_cnt; | |
| 789 } | |
| 790 }; | |
| 791 | |
| 792 void VaapiH264Decoder::ConstructReferencePicListsB( | |
| 793 media::H264SliceHeader* slice_hdr) { | |
| 794 // RefPicList0 (8.2.4.2.3) [[1] [2] [3]], where: | |
| 795 // [1] shortterm ref pics with POC < curr_pic's POC sorted by descending POC, | |
| 796 // [2] shortterm ref pics with POC > curr_pic's POC by ascending POC, | |
| 797 // [3] longterm ref pics by ascending long_term_pic_num. | |
| 798 DCHECK(ref_pic_list0_.empty() && ref_pic_list1_.empty()); | |
| 799 dpb_.GetShortTermRefPicsAppending(ref_pic_list0_); | |
| 800 size_t num_short_refs = ref_pic_list0_.size(); | |
| 801 | |
| 802 // First sort ascending, this will put [1] in right place and finish [2]. | |
| 803 std::sort(ref_pic_list0_.begin(), ref_pic_list0_.end(), POCAscCompare()); | |
| 804 | |
| 805 // Find first with POC > curr_pic's POC to get first element in [2]... | |
| 806 VaapiH264Picture::PtrVector::iterator iter; | |
| 807 iter = std::upper_bound(ref_pic_list0_.begin(), ref_pic_list0_.end(), | |
| 808 curr_pic_.get(), POCAscCompare()); | |
| 809 | |
| 810 // and sort [1] descending, thus finishing sequence [1] [2]. | |
| 811 std::sort(ref_pic_list0_.begin(), iter, POCDescCompare()); | |
| 812 | |
| 813 // Now add [3] and sort by ascending long_term_pic_num. | |
| 814 dpb_.GetLongTermRefPicsAppending(ref_pic_list0_); | |
| 815 std::sort(ref_pic_list0_.begin() + num_short_refs, ref_pic_list0_.end(), | |
| 816 LongTermPicNumAscCompare()); | |
| 817 | |
| 818 // RefPicList1 (8.2.4.2.4) [[1] [2] [3]], where: | |
| 819 // [1] shortterm ref pics with POC > curr_pic's POC sorted by ascending POC, | |
| 820 // [2] shortterm ref pics with POC < curr_pic's POC by descending POC, | |
| 821 // [3] longterm ref pics by ascending long_term_pic_num. | |
| 822 | |
| 823 dpb_.GetShortTermRefPicsAppending(ref_pic_list1_); | |
| 824 num_short_refs = ref_pic_list1_.size(); | |
| 825 | |
| 826 // First sort by descending POC. | |
| 827 std::sort(ref_pic_list1_.begin(), ref_pic_list1_.end(), POCDescCompare()); | |
| 828 | |
| 829 // Find first with POC < curr_pic's POC to get first element in [2]... | |
| 830 iter = std::upper_bound(ref_pic_list1_.begin(), ref_pic_list1_.end(), | |
| 831 curr_pic_.get(), POCDescCompare()); | |
| 832 | |
| 833 // and sort [1] ascending. | |
| 834 std::sort(ref_pic_list1_.begin(), iter, POCAscCompare()); | |
| 835 | |
| 836 // Now add [3] and sort by ascending long_term_pic_num | |
| 837 dpb_.GetShortTermRefPicsAppending(ref_pic_list1_); | |
| 838 std::sort(ref_pic_list1_.begin() + num_short_refs, ref_pic_list1_.end(), | |
| 839 LongTermPicNumAscCompare()); | |
| 840 | |
| 841 // If lists identical, swap first two entries in RefPicList1 (spec 8.2.4.2.3) | |
| 842 if (ref_pic_list1_.size() > 1 && | |
| 843 std::equal(ref_pic_list0_.begin(), ref_pic_list0_.end(), | |
| 844 ref_pic_list1_.begin())) | |
| 845 std::swap(ref_pic_list1_[0], ref_pic_list1_[1]); | |
| 846 | |
| 847 // Per 8.2.4.2 it's possible for num_ref_idx_lX_active_minus1 to indicate | |
| 848 // there should be more ref pics on list than we constructed. | |
| 849 // Those superfluous ones should be treated as non-reference. | |
| 850 ref_pic_list0_.resize(slice_hdr->num_ref_idx_l0_active_minus1 + 1); | |
| 851 ref_pic_list1_.resize(slice_hdr->num_ref_idx_l1_active_minus1 + 1); | |
| 852 } | |
| 853 | |
| 854 // See 8.2.4 | |
| 855 int VaapiH264Decoder::PicNumF(VaapiH264Picture* pic) { | |
| 856 if (!pic) | |
| 857 return -1; | |
| 858 | |
| 859 if (!pic->long_term) | |
| 860 return pic->pic_num; | |
| 861 else | |
| 862 return max_pic_num_; | |
| 863 } | |
| 864 | |
| 865 // See 8.2.4 | |
| 866 int VaapiH264Decoder::LongTermPicNumF(VaapiH264Picture* pic) { | |
| 867 if (pic->ref && pic->long_term) | |
| 868 return pic->long_term_pic_num; | |
| 869 else | |
| 870 return 2 * (max_long_term_frame_idx_ + 1); | |
| 871 } | |
| 872 | |
| 873 // Shift elements on the |v| starting from |from| to |to|, inclusive, | |
| 874 // one position to the right and insert pic at |from|. | |
| 875 static void ShiftRightAndInsert(VaapiH264Picture::PtrVector* v, | |
| 876 int from, | |
| 877 int to, | |
| 878 VaapiH264Picture* pic) { | |
| 879 // Security checks, do not disable in Debug mode. | |
| 880 CHECK(from <= to); | |
| 881 CHECK(to <= std::numeric_limits<int>::max() - 2); | |
| 882 // Additional checks. Debug mode ok. | |
| 883 DCHECK(v); | |
| 884 DCHECK(pic); | |
| 885 DCHECK((to + 1 == static_cast<int>(v->size())) || | |
| 886 (to + 2 == static_cast<int>(v->size()))); | |
| 887 | |
| 888 v->resize(to + 2); | |
| 889 | |
| 890 for (int i = to + 1; i > from; --i) | |
| 891 (*v)[i] = (*v)[i - 1]; | |
| 892 | |
| 893 (*v)[from] = pic; | |
| 894 } | |
| 895 | |
| 896 bool VaapiH264Decoder::ModifyReferencePicList(media::H264SliceHeader* slice_hdr, | |
| 897 int list) { | |
| 898 int num_ref_idx_lX_active_minus1; | |
| 899 VaapiH264Picture::PtrVector* ref_pic_listx; | |
| 900 media::H264ModificationOfPicNum* list_mod; | |
| 901 | |
| 902 // This can process either ref_pic_list0 or ref_pic_list1, depending on | |
| 903 // the list argument. Set up pointers to proper list to be processed here. | |
| 904 if (list == 0) { | |
| 905 if (!slice_hdr->ref_pic_list_modification_flag_l0) | |
| 906 return true; | |
| 907 | |
| 908 list_mod = slice_hdr->ref_list_l0_modifications; | |
| 909 num_ref_idx_lX_active_minus1 = ref_pic_list0_.size() - 1; | |
| 910 | |
| 911 ref_pic_listx = &ref_pic_list0_; | |
| 912 } else { | |
| 913 if (!slice_hdr->ref_pic_list_modification_flag_l1) | |
| 914 return true; | |
| 915 | |
| 916 list_mod = slice_hdr->ref_list_l1_modifications; | |
| 917 num_ref_idx_lX_active_minus1 = ref_pic_list1_.size() - 1; | |
| 918 | |
| 919 ref_pic_listx = &ref_pic_list1_; | |
| 920 } | |
| 921 | |
| 922 DCHECK_GE(num_ref_idx_lX_active_minus1, 0); | |
| 923 | |
| 924 // Spec 8.2.4.3: | |
| 925 // Reorder pictures on the list in a way specified in the stream. | |
| 926 int pic_num_lx_pred = curr_pic_->pic_num; | |
| 927 int ref_idx_lx = 0; | |
| 928 int pic_num_lx_no_wrap; | |
| 929 int pic_num_lx; | |
| 930 bool done = false; | |
| 931 VaapiH264Picture* pic; | |
| 932 for (int i = 0; i < media::H264SliceHeader::kRefListModSize && !done; ++i) { | |
| 933 switch (list_mod->modification_of_pic_nums_idc) { | |
| 934 case 0: | |
| 935 case 1: | |
| 936 // Modify short reference picture position. | |
| 937 if (list_mod->modification_of_pic_nums_idc == 0) { | |
| 938 // Subtract given value from predicted PicNum. | |
| 939 pic_num_lx_no_wrap = pic_num_lx_pred - | |
| 940 (static_cast<int>(list_mod->abs_diff_pic_num_minus1) + 1); | |
| 941 // Wrap around max_pic_num_ if it becomes < 0 as result | |
| 942 // of subtraction. | |
| 943 if (pic_num_lx_no_wrap < 0) | |
| 944 pic_num_lx_no_wrap += max_pic_num_; | |
| 945 } else { | |
| 946 // Add given value to predicted PicNum. | |
| 947 pic_num_lx_no_wrap = pic_num_lx_pred + | |
| 948 (static_cast<int>(list_mod->abs_diff_pic_num_minus1) + 1); | |
| 949 // Wrap around max_pic_num_ if it becomes >= max_pic_num_ as result | |
| 950 // of the addition. | |
| 951 if (pic_num_lx_no_wrap >= max_pic_num_) | |
| 952 pic_num_lx_no_wrap -= max_pic_num_; | |
| 953 } | |
| 954 | |
| 955 // For use in next iteration. | |
| 956 pic_num_lx_pred = pic_num_lx_no_wrap; | |
| 957 | |
| 958 if (pic_num_lx_no_wrap > curr_pic_->pic_num) | |
| 959 pic_num_lx = pic_num_lx_no_wrap - max_pic_num_; | |
| 960 else | |
| 961 pic_num_lx = pic_num_lx_no_wrap; | |
| 962 | |
| 963 DCHECK_LT(num_ref_idx_lX_active_minus1 + 1, | |
| 964 media::H264SliceHeader::kRefListModSize); | |
| 965 pic = dpb_.GetShortRefPicByPicNum(pic_num_lx); | |
| 966 if (!pic) { | |
| 967 DVLOG(1) << "Malformed stream, no pic num " << pic_num_lx; | |
| 968 return false; | |
| 969 } | |
| 970 ShiftRightAndInsert(ref_pic_listx, ref_idx_lx, | |
| 971 num_ref_idx_lX_active_minus1, pic); | |
| 972 ref_idx_lx++; | |
| 973 | |
| 974 for (int src = ref_idx_lx, dst = ref_idx_lx; | |
| 975 src <= num_ref_idx_lX_active_minus1 + 1; ++src) { | |
| 976 if (PicNumF((*ref_pic_listx)[src]) != pic_num_lx) | |
| 977 (*ref_pic_listx)[dst++] = (*ref_pic_listx)[src]; | |
| 978 } | |
| 979 break; | |
| 980 | |
| 981 case 2: | |
| 982 // Modify long term reference picture position. | |
| 983 DCHECK_LT(num_ref_idx_lX_active_minus1 + 1, | |
| 984 media::H264SliceHeader::kRefListModSize); | |
| 985 pic = dpb_.GetLongRefPicByLongTermPicNum(list_mod->long_term_pic_num); | |
| 986 if (!pic) { | |
| 987 DVLOG(1) << "Malformed stream, no pic num " | |
| 988 << list_mod->long_term_pic_num; | |
| 989 return false; | |
| 990 } | |
| 991 ShiftRightAndInsert(ref_pic_listx, ref_idx_lx, | |
| 992 num_ref_idx_lX_active_minus1, pic); | |
| 993 ref_idx_lx++; | |
| 994 | |
| 995 for (int src = ref_idx_lx, dst = ref_idx_lx; | |
| 996 src <= num_ref_idx_lX_active_minus1 + 1; ++src) { | |
| 997 if (LongTermPicNumF((*ref_pic_listx)[src]) | |
| 998 != static_cast<int>(list_mod->long_term_pic_num)) | |
| 999 (*ref_pic_listx)[dst++] = (*ref_pic_listx)[src]; | |
| 1000 } | |
| 1001 break; | |
| 1002 | |
| 1003 case 3: | |
| 1004 // End of modification list. | |
| 1005 done = true; | |
| 1006 break; | |
| 1007 | |
| 1008 default: | |
| 1009 // May be recoverable. | |
| 1010 DVLOG(1) << "Invalid modification_of_pic_nums_idc=" | |
| 1011 << list_mod->modification_of_pic_nums_idc | |
| 1012 << " in position " << i; | |
| 1013 break; | |
| 1014 } | |
| 1015 | |
| 1016 ++list_mod; | |
| 1017 } | |
| 1018 | |
| 1019 // Per NOTE 2 in 8.2.4.3.2, the ref_pic_listx size in the above loop is | |
| 1020 // temporarily made one element longer than the required final list. | |
| 1021 // Resize the list back to its required size. | |
| 1022 ref_pic_listx->resize(num_ref_idx_lX_active_minus1 + 1); | |
| 1023 | |
| 1024 return true; | |
| 1025 } | |
| 1026 | |
| 1027 bool VaapiH264Decoder::OutputPic(VaapiH264Picture* pic) { | |
| 1028 DCHECK(!pic->outputted); | |
| 1029 pic->outputted = true; | |
| 1030 last_output_poc_ = pic->pic_order_cnt; | |
| 1031 | |
| 1032 DecodeSurface* dec_surface = DecodeSurfaceByPoC(pic->pic_order_cnt); | |
| 1033 if (!dec_surface) | |
| 1034 return false; | |
| 1035 | |
| 1036 DCHECK_GE(dec_surface->input_id(), 0); | |
| 1037 DVLOG(4) << "Posting output task for POC: " << pic->pic_order_cnt | |
| 1038 << " input_id: " << dec_surface->input_id(); | |
| 1039 output_pic_cb_.Run(dec_surface->input_id(), dec_surface->va_surface()); | |
| 1040 | |
| 1041 return true; | |
| 1042 } | |
| 1043 | |
| 1044 void VaapiH264Decoder::ClearDPB() { | |
| 1045 // Clear DPB contents, marking the pictures as unused first. | |
| 1046 for (VaapiH264DPB::Pictures::iterator it = dpb_.begin(); it != dpb_.end(); | |
| 1047 ++it) | |
| 1048 UnassignSurfaceFromPoC((*it)->pic_order_cnt); | |
| 1049 | |
| 1050 dpb_.Clear(); | |
| 1051 last_output_poc_ = std::numeric_limits<int>::min(); | |
| 1052 } | |
| 1053 | |
| 1054 bool VaapiH264Decoder::OutputAllRemainingPics() { | |
| 1055 // Output all pictures that are waiting to be outputted. | |
| 1056 FinishPrevFrameIfPresent(); | |
| 1057 VaapiH264Picture::PtrVector to_output; | |
| 1058 dpb_.GetNotOutputtedPicsAppending(to_output); | |
| 1059 // Sort them by ascending POC to output in order. | |
| 1060 std::sort(to_output.begin(), to_output.end(), POCAscCompare()); | |
| 1061 | |
| 1062 VaapiH264Picture::PtrVector::iterator it; | |
| 1063 for (it = to_output.begin(); it != to_output.end(); ++it) { | |
| 1064 if (!OutputPic(*it)) { | |
| 1065 DVLOG(1) << "Failed to output pic POC: " << (*it)->pic_order_cnt; | |
| 1066 return false; | |
| 1067 } | |
| 1068 } | |
| 1069 | |
| 1070 return true; | |
| 1071 } | |
| 1072 | |
| 1073 bool VaapiH264Decoder::Flush() { | |
| 1074 DVLOG(2) << "Decoder flush"; | |
| 1075 | |
| 1076 if (!OutputAllRemainingPics()) | |
| 1077 return false; | |
| 1078 | |
| 1079 ClearDPB(); | |
| 1080 | |
| 1081 DCHECK(decode_surfaces_in_use_.empty()); | |
| 1082 return true; | |
| 1083 } | |
| 1084 | |
| 1085 bool VaapiH264Decoder::StartNewFrame(media::H264SliceHeader* slice_hdr) { | |
| 1086 // TODO posciak: add handling of max_num_ref_frames per spec. | |
| 1087 | |
| 1088 // If the new frame is an IDR, output what's left to output and clear DPB | |
| 1089 if (slice_hdr->idr_pic_flag) { | |
| 1090 // (unless we are explicitly instructed not to do so). | |
| 1091 if (!slice_hdr->no_output_of_prior_pics_flag) { | |
| 1092 // Output DPB contents. | |
| 1093 if (!Flush()) | |
| 1094 return false; | |
| 1095 } | |
| 1096 dpb_.Clear(); | |
| 1097 last_output_poc_ = std::numeric_limits<int>::min(); | |
| 1098 } | |
| 1099 | |
| 1100 // curr_pic_ should have either been added to DPB or discarded when finishing | |
| 1101 // the last frame. DPB is responsible for releasing that memory once it's | |
| 1102 // not needed anymore. | |
| 1103 DCHECK(!curr_pic_.get()); | |
| 1104 curr_pic_.reset(new VaapiH264Picture); | |
| 1105 CHECK(curr_pic_.get()); | |
| 1106 | |
| 1107 if (!InitCurrPicture(slice_hdr)) | |
| 1108 return false; | |
| 1109 | |
| 1110 DCHECK_GT(max_frame_num_, 0); | |
| 1111 | |
| 1112 UpdatePicNums(); | |
| 1113 | |
| 1114 // Send parameter buffers before each new picture, before the first slice. | |
| 1115 if (!SendPPS()) | |
| 1116 return false; | |
| 1117 | |
| 1118 if (!SendIQMatrix()) | |
| 1119 return false; | |
| 1120 | |
| 1121 if (!QueueSlice(slice_hdr)) | |
| 1122 return false; | |
| 1123 | |
| 1124 return true; | |
| 1125 } | |
| 1126 | |
| 1127 bool VaapiH264Decoder::HandleMemoryManagementOps() { | |
| 1128 // 8.2.5.4 | |
| 1129 for (unsigned int i = 0; i < arraysize(curr_pic_->ref_pic_marking); ++i) { | |
| 1130 // Code below does not support interlaced stream (per-field pictures). | |
| 1131 media::H264DecRefPicMarking* ref_pic_marking = | |
| 1132 &curr_pic_->ref_pic_marking[i]; | |
| 1133 VaapiH264Picture* to_mark; | |
| 1134 int pic_num_x; | |
| 1135 | |
| 1136 switch (ref_pic_marking->memory_mgmnt_control_operation) { | |
| 1137 case 0: | |
| 1138 // Normal end of operations' specification. | |
| 1139 return true; | |
| 1140 | |
| 1141 case 1: | |
| 1142 // Mark a short term reference picture as unused so it can be removed | |
| 1143 // if outputted. | |
| 1144 pic_num_x = curr_pic_->pic_num - | |
| 1145 (ref_pic_marking->difference_of_pic_nums_minus1 + 1); | |
| 1146 to_mark = dpb_.GetShortRefPicByPicNum(pic_num_x); | |
| 1147 if (to_mark) { | |
| 1148 to_mark->ref = false; | |
| 1149 } else { | |
| 1150 DVLOG(1) << "Invalid short ref pic num to unmark"; | |
| 1151 return false; | |
| 1152 } | |
| 1153 break; | |
| 1154 | |
| 1155 case 2: | |
| 1156 // Mark a long term reference picture as unused so it can be removed | |
| 1157 // if outputted. | |
| 1158 to_mark = dpb_.GetLongRefPicByLongTermPicNum( | |
| 1159 ref_pic_marking->long_term_pic_num); | |
| 1160 if (to_mark) { | |
| 1161 to_mark->ref = false; | |
| 1162 } else { | |
| 1163 DVLOG(1) << "Invalid long term ref pic num to unmark"; | |
| 1164 return false; | |
| 1165 } | |
| 1166 break; | |
| 1167 | |
| 1168 case 3: | |
| 1169 // Mark a short term reference picture as long term reference. | |
| 1170 pic_num_x = curr_pic_->pic_num - | |
| 1171 (ref_pic_marking->difference_of_pic_nums_minus1 + 1); | |
| 1172 to_mark = dpb_.GetShortRefPicByPicNum(pic_num_x); | |
| 1173 if (to_mark) { | |
| 1174 DCHECK(to_mark->ref && !to_mark->long_term); | |
| 1175 to_mark->long_term = true; | |
| 1176 to_mark->long_term_frame_idx = ref_pic_marking->long_term_frame_idx; | |
| 1177 } else { | |
| 1178 DVLOG(1) << "Invalid short term ref pic num to mark as long ref"; | |
| 1179 return false; | |
| 1180 } | |
| 1181 break; | |
| 1182 | |
| 1183 case 4: { | |
| 1184 // Unmark all reference pictures with long_term_frame_idx over new max. | |
| 1185 max_long_term_frame_idx_ | |
| 1186 = ref_pic_marking->max_long_term_frame_idx_plus1 - 1; | |
| 1187 VaapiH264Picture::PtrVector long_terms; | |
| 1188 dpb_.GetLongTermRefPicsAppending(long_terms); | |
| 1189 for (size_t i = 0; i < long_terms.size(); ++i) { | |
| 1190 VaapiH264Picture* pic = long_terms[i]; | |
| 1191 DCHECK(pic->ref && pic->long_term); | |
| 1192 // Ok to cast, max_long_term_frame_idx is much smaller than 16bit. | |
| 1193 if (pic->long_term_frame_idx > | |
| 1194 static_cast<int>(max_long_term_frame_idx_)) | |
| 1195 pic->ref = false; | |
| 1196 } | |
| 1197 break; | |
| 1198 } | |
| 1199 | |
| 1200 case 5: | |
| 1201 // Unmark all reference pictures. | |
| 1202 dpb_.MarkAllUnusedForRef(); | |
| 1203 max_long_term_frame_idx_ = -1; | |
| 1204 curr_pic_->mem_mgmt_5 = true; | |
| 1205 break; | |
| 1206 | |
| 1207 case 6: { | |
| 1208 // Replace long term reference pictures with current picture. | |
| 1209 // First unmark if any existing with this long_term_frame_idx... | |
| 1210 VaapiH264Picture::PtrVector long_terms; | |
| 1211 dpb_.GetLongTermRefPicsAppending(long_terms); | |
| 1212 for (size_t i = 0; i < long_terms.size(); ++i) { | |
| 1213 VaapiH264Picture* pic = long_terms[i]; | |
| 1214 DCHECK(pic->ref && pic->long_term); | |
| 1215 // Ok to cast, long_term_frame_idx is much smaller than 16bit. | |
| 1216 if (pic->long_term_frame_idx == | |
| 1217 static_cast<int>(ref_pic_marking->long_term_frame_idx)) | |
| 1218 pic->ref = false; | |
| 1219 } | |
| 1220 | |
| 1221 // and mark the current one instead. | |
| 1222 curr_pic_->ref = true; | |
| 1223 curr_pic_->long_term = true; | |
| 1224 curr_pic_->long_term_frame_idx = ref_pic_marking->long_term_frame_idx; | |
| 1225 break; | |
| 1226 } | |
| 1227 | |
| 1228 default: | |
| 1229 // Would indicate a bug in parser. | |
| 1230 NOTREACHED(); | |
| 1231 } | |
| 1232 } | |
| 1233 | |
| 1234 return true; | |
| 1235 } | |
| 1236 | |
| 1237 // This method ensures that DPB does not overflow, either by removing | |
| 1238 // reference pictures as specified in the stream, or using a sliding window | |
| 1239 // procedure to remove the oldest one. | |
| 1240 // It also performs marking and unmarking pictures as reference. | |
| 1241 // See spac 8.2.5.1. | |
| 1242 void VaapiH264Decoder::ReferencePictureMarking() { | |
| 1243 if (curr_pic_->idr) { | |
| 1244 // If current picture is an IDR, all reference pictures are unmarked. | |
| 1245 dpb_.MarkAllUnusedForRef(); | |
| 1246 | |
| 1247 if (curr_pic_->long_term_reference_flag) { | |
| 1248 curr_pic_->long_term = true; | |
| 1249 curr_pic_->long_term_frame_idx = 0; | |
| 1250 max_long_term_frame_idx_ = 0; | |
| 1251 } else { | |
| 1252 curr_pic_->long_term = false; | |
| 1253 max_long_term_frame_idx_ = -1; | |
| 1254 } | |
| 1255 } else { | |
| 1256 if (!curr_pic_->adaptive_ref_pic_marking_mode_flag) { | |
| 1257 // If non-IDR, and the stream does not indicate what we should do to | |
| 1258 // ensure DPB doesn't overflow, discard oldest picture. | |
| 1259 // See spec 8.2.5.3. | |
| 1260 if (curr_pic_->field == VaapiH264Picture::FIELD_NONE) { | |
| 1261 DCHECK_LE(dpb_.CountRefPics(), | |
| 1262 std::max<int>(parser_.GetSPS(curr_sps_id_)->max_num_ref_frames, | |
| 1263 1)); | |
| 1264 if (dpb_.CountRefPics() == | |
| 1265 std::max<int>(parser_.GetSPS(curr_sps_id_)->max_num_ref_frames, | |
| 1266 1)) { | |
| 1267 // Max number of reference pics reached, | |
| 1268 // need to remove one of the short term ones. | |
| 1269 // Find smallest frame_num_wrap short reference picture and mark | |
| 1270 // it as unused. | |
| 1271 VaapiH264Picture* to_unmark = dpb_.GetLowestFrameNumWrapShortRefPic(); | |
| 1272 if (to_unmark == NULL) { | |
| 1273 DVLOG(1) << "Couldn't find a short ref picture to unmark"; | |
| 1274 return; | |
| 1275 } | |
| 1276 to_unmark->ref = false; | |
| 1277 } | |
| 1278 } else { | |
| 1279 // Shouldn't get here. | |
| 1280 DVLOG(1) << "Interlaced video not supported."; | |
| 1281 report_error_to_uma_cb_.Run(INTERLACED_STREAM); | |
| 1282 } | |
| 1283 } else { | |
| 1284 // Stream has instructions how to discard pictures from DPB and how | |
| 1285 // to mark/unmark existing reference pictures. Do it. | |
| 1286 // Spec 8.2.5.4. | |
| 1287 if (curr_pic_->field == VaapiH264Picture::FIELD_NONE) { | |
| 1288 HandleMemoryManagementOps(); | |
| 1289 } else { | |
| 1290 // Shouldn't get here. | |
| 1291 DVLOG(1) << "Interlaced video not supported."; | |
| 1292 report_error_to_uma_cb_.Run(INTERLACED_STREAM); | |
| 1293 } | |
| 1294 } | |
| 1295 } | |
| 1296 } | |
| 1297 | |
| 1298 bool VaapiH264Decoder::FinishPicture() { | |
| 1299 DCHECK(curr_pic_.get()); | |
| 1300 | |
| 1301 // Finish processing previous picture. | |
| 1302 // Start by storing previous reference picture data for later use, | |
| 1303 // if picture being finished is a reference picture. | |
| 1304 if (curr_pic_->ref) { | |
| 1305 ReferencePictureMarking(); | |
| 1306 prev_ref_has_memmgmnt5_ = curr_pic_->mem_mgmt_5; | |
| 1307 prev_ref_top_field_order_cnt_ = curr_pic_->top_field_order_cnt; | |
| 1308 prev_ref_pic_order_cnt_msb_ = curr_pic_->pic_order_cnt_msb; | |
| 1309 prev_ref_pic_order_cnt_lsb_ = curr_pic_->pic_order_cnt_lsb; | |
| 1310 prev_ref_field_ = curr_pic_->field; | |
| 1311 } | |
| 1312 prev_has_memmgmnt5_ = curr_pic_->mem_mgmt_5; | |
| 1313 prev_frame_num_offset_ = curr_pic_->frame_num_offset; | |
| 1314 | |
| 1315 // Remove unused (for reference or later output) pictures from DPB, marking | |
| 1316 // them as such. | |
| 1317 for (VaapiH264DPB::Pictures::iterator it = dpb_.begin(); it != dpb_.end(); | |
| 1318 ++it) { | |
| 1319 if ((*it)->outputted && !(*it)->ref) | |
| 1320 UnassignSurfaceFromPoC((*it)->pic_order_cnt); | |
| 1321 } | |
| 1322 dpb_.DeleteUnused(); | |
| 1323 | |
| 1324 DVLOG(4) << "Finishing picture, entries in DPB: " << dpb_.size(); | |
| 1325 | |
| 1326 // Whatever happens below, curr_pic_ will stop managing the pointer to the | |
| 1327 // picture after this function returns. The ownership will either be | |
| 1328 // transferred to DPB, if the image is still needed (for output and/or | |
| 1329 // reference), or the memory will be released if we manage to output it here | |
| 1330 // without having to store it for future reference. | |
| 1331 scoped_ptr<VaapiH264Picture> pic(curr_pic_.release()); | |
| 1332 | |
| 1333 // Get all pictures that haven't been outputted yet. | |
| 1334 VaapiH264Picture::PtrVector not_outputted; | |
| 1335 // TODO(posciak): pass as pointer, not reference (violates coding style). | |
| 1336 dpb_.GetNotOutputtedPicsAppending(not_outputted); | |
| 1337 // Include the one we've just decoded. | |
| 1338 not_outputted.push_back(pic.get()); | |
| 1339 | |
| 1340 // Sort in output order. | |
| 1341 std::sort(not_outputted.begin(), not_outputted.end(), POCAscCompare()); | |
| 1342 | |
| 1343 // Try to output as many pictures as we can. A picture can be output, | |
| 1344 // if the number of decoded and not yet outputted pictures that would remain | |
| 1345 // in DPB afterwards would at least be equal to max_num_reorder_frames. | |
| 1346 // If the outputted picture is not a reference picture, it doesn't have | |
| 1347 // to remain in the DPB and can be removed. | |
| 1348 VaapiH264Picture::PtrVector::iterator output_candidate = | |
| 1349 not_outputted.begin(); | |
| 1350 size_t num_remaining = not_outputted.size(); | |
| 1351 while (num_remaining > max_num_reorder_frames_) { | |
| 1352 int poc = (*output_candidate)->pic_order_cnt; | |
| 1353 DCHECK_GE(poc, last_output_poc_); | |
| 1354 if (!OutputPic(*output_candidate)) | |
| 1355 return false; | |
| 1356 | |
| 1357 if (!(*output_candidate)->ref) { | |
| 1358 // Current picture hasn't been inserted into DPB yet, so don't remove it | |
| 1359 // if we managed to output it immediately. | |
| 1360 if (*output_candidate != pic) | |
| 1361 dpb_.DeleteByPOC(poc); | |
| 1362 // Mark as unused. | |
| 1363 UnassignSurfaceFromPoC(poc); | |
| 1364 } | |
| 1365 | |
| 1366 ++output_candidate; | |
| 1367 --num_remaining; | |
| 1368 } | |
| 1369 | |
| 1370 // If we haven't managed to output the picture that we just decoded, or if | |
| 1371 // it's a reference picture, we have to store it in DPB. | |
| 1372 if (!pic->outputted || pic->ref) { | |
| 1373 if (dpb_.IsFull()) { | |
| 1374 // If we haven't managed to output anything to free up space in DPB | |
| 1375 // to store this picture, it's an error in the stream. | |
| 1376 DVLOG(1) << "Could not free up space in DPB!"; | |
| 1377 return false; | |
| 1378 } | |
| 1379 | |
| 1380 dpb_.StorePic(pic.release()); | |
| 1381 } | |
| 1382 | |
| 1383 return true; | |
| 1384 } | |
| 1385 | |
| 1386 static int LevelToMaxDpbMbs(int level) { | |
| 1387 // See table A-1 in spec. | |
| 1388 switch (level) { | |
| 1389 case 10: return 396; | |
| 1390 case 11: return 900; | |
| 1391 case 12: // fallthrough | |
| 1392 case 13: // fallthrough | |
| 1393 case 20: return 2376; | |
| 1394 case 21: return 4752; | |
| 1395 case 22: // fallthrough | |
| 1396 case 30: return 8100; | |
| 1397 case 31: return 18000; | |
| 1398 case 32: return 20480; | |
| 1399 case 40: // fallthrough | |
| 1400 case 41: return 32768; | |
| 1401 case 42: return 34816; | |
| 1402 case 50: return 110400; | |
| 1403 case 51: // fallthrough | |
| 1404 case 52: return 184320; | |
| 1405 default: | |
| 1406 DVLOG(1) << "Invalid codec level (" << level << ")"; | |
| 1407 return 0; | |
| 1408 } | |
| 1409 } | |
| 1410 | |
| 1411 bool VaapiH264Decoder::UpdateMaxNumReorderFrames(const media::H264SPS* sps) { | |
| 1412 if (sps->vui_parameters_present_flag && sps->bitstream_restriction_flag) { | |
| 1413 max_num_reorder_frames_ = | |
| 1414 base::checked_cast<size_t>(sps->max_num_reorder_frames); | |
| 1415 if (max_num_reorder_frames_ > dpb_.max_num_pics()) { | |
| 1416 DVLOG(1) | |
| 1417 << "max_num_reorder_frames present, but larger than MaxDpbFrames (" | |
| 1418 << max_num_reorder_frames_ << " > " << dpb_.max_num_pics() << ")"; | |
| 1419 max_num_reorder_frames_ = 0; | |
| 1420 return false; | |
| 1421 } | |
| 1422 return true; | |
| 1423 } | |
| 1424 | |
| 1425 // max_num_reorder_frames not present, infer from profile/constraints | |
| 1426 // (see VUI semantics in spec). | |
| 1427 if (sps->constraint_set3_flag) { | |
| 1428 switch (sps->profile_idc) { | |
| 1429 case 44: | |
| 1430 case 86: | |
| 1431 case 100: | |
| 1432 case 110: | |
| 1433 case 122: | |
| 1434 case 244: | |
| 1435 max_num_reorder_frames_ = 0; | |
| 1436 break; | |
| 1437 default: | |
| 1438 max_num_reorder_frames_ = dpb_.max_num_pics(); | |
| 1439 break; | |
| 1440 } | |
| 1441 } else { | |
| 1442 max_num_reorder_frames_ = dpb_.max_num_pics(); | |
| 1443 } | |
| 1444 | |
| 1445 return true; | |
| 1446 } | |
| 1447 | |
| 1448 bool VaapiH264Decoder::ProcessSPS(int sps_id, bool* need_new_buffers) { | |
| 1449 const media::H264SPS* sps = parser_.GetSPS(sps_id); | |
| 1450 DCHECK(sps); | |
| 1451 DVLOG(4) << "Processing SPS"; | |
| 1452 | |
| 1453 *need_new_buffers = false; | |
| 1454 | |
| 1455 if (sps->frame_mbs_only_flag == 0) { | |
| 1456 DVLOG(1) << "frame_mbs_only_flag != 1 not supported"; | |
| 1457 report_error_to_uma_cb_.Run(FRAME_MBS_ONLY_FLAG_NOT_ONE); | |
| 1458 return false; | |
| 1459 } | |
| 1460 | |
| 1461 if (sps->gaps_in_frame_num_value_allowed_flag) { | |
| 1462 DVLOG(1) << "Gaps in frame numbers not supported"; | |
| 1463 report_error_to_uma_cb_.Run(GAPS_IN_FRAME_NUM); | |
| 1464 return false; | |
| 1465 } | |
| 1466 | |
| 1467 curr_sps_id_ = sps->seq_parameter_set_id; | |
| 1468 | |
| 1469 // Calculate picture height/width in macroblocks and pixels | |
| 1470 // (spec 7.4.2.1.1, 7.4.3). | |
| 1471 int width_mb = sps->pic_width_in_mbs_minus1 + 1; | |
| 1472 int height_mb = (2 - sps->frame_mbs_only_flag) * | |
| 1473 (sps->pic_height_in_map_units_minus1 + 1); | |
| 1474 | |
| 1475 gfx::Size new_pic_size(16 * width_mb, 16 * height_mb); | |
| 1476 if (new_pic_size.IsEmpty()) { | |
| 1477 DVLOG(1) << "Invalid picture size: " << new_pic_size.ToString(); | |
| 1478 return false; | |
| 1479 } | |
| 1480 | |
| 1481 if (!pic_size_.IsEmpty() && new_pic_size == pic_size_) { | |
| 1482 // Already have surfaces and this SPS keeps the same resolution, | |
| 1483 // no need to request a new set. | |
| 1484 return true; | |
| 1485 } | |
| 1486 | |
| 1487 pic_size_ = new_pic_size; | |
| 1488 DVLOG(1) << "New picture size: " << pic_size_.ToString(); | |
| 1489 | |
| 1490 max_pic_order_cnt_lsb_ = 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4); | |
| 1491 max_frame_num_ = 1 << (sps->log2_max_frame_num_minus4 + 4); | |
| 1492 | |
| 1493 int level = sps->level_idc; | |
| 1494 int max_dpb_mbs = LevelToMaxDpbMbs(level); | |
| 1495 if (max_dpb_mbs == 0) | |
| 1496 return false; | |
| 1497 | |
| 1498 size_t max_dpb_size = std::min(max_dpb_mbs / (width_mb * height_mb), | |
| 1499 static_cast<int>(VaapiH264DPB::kDPBMaxSize)); | |
| 1500 DVLOG(1) << "Codec level: " << level << ", DPB size: " << max_dpb_size; | |
| 1501 if (max_dpb_size == 0) { | |
| 1502 DVLOG(1) << "Invalid DPB Size"; | |
| 1503 return false; | |
| 1504 } | |
| 1505 | |
| 1506 dpb_.set_max_num_pics(max_dpb_size); | |
| 1507 | |
| 1508 if (!UpdateMaxNumReorderFrames(sps)) | |
| 1509 return false; | |
| 1510 DVLOG(1) << "max_num_reorder_frames: " << max_num_reorder_frames_; | |
| 1511 | |
| 1512 *need_new_buffers = true; | |
| 1513 return true; | |
| 1514 } | |
| 1515 | |
| 1516 bool VaapiH264Decoder::ProcessPPS(int pps_id) { | |
| 1517 const media::H264PPS* pps = parser_.GetPPS(pps_id); | |
| 1518 DCHECK(pps); | |
| 1519 | |
| 1520 curr_pps_id_ = pps->pic_parameter_set_id; | |
| 1521 | |
| 1522 return true; | |
| 1523 } | |
| 1524 | |
| 1525 bool VaapiH264Decoder::FinishPrevFrameIfPresent() { | |
| 1526 // If we already have a frame waiting to be decoded, decode it and finish. | |
| 1527 if (curr_pic_ != NULL) { | |
| 1528 if (!DecodePicture()) | |
| 1529 return false; | |
| 1530 return FinishPicture(); | |
| 1531 } | |
| 1532 | |
| 1533 return true; | |
| 1534 } | |
| 1535 | |
| 1536 bool VaapiH264Decoder::ProcessSlice(media::H264SliceHeader* slice_hdr) { | |
| 1537 prev_frame_num_ = frame_num_; | |
| 1538 frame_num_ = slice_hdr->frame_num; | |
| 1539 | |
| 1540 if (prev_frame_num_ > 0 && prev_frame_num_ < frame_num_ - 1) { | |
| 1541 DVLOG(1) << "Gap in frame_num!"; | |
| 1542 report_error_to_uma_cb_.Run(GAPS_IN_FRAME_NUM); | |
| 1543 return false; | |
| 1544 } | |
| 1545 | |
| 1546 if (slice_hdr->field_pic_flag == 0) | |
| 1547 max_pic_num_ = max_frame_num_; | |
| 1548 else | |
| 1549 max_pic_num_ = 2 * max_frame_num_; | |
| 1550 | |
| 1551 // TODO posciak: switch to new picture detection per 7.4.1.2.4. | |
| 1552 if (curr_pic_ != NULL && slice_hdr->first_mb_in_slice != 0) { | |
| 1553 // This is just some more slice data of the current picture, so | |
| 1554 // just queue it and return. | |
| 1555 QueueSlice(slice_hdr); | |
| 1556 return true; | |
| 1557 } else { | |
| 1558 // A new frame, so first finish the previous one before processing it... | |
| 1559 if (!FinishPrevFrameIfPresent()) | |
| 1560 return false; | |
| 1561 | |
| 1562 // and then start a new one. | |
| 1563 return StartNewFrame(slice_hdr); | |
| 1564 } | |
| 1565 } | |
| 1566 | |
| 1567 #define SET_ERROR_AND_RETURN() \ | |
| 1568 do { \ | |
| 1569 DVLOG(1) << "Error during decode"; \ | |
| 1570 state_ = kError; \ | |
| 1571 return VaapiH264Decoder::kDecodeError; \ | |
| 1572 } while (0) | |
| 1573 | |
| 1574 void VaapiH264Decoder::SetStream(const uint8* ptr, | |
| 1575 size_t size, | |
| 1576 int32 input_id) { | |
| 1577 DCHECK(ptr); | |
| 1578 DCHECK(size); | |
| 1579 | |
| 1580 // Got new input stream data from the client. | |
| 1581 DVLOG(4) << "New input stream id: " << input_id << " at: " << (void*) ptr | |
| 1582 << " size: " << size; | |
| 1583 parser_.SetStream(ptr, size); | |
| 1584 curr_input_id_ = input_id; | |
| 1585 } | |
| 1586 | |
| 1587 VaapiH264Decoder::DecResult VaapiH264Decoder::Decode() { | |
| 1588 media::H264Parser::Result par_res; | |
| 1589 media::H264NALU nalu; | |
| 1590 DCHECK_NE(state_, kError); | |
| 1591 | |
| 1592 while (1) { | |
| 1593 // If we've already decoded some of the stream (after reset, i.e. we are | |
| 1594 // not in kNeedStreamMetadata state), we may be able to go back into | |
| 1595 // decoding state not only starting at/resuming from an SPS, but also from | |
| 1596 // other resume points, such as IDRs. In the latter case we need an output | |
| 1597 // surface, because we will end up decoding that IDR in the process. | |
| 1598 // Otherwise we just look for an SPS and don't produce any output frames. | |
| 1599 if (state_ != kNeedStreamMetadata && available_va_surfaces_.empty()) { | |
| 1600 DVLOG(4) << "No output surfaces available"; | |
| 1601 return kRanOutOfSurfaces; | |
| 1602 } | |
| 1603 | |
| 1604 par_res = parser_.AdvanceToNextNALU(&nalu); | |
| 1605 if (par_res == media::H264Parser::kEOStream) | |
| 1606 return kRanOutOfStreamData; | |
| 1607 else if (par_res != media::H264Parser::kOk) | |
| 1608 SET_ERROR_AND_RETURN(); | |
| 1609 | |
| 1610 DVLOG(4) << "NALU found: " << static_cast<int>(nalu.nal_unit_type); | |
| 1611 | |
| 1612 switch (nalu.nal_unit_type) { | |
| 1613 case media::H264NALU::kNonIDRSlice: | |
| 1614 // We can't resume from a non-IDR slice. | |
| 1615 if (state_ != kDecoding) | |
| 1616 break; | |
| 1617 // else fallthrough | |
| 1618 case media::H264NALU::kIDRSlice: { | |
| 1619 // TODO(posciak): the IDR may require an SPS that we don't have | |
| 1620 // available. For now we'd fail if that happens, but ideally we'd like | |
| 1621 // to keep going until the next SPS in the stream. | |
| 1622 if (state_ == kNeedStreamMetadata) { | |
| 1623 // We need an SPS, skip this IDR and keep looking. | |
| 1624 break; | |
| 1625 } | |
| 1626 | |
| 1627 // If after reset, we should be able to recover from an IDR. | |
| 1628 media::H264SliceHeader slice_hdr; | |
| 1629 | |
| 1630 par_res = parser_.ParseSliceHeader(nalu, &slice_hdr); | |
| 1631 if (par_res != media::H264Parser::kOk) | |
| 1632 SET_ERROR_AND_RETURN(); | |
| 1633 | |
| 1634 if (!ProcessSlice(&slice_hdr)) | |
| 1635 SET_ERROR_AND_RETURN(); | |
| 1636 | |
| 1637 state_ = kDecoding; | |
| 1638 break; | |
| 1639 } | |
| 1640 | |
| 1641 case media::H264NALU::kSPS: { | |
| 1642 int sps_id; | |
| 1643 | |
| 1644 if (!FinishPrevFrameIfPresent()) | |
| 1645 SET_ERROR_AND_RETURN(); | |
| 1646 | |
| 1647 par_res = parser_.ParseSPS(&sps_id); | |
| 1648 if (par_res != media::H264Parser::kOk) | |
| 1649 SET_ERROR_AND_RETURN(); | |
| 1650 | |
| 1651 bool need_new_buffers = false; | |
| 1652 if (!ProcessSPS(sps_id, &need_new_buffers)) | |
| 1653 SET_ERROR_AND_RETURN(); | |
| 1654 | |
| 1655 state_ = kDecoding; | |
| 1656 | |
| 1657 if (need_new_buffers) { | |
| 1658 if (!Flush()) | |
| 1659 return kDecodeError; | |
| 1660 | |
| 1661 available_va_surfaces_.clear(); | |
| 1662 return kAllocateNewSurfaces; | |
| 1663 } | |
| 1664 break; | |
| 1665 } | |
| 1666 | |
| 1667 case media::H264NALU::kPPS: { | |
| 1668 if (state_ != kDecoding) | |
| 1669 break; | |
| 1670 | |
| 1671 int pps_id; | |
| 1672 | |
| 1673 if (!FinishPrevFrameIfPresent()) | |
| 1674 SET_ERROR_AND_RETURN(); | |
| 1675 | |
| 1676 par_res = parser_.ParsePPS(&pps_id); | |
| 1677 if (par_res != media::H264Parser::kOk) | |
| 1678 SET_ERROR_AND_RETURN(); | |
| 1679 | |
| 1680 if (!ProcessPPS(pps_id)) | |
| 1681 SET_ERROR_AND_RETURN(); | |
| 1682 break; | |
| 1683 } | |
| 1684 | |
| 1685 case media::H264NALU::kAUD: | |
| 1686 case media::H264NALU::kEOSeq: | |
| 1687 case media::H264NALU::kEOStream: | |
| 1688 if (state_ != kDecoding) | |
| 1689 break; | |
| 1690 if (!FinishPrevFrameIfPresent()) | |
| 1691 SET_ERROR_AND_RETURN(); | |
| 1692 | |
| 1693 break; | |
| 1694 | |
| 1695 default: | |
| 1696 DVLOG(4) << "Skipping NALU type: " << nalu.nal_unit_type; | |
| 1697 break; | |
| 1698 } | |
| 1699 } | |
| 1700 } | |
| 1701 | |
| 1702 size_t VaapiH264Decoder::GetRequiredNumOfPictures() { | |
| 1703 return dpb_.max_num_pics() + kPicsInPipeline; | |
| 1704 } | |
| 1705 | |
| 1706 } // namespace content | |
| OLD | NEW |