PR #26355 opened by Romain Beauxis (toots)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26355
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26355.patch

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 `aresample` 
failed with "Cannot convert dsd sample format to s16p sample format". Format 
negotiation keeps DSD whenever the next filter accepts it, so this hits any DSD 
chain that reorders or leaves out channels, e.g. a layout with unused channels 
converted to the used ones.

1-bit samples cannot be mixed, so DSD is now copied directly with the existing 
DSD to DSD conversion, each output channel from the one input channel the 
matrix takes it from, or as silence. A matrix that mixes channels, or 
dithering, is refused with "DSD channels can only be selected, not mixed".

Tests: `fate-swr-dsd-reorder` reorders the channels of the DST 5.0 area of the 
existing `sacd/2ch-dsd_5ch-dst.iso` sample and stays DSD. It fails without the 
fix. The output was checked byte for byte against the source channels.


>From 79fa2bdab5afa0955e35b1b5f12754d51e47f006 Mon Sep 17 00:00:00 2001
From: Romain Beauxis <[email protected]>
Date: Wed, 25 Nov 2026 09:47:30 -0600
Subject: [PATCH] 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, or dithering, is refused.
---
 libswresample/swresample.c          | 29 +++++++++++
 libswresample/swresample_internal.h |  1 +
 tests/fate/libswresample.mak        |  6 +++
 tests/ref/fate/swr-dsd-reorder      | 74 +++++++++++++++++++++++++++++
 4 files changed, 110 insertions(+)
 create mode 100644 tests/ref/fate/swr-dsd-reorder

diff --git a/libswresample/swresample.c b/libswresample/swresample.c
index 52ad73836e8..8a36c9e785c 100644
--- a/libswresample/swresample.c
+++ b/libswresample/swresample.c
@@ -363,6 +363,35 @@ 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) {
+        if (mixing || s->dither.method) {
+            av_log(s, AV_LOG_ERROR, "DSD channels can only be selected, not 
mixed\n");
+            ret = AVERROR(EINVAL);
+            goto fail;
+        }
+        for (int i = 0; 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];
+            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,
+                                                   s->rematrix || 
s->channel_map ? s->dsd_ch_map : NULL, 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 dcfb5335e73..88c734e50eb 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 bd3b228a584..13dd0d9a33a 100644
--- a/tests/fate/libswresample.mak
+++ b/tests/fate/libswresample.mak
@@ -1209,5 +1209,11 @@ fate-swr: $(FATE_SWR_SELFTEST-yes)
 
 FATE_SWR += $(FATE_SWR_REALLOC-yes)
 
+FATE_SWR_DSD-$(call FRAMECRC, SACD, DST, ARESAMPLE_FILTER DSD_MSBF_ENCODER) += 
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 -c:a dsd_msbf
+
+FATE_SAMPLES_FFMPEG += $(FATE_SWR_DSD-yes)
+fate-swr: $(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 00000000000..2b69b9b09d7
--- /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: 5 channels (FR+FL+FC+SL+SR)
+0,          0,          0,     4704,    23520, 0xfaa92c21
+0,       4704,       4704,     4704,    23520, 0x57759566
+0,       9408,       9408,     4704,    23520, 0x01603431
+0,      14112,      14112,     4704,    23520, 0xd3974881
+0,      18816,      18816,     4704,    23520, 0x185daabf
+0,      23520,      23520,     4704,    23520, 0x5b91375b
+0,      28224,      28224,     4704,    23520, 0x9e77b10b
+0,      32928,      32928,     4704,    23520, 0x9e77b10b
+0,      37632,      37632,     4704,    23520, 0x9e77b10b
+0,      42336,      42336,     4704,    23520, 0x9e77b10b
+0,      47040,      47040,     4704,    23520, 0x9e77b10b
+0,      51744,      51744,     4704,    23520, 0x9e77b10b
+0,      56448,      56448,     4704,    23520, 0x9e77b10b
+0,      61152,      61152,     4704,    23520, 0x9e77b10b
+0,      65856,      65856,     4704,    23520, 0x9e77b10b
+0,      70560,      70560,     4704,    23520, 0x9e77b10b
+0,      75264,      75264,     4704,    23520, 0x9e77b10b
+0,      79968,      79968,     4704,    23520, 0x9e77b10b
+0,      84672,      84672,     4704,    23520, 0x9e77b10b
+0,      89376,      89376,     4704,    23520, 0x9e77b10b
+0,      94080,      94080,     4704,    23520, 0x9e77b10b
+0,      98784,      98784,     4704,    23520, 0x9e77b10b
+0,     103488,     103488,     4704,    23520, 0x9e77b10b
+0,     108192,     108192,     4704,    23520, 0x9e77b10b
+0,     112896,     112896,     4704,    23520, 0x9e77b10b
+0,     117600,     117600,     4704,    23520, 0x9e77b10b
+0,     122304,     122304,     4704,    23520, 0x9e77b10b
+0,     127008,     127008,     4704,    23520, 0x9e77b10b
+0,     131712,     131712,     4704,    23520, 0x9e77b10b
+0,     136416,     136416,     4704,    23520, 0x9e77b10b
+0,     141120,     141120,     4704,    23520, 0x9e77b10b
+0,     145824,     145824,     4704,    23520, 0x9e77b10b
+0,     150528,     150528,     4704,    23520, 0x9e77b10b
+0,     155232,     155232,     4704,    23520, 0x9e77b10b
+0,     159936,     159936,     4704,    23520, 0x9e77b10b
+0,     164640,     164640,     4704,    23520, 0x9e77b10b
+0,     169344,     169344,     4704,    23520, 0x2f442c1d
+0,     174048,     174048,     4704,    23520, 0xe390b22b
+0,     178752,     178752,     4704,    23520, 0x2a25147f
+0,     183456,     183456,     4704,    23520, 0x541a25dd
+0,     188160,     188160,     4704,    23520, 0x996bd402
+0,     192864,     192864,     4704,    23520, 0x79af4006
+0,     197568,     197568,     4704,    23520, 0x9e77b10b
+0,     202272,     202272,     4704,    23520, 0x9e77b10b
+0,     206976,     206976,     4704,    23520, 0x9e77b10b
+0,     211680,     211680,     4704,    23520, 0x9e77b10b
+0,     216384,     216384,     4704,    23520, 0x9e77b10b
+0,     221088,     221088,     4704,    23520, 0x9e77b10b
+0,     225792,     225792,     4704,    23520, 0x9e77b10b
+0,     230496,     230496,     4704,    23520, 0x9e77b10b
+0,     235200,     235200,     4704,    23520, 0x9e77b10b
+0,     239904,     239904,     4704,    23520, 0x9e77b10b
+0,     244608,     244608,     4704,    23520, 0x9e77b10b
+0,     249312,     249312,     4704,    23520, 0x9e77b10b
+0,     254016,     254016,     4704,    23520, 0x9e77b10b
+0,     258720,     258720,     4704,    23520, 0x9e77b10b
+0,     263424,     263424,     4704,    23520, 0x9e77b10b
+0,     268128,     268128,     4704,    23520, 0x9e77b10b
+0,     272832,     272832,     4704,    23520, 0x9e77b10b
+0,     277536,     277536,     4704,    23520, 0x9e77b10b
+0,     282240,     282240,     4704,    23520, 0x9e77b10b
+0,     286944,     286944,     4704,    23520, 0x9e77b10b
+0,     291648,     291648,     4704,    23520, 0x9e77b10b
+0,     296352,     296352,     4704,    23520, 0x9e77b10b
+0,     301056,     301056,     4704,    23520, 0x9e77b10b
+0,     305760,     305760,     4704,    23520, 0x9e77b10b
+0,     310464,     310464,     4704,    23520, 0x9e77b10b
+0,     315168,     315168,     4704,    23520, 0x9e77b10b
+0,     319872,     319872,     4704,    23520, 0x9e77b10b
-- 
2.54.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to