35 #if FF_API_AVCODEC_RESAMPLE
37 #define MAX_CHANNELS 8
39 struct AVResampleContext;
43 return "audioresample";
47 static const AVClass audioresample_context_class = {
51 struct ReSampleContext {
52 struct AVResampleContext *resample_context;
57 int input_channels, output_channels, filter_channels;
60 unsigned sample_size[2];
62 unsigned buffer_size[2];
66 static void stereo_to_mono(
short *output,
short *input,
int n1)
74 q[0] = (p[0] + p[1]) >> 1;
75 q[1] = (p[2] + p[3]) >> 1;
76 q[2] = (p[4] + p[5]) >> 1;
77 q[3] = (p[6] + p[7]) >> 1;
83 q[0] = (p[0] + p[1]) >> 1;
91 static void mono_to_stereo(
short *output,
short *input,
int n1)
100 v = p[0]; q[0] =
v; q[1] =
v;
101 v = p[1]; q[2] =
v; q[3] =
v;
102 v = p[2]; q[4] =
v; q[5] =
v;
103 v = p[3]; q[6] =
v; q[7] =
v;
109 v = p[0]; q[0] =
v; q[1] =
v;
123 static void surround_to_stereo(
short **output,
short *input,
int channels,
int samples)
128 for (i = 0; i <
samples; i++) {
137 l = av_clip_int16(fl + (0.5 * rl) + (0.7 * c));
138 r = av_clip_int16(fr + (0.5 * rr) + (0.7 * c));
149 static void deinterleave(
short **output,
short *input,
int channels,
int samples)
153 for (i = 0; i <
samples; i++) {
154 for (j = 0; j < channels; j++) {
155 *output[j]++ = *input++;
160 static void interleave(
short *output,
short **input,
int channels,
int samples)
164 for (i = 0; i <
samples; i++) {
165 for (j = 0; j < channels; j++) {
166 *output++ = *input[j]++;
171 static void ac3_5p1_mux(
short *output,
short *input1,
short *input2,
int n)
176 for (i = 0; i < n; i++) {
180 *output++ = (l / 2) + (r / 2);
188 #define SUPPORT_RESAMPLE(ch1, ch2, ch3, ch4, ch5, ch6, ch7, ch8) \
189 ch8<<7 | ch7<<6 | ch6<<5 | ch5<<4 | ch4<<3 | ch3<<2 | ch2<<1 | ch1<<0
193 SUPPORT_RESAMPLE(1, 1, 0, 0, 0, 0, 0, 0),
194 SUPPORT_RESAMPLE(1, 1, 0, 0, 0, 1, 0, 0),
195 SUPPORT_RESAMPLE(0, 0, 1, 0, 0, 0, 0, 0),
196 SUPPORT_RESAMPLE(0, 0, 0, 1, 0, 0, 0, 0),
197 SUPPORT_RESAMPLE(0, 0, 0, 0, 1, 0, 0, 0),
198 SUPPORT_RESAMPLE(0, 1, 0, 0, 0, 1, 0, 0),
199 SUPPORT_RESAMPLE(0, 0, 0, 0, 0, 0, 1, 0),
200 SUPPORT_RESAMPLE(0, 0, 0, 0, 0, 0, 0, 1),
203 ReSampleContext *av_audio_resample_init(
int output_channels,
int input_channels,
204 int output_rate,
int input_rate,
207 int filter_length,
int log2_phase_count,
208 int linear,
double cutoff)
214 "Resampling with input channels greater than %d is unsupported.\n",
218 if (!(supported_resampling[input_channels-1] & (1<<(output_channels-1)))) {
221 "output channels for %d input channel%s", input_channels,
222 input_channels > 1 ?
"s:" :
":");
224 if (supported_resampling[input_channels-1] & (1<<i))
236 s->ratio = (float)output_rate / (
float)input_rate;
238 s->input_channels = input_channels;
239 s->output_channels = output_channels;
241 s->filter_channels = s->input_channels;
242 if (s->output_channels < s->filter_channels)
243 s->filter_channels = s->output_channels;
245 s->sample_fmt[0] = sample_fmt_in;
246 s->sample_fmt[1] = sample_fmt_out;
252 s->sample_fmt[0], 1,
NULL, 0))) {
254 "Cannot convert %s sample format to s16 sample format\n",
265 "Cannot convert s16 sample format to %s sample format\n",
273 s->resample_context = av_resample_init(output_rate, input_rate,
274 filter_length, log2_phase_count,
277 *(
const AVClass**)s->resample_context = &audioresample_context_class;
284 int audio_resample(ReSampleContext *s,
short *output,
short *input,
int nb_samples)
290 short *output_bak =
NULL;
293 if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) {
295 memcpy(output, input, nb_samples * s->input_channels *
sizeof(
short));
300 int istride[1] = { s->sample_size[0] };
301 int ostride[1] = { 2 };
302 const void *ibuf[1] = { input };
304 unsigned input_size = nb_samples * s->input_channels * 2;
306 if (!s->buffer_size[0] || s->buffer_size[0] < input_size) {
308 s->buffer_size[0] = input_size;
309 s->buffer[0] =
av_malloc(s->buffer_size[0]);
316 obuf[0] = s->buffer[0];
319 ibuf, istride, nb_samples * s->input_channels) < 0) {
321 "Audio sample format conversion failed\n");
325 input = s->buffer[0];
328 lenout= 2*s->output_channels*nb_samples * s->ratio + 16;
335 if (!s->buffer_size[1] || s->buffer_size[1] < out_size) {
337 s->buffer_size[1] = out_size;
338 s->buffer[1] =
av_malloc(s->buffer_size[1]);
345 output = s->buffer[1];
349 for (i = 0; i < s->filter_channels; i++) {
350 bufin[i] =
av_malloc((nb_samples + s->temp_len) *
sizeof(
short));
351 memcpy(bufin[i], s->temp[i], s->temp_len *
sizeof(
short));
352 buftmp2[i] = bufin[i] + s->temp_len;
353 bufout[i] =
av_malloc(lenout *
sizeof(
short));
356 if (s->input_channels == 2 && s->output_channels == 1) {
358 stereo_to_mono(buftmp2[0], input, nb_samples);
359 }
else if (s->output_channels >= 2 && s->input_channels == 1) {
360 buftmp3[0] = bufout[0];
361 memcpy(buftmp2[0], input, nb_samples *
sizeof(
short));
362 }
else if (s->input_channels == 6 && s->output_channels ==2) {
363 buftmp3[0] = bufout[0];
364 buftmp3[1] = bufout[1];
365 surround_to_stereo(buftmp2, input, s->input_channels, nb_samples);
366 }
else if (s->output_channels >= s->input_channels && s->input_channels >= 2) {
367 for (i = 0; i < s->input_channels; i++) {
368 buftmp3[i] = bufout[i];
370 deinterleave(buftmp2, input, s->input_channels, nb_samples);
373 memcpy(buftmp2[0], input, nb_samples *
sizeof(
short));
376 nb_samples += s->temp_len;
380 for (i = 0; i < s->filter_channels; i++) {
382 int is_last = i + 1 == s->filter_channels;
384 nb_samples1 = av_resample(s->resample_context, buftmp3[i], bufin[i],
385 &consumed, nb_samples, lenout, is_last);
386 s->temp_len = nb_samples - consumed;
387 s->temp[i] =
av_realloc(s->temp[i], s->temp_len *
sizeof(
short));
388 memcpy(s->temp[i], bufin[i] + consumed, s->temp_len *
sizeof(
short));
391 if (s->output_channels == 2 && s->input_channels == 1) {
392 mono_to_stereo(output, buftmp3[0], nb_samples1);
393 }
else if (s->output_channels == 6 && s->input_channels == 2) {
394 ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
395 }
else if ((s->output_channels == s->input_channels && s->input_channels >= 2) ||
396 (s->output_channels == 2 && s->input_channels == 6)) {
397 interleave(output, buftmp3, s->output_channels, nb_samples1);
401 int istride[1] = { 2 };
402 int ostride[1] = { s->sample_size[1] };
403 const void *ibuf[1] = { output };
404 void *obuf[1] = { output_bak };
407 ibuf, istride, nb_samples1 * s->output_channels) < 0) {
409 "Audio sample format conversion failed\n");
414 for (i = 0; i < s->filter_channels; i++) {
422 void audio_resample_close(ReSampleContext *s)
425 av_resample_close(s->resample_context);
426 for (i = 0; i < s->filter_channels; i++)