2 * Copyright (C) 2011-2012 Michael Niedermayer (michaelni@gmx.at)
4 * This file is part of libswresample
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.
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.
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
21 #include "swresample_internal.h"
22 #include "libavutil/avassert.h"
23 #include "libavutil/channel_layout.h"
25 #define TEMPLATE_REMATRIX_FLT
26 #include "rematrix_template.c"
27 #undef TEMPLATE_REMATRIX_FLT
29 #define TEMPLATE_REMATRIX_DBL
30 #include "rematrix_template.c"
31 #undef TEMPLATE_REMATRIX_DBL
33 #define TEMPLATE_REMATRIX_S16
34 #include "rematrix_template.c"
36 #include "rematrix_template.c"
38 #undef TEMPLATE_REMATRIX_S16
40 #define TEMPLATE_REMATRIX_S32
41 #include "rematrix_template.c"
42 #undef TEMPLATE_REMATRIX_S32
46 #define FRONT_CENTER 2
47 #define LOW_FREQUENCY 3
50 #define FRONT_LEFT_OF_CENTER 6
51 #define FRONT_RIGHT_OF_CENTER 7
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
64 int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride)
66 int nb_in, nb_out, in, out;
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 memset(s->matrix_flt, 0, sizeof(s->matrix_flt));
72 nb_in = (s->user_in_ch_count > 0) ? s->user_in_ch_count :
73 av_get_channel_layout_nb_channels(s->user_in_ch_layout);
74 nb_out = (s->user_out_ch_count > 0) ? s->user_out_ch_count :
75 av_get_channel_layout_nb_channels(s->user_out_ch_layout);
76 for (out = 0; out < nb_out; out++) {
77 for (in = 0; in < nb_in; in++)
78 s->matrix_flt[out][in] = s->matrix[out][in] = matrix[in];
81 s->rematrix_custom = 1;
85 static int even(int64_t layout){
87 if(layout&(layout-1)) return 1;
91 static int clean_layout(void *s, int64_t layout){
92 if(layout && layout != AV_CH_FRONT_CENTER && !(layout&(layout-1))) {
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;
102 static int sane_layout(int64_t layout){
103 if(!(layout & AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
105 if(!even(layout & (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT))) // no asymetric front
107 if(!even(layout & (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT))) // no asymetric side
109 if(!even(layout & (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)))
111 if(!even(layout & (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)))
113 if(av_get_channel_layout_nb_channels(layout) >= SWR_CH_MAX)
119 av_cold int swr_build_matrix(uint64_t in_ch_layout_param, uint64_t out_ch_layout_param,
120 double center_mix_level, double surround_mix_level,
121 double lfe_mix_level, double maxval,
122 double rematrix_volume, double *matrix_param,
123 int stride, enum AVMatrixEncoding matrix_encoding, void *log_context)
126 double matrix[NUM_NAMED_CHANNELS][NUM_NAMED_CHANNELS]={{0}};
127 int64_t unaccounted, in_ch_layout, out_ch_layout;
131 in_ch_layout = clean_layout(log_context, in_ch_layout_param);
132 out_ch_layout = clean_layout(log_context, out_ch_layout_param);
134 if( out_ch_layout == AV_CH_LAYOUT_STEREO_DOWNMIX
135 && (in_ch_layout & AV_CH_LAYOUT_STEREO_DOWNMIX) == 0
137 out_ch_layout = AV_CH_LAYOUT_STEREO;
139 if( in_ch_layout == AV_CH_LAYOUT_STEREO_DOWNMIX
140 && (out_ch_layout & AV_CH_LAYOUT_STEREO_DOWNMIX) == 0
142 in_ch_layout = AV_CH_LAYOUT_STEREO;
144 if(!sane_layout(in_ch_layout)){
145 av_get_channel_layout_string(buf, sizeof(buf), -1, in_ch_layout_param);
146 av_log(log_context, AV_LOG_ERROR, "Input channel layout '%s' is not supported\n", buf);
147 return AVERROR(EINVAL);
150 if(!sane_layout(out_ch_layout)){
151 av_get_channel_layout_string(buf, sizeof(buf), -1, out_ch_layout_param);
152 av_log(log_context, AV_LOG_ERROR, "Output channel layout '%s' is not supported\n", buf);
153 return AVERROR(EINVAL);
156 for(i=0; i<FF_ARRAY_ELEMS(matrix); i++){
157 if(in_ch_layout & out_ch_layout & (1ULL<<i))
161 unaccounted= in_ch_layout & ~out_ch_layout;
163 //FIXME implement dolby surround
164 //FIXME implement full ac3
167 if(unaccounted & AV_CH_FRONT_CENTER){
168 if((out_ch_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO){
169 if(in_ch_layout & AV_CH_LAYOUT_STEREO) {
170 matrix[ FRONT_LEFT][FRONT_CENTER]+= center_mix_level;
171 matrix[FRONT_RIGHT][FRONT_CENTER]+= center_mix_level;
173 matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
174 matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
179 if(unaccounted & AV_CH_LAYOUT_STEREO){
180 if(out_ch_layout & AV_CH_FRONT_CENTER){
181 matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
182 matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
183 if(in_ch_layout & AV_CH_FRONT_CENTER)
184 matrix[FRONT_CENTER][ FRONT_CENTER] = center_mix_level*sqrt(2);
189 if(unaccounted & AV_CH_BACK_CENTER){
190 if(out_ch_layout & AV_CH_BACK_LEFT){
191 matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
192 matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
193 }else if(out_ch_layout & AV_CH_SIDE_LEFT){
194 matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
195 matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
196 }else if(out_ch_layout & AV_CH_FRONT_LEFT){
197 if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY ||
198 matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
199 if (unaccounted & (AV_CH_BACK_LEFT | AV_CH_SIDE_LEFT)) {
200 matrix[FRONT_LEFT ][BACK_CENTER] -= surround_mix_level * M_SQRT1_2;
201 matrix[FRONT_RIGHT][BACK_CENTER] += surround_mix_level * M_SQRT1_2;
203 matrix[FRONT_LEFT ][BACK_CENTER] -= surround_mix_level;
204 matrix[FRONT_RIGHT][BACK_CENTER] += surround_mix_level;
207 matrix[ FRONT_LEFT][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
208 matrix[FRONT_RIGHT][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
210 }else if(out_ch_layout & AV_CH_FRONT_CENTER){
211 matrix[ FRONT_CENTER][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
215 if(unaccounted & AV_CH_BACK_LEFT){
216 if(out_ch_layout & AV_CH_BACK_CENTER){
217 matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
218 matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
219 }else if(out_ch_layout & AV_CH_SIDE_LEFT){
220 if(in_ch_layout & AV_CH_SIDE_LEFT){
221 matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
222 matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
224 matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
225 matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
227 }else if(out_ch_layout & AV_CH_FRONT_LEFT){
228 if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
229 matrix[FRONT_LEFT ][BACK_LEFT ] -= surround_mix_level * M_SQRT1_2;
230 matrix[FRONT_LEFT ][BACK_RIGHT] -= surround_mix_level * M_SQRT1_2;
231 matrix[FRONT_RIGHT][BACK_LEFT ] += surround_mix_level * M_SQRT1_2;
232 matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level * M_SQRT1_2;
233 } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
234 matrix[FRONT_LEFT ][BACK_LEFT ] -= surround_mix_level * SQRT3_2;
235 matrix[FRONT_LEFT ][BACK_RIGHT] -= surround_mix_level * M_SQRT1_2;
236 matrix[FRONT_RIGHT][BACK_LEFT ] += surround_mix_level * M_SQRT1_2;
237 matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level * SQRT3_2;
239 matrix[ FRONT_LEFT][ BACK_LEFT] += surround_mix_level;
240 matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level;
242 }else if(out_ch_layout & AV_CH_FRONT_CENTER){
243 matrix[ FRONT_CENTER][BACK_LEFT ]+= surround_mix_level*M_SQRT1_2;
244 matrix[ FRONT_CENTER][BACK_RIGHT]+= surround_mix_level*M_SQRT1_2;
249 if(unaccounted & AV_CH_SIDE_LEFT){
250 if(out_ch_layout & AV_CH_BACK_LEFT){
251 /* if back channels do not exist in the input, just copy side
252 channels to back channels, otherwise mix side into back */
253 if (in_ch_layout & AV_CH_BACK_LEFT) {
254 matrix[BACK_LEFT ][SIDE_LEFT ] += M_SQRT1_2;
255 matrix[BACK_RIGHT][SIDE_RIGHT] += M_SQRT1_2;
257 matrix[BACK_LEFT ][SIDE_LEFT ] += 1.0;
258 matrix[BACK_RIGHT][SIDE_RIGHT] += 1.0;
260 }else if(out_ch_layout & AV_CH_BACK_CENTER){
261 matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
262 matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
263 }else if(out_ch_layout & AV_CH_FRONT_LEFT){
264 if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
265 matrix[FRONT_LEFT ][SIDE_LEFT ] -= surround_mix_level * M_SQRT1_2;
266 matrix[FRONT_LEFT ][SIDE_RIGHT] -= surround_mix_level * M_SQRT1_2;
267 matrix[FRONT_RIGHT][SIDE_LEFT ] += surround_mix_level * M_SQRT1_2;
268 matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level * M_SQRT1_2;
269 } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
270 matrix[FRONT_LEFT ][SIDE_LEFT ] -= surround_mix_level * SQRT3_2;
271 matrix[FRONT_LEFT ][SIDE_RIGHT] -= surround_mix_level * M_SQRT1_2;
272 matrix[FRONT_RIGHT][SIDE_LEFT ] += surround_mix_level * M_SQRT1_2;
273 matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level * SQRT3_2;
275 matrix[ FRONT_LEFT][ SIDE_LEFT] += surround_mix_level;
276 matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level;
278 }else if(out_ch_layout & AV_CH_FRONT_CENTER){
279 matrix[ FRONT_CENTER][SIDE_LEFT ]+= surround_mix_level * M_SQRT1_2;
280 matrix[ FRONT_CENTER][SIDE_RIGHT]+= surround_mix_level * M_SQRT1_2;
285 if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
286 if(out_ch_layout & AV_CH_FRONT_LEFT){
287 matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
288 matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
289 }else if(out_ch_layout & AV_CH_FRONT_CENTER){
290 matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
291 matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
295 /* mix LFE into front left/right or center */
296 if (unaccounted & AV_CH_LOW_FREQUENCY) {
297 if (out_ch_layout & AV_CH_FRONT_CENTER) {
298 matrix[FRONT_CENTER][LOW_FREQUENCY] += lfe_mix_level;
299 } else if (out_ch_layout & AV_CH_FRONT_LEFT) {
300 matrix[FRONT_LEFT ][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
301 matrix[FRONT_RIGHT][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
306 for(out_i=i=0; i<64; i++){
309 if((out_ch_layout & (1ULL<<i)) == 0)
312 if((in_ch_layout & (1ULL<<j)) == 0)
314 if (i < FF_ARRAY_ELEMS(matrix) && j < FF_ARRAY_ELEMS(matrix[0]))
315 matrix_param[stride*out_i + in_i] = matrix[i][j];
317 matrix_param[stride*out_i + in_i] = i == j && (in_ch_layout & out_ch_layout & (1ULL<<i));
318 sum += fabs(matrix_param[stride*out_i + in_i]);
321 maxcoef= FFMAX(maxcoef, sum);
324 if(rematrix_volume < 0)
325 maxcoef = -rematrix_volume;
327 if(maxcoef > maxval || rematrix_volume < 0){
329 for(i=0; i<SWR_CH_MAX; i++)
330 for(j=0; j<SWR_CH_MAX; j++){
331 matrix_param[stride*i + j] /= maxcoef;
335 if(rematrix_volume > 0){
336 for(i=0; i<SWR_CH_MAX; i++)
337 for(j=0; j<SWR_CH_MAX; j++){
338 matrix_param[stride*i + j] *= rematrix_volume;
342 av_log(log_context, AV_LOG_DEBUG, "Matrix coefficients:\n");
343 for(i=0; i<av_get_channel_layout_nb_channels(out_ch_layout); i++){
345 av_get_channel_name(av_channel_layout_extract_channel(out_ch_layout, i));
346 av_log(log_context, AV_LOG_DEBUG, "%s: ", c ? c : "?");
347 for(j=0; j<av_get_channel_layout_nb_channels(in_ch_layout); j++){
348 c = av_get_channel_name(av_channel_layout_extract_channel(in_ch_layout, j));
349 av_log(log_context, AV_LOG_DEBUG, "%s:%f ", c ? c : "?", matrix_param[stride*i + j]);
351 av_log(log_context, AV_LOG_DEBUG, "\n");
356 av_cold static int auto_matrix(SwrContext *s)
361 if (s->rematrix_maxval > 0) {
362 maxval = s->rematrix_maxval;
363 } else if ( av_get_packed_sample_fmt(s->out_sample_fmt) < AV_SAMPLE_FMT_FLT
364 || av_get_packed_sample_fmt(s->int_sample_fmt) < AV_SAMPLE_FMT_FLT) {
369 memset(s->matrix, 0, sizeof(s->matrix));
370 ret = swr_build_matrix(s->in_ch_layout, s->out_ch_layout,
371 s->clev, s->slev, s->lfe_mix_level,
372 maxval, s->rematrix_volume, (double*)s->matrix,
373 s->matrix[1] - s->matrix[0], s->matrix_encoding, s);
375 if (ret >= 0 && s->int_sample_fmt == AV_SAMPLE_FMT_FLTP) {
377 for (i = 0; i < FF_ARRAY_ELEMS(s->matrix[0]); i++)
378 for (j = 0; j < FF_ARRAY_ELEMS(s->matrix[0]); j++)
379 s->matrix_flt[i][j] = s->matrix[i][j];
385 av_cold int swri_rematrix_init(SwrContext *s){
387 int nb_in = s->used_ch_count;
388 int nb_out = s->out.ch_count;
392 if (!s->rematrix_custom) {
393 int r = auto_matrix(s);
397 if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
399 s->native_matrix = av_calloc(nb_in * nb_out, sizeof(int));
400 s->native_one = av_mallocz(sizeof(int));
401 if (!s->native_matrix || !s->native_one)
402 return AVERROR(ENOMEM);
403 for (i = 0; i < nb_out; i++) {
407 for (j = 0; j < nb_in; j++) {
408 double target = s->matrix[i][j] * 32768 + rem;
409 ((int*)s->native_matrix)[i * nb_in + j] = lrintf(target);
410 rem += target - ((int*)s->native_matrix)[i * nb_in + j];
411 sum += FFABS(((int*)s->native_matrix)[i * nb_in + j]);
413 maxsum = FFMAX(maxsum, sum);
415 *((int*)s->native_one) = 32768;
416 if (maxsum <= 32768) {
417 s->mix_1_1_f = (mix_1_1_func_type*)copy_s16;
418 s->mix_2_1_f = (mix_2_1_func_type*)sum2_s16;
419 s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s16(s);
421 s->mix_1_1_f = (mix_1_1_func_type*)copy_clip_s16;
422 s->mix_2_1_f = (mix_2_1_func_type*)sum2_clip_s16;
423 s->mix_any_f = (mix_any_func_type*)get_mix_any_func_clip_s16(s);
425 }else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
426 s->native_matrix = av_calloc(nb_in * nb_out, sizeof(float));
427 s->native_one = av_mallocz(sizeof(float));
428 if (!s->native_matrix || !s->native_one)
429 return AVERROR(ENOMEM);
430 for (i = 0; i < nb_out; i++)
431 for (j = 0; j < nb_in; j++)
432 ((float*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
433 *((float*)s->native_one) = 1.0;
434 s->mix_1_1_f = (mix_1_1_func_type*)copy_float;
435 s->mix_2_1_f = (mix_2_1_func_type*)sum2_float;
436 s->mix_any_f = (mix_any_func_type*)get_mix_any_func_float(s);
437 }else if(s->midbuf.fmt == AV_SAMPLE_FMT_DBLP){
438 s->native_matrix = av_calloc(nb_in * nb_out, sizeof(double));
439 s->native_one = av_mallocz(sizeof(double));
440 if (!s->native_matrix || !s->native_one)
441 return AVERROR(ENOMEM);
442 for (i = 0; i < nb_out; i++)
443 for (j = 0; j < nb_in; j++)
444 ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
445 *((double*)s->native_one) = 1.0;
446 s->mix_1_1_f = (mix_1_1_func_type*)copy_double;
447 s->mix_2_1_f = (mix_2_1_func_type*)sum2_double;
448 s->mix_any_f = (mix_any_func_type*)get_mix_any_func_double(s);
449 }else if(s->midbuf.fmt == AV_SAMPLE_FMT_S32P){
450 s->native_one = av_mallocz(sizeof(int));
452 return AVERROR(ENOMEM);
453 s->native_matrix = av_calloc(nb_in * nb_out, sizeof(int));
454 if (!s->native_matrix) {
455 av_freep(&s->native_one);
456 return AVERROR(ENOMEM);
458 for (i = 0; i < nb_out; i++) {
461 for (j = 0; j < nb_in; j++) {
462 double target = s->matrix[i][j] * 32768 + rem;
463 ((int*)s->native_matrix)[i * nb_in + j] = lrintf(target);
464 rem += target - ((int*)s->native_matrix)[i * nb_in + j];
467 *((int*)s->native_one) = 32768;
468 s->mix_1_1_f = (mix_1_1_func_type*)copy_s32;
469 s->mix_2_1_f = (mix_2_1_func_type*)sum2_s32;
470 s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s32(s);
473 //FIXME quantize for integeres
474 for (i = 0; i < SWR_CH_MAX; i++) {
476 for (j = 0; j < SWR_CH_MAX; j++) {
477 s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
479 s->matrix_ch[i][++ch_in]= j;
481 s->matrix_ch[i][0]= ch_in;
484 if(HAVE_X86ASM && HAVE_MMX)
485 return swri_rematrix_init_x86(s);
490 av_cold void swri_rematrix_free(SwrContext *s){
491 av_freep(&s->native_matrix);
492 av_freep(&s->native_one);
493 av_freep(&s->native_simd_matrix);
494 av_freep(&s->native_simd_one);
497 int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
498 int out_i, in_i, i, j;
503 s->mix_any_f(out->ch, (const uint8_t **)in->ch, s->native_matrix, len);
507 if(s->mix_2_1_simd || s->mix_1_1_simd){
509 off = len1 * out->bps;
512 av_assert0(!s->out_ch_layout || out->ch_count == av_get_channel_layout_nb_channels(s->out_ch_layout));
513 av_assert0(!s-> in_ch_layout || in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
515 for(out_i=0; out_i<out->ch_count; out_i++){
516 switch(s->matrix_ch[out_i][0]){
519 memset(out->ch[out_i], 0, len * av_get_bytes_per_sample(s->int_sample_fmt));
522 in_i= s->matrix_ch[out_i][1];
523 if(s->matrix[out_i][in_i]!=1.0){
524 if(s->mix_1_1_simd && len1)
525 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);
527 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);
529 memcpy(out->ch[out_i], in->ch[in_i], len*out->bps);
531 out->ch[out_i]= in->ch[in_i];
535 int in_i1 = s->matrix_ch[out_i][1];
536 int in_i2 = s->matrix_ch[out_i][2];
537 if(s->mix_2_1_simd && len1)
538 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);
540 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);
542 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);
545 if(s->int_sample_fmt == AV_SAMPLE_FMT_FLTP){
546 for(i=0; i<len; i++){
548 for(j=0; j<s->matrix_ch[out_i][0]; j++){
549 in_i= s->matrix_ch[out_i][1+j];
550 v+= ((float*)in->ch[in_i])[i] * s->matrix_flt[out_i][in_i];
552 ((float*)out->ch[out_i])[i]= v;
554 }else if(s->int_sample_fmt == AV_SAMPLE_FMT_DBLP){
555 for(i=0; i<len; i++){
557 for(j=0; j<s->matrix_ch[out_i][0]; j++){
558 in_i= s->matrix_ch[out_i][1+j];
559 v+= ((double*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
561 ((double*)out->ch[out_i])[i]= v;
564 for(i=0; i<len; i++){
566 for(j=0; j<s->matrix_ch[out_i][0]; j++){
567 in_i= s->matrix_ch[out_i][1+j];
568 v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
570 ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;