[FFmpeg-cvslog] avfilters/vf_v360: add equisolid projection support

Paul B Mahol git at videolan.org
Mon Jun 22 15:43:07 EEST 2020


ffmpeg | branch: master | Paul B Mahol <onemda at gmail.com> | Mon Jun 22 14:39:35 2020 +0200| [44ce333f03e5daf7883147938f74018cec287aec] | committer: Paul B Mahol

avfilters/vf_v360: add equisolid projection support

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=44ce333f03e5daf7883147938f74018cec287aec
---

 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.
+
+ at item equisolid
+Equisolid format.
+
+Format specific options:
+ at table @option
+ at item h_fov
+ at item v_fov
+ at 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.
+
+ at item ih_fov
+ at item iv_fov
+ at 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.
+ at 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;



More information about the ffmpeg-cvslog mailing list