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
28
29
30 /* double template */
31 #define CONFIG_RESAMPLE_DBL
33 #undef CONFIG_RESAMPLE_DBL
34
35 /* float template */
36 #define CONFIG_RESAMPLE_FLT
38 #undef CONFIG_RESAMPLE_FLT
39
40 /* s32 template */
41 #define CONFIG_RESAMPLE_S32
43 #undef CONFIG_RESAMPLE_S32
44
45 /* s16 template */
47
48
49 /* 0th order modified bessel function of the first kind. */
51 {
53 double lastv = 0;
54 double t = 1;
55 int i;
56
57 x = x * x / 4;
58 for (i = 1; v != lastv; i++) {
60 t *= x / (i * i);
61 v += t;
62 }
64 }
65
66 /* Build a polyphase filterbank. */
68 {
69 int ph, i;
74 const int center = (tap_count - 1) / 2;
75
76 tab =
av_malloc(tap_count *
sizeof(*tab));
77 if (!tab)
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;
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 }
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;
101 w = 2.0 * x / (factor * tap_count *
M_PI);
103 break;
104 }
105
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
114 }
115
117 return 0;
118 }
119
121 {
127 int felem_size;
128
134 "resampling: %s\n",
137 }
139 if (!c)
141
149
155 break;
160 break;
165 break;
170 break;
171 }
172
173 if (ARCH_AARCH64)
175 if (ARCH_ARM)
177
181 goto error;
182
184 goto error;
185
190
193 in_rate * (int64_t)phase_count, INT32_MAX / 2))
194 goto error;
196
201
202 /* allocate internal buffer */
205 "resample buffer");
207 goto error;
210
214
216
217 error:
222 }
223
225 {
226 if (!*c)
227 return;
231 }
232
235 {
239
240 if (compensation_distance < 0)
242 if (!compensation_distance && sample_delta)
244
246 #if FF_API_RESAMPLE_CLOSE_OPEN
247 /* if resampling was not enabled previously, re-initialize the
248 AVAudioResampleContext and force resampling */
249 int fifo_samples;
250 int restore_matrix = 0;
252
253 /* buffer any remaining samples in the output FIFO before closing */
255 if (fifo_samples > 0) {
258 if (!fifo_buf)
261 fifo_samples);
262 if (ret < 0)
263 goto reinit_fail;
264 }
265 /* save the channel mixing matrix */
268 if (ret < 0)
269 goto reinit_fail;
270 restore_matrix = 1;
271 }
272
273 /* close the AVAudioResampleContext */
275
277
278 /* restore the channel mixing matrix */
279 if (restore_matrix) {
281 if (ret < 0)
282 goto reinit_fail;
283 }
284
285 /* re-open the AVAudioResampleContext */
287 if (ret < 0)
288 goto reinit_fail;
289
290 /* restore buffered samples to the output FIFO */
291 if (fifo_samples > 0) {
293 fifo_samples);
294 if (ret < 0)
295 goto reinit_fail;
297 }
298 #else
301 #endif
302 }
305 if (compensation_distance) {
307 (int64_t)sample_delta / compensation_distance;
308 } else {
310 }
311 return 0;
312
313 reinit_fail:
316 }
317
319 int *consumed, int src_size, int dst_size, int update_ctx,
320 int nearest_neighbour)
321 {
322 int dst_index;
328
329 if (!dst != !src)
331
332 if (nearest_neighbour) {
333 uint64_t index2 = ((uint64_t)index) << 32;
335 dst_size =
FFMIN(dst_size,
336 (src_size-1-index) * (int64_t)c->
src_incr /
338
339 if (dst) {
340 for(dst_index = 0; dst_index < dst_size; dst_index++) {
342 index2 += incr;
343 }
344 } else {
345 dst_index = dst_size;
346 }
348 index += (frac + dst_index * (int64_t)dst_incr_frac) / c->
src_incr;
349 frac = (frac + dst_index * (int64_t)dst_incr_frac) % c->
src_incr;
350 } else {
351 for (dst_index = 0; dst_index < dst_size; dst_index++) {
353
355 break;
356
357 if (dst)
359
360 frac += dst_incr_frac;
364 index++;
365 }
366 if (dst_index + 1 == compensation_distance) {
367 compensation_distance = 0;
370 }
371 }
372 }
373 if (consumed)
375
376 if (update_ctx) {
378
379 if (compensation_distance) {
380 compensation_distance -= dst_index;
381 if (compensation_distance <= 0)
383 }
388 }
389
390 return dst_index;
391 }
392
394 {
395 int ch, in_samples, in_leftover, consumed = 0, out_samples = 0;
400
403
404 /* add input samples to the internal buffer */
405 if (src) {
407 if (ret < 0)
409 } else if (in_leftover <= c->final_padding_samples) {
410 /* no remaining samples to flush */
411 return 0;
412 }
413
416 int i;
417
419 return 0;
420
426 } else {
427 memset(c->
buffer->
data[ch] + bps * i, 0, bps);
428 }
429 }
431 }
432
435 int i;
436
438 if (ret < 0) {
441 }
442
445 if (in_leftover > i) {
446 memcpy(c->
buffer->
data[ch] + bps * (in_leftover + i),
447 c->
buffer->
data[ch] + bps * (in_leftover - i - 1),
448 bps);
449 } else {
450 memset(c->
buffer->
data[ch] + bps * (in_leftover + i),
451 0, bps);
452 }
453 }
457 }
458
459
460 /* calculate output size and reallocate output buffer if needed */
461 /* TODO: try to calculate this without the dummy resample() run */
464 INT_MAX, 0, nearest_neighbour);
466 if (ret < 0) {
469 }
470 }
471
472 /* resample each channel plane */
478 }
479 if (out_samples < 0) {
481 return out_samples;
482 }
483
484 /* drain consumed samples from the internal buffer */
487
488 av_dlog(c->
avr,
"resampled %d in + %d leftover to %d out + %d leftover\n",
490
492 return 0;
493 }
494
496 {
498
500 return 0;
501
503 }