| @@ -0,0 +1,1857 @@ |
| | 1 | /* |
| | 2 | * Copyright (c) 2019 Eugene Lyapustin |
| | 3 | * |
| | 4 | * This file is part of FFmpeg. |
| | 5 | * |
| | 6 | * FFmpeg is free software; you can redistribute it and/or |
| | 7 | * modify it under the terms of the GNU Lesser General Public |
| | 8 | * License as published by the Free Software Foundation; either |
| | 9 | * version 2.1 of the License, or (at your option) any later version. |
| | 10 | * |
| | 11 | * FFmpeg is distributed in the hope that it will be useful, |
| | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| | 14 | * Lesser General Public License for more details. |
| | 15 | * |
| | 16 | * You should have received a copy of the GNU Lesser General Public |
| | 17 | * License along with FFmpeg; if not, write to the Free Software |
| | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| | 19 | */ |
| | 20 | |
| | 21 | /** |
| | 22 | * @file |
| | 23 | * 360 video conversion filter. |
| | 24 | * Principle of operation: |
| | 25 | * |
| | 26 | * (for each pixel in output frame)\n |
| | 27 | * 1) Calculate OpenGL-like coordinates (x, y, z) for pixel position (i, j)\n |
| | 28 | * 2) Apply 360 operations (rotation, mirror) to (x, y, z)\n |
| | 29 | * 3) Calculate pixel position (u, v) in input frame\n |
| | 30 | * 4) Calculate interpolation window and weight for each pixel |
| | 31 | * |
| | 32 | * (for each frame)\n |
| | 33 | * 5) Remap input frame to output frame using precalculated data\n |
| | 34 | */ |
| | 35 | |
| | 36 | #include "libavutil/eval.h" |
| | 37 | #include "libavutil/imgutils.h" |
| | 38 | #include "libavutil/pixdesc.h" |
| | 39 | #include "libavutil/opt.h" |
| | 40 | #include "avfilter.h" |
| | 41 | #include "formats.h" |
| | 42 | #include "internal.h" |
| | 43 | #include "video.h" |
| | 44 | |
| | 45 | enum Projections { |
| | 46 | EQUIRECTANGULAR, |
| | 47 | CUBEMAP_3_2, |
| | 48 | CUBEMAP_6_1, |
| | 49 | EQUIANGULAR, |
| | 50 | FLAT, |
| | 51 | NB_PROJECTIONS, |
| | 52 | }; |
| | 53 | |
| | 54 | enum InterpMethod { |
| | 55 | NEAREST, |
| | 56 | BILINEAR, |
| | 57 | BICUBIC, |
| | 58 | LANCZOS, |
| | 59 | NB_INTERP_METHODS, |
| | 60 | }; |
| | 61 | |
| | 62 | enum Faces { |
| | 63 | TOP_LEFT, |
| | 64 | TOP_MIDDLE, |
| | 65 | TOP_RIGHT, |
| | 66 | BOTTOM_LEFT, |
| | 67 | BOTTOM_MIDDLE, |
| | 68 | BOTTOM_RIGHT, |
| | 69 | NB_FACES, |
| | 70 | }; |
| | 71 | |
| | 72 | enum Direction { |
| | 73 | RIGHT, ///< Axis +X |
| | 74 | LEFT, ///< Axis -X |
| | 75 | UP, ///< Axis +Y |
| | 76 | DOWN, ///< Axis -Y |
| | 77 | FRONT, ///< Axis -Z |
| | 78 | BACK, ///< Axis +Z |
| | 79 | NB_DIRECTIONS, |
| | 80 | }; |
| | 81 | |
| | 82 | enum Rotation { |
| | 83 | ROT_0, |
| | 84 | ROT_90, |
| | 85 | ROT_180, |
| | 86 | ROT_270, |
| | 87 | NB_ROTATIONS, |
| | 88 | }; |
| | 89 | |
| | 90 | typedef struct V360Context { |
| | 91 | const AVClass *class; |
| | 92 | int in, out; |
| | 93 | int interp; |
| | 94 | int width, height; |
| | 95 | char* in_forder; |
| | 96 | char* out_forder; |
| | 97 | char* in_frot; |
| | 98 | char* out_frot; |
| | 99 | |
| | 100 | int in_cubemap_face_order[6]; |
| | 101 | int out_cubemap_direction_order[6]; |
| | 102 | int in_cubemap_face_rotation[6]; |
| | 103 | int out_cubemap_face_rotation[6]; |
| | 104 | |
| | 105 | float yaw, pitch, roll; |
| | 106 | |
| | 107 | int h_flip, v_flip, d_flip; |
| | 108 | |
| | 109 | float h_fov, v_fov; |
| | 110 | float flat_range[3]; |
| | 111 | |
| | 112 | int planewidth[4], planeheight[4]; |
| | 113 | int inplanewidth[4], inplaneheight[4]; |
| | 114 | int nb_planes; |
| | 115 | |
| | 116 | void *remap[4]; |
| | 117 | |
| | 118 | int (*remap_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); |
| | 119 | } V360Context; |
| | 120 | |
| | 121 | typedef struct ThreadData { |
| | 122 | V360Context *s; |
| | 123 | AVFrame *in; |
| | 124 | AVFrame *out; |
| | 125 | int nb_planes; |
| | 126 | } ThreadData; |
| | 127 | |
| | 128 | #define OFFSET(x) offsetof(V360Context, x) |
| | 129 | #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM |
| | 130 | |
| | 131 | static const AVOption v360_options[] = { |
| | 132 | { "input", "set input projection", OFFSET(in), AV_OPT_TYPE_INT, {.i64=EQUIRECTANGULAR}, 0, NB_PROJECTIONS-1, FLAGS, "in" }, |
| | 133 | { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "in" }, |
| | 134 | { "c3x2", "cubemap3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "in" }, |
| | 135 | { "c6x1", "cubemap6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "in" }, |
| | 136 | { "eac", "equi-angular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "in" }, |
| | 137 | { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" }, |
| | 138 | { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" }, |
| | 139 | { "c3x2", "cubemap3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "out" }, |
| | 140 | { "c6x1", "cubemap6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "out" }, |
| | 141 | { "eac", "equi-angular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "out" }, |
| | 142 | { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" }, |
| | 143 | { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" }, |
| | 144 | { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" }, |
| | 145 | { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" }, |
| | 146 | { "line", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" }, |
| | 147 | { "linear", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" }, |
| | 148 | { "cube", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" }, |
| | 149 | { "cubic", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" }, |
| | 150 | { "lanc", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" }, |
| | 151 | { "lanczos", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" }, |
| | 152 | { "w", "output width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS, "w"}, |
| | 153 | { "h", "output height", OFFSET(height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS, "h"}, |
| | 154 | { "in_forder", "input cubemap face order", OFFSET(in_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "in_forder"}, |
| | 155 | {"out_forder", "output cubemap face order", OFFSET(out_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "out_forder"}, |
| | 156 | { "in_frot", "input cubemap face rotation", OFFSET(in_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "in_frot"}, |
| | 157 | { "out_frot", "output cubemap face rotation",OFFSET(out_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "out_frot"}, |
| | 158 | { "yaw", "yaw rotation", OFFSET(yaw), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "yaw"}, |
| | 159 | { "pitch", "pitch rotation", OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "pitch"}, |
| | 160 | { "roll", "roll rotation", OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f, FLAGS, "roll"}, |
| | 161 | { "h_fov", "horizontal field of view", OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.f, 180.f, FLAGS, "h_fov"}, |
| | 162 | { "v_fov", "vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.f, 90.f, FLAGS, "v_fov"}, |
| | 163 | { "h_flip", "flip video horizontally", OFFSET(h_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "h_flip"}, |
| | 164 | { "v_flip", "flip video vertically", OFFSET(v_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "v_flip"}, |
| | 165 | { "d_flip", "flip video indepth", OFFSET(d_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "d_flip"}, |
| | 166 | { NULL } |
| | 167 | }; |
| | 168 | |
| | 169 | AVFILTER_DEFINE_CLASS(v360); |
| | 170 | |
| | 171 | static int query_formats(AVFilterContext *ctx) |
| | 172 | { |
| | 173 | static const enum AVPixelFormat pix_fmts[] = { |
| | 174 | // YUVA444 |
| | 175 | AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9, |
| | 176 | AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, |
| | 177 | AV_PIX_FMT_YUVA444P16, |
| | 178 | |
| | 179 | // YUVA422 |
| | 180 | AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9, |
| | 181 | AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, |
| | 182 | AV_PIX_FMT_YUVA422P16, |
| | 183 | |
| | 184 | // YUVA420 |
| | 185 | AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9, |
| | 186 | AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16, |
| | 187 | |
| | 188 | // YUVJ |
| | 189 | AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, |
| | 190 | AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, |
| | 191 | AV_PIX_FMT_YUVJ411P, |
| | 192 | |
| | 193 | // YUV444 |
| | 194 | AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P9, |
| | 195 | AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12, |
| | 196 | AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16, |
| | 197 | |
| | 198 | // YUV440 |
| | 199 | AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV440P10, |
| | 200 | AV_PIX_FMT_YUV440P12, |
| | 201 | |
| | 202 | // YUV422 |
| | 203 | AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P9, |
| | 204 | AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12, |
| | 205 | AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16, |
| | 206 | |
| | 207 | // YUV420 |
| | 208 | AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P9, |
| | 209 | AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12, |
| | 210 | AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16, |
| | 211 | |
| | 212 | // YUV411 |
| | 213 | AV_PIX_FMT_YUV411P, |
| | 214 | |
| | 215 | // YUV410 |
| | 216 | AV_PIX_FMT_YUV410P, |
| | 217 | |
| | 218 | // GBR |
| | 219 | AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, |
| | 220 | AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, |
| | 221 | AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16, |
| | 222 | |
| | 223 | // GBRA |
| | 224 | AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, |
| | 225 | AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16, |
| | 226 | |
| | 227 | // GRAY |
| | 228 | AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, |
| | 229 | AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, |
| | 230 | AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16, |
| | 231 | |
| | 232 | AV_PIX_FMT_NONE |
| | 233 | }; |
| | 234 | |
| | 235 | AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts); |
| | 236 | if (!fmts_list) |
| | 237 | return AVERROR(ENOMEM); |
| | 238 | return ff_set_common_formats(ctx, fmts_list); |
| | 239 | } |
| | 240 | |
| | 241 | typedef struct XYRemap1 { |
| | 242 | uint16_t u; |
| | 243 | uint16_t v; |
| | 244 | } XYRemap1; |
| | 245 | |
| | 246 | /** |
| | 247 | * Generate no-interpolation remapping function with a given pixel depth. |
| | 248 | * |
| | 249 | * @param bits number of bits per pixel |
| | 250 | * @param div number of bytes per pixel |
| | 251 | */ |
| | 252 | #define DEFINE_REMAP1(bits, div) \ |
| | 253 | static int remap1_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \ |
| | 254 | { \ |
| | 255 | ThreadData *td = (ThreadData*)arg; \ |
| | 256 | const V360Context *s = td->s; \ |
| | 257 | const AVFrame *in = td->in; \ |
| | 258 | AVFrame *out = td->out; \ |
| | 259 | \ |
| | 260 | int plane, x, y; \ |
| | 261 | \ |
| | 262 | for (plane = 0; plane < td->nb_planes; plane++) { \ |
| | 263 | const int in_linesize = in->linesize[plane] / div; \ |
| | 264 | const int out_linesize = out->linesize[plane] / div; \ |
| | 265 | const uint##bits##_t *src = (const uint##bits##_t *)in->data[plane]; \ |
| | 266 | uint##bits##_t *dst = (uint##bits##_t *)out->data[plane]; \ |
| | 267 | const XYRemap1 *remap = s->remap[plane]; \ |
| | 268 | const int width = s->planewidth[plane]; \ |
| | 269 | const int height = s->planeheight[plane]; \ |
| | 270 | \ |
| | 271 | const int slice_start = (height * jobnr ) / nb_jobs; \ |
| | 272 | const int slice_end = (height * (jobnr + 1)) / nb_jobs; \ |
| | 273 | \ |
| | 274 | for (y = slice_start; y < slice_end; y++) { \ |
| | 275 | uint##bits##_t *d = dst + y * out_linesize; \ |
| | 276 | for (x = 0; x < width; x++) { \ |
| | 277 | const XYRemap1 *r = &remap[y * width + x]; \ |
| | 278 | \ |
| | 279 | *d++ = src[r->v * in_linesize + r->u]; \ |
| | 280 | } \ |
| | 281 | } \ |
| | 282 | } \ |
| | 283 | \ |
| | 284 | return 0; \ |
| | 285 | } |
| | 286 | |
| | 287 | DEFINE_REMAP1( 8, 1) |
| | 288 | DEFINE_REMAP1(16, 2) |
| | 289 | |
| | 290 | typedef struct XYRemap2 { |
| | 291 | uint16_t u[2][2]; |
| | 292 | uint16_t v[2][2]; |
| | 293 | float ker[2][2]; |
| | 294 | } XYRemap2; |
| | 295 | |
| | 296 | typedef struct XYRemap4 { |
| | 297 | uint16_t u[4][4]; |
| | 298 | uint16_t v[4][4]; |
| | 299 | float ker[4][4]; |
| | 300 | } XYRemap4; |
| | 301 | |
| | 302 | /** |
| | 303 | * Generate remapping function with a given window size and pixel depth. |
| | 304 | * |
| | 305 | * @param window_size size of interpolation window |
| | 306 | * @param bits number of bits per pixel |
| | 307 | * @param div number of bytes per pixel |
| | 308 | */ |
| | 309 | #define DEFINE_REMAP(window_size, bits, div) \ |
| | 310 | static int remap##window_size##_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \ |
| | 311 | { \ |
| | 312 | ThreadData *td = (ThreadData*)arg; \ |
| | 313 | const V360Context *s = td->s; \ |
| | 314 | const AVFrame *in = td->in; \ |
| | 315 | AVFrame *out = td->out; \ |
| | 316 | \ |
| | 317 | int plane, x, y, i, j; \ |
| | 318 | \ |
| | 319 | for (plane = 0; plane < td->nb_planes; plane++) { \ |
| | 320 | const int in_linesize = in->linesize[plane] / div; \ |
| | 321 | const int out_linesize = out->linesize[plane] / div; \ |
| | 322 | const uint##bits##_t *src = (const uint##bits##_t *)in->data[plane]; \ |
| | 323 | uint##bits##_t *dst = (uint##bits##_t *)out->data[plane]; \ |
| | 324 | const XYRemap##window_size *remap = s->remap[plane]; \ |
| | 325 | const int width = s->planewidth[plane]; \ |
| | 326 | const int height = s->planeheight[plane]; \ |
| | 327 | \ |
| | 328 | const int slice_start = (height * jobnr ) / nb_jobs; \ |
| | 329 | const int slice_end = (height * (jobnr + 1)) / nb_jobs; \ |
| | 330 | \ |
| | 331 | for (y = slice_start; y < slice_end; y++) { \ |
| | 332 | uint##bits##_t *d = dst + y * out_linesize; \ |
| | 333 | for (x = 0; x < width; x++) { \ |
| | 334 | const XYRemap##window_size *r = &remap[y * width + x]; \ |
| | 335 | float tmp = 0.f; \ |
| | 336 | \ |
| | 337 | for (i = 0; i < window_size; i++) { \ |
| | 338 | for (j = 0; j < window_size; j++) { \ |
| | 339 | tmp += r->ker[i][j] * src[r->v[i][j] * in_linesize + r->u[i][j]]; \ |
| | 340 | } \ |
| | 341 | } \ |
| | 342 | \ |
| | 343 | *d++ = av_clip_uint##bits(roundf(tmp)); \ |
| | 344 | } \ |
| | 345 | } \ |
| | 346 | } \ |
| | 347 | \ |
| | 348 | return 0; \ |
| | 349 | } |
| | 350 | |
| | 351 | DEFINE_REMAP(2, 8, 1) |
| | 352 | DEFINE_REMAP(4, 8, 1) |
| | 353 | DEFINE_REMAP(2, 16, 2) |
| | 354 | DEFINE_REMAP(4, 16, 2) |
| | 355 | |
| | 356 | /** |
| | 357 | * Save nearest pixel coordinates for remapping. |
| | 358 | * |
| | 359 | * @param du horizontal relative coordinate |
| | 360 | * @param dv vertical relative coordinate |
| | 361 | * @param shift shift for remap array |
| | 362 | * @param r_tmp calculated 4x4 window |
| | 363 | * @param r_void remap data |
| | 364 | */ |
| | 365 | static void nearest_kernel(float du, float dv, int shift, const XYRemap4 *r_tmp, void *r_void) |
| | 366 | { |
| | 367 | XYRemap1 *r = (XYRemap1*)r_void + shift; |
| | 368 | const int i = roundf(dv) + 1; |
| | 369 | const int j = roundf(du) + 1; |
| | 370 | |
| | 371 | r->u = r_tmp->u[i][j]; |
| | 372 | r->v = r_tmp->v[i][j]; |
| | 373 | } |
| | 374 | |
| | 375 | /** |
| | 376 | * Calculate kernel for bilinear interpolation. |
| | 377 | * |
| | 378 | * @param du horizontal relative coordinate |
| | 379 | * @param dv vertical relative coordinate |
| | 380 | * @param shift shift for remap array |
| | 381 | * @param r_tmp calculated 4x4 window |
| | 382 | * @param r_void remap data |
| | 383 | */ |
| | 384 | static void bilinear_kernel(float du, float dv, int shift, const XYRemap4 *r_tmp, void *r_void) |
| | 385 | { |
| | 386 | XYRemap2 *r = (XYRemap2*)r_void + shift; |
| | 387 | int i, j; |
| | 388 | |
| | 389 | for (i = 0; i < 2; i++) { |
| | 390 | for (j = 0; j < 2; j++) { |
| | 391 | r->u[i][j] = r_tmp->u[i + 1][j + 1]; |
| | 392 | r->v[i][j] = r_tmp->v[i + 1][j + 1]; |
| | 393 | } |
| | 394 | } |
| | 395 | |
| | 396 | r->ker[0][0] = (1.f - du) * (1.f - dv); |
| | 397 | r->ker[0][1] = du * (1.f - dv); |
| | 398 | r->ker[1][0] = (1.f - du) * dv; |
| | 399 | r->ker[1][1] = du * dv; |
| | 400 | } |
| | 401 | |
| | 402 | /** |
| | 403 | * Calculate 1-dimensional cubic coefficients. |
| | 404 | * |
| | 405 | * @param t relative coordinate |
| | 406 | * @param coeffs coefficients |
| | 407 | */ |
| | 408 | static inline void calculate_bicubic_coeffs(float t, float *coeffs) |
| | 409 | { |
| | 410 | const float tt = t * t; |
| | 411 | const float ttt = t * t * t; |
| | 412 | |
| | 413 | coeffs[0] = - t / 3.f + tt / 2.f - ttt / 6.f; |
| | 414 | coeffs[1] = 1.f - t / 2.f - tt + ttt / 2.f; |
| | 415 | coeffs[2] = t + tt / 2.f - ttt / 2.f; |
| | 416 | coeffs[3] = - t / 6.f + ttt / 6.f; |
| | 417 | } |
| | 418 | |
| | 419 | /** |
| | 420 | * Calculate kernel for bicubic interpolation. |
| | 421 | * |
| | 422 | * @param du horizontal relative coordinate |
| | 423 | * @param dv vertical relative coordinate |
| | 424 | * @param shift shift for remap array |
| | 425 | * @param r_tmp calculated 4x4 window |
| | 426 | * @param r_void remap data |
| | 427 | */ |
| | 428 | static void bicubic_kernel(float du, float dv, int shift, const XYRemap4 *r_tmp, void *r_void) |
| | 429 | { |
| | 430 | XYRemap4 *r = (XYRemap4*)r_void + shift; |
| | 431 | int i, j; |
| | 432 | float du_coeffs[4]; |
| | 433 | float dv_coeffs[4]; |
| | 434 | |
| | 435 | calculate_bicubic_coeffs(du, du_coeffs); |
| | 436 | calculate_bicubic_coeffs(dv, dv_coeffs); |
| | 437 | |
| | 438 | for (i = 0; i < 4; i++) { |
| | 439 | for (j = 0; j < 4; j++) { |
| | 440 | r->u[i][j] = r_tmp->u[i][j]; |
| | 441 | r->v[i][j] = r_tmp->v[i][j]; |
| | 442 | r->ker[i][j] = du_coeffs[j] * dv_coeffs[i]; |
| | 443 | } |
| | 444 | } |
| | 445 | } |
| | 446 | |
| | 447 | /** |
| | 448 | * Calculate 1-dimensional lanczos coefficients. |
| | 449 | * |
| | 450 | * @param t relative coordinate |
| | 451 | * @param coeffs coefficients |
| | 452 | */ |
| | 453 | static inline void calculate_lanczos_coeffs(float t, float *coeffs) |
| | 454 | { |
| | 455 | int i; |
| | 456 | float sum = 0.f; |
| | 457 | |
| | 458 | for (i = 0; i < 4; i++) { |
| | 459 | const float x = M_PI * (t - i + 1); |
| | 460 | if (x == 0.f) { |
| | 461 | coeffs[i] = 1.f; |
| | 462 | } else { |
| | 463 | coeffs[i] = sinf(x) * sinf(x / 2.f) / (x * x / 2.f); |
| | 464 | } |
| | 465 | sum += coeffs[i]; |
| | 466 | } |
| | 467 | |
| | 468 | for (i = 0; i < 4; i++) { |
| | 469 | coeffs[i] /= sum; |
| | 470 | } |
| | 471 | } |
| | 472 | |
| | 473 | /** |
| | 474 | * Calculate kernel for lanczos interpolation. |
| | 475 | * |
| | 476 | * @param du horizontal relative coordinate |
| | 477 | * @param dv vertical relative coordinate |
| | 478 | * @param shift shift for remap array |
| | 479 | * @param r_tmp calculated 4x4 window |
| | 480 | * @param r_void remap data |
| | 481 | */ |
| | 482 | static void lanczos_kernel(float du, float dv, int shift, const XYRemap4 *r_tmp, void *r_void) |
| | 483 | { |
| | 484 | XYRemap4 *r = (XYRemap4*)r_void + shift; |
| | 485 | int i, j; |
| | 486 | float du_coeffs[4]; |
| | 487 | float dv_coeffs[4]; |
| | 488 | |
| | 489 | calculate_lanczos_coeffs(du, du_coeffs); |
| | 490 | calculate_lanczos_coeffs(dv, dv_coeffs); |
| | 491 | |
| | 492 | for (i = 0; i < 4; i++) { |
| | 493 | for (j = 0; j < 4; j++) { |
| | 494 | r->u[i][j] = r_tmp->u[i][j]; |
| | 495 | r->v[i][j] = r_tmp->v[i][j]; |
| | 496 | r->ker[i][j] = du_coeffs[j] * dv_coeffs[i]; |
| | 497 | } |
| | 498 | } |
| | 499 | } |
| | 500 | |
| | 501 | /** |
| | 502 | * Modulo operation with only positive remainders. |
| | 503 | * |
| | 504 | * @param a dividend |
| | 505 | * @param b divisor |
| | 506 | * |
| | 507 | * @return positive remainder of (a / b) |
| | 508 | */ |
| | 509 | static inline int mod(int a, int b) |
| | 510 | { |
| | 511 | const int res = a % b; |
| | 512 | if (res < 0) { |
| | 513 | return res + b; |
| | 514 | } else { |
| | 515 | return res; |
| | 516 | } |
| | 517 | } |
| | 518 | |
| | 519 | /** |
| | 520 | * Convert char to corresponding direction. |
| | 521 | * Used for cubemap options. |
| | 522 | */ |
| | 523 | static int get_direction(char c) |
| | 524 | { |
| | 525 | switch (c) { |
| | 526 | case 'r': |
| | 527 | return RIGHT; |
| | 528 | case 'l': |
| | 529 | return LEFT; |
| | 530 | case 'u': |
| | 531 | return UP; |
| | 532 | case 'd': |
| | 533 | return DOWN; |
| | 534 | case 'f': |
| | 535 | return FRONT; |
| | 536 | case 'b': |
| | 537 | return BACK; |
| | 538 | default: |
| | 539 | return -1; |
| | 540 | } |
| | 541 | } |
| | 542 | |
| | 543 | /** |
| | 544 | * Convert char to corresponding rotation angle. |
| | 545 | * Used for cubemap options. |
| | 546 | */ |
| | 547 | static int get_rotation(char c) |
| | 548 | { |
| | 549 | switch (c) { |
| | 550 | case '0': |
| | 551 | return ROT_0; |
| | 552 | case '1': |
| | 553 | return ROT_90; |
| | 554 | case '2': |
| | 555 | return ROT_180; |
| | 556 | case '3': |
| | 557 | return ROT_270; |
| | 558 | default: |
| | 559 | return -1; |
| | 560 | } |
| | 561 | } |
| | 562 | |
| | 563 | /** |
| | 564 | * Prepare data for processing cubemap input format. |
| | 565 | * |
| | 566 | * @param ctx filter context |
| | 567 | * |
| | 568 | * @return error code |
| | 569 | */ |
| | 570 | static int prepare_cube_in(AVFilterContext *ctx) |
| | 571 | { |
| | 572 | V360Context *s = ctx->priv; |
| | 573 | |
| | 574 | for (int face = 0; face < NB_FACES; face++) { |
| | 575 | const char c = s->in_forder[face]; |
| | 576 | int direction; |
| | 577 | |
| | 578 | if (c == '\0') { |
| | 579 | av_log(ctx, AV_LOG_ERROR, |
| | 580 | "Incomplete in_forder option. Direction for all 6 faces should be specified.\n"); |
| | 581 | return AVERROR(EINVAL); |
| | 582 | } |
| | 583 | |
| | 584 | direction = get_direction(c); |
| | 585 | if (direction == -1) { |
| | 586 | av_log(ctx, AV_LOG_ERROR, |
| | 587 | "Incorrect direction symbol '%c' in in_forder option.\n", c); |
| | 588 | return AVERROR(EINVAL); |
| | 589 | } |
| | 590 | |
| | 591 | s->in_cubemap_face_order[direction] = face; |
| | 592 | } |
| | 593 | |
| | 594 | for (int face = 0; face < NB_FACES; face++) { |
| | 595 | const char c = s->in_frot[face]; |
| | 596 | int rotation; |
| | 597 | |
| | 598 | if (c == '\0') { |
| | 599 | av_log(ctx, AV_LOG_ERROR, |
| | 600 | "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n"); |
| | 601 | return AVERROR(EINVAL); |
| | 602 | } |
| | 603 | |
| | 604 | rotation = get_rotation(c); |
| | 605 | if (rotation == -1) { |
| | 606 | av_log(ctx, AV_LOG_ERROR, |
| | 607 | "Incorrect rotation symbol '%c' in in_frot option.\n", c); |
| | 608 | return AVERROR(EINVAL); |
| | 609 | } |
| | 610 | |
| | 611 | s->in_cubemap_face_rotation[face] = rotation; |
| | 612 | } |
| | 613 | |
| | 614 | return 0; |
| | 615 | } |
| | 616 | |
| | 617 | /** |
| | 618 | * Prepare data for processing cubemap output format. |
| | 619 | * |
| | 620 | * @param ctx filter context |
| | 621 | * |
| | 622 | * @return error code |
| | 623 | */ |
| | 624 | static int prepare_cube_out(AVFilterContext *ctx) |
| | 625 | { |
| | 626 | V360Context *s = ctx->priv; |
| | 627 | |
| | 628 | for (int face = 0; face < NB_FACES; face++) { |
| | 629 | const char c = s->out_forder[face]; |
| | 630 | int direction; |
| | 631 | |
| | 632 | if (c == '\0') { |
| | 633 | av_log(ctx, AV_LOG_ERROR, |
| | 634 | "Incomplete out_forder option. Direction for all 6 faces should be specified.\n"); |
| | 635 | return AVERROR(EINVAL); |
| | 636 | } |
| | 637 | |
| | 638 | direction = get_direction(c); |
| | 639 | if (direction == -1) { |
| | 640 | av_log(ctx, AV_LOG_ERROR, |
| | 641 | "Incorrect direction symbol '%c' in out_forder option.\n", c); |
| | 642 | return AVERROR(EINVAL); |
| | 643 | } |
| | 644 | |
| | 645 | s->out_cubemap_direction_order[face] = direction; |
| | 646 | } |
| | 647 | |
| | 648 | for (int face = 0; face < NB_FACES; face++) { |
| | 649 | const char c = s->out_frot[face]; |
| | 650 | int rotation; |
| | 651 | |
| | 652 | if (c == '\0') { |
| | 653 | av_log(ctx, AV_LOG_ERROR, |
| | 654 | "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n"); |
| | 655 | return AVERROR(EINVAL); |
| | 656 | } |
| | 657 | |
| | 658 | rotation = get_rotation(c); |
| | 659 | if (rotation == -1) { |
| | 660 | av_log(ctx, AV_LOG_ERROR, |
| | 661 | "Incorrect rotation symbol '%c' in out_frot option.\n", c); |
| | 662 | return AVERROR(EINVAL); |
| | 663 | } |
| | 664 | |
| | 665 | s->out_cubemap_face_rotation[face] = rotation; |
| | 666 | } |
| | 667 | |
| | 668 | return 0; |
| | 669 | } |
| | 670 | |
| | 671 | static inline void rotate_cube_face(float *uf, float *vf, int rotation) |
| | 672 | { |
| | 673 | float tmp; |
| | 674 | |
| | 675 | switch (rotation) { |
| | 676 | case ROT_0: |
| | 677 | break; |
| | 678 | case ROT_90: |
| | 679 | tmp = *uf; |
| | 680 | *uf = -*vf; |
| | 681 | *vf = tmp; |
| | 682 | break; |
| | 683 | case ROT_180: |
| | 684 | *uf = -*uf; |
| | 685 | *vf = -*vf; |
| | 686 | break; |
| | 687 | case ROT_270: |
| | 688 | tmp = -*uf; |
| | 689 | *uf = *vf; |
| | 690 | *vf = tmp; |
| | 691 | break; |
| | 692 | } |
| | 693 | } |
| | 694 | |
| | 695 | static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation) |
| | 696 | { |
| | 697 | float tmp; |
| | 698 | |
| | 699 | switch (rotation) { |
| | 700 | case ROT_0: |
| | 701 | break; |
| | 702 | case ROT_90: |
| | 703 | tmp = -*uf; |
| | 704 | *uf = *vf; |
| | 705 | *vf = tmp; |
| | 706 | break; |
| | 707 | case ROT_180: |
| | 708 | *uf = -*uf; |
| | 709 | *vf = -*vf; |
| | 710 | break; |
| | 711 | case ROT_270: |
| | 712 | tmp = *uf; |
| | 713 | *uf = -*vf; |
| | 714 | *vf = tmp; |
| | 715 | break; |
| | 716 | } |
| | 717 | } |
| | 718 | |
| | 719 | /** |
| | 720 | * Calculate 3D coordinates on sphere for corresponding cubemap position. |
| | 721 | * Common operation for every cubemap. |
| | 722 | * |
| | 723 | * @param s filter context |
| | 724 | * @param uf horizontal cubemap coordinate [0, 1) |
| | 725 | * @param vf vertical cubemap coordinate [0, 1) |
| | 726 | * @param face face of cubemap |
| | 727 | * @param vec coordinates on sphere |
| | 728 | */ |
| | 729 | static void cube_to_xyz(const V360Context *s, |
| | 730 | float uf, float vf, int face, |
| | 731 | float *vec) |
| | 732 | { |
| | 733 | const int direction = s->out_cubemap_direction_order[face]; |
| | 734 | float norm; |
| | 735 | float l_x, l_y, l_z; |
| | 736 | |
| | 737 | rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]); |
| | 738 | |
| | 739 | switch (direction) { |
| | 740 | case RIGHT: |
| | 741 | l_x = 1.f; |
| | 742 | l_y = -vf; |
| | 743 | l_z = uf; |
| | 744 | break; |
| | 745 | case LEFT: |
| | 746 | l_x = -1.f; |
| | 747 | l_y = -vf; |
| | 748 | l_z = -uf; |
| | 749 | break; |
| | 750 | case UP: |
| | 751 | l_x = uf; |
| | 752 | l_y = 1.f; |
| | 753 | l_z = -vf; |
| | 754 | break; |
| | 755 | case DOWN: |
| | 756 | l_x = uf; |
| | 757 | l_y = -1.f; |
| | 758 | l_z = vf; |
| | 759 | break; |
| | 760 | case FRONT: |
| | 761 | l_x = uf; |
| | 762 | l_y = -vf; |
| | 763 | l_z = -1.f; |
| | 764 | break; |
| | 765 | case BACK: |
| | 766 | l_x = -uf; |
| | 767 | l_y = -vf; |
| | 768 | l_z = 1.f; |
| | 769 | break; |
| | 770 | } |
| | 771 | |
| | 772 | norm = sqrtf(l_x * l_x + l_y * l_y + l_z * l_z); |
| | 773 | vec[0] = l_x / norm; |
| | 774 | vec[1] = l_y / norm; |
| | 775 | vec[2] = l_z / norm; |
| | 776 | } |
| | 777 | |
| | 778 | /** |
| | 779 | * Calculate cubemap position for corresponding 3D coordinates on sphere. |
| | 780 | * Common operation for every cubemap. |
| | 781 | * |
| | 782 | * @param s filter context |
| | 783 | * @param vec coordinated on sphere |
| | 784 | * @param uf horizontal cubemap coordinate [0, 1) |
| | 785 | * @param vf vertical cubemap coordinate [0, 1) |
| | 786 | * @param direction direction of view |
| | 787 | */ |
| | 788 | static void xyz_to_cube(const V360Context *s, |
| | 789 | const float *vec, |
| | 790 | float *uf, float *vf, int *direction) |
| | 791 | { |
| | 792 | const float phi = atan2f(vec[0], -vec[2]); |
| | 793 | const float theta = asinf(-vec[1]); |
| | 794 | float phi_norm, theta_threshold; |
| | 795 | int face; |
| | 796 | |
| | 797 | if (phi >= -M_PI_4 && phi < M_PI_4) { |
| | 798 | *direction = FRONT; |
| | 799 | phi_norm = phi; |
| | 800 | } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) { |
| | 801 | *direction = LEFT; |
| | 802 | phi_norm = phi + M_PI_2; |
| | 803 | } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) { |
| | 804 | *direction = RIGHT; |
| | 805 | phi_norm = phi - M_PI_2; |
| | 806 | } else { |
| | 807 | *direction = BACK; |
| | 808 | phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI); |
| | 809 | } |
| | 810 | |
| | 811 | theta_threshold = atanf(cosf(phi_norm)); |
| | 812 | if (theta > theta_threshold) { |
| | 813 | *direction = DOWN; |
| | 814 | } else if (theta < -theta_threshold) { |
| | 815 | *direction = UP; |
| | 816 | } |
| | 817 | |
| | 818 | switch (*direction) { |
| | 819 | case RIGHT: |
| | 820 | *uf = vec[2] / vec[0]; |
| | 821 | *vf = -vec[1] / vec[0]; |
| | 822 | break; |
| | 823 | case LEFT: |
| | 824 | *uf = vec[2] / vec[0]; |
| | 825 | *vf = vec[1] / vec[0]; |
| | 826 | break; |
| | 827 | case UP: |
| | 828 | *uf = vec[0] / vec[1]; |
| | 829 | *vf = -vec[2] / vec[1]; |
| | 830 | break; |
| | 831 | case DOWN: |
| | 832 | *uf = -vec[0] / vec[1]; |
| | 833 | *vf = -vec[2] / vec[1]; |
| | 834 | break; |
| | 835 | case FRONT: |
| | 836 | *uf = -vec[0] / vec[2]; |
| | 837 | *vf = vec[1] / vec[2]; |
| | 838 | break; |
| | 839 | case BACK: |
| | 840 | *uf = -vec[0] / vec[2]; |
| | 841 | *vf = -vec[1] / vec[2]; |
| | 842 | break; |
| | 843 | } |
| | 844 | |
| | 845 | face = s->in_cubemap_face_order[*direction]; |
| | 846 | rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]); |
| | 847 | } |
| | 848 | |
| | 849 | /** |
| | 850 | * Find position on another cube face in case of overflow/underflow. |
| | 851 | * Used for calculation of interpolation window. |
| | 852 | * |
| | 853 | * @param s filter context |
| | 854 | * @param uf horizontal cubemap coordinate |
| | 855 | * @param vf vertical cubemap coordinate |
| | 856 | * @param direction direction of view |
| | 857 | * @param new_uf new horizontal cubemap coordinate |
| | 858 | * @param new_vf new vertical cubemap coordinate |
| | 859 | * @param face face position on cubemap |
| | 860 | */ |
| | 861 | static void process_cube_coordinates(const V360Context *s, |
| | 862 | float uf, float vf, int direction, |
| | 863 | float *new_uf, float *new_vf, int *face) |
| | 864 | { |
| | 865 | /* |
| | 866 | * Cubemap orientation |
| | 867 | * |
| | 868 | * width |
| | 869 | * <-------> |
| | 870 | * +-------+ |
| | 871 | * | | U |
| | 872 | * | up | h -------> |
| | 873 | * +-------+-------+-------+-------+ ^ e | |
| | 874 | * | | | | | | i V | |
| | 875 | * | left | front | right | back | | g | |
| | 876 | * +-------+-------+-------+-------+ v h v |
| | 877 | * | | t |
| | 878 | * | down | |
| | 879 | * +-------+ |
| | 880 | */ |
| | 881 | |
| | 882 | *face = s->in_cubemap_face_order[direction]; |
| | 883 | rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]); |
| | 884 | |
| | 885 | if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) { |
| | 886 | // There are no pixels to use in this case |
| | 887 | *new_uf = uf; |
| | 888 | *new_vf = vf; |
| | 889 | } else if (uf < -1.f) { |
| | 890 | uf += 2.f; |
| | 891 | switch (direction) { |
| | 892 | case RIGHT: |
| | 893 | direction = FRONT; |
| | 894 | *new_uf = uf; |
| | 895 | *new_vf = vf; |
| | 896 | break; |
| | 897 | case LEFT: |
| | 898 | direction = BACK; |
| | 899 | *new_uf = uf; |
| | 900 | *new_vf = vf; |
| | 901 | break; |
| | 902 | case UP: |
| | 903 | direction = LEFT; |
| | 904 | *new_uf = vf; |
| | 905 | *new_vf = -uf; |
| | 906 | break; |
| | 907 | case DOWN: |
| | 908 | direction = LEFT; |
| | 909 | *new_uf = -vf; |
| | 910 | *new_vf = uf; |
| | 911 | break; |
| | 912 | case FRONT: |
| | 913 | direction = LEFT; |
| | 914 | *new_uf = uf; |
| | 915 | *new_vf = vf; |
| | 916 | break; |
| | 917 | case BACK: |
| | 918 | direction = RIGHT; |
| | 919 | *new_uf = uf; |
| | 920 | *new_vf = vf; |
| | 921 | break; |
| | 922 | } |
| | 923 | } else if (uf >= 1.f) { |
| | 924 | uf -= 2.f; |
| | 925 | switch (direction) { |
| | 926 | case RIGHT: |
| | 927 | direction = BACK; |
| | 928 | *new_uf = uf; |
| | 929 | *new_vf = vf; |
| | 930 | break; |
| | 931 | case LEFT: |
| | 932 | direction = FRONT; |
| | 933 | *new_uf = uf; |
| | 934 | *new_vf = vf; |
| | 935 | break; |
| | 936 | case UP: |
| | 937 | direction = RIGHT; |
| | 938 | *new_uf = -vf; |
| | 939 | *new_vf = uf; |
| | 940 | break; |
| | 941 | case DOWN: |
| | 942 | direction = RIGHT; |
| | 943 | *new_uf = vf; |
| | 944 | *new_vf = -uf; |
| | 945 | break; |
| | 946 | case FRONT: |
| | 947 | direction = RIGHT; |
| | 948 | *new_uf = uf; |
| | 949 | *new_vf = vf; |
| | 950 | break; |
| | 951 | case BACK: |
| | 952 | direction = LEFT; |
| | 953 | *new_uf = uf; |
| | 954 | *new_vf = vf; |
| | 955 | break; |
| | 956 | } |
| | 957 | } else if (vf < -1.f) { |
| | 958 | vf += 2.f; |
| | 959 | switch (direction) { |
| | 960 | case RIGHT: |
| | 961 | direction = UP; |
| | 962 | *new_uf = vf; |
| | 963 | *new_vf = -uf; |
| | 964 | break; |
| | 965 | case LEFT: |
| | 966 | direction = UP; |
| | 967 | *new_uf = -vf; |
| | 968 | *new_vf = uf; |
| | 969 | break; |
| | 970 | case UP: |
| | 971 | direction = BACK; |
| | 972 | *new_uf = -uf; |
| | 973 | *new_vf = -vf; |
| | 974 | break; |
| | 975 | case DOWN: |
| | 976 | direction = FRONT; |
| | 977 | *new_uf = uf; |
| | 978 | *new_vf = vf; |
| | 979 | break; |
| | 980 | case FRONT: |
| | 981 | direction = UP; |
| | 982 | *new_uf = uf; |
| | 983 | *new_vf = vf; |
| | 984 | break; |
| | 985 | case BACK: |
| | 986 | direction = UP; |
| | 987 | *new_uf = -uf; |
| | 988 | *new_vf = -vf; |
| | 989 | break; |
| | 990 | } |
| | 991 | } else if (vf >= 1.f) { |
| | 992 | vf -= 2.f; |
| | 993 | switch (direction) { |
| | 994 | case RIGHT: |
| | 995 | direction = DOWN; |
| | 996 | *new_uf = -vf; |
| | 997 | *new_vf = uf; |
| | 998 | break; |
| | 999 | case LEFT: |
| | 1000 | direction = DOWN; |
| | 1001 | *new_uf = vf; |
| | 1002 | *new_vf = -uf; |
| | 1003 | break; |
| | 1004 | case UP: |
| | 1005 | direction = FRONT; |
| | 1006 | *new_uf = uf; |
| | 1007 | *new_vf = vf; |
| | 1008 | break; |
| | 1009 | case DOWN: |
| | 1010 | direction = BACK; |
| | 1011 | *new_uf = -uf; |
| | 1012 | *new_vf = -vf; |
| | 1013 | break; |
| | 1014 | case FRONT: |
| | 1015 | direction = DOWN; |
| | 1016 | *new_uf = uf; |
| | 1017 | *new_vf = vf; |
| | 1018 | break; |
| | 1019 | case BACK: |
| | 1020 | direction = DOWN; |
| | 1021 | *new_uf = -uf; |
| | 1022 | *new_vf = -vf; |
| | 1023 | break; |
| | 1024 | } |
| | 1025 | } else { |
| | 1026 | // Inside cube face |
| | 1027 | *new_uf = uf; |
| | 1028 | *new_vf = vf; |
| | 1029 | } |
| | 1030 | |
| | 1031 | *face = s->in_cubemap_face_order[direction]; |
| | 1032 | rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]); |
| | 1033 | } |
| | 1034 | |
| | 1035 | /** |
| | 1036 | * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format. |
| | 1037 | * |
| | 1038 | * @param s filter context |
| | 1039 | * @param i horizontal position on frame [0, height) |
| | 1040 | * @param j vertical position on frame [0, width) |
| | 1041 | * @param width frame width |
| | 1042 | * @param height frame height |
| | 1043 | * @param vec coordinates on sphere |
| | 1044 | */ |
| | 1045 | static void cube3x2_to_xyz(const V360Context *s, |
| | 1046 | int i, int j, int width, int height, |
| | 1047 | float *vec) |
| | 1048 | { |
| | 1049 | const float ew = width / 3.f; |
| | 1050 | const float eh = height / 2.f; |
| | 1051 | |
| | 1052 | const int u_face = floorf(i / ew); |
| | 1053 | const int v_face = floorf(j / eh); |
| | 1054 | const int face = u_face + 3 * v_face; |
| | 1055 | |
| | 1056 | const int u_shift = ceilf(ew * u_face); |
| | 1057 | const int v_shift = ceilf(eh * v_face); |
| | 1058 | const int ewi = ceilf(ew * (u_face + 1)) - u_shift; |
| | 1059 | const int ehi = ceilf(eh * (v_face + 1)) - v_shift; |
| | 1060 | |
| | 1061 | const float uf = 2.f * (i - u_shift) / ewi - 1.f; |
| | 1062 | const float vf = 2.f * (j - v_shift) / ehi - 1.f; |
| | 1063 | |
| | 1064 | cube_to_xyz(s, uf, vf, face, vec); |
| | 1065 | } |
| | 1066 | |
| | 1067 | /** |
| | 1068 | * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere. |
| | 1069 | * |
| | 1070 | * @param s filter context |
| | 1071 | * @param vec coordinates on sphere |
| | 1072 | * @param width frame width |
| | 1073 | * @param height frame height |
| | 1074 | * @param us horizontal coordinates for interpolation window |
| | 1075 | * @param vs vertical coordinates for interpolation window |
| | 1076 | * @param du horizontal relative coordinate |
| | 1077 | * @param dv vertical relative coordinate |
| | 1078 | */ |
| | 1079 | static void xyz_to_cube3x2(const V360Context *s, |
| | 1080 | const float *vec, int width, int height, |
| | 1081 | uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv) |
| | 1082 | { |
| | 1083 | const float ew = width / 3.f; |
| | 1084 | const float eh = height / 2.f; |
| | 1085 | float uf, vf; |
| | 1086 | int ui, vi; |
| | 1087 | int ewi, ehi; |
| | 1088 | int i, j; |
| | 1089 | int direction, face; |
| | 1090 | int u_face, v_face; |
| | 1091 | |
| | 1092 | xyz_to_cube(s, vec, &uf, &vf, &direction); |
| | 1093 | |
| | 1094 | face = s->in_cubemap_face_order[direction]; |
| | 1095 | u_face = face % 3; |
| | 1096 | v_face = face / 3; |
| | 1097 | ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face); |
| | 1098 | ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face); |
| | 1099 | |
| | 1100 | uf = 0.5f * ewi * (uf + 1.f); |
| | 1101 | vf = 0.5f * ehi * (vf + 1.f); |
| | 1102 | |
| | 1103 | ui = floorf(uf); |
| | 1104 | vi = floorf(vf); |
| | 1105 | |
| | 1106 | *du = uf - ui; |
| | 1107 | *dv = vf - vi; |
| | 1108 | |
| | 1109 | for (i = -1; i < 3; i++) { |
| | 1110 | for (j = -1; j < 3; j++) { |
| | 1111 | float u, v; |
| | 1112 | int u_shift, v_shift; |
| | 1113 | int new_ewi, new_ehi; |
| | 1114 | |
| | 1115 | process_cube_coordinates(s, 2.f * (ui + j) / ewi - 1.f, |
| | 1116 | 2.f * (vi + i) / ehi - 1.f, |
| | 1117 | direction, &u, &v, &face); |
| | 1118 | u_face = face % 3; |
| | 1119 | v_face = face / 3; |
| | 1120 | u_shift = ceilf(ew * u_face); |
| | 1121 | v_shift = ceilf(eh * v_face); |
| | 1122 | new_ewi = ceilf(ew * (u_face + 1)) - u_shift; |
| | 1123 | new_ehi = ceilf(eh * (v_face + 1)) - v_shift; |
| | 1124 | |
| | 1125 | us[i + 1][j + 1] = u_shift + av_clip(roundf(0.5f * new_ewi * (u + 1.f)), 0, new_ewi - 1); |
| | 1126 | vs[i + 1][j + 1] = v_shift + av_clip(roundf(0.5f * new_ehi * (v + 1.f)), 0, new_ehi - 1); |
| | 1127 | } |
| | 1128 | } |
| | 1129 | } |
| | 1130 | |
| | 1131 | /** |
| | 1132 | * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format. |
| | 1133 | * |
| | 1134 | * @param s filter context |
| | 1135 | * @param i horizontal position on frame [0, height) |
| | 1136 | * @param j vertical position on frame [0, width) |
| | 1137 | * @param width frame width |
| | 1138 | * @param height frame height |
| | 1139 | * @param vec coordinates on sphere |
| | 1140 | */ |
| | 1141 | static void cube6x1_to_xyz(const V360Context *s, |
| | 1142 | int i, int j, int width, int height, |
| | 1143 | float *vec) |
| | 1144 | { |
| | 1145 | const float ew = width / 6.f; |
| | 1146 | const float eh = height; |
| | 1147 | |
| | 1148 | const int face = floorf(i / ew); |
| | 1149 | |
| | 1150 | const int u_shift = ceilf(ew * face); |
| | 1151 | const int ewi = ceilf(ew * (face + 1)) - u_shift; |
| | 1152 | |
| | 1153 | const float uf = 2.f * (i - u_shift) / ewi - 1.f; |
| | 1154 | const float vf = 2.f * j / eh - 1.f; |
| | 1155 | |
| | 1156 | cube_to_xyz(s, uf, vf, face, vec); |
| | 1157 | } |
| | 1158 | |
| | 1159 | /** |
| | 1160 | * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere. |
| | 1161 | * |
| | 1162 | * @param s filter context |
| | 1163 | * @param vec coordinates on sphere |
| | 1164 | * @param width frame width |
| | 1165 | * @param height frame height |
| | 1166 | * @param us horizontal coordinates for interpolation window |
| | 1167 | * @param vs vertical coordinates for interpolation window |
| | 1168 | * @param du horizontal relative coordinate |
| | 1169 | * @param dv vertical relative coordinate |
| | 1170 | */ |
| | 1171 | static void xyz_to_cube6x1(const V360Context *s, |
| | 1172 | const float *vec, int width, int height, |
| | 1173 | uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv) |
| | 1174 | { |
| | 1175 | const float ew = width / 6.f; |
| | 1176 | const float eh = height; |
| | 1177 | float uf, vf; |
| | 1178 | int ui, vi; |
| | 1179 | int ewi; |
| | 1180 | int i, j; |
| | 1181 | int direction, face; |
| | 1182 | |
| | 1183 | xyz_to_cube(s, vec, &uf, &vf, &direction); |
| | 1184 | |
| | 1185 | face = s->in_cubemap_face_order[direction]; |
| | 1186 | ewi = ceilf(ew * (face + 1)) - ceilf(ew * face); |
| | 1187 | |
| | 1188 | uf = 0.5f * ewi * (uf + 1.f); |
| | 1189 | vf = 0.5f * eh * (vf + 1.f); |
| | 1190 | |
| | 1191 | ui = floorf(uf); |
| | 1192 | vi = floorf(vf); |
| | 1193 | |
| | 1194 | *du = uf - ui; |
| | 1195 | *dv = vf - vi; |
| | 1196 | |
| | 1197 | for (i = -1; i < 3; i++) { |
| | 1198 | for (j = -1; j < 3; j++) { |
| | 1199 | float u, v; |
| | 1200 | int u_shift; |
| | 1201 | int new_ewi; |
| | 1202 | |
| | 1203 | process_cube_coordinates(s, 2.f * (ui + j) / ewi - 1.f, |
| | 1204 | 2.f * (vi + i) / eh - 1.f, |
| | 1205 | direction, &u, &v, &face); |
| | 1206 | u_shift = ceilf(ew * face); |
| | 1207 | new_ewi = ceilf(ew * (face + 1)) - u_shift; |
| | 1208 | |
| | 1209 | us[i + 1][j + 1] = u_shift + av_clip(roundf(0.5f * new_ewi * (u + 1.f)), 0, new_ewi - 1); |
| | 1210 | vs[i + 1][j + 1] = av_clip(roundf(0.5f * eh * (v + 1.f)), 0, eh - 1); |
| | 1211 | } |
| | 1212 | } |
| | 1213 | } |
| | 1214 | |
| | 1215 | /** |
| | 1216 | * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format. |
| | 1217 | * |
| | 1218 | * @param s filter context |
| | 1219 | * @param i horizontal position on frame [0, height) |
| | 1220 | * @param j vertical position on frame [0, width) |
| | 1221 | * @param width frame width |
| | 1222 | * @param height frame height |
| | 1223 | * @param vec coordinates on sphere |
| | 1224 | */ |
| | 1225 | static void equirect_to_xyz(const V360Context *s, |
| | 1226 | int i, int j, int width, int height, |
| | 1227 | float *vec) |
| | 1228 | { |
| | 1229 | const float phi = ((2.f * i) / width - 1.f) * M_PI; |
| | 1230 | const float theta = ((2.f * j) / height - 1.f) * M_PI_2; |
| | 1231 | |
| | 1232 | const float sin_phi = sinf(phi); |
| | 1233 | const float cos_phi = cosf(phi); |
| | 1234 | const float sin_theta = sinf(theta); |
| | 1235 | const float cos_theta = cosf(theta); |
| | 1236 | |
| | 1237 | vec[0] = cos_theta * sin_phi; |
| | 1238 | vec[1] = -sin_theta; |
| | 1239 | vec[2] = -cos_theta * cos_phi; |
| | 1240 | } |
| | 1241 | |
| | 1242 | /** |
| | 1243 | * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere. |
| | 1244 | * |
| | 1245 | * @param s filter context |
| | 1246 | * @param vec coordinates on sphere |
| | 1247 | * @param width frame width |
| | 1248 | * @param height frame height |
| | 1249 | * @param us horizontal coordinates for interpolation window |
| | 1250 | * @param vs vertical coordinates for interpolation window |
| | 1251 | * @param du horizontal relative coordinate |
| | 1252 | * @param dv vertical relative coordinate |
| | 1253 | */ |
| | 1254 | static void xyz_to_equirect(const V360Context *s, |
| | 1255 | const float *vec, int width, int height, |
| | 1256 | uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv) |
| | 1257 | { |
| | 1258 | const float phi = atan2f(vec[0], -vec[2]); |
| | 1259 | const float theta = asinf(-vec[1]); |
| | 1260 | float uf, vf; |
| | 1261 | int ui, vi; |
| | 1262 | int i, j; |
| | 1263 | |
| | 1264 | uf = (phi / M_PI + 1.f) * width / 2.f; |
| | 1265 | vf = (theta / M_PI_2 + 1.f) * height / 2.f; |
| | 1266 | ui = floorf(uf); |
| | 1267 | vi = floorf(vf); |
| | 1268 | |
| | 1269 | *du = uf - ui; |
| | 1270 | *dv = vf - vi; |
| | 1271 | |
| | 1272 | for (i = -1; i < 3; i++) { |
| | 1273 | for (j = -1; j < 3; j++) { |
| | 1274 | us[i + 1][j + 1] = mod(ui + j, width); |
| | 1275 | vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1); |
| | 1276 | } |
| | 1277 | } |
| | 1278 | } |
| | 1279 | |
| | 1280 | /** |
| | 1281 | * Prepare data for processing equi-angular cubemap input format. |
| | 1282 | * |
| | 1283 | * @param ctx filter context |
| | 1284 | |
| | 1285 | * @return error code |
| | 1286 | */ |
| | 1287 | static int prepare_eac_in(AVFilterContext *ctx) |
| | 1288 | { |
| | 1289 | V360Context *s = ctx->priv; |
| | 1290 | |
| | 1291 | s->in_cubemap_face_order[RIGHT] = TOP_RIGHT; |
| | 1292 | s->in_cubemap_face_order[LEFT] = TOP_LEFT; |
| | 1293 | s->in_cubemap_face_order[UP] = BOTTOM_RIGHT; |
| | 1294 | s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT; |
| | 1295 | s->in_cubemap_face_order[FRONT] = TOP_MIDDLE; |
| | 1296 | s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE; |
| | 1297 | |
| | 1298 | s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0; |
| | 1299 | s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0; |
| | 1300 | s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0; |
| | 1301 | s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270; |
| | 1302 | s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90; |
| | 1303 | s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270; |
| | 1304 | |
| | 1305 | return 0; |
| | 1306 | } |
| | 1307 | |
| | 1308 | /** |
| | 1309 | * Prepare data for processing equi-angular cubemap output format. |
| | 1310 | * |
| | 1311 | * @param ctx filter context |
| | 1312 | * |
| | 1313 | * @return error code |
| | 1314 | */ |
| | 1315 | static int prepare_eac_out(AVFilterContext *ctx) |
| | 1316 | { |
| | 1317 | V360Context *s = ctx->priv; |
| | 1318 | |
| | 1319 | s->out_cubemap_direction_order[TOP_LEFT] = LEFT; |
| | 1320 | s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT; |
| | 1321 | s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT; |
| | 1322 | s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN; |
| | 1323 | s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK; |
| | 1324 | s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP; |
| | 1325 | |
| | 1326 | s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0; |
| | 1327 | s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0; |
| | 1328 | s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0; |
| | 1329 | s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270; |
| | 1330 | s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90; |
| | 1331 | s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270; |
| | 1332 | |
| | 1333 | return 0; |
| | 1334 | } |
| | 1335 | |
| | 1336 | /** |
| | 1337 | * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format. |
| | 1338 | * |
| | 1339 | * @param s filter context |
| | 1340 | * @param i horizontal position on frame [0, height) |
| | 1341 | * @param j vertical position on frame [0, width) |
| | 1342 | * @param width frame width |
| | 1343 | * @param height frame height |
| | 1344 | * @param vec coordinates on sphere |
| | 1345 | */ |
| | 1346 | static void eac_to_xyz(const V360Context *s, |
| | 1347 | int i, int j, int width, int height, |
| | 1348 | float *vec) |
| | 1349 | { |
| | 1350 | const float pixel_pad = 2; |
| | 1351 | const float u_pad = pixel_pad / width; |
| | 1352 | const float v_pad = pixel_pad / height; |
| | 1353 | |
| | 1354 | int u_face, v_face, face; |
| | 1355 | |
| | 1356 | float l_x, l_y, l_z; |
| | 1357 | float norm; |
| | 1358 | |
| | 1359 | float uf = (float)i / width; |
| | 1360 | float vf = (float)j / height; |
| | 1361 | |
| | 1362 | // EAC has 2-pixel padding on faces except between faces on the same row |
| | 1363 | // Padding pixels seems not to be stretched with tangent as regular pixels |
| | 1364 | // Formulas below approximate original padding as close as I could get experimentally |
| | 1365 | |
| | 1366 | // Horizontal padding |
| | 1367 | uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad); |
| | 1368 | if (uf < 0.f) { |
| | 1369 | u_face = 0; |
| | 1370 | uf -= 0.5f; |
| | 1371 | } else if (uf >= 3.f) { |
| | 1372 | u_face = 2; |
| | 1373 | uf -= 2.5f; |
| | 1374 | } else { |
| | 1375 | u_face = floorf(uf); |
| | 1376 | uf = fmodf(uf, 1.f) - 0.5f; |
| | 1377 | } |
| | 1378 | |
| | 1379 | // Vertical padding |
| | 1380 | v_face = floorf(vf * 2.f); |
| | 1381 | vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f; |
| | 1382 | |
| | 1383 | if (uf >= -0.5f && uf < 0.5f) { |
| | 1384 | uf = tanf(M_PI_2 * uf); |
| | 1385 | } else { |
| | 1386 | uf = 2.f * uf; |
| | 1387 | } |
| | 1388 | if (vf >= -0.5f && vf < 0.5f) { |
| | 1389 | vf = tanf(M_PI_2 * vf); |
| | 1390 | } else { |
| | 1391 | vf = 2.f * vf; |
| | 1392 | } |
| | 1393 | |
| | 1394 | face = u_face + 3 * v_face; |
| | 1395 | |
| | 1396 | switch (face) { |
| | 1397 | case TOP_LEFT: |
| | 1398 | l_x = -1.f; |
| | 1399 | l_y = -vf; |
| | 1400 | l_z = -uf; |
| | 1401 | break; |
| | 1402 | case TOP_MIDDLE: |
| | 1403 | l_x = uf; |
| | 1404 | l_y = -vf; |
| | 1405 | l_z = -1.f; |
| | 1406 | break; |
| | 1407 | case TOP_RIGHT: |
| | 1408 | l_x = 1.f; |
| | 1409 | l_y = -vf; |
| | 1410 | l_z = uf; |
| | 1411 | break; |
| | 1412 | case BOTTOM_LEFT: |
| | 1413 | l_x = -vf; |
| | 1414 | l_y = -1.f; |
| | 1415 | l_z = uf; |
| | 1416 | break; |
| | 1417 | case BOTTOM_MIDDLE: |
| | 1418 | l_x = -vf; |
| | 1419 | l_y = uf; |
| | 1420 | l_z = 1.f; |
| | 1421 | break; |
| | 1422 | case BOTTOM_RIGHT: |
| | 1423 | l_x = -vf; |
| | 1424 | l_y = 1.f; |
| | 1425 | l_z = -uf; |
| | 1426 | break; |
| | 1427 | } |
| | 1428 | |
| | 1429 | norm = sqrtf(l_x * l_x + l_y * l_y + l_z * l_z); |
| | 1430 | vec[0] = l_x / norm; |
| | 1431 | vec[1] = l_y / norm; |
| | 1432 | vec[2] = l_z / norm; |
| | 1433 | } |
| | 1434 | |
| | 1435 | /** |
| | 1436 | * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere. |
| | 1437 | * |
| | 1438 | * @param s filter context |
| | 1439 | * @param vec coordinates on sphere |
| | 1440 | * @param width frame width |
| | 1441 | * @param height frame height |
| | 1442 | * @param us horizontal coordinates for interpolation window |
| | 1443 | * @param vs vertical coordinates for interpolation window |
| | 1444 | * @param du horizontal relative coordinate |
| | 1445 | * @param dv vertical relative coordinate |
| | 1446 | */ |
| | 1447 | static void xyz_to_eac(const V360Context *s, |
| | 1448 | const float *vec, int width, int height, |
| | 1449 | uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv) |
| | 1450 | { |
| | 1451 | const float pixel_pad = 2; |
| | 1452 | const float u_pad = pixel_pad / width; |
| | 1453 | const float v_pad = pixel_pad / height; |
| | 1454 | |
| | 1455 | float uf, vf; |
| | 1456 | int ui, vi; |
| | 1457 | int i, j; |
| | 1458 | int direction, face; |
| | 1459 | int u_face, v_face; |
| | 1460 | |
| | 1461 | xyz_to_cube(s, vec, &uf, &vf, &direction); |
| | 1462 | |
| | 1463 | face = s->in_cubemap_face_order[direction]; |
| | 1464 | u_face = face % 3; |
| | 1465 | v_face = face / 3; |
| | 1466 | |
| | 1467 | uf = M_2_PI * atanf(uf) + 0.5f; |
| | 1468 | vf = M_2_PI * atanf(vf) + 0.5f; |
| | 1469 | |
| | 1470 | // These formulas are inversed from eac_to_xyz ones |
| | 1471 | uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad; |
| | 1472 | vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face; |
| | 1473 | |
| | 1474 | uf *= width; |
| | 1475 | vf *= height; |
| | 1476 | |
| | 1477 | ui = floorf(uf); |
| | 1478 | vi = floorf(vf); |
| | 1479 | |
| | 1480 | *du = uf - ui; |
| | 1481 | *dv = vf - vi; |
| | 1482 | |
| | 1483 | for (i = -1; i < 3; i++) { |
| | 1484 | for (j = -1; j < 3; j++) { |
| | 1485 | us[i + 1][j + 1] = av_clip(ui + j, 0, width - 1); |
| | 1486 | vs[i + 1][j + 1] = av_clip(vi + i, 0, height - 1); |
| | 1487 | } |
| | 1488 | } |
| | 1489 | } |
| | 1490 | |
| | 1491 | /** |
| | 1492 | * Prepare data for processing flat output format. |
| | 1493 | * |
| | 1494 | * @param ctx filter context |
| | 1495 | * |
| | 1496 | * @return error code |
| | 1497 | */ |
| | 1498 | static int prepare_flat_out(AVFilterContext *ctx) |
| | 1499 | { |
| | 1500 | V360Context *s = ctx->priv; |
| | 1501 | |
| | 1502 | const float h_angle = 0.5f * s->h_fov * M_PI / 180.f; |
| | 1503 | const float v_angle = 0.5f * s->v_fov * M_PI / 180.f; |
| | 1504 | |
| | 1505 | const float sin_phi = sinf(h_angle); |
| | 1506 | const float cos_phi = cosf(h_angle); |
| | 1507 | const float sin_theta = sinf(v_angle); |
| | 1508 | const float cos_theta = cosf(v_angle); |
| | 1509 | |
| | 1510 | s->flat_range[0] = cos_theta * sin_phi; |
| | 1511 | s->flat_range[1] = sin_theta; |
| | 1512 | s->flat_range[2] = -cos_theta * cos_phi; |
| | 1513 | |
| | 1514 | return 0; |
| | 1515 | } |
| | 1516 | |
| | 1517 | /** |
| | 1518 | * Calculate 3D coordinates on sphere for corresponding frame position in flat format. |
| | 1519 | * |
| | 1520 | * @param s filter context |
| | 1521 | * @param i horizontal position on frame [0, height) |
| | 1522 | * @param j vertical position on frame [0, width) |
| | 1523 | * @param width frame width |
| | 1524 | * @param height frame height |
| | 1525 | * @param vec coordinates on sphere |
| | 1526 | */ |
| | 1527 | static void flat_to_xyz(const V360Context *s, |
| | 1528 | int i, int j, int width, int height, |
| | 1529 | float *vec) |
| | 1530 | { |
| | 1531 | const float l_x = s->flat_range[0] * (2.f * i / width - 1.f); |
| | 1532 | const float l_y = -s->flat_range[1] * (2.f * j / height - 1.f); |
| | 1533 | const float l_z = s->flat_range[2]; |
| | 1534 | |
| | 1535 | const float norm = sqrtf(l_x * l_x + l_y * l_y + l_z * l_z); |
| | 1536 | |
| | 1537 | vec[0] = l_x / norm; |
| | 1538 | vec[1] = l_y / norm; |
| | 1539 | vec[2] = l_z / norm; |
| | 1540 | } |
| | 1541 | |
| | 1542 | /** |
| | 1543 | * Calculate rotation matrix for yaw/pitch/roll angles. |
| | 1544 | */ |
| | 1545 | static inline void calculate_rotation_matrix(float yaw, float pitch, float roll, |
| | 1546 | float rot_mat[3][3]) |
| | 1547 | { |
| | 1548 | const float yaw_rad = yaw * M_PI / 180.f; |
| | 1549 | const float pitch_rad = pitch * M_PI / 180.f; |
| | 1550 | const float roll_rad = roll * M_PI / 180.f; |
| | 1551 | |
| | 1552 | const float sin_yaw = sinf(-yaw_rad); |
| | 1553 | const float cos_yaw = cosf(-yaw_rad); |
| | 1554 | const float sin_pitch = sinf(pitch_rad); |
| | 1555 | const float cos_pitch = cosf(pitch_rad); |
| | 1556 | const float sin_roll = sinf(roll_rad); |
| | 1557 | const float cos_roll = cosf(roll_rad); |
| | 1558 | |
| | 1559 | rot_mat[0][0] = sin_yaw * sin_pitch * sin_roll + cos_yaw * cos_roll; |
| | 1560 | rot_mat[0][1] = sin_yaw * sin_pitch * cos_roll - cos_yaw * sin_roll; |
| | 1561 | rot_mat[0][2] = sin_yaw * cos_pitch; |
| | 1562 | |
| | 1563 | rot_mat[1][0] = cos_pitch * sin_roll; |
| | 1564 | rot_mat[1][1] = cos_pitch * cos_roll; |
| | 1565 | rot_mat[1][2] = -sin_pitch; |
| | 1566 | |
| | 1567 | rot_mat[2][0] = cos_yaw * sin_pitch * sin_roll - sin_yaw * cos_roll; |
| | 1568 | rot_mat[2][1] = cos_yaw * sin_pitch * cos_roll + sin_yaw * sin_roll; |
| | 1569 | rot_mat[2][2] = cos_yaw * cos_pitch; |
| | 1570 | } |
| | 1571 | |
| | 1572 | /** |
| | 1573 | * Rotate vector with given rotation matrix. |
| | 1574 | * |
| | 1575 | * @param rot_mat rotation matrix |
| | 1576 | * @param vec vector |
| | 1577 | */ |
| | 1578 | static inline void rotate(const float rot_mat[3][3], |
| | 1579 | float *vec) |
| | 1580 | { |
| | 1581 | const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2]; |
| | 1582 | const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2]; |
| | 1583 | const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2]; |
| | 1584 | |
| | 1585 | vec[0] = x_tmp; |
| | 1586 | vec[1] = y_tmp; |
| | 1587 | vec[2] = z_tmp; |
| | 1588 | } |
| | 1589 | |
| | 1590 | static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip, |
| | 1591 | float *modifier) |
| | 1592 | { |
| | 1593 | modifier[0] = h_flip ? -1.f : 1.f; |
| | 1594 | modifier[1] = v_flip ? -1.f : 1.f; |
| | 1595 | modifier[2] = d_flip ? -1.f : 1.f; |
| | 1596 | } |
| | 1597 | |
| | 1598 | static inline void mirror(const float *modifier, |
| | 1599 | float *vec) |
| | 1600 | { |
| | 1601 | vec[0] *= modifier[0]; |
| | 1602 | vec[1] *= modifier[1]; |
| | 1603 | vec[2] *= modifier[2]; |
| | 1604 | } |
| | 1605 | |
| | 1606 | static int config_output(AVFilterLink *outlink) |
| | 1607 | { |
| | 1608 | AVFilterContext *ctx = outlink->src; |
| | 1609 | AVFilterLink *inlink = ctx->inputs[0]; |
| | 1610 | V360Context *s = ctx->priv; |
| | 1611 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); |
| | 1612 | const int depth = desc->comp[0].depth; |
| | 1613 | float remap_data_size = 0.f; |
| | 1614 | int sizeof_remap; |
| | 1615 | int err; |
| | 1616 | int p, h, w; |
| | 1617 | float hf, wf; |
| | 1618 | float mirror_modifier[3]; |
| | 1619 | void (*in_transform)(const V360Context *s, |
| | 1620 | const float *vec, int width, int height, |
| | 1621 | uint16_t us[4][4], uint16_t vs[4][4], float *du, float *dv); |
| | 1622 | void (*out_transform)(const V360Context *s, |
| | 1623 | int i, int j, int width, int height, |
| | 1624 | float *vec); |
| | 1625 | void (*calculate_kernel)(float du, float dv, int shift, const XYRemap4 *r_tmp, void *r); |
| | 1626 | float rot_mat[3][3]; |
| | 1627 | |
| | 1628 | switch (s->interp) { |
| | 1629 | case NEAREST: |
| | 1630 | calculate_kernel = nearest_kernel; |
| | 1631 | s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice; |
| | 1632 | sizeof_remap = sizeof(XYRemap1); |
| | 1633 | break; |
| | 1634 | case BILINEAR: |
| | 1635 | calculate_kernel = bilinear_kernel; |
| | 1636 | s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice; |
| | 1637 | sizeof_remap = sizeof(XYRemap2); |
| | 1638 | break; |
| | 1639 | case BICUBIC: |
| | 1640 | calculate_kernel = bicubic_kernel; |
| | 1641 | s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice; |
| | 1642 | sizeof_remap = sizeof(XYRemap4); |
| | 1643 | break; |
| | 1644 | case LANCZOS: |
| | 1645 | calculate_kernel = lanczos_kernel; |
| | 1646 | s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice; |
| | 1647 | sizeof_remap = sizeof(XYRemap4); |
| | 1648 | break; |
| | 1649 | } |
| | 1650 | |
| | 1651 | switch (s->in) { |
| | 1652 | case EQUIRECTANGULAR: |
| | 1653 | in_transform = xyz_to_equirect; |
| | 1654 | err = 0; |
| | 1655 | wf = inlink->w; |
| | 1656 | hf = inlink->h; |
| | 1657 | break; |
| | 1658 | case CUBEMAP_3_2: |
| | 1659 | in_transform = xyz_to_cube3x2; |
| | 1660 | err = prepare_cube_in(ctx); |
| | 1661 | wf = inlink->w / 3.f * 4.f; |
| | 1662 | hf = inlink->h; |
| | 1663 | break; |
| | 1664 | case CUBEMAP_6_1: |
| | 1665 | in_transform = xyz_to_cube6x1; |
| | 1666 | err = prepare_cube_in(ctx); |
| | 1667 | wf = inlink->w / 3.f * 2.f; |
| | 1668 | hf = inlink->h * 2.f; |
| | 1669 | break; |
| | 1670 | case EQUIANGULAR: |
| | 1671 | in_transform = xyz_to_eac; |
| | 1672 | err = prepare_eac_in(ctx); |
| | 1673 | wf = inlink->w; |
| | 1674 | hf = inlink->h / 9.f * 8.f; |
| | 1675 | break; |
| | 1676 | case FLAT: |
| | 1677 | av_log(ctx, AV_LOG_ERROR, "Flat format is not accepted as input.\n"); |
| | 1678 | return AVERROR(EINVAL); |
| | 1679 | default: |
| | 1680 | av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n"); |
| | 1681 | return AVERROR_BUG; |
| | 1682 | } |
| | 1683 | |
| | 1684 | if (err != 0) { |
| | 1685 | return err; |
| | 1686 | } |
| | 1687 | |
| | 1688 | switch (s->out) { |
| | 1689 | case EQUIRECTANGULAR: |
| | 1690 | out_transform = equirect_to_xyz; |
| | 1691 | err = 0; |
| | 1692 | w = roundf(wf); |
| | 1693 | h = roundf(hf); |
| | 1694 | break; |
| | 1695 | case CUBEMAP_3_2: |
| | 1696 | out_transform = cube3x2_to_xyz; |
| | 1697 | err = prepare_cube_out(ctx); |
| | 1698 | w = roundf(wf / 4.f * 3.f); |
| | 1699 | h = roundf(hf); |
| | 1700 | break; |
| | 1701 | case CUBEMAP_6_1: |
| | 1702 | out_transform = cube6x1_to_xyz; |
| | 1703 | err = prepare_cube_out(ctx); |
| | 1704 | w = roundf(wf / 2.f * 3.f); |
| | 1705 | h = roundf(hf / 2.f); |
| | 1706 | break; |
| | 1707 | case EQUIANGULAR: |
| | 1708 | out_transform = eac_to_xyz; |
| | 1709 | err = prepare_eac_out(ctx); |
| | 1710 | w = roundf(wf); |
| | 1711 | h = roundf(hf / 8.f * 9.f); |
| | 1712 | break; |
| | 1713 | case FLAT: |
| | 1714 | out_transform = flat_to_xyz; |
| | 1715 | err = prepare_flat_out(ctx); |
| | 1716 | w = roundf(wf * s->flat_range[0] / s->flat_range[1] / 2.f); |
| | 1717 | h = roundf(hf); |
| | 1718 | break; |
| | 1719 | default: |
| | 1720 | av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n"); |
| | 1721 | return AVERROR_BUG; |
| | 1722 | } |
| | 1723 | |
| | 1724 | if (err != 0) { |
| | 1725 | return err; |
| | 1726 | } |
| | 1727 | |
| | 1728 | // Override resolution with user values if specified |
| | 1729 | if (s->width > 0 && s->height > 0) { |
| | 1730 | w = s->width; |
| | 1731 | h = s->height; |
| | 1732 | } else if (s->width > 0 || s->height > 0) { |
| | 1733 | av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n"); |
| | 1734 | return AVERROR(EINVAL); |
| | 1735 | } |
| | 1736 | |
| | 1737 | s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h); |
| | 1738 | s->planeheight[0] = s->planeheight[3] = h; |
| | 1739 | s->planewidth[1] = s->planewidth[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w); |
| | 1740 | s->planewidth[0] = s->planewidth[3] = w; |
| | 1741 | |
| | 1742 | outlink->h = h; |
| | 1743 | outlink->w = w; |
| | 1744 | |
| | 1745 | s->inplaneheight[1] = s->inplaneheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); |
| | 1746 | s->inplaneheight[0] = s->inplaneheight[3] = inlink->h; |
| | 1747 | s->inplanewidth[1] = s->inplanewidth[2] = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); |
| | 1748 | s->inplanewidth[0] = s->inplanewidth[3] = inlink->w; |
| | 1749 | s->nb_planes = av_pix_fmt_count_planes(inlink->format); |
| | 1750 | |
| | 1751 | for (p = 0; p < s->nb_planes; p++) { |
| | 1752 | remap_data_size += (float)s->planewidth[p] * s->planeheight[p] * sizeof_remap; |
| | 1753 | } |
| | 1754 | |
| | 1755 | for (p = 0; p < s->nb_planes; p++) { |
| | 1756 | s->remap[p] = av_calloc(s->planewidth[p] * s->planeheight[p], sizeof_remap); |
| | 1757 | if (!s->remap[p]) { |
| | 1758 | av_log(ctx, AV_LOG_ERROR, |
| | 1759 | "Not enough memory to allocate remap data. Need at least %.3f GiB.\n", |
| | 1760 | remap_data_size / (1024 * 1024 * 1024)); |
| | 1761 | return AVERROR(ENOMEM); |
| | 1762 | } |
| | 1763 | } |
| | 1764 | |
| | 1765 | calculate_rotation_matrix(s->yaw, s->pitch, s->roll, rot_mat); |
| | 1766 | set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, mirror_modifier); |
| | 1767 | |
| | 1768 | // Calculate remap data |
| | 1769 | for (p = 0; p < s->nb_planes; p++) { |
| | 1770 | const int width = s->planewidth[p]; |
| | 1771 | const int height = s->planeheight[p]; |
| | 1772 | const int in_width = s->inplanewidth[p]; |
| | 1773 | const int in_height = s->inplaneheight[p]; |
| | 1774 | void *r = s->remap[p]; |
| | 1775 | float du, dv; |
| | 1776 | float vec[3]; |
| | 1777 | XYRemap4 r_tmp; |
| | 1778 | int i, j; |
| | 1779 | |
| | 1780 | for (i = 0; i < width; i++) { |
| | 1781 | for (j = 0; j < height; j++) { |
| | 1782 | out_transform(s, i, j, width, height, vec); |
| | 1783 | rotate(rot_mat, vec); |
| | 1784 | mirror(mirror_modifier, vec); |
| | 1785 | in_transform(s, vec, in_width, in_height, r_tmp.u, r_tmp.v, &du, &dv); |
| | 1786 | calculate_kernel(du, dv, j * width + i, &r_tmp, r); |
| | 1787 | } |
| | 1788 | } |
| | 1789 | } |
| | 1790 | |
| | 1791 | return 0; |
| | 1792 | } |
| | 1793 | |
| | 1794 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
| | 1795 | { |
| | 1796 | AVFilterContext *ctx = inlink->dst; |
| | 1797 | AVFilterLink *outlink = ctx->outputs[0]; |
| | 1798 | V360Context *s = ctx->priv; |
| | 1799 | AVFrame *out; |
| | 1800 | ThreadData td; |
| | 1801 | |
| | 1802 | out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
| | 1803 | if (!out) { |
| | 1804 | av_frame_free(&in); |
| | 1805 | return AVERROR(ENOMEM); |
| | 1806 | } |
| | 1807 | av_frame_copy_props(out, in); |
| | 1808 | |
| | 1809 | td.s = s; |
| | 1810 | td.in = in; |
| | 1811 | td.out = out; |
| | 1812 | td.nb_planes = s->nb_planes; |
| | 1813 | |
| | 1814 | ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx))); |
| | 1815 | |
| | 1816 | av_frame_free(&in); |
| | 1817 | return ff_filter_frame(outlink, out); |
| | 1818 | } |
| | 1819 | |
| | 1820 | static av_cold void uninit(AVFilterContext *ctx) |
| | 1821 | { |
| | 1822 | V360Context *s = ctx->priv; |
| | 1823 | int p; |
| | 1824 | |
| | 1825 | for (p = 0; p < s->nb_planes; p++) |
| | 1826 | av_freep(&s->remap[p]); |
| | 1827 | } |
| | 1828 | |
| | 1829 | static const AVFilterPad inputs[] = { |
| | 1830 | { |
| | 1831 | .name = "default", |
| | 1832 | .type = AVMEDIA_TYPE_VIDEO, |
| | 1833 | .filter_frame = filter_frame, |
| | 1834 | }, |
| | 1835 | { NULL } |
| | 1836 | }; |
| | 1837 | |
| | 1838 | static const AVFilterPad outputs[] = { |
| | 1839 | { |
| | 1840 | .name = "default", |
| | 1841 | .type = AVMEDIA_TYPE_VIDEO, |
| | 1842 | .config_props = config_output, |
| | 1843 | }, |
| | 1844 | { NULL } |
| | 1845 | }; |
| | 1846 | |
| | 1847 | AVFilter ff_vf_v360 = { |
| | 1848 | .name = "v360", |
| | 1849 | .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."), |
| | 1850 | .priv_size = sizeof(V360Context), |
| | 1851 | .uninit = uninit, |
| | 1852 | .query_formats = query_formats, |
| | 1853 | .inputs = inputs, |
| | 1854 | .outputs = outputs, |
| | 1855 | .priv_class = &v360_class, |
| | 1856 | .flags = AVFILTER_FLAG_SLICE_THREADS, |
| | 1857 | }; |