]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_displace.c
x86/vf_w3fdif: simplify w3fdif_simple_high
[ffmpeg] / libavfilter / vf_displace.c
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/imgutils.h"
22 #include "libavutil/pixdesc.h"
23 #include "libavutil/opt.h"
24 #include "avfilter.h"
25 #include "formats.h"
26 #include "framesync.h"
27 #include "internal.h"
28 #include "video.h"
29
30 enum EdgeMode {
31     EDGE_BLANK,
32     EDGE_SMEAR,
33     EDGE_WRAP,
34     EDGE_NB
35 };
36
37 typedef struct DisplaceContext {
38     const AVClass *class;
39     int width[4], height[4];
40     enum EdgeMode edge;
41     int nb_planes;
42     int nb_components;
43     int step;
44     uint8_t blank[4];
45     FFFrameSync fs;
46
47     void (*displace)(struct DisplaceContext *s, const AVFrame *in,
48                      const AVFrame *xpic, const AVFrame *ypic, AVFrame *out);
49 } DisplaceContext;
50
51 #define OFFSET(x) offsetof(DisplaceContext, x)
52 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
53
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" },
59     { NULL }
60 };
61
62 AVFILTER_DEFINE_CLASS(displace);
63
64 static int query_formats(AVFilterContext *ctx)
65 {
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
77     };
78
79     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
80     return 0;
81 }
82
83 static void displace_planar(DisplaceContext *s, const AVFrame *in,
84                             const AVFrame *xpic, const AVFrame *ypic,
85                             AVFrame *out)
86 {
87     int plane, x, y;
88
89     for (plane = 0; plane < s->nb_planes; plane++) {
90         const int h = s->height[plane];
91         const int w = s->width[plane];
92         const int dlinesize = out->linesize[plane];
93         const int slinesize = in->linesize[plane];
94         const int xlinesize = xpic->linesize[plane];
95         const int ylinesize = ypic->linesize[plane];
96         const uint8_t *src = in->data[plane];
97         const uint8_t *ysrc = ypic->data[plane];
98         const uint8_t *xsrc = xpic->data[plane];
99         uint8_t *dst = out->data[plane];
100         const uint8_t blank = s->blank[plane];
101
102         for (y = 0; y < h; y++) {
103             switch (s->edge) {
104             case EDGE_BLANK:
105                 for (x = 0; x < w; x++) {
106                     int Y = y + ysrc[x] - 128;
107                     int X = x + xsrc[x] - 128;
108
109                     if (Y < 0 || Y >= h || X < 0 || X >= w)
110                         dst[x] = blank;
111                     else
112                         dst[x] = src[Y * slinesize + X];
113                 }
114                 break;
115             case EDGE_SMEAR:
116                 for (x = 0; x < w; x++) {
117                     int Y = av_clip(y + ysrc[x] - 128, 0, h - 1);
118                     int X = av_clip(x + xsrc[x] - 128, 0, w - 1);
119                     dst[x] = src[Y * slinesize + X];
120                 }
121                 break;
122             case EDGE_WRAP:
123                 for (x = 0; x < w; x++) {
124                     int Y = (y + ysrc[x] - 128) % h;
125                     int X = (x + xsrc[x] - 128) % w;
126
127                     if (Y < 0)
128                         Y += h;
129                     if (X < 0)
130                         X += w;
131                     dst[x] = src[Y * slinesize + X];
132                 }
133                 break;
134             }
135
136             ysrc += ylinesize;
137             xsrc += xlinesize;
138             dst  += dlinesize;
139         }
140     }
141 }
142
143 static void displace_packed(DisplaceContext *s, const AVFrame *in,
144                             const AVFrame *xpic, const AVFrame *ypic,
145                             AVFrame *out)
146 {
147     const int step = s->step;
148     const int h = s->height[0];
149     const int w = s->width[0];
150     const int dlinesize = out->linesize[0];
151     const int slinesize = in->linesize[0];
152     const int xlinesize = xpic->linesize[0];
153     const int ylinesize = ypic->linesize[0];
154     const uint8_t *src = in->data[0];
155     const uint8_t *ysrc = ypic->data[0];
156     const uint8_t *xsrc = xpic->data[0];
157     const uint8_t *blank = s->blank;
158     uint8_t *dst = out->data[0];
159     int c, x, y;
160
161     for (y = 0; y < h; y++) {
162         switch (s->edge) {
163         case EDGE_BLANK:
164             for (x = 0; x < w; x++) {
165                 for (c = 0; c < s->nb_components; c++) {
166                     int Y = y + (ysrc[x * step + c] - 128);
167                     int X = x + (xsrc[x * step + c] - 128);
168
169                     if (Y < 0 || Y >= h || X < 0 || X >= w)
170                         dst[x * step + c] = blank[c];
171                     else
172                         dst[x * step + c] = src[Y * slinesize + X * step + c];
173                 }
174             }
175             break;
176         case EDGE_SMEAR:
177             for (x = 0; x < w; x++) {
178                 for (c = 0; c < s->nb_components; c++) {
179                     int Y = av_clip(y + (ysrc[x * step + c] - 128), 0, h - 1);
180                     int X = av_clip(x + (xsrc[x * step + c] - 128), 0, w - 1);
181
182                     dst[x * step + c] = src[Y * slinesize + X * step + c];
183                 }
184             }
185             break;
186         case EDGE_WRAP:
187             for (x = 0; x < w; x++) {
188                 for (c = 0; c < s->nb_components; c++) {
189                     int Y = (y + (ysrc[x * step + c] - 128)) % h;
190                     int X = (x + (xsrc[x * step + c] - 128)) % w;
191
192                     if (Y < 0)
193                         Y += h;
194                     if (X < 0)
195                         X += w;
196                     dst[x * step + c] = src[Y * slinesize + X * step + c];
197                 }
198             }
199             break;
200         }
201
202         ysrc += ylinesize;
203         xsrc += xlinesize;
204         dst  += dlinesize;
205     }
206 }
207
208 static int process_frame(FFFrameSync *fs)
209 {
210     AVFilterContext *ctx = fs->parent;
211     DisplaceContext *s = fs->opaque;
212     AVFilterLink *outlink = ctx->outputs[0];
213     AVFrame *out, *in, *xpic, *ypic;
214     int ret;
215
216     if ((ret = ff_framesync_get_frame(&s->fs, 0, &in,   0)) < 0 ||
217         (ret = ff_framesync_get_frame(&s->fs, 1, &xpic, 0)) < 0 ||
218         (ret = ff_framesync_get_frame(&s->fs, 2, &ypic, 0)) < 0)
219         return ret;
220
221     if (ctx->is_disabled) {
222         out = av_frame_clone(in);
223         if (!out)
224             return AVERROR(ENOMEM);
225     } else {
226         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
227         if (!out)
228             return AVERROR(ENOMEM);
229         av_frame_copy_props(out, in);
230
231         s->displace(s, in, xpic, ypic, out);
232     }
233     out->pts = av_rescale_q(in->pts, s->fs.time_base, outlink->time_base);
234
235     return ff_filter_frame(outlink, out);
236 }
237
238 static int config_input(AVFilterLink *inlink)
239 {
240     AVFilterContext *ctx = inlink->dst;
241     DisplaceContext *s = ctx->priv;
242     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
243     int vsub, hsub;
244
245     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
246     s->nb_components = desc->nb_components;
247
248     if (s->nb_planes > 1 || s->nb_components == 1)
249         s->displace = displace_planar;
250     else
251         s->displace = displace_packed;
252
253     if (!(desc->flags & AV_PIX_FMT_FLAG_RGB)) {
254         s->blank[1] = s->blank[2] = 128;
255         s->blank[0] = 16;
256     }
257
258     s->step = av_get_padded_bits_per_pixel(desc) >> 3;
259     hsub = desc->log2_chroma_w;
260     vsub = desc->log2_chroma_h;
261     s->height[1] = s->height[2] = FF_CEIL_RSHIFT(inlink->h, vsub);
262     s->height[0] = s->height[3] = inlink->h;
263     s->width[1]  = s->width[2]  = FF_CEIL_RSHIFT(inlink->w, hsub);
264     s->width[0]  = s->width[3]  = inlink->w;
265
266     return 0;
267 }
268
269 static int config_output(AVFilterLink *outlink)
270 {
271     AVFilterContext *ctx = outlink->src;
272     DisplaceContext *s = ctx->priv;
273     AVFilterLink *srclink = ctx->inputs[0];
274     AVFilterLink *xlink = ctx->inputs[1];
275     AVFilterLink *ylink = ctx->inputs[2];
276     FFFrameSyncIn *in;
277     int ret;
278
279     if (srclink->format != xlink->format ||
280         srclink->format != ylink->format) {
281         av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
282         return AVERROR(EINVAL);
283     }
284     if (srclink->w                       != xlink->w ||
285         srclink->h                       != xlink->h ||
286         srclink->sample_aspect_ratio.num != xlink->sample_aspect_ratio.num ||
287         srclink->sample_aspect_ratio.den != xlink->sample_aspect_ratio.den ||
288         srclink->w                       != ylink->w ||
289         srclink->h                       != ylink->h ||
290         srclink->sample_aspect_ratio.num != ylink->sample_aspect_ratio.num ||
291         srclink->sample_aspect_ratio.den != ylink->sample_aspect_ratio.den) {
292         av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
293                "(size %dx%d, SAR %d:%d) do not match the corresponding "
294                "second input link %s parameters (%dx%d, SAR %d:%d) "
295                "and/or third input link %s parameters (%dx%d, SAR %d:%d)\n",
296                ctx->input_pads[0].name, srclink->w, srclink->h,
297                srclink->sample_aspect_ratio.num,
298                srclink->sample_aspect_ratio.den,
299                ctx->input_pads[1].name, xlink->w, xlink->h,
300                xlink->sample_aspect_ratio.num,
301                xlink->sample_aspect_ratio.den,
302                ctx->input_pads[2].name, ylink->w, ylink->h,
303                ylink->sample_aspect_ratio.num,
304                ylink->sample_aspect_ratio.den);
305         return AVERROR(EINVAL);
306     }
307
308     outlink->w = srclink->w;
309     outlink->h = srclink->h;
310     outlink->time_base = srclink->time_base;
311     outlink->sample_aspect_ratio = srclink->sample_aspect_ratio;
312     outlink->frame_rate = srclink->frame_rate;
313
314     ret = ff_framesync_init(&s->fs, ctx, 3);
315     if (ret < 0)
316         return ret;
317
318     in = s->fs.in;
319     in[0].time_base = srclink->time_base;
320     in[1].time_base = xlink->time_base;
321     in[2].time_base = ylink->time_base;
322     in[0].sync   = 2;
323     in[0].before = EXT_STOP;
324     in[0].after  = EXT_STOP;
325     in[1].sync   = 1;
326     in[1].before = EXT_NULL;
327     in[1].after  = EXT_INFINITY;
328     in[2].sync   = 1;
329     in[2].before = EXT_NULL;
330     in[2].after  = EXT_INFINITY;
331     s->fs.opaque   = s;
332     s->fs.on_event = process_frame;
333
334     return ff_framesync_configure(&s->fs);
335 }
336
337 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
338 {
339     DisplaceContext *s = inlink->dst->priv;
340     return ff_framesync_filter_frame(&s->fs, inlink, buf);
341 }
342
343 static int request_frame(AVFilterLink *outlink)
344 {
345     DisplaceContext *s = outlink->src->priv;
346     return ff_framesync_request_frame(&s->fs, outlink);
347 }
348
349 static av_cold void uninit(AVFilterContext *ctx)
350 {
351     DisplaceContext *s = ctx->priv;
352
353     ff_framesync_uninit(&s->fs);
354 }
355
356 static const AVFilterPad displace_inputs[] = {
357     {
358         .name         = "source",
359         .type         = AVMEDIA_TYPE_VIDEO,
360         .filter_frame = filter_frame,
361         .config_props = config_input,
362     },
363     {
364         .name         = "xmap",
365         .type         = AVMEDIA_TYPE_VIDEO,
366         .filter_frame = filter_frame,
367     },
368     {
369         .name         = "ymap",
370         .type         = AVMEDIA_TYPE_VIDEO,
371         .filter_frame = filter_frame,
372     },
373     { NULL }
374 };
375
376 static const AVFilterPad displace_outputs[] = {
377     {
378         .name          = "default",
379         .type          = AVMEDIA_TYPE_VIDEO,
380         .config_props  = config_output,
381         .request_frame = request_frame,
382     },
383     { NULL }
384 };
385
386 AVFilter ff_vf_displace = {
387     .name          = "displace",
388     .description   = NULL_IF_CONFIG_SMALL("Displace pixels."),
389     .priv_size     = sizeof(DisplaceContext),
390     .uninit        = uninit,
391     .query_formats = query_formats,
392     .inputs        = displace_inputs,
393     .outputs       = displace_outputs,
394     .priv_class    = &displace_class,
395     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
396 };