2012-03-23 21:42:17 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
|
|
|
|
* Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
|
|
|
|
*
|
2013-08-15 21:12:51 +00:00
|
|
|
* This file is part of FFmpeg.
|
2012-03-23 21:42:17 +00:00
|
|
|
*
|
2013-08-15 21:12:51 +00:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2012-03-23 21:42:17 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2013-08-15 21:12:51 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2012-03-23 21:42:17 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2013-08-15 21:12:51 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2012-03-23 21:42:17 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2012-08-06 13:49:32 +00:00
|
|
|
#include "libavutil/common.h"
|
2012-03-23 21:42:17 +00:00
|
|
|
#include "libavutil/libm.h"
|
|
|
|
#include "libavutil/log.h"
|
|
|
|
#include "internal.h"
|
2012-12-21 21:26:51 +00:00
|
|
|
#include "resample.h"
|
2012-03-23 21:42:17 +00:00
|
|
|
#include "audio_data.h"
|
|
|
|
|
2012-05-28 01:44:55 +00:00
|
|
|
|
|
|
|
/* double template */
|
|
|
|
#define CONFIG_RESAMPLE_DBL
|
|
|
|
#include "resample_template.c"
|
|
|
|
#undef CONFIG_RESAMPLE_DBL
|
|
|
|
|
|
|
|
/* float template */
|
|
|
|
#define CONFIG_RESAMPLE_FLT
|
|
|
|
#include "resample_template.c"
|
|
|
|
#undef CONFIG_RESAMPLE_FLT
|
|
|
|
|
|
|
|
/* s32 template */
|
|
|
|
#define CONFIG_RESAMPLE_S32
|
|
|
|
#include "resample_template.c"
|
|
|
|
#undef CONFIG_RESAMPLE_S32
|
|
|
|
|
|
|
|
/* s16 template */
|
|
|
|
#include "resample_template.c"
|
|
|
|
|
|
|
|
|
2012-08-09 01:06:23 +00:00
|
|
|
/* 0th order modified bessel function of the first kind. */
|
2012-03-23 21:42:17 +00:00
|
|
|
static double bessel(double x)
|
|
|
|
{
|
|
|
|
double v = 1;
|
|
|
|
double lastv = 0;
|
|
|
|
double t = 1;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
x = x * x / 4;
|
|
|
|
for (i = 1; v != lastv; i++) {
|
|
|
|
lastv = v;
|
|
|
|
t *= x / (i * i);
|
|
|
|
v += t;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2012-08-09 01:06:23 +00:00
|
|
|
/* Build a polyphase filterbank. */
|
2014-02-22 19:27:44 +00:00
|
|
|
static int build_filter(ResampleContext *c, double factor)
|
2012-03-23 21:42:17 +00:00
|
|
|
{
|
|
|
|
int ph, i;
|
2014-02-22 19:27:44 +00:00
|
|
|
double x, y, w;
|
2012-03-23 21:42:17 +00:00
|
|
|
double *tab;
|
2012-05-28 01:44:55 +00:00
|
|
|
int tap_count = c->filter_length;
|
|
|
|
int phase_count = 1 << c->phase_shift;
|
2012-03-23 21:42:17 +00:00
|
|
|
const int center = (tap_count - 1) / 2;
|
|
|
|
|
|
|
|
tab = av_malloc(tap_count * sizeof(*tab));
|
|
|
|
if (!tab)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
for (ph = 0; ph < phase_count; ph++) {
|
|
|
|
double norm = 0;
|
|
|
|
for (i = 0; i < tap_count; i++) {
|
|
|
|
x = M_PI * ((double)(i - center) - (double)ph / phase_count) * factor;
|
|
|
|
if (x == 0) y = 1.0;
|
|
|
|
else y = sin(x) / x;
|
2012-05-28 01:44:55 +00:00
|
|
|
switch (c->filter_type) {
|
2012-05-26 18:50:02 +00:00
|
|
|
case AV_RESAMPLE_FILTER_TYPE_CUBIC: {
|
2012-03-23 21:42:17 +00:00
|
|
|
const float d = -0.5; //first order derivative = -0.5
|
|
|
|
x = fabs(((double)(i - center) - (double)ph / phase_count) * factor);
|
|
|
|
if (x < 1.0) y = 1 - 3 * x*x + 2 * x*x*x + d * ( -x*x + x*x*x);
|
|
|
|
else y = d * (-4 + 8 * x - 5 * x*x + x*x*x);
|
|
|
|
break;
|
|
|
|
}
|
2012-05-26 18:50:02 +00:00
|
|
|
case AV_RESAMPLE_FILTER_TYPE_BLACKMAN_NUTTALL:
|
2012-03-23 21:42:17 +00:00
|
|
|
w = 2.0 * x / (factor * tap_count) + M_PI;
|
|
|
|
y *= 0.3635819 - 0.4891775 * cos( w) +
|
|
|
|
0.1365995 * cos(2 * w) -
|
|
|
|
0.0106411 * cos(3 * w);
|
|
|
|
break;
|
2012-05-26 18:50:02 +00:00
|
|
|
case AV_RESAMPLE_FILTER_TYPE_KAISER:
|
2012-03-23 21:42:17 +00:00
|
|
|
w = 2.0 * x / (factor * tap_count * M_PI);
|
2012-05-28 01:44:55 +00:00
|
|
|
y *= bessel(c->kaiser_beta * sqrt(FFMAX(1 - w * w, 0)));
|
2012-03-23 21:42:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tab[i] = y;
|
|
|
|
norm += y;
|
|
|
|
}
|
|
|
|
/* normalize so that an uniform color remains the same */
|
2012-05-28 01:44:55 +00:00
|
|
|
for (i = 0; i < tap_count; i++)
|
|
|
|
tab[i] = tab[i] / norm;
|
|
|
|
|
|
|
|
c->set_filter(c->filter_bank, tab, ph, tap_count);
|
2012-03-23 21:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
av_free(tab);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResampleContext *ff_audio_resample_init(AVAudioResampleContext *avr)
|
|
|
|
{
|
|
|
|
ResampleContext *c;
|
|
|
|
int out_rate = avr->out_sample_rate;
|
|
|
|
int in_rate = avr->in_sample_rate;
|
|
|
|
double factor = FFMIN(out_rate * avr->cutoff / in_rate, 1.0);
|
|
|
|
int phase_count = 1 << avr->phase_shift;
|
2012-05-28 01:44:55 +00:00
|
|
|
int felem_size;
|
2012-03-23 21:42:17 +00:00
|
|
|
|
2012-05-28 01:44:55 +00:00
|
|
|
if (avr->internal_sample_fmt != AV_SAMPLE_FMT_S16P &&
|
|
|
|
avr->internal_sample_fmt != AV_SAMPLE_FMT_S32P &&
|
|
|
|
avr->internal_sample_fmt != AV_SAMPLE_FMT_FLTP &&
|
|
|
|
avr->internal_sample_fmt != AV_SAMPLE_FMT_DBLP) {
|
2012-03-23 21:42:17 +00:00
|
|
|
av_log(avr, AV_LOG_ERROR, "Unsupported internal format for "
|
|
|
|
"resampling: %s\n",
|
|
|
|
av_get_sample_fmt_name(avr->internal_sample_fmt));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
c = av_mallocz(sizeof(*c));
|
|
|
|
if (!c)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
c->avr = avr;
|
|
|
|
c->phase_shift = avr->phase_shift;
|
|
|
|
c->phase_mask = phase_count - 1;
|
|
|
|
c->linear = avr->linear_interp;
|
|
|
|
c->filter_length = FFMAX((int)ceil(avr->filter_size / factor), 1);
|
2012-05-26 18:50:02 +00:00
|
|
|
c->filter_type = avr->filter_type;
|
|
|
|
c->kaiser_beta = avr->kaiser_beta;
|
2012-03-23 21:42:17 +00:00
|
|
|
|
2012-05-28 01:44:55 +00:00
|
|
|
switch (avr->internal_sample_fmt) {
|
|
|
|
case AV_SAMPLE_FMT_DBLP:
|
2014-03-04 16:12:33 +00:00
|
|
|
c->resample_one = c->linear ? resample_linear_dbl : resample_one_dbl;
|
2014-03-03 21:26:57 +00:00
|
|
|
c->resample_nearest = resample_nearest_dbl;
|
2012-05-28 01:44:55 +00:00
|
|
|
c->set_filter = set_filter_dbl;
|
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_FLTP:
|
2014-03-04 16:12:33 +00:00
|
|
|
c->resample_one = c->linear ? resample_linear_flt : resample_one_flt;
|
2014-03-03 21:26:57 +00:00
|
|
|
c->resample_nearest = resample_nearest_flt;
|
2012-05-28 01:44:55 +00:00
|
|
|
c->set_filter = set_filter_flt;
|
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_S32P:
|
2014-03-04 16:12:33 +00:00
|
|
|
c->resample_one = c->linear ? resample_linear_s32 : resample_one_s32;
|
2014-03-03 21:26:57 +00:00
|
|
|
c->resample_nearest = resample_nearest_s32;
|
2012-05-28 01:44:55 +00:00
|
|
|
c->set_filter = set_filter_s32;
|
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_S16P:
|
2014-03-04 16:12:33 +00:00
|
|
|
c->resample_one = c->linear ? resample_linear_s16 : resample_one_s16;
|
2014-03-03 21:26:57 +00:00
|
|
|
c->resample_nearest = resample_nearest_s16;
|
2012-05-28 01:44:55 +00:00
|
|
|
c->set_filter = set_filter_s16;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-04-21 14:13:28 +00:00
|
|
|
if (ARCH_AARCH64)
|
|
|
|
ff_audio_resample_init_aarch64(c, avr->internal_sample_fmt);
|
2015-02-19 22:28:26 +00:00
|
|
|
if (ARCH_ARM)
|
|
|
|
ff_audio_resample_init_arm(c, avr->internal_sample_fmt);
|
2014-04-21 14:13:28 +00:00
|
|
|
|
2012-05-28 01:44:55 +00:00
|
|
|
felem_size = av_get_bytes_per_sample(avr->internal_sample_fmt);
|
|
|
|
c->filter_bank = av_mallocz(c->filter_length * (phase_count + 1) * felem_size);
|
2012-03-23 21:42:17 +00:00
|
|
|
if (!c->filter_bank)
|
|
|
|
goto error;
|
|
|
|
|
2014-02-22 19:27:44 +00:00
|
|
|
if (build_filter(c, factor) < 0)
|
2012-03-23 21:42:17 +00:00
|
|
|
goto error;
|
|
|
|
|
2012-05-28 01:44:55 +00:00
|
|
|
memcpy(&c->filter_bank[(c->filter_length * phase_count + 1) * felem_size],
|
|
|
|
c->filter_bank, (c->filter_length - 1) * felem_size);
|
|
|
|
memcpy(&c->filter_bank[c->filter_length * phase_count * felem_size],
|
|
|
|
&c->filter_bank[(c->filter_length - 1) * felem_size], felem_size);
|
2012-03-23 21:42:17 +00:00
|
|
|
|
|
|
|
c->compensation_distance = 0;
|
|
|
|
if (!av_reduce(&c->src_incr, &c->dst_incr, out_rate,
|
|
|
|
in_rate * (int64_t)phase_count, INT32_MAX / 2))
|
|
|
|
goto error;
|
|
|
|
c->ideal_dst_incr = c->dst_incr;
|
|
|
|
|
2014-03-04 20:18:27 +00:00
|
|
|
c->padding_size = (c->filter_length - 1) / 2;
|
2014-03-04 15:56:01 +00:00
|
|
|
c->initial_padding_filled = 0;
|
|
|
|
c->index = 0;
|
2012-03-23 21:42:17 +00:00
|
|
|
c->frac = 0;
|
|
|
|
|
|
|
|
/* allocate internal buffer */
|
2014-03-04 15:56:01 +00:00
|
|
|
c->buffer = ff_audio_data_alloc(avr->resample_channels, c->padding_size,
|
2012-03-23 21:42:17 +00:00
|
|
|
avr->internal_sample_fmt,
|
|
|
|
"resample buffer");
|
|
|
|
if (!c->buffer)
|
|
|
|
goto error;
|
2014-03-04 15:56:01 +00:00
|
|
|
c->buffer->nb_samples = c->padding_size;
|
|
|
|
c->initial_padding_samples = c->padding_size;
|
2012-03-23 21:42:17 +00:00
|
|
|
|
|
|
|
av_log(avr, AV_LOG_DEBUG, "resample: %s from %d Hz to %d Hz\n",
|
|
|
|
av_get_sample_fmt_name(avr->internal_sample_fmt),
|
|
|
|
avr->in_sample_rate, avr->out_sample_rate);
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
|
|
|
error:
|
|
|
|
ff_audio_data_free(&c->buffer);
|
|
|
|
av_free(c->filter_bank);
|
|
|
|
av_free(c);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ff_audio_resample_free(ResampleContext **c)
|
|
|
|
{
|
|
|
|
if (!*c)
|
|
|
|
return;
|
|
|
|
ff_audio_data_free(&(*c)->buffer);
|
|
|
|
av_free((*c)->filter_bank);
|
|
|
|
av_freep(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
int avresample_set_compensation(AVAudioResampleContext *avr, int sample_delta,
|
|
|
|
int compensation_distance)
|
|
|
|
{
|
|
|
|
ResampleContext *c;
|
|
|
|
|
|
|
|
if (compensation_distance < 0)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
if (!compensation_distance && sample_delta)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
|
|
if (!avr->resample_needed) {
|
2012-11-30 01:58:05 +00:00
|
|
|
av_log(avr, AV_LOG_ERROR, "Unable to set resampling compensation\n");
|
|
|
|
return AVERROR(EINVAL);
|
2012-03-23 21:42:17 +00:00
|
|
|
}
|
|
|
|
c = avr->resample;
|
|
|
|
c->compensation_distance = compensation_distance;
|
|
|
|
if (compensation_distance) {
|
|
|
|
c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr *
|
|
|
|
(int64_t)sample_delta / compensation_distance;
|
|
|
|
} else {
|
|
|
|
c->dst_incr = c->ideal_dst_incr;
|
|
|
|
}
|
|
|
|
|
2015-09-10 15:05:55 +00:00
|
|
|
return 0;
|
2012-03-23 21:42:17 +00:00
|
|
|
}
|
|
|
|
|
2012-05-28 01:44:55 +00:00
|
|
|
static int resample(ResampleContext *c, void *dst, const void *src,
|
2014-03-03 21:26:57 +00:00
|
|
|
int *consumed, int src_size, int dst_size, int update_ctx,
|
|
|
|
int nearest_neighbour)
|
2012-03-23 21:42:17 +00:00
|
|
|
{
|
2012-05-28 01:44:55 +00:00
|
|
|
int dst_index;
|
2014-03-04 15:56:01 +00:00
|
|
|
unsigned int index = c->index;
|
2012-03-23 21:42:17 +00:00
|
|
|
int frac = c->frac;
|
|
|
|
int dst_incr_frac = c->dst_incr % c->src_incr;
|
|
|
|
int dst_incr = c->dst_incr / c->src_incr;
|
|
|
|
int compensation_distance = c->compensation_distance;
|
|
|
|
|
|
|
|
if (!dst != !src)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
2014-03-03 21:26:57 +00:00
|
|
|
if (nearest_neighbour) {
|
2014-03-04 15:56:01 +00:00
|
|
|
uint64_t index2 = ((uint64_t)index) << 32;
|
2012-03-23 21:42:17 +00:00
|
|
|
int64_t incr = (1LL << 32) * c->dst_incr / c->src_incr;
|
|
|
|
dst_size = FFMIN(dst_size,
|
|
|
|
(src_size-1-index) * (int64_t)c->src_incr /
|
|
|
|
c->dst_incr);
|
|
|
|
|
|
|
|
if (dst) {
|
|
|
|
for(dst_index = 0; dst_index < dst_size; dst_index++) {
|
2014-03-03 21:26:57 +00:00
|
|
|
c->resample_nearest(dst, dst_index, src, index2 >> 32);
|
2012-03-23 21:42:17 +00:00
|
|
|
index2 += incr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dst_index = dst_size;
|
|
|
|
}
|
|
|
|
index += dst_index * dst_incr;
|
|
|
|
index += (frac + dst_index * (int64_t)dst_incr_frac) / c->src_incr;
|
|
|
|
frac = (frac + dst_index * (int64_t)dst_incr_frac) % c->src_incr;
|
|
|
|
} else {
|
|
|
|
for (dst_index = 0; dst_index < dst_size; dst_index++) {
|
|
|
|
int sample_index = index >> c->phase_shift;
|
|
|
|
|
2014-03-04 15:56:01 +00:00
|
|
|
if (sample_index + c->filter_length > src_size)
|
2012-03-23 21:42:17 +00:00
|
|
|
break;
|
|
|
|
|
2012-05-28 01:44:55 +00:00
|
|
|
if (dst)
|
2014-03-04 15:56:01 +00:00
|
|
|
c->resample_one(c, dst, dst_index, src, index, frac);
|
2012-03-23 21:42:17 +00:00
|
|
|
|
|
|
|
frac += dst_incr_frac;
|
|
|
|
index += dst_incr;
|
|
|
|
if (frac >= c->src_incr) {
|
|
|
|
frac -= c->src_incr;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
if (dst_index + 1 == compensation_distance) {
|
|
|
|
compensation_distance = 0;
|
|
|
|
dst_incr_frac = c->ideal_dst_incr % c->src_incr;
|
|
|
|
dst_incr = c->ideal_dst_incr / c->src_incr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (consumed)
|
2014-03-04 15:56:01 +00:00
|
|
|
*consumed = index >> c->phase_shift;
|
2012-03-23 21:42:17 +00:00
|
|
|
|
|
|
|
if (update_ctx) {
|
2014-03-04 15:56:01 +00:00
|
|
|
index &= c->phase_mask;
|
2012-03-23 21:42:17 +00:00
|
|
|
|
|
|
|
if (compensation_distance) {
|
|
|
|
compensation_distance -= dst_index;
|
|
|
|
if (compensation_distance <= 0)
|
|
|
|
return AVERROR_BUG;
|
|
|
|
}
|
|
|
|
c->frac = frac;
|
|
|
|
c->index = index;
|
|
|
|
c->dst_incr = dst_incr_frac + c->src_incr*dst_incr;
|
|
|
|
c->compensation_distance = compensation_distance;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst_index;
|
|
|
|
}
|
|
|
|
|
2012-11-01 15:08:08 +00:00
|
|
|
int ff_audio_resample(ResampleContext *c, AudioData *dst, AudioData *src)
|
2012-03-23 21:42:17 +00:00
|
|
|
{
|
2012-11-01 15:08:08 +00:00
|
|
|
int ch, in_samples, in_leftover, consumed = 0, out_samples = 0;
|
2012-03-23 21:42:17 +00:00
|
|
|
int ret = AVERROR(EINVAL);
|
2014-03-03 21:26:57 +00:00
|
|
|
int nearest_neighbour = (c->compensation_distance == 0 &&
|
|
|
|
c->filter_length == 1 &&
|
|
|
|
c->phase_shift == 0);
|
2012-03-23 21:42:17 +00:00
|
|
|
|
|
|
|
in_samples = src ? src->nb_samples : 0;
|
|
|
|
in_leftover = c->buffer->nb_samples;
|
|
|
|
|
|
|
|
/* add input samples to the internal buffer */
|
|
|
|
if (src) {
|
|
|
|
ret = ff_audio_data_combine(c->buffer, in_leftover, src, 0, in_samples);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2014-03-04 21:38:47 +00:00
|
|
|
} else if (in_leftover <= c->final_padding_samples) {
|
2012-03-23 21:42:17 +00:00
|
|
|
/* no remaining samples to flush */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-04 15:56:01 +00:00
|
|
|
if (!c->initial_padding_filled) {
|
|
|
|
int bps = av_get_bytes_per_sample(c->avr->internal_sample_fmt);
|
|
|
|
int i;
|
|
|
|
|
2014-03-04 21:38:47 +00:00
|
|
|
if (src && c->buffer->nb_samples < 2 * c->padding_size)
|
2014-03-04 15:56:01 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < c->padding_size; i++)
|
2014-03-04 21:38:47 +00:00
|
|
|
for (ch = 0; ch < c->buffer->channels; ch++) {
|
|
|
|
if (c->buffer->nb_samples > 2 * c->padding_size - i) {
|
|
|
|
memcpy(c->buffer->data[ch] + bps * i,
|
|
|
|
c->buffer->data[ch] + bps * (2 * c->padding_size - i), bps);
|
|
|
|
} else {
|
|
|
|
memset(c->buffer->data[ch] + bps * i, 0, bps);
|
|
|
|
}
|
|
|
|
}
|
2014-03-04 15:56:01 +00:00
|
|
|
c->initial_padding_filled = 1;
|
|
|
|
}
|
|
|
|
|
2014-03-04 21:38:47 +00:00
|
|
|
if (!src && !c->final_padding_filled) {
|
|
|
|
int bps = av_get_bytes_per_sample(c->avr->internal_sample_fmt);
|
|
|
|
int i;
|
|
|
|
|
2015-04-27 23:55:10 +00:00
|
|
|
ret = ff_audio_data_realloc(c->buffer,
|
|
|
|
FFMAX(in_samples, in_leftover) +
|
|
|
|
c->padding_size);
|
2014-03-04 21:38:47 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
av_log(c->avr, AV_LOG_ERROR, "Error reallocating resampling buffer\n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < c->padding_size; i++)
|
|
|
|
for (ch = 0; ch < c->buffer->channels; ch++) {
|
|
|
|
if (in_leftover > i) {
|
|
|
|
memcpy(c->buffer->data[ch] + bps * (in_leftover + i),
|
|
|
|
c->buffer->data[ch] + bps * (in_leftover - i - 1),
|
|
|
|
bps);
|
|
|
|
} else {
|
|
|
|
memset(c->buffer->data[ch] + bps * (in_leftover + i),
|
|
|
|
0, bps);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c->buffer->nb_samples += c->padding_size;
|
|
|
|
c->final_padding_samples = c->padding_size;
|
|
|
|
c->final_padding_filled = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-23 21:42:17 +00:00
|
|
|
/* calculate output size and reallocate output buffer if needed */
|
|
|
|
/* TODO: try to calculate this without the dummy resample() run */
|
|
|
|
if (!dst->read_only && dst->allow_realloc) {
|
|
|
|
out_samples = resample(c, NULL, NULL, NULL, c->buffer->nb_samples,
|
2014-03-03 21:26:57 +00:00
|
|
|
INT_MAX, 0, nearest_neighbour);
|
2012-03-23 21:42:17 +00:00
|
|
|
ret = ff_audio_data_realloc(dst, out_samples);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(c->avr, AV_LOG_ERROR, "error reallocating output\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* resample each channel plane */
|
|
|
|
for (ch = 0; ch < c->buffer->channels; ch++) {
|
2012-05-28 01:44:55 +00:00
|
|
|
out_samples = resample(c, (void *)dst->data[ch],
|
2012-11-01 15:08:08 +00:00
|
|
|
(const void *)c->buffer->data[ch], &consumed,
|
2012-03-23 21:42:17 +00:00
|
|
|
c->buffer->nb_samples, dst->allocated_samples,
|
2014-03-03 21:26:57 +00:00
|
|
|
ch + 1 == c->buffer->channels, nearest_neighbour);
|
2012-03-23 21:42:17 +00:00
|
|
|
}
|
|
|
|
if (out_samples < 0) {
|
|
|
|
av_log(c->avr, AV_LOG_ERROR, "error during resampling\n");
|
|
|
|
return out_samples;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* drain consumed samples from the internal buffer */
|
2012-11-01 15:08:08 +00:00
|
|
|
ff_audio_data_drain(c->buffer, consumed);
|
2014-03-04 15:56:01 +00:00
|
|
|
c->initial_padding_samples = FFMAX(c->initial_padding_samples - consumed, 0);
|
2012-03-23 21:42:17 +00:00
|
|
|
|
2015-03-16 08:57:35 +00:00
|
|
|
av_log(c->avr, AV_LOG_TRACE, "resampled %d in + %d leftover to %d out + %d leftover\n",
|
2012-03-23 21:42:17 +00:00
|
|
|
in_samples, in_leftover, out_samples, c->buffer->nb_samples);
|
|
|
|
|
|
|
|
dst->nb_samples = out_samples;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int avresample_get_delay(AVAudioResampleContext *avr)
|
|
|
|
{
|
2014-03-04 20:18:27 +00:00
|
|
|
ResampleContext *c = avr->resample;
|
|
|
|
|
2012-03-23 21:42:17 +00:00
|
|
|
if (!avr->resample_needed || !avr->resample)
|
|
|
|
return 0;
|
|
|
|
|
2014-03-04 20:18:27 +00:00
|
|
|
return FFMAX(c->buffer->nb_samples - c->padding_size, 0);
|
2012-03-23 21:42:17 +00:00
|
|
|
}
|