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"
37 typedef struct DisplaceContext {
39 int width[4], height[4];
47 void (*displace)(struct DisplaceContext *s, const AVFrame *in,
48 const AVFrame *xpic, const AVFrame *ypic, AVFrame *out);
51 #define OFFSET(x) offsetof(DisplaceContext, x)
52 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
54 static const AVOption displace_options[] = {
55 { "edge", "set edge mode", OFFSET(edge), AV_OPT_TYPE_INT, {.i64=EDGE_SMEAR}, 0, EDGE_NB-1, FLAGS, "edge" },
56 { "blank", "", 0, AV_OPT_TYPE_CONST, {.i64=EDGE_BLANK}, 0, 0, FLAGS, "edge" },
57 { "smear", "", 0, AV_OPT_TYPE_CONST, {.i64=EDGE_SMEAR}, 0, 0, FLAGS, "edge" },
58 { "wrap" , "", 0, AV_OPT_TYPE_CONST, {.i64=EDGE_WRAP}, 0, 0, FLAGS, "edge" },
62 AVFILTER_DEFINE_CLASS(displace);
64 static int query_formats(AVFilterContext *ctx)
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 return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
82 static void displace_planar(DisplaceContext *s, const AVFrame *in,
83 const AVFrame *xpic, const AVFrame *ypic,
88 for (plane = 0; plane < s->nb_planes; plane++) {
89 const int h = s->height[plane];
90 const int w = s->width[plane];
91 const int dlinesize = out->linesize[plane];
92 const int slinesize = in->linesize[plane];
93 const int xlinesize = xpic->linesize[plane];
94 const int ylinesize = ypic->linesize[plane];
95 const uint8_t *src = in->data[plane];
96 const uint8_t *ysrc = ypic->data[plane];
97 const uint8_t *xsrc = xpic->data[plane];
98 uint8_t *dst = out->data[plane];
99 const uint8_t blank = s->blank[plane];
101 for (y = 0; y < h; y++) {
104 for (x = 0; x < w; x++) {
105 int Y = y + ysrc[x] - 128;
106 int X = x + xsrc[x] - 128;
108 if (Y < 0 || Y >= h || X < 0 || X >= w)
111 dst[x] = src[Y * slinesize + X];
115 for (x = 0; x < w; x++) {
116 int Y = av_clip(y + ysrc[x] - 128, 0, h - 1);
117 int X = av_clip(x + xsrc[x] - 128, 0, w - 1);
118 dst[x] = src[Y * slinesize + X];
122 for (x = 0; x < w; x++) {
123 int Y = (y + ysrc[x] - 128) % h;
124 int X = (x + xsrc[x] - 128) % w;
130 dst[x] = src[Y * slinesize + X];
142 static void displace_packed(DisplaceContext *s, const AVFrame *in,
143 const AVFrame *xpic, const AVFrame *ypic,
146 const int step = s->step;
147 const int h = s->height[0];
148 const int w = s->width[0];
149 const int dlinesize = out->linesize[0];
150 const int slinesize = in->linesize[0];
151 const int xlinesize = xpic->linesize[0];
152 const int ylinesize = ypic->linesize[0];
153 const uint8_t *src = in->data[0];
154 const uint8_t *ysrc = ypic->data[0];
155 const uint8_t *xsrc = xpic->data[0];
156 const uint8_t *blank = s->blank;
157 uint8_t *dst = out->data[0];
160 for (y = 0; y < h; y++) {
163 for (x = 0; x < w; x++) {
164 for (c = 0; c < s->nb_components; c++) {
165 int Y = y + (ysrc[x * step + c] - 128);
166 int X = x + (xsrc[x * step + c] - 128);
168 if (Y < 0 || Y >= h || X < 0 || X >= w)
169 dst[x * step + c] = blank[c];
171 dst[x * step + c] = src[Y * slinesize + X * step + c];
176 for (x = 0; x < w; x++) {
177 for (c = 0; c < s->nb_components; c++) {
178 int Y = av_clip(y + (ysrc[x * step + c] - 128), 0, h - 1);
179 int X = av_clip(x + (xsrc[x * step + c] - 128), 0, w - 1);
181 dst[x * step + c] = src[Y * slinesize + X * step + c];
186 for (x = 0; x < w; x++) {
187 for (c = 0; c < s->nb_components; c++) {
188 int Y = (y + (ysrc[x * step + c] - 128)) % h;
189 int X = (x + (xsrc[x * step + c] - 128)) % w;
195 dst[x * step + c] = src[Y * slinesize + X * step + c];
207 static int process_frame(FFFrameSync *fs)
209 AVFilterContext *ctx = fs->parent;
210 DisplaceContext *s = fs->opaque;
211 AVFilterLink *outlink = ctx->outputs[0];
212 AVFrame *out, *in, *xpic, *ypic;
215 if ((ret = ff_framesync_get_frame(&s->fs, 0, &in, 0)) < 0 ||
216 (ret = ff_framesync_get_frame(&s->fs, 1, &xpic, 0)) < 0 ||
217 (ret = ff_framesync_get_frame(&s->fs, 2, &ypic, 0)) < 0)
220 if (ctx->is_disabled) {
221 out = av_frame_clone(in);
223 return AVERROR(ENOMEM);
225 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
227 return AVERROR(ENOMEM);
228 av_frame_copy_props(out, in);
230 s->displace(s, in, xpic, ypic, out);
232 out->pts = av_rescale_q(in->pts, s->fs.time_base, outlink->time_base);
234 return ff_filter_frame(outlink, out);
237 static int config_input(AVFilterLink *inlink)
239 AVFilterContext *ctx = inlink->dst;
240 DisplaceContext *s = ctx->priv;
241 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
244 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
245 s->nb_components = desc->nb_components;
247 if (s->nb_planes > 1 || s->nb_components == 1)
248 s->displace = displace_planar;
250 s->displace = displace_packed;
252 if (!(desc->flags & AV_PIX_FMT_FLAG_RGB)) {
253 s->blank[1] = s->blank[2] = 128;
257 s->step = av_get_padded_bits_per_pixel(desc) >> 3;
258 hsub = desc->log2_chroma_w;
259 vsub = desc->log2_chroma_h;
260 s->height[1] = s->height[2] = FF_CEIL_RSHIFT(inlink->h, vsub);
261 s->height[0] = s->height[3] = inlink->h;
262 s->width[1] = s->width[2] = FF_CEIL_RSHIFT(inlink->w, hsub);
263 s->width[0] = s->width[3] = inlink->w;
268 static int config_output(AVFilterLink *outlink)
270 AVFilterContext *ctx = outlink->src;
271 DisplaceContext *s = ctx->priv;
272 AVFilterLink *srclink = ctx->inputs[0];
273 AVFilterLink *xlink = ctx->inputs[1];
274 AVFilterLink *ylink = ctx->inputs[2];
278 if (srclink->format != xlink->format ||
279 srclink->format != ylink->format) {
280 av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
281 return AVERROR(EINVAL);
283 if (srclink->w != xlink->w ||
284 srclink->h != xlink->h ||
285 srclink->sample_aspect_ratio.num != xlink->sample_aspect_ratio.num ||
286 srclink->sample_aspect_ratio.den != xlink->sample_aspect_ratio.den ||
287 srclink->w != ylink->w ||
288 srclink->h != ylink->h ||
289 srclink->sample_aspect_ratio.num != ylink->sample_aspect_ratio.num ||
290 srclink->sample_aspect_ratio.den != ylink->sample_aspect_ratio.den) {
291 av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
292 "(size %dx%d, SAR %d:%d) do not match the corresponding "
293 "second input link %s parameters (%dx%d, SAR %d:%d) "
294 "and/or third input link %s parameters (%dx%d, SAR %d:%d)\n",
295 ctx->input_pads[0].name, srclink->w, srclink->h,
296 srclink->sample_aspect_ratio.num,
297 srclink->sample_aspect_ratio.den,
298 ctx->input_pads[1].name, xlink->w, xlink->h,
299 xlink->sample_aspect_ratio.num,
300 xlink->sample_aspect_ratio.den,
301 ctx->input_pads[2].name, ylink->w, ylink->h,
302 ylink->sample_aspect_ratio.num,
303 ylink->sample_aspect_ratio.den);
304 return AVERROR(EINVAL);
307 outlink->w = srclink->w;
308 outlink->h = srclink->h;
309 outlink->time_base = srclink->time_base;
310 outlink->sample_aspect_ratio = srclink->sample_aspect_ratio;
311 outlink->frame_rate = srclink->frame_rate;
313 ret = ff_framesync_init(&s->fs, ctx, 3);
318 in[0].time_base = srclink->time_base;
319 in[1].time_base = xlink->time_base;
320 in[2].time_base = ylink->time_base;
322 in[0].before = EXT_STOP;
323 in[0].after = EXT_STOP;
325 in[1].before = EXT_NULL;
326 in[1].after = EXT_INFINITY;
328 in[2].before = EXT_NULL;
329 in[2].after = EXT_INFINITY;
331 s->fs.on_event = process_frame;
333 return ff_framesync_configure(&s->fs);
336 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
338 DisplaceContext *s = inlink->dst->priv;
339 return ff_framesync_filter_frame(&s->fs, inlink, buf);
342 static int request_frame(AVFilterLink *outlink)
344 DisplaceContext *s = outlink->src->priv;
345 return ff_framesync_request_frame(&s->fs, outlink);
348 static av_cold void uninit(AVFilterContext *ctx)
350 DisplaceContext *s = ctx->priv;
352 ff_framesync_uninit(&s->fs);
355 static const AVFilterPad displace_inputs[] = {
358 .type = AVMEDIA_TYPE_VIDEO,
359 .filter_frame = filter_frame,
360 .config_props = config_input,
364 .type = AVMEDIA_TYPE_VIDEO,
365 .filter_frame = filter_frame,
369 .type = AVMEDIA_TYPE_VIDEO,
370 .filter_frame = filter_frame,
375 static const AVFilterPad displace_outputs[] = {
378 .type = AVMEDIA_TYPE_VIDEO,
379 .config_props = config_output,
380 .request_frame = request_frame,
385 AVFilter ff_vf_displace = {
387 .description = NULL_IF_CONFIG_SMALL("Displace pixels."),
388 .priv_size = sizeof(DisplaceContext),
390 .query_formats = query_formats,
391 .inputs = displace_inputs,
392 .outputs = displace_outputs,
393 .priv_class = &displace_class,
394 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,