ba2bb81da225eb8f89a3b23ee90224c6591584ad
[libav.git] / libavcodec / resample.c
1 /*
2 * Sample rate convertion for both audio and video
3 * Copyright (c) 2000 Fabrice Bellard.
4 *
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.
9 *
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.
14 *
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
18 */
19
20 /**
21 * @file resample.c
22 * Sample rate convertion for both audio and video.
23 */
24
25 #include "avcodec.h"
26
27 #if defined (CONFIG_OS2)
28 #define floorf(n) floor(n)
29 #endif
30
31 typedef struct {
32 /* fractional resampling */
33 uint32_t incr; /* fractional increment */
34 uint32_t frac;
35 int last_sample;
36 /* integer down sample */
37 int iratio; /* integer divison ratio */
38 int icount, isum;
39 int inv;
40 } ReSampleChannelContext;
41
42 struct ReSampleContext {
43 ReSampleChannelContext channel_ctx[2];
44 float ratio;
45 /* channel convert */
46 int input_channels, output_channels, filter_channels;
47 };
48
49
50 #define FRAC_BITS 16
51 #define FRAC (1 << FRAC_BITS)
52
53 static void init_mono_resample(ReSampleChannelContext *s, float ratio)
54 {
55 ratio = 1.0 / ratio;
56 s->iratio = (int)floorf(ratio);
57 if (s->iratio == 0)
58 s->iratio = 1;
59 s->incr = (int)((ratio / s->iratio) * FRAC);
60 s->frac = FRAC;
61 s->last_sample = 0;
62 s->icount = s->iratio;
63 s->isum = 0;
64 s->inv = (FRAC / s->iratio);
65 }
66
67 /* fractional audio resampling */
68 static int fractional_resample(ReSampleChannelContext *s, short *output, short *input, int nb_samples)
69 {
70 unsigned int frac, incr;
71 int l0, l1;
72 short *q, *p, *pend;
73
74 l0 = s->last_sample;
75 incr = s->incr;
76 frac = s->frac;
77
78 p = input;
79 pend = input + nb_samples;
80 q = output;
81
82 l1 = *p++;
83 for(;;) {
84 /* interpolate */
85 *q++ = (l0 * (FRAC - frac) + l1 * frac) >> FRAC_BITS;
86 frac = frac + s->incr;
87 while (frac >= FRAC) {
88 frac -= FRAC;
89 if (p >= pend)
90 goto the_end;
91 l0 = l1;
92 l1 = *p++;
93 }
94 }
95 the_end:
96 s->last_sample = l1;
97 s->frac = frac;
98 return q - output;
99 }
100
101 static int integer_downsample(ReSampleChannelContext *s, short *output, short *input, int nb_samples)
102 {
103 short *q, *p, *pend;
104 int c, sum;
105
106 p = input;
107 pend = input + nb_samples;
108 q = output;
109
110 c = s->icount;
111 sum = s->isum;
112
113 for(;;) {
114 sum += *p++;
115 if (--c == 0) {
116 *q++ = (sum * s->inv) >> FRAC_BITS;
117 c = s->iratio;
118 sum = 0;
119 }
120 if (p >= pend)
121 break;
122 }
123 s->isum = sum;
124 s->icount = c;
125 return q - output;
126 }
127
128 /* n1: number of samples */
129 static void stereo_to_mono(short *output, short *input, int n1)
130 {
131 short *p, *q;
132 int n = n1;
133
134 p = input;
135 q = output;
136 while (n >= 4) {
137 q[0] = (p[0] + p[1]) >> 1;
138 q[1] = (p[2] + p[3]) >> 1;
139 q[2] = (p[4] + p[5]) >> 1;
140 q[3] = (p[6] + p[7]) >> 1;
141 q += 4;
142 p += 8;
143 n -= 4;
144 }
145 while (n > 0) {
146 q[0] = (p[0] + p[1]) >> 1;
147 q++;
148 p += 2;
149 n--;
150 }
151 }
152
153 /* n1: number of samples */
154 static void mono_to_stereo(short *output, short *input, int n1)
155 {
156 short *p, *q;
157 int n = n1;
158 int v;
159
160 p = input;
161 q = output;
162 while (n >= 4) {
163 v = p[0]; q[0] = v; q[1] = v;
164 v = p[1]; q[2] = v; q[3] = v;
165 v = p[2]; q[4] = v; q[5] = v;
166 v = p[3]; q[6] = v; q[7] = v;
167 q += 8;
168 p += 4;
169 n -= 4;
170 }
171 while (n > 0) {
172 v = p[0]; q[0] = v; q[1] = v;
173 q += 2;
174 p += 1;
175 n--;
176 }
177 }
178
179 /* XXX: should use more abstract 'N' channels system */
180 static void stereo_split(short *output1, short *output2, short *input, int n)
181 {
182 int i;
183
184 for(i=0;i<n;i++) {
185 *output1++ = *input++;
186 *output2++ = *input++;
187 }
188 }
189
190 static void stereo_mux(short *output, short *input1, short *input2, int n)
191 {
192 int i;
193
194 for(i=0;i<n;i++) {
195 *output++ = *input1++;
196 *output++ = *input2++;
197 }
198 }
199
200 static int mono_resample(ReSampleChannelContext *s, short *output, short *input, int nb_samples)
201 {
202 short *buf1;
203 short *buftmp;
204
205 buf1= (short*)av_malloc( nb_samples * sizeof(short) );
206
207 /* first downsample by an integer factor with averaging filter */
208 if (s->iratio > 1) {
209 buftmp = buf1;
210 nb_samples = integer_downsample(s, buftmp, input, nb_samples);
211 } else {
212 buftmp = input;
213 }
214
215 /* then do a fractional resampling with linear interpolation */
216 if (s->incr != FRAC) {
217 nb_samples = fractional_resample(s, output, buftmp, nb_samples);
218 } else {
219 memcpy(output, buftmp, nb_samples * sizeof(short));
220 }
221 av_free(buf1);
222 return nb_samples;
223 }
224
225 ReSampleContext *audio_resample_init(int output_channels, int input_channels,
226 int output_rate, int input_rate)
227 {
228 ReSampleContext *s;
229 int i;
230
231 if (output_channels > 2 || input_channels > 2)
232 return NULL;
233
234 s = av_mallocz(sizeof(ReSampleContext));
235 if (!s)
236 return NULL;
237
238 s->ratio = (float)output_rate / (float)input_rate;
239
240 s->input_channels = input_channels;
241 s->output_channels = output_channels;
242
243 s->filter_channels = s->input_channels;
244 if (s->output_channels < s->filter_channels)
245 s->filter_channels = s->output_channels;
246
247 for(i=0;i<s->filter_channels;i++) {
248 init_mono_resample(&s->channel_ctx[i], s->ratio);
249 }
250 return s;
251 }
252
253 /* resample audio. 'nb_samples' is the number of input samples */
254 /* XXX: optimize it ! */
255 /* XXX: do it with polyphase filters, since the quality here is
256 HORRIBLE. Return the number of samples available in output */
257 int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
258 {
259 int i, nb_samples1;
260 short *bufin[2];
261 short *bufout[2];
262 short *buftmp2[2], *buftmp3[2];
263 int lenout;
264
265 if (s->input_channels == s->output_channels && s->ratio == 1.0) {
266 /* nothing to do */
267 memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
268 return nb_samples;
269 }
270
271 /* XXX: move those malloc to resample init code */
272 bufin[0]= (short*) av_malloc( nb_samples * sizeof(short) );
273 bufin[1]= (short*) av_malloc( nb_samples * sizeof(short) );
274
275 /* make some zoom to avoid round pb */
276 lenout= (int)(nb_samples * s->ratio) + 16;
277 bufout[0]= (short*) av_malloc( lenout * sizeof(short) );
278 bufout[1]= (short*) av_malloc( lenout * sizeof(short) );
279
280 if (s->input_channels == 2 &&
281 s->output_channels == 1) {
282 buftmp2[0] = bufin[0];
283 buftmp3[0] = output;
284 stereo_to_mono(buftmp2[0], input, nb_samples);
285 } else if (s->output_channels == 2 && s->input_channels == 1) {
286 buftmp2[0] = input;
287 buftmp3[0] = bufout[0];
288 } else if (s->output_channels == 2) {
289 buftmp2[0] = bufin[0];
290 buftmp2[1] = bufin[1];
291 buftmp3[0] = bufout[0];
292 buftmp3[1] = bufout[1];
293 stereo_split(buftmp2[0], buftmp2[1], input, nb_samples);
294 } else {
295 buftmp2[0] = input;
296 buftmp3[0] = output;
297 }
298
299 /* resample each channel */
300 nb_samples1 = 0; /* avoid warning */
301 for(i=0;i<s->filter_channels;i++) {
302 nb_samples1 = mono_resample(&s->channel_ctx[i], buftmp3[i], buftmp2[i], nb_samples);
303 }
304
305 if (s->output_channels == 2 && s->input_channels == 1) {
306 mono_to_stereo(output, buftmp3[0], nb_samples1);
307 } else if (s->output_channels == 2) {
308 stereo_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
309 }
310
311 av_free(bufin[0]);
312 av_free(bufin[1]);
313
314 av_free(bufout[0]);
315 av_free(bufout[1]);
316 return nb_samples1;
317 }
318
319 void audio_resample_close(ReSampleContext *s)
320 {
321 av_free(s);
322 }