Commit a020f58818 for ffmpeg
commit a020f58818801c617cb189d35edd0ac4b92216c4
Author: Romain Beauxis <romain.beauxis@gmail.com>
Date: Wed Nov 25 09:47:30 2026 -0600
swresample: select the channels of DSD
DSD output was accepted from DSD whatever the layouts, but a change of
channels went through an internal format, and none is converted from
DSD, so it failed with a misleading error.
1-bit samples cannot be mixed, so DSD is now copied directly, each
output channel from the one input channel the matrix takes it from, or
as silence. A matrix that mixes channels is refused.
diff --git a/libswresample/swresample.c b/libswresample/swresample.c
index 52ad73836e..b1e86af07e 100644
--- a/libswresample/swresample.c
+++ b/libswresample/swresample.c
@@ -363,6 +363,44 @@ av_assert0(s->out.ch_count);
if ((ret = swri_dither_init(s, s->out_sample_fmt, s->int_sample_fmt)) < 0)
goto fail;
+ /* 1-bit samples cannot be mixed, the matrix may only select channels */
+ if (s->out_sample_fmt == AV_SAMPLE_FMT_DSD) {
+ const int *ch_map = NULL;
+
+ if (mixing) {
+ av_log(s, AV_LOG_ERROR, "DSD channels can only be selected, not mixed\n");
+ 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)
+ ch_map = s->dsd_ch_map;
+ for (int i = 0; ch_map && i < s->out.ch_count; i++) {
+ int ich = i;
+ if (s->rematrix) {
+ ich = -1;
+ for (int j = 0; j < s->used_ch_layout.nb_channels; j++)
+ if (s->matrix[i][j] != 0.0)
+ ich = j;
+ }
+ 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,
+ s->out.ch_count, ch_map, 0);
+ if (!s->full_convert) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+ return 0;
+ }
+
if(!s->resample && !s->rematrix && !s->channel_map && !s->dither.method){
s->full_convert = swri_audio_convert_alloc(s->out_sample_fmt,
s-> in_sample_fmt, s-> in.ch_count, NULL, 0);
diff --git a/libswresample/swresample_internal.h b/libswresample/swresample_internal.h
index dcfb5335e7..88c734e50e 100644
--- a/libswresample/swresample_internal.h
+++ b/libswresample/swresample_internal.h
@@ -114,6 +114,7 @@ struct SwrContext {
float rematrix_maxval; ///< maximum value for rematrixing output
int matrix_encoding; /**< matrixed stereo encoding */
const int *channel_map; ///< channel index (or -1 if muted channel) map
+ int dsd_ch_map[SWR_CH_MAX]; ///< input channel of each output channel, or -1, when DSD is copied
int engine;
AVChannelLayout user_used_chlayout; ///< User set used channel layout
diff --git a/tests/fate/libswresample.mak b/tests/fate/libswresample.mak
index bd3b228a58..21bb28598c 100644
--- a/tests/fate/libswresample.mak
+++ b/tests/fate/libswresample.mak
@@ -1209,5 +1209,12 @@ 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-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_SAMPLES_FFMPEG += $(FATE_SWR_DSD-yes)
+fate-swr: $(if $(SAMPLES),$(FATE_SWR_DSD-yes))
+
FATE_FFMPEG += $(FATE_SWR)
fate-swr: $(FATE_SWR)
diff --git a/tests/ref/fate/swr-dsd-reorder b/tests/ref/fate/swr-dsd-reorder
new file mode 100644
index 0000000000..466bec512d
--- /dev/null
+++ b/tests/ref/fate/swr-dsd-reorder
@@ -0,0 +1,74 @@
+#tb 0: 1/352800
+#media_type 0: audio
+#codec_id 0: dsd_msbf
+#sample_rate 0: 352800
+#channel_layout_name 0: 6 channels (FR+FL+FC+SL+SR+UNSD)
+0, 0, 0, 4704, 28224, 0x143db5ea
+0, 4704, 4704, 4704, 28224, 0xb93b1f3e
+0, 9408, 9408, 4704, 28224, 0xabf7bdfa
+0, 14112, 14112, 4704, 28224, 0xc4a2d24a
+0, 18816, 18816, 4704, 28224, 0xb1ae3497
+0, 23520, 23520, 4704, 28224, 0x4681c124
+0, 28224, 28224, 4704, 28224, 0x7d0b3ae3
+0, 32928, 32928, 4704, 28224, 0x7d0b3ae3
+0, 37632, 37632, 4704, 28224, 0x7d0b3ae3
+0, 42336, 42336, 4704, 28224, 0x7d0b3ae3
+0, 47040, 47040, 4704, 28224, 0x7d0b3ae3
+0, 51744, 51744, 4704, 28224, 0x7d0b3ae3
+0, 56448, 56448, 4704, 28224, 0x7d0b3ae3
+0, 61152, 61152, 4704, 28224, 0x7d0b3ae3
+0, 65856, 65856, 4704, 28224, 0x7d0b3ae3
+0, 70560, 70560, 4704, 28224, 0x7d0b3ae3
+0, 75264, 75264, 4704, 28224, 0x7d0b3ae3
+0, 79968, 79968, 4704, 28224, 0x7d0b3ae3
+0, 84672, 84672, 4704, 28224, 0x7d0b3ae3
+0, 89376, 89376, 4704, 28224, 0x7d0b3ae3
+0, 94080, 94080, 4704, 28224, 0x7d0b3ae3
+0, 98784, 98784, 4704, 28224, 0x7d0b3ae3
+0, 103488, 103488, 4704, 28224, 0x7d0b3ae3
+0, 108192, 108192, 4704, 28224, 0x7d0b3ae3
+0, 112896, 112896, 4704, 28224, 0x7d0b3ae3
+0, 117600, 117600, 4704, 28224, 0x7d0b3ae3
+0, 122304, 122304, 4704, 28224, 0x7d0b3ae3
+0, 127008, 127008, 4704, 28224, 0x7d0b3ae3
+0, 131712, 131712, 4704, 28224, 0x7d0b3ae3
+0, 136416, 136416, 4704, 28224, 0x7d0b3ae3
+0, 141120, 141120, 4704, 28224, 0x7d0b3ae3
+0, 145824, 145824, 4704, 28224, 0x7d0b3ae3
+0, 150528, 150528, 4704, 28224, 0x7d0b3ae3
+0, 155232, 155232, 4704, 28224, 0x7d0b3ae3
+0, 159936, 159936, 4704, 28224, 0x7d0b3ae3
+0, 164640, 164640, 4704, 28224, 0x7d0b3ae3
+0, 169344, 169344, 4704, 28224, 0xc293b5e6
+0, 174048, 174048, 4704, 28224, 0xdd6f3c03
+0, 178752, 178752, 4704, 28224, 0xc5289e48
+0, 183456, 183456, 4704, 28224, 0x4a74afa6
+0, 188160, 188160, 4704, 28224, 0x25555dda
+0, 192864, 192864, 4704, 28224, 0xd7e9c9cf
+0, 197568, 197568, 4704, 28224, 0x7d0b3ae3
+0, 202272, 202272, 4704, 28224, 0x7d0b3ae3
+0, 206976, 206976, 4704, 28224, 0x7d0b3ae3
+0, 211680, 211680, 4704, 28224, 0x7d0b3ae3
+0, 216384, 216384, 4704, 28224, 0x7d0b3ae3
+0, 221088, 221088, 4704, 28224, 0x7d0b3ae3
+0, 225792, 225792, 4704, 28224, 0x7d0b3ae3
+0, 230496, 230496, 4704, 28224, 0x7d0b3ae3
+0, 235200, 235200, 4704, 28224, 0x7d0b3ae3
+0, 239904, 239904, 4704, 28224, 0x7d0b3ae3
+0, 244608, 244608, 4704, 28224, 0x7d0b3ae3
+0, 249312, 249312, 4704, 28224, 0x7d0b3ae3
+0, 254016, 254016, 4704, 28224, 0x7d0b3ae3
+0, 258720, 258720, 4704, 28224, 0x7d0b3ae3
+0, 263424, 263424, 4704, 28224, 0x7d0b3ae3
+0, 268128, 268128, 4704, 28224, 0x7d0b3ae3
+0, 272832, 272832, 4704, 28224, 0x7d0b3ae3
+0, 277536, 277536, 4704, 28224, 0x7d0b3ae3
+0, 282240, 282240, 4704, 28224, 0x7d0b3ae3
+0, 286944, 286944, 4704, 28224, 0x7d0b3ae3
+0, 291648, 291648, 4704, 28224, 0x7d0b3ae3
+0, 296352, 296352, 4704, 28224, 0x7d0b3ae3
+0, 301056, 301056, 4704, 28224, 0x7d0b3ae3
+0, 305760, 305760, 4704, 28224, 0x7d0b3ae3
+0, 310464, 310464, 4704, 28224, 0x7d0b3ae3
+0, 315168, 315168, 4704, 28224, 0x7d0b3ae3
+0, 319872, 319872, 4704, 28224, 0x7d0b3ae3
diff --git a/tests/ref/fate/swr-dsd-select b/tests/ref/fate/swr-dsd-select
new file mode 100644
index 0000000000..938c4889d8
--- /dev/null
+++ b/tests/ref/fate/swr-dsd-select
@@ -0,0 +1,7 @@
+#tb 0: 1/352800
+#media_type 0: audio
+#codec_id 0: dsd_msbf
+#sample_rate 0: 352800
+#channel_layout_name 0: stereo
+0, 0, 0, 4704, 9408, 0xa17278fd
+0, 4704, 4704, 4704, 9408, 0x8d723e06