FFmpeg
vf_lensfun.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 by Andrew Zabolotny (author of lensfun, from which this filter derives from)
3  * Copyright (C) 2018 Stephen Seo
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20 
21 /**
22  * @file
23  * Lensfun filter, applies lens correction with parameters from the lensfun database
24  *
25  * @see https://lensfun.sourceforge.net/
26  */
27 
28 #include <float.h>
29 #include <math.h>
30 
31 #include "libavutil/imgutils.h"
32 #include "libavutil/opt.h"
33 #include "libswscale/swscale.h"
34 #include "avfilter.h"
35 #include "formats.h"
36 #include "internal.h"
37 #include "video.h"
38 
39 #include <lensfun.h>
40 
41 #define LANCZOS_RESOLUTION 256
42 
43 enum Mode {
44  VIGNETTING = 0x1,
47 };
48 
53 };
54 
55 typedef struct VignettingThreadData {
56  int width, height;
57  uint8_t *data_in;
60  lfModifier *modifier;
62 
64  int width, height;
65  const float *distortion_coords;
66  const uint8_t *data_in;
67  uint8_t *data_out;
69  const float *interpolation;
70  int mode;
73 
74 typedef struct LensfunContext {
75  const AVClass *class;
76  const char *make, *model, *lens_model;
77  int mode;
78  float focal_length;
79  float aperture;
81  float scale;
83  int reverse;
85 
87  float *interpolation;
88 
89  lfLens *lens;
90  lfCamera *camera;
91  lfModifier *modifier;
93 
94 #define OFFSET(x) offsetof(LensfunContext, x)
95 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
96 static const AVOption lensfun_options[] = {
97  { "make", "set camera maker", OFFSET(make), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
98  { "model", "set camera model", OFFSET(model), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
99  { "lens_model", "set lens model", OFFSET(lens_model), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
100  { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=GEOMETRY_DISTORTION}, 0, VIGNETTING | GEOMETRY_DISTORTION | SUBPIXEL_DISTORTION, FLAGS, "mode" },
101  { "vignetting", "fix lens vignetting", 0, AV_OPT_TYPE_CONST, {.i64=VIGNETTING}, 0, 0, FLAGS, "mode" },
102  { "geometry", "correct geometry distortion", 0, AV_OPT_TYPE_CONST, {.i64=GEOMETRY_DISTORTION}, 0, 0, FLAGS, "mode" },
103  { "subpixel", "fix chromatic aberrations", 0, AV_OPT_TYPE_CONST, {.i64=SUBPIXEL_DISTORTION}, 0, 0, FLAGS, "mode" },
104  { "vig_geo", "fix lens vignetting and correct geometry distortion", 0, AV_OPT_TYPE_CONST, {.i64=VIGNETTING | GEOMETRY_DISTORTION}, 0, 0, FLAGS, "mode" },
105  { "vig_subpixel", "fix lens vignetting and chromatic aberrations", 0, AV_OPT_TYPE_CONST, {.i64=VIGNETTING | SUBPIXEL_DISTORTION}, 0, 0, FLAGS, "mode" },
106  { "distortion", "correct geometry distortion and chromatic aberrations", 0, AV_OPT_TYPE_CONST, {.i64=GEOMETRY_DISTORTION | SUBPIXEL_DISTORTION}, 0, 0, FLAGS, "mode" },
107  { "all", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VIGNETTING | GEOMETRY_DISTORTION | SUBPIXEL_DISTORTION}, 0, 0, FLAGS, "mode" },
108  { "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 },
109  { "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 },
110  { "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 },
111  { "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 },
112  { "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" },
113  { "rectilinear", "rectilinear lens (default)", 0, AV_OPT_TYPE_CONST, {.i64=LF_RECTILINEAR}, 0, 0, FLAGS, "lens_geometry" },
114  { "fisheye", "fisheye lens", 0, AV_OPT_TYPE_CONST, {.i64=LF_FISHEYE}, 0, 0, FLAGS, "lens_geometry" },
115  { "panoramic", "panoramic (cylindrical)", 0, AV_OPT_TYPE_CONST, {.i64=LF_PANORAMIC}, 0, 0, FLAGS, "lens_geometry" },
116  { "equirectangular", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=LF_EQUIRECTANGULAR}, 0, 0, FLAGS, "lens_geometry" },
117  { "fisheye_orthographic", "orthographic fisheye", 0, AV_OPT_TYPE_CONST, {.i64=LF_FISHEYE_ORTHOGRAPHIC}, 0, 0, FLAGS, "lens_geometry" },
118  { "fisheye_stereographic", "stereographic fisheye", 0, AV_OPT_TYPE_CONST, {.i64=LF_FISHEYE_STEREOGRAPHIC}, 0, 0, FLAGS, "lens_geometry" },
119  { "fisheye_equisolid", "equisolid fisheye", 0, AV_OPT_TYPE_CONST, {.i64=LF_FISHEYE_EQUISOLID}, 0, 0, FLAGS, "lens_geometry" },
120  { "fisheye_thoby", "fisheye as measured by thoby", 0, AV_OPT_TYPE_CONST, {.i64=LF_FISHEYE_THOBY}, 0, 0, FLAGS, "lens_geometry" },
121  { "reverse", "Does reverse correction (regular image to lens distorted)", OFFSET(reverse), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
122  { "interpolation", "Type of interpolation", OFFSET(interpolation_type), AV_OPT_TYPE_INT, {.i64=LINEAR}, 0, LANCZOS, FLAGS, "interpolation" },
123  { "nearest", NULL, 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interpolation" },
124  { "linear", NULL, 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "interpolation" },
125  { "lanczos", NULL, 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interpolation" },
126  { NULL }
127 };
128 
129 AVFILTER_DEFINE_CLASS(lensfun);
130 
132 {
133  LensfunContext *lensfun = ctx->priv;
134  lfDatabase *db;
135  const lfCamera **cameras;
136  const lfLens **lenses;
137 
138  db = lf_db_create();
139  if (lf_db_load(db) != LF_NO_ERROR) {
140  lf_db_destroy(db);
141  av_log(ctx, AV_LOG_FATAL, "Failed to load lensfun database\n");
142  return AVERROR_INVALIDDATA;
143  }
144 
145  if (!lensfun->make || !lensfun->model) {
146  const lfCamera *const *cameras = lf_db_get_cameras(db);
147 
148  av_log(ctx, AV_LOG_FATAL, "Option \"make\" or option \"model\" not specified\n");
149  av_log(ctx, AV_LOG_INFO, "Available values for \"make\" and \"model\":\n");
150  for (int i = 0; cameras && cameras[i]; i++)
151  av_log(ctx, AV_LOG_INFO, "\t%s\t%s\n", cameras[i]->Maker, cameras[i]->Model);
152  lf_db_destroy(db);
153  return AVERROR(EINVAL);
154  } else if (!lensfun->lens_model) {
155  const lfLens *const *lenses = lf_db_get_lenses(db);
156 
157  av_log(ctx, AV_LOG_FATAL, "Option \"lens_model\" not specified\n");
158  av_log(ctx, AV_LOG_INFO, "Available values for \"lens_model\":\n");
159  for (int i = 0; lenses && lenses[i]; i++)
160  av_log(ctx, AV_LOG_INFO, "\t%s\t(make %s)\n", lenses[i]->Model, lenses[i]->Maker);
161  lf_db_destroy(db);
162  return AVERROR(EINVAL);
163  }
164 
165  lensfun->lens = lf_lens_create();
166  lensfun->camera = lf_camera_create();
167 
168  cameras = lf_db_find_cameras(db, lensfun->make, lensfun->model);
169  if (cameras && *cameras) {
170  lf_camera_copy(lensfun->camera, *cameras);
171  av_log(ctx, AV_LOG_INFO, "Using camera %s\n", lensfun->camera->Model);
172  } else {
173  lf_free(cameras);
174  lf_db_destroy(db);
175  av_log(ctx, AV_LOG_FATAL, "Failed to find camera in lensfun database\n");
176  return AVERROR_INVALIDDATA;
177  }
178  lf_free(cameras);
179 
180  lenses = lf_db_find_lenses(db, lensfun->camera, NULL, lensfun->lens_model, 0);
181  if (lenses && *lenses) {
182  lf_lens_copy(lensfun->lens, *lenses);
183  av_log(ctx, AV_LOG_INFO, "Using lens %s\n", lensfun->lens->Model);
184  } else {
185  lf_free(lenses);
186  lf_db_destroy(db);
187  av_log(ctx, AV_LOG_FATAL, "Failed to find lens in lensfun database\n");
188  return AVERROR_INVALIDDATA;
189  }
190  lf_free(lenses);
191 
192  lf_db_destroy(db);
193  return 0;
194 }
195 
197 {
198  // Some of the functions provided by lensfun require pixels in RGB format
199  static const enum AVPixelFormat fmts[] = {AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE};
200  return ff_set_common_formats_from_list(ctx, fmts);
201 }
202 
203 static float lanczos_kernel(float x)
204 {
205  if (x == 0.0f) {
206  return 1.0f;
207  } else if (x > -2.0f && x < 2.0f) {
208  return (2.0f * sin(M_PI * x) * sin(M_PI / 2.0f * x)) / (M_PI * M_PI * x * x);
209  } else {
210  return 0.0f;
211  }
212 }
213 
215 {
216  AVFilterContext *ctx = inlink->dst;
217  LensfunContext *lensfun = ctx->priv;
218  int index;
219  float a;
220 
221  if (!lensfun->modifier) {
222  if (lensfun->camera && lensfun->lens) {
223  lensfun->modifier = lf_modifier_create(lensfun->lens,
224  lensfun->focal_length,
225  lensfun->camera->CropFactor,
226  inlink->w,
227  inlink->h, LF_PF_U8, lensfun->reverse);
228  if (lensfun->mode & VIGNETTING)
229  lf_modifier_enable_vignetting_correction(lensfun->modifier, lensfun->aperture, lensfun->focus_distance);
230  if (lensfun->mode & GEOMETRY_DISTORTION) {
231  lf_modifier_enable_distortion_correction(lensfun->modifier);
232  lf_modifier_enable_projection_transform(lensfun->modifier, lensfun->target_geometry);
233  lf_modifier_enable_scaling(lensfun->modifier, lensfun->scale);
234  }
235  if (lensfun->mode & SUBPIXEL_DISTORTION)
236  lf_modifier_enable_tca_correction(lensfun->modifier);
237  } else {
238  // lensfun->camera and lensfun->lens should have been initialized
239  return AVERROR_BUG;
240  }
241  }
242 
243  if (!lensfun->distortion_coords) {
244  if (lensfun->mode & SUBPIXEL_DISTORTION) {
245  lensfun->distortion_coords = av_malloc_array(inlink->w * inlink->h, sizeof(float) * 2 * 3);
246  if (!lensfun->distortion_coords)
247  return AVERROR(ENOMEM);
248  if (lensfun->mode & GEOMETRY_DISTORTION) {
249  // apply both geometry and subpixel distortion
250  lf_modifier_apply_subpixel_geometry_distortion(lensfun->modifier,
251  0, 0,
252  inlink->w, inlink->h,
253  lensfun->distortion_coords);
254  } else {
255  // apply only subpixel distortion
256  lf_modifier_apply_subpixel_distortion(lensfun->modifier,
257  0, 0,
258  inlink->w, inlink->h,
259  lensfun->distortion_coords);
260  }
261  } else if (lensfun->mode & GEOMETRY_DISTORTION) {
262  lensfun->distortion_coords = av_malloc_array(inlink->w * inlink->h, sizeof(float) * 2);
263  if (!lensfun->distortion_coords)
264  return AVERROR(ENOMEM);
265  // apply only geometry distortion
266  lf_modifier_apply_geometry_distortion(lensfun->modifier,
267  0, 0,
268  inlink->w, inlink->h,
269  lensfun->distortion_coords);
270  }
271  }
272 
273  if (!lensfun->interpolation)
274  if (lensfun->interpolation_type == LANCZOS) {
275  lensfun->interpolation = av_malloc_array(LANCZOS_RESOLUTION, sizeof(float) * 4);
276  if (!lensfun->interpolation)
277  return AVERROR(ENOMEM);
278  for (index = 0; index < 4 * LANCZOS_RESOLUTION; ++index) {
279  if (index == 0) {
280  lensfun->interpolation[index] = 1.0f;
281  } else {
282  a = sqrtf((float)index / LANCZOS_RESOLUTION);
283  lensfun->interpolation[index] = lanczos_kernel(a);
284  }
285  }
286  }
287 
288  return 0;
289 }
290 
291 static int vignetting_filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
292 {
294  const int slice_start = thread_data->height * jobnr / nb_jobs;
295  const int slice_end = thread_data->height * (jobnr + 1) / nb_jobs;
296 
297  lf_modifier_apply_color_modification(thread_data->modifier,
298  thread_data->data_in + slice_start * thread_data->linesize_in,
299  0,
300  slice_start,
301  thread_data->width,
302  slice_end - slice_start,
303  thread_data->pixel_composition,
304  thread_data->linesize_in);
305 
306  return 0;
307 }
308 
309 static float square(float x)
310 {
311  return x * x;
312 }
313 
314 static int distortion_correction_filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
315 {
317  const int slice_start = thread_data->height * jobnr / nb_jobs;
318  const int slice_end = thread_data->height * (jobnr + 1) / nb_jobs;
319 
320  int x, y, i, j, rgb_index;
321  float interpolated, new_x, new_y, d, norm;
322  int new_x_int, new_y_int;
323  for (y = slice_start; y < slice_end; ++y)
324  for (x = 0; x < thread_data->width; ++x)
325  for (rgb_index = 0; rgb_index < 3; ++rgb_index) {
326  if (thread_data->mode & SUBPIXEL_DISTORTION) {
327  // subpixel (and possibly geometry) distortion correction was applied, correct distortion
328  switch(thread_data->interpolation_type) {
329  case NEAREST:
330  new_x_int = thread_data->distortion_coords[x * 2 * 3 + y * thread_data->width * 2 * 3 + rgb_index * 2] + 0.5f;
331  new_y_int = thread_data->distortion_coords[x * 2 * 3 + y * thread_data->width * 2 * 3 + rgb_index * 2 + 1] + 0.5f;
332  if (new_x_int < 0 || new_x_int >= thread_data->width || new_y_int < 0 || new_y_int >= thread_data->height) {
333  thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = 0;
334  } else {
335  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];
336  }
337  break;
338  case LINEAR:
339  interpolated = 0.0f;
340  new_x = thread_data->distortion_coords[x * 2 * 3 + y * thread_data->width * 2 * 3 + rgb_index * 2];
341  new_x_int = new_x;
342  new_y = thread_data->distortion_coords[x * 2 * 3 + y * thread_data->width * 2 * 3 + rgb_index * 2 + 1];
343  new_y_int = new_y;
344  if (new_x_int < 0 || new_x_int + 1 >= thread_data->width || new_y_int < 0 || new_y_int + 1 >= thread_data->height) {
345  thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = 0;
346  } else {
347  thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] =
348  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)
349  + 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)
350  + 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)
351  + 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);
352  }
353  break;
354  case LANCZOS:
355  interpolated = 0.0f;
356  norm = 0.0f;
357  new_x = thread_data->distortion_coords[x * 2 * 3 + y * thread_data->width * 2 * 3 + rgb_index * 2];
358  new_x_int = new_x;
359  new_y = thread_data->distortion_coords[x * 2 * 3 + y * thread_data->width * 2 * 3 + rgb_index * 2 + 1];
360  new_y_int = new_y;
361  for (j = 0; j < 4; ++j)
362  for (i = 0; i < 4; ++i) {
363  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)
364  continue;
365  d = square(new_x - (new_x_int + i - 2)) * square(new_y - (new_y_int + j - 2));
366  if (d >= 4.0f)
367  continue;
368  d = thread_data->interpolation[(int)(d * LANCZOS_RESOLUTION)];
369  norm += d;
370  interpolated += thread_data->data_in[(new_x_int + i - 2) * 3 + rgb_index + (new_y_int + j - 2) * thread_data->linesize_in] * d;
371  }
372  if (norm == 0.0f) {
373  thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = 0;
374  } else {
375  interpolated /= norm;
376  thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = interpolated < 0.0f ? 0.0f : interpolated > 255.0f ? 255.0f : interpolated;
377  }
378  break;
379  }
380  } else if (thread_data->mode & GEOMETRY_DISTORTION) {
381  // geometry distortion correction was applied, correct distortion
382  switch(thread_data->interpolation_type) {
383  case NEAREST:
384  new_x_int = thread_data->distortion_coords[x * 2 + y * thread_data->width * 2] + 0.5f;
385  new_y_int = thread_data->distortion_coords[x * 2 + y * thread_data->width * 2 + 1] + 0.5f;
386  if (new_x_int < 0 || new_x_int >= thread_data->width || new_y_int < 0 || new_y_int >= thread_data->height) {
387  thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = 0;
388  } else {
389  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];
390  }
391  break;
392  case LINEAR:
393  interpolated = 0.0f;
394  new_x = thread_data->distortion_coords[x * 2 + y * thread_data->width * 2];
395  new_x_int = new_x;
396  new_y = thread_data->distortion_coords[x * 2 + y * thread_data->width * 2 + 1];
397  new_y_int = new_y;
398  if (new_x_int < 0 || new_x_int + 1 >= thread_data->width || new_y_int < 0 || new_y_int + 1 >= thread_data->height) {
399  thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = 0;
400  } else {
401  thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] =
402  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)
403  + 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)
404  + 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)
405  + 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);
406  }
407  break;
408  case LANCZOS:
409  interpolated = 0.0f;
410  norm = 0.0f;
411  new_x = thread_data->distortion_coords[x * 2 + y * thread_data->width * 2];
412  new_x_int = new_x;
413  new_y = thread_data->distortion_coords[x * 2 + 1 + y * thread_data->width * 2];
414  new_y_int = new_y;
415  for (j = 0; j < 4; ++j)
416  for (i = 0; i < 4; ++i) {
417  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)
418  continue;
419  d = square(new_x - (new_x_int + i - 2)) * square(new_y - (new_y_int + j - 2));
420  if (d >= 4.0f)
421  continue;
422  d = thread_data->interpolation[(int)(d * LANCZOS_RESOLUTION)];
423  norm += d;
424  interpolated += thread_data->data_in[(new_x_int + i - 2) * 3 + rgb_index + (new_y_int + j - 2) * thread_data->linesize_in] * d;
425  }
426  if (norm == 0.0f) {
427  thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = 0;
428  } else {
429  interpolated /= norm;
430  thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = interpolated < 0.0f ? 0.0f : interpolated > 255.0f ? 255.0f : interpolated;
431  }
432  break;
433  }
434  } else {
435  // no distortion correction was applied
436  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];
437  }
438  }
439 
440  return 0;
441 }
442 
444 {
445  AVFilterContext *ctx = inlink->dst;
446  LensfunContext *lensfun = ctx->priv;
447  AVFilterLink *outlink = ctx->outputs[0];
448  AVFrame *out;
449  VignettingThreadData vignetting_thread_data;
450  DistortionCorrectionThreadData distortion_correction_thread_data;
451 
452  if (lensfun->mode & VIGNETTING) {
454 
455  vignetting_thread_data = (VignettingThreadData) {
456  .width = inlink->w,
457  .height = inlink->h,
458  .data_in = in->data[0],
459  .linesize_in = in->linesize[0],
460  .pixel_composition = LF_CR_3(RED, GREEN, BLUE),
461  .modifier = lensfun->modifier
462  };
463 
465  &vignetting_thread_data, NULL,
466  FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
467  }
468 
469  if (lensfun->mode & (GEOMETRY_DISTORTION | SUBPIXEL_DISTORTION)) {
470  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
471  if (!out) {
472  av_frame_free(&in);
473  return AVERROR(ENOMEM);
474  }
476 
477  distortion_correction_thread_data = (DistortionCorrectionThreadData) {
478  .width = inlink->w,
479  .height = inlink->h,
480  .distortion_coords = lensfun->distortion_coords,
481  .data_in = in->data[0],
482  .data_out = out->data[0],
483  .linesize_in = in->linesize[0],
484  .linesize_out = out->linesize[0],
485  .interpolation = lensfun->interpolation,
486  .mode = lensfun->mode,
487  .interpolation_type = lensfun->interpolation_type
488  };
489 
491  &distortion_correction_thread_data, NULL,
492  FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
493 
494  av_frame_free(&in);
495  return ff_filter_frame(outlink, out);
496  } else {
497  return ff_filter_frame(outlink, in);
498  }
499 }
500 
502 {
503  LensfunContext *lensfun = ctx->priv;
504 
505  if (lensfun->camera)
506  lf_camera_destroy(lensfun->camera);
507  if (lensfun->lens)
508  lf_lens_destroy(lensfun->lens);
509  if (lensfun->modifier)
510  lf_modifier_destroy(lensfun->modifier);
511  av_freep(&lensfun->distortion_coords);
512  av_freep(&lensfun->interpolation);
513 }
514 
515 static const AVFilterPad lensfun_inputs[] = {
516  {
517  .name = "default",
518  .type = AVMEDIA_TYPE_VIDEO,
519  .config_props = config_props,
520  .filter_frame = filter_frame,
521  },
522 };
523 
524 static const AVFilterPad lensfun_outputs[] = {
525  {
526  .name = "default",
527  .type = AVMEDIA_TYPE_VIDEO,
528  },
529 };
530 
532  .name = "lensfun",
533  .description = NULL_IF_CONFIG_SMALL("Apply correction to an image based on info derived from the lensfun database."),
534  .priv_size = sizeof(LensfunContext),
535  .init = init,
536  .uninit = uninit,
540  .priv_class = &lensfun_class,
542 };
LensfunContext::focal_length
float focal_length
Definition: vf_lensfun.c:78
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:98
distortion_correction_filter_slice
static int distortion_correction_filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_lensfun.c:314
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
RED
@ RED
Definition: rpzaenc.c:54
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
LINEAR
@ LINEAR
Definition: vf_lensfun.c:51
out
FILE * out
Definition: movenc.c:54
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1019
lanczos_kernel
static float lanczos_kernel(float x)
Definition: vf_lensfun.c:203
LensfunContext
Definition: vf_lensfun.c:74
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:112
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
av_frame_make_writable
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
Definition: frame.c:490
index
fg index
Definition: ffmpeg_filter.c:168
LensfunContext::model
const char * model
Definition: vf_lensfun.c:76
AVOption
AVOption.
Definition: opt.h:247
DistortionCorrectionThreadData
Definition: vf_lensfun.c:63
float.h
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:153
LensfunContext::reverse
int reverse
Definition: vf_lensfun.c:83
LensfunContext::camera
lfCamera * camera
Definition: vf_lensfun.c:90
DistortionCorrectionThreadData::data_out
uint8_t * data_out
Definition: vf_lensfun.c:67
video.h
GREEN
@ GREEN
Definition: rpzaenc.c:55
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:317
formats.h
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_lensfun.c:501
VignettingThreadData::data_in
uint8_t * data_in
Definition: vf_lensfun.c:57
LensfunContext::interpolation
float * interpolation
Definition: vf_lensfun.c:87
OFFSET
#define OFFSET(x)
Definition: vf_lensfun.c:94
DistortionCorrectionThreadData::height
int height
Definition: vf_lensfun.c:64
DistortionCorrectionThreadData::distortion_coords
const float * distortion_coords
Definition: vf_lensfun.c:65
scale
static av_always_inline float scale(float x, float s)
Definition: vf_v360.c:1376
lensfun_inputs
static const AVFilterPad lensfun_inputs[]
Definition: vf_lensfun.c:515
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
VignettingThreadData::pixel_composition
int pixel_composition
Definition: vf_lensfun.c:59
av_cold
#define av_cold
Definition: attributes.h:90
thread_data
Definition: vf_lut.c:337
Mode
Mode
Frame type (Table 1a in 3GPP TS 26.101)
Definition: amrnbdata.h:39
VignettingThreadData::modifier
lfModifier * modifier
Definition: vf_lensfun.c:60
square
static float square(float x)
Definition: vf_lensfun.c:309
slice_end
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2041
ff_set_common_formats_from_list
int ff_set_common_formats_from_list(AVFilterContext *ctx, const int *fmts)
Equivalent to ff_set_common_formats(ctx, ff_make_format_list(fmts))
Definition: formats.c:703
VignettingThreadData::width
int width
Definition: vf_lensfun.c:56
DistortionCorrectionThreadData::mode
int mode
Definition: vf_lensfun.c:70
ctx
AVFormatContext * ctx
Definition: movenc.c:48
f
#define f(width, name)
Definition: cbs_vp9.c:255
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:152
VignettingThreadData::linesize_in
int linesize_in
Definition: vf_lensfun.c:58
arg
const char * arg
Definition: jacosubdec.c:67
BLUE
@ BLUE
Definition: rpzaenc.c:56
LensfunContext::focus_distance
float focus_distance
Definition: vf_lensfun.c:80
DistortionCorrectionThreadData::linesize_out
int linesize_out
Definition: vf_lensfun.c:68
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
lensfun_outputs
static const AVFilterPad lensfun_outputs[]
Definition: vf_lensfun.c:524
DistortionCorrectionThreadData::linesize_in
int linesize_in
Definition: vf_lensfun.c:68
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:537
SUBPIXEL_DISTORTION
@ SUBPIXEL_DISTORTION
Definition: vf_lensfun.c:46
lensfun_options
static const AVOption lensfun_options[]
Definition: vf_lensfun.c:96
vignetting_filter_slice
static int vignetting_filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_lensfun.c:291
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_lensfun.c:443
LensfunContext::distortion_coords
float * distortion_coords
Definition: vf_lensfun.c:86
VignettingThreadData::height
int height
Definition: vf_lensfun.c:56
DistortionCorrectionThreadData::interpolation
const float * interpolation
Definition: vf_lensfun.c:69
LensfunContext::interpolation_type
int interpolation_type
Definition: vf_lensfun.c:84
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
LensfunContext::target_geometry
int target_geometry
Definition: vf_lensfun.c:82
LANCZOS
@ LANCZOS
Definition: vf_lensfun.c:52
LANCZOS_RESOLUTION
#define LANCZOS_RESOLUTION
Definition: vf_lensfun.c:41
FLAGS
#define FLAGS
Definition: vf_lensfun.c:95
NEAREST
@ NEAREST
Definition: vf_lensfun.c:50
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
M_PI
#define M_PI
Definition: mathematics.h:52
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
internal.h
config_props
static int config_props(AVFilterLink *inlink)
Definition: vf_lensfun.c:214
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:130
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:227
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_lensfun.c:196
i
int i
Definition: input.c:406
LensfunContext::aperture
float aperture
Definition: vf_lensfun.c:79
LensfunContext::make
const char * make
Definition: vf_lensfun.c:76
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:804
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
DistortionCorrectionThreadData::data_in
const uint8_t * data_in
Definition: vf_lensfun.c:66
AVFilter
Filter definition.
Definition: avfilter.h:149
AV_LOG_FATAL
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:174
LensfunContext::mode
int mode
Definition: vf_lensfun.c:77
InterpolationType
InterpolationType
Definition: vf_lensfun.c:49
LensfunContext::lens_model
const char * lens_model
Definition: vf_lensfun.c:76
GEOMETRY_DISTORTION
@ GEOMETRY_DISTORTION
Definition: vf_lensfun.c:45
DistortionCorrectionThreadData::interpolation_type
int interpolation_type
Definition: vf_lensfun.c:71
mode
mode
Definition: ebur128.h:83
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
avfilter.h
LensfunContext::scale
float scale
Definition: vf_lensfun.c:81
VignettingThreadData
Definition: vf_lensfun.c:55
LensfunContext::modifier
lfModifier * modifier
Definition: vf_lensfun.c:91
AVFilterContext
An instance of a filter.
Definition: avfilter.h:346
AVFILTER_FLAG_SLICE_THREADS
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:121
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
VIGNETTING
@ VIGNETTING
Definition: vf_lensfun.c:44
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(lensfun)
DistortionCorrectionThreadData::width
int width
Definition: vf_lensfun.c:64
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:241
Model
Definition: mss12.h:40
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:153
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
d
d
Definition: ffmpeg_filter.c:156
imgutils.h
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:334
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:228
ff_filter_execute
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: internal.h:143
int
int
Definition: ffmpeg_filter.c:156
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:233
LensfunContext::lens
lfLens * lens
Definition: vf_lensfun.c:89
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_lensfun.c:131
swscale.h
ff_vf_lensfun
const AVFilter ff_vf_lensfun
Definition: vf_lensfun.c:531