]> git.sesse.net Git - ffmpeg/blob - libavcodec/resample.c
mp3dec: ask for 8khz switch point mp3s
[ffmpeg] / libavcodec / resample.c
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
24  * samplerate conversion for both audio and video
25  */
26
27 #include <string.h>
28
29 #include "avcodec.h"
30 #include "audioconvert.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/mem.h"
33 #include "libavutil/samplefmt.h"
34
35 #define MAX_CHANNELS 8
36
37 struct AVResampleContext;
38
39 static const char *context_to_name(void *ptr)
40 {
41     return "audioresample";
42 }
43
44 static const AVOption options[] = {{NULL}};
45 static const AVClass audioresample_context_class = {
46     "ReSampleContext", context_to_name, options, LIBAVUTIL_VERSION_INT
47 };
48
49 struct ReSampleContext {
50     struct AVResampleContext *resample_context;
51     short *temp[MAX_CHANNELS];
52     int temp_len;
53     float ratio;
54     /* channel convert */
55     int input_channels, output_channels, filter_channels;
56     AVAudioConvert *convert_ctx[2];
57     enum AVSampleFormat sample_fmt[2]; ///< input and output sample format
58     unsigned sample_size[2];           ///< size of one sample in sample_fmt
59     short *buffer[2];                  ///< buffers used for conversion to S16
60     unsigned buffer_size[2];           ///< sizes of allocated buffers
61 };
62
63 /* n1: number of samples */
64 static void stereo_to_mono(short *output, short *input, int n1)
65 {
66     short *p, *q;
67     int n = n1;
68
69     p = input;
70     q = output;
71     while (n >= 4) {
72         q[0] = (p[0] + p[1]) >> 1;
73         q[1] = (p[2] + p[3]) >> 1;
74         q[2] = (p[4] + p[5]) >> 1;
75         q[3] = (p[6] + p[7]) >> 1;
76         q += 4;
77         p += 8;
78         n -= 4;
79     }
80     while (n > 0) {
81         q[0] = (p[0] + p[1]) >> 1;
82         q++;
83         p += 2;
84         n--;
85     }
86 }
87
88 /* n1: number of samples */
89 static void mono_to_stereo(short *output, short *input, int n1)
90 {
91     short *p, *q;
92     int n = n1;
93     int v;
94
95     p = input;
96     q = output;
97     while (n >= 4) {
98         v = p[0]; q[0] = v; q[1] = v;
99         v = p[1]; q[2] = v; q[3] = v;
100         v = p[2]; q[4] = v; q[5] = v;
101         v = p[3]; q[6] = v; q[7] = v;
102         q += 8;
103         p += 4;
104         n -= 4;
105     }
106     while (n > 0) {
107         v = p[0]; q[0] = v; q[1] = v;
108         q += 2;
109         p += 1;
110         n--;
111     }
112 }
113
114 /*
115 5.1 to stereo input: [fl, fr, c, lfe, rl, rr]
116 - Left = front_left + rear_gain * rear_left + center_gain * center
117 - Right = front_right + rear_gain * rear_right + center_gain * center
118 Where rear_gain is usually around 0.5-1.0 and
119       center_gain is almost always 0.7 (-3 dB)
120 */
121 static void surround_to_stereo(short **output, short *input, int channels, int samples)
122 {
123     int i;
124     short l, r;
125
126     for (i = 0; i < samples; i++) {
127         int fl,fr,c,rl,rr;
128         fl = input[0];
129         fr = input[1];
130         c = input[2];
131         // lfe = input[3];
132         rl = input[4];
133         rr = input[5];
134
135         l = av_clip_int16(fl + (0.5 * rl) + (0.7 * c));
136         r = av_clip_int16(fr + (0.5 * rr) + (0.7 * c));
137
138         /* output l & r. */
139         *output[0]++ = l;
140         *output[1]++ = r;
141
142         /* increment input. */
143         input += channels;
144     }
145 }
146
147 static void deinterleave(short **output, short *input, int channels, int samples)
148 {
149     int i, j;
150
151     for (i = 0; i < samples; i++) {
152         for (j = 0; j < channels; j++) {
153             *output[j]++ = *input++;
154         }
155     }
156 }
157
158 static void interleave(short *output, short **input, int channels, int samples)
159 {
160     int i, j;
161
162     for (i = 0; i < samples; i++) {
163         for (j = 0; j < channels; j++) {
164             *output++ = *input[j]++;
165         }
166     }
167 }
168
169 static void ac3_5p1_mux(short *output, short *input1, short *input2, int n)
170 {
171     int i;
172     short l, r;
173
174     for (i = 0; i < n; i++) {
175         l = *input1++;
176         r = *input2++;
177         *output++ = l;                  /* left */
178         *output++ = (l / 2) + (r / 2);  /* center */
179         *output++ = r;                  /* right */
180         *output++ = 0;                  /* left surround */
181         *output++ = 0;                  /* right surroud */
182         *output++ = 0;                  /* low freq */
183     }
184 }
185
186 #define SUPPORT_RESAMPLE(ch1, ch2, ch3, ch4, ch5, ch6, ch7, ch8) \
187     ch8<<7 | ch7<<6 | ch6<<5 | ch5<<4 | ch4<<3 | ch3<<2 | ch2<<1 | ch1<<0
188
189 static const uint8_t supported_resampling[MAX_CHANNELS] = {
190     // output ch:    1  2  3  4  5  6  7  8
191     SUPPORT_RESAMPLE(1, 1, 0, 0, 0, 0, 0, 0), // 1 input channel
192     SUPPORT_RESAMPLE(1, 1, 0, 0, 0, 1, 0, 0), // 2 input channels
193     SUPPORT_RESAMPLE(0, 0, 1, 0, 0, 0, 0, 0), // 3 input channels
194     SUPPORT_RESAMPLE(0, 0, 0, 1, 0, 0, 0, 0), // 4 input channels
195     SUPPORT_RESAMPLE(0, 0, 0, 0, 1, 0, 0, 0), // 5 input channels
196     SUPPORT_RESAMPLE(0, 1, 0, 0, 0, 1, 0, 0), // 6 input channels
197     SUPPORT_RESAMPLE(0, 0, 0, 0, 0, 0, 1, 0), // 7 input channels
198     SUPPORT_RESAMPLE(0, 0, 0, 0, 0, 0, 0, 1), // 8 input channels
199 };
200
201 ReSampleContext *av_audio_resample_init(int output_channels, int input_channels,
202                                         int output_rate, int input_rate,
203                                         enum AVSampleFormat sample_fmt_out,
204                                         enum AVSampleFormat sample_fmt_in,
205                                         int filter_length, int log2_phase_count,
206                                         int linear, double cutoff)
207 {
208     ReSampleContext *s;
209
210     if (input_channels > MAX_CHANNELS) {
211         av_log(NULL, AV_LOG_ERROR,
212                "Resampling with input channels greater than %d is unsupported.\n",
213                MAX_CHANNELS);
214         return NULL;
215     }
216     if (!(supported_resampling[input_channels-1] & (1<<(output_channels-1)))) {
217         int i;
218         av_log(NULL, AV_LOG_ERROR, "Unsupported audio resampling. Allowed "
219                "output channels for %d input channel%s", input_channels,
220                input_channels > 1 ? "s:" : ":");
221         for (i = 0; i < MAX_CHANNELS; i++)
222             if (supported_resampling[input_channels-1] & (1<<i))
223                 av_log(NULL, AV_LOG_ERROR, " %d", i + 1);
224         av_log(NULL, AV_LOG_ERROR, "\n");
225         return NULL;
226     }
227
228     s = av_mallocz(sizeof(ReSampleContext));
229     if (!s) {
230         av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for resample context.\n");
231         return NULL;
232     }
233
234     s->ratio = (float)output_rate / (float)input_rate;
235
236     s->input_channels = input_channels;
237     s->output_channels = output_channels;
238
239     s->filter_channels = s->input_channels;
240     if (s->output_channels < s->filter_channels)
241         s->filter_channels = s->output_channels;
242
243     s->sample_fmt[0]  = sample_fmt_in;
244     s->sample_fmt[1]  = sample_fmt_out;
245     s->sample_size[0] = av_get_bytes_per_sample(s->sample_fmt[0]);
246     s->sample_size[1] = av_get_bytes_per_sample(s->sample_fmt[1]);
247
248     if (s->sample_fmt[0] != AV_SAMPLE_FMT_S16) {
249         if (!(s->convert_ctx[0] = av_audio_convert_alloc(AV_SAMPLE_FMT_S16, 1,
250                                                          s->sample_fmt[0], 1, NULL, 0))) {
251             av_log(s, AV_LOG_ERROR,
252                    "Cannot convert %s sample format to s16 sample format\n",
253                    av_get_sample_fmt_name(s->sample_fmt[0]));
254             av_free(s);
255             return NULL;
256         }
257     }
258
259     if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
260         if (!(s->convert_ctx[1] = av_audio_convert_alloc(s->sample_fmt[1], 1,
261                                                          AV_SAMPLE_FMT_S16, 1, NULL, 0))) {
262             av_log(s, AV_LOG_ERROR,
263                    "Cannot convert s16 sample format to %s sample format\n",
264                    av_get_sample_fmt_name(s->sample_fmt[1]));
265             av_audio_convert_free(s->convert_ctx[0]);
266             av_free(s);
267             return NULL;
268         }
269     }
270
271     s->resample_context = av_resample_init(output_rate, input_rate,
272                                            filter_length, log2_phase_count,
273                                            linear, cutoff);
274
275     *(const AVClass**)s->resample_context = &audioresample_context_class;
276
277     return s;
278 }
279
280 /* resample audio. 'nb_samples' is the number of input samples */
281 /* XXX: optimize it ! */
282 int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
283 {
284     int i, nb_samples1;
285     short *bufin[MAX_CHANNELS];
286     short *bufout[MAX_CHANNELS];
287     short *buftmp2[MAX_CHANNELS], *buftmp3[MAX_CHANNELS];
288     short *output_bak = NULL;
289     int lenout;
290
291     if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) {
292         /* nothing to do */
293         memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
294         return nb_samples;
295     }
296
297     if (s->sample_fmt[0] != AV_SAMPLE_FMT_S16) {
298         int istride[1] = { s->sample_size[0] };
299         int ostride[1] = { 2 };
300         const void *ibuf[1] = { input };
301         void       *obuf[1];
302         unsigned input_size = nb_samples * s->input_channels * 2;
303
304         if (!s->buffer_size[0] || s->buffer_size[0] < input_size) {
305             av_free(s->buffer[0]);
306             s->buffer_size[0] = input_size;
307             s->buffer[0] = av_malloc(s->buffer_size[0]);
308             if (!s->buffer[0]) {
309                 av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
310                 return 0;
311             }
312         }
313
314         obuf[0] = s->buffer[0];
315
316         if (av_audio_convert(s->convert_ctx[0], obuf, ostride,
317                              ibuf, istride, nb_samples * s->input_channels) < 0) {
318             av_log(s->resample_context, AV_LOG_ERROR,
319                    "Audio sample format conversion failed\n");
320             return 0;
321         }
322
323         input = s->buffer[0];
324     }
325
326     lenout= 2*s->output_channels*nb_samples * s->ratio + 16;
327
328     if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
329         int out_size = lenout * av_get_bytes_per_sample(s->sample_fmt[1]) *
330                        s->output_channels;
331         output_bak = output;
332
333         if (!s->buffer_size[1] || s->buffer_size[1] < out_size) {
334             av_free(s->buffer[1]);
335             s->buffer_size[1] = out_size;
336             s->buffer[1] = av_malloc(s->buffer_size[1]);
337             if (!s->buffer[1]) {
338                 av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
339                 return 0;
340             }
341         }
342
343         output = s->buffer[1];
344     }
345
346     /* XXX: move those malloc to resample init code */
347     for (i = 0; i < s->filter_channels; i++) {
348         bufin[i] = av_malloc((nb_samples + s->temp_len) * sizeof(short));
349         memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short));
350         buftmp2[i] = bufin[i] + s->temp_len;
351         bufout[i] = av_malloc(lenout * sizeof(short));
352     }
353
354     if (s->input_channels == 2 && s->output_channels == 1) {
355         buftmp3[0] = output;
356         stereo_to_mono(buftmp2[0], input, nb_samples);
357     } else if (s->output_channels >= 2 && s->input_channels == 1) {
358         buftmp3[0] = bufout[0];
359         memcpy(buftmp2[0], input, nb_samples * sizeof(short));
360     } else if (s->input_channels == 6 && s->output_channels ==2) {
361         buftmp3[0] = bufout[0];
362         buftmp3[1] = bufout[1];
363         surround_to_stereo(buftmp2, input, s->input_channels, nb_samples);
364     } else if (s->output_channels >= s->input_channels && s->input_channels >= 2) {
365         for (i = 0; i < s->input_channels; i++) {
366             buftmp3[i] = bufout[i];
367         }
368         deinterleave(buftmp2, input, s->input_channels, nb_samples);
369     } else {
370         buftmp3[0] = output;
371         memcpy(buftmp2[0], input, nb_samples * sizeof(short));
372     }
373
374     nb_samples += s->temp_len;
375
376     /* resample each channel */
377     nb_samples1 = 0; /* avoid warning */
378     for (i = 0; i < s->filter_channels; i++) {
379         int consumed;
380         int is_last = i + 1 == s->filter_channels;
381
382         nb_samples1 = av_resample(s->resample_context, buftmp3[i], bufin[i],
383                                   &consumed, nb_samples, lenout, is_last);
384         s->temp_len = nb_samples - consumed;
385         s->temp[i] = av_realloc(s->temp[i], s->temp_len * sizeof(short));
386         memcpy(s->temp[i], bufin[i] + consumed, s->temp_len * sizeof(short));
387     }
388
389     if (s->output_channels == 2 && s->input_channels == 1) {
390         mono_to_stereo(output, buftmp3[0], nb_samples1);
391     } else if (s->output_channels == 6 && s->input_channels == 2) {
392         ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
393     } else if ((s->output_channels == s->input_channels && s->input_channels >= 2) ||
394                (s->output_channels == 2 && s->input_channels == 6)) {
395         interleave(output, buftmp3, s->output_channels, nb_samples1);
396     }
397
398     if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
399         int istride[1] = { 2 };
400         int ostride[1] = { s->sample_size[1] };
401         const void *ibuf[1] = { output };
402         void       *obuf[1] = { output_bak };
403
404         if (av_audio_convert(s->convert_ctx[1], obuf, ostride,
405                              ibuf, istride, nb_samples1 * s->output_channels) < 0) {
406             av_log(s->resample_context, AV_LOG_ERROR,
407                    "Audio sample format convertion failed\n");
408             return 0;
409         }
410     }
411
412     for (i = 0; i < s->filter_channels; i++) {
413         av_free(bufin[i]);
414         av_free(bufout[i]);
415     }
416
417     return nb_samples1;
418 }
419
420 void audio_resample_close(ReSampleContext *s)
421 {
422     int i;
423     av_resample_close(s->resample_context);
424     for (i = 0; i < s->filter_channels; i++)
425         av_freep(&s->temp[i]);
426     av_freep(&s->buffer[0]);
427     av_freep(&s->buffer[1]);
428     av_audio_convert_free(s->convert_ctx[0]);
429     av_audio_convert_free(s->convert_ctx[1]);
430     av_free(s);
431 }