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