diff options
author | Paul B Mahol <onemda@gmail.com> | 2021-06-30 00:12:41 +0200 |
---|---|---|
committer | Paul B Mahol <onemda@gmail.com> | 2021-06-30 00:12:41 +0200 |
commit | e11fd47f8df2631db9da14b22d34e3c390d91ba8 (patch) | |
tree | c9fde52a78b1dc434cd89940249fbbbe34acfd58 | |
parent | cea05864e65db9a2dc8af82b2c63fb8f03c5f876 (diff) | |
download | ffmpeg-e11fd47f8df2631db9da14b22d34e3c390d91ba8.tar.gz |
avfilter/vf_v360: add cylindrical equal area format
-rw-r--r-- | doc/filters.texi | 4 | ||||
-rw-r--r-- | libavfilter/v360.h | 1 | ||||
-rw-r--r-- | libavfilter/vf_v360.c | 124 |
3 files changed, 129 insertions, 0 deletions
diff --git a/doc/filters.texi b/doc/filters.texi index 67ad228d8b..13236965ce 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -20977,6 +20977,10 @@ If diagonal field of view is set it overrides horizontal and vertical field of v Octahedron projection. @end table +@item cylindricalea +Cylindrical Equal Area projection. +@end table + @item interp Set interpolation method.@* @i{Note: more complex interpolation methods require much more memory to run.} diff --git a/libavfilter/v360.h b/libavfilter/v360.h index 87770664a3..b8ade2591c 100644 --- a/libavfilter/v360.h +++ b/libavfilter/v360.h @@ -54,6 +54,7 @@ enum Projections { EQUISOLID, ORTHOGRAPHIC, OCTAHEDRON, + CYLINDRICALEA, NB_PROJECTIONS, }; diff --git a/libavfilter/vf_v360.c b/libavfilter/vf_v360.c index c47a4015e8..0c47883d4d 100644 --- a/libavfilter/vf_v360.c +++ b/libavfilter/vf_v360.c @@ -84,6 +84,7 @@ static const AVOption v360_options[] = { { "equisolid", "equisolid", 0, AV_OPT_TYPE_CONST, {.i64=EQUISOLID}, 0, 0, FLAGS, "in" }, { "og", "orthographic", 0, AV_OPT_TYPE_CONST, {.i64=ORTHOGRAPHIC}, 0, 0, FLAGS, "in" }, {"octahedron", "octahedron", 0, AV_OPT_TYPE_CONST, {.i64=OCTAHEDRON}, 0, 0, FLAGS, "in" }, + {"cylindricalea", "cylindrical equal area", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICALEA}, 0, 0, FLAGS, "in" }, { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" }, { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" }, { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" }, @@ -114,6 +115,7 @@ static const AVOption v360_options[] = { { "equisolid", "equisolid", 0, AV_OPT_TYPE_CONST, {.i64=EQUISOLID}, 0, 0, FLAGS, "out" }, { "og", "orthographic", 0, AV_OPT_TYPE_CONST, {.i64=ORTHOGRAPHIC}, 0, 0, FLAGS, "out" }, {"octahedron", "octahedron", 0, AV_OPT_TYPE_CONST, {.i64=OCTAHEDRON}, 0, 0, FLAGS, "out" }, + {"cylindricalea", "cylindrical equal area", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICALEA}, 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" }, @@ -3137,6 +3139,116 @@ static int xyz_to_cylindrical(const V360Context *s, } /** + * Prepare data for processing cylindrical equal area output format. + * + * @param ctx filter context + * + * @return error code + */ +static int prepare_cylindricalea_out(AVFilterContext *ctx) +{ + V360Context *s = ctx->priv; + + s->flat_range[0] = s->h_fov * M_PI / 360.f; + s->flat_range[1] = s->v_fov / 180.f; + + return 0; +} + +/** + * Prepare data for processing cylindrical equal area input format. + * + * @param ctx filter context + * + * @return error code + */ +static int prepare_cylindricalea_in(AVFilterContext *ctx) +{ + V360Context *s = ctx->priv; + + s->iflat_range[0] = M_PI * s->ih_fov / 360.f; + s->iflat_range[1] = s->iv_fov / 180.f; + + return 0; +} + +/** + * Calculate 3D coordinates on sphere for corresponding frame position in cylindrical equal area 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 cylindricalea_to_xyz(const V360Context *s, + int i, int j, int width, int height, + float *vec) +{ + const float uf = s->flat_range[0] * ((2.f * i + 1.f) / width - 1.f); + const float vf = s->flat_range[1] * ((2.f * j + 1.f) / height - 1.f); + + const float phi = uf; + const float theta = asinf(vf); + + const float sin_phi = sinf(phi); + const float cos_phi = cosf(phi); + const float sin_theta = sinf(theta); + const float cos_theta = cosf(theta); + + vec[0] = cos_theta * sin_phi; + vec[1] = sin_theta; + vec[2] = cos_theta * cos_phi; + + normalize_vector(vec); + + return 1; +} + +/** + * Calculate frame position in cylindrical equal area 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_cylindricalea(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]) / s->iflat_range[0]; + const float theta = asinf(vec[1]); + + const float uf = (phi + 1.f) * (width - 1) / 2.f; + const float vf = (sinf(theta) / s->iflat_range[1] + 1.f) * height / 2.f; + + const int ui = floorf(uf); + const int vi = floorf(vf); + + const int visible = vi >= 0 && vi < height && ui >= 0 && ui < width && + theta <= M_PI * s->iv_fov / 180.f && + theta >= -M_PI * s->iv_fov / 180.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 3D coordinates on sphere for corresponding frame position in perspective format. * * @param s filter private context @@ -4448,6 +4560,12 @@ static int config_output(AVFilterLink *outlink) wf = w; hf = h * 2.f; break; + case CYLINDRICALEA: + s->in_transform = xyz_to_cylindricalea; + err = prepare_cylindricalea_in(ctx); + wf = w; + hf = h; + break; case TETRAHEDRON: s->in_transform = xyz_to_tetrahedron; err = 0; @@ -4596,6 +4714,12 @@ static int config_output(AVFilterLink *outlink) w = lrintf(wf); h = lrintf(hf * 0.5f); break; + case CYLINDRICALEA: + s->out_transform = cylindricalea_to_xyz; + prepare_out = prepare_cylindricalea_out; + w = lrintf(wf); + h = lrintf(hf); + break; case PERSPECTIVE: s->out_transform = perspective_to_xyz; prepare_out = NULL; |