2 * Copyright (C) 2007 Richard Spindler (author of frei0r plugin from which this was derived)
3 * Copyright (C) 2014 Daniel Oberhoff
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 Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 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 GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Lenscorrection filter, algorithm from the frei0r plugin with the same name
29 #include "libavutil/opt.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/pixdesc.h"
37 typedef struct LenscorrectionCtx {
38 const AVClass *av_class;
43 double cx, cy, k1, k2;
46 int32_t *correction[4];
48 int (*filter_slice)(AVFilterContext *ctx, void *arg, int job, int nb_jobs);
51 #define OFFSET(x) offsetof(LenscorrectionCtx, x)
52 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
53 static const AVOption lenscorrection_options[] = {
54 { "cx", "set relative center x", OFFSET(cx), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
55 { "cy", "set relative center y", OFFSET(cy), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
56 { "k1", "set quadratic distortion factor", OFFSET(k1), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -1, 1, .flags=FLAGS },
57 { "k2", "set double quadratic distortion factor", OFFSET(k2), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -1, 1, .flags=FLAGS },
58 { "i", "set interpolation type", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=0}, 0, 64, .flags=FLAGS, "i" },
59 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=0},0, 0, .flags=FLAGS, "i" },
60 { "bilinear", "bilinear", 0, AV_OPT_TYPE_CONST, {.i64=1},0, 0, .flags=FLAGS, "i" },
64 AVFILTER_DEFINE_CLASS(lenscorrection);
66 typedef struct ThreadData {
74 static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
78 AVFrame *out = td->out;
80 const int w = td->w, h = td->h;
81 const int xcenter = td->xcenter;
82 const int ycenter = td->ycenter;
83 const int start = (h * job ) / nb_jobs;
84 const int end = (h * (job+1)) / nb_jobs;
85 const int plane = td->plane;
86 const int inlinesize = in->linesize[plane];
87 const int outlinesize = out->linesize[plane];
88 const uint8_t *indata = in->data[plane];
89 uint8_t *outrow = out->data[plane] + start * outlinesize;
91 for (i = start; i < end; i++, outrow += outlinesize) {
92 const int off_y = i - ycenter;
93 uint8_t *out = outrow;
95 for (j = 0; j < w; j++) {
96 const int off_x = j - xcenter;
97 const int64_t radius_mult = td->correction[j + i*w];
98 const int x = xcenter + ((radius_mult * off_x + (1<<23))>>24);
99 const int y = ycenter + ((radius_mult * off_y + (1<<23))>>24);
100 const char isvalid = x > 0 && x < w - 1 && y > 0 && y < h - 1;
101 *out++ = isvalid ? indata[y * inlinesize + x] : 0;
107 static int filter_slice_bilinear(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
109 ThreadData *td = arg;
110 AVFrame *in = td->in;
111 AVFrame *out = td->out;
113 const int64_t max = (1 << 24) - 1;
114 const int64_t add = (1 << 23);
115 const int w = td->w, h = td->h;
116 const int xcenter = td->xcenter;
117 const int ycenter = td->ycenter;
118 const int start = (h * job ) / nb_jobs;
119 const int end = (h * (job+1)) / nb_jobs;
120 const int plane = td->plane;
121 const int inlinesize = in->linesize[plane];
122 const int outlinesize = out->linesize[plane];
123 const uint8_t *indata = in->data[plane];
124 uint8_t *outrow = out->data[plane] + start * outlinesize;
126 for (int i = start; i < end; i++, outrow += outlinesize) {
127 const int off_y = i - ycenter;
128 uint8_t *out = outrow;
130 for (int j = 0; j < w; j++) {
131 const int off_x = j - xcenter;
132 const int64_t radius_mult = td->correction[j + i*w];
133 const int x = xcenter + ((radius_mult * off_x + (1<<23)) >> 24);
134 const int y = ycenter + ((radius_mult * off_y + (1<<23)) >> 24);
135 const char isvalid = x >= 0 && x <= w - 1 && y >= 0 && y <= h - 1;
138 const int nx = FFMIN(x + 1, w - 1);
139 const int ny = FFMIN(y + 1, h - 1);
140 const int64_t du = off_x >= 0 ? (radius_mult * off_x + add) & max : max - ((radius_mult * -off_x + add) & max);
141 const int64_t dv = off_y >= 0 ? (radius_mult * off_y + add) & max : max - ((radius_mult * -off_y + add) & max);
142 const int64_t p0 = indata[ y * inlinesize + x];
143 const int64_t p1 = indata[ y * inlinesize + nx];
144 const int64_t p2 = indata[ny * inlinesize + x];
145 const int64_t p3 = indata[ny * inlinesize + nx];
148 sum += (max - du) * (max - dv) * p0;
149 sum += ( du) * (max - dv) * p1;
150 sum += (max - du) * ( dv) * p2;
151 sum += ( du) * ( dv) * p3;
153 out[j] = av_clip_uint8((sum + (1LL << 47)) >> 48);
163 static int query_formats(AVFilterContext *ctx)
165 static const enum AVPixelFormat pix_fmts[] = {
167 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
168 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
169 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
170 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
171 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
174 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
178 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
180 return AVERROR(ENOMEM);
181 return ff_set_common_formats(ctx, fmts_list);
184 static av_cold void uninit(AVFilterContext *ctx)
186 LenscorrectionCtx *rect = ctx->priv;
189 for (i = 0; i < FF_ARRAY_ELEMS(rect->correction); i++) {
190 av_freep(&rect->correction[i]);
194 static int config_props(AVFilterLink *outlink)
196 AVFilterContext *ctx = outlink->src;
197 LenscorrectionCtx *rect = ctx->priv;
198 AVFilterLink *inlink = ctx->inputs[0];
199 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
200 rect->hsub = pixdesc->log2_chroma_w;
201 rect->vsub = pixdesc->log2_chroma_h;
202 outlink->w = rect->width = inlink->w;
203 outlink->h = rect->height = inlink->h;
204 rect->nb_planes = av_pix_fmt_count_planes(inlink->format);
205 rect->filter_slice = filter_slice;
206 if (rect->interpolation)
207 rect->filter_slice = filter_slice_bilinear;
211 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
213 AVFilterContext *ctx = inlink->dst;
214 AVFilterLink *outlink = ctx->outputs[0];
215 LenscorrectionCtx *rect = (LenscorrectionCtx*)ctx->priv;
216 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
221 return AVERROR(ENOMEM);
224 av_frame_copy_props(out, in);
226 for (plane = 0; plane < rect->nb_planes; ++plane) {
227 int hsub = plane == 1 || plane == 2 ? rect->hsub : 0;
228 int vsub = plane == 1 || plane == 2 ? rect->vsub : 0;
229 int w = AV_CEIL_RSHIFT(rect->width, hsub);
230 int h = AV_CEIL_RSHIFT(rect->height, vsub);
231 int xcenter = rect->cx * w;
232 int ycenter = rect->cy * h;
233 int k1 = rect->k1 * (1<<24);
234 int k2 = rect->k2 * (1<<24);
244 if (!rect->correction[plane]) {
246 const int64_t r2inv = (4LL<<60) / (w * w + h * h);
248 rect->correction[plane] = av_malloc_array(w, h * sizeof(**rect->correction));
249 if (!rect->correction[plane])
250 return AVERROR(ENOMEM);
251 for (j = 0; j < h; j++) {
252 const int off_y = j - ycenter;
253 const int off_y2 = off_y * off_y;
254 for (i = 0; i < w; i++) {
255 const int off_x = i - xcenter;
256 const int64_t r2 = ((off_x * off_x + off_y2) * r2inv + (1LL<<31)) >> 32;
257 const int64_t r4 = (r2 * r2 + (1<<27)) >> 28;
258 const int radius_mult = (r2 * k1 + r4 * k2 + (1LL<<27) + (1LL<<52))>>28;
259 rect->correction[plane][j * w + i] = radius_mult;
264 td.correction = rect->correction[plane];
265 ctx->internal->execute(ctx, rect->filter_slice, &td, NULL, FFMIN(h, ff_filter_get_nb_threads(ctx)));
269 return ff_filter_frame(outlink, out);
272 static const AVFilterPad lenscorrection_inputs[] = {
275 .type = AVMEDIA_TYPE_VIDEO,
276 .filter_frame = filter_frame,
281 static const AVFilterPad lenscorrection_outputs[] = {
284 .type = AVMEDIA_TYPE_VIDEO,
285 .config_props = config_props,
290 AVFilter ff_vf_lenscorrection = {
291 .name = "lenscorrection",
292 .description = NULL_IF_CONFIG_SMALL("Rectify the image by correcting for lens distortion."),
293 .priv_size = sizeof(LenscorrectionCtx),
294 .query_formats = query_formats,
295 .inputs = lenscorrection_inputs,
296 .outputs = lenscorrection_outputs,
297 .priv_class = &lenscorrection_class,
299 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,