~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/resample.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-25 15:47:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080225154712-qvr11ekcea4c9ry8
Tags: 1.1.6-0.1ubuntu1
* Merge from debian-multimedia (LP: #120003), Ubuntu Changes:
 - For ffmpeg-related build-deps, remove cvs from package names.
 - Standards-Version 3.7.3
 - Maintainer Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * Sample rate convertion for both audio and video
3
3
 * Copyright (c) 2000 Fabrice Bellard.
4
4
 *
5
 
 * This library is free software; you can redistribute it and/or
 
5
 * This file is part of FFmpeg.
 
6
 *
 
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.
9
11
 *
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.
14
16
 *
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
18
20
 */
19
21
 
20
22
/**
24
26
 
25
27
#include "avcodec.h"
26
28
 
27
 
typedef struct {
28
 
    /* fractional resampling */
29
 
    uint32_t incr; /* fractional increment */
30
 
    uint32_t frac;
31
 
    int last_sample;
32
 
    /* integer down sample */
33
 
    int iratio;  /* integer divison ratio */
34
 
    int icount, isum;
35
 
    int inv;
36
 
} ReSampleChannelContext;
 
29
struct AVResampleContext;
37
30
 
38
31
struct ReSampleContext {
39
 
    ReSampleChannelContext channel_ctx[2];
 
32
    struct AVResampleContext *resample_context;
 
33
    short *temp[2];
 
34
    int temp_len;
40
35
    float ratio;
41
36
    /* channel convert */
42
37
    int input_channels, output_channels, filter_channels;
43
38
};
44
39
 
45
 
 
46
 
#define FRAC_BITS 16
47
 
#define FRAC (1 << FRAC_BITS)
48
 
 
49
 
static void init_mono_resample(ReSampleChannelContext *s, float ratio)
50
 
{
51
 
    ratio = 1.0 / ratio;
52
 
    s->iratio = (int)floorf(ratio);
53
 
    if (s->iratio == 0)
54
 
        s->iratio = 1;
55
 
    s->incr = (int)((ratio / s->iratio) * FRAC);
56
 
    s->frac = FRAC;
57
 
    s->last_sample = 0;
58
 
    s->icount = s->iratio;
59
 
    s->isum = 0;
60
 
    s->inv = (FRAC / s->iratio);
61
 
}
62
 
 
63
 
/* fractional audio resampling */
64
 
static int fractional_resample(ReSampleChannelContext *s, short *output, short *input, int nb_samples)
65
 
{
66
 
    unsigned int frac, incr;
67
 
    int l0, l1;
68
 
    short *q, *p, *pend;
69
 
 
70
 
    l0 = s->last_sample;
71
 
    incr = s->incr;
72
 
    frac = s->frac;
73
 
 
74
 
    p = input;
75
 
    pend = input + nb_samples;
76
 
    q = output;
77
 
 
78
 
    l1 = *p++;
79
 
    for(;;) {
80
 
        /* interpolate */
81
 
        *q++ = (l0 * (FRAC - frac) + l1 * frac) >> FRAC_BITS;
82
 
        frac = frac + s->incr;
83
 
        while (frac >= FRAC) {
84
 
            frac -= FRAC;
85
 
            if (p >= pend)
86
 
                goto the_end;
87
 
            l0 = l1;
88
 
            l1 = *p++;
89
 
        }
90
 
    }
91
 
 the_end:
92
 
    s->last_sample = l1;
93
 
    s->frac = frac;
94
 
    return q - output;
95
 
}
96
 
 
97
 
static int integer_downsample(ReSampleChannelContext *s, short *output, short *input, int nb_samples)
98
 
{
99
 
    short *q, *p, *pend;
100
 
    int c, sum;
101
 
 
102
 
    p = input;
103
 
    pend = input + nb_samples;
104
 
    q = output;
105
 
 
106
 
    c = s->icount;
107
 
    sum = s->isum;
108
 
 
109
 
    for(;;) {
110
 
        sum += *p++;
111
 
        if (--c == 0) {
112
 
            *q++ = (sum * s->inv) >> FRAC_BITS;
113
 
            c = s->iratio;
114
 
            sum = 0;
115
 
        }
116
 
        if (p >= pend)
117
 
            break;
118
 
    }
119
 
    s->isum = sum;
120
 
    s->icount = c;
121
 
    return q - output;
122
 
}
123
 
 
124
40
/* n1: number of samples */
125
41
static void stereo_to_mono(short *output, short *input, int n1)
126
42
{
210
126
    }
211
127
}
212
128
 
213
 
static int mono_resample(ReSampleChannelContext *s, short *output, short *input, int nb_samples)
214
 
{
215
 
    short *buf1;
216
 
    short *buftmp;
217
 
 
218
 
    buf1= (short*)av_malloc( nb_samples * sizeof(short) );
219
 
 
220
 
    /* first downsample by an integer factor with averaging filter */
221
 
    if (s->iratio > 1) {
222
 
        buftmp = buf1;
223
 
        nb_samples = integer_downsample(s, buftmp, input, nb_samples);
224
 
    } else {
225
 
        buftmp = input;
226
 
    }
227
 
 
228
 
    /* then do a fractional resampling with linear interpolation */
229
 
    if (s->incr != FRAC) {
230
 
        nb_samples = fractional_resample(s, output, buftmp, nb_samples);
231
 
    } else {
232
 
        memcpy(output, buftmp, nb_samples * sizeof(short));
233
 
    }
234
 
    av_free(buf1);
235
 
    return nb_samples;
236
 
}
237
 
 
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)
240
131
{
241
132
    ReSampleContext *s;
242
 
    int i;
243
 
    
 
133
 
244
134
    if ( input_channels > 2)
245
135
      {
246
 
        printf("Resampling with input channels greater than 2 unsupported.");
247
 
        return NULL;
 
136
        av_log(NULL, AV_LOG_ERROR, "Resampling with input channels greater than 2 unsupported.");
 
137
        return NULL;
248
138
      }
249
139
 
250
140
    s = av_mallocz(sizeof(ReSampleContext));
251
141
    if (!s)
252
142
      {
253
 
        printf("Can't allocate memory for resample context.");
254
 
        return NULL;
 
143
        av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for resample context.");
 
144
        return NULL;
255
145
      }
256
146
 
257
147
    s->ratio = (float)output_rate / (float)input_rate;
258
 
    
 
148
 
259
149
    s->input_channels = input_channels;
260
150
    s->output_channels = output_channels;
261
 
    
 
151
 
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;
273
163
 
274
 
    for(i=0;i<s->filter_channels;i++) {
275
 
        init_mono_resample(&s->channel_ctx[i], s->ratio);
276
 
    }
 
164
    s->resample_context= av_resample_init(output_rate, input_rate, 16, 10, 0, 1.0);
 
165
 
277
166
    return s;
278
167
}
279
168
 
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)
285
172
{
286
173
    int i, nb_samples1;
289
176
    short *buftmp2[2], *buftmp3[2];
290
177
    int lenout;
291
178
 
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;
296
183
    }
297
184
 
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) );
301
 
    
 
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;
 
190
    }
 
191
 
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) );
306
196
 
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) {
313
 
        buftmp2[0] = input;
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);
321
208
    } else {
322
 
        buftmp2[0] = input;
323
209
        buftmp3[0] = output;
 
210
        memcpy(buftmp2[0], input, nb_samples*sizeof(short));
324
211
    }
325
212
 
 
213
    nb_samples += s->temp_len;
 
214
 
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);
 
218
        int consumed;
 
219
        int is_last= i+1 == s->filter_channels;
 
220
 
 
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));
330
225
    }
331
226
 
332
227
    if (s->output_channels == 2 && s->input_channels == 1) {
337
232
        ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
338
233
    }
339
234
 
340
 
    av_free(bufin[0]);
341
 
    av_free(bufin[1]);
 
235
    for(i=0; i<s->filter_channels; i++)
 
236
        av_free(bufin[i]);
342
237
 
343
238
    av_free(bufout[0]);
344
239
    av_free(bufout[1]);
347
242
 
348
243
void audio_resample_close(ReSampleContext *s)
349
244
{
 
245
    av_resample_close(s->resample_context);
 
246
    av_freep(&s->temp[0]);
 
247
    av_freep(&s->temp[1]);
350
248
    av_free(s);
351
249
}