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;
00125 fl = input[0];
00126 fr = input[1];
00127 c = input[2];
00128
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 s->resample_context = av_resample_init(output_rate, input_rate,
00269 filter_length, log2_phase_count,
00270 linear, cutoff);
00271
00272 *(const AVClass**)s->resample_context = &audioresample_context_class;
00273
00274 return s;
00275 }
00276
00277
00278
00279 int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
00280 {
00281 int i, nb_samples1;
00282 short *bufin[MAX_CHANNELS];
00283 short *bufout[MAX_CHANNELS];
00284 short *buftmp2[MAX_CHANNELS], *buftmp3[MAX_CHANNELS];
00285 short *output_bak = NULL;
00286 int lenout;
00287
00288 if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) {
00289
00290 memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
00291 return nb_samples;
00292 }
00293
00294 if (s->sample_fmt[0] != AV_SAMPLE_FMT_S16) {
00295 int istride[1] = { s->sample_size[0] };
00296 int ostride[1] = { 2 };
00297 const void *ibuf[1] = { input };
00298 void *obuf[1];
00299 unsigned input_size = nb_samples * s->input_channels * 2;
00300
00301 if (!s->buffer_size[0] || s->buffer_size[0] < input_size) {
00302 av_free(s->buffer[0]);
00303 s->buffer_size[0] = input_size;
00304 s->buffer[0] = av_malloc(s->buffer_size[0]);
00305 if (!s->buffer[0]) {
00306 av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
00307 return 0;
00308 }
00309 }
00310
00311 obuf[0] = s->buffer[0];
00312
00313 if (av_audio_convert(s->convert_ctx[0], obuf, ostride,
00314 ibuf, istride, nb_samples * s->input_channels) < 0) {
00315 av_log(s->resample_context, AV_LOG_ERROR,
00316 "Audio sample format conversion failed\n");
00317 return 0;
00318 }
00319
00320 input = s->buffer[0];
00321 }
00322
00323 lenout= 2*s->output_channels*nb_samples * s->ratio + 16;
00324
00325 if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
00326 int out_size = lenout * av_get_bytes_per_sample(s->sample_fmt[1]) *
00327 s->output_channels;
00328 output_bak = output;
00329
00330 if (!s->buffer_size[1] || s->buffer_size[1] < out_size) {
00331 av_free(s->buffer[1]);
00332 s->buffer_size[1] = out_size;
00333 s->buffer[1] = av_malloc(s->buffer_size[1]);
00334 if (!s->buffer[1]) {
00335 av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
00336 return 0;
00337 }
00338 }
00339
00340 output = s->buffer[1];
00341 }
00342
00343
00344 for (i = 0; i < s->filter_channels; i++) {
00345 bufin[i] = av_malloc((nb_samples + s->temp_len) * sizeof(short));
00346 memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short));
00347 buftmp2[i] = bufin[i] + s->temp_len;
00348 bufout[i] = av_malloc(lenout * sizeof(short));
00349 }
00350
00351 if (s->input_channels == 2 && s->output_channels == 1) {
00352 buftmp3[0] = output;
00353 stereo_to_mono(buftmp2[0], input, nb_samples);
00354 } else if (s->output_channels >= 2 && s->input_channels == 1) {
00355 buftmp3[0] = bufout[0];
00356 memcpy(buftmp2[0], input, nb_samples * sizeof(short));
00357 } else if (s->input_channels == 6 && s->output_channels ==2) {
00358 buftmp3[0] = bufout[0];
00359 buftmp3[1] = bufout[1];
00360 surround_to_stereo(buftmp2, input, s->input_channels, nb_samples);
00361 } else if (s->output_channels >= s->input_channels && s->input_channels >= 2) {
00362 for (i = 0; i < s->input_channels; i++) {
00363 buftmp3[i] = bufout[i];
00364 }
00365 deinterleave(buftmp2, input, s->input_channels, nb_samples);
00366 } else {
00367 buftmp3[0] = output;
00368 memcpy(buftmp2[0], input, nb_samples * sizeof(short));
00369 }
00370
00371 nb_samples += s->temp_len;
00372
00373
00374 nb_samples1 = 0;
00375 for (i = 0; i < s->filter_channels; i++) {
00376 int consumed;
00377 int is_last = i + 1 == s->filter_channels;
00378
00379 nb_samples1 = av_resample(s->resample_context, buftmp3[i], bufin[i],
00380 &consumed, nb_samples, lenout, is_last);
00381 s->temp_len = nb_samples - consumed;
00382 s->temp[i] = av_realloc(s->temp[i], s->temp_len * sizeof(short));
00383 memcpy(s->temp[i], bufin[i] + consumed, s->temp_len * sizeof(short));
00384 }
00385
00386 if (s->output_channels == 2 && s->input_channels == 1) {
00387 mono_to_stereo(output, buftmp3[0], nb_samples1);
00388 } else if (s->output_channels == 6 && s->input_channels == 2) {
00389 ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
00390 } else if ((s->output_channels == s->input_channels && s->input_channels >= 2) ||
00391 (s->output_channels == 2 && s->input_channels == 6)) {
00392 interleave(output, buftmp3, s->output_channels, nb_samples1);
00393 }
00394
00395 if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
00396 int istride[1] = { 2 };
00397 int ostride[1] = { s->sample_size[1] };
00398 const void *ibuf[1] = { output };
00399 void *obuf[1] = { output_bak };
00400
00401 if (av_audio_convert(s->convert_ctx[1], obuf, ostride,
00402 ibuf, istride, nb_samples1 * s->output_channels) < 0) {
00403 av_log(s->resample_context, AV_LOG_ERROR,
00404 "Audio sample format convertion failed\n");
00405 return 0;
00406 }
00407 }
00408
00409 for (i = 0; i < s->filter_channels; i++) {
00410 av_free(bufin[i]);
00411 av_free(bufout[i]);
00412 }
00413
00414 return nb_samples1;
00415 }
00416
00417 void audio_resample_close(ReSampleContext *s)
00418 {
00419 int i;
00420 av_resample_close(s->resample_context);
00421 for (i = 0; i < s->filter_channels; i++)
00422 av_freep(&s->temp[i]);
00423 av_freep(&s->buffer[0]);
00424 av_freep(&s->buffer[1]);
00425 av_audio_convert_free(s->convert_ctx[0]);
00426 av_audio_convert_free(s->convert_ctx[1]);
00427 av_free(s);
00428 }