From 3f5d05920dc6826b4c0ea0ed7969e9259e08084e Mon Sep 17 00:00:00 2001 From: sfan5 Date: Wed, 7 Aug 2024 17:48:06 +0200 Subject: [PATCH] avcodec/mediacodecdec: call MediaCodec.stop on close This can free up vital resources in case of using multiple decoding instances and there are buffer references left over and not immediately cleaned up. Signed-off-by: sfan5 --- libavcodec/mediacodecdec_common.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/libavcodec/mediacodecdec_common.c b/libavcodec/mediacodecdec_common.c index d6f91e6e89..c888dea8cf 100644 --- a/libavcodec/mediacodecdec_common.c +++ b/libavcodec/mediacodecdec_common.c @@ -841,6 +841,18 @@ int ff_mediacodec_dec_flush(AVCodecContext *avctx, MediaCodecDecContext *s) int ff_mediacodec_dec_close(AVCodecContext *avctx, MediaCodecDecContext *s) { + if (!s) + return 0; + + if (s->codec) { + if (atomic_load(&s->hw_buffer_count) == 0) { + ff_AMediaCodec_stop(s->codec); + av_log(avctx, AV_LOG_DEBUG, "MediaCodec %p stopped\n", s->codec); + } else { + av_log(avctx, AV_LOG_DEBUG, "Not stopping MediaCodec (there are buffers pending)\n"); + } + } + ff_mediacodec_dec_unref(s); return 0; -- 2.46.0