diff --git a/src/h264.c b/src/h264.c index 6efacb2..ff1585a 100644 --- a/src/h264.c +++ b/src/h264.c @@ -226,8 +226,12 @@ static inline int32_t h264_strip_ffmpeg_poc_sentinel(int32_t poc, uint32_t flags static void h264_fill_dpb(struct request_data *data, struct object_context *context, + VAPictureParameterBufferH264 *VAPicture, struct v4l2_ctrl_h264_decode_params *decode) { + const int max_frame_num = + 1 << (VAPicture->seq_fields.bits.log2_max_frame_num_minus4 + 4); + const int cur_frame_num = (int)VAPicture->frame_num; int i; for (i = 0; i < H264_DPB_SIZE; i++) { @@ -246,7 +250,41 @@ static void h264_fill_dpb(struct request_data *data, } dpb->frame_num = entry->pic.frame_idx; - dpb->pic_num = entry->pic.picture_id; + + /* + * Per ext-ctrls-codec-stateless.rst, dpb[].pic_num must + * equal the H.264 spec's PicNum (8-28) for short-term refs + * or LongTermPicNum (8-29) for long-term refs. + * + * For frames (not field-coded), PicNum = FrameNumWrap. + * FrameNumWrap = (frame_num > cur_frame_num) + * ? frame_num - max_frame_num + * : frame_num + * (per spec section 8.2.4.1, frame_num wraparound). + * + * VAAPI convention (libavcodec/vaapi_h264.c::fill_vaapi_pic + * line 64): VAPictureH264.frame_idx holds long_term_frame_idx + * for long-term refs and frame_num for short-term refs. So + * for long-term entries we copy frame_idx straight through + * as LongTermPicNum. + * + * fourier's previous code set pic_num to picture_id (the + * VAAPI surface id) which is unrelated to H.264 PicNum; + * mediatek's vdec_h264_req_common.c::dst_entry->pic_num is + * one consumer that fails on that. Hantro doesn't read + * pic_num at all (uses reference_ts for ref resolution), + * which is why fourier's wrong value never surfaced on + * RK3568. + */ + if (entry->pic.flags & VA_PICTURE_H264_LONG_TERM_REFERENCE) { + dpb->pic_num = entry->pic.frame_idx; + } else { + int frame_num = (int)entry->pic.frame_idx; + dpb->pic_num = (frame_num > cur_frame_num) + ? frame_num - max_frame_num + : frame_num; + } + dpb->top_field_order_cnt = h264_strip_ffmpeg_poc_sentinel(entry->pic.TopFieldOrderCnt, entry->pic.flags); @@ -283,7 +321,7 @@ static void h264_va_picture_to_v4l2(struct request_data *driver_data, nal_ref_idc = (b[0] >> 5) & 0x3; nal_unit_type = b[0] & 0x1f; - h264_fill_dpb(driver_data, context, decode); + h264_fill_dpb(driver_data, context, VAPicture, decode); /* * Populate every V4L2_CID_STATELESS_H264_DECODE_PARAMS field