/src/ffmpeg/libswresample/resample.c
Line | Count | Source |
1 | | /* |
2 | | * audio resampling |
3 | | * Copyright (c) 2004-2012 Michael Niedermayer <michaelni@gmx.at> |
4 | | * bessel function: Copyright (c) 2006 Xiaogang Zhang |
5 | | * |
6 | | * This file is part of FFmpeg. |
7 | | * |
8 | | * FFmpeg is free software; you can redistribute it and/or |
9 | | * modify it under the terms of the GNU Lesser General Public |
10 | | * License as published by the Free Software Foundation; either |
11 | | * version 2.1 of the License, or (at your option) any later version. |
12 | | * |
13 | | * FFmpeg is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | | * Lesser General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public |
19 | | * License along with FFmpeg; if not, write to the Free Software |
20 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 | | */ |
22 | | |
23 | | /** |
24 | | * @file |
25 | | * audio resampling |
26 | | * @author Michael Niedermayer <michaelni@gmx.at> |
27 | | */ |
28 | | |
29 | | #include "libavutil/avassert.h" |
30 | | #include "libavutil/mem.h" |
31 | | #include "resample.h" |
32 | | |
33 | | /** |
34 | | * builds a polyphase filterbank. |
35 | | * @param factor resampling factor |
36 | | * @param scale wanted sum of coefficients for each filter |
37 | | * @param filter_type filter type |
38 | | * @param kaiser_beta kaiser window beta |
39 | | * @return 0 on success, negative on error |
40 | | */ |
41 | | static int build_filter(ResampleContext *c, void *filter, double factor, int tap_count, int alloc, int phase_count, int scale, |
42 | 33.8k | int filter_type, double kaiser_beta){ |
43 | 33.8k | int ph, i; |
44 | 33.8k | int ph_nb = phase_count % 2 ? phase_count : phase_count / 2 + 1; |
45 | 33.8k | double x, y, w, t, s; |
46 | 33.8k | double *tab = av_malloc_array(tap_count+1, sizeof(*tab)); |
47 | 33.8k | double *sin_lut = av_malloc_array(ph_nb, sizeof(*sin_lut)); |
48 | 33.8k | const int center= (tap_count-1)/2; |
49 | 33.8k | double norm = 0; |
50 | 33.8k | int ret = AVERROR(ENOMEM); |
51 | | |
52 | 33.8k | if (!tab || !sin_lut) |
53 | 0 | goto fail; |
54 | | |
55 | 33.8k | av_assert0(tap_count == 1 || tap_count % 2 == 0); |
56 | | |
57 | | /* if upsampling, only need to interpolate, no filter */ |
58 | 33.8k | if (factor > 1.0) |
59 | 0 | factor = 1.0; |
60 | | |
61 | 33.8k | if (factor == 1.0) { |
62 | 497k | for (ph = 0; ph < ph_nb; ph++) |
63 | 464k | sin_lut[ph] = sin(M_PI * ph / phase_count) * (center & 1 ? 1 : -1); |
64 | 33.3k | } |
65 | 670k | for(ph = 0; ph < ph_nb; ph++) { |
66 | 636k | s = sin_lut[ph]; |
67 | 176M | for(i=0;i<tap_count;i++) { |
68 | 176M | x = M_PI * ((double)(i - center) - (double)ph / phase_count) * factor; |
69 | 176M | if (x == 0) y = 1.0; |
70 | 176M | else if (factor == 1.0) |
71 | 12.9M | y = s / x; |
72 | 163M | else |
73 | 163M | y = sin(x) / x; |
74 | 176M | switch(filter_type){ |
75 | 0 | case SWR_FILTER_TYPE_CUBIC:{ |
76 | 0 | const float d= -0.5; //first order derivative = -0.5 |
77 | 0 | x = fabs(((double)(i - center) - (double)ph / phase_count) * factor); |
78 | 0 | if(x<1.0) y= 1 - 3*x*x + 2*x*x*x + d*( -x*x + x*x*x); |
79 | 0 | else y= d*(-4 + 8*x - 5*x*x + x*x*x); |
80 | 0 | break;} |
81 | 0 | case SWR_FILTER_TYPE_BLACKMAN_NUTTALL: |
82 | 0 | w = 2.0*x / (factor*tap_count); |
83 | 0 | t = -cos(w); |
84 | 0 | y *= 0.3635819 - 0.4891775 * t + 0.1365995 * (2*t*t-1) - 0.0106411 * (4*t*t*t - 3*t); |
85 | 0 | break; |
86 | 176M | case SWR_FILTER_TYPE_KAISER: |
87 | 176M | w = 2.0*x / (factor*tap_count*M_PI); |
88 | 176M | y *= av_bessel_i0(kaiser_beta*sqrt(FFMAX(1-w*w, 0))); |
89 | 176M | break; |
90 | 0 | default: |
91 | 0 | av_assert0(0); |
92 | 176M | } |
93 | | |
94 | 176M | tab[i] = y; |
95 | 176M | s = -s; |
96 | 176M | if (!ph) |
97 | 46.0M | norm += y; |
98 | 176M | } |
99 | | |
100 | | /* normalize so that an uniform color remains the same */ |
101 | 636k | switch(c->format){ |
102 | 214k | case AV_SAMPLE_FMT_S16P: |
103 | 102M | for(i=0;i<tap_count;i++) |
104 | 101M | ((int16_t*)filter)[ph * alloc + i] = av_clip_int16(lrintf(tab[i] * scale / norm)); |
105 | 214k | if (phase_count % 2) break; |
106 | 38.8M | for (i = 0; i < tap_count; i++) |
107 | 38.6M | ((int16_t*)filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((int16_t*)filter)[ph * alloc + i]; |
108 | 180k | break; |
109 | 0 | case AV_SAMPLE_FMT_S32P: |
110 | 0 | for(i=0;i<tap_count;i++) |
111 | 0 | ((int32_t*)filter)[ph * alloc + i] = av_clipl_int32(llrint(tab[i] * scale / norm)); |
112 | 0 | if (phase_count % 2) break; |
113 | 0 | for (i = 0; i < tap_count; i++) |
114 | 0 | ((int32_t*)filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((int32_t*)filter)[ph * alloc + i]; |
115 | 0 | break; |
116 | 340k | case AV_SAMPLE_FMT_FLTP: |
117 | 56.3M | for(i=0;i<tap_count;i++) |
118 | 56.0M | ((float*)filter)[ph * alloc + i] = tab[i] * scale / norm; |
119 | 340k | if (phase_count % 2) break; |
120 | 26.6M | for (i = 0; i < tap_count; i++) |
121 | 26.3M | ((float*)filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((float*)filter)[ph * alloc + i]; |
122 | 284k | break; |
123 | 81.5k | case AV_SAMPLE_FMT_DBLP: |
124 | 18.3M | for(i=0;i<tap_count;i++) |
125 | 18.2M | ((double*)filter)[ph * alloc + i] = tab[i] * scale / norm; |
126 | 81.5k | if (phase_count % 2) break; |
127 | 8.05M | for (i = 0; i < tap_count; i++) |
128 | 7.99M | ((double*)filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((double*)filter)[ph * alloc + i]; |
129 | 60.1k | break; |
130 | 636k | } |
131 | 636k | } |
132 | | #if 0 |
133 | | { |
134 | | #define LEN 1024 |
135 | | int j,k; |
136 | | double sine[LEN + tap_count]; |
137 | | double filtered[LEN]; |
138 | | double maxff=-2, minff=2, maxsf=-2, minsf=2; |
139 | | for(i=0; i<LEN; i++){ |
140 | | double ss=0, sf=0, ff=0; |
141 | | for(j=0; j<LEN+tap_count; j++) |
142 | | sine[j]= cos(i*j*M_PI/LEN); |
143 | | for(j=0; j<LEN; j++){ |
144 | | double sum=0; |
145 | | ph=0; |
146 | | for(k=0; k<tap_count; k++) |
147 | | sum += filter[ph * tap_count + k] * sine[k+j]; |
148 | | filtered[j]= sum / (1<<FILTER_SHIFT); |
149 | | ss+= sine[j + center] * sine[j + center]; |
150 | | ff+= filtered[j] * filtered[j]; |
151 | | sf+= sine[j + center] * filtered[j]; |
152 | | } |
153 | | ss= sqrt(2*ss/LEN); |
154 | | ff= sqrt(2*ff/LEN); |
155 | | sf= 2*sf/LEN; |
156 | | maxff= FFMAX(maxff, ff); |
157 | | minff= FFMIN(minff, ff); |
158 | | maxsf= FFMAX(maxsf, sf); |
159 | | minsf= FFMIN(minsf, sf); |
160 | | if(i%11==0){ |
161 | | av_log(NULL, AV_LOG_ERROR, "i:%4d ss:%f ff:%13.6e-%13.6e sf:%13.6e-%13.6e\n", i, ss, maxff, minff, maxsf, minsf); |
162 | | minff=minsf= 2; |
163 | | maxff=maxsf= -2; |
164 | | } |
165 | | } |
166 | | } |
167 | | #endif |
168 | | |
169 | 33.8k | ret = 0; |
170 | 33.8k | fail: |
171 | 33.8k | av_free(tab); |
172 | 33.8k | av_free(sin_lut); |
173 | 33.8k | return ret; |
174 | 33.8k | } |
175 | | |
176 | 49.0k | static void resample_free(ResampleContext **cc){ |
177 | 49.0k | ResampleContext *c = *cc; |
178 | 49.0k | if(!c) |
179 | 15.2k | return; |
180 | 33.8k | av_freep(&c->filter_bank); |
181 | 33.8k | av_freep(cc); |
182 | 33.8k | } |
183 | | |
184 | | static ResampleContext *resample_init(ResampleContext *c, int out_rate, int in_rate, int filter_size, int phase_shift, int linear, |
185 | | double cutoff0, enum AVSampleFormat format, enum SwrFilterType filter_type, double kaiser_beta, |
186 | | double precision, int cheby, int exact_rational) |
187 | 82.4k | { |
188 | 82.4k | double cutoff = cutoff0? cutoff0 : 0.97; |
189 | 82.4k | double factor= FFMIN(out_rate * cutoff / in_rate, 1.0); |
190 | 82.4k | int phase_count= 1<<phase_shift; |
191 | 82.4k | int phase_count_compensation = phase_count; |
192 | 82.4k | int filter_length = FFMAX((int)ceil(filter_size/factor), 1); |
193 | | |
194 | 82.4k | if (filter_length > 1) |
195 | 82.4k | filter_length = FFALIGN(filter_length, 2); |
196 | | |
197 | 82.4k | if (exact_rational) { |
198 | 82.4k | int phase_count_exact, phase_count_exact_den; |
199 | | |
200 | 82.4k | av_reduce(&phase_count_exact, &phase_count_exact_den, out_rate, in_rate, INT_MAX); |
201 | 82.4k | if (phase_count_exact <= phase_count) { |
202 | 81.6k | phase_count_compensation = phase_count_exact * (phase_count / phase_count_exact); |
203 | 81.6k | phase_count = phase_count_exact; |
204 | 81.6k | } |
205 | 82.4k | } |
206 | | |
207 | 82.4k | if (!c || c->phase_count != phase_count || c->linear!=linear || c->factor != factor |
208 | 48.6k | || c->filter_length != filter_length || c->format != format |
209 | 48.6k | || c->filter_type != filter_type || c->kaiser_beta != kaiser_beta) { |
210 | 33.8k | resample_free(&c); |
211 | 33.8k | c = av_mallocz(sizeof(*c)); |
212 | 33.8k | if (!c) |
213 | 0 | return NULL; |
214 | | |
215 | 33.8k | c->format= format; |
216 | | |
217 | 33.8k | c->felem_size= av_get_bytes_per_sample(c->format); |
218 | | |
219 | 33.8k | switch(c->format){ |
220 | 590 | case AV_SAMPLE_FMT_S16P: |
221 | 590 | c->filter_shift = 15; |
222 | 590 | break; |
223 | 0 | case AV_SAMPLE_FMT_S32P: |
224 | 0 | c->filter_shift = 30; |
225 | 0 | break; |
226 | 33.1k | case AV_SAMPLE_FMT_FLTP: |
227 | 33.2k | case AV_SAMPLE_FMT_DBLP: |
228 | 33.2k | c->filter_shift = 0; |
229 | 33.2k | break; |
230 | 0 | default: |
231 | 0 | av_log(NULL, AV_LOG_ERROR, "Unsupported sample format\n"); |
232 | 0 | av_assert0(0); |
233 | 33.8k | } |
234 | | |
235 | 33.8k | if (filter_size/factor > INT32_MAX/256) { |
236 | 0 | av_log(NULL, AV_LOG_ERROR, "Filter length too large\n"); |
237 | 0 | goto error; |
238 | 0 | } |
239 | | |
240 | 33.8k | c->phase_count = phase_count; |
241 | 33.8k | c->linear = linear; |
242 | 33.8k | c->factor = factor; |
243 | 33.8k | c->filter_length = filter_length; |
244 | 33.8k | c->filter_alloc = FFALIGN(c->filter_length, 8); |
245 | 33.8k | c->filter_bank = av_calloc(c->filter_alloc, (phase_count+1)*c->felem_size); |
246 | 33.8k | c->filter_type = filter_type; |
247 | 33.8k | c->kaiser_beta = kaiser_beta; |
248 | 33.8k | c->phase_count_compensation = phase_count_compensation; |
249 | 33.8k | if (!c->filter_bank) |
250 | 0 | goto error; |
251 | 33.8k | if (build_filter(c, (void*)c->filter_bank, factor, c->filter_length, c->filter_alloc, phase_count, 1<<c->filter_shift, filter_type, kaiser_beta)) |
252 | 0 | goto error; |
253 | 33.8k | memcpy(c->filter_bank + (c->filter_alloc*phase_count+1)*c->felem_size, c->filter_bank, (c->filter_alloc-1)*c->felem_size); |
254 | 33.8k | memcpy(c->filter_bank + (c->filter_alloc*phase_count )*c->felem_size, c->filter_bank + (c->filter_alloc - 1)*c->felem_size, c->felem_size); |
255 | 33.8k | } |
256 | | |
257 | 82.4k | c->compensation_distance= 0; |
258 | 82.4k | if(!av_reduce(&c->src_incr, &c->dst_incr, out_rate, in_rate * (int64_t)phase_count, INT32_MAX/2)) |
259 | 0 | goto error; |
260 | 1.71M | while (c->dst_incr < (1<<20) && c->src_incr < (1<<20)) { |
261 | 1.63M | c->dst_incr *= 2; |
262 | 1.63M | c->src_incr *= 2; |
263 | 1.63M | } |
264 | 82.4k | c->ideal_dst_incr = c->dst_incr; |
265 | 82.4k | c->dst_incr_div = c->dst_incr / c->src_incr; |
266 | 82.4k | c->dst_incr_mod = c->dst_incr % c->src_incr; |
267 | | |
268 | 82.4k | c->index= -phase_count*((c->filter_length-1)/2); |
269 | 82.4k | c->frac= 0; |
270 | | |
271 | 82.4k | swri_resample_dsp_init(c); |
272 | | |
273 | 82.4k | return c; |
274 | 0 | error: |
275 | 0 | av_freep(&c->filter_bank); |
276 | 0 | av_free(c); |
277 | 0 | return NULL; |
278 | 82.4k | } |
279 | | |
280 | | static int rebuild_filter_bank_with_compensation(ResampleContext *c) |
281 | 0 | { |
282 | 0 | uint8_t *new_filter_bank; |
283 | 0 | int new_src_incr, new_dst_incr; |
284 | 0 | int phase_count = c->phase_count_compensation; |
285 | 0 | int ret; |
286 | |
|
287 | 0 | if (phase_count == c->phase_count) |
288 | 0 | return 0; |
289 | | |
290 | 0 | av_assert0(!c->frac && !c->dst_incr_mod); |
291 | | |
292 | 0 | new_filter_bank = av_calloc(c->filter_alloc, (phase_count + 1) * c->felem_size); |
293 | 0 | if (!new_filter_bank) |
294 | 0 | return AVERROR(ENOMEM); |
295 | | |
296 | 0 | ret = build_filter(c, new_filter_bank, c->factor, c->filter_length, c->filter_alloc, |
297 | 0 | phase_count, 1 << c->filter_shift, c->filter_type, c->kaiser_beta); |
298 | 0 | if (ret < 0) { |
299 | 0 | av_freep(&new_filter_bank); |
300 | 0 | return ret; |
301 | 0 | } |
302 | 0 | memcpy(new_filter_bank + (c->filter_alloc*phase_count+1)*c->felem_size, new_filter_bank, (c->filter_alloc-1)*c->felem_size); |
303 | 0 | memcpy(new_filter_bank + (c->filter_alloc*phase_count )*c->felem_size, new_filter_bank + (c->filter_alloc - 1)*c->felem_size, c->felem_size); |
304 | |
|
305 | 0 | if (!av_reduce(&new_src_incr, &new_dst_incr, c->src_incr, |
306 | 0 | c->dst_incr * (int64_t)(phase_count/c->phase_count), INT32_MAX/2)) |
307 | 0 | { |
308 | 0 | av_freep(&new_filter_bank); |
309 | 0 | return AVERROR(EINVAL); |
310 | 0 | } |
311 | | |
312 | 0 | c->src_incr = new_src_incr; |
313 | 0 | c->dst_incr = new_dst_incr; |
314 | 0 | while (c->dst_incr < (1<<20) && c->src_incr < (1<<20)) { |
315 | 0 | c->dst_incr *= 2; |
316 | 0 | c->src_incr *= 2; |
317 | 0 | } |
318 | 0 | c->ideal_dst_incr = c->dst_incr; |
319 | 0 | c->dst_incr_div = c->dst_incr / c->src_incr; |
320 | 0 | c->dst_incr_mod = c->dst_incr % c->src_incr; |
321 | 0 | c->index *= phase_count / c->phase_count; |
322 | 0 | c->phase_count = phase_count; |
323 | 0 | av_freep(&c->filter_bank); |
324 | 0 | c->filter_bank = new_filter_bank; |
325 | 0 | return 0; |
326 | 0 | } |
327 | | |
328 | 0 | static int set_compensation(ResampleContext *c, int sample_delta, int compensation_distance){ |
329 | 0 | int ret; |
330 | |
|
331 | 0 | if (compensation_distance && sample_delta) { |
332 | 0 | ret = rebuild_filter_bank_with_compensation(c); |
333 | 0 | if (ret < 0) |
334 | 0 | return ret; |
335 | 0 | } |
336 | | |
337 | 0 | c->compensation_distance= compensation_distance; |
338 | 0 | if (compensation_distance) |
339 | 0 | c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr * (int64_t)sample_delta / compensation_distance; |
340 | 0 | else |
341 | 0 | c->dst_incr = c->ideal_dst_incr; |
342 | |
|
343 | 0 | c->dst_incr_div = c->dst_incr / c->src_incr; |
344 | 0 | c->dst_incr_mod = c->dst_incr % c->src_incr; |
345 | |
|
346 | 0 | return 0; |
347 | 0 | } |
348 | | |
349 | 511k | static int multiple_resample(ResampleContext *c, AudioData *dst, int dst_size, AudioData *src, int src_size, int *consumed){ |
350 | 511k | int i; |
351 | 511k | int64_t max_src_size = (INT64_MAX/2 / c->phase_count) / c->src_incr; |
352 | | |
353 | 511k | if (c->compensation_distance) |
354 | 0 | dst_size = FFMIN(dst_size, c->compensation_distance); |
355 | 511k | src_size = FFMIN(src_size, max_src_size); |
356 | | |
357 | 511k | *consumed = 0; |
358 | | |
359 | 511k | if (c->filter_length == 1 && c->phase_count == 1) { |
360 | 0 | int64_t index2= (1LL<<32)*c->frac/c->src_incr + (1LL<<32)*c->index + 1; |
361 | 0 | int64_t incr= (1LL<<32) * c->dst_incr / c->src_incr + 1; |
362 | 0 | int new_size = (src_size * (int64_t)c->src_incr - c->frac + c->dst_incr - 1) / c->dst_incr; |
363 | |
|
364 | 0 | dst_size = FFMAX(FFMIN(dst_size, new_size), 0); |
365 | 0 | if (dst_size > 0) { |
366 | 0 | for (i = 0; i < dst->ch_count; i++) { |
367 | 0 | c->dsp.resample_one(dst->ch[i], src->ch[i], dst_size, index2, incr); |
368 | 0 | if (i+1 == dst->ch_count) { |
369 | 0 | c->index += dst_size * c->dst_incr_div; |
370 | 0 | c->index += (c->frac + dst_size * (int64_t)c->dst_incr_mod) / c->src_incr; |
371 | 0 | av_assert2(c->index >= 0); |
372 | 0 | *consumed = c->index; |
373 | 0 | c->frac = (c->frac + dst_size * (int64_t)c->dst_incr_mod) % c->src_incr; |
374 | 0 | c->index = 0; |
375 | 0 | } |
376 | 0 | } |
377 | 0 | } |
378 | 511k | } else { |
379 | 511k | int64_t end_index = (1LL + src_size - c->filter_length) * c->phase_count; |
380 | 511k | int64_t delta_frac = (end_index - c->index) * c->src_incr - c->frac; |
381 | 511k | int delta_n = (delta_frac + c->dst_incr - 1) / c->dst_incr; |
382 | 511k | int (*resample_func)(struct ResampleContext *c, void *dst, |
383 | 511k | const void *src, int n, int update_ctx); |
384 | | |
385 | 511k | dst_size = FFMAX(FFMIN(dst_size, delta_n), 0); |
386 | 511k | if (dst_size > 0) { |
387 | | /* resample_linear and resample_common should have same behavior |
388 | | * when frac and dst_incr_mod are zero */ |
389 | 450k | resample_func = (c->linear && (c->frac || c->dst_incr_mod)) ? |
390 | 449k | c->dsp.resample_linear : c->dsp.resample_common; |
391 | 1.22M | for (i = 0; i < dst->ch_count; i++) |
392 | 777k | *consumed = resample_func(c, dst->ch[i], src->ch[i], dst_size, i+1 == dst->ch_count); |
393 | 450k | } |
394 | 511k | } |
395 | | |
396 | 511k | if (c->compensation_distance) { |
397 | 0 | c->compensation_distance -= dst_size; |
398 | 0 | if (!c->compensation_distance) { |
399 | 0 | c->dst_incr = c->ideal_dst_incr; |
400 | 0 | c->dst_incr_div = c->dst_incr / c->src_incr; |
401 | 0 | c->dst_incr_mod = c->dst_incr % c->src_incr; |
402 | 0 | } |
403 | 0 | } |
404 | | |
405 | 511k | return dst_size; |
406 | 511k | } |
407 | | |
408 | 0 | static int64_t get_delay(struct SwrContext *s, int64_t base){ |
409 | 0 | ResampleContext *c = s->resample; |
410 | 0 | int64_t num = s->in_buffer_count - (c->filter_length-1)/2; |
411 | 0 | num *= c->phase_count; |
412 | 0 | num -= c->index; |
413 | 0 | num *= c->src_incr; |
414 | 0 | num -= c->frac; |
415 | 0 | return av_rescale(num, base, s->in_sample_rate*(int64_t)c->src_incr * c->phase_count); |
416 | 0 | } |
417 | | |
418 | 0 | static int64_t get_out_samples(struct SwrContext *s, int in_samples) { |
419 | 0 | ResampleContext *c = s->resample; |
420 | | // The + 2 are added to allow implementations to be slightly inaccurate, they should not be needed currently. |
421 | | // They also make it easier to proof that changes and optimizations do not |
422 | | // break the upper bound. |
423 | 0 | int64_t num = s->in_buffer_count + 2LL + in_samples; |
424 | 0 | num *= c->phase_count; |
425 | 0 | num -= c->index; |
426 | 0 | num = av_rescale_rnd(num, s->out_sample_rate, ((int64_t)s->in_sample_rate) * c->phase_count, AV_ROUND_UP) + 2; |
427 | |
|
428 | 0 | if (c->compensation_distance) { |
429 | 0 | if (num > INT_MAX) |
430 | 0 | return AVERROR(EINVAL); |
431 | | |
432 | 0 | num = FFMAX(num, (num * c->ideal_dst_incr - 1) / c->dst_incr + 1); |
433 | 0 | } |
434 | 0 | return num; |
435 | 0 | } |
436 | | |
437 | 22.4k | static int resample_flush(struct SwrContext *s) { |
438 | 22.4k | ResampleContext *c = s->resample; |
439 | 22.4k | AudioData *a= &s->in_buffer; |
440 | 22.4k | int i, j, ret; |
441 | 22.4k | int reflection = (FFMIN(s->in_buffer_count, c->filter_length) + 1) / 2; |
442 | | |
443 | 22.4k | if((ret = swri_realloc_audio(a, s->in_buffer_index + s->in_buffer_count + reflection)) < 0) |
444 | 0 | return ret; |
445 | 22.4k | av_assert0(a->planar); |
446 | 63.0k | for(i=0; i<a->ch_count; i++){ |
447 | 365k | for(j=0; j<reflection; j++){ |
448 | 325k | memcpy(a->ch[i] + (s->in_buffer_index+s->in_buffer_count+j )*a->bps, |
449 | 325k | a->ch[i] + (s->in_buffer_index+s->in_buffer_count-j-1)*a->bps, a->bps); |
450 | 325k | } |
451 | 40.6k | } |
452 | 22.4k | s->in_buffer_count += reflection; |
453 | 22.4k | return 0; |
454 | 22.4k | } |
455 | | |
456 | | // in fact the whole handle multiple ridiculously small buffers might need more thinking... |
457 | | static int invert_initial_buffer(ResampleContext *c, AudioData *dst, const AudioData *src, |
458 | | int in_count, int *out_idx, int *out_sz) |
459 | 274k | { |
460 | 274k | int n, ch, num = FFMIN(in_count + *out_sz, c->filter_length + 1), res; |
461 | | |
462 | 274k | if (c->index >= 0) |
463 | 110k | return 0; |
464 | | |
465 | 163k | if ((res = swri_realloc_audio(dst, c->filter_length * 2 + 1)) < 0) |
466 | 0 | return res; |
467 | | |
468 | | // copy |
469 | 10.3M | for (n = *out_sz; n < num; n++) { |
470 | 33.2M | for (ch = 0; ch < src->ch_count; ch++) { |
471 | 23.0M | memcpy(dst->ch[ch] + ((c->filter_length + n) * c->felem_size), |
472 | 23.0M | src->ch[ch] + ((n - *out_sz) * c->felem_size), c->felem_size); |
473 | 23.0M | } |
474 | 10.1M | } |
475 | | |
476 | | // if not enough data is in, return and wait for more |
477 | 163k | if (num < c->filter_length + 1) { |
478 | 81.4k | *out_sz = num; |
479 | 81.4k | *out_idx = c->filter_length; |
480 | 81.4k | return INT_MAX; |
481 | 81.4k | } |
482 | | |
483 | | // else invert |
484 | 8.48M | for (n = 1; n <= c->filter_length; n++) { |
485 | 27.5M | for (ch = 0; ch < src->ch_count; ch++) { |
486 | 19.1M | memcpy(dst->ch[ch] + ((c->filter_length - n) * c->felem_size), |
487 | 19.1M | dst->ch[ch] + ((c->filter_length + n) * c->felem_size), |
488 | 19.1M | c->felem_size); |
489 | 19.1M | } |
490 | 8.40M | } |
491 | | |
492 | 82.0k | res = num - *out_sz; |
493 | 82.0k | *out_idx = c->filter_length; |
494 | 4.20M | while (c->index < 0) { |
495 | 4.11M | --*out_idx; |
496 | 4.11M | c->index += c->phase_count; |
497 | 4.11M | } |
498 | 82.0k | *out_sz = FFMAX(*out_sz + c->filter_length, |
499 | 82.0k | 1 + c->filter_length * 2) - *out_idx; |
500 | | |
501 | 82.0k | return FFMAX(res, 0); |
502 | 163k | } |
503 | | |
504 | | const struct Resampler swri_resampler = { |
505 | | .init = resample_init, |
506 | | .free = resample_free, |
507 | | .multiple_resample = multiple_resample, |
508 | | .flush = resample_flush, |
509 | | .set_compensation = set_compensation, |
510 | | .get_delay = get_delay, |
511 | | .invert_initial_buffer = invert_initial_buffer, |
512 | | .get_out_samples = get_out_samples, |
513 | | }; |