FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_swaprect.c
Date: 2024-04-24 18:52:15
Exec Total Coverage
Lines: 83 91 91.2%
Functions: 4 4 100.0%
Branches: 24 44 54.5%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2015 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/avassert.h"
22 #include "libavutil/eval.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/mem.h"
25 #include "libavutil/opt.h"
26
27 #include "avfilter.h"
28 #include "formats.h"
29 #include "internal.h"
30 #include "video.h"
31
32 typedef struct SwapRectContext {
33 const AVClass *class;
34 char *w, *h;
35 char *x1, *y1;
36 char *x2, *y2;
37
38 int nb_planes;
39 int pixsteps[4];
40
41 const AVPixFmtDescriptor *desc;
42 uint8_t *temp;
43 } SwapRectContext;
44
45 #define OFFSET(x) offsetof(SwapRectContext, x)
46 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
47 static const AVOption swaprect_options[] = {
48 { "w", "set rect width", OFFSET(w), AV_OPT_TYPE_STRING, {.str="w/2"}, 0, 0, .flags = FLAGS },
49 { "h", "set rect height", OFFSET(h), AV_OPT_TYPE_STRING, {.str="h/2"}, 0, 0, .flags = FLAGS },
50 { "x1", "set 1st rect x top left coordinate", OFFSET(x1), AV_OPT_TYPE_STRING, {.str="w/2"}, 0, 0, .flags = FLAGS },
51 { "y1", "set 1st rect y top left coordinate", OFFSET(y1), AV_OPT_TYPE_STRING, {.str="h/2"}, 0, 0, .flags = FLAGS },
52 { "x2", "set 2nd rect x top left coordinate", OFFSET(x2), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, .flags = FLAGS },
53 { "y2", "set 2nd rect y top left coordinate", OFFSET(y2), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, .flags = FLAGS },
54 { NULL },
55 };
56
57 AVFILTER_DEFINE_CLASS(swaprect);
58
59 1 static int query_formats(AVFilterContext *ctx)
60 {
61 1 int reject_flags = AV_PIX_FMT_FLAG_PAL |
62 AV_PIX_FMT_FLAG_HWACCEL |
63 AV_PIX_FMT_FLAG_BITSTREAM;
64
65 1 return ff_set_common_formats(ctx, ff_formats_pixdesc_filter(0, reject_flags));
66 }
67
68 static const char *const var_names[] = { "w", "h", "a", "n", "t",
69 #if FF_API_FRAME_PKT
70 "pos",
71 #endif
72 "sar", "dar", NULL };
73 enum { VAR_W, VAR_H, VAR_A, VAR_N, VAR_T,
74 #if FF_API_FRAME_PKT
75 VAR_POS,
76 #endif
77 VAR_SAR, VAR_DAR, VAR_VARS_NB };
78
79 50 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
80 {
81 50 AVFilterContext *ctx = inlink->dst;
82 50 AVFilterLink *outlink = ctx->outputs[0];
83 50 SwapRectContext *s = ctx->priv;
84 double var_values[VAR_VARS_NB];
85 int x1[4], y1[4];
86 int x2[4], y2[4];
87 int aw[4], ah[4];
88 int lw[4], lh[4];
89 int pw[4], ph[4];
90 double dw, dh;
91 double dx1, dy1;
92 double dx2, dy2;
93 int y, p, w, h, ret;
94
95 50 var_values[VAR_W] = inlink->w;
96 50 var_values[VAR_H] = inlink->h;
97 50 var_values[VAR_A] = (float) inlink->w / inlink->h;
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ? av_q2d(inlink->sample_aspect_ratio) : 1;
99 50 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
100 50 var_values[VAR_N] = inlink->frame_count_out;
101
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 var_values[VAR_T] = in->pts == AV_NOPTS_VALUE ? NAN : in->pts * av_q2d(inlink->time_base);
102 #if FF_API_FRAME_PKT
103 FF_DISABLE_DEPRECATION_WARNINGS
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 var_values[VAR_POS] = in->pkt_pos == -1 ? NAN : in->pkt_pos;
105 FF_ENABLE_DEPRECATION_WARNINGS
106 #endif
107
108 50 ret = av_expr_parse_and_eval(&dw, s->w,
109 var_names, &var_values[0],
110 NULL, NULL, NULL, NULL,
111 0, 0, ctx);
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (ret < 0)
113 return ret;
114
115 50 ret = av_expr_parse_and_eval(&dh, s->h,
116 var_names, &var_values[0],
117 NULL, NULL, NULL, NULL,
118 0, 0, ctx);
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (ret < 0)
120 return ret;
121
122 50 ret = av_expr_parse_and_eval(&dx1, s->x1,
123 var_names, &var_values[0],
124 NULL, NULL, NULL, NULL,
125 0, 0, ctx);
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (ret < 0)
127 return ret;
128
129 50 ret = av_expr_parse_and_eval(&dy1, s->y1,
130 var_names, &var_values[0],
131 NULL, NULL, NULL, NULL,
132 0, 0, ctx);
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (ret < 0)
134 return ret;
135
136 50 ret = av_expr_parse_and_eval(&dx2, s->x2,
137 var_names, &var_values[0],
138 NULL, NULL, NULL, NULL,
139 0, 0, ctx);
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (ret < 0)
141 return ret;
142
143 50 ret = av_expr_parse_and_eval(&dy2, s->y2,
144 var_names, &var_values[0],
145 NULL, NULL, NULL, NULL,
146 0, 0, ctx);
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (ret < 0)
148 return ret;
149
150 50 w = dw; h = dh; x1[0] = dx1; y1[0] = dy1; x2[0] = dx2; y2[0] = dy2;
151
152 50 x1[0] = av_clip(x1[0], 0, inlink->w - 1);
153 50 y1[0] = av_clip(y1[0], 0, inlink->h - 1);
154
155 50 x2[0] = av_clip(x2[0], 0, inlink->w - 1);
156 50 y2[0] = av_clip(y2[0], 0, inlink->h - 1);
157
158 50 ah[1] = ah[2] = AV_CEIL_RSHIFT(h, s->desc->log2_chroma_h);
159 50 ah[0] = ah[3] = h;
160 50 aw[1] = aw[2] = AV_CEIL_RSHIFT(w, s->desc->log2_chroma_w);
161 50 aw[0] = aw[3] = w;
162
163 50 w = FFMIN3(w, inlink->w - x1[0], inlink->w - x2[0]);
164 50 h = FFMIN3(h, inlink->h - y1[0], inlink->h - y2[0]);
165
166 50 ph[1] = ph[2] = AV_CEIL_RSHIFT(h, s->desc->log2_chroma_h);
167 50 ph[0] = ph[3] = h;
168 50 pw[1] = pw[2] = AV_CEIL_RSHIFT(w, s->desc->log2_chroma_w);
169 50 pw[0] = pw[3] = w;
170
171 50 lh[1] = lh[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h);
172 50 lh[0] = lh[3] = inlink->h;
173 50 lw[1] = lw[2] = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w);
174 50 lw[0] = lw[3] = inlink->w;
175
176 50 x1[1] = x1[2] = (x1[0] >> s->desc->log2_chroma_w);
177 50 x1[0] = x1[3] = x1[0];
178 50 y1[1] = y1[2] = (y1[0] >> s->desc->log2_chroma_h);
179 50 y1[0] = y1[3] = y1[0];
180
181 50 x2[1] = x2[2] = (x2[0] >> s->desc->log2_chroma_w);
182 50 x2[0] = x2[3] = x2[0];
183 50 y2[1] = y2[2] = (y2[0] >> s->desc->log2_chroma_h);
184 50 y2[0] = y2[3] = y2[0];
185
186
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 av_assert0(FFMAX(x1[1], x2[1]) + pw[1] <= lw[1]);
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 av_assert0(FFMAX(y1[1], y2[1]) + ph[1] <= lh[1]);
189
190
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 50 times.
200 for (p = 0; p < s->nb_planes; p++) {
191
2/4
✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 150 times.
✗ Branch 3 not taken.
150 if (ph[p] == ah[p] && pw[p] == aw[p]) {
192 150 uint8_t *src = in->data[p] + y1[p] * in->linesize[p] + x1[p] * s->pixsteps[p];
193 150 uint8_t *dst = in->data[p] + y2[p] * in->linesize[p] + x2[p] * s->pixsteps[p];
194
195
2/2
✓ Branch 0 taken 14400 times.
✓ Branch 1 taken 150 times.
14550 for (y = 0; y < ph[p]; y++) {
196 14400 memcpy(s->temp, src, pw[p] * s->pixsteps[p]);
197 14400 memmove(src, dst, pw[p] * s->pixsteps[p]);
198 14400 memcpy(dst, s->temp, pw[p] * s->pixsteps[p]);
199 14400 src += in->linesize[p];
200 14400 dst += in->linesize[p];
201 }
202 }
203 }
204
205 50 return ff_filter_frame(outlink, in);
206 }
207
208 1 static int config_input(AVFilterLink *inlink)
209 {
210 1 AVFilterContext *ctx = inlink->dst;
211 1 SwapRectContext *s = ctx->priv;
212
213
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (!s->w || !s->h ||
214
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 !s->x1 || !s->y1 ||
215
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 !s->x2 || !s->y2)
216 return AVERROR(EINVAL);
217
218 1 s->desc = av_pix_fmt_desc_get(inlink->format);
219 1 av_image_fill_max_pixsteps(s->pixsteps, NULL, s->desc);
220 1 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
221
222 1 s->temp = av_malloc_array(inlink->w, s->pixsteps[0]);
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!s->temp)
224 return AVERROR(ENOMEM);
225
226 1 return 0;
227 }
228
229 2 static av_cold void uninit(AVFilterContext *ctx)
230 {
231 2 SwapRectContext *s = ctx->priv;
232 2 av_freep(&s->temp);
233 2 }
234
235 static const AVFilterPad inputs[] = {
236 {
237 .name = "default",
238 .type = AVMEDIA_TYPE_VIDEO,
239 .flags = AVFILTERPAD_FLAG_NEEDS_WRITABLE,
240 .filter_frame = filter_frame,
241 .config_props = config_input,
242 },
243 };
244
245 const AVFilter ff_vf_swaprect = {
246 .name = "swaprect",
247 .description = NULL_IF_CONFIG_SMALL("Swap 2 rectangular objects in video."),
248 .priv_size = sizeof(SwapRectContext),
249 .priv_class = &swaprect_class,
250 .uninit = uninit,
251 FILTER_INPUTS(inputs),
252 FILTER_OUTPUTS(ff_video_default_filterpad),
253 FILTER_QUERY_FUNC(query_formats),
254 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
255 .process_command = ff_filter_process_command,
256 };
257