]> git.sesse.net Git - ffmpeg/blob - libavresample/audio_mix.c
Merge commit '14758e3211d34a97c42b07acae117ce5627d7f57'
[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 void ff_audio_mix_set_func(AudioMix *am, enum AVSampleFormat fmt,
34                            enum AVMixCoeffType coeff_type, int in_channels,
35                            int out_channels, int ptr_align, int samples_align,
36                            const char *descr, void *mix_func)
37 {
38     if (fmt == am->fmt && coeff_type == am->coeff_type &&
39         ( in_channels ==  am->in_channels ||  in_channels == 0) &&
40         (out_channels == am->out_channels || out_channels == 0)) {
41         char chan_str[16];
42         am->mix           = mix_func;
43         am->func_descr    = descr;
44         am->ptr_align     = ptr_align;
45         am->samples_align = samples_align;
46         if (ptr_align == 1 && samples_align == 1) {
47             am->mix_generic        = mix_func;
48             am->func_descr_generic = descr;
49         } else {
50             am->has_optimized_func = 1;
51         }
52         if (in_channels) {
53             if (out_channels)
54                 snprintf(chan_str, sizeof(chan_str), "[%d to %d] ",
55                          in_channels, out_channels);
56             else
57                 snprintf(chan_str, sizeof(chan_str), "[%d to any] ",
58                          in_channels);
59         } else if (out_channels) {
60                 snprintf(chan_str, sizeof(chan_str), "[any to %d] ",
61                          out_channels);
62         }
63         av_log(am->avr, AV_LOG_DEBUG, "audio_mix: found function: [fmt=%s] "
64                "[c=%s] %s(%s)\n", av_get_sample_fmt_name(fmt),
65                coeff_type_names[coeff_type],
66                (in_channels || out_channels) ? chan_str : "", descr);
67     }
68 }
69
70 #define MIX_FUNC_NAME(fmt, cfmt) mix_any_ ## fmt ##_## cfmt ##_c
71
72 #define MIX_FUNC_GENERIC(fmt, cfmt, stype, ctype, sumtype, expr)            \
73 static void MIX_FUNC_NAME(fmt, cfmt)(stype **samples, ctype **matrix,       \
74                                      int len, int out_ch, int in_ch)        \
75 {                                                                           \
76     int i, in, out;                                                         \
77     stype temp[AVRESAMPLE_MAX_CHANNELS];                                    \
78     for (i = 0; i < len; i++) {                                             \
79         for (out = 0; out < out_ch; out++) {                                \
80             sumtype sum = 0;                                                \
81             for (in = 0; in < in_ch; in++)                                  \
82                 sum += samples[in][i] * matrix[out][in];                    \
83             temp[out] = expr;                                               \
84         }                                                                   \
85         for (out = 0; out < out_ch; out++)                                  \
86             samples[out][i] = temp[out];                                    \
87     }                                                                       \
88 }
89
90 MIX_FUNC_GENERIC(FLTP, FLT, float,   float,   float,   sum)
91 MIX_FUNC_GENERIC(S16P, FLT, int16_t, float,   float,   av_clip_int16(lrintf(sum)))
92 MIX_FUNC_GENERIC(S16P, Q15, int16_t, int32_t, int64_t, av_clip_int16(sum >> 15))
93 MIX_FUNC_GENERIC(S16P, Q8,  int16_t, int16_t, int32_t, av_clip_int16(sum >>  8))
94
95 /* TODO: templatize the channel-specific C functions */
96
97 static void mix_2_to_1_fltp_flt_c(float **samples, float **matrix, int len,
98                                   int out_ch, int in_ch)
99 {
100     float *src0 = samples[0];
101     float *src1 = samples[1];
102     float *dst  = src0;
103     float m0    = matrix[0][0];
104     float m1    = matrix[0][1];
105
106     while (len > 4) {
107         *dst++ = *src0++ * m0 + *src1++ * m1;
108         *dst++ = *src0++ * m0 + *src1++ * m1;
109         *dst++ = *src0++ * m0 + *src1++ * m1;
110         *dst++ = *src0++ * m0 + *src1++ * m1;
111         len -= 4;
112     }
113     while (len > 0) {
114         *dst++ = *src0++ * m0 + *src1++ * m1;
115         len--;
116     }
117 }
118
119 static void mix_2_to_1_s16p_flt_c(int16_t **samples, float **matrix, int len,
120                                   int out_ch, int in_ch)
121 {
122     int16_t *src0 = samples[0];
123     int16_t *src1 = samples[1];
124     int16_t *dst  = src0;
125     float m0      = matrix[0][0];
126     float m1      = matrix[0][1];
127
128     while (len > 4) {
129         *dst++ = av_clip_int16(lrintf(*src0++ * m0 + *src1++ * m1));
130         *dst++ = av_clip_int16(lrintf(*src0++ * m0 + *src1++ * m1));
131         *dst++ = av_clip_int16(lrintf(*src0++ * m0 + *src1++ * m1));
132         *dst++ = av_clip_int16(lrintf(*src0++ * m0 + *src1++ * m1));
133         len -= 4;
134     }
135     while (len > 0) {
136         *dst++ = av_clip_int16(lrintf(*src0++ * m0 + *src1++ * m1));
137         len--;
138     }
139 }
140
141 static void mix_2_to_1_s16p_q8_c(int16_t **samples, int16_t **matrix, int len,
142                                  int out_ch, int in_ch)
143 {
144     int16_t *src0 = samples[0];
145     int16_t *src1 = samples[1];
146     int16_t *dst  = src0;
147     int16_t m0    = matrix[0][0];
148     int16_t m1    = matrix[0][1];
149
150     while (len > 4) {
151         *dst++ = (*src0++ * m0 + *src1++ * m1) >> 8;
152         *dst++ = (*src0++ * m0 + *src1++ * m1) >> 8;
153         *dst++ = (*src0++ * m0 + *src1++ * m1) >> 8;
154         *dst++ = (*src0++ * m0 + *src1++ * m1) >> 8;
155         len -= 4;
156     }
157     while (len > 0) {
158         *dst++ = (*src0++ * m0 + *src1++ * m1) >> 8;
159         len--;
160     }
161 }
162
163 static void mix_1_to_2_fltp_flt_c(float **samples, float **matrix, int len,
164                                   int out_ch, int in_ch)
165 {
166     float v;
167     float *dst0 = samples[0];
168     float *dst1 = samples[1];
169     float *src  = dst0;
170     float m0    = matrix[0][0];
171     float m1    = matrix[1][0];
172
173     while (len > 4) {
174         v = *src++;
175         *dst0++ = v * m1;
176         *dst1++ = v * m0;
177         v = *src++;
178         *dst0++ = v * m1;
179         *dst1++ = v * m0;
180         v = *src++;
181         *dst0++ = v * m1;
182         *dst1++ = v * m0;
183         v = *src++;
184         *dst0++ = v * m1;
185         *dst1++ = v * m0;
186         len -= 4;
187     }
188     while (len > 0) {
189         v = *src++;
190         *dst0++ = v * m1;
191         *dst1++ = v * m0;
192         len--;
193     }
194 }
195
196 static void mix_6_to_2_fltp_flt_c(float **samples, float **matrix, int len,
197                                   int out_ch, int in_ch)
198 {
199     float v0, v1;
200     float *src0 = samples[0];
201     float *src1 = samples[1];
202     float *src2 = samples[2];
203     float *src3 = samples[3];
204     float *src4 = samples[4];
205     float *src5 = samples[5];
206     float *dst0 = src0;
207     float *dst1 = src1;
208     float *m0   = matrix[0];
209     float *m1   = matrix[1];
210
211     while (len > 0) {
212         v0 = *src0++;
213         v1 = *src1++;
214         *dst0++ = v0      * m0[0] +
215                   v1      * m0[1] +
216                   *src2   * m0[2] +
217                   *src3   * m0[3] +
218                   *src4   * m0[4] +
219                   *src5   * m0[5];
220         *dst1++ = v0      * m1[0] +
221                   v1      * m1[1] +
222                   *src2++ * m1[2] +
223                   *src3++ * m1[3] +
224                   *src4++ * m1[4] +
225                   *src5++ * m1[5];
226         len--;
227     }
228 }
229
230 static void mix_2_to_6_fltp_flt_c(float **samples, float **matrix, int len,
231                                   int out_ch, int in_ch)
232 {
233     float v0, v1;
234     float *dst0 = samples[0];
235     float *dst1 = samples[1];
236     float *dst2 = samples[2];
237     float *dst3 = samples[3];
238     float *dst4 = samples[4];
239     float *dst5 = samples[5];
240     float *src0 = dst0;
241     float *src1 = dst1;
242
243     while (len > 0) {
244         v0 = *src0++;
245         v1 = *src1++;
246         *dst0++ = v0 * matrix[0][0] + v1 * matrix[0][1];
247         *dst1++ = v0 * matrix[1][0] + v1 * matrix[1][1];
248         *dst2++ = v0 * matrix[2][0] + v1 * matrix[2][1];
249         *dst3++ = v0 * matrix[3][0] + v1 * matrix[3][1];
250         *dst4++ = v0 * matrix[4][0] + v1 * matrix[4][1];
251         *dst5++ = v0 * matrix[5][0] + v1 * matrix[5][1];
252         len--;
253     }
254 }
255
256 static int mix_function_init(AudioMix *am)
257 {
258     /* any-to-any C versions */
259
260     ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
261                           0, 0, 1, 1, "C", MIX_FUNC_NAME(FLTP, FLT));
262
263     ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,
264                           0, 0, 1, 1, "C", MIX_FUNC_NAME(S16P, FLT));
265
266     ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_Q15,
267                           0, 0, 1, 1, "C", MIX_FUNC_NAME(S16P, Q15));
268
269     ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_Q8,
270                           0, 0, 1, 1, "C", MIX_FUNC_NAME(S16P, Q8));
271
272     /* channel-specific C versions */
273
274     ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
275                           2, 1, 1, 1, "C", mix_2_to_1_fltp_flt_c);
276
277     ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,
278                           2, 1, 1, 1, "C", mix_2_to_1_s16p_flt_c);
279
280     ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_Q8,
281                           2, 1, 1, 1, "C", mix_2_to_1_s16p_q8_c);
282
283     ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
284                           1, 2, 1, 1, "C", mix_1_to_2_fltp_flt_c);
285
286     ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
287                           6, 2, 1, 1, "C", mix_6_to_2_fltp_flt_c);
288
289     ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
290                           2, 6, 1, 1, "C", mix_2_to_6_fltp_flt_c);
291
292     if (ARCH_X86)
293         ff_audio_mix_init_x86(am);
294
295     if (!am->mix) {
296         av_log(am->avr, AV_LOG_ERROR, "audio_mix: NO FUNCTION FOUND: [fmt=%s] "
297                "[c=%s] [%d to %d]\n", av_get_sample_fmt_name(am->fmt),
298                coeff_type_names[am->coeff_type], am->in_channels,
299                am->out_channels);
300         return AVERROR_PATCHWELCOME;
301     }
302     return 0;
303 }
304
305 AudioMix *ff_audio_mix_alloc(AVAudioResampleContext *avr)
306 {
307     AudioMix *am;
308     int ret;
309
310     am = av_mallocz(sizeof(*am));
311     if (!am)
312         return NULL;
313     am->avr = avr;
314
315     if (avr->internal_sample_fmt != AV_SAMPLE_FMT_S16P &&
316         avr->internal_sample_fmt != AV_SAMPLE_FMT_FLTP) {
317         av_log(avr, AV_LOG_ERROR, "Unsupported internal format for "
318                "mixing: %s\n",
319                av_get_sample_fmt_name(avr->internal_sample_fmt));
320         goto error;
321     }
322
323     am->fmt          = avr->internal_sample_fmt;
324     am->coeff_type   = avr->mix_coeff_type;
325     am->in_layout    = avr->in_channel_layout;
326     am->out_layout   = avr->out_channel_layout;
327     am->in_channels  = avr->in_channels;
328     am->out_channels = avr->out_channels;
329
330     /* build matrix if the user did not already set one */
331     if (avr->mix_matrix) {
332         ret = ff_audio_mix_set_matrix(am, avr->mix_matrix, avr->in_channels);
333         if (ret < 0)
334             goto error;
335         av_freep(&avr->mix_matrix);
336     } else {
337         int i, j;
338         char in_layout_name[128];
339         char out_layout_name[128];
340         double *matrix_dbl = av_mallocz(avr->out_channels * avr->in_channels *
341                                         sizeof(*matrix_dbl));
342         if (!matrix_dbl)
343             goto error;
344
345         ret = avresample_build_matrix(avr->in_channel_layout,
346                                       avr->out_channel_layout,
347                                       avr->center_mix_level,
348                                       avr->surround_mix_level,
349                                       avr->lfe_mix_level,
350                                       avr->normalize_mix_level,
351                                       matrix_dbl,
352                                       avr->in_channels,
353                                       avr->matrix_encoding);
354         if (ret < 0) {
355             av_free(matrix_dbl);
356             goto error;
357         }
358
359         av_get_channel_layout_string(in_layout_name, sizeof(in_layout_name),
360                                      avr->in_channels, avr->in_channel_layout);
361         av_get_channel_layout_string(out_layout_name, sizeof(out_layout_name),
362                                      avr->out_channels, avr->out_channel_layout);
363         av_log(avr, AV_LOG_DEBUG, "audio_mix: %s to %s\n",
364                in_layout_name, out_layout_name);
365         for (i = 0; i < avr->out_channels; i++) {
366             for (j = 0; j < avr->in_channels; j++) {
367                 av_log(avr, AV_LOG_DEBUG, "  %0.3f ",
368                        matrix_dbl[i * avr->in_channels + j]);
369             }
370             av_log(avr, AV_LOG_DEBUG, "\n");
371         }
372
373         ret = ff_audio_mix_set_matrix(am, matrix_dbl, avr->in_channels);
374         if (ret < 0) {
375             av_free(matrix_dbl);
376             goto error;
377         }
378         av_free(matrix_dbl);
379     }
380
381     ret = mix_function_init(am);
382     if (ret < 0)
383         goto error;
384
385     return am;
386
387 error:
388     av_free(am);
389     return NULL;
390 }
391
392 void ff_audio_mix_free(AudioMix **am_p)
393 {
394     AudioMix *am;
395
396     if (!*am_p)
397         return;
398     am = *am_p;
399
400     if (am->matrix) {
401         av_free(am->matrix[0]);
402         am->matrix = NULL;
403     }
404     memset(am->matrix_q8,  0, sizeof(am->matrix_q8 ));
405     memset(am->matrix_q15, 0, sizeof(am->matrix_q15));
406     memset(am->matrix_flt, 0, sizeof(am->matrix_flt));
407
408     av_freep(am_p);
409 }
410
411 int ff_audio_mix(AudioMix *am, AudioData *src)
412 {
413     int use_generic = 1;
414     int len = src->nb_samples;
415
416     /* determine whether to use the optimized function based on pointer and
417        samples alignment in both the input and output */
418     if (am->has_optimized_func) {
419         int aligned_len = FFALIGN(len, am->samples_align);
420         if (!(src->ptr_align % am->ptr_align) &&
421             src->samples_align >= aligned_len) {
422             len = aligned_len;
423             use_generic = 0;
424         }
425     }
426     av_dlog(am->avr, "audio_mix: %d samples - %d to %d channels (%s)\n",
427             src->nb_samples, am->in_channels, am->out_channels,
428             use_generic ? am->func_descr_generic : am->func_descr);
429
430     if (use_generic)
431         am->mix_generic(src->data, am->matrix, len, am->out_channels,
432                         am->in_channels);
433     else
434         am->mix(src->data, am->matrix, len, am->out_channels, am->in_channels);
435
436     ff_audio_data_set_channels(src, am->out_channels);
437
438     return 0;
439 }
440
441 int ff_audio_mix_get_matrix(AudioMix *am, double *matrix, int stride)
442 {
443     int i, o;
444
445     if ( am->in_channels <= 0 ||  am->in_channels > AVRESAMPLE_MAX_CHANNELS ||
446         am->out_channels <= 0 || am->out_channels > AVRESAMPLE_MAX_CHANNELS) {
447         av_log(am, AV_LOG_ERROR, "Invalid channel counts\n");
448         return AVERROR(EINVAL);
449     }
450
451 #define GET_MATRIX_CONVERT(suffix, scale)                                   \
452     if (!am->matrix_ ## suffix[0]) {                                        \
453         av_log(am, AV_LOG_ERROR, "matrix is not set\n");                    \
454         return AVERROR(EINVAL);                                             \
455     }                                                                       \
456     for (o = 0; o < am->out_channels; o++)                                  \
457         for (i = 0; i < am->in_channels; i++)                               \
458             matrix[o * stride + i] = am->matrix_ ## suffix[o][i] * (scale);
459
460     switch (am->coeff_type) {
461     case AV_MIX_COEFF_TYPE_Q8:
462         GET_MATRIX_CONVERT(q8, 1.0 / 256.0);
463         break;
464     case AV_MIX_COEFF_TYPE_Q15:
465         GET_MATRIX_CONVERT(q15, 1.0 / 32768.0);
466         break;
467     case AV_MIX_COEFF_TYPE_FLT:
468         GET_MATRIX_CONVERT(flt, 1.0);
469         break;
470     default:
471         av_log(am, AV_LOG_ERROR, "Invalid mix coeff type\n");
472         return AVERROR(EINVAL);
473     }
474
475     return 0;
476 }
477
478 int ff_audio_mix_set_matrix(AudioMix *am, const double *matrix, int stride)
479 {
480     int i, o;
481
482     if ( am->in_channels <= 0 ||  am->in_channels > AVRESAMPLE_MAX_CHANNELS ||
483         am->out_channels <= 0 || am->out_channels > AVRESAMPLE_MAX_CHANNELS) {
484         av_log(am, AV_LOG_ERROR, "Invalid channel counts\n");
485         return AVERROR(EINVAL);
486     }
487
488     if (am->matrix) {
489         av_free(am->matrix[0]);
490         am->matrix = NULL;
491     }
492
493 #define CONVERT_MATRIX(type, expr)                                          \
494     am->matrix_## type[0] = av_mallocz(am->out_channels * am->in_channels * \
495                                        sizeof(*am->matrix_## type[0]));     \
496     if (!am->matrix_## type[0])                                             \
497         return AVERROR(ENOMEM);                                             \
498     for (o = 0; o < am->out_channels; o++) {                                \
499         if (o > 0)                                                          \
500             am->matrix_## type[o] = am->matrix_## type[o - 1] +             \
501                                     am->in_channels;                        \
502         for (i = 0; i < am->in_channels; i++) {                             \
503             double v = matrix[o * stride + i];                              \
504             am->matrix_## type[o][i] = expr;                                \
505         }                                                                   \
506     }                                                                       \
507     am->matrix = (void **)am->matrix_## type;
508
509     switch (am->coeff_type) {
510     case AV_MIX_COEFF_TYPE_Q8:
511         CONVERT_MATRIX(q8, av_clip_int16(lrint(256.0 * v)))
512         break;
513     case AV_MIX_COEFF_TYPE_Q15:
514         CONVERT_MATRIX(q15, av_clipl_int32(llrint(32768.0 * v)))
515         break;
516     case AV_MIX_COEFF_TYPE_FLT:
517         CONVERT_MATRIX(flt, v)
518         break;
519     default:
520         av_log(am, AV_LOG_ERROR, "Invalid mix coeff type\n");
521         return AVERROR(EINVAL);
522     }
523
524     /* TODO: detect situations where we can just swap around pointers
525              instead of doing matrix multiplications with 0.0 and 1.0 */
526
527     return 0;
528 }