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