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