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