FFmpeg
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/avassert.h"
22 #include "libavutil/avstring.h"
23 #include "libavutil/common.h"
24 #include "libavutil/internal.h"
25 #include "libavutil/opt.h"
26 
27 #include "avfilter.h"
28 #include "filters.h"
29 #include "internal.h"
30 #include "video.h"
31 
32 typedef struct FreezeFramesContext {
33  const AVClass *class;
34  int64_t first, last, replace;
35 
38 
39 #define OFFSET(x) offsetof(FreezeFramesContext, x)
40 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
41 
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 },
46  { NULL },
47 };
48 
49 AVFILTER_DEFINE_CLASS(freezeframes);
50 
51 static int config_output(AVFilterLink *outlink)
52 {
53  AVFilterContext *ctx = outlink->src;
54  AVFilterLink *sourcelink = ctx->inputs[0];
55  AVFilterLink *replacelink = ctx->inputs[1];
56 
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);
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  outlink->frame_rate = sourcelink->frame_rate;
70 
71  return 0;
72 }
73 
75 {
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;
82  int ret;
83 
85 
86  if (drop && s->replace_frame) {
87  ret = ff_inlink_consume_frame(ctx->inputs[0], &frame);
88  if (ret < 0)
89  return ret;
90 
91  if (frame) {
92  int64_t dropped_pts = frame->pts;
93 
94  av_frame_free(&frame);
95  frame = av_frame_clone(s->replace_frame);
96  if (!frame)
97  return AVERROR(ENOMEM);
98  frame->pts = dropped_pts;
99  return ff_filter_frame(outlink, frame);
100  }
101  } else if (!drop) {
102  ret = ff_inlink_consume_frame(ctx->inputs[0], &frame);
103  if (ret < 0)
104  return ret;
105 
106  if (frame)
107  return ff_filter_frame(outlink, frame);
108  }
109 
110  ret = ff_inlink_consume_frame(ctx->inputs[1], &frame);
111  if (ret < 0)
112  return ret;
113  if (replace && frame) {
114  s->replace_frame = frame;
115  } else if (frame) {
116  av_frame_free(&frame);
117  }
118 
119  FF_FILTER_FORWARD_STATUS(ctx->inputs[0], outlink);
120  FF_FILTER_FORWARD_STATUS(ctx->inputs[1], outlink);
121 
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]);
126 
127  return FFERROR_NOT_READY;
128 }
129 
131 {
132  FreezeFramesContext *s = ctx->priv;
133 
135 }
136 
138  {
139  .name = "source",
140  .type = AVMEDIA_TYPE_VIDEO,
141  },
142  {
143  .name = "replace",
144  .type = AVMEDIA_TYPE_VIDEO,
145  },
146  { NULL },
147 };
148 
150  {
151  .name = "default",
152  .type = AVMEDIA_TYPE_VIDEO,
153  .config_props = config_output,
154  },
155  { NULL },
156 };
157 
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,
166  .uninit = uninit,
167 };
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link&#39;s FIFO and update the link&#39;s stats.
Definition: avfilter.c:1489
#define NULL
Definition: coverity.c:32
This structure describes decoded (raw) audio or video data.
Definition: frame.h:308
AVOption.
Definition: opt.h:248
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Definition: filters.h:212
Main libavfilter public API header.
FF_FILTER_FORWARD_STATUS(inlink, outlink)
return FFERROR_NOT_READY
AVFilter ff_vf_freezeframes
const char * name
Pad name.
Definition: internal.h:60
static const AVFilterPad freezeframes_outputs[]
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:347
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1091
#define av_cold
Definition: attributes.h:88
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:401
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:54
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:153
void * priv
private data for use by the filter
Definition: avfilter.h:354
simple assert() macros that are a bit more flexible than ISO C assert().
static int config_output(AVFilterLink *outlink)
static av_cold void uninit(AVFilterContext *ctx)
common internal API header
static const AVFilterPad freezeframes_inputs[]
AVFormatContext * ctx
Definition: movenc.c:48
#define FLAGS
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
#define s(width, name)
Definition: cbs_vp9.c:257
AVFILTER_DEFINE_CLASS(freezeframes)
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:553
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
static const AVOption freezeframes_options[]
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:351
common internal and external API header
FF_FILTER_FORWARD_WANTED(outlink, inlink)
An instance of a filter.
Definition: avfilter.h:339
static int activate(AVFilterContext *ctx)
#define OFFSET(x)
internal API functions
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions