2 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2013 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/eval.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/pixdesc.h"
25 #include "libavutil/opt.h"
31 #define SUB_PIXEL_BITS 8
32 #define SUB_PIXELS (1 << SUB_PIXEL_BITS)
38 typedef struct PerspectiveContext
{
43 int32_t coeff
[SUB_PIXELS
][4];
51 int (*perspective
)(AVFilterContext
*ctx
,
52 void *arg
, int job
, int nb_jobs
);
55 #define OFFSET(x) offsetof(PerspectiveContext, x)
56 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
58 enum PERSPECTIVESense
{
59 PERSPECTIVE_SENSE_SOURCE
= 0, ///< coordinates give locations in source of corners of destination.
60 PERSPECTIVE_SENSE_DESTINATION
= 1, ///< coordinates give locations in destination of corners of source.
63 static const AVOption perspective_options
[] = {
64 { "x0", "set top left x coordinate", OFFSET(expr_str
[0][0]), AV_OPT_TYPE_STRING
, {.str
="0"}, 0, 0, FLAGS
},
65 { "y0", "set top left y coordinate", OFFSET(expr_str
[0][1]), AV_OPT_TYPE_STRING
, {.str
="0"}, 0, 0, FLAGS
},
66 { "x1", "set top right x coordinate", OFFSET(expr_str
[1][0]), AV_OPT_TYPE_STRING
, {.str
="W"}, 0, 0, FLAGS
},
67 { "y1", "set top right y coordinate", OFFSET(expr_str
[1][1]), AV_OPT_TYPE_STRING
, {.str
="0"}, 0, 0, FLAGS
},
68 { "x2", "set bottom left x coordinate", OFFSET(expr_str
[2][0]), AV_OPT_TYPE_STRING
, {.str
="0"}, 0, 0, FLAGS
},
69 { "y2", "set bottom left y coordinate", OFFSET(expr_str
[2][1]), AV_OPT_TYPE_STRING
, {.str
="H"}, 0, 0, FLAGS
},
70 { "x3", "set bottom right x coordinate", OFFSET(expr_str
[3][0]), AV_OPT_TYPE_STRING
, {.str
="W"}, 0, 0, FLAGS
},
71 { "y3", "set bottom right y coordinate", OFFSET(expr_str
[3][1]), AV_OPT_TYPE_STRING
, {.str
="H"}, 0, 0, FLAGS
},
72 { "interpolation", "set interpolation", OFFSET(interpolation
), AV_OPT_TYPE_INT
, {.i64
=LINEAR
}, 0, 1, FLAGS
, "interpolation" },
73 { "linear", "", 0, AV_OPT_TYPE_CONST
, {.i64
=LINEAR
}, 0, 0, FLAGS
, "interpolation" },
74 { "cubic", "", 0, AV_OPT_TYPE_CONST
, {.i64
=CUBIC
}, 0, 0, FLAGS
, "interpolation" },
75 { "sense", "specify the sense of the coordinates", OFFSET(sense
), AV_OPT_TYPE_INT
, {.i64
=PERSPECTIVE_SENSE_SOURCE
}, 0, 1, FLAGS
, "sense"},
76 { "source", "specify locations in source to send to corners in destination",
77 0, AV_OPT_TYPE_CONST
, {.i64
=PERSPECTIVE_SENSE_SOURCE
}, 0, 0, FLAGS
, "sense"},
78 { "destination", "specify locations in destination to send corners of source",
79 0, AV_OPT_TYPE_CONST
, {.i64
=PERSPECTIVE_SENSE_DESTINATION
}, 0, 0, FLAGS
, "sense"},
84 AVFILTER_DEFINE_CLASS(perspective
);
86 static int query_formats(AVFilterContext
*ctx
)
88 static const enum AVPixelFormat pix_fmts
[] = {
89 AV_PIX_FMT_YUVA444P
, AV_PIX_FMT_YUVA422P
, AV_PIX_FMT_YUVA420P
,
90 AV_PIX_FMT_YUVJ444P
, AV_PIX_FMT_YUVJ440P
, AV_PIX_FMT_YUVJ422P
,AV_PIX_FMT_YUVJ420P
, AV_PIX_FMT_YUVJ411P
,
91 AV_PIX_FMT_YUV444P
, AV_PIX_FMT_YUV440P
, AV_PIX_FMT_YUV422P
, AV_PIX_FMT_YUV420P
, AV_PIX_FMT_YUV411P
, AV_PIX_FMT_YUV410P
,
92 AV_PIX_FMT_GBRP
, AV_PIX_FMT_GBRAP
, AV_PIX_FMT_GRAY8
, AV_PIX_FMT_NONE
95 ff_set_common_formats(ctx
, ff_make_format_list(pix_fmts
));
99 static inline double get_coeff(double d
)
101 double coeff
, A
= -0.60;
106 coeff
= (1.0 - (A
+ 3.0) * d
* d
+ (A
+ 2.0) * d
* d
* d
);
108 coeff
= (-4.0 * A
+ 8.0 * A
* d
- 5.0 * A
* d
* d
+ A
* d
* d
* d
);
115 static const char *const var_names
[] = { "W", "H", NULL
};
116 enum { VAR_W
, VAR_H
, VAR_VARS_NB
};
118 static int config_input(AVFilterLink
*inlink
)
120 double x0
, x1
, x2
, x3
, x4
, x5
, x6
, x7
, x8
, q
;
121 double t0
, t1
, t2
, t3
;
122 AVFilterContext
*ctx
= inlink
->dst
;
123 PerspectiveContext
*s
= ctx
->priv
;
124 double (*ref
)[2] = s
->ref
;
125 const AVPixFmtDescriptor
*desc
= av_pix_fmt_desc_get(inlink
->format
);
126 double values
[VAR_VARS_NB
] = { [VAR_W
] = inlink
->w
, [VAR_H
] = inlink
->h
};
131 for (i
= 0; i
< 4; i
++) {
132 for (j
= 0; j
< 2; j
++) {
133 if (!s
->expr_str
[i
][j
])
134 return AVERROR(EINVAL
);
135 ret
= av_expr_parse_and_eval(&s
->ref
[i
][j
], s
->expr_str
[i
][j
],
136 var_names
, &values
[0],
137 NULL
, NULL
, NULL
, NULL
,
144 s
->hsub
= desc
->log2_chroma_w
;
145 s
->vsub
= desc
->log2_chroma_h
;
146 s
->nb_planes
= av_pix_fmt_count_planes(inlink
->format
);
147 if ((ret
= av_image_fill_linesizes(s
->linesize
, inlink
->format
, inlink
->w
)) < 0)
150 s
->height
[1] = s
->height
[2] = FF_CEIL_RSHIFT(inlink
->h
, desc
->log2_chroma_h
);
151 s
->height
[0] = s
->height
[3] = inlink
->h
;
153 s
->pv
= av_realloc_f(s
->pv
, w
* h
, 2 * sizeof(*s
->pv
));
155 return AVERROR(ENOMEM
);
158 case PERSPECTIVE_SENSE_SOURCE
:
159 x6
= ((ref
[0][0] - ref
[1][0] - ref
[2][0] + ref
[3][0]) *
160 (ref
[2][1] - ref
[3][1]) -
161 ( ref
[0][1] - ref
[1][1] - ref
[2][1] + ref
[3][1]) *
162 (ref
[2][0] - ref
[3][0])) * h
;
163 x7
= ((ref
[0][1] - ref
[1][1] - ref
[2][1] + ref
[3][1]) *
164 (ref
[1][0] - ref
[3][0]) -
165 ( ref
[0][0] - ref
[1][0] - ref
[2][0] + ref
[3][0]) *
166 (ref
[1][1] - ref
[3][1])) * w
;
167 q
= ( ref
[1][0] - ref
[3][0]) * (ref
[2][1] - ref
[3][1]) -
168 ( ref
[2][0] - ref
[3][0]) * (ref
[1][1] - ref
[3][1]);
170 x0
= q
* (ref
[1][0] - ref
[0][0]) * h
+ x6
* ref
[1][0];
171 x1
= q
* (ref
[2][0] - ref
[0][0]) * w
+ x7
* ref
[2][0];
172 x2
= q
* ref
[0][0] * w
* h
;
173 x3
= q
* (ref
[1][1] - ref
[0][1]) * h
+ x6
* ref
[1][1];
174 x4
= q
* (ref
[2][1] - ref
[0][1]) * w
+ x7
* ref
[2][1];
175 x5
= q
* ref
[0][1] * w
* h
;
178 case PERSPECTIVE_SENSE_DESTINATION
:
179 t0
= ref
[0][0] * (ref
[3][1] - ref
[1][1]) +
180 ref
[1][0] * (ref
[0][1] - ref
[3][1]) +
181 ref
[3][0] * (ref
[1][1] - ref
[0][1]);
182 t1
= ref
[1][0] * (ref
[2][1] - ref
[3][1]) +
183 ref
[2][0] * (ref
[3][1] - ref
[1][1]) +
184 ref
[3][0] * (ref
[1][1] - ref
[2][1]);
185 t2
= ref
[0][0] * (ref
[3][1] - ref
[2][1]) +
186 ref
[2][0] * (ref
[0][1] - ref
[3][1]) +
187 ref
[3][0] * (ref
[2][1] - ref
[0][1]);
188 t3
= ref
[0][0] * (ref
[1][1] - ref
[2][1]) +
189 ref
[1][0] * (ref
[2][1] - ref
[0][1]) +
190 ref
[2][0] * (ref
[0][1] - ref
[1][1]);
192 x0
= t0
* t1
* w
* (ref
[2][1] - ref
[0][1]);
193 x1
= t0
* t1
* w
* (ref
[0][0] - ref
[2][0]);
194 x2
= t0
* t1
* w
* (ref
[0][1] * ref
[2][0] - ref
[0][0] * ref
[2][1]);
195 x3
= t1
* t2
* h
* (ref
[1][1] - ref
[0][1]);
196 x4
= t1
* t2
* h
* (ref
[0][0] - ref
[1][0]);
197 x5
= t1
* t2
* h
* (ref
[0][1] * ref
[1][0] - ref
[0][0] * ref
[1][1]);
198 x6
= t1
* t2
* (ref
[1][1] - ref
[0][1]) +
199 t0
* t3
* (ref
[2][1] - ref
[3][1]);
200 x7
= t1
* t2
* (ref
[0][0] - ref
[1][0]) +
201 t0
* t3
* (ref
[3][0] - ref
[2][0]);
202 x8
= t1
* t2
* (ref
[0][1] * ref
[1][0] - ref
[0][0] * ref
[1][1]) +
203 t0
* t3
* (ref
[2][0] * ref
[3][1] - ref
[2][1] * ref
[3][0]);
207 for (y
= 0; y
< h
; y
++){
208 for (x
= 0; x
< w
; x
++){
211 u
= (int)floor(SUB_PIXELS
* (x0
* x
+ x1
* y
+ x2
) /
212 (x6
* x
+ x7
* y
+ x8
) + 0.5);
213 v
= (int)floor(SUB_PIXELS
* (x3
* x
+ x4
* y
+ x5
) /
214 (x6
* x
+ x7
* y
+ x8
) + 0.5);
216 s
->pv
[x
+ y
* w
][0] = u
;
217 s
->pv
[x
+ y
* w
][1] = v
;
221 for (i
= 0; i
< SUB_PIXELS
; i
++){
222 double d
= i
/ (double)SUB_PIXELS
;
226 for (j
= 0; j
< 4; j
++)
227 temp
[j
] = get_coeff(j
- d
- 1);
229 for (j
= 0; j
< 4; j
++)
232 for (j
= 0; j
< 4; j
++)
233 s
->coeff
[i
][j
] = (int)floor((1 << COEFF_BITS
) * temp
[j
] / sum
+ 0.5);
239 typedef struct ThreadData
{
248 static int resample_cubic(AVFilterContext
*ctx
, void *arg
,
249 int job
, int nb_jobs
)
251 PerspectiveContext
*s
= ctx
->priv
;
252 ThreadData
*td
= arg
;
253 uint8_t *dst
= td
->dst
;
254 int dst_linesize
= td
->dst_linesize
;
255 uint8_t *src
= td
->src
;
256 int src_linesize
= td
->src_linesize
;
261 int start
= (h
* job
) / nb_jobs
;
262 int end
= (h
* (job
+1)) / nb_jobs
;
263 const int linesize
= s
->linesize
[0];
266 for (y
= start
; y
< end
; y
++) {
268 for (x
= 0; x
< w
; x
++) {
269 int u
, v
, subU
, subV
, sum
, sx
;
272 u
= s
->pv
[sx
+ sy
* linesize
][0] >> hsub
;
273 v
= s
->pv
[sx
+ sy
* linesize
][1] >> vsub
;
274 subU
= u
& (SUB_PIXELS
- 1);
275 subV
= v
& (SUB_PIXELS
- 1);
276 u
>>= SUB_PIXEL_BITS
;
277 v
>>= SUB_PIXEL_BITS
;
279 if (u
> 0 && v
> 0 && u
< w
- 2 && v
< h
- 2){
280 const int index
= u
+ v
*src_linesize
;
281 const int a
= s
->coeff
[subU
][0];
282 const int b
= s
->coeff
[subU
][1];
283 const int c
= s
->coeff
[subU
][2];
284 const int d
= s
->coeff
[subU
][3];
286 sum
= s
->coeff
[subV
][0] * (a
* src
[index
- 1 - src_linesize
] + b
* src
[index
- 0 - src_linesize
] +
287 c
* src
[index
+ 1 - src_linesize
] + d
* src
[index
+ 2 - src_linesize
]) +
288 s
->coeff
[subV
][1] * (a
* src
[index
- 1 ] + b
* src
[index
- 0 ] +
289 c
* src
[index
+ 1 ] + d
* src
[index
+ 2 ]) +
290 s
->coeff
[subV
][2] * (a
* src
[index
- 1 + src_linesize
] + b
* src
[index
- 0 + src_linesize
] +
291 c
* src
[index
+ 1 + src_linesize
] + d
* src
[index
+ 2 + src_linesize
]) +
292 s
->coeff
[subV
][3] * (a
* src
[index
- 1 + 2 * src_linesize
] + b
* src
[index
- 0 + 2 * src_linesize
] +
293 c
* src
[index
+ 1 + 2 * src_linesize
] + d
* src
[index
+ 2 + 2 * src_linesize
]);
299 for (dy
= 0; dy
< 4; dy
++) {
306 for (dx
= 0; dx
< 4; dx
++) {
314 sum
+= s
->coeff
[subU
][dx
] * s
->coeff
[subV
][dy
] * src
[ ix
+ iy
* src_linesize
];
319 sum
= (sum
+ (1<<(COEFF_BITS
* 2 - 1))) >> (COEFF_BITS
* 2);
320 sum
= av_clip(sum
, 0, 255);
321 dst
[x
+ y
* dst_linesize
] = sum
;
327 static int resample_linear(AVFilterContext
*ctx
, void *arg
,
328 int job
, int nb_jobs
)
330 PerspectiveContext
*s
= ctx
->priv
;
331 ThreadData
*td
= arg
;
332 uint8_t *dst
= td
->dst
;
333 int dst_linesize
= td
->dst_linesize
;
334 uint8_t *src
= td
->src
;
335 int src_linesize
= td
->src_linesize
;
340 int start
= (h
* job
) / nb_jobs
;
341 int end
= (h
* (job
+1)) / nb_jobs
;
342 const int linesize
= s
->linesize
[0];
345 for (y
= start
; y
< end
; y
++){
347 for (x
= 0; x
< w
; x
++){
348 int u
, v
, subU
, subV
, sum
, sx
, index
, subUI
, subVI
;
351 u
= s
->pv
[sx
+ sy
* linesize
][0] >> hsub
;
352 v
= s
->pv
[sx
+ sy
* linesize
][1] >> vsub
;
353 subU
= u
& (SUB_PIXELS
- 1);
354 subV
= v
& (SUB_PIXELS
- 1);
355 u
>>= SUB_PIXEL_BITS
;
356 v
>>= SUB_PIXEL_BITS
;
358 index
= u
+ v
* src_linesize
;
359 subUI
= SUB_PIXELS
- subU
;
360 subVI
= SUB_PIXELS
- subV
;
362 if ((unsigned)u
< (unsigned)(w
- 1)){
363 if((unsigned)v
< (unsigned)(h
- 1)){
364 sum
= subVI
* (subUI
* src
[index
] + subU
* src
[index
+ 1]) +
365 subV
* (subUI
* src
[index
+ src_linesize
] + subU
* src
[index
+ src_linesize
+ 1]);
366 sum
= (sum
+ (1 << (SUB_PIXEL_BITS
* 2 - 1)))>> (SUB_PIXEL_BITS
* 2);
372 index
= u
+ v
* src_linesize
;
373 sum
= subUI
* src
[index
] + subU
* src
[index
+ 1];
374 sum
= (sum
+ (1 << (SUB_PIXEL_BITS
- 1))) >> SUB_PIXEL_BITS
;
381 if ((unsigned)v
< (unsigned)(h
- 1)){
382 index
= u
+ v
* src_linesize
;
383 sum
= subVI
* src
[index
] + subV
* src
[index
+ src_linesize
];
384 sum
= (sum
+ (1 << (SUB_PIXEL_BITS
- 1))) >> SUB_PIXEL_BITS
;
390 index
= u
+ v
* src_linesize
;
395 sum
= av_clip(sum
, 0, 255);
396 dst
[x
+ y
* dst_linesize
] = sum
;
402 static av_cold
int init(AVFilterContext
*ctx
)
404 PerspectiveContext
*s
= ctx
->priv
;
406 switch (s
->interpolation
) {
407 case LINEAR
: s
->perspective
= resample_linear
; break;
408 case CUBIC
: s
->perspective
= resample_cubic
; break;
414 static int filter_frame(AVFilterLink
*inlink
, AVFrame
*frame
)
416 AVFilterContext
*ctx
= inlink
->dst
;
417 AVFilterLink
*outlink
= ctx
->outputs
[0];
418 PerspectiveContext
*s
= ctx
->priv
;
422 out
= ff_get_video_buffer(outlink
, outlink
->w
, outlink
->h
);
424 av_frame_free(&frame
);
425 return AVERROR(ENOMEM
);
427 av_frame_copy_props(out
, frame
);
429 for (plane
= 0; plane
< s
->nb_planes
; plane
++) {
430 int hsub
= plane
== 1 || plane
== 2 ? s
->hsub
: 0;
431 int vsub
= plane
== 1 || plane
== 2 ? s
->vsub
: 0;
432 ThreadData td
= {.dst
= out
->data
[plane
],
433 .dst_linesize
= out
->linesize
[plane
],
434 .src
= frame
->data
[plane
],
435 .src_linesize
= frame
->linesize
[plane
],
436 .w
= s
->linesize
[plane
],
437 .h
= s
->height
[plane
],
440 ctx
->internal
->execute(ctx
, s
->perspective
, &td
, NULL
, FFMIN(td
.h
, ctx
->graph
->nb_threads
));
443 av_frame_free(&frame
);
444 return ff_filter_frame(outlink
, out
);
447 static av_cold
void uninit(AVFilterContext
*ctx
)
449 PerspectiveContext
*s
= ctx
->priv
;
454 static const AVFilterPad perspective_inputs
[] = {
457 .type
= AVMEDIA_TYPE_VIDEO
,
458 .filter_frame
= filter_frame
,
459 .config_props
= config_input
,
464 static const AVFilterPad perspective_outputs
[] = {
467 .type
= AVMEDIA_TYPE_VIDEO
,
472 AVFilter ff_vf_perspective
= {
473 .name
= "perspective",
474 .description
= NULL_IF_CONFIG_SMALL("Correct the perspective of video."),
475 .priv_size
= sizeof(PerspectiveContext
),
478 .query_formats
= query_formats
,
479 .inputs
= perspective_inputs
,
480 .outputs
= perspective_outputs
,
481 .priv_class
= &perspective_class
,
482 .flags
= AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
| AVFILTER_FLAG_SLICE_THREADS
,