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