2
2
* Sample rate convertion for both audio and video
3
3
* Copyright (c) 2000 Fabrice Bellard.
5
* This library is free software; you can redistribute it and/or
5
* This file is part of FFmpeg.
7
* FFmpeg is free software; you can redistribute it and/or
6
8
* modify it under the terms of the GNU Lesser General Public
7
9
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
12
* FFmpeg is distributed in the hope that it will be useful,
11
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
15
* Lesser General Public License for more details.
15
17
* 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
* License along with FFmpeg; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25
27
#include "avcodec.h"
28
/* fractional resampling */
29
uint32_t incr; /* fractional increment */
32
/* integer down sample */
33
int iratio; /* integer divison ratio */
36
} ReSampleChannelContext;
29
struct AVResampleContext;
38
31
struct ReSampleContext {
39
ReSampleChannelContext channel_ctx[2];
32
struct AVResampleContext *resample_context;
41
36
/* channel convert */
42
37
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
40
/* n1: number of samples */
125
41
static void stereo_to_mono(short *output, short *input, int n1)
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,
129
ReSampleContext *audio_resample_init(int output_channels, int input_channels,
239
130
int output_rate, int input_rate)
241
132
ReSampleContext *s;
244
134
if ( input_channels > 2)
246
printf("Resampling with input channels greater than 2 unsupported.");
136
av_log(NULL, AV_LOG_ERROR, "Resampling with input channels greater than 2 unsupported.");
250
140
s = av_mallocz(sizeof(ReSampleContext));
253
printf("Can't allocate memory for resample context.");
143
av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for resample context.");
257
147
s->ratio = (float)output_rate / (float)input_rate;
259
149
s->input_channels = input_channels;
260
150
s->output_channels = output_channels;
262
152
s->filter_channels = s->input_channels;
263
153
if (s->output_channels < s->filter_channels)
264
154
s->filter_channels = s->output_channels;
271
161
if(s->filter_channels>2)
272
162
s->filter_channels = 2;
274
for(i=0;i<s->filter_channels;i++) {
275
init_mono_resample(&s->channel_ctx[i], s->ratio);
164
s->resample_context= av_resample_init(output_rate, input_rate, 16, 10, 0, 1.0);
280
169
/* resample audio. 'nb_samples' is the number of input samples */
281
170
/* 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
171
int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
286
173
int i, nb_samples1;
289
176
short *buftmp2[2], *buftmp3[2];
292
if (s->input_channels == s->output_channels && s->ratio == 1.0) {
179
if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) {
293
180
/* nothing to do */
294
181
memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
295
182
return nb_samples;
298
185
/* 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) );
186
for(i=0; i<s->filter_channels; i++){
187
bufin[i]= (short*) av_malloc( (nb_samples + s->temp_len) * sizeof(short) );
188
memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short));
189
buftmp2[i] = bufin[i] + s->temp_len;
302
192
/* make some zoom to avoid round pb */
303
193
lenout= (int)(nb_samples * s->ratio) + 16;
304
194
bufout[0]= (short*) av_malloc( lenout * sizeof(short) );
307
197
if (s->input_channels == 2 &&
308
198
s->output_channels == 1) {
309
buftmp2[0] = bufin[0];
310
199
buftmp3[0] = output;
311
200
stereo_to_mono(buftmp2[0], input, nb_samples);
312
201
} else if (s->output_channels >= 2 && s->input_channels == 1) {
314
202
buftmp3[0] = bufout[0];
203
memcpy(buftmp2[0], input, nb_samples*sizeof(short));
315
204
} else if (s->output_channels >= 2) {
316
buftmp2[0] = bufin[0];
317
buftmp2[1] = bufin[1];
318
205
buftmp3[0] = bufout[0];
319
206
buftmp3[1] = bufout[1];
320
207
stereo_split(buftmp2[0], buftmp2[1], input, nb_samples);
323
209
buftmp3[0] = output;
210
memcpy(buftmp2[0], input, nb_samples*sizeof(short));
213
nb_samples += s->temp_len;
326
215
/* resample each channel */
327
216
nb_samples1 = 0; /* avoid warning */
328
217
for(i=0;i<s->filter_channels;i++) {
329
nb_samples1 = mono_resample(&s->channel_ctx[i], buftmp3[i], buftmp2[i], nb_samples);
219
int is_last= i+1 == s->filter_channels;
221
nb_samples1 = av_resample(s->resample_context, buftmp3[i], bufin[i], &consumed, nb_samples, lenout, is_last);
222
s->temp_len= nb_samples - consumed;
223
s->temp[i]= av_realloc(s->temp[i], s->temp_len*sizeof(short));
224
memcpy(s->temp[i], bufin[i] + consumed, s->temp_len*sizeof(short));
332
227
if (s->output_channels == 2 && s->input_channels == 1) {