]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/vf_lensfun.c
avutil/mem: Also poison new av_realloc-allocated blocks
[ffmpeg] / libavfilter / vf_lensfun.c
index 901cd9ff90b1e4c98c6206d4ee8d8feaffedd95c..e9bb69187e0058445bf2bb4832b78dbbccad1db6 100644 (file)
@@ -79,6 +79,7 @@ typedef struct LensfunContext {
     float focal_length;
     float aperture;
     float focus_distance;
+    float scale;
     int target_geometry;
     int reverse;
     int interpolation_type;
@@ -108,6 +109,7 @@ static const AVOption lensfun_options[] = {
     { "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 },
     { "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 },
     { "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 },
+    { "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 },
     { "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" },
         { "rectilinear", "rectilinear lens (default)", 0, AV_OPT_TYPE_CONST, {.i64=LF_RECTILINEAR}, 0, 0, FLAGS, "lens_geometry" },
         { "fisheye", "fisheye lens", 0, AV_OPT_TYPE_CONST, {.i64=LF_FISHEYE}, 0, 0, FLAGS, "lens_geometry" },
@@ -134,26 +136,35 @@ static av_cold int init(AVFilterContext *ctx)
     const lfCamera **cameras;
     const lfLens **lenses;
 
-    if (!lensfun->make) {
-        av_log(ctx, AV_LOG_FATAL, "Option \"make\" not specified\n");
-        return AVERROR(EINVAL);
-    } else if (!lensfun->model) {
-        av_log(ctx, AV_LOG_FATAL, "Option \"model\" not specified\n");
+    db = lf_db_create();
+    if (lf_db_load(db) != LF_NO_ERROR) {
+        lf_db_destroy(db);
+        av_log(ctx, AV_LOG_FATAL, "Failed to load lensfun database\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    if (!lensfun->make || !lensfun->model) {
+        const lfCamera *const *cameras = lf_db_get_cameras(db);
+
+        av_log(ctx, AV_LOG_FATAL, "Option \"make\" or option \"model\" not specified\n");
+        av_log(ctx, AV_LOG_INFO, "Available values for \"make\" and \"model\":\n");
+        for (int i = 0; cameras && cameras[i]; i++)
+            av_log(ctx, AV_LOG_INFO, "\t%s\t%s\n", cameras[i]->Maker, cameras[i]->Model);
+        lf_db_destroy(db);
         return AVERROR(EINVAL);
     } else if (!lensfun->lens_model) {
+        const lfLens *const *lenses = lf_db_get_lenses(db);
+
         av_log(ctx, AV_LOG_FATAL, "Option \"lens_model\" not specified\n");
+        av_log(ctx, AV_LOG_INFO, "Available values for \"lens_model\":\n");
+        for (int i = 0; lenses && lenses[i]; i++)
+            av_log(ctx, AV_LOG_INFO, "\t%s\t(make %s)\n", lenses[i]->Model, lenses[i]->Maker);
+        lf_db_destroy(db);
         return AVERROR(EINVAL);
     }
 
-    lensfun->lens = lf_lens_new();
-    lensfun->camera = lf_camera_new();
-
-    db = lf_db_new();
-    if (lf_db_load(db) != LF_NO_ERROR) {
-        lf_db_destroy(db);
-        av_log(ctx, AV_LOG_FATAL, "Failed to load lensfun database\n");
-        return AVERROR_INVALIDDATA;
-    }
+    lensfun->lens = lf_lens_create();
+    lensfun->camera = lf_camera_create();
 
     cameras = lf_db_find_cameras(db, lensfun->make, lensfun->model);
     if (cameras && *cameras) {
@@ -167,7 +178,7 @@ static av_cold int init(AVFilterContext *ctx)
     }
     lf_free(cameras);
 
-    lenses = lf_db_find_lenses_hd(db, lensfun->camera, NULL, lensfun->lens_model, 0);
+    lenses = lf_db_find_lenses(db, lensfun->camera, NULL, lensfun->lens_model, 0);
     if (lenses && *lenses) {
         lf_lens_copy(lensfun->lens, *lenses);
         av_log(ctx, AV_LOG_INFO, "Using lens %s\n", lensfun->lens->Model);
@@ -208,30 +219,23 @@ static int config_props(AVFilterLink *inlink)
     LensfunContext *lensfun = ctx->priv;
     int index;
     float a;
-    int lensfun_mode = 0;
 
     if (!lensfun->modifier) {
         if (lensfun->camera && lensfun->lens) {
-            lensfun->modifier = lf_modifier_new(lensfun->lens,
-                                                lensfun->camera->CropFactor,
-                                                inlink->w,
-                                                inlink->h);
+            lensfun->modifier = lf_modifier_create(lensfun->lens,
+                                                   lensfun->focal_length,
+                                                   lensfun->camera->CropFactor,
+                                                   inlink->w,
+                                                   inlink->h, LF_PF_U8, lensfun->reverse);
             if (lensfun->mode & VIGNETTING)
-                lensfun_mode |= LF_MODIFY_VIGNETTING;
-            if (lensfun->mode & GEOMETRY_DISTORTION)
-                lensfun_mode |= LF_MODIFY_DISTORTION | LF_MODIFY_GEOMETRY | LF_MODIFY_SCALE;
+                lf_modifier_enable_vignetting_correction(lensfun->modifier, lensfun->aperture, lensfun->focus_distance);
+            if (lensfun->mode & GEOMETRY_DISTORTION) {
+                lf_modifier_enable_distortion_correction(lensfun->modifier);
+                lf_modifier_enable_projection_transform(lensfun->modifier, lensfun->target_geometry);
+                lf_modifier_enable_scaling(lensfun->modifier, lensfun->scale);
+            }
             if (lensfun->mode & SUBPIXEL_DISTORTION)
-                lensfun_mode |= LF_MODIFY_TCA;
-            lf_modifier_initialize(lensfun->modifier,
-                                   lensfun->lens,
-                                   LF_PF_U8,
-                                   lensfun->focal_length,
-                                   lensfun->aperture,
-                                   lensfun->focus_distance,
-                                   0.0,
-                                   lensfun->target_geometry,
-                                   lensfun_mode,
-                                   lensfun->reverse);
+                lf_modifier_enable_tca_correction(lensfun->modifier);
         } else {
             // lensfun->camera and lensfun->lens should have been initialized
             return AVERROR_BUG;
@@ -463,7 +467,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
                                vignetting_filter_slice,
                                &vignetting_thread_data,
                                NULL,
-                               FFMIN(outlink->h, ctx->graph->nb_threads));
+                               FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
     }
 
     if (lensfun->mode & (GEOMETRY_DISTORTION | SUBPIXEL_DISTORTION)) {
@@ -491,7 +495,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
                                distortion_correction_filter_slice,
                                &distortion_correction_thread_data,
                                NULL,
-                               FFMIN(outlink->h, ctx->graph->nb_threads));
+                               FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
 
         av_frame_free(&in);
         return ff_filter_frame(outlink, out);
@@ -532,7 +536,7 @@ static const AVFilterPad lensfun_outputs[] = {
     { NULL }
 };
 
-AVFilter ff_vf_lensfun = {
+const AVFilter ff_vf_lensfun = {
     .name          = "lensfun",
     .description   = NULL_IF_CONFIG_SMALL("Apply correction to an image based on info derived from the lensfun database."),
     .priv_size     = sizeof(LensfunContext),