PR #26327 opened by Zhao Zhili (quink)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26327
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26327.patch

# Summary of changes

AV_CODEC_CAP_ENCODER_FLUSH tells callers they can start another stream
with the same context after avcodec_flush_buffers(). The libx264 flush
callback sends a NULL picture to x264, which ends the stream. The
callback discards the resulting packets, and x264 rejects new frames.

With frame threading enabled, draining again after x264 rejects a
post-flush frame can hang or crash.

Callers can retrieve delayed packets by sending a NULL frame through
avcodec_send_frame() and receiving packets until EOF. Encoding a new
stream requires a new context.


>From 901444cc94e11723ce679e0b28db77917a52a123 Mon Sep 17 00:00:00 2001
From: Zhao Zhili <[email protected]>
Date: Wed, 25 Nov 2026 00:56:21 +0800
Subject: [PATCH] avcodec/libx264: remove the misleading encoder flush flag

AV_CODEC_CAP_ENCODER_FLUSH tells callers they can start another stream
with the same context after avcodec_flush_buffers(). The libx264 flush
callback sends a NULL picture to x264, which ends the stream. The
callback discards the resulting packets, and x264 rejects new frames.

With frame threading enabled, draining again after x264 rejects a
post-flush frame can hang or crash.

Callers can retrieve delayed packets by sending a NULL frame through
avcodec_send_frame() and receiving packets until EOF. Encoding a new
stream requires a new context.
---
 libavcodec/libx264.c | 26 +++-----------------------
 1 file changed, 3 insertions(+), 23 deletions(-)

diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
index 96aef2eefad..f53aa3d65f5 100644
--- a/libavcodec/libx264.c
+++ b/libavcodec/libx264.c
@@ -160,7 +160,7 @@ static int encode_nals(AVCodecContext *ctx, AVPacket *pkt,
 {
     X264Context *x4 = ctx->priv_data;
     uint8_t *p;
-    uint64_t size = FFMAX(x4->sei_size, 0);
+    uint64_t size = x4->sei_size;
     int ret;
 
     if (!nnal)
@@ -187,8 +187,8 @@ static int encode_nals(AVCodecContext *ctx, AVPacket *pkt,
         memcpy(p, x4->sei, x4->sei_size);
         p += x4->sei_size;
         size -= x4->sei_size;
-        /* Keep the value around in case of flush */
-        x4->sei_size = -x4->sei_size;
+        x4->sei_size = 0;
+        av_freep(&x4->sei);
     }
 
     /* x264 guarantees the payloads of the NALs
@@ -813,24 +813,6 @@ static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, 
const AVFrame *frame,
     return 0;
 }
 
-static void X264_flush(AVCodecContext *avctx)
-{
-    X264Context *x4 = avctx->priv_data;
-    x264_nal_t *nal;
-    int nnal, ret;
-    x264_picture_t pic_out = {0};
-
-    do {
-        ret = x264_encoder_encode(x4->enc, &nal, &nnal, NULL, &pic_out);
-    } while (ret > 0 && x264_encoder_delayed_frames(x4->enc));
-
-    for (int i = 0; i < x4->nb_reordered_opaque; i++)
-        opaque_uninit(&x4->reordered_opaque[i]);
-
-    if (x4->sei_size < 0)
-        x4->sei_size = -x4->sei_size;
-}
-
 static av_cold int X264_reconf(AVCodecContext *avctx, AVDictionary **dict)
 {
     int ret;
@@ -1741,7 +1723,6 @@ const FFCodec ff_libx264_encoder = {
     .p.capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
                         AV_CODEC_CAP_OTHER_THREADS |
                         AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE |
-                        AV_CODEC_CAP_ENCODER_FLUSH |
                         AV_CODEC_CAP_ENCODER_RECONF |
                         AV_CODEC_CAP_ENCODER_RECON_FRAME,
     .p.priv_class     = &x264_class,
@@ -1749,7 +1730,6 @@ const FFCodec ff_libx264_encoder = {
     .priv_data_size   = sizeof(X264Context),
     .init             = X264_init,
     FF_CODEC_ENCODE_CB(X264_frame),
-    .flush            = X264_flush,
     .reconf           = X264_reconf,
     .close            = X264_close,
     .defaults         = x264_defaults,
-- 
2.54.1

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

Reply via email to