FFmpeg
vf_tpad.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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 
22 #include "libavutil/opt.h"
23 #include "avfilter.h"
24 #include "audio.h"
25 #include "filters.h"
26 #include "internal.h"
27 #include "formats.h"
28 #include "drawutils.h"
29 #include "video.h"
30 
31 enum PadMode {
32  MODE_ADD = 0,
35 };
36 
37 typedef struct TPadContext {
38  const AVClass *class;
39  int pad_start;
40  int pad_stop;
42  int stop_mode;
43  int64_t start_duration;
44  int64_t stop_duration;
45  uint8_t rgba_color[4]; ///< color for the padding area
46 
49  int64_t pts;
50  int eof;
53 } TPadContext;
54 
55 #define OFFSET(x) offsetof(TPadContext, x)
56 #define VF AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
57 
58 static const AVOption tpad_options[] = {
59  { "start", "set the number of frames to delay input", OFFSET(pad_start), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, VF },
60  { "stop", "set the number of frames to add after input finished", OFFSET(pad_stop), AV_OPT_TYPE_INT, {.i64=0}, -1, INT_MAX, VF },
61  { "start_mode", "set the mode of added frames to start", OFFSET(start_mode), AV_OPT_TYPE_INT, {.i64=MODE_ADD}, 0, NB_MODE-1, VF, "mode" },
62  { "add", "add solid-color frames", 0, AV_OPT_TYPE_CONST, {.i64=MODE_ADD}, 0, 0, VF, "mode" },
63  { "clone", "clone first/last frame", 0, AV_OPT_TYPE_CONST, {.i64=MODE_CLONE}, 0, 0, VF, "mode" },
64  { "stop_mode", "set the mode of added frames to end", OFFSET(stop_mode), AV_OPT_TYPE_INT, {.i64=MODE_ADD}, 0, NB_MODE-1, VF, "mode" },
65  { "start_duration", "set the duration to delay input", OFFSET(start_duration), AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT64_MAX, VF },
66  { "stop_duration", "set the duration to pad input", OFFSET(stop_duration), AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT64_MAX, VF },
67  { "color", "set the color of the added frames", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str="black"}, 0, 0, VF },
68  { NULL }
69 };
70 
72 
73 static int needs_drawing(const TPadContext *s) {
74  return (
75  (s->stop_mode == MODE_ADD && (s->pad_stop != 0 || s->stop_duration != 0)) ||
76  (s->start_mode == MODE_ADD && (s->pad_start != 0 || s->start_duration != 0))
77  );
78 }
79 
81 {
82  TPadContext *s = ctx->priv;
83  if (needs_drawing(s))
85 
87 }
88 
90 {
91  AVFilterLink *inlink = ctx->inputs[0];
92  AVFilterLink *outlink = ctx->outputs[0];
93  TPadContext *s = ctx->priv;
94  AVFrame *frame = NULL;
95  int ret, status;
96  int64_t duration, pts;
97 
99 
100  if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
101  if (status == AVERROR_EOF) {
102  pts = av_rescale_q(pts, inlink->time_base, outlink->time_base);
103  if (!s->pad_stop && !s->pad_start) {
104  ff_outlink_set_status(outlink, status, pts);
105  return 0;
106  }
107  s->eof = 1;
108  s->pts += pts;
109  }
110  }
111 
112  if (s->start_mode == MODE_ADD && s->pad_start > 0 && ff_outlink_frame_wanted(outlink)) {
113  frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
114  if (!frame)
115  return AVERROR(ENOMEM);
116  ff_fill_rectangle(&s->draw, &s->color,
118  0, 0, frame->width, frame->height);
119  duration = av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
120  frame->pts = s->pts;
122  s->pts += duration;
123  s->pad_start--;
124  return ff_filter_frame(outlink, frame);
125  }
126 
127  if (s->start_mode == MODE_CLONE && s->pad_start > 0) {
128  if (s->eof) {
129  ff_outlink_set_status(outlink, AVERROR_EOF, 0);
130  return 0;
131  } else if (!s->cache_start && ff_inlink_queued_frames(inlink)) {
132  s->cache_start = ff_inlink_peek_frame(inlink, 0);
133  } else if (!s->cache_start) {
135  }
136  frame = av_frame_clone(s->cache_start);
137  if (!frame)
138  return AVERROR(ENOMEM);
139  duration = av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
140  frame->pts = s->pts;
142  s->pts += duration;
143  s->pad_start--;
144  if (s->pad_start == 0)
145  s->cache_start = NULL;
146  return ff_filter_frame(outlink, frame);
147  }
148 
149  if (!s->eof && !s->pad_start) {
151  if (ret < 0)
152  return ret;
153  if (ret > 0) {
154  if (s->stop_mode == MODE_CLONE && s->pad_stop != 0) {
155  av_frame_free(&s->cache_stop);
156  s->cache_stop = av_frame_clone(frame);
157  }
158  frame->pts += s->pts;
159  return ff_filter_frame(outlink, frame);
160  }
161  }
162 
163  if (s->eof) {
164  if (!s->pad_stop) {
165  ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
166  return 0;
167  }
168  if (s->stop_mode == MODE_ADD) {
169  frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
170  if (!frame)
171  return AVERROR(ENOMEM);
172  ff_fill_rectangle(&s->draw, &s->color,
174  0, 0, frame->width, frame->height);
175  } else if (s->stop_mode == MODE_CLONE) {
176  if (!s->cache_stop) {
177  s->pad_stop = 0;
178  ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
179  return 0;
180  }
181  frame = av_frame_clone(s->cache_stop);
182  if (!frame)
183  return AVERROR(ENOMEM);
184  }
185  duration = av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
186  frame->pts = s->pts;
188  s->pts += duration;
189  if (s->pad_stop > 0)
190  s->pad_stop--;
191  return ff_filter_frame(outlink, frame);
192  }
193 
194  if (!s->pad_start)
196 
197  return FFERROR_NOT_READY;
198 }
199 
201 {
202  AVFilterContext *ctx = inlink->dst;
203  TPadContext *s = ctx->priv;
204 
205  if (needs_drawing(s)) {
206  ff_draw_init(&s->draw, inlink->format, 0);
207  ff_draw_color(&s->draw, &s->color, s->rgba_color);
208  }
209 
210  if (s->start_duration)
211  s->pad_start = av_rescale_q(s->start_duration, inlink->frame_rate, av_inv_q(AV_TIME_BASE_Q));
212  if (s->stop_duration)
213  s->pad_stop = av_rescale_q(s->stop_duration, inlink->frame_rate, av_inv_q(AV_TIME_BASE_Q));
214 
215  return 0;
216 }
217 
219 {
220  TPadContext *s = ctx->priv;
221 
222  av_frame_free(&s->cache_stop);
223 }
224 
225 static const AVFilterPad tpad_inputs[] = {
226  {
227  .name = "default",
228  .type = AVMEDIA_TYPE_VIDEO,
229  .config_props = config_input,
230  },
231 };
232 
234  .name = "tpad",
235  .description = NULL_IF_CONFIG_SMALL("Temporarily pad video frames."),
236  .priv_size = sizeof(TPadContext),
237  .priv_class = &tpad_class,
238  .activate = activate,
239  .uninit = uninit,
243 };
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:108
FFDrawColor
Definition: drawutils.h:50
activate
static int activate(AVFilterContext *ctx)
Definition: vf_tpad.c:89
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
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:978
AVFrame::duration
int64_t duration
Duration of the frame, in the same units as pts.
Definition: frame.h:807
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
TPadContext::start_mode
int start_mode
Definition: vf_tpad.c:41
AV_TIME_BASE_Q
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:264
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:100
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:452
AVFrame::width
int width
Definition: frame.h:412
MODE_ADD
@ MODE_ADD
Definition: vf_tpad.c:32
AVOption
AVOption.
Definition: opt.h:251
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:169
TPadContext::draw
FFDrawContext draw
Definition: vf_tpad.c:47
AV_OPT_TYPE_DURATION
@ AV_OPT_TYPE_DURATION
Definition: opt.h:239
TPadContext
Definition: vf_tpad.c:37
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
video.h
TPadContext::pts
int64_t pts
Definition: vf_tpad.c:49
FF_FILTER_FORWARD_STATUS_BACK
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:199
ff_vf_tpad
const AVFilter ff_vf_tpad
Definition: vf_tpad.c:233
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
TPadContext::start_duration
int64_t start_duration
Definition: vf_tpad.c:43
formats.h
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:1383
pts
static int64_t pts
Definition: transcode_aac.c:643
ff_all_formats
AVFilterFormats * ff_all_formats(enum AVMediaType type)
Return a list of all formats supported by FFmpeg for the given media type.
Definition: formats.c:501
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:47
av_cold
#define av_cold
Definition: attributes.h:90
ff_set_common_formats
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:770
ff_video_default_filterpad
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition: video.c:36
duration
int64_t duration
Definition: movenc.c:64
ff_outlink_set_status
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:189
s
#define s(width, name)
Definition: cbs_vp9.c:198
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:609
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
ff_draw_init
int ff_draw_init(FFDrawContext *draw, enum AVPixelFormat format, unsigned flags)
Definition: drawutils.c:151
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:192
frame
static AVFrame * frame
Definition: demux_decode.c:54
ff_inlink_peek_frame
AVFrame * ff_inlink_peek_frame(AVFilterLink *link, size_t idx)
Access a frame in the link fifo without consuming it.
Definition: avfilter.c:1422
TPadContext::cache_stop
AVFrame * cache_stop
Definition: vf_tpad.c:52
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
NB_MODE
@ NB_MODE
Definition: vf_tpad.c:34
tpad_inputs
static const AVFilterPad tpad_inputs[]
Definition: vf_tpad.c:225
AV_OPT_TYPE_COLOR
@ AV_OPT_TYPE_COLOR
Definition: opt.h:240
ff_inlink_acknowledge_status
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition: avfilter.c:1337
ff_inlink_queued_frames
size_t ff_inlink_queued_frames(AVFilterLink *link)
Get the number of frames available on the link.
Definition: avfilter.c:1352
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_tpad.c:218
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:106
needs_drawing
static int needs_drawing(const TPadContext *s)
Definition: vf_tpad.c:73
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(tpad)
ff_fill_rectangle
void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color, uint8_t *dst[], int dst_linesize[], int dst_x, int dst_y, int w, int h)
Fill a rectangle with an uniform color.
Definition: drawutils.c:231
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_tpad.c:200
OFFSET
#define OFFSET(x)
Definition: vf_tpad.c:55
FF_FILTER_FORWARD_WANTED
FF_FILTER_FORWARD_WANTED(outlink, inlink)
TPadContext::pad_start
int pad_start
Definition: vf_tpad.c:39
internal.h
TPadContext::stop_mode
int stop_mode
Definition: vf_tpad.c:42
TPadContext::color
FFDrawColor color
Definition: vf_tpad.c:48
ff_draw_supported_pixel_formats
AVFilterFormats * ff_draw_supported_pixel_formats(unsigned flags)
Return the list of pixel formats supported by the draw functions.
Definition: drawutils.c:647
FFDrawContext
Definition: drawutils.h:35
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
MODE_CLONE
@ MODE_CLONE
Definition: vf_tpad.c:33
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:53
TPadContext::rgba_color
uint8_t rgba_color[4]
color for the padding area
Definition: vf_tpad.c:45
ff_draw_color
void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4])
Prepare a color.
Definition: drawutils.c:156
AVFilter
Filter definition.
Definition: avfilter.h:166
ret
ret
Definition: filter_design.txt:187
VF
#define VF
Definition: vf_tpad.c:56
AVFrame::height
int height
Definition: frame.h:412
status
ov_status_e status
Definition: dnn_backend_openvino.c:119
channel_layout.h
TPadContext::stop_duration
int64_t stop_duration
Definition: vf_tpad.c:44
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
TPadContext::eof
int eof
Definition: vf_tpad.c:50
AVFilterContext
An instance of a filter.
Definition: avfilter.h:397
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
audio.h
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:193
TPadContext::pad_stop
int pad_stop
Definition: vf_tpad.c:40
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_tpad.c:80
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:385
PadMode
PadMode
Definition: vf_tpad.c:31
ff_outlink_frame_wanted
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the ff_outlink_frame_wanted() function. If this function returns true
TPadContext::cache_start
AVFrame * cache_start
Definition: vf_tpad.c:51
drawutils.h
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
tpad_options
static const AVOption tpad_options[]
Definition: vf_tpad.c:58