diff --git a/src/h264.c b/src/h264.c index 34f5025..7d20a98 100644 --- a/src/h264.c +++ b/src/h264.c @@ -187,6 +187,43 @@ static void dpb_update(struct object_context *context, } } +/* + * Strip ffmpeg-vaapi's POC sentinel. + * + * ffmpeg's H264POCContext initialises prev_poc_msb to (1 << 16) = + * 0x10000 in libavcodec/h264dec.c (lines 301 and 444 of v8.0). After + * an IDR the idr() helper resets prev_poc_msb to that same sentinel. + * ff_h264_init_poc (libavcodec/h264_parse.c lines 296-305) then + * computes pc->poc_msb as prev_poc_msb when the slice header's + * poc_lsb hasn't wrapped — which is the typical case for normal + * content. The sentinel leaks into field_poc[] and from there into + * VAPictureH264.TopFieldOrderCnt / BottomFieldOrderCnt at + * libavcodec/vaapi_h264.c::fill_vaapi_pic. + * + * Working VAAPI backends (intel-iHD, i965 verified empirically on + * meitner 2026-05-02) tolerate the high word — they either mask it + * or treat POCs as relative comparisons. V4L2 stateless H.264 + * driver-side consumers (hantro_h264.c::prepare_table feeds the + * value direct to tbl->poc[]) need the spec value, so we strip the + * sentinel here at the libva-v4l2-request boundary. + * + * Detection by bit-16-set rather than blind subtraction so that a + * future ffmpeg version that fixes the sentinel leak degrades + * gracefully. POC values for non-degenerate H.264 content rarely + * exceed 16 bits; bit 16 set is a strong signal of the sentinel. + * + * Empty DPB slots (VA_PICTURE_H264_INVALID) carry POC=0 by + * libavcodec/vaapi_h264.c::init_vaapi_pic and need no fix-up. + */ +static inline int32_t h264_strip_ffmpeg_poc_sentinel(int32_t poc, uint32_t flags) +{ + if (flags & VA_PICTURE_H264_INVALID) + return 0; + if (poc & (1 << 16)) + return poc - (1 << 16); + return poc; +} + static void h264_fill_dpb(struct request_data *data, struct object_context *context, struct v4l2_ctrl_h264_decode_params *decode) @@ -210,8 +247,12 @@ static void h264_fill_dpb(struct request_data *data, dpb->frame_num = entry->pic.frame_idx; dpb->pic_num = entry->pic.picture_id; - dpb->top_field_order_cnt = entry->pic.TopFieldOrderCnt; - dpb->bottom_field_order_cnt = entry->pic.BottomFieldOrderCnt; + dpb->top_field_order_cnt = + h264_strip_ffmpeg_poc_sentinel(entry->pic.TopFieldOrderCnt, + entry->pic.flags); + dpb->bottom_field_order_cnt = + h264_strip_ffmpeg_poc_sentinel(entry->pic.BottomFieldOrderCnt, + entry->pic.flags); dpb->flags = V4L2_H264_DPB_ENTRY_FLAG_VALID; @@ -298,8 +339,12 @@ static void h264_va_picture_to_v4l2(struct request_data *driver_data, decode->nal_ref_idc = nal_ref_idc; decode->frame_num = VAPicture->frame_num; - decode->top_field_order_cnt = VAPicture->CurrPic.TopFieldOrderCnt; - decode->bottom_field_order_cnt = VAPicture->CurrPic.BottomFieldOrderCnt; + decode->top_field_order_cnt = + h264_strip_ffmpeg_poc_sentinel(VAPicture->CurrPic.TopFieldOrderCnt, + VAPicture->CurrPic.flags); + decode->bottom_field_order_cnt = + h264_strip_ffmpeg_poc_sentinel(VAPicture->CurrPic.BottomFieldOrderCnt, + VAPicture->CurrPic.flags); if (nal_unit_type == 5) decode->flags |= V4L2_H264_DECODE_PARAM_FLAG_IDR_PIC;