FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_swaprect.c
Date: 2022-12-05 03:11:11
Exec Total Coverage
Lines: 81 89 91.0%
Functions: 4 4 100.0%
Branches: 22 40 55.0%

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