]> git.sesse.net Git - ffmpeg/commitdiff
avfilter/vf_v360: add stereographic output projection
authorPaul B Mahol <onemda@gmail.com>
Sun, 8 Sep 2019 16:17:57 +0000 (18:17 +0200)
committerPaul B Mahol <onemda@gmail.com>
Sun, 8 Sep 2019 19:07:59 +0000 (21:07 +0200)
libavfilter/v360.h
libavfilter/vf_v360.c

index 0c26b54ec127a9d3bff58b85b3836bf22bddb111..93ac8034ef9235d2f932e03f6a3d68656b401a17 100644 (file)
@@ -31,6 +31,7 @@ enum Projections {
     DUAL_FISHEYE,
     BARREL,
     CUBEMAP_1_6,
+    STEREOGRAPHIC,
     NB_PROJECTIONS,
 };
 
index 426697145b1dd1726269295cb24894519e15c12a..13dffd9e1500be4920cd0340026478ea62544a89 100644 (file)
@@ -74,6 +74,7 @@ static const AVOption v360_options[] = {
     {    "barrel", "barrel facebook's 360 format",               0, AV_OPT_TYPE_CONST,  {.i64=BARREL},          0,                   0, FLAGS, "out" },
     {        "fb", "barrel facebook's 360 format",               0, AV_OPT_TYPE_CONST,  {.i64=BARREL},          0,                   0, FLAGS, "out" },
     {      "c1x6", "cubemap 1x6",                                0, AV_OPT_TYPE_CONST,  {.i64=CUBEMAP_1_6},     0,                   0, FLAGS, "out" },
+    {        "sg", "stereographic",                              0, AV_OPT_TYPE_CONST,  {.i64=STEREOGRAPHIC},   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" },
@@ -1377,6 +1378,36 @@ static void equirect_to_xyz(const V360Context *s,
     vec[2] = -cos_theta * cos_phi;
 }
 
+/**
+ * Calculate 3D coordinates on sphere for corresponding frame position in stereographic format.
+ *
+ * @param s filter context
+ * @param i horizontal position on frame [0, height)
+ * @param j vertical position on frame [0, width)
+ * @param width frame width
+ * @param height frame height
+ * @param vec coordinates on sphere
+ */
+static void stereographic_to_xyz(const V360Context *s,
+                                 int i, int j, int width, int height,
+                                 float *vec)
+{
+    const float z  = 0.5f * s->h_fov * M_PI / 180.f;
+    const float x  = z * (2.f * i / width  - 1.f);
+    const float y  = z * (2.f * j / height - 1.f);
+    const float xy = x * x + y * y;
+    float norm;
+
+    vec[0] = 2.f * x / (1.f + xy);
+    vec[1] = (-1.f + xy) / (1.f + xy);
+    vec[2] = 2.f * y / (1.f + xy);
+
+    norm = sqrtf(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
+    vec[0] /= norm;
+    vec[1] /= norm;
+    vec[2] /= norm;
+}
+
 /**
  * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
  *
@@ -2194,6 +2225,12 @@ static int config_output(AVFilterLink *outlink)
         w = roundf(wf / 4.f * 5.f);
         h = roundf(hf);
         break;
+    case STEREOGRAPHIC:
+        out_transform = stereographic_to_xyz;
+        err = 0;
+        w = FFMAX(roundf(wf), roundf(hf));
+        h = w;
+        break;
     default:
         av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
         return AVERROR_BUG;