2 * Sample rate convertion for both audio and video
3 * Copyright (c) 2000 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Sample rate convertion for both audio and video.
28 /* fractional resampling */
29 uint32_t incr; /* fractional increment */
32 /* integer down sample */
33 int iratio; /* integer divison ratio */
36 } ReSampleChannelContext;
38 struct ReSampleContext {
39 ReSampleChannelContext channel_ctx[2];
42 int input_channels, output_channels, filter_channels;
47 #define FRAC (1 << FRAC_BITS)
49 static void init_mono_resample(ReSampleChannelContext *s, float ratio)
52 s->iratio = (int)floorf(ratio);
55 s->incr = (int)((ratio / s->iratio) * FRAC);
58 s->icount = s->iratio;
60 s->inv = (FRAC / s->iratio);
63 /* fractional audio resampling */
64 static int fractional_resample(ReSampleChannelContext *s, short *output, short *input, int nb_samples)
66 unsigned int frac, incr;
75 pend = input + nb_samples;
81 *q++ = (l0 * (FRAC - frac) + l1 * frac) >> FRAC_BITS;
82 frac = frac + s->incr;
83 while (frac >= FRAC) {
97 static int integer_downsample(ReSampleChannelContext *s, short *output, short *input, int nb_samples)
103 pend = input + nb_samples;
112 *q++ = (sum * s->inv) >> FRAC_BITS;
124 /* n1: number of samples */
125 static void stereo_to_mono(short *output, short *input, int n1)
133 q[0] = (p[0] + p[1]) >> 1;
134 q[1] = (p[2] + p[3]) >> 1;
135 q[2] = (p[4] + p[5]) >> 1;
136 q[3] = (p[6] + p[7]) >> 1;
142 q[0] = (p[0] + p[1]) >> 1;
149 /* n1: number of samples */
150 static void mono_to_stereo(short *output, short *input, int n1)
159 v = p[0]; q[0] = v; q[1] = v;
160 v = p[1]; q[2] = v; q[3] = v;
161 v = p[2]; q[4] = v; q[5] = v;
162 v = p[3]; q[6] = v; q[7] = v;
168 v = p[0]; q[0] = v; q[1] = v;
175 /* XXX: should use more abstract 'N' channels system */
176 static void stereo_split(short *output1, short *output2, short *input, int n)
181 *output1++ = *input++;
182 *output2++ = *input++;
186 static void stereo_mux(short *output, short *input1, short *input2, int n)
191 *output++ = *input1++;
192 *output++ = *input2++;
196 static void ac3_5p1_mux(short *output, short *input1, short *input2, int n)
204 *output++ = l; /* left */
205 *output++ = (l/2)+(r/2); /* center */
206 *output++ = r; /* right */
207 *output++ = 0; /* left surround */
208 *output++ = 0; /* right surroud */
209 *output++ = 0; /* low freq */
213 static int mono_resample(ReSampleChannelContext *s, short *output, short *input, int nb_samples)
218 buf1= (short*)av_malloc( nb_samples * sizeof(short) );
220 /* first downsample by an integer factor with averaging filter */
223 nb_samples = integer_downsample(s, buftmp, input, nb_samples);
228 /* then do a fractional resampling with linear interpolation */
229 if (s->incr != FRAC) {
230 nb_samples = fractional_resample(s, output, buftmp, nb_samples);
232 memcpy(output, buftmp, nb_samples * sizeof(short));
238 ReSampleContext *audio_resample_init(int output_channels, int input_channels,
239 int output_rate, int input_rate)
244 if ( input_channels > 2)
246 printf("Resampling with input channels greater than 2 unsupported.");
250 s = av_mallocz(sizeof(ReSampleContext));
253 printf("Can't allocate memory for resample context.");
257 s->ratio = (float)output_rate / (float)input_rate;
259 s->input_channels = input_channels;
260 s->output_channels = output_channels;
262 s->filter_channels = s->input_channels;
263 if (s->output_channels < s->filter_channels)
264 s->filter_channels = s->output_channels;
267 * ac3 output is the only case where filter_channels could be greater than 2.
268 * input channels can't be greater than 2, so resample the 2 channels and then
269 * expand to 6 channels after the resampling.
271 if(s->filter_channels>2)
272 s->filter_channels = 2;
274 for(i=0;i<s->filter_channels;i++) {
275 init_mono_resample(&s->channel_ctx[i], s->ratio);
280 /* resample audio. 'nb_samples' is the number of input samples */
281 /* XXX: optimize it ! */
282 /* XXX: do it with polyphase filters, since the quality here is
283 HORRIBLE. Return the number of samples available in output */
284 int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
289 short *buftmp2[2], *buftmp3[2];
292 if (s->input_channels == s->output_channels && s->ratio == 1.0) {
294 memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
298 /* XXX: move those malloc to resample init code */
299 bufin[0]= (short*) av_malloc( nb_samples * sizeof(short) );
300 bufin[1]= (short*) av_malloc( nb_samples * sizeof(short) );
302 /* make some zoom to avoid round pb */
303 lenout= (int)(nb_samples * s->ratio) + 16;
304 bufout[0]= (short*) av_malloc( lenout * sizeof(short) );
305 bufout[1]= (short*) av_malloc( lenout * sizeof(short) );
307 if (s->input_channels == 2 &&
308 s->output_channels == 1) {
309 buftmp2[0] = bufin[0];
311 stereo_to_mono(buftmp2[0], input, nb_samples);
312 } else if (s->output_channels >= 2 && s->input_channels == 1) {
314 buftmp3[0] = bufout[0];
315 } else if (s->output_channels >= 2) {
316 buftmp2[0] = bufin[0];
317 buftmp2[1] = bufin[1];
318 buftmp3[0] = bufout[0];
319 buftmp3[1] = bufout[1];
320 stereo_split(buftmp2[0], buftmp2[1], input, nb_samples);
326 /* resample each channel */
327 nb_samples1 = 0; /* avoid warning */
328 for(i=0;i<s->filter_channels;i++) {
329 nb_samples1 = mono_resample(&s->channel_ctx[i], buftmp3[i], buftmp2[i], nb_samples);
332 if (s->output_channels == 2 && s->input_channels == 1) {
333 mono_to_stereo(output, buftmp3[0], nb_samples1);
334 } else if (s->output_channels == 2) {
335 stereo_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
336 } else if (s->output_channels == 6) {
337 ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
348 void audio_resample_close(ReSampleContext *s)