2 * Copyright (c) 2012 Michael Niedermayer
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * Based on af_aresample.c
29 #include "libavutil/avstring.h"
30 #include "libavutil/channel_layout.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/samplefmt.h"
33 #include "libavutil/avassert.h"
47 #define OFFSET(x) offsetof(APadContext, x)
48 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
50 static const AVOption apad_options[] = {
51 { "packet_size", "set silence packet size", OFFSET(packet_size), AV_OPT_TYPE_INT, { .i64 = 4096 }, 0, INT_MAX, A },
52 { "pad_len", "number of samples of silence to add", OFFSET(pad_len), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, A },
53 { "whole_len", "target number of samples in the audio stream", OFFSET(whole_len), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, A },
57 AVFILTER_DEFINE_CLASS(apad);
59 static av_cold int init(AVFilterContext *ctx, const char *args)
62 APadContext *apad = ctx->priv;
64 apad->class = &apad_class;
65 apad->next_pts = AV_NOPTS_VALUE;
67 av_opt_set_defaults(apad);
69 if ((ret = av_opt_set_from_string(apad, args, NULL, "=", ":")) < 0)
72 if (apad->whole_len && apad->pad_len) {
73 av_log(ctx, AV_LOG_ERROR, "Both whole and pad length are set, this is not possible\n");
74 return AVERROR(EINVAL);
80 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
82 AVFilterContext *ctx = inlink->dst;
83 APadContext *apad = ctx->priv;
86 apad->whole_len -= frame->audio->nb_samples;
88 apad->next_pts = frame->pts + av_rescale_q(frame->audio->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
89 return ff_filter_frame(ctx->outputs[0], frame);
92 static int request_frame(AVFilterLink *outlink)
94 AVFilterContext *ctx = outlink->src;
95 APadContext *apad = ctx->priv;
98 ret = ff_request_frame(ctx->inputs[0]);
100 if (ret == AVERROR_EOF) {
101 int n_out = apad->packet_size;
102 AVFilterBufferRef *outsamplesref;
104 if (apad->whole_len > 0) {
105 apad->pad_len = apad->whole_len;
108 if (apad->pad_len > 0) {
109 n_out = FFMIN(n_out, apad->pad_len);
110 apad->pad_len -= n_out;
116 outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n_out);
118 return AVERROR(ENOMEM);
120 av_assert0(outsamplesref->audio->sample_rate == outlink->sample_rate);
121 av_assert0(outsamplesref->audio->nb_samples == n_out);
123 av_samples_set_silence(outsamplesref->extended_data, 0,
125 outsamplesref->audio->channels,
126 outsamplesref->format);
128 outsamplesref->pts = apad->next_pts;
129 if (apad->next_pts != AV_NOPTS_VALUE)
130 apad->next_pts += av_rescale_q(n_out, (AVRational){1, outlink->sample_rate}, outlink->time_base);
132 return ff_filter_frame(outlink, outsamplesref);
137 static const AVFilterPad apad_inputs[] = {
140 .type = AVMEDIA_TYPE_AUDIO,
141 .filter_frame = filter_frame,
142 .min_perms = AV_PERM_READ,
147 static const AVFilterPad apad_outputs[] = {
150 .request_frame = request_frame,
151 .type = AVMEDIA_TYPE_AUDIO,
156 AVFilter avfilter_af_apad = {
158 .description = NULL_IF_CONFIG_SMALL("Pad audio with silence."),
160 .priv_size = sizeof(APadContext),
161 .inputs = apad_inputs,
162 .outputs = apad_outputs,
163 .priv_class = &apad_class,