Add version to AVClass so we can add to and use fields of AVClass without ABI issues.
[libav.git] / libavcodec / resample.c
CommitLineData
de6d9b64 1/*
f1ea5c2a 2 * samplerate conversion for both audio and video
406792e7 3 * Copyright (c) 2000 Fabrice Bellard
de6d9b64 4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
ff4ec49e
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
ff4ec49e 17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 20 */
983e3246
MN
21
22/**
ba87f080 23 * @file
f1ea5c2a 24 * samplerate conversion for both audio and video
983e3246
MN
25 */
26
de6d9b64 27#include "avcodec.h"
d1e3c6fd
BC
28#include "audioconvert.h"
29#include "opt.h"
69db4e10 30
aaaf1635 31struct AVResampleContext;
de6d9b64 32
d1e3c6fd
BC
33static const char *context_to_name(void *ptr)
34{
35 return "audioresample";
36}
37
38static const AVOption options[] = {{NULL}};
2308b6c1 39static const AVClass audioresample_context_class = { "ReSampleContext", context_to_name, options, LIBAVUTIL_VERSION_INT };
d1e3c6fd 40
de6d9b64 41struct ReSampleContext {
aaaf1635
MN
42 struct AVResampleContext *resample_context;
43 short *temp[2];
44 int temp_len;
de6d9b64
FB
45 float ratio;
46 /* channel convert */
47 int input_channels, output_channels, filter_channels;
d1e3c6fd
BC
48 AVAudioConvert *convert_ctx[2];
49 enum SampleFormat sample_fmt[2]; ///< input and output sample format
50 unsigned sample_size[2]; ///< size of one sample in sample_fmt
51 short *buffer[2]; ///< buffers used for conversion to S16
52 unsigned buffer_size[2]; ///< sizes of allocated buffers
de6d9b64
FB
53};
54
de6d9b64
FB
55/* n1: number of samples */
56static void stereo_to_mono(short *output, short *input, int n1)
57{
58 short *p, *q;
59 int n = n1;
60
61 p = input;
62 q = output;
63 while (n >= 4) {
64 q[0] = (p[0] + p[1]) >> 1;
65 q[1] = (p[2] + p[3]) >> 1;
66 q[2] = (p[4] + p[5]) >> 1;
67 q[3] = (p[6] + p[7]) >> 1;
68 q += 4;
69 p += 8;
70 n -= 4;
71 }
72 while (n > 0) {
73 q[0] = (p[0] + p[1]) >> 1;
74 q++;
75 p += 2;
76 n--;
77 }
78}
79
80/* n1: number of samples */
81static void mono_to_stereo(short *output, short *input, int n1)
82{
83 short *p, *q;
84 int n = n1;
85 int v;
86
87 p = input;
88 q = output;
89 while (n >= 4) {
90 v = p[0]; q[0] = v; q[1] = v;
91 v = p[1]; q[2] = v; q[3] = v;
92 v = p[2]; q[4] = v; q[5] = v;
93 v = p[3]; q[6] = v; q[7] = v;
94 q += 8;
95 p += 4;
96 n -= 4;
97 }
98 while (n > 0) {
99 v = p[0]; q[0] = v; q[1] = v;
100 q += 2;
101 p += 1;
102 n--;
103 }
104}
105
106/* XXX: should use more abstract 'N' channels system */
107static void stereo_split(short *output1, short *output2, short *input, int n)
108{
109 int i;
110
111 for(i=0;i<n;i++) {
112 *output1++ = *input++;
113 *output2++ = *input++;
114 }
115}
116
117static void stereo_mux(short *output, short *input1, short *input2, int n)
118{
119 int i;
120
121 for(i=0;i<n;i++) {
122 *output++ = *input1++;
123 *output++ = *input2++;
124 }
125}
126
743739d2
MN
127static void ac3_5p1_mux(short *output, short *input1, short *input2, int n)
128{
129 int i;
130 short l,r;
131
132 for(i=0;i<n;i++) {
133 l=*input1++;
134 r=*input2++;
135 *output++ = l; /* left */
136 *output++ = (l/2)+(r/2); /* center */
137 *output++ = r; /* right */
138 *output++ = 0; /* left surround */
139 *output++ = 0; /* right surroud */
140 *output++ = 0; /* low freq */
141 }
142}
143
d1e3c6fd
BC
144ReSampleContext *av_audio_resample_init(int output_channels, int input_channels,
145 int output_rate, int input_rate,
146 enum SampleFormat sample_fmt_out,
147 enum SampleFormat sample_fmt_in,
148 int filter_length, int log2_phase_count,
149 int linear, double cutoff)
de6d9b64
FB
150{
151 ReSampleContext *s;
115329f1 152
743739d2
MN
153 if ( input_channels > 2)
154 {
30dc5541 155 av_log(NULL, AV_LOG_ERROR, "Resampling with input channels greater than 2 unsupported.\n");
bb270c08 156 return NULL;
743739d2 157 }
de6d9b64
FB
158
159 s = av_mallocz(sizeof(ReSampleContext));
160 if (!s)
743739d2 161 {
30dc5541 162 av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for resample context.\n");
bb270c08 163 return NULL;
743739d2 164 }
de6d9b64
FB
165
166 s->ratio = (float)output_rate / (float)input_rate;
115329f1 167
de6d9b64
FB
168 s->input_channels = input_channels;
169 s->output_channels = output_channels;
115329f1 170
de6d9b64
FB
171 s->filter_channels = s->input_channels;
172 if (s->output_channels < s->filter_channels)
173 s->filter_channels = s->output_channels;
174
d1e3c6fd
BC
175 s->sample_fmt [0] = sample_fmt_in;
176 s->sample_fmt [1] = sample_fmt_out;
177 s->sample_size[0] = av_get_bits_per_sample_format(s->sample_fmt[0])>>3;
178 s->sample_size[1] = av_get_bits_per_sample_format(s->sample_fmt[1])>>3;
179
180 if (s->sample_fmt[0] != SAMPLE_FMT_S16) {
181 if (!(s->convert_ctx[0] = av_audio_convert_alloc(SAMPLE_FMT_S16, 1,
182 s->sample_fmt[0], 1, NULL, 0))) {
183 av_log(s, AV_LOG_ERROR,
184 "Cannot convert %s sample format to s16 sample format\n",
185 avcodec_get_sample_fmt_name(s->sample_fmt[0]));
186 av_free(s);
187 return NULL;
188 }
189 }
190
191 if (s->sample_fmt[1] != SAMPLE_FMT_S16) {
192 if (!(s->convert_ctx[1] = av_audio_convert_alloc(s->sample_fmt[1], 1,
193 SAMPLE_FMT_S16, 1, NULL, 0))) {
194 av_log(s, AV_LOG_ERROR,
195 "Cannot convert s16 sample format to %s sample format\n",
196 avcodec_get_sample_fmt_name(s->sample_fmt[1]));
197 av_audio_convert_free(s->convert_ctx[0]);
198 av_free(s);
199 return NULL;
200 }
201 }
202
743739d2 203/*
14b70628 204 * AC-3 output is the only case where filter_channels could be greater than 2.
743739d2
MN
205 * input channels can't be greater than 2, so resample the 2 channels and then
206 * expand to 6 channels after the resampling.
207 */
208 if(s->filter_channels>2)
209 s->filter_channels = 2;
210
8ec04d34 211#define TAPS 16
d1e3c6fd
BC
212 s->resample_context= av_resample_init(output_rate, input_rate,
213 filter_length, log2_phase_count, linear, cutoff);
214
844d17fb 215 *(const AVClass**)s->resample_context = &audioresample_context_class;
115329f1 216
de6d9b64
FB
217 return s;
218}
219
d1e3c6fd
BC
220#if LIBAVCODEC_VERSION_MAJOR < 53
221ReSampleContext *audio_resample_init(int output_channels, int input_channels,
222 int output_rate, int input_rate)
223{
224 return av_audio_resample_init(output_channels, input_channels,
225 output_rate, input_rate,
226 SAMPLE_FMT_S16, SAMPLE_FMT_S16,
227 TAPS, 10, 0, 0.8);
228}
229#endif
230
de6d9b64
FB
231/* resample audio. 'nb_samples' is the number of input samples */
232/* XXX: optimize it ! */
de6d9b64
FB
233int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
234{
235 int i, nb_samples1;
1a565432
FB
236 short *bufin[2];
237 short *bufout[2];
de6d9b64 238 short *buftmp2[2], *buftmp3[2];
d1e3c6fd 239 short *output_bak = NULL;
1a565432 240 int lenout;
de6d9b64 241
b9d2085b 242 if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) {
de6d9b64
FB
243 /* nothing to do */
244 memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
245 return nb_samples;
246 }
247
d1e3c6fd
BC
248 if (s->sample_fmt[0] != 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];
5f5e6af1 253 unsigned input_size = nb_samples*s->input_channels*2;
d1e3c6fd
BC
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]) {
89bc05d1 260 av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
d1e3c6fd
BC
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) {
89bc05d1 269 av_log(s->resample_context, AV_LOG_ERROR, "Audio sample format conversion failed\n");
d1e3c6fd
BC
270 return 0;
271 }
272
273 input = s->buffer[0];
274 }
275
276 lenout= 4*nb_samples * s->ratio + 16;
277
278 if (s->sample_fmt[1] != SAMPLE_FMT_S16) {
279 output_bak = output;
280
281 if (!s->buffer_size[1] || s->buffer_size[1] < lenout) {
282 av_free(s->buffer[1]);
283 s->buffer_size[1] = lenout;
284 s->buffer[1] = av_malloc(s->buffer_size[1]);
285 if (!s->buffer[1]) {
89bc05d1 286 av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
d1e3c6fd
BC
287 return 0;
288 }
289 }
290
291 output = s->buffer[1];
292 }
293
1a565432 294 /* XXX: move those malloc to resample init code */
aaaf1635 295 for(i=0; i<s->filter_channels; i++){
90901860 296 bufin[i]= av_malloc( (nb_samples + s->temp_len) * sizeof(short) );
aaaf1635
MN
297 memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short));
298 buftmp2[i] = bufin[i] + s->temp_len;
299 }
115329f1 300
1a565432 301 /* make some zoom to avoid round pb */
90901860
MN
302 bufout[0]= av_malloc( lenout * sizeof(short) );
303 bufout[1]= av_malloc( lenout * sizeof(short) );
1a565432 304
de6d9b64
FB
305 if (s->input_channels == 2 &&
306 s->output_channels == 1) {
de6d9b64
FB
307 buftmp3[0] = output;
308 stereo_to_mono(buftmp2[0], input, nb_samples);
743739d2 309 } else if (s->output_channels >= 2 && s->input_channels == 1) {
de6d9b64 310 buftmp3[0] = bufout[0];
aaaf1635 311 memcpy(buftmp2[0], input, nb_samples*sizeof(short));
743739d2 312 } else if (s->output_channels >= 2) {
de6d9b64
FB
313 buftmp3[0] = bufout[0];
314 buftmp3[1] = bufout[1];
315 stereo_split(buftmp2[0], buftmp2[1], input, nb_samples);
316 } else {
de6d9b64 317 buftmp3[0] = output;
aaaf1635 318 memcpy(buftmp2[0], input, nb_samples*sizeof(short));
de6d9b64
FB
319 }
320
aaaf1635
MN
321 nb_samples += s->temp_len;
322
de6d9b64
FB
323 /* resample each channel */
324 nb_samples1 = 0; /* avoid warning */
325 for(i=0;i<s->filter_channels;i++) {
aaaf1635
MN
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], &consumed, nb_samples, lenout, is_last);
330 s->temp_len= nb_samples - consumed;
331 s->temp[i]= av_realloc(s->temp[i], s->temp_len*sizeof(short));
332 memcpy(s->temp[i], bufin[i] + consumed, s->temp_len*sizeof(short));
de6d9b64
FB
333 }
334
335 if (s->output_channels == 2 && s->input_channels == 1) {
336 mono_to_stereo(output, buftmp3[0], nb_samples1);
337 } else if (s->output_channels == 2) {
338 stereo_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
743739d2
MN
339 } else if (s->output_channels == 6) {
340 ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
de6d9b64
FB
341 }
342
d1e3c6fd
BC
343 if (s->sample_fmt[1] != SAMPLE_FMT_S16) {
344 int istride[1] = { 2 };
345 int ostride[1] = { s->sample_size[1] };
346 const void *ibuf[1] = { output };
347 void *obuf[1] = { output_bak };
348
349 if (av_audio_convert(s->convert_ctx[1], obuf, ostride,
350 ibuf, istride, nb_samples1*s->output_channels) < 0) {
89bc05d1 351 av_log(s->resample_context, AV_LOG_ERROR, "Audio sample format convertion failed\n");
d1e3c6fd
BC
352 return 0;
353 }
354 }
355
dca97cbe
MN
356 for(i=0; i<s->filter_channels; i++)
357 av_free(bufin[i]);
1a565432 358
6000abfa
FB
359 av_free(bufout[0]);
360 av_free(bufout[1]);
de6d9b64
FB
361 return nb_samples1;
362}
363
364void audio_resample_close(ReSampleContext *s)
365{
aaaf1635
MN
366 av_resample_close(s->resample_context);
367 av_freep(&s->temp[0]);
368 av_freep(&s->temp[1]);
d1e3c6fd
BC
369 av_freep(&s->buffer[0]);
370 av_freep(&s->buffer[1]);
371 av_audio_convert_free(s->convert_ctx[0]);
372 av_audio_convert_free(s->convert_ctx[1]);
6000abfa 373 av_free(s);
de6d9b64 374}