1/*
2 * samplerate conversion for both audio and video
3 * Copyright (c) 2000 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 libavcodec/resample.c
24 * samplerate conversion for both audio and video
25 */
26
27#include "avcodec.h"
28#include "audioconvert.h"
29#include "opt.h"
30
31struct AVResampleContext;
32
33static const char *context_to_name(void *ptr)
34{
35    return "audioresample";
36}
37
38static const AVOption options[] = {{NULL}};
39static const AVClass audioresample_context_class = { "ReSampleContext", context_to_name, options };
40
41struct ReSampleContext {
42    const AVClass *av_class;
43    struct AVResampleContext *resample_context;
44    short *temp[2];
45    int temp_len;
46    float ratio;
47    /* channel convert */
48    int input_channels, output_channels, filter_channels;
49    AVAudioConvert *convert_ctx[2];
50    enum SampleFormat sample_fmt[2]; ///< input and output sample format
51    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};
55
56/* n1: number of samples */
57static 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 */
82static 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 */
108static 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
118static 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
128static 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
145ReSampleContext *av_audio_resample_init(int output_channels, int input_channels,
146                                        int output_rate, int input_rate,
147                                        enum SampleFormat sample_fmt_out,
148                                        enum SampleFormat sample_fmt_in,
149                                        int filter_length, int log2_phase_count,
150                                        int linear, double cutoff)
151{
152    ReSampleContext *s;
153
154    if ( input_channels > 2)
155      {
156        av_log(NULL, AV_LOG_ERROR, "Resampling with input channels greater than 2 unsupported.\n");
157        return NULL;
158      }
159
160    s = av_mallocz(sizeof(ReSampleContext));
161    if (!s)
162      {
163        av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for resample context.\n");
164        return NULL;
165      }
166
167    s->ratio = (float)output_rate / (float)input_rate;
168
169    s->input_channels = input_channels;
170    s->output_channels = output_channels;
171
172    s->filter_channels = s->input_channels;
173    if (s->output_channels < s->filter_channels)
174        s->filter_channels = s->output_channels;
175
176    s->sample_fmt [0] = sample_fmt_in;
177    s->sample_fmt [1] = sample_fmt_out;
178    s->sample_size[0] = av_get_bits_per_sample_format(s->sample_fmt[0])>>3;
179    s->sample_size[1] = av_get_bits_per_sample_format(s->sample_fmt[1])>>3;
180
181    if (s->sample_fmt[0] != SAMPLE_FMT_S16) {
182        if (!(s->convert_ctx[0] = av_audio_convert_alloc(SAMPLE_FMT_S16, 1,
183                                                         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                   avcodec_get_sample_fmt_name(s->sample_fmt[0]));
187            av_free(s);
188            return NULL;
189        }
190    }
191
192    if (s->sample_fmt[1] != SAMPLE_FMT_S16) {
193        if (!(s->convert_ctx[1] = av_audio_convert_alloc(s->sample_fmt[1], 1,
194                                                         SAMPLE_FMT_S16, 1, NULL, 0))) {
195            av_log(s, AV_LOG_ERROR,
196                   "Cannot convert s16 sample format to %s sample format\n",
197                   avcodec_get_sample_fmt_name(s->sample_fmt[1]));
198            av_audio_convert_free(s->convert_ctx[0]);
199            av_free(s);
200            return NULL;
201        }
202    }
203
204/*
205 * AC-3 output is the only case where filter_channels could be greater than 2.
206 * 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#define TAPS 16
213    s->resample_context= av_resample_init(output_rate, input_rate,
214                         filter_length, log2_phase_count, linear, cutoff);
215
216    s->av_class= &audioresample_context_class;
217
218    return s;
219}
220
221#if LIBAVCODEC_VERSION_MAJOR < 53
222ReSampleContext *audio_resample_init(int output_channels, int input_channels,
223                                     int output_rate, int input_rate)
224{
225    return av_audio_resample_init(output_channels, input_channels,
226                                  output_rate, input_rate,
227                                  SAMPLE_FMT_S16, SAMPLE_FMT_S16,
228                                  TAPS, 10, 0, 0.8);
229}
230#endif
231
232/* resample audio. 'nb_samples' is the number of input samples */
233/* XXX: optimize it ! */
234int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
235{
236    int i, nb_samples1;
237    short *bufin[2];
238    short *bufout[2];
239    short *buftmp2[2], *buftmp3[2];
240    short *output_bak = NULL;
241    int lenout;
242
243    if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) {
244        /* nothing to do */
245        memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
246        return nb_samples;
247    }
248
249    if (s->sample_fmt[0] != SAMPLE_FMT_S16) {
250        int istride[1] = { s->sample_size[0] };
251        int ostride[1] = { 2 };
252        const void *ibuf[1] = { input };
253        void       *obuf[1];
254        unsigned input_size = nb_samples*s->input_channels*2;
255
256        if (!s->buffer_size[0] || s->buffer_size[0] < input_size) {
257            av_free(s->buffer[0]);
258            s->buffer_size[0] = input_size;
259            s->buffer[0] = av_malloc(s->buffer_size[0]);
260            if (!s->buffer[0]) {
261                av_log(s, AV_LOG_ERROR, "Could not allocate buffer\n");
262                return 0;
263            }
264        }
265
266        obuf[0] = s->buffer[0];
267
268        if (av_audio_convert(s->convert_ctx[0], obuf, ostride,
269                             ibuf, istride, nb_samples*s->input_channels) < 0) {
270            av_log(s, AV_LOG_ERROR, "Audio sample format conversion failed\n");
271            return 0;
272        }
273
274        input  = s->buffer[0];
275    }
276
277    lenout= 4*nb_samples * s->ratio + 16;
278
279    if (s->sample_fmt[1] != SAMPLE_FMT_S16) {
280        output_bak = output;
281
282        if (!s->buffer_size[1] || s->buffer_size[1] < lenout) {
283            av_free(s->buffer[1]);
284            s->buffer_size[1] = lenout;
285            s->buffer[1] = av_malloc(s->buffer_size[1]);
286            if (!s->buffer[1]) {
287                av_log(s, AV_LOG_ERROR, "Could not allocate buffer\n");
288                return 0;
289            }
290        }
291
292        output = s->buffer[1];
293    }
294
295    /* XXX: move those malloc to resample init code */
296    for(i=0; i<s->filter_channels; i++){
297        bufin[i]= av_malloc( (nb_samples + s->temp_len) * sizeof(short) );
298        memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short));
299        buftmp2[i] = bufin[i] + s->temp_len;
300    }
301
302    /* make some zoom to avoid round pb */
303    bufout[0]= av_malloc( lenout * sizeof(short) );
304    bufout[1]= av_malloc( lenout * sizeof(short) );
305
306    if (s->input_channels == 2 &&
307        s->output_channels == 1) {
308        buftmp3[0] = output;
309        stereo_to_mono(buftmp2[0], input, nb_samples);
310    } else if (s->output_channels >= 2 && s->input_channels == 1) {
311        buftmp3[0] = bufout[0];
312        memcpy(buftmp2[0], input, nb_samples*sizeof(short));
313    } else if (s->output_channels >= 2) {
314        buftmp3[0] = bufout[0];
315        buftmp3[1] = bufout[1];
316        stereo_split(buftmp2[0], buftmp2[1], input, nb_samples);
317    } else {
318        buftmp3[0] = output;
319        memcpy(buftmp2[0], input, nb_samples*sizeof(short));
320    }
321
322    nb_samples += s->temp_len;
323
324    /* resample each channel */
325    nb_samples1 = 0; /* avoid warning */
326    for(i=0;i<s->filter_channels;i++) {
327        int consumed;
328        int is_last= i+1 == s->filter_channels;
329
330        nb_samples1 = av_resample(s->resample_context, buftmp3[i], bufin[i], &consumed, nb_samples, lenout, is_last);
331        s->temp_len= nb_samples - consumed;
332        s->temp[i]= av_realloc(s->temp[i], s->temp_len*sizeof(short));
333        memcpy(s->temp[i], bufin[i] + consumed, s->temp_len*sizeof(short));
334    }
335
336    if (s->output_channels == 2 && s->input_channels == 1) {
337        mono_to_stereo(output, buftmp3[0], nb_samples1);
338    } else if (s->output_channels == 2) {
339        stereo_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
340    } else if (s->output_channels == 6) {
341        ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
342    }
343
344    if (s->sample_fmt[1] != SAMPLE_FMT_S16) {
345        int istride[1] = { 2 };
346        int ostride[1] = { s->sample_size[1] };
347        const void *ibuf[1] = { output };
348        void       *obuf[1] = { output_bak };
349
350        if (av_audio_convert(s->convert_ctx[1], obuf, ostride,
351                             ibuf, istride, nb_samples1*s->output_channels) < 0) {
352            av_log(s, AV_LOG_ERROR, "Audio sample format convertion failed\n");
353            return 0;
354        }
355    }
356
357    for(i=0; i<s->filter_channels; i++)
358        av_free(bufin[i]);
359
360    av_free(bufout[0]);
361    av_free(bufout[1]);
362    return nb_samples1;
363}
364
365void audio_resample_close(ReSampleContext *s)
366{
367    av_resample_close(s->resample_context);
368    av_freep(&s->temp[0]);
369    av_freep(&s->temp[1]);
370    av_freep(&s->buffer[0]);
371    av_freep(&s->buffer[1]);
372    av_audio_convert_free(s->convert_ctx[0]);
373    av_audio_convert_free(s->convert_ctx[1]);
374    av_free(s);
375}
376