]> git.sesse.net Git - ffmpeg/blob - libswresample/rematrix.c
swresample: Use double and float for matrixes for best quality and speed
[ffmpeg] / libswresample / rematrix.c
1 /*
2  * Copyright (C) 2011-2012 Michael Niedermayer (michaelni@gmx.at)
3  *
4  * This file is part of libswresample
5  *
6  * libswresample is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * libswresample is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with libswresample; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "swresample_internal.h"
22 #include "libavutil/avassert.h"
23 #include "libavutil/channel_layout.h"
24
25 #define TEMPLATE_REMATRIX_FLT
26 #include "rematrix_template.c"
27 #undef TEMPLATE_REMATRIX_FLT
28
29 #define TEMPLATE_REMATRIX_DBL
30 #include "rematrix_template.c"
31 #undef TEMPLATE_REMATRIX_DBL
32
33 #define TEMPLATE_REMATRIX_S16
34 #include "rematrix_template.c"
35 #define TEMPLATE_CLIP
36 #include "rematrix_template.c"
37 #undef TEMPLATE_CLIP
38 #undef TEMPLATE_REMATRIX_S16
39
40 #define TEMPLATE_REMATRIX_S32
41 #include "rematrix_template.c"
42 #undef TEMPLATE_REMATRIX_S32
43
44 #define FRONT_LEFT             0
45 #define FRONT_RIGHT            1
46 #define FRONT_CENTER           2
47 #define LOW_FREQUENCY          3
48 #define BACK_LEFT              4
49 #define BACK_RIGHT             5
50 #define FRONT_LEFT_OF_CENTER   6
51 #define FRONT_RIGHT_OF_CENTER  7
52 #define BACK_CENTER            8
53 #define SIDE_LEFT              9
54 #define SIDE_RIGHT             10
55 #define TOP_CENTER             11
56 #define TOP_FRONT_LEFT         12
57 #define TOP_FRONT_CENTER       13
58 #define TOP_FRONT_RIGHT        14
59 #define TOP_BACK_LEFT          15
60 #define TOP_BACK_CENTER        16
61 #define TOP_BACK_RIGHT         17
62 #define NUM_NAMED_CHANNELS     18
63
64 int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride)
65 {
66     int nb_in, nb_out, in, out;
67
68     if (!s || s->in_convert) // s needs to be allocated but not initialized
69         return AVERROR(EINVAL);
70     memset(s->matrix, 0, sizeof(s->matrix));
71     nb_in  = av_get_channel_layout_nb_channels(s->user_in_ch_layout);
72     nb_out = av_get_channel_layout_nb_channels(s->user_out_ch_layout);
73     for (out = 0; out < nb_out; out++) {
74         for (in = 0; in < nb_in; in++)
75             s->matrix[out][in] = matrix[in];
76         if (s->int_sample_fmt == AV_SAMPLE_FMT_FLTP)
77             for (in = 0; in < nb_in; in++)
78                 s->matrix_flt[out][in] = matrix[in];
79         matrix += stride;
80     }
81     s->rematrix_custom = 1;
82     return 0;
83 }
84
85 static int even(int64_t layout){
86     if(!layout) return 1;
87     if(layout&(layout-1)) return 1;
88     return 0;
89 }
90
91 static int clean_layout(SwrContext *s, int64_t layout){
92     if(layout && layout != AV_CH_FRONT_CENTER && !(layout&(layout-1))) {
93         char buf[128];
94         av_get_channel_layout_string(buf, sizeof(buf), -1, layout);
95         av_log(s, AV_LOG_VERBOSE, "Treating %s as mono\n", buf);
96         return AV_CH_FRONT_CENTER;
97     }
98
99     return layout;
100 }
101
102 static int sane_layout(int64_t layout){
103     if(!(layout & AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
104         return 0;
105     if(!even(layout & (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT))) // no asymetric front
106         return 0;
107     if(!even(layout & (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)))   // no asymetric side
108         return 0;
109     if(!even(layout & (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)))
110         return 0;
111     if(!even(layout & (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)))
112         return 0;
113     if(av_get_channel_layout_nb_channels(layout) >= SWR_CH_MAX)
114         return 0;
115
116     return 1;
117 }
118
119 av_cold static int auto_matrix(SwrContext *s)
120 {
121     int i, j, out_i;
122     double matrix[NUM_NAMED_CHANNELS][NUM_NAMED_CHANNELS]={{0}};
123     int64_t unaccounted, in_ch_layout, out_ch_layout;
124     double maxcoef=0;
125     char buf[128];
126     const int matrix_encoding = s->matrix_encoding;
127     float maxval;
128
129     in_ch_layout = clean_layout(s, s->in_ch_layout);
130     out_ch_layout = clean_layout(s, s->out_ch_layout);
131
132     if(   out_ch_layout == AV_CH_LAYOUT_STEREO_DOWNMIX
133        && (in_ch_layout & AV_CH_LAYOUT_STEREO_DOWNMIX) == 0
134     )
135         out_ch_layout = AV_CH_LAYOUT_STEREO;
136
137     if(    in_ch_layout == AV_CH_LAYOUT_STEREO_DOWNMIX
138        && (out_ch_layout & AV_CH_LAYOUT_STEREO_DOWNMIX) == 0
139     )
140         in_ch_layout = AV_CH_LAYOUT_STEREO;
141
142     if(!sane_layout(in_ch_layout)){
143         av_get_channel_layout_string(buf, sizeof(buf), -1, s->in_ch_layout);
144         av_log(s, AV_LOG_ERROR, "Input channel layout '%s' is not supported\n", buf);
145         return AVERROR(EINVAL);
146     }
147
148     if(!sane_layout(out_ch_layout)){
149         av_get_channel_layout_string(buf, sizeof(buf), -1, s->out_ch_layout);
150         av_log(s, AV_LOG_ERROR, "Output channel layout '%s' is not supported\n", buf);
151         return AVERROR(EINVAL);
152     }
153
154     memset(s->matrix, 0, sizeof(s->matrix));
155     for(i=0; i<FF_ARRAY_ELEMS(matrix); i++){
156         if(in_ch_layout & out_ch_layout & (1ULL<<i))
157             matrix[i][i]= 1.0;
158     }
159
160     unaccounted= in_ch_layout & ~out_ch_layout;
161
162 //FIXME implement dolby surround
163 //FIXME implement full ac3
164
165
166     if(unaccounted & AV_CH_FRONT_CENTER){
167         if((out_ch_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO){
168             if(in_ch_layout & AV_CH_LAYOUT_STEREO) {
169                 matrix[ FRONT_LEFT][FRONT_CENTER]+= s->clev;
170                 matrix[FRONT_RIGHT][FRONT_CENTER]+= s->clev;
171             } else {
172                 matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
173                 matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
174             }
175         }else
176             av_assert0(0);
177     }
178     if(unaccounted & AV_CH_LAYOUT_STEREO){
179         if(out_ch_layout & AV_CH_FRONT_CENTER){
180             matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
181             matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
182             if(in_ch_layout & AV_CH_FRONT_CENTER)
183                 matrix[FRONT_CENTER][ FRONT_CENTER] = s->clev*sqrt(2);
184         }else
185             av_assert0(0);
186     }
187
188     if(unaccounted & AV_CH_BACK_CENTER){
189         if(out_ch_layout & AV_CH_BACK_LEFT){
190             matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
191             matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
192         }else if(out_ch_layout & AV_CH_SIDE_LEFT){
193             matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
194             matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
195         }else if(out_ch_layout & AV_CH_FRONT_LEFT){
196             if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY ||
197                 matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
198                 if (unaccounted & (AV_CH_BACK_LEFT | AV_CH_SIDE_LEFT)) {
199                     matrix[FRONT_LEFT ][BACK_CENTER] -= s->slev * M_SQRT1_2;
200                     matrix[FRONT_RIGHT][BACK_CENTER] += s->slev * M_SQRT1_2;
201                 } else {
202                     matrix[FRONT_LEFT ][BACK_CENTER] -= s->slev;
203                     matrix[FRONT_RIGHT][BACK_CENTER] += s->slev;
204                 }
205             } else {
206                 matrix[ FRONT_LEFT][BACK_CENTER]+= s->slev*M_SQRT1_2;
207                 matrix[FRONT_RIGHT][BACK_CENTER]+= s->slev*M_SQRT1_2;
208             }
209         }else if(out_ch_layout & AV_CH_FRONT_CENTER){
210             matrix[ FRONT_CENTER][BACK_CENTER]+= s->slev*M_SQRT1_2;
211         }else
212             av_assert0(0);
213     }
214     if(unaccounted & AV_CH_BACK_LEFT){
215         if(out_ch_layout & AV_CH_BACK_CENTER){
216             matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
217             matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
218         }else if(out_ch_layout & AV_CH_SIDE_LEFT){
219             if(in_ch_layout & AV_CH_SIDE_LEFT){
220                 matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
221                 matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
222             }else{
223             matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
224             matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
225             }
226         }else if(out_ch_layout & AV_CH_FRONT_LEFT){
227             if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
228                 matrix[FRONT_LEFT ][BACK_LEFT ] -= s->slev * M_SQRT1_2;
229                 matrix[FRONT_LEFT ][BACK_RIGHT] -= s->slev * M_SQRT1_2;
230                 matrix[FRONT_RIGHT][BACK_LEFT ] += s->slev * M_SQRT1_2;
231                 matrix[FRONT_RIGHT][BACK_RIGHT] += s->slev * M_SQRT1_2;
232             } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
233                 matrix[FRONT_LEFT ][BACK_LEFT ] -= s->slev * SQRT3_2;
234                 matrix[FRONT_LEFT ][BACK_RIGHT] -= s->slev * M_SQRT1_2;
235                 matrix[FRONT_RIGHT][BACK_LEFT ] += s->slev * M_SQRT1_2;
236                 matrix[FRONT_RIGHT][BACK_RIGHT] += s->slev * SQRT3_2;
237             } else {
238                 matrix[ FRONT_LEFT][ BACK_LEFT] += s->slev;
239                 matrix[FRONT_RIGHT][BACK_RIGHT] += s->slev;
240             }
241         }else if(out_ch_layout & AV_CH_FRONT_CENTER){
242             matrix[ FRONT_CENTER][BACK_LEFT ]+= s->slev*M_SQRT1_2;
243             matrix[ FRONT_CENTER][BACK_RIGHT]+= s->slev*M_SQRT1_2;
244         }else
245             av_assert0(0);
246     }
247
248     if(unaccounted & AV_CH_SIDE_LEFT){
249         if(out_ch_layout & AV_CH_BACK_LEFT){
250             /* if back channels do not exist in the input, just copy side
251                channels to back channels, otherwise mix side into back */
252             if (in_ch_layout & AV_CH_BACK_LEFT) {
253                 matrix[BACK_LEFT ][SIDE_LEFT ] += M_SQRT1_2;
254                 matrix[BACK_RIGHT][SIDE_RIGHT] += M_SQRT1_2;
255             } else {
256                 matrix[BACK_LEFT ][SIDE_LEFT ] += 1.0;
257                 matrix[BACK_RIGHT][SIDE_RIGHT] += 1.0;
258             }
259         }else if(out_ch_layout & AV_CH_BACK_CENTER){
260             matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
261             matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
262         }else if(out_ch_layout & AV_CH_FRONT_LEFT){
263             if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
264                 matrix[FRONT_LEFT ][SIDE_LEFT ] -= s->slev * M_SQRT1_2;
265                 matrix[FRONT_LEFT ][SIDE_RIGHT] -= s->slev * M_SQRT1_2;
266                 matrix[FRONT_RIGHT][SIDE_LEFT ] += s->slev * M_SQRT1_2;
267                 matrix[FRONT_RIGHT][SIDE_RIGHT] += s->slev * M_SQRT1_2;
268             } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
269                 matrix[FRONT_LEFT ][SIDE_LEFT ] -= s->slev * SQRT3_2;
270                 matrix[FRONT_LEFT ][SIDE_RIGHT] -= s->slev * M_SQRT1_2;
271                 matrix[FRONT_RIGHT][SIDE_LEFT ] += s->slev * M_SQRT1_2;
272                 matrix[FRONT_RIGHT][SIDE_RIGHT] += s->slev * SQRT3_2;
273             } else {
274                 matrix[ FRONT_LEFT][ SIDE_LEFT] += s->slev;
275                 matrix[FRONT_RIGHT][SIDE_RIGHT] += s->slev;
276             }
277         }else if(out_ch_layout & AV_CH_FRONT_CENTER){
278             matrix[ FRONT_CENTER][SIDE_LEFT ]+= s->slev*M_SQRT1_2;
279             matrix[ FRONT_CENTER][SIDE_RIGHT]+= s->slev*M_SQRT1_2;
280         }else
281             av_assert0(0);
282     }
283
284     if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
285         if(out_ch_layout & AV_CH_FRONT_LEFT){
286             matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
287             matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
288         }else if(out_ch_layout & AV_CH_FRONT_CENTER){
289             matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
290             matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
291         }else
292             av_assert0(0);
293     }
294     /* mix LFE into front left/right or center */
295     if (unaccounted & AV_CH_LOW_FREQUENCY) {
296         if (out_ch_layout & AV_CH_FRONT_CENTER) {
297             matrix[FRONT_CENTER][LOW_FREQUENCY] += s->lfe_mix_level;
298         } else if (out_ch_layout & AV_CH_FRONT_LEFT) {
299             matrix[FRONT_LEFT ][LOW_FREQUENCY] += s->lfe_mix_level * M_SQRT1_2;
300             matrix[FRONT_RIGHT][LOW_FREQUENCY] += s->lfe_mix_level * M_SQRT1_2;
301         } else
302             av_assert0(0);
303     }
304
305     for(out_i=i=0; i<64; i++){
306         double sum=0;
307         int in_i=0;
308         if((out_ch_layout & (1ULL<<i)) == 0)
309             continue;
310         for(j=0; j<64; j++){
311             if((in_ch_layout & (1ULL<<j)) == 0)
312                continue;
313             if (i < FF_ARRAY_ELEMS(matrix) && j < FF_ARRAY_ELEMS(matrix[0]))
314                 s->matrix[out_i][in_i]= matrix[i][j];
315             else
316                 s->matrix[out_i][in_i]= i == j && (in_ch_layout & out_ch_layout & (1ULL<<i));
317             sum += fabs(s->matrix[out_i][in_i]);
318             in_i++;
319         }
320         maxcoef= FFMAX(maxcoef, sum);
321         out_i++;
322     }
323     if(s->rematrix_volume  < 0)
324         maxcoef = -s->rematrix_volume;
325
326     if (s->rematrix_maxval > 0) {
327         maxval = s->rematrix_maxval;
328     } else if (   av_get_packed_sample_fmt(s->out_sample_fmt) < AV_SAMPLE_FMT_FLT
329                || av_get_packed_sample_fmt(s->int_sample_fmt) < AV_SAMPLE_FMT_FLT) {
330         maxval = 1.0;
331     } else
332         maxval = INT_MAX;
333
334     if(maxcoef > maxval || s->rematrix_volume  < 0){
335         maxcoef /= maxval;
336         for(i=0; i<SWR_CH_MAX; i++)
337             for(j=0; j<SWR_CH_MAX; j++){
338                 s->matrix[i][j] /= maxcoef;
339             }
340     }
341
342     if(s->rematrix_volume > 0){
343         for(i=0; i<SWR_CH_MAX; i++)
344             for(j=0; j<SWR_CH_MAX; j++){
345                 s->matrix[i][j] *= s->rematrix_volume;
346             }
347     }
348
349     av_log(s, AV_LOG_DEBUG, "Matrix coefficients:\n");
350     for(i=0; i<av_get_channel_layout_nb_channels(out_ch_layout); i++){
351         const char *c =
352             av_get_channel_name(av_channel_layout_extract_channel(out_ch_layout, i));
353         av_log(s, AV_LOG_DEBUG, "%s: ", c ? c : "?");
354         for(j=0; j<av_get_channel_layout_nb_channels(in_ch_layout); j++){
355             c = av_get_channel_name(av_channel_layout_extract_channel(in_ch_layout, j));
356             av_log(s, AV_LOG_DEBUG, "%s:%f ", c ? c : "?", s->matrix[i][j]);
357         }
358         av_log(s, AV_LOG_DEBUG, "\n");
359     }
360     if (s->int_sample_fmt == AV_SAMPLE_FMT_FLTP) {
361         int i;
362         for (i = 0; i < FF_ARRAY_ELEMS(s->matrix[0])*FF_ARRAY_ELEMS(s->matrix[0]); i++)
363             s->matrix_flt[0][i] = s->matrix[0][i];
364     }
365
366     return 0;
367 }
368
369 av_cold int swri_rematrix_init(SwrContext *s){
370     int i, j;
371     int nb_in  = av_get_channel_layout_nb_channels(s->in_ch_layout);
372     int nb_out = av_get_channel_layout_nb_channels(s->out_ch_layout);
373
374     s->mix_any_f = NULL;
375
376     if (!s->rematrix_custom) {
377         int r = auto_matrix(s);
378         if (r)
379             return r;
380     }
381     if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
382         int maxsum = 0;
383         s->native_matrix = av_calloc(nb_in * nb_out, sizeof(int));
384         s->native_one    = av_mallocz(sizeof(int));
385         if (!s->native_matrix || !s->native_one)
386             return AVERROR(ENOMEM);
387         for (i = 0; i < nb_out; i++) {
388             double rem = 0;
389             int sum = 0;
390
391             for (j = 0; j < nb_in; j++) {
392                 double target = s->matrix[i][j] * 32768 + rem;
393                 ((int*)s->native_matrix)[i * nb_in + j] = lrintf(target);
394                 rem += target - ((int*)s->native_matrix)[i * nb_in + j];
395                 sum += FFABS(((int*)s->native_matrix)[i * nb_in + j]);
396             }
397             maxsum = FFMAX(maxsum, sum);
398         }
399         *((int*)s->native_one) = 32768;
400         if (maxsum <= 32768) {
401             s->mix_1_1_f = (mix_1_1_func_type*)copy_s16;
402             s->mix_2_1_f = (mix_2_1_func_type*)sum2_s16;
403             s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s16(s);
404         } else {
405             s->mix_1_1_f = (mix_1_1_func_type*)copy_clip_s16;
406             s->mix_2_1_f = (mix_2_1_func_type*)sum2_clip_s16;
407             s->mix_any_f = (mix_any_func_type*)get_mix_any_func_clip_s16(s);
408         }
409     }else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
410         s->native_matrix = av_calloc(nb_in * nb_out, sizeof(float));
411         s->native_one    = av_mallocz(sizeof(float));
412         if (!s->native_matrix || !s->native_one)
413             return AVERROR(ENOMEM);
414         for (i = 0; i < nb_out; i++)
415             for (j = 0; j < nb_in; j++)
416                 ((float*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
417         *((float*)s->native_one) = 1.0;
418         s->mix_1_1_f = (mix_1_1_func_type*)copy_float;
419         s->mix_2_1_f = (mix_2_1_func_type*)sum2_float;
420         s->mix_any_f = (mix_any_func_type*)get_mix_any_func_float(s);
421     }else if(s->midbuf.fmt == AV_SAMPLE_FMT_DBLP){
422         s->native_matrix = av_calloc(nb_in * nb_out, sizeof(double));
423         s->native_one    = av_mallocz(sizeof(double));
424         if (!s->native_matrix || !s->native_one)
425             return AVERROR(ENOMEM);
426         for (i = 0; i < nb_out; i++)
427             for (j = 0; j < nb_in; j++)
428                 ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
429         *((double*)s->native_one) = 1.0;
430         s->mix_1_1_f = (mix_1_1_func_type*)copy_double;
431         s->mix_2_1_f = (mix_2_1_func_type*)sum2_double;
432         s->mix_any_f = (mix_any_func_type*)get_mix_any_func_double(s);
433     }else if(s->midbuf.fmt == AV_SAMPLE_FMT_S32P){
434         // Only for dithering currently
435 //         s->native_matrix = av_calloc(nb_in * nb_out, sizeof(double));
436         s->native_one    = av_mallocz(sizeof(int));
437         if (!s->native_one)
438             return AVERROR(ENOMEM);
439 //         for (i = 0; i < nb_out; i++)
440 //             for (j = 0; j < nb_in; j++)
441 //                 ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
442         *((int*)s->native_one) = 32768;
443         s->mix_1_1_f = (mix_1_1_func_type*)copy_s32;
444         s->mix_2_1_f = (mix_2_1_func_type*)sum2_s32;
445         s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s32(s);
446     }else
447         av_assert0(0);
448     //FIXME quantize for integeres
449     for (i = 0; i < SWR_CH_MAX; i++) {
450         int ch_in=0;
451         for (j = 0; j < SWR_CH_MAX; j++) {
452             s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
453             if(s->matrix[i][j])
454                 s->matrix_ch[i][++ch_in]= j;
455         }
456         s->matrix_ch[i][0]= ch_in;
457     }
458
459     if(HAVE_YASM && HAVE_MMX)
460         return swri_rematrix_init_x86(s);
461
462     return 0;
463 }
464
465 av_cold void swri_rematrix_free(SwrContext *s){
466     av_freep(&s->native_matrix);
467     av_freep(&s->native_one);
468     av_freep(&s->native_simd_matrix);
469     av_freep(&s->native_simd_one);
470 }
471
472 int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
473     int out_i, in_i, i, j;
474     int len1 = 0;
475     int off = 0;
476
477     if(s->mix_any_f) {
478         s->mix_any_f(out->ch, (const uint8_t **)in->ch, s->native_matrix, len);
479         return 0;
480     }
481
482     if(s->mix_2_1_simd || s->mix_1_1_simd){
483         len1= len&~15;
484         off = len1 * out->bps;
485     }
486
487     av_assert0(!s->out_ch_layout || out->ch_count == av_get_channel_layout_nb_channels(s->out_ch_layout));
488     av_assert0(!s-> in_ch_layout || in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
489
490     for(out_i=0; out_i<out->ch_count; out_i++){
491         switch(s->matrix_ch[out_i][0]){
492         case 0:
493             if(mustcopy)
494                 memset(out->ch[out_i], 0, len * av_get_bytes_per_sample(s->int_sample_fmt));
495             break;
496         case 1:
497             in_i= s->matrix_ch[out_i][1];
498             if(s->matrix[out_i][in_i]!=1.0){
499                 if(s->mix_1_1_simd && len1)
500                     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);
501                 if(len != len1)
502                     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);
503             }else if(mustcopy){
504                 memcpy(out->ch[out_i], in->ch[in_i], len*out->bps);
505             }else{
506                 out->ch[out_i]= in->ch[in_i];
507             }
508             break;
509         case 2: {
510             int in_i1 = s->matrix_ch[out_i][1];
511             int in_i2 = s->matrix_ch[out_i][2];
512             if(s->mix_2_1_simd && len1)
513                 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);
514             else
515                 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);
516             if(len != len1)
517                 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);
518             break;}
519         default:
520             if(s->int_sample_fmt == AV_SAMPLE_FMT_FLTP){
521                 for(i=0; i<len; i++){
522                     float v=0;
523                     for(j=0; j<s->matrix_ch[out_i][0]; j++){
524                         in_i= s->matrix_ch[out_i][1+j];
525                         v+= ((float*)in->ch[in_i])[i] * s->matrix_flt[out_i][in_i];
526                     }
527                     ((float*)out->ch[out_i])[i]= v;
528                 }
529             }else if(s->int_sample_fmt == AV_SAMPLE_FMT_DBLP){
530                 for(i=0; i<len; i++){
531                     double v=0;
532                     for(j=0; j<s->matrix_ch[out_i][0]; j++){
533                         in_i= s->matrix_ch[out_i][1+j];
534                         v+= ((double*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
535                     }
536                     ((double*)out->ch[out_i])[i]= v;
537                 }
538             }else{
539                 for(i=0; i<len; i++){
540                     int v=0;
541                     for(j=0; j<s->matrix_ch[out_i][0]; j++){
542                         in_i= s->matrix_ch[out_i][1+j];
543                         v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
544                     }
545                     ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;
546                 }
547             }
548         }
549     }
550     return 0;
551 }