FFmpeg
Loading...
Searching...
No Matches
vf_freezeframes.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include "libavutil/avstring.h"
22#include "libavutil/common.h"
23#include "libavutil/internal.h"
24#include "libavutil/opt.h"
25
26#include "avfilter.h"
27#include "filters.h"
28#include "video.h"
29
36
37#define OFFSET(x) offsetof(FreezeFramesContext, x)
38#define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
39
41 { "first", "set first frame to freeze", OFFSET(first), AV_OPT_TYPE_INT64, {.i64=0}, 0, INT64_MAX, FLAGS },
42 { "last", "set last frame to freeze", OFFSET(last), AV_OPT_TYPE_INT64, {.i64=0}, 0, INT64_MAX, FLAGS },
43 { "replace", "set frame to replace", OFFSET(replace), AV_OPT_TYPE_INT64, {.i64=0}, 0, INT64_MAX, FLAGS },
44 { NULL },
45};
46
48
49static int config_output(AVFilterLink *outlink)
50{
51 AVFilterContext *ctx = outlink->src;
52 AVFilterLink *sourcelink = ctx->inputs[0];
53 AVFilterLink *replacelink = ctx->inputs[1];
54 FilterLink *il = ff_filter_link(sourcelink);
55 FilterLink *ol = ff_filter_link(outlink);
56
57 if (sourcelink->w != replacelink->w || sourcelink->h != replacelink->h) {
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);
63 }
64
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 ol->frame_rate = il->frame_rate;
70
71 return 0;
72}
73
75{
76 FilterLink *inl0 = ff_filter_link(ctx->inputs[0]);
77 FilterLink *inl1 = ff_filter_link(ctx->inputs[1]);
78 AVFilterLink *outlink = ctx->outputs[0];
79 FreezeFramesContext *s = ctx->priv;
81 int drop = inl0->frame_count_out >= s->first &&
82 inl0->frame_count_out <= s->last;
83 int replace = inl1->frame_count_out == s->replace;
84 int ret;
85
87
88 if (drop && s->replace_frame) {
89 ret = ff_inlink_consume_frame(ctx->inputs[0], &frame);
90 if (ret < 0)
91 return ret;
92
93 if (frame) {
94 int64_t dropped_pts = frame->pts;
95
97 frame = av_frame_clone(s->replace_frame);
98 if (!frame)
99 return AVERROR(ENOMEM);
100 frame->pts = dropped_pts;
101 return ff_filter_frame(outlink, frame);
102 }
103 } else if (!drop) {
104 ret = ff_inlink_consume_frame(ctx->inputs[0], &frame);
105 if (ret < 0)
106 return ret;
107
108 if (frame)
109 return ff_filter_frame(outlink, frame);
110 }
111
112 ret = ff_inlink_consume_frame(ctx->inputs[1], &frame);
113 if (ret < 0)
114 return ret;
115 if (replace && frame) {
116 s->replace_frame = frame;
117 } else if (frame) {
119 }
120
121 FF_FILTER_FORWARD_STATUS(ctx->inputs[0], outlink);
122 FF_FILTER_FORWARD_STATUS(ctx->inputs[1], outlink);
123
124 if (!drop || (drop && s->replace_frame))
125 FF_FILTER_FORWARD_WANTED(outlink, ctx->inputs[0]);
126 if (!s->replace_frame)
127 FF_FILTER_FORWARD_WANTED(outlink, ctx->inputs[1]);
128
129 return FFERROR_NOT_READY;
130}
131
133{
134 FreezeFramesContext *s = ctx->priv;
135
136 av_frame_free(&s->replace_frame);
137}
138
140 {
141 .name = "source",
142 .type = AVMEDIA_TYPE_VIDEO,
143 },
144 {
145 .name = "replace",
146 .type = AVMEDIA_TYPE_VIDEO,
147 },
148};
149
151 {
152 .name = "default",
153 .type = AVMEDIA_TYPE_VIDEO,
154 .config_props = config_output,
155 },
156};
157
159 .p.name = "freezeframes",
160 .p.description = NULL_IF_CONFIG_SMALL("Freeze video frames."),
161 .p.priv_class = &freezeframes_class,
162 .priv_size = sizeof(FreezeFramesContext),
165 .activate = activate,
166 .uninit = uninit,
167};
const FFFilter ff_vf_freezeframes
static AVFormatContext * ctx
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition avfilter.c:1520
Main libavfilter public API header.
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
common internal and external API header
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVFrame * frame
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition opt.h:262
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition frame.c:483
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
Definition filters.h:694
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
Definition filters.h:666
#define FFERROR_NOT_READY
Filters implementation helper functions and internal structures.
Definition filters.h:34
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Definition filters.h:652
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
AVOptions.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
#define av_log(a,...)
static const AVFilterPad freezeframes_inputs[]
static int activate(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
static const AVOption freezeframes_options[]
static const AVFilterPad freezeframes_outputs[]
#define OFFSET(x)
static int config_output(AVFilterLink *outlink)