PR #25288 opened by Forgejo_Fairy
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25288
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25288.patch

When `MFXVideoCORE_SyncOperation()` fails, `ff_qsv_encode()` continues reading 
the bitstream. An empty result then produces `Invalid FrameType:0`, hiding the 
runtime error.

Check negative synchronization results before consuming the bitstream and 
report them through `ff_qsv_print_error()`. Route synchronization failures and 
invalid frame types through a common cleanup path that releases the dequeued 
packet, bitstream, sync-point allocation, and H.264 extension buffers.

Fixes #24967. This addresses error propagation independently of the 
buffer-sizing problem in #24966.

Validation on master 3b3dc211fed4 with libvpl 2.9 on x86-64 Linux: the QSV 
encoder build and `fate-avpacket`, `fate-buffer`, and `fate-fifo` pass. A 
hardware-independent harness covers 60 cases across the six QSV encoders, 
including four negative completion statuses, synchronization retries, 
successful packets, invalid empty output, and queued-packet cleanup. All 30 
successful outputs match baseline; Valgrind reports zero errors and no 
remaining allocations. The harness and reproduction instructions are preserved 
separately on `fairy/issue24967-tooling` in `tools/issue24967/README.md`.

Intel hardware encoding was not available for end-to-end validation.


>From 666a4042b5c4cdbd882abf347d6ddd4651700eda Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Wed, 21 Oct 2026 05:31:34 +0000
Subject: [PATCH] avcodec/qsvenc: handle asynchronous encoding errors

Check MFXVideoCORE_SyncOperation before consuming the bitstream and
report failures through ff_qsv_print_error. Otherwise runtime failures
can be hidden by an Invalid FrameType:0 error.

Use a common cleanup path for dequeued packets, including the H.264
extension buffers. Also route invalid frame types through this path.

Fixes: #24967
Assisted-by: Fairy
---
 libavcodec/qsvenc.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c
index c79db089a0d..e0258c87aaf 100644
--- a/libavcodec/qsvenc.c
+++ b/libavcodec/qsvenc.c
@@ -2664,6 +2664,10 @@ int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext 
*q,
         do {
             ret = MFXVideoCORE_SyncOperation(q->session, *qpkt.sync, 1000);
         } while (ret == MFX_WRN_IN_EXECUTION);
+        if (ret < 0) {
+            ret = ff_qsv_print_error(avctx, ret, "Error while encoding");
+            goto free;
+        }
 
         qpkt.pkt.dts  = av_rescale_q(qpkt.bs->DecodeTimeStamp, (AVRational){1, 
90000}, avctx->time_base);
         qpkt.pkt.pts  = av_rescale_q(qpkt.bs->TimeStamp,       (AVRational){1, 
90000}, avctx->time_base);
@@ -2685,7 +2689,8 @@ int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q,
             av_log(avctx, AV_LOG_WARNING, "Unknown FrameType, set pict_type to 
AV_PICTURE_TYPE_NONE.\n");
         } else {
             av_log(avctx, AV_LOG_ERROR, "Invalid FrameType:%d.\n", 
qpkt.bs->FrameType);
-            return AVERROR_INVALIDDATA;
+            ret = AVERROR_INVALIDDATA;
+            goto free;
         }
 
         if (avctx->codec_id == AV_CODEC_ID_H264) {
@@ -2693,15 +2698,23 @@ int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext 
*q,
             enc_info = (mfxExtAVCEncodedFrameInfo *)(*enc_buf);
             ff_encode_add_stats_side_data(&qpkt.pkt,
                 enc_info->QP * FF_QP2LAMBDA, NULL, 0, pict_type);
+        }
+
+        av_packet_move_ref(pkt, &qpkt.pkt);
+        *got_packet = 1;
+        ret = 0;
+
+free:
+        if (avctx->codec_id == AV_CODEC_ID_H264) {
+            enc_buf = qpkt.bs->ExtParam;
+            enc_info = (mfxExtAVCEncodedFrameInfo *)(*enc_buf);
             av_freep(&enc_info);
             av_freep(&enc_buf);
         }
         av_freep(&qpkt.bs);
         av_freep(&qpkt.sync);
-
-        av_packet_move_ref(pkt, &qpkt.pkt);
-
-        *got_packet = 1;
+        av_packet_unref(&qpkt.pkt);
+        return ret;
     }
 
     return 0;
-- 
2.54.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to