rematrix.c 19.3 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
#undef TEMPLATE_REMATRIX_S16
Michael Niedermayer's avatar
Michael Niedermayer committed
36

37 38 39 40
#define TEMPLATE_REMATRIX_S32
#include "rematrix_template.c"
#undef TEMPLATE_REMATRIX_S32

Michael Niedermayer's avatar
Michael Niedermayer committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#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

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
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));
    nb_in  = av_get_channel_layout_nb_channels(s->in_ch_layout);
    nb_out = av_get_channel_layout_nb_channels(s->out_ch_layout);
    for (out = 0; out < nb_out; out++) {
        for (in = 0; in < nb_in; in++)
            s->matrix[out][in] = matrix[in];
        matrix += stride;
    }
    s->rematrix_custom = 1;
    return 0;
}

Michael Niedermayer's avatar
Michael Niedermayer committed
78 79 80 81 82 83
static int even(int64_t layout){
    if(!layout) return 1;
    if(layout&(layout-1)) return 1;
    return 0;
}

84
static int clean_layout(SwrContext *s, int64_t layout){
85 86 87 88 89 90 91
    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;
    }

92 93 94
    return layout;
}

Michael Niedermayer's avatar
Michael Niedermayer committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
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;
}

112
av_cold static int auto_matrix(SwrContext *s)
113
{
114
    int i, j, out_i;
115
    double matrix[64][64]={{0}};
116
    int64_t unaccounted, in_ch_layout, out_ch_layout;
117
    double maxcoef=0;
118
    char buf[128];
119
    const int matrix_encoding = s->matrix_encoding;
120
    float maxval;
Michael Niedermayer's avatar
Michael Niedermayer committed
121

122
    in_ch_layout = clean_layout(s, s->in_ch_layout);
123 124
    out_ch_layout = clean_layout(s, s->out_ch_layout);

125 126 127 128 129
    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;

130
    if(!sane_layout(in_ch_layout)){
131 132
        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);
Michael Niedermayer's avatar
Michael Niedermayer committed
133 134
        return AVERROR(EINVAL);
    }
135

136
    if(!sane_layout(out_ch_layout)){
137 138
        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);
Michael Niedermayer's avatar
Michael Niedermayer committed
139 140 141
        return AVERROR(EINVAL);
    }

142 143
    memset(s->matrix, 0, sizeof(s->matrix));
    for(i=0; i<64; i++){
144
        if(in_ch_layout & out_ch_layout & (1ULL<<i))
145 146 147 148 149
            matrix[i][i]= 1.0;
    }

    unaccounted= in_ch_layout & ~out_ch_layout;

Michael Niedermayer's avatar
Michael Niedermayer committed
150 151 152 153 154
//FIXME implement dolby surround
//FIXME implement full ac3


    if(unaccounted & AV_CH_FRONT_CENTER){
155 156
        if((out_ch_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO){
            if(in_ch_layout & AV_CH_LAYOUT_STEREO) {
157 158 159 160 161 162
                matrix[ FRONT_LEFT][FRONT_CENTER]+= s->clev;
                matrix[FRONT_RIGHT][FRONT_CENTER]+= s->clev;
            } else {
                matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
                matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
163 164 165 166
        }else
            av_assert0(0);
    }
    if(unaccounted & AV_CH_LAYOUT_STEREO){
167
        if(out_ch_layout & AV_CH_FRONT_CENTER){
168 169
            matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
            matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
170
            if(in_ch_layout & AV_CH_FRONT_CENTER)
Michael Niedermayer's avatar
Michael Niedermayer committed
171 172 173 174 175 176
                matrix[FRONT_CENTER][ FRONT_CENTER] = s->clev*sqrt(2);
        }else
            av_assert0(0);
    }

    if(unaccounted & AV_CH_BACK_CENTER){
177
        if(out_ch_layout & AV_CH_BACK_LEFT){
178 179
            matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
            matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
180
        }else if(out_ch_layout & AV_CH_SIDE_LEFT){
181 182
            matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
            matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
183
        }else if(out_ch_layout & AV_CH_FRONT_LEFT){
184 185 186 187 188 189 190 191 192 193 194 195 196
            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;
                } else {
                    matrix[FRONT_LEFT ][BACK_CENTER] -= s->slev;
                    matrix[FRONT_RIGHT][BACK_CENTER] += s->slev;
                }
            } else {
                matrix[ FRONT_LEFT][BACK_CENTER]+= s->slev*M_SQRT1_2;
                matrix[FRONT_RIGHT][BACK_CENTER]+= s->slev*M_SQRT1_2;
            }
197
        }else if(out_ch_layout & AV_CH_FRONT_CENTER){
198
            matrix[ FRONT_CENTER][BACK_CENTER]+= s->slev*M_SQRT1_2;
Michael Niedermayer's avatar
Michael Niedermayer committed
199 200 201 202
        }else
            av_assert0(0);
    }
    if(unaccounted & AV_CH_BACK_LEFT){
203
        if(out_ch_layout & AV_CH_BACK_CENTER){
204 205
            matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
            matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
206 207
        }else if(out_ch_layout & AV_CH_SIDE_LEFT){
            if(in_ch_layout & AV_CH_SIDE_LEFT){
208 209
                matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
                matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
Michael Niedermayer's avatar
Michael Niedermayer committed
210 211 212 213
            }else{
            matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
            matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
            }
214
        }else if(out_ch_layout & AV_CH_FRONT_LEFT){
215 216 217 218 219 220 221 222 223 224 225 226 227 228
            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;
            } 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;
            } else {
                matrix[ FRONT_LEFT][ BACK_LEFT] += s->slev;
                matrix[FRONT_RIGHT][BACK_RIGHT] += s->slev;
            }
229
        }else if(out_ch_layout & AV_CH_FRONT_CENTER){
230 231
            matrix[ FRONT_CENTER][BACK_LEFT ]+= s->slev*M_SQRT1_2;
            matrix[ FRONT_CENTER][BACK_RIGHT]+= s->slev*M_SQRT1_2;
Michael Niedermayer's avatar
Michael Niedermayer committed
232 233 234 235 236
        }else
            av_assert0(0);
    }

    if(unaccounted & AV_CH_SIDE_LEFT){
237
        if(out_ch_layout & AV_CH_BACK_LEFT){
238 239
            /* if back channels do not exist in the input, just copy side
               channels to back channels, otherwise mix side into back */
240
            if (in_ch_layout & AV_CH_BACK_LEFT) {
241 242 243 244 245 246
                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;
            }
247
        }else if(out_ch_layout & AV_CH_BACK_CENTER){
248 249
            matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
            matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
250
        }else if(out_ch_layout & AV_CH_FRONT_LEFT){
251 252 253 254 255 256 257 258 259 260 261 262 263 264
            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;
            } 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;
            } else {
                matrix[ FRONT_LEFT][ SIDE_LEFT] += s->slev;
                matrix[FRONT_RIGHT][SIDE_RIGHT] += s->slev;
            }
265
        }else if(out_ch_layout & AV_CH_FRONT_CENTER){
266 267
            matrix[ FRONT_CENTER][SIDE_LEFT ]+= s->slev*M_SQRT1_2;
            matrix[ FRONT_CENTER][SIDE_RIGHT]+= s->slev*M_SQRT1_2;
Michael Niedermayer's avatar
Michael Niedermayer committed
268 269 270 271 272
        }else
            av_assert0(0);
    }

    if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
273
        if(out_ch_layout & AV_CH_FRONT_LEFT){
Michael Niedermayer's avatar
Michael Niedermayer committed
274 275
            matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
            matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
276
        }else if(out_ch_layout & AV_CH_FRONT_CENTER){
277 278
            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
279 280 281
        }else
            av_assert0(0);
    }
Justin Ruggles's avatar
Justin Ruggles committed
282 283
    /* mix LFE into front left/right or center */
    if (unaccounted & AV_CH_LOW_FREQUENCY) {
284
        if (out_ch_layout & AV_CH_FRONT_CENTER) {
Justin Ruggles's avatar
Justin Ruggles committed
285
            matrix[FRONT_CENTER][LOW_FREQUENCY] += s->lfe_mix_level;
286
        } else if (out_ch_layout & AV_CH_FRONT_LEFT) {
Justin Ruggles's avatar
Justin Ruggles committed
287 288 289 290 291 292
            matrix[FRONT_LEFT ][LOW_FREQUENCY] += s->lfe_mix_level * M_SQRT1_2;
            matrix[FRONT_RIGHT][LOW_FREQUENCY] += s->lfe_mix_level * M_SQRT1_2;
        } else
            av_assert0(0);
    }

Michael Niedermayer's avatar
Michael Niedermayer committed
293 294 295 296 297 298 299 300
    for(out_i=i=0; i<64; i++){
        double sum=0;
        int in_i=0;
        for(j=0; j<64; j++){
            s->matrix[out_i][in_i]= matrix[i][j];
            if(matrix[i][j]){
                sum += fabs(matrix[i][j]);
            }
301
            if(in_ch_layout & (1ULL<<j))
Michael Niedermayer's avatar
Michael Niedermayer committed
302 303 304
                in_i++;
        }
        maxcoef= FFMAX(maxcoef, sum);
305
        if(out_ch_layout & (1ULL<<i))
Michael Niedermayer's avatar
Michael Niedermayer committed
306 307
            out_i++;
    }
308 309 310
    if(s->rematrix_volume  < 0)
        maxcoef = -s->rematrix_volume;

311 312 313 314 315 316 317 318
    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;

319
    if(maxcoef > maxval || s->rematrix_volume  < 0){
320
        maxcoef /= maxval;
Michael Niedermayer's avatar
Michael Niedermayer committed
321
        for(i=0; i<SWR_CH_MAX; i++)
322
            for(j=0; j<SWR_CH_MAX; j++){
Michael Niedermayer's avatar
Michael Niedermayer committed
323
                s->matrix[i][j] /= maxcoef;
324
            }
Michael Niedermayer's avatar
Michael Niedermayer committed
325
    }
326 327 328 329 330 331 332 333

    if(s->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;
            }
    }

334 335
    for(i=0; i<av_get_channel_layout_nb_channels(out_ch_layout); i++){
        for(j=0; j<av_get_channel_layout_nb_channels(in_ch_layout); j++){
336
            av_log(NULL, AV_LOG_DEBUG, "%f ", s->matrix[i][j]);
Michael Niedermayer's avatar
Michael Niedermayer committed
337
        }
338 339
        av_log(NULL, AV_LOG_DEBUG, "\n");
    }
Michael Niedermayer's avatar
Michael Niedermayer committed
340 341 342
    return 0;
}

343
av_cold int swri_rematrix_init(SwrContext *s){
344
    int i, j;
345 346
    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);
347

348 349
    s->mix_any_f = NULL;

350 351 352 353 354
    if (!s->rematrix_custom) {
        int r = auto_matrix(s);
        if (r)
            return r;
    }
355
    if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
356
        s->native_matrix = av_calloc(nb_in * nb_out, sizeof(int));
357 358 359 360 361
        s->native_one    = av_mallocz(sizeof(int));
        for (i = 0; i < nb_out; i++)
            for (j = 0; j < nb_in; j++)
                ((int*)s->native_matrix)[i * nb_in + j] = lrintf(s->matrix[i][j] * 32768);
        *((int*)s->native_one) = 32768;
362 363 364
        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);
365
    }else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
366
        s->native_matrix = av_calloc(nb_in * nb_out, sizeof(float));
367 368 369 370 371
        s->native_one    = av_mallocz(sizeof(float));
        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;
372 373 374
        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);
375
    }else if(s->midbuf.fmt == AV_SAMPLE_FMT_DBLP){
376
        s->native_matrix = av_calloc(nb_in * nb_out, sizeof(double));
377 378 379 380 381
        s->native_one    = av_mallocz(sizeof(double));
        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;
382 383 384
        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);
385 386 387 388 389 390 391 392 393 394 395
    }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));
//         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);
396 397
    }else
        av_assert0(0);
398 399 400 401 402 403 404 405 406 407
    //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;
    }
408 409 410

    if(HAVE_YASM && HAVE_MMX) swri_rematrix_init_x86(s);

411 412 413
    return 0;
}

414
av_cold void swri_rematrix_free(SwrContext *s){
415 416
    av_freep(&s->native_matrix);
    av_freep(&s->native_one);
417
    av_freep(&s->native_simd_matrix);
418
    av_freep(&s->native_simd_one);
419 420
}

421
int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
Michael Niedermayer's avatar
Michael Niedermayer committed
422
    int out_i, in_i, i, j;
423 424
    int len1 = 0;
    int off = 0;
Michael Niedermayer's avatar
Michael Niedermayer committed
425

426
    if(s->mix_any_f) {
427
        s->mix_any_f(out->ch, (const uint8_t **)in->ch, s->native_matrix, len);
428 429 430
        return 0;
    }

431 432 433 434 435
    if(s->mix_2_1_simd || s->mix_1_1_simd){
        len1= len&~15;
        off = len1 * out->bps;
    }

436 437
    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
438 439 440

    for(out_i=0; out_i<out->ch_count; out_i++){
        switch(s->matrix_ch[out_i][0]){
441
        case 0:
442 443
            if(mustcopy)
                memset(out->ch[out_i], 0, len * av_get_bytes_per_sample(s->int_sample_fmt));
444
            break;
Michael Niedermayer's avatar
Michael Niedermayer committed
445 446
        case 1:
            in_i= s->matrix_ch[out_i][1];
447
            if(s->matrix[out_i][in_i]!=1.0){
448
                if(s->mix_1_1_simd && len1)
449
                    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);
450 451
                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);
452 453
            }else if(mustcopy){
                memcpy(out->ch[out_i], in->ch[in_i], len*out->bps);
Michael Niedermayer's avatar
Michael Niedermayer committed
454 455 456 457
            }else{
                out->ch[out_i]= in->ch[in_i];
            }
            break;
458 459 460
        case 2: {
            int in_i1 = s->matrix_ch[out_i][1];
            int in_i2 = s->matrix_ch[out_i][2];
461
            if(s->mix_2_1_simd && len1)
462
                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);
463 464 465 466
            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);
467
            break;}
Michael Niedermayer's avatar
Michael Niedermayer committed
468
        default:
469
            if(s->int_sample_fmt == AV_SAMPLE_FMT_FLTP){
Michael Niedermayer's avatar
Michael Niedermayer committed
470 471 472 473 474 475 476 477
                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];
                        v+= ((float*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
                    }
                    ((float*)out->ch[out_i])[i]= v;
                }
478 479 480 481 482 483 484 485 486
            }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
487 488 489 490 491
            }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];
492
                        v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
Michael Niedermayer's avatar
Michael Niedermayer committed
493
                    }
494
                    ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;
Michael Niedermayer's avatar
Michael Niedermayer committed
495 496 497 498 499 500
                }
            }
        }
    }
    return 0;
}