FFmpeg
resample.c
Go to the documentation of this file.
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/cpu.h"
31 #include "resample.h"
32 
33 static inline double eval_poly(const double *coeff, int size, double x) {
34  double sum = coeff[size-1];
35  int i;
36  for (i = size-2; i >= 0; --i) {
37  sum *= x;
38  sum += coeff[i];
39  }
40  return sum;
41 }
42 
43 /**
44  * 0th order modified bessel function of the first kind.
45  * Algorithm taken from the Boost project, source:
46  * https://searchcode.com/codesearch/view/14918379/
47  * Use, modification and distribution are subject to the
48  * Boost Software License, Version 1.0 (see notice below).
49  * Boost Software License - Version 1.0 - August 17th, 2003
50 Permission is hereby granted, free of charge, to any person or organization
51 obtaining a copy of the software and accompanying documentation covered by
52 this license (the "Software") to use, reproduce, display, distribute,
53 execute, and transmit the Software, and to prepare derivative works of the
54 Software, and to permit third-parties to whom the Software is furnished to
55 do so, all subject to the following:
56 
57 The copyright notices in the Software and this entire statement, including
58 the above license grant, this restriction and the following disclaimer,
59 must be included in all copies of the Software, in whole or in part, and
60 all derivative works of the Software, unless such copies or derivative
61 works are solely in the form of machine-executable object code generated by
62 a source language processor.
63 
64 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
65 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
66 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
67 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
68 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
69 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
70 DEALINGS IN THE SOFTWARE.
71  */
72 
73 static double bessel(double x) {
74 // Modified Bessel function of the first kind of order zero
75 // minimax rational approximations on intervals, see
76 // Blair and Edwards, Chalk River Report AECL-4928, 1974
77  static const double p1[] = {
78  -2.2335582639474375249e+15,
79  -5.5050369673018427753e+14,
80  -3.2940087627407749166e+13,
81  -8.4925101247114157499e+11,
82  -1.1912746104985237192e+10,
83  -1.0313066708737980747e+08,
84  -5.9545626019847898221e+05,
85  -2.4125195876041896775e+03,
86  -7.0935347449210549190e+00,
87  -1.5453977791786851041e-02,
88  -2.5172644670688975051e-05,
89  -3.0517226450451067446e-08,
90  -2.6843448573468483278e-11,
91  -1.5982226675653184646e-14,
92  -5.2487866627945699800e-18,
93  };
94  static const double q1[] = {
95  -2.2335582639474375245e+15,
96  7.8858692566751002988e+12,
97  -1.2207067397808979846e+10,
98  1.0377081058062166144e+07,
99  -4.8527560179962773045e+03,
100  1.0,
101  };
102  static const double p2[] = {
103  -2.2210262233306573296e-04,
104  1.3067392038106924055e-02,
105  -4.4700805721174453923e-01,
106  5.5674518371240761397e+00,
107  -2.3517945679239481621e+01,
108  3.1611322818701131207e+01,
109  -9.6090021968656180000e+00,
110  };
111  static const double q2[] = {
112  -5.5194330231005480228e-04,
113  3.2547697594819615062e-02,
114  -1.1151759188741312645e+00,
115  1.3982595353892851542e+01,
116  -6.0228002066743340583e+01,
117  8.5539563258012929600e+01,
118  -3.1446690275135491500e+01,
119  1.0,
120  };
121  double y, r, factor;
122  if (x == 0)
123  return 1.0;
124  x = fabs(x);
125  if (x <= 15) {
126  y = x * x;
127  return eval_poly(p1, FF_ARRAY_ELEMS(p1), y) / eval_poly(q1, FF_ARRAY_ELEMS(q1), y);
128  }
129  else {
130  y = 1 / x - 1.0 / 15;
131  r = eval_poly(p2, FF_ARRAY_ELEMS(p2), y) / eval_poly(q2, FF_ARRAY_ELEMS(q2), y);
132  factor = exp(x) / sqrt(x);
133  return factor * r;
134  }
135 }
136 
137 /**
138  * builds a polyphase filterbank.
139  * @param factor resampling factor
140  * @param scale wanted sum of coefficients for each filter
141  * @param filter_type filter type
142  * @param kaiser_beta kaiser window beta
143  * @return 0 on success, negative on error
144  */
145 static int build_filter(ResampleContext *c, void *filter, double factor, int tap_count, int alloc, int phase_count, int scale,
146  int filter_type, double kaiser_beta){
147  int ph, i;
148  int ph_nb = phase_count % 2 ? phase_count : phase_count / 2 + 1;
149  double x, y, w, t, s;
150  double *tab = av_malloc_array(tap_count+1, sizeof(*tab));
151  double *sin_lut = av_malloc_array(ph_nb, sizeof(*sin_lut));
152  const int center= (tap_count-1)/2;
153  double norm = 0;
154  int ret = AVERROR(ENOMEM);
155 
156  if (!tab || !sin_lut)
157  goto fail;
158 
159  av_assert0(tap_count == 1 || tap_count % 2 == 0);
160 
161  /* if upsampling, only need to interpolate, no filter */
162  if (factor > 1.0)
163  factor = 1.0;
164 
165  if (factor == 1.0) {
166  for (ph = 0; ph < ph_nb; ph++)
167  sin_lut[ph] = sin(M_PI * ph / phase_count) * (center & 1 ? 1 : -1);
168  }
169  for(ph = 0; ph < ph_nb; ph++) {
170  s = sin_lut[ph];
171  for(i=0;i<tap_count;i++) {
172  x = M_PI * ((double)(i - center) - (double)ph / phase_count) * factor;
173  if (x == 0) y = 1.0;
174  else if (factor == 1.0)
175  y = s / x;
176  else
177  y = sin(x) / x;
178  switch(filter_type){
179  case SWR_FILTER_TYPE_CUBIC:{
180  const float d= -0.5; //first order derivative = -0.5
181  x = fabs(((double)(i - center) - (double)ph / phase_count) * factor);
182  if(x<1.0) y= 1 - 3*x*x + 2*x*x*x + d*( -x*x + x*x*x);
183  else y= d*(-4 + 8*x - 5*x*x + x*x*x);
184  break;}
186  w = 2.0*x / (factor*tap_count);
187  t = -cos(w);
188  y *= 0.3635819 - 0.4891775 * t + 0.1365995 * (2*t*t-1) - 0.0106411 * (4*t*t*t - 3*t);
189  break;
191  w = 2.0*x / (factor*tap_count*M_PI);
192  y *= bessel(kaiser_beta*sqrt(FFMAX(1-w*w, 0)));
193  break;
194  default:
195  av_assert0(0);
196  }
197 
198  tab[i] = y;
199  s = -s;
200  if (!ph)
201  norm += y;
202  }
203 
204  /* normalize so that an uniform color remains the same */
205  switch(c->format){
206  case AV_SAMPLE_FMT_S16P:
207  for(i=0;i<tap_count;i++)
208  ((int16_t*)filter)[ph * alloc + i] = av_clip_int16(lrintf(tab[i] * scale / norm));
209  if (phase_count % 2) break;
210  for (i = 0; i < tap_count; i++)
211  ((int16_t*)filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((int16_t*)filter)[ph * alloc + i];
212  break;
213  case AV_SAMPLE_FMT_S32P:
214  for(i=0;i<tap_count;i++)
215  ((int32_t*)filter)[ph * alloc + i] = av_clipl_int32(llrint(tab[i] * scale / norm));
216  if (phase_count % 2) break;
217  for (i = 0; i < tap_count; i++)
218  ((int32_t*)filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((int32_t*)filter)[ph * alloc + i];
219  break;
220  case AV_SAMPLE_FMT_FLTP:
221  for(i=0;i<tap_count;i++)
222  ((float*)filter)[ph * alloc + i] = tab[i] * scale / norm;
223  if (phase_count % 2) break;
224  for (i = 0; i < tap_count; i++)
225  ((float*)filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((float*)filter)[ph * alloc + i];
226  break;
227  case AV_SAMPLE_FMT_DBLP:
228  for(i=0;i<tap_count;i++)
229  ((double*)filter)[ph * alloc + i] = tab[i] * scale / norm;
230  if (phase_count % 2) break;
231  for (i = 0; i < tap_count; i++)
232  ((double*)filter)[(phase_count-ph) * alloc + tap_count-1-i] = ((double*)filter)[ph * alloc + i];
233  break;
234  }
235  }
236 #if 0
237  {
238 #define LEN 1024
239  int j,k;
240  double sine[LEN + tap_count];
241  double filtered[LEN];
242  double maxff=-2, minff=2, maxsf=-2, minsf=2;
243  for(i=0; i<LEN; i++){
244  double ss=0, sf=0, ff=0;
245  for(j=0; j<LEN+tap_count; j++)
246  sine[j]= cos(i*j*M_PI/LEN);
247  for(j=0; j<LEN; j++){
248  double sum=0;
249  ph=0;
250  for(k=0; k<tap_count; k++)
251  sum += filter[ph * tap_count + k] * sine[k+j];
252  filtered[j]= sum / (1<<FILTER_SHIFT);
253  ss+= sine[j + center] * sine[j + center];
254  ff+= filtered[j] * filtered[j];
255  sf+= sine[j + center] * filtered[j];
256  }
257  ss= sqrt(2*ss/LEN);
258  ff= sqrt(2*ff/LEN);
259  sf= 2*sf/LEN;
260  maxff= FFMAX(maxff, ff);
261  minff= FFMIN(minff, ff);
262  maxsf= FFMAX(maxsf, sf);
263  minsf= FFMIN(minsf, sf);
264  if(i%11==0){
265  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);
266  minff=minsf= 2;
267  maxff=maxsf= -2;
268  }
269  }
270  }
271 #endif
272 
273  ret = 0;
274 fail:
275  av_free(tab);
276  av_free(sin_lut);
277  return ret;
278 }
279 
280 static void resample_free(ResampleContext **cc){
281  ResampleContext *c = *cc;
282  if(!c)
283  return;
284  av_freep(&c->filter_bank);
285  av_freep(cc);
286 }
287 
288 static ResampleContext *resample_init(ResampleContext *c, int out_rate, int in_rate, int filter_size, int phase_shift, int linear,
289  double cutoff0, enum AVSampleFormat format, enum SwrFilterType filter_type, double kaiser_beta,
290  double precision, int cheby, int exact_rational)
291 {
292  double cutoff = cutoff0? cutoff0 : 0.97;
293  double factor= FFMIN(out_rate * cutoff / in_rate, 1.0);
294  int phase_count= 1<<phase_shift;
295  int phase_count_compensation = phase_count;
296  int filter_length = FFMAX((int)ceil(filter_size/factor), 1);
297 
298  if (filter_length > 1)
299  filter_length = FFALIGN(filter_length, 2);
300 
301  if (exact_rational) {
302  int phase_count_exact, phase_count_exact_den;
303 
304  av_reduce(&phase_count_exact, &phase_count_exact_den, out_rate, in_rate, INT_MAX);
305  if (phase_count_exact <= phase_count) {
306  phase_count_compensation = phase_count_exact * (phase_count / phase_count_exact);
307  phase_count = phase_count_exact;
308  }
309  }
310 
311  if (!c || c->phase_count != phase_count || c->linear!=linear || c->factor != factor
312  || c->filter_length != filter_length || c->format != format
313  || c->filter_type != filter_type || c->kaiser_beta != kaiser_beta) {
314  resample_free(&c);
315  c = av_mallocz(sizeof(*c));
316  if (!c)
317  return NULL;
318 
319  c->format= format;
320 
321  c->felem_size= av_get_bytes_per_sample(c->format);
322 
323  switch(c->format){
324  case AV_SAMPLE_FMT_S16P:
325  c->filter_shift = 15;
326  break;
327  case AV_SAMPLE_FMT_S32P:
328  c->filter_shift = 30;
329  break;
330  case AV_SAMPLE_FMT_FLTP:
331  case AV_SAMPLE_FMT_DBLP:
332  c->filter_shift = 0;
333  break;
334  default:
335  av_log(NULL, AV_LOG_ERROR, "Unsupported sample format\n");
336  av_assert0(0);
337  }
338 
339  if (filter_size/factor > INT32_MAX/256) {
340  av_log(NULL, AV_LOG_ERROR, "Filter length too large\n");
341  goto error;
342  }
343 
344  c->phase_count = phase_count;
345  c->linear = linear;
346  c->factor = factor;
347  c->filter_length = filter_length;
348  c->filter_alloc = FFALIGN(c->filter_length, 8);
349  c->filter_bank = av_calloc(c->filter_alloc, (phase_count+1)*c->felem_size);
350  c->filter_type = filter_type;
351  c->kaiser_beta = kaiser_beta;
352  c->phase_count_compensation = phase_count_compensation;
353  if (!c->filter_bank)
354  goto error;
355  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))
356  goto error;
357  memcpy(c->filter_bank + (c->filter_alloc*phase_count+1)*c->felem_size, c->filter_bank, (c->filter_alloc-1)*c->felem_size);
358  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);
359  }
360 
361  c->compensation_distance= 0;
362  if(!av_reduce(&c->src_incr, &c->dst_incr, out_rate, in_rate * (int64_t)phase_count, INT32_MAX/2))
363  goto error;
364  while (c->dst_incr < (1<<20) && c->src_incr < (1<<20)) {
365  c->dst_incr *= 2;
366  c->src_incr *= 2;
367  }
368  c->ideal_dst_incr = c->dst_incr;
369  c->dst_incr_div = c->dst_incr / c->src_incr;
370  c->dst_incr_mod = c->dst_incr % c->src_incr;
371 
372  c->index= -phase_count*((c->filter_length-1)/2);
373  c->frac= 0;
374 
376 
377  return c;
378 error:
379  av_freep(&c->filter_bank);
380  av_free(c);
381  return NULL;
382 }
383 
385 {
386  uint8_t *new_filter_bank;
387  int new_src_incr, new_dst_incr;
388  int phase_count = c->phase_count_compensation;
389  int ret;
390 
391  if (phase_count == c->phase_count)
392  return 0;
393 
394  av_assert0(!c->frac && !c->dst_incr_mod);
395 
396  new_filter_bank = av_calloc(c->filter_alloc, (phase_count + 1) * c->felem_size);
397  if (!new_filter_bank)
398  return AVERROR(ENOMEM);
399 
400  ret = build_filter(c, new_filter_bank, c->factor, c->filter_length, c->filter_alloc,
401  phase_count, 1 << c->filter_shift, c->filter_type, c->kaiser_beta);
402  if (ret < 0) {
403  av_freep(&new_filter_bank);
404  return ret;
405  }
406  memcpy(new_filter_bank + (c->filter_alloc*phase_count+1)*c->felem_size, new_filter_bank, (c->filter_alloc-1)*c->felem_size);
407  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);
408 
409  if (!av_reduce(&new_src_incr, &new_dst_incr, c->src_incr,
410  c->dst_incr * (int64_t)(phase_count/c->phase_count), INT32_MAX/2))
411  {
412  av_freep(&new_filter_bank);
413  return AVERROR(EINVAL);
414  }
415 
416  c->src_incr = new_src_incr;
417  c->dst_incr = new_dst_incr;
418  while (c->dst_incr < (1<<20) && c->src_incr < (1<<20)) {
419  c->dst_incr *= 2;
420  c->src_incr *= 2;
421  }
422  c->ideal_dst_incr = c->dst_incr;
423  c->dst_incr_div = c->dst_incr / c->src_incr;
424  c->dst_incr_mod = c->dst_incr % c->src_incr;
425  c->index *= phase_count / c->phase_count;
426  c->phase_count = phase_count;
427  av_freep(&c->filter_bank);
428  c->filter_bank = new_filter_bank;
429  return 0;
430 }
431 
432 static int set_compensation(ResampleContext *c, int sample_delta, int compensation_distance){
433  int ret;
434 
435  if (compensation_distance && sample_delta) {
437  if (ret < 0)
438  return ret;
439  }
440 
441  c->compensation_distance= compensation_distance;
442  if (compensation_distance)
443  c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr * (int64_t)sample_delta / compensation_distance;
444  else
445  c->dst_incr = c->ideal_dst_incr;
446 
447  c->dst_incr_div = c->dst_incr / c->src_incr;
448  c->dst_incr_mod = c->dst_incr % c->src_incr;
449 
450  return 0;
451 }
452 
453 static int multiple_resample(ResampleContext *c, AudioData *dst, int dst_size, AudioData *src, int src_size, int *consumed){
454  int i;
455  int av_unused mm_flags = av_get_cpu_flags();
456  int need_emms = c->format == AV_SAMPLE_FMT_S16P && ARCH_X86_32 &&
458  int64_t max_src_size = (INT64_MAX/2 / c->phase_count) / c->src_incr;
459 
460  if (c->compensation_distance)
461  dst_size = FFMIN(dst_size, c->compensation_distance);
462  src_size = FFMIN(src_size, max_src_size);
463 
464  *consumed = 0;
465 
466  if (c->filter_length == 1 && c->phase_count == 1) {
467  int64_t index2= (1LL<<32)*c->frac/c->src_incr + (1LL<<32)*c->index;
468  int64_t incr= (1LL<<32) * c->dst_incr / c->src_incr;
469  int new_size = (src_size * (int64_t)c->src_incr - c->frac + c->dst_incr - 1) / c->dst_incr;
470 
471  dst_size = FFMAX(FFMIN(dst_size, new_size), 0);
472  if (dst_size > 0) {
473  for (i = 0; i < dst->ch_count; i++) {
474  c->dsp.resample_one(dst->ch[i], src->ch[i], dst_size, index2, incr);
475  if (i+1 == dst->ch_count) {
476  c->index += dst_size * c->dst_incr_div;
477  c->index += (c->frac + dst_size * (int64_t)c->dst_incr_mod) / c->src_incr;
478  av_assert2(c->index >= 0);
479  *consumed = c->index;
480  c->frac = (c->frac + dst_size * (int64_t)c->dst_incr_mod) % c->src_incr;
481  c->index = 0;
482  }
483  }
484  }
485  } else {
486  int64_t end_index = (1LL + src_size - c->filter_length) * c->phase_count;
487  int64_t delta_frac = (end_index - c->index) * c->src_incr - c->frac;
488  int delta_n = (delta_frac + c->dst_incr - 1) / c->dst_incr;
489  int (*resample_func)(struct ResampleContext *c, void *dst,
490  const void *src, int n, int update_ctx);
491 
492  dst_size = FFMAX(FFMIN(dst_size, delta_n), 0);
493  if (dst_size > 0) {
494  /* resample_linear and resample_common should have same behavior
495  * when frac and dst_incr_mod are zero */
496  resample_func = (c->linear && (c->frac || c->dst_incr_mod)) ?
497  c->dsp.resample_linear : c->dsp.resample_common;
498  for (i = 0; i < dst->ch_count; i++)
499  *consumed = resample_func(c, dst->ch[i], src->ch[i], dst_size, i+1 == dst->ch_count);
500  }
501  }
502 
503  if(need_emms)
504  emms_c();
505 
506  if (c->compensation_distance) {
507  c->compensation_distance -= dst_size;
508  if (!c->compensation_distance) {
509  c->dst_incr = c->ideal_dst_incr;
510  c->dst_incr_div = c->dst_incr / c->src_incr;
511  c->dst_incr_mod = c->dst_incr % c->src_incr;
512  }
513  }
514 
515  return dst_size;
516 }
517 
518 static int64_t get_delay(struct SwrContext *s, int64_t base){
519  ResampleContext *c = s->resample;
520  int64_t num = s->in_buffer_count - (c->filter_length-1)/2;
521  num *= c->phase_count;
522  num -= c->index;
523  num *= c->src_incr;
524  num -= c->frac;
525  return av_rescale(num, base, s->in_sample_rate*(int64_t)c->src_incr * c->phase_count);
526 }
527 
528 static int64_t get_out_samples(struct SwrContext *s, int in_samples) {
529  ResampleContext *c = s->resample;
530  // The + 2 are added to allow implementations to be slightly inaccurate, they should not be needed currently.
531  // They also make it easier to proof that changes and optimizations do not
532  // break the upper bound.
533  int64_t num = s->in_buffer_count + 2LL + in_samples;
534  num *= c->phase_count;
535  num -= c->index;
536  num = av_rescale_rnd(num, s->out_sample_rate, ((int64_t)s->in_sample_rate) * c->phase_count, AV_ROUND_UP) + 2;
537 
538  if (c->compensation_distance) {
539  if (num > INT_MAX)
540  return AVERROR(EINVAL);
541 
542  num = FFMAX(num, (num * c->ideal_dst_incr - 1) / c->dst_incr + 1);
543  }
544  return num;
545 }
546 
547 static int resample_flush(struct SwrContext *s) {
548  ResampleContext *c = s->resample;
549  AudioData *a= &s->in_buffer;
550  int i, j, ret;
551  int reflection = (FFMIN(s->in_buffer_count, c->filter_length) + 1) / 2;
552 
553  if((ret = swri_realloc_audio(a, s->in_buffer_index + s->in_buffer_count + reflection)) < 0)
554  return ret;
555  av_assert0(a->planar);
556  for(i=0; i<a->ch_count; i++){
557  for(j=0; j<reflection; j++){
558  memcpy(a->ch[i] + (s->in_buffer_index+s->in_buffer_count+j )*a->bps,
559  a->ch[i] + (s->in_buffer_index+s->in_buffer_count-j-1)*a->bps, a->bps);
560  }
561  }
562  s->in_buffer_count += reflection;
563  return 0;
564 }
565 
566 // in fact the whole handle multiple ridiculously small buffers might need more thinking...
568  int in_count, int *out_idx, int *out_sz)
569 {
570  int n, ch, num = FFMIN(in_count + *out_sz, c->filter_length + 1), res;
571 
572  if (c->index >= 0)
573  return 0;
574 
575  if ((res = swri_realloc_audio(dst, c->filter_length * 2 + 1)) < 0)
576  return res;
577 
578  // copy
579  for (n = *out_sz; n < num; n++) {
580  for (ch = 0; ch < src->ch_count; ch++) {
581  memcpy(dst->ch[ch] + ((c->filter_length + n) * c->felem_size),
582  src->ch[ch] + ((n - *out_sz) * c->felem_size), c->felem_size);
583  }
584  }
585 
586  // if not enough data is in, return and wait for more
587  if (num < c->filter_length + 1) {
588  *out_sz = num;
589  *out_idx = c->filter_length;
590  return INT_MAX;
591  }
592 
593  // else invert
594  for (n = 1; n <= c->filter_length; n++) {
595  for (ch = 0; ch < src->ch_count; ch++) {
596  memcpy(dst->ch[ch] + ((c->filter_length - n) * c->felem_size),
597  dst->ch[ch] + ((c->filter_length + n) * c->felem_size),
598  c->felem_size);
599  }
600  }
601 
602  res = num - *out_sz;
603  *out_idx = c->filter_length;
604  while (c->index < 0) {
605  --*out_idx;
606  c->index += c->phase_count;
607  }
608  *out_sz = FFMAX(*out_sz + c->filter_length,
609  1 + c->filter_length * 2) - *out_idx;
610 
611  return FFMAX(res, 0);
612 }
613 
614 struct Resampler const swri_resampler={
620  get_delay,
623 };
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:69
q1
static const uint8_t q1[256]
Definition: twofish.c:96
r
const char * r
Definition: vf_curves.c:116
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
resample_free
static void resample_free(ResampleContext **cc)
Definition: resample.c:280
av_unused
#define av_unused
Definition: attributes.h:131
w
uint8_t w
Definition: llviddspenc.c:38
kaiser_beta
static float kaiser_beta(float att, float tr_bw)
Definition: asrc_sinc.c:134
AV_SAMPLE_FMT_S32P
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition: samplefmt.h:68
linear
static int linear(InterplayACMContext *s, unsigned ind, unsigned col)
Definition: interplayacm.c:131
LEN
#define LEN
base
uint8_t base
Definition: vp3data.h:141
filter
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
Definition: filter_design.txt:228
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
av_get_cpu_flags
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:98
AudioData
Definition: swresample_internal.h:45
SWR_FILTER_TYPE_BLACKMAN_NUTTALL
@ SWR_FILTER_TYPE_BLACKMAN_NUTTALL
Blackman Nuttall windowed sinc.
Definition: swresample.h:168
ResampleContext::filter_length
int filter_length
Definition: resample.h:33
fail
#define fail()
Definition: checkasm.h:127
swri_realloc_audio
int swri_realloc_audio(AudioData *a, int count)
Definition: swresample.c:400
tab
static const struct twinvq_data tab
Definition: twinvq_data.h:10345
scale
static av_always_inline float scale(float x, float s)
Definition: vf_v360.c:1388
AV_ROUND_UP
@ AV_ROUND_UP
Round toward +infinity.
Definition: mathematics.h:83
SWR_FILTER_TYPE_KAISER
@ SWR_FILTER_TYPE_KAISER
Kaiser windowed sinc.
Definition: swresample.h:169
ss
#define ss(width, name, subs,...)
Definition: cbs_vp9.c:261
av_reduce
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
avassert.h
ceil
static __device__ float ceil(float a)
Definition: cuda_runtime.h:176
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
s
#define s(width, name)
Definition: cbs_vp9.c:257
ResampleContext
Definition: resample.h:30
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
SwrContext
The libswresample context.
Definition: swresample_internal.h:95
AudioData::ch
uint8_t * ch[SWR_CH_MAX]
samples buffer per channel
Definition: swresample_internal.h:46
if
if(ret)
Definition: filter_design.txt:179
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
av_clip_int16
#define av_clip_int16
Definition: common.h:111
NULL
#define NULL
Definition: coverity.c:32
SwrFilterType
SwrFilterType
Resampling Filter Types.
Definition: swresample.h:166
bessel
static double bessel(double x)
0th order modified bessel function of the first kind.
Definition: resample.c:73
get_delay
static int64_t get_delay(struct SwrContext *s, int64_t base)
Definition: resample.c:518
src
#define src
Definition: vp8dsp.c:255
exp
int8_t exp
Definition: eval.c:72
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AudioData::ch_count
int ch_count
number of channels
Definition: swresample_internal.h:48
AV_CPU_FLAG_SSE2
#define AV_CPU_FLAG_SSE2
PIV SSE2 functions.
Definition: cpu.h:34
av_rescale_rnd
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:57
av_clipl_int32
#define av_clipl_int32
Definition: common.h:114
cpu.h
size
int size
Definition: twinvq_data.h:10344
SWR_FILTER_TYPE_CUBIC
@ SWR_FILTER_TYPE_CUBIC
Cubic.
Definition: swresample.h:167
format
ofilter format
Definition: ffmpeg_filter.c:172
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
rebuild_filter_bank_with_compensation
static int rebuild_filter_bank_with_compensation(ResampleContext *c)
Definition: resample.c:384
Resampler
Definition: swresample_internal.h:81
M_PI
#define M_PI
Definition: mathematics.h:52
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:67
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
AV_CPU_FLAG_MMX2
#define AV_CPU_FLAG_MMX2
SSE integer functions or AMD MMX ext.
Definition: cpu.h:31
lrintf
#define lrintf(x)
Definition: libm_mips.h:72
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
av_get_bytes_per_sample
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:106
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
invert_initial_buffer
static int invert_initial_buffer(ResampleContext *c, AudioData *dst, const AudioData *src, int in_count, int *out_idx, int *out_sz)
Definition: resample.c:567
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:263
av_rescale
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:128
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:271
resample.h
ret
ret
Definition: filter_design.txt:187
get_out_samples
static int64_t get_out_samples(struct SwrContext *s, int in_samples)
Definition: resample.c:528
set_compensation
static int set_compensation(ResampleContext *c, int sample_delta, int compensation_distance)
Definition: resample.c:432
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:70
factor
static const int factor[16]
Definition: vf_pp7.c:76
swri_resample_dsp_init
void swri_resample_dsp_init(ResampleContext *c)
Definition: resample_dsp.c:46
build_filter
static int build_filter(ResampleContext *c, void *filter, double factor, int tap_count, int alloc, int phase_count, int scale, int filter_type, double kaiser_beta)
builds a polyphase filterbank.
Definition: resample.c:145
resample_flush
static int resample_flush(struct SwrContext *s)
Definition: resample.c:547
multiple_resample
static int multiple_resample(ResampleContext *c, AudioData *dst, int dst_size, AudioData *src, int src_size, int *consumed)
Definition: resample.c:453
llrint
#define llrint(x)
Definition: libm.h:394
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
d
d
Definition: ffmpeg_filter.c:153
int32_t
int32_t
Definition: audioconvert.c:56
coeff
static const double coeff[2][5]
Definition: vf_owdenoise.c:78
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
resample_init
static ResampleContext * resample_init(ResampleContext *c, int out_rate, int in_rate, int filter_size, int phase_shift, int linear, double cutoff0, enum AVSampleFormat format, enum SwrFilterType filter_type, double kaiser_beta, double precision, int cheby, int exact_rational)
Definition: resample.c:288
eval_poly
static double eval_poly(const double *coeff, int size, double x)
Definition: resample.c:33
int
int
Definition: ffmpeg_filter.c:153
swri_resampler
struct Resampler const swri_resampler
Definition: resample.c:614