From ea0532df88b8de517a2a4817ae347c530ce3f563 Mon Sep 17 00:00:00 2001 From: TheM14 Date: Sat, 14 Nov 2026 23:38:46 +0800 Subject: [PATCH] Initialize native PCM decoders from mpv codec parameters The PCM path only copied lav_codecpar or extradata into its decoder context. mpv's native Matroska demuxer stores the sample rate, channel layout and sample depth in mp_codec_params instead. PCM tracks in MKA therefore reached avcodec_open2 with sample_rate=0 and failed before binaural rendering or WAV output could start. Use mp_set_avctx_codec_headers, as ad_lavc does, to initialize the PCM decoder for both native and libavformat demuxers. Check the conversion result and report initialization failures. Keep the renderer, EOF drain and decoder routing unchanged. Verified the failing 48 kHz, 6-channel, 24-bit PCM MKA now exports through the native Matroska path to a stereo float32 binaural WAV with all 37125120 samples (773.44 seconds). WAV and full FLAC exports still reach EOF with their exact sample counts. E-AC-3 JOC and ad-joc-pcm=no output are bit-identical to the baseline in their regression cases. --- fork/files/audio/decode/ad_joc.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/fork/files/audio/decode/ad_joc.c b/fork/files/audio/decode/ad_joc.c index 68c504c..cb3886a 100644 --- a/fork/files/audio/decode/ad_joc.c +++ b/fork/files/audio/decode/ad_joc.c @@ -43,6 +43,7 @@ #include "audio/aframe.h" #include "audio/chmap.h" #include "audio/format.h" +#include "common/av_common.h" #include "common/msg.h" #include "demux/packet.h" #include "demux/stheader.h" @@ -1088,16 +1089,12 @@ static struct mp_decoder *create(struct mp_filter *parent, talloc_free(da); return NULL; } - if (codec->lav_codecpar) { - avcodec_parameters_to_context(p->pcm_avctx, codec->lav_codecpar); - } else if (codec->extradata && codec->extradata_size > 0) { - p->pcm_avctx->extradata = av_mallocz(codec->extradata_size + - AV_INPUT_BUFFER_PADDING_SIZE); - if (p->pcm_avctx->extradata) { - memcpy(p->pcm_avctx->extradata, codec->extradata, - codec->extradata_size); - p->pcm_avctx->extradata_size = codec->extradata_size; - } + /* Native demuxers (e.g. Matroska) supply mpv codec parameters rather + * than lav_codecpar. Use the same conversion as ad_lavc for both. */ + if (mp_set_avctx_codec_headers(p->pcm_avctx, codec) < 0) { + MP_ERR(da, "cannot set the '%s' decoder parameters\n", codec->codec); + talloc_free(da); + return NULL; } p->pcm_avctx->pkt_timebase = (AVRational){1, 48000}; if (avcodec_open2(p->pcm_avctx, pcm_lavc, NULL) < 0) {