Hi ffmpeg-devel,
When Android MediaCodec rejects an input submission, the encoder can
treat it as accepted. An ordinary frame can be lost and surface as
EAGAIN. A rejected EOS buffer can leave the encoder waiting
indefinitely for EOS output.
The attached patch propagates input submission errors and updates
eof_sent only after MediaCodec accepts the EOS request. It is based on
master 7fdb4c23411c8dc0342f19b85a8729cd22afa0dc.
I cross-built the original and patched variants for Android arm64 API
30 and ran the attached probe on Android 11 with Qualcomm OMX H.264
and HEVC encoders. With an NDK shim returning
AMEDIA_ERROR_INVALID_OPERATION, both original encoders returned EAGAIN
and produced 11 packets after the final ordinary frame was rejected.
With EOS rejection, both original runs hit the 12-second watchdog.
Both patched encoders returned AVERROR_EXTERNAL in both fault cases.
The unfaulted runs produced 12 packets before and after the patch. The
Surface change was checked at source level. Full FATE coverage remains
pending.
The attached C files and BUILD-REPRO.txt describe the synthetic input
and fault injection. The patch and probe were prepared with OpenAI
Codex assistance.
Thanks,
lei peng
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libavcodec/avcodec.h>
#include <libavutil/error.h>
#include <libavutil/opt.h>
int main(int argc, char **argv)
{
if (argc != 4)
return 2;
const AVCodec *codec = avcodec_find_encoder_by_name(argv[1]);
AVCodecContext *ctx = codec ? avcodec_alloc_context3(codec) : NULL;
AVFrame *frame = av_frame_alloc();
AVPacket *packet = av_packet_alloc();
int ret = 0, packets = 0, success = 0;
if (!ctx || !frame || !packet)
return 2;
ctx->width = 384;
ctx->height = 256;
ctx->time_base = (AVRational){1, 30};
ctx->framerate = (AVRational){30, 1};
ctx->pix_fmt = AV_PIX_FMT_NV12;
ctx->bit_rate = 4000000;
ctx->gop_size = 30;
ctx->max_b_frames = 0;
if ((ret = av_opt_set(ctx->priv_data, "codec_name", argv[2], 0)) < 0 ||
(ret = av_opt_set_int(ctx->priv_data, "ndk_codec", 1, 0)) < 0 ||
(ret = avcodec_open2(ctx, codec, NULL)) < 0)
goto done;
frame->format = ctx->pix_fmt;
frame->width = ctx->width;
frame->height = ctx->height;
if ((ret = av_frame_get_buffer(frame, 0)) < 0)
goto done;
for (int i = 0; i < 12; ++i) {
if ((ret = av_frame_make_writable(frame)) < 0)
goto done;
memset(frame->data[0], 32 + i * 8, frame->linesize[0] * frame->height);
memset(frame->data[1], 128, frame->linesize[1] * frame->height / 2);
frame->pts = i;
if (i == 11 && !strcmp(argv[3], "frame"))
setenv("FFMC_REJECT", "frame", 1);
if ((ret = avcodec_send_frame(ctx, frame)) < 0)
goto done;
while (!(ret = avcodec_receive_packet(ctx, packet))) {
++packets;
av_packet_unref(packet);
}
if (ret != AVERROR(EAGAIN))
goto done;
if (i == 11 && !strcmp(argv[3], "frame"))
goto done;
usleep(35000);
}
if (!strcmp(argv[3], "eos"))
setenv("FFMC_REJECT", "eos", 1);
if ((ret = avcodec_send_frame(ctx, NULL)) < 0)
goto done;
while (!(ret = avcodec_receive_packet(ctx, packet))) {
++packets;
av_packet_unref(packet);
}
if (ret == AVERROR_EOF && packets == 12 && !strcmp(argv[3], "normal"))
success = 1;
done:
printf("mode=%s status=%d (%s) packets=%d\n", argv[3], ret,
av_err2str(ret), packets);
if (strcmp(argv[3], "normal") && ret == AVERROR_EXTERNAL)
success = 1;
av_packet_free(&packet);
av_frame_free(&frame);
avcodec_free_context(&ctx);
return success ? 0 : 1;
}
From a802ee65c6607c1e367776da8c1d9ecf5d5b2b94 Mon Sep 17 00:00:00 2001
From: lei peng <[email protected]>
Date: Fri, 27 Nov 2026 22:38:08 +0800
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 631184c..fa5ae5c 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;
}
base-commit: 7fdb4c23411c8dc0342f19b85a8729cd22afa0dc
--
2.34.1
#include <dlfcn.h>
#include <media/NdkMediaCodec.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
media_status_t AMediaCodec_queueInputBuffer(AMediaCodec *codec, size_t index,
off_t offset, size_t size, uint64_t timestamp, uint32_t flags)
{
const char *mode = getenv("FFMC_REJECT");
const int eos = !!(flags & AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM);
if (mode && ((!eos && !strcmp(mode, "frame")) || (eos && !strcmp(mode,
"eos")))) {
fprintf(stderr, "Injected AMEDIA_ERROR_INVALID_OPERATION while queuing
%s\n",
eos ? "EOS" : "frame");
return AMEDIA_ERROR_INVALID_OPERATION;
}
media_status_t (*queue)(AMediaCodec *, size_t, off_t, size_t, uint64_t,
uint32_t) =
dlsym(RTLD_NEXT, "AMediaCodec_queueInputBuffer");
if (!queue)
abort();
return queue(codec, index, offset, size, timestamp, flags);
}
#!/system/bin/sh
set -e
unset FFMC_REJECT
for build in baseline fixed; do
for codec in h264 hevc; do
if [ "$codec" = h264 ]; then
component=OMX.qcom.video.encoder.avc
else
component=OMX.qcom.video.encoder.hevc
fi
for mode in normal frame eos; do
printf 'CASE %s %s %s\n' "$build" "$codec" "$mode"
status=0
timeout 12 env LD_LIBRARY_PATH="$PWD/$build"
LD_PRELOAD="$PWD/reject_input.so" \
./repro "${codec}_mediacodec" "$component" "$mode" || status=$?
printf 'EXIT %s %s %s %d\n' "$build" "$codec" "$mode" "$status"
done
done
done
This reproducer was tested on Android 11 arm64 with NDK r25c and API 30.
Use FFmpeg commit 7fdb4c23411c8dc0342f19b85a8729cd22afa0dc for both builds.
Apply 0001-avcodec-mediacodecenc-propagate-input-submission-errors.patch to the
fixed source directory.
For each source directory, configure a separate build directory.
src=/path/to/ffmpeg
toolchain="$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin"
"$src/configure" --disable-everything --disable-programs --disable-doc \
--disable-avdevice --disable-avformat --disable-avfilter --disable-swscale \
--disable-swresample --disable-network --disable-autodetect \
--disable-static --enable-shared --enable-pic --enable-cross-compile \
--target-os=android --arch=aarch64
--cc="$toolchain/aarch64-linux-android30-clang" \
--ar="$toolchain/llvm-ar" --ranlib="$toolchain/llvm-ranlib" \
--strip="$toolchain/llvm-strip" --enable-mediacodec --enable-jni \
--enable-encoder=h264_mediacodec,hevc_mediacodec
make -j6
Build the probe and the preload library from the directory containing the
attached sources. Set b to the original FFmpeg build directory.
b=/path/to/baseline-build
clang="$toolchain/aarch64-linux-android30-clang"
"$clang" -O2 -I"$src" -I"$b" repro.c \
-L"$b/libavcodec" -L"$b/libavutil" -lavcodec -lavutil -o repro
"$clang" -O2 -shared -fPIC reject_input.c -ldl -o reject_input.so
In the Android test directory, place repro, reject_input.so and run.sh.
Copy the original libavcodec.so and libavutil.so into baseline and the
patched versions into fixed. Run sh run.sh from the test directory.
If needed, substitute the component names in run.sh with matching
hardware encoder names on the test device.
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]