From 11b353e9b12c67b9ef42a45740fe87b30aefbfb7 Mon Sep 17 00:00:00 2001 From: TheM14 Date: Fri, 13 Nov 2026 17:49:38 +0800 Subject: [PATCH] Bound native PCM lookahead and clear EOF state on seek The PCM path decoded and pushed samples into joc_core without any backpressure. The kernel's rendered-output queue is bounded, but its input queue is not, so a large part of the track could pile up and joc_stream_flush() at EOF rendered all of it in one call, causing the stall near the end of playback. Keep at most 96000 samples (2 seconds at 48 kHz) between the samples pushed into the renderer and the samples already handed to mpv. When that limit is reached, drain output and wait instead of decoding another packet. The flush at EOF then only has a short queue to process. Also clear eof_pending/eof_frame and the PCM decoder's in-flight frame/packet in ad_joc_reset(). Near the end of a track the demuxer may already have delivered EOF; seeking back left the stale EOF state in place, and the next process call wrote EOF immediately, closing the track. E-AC-3 JOC and the ad-joc-pcm=no path are unchanged. --- fork/files/audio/decode/ad_joc.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/fork/files/audio/decode/ad_joc.c b/fork/files/audio/decode/ad_joc.c index 1bdd364..aedeeb3 100644 --- a/fork/files/audio/decode/ad_joc.c +++ b/fork/files/audio/decode/ad_joc.c @@ -92,6 +92,9 @@ const struct m_sub_options ad_joc_conf = { #define JOC_FRAME_SAMPLES 1536 #define PULL_BLOCK 4096 +/* Keep the PCM input queue short: a long queue would make flush() render + * seconds of audio in one go and starve the audio thread at EOF. */ +#define PCM_MAX_QUEUE_SAMPLES 96000 struct priv { struct mp_decoder public; @@ -560,6 +563,16 @@ static void process_pcm(struct mp_filter *f) return; } + /* Do not decode far ahead of what has actually been handed to mpv. The + * kernel keeps a rendered-output queue but its input queue is unbounded; + * without this gate a whole track can pile up and flush() at EOF renders + * all of it at once. */ + if (!p->eof && p->pcm_input_samples > p->samples_out + PCM_MAX_QUEUE_SAMPLES) { + if (emit(f)) + return; + return; + } + struct mp_frame in = mp_pin_out_read(f->ppins[0]); if (in.type == MP_FRAME_EOF) { if (!p->eof) { @@ -746,6 +759,15 @@ static void ad_joc_reset(struct mp_filter *f) avcodec_flush_buffers(p->avctx); if (p->pcm_avctx) avcodec_flush_buffers(p->pcm_avctx); + if (p->pcm_avctx) { + av_frame_unref(p->pcm_frame); + av_packet_unref(p->pcm_pkt); + } + if (p->eof_pending) { + mp_frame_unref(&p->eof_frame); + p->eof_pending = false; + } + p->eof_frame = MP_NO_FRAME; if (p->swr) { swr_free(&p->swr); av_channel_layout_uninit(&p->swr_in_layout);