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