diff --git a/doc/filters.texi b/doc/filters.texi index 252eb4b7ad..237e182aa8 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -19003,6 +19003,15 @@ Set horizontal/vertical/diagonal field of view. Values in degrees. If diagonal field of view is set it overrides horizontal and vertical field of view. @end table +@item perspective +Perspective projection. @i{(output only)} + +Format specific options: +@table @option +@item v_fov +Set perspective parameter. +@end table + @end table @item interp diff --git a/libavfilter/v360.h b/libavfilter/v360.h index 29bf27565c..88de9cbd7c 100644 --- a/libavfilter/v360.h +++ b/libavfilter/v360.h @@ -46,6 +46,7 @@ enum Projections { FISHEYE, PANNINI, CYLINDRICAL, + PERSPECTIVE, NB_PROJECTIONS, }; diff --git a/libavfilter/vf_v360.c b/libavfilter/vf_v360.c index 452f52dce6..bc10ced55d 100644 --- a/libavfilter/vf_v360.c +++ b/libavfilter/vf_v360.c @@ -90,6 +90,7 @@ static const AVOption v360_options[] = { { "fisheye", "fisheye", 0, AV_OPT_TYPE_CONST, {.i64=FISHEYE}, 0, 0, FLAGS, "out" }, { "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" }, { "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" }, @@ -2334,6 +2335,51 @@ static void cylindrical_to_xyz(const V360Context *s, normalize_vector(vec); } +/** + * Calculate 3D coordinates on sphere for corresponding frame position in perspective 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 perspective_to_xyz(const V360Context *s, + int i, int j, int width, int height, + float *vec) +{ + const float uf = ((2.f * i) / width - 1.f); + const float vf = ((2.f * j) / height - 1.f); + const float rh = hypotf(uf, vf); + const float sinzz = 1.f - rh * rh; + const float h = 1.f + s->v_fov; + const float sinz = (h - sqrtf(sinzz)) / (h / rh + rh / h); + const float sinz2 = sinz * sinz; + + if (sinz2 <= 1.f) { + const float cosz = sqrtf(1.f - sinz2); + + const float theta = asinf(cosz); + const float phi = atan2f(uf, 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; + } else { + vec[0] = 0.f; + vec[1] = -1.f; + vec[2] = 0.f; + } + + normalize_vector(vec); +} + /** * Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format. * @@ -2884,6 +2930,7 @@ static int config_output(AVFilterLink *outlink) wf = w; hf = h / 9.f * 8.f; break; + case PERSPECTIVE: case CYLINDRICAL: case PANNINI: case FISHEYE: @@ -3038,6 +3085,12 @@ static int config_output(AVFilterLink *outlink) w = roundf(wf); h = roundf(hf * 0.5f); break; + case PERSPECTIVE: + s->out_transform = perspective_to_xyz; + prepare_out = NULL; + w = roundf(wf / 2.f); + h = roundf(hf); + break; default: av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n"); return AVERROR_BUG;