From ec4f764249817a93787d76990dc4618d59e70f5d Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Wed, 22 Jan 2020 22:21:32 +0100 Subject: avfilter/vf_v360: add tetrahedron format --- doc/filters.texi | 2 ++ libavfilter/v360.h | 1 + libavfilter/vf_v360.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) diff --git a/doc/filters.texi b/doc/filters.texi index 40db2e5c54..a9ae75f0c0 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -19043,6 +19043,8 @@ Format specific options: Set perspective parameter. @end table +@item tetrahedron +Tetrahedron projection. @end table @item interp diff --git a/libavfilter/v360.h b/libavfilter/v360.h index ba3d0da60f..8e080f0b6c 100644 --- a/libavfilter/v360.h +++ b/libavfilter/v360.h @@ -47,6 +47,7 @@ enum Projections { PANNINI, CYLINDRICAL, PERSPECTIVE, + TETRAHEDRON, NB_PROJECTIONS, }; diff --git a/libavfilter/vf_v360.c b/libavfilter/vf_v360.c index 2ed0acb5a7..1173b33ae1 100644 --- a/libavfilter/vf_v360.c +++ b/libavfilter/vf_v360.c @@ -74,6 +74,7 @@ static const AVOption v360_options[] = { {"sinusoidal", "sinusoidal", 0, AV_OPT_TYPE_CONST, {.i64=SINUSOIDAL}, 0, 0, FLAGS, "in" }, { "fisheye", "fisheye", 0, AV_OPT_TYPE_CONST, {.i64=FISHEYE}, 0, 0, FLAGS, "in" }, {"cylindrical", "cylindrical", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICAL}, 0, 0, FLAGS, "in" }, + {"tetrahedron", "tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=TETRAHEDRON}, 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" }, @@ -96,6 +97,7 @@ static const AVOption v360_options[] = { { "pannini", "pannini", 0, AV_OPT_TYPE_CONST, {.i64=PANNINI}, 0, 0, FLAGS, "out" }, {"cylindrical", "cylindrical", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICAL}, 0, 0, FLAGS, "out" }, {"perspective", "perspective", 0, AV_OPT_TYPE_CONST, {.i64=PERSPECTIVE}, 0, 0, FLAGS, "out" }, + {"tetrahedron", "tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=TETRAHEDRON}, 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" }, @@ -2589,6 +2591,88 @@ static void perspective_to_xyz(const V360Context *s, normalize_vector(vec); } +/** + * Calculate 3D coordinates on sphere for corresponding frame position in tetrahedron 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 void tetrahedron_to_xyz(const V360Context *s, + int i, int j, int width, int height, + float *vec) +{ + const float uf = (float)i / width; + const float vf = (float)j / height; + + vec[0] = uf < 0.5f ? uf * 4.f - 1.f : 3.f - uf * 4.f; + vec[1] = 1.f - vf * 2.f; + vec[2] = 2.f * fabsf(1.f - fabsf(1.f - uf * 2.f + vf)) - 1.f; + + normalize_vector(vec); +} + +/** + * Calculate frame position in tetrahedron 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 void xyz_to_tetrahedron(const V360Context *s, + const float *vec, int width, int height, + int16_t us[4][4], int16_t vs[4][4], float *du, float *dv) +{ + float d = 0.5f * (vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]); + + const float d0 = (vec[0] * 0.5f + vec[1] * 0.5f + vec[2] *-0.5f) / d; + const float d1 = (vec[0] *-0.5f + vec[1] *-0.5f + vec[2] *-0.5f) / d; + const float d2 = (vec[0] * 0.5f + vec[1] *-0.5f + vec[2] * 0.5f) / d; + const float d3 = (vec[0] *-0.5f + vec[1] * 0.5f + vec[2] * 0.5f) / d; + + float uf, vf, x, y, z; + int ui, vi; + + d = FFMAX(d0, FFMAX3(d1, d2, d3)); + + x = vec[0] / d; + y = vec[1] / d; + z = -vec[2] / d; + + vf = 0.5f - y * 0.5f; + + if ((x + y >= 0.f && y + z >= 0.f && -z - x <= 0.f) || + (x + y <= 0.f && -y + z >= 0.f && z - x >= 0.f)) { + uf = 0.25f * x + 0.25f; + } else { + uf = 0.75f - 0.25f * x; + } + + uf *= width - 1; + vf *= height - 1; + + ui = floorf(uf); + vi = floorf(vf); + + *du = uf - ui; + *dv = vf - vi; + + for (int i = -1; i < 3; i++) { + for (int j = -1; j < 3; j++) { + us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1); + vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1); + } + } +} + /** * Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format. * @@ -3206,6 +3290,12 @@ static int config_output(AVFilterLink *outlink) wf = w; hf = h * 2.f; break; + case TETRAHEDRON: + s->in_transform = xyz_to_tetrahedron; + err = 0; + wf = w; + hf = h; + break; default: av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n"); return AVERROR_BUG; @@ -3318,6 +3408,12 @@ static int config_output(AVFilterLink *outlink) w = lrintf(wf / 2.f); h = lrintf(hf); break; + case TETRAHEDRON: + s->out_transform = tetrahedron_to_xyz; + prepare_out = NULL; + w = lrintf(wf); + h = lrintf(hf); + break; default: av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n"); return AVERROR_BUG; -- cgit v1.2.3