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.
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"
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
{
21 DecodeSurface(int poc
,
23 const scoped_refptr
<VASurface
>& va_surface
);
24 DecodeSurface(int poc
, const scoped_refptr
<DecodeSurface
>& dec_surface
);
31 scoped_refptr
<VASurface
> va_surface() {
42 scoped_refptr
<VASurface
> va_surface_
;
45 VaapiH264Decoder::DecodeSurface::DecodeSurface(
48 const scoped_refptr
<VASurface
>& va_surface
)
51 va_surface_(va_surface
) {
52 DCHECK(va_surface_
.get());
55 VaapiH264Decoder::DecodeSurface::~DecodeSurface() {
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),
65 max_long_term_frame_idx_(0),
66 max_num_reorder_frames_(0),
69 vaapi_wrapper_(vaapi_wrapper
),
70 output_pic_cb_(output_pic_cb
),
71 report_error_to_uma_cb_(report_error_to_uma_cb
) {
73 state_
= kNeedStreamMetadata
;
76 VaapiH264Decoder::~VaapiH264Decoder() {
79 void VaapiH264Decoder::Reset() {
85 prev_frame_num_offset_
= -1;
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
;
93 vaapi_wrapper_
->DestroyPendingBuffers();
95 ref_pic_list0_
.clear();
96 ref_pic_list1_
.clear();
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
104 UnassignSurfaceFromPoC(poc
);
106 DCHECK(decode_surfaces_in_use_
.empty());
110 last_output_poc_
= std::numeric_limits
<int>::min();
112 // If we are in kDecoding, we can resume without processing an SPS.
113 if (state_
== kDecoding
)
114 state_
= kAfterReset
;
117 void VaapiH264Decoder::ReuseSurface(
118 const scoped_refptr
<VASurface
>& va_surface
) {
119 available_va_surfaces_
.push_back(va_surface
);
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
;
129 void VaapiH264Decoder::FillVAPicture(VAPictureH264
* va_pic
,
130 VaapiH264Picture
* pic
) {
133 DecodeSurface
* dec_surface
= DecodeSurfaceByPoC(pic
->pic_order_cnt
);
135 // Cannot provide a ref picture, will corrupt output, but may be able
137 InitVAPicture(va_pic
);
141 va_pic
->picture_id
= dec_surface
->va_surface()->id();
142 va_pic
->frame_idx
= pic
->frame_num
;
145 switch (pic
->field
) {
146 case VaapiH264Picture::FIELD_NONE
:
148 case VaapiH264Picture::FIELD_TOP
:
149 va_pic
->flags
|= VA_PICTURE_H264_TOP_FIELD
;
151 case VaapiH264Picture::FIELD_BOTTOM
:
152 va_pic
->flags
|= VA_PICTURE_H264_BOTTOM_FIELD
;
157 va_pic
->flags
|= pic
->long_term
? VA_PICTURE_H264_LONG_TERM_REFERENCE
158 : VA_PICTURE_H264_SHORT_TERM_REFERENCE
;
161 va_pic
->TopFieldOrderCnt
= pic
->top_field_order_cnt
;
162 va_pic
->BottomFieldOrderCnt
= pic
->bottom_field_order_cnt
;
165 int VaapiH264Decoder::FillVARefFramesFromDPB(VAPictureH264
*va_pics
,
167 VaapiH264DPB::Pictures::reverse_iterator rit
;
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
) {
175 FillVAPicture(&va_pics
[i
++], *rit
);
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
;
188 return iter
->second
.get();
191 bool VaapiH264Decoder::AssignSurfaceToPoC(int32 input_id
, int poc
) {
192 if (available_va_surfaces_
.empty()) {
193 DVLOG(1) << "No VA Surfaces available";
197 linked_ptr
<DecodeSurface
> dec_surface(new DecodeSurface(
198 poc
, input_id
, available_va_surfaces_
.back()));
199 available_va_surfaces_
.pop_back();
201 DVLOG(4) << "POC " << poc
202 << " will use surface " << dec_surface
->va_surface()->id();
204 bool inserted
= decode_surfaces_in_use_
.insert(
205 std::make_pair(poc
, dec_surface
)).second
;
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
;
218 DVLOG(4) << "POC " << poc
<< " no longer using VA surface "
219 << it
->second
->va_surface()->id();
221 decode_surfaces_in_use_
.erase(it
);
224 bool VaapiH264Decoder::SendPPS() {
225 const media::H264PPS
* pps
= parser_
.GetPPS(curr_pps_id_
);
228 const media::H264SPS
* sps
= parser_
.GetSPS(pps
->seq_parameter_set_id
);
231 DCHECK(curr_pic_
.get());
233 VAPictureParameterBufferH264 pic_param
;
234 memset(&pic_param
, 0, sizeof(VAPictureParameterBufferH264
));
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
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
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
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
);
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
291 pic_param
.frame_num
= curr_pic_
->frame_num
;
293 InitVAPicture(&pic_param
.CurrPic
);
294 FillVAPicture(&pic_param
.CurrPic
, curr_pic_
.get());
296 // Init reference pictures' array.
297 for (int i
= 0; i
< 16; ++i
)
298 InitVAPicture(&pic_param
.ReferenceFrames
[i
]);
300 // And fill it with picture info from DPB.
301 FillVARefFramesFromDPB(pic_param
.ReferenceFrames
,
302 arraysize(pic_param
.ReferenceFrames
));
304 pic_param
.num_ref_frames
= sps
->max_num_ref_frames
;
306 return vaapi_wrapper_
->SubmitBuffer(VAPictureParameterBufferType
,
307 sizeof(VAPictureParameterBufferH264
),
311 bool VaapiH264Decoder::SendIQMatrix() {
312 const media::H264PPS
* pps
= parser_
.GetPPS(curr_pps_id_
);
315 VAIQMatrixBufferH264 iq_matrix_buf
;
316 memset(&iq_matrix_buf
, 0, sizeof(VAIQMatrixBufferH264
));
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
];
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
];
329 const media::H264SPS
* sps
= parser_
.GetSPS(pps
->seq_parameter_set_id
);
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
];
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
];
342 return vaapi_wrapper_
->SubmitBuffer(VAIQMatrixBufferType
,
343 sizeof(VAIQMatrixBufferH264
),
347 bool VaapiH264Decoder::SendVASliceParam(media::H264SliceHeader
* slice_hdr
) {
348 const media::H264PPS
* pps
= parser_
.GetPPS(slice_hdr
->pic_parameter_set_id
);
351 const media::H264SPS
* sps
= parser_
.GetSPS(pps
->seq_parameter_set_id
);
354 VASliceParameterBufferH264 slice_param
;
355 memset(&slice_param
, 0, sizeof(VASliceParameterBufferH264
));
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
;
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
);
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
);
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
);
383 SHDRToSP(luma_weight_l0_flag
);
384 SHDRToSP(luma_weight_l1_flag
);
386 SHDRToSP(chroma_weight_l0_flag
);
387 SHDRToSP(chroma_weight_l1_flag
);
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
];
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
];
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
];
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
];
420 for (int i
= 0; i
< 32; ++i
) {
421 InitVAPicture(&slice_param
.RefPicList0
[i
]);
422 InitVAPicture(&slice_param
.RefPicList1
[i
]);
426 VaapiH264Picture::PtrVector::iterator it
;
427 for (it
= ref_pic_list0_
.begin(), i
= 0; it
!= ref_pic_list0_
.end() && *it
;
429 FillVAPicture(&slice_param
.RefPicList0
[i
], *it
);
430 for (it
= ref_pic_list1_
.begin(), i
= 0; it
!= ref_pic_list1_
.end() && *it
;
432 FillVAPicture(&slice_param
.RefPicList1
[i
], *it
);
434 return vaapi_wrapper_
->SubmitBuffer(VASliceParameterBufferType
,
435 sizeof(VASliceParameterBufferH264
),
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
,
446 bool VaapiH264Decoder::PrepareRefPicLists(media::H264SliceHeader
* slice_hdr
) {
447 ref_pic_list0_
.clear();
448 ref_pic_list1_
.clear();
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);
456 if (slice_hdr
->IsBSlice()) {
457 ConstructReferencePicListsB(slice_hdr
);
458 return ModifyReferencePicList(slice_hdr
, 0) &&
459 ModifyReferencePicList(slice_hdr
, 1);
465 bool VaapiH264Decoder::QueueSlice(media::H264SliceHeader
* slice_hdr
) {
466 DCHECK(curr_pic_
.get());
468 if (!PrepareRefPicLists(slice_hdr
))
471 if (!SendVASliceParam(slice_hdr
))
474 if (!SendSliceData(slice_hdr
->nalu_data
, slice_hdr
->nalu_size
))
480 // TODO(posciak) start using vaMapBuffer instead of vaCreateBuffer wherever
482 bool VaapiH264Decoder::DecodePicture() {
483 DCHECK(curr_pic_
.get());
485 DVLOG(4) << "Decoding POC " << curr_pic_
->pic_order_cnt
;
486 DecodeSurface
* dec_surface
= DecodeSurfaceByPoC(curr_pic_
->pic_order_cnt
);
488 DVLOG(1) << "Asked to decode an invalid POC " << curr_pic_
->pic_order_cnt
;
492 if (!vaapi_wrapper_
->ExecuteAndDestroyPendingBuffers(
493 dec_surface
->va_surface()->id())) {
494 DVLOG(1) << "Failed decoding picture";
501 bool VaapiH264Decoder::InitCurrPicture(media::H264SliceHeader
* slice_hdr
) {
502 DCHECK(curr_pic_
.get());
504 memset(curr_pic_
.get(), 0, sizeof(VaapiH264Picture
));
506 curr_pic_
->idr
= slice_hdr
->idr_pic_flag
;
508 if (slice_hdr
->field_pic_flag
) {
509 curr_pic_
->field
= slice_hdr
->bottom_field_flag
510 ? VaapiH264Picture::FIELD_BOTTOM
511 : VaapiH264Picture::FIELD_TOP
;
513 curr_pic_
->field
= VaapiH264Picture::FIELD_NONE
;
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
;
520 if (!CalculatePicOrderCounts(slice_hdr
))
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";
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
;
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
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
));
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_
);
552 int pic_order_cnt_lsb
= slice_hdr
->pic_order_cnt_lsb
;
553 curr_pic_
->pic_order_cnt_lsb
= pic_order_cnt_lsb
;
555 switch (sps
->pic_order_cnt_type
) {
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;
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_
;
567 prev_pic_order_cnt_msb
= 0;
568 prev_pic_order_cnt_lsb
= 0;
571 prev_pic_order_cnt_msb
= prev_ref_pic_order_cnt_msb_
;
572 prev_pic_order_cnt_lsb
= prev_ref_pic_order_cnt_lsb_
;
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_
;
588 curr_pic_
->pic_order_cnt_msb
= prev_pic_order_cnt_msb
;
591 if (curr_pic_
->field
!= VaapiH264Picture::FIELD_BOTTOM
) {
592 curr_pic_
->top_field_order_cnt
= curr_pic_
->pic_order_cnt_msb
+
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
;
602 curr_pic_
->bottom_field_order_cnt
= curr_pic_
->pic_order_cnt_msb
+
610 if (prev_has_memmgmnt5_
)
611 prev_frame_num_offset_
= 0;
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_
;
618 curr_pic_
->frame_num_offset
= prev_frame_num_offset_
;
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
;
626 if (slice_hdr
->nal_ref_idc
== 0 && abs_frame_num
> 0)
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 "
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
;
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
];
649 if (!slice_hdr
->nal_ref_idc
)
650 expected_pic_order_cnt
+= sps
->offset_for_non_ref_pic
;
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
;
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
;
671 if (prev_has_memmgmnt5_
)
672 prev_frame_num_offset_
= 0;
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_
;
679 curr_pic_
->frame_num_offset
= prev_frame_num_offset_
;
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
) {
686 2 * (curr_pic_
->frame_num_offset
+ slice_hdr
->frame_num
) - 1;
688 temp_pic_order_cnt
= 2 * (curr_pic_
->frame_num_offset
+
689 slice_hdr
->frame_num
);
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
;
698 curr_pic_
->top_field_order_cnt
= temp_pic_order_cnt
;
703 DVLOG(1) << "Invalid pic_order_cnt_type: " << sps
->pic_order_cnt_type
;
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
);
712 case VaapiH264Picture::FIELD_TOP
:
713 curr_pic_
->pic_order_cnt
= curr_pic_
->top_field_order_cnt
;
715 case VaapiH264Picture::FIELD_BOTTOM
:
716 curr_pic_
->pic_order_cnt
= curr_pic_
->bottom_field_order_cnt
;
723 void VaapiH264Decoder::UpdatePicNums() {
724 for (VaapiH264DPB::Pictures::iterator it
= dpb_
.begin(); it
!= dpb_
.end();
726 VaapiH264Picture
* pic
= *it
;
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
;
736 if (pic
->frame_num
> frame_num_
)
737 pic
->frame_num_wrap
= pic
->frame_num
- max_frame_num_
;
739 pic
->frame_num_wrap
= pic
->frame_num
;
741 pic
->pic_num
= pic
->frame_num_wrap
;
746 struct PicNumDescCompare
{
747 bool operator()(const VaapiH264Picture
* a
, const VaapiH264Picture
* b
) const {
748 return a
->pic_num
> b
->pic_num
;
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
;
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();
768 // and sort them to get [1].
769 std::sort(ref_pic_list0_
.begin(), ref_pic_list0_
.end(), PicNumDescCompare());
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());
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);
780 struct POCAscCompare
{
781 bool operator()(const VaapiH264Picture
* a
, const VaapiH264Picture
* b
) const {
782 return a
->pic_order_cnt
< b
->pic_order_cnt
;
786 struct POCDescCompare
{
787 bool operator()(const VaapiH264Picture
* a
, const VaapiH264Picture
* b
) const {
788 return a
->pic_order_cnt
> b
->pic_order_cnt
;
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();
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());
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());
810 // and sort [1] descending, thus finishing sequence [1] [2].
811 std::sort(ref_pic_list0_
.begin(), iter
, POCDescCompare());
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());
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.
823 dpb_
.GetShortTermRefPicsAppending(ref_pic_list1_
);
824 num_short_refs
= ref_pic_list1_
.size();
826 // First sort by descending POC.
827 std::sort(ref_pic_list1_
.begin(), ref_pic_list1_
.end(), POCDescCompare());
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());
833 // and sort [1] ascending.
834 std::sort(ref_pic_list1_
.begin(), iter
, POCAscCompare());
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());
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]);
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);
855 int VaapiH264Decoder::PicNumF(VaapiH264Picture
* pic
) {
866 int VaapiH264Decoder::LongTermPicNumF(VaapiH264Picture
* pic
) {
867 if (pic
->ref
&& pic
->long_term
)
868 return pic
->long_term_pic_num
;
870 return 2 * (max_long_term_frame_idx_
+ 1);
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
,
878 VaapiH264Picture
* pic
) {
879 // Security checks, do not disable in Debug mode.
881 CHECK(to
<= std::numeric_limits
<int>::max() - 2);
882 // Additional checks. Debug mode ok.
885 DCHECK((to
+ 1 == static_cast<int>(v
->size())) ||
886 (to
+ 2 == static_cast<int>(v
->size())));
890 for (int i
= to
+ 1; i
> from
; --i
)
891 (*v
)[i
] = (*v
)[i
- 1];
896 bool VaapiH264Decoder::ModifyReferencePicList(media::H264SliceHeader
* slice_hdr
,
898 int num_ref_idx_lX_active_minus1
;
899 VaapiH264Picture::PtrVector
* ref_pic_listx
;
900 media::H264ModificationOfPicNum
* list_mod
;
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.
905 if (!slice_hdr
->ref_pic_list_modification_flag_l0
)
908 list_mod
= slice_hdr
->ref_list_l0_modifications
;
909 num_ref_idx_lX_active_minus1
= ref_pic_list0_
.size() - 1;
911 ref_pic_listx
= &ref_pic_list0_
;
913 if (!slice_hdr
->ref_pic_list_modification_flag_l1
)
916 list_mod
= slice_hdr
->ref_list_l1_modifications
;
917 num_ref_idx_lX_active_minus1
= ref_pic_list1_
.size() - 1;
919 ref_pic_listx
= &ref_pic_list1_
;
922 DCHECK_GE(num_ref_idx_lX_active_minus1
, 0);
925 // Reorder pictures on the list in a way specified in the stream.
926 int pic_num_lx_pred
= curr_pic_
->pic_num
;
928 int pic_num_lx_no_wrap
;
931 VaapiH264Picture
* pic
;
932 for (int i
= 0; i
< media::H264SliceHeader::kRefListModSize
&& !done
; ++i
) {
933 switch (list_mod
->modification_of_pic_nums_idc
) {
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
943 if (pic_num_lx_no_wrap
< 0)
944 pic_num_lx_no_wrap
+= max_pic_num_
;
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
951 if (pic_num_lx_no_wrap
>= max_pic_num_
)
952 pic_num_lx_no_wrap
-= max_pic_num_
;
955 // For use in next iteration.
956 pic_num_lx_pred
= pic_num_lx_no_wrap
;
958 if (pic_num_lx_no_wrap
> curr_pic_
->pic_num
)
959 pic_num_lx
= pic_num_lx_no_wrap
- max_pic_num_
;
961 pic_num_lx
= pic_num_lx_no_wrap
;
963 DCHECK_LT(num_ref_idx_lX_active_minus1
+ 1,
964 media::H264SliceHeader::kRefListModSize
);
965 pic
= dpb_
.GetShortRefPicByPicNum(pic_num_lx
);
967 DVLOG(1) << "Malformed stream, no pic num " << pic_num_lx
;
970 ShiftRightAndInsert(ref_pic_listx
, ref_idx_lx
,
971 num_ref_idx_lX_active_minus1
, pic
);
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
];
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
);
987 DVLOG(1) << "Malformed stream, no pic num "
988 << list_mod
->long_term_pic_num
;
991 ShiftRightAndInsert(ref_pic_listx
, ref_idx_lx
,
992 num_ref_idx_lX_active_minus1
, pic
);
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
];
1004 // End of modification list.
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
;
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);
1027 bool VaapiH264Decoder::OutputPic(VaapiH264Picture
* pic
) {
1028 DCHECK(!pic
->outputted
);
1029 pic
->outputted
= true;
1030 last_output_poc_
= pic
->pic_order_cnt
;
1032 DecodeSurface
* dec_surface
= DecodeSurfaceByPoC(pic
->pic_order_cnt
);
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());
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();
1048 UnassignSurfaceFromPoC((*it
)->pic_order_cnt
);
1051 last_output_poc_
= std::numeric_limits
<int>::min();
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());
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
;
1073 bool VaapiH264Decoder::Flush() {
1074 DVLOG(2) << "Decoder flush";
1076 if (!OutputAllRemainingPics())
1081 DCHECK(decode_surfaces_in_use_
.empty());
1085 bool VaapiH264Decoder::StartNewFrame(media::H264SliceHeader
* slice_hdr
) {
1086 // TODO posciak: add handling of max_num_ref_frames per spec.
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.
1097 last_output_poc_
= std::numeric_limits
<int>::min();
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());
1107 if (!InitCurrPicture(slice_hdr
))
1110 DCHECK_GT(max_frame_num_
, 0);
1114 // Send parameter buffers before each new picture, before the first slice.
1118 if (!SendIQMatrix())
1121 if (!QueueSlice(slice_hdr
))
1127 bool VaapiH264Decoder::HandleMemoryManagementOps() {
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
;
1136 switch (ref_pic_marking
->memory_mgmnt_control_operation
) {
1138 // Normal end of operations' specification.
1142 // Mark a short term reference picture as unused so it can be removed
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
);
1148 to_mark
->ref
= false;
1150 DVLOG(1) << "Invalid short ref pic num to unmark";
1156 // Mark a long term reference picture as unused so it can be removed
1158 to_mark
= dpb_
.GetLongRefPicByLongTermPicNum(
1159 ref_pic_marking
->long_term_pic_num
);
1161 to_mark
->ref
= false;
1163 DVLOG(1) << "Invalid long term ref pic num to unmark";
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
);
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
;
1178 DVLOG(1) << "Invalid short term ref pic num to mark as long ref";
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_
))
1201 // Unmark all reference pictures.
1202 dpb_
.MarkAllUnusedForRef();
1203 max_long_term_frame_idx_
= -1;
1204 curr_pic_
->mem_mgmt_5
= true;
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
))
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
;
1229 // Would indicate a bug in parser.
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();
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;
1252 curr_pic_
->long_term
= false;
1253 max_long_term_frame_idx_
= -1;
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
,
1264 if (dpb_
.CountRefPics() ==
1265 std::max
<int>(parser_
.GetSPS(curr_sps_id_
)->max_num_ref_frames
,
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
1271 VaapiH264Picture
* to_unmark
= dpb_
.GetLowestFrameNumWrapShortRefPic();
1272 if (to_unmark
== NULL
) {
1273 DVLOG(1) << "Couldn't find a short ref picture to unmark";
1276 to_unmark
->ref
= false;
1279 // Shouldn't get here.
1280 DVLOG(1) << "Interlaced video not supported.";
1281 report_error_to_uma_cb_
.Run(INTERLACED_STREAM
);
1284 // Stream has instructions how to discard pictures from DPB and how
1285 // to mark/unmark existing reference pictures. Do it.
1287 if (curr_pic_
->field
== VaapiH264Picture::FIELD_NONE
) {
1288 HandleMemoryManagementOps();
1290 // Shouldn't get here.
1291 DVLOG(1) << "Interlaced video not supported.";
1292 report_error_to_uma_cb_
.Run(INTERLACED_STREAM
);
1298 bool VaapiH264Decoder::FinishPicture() {
1299 DCHECK(curr_pic_
.get());
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
;
1312 prev_has_memmgmnt5_
= curr_pic_
->mem_mgmt_5
;
1313 prev_frame_num_offset_
= curr_pic_
->frame_num_offset
;
1315 // Remove unused (for reference or later output) pictures from DPB, marking
1317 for (VaapiH264DPB::Pictures::iterator it
= dpb_
.begin(); it
!= dpb_
.end();
1319 if ((*it
)->outputted
&& !(*it
)->ref
)
1320 UnassignSurfaceFromPoC((*it
)->pic_order_cnt
);
1322 dpb_
.DeleteUnused();
1324 DVLOG(4) << "Finishing picture, entries in DPB: " << dpb_
.size();
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());
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());
1340 // Sort in output order.
1341 std::sort(not_outputted
.begin(), not_outputted
.end(), POCAscCompare());
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
))
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
);
1363 UnassignSurfaceFromPoC(poc
);
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!";
1380 dpb_
.StorePic(pic
.release());
1386 static int LevelToMaxDpbMbs(int level
) {
1387 // See table A-1 in spec.
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;
1406 DVLOG(1) << "Invalid codec level (" << level
<< ")";
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()) {
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;
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
) {
1435 max_num_reorder_frames_
= 0;
1438 max_num_reorder_frames_
= dpb_
.max_num_pics();
1442 max_num_reorder_frames_
= dpb_
.max_num_pics();
1448 bool VaapiH264Decoder::ProcessSPS(int sps_id
, bool* need_new_buffers
) {
1449 const media::H264SPS
* sps
= parser_
.GetSPS(sps_id
);
1451 DVLOG(4) << "Processing SPS";
1453 *need_new_buffers
= false;
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
);
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
);
1467 curr_sps_id_
= sps
->seq_parameter_set_id
;
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);
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();
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.
1487 pic_size_
= new_pic_size
;
1488 DVLOG(1) << "New picture size: " << pic_size_
.ToString();
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);
1493 int level
= sps
->level_idc
;
1494 int max_dpb_mbs
= LevelToMaxDpbMbs(level
);
1495 if (max_dpb_mbs
== 0)
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";
1506 dpb_
.set_max_num_pics(max_dpb_size
);
1508 if (!UpdateMaxNumReorderFrames(sps
))
1510 DVLOG(1) << "max_num_reorder_frames: " << max_num_reorder_frames_
;
1512 *need_new_buffers
= true;
1516 bool VaapiH264Decoder::ProcessPPS(int pps_id
) {
1517 const media::H264PPS
* pps
= parser_
.GetPPS(pps_id
);
1520 curr_pps_id_
= pps
->pic_parameter_set_id
;
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())
1530 return FinishPicture();
1536 bool VaapiH264Decoder::ProcessSlice(media::H264SliceHeader
* slice_hdr
) {
1537 prev_frame_num_
= frame_num_
;
1538 frame_num_
= slice_hdr
->frame_num
;
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
);
1546 if (slice_hdr
->field_pic_flag
== 0)
1547 max_pic_num_
= max_frame_num_
;
1549 max_pic_num_
= 2 * max_frame_num_
;
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
);
1558 // A new frame, so first finish the previous one before processing it...
1559 if (!FinishPrevFrameIfPresent())
1562 // and then start a new one.
1563 return StartNewFrame(slice_hdr
);
1567 #define SET_ERROR_AND_RETURN() \
1569 DVLOG(1) << "Error during decode"; \
1571 return VaapiH264Decoder::kDecodeError; \
1574 void VaapiH264Decoder::SetStream(const uint8
* ptr
,
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
;
1587 VaapiH264Decoder::DecResult
VaapiH264Decoder::Decode() {
1588 media::H264Parser::Result par_res
;
1589 media::H264NALU nalu
;
1590 DCHECK_NE(state_
, kError
);
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
;
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();
1610 DVLOG(4) << "NALU found: " << static_cast<int>(nalu
.nal_unit_type
);
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
)
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.
1627 // If after reset, we should be able to recover from an IDR.
1628 media::H264SliceHeader slice_hdr
;
1630 par_res
= parser_
.ParseSliceHeader(nalu
, &slice_hdr
);
1631 if (par_res
!= media::H264Parser::kOk
)
1632 SET_ERROR_AND_RETURN();
1634 if (!ProcessSlice(&slice_hdr
))
1635 SET_ERROR_AND_RETURN();
1641 case media::H264NALU::kSPS
: {
1644 if (!FinishPrevFrameIfPresent())
1645 SET_ERROR_AND_RETURN();
1647 par_res
= parser_
.ParseSPS(&sps_id
);
1648 if (par_res
!= media::H264Parser::kOk
)
1649 SET_ERROR_AND_RETURN();
1651 bool need_new_buffers
= false;
1652 if (!ProcessSPS(sps_id
, &need_new_buffers
))
1653 SET_ERROR_AND_RETURN();
1657 if (need_new_buffers
) {
1659 return kDecodeError
;
1661 available_va_surfaces_
.clear();
1662 return kAllocateNewSurfaces
;
1667 case media::H264NALU::kPPS
: {
1668 if (state_
!= kDecoding
)
1673 if (!FinishPrevFrameIfPresent())
1674 SET_ERROR_AND_RETURN();
1676 par_res
= parser_
.ParsePPS(&pps_id
);
1677 if (par_res
!= media::H264Parser::kOk
)
1678 SET_ERROR_AND_RETURN();
1680 if (!ProcessPPS(pps_id
))
1681 SET_ERROR_AND_RETURN();
1685 case media::H264NALU::kAUD
:
1686 case media::H264NALU::kEOSeq
:
1687 case media::H264NALU::kEOStream
:
1688 if (state_
!= kDecoding
)
1690 if (!FinishPrevFrameIfPresent())
1691 SET_ERROR_AND_RETURN();
1696 DVLOG(4) << "Skipping NALU type: " << nalu
.nal_unit_type
;
1702 size_t VaapiH264Decoder::GetRequiredNumOfPictures() {
1703 return dpb_
.max_num_pics() + kPicsInPipeline
;
1706 } // namespace content