vf_lensfun.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. * @file
  22. * Lensfun filter, applies lens correction with parameters from the lensfun database
  23. *
  24. * @see https://lensfun.sourceforge.net/
  25. */
  26. #include <float.h>
  27. #include <math.h>
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/opt.h"
  31. #include "libswscale/swscale.h"
  32. #include "avfilter.h"
  33. #include "formats.h"
  34. #include "internal.h"
  35. #include "video.h"
  36. #include <lensfun.h>
  37. #define LANCZOS_RESOLUTION 256
  38. enum Mode {
  39. VIGNETTING = 0x1,
  40. GEOMETRY_DISTORTION = 0x2,
  41. SUBPIXEL_DISTORTION = 0x4
  42. };
  43. enum InterpolationType {
  44. NEAREST,
  45. LINEAR,
  46. LANCZOS
  47. };
  48. typedef struct VignettingThreadData {
  49. int width, height;
  50. uint8_t *data_in;
  51. int linesize_in;
  52. int pixel_composition;
  53. lfModifier *modifier;
  54. } VignettingThreadData;
  55. typedef struct DistortionCorrectionThreadData {
  56. int width, height;
  57. const float *distortion_coords;
  58. const uint8_t *data_in;
  59. uint8_t *data_out;
  60. int linesize_in, linesize_out;
  61. const float *interpolation;
  62. int mode;
  63. int interpolation_type;
  64. } DistortionCorrectionThreadData;
  65. typedef struct LensfunContext {
  66. const AVClass *class;
  67. const char *make, *model, *lens_model;
  68. int mode;
  69. float focal_length;
  70. float aperture;
  71. float focus_distance;
  72. float scale;
  73. int target_geometry;
  74. int reverse;
  75. int interpolation_type;
  76. float *distortion_coords;
  77. float *interpolation;
  78. lfLens *lens;
  79. lfCamera *camera;
  80. lfModifier *modifier;
  81. } LensfunContext;
  82. #define OFFSET(x) offsetof(LensfunContext, x)
  83. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  84. static const AVOption lensfun_options[] = {
  85. { "make", "set camera maker", OFFSET(make), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
  86. { "model", "set camera model", OFFSET(model), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
  87. { "lens_model", "set lens model", OFFSET(lens_model), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
  88. { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=GEOMETRY_DISTORTION}, 0, VIGNETTING | GEOMETRY_DISTORTION | SUBPIXEL_DISTORTION, FLAGS, "mode" },
  89. { "vignetting", "fix lens vignetting", 0, AV_OPT_TYPE_CONST, {.i64=VIGNETTING}, 0, 0, FLAGS, "mode" },
  90. { "geometry", "correct geometry distortion", 0, AV_OPT_TYPE_CONST, {.i64=GEOMETRY_DISTORTION}, 0, 0, FLAGS, "mode" },
  91. { "subpixel", "fix chromatic aberrations", 0, AV_OPT_TYPE_CONST, {.i64=SUBPIXEL_DISTORTION}, 0, 0, FLAGS, "mode" },
  92. { "vig_geo", "fix lens vignetting and correct geometry distortion", 0, AV_OPT_TYPE_CONST, {.i64=VIGNETTING | GEOMETRY_DISTORTION}, 0, 0, FLAGS, "mode" },
  93. { "vig_subpixel", "fix lens vignetting and chromatic aberrations", 0, AV_OPT_TYPE_CONST, {.i64=VIGNETTING | SUBPIXEL_DISTORTION}, 0, 0, FLAGS, "mode" },
  94. { "distortion", "correct geometry distortion and chromatic aberrations", 0, AV_OPT_TYPE_CONST, {.i64=GEOMETRY_DISTORTION | SUBPIXEL_DISTORTION}, 0, 0, FLAGS, "mode" },
  95. { "all", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VIGNETTING | GEOMETRY_DISTORTION | SUBPIXEL_DISTORTION}, 0, 0, FLAGS, "mode" },
  96. { "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 },
  97. { "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 },
  98. { "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 },
  99. { "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 },
  100. { "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" },
  101. { "rectilinear", "rectilinear lens (default)", 0, AV_OPT_TYPE_CONST, {.i64=LF_RECTILINEAR}, 0, 0, FLAGS, "lens_geometry" },
  102. { "fisheye", "fisheye lens", 0, AV_OPT_TYPE_CONST, {.i64=LF_FISHEYE}, 0, 0, FLAGS, "lens_geometry" },
  103. { "panoramic", "panoramic (cylindrical)", 0, AV_OPT_TYPE_CONST, {.i64=LF_PANORAMIC}, 0, 0, FLAGS, "lens_geometry" },
  104. { "equirectangular", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=LF_EQUIRECTANGULAR}, 0, 0, FLAGS, "lens_geometry" },
  105. { "fisheye_orthographic", "orthographic fisheye", 0, AV_OPT_TYPE_CONST, {.i64=LF_FISHEYE_ORTHOGRAPHIC}, 0, 0, FLAGS, "lens_geometry" },
  106. { "fisheye_stereographic", "stereographic fisheye", 0, AV_OPT_TYPE_CONST, {.i64=LF_FISHEYE_STEREOGRAPHIC}, 0, 0, FLAGS, "lens_geometry" },
  107. { "fisheye_equisolid", "equisolid fisheye", 0, AV_OPT_TYPE_CONST, {.i64=LF_FISHEYE_EQUISOLID}, 0, 0, FLAGS, "lens_geometry" },
  108. { "fisheye_thoby", "fisheye as measured by thoby", 0, AV_OPT_TYPE_CONST, {.i64=LF_FISHEYE_THOBY}, 0, 0, FLAGS, "lens_geometry" },
  109. { "reverse", "Does reverse correction (regular image to lens distorted)", OFFSET(reverse), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
  110. { "interpolation", "Type of interpolation", OFFSET(interpolation_type), AV_OPT_TYPE_INT, {.i64=LINEAR}, 0, LANCZOS, FLAGS, "interpolation" },
  111. { "nearest", NULL, 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interpolation" },
  112. { "linear", NULL, 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "interpolation" },
  113. { "lanczos", NULL, 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interpolation" },
  114. { NULL }
  115. };
  116. AVFILTER_DEFINE_CLASS(lensfun);
  117. static av_cold int init(AVFilterContext *ctx)
  118. {
  119. LensfunContext *lensfun = ctx->priv;
  120. lfDatabase *db;
  121. const lfCamera **cameras;
  122. const lfLens **lenses;
  123. if (!lensfun->make) {
  124. av_log(ctx, AV_LOG_FATAL, "Option \"make\" not specified\n");
  125. return AVERROR(EINVAL);
  126. } else if (!lensfun->model) {
  127. av_log(ctx, AV_LOG_FATAL, "Option \"model\" not specified\n");
  128. return AVERROR(EINVAL);
  129. } else if (!lensfun->lens_model) {
  130. av_log(ctx, AV_LOG_FATAL, "Option \"lens_model\" not specified\n");
  131. return AVERROR(EINVAL);
  132. }
  133. lensfun->lens = lf_lens_new();
  134. lensfun->camera = lf_camera_new();
  135. db = lf_db_new();
  136. if (lf_db_load(db) != LF_NO_ERROR) {
  137. lf_db_destroy(db);
  138. av_log(ctx, AV_LOG_FATAL, "Failed to load lensfun database\n");
  139. return AVERROR_INVALIDDATA;
  140. }
  141. cameras = lf_db_find_cameras(db, lensfun->make, lensfun->model);
  142. if (cameras && *cameras) {
  143. lf_camera_copy(lensfun->camera, *cameras);
  144. av_log(ctx, AV_LOG_INFO, "Using camera %s\n", lensfun->camera->Model);
  145. } else {
  146. lf_free(cameras);
  147. lf_db_destroy(db);
  148. av_log(ctx, AV_LOG_FATAL, "Failed to find camera in lensfun database\n");
  149. return AVERROR_INVALIDDATA;
  150. }
  151. lf_free(cameras);
  152. lenses = lf_db_find_lenses_hd(db, lensfun->camera, NULL, lensfun->lens_model, 0);
  153. if (lenses && *lenses) {
  154. lf_lens_copy(lensfun->lens, *lenses);
  155. av_log(ctx, AV_LOG_INFO, "Using lens %s\n", lensfun->lens->Model);
  156. } else {
  157. lf_free(lenses);
  158. lf_db_destroy(db);
  159. av_log(ctx, AV_LOG_FATAL, "Failed to find lens in lensfun database\n");
  160. return AVERROR_INVALIDDATA;
  161. }
  162. lf_free(lenses);
  163. lf_db_destroy(db);
  164. return 0;
  165. }
  166. static int query_formats(AVFilterContext *ctx)
  167. {
  168. // Some of the functions provided by lensfun require pixels in RGB format
  169. static const enum AVPixelFormat fmts[] = {AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE};
  170. AVFilterFormats *fmts_list = ff_make_format_list(fmts);
  171. return ff_set_common_formats(ctx, fmts_list);
  172. }
  173. static float lanczos_kernel(float x)
  174. {
  175. if (x == 0.0f) {
  176. return 1.0f;
  177. } else if (x > -2.0f && x < 2.0f) {
  178. return (2.0f * sin(M_PI * x) * sin(M_PI / 2.0f * x)) / (M_PI * M_PI * x * x);
  179. } else {
  180. return 0.0f;
  181. }
  182. }
  183. static int config_props(AVFilterLink *inlink)
  184. {
  185. AVFilterContext *ctx = inlink->dst;
  186. LensfunContext *lensfun = ctx->priv;
  187. int index;
  188. float a;
  189. int lensfun_mode = 0;
  190. if (!lensfun->modifier) {
  191. if (lensfun->camera && lensfun->lens) {
  192. lensfun->modifier = lf_modifier_new(lensfun->lens,
  193. lensfun->camera->CropFactor,
  194. inlink->w,
  195. inlink->h);
  196. if (lensfun->mode & VIGNETTING)
  197. lensfun_mode |= LF_MODIFY_VIGNETTING;
  198. if (lensfun->mode & GEOMETRY_DISTORTION)
  199. lensfun_mode |= LF_MODIFY_DISTORTION | LF_MODIFY_GEOMETRY | LF_MODIFY_SCALE;
  200. if (lensfun->mode & SUBPIXEL_DISTORTION)
  201. lensfun_mode |= LF_MODIFY_TCA;
  202. lf_modifier_initialize(lensfun->modifier,
  203. lensfun->lens,
  204. LF_PF_U8,
  205. lensfun->focal_length,
  206. lensfun->aperture,
  207. lensfun->focus_distance,
  208. lensfun->scale,
  209. lensfun->target_geometry,
  210. lensfun_mode,
  211. lensfun->reverse);
  212. } else {
  213. // lensfun->camera and lensfun->lens should have been initialized
  214. return AVERROR_BUG;
  215. }
  216. }
  217. if (!lensfun->distortion_coords) {
  218. if (lensfun->mode & SUBPIXEL_DISTORTION) {
  219. lensfun->distortion_coords = av_malloc_array(inlink->w * inlink->h, sizeof(float) * 2 * 3);
  220. if (!lensfun->distortion_coords)
  221. return AVERROR(ENOMEM);
  222. if (lensfun->mode & GEOMETRY_DISTORTION) {
  223. // apply both geometry and subpixel distortion
  224. lf_modifier_apply_subpixel_geometry_distortion(lensfun->modifier,
  225. 0, 0,
  226. inlink->w, inlink->h,
  227. lensfun->distortion_coords);
  228. } else {
  229. // apply only subpixel distortion
  230. lf_modifier_apply_subpixel_distortion(lensfun->modifier,
  231. 0, 0,
  232. inlink->w, inlink->h,
  233. lensfun->distortion_coords);
  234. }
  235. } else if (lensfun->mode & GEOMETRY_DISTORTION) {
  236. lensfun->distortion_coords = av_malloc_array(inlink->w * inlink->h, sizeof(float) * 2);
  237. if (!lensfun->distortion_coords)
  238. return AVERROR(ENOMEM);
  239. // apply only geometry distortion
  240. lf_modifier_apply_geometry_distortion(lensfun->modifier,
  241. 0, 0,
  242. inlink->w, inlink->h,
  243. lensfun->distortion_coords);
  244. }
  245. }
  246. if (!lensfun->interpolation)
  247. if (lensfun->interpolation_type == LANCZOS) {
  248. lensfun->interpolation = av_malloc_array(LANCZOS_RESOLUTION, sizeof(float) * 4);
  249. if (!lensfun->interpolation)
  250. return AVERROR(ENOMEM);
  251. for (index = 0; index < 4 * LANCZOS_RESOLUTION; ++index) {
  252. if (index == 0) {
  253. lensfun->interpolation[index] = 1.0f;
  254. } else {
  255. a = sqrtf((float)index / LANCZOS_RESOLUTION);
  256. lensfun->interpolation[index] = lanczos_kernel(a);
  257. }
  258. }
  259. }
  260. return 0;
  261. }
  262. static int vignetting_filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  263. {
  264. const VignettingThreadData *thread_data = arg;
  265. const int slice_start = thread_data->height * jobnr / nb_jobs;
  266. const int slice_end = thread_data->height * (jobnr + 1) / nb_jobs;
  267. lf_modifier_apply_color_modification(thread_data->modifier,
  268. thread_data->data_in + slice_start * thread_data->linesize_in,
  269. 0,
  270. slice_start,
  271. thread_data->width,
  272. slice_end - slice_start,
  273. thread_data->pixel_composition,
  274. thread_data->linesize_in);
  275. return 0;
  276. }
  277. static float square(float x)
  278. {
  279. return x * x;
  280. }
  281. static int distortion_correction_filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  282. {
  283. const DistortionCorrectionThreadData *thread_data = arg;
  284. const int slice_start = thread_data->height * jobnr / nb_jobs;
  285. const int slice_end = thread_data->height * (jobnr + 1) / nb_jobs;
  286. int x, y, i, j, rgb_index;
  287. float interpolated, new_x, new_y, d, norm;
  288. int new_x_int, new_y_int;
  289. for (y = slice_start; y < slice_end; ++y)
  290. for (x = 0; x < thread_data->width; ++x)
  291. for (rgb_index = 0; rgb_index < 3; ++rgb_index) {
  292. if (thread_data->mode & SUBPIXEL_DISTORTION) {
  293. // subpixel (and possibly geometry) distortion correction was applied, correct distortion
  294. switch(thread_data->interpolation_type) {
  295. case NEAREST:
  296. new_x_int = thread_data->distortion_coords[x * 2 * 3 + y * thread_data->width * 2 * 3 + rgb_index * 2] + 0.5f;
  297. new_y_int = thread_data->distortion_coords[x * 2 * 3 + y * thread_data->width * 2 * 3 + rgb_index * 2 + 1] + 0.5f;
  298. if (new_x_int < 0 || new_x_int >= thread_data->width || new_y_int < 0 || new_y_int >= thread_data->height) {
  299. thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = 0;
  300. } else {
  301. 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];
  302. }
  303. break;
  304. case LINEAR:
  305. interpolated = 0.0f;
  306. new_x = thread_data->distortion_coords[x * 2 * 3 + y * thread_data->width * 2 * 3 + rgb_index * 2];
  307. new_x_int = new_x;
  308. new_y = thread_data->distortion_coords[x * 2 * 3 + y * thread_data->width * 2 * 3 + rgb_index * 2 + 1];
  309. new_y_int = new_y;
  310. if (new_x_int < 0 || new_x_int + 1 >= thread_data->width || new_y_int < 0 || new_y_int + 1 >= thread_data->height) {
  311. thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = 0;
  312. } else {
  313. thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] =
  314. 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)
  315. + 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)
  316. + 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)
  317. + 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);
  318. }
  319. break;
  320. case LANCZOS:
  321. interpolated = 0.0f;
  322. norm = 0.0f;
  323. new_x = thread_data->distortion_coords[x * 2 * 3 + y * thread_data->width * 2 * 3 + rgb_index * 2];
  324. new_x_int = new_x;
  325. new_y = thread_data->distortion_coords[x * 2 * 3 + y * thread_data->width * 2 * 3 + rgb_index * 2 + 1];
  326. new_y_int = new_y;
  327. for (j = 0; j < 4; ++j)
  328. for (i = 0; i < 4; ++i) {
  329. 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)
  330. continue;
  331. d = square(new_x - (new_x_int + i - 2)) * square(new_y - (new_y_int + j - 2));
  332. if (d >= 4.0f)
  333. continue;
  334. d = thread_data->interpolation[(int)(d * LANCZOS_RESOLUTION)];
  335. norm += d;
  336. interpolated += thread_data->data_in[(new_x_int + i - 2) * 3 + rgb_index + (new_y_int + j - 2) * thread_data->linesize_in] * d;
  337. }
  338. if (norm == 0.0f) {
  339. thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = 0;
  340. } else {
  341. interpolated /= norm;
  342. thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = interpolated < 0.0f ? 0.0f : interpolated > 255.0f ? 255.0f : interpolated;
  343. }
  344. break;
  345. }
  346. } else if (thread_data->mode & GEOMETRY_DISTORTION) {
  347. // geometry distortion correction was applied, correct distortion
  348. switch(thread_data->interpolation_type) {
  349. case NEAREST:
  350. new_x_int = thread_data->distortion_coords[x * 2 + y * thread_data->width * 2] + 0.5f;
  351. new_y_int = thread_data->distortion_coords[x * 2 + y * thread_data->width * 2 + 1] + 0.5f;
  352. if (new_x_int < 0 || new_x_int >= thread_data->width || new_y_int < 0 || new_y_int >= thread_data->height) {
  353. thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = 0;
  354. } else {
  355. 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];
  356. }
  357. break;
  358. case LINEAR:
  359. interpolated = 0.0f;
  360. new_x = thread_data->distortion_coords[x * 2 + y * thread_data->width * 2];
  361. new_x_int = new_x;
  362. new_y = thread_data->distortion_coords[x * 2 + y * thread_data->width * 2 + 1];
  363. new_y_int = new_y;
  364. if (new_x_int < 0 || new_x_int + 1 >= thread_data->width || new_y_int < 0 || new_y_int + 1 >= thread_data->height) {
  365. thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = 0;
  366. } else {
  367. thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] =
  368. 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)
  369. + 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)
  370. + 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)
  371. + 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);
  372. }
  373. break;
  374. case LANCZOS:
  375. interpolated = 0.0f;
  376. norm = 0.0f;
  377. new_x = thread_data->distortion_coords[x * 2 + y * thread_data->width * 2];
  378. new_x_int = new_x;
  379. new_y = thread_data->distortion_coords[x * 2 + 1 + y * thread_data->width * 2];
  380. new_y_int = new_y;
  381. for (j = 0; j < 4; ++j)
  382. for (i = 0; i < 4; ++i) {
  383. 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)
  384. continue;
  385. d = square(new_x - (new_x_int + i - 2)) * square(new_y - (new_y_int + j - 2));
  386. if (d >= 4.0f)
  387. continue;
  388. d = thread_data->interpolation[(int)(d * LANCZOS_RESOLUTION)];
  389. norm += d;
  390. interpolated += thread_data->data_in[(new_x_int + i - 2) * 3 + rgb_index + (new_y_int + j - 2) * thread_data->linesize_in] * d;
  391. }
  392. if (norm == 0.0f) {
  393. thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = 0;
  394. } else {
  395. interpolated /= norm;
  396. thread_data->data_out[x * 3 + rgb_index + y * thread_data->linesize_out] = interpolated < 0.0f ? 0.0f : interpolated > 255.0f ? 255.0f : interpolated;
  397. }
  398. break;
  399. }
  400. } else {
  401. // no distortion correction was applied
  402. 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];
  403. }
  404. }
  405. return 0;
  406. }
  407. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  408. {
  409. AVFilterContext *ctx = inlink->dst;
  410. LensfunContext *lensfun = ctx->priv;
  411. AVFilterLink *outlink = ctx->outputs[0];
  412. AVFrame *out;
  413. VignettingThreadData vignetting_thread_data;
  414. DistortionCorrectionThreadData distortion_correction_thread_data;
  415. if (lensfun->mode & VIGNETTING) {
  416. av_frame_make_writable(in);
  417. vignetting_thread_data = (VignettingThreadData) {
  418. .width = inlink->w,
  419. .height = inlink->h,
  420. .data_in = in->data[0],
  421. .linesize_in = in->linesize[0],
  422. .pixel_composition = LF_CR_3(RED, GREEN, BLUE),
  423. .modifier = lensfun->modifier
  424. };
  425. ctx->internal->execute(ctx,
  426. vignetting_filter_slice,
  427. &vignetting_thread_data,
  428. NULL,
  429. FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
  430. }
  431. if (lensfun->mode & (GEOMETRY_DISTORTION | SUBPIXEL_DISTORTION)) {
  432. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  433. if (!out) {
  434. av_frame_free(&in);
  435. return AVERROR(ENOMEM);
  436. }
  437. av_frame_copy_props(out, in);
  438. distortion_correction_thread_data = (DistortionCorrectionThreadData) {
  439. .width = inlink->w,
  440. .height = inlink->h,
  441. .distortion_coords = lensfun->distortion_coords,
  442. .data_in = in->data[0],
  443. .data_out = out->data[0],
  444. .linesize_in = in->linesize[0],
  445. .linesize_out = out->linesize[0],
  446. .interpolation = lensfun->interpolation,
  447. .mode = lensfun->mode,
  448. .interpolation_type = lensfun->interpolation_type
  449. };
  450. ctx->internal->execute(ctx,
  451. distortion_correction_filter_slice,
  452. &distortion_correction_thread_data,
  453. NULL,
  454. FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
  455. av_frame_free(&in);
  456. return ff_filter_frame(outlink, out);
  457. } else {
  458. return ff_filter_frame(outlink, in);
  459. }
  460. }
  461. static av_cold void uninit(AVFilterContext *ctx)
  462. {
  463. LensfunContext *lensfun = ctx->priv;
  464. if (lensfun->camera)
  465. lf_camera_destroy(lensfun->camera);
  466. if (lensfun->lens)
  467. lf_lens_destroy(lensfun->lens);
  468. if (lensfun->modifier)
  469. lf_modifier_destroy(lensfun->modifier);
  470. av_freep(&lensfun->distortion_coords);
  471. av_freep(&lensfun->interpolation);
  472. }
  473. static const AVFilterPad lensfun_inputs[] = {
  474. {
  475. .name = "default",
  476. .type = AVMEDIA_TYPE_VIDEO,
  477. .config_props = config_props,
  478. .filter_frame = filter_frame,
  479. },
  480. { NULL }
  481. };
  482. static const AVFilterPad lensfun_outputs[] = {
  483. {
  484. .name = "default",
  485. .type = AVMEDIA_TYPE_VIDEO,
  486. },
  487. { NULL }
  488. };
  489. AVFilter ff_vf_lensfun = {
  490. .name = "lensfun",
  491. .description = NULL_IF_CONFIG_SMALL("Apply correction to an image based on info derived from the lensfun database."),
  492. .priv_size = sizeof(LensfunContext),
  493. .init = init,
  494. .uninit = uninit,
  495. .query_formats = query_formats,
  496. .inputs = lensfun_inputs,
  497. .outputs = lensfun_outputs,
  498. .priv_class = &lensfun_class,
  499. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  500. };