diff --git a/doc/filters.texi b/doc/filters.texi index 1f65b097d6..2cc0d9dfc5 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -18063,6 +18063,9 @@ Mercator format. @item ball Ball format, gives significant distortion toward the back. +@item hammer +Hammer-Aitoff map projection format. + @end table @item interp diff --git a/libavfilter/v360.h b/libavfilter/v360.h index 70b30264a0..aa0bb948d5 100644 --- a/libavfilter/v360.h +++ b/libavfilter/v360.h @@ -41,6 +41,7 @@ enum Projections { STEREOGRAPHIC, MERCATOR, BALL, + HAMMER, NB_PROJECTIONS, }; diff --git a/libavfilter/vf_v360.c b/libavfilter/vf_v360.c index 4f7d22f11e..d966723770 100644 --- a/libavfilter/vf_v360.c +++ b/libavfilter/vf_v360.c @@ -67,6 +67,7 @@ static const AVOption v360_options[] = { { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "in" }, { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "in" }, { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "in" }, + { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 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" }, @@ -83,6 +84,7 @@ static const AVOption v360_options[] = { { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "out" }, { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "out" }, { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "out" }, + { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 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" }, @@ -1658,6 +1660,83 @@ static void ball_to_xyz(const V360Context *s, } } +/** + * Calculate 3D coordinates on sphere for corresponding frame position in hammer 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 hammer_to_xyz(const V360Context *s, + int i, int j, int width, int height, + float *vec) +{ + const float x = ((2.f * i) / width - 1.f); + const float y = ((2.f * j) / height - 1.f); + + const float xx = x * x; + const float yy = y * y; + + const float z = sqrtf(1.f - xx * 0.5f - yy * 0.5f); + + const float a = M_SQRT2 * x * z; + const float b = 2.f * z * z - 1.f; + + const float aa = a * a; + const float bb = b * b; + + const float w = sqrtf(1.f - 2.f * yy * z * z); + + vec[0] = w * 2.f * a * b / (aa + bb); + vec[1] = -M_SQRT2 * y * z; + vec[2] = -w * (bb - aa) / (aa + bb); + + normalize_vector(vec); +} + +/** + * Calculate frame position in hammer 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_hammer(const V360Context *s, + const float *vec, int width, int height, + uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv) +{ + const float theta = atan2f(vec[0], -vec[2]) * s->input_mirror_modifier[0]; + + const float z = sqrtf(1.f + sqrtf(1.f - vec[1] * vec[1]) * cosf(theta * 0.5f)); + const float x = sqrtf(1.f - vec[1] * vec[1]) * sinf(theta * 0.5f) / z; + const float y = -vec[1] / z * s->input_mirror_modifier[1]; + float uf, vf; + int ui, vi; + + uf = (x + 1.f) * width / 2.f; + vf = (y + 1.f) * height / 2.f; + 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] = mod(ui + j, width); + vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1); + } + } +} + /** * Prepare data for processing equi-angular cubemap input format. * @@ -2512,6 +2591,12 @@ static int config_output(AVFilterLink *outlink) wf = w; hf = h / 2.f; break; + case HAMMER: + s->in_transform = xyz_to_hammer; + err = 0; + wf = w; + hf = h; + break; default: av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n"); return AVERROR_BUG; @@ -2588,6 +2673,12 @@ static int config_output(AVFilterLink *outlink) w = roundf(wf); h = roundf(hf * 2.f); break; + case HAMMER: + s->out_transform = hammer_to_xyz; + prepare_out = NULL; + w = roundf(wf); + h = roundf(hf); + break; default: av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n"); return AVERROR_BUG;