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 int query_formats(AVFilterContext *ctx)
68 static const enum AVPixelFormat pix_fmts[] = {
69 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
70 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
71 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
72 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
73 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
74 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
75 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
76 AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR, AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
77 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
78 AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
81 return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
84 static void displace_planar(DisplaceContext *s, const AVFrame *in,
85 const AVFrame *xpic, const AVFrame *ypic,
90 for (plane = 0; plane < s->nb_planes; plane++) {
91 const int h = s->height[plane];
92 const int w = s->width[plane];
93 const int dlinesize = out->linesize[plane];
94 const int slinesize = in->linesize[plane];
95 const int xlinesize = xpic->linesize[plane];
96 const int ylinesize = ypic->linesize[plane];
97 const uint8_t *src = in->data[plane];
98 const uint8_t *ysrc = ypic->data[plane];
99 const uint8_t *xsrc = xpic->data[plane];
100 uint8_t *dst = out->data[plane];
101 const uint8_t blank = s->blank[plane];
103 for (y = 0; y < h; y++) {
106 for (x = 0; x < w; x++) {
107 int Y = y + ysrc[x] - 128;
108 int X = x + xsrc[x] - 128;
110 if (Y < 0 || Y >= h || X < 0 || X >= w)
113 dst[x] = src[Y * slinesize + X];
117 for (x = 0; x < w; x++) {
118 int Y = av_clip(y + ysrc[x] - 128, 0, h - 1);
119 int X = av_clip(x + xsrc[x] - 128, 0, w - 1);
120 dst[x] = src[Y * slinesize + X];
124 for (x = 0; x < w; x++) {
125 int Y = (y + ysrc[x] - 128) % h;
126 int X = (x + xsrc[x] - 128) % w;
132 dst[x] = src[Y * slinesize + X];
136 for (x = 0; x < w; x++) {
137 int Y = y + ysrc[x] - 128;
138 int X = x + xsrc[x] - 128;
148 dst[x] = src[Y * slinesize + X];
160 static void displace_packed(DisplaceContext *s, const AVFrame *in,
161 const AVFrame *xpic, const AVFrame *ypic,
164 const int step = s->step;
165 const int h = s->height[0];
166 const int w = s->width[0];
167 const int dlinesize = out->linesize[0];
168 const int slinesize = in->linesize[0];
169 const int xlinesize = xpic->linesize[0];
170 const int ylinesize = ypic->linesize[0];
171 const uint8_t *src = in->data[0];
172 const uint8_t *ysrc = ypic->data[0];
173 const uint8_t *xsrc = xpic->data[0];
174 const uint8_t *blank = s->blank;
175 uint8_t *dst = out->data[0];
178 for (y = 0; y < h; y++) {
181 for (x = 0; x < w; x++) {
182 for (c = 0; c < s->nb_components; c++) {
183 int Y = y + (ysrc[x * step + c] - 128);
184 int X = x + (xsrc[x * step + c] - 128);
186 if (Y < 0 || Y >= h || X < 0 || X >= w)
187 dst[x * step + c] = blank[c];
189 dst[x * step + c] = src[Y * slinesize + X * step + c];
194 for (x = 0; x < w; x++) {
195 for (c = 0; c < s->nb_components; c++) {
196 int Y = av_clip(y + (ysrc[x * step + c] - 128), 0, h - 1);
197 int X = av_clip(x + (xsrc[x * step + c] - 128), 0, w - 1);
199 dst[x * step + c] = src[Y * slinesize + X * step + c];
204 for (x = 0; x < w; x++) {
205 for (c = 0; c < s->nb_components; c++) {
206 int Y = (y + (ysrc[x * step + c] - 128)) % h;
207 int X = (x + (xsrc[x * step + c] - 128)) % w;
213 dst[x * step + c] = src[Y * slinesize + X * step + c];
218 for (x = 0; x < w; x++) {
219 for (c = 0; c < s->nb_components; c++) {
220 int Y = y + ysrc[x * step + c] - 128;
221 int X = x + xsrc[x * step + c] - 128;
231 dst[x * step + c] = src[Y * slinesize + X * step + c];
243 static int process_frame(FFFrameSync *fs)
245 AVFilterContext *ctx = fs->parent;
246 DisplaceContext *s = fs->opaque;
247 AVFilterLink *outlink = ctx->outputs[0];
248 AVFrame *out, *in, *xpic, *ypic;
251 if ((ret = ff_framesync_get_frame(&s->fs, 0, &in, 0)) < 0 ||
252 (ret = ff_framesync_get_frame(&s->fs, 1, &xpic, 0)) < 0 ||
253 (ret = ff_framesync_get_frame(&s->fs, 2, &ypic, 0)) < 0)
256 if (ctx->is_disabled) {
257 out = av_frame_clone(in);
259 return AVERROR(ENOMEM);
261 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
263 return AVERROR(ENOMEM);
264 av_frame_copy_props(out, in);
266 s->displace(s, in, xpic, ypic, out);
268 out->pts = av_rescale_q(in->pts, s->fs.time_base, outlink->time_base);
270 return ff_filter_frame(outlink, out);
273 static int config_input(AVFilterLink *inlink)
275 AVFilterContext *ctx = inlink->dst;
276 DisplaceContext *s = ctx->priv;
277 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
280 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
281 s->nb_components = desc->nb_components;
283 if (s->nb_planes > 1 || s->nb_components == 1)
284 s->displace = displace_planar;
286 s->displace = displace_packed;
288 if (!(desc->flags & AV_PIX_FMT_FLAG_RGB)) {
289 s->blank[1] = s->blank[2] = 128;
293 s->step = av_get_padded_bits_per_pixel(desc) >> 3;
294 hsub = desc->log2_chroma_w;
295 vsub = desc->log2_chroma_h;
296 s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
297 s->height[0] = s->height[3] = inlink->h;
298 s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
299 s->width[0] = s->width[3] = inlink->w;
304 static int config_output(AVFilterLink *outlink)
306 AVFilterContext *ctx = outlink->src;
307 DisplaceContext *s = ctx->priv;
308 AVFilterLink *srclink = ctx->inputs[0];
309 AVFilterLink *xlink = ctx->inputs[1];
310 AVFilterLink *ylink = ctx->inputs[2];
314 if (srclink->format != xlink->format ||
315 srclink->format != ylink->format) {
316 av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
317 return AVERROR(EINVAL);
319 if (srclink->w != xlink->w ||
320 srclink->h != xlink->h ||
321 srclink->w != ylink->w ||
322 srclink->h != ylink->h) {
323 av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
324 "(size %dx%d) do not match the corresponding "
325 "second input link %s parameters (%dx%d) "
326 "and/or third input link %s parameters (%dx%d)\n",
327 ctx->input_pads[0].name, srclink->w, srclink->h,
328 ctx->input_pads[1].name, xlink->w, xlink->h,
329 ctx->input_pads[2].name, ylink->w, ylink->h);
330 return AVERROR(EINVAL);
333 outlink->w = srclink->w;
334 outlink->h = srclink->h;
335 outlink->time_base = srclink->time_base;
336 outlink->sample_aspect_ratio = srclink->sample_aspect_ratio;
337 outlink->frame_rate = srclink->frame_rate;
339 ret = ff_framesync_init(&s->fs, ctx, 3);
344 in[0].time_base = srclink->time_base;
345 in[1].time_base = xlink->time_base;
346 in[2].time_base = ylink->time_base;
348 in[0].before = EXT_STOP;
349 in[0].after = EXT_STOP;
351 in[1].before = EXT_NULL;
352 in[1].after = EXT_INFINITY;
354 in[2].before = EXT_NULL;
355 in[2].after = EXT_INFINITY;
357 s->fs.on_event = process_frame;
359 return ff_framesync_configure(&s->fs);
362 static int activate(AVFilterContext *ctx)
364 DisplaceContext *s = ctx->priv;
365 return ff_framesync_activate(&s->fs);
368 static av_cold void uninit(AVFilterContext *ctx)
370 DisplaceContext *s = ctx->priv;
372 ff_framesync_uninit(&s->fs);
375 static const AVFilterPad displace_inputs[] = {
378 .type = AVMEDIA_TYPE_VIDEO,
379 .config_props = config_input,
383 .type = AVMEDIA_TYPE_VIDEO,
387 .type = AVMEDIA_TYPE_VIDEO,
392 static const AVFilterPad displace_outputs[] = {
395 .type = AVMEDIA_TYPE_VIDEO,
396 .config_props = config_output,
401 AVFilter ff_vf_displace = {
403 .description = NULL_IF_CONFIG_SMALL("Displace pixels."),
404 .priv_size = sizeof(DisplaceContext),
406 .query_formats = query_formats,
407 .activate = activate,
408 .inputs = displace_inputs,
409 .outputs = displace_outputs,
410 .priv_class = &displace_class,
411 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,