History

avfilters/vf_v360: add equisolid projection support

Paul B Mahol committed 6 years ago

Commit 44ce3331 parent 3f2be533 files · +150 −0Browse files

Showing the changes to libavfilter (2 of 3 files). Show all

Unified · Split

2 files
  1. libavfilter/v360.h +1 −0
  2. libavfilter/vf_v360.c +129 −0
libavfilter/v360.h+1−0
@@ -51,6 +51,7 @@ enum Projections { · 50 unmodified lines
5151 BARREL_SPLIT,
5252 TSPYRAMID,
5353 HEQUIRECTANGULAR,
54+ EQUISOLID,
5455 NB_PROJECTIONS,
5556 };
5657
libavfilter/vf_v360.c+129−0
@@ -81,6 +81,7 @@ static const AVOption v360_options[] = { · 80 unmodified lines
8181 { "tsp", "truncated square pyramid", 0, AV_OPT_TYPE_CONST, {.i64=TSPYRAMID}, 0, 0, FLAGS, "in" },
8282 { "hequirect", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "in" },
8383 { "he", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "in" },
84+ { "equisolid", "equisolid", 0, AV_OPT_TYPE_CONST, {.i64=EQUISOLID}, 0, 0, FLAGS, "in" },
8485 { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" },
8586 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
8687 { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
@@ -108,6 +109,7 @@ static const AVOption v360_options[] = { · 21 unmodified lines
108109 { "tsp", "truncated square pyramid", 0, AV_OPT_TYPE_CONST, {.i64=TSPYRAMID}, 0, 0, FLAGS, "out" },
109110 { "hequirect", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "out" },
110111 { "he", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "out" },
112+ { "equisolid", "equisolid", 0, AV_OPT_TYPE_CONST, {.i64=EQUISOLID}, 0, 0, FLAGS, "out" },
111113 { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },
112114 { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
113115 { "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, · 1705 unmodified lines
18191821 return visible;
18201822 }
18211823
1824+/**
1825+ * Prepare data for processing equisolid output format.
1826+ *
1827+ * @param ctx filter context
1828+ *
1829+ * @return error code
1830+ */
1831+static int prepare_equisolid_out(AVFilterContext *ctx)
1832+{
1833+ V360Context *s = ctx->priv;
1834+
1835+ s->flat_range[0] = sinf(FFMIN(s->h_fov, 359.f) * M_PI / 720.f);
1836+ s->flat_range[1] = sinf(FFMIN(s->v_fov, 359.f) * M_PI / 720.f);
1837+
1838+ return 0;
1839+}
1840+
1841+/**
1842+ * Calculate 3D coordinates on sphere for corresponding frame position in equisolid format.
1843+ *
1844+ * @param s filter private context
1845+ * @param i horizontal position on frame [0, width)
1846+ * @param j vertical position on frame [0, height)
1847+ * @param width frame width
1848+ * @param height frame height
1849+ * @param vec coordinates on sphere
1850+ */
1851+static int equisolid_to_xyz(const V360Context *s,
1852+ int i, int j, int width, int height,
1853+ float *vec)
1854+{
1855+ const float x = ((2.f * i + 1.f) / width - 1.f) * s->flat_range[0];
1856+ const float y = ((2.f * j + 1.f) / height - 1.f) * s->flat_range[1];
1857+ const float r = hypotf(x, y);
1858+ const float theta = asinf(r) * 2.f;
1859+ const float sin_theta = sinf(theta);
1860+
1861+ vec[0] = x / r * sin_theta;
1862+ vec[1] = y / r * sin_theta;
1863+ vec[2] = cosf(theta);
1864+
1865+ normalize_vector(vec);
1866+
1867+ return 1;
1868+}
1869+
1870+/**
1871+ * Prepare data for processing equisolid input format.
1872+ *
1873+ * @param ctx filter context
1874+ *
1875+ * @return error code
1876+ */
1877+static int prepare_equisolid_in(AVFilterContext *ctx)
1878+{
1879+ V360Context *s = ctx->priv;
1880+
1881+ s->iflat_range[0] = sinf(FFMIN(s->ih_fov, 359.f) * M_PI / 720.f);
1882+ s->iflat_range[1] = sinf(FFMIN(s->iv_fov, 359.f) * M_PI / 720.f);
1883+
1884+ return 0;
1885+}
1886+
1887+/**
1888+ * Calculate frame position in equisolid format for corresponding 3D coordinates on sphere.
1889+ *
1890+ * @param s filter private context
1891+ * @param vec coordinates on sphere
1892+ * @param width frame width
1893+ * @param height frame height
1894+ * @param us horizontal coordinates for interpolation window
1895+ * @param vs vertical coordinates for interpolation window
1896+ * @param du horizontal relative coordinate
1897+ * @param dv vertical relative coordinate
1898+ */
1899+static int xyz_to_equisolid(const V360Context *s,
1900+ const float *vec, int width, int height,
1901+ int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1902+{
1903+ const float theta = acosf(vec[2]);
1904+ const float r = sinf(theta * 0.5f);
1905+ const float c = r / hypotf(vec[0], vec[1]);
1906+ const float x = vec[0] * c / s->iflat_range[0] * s->input_mirror_modifier[0];
1907+ const float y = vec[1] * c / s->iflat_range[1] * s->input_mirror_modifier[1];
1908+
1909+ const float uf = (x + 1.f) * width / 2.f;
1910+ const float vf = (y + 1.f) * height / 2.f;
1911+
1912+ const int ui = floorf(uf);
1913+ const int vi = floorf(vf);
1914+
1915+ const int visible = isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
1916+
1917+ *du = visible ? uf - ui : 0.f;
1918+ *dv = visible ? vf - vi : 0.f;
1919+
1920+ for (int i = 0; i < 4; i++) {
1921+ for (int j = 0; j < 4; j++) {
1922+ us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
1923+ vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
1924+ }
1925+ }
1926+
1927+ return visible;
1928+}
1929+
18221930 /**
18231931 * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
18241932 *
@@ -3644,6 +3752,15 @@ static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int siz · 1819 unmodified lines
36443752 static void fov_from_dfov(int format, float d_fov, float w, float h, float *h_fov, float *v_fov)
36453753 {
36463754 switch (format) {
3755+ case EQUISOLID:
3756+ {
3757+ const float d = 0.5f * hypotf(w, h);
3758+ const float l = d / (sinf(d_fov * M_PI / 720.f));
3759+
3760+ *h_fov = 2.f * asinf(w * 0.5f / l) * 360.f / M_PI;
3761+ *v_fov = 2.f * asinf(h * 0.5f / l) * 360.f / M_PI;
3762+ }
3763+ break;
36473764 case STEREOGRAPHIC:
36483765 {
36493766 const float d = 0.5f * hypotf(w, h);
@@ -4014,6 +4131,12 @@ static int config_output(AVFilterLink *outlink) · 364 unmodified lines
40144131 wf = w * 2.f;
40154132 hf = h;
40164133 break;
4134+ case EQUISOLID:
4135+ s->in_transform = xyz_to_equisolid;
4136+ err = prepare_equisolid_in(ctx);
4137+ wf = w;
4138+ hf = h / 2.f;
4139+ break;
40174140 default:
40184141 av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
40194142 return AVERROR_BUG;
@@ -4150,6 +4273,12 @@ static int config_output(AVFilterLink *outlink) · 130 unmodified lines
41504273 w = lrintf(wf / 2.f);
41514274 h = lrintf(hf);
41524275 break;
4276+ case EQUISOLID:
4277+ s->out_transform = equisolid_to_xyz;
4278+ prepare_out = prepare_equisolid_out;
4279+ w = lrintf(wf);
4280+ h = lrintf(hf * 2.f);
4281+ break;
41534282 default:
41544283 av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
41554284 return AVERROR_BUG;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t