00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdint.h>
00022
00023 #include "libavutil/libm.h"
00024 #include "libavutil/samplefmt.h"
00025 #include "avresample.h"
00026 #include "internal.h"
00027 #include "audio_data.h"
00028 #include "audio_mix.h"
00029
00030 static const char *coeff_type_names[] = { "q8", "q15", "flt" };
00031
00032 void ff_audio_mix_set_func(AudioMix *am, enum AVSampleFormat fmt,
00033 enum AVMixCoeffType coeff_type, int in_channels,
00034 int out_channels, int ptr_align, int samples_align,
00035 const char *descr, void *mix_func)
00036 {
00037 if (fmt == am->fmt && coeff_type == am->coeff_type &&
00038 ( in_channels == am->in_channels || in_channels == 0) &&
00039 (out_channels == am->out_channels || out_channels == 0)) {
00040 char chan_str[16];
00041 am->mix = mix_func;
00042 am->func_descr = descr;
00043 am->ptr_align = ptr_align;
00044 am->samples_align = samples_align;
00045 if (ptr_align == 1 && samples_align == 1) {
00046 am->mix_generic = mix_func;
00047 am->func_descr_generic = descr;
00048 } else {
00049 am->has_optimized_func = 1;
00050 }
00051 if (in_channels) {
00052 if (out_channels)
00053 snprintf(chan_str, sizeof(chan_str), "[%d to %d] ",
00054 in_channels, out_channels);
00055 else
00056 snprintf(chan_str, sizeof(chan_str), "[%d to any] ",
00057 in_channels);
00058 } else if (out_channels) {
00059 snprintf(chan_str, sizeof(chan_str), "[any to %d] ",
00060 out_channels);
00061 }
00062 av_log(am->avr, AV_LOG_DEBUG, "audio_mix: found function: [fmt=%s] "
00063 "[c=%s] %s(%s)\n", av_get_sample_fmt_name(fmt),
00064 coeff_type_names[coeff_type],
00065 (in_channels || out_channels) ? chan_str : "", descr);
00066 }
00067 }
00068
00069 #define MIX_FUNC_NAME(fmt, cfmt) mix_any_ ## fmt ##_## cfmt ##_c
00070
00071 #define MIX_FUNC_GENERIC(fmt, cfmt, stype, ctype, sumtype, expr) \
00072 static void MIX_FUNC_NAME(fmt, cfmt)(stype **samples, ctype **matrix, \
00073 int len, int out_ch, int in_ch) \
00074 { \
00075 int i, in, out; \
00076 stype temp[AVRESAMPLE_MAX_CHANNELS]; \
00077 for (i = 0; i < len; i++) { \
00078 for (out = 0; out < out_ch; out++) { \
00079 sumtype sum = 0; \
00080 for (in = 0; in < in_ch; in++) \
00081 sum += samples[in][i] * matrix[out][in]; \
00082 temp[out] = expr; \
00083 } \
00084 for (out = 0; out < out_ch; out++) \
00085 samples[out][i] = temp[out]; \
00086 } \
00087 }
00088
00089 MIX_FUNC_GENERIC(FLTP, FLT, float, float, float, sum)
00090 MIX_FUNC_GENERIC(S16P, FLT, int16_t, float, float, av_clip_int16(lrintf(sum)))
00091 MIX_FUNC_GENERIC(S16P, Q15, int16_t, int32_t, int64_t, av_clip_int16(sum >> 15))
00092 MIX_FUNC_GENERIC(S16P, Q8, int16_t, int16_t, int32_t, av_clip_int16(sum >> 8))
00093
00094
00095
00096 static void mix_2_to_1_fltp_flt_c(float **samples, float **matrix, int len,
00097 int out_ch, int in_ch)
00098 {
00099 float *src0 = samples[0];
00100 float *src1 = samples[1];
00101 float *dst = src0;
00102 float m0 = matrix[0][0];
00103 float m1 = matrix[0][1];
00104
00105 while (len > 4) {
00106 *dst++ = *src0++ * m0 + *src1++ * m1;
00107 *dst++ = *src0++ * m0 + *src1++ * m1;
00108 *dst++ = *src0++ * m0 + *src1++ * m1;
00109 *dst++ = *src0++ * m0 + *src1++ * m1;
00110 len -= 4;
00111 }
00112 while (len > 0) {
00113 *dst++ = *src0++ * m0 + *src1++ * m1;
00114 len--;
00115 }
00116 }
00117
00118 static void mix_1_to_2_fltp_flt_c(float **samples, float **matrix, int len,
00119 int out_ch, int in_ch)
00120 {
00121 float v;
00122 float *dst0 = samples[0];
00123 float *dst1 = samples[1];
00124 float *src = dst0;
00125 float m0 = matrix[0][0];
00126 float m1 = matrix[1][0];
00127
00128 while (len > 4) {
00129 v = *src++;
00130 *dst0++ = v * m1;
00131 *dst1++ = v * m0;
00132 v = *src++;
00133 *dst0++ = v * m1;
00134 *dst1++ = v * m0;
00135 v = *src++;
00136 *dst0++ = v * m1;
00137 *dst1++ = v * m0;
00138 v = *src++;
00139 *dst0++ = v * m1;
00140 *dst1++ = v * m0;
00141 len -= 4;
00142 }
00143 while (len > 0) {
00144 v = *src++;
00145 *dst0++ = v * m1;
00146 *dst1++ = v * m0;
00147 len--;
00148 }
00149 }
00150
00151 static void mix_6_to_2_fltp_flt_c(float **samples, float **matrix, int len,
00152 int out_ch, int in_ch)
00153 {
00154 float v0, v1;
00155 float *src0 = samples[0];
00156 float *src1 = samples[1];
00157 float *src2 = samples[2];
00158 float *src3 = samples[3];
00159 float *src4 = samples[4];
00160 float *src5 = samples[5];
00161 float *dst0 = src0;
00162 float *dst1 = src1;
00163 float *m0 = matrix[0];
00164 float *m1 = matrix[1];
00165
00166 while (len > 0) {
00167 v0 = *src0++;
00168 v1 = *src1++;
00169 *dst0++ = v0 * m0[0] +
00170 v1 * m0[1] +
00171 *src2 * m0[2] +
00172 *src3 * m0[3] +
00173 *src4 * m0[4] +
00174 *src5 * m0[5];
00175 *dst1++ = v0 * m1[0] +
00176 v1 * m1[1] +
00177 *src2++ * m1[2] +
00178 *src3++ * m1[3] +
00179 *src4++ * m1[4] +
00180 *src5++ * m1[5];
00181 len--;
00182 }
00183 }
00184
00185 static void mix_2_to_6_fltp_flt_c(float **samples, float **matrix, int len,
00186 int out_ch, int in_ch)
00187 {
00188 float v0, v1;
00189 float *dst0 = samples[0];
00190 float *dst1 = samples[1];
00191 float *dst2 = samples[2];
00192 float *dst3 = samples[3];
00193 float *dst4 = samples[4];
00194 float *dst5 = samples[5];
00195 float *src0 = dst0;
00196 float *src1 = dst1;
00197
00198 while (len > 0) {
00199 v0 = *src0++;
00200 v1 = *src1++;
00201 *dst0++ = v0 * matrix[0][0] + v1 * matrix[0][1];
00202 *dst1++ = v0 * matrix[1][0] + v1 * matrix[1][1];
00203 *dst2++ = v0 * matrix[2][0] + v1 * matrix[2][1];
00204 *dst3++ = v0 * matrix[3][0] + v1 * matrix[3][1];
00205 *dst4++ = v0 * matrix[4][0] + v1 * matrix[4][1];
00206 *dst5++ = v0 * matrix[5][0] + v1 * matrix[5][1];
00207 len--;
00208 }
00209 }
00210
00211 static int mix_function_init(AudioMix *am)
00212 {
00213
00214
00215 ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
00216 0, 0, 1, 1, "C", MIX_FUNC_NAME(FLTP, FLT));
00217
00218 ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_FLT,
00219 0, 0, 1, 1, "C", MIX_FUNC_NAME(S16P, FLT));
00220
00221 ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_Q15,
00222 0, 0, 1, 1, "C", MIX_FUNC_NAME(S16P, Q15));
00223
00224 ff_audio_mix_set_func(am, AV_SAMPLE_FMT_S16P, AV_MIX_COEFF_TYPE_Q8,
00225 0, 0, 1, 1, "C", MIX_FUNC_NAME(S16P, Q8));
00226
00227
00228
00229 ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
00230 2, 1, 1, 1, "C", mix_2_to_1_fltp_flt_c);
00231
00232 ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
00233 1, 2, 1, 1, "C", mix_1_to_2_fltp_flt_c);
00234
00235 ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
00236 6, 2, 1, 1, "C", mix_6_to_2_fltp_flt_c);
00237
00238 ff_audio_mix_set_func(am, AV_SAMPLE_FMT_FLTP, AV_MIX_COEFF_TYPE_FLT,
00239 2, 6, 1, 1, "C", mix_2_to_6_fltp_flt_c);
00240
00241 if (ARCH_X86)
00242 ff_audio_mix_init_x86(am);
00243
00244 if (!am->mix) {
00245 av_log(am->avr, AV_LOG_ERROR, "audio_mix: NO FUNCTION FOUND: [fmt=%s] "
00246 "[c=%s] [%d to %d]\n", av_get_sample_fmt_name(am->fmt),
00247 coeff_type_names[am->coeff_type], am->in_channels,
00248 am->out_channels);
00249 return AVERROR_PATCHWELCOME;
00250 }
00251 return 0;
00252 }
00253
00254 int ff_audio_mix_init(AVAudioResampleContext *avr)
00255 {
00256 int ret;
00257
00258
00259 if (!avr->am->matrix) {
00260 int i, j;
00261 char in_layout_name[128];
00262 char out_layout_name[128];
00263 double *matrix_dbl = av_mallocz(avr->out_channels * avr->in_channels *
00264 sizeof(*matrix_dbl));
00265 if (!matrix_dbl)
00266 return AVERROR(ENOMEM);
00267
00268 ret = avresample_build_matrix(avr->in_channel_layout,
00269 avr->out_channel_layout,
00270 avr->center_mix_level,
00271 avr->surround_mix_level,
00272 avr->lfe_mix_level, 1, matrix_dbl,
00273 avr->in_channels);
00274 if (ret < 0) {
00275 av_free(matrix_dbl);
00276 return ret;
00277 }
00278
00279 av_get_channel_layout_string(in_layout_name, sizeof(in_layout_name),
00280 avr->in_channels, avr->in_channel_layout);
00281 av_get_channel_layout_string(out_layout_name, sizeof(out_layout_name),
00282 avr->out_channels, avr->out_channel_layout);
00283 av_log(avr, AV_LOG_DEBUG, "audio_mix: %s to %s\n",
00284 in_layout_name, out_layout_name);
00285 for (i = 0; i < avr->out_channels; i++) {
00286 for (j = 0; j < avr->in_channels; j++) {
00287 av_log(avr, AV_LOG_DEBUG, " %0.3f ",
00288 matrix_dbl[i * avr->in_channels + j]);
00289 }
00290 av_log(avr, AV_LOG_DEBUG, "\n");
00291 }
00292
00293 ret = avresample_set_matrix(avr, matrix_dbl, avr->in_channels);
00294 if (ret < 0) {
00295 av_free(matrix_dbl);
00296 return ret;
00297 }
00298 av_free(matrix_dbl);
00299 }
00300
00301 avr->am->fmt = avr->internal_sample_fmt;
00302 avr->am->coeff_type = avr->mix_coeff_type;
00303 avr->am->in_layout = avr->in_channel_layout;
00304 avr->am->out_layout = avr->out_channel_layout;
00305 avr->am->in_channels = avr->in_channels;
00306 avr->am->out_channels = avr->out_channels;
00307
00308 ret = mix_function_init(avr->am);
00309 if (ret < 0)
00310 return ret;
00311
00312 return 0;
00313 }
00314
00315 void ff_audio_mix_close(AudioMix *am)
00316 {
00317 if (!am)
00318 return;
00319 if (am->matrix) {
00320 av_free(am->matrix[0]);
00321 am->matrix = NULL;
00322 }
00323 memset(am->matrix_q8, 0, sizeof(am->matrix_q8 ));
00324 memset(am->matrix_q15, 0, sizeof(am->matrix_q15));
00325 memset(am->matrix_flt, 0, sizeof(am->matrix_flt));
00326 }
00327
00328 int ff_audio_mix(AudioMix *am, AudioData *src)
00329 {
00330 int use_generic = 1;
00331 int len = src->nb_samples;
00332
00333
00334
00335 if (am->has_optimized_func) {
00336 int aligned_len = FFALIGN(len, am->samples_align);
00337 if (!(src->ptr_align % am->ptr_align) &&
00338 src->samples_align >= aligned_len) {
00339 len = aligned_len;
00340 use_generic = 0;
00341 }
00342 }
00343 av_dlog(am->avr, "audio_mix: %d samples - %d to %d channels (%s)\n",
00344 src->nb_samples, am->in_channels, am->out_channels,
00345 use_generic ? am->func_descr_generic : am->func_descr);
00346
00347 if (use_generic)
00348 am->mix_generic(src->data, am->matrix, len, am->out_channels,
00349 am->in_channels);
00350 else
00351 am->mix(src->data, am->matrix, len, am->out_channels, am->in_channels);
00352
00353 ff_audio_data_set_channels(src, am->out_channels);
00354
00355 return 0;
00356 }