FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_displace.c
Date: 2026-04-24 19:58:39
Exec Total Coverage
Lines: 0 227 0.0%
Functions: 0 7 0.0%
Branches: 0 110 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2013 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/pixdesc.h"
22 #include "libavutil/opt.h"
23 #include "avfilter.h"
24 #include "filters.h"
25 #include "framesync.h"
26 #include "video.h"
27
28 enum EdgeMode {
29 EDGE_BLANK,
30 EDGE_SMEAR,
31 EDGE_WRAP,
32 EDGE_MIRROR,
33 EDGE_NB
34 };
35
36 typedef struct DisplaceContext {
37 const AVClass *class;
38 int width[4], height[4];
39 /* enum EdgeMode */
40 int edge;
41 int nb_planes;
42 int nb_components;
43 int step;
44 uint8_t blank[4];
45 FFFrameSync fs;
46
47 int (*displace_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
48 } DisplaceContext;
49
50 #define OFFSET(x) offsetof(DisplaceContext, x)
51 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
52
53 static const AVOption displace_options[] = {
54 { "edge", "set edge mode", OFFSET(edge), AV_OPT_TYPE_INT, {.i64=EDGE_SMEAR}, 0, EDGE_NB-1, FLAGS, .unit = "edge" },
55 { "blank", "", 0, AV_OPT_TYPE_CONST, {.i64=EDGE_BLANK}, 0, 0, FLAGS, .unit = "edge" },
56 { "smear", "", 0, AV_OPT_TYPE_CONST, {.i64=EDGE_SMEAR}, 0, 0, FLAGS, .unit = "edge" },
57 { "wrap" , "", 0, AV_OPT_TYPE_CONST, {.i64=EDGE_WRAP}, 0, 0, FLAGS, .unit = "edge" },
58 { "mirror" , "", 0, AV_OPT_TYPE_CONST, {.i64=EDGE_MIRROR}, 0, 0, FLAGS, .unit = "edge" },
59 { NULL }
60 };
61
62 AVFILTER_DEFINE_CLASS(displace);
63
64 static const enum AVPixelFormat pix_fmts[] = {
65 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
66 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
67 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
68 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
69 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
70 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
71 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
72 AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR, AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
73 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
74 AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
75 };
76
77 typedef struct ThreadData {
78 AVFrame *in, *xin, *yin, *out;
79 } ThreadData;
80
81 static int displace_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
82 {
83 DisplaceContext *s = ctx->priv;
84 const ThreadData *td = arg;
85 const AVFrame *in = td->in;
86 const AVFrame *xin = td->xin;
87 const AVFrame *yin = td->yin;
88 const AVFrame *out = td->out;
89
90 for (int plane = 0; plane < s->nb_planes; plane++) {
91 const int h = s->height[plane];
92 const int w = s->width[plane];
93 const int slice_start = (h * jobnr ) / nb_jobs;
94 const int slice_end = (h * (jobnr+1)) / nb_jobs;
95 const int dlinesize = out->linesize[plane];
96 const int slinesize = in->linesize[plane];
97 const int xlinesize = xin->linesize[plane];
98 const int ylinesize = yin->linesize[plane];
99 const uint8_t *src = in->data[plane];
100 const uint8_t *ysrc = yin->data[plane] + slice_start * ylinesize;
101 const uint8_t *xsrc = xin->data[plane] + slice_start * xlinesize;
102 uint8_t *dst = out->data[plane] + slice_start * dlinesize;
103 const uint8_t blank = s->blank[plane];
104
105 for (int y = slice_start; y < slice_end; y++) {
106 switch (s->edge) {
107 case EDGE_BLANK:
108 for (int x = 0; x < w; x++) {
109 int Y = y + ysrc[x] - 128;
110 int X = x + xsrc[x] - 128;
111
112 if (Y < 0 || Y >= h || X < 0 || X >= w)
113 dst[x] = blank;
114 else
115 dst[x] = src[Y * slinesize + X];
116 }
117 break;
118 case EDGE_SMEAR:
119 for (int x = 0; x < w; x++) {
120 int Y = av_clip(y + ysrc[x] - 128, 0, h - 1);
121 int X = av_clip(x + xsrc[x] - 128, 0, w - 1);
122 dst[x] = src[Y * slinesize + X];
123 }
124 break;
125 case EDGE_WRAP:
126 for (int x = 0; x < w; x++) {
127 int Y = (y + ysrc[x] - 128) % h;
128 int X = (x + xsrc[x] - 128) % w;
129
130 if (Y < 0)
131 Y += h;
132 if (X < 0)
133 X += w;
134 dst[x] = src[Y * slinesize + X];
135 }
136 break;
137 case EDGE_MIRROR:
138 for (int x = 0; x < w; x++) {
139 int Y = y + ysrc[x] - 128;
140 int X = x + xsrc[x] - 128;
141
142 if (Y < 0)
143 Y = (-Y) % h;
144 if (X < 0)
145 X = (-X) % w;
146 if (Y >= h)
147 Y = h - (Y % h) - 1;
148 if (X >= w)
149 X = w - (X % w) - 1;
150 dst[x] = src[Y * slinesize + X];
151 }
152 break;
153 }
154
155 ysrc += ylinesize;
156 xsrc += xlinesize;
157 dst += dlinesize;
158 }
159 }
160 return 0;
161 }
162
163 static int displace_packed(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
164 {
165 DisplaceContext *s = ctx->priv;
166 const ThreadData *td = arg;
167 const AVFrame *in = td->in;
168 const AVFrame *xin = td->xin;
169 const AVFrame *yin = td->yin;
170 const AVFrame *out = td->out;
171 const int step = s->step;
172 const int h = s->height[0];
173 const int w = s->width[0];
174 const int slice_start = (h * jobnr ) / nb_jobs;
175 const int slice_end = (h * (jobnr+1)) / nb_jobs;
176 const int dlinesize = out->linesize[0];
177 const int slinesize = in->linesize[0];
178 const int xlinesize = xin->linesize[0];
179 const int ylinesize = yin->linesize[0];
180 const uint8_t *src = in->data[0];
181 const uint8_t *ysrc = yin->data[0] + slice_start * ylinesize;
182 const uint8_t *xsrc = xin->data[0] + slice_start * xlinesize;
183 uint8_t *dst = out->data[0] + slice_start * dlinesize;
184 const uint8_t *blank = s->blank;
185
186 for (int y = slice_start; y < slice_end; y++) {
187 switch (s->edge) {
188 case EDGE_BLANK:
189 for (int x = 0; x < w; x++) {
190 for (int c = 0; c < s->nb_components; c++) {
191 int Y = y + (ysrc[x * step + c] - 128);
192 int X = x + (xsrc[x * step + c] - 128);
193
194 if (Y < 0 || Y >= h || X < 0 || X >= w)
195 dst[x * step + c] = blank[c];
196 else
197 dst[x * step + c] = src[Y * slinesize + X * step + c];
198 }
199 }
200 break;
201 case EDGE_SMEAR:
202 for (int x = 0; x < w; x++) {
203 for (int c = 0; c < s->nb_components; c++) {
204 int Y = av_clip(y + (ysrc[x * step + c] - 128), 0, h - 1);
205 int X = av_clip(x + (xsrc[x * step + c] - 128), 0, w - 1);
206
207 dst[x * step + c] = src[Y * slinesize + X * step + c];
208 }
209 }
210 break;
211 case EDGE_WRAP:
212 for (int x = 0; x < w; x++) {
213 for (int c = 0; c < s->nb_components; c++) {
214 int Y = (y + (ysrc[x * step + c] - 128)) % h;
215 int X = (x + (xsrc[x * step + c] - 128)) % w;
216
217 if (Y < 0)
218 Y += h;
219 if (X < 0)
220 X += w;
221 dst[x * step + c] = src[Y * slinesize + X * step + c];
222 }
223 }
224 break;
225 case EDGE_MIRROR:
226 for (int x = 0; x < w; x++) {
227 for (int c = 0; c < s->nb_components; c++) {
228 int Y = y + ysrc[x * step + c] - 128;
229 int X = x + xsrc[x * step + c] - 128;
230
231 if (Y < 0)
232 Y = (-Y) % h;
233 if (X < 0)
234 X = (-X) % w;
235 if (Y >= h)
236 Y = h - (Y % h) - 1;
237 if (X >= w)
238 X = w - (X % w) - 1;
239 dst[x * step + c] = src[Y * slinesize + X * step + c];
240 }
241 }
242 break;
243 }
244
245 ysrc += ylinesize;
246 xsrc += xlinesize;
247 dst += dlinesize;
248 }
249 return 0;
250 }
251
252 static int process_frame(FFFrameSync *fs)
253 {
254 AVFilterContext *ctx = fs->parent;
255 DisplaceContext *s = fs->opaque;
256 AVFilterLink *outlink = ctx->outputs[0];
257 AVFrame *out, *in, *xin, *yin;
258 int ret;
259
260 if ((ret = ff_framesync_get_frame(&s->fs, 0, &in, 0)) < 0 ||
261 (ret = ff_framesync_get_frame(&s->fs, 1, &xin, 0)) < 0 ||
262 (ret = ff_framesync_get_frame(&s->fs, 2, &yin, 0)) < 0)
263 return ret;
264
265 if (ctx->is_disabled) {
266 out = av_frame_clone(in);
267 if (!out)
268 return AVERROR(ENOMEM);
269 } else {
270 ThreadData td;
271
272 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
273 if (!out)
274 return AVERROR(ENOMEM);
275 av_frame_copy_props(out, in);
276
277 td.in = in;
278 td.xin = xin;
279 td.yin = yin;
280 td.out = out;
281 ff_filter_execute(ctx, s->displace_slice, &td, NULL,
282 FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
283 }
284 out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
285
286 return ff_filter_frame(outlink, out);
287 }
288
289 static int config_input(AVFilterLink *inlink)
290 {
291 AVFilterContext *ctx = inlink->dst;
292 DisplaceContext *s = ctx->priv;
293 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
294 int vsub, hsub;
295
296 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
297 s->nb_components = desc->nb_components;
298
299 if (s->nb_planes > 1 || s->nb_components == 1)
300 s->displace_slice = displace_planar;
301 else
302 s->displace_slice = displace_packed;
303
304 if (!(desc->flags & AV_PIX_FMT_FLAG_RGB)) {
305 s->blank[1] = s->blank[2] = 128;
306 s->blank[0] = 16;
307 }
308
309 s->step = av_get_padded_bits_per_pixel(desc) >> 3;
310 hsub = desc->log2_chroma_w;
311 vsub = desc->log2_chroma_h;
312 s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
313 s->height[0] = s->height[3] = inlink->h;
314 s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
315 s->width[0] = s->width[3] = inlink->w;
316
317 return 0;
318 }
319
320 static int config_output(AVFilterLink *outlink)
321 {
322 FilterLink *outl = ff_filter_link(outlink);
323 AVFilterContext *ctx = outlink->src;
324 DisplaceContext *s = ctx->priv;
325 AVFilterLink *srclink = ctx->inputs[0];
326 FilterLink *sl = ff_filter_link(srclink);
327 AVFilterLink *xlink = ctx->inputs[1];
328 AVFilterLink *ylink = ctx->inputs[2];
329 FFFrameSyncIn *in;
330 int ret;
331
332 if (srclink->w != xlink->w ||
333 srclink->h != xlink->h ||
334 srclink->w != ylink->w ||
335 srclink->h != ylink->h) {
336 av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
337 "(size %dx%d) do not match the corresponding "
338 "second input link %s parameters (%dx%d) "
339 "and/or third input link %s parameters (%dx%d)\n",
340 ctx->input_pads[0].name, srclink->w, srclink->h,
341 ctx->input_pads[1].name, xlink->w, xlink->h,
342 ctx->input_pads[2].name, ylink->w, ylink->h);
343 return AVERROR(EINVAL);
344 }
345
346 outlink->w = srclink->w;
347 outlink->h = srclink->h;
348 outlink->sample_aspect_ratio = srclink->sample_aspect_ratio;
349 outl->frame_rate = sl->frame_rate;
350
351 ret = ff_framesync_init(&s->fs, ctx, 3);
352 if (ret < 0)
353 return ret;
354
355 in = s->fs.in;
356 in[0].time_base = srclink->time_base;
357 in[1].time_base = xlink->time_base;
358 in[2].time_base = ylink->time_base;
359 in[0].sync = 2;
360 in[0].before = EXT_STOP;
361 in[0].after = EXT_STOP;
362 in[1].sync = 1;
363 in[1].before = EXT_NULL;
364 in[1].after = EXT_INFINITY;
365 in[2].sync = 1;
366 in[2].before = EXT_NULL;
367 in[2].after = EXT_INFINITY;
368 s->fs.opaque = s;
369 s->fs.on_event = process_frame;
370
371 ret = ff_framesync_configure(&s->fs);
372 outlink->time_base = s->fs.time_base;
373
374 return ret;
375 }
376
377 static int activate(AVFilterContext *ctx)
378 {
379 DisplaceContext *s = ctx->priv;
380 return ff_framesync_activate(&s->fs);
381 }
382
383 static av_cold void uninit(AVFilterContext *ctx)
384 {
385 DisplaceContext *s = ctx->priv;
386
387 ff_framesync_uninit(&s->fs);
388 }
389
390 static const AVFilterPad displace_inputs[] = {
391 {
392 .name = "source",
393 .type = AVMEDIA_TYPE_VIDEO,
394 .config_props = config_input,
395 },
396 {
397 .name = "xmap",
398 .type = AVMEDIA_TYPE_VIDEO,
399 },
400 {
401 .name = "ymap",
402 .type = AVMEDIA_TYPE_VIDEO,
403 },
404 };
405
406 static const AVFilterPad displace_outputs[] = {
407 {
408 .name = "default",
409 .type = AVMEDIA_TYPE_VIDEO,
410 .config_props = config_output,
411 },
412 };
413
414 const FFFilter ff_vf_displace = {
415 .p.name = "displace",
416 .p.description = NULL_IF_CONFIG_SMALL("Displace pixels."),
417 .p.priv_class = &displace_class,
418 .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
419 AVFILTER_FLAG_SLICE_THREADS,
420 .priv_size = sizeof(DisplaceContext),
421 .uninit = uninit,
422 .activate = activate,
423 FILTER_INPUTS(displace_inputs),
424 FILTER_OUTPUTS(displace_outputs),
425 FILTER_PIXFMTS_ARRAY(pix_fmts),
426 .process_command = ff_filter_process_command,
427 };
428