rematrix.c 22.5 KB
Newer Older
Michael Niedermayer's avatar
Michael Niedermayer committed
1
/*
2
 * Copyright (C) 2011-2012 Michael Niedermayer (michaelni@gmx.at)
Michael Niedermayer's avatar
Michael Niedermayer committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This file is part of libswresample
 *
 * libswresample is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * libswresample is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with libswresample; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "swresample_internal.h"
#include "libavutil/avassert.h"
23
#include "libavutil/channel_layout.h"
Michael Niedermayer's avatar
Michael Niedermayer committed
24

25
#define TEMPLATE_REMATRIX_FLT
Michael Niedermayer's avatar
Michael Niedermayer committed
26
#include "rematrix_template.c"
27
#undef TEMPLATE_REMATRIX_FLT
Michael Niedermayer's avatar
Michael Niedermayer committed
28

29
#define TEMPLATE_REMATRIX_DBL
30
#include "rematrix_template.c"
31
#undef TEMPLATE_REMATRIX_DBL
32

33
#define TEMPLATE_REMATRIX_S16
Michael Niedermayer's avatar
Michael Niedermayer committed
34
#include "rematrix_template.c"
35 36 37
#define TEMPLATE_CLIP
#include "rematrix_template.c"
#undef TEMPLATE_CLIP
38
#undef TEMPLATE_REMATRIX_S16
Michael Niedermayer's avatar
Michael Niedermayer committed
39

40 41 42 43
#define TEMPLATE_REMATRIX_S32
#include "rematrix_template.c"
#undef TEMPLATE_REMATRIX_S32

Michael Niedermayer's avatar
Michael Niedermayer committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#define FRONT_LEFT             0
#define FRONT_RIGHT            1
#define FRONT_CENTER           2
#define LOW_FREQUENCY          3
#define BACK_LEFT              4
#define BACK_RIGHT             5
#define FRONT_LEFT_OF_CENTER   6
#define FRONT_RIGHT_OF_CENTER  7
#define BACK_CENTER            8
#define SIDE_LEFT              9
#define SIDE_RIGHT             10
#define TOP_CENTER             11
#define TOP_FRONT_LEFT         12
#define TOP_FRONT_CENTER       13
#define TOP_FRONT_RIGHT        14
#define TOP_BACK_LEFT          15
#define TOP_BACK_CENTER        16
#define TOP_BACK_RIGHT         17
62
#define NUM_NAMED_CHANNELS     18
Michael Niedermayer's avatar
Michael Niedermayer committed
63

64 65 66 67 68 69 70
int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride)
{
    int nb_in, nb_out, in, out;

    if (!s || s->in_convert) // s needs to be allocated but not initialized
        return AVERROR(EINVAL);
    memset(s->matrix, 0, sizeof(s->matrix));
71
    memset(s->matrix_flt, 0, sizeof(s->matrix_flt));
72 73
    nb_in  = av_get_channel_layout_nb_channels(s->user_in_ch_layout);
    nb_out = av_get_channel_layout_nb_channels(s->user_out_ch_layout);
74 75
    for (out = 0; out < nb_out; out++) {
        for (in = 0; in < nb_in; in++)
76
            s->matrix_flt[out][in] = s->matrix[out][in] = matrix[in];
77 78 79 80 81 82
        matrix += stride;
    }
    s->rematrix_custom = 1;
    return 0;
}

Michael Niedermayer's avatar
Michael Niedermayer committed
83 84 85 86 87 88
static int even(int64_t layout){
    if(!layout) return 1;
    if(layout&(layout-1)) return 1;
    return 0;
}

89
static int clean_layout(void *s, int64_t layout){
90 91 92 93 94 95 96
    if(layout && layout != AV_CH_FRONT_CENTER && !(layout&(layout-1))) {
        char buf[128];
        av_get_channel_layout_string(buf, sizeof(buf), -1, layout);
        av_log(s, AV_LOG_VERBOSE, "Treating %s as mono\n", buf);
        return AV_CH_FRONT_CENTER;
    }

97 98 99
    return layout;
}

Michael Niedermayer's avatar
Michael Niedermayer committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
static int sane_layout(int64_t layout){
    if(!(layout & AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
        return 0;
    if(!even(layout & (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT))) // no asymetric front
        return 0;
    if(!even(layout & (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)))   // no asymetric side
        return 0;
    if(!even(layout & (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)))
        return 0;
    if(!even(layout & (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)))
        return 0;
    if(av_get_channel_layout_nb_channels(layout) >= SWR_CH_MAX)
        return 0;

    return 1;
}

117 118 119 120 121
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)
122
{
123
    int i, j, out_i;
124
    double matrix[NUM_NAMED_CHANNELS][NUM_NAMED_CHANNELS]={{0}};
125
    int64_t unaccounted, in_ch_layout, out_ch_layout;
126
    double maxcoef=0;
127
    char buf[128];
Michael Niedermayer's avatar
Michael Niedermayer committed
128

129 130
     in_ch_layout = clean_layout(log_context,  in_ch_layout_param);
    out_ch_layout = clean_layout(log_context, out_ch_layout_param);
131

132 133 134 135 136
    if(   out_ch_layout == AV_CH_LAYOUT_STEREO_DOWNMIX
       && (in_ch_layout & AV_CH_LAYOUT_STEREO_DOWNMIX) == 0
    )
        out_ch_layout = AV_CH_LAYOUT_STEREO;

137 138 139 140 141
    if(    in_ch_layout == AV_CH_LAYOUT_STEREO_DOWNMIX
       && (out_ch_layout & AV_CH_LAYOUT_STEREO_DOWNMIX) == 0
    )
        in_ch_layout = AV_CH_LAYOUT_STEREO;

142
    if(!sane_layout(in_ch_layout)){
143 144
        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);
Michael Niedermayer's avatar
Michael Niedermayer committed
145 146
        return AVERROR(EINVAL);
    }
147

148
    if(!sane_layout(out_ch_layout)){
149 150
        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);
Michael Niedermayer's avatar
Michael Niedermayer committed
151 152 153
        return AVERROR(EINVAL);
    }

154
    for(i=0; i<FF_ARRAY_ELEMS(matrix); i++){
155
        if(in_ch_layout & out_ch_layout & (1ULL<<i))
156 157 158 159 160
            matrix[i][i]= 1.0;
    }

    unaccounted= in_ch_layout & ~out_ch_layout;

Michael Niedermayer's avatar
Michael Niedermayer committed
161 162 163 164 165
//FIXME implement dolby surround
//FIXME implement full ac3


    if(unaccounted & AV_CH_FRONT_CENTER){
166 167
        if((out_ch_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO){
            if(in_ch_layout & AV_CH_LAYOUT_STEREO) {
168 169
                matrix[ FRONT_LEFT][FRONT_CENTER]+= center_mix_level;
                matrix[FRONT_RIGHT][FRONT_CENTER]+= center_mix_level;
170 171 172 173
            } else {
                matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
                matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
174 175 176 177
        }else
            av_assert0(0);
    }
    if(unaccounted & AV_CH_LAYOUT_STEREO){
178
        if(out_ch_layout & AV_CH_FRONT_CENTER){
179 180
            matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
            matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
181
            if(in_ch_layout & AV_CH_FRONT_CENTER)
182
                matrix[FRONT_CENTER][ FRONT_CENTER] = center_mix_level*sqrt(2);
Michael Niedermayer's avatar
Michael Niedermayer committed
183 184 185 186 187
        }else
            av_assert0(0);
    }

    if(unaccounted & AV_CH_BACK_CENTER){
188
        if(out_ch_layout & AV_CH_BACK_LEFT){
189 190
            matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
            matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
191
        }else if(out_ch_layout & AV_CH_SIDE_LEFT){
192 193
            matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
            matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
194
        }else if(out_ch_layout & AV_CH_FRONT_LEFT){
195 196 197
            if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY ||
                matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
                if (unaccounted & (AV_CH_BACK_LEFT | AV_CH_SIDE_LEFT)) {
198 199
                    matrix[FRONT_LEFT ][BACK_CENTER] -= surround_mix_level * M_SQRT1_2;
                    matrix[FRONT_RIGHT][BACK_CENTER] += surround_mix_level * M_SQRT1_2;
200
                } else {
201 202
                    matrix[FRONT_LEFT ][BACK_CENTER] -= surround_mix_level;
                    matrix[FRONT_RIGHT][BACK_CENTER] += surround_mix_level;
203 204
                }
            } else {
205 206
                matrix[ FRONT_LEFT][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
                matrix[FRONT_RIGHT][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
207
            }
208
        }else if(out_ch_layout & AV_CH_FRONT_CENTER){
209
            matrix[ FRONT_CENTER][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
Michael Niedermayer's avatar
Michael Niedermayer committed
210 211 212 213
        }else
            av_assert0(0);
    }
    if(unaccounted & AV_CH_BACK_LEFT){
214
        if(out_ch_layout & AV_CH_BACK_CENTER){
215 216
            matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
            matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
217 218
        }else if(out_ch_layout & AV_CH_SIDE_LEFT){
            if(in_ch_layout & AV_CH_SIDE_LEFT){
219 220
                matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
                matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
Michael Niedermayer's avatar
Michael Niedermayer committed
221 222 223 224
            }else{
            matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
            matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
            }
225
        }else if(out_ch_layout & AV_CH_FRONT_LEFT){
226
            if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
227 228 229 230
                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;
231
            } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
232 233 234 235
                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;
236
            } else {
237 238
                matrix[ FRONT_LEFT][ BACK_LEFT] += surround_mix_level;
                matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level;
239
            }
240
        }else if(out_ch_layout & AV_CH_FRONT_CENTER){
241 242
            matrix[ FRONT_CENTER][BACK_LEFT ]+= surround_mix_level*M_SQRT1_2;
            matrix[ FRONT_CENTER][BACK_RIGHT]+= surround_mix_level*M_SQRT1_2;
Michael Niedermayer's avatar
Michael Niedermayer committed
243 244 245 246 247
        }else
            av_assert0(0);
    }

    if(unaccounted & AV_CH_SIDE_LEFT){
248
        if(out_ch_layout & AV_CH_BACK_LEFT){
249 250
            /* if back channels do not exist in the input, just copy side
               channels to back channels, otherwise mix side into back */
251
            if (in_ch_layout & AV_CH_BACK_LEFT) {
252 253 254 255 256 257
                matrix[BACK_LEFT ][SIDE_LEFT ] += M_SQRT1_2;
                matrix[BACK_RIGHT][SIDE_RIGHT] += M_SQRT1_2;
            } else {
                matrix[BACK_LEFT ][SIDE_LEFT ] += 1.0;
                matrix[BACK_RIGHT][SIDE_RIGHT] += 1.0;
            }
258
        }else if(out_ch_layout & AV_CH_BACK_CENTER){
259 260
            matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
            matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
261
        }else if(out_ch_layout & AV_CH_FRONT_LEFT){
262
            if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
263 264 265 266
                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;
267
            } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
268 269 270 271
                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;
272
            } else {
273 274
                matrix[ FRONT_LEFT][ SIDE_LEFT] += surround_mix_level;
                matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level;
275
            }
276
        }else if(out_ch_layout & AV_CH_FRONT_CENTER){
277 278
            matrix[ FRONT_CENTER][SIDE_LEFT ]+= surround_mix_level * M_SQRT1_2;
            matrix[ FRONT_CENTER][SIDE_RIGHT]+= surround_mix_level * M_SQRT1_2;
Michael Niedermayer's avatar
Michael Niedermayer committed
279 280 281 282 283
        }else
            av_assert0(0);
    }

    if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
284
        if(out_ch_layout & AV_CH_FRONT_LEFT){
Michael Niedermayer's avatar
Michael Niedermayer committed
285 286
            matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
            matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
287
        }else if(out_ch_layout & AV_CH_FRONT_CENTER){
288 289
            matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
            matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
Michael Niedermayer's avatar
Michael Niedermayer committed
290 291 292
        }else
            av_assert0(0);
    }
Justin Ruggles's avatar
Justin Ruggles committed
293 294
    /* mix LFE into front left/right or center */
    if (unaccounted & AV_CH_LOW_FREQUENCY) {
295
        if (out_ch_layout & AV_CH_FRONT_CENTER) {
296
            matrix[FRONT_CENTER][LOW_FREQUENCY] += lfe_mix_level;
297
        } else if (out_ch_layout & AV_CH_FRONT_LEFT) {
298 299
            matrix[FRONT_LEFT ][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
            matrix[FRONT_RIGHT][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
Justin Ruggles's avatar
Justin Ruggles committed
300 301 302 303
        } else
            av_assert0(0);
    }

Michael Niedermayer's avatar
Michael Niedermayer committed
304 305 306
    for(out_i=i=0; i<64; i++){
        double sum=0;
        int in_i=0;
307 308
        if((out_ch_layout & (1ULL<<i)) == 0)
            continue;
Michael Niedermayer's avatar
Michael Niedermayer committed
309
        for(j=0; j<64; j++){
310 311
            if((in_ch_layout & (1ULL<<j)) == 0)
               continue;
312
            if (i < FF_ARRAY_ELEMS(matrix) && j < FF_ARRAY_ELEMS(matrix[0]))
313
                matrix_param[stride*out_i + in_i] = matrix[i][j];
314
            else
315 316
                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]);
317
            in_i++;
Michael Niedermayer's avatar
Michael Niedermayer committed
318 319
        }
        maxcoef= FFMAX(maxcoef, sum);
320
        out_i++;
Michael Niedermayer's avatar
Michael Niedermayer committed
321
    }
322 323
    if(rematrix_volume  < 0)
        maxcoef = -rematrix_volume;
324

325
    if(maxcoef > maxval || rematrix_volume  < 0){
326
        maxcoef /= maxval;
Michael Niedermayer's avatar
Michael Niedermayer committed
327
        for(i=0; i<SWR_CH_MAX; i++)
328
            for(j=0; j<SWR_CH_MAX; j++){
329
                matrix_param[stride*i + j] /= maxcoef;
330
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
331
    }
332

333
    if(rematrix_volume > 0){
334 335
        for(i=0; i<SWR_CH_MAX; i++)
            for(j=0; j<SWR_CH_MAX; j++){
336
                matrix_param[stride*i + j] *= rematrix_volume;
337 338 339
            }
    }

340
    av_log(log_context, AV_LOG_DEBUG, "Matrix coefficients:\n");
341
    for(i=0; i<av_get_channel_layout_nb_channels(out_ch_layout); i++){
342 343
        const char *c =
            av_get_channel_name(av_channel_layout_extract_channel(out_ch_layout, i));
344
        av_log(log_context, AV_LOG_DEBUG, "%s: ", c ? c : "?");
345
        for(j=0; j<av_get_channel_layout_nb_channels(in_ch_layout); j++){
346
            c = av_get_channel_name(av_channel_layout_extract_channel(in_ch_layout, j));
347
            av_log(log_context, AV_LOG_DEBUG, "%s:%f ", c ? c : "?", matrix_param[stride*i + j]);
Michael Niedermayer's avatar
Michael Niedermayer committed
348
        }
349
        av_log(log_context, AV_LOG_DEBUG, "\n");
350
    }
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
    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) {
374 375 376 377 378
        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];
    }

379
    return ret;
Michael Niedermayer's avatar
Michael Niedermayer committed
380 381
}

382
av_cold int swri_rematrix_init(SwrContext *s){
383
    int i, j;
384 385
    int nb_in  = av_get_channel_layout_nb_channels(s->in_ch_layout);
    int nb_out = av_get_channel_layout_nb_channels(s->out_ch_layout);
386

387 388
    s->mix_any_f = NULL;

389 390 391 392 393
    if (!s->rematrix_custom) {
        int r = auto_matrix(s);
        if (r)
            return r;
    }
394
    if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
395
        int maxsum = 0;
396
        s->native_matrix = av_calloc(nb_in * nb_out, sizeof(int));
397
        s->native_one    = av_mallocz(sizeof(int));
398 399
        if (!s->native_matrix || !s->native_one)
            return AVERROR(ENOMEM);
400 401
        for (i = 0; i < nb_out; i++) {
            double rem = 0;
402
            int sum = 0;
403 404 405 406 407

            for (j = 0; j < nb_in; j++) {
                double target = s->matrix[i][j] * 32768 + rem;
                ((int*)s->native_matrix)[i * nb_in + j] = lrintf(target);
                rem += target - ((int*)s->native_matrix)[i * nb_in + j];
408
                sum += FFABS(((int*)s->native_matrix)[i * nb_in + j]);
409
            }
410
            maxsum = FFMAX(maxsum, sum);
411
        }
412
        *((int*)s->native_one) = 32768;
413 414 415 416 417 418 419 420 421
        if (maxsum <= 32768) {
            s->mix_1_1_f = (mix_1_1_func_type*)copy_s16;
            s->mix_2_1_f = (mix_2_1_func_type*)sum2_s16;
            s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s16(s);
        } else {
            s->mix_1_1_f = (mix_1_1_func_type*)copy_clip_s16;
            s->mix_2_1_f = (mix_2_1_func_type*)sum2_clip_s16;
            s->mix_any_f = (mix_any_func_type*)get_mix_any_func_clip_s16(s);
        }
422
    }else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
423
        s->native_matrix = av_calloc(nb_in * nb_out, sizeof(float));
424
        s->native_one    = av_mallocz(sizeof(float));
425 426
        if (!s->native_matrix || !s->native_one)
            return AVERROR(ENOMEM);
427 428 429 430
        for (i = 0; i < nb_out; i++)
            for (j = 0; j < nb_in; j++)
                ((float*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
        *((float*)s->native_one) = 1.0;
431 432 433
        s->mix_1_1_f = (mix_1_1_func_type*)copy_float;
        s->mix_2_1_f = (mix_2_1_func_type*)sum2_float;
        s->mix_any_f = (mix_any_func_type*)get_mix_any_func_float(s);
434
    }else if(s->midbuf.fmt == AV_SAMPLE_FMT_DBLP){
435
        s->native_matrix = av_calloc(nb_in * nb_out, sizeof(double));
436
        s->native_one    = av_mallocz(sizeof(double));
437 438
        if (!s->native_matrix || !s->native_one)
            return AVERROR(ENOMEM);
439 440 441 442
        for (i = 0; i < nb_out; i++)
            for (j = 0; j < nb_in; j++)
                ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
        *((double*)s->native_one) = 1.0;
443 444 445
        s->mix_1_1_f = (mix_1_1_func_type*)copy_double;
        s->mix_2_1_f = (mix_2_1_func_type*)sum2_double;
        s->mix_any_f = (mix_any_func_type*)get_mix_any_func_double(s);
446 447 448 449
    }else if(s->midbuf.fmt == AV_SAMPLE_FMT_S32P){
        // Only for dithering currently
//         s->native_matrix = av_calloc(nb_in * nb_out, sizeof(double));
        s->native_one    = av_mallocz(sizeof(int));
450 451
        if (!s->native_one)
            return AVERROR(ENOMEM);
452 453 454 455 456 457 458
//         for (i = 0; i < nb_out; i++)
//             for (j = 0; j < nb_in; j++)
//                 ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
        *((int*)s->native_one) = 32768;
        s->mix_1_1_f = (mix_1_1_func_type*)copy_s32;
        s->mix_2_1_f = (mix_2_1_func_type*)sum2_s32;
        s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s32(s);
459 460
    }else
        av_assert0(0);
461 462 463 464 465 466 467 468 469 470
    //FIXME quantize for integeres
    for (i = 0; i < SWR_CH_MAX; i++) {
        int ch_in=0;
        for (j = 0; j < SWR_CH_MAX; j++) {
            s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
            if(s->matrix[i][j])
                s->matrix_ch[i][++ch_in]= j;
        }
        s->matrix_ch[i][0]= ch_in;
    }
471

472 473
    if(HAVE_YASM && HAVE_MMX)
        return swri_rematrix_init_x86(s);
474

475 476 477
    return 0;
}

478
av_cold void swri_rematrix_free(SwrContext *s){
479 480
    av_freep(&s->native_matrix);
    av_freep(&s->native_one);
481
    av_freep(&s->native_simd_matrix);
482
    av_freep(&s->native_simd_one);
483 484
}

485
int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
Michael Niedermayer's avatar
Michael Niedermayer committed
486
    int out_i, in_i, i, j;
487 488
    int len1 = 0;
    int off = 0;
Michael Niedermayer's avatar
Michael Niedermayer committed
489

490
    if(s->mix_any_f) {
491
        s->mix_any_f(out->ch, (const uint8_t **)in->ch, s->native_matrix, len);
492 493 494
        return 0;
    }

495 496 497 498 499
    if(s->mix_2_1_simd || s->mix_1_1_simd){
        len1= len&~15;
        off = len1 * out->bps;
    }

500 501
    av_assert0(!s->out_ch_layout || out->ch_count == av_get_channel_layout_nb_channels(s->out_ch_layout));
    av_assert0(!s-> in_ch_layout || in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
Michael Niedermayer's avatar
Michael Niedermayer committed
502 503 504

    for(out_i=0; out_i<out->ch_count; out_i++){
        switch(s->matrix_ch[out_i][0]){
505
        case 0:
506 507
            if(mustcopy)
                memset(out->ch[out_i], 0, len * av_get_bytes_per_sample(s->int_sample_fmt));
508
            break;
Michael Niedermayer's avatar
Michael Niedermayer committed
509 510
        case 1:
            in_i= s->matrix_ch[out_i][1];
511
            if(s->matrix[out_i][in_i]!=1.0){
512
                if(s->mix_1_1_simd && len1)
513
                    s->mix_1_1_simd(out->ch[out_i]    , in->ch[in_i]    , s->native_simd_matrix, in->ch_count*out_i + in_i, len1);
514 515
                if(len != len1)
                    s->mix_1_1_f   (out->ch[out_i]+off, in->ch[in_i]+off, s->native_matrix, in->ch_count*out_i + in_i, len-len1);
516 517
            }else if(mustcopy){
                memcpy(out->ch[out_i], in->ch[in_i], len*out->bps);
Michael Niedermayer's avatar
Michael Niedermayer committed
518 519 520 521
            }else{
                out->ch[out_i]= in->ch[in_i];
            }
            break;
522 523 524
        case 2: {
            int in_i1 = s->matrix_ch[out_i][1];
            int in_i2 = s->matrix_ch[out_i][2];
525
            if(s->mix_2_1_simd && len1)
526
                s->mix_2_1_simd(out->ch[out_i]    , in->ch[in_i1]    , in->ch[in_i2]    , s->native_simd_matrix, in->ch_count*out_i + in_i1, in->ch_count*out_i + in_i2, len1);
527 528 529 530
            else
                s->mix_2_1_f   (out->ch[out_i]    , in->ch[in_i1]    , in->ch[in_i2]    , s->native_matrix, in->ch_count*out_i + in_i1, in->ch_count*out_i + in_i2, len1);
            if(len != len1)
                s->mix_2_1_f   (out->ch[out_i]+off, in->ch[in_i1]+off, in->ch[in_i2]+off, s->native_matrix, in->ch_count*out_i + in_i1, in->ch_count*out_i + in_i2, len-len1);
531
            break;}
Michael Niedermayer's avatar
Michael Niedermayer committed
532
        default:
533
            if(s->int_sample_fmt == AV_SAMPLE_FMT_FLTP){
Michael Niedermayer's avatar
Michael Niedermayer committed
534 535 536 537
                for(i=0; i<len; i++){
                    float v=0;
                    for(j=0; j<s->matrix_ch[out_i][0]; j++){
                        in_i= s->matrix_ch[out_i][1+j];
538
                        v+= ((float*)in->ch[in_i])[i] * s->matrix_flt[out_i][in_i];
Michael Niedermayer's avatar
Michael Niedermayer committed
539 540 541
                    }
                    ((float*)out->ch[out_i])[i]= v;
                }
542 543 544 545 546 547 548 549 550
            }else if(s->int_sample_fmt == AV_SAMPLE_FMT_DBLP){
                for(i=0; i<len; i++){
                    double v=0;
                    for(j=0; j<s->matrix_ch[out_i][0]; j++){
                        in_i= s->matrix_ch[out_i][1+j];
                        v+= ((double*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
                    }
                    ((double*)out->ch[out_i])[i]= v;
                }
Michael Niedermayer's avatar
Michael Niedermayer committed
551 552 553 554 555
            }else{
                for(i=0; i<len; i++){
                    int v=0;
                    for(j=0; j<s->matrix_ch[out_i][0]; j++){
                        in_i= s->matrix_ch[out_i][1+j];
556
                        v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
Michael Niedermayer's avatar
Michael Niedermayer committed
557
                    }
558
                    ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;
Michael Niedermayer's avatar
Michael Niedermayer committed
559 560 561 562 563 564
                }
            }
        }
    }
    return 0;
}