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

## Summary of changes

Check the return value of `ff_AMediaCodec_queueInputBuffer()` and report 
rejected frames or EOS buffers as `AVERROR_EXTERNAL`. Set `eof_sent` only after 
a successful EOS submission in the buffer and Surface paths. On failure, log 
the underlying MediaCodec status.

An EOS submission failure previously left the encoder waiting for EOS output 
that the hardware was never asked to produce. A failed ordinary submission 
could also be consumed as if MediaCodec had accepted it.

Closes #26412

The source change has been rebased onto master 
`25bf0776155da64accda16cf3e1834cdaf42f3a4`. The patched diff and the encoder 
and wrapper sources are unchanged from the tested baseline.

## Validation

Cross-compiled both the original and patched FFmpeg master 
`7fdb4c23411c8dc0342f19b85a8729cd22afa0dc` for Android arm64 API 30 and ran 
both on Android 11 with Qualcomm OMX encoders.

| Injected buffer-path condition | Original H.264 and HEVC | Patched H.264 and 
HEVC |
| --- | --- | --- |
| No fault | 12 packets and EOF | 12 packets and EOF |
| Last ordinary frame rejected | 11 packets and `EAGAIN` | 11 packets and 
`AVERROR_EXTERNAL` |
| EOS submission rejected | Terminated by 12-second watchdog | 12 packets and 
`AVERROR_EXTERNAL` |

The Surface branch was checked at source level. Full FATE coverage remains 
pending. The retry policy for a MediaCodec call that keeps reporting temporary 
unavailability is a separate change.

The patch and test probe were prepared with OpenAI Codex assistance.


>From 8bb846ee5257b407ceaa326cdbfc70da1386f5cb Mon Sep 17 00:00:00 2001
From: tiantiantianblue <[email protected]>
Date: Fri, 27 Nov 2026 18:07:06 +0000
Subject: [PATCH] avcodec/mediacodecenc: propagate input submission errors

The encoder ignores queueInputBuffer failures. When MediaCodec
rejects an ordinary frame, the caller can receive EAGAIN even though
the frame has been lost. If the EOS buffer is rejected, eof_sent
is already set and the caller can wait for output EOS indefinitely.

Return AVERROR_EXTERNAL on rejected input. Advance eof_sent only after
MediaCodec accepts the EOS request, including the input Surface path.

Cross-built both variants from 7fdb4c23411c8dc0342f19b85a8729cd22afa0dc
for Android arm64 API 30. On Android 11, an NDK test shim returned
AMEDIA_ERROR_INVALID_OPERATION from queueInputBuffer after sending
synthetic NV12 frames. H.264 and HEVC reproduced both failures.
With the patch, the encoder returned AVERROR_EXTERNAL in both cases.
Without a fault, both versions produced 12 packets per codec.

The Surface change was checked at source level. The patch and test
probe were prepared with OpenAI Codex assistance.

Signed-off-by: lei peng <[email protected]>
---
 libavcodec/mediacodecenc.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/libavcodec/mediacodecenc.c b/libavcodec/mediacodecenc.c
index 631184cbee0..fa5ae5c9b73 100644
--- a/libavcodec/mediacodecenc.c
+++ b/libavcodec/mediacodecenc.c
@@ -802,8 +802,13 @@ static int mediacodec_send(AVCodecContext *avctx,
 
     if (s->window) {
         if (!frame) {
+            ret = ff_AMediaCodec_signalEndOfInputStream(codec);
+            if (ret < 0) {
+                av_log(avctx, AV_LOG_ERROR, "Failed to signal end of input: 
%d\n", ret);
+                return AVERROR_EXTERNAL;
+            }
             s->eof_sent = 1;
-            return ff_AMediaCodec_signalEndOfInputStream(codec);
+            return 0;
         }
 
         if (frame->data[3])
@@ -830,10 +835,16 @@ static int mediacodec_send(AVCodecContext *avctx,
         pts = av_rescale_q(frame->pts, avctx->time_base, AV_TIME_BASE_Q);
     } else {
         flags |= ff_AMediaCodec_getBufferFlagEndOfStream(codec);
-        s->eof_sent = 1;
     }
 
-    ff_AMediaCodec_queueInputBuffer(codec, index, 0, input_size, pts, flags);
+    ret = ff_AMediaCodec_queueInputBuffer(codec, index, 0, input_size, pts, 
flags);
+    if (ret < 0) {
+        av_log(avctx, AV_LOG_ERROR, "Failed to queue %s input buffer: %d\n",
+               frame ? "frame" : "end-of-stream", ret);
+        return AVERROR_EXTERNAL;
+    }
+    if (!frame)
+        s->eof_sent = 1;
     return 0;
 }
 
-- 
2.54.1

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

Reply via email to