X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavfilter%2Fvf_v360.c;h=053ea8868eb4dfbf383c30f187bb279189e7f2b1;hb=c1fd4033dca192d709ff01203aa833256df5b70f;hp=824f0e615495390b79df211abfeb63dbd99743b9;hpb=a086b73e1f7ae90f7ab17b3a0c40c40c1ff2c8a0;p=ffmpeg diff --git a/libavfilter/vf_v360.c b/libavfilter/vf_v360.c index 824f0e61549..053ea8868eb 100644 --- a/libavfilter/vf_v360.c +++ b/libavfilter/vf_v360.c @@ -148,8 +148,8 @@ static const AVOption v360_options[] = { { "pitch", "pitch rotation", OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "pitch"}, { "roll", "roll rotation", OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "roll"}, { "rorder", "rotation order", OFFSET(rorder), AV_OPT_TYPE_STRING, {.str="ypr"}, 0, 0,TFLAGS, "rorder"}, - { "h_fov", "output horizontal field of view",OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f,TFLAGS, "h_fov"}, - { "v_fov", "output vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f,TFLAGS, "v_fov"}, + { "h_fov", "output horizontal field of view",OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "h_fov"}, + { "v_fov", "output vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "v_fov"}, { "d_fov", "output diagonal field of view", OFFSET(d_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "d_fov"}, { "h_flip", "flip out video horizontally", OFFSET(h_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "h_flip"}, { "v_flip", "flip out video vertically", OFFSET(v_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "v_flip"}, @@ -158,8 +158,8 @@ static const AVOption v360_options[] = { { "iv_flip", "flip in video vertically", OFFSET(iv_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "iv_flip"}, { "in_trans", "transpose video input", OFFSET(in_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "in_transpose"}, { "out_trans", "transpose video output", OFFSET(out_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "out_transpose"}, - { "ih_fov", "input horizontal field of view",OFFSET(ih_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f,TFLAGS, "ih_fov"}, - { "iv_fov", "input vertical field of view", OFFSET(iv_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f,TFLAGS, "iv_fov"}, + { "ih_fov", "input horizontal field of view",OFFSET(ih_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "ih_fov"}, + { "iv_fov", "input vertical field of view", OFFSET(iv_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "iv_fov"}, { "id_fov", "input diagonal field of view", OFFSET(id_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "id_fov"}, {"alpha_mask", "build mask in alpha plane", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "alpha"}, { NULL } @@ -1728,6 +1728,23 @@ static int xyz_to_cube6x1(const V360Context *s, return 1; } +/** + * Prepare data for processing equirectangular output format. + * + * @param ctx filter context + * + * @return error code + */ +static int prepare_equirect_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 * M_PI / 360.f; + + return 0; +} + /** * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format. * @@ -1742,8 +1759,8 @@ static int equirect_to_xyz(const V360Context *s, int i, int j, int width, int height, float *vec) { - const float phi = ((2.f * i + 0.5f) / width - 1.f) * M_PI; - const float theta = ((2.f * j + 0.5f) / height - 1.f) * M_PI_2; + const float phi = ((2.f * i + 0.5f) / width - 1.f) * s->flat_range[0]; + const float theta = ((2.f * j + 0.5f) / height - 1.f) * s->flat_range[1]; const float sin_phi = sinf(phi); const float cos_phi = cosf(phi); @@ -2103,6 +2120,23 @@ static int xyz_to_orthographic(const V360Context *s, 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. * @@ -2122,15 +2156,18 @@ static int xyz_to_equirect(const V360Context *s, const float phi = atan2f(vec[0], vec[2]); const float theta = asinf(vec[1]); - const float uf = (phi / M_PI + 1.f) * width / 2.f; - const float vf = (theta / M_PI_2 + 1.f) * height / 2.f; + 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); @@ -2138,7 +2175,7 @@ static int xyz_to_equirect(const V360Context *s, } } - return 1; + return visible; } /** @@ -3133,8 +3170,8 @@ static int perspective_to_xyz(const V360Context *s, const float cos_theta = cosf(theta); vec[0] = cos_theta * sin_phi; - vec[1] = sin_theta; - vec[2] = cos_theta * cos_phi; + vec[1] = cos_theta * cos_phi; + vec[2] = sin_theta; } else { vec[0] = 0.f; vec[1] = 1.f; @@ -3142,7 +3179,6 @@ static int perspective_to_xyz(const V360Context *s, return 0; } - normalize_vector(vec); return 1; } @@ -3922,7 +3958,7 @@ static inline void rotate(const float rot_quaternion[2][4], { float qv[4], temp[4], rqv[4]; - qv[0] = 0; + qv[0] = 0.f; qv[1] = vec[0]; qv[2] = vec[1]; qv[3] = vec[2]; @@ -4004,6 +4040,10 @@ static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int siz static void fov_from_dfov(int format, float d_fov, float w, float h, float *h_fov, float *v_fov) { switch (format) { + case EQUIRECTANGULAR: + *h_fov = d_fov; + *v_fov = d_fov * 0.5f; + break; case ORTHOGRAPHIC: { const float d = 0.5f * hypotf(w, h); @@ -4038,18 +4078,18 @@ static void fov_from_dfov(int format, float d_fov, float w, float h, float *h_fo break; case DUAL_FISHEYE: { - const float d = 0.5f * hypotf(w * 0.5f, h); + const float d = hypotf(w * 0.5f, h); - *h_fov = d / w * 2.f * d_fov; - *v_fov = d / h * d_fov; + *h_fov = 0.5f * w / d * d_fov; + *v_fov = h / d * d_fov; } break; case FISHEYE: { - const float d = 0.5f * hypotf(w, h); + const float d = hypotf(w, h); - *h_fov = d / w * d_fov; - *v_fov = d / h * d_fov; + *h_fov = w / d * d_fov; + *v_fov = h / d * d_fov; } break; case FLAT: @@ -4146,6 +4186,10 @@ static int config_output(AVFilterLink *outlink) const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); const int depth = desc->comp[0].depth; const int sizeof_mask = s->mask_size = (depth + 7) >> 3; + float default_h_fov = 360.f; + float default_v_fov = 180.f; + float default_ih_fov = 360.f; + float default_iv_fov = 180.f; int sizeof_uv; int sizeof_ker; int err; @@ -4275,6 +4319,29 @@ static int config_output(AVFilterLink *outlink) s->in_width = s->inplanewidth[0]; s->in_height = s->inplaneheight[0]; + switch (s->in) { + case CYLINDRICAL: + case FLAT: + default_ih_fov = 90.f; + default_iv_fov = 45.f; + break; + case EQUISOLID: + case ORTHOGRAPHIC: + case STEREOGRAPHIC: + case DUAL_FISHEYE: + case FISHEYE: + default_ih_fov = 180.f; + default_iv_fov = 180.f; + default: + break; + } + + if (s->ih_fov == 0.f) + s->ih_fov = default_ih_fov; + + if (s->iv_fov == 0.f) + s->iv_fov = default_iv_fov; + if (s->id_fov > 0.f) fov_from_dfov(s->in, s->id_fov, w, h, &s->ih_fov, &s->iv_fov); @@ -4284,7 +4351,7 @@ static int config_output(AVFilterLink *outlink) switch (s->in) { case EQUIRECTANGULAR: s->in_transform = xyz_to_equirect; - err = 0; + err = prepare_equirect_in(ctx); wf = w; hf = h; break; @@ -4435,7 +4502,7 @@ static int config_output(AVFilterLink *outlink) switch (s->out) { case EQUIRECTANGULAR: s->out_transform = equirect_to_xyz; - prepare_out = NULL; + prepare_out = prepare_equirect_out; w = lrintf(wf); h = lrintf(hf); break; @@ -4608,6 +4675,30 @@ static int config_output(AVFilterLink *outlink) s->width = w; s->height = h; + switch (s->out) { + case CYLINDRICAL: + case FLAT: + default_h_fov = 90.f; + default_v_fov = 45.f; + break; + case EQUISOLID: + case ORTHOGRAPHIC: + case STEREOGRAPHIC: + case DUAL_FISHEYE: + case FISHEYE: + default_h_fov = 180.f; + default_v_fov = 180.f; + break; + default: + break; + } + + if (s->h_fov == 0.f) + s->h_fov = default_h_fov; + + if (s->v_fov == 0.f) + s->v_fov = default_v_fov; + if (s->d_fov > 0.f) fov_from_dfov(s->out, s->d_fov, w, h, &s->h_fov, &s->v_fov); @@ -4670,13 +4761,8 @@ static int config_output(AVFilterLink *outlink) return err; } - s->rot_quaternion[0][0] = 1.f; - s->rot_quaternion[0][1] = s->rot_quaternion[0][2] = s->rot_quaternion[0][3] = 0.f; - - for (int i = 0; i < 4; i++) { - calculate_rotation(s->yaw * 0.25f, s->pitch * 0.25f, s->roll * 0.25f, - s->rot_quaternion, s->rotation_order); - } + calculate_rotation(s->yaw, s->pitch, s->roll, + s->rot_quaternion, s->rotation_order); set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, s->output_mirror_modifier); @@ -4712,8 +4798,11 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags) { + V360Context *s = ctx->priv; int ret; + s->yaw = s->pitch = s->roll = 0.f; + ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags); if (ret < 0) return ret; @@ -4721,6 +4810,16 @@ static int process_command(AVFilterContext *ctx, const char *cmd, const char *ar return config_output(ctx->outputs[0]); } +static av_cold int init(AVFilterContext *ctx) +{ + V360Context *s = ctx->priv; + + s->rot_quaternion[0][0] = 1.f; + s->rot_quaternion[0][1] = s->rot_quaternion[0][2] = s->rot_quaternion[0][3] = 0.f; + + return 0; +} + static av_cold void uninit(AVFilterContext *ctx) { V360Context *s = ctx->priv; @@ -4762,6 +4861,7 @@ AVFilter ff_vf_v360 = { .name = "v360", .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."), .priv_size = sizeof(V360Context), + .init = init, .uninit = uninit, .query_formats = query_formats, .inputs = inputs,