+static int orthographic_to_xyz(const V360Context *s,
+ int i, int j, int width, int height,
+ float *vec)
+{
+ const float x = ((2.f * i + 1.f) / width - 1.f) * s->flat_range[0];
+ const float y = ((2.f * j + 1.f) / height - 1.f) * s->flat_range[1];
+ const float r = hypotf(x, y);
+ const float theta = asinf(r);
+
+ vec[0] = x;
+ vec[1] = y;
+ vec[2] = cosf(theta);
+
+ normalize_vector(vec);
+
+ return 1;
+}
+
+/**
+ * Prepare data for processing orthographic input format.
+ *
+ * @param ctx filter context
+ *
+ * @return error code
+ */
+static int prepare_orthographic_in(AVFilterContext *ctx)
+{
+ V360Context *s = ctx->priv;
+
+ s->iflat_range[0] = sinf(FFMIN(s->ih_fov, 180.f) * M_PI / 360.f);
+ s->iflat_range[1] = sinf(FFMIN(s->iv_fov, 180.f) * M_PI / 360.f);
+
+ return 0;
+}
+
+/**
+ * Calculate frame position in orthographic format for corresponding 3D coordinates on sphere.
+ *
+ * @param s filter private context
+ * @param vec coordinates on sphere
+ * @param width frame width
+ * @param height frame height
+ * @param us horizontal coordinates for interpolation window
+ * @param vs vertical coordinates for interpolation window
+ * @param du horizontal relative coordinate
+ * @param dv vertical relative coordinate
+ */
+static int xyz_to_orthographic(const V360Context *s,
+ const float *vec, int width, int height,
+ int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
+{
+ const float theta = acosf(vec[2]);
+ const float r = sinf(theta);
+ const float c = r / hypotf(vec[0], vec[1]);
+ const float x = vec[0] * c / s->iflat_range[0];
+ const float y = vec[1] * c / s->iflat_range[1];
+
+ const float uf = (x + 1.f) * width / 2.f;
+ const float vf = (y + 1.f) * height / 2.f;
+
+ const int ui = floorf(uf);
+ const int vi = floorf(vf);
+
+ const int visible = vec[2] >= 0.f && isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
+
+ *du = visible ? uf - ui : 0.f;
+ *dv = visible ? vf - vi : 0.f;
+
+ for (int i = 0; i < 4; i++) {
+ for (int j = 0; j < 4; j++) {
+ us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
+ vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
+ }
+ }
+
+ return visible;
+}
+
+/**
+ * Prepare data for processing equirectangular input format.
+ *
+ * @param ctx filter context
+ *
+ * @return error code
+ */
+static int prepare_equirect_in(AVFilterContext *ctx)
+{
+ V360Context *s = ctx->priv;
+
+ s->iflat_range[0] = s->ih_fov * M_PI / 360.f;
+ s->iflat_range[1] = s->iv_fov * M_PI / 360.f;
+
+ return 0;
+}
+
+/**
+ * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
+ *
+ * @param s filter private context
+ * @param vec coordinates on sphere
+ * @param width frame width
+ * @param height frame height
+ * @param us horizontal coordinates for interpolation window
+ * @param vs vertical coordinates for interpolation window
+ * @param du horizontal relative coordinate
+ * @param dv vertical relative coordinate
+ */
+static int xyz_to_equirect(const V360Context *s,
+ const float *vec, int width, int height,
+ int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
+{
+ const float phi = atan2f(vec[0], vec[2]);
+ const float theta = asinf(vec[1]);
+
+ const float uf = (phi / s->iflat_range[0] + 1.f) * width / 2.f;
+ const float vf = (theta / s->iflat_range[1] + 1.f) * height / 2.f;
+
+ const int ui = floorf(uf);
+ const int vi = floorf(vf);
+ int visible;
+
+ *du = uf - ui;
+ *dv = vf - vi;
+
+ visible = vi >= 0 && vi < height && ui >= 0 && ui < width;
+
+ for (int i = 0; i < 4; i++) {
+ for (int j = 0; j < 4; j++) {
+ us[i][j] = ereflectx(ui + j - 1, vi + i - 1, width, height);
+ vs[i][j] = reflecty(vi + i - 1, height);
+ }
+ }
+
+ return visible;
+}
+
+/**
+ * Calculate frame position in half equirectangular format for corresponding 3D coordinates on sphere.
+ *
+ * @param s filter private context
+ * @param vec coordinates on sphere
+ * @param width frame width
+ * @param height frame height
+ * @param us horizontal coordinates for interpolation window
+ * @param vs vertical coordinates for interpolation window
+ * @param du horizontal relative coordinate
+ * @param dv vertical relative coordinate
+ */
+static int xyz_to_hequirect(const V360Context *s,
+ const float *vec, int width, int height,
+ int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
+{
+ const float phi = atan2f(vec[0], vec[2]);
+ const float theta = asinf(vec[1]);
+
+ const float uf = (phi / M_PI_2 + 1.f) * width / 2.f;
+ const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
+
+ const int ui = floorf(uf);
+ const int vi = floorf(vf);
+
+ const int visible = phi >= -M_PI_2 && phi <= M_PI_2;
+
+ *du = uf - ui;
+ *dv = vf - vi;
+
+ for (int i = 0; i < 4; i++) {
+ for (int j = 0; j < 4; j++) {
+ us[i][j] = av_clip(ui + j - 1, 0, width - 1);
+ vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
+ }
+ }
+
+ return visible;
+}
+
+/**
+ * Prepare data for processing flat input format.
+ *
+ * @param ctx filter context
+ *
+ * @return error code
+ */
+static int prepare_flat_in(AVFilterContext *ctx)
+{
+ V360Context *s = ctx->priv;
+
+ s->iflat_range[0] = tanf(0.5f * s->ih_fov * M_PI / 180.f);
+ s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
+
+ return 0;
+}
+
+/**
+ * Calculate frame position in flat format for corresponding 3D coordinates on sphere.
+ *
+ * @param s filter private context
+ * @param vec coordinates on sphere
+ * @param width frame width
+ * @param height frame height
+ * @param us horizontal coordinates for interpolation window
+ * @param vs vertical coordinates for interpolation window
+ * @param du horizontal relative coordinate
+ * @param dv vertical relative coordinate
+ */
+static int xyz_to_flat(const V360Context *s,
+ const float *vec, int width, int height,
+ int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
+{
+ const float theta = acosf(vec[2]);
+ const float r = tanf(theta);
+ const float rr = fabsf(r) < 1e+6f ? r : hypotf(width, height);
+ const float zf = vec[2];
+ const float h = hypotf(vec[0], vec[1]);
+ const float c = h <= 1e-6f ? 1.f : rr / h;
+ float uf = vec[0] * c / s->iflat_range[0];
+ float vf = vec[1] * c / s->iflat_range[1];
+ int visible, ui, vi;
+
+ uf = zf >= 0.f ? (uf + 1.f) * width / 2.f : 0.f;
+ vf = zf >= 0.f ? (vf + 1.f) * height / 2.f : 0.f;
+
+ ui = floorf(uf);
+ vi = floorf(vf);
+
+ visible = vi >= 0 && vi < height && ui >= 0 && ui < width && zf >= 0.f;
+
+ *du = uf - ui;
+ *dv = vf - vi;
+
+ for (int i = 0; i < 4; i++) {
+ for (int j = 0; j < 4; j++) {
+ us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
+ vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
+ }
+ }
+
+ return visible;
+}
+
+/**
+ * Calculate frame position in mercator format for corresponding 3D coordinates on sphere.
+ *
+ * @param s filter private context
+ * @param vec coordinates on sphere
+ * @param width frame width
+ * @param height frame height
+ * @param us horizontal coordinates for interpolation window
+ * @param vs vertical coordinates for interpolation window
+ * @param du horizontal relative coordinate
+ * @param dv vertical relative coordinate
+ */
+static int xyz_to_mercator(const V360Context *s,
+ const float *vec, int width, int height,
+ int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
+{
+ const float phi = atan2f(vec[0], vec[2]);
+ const float theta = vec[1];
+
+ const float uf = (phi / M_PI + 1.f) * width / 2.f;
+ const float vf = (av_clipf(logf((1.f + theta) / (1.f - theta)) / (2.f * M_PI), -1.f, 1.f) + 1.f) * height / 2.f;
+
+ const int ui = floorf(uf);
+ const int vi = floorf(vf);
+
+ *du = uf - ui;
+ *dv = vf - vi;
+
+ for (int i = 0; i < 4; i++) {
+ for (int j = 0; j < 4; j++) {
+ us[i][j] = av_clip(ui + j - 1, 0, width - 1);
+ vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
+ }
+ }
+
+ return 1;
+}
+
+/**
+ * Calculate 3D coordinates on sphere for corresponding frame position in mercator 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 int mercator_to_xyz(const V360Context *s,
+ int i, int j, int width, int height,