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/colorspace.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/pixdesc.h"
35 #include "drawutils.h"
39 typedef struct LenscorrectionCtx {
40 const AVClass *av_class;
45 double cx, cy, k1, k2;
50 int32_t *correction[4];
52 int (*filter_slice)(AVFilterContext *ctx, void *arg, int job, int nb_jobs, int plane);
55 #define OFFSET(x) offsetof(LenscorrectionCtx, x)
56 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
57 static const AVOption lenscorrection_options[] = {
58 { "cx", "set relative center x", OFFSET(cx), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
59 { "cy", "set relative center y", OFFSET(cy), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
60 { "k1", "set quadratic distortion factor", OFFSET(k1), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -1, 1, .flags=FLAGS },
61 { "k2", "set double quadratic distortion factor", OFFSET(k2), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -1, 1, .flags=FLAGS },
62 { "i", "set interpolation type", OFFSET(interpolation), AV_OPT_TYPE_INT, {.i64=0}, 0, 64, .flags=FLAGS, "i" },
63 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=0},0, 0, .flags=FLAGS, "i" },
64 { "bilinear", "bilinear", 0, AV_OPT_TYPE_CONST, {.i64=1},0, 0, .flags=FLAGS, "i" },
65 { "fc", "set the color of the unmapped pixels", OFFSET(fill_rgba), AV_OPT_TYPE_COLOR, {.str="black@0"}, .flags = FLAGS },
69 AVFILTER_DEFINE_CLASS(lenscorrection);
71 typedef struct ThreadData {
75 #define NEAREST(type, name) \
76 static int filter##name##_slice(AVFilterContext *ctx, void *arg, int job, \
77 int nb_jobs, int plane) \
79 LenscorrectionCtx *rect = ctx->priv; \
80 ThreadData *td = arg; \
81 AVFrame *in = td->in; \
82 AVFrame *out = td->out; \
84 const int32_t *correction = rect->correction[plane]; \
85 const int fill_color = rect->fill_color[plane]; \
86 const int w = rect->planewidth[plane], h = rect->planeheight[plane]; \
87 const int xcenter = rect->cx * w; \
88 const int ycenter = rect->cy * h; \
89 const int start = (h * job ) / nb_jobs; \
90 const int end = (h * (job+1)) / nb_jobs; \
91 const int inlinesize = in->linesize[plane] / sizeof(type); \
92 const int outlinesize = out->linesize[plane] / sizeof(type); \
93 const type *indata = (const type *)in->data[plane]; \
94 type *outrow = (type *)out->data[plane] + start * outlinesize; \
95 for (int i = start; i < end; i++, outrow += outlinesize) { \
96 const int off_y = i - ycenter; \
98 for (int j = 0; j < w; j++) { \
99 const int off_x = j - xcenter; \
100 const int64_t radius_mult = correction[j + i*w]; \
101 const int x = xcenter + ((radius_mult * off_x + (1<<23))>>24); \
102 const int y = ycenter + ((radius_mult * off_y + (1<<23))>>24); \
103 const char isvalid = x >= 0 && x < w && y >= 0 && y < h; \
104 *out++ = isvalid ? indata[y * inlinesize + x] : fill_color; \
112 NEAREST(uint16_t, 16)
114 #define BILINEAR(type, name) \
115 static int filter##name##_slice_bilinear(AVFilterContext *ctx, void *arg, \
116 int job, int nb_jobs, int plane) \
118 LenscorrectionCtx *rect = ctx->priv; \
119 ThreadData *td = arg; \
120 AVFrame *in = td->in; \
121 AVFrame *out = td->out; \
123 const int32_t *correction = rect->correction[plane]; \
124 const int fill_color = rect->fill_color[plane]; \
125 const int depth = rect->depth; \
126 const uint64_t max = (1 << 24) - 1; \
127 const uint64_t add = (1 << 23); \
128 const int w = rect->planewidth[plane], h = rect->planeheight[plane]; \
129 const int xcenter = rect->cx * w; \
130 const int ycenter = rect->cy * h; \
131 const int start = (h * job ) / nb_jobs; \
132 const int end = (h * (job+1)) / nb_jobs; \
133 const int inlinesize = in->linesize[plane] / sizeof(type); \
134 const int outlinesize = out->linesize[plane] / sizeof(type); \
135 const type *indata = (const type *)in->data[plane]; \
136 type *outrow = (type *)out->data[plane] + start * outlinesize; \
138 for (int i = start; i < end; i++, outrow += outlinesize) { \
139 const int off_y = i - ycenter; \
140 type *out = outrow; \
142 for (int j = 0; j < w; j++) { \
143 const int off_x = j - xcenter; \
144 const int64_t radius_mult = correction[j + i*w]; \
145 const int x = xcenter + ((radius_mult * off_x + (1<<23)) >> 24); \
146 const int y = ycenter + ((radius_mult * off_y + (1<<23)) >> 24); \
147 const char isvalid = x >= 0 && x <= w - 1 && y >= 0 && y <= h - 1; \
150 const int nx = FFMIN(x + 1, w - 1); \
151 const int ny = FFMIN(y + 1, h - 1); \
152 const uint64_t du = off_x >= 0 ? (radius_mult * off_x + add) & max : max - ((radius_mult * -off_x + add) & max); \
153 const uint64_t dv = off_y >= 0 ? (radius_mult * off_y + add) & max : max - ((radius_mult * -off_y + add) & max); \
154 const uint64_t p0 = indata[ y * inlinesize + x]; \
155 const uint64_t p1 = indata[ y * inlinesize + nx]; \
156 const uint64_t p2 = indata[ny * inlinesize + x]; \
157 const uint64_t p3 = indata[ny * inlinesize + nx]; \
160 sum += (max - du) * (max - dv) * p0; \
161 sum += ( du) * (max - dv) * p1; \
162 sum += (max - du) * ( dv) * p2; \
163 sum += ( du) * ( dv) * p3; \
165 out[j] = av_clip_uintp2_c((sum + (1ULL << 47)) >> 48, depth); \
167 out[j] = fill_color; \
176 BILINEAR(uint16_t, 16)
178 static int query_formats(AVFilterContext *ctx)
180 static const enum AVPixelFormat pix_fmts[] = {
181 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
182 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14,
184 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
185 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
186 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
187 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
188 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
190 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
191 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
192 AV_PIX_FMT_YUV440P10,
193 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
194 AV_PIX_FMT_YUV440P12,
195 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
196 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
197 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
198 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
199 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
200 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
201 AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
202 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
203 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
206 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
208 return AVERROR(ENOMEM);
209 return ff_set_common_formats(ctx, fmts_list);
212 static av_cold void uninit(AVFilterContext *ctx)
214 LenscorrectionCtx *rect = ctx->priv;
217 for (i = 0; i < FF_ARRAY_ELEMS(rect->correction); i++) {
218 av_freep(&rect->correction[i]);
222 static void calc_correction(AVFilterContext *ctx, int plane)
224 LenscorrectionCtx *rect = ctx->priv;
225 int w = rect->planewidth[plane];
226 int h = rect->planeheight[plane];
227 int xcenter = rect->cx * w;
228 int ycenter = rect->cy * h;
229 int k1 = rect->k1 * (1<<24);
230 int k2 = rect->k2 * (1<<24);
231 const int64_t r2inv = (4LL<<60) / (w * w + h * h);
233 for (int j = 0; j < h; j++) {
234 const int off_y = j - ycenter;
235 const int off_y2 = off_y * off_y;
236 for (int i = 0; i < w; i++) {
237 const int off_x = i - xcenter;
238 const int64_t r2 = ((off_x * off_x + off_y2) * r2inv + (1LL<<31)) >> 32;
239 const int64_t r4 = (r2 * r2 + (1<<27)) >> 28;
240 const int radius_mult = (r2 * k1 + r4 * k2 + (1LL<<27) + (1LL<<52))>>28;
241 rect->correction[plane][j * w + i] = radius_mult;
246 static int config_output(AVFilterLink *outlink)
248 AVFilterContext *ctx = outlink->src;
249 LenscorrectionCtx *rect = ctx->priv;
250 AVFilterLink *inlink = ctx->inputs[0];
251 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
252 int is_rgb = !!(pixdesc->flags & AV_PIX_FMT_FLAG_RGB);
256 ff_fill_rgba_map(rgba_map, inlink->format);
257 rect->depth = pixdesc->comp[0].depth;
258 factor = 1 << (rect->depth - 8);
259 rect->planeheight[1] = rect->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, pixdesc->log2_chroma_h);
260 rect->planeheight[0] = rect->planeheight[3] = inlink->h;
261 rect->planewidth[1] = rect->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, pixdesc->log2_chroma_w);
262 rect->planewidth[0] = rect->planewidth[3] = inlink->w;
263 rect->nb_planes = av_pix_fmt_count_planes(inlink->format);
264 rect->filter_slice = rect->depth <= 8 ? filter8_slice : filter16_slice;
265 if (rect->interpolation)
266 rect->filter_slice = rect->depth <= 8 ? filter8_slice_bilinear : filter16_slice_bilinear;
269 rect->fill_color[rgba_map[0]] = rect->fill_rgba[0] * factor;
270 rect->fill_color[rgba_map[1]] = rect->fill_rgba[1] * factor;
271 rect->fill_color[rgba_map[2]] = rect->fill_rgba[2] * factor;
272 rect->fill_color[rgba_map[3]] = rect->fill_rgba[3] * factor;
274 rect->fill_color[0] = RGB_TO_Y_BT709(rect->fill_rgba[0], rect->fill_rgba[1], rect->fill_rgba[2]) * factor;
275 rect->fill_color[1] = RGB_TO_U_BT709(rect->fill_rgba[0], rect->fill_rgba[1], rect->fill_rgba[2], 0) * factor;
276 rect->fill_color[2] = RGB_TO_V_BT709(rect->fill_rgba[0], rect->fill_rgba[1], rect->fill_rgba[2], 0) * factor;
277 rect->fill_color[3] = rect->fill_rgba[3] * factor;
280 for (int plane = 0; plane < rect->nb_planes; plane++) {
281 int w = rect->planewidth[plane];
282 int h = rect->planeheight[plane];
284 if (!rect->correction[plane])
285 rect->correction[plane] = av_malloc_array(w, h * sizeof(**rect->correction));
286 if (!rect->correction[plane])
287 return AVERROR(ENOMEM);
288 calc_correction(ctx, plane);
294 static int filter_slice(AVFilterContext *ctx, void *arg, int job,
297 LenscorrectionCtx *rect = ctx->priv;
299 for (int plane = 0; plane < rect->nb_planes; plane++)
300 rect->filter_slice(ctx, arg, job, nb_jobs, plane);
305 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
307 AVFilterContext *ctx = inlink->dst;
308 AVFilterLink *outlink = ctx->outputs[0];
309 LenscorrectionCtx *rect = ctx->priv;
310 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
315 return AVERROR(ENOMEM);
318 av_frame_copy_props(out, in);
320 td.in = in; td.out = out;
321 ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(rect->planeheight[1], ff_filter_get_nb_threads(ctx)));
324 return ff_filter_frame(outlink, out);
327 static int process_command(AVFilterContext *ctx,
334 int ret = ff_filter_process_command(ctx, cmd, arg, res, res_len, flags);
339 return config_output(ctx->outputs[0]);
342 static const AVFilterPad lenscorrection_inputs[] = {
345 .type = AVMEDIA_TYPE_VIDEO,
346 .filter_frame = filter_frame,
351 static const AVFilterPad lenscorrection_outputs[] = {
354 .type = AVMEDIA_TYPE_VIDEO,
355 .config_props = config_output,
360 const AVFilter ff_vf_lenscorrection = {
361 .name = "lenscorrection",
362 .description = NULL_IF_CONFIG_SMALL("Rectify the image by correcting for lens distortion."),
363 .priv_size = sizeof(LenscorrectionCtx),
364 .query_formats = query_formats,
365 .inputs = lenscorrection_inputs,
366 .outputs = lenscorrection_outputs,
367 .priv_class = &lenscorrection_class,
369 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
370 .process_command = process_command,