libavcodec/resample.c
Go to the documentation of this file.
00001 /*
00002  * samplerate conversion for both audio and video
00003  * Copyright (c) 2000 Fabrice Bellard
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 #include "avcodec.h"
00028 #include "audioconvert.h"
00029 #include "libavutil/opt.h"
00030 #include "libavutil/samplefmt.h"
00031 
00032 #define MAX_CHANNELS 8
00033 
00034 struct AVResampleContext;
00035 
00036 static const char *context_to_name(void *ptr)
00037 {
00038     return "audioresample";
00039 }
00040 
00041 static const AVOption options[] = {{NULL}};
00042 static const AVClass audioresample_context_class = {
00043     "ReSampleContext", context_to_name, options, LIBAVUTIL_VERSION_INT
00044 };
00045 
00046 struct ReSampleContext {
00047     struct AVResampleContext *resample_context;
00048     short *temp[MAX_CHANNELS];
00049     int temp_len;
00050     float ratio;
00051     /* channel convert */
00052     int input_channels, output_channels, filter_channels;
00053     AVAudioConvert *convert_ctx[2];
00054     enum AVSampleFormat sample_fmt[2]; 
00055     unsigned sample_size[2];           
00056     short *buffer[2];                  
00057     unsigned buffer_size[2];           
00058 };
00059 
00060 /* n1: number of samples */
00061 static void stereo_to_mono(short *output, short *input, int n1)
00062 {
00063     short *p, *q;
00064     int n = n1;
00065 
00066     p = input;
00067     q = output;
00068     while (n >= 4) {
00069         q[0] = (p[0] + p[1]) >> 1;
00070         q[1] = (p[2] + p[3]) >> 1;
00071         q[2] = (p[4] + p[5]) >> 1;
00072         q[3] = (p[6] + p[7]) >> 1;
00073         q += 4;
00074         p += 8;
00075         n -= 4;
00076     }
00077     while (n > 0) {
00078         q[0] = (p[0] + p[1]) >> 1;
00079         q++;
00080         p += 2;
00081         n--;
00082     }
00083 }
00084 
00085 /* n1: number of samples */
00086 static void mono_to_stereo(short *output, short *input, int n1)
00087 {
00088     short *p, *q;
00089     int n = n1;
00090     int v;
00091 
00092     p = input;
00093     q = output;
00094     while (n >= 4) {
00095         v = p[0]; q[0] = v; q[1] = v;
00096         v = p[1]; q[2] = v; q[3] = v;
00097         v = p[2]; q[4] = v; q[5] = v;
00098         v = p[3]; q[6] = v; q[7] = v;
00099         q += 8;
00100         p += 4;
00101         n -= 4;
00102     }
00103     while (n > 0) {
00104         v = p[0]; q[0] = v; q[1] = v;
00105         q += 2;
00106         p += 1;
00107         n--;
00108     }
00109 }
00110 
00111 /*
00112 5.1 to stereo input: [fl, fr, c, lfe, rl, rr]
00113 - Left = front_left + rear_gain * rear_left + center_gain * center
00114 - Right = front_right + rear_gain * rear_right + center_gain * center
00115 Where rear_gain is usually around 0.5-1.0 and
00116       center_gain is almost always 0.7 (-3 dB)
00117 */
00118 static void surround_to_stereo(short **output, short *input, int channels, int samples)
00119 {
00120     int i;
00121     short l, r;
00122 
00123     for (i = 0; i < samples; i++) {
00124         int fl,fr,c,rl,rr;
00125         fl = input[0];
00126         fr = input[1];
00127         c = input[2];
00128         // lfe = input[3];
00129         rl = input[4];
00130         rr = input[5];
00131 
00132         l = av_clip_int16(fl + (0.5 * rl) + (0.7 * c));
00133         r = av_clip_int16(fr + (0.5 * rr) + (0.7 * c));
00134 
00135         /* output l & r. */
00136         *output[0]++ = l;
00137         *output[1]++ = r;
00138 
00139         /* increment input. */
00140         input += channels;
00141     }
00142 }
00143 
00144 static void deinterleave(short **output, short *input, int channels, int samples)
00145 {
00146     int i, j;
00147 
00148     for (i = 0; i < samples; i++) {
00149         for (j = 0; j < channels; j++) {
00150             *output[j]++ = *input++;
00151         }
00152     }
00153 }
00154 
00155 static void interleave(short *output, short **input, int channels, int samples)
00156 {
00157     int i, j;
00158 
00159     for (i = 0; i < samples; i++) {
00160         for (j = 0; j < channels; j++) {
00161             *output++ = *input[j]++;
00162         }
00163     }
00164 }
00165 
00166 static void ac3_5p1_mux(short *output, short *input1, short *input2, int n)
00167 {
00168     int i;
00169     short l, r;
00170 
00171     for (i = 0; i < n; i++) {
00172         l = *input1++;
00173         r = *input2++;
00174         *output++ = l;                  /* left */
00175         *output++ = (l / 2) + (r / 2);  /* center */
00176         *output++ = r;                  /* right */
00177         *output++ = 0;                  /* left surround */
00178         *output++ = 0;                  /* right surroud */
00179         *output++ = 0;                  /* low freq */
00180     }
00181 }
00182 
00183 #define SUPPORT_RESAMPLE(ch1, ch2, ch3, ch4, ch5, ch6, ch7, ch8) \
00184     ch8<<7 | ch7<<6 | ch6<<5 | ch5<<4 | ch4<<3 | ch3<<2 | ch2<<1 | ch1<<0
00185 
00186 static const uint8_t supported_resampling[MAX_CHANNELS] = {
00187     // output ch:    1  2  3  4  5  6  7  8
00188     SUPPORT_RESAMPLE(1, 1, 0, 0, 0, 0, 0, 0), // 1 input channel
00189     SUPPORT_RESAMPLE(1, 1, 0, 0, 0, 1, 0, 0), // 2 input channels
00190     SUPPORT_RESAMPLE(0, 0, 1, 0, 0, 0, 0, 0), // 3 input channels
00191     SUPPORT_RESAMPLE(0, 0, 0, 1, 0, 0, 0, 0), // 4 input channels
00192     SUPPORT_RESAMPLE(0, 0, 0, 0, 1, 0, 0, 0), // 5 input channels
00193     SUPPORT_RESAMPLE(0, 1, 0, 0, 0, 1, 0, 0), // 6 input channels
00194     SUPPORT_RESAMPLE(0, 0, 0, 0, 0, 0, 1, 0), // 7 input channels
00195     SUPPORT_RESAMPLE(0, 0, 0, 0, 0, 0, 0, 1), // 8 input channels
00196 };
00197 
00198 ReSampleContext *av_audio_resample_init(int output_channels, int input_channels,
00199                                         int output_rate, int input_rate,
00200                                         enum AVSampleFormat sample_fmt_out,
00201                                         enum AVSampleFormat sample_fmt_in,
00202                                         int filter_length, int log2_phase_count,
00203                                         int linear, double cutoff)
00204 {
00205     ReSampleContext *s;
00206 
00207     if (input_channels > MAX_CHANNELS) {
00208         av_log(NULL, AV_LOG_ERROR,
00209                "Resampling with input channels greater than %d is unsupported.\n",
00210                MAX_CHANNELS);
00211         return NULL;
00212     }
00213     if (!(supported_resampling[input_channels-1] & (1<<(output_channels-1)))) {
00214         int i;
00215         av_log(NULL, AV_LOG_ERROR, "Unsupported audio resampling. Allowed "
00216                "output channels for %d input channel%s", input_channels,
00217                input_channels > 1 ? "s:" : ":");
00218         for (i = 0; i < MAX_CHANNELS; i++)
00219             if (supported_resampling[input_channels-1] & (1<<i))
00220                 av_log(NULL, AV_LOG_ERROR, " %d", i + 1);
00221         av_log(NULL, AV_LOG_ERROR, "\n");
00222         return NULL;
00223     }
00224 
00225     s = av_mallocz(sizeof(ReSampleContext));
00226     if (!s) {
00227         av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for resample context.\n");
00228         return NULL;
00229     }
00230 
00231     s->ratio = (float)output_rate / (float)input_rate;
00232 
00233     s->input_channels = input_channels;
00234     s->output_channels = output_channels;
00235 
00236     s->filter_channels = s->input_channels;
00237     if (s->output_channels < s->filter_channels)
00238         s->filter_channels = s->output_channels;
00239 
00240     s->sample_fmt[0]  = sample_fmt_in;
00241     s->sample_fmt[1]  = sample_fmt_out;
00242     s->sample_size[0] = av_get_bytes_per_sample(s->sample_fmt[0]);
00243     s->sample_size[1] = av_get_bytes_per_sample(s->sample_fmt[1]);
00244 
00245     if (s->sample_fmt[0] != AV_SAMPLE_FMT_S16) {
00246         if (!(s->convert_ctx[0] = av_audio_convert_alloc(AV_SAMPLE_FMT_S16, 1,
00247                                                          s->sample_fmt[0], 1, NULL, 0))) {
00248             av_log(s, AV_LOG_ERROR,
00249                    "Cannot convert %s sample format to s16 sample format\n",
00250                    av_get_sample_fmt_name(s->sample_fmt[0]));
00251             av_free(s);
00252             return NULL;
00253         }
00254     }
00255 
00256     if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
00257         if (!(s->convert_ctx[1] = av_audio_convert_alloc(s->sample_fmt[1], 1,
00258                                                          AV_SAMPLE_FMT_S16, 1, NULL, 0))) {
00259             av_log(s, AV_LOG_ERROR,
00260                    "Cannot convert s16 sample format to %s sample format\n",
00261                    av_get_sample_fmt_name(s->sample_fmt[1]));
00262             av_audio_convert_free(s->convert_ctx[0]);
00263             av_free(s);
00264             return NULL;
00265         }
00266     }
00267 
00268     s->resample_context = av_resample_init(output_rate, input_rate,
00269                                            filter_length, log2_phase_count,
00270                                            linear, cutoff);
00271 
00272     *(const AVClass**)s->resample_context = &audioresample_context_class;
00273 
00274     return s;
00275 }
00276 
00277 /* resample audio. 'nb_samples' is the number of input samples */
00278 /* XXX: optimize it ! */
00279 int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
00280 {
00281     int i, nb_samples1;
00282     short *bufin[MAX_CHANNELS];
00283     short *bufout[MAX_CHANNELS];
00284     short *buftmp2[MAX_CHANNELS], *buftmp3[MAX_CHANNELS];
00285     short *output_bak = NULL;
00286     int lenout;
00287 
00288     if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) {
00289         /* nothing to do */
00290         memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
00291         return nb_samples;
00292     }
00293 
00294     if (s->sample_fmt[0] != AV_SAMPLE_FMT_S16) {
00295         int istride[1] = { s->sample_size[0] };
00296         int ostride[1] = { 2 };
00297         const void *ibuf[1] = { input };
00298         void       *obuf[1];
00299         unsigned input_size = nb_samples * s->input_channels * 2;
00300 
00301         if (!s->buffer_size[0] || s->buffer_size[0] < input_size) {
00302             av_free(s->buffer[0]);
00303             s->buffer_size[0] = input_size;
00304             s->buffer[0] = av_malloc(s->buffer_size[0]);
00305             if (!s->buffer[0]) {
00306                 av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
00307                 return 0;
00308             }
00309         }
00310 
00311         obuf[0] = s->buffer[0];
00312 
00313         if (av_audio_convert(s->convert_ctx[0], obuf, ostride,
00314                              ibuf, istride, nb_samples * s->input_channels) < 0) {
00315             av_log(s->resample_context, AV_LOG_ERROR,
00316                    "Audio sample format conversion failed\n");
00317             return 0;
00318         }
00319 
00320         input = s->buffer[0];
00321     }
00322 
00323     lenout= 2*s->output_channels*nb_samples * s->ratio + 16;
00324 
00325     if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
00326         output_bak = output;
00327 
00328         if (!s->buffer_size[1] || s->buffer_size[1] < 2*lenout) {
00329             av_free(s->buffer[1]);
00330             s->buffer_size[1] = 2*lenout;
00331             s->buffer[1] = av_malloc(s->buffer_size[1]);
00332             if (!s->buffer[1]) {
00333                 av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
00334                 return 0;
00335             }
00336         }
00337 
00338         output = s->buffer[1];
00339     }
00340 
00341     /* XXX: move those malloc to resample init code */
00342     for (i = 0; i < s->filter_channels; i++) {
00343         bufin[i] = av_malloc((nb_samples + s->temp_len) * sizeof(short));
00344         memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short));
00345         buftmp2[i] = bufin[i] + s->temp_len;
00346         bufout[i] = av_malloc(lenout * sizeof(short));
00347     }
00348 
00349     if (s->input_channels == 2 && s->output_channels == 1) {
00350         buftmp3[0] = output;
00351         stereo_to_mono(buftmp2[0], input, nb_samples);
00352     } else if (s->output_channels >= 2 && s->input_channels == 1) {
00353         buftmp3[0] = bufout[0];
00354         memcpy(buftmp2[0], input, nb_samples * sizeof(short));
00355     } else if (s->input_channels == 6 && s->output_channels ==2) {
00356         buftmp3[0] = bufout[0];
00357         buftmp3[1] = bufout[1];
00358         surround_to_stereo(buftmp2, input, s->input_channels, nb_samples);
00359     } else if (s->output_channels >= s->input_channels && s->input_channels >= 2) {
00360         for (i = 0; i < s->input_channels; i++) {
00361             buftmp3[i] = bufout[i];
00362         }
00363         deinterleave(buftmp2, input, s->input_channels, nb_samples);
00364     } else {
00365         buftmp3[0] = output;
00366         memcpy(buftmp2[0], input, nb_samples * sizeof(short));
00367     }
00368 
00369     nb_samples += s->temp_len;
00370 
00371     /* resample each channel */
00372     nb_samples1 = 0; /* avoid warning */
00373     for (i = 0; i < s->filter_channels; i++) {
00374         int consumed;
00375         int is_last = i + 1 == s->filter_channels;
00376 
00377         nb_samples1 = av_resample(s->resample_context, buftmp3[i], bufin[i],
00378                                   &consumed, nb_samples, lenout, is_last);
00379         s->temp_len = nb_samples - consumed;
00380         s->temp[i] = av_realloc(s->temp[i], s->temp_len * sizeof(short));
00381         memcpy(s->temp[i], bufin[i] + consumed, s->temp_len * sizeof(short));
00382     }
00383 
00384     if (s->output_channels == 2 && s->input_channels == 1) {
00385         mono_to_stereo(output, buftmp3[0], nb_samples1);
00386     } else if (s->output_channels == 6 && s->input_channels == 2) {
00387         ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
00388     } else if ((s->output_channels == s->input_channels && s->input_channels >= 2) ||
00389                (s->output_channels == 2 && s->input_channels == 6)) {
00390         interleave(output, buftmp3, s->output_channels, nb_samples1);
00391     }
00392 
00393     if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
00394         int istride[1] = { 2 };
00395         int ostride[1] = { s->sample_size[1] };
00396         const void *ibuf[1] = { output };
00397         void       *obuf[1] = { output_bak };
00398 
00399         if (av_audio_convert(s->convert_ctx[1], obuf, ostride,
00400                              ibuf, istride, nb_samples1 * s->output_channels) < 0) {
00401             av_log(s->resample_context, AV_LOG_ERROR,
00402                    "Audio sample format convertion failed\n");
00403             return 0;
00404         }
00405     }
00406 
00407     for (i = 0; i < s->filter_channels; i++) {
00408         av_free(bufin[i]);
00409         av_free(bufout[i]);
00410     }
00411 
00412     return nb_samples1;
00413 }
00414 
00415 void audio_resample_close(ReSampleContext *s)
00416 {
00417     int i;
00418     av_resample_close(s->resample_context);
00419     for (i = 0; i < s->filter_channels; i++)
00420         av_freep(&s->temp[i]);
00421     av_freep(&s->buffer[0]);
00422     av_freep(&s->buffer[1]);
00423     av_audio_convert_free(s->convert_ctx[0]);
00424     av_audio_convert_free(s->convert_ctx[1]);
00425     av_free(s);
00426 }