Statistics
| Branch: | Revision:

ffmpeg / libavcodec / resample.c @ 9b4f1cdb

History | View | Annotate | Download (11.2 KB)

1 de6d9b64 Fabrice Bellard
/*
2 f1ea5c2a Diego Biurrun
 * samplerate conversion for both audio and video
3 406792e7 Diego Biurrun
 * Copyright (c) 2000 Fabrice Bellard
4 de6d9b64 Fabrice Bellard
 *
5 2912e87a Mans Rullgard
 * This file is part of Libav.
6 b78e7197 Diego Biurrun
 *
7 2912e87a Mans Rullgard
 * Libav is free software; you can redistribute it and/or
8 ff4ec49e Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 de6d9b64 Fabrice Bellard
 *
12 2912e87a Mans Rullgard
 * Libav is distributed in the hope that it will be useful,
13 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ff4ec49e Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 de6d9b64 Fabrice Bellard
 *
17 ff4ec49e Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18 2912e87a Mans Rullgard
 * License along with Libav; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 de6d9b64 Fabrice Bellard
 */
21 983e3246 Michael Niedermayer
22
/**
23 ba87f080 Diego Biurrun
 * @file
24 f1ea5c2a Diego Biurrun
 * samplerate conversion for both audio and video
25 983e3246 Michael Niedermayer
 */
26
27 de6d9b64 Fabrice Bellard
#include "avcodec.h"
28 d1e3c6fd Baptiste Coudurier
#include "audioconvert.h"
29 6ed04040 Michael Niedermayer
#include "libavutil/opt.h"
30 737eb597 Reinhard Tartler
#include "libavutil/samplefmt.h"
31 69db4e10 Slavik Gnatenko
32 aaaf1635 Michael Niedermayer
struct AVResampleContext;
33 de6d9b64 Fabrice Bellard
34 d1e3c6fd Baptiste Coudurier
static const char *context_to_name(void *ptr)
35
{
36
    return "audioresample";
37
}
38
39
static const AVOption options[] = {{NULL}};
40 2308b6c1 Michael Niedermayer
static const AVClass audioresample_context_class = { "ReSampleContext", context_to_name, options, LIBAVUTIL_VERSION_INT };
41 d1e3c6fd Baptiste Coudurier
42 de6d9b64 Fabrice Bellard
struct ReSampleContext {
43 aaaf1635 Michael Niedermayer
    struct AVResampleContext *resample_context;
44
    short *temp[2];
45
    int temp_len;
46 de6d9b64 Fabrice Bellard
    float ratio;
47
    /* channel convert */
48
    int input_channels, output_channels, filter_channels;
49 d1e3c6fd Baptiste Coudurier
    AVAudioConvert *convert_ctx[2];
50 5d6e4c16 Stefano Sabatini
    enum AVSampleFormat sample_fmt[2]; ///< input and output sample format
51 d1e3c6fd Baptiste Coudurier
    unsigned sample_size[2];         ///< size of one sample in sample_fmt
52
    short *buffer[2];                ///< buffers used for conversion to S16
53
    unsigned buffer_size[2];         ///< sizes of allocated buffers
54 de6d9b64 Fabrice Bellard
};
55
56
/* n1: number of samples */
57
static void stereo_to_mono(short *output, short *input, int n1)
58
{
59
    short *p, *q;
60
    int n = n1;
61
62
    p = input;
63
    q = output;
64
    while (n >= 4) {
65
        q[0] = (p[0] + p[1]) >> 1;
66
        q[1] = (p[2] + p[3]) >> 1;
67
        q[2] = (p[4] + p[5]) >> 1;
68
        q[3] = (p[6] + p[7]) >> 1;
69
        q += 4;
70
        p += 8;
71
        n -= 4;
72
    }
73
    while (n > 0) {
74
        q[0] = (p[0] + p[1]) >> 1;
75
        q++;
76
        p += 2;
77
        n--;
78
    }
79
}
80
81
/* n1: number of samples */
82
static void mono_to_stereo(short *output, short *input, int n1)
83
{
84
    short *p, *q;
85
    int n = n1;
86
    int v;
87
88
    p = input;
89
    q = output;
90
    while (n >= 4) {
91
        v = p[0]; q[0] = v; q[1] = v;
92
        v = p[1]; q[2] = v; q[3] = v;
93
        v = p[2]; q[4] = v; q[5] = v;
94
        v = p[3]; q[6] = v; q[7] = v;
95
        q += 8;
96
        p += 4;
97
        n -= 4;
98
    }
99
    while (n > 0) {
100
        v = p[0]; q[0] = v; q[1] = v;
101
        q += 2;
102
        p += 1;
103
        n--;
104
    }
105
}
106
107
/* XXX: should use more abstract 'N' channels system */
108
static void stereo_split(short *output1, short *output2, short *input, int n)
109
{
110
    int i;
111
112
    for(i=0;i<n;i++) {
113
        *output1++ = *input++;
114
        *output2++ = *input++;
115
    }
116
}
117
118
static void stereo_mux(short *output, short *input1, short *input2, int n)
119
{
120
    int i;
121
122
    for(i=0;i<n;i++) {
123
        *output++ = *input1++;
124
        *output++ = *input2++;
125
    }
126
}
127
128 743739d2 Michael Niedermayer
static void ac3_5p1_mux(short *output, short *input1, short *input2, int n)
129
{
130
    int i;
131
    short l,r;
132
133
    for(i=0;i<n;i++) {
134
      l=*input1++;
135
      r=*input2++;
136
      *output++ = l;           /* left */
137
      *output++ = (l/2)+(r/2); /* center */
138
      *output++ = r;           /* right */
139
      *output++ = 0;           /* left surround */
140
      *output++ = 0;           /* right surroud */
141
      *output++ = 0;           /* low freq */
142
    }
143
}
144
145 d1e3c6fd Baptiste Coudurier
ReSampleContext *av_audio_resample_init(int output_channels, int input_channels,
146
                                        int output_rate, int input_rate,
147 5d6e4c16 Stefano Sabatini
                                        enum AVSampleFormat sample_fmt_out,
148
                                        enum AVSampleFormat sample_fmt_in,
149 d1e3c6fd Baptiste Coudurier
                                        int filter_length, int log2_phase_count,
150
                                        int linear, double cutoff)
151 de6d9b64 Fabrice Bellard
{
152
    ReSampleContext *s;
153 115329f1 Diego Biurrun
154 743739d2 Michael Niedermayer
    if ( input_channels > 2)
155
      {
156 30dc5541 Andreas Öman
        av_log(NULL, AV_LOG_ERROR, "Resampling with input channels greater than 2 unsupported.\n");
157 bb270c08 Diego Biurrun
        return NULL;
158 743739d2 Michael Niedermayer
      }
159 de6d9b64 Fabrice Bellard
160
    s = av_mallocz(sizeof(ReSampleContext));
161
    if (!s)
162 743739d2 Michael Niedermayer
      {
163 30dc5541 Andreas Öman
        av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for resample context.\n");
164 bb270c08 Diego Biurrun
        return NULL;
165 743739d2 Michael Niedermayer
      }
166 de6d9b64 Fabrice Bellard
167
    s->ratio = (float)output_rate / (float)input_rate;
168 115329f1 Diego Biurrun
169 de6d9b64 Fabrice Bellard
    s->input_channels = input_channels;
170
    s->output_channels = output_channels;
171 115329f1 Diego Biurrun
172 de6d9b64 Fabrice Bellard
    s->filter_channels = s->input_channels;
173
    if (s->output_channels < s->filter_channels)
174
        s->filter_channels = s->output_channels;
175
176 d1e3c6fd Baptiste Coudurier
    s->sample_fmt [0] = sample_fmt_in;
177
    s->sample_fmt [1] = sample_fmt_out;
178 ba7d6e79 Stefano Sabatini
    s->sample_size[0] = av_get_bits_per_sample_fmt(s->sample_fmt[0])>>3;
179
    s->sample_size[1] = av_get_bits_per_sample_fmt(s->sample_fmt[1])>>3;
180 d1e3c6fd Baptiste Coudurier
181 5d6e4c16 Stefano Sabatini
    if (s->sample_fmt[0] != AV_SAMPLE_FMT_S16) {
182
        if (!(s->convert_ctx[0] = av_audio_convert_alloc(AV_SAMPLE_FMT_S16, 1,
183 d1e3c6fd Baptiste Coudurier
                                                         s->sample_fmt[0], 1, NULL, 0))) {
184
            av_log(s, AV_LOG_ERROR,
185
                   "Cannot convert %s sample format to s16 sample format\n",
186 ba7d6e79 Stefano Sabatini
                   av_get_sample_fmt_name(s->sample_fmt[0]));
187 d1e3c6fd Baptiste Coudurier
            av_free(s);
188
            return NULL;
189
        }
190
    }
191
192 5d6e4c16 Stefano Sabatini
    if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
193 d1e3c6fd Baptiste Coudurier
        if (!(s->convert_ctx[1] = av_audio_convert_alloc(s->sample_fmt[1], 1,
194 5d6e4c16 Stefano Sabatini
                                                         AV_SAMPLE_FMT_S16, 1, NULL, 0))) {
195 d1e3c6fd Baptiste Coudurier
            av_log(s, AV_LOG_ERROR,
196
                   "Cannot convert s16 sample format to %s sample format\n",
197 ba7d6e79 Stefano Sabatini
                   av_get_sample_fmt_name(s->sample_fmt[1]));
198 d1e3c6fd Baptiste Coudurier
            av_audio_convert_free(s->convert_ctx[0]);
199
            av_free(s);
200
            return NULL;
201
        }
202
    }
203
204 743739d2 Michael Niedermayer
/*
205 14b70628 Justin Ruggles
 * AC-3 output is the only case where filter_channels could be greater than 2.
206 743739d2 Michael Niedermayer
 * input channels can't be greater than 2, so resample the 2 channels and then
207
 * expand to 6 channels after the resampling.
208
 */
209
    if(s->filter_channels>2)
210
      s->filter_channels = 2;
211
212 8ec04d34 Michael Niedermayer
#define TAPS 16
213 d1e3c6fd Baptiste Coudurier
    s->resample_context= av_resample_init(output_rate, input_rate,
214
                         filter_length, log2_phase_count, linear, cutoff);
215
216 844d17fb Baptiste Coudurier
    *(const AVClass**)s->resample_context = &audioresample_context_class;
217 115329f1 Diego Biurrun
218 de6d9b64 Fabrice Bellard
    return s;
219
}
220
221
/* resample audio. 'nb_samples' is the number of input samples */
222
/* XXX: optimize it ! */
223
int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
224
{
225
    int i, nb_samples1;
226 1a565432 Fabrice Bellard
    short *bufin[2];
227
    short *bufout[2];
228 de6d9b64 Fabrice Bellard
    short *buftmp2[2], *buftmp3[2];
229 d1e3c6fd Baptiste Coudurier
    short *output_bak = NULL;
230 1a565432 Fabrice Bellard
    int lenout;
231 de6d9b64 Fabrice Bellard
232 b9d2085b Michael Niedermayer
    if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) {
233 de6d9b64 Fabrice Bellard
        /* nothing to do */
234
        memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
235
        return nb_samples;
236
    }
237
238 5d6e4c16 Stefano Sabatini
    if (s->sample_fmt[0] != AV_SAMPLE_FMT_S16) {
239 d1e3c6fd Baptiste Coudurier
        int istride[1] = { s->sample_size[0] };
240
        int ostride[1] = { 2 };
241
        const void *ibuf[1] = { input };
242
        void       *obuf[1];
243 5f5e6af1 Peter Ross
        unsigned input_size = nb_samples*s->input_channels*2;
244 d1e3c6fd Baptiste Coudurier
245
        if (!s->buffer_size[0] || s->buffer_size[0] < input_size) {
246
            av_free(s->buffer[0]);
247
            s->buffer_size[0] = input_size;
248
            s->buffer[0] = av_malloc(s->buffer_size[0]);
249
            if (!s->buffer[0]) {
250 89bc05d1 Baptiste Coudurier
                av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
251 d1e3c6fd Baptiste Coudurier
                return 0;
252
            }
253
        }
254
255
        obuf[0] = s->buffer[0];
256
257
        if (av_audio_convert(s->convert_ctx[0], obuf, ostride,
258
                             ibuf, istride, nb_samples*s->input_channels) < 0) {
259 89bc05d1 Baptiste Coudurier
            av_log(s->resample_context, AV_LOG_ERROR, "Audio sample format conversion failed\n");
260 d1e3c6fd Baptiste Coudurier
            return 0;
261
        }
262
263
        input  = s->buffer[0];
264
    }
265
266
    lenout= 4*nb_samples * s->ratio + 16;
267
268 5d6e4c16 Stefano Sabatini
    if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
269 d1e3c6fd Baptiste Coudurier
        output_bak = output;
270
271
        if (!s->buffer_size[1] || s->buffer_size[1] < lenout) {
272
            av_free(s->buffer[1]);
273
            s->buffer_size[1] = lenout;
274
            s->buffer[1] = av_malloc(s->buffer_size[1]);
275
            if (!s->buffer[1]) {
276 89bc05d1 Baptiste Coudurier
                av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
277 d1e3c6fd Baptiste Coudurier
                return 0;
278
            }
279
        }
280
281
        output = s->buffer[1];
282
    }
283
284 1a565432 Fabrice Bellard
    /* XXX: move those malloc to resample init code */
285 aaaf1635 Michael Niedermayer
    for(i=0; i<s->filter_channels; i++){
286 90901860 Michael Niedermayer
        bufin[i]= av_malloc( (nb_samples + s->temp_len) * sizeof(short) );
287 aaaf1635 Michael Niedermayer
        memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short));
288
        buftmp2[i] = bufin[i] + s->temp_len;
289
    }
290 115329f1 Diego Biurrun
291 1a565432 Fabrice Bellard
    /* make some zoom to avoid round pb */
292 90901860 Michael Niedermayer
    bufout[0]= av_malloc( lenout * sizeof(short) );
293
    bufout[1]= av_malloc( lenout * sizeof(short) );
294 1a565432 Fabrice Bellard
295 de6d9b64 Fabrice Bellard
    if (s->input_channels == 2 &&
296
        s->output_channels == 1) {
297
        buftmp3[0] = output;
298
        stereo_to_mono(buftmp2[0], input, nb_samples);
299 743739d2 Michael Niedermayer
    } else if (s->output_channels >= 2 && s->input_channels == 1) {
300 de6d9b64 Fabrice Bellard
        buftmp3[0] = bufout[0];
301 aaaf1635 Michael Niedermayer
        memcpy(buftmp2[0], input, nb_samples*sizeof(short));
302 743739d2 Michael Niedermayer
    } else if (s->output_channels >= 2) {
303 de6d9b64 Fabrice Bellard
        buftmp3[0] = bufout[0];
304
        buftmp3[1] = bufout[1];
305
        stereo_split(buftmp2[0], buftmp2[1], input, nb_samples);
306
    } else {
307
        buftmp3[0] = output;
308 aaaf1635 Michael Niedermayer
        memcpy(buftmp2[0], input, nb_samples*sizeof(short));
309 de6d9b64 Fabrice Bellard
    }
310
311 aaaf1635 Michael Niedermayer
    nb_samples += s->temp_len;
312
313 de6d9b64 Fabrice Bellard
    /* resample each channel */
314
    nb_samples1 = 0; /* avoid warning */
315
    for(i=0;i<s->filter_channels;i++) {
316 aaaf1635 Michael Niedermayer
        int consumed;
317
        int is_last= i+1 == s->filter_channels;
318
319
        nb_samples1 = av_resample(s->resample_context, buftmp3[i], bufin[i], &consumed, nb_samples, lenout, is_last);
320
        s->temp_len= nb_samples - consumed;
321
        s->temp[i]= av_realloc(s->temp[i], s->temp_len*sizeof(short));
322
        memcpy(s->temp[i], bufin[i] + consumed, s->temp_len*sizeof(short));
323 de6d9b64 Fabrice Bellard
    }
324
325
    if (s->output_channels == 2 && s->input_channels == 1) {
326
        mono_to_stereo(output, buftmp3[0], nb_samples1);
327
    } else if (s->output_channels == 2) {
328
        stereo_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
329 743739d2 Michael Niedermayer
    } else if (s->output_channels == 6) {
330
        ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
331 de6d9b64 Fabrice Bellard
    }
332
333 5d6e4c16 Stefano Sabatini
    if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
334 d1e3c6fd Baptiste Coudurier
        int istride[1] = { 2 };
335
        int ostride[1] = { s->sample_size[1] };
336
        const void *ibuf[1] = { output };
337
        void       *obuf[1] = { output_bak };
338
339
        if (av_audio_convert(s->convert_ctx[1], obuf, ostride,
340
                             ibuf, istride, nb_samples1*s->output_channels) < 0) {
341 89bc05d1 Baptiste Coudurier
            av_log(s->resample_context, AV_LOG_ERROR, "Audio sample format convertion failed\n");
342 d1e3c6fd Baptiste Coudurier
            return 0;
343
        }
344
    }
345
346 dca97cbe Michael Niedermayer
    for(i=0; i<s->filter_channels; i++)
347
        av_free(bufin[i]);
348 1a565432 Fabrice Bellard
349 6000abfa Fabrice Bellard
    av_free(bufout[0]);
350
    av_free(bufout[1]);
351 de6d9b64 Fabrice Bellard
    return nb_samples1;
352
}
353
354
void audio_resample_close(ReSampleContext *s)
355
{
356 aaaf1635 Michael Niedermayer
    av_resample_close(s->resample_context);
357
    av_freep(&s->temp[0]);
358
    av_freep(&s->temp[1]);
359 d1e3c6fd Baptiste Coudurier
    av_freep(&s->buffer[0]);
360
    av_freep(&s->buffer[1]);
361
    av_audio_convert_free(s->convert_ctx[0]);
362
    av_audio_convert_free(s->convert_ctx[1]);
363 6000abfa Fabrice Bellard
    av_free(s);
364 de6d9b64 Fabrice Bellard
}