FFmpeg coverage


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