]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_perspective.c
Merge commit '82f8eb5f773d87e3d9c0066ef19b501a461ddfae'
[ffmpeg] / libavfilter / vf_perspective.c
1 /*
2  * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
3  * Copyright (c) 2013 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/eval.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/pixdesc.h"
25 #include "libavutil/opt.h"
26 #include "avfilter.h"
27 #include "formats.h"
28 #include "internal.h"
29 #include "video.h"
30
31 #define SUB_PIXEL_BITS  8
32 #define SUB_PIXELS      (1 << SUB_PIXEL_BITS)
33 #define COEFF_BITS      11
34
35 #define LINEAR 0
36 #define CUBIC  1
37
38 typedef struct PerspectiveContext {
39     const AVClass *class;
40     char *expr_str[4][2];
41     double ref[4][2];
42     int32_t (*pv)[2];
43     int32_t coeff[SUB_PIXELS][4];
44     int interpolation;
45     int linesize[4];
46     int height[4];
47     int hsub, vsub;
48     int nb_planes;
49
50     int (*perspective)(AVFilterContext *ctx,
51                        void *arg, int job, int nb_jobs);
52 } PerspectiveContext;
53
54 #define OFFSET(x) offsetof(PerspectiveContext, x)
55 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
56
57 static const AVOption perspective_options[] = {
58     { "x0", "set top left x coordinate",     OFFSET(expr_str[0][0]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
59     { "y0", "set top left y coordinate",     OFFSET(expr_str[0][1]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
60     { "x1", "set top right x coordinate",    OFFSET(expr_str[1][0]), AV_OPT_TYPE_STRING, {.str="W"}, 0, 0, FLAGS },
61     { "y1", "set top right y coordinate",    OFFSET(expr_str[1][1]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
62     { "x2", "set bottom left x coordinate",  OFFSET(expr_str[2][0]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
63     { "y2", "set bottom left y coordinate",  OFFSET(expr_str[2][1]), AV_OPT_TYPE_STRING, {.str="H"}, 0, 0, FLAGS },
64     { "x3", "set bottom right x coordinate", OFFSET(expr_str[3][0]), AV_OPT_TYPE_STRING, {.str="W"}, 0, 0, FLAGS },
65     { "y3", "set bottom right y coordinate", OFFSET(expr_str[3][1]), AV_OPT_TYPE_STRING, {.str="H"}, 0, 0, FLAGS },
66     { "interpolation", "set interpolation", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=LINEAR}, 0, 1, FLAGS, "interpolation" },
67     {      "linear", "", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "interpolation" },
68     {       "cubic", "", 0, AV_OPT_TYPE_CONST, {.i64=CUBIC},  0, 0, FLAGS, "interpolation" },
69     { NULL }
70 };
71
72 AVFILTER_DEFINE_CLASS(perspective);
73
74 static int query_formats(AVFilterContext *ctx)
75 {
76     static const enum AVPixelFormat pix_fmts[] = {
77         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
78         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
79         AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
80         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
81     };
82
83     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
84     return 0;
85 }
86
87 static inline double get_coeff(double d)
88 {
89     double coeff, A = -0.60;
90
91     d = fabs(d);
92
93     if (d < 1.0)
94         coeff = (1.0 - (A + 3.0) * d * d + (A + 2.0) * d * d * d);
95     else if (d < 2.0)
96         coeff = (-4.0 * A + 8.0 * A * d - 5.0 * A * d * d + A * d * d * d);
97     else
98         coeff = 0.0;
99
100     return coeff;
101 }
102
103 static const char *const var_names[] = {   "W",   "H",        NULL };
104 enum                                   { VAR_W, VAR_H, VAR_VARS_NB };
105
106 static int config_input(AVFilterLink *inlink)
107 {
108     double x0, x1, x2, x3, x4, x5, x6, x7, q;
109     AVFilterContext *ctx = inlink->dst;
110     PerspectiveContext *s = ctx->priv;
111     double (*ref)[2] = s->ref;
112     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
113     double values[VAR_VARS_NB] = { [VAR_W] = inlink->w, [VAR_H] = inlink->h };
114     int h = inlink->h;
115     int w = inlink->w;
116     int x, y, i, j, ret;
117
118     for (i = 0; i < 4; i++) {
119         for (j = 0; j < 2; j++) {
120             if (!s->expr_str[i][j])
121                 return AVERROR(EINVAL);
122             ret = av_expr_parse_and_eval(&s->ref[i][j], s->expr_str[i][j],
123                                          var_names, &values[0],
124                                          NULL, NULL, NULL, NULL,
125                                          0, 0, ctx);
126             if (ret < 0)
127                 return ret;
128         }
129     }
130
131     s->hsub = desc->log2_chroma_w;
132     s->vsub = desc->log2_chroma_h;
133     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
134     if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
135         return ret;
136
137     s->height[1] = s->height[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
138     s->height[0] = s->height[3] = inlink->h;
139
140     s->pv = av_realloc_f(s->pv, w * h, 2 * sizeof(*s->pv));
141     if (!s->pv)
142         return AVERROR(ENOMEM);
143
144     x6 = ((ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0]) *
145           (ref[2][1] - ref[3][1]) -
146          ( ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1]) *
147           (ref[2][0] - ref[3][0])) * h;
148     x7 = ((ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1]) *
149           (ref[1][0] - ref[3][0]) -
150          ( ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0]) *
151           (ref[1][1] - ref[3][1])) * w;
152     q =  ( ref[1][0] - ref[3][0]) * (ref[2][1] - ref[3][1]) -
153          ( ref[2][0] - ref[3][0]) * (ref[1][1] - ref[3][1]);
154
155     x0 = q * (ref[1][0] - ref[0][0]) * h + x6 * ref[1][0];
156     x1 = q * (ref[2][0] - ref[0][0]) * w + x7 * ref[2][0];
157     x2 = q *  ref[0][0] * w * h;
158     x3 = q * (ref[1][1] - ref[0][1]) * h + x6 * ref[1][1];
159     x4 = q * (ref[2][1] - ref[0][1]) * w + x7 * ref[2][1];
160     x5 = q *  ref[0][1] * w * h;
161
162     for (y = 0; y < h; y++){
163         for (x = 0; x < w; x++){
164             int u, v;
165
166             u = (int)floor(SUB_PIXELS * (x0 * x + x1 * y + x2) /
167                                         (x6 * x + x7 * y + q * w * h) + 0.5);
168             v = (int)floor(SUB_PIXELS * (x3 * x + x4 * y + x5) /
169                                         (x6 * x + x7 * y + q * w * h) + 0.5);
170
171             s->pv[x + y * w][0] = u;
172             s->pv[x + y * w][1] = v;
173         }
174     }
175
176     for (i = 0; i < SUB_PIXELS; i++){
177         double d = i / (double)SUB_PIXELS;
178         double temp[4];
179         double sum = 0;
180
181         for (j = 0; j < 4; j++)
182             temp[j] = get_coeff(j - d - 1);
183
184         for (j = 0; j < 4; j++)
185             sum += temp[j];
186
187         for (j = 0; j < 4; j++)
188             s->coeff[i][j] = (int)floor((1 << COEFF_BITS) * temp[j] / sum + 0.5);
189     }
190
191     return 0;
192 }
193
194 typedef struct ThreadData {
195     uint8_t *dst;
196     int dst_linesize;
197     uint8_t *src;
198     int src_linesize;
199     int w, h;
200     int hsub, vsub;
201 } ThreadData;
202
203 static int resample_cubic(AVFilterContext *ctx, void *arg,
204                           int job, int nb_jobs)
205 {
206     PerspectiveContext *s = ctx->priv;
207     ThreadData *td = arg;
208     uint8_t *dst = td->dst;
209     int dst_linesize = td->dst_linesize;
210     uint8_t *src = td->src;
211     int src_linesize = td->src_linesize;
212     int w = td->w;
213     int h = td->h;
214     int hsub = td->hsub;
215     int vsub = td->vsub;
216     int start = (h * job) / nb_jobs;
217     int end   = (h * (job+1)) / nb_jobs;
218     const int linesize = s->linesize[0];
219     int x, y;
220
221     for (y = start; y < end; y++) {
222         int sy = y << vsub;
223         for (x = 0; x < w; x++) {
224             int u, v, subU, subV, sum, sx;
225
226             sx   = x << hsub;
227             u    = s->pv[sx + sy * linesize][0] >> hsub;
228             v    = s->pv[sx + sy * linesize][1] >> vsub;
229             subU = u & (SUB_PIXELS - 1);
230             subV = v & (SUB_PIXELS - 1);
231             u  >>= SUB_PIXEL_BITS;
232             v  >>= SUB_PIXEL_BITS;
233
234             if (u > 0 && v > 0 && u < w - 2 && v < h - 2){
235                 const int index = u + v*src_linesize;
236                 const int a = s->coeff[subU][0];
237                 const int b = s->coeff[subU][1];
238                 const int c = s->coeff[subU][2];
239                 const int d = s->coeff[subU][3];
240
241                 sum = s->coeff[subV][0] * (a * src[index - 1 -     src_linesize] + b * src[index - 0 -     src_linesize]  +
242                                       c *      src[index + 1 -     src_linesize] + d * src[index + 2 -     src_linesize]) +
243                       s->coeff[subV][1] * (a * src[index - 1                   ] + b * src[index - 0                   ]  +
244                                       c *      src[index + 1                   ] + d * src[index + 2                   ]) +
245                       s->coeff[subV][2] * (a * src[index - 1 +     src_linesize] + b * src[index - 0 +     src_linesize]  +
246                                       c *      src[index + 1 +     src_linesize] + d * src[index + 2 +     src_linesize]) +
247                       s->coeff[subV][3] * (a * src[index - 1 + 2 * src_linesize] + b * src[index - 0 + 2 * src_linesize]  +
248                                       c *      src[index + 1 + 2 * src_linesize] + d * src[index + 2 + 2 * src_linesize]);
249             } else {
250                 int dx, dy;
251
252                 sum = 0;
253
254                 for (dy = 0; dy < 4; dy++) {
255                     int iy = v + dy - 1;
256
257                     if (iy < 0)
258                         iy = 0;
259                     else if (iy >= h)
260                         iy = h-1;
261                     for (dx = 0; dx < 4; dx++) {
262                         int ix = u + dx - 1;
263
264                         if (ix < 0)
265                             ix = 0;
266                         else if (ix >= w)
267                             ix = w - 1;
268
269                         sum += s->coeff[subU][dx] * s->coeff[subV][dy] * src[ ix + iy * src_linesize];
270                     }
271                 }
272             }
273
274             sum = (sum + (1<<(COEFF_BITS * 2 - 1))) >> (COEFF_BITS * 2);
275             sum = av_clip(sum, 0, 255);
276             dst[x + y * dst_linesize] = sum;
277         }
278     }
279     return 0;
280 }
281
282 static int resample_linear(AVFilterContext *ctx, void *arg,
283                            int job, int nb_jobs)
284 {
285     PerspectiveContext *s = ctx->priv;
286     ThreadData *td = arg;
287     uint8_t *dst = td->dst;
288     int dst_linesize = td->dst_linesize;
289     uint8_t *src = td->src;
290     int src_linesize = td->src_linesize;
291     int w = td->w;
292     int h = td->h;
293     int hsub = td->hsub;
294     int vsub = td->vsub;
295     int start = (h * job) / nb_jobs;
296     int end   = (h * (job+1)) / nb_jobs;
297     const int linesize = s->linesize[0];
298     int x, y;
299
300     for (y = start; y < end; y++){
301         int sy = y << vsub;
302         for (x = 0; x < w; x++){
303             int u, v, subU, subV, sum, sx, index, subUI, subVI;
304
305             sx   = x << hsub;
306             u    = s->pv[sx + sy * linesize][0] >> hsub;
307             v    = s->pv[sx + sy * linesize][1] >> vsub;
308             subU = u & (SUB_PIXELS - 1);
309             subV = v & (SUB_PIXELS - 1);
310             u  >>= SUB_PIXEL_BITS;
311             v  >>= SUB_PIXEL_BITS;
312
313             index = u + v * src_linesize;
314             subUI = SUB_PIXELS - subU;
315             subVI = SUB_PIXELS - subV;
316
317             if ((unsigned)u < (unsigned)(w - 1)){
318                 if((unsigned)v < (unsigned)(h - 1)){
319                     sum = subVI * (subUI * src[index] + subU * src[index + 1]) +
320                           subV  * (subUI * src[index + src_linesize] + subU * src[index + src_linesize + 1]);
321                     sum = (sum + (1 << (SUB_PIXEL_BITS * 2 - 1)))>> (SUB_PIXEL_BITS * 2);
322                 } else {
323                     if (v < 0)
324                         v = 0;
325                     else
326                         v = h - 1;
327                     index = u + v * src_linesize;
328                     sum   = subUI * src[index] + subU * src[index + 1];
329                     sum   = (sum + (1 << (SUB_PIXEL_BITS - 1))) >> SUB_PIXEL_BITS;
330                 }
331             } else {
332                 if (u < 0)
333                     u = 0;
334                 else
335                     u = w - 1;
336                 if ((unsigned)v < (unsigned)(h - 1)){
337                     index = u + v * src_linesize;
338                     sum   = subVI * src[index] + subV * src[index + src_linesize];
339                     sum   = (sum + (1 << (SUB_PIXEL_BITS - 1))) >> SUB_PIXEL_BITS;
340                 } else {
341                     if (v < 0)
342                         v = 0;
343                     else
344                         v = h - 1;
345                     index = u + v * src_linesize;
346                     sum   = src[index];
347                 }
348             }
349
350             sum = av_clip(sum, 0, 255);
351             dst[x + y * dst_linesize] = sum;
352         }
353     }
354     return 0;
355 }
356
357 static av_cold int init(AVFilterContext *ctx)
358 {
359     PerspectiveContext *s = ctx->priv;
360
361     switch (s->interpolation) {
362     case LINEAR: s->perspective = resample_linear; break;
363     case CUBIC:  s->perspective = resample_cubic;  break;
364     }
365
366     return 0;
367 }
368
369 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
370 {
371     AVFilterContext *ctx = inlink->dst;
372     AVFilterLink *outlink = ctx->outputs[0];
373     PerspectiveContext *s = ctx->priv;
374     AVFrame *out;
375     int plane;
376
377     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
378     if (!out) {
379         av_frame_free(&frame);
380         return AVERROR(ENOMEM);
381     }
382     av_frame_copy_props(out, frame);
383
384     for (plane = 0; plane < s->nb_planes; plane++) {
385         int hsub = plane == 1 || plane == 2 ? s->hsub : 0;
386         int vsub = plane == 1 || plane == 2 ? s->vsub : 0;
387         ThreadData td = {.dst = out->data[plane],
388                          .dst_linesize = out->linesize[plane],
389                          .src = frame->data[plane],
390                          .src_linesize = frame->linesize[plane],
391                          .w = s->linesize[plane],
392                          .h = s->height[plane],
393                          .hsub = hsub,
394                          .vsub = vsub };
395         ctx->internal->execute(ctx, s->perspective, &td, NULL, FFMIN(td.h, ctx->graph->nb_threads));
396     }
397
398     av_frame_free(&frame);
399     return ff_filter_frame(outlink, out);
400 }
401
402 static av_cold void uninit(AVFilterContext *ctx)
403 {
404     PerspectiveContext *s = ctx->priv;
405
406     av_freep(&s->pv);
407 }
408
409 static const AVFilterPad perspective_inputs[] = {
410     {
411         .name         = "default",
412         .type         = AVMEDIA_TYPE_VIDEO,
413         .filter_frame = filter_frame,
414         .config_props = config_input,
415     },
416     { NULL }
417 };
418
419 static const AVFilterPad perspective_outputs[] = {
420     {
421         .name = "default",
422         .type = AVMEDIA_TYPE_VIDEO,
423     },
424     { NULL }
425 };
426
427 AVFilter ff_vf_perspective = {
428     .name          = "perspective",
429     .description   = NULL_IF_CONFIG_SMALL("Correct the perspective of video."),
430     .priv_size     = sizeof(PerspectiveContext),
431     .init          = init,
432     .uninit        = uninit,
433     .query_formats = query_formats,
434     .inputs        = perspective_inputs,
435     .outputs       = perspective_outputs,
436     .priv_class    = &perspective_class,
437     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
438 };