FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_tpad.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 67 95 70.5%
Functions: 4 4 100.0%
Branches: 37 74 50.0%

Line Branch Exec Source
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
21 #include "libavutil/channel_layout.h"
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
30 typedef struct TPadContext {
31 const AVClass *class;
32 int pad_start;
33 int pad_stop;
34 int start_mode;
35 int stop_mode;
36 int64_t start_duration;
37 int64_t stop_duration;
38 uint8_t rgba_color[4]; ///< color for the padding area
39
40 FFDrawContext draw;
41 FFDrawColor color;
42 int64_t pts;
43 int eof;
44 AVFrame *cache_start;
45 AVFrame *cache_stop;
46 } TPadContext;
47
48 #define OFFSET(x) offsetof(TPadContext, x)
49 #define VF AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
50
51 static const AVOption tpad_options[] = {
52 { "start", "set the number of frames to delay input", OFFSET(pad_start), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, VF },
53 { "stop", "set the number of frames to add after input finished", OFFSET(pad_stop), AV_OPT_TYPE_INT, {.i64=0}, -1, INT_MAX, VF },
54 { "start_mode", "set the mode of added frames to start", OFFSET(start_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, VF, "mode" },
55 { "add", "add solid-color frames", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, VF, "mode" },
56 { "clone", "clone first/last frame", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, VF, "mode" },
57 { "stop_mode", "set the mode of added frames to end", OFFSET(stop_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, VF, "mode" },
58 { "start_duration", "set the duration to delay input", OFFSET(start_duration), AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT64_MAX, VF },
59 { "stop_duration", "set the duration to pad input", OFFSET(stop_duration), AV_OPT_TYPE_DURATION, {.i64=0}, 0, INT64_MAX, VF },
60 { "color", "set the color of the added frames", OFFSET(rgba_color), AV_OPT_TYPE_COLOR, {.str="black"}, 0, 0, VF },
61 { NULL }
62 };
63
64 AVFILTER_DEFINE_CLASS(tpad);
65
66 2 static int query_formats(AVFilterContext *ctx)
67 {
68 2 return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
69 }
70
71 19 static int activate(AVFilterContext *ctx)
72 {
73 19 AVFilterLink *inlink = ctx->inputs[0];
74 19 AVFilterLink *outlink = ctx->outputs[0];
75 19 TPadContext *s = ctx->priv;
76 19 AVFrame *frame = NULL;
77 int ret, status;
78 int64_t pts;
79
80
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
81
82
4/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 5 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 12 times.
19 if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
83
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (status == AVERROR_EOF) {
84 2 pts = av_rescale_q(pts, inlink->time_base, outlink->time_base);
85
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 if (!s->pad_stop && !s->pad_start) {
86 ff_outlink_set_status(outlink, status, pts);
87 return 0;
88 }
89 2 s->eof = 1;
90 2 s->pts += pts;
91 }
92 }
93
94
4/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 17 times.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
19 if (s->start_mode == 0 && s->pad_start > 0 && ff_outlink_frame_wanted(outlink)) {
95 2 frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!frame)
97 return AVERROR(ENOMEM);
98 2 ff_fill_rectangle(&s->draw, &s->color,
99 2 frame->data, frame->linesize,
100 2 0, 0, frame->width, frame->height);
101 2 frame->pts = s->pts;
102 2 s->pts += av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
103 2 s->pad_start--;
104 2 return ff_filter_frame(outlink, frame);
105 }
106
107
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
17 if (s->start_mode == 1 && s->pad_start > 0) {
108 if (s->eof) {
109 ff_outlink_set_status(outlink, AVERROR_EOF, 0);
110 return 0;
111 } else if (!s->cache_start && ff_inlink_queued_frames(inlink)) {
112 s->cache_start = ff_inlink_peek_frame(inlink, 0);
113 } else if (!s->cache_start) {
114 FF_FILTER_FORWARD_WANTED(outlink, inlink);
115 }
116 frame = av_frame_clone(s->cache_start);
117 if (!frame)
118 return AVERROR(ENOMEM);
119 frame->pts = s->pts;
120 s->pts += av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
121 s->pad_start--;
122 if (s->pad_start == 0)
123 s->cache_start = NULL;
124 return ff_filter_frame(outlink, frame);
125 }
126
127
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
17 if (!s->eof && !s->pad_start) {
128 10 ret = ff_inlink_consume_frame(inlink, &frame);
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (ret < 0)
130 return ret;
131
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
10 if (ret > 0) {
132
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 if (s->stop_mode == 1 && s->pad_stop != 0) {
133 2 av_frame_free(&s->cache_stop);
134 2 s->cache_stop = av_frame_clone(frame);
135 }
136 4 frame->pts += s->pts;
137 4 return ff_filter_frame(outlink, frame);
138 }
139 }
140
141
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 6 times.
13 if (s->eof) {
142
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (!s->pad_stop) {
143 2 ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
144 2 return 0;
145 }
146
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 if (s->stop_mode == 0) {
147 3 frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!frame)
149 return AVERROR(ENOMEM);
150 3 ff_fill_rectangle(&s->draw, &s->color,
151 3 frame->data, frame->linesize,
152 3 0, 0, frame->width, frame->height);
153
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (s->stop_mode == 1) {
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->cache_stop) {
155 s->pad_stop = 0;
156 ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
157 return 0;
158 }
159 2 frame = av_frame_clone(s->cache_stop);
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!frame)
161 return AVERROR(ENOMEM);
162 }
163 5 frame->pts = s->pts;
164 5 s->pts += av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
165
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (s->pad_stop > 0)
166 5 s->pad_stop--;
167 5 return ff_filter_frame(outlink, frame);
168 }
169
170
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (!s->pad_start)
171
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 FF_FILTER_FORWARD_WANTED(outlink, inlink);
172
173 return FFERROR_NOT_READY;
174 }
175
176 2 static int config_input(AVFilterLink *inlink)
177 {
178 2 AVFilterContext *ctx = inlink->dst;
179 2 TPadContext *s = ctx->priv;
180
181 2 ff_draw_init(&s->draw, inlink->format, 0);
182 2 ff_draw_color(&s->draw, &s->color, s->rgba_color);
183
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->start_duration)
185 s->pad_start = av_rescale_q(s->start_duration, inlink->frame_rate, av_inv_q(AV_TIME_BASE_Q));
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->stop_duration)
187 s->pad_stop = av_rescale_q(s->stop_duration, inlink->frame_rate, av_inv_q(AV_TIME_BASE_Q));
188
189 2 return 0;
190 }
191
192 4 static av_cold void uninit(AVFilterContext *ctx)
193 {
194 4 TPadContext *s = ctx->priv;
195
196 4 av_frame_free(&s->cache_stop);
197 4 }
198
199 static const AVFilterPad tpad_inputs[] = {
200 {
201 .name = "default",
202 .type = AVMEDIA_TYPE_VIDEO,
203 .config_props = config_input,
204 },
205 };
206
207 static const AVFilterPad tpad_outputs[] = {
208 {
209 .name = "default",
210 .type = AVMEDIA_TYPE_VIDEO,
211 },
212 };
213
214 const AVFilter ff_vf_tpad = {
215 .name = "tpad",
216 .description = NULL_IF_CONFIG_SMALL("Temporarily pad video frames."),
217 .priv_size = sizeof(TPadContext),
218 .priv_class = &tpad_class,
219 .activate = activate,
220 .uninit = uninit,
221 FILTER_INPUTS(tpad_inputs),
222 FILTER_OUTPUTS(tpad_outputs),
223 FILTER_QUERY_FUNC(query_formats),
224 };
225