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