]> git.sesse.net Git - ffmpeg/commitdiff
avfilter/vf_v360: add perspective output projection
authorPaul B Mahol <onemda@gmail.com>
Sat, 18 Jan 2020 15:26:59 +0000 (16:26 +0100)
committerPaul B Mahol <onemda@gmail.com>
Sat, 18 Jan 2020 16:25:48 +0000 (17:25 +0100)
doc/filters.texi
libavfilter/v360.h
libavfilter/vf_v360.c

index 252eb4b7ad88e82bd4076161281be032be09bf0e..237e182aa8bac08aa0d3170bcc07303724129aa6 100644 (file)
@@ -19003,6 +19003,15 @@ Set 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
 
+@item perspective
+Perspective projection. @i{(output only)}
+
+Format specific options:
+@table @option
+@item v_fov
+Set perspective parameter.
+@end table
+
 @end table
 
 @item interp
index 29bf27565c22fd6516a692087bbbb9513a7c25a7..88de9cbd7cdebe9aa44093f88927b70ac0774e43 100644 (file)
@@ -46,6 +46,7 @@ enum Projections {
     FISHEYE,
     PANNINI,
     CYLINDRICAL,
+    PERSPECTIVE,
     NB_PROJECTIONS,
 };
 
index 452f52dce605a8d40c9b04dd04e60a82c50a2401..bc10ced55d25a9a7df55e0226068e55d65d1270d 100644 (file)
@@ -90,6 +90,7 @@ static const AVOption v360_options[] = {
     {   "fisheye", "fisheye",                                    0, AV_OPT_TYPE_CONST,  {.i64=FISHEYE},         0,                   0, FLAGS, "out" },
     {   "pannini", "pannini",                                    0, AV_OPT_TYPE_CONST,  {.i64=PANNINI},         0,                   0, FLAGS, "out" },
     {"cylindrical", "cylindrical",                               0, AV_OPT_TYPE_CONST,  {.i64=CYLINDRICAL},     0,                   0, FLAGS, "out" },
+    {"perspective", "perspective",                               0, AV_OPT_TYPE_CONST,  {.i64=PERSPECTIVE},     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" },
@@ -2334,6 +2335,51 @@ static void cylindrical_to_xyz(const V360Context *s,
     normalize_vector(vec);
 }
 
+/**
+ * Calculate 3D coordinates on sphere for corresponding frame position in perspective 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 void perspective_to_xyz(const V360Context *s,
+                               int i, int j, int width, int height,
+                               float *vec)
+{
+    const float uf = ((2.f * i) / width  - 1.f);
+    const float vf = ((2.f * j) / height - 1.f);
+    const float rh = hypotf(uf, vf);
+    const float sinzz = 1.f - rh * rh;
+    const float h = 1.f + s->v_fov;
+    const float sinz = (h - sqrtf(sinzz)) / (h / rh + rh / h);
+    const float sinz2 = sinz * sinz;
+
+    if (sinz2 <= 1.f) {
+        const float cosz = sqrtf(1.f - sinz2);
+
+        const float theta = asinf(cosz);
+        const float phi   = atan2f(uf, vf);
+
+        const float sin_phi   = sinf(phi);
+        const float cos_phi   = cosf(phi);
+        const float sin_theta = sinf(theta);
+        const float cos_theta = cosf(theta);
+
+        vec[0] =  cos_theta * sin_phi;
+        vec[1] =  sin_theta;
+        vec[2] = -cos_theta * cos_phi;
+    } else {
+        vec[0] =  0.f;
+        vec[1] = -1.f;
+        vec[2] =  0.f;
+    }
+
+    normalize_vector(vec);
+}
+
 /**
  * Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format.
  *
@@ -2884,6 +2930,7 @@ static int config_output(AVFilterLink *outlink)
         wf = w;
         hf = h / 9.f * 8.f;
         break;
+    case PERSPECTIVE:
     case CYLINDRICAL:
     case PANNINI:
     case FISHEYE:
@@ -3038,6 +3085,12 @@ static int config_output(AVFilterLink *outlink)
         w = roundf(wf);
         h = roundf(hf * 0.5f);
         break;
+    case PERSPECTIVE:
+        s->out_transform = perspective_to_xyz;
+        prepare_out = NULL;
+        w = roundf(wf / 2.f);
+        h = roundf(hf);
+        break;
     default:
         av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
         return AVERROR_BUG;