2012-12-18 07:13:59 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Michael Niedermayer
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* audio pad filter.
|
|
|
|
*
|
|
|
|
* Based on af_aresample.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/avstring.h"
|
|
|
|
#include "libavutil/channel_layout.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/samplefmt.h"
|
|
|
|
#include "libavutil/avassert.h"
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "audio.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
2017-05-12 20:00:49 +02:00
|
|
|
typedef struct APadContext {
|
2012-12-18 19:01:33 +03:00
|
|
|
const AVClass *class;
|
2012-12-18 07:13:59 +03:00
|
|
|
int64_t next_pts;
|
2012-12-18 19:01:33 +03:00
|
|
|
|
|
|
|
int packet_size;
|
2014-08-18 15:51:25 +03:00
|
|
|
int64_t pad_len, pad_len_left;
|
|
|
|
int64_t whole_len, whole_len_left;
|
2018-12-16 18:00:56 +02:00
|
|
|
int64_t pad_dur;
|
|
|
|
int64_t whole_dur;
|
2012-12-18 07:13:59 +03:00
|
|
|
} APadContext;
|
|
|
|
|
2012-12-18 19:01:33 +03:00
|
|
|
#define OFFSET(x) offsetof(APadContext, x)
|
|
|
|
#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
|
|
|
|
|
|
|
static const AVOption apad_options[] = {
|
2014-08-18 15:51:25 +03:00
|
|
|
{ "packet_size", "set silence packet size", OFFSET(packet_size), AV_OPT_TYPE_INT, { .i64 = 4096 }, 0, INT_MAX, A },
|
|
|
|
{ "pad_len", "set number of samples of silence to add", OFFSET(pad_len), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, A },
|
|
|
|
{ "whole_len", "set minimum target number of samples in the audio stream", OFFSET(whole_len), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, A },
|
2021-07-28 23:53:42 +02:00
|
|
|
{ "pad_dur", "set duration of silence to add", OFFSET(pad_dur), AV_OPT_TYPE_DURATION, { .i64 = -1 }, -1, INT64_MAX, A },
|
|
|
|
{ "whole_dur", "set minimum target duration in the audio stream", OFFSET(whole_dur), AV_OPT_TYPE_DURATION, { .i64 = -1 }, -1, INT64_MAX, A },
|
2013-09-07 15:13:50 +03:00
|
|
|
{ NULL }
|
2012-12-18 19:01:33 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(apad);
|
|
|
|
|
2013-04-12 12:13:33 +03:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2012-12-18 07:13:59 +03:00
|
|
|
{
|
2015-08-26 19:03:32 +02:00
|
|
|
APadContext *s = ctx->priv;
|
2012-12-18 07:13:59 +03:00
|
|
|
|
2015-08-26 19:03:32 +02:00
|
|
|
s->next_pts = AV_NOPTS_VALUE;
|
|
|
|
if (s->whole_len >= 0 && s->pad_len >= 0) {
|
2012-12-18 19:22:36 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Both whole and pad length are set, this is not possible\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2012-12-18 07:13:59 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-10 03:30:30 +03:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
|
2012-12-18 07:13:59 +03:00
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
2015-08-26 19:03:32 +02:00
|
|
|
APadContext *s = ctx->priv;
|
2012-12-18 19:22:36 +03:00
|
|
|
|
2015-08-26 19:03:32 +02:00
|
|
|
if (s->whole_len >= 0) {
|
|
|
|
s->whole_len_left = FFMAX(s->whole_len_left - frame->nb_samples, 0);
|
2014-08-18 15:51:25 +03:00
|
|
|
av_log(ctx, AV_LOG_DEBUG,
|
2015-08-26 19:03:32 +02:00
|
|
|
"n_out:%d whole_len_left:%"PRId64"\n", frame->nb_samples, s->whole_len_left);
|
2014-08-18 15:51:25 +03:00
|
|
|
}
|
2012-12-18 19:22:36 +03:00
|
|
|
|
2015-08-26 19:03:32 +02:00
|
|
|
s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
|
2012-12-18 07:13:59 +03:00
|
|
|
return ff_filter_frame(ctx->outputs[0], frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int request_frame(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
2015-08-26 19:03:32 +02:00
|
|
|
APadContext *s = ctx->priv;
|
2012-12-18 07:13:59 +03:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ff_request_frame(ctx->inputs[0]);
|
|
|
|
|
2013-05-08 22:59:28 +03:00
|
|
|
if (ret == AVERROR_EOF && !ctx->is_disabled) {
|
2015-08-26 19:03:32 +02:00
|
|
|
int n_out = s->packet_size;
|
2013-03-10 03:30:30 +03:00
|
|
|
AVFrame *outsamplesref;
|
2012-12-18 19:22:36 +03:00
|
|
|
|
2015-08-26 19:03:32 +02:00
|
|
|
if (s->whole_len >= 0 && s->pad_len < 0) {
|
|
|
|
s->pad_len = s->pad_len_left = s->whole_len_left;
|
2012-12-18 19:22:36 +03:00
|
|
|
}
|
2015-08-26 19:03:32 +02:00
|
|
|
if (s->pad_len >=0 || s->whole_len >= 0) {
|
|
|
|
n_out = FFMIN(n_out, s->pad_len_left);
|
|
|
|
s->pad_len_left -= n_out;
|
2014-08-18 15:51:25 +03:00
|
|
|
av_log(ctx, AV_LOG_DEBUG,
|
2015-08-26 19:03:32 +02:00
|
|
|
"padding n_out:%d pad_len_left:%"PRId64"\n", n_out, s->pad_len_left);
|
2012-12-18 19:22:36 +03:00
|
|
|
}
|
|
|
|
|
2014-08-18 15:52:38 +03:00
|
|
|
if (!n_out)
|
2012-12-18 19:22:36 +03:00
|
|
|
return AVERROR_EOF;
|
|
|
|
|
2013-03-10 03:30:30 +03:00
|
|
|
outsamplesref = ff_get_audio_buffer(outlink, n_out);
|
2012-12-18 07:13:59 +03:00
|
|
|
if (!outsamplesref)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2013-03-10 03:30:30 +03:00
|
|
|
av_assert0(outsamplesref->sample_rate == outlink->sample_rate);
|
|
|
|
av_assert0(outsamplesref->nb_samples == n_out);
|
2012-12-18 07:13:59 +03:00
|
|
|
|
|
|
|
av_samples_set_silence(outsamplesref->extended_data, 0,
|
|
|
|
n_out,
|
2021-08-31 16:03:14 +02:00
|
|
|
outsamplesref->ch_layout.nb_channels,
|
2012-12-18 07:13:59 +03:00
|
|
|
outsamplesref->format);
|
|
|
|
|
2015-08-26 19:03:32 +02:00
|
|
|
outsamplesref->pts = s->next_pts;
|
|
|
|
if (s->next_pts != AV_NOPTS_VALUE)
|
|
|
|
s->next_pts += av_rescale_q(n_out, (AVRational){1, outlink->sample_rate}, outlink->time_base);
|
2012-12-18 07:13:59 +03:00
|
|
|
|
|
|
|
return ff_filter_frame(outlink, outsamplesref);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-12-16 18:00:56 +02:00
|
|
|
static int config_output(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
APadContext *s = ctx->priv;
|
|
|
|
|
2021-07-28 23:53:42 +02:00
|
|
|
if (s->pad_dur >= 0)
|
2018-12-16 18:00:56 +02:00
|
|
|
s->pad_len = av_rescale(s->pad_dur, outlink->sample_rate, AV_TIME_BASE);
|
2021-07-28 23:53:42 +02:00
|
|
|
if (s->whole_dur >= 0)
|
2018-12-16 18:00:56 +02:00
|
|
|
s->whole_len = av_rescale(s->whole_dur, outlink->sample_rate, AV_TIME_BASE);
|
|
|
|
|
|
|
|
s->pad_len_left = s->pad_len;
|
|
|
|
s->whole_len_left = s->whole_len;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-18 07:13:59 +03:00
|
|
|
static const AVFilterPad apad_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad apad_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.request_frame = request_frame,
|
2018-12-16 18:00:56 +02:00
|
|
|
.config_props = config_output,
|
2012-12-18 07:13:59 +03:00
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_af_apad = {
|
2012-12-18 07:13:59 +03:00
|
|
|
.name = "apad",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Pad audio with silence."),
|
|
|
|
.init = init,
|
|
|
|
.priv_size = sizeof(APadContext),
|
2021-08-12 13:05:31 +02:00
|
|
|
FILTER_INPUTS(apad_inputs),
|
|
|
|
FILTER_OUTPUTS(apad_outputs),
|
2012-12-18 19:01:33 +03:00
|
|
|
.priv_class = &apad_class,
|
2013-05-09 02:04:41 +03:00
|
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
|
2012-12-18 07:13:59 +03:00
|
|
|
};
|