Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
Daniel Oberhoff authored on 2014/08/12 07:52:45... | ... |
@@ -5546,6 +5546,51 @@ kerndeint=map=1 |
5546 | 5546 |
@end example |
5547 | 5547 |
@end itemize |
5548 | 5548 |
|
5549 |
+@section lenscorrection |
|
5550 |
+ |
|
5551 |
+Correct radial lens distortion |
|
5552 |
+ |
|
5553 |
+This filter can be used to correct for radial distortion as can result from the use |
|
5554 |
+of wide angle lenses, and thereby re-rectify the image. To find the right parameters |
|
5555 |
+one can use tools available for example as part of opencv or simply trial-and-error. |
|
5556 |
+To use opencv use the calibration sample (under samples/cpp) from the opencv sources |
|
5557 |
+and extract the k1 and k2 coefficients from the resulting matrix. |
|
5558 |
+ |
|
5559 |
+Note that effectively the same filter is available in the open-source tools Krita and |
|
5560 |
+Digikam from the KDE project. |
|
5561 |
+ |
|
5562 |
+In contrast to the @ref{vignette} filter, which can also be used to compensate lens errors, |
|
5563 |
+this filter corrects the distortion of the image, whereas @ref{vignette} corrects the |
|
5564 |
+brightness distribution, so you may want to use both filters together in certain |
|
5565 |
+cases, though you will have to take care of ordering, i.e. whether vignetting should |
|
5566 |
+be applied before or after lens correction. |
|
5567 |
+ |
|
5568 |
+@subsection Options |
|
5569 |
+ |
|
5570 |
+The filter accepts the following options: |
|
5571 |
+ |
|
5572 |
+@table @option |
|
5573 |
+@item cx |
|
5574 |
+Relative x-coordinate of the focal point of the image, and thereby the center of the |
|
5575 |
+distrortion. This value has a range [0,1] and is expressed as fractions of the image |
|
5576 |
+width. |
|
5577 |
+@item cy |
|
5578 |
+Relative y-coordinate of the focal point of the image, and thereby the center of the |
|
5579 |
+distrortion. This value has a range [0,1] and is expressed as fractions of the image |
|
5580 |
+height. |
|
5581 |
+@item k1 |
|
5582 |
+Coefficient of the quadratic correction term. 0.5 means no correction. |
|
5583 |
+@item k2 |
|
5584 |
+Coefficient of the double quadratic correction term. 0.5 means no correction. |
|
5585 |
+@end table |
|
5586 |
+ |
|
5587 |
+The formula that generates the correction is: |
|
5588 |
+ |
|
5589 |
+@var{r_src} = @var{r_tgt} * (1 + @var{k1} * (@var{r_tgt} / @var{r_0})^2 + @var{k2} * (@var{r_tgt} / @var{r_0})^4) |
|
5590 |
+ |
|
5591 |
+where @var{r_0} is halve of the image diagonal and @var{r_src} and @var{r_tgt} are the |
|
5592 |
+distances from the focal point in the source and target images, respectively. |
|
5593 |
+ |
|
5549 | 5594 |
@anchor{lut3d} |
5550 | 5595 |
@section lut3d |
5551 | 5596 |
|
... | ... |
@@ -8758,6 +8803,7 @@ For example, to vertically flip a video with @command{ffmpeg}: |
8758 | 8758 |
ffmpeg -i in.avi -vf "vflip" out.avi |
8759 | 8759 |
@end example |
8760 | 8760 |
|
8761 |
+@anchor{vignette} |
|
8761 | 8762 |
@section vignette |
8762 | 8763 |
|
8763 | 8764 |
Make or reverse a natural vignetting effect. |
... | ... |
@@ -138,6 +138,7 @@ OBJS-$(CONFIG_IL_FILTER) += vf_il.o |
138 | 138 |
OBJS-$(CONFIG_INTERLACE_FILTER) += vf_interlace.o |
139 | 139 |
OBJS-$(CONFIG_INTERLEAVE_FILTER) += f_interleave.o |
140 | 140 |
OBJS-$(CONFIG_KERNDEINT_FILTER) += vf_kerndeint.o |
141 |
+OBJS-$(CONFIG_LENSCORRECTION_FILTER) += vf_lenscorrection.o |
|
141 | 142 |
OBJS-$(CONFIG_LUT3D_FILTER) += vf_lut3d.o |
142 | 143 |
OBJS-$(CONFIG_LUT_FILTER) += vf_lut.o |
143 | 144 |
OBJS-$(CONFIG_LUTRGB_FILTER) += vf_lut.o |
... | ... |
@@ -156,6 +156,7 @@ void avfilter_register_all(void) |
156 | 156 |
REGISTER_FILTER(INTERLACE, interlace, vf); |
157 | 157 |
REGISTER_FILTER(INTERLEAVE, interleave, vf); |
158 | 158 |
REGISTER_FILTER(KERNDEINT, kerndeint, vf); |
159 |
+ REGISTER_FILTER(LENSCORRECTION, lenscorrection, vf); |
|
159 | 160 |
REGISTER_FILTER(LUT3D, lut3d, vf); |
160 | 161 |
REGISTER_FILTER(LUT, lut, vf); |
161 | 162 |
REGISTER_FILTER(LUTRGB, lutrgb, vf); |
162 | 163 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,198 @@ |
0 |
+/* |
|
1 |
+ * Copyright (C) 2007 Richard Spindler (author of frei0r plugin from which this was derived) |
|
2 |
+ * Copyright (C) 2014 Daniel Oberhoff |
|
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 |
+/** |
|
22 |
+ * @file |
|
23 |
+ * Lenscorrection filter, algorithm from the frei0r plugin with the same name |
|
24 |
+*/ |
|
25 |
+#include <stdlib.h> |
|
26 |
+#include <math.h> |
|
27 |
+ |
|
28 |
+#include "libavutil/opt.h" |
|
29 |
+#include "libavutil/intreadwrite.h" |
|
30 |
+#include "libavutil/pixdesc.h" |
|
31 |
+ |
|
32 |
+#include "avfilter.h" |
|
33 |
+#include "internal.h" |
|
34 |
+#include "video.h" |
|
35 |
+ |
|
36 |
+typedef struct LenscorrectionCtx { |
|
37 |
+ const AVClass *av_class; |
|
38 |
+ unsigned int width; |
|
39 |
+ unsigned int height; |
|
40 |
+ int hsub, vsub; |
|
41 |
+ int nb_planes; |
|
42 |
+ double cx, cy, k1, k2; |
|
43 |
+} LenscorrectionCtx; |
|
44 |
+ |
|
45 |
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM |
|
46 |
+static const AVOption lenscorrection_options[] = { |
|
47 |
+ { "cx", "set relative center x", offsetof(LenscorrectionCtx, cx), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS }, |
|
48 |
+ { "cy", "set relative center y", offsetof(LenscorrectionCtx, cy), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS }, |
|
49 |
+ { "k1", "set quadratic distortion factor", offsetof(LenscorrectionCtx, k1), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -1, 1, .flags=FLAGS }, |
|
50 |
+ { "k2", "set double quadratic distortion factor", offsetof(LenscorrectionCtx, k2), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -1, 1, .flags=FLAGS }, |
|
51 |
+ { NULL } |
|
52 |
+}; |
|
53 |
+ |
|
54 |
+AVFILTER_DEFINE_CLASS(lenscorrection); |
|
55 |
+ |
|
56 |
+typedef struct ThreadData { |
|
57 |
+ AVFrame *in, *out; |
|
58 |
+ float w, h; |
|
59 |
+ int plane; |
|
60 |
+ float xcenter, ycenter; |
|
61 |
+ float k1, k2; |
|
62 |
+} ThreadData; |
|
63 |
+ |
|
64 |
+static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs) |
|
65 |
+{ |
|
66 |
+ ThreadData *td = (ThreadData*)arg; |
|
67 |
+ AVFrame *in = td->in; |
|
68 |
+ AVFrame *out = td->out; |
|
69 |
+ |
|
70 |
+ const float w = td->w, h = td->h; |
|
71 |
+ const float xcenter = td->xcenter; |
|
72 |
+ const float ycenter = td->ycenter; |
|
73 |
+ const float r2inv = 4.0 / (w * w + h * h); |
|
74 |
+ const float k1 = td->k1; |
|
75 |
+ const float k2 = td->k2; |
|
76 |
+ const int start = (h * job ) / nb_jobs; |
|
77 |
+ const int end = (h * (job+1)) / nb_jobs; |
|
78 |
+ const int plane = td->plane; |
|
79 |
+ const int inlinesize = in->linesize[plane]; |
|
80 |
+ const int outlinesize = out->linesize[plane]; |
|
81 |
+ const uint8_t *indata = in->data[plane]; |
|
82 |
+ uint8_t *outrow = out->data[plane] + start * outlinesize; |
|
83 |
+ int i; |
|
84 |
+ for (i = start; i < end; i++, outrow += outlinesize) { |
|
85 |
+ const float off_y = i - ycenter; |
|
86 |
+ const float off_y2 = off_y * off_y; |
|
87 |
+ uint8_t *out = outrow; |
|
88 |
+ int j; |
|
89 |
+ for (j = 0; j < w; j++) { |
|
90 |
+ const float off_x = j - xcenter; |
|
91 |
+ const float r2 = (off_x * off_x + off_y2) * r2inv; |
|
92 |
+ const float radius_mult = 1.0f + r2 * k1 + r2 * r2 * k2; |
|
93 |
+ const int x = xcenter + radius_mult * off_x + 0.5f; |
|
94 |
+ const int y = ycenter + radius_mult * off_y + 0.5f; |
|
95 |
+ const char isvalid = x > 0 && x < w - 1 && y > 0 && y < h - 1; |
|
96 |
+ *out++ = isvalid ? indata[y * inlinesize + x] : 0; |
|
97 |
+ } |
|
98 |
+ } |
|
99 |
+ return 0; |
|
100 |
+} |
|
101 |
+ |
|
102 |
+static int query_formats(AVFilterContext *ctx) |
|
103 |
+{ |
|
104 |
+ static enum AVPixelFormat pix_fmts[] = { |
|
105 |
+ AV_PIX_FMT_YUV410P, |
|
106 |
+ AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P, |
|
107 |
+ AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P, |
|
108 |
+ AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA420P, |
|
109 |
+ AV_PIX_FMT_YUV422P, |
|
110 |
+ AV_PIX_FMT_NONE |
|
111 |
+ }; |
|
112 |
+ |
|
113 |
+ ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); |
|
114 |
+ return 0; |
|
115 |
+} |
|
116 |
+ |
|
117 |
+static int config_props(AVFilterLink *outlink) |
|
118 |
+{ |
|
119 |
+ AVFilterContext *ctx = outlink->src; |
|
120 |
+ LenscorrectionCtx *rect = ctx->priv; |
|
121 |
+ AVFilterLink *inlink = ctx->inputs[0]; |
|
122 |
+ const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format); |
|
123 |
+ rect->hsub = pixdesc->log2_chroma_w; |
|
124 |
+ rect->vsub = pixdesc->log2_chroma_h; |
|
125 |
+ outlink->w = rect->width = inlink->w; |
|
126 |
+ outlink->h = rect->height = inlink->h; |
|
127 |
+ rect->nb_planes = av_pix_fmt_count_planes(inlink->format); |
|
128 |
+ return 0; |
|
129 |
+} |
|
130 |
+ |
|
131 |
+static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
|
132 |
+{ |
|
133 |
+ AVFilterContext *ctx = inlink->dst; |
|
134 |
+ AVFilterLink *outlink = ctx->outputs[0]; |
|
135 |
+ LenscorrectionCtx *rect = (LenscorrectionCtx*)ctx->priv; |
|
136 |
+ AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
|
137 |
+ int plane; |
|
138 |
+ |
|
139 |
+ if (!out) { |
|
140 |
+ av_frame_free(&in); |
|
141 |
+ return AVERROR(ENOMEM); |
|
142 |
+ } |
|
143 |
+ |
|
144 |
+ av_frame_copy_props(out, in); |
|
145 |
+ |
|
146 |
+ for (plane = 0; plane < rect->nb_planes; ++plane) { |
|
147 |
+ int hsub = plane == 1 || plane == 2 ? rect->hsub : 0; |
|
148 |
+ int vsub = plane == 1 || plane == 2 ? rect->vsub : 0; |
|
149 |
+ float hdiv = 1 << hsub; |
|
150 |
+ float vdiv = 1 << vsub; |
|
151 |
+ float w = rect->width / hdiv; |
|
152 |
+ float h = rect->height / vdiv; |
|
153 |
+ ThreadData td = { |
|
154 |
+ .in = in, |
|
155 |
+ .out = out, |
|
156 |
+ .w = w, |
|
157 |
+ .h = h, |
|
158 |
+ .xcenter = rect->cx * w, |
|
159 |
+ .ycenter = rect->cy * h, |
|
160 |
+ .k1 = rect->k1, |
|
161 |
+ .k2 = rect->k2, |
|
162 |
+ .plane = plane}; |
|
163 |
+ ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(h, ctx->graph->nb_threads)); |
|
164 |
+ } |
|
165 |
+ |
|
166 |
+ av_frame_free(&in); |
|
167 |
+ return ff_filter_frame(outlink, out); |
|
168 |
+} |
|
169 |
+ |
|
170 |
+static const AVFilterPad lenscorrection_inputs[] = { |
|
171 |
+ { |
|
172 |
+ .name = "default", |
|
173 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
174 |
+ .filter_frame = filter_frame, |
|
175 |
+ }, |
|
176 |
+ { NULL } |
|
177 |
+}; |
|
178 |
+ |
|
179 |
+static const AVFilterPad lenscorrection_outputs[] = { |
|
180 |
+ { |
|
181 |
+ .name = "default", |
|
182 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
183 |
+ .config_props = config_props, |
|
184 |
+ }, |
|
185 |
+ { NULL } |
|
186 |
+}; |
|
187 |
+ |
|
188 |
+AVFilter ff_vf_lenscorrection = { |
|
189 |
+ .name = "lenscorrection", |
|
190 |
+ .description = NULL_IF_CONFIG_SMALL("Rectify the image by correcting for lens distortion."), |
|
191 |
+ .priv_size = sizeof(LenscorrectionCtx), |
|
192 |
+ .query_formats = query_formats, |
|
193 |
+ .inputs = lenscorrection_inputs, |
|
194 |
+ .outputs = lenscorrection_outputs, |
|
195 |
+ .priv_class = &lenscorrection_class, |
|
196 |
+ .flags = AVFILTER_FLAG_SLICE_THREADS, |
|
197 |
+}; |