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