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

# Summary of changes

MediaCodec invalidates all buffer indices on flush and, in async mode,
delivers input again only after start(). The encoder kept stale
eof_sent and queued indices, so the next encode hung or crashed.

Signed-off-by: Zhao Zhili <[email protected]>



>From 4410fed09146185752cfefba8f342b3edea7584f Mon Sep 17 00:00:00 2001
From: Zhao Zhili <[email protected]>
Date: Sun, 15 Nov 2026 02:19:20 +0800
Subject: [PATCH] avcodec/mediacodecenc: fix encoding after flush

MediaCodec invalidates all buffer indices on flush and, in async mode,
delivers input again only after start(). The encoder kept stale
eof_sent and queued indices, so the next encode hung or crashed.

Signed-off-by: Zhao Zhili <[email protected]>
---
 libavcodec/mediacodecenc.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c
index 507c48df9c9..b8df2c16433 100644
--- a/libavcodec/mediacodecenc.c
+++ b/libavcodec/mediacodecenc.c
@@ -1012,10 +1012,39 @@ static av_cold int mediacodec_close(AVCodecContext 
*avctx)
 static av_cold void mediacodec_flush(AVCodecContext *avctx)
 {
     MediaCodecEncContext *s = avctx->priv_data;
+    int ret;
+
     if (s->bsf)
         av_bsf_flush(s->bsf);
     av_frame_unref(s->frame);
-    ff_AMediaCodec_flush(s->codec);
+
+    ret = ff_AMediaCodec_flush(s->codec);
+    if (ret < 0)
+        av_log(avctx, AV_LOG_ERROR, "Failed to flush MediaCodec encoder\n");
+
+    s->eof_sent = 0;
+
+    if (!s->async_mode)
+        return;
+
+    ff_mutex_lock(&s->input_mutex);
+    ff_mutex_lock(&s->output_mutex);
+
+    av_fifo_reset2(s->input_index);
+    av_fifo_reset2(s->async_output);
+    s->encode_status = 0;
+
+    ff_cond_signal(&s->output_cond);
+    ff_cond_signal(&s->input_cond);
+
+    ff_mutex_unlock(&s->output_mutex);
+    ff_mutex_unlock(&s->input_mutex);
+
+    // MediaCodec delivers input buffers only after start().
+    ret = ff_AMediaCodec_start(s->codec);
+    if (ret < 0)
+        av_log(avctx, AV_LOG_WARNING,
+               "Failed to resume MediaCodec after async flush\n");
 }
 
 static const AVCodecHWConfigInternal *const mediacodec_hw_configs[] = {
-- 
2.54.1

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

Reply via email to