FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_tpad.c
Date: 2024-03-29 01:21:52
Exec Total Coverage
Lines: 79 108 73.1%
Functions: 5 5 100.0%
Branches: 48 90 53.3%

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 #include "video.h"
30
31 enum PadMode {
32 MODE_ADD = 0,
33 MODE_CLONE,
34 NB_MODE
35 };
36
37 typedef struct TPadContext {
38 const AVClass *class;
39 int pad_start;
40 int pad_stop;
41 int start_mode;
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
47 FFDrawContext draw;
48 FFDrawColor color;
49 int64_t pts;
50 int eof;
51 AVFrame *cache_start;
52 AVFrame *cache_stop;
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, .unit = "mode" },
62 { "add", "add solid-color frames", 0, AV_OPT_TYPE_CONST, {.i64=MODE_ADD}, 0, 0, VF, .unit = "mode" },
63 { "clone", "clone first/last frame", 0, AV_OPT_TYPE_CONST, {.i64=MODE_CLONE}, 0, 0, VF, .unit = "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, .unit = "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
71 AVFILTER_DEFINE_CLASS(tpad);
72
73 6 static int needs_drawing(const TPadContext *s) {
74 return (
75
5/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
8 (s->stop_mode == MODE_ADD && (s->pad_stop != 0 || s->stop_duration != 0)) ||
76
2/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
2 (s->start_mode == MODE_ADD && (s->pad_start != 0 || s->start_duration != 0))
77 );
78 }
79
80 3 static int query_formats(AVFilterContext *ctx)
81 {
82 3 TPadContext *s = ctx->priv;
83
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 if (needs_drawing(s))
84 3 return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
85
86 return ff_set_common_formats(ctx, ff_all_formats(AVMEDIA_TYPE_VIDEO));
87 }
88
89 29 static int activate(AVFilterContext *ctx)
90 {
91 29 AVFilterLink *inlink = ctx->inputs[0];
92 29 AVFilterLink *outlink = ctx->outputs[0];
93 29 TPadContext *s = ctx->priv;
94 29 AVFrame *frame = NULL;
95 int ret, status;
96 int64_t duration, pts;
97
98
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
29 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
99
100
4/4
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 8 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 18 times.
29 if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
101
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (status == AVERROR_EOF) {
102 3 pts = av_rescale_q(pts, inlink->time_base, outlink->time_base);
103
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 if (!s->pad_stop && !s->pad_start) {
104 ff_outlink_set_status(outlink, status, pts);
105 return 0;
106 }
107 3 s->eof = 1;
108 3 s->pts += pts;
109 }
110 }
111
112
4/6
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 26 times.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
29 if (s->start_mode == MODE_ADD && s->pad_start > 0 && ff_outlink_frame_wanted(outlink)) {
113 3 frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!frame)
115 return AVERROR(ENOMEM);
116 3 ff_fill_rectangle(&s->draw, &s->color,
117 3 frame->data, frame->linesize,
118 3 0, 0, frame->width, frame->height);
119 3 duration = av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
120 3 frame->pts = s->pts;
121 3 frame->duration = duration;
122 3 s->pts += duration;
123 3 s->pad_start--;
124 3 return ff_filter_frame(outlink, frame);
125 }
126
127
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
26 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) {
134 FF_FILTER_FORWARD_WANTED(outlink, inlink);
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;
141 frame->duration = duration;
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
3/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
26 if (!s->eof && !s->pad_start) {
150 15 ret = ff_inlink_consume_frame(inlink, &frame);
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (ret < 0)
152 return ret;
153
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 9 times.
15 if (ret > 0) {
154
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
6 if (s->stop_mode == MODE_CLONE && s->pad_stop != 0) {
155 2 av_frame_free(&s->cache_stop);
156 2 s->cache_stop = av_frame_clone(frame);
157 }
158 6 frame->pts += s->pts;
159 6 return ff_filter_frame(outlink, frame);
160 }
161 }
162
163
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 9 times.
20 if (s->eof) {
164
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 8 times.
11 if (!s->pad_stop) {
165 3 ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
166 3 return 0;
167 }
168
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 if (s->stop_mode == MODE_ADD) {
169 6 frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!frame)
171 return AVERROR(ENOMEM);
172 6 ff_fill_rectangle(&s->draw, &s->color,
173 6 frame->data, frame->linesize,
174 6 0, 0, frame->width, frame->height);
175
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (s->stop_mode == MODE_CLONE) {
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->cache_stop) {
177 s->pad_stop = 0;
178 ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
179 return 0;
180 }
181 2 frame = av_frame_clone(s->cache_stop);
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!frame)
183 return AVERROR(ENOMEM);
184 }
185 8 duration = av_rescale_q(1, av_inv_q(outlink->frame_rate), outlink->time_base);
186 8 frame->pts = s->pts;
187 8 frame->duration = duration;
188 8 s->pts += duration;
189
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (s->pad_stop > 0)
190 8 s->pad_stop--;
191 8 return ff_filter_frame(outlink, frame);
192 }
193
194
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (!s->pad_start)
195
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
9 FF_FILTER_FORWARD_WANTED(outlink, inlink);
196
197 return FFERROR_NOT_READY;
198 }
199
200 3 static int config_input(AVFilterLink *inlink)
201 {
202 3 AVFilterContext *ctx = inlink->dst;
203 3 TPadContext *s = ctx->priv;
204
205
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 if (needs_drawing(s)) {
206 3 ff_draw_init2(&s->draw, inlink->format, inlink->colorspace, inlink->color_range, 0);
207 3 ff_draw_color(&s->draw, &s->color, s->rgba_color);
208 }
209
210
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (s->start_duration)
211 1 s->pad_start = av_rescale_q(s->start_duration, inlink->frame_rate, av_inv_q(AV_TIME_BASE_Q));
212
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (s->stop_duration)
213 1 s->pad_stop = av_rescale_q(s->stop_duration, inlink->frame_rate, av_inv_q(AV_TIME_BASE_Q));
214
215 3 return 0;
216 }
217
218 6 static av_cold void uninit(AVFilterContext *ctx)
219 {
220 6 TPadContext *s = ctx->priv;
221
222 6 av_frame_free(&s->cache_stop);
223 6 }
224
225 static const AVFilterPad tpad_inputs[] = {
226 {
227 .name = "default",
228 .type = AVMEDIA_TYPE_VIDEO,
229 .config_props = config_input,
230 },
231 };
232
233 const AVFilter ff_vf_tpad = {
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,
240 FILTER_INPUTS(tpad_inputs),
241 FILTER_OUTPUTS(ff_video_default_filterpad),
242 FILTER_QUERY_FUNC(query_formats),
243 };
244