blob: 9daa0c9ddba89c6b1c4dfc19fc72bf4835d6b33d
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 | return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); |
80 | } |
81 | |
82 | static void displace_planar(DisplaceContext *s, const AVFrame *in, |
83 | const AVFrame *xpic, const AVFrame *ypic, |
84 | AVFrame *out) |
85 | { |
86 | int plane, x, y; |
87 | |
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]; |
100 | |
101 | for (y = 0; y < h; y++) { |
102 | switch (s->edge) { |
103 | case EDGE_BLANK: |
104 | for (x = 0; x < w; x++) { |
105 | int Y = y + ysrc[x] - 128; |
106 | int X = x + xsrc[x] - 128; |
107 | |
108 | if (Y < 0 || Y >= h || X < 0 || X >= w) |
109 | dst[x] = blank; |
110 | else |
111 | dst[x] = src[Y * slinesize + X]; |
112 | } |
113 | break; |
114 | case EDGE_SMEAR: |
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]; |
119 | } |
120 | break; |
121 | case EDGE_WRAP: |
122 | for (x = 0; x < w; x++) { |
123 | int Y = (y + ysrc[x] - 128) % h; |
124 | int X = (x + xsrc[x] - 128) % w; |
125 | |
126 | if (Y < 0) |
127 | Y += h; |
128 | if (X < 0) |
129 | X += w; |
130 | dst[x] = src[Y * slinesize + X]; |
131 | } |
132 | break; |
133 | } |
134 | |
135 | ysrc += ylinesize; |
136 | xsrc += xlinesize; |
137 | dst += dlinesize; |
138 | } |
139 | } |
140 | } |
141 | |
142 | static void displace_packed(DisplaceContext *s, const AVFrame *in, |
143 | const AVFrame *xpic, const AVFrame *ypic, |
144 | AVFrame *out) |
145 | { |
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]; |
158 | int c, x, y; |
159 | |
160 | for (y = 0; y < h; y++) { |
161 | switch (s->edge) { |
162 | case EDGE_BLANK: |
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); |
167 | |
168 | if (Y < 0 || Y >= h || X < 0 || X >= w) |
169 | dst[x * step + c] = blank[c]; |
170 | else |
171 | dst[x * step + c] = src[Y * slinesize + X * step + c]; |
172 | } |
173 | } |
174 | break; |
175 | case EDGE_SMEAR: |
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); |
180 | |
181 | dst[x * step + c] = src[Y * slinesize + X * step + c]; |
182 | } |
183 | } |
184 | break; |
185 | case EDGE_WRAP: |
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; |
190 | |
191 | if (Y < 0) |
192 | Y += h; |
193 | if (X < 0) |
194 | X += w; |
195 | dst[x * step + c] = src[Y * slinesize + X * step + c]; |
196 | } |
197 | } |
198 | break; |
199 | } |
200 | |
201 | ysrc += ylinesize; |
202 | xsrc += xlinesize; |
203 | dst += dlinesize; |
204 | } |
205 | } |
206 | |
207 | static int process_frame(FFFrameSync *fs) |
208 | { |
209 | AVFilterContext *ctx = fs->parent; |
210 | DisplaceContext *s = fs->opaque; |
211 | AVFilterLink *outlink = ctx->outputs[0]; |
212 | AVFrame *out, *in, *xpic, *ypic; |
213 | int ret; |
214 | |
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) |
218 | return ret; |
219 | |
220 | if (ctx->is_disabled) { |
221 | out = av_frame_clone(in); |
222 | if (!out) |
223 | return AVERROR(ENOMEM); |
224 | } else { |
225 | out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
226 | if (!out) |
227 | return AVERROR(ENOMEM); |
228 | av_frame_copy_props(out, in); |
229 | |
230 | s->displace(s, in, xpic, ypic, out); |
231 | } |
232 | out->pts = av_rescale_q(in->pts, s->fs.time_base, outlink->time_base); |
233 | |
234 | return ff_filter_frame(outlink, out); |
235 | } |
236 | |
237 | static int config_input(AVFilterLink *inlink) |
238 | { |
239 | AVFilterContext *ctx = inlink->dst; |
240 | DisplaceContext *s = ctx->priv; |
241 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); |
242 | int vsub, hsub; |
243 | |
244 | s->nb_planes = av_pix_fmt_count_planes(inlink->format); |
245 | s->nb_components = desc->nb_components; |
246 | |
247 | if (s->nb_planes > 1 || s->nb_components == 1) |
248 | s->displace = displace_planar; |
249 | else |
250 | s->displace = displace_packed; |
251 | |
252 | if (!(desc->flags & AV_PIX_FMT_FLAG_RGB)) { |
253 | s->blank[1] = s->blank[2] = 128; |
254 | s->blank[0] = 16; |
255 | } |
256 | |
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] = AV_CEIL_RSHIFT(inlink->h, vsub); |
261 | s->height[0] = s->height[3] = inlink->h; |
262 | s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, hsub); |
263 | s->width[0] = s->width[3] = inlink->w; |
264 | |
265 | return 0; |
266 | } |
267 | |
268 | static int config_output(AVFilterLink *outlink) |
269 | { |
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]; |
275 | FFFrameSyncIn *in; |
276 | int ret; |
277 | |
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); |
282 | } |
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); |
305 | } |
306 | |
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; |
312 | |
313 | ret = ff_framesync_init(&s->fs, ctx, 3); |
314 | if (ret < 0) |
315 | return ret; |
316 | |
317 | in = s->fs.in; |
318 | in[0].time_base = srclink->time_base; |
319 | in[1].time_base = xlink->time_base; |
320 | in[2].time_base = ylink->time_base; |
321 | in[0].sync = 2; |
322 | in[0].before = EXT_STOP; |
323 | in[0].after = EXT_STOP; |
324 | in[1].sync = 1; |
325 | in[1].before = EXT_NULL; |
326 | in[1].after = EXT_INFINITY; |
327 | in[2].sync = 1; |
328 | in[2].before = EXT_NULL; |
329 | in[2].after = EXT_INFINITY; |
330 | s->fs.opaque = s; |
331 | s->fs.on_event = process_frame; |
332 | |
333 | return ff_framesync_configure(&s->fs); |
334 | } |
335 | |
336 | static int filter_frame(AVFilterLink *inlink, AVFrame *buf) |
337 | { |
338 | DisplaceContext *s = inlink->dst->priv; |
339 | return ff_framesync_filter_frame(&s->fs, inlink, buf); |
340 | } |
341 | |
342 | static int request_frame(AVFilterLink *outlink) |
343 | { |
344 | DisplaceContext *s = outlink->src->priv; |
345 | return ff_framesync_request_frame(&s->fs, outlink); |
346 | } |
347 | |
348 | static av_cold void uninit(AVFilterContext *ctx) |
349 | { |
350 | DisplaceContext *s = ctx->priv; |
351 | |
352 | ff_framesync_uninit(&s->fs); |
353 | } |
354 | |
355 | static const AVFilterPad displace_inputs[] = { |
356 | { |
357 | .name = "source", |
358 | .type = AVMEDIA_TYPE_VIDEO, |
359 | .filter_frame = filter_frame, |
360 | .config_props = config_input, |
361 | }, |
362 | { |
363 | .name = "xmap", |
364 | .type = AVMEDIA_TYPE_VIDEO, |
365 | .filter_frame = filter_frame, |
366 | }, |
367 | { |
368 | .name = "ymap", |
369 | .type = AVMEDIA_TYPE_VIDEO, |
370 | .filter_frame = filter_frame, |
371 | }, |
372 | { NULL } |
373 | }; |
374 | |
375 | static const AVFilterPad displace_outputs[] = { |
376 | { |
377 | .name = "default", |
378 | .type = AVMEDIA_TYPE_VIDEO, |
379 | .config_props = config_output, |
380 | .request_frame = request_frame, |
381 | }, |
382 | { NULL } |
383 | }; |
384 | |
385 | AVFilter ff_vf_displace = { |
386 | .name = "displace", |
387 | .description = NULL_IF_CONFIG_SMALL("Displace pixels."), |
388 | .priv_size = sizeof(DisplaceContext), |
389 | .uninit = uninit, |
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, |
395 | }; |
396 |