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 Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; 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 static void deinterleave(short **output, short *input, int channels, int samples)
00112 {
00113     int i, j;
00114 
00115     for (i = 0; i < samples; i++) {
00116         for (j = 0; j < channels; j++) {
00117             *output[j]++ = *input++;
00118         }
00119     }
00120 }
00121 
00122 static void interleave(short *output, short **input, int channels, int samples)
00123 {
00124     int i, j;
00125 
00126     for (i = 0; i < samples; i++) {
00127         for (j = 0; j < channels; j++) {
00128             *output++ = *input[j]++;
00129         }
00130     }
00131 }
00132 
00133 static void ac3_5p1_mux(short *output, short *input1, short *input2, int n)
00134 {
00135     int i;
00136     short l, r;
00137 
00138     for (i = 0; i < n; i++) {
00139         l = *input1++;
00140         r = *input2++;
00141         *output++ = l;                  /* left */
00142         *output++ = (l / 2) + (r / 2);  /* center */
00143         *output++ = r;                  /* right */
00144         *output++ = 0;                  /* left surround */
00145         *output++ = 0;                  /* right surroud */
00146         *output++ = 0;                  /* low freq */
00147     }
00148 }
00149 
00150 ReSampleContext *av_audio_resample_init(int output_channels, int input_channels,
00151                                         int output_rate, int input_rate,
00152                                         enum AVSampleFormat sample_fmt_out,
00153                                         enum AVSampleFormat sample_fmt_in,
00154                                         int filter_length, int log2_phase_count,
00155                                         int linear, double cutoff)
00156 {
00157     ReSampleContext *s;
00158 
00159     if (input_channels > MAX_CHANNELS) {
00160         av_log(NULL, AV_LOG_ERROR,
00161                "Resampling with input channels greater than %d is unsupported.\n",
00162                MAX_CHANNELS);
00163         return NULL;
00164     }
00165     if (output_channels != input_channels &&
00166         (input_channels  > 2 ||
00167          output_channels > 2 &&
00168          !(output_channels == 6 && input_channels == 2))) {
00169         av_log(NULL, AV_LOG_ERROR,
00170                "Resampling output channel count must be 1 or 2 for mono input; 1, 2 or 6 for stereo input; or N for N channel input.\n");
00171         return NULL;
00172     }
00173 
00174     s = av_mallocz(sizeof(ReSampleContext));
00175     if (!s) {
00176         av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for resample context.\n");
00177         return NULL;
00178     }
00179 
00180     s->ratio = (float)output_rate / (float)input_rate;
00181 
00182     s->input_channels = input_channels;
00183     s->output_channels = output_channels;
00184 
00185     s->filter_channels = s->input_channels;
00186     if (s->output_channels < s->filter_channels)
00187         s->filter_channels = s->output_channels;
00188 
00189     s->sample_fmt[0]  = sample_fmt_in;
00190     s->sample_fmt[1]  = sample_fmt_out;
00191     s->sample_size[0] = av_get_bytes_per_sample(s->sample_fmt[0]);
00192     s->sample_size[1] = av_get_bytes_per_sample(s->sample_fmt[1]);
00193 
00194     if (s->sample_fmt[0] != AV_SAMPLE_FMT_S16) {
00195         if (!(s->convert_ctx[0] = av_audio_convert_alloc(AV_SAMPLE_FMT_S16, 1,
00196                                                          s->sample_fmt[0], 1, NULL, 0))) {
00197             av_log(s, AV_LOG_ERROR,
00198                    "Cannot convert %s sample format to s16 sample format\n",
00199                    av_get_sample_fmt_name(s->sample_fmt[0]));
00200             av_free(s);
00201             return NULL;
00202         }
00203     }
00204 
00205     if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
00206         if (!(s->convert_ctx[1] = av_audio_convert_alloc(s->sample_fmt[1], 1,
00207                                                          AV_SAMPLE_FMT_S16, 1, NULL, 0))) {
00208             av_log(s, AV_LOG_ERROR,
00209                    "Cannot convert s16 sample format to %s sample format\n",
00210                    av_get_sample_fmt_name(s->sample_fmt[1]));
00211             av_audio_convert_free(s->convert_ctx[0]);
00212             av_free(s);
00213             return NULL;
00214         }
00215     }
00216 
00217     s->resample_context = av_resample_init(output_rate, input_rate,
00218                                            filter_length, log2_phase_count,
00219                                            linear, cutoff);
00220 
00221     *(const AVClass**)s->resample_context = &audioresample_context_class;
00222 
00223     return s;
00224 }
00225 
00226 /* resample audio. 'nb_samples' is the number of input samples */
00227 /* XXX: optimize it ! */
00228 int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
00229 {
00230     int i, nb_samples1;
00231     short *bufin[MAX_CHANNELS];
00232     short *bufout[MAX_CHANNELS];
00233     short *buftmp2[MAX_CHANNELS], *buftmp3[MAX_CHANNELS];
00234     short *output_bak = NULL;
00235     int lenout;
00236 
00237     if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) {
00238         /* nothing to do */
00239         memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
00240         return nb_samples;
00241     }
00242 
00243     if (s->sample_fmt[0] != AV_SAMPLE_FMT_S16) {
00244         int istride[1] = { s->sample_size[0] };
00245         int ostride[1] = { 2 };
00246         const void *ibuf[1] = { input };
00247         void       *obuf[1];
00248         unsigned input_size = nb_samples * s->input_channels * 2;
00249 
00250         if (!s->buffer_size[0] || s->buffer_size[0] < input_size) {
00251             av_free(s->buffer[0]);
00252             s->buffer_size[0] = input_size;
00253             s->buffer[0] = av_malloc(s->buffer_size[0]);
00254             if (!s->buffer[0]) {
00255                 av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
00256                 return 0;
00257             }
00258         }
00259 
00260         obuf[0] = s->buffer[0];
00261 
00262         if (av_audio_convert(s->convert_ctx[0], obuf, ostride,
00263                              ibuf, istride, nb_samples * s->input_channels) < 0) {
00264             av_log(s->resample_context, AV_LOG_ERROR,
00265                    "Audio sample format conversion failed\n");
00266             return 0;
00267         }
00268 
00269         input = s->buffer[0];
00270     }
00271 
00272     lenout = 4 * nb_samples * s->ratio + 16;
00273 
00274     if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
00275         output_bak = output;
00276 
00277         if (!s->buffer_size[1] || s->buffer_size[1] < lenout) {
00278             av_free(s->buffer[1]);
00279             s->buffer_size[1] = lenout;
00280             s->buffer[1] = av_malloc(s->buffer_size[1]);
00281             if (!s->buffer[1]) {
00282                 av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
00283                 return 0;
00284             }
00285         }
00286 
00287         output = s->buffer[1];
00288     }
00289 
00290     /* XXX: move those malloc to resample init code */
00291     for (i = 0; i < s->filter_channels; i++) {
00292         bufin[i] = av_malloc((nb_samples + s->temp_len) * sizeof(short));
00293         memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short));
00294         buftmp2[i] = bufin[i] + s->temp_len;
00295         bufout[i] = av_malloc(lenout * sizeof(short));
00296     }
00297 
00298     if (s->input_channels == 2 && s->output_channels == 1) {
00299         buftmp3[0] = output;
00300         stereo_to_mono(buftmp2[0], input, nb_samples);
00301     } else if (s->output_channels >= 2 && s->input_channels == 1) {
00302         buftmp3[0] = bufout[0];
00303         memcpy(buftmp2[0], input, nb_samples * sizeof(short));
00304     } else if (s->output_channels >= s->input_channels && s->input_channels >= 2) {
00305         for (i = 0; i < s->input_channels; i++) {
00306             buftmp3[i] = bufout[i];
00307         }
00308         deinterleave(buftmp2, input, s->input_channels, nb_samples);
00309     } else {
00310         buftmp3[0] = output;
00311         memcpy(buftmp2[0], input, nb_samples * sizeof(short));
00312     }
00313 
00314     nb_samples += s->temp_len;
00315 
00316     /* resample each channel */
00317     nb_samples1 = 0; /* avoid warning */
00318     for (i = 0; i < s->filter_channels; i++) {
00319         int consumed;
00320         int is_last = i + 1 == s->filter_channels;
00321 
00322         nb_samples1 = av_resample(s->resample_context, buftmp3[i], bufin[i],
00323                                   &consumed, nb_samples, lenout, is_last);
00324         s->temp_len = nb_samples - consumed;
00325         s->temp[i] = av_realloc(s->temp[i], s->temp_len * sizeof(short));
00326         memcpy(s->temp[i], bufin[i] + consumed, s->temp_len * sizeof(short));
00327     }
00328 
00329     if (s->output_channels == 2 && s->input_channels == 1) {
00330         mono_to_stereo(output, buftmp3[0], nb_samples1);
00331     } else if (s->output_channels == 6 && s->input_channels == 2) {
00332         ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
00333     } else if (s->output_channels == s->input_channels && s->input_channels >= 2) {
00334         interleave(output, buftmp3, s->output_channels, nb_samples1);
00335     }
00336 
00337     if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
00338         int istride[1] = { 2 };
00339         int ostride[1] = { s->sample_size[1] };
00340         const void *ibuf[1] = { output };
00341         void       *obuf[1] = { output_bak };
00342 
00343         if (av_audio_convert(s->convert_ctx[1], obuf, ostride,
00344                              ibuf, istride, nb_samples1 * s->output_channels) < 0) {
00345             av_log(s->resample_context, AV_LOG_ERROR,
00346                    "Audio sample format convertion failed\n");
00347             return 0;
00348         }
00349     }
00350 
00351     for (i = 0; i < s->filter_channels; i++) {
00352         av_free(bufin[i]);
00353         av_free(bufout[i]);
00354     }
00355 
00356     return nb_samples1;
00357 }
00358 
00359 void audio_resample_close(ReSampleContext *s)
00360 {
00361     int i;
00362     av_resample_close(s->resample_context);
00363     for (i = 0; i < s->filter_channels; i++)
00364         av_freep(&s->temp[i]);
00365     av_freep(&s->buffer[0]);
00366     av_freep(&s->buffer[1]);
00367     av_audio_convert_free(s->convert_ctx[0]);
00368     av_audio_convert_free(s->convert_ctx[1]);
00369     av_free(s);
00370 }