]> git.sesse.net Git - ffmpeg/blob - libavcodec/resample.c
ra288dec: set channel layout
[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 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 <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 #if FF_API_AVCODEC_RESAMPLE
36
37 #define MAX_CHANNELS 8
38
39 struct AVResampleContext;
40
41 static const char *context_to_name(void *ptr)
42 {
43     return "audioresample";
44 }
45
46 static const AVOption options[] = {{NULL}};
47 static const AVClass audioresample_context_class = {
48     "ReSampleContext", context_to_name, options, LIBAVUTIL_VERSION_INT
49 };
50
51 struct ReSampleContext {
52     struct AVResampleContext *resample_context;
53     short *temp[MAX_CHANNELS];
54     int temp_len;
55     float ratio;
56     /* channel convert */
57     int input_channels, output_channels, filter_channels;
58     AVAudioConvert *convert_ctx[2];
59     enum AVSampleFormat sample_fmt[2]; ///< input and output sample format
60     unsigned sample_size[2];           ///< size of one sample in sample_fmt
61     short *buffer[2];                  ///< buffers used for conversion to S16
62     unsigned buffer_size[2];           ///< sizes of allocated buffers
63 };
64
65 /* n1: number of samples */
66 static void stereo_to_mono(short *output, short *input, int n1)
67 {
68     short *p, *q;
69     int n = n1;
70
71     p = input;
72     q = output;
73     while (n >= 4) {
74         q[0] = (p[0] + p[1]) >> 1;
75         q[1] = (p[2] + p[3]) >> 1;
76         q[2] = (p[4] + p[5]) >> 1;
77         q[3] = (p[6] + p[7]) >> 1;
78         q += 4;
79         p += 8;
80         n -= 4;
81     }
82     while (n > 0) {
83         q[0] = (p[0] + p[1]) >> 1;
84         q++;
85         p += 2;
86         n--;
87     }
88 }
89
90 /* n1: number of samples */
91 static void mono_to_stereo(short *output, short *input, int n1)
92 {
93     short *p, *q;
94     int n = n1;
95     int v;
96
97     p = input;
98     q = output;
99     while (n >= 4) {
100         v = p[0]; q[0] = v; q[1] = v;
101         v = p[1]; q[2] = v; q[3] = v;
102         v = p[2]; q[4] = v; q[5] = v;
103         v = p[3]; q[6] = v; q[7] = v;
104         q += 8;
105         p += 4;
106         n -= 4;
107     }
108     while (n > 0) {
109         v = p[0]; q[0] = v; q[1] = v;
110         q += 2;
111         p += 1;
112         n--;
113     }
114 }
115
116 static void deinterleave(short **output, short *input, int channels, int samples)
117 {
118     int i, j;
119
120     for (i = 0; i < samples; i++) {
121         for (j = 0; j < channels; j++) {
122             *output[j]++ = *input++;
123         }
124     }
125 }
126
127 static void interleave(short *output, short **input, int channels, int samples)
128 {
129     int i, j;
130
131     for (i = 0; i < samples; i++) {
132         for (j = 0; j < channels; j++) {
133             *output++ = *input[j]++;
134         }
135     }
136 }
137
138 static void ac3_5p1_mux(short *output, short *input1, short *input2, int n)
139 {
140     int i;
141     short l, r;
142
143     for (i = 0; i < n; i++) {
144         l = *input1++;
145         r = *input2++;
146         *output++ = l;                  /* left */
147         *output++ = (l / 2) + (r / 2);  /* center */
148         *output++ = r;                  /* right */
149         *output++ = 0;                  /* left surround */
150         *output++ = 0;                  /* right surroud */
151         *output++ = 0;                  /* low freq */
152     }
153 }
154
155 ReSampleContext *av_audio_resample_init(int output_channels, int input_channels,
156                                         int output_rate, int input_rate,
157                                         enum AVSampleFormat sample_fmt_out,
158                                         enum AVSampleFormat sample_fmt_in,
159                                         int filter_length, int log2_phase_count,
160                                         int linear, double cutoff)
161 {
162     ReSampleContext *s;
163
164     if (input_channels > MAX_CHANNELS) {
165         av_log(NULL, AV_LOG_ERROR,
166                "Resampling with input channels greater than %d is unsupported.\n",
167                MAX_CHANNELS);
168         return NULL;
169     }
170     if (output_channels != input_channels &&
171         (input_channels  > 2 ||
172          output_channels > 2 &&
173          !(output_channels == 6 && input_channels == 2))) {
174         av_log(NULL, AV_LOG_ERROR,
175                "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");
176         return NULL;
177     }
178
179     s = av_mallocz(sizeof(ReSampleContext));
180     if (!s) {
181         av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for resample context.\n");
182         return NULL;
183     }
184
185     s->ratio = (float)output_rate / (float)input_rate;
186
187     s->input_channels = input_channels;
188     s->output_channels = output_channels;
189
190     s->filter_channels = s->input_channels;
191     if (s->output_channels < s->filter_channels)
192         s->filter_channels = s->output_channels;
193
194     s->sample_fmt[0]  = sample_fmt_in;
195     s->sample_fmt[1]  = sample_fmt_out;
196     s->sample_size[0] = av_get_bytes_per_sample(s->sample_fmt[0]);
197     s->sample_size[1] = av_get_bytes_per_sample(s->sample_fmt[1]);
198
199     if (s->sample_fmt[0] != AV_SAMPLE_FMT_S16) {
200         if (!(s->convert_ctx[0] = av_audio_convert_alloc(AV_SAMPLE_FMT_S16, 1,
201                                                          s->sample_fmt[0], 1, NULL, 0))) {
202             av_log(s, AV_LOG_ERROR,
203                    "Cannot convert %s sample format to s16 sample format\n",
204                    av_get_sample_fmt_name(s->sample_fmt[0]));
205             av_free(s);
206             return NULL;
207         }
208     }
209
210     if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
211         if (!(s->convert_ctx[1] = av_audio_convert_alloc(s->sample_fmt[1], 1,
212                                                          AV_SAMPLE_FMT_S16, 1, NULL, 0))) {
213             av_log(s, AV_LOG_ERROR,
214                    "Cannot convert s16 sample format to %s sample format\n",
215                    av_get_sample_fmt_name(s->sample_fmt[1]));
216             av_audio_convert_free(s->convert_ctx[0]);
217             av_free(s);
218             return NULL;
219         }
220     }
221
222     s->resample_context = av_resample_init(output_rate, input_rate,
223                                            filter_length, log2_phase_count,
224                                            linear, cutoff);
225
226     *(const AVClass**)s->resample_context = &audioresample_context_class;
227
228     return s;
229 }
230
231 /* resample audio. 'nb_samples' is the number of input samples */
232 /* XXX: optimize it ! */
233 int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
234 {
235     int i, nb_samples1;
236     short *bufin[MAX_CHANNELS];
237     short *bufout[MAX_CHANNELS];
238     short *buftmp2[MAX_CHANNELS], *buftmp3[MAX_CHANNELS];
239     short *output_bak = NULL;
240     int lenout;
241
242     if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) {
243         /* nothing to do */
244         memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
245         return nb_samples;
246     }
247
248     if (s->sample_fmt[0] != AV_SAMPLE_FMT_S16) {
249         int istride[1] = { s->sample_size[0] };
250         int ostride[1] = { 2 };
251         const void *ibuf[1] = { input };
252         void       *obuf[1];
253         unsigned input_size = nb_samples * s->input_channels * 2;
254
255         if (!s->buffer_size[0] || s->buffer_size[0] < input_size) {
256             av_free(s->buffer[0]);
257             s->buffer_size[0] = input_size;
258             s->buffer[0] = av_malloc(s->buffer_size[0]);
259             if (!s->buffer[0]) {
260                 av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
261                 return 0;
262             }
263         }
264
265         obuf[0] = s->buffer[0];
266
267         if (av_audio_convert(s->convert_ctx[0], obuf, ostride,
268                              ibuf, istride, nb_samples * s->input_channels) < 0) {
269             av_log(s->resample_context, AV_LOG_ERROR,
270                    "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] != AV_SAMPLE_FMT_S16) {
280         int out_size = lenout * av_get_bytes_per_sample(s->sample_fmt[1]) *
281                        s->output_channels;
282         output_bak = output;
283
284         if (!s->buffer_size[1] || s->buffer_size[1] < out_size) {
285             av_free(s->buffer[1]);
286             s->buffer_size[1] = out_size;
287             s->buffer[1] = av_malloc(s->buffer_size[1]);
288             if (!s->buffer[1]) {
289                 av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
290                 return 0;
291             }
292         }
293
294         output = s->buffer[1];
295     }
296
297     /* XXX: move those malloc to resample init code */
298     for (i = 0; i < s->filter_channels; i++) {
299         bufin[i] = av_malloc((nb_samples + s->temp_len) * sizeof(short));
300         memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short));
301         buftmp2[i] = bufin[i] + s->temp_len;
302         bufout[i] = av_malloc(lenout * sizeof(short));
303     }
304
305     if (s->input_channels == 2 && s->output_channels == 1) {
306         buftmp3[0] = output;
307         stereo_to_mono(buftmp2[0], input, nb_samples);
308     } else if (s->output_channels >= 2 && s->input_channels == 1) {
309         buftmp3[0] = bufout[0];
310         memcpy(buftmp2[0], input, nb_samples * sizeof(short));
311     } else if (s->output_channels >= s->input_channels && s->input_channels >= 2) {
312         for (i = 0; i < s->input_channels; i++) {
313             buftmp3[i] = bufout[i];
314         }
315         deinterleave(buftmp2, input, s->input_channels, nb_samples);
316     } else {
317         buftmp3[0] = output;
318         memcpy(buftmp2[0], input, nb_samples * sizeof(short));
319     }
320
321     nb_samples += s->temp_len;
322
323     /* resample each channel */
324     nb_samples1 = 0; /* avoid warning */
325     for (i = 0; i < s->filter_channels; i++) {
326         int consumed;
327         int is_last = i + 1 == s->filter_channels;
328
329         nb_samples1 = av_resample(s->resample_context, buftmp3[i], bufin[i],
330                                   &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 == 6 && s->input_channels == 2) {
339         ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
340     } else if (s->output_channels == s->input_channels && s->input_channels >= 2) {
341         interleave(output, buftmp3, s->output_channels, nb_samples1);
342     }
343
344     if (s->sample_fmt[1] != AV_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->resample_context, AV_LOG_ERROR,
353                    "Audio sample format convertion failed\n");
354             return 0;
355         }
356     }
357
358     for (i = 0; i < s->filter_channels; i++) {
359         av_free(bufin[i]);
360         av_free(bufout[i]);
361     }
362
363     return nb_samples1;
364 }
365
366 void audio_resample_close(ReSampleContext *s)
367 {
368     int i;
369     av_resample_close(s->resample_context);
370     for (i = 0; i < s->filter_channels; i++)
371         av_freep(&s->temp[i]);
372     av_freep(&s->buffer[0]);
373     av_freep(&s->buffer[1]);
374     av_audio_convert_free(s->convert_ctx[0]);
375     av_audio_convert_free(s->convert_ctx[1]);
376     av_free(s);
377 }
378
379 #endif