diff --git a/libswresample/rematrix.c b/libswresample/rematrix.c
index 0d6138d..1af2498 100644
--- a/libswresample/rematrix.c
+++ b/libswresample/rematrix.c
@@ -88,7 +88,7 @@ static int even(int64_t layout){
     return 0;
 }
 
-static int clean_layout(SwrContext *s, int64_t layout){
+static int clean_layout(void *s, int64_t layout){
     if(layout && layout != AV_CH_FRONT_CENTER && !(layout&(layout-1))) {
         char buf[128];
         av_get_channel_layout_string(buf, sizeof(buf), -1, layout);
@@ -116,18 +116,20 @@ static int sane_layout(int64_t layout){
     return 1;
 }
 
-av_cold static int auto_matrix(SwrContext *s)
+av_cold int swr_build_matrix(uint64_t in_ch_layout_param, uint64_t out_ch_layout_param,
+                             double center_mix_level, double surround_mix_level,
+                             double lfe_mix_level, double maxval,
+                             double rematrix_volume, double *matrix_param,
+                             int stride, enum AVMatrixEncoding matrix_encoding, void *log_context)
 {
     int i, j, out_i;
     double matrix[NUM_NAMED_CHANNELS][NUM_NAMED_CHANNELS]={{0}};
     int64_t unaccounted, in_ch_layout, out_ch_layout;
     double maxcoef=0;
     char buf[128];
-    const int matrix_encoding = s->matrix_encoding;
-    float maxval;
 
-    in_ch_layout = clean_layout(s, s->in_ch_layout);
-    out_ch_layout = clean_layout(s, s->out_ch_layout);
+     in_ch_layout = clean_layout(log_context,  in_ch_layout_param);
+    out_ch_layout = clean_layout(log_context, out_ch_layout_param);
 
     if(   out_ch_layout == AV_CH_LAYOUT_STEREO_DOWNMIX
        && (in_ch_layout & AV_CH_LAYOUT_STEREO_DOWNMIX) == 0
@@ -140,18 +142,17 @@ av_cold static int auto_matrix(SwrContext *s)
         in_ch_layout = AV_CH_LAYOUT_STEREO;
 
     if(!sane_layout(in_ch_layout)){
-        av_get_channel_layout_string(buf, sizeof(buf), -1, s->in_ch_layout);
-        av_log(s, AV_LOG_ERROR, "Input channel layout '%s' is not supported\n", buf);
+        av_get_channel_layout_string(buf, sizeof(buf), -1, in_ch_layout_param);
+        av_log(log_context, AV_LOG_ERROR, "Input channel layout '%s' is not supported\n", buf);
         return AVERROR(EINVAL);
     }
 
     if(!sane_layout(out_ch_layout)){
-        av_get_channel_layout_string(buf, sizeof(buf), -1, s->out_ch_layout);
-        av_log(s, AV_LOG_ERROR, "Output channel layout '%s' is not supported\n", buf);
+        av_get_channel_layout_string(buf, sizeof(buf), -1, out_ch_layout_param);
+        av_log(log_context, AV_LOG_ERROR, "Output channel layout '%s' is not supported\n", buf);
         return AVERROR(EINVAL);
     }
 
-    memset(s->matrix, 0, sizeof(s->matrix));
     for(i=0; i<FF_ARRAY_ELEMS(matrix); i++){
         if(in_ch_layout & out_ch_layout & (1ULL<<i))
             matrix[i][i]= 1.0;
@@ -166,8 +167,8 @@ av_cold static int auto_matrix(SwrContext *s)
     if(unaccounted & AV_CH_FRONT_CENTER){
         if((out_ch_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO){
             if(in_ch_layout & AV_CH_LAYOUT_STEREO) {
-                matrix[ FRONT_LEFT][FRONT_CENTER]+= s->clev;
-                matrix[FRONT_RIGHT][FRONT_CENTER]+= s->clev;
+                matrix[ FRONT_LEFT][FRONT_CENTER]+= center_mix_level;
+                matrix[FRONT_RIGHT][FRONT_CENTER]+= center_mix_level;
             } else {
                 matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
                 matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
@@ -180,7 +181,7 @@ av_cold static int auto_matrix(SwrContext *s)
             matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
             matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
             if(in_ch_layout & AV_CH_FRONT_CENTER)
-                matrix[FRONT_CENTER][ FRONT_CENTER] = s->clev*sqrt(2);
+                matrix[FRONT_CENTER][ FRONT_CENTER] = center_mix_level*sqrt(2);
         }else
             av_assert0(0);
     }
@@ -196,18 +197,18 @@ av_cold static int auto_matrix(SwrContext *s)
             if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY ||
                 matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
                 if (unaccounted & (AV_CH_BACK_LEFT | AV_CH_SIDE_LEFT)) {
-                    matrix[FRONT_LEFT ][BACK_CENTER] -= s->slev * M_SQRT1_2;
-                    matrix[FRONT_RIGHT][BACK_CENTER] += s->slev * M_SQRT1_2;
+                    matrix[FRONT_LEFT ][BACK_CENTER] -= surround_mix_level * M_SQRT1_2;
+                    matrix[FRONT_RIGHT][BACK_CENTER] += surround_mix_level * M_SQRT1_2;
                 } else {
-                    matrix[FRONT_LEFT ][BACK_CENTER] -= s->slev;
-                    matrix[FRONT_RIGHT][BACK_CENTER] += s->slev;
+                    matrix[FRONT_LEFT ][BACK_CENTER] -= surround_mix_level;
+                    matrix[FRONT_RIGHT][BACK_CENTER] += surround_mix_level;
                 }
             } else {
-                matrix[ FRONT_LEFT][BACK_CENTER]+= s->slev*M_SQRT1_2;
-                matrix[FRONT_RIGHT][BACK_CENTER]+= s->slev*M_SQRT1_2;
+                matrix[ FRONT_LEFT][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
+                matrix[FRONT_RIGHT][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
             }
         }else if(out_ch_layout & AV_CH_FRONT_CENTER){
-            matrix[ FRONT_CENTER][BACK_CENTER]+= s->slev*M_SQRT1_2;
+            matrix[ FRONT_CENTER][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
         }else
             av_assert0(0);
     }
@@ -225,22 +226,22 @@ av_cold static int auto_matrix(SwrContext *s)
             }
         }else if(out_ch_layout & AV_CH_FRONT_LEFT){
             if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
-                matrix[FRONT_LEFT ][BACK_LEFT ] -= s->slev * M_SQRT1_2;
-                matrix[FRONT_LEFT ][BACK_RIGHT] -= s->slev * M_SQRT1_2;
-                matrix[FRONT_RIGHT][BACK_LEFT ] += s->slev * M_SQRT1_2;
-                matrix[FRONT_RIGHT][BACK_RIGHT] += s->slev * M_SQRT1_2;
+                matrix[FRONT_LEFT ][BACK_LEFT ] -= surround_mix_level * M_SQRT1_2;
+                matrix[FRONT_LEFT ][BACK_RIGHT] -= surround_mix_level * M_SQRT1_2;
+                matrix[FRONT_RIGHT][BACK_LEFT ] += surround_mix_level * M_SQRT1_2;
+                matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level * M_SQRT1_2;
             } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
-                matrix[FRONT_LEFT ][BACK_LEFT ] -= s->slev * SQRT3_2;
-                matrix[FRONT_LEFT ][BACK_RIGHT] -= s->slev * M_SQRT1_2;
-                matrix[FRONT_RIGHT][BACK_LEFT ] += s->slev * M_SQRT1_2;
-                matrix[FRONT_RIGHT][BACK_RIGHT] += s->slev * SQRT3_2;
+                matrix[FRONT_LEFT ][BACK_LEFT ] -= surround_mix_level * SQRT3_2;
+                matrix[FRONT_LEFT ][BACK_RIGHT] -= surround_mix_level * M_SQRT1_2;
+                matrix[FRONT_RIGHT][BACK_LEFT ] += surround_mix_level * M_SQRT1_2;
+                matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level * SQRT3_2;
             } else {
-                matrix[ FRONT_LEFT][ BACK_LEFT] += s->slev;
-                matrix[FRONT_RIGHT][BACK_RIGHT] += s->slev;
+                matrix[ FRONT_LEFT][ BACK_LEFT] += surround_mix_level;
+                matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level;
             }
         }else if(out_ch_layout & AV_CH_FRONT_CENTER){
-            matrix[ FRONT_CENTER][BACK_LEFT ]+= s->slev*M_SQRT1_2;
-            matrix[ FRONT_CENTER][BACK_RIGHT]+= s->slev*M_SQRT1_2;
+            matrix[ FRONT_CENTER][BACK_LEFT ]+= surround_mix_level*M_SQRT1_2;
+            matrix[ FRONT_CENTER][BACK_RIGHT]+= surround_mix_level*M_SQRT1_2;
         }else
             av_assert0(0);
     }
@@ -261,22 +262,22 @@ av_cold static int auto_matrix(SwrContext *s)
             matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
         }else if(out_ch_layout & AV_CH_FRONT_LEFT){
             if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
-                matrix[FRONT_LEFT ][SIDE_LEFT ] -= s->slev * M_SQRT1_2;
-                matrix[FRONT_LEFT ][SIDE_RIGHT] -= s->slev * M_SQRT1_2;
-                matrix[FRONT_RIGHT][SIDE_LEFT ] += s->slev * M_SQRT1_2;
-                matrix[FRONT_RIGHT][SIDE_RIGHT] += s->slev * M_SQRT1_2;
+                matrix[FRONT_LEFT ][SIDE_LEFT ] -= surround_mix_level * M_SQRT1_2;
+                matrix[FRONT_LEFT ][SIDE_RIGHT] -= surround_mix_level * M_SQRT1_2;
+                matrix[FRONT_RIGHT][SIDE_LEFT ] += surround_mix_level * M_SQRT1_2;
+                matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level * M_SQRT1_2;
             } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
-                matrix[FRONT_LEFT ][SIDE_LEFT ] -= s->slev * SQRT3_2;
-                matrix[FRONT_LEFT ][SIDE_RIGHT] -= s->slev * M_SQRT1_2;
-                matrix[FRONT_RIGHT][SIDE_LEFT ] += s->slev * M_SQRT1_2;
-                matrix[FRONT_RIGHT][SIDE_RIGHT] += s->slev * SQRT3_2;
+                matrix[FRONT_LEFT ][SIDE_LEFT ] -= surround_mix_level * SQRT3_2;
+                matrix[FRONT_LEFT ][SIDE_RIGHT] -= surround_mix_level * M_SQRT1_2;
+                matrix[FRONT_RIGHT][SIDE_LEFT ] += surround_mix_level * M_SQRT1_2;
+                matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level * SQRT3_2;
             } else {
-                matrix[ FRONT_LEFT][ SIDE_LEFT] += s->slev;
-                matrix[FRONT_RIGHT][SIDE_RIGHT] += s->slev;
+                matrix[ FRONT_LEFT][ SIDE_LEFT] += surround_mix_level;
+                matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level;
             }
         }else if(out_ch_layout & AV_CH_FRONT_CENTER){
-            matrix[ FRONT_CENTER][SIDE_LEFT ]+= s->slev*M_SQRT1_2;
-            matrix[ FRONT_CENTER][SIDE_RIGHT]+= s->slev*M_SQRT1_2;
+            matrix[ FRONT_CENTER][SIDE_LEFT ]+= surround_mix_level * M_SQRT1_2;
+            matrix[ FRONT_CENTER][SIDE_RIGHT]+= surround_mix_level * M_SQRT1_2;
         }else
             av_assert0(0);
     }
@@ -294,10 +295,10 @@ av_cold static int auto_matrix(SwrContext *s)
     /* mix LFE into front left/right or center */
     if (unaccounted & AV_CH_LOW_FREQUENCY) {
         if (out_ch_layout & AV_CH_FRONT_CENTER) {
-            matrix[FRONT_CENTER][LOW_FREQUENCY] += s->lfe_mix_level;
+            matrix[FRONT_CENTER][LOW_FREQUENCY] += lfe_mix_level;
         } else if (out_ch_layout & AV_CH_FRONT_LEFT) {
-            matrix[FRONT_LEFT ][LOW_FREQUENCY] += s->lfe_mix_level * M_SQRT1_2;
-            matrix[FRONT_RIGHT][LOW_FREQUENCY] += s->lfe_mix_level * M_SQRT1_2;
+            matrix[FRONT_LEFT ][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
+            matrix[FRONT_RIGHT][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
         } else
             av_assert0(0);
     }
@@ -311,59 +312,73 @@ av_cold static int auto_matrix(SwrContext *s)
             if((in_ch_layout & (1ULL<<j)) == 0)
                continue;
             if (i < FF_ARRAY_ELEMS(matrix) && j < FF_ARRAY_ELEMS(matrix[0]))
-                s->matrix[out_i][in_i]= matrix[i][j];
+                matrix_param[stride*out_i + in_i] = matrix[i][j];
             else
-                s->matrix[out_i][in_i]= i == j && (in_ch_layout & out_ch_layout & (1ULL<<i));
-            sum += fabs(s->matrix[out_i][in_i]);
+                matrix_param[stride*out_i + in_i] = i == j && (in_ch_layout & out_ch_layout & (1ULL<<i));
+            sum += fabs(matrix_param[stride*out_i + in_i]);
             in_i++;
         }
         maxcoef= FFMAX(maxcoef, sum);
         out_i++;
     }
-    if(s->rematrix_volume  < 0)
-        maxcoef = -s->rematrix_volume;
-
-    if (s->rematrix_maxval > 0) {
-        maxval = s->rematrix_maxval;
-    } else if (   av_get_packed_sample_fmt(s->out_sample_fmt) < AV_SAMPLE_FMT_FLT
-               || av_get_packed_sample_fmt(s->int_sample_fmt) < AV_SAMPLE_FMT_FLT) {
-        maxval = 1.0;
-    } else
-        maxval = INT_MAX;
+    if(rematrix_volume  < 0)
+        maxcoef = -rematrix_volume;
 
-    if(maxcoef > maxval || s->rematrix_volume  < 0){
+    if(maxcoef > maxval || rematrix_volume  < 0){
         maxcoef /= maxval;
         for(i=0; i<SWR_CH_MAX; i++)
             for(j=0; j<SWR_CH_MAX; j++){
-                s->matrix[i][j] /= maxcoef;
+                matrix_param[stride*i + j] /= maxcoef;
             }
     }
 
-    if(s->rematrix_volume > 0){
+    if(rematrix_volume > 0){
         for(i=0; i<SWR_CH_MAX; i++)
             for(j=0; j<SWR_CH_MAX; j++){
-                s->matrix[i][j] *= s->rematrix_volume;
+                matrix_param[stride*i + j] *= rematrix_volume;
             }
     }
 
-    av_log(s, AV_LOG_DEBUG, "Matrix coefficients:\n");
+    av_log(log_context, AV_LOG_DEBUG, "Matrix coefficients:\n");
     for(i=0; i<av_get_channel_layout_nb_channels(out_ch_layout); i++){
         const char *c =
             av_get_channel_name(av_channel_layout_extract_channel(out_ch_layout, i));
-        av_log(s, AV_LOG_DEBUG, "%s: ", c ? c : "?");
+        av_log(log_context, AV_LOG_DEBUG, "%s: ", c ? c : "?");
         for(j=0; j<av_get_channel_layout_nb_channels(in_ch_layout); j++){
             c = av_get_channel_name(av_channel_layout_extract_channel(in_ch_layout, j));
-            av_log(s, AV_LOG_DEBUG, "%s:%f ", c ? c : "?", s->matrix[i][j]);
+            av_log(log_context, AV_LOG_DEBUG, "%s:%f ", c ? c : "?", matrix_param[stride*i + j]);
         }
-        av_log(s, AV_LOG_DEBUG, "\n");
+        av_log(log_context, AV_LOG_DEBUG, "\n");
     }
-    if (s->int_sample_fmt == AV_SAMPLE_FMT_FLTP) {
+    return 0;
+}
+
+av_cold static int auto_matrix(SwrContext *s)
+{
+    double maxval;
+    int ret;
+
+    if (s->rematrix_maxval > 0) {
+        maxval = s->rematrix_maxval;
+    } else if (   av_get_packed_sample_fmt(s->out_sample_fmt) < AV_SAMPLE_FMT_FLT
+               || av_get_packed_sample_fmt(s->int_sample_fmt) < AV_SAMPLE_FMT_FLT) {
+        maxval = 1.0;
+    } else
+        maxval = INT_MAX;
+
+    memset(s->matrix, 0, sizeof(s->matrix));
+    ret = swr_build_matrix(s->in_ch_layout, s->out_ch_layout,
+                           s->clev, s->slev, s->lfe_mix_level,
+                           maxval, s->rematrix_volume, (double*)s->matrix,
+                           s->matrix[1] - s->matrix[0], s->matrix_encoding, s);
+
+    if (ret >= 0 && s->int_sample_fmt == AV_SAMPLE_FMT_FLTP) {
         int i;
         for (i = 0; i < FF_ARRAY_ELEMS(s->matrix[0])*FF_ARRAY_ELEMS(s->matrix[0]); i++)
             s->matrix_flt[0][i] = s->matrix[0][i];
     }
 
-    return 0;
+    return ret;
 }
 
 av_cold int swri_rematrix_init(SwrContext *s){
diff --git a/libswresample/swresample.h b/libswresample/swresample.h
index a09563b..a8db5c2 100644
--- a/libswresample/swresample.h
+++ b/libswresample/swresample.h
@@ -120,6 +120,7 @@
  */
 
 #include <stdint.h>
+#include "libavutil/channel_layout.h"
 #include "libavutil/frame.h"
 #include "libavutil/samplefmt.h"
 
@@ -366,6 +367,36 @@ int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensatio
 int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map);
 
 /**
+ * Generate a channel mixing matrix.
+ *
+ * This function is the one used internally by libswresample for building the
+ * default mixing matrix. It is made public just as a utility function for
+ * building custom matrices.
+ *
+ * @param in_layout           input channel layout
+ * @param out_layout          output channel layout
+ * @param center_mix_level    mix level for the center channel
+ * @param surround_mix_level  mix level for the surround channel(s)
+ * @param lfe_mix_level       mix level for the low-frequency effects channel
+ * @param rematrix_maxval     if 1.0, coefficients will be normalized to prevent
+ *                            overflow. if INT_MAX, coefficients will not be
+ *                            normalized.
+ * @param[out] matrix         mixing coefficients; matrix[i + stride * o] is
+ *                            the weight of input channel i in output channel o.
+ * @param stride              distance between adjacent input channels in the
+ *                            matrix array
+ * @param matrix_encoding     matrixed stereo downmix mode (e.g. dplii)
+ * @param log_ctx             parent logging context, can be NULL
+ * @return                    0 on success, negative AVERROR code on failure
+ */
+int swr_build_matrix(uint64_t in_layout, uint64_t out_layout,
+                     double center_mix_level, double surround_mix_level,
+                     double lfe_mix_level, double rematrix_maxval,
+                     double rematrix_volume, double *matrix,
+                     int stride, enum AVMatrixEncoding matrix_encoding,
+                     void *log_ctx);
+
+/**
  * Set a customized remix matrix.
  *
  * @param s       allocated Swr context, not yet initialized
