2 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/common.h"
23 #include "libavutil/libm.h"
24 #include "libavutil/log.h"
27 #include "audio_data.h"
29 struct ResampleContext {
30 AVAudioResampleContext *avr;
39 int compensation_distance;
43 enum AVResampleFilterType filter_type;
45 void (*set_filter)(void *filter, double *tab, int phase, int tap_count);
46 void (*resample_one)(struct ResampleContext *c, void *dst0,
47 int dst_index, const void *src0,
48 unsigned int index, int frac);
49 void (*resample_nearest)(void *dst0, int dst_index,
50 const void *src0, unsigned int index);
52 int initial_padding_filled;
53 int initial_padding_samples;
54 int final_padding_filled;
55 int final_padding_samples;
60 #define CONFIG_RESAMPLE_DBL
61 #include "resample_template.c"
62 #undef CONFIG_RESAMPLE_DBL
65 #define CONFIG_RESAMPLE_FLT
66 #include "resample_template.c"
67 #undef CONFIG_RESAMPLE_FLT
70 #define CONFIG_RESAMPLE_S32
71 #include "resample_template.c"
72 #undef CONFIG_RESAMPLE_S32
75 #include "resample_template.c"
78 /* 0th order modified bessel function of the first kind. */
79 static double bessel(double x)
87 for (i = 1; v != lastv; i++) {
95 /* Build a polyphase filterbank. */
96 static int build_filter(ResampleContext *c, double factor)
101 int tap_count = c->filter_length;
102 int phase_count = 1 << c->phase_shift;
103 const int center = (tap_count - 1) / 2;
105 tab = av_malloc(tap_count * sizeof(*tab));
107 return AVERROR(ENOMEM);
109 for (ph = 0; ph < phase_count; ph++) {
111 for (i = 0; i < tap_count; i++) {
112 x = M_PI * ((double)(i - center) - (double)ph / phase_count) * factor;
115 switch (c->filter_type) {
116 case AV_RESAMPLE_FILTER_TYPE_CUBIC: {
117 const float d = -0.5; //first order derivative = -0.5
118 x = fabs(((double)(i - center) - (double)ph / phase_count) * factor);
119 if (x < 1.0) y = 1 - 3 * x*x + 2 * x*x*x + d * ( -x*x + x*x*x);
120 else y = d * (-4 + 8 * x - 5 * x*x + x*x*x);
123 case AV_RESAMPLE_FILTER_TYPE_BLACKMAN_NUTTALL:
124 w = 2.0 * x / (factor * tap_count) + M_PI;
125 y *= 0.3635819 - 0.4891775 * cos( w) +
126 0.1365995 * cos(2 * w) -
127 0.0106411 * cos(3 * w);
129 case AV_RESAMPLE_FILTER_TYPE_KAISER:
130 w = 2.0 * x / (factor * tap_count * M_PI);
131 y *= bessel(c->kaiser_beta * sqrt(FFMAX(1 - w * w, 0)));
138 /* normalize so that an uniform color remains the same */
139 for (i = 0; i < tap_count; i++)
140 tab[i] = tab[i] / norm;
142 c->set_filter(c->filter_bank, tab, ph, tap_count);
149 ResampleContext *ff_audio_resample_init(AVAudioResampleContext *avr)
152 int out_rate = avr->out_sample_rate;
153 int in_rate = avr->in_sample_rate;
154 double factor = FFMIN(out_rate * avr->cutoff / in_rate, 1.0);
155 int phase_count = 1 << avr->phase_shift;
158 if (avr->internal_sample_fmt != AV_SAMPLE_FMT_S16P &&
159 avr->internal_sample_fmt != AV_SAMPLE_FMT_S32P &&
160 avr->internal_sample_fmt != AV_SAMPLE_FMT_FLTP &&
161 avr->internal_sample_fmt != AV_SAMPLE_FMT_DBLP) {
162 av_log(avr, AV_LOG_ERROR, "Unsupported internal format for "
164 av_get_sample_fmt_name(avr->internal_sample_fmt));
167 c = av_mallocz(sizeof(*c));
172 c->phase_shift = avr->phase_shift;
173 c->phase_mask = phase_count - 1;
174 c->linear = avr->linear_interp;
175 c->filter_length = FFMAX((int)ceil(avr->filter_size / factor), 1);
176 c->filter_type = avr->filter_type;
177 c->kaiser_beta = avr->kaiser_beta;
179 switch (avr->internal_sample_fmt) {
180 case AV_SAMPLE_FMT_DBLP:
181 c->resample_one = c->linear ? resample_linear_dbl : resample_one_dbl;
182 c->resample_nearest = resample_nearest_dbl;
183 c->set_filter = set_filter_dbl;
185 case AV_SAMPLE_FMT_FLTP:
186 c->resample_one = c->linear ? resample_linear_flt : resample_one_flt;
187 c->resample_nearest = resample_nearest_flt;
188 c->set_filter = set_filter_flt;
190 case AV_SAMPLE_FMT_S32P:
191 c->resample_one = c->linear ? resample_linear_s32 : resample_one_s32;
192 c->resample_nearest = resample_nearest_s32;
193 c->set_filter = set_filter_s32;
195 case AV_SAMPLE_FMT_S16P:
196 c->resample_one = c->linear ? resample_linear_s16 : resample_one_s16;
197 c->resample_nearest = resample_nearest_s16;
198 c->set_filter = set_filter_s16;
202 felem_size = av_get_bytes_per_sample(avr->internal_sample_fmt);
203 c->filter_bank = av_mallocz(c->filter_length * (phase_count + 1) * felem_size);
207 if (build_filter(c, factor) < 0)
210 memcpy(&c->filter_bank[(c->filter_length * phase_count + 1) * felem_size],
211 c->filter_bank, (c->filter_length - 1) * felem_size);
212 memcpy(&c->filter_bank[c->filter_length * phase_count * felem_size],
213 &c->filter_bank[(c->filter_length - 1) * felem_size], felem_size);
215 c->compensation_distance = 0;
216 if (!av_reduce(&c->src_incr, &c->dst_incr, out_rate,
217 in_rate * (int64_t)phase_count, INT32_MAX / 2))
219 c->ideal_dst_incr = c->dst_incr;
221 c->padding_size = (c->filter_length - 1) / 2;
222 c->initial_padding_filled = 0;
226 /* allocate internal buffer */
227 c->buffer = ff_audio_data_alloc(avr->resample_channels, c->padding_size,
228 avr->internal_sample_fmt,
232 c->buffer->nb_samples = c->padding_size;
233 c->initial_padding_samples = c->padding_size;
235 av_log(avr, AV_LOG_DEBUG, "resample: %s from %d Hz to %d Hz\n",
236 av_get_sample_fmt_name(avr->internal_sample_fmt),
237 avr->in_sample_rate, avr->out_sample_rate);
242 ff_audio_data_free(&c->buffer);
243 av_free(c->filter_bank);
248 void ff_audio_resample_free(ResampleContext **c)
252 ff_audio_data_free(&(*c)->buffer);
253 av_free((*c)->filter_bank);
257 int avresample_set_compensation(AVAudioResampleContext *avr, int sample_delta,
258 int compensation_distance)
261 AudioData *fifo_buf = NULL;
264 if (compensation_distance < 0)
265 return AVERROR(EINVAL);
266 if (!compensation_distance && sample_delta)
267 return AVERROR(EINVAL);
269 if (!avr->resample_needed) {
270 #if FF_API_RESAMPLE_CLOSE_OPEN
271 /* if resampling was not enabled previously, re-initialize the
272 AVAudioResampleContext and force resampling */
274 int restore_matrix = 0;
275 double matrix[AVRESAMPLE_MAX_CHANNELS * AVRESAMPLE_MAX_CHANNELS] = { 0 };
277 /* buffer any remaining samples in the output FIFO before closing */
278 fifo_samples = av_audio_fifo_size(avr->out_fifo);
279 if (fifo_samples > 0) {
280 fifo_buf = ff_audio_data_alloc(avr->out_channels, fifo_samples,
281 avr->out_sample_fmt, NULL);
283 return AVERROR(EINVAL);
284 ret = ff_audio_data_read_from_fifo(avr->out_fifo, fifo_buf,
289 /* save the channel mixing matrix */
291 ret = avresample_get_matrix(avr, matrix, AVRESAMPLE_MAX_CHANNELS);
297 /* close the AVAudioResampleContext */
298 avresample_close(avr);
300 avr->force_resampling = 1;
302 /* restore the channel mixing matrix */
303 if (restore_matrix) {
304 ret = avresample_set_matrix(avr, matrix, AVRESAMPLE_MAX_CHANNELS);
309 /* re-open the AVAudioResampleContext */
310 ret = avresample_open(avr);
314 /* restore buffered samples to the output FIFO */
315 if (fifo_samples > 0) {
316 ret = ff_audio_data_add_to_fifo(avr->out_fifo, fifo_buf, 0,
320 ff_audio_data_free(&fifo_buf);
323 av_log(avr, AV_LOG_ERROR, "Unable to set resampling compensation\n");
324 return AVERROR(EINVAL);
328 c->compensation_distance = compensation_distance;
329 if (compensation_distance) {
330 c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr *
331 (int64_t)sample_delta / compensation_distance;
333 c->dst_incr = c->ideal_dst_incr;
338 ff_audio_data_free(&fifo_buf);
342 static int resample(ResampleContext *c, void *dst, const void *src,
343 int *consumed, int src_size, int dst_size, int update_ctx,
344 int nearest_neighbour)
347 unsigned int index = c->index;
349 int dst_incr_frac = c->dst_incr % c->src_incr;
350 int dst_incr = c->dst_incr / c->src_incr;
351 int compensation_distance = c->compensation_distance;
354 return AVERROR(EINVAL);
356 if (nearest_neighbour) {
357 uint64_t index2 = ((uint64_t)index) << 32;
358 int64_t incr = (1LL << 32) * c->dst_incr / c->src_incr;
359 dst_size = FFMIN(dst_size,
360 (src_size-1-index) * (int64_t)c->src_incr /
364 for(dst_index = 0; dst_index < dst_size; dst_index++) {
365 c->resample_nearest(dst, dst_index, src, index2 >> 32);
369 dst_index = dst_size;
371 index += dst_index * dst_incr;
372 index += (frac + dst_index * (int64_t)dst_incr_frac) / c->src_incr;
373 frac = (frac + dst_index * (int64_t)dst_incr_frac) % c->src_incr;
375 for (dst_index = 0; dst_index < dst_size; dst_index++) {
376 int sample_index = index >> c->phase_shift;
378 if (sample_index + c->filter_length > src_size)
382 c->resample_one(c, dst, dst_index, src, index, frac);
384 frac += dst_incr_frac;
386 if (frac >= c->src_incr) {
390 if (dst_index + 1 == compensation_distance) {
391 compensation_distance = 0;
392 dst_incr_frac = c->ideal_dst_incr % c->src_incr;
393 dst_incr = c->ideal_dst_incr / c->src_incr;
398 *consumed = index >> c->phase_shift;
401 index &= c->phase_mask;
403 if (compensation_distance) {
404 compensation_distance -= dst_index;
405 if (compensation_distance <= 0)
410 c->dst_incr = dst_incr_frac + c->src_incr*dst_incr;
411 c->compensation_distance = compensation_distance;
417 int ff_audio_resample(ResampleContext *c, AudioData *dst, AudioData *src)
419 int ch, in_samples, in_leftover, consumed = 0, out_samples = 0;
420 int ret = AVERROR(EINVAL);
421 int nearest_neighbour = (c->compensation_distance == 0 &&
422 c->filter_length == 1 &&
423 c->phase_shift == 0);
425 in_samples = src ? src->nb_samples : 0;
426 in_leftover = c->buffer->nb_samples;
428 /* add input samples to the internal buffer */
430 ret = ff_audio_data_combine(c->buffer, in_leftover, src, 0, in_samples);
433 } else if (in_leftover <= c->final_padding_samples) {
434 /* no remaining samples to flush */
438 if (!c->initial_padding_filled) {
439 int bps = av_get_bytes_per_sample(c->avr->internal_sample_fmt);
442 if (src && c->buffer->nb_samples < 2 * c->padding_size)
445 for (i = 0; i < c->padding_size; i++)
446 for (ch = 0; ch < c->buffer->channels; ch++) {
447 if (c->buffer->nb_samples > 2 * c->padding_size - i) {
448 memcpy(c->buffer->data[ch] + bps * i,
449 c->buffer->data[ch] + bps * (2 * c->padding_size - i), bps);
451 memset(c->buffer->data[ch] + bps * i, 0, bps);
454 c->initial_padding_filled = 1;
457 if (!src && !c->final_padding_filled) {
458 int bps = av_get_bytes_per_sample(c->avr->internal_sample_fmt);
461 ret = ff_audio_data_realloc(c->buffer, in_samples + c->padding_size);
463 av_log(c->avr, AV_LOG_ERROR, "Error reallocating resampling buffer\n");
464 return AVERROR(ENOMEM);
467 for (i = 0; i < c->padding_size; i++)
468 for (ch = 0; ch < c->buffer->channels; ch++) {
469 if (in_leftover > i) {
470 memcpy(c->buffer->data[ch] + bps * (in_leftover + i),
471 c->buffer->data[ch] + bps * (in_leftover - i - 1),
474 memset(c->buffer->data[ch] + bps * (in_leftover + i),
478 c->buffer->nb_samples += c->padding_size;
479 c->final_padding_samples = c->padding_size;
480 c->final_padding_filled = 1;
484 /* calculate output size and reallocate output buffer if needed */
485 /* TODO: try to calculate this without the dummy resample() run */
486 if (!dst->read_only && dst->allow_realloc) {
487 out_samples = resample(c, NULL, NULL, NULL, c->buffer->nb_samples,
488 INT_MAX, 0, nearest_neighbour);
489 ret = ff_audio_data_realloc(dst, out_samples);
491 av_log(c->avr, AV_LOG_ERROR, "error reallocating output\n");
496 /* resample each channel plane */
497 for (ch = 0; ch < c->buffer->channels; ch++) {
498 out_samples = resample(c, (void *)dst->data[ch],
499 (const void *)c->buffer->data[ch], &consumed,
500 c->buffer->nb_samples, dst->allocated_samples,
501 ch + 1 == c->buffer->channels, nearest_neighbour);
503 if (out_samples < 0) {
504 av_log(c->avr, AV_LOG_ERROR, "error during resampling\n");
508 /* drain consumed samples from the internal buffer */
509 ff_audio_data_drain(c->buffer, consumed);
510 c->initial_padding_samples = FFMAX(c->initial_padding_samples - consumed, 0);
512 av_dlog(c->avr, "resampled %d in + %d leftover to %d out + %d leftover\n",
513 in_samples, in_leftover, out_samples, c->buffer->nb_samples);
515 dst->nb_samples = out_samples;
519 int avresample_get_delay(AVAudioResampleContext *avr)
521 ResampleContext *c = avr->resample;
523 if (!avr->resample_needed || !avr->resample)
526 return FFMAX(c->buffer->nb_samples - c->padding_size, 0);