Statistics
| Branch: | Revision:

ffmpeg / libavcodec / resample.c @ 3e00abab

History | View | Annotate | Download (11.5 KB)

1
/*
2
 * samplerate conversion for both audio and video
3
 * Copyright (c) 2000 Fabrice Bellard
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * Libav is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with Libav; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
/**
23
 * @file
24
 * samplerate conversion for both audio and video
25
 */
26

    
27
#include "avcodec.h"
28
#include "audioconvert.h"
29
#include "libavutil/opt.h"
30
#include "libavutil/samplefmt.h"
31

    
32
#define MAX_CHANNELS 8
33

    
34
struct AVResampleContext;
35

    
36
static const char *context_to_name(void *ptr)
37
{
38
    return "audioresample";
39
}
40

    
41
static const AVOption options[] = {{NULL}};
42
static const AVClass audioresample_context_class = { "ReSampleContext", context_to_name, options, LIBAVUTIL_VERSION_INT };
43

    
44
struct ReSampleContext {
45
    struct AVResampleContext *resample_context;
46
    short *temp[MAX_CHANNELS];
47
    int temp_len;
48
    float ratio;
49
    /* channel convert */
50
    int input_channels, output_channels, filter_channels;
51
    AVAudioConvert *convert_ctx[2];
52
    enum AVSampleFormat sample_fmt[2]; ///< input and output sample format
53
    unsigned sample_size[2];         ///< size of one sample in sample_fmt
54
    short *buffer[2];                ///< buffers used for conversion to S16
55
    unsigned buffer_size[2];         ///< sizes of allocated buffers
56
};
57

    
58
/* n1: number of samples */
59
static void stereo_to_mono(short *output, short *input, int n1)
60
{
61
    short *p, *q;
62
    int n = n1;
63

    
64
    p = input;
65
    q = output;
66
    while (n >= 4) {
67
        q[0] = (p[0] + p[1]) >> 1;
68
        q[1] = (p[2] + p[3]) >> 1;
69
        q[2] = (p[4] + p[5]) >> 1;
70
        q[3] = (p[6] + p[7]) >> 1;
71
        q += 4;
72
        p += 8;
73
        n -= 4;
74
    }
75
    while (n > 0) {
76
        q[0] = (p[0] + p[1]) >> 1;
77
        q++;
78
        p += 2;
79
        n--;
80
    }
81
}
82

    
83
/* n1: number of samples */
84
static void mono_to_stereo(short *output, short *input, int n1)
85
{
86
    short *p, *q;
87
    int n = n1;
88
    int v;
89

    
90
    p = input;
91
    q = output;
92
    while (n >= 4) {
93
        v = p[0]; q[0] = v; q[1] = v;
94
        v = p[1]; q[2] = v; q[3] = v;
95
        v = p[2]; q[4] = v; q[5] = v;
96
        v = p[3]; q[6] = v; q[7] = v;
97
        q += 8;
98
        p += 4;
99
        n -= 4;
100
    }
101
    while (n > 0) {
102
        v = p[0]; q[0] = v; q[1] = v;
103
        q += 2;
104
        p += 1;
105
        n--;
106
    }
107
}
108

    
109
static void deinterleave(short **output, short *input, int channels, int samples)
110
{
111
    int i, j;
112

    
113
    for (i = 0; i < samples; i++) {
114
        for (j = 0; j < channels; j++) {
115
            *output[j]++ = *input++;
116
        }
117
    }
118
}
119

    
120
static void interleave(short *output, short **input, int channels, int samples)
121
{
122
    int i, j;
123

    
124
    for (i = 0; i < samples; i++) {
125
        for (j = 0; j < channels; j++) {
126
            *output++ = *input[j]++;
127
        }
128
    }
129
}
130

    
131
static void ac3_5p1_mux(short *output, short *input1, short *input2, int n)
132
{
133
    int i;
134
    short l,r;
135

    
136
    for(i=0;i<n;i++) {
137
      l=*input1++;
138
      r=*input2++;
139
      *output++ = l;           /* left */
140
      *output++ = (l/2)+(r/2); /* center */
141
      *output++ = r;           /* right */
142
      *output++ = 0;           /* left surround */
143
      *output++ = 0;           /* right surroud */
144
      *output++ = 0;           /* low freq */
145
    }
146
}
147

    
148
ReSampleContext *av_audio_resample_init(int output_channels, int input_channels,
149
                                        int output_rate, int input_rate,
150
                                        enum AVSampleFormat sample_fmt_out,
151
                                        enum AVSampleFormat sample_fmt_in,
152
                                        int filter_length, int log2_phase_count,
153
                                        int linear, double cutoff)
154
{
155
    ReSampleContext *s;
156

    
157
    if (input_channels > MAX_CHANNELS)
158
      {
159
        av_log(NULL, AV_LOG_ERROR,
160
               "Resampling with input channels greater than %d is unsupported.\n",
161
               MAX_CHANNELS);
162
        return NULL;
163
      }
164
    if (  output_channels > 2 &&
165
        !(output_channels == 6 && input_channels == 2) &&
166
          output_channels != input_channels) {
167
        av_log(NULL, AV_LOG_ERROR,
168
               "Resampling output channel count must be 1 or 2 for mono input; 1, 2 or 6 for stereo input; or N for N channel input.\n");
169
        return NULL;
170
    }
171

    
172
    s = av_mallocz(sizeof(ReSampleContext));
173
    if (!s)
174
      {
175
        av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for resample context.\n");
176
        return NULL;
177
      }
178

    
179
    s->ratio = (float)output_rate / (float)input_rate;
180

    
181
    s->input_channels = input_channels;
182
    s->output_channels = output_channels;
183

    
184
    s->filter_channels = s->input_channels;
185
    if (s->output_channels < s->filter_channels)
186
        s->filter_channels = s->output_channels;
187

    
188
    s->sample_fmt [0] = sample_fmt_in;
189
    s->sample_fmt [1] = sample_fmt_out;
190
    s->sample_size[0] = av_get_bits_per_sample_fmt(s->sample_fmt[0])>>3;
191
    s->sample_size[1] = av_get_bits_per_sample_fmt(s->sample_fmt[1])>>3;
192

    
193
    if (s->sample_fmt[0] != AV_SAMPLE_FMT_S16) {
194
        if (!(s->convert_ctx[0] = av_audio_convert_alloc(AV_SAMPLE_FMT_S16, 1,
195
                                                         s->sample_fmt[0], 1, NULL, 0))) {
196
            av_log(s, AV_LOG_ERROR,
197
                   "Cannot convert %s sample format to s16 sample format\n",
198
                   av_get_sample_fmt_name(s->sample_fmt[0]));
199
            av_free(s);
200
            return NULL;
201
        }
202
    }
203

    
204
    if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
205
        if (!(s->convert_ctx[1] = av_audio_convert_alloc(s->sample_fmt[1], 1,
206
                                                         AV_SAMPLE_FMT_S16, 1, NULL, 0))) {
207
            av_log(s, AV_LOG_ERROR,
208
                   "Cannot convert s16 sample format to %s sample format\n",
209
                   av_get_sample_fmt_name(s->sample_fmt[1]));
210
            av_audio_convert_free(s->convert_ctx[0]);
211
            av_free(s);
212
            return NULL;
213
        }
214
    }
215

    
216
#define TAPS 16
217
    s->resample_context= av_resample_init(output_rate, input_rate,
218
                         filter_length, log2_phase_count, linear, cutoff);
219

    
220
    *(const AVClass**)s->resample_context = &audioresample_context_class;
221

    
222
    return s;
223
}
224

    
225
/* resample audio. 'nb_samples' is the number of input samples */
226
/* XXX: optimize it ! */
227
int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
228
{
229
    int i, nb_samples1;
230
    short *bufin[MAX_CHANNELS];
231
    short *bufout[MAX_CHANNELS];
232
    short *buftmp2[MAX_CHANNELS], *buftmp3[MAX_CHANNELS];
233
    short *output_bak = NULL;
234
    int lenout;
235

    
236
    if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) {
237
        /* nothing to do */
238
        memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
239
        return nb_samples;
240
    }
241

    
242
    if (s->sample_fmt[0] != AV_SAMPLE_FMT_S16) {
243
        int istride[1] = { s->sample_size[0] };
244
        int ostride[1] = { 2 };
245
        const void *ibuf[1] = { input };
246
        void       *obuf[1];
247
        unsigned input_size = nb_samples*s->input_channels*2;
248

    
249
        if (!s->buffer_size[0] || s->buffer_size[0] < input_size) {
250
            av_free(s->buffer[0]);
251
            s->buffer_size[0] = input_size;
252
            s->buffer[0] = av_malloc(s->buffer_size[0]);
253
            if (!s->buffer[0]) {
254
                av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
255
                return 0;
256
            }
257
        }
258

    
259
        obuf[0] = s->buffer[0];
260

    
261
        if (av_audio_convert(s->convert_ctx[0], obuf, ostride,
262
                             ibuf, istride, nb_samples*s->input_channels) < 0) {
263
            av_log(s->resample_context, AV_LOG_ERROR, "Audio sample format conversion failed\n");
264
            return 0;
265
        }
266

    
267
        input  = s->buffer[0];
268
    }
269

    
270
    lenout= 4*nb_samples * s->ratio + 16;
271

    
272
    if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
273
        output_bak = output;
274

    
275
        if (!s->buffer_size[1] || s->buffer_size[1] < lenout) {
276
            av_free(s->buffer[1]);
277
            s->buffer_size[1] = lenout;
278
            s->buffer[1] = av_malloc(s->buffer_size[1]);
279
            if (!s->buffer[1]) {
280
                av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
281
                return 0;
282
            }
283
        }
284

    
285
        output = s->buffer[1];
286
    }
287

    
288
    /* XXX: move those malloc to resample init code */
289
    for(i=0; i<s->filter_channels; i++){
290
        bufin[i]= av_malloc( (nb_samples + s->temp_len) * sizeof(short) );
291
        memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short));
292
        buftmp2[i] = bufin[i] + s->temp_len;
293
        bufout[i] = av_malloc(lenout * sizeof(short));
294
    }
295

    
296
    if (s->input_channels == 2 &&
297
        s->output_channels == 1) {
298
        buftmp3[0] = output;
299
        stereo_to_mono(buftmp2[0], input, nb_samples);
300
    } else if (s->output_channels >= 2 && s->input_channels == 1) {
301
        buftmp3[0] = bufout[0];
302
        memcpy(buftmp2[0], input, nb_samples*sizeof(short));
303
    } else if (s->output_channels >= s->input_channels && s->input_channels >= 2) {
304
        for (i = 0; i < s->input_channels; i++) {
305
            buftmp3[i] = bufout[i];
306
        }
307
        deinterleave(buftmp2, input, s->input_channels, nb_samples);
308
    } else {
309
        buftmp3[0] = output;
310
        memcpy(buftmp2[0], input, nb_samples*sizeof(short));
311
    }
312

    
313
    nb_samples += s->temp_len;
314

    
315
    /* resample each channel */
316
    nb_samples1 = 0; /* avoid warning */
317
    for(i=0;i<s->filter_channels;i++) {
318
        int consumed;
319
        int is_last= i+1 == s->filter_channels;
320

    
321
        nb_samples1 = av_resample(s->resample_context, buftmp3[i], bufin[i], &consumed, nb_samples, lenout, is_last);
322
        s->temp_len= nb_samples - consumed;
323
        s->temp[i]= av_realloc(s->temp[i], s->temp_len*sizeof(short));
324
        memcpy(s->temp[i], bufin[i] + consumed, s->temp_len*sizeof(short));
325
    }
326

    
327
    if (s->output_channels == 2 && s->input_channels == 1) {
328
        mono_to_stereo(output, buftmp3[0], nb_samples1);
329
    } else if (s->output_channels == 6 && s->input_channels == 2) {
330
        ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
331
    } else if (s->output_channels == s->input_channels && s->input_channels >= 2) {
332
        interleave(output, buftmp3, s->output_channels, nb_samples1);
333
    }
334

    
335
    if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
336
        int istride[1] = { 2 };
337
        int ostride[1] = { s->sample_size[1] };
338
        const void *ibuf[1] = { output };
339
        void       *obuf[1] = { output_bak };
340

    
341
        if (av_audio_convert(s->convert_ctx[1], obuf, ostride,
342
                             ibuf, istride, nb_samples1*s->output_channels) < 0) {
343
            av_log(s->resample_context, AV_LOG_ERROR, "Audio sample format convertion failed\n");
344
            return 0;
345
        }
346
    }
347

    
348
    for (i = 0; i < s->filter_channels; i++) {
349
        av_free(bufin[i]);
350
        av_free(bufout[i]);
351
    }
352

    
353
    return nb_samples1;
354
}
355

    
356
void audio_resample_close(ReSampleContext *s)
357
{
358
    int i;
359
    av_resample_close(s->resample_context);
360
    for (i = 0; i < s->filter_channels; i++)
361
        av_freep(&s->temp[i]);
362
    av_freep(&s->buffer[0]);
363
    av_freep(&s->buffer[1]);
364
    av_audio_convert_free(s->convert_ctx[0]);
365
    av_audio_convert_free(s->convert_ctx[1]);
366
    av_free(s);
367
}