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 Libav.
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.
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.
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
22 #include "libavutil/common.h"
23 #include "libavutil/libm.h"
24 #include "libavutil/log.h"
27 #include "audio_data.h"
31 #define CONFIG_RESAMPLE_DBL
32 #include "resample_template.c"
33 #undef CONFIG_RESAMPLE_DBL
36 #define CONFIG_RESAMPLE_FLT
37 #include "resample_template.c"
38 #undef CONFIG_RESAMPLE_FLT
41 #define CONFIG_RESAMPLE_S32
42 #include "resample_template.c"
43 #undef CONFIG_RESAMPLE_S32
46 #include "resample_template.c"
49 /* 0th order modified bessel function of the first kind. */
50 static double bessel(double x)
58 for (i = 1; v != lastv; i++) {
66 /* Build a polyphase filterbank. */
67 static int build_filter(ResampleContext *c, double factor)
72 int tap_count = c->filter_length;
73 int phase_count = 1 << c->phase_shift;
74 const int center = (tap_count - 1) / 2;
76 tab = av_malloc(tap_count * sizeof(*tab));
78 return AVERROR(ENOMEM);
80 for (ph = 0; ph < phase_count; ph++) {
82 for (i = 0; i < tap_count; i++) {
83 x = M_PI * ((double)(i - center) - (double)ph / phase_count) * factor;
86 switch (c->filter_type) {
87 case AV_RESAMPLE_FILTER_TYPE_CUBIC: {
88 const float d = -0.5; //first order derivative = -0.5
89 x = fabs(((double)(i - center) - (double)ph / phase_count) * factor);
90 if (x < 1.0) y = 1 - 3 * x*x + 2 * x*x*x + d * ( -x*x + x*x*x);
91 else y = d * (-4 + 8 * x - 5 * x*x + x*x*x);
94 case AV_RESAMPLE_FILTER_TYPE_BLACKMAN_NUTTALL:
95 w = 2.0 * x / (factor * tap_count) + M_PI;
96 y *= 0.3635819 - 0.4891775 * cos( w) +
97 0.1365995 * cos(2 * w) -
98 0.0106411 * cos(3 * w);
100 case AV_RESAMPLE_FILTER_TYPE_KAISER:
101 w = 2.0 * x / (factor * tap_count * M_PI);
102 y *= bessel(c->kaiser_beta * sqrt(FFMAX(1 - w * w, 0)));
109 /* normalize so that an uniform color remains the same */
110 for (i = 0; i < tap_count; i++)
111 tab[i] = tab[i] / norm;
113 c->set_filter(c->filter_bank, tab, ph, tap_count);
120 ResampleContext *ff_audio_resample_init(AVAudioResampleContext *avr)
123 int out_rate = avr->out_sample_rate;
124 int in_rate = avr->in_sample_rate;
125 double factor = FFMIN(out_rate * avr->cutoff / in_rate, 1.0);
126 int phase_count = 1 << avr->phase_shift;
129 if (avr->internal_sample_fmt != AV_SAMPLE_FMT_S16P &&
130 avr->internal_sample_fmt != AV_SAMPLE_FMT_S32P &&
131 avr->internal_sample_fmt != AV_SAMPLE_FMT_FLTP &&
132 avr->internal_sample_fmt != AV_SAMPLE_FMT_DBLP) {
133 av_log(avr, AV_LOG_ERROR, "Unsupported internal format for "
135 av_get_sample_fmt_name(avr->internal_sample_fmt));
138 c = av_mallocz(sizeof(*c));
143 c->phase_shift = avr->phase_shift;
144 c->phase_mask = phase_count - 1;
145 c->linear = avr->linear_interp;
146 c->filter_length = FFMAX((int)ceil(avr->filter_size / factor), 1);
147 c->filter_type = avr->filter_type;
148 c->kaiser_beta = avr->kaiser_beta;
150 switch (avr->internal_sample_fmt) {
151 case AV_SAMPLE_FMT_DBLP:
152 c->resample_one = c->linear ? resample_linear_dbl : resample_one_dbl;
153 c->resample_nearest = resample_nearest_dbl;
154 c->set_filter = set_filter_dbl;
156 case AV_SAMPLE_FMT_FLTP:
157 c->resample_one = c->linear ? resample_linear_flt : resample_one_flt;
158 c->resample_nearest = resample_nearest_flt;
159 c->set_filter = set_filter_flt;
161 case AV_SAMPLE_FMT_S32P:
162 c->resample_one = c->linear ? resample_linear_s32 : resample_one_s32;
163 c->resample_nearest = resample_nearest_s32;
164 c->set_filter = set_filter_s32;
166 case AV_SAMPLE_FMT_S16P:
167 c->resample_one = c->linear ? resample_linear_s16 : resample_one_s16;
168 c->resample_nearest = resample_nearest_s16;
169 c->set_filter = set_filter_s16;
174 ff_audio_resample_init_aarch64(c, avr->internal_sample_fmt);
176 ff_audio_resample_init_arm(c, avr->internal_sample_fmt);
178 felem_size = av_get_bytes_per_sample(avr->internal_sample_fmt);
179 c->filter_bank = av_mallocz(c->filter_length * (phase_count + 1) * felem_size);
183 if (build_filter(c, factor) < 0)
186 memcpy(&c->filter_bank[(c->filter_length * phase_count + 1) * felem_size],
187 c->filter_bank, (c->filter_length - 1) * felem_size);
188 memcpy(&c->filter_bank[c->filter_length * phase_count * felem_size],
189 &c->filter_bank[(c->filter_length - 1) * felem_size], felem_size);
191 c->compensation_distance = 0;
192 if (!av_reduce(&c->src_incr, &c->dst_incr, out_rate,
193 in_rate * (int64_t)phase_count, INT32_MAX / 2))
195 c->ideal_dst_incr = c->dst_incr;
197 c->padding_size = (c->filter_length - 1) / 2;
198 c->initial_padding_filled = 0;
202 /* allocate internal buffer */
203 c->buffer = ff_audio_data_alloc(avr->resample_channels, c->padding_size,
204 avr->internal_sample_fmt,
208 c->buffer->nb_samples = c->padding_size;
209 c->initial_padding_samples = c->padding_size;
211 av_log(avr, AV_LOG_DEBUG, "resample: %s from %d Hz to %d Hz\n",
212 av_get_sample_fmt_name(avr->internal_sample_fmt),
213 avr->in_sample_rate, avr->out_sample_rate);
218 ff_audio_data_free(&c->buffer);
219 av_free(c->filter_bank);
224 void ff_audio_resample_free(ResampleContext **c)
228 ff_audio_data_free(&(*c)->buffer);
229 av_free((*c)->filter_bank);
233 int avresample_set_compensation(AVAudioResampleContext *avr, int sample_delta,
234 int compensation_distance)
237 AudioData *fifo_buf = NULL;
239 if (compensation_distance < 0)
240 return AVERROR(EINVAL);
241 if (!compensation_distance && sample_delta)
242 return AVERROR(EINVAL);
244 if (!avr->resample_needed) {
245 av_log(avr, AV_LOG_ERROR, "Unable to set resampling compensation\n");
246 return AVERROR(EINVAL);
249 c->compensation_distance = compensation_distance;
250 if (compensation_distance) {
251 c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr *
252 (int64_t)sample_delta / compensation_distance;
254 c->dst_incr = c->ideal_dst_incr;
260 static int resample(ResampleContext *c, void *dst, const void *src,
261 int *consumed, int src_size, int dst_size, int update_ctx,
262 int nearest_neighbour)
265 unsigned int index = c->index;
267 int dst_incr_frac = c->dst_incr % c->src_incr;
268 int dst_incr = c->dst_incr / c->src_incr;
269 int compensation_distance = c->compensation_distance;
272 return AVERROR(EINVAL);
274 if (nearest_neighbour) {
275 uint64_t index2 = ((uint64_t)index) << 32;
276 int64_t incr = (1LL << 32) * c->dst_incr / c->src_incr;
277 dst_size = FFMIN(dst_size,
278 (src_size-1-index) * (int64_t)c->src_incr /
282 for(dst_index = 0; dst_index < dst_size; dst_index++) {
283 c->resample_nearest(dst, dst_index, src, index2 >> 32);
287 dst_index = dst_size;
289 index += dst_index * dst_incr;
290 index += (frac + dst_index * (int64_t)dst_incr_frac) / c->src_incr;
291 frac = (frac + dst_index * (int64_t)dst_incr_frac) % c->src_incr;
293 for (dst_index = 0; dst_index < dst_size; dst_index++) {
294 int sample_index = index >> c->phase_shift;
296 if (sample_index + c->filter_length > src_size)
300 c->resample_one(c, dst, dst_index, src, index, frac);
302 frac += dst_incr_frac;
304 if (frac >= c->src_incr) {
308 if (dst_index + 1 == compensation_distance) {
309 compensation_distance = 0;
310 dst_incr_frac = c->ideal_dst_incr % c->src_incr;
311 dst_incr = c->ideal_dst_incr / c->src_incr;
316 *consumed = index >> c->phase_shift;
319 index &= c->phase_mask;
321 if (compensation_distance) {
322 compensation_distance -= dst_index;
323 if (compensation_distance <= 0)
328 c->dst_incr = dst_incr_frac + c->src_incr*dst_incr;
329 c->compensation_distance = compensation_distance;
335 int ff_audio_resample(ResampleContext *c, AudioData *dst, AudioData *src)
337 int ch, in_samples, in_leftover, consumed = 0, out_samples = 0;
338 int ret = AVERROR(EINVAL);
339 int nearest_neighbour = (c->compensation_distance == 0 &&
340 c->filter_length == 1 &&
341 c->phase_shift == 0);
343 in_samples = src ? src->nb_samples : 0;
344 in_leftover = c->buffer->nb_samples;
346 /* add input samples to the internal buffer */
348 ret = ff_audio_data_combine(c->buffer, in_leftover, src, 0, in_samples);
351 } else if (in_leftover <= c->final_padding_samples) {
352 /* no remaining samples to flush */
356 if (!c->initial_padding_filled) {
357 int bps = av_get_bytes_per_sample(c->avr->internal_sample_fmt);
360 if (src && c->buffer->nb_samples < 2 * c->padding_size)
363 for (i = 0; i < c->padding_size; i++)
364 for (ch = 0; ch < c->buffer->channels; ch++) {
365 if (c->buffer->nb_samples > 2 * c->padding_size - i) {
366 memcpy(c->buffer->data[ch] + bps * i,
367 c->buffer->data[ch] + bps * (2 * c->padding_size - i), bps);
369 memset(c->buffer->data[ch] + bps * i, 0, bps);
372 c->initial_padding_filled = 1;
375 if (!src && !c->final_padding_filled) {
376 int bps = av_get_bytes_per_sample(c->avr->internal_sample_fmt);
379 ret = ff_audio_data_realloc(c->buffer,
380 FFMAX(in_samples, in_leftover) +
383 av_log(c->avr, AV_LOG_ERROR, "Error reallocating resampling buffer\n");
384 return AVERROR(ENOMEM);
387 for (i = 0; i < c->padding_size; i++)
388 for (ch = 0; ch < c->buffer->channels; ch++) {
389 if (in_leftover > i) {
390 memcpy(c->buffer->data[ch] + bps * (in_leftover + i),
391 c->buffer->data[ch] + bps * (in_leftover - i - 1),
394 memset(c->buffer->data[ch] + bps * (in_leftover + i),
398 c->buffer->nb_samples += c->padding_size;
399 c->final_padding_samples = c->padding_size;
400 c->final_padding_filled = 1;
404 /* calculate output size and reallocate output buffer if needed */
405 /* TODO: try to calculate this without the dummy resample() run */
406 if (!dst->read_only && dst->allow_realloc) {
407 out_samples = resample(c, NULL, NULL, NULL, c->buffer->nb_samples,
408 INT_MAX, 0, nearest_neighbour);
409 ret = ff_audio_data_realloc(dst, out_samples);
411 av_log(c->avr, AV_LOG_ERROR, "error reallocating output\n");
416 /* resample each channel plane */
417 for (ch = 0; ch < c->buffer->channels; ch++) {
418 out_samples = resample(c, (void *)dst->data[ch],
419 (const void *)c->buffer->data[ch], &consumed,
420 c->buffer->nb_samples, dst->allocated_samples,
421 ch + 1 == c->buffer->channels, nearest_neighbour);
423 if (out_samples < 0) {
424 av_log(c->avr, AV_LOG_ERROR, "error during resampling\n");
428 /* drain consumed samples from the internal buffer */
429 ff_audio_data_drain(c->buffer, consumed);
430 c->initial_padding_samples = FFMAX(c->initial_padding_samples - consumed, 0);
432 av_log(c->avr, AV_LOG_TRACE, "resampled %d in + %d leftover to %d out + %d leftover\n",
433 in_samples, in_leftover, out_samples, c->buffer->nb_samples);
435 dst->nb_samples = out_samples;
439 int avresample_get_delay(AVAudioResampleContext *avr)
441 ResampleContext *c = avr->resample;
443 if (!avr->resample_needed || !avr->resample)
446 return FFMAX(c->buffer->nb_samples - c->padding_size, 0);