History

avfilter/vf_v360: allow user to control fov for equirectagular format

It may be useful to use different values from typical 360/180 deg.

Paul B Mahol committed 5 years ago

Commit 1050f941 parent 282682a1 file · +103 −11Browse files

Showing the changes to libavfilter (1 of 1 file). Show all

Unified · Split

libavfilter/vf_v360.c+103−11
@@ -148,8 +148,8 @@ static const AVOption v360_options[] = { · 147 unmodified lines
148148 { "pitch", "pitch rotation", OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "pitch"},
149149 { "roll", "roll rotation", OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "roll"},
150150 { "rorder", "rotation order", OFFSET(rorder), AV_OPT_TYPE_STRING, {.str="ypr"}, 0, 0,TFLAGS, "rorder"},
151− { "h_fov", "output horizontal field of view",OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f,TFLAGS, "h_fov"},
152− { "v_fov", "output vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f,TFLAGS, "v_fov"},
151+ { "h_fov", "output horizontal field of view",OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "h_fov"},
152+ { "v_fov", "output vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "v_fov"},
153153 { "d_fov", "output diagonal field of view", OFFSET(d_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "d_fov"},
154154 { "h_flip", "flip out video horizontally", OFFSET(h_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "h_flip"},
155155 { "v_flip", "flip out video vertically", OFFSET(v_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "v_flip"},
@@ -158,8 +158,8 @@ static const AVOption v360_options[] = { · 2 unmodified lines
158158 { "iv_flip", "flip in video vertically", OFFSET(iv_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "iv_flip"},
159159 { "in_trans", "transpose video input", OFFSET(in_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "in_transpose"},
160160 { "out_trans", "transpose video output", OFFSET(out_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "out_transpose"},
161− { "ih_fov", "input horizontal field of view",OFFSET(ih_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f,TFLAGS, "ih_fov"},
162− { "iv_fov", "input vertical field of view", OFFSET(iv_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f,TFLAGS, "iv_fov"},
161+ { "ih_fov", "input horizontal field of view",OFFSET(ih_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "ih_fov"},
162+ { "iv_fov", "input vertical field of view", OFFSET(iv_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "iv_fov"},
163163 { "id_fov", "input diagonal field of view", OFFSET(id_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "id_fov"},
164164 {"alpha_mask", "build mask in alpha plane", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "alpha"},
165165 { NULL }
@@ -1728,6 +1728,23 @@ static int xyz_to_cube6x1(const V360Context *s, · 1562 unmodified lines
17281728 return 1;
17291729 }
17301730
1731+/**
1732+ * Prepare data for processing equirectangular output format.
1733+ *
1734+ * @param ctx filter context
1735+ *
1736+ * @return error code
1737+ */
1738+static int prepare_equirect_out(AVFilterContext *ctx)
1739+{
1740+ V360Context *s = ctx->priv;
1741+
1742+ s->flat_range[0] = s->h_fov * M_PI / 360.f;
1743+ s->flat_range[1] = s->v_fov * M_PI / 360.f;
1744+
1745+ return 0;
1746+}
1747+
17311748 /**
17321749 * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
17331750 *
@@ -1742,8 +1759,8 @@ static int equirect_to_xyz(const V360Context *s, · 8 unmodified lines
17421759 int i, int j, int width, int height,
17431760 float *vec)
17441761 {
1745− const float phi = ((2.f * i + 0.5f) / width - 1.f) * M_PI;
1746− const float theta = ((2.f * j + 0.5f) / height - 1.f) * M_PI_2;
1762+ const float phi = ((2.f * i + 0.5f) / width - 1.f) * s->flat_range[0];
1763+ const float theta = ((2.f * j + 0.5f) / height - 1.f) * s->flat_range[1];
17471764
17481765 const float sin_phi = sinf(phi);
17491766 const float cos_phi = cosf(phi);
@@ -2103,6 +2120,23 @@ static int xyz_to_orthographic(const V360Context *s, · 353 unmodified lines
21032120 return visible;
21042121 }
21052122
2123+/**
2124+ * Prepare data for processing equirectangular input format.
2125+ *
2126+ * @param ctx filter context
2127+ *
2128+ * @return error code
2129+ */
2130+static int prepare_equirect_in(AVFilterContext *ctx)
2131+{
2132+ V360Context *s = ctx->priv;
2133+
2134+ s->iflat_range[0] = s->ih_fov * M_PI / 360.f;
2135+ s->iflat_range[1] = s->iv_fov * M_PI / 360.f;
2136+
2137+ return 0;
2138+}
2139+
21062140 /**
21072141 * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
21082142 *
@@ -2122,15 +2156,18 @@ static int xyz_to_equirect(const V360Context *s, · 13 unmodified lines
21222156 const float phi = atan2f(vec[0], vec[2]);
21232157 const float theta = asinf(vec[1]);
21242158
2125− const float uf = (phi / M_PI + 1.f) * width / 2.f;
2126− const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
2159+ const float uf = (phi / s->iflat_range[0] + 1.f) * width / 2.f;
2160+ const float vf = (theta / s->iflat_range[1] + 1.f) * height / 2.f;
21272161
21282162 const int ui = floorf(uf);
21292163 const int vi = floorf(vf);
2164+ int visible;
21302165
21312166 *du = uf - ui;
21322167 *dv = vf - vi;
21332168
2169+ visible = vi >= 0 && vi < height && ui >= 0 && ui < width;
2170+
21342171 for (int i = 0; i < 4; i++) {
21352172 for (int j = 0; j < 4; j++) {
21362173 us[i][j] = ereflectx(ui + j - 1, vi + i - 1, width, height);
@@ -2138,7 +2175,7 @@ static int xyz_to_equirect(const V360Context *s, · 1 unmodified line
21382175 }
21392176 }
21402177
2141− return 1;
2178+ return visible;
21422179 }
21432180
21442181 /**
@@ -4003,6 +4040,10 @@ static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int siz · 1858 unmodified lines
40034040 static void fov_from_dfov(int format, float d_fov, float w, float h, float *h_fov, float *v_fov)
40044041 {
40054042 switch (format) {
4043+ case EQUIRECTANGULAR:
4044+ *h_fov = d_fov;
4045+ *v_fov = d_fov * 0.5f;
4046+ break;
40064047 case ORTHOGRAPHIC:
40074048 {
40084049 const float d = 0.5f * hypotf(w, h);
@@ -4145,6 +4186,10 @@ static int config_output(AVFilterLink *outlink) · 136 unmodified lines
41454186 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
41464187 const int depth = desc->comp[0].depth;
41474188 const int sizeof_mask = s->mask_size = (depth + 7) >> 3;
4189+ float default_h_fov = 360.f;
4190+ float default_v_fov = 180.f;
4191+ float default_ih_fov = 360.f;
4192+ float default_iv_fov = 180.f;
41484193 int sizeof_uv;
41494194 int sizeof_ker;
41504195 int err;
@@ -4274,6 +4319,29 @@ static int config_output(AVFilterLink *outlink) · 123 unmodified lines
42744319 s->in_width = s->inplanewidth[0];
42754320 s->in_height = s->inplaneheight[0];
42764321
4322+ switch (s->in) {
4323+ case CYLINDRICAL:
4324+ case FLAT:
4325+ default_ih_fov = 90.f;
4326+ default_iv_fov = 45.f;
4327+ break;
4328+ case EQUISOLID:
4329+ case ORTHOGRAPHIC:
4330+ case STEREOGRAPHIC:
4331+ case DUAL_FISHEYE:
4332+ case FISHEYE:
4333+ default_ih_fov = 180.f;
4334+ default_iv_fov = 180.f;
4335+ default:
4336+ break;
4337+ }
4338+
4339+ if (s->ih_fov == 0.f)
4340+ s->ih_fov = default_ih_fov;
4341+
4342+ if (s->iv_fov == 0.f)
4343+ s->iv_fov = default_iv_fov;
4344+
42774345 if (s->id_fov > 0.f)
42784346 fov_from_dfov(s->in, s->id_fov, w, h, &s->ih_fov, &s->iv_fov);
42794347
@@ -4283,7 +4351,7 @@ static int config_output(AVFilterLink *outlink) · 3 unmodified lines
42834351 switch (s->in) {
42844352 case EQUIRECTANGULAR:
42854353 s->in_transform = xyz_to_equirect;
4286− err = 0;
4354+ err = prepare_equirect_in(ctx);
42874355 wf = w;
42884356 hf = h;
42894357 break;
@@ -4434,7 +4502,7 @@ static int config_output(AVFilterLink *outlink) · 144 unmodified lines
44344502 switch (s->out) {
44354503 case EQUIRECTANGULAR:
44364504 s->out_transform = equirect_to_xyz;
4437− prepare_out = NULL;
4505+ prepare_out = prepare_equirect_out;
44384506 w = lrintf(wf);
44394507 h = lrintf(hf);
44404508 break;
@@ -4607,6 +4675,30 @@ static int config_output(AVFilterLink *outlink) · 166 unmodified lines
46074675 s->width = w;
46084676 s->height = h;
46094677
4678+ switch (s->out) {
4679+ case CYLINDRICAL:
4680+ case FLAT:
4681+ default_h_fov = 90.f;
4682+ default_v_fov = 45.f;
4683+ break;
4684+ case EQUISOLID:
4685+ case ORTHOGRAPHIC:
4686+ case STEREOGRAPHIC:
4687+ case DUAL_FISHEYE:
4688+ case FISHEYE:
4689+ default_h_fov = 180.f;
4690+ default_v_fov = 180.f;
4691+ break;
4692+ default:
4693+ break;
4694+ }
4695+
4696+ if (s->h_fov == 0.f)
4697+ s->h_fov = default_h_fov;
4698+
4699+ if (s->v_fov == 0.f)
4700+ s->v_fov = default_v_fov;
4701+
46104702 if (s->d_fov > 0.f)
46114703 fov_from_dfov(s->out, s->d_fov, w, h, &s->h_fov, &s->v_fov);
46124704

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