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.
This commit is contained in:
TheM14 committed 2026-11-14 23:38:46 +08:00
1 parent ad9419fa7d
commit ea0532df88
1 file changed
+7 -10
+7 -10
View File
@@ -43,6 +43,7 @@
#include "audio/aframe.h" #include "audio/aframe.h"
#include "audio/chmap.h" #include "audio/chmap.h"
#include "audio/format.h" #include "audio/format.h"
#include "common/av_common.h"
#include "common/msg.h" #include "common/msg.h"
#include "demux/packet.h" #include "demux/packet.h"
#include "demux/stheader.h" #include "demux/stheader.h"
@@ -1088,16 +1089,12 @@ static struct mp_decoder *create(struct mp_filter *parent,
talloc_free(da); talloc_free(da);
return NULL; return NULL;
} }
if (codec->lav_codecpar) { /* Native demuxers (e.g. Matroska) supply mpv codec parameters rather
avcodec_parameters_to_context(p->pcm_avctx, codec->lav_codecpar); * than lav_codecpar. Use the same conversion as ad_lavc for both. */
} else if (codec->extradata && codec->extradata_size > 0) { if (mp_set_avctx_codec_headers(p->pcm_avctx, codec) < 0) {
p->pcm_avctx->extradata = av_mallocz(codec->extradata_size + MP_ERR(da, "cannot set the '%s' decoder parameters\n", codec->codec);
AV_INPUT_BUFFER_PADDING_SIZE); talloc_free(da);
if (p->pcm_avctx->extradata) { return NULL;
memcpy(p->pcm_avctx->extradata, codec->extradata,
codec->extradata_size);
p->pcm_avctx->extradata_size = codec->extradata_size;
}
} }
p->pcm_avctx->pkt_timebase = (AVRational){1, 48000}; p->pcm_avctx->pkt_timebase = (AVRational){1, 48000};
if (avcodec_open2(p->pcm_avctx, pcm_lavc, NULL) < 0) { if (avcodec_open2(p->pcm_avctx, pcm_lavc, NULL) < 0) {