]> git.sesse.net Git - ffmpeg/blob - libswresample/rematrix.c
swresample/resample: do not increase phase_count on exact_rational
[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         matrix += stride;
77     }
78     s->rematrix_custom = 1;
79     return 0;
80 }
81
82 static int even(int64_t layout){
83     if(!layout) return 1;
84     if(layout&(layout-1)) return 1;
85     return 0;
86 }
87
88 static int clean_layout(SwrContext *s, int64_t layout){
89     if(layout && layout != AV_CH_FRONT_CENTER && !(layout&(layout-1))) {
90         char buf[128];
91         av_get_channel_layout_string(buf, sizeof(buf), -1, layout);
92         av_log(s, AV_LOG_VERBOSE, "Treating %s as mono\n", buf);
93         return AV_CH_FRONT_CENTER;
94     }
95
96     return layout;
97 }
98
99 static int sane_layout(int64_t layout){
100     if(!(layout & AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
101         return 0;
102     if(!even(layout & (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT))) // no asymetric front
103         return 0;
104     if(!even(layout & (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)))   // no asymetric side
105         return 0;
106     if(!even(layout & (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)))
107         return 0;
108     if(!even(layout & (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)))
109         return 0;
110     if(av_get_channel_layout_nb_channels(layout) >= SWR_CH_MAX)
111         return 0;
112
113     return 1;
114 }
115
116 av_cold static int auto_matrix(SwrContext *s)
117 {
118     int i, j, out_i;
119     double matrix[NUM_NAMED_CHANNELS][NUM_NAMED_CHANNELS]={{0}};
120     int64_t unaccounted, in_ch_layout, out_ch_layout;
121     double maxcoef=0;
122     char buf[128];
123     const int matrix_encoding = s->matrix_encoding;
124     float maxval;
125
126     in_ch_layout = clean_layout(s, s->in_ch_layout);
127     out_ch_layout = clean_layout(s, s->out_ch_layout);
128
129     if(   out_ch_layout == AV_CH_LAYOUT_STEREO_DOWNMIX
130        && (in_ch_layout & AV_CH_LAYOUT_STEREO_DOWNMIX) == 0
131     )
132         out_ch_layout = AV_CH_LAYOUT_STEREO;
133
134     if(    in_ch_layout == AV_CH_LAYOUT_STEREO_DOWNMIX
135        && (out_ch_layout & AV_CH_LAYOUT_STEREO_DOWNMIX) == 0
136     )
137         in_ch_layout = AV_CH_LAYOUT_STEREO;
138
139     if(!sane_layout(in_ch_layout)){
140         av_get_channel_layout_string(buf, sizeof(buf), -1, s->in_ch_layout);
141         av_log(s, AV_LOG_ERROR, "Input channel layout '%s' is not supported\n", buf);
142         return AVERROR(EINVAL);
143     }
144
145     if(!sane_layout(out_ch_layout)){
146         av_get_channel_layout_string(buf, sizeof(buf), -1, s->out_ch_layout);
147         av_log(s, AV_LOG_ERROR, "Output channel layout '%s' is not supported\n", buf);
148         return AVERROR(EINVAL);
149     }
150
151     memset(s->matrix, 0, sizeof(s->matrix));
152     for(i=0; i<FF_ARRAY_ELEMS(matrix); i++){
153         if(in_ch_layout & out_ch_layout & (1ULL<<i))
154             matrix[i][i]= 1.0;
155     }
156
157     unaccounted= in_ch_layout & ~out_ch_layout;
158
159 //FIXME implement dolby surround
160 //FIXME implement full ac3
161
162
163     if(unaccounted & AV_CH_FRONT_CENTER){
164         if((out_ch_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO){
165             if(in_ch_layout & AV_CH_LAYOUT_STEREO) {
166                 matrix[ FRONT_LEFT][FRONT_CENTER]+= s->clev;
167                 matrix[FRONT_RIGHT][FRONT_CENTER]+= s->clev;
168             } else {
169                 matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
170                 matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
171             }
172         }else
173             av_assert0(0);
174     }
175     if(unaccounted & AV_CH_LAYOUT_STEREO){
176         if(out_ch_layout & AV_CH_FRONT_CENTER){
177             matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
178             matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
179             if(in_ch_layout & AV_CH_FRONT_CENTER)
180                 matrix[FRONT_CENTER][ FRONT_CENTER] = s->clev*sqrt(2);
181         }else
182             av_assert0(0);
183     }
184
185     if(unaccounted & AV_CH_BACK_CENTER){
186         if(out_ch_layout & AV_CH_BACK_LEFT){
187             matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
188             matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
189         }else if(out_ch_layout & AV_CH_SIDE_LEFT){
190             matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
191             matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
192         }else if(out_ch_layout & AV_CH_FRONT_LEFT){
193             if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY ||
194                 matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
195                 if (unaccounted & (AV_CH_BACK_LEFT | AV_CH_SIDE_LEFT)) {
196                     matrix[FRONT_LEFT ][BACK_CENTER] -= s->slev * M_SQRT1_2;
197                     matrix[FRONT_RIGHT][BACK_CENTER] += s->slev * M_SQRT1_2;
198                 } else {
199                     matrix[FRONT_LEFT ][BACK_CENTER] -= s->slev;
200                     matrix[FRONT_RIGHT][BACK_CENTER] += s->slev;
201                 }
202             } else {
203                 matrix[ FRONT_LEFT][BACK_CENTER]+= s->slev*M_SQRT1_2;
204                 matrix[FRONT_RIGHT][BACK_CENTER]+= s->slev*M_SQRT1_2;
205             }
206         }else if(out_ch_layout & AV_CH_FRONT_CENTER){
207             matrix[ FRONT_CENTER][BACK_CENTER]+= s->slev*M_SQRT1_2;
208         }else
209             av_assert0(0);
210     }
211     if(unaccounted & AV_CH_BACK_LEFT){
212         if(out_ch_layout & AV_CH_BACK_CENTER){
213             matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
214             matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
215         }else if(out_ch_layout & AV_CH_SIDE_LEFT){
216             if(in_ch_layout & AV_CH_SIDE_LEFT){
217                 matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
218                 matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
219             }else{
220             matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
221             matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
222             }
223         }else if(out_ch_layout & AV_CH_FRONT_LEFT){
224             if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
225                 matrix[FRONT_LEFT ][BACK_LEFT ] -= s->slev * M_SQRT1_2;
226                 matrix[FRONT_LEFT ][BACK_RIGHT] -= s->slev * M_SQRT1_2;
227                 matrix[FRONT_RIGHT][BACK_LEFT ] += s->slev * M_SQRT1_2;
228                 matrix[FRONT_RIGHT][BACK_RIGHT] += s->slev * M_SQRT1_2;
229             } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
230                 matrix[FRONT_LEFT ][BACK_LEFT ] -= s->slev * SQRT3_2;
231                 matrix[FRONT_LEFT ][BACK_RIGHT] -= s->slev * M_SQRT1_2;
232                 matrix[FRONT_RIGHT][BACK_LEFT ] += s->slev * M_SQRT1_2;
233                 matrix[FRONT_RIGHT][BACK_RIGHT] += s->slev * SQRT3_2;
234             } else {
235                 matrix[ FRONT_LEFT][ BACK_LEFT] += s->slev;
236                 matrix[FRONT_RIGHT][BACK_RIGHT] += s->slev;
237             }
238         }else if(out_ch_layout & AV_CH_FRONT_CENTER){
239             matrix[ FRONT_CENTER][BACK_LEFT ]+= s->slev*M_SQRT1_2;
240             matrix[ FRONT_CENTER][BACK_RIGHT]+= s->slev*M_SQRT1_2;
241         }else
242             av_assert0(0);
243     }
244
245     if(unaccounted & AV_CH_SIDE_LEFT){
246         if(out_ch_layout & AV_CH_BACK_LEFT){
247             /* if back channels do not exist in the input, just copy side
248                channels to back channels, otherwise mix side into back */
249             if (in_ch_layout & AV_CH_BACK_LEFT) {
250                 matrix[BACK_LEFT ][SIDE_LEFT ] += M_SQRT1_2;
251                 matrix[BACK_RIGHT][SIDE_RIGHT] += M_SQRT1_2;
252             } else {
253                 matrix[BACK_LEFT ][SIDE_LEFT ] += 1.0;
254                 matrix[BACK_RIGHT][SIDE_RIGHT] += 1.0;
255             }
256         }else if(out_ch_layout & AV_CH_BACK_CENTER){
257             matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
258             matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
259         }else if(out_ch_layout & AV_CH_FRONT_LEFT){
260             if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
261                 matrix[FRONT_LEFT ][SIDE_LEFT ] -= s->slev * M_SQRT1_2;
262                 matrix[FRONT_LEFT ][SIDE_RIGHT] -= s->slev * M_SQRT1_2;
263                 matrix[FRONT_RIGHT][SIDE_LEFT ] += s->slev * M_SQRT1_2;
264                 matrix[FRONT_RIGHT][SIDE_RIGHT] += s->slev * M_SQRT1_2;
265             } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
266                 matrix[FRONT_LEFT ][SIDE_LEFT ] -= s->slev * SQRT3_2;
267                 matrix[FRONT_LEFT ][SIDE_RIGHT] -= s->slev * M_SQRT1_2;
268                 matrix[FRONT_RIGHT][SIDE_LEFT ] += s->slev * M_SQRT1_2;
269                 matrix[FRONT_RIGHT][SIDE_RIGHT] += s->slev * SQRT3_2;
270             } else {
271                 matrix[ FRONT_LEFT][ SIDE_LEFT] += s->slev;
272                 matrix[FRONT_RIGHT][SIDE_RIGHT] += s->slev;
273             }
274         }else if(out_ch_layout & AV_CH_FRONT_CENTER){
275             matrix[ FRONT_CENTER][SIDE_LEFT ]+= s->slev*M_SQRT1_2;
276             matrix[ FRONT_CENTER][SIDE_RIGHT]+= s->slev*M_SQRT1_2;
277         }else
278             av_assert0(0);
279     }
280
281     if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
282         if(out_ch_layout & AV_CH_FRONT_LEFT){
283             matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
284             matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
285         }else if(out_ch_layout & AV_CH_FRONT_CENTER){
286             matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
287             matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
288         }else
289             av_assert0(0);
290     }
291     /* mix LFE into front left/right or center */
292     if (unaccounted & AV_CH_LOW_FREQUENCY) {
293         if (out_ch_layout & AV_CH_FRONT_CENTER) {
294             matrix[FRONT_CENTER][LOW_FREQUENCY] += s->lfe_mix_level;
295         } else if (out_ch_layout & AV_CH_FRONT_LEFT) {
296             matrix[FRONT_LEFT ][LOW_FREQUENCY] += s->lfe_mix_level * M_SQRT1_2;
297             matrix[FRONT_RIGHT][LOW_FREQUENCY] += s->lfe_mix_level * M_SQRT1_2;
298         } else
299             av_assert0(0);
300     }
301
302     for(out_i=i=0; i<64; i++){
303         double sum=0;
304         int in_i=0;
305         if((out_ch_layout & (1ULL<<i)) == 0)
306             continue;
307         for(j=0; j<64; j++){
308             if((in_ch_layout & (1ULL<<j)) == 0)
309                continue;
310             if (i < FF_ARRAY_ELEMS(matrix) && j < FF_ARRAY_ELEMS(matrix[0]))
311                 s->matrix[out_i][in_i]= matrix[i][j];
312             else
313                 s->matrix[out_i][in_i]= i == j && (in_ch_layout & out_ch_layout & (1ULL<<i));
314             sum += fabs(s->matrix[out_i][in_i]);
315             in_i++;
316         }
317         maxcoef= FFMAX(maxcoef, sum);
318         out_i++;
319     }
320     if(s->rematrix_volume  < 0)
321         maxcoef = -s->rematrix_volume;
322
323     if (s->rematrix_maxval > 0) {
324         maxval = s->rematrix_maxval;
325     } else if (   av_get_packed_sample_fmt(s->out_sample_fmt) < AV_SAMPLE_FMT_FLT
326                || av_get_packed_sample_fmt(s->int_sample_fmt) < AV_SAMPLE_FMT_FLT) {
327         maxval = 1.0;
328     } else
329         maxval = INT_MAX;
330
331     if(maxcoef > maxval || s->rematrix_volume  < 0){
332         maxcoef /= maxval;
333         for(i=0; i<SWR_CH_MAX; i++)
334             for(j=0; j<SWR_CH_MAX; j++){
335                 s->matrix[i][j] /= maxcoef;
336             }
337     }
338
339     if(s->rematrix_volume > 0){
340         for(i=0; i<SWR_CH_MAX; i++)
341             for(j=0; j<SWR_CH_MAX; j++){
342                 s->matrix[i][j] *= s->rematrix_volume;
343             }
344     }
345
346     av_log(s, AV_LOG_DEBUG, "Matrix coefficients:\n");
347     for(i=0; i<av_get_channel_layout_nb_channels(out_ch_layout); i++){
348         const char *c =
349             av_get_channel_name(av_channel_layout_extract_channel(out_ch_layout, i));
350         av_log(s, AV_LOG_DEBUG, "%s: ", c ? c : "?");
351         for(j=0; j<av_get_channel_layout_nb_channels(in_ch_layout); j++){
352             c = av_get_channel_name(av_channel_layout_extract_channel(in_ch_layout, j));
353             av_log(s, AV_LOG_DEBUG, "%s:%f ", c ? c : "?", s->matrix[i][j]);
354         }
355         av_log(s, AV_LOG_DEBUG, "\n");
356     }
357     return 0;
358 }
359
360 av_cold int swri_rematrix_init(SwrContext *s){
361     int i, j;
362     int nb_in  = av_get_channel_layout_nb_channels(s->in_ch_layout);
363     int nb_out = av_get_channel_layout_nb_channels(s->out_ch_layout);
364
365     s->mix_any_f = NULL;
366
367     if (!s->rematrix_custom) {
368         int r = auto_matrix(s);
369         if (r)
370             return r;
371     }
372     if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
373         int maxsum = 0;
374         s->native_matrix = av_calloc(nb_in * nb_out, sizeof(int));
375         s->native_one    = av_mallocz(sizeof(int));
376         if (!s->native_matrix || !s->native_one)
377             return AVERROR(ENOMEM);
378         for (i = 0; i < nb_out; i++) {
379             double rem = 0;
380             int sum = 0;
381
382             for (j = 0; j < nb_in; j++) {
383                 double target = s->matrix[i][j] * 32768 + rem;
384                 ((int*)s->native_matrix)[i * nb_in + j] = lrintf(target);
385                 rem += target - ((int*)s->native_matrix)[i * nb_in + j];
386                 sum += FFABS(((int*)s->native_matrix)[i * nb_in + j]);
387             }
388             maxsum = FFMAX(maxsum, sum);
389         }
390         *((int*)s->native_one) = 32768;
391         if (maxsum <= 32768) {
392             s->mix_1_1_f = (mix_1_1_func_type*)copy_s16;
393             s->mix_2_1_f = (mix_2_1_func_type*)sum2_s16;
394             s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s16(s);
395         } else {
396             s->mix_1_1_f = (mix_1_1_func_type*)copy_clip_s16;
397             s->mix_2_1_f = (mix_2_1_func_type*)sum2_clip_s16;
398             s->mix_any_f = (mix_any_func_type*)get_mix_any_func_clip_s16(s);
399         }
400     }else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
401         s->native_matrix = av_calloc(nb_in * nb_out, sizeof(float));
402         s->native_one    = av_mallocz(sizeof(float));
403         if (!s->native_matrix || !s->native_one)
404             return AVERROR(ENOMEM);
405         for (i = 0; i < nb_out; i++)
406             for (j = 0; j < nb_in; j++)
407                 ((float*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
408         *((float*)s->native_one) = 1.0;
409         s->mix_1_1_f = (mix_1_1_func_type*)copy_float;
410         s->mix_2_1_f = (mix_2_1_func_type*)sum2_float;
411         s->mix_any_f = (mix_any_func_type*)get_mix_any_func_float(s);
412     }else if(s->midbuf.fmt == AV_SAMPLE_FMT_DBLP){
413         s->native_matrix = av_calloc(nb_in * nb_out, sizeof(double));
414         s->native_one    = av_mallocz(sizeof(double));
415         if (!s->native_matrix || !s->native_one)
416             return AVERROR(ENOMEM);
417         for (i = 0; i < nb_out; i++)
418             for (j = 0; j < nb_in; j++)
419                 ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
420         *((double*)s->native_one) = 1.0;
421         s->mix_1_1_f = (mix_1_1_func_type*)copy_double;
422         s->mix_2_1_f = (mix_2_1_func_type*)sum2_double;
423         s->mix_any_f = (mix_any_func_type*)get_mix_any_func_double(s);
424     }else if(s->midbuf.fmt == AV_SAMPLE_FMT_S32P){
425         // Only for dithering currently
426 //         s->native_matrix = av_calloc(nb_in * nb_out, sizeof(double));
427         s->native_one    = av_mallocz(sizeof(int));
428         if (!s->native_one)
429             return AVERROR(ENOMEM);
430 //         for (i = 0; i < nb_out; i++)
431 //             for (j = 0; j < nb_in; j++)
432 //                 ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
433         *((int*)s->native_one) = 32768;
434         s->mix_1_1_f = (mix_1_1_func_type*)copy_s32;
435         s->mix_2_1_f = (mix_2_1_func_type*)sum2_s32;
436         s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s32(s);
437     }else
438         av_assert0(0);
439     //FIXME quantize for integeres
440     for (i = 0; i < SWR_CH_MAX; i++) {
441         int ch_in=0;
442         for (j = 0; j < SWR_CH_MAX; j++) {
443             s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
444             if(s->matrix[i][j])
445                 s->matrix_ch[i][++ch_in]= j;
446         }
447         s->matrix_ch[i][0]= ch_in;
448     }
449
450     if(HAVE_YASM && HAVE_MMX)
451         return swri_rematrix_init_x86(s);
452
453     return 0;
454 }
455
456 av_cold void swri_rematrix_free(SwrContext *s){
457     av_freep(&s->native_matrix);
458     av_freep(&s->native_one);
459     av_freep(&s->native_simd_matrix);
460     av_freep(&s->native_simd_one);
461 }
462
463 int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
464     int out_i, in_i, i, j;
465     int len1 = 0;
466     int off = 0;
467
468     if(s->mix_any_f) {
469         s->mix_any_f(out->ch, (const uint8_t **)in->ch, s->native_matrix, len);
470         return 0;
471     }
472
473     if(s->mix_2_1_simd || s->mix_1_1_simd){
474         len1= len&~15;
475         off = len1 * out->bps;
476     }
477
478     av_assert0(!s->out_ch_layout || out->ch_count == av_get_channel_layout_nb_channels(s->out_ch_layout));
479     av_assert0(!s-> in_ch_layout || in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
480
481     for(out_i=0; out_i<out->ch_count; out_i++){
482         switch(s->matrix_ch[out_i][0]){
483         case 0:
484             if(mustcopy)
485                 memset(out->ch[out_i], 0, len * av_get_bytes_per_sample(s->int_sample_fmt));
486             break;
487         case 1:
488             in_i= s->matrix_ch[out_i][1];
489             if(s->matrix[out_i][in_i]!=1.0){
490                 if(s->mix_1_1_simd && len1)
491                     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);
492                 if(len != len1)
493                     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);
494             }else if(mustcopy){
495                 memcpy(out->ch[out_i], in->ch[in_i], len*out->bps);
496             }else{
497                 out->ch[out_i]= in->ch[in_i];
498             }
499             break;
500         case 2: {
501             int in_i1 = s->matrix_ch[out_i][1];
502             int in_i2 = s->matrix_ch[out_i][2];
503             if(s->mix_2_1_simd && len1)
504                 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);
505             else
506                 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);
507             if(len != len1)
508                 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);
509             break;}
510         default:
511             if(s->int_sample_fmt == AV_SAMPLE_FMT_FLTP){
512                 for(i=0; i<len; i++){
513                     float v=0;
514                     for(j=0; j<s->matrix_ch[out_i][0]; j++){
515                         in_i= s->matrix_ch[out_i][1+j];
516                         v+= ((float*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
517                     }
518                     ((float*)out->ch[out_i])[i]= v;
519                 }
520             }else if(s->int_sample_fmt == AV_SAMPLE_FMT_DBLP){
521                 for(i=0; i<len; i++){
522                     double 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+= ((double*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
526                     }
527                     ((double*)out->ch[out_i])[i]= v;
528                 }
529             }else{
530                 for(i=0; i<len; i++){
531                     int 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+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
535                     }
536                     ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;
537                 }
538             }
539         }
540     }
541     return 0;
542 }