FFmpeg coverage


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