Commit d00595b30c for ffmpeg
commit d00595b30c62deca7b0e8dcd1b1abf7510c4a002
Author: Kacper Michajłow <kasper93@gmail.com>
Date: Mon Nov 30 15:55:09 2026 +0100
swresample: check the used layout and the channel map against the input
Without a channel map the used channels are the input channels. A used
channel layout with another channel count caused a crash. A channel
map entry past the input channels read a channel pointer that was never
set.
Fail swr_init() for both. a020f58818 checked the same for DSD only and
let the copy of DSD take the first input channels of a shorter used
channel layout, which is an error like for the other formats now.
fate-swr-dsd-select selects its channels with pan instead, the output
is the same.
diff --git a/libswresample/swresample.c b/libswresample/swresample.c
index 10b076b38d..1f4cd8cac3 100644
--- a/libswresample/swresample.c
+++ b/libswresample/swresample.c
@@ -217,6 +217,21 @@ av_cold int swr_init(struct SwrContext *s){
if (swri_check_chlayout(s, &s->used_ch_layout, "used"))
return AVERROR(EINVAL);
+ if (!s->channel_map && s->used_ch_layout.nb_channels != s->in.ch_count) {
+ av_log(s, AV_LOG_ERROR, "Used channel layout of %d channels needs a "
+ "channel map for %d input channels\n",
+ s->used_ch_layout.nb_channels, s->in.ch_count);
+ return AVERROR(EINVAL);
+ }
+ for (int i = 0; s->channel_map && i < s->used_ch_layout.nb_channels; i++) {
+ if (s->channel_map[i] >= s->in.ch_count) {
+ av_log(s, AV_LOG_ERROR, "Channel map selects channel %d of %d "
+ "input channels\n",
+ s->channel_map[i], s->in.ch_count);
+ return AVERROR(EINVAL);
+ }
+ }
+
if (s->used_ch_layout.nb_channels != s->in_ch_layout.nb_channels)
av_channel_layout_uninit(&s->in_ch_layout);
@@ -374,8 +389,7 @@ av_assert0(s->out.ch_count);
ret = AVERROR(EINVAL);
goto fail;
}
- /* the packed copy takes a prefix when the channel counts differ */
- if (s->rematrix || s->channel_map || s->in.ch_count != s->out.ch_count)
+ if (s->rematrix || s->channel_map)
ch_map = s->dsd_ch_map;
for (int i = 0; ch_map && i < s->out.ch_count; i++) {
int ich = i;
@@ -387,11 +401,6 @@ av_assert0(s->out.ch_count);
}
if (ich >= 0 && s->channel_map)
ich = s->channel_map[ich];
- if (ich >= s->in.ch_count) {
- av_log(s, AV_LOG_ERROR, "DSD output channel %d selects missing input channel %d\n", i, ich);
- ret = AVERROR(EINVAL);
- goto fail;
- }
s->dsd_ch_map[i] = ich;
}
s->full_convert = swri_audio_convert_alloc(s->out_sample_fmt, s->in_sample_fmt,
diff --git a/tests/fate/libswresample.mak b/tests/fate/libswresample.mak
index 21bb28598c..fda06b3482 100644
--- a/tests/fate/libswresample.mak
+++ b/tests/fate/libswresample.mak
@@ -1209,9 +1209,10 @@ fate-swr: $(FATE_SWR_SELFTEST-yes)
FATE_SWR += $(FATE_SWR_REALLOC-yes)
-FATE_SWR_DSD-$(call FILTERDEMDECENCMUX, ARESAMPLE, SACD, DST, DSD_MSBF, FRAMECRC, PIPE_PROTOCOL) += fate-swr-dsd-reorder fate-swr-dsd-select
+FATE_SWR_DSD-$(call FILTERDEMDECENCMUX, ARESAMPLE, SACD, DST, DSD_MSBF, FRAMECRC, PIPE_PROTOCOL) += fate-swr-dsd-reorder
fate-swr-dsd-reorder: CMD = framecrc -request_sample_fmt dsd -i $(TARGET_SAMPLES)/sacd/2ch-dsd_5ch-dst.iso -map 0:p:1 -af aresample=ochl=FR+FL+FC+SL+SR+UNSD -c:a dsd_msbf
-fate-swr-dsd-select: CMD = framecrc -request_sample_fmt dsd -i $(TARGET_SAMPLES)/sacd/2ch-dsd_5ch-dst.iso -map 0:p:1 -af aresample=uchl=stereo:ochl=stereo -frames:a 2 -c:a dsd_msbf
+FATE_SWR_DSD-$(call FILTERDEMDECENCMUX, PAN, SACD, DST, DSD_MSBF, FRAMECRC, PIPE_PROTOCOL) += fate-swr-dsd-select
+fate-swr-dsd-select: CMD = framecrc -request_sample_fmt dsd -i $(TARGET_SAMPLES)/sacd/2ch-dsd_5ch-dst.iso -map 0:p:1 -af "pan=stereo|c0=c0|c1=c1" -frames:a 2 -c:a dsd_msbf
FATE_SAMPLES_FFMPEG += $(FATE_SWR_DSD-yes)
fate-swr: $(if $(SAMPLES),$(FATE_SWR_DSD-yes))