X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavfilter%2Fvf_lensfun.c;h=430daa941beab16b5e127d11561fe00e62d5ce05;hb=a240097ecd4fb1639db99e7becb888ae478405cd;hp=fd14210addbd2d85a0f1f160a9b271869d46cbbb;hpb=8e789d244cc946bc350672eeb02453918b21a09f;p=ffmpeg diff --git a/libavfilter/vf_lensfun.c b/libavfilter/vf_lensfun.c index fd14210addb..430daa941be 100644 --- a/libavfilter/vf_lensfun.c +++ b/libavfilter/vf_lensfun.c @@ -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; @@ -240,7 +244,7 @@ static int config_props(AVFilterLink *inlink) if (!lensfun->distortion_coords) { if (lensfun->mode & SUBPIXEL_DISTORTION) { - lensfun->distortion_coords = av_malloc(sizeof(float) * inlink->w * inlink->h * 2 * 3); + lensfun->distortion_coords = av_malloc_array(inlink->w * inlink->h, sizeof(float) * 2 * 3); if (!lensfun->distortion_coords) return AVERROR(ENOMEM); if (lensfun->mode & GEOMETRY_DISTORTION) { @@ -257,7 +261,7 @@ static int config_props(AVFilterLink *inlink) lensfun->distortion_coords); } } else if (lensfun->mode & GEOMETRY_DISTORTION) { - lensfun->distortion_coords = av_malloc(sizeof(float) * inlink->w * inlink->h * 2); + lensfun->distortion_coords = av_malloc_array(inlink->w * inlink->h, sizeof(float) * 2); if (!lensfun->distortion_coords) return AVERROR(ENOMEM); // apply only geometry distortion @@ -270,7 +274,7 @@ static int config_props(AVFilterLink *inlink) if (!lensfun->interpolation) if (lensfun->interpolation_type == LANCZOS) { - lensfun->interpolation = av_malloc(sizeof(float) * 4 * LANCZOS_RESOLUTION); + lensfun->interpolation = av_malloc_array(LANCZOS_RESOLUTION, sizeof(float) * 4); if (!lensfun->interpolation) return AVERROR(ENOMEM); for (index = 0; index < 4 * LANCZOS_RESOLUTION; ++index) { @@ -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); @@ -510,10 +514,8 @@ static av_cold void uninit(AVFilterContext *ctx) lf_lens_destroy(lensfun->lens); if (lensfun->modifier) lf_modifier_destroy(lensfun->modifier); - if (lensfun->distortion_coords) - av_free(lensfun->distortion_coords); - if (lensfun->interpolation) - av_free(lensfun->interpolation); + av_freep(&lensfun->distortion_coords); + av_freep(&lensfun->interpolation); } static const AVFilterPad lensfun_inputs[] = {