Signed-off-by: Paul B Mahol <onemda@gmail.com>
Paul B Mahol authored on 2013/07/13 02:50:22... | ... |
@@ -2175,6 +2175,7 @@ mpdecimate_filter_deps="gpl avcodec" |
2175 | 2175 |
mptestsrc_filter_deps="gpl" |
2176 | 2176 |
negate_filter_deps="lut_filter" |
2177 | 2177 |
noise_filter_deps="gpl" |
2178 |
+perspective_filter_deps="gpl" |
|
2178 | 2179 |
resample_filter_deps="avresample" |
2179 | 2180 |
ocv_filter_deps="libopencv" |
2180 | 2181 |
owdenoise_filter_deps="gpl" |
... | ... |
@@ -5722,6 +5722,44 @@ pad="2*iw:2*ih:ow-iw:oh-ih" |
5722 | 5722 |
@end example |
5723 | 5723 |
@end itemize |
5724 | 5724 |
|
5725 |
+@section perspective |
|
5726 |
+ |
|
5727 |
+Correct perspective of video not recorded perpendicular to the screen. |
|
5728 |
+ |
|
5729 |
+A description of the accepted parameters follows. |
|
5730 |
+ |
|
5731 |
+@table @option |
|
5732 |
+@item x0 |
|
5733 |
+@item y0 |
|
5734 |
+@item x1 |
|
5735 |
+@item y1 |
|
5736 |
+@item x2 |
|
5737 |
+@item y2 |
|
5738 |
+@item x3 |
|
5739 |
+@item y3 |
|
5740 |
+Set coordinates expression for top left, top right, bottom left and bottom right corners. |
|
5741 |
+Default values are @code{0:0:W:0:0:H:W:H} with which perspective will remain unchanged. |
|
5742 |
+ |
|
5743 |
+The expressions can use the following variables: |
|
5744 |
+ |
|
5745 |
+@table @option |
|
5746 |
+@item W |
|
5747 |
+@item H |
|
5748 |
+the width and height of video frame. |
|
5749 |
+@end table |
|
5750 |
+ |
|
5751 |
+@item interpolation |
|
5752 |
+Set interpolation for perspective correction. |
|
5753 |
+ |
|
5754 |
+It accepts the following values: |
|
5755 |
+@table @samp |
|
5756 |
+@item linear |
|
5757 |
+@item cubic |
|
5758 |
+@end table |
|
5759 |
+ |
|
5760 |
+Default value is @samp{linear}. |
|
5761 |
+@end table |
|
5762 |
+ |
|
5725 | 5763 |
@section pixdesctest |
5726 | 5764 |
|
5727 | 5765 |
Pixel format descriptor test filter, mainly useful for internal |
... | ... |
@@ -166,6 +166,7 @@ OBJS-$(CONFIG_OVERLAY_FILTER) += vf_overlay.o dualinput.o |
166 | 166 |
OBJS-$(CONFIG_OWDENOISE_FILTER) += vf_owdenoise.o |
167 | 167 |
OBJS-$(CONFIG_PAD_FILTER) += vf_pad.o |
168 | 168 |
OBJS-$(CONFIG_PERMS_FILTER) += f_perms.o |
169 |
+OBJS-$(CONFIG_PERSPECTIVE_FILTER) += vf_perspective.o |
|
169 | 170 |
OBJS-$(CONFIG_PIXDESCTEST_FILTER) += vf_pixdesctest.o |
170 | 171 |
OBJS-$(CONFIG_PP_FILTER) += vf_pp.o |
171 | 172 |
OBJS-$(CONFIG_PSNR_FILTER) += vf_psnr.o dualinput.o |
... | ... |
@@ -161,6 +161,7 @@ void avfilter_register_all(void) |
161 | 161 |
REGISTER_FILTER(OWDENOISE, owdenoise, vf); |
162 | 162 |
REGISTER_FILTER(PAD, pad, vf); |
163 | 163 |
REGISTER_FILTER(PERMS, perms, vf); |
164 |
+ REGISTER_FILTER(PERSPECTIVE, perspective, vf); |
|
164 | 165 |
REGISTER_FILTER(PIXDESCTEST, pixdesctest, vf); |
165 | 166 |
REGISTER_FILTER(PP, pp, vf); |
166 | 167 |
REGISTER_FILTER(PSNR, psnr, vf); |
... | ... |
@@ -30,8 +30,8 @@ |
30 | 30 |
#include "libavutil/avutil.h" |
31 | 31 |
|
32 | 32 |
#define LIBAVFILTER_VERSION_MAJOR 3 |
33 |
-#define LIBAVFILTER_VERSION_MINOR 80 |
|
34 |
-#define LIBAVFILTER_VERSION_MICRO 101 |
|
33 |
+#define LIBAVFILTER_VERSION_MINOR 81 |
|
34 |
+#define LIBAVFILTER_VERSION_MICRO 100 |
|
35 | 35 |
|
36 | 36 |
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ |
37 | 37 |
LIBAVFILTER_VERSION_MINOR, \ |
38 | 38 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,407 @@ |
0 |
+/* |
|
1 |
+ * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at> |
|
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 General Public |
|
8 |
+ * License as published by the Free Software Foundation; either |
|
9 |
+ * version 2 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 |
|
14 |
+ * GNU General Public License for more details. |
|
15 |
+ * |
|
16 |
+ * You should have received a copy of the GNU General Public License along |
|
17 |
+ * 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/eval.h" |
|
22 |
+#include "libavutil/imgutils.h" |
|
23 |
+#include "libavutil/pixdesc.h" |
|
24 |
+#include "libavutil/opt.h" |
|
25 |
+#include "avfilter.h" |
|
26 |
+#include "formats.h" |
|
27 |
+#include "internal.h" |
|
28 |
+#include "video.h" |
|
29 |
+ |
|
30 |
+#define SUB_PIXEL_BITS 8 |
|
31 |
+#define SUB_PIXELS (1 << SUB_PIXEL_BITS) |
|
32 |
+#define COEFF_BITS 11 |
|
33 |
+ |
|
34 |
+#define LINEAR 0 |
|
35 |
+#define CUBIC 1 |
|
36 |
+ |
|
37 |
+typedef struct PerspectiveContext { |
|
38 |
+ const AVClass *class; |
|
39 |
+ char *expr_str[4][2]; |
|
40 |
+ double ref[4][2]; |
|
41 |
+ int32_t (*pv)[2]; |
|
42 |
+ int32_t coeff[SUB_PIXELS][4]; |
|
43 |
+ int interpolation; |
|
44 |
+ int linesize[4]; |
|
45 |
+ int height[4]; |
|
46 |
+ int hsub, vsub; |
|
47 |
+ int nb_planes; |
|
48 |
+ |
|
49 |
+ void (*perspective)(struct PerspectiveContext *s, |
|
50 |
+ uint8_t *dst, int dst_linesize, |
|
51 |
+ uint8_t *src, int src_linesize, |
|
52 |
+ int w, int h, int hsub, int vsub); |
|
53 |
+} PerspectiveContext; |
|
54 |
+ |
|
55 |
+#define OFFSET(x) offsetof(PerspectiveContext, x) |
|
56 |
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM |
|
57 |
+ |
|
58 |
+static const AVOption perspective_options[] = { |
|
59 |
+ { "x0", "set top left x coordinate", OFFSET(expr_str[0][0]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS }, |
|
60 |
+ { "y0", "set top left y coordinate", OFFSET(expr_str[0][1]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS }, |
|
61 |
+ { "x1", "set top right x coordinate", OFFSET(expr_str[1][0]), AV_OPT_TYPE_STRING, {.str="W"}, 0, 0, FLAGS }, |
|
62 |
+ { "y1", "set top right y coordinate", OFFSET(expr_str[1][1]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS }, |
|
63 |
+ { "x2", "set bottom left x coordinate", OFFSET(expr_str[2][0]), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS }, |
|
64 |
+ { "y2", "set bottom left y coordinate", OFFSET(expr_str[2][1]), AV_OPT_TYPE_STRING, {.str="H"}, 0, 0, FLAGS }, |
|
65 |
+ { "x3", "set bottom right x coordinate", OFFSET(expr_str[3][0]), AV_OPT_TYPE_STRING, {.str="W"}, 0, 0, FLAGS }, |
|
66 |
+ { "y3", "set bottom right y coordinate", OFFSET(expr_str[3][1]), AV_OPT_TYPE_STRING, {.str="H"}, 0, 0, FLAGS }, |
|
67 |
+ { "interpolation", "set interpolation", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=LINEAR}, 0, 1, FLAGS, "interpolation" }, |
|
68 |
+ { "linear", "", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "interpolation" }, |
|
69 |
+ { "cubic", "", 0, AV_OPT_TYPE_CONST, {.i64=CUBIC}, 0, 0, FLAGS, "interpolation" }, |
|
70 |
+ { NULL } |
|
71 |
+}; |
|
72 |
+ |
|
73 |
+AVFILTER_DEFINE_CLASS(perspective); |
|
74 |
+ |
|
75 |
+static int query_formats(AVFilterContext *ctx) |
|
76 |
+{ |
|
77 |
+ static const enum AVPixelFormat pix_fmts[] = { |
|
78 |
+ AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P, |
|
79 |
+ AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P, |
|
80 |
+ AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, |
|
81 |
+ AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE |
|
82 |
+ }; |
|
83 |
+ |
|
84 |
+ ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); |
|
85 |
+ return 0; |
|
86 |
+} |
|
87 |
+ |
|
88 |
+static inline double get_coeff(double d) |
|
89 |
+{ |
|
90 |
+ double coeff, A = -0.60; |
|
91 |
+ |
|
92 |
+ d = fabs(d); |
|
93 |
+ |
|
94 |
+ if (d < 1.0) |
|
95 |
+ coeff = (1.0 - (A + 3.0) * d * d + (A + 2.0) * d * d * d); |
|
96 |
+ else if (d < 2.0) |
|
97 |
+ coeff = (-4.0 * A + 8.0 * A * d - 5.0 * A * d * d + A * d * d * d); |
|
98 |
+ else |
|
99 |
+ coeff = 0.0; |
|
100 |
+ |
|
101 |
+ return coeff; |
|
102 |
+} |
|
103 |
+ |
|
104 |
+static const char *const var_names[] = { "W", "H", NULL }; |
|
105 |
+enum { VAR_W, VAR_H, VAR_VARS_NB }; |
|
106 |
+ |
|
107 |
+static int config_input(AVFilterLink *inlink) |
|
108 |
+{ |
|
109 |
+ double x0, x1, x2, x3, x4, x5, x6, x7, q; |
|
110 |
+ AVFilterContext *ctx = inlink->dst; |
|
111 |
+ PerspectiveContext *s = ctx->priv; |
|
112 |
+ double (*ref)[2] = s->ref; |
|
113 |
+ const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); |
|
114 |
+ double values[VAR_VARS_NB] = { [VAR_W] = inlink->w, [VAR_H] = inlink->h }; |
|
115 |
+ int h = inlink->h; |
|
116 |
+ int w = inlink->w; |
|
117 |
+ int x, y, i, j, ret; |
|
118 |
+ |
|
119 |
+ for (i = 0; i < 4; i++) { |
|
120 |
+ for (j = 0; j < 2; j++) { |
|
121 |
+ if (!s->expr_str[i][j]) |
|
122 |
+ return AVERROR(EINVAL); |
|
123 |
+ ret = av_expr_parse_and_eval(&s->ref[i][j], s->expr_str[i][j], |
|
124 |
+ var_names, &values[0], |
|
125 |
+ NULL, NULL, NULL, NULL, |
|
126 |
+ 0, 0, ctx); |
|
127 |
+ if (ret < 0) |
|
128 |
+ return ret; |
|
129 |
+ } |
|
130 |
+ } |
|
131 |
+ |
|
132 |
+ s->hsub = desc->log2_chroma_w; |
|
133 |
+ s->vsub = desc->log2_chroma_h; |
|
134 |
+ s->nb_planes = av_pix_fmt_count_planes(inlink->format); |
|
135 |
+ if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0) |
|
136 |
+ return ret; |
|
137 |
+ |
|
138 |
+ s->height[1] = s->height[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); |
|
139 |
+ s->height[0] = s->height[3] = inlink->h; |
|
140 |
+ |
|
141 |
+ s->pv = av_realloc_f(s->pv, w * h, 2 * sizeof(*s->pv)); |
|
142 |
+ if (!s->pv) |
|
143 |
+ return AVERROR(ENOMEM); |
|
144 |
+ |
|
145 |
+ x6 = ((ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0]) * |
|
146 |
+ (ref[2][1] - ref[3][1]) - |
|
147 |
+ ( ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1]) * |
|
148 |
+ (ref[2][0] - ref[3][0])) * h; |
|
149 |
+ x7 = ((ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1]) * |
|
150 |
+ (ref[1][0] - ref[3][0]) - |
|
151 |
+ ( ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0]) * |
|
152 |
+ (ref[1][1] - ref[3][1])) * w; |
|
153 |
+ q = ( ref[1][0] - ref[3][0]) * (ref[2][1] - ref[3][1]) - |
|
154 |
+ ( ref[2][0] - ref[3][0]) * (ref[1][1] - ref[3][1]); |
|
155 |
+ |
|
156 |
+ x0 = q * (ref[1][0] - ref[0][0]) * h + x6 * ref[1][0]; |
|
157 |
+ x1 = q * (ref[2][0] - ref[0][0]) * w + x7 * ref[2][0]; |
|
158 |
+ x2 = q * ref[0][0] * w * h; |
|
159 |
+ x3 = q * (ref[1][1] - ref[0][1]) * h + x6 * ref[1][1]; |
|
160 |
+ x4 = q * (ref[2][1] - ref[0][1]) * w + x7 * ref[2][1]; |
|
161 |
+ x5 = q * ref[0][1] * w * h; |
|
162 |
+ |
|
163 |
+ for (y = 0; y < h; y++){ |
|
164 |
+ for (x = 0; x < w; x++){ |
|
165 |
+ int u, v; |
|
166 |
+ |
|
167 |
+ u = (int)floor(SUB_PIXELS * (x0 * x + x1 * y + x2) / |
|
168 |
+ (x6 * x + x7 * y + q * w * h) + 0.5); |
|
169 |
+ v = (int)floor(SUB_PIXELS * (x3 * x + x4 * y + x5) / |
|
170 |
+ (x6 * x + x7 * y + q * w * h) + 0.5); |
|
171 |
+ |
|
172 |
+ s->pv[x + y * w][0] = u; |
|
173 |
+ s->pv[x + y * w][1] = v; |
|
174 |
+ } |
|
175 |
+ } |
|
176 |
+ |
|
177 |
+ for (i = 0; i < SUB_PIXELS; i++){ |
|
178 |
+ double d = i / (double)SUB_PIXELS; |
|
179 |
+ double temp[4]; |
|
180 |
+ double sum = 0; |
|
181 |
+ |
|
182 |
+ for (j = 0; j < 4; j++) |
|
183 |
+ temp[j] = get_coeff(j - d - 1); |
|
184 |
+ |
|
185 |
+ for (j = 0; j < 4; j++) |
|
186 |
+ sum += temp[j]; |
|
187 |
+ |
|
188 |
+ for (j = 0; j < 4; j++) |
|
189 |
+ s->coeff[i][j] = (int)floor((1 << COEFF_BITS) * temp[j] / sum + 0.5); |
|
190 |
+ } |
|
191 |
+ |
|
192 |
+ return 0; |
|
193 |
+} |
|
194 |
+ |
|
195 |
+static void resample_cubic(PerspectiveContext *s, |
|
196 |
+ uint8_t *dst, int dst_linesize, |
|
197 |
+ uint8_t *src, int src_linesize, |
|
198 |
+ int w, int h, int hsub, int vsub) |
|
199 |
+{ |
|
200 |
+ const int linesize = s->linesize[0]; |
|
201 |
+ int x, y; |
|
202 |
+ |
|
203 |
+ for (y = 0; y < h; y++) { |
|
204 |
+ int sy = y << vsub; |
|
205 |
+ for (x = 0; x < w; x++) { |
|
206 |
+ int u, v, subU, subV, sum, sx; |
|
207 |
+ |
|
208 |
+ sx = x << hsub; |
|
209 |
+ u = s->pv[sx + sy * linesize][0] >> hsub; |
|
210 |
+ v = s->pv[sx + sy * linesize][1] >> vsub; |
|
211 |
+ subU = u & (SUB_PIXELS - 1); |
|
212 |
+ subV = v & (SUB_PIXELS - 1); |
|
213 |
+ u >>= SUB_PIXEL_BITS; |
|
214 |
+ v >>= SUB_PIXEL_BITS; |
|
215 |
+ |
|
216 |
+ if (u > 0 && v > 0 && u < w - 2 && v < h - 2){ |
|
217 |
+ const int index = u + v*src_linesize; |
|
218 |
+ const int a = s->coeff[subU][0]; |
|
219 |
+ const int b = s->coeff[subU][1]; |
|
220 |
+ const int c = s->coeff[subU][2]; |
|
221 |
+ const int d = s->coeff[subU][3]; |
|
222 |
+ |
|
223 |
+ sum = s->coeff[subV][0] * (a * src[index - 1 - src_linesize] + b * src[index - 0 - src_linesize] + |
|
224 |
+ c * src[index + 1 - src_linesize] + d * src[index + 2 - src_linesize]) + |
|
225 |
+ s->coeff[subV][1] * (a * src[index - 1 ] + b * src[index - 0 ] + |
|
226 |
+ c * src[index + 1 ] + d * src[index + 2 ]) + |
|
227 |
+ s->coeff[subV][2] * (a * src[index - 1 + src_linesize] + b * src[index - 0 + src_linesize] + |
|
228 |
+ c * src[index + 1 + src_linesize] + d * src[index + 2 + src_linesize]) + |
|
229 |
+ s->coeff[subV][3] * (a * src[index - 1 + 2 * src_linesize] + b * src[index - 0 + 2 * src_linesize] + |
|
230 |
+ c * src[index + 1 + 2 * src_linesize] + d * src[index + 2 + 2 * src_linesize]); |
|
231 |
+ } else { |
|
232 |
+ int dx, dy; |
|
233 |
+ |
|
234 |
+ sum = 0; |
|
235 |
+ |
|
236 |
+ for (dy = 0; dy < 4; dy++) { |
|
237 |
+ int iy = v + dy - 1; |
|
238 |
+ |
|
239 |
+ if (iy < 0) |
|
240 |
+ iy = 0; |
|
241 |
+ else if (iy >= h) |
|
242 |
+ iy = h-1; |
|
243 |
+ for (dx = 0; dx < 4; dx++) { |
|
244 |
+ int ix = u + dx - 1; |
|
245 |
+ |
|
246 |
+ if (ix < 0) |
|
247 |
+ ix = 0; |
|
248 |
+ else if (ix >= w) |
|
249 |
+ ix = w - 1; |
|
250 |
+ |
|
251 |
+ sum += s->coeff[subU][dx] * s->coeff[subV][dy] * src[ ix + iy * src_linesize]; |
|
252 |
+ } |
|
253 |
+ } |
|
254 |
+ } |
|
255 |
+ |
|
256 |
+ sum = (sum + (1<<(COEFF_BITS * 2 - 1))) >> (COEFF_BITS * 2); |
|
257 |
+ sum = av_clip(sum, 0, 255); |
|
258 |
+ dst[x + y * dst_linesize] = sum; |
|
259 |
+ } |
|
260 |
+ } |
|
261 |
+} |
|
262 |
+ |
|
263 |
+static void resample_linear(PerspectiveContext *s, |
|
264 |
+ uint8_t *dst, int dst_linesize, |
|
265 |
+ uint8_t *src, int src_linesize, |
|
266 |
+ int w, int h, int hsub, int vsub) |
|
267 |
+{ |
|
268 |
+ const int linesize = s->linesize[0]; |
|
269 |
+ int x, y; |
|
270 |
+ |
|
271 |
+ for (y = 0; y < h; y++){ |
|
272 |
+ int sy = y << vsub; |
|
273 |
+ for (x = 0; x < w; x++){ |
|
274 |
+ int u, v, subU, subV, sum, sx, index, subUI, subVI; |
|
275 |
+ |
|
276 |
+ sx = x << hsub; |
|
277 |
+ u = s->pv[sx + sy * linesize][0] >> hsub; |
|
278 |
+ v = s->pv[sx + sy * linesize][1] >> vsub; |
|
279 |
+ subU = u & (SUB_PIXELS - 1); |
|
280 |
+ subV = v & (SUB_PIXELS - 1); |
|
281 |
+ u >>= SUB_PIXEL_BITS; |
|
282 |
+ v >>= SUB_PIXEL_BITS; |
|
283 |
+ |
|
284 |
+ index = u + v * src_linesize; |
|
285 |
+ subUI = SUB_PIXELS - subU; |
|
286 |
+ subVI = SUB_PIXELS - subV; |
|
287 |
+ |
|
288 |
+ if ((unsigned)u < (unsigned)(w - 1)){ |
|
289 |
+ if((unsigned)v < (unsigned)(h - 1)){ |
|
290 |
+ sum = subVI * (subUI * src[index] + subU * src[index + 1]) + |
|
291 |
+ subV * (subUI * src[index + src_linesize] + subU * src[index + src_linesize + 1]); |
|
292 |
+ sum = (sum + (1 << (SUB_PIXEL_BITS * 2 - 1)))>> (SUB_PIXEL_BITS * 2); |
|
293 |
+ } else { |
|
294 |
+ if (v < 0) |
|
295 |
+ v = 0; |
|
296 |
+ else |
|
297 |
+ v = h - 1; |
|
298 |
+ index = u + v * src_linesize; |
|
299 |
+ sum = subUI * src[index] + subU * src[index + 1]; |
|
300 |
+ sum = (sum + (1 << (SUB_PIXEL_BITS - 1))) >> SUB_PIXEL_BITS; |
|
301 |
+ } |
|
302 |
+ } else { |
|
303 |
+ if ((unsigned)v < (unsigned)(h - 1)){ |
|
304 |
+ if (u < 0) |
|
305 |
+ u = 0; |
|
306 |
+ else |
|
307 |
+ u = w - 1; |
|
308 |
+ index = u + v * src_linesize; |
|
309 |
+ sum = subVI * src[index] + subV * src[index + src_linesize]; |
|
310 |
+ sum = (sum + (1 << (SUB_PIXEL_BITS - 1))) >> SUB_PIXEL_BITS; |
|
311 |
+ } else { |
|
312 |
+ if (u < 0) |
|
313 |
+ u = 0; |
|
314 |
+ else |
|
315 |
+ u = w - 1; |
|
316 |
+ if (v < 0) |
|
317 |
+ v = 0; |
|
318 |
+ else |
|
319 |
+ v = h - 1; |
|
320 |
+ index = u + v * src_linesize; |
|
321 |
+ sum = src[index]; |
|
322 |
+ } |
|
323 |
+ } |
|
324 |
+ |
|
325 |
+ sum = av_clip(sum, 0, 255); |
|
326 |
+ dst[x + y * dst_linesize] = sum; |
|
327 |
+ } |
|
328 |
+ } |
|
329 |
+} |
|
330 |
+ |
|
331 |
+static av_cold int init(AVFilterContext *ctx) |
|
332 |
+{ |
|
333 |
+ PerspectiveContext *s = ctx->priv; |
|
334 |
+ |
|
335 |
+ switch (s->interpolation) { |
|
336 |
+ case LINEAR: s->perspective = resample_linear; break; |
|
337 |
+ case CUBIC: s->perspective = resample_cubic; break; |
|
338 |
+ } |
|
339 |
+ |
|
340 |
+ return 0; |
|
341 |
+} |
|
342 |
+ |
|
343 |
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame) |
|
344 |
+{ |
|
345 |
+ AVFilterContext *ctx = inlink->dst; |
|
346 |
+ AVFilterLink *outlink = ctx->outputs[0]; |
|
347 |
+ PerspectiveContext *s = ctx->priv; |
|
348 |
+ AVFrame *out; |
|
349 |
+ int plane; |
|
350 |
+ |
|
351 |
+ out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
|
352 |
+ if (!out) { |
|
353 |
+ av_frame_free(&frame); |
|
354 |
+ return AVERROR(ENOMEM); |
|
355 |
+ } |
|
356 |
+ av_frame_copy_props(out, frame); |
|
357 |
+ |
|
358 |
+ for (plane = 0; plane < s->nb_planes; plane++) { |
|
359 |
+ int hsub = plane == 1 || plane == 2 ? s->hsub : 0; |
|
360 |
+ int vsub = plane == 1 || plane == 2 ? s->vsub : 0; |
|
361 |
+ s->perspective(s, out->data[plane], out->linesize[plane], |
|
362 |
+ frame->data[plane], frame->linesize[plane], |
|
363 |
+ s->linesize[plane], s->height[plane], hsub, vsub); |
|
364 |
+ } |
|
365 |
+ |
|
366 |
+ av_frame_free(&frame); |
|
367 |
+ return ff_filter_frame(outlink, out); |
|
368 |
+} |
|
369 |
+ |
|
370 |
+static av_cold void uninit(AVFilterContext *ctx) |
|
371 |
+{ |
|
372 |
+ PerspectiveContext *s = ctx->priv; |
|
373 |
+ |
|
374 |
+ av_freep(&s->pv); |
|
375 |
+} |
|
376 |
+ |
|
377 |
+static const AVFilterPad perspective_inputs[] = { |
|
378 |
+ { |
|
379 |
+ .name = "default", |
|
380 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
381 |
+ .filter_frame = filter_frame, |
|
382 |
+ .config_props = config_input, |
|
383 |
+ }, |
|
384 |
+ { NULL } |
|
385 |
+}; |
|
386 |
+ |
|
387 |
+static const AVFilterPad perspective_outputs[] = { |
|
388 |
+ { |
|
389 |
+ .name = "default", |
|
390 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
391 |
+ }, |
|
392 |
+ { NULL } |
|
393 |
+}; |
|
394 |
+ |
|
395 |
+AVFilter avfilter_vf_perspective = { |
|
396 |
+ .name = "perspective", |
|
397 |
+ .description = NULL_IF_CONFIG_SMALL("Correct the perspective of video."), |
|
398 |
+ .priv_size = sizeof(PerspectiveContext), |
|
399 |
+ .init = init, |
|
400 |
+ .uninit = uninit, |
|
401 |
+ .query_formats = query_formats, |
|
402 |
+ .inputs = perspective_inputs, |
|
403 |
+ .outputs = perspective_outputs, |
|
404 |
+ .priv_class = &perspective_class, |
|
405 |
+ .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, |
|
406 |
+}; |