2 * samplerate conversion for both audio and video
3 * Copyright (c) 2000 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * samplerate conversion for both audio and video
28 #include "audioconvert.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/samplefmt.h"
32 #define MAX_CHANNELS 8
34 struct AVResampleContext;
36 static const char *context_to_name(void *ptr)
38 return "audioresample";
41 static const AVOption options[] = {{NULL}};
42 static const AVClass audioresample_context_class = {
43 "ReSampleContext", context_to_name, options, LIBAVUTIL_VERSION_INT
46 struct ReSampleContext {
47 struct AVResampleContext *resample_context;
48 short *temp[MAX_CHANNELS];
52 int input_channels, output_channels, filter_channels;
53 AVAudioConvert *convert_ctx[2];
54 enum AVSampleFormat sample_fmt[2]; ///< input and output sample format
55 unsigned sample_size[2]; ///< size of one sample in sample_fmt
56 short *buffer[2]; ///< buffers used for conversion to S16
57 unsigned buffer_size[2]; ///< sizes of allocated buffers
60 /* n1: number of samples */
61 static void stereo_to_mono(short *output, short *input, int n1)
69 q[0] = (p[0] + p[1]) >> 1;
70 q[1] = (p[2] + p[3]) >> 1;
71 q[2] = (p[4] + p[5]) >> 1;
72 q[3] = (p[6] + p[7]) >> 1;
78 q[0] = (p[0] + p[1]) >> 1;
85 /* n1: number of samples */
86 static void mono_to_stereo(short *output, short *input, int n1)
95 v = p[0]; q[0] = v; q[1] = v;
96 v = p[1]; q[2] = v; q[3] = v;
97 v = p[2]; q[4] = v; q[5] = v;
98 v = p[3]; q[6] = v; q[7] = v;
104 v = p[0]; q[0] = v; q[1] = v;
111 static void deinterleave(short **output, short *input, int channels, int samples)
115 for (i = 0; i < samples; i++) {
116 for (j = 0; j < channels; j++) {
117 *output[j]++ = *input++;
122 static void interleave(short *output, short **input, int channels, int samples)
126 for (i = 0; i < samples; i++) {
127 for (j = 0; j < channels; j++) {
128 *output++ = *input[j]++;
133 static void ac3_5p1_mux(short *output, short *input1, short *input2, int n)
138 for (i = 0; i < n; i++) {
141 *output++ = l; /* left */
142 *output++ = (l / 2) + (r / 2); /* center */
143 *output++ = r; /* right */
144 *output++ = 0; /* left surround */
145 *output++ = 0; /* right surroud */
146 *output++ = 0; /* low freq */
150 ReSampleContext *av_audio_resample_init(int output_channels, int input_channels,
151 int output_rate, int input_rate,
152 enum AVSampleFormat sample_fmt_out,
153 enum AVSampleFormat sample_fmt_in,
154 int filter_length, int log2_phase_count,
155 int linear, double cutoff)
159 if (input_channels > MAX_CHANNELS) {
160 av_log(NULL, AV_LOG_ERROR,
161 "Resampling with input channels greater than %d is unsupported.\n",
165 if (output_channels != input_channels &&
166 (input_channels > 2 ||
167 output_channels > 2 &&
168 !(output_channels == 6 && input_channels == 2))) {
169 av_log(NULL, AV_LOG_ERROR,
170 "Resampling output channel count must be 1 or 2 for mono input; 1, 2 or 6 for stereo input; or N for N channel input.\n");
174 s = av_mallocz(sizeof(ReSampleContext));
176 av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for resample context.\n");
180 s->ratio = (float)output_rate / (float)input_rate;
182 s->input_channels = input_channels;
183 s->output_channels = output_channels;
185 s->filter_channels = s->input_channels;
186 if (s->output_channels < s->filter_channels)
187 s->filter_channels = s->output_channels;
189 s->sample_fmt[0] = sample_fmt_in;
190 s->sample_fmt[1] = sample_fmt_out;
191 s->sample_size[0] = av_get_bytes_per_sample(s->sample_fmt[0]);
192 s->sample_size[1] = av_get_bytes_per_sample(s->sample_fmt[1]);
194 if (s->sample_fmt[0] != AV_SAMPLE_FMT_S16) {
195 if (!(s->convert_ctx[0] = av_audio_convert_alloc(AV_SAMPLE_FMT_S16, 1,
196 s->sample_fmt[0], 1, NULL, 0))) {
197 av_log(s, AV_LOG_ERROR,
198 "Cannot convert %s sample format to s16 sample format\n",
199 av_get_sample_fmt_name(s->sample_fmt[0]));
205 if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
206 if (!(s->convert_ctx[1] = av_audio_convert_alloc(s->sample_fmt[1], 1,
207 AV_SAMPLE_FMT_S16, 1, NULL, 0))) {
208 av_log(s, AV_LOG_ERROR,
209 "Cannot convert s16 sample format to %s sample format\n",
210 av_get_sample_fmt_name(s->sample_fmt[1]));
211 av_audio_convert_free(s->convert_ctx[0]);
217 s->resample_context = av_resample_init(output_rate, input_rate,
218 filter_length, log2_phase_count,
221 *(const AVClass**)s->resample_context = &audioresample_context_class;
226 /* resample audio. 'nb_samples' is the number of input samples */
227 /* XXX: optimize it ! */
228 int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
231 short *bufin[MAX_CHANNELS];
232 short *bufout[MAX_CHANNELS];
233 short *buftmp2[MAX_CHANNELS], *buftmp3[MAX_CHANNELS];
234 short *output_bak = NULL;
237 if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) {
239 memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
243 if (s->sample_fmt[0] != AV_SAMPLE_FMT_S16) {
244 int istride[1] = { s->sample_size[0] };
245 int ostride[1] = { 2 };
246 const void *ibuf[1] = { input };
248 unsigned input_size = nb_samples * s->input_channels * 2;
250 if (!s->buffer_size[0] || s->buffer_size[0] < input_size) {
251 av_free(s->buffer[0]);
252 s->buffer_size[0] = input_size;
253 s->buffer[0] = av_malloc(s->buffer_size[0]);
255 av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
260 obuf[0] = s->buffer[0];
262 if (av_audio_convert(s->convert_ctx[0], obuf, ostride,
263 ibuf, istride, nb_samples * s->input_channels) < 0) {
264 av_log(s->resample_context, AV_LOG_ERROR,
265 "Audio sample format conversion failed\n");
269 input = s->buffer[0];
272 lenout = 4 * nb_samples * s->ratio + 16;
274 if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
275 int out_size = lenout * av_get_bytes_per_sample(s->sample_fmt[1]) *
279 if (!s->buffer_size[1] || s->buffer_size[1] < out_size) {
280 av_free(s->buffer[1]);
281 s->buffer_size[1] = out_size;
282 s->buffer[1] = av_malloc(s->buffer_size[1]);
284 av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
289 output = s->buffer[1];
292 /* XXX: move those malloc to resample init code */
293 for (i = 0; i < s->filter_channels; i++) {
294 bufin[i] = av_malloc((nb_samples + s->temp_len) * sizeof(short));
295 memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short));
296 buftmp2[i] = bufin[i] + s->temp_len;
297 bufout[i] = av_malloc(lenout * sizeof(short));
300 if (s->input_channels == 2 && s->output_channels == 1) {
302 stereo_to_mono(buftmp2[0], input, nb_samples);
303 } else if (s->output_channels >= 2 && s->input_channels == 1) {
304 buftmp3[0] = bufout[0];
305 memcpy(buftmp2[0], input, nb_samples * sizeof(short));
306 } else if (s->output_channels >= s->input_channels && s->input_channels >= 2) {
307 for (i = 0; i < s->input_channels; i++) {
308 buftmp3[i] = bufout[i];
310 deinterleave(buftmp2, input, s->input_channels, nb_samples);
313 memcpy(buftmp2[0], input, nb_samples * sizeof(short));
316 nb_samples += s->temp_len;
318 /* resample each channel */
319 nb_samples1 = 0; /* avoid warning */
320 for (i = 0; i < s->filter_channels; i++) {
322 int is_last = i + 1 == s->filter_channels;
324 nb_samples1 = av_resample(s->resample_context, buftmp3[i], bufin[i],
325 &consumed, nb_samples, lenout, is_last);
326 s->temp_len = nb_samples - consumed;
327 s->temp[i] = av_realloc(s->temp[i], s->temp_len * sizeof(short));
328 memcpy(s->temp[i], bufin[i] + consumed, s->temp_len * sizeof(short));
331 if (s->output_channels == 2 && s->input_channels == 1) {
332 mono_to_stereo(output, buftmp3[0], nb_samples1);
333 } else if (s->output_channels == 6 && s->input_channels == 2) {
334 ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
335 } else if (s->output_channels == s->input_channels && s->input_channels >= 2) {
336 interleave(output, buftmp3, s->output_channels, nb_samples1);
339 if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
340 int istride[1] = { 2 };
341 int ostride[1] = { s->sample_size[1] };
342 const void *ibuf[1] = { output };
343 void *obuf[1] = { output_bak };
345 if (av_audio_convert(s->convert_ctx[1], obuf, ostride,
346 ibuf, istride, nb_samples1 * s->output_channels) < 0) {
347 av_log(s->resample_context, AV_LOG_ERROR,
348 "Audio sample format convertion failed\n");
353 for (i = 0; i < s->filter_channels; i++) {
361 void audio_resample_close(ReSampleContext *s)
364 av_resample_close(s->resample_context);
365 for (i = 0; i < s->filter_channels; i++)
366 av_freep(&s->temp[i]);
367 av_freep(&s->buffer[0]);
368 av_freep(&s->buffer[1]);
369 av_audio_convert_free(s->convert_ctx[0]);
370 av_audio_convert_free(s->convert_ctx[1]);