]> git.sesse.net Git - ffmpeg/blob - libswresample/rematrix.c
Merge commit '71f7b22dba60524b2285643ae0b49d8f64977129'
[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/audioconvert.h"
23 #include "libavutil/avassert.h"
24
25 #define ONE (1.0)
26 #define R(x) x
27 #define SAMPLE float
28 #define COEFF float
29 #define INTER float
30 #define RENAME(x) x ## _float
31 #include "rematrix_template.c"
32 #undef SAMPLE
33 #undef RENAME
34 #undef R
35 #undef ONE
36 #undef COEFF
37 #undef INTER
38
39 #define ONE (1.0)
40 #define R(x) x
41 #define SAMPLE double
42 #define COEFF double
43 #define INTER double
44 #define RENAME(x) x ## _double
45 #include "rematrix_template.c"
46 #undef SAMPLE
47 #undef RENAME
48 #undef R
49 #undef ONE
50 #undef COEFF
51 #undef INTER
52
53 #define ONE (-32768)
54 #define R(x) (((x) + 16384)>>15)
55 #define SAMPLE int16_t
56 #define COEFF int
57 #define INTER int
58 #define RENAME(x) x ## _s16
59 #include "rematrix_template.c"
60
61
62 #define FRONT_LEFT             0
63 #define FRONT_RIGHT            1
64 #define FRONT_CENTER           2
65 #define LOW_FREQUENCY          3
66 #define BACK_LEFT              4
67 #define BACK_RIGHT             5
68 #define FRONT_LEFT_OF_CENTER   6
69 #define FRONT_RIGHT_OF_CENTER  7
70 #define BACK_CENTER            8
71 #define SIDE_LEFT              9
72 #define SIDE_RIGHT             10
73 #define TOP_CENTER             11
74 #define TOP_FRONT_LEFT         12
75 #define TOP_FRONT_CENTER       13
76 #define TOP_FRONT_RIGHT        14
77 #define TOP_BACK_LEFT          15
78 #define TOP_BACK_CENTER        16
79 #define TOP_BACK_RIGHT         17
80
81 int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride)
82 {
83     int nb_in, nb_out, in, out;
84
85     if (!s || s->in_convert) // s needs to be allocated but not initialized
86         return AVERROR(EINVAL);
87     memset(s->matrix, 0, sizeof(s->matrix));
88     nb_in  = av_get_channel_layout_nb_channels(s->in_ch_layout);
89     nb_out = av_get_channel_layout_nb_channels(s->out_ch_layout);
90     for (out = 0; out < nb_out; out++) {
91         for (in = 0; in < nb_in; in++)
92             s->matrix[out][in] = matrix[in];
93         matrix += stride;
94     }
95     s->rematrix_custom = 1;
96     return 0;
97 }
98
99 static int even(int64_t layout){
100     if(!layout) return 1;
101     if(layout&(layout-1)) return 1;
102     return 0;
103 }
104
105 static int clean_layout(SwrContext *s, int64_t layout){
106     if((layout & AV_CH_LAYOUT_STEREO_DOWNMIX) == AV_CH_LAYOUT_STEREO_DOWNMIX)
107         return AV_CH_LAYOUT_STEREO;
108
109     if(layout && layout != AV_CH_FRONT_CENTER && !(layout&(layout-1))) {
110         char buf[128];
111         av_get_channel_layout_string(buf, sizeof(buf), -1, layout);
112         av_log(s, AV_LOG_VERBOSE, "Treating %s as mono\n", buf);
113         return AV_CH_FRONT_CENTER;
114     }
115
116     return layout;
117 }
118
119 static int sane_layout(int64_t layout){
120     if(!(layout & AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
121         return 0;
122     if(!even(layout & (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT))) // no asymetric front
123         return 0;
124     if(!even(layout & (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)))   // no asymetric side
125         return 0;
126     if(!even(layout & (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)))
127         return 0;
128     if(!even(layout & (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)))
129         return 0;
130     if(av_get_channel_layout_nb_channels(layout) >= SWR_CH_MAX)
131         return 0;
132
133     return 1;
134 }
135
136 av_cold static int auto_matrix(SwrContext *s)
137 {
138     int i, j, out_i;
139     double matrix[64][64]={{0}};
140     int64_t unaccounted, in_ch_layout, out_ch_layout;
141     double maxcoef=0;
142     char buf[128];
143     const int matrix_encoding = s->matrix_encoding;
144
145     in_ch_layout = clean_layout(s, s->in_ch_layout);
146     if(!sane_layout(in_ch_layout)){
147         av_get_channel_layout_string(buf, sizeof(buf), -1, s->in_ch_layout);
148         av_log(s, AV_LOG_ERROR, "Input channel layout '%s' is not supported\n", buf);
149         return AVERROR(EINVAL);
150     }
151
152     out_ch_layout = clean_layout(s, s->out_ch_layout);
153     if(!sane_layout(out_ch_layout)){
154         av_get_channel_layout_string(buf, sizeof(buf), -1, s->out_ch_layout);
155         av_log(s, AV_LOG_ERROR, "Output channel layout '%s' is not supported\n", buf);
156         return AVERROR(EINVAL);
157     }
158
159     memset(s->matrix, 0, sizeof(s->matrix));
160     for(i=0; i<64; i++){
161         if(in_ch_layout & out_ch_layout & (1ULL<<i))
162             matrix[i][i]= 1.0;
163     }
164
165     unaccounted= in_ch_layout & ~out_ch_layout;
166
167 //FIXME implement dolby surround
168 //FIXME implement full ac3
169
170
171     if(unaccounted & AV_CH_FRONT_CENTER){
172         if((out_ch_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO){
173             if(in_ch_layout & AV_CH_LAYOUT_STEREO) {
174                 matrix[ FRONT_LEFT][FRONT_CENTER]+= s->clev;
175                 matrix[FRONT_RIGHT][FRONT_CENTER]+= s->clev;
176             } else {
177                 matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
178                 matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
179             }
180         }else
181             av_assert0(0);
182     }
183     if(unaccounted & AV_CH_LAYOUT_STEREO){
184         if(out_ch_layout & AV_CH_FRONT_CENTER){
185             matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
186             matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
187             if(in_ch_layout & AV_CH_FRONT_CENTER)
188                 matrix[FRONT_CENTER][ FRONT_CENTER] = s->clev*sqrt(2);
189         }else
190             av_assert0(0);
191     }
192
193     if(unaccounted & AV_CH_BACK_CENTER){
194         if(out_ch_layout & AV_CH_BACK_LEFT){
195             matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
196             matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
197         }else if(out_ch_layout & AV_CH_SIDE_LEFT){
198             matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
199             matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
200         }else if(out_ch_layout & AV_CH_FRONT_LEFT){
201             if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY ||
202                 matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
203                 if (unaccounted & (AV_CH_BACK_LEFT | AV_CH_SIDE_LEFT)) {
204                     matrix[FRONT_LEFT ][BACK_CENTER] -= s->slev * M_SQRT1_2;
205                     matrix[FRONT_RIGHT][BACK_CENTER] += s->slev * M_SQRT1_2;
206                 } else {
207                     matrix[FRONT_LEFT ][BACK_CENTER] -= s->slev;
208                     matrix[FRONT_RIGHT][BACK_CENTER] += s->slev;
209                 }
210             } else {
211                 matrix[ FRONT_LEFT][BACK_CENTER]+= s->slev*M_SQRT1_2;
212                 matrix[FRONT_RIGHT][BACK_CENTER]+= s->slev*M_SQRT1_2;
213             }
214         }else if(out_ch_layout & AV_CH_FRONT_CENTER){
215             matrix[ FRONT_CENTER][BACK_CENTER]+= s->slev*M_SQRT1_2;
216         }else
217             av_assert0(0);
218     }
219     if(unaccounted & AV_CH_BACK_LEFT){
220         if(out_ch_layout & AV_CH_BACK_CENTER){
221             matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
222             matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
223         }else if(out_ch_layout & AV_CH_SIDE_LEFT){
224             if(in_ch_layout & AV_CH_SIDE_LEFT){
225                 matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
226                 matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
227             }else{
228             matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
229             matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
230             }
231         }else if(out_ch_layout & AV_CH_FRONT_LEFT){
232             if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
233                 matrix[FRONT_LEFT ][BACK_LEFT ] -= s->slev * M_SQRT1_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 * M_SQRT1_2;
237             } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
238                 matrix[FRONT_LEFT ][BACK_LEFT ] -= s->slev * SQRT3_2;
239                 matrix[FRONT_LEFT ][BACK_RIGHT] -= s->slev * M_SQRT1_2;
240                 matrix[FRONT_RIGHT][BACK_LEFT ] += s->slev * M_SQRT1_2;
241                 matrix[FRONT_RIGHT][BACK_RIGHT] += s->slev * SQRT3_2;
242             } else {
243                 matrix[ FRONT_LEFT][ BACK_LEFT] += s->slev;
244                 matrix[FRONT_RIGHT][BACK_RIGHT] += s->slev;
245             }
246         }else if(out_ch_layout & AV_CH_FRONT_CENTER){
247             matrix[ FRONT_CENTER][BACK_LEFT ]+= s->slev*M_SQRT1_2;
248             matrix[ FRONT_CENTER][BACK_RIGHT]+= s->slev*M_SQRT1_2;
249         }else
250             av_assert0(0);
251     }
252
253     if(unaccounted & AV_CH_SIDE_LEFT){
254         if(out_ch_layout & AV_CH_BACK_LEFT){
255             /* if back channels do not exist in the input, just copy side
256                channels to back channels, otherwise mix side into back */
257             if (in_ch_layout & AV_CH_BACK_LEFT) {
258                 matrix[BACK_LEFT ][SIDE_LEFT ] += M_SQRT1_2;
259                 matrix[BACK_RIGHT][SIDE_RIGHT] += M_SQRT1_2;
260             } else {
261                 matrix[BACK_LEFT ][SIDE_LEFT ] += 1.0;
262                 matrix[BACK_RIGHT][SIDE_RIGHT] += 1.0;
263             }
264         }else if(out_ch_layout & AV_CH_BACK_CENTER){
265             matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
266             matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
267         }else if(out_ch_layout & AV_CH_FRONT_LEFT){
268             if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
269                 matrix[FRONT_LEFT ][SIDE_LEFT ] -= s->slev * M_SQRT1_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 * M_SQRT1_2;
273             } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
274                 matrix[FRONT_LEFT ][SIDE_LEFT ] -= s->slev * SQRT3_2;
275                 matrix[FRONT_LEFT ][SIDE_RIGHT] -= s->slev * M_SQRT1_2;
276                 matrix[FRONT_RIGHT][SIDE_LEFT ] += s->slev * M_SQRT1_2;
277                 matrix[FRONT_RIGHT][SIDE_RIGHT] += s->slev * SQRT3_2;
278             } else {
279                 matrix[ FRONT_LEFT][ SIDE_LEFT] += s->slev;
280                 matrix[FRONT_RIGHT][SIDE_RIGHT] += s->slev;
281             }
282         }else if(out_ch_layout & AV_CH_FRONT_CENTER){
283             matrix[ FRONT_CENTER][SIDE_LEFT ]+= s->slev*M_SQRT1_2;
284             matrix[ FRONT_CENTER][SIDE_RIGHT]+= s->slev*M_SQRT1_2;
285         }else
286             av_assert0(0);
287     }
288
289     if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
290         if(out_ch_layout & AV_CH_FRONT_LEFT){
291             matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
292             matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
293         }else if(out_ch_layout & AV_CH_FRONT_CENTER){
294             matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
295             matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
296         }else
297             av_assert0(0);
298     }
299     /* mix LFE into front left/right or center */
300     if (unaccounted & AV_CH_LOW_FREQUENCY) {
301         if (out_ch_layout & AV_CH_FRONT_CENTER) {
302             matrix[FRONT_CENTER][LOW_FREQUENCY] += s->lfe_mix_level;
303         } else if (out_ch_layout & AV_CH_FRONT_LEFT) {
304             matrix[FRONT_LEFT ][LOW_FREQUENCY] += s->lfe_mix_level * M_SQRT1_2;
305             matrix[FRONT_RIGHT][LOW_FREQUENCY] += s->lfe_mix_level * M_SQRT1_2;
306         } else
307             av_assert0(0);
308     }
309
310     for(out_i=i=0; i<64; i++){
311         double sum=0;
312         int in_i=0;
313         for(j=0; j<64; j++){
314             s->matrix[out_i][in_i]= matrix[i][j];
315             if(matrix[i][j]){
316                 sum += fabs(matrix[i][j]);
317             }
318             if(in_ch_layout & (1ULL<<j))
319                 in_i++;
320         }
321         maxcoef= FFMAX(maxcoef, sum);
322         if(out_ch_layout & (1ULL<<i))
323             out_i++;
324     }
325     if(s->rematrix_volume  < 0)
326         maxcoef = -s->rematrix_volume;
327
328     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) && maxcoef > 1.0){
330         for(i=0; i<SWR_CH_MAX; i++)
331             for(j=0; j<SWR_CH_MAX; j++){
332                 s->matrix[i][j] /= maxcoef;
333             }
334     }
335
336     if(s->rematrix_volume > 0){
337         for(i=0; i<SWR_CH_MAX; i++)
338             for(j=0; j<SWR_CH_MAX; j++){
339                 s->matrix[i][j] *= s->rematrix_volume;
340             }
341     }
342
343     for(i=0; i<av_get_channel_layout_nb_channels(out_ch_layout); i++){
344         for(j=0; j<av_get_channel_layout_nb_channels(in_ch_layout); j++){
345             av_log(NULL, AV_LOG_DEBUG, "%f ", s->matrix[i][j]);
346         }
347         av_log(NULL, AV_LOG_DEBUG, "\n");
348     }
349     return 0;
350 }
351
352 av_cold int swri_rematrix_init(SwrContext *s){
353     int i, j;
354     int nb_in  = av_get_channel_layout_nb_channels(s->in_ch_layout);
355     int nb_out = av_get_channel_layout_nb_channels(s->out_ch_layout);
356
357     s->mix_any_f = NULL;
358
359     if (!s->rematrix_custom) {
360         int r = auto_matrix(s);
361         if (r)
362             return r;
363     }
364     if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
365         s->native_matrix = av_mallocz(nb_in * nb_out * sizeof(int));
366         s->native_one    = av_mallocz(sizeof(int));
367         for (i = 0; i < nb_out; i++)
368             for (j = 0; j < nb_in; j++)
369                 ((int*)s->native_matrix)[i * nb_in + j] = lrintf(s->matrix[i][j] * 32768);
370         *((int*)s->native_one) = 32768;
371         s->mix_1_1_f = (mix_1_1_func_type*)copy_s16;
372         s->mix_2_1_f = (mix_2_1_func_type*)sum2_s16;
373         s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s16(s);
374     }else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
375         s->native_matrix = av_mallocz(nb_in * nb_out * sizeof(float));
376         s->native_one    = av_mallocz(sizeof(float));
377         for (i = 0; i < nb_out; i++)
378             for (j = 0; j < nb_in; j++)
379                 ((float*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
380         *((float*)s->native_one) = 1.0;
381         s->mix_1_1_f = (mix_1_1_func_type*)copy_float;
382         s->mix_2_1_f = (mix_2_1_func_type*)sum2_float;
383         s->mix_any_f = (mix_any_func_type*)get_mix_any_func_float(s);
384     }else if(s->midbuf.fmt == AV_SAMPLE_FMT_DBLP){
385         s->native_matrix = av_mallocz(nb_in * nb_out * sizeof(double));
386         s->native_one    = av_mallocz(sizeof(double));
387         for (i = 0; i < nb_out; i++)
388             for (j = 0; j < nb_in; j++)
389                 ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
390         *((double*)s->native_one) = 1.0;
391         s->mix_1_1_f = (mix_1_1_func_type*)copy_double;
392         s->mix_2_1_f = (mix_2_1_func_type*)sum2_double;
393         s->mix_any_f = (mix_any_func_type*)get_mix_any_func_double(s);
394     }else
395         av_assert0(0);
396     //FIXME quantize for integeres
397     for (i = 0; i < SWR_CH_MAX; i++) {
398         int ch_in=0;
399         for (j = 0; j < SWR_CH_MAX; j++) {
400             s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
401             if(s->matrix[i][j])
402                 s->matrix_ch[i][++ch_in]= j;
403         }
404         s->matrix_ch[i][0]= ch_in;
405     }
406
407     if(HAVE_YASM && HAVE_MMX) swri_rematrix_init_x86(s);
408
409     return 0;
410 }
411
412 av_cold void swri_rematrix_free(SwrContext *s){
413     av_freep(&s->native_matrix);
414     av_freep(&s->native_one);
415     av_freep(&s->native_simd_matrix);
416 }
417
418 int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
419     int out_i, in_i, i, j;
420     int len1 = 0;
421     int off = 0;
422
423     if(s->mix_any_f) {
424         s->mix_any_f(out->ch, (const uint8_t **)in->ch, s->native_matrix, len);
425         return 0;
426     }
427
428     if(s->mix_2_1_simd || s->mix_1_1_simd){
429         len1= len&~15;
430         off = len1 * out->bps;
431     }
432
433     av_assert0(out->ch_count == av_get_channel_layout_nb_channels(s->out_ch_layout));
434     av_assert0(in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
435
436     for(out_i=0; out_i<out->ch_count; out_i++){
437         switch(s->matrix_ch[out_i][0]){
438         case 0:
439             if(mustcopy)
440                 memset(out->ch[out_i], 0, len * av_get_bytes_per_sample(s->int_sample_fmt));
441             break;
442         case 1:
443             in_i= s->matrix_ch[out_i][1];
444             if(s->matrix[out_i][in_i]!=1.0){
445                 if(s->mix_1_1_simd && len1)
446                     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);
447                 if(len != len1)
448                     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);
449             }else if(mustcopy){
450                 memcpy(out->ch[out_i], in->ch[in_i], len*out->bps);
451             }else{
452                 out->ch[out_i]= in->ch[in_i];
453             }
454             break;
455         case 2: {
456             int in_i1 = s->matrix_ch[out_i][1];
457             int in_i2 = s->matrix_ch[out_i][2];
458             if(s->mix_2_1_simd && len1)
459                 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);
460             else
461                 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);
462             if(len != len1)
463                 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);
464             break;}
465         default:
466             if(s->int_sample_fmt == AV_SAMPLE_FMT_FLTP){
467                 for(i=0; i<len; i++){
468                     float v=0;
469                     for(j=0; j<s->matrix_ch[out_i][0]; j++){
470                         in_i= s->matrix_ch[out_i][1+j];
471                         v+= ((float*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
472                     }
473                     ((float*)out->ch[out_i])[i]= v;
474                 }
475             }else if(s->int_sample_fmt == AV_SAMPLE_FMT_DBLP){
476                 for(i=0; i<len; i++){
477                     double v=0;
478                     for(j=0; j<s->matrix_ch[out_i][0]; j++){
479                         in_i= s->matrix_ch[out_i][1+j];
480                         v+= ((double*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
481                     }
482                     ((double*)out->ch[out_i])[i]= v;
483                 }
484             }else{
485                 for(i=0; i<len; i++){
486                     int v=0;
487                     for(j=0; j<s->matrix_ch[out_i][0]; j++){
488                         in_i= s->matrix_ch[out_i][1+j];
489                         v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
490                     }
491                     ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;
492                 }
493             }
494         }
495     }
496     return 0;
497 }