History

avfilter/vf_v360: add orthographic projection support

Paul B Mahol committed 6 years ago

Commit 00a5df71 parent 44ce3333 files · +156 −2Browse 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 +135 −2
libavfilter/v360.h+1−0
@@ -52,6 +52,7 @@ enum Projections { · 51 unmodified lines
5252 TSPYRAMID,
5353 HEQUIRECTANGULAR,
5454 EQUISOLID,
55+ ORTHOGRAPHIC,
5556 NB_PROJECTIONS,
5657 };
5758
libavfilter/vf_v360.c+135−2
@@ -82,6 +82,7 @@ static const AVOption v360_options[] = { · 81 unmodified lines
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" },
8484 { "equisolid", "equisolid", 0, AV_OPT_TYPE_CONST, {.i64=EQUISOLID}, 0, 0, FLAGS, "in" },
85+ { "og", "orthographic", 0, AV_OPT_TYPE_CONST, {.i64=ORTHOGRAPHIC}, 0, 0, FLAGS, "in" },
8586 { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" },
8687 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
8788 { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
@@ -110,6 +111,7 @@ static const AVOption v360_options[] = { · 22 unmodified lines
110111 { "hequirect", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "out" },
111112 { "he", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "out" },
112113 { "equisolid", "equisolid", 0, AV_OPT_TYPE_CONST, {.i64=EQUISOLID}, 0, 0, FLAGS, "out" },
114+ { "og", "orthographic", 0, AV_OPT_TYPE_CONST, {.i64=ORTHOGRAPHIC}, 0, 0, FLAGS, "out" },
113115 { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },
114116 { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
115117 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
@@ -1832,8 +1834,8 @@ static int prepare_equisolid_out(AVFilterContext *ctx) · 1716 unmodified lines
18321834 {
18331835 V360Context *s = ctx->priv;
18341836
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+ s->flat_range[0] = sinf(s->h_fov * M_PI / 720.f);
1838+ s->flat_range[1] = sinf(s->v_fov * M_PI / 720.f);
18371839
18381840 return 0;
18391841 }
@@ -1927,6 +1929,111 @@ static int xyz_to_equisolid(const V360Context *s, · 87 unmodified lines
19271929 return visible;
19281930 }
19291931
1932+/**
1933+ * Prepare data for processing orthographic output format.
1934+ *
1935+ * @param ctx filter context
1936+ *
1937+ * @return error code
1938+ */
1939+static int prepare_orthographic_out(AVFilterContext *ctx)
1940+{
1941+ V360Context *s = ctx->priv;
1942+
1943+ s->flat_range[0] = sinf(FFMIN(s->h_fov, 180.f) * M_PI / 360.f);
1944+ s->flat_range[1] = sinf(FFMIN(s->v_fov, 180.f) * M_PI / 360.f);
1945+
1946+ return 0;
1947+}
1948+
1949+/**
1950+ * Calculate 3D coordinates on sphere for corresponding frame position in orthographic format.
1951+ *
1952+ * @param s filter private context
1953+ * @param i horizontal position on frame [0, width)
1954+ * @param j vertical position on frame [0, height)
1955+ * @param width frame width
1956+ * @param height frame height
1957+ * @param vec coordinates on sphere
1958+ */
1959+static int orthographic_to_xyz(const V360Context *s,
1960+ int i, int j, int width, int height,
1961+ float *vec)
1962+{
1963+ const float x = ((2.f * i + 1.f) / width - 1.f) * s->flat_range[0];
1964+ const float y = ((2.f * j + 1.f) / height - 1.f) * s->flat_range[1];
1965+ const float r = hypotf(x, y);
1966+ const float theta = asinf(r);
1967+
1968+ vec[0] = x;
1969+ vec[1] = y;
1970+ vec[2] = cosf(theta);
1971+
1972+ normalize_vector(vec);
1973+
1974+ return 1;
1975+}
1976+
1977+/**
1978+ * Prepare data for processing orthographic input format.
1979+ *
1980+ * @param ctx filter context
1981+ *
1982+ * @return error code
1983+ */
1984+static int prepare_orthographic_in(AVFilterContext *ctx)
1985+{
1986+ V360Context *s = ctx->priv;
1987+
1988+ s->iflat_range[0] = sinf(FFMIN(s->ih_fov, 180.f) * M_PI / 360.f);
1989+ s->iflat_range[1] = sinf(FFMIN(s->iv_fov, 180.f) * M_PI / 360.f);
1990+
1991+ return 0;
1992+}
1993+
1994+/**
1995+ * Calculate frame position in orthographic format for corresponding 3D coordinates on sphere.
1996+ *
1997+ * @param s filter private context
1998+ * @param vec coordinates on sphere
1999+ * @param width frame width
2000+ * @param height frame height
2001+ * @param us horizontal coordinates for interpolation window
2002+ * @param vs vertical coordinates for interpolation window
2003+ * @param du horizontal relative coordinate
2004+ * @param dv vertical relative coordinate
2005+ */
2006+static int xyz_to_orthographic(const V360Context *s,
2007+ const float *vec, int width, int height,
2008+ int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2009+{
2010+ const float theta = acosf(vec[2]);
2011+ const float r = sinf(theta);
2012+ const float c = r / hypotf(vec[0], vec[1]);
2013+ const float x = vec[0] * c / s->iflat_range[0] * s->input_mirror_modifier[0];
2014+ const float y = vec[1] * c / s->iflat_range[1] * s->input_mirror_modifier[1];
2015+
2016+ const float uf = (x + 1.f) * width / 2.f;
2017+ const float vf = (y + 1.f) * height / 2.f;
2018+
2019+ const int ui = floorf(uf);
2020+ const int vi = floorf(vf);
2021+
2022+ const int visible = vec[2] >= 0.f && isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
2023+
2024+ *du = visible ? uf - ui : 0.f;
2025+ *dv = visible ? vf - vi : 0.f;
2026+
2027+ for (int i = 0; i < 4; i++) {
2028+ for (int j = 0; j < 4; j++) {
2029+ us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
2030+ vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
2031+ }
2032+ }
2033+
2034+ return visible;
2035+}
2036+
19302037 /**
19312038 * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
19322039 *
@@ -3752,6 +3859,20 @@ static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int siz · 1819 unmodified lines
37523859 static void fov_from_dfov(int format, float d_fov, float w, float h, float *h_fov, float *v_fov)
37533860 {
37543861 switch (format) {
3862+ case ORTHOGRAPHIC:
3863+ {
3864+ const float d = 0.5f * hypotf(w, h);
3865+ const float l = sinf(d_fov * M_PI / 360.f) / d;
3866+
3867+ *h_fov = asinf(w * 0.5 * l) * 360.f / M_PI;
3868+ *v_fov = asinf(h * 0.5 * l) * 360.f / M_PI;
3869+
3870+ if (d_fov > 180.f) {
3871+ *h_fov = 180.f - *h_fov;
3872+ *v_fov = 180.f - *v_fov;
3873+ }
3874+ }
3875+ break;
37553876 case EQUISOLID:
37563877 {
37573878 const float d = 0.5f * hypotf(w, h);
@@ -4137,6 +4258,12 @@ static int config_output(AVFilterLink *outlink) · 379 unmodified lines
41374258 wf = w;
41384259 hf = h / 2.f;
41394260 break;
4261+ case ORTHOGRAPHIC:
4262+ s->in_transform = xyz_to_orthographic;
4263+ err = prepare_orthographic_in(ctx);
4264+ wf = w;
4265+ hf = h / 2.f;
4266+ break;
41404267 default:
41414268 av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
41424269 return AVERROR_BUG;
@@ -4279,6 +4406,12 @@ static int config_output(AVFilterLink *outlink) · 136 unmodified lines
42794406 w = lrintf(wf);
42804407 h = lrintf(hf * 2.f);
42814408 break;
4409+ case ORTHOGRAPHIC:
4410+ s->out_transform = orthographic_to_xyz;
4411+ prepare_out = prepare_orthographic_out;
4412+ w = lrintf(wf);
4413+ h = lrintf(hf * 2.f);
4414+ break;
42824415 default:
42834416 av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
42844417 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