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 "opt.h" 00030 00031 struct AVResampleContext; 00032 00033 static const char *context_to_name(void *ptr) 00034 { 00035 return "audioresample"; 00036 } 00037 00038 static const AVOption options[] = {{NULL}}; 00039 static const AVClass audioresample_context_class = { "ReSampleContext", context_to_name, options }; 00040 00041 struct ReSampleContext { 00042 const AVClass *av_class; 00043 struct AVResampleContext *resample_context; 00044 short *temp[2]; 00045 int temp_len; 00046 float ratio; 00047 /* channel convert */ 00048 int input_channels, output_channels, filter_channels; 00049 AVAudioConvert *convert_ctx[2]; 00050 enum SampleFormat sample_fmt[2]; 00051 unsigned sample_size[2]; 00052 short *buffer[2]; 00053 unsigned buffer_size[2]; 00054 }; 00055 00056 /* n1: number of samples */ 00057 static void stereo_to_mono(short *output, short *input, int n1) 00058 { 00059 short *p, *q; 00060 int n = n1; 00061 00062 p = input; 00063 q = output; 00064 while (n >= 4) { 00065 q[0] = (p[0] + p[1]) >> 1; 00066 q[1] = (p[2] + p[3]) >> 1; 00067 q[2] = (p[4] + p[5]) >> 1; 00068 q[3] = (p[6] + p[7]) >> 1; 00069 q += 4; 00070 p += 8; 00071 n -= 4; 00072 } 00073 while (n > 0) { 00074 q[0] = (p[0] + p[1]) >> 1; 00075 q++; 00076 p += 2; 00077 n--; 00078 } 00079 } 00080 00081 /* n1: number of samples */ 00082 static void mono_to_stereo(short *output, short *input, int n1) 00083 { 00084 short *p, *q; 00085 int n = n1; 00086 int v; 00087 00088 p = input; 00089 q = output; 00090 while (n >= 4) { 00091 v = p[0]; q[0] = v; q[1] = v; 00092 v = p[1]; q[2] = v; q[3] = v; 00093 v = p[2]; q[4] = v; q[5] = v; 00094 v = p[3]; q[6] = v; q[7] = v; 00095 q += 8; 00096 p += 4; 00097 n -= 4; 00098 } 00099 while (n > 0) { 00100 v = p[0]; q[0] = v; q[1] = v; 00101 q += 2; 00102 p += 1; 00103 n--; 00104 } 00105 } 00106 00107 /* XXX: should use more abstract 'N' channels system */ 00108 static void stereo_split(short *output1, short *output2, short *input, int n) 00109 { 00110 int i; 00111 00112 for(i=0;i<n;i++) { 00113 *output1++ = *input++; 00114 *output2++ = *input++; 00115 } 00116 } 00117 00118 static void stereo_mux(short *output, short *input1, short *input2, int n) 00119 { 00120 int i; 00121 00122 for(i=0;i<n;i++) { 00123 *output++ = *input1++; 00124 *output++ = *input2++; 00125 } 00126 } 00127 00128 static void ac3_5p1_mux(short *output, short *input1, short *input2, int n) 00129 { 00130 int i; 00131 short l,r; 00132 00133 for(i=0;i<n;i++) { 00134 l=*input1++; 00135 r=*input2++; 00136 *output++ = l; /* left */ 00137 *output++ = (l/2)+(r/2); /* center */ 00138 *output++ = r; /* right */ 00139 *output++ = 0; /* left surround */ 00140 *output++ = 0; /* right surroud */ 00141 *output++ = 0; /* low freq */ 00142 } 00143 } 00144 00145 ReSampleContext *av_audio_resample_init(int output_channels, int input_channels, 00146 int output_rate, int input_rate, 00147 enum SampleFormat sample_fmt_out, 00148 enum SampleFormat sample_fmt_in, 00149 int filter_length, int log2_phase_count, 00150 int linear, double cutoff) 00151 { 00152 ReSampleContext *s; 00153 00154 if ( input_channels > 2) 00155 { 00156 av_log(NULL, AV_LOG_ERROR, "Resampling with input channels greater than 2 unsupported.\n"); 00157 return NULL; 00158 } 00159 00160 s = av_mallocz(sizeof(ReSampleContext)); 00161 if (!s) 00162 { 00163 av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for resample context.\n"); 00164 return NULL; 00165 } 00166 00167 s->ratio = (float)output_rate / (float)input_rate; 00168 00169 s->input_channels = input_channels; 00170 s->output_channels = output_channels; 00171 00172 s->filter_channels = s->input_channels; 00173 if (s->output_channels < s->filter_channels) 00174 s->filter_channels = s->output_channels; 00175 00176 s->sample_fmt [0] = sample_fmt_in; 00177 s->sample_fmt [1] = sample_fmt_out; 00178 s->sample_size[0] = av_get_bits_per_sample_format(s->sample_fmt[0])>>3; 00179 s->sample_size[1] = av_get_bits_per_sample_format(s->sample_fmt[1])>>3; 00180 00181 if (s->sample_fmt[0] != SAMPLE_FMT_S16) { 00182 if (!(s->convert_ctx[0] = av_audio_convert_alloc(SAMPLE_FMT_S16, 1, 00183 s->sample_fmt[0], 1, NULL, 0))) { 00184 av_log(s, AV_LOG_ERROR, 00185 "Cannot convert %s sample format to s16 sample format\n", 00186 avcodec_get_sample_fmt_name(s->sample_fmt[0])); 00187 av_free(s); 00188 return NULL; 00189 } 00190 } 00191 00192 if (s->sample_fmt[1] != SAMPLE_FMT_S16) { 00193 if (!(s->convert_ctx[1] = av_audio_convert_alloc(s->sample_fmt[1], 1, 00194 SAMPLE_FMT_S16, 1, NULL, 0))) { 00195 av_log(s, AV_LOG_ERROR, 00196 "Cannot convert s16 sample format to %s sample format\n", 00197 avcodec_get_sample_fmt_name(s->sample_fmt[1])); 00198 av_audio_convert_free(s->convert_ctx[0]); 00199 av_free(s); 00200 return NULL; 00201 } 00202 } 00203 00204 /* 00205 * AC-3 output is the only case where filter_channels could be greater than 2. 00206 * input channels can't be greater than 2, so resample the 2 channels and then 00207 * expand to 6 channels after the resampling. 00208 */ 00209 if(s->filter_channels>2) 00210 s->filter_channels = 2; 00211 00212 #define TAPS 16 00213 s->resample_context= av_resample_init(output_rate, input_rate, 00214 filter_length, log2_phase_count, linear, cutoff); 00215 00216 s->av_class= &audioresample_context_class; 00217 00218 return s; 00219 } 00220 00221 #if LIBAVCODEC_VERSION_MAJOR < 53 00222 ReSampleContext *audio_resample_init(int output_channels, int input_channels, 00223 int output_rate, int input_rate) 00224 { 00225 return av_audio_resample_init(output_channels, input_channels, 00226 output_rate, input_rate, 00227 SAMPLE_FMT_S16, SAMPLE_FMT_S16, 00228 TAPS, 10, 0, 0.8); 00229 } 00230 #endif 00231 00232 /* resample audio. 'nb_samples' is the number of input samples */ 00233 /* XXX: optimize it ! */ 00234 int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples) 00235 { 00236 int i, nb_samples1; 00237 short *bufin[2]; 00238 short *bufout[2]; 00239 short *buftmp2[2], *buftmp3[2]; 00240 short *output_bak = NULL; 00241 int lenout; 00242 00243 if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) { 00244 /* nothing to do */ 00245 memcpy(output, input, nb_samples * s->input_channels * sizeof(short)); 00246 return nb_samples; 00247 } 00248 00249 if (s->sample_fmt[0] != SAMPLE_FMT_S16) { 00250 int istride[1] = { s->sample_size[0] }; 00251 int ostride[1] = { 2 }; 00252 const void *ibuf[1] = { input }; 00253 void *obuf[1]; 00254 unsigned input_size = nb_samples*s->input_channels*2; 00255 00256 if (!s->buffer_size[0] || s->buffer_size[0] < input_size) { 00257 av_free(s->buffer[0]); 00258 s->buffer_size[0] = input_size; 00259 s->buffer[0] = av_malloc(s->buffer_size[0]); 00260 if (!s->buffer[0]) { 00261 av_log(s, AV_LOG_ERROR, "Could not allocate buffer\n"); 00262 return 0; 00263 } 00264 } 00265 00266 obuf[0] = s->buffer[0]; 00267 00268 if (av_audio_convert(s->convert_ctx[0], obuf, ostride, 00269 ibuf, istride, nb_samples*s->input_channels) < 0) { 00270 av_log(s, AV_LOG_ERROR, "Audio sample format conversion failed\n"); 00271 return 0; 00272 } 00273 00274 input = s->buffer[0]; 00275 } 00276 00277 lenout= 4*nb_samples * s->ratio + 16; 00278 00279 if (s->sample_fmt[1] != SAMPLE_FMT_S16) { 00280 output_bak = output; 00281 00282 if (!s->buffer_size[1] || s->buffer_size[1] < 2*lenout) { 00283 av_free(s->buffer[1]); 00284 s->buffer_size[1] = 2*lenout; 00285 s->buffer[1] = av_malloc(s->buffer_size[1]); 00286 if (!s->buffer[1]) { 00287 av_log(s, AV_LOG_ERROR, "Could not allocate buffer\n"); 00288 return 0; 00289 } 00290 } 00291 00292 output = s->buffer[1]; 00293 } 00294 00295 /* XXX: move those malloc to resample init code */ 00296 for(i=0; i<s->filter_channels; i++){ 00297 bufin[i]= av_malloc( (nb_samples + s->temp_len) * sizeof(short) ); 00298 memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short)); 00299 buftmp2[i] = bufin[i] + s->temp_len; 00300 } 00301 00302 /* make some zoom to avoid round pb */ 00303 bufout[0]= av_malloc( lenout * sizeof(short) ); 00304 bufout[1]= av_malloc( lenout * sizeof(short) ); 00305 00306 if (s->input_channels == 2 && 00307 s->output_channels == 1) { 00308 buftmp3[0] = output; 00309 stereo_to_mono(buftmp2[0], input, nb_samples); 00310 } else if (s->output_channels >= 2 && s->input_channels == 1) { 00311 buftmp3[0] = bufout[0]; 00312 memcpy(buftmp2[0], input, nb_samples*sizeof(short)); 00313 } else if (s->output_channels >= 2) { 00314 buftmp3[0] = bufout[0]; 00315 buftmp3[1] = bufout[1]; 00316 stereo_split(buftmp2[0], buftmp2[1], input, nb_samples); 00317 } else { 00318 buftmp3[0] = output; 00319 memcpy(buftmp2[0], input, nb_samples*sizeof(short)); 00320 } 00321 00322 nb_samples += s->temp_len; 00323 00324 /* resample each channel */ 00325 nb_samples1 = 0; /* avoid warning */ 00326 for(i=0;i<s->filter_channels;i++) { 00327 int consumed; 00328 int is_last= i+1 == s->filter_channels; 00329 00330 nb_samples1 = av_resample(s->resample_context, buftmp3[i], bufin[i], &consumed, nb_samples, lenout, is_last); 00331 s->temp_len= nb_samples - consumed; 00332 s->temp[i]= av_realloc(s->temp[i], s->temp_len*sizeof(short)); 00333 memcpy(s->temp[i], bufin[i] + consumed, s->temp_len*sizeof(short)); 00334 } 00335 00336 if (s->output_channels == 2 && s->input_channels == 1) { 00337 mono_to_stereo(output, buftmp3[0], nb_samples1); 00338 } else if (s->output_channels == 2) { 00339 stereo_mux(output, buftmp3[0], buftmp3[1], nb_samples1); 00340 } else if (s->output_channels == 6) { 00341 ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1); 00342 } 00343 00344 if (s->sample_fmt[1] != SAMPLE_FMT_S16) { 00345 int istride[1] = { 2 }; 00346 int ostride[1] = { s->sample_size[1] }; 00347 const void *ibuf[1] = { output }; 00348 void *obuf[1] = { output_bak }; 00349 00350 if (av_audio_convert(s->convert_ctx[1], obuf, ostride, 00351 ibuf, istride, nb_samples1*s->output_channels) < 0) { 00352 av_log(s, AV_LOG_ERROR, "Audio sample format convertion failed\n"); 00353 return 0; 00354 } 00355 } 00356 00357 for(i=0; i<s->filter_channels; i++) 00358 av_free(bufin[i]); 00359 00360 av_free(bufout[0]); 00361 av_free(bufout[1]); 00362 return nb_samples1; 00363 } 00364 00365 void audio_resample_close(ReSampleContext *s) 00366 { 00367 av_resample_close(s->resample_context); 00368 av_freep(&s->temp[0]); 00369 av_freep(&s->temp[1]); 00370 av_freep(&s->buffer[0]); 00371 av_freep(&s->buffer[1]); 00372 av_audio_convert_free(s->convert_ctx[0]); 00373 av_audio_convert_free(s->convert_ctx[1]); 00374 av_free(s); 00375 }