Commit | Line | Data |
---|---|---|
2ba45a60 DM |
1 | /* |
2 | * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at> | |
3 | * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com> | |
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 | #include "libavutil/common.h" | |
23 | #include "libavutil/libm.h" | |
24 | #include "libavutil/log.h" | |
25 | #include "internal.h" | |
26 | #include "resample.h" | |
27 | #include "audio_data.h" | |
28 | ||
29 | ||
30 | /* double template */ | |
31 | #define CONFIG_RESAMPLE_DBL | |
32 | #include "resample_template.c" | |
33 | #undef CONFIG_RESAMPLE_DBL | |
34 | ||
35 | /* float template */ | |
36 | #define CONFIG_RESAMPLE_FLT | |
37 | #include "resample_template.c" | |
38 | #undef CONFIG_RESAMPLE_FLT | |
39 | ||
40 | /* s32 template */ | |
41 | #define CONFIG_RESAMPLE_S32 | |
42 | #include "resample_template.c" | |
43 | #undef CONFIG_RESAMPLE_S32 | |
44 | ||
45 | /* s16 template */ | |
46 | #include "resample_template.c" | |
47 | ||
48 | ||
49 | /* 0th order modified bessel function of the first kind. */ | |
50 | static double bessel(double x) | |
51 | { | |
52 | double v = 1; | |
53 | double lastv = 0; | |
54 | double t = 1; | |
55 | int i; | |
56 | ||
57 | x = x * x / 4; | |
58 | for (i = 1; v != lastv; i++) { | |
59 | lastv = v; | |
60 | t *= x / (i * i); | |
61 | v += t; | |
62 | } | |
63 | return v; | |
64 | } | |
65 | ||
66 | /* Build a polyphase filterbank. */ | |
67 | static int build_filter(ResampleContext *c, double factor) | |
68 | { | |
69 | int ph, i; | |
70 | double x, y, w; | |
71 | double *tab; | |
72 | int tap_count = c->filter_length; | |
73 | int phase_count = 1 << c->phase_shift; | |
74 | const int center = (tap_count - 1) / 2; | |
75 | ||
76 | tab = av_malloc(tap_count * sizeof(*tab)); | |
77 | if (!tab) | |
78 | return AVERROR(ENOMEM); | |
79 | ||
80 | for (ph = 0; ph < phase_count; ph++) { | |
81 | double norm = 0; | |
82 | for (i = 0; i < tap_count; i++) { | |
83 | x = M_PI * ((double)(i - center) - (double)ph / phase_count) * factor; | |
84 | if (x == 0) y = 1.0; | |
85 | else y = sin(x) / x; | |
86 | switch (c->filter_type) { | |
87 | case AV_RESAMPLE_FILTER_TYPE_CUBIC: { | |
88 | const float d = -0.5; //first order derivative = -0.5 | |
89 | x = fabs(((double)(i - center) - (double)ph / phase_count) * factor); | |
90 | if (x < 1.0) y = 1 - 3 * x*x + 2 * x*x*x + d * ( -x*x + x*x*x); | |
91 | else y = d * (-4 + 8 * x - 5 * x*x + x*x*x); | |
92 | break; | |
93 | } | |
94 | case AV_RESAMPLE_FILTER_TYPE_BLACKMAN_NUTTALL: | |
95 | w = 2.0 * x / (factor * tap_count) + M_PI; | |
96 | y *= 0.3635819 - 0.4891775 * cos( w) + | |
97 | 0.1365995 * cos(2 * w) - | |
98 | 0.0106411 * cos(3 * w); | |
99 | break; | |
100 | case AV_RESAMPLE_FILTER_TYPE_KAISER: | |
101 | w = 2.0 * x / (factor * tap_count * M_PI); | |
102 | y *= bessel(c->kaiser_beta * sqrt(FFMAX(1 - w * w, 0))); | |
103 | break; | |
104 | } | |
105 | ||
106 | tab[i] = y; | |
107 | norm += y; | |
108 | } | |
109 | /* normalize so that an uniform color remains the same */ | |
110 | for (i = 0; i < tap_count; i++) | |
111 | tab[i] = tab[i] / norm; | |
112 | ||
113 | c->set_filter(c->filter_bank, tab, ph, tap_count); | |
114 | } | |
115 | ||
116 | av_free(tab); | |
117 | return 0; | |
118 | } | |
119 | ||
120 | ResampleContext *ff_audio_resample_init(AVAudioResampleContext *avr) | |
121 | { | |
122 | ResampleContext *c; | |
123 | int out_rate = avr->out_sample_rate; | |
124 | int in_rate = avr->in_sample_rate; | |
125 | double factor = FFMIN(out_rate * avr->cutoff / in_rate, 1.0); | |
126 | int phase_count = 1 << avr->phase_shift; | |
127 | int felem_size; | |
128 | ||
129 | if (avr->internal_sample_fmt != AV_SAMPLE_FMT_S16P && | |
130 | avr->internal_sample_fmt != AV_SAMPLE_FMT_S32P && | |
131 | avr->internal_sample_fmt != AV_SAMPLE_FMT_FLTP && | |
132 | avr->internal_sample_fmt != AV_SAMPLE_FMT_DBLP) { | |
133 | av_log(avr, AV_LOG_ERROR, "Unsupported internal format for " | |
134 | "resampling: %s\n", | |
135 | av_get_sample_fmt_name(avr->internal_sample_fmt)); | |
136 | return NULL; | |
137 | } | |
138 | c = av_mallocz(sizeof(*c)); | |
139 | if (!c) | |
140 | return NULL; | |
141 | ||
142 | c->avr = avr; | |
143 | c->phase_shift = avr->phase_shift; | |
144 | c->phase_mask = phase_count - 1; | |
145 | c->linear = avr->linear_interp; | |
146 | c->filter_length = FFMAX((int)ceil(avr->filter_size / factor), 1); | |
147 | c->filter_type = avr->filter_type; | |
148 | c->kaiser_beta = avr->kaiser_beta; | |
149 | ||
150 | switch (avr->internal_sample_fmt) { | |
151 | case AV_SAMPLE_FMT_DBLP: | |
152 | c->resample_one = c->linear ? resample_linear_dbl : resample_one_dbl; | |
153 | c->resample_nearest = resample_nearest_dbl; | |
154 | c->set_filter = set_filter_dbl; | |
155 | break; | |
156 | case AV_SAMPLE_FMT_FLTP: | |
157 | c->resample_one = c->linear ? resample_linear_flt : resample_one_flt; | |
158 | c->resample_nearest = resample_nearest_flt; | |
159 | c->set_filter = set_filter_flt; | |
160 | break; | |
161 | case AV_SAMPLE_FMT_S32P: | |
162 | c->resample_one = c->linear ? resample_linear_s32 : resample_one_s32; | |
163 | c->resample_nearest = resample_nearest_s32; | |
164 | c->set_filter = set_filter_s32; | |
165 | break; | |
166 | case AV_SAMPLE_FMT_S16P: | |
167 | c->resample_one = c->linear ? resample_linear_s16 : resample_one_s16; | |
168 | c->resample_nearest = resample_nearest_s16; | |
169 | c->set_filter = set_filter_s16; | |
170 | break; | |
171 | } | |
172 | ||
173 | if (ARCH_AARCH64) | |
174 | ff_audio_resample_init_aarch64(c, avr->internal_sample_fmt); | |
175 | ||
176 | felem_size = av_get_bytes_per_sample(avr->internal_sample_fmt); | |
177 | c->filter_bank = av_mallocz(c->filter_length * (phase_count + 1) * felem_size); | |
178 | if (!c->filter_bank) | |
179 | goto error; | |
180 | ||
181 | if (build_filter(c, factor) < 0) | |
182 | goto error; | |
183 | ||
184 | memcpy(&c->filter_bank[(c->filter_length * phase_count + 1) * felem_size], | |
185 | c->filter_bank, (c->filter_length - 1) * felem_size); | |
186 | memcpy(&c->filter_bank[c->filter_length * phase_count * felem_size], | |
187 | &c->filter_bank[(c->filter_length - 1) * felem_size], felem_size); | |
188 | ||
189 | c->compensation_distance = 0; | |
190 | if (!av_reduce(&c->src_incr, &c->dst_incr, out_rate, | |
191 | in_rate * (int64_t)phase_count, INT32_MAX / 2)) | |
192 | goto error; | |
193 | c->ideal_dst_incr = c->dst_incr; | |
194 | ||
195 | c->padding_size = (c->filter_length - 1) / 2; | |
196 | c->initial_padding_filled = 0; | |
197 | c->index = 0; | |
198 | c->frac = 0; | |
199 | ||
200 | /* allocate internal buffer */ | |
201 | c->buffer = ff_audio_data_alloc(avr->resample_channels, c->padding_size, | |
202 | avr->internal_sample_fmt, | |
203 | "resample buffer"); | |
204 | if (!c->buffer) | |
205 | goto error; | |
206 | c->buffer->nb_samples = c->padding_size; | |
207 | c->initial_padding_samples = c->padding_size; | |
208 | ||
209 | av_log(avr, AV_LOG_DEBUG, "resample: %s from %d Hz to %d Hz\n", | |
210 | av_get_sample_fmt_name(avr->internal_sample_fmt), | |
211 | avr->in_sample_rate, avr->out_sample_rate); | |
212 | ||
213 | return c; | |
214 | ||
215 | error: | |
216 | ff_audio_data_free(&c->buffer); | |
217 | av_free(c->filter_bank); | |
218 | av_free(c); | |
219 | return NULL; | |
220 | } | |
221 | ||
222 | void ff_audio_resample_free(ResampleContext **c) | |
223 | { | |
224 | if (!*c) | |
225 | return; | |
226 | ff_audio_data_free(&(*c)->buffer); | |
227 | av_free((*c)->filter_bank); | |
228 | av_freep(c); | |
229 | } | |
230 | ||
231 | int avresample_set_compensation(AVAudioResampleContext *avr, int sample_delta, | |
232 | int compensation_distance) | |
233 | { | |
234 | ResampleContext *c; | |
235 | AudioData *fifo_buf = NULL; | |
236 | int ret = 0; | |
237 | ||
238 | if (compensation_distance < 0) | |
239 | return AVERROR(EINVAL); | |
240 | if (!compensation_distance && sample_delta) | |
241 | return AVERROR(EINVAL); | |
242 | ||
243 | if (!avr->resample_needed) { | |
244 | #if FF_API_RESAMPLE_CLOSE_OPEN | |
245 | /* if resampling was not enabled previously, re-initialize the | |
246 | AVAudioResampleContext and force resampling */ | |
247 | int fifo_samples; | |
248 | int restore_matrix = 0; | |
249 | double matrix[AVRESAMPLE_MAX_CHANNELS * AVRESAMPLE_MAX_CHANNELS] = { 0 }; | |
250 | ||
251 | /* buffer any remaining samples in the output FIFO before closing */ | |
252 | fifo_samples = av_audio_fifo_size(avr->out_fifo); | |
253 | if (fifo_samples > 0) { | |
254 | fifo_buf = ff_audio_data_alloc(avr->out_channels, fifo_samples, | |
255 | avr->out_sample_fmt, NULL); | |
256 | if (!fifo_buf) | |
257 | return AVERROR(EINVAL); | |
258 | ret = ff_audio_data_read_from_fifo(avr->out_fifo, fifo_buf, | |
259 | fifo_samples); | |
260 | if (ret < 0) | |
261 | goto reinit_fail; | |
262 | } | |
263 | /* save the channel mixing matrix */ | |
264 | if (avr->am) { | |
265 | ret = avresample_get_matrix(avr, matrix, AVRESAMPLE_MAX_CHANNELS); | |
266 | if (ret < 0) | |
267 | goto reinit_fail; | |
268 | restore_matrix = 1; | |
269 | } | |
270 | ||
271 | /* close the AVAudioResampleContext */ | |
272 | avresample_close(avr); | |
273 | ||
274 | avr->force_resampling = 1; | |
275 | ||
276 | /* restore the channel mixing matrix */ | |
277 | if (restore_matrix) { | |
278 | ret = avresample_set_matrix(avr, matrix, AVRESAMPLE_MAX_CHANNELS); | |
279 | if (ret < 0) | |
280 | goto reinit_fail; | |
281 | } | |
282 | ||
283 | /* re-open the AVAudioResampleContext */ | |
284 | ret = avresample_open(avr); | |
285 | if (ret < 0) | |
286 | goto reinit_fail; | |
287 | ||
288 | /* restore buffered samples to the output FIFO */ | |
289 | if (fifo_samples > 0) { | |
290 | ret = ff_audio_data_add_to_fifo(avr->out_fifo, fifo_buf, 0, | |
291 | fifo_samples); | |
292 | if (ret < 0) | |
293 | goto reinit_fail; | |
294 | ff_audio_data_free(&fifo_buf); | |
295 | } | |
296 | #else | |
297 | av_log(avr, AV_LOG_ERROR, "Unable to set resampling compensation\n"); | |
298 | return AVERROR(EINVAL); | |
299 | #endif | |
300 | } | |
301 | c = avr->resample; | |
302 | c->compensation_distance = compensation_distance; | |
303 | if (compensation_distance) { | |
304 | c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr * | |
305 | (int64_t)sample_delta / compensation_distance; | |
306 | } else { | |
307 | c->dst_incr = c->ideal_dst_incr; | |
308 | } | |
309 | return 0; | |
310 | ||
311 | reinit_fail: | |
312 | ff_audio_data_free(&fifo_buf); | |
313 | return ret; | |
314 | } | |
315 | ||
316 | static int resample(ResampleContext *c, void *dst, const void *src, | |
317 | int *consumed, int src_size, int dst_size, int update_ctx, | |
318 | int nearest_neighbour) | |
319 | { | |
320 | int dst_index; | |
321 | unsigned int index = c->index; | |
322 | int frac = c->frac; | |
323 | int dst_incr_frac = c->dst_incr % c->src_incr; | |
324 | int dst_incr = c->dst_incr / c->src_incr; | |
325 | int compensation_distance = c->compensation_distance; | |
326 | ||
327 | if (!dst != !src) | |
328 | return AVERROR(EINVAL); | |
329 | ||
330 | if (nearest_neighbour) { | |
331 | uint64_t index2 = ((uint64_t)index) << 32; | |
332 | int64_t incr = (1LL << 32) * c->dst_incr / c->src_incr; | |
333 | dst_size = FFMIN(dst_size, | |
334 | (src_size-1-index) * (int64_t)c->src_incr / | |
335 | c->dst_incr); | |
336 | ||
337 | if (dst) { | |
338 | for(dst_index = 0; dst_index < dst_size; dst_index++) { | |
339 | c->resample_nearest(dst, dst_index, src, index2 >> 32); | |
340 | index2 += incr; | |
341 | } | |
342 | } else { | |
343 | dst_index = dst_size; | |
344 | } | |
345 | index += dst_index * dst_incr; | |
346 | index += (frac + dst_index * (int64_t)dst_incr_frac) / c->src_incr; | |
347 | frac = (frac + dst_index * (int64_t)dst_incr_frac) % c->src_incr; | |
348 | } else { | |
349 | for (dst_index = 0; dst_index < dst_size; dst_index++) { | |
350 | int sample_index = index >> c->phase_shift; | |
351 | ||
352 | if (sample_index + c->filter_length > src_size) | |
353 | break; | |
354 | ||
355 | if (dst) | |
356 | c->resample_one(c, dst, dst_index, src, index, frac); | |
357 | ||
358 | frac += dst_incr_frac; | |
359 | index += dst_incr; | |
360 | if (frac >= c->src_incr) { | |
361 | frac -= c->src_incr; | |
362 | index++; | |
363 | } | |
364 | if (dst_index + 1 == compensation_distance) { | |
365 | compensation_distance = 0; | |
366 | dst_incr_frac = c->ideal_dst_incr % c->src_incr; | |
367 | dst_incr = c->ideal_dst_incr / c->src_incr; | |
368 | } | |
369 | } | |
370 | } | |
371 | if (consumed) | |
372 | *consumed = index >> c->phase_shift; | |
373 | ||
374 | if (update_ctx) { | |
375 | index &= c->phase_mask; | |
376 | ||
377 | if (compensation_distance) { | |
378 | compensation_distance -= dst_index; | |
379 | if (compensation_distance <= 0) | |
380 | return AVERROR_BUG; | |
381 | } | |
382 | c->frac = frac; | |
383 | c->index = index; | |
384 | c->dst_incr = dst_incr_frac + c->src_incr*dst_incr; | |
385 | c->compensation_distance = compensation_distance; | |
386 | } | |
387 | ||
388 | return dst_index; | |
389 | } | |
390 | ||
391 | int ff_audio_resample(ResampleContext *c, AudioData *dst, AudioData *src) | |
392 | { | |
393 | int ch, in_samples, in_leftover, consumed = 0, out_samples = 0; | |
394 | int ret = AVERROR(EINVAL); | |
395 | int nearest_neighbour = (c->compensation_distance == 0 && | |
396 | c->filter_length == 1 && | |
397 | c->phase_shift == 0); | |
398 | ||
399 | in_samples = src ? src->nb_samples : 0; | |
400 | in_leftover = c->buffer->nb_samples; | |
401 | ||
402 | /* add input samples to the internal buffer */ | |
403 | if (src) { | |
404 | ret = ff_audio_data_combine(c->buffer, in_leftover, src, 0, in_samples); | |
405 | if (ret < 0) | |
406 | return ret; | |
407 | } else if (in_leftover <= c->final_padding_samples) { | |
408 | /* no remaining samples to flush */ | |
409 | return 0; | |
410 | } | |
411 | ||
412 | if (!c->initial_padding_filled) { | |
413 | int bps = av_get_bytes_per_sample(c->avr->internal_sample_fmt); | |
414 | int i; | |
415 | ||
416 | if (src && c->buffer->nb_samples < 2 * c->padding_size) | |
417 | return 0; | |
418 | ||
419 | for (i = 0; i < c->padding_size; i++) | |
420 | for (ch = 0; ch < c->buffer->channels; ch++) { | |
421 | if (c->buffer->nb_samples > 2 * c->padding_size - i) { | |
422 | memcpy(c->buffer->data[ch] + bps * i, | |
423 | c->buffer->data[ch] + bps * (2 * c->padding_size - i), bps); | |
424 | } else { | |
425 | memset(c->buffer->data[ch] + bps * i, 0, bps); | |
426 | } | |
427 | } | |
428 | c->initial_padding_filled = 1; | |
429 | } | |
430 | ||
431 | if (!src && !c->final_padding_filled) { | |
432 | int bps = av_get_bytes_per_sample(c->avr->internal_sample_fmt); | |
433 | int i; | |
434 | ||
435 | ret = ff_audio_data_realloc(c->buffer, in_samples + c->padding_size); | |
436 | if (ret < 0) { | |
437 | av_log(c->avr, AV_LOG_ERROR, "Error reallocating resampling buffer\n"); | |
438 | return AVERROR(ENOMEM); | |
439 | } | |
440 | ||
441 | for (i = 0; i < c->padding_size; i++) | |
442 | for (ch = 0; ch < c->buffer->channels; ch++) { | |
443 | if (in_leftover > i) { | |
444 | memcpy(c->buffer->data[ch] + bps * (in_leftover + i), | |
445 | c->buffer->data[ch] + bps * (in_leftover - i - 1), | |
446 | bps); | |
447 | } else { | |
448 | memset(c->buffer->data[ch] + bps * (in_leftover + i), | |
449 | 0, bps); | |
450 | } | |
451 | } | |
452 | c->buffer->nb_samples += c->padding_size; | |
453 | c->final_padding_samples = c->padding_size; | |
454 | c->final_padding_filled = 1; | |
455 | } | |
456 | ||
457 | ||
458 | /* calculate output size and reallocate output buffer if needed */ | |
459 | /* TODO: try to calculate this without the dummy resample() run */ | |
460 | if (!dst->read_only && dst->allow_realloc) { | |
461 | out_samples = resample(c, NULL, NULL, NULL, c->buffer->nb_samples, | |
462 | INT_MAX, 0, nearest_neighbour); | |
463 | ret = ff_audio_data_realloc(dst, out_samples); | |
464 | if (ret < 0) { | |
465 | av_log(c->avr, AV_LOG_ERROR, "error reallocating output\n"); | |
466 | return ret; | |
467 | } | |
468 | } | |
469 | ||
470 | /* resample each channel plane */ | |
471 | for (ch = 0; ch < c->buffer->channels; ch++) { | |
472 | out_samples = resample(c, (void *)dst->data[ch], | |
473 | (const void *)c->buffer->data[ch], &consumed, | |
474 | c->buffer->nb_samples, dst->allocated_samples, | |
475 | ch + 1 == c->buffer->channels, nearest_neighbour); | |
476 | } | |
477 | if (out_samples < 0) { | |
478 | av_log(c->avr, AV_LOG_ERROR, "error during resampling\n"); | |
479 | return out_samples; | |
480 | } | |
481 | ||
482 | /* drain consumed samples from the internal buffer */ | |
483 | ff_audio_data_drain(c->buffer, consumed); | |
484 | c->initial_padding_samples = FFMAX(c->initial_padding_samples - consumed, 0); | |
485 | ||
486 | av_dlog(c->avr, "resampled %d in + %d leftover to %d out + %d leftover\n", | |
487 | in_samples, in_leftover, out_samples, c->buffer->nb_samples); | |
488 | ||
489 | dst->nb_samples = out_samples; | |
490 | return 0; | |
491 | } | |
492 | ||
493 | int avresample_get_delay(AVAudioResampleContext *avr) | |
494 | { | |
495 | ResampleContext *c = avr->resample; | |
496 | ||
497 | if (!avr->resample_needed || !avr->resample) | |
498 | return 0; | |
499 | ||
500 | return FFMAX(c->buffer->nb_samples - c->padding_size, 0); | |
501 | } |