* [FFmpeg-devel] [PATCH] avcodec/mediacodecdec: call MediaCodec.stop on close
@ 2024-08-07 16:27 sfan5
2024-08-07 19:11 ` Matthieu Bouron
2024-08-08 15:29 ` Zhao Zhili
0 siblings, 2 replies; 5+ messages in thread
From: sfan5 @ 2024-08-07 16:27 UTC (permalink / raw)
To: FFmpeg development discussions and patches
[-- Attachment #1: Type: text/plain, Size: 81 bytes --]
Hi all,
attached is a small fix for the MediaCodec code. Tested on Android 14.
[-- Attachment #2: 0001-avcodec-mediacodecdec-call-MediaCodec.stop-on-close.patch --]
[-- Type: text/x-patch, Size: 1298 bytes --]
From 3f5d05920dc6826b4c0ea0ed7969e9259e08084e Mon Sep 17 00:00:00 2001
From: sfan5 <sfan5@live.de>
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 <sfan5@live.de>
---
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
[-- Attachment #3: Type: text/plain, Size: 251 bytes --]
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [FFmpeg-devel] [PATCH] avcodec/mediacodecdec: call MediaCodec.stop on close
2024-08-07 16:27 [FFmpeg-devel] [PATCH] avcodec/mediacodecdec: call MediaCodec.stop on close sfan5
@ 2024-08-07 19:11 ` Matthieu Bouron
2024-08-08 15:29 ` Zhao Zhili
1 sibling, 0 replies; 5+ messages in thread
From: Matthieu Bouron @ 2024-08-07 19:11 UTC (permalink / raw)
To: FFmpeg development discussions and patches
On Wed, Aug 7, 2024 at 6:28 PM sfan5 <sfan5@live.de> wrote:
>
> Hi all,
>
> attached is a small fix for the MediaCodec code. Tested on Android 14.
>
LGTM, will apply in a few days.
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [FFmpeg-devel] [PATCH] avcodec/mediacodecdec: call MediaCodec.stop on close
2024-08-07 16:27 [FFmpeg-devel] [PATCH] avcodec/mediacodecdec: call MediaCodec.stop on close sfan5
2024-08-07 19:11 ` Matthieu Bouron
@ 2024-08-08 15:29 ` Zhao Zhili
2024-08-08 18:14 ` sfan5
1 sibling, 1 reply; 5+ messages in thread
From: Zhao Zhili @ 2024-08-08 15:29 UTC (permalink / raw)
To: FFmpeg development discussions and patches
> On Aug 8, 2024, at 00:27, sfan5 <sfan5@live.de> wrote:
>
> Hi all,
>
> attached is a small fix for the MediaCodec code. Tested on Android 14.
> 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 <sfan5@live.de>
> ---
> 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");
> + }
> + }
> +
Could you elaborate on how this resolved the issue?
If hw_buffer_count is zero, isn’t MediaCodecDecContext will be released immediately?
If hw_buffer_count isn’t zero, the patch make no real change, so how does this patch work?
> ff_mediacodec_dec_unref(s);
>
> return 0;
> --
> 2.46.0
>
>
> <0001-avcodec-mediacodecdec-call-MediaCodec.stop-on-close.patch>_______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [FFmpeg-devel] [PATCH] avcodec/mediacodecdec: call MediaCodec.stop on close
2024-08-08 15:29 ` Zhao Zhili
@ 2024-08-08 18:14 ` sfan5
2024-08-09 2:17 ` Zhao Zhili
0 siblings, 1 reply; 5+ messages in thread
From: sfan5 @ 2024-08-08 18:14 UTC (permalink / raw)
To: ffmpeg-devel
Am 08.08.24 um 17:29 schrieb Zhao Zhili:
>> On Aug 8, 2024, at 00:27, sfan5<sfan5@live.de> wrote:
>>
>> Hi all,
>>
>> attached is a small fix for the MediaCodec code. Tested on Android 14.
>>
>> 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<sfan5@live.de>
>> ---
>> 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");
>> + }
>> + }
>> +
> Could you elaborate on how this resolved the issue?
> If hw_buffer_count is zero, isn’t MediaCodecDecContext will be released immediately?
> If hw_buffer_count isn’t zero, the patch make no real change, so how does this patch work?
Sure.
here's the original report:
https://github.com/mpv-android/mpv-android/issues/966
summary:
mpv's hwdec code uses a single surface to facilitate zero-copy video
playback. Keeping an active MediaCodec instance connected to the surface
blocks the same surface from being used with a different MediaCodec
instance.
However mpv also keeps a reference to the last rendered frame alive even
when switching between files. It also flushes the codec when it's done
with a file. This leads to a situation where hw_buffer_count == 0 &&
refcount > 1.
If you were to say that this should be fixed in mpv I would agree and I
have indeed proposed a PR for this. But I noticed we're never calling
stop() in mediacodecdec and it's a very simple remedy.
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [FFmpeg-devel] [PATCH] avcodec/mediacodecdec: call MediaCodec.stop on close
2024-08-08 18:14 ` sfan5
@ 2024-08-09 2:17 ` Zhao Zhili
0 siblings, 0 replies; 5+ messages in thread
From: Zhao Zhili @ 2024-08-09 2:17 UTC (permalink / raw)
To: FFmpeg development discussions and patches
> On Aug 9, 2024, at 02:14, sfan5 <sfan5@live.de> wrote:
>
> Am 08.08.24 um 17:29 schrieb Zhao Zhili:
>>> On Aug 8, 2024, at 00:27, sfan5<sfan5@live.de> wrote:
>>>
>>> Hi all,
>>>
>>> attached is a small fix for the MediaCodec code. Tested on Android 14.
>>>
>>> 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<sfan5@live.de>
>>> ---
>>> 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");
>>> + }
>>> + }
>>> +
>> Could you elaborate on how this resolved the issue?
>> If hw_buffer_count is zero, isn’t MediaCodecDecContext will be released immediately?
>> If hw_buffer_count isn’t zero, the patch make no real change, so how does this patch work?
>
> Sure.
>
> here's the original report: https://github.com/mpv-android/mpv-android/issues/966
>
> summary:
>
> mpv's hwdec code uses a single surface to facilitate zero-copy video playback. Keeping an active MediaCodec instance connected to the surface blocks the same surface from being used with a different MediaCodec instance.
>
> However mpv also keeps a reference to the last rendered frame alive even when switching between files. It also flushes the codec when it's done with a file. This leads to a situation where hw_buffer_count == 0 && refcount > 1.
>
> If you were to say that this should be fixed in mpv I would agree and I have indeed proposed a PR for this. But I noticed we're never calling stop() in mediacodecdec and it's a very simple remedy.
OK. Please add the background to the commit message. It’s not obvious how to trigger the "hw_buffer_count == 0 && refcount > 1” case.
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org <mailto:ffmpeg-devel@ffmpeg.org>
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org <mailto:ffmpeg-devel-request@ffmpeg.org> with subject "unsubscribe".
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-08-09 2:18 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-08-07 16:27 [FFmpeg-devel] [PATCH] avcodec/mediacodecdec: call MediaCodec.stop on close sfan5
2024-08-07 19:11 ` Matthieu Bouron
2024-08-08 15:29 ` Zhao Zhili
2024-08-08 18:14 ` sfan5
2024-08-09 2:17 ` Zhao Zhili
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git