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/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 "internal.h"
29 #include "video.h"
30 
31 typedef struct FreezeFramesContext {
32  const AVClass *class;
33  int64_t first, last, replace;
34 
37 
38 #define OFFSET(x) offsetof(FreezeFramesContext, x)
39 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
40 
41 static const AVOption freezeframes_options[] = {
42  { "first", "set first frame to freeze", OFFSET(first), AV_OPT_TYPE_INT64, {.i64=0}, 0, INT64_MAX, FLAGS },
43  { "last", "set last frame to freeze", OFFSET(last), AV_OPT_TYPE_INT64, {.i64=0}, 0, INT64_MAX, FLAGS },
44  { "replace", "set frame to replace", OFFSET(replace), AV_OPT_TYPE_INT64, {.i64=0}, 0, INT64_MAX, FLAGS },
45  { NULL },
46 };
47 
48 AVFILTER_DEFINE_CLASS(freezeframes);
49 
50 static int config_output(AVFilterLink *outlink)
51 {
52  AVFilterContext *ctx = outlink->src;
53  AVFilterLink *sourcelink = ctx->inputs[0];
54  AVFilterLink *replacelink = ctx->inputs[1];
55 
56  if (sourcelink->w != replacelink->w || sourcelink->h != replacelink->h) {
58  "Input frame sizes do not match (%dx%d vs %dx%d).\n",
59  sourcelink->w, sourcelink->h,
60  replacelink->w, replacelink->h);
61  return AVERROR(EINVAL);
62  }
63 
64  outlink->w = sourcelink->w;
65  outlink->h = sourcelink->h;
66  outlink->time_base = sourcelink->time_base;
67  outlink->sample_aspect_ratio = sourcelink->sample_aspect_ratio;
68  outlink->frame_rate = sourcelink->frame_rate;
69 
70  return 0;
71 }
72 
74 {
75  AVFilterLink *outlink = ctx->outputs[0];
76  FreezeFramesContext *s = ctx->priv;
77  AVFrame *frame = NULL;
78  int drop = ctx->inputs[0]->frame_count_out >= s->first &&
79  ctx->inputs[0]->frame_count_out <= s->last;
80  int replace = ctx->inputs[1]->frame_count_out == s->replace;
81  int ret;
82 
84 
85  if (drop && s->replace_frame) {
86  ret = ff_inlink_consume_frame(ctx->inputs[0], &frame);
87  if (ret < 0)
88  return ret;
89 
90  if (frame) {
91  int64_t dropped_pts = frame->pts;
92 
94  frame = av_frame_clone(s->replace_frame);
95  if (!frame)
96  return AVERROR(ENOMEM);
97  frame->pts = dropped_pts;
98  return ff_filter_frame(outlink, frame);
99  }
100  } else if (!drop) {
101  ret = ff_inlink_consume_frame(ctx->inputs[0], &frame);
102  if (ret < 0)
103  return ret;
104 
105  if (frame)
106  return ff_filter_frame(outlink, frame);
107  }
108 
109  ret = ff_inlink_consume_frame(ctx->inputs[1], &frame);
110  if (ret < 0)
111  return ret;
112  if (replace && frame) {
113  s->replace_frame = frame;
114  } else if (frame) {
116  }
117 
118  FF_FILTER_FORWARD_STATUS(ctx->inputs[0], outlink);
119  FF_FILTER_FORWARD_STATUS(ctx->inputs[1], outlink);
120 
121  if (!drop || (drop && s->replace_frame))
122  FF_FILTER_FORWARD_WANTED(outlink, ctx->inputs[0]);
123  if (!s->replace_frame)
124  FF_FILTER_FORWARD_WANTED(outlink, ctx->inputs[1]);
125 
126  return FFERROR_NOT_READY;
127 }
128 
130 {
131  FreezeFramesContext *s = ctx->priv;
132 
133  av_frame_free(&s->replace_frame);
134 }
135 
137  {
138  .name = "source",
139  .type = AVMEDIA_TYPE_VIDEO,
140  },
141  {
142  .name = "replace",
143  .type = AVMEDIA_TYPE_VIDEO,
144  },
145 };
146 
148  {
149  .name = "default",
150  .type = AVMEDIA_TYPE_VIDEO,
151  .config_props = config_output,
152  },
153 };
154 
156  .name = "freezeframes",
157  .description = NULL_IF_CONFIG_SMALL("Freeze video frames."),
158  .priv_size = sizeof(FreezeFramesContext),
159  .priv_class = &freezeframes_class,
162  .activate = activate,
163  .uninit = uninit,
164 };
AVERROR
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
opt.h
config_output
static int config_output(AVFilterLink *outlink)
Definition: vf_freezeframes.c:50
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:109
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
AVOption
AVOption.
Definition: opt.h:247
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:169
video.h
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_freezeframes.c:129
ff_inlink_consume_frame
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:1417
OFFSET
#define OFFSET(x)
Definition: vf_freezeframes.c:38
FF_FILTER_FORWARD_STATUS_BACK_ALL
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Definition: filters.h:212
ff_vf_freezeframes
const AVFilter ff_vf_freezeframes
Definition: vf_freezeframes.c:155
FLAGS
#define FLAGS
Definition: vf_freezeframes.c:39
FreezeFramesContext
Definition: vf_freezeframes.c:31
freezeframes_inputs
static const AVFilterPad freezeframes_inputs[]
Definition: vf_freezeframes.c:136
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
first
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But first
Definition: rate_distortion.txt:12
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_OPT_TYPE_INT64
@ AV_OPT_TYPE_INT64
Definition: opt.h:225
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:48
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:422
freezeframes_options
static const AVOption freezeframes_options[]
Definition: vf_freezeframes.c:41
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
FreezeFramesContext::replace
int64_t replace
Definition: vf_freezeframes.c:33
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
FF_FILTER_FORWARD_WANTED
FF_FILTER_FORWARD_WANTED(outlink, inlink)
internal.h
FreezeFramesContext::first
int64_t first
Definition: vf_freezeframes.c:33
internal.h
common.h
FreezeFramesContext::replace_frame
AVFrame * replace_frame
Definition: vf_freezeframes.c:35
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
AVFilter
Filter definition.
Definition: avfilter.h:165
ret
ret
Definition: filter_design.txt:187
activate
static int activate(AVFilterContext *ctx)
Definition: vf_freezeframes.c:73
frame
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
Definition: filter_design.txt:264
avfilter.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:402
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(freezeframes)
FF_FILTER_FORWARD_STATUS
FF_FILTER_FORWARD_STATUS(inlink, outlink)
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
FreezeFramesContext::last
int64_t last
Definition: vf_freezeframes.c:33
avstring.h
freezeframes_outputs
static const AVFilterPad freezeframes_outputs[]
Definition: vf_freezeframes.c:147