00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
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
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
00113
00114
00115
00116
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,lfe;
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
00136 *output[0]++ = l;
00137 *output[1]++ = r;
00138
00139
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;
00175 *output++ = (l / 2) + (r / 2);
00176 *output++ = r;
00177 *output++ = 0;
00178 *output++ = 0;
00179 *output++ = 0;
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
00188 SUPPORT_RESAMPLE(1, 1, 0, 0, 0, 0, 0, 0),
00189 SUPPORT_RESAMPLE(1, 1, 0, 0, 0, 1, 0, 0),
00190 SUPPORT_RESAMPLE(0, 0, 1, 0, 0, 0, 0, 0),
00191 SUPPORT_RESAMPLE(0, 0, 0, 1, 0, 0, 0, 0),
00192 SUPPORT_RESAMPLE(0, 0, 0, 0, 1, 0, 0, 0),
00193 SUPPORT_RESAMPLE(0, 1, 0, 0, 0, 1, 0, 0),
00194 SUPPORT_RESAMPLE(0, 0, 0, 0, 0, 0, 1, 0),
00195 SUPPORT_RESAMPLE(0, 0, 0, 0, 0, 0, 0, 1),
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 #define TAPS 16
00269 s->resample_context = av_resample_init(output_rate, input_rate,
00270 filter_length, log2_phase_count,
00271 linear, cutoff);
00272
00273 *(const AVClass**)s->resample_context = &audioresample_context_class;
00274
00275 return s;
00276 }
00277
00278 #if FF_API_AUDIO_OLD
00279 ReSampleContext *audio_resample_init(int output_channels, int input_channels,
00280 int output_rate, int input_rate)
00281 {
00282 return av_audio_resample_init(output_channels, input_channels,
00283 output_rate, input_rate,
00284 AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16,
00285 TAPS, 10, 0, 0.8);
00286 }
00287 #endif
00288
00289
00290
00291 int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
00292 {
00293 int i, nb_samples1;
00294 short *bufin[MAX_CHANNELS];
00295 short *bufout[MAX_CHANNELS];
00296 short *buftmp2[MAX_CHANNELS], *buftmp3[MAX_CHANNELS];
00297 short *output_bak = NULL;
00298 int lenout;
00299
00300 if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) {
00301
00302 memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
00303 return nb_samples;
00304 }
00305
00306 if (s->sample_fmt[0] != AV_SAMPLE_FMT_S16) {
00307 int istride[1] = { s->sample_size[0] };
00308 int ostride[1] = { 2 };
00309 const void *ibuf[1] = { input };
00310 void *obuf[1];
00311 unsigned input_size = nb_samples * s->input_channels * 2;
00312
00313 if (!s->buffer_size[0] || s->buffer_size[0] < input_size) {
00314 av_free(s->buffer[0]);
00315 s->buffer_size[0] = input_size;
00316 s->buffer[0] = av_malloc(s->buffer_size[0]);
00317 if (!s->buffer[0]) {
00318 av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
00319 return 0;
00320 }
00321 }
00322
00323 obuf[0] = s->buffer[0];
00324
00325 if (av_audio_convert(s->convert_ctx[0], obuf, ostride,
00326 ibuf, istride, nb_samples * s->input_channels) < 0) {
00327 av_log(s->resample_context, AV_LOG_ERROR,
00328 "Audio sample format conversion failed\n");
00329 return 0;
00330 }
00331
00332 input = s->buffer[0];
00333 }
00334
00335 lenout= 2*s->output_channels*nb_samples * s->ratio + 16;
00336
00337 if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
00338 output_bak = output;
00339
00340 if (!s->buffer_size[1] || s->buffer_size[1] < 2*lenout) {
00341 av_free(s->buffer[1]);
00342 s->buffer_size[1] = 2*lenout;
00343 s->buffer[1] = av_malloc(s->buffer_size[1]);
00344 if (!s->buffer[1]) {
00345 av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
00346 return 0;
00347 }
00348 }
00349
00350 output = s->buffer[1];
00351 }
00352
00353
00354 for (i = 0; i < s->filter_channels; i++) {
00355 bufin[i] = av_malloc((nb_samples + s->temp_len) * sizeof(short));
00356 memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short));
00357 buftmp2[i] = bufin[i] + s->temp_len;
00358 bufout[i] = av_malloc(lenout * sizeof(short));
00359 }
00360
00361 if (s->input_channels == 2 && s->output_channels == 1) {
00362 buftmp3[0] = output;
00363 stereo_to_mono(buftmp2[0], input, nb_samples);
00364 } else if (s->output_channels >= 2 && s->input_channels == 1) {
00365 buftmp3[0] = bufout[0];
00366 memcpy(buftmp2[0], input, nb_samples * sizeof(short));
00367 } else if (s->input_channels == 6 && s->output_channels ==2) {
00368 buftmp3[0] = bufout[0];
00369 buftmp3[1] = bufout[1];
00370 surround_to_stereo(buftmp2, input, s->input_channels, nb_samples);
00371 } else if (s->output_channels >= s->input_channels && s->input_channels >= 2) {
00372 for (i = 0; i < s->input_channels; i++) {
00373 buftmp3[i] = bufout[i];
00374 }
00375 deinterleave(buftmp2, input, s->input_channels, nb_samples);
00376 } else {
00377 buftmp3[0] = output;
00378 memcpy(buftmp2[0], input, nb_samples * sizeof(short));
00379 }
00380
00381 nb_samples += s->temp_len;
00382
00383
00384 nb_samples1 = 0;
00385 for (i = 0; i < s->filter_channels; i++) {
00386 int consumed;
00387 int is_last = i + 1 == s->filter_channels;
00388
00389 nb_samples1 = av_resample(s->resample_context, buftmp3[i], bufin[i],
00390 &consumed, nb_samples, lenout, is_last);
00391 s->temp_len = nb_samples - consumed;
00392 s->temp[i] = av_realloc(s->temp[i], s->temp_len * sizeof(short));
00393 memcpy(s->temp[i], bufin[i] + consumed, s->temp_len * sizeof(short));
00394 }
00395
00396 if (s->output_channels == 2 && s->input_channels == 1) {
00397 mono_to_stereo(output, buftmp3[0], nb_samples1);
00398 } else if (s->output_channels == 6 && s->input_channels == 2) {
00399 ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
00400 } else if ((s->output_channels == s->input_channels && s->input_channels >= 2) ||
00401 (s->output_channels == 2 && s->input_channels == 6)) {
00402 interleave(output, buftmp3, s->output_channels, nb_samples1);
00403 }
00404
00405 if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
00406 int istride[1] = { 2 };
00407 int ostride[1] = { s->sample_size[1] };
00408 const void *ibuf[1] = { output };
00409 void *obuf[1] = { output_bak };
00410
00411 if (av_audio_convert(s->convert_ctx[1], obuf, ostride,
00412 ibuf, istride, nb_samples1 * s->output_channels) < 0) {
00413 av_log(s->resample_context, AV_LOG_ERROR,
00414 "Audio sample format convertion failed\n");
00415 return 0;
00416 }
00417 }
00418
00419 for (i = 0; i < s->filter_channels; i++) {
00420 av_free(bufin[i]);
00421 av_free(bufout[i]);
00422 }
00423
00424 return nb_samples1;
00425 }
00426
00427 void audio_resample_close(ReSampleContext *s)
00428 {
00429 int i;
00430 av_resample_close(s->resample_context);
00431 for (i = 0; i < s->filter_channels; i++)
00432 av_freep(&s->temp[i]);
00433 av_freep(&s->buffer[0]);
00434 av_freep(&s->buffer[1]);
00435 av_audio_convert_free(s->convert_ctx[0]);
00436 av_audio_convert_free(s->convert_ctx[1]);
00437 av_free(s);
00438 }