Usually the MediaCodec context will be released immediately, or it needs to stay alive due to existing hardware buffers. However we can free resources early in the case of hw_buffer_count == 0 && refcount > 1, which can be reproduced by keeping frames referenced after flushing and closing. mpv currently behaves like this.
Unified · Split
| @@ -841,6 +841,18 @@ int ff_mediacodec_dec_flush(AVCodecContext *avctx, MediaCodecDecContext *s) · 840 unmodified lines | ||
| 841 | 841 | |
| 842 | 842 | int ff_mediacodec_dec_close(AVCodecContext *avctx, MediaCodecDecContext *s) |
| 843 | 843 | { |
| 844 | + if (!s) | |
| 845 | + return 0; | |
| 846 | + | |
| 847 | + if (s->codec) { | |
| 848 | + if (atomic_load(&s->hw_buffer_count) == 0) { | |
| 849 | + ff_AMediaCodec_stop(s->codec); | |
| 850 | + av_log(avctx, AV_LOG_DEBUG, "MediaCodec %p stopped\n", s->codec); | |
| 851 | + } else { | |
| 852 | + av_log(avctx, AV_LOG_DEBUG, "Not stopping MediaCodec (there are buffers pending)\n"); | |
| 853 | + } | |
| 854 | + } | |
| 855 | + | |
| 844 | 856 | ff_mediacodec_dec_unref(s); |
| 845 | 857 | |
| 846 | 858 | return 0; |