2013-09-13 11:36:52 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Paul B Mahol
|
|
|
|
*
|
|
|
|
* 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 "libavutil/avstring.h"
|
2018-11-18 19:16:59 +00:00
|
|
|
#include "libavutil/eval.h"
|
2013-09-13 11:36:52 +00:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/samplefmt.h"
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "audio.h"
|
2018-10-25 20:57:27 +00:00
|
|
|
#include "filters.h"
|
2013-09-13 11:36:52 +00:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
typedef struct ChanDelay {
|
2021-04-23 03:26:09 +00:00
|
|
|
int64_t delay;
|
|
|
|
size_t delay_index;
|
|
|
|
size_t index;
|
2013-09-13 11:36:52 +00:00
|
|
|
uint8_t *samples;
|
|
|
|
} ChanDelay;
|
|
|
|
|
|
|
|
typedef struct AudioDelayContext {
|
|
|
|
const AVClass *class;
|
2019-10-07 16:02:50 +00:00
|
|
|
int all;
|
2013-09-13 11:36:52 +00:00
|
|
|
char *delays;
|
|
|
|
ChanDelay *chandelay;
|
|
|
|
int nb_delays;
|
|
|
|
int block_align;
|
2018-10-25 20:57:27 +00:00
|
|
|
int64_t padding;
|
|
|
|
int64_t max_delay;
|
2013-09-13 11:36:52 +00:00
|
|
|
int64_t next_pts;
|
2018-10-25 20:57:27 +00:00
|
|
|
int eof;
|
2013-09-13 11:36:52 +00:00
|
|
|
|
|
|
|
void (*delay_channel)(ChanDelay *d, int nb_samples,
|
|
|
|
const uint8_t *src, uint8_t *dst);
|
|
|
|
} AudioDelayContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(AudioDelayContext, x)
|
|
|
|
#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
|
|
|
|
|
|
|
static const AVOption adelay_options[] = {
|
|
|
|
{ "delays", "set list of delays for each channel", OFFSET(delays), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, A },
|
2019-10-07 16:02:50 +00:00
|
|
|
{ "all", "use last available delay for remained channels", OFFSET(all), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
|
2013-09-13 11:36:52 +00:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(adelay);
|
|
|
|
|
|
|
|
#define DELAY(name, type, fill) \
|
|
|
|
static void delay_channel_## name ##p(ChanDelay *d, int nb_samples, \
|
|
|
|
const uint8_t *ssrc, uint8_t *ddst) \
|
|
|
|
{ \
|
|
|
|
const type *src = (type *)ssrc; \
|
|
|
|
type *dst = (type *)ddst; \
|
|
|
|
type *samples = (type *)d->samples; \
|
|
|
|
\
|
|
|
|
while (nb_samples) { \
|
|
|
|
if (d->delay_index < d->delay) { \
|
|
|
|
const int len = FFMIN(nb_samples, d->delay - d->delay_index); \
|
|
|
|
\
|
|
|
|
memcpy(&samples[d->delay_index], src, len * sizeof(type)); \
|
|
|
|
memset(dst, fill, len * sizeof(type)); \
|
|
|
|
d->delay_index += len; \
|
|
|
|
src += len; \
|
|
|
|
dst += len; \
|
|
|
|
nb_samples -= len; \
|
|
|
|
} else { \
|
|
|
|
*dst = samples[d->index]; \
|
|
|
|
samples[d->index] = *src; \
|
|
|
|
nb_samples--; \
|
|
|
|
d->index++; \
|
|
|
|
src++, dst++; \
|
|
|
|
d->index = d->index >= d->delay ? 0 : d->index; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
DELAY(u8, uint8_t, 0x80)
|
|
|
|
DELAY(s16, int16_t, 0)
|
|
|
|
DELAY(s32, int32_t, 0)
|
|
|
|
DELAY(flt, float, 0)
|
|
|
|
DELAY(dbl, double, 0)
|
|
|
|
|
|
|
|
static int config_input(AVFilterLink *inlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
AudioDelayContext *s = ctx->priv;
|
|
|
|
char *p, *arg, *saveptr = NULL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
s->chandelay = av_calloc(inlink->channels, sizeof(*s->chandelay));
|
|
|
|
if (!s->chandelay)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
s->nb_delays = inlink->channels;
|
|
|
|
s->block_align = av_get_bytes_per_sample(inlink->format);
|
|
|
|
|
|
|
|
p = s->delays;
|
|
|
|
for (i = 0; i < s->nb_delays; i++) {
|
|
|
|
ChanDelay *d = &s->chandelay[i];
|
2019-01-01 12:33:16 +00:00
|
|
|
float delay, div;
|
2016-08-11 19:42:56 +00:00
|
|
|
char type = 0;
|
|
|
|
int ret;
|
2013-09-13 11:36:52 +00:00
|
|
|
|
|
|
|
if (!(arg = av_strtok(p, "|", &saveptr)))
|
|
|
|
break;
|
|
|
|
|
|
|
|
p = NULL;
|
|
|
|
|
2021-04-23 03:26:09 +00:00
|
|
|
ret = av_sscanf(arg, "%"SCNd64"%c", &d->delay, &type);
|
2016-08-11 19:42:56 +00:00
|
|
|
if (ret != 2 || type != 'S') {
|
2019-01-01 12:33:16 +00:00
|
|
|
div = type == 's' ? 1.0 : 1000.0;
|
2020-03-29 00:08:05 +00:00
|
|
|
if (av_sscanf(arg, "%f", &delay) != 1) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Invalid syntax for delay.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2019-01-01 12:33:16 +00:00
|
|
|
d->delay = delay * inlink->sample_rate / div;
|
2016-08-11 19:42:56 +00:00
|
|
|
}
|
|
|
|
|
2013-09-13 11:36:52 +00:00
|
|
|
if (d->delay < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Delay must be non negative number.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-09 07:56:33 +00:00
|
|
|
if (s->all && i) {
|
|
|
|
for (int j = i; j < s->nb_delays; j++)
|
|
|
|
s->chandelay[j].delay = s->chandelay[i-1].delay;
|
2019-10-07 16:02:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 20:57:27 +00:00
|
|
|
s->padding = s->chandelay[0].delay;
|
|
|
|
for (i = 1; i < s->nb_delays; i++) {
|
|
|
|
ChanDelay *d = &s->chandelay[i];
|
|
|
|
|
|
|
|
s->padding = FFMIN(s->padding, d->delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->padding) {
|
|
|
|
for (i = 0; i < s->nb_delays; i++) {
|
|
|
|
ChanDelay *d = &s->chandelay[i];
|
|
|
|
|
|
|
|
d->delay -= s->padding;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-13 11:36:52 +00:00
|
|
|
for (i = 0; i < s->nb_delays; i++) {
|
|
|
|
ChanDelay *d = &s->chandelay[i];
|
|
|
|
|
|
|
|
if (!d->delay)
|
|
|
|
continue;
|
|
|
|
|
2021-04-23 03:26:09 +00:00
|
|
|
if (d->delay > SIZE_MAX) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Requested delay is too big.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2013-09-13 11:36:52 +00:00
|
|
|
d->samples = av_malloc_array(d->delay, s->block_align);
|
|
|
|
if (!d->samples)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
s->max_delay = FFMAX(s->max_delay, d->delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (inlink->format) {
|
|
|
|
case AV_SAMPLE_FMT_U8P : s->delay_channel = delay_channel_u8p ; break;
|
|
|
|
case AV_SAMPLE_FMT_S16P: s->delay_channel = delay_channel_s16p; break;
|
|
|
|
case AV_SAMPLE_FMT_S32P: s->delay_channel = delay_channel_s32p; break;
|
|
|
|
case AV_SAMPLE_FMT_FLTP: s->delay_channel = delay_channel_fltp; break;
|
|
|
|
case AV_SAMPLE_FMT_DBLP: s->delay_channel = delay_channel_dblp; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
2021-11-07 18:00:50 +00:00
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2013-09-13 11:36:52 +00:00
|
|
|
AudioDelayContext *s = ctx->priv;
|
|
|
|
AVFrame *out_frame;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (ctx->is_disabled || !s->delays)
|
2021-11-07 18:00:50 +00:00
|
|
|
return ff_filter_frame(outlink, frame);
|
2013-09-13 11:36:52 +00:00
|
|
|
|
2021-11-07 18:00:50 +00:00
|
|
|
out_frame = ff_get_audio_buffer(outlink, frame->nb_samples);
|
2015-11-25 20:59:33 +00:00
|
|
|
if (!out_frame) {
|
|
|
|
av_frame_free(&frame);
|
2013-09-13 11:36:52 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2015-11-25 20:59:33 +00:00
|
|
|
}
|
2013-09-13 11:36:52 +00:00
|
|
|
av_frame_copy_props(out_frame, frame);
|
|
|
|
|
|
|
|
for (i = 0; i < s->nb_delays; i++) {
|
|
|
|
ChanDelay *d = &s->chandelay[i];
|
|
|
|
const uint8_t *src = frame->extended_data[i];
|
|
|
|
uint8_t *dst = out_frame->extended_data[i];
|
|
|
|
|
|
|
|
if (!d->delay)
|
|
|
|
memcpy(dst, src, frame->nb_samples * s->block_align);
|
|
|
|
else
|
|
|
|
s->delay_channel(d, frame->nb_samples, src, dst);
|
|
|
|
}
|
|
|
|
|
2018-10-25 20:57:27 +00:00
|
|
|
out_frame->pts = s->next_pts;
|
2021-11-07 18:00:50 +00:00
|
|
|
s->next_pts += av_rescale_q(frame->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
|
2013-09-13 11:36:52 +00:00
|
|
|
av_frame_free(&frame);
|
2021-11-07 18:00:50 +00:00
|
|
|
return ff_filter_frame(outlink, out_frame);
|
2013-09-13 11:36:52 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 20:57:27 +00:00
|
|
|
static int activate(AVFilterContext *ctx)
|
2013-09-13 11:36:52 +00:00
|
|
|
{
|
2018-10-25 20:57:27 +00:00
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2013-09-13 11:36:52 +00:00
|
|
|
AudioDelayContext *s = ctx->priv;
|
2018-10-25 20:57:27 +00:00
|
|
|
AVFrame *frame = NULL;
|
|
|
|
int ret, status;
|
|
|
|
int64_t pts;
|
2013-09-13 11:36:52 +00:00
|
|
|
|
2018-10-25 20:57:27 +00:00
|
|
|
FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
|
|
|
|
|
|
|
|
if (s->padding) {
|
|
|
|
int nb_samples = FFMIN(s->padding, 2048);
|
2013-09-13 11:36:52 +00:00
|
|
|
|
|
|
|
frame = ff_get_audio_buffer(outlink, nb_samples);
|
|
|
|
if (!frame)
|
|
|
|
return AVERROR(ENOMEM);
|
2018-10-25 20:57:27 +00:00
|
|
|
s->padding -= nb_samples;
|
2013-09-13 11:36:52 +00:00
|
|
|
|
|
|
|
av_samples_set_silence(frame->extended_data, 0,
|
|
|
|
frame->nb_samples,
|
|
|
|
outlink->channels,
|
|
|
|
frame->format);
|
|
|
|
|
|
|
|
frame->pts = s->next_pts;
|
|
|
|
if (s->next_pts != AV_NOPTS_VALUE)
|
|
|
|
s->next_pts += av_rescale_q(nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
|
|
|
|
|
2018-10-25 20:57:27 +00:00
|
|
|
return ff_filter_frame(outlink, frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ff_inlink_consume_frame(inlink, &frame);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (ret > 0)
|
|
|
|
return filter_frame(inlink, frame);
|
|
|
|
|
|
|
|
if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
|
|
|
|
if (status == AVERROR_EOF)
|
|
|
|
s->eof = 1;
|
2013-09-13 11:36:52 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 20:57:27 +00:00
|
|
|
if (s->eof && s->max_delay) {
|
|
|
|
int nb_samples = FFMIN(s->max_delay, 2048);
|
|
|
|
|
|
|
|
frame = ff_get_audio_buffer(outlink, nb_samples);
|
|
|
|
if (!frame)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
s->max_delay -= nb_samples;
|
|
|
|
|
|
|
|
av_samples_set_silence(frame->extended_data, 0,
|
|
|
|
frame->nb_samples,
|
|
|
|
outlink->channels,
|
|
|
|
frame->format);
|
|
|
|
|
|
|
|
frame->pts = s->next_pts;
|
|
|
|
return filter_frame(inlink, frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->eof && s->max_delay == 0) {
|
|
|
|
ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!s->eof)
|
|
|
|
FF_FILTER_FORWARD_WANTED(outlink, inlink);
|
|
|
|
|
|
|
|
return FFERROR_NOT_READY;
|
2013-09-13 11:36:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AudioDelayContext *s = ctx->priv;
|
|
|
|
|
2018-10-18 08:21:09 +00:00
|
|
|
if (s->chandelay) {
|
|
|
|
for (int i = 0; i < s->nb_delays; i++)
|
|
|
|
av_freep(&s->chandelay[i].samples);
|
|
|
|
}
|
2013-09-13 11:36:52 +00:00
|
|
|
av_freep(&s->chandelay);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AVFilterPad adelay_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.config_props = config_input,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad adelay_outputs[] = {
|
|
|
|
{
|
2018-10-25 20:57:27 +00:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
2013-09-13 11:36:52 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 16:33:56 +00:00
|
|
|
const AVFilter ff_af_adelay = {
|
2013-09-13 11:36:52 +00:00
|
|
|
.name = "adelay",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Delay one or more audio channels."),
|
|
|
|
.priv_size = sizeof(AudioDelayContext),
|
|
|
|
.priv_class = &adelay_class,
|
2018-10-25 20:57:27 +00:00
|
|
|
.activate = activate,
|
2013-09-13 11:36:52 +00:00
|
|
|
.uninit = uninit,
|
2021-08-12 11:05:31 +00:00
|
|
|
FILTER_INPUTS(adelay_inputs),
|
|
|
|
FILTER_OUTPUTS(adelay_outputs),
|
2021-09-27 10:28:23 +00:00
|
|
|
FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_U8P, AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
|
|
|
|
AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP),
|
2013-09-13 11:36:52 +00:00
|
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
|
|
|
|
};
|