2 * Copyright (c) 2019 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/avstring.h"
23 #include "libavutil/common.h"
24 #include "libavutil/internal.h"
25 #include "libavutil/opt.h"
32 typedef struct FreezeFramesContext {
34 int64_t first, last, replace;
36 AVFrame *replace_frame;
37 } FreezeFramesContext;
39 #define OFFSET(x) offsetof(FreezeFramesContext, x)
40 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
42 static const AVOption freezeframes_options[] = {
43 { "first", "set first frame to freeze", OFFSET(first), AV_OPT_TYPE_INT64, {.i64=0}, 0, INT64_MAX, FLAGS },
44 { "last", "set last frame to freeze", OFFSET(last), AV_OPT_TYPE_INT64, {.i64=0}, 0, INT64_MAX, FLAGS },
45 { "replace", "set frame to replace", OFFSET(replace), AV_OPT_TYPE_INT64, {.i64=0}, 0, INT64_MAX, FLAGS },
49 AVFILTER_DEFINE_CLASS(freezeframes);
51 static int config_output(AVFilterLink *outlink)
53 AVFilterContext *ctx = outlink->src;
54 AVFilterLink *sourcelink = ctx->inputs[0];
55 AVFilterLink *replacelink = ctx->inputs[1];
57 if (sourcelink->w != replacelink->w || sourcelink->h != replacelink->h) {
58 av_log(ctx, AV_LOG_ERROR,
59 "Input frame sizes do not match (%dx%d vs %dx%d).\n",
60 sourcelink->w, sourcelink->h,
61 replacelink->w, replacelink->h);
62 return AVERROR(EINVAL);
65 outlink->w = sourcelink->w;
66 outlink->h = sourcelink->h;
67 outlink->time_base = sourcelink->time_base;
68 outlink->sample_aspect_ratio = sourcelink->sample_aspect_ratio;
69 outlink->frame_rate = sourcelink->frame_rate;
74 static int activate(AVFilterContext *ctx)
76 AVFilterLink *outlink = ctx->outputs[0];
77 FreezeFramesContext *s = ctx->priv;
78 AVFrame *frame = NULL;
79 int drop = ctx->inputs[0]->frame_count_out >= s->first &&
80 ctx->inputs[0]->frame_count_out <= s->last;
81 int replace = ctx->inputs[1]->frame_count_out == s->replace;
84 FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, ctx);
86 if (drop && s->replace_frame) {
87 ret = ff_inlink_consume_frame(ctx->inputs[0], &frame);
92 int64_t dropped_pts = frame->pts;
94 av_frame_free(&frame);
95 frame = av_frame_clone(s->replace_frame);
97 return AVERROR(ENOMEM);
98 frame->pts = dropped_pts;
99 return ff_filter_frame(outlink, frame);
102 ret = ff_inlink_consume_frame(ctx->inputs[0], &frame);
107 return ff_filter_frame(outlink, frame);
110 ret = ff_inlink_consume_frame(ctx->inputs[1], &frame);
113 if (replace && frame) {
114 s->replace_frame = frame;
116 av_frame_free(&frame);
119 FF_FILTER_FORWARD_STATUS(ctx->inputs[0], outlink);
120 FF_FILTER_FORWARD_STATUS(ctx->inputs[1], outlink);
122 if (!drop || (drop && s->replace_frame))
123 FF_FILTER_FORWARD_WANTED(outlink, ctx->inputs[0]);
124 if (!s->replace_frame)
125 FF_FILTER_FORWARD_WANTED(outlink, ctx->inputs[1]);
127 return FFERROR_NOT_READY;
130 static av_cold void uninit(AVFilterContext *ctx)
132 FreezeFramesContext *s = ctx->priv;
134 av_frame_free(&s->replace_frame);
137 static const AVFilterPad freezeframes_inputs[] = {
140 .type = AVMEDIA_TYPE_VIDEO,
144 .type = AVMEDIA_TYPE_VIDEO,
149 static const AVFilterPad freezeframes_outputs[] = {
152 .type = AVMEDIA_TYPE_VIDEO,
153 .config_props = config_output,
158 const AVFilter ff_vf_freezeframes = {
159 .name = "freezeframes",
160 .description = NULL_IF_CONFIG_SMALL("Freeze video frames."),
161 .priv_size = sizeof(FreezeFramesContext),
162 .priv_class = &freezeframes_class,
163 .inputs = freezeframes_inputs,
164 .outputs = freezeframes_outputs,
165 .activate = activate,