2015-09-18 22:22:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rubberband/rubberband-c.h>
|
|
|
|
|
|
|
|
#include "libavutil/channel_layout.h"
|
|
|
|
#include "libavutil/common.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
|
|
|
|
#include "audio.h"
|
|
|
|
#include "avfilter.h"
|
2019-04-29 14:22:13 +02:00
|
|
|
#include "filters.h"
|
2015-09-18 22:22:05 +02:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
typedef struct RubberBandContext {
|
|
|
|
const AVClass *class;
|
|
|
|
RubberBandState rbs;
|
|
|
|
|
|
|
|
double tempo, pitch;
|
|
|
|
int transients, detector, phase, window,
|
|
|
|
smoothing, formant, opitch, channels;
|
|
|
|
int64_t nb_samples_out;
|
|
|
|
int64_t nb_samples_in;
|
2019-04-29 21:11:46 +02:00
|
|
|
int64_t first_pts;
|
2023-06-08 02:29:16 +02:00
|
|
|
int64_t last_pts;
|
2019-04-29 14:22:13 +02:00
|
|
|
int nb_samples;
|
2023-06-08 02:29:16 +02:00
|
|
|
int eof;
|
2015-09-18 22:22:05 +02:00
|
|
|
} RubberBandContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(RubberBandContext, x)
|
|
|
|
#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
2019-10-08 22:15:09 +02:00
|
|
|
#define AT AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
|
2015-09-18 22:22:05 +02:00
|
|
|
|
|
|
|
static const AVOption rubberband_options[] = {
|
2019-10-08 22:15:09 +02:00
|
|
|
{ "tempo", "set tempo scale factor", OFFSET(tempo), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.01, 100, AT },
|
|
|
|
{ "pitch", "set pitch scale factor", OFFSET(pitch), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.01, 100, AT },
|
2024-02-11 16:41:05 +02:00
|
|
|
{ "transients", "set transients", OFFSET(transients), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, .unit = "transients" },
|
|
|
|
{ "crisp", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionTransientsCrisp}, 0, 0, A, .unit = "transients" },
|
|
|
|
{ "mixed", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionTransientsMixed}, 0, 0, A, .unit = "transients" },
|
|
|
|
{ "smooth", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionTransientsSmooth}, 0, 0, A, .unit = "transients" },
|
|
|
|
{ "detector", "set detector", OFFSET(detector), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, .unit = "detector" },
|
|
|
|
{ "compound", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionDetectorCompound}, 0, 0, A, .unit = "detector" },
|
|
|
|
{ "percussive", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionDetectorPercussive}, 0, 0, A, .unit = "detector" },
|
|
|
|
{ "soft", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionDetectorSoft}, 0, 0, A, .unit = "detector" },
|
|
|
|
{ "phase", "set phase", OFFSET(phase), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, .unit = "phase" },
|
|
|
|
{ "laminar", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionPhaseLaminar}, 0, 0, A, .unit = "phase" },
|
|
|
|
{ "independent", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionPhaseIndependent}, 0, 0, A, .unit = "phase" },
|
|
|
|
{ "window", "set window", OFFSET(window), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, .unit = "window" },
|
|
|
|
{ "standard", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionWindowStandard}, 0, 0, A, .unit = "window" },
|
|
|
|
{ "short", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionWindowShort}, 0, 0, A, .unit = "window" },
|
|
|
|
{ "long", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionWindowLong}, 0, 0, A, .unit = "window" },
|
|
|
|
{ "smoothing", "set smoothing", OFFSET(smoothing), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, .unit = "smoothing" },
|
|
|
|
{ "off", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionSmoothingOff}, 0, 0, A, .unit = "smoothing" },
|
|
|
|
{ "on", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionSmoothingOn}, 0, 0, A, .unit = "smoothing" },
|
|
|
|
{ "formant", "set formant", OFFSET(formant), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, .unit = "formant" },
|
|
|
|
{ "shifted", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionFormantShifted}, 0, 0, A, .unit = "formant" },
|
|
|
|
{ "preserved", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionFormantPreserved}, 0, 0, A, .unit = "formant" },
|
|
|
|
{ "pitchq", "set pitch quality", OFFSET(opitch), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, .unit = "pitch" },
|
|
|
|
{ "quality", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionPitchHighQuality}, 0, 0, A, .unit = "pitch" },
|
|
|
|
{ "speed", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionPitchHighSpeed}, 0, 0, A, .unit = "pitch" },
|
|
|
|
{ "consistency", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionPitchHighConsistency}, 0, 0, A, .unit = "pitch" },
|
|
|
|
{ "channels", "set channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, A, .unit = "channels" },
|
|
|
|
{ "apart", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionChannelsApart}, 0, 0, A, .unit = "channels" },
|
|
|
|
{ "together", 0, 0, AV_OPT_TYPE_CONST, {.i64=RubberBandOptionChannelsTogether}, 0, 0, A, .unit = "channels" },
|
2015-09-18 22:22:05 +02:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(rubberband);
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
RubberBandContext *s = ctx->priv;
|
|
|
|
|
|
|
|
if (s->rbs)
|
|
|
|
rubberband_delete(s->rbs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
|
|
|
{
|
2019-11-20 14:05:50 +02:00
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
RubberBandContext *s = ctx->priv;
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2015-09-18 22:22:05 +02:00
|
|
|
AVFrame *out;
|
|
|
|
int ret = 0, nb_samples;
|
|
|
|
|
2019-04-29 21:11:46 +02:00
|
|
|
if (s->first_pts == AV_NOPTS_VALUE)
|
|
|
|
s->first_pts = in->pts;
|
|
|
|
|
2023-06-08 02:29:16 +02:00
|
|
|
rubberband_process(s->rbs, (const float *const *)in->extended_data, in->nb_samples, s->eof);
|
2015-09-18 22:22:05 +02:00
|
|
|
s->nb_samples_in += in->nb_samples;
|
|
|
|
|
|
|
|
nb_samples = rubberband_available(s->rbs);
|
|
|
|
if (nb_samples > 0) {
|
2018-01-03 23:47:40 +02:00
|
|
|
out = ff_get_audio_buffer(outlink, nb_samples);
|
2015-09-18 22:22:05 +02:00
|
|
|
if (!out) {
|
|
|
|
av_frame_free(&in);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2019-04-29 21:11:46 +02:00
|
|
|
out->pts = s->first_pts + av_rescale_q(s->nb_samples_out,
|
2015-09-18 22:22:05 +02:00
|
|
|
(AVRational){ 1, outlink->sample_rate },
|
|
|
|
outlink->time_base);
|
2023-06-08 02:29:16 +02:00
|
|
|
s->last_pts = out->pts;
|
2023-03-30 14:06:45 +02:00
|
|
|
nb_samples = rubberband_retrieve(s->rbs, (float *const *)out->extended_data, nb_samples);
|
2015-09-18 22:22:05 +02:00
|
|
|
out->nb_samples = nb_samples;
|
|
|
|
ret = ff_filter_frame(outlink, out);
|
|
|
|
s->nb_samples_out += nb_samples;
|
|
|
|
}
|
|
|
|
|
|
|
|
av_frame_free(&in);
|
2019-11-20 14:05:50 +02:00
|
|
|
if (ff_inlink_queued_samples(inlink) >= s->nb_samples)
|
|
|
|
ff_filter_set_ready(ctx, 100);
|
|
|
|
return ret < 0 ? ret : nb_samples;
|
2015-09-18 22:22:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int config_input(AVFilterLink *inlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
RubberBandContext *s = ctx->priv;
|
|
|
|
int opts = s->transients|s->detector|s->phase|s->window|
|
|
|
|
s->smoothing|s->formant|s->opitch|s->channels|
|
|
|
|
RubberBandOptionProcessRealTime;
|
|
|
|
|
|
|
|
if (s->rbs)
|
|
|
|
rubberband_delete(s->rbs);
|
2021-08-31 16:03:14 +02:00
|
|
|
s->rbs = rubberband_new(inlink->sample_rate, inlink->ch_layout.nb_channels, opts, 1. / s->tempo, s->pitch);
|
2019-04-29 13:47:46 +02:00
|
|
|
if (!s->rbs)
|
|
|
|
return AVERROR(ENOMEM);
|
2015-09-18 22:22:05 +02:00
|
|
|
|
2019-04-29 14:22:13 +02:00
|
|
|
s->nb_samples = rubberband_get_samples_required(s->rbs);
|
2019-04-29 21:11:46 +02:00
|
|
|
s->first_pts = AV_NOPTS_VALUE;
|
2015-09-18 22:22:05 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-29 14:22:13 +02:00
|
|
|
static int activate(AVFilterContext *ctx)
|
2015-09-18 22:22:05 +02:00
|
|
|
{
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
2019-04-29 14:22:13 +02:00
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
RubberBandContext *s = ctx->priv;
|
|
|
|
AVFrame *in = NULL;
|
2023-06-08 02:29:16 +02:00
|
|
|
int64_t pts;
|
|
|
|
int status, ret;
|
2015-09-18 22:22:05 +02:00
|
|
|
|
2019-04-29 14:22:13 +02:00
|
|
|
FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
|
|
|
|
|
|
|
|
ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in);
|
2023-06-08 02:29:16 +02:00
|
|
|
|
|
|
|
if (ff_inlink_acknowledge_status(inlink, &status, &pts))
|
|
|
|
s->eof |= status == AVERROR_EOF;
|
|
|
|
|
2019-04-29 14:22:13 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2019-05-06 21:04:52 +02:00
|
|
|
if (ret > 0) {
|
|
|
|
ret = filter_frame(inlink, in);
|
2019-05-06 21:09:43 +02:00
|
|
|
if (ret != 0)
|
2019-05-06 21:04:52 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2019-04-29 14:22:13 +02:00
|
|
|
|
2023-06-08 02:29:16 +02:00
|
|
|
if (s->eof) {
|
|
|
|
ff_outlink_set_status(outlink, AVERROR_EOF, s->last_pts);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-29 14:22:13 +02:00
|
|
|
FF_FILTER_FORWARD_WANTED(outlink, inlink);
|
|
|
|
|
|
|
|
return FFERROR_NOT_READY;
|
2015-09-18 22:22:05 +02:00
|
|
|
}
|
|
|
|
|
2015-09-30 21:40:54 +02:00
|
|
|
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
|
|
|
|
char *res, int res_len, int flags)
|
|
|
|
{
|
|
|
|
RubberBandContext *s = ctx->priv;
|
2019-10-08 22:15:09 +02:00
|
|
|
int ret;
|
2015-09-30 21:40:54 +02:00
|
|
|
|
2019-10-08 22:15:09 +02:00
|
|
|
ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2015-09-30 21:40:54 +02:00
|
|
|
|
2019-10-08 22:15:09 +02:00
|
|
|
rubberband_set_time_ratio(s->rbs, 1. / s->tempo);
|
|
|
|
rubberband_set_pitch_scale(s->rbs, s->pitch);
|
2020-06-12 12:57:43 +02:00
|
|
|
s->nb_samples = rubberband_get_samples_required(s->rbs);
|
2015-09-30 21:40:54 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-18 22:22:05 +02:00
|
|
|
static const AVFilterPad rubberband_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.config_props = config_input,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_af_rubberband = {
|
2015-09-18 22:22:05 +02:00
|
|
|
.name = "rubberband",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Apply time-stretching and pitch-shifting."),
|
|
|
|
.priv_size = sizeof(RubberBandContext),
|
|
|
|
.priv_class = &rubberband_class,
|
|
|
|
.uninit = uninit,
|
2019-04-29 14:22:13 +02:00
|
|
|
.activate = activate,
|
2021-08-12 13:05:31 +02:00
|
|
|
FILTER_INPUTS(rubberband_inputs),
|
2023-08-03 00:59:02 +02:00
|
|
|
FILTER_OUTPUTS(ff_audio_default_filterpad),
|
2021-09-27 13:10:11 +02:00
|
|
|
FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_FLTP),
|
2015-09-30 21:40:54 +02:00
|
|
|
.process_command = process_command,
|
2015-09-18 22:22:05 +02:00
|
|
|
};
|