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
48 int dst_index, const void *src0, int src_size,
50 };
51
52
53 /* double template */
54 #define CONFIG_RESAMPLE_DBL
56 #undef CONFIG_RESAMPLE_DBL
57
58 /* float template */
59 #define CONFIG_RESAMPLE_FLT
61 #undef CONFIG_RESAMPLE_FLT
62
63 /* s32 template */
64 #define CONFIG_RESAMPLE_S32
66 #undef CONFIG_RESAMPLE_S32
67
68 /* s16 template */
70
71
72 /* 0th order modified bessel function of the first kind. */
74 {
76 double lastv = 0;
78 int i;
79
80 x = x * x / 4;
81 for (i = 1; v != lastv; i++) {
83 t *= x / (i * i);
85 }
87 }
88
89 /* Build a polyphase filterbank. */
91 {
92 int ph, i;
97 const int center = (tap_count - 1) / 2;
98
99 tab =
av_malloc(tap_count *
sizeof(*tab));
100 if (!tab)
102
103 /* if upsampling, only need to interpolate, no filter */
105
106 for (ph = 0; ph < phase_count; ph++) {
107 double norm = 0;
108 for (i = 0; i < tap_count; i++) {
109 x =
M_PI * ((double)(i - center) - (double)ph / phase_count) *
factor;
110 if (x == 0) y = 1.0;
111 else y = sin(x) / x;
114 const float d = -0.5; //first order derivative = -0.5
115 x = fabs(((double)(i - center) - (double)ph / phase_count) * factor);
116 if (x < 1.0) y = 1 - 3 * x*x + 2 * x*x*x + d * ( -x*x + x*x*x);
117 else y = d * (-4 + 8 * x - 5 * x*x + x*x*x);
118 break;
119 }
121 w = 2.0 * x / (factor * tap_count) +
M_PI;
122 y *= 0.3635819 - 0.4891775 * cos( w) +
123 0.1365995 * cos(2 * w) -
124 0.0106411 * cos(3 * w);
125 break;
127 w = 2.0 * x / (factor * tap_count *
M_PI);
129 break;
130 }
131
134 }
135 /* normalize so that an uniform color remains the same */
136 for (i = 0; i < tap_count; i++)
137 tab[i] = tab[i] / norm;
138
140 }
141
143 return 0;
144 }
145
147 {
154
160 "resampling: %s\n",
162 return NULL;
163 }
165 if (!c)
166 return NULL;
167
176
181 break;
185 break;
189 break;
193 break;
194 }
195
199 goto error;
200
202 goto error;
203
208
211 in_rate * (int64_t)phase_count, INT32_MAX / 2))
212 goto error;
214
217
218 /* allocate internal buffer */
221 "resample buffer");
223 goto error;
224
228
230
231 error:
235 return NULL;
236 }
237
239 {
240 if (!*c)
241 return;
245 }
246
249 {
253
254 if (compensation_distance < 0)
256 if (!compensation_distance && sample_delta)
258
260 #if FF_API_RESAMPLE_CLOSE_OPEN
261 /* if resampling was not enabled previously, re-initialize the
262 AVAudioResampleContext and force resampling */
263 int fifo_samples;
264 int restore_matrix = 0;
266
267 /* buffer any remaining samples in the output FIFO before closing */
269 if (fifo_samples > 0) {
272 if (!fifo_buf)
275 fifo_samples);
276 if (ret < 0)
277 goto reinit_fail;
278 }
279 /* save the channel mixing matrix */
282 if (ret < 0)
283 goto reinit_fail;
284 restore_matrix = 1;
285 }
286
287 /* close the AVAudioResampleContext */
289
291
292 /* restore the channel mixing matrix */
293 if (restore_matrix) {
295 if (ret < 0)
296 goto reinit_fail;
297 }
298
299 /* re-open the AVAudioResampleContext */
301 if (ret < 0)
302 goto reinit_fail;
303
304 /* restore buffered samples to the output FIFO */
305 if (fifo_samples > 0) {
307 fifo_samples);
308 if (ret < 0)
309 goto reinit_fail;
311 }
312 #else
315 #endif
316 }
319 if (compensation_distance) {
321 (int64_t)sample_delta / compensation_distance;
322 } else {
324 }
325 return 0;
326
327 reinit_fail:
330 }
331
333 int *consumed, int src_size, int dst_size, int update_ctx)
334 {
335 int dst_index;
341
342 if (!dst != !src)
344
347 int64_t index2 = ((int64_t)index) << 32;
349 dst_size =
FFMIN(dst_size,
350 (src_size-1-index) * (int64_t)c->
src_incr /
352
353 if (dst) {
354 for(dst_index = 0; dst_index < dst_size; dst_index++) {
355 c->
resample_one(c, 1, dst, dst_index, src, 0, index2 >> 32, 0);
356 index2 += incr;
357 }
358 } else {
359 dst_index = dst_size;
360 }
362 index += (frac + dst_index * (int64_t)dst_incr_frac) / c->
src_incr;
363 frac = (frac + dst_index * (int64_t)dst_incr_frac) % c->
src_incr;
364 } else {
365 for (dst_index = 0; dst_index < dst_size; dst_index++) {
367
369 -sample_index >= src_size)
370 break;
371
372 if (dst)
373 c->
resample_one(c, 0, dst, dst_index, src, src_size, index, frac);
374
375 frac += dst_incr_frac;
379 index++;
380 }
381 if (dst_index + 1 == compensation_distance) {
382 compensation_distance = 0;
385 }
386 }
387 }
388 if (consumed)
390
391 if (update_ctx) {
392 if (index >= 0)
394
395 if (compensation_distance) {
396 compensation_distance -= dst_index;
397 if (compensation_distance <= 0)
399 }
404 }
405
406 return dst_index;
407 }
408
410 {
411 int ch, in_samples, in_leftover, consumed = 0, out_samples = 0;
413
416
417 /* add input samples to the internal buffer */
418 if (src) {
420 if (ret < 0)
422 } else if (!in_leftover) {
423 /* no remaining samples to flush */
424 return 0;
425 } else {
426 /* TODO: pad buffer to flush completely */
427 }
428
429 /* calculate output size and reallocate output buffer if needed */
430 /* TODO: try to calculate this without the dummy resample() run */
433 INT_MAX, 0);
435 if (ret < 0) {
438 }
439 }
440
441 /* resample each channel plane */
447 }
448 if (out_samples < 0) {
450 return out_samples;
451 }
452
453 /* drain consumed samples from the internal buffer */
455
456 av_dlog(c->
avr,
"resampled %d in + %d leftover to %d out + %d leftover\n",
458
460 return 0;
461 }
462
464 {
466 return 0;
467
469 }