resample.c
Go to the documentation of this file.
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 "audio_data.h"
27 
28 struct ResampleContext {
34  int dst_incr;
35  int index;
36  int frac;
37  int src_incr;
41  int linear;
44  double factor;
45  void (*set_filter)(void *filter, double *tab, int phase, int tap_count);
46  void (*resample_one)(struct ResampleContext *c, int no_filter, void *dst0,
47  int dst_index, const void *src0, int src_size,
48  int index, int frac);
50 };
51 
52 
53 /* double template */
54 #define CONFIG_RESAMPLE_DBL
55 #include "resample_template.c"
56 #undef CONFIG_RESAMPLE_DBL
57 
58 /* float template */
59 #define CONFIG_RESAMPLE_FLT
60 #include "resample_template.c"
61 #undef CONFIG_RESAMPLE_FLT
62 
63 /* s32 template */
64 #define CONFIG_RESAMPLE_S32
65 #include "resample_template.c"
66 #undef CONFIG_RESAMPLE_S32
67 
68 /* s16 template */
69 #include "resample_template.c"
70 
71 
72 /* 0th order modified bessel function of the first kind. */
73 static double bessel(double x)
74 {
75  double v = 1;
76  double lastv = 0;
77  double t = 1;
78  int i;
79 
80  x = x * x / 4;
81  for (i = 1; v != lastv; i++) {
82  lastv = v;
83  t *= x / (i * i);
84  v += t;
85  }
86  return v;
87 }
88 
89 /* Build a polyphase filterbank. */
91 {
92  int ph, i;
93  double x, y, w, factor;
94  double *tab;
95  int tap_count = c->filter_length;
96  int phase_count = 1 << c->phase_shift;
97  const int center = (tap_count - 1) / 2;
98 
99  tab = av_malloc(tap_count * sizeof(*tab));
100  if (!tab)
101  return AVERROR(ENOMEM);
102 
103  /* if upsampling, only need to interpolate, no filter */
104  factor = FFMIN(c->factor, 1.0);
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;
112  switch (c->filter_type) {
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);
128  y *= bessel(c->kaiser_beta * sqrt(FFMAX(1 - w * w, 0)));
129  break;
130  }
131 
132  tab[i] = y;
133  norm += y;
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 
139  c->set_filter(c->filter_bank, tab, ph, tap_count);
140  }
141 
142  av_free(tab);
143  return 0;
144 }
145 
147 {
148  ResampleContext *c;
149  int out_rate = avr->out_sample_rate;
150  int in_rate = avr->in_sample_rate;
151  double factor = FFMIN(out_rate * avr->cutoff / in_rate, 1.0);
152  int phase_count = 1 << avr->phase_shift;
153  int felem_size;
154 
159  av_log(avr, AV_LOG_ERROR, "Unsupported internal format for "
160  "resampling: %s\n",
162  return NULL;
163  }
164  c = av_mallocz(sizeof(*c));
165  if (!c)
166  return NULL;
167 
168  c->avr = avr;
169  c->phase_shift = avr->phase_shift;
170  c->phase_mask = phase_count - 1;
171  c->linear = avr->linear_interp;
172  c->factor = factor;
173  c->filter_length = FFMAX((int)ceil(avr->filter_size / factor), 1);
174  c->filter_type = avr->filter_type;
175  c->kaiser_beta = avr->kaiser_beta;
176 
177  switch (avr->internal_sample_fmt) {
178  case AV_SAMPLE_FMT_DBLP:
179  c->resample_one = resample_one_dbl;
180  c->set_filter = set_filter_dbl;
181  break;
182  case AV_SAMPLE_FMT_FLTP:
183  c->resample_one = resample_one_flt;
184  c->set_filter = set_filter_flt;
185  break;
186  case AV_SAMPLE_FMT_S32P:
187  c->resample_one = resample_one_s32;
188  c->set_filter = set_filter_s32;
189  break;
190  case AV_SAMPLE_FMT_S16P:
191  c->resample_one = resample_one_s16;
192  c->set_filter = set_filter_s16;
193  break;
194  }
195 
196  felem_size = av_get_bytes_per_sample(avr->internal_sample_fmt);
197  c->filter_bank = av_mallocz(c->filter_length * (phase_count + 1) * felem_size);
198  if (!c->filter_bank)
199  goto error;
200 
201  if (build_filter(c) < 0)
202  goto error;
203 
204  memcpy(&c->filter_bank[(c->filter_length * phase_count + 1) * felem_size],
205  c->filter_bank, (c->filter_length - 1) * felem_size);
206  memcpy(&c->filter_bank[c->filter_length * phase_count * felem_size],
207  &c->filter_bank[(c->filter_length - 1) * felem_size], felem_size);
208 
209  c->compensation_distance = 0;
210  if (!av_reduce(&c->src_incr, &c->dst_incr, out_rate,
211  in_rate * (int64_t)phase_count, INT32_MAX / 2))
212  goto error;
213  c->ideal_dst_incr = c->dst_incr;
214 
215  c->padding_size = (c->filter_length - 1) / 2;
216  c->index = -phase_count * ((c->filter_length - 1) / 2);
217  c->frac = 0;
218 
219  /* allocate internal buffer */
221  avr->internal_sample_fmt,
222  "resample buffer");
223  if (!c->buffer)
224  goto error;
225 
226  av_log(avr, AV_LOG_DEBUG, "resample: %s from %d Hz to %d Hz\n",
228  avr->in_sample_rate, avr->out_sample_rate);
229 
230  return c;
231 
232 error:
234  av_free(c->filter_bank);
235  av_free(c);
236  return NULL;
237 }
238 
240 {
241  if (!*c)
242  return;
243  ff_audio_data_free(&(*c)->buffer);
244  av_free((*c)->filter_bank);
245  av_freep(c);
246 }
247 
250 {
251  ResampleContext *c;
252  AudioData *fifo_buf = NULL;
253  int ret = 0;
254 
255  if (compensation_distance < 0)
256  return AVERROR(EINVAL);
257  if (!compensation_distance && sample_delta)
258  return AVERROR(EINVAL);
259 
260  if (!avr->resample_needed) {
261 #if FF_API_RESAMPLE_CLOSE_OPEN
262  /* if resampling was not enabled previously, re-initialize the
263  AVAudioResampleContext and force resampling */
264  int fifo_samples;
265  int restore_matrix = 0;
266  double matrix[AVRESAMPLE_MAX_CHANNELS * AVRESAMPLE_MAX_CHANNELS] = { 0 };
267 
268  /* buffer any remaining samples in the output FIFO before closing */
269  fifo_samples = av_audio_fifo_size(avr->out_fifo);
270  if (fifo_samples > 0) {
271  fifo_buf = ff_audio_data_alloc(avr->out_channels, fifo_samples,
272  avr->out_sample_fmt, NULL);
273  if (!fifo_buf)
274  return AVERROR(EINVAL);
275  ret = ff_audio_data_read_from_fifo(avr->out_fifo, fifo_buf,
276  fifo_samples);
277  if (ret < 0)
278  goto reinit_fail;
279  }
280  /* save the channel mixing matrix */
281  if (avr->am) {
283  if (ret < 0)
284  goto reinit_fail;
285  restore_matrix = 1;
286  }
287 
288  /* close the AVAudioResampleContext */
289  avresample_close(avr);
290 
291  avr->force_resampling = 1;
292 
293  /* restore the channel mixing matrix */
294  if (restore_matrix) {
296  if (ret < 0)
297  goto reinit_fail;
298  }
299 
300  /* re-open the AVAudioResampleContext */
301  ret = avresample_open(avr);
302  if (ret < 0)
303  goto reinit_fail;
304 
305  /* restore buffered samples to the output FIFO */
306  if (fifo_samples > 0) {
307  ret = ff_audio_data_add_to_fifo(avr->out_fifo, fifo_buf, 0,
308  fifo_samples);
309  if (ret < 0)
310  goto reinit_fail;
311  ff_audio_data_free(&fifo_buf);
312  }
313 #else
314  av_log(avr, AV_LOG_ERROR, "Unable to set resampling compensation\n");
315  return AVERROR(EINVAL);
316 #endif
317  }
318  c = avr->resample;
320  if (compensation_distance) {
321  c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr *
322  (int64_t)sample_delta / compensation_distance;
323  } else {
324  c->dst_incr = c->ideal_dst_incr;
325  }
326  return 0;
327 
328 reinit_fail:
329  ff_audio_data_free(&fifo_buf);
330  return ret;
331 }
332 
333 static int resample(ResampleContext *c, void *dst, const void *src,
334  int *consumed, int src_size, int dst_size, int update_ctx)
335 {
336  int dst_index;
337  int index = c->index;
338  int frac = c->frac;
339  int dst_incr_frac = c->dst_incr % c->src_incr;
340  int dst_incr = c->dst_incr / c->src_incr;
342 
343  if (!dst != !src)
344  return AVERROR(EINVAL);
345 
346  if (compensation_distance == 0 && c->filter_length == 1 &&
347  c->phase_shift == 0) {
348  int64_t index2 = ((int64_t)index) << 32;
349  int64_t incr = (1LL << 32) * c->dst_incr / c->src_incr;
350  dst_size = FFMIN(dst_size,
351  (src_size-1-index) * (int64_t)c->src_incr /
352  c->dst_incr);
353 
354  if (dst) {
355  for(dst_index = 0; dst_index < dst_size; dst_index++) {
356  c->resample_one(c, 1, dst, dst_index, src, 0, index2 >> 32, 0);
357  index2 += incr;
358  }
359  } else {
360  dst_index = dst_size;
361  }
362  index += dst_index * dst_incr;
363  index += (frac + dst_index * (int64_t)dst_incr_frac) / c->src_incr;
364  frac = (frac + dst_index * (int64_t)dst_incr_frac) % c->src_incr;
365  } else {
366  for (dst_index = 0; dst_index < dst_size; dst_index++) {
367  int sample_index = index >> c->phase_shift;
368 
369  if (sample_index + c->filter_length > src_size ||
370  -sample_index >= src_size)
371  break;
372 
373  if (dst)
374  c->resample_one(c, 0, dst, dst_index, src, src_size, index, frac);
375 
376  frac += dst_incr_frac;
377  index += dst_incr;
378  if (frac >= c->src_incr) {
379  frac -= c->src_incr;
380  index++;
381  }
382  if (dst_index + 1 == compensation_distance) {
383  compensation_distance = 0;
384  dst_incr_frac = c->ideal_dst_incr % c->src_incr;
385  dst_incr = c->ideal_dst_incr / c->src_incr;
386  }
387  }
388  }
389  if (consumed)
390  *consumed = FFMAX(index, 0) >> c->phase_shift;
391 
392  if (update_ctx) {
393  if (index >= 0)
394  index &= c->phase_mask;
395 
396  if (compensation_distance) {
397  compensation_distance -= dst_index;
398  if (compensation_distance <= 0)
399  return AVERROR_BUG;
400  }
401  c->frac = frac;
402  c->index = index;
403  c->dst_incr = dst_incr_frac + c->src_incr*dst_incr;
405  }
406 
407  return dst_index;
408 }
409 
411 {
412  int ch, in_samples, in_leftover, consumed = 0, out_samples = 0;
413  int ret = AVERROR(EINVAL);
414 
415  in_samples = src ? src->nb_samples : 0;
416  in_leftover = c->buffer->nb_samples;
417 
418  /* add input samples to the internal buffer */
419  if (src) {
420  ret = ff_audio_data_combine(c->buffer, in_leftover, src, 0, in_samples);
421  if (ret < 0)
422  return ret;
423  } else if (!in_leftover) {
424  /* no remaining samples to flush */
425  return 0;
426  } else {
427  /* TODO: pad buffer to flush completely */
428  }
429 
430  /* calculate output size and reallocate output buffer if needed */
431  /* TODO: try to calculate this without the dummy resample() run */
432  if (!dst->read_only && dst->allow_realloc) {
433  out_samples = resample(c, NULL, NULL, NULL, c->buffer->nb_samples,
434  INT_MAX, 0);
435  ret = ff_audio_data_realloc(dst, out_samples);
436  if (ret < 0) {
437  av_log(c->avr, AV_LOG_ERROR, "error reallocating output\n");
438  return ret;
439  }
440  }
441 
442  /* resample each channel plane */
443  for (ch = 0; ch < c->buffer->channels; ch++) {
444  out_samples = resample(c, (void *)dst->data[ch],
445  (const void *)c->buffer->data[ch], &consumed,
447  ch + 1 == c->buffer->channels);
448  }
449  if (out_samples < 0) {
450  av_log(c->avr, AV_LOG_ERROR, "error during resampling\n");
451  return out_samples;
452  }
453 
454  /* drain consumed samples from the internal buffer */
455  ff_audio_data_drain(c->buffer, consumed);
456 
457  av_dlog(c->avr, "resampled %d in + %d leftover to %d out + %d leftover\n",
458  in_samples, in_leftover, out_samples, c->buffer->nb_samples);
459 
460  dst->nb_samples = out_samples;
461  return 0;
462 }
463 
465 {
466  ResampleContext *c = avr->resample;
467 
468  if (!avr->resample_needed || !avr->resample)
469  return 0;
470 
471  return FFMAX(c->buffer->nb_samples - c->padding_size, 0);
472 }