Commit 6821e70fc6 for ffmpeg

commit 6821e70fc6c4ed591f2772de32eb5f3796fe7dd7
Author: Kacper Michajłow <kasper93@gmail.com>
Date:   Mon Nov 30 15:59:55 2026 +0100

    swresample: let the full conversion select the DSD channels

    swri_rematrix_build() already finds out whether a matrix only copies
    channels. Let it store the input channel that each output channel
    copies, and pass that or the channel map to the full conversion, which
    DSD takes in any case.

    This replaces the manual conversion for DSD.

diff --git a/libswresample/rematrix.c b/libswresample/rematrix.c
index 31b1078bcc..2c307c9f9a 100644
--- a/libswresample/rematrix.c
+++ b/libswresample/rematrix.c
@@ -739,11 +739,13 @@ av_cold int swri_rematrix_build(SwrContext *s)
     /* a channel that is one channel of the input, or silent, is not mixed */
     for (i = 0; i < nb_out; i++) {
         int sources = 0;
+        s->copy_map[i] = -1;
         for (j = 0; j < nb_in; j++) {
             if (s->matrix[i][j] == 0.0)
                 continue;
             if (s->matrix[i][j] != 1.0 || sources++)
                 return 1;
+            s->copy_map[i] = s->channel_map ? s->channel_map[j] : j;
         }
     }

diff --git a/libswresample/swresample.c b/libswresample/swresample.c
index 1f4cd8cac3..c8a6d76418 100644
--- a/libswresample/swresample.c
+++ b/libswresample/swresample.c
@@ -266,6 +266,10 @@ av_cold int swr_init(struct SwrContext *s){
         if (mixing < 0)
             return mixing;
     }
+    if (mixing && s->out_sample_fmt == AV_SAMPLE_FMT_DSD) {
+        av_log(s, AV_LOG_ERROR, "DSD channels can only be selected, not mixed\n");
+        return AVERROR(EINVAL);
+    }

     if(s->int_sample_fmt == AV_SAMPLE_FMT_NONE){
         // DSD to PCM conversion is done in floating point
@@ -380,41 +384,13 @@ 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;
-        }
-        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;
-            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, 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){
+    /* DSD has no format in between, so its full conversion also copies the
+     * channels that the matrix and the channel map select */
+    if(!s->resample && !s->dither.method &&
+       (s->out_sample_fmt == AV_SAMPLE_FMT_DSD || !s->rematrix && !s->channel_map)){
         s->full_convert = swri_audio_convert_alloc(s->out_sample_fmt,
-                                                   s-> in_sample_fmt, s-> in.ch_count, NULL, 0);
+                                                   s-> in_sample_fmt, s->out.ch_count,
+                                                   s->rematrix ? s->copy_map : s->channel_map, 0);
         // fall through to the generic path for conversions that have no
         // direct implementation (e.g. DSD input to non-float output)
         if (s->full_convert)
diff --git a/libswresample/swresample_internal.h b/libswresample/swresample_internal.h
index 88c734e50e..cf77428132 100644
--- a/libswresample/swresample_internal.h
+++ b/libswresample/swresample_internal.h
@@ -114,7 +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 copy_map[SWR_CH_MAX];                       ///< input channel that each output channel copies
     int engine;

     AVChannelLayout user_used_chlayout;             ///< User set used channel layout