ffmpeg/libavfilter/af_apad.c

209 lines
6.4 KiB
C
Raw Normal View History

/*
* 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 "filters.h"
#include "internal.h"
typedef struct APadContext {
const AVClass *class;
int64_t next_pts;
int eof;
int packet_size;
int64_t pad_len, pad_len_left;
int64_t whole_len, whole_len_left;
int64_t pad_dur;
int64_t whole_dur;
} APadContext;
#define OFFSET(x) offsetof(APadContext, x)
#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
static const AVOption apad_options[] = {
{ "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 },
{ "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 },
{ NULL }
};
AVFILTER_DEFINE_CLASS(apad);
static av_cold int init(AVFilterContext *ctx)
{
APadContext *s = ctx->priv;
s->next_pts = AV_NOPTS_VALUE;
if (s->whole_len >= 0 && s->pad_len >= 0) {
av_log(ctx, AV_LOG_ERROR, "Both whole and pad length are set, this is not possible\n");
return AVERROR(EINVAL);
}
return 0;
}
Merge commit '7e350379f87e7f74420b4813170fe808e2313911' * commit '7e350379f87e7f74420b4813170fe808e2313911': lavfi: switch to AVFrame. Conflicts: doc/filters.texi libavfilter/af_ashowinfo.c libavfilter/audio.c libavfilter/avfilter.c libavfilter/avfilter.h libavfilter/buffersink.c libavfilter/buffersrc.c libavfilter/buffersrc.h libavfilter/f_select.c libavfilter/f_setpts.c libavfilter/fifo.c libavfilter/split.c libavfilter/src_movie.c libavfilter/version.h libavfilter/vf_aspect.c libavfilter/vf_bbox.c libavfilter/vf_blackframe.c libavfilter/vf_delogo.c libavfilter/vf_drawbox.c libavfilter/vf_drawtext.c libavfilter/vf_fade.c libavfilter/vf_fieldorder.c libavfilter/vf_fps.c libavfilter/vf_frei0r.c libavfilter/vf_gradfun.c libavfilter/vf_hqdn3d.c libavfilter/vf_lut.c libavfilter/vf_overlay.c libavfilter/vf_pad.c libavfilter/vf_scale.c libavfilter/vf_showinfo.c libavfilter/vf_transpose.c libavfilter/vf_vflip.c libavfilter/vf_yadif.c libavfilter/video.c libavfilter/vsrc_testsrc.c libavfilter/yadif.h Following are notes about the merge authorship and various technical details. Michael Niedermayer: * Main merge operation, notably avfilter.c and video.c * Switch to AVFrame: - afade - anullsrc - apad - aresample - blackframe - deshake - idet - il - mandelbrot - mptestsrc - noise - setfield - smartblur - tinterlace * various merge changes and fixes in: - ashowinfo - blackdetect - field - fps - select - testsrc - yadif Nicolas George: * Switch to AVFrame: - make rawdec work with refcounted frames. Adapted from commit 759001c534287a96dc96d1e274665feb7059145d by Anton Khirnov. Also, fix the use of || instead of | in a flags check. - make buffer sink and src, audio and video work all together Clément Bœsch: * Switch to AVFrame: - aevalsrc - alphaextract - blend - cellauto - colormatrix - concat - earwax - ebur128 - edgedetect - geq - histeq - histogram - hue - kerndeint - life - movie - mp (with the help of Michael) - overlay - pad - pan - pp - pp - removelogo - sendcmd - showspectrum - showwaves - silencedetect - stereo3d - subtitles - super2xsai - swapuv - thumbnail - tile Hendrik Leppkes: * Switch to AVFrame: - aconvert - amerge - asetnsamples - atempo - biquads Matthieu Bouron: * Switch to AVFrame - alphamerge - decimate - volumedetect Stefano Sabatini: * Switch to AVFrame: - astreamsync - flite - framestep Signed-off-by: Michael Niedermayer <michaelni@gmx.at> Signed-off-by: Nicolas George <nicolas.george@normalesup.org> Signed-off-by: Clément Bœsch <ubitux@gmail.com> Signed-off-by: Hendrik Leppkes <h.leppkes@gmail.com> Signed-off-by: Matthieu Bouron <matthieu.bouron@gmail.com> Signed-off-by: Stefano Sabatini <stefasab@gmail.com> Merged-by: Michael Niedermayer <michaelni@gmx.at>
2013-03-10 01:30:30 +01:00
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
{
AVFilterContext *ctx = inlink->dst;
APadContext *s = ctx->priv;
if (s->whole_len >= 0) {
s->whole_len_left = FFMAX(s->whole_len_left - frame->nb_samples, 0);
av_log(ctx, AV_LOG_DEBUG,
"n_out:%d whole_len_left:%"PRId64"\n", frame->nb_samples, s->whole_len_left);
}
s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
return ff_filter_frame(ctx->outputs[0], frame);
}
static int push_frame(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
APadContext *s = ctx->priv;
AVFrame *outsamplesref;
int n_out;
if (ctx->is_disabled)
return 0;
n_out = s->packet_size;
if (s->whole_len >= 0 && s->pad_len < 0) {
s->pad_len = s->pad_len_left = s->whole_len_left;
}
if (s->pad_len >=0 || s->whole_len >= 0) {
n_out = FFMIN(n_out, s->pad_len_left);
s->pad_len_left -= n_out;
av_log(ctx, AV_LOG_DEBUG,
"padding n_out:%d pad_len_left:%"PRId64"\n", n_out, s->pad_len_left);
}
if (!n_out)
return AVERROR_EOF;
outsamplesref = ff_get_audio_buffer(outlink, n_out);
if (!outsamplesref)
return AVERROR(ENOMEM);
av_assert0(outsamplesref->sample_rate == outlink->sample_rate);
av_assert0(outsamplesref->nb_samples == n_out);
av_samples_set_silence(outsamplesref->extended_data, 0,
n_out,
outsamplesref->ch_layout.nb_channels,
outsamplesref->format);
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);
return ff_filter_frame(outlink, outsamplesref);
}
static int activate(AVFilterContext *ctx)
{
AVFilterLink *inlink = ctx->inputs[0];
AVFilterLink *outlink = ctx->outputs[0];
APadContext *s = ctx->priv;
int64_t pts;
int status;
FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
if (!s->eof && ff_inlink_queued_frames(inlink)) {
AVFrame *frame = NULL;
int ret;
ret = ff_inlink_consume_frame(inlink, &frame);
if (ret < 0)
return ret;
if (ret > 0)
return filter_frame(inlink, frame);
}
if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts))
s->eof = status == AVERROR_EOF;
if (s->eof) {
int ret = push_frame(outlink);
if (ret == AVERROR_EOF) {
ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts);
return 0;
}
return ret;
}
FF_FILTER_FORWARD_WANTED(outlink, inlink);
return FFERROR_NOT_READY;
}
static int config_output(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
APadContext *s = ctx->priv;
if (s->pad_dur >= 0)
s->pad_len = av_rescale(s->pad_dur, outlink->sample_rate, AV_TIME_BASE);
if (s->whole_dur >= 0)
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;
}
static const AVFilterPad apad_outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_AUDIO,
.config_props = config_output,
},
};
const AVFilter ff_af_apad = {
.name = "apad",
.description = NULL_IF_CONFIG_SMALL("Pad audio with silence."),
.init = init,
.activate = activate,
.priv_size = sizeof(APadContext),
FILTER_INPUTS(ff_audio_default_filterpad),
2021-08-12 13:05:31 +02:00
FILTER_OUTPUTS(apad_outputs),
.priv_class = &apad_class,
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
};