]> git.sesse.net Git - ffmpeg/blob - libavresample/audio_mix.c
lavr: call mix_function_init() in ff_audio_mix_set_matrix()
[ffmpeg] / libavresample / audio_mix.c
1 /*
2  * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include <stdint.h>
22
23 #include "libavutil/common.h"
24 #include "libavutil/libm.h"
25 #include "libavutil/samplefmt.h"
26 #include "avresample.h"
27 #include "internal.h"
28 #include "audio_data.h"
29 #include "audio_mix.h"
30
31 static const char *coeff_type_names[] = { "q8", "q15", "flt" };
32
33 struct AudioMix {
34     AVAudioResampleContext *avr;
35     enum AVSampleFormat fmt;
36     enum AVMixCoeffType coeff_type;
37     uint64_t in_layout;
38     uint64_t out_layout;
39     int in_channels;
40     int out_channels;
41
42     int ptr_align;
43     int samples_align;
44     int has_optimized_func;
45     const char *func_descr;
46     const char *func_descr_generic;
47     mix_func *mix;
48     mix_func *mix_generic;
49
50     int in_matrix_channels;
51     int out_matrix_channels;
52     int output_zero[AVRESAMPLE_MAX_CHANNELS];
53     int input_skip[AVRESAMPLE_MAX_CHANNELS];
54     int output_skip[AVRESAMPLE_MAX_CHANNELS];
55     int16_t *matrix_q8[AVRESAMPLE_MAX_CHANNELS];
56     int32_t *matrix_q15[AVRESAMPLE_MAX_CHANNELS];
57     float   *matrix_flt[AVRESAMPLE_MAX_CHANNELS];
58     void   **matrix;
59 };
60
61 void ff_audio_mix_set_func(AudioMix *am, enum AVSampleFormat fmt,
62                            enum AVMixCoeffType coeff_type, int in_channels,
63                            int out_channels, int ptr_align, int samples_align,
64                            const char *descr, void *mix_func)
65 {
66     if (fmt == am->fmt && coeff_type == am->coeff_type &&
67         ( in_channels ==  am->in_matrix_channels ||  in_channels == 0) &&
68         (out_channels == am->out_matrix_channels || out_channels == 0)) {
69         char chan_str[16];
70         am->mix           = mix_func;
71         am->func_descr    = descr;
72         am->ptr_align     = ptr_align;
73         am->samples_align = samples_align;
74         if (ptr_align == 1 && samples_align == 1) {
75             am->mix_generic        = mix_func;
76             am->func_descr_generic = descr;
77         } else {
78             am->has_optimized_func = 1;
79         }
80         if (in_channels) {
81             if (out_channels)
82                 snprintf(chan_str, sizeof(chan_str), "[%d to %d] ",
83                          in_channels, out_channels);
84             else
85                 snprintf(chan_str, sizeof(chan_str), "[%d to any] ",
86                          in_channels);
87         } else if (out_channels) {
88                 snprintf(chan_str, sizeof(chan_str), "[any to %d] ",
89                          out_channels);
90         } else {
91             snprintf(chan_str, sizeof(chan_str), "[any to any] ");
92         }
93         av_log(am->avr, AV_LOG_DEBUG, "audio_mix: found function: [fmt=%s] "
94                "[c=%s] %s(%s)\n", av_get_sample_fmt_name(fmt),
95                coeff_type_names[coeff_type], chan_str, descr);
96     }
97 }
98
99 #define MIX_FUNC_NAME(fmt, cfmt) mix_any_ ## fmt ##_## cfmt ##_c
100
101 #define MIX_FUNC_GENERIC(fmt, cfmt, stype, ctype, sumtype, expr)            \
102 static void MIX_FUNC_NAME(fmt, cfmt)(stype **samples, ctype **matrix,       \
103                                      int len, int out_ch, int in_ch)        \
104 {                                                                           \
105     int i, in, out;                                                         \
106     stype temp[AVRESAMPLE_MAX_CHANNELS];                                    \
107     for (i = 0; i < len; i++) {                                             \
108         for (out = 0; out < out_ch; out++) {                                \
109             sumtype sum = 0;                                                \
110             for (in = 0; in < in_ch; in++)                                  \
111                 sum += samples[in][i] * matrix[out][in];                    \
112             temp[out] = expr;                                               \
113         }                                                                   \
114         for (out = 0; out < out_ch; out++)                                  \
115             samples[out][i] = temp[out];                                    \
116     }                                                                       \
117 }
118
119 MIX_FUNC_GENERIC(FLTP, FLT, float,   float,   float,   sum)
120 MIX_FUNC_GENERIC(S16P, FLT, int16_t, float,   float,   av_clip_int16(lrintf(sum)))
121 MIX_FUNC_GENERIC(S16P, Q15, int16_t, int32_t, int64_t, av_clip_int16(sum >> 15))
122 MIX_FUNC_GENERIC(S16P, Q8,  int16_t, int16_t, int32_t, av_clip_int16(sum >>  8))
123
124 /* TODO: templatize the channel-specific C functions */
125
126 static void mix_2_to_1_fltp_flt_c(float **samples, float **matrix, int len,
127                                   int out_ch, int in_ch)
128 {
129     float *src0 = samples[0];
130     float *src1 = samples[1];
131     float *dst  = src0;
132     float m0    = matrix[0][0];
133     float m1    = matrix[0][1];
134
135     while (len > 4) {
136         *dst++ = *src0++ * m0 + *src1++ * m1;
137         *dst++ = *src0++ * m0 + *src1++ * m1;
138         *dst++ = *src0++ * m0 + *src1++ * m1;
139         *dst++ = *src0++ * m0 + *src1++ * m1;
140         len -= 4;
141     }
142     while (len > 0) {
143         *dst++ = *src0++ * m0 + *src1++ * m1;
144         len--;
145     }
146 }
147
148 static void mix_2_to_1_s16p_flt_c(int16_t **samples, float **matrix, int len,
149                                   int out_ch, int in_ch)
150 {
151     int16_t *src0 = samples[0];
152     int16_t *src1 = samples[1];
153     int16_t *dst  = src0;
154     float m0      = matrix[0][0];
155     float m1      = matrix[0][1];
156
157     while (len > 4) {
158         *dst++ = av_clip_int16(lrintf(*src0++ * m0 + *src1++ * m1));
159         *dst++ = av_clip_int16(lrintf(*src0++ * m0 + *src1++ * m1));
160         *dst++ = av_clip_int16(lrintf(*src0++ * m0 + *src1++ * m1));
161         *dst++ = av_clip_int16(lrintf(*src0++ * m0 + *src1++ * m1));
162         len -= 4;
163     }
164     while (len > 0) {
165         *dst++ = av_clip_int16(lrintf(*src0++ * m0 + *src1++ * m1));
166         len--;
167     }
168 }
169
170 static void mix_2_to_1_s16p_q8_c(int16_t **samples, int16_t **matrix, int len,
171                                  int out_ch, int in_ch)
172 {
173     int16_t *src0 = samples[0];
174     int16_t *src1 = samples[1];
175     int16_t *dst  = src0;
176     int16_t m0    = matrix[0][0];
177     int16_t m1    = matrix[0][1];
178
179     while (len > 4) {
180         *dst++ = (*src0++ * m0 + *src1++ * m1) >> 8;
181         *dst++ = (*src0++ * m0 + *src1++ * m1) >> 8;
182         *dst++ = (*src0++ * m0 + *src1++ * m1) >> 8;
183         *dst++ = (*src0++ * m0 + *src1++ * m1) >> 8;
184         len -= 4;
185     }
186     while (len > 0) {
187         *dst++ = (*src0++ * m0 + *src1++ * m1) >> 8;
188         len--;
189     }
190 }
191
192 static void mix_1_to_2_fltp_flt_c(float **samples, float **matrix, int len,
193                                   int out_ch, int in_ch)
194 {
195     float v;
196     float *dst0 = samples[0];
197     float *dst1 = samples[1];
198     float *src  = dst0;
199     float m0    = matrix[0][0];
200     float m1    = matrix[1][0];
201
202     while (len > 4) {
203         v = *src++;
204         *dst0++ = v * m1;
205         *dst1++ = v * m0;
206         v = *src++;
207         *dst0++ = v * m1;
208         *dst1++ = v * m0;
209         v = *src++;
210         *dst0++ = v * m1;
211         *dst1++ = v * m0;
212         v = *src++;
213         *dst0++ = v * m1;
214         *dst1++ = v * m0;
215         len -= 4;
216     }
217     while (len > 0) {
218         v = *src++;
219         *dst0++ = v * m1;
220         *dst1++ = v * m0;
221         len--;
222     }
223 }
224
225 static void mix_6_to_2_fltp_flt_c(float **samples, float **matrix, int len,
226                                   int out_ch, int in_ch)
227 {
228     float v0, v1;
229     float *src0 = samples[0];
230     float *src1 = samples[1];
231     float *src2 = samples[2];
232     float *src3 = samples[3];
233     float *src4 = samples[4];
234     float *src5 = samples[5];
235     float *dst0 = src0;
236     float *dst1 = src1;
237     float *m0   = matrix[0];
238     float *m1   = matrix[1];
239
240     while (len > 0) {
241         v0 = *src0++;
242         v1 = *src1++;
243         *dst0++ = v0      * m0[0] +
244                   v1      * m0[1] +
245                   *src2   * m0[2] +
246                   *src3   * m0[3] +
247                   *src4   * m0[4] +
248                   *src5   * m0[5];
249         *dst1++ = v0      * m1[0] +
250                   v1      * m1[1] +
251                   *src2++ * m1[2] +
252                   *src3++ * m1[3] +
253                   *src4++ * m1[4] +
254                   *src5++ * m1[5];
255         len--;
256     }
257 }
258
259 static void mix_2_to_6_fltp_flt_c(float **samples, float **matrix, int len,
260                                   int out_ch, int in_ch)
261 {
262     float v0, v1;
263     float *dst0 = samples[0];
264     float *dst1 = samples[1];
265     float *dst2 = samples[2];
266     float *dst3 = samples[3];
267     float *dst4 = samples[4];
268     float *dst5 = samples[5];
269     float *src0 = dst0;
270     float *src1 = dst1;
271
272     while (len > 0) {
273         v0 = *src0++;
274         v1 = *src1++;
275         *dst0++ = v0 * matrix[0][0] + v1 * matrix[0][1];
276         *dst1++ = v0 * matrix[1][0] + v1 * matrix[1][1];
277         *dst2++ = v0 * matrix[2][0] + v1 * matrix[2][1];
278         *dst3++ = v0 * matrix[3][0] + v1 * matrix[3][1];
279         *dst4++ = v0 * matrix[4][0] + v1 * matrix[4][1];
280         *dst5++ = v0 * matrix[5][0] + v1 * matrix[5][1];
281         len--;
282     }
283 }
284
285 static int mix_function_init(AudioMix *am)
286 {
287     /* no need to set a mix function when we're skipping mixing */
288     if (!am->in_matrix_channels || !am->out_matrix_channels) {
289         am->func_descr = "n/a";
290         return 0;
291     }
292
293     /* any-to-any C versions */
294
295     ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
296                           0, 0, 1, 1, "C", MIX_FUNC_NAME(FLTP, FLT));
297
298     ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,
299                           0, 0, 1, 1, "C", MIX_FUNC_NAME(S16P, FLT));
300
301     ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_Q15,
302                           0, 0, 1, 1, "C", MIX_FUNC_NAME(S16P, Q15));
303
304     ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_Q8,
305                           0, 0, 1, 1, "C", MIX_FUNC_NAME(S16P, Q8));
306
307     /* channel-specific C versions */
308
309     ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
310                           2, 1, 1, 1, "C", mix_2_to_1_fltp_flt_c);
311
312     ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,
313                           2, 1, 1, 1, "C", mix_2_to_1_s16p_flt_c);
314
315     ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_Q8,
316                           2, 1, 1, 1, "C", mix_2_to_1_s16p_q8_c);
317
318     ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
319                           1, 2, 1, 1, "C", mix_1_to_2_fltp_flt_c);
320
321     ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
322                           6, 2, 1, 1, "C", mix_6_to_2_fltp_flt_c);
323
324     ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
325                           2, 6, 1, 1, "C", mix_2_to_6_fltp_flt_c);
326
327     if (ARCH_X86)
328         ff_audio_mix_init_x86(am);
329
330     if (!am->mix) {
331         av_log(am->avr, AV_LOG_ERROR, "audio_mix: NO FUNCTION FOUND: [fmt=%s] "
332                "[c=%s] [%d to %d]\n", av_get_sample_fmt_name(am->fmt),
333                coeff_type_names[am->coeff_type], am->in_channels,
334                am->out_channels);
335         return AVERROR_PATCHWELCOME;
336     }
337     return 0;
338 }
339
340 AudioMix *ff_audio_mix_alloc(AVAudioResampleContext *avr)
341 {
342     AudioMix *am;
343     int ret;
344
345     am = av_mallocz(sizeof(*am));
346     if (!am)
347         return NULL;
348     am->avr = avr;
349
350     if (avr->internal_sample_fmt != AV_SAMPLE_FMT_S16P &&
351         avr->internal_sample_fmt != AV_SAMPLE_FMT_FLTP) {
352         av_log(avr, AV_LOG_ERROR, "Unsupported internal format for "
353                "mixing: %s\n",
354                av_get_sample_fmt_name(avr->internal_sample_fmt));
355         goto error;
356     }
357
358     am->fmt          = avr->internal_sample_fmt;
359     am->coeff_type   = avr->mix_coeff_type;
360     am->in_layout    = avr->in_channel_layout;
361     am->out_layout   = avr->out_channel_layout;
362     am->in_channels  = avr->in_channels;
363     am->out_channels = avr->out_channels;
364
365     /* build matrix if the user did not already set one */
366     if (avr->mix_matrix) {
367         ret = ff_audio_mix_set_matrix(am, avr->mix_matrix, avr->in_channels);
368         if (ret < 0)
369             goto error;
370         av_freep(&avr->mix_matrix);
371     } else {
372         int i, j;
373         char in_layout_name[128];
374         char out_layout_name[128];
375         double *matrix_dbl = av_mallocz(avr->out_channels * avr->in_channels *
376                                         sizeof(*matrix_dbl));
377         if (!matrix_dbl)
378             goto error;
379
380         ret = avresample_build_matrix(avr->in_channel_layout,
381                                       avr->out_channel_layout,
382                                       avr->center_mix_level,
383                                       avr->surround_mix_level,
384                                       avr->lfe_mix_level,
385                                       avr->normalize_mix_level,
386                                       matrix_dbl,
387                                       avr->in_channels,
388                                       avr->matrix_encoding);
389         if (ret < 0) {
390             av_free(matrix_dbl);
391             goto error;
392         }
393
394         ret = ff_audio_mix_set_matrix(am, matrix_dbl, avr->in_channels);
395         if (ret < 0) {
396             av_log(avr, AV_LOG_ERROR, "error setting mix matrix\n");
397             av_free(matrix_dbl);
398             goto error;
399         }
400
401         av_get_channel_layout_string(in_layout_name, sizeof(in_layout_name),
402                                      avr->in_channels, avr->in_channel_layout);
403         av_get_channel_layout_string(out_layout_name, sizeof(out_layout_name),
404                                      avr->out_channels, avr->out_channel_layout);
405         av_log(avr, AV_LOG_DEBUG, "audio_mix: %s to %s\n",
406                in_layout_name, out_layout_name);
407         av_log(avr, AV_LOG_DEBUG, "matrix size: %d x %d\n",
408                am->in_matrix_channels, am->out_matrix_channels);
409         for (i = 0; i < avr->out_channels; i++) {
410             for (j = 0; j < avr->in_channels; j++) {
411                 if (am->output_zero[i])
412                     av_log(avr, AV_LOG_DEBUG, "  (ZERO)");
413                 else if (am->input_skip[j] || am->output_skip[i])
414                     av_log(avr, AV_LOG_DEBUG, "  (SKIP)");
415                 else
416                     av_log(avr, AV_LOG_DEBUG, "  %0.3f ",
417                            matrix_dbl[i * avr->in_channels + j]);
418             }
419             av_log(avr, AV_LOG_DEBUG, "\n");
420         }
421
422         av_free(matrix_dbl);
423     }
424
425     return am;
426
427 error:
428     av_free(am);
429     return NULL;
430 }
431
432 void ff_audio_mix_free(AudioMix **am_p)
433 {
434     AudioMix *am;
435
436     if (!*am_p)
437         return;
438     am = *am_p;
439
440     if (am->matrix) {
441         av_free(am->matrix[0]);
442         am->matrix = NULL;
443     }
444     memset(am->matrix_q8,  0, sizeof(am->matrix_q8 ));
445     memset(am->matrix_q15, 0, sizeof(am->matrix_q15));
446     memset(am->matrix_flt, 0, sizeof(am->matrix_flt));
447
448     av_freep(am_p);
449 }
450
451 int ff_audio_mix(AudioMix *am, AudioData *src)
452 {
453     int use_generic = 1;
454     int len = src->nb_samples;
455     int i, j;
456
457     /* determine whether to use the optimized function based on pointer and
458        samples alignment in both the input and output */
459     if (am->has_optimized_func) {
460         int aligned_len = FFALIGN(len, am->samples_align);
461         if (!(src->ptr_align % am->ptr_align) &&
462             src->samples_align >= aligned_len) {
463             len = aligned_len;
464             use_generic = 0;
465         }
466     }
467     av_dlog(am->avr, "audio_mix: %d samples - %d to %d channels (%s)\n",
468             src->nb_samples, am->in_channels, am->out_channels,
469             use_generic ? am->func_descr_generic : am->func_descr);
470
471     if (am->in_matrix_channels && am->out_matrix_channels) {
472         uint8_t **data;
473         uint8_t *data0[AVRESAMPLE_MAX_CHANNELS];
474
475         if (am->out_matrix_channels < am->out_channels ||
476              am->in_matrix_channels <  am->in_channels) {
477             for (i = 0, j = 0; i < FFMAX(am->in_channels, am->out_channels); i++) {
478                 if (am->input_skip[i] || am->output_skip[i] || am->output_zero[i])
479                     continue;
480                 data0[j++] = src->data[i];
481             }
482             data = data0;
483         } else {
484             data = src->data;
485         }
486
487         if (use_generic)
488             am->mix_generic(data, am->matrix, len, am->out_matrix_channels,
489                             am->in_matrix_channels);
490         else
491             am->mix(data, am->matrix, len, am->out_matrix_channels,
492                     am->in_matrix_channels);
493     }
494
495     if (am->out_matrix_channels < am->out_channels) {
496         for (i = 0; i < am->out_channels; i++)
497             if (am->output_zero[i])
498                 av_samples_set_silence(&src->data[i], 0, len, 1, am->fmt);
499     }
500
501     ff_audio_data_set_channels(src, am->out_channels);
502
503     return 0;
504 }
505
506 int ff_audio_mix_get_matrix(AudioMix *am, double *matrix, int stride)
507 {
508     int i, o, i0, o0;
509
510     if ( am->in_channels <= 0 ||  am->in_channels > AVRESAMPLE_MAX_CHANNELS ||
511         am->out_channels <= 0 || am->out_channels > AVRESAMPLE_MAX_CHANNELS) {
512         av_log(am->avr, AV_LOG_ERROR, "Invalid channel counts\n");
513         return AVERROR(EINVAL);
514     }
515
516 #define GET_MATRIX_CONVERT(suffix, scale)                                   \
517     if (!am->matrix_ ## suffix[0]) {                                        \
518         av_log(am->avr, AV_LOG_ERROR, "matrix is not set\n");               \
519         return AVERROR(EINVAL);                                             \
520     }                                                                       \
521     for (o = 0, o0 = 0; o < am->out_channels; o++) {                        \
522         for (i = 0, i0 = 0; i < am->in_channels; i++) {                     \
523             if (am->input_skip[i] || am->output_zero[o])                    \
524                 matrix[o * stride + i] = 0.0;                               \
525             else                                                            \
526                 matrix[o * stride + i] = am->matrix_ ## suffix[o0][i0] *    \
527                                          (scale);                           \
528             if (!am->input_skip[i])                                         \
529                 i0++;                                                       \
530         }                                                                   \
531         if (!am->output_zero[o])                                            \
532             o0++;                                                           \
533     }
534
535     switch (am->coeff_type) {
536     case AV_MIX_COEFF_TYPE_Q8:
537         GET_MATRIX_CONVERT(q8, 1.0 / 256.0);
538         break;
539     case AV_MIX_COEFF_TYPE_Q15:
540         GET_MATRIX_CONVERT(q15, 1.0 / 32768.0);
541         break;
542     case AV_MIX_COEFF_TYPE_FLT:
543         GET_MATRIX_CONVERT(flt, 1.0);
544         break;
545     default:
546         av_log(am->avr, AV_LOG_ERROR, "Invalid mix coeff type\n");
547         return AVERROR(EINVAL);
548     }
549
550     return 0;
551 }
552
553 int ff_audio_mix_set_matrix(AudioMix *am, const double *matrix, int stride)
554 {
555     int i, o, i0, o0;
556
557     if ( am->in_channels <= 0 ||  am->in_channels > AVRESAMPLE_MAX_CHANNELS ||
558         am->out_channels <= 0 || am->out_channels > AVRESAMPLE_MAX_CHANNELS) {
559         av_log(am->avr, AV_LOG_ERROR, "Invalid channel counts\n");
560         return AVERROR(EINVAL);
561     }
562
563     if (am->matrix) {
564         av_free(am->matrix[0]);
565         am->matrix = NULL;
566     }
567
568     memset(am->output_zero, 0, sizeof(am->output_zero));
569     memset(am->input_skip,  0, sizeof(am->input_skip));
570     memset(am->output_skip, 0, sizeof(am->output_zero));
571     am->in_matrix_channels  = am->in_channels;
572     am->out_matrix_channels = am->out_channels;
573
574     /* exclude output channels if they can be zeroed instead of mixed */
575     for (o = 0; o < am->out_channels; o++) {
576         int zero = 1;
577
578         /* check if the output is always silent */
579         for (i = 0; i < am->in_channels; i++) {
580             if (matrix[o * stride + i] != 0.0) {
581                 zero = 0;
582                 break;
583             }
584         }
585         /* check if the corresponding input channel makes a contribution to
586            any output channel */
587         if (o < am->in_channels) {
588             for (i = 0; i < am->out_channels; i++) {
589                 if (matrix[i * stride + o] != 0.0) {
590                     zero = 0;
591                     break;
592                 }
593             }
594         }
595         if (zero) {
596             am->output_zero[o] = 1;
597             am->out_matrix_channels--;
598         }
599     }
600     if (am->out_matrix_channels == 0) {
601         am->in_matrix_channels = 0;
602         return 0;
603     }
604
605     /* skip input channels that contribute fully only to the corresponding
606        output channel */
607     for (i = 0; i < FFMIN(am->in_channels, am->out_channels); i++) {
608         int skip = 1;
609
610         for (o = 0; o < am->out_channels; o++) {
611             if ((o != i && matrix[o * stride + i] != 0.0) ||
612                 (o == i && matrix[o * stride + i] != 1.0)) {
613                 skip = 0;
614                 break;
615             }
616         }
617         if (skip) {
618             am->input_skip[i] = 1;
619             am->in_matrix_channels--;
620         }
621     }
622     /* skip input channels that do not contribute to any output channel */
623     for (; i < am->in_channels; i++) {
624         int contrib = 0;
625
626         for (o = 0; o < am->out_channels; o++) {
627             if (matrix[o * stride + i] != 0.0) {
628                 contrib = 1;
629                 break;
630             }
631         }
632         if (!contrib) {
633             am->input_skip[i] = 1;
634             am->in_matrix_channels--;
635         }
636     }
637     if (am->in_matrix_channels == 0) {
638         am->out_matrix_channels = 0;
639         return 0;
640     }
641
642     /* skip output channels that only get full contribution from the
643        corresponding input channel */
644     for (o = 0; o < FFMIN(am->in_channels, am->out_channels); o++) {
645         int skip = 1;
646
647         for (i = 0; i < am->in_channels; i++) {
648             if ((o != i && matrix[o * stride + i] != 0.0) ||
649                 (o == i && matrix[o * stride + i] != 1.0)) {
650                 skip = 0;
651                 break;
652             }
653         }
654         if (skip) {
655             am->output_skip[o] = 1;
656             am->out_matrix_channels--;
657         }
658     }
659     if (am->out_matrix_channels == 0) {
660         am->in_matrix_channels = 0;
661         return 0;
662     }
663
664 #define CONVERT_MATRIX(type, expr)                                          \
665     am->matrix_## type[0] = av_mallocz(am->out_matrix_channels *            \
666                                        am->in_matrix_channels  *            \
667                                        sizeof(*am->matrix_## type[0]));     \
668     if (!am->matrix_## type[0])                                             \
669         return AVERROR(ENOMEM);                                             \
670     for (o = 0, o0 = 0; o < am->out_channels; o++) {                        \
671         if (am->output_zero[o] || am->output_skip[o])                       \
672             continue;                                                       \
673         if (o0 > 0)                                                         \
674             am->matrix_## type[o0] = am->matrix_## type[o0 - 1] +           \
675                                      am->in_matrix_channels;                \
676         for (i = 0, i0 = 0; i < am->in_channels; i++) {                     \
677             double v;                                                       \
678             if (am->input_skip[i])                                          \
679                 continue;                                                   \
680             v = matrix[o * stride + i];                                     \
681             am->matrix_## type[o0][i0] = expr;                              \
682             i0++;                                                           \
683         }                                                                   \
684         o0++;                                                               \
685     }                                                                       \
686     am->matrix = (void **)am->matrix_## type;
687
688     switch (am->coeff_type) {
689     case AV_MIX_COEFF_TYPE_Q8:
690         CONVERT_MATRIX(q8, av_clip_int16(lrint(256.0 * v)))
691         break;
692     case AV_MIX_COEFF_TYPE_Q15:
693         CONVERT_MATRIX(q15, av_clipl_int32(llrint(32768.0 * v)))
694         break;
695     case AV_MIX_COEFF_TYPE_FLT:
696         CONVERT_MATRIX(flt, v)
697         break;
698     default:
699         av_log(am->avr, AV_LOG_ERROR, "Invalid mix coeff type\n");
700         return AVERROR(EINVAL);
701     }
702
703     return mix_function_init(am);
704 }