2 * Copyright (c) 2013 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/imgutils.h"
22 #include "libavutil/pixdesc.h"
23 #include "libavutil/opt.h"
26 #include "framesync.h"
38 typedef struct DisplaceContext {
40 int width[4], height[4];
48 void (*displace)(struct DisplaceContext *s, const AVFrame *in,
49 const AVFrame *xpic, const AVFrame *ypic, AVFrame *out);
52 #define OFFSET(x) offsetof(DisplaceContext, x)
53 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
55 static const AVOption displace_options[] = {
56 { "edge", "set edge mode", OFFSET(edge), AV_OPT_TYPE_INT, {.i64=EDGE_SMEAR}, 0, EDGE_NB-1, FLAGS, "edge" },
57 { "blank", "", 0, AV_OPT_TYPE_CONST, {.i64=EDGE_BLANK}, 0, 0, FLAGS, "edge" },
58 { "smear", "", 0, AV_OPT_TYPE_CONST, {.i64=EDGE_SMEAR}, 0, 0, FLAGS, "edge" },
59 { "wrap" , "", 0, AV_OPT_TYPE_CONST, {.i64=EDGE_WRAP}, 0, 0, FLAGS, "edge" },
60 { "mirror" , "", 0, AV_OPT_TYPE_CONST, {.i64=EDGE_MIRROR}, 0, 0, FLAGS, "edge" },
64 AVFILTER_DEFINE_CLASS(displace);
66 static const enum AVPixelFormat pix_fmts[] = {
67 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
68 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
69 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
70 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
71 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
72 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
73 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
74 AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR, AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
75 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
76 AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
79 static void displace_planar(DisplaceContext *s, const AVFrame *in,
80 const AVFrame *xpic, const AVFrame *ypic,
85 for (plane = 0; plane < s->nb_planes; plane++) {
86 const int h = s->height[plane];
87 const int w = s->width[plane];
88 const int dlinesize = out->linesize[plane];
89 const int slinesize = in->linesize[plane];
90 const int xlinesize = xpic->linesize[plane];
91 const int ylinesize = ypic->linesize[plane];
92 const uint8_t *src = in->data[plane];
93 const uint8_t *ysrc = ypic->data[plane];
94 const uint8_t *xsrc = xpic->data[plane];
95 uint8_t *dst = out->data[plane];
96 const uint8_t blank = s->blank[plane];
98 for (y = 0; y < h; y++) {
101 for (x = 0; x < w; x++) {
102 int Y = y + ysrc[x] - 128;
103 int X = x + xsrc[x] - 128;
105 if (Y < 0 || Y >= h || X < 0 || X >= w)
108 dst[x] = src[Y * slinesize + X];
112 for (x = 0; x < w; x++) {
113 int Y = av_clip(y + ysrc[x] - 128, 0, h - 1);
114 int X = av_clip(x + xsrc[x] - 128, 0, w - 1);
115 dst[x] = src[Y * slinesize + X];
119 for (x = 0; x < w; x++) {
120 int Y = (y + ysrc[x] - 128) % h;
121 int X = (x + xsrc[x] - 128) % w;
127 dst[x] = src[Y * slinesize + X];
131 for (x = 0; x < w; x++) {
132 int Y = y + ysrc[x] - 128;
133 int X = x + xsrc[x] - 128;
143 dst[x] = src[Y * slinesize + X];
155 static void displace_packed(DisplaceContext *s, const AVFrame *in,
156 const AVFrame *xpic, const AVFrame *ypic,
159 const int step = s->step;
160 const int h = s->height[0];
161 const int w = s->width[0];
162 const int dlinesize = out->linesize[0];
163 const int slinesize = in->linesize[0];
164 const int xlinesize = xpic->linesize[0];
165 const int ylinesize = ypic->linesize[0];
166 const uint8_t *src = in->data[0];
167 const uint8_t *ysrc = ypic->data[0];
168 const uint8_t *xsrc = xpic->data[0];
169 const uint8_t *blank = s->blank;
170 uint8_t *dst = out->data[0];
173 for (y = 0; y < h; y++) {
176 for (x = 0; x < w; x++) {
177 for (c = 0; c < s->nb_components; c++) {
178 int Y = y + (ysrc[x * step + c] - 128);
179 int X = x + (xsrc[x * step + c] - 128);
181 if (Y < 0 || Y >= h || X < 0 || X >= w)
182 dst[x * step + c] = blank[c];
184 dst[x * step + c] = src[Y * slinesize + X * step + c];
189 for (x = 0; x < w; x++) {
190 for (c = 0; c < s->nb_components; c++) {
191 int Y = av_clip(y + (ysrc[x * step + c] - 128), 0, h - 1);
192 int X = av_clip(x + (xsrc[x * step + c] - 128), 0, w - 1);
194 dst[x * step + c] = src[Y * slinesize + X * step + c];
199 for (x = 0; x < w; x++) {
200 for (c = 0; c < s->nb_components; c++) {
201 int Y = (y + (ysrc[x * step + c] - 128)) % h;
202 int X = (x + (xsrc[x * step + c] - 128)) % w;
208 dst[x * step + c] = src[Y * slinesize + X * step + c];
213 for (x = 0; x < w; x++) {
214 for (c = 0; c < s->nb_components; c++) {
215 int Y = y + ysrc[x * step + c] - 128;
216 int X = x + xsrc[x * step + c] - 128;
226 dst[x * step + c] = src[Y * slinesize + X * step + c];
238 static int process_frame(FFFrameSync *fs)
240 AVFilterContext *ctx = fs->parent;
241 DisplaceContext *s = fs->opaque;
242 AVFilterLink *outlink = ctx->outputs[0];
243 AVFrame *out, *in, *xpic, *ypic;
246 if ((ret = ff_framesync_get_frame(&s->fs, 0, &in, 0)) < 0 ||
247 (ret = ff_framesync_get_frame(&s->fs, 1, &xpic, 0)) < 0 ||
248 (ret = ff_framesync_get_frame(&s->fs, 2, &ypic, 0)) < 0)
251 if (ctx->is_disabled) {
252 out = av_frame_clone(in);
254 return AVERROR(ENOMEM);
256 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
258 return AVERROR(ENOMEM);
259 av_frame_copy_props(out, in);
261 s->displace(s, in, xpic, ypic, out);
263 out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
265 return ff_filter_frame(outlink, out);
268 static int config_input(AVFilterLink *inlink)
270 AVFilterContext *ctx = inlink->dst;
271 DisplaceContext *s = ctx->priv;
272 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
275 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
276 s->nb_components = desc->nb_components;
278 if (s->nb_planes > 1 || s->nb_components == 1)
279 s->displace = displace_planar;
281 s->displace = displace_packed;
283 if (!(desc->flags & AV_PIX_FMT_FLAG_RGB)) {
284 s->blank[1] = s->blank[2] = 128;
288 s->step = av_get_padded_bits_per_pixel(desc) >> 3;
289 hsub = desc->log2_chroma_w;
290 vsub = desc->log2_chroma_h;
291 s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
292 s->height[0] = s->height[3] = inlink->h;
293 s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
294 s->width[0] = s->width[3] = inlink->w;
299 static int config_output(AVFilterLink *outlink)
301 AVFilterContext *ctx = outlink->src;
302 DisplaceContext *s = ctx->priv;
303 AVFilterLink *srclink = ctx->inputs[0];
304 AVFilterLink *xlink = ctx->inputs[1];
305 AVFilterLink *ylink = ctx->inputs[2];
309 if (srclink->w != xlink->w ||
310 srclink->h != xlink->h ||
311 srclink->w != ylink->w ||
312 srclink->h != ylink->h) {
313 av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
314 "(size %dx%d) do not match the corresponding "
315 "second input link %s parameters (%dx%d) "
316 "and/or third input link %s parameters (%dx%d)\n",
317 ctx->input_pads[0].name, srclink->w, srclink->h,
318 ctx->input_pads[1].name, xlink->w, xlink->h,
319 ctx->input_pads[2].name, ylink->w, ylink->h);
320 return AVERROR(EINVAL);
323 outlink->w = srclink->w;
324 outlink->h = srclink->h;
325 outlink->sample_aspect_ratio = srclink->sample_aspect_ratio;
326 outlink->frame_rate = srclink->frame_rate;
328 ret = ff_framesync_init(&s->fs, ctx, 3);
333 in[0].time_base = srclink->time_base;
334 in[1].time_base = xlink->time_base;
335 in[2].time_base = ylink->time_base;
337 in[0].before = EXT_STOP;
338 in[0].after = EXT_STOP;
340 in[1].before = EXT_NULL;
341 in[1].after = EXT_INFINITY;
343 in[2].before = EXT_NULL;
344 in[2].after = EXT_INFINITY;
346 s->fs.on_event = process_frame;
348 ret = ff_framesync_configure(&s->fs);
349 outlink->time_base = s->fs.time_base;
354 static int activate(AVFilterContext *ctx)
356 DisplaceContext *s = ctx->priv;
357 return ff_framesync_activate(&s->fs);
360 static av_cold void uninit(AVFilterContext *ctx)
362 DisplaceContext *s = ctx->priv;
364 ff_framesync_uninit(&s->fs);
367 static const AVFilterPad displace_inputs[] = {
370 .type = AVMEDIA_TYPE_VIDEO,
371 .config_props = config_input,
375 .type = AVMEDIA_TYPE_VIDEO,
379 .type = AVMEDIA_TYPE_VIDEO,
383 static const AVFilterPad displace_outputs[] = {
386 .type = AVMEDIA_TYPE_VIDEO,
387 .config_props = config_output,
391 const AVFilter ff_vf_displace = {
393 .description = NULL_IF_CONFIG_SMALL("Displace pixels."),
394 .priv_size = sizeof(DisplaceContext),
396 .activate = activate,
397 FILTER_INPUTS(displace_inputs),
398 FILTER_OUTPUTS(displace_outputs),
399 FILTER_PIXFMTS_ARRAY(pix_fmts),
400 .priv_class = &displace_class,
401 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,