From 44ce333f03e5daf7883147938f74018cec287aec Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Mon, 22 Jun 2020 14:39:35 +0200 Subject: [PATCH] avfilters/vf_v360: add equisolid projection support --- doc/filters.texi | 20 +++++++ libavfilter/v360.h | 1 + libavfilter/vf_v360.c | 129 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+) diff --git a/doc/filters.texi b/doc/filters.texi index 551604a143..1a55f3df72 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -19322,6 +19322,26 @@ Truncated square pyramid projection. @item he @item hequirect Half equirectangular projection. + +@item equisolid +Equisolid format. + +Format specific options: +@table @option +@item h_fov +@item v_fov +@item d_fov +Set output horizontal/vertical/diagonal field of view. Values in degrees. + +If diagonal field of view is set it overrides horizontal and vertical field of view. + +@item ih_fov +@item iv_fov +@item id_fov +Set input horizontal/vertical/diagonal field of view. Values in degrees. + +If diagonal field of view is set it overrides horizontal and vertical field of view. +@end table @end table @item interp diff --git a/libavfilter/v360.h b/libavfilter/v360.h index e9a47593c9..337e74b410 100644 --- a/libavfilter/v360.h +++ b/libavfilter/v360.h @@ -51,6 +51,7 @@ enum Projections { BARREL_SPLIT, TSPYRAMID, HEQUIRECTANGULAR, + EQUISOLID, NB_PROJECTIONS, }; diff --git a/libavfilter/vf_v360.c b/libavfilter/vf_v360.c index e9457d9400..f892e0321f 100644 --- a/libavfilter/vf_v360.c +++ b/libavfilter/vf_v360.c @@ -81,6 +81,7 @@ static const AVOption v360_options[] = { { "tsp", "truncated square pyramid", 0, AV_OPT_TYPE_CONST, {.i64=TSPYRAMID}, 0, 0, FLAGS, "in" }, { "hequirect", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "in" }, { "he", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "in" }, + { "equisolid", "equisolid", 0, AV_OPT_TYPE_CONST, {.i64=EQUISOLID}, 0, 0, FLAGS, "in" }, { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" }, { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" }, { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" }, @@ -108,6 +109,7 @@ static const AVOption v360_options[] = { { "tsp", "truncated square pyramid", 0, AV_OPT_TYPE_CONST, {.i64=TSPYRAMID}, 0, 0, FLAGS, "out" }, { "hequirect", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "out" }, { "he", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "out" }, + { "equisolid", "equisolid", 0, AV_OPT_TYPE_CONST, {.i64=EQUISOLID}, 0, 0, FLAGS, "out" }, { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" }, { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" }, { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" }, @@ -1819,6 +1821,112 @@ static int xyz_to_stereographic(const V360Context *s, return visible; } +/** + * Prepare data for processing equisolid output format. + * + * @param ctx filter context + * + * @return error code + */ +static int prepare_equisolid_out(AVFilterContext *ctx) +{ + V360Context *s = ctx->priv; + + s->flat_range[0] = sinf(FFMIN(s->h_fov, 359.f) * M_PI / 720.f); + s->flat_range[1] = sinf(FFMIN(s->v_fov, 359.f) * M_PI / 720.f); + + return 0; +} + +/** + * Calculate 3D coordinates on sphere for corresponding frame position in equisolid format. + * + * @param s filter private context + * @param i horizontal position on frame [0, width) + * @param j vertical position on frame [0, height) + * @param width frame width + * @param height frame height + * @param vec coordinates on sphere + */ +static int equisolid_to_xyz(const V360Context *s, + int i, int j, int width, int height, + float *vec) +{ + const float x = ((2.f * i + 1.f) / width - 1.f) * s->flat_range[0]; + const float y = ((2.f * j + 1.f) / height - 1.f) * s->flat_range[1]; + const float r = hypotf(x, y); + const float theta = asinf(r) * 2.f; + const float sin_theta = sinf(theta); + + vec[0] = x / r * sin_theta; + vec[1] = y / r * sin_theta; + vec[2] = cosf(theta); + + normalize_vector(vec); + + return 1; +} + +/** + * Prepare data for processing equisolid input format. + * + * @param ctx filter context + * + * @return error code + */ +static int prepare_equisolid_in(AVFilterContext *ctx) +{ + V360Context *s = ctx->priv; + + s->iflat_range[0] = sinf(FFMIN(s->ih_fov, 359.f) * M_PI / 720.f); + s->iflat_range[1] = sinf(FFMIN(s->iv_fov, 359.f) * M_PI / 720.f); + + return 0; +} + +/** + * Calculate frame position in equisolid format for corresponding 3D coordinates on sphere. + * + * @param s filter private context + * @param vec coordinates on sphere + * @param width frame width + * @param height frame height + * @param us horizontal coordinates for interpolation window + * @param vs vertical coordinates for interpolation window + * @param du horizontal relative coordinate + * @param dv vertical relative coordinate + */ +static int xyz_to_equisolid(const V360Context *s, + const float *vec, int width, int height, + int16_t us[4][4], int16_t vs[4][4], float *du, float *dv) +{ + const float theta = acosf(vec[2]); + const float r = sinf(theta * 0.5f); + const float c = r / hypotf(vec[0], vec[1]); + const float x = vec[0] * c / s->iflat_range[0] * s->input_mirror_modifier[0]; + const float y = vec[1] * c / s->iflat_range[1] * s->input_mirror_modifier[1]; + + const float uf = (x + 1.f) * width / 2.f; + const float vf = (y + 1.f) * height / 2.f; + + const int ui = floorf(uf); + const int vi = floorf(vf); + + const int visible = isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width; + + *du = visible ? uf - ui : 0.f; + *dv = visible ? vf - vi : 0.f; + + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0; + vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0; + } + } + + return visible; +} + /** * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere. * @@ -3644,6 +3752,15 @@ static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int siz static void fov_from_dfov(int format, float d_fov, float w, float h, float *h_fov, float *v_fov) { switch (format) { + case EQUISOLID: + { + const float d = 0.5f * hypotf(w, h); + const float l = d / (sinf(d_fov * M_PI / 720.f)); + + *h_fov = 2.f * asinf(w * 0.5f / l) * 360.f / M_PI; + *v_fov = 2.f * asinf(h * 0.5f / l) * 360.f / M_PI; + } + break; case STEREOGRAPHIC: { const float d = 0.5f * hypotf(w, h); @@ -4014,6 +4131,12 @@ static int config_output(AVFilterLink *outlink) wf = w * 2.f; hf = h; break; + case EQUISOLID: + s->in_transform = xyz_to_equisolid; + err = prepare_equisolid_in(ctx); + wf = w; + hf = h / 2.f; + break; default: av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n"); return AVERROR_BUG; @@ -4150,6 +4273,12 @@ static int config_output(AVFilterLink *outlink) w = lrintf(wf / 2.f); h = lrintf(hf); break; + case EQUISOLID: + s->out_transform = equisolid_to_xyz; + prepare_out = prepare_equisolid_out; + w = lrintf(wf); + h = lrintf(hf * 2.f); + break; default: av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n"); return AVERROR_BUG;