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;
44 double cx, cy, k1, k2;
47 int32_t *correction[4];
49 int (*filter_slice)(AVFilterContext *ctx, void *arg, int job, int nb_jobs);
52 #define OFFSET(x) offsetof(LenscorrectionCtx, x)
53 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
54 static const AVOption lenscorrection_options[] = {
55 { "cx", "set relative center x", OFFSET(cx), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
56 { "cy", "set relative center y", OFFSET(cy), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
57 { "k1", "set quadratic distortion factor", OFFSET(k1), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -1, 1, .flags=FLAGS },
58 { "k2", "set double quadratic distortion factor", OFFSET(k2), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -1, 1, .flags=FLAGS },
59 { "i", "set interpolation type", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=0}, 0, 64, .flags=FLAGS, "i" },
60 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=0},0, 0, .flags=FLAGS, "i" },
61 { "bilinear", "bilinear", 0, AV_OPT_TYPE_CONST, {.i64=1},0, 0, .flags=FLAGS, "i" },
65 AVFILTER_DEFINE_CLASS(lenscorrection);
67 typedef struct ThreadData {
76 #define NEAREST(type, name) \
77 static int filter##name##_slice(AVFilterContext *ctx, void *arg, int job, \
80 ThreadData *td = arg; \
81 AVFrame *in = td->in; \
82 AVFrame *out = td->out; \
84 const int w = td->w, h = td->h; \
85 const int xcenter = td->xcenter; \
86 const int ycenter = td->ycenter; \
87 const int start = (h * job ) / nb_jobs; \
88 const int end = (h * (job+1)) / nb_jobs; \
89 const int plane = td->plane; \
90 const int inlinesize = in->linesize[plane] / sizeof(type); \
91 const int outlinesize = out->linesize[plane] / sizeof(type); \
92 const type *indata = (const type *)in->data[plane]; \
93 type *outrow = (type *)out->data[plane] + start * outlinesize; \
94 for (int i = start; i < end; i++, outrow += outlinesize) { \
95 const int off_y = i - ycenter; \
97 for (int j = 0; j < w; j++) { \
98 const int off_x = j - xcenter; \
99 const int64_t radius_mult = td->correction[j + i*w]; \
100 const int x = xcenter + ((radius_mult * off_x + (1<<23))>>24); \
101 const int y = ycenter + ((radius_mult * off_y + (1<<23))>>24); \
102 const char isvalid = x >= 0 && x < w && y >= 0 && y < h; \
103 *out++ = isvalid ? indata[y * inlinesize + x] : 0; \
111 NEAREST(uint16_t, 16)
113 #define BILINEAR(type, name) \
114 static int filter##name##_slice_bilinear(AVFilterContext *ctx, void *arg, \
115 int job, int nb_jobs) \
117 ThreadData *td = arg; \
118 AVFrame *in = td->in; \
119 AVFrame *out = td->out; \
121 const int depth = td->depth; \
122 const uint64_t max = (1 << 24) - 1; \
123 const uint64_t add = (1 << 23); \
124 const int w = td->w, h = td->h; \
125 const int xcenter = td->xcenter; \
126 const int ycenter = td->ycenter; \
127 const int start = (h * job ) / nb_jobs; \
128 const int end = (h * (job+1)) / nb_jobs; \
129 const int plane = td->plane; \
130 const int inlinesize = in->linesize[plane] / sizeof(type); \
131 const int outlinesize = out->linesize[plane] / sizeof(type); \
132 const type *indata = (const type *)in->data[plane]; \
133 type *outrow = (type *)out->data[plane] + start * outlinesize; \
135 for (int i = start; i < end; i++, outrow += outlinesize) { \
136 const int off_y = i - ycenter; \
137 type *out = outrow; \
139 for (int j = 0; j < w; j++) { \
140 const int off_x = j - xcenter; \
141 const int64_t radius_mult = td->correction[j + i*w]; \
142 const int x = xcenter + ((radius_mult * off_x + (1<<23)) >> 24); \
143 const int y = ycenter + ((radius_mult * off_y + (1<<23)) >> 24); \
144 const char isvalid = x >= 0 && x <= w - 1 && y >= 0 && y <= h - 1; \
147 const int nx = FFMIN(x + 1, w - 1); \
148 const int ny = FFMIN(y + 1, h - 1); \
149 const uint64_t du = off_x >= 0 ? (radius_mult * off_x + add) & max : max - ((radius_mult * -off_x + add) & max); \
150 const uint64_t dv = off_y >= 0 ? (radius_mult * off_y + add) & max : max - ((radius_mult * -off_y + add) & max); \
151 const uint64_t p0 = indata[ y * inlinesize + x]; \
152 const uint64_t p1 = indata[ y * inlinesize + nx]; \
153 const uint64_t p2 = indata[ny * inlinesize + x]; \
154 const uint64_t p3 = indata[ny * inlinesize + nx]; \
157 sum += (max - du) * (max - dv) * p0; \
158 sum += ( du) * (max - dv) * p1; \
159 sum += (max - du) * ( dv) * p2; \
160 sum += ( du) * ( dv) * p3; \
162 out[j] = av_clip_uintp2_c((sum + (1ULL << 47)) >> 48, depth); \
173 BILINEAR(uint16_t, 16)
175 static int query_formats(AVFilterContext *ctx)
177 static const enum AVPixelFormat pix_fmts[] = {
178 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
179 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
181 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
182 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
183 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
184 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
185 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
187 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
188 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
189 AV_PIX_FMT_YUV440P10,
190 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
191 AV_PIX_FMT_YUV440P12,
192 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
193 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
194 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
195 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
196 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
197 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
198 AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
199 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
200 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
203 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
205 return AVERROR(ENOMEM);
206 return ff_set_common_formats(ctx, fmts_list);
209 static av_cold void uninit(AVFilterContext *ctx)
211 LenscorrectionCtx *rect = ctx->priv;
214 for (i = 0; i < FF_ARRAY_ELEMS(rect->correction); i++) {
215 av_freep(&rect->correction[i]);
219 static int config_props(AVFilterLink *outlink)
221 AVFilterContext *ctx = outlink->src;
222 LenscorrectionCtx *rect = ctx->priv;
223 AVFilterLink *inlink = ctx->inputs[0];
224 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
226 rect->depth = pixdesc->comp[0].depth;
227 rect->hsub = pixdesc->log2_chroma_w;
228 rect->vsub = pixdesc->log2_chroma_h;
229 outlink->w = rect->width = inlink->w;
230 outlink->h = rect->height = inlink->h;
231 rect->nb_planes = av_pix_fmt_count_planes(inlink->format);
232 rect->filter_slice = rect->depth <= 8 ? filter8_slice : filter16_slice;
233 if (rect->interpolation)
234 rect->filter_slice = rect->depth <= 8 ? filter8_slice_bilinear : filter16_slice_bilinear;
238 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
240 AVFilterContext *ctx = inlink->dst;
241 AVFilterLink *outlink = ctx->outputs[0];
242 LenscorrectionCtx *rect = (LenscorrectionCtx*)ctx->priv;
243 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
248 return AVERROR(ENOMEM);
251 av_frame_copy_props(out, in);
253 for (plane = 0; plane < rect->nb_planes; ++plane) {
254 int hsub = plane == 1 || plane == 2 ? rect->hsub : 0;
255 int vsub = plane == 1 || plane == 2 ? rect->vsub : 0;
256 int w = AV_CEIL_RSHIFT(rect->width, hsub);
257 int h = AV_CEIL_RSHIFT(rect->height, vsub);
258 int xcenter = rect->cx * w;
259 int ycenter = rect->cy * h;
260 int k1 = rect->k1 * (1<<24);
261 int k2 = rect->k2 * (1<<24);
270 .depth = rect->depth,
273 if (!rect->correction[plane]) {
275 const int64_t r2inv = (4LL<<60) / (w * w + h * h);
277 rect->correction[plane] = av_malloc_array(w, h * sizeof(**rect->correction));
278 if (!rect->correction[plane])
279 return AVERROR(ENOMEM);
280 for (j = 0; j < h; j++) {
281 const int off_y = j - ycenter;
282 const int off_y2 = off_y * off_y;
283 for (i = 0; i < w; i++) {
284 const int off_x = i - xcenter;
285 const int64_t r2 = ((off_x * off_x + off_y2) * r2inv + (1LL<<31)) >> 32;
286 const int64_t r4 = (r2 * r2 + (1<<27)) >> 28;
287 const int radius_mult = (r2 * k1 + r4 * k2 + (1LL<<27) + (1LL<<52))>>28;
288 rect->correction[plane][j * w + i] = radius_mult;
293 td.correction = rect->correction[plane];
294 ctx->internal->execute(ctx, rect->filter_slice, &td, NULL, FFMIN(h, ff_filter_get_nb_threads(ctx)));
298 return ff_filter_frame(outlink, out);
301 static const AVFilterPad lenscorrection_inputs[] = {
304 .type = AVMEDIA_TYPE_VIDEO,
305 .filter_frame = filter_frame,
310 static const AVFilterPad lenscorrection_outputs[] = {
313 .type = AVMEDIA_TYPE_VIDEO,
314 .config_props = config_props,
319 AVFilter ff_vf_lenscorrection = {
320 .name = "lenscorrection",
321 .description = NULL_IF_CONFIG_SMALL("Rectify the image by correcting for lens distortion."),
322 .priv_size = sizeof(LenscorrectionCtx),
323 .query_formats = query_formats,
324 .inputs = lenscorrection_inputs,
325 .outputs = lenscorrection_outputs,
326 .priv_class = &lenscorrection_class,
328 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,