2 * Copyright (C) 2007 by Andrew Zabolotny (author of lensfun, from which this filter derives from)
3 * Copyright (C) 2018 Stephen Seo
5 * This file is part of FFmpeg.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program 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
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 * Lensfun filter, applies lens correction with parameters from the lensfun database
25 * @see https://lensfun.sourceforge.net/
31 #include "libavutil/avassert.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/opt.h"
34 #include "libswscale/swscale.h"
42 #define LANCZOS_RESOLUTION 256
46 GEOMETRY_DISTORTION = 0x2,
47 SUBPIXEL_DISTORTION = 0x4
50 enum InterpolationType {
56 typedef struct VignettingThreadData {
60 int pixel_composition;
62 } VignettingThreadData;
64 typedef struct DistortionCorrectionThreadData {
66 const float *distortion_coords;
67 const uint8_t *data_in;
69 int linesize_in, linesize_out;
70 const float *interpolation;
72 int interpolation_type;
73 } DistortionCorrectionThreadData;
75 typedef struct LensfunContext {
77 const char *make, *model, *lens_model;
85 int interpolation_type;
87 float *distortion_coords;
95 #define OFFSET(x) offsetof(LensfunContext, x)
96 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
97 static const AVOption lensfun_options[] = {
98 { "make", "set camera maker", OFFSET(make), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
99 { "model", "set camera model", OFFSET(model), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
100 { "lens_model", "set lens model", OFFSET(lens_model), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
101 { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=GEOMETRY_DISTORTION}, 0, VIGNETTING | GEOMETRY_DISTORTION | SUBPIXEL_DISTORTION, FLAGS, "mode" },
102 { "vignetting", "fix lens vignetting", 0, AV_OPT_TYPE_CONST, {.i64=VIGNETTING}, 0, 0, FLAGS, "mode" },
103 { "geometry", "correct geometry distortion", 0, AV_OPT_TYPE_CONST, {.i64=GEOMETRY_DISTORTION}, 0, 0, FLAGS, "mode" },
104 { "subpixel", "fix chromatic aberrations", 0, AV_OPT_TYPE_CONST, {.i64=SUBPIXEL_DISTORTION}, 0, 0, FLAGS, "mode" },
105 { "vig_geo", "fix lens vignetting and correct geometry distortion", 0, AV_OPT_TYPE_CONST, {.i64=VIGNETTING | GEOMETRY_DISTORTION}, 0, 0, FLAGS, "mode" },
106 { "vig_subpixel", "fix lens vignetting and chromatic aberrations", 0, AV_OPT_TYPE_CONST, {.i64=VIGNETTING | SUBPIXEL_DISTORTION}, 0, 0, FLAGS, "mode" },
107 { "distortion", "correct geometry distortion and chromatic aberrations", 0, AV_OPT_TYPE_CONST, {.i64=GEOMETRY_DISTORTION | SUBPIXEL_DISTORTION}, 0, 0, FLAGS, "mode" },
108 { "all", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VIGNETTING | GEOMETRY_DISTORTION | SUBPIXEL_DISTORTION}, 0, 0, FLAGS, "mode" },
109 { "focal_length", "focal length of video (zoom; constant for the duration of the use of this filter)", OFFSET(focal_length), AV_OPT_TYPE_FLOAT, {.dbl=18}, 0.0, DBL_MAX, FLAGS },
110 { "aperture", "aperture (constant for the duration of the use of this filter)", OFFSET(aperture), AV_OPT_TYPE_FLOAT, {.dbl=3.5}, 0.0, DBL_MAX, FLAGS },
111 { "focus_distance", "focus distance (constant for the duration of the use of this filter)", OFFSET(focus_distance), AV_OPT_TYPE_FLOAT, {.dbl=1000.0f}, 0.0, DBL_MAX, FLAGS },
112 { "scale", "scale factor applied after corrections (0.0 means automatic scaling)", OFFSET(scale), AV_OPT_TYPE_FLOAT, {.dbl=0.0}, 0.0, DBL_MAX, FLAGS },
113 { "target_geometry", "target geometry of the lens correction (only when geometry correction is enabled)", OFFSET(target_geometry), AV_OPT_TYPE_INT, {.i64=LF_RECTILINEAR}, 0, INT_MAX, FLAGS, "lens_geometry" },
114 { "rectilinear", "rectilinear lens (default)", 0, AV_OPT_TYPE_CONST, {.i64=LF_RECTILINEAR}, 0, 0, FLAGS, "lens_geometry" },
115 { "fisheye", "fisheye lens", 0, AV_OPT_TYPE_CONST, {.i64=LF_FISHEYE}, 0, 0, FLAGS, "lens_geometry" },
116 { "panoramic", "panoramic (cylindrical)", 0, AV_OPT_TYPE_CONST, {.i64=LF_PANORAMIC}, 0, 0, FLAGS, "lens_geometry" },
117 { "equirectangular", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=LF_EQUIRECTANGULAR}, 0, 0, FLAGS, "lens_geometry" },
118 { "fisheye_orthographic", "orthographic fisheye", 0, AV_OPT_TYPE_CONST, {.i64=LF_FISHEYE_ORTHOGRAPHIC}, 0, 0, FLAGS, "lens_geometry" },
119 { "fisheye_stereographic", "stereographic fisheye", 0, AV_OPT_TYPE_CONST, {.i64=LF_FISHEYE_STEREOGRAPHIC}, 0, 0, FLAGS, "lens_geometry" },
120 { "fisheye_equisolid", "equisolid fisheye", 0, AV_OPT_TYPE_CONST, {.i64=LF_FISHEYE_EQUISOLID}, 0, 0, FLAGS, "lens_geometry" },
121 { "fisheye_thoby", "fisheye as measured by thoby", 0, AV_OPT_TYPE_CONST, {.i64=LF_FISHEYE_THOBY}, 0, 0, FLAGS, "lens_geometry" },
122 { "reverse", "Does reverse correction (regular image to lens distorted)", OFFSET(reverse), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
123 { "interpolation", "Type of interpolation", OFFSET(interpolation_type), AV_OPT_TYPE_INT, {.i64=LINEAR}, 0, LANCZOS, FLAGS, "interpolation" },
124 { "nearest", NULL, 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interpolation" },
125 { "linear", NULL, 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "interpolation" },
126 { "lanczos", NULL, 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interpolation" },
130 AVFILTER_DEFINE_CLASS(lensfun);
132 static av_cold int init(AVFilterContext *ctx)
134 LensfunContext *lensfun = ctx->priv;
136 const lfCamera **cameras;
137 const lfLens **lenses;
139 if (!lensfun->make) {
140 av_log(ctx, AV_LOG_FATAL, "Option \"make\" not specified\n");
141 return AVERROR(EINVAL);
142 } else if (!lensfun->model) {
143 av_log(ctx, AV_LOG_FATAL, "Option \"model\" not specified\n");
144 return AVERROR(EINVAL);
145 } else if (!lensfun->lens_model) {
146 av_log(ctx, AV_LOG_FATAL, "Option \"lens_model\" not specified\n");
147 return AVERROR(EINVAL);
150 lensfun->lens = lf_lens_create();
151 lensfun->camera = lf_camera_create();
154 if (lf_db_load(db) != LF_NO_ERROR) {
156 av_log(ctx, AV_LOG_FATAL, "Failed to load lensfun database\n");
157 return AVERROR_INVALIDDATA;
160 cameras = lf_db_find_cameras(db, lensfun->make, lensfun->model);
161 if (cameras && *cameras) {
162 lf_camera_copy(lensfun->camera, *cameras);
163 av_log(ctx, AV_LOG_INFO, "Using camera %s\n", lensfun->camera->Model);
167 av_log(ctx, AV_LOG_FATAL, "Failed to find camera in lensfun database\n");
168 return AVERROR_INVALIDDATA;
172 lenses = lf_db_find_lenses(db, lensfun->camera, NULL, lensfun->lens_model, 0);
173 if (lenses && *lenses) {
174 lf_lens_copy(lensfun->lens, *lenses);
175 av_log(ctx, AV_LOG_INFO, "Using lens %s\n", lensfun->lens->Model);
179 av_log(ctx, AV_LOG_FATAL, "Failed to find lens in lensfun database\n");
180 return AVERROR_INVALIDDATA;
188 static int query_formats(AVFilterContext *ctx)
190 // Some of the functions provided by lensfun require pixels in RGB format
191 static const enum AVPixelFormat fmts[] = {AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE};
192 AVFilterFormats *fmts_list = ff_make_format_list(fmts);
193 return ff_set_common_formats(ctx, fmts_list);
196 static float lanczos_kernel(float x)
200 } else if (x > -2.0f && x < 2.0f) {
201 return (2.0f * sin(M_PI * x) * sin(M_PI / 2.0f * x)) / (M_PI * M_PI * x * x);
207 static int config_props(AVFilterLink *inlink)
209 AVFilterContext *ctx = inlink->dst;
210 LensfunContext *lensfun = ctx->priv;
214 if (!lensfun->modifier) {
215 if (lensfun->camera && lensfun->lens) {
216 lensfun->modifier = lf_modifier_create(lensfun->lens,
217 lensfun->focal_length,
218 lensfun->camera->CropFactor,
220 inlink->h, LF_PF_U8, lensfun->reverse);
221 if (lensfun->mode & VIGNETTING)
222 lf_modifier_enable_vignetting_correction(lensfun->modifier, lensfun->aperture, lensfun->focus_distance);
223 if (lensfun->mode & GEOMETRY_DISTORTION) {
224 lf_modifier_enable_distortion_correction(lensfun->modifier);
225 lf_modifier_enable_projection_transform(lensfun->modifier, lensfun->target_geometry);
226 lf_modifier_enable_scaling(lensfun->modifier, lensfun->scale);
228 if (lensfun->mode & SUBPIXEL_DISTORTION)
229 lf_modifier_enable_tca_correction(lensfun->modifier);
231 // lensfun->camera and lensfun->lens should have been initialized
236 if (!lensfun->distortion_coords) {
237 if (lensfun->mode & SUBPIXEL_DISTORTION) {
238 lensfun->distortion_coords = av_malloc_array(inlink->w * inlink->h, sizeof(float) * 2 * 3);
239 if (!lensfun->distortion_coords)
240 return AVERROR(ENOMEM);
241 if (lensfun->mode & GEOMETRY_DISTORTION) {
242 // apply both geometry and subpixel distortion
243 lf_modifier_apply_subpixel_geometry_distortion(lensfun->modifier,
245 inlink->w, inlink->h,
246 lensfun->distortion_coords);
248 // apply only subpixel distortion
249 lf_modifier_apply_subpixel_distortion(lensfun->modifier,
251 inlink->w, inlink->h,
252 lensfun->distortion_coords);
254 } else if (lensfun->mode & GEOMETRY_DISTORTION) {
255 lensfun->distortion_coords = av_malloc_array(inlink->w * inlink->h, sizeof(float) * 2);
256 if (!lensfun->distortion_coords)
257 return AVERROR(ENOMEM);
258 // apply only geometry distortion
259 lf_modifier_apply_geometry_distortion(lensfun->modifier,
261 inlink->w, inlink->h,
262 lensfun->distortion_coords);
266 if (!lensfun->interpolation)
267 if (lensfun->interpolation_type == LANCZOS) {
268 lensfun->interpolation = av_malloc_array(LANCZOS_RESOLUTION, sizeof(float) * 4);
269 if (!lensfun->interpolation)
270 return AVERROR(ENOMEM);
271 for (index = 0; index < 4 * LANCZOS_RESOLUTION; ++index) {
273 lensfun->interpolation[index] = 1.0f;
275 a = sqrtf((float)index / LANCZOS_RESOLUTION);
276 lensfun->interpolation[index] = lanczos_kernel(a);
284 static int vignetting_filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
286 const VignettingThreadData *thread_data = arg;
287 const int slice_start = thread_data->height * jobnr / nb_jobs;
288 const int slice_end = thread_data->height * (jobnr + 1) / nb_jobs;
290 lf_modifier_apply_color_modification(thread_data->modifier,
291 thread_data->data_in + slice_start * thread_data->linesize_in,
295 slice_end - slice_start,
296 thread_data->pixel_composition,
297 thread_data->linesize_in);
302 static float square(float x)
307 static int distortion_correction_filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
309 const DistortionCorrectionThreadData *thread_data = arg;
310 const int slice_start = thread_data->height * jobnr / nb_jobs;
311 const int slice_end = thread_data->height * (jobnr + 1) / nb_jobs;
313 int x, y, i, j, rgb_index;
314 float interpolated, new_x, new_y, d, norm;
315 int new_x_int, new_y_int;
316 for (y = slice_start; y < slice_end; ++y)
317 for (x = 0; x < thread_data->width; ++x)
318 for (rgb_index = 0; rgb_index < 3; ++rgb_index) {
319 if (thread_data->mode & SUBPIXEL_DISTORTION) {
320 // subpixel (and possibly geometry) distortion correction was applied, correct distortion
321 switch(thread_data->interpolation_type) {
323 new_x_int = thread_data->distortion_coords[x * 2 * 3 + y * thread_data->width * 2 * 3 + rgb_index * 2] + 0.5f;
324 new_y_int = thread_data->distortion_coords[x * 2 * 3 + y * thread_data->width * 2 * 3 + rgb_index * 2 + 1] + 0.5f;
325 if (new_x_int < 0 || new_x_int >= thread_data->width || new_y_int < 0 || new_y_int >= thread_data->height) {
326 thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = 0;
328 thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = thread_data->data_in[new_x_int * 3 + rgb_index + new_y_int * thread_data->linesize_in];
333 new_x = thread_data->distortion_coords[x * 2 * 3 + y * thread_data->width * 2 * 3 + rgb_index * 2];
335 new_y = thread_data->distortion_coords[x * 2 * 3 + y * thread_data->width * 2 * 3 + rgb_index * 2 + 1];
337 if (new_x_int < 0 || new_x_int + 1 >= thread_data->width || new_y_int < 0 || new_y_int + 1 >= thread_data->height) {
338 thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = 0;
340 thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] =
341 thread_data->data_in[ new_x_int * 3 + rgb_index + new_y_int * thread_data->linesize_in] * (new_x_int + 1 - new_x) * (new_y_int + 1 - new_y)
342 + thread_data->data_in[(new_x_int + 1) * 3 + rgb_index + new_y_int * thread_data->linesize_in] * (new_x - new_x_int) * (new_y_int + 1 - new_y)
343 + thread_data->data_in[ new_x_int * 3 + rgb_index + (new_y_int + 1) * thread_data->linesize_in] * (new_x_int + 1 - new_x) * (new_y - new_y_int)
344 + thread_data->data_in[(new_x_int + 1) * 3 + rgb_index + (new_y_int + 1) * thread_data->linesize_in] * (new_x - new_x_int) * (new_y - new_y_int);
350 new_x = thread_data->distortion_coords[x * 2 * 3 + y * thread_data->width * 2 * 3 + rgb_index * 2];
352 new_y = thread_data->distortion_coords[x * 2 * 3 + y * thread_data->width * 2 * 3 + rgb_index * 2 + 1];
354 for (j = 0; j < 4; ++j)
355 for (i = 0; i < 4; ++i) {
356 if (new_x_int + i - 2 < 0 || new_x_int + i - 2 >= thread_data->width || new_y_int + j - 2 < 0 || new_y_int + j - 2 >= thread_data->height)
358 d = square(new_x - (new_x_int + i - 2)) * square(new_y - (new_y_int + j - 2));
361 d = thread_data->interpolation[(int)(d * LANCZOS_RESOLUTION)];
363 interpolated += thread_data->data_in[(new_x_int + i - 2) * 3 + rgb_index + (new_y_int + j - 2) * thread_data->linesize_in] * d;
366 thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = 0;
368 interpolated /= norm;
369 thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = interpolated < 0.0f ? 0.0f : interpolated > 255.0f ? 255.0f : interpolated;
373 } else if (thread_data->mode & GEOMETRY_DISTORTION) {
374 // geometry distortion correction was applied, correct distortion
375 switch(thread_data->interpolation_type) {
377 new_x_int = thread_data->distortion_coords[x * 2 + y * thread_data->width * 2] + 0.5f;
378 new_y_int = thread_data->distortion_coords[x * 2 + y * thread_data->width * 2 + 1] + 0.5f;
379 if (new_x_int < 0 || new_x_int >= thread_data->width || new_y_int < 0 || new_y_int >= thread_data->height) {
380 thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = 0;
382 thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = thread_data->data_in[new_x_int * 3 + rgb_index + new_y_int * thread_data->linesize_in];
387 new_x = thread_data->distortion_coords[x * 2 + y * thread_data->width * 2];
389 new_y = thread_data->distortion_coords[x * 2 + y * thread_data->width * 2 + 1];
391 if (new_x_int < 0 || new_x_int + 1 >= thread_data->width || new_y_int < 0 || new_y_int + 1 >= thread_data->height) {
392 thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = 0;
394 thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] =
395 thread_data->data_in[ new_x_int * 3 + rgb_index + new_y_int * thread_data->linesize_in] * (new_x_int + 1 - new_x) * (new_y_int + 1 - new_y)
396 + thread_data->data_in[(new_x_int + 1) * 3 + rgb_index + new_y_int * thread_data->linesize_in] * (new_x - new_x_int) * (new_y_int + 1 - new_y)
397 + thread_data->data_in[ new_x_int * 3 + rgb_index + (new_y_int + 1) * thread_data->linesize_in] * (new_x_int + 1 - new_x) * (new_y - new_y_int)
398 + thread_data->data_in[(new_x_int + 1) * 3 + rgb_index + (new_y_int + 1) * thread_data->linesize_in] * (new_x - new_x_int) * (new_y - new_y_int);
404 new_x = thread_data->distortion_coords[x * 2 + y * thread_data->width * 2];
406 new_y = thread_data->distortion_coords[x * 2 + 1 + y * thread_data->width * 2];
408 for (j = 0; j < 4; ++j)
409 for (i = 0; i < 4; ++i) {
410 if (new_x_int + i - 2 < 0 || new_x_int + i - 2 >= thread_data->width || new_y_int + j - 2 < 0 || new_y_int + j - 2 >= thread_data->height)
412 d = square(new_x - (new_x_int + i - 2)) * square(new_y - (new_y_int + j - 2));
415 d = thread_data->interpolation[(int)(d * LANCZOS_RESOLUTION)];
417 interpolated += thread_data->data_in[(new_x_int + i - 2) * 3 + rgb_index + (new_y_int + j - 2) * thread_data->linesize_in] * d;
420 thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = 0;
422 interpolated /= norm;
423 thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = interpolated < 0.0f ? 0.0f : interpolated > 255.0f ? 255.0f : interpolated;
428 // no distortion correction was applied
429 thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = thread_data->data_in[x * 3 + rgb_index + y * thread_data->linesize_in];
436 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
438 AVFilterContext *ctx = inlink->dst;
439 LensfunContext *lensfun = ctx->priv;
440 AVFilterLink *outlink = ctx->outputs[0];
442 VignettingThreadData vignetting_thread_data;
443 DistortionCorrectionThreadData distortion_correction_thread_data;
445 if (lensfun->mode & VIGNETTING) {
446 av_frame_make_writable(in);
448 vignetting_thread_data = (VignettingThreadData) {
451 .data_in = in->data[0],
452 .linesize_in = in->linesize[0],
453 .pixel_composition = LF_CR_3(RED, GREEN, BLUE),
454 .modifier = lensfun->modifier
457 ctx->internal->execute(ctx,
458 vignetting_filter_slice,
459 &vignetting_thread_data,
461 FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
464 if (lensfun->mode & (GEOMETRY_DISTORTION | SUBPIXEL_DISTORTION)) {
465 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
468 return AVERROR(ENOMEM);
470 av_frame_copy_props(out, in);
472 distortion_correction_thread_data = (DistortionCorrectionThreadData) {
475 .distortion_coords = lensfun->distortion_coords,
476 .data_in = in->data[0],
477 .data_out = out->data[0],
478 .linesize_in = in->linesize[0],
479 .linesize_out = out->linesize[0],
480 .interpolation = lensfun->interpolation,
481 .mode = lensfun->mode,
482 .interpolation_type = lensfun->interpolation_type
485 ctx->internal->execute(ctx,
486 distortion_correction_filter_slice,
487 &distortion_correction_thread_data,
489 FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
492 return ff_filter_frame(outlink, out);
494 return ff_filter_frame(outlink, in);
498 static av_cold void uninit(AVFilterContext *ctx)
500 LensfunContext *lensfun = ctx->priv;
503 lf_camera_destroy(lensfun->camera);
505 lf_lens_destroy(lensfun->lens);
506 if (lensfun->modifier)
507 lf_modifier_destroy(lensfun->modifier);
508 av_freep(&lensfun->distortion_coords);
509 av_freep(&lensfun->interpolation);
512 static const AVFilterPad lensfun_inputs[] = {
515 .type = AVMEDIA_TYPE_VIDEO,
516 .config_props = config_props,
517 .filter_frame = filter_frame,
522 static const AVFilterPad lensfun_outputs[] = {
525 .type = AVMEDIA_TYPE_VIDEO,
530 AVFilter ff_vf_lensfun = {
532 .description = NULL_IF_CONFIG_SMALL("Apply correction to an image based on info derived from the lensfun database."),
533 .priv_size = sizeof(LensfunContext),
536 .query_formats = query_formats,
537 .inputs = lensfun_inputs,
538 .outputs = lensfun_outputs,
539 .priv_class = &lensfun_class,
540 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,