Commit 17de9f8597 for ffmpeg
commit 17de9f85972c89b5dd81378bea7b80b67b3af7f0
Author: Zhao Zhili <quinkblack@foxmail.com>
Date: Wed Nov 25 00:56:21 2026 +0800
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.
diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
index 96aef2eefa..f53aa3d65f 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,