2 * Copyright (c) 2018 Paul B Mahol
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
21 #include "libavutil/avassert.h"
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/opt.h"
29 #include "drawutils.h"
31 typedef struct TPadContext {
37 int64_t start_duration;
38 int64_t stop_duration;
39 uint8_t rgba_color[4]; ///< color for the padding area
49 #define OFFSET(x) offsetof(TPadContext, x)
50 #define VF AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
52 static const AVOption tpad_options[] = {
53 { "start", "set the number of frames to delay input", OFFSET(pad_start), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, VF },
54 { "stop", "set the number of frames to add after input finished", OFFSET(pad_stop), AV_OPT_TYPE_INT, {.i64=0}, -1, INT_MAX, VF },
55 { "start_mode", "set the mode of added frames to start", OFFSET(start_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, VF, "mode" },
56 { "add", "add solid-color frames", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, VF, "mode" },
57 { "clone", "clone first/last frame", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, VF, "mode" },
58 { "stop_mode", "set the mode of added frames to end", OFFSET(stop_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, VF, "mode" },
59 { "start_duration", "set the duration to delay input", OFFSET(start_duration), AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT64_MAX, VF },
60 { "stop_duration", "set the duration to pad input", OFFSET(stop_duration), AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT64_MAX, VF },
61 { "color", "set the color of the added frames", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str="black"}, 0, 0, VF },
65 AVFILTER_DEFINE_CLASS(tpad);
67 static int query_formats(AVFilterContext *ctx)
69 return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
72 static int activate(AVFilterContext *ctx)
74 AVFilterLink *inlink = ctx->inputs[0];
75 AVFilterLink *outlink = ctx->outputs[0];
76 TPadContext *s = ctx->priv;
77 AVFrame *frame = NULL;
81 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
83 if (s->start_mode == 0 && s->pad_start > 0 && ff_outlink_frame_wanted(outlink)) {
84 frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
86 return AVERROR(ENOMEM);
87 ff_fill_rectangle(&s->draw, &s->color,
88 frame->data, frame->linesize,
89 0, 0, frame->width, frame->height);
91 s->pts += av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
93 return ff_filter_frame(outlink, frame);
96 if (s->start_mode == 1 && s->pad_start > 0) {
97 if (!s->cache_start && ff_inlink_queued_frames(inlink)) {
98 s->cache_start = ff_inlink_peek_frame(inlink, 0);
99 } else if (!s->cache_start) {
100 FF_FILTER_FORWARD_WANTED(outlink, inlink);
102 frame = av_frame_clone(s->cache_start);
104 return AVERROR(ENOMEM);
106 s->pts += av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
108 if (s->pad_start == 0)
109 s->cache_start = NULL;
110 return ff_filter_frame(outlink, frame);
113 if (!s->eof && !s->pad_start) {
114 ret = ff_inlink_consume_frame(inlink, &frame);
118 if (s->stop_mode == 1 && s->pad_stop != 0) {
119 av_frame_free(&s->cache_stop);
120 s->cache_stop = av_frame_clone(frame);
122 frame->pts += s->pts;
123 return ff_filter_frame(outlink, frame);
127 if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
128 if (status == AVERROR_EOF) {
130 ff_outlink_set_status(outlink, status, pts);
140 ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
143 if (s->stop_mode == 0) {
144 frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
146 return AVERROR(ENOMEM);
147 ff_fill_rectangle(&s->draw, &s->color,
148 frame->data, frame->linesize,
149 0, 0, frame->width, frame->height);
150 } else if (s->stop_mode == 1) {
151 frame = av_frame_clone(s->cache_stop);
153 return AVERROR(ENOMEM);
156 s->pts += av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
159 return ff_filter_frame(outlink, frame);
163 FF_FILTER_FORWARD_WANTED(outlink, inlink);
165 return FFERROR_NOT_READY;
168 static int config_input(AVFilterLink *inlink)
170 AVFilterContext *ctx = inlink->dst;
171 TPadContext *s = ctx->priv;
173 ff_draw_init(&s->draw, inlink->format, 0);
174 ff_draw_color(&s->draw, &s->color, s->rgba_color);
176 if (s->start_duration)
177 s->pad_start = av_rescale_q(s->start_duration, inlink->frame_rate, av_inv_q(AV_TIME_BASE_Q));
178 if (s->stop_duration)
179 s->pad_stop = av_rescale_q(s->stop_duration, inlink->frame_rate, av_inv_q(AV_TIME_BASE_Q));
184 static av_cold void uninit(AVFilterContext *ctx)
186 TPadContext *s = ctx->priv;
188 av_frame_free(&s->cache_stop);
191 static const AVFilterPad tpad_inputs[] = {
194 .type = AVMEDIA_TYPE_VIDEO,
195 .config_props = config_input,
200 static const AVFilterPad tpad_outputs[] = {
203 .type = AVMEDIA_TYPE_VIDEO,
208 AVFilter ff_vf_tpad = {
210 .description = NULL_IF_CONFIG_SMALL("Temporarily pad video frames."),
211 .priv_size = sizeof(TPadContext),
212 .priv_class = &tpad_class,
213 .query_formats = query_formats,
214 .activate = activate,
216 .inputs = tpad_inputs,
217 .outputs = tpad_outputs,