2 * Copyright (c) 2019 Eugene Lyapustin
4 * This file is part of FFmpeg.
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.
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.
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
23 * 360 video conversion filter.
24 * Principle of operation:
26 * (for each pixel in output frame)
27 * 1) Calculate OpenGL-like coordinates (x, y, z) for pixel position (i, j)
28 * 2) Apply 360 operations (rotation, mirror) to (x, y, z)
29 * 3) Calculate pixel position (u, v) in input frame
30 * 4) Calculate interpolation window and weight for each pixel
33 * 5) Remap input frame to output frame using precalculated data
38 #include "libavutil/avassert.h"
39 #include "libavutil/imgutils.h"
40 #include "libavutil/pixdesc.h"
41 #include "libavutil/opt.h"
48 typedef struct ThreadData {
53 #define OFFSET(x) offsetof(V360Context, x)
54 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
55 #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
57 static const AVOption v360_options[] = {
58 { "input", "set input projection", OFFSET(in), AV_OPT_TYPE_INT, {.i64=EQUIRECTANGULAR}, 0, NB_PROJECTIONS-1, FLAGS, "in" },
59 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "in" },
60 { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "in" },
61 { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "in" },
62 { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "in" },
63 { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "in" },
64 { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "in" },
65 { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "in" },
66 {"rectilinear", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "in" },
67 { "gnomonic", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "in" },
68 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
69 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "in" },
70 { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "in" },
71 { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "in" },
72 { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "in" },
73 { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "in" },
74 { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 0, 0, FLAGS, "in" },
75 {"sinusoidal", "sinusoidal", 0, AV_OPT_TYPE_CONST, {.i64=SINUSOIDAL}, 0, 0, FLAGS, "in" },
76 { "fisheye", "fisheye", 0, AV_OPT_TYPE_CONST, {.i64=FISHEYE}, 0, 0, FLAGS, "in" },
77 { "pannini", "pannini", 0, AV_OPT_TYPE_CONST, {.i64=PANNINI}, 0, 0, FLAGS, "in" },
78 {"cylindrical", "cylindrical", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICAL}, 0, 0, FLAGS, "in" },
79 {"tetrahedron", "tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=TETRAHEDRON}, 0, 0, FLAGS, "in" },
80 {"barrelsplit", "barrel split facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL_SPLIT}, 0, 0, FLAGS, "in" },
81 { "tsp", "truncated square pyramid", 0, AV_OPT_TYPE_CONST, {.i64=TSPYRAMID}, 0, 0, FLAGS, "in" },
82 { "hequirect", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "in" },
83 { "he", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "in" },
84 { "equisolid", "equisolid", 0, AV_OPT_TYPE_CONST, {.i64=EQUISOLID}, 0, 0, FLAGS, "in" },
85 { "og", "orthographic", 0, AV_OPT_TYPE_CONST, {.i64=ORTHOGRAPHIC}, 0, 0, FLAGS, "in" },
86 {"octahedron", "octahedron", 0, AV_OPT_TYPE_CONST, {.i64=OCTAHEDRON}, 0, 0, FLAGS, "in" },
87 { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" },
88 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
89 { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
90 { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "out" },
91 { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "out" },
92 { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "out" },
93 { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "out" },
94 { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
95 {"rectilinear", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
96 { "gnomonic", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
97 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
98 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
99 { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "out" },
100 { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "out" },
101 { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "out" },
102 { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "out" },
103 { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 0, 0, FLAGS, "out" },
104 {"sinusoidal", "sinusoidal", 0, AV_OPT_TYPE_CONST, {.i64=SINUSOIDAL}, 0, 0, FLAGS, "out" },
105 { "fisheye", "fisheye", 0, AV_OPT_TYPE_CONST, {.i64=FISHEYE}, 0, 0, FLAGS, "out" },
106 { "pannini", "pannini", 0, AV_OPT_TYPE_CONST, {.i64=PANNINI}, 0, 0, FLAGS, "out" },
107 {"cylindrical", "cylindrical", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICAL}, 0, 0, FLAGS, "out" },
108 {"perspective", "perspective", 0, AV_OPT_TYPE_CONST, {.i64=PERSPECTIVE}, 0, 0, FLAGS, "out" },
109 {"tetrahedron", "tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=TETRAHEDRON}, 0, 0, FLAGS, "out" },
110 {"barrelsplit", "barrel split facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL_SPLIT}, 0, 0, FLAGS, "out" },
111 { "tsp", "truncated square pyramid", 0, AV_OPT_TYPE_CONST, {.i64=TSPYRAMID}, 0, 0, FLAGS, "out" },
112 { "hequirect", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "out" },
113 { "he", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "out" },
114 { "equisolid", "equisolid", 0, AV_OPT_TYPE_CONST, {.i64=EQUISOLID}, 0, 0, FLAGS, "out" },
115 { "og", "orthographic", 0, AV_OPT_TYPE_CONST, {.i64=ORTHOGRAPHIC}, 0, 0, FLAGS, "out" },
116 {"octahedron", "octahedron", 0, AV_OPT_TYPE_CONST, {.i64=OCTAHEDRON}, 0, 0, FLAGS, "out" },
117 { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },
118 { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
119 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
120 { "line", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
121 { "linear", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
122 { "lagrange9", "lagrange9 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LAGRANGE9}, 0, 0, FLAGS, "interp" },
123 { "cube", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
124 { "cubic", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
125 { "lanc", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
126 { "lanczos", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
127 { "sp16", "spline16 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=SPLINE16}, 0, 0, FLAGS, "interp" },
128 { "spline16", "spline16 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=SPLINE16}, 0, 0, FLAGS, "interp" },
129 { "gauss", "gaussian interpolation", 0, AV_OPT_TYPE_CONST, {.i64=GAUSSIAN}, 0, 0, FLAGS, "interp" },
130 { "gaussian", "gaussian interpolation", 0, AV_OPT_TYPE_CONST, {.i64=GAUSSIAN}, 0, 0, FLAGS, "interp" },
131 { "w", "output width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "w"},
132 { "h", "output height", OFFSET(height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "h"},
133 { "in_stereo", "input stereo format", OFFSET(in_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
134 {"out_stereo", "output stereo format", OFFSET(out_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
135 { "2d", "2d mono", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_2D}, 0, 0, FLAGS, "stereo" },
136 { "sbs", "side by side", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_SBS}, 0, 0, FLAGS, "stereo" },
137 { "tb", "top bottom", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_TB}, 0, 0, FLAGS, "stereo" },
138 { "in_forder", "input cubemap face order", OFFSET(in_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "in_forder"},
139 {"out_forder", "output cubemap face order", OFFSET(out_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "out_forder"},
140 { "in_frot", "input cubemap face rotation", OFFSET(in_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "in_frot"},
141 { "out_frot", "output cubemap face rotation",OFFSET(out_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "out_frot"},
142 { "in_pad", "percent input cubemap pads", OFFSET(in_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 0.1,TFLAGS, "in_pad"},
143 { "out_pad", "percent output cubemap pads", OFFSET(out_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 0.1,TFLAGS, "out_pad"},
144 { "fin_pad", "fixed input cubemap pads", OFFSET(fin_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100,TFLAGS, "fin_pad"},
145 { "fout_pad", "fixed output cubemap pads", OFFSET(fout_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100,TFLAGS, "fout_pad"},
146 { "yaw", "yaw rotation", OFFSET(yaw), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "yaw"},
147 { "pitch", "pitch rotation", OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "pitch"},
148 { "roll", "roll rotation", OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "roll"},
149 { "rorder", "rotation order", OFFSET(rorder), AV_OPT_TYPE_STRING, {.str="ypr"}, 0, 0,TFLAGS, "rorder"},
150 { "h_fov", "output horizontal field of view",OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f,TFLAGS, "h_fov"},
151 { "v_fov", "output vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f,TFLAGS, "v_fov"},
152 { "d_fov", "output diagonal field of view", OFFSET(d_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "d_fov"},
153 { "h_flip", "flip out video horizontally", OFFSET(h_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "h_flip"},
154 { "v_flip", "flip out video vertically", OFFSET(v_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "v_flip"},
155 { "d_flip", "flip out video indepth", OFFSET(d_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "d_flip"},
156 { "ih_flip", "flip in video horizontally", OFFSET(ih_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "ih_flip"},
157 { "iv_flip", "flip in video vertically", OFFSET(iv_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "iv_flip"},
158 { "in_trans", "transpose video input", OFFSET(in_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "in_transpose"},
159 { "out_trans", "transpose video output", OFFSET(out_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "out_transpose"},
160 { "ih_fov", "input horizontal field of view",OFFSET(ih_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f,TFLAGS, "ih_fov"},
161 { "iv_fov", "input vertical field of view", OFFSET(iv_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f,TFLAGS, "iv_fov"},
162 { "id_fov", "input diagonal field of view", OFFSET(id_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "id_fov"},
163 {"alpha_mask", "build mask in alpha plane", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "alpha"},
167 AVFILTER_DEFINE_CLASS(v360);
169 static int query_formats(AVFilterContext *ctx)
171 V360Context *s = ctx->priv;
172 static const enum AVPixelFormat pix_fmts[] = {
174 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
175 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
176 AV_PIX_FMT_YUVA444P16,
179 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
180 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
181 AV_PIX_FMT_YUVA422P16,
184 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
185 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
188 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
189 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
193 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P9,
194 AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
195 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
198 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV440P10,
199 AV_PIX_FMT_YUV440P12,
202 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P9,
203 AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
204 AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
207 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P9,
208 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
209 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
218 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9,
219 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
220 AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
223 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
224 AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
227 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
228 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
229 AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
233 static const enum AVPixelFormat alpha_pix_fmts[] = {
234 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
235 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
236 AV_PIX_FMT_YUVA444P16,
237 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
238 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
239 AV_PIX_FMT_YUVA422P16,
240 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
241 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
242 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
243 AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
247 AVFilterFormats *fmts_list = ff_make_format_list(s->alpha ? alpha_pix_fmts : pix_fmts);
249 return AVERROR(ENOMEM);
250 return ff_set_common_formats(ctx, fmts_list);
253 #define DEFINE_REMAP1_LINE(bits, div) \
254 static void remap1_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *const src, \
255 ptrdiff_t in_linesize, \
256 const int16_t *const u, const int16_t *const v, \
257 const int16_t *const ker) \
259 const uint##bits##_t *const s = (const uint##bits##_t *const)src; \
260 uint##bits##_t *d = (uint##bits##_t *)dst; \
262 in_linesize /= div; \
264 for (int x = 0; x < width; x++) \
265 d[x] = s[v[x] * in_linesize + u[x]]; \
268 DEFINE_REMAP1_LINE( 8, 1)
269 DEFINE_REMAP1_LINE(16, 2)
272 * Generate remapping function with a given window size and pixel depth.
274 * @param ws size of interpolation window
275 * @param bits number of bits per pixel
277 #define DEFINE_REMAP(ws, bits) \
278 static int remap##ws##_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
280 ThreadData *td = arg; \
281 const V360Context *s = ctx->priv; \
282 const SliceXYRemap *r = &s->slice_remap[jobnr]; \
283 const AVFrame *in = td->in; \
284 AVFrame *out = td->out; \
286 for (int stereo = 0; stereo < 1 + s->out_stereo > STEREO_2D; stereo++) { \
287 for (int plane = 0; plane < s->nb_planes; plane++) { \
288 const unsigned map = s->map[plane]; \
289 const int in_linesize = in->linesize[plane]; \
290 const int out_linesize = out->linesize[plane]; \
291 const int uv_linesize = s->uv_linesize[plane]; \
292 const int in_offset_w = stereo ? s->in_offset_w[plane] : 0; \
293 const int in_offset_h = stereo ? s->in_offset_h[plane] : 0; \
294 const int out_offset_w = stereo ? s->out_offset_w[plane] : 0; \
295 const int out_offset_h = stereo ? s->out_offset_h[plane] : 0; \
296 const uint8_t *const src = in->data[plane] + \
297 in_offset_h * in_linesize + in_offset_w * (bits >> 3); \
298 uint8_t *dst = out->data[plane] + out_offset_h * out_linesize + out_offset_w * (bits >> 3); \
299 const uint8_t *mask = plane == 3 ? r->mask : NULL; \
300 const int width = s->pr_width[plane]; \
301 const int height = s->pr_height[plane]; \
303 const int slice_start = (height * jobnr ) / nb_jobs; \
304 const int slice_end = (height * (jobnr + 1)) / nb_jobs; \
306 for (int y = slice_start; y < slice_end && !mask; y++) { \
307 const int16_t *const u = r->u[map] + (y - slice_start) * uv_linesize * ws * ws; \
308 const int16_t *const v = r->v[map] + (y - slice_start) * uv_linesize * ws * ws; \
309 const int16_t *const ker = r->ker[map] + (y - slice_start) * uv_linesize * ws * ws; \
311 s->remap_line(dst + y * out_linesize, width, src, in_linesize, u, v, ker); \
314 for (int y = slice_start; y < slice_end && mask; y++) { \
315 memcpy(dst + y * out_linesize, mask + \
316 (y - slice_start) * width * (bits >> 3), width * (bits >> 3)); \
333 #define DEFINE_REMAP_LINE(ws, bits, div) \
334 static void remap##ws##_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *const src, \
335 ptrdiff_t in_linesize, \
336 const int16_t *const u, const int16_t *const v, \
337 const int16_t *const ker) \
339 const uint##bits##_t *const s = (const uint##bits##_t *const)src; \
340 uint##bits##_t *d = (uint##bits##_t *)dst; \
342 in_linesize /= div; \
344 for (int x = 0; x < width; x++) { \
345 const int16_t *const uu = u + x * ws * ws; \
346 const int16_t *const vv = v + x * ws * ws; \
347 const int16_t *const kker = ker + x * ws * ws; \
350 for (int i = 0; i < ws; i++) { \
351 const int iws = i * ws; \
352 for (int j = 0; j < ws; j++) { \
353 tmp += kker[iws + j] * s[vv[iws + j] * in_linesize + uu[iws + j]]; \
357 d[x] = av_clip_uint##bits(tmp >> 14); \
361 DEFINE_REMAP_LINE(2, 8, 1)
362 DEFINE_REMAP_LINE(3, 8, 1)
363 DEFINE_REMAP_LINE(4, 8, 1)
364 DEFINE_REMAP_LINE(2, 16, 2)
365 DEFINE_REMAP_LINE(3, 16, 2)
366 DEFINE_REMAP_LINE(4, 16, 2)
368 void ff_v360_init(V360Context *s, int depth)
372 s->remap_line = depth <= 8 ? remap1_8bit_line_c : remap1_16bit_line_c;
375 s->remap_line = depth <= 8 ? remap2_8bit_line_c : remap2_16bit_line_c;
378 s->remap_line = depth <= 8 ? remap3_8bit_line_c : remap3_16bit_line_c;
384 s->remap_line = depth <= 8 ? remap4_8bit_line_c : remap4_16bit_line_c;
389 ff_v360_init_x86(s, depth);
393 * Save nearest pixel coordinates for remapping.
395 * @param du horizontal relative coordinate
396 * @param dv vertical relative coordinate
397 * @param rmap calculated 4x4 window
398 * @param u u remap data
399 * @param v v remap data
400 * @param ker ker remap data
402 static void nearest_kernel(float du, float dv, const XYRemap *rmap,
403 int16_t *u, int16_t *v, int16_t *ker)
405 const int i = lrintf(dv) + 1;
406 const int j = lrintf(du) + 1;
408 u[0] = rmap->u[i][j];
409 v[0] = rmap->v[i][j];
413 * Calculate kernel for bilinear interpolation.
415 * @param du horizontal relative coordinate
416 * @param dv vertical relative coordinate
417 * @param rmap calculated 4x4 window
418 * @param u u remap data
419 * @param v v remap data
420 * @param ker ker remap data
422 static void bilinear_kernel(float du, float dv, const XYRemap *rmap,
423 int16_t *u, int16_t *v, int16_t *ker)
425 for (int i = 0; i < 2; i++) {
426 for (int j = 0; j < 2; j++) {
427 u[i * 2 + j] = rmap->u[i + 1][j + 1];
428 v[i * 2 + j] = rmap->v[i + 1][j + 1];
432 ker[0] = lrintf((1.f - du) * (1.f - dv) * 16385.f);
433 ker[1] = lrintf( du * (1.f - dv) * 16385.f);
434 ker[2] = lrintf((1.f - du) * dv * 16385.f);
435 ker[3] = lrintf( du * dv * 16385.f);
439 * Calculate 1-dimensional lagrange coefficients.
441 * @param t relative coordinate
442 * @param coeffs coefficients
444 static inline void calculate_lagrange_coeffs(float t, float *coeffs)
446 coeffs[0] = (t - 1.f) * (t - 2.f) * 0.5f;
447 coeffs[1] = -t * (t - 2.f);
448 coeffs[2] = t * (t - 1.f) * 0.5f;
452 * Calculate kernel for lagrange interpolation.
454 * @param du horizontal relative coordinate
455 * @param dv vertical relative coordinate
456 * @param rmap calculated 4x4 window
457 * @param u u remap data
458 * @param v v remap data
459 * @param ker ker remap data
461 static void lagrange_kernel(float du, float dv, const XYRemap *rmap,
462 int16_t *u, int16_t *v, int16_t *ker)
467 calculate_lagrange_coeffs(du, du_coeffs);
468 calculate_lagrange_coeffs(dv, dv_coeffs);
470 for (int i = 0; i < 3; i++) {
471 for (int j = 0; j < 3; j++) {
472 u[i * 3 + j] = rmap->u[i + 1][j + 1];
473 v[i * 3 + j] = rmap->v[i + 1][j + 1];
474 ker[i * 3 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
480 * Calculate 1-dimensional cubic coefficients.
482 * @param t relative coordinate
483 * @param coeffs coefficients
485 static inline void calculate_bicubic_coeffs(float t, float *coeffs)
487 const float tt = t * t;
488 const float ttt = t * t * t;
490 coeffs[0] = - t / 3.f + tt / 2.f - ttt / 6.f;
491 coeffs[1] = 1.f - t / 2.f - tt + ttt / 2.f;
492 coeffs[2] = t + tt / 2.f - ttt / 2.f;
493 coeffs[3] = - t / 6.f + ttt / 6.f;
497 * Calculate kernel for bicubic interpolation.
499 * @param du horizontal relative coordinate
500 * @param dv vertical relative coordinate
501 * @param rmap calculated 4x4 window
502 * @param u u remap data
503 * @param v v remap data
504 * @param ker ker remap data
506 static void bicubic_kernel(float du, float dv, const XYRemap *rmap,
507 int16_t *u, int16_t *v, int16_t *ker)
512 calculate_bicubic_coeffs(du, du_coeffs);
513 calculate_bicubic_coeffs(dv, dv_coeffs);
515 for (int i = 0; i < 4; i++) {
516 for (int j = 0; j < 4; j++) {
517 u[i * 4 + j] = rmap->u[i][j];
518 v[i * 4 + j] = rmap->v[i][j];
519 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
525 * Calculate 1-dimensional lanczos coefficients.
527 * @param t relative coordinate
528 * @param coeffs coefficients
530 static inline void calculate_lanczos_coeffs(float t, float *coeffs)
534 for (int i = 0; i < 4; i++) {
535 const float x = M_PI * (t - i + 1);
539 coeffs[i] = sinf(x) * sinf(x / 2.f) / (x * x / 2.f);
544 for (int i = 0; i < 4; i++) {
550 * Calculate kernel for lanczos interpolation.
552 * @param du horizontal relative coordinate
553 * @param dv vertical relative coordinate
554 * @param rmap calculated 4x4 window
555 * @param u u remap data
556 * @param v v remap data
557 * @param ker ker remap data
559 static void lanczos_kernel(float du, float dv, const XYRemap *rmap,
560 int16_t *u, int16_t *v, int16_t *ker)
565 calculate_lanczos_coeffs(du, du_coeffs);
566 calculate_lanczos_coeffs(dv, dv_coeffs);
568 for (int i = 0; i < 4; i++) {
569 for (int j = 0; j < 4; j++) {
570 u[i * 4 + j] = rmap->u[i][j];
571 v[i * 4 + j] = rmap->v[i][j];
572 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
578 * Calculate 1-dimensional spline16 coefficients.
580 * @param t relative coordinate
581 * @param coeffs coefficients
583 static void calculate_spline16_coeffs(float t, float *coeffs)
585 coeffs[0] = ((-1.f / 3.f * t + 0.8f) * t - 7.f / 15.f) * t;
586 coeffs[1] = ((t - 9.f / 5.f) * t - 0.2f) * t + 1.f;
587 coeffs[2] = ((6.f / 5.f - t) * t + 0.8f) * t;
588 coeffs[3] = ((1.f / 3.f * t - 0.2f) * t - 2.f / 15.f) * t;
592 * Calculate kernel for spline16 interpolation.
594 * @param du horizontal relative coordinate
595 * @param dv vertical relative coordinate
596 * @param rmap calculated 4x4 window
597 * @param u u remap data
598 * @param v v remap data
599 * @param ker ker remap data
601 static void spline16_kernel(float du, float dv, const XYRemap *rmap,
602 int16_t *u, int16_t *v, int16_t *ker)
607 calculate_spline16_coeffs(du, du_coeffs);
608 calculate_spline16_coeffs(dv, dv_coeffs);
610 for (int i = 0; i < 4; i++) {
611 for (int j = 0; j < 4; j++) {
612 u[i * 4 + j] = rmap->u[i][j];
613 v[i * 4 + j] = rmap->v[i][j];
614 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
620 * Calculate 1-dimensional gaussian coefficients.
622 * @param t relative coordinate
623 * @param coeffs coefficients
625 static void calculate_gaussian_coeffs(float t, float *coeffs)
629 for (int i = 0; i < 4; i++) {
630 const float x = t - (i - 1);
634 coeffs[i] = expf(-2.f * x * x) * expf(-x * x / 2.f);
639 for (int i = 0; i < 4; i++) {
645 * Calculate kernel for gaussian interpolation.
647 * @param du horizontal relative coordinate
648 * @param dv vertical relative coordinate
649 * @param rmap calculated 4x4 window
650 * @param u u remap data
651 * @param v v remap data
652 * @param ker ker remap data
654 static void gaussian_kernel(float du, float dv, const XYRemap *rmap,
655 int16_t *u, int16_t *v, int16_t *ker)
660 calculate_gaussian_coeffs(du, du_coeffs);
661 calculate_gaussian_coeffs(dv, dv_coeffs);
663 for (int i = 0; i < 4; i++) {
664 for (int j = 0; j < 4; j++) {
665 u[i * 4 + j] = rmap->u[i][j];
666 v[i * 4 + j] = rmap->v[i][j];
667 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
673 * Modulo operation with only positive remainders.
678 * @return positive remainder of (a / b)
680 static inline int mod(int a, int b)
682 const int res = a % b;
691 * Reflect y operation.
693 * @param y input vertical position
694 * @param h input height
696 static inline int reflecty(int y, int h)
701 return 2 * h - 1 - y;
708 * Reflect x operation for equirect.
710 * @param x input horizontal position
711 * @param y input vertical position
712 * @param w input width
713 * @param h input height
715 static inline int ereflectx(int x, int y, int w, int h)
724 * Reflect x operation.
726 * @param x input horizontal position
727 * @param y input vertical position
728 * @param w input width
729 * @param h input height
731 static inline int reflectx(int x, int y, int w, int h)
740 * Convert char to corresponding direction.
741 * Used for cubemap options.
743 static int get_direction(char c)
764 * Convert char to corresponding rotation angle.
765 * Used for cubemap options.
767 static int get_rotation(char c)
784 * Convert char to corresponding rotation order.
786 static int get_rorder(char c)
804 * Prepare data for processing cubemap input format.
806 * @param ctx filter context
810 static int prepare_cube_in(AVFilterContext *ctx)
812 V360Context *s = ctx->priv;
814 for (int face = 0; face < NB_FACES; face++) {
815 const char c = s->in_forder[face];
819 av_log(ctx, AV_LOG_ERROR,
820 "Incomplete in_forder option. Direction for all 6 faces should be specified.\n");
821 return AVERROR(EINVAL);
824 direction = get_direction(c);
825 if (direction == -1) {
826 av_log(ctx, AV_LOG_ERROR,
827 "Incorrect direction symbol '%c' in in_forder option.\n", c);
828 return AVERROR(EINVAL);
831 s->in_cubemap_face_order[direction] = face;
834 for (int face = 0; face < NB_FACES; face++) {
835 const char c = s->in_frot[face];
839 av_log(ctx, AV_LOG_ERROR,
840 "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n");
841 return AVERROR(EINVAL);
844 rotation = get_rotation(c);
845 if (rotation == -1) {
846 av_log(ctx, AV_LOG_ERROR,
847 "Incorrect rotation symbol '%c' in in_frot option.\n", c);
848 return AVERROR(EINVAL);
851 s->in_cubemap_face_rotation[face] = rotation;
858 * Prepare data for processing cubemap output format.
860 * @param ctx filter context
864 static int prepare_cube_out(AVFilterContext *ctx)
866 V360Context *s = ctx->priv;
868 for (int face = 0; face < NB_FACES; face++) {
869 const char c = s->out_forder[face];
873 av_log(ctx, AV_LOG_ERROR,
874 "Incomplete out_forder option. Direction for all 6 faces should be specified.\n");
875 return AVERROR(EINVAL);
878 direction = get_direction(c);
879 if (direction == -1) {
880 av_log(ctx, AV_LOG_ERROR,
881 "Incorrect direction symbol '%c' in out_forder option.\n", c);
882 return AVERROR(EINVAL);
885 s->out_cubemap_direction_order[face] = direction;
888 for (int face = 0; face < NB_FACES; face++) {
889 const char c = s->out_frot[face];
893 av_log(ctx, AV_LOG_ERROR,
894 "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n");
895 return AVERROR(EINVAL);
898 rotation = get_rotation(c);
899 if (rotation == -1) {
900 av_log(ctx, AV_LOG_ERROR,
901 "Incorrect rotation symbol '%c' in out_frot option.\n", c);
902 return AVERROR(EINVAL);
905 s->out_cubemap_face_rotation[face] = rotation;
911 static inline void rotate_cube_face(float *uf, float *vf, int rotation)
937 static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
968 static void normalize_vector(float *vec)
970 const float norm = sqrtf(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
978 * Calculate 3D coordinates on sphere for corresponding cubemap position.
979 * Common operation for every cubemap.
981 * @param s filter private context
982 * @param uf horizontal cubemap coordinate [0, 1)
983 * @param vf vertical cubemap coordinate [0, 1)
984 * @param face face of cubemap
985 * @param vec coordinates on sphere
986 * @param scalew scale for uf
987 * @param scaleh scale for vf
989 static void cube_to_xyz(const V360Context *s,
990 float uf, float vf, int face,
991 float *vec, float scalew, float scaleh)
993 const int direction = s->out_cubemap_direction_order[face];
999 rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]);
1001 switch (direction) {
1040 normalize_vector(vec);
1044 * Calculate cubemap position for corresponding 3D coordinates on sphere.
1045 * Common operation for every cubemap.
1047 * @param s filter private context
1048 * @param vec coordinated on sphere
1049 * @param uf horizontal cubemap coordinate [0, 1)
1050 * @param vf vertical cubemap coordinate [0, 1)
1051 * @param direction direction of view
1053 static void xyz_to_cube(const V360Context *s,
1055 float *uf, float *vf, int *direction)
1057 const float phi = atan2f(vec[0], vec[2]);
1058 const float theta = asinf(vec[1]);
1059 float phi_norm, theta_threshold;
1062 if (phi >= -M_PI_4 && phi < M_PI_4) {
1065 } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) {
1067 phi_norm = phi + M_PI_2;
1068 } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) {
1070 phi_norm = phi - M_PI_2;
1073 phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI);
1076 theta_threshold = atanf(cosf(phi_norm));
1077 if (theta > theta_threshold) {
1079 } else if (theta < -theta_threshold) {
1083 switch (*direction) {
1085 *uf = -vec[2] / vec[0];
1086 *vf = vec[1] / vec[0];
1089 *uf = -vec[2] / vec[0];
1090 *vf = -vec[1] / vec[0];
1093 *uf = -vec[0] / vec[1];
1094 *vf = -vec[2] / vec[1];
1097 *uf = vec[0] / vec[1];
1098 *vf = -vec[2] / vec[1];
1101 *uf = vec[0] / vec[2];
1102 *vf = vec[1] / vec[2];
1105 *uf = vec[0] / vec[2];
1106 *vf = -vec[1] / vec[2];
1112 face = s->in_cubemap_face_order[*direction];
1113 rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]);
1115 (*uf) *= s->input_mirror_modifier[0];
1116 (*vf) *= s->input_mirror_modifier[1];
1120 * Find position on another cube face in case of overflow/underflow.
1121 * Used for calculation of interpolation window.
1123 * @param s filter private context
1124 * @param uf horizontal cubemap coordinate
1125 * @param vf vertical cubemap coordinate
1126 * @param direction direction of view
1127 * @param new_uf new horizontal cubemap coordinate
1128 * @param new_vf new vertical cubemap coordinate
1129 * @param face face position on cubemap
1131 static void process_cube_coordinates(const V360Context *s,
1132 float uf, float vf, int direction,
1133 float *new_uf, float *new_vf, int *face)
1136 * Cubemap orientation
1143 * +-------+-------+-------+-------+ ^ e |
1145 * | left | front | right | back | | g |
1146 * +-------+-------+-------+-------+ v h v
1152 *face = s->in_cubemap_face_order[direction];
1153 rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]);
1155 if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) {
1156 // There are no pixels to use in this case
1159 } else if (uf < -1.f) {
1161 switch (direction) {
1195 } else if (uf >= 1.f) {
1197 switch (direction) {
1231 } else if (vf < -1.f) {
1233 switch (direction) {
1267 } else if (vf >= 1.f) {
1269 switch (direction) {
1309 *face = s->in_cubemap_face_order[direction];
1310 rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]);
1314 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
1316 * @param s filter private context
1317 * @param i horizontal position on frame [0, width)
1318 * @param j vertical position on frame [0, height)
1319 * @param width frame width
1320 * @param height frame height
1321 * @param vec coordinates on sphere
1323 static int cube3x2_to_xyz(const V360Context *s,
1324 int i, int j, int width, int height,
1327 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width / 3.f) : 1.f - s->out_pad;
1328 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 2.f) : 1.f - s->out_pad;
1330 const float ew = width / 3.f;
1331 const float eh = height / 2.f;
1333 const int u_face = floorf(i / ew);
1334 const int v_face = floorf(j / eh);
1335 const int face = u_face + 3 * v_face;
1337 const int u_shift = ceilf(ew * u_face);
1338 const int v_shift = ceilf(eh * v_face);
1339 const int ewi = ceilf(ew * (u_face + 1)) - u_shift;
1340 const int ehi = ceilf(eh * (v_face + 1)) - v_shift;
1342 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1343 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1345 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1351 * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
1353 * @param s filter private context
1354 * @param vec coordinates on sphere
1355 * @param width frame width
1356 * @param height frame height
1357 * @param us horizontal coordinates for interpolation window
1358 * @param vs vertical coordinates for interpolation window
1359 * @param du horizontal relative coordinate
1360 * @param dv vertical relative coordinate
1362 static int xyz_to_cube3x2(const V360Context *s,
1363 const float *vec, int width, int height,
1364 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1366 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width / 3.f) : 1.f - s->in_pad;
1367 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 2.f) : 1.f - s->in_pad;
1368 const float ew = width / 3.f;
1369 const float eh = height / 2.f;
1373 int direction, face;
1376 xyz_to_cube(s, vec, &uf, &vf, &direction);
1381 face = s->in_cubemap_face_order[direction];
1384 ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face);
1385 ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face);
1387 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1388 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1396 for (int i = 0; i < 4; i++) {
1397 for (int j = 0; j < 4; j++) {
1398 int new_ui = ui + j - 1;
1399 int new_vi = vi + i - 1;
1400 int u_shift, v_shift;
1401 int new_ewi, new_ehi;
1403 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1404 face = s->in_cubemap_face_order[direction];
1408 u_shift = ceilf(ew * u_face);
1409 v_shift = ceilf(eh * v_face);
1411 uf = 2.f * new_ui / ewi - 1.f;
1412 vf = 2.f * new_vi / ehi - 1.f;
1417 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1424 u_shift = ceilf(ew * u_face);
1425 v_shift = ceilf(eh * v_face);
1426 new_ewi = ceilf(ew * (u_face + 1)) - u_shift;
1427 new_ehi = ceilf(eh * (v_face + 1)) - v_shift;
1429 new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1430 new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1433 us[i][j] = u_shift + new_ui;
1434 vs[i][j] = v_shift + new_vi;
1442 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
1444 * @param s filter private context
1445 * @param i horizontal position on frame [0, width)
1446 * @param j vertical position on frame [0, height)
1447 * @param width frame width
1448 * @param height frame height
1449 * @param vec coordinates on sphere
1451 static int cube1x6_to_xyz(const V360Context *s,
1452 int i, int j, int width, int height,
1455 const float scalew = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / width : 1.f - s->out_pad;
1456 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 6.f) : 1.f - s->out_pad;
1458 const float ew = width;
1459 const float eh = height / 6.f;
1461 const int face = floorf(j / eh);
1463 const int v_shift = ceilf(eh * face);
1464 const int ehi = ceilf(eh * (face + 1)) - v_shift;
1466 const float uf = 2.f * (i + 0.5f) / ew - 1.f;
1467 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1469 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1475 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
1477 * @param s filter private context
1478 * @param i horizontal position on frame [0, width)
1479 * @param j vertical position on frame [0, height)
1480 * @param width frame width
1481 * @param height frame height
1482 * @param vec coordinates on sphere
1484 static int cube6x1_to_xyz(const V360Context *s,
1485 int i, int j, int width, int height,
1488 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width / 6.f) : 1.f - s->out_pad;
1489 const float scaleh = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / height : 1.f - s->out_pad;
1491 const float ew = width / 6.f;
1492 const float eh = height;
1494 const int face = floorf(i / ew);
1496 const int u_shift = ceilf(ew * face);
1497 const int ewi = ceilf(ew * (face + 1)) - u_shift;
1499 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1500 const float vf = 2.f * (j + 0.5f) / eh - 1.f;
1502 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1508 * Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
1510 * @param s filter private context
1511 * @param vec coordinates on sphere
1512 * @param width frame width
1513 * @param height frame height
1514 * @param us horizontal coordinates for interpolation window
1515 * @param vs vertical coordinates for interpolation window
1516 * @param du horizontal relative coordinate
1517 * @param dv vertical relative coordinate
1519 static int xyz_to_cube1x6(const V360Context *s,
1520 const float *vec, int width, int height,
1521 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1523 const float scalew = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / width : 1.f - s->in_pad;
1524 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 6.f) : 1.f - s->in_pad;
1525 const float eh = height / 6.f;
1526 const int ewi = width;
1530 int direction, face;
1532 xyz_to_cube(s, vec, &uf, &vf, &direction);
1537 face = s->in_cubemap_face_order[direction];
1538 ehi = ceilf(eh * (face + 1)) - ceilf(eh * face);
1540 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1541 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1549 for (int i = 0; i < 4; i++) {
1550 for (int j = 0; j < 4; j++) {
1551 int new_ui = ui + j - 1;
1552 int new_vi = vi + i - 1;
1556 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1557 face = s->in_cubemap_face_order[direction];
1559 v_shift = ceilf(eh * face);
1561 uf = 2.f * new_ui / ewi - 1.f;
1562 vf = 2.f * new_vi / ehi - 1.f;
1567 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1572 v_shift = ceilf(eh * face);
1573 new_ehi = ceilf(eh * (face + 1)) - v_shift;
1575 new_ui = av_clip(lrintf(0.5f * ewi * (uf + 1.f)), 0, ewi - 1);
1576 new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1580 vs[i][j] = v_shift + new_vi;
1588 * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
1590 * @param s filter private context
1591 * @param vec coordinates on sphere
1592 * @param width frame width
1593 * @param height frame height
1594 * @param us horizontal coordinates for interpolation window
1595 * @param vs vertical coordinates for interpolation window
1596 * @param du horizontal relative coordinate
1597 * @param dv vertical relative coordinate
1599 static int xyz_to_cube6x1(const V360Context *s,
1600 const float *vec, int width, int height,
1601 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1603 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width / 6.f) : 1.f - s->in_pad;
1604 const float scaleh = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / height : 1.f - s->in_pad;
1605 const float ew = width / 6.f;
1606 const int ehi = height;
1610 int direction, face;
1612 xyz_to_cube(s, vec, &uf, &vf, &direction);
1617 face = s->in_cubemap_face_order[direction];
1618 ewi = ceilf(ew * (face + 1)) - ceilf(ew * face);
1620 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1621 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1629 for (int i = 0; i < 4; i++) {
1630 for (int j = 0; j < 4; j++) {
1631 int new_ui = ui + j - 1;
1632 int new_vi = vi + i - 1;
1636 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1637 face = s->in_cubemap_face_order[direction];
1639 u_shift = ceilf(ew * face);
1641 uf = 2.f * new_ui / ewi - 1.f;
1642 vf = 2.f * new_vi / ehi - 1.f;
1647 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1652 u_shift = ceilf(ew * face);
1653 new_ewi = ceilf(ew * (face + 1)) - u_shift;
1655 new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1656 new_vi = av_clip(lrintf(0.5f * ehi * (vf + 1.f)), 0, ehi - 1);
1659 us[i][j] = u_shift + new_ui;
1668 * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
1670 * @param s filter private context
1671 * @param i horizontal position on frame [0, width)
1672 * @param j vertical position on frame [0, height)
1673 * @param width frame width
1674 * @param height frame height
1675 * @param vec coordinates on sphere
1677 static int equirect_to_xyz(const V360Context *s,
1678 int i, int j, int width, int height,
1681 const float phi = ((2.f * i + 0.5f) / width - 1.f) * M_PI;
1682 const float theta = ((2.f * j + 0.5f) / height - 1.f) * M_PI_2;
1684 const float sin_phi = sinf(phi);
1685 const float cos_phi = cosf(phi);
1686 const float sin_theta = sinf(theta);
1687 const float cos_theta = cosf(theta);
1689 vec[0] = cos_theta * sin_phi;
1691 vec[2] = cos_theta * cos_phi;
1697 * Calculate 3D coordinates on sphere for corresponding frame position in half equirectangular format.
1699 * @param s filter private context
1700 * @param i horizontal position on frame [0, width)
1701 * @param j vertical position on frame [0, height)
1702 * @param width frame width
1703 * @param height frame height
1704 * @param vec coordinates on sphere
1706 static int hequirect_to_xyz(const V360Context *s,
1707 int i, int j, int width, int height,
1710 const float phi = ((2.f * i + 0.5f) / width - 1.f) * M_PI_2;
1711 const float theta = ((2.f * j + 0.5f) / height - 1.f) * M_PI_2;
1713 const float sin_phi = sinf(phi);
1714 const float cos_phi = cosf(phi);
1715 const float sin_theta = sinf(theta);
1716 const float cos_theta = cosf(theta);
1718 vec[0] = cos_theta * sin_phi;
1720 vec[2] = cos_theta * cos_phi;
1726 * Prepare data for processing stereographic output format.
1728 * @param ctx filter context
1730 * @return error code
1732 static int prepare_stereographic_out(AVFilterContext *ctx)
1734 V360Context *s = ctx->priv;
1736 s->flat_range[0] = tanf(FFMIN(s->h_fov, 359.f) * M_PI / 720.f);
1737 s->flat_range[1] = tanf(FFMIN(s->v_fov, 359.f) * M_PI / 720.f);
1743 * Calculate 3D coordinates on sphere for corresponding frame position in stereographic format.
1745 * @param s filter private context
1746 * @param i horizontal position on frame [0, width)
1747 * @param j vertical position on frame [0, height)
1748 * @param width frame width
1749 * @param height frame height
1750 * @param vec coordinates on sphere
1752 static int stereographic_to_xyz(const V360Context *s,
1753 int i, int j, int width, int height,
1756 const float x = ((2.f * i + 1.f) / width - 1.f) * s->flat_range[0];
1757 const float y = ((2.f * j + 1.f) / height - 1.f) * s->flat_range[1];
1758 const float r = hypotf(x, y);
1759 const float theta = atanf(r) * 2.f;
1760 const float sin_theta = sinf(theta);
1762 vec[0] = x / r * sin_theta;
1763 vec[1] = y / r * sin_theta;
1764 vec[2] = cosf(theta);
1766 normalize_vector(vec);
1772 * Prepare data for processing stereographic input format.
1774 * @param ctx filter context
1776 * @return error code
1778 static int prepare_stereographic_in(AVFilterContext *ctx)
1780 V360Context *s = ctx->priv;
1782 s->iflat_range[0] = tanf(FFMIN(s->ih_fov, 359.f) * M_PI / 720.f);
1783 s->iflat_range[1] = tanf(FFMIN(s->iv_fov, 359.f) * M_PI / 720.f);
1789 * Calculate frame position in stereographic format for corresponding 3D coordinates on sphere.
1791 * @param s filter private context
1792 * @param vec coordinates on sphere
1793 * @param width frame width
1794 * @param height frame height
1795 * @param us horizontal coordinates for interpolation window
1796 * @param vs vertical coordinates for interpolation window
1797 * @param du horizontal relative coordinate
1798 * @param dv vertical relative coordinate
1800 static int xyz_to_stereographic(const V360Context *s,
1801 const float *vec, int width, int height,
1802 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1804 const float theta = acosf(vec[2]);
1805 const float r = tanf(theta * 0.5f);
1806 const float c = r / hypotf(vec[0], vec[1]);
1807 const float x = vec[0] * c / s->iflat_range[0] * s->input_mirror_modifier[0];
1808 const float y = vec[1] * c / s->iflat_range[1] * s->input_mirror_modifier[1];
1810 const float uf = (x + 1.f) * width / 2.f;
1811 const float vf = (y + 1.f) * height / 2.f;
1813 const int ui = floorf(uf);
1814 const int vi = floorf(vf);
1816 const int visible = isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
1818 *du = visible ? uf - ui : 0.f;
1819 *dv = visible ? vf - vi : 0.f;
1821 for (int i = 0; i < 4; i++) {
1822 for (int j = 0; j < 4; j++) {
1823 us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
1824 vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
1832 * Prepare data for processing equisolid output format.
1834 * @param ctx filter context
1836 * @return error code
1838 static int prepare_equisolid_out(AVFilterContext *ctx)
1840 V360Context *s = ctx->priv;
1842 s->flat_range[0] = sinf(s->h_fov * M_PI / 720.f);
1843 s->flat_range[1] = sinf(s->v_fov * M_PI / 720.f);
1849 * Calculate 3D coordinates on sphere for corresponding frame position in equisolid format.
1851 * @param s filter private context
1852 * @param i horizontal position on frame [0, width)
1853 * @param j vertical position on frame [0, height)
1854 * @param width frame width
1855 * @param height frame height
1856 * @param vec coordinates on sphere
1858 static int equisolid_to_xyz(const V360Context *s,
1859 int i, int j, int width, int height,
1862 const float x = ((2.f * i + 1.f) / width - 1.f) * s->flat_range[0];
1863 const float y = ((2.f * j + 1.f) / height - 1.f) * s->flat_range[1];
1864 const float r = hypotf(x, y);
1865 const float theta = asinf(r) * 2.f;
1866 const float sin_theta = sinf(theta);
1868 vec[0] = x / r * sin_theta;
1869 vec[1] = y / r * sin_theta;
1870 vec[2] = cosf(theta);
1872 normalize_vector(vec);
1878 * Prepare data for processing equisolid input format.
1880 * @param ctx filter context
1882 * @return error code
1884 static int prepare_equisolid_in(AVFilterContext *ctx)
1886 V360Context *s = ctx->priv;
1888 s->iflat_range[0] = sinf(FFMIN(s->ih_fov, 359.f) * M_PI / 720.f);
1889 s->iflat_range[1] = sinf(FFMIN(s->iv_fov, 359.f) * M_PI / 720.f);
1895 * Calculate frame position in equisolid format for corresponding 3D coordinates on sphere.
1897 * @param s filter private context
1898 * @param vec coordinates on sphere
1899 * @param width frame width
1900 * @param height frame height
1901 * @param us horizontal coordinates for interpolation window
1902 * @param vs vertical coordinates for interpolation window
1903 * @param du horizontal relative coordinate
1904 * @param dv vertical relative coordinate
1906 static int xyz_to_equisolid(const V360Context *s,
1907 const float *vec, int width, int height,
1908 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1910 const float theta = acosf(vec[2]);
1911 const float r = sinf(theta * 0.5f);
1912 const float c = r / hypotf(vec[0], vec[1]);
1913 const float x = vec[0] * c / s->iflat_range[0] * s->input_mirror_modifier[0];
1914 const float y = vec[1] * c / s->iflat_range[1] * s->input_mirror_modifier[1];
1916 const float uf = (x + 1.f) * width / 2.f;
1917 const float vf = (y + 1.f) * height / 2.f;
1919 const int ui = floorf(uf);
1920 const int vi = floorf(vf);
1922 const int visible = isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
1924 *du = visible ? uf - ui : 0.f;
1925 *dv = visible ? vf - vi : 0.f;
1927 for (int i = 0; i < 4; i++) {
1928 for (int j = 0; j < 4; j++) {
1929 us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
1930 vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
1938 * Prepare data for processing orthographic output format.
1940 * @param ctx filter context
1942 * @return error code
1944 static int prepare_orthographic_out(AVFilterContext *ctx)
1946 V360Context *s = ctx->priv;
1948 s->flat_range[0] = sinf(FFMIN(s->h_fov, 180.f) * M_PI / 360.f);
1949 s->flat_range[1] = sinf(FFMIN(s->v_fov, 180.f) * M_PI / 360.f);
1955 * Calculate 3D coordinates on sphere for corresponding frame position in orthographic format.
1957 * @param s filter private context
1958 * @param i horizontal position on frame [0, width)
1959 * @param j vertical position on frame [0, height)
1960 * @param width frame width
1961 * @param height frame height
1962 * @param vec coordinates on sphere
1964 static int orthographic_to_xyz(const V360Context *s,
1965 int i, int j, int width, int height,
1968 const float x = ((2.f * i + 1.f) / width - 1.f) * s->flat_range[0];
1969 const float y = ((2.f * j + 1.f) / height - 1.f) * s->flat_range[1];
1970 const float r = hypotf(x, y);
1971 const float theta = asinf(r);
1975 vec[2] = cosf(theta);
1977 normalize_vector(vec);
1983 * Prepare data for processing orthographic input format.
1985 * @param ctx filter context
1987 * @return error code
1989 static int prepare_orthographic_in(AVFilterContext *ctx)
1991 V360Context *s = ctx->priv;
1993 s->iflat_range[0] = sinf(FFMIN(s->ih_fov, 180.f) * M_PI / 360.f);
1994 s->iflat_range[1] = sinf(FFMIN(s->iv_fov, 180.f) * M_PI / 360.f);
2000 * Calculate frame position in orthographic format for corresponding 3D coordinates on sphere.
2002 * @param s filter private context
2003 * @param vec coordinates on sphere
2004 * @param width frame width
2005 * @param height frame height
2006 * @param us horizontal coordinates for interpolation window
2007 * @param vs vertical coordinates for interpolation window
2008 * @param du horizontal relative coordinate
2009 * @param dv vertical relative coordinate
2011 static int xyz_to_orthographic(const V360Context *s,
2012 const float *vec, int width, int height,
2013 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2015 const float theta = acosf(vec[2]);
2016 const float r = sinf(theta);
2017 const float c = r / hypotf(vec[0], vec[1]);
2018 const float x = vec[0] * c / s->iflat_range[0] * s->input_mirror_modifier[0];
2019 const float y = vec[1] * c / s->iflat_range[1] * s->input_mirror_modifier[1];
2021 const float uf = (x + 1.f) * width / 2.f;
2022 const float vf = (y + 1.f) * height / 2.f;
2024 const int ui = floorf(uf);
2025 const int vi = floorf(vf);
2027 const int visible = vec[2] >= 0.f && isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
2029 *du = visible ? uf - ui : 0.f;
2030 *dv = visible ? vf - vi : 0.f;
2032 for (int i = 0; i < 4; i++) {
2033 for (int j = 0; j < 4; j++) {
2034 us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
2035 vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
2043 * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
2045 * @param s filter private context
2046 * @param vec coordinates on sphere
2047 * @param width frame width
2048 * @param height frame height
2049 * @param us horizontal coordinates for interpolation window
2050 * @param vs vertical coordinates for interpolation window
2051 * @param du horizontal relative coordinate
2052 * @param dv vertical relative coordinate
2054 static int xyz_to_equirect(const V360Context *s,
2055 const float *vec, int width, int height,
2056 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2058 const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0];
2059 const float theta = asinf(vec[1]) * s->input_mirror_modifier[1];
2061 const float uf = (phi / M_PI + 1.f) * width / 2.f;
2062 const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
2064 const int ui = floorf(uf);
2065 const int vi = floorf(vf);
2070 for (int i = 0; i < 4; i++) {
2071 for (int j = 0; j < 4; j++) {
2072 us[i][j] = ereflectx(ui + j - 1, vi + i - 1, width, height);
2073 vs[i][j] = reflecty(vi + i - 1, height);
2081 * Calculate frame position in half equirectangular format for corresponding 3D coordinates on sphere.
2083 * @param s filter private context
2084 * @param vec coordinates on sphere
2085 * @param width frame width
2086 * @param height frame height
2087 * @param us horizontal coordinates for interpolation window
2088 * @param vs vertical coordinates for interpolation window
2089 * @param du horizontal relative coordinate
2090 * @param dv vertical relative coordinate
2092 static int xyz_to_hequirect(const V360Context *s,
2093 const float *vec, int width, int height,
2094 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2096 const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0];
2097 const float theta = asinf(vec[1]) * s->input_mirror_modifier[1];
2099 const float uf = (phi / M_PI_2 + 1.f) * width / 2.f;
2100 const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
2102 const int ui = floorf(uf);
2103 const int vi = floorf(vf);
2105 const int visible = phi >= -M_PI_2 && phi <= M_PI_2;
2110 for (int i = 0; i < 4; i++) {
2111 for (int j = 0; j < 4; j++) {
2112 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2113 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2121 * Prepare data for processing flat input format.
2123 * @param ctx filter context
2125 * @return error code
2127 static int prepare_flat_in(AVFilterContext *ctx)
2129 V360Context *s = ctx->priv;
2131 s->iflat_range[0] = tanf(0.5f * s->ih_fov * M_PI / 180.f);
2132 s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
2138 * Calculate frame position in flat format for corresponding 3D coordinates on sphere.
2140 * @param s filter private context
2141 * @param vec coordinates on sphere
2142 * @param width frame width
2143 * @param height frame height
2144 * @param us horizontal coordinates for interpolation window
2145 * @param vs vertical coordinates for interpolation window
2146 * @param du horizontal relative coordinate
2147 * @param dv vertical relative coordinate
2149 static int xyz_to_flat(const V360Context *s,
2150 const float *vec, int width, int height,
2151 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2153 const float theta = acosf(vec[2]);
2154 const float r = tanf(theta);
2155 const float rr = fabsf(r) < 1e+6f ? r : hypotf(width, height);
2156 const float zf = vec[2];
2157 const float h = hypotf(vec[0], vec[1]);
2158 const float c = h <= 1e-6f ? 1.f : rr / h;
2159 float uf = vec[0] * c / s->iflat_range[0] * s->input_mirror_modifier[0];
2160 float vf = vec[1] * c / s->iflat_range[1] * s->input_mirror_modifier[1];
2161 int visible, ui, vi;
2163 uf = zf >= 0.f ? (uf + 1.f) * width / 2.f : 0.f;
2164 vf = zf >= 0.f ? (vf + 1.f) * height / 2.f : 0.f;
2169 visible = vi >= 0 && vi < height && ui >= 0 && ui < width && zf >= 0.f;
2174 for (int i = 0; i < 4; i++) {
2175 for (int j = 0; j < 4; j++) {
2176 us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
2177 vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
2185 * Calculate frame position in mercator format for corresponding 3D coordinates on sphere.
2187 * @param s filter private context
2188 * @param vec coordinates on sphere
2189 * @param width frame width
2190 * @param height frame height
2191 * @param us horizontal coordinates for interpolation window
2192 * @param vs vertical coordinates for interpolation window
2193 * @param du horizontal relative coordinate
2194 * @param dv vertical relative coordinate
2196 static int xyz_to_mercator(const V360Context *s,
2197 const float *vec, int width, int height,
2198 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2200 const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0];
2201 const float theta = vec[1] * s->input_mirror_modifier[1];
2203 const float uf = (phi / M_PI + 1.f) * width / 2.f;
2204 const float vf = (av_clipf(logf((1.f + theta) / (1.f - theta)) / (2.f * M_PI), -1.f, 1.f) + 1.f) * height / 2.f;
2206 const int ui = floorf(uf);
2207 const int vi = floorf(vf);
2212 for (int i = 0; i < 4; i++) {
2213 for (int j = 0; j < 4; j++) {
2214 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2215 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2223 * Calculate 3D coordinates on sphere for corresponding frame position in mercator format.
2225 * @param s filter private context
2226 * @param i horizontal position on frame [0, width)
2227 * @param j vertical position on frame [0, height)
2228 * @param width frame width
2229 * @param height frame height
2230 * @param vec coordinates on sphere
2232 static int mercator_to_xyz(const V360Context *s,
2233 int i, int j, int width, int height,
2236 const float phi = ((2.f * i + 1.f) / width - 1.f) * M_PI + M_PI_2;
2237 const float y = ((2.f * j + 1.f) / height - 1.f) * M_PI;
2238 const float div = expf(2.f * y) + 1.f;
2240 const float sin_phi = sinf(phi);
2241 const float cos_phi = cosf(phi);
2242 const float sin_theta = 2.f * expf(y) / div;
2243 const float cos_theta = (expf(2.f * y) - 1.f) / div;
2245 vec[0] = -sin_theta * cos_phi;
2247 vec[2] = sin_theta * sin_phi;
2253 * Calculate frame position in ball format for corresponding 3D coordinates on sphere.
2255 * @param s filter private context
2256 * @param vec coordinates on sphere
2257 * @param width frame width
2258 * @param height frame height
2259 * @param us horizontal coordinates for interpolation window
2260 * @param vs vertical coordinates for interpolation window
2261 * @param du horizontal relative coordinate
2262 * @param dv vertical relative coordinate
2264 static int xyz_to_ball(const V360Context *s,
2265 const float *vec, int width, int height,
2266 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2268 const float l = hypotf(vec[0], vec[1]);
2269 const float r = sqrtf(1.f - vec[2]) / M_SQRT2;
2271 const float uf = (1.f + r * vec[0] * s->input_mirror_modifier[0] / (l > 0.f ? l : 1.f)) * width * 0.5f;
2272 const float vf = (1.f + r * vec[1] * s->input_mirror_modifier[1] / (l > 0.f ? l : 1.f)) * height * 0.5f;
2274 const int ui = floorf(uf);
2275 const int vi = floorf(vf);
2280 for (int i = 0; i < 4; i++) {
2281 for (int j = 0; j < 4; j++) {
2282 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2283 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2291 * Calculate 3D coordinates on sphere for corresponding frame position in ball format.
2293 * @param s filter private context
2294 * @param i horizontal position on frame [0, width)
2295 * @param j vertical position on frame [0, height)
2296 * @param width frame width
2297 * @param height frame height
2298 * @param vec coordinates on sphere
2300 static int ball_to_xyz(const V360Context *s,
2301 int i, int j, int width, int height,
2304 const float x = (2.f * i + 1.f) / width - 1.f;
2305 const float y = (2.f * j + 1.f) / height - 1.f;
2306 const float l = hypotf(x, y);
2309 const float z = 2.f * l * sqrtf(1.f - l * l);
2311 vec[0] = z * x / (l > 0.f ? l : 1.f);
2312 vec[1] = z * y / (l > 0.f ? l : 1.f);
2313 vec[2] = 1.f - 2.f * l * l;
2325 * Calculate 3D coordinates on sphere for corresponding frame position in hammer format.
2327 * @param s filter private context
2328 * @param i horizontal position on frame [0, width)
2329 * @param j vertical position on frame [0, height)
2330 * @param width frame width
2331 * @param height frame height
2332 * @param vec coordinates on sphere
2334 static int hammer_to_xyz(const V360Context *s,
2335 int i, int j, int width, int height,
2338 const float x = ((2.f * i + 1.f) / width - 1.f);
2339 const float y = ((2.f * j + 1.f) / height - 1.f);
2341 const float xx = x * x;
2342 const float yy = y * y;
2344 const float z = sqrtf(1.f - xx * 0.5f - yy * 0.5f);
2346 const float a = M_SQRT2 * x * z;
2347 const float b = 2.f * z * z - 1.f;
2349 const float aa = a * a;
2350 const float bb = b * b;
2352 const float w = sqrtf(1.f - 2.f * yy * z * z);
2354 vec[0] = w * 2.f * a * b / (aa + bb);
2355 vec[1] = M_SQRT2 * y * z;
2356 vec[2] = w * (bb - aa) / (aa + bb);
2358 normalize_vector(vec);
2364 * Calculate frame position in hammer format for corresponding 3D coordinates on sphere.
2366 * @param s filter private context
2367 * @param vec coordinates on sphere
2368 * @param width frame width
2369 * @param height frame height
2370 * @param us horizontal coordinates for interpolation window
2371 * @param vs vertical coordinates for interpolation window
2372 * @param du horizontal relative coordinate
2373 * @param dv vertical relative coordinate
2375 static int xyz_to_hammer(const V360Context *s,
2376 const float *vec, int width, int height,
2377 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2379 const float theta = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0];
2381 const float z = sqrtf(1.f + sqrtf(1.f - vec[1] * vec[1]) * cosf(theta * 0.5f));
2382 const float x = sqrtf(1.f - vec[1] * vec[1]) * sinf(theta * 0.5f) / z;
2383 const float y = vec[1] / z * s->input_mirror_modifier[1];
2385 const float uf = (x + 1.f) * width / 2.f;
2386 const float vf = (y + 1.f) * height / 2.f;
2388 const int ui = floorf(uf);
2389 const int vi = floorf(vf);
2394 for (int i = 0; i < 4; i++) {
2395 for (int j = 0; j < 4; j++) {
2396 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2397 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2405 * Calculate 3D coordinates on sphere for corresponding frame position in sinusoidal format.
2407 * @param s filter private context
2408 * @param i horizontal position on frame [0, width)
2409 * @param j vertical position on frame [0, height)
2410 * @param width frame width
2411 * @param height frame height
2412 * @param vec coordinates on sphere
2414 static int sinusoidal_to_xyz(const V360Context *s,
2415 int i, int j, int width, int height,
2418 const float theta = ((2.f * j + 1.f) / height - 1.f) * M_PI_2;
2419 const float phi = ((2.f * i + 1.f) / width - 1.f) * M_PI / cosf(theta);
2421 const float sin_phi = sinf(phi);
2422 const float cos_phi = cosf(phi);
2423 const float sin_theta = sinf(theta);
2424 const float cos_theta = cosf(theta);
2426 vec[0] = cos_theta * sin_phi;
2428 vec[2] = cos_theta * cos_phi;
2430 normalize_vector(vec);
2436 * Calculate frame position in sinusoidal format for corresponding 3D coordinates on sphere.
2438 * @param s filter private context
2439 * @param vec coordinates on sphere
2440 * @param width frame width
2441 * @param height frame height
2442 * @param us horizontal coordinates for interpolation window
2443 * @param vs vertical coordinates for interpolation window
2444 * @param du horizontal relative coordinate
2445 * @param dv vertical relative coordinate
2447 static int xyz_to_sinusoidal(const V360Context *s,
2448 const float *vec, int width, int height,
2449 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2451 const float theta = asinf(vec[1]) * s->input_mirror_modifier[1];
2452 const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0] * cosf(theta);
2454 const float uf = (phi / M_PI + 1.f) * width / 2.f;
2455 const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
2457 const int ui = floorf(uf);
2458 const int vi = floorf(vf);
2463 for (int i = 0; i < 4; i++) {
2464 for (int j = 0; j < 4; j++) {
2465 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2466 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2474 * Prepare data for processing equi-angular cubemap input format.
2476 * @param ctx filter context
2478 * @return error code
2480 static int prepare_eac_in(AVFilterContext *ctx)
2482 V360Context *s = ctx->priv;
2484 if (s->ih_flip && s->iv_flip) {
2485 s->in_cubemap_face_order[RIGHT] = BOTTOM_LEFT;
2486 s->in_cubemap_face_order[LEFT] = BOTTOM_RIGHT;
2487 s->in_cubemap_face_order[UP] = TOP_LEFT;
2488 s->in_cubemap_face_order[DOWN] = TOP_RIGHT;
2489 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
2490 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
2491 } else if (s->ih_flip) {
2492 s->in_cubemap_face_order[RIGHT] = TOP_LEFT;
2493 s->in_cubemap_face_order[LEFT] = TOP_RIGHT;
2494 s->in_cubemap_face_order[UP] = BOTTOM_LEFT;
2495 s->in_cubemap_face_order[DOWN] = BOTTOM_RIGHT;
2496 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
2497 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
2498 } else if (s->iv_flip) {
2499 s->in_cubemap_face_order[RIGHT] = BOTTOM_RIGHT;
2500 s->in_cubemap_face_order[LEFT] = BOTTOM_LEFT;
2501 s->in_cubemap_face_order[UP] = TOP_RIGHT;
2502 s->in_cubemap_face_order[DOWN] = TOP_LEFT;
2503 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
2504 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
2506 s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
2507 s->in_cubemap_face_order[LEFT] = TOP_LEFT;
2508 s->in_cubemap_face_order[UP] = BOTTOM_RIGHT;
2509 s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT;
2510 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
2511 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
2515 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_270;
2516 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_90;
2517 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_270;
2518 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_0;
2519 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_0;
2520 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_0;
2522 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0;
2523 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
2524 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
2525 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
2526 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
2527 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
2534 * Prepare data for processing equi-angular cubemap output format.
2536 * @param ctx filter context
2538 * @return error code
2540 static int prepare_eac_out(AVFilterContext *ctx)
2542 V360Context *s = ctx->priv;
2544 s->out_cubemap_direction_order[TOP_LEFT] = LEFT;
2545 s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT;
2546 s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT;
2547 s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN;
2548 s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
2549 s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP;
2551 s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0;
2552 s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
2553 s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
2554 s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
2555 s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
2556 s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
2562 * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
2564 * @param s filter private context
2565 * @param i horizontal position on frame [0, width)
2566 * @param j vertical position on frame [0, height)
2567 * @param width frame width
2568 * @param height frame height
2569 * @param vec coordinates on sphere
2571 static int eac_to_xyz(const V360Context *s,
2572 int i, int j, int width, int height,
2575 const float pixel_pad = 2;
2576 const float u_pad = pixel_pad / width;
2577 const float v_pad = pixel_pad / height;
2579 int u_face, v_face, face;
2581 float l_x, l_y, l_z;
2583 float uf = (i + 0.5f) / width;
2584 float vf = (j + 0.5f) / height;
2586 // EAC has 2-pixel padding on faces except between faces on the same row
2587 // Padding pixels seems not to be stretched with tangent as regular pixels
2588 // Formulas below approximate original padding as close as I could get experimentally
2590 // Horizontal padding
2591 uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
2595 } else if (uf >= 3.f) {
2599 u_face = floorf(uf);
2600 uf = fmodf(uf, 1.f) - 0.5f;
2604 v_face = floorf(vf * 2.f);
2605 vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
2607 if (uf >= -0.5f && uf < 0.5f) {
2608 uf = tanf(M_PI_2 * uf);
2612 if (vf >= -0.5f && vf < 0.5f) {
2613 vf = tanf(M_PI_2 * vf);
2618 face = u_face + 3 * v_face;
2659 normalize_vector(vec);
2665 * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
2667 * @param s filter private context
2668 * @param vec coordinates on sphere
2669 * @param width frame width
2670 * @param height frame height
2671 * @param us horizontal coordinates for interpolation window
2672 * @param vs vertical coordinates for interpolation window
2673 * @param du horizontal relative coordinate
2674 * @param dv vertical relative coordinate
2676 static int xyz_to_eac(const V360Context *s,
2677 const float *vec, int width, int height,
2678 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2680 const float pixel_pad = 2;
2681 const float u_pad = pixel_pad / width;
2682 const float v_pad = pixel_pad / height;
2686 int direction, face;
2689 xyz_to_cube(s, vec, &uf, &vf, &direction);
2691 face = s->in_cubemap_face_order[direction];
2695 uf = M_2_PI * atanf(uf) + 0.5f;
2696 vf = M_2_PI * atanf(vf) + 0.5f;
2698 // These formulas are inversed from eac_to_xyz ones
2699 uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
2700 vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
2714 for (int i = 0; i < 4; i++) {
2715 for (int j = 0; j < 4; j++) {
2716 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2717 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2725 * Prepare data for processing flat output format.
2727 * @param ctx filter context
2729 * @return error code
2731 static int prepare_flat_out(AVFilterContext *ctx)
2733 V360Context *s = ctx->priv;
2735 s->flat_range[0] = tanf(0.5f * s->h_fov * M_PI / 180.f);
2736 s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2742 * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
2744 * @param s filter private context
2745 * @param i horizontal position on frame [0, width)
2746 * @param j vertical position on frame [0, height)
2747 * @param width frame width
2748 * @param height frame height
2749 * @param vec coordinates on sphere
2751 static int flat_to_xyz(const V360Context *s,
2752 int i, int j, int width, int height,
2755 const float l_x = s->flat_range[0] * ((2.f * i + 0.5f) / width - 1.f);
2756 const float l_y = s->flat_range[1] * ((2.f * j + 0.5f) / height - 1.f);
2762 normalize_vector(vec);
2768 * Prepare data for processing fisheye output format.
2770 * @param ctx filter context
2772 * @return error code
2774 static int prepare_fisheye_out(AVFilterContext *ctx)
2776 V360Context *s = ctx->priv;
2778 s->flat_range[0] = s->h_fov / 180.f;
2779 s->flat_range[1] = s->v_fov / 180.f;
2785 * Calculate 3D coordinates on sphere for corresponding frame position in fisheye format.
2787 * @param s filter private context
2788 * @param i horizontal position on frame [0, width)
2789 * @param j vertical position on frame [0, height)
2790 * @param width frame width
2791 * @param height frame height
2792 * @param vec coordinates on sphere
2794 static int fisheye_to_xyz(const V360Context *s,
2795 int i, int j, int width, int height,
2798 const float uf = s->flat_range[0] * ((2.f * i) / width - 1.f);
2799 const float vf = s->flat_range[1] * ((2.f * j + 1.f) / height - 1.f);
2801 const float phi = atan2f(vf, uf);
2802 const float theta = M_PI_2 * (1.f - hypotf(uf, vf));
2804 const float sin_phi = sinf(phi);
2805 const float cos_phi = cosf(phi);
2806 const float sin_theta = sinf(theta);
2807 const float cos_theta = cosf(theta);
2809 vec[0] = cos_theta * cos_phi;
2810 vec[1] = cos_theta * sin_phi;
2813 normalize_vector(vec);
2819 * Prepare data for processing fisheye input format.
2821 * @param ctx filter context
2823 * @return error code
2825 static int prepare_fisheye_in(AVFilterContext *ctx)
2827 V360Context *s = ctx->priv;
2829 s->iflat_range[0] = s->ih_fov / 180.f;
2830 s->iflat_range[1] = s->iv_fov / 180.f;
2836 * Calculate frame position in fisheye format for corresponding 3D coordinates on sphere.
2838 * @param s filter private context
2839 * @param vec coordinates on sphere
2840 * @param width frame width
2841 * @param height frame height
2842 * @param us horizontal coordinates for interpolation window
2843 * @param vs vertical coordinates for interpolation window
2844 * @param du horizontal relative coordinate
2845 * @param dv vertical relative coordinate
2847 static int xyz_to_fisheye(const V360Context *s,
2848 const float *vec, int width, int height,
2849 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2851 const float h = hypotf(vec[0], vec[1]);
2852 const float lh = h > 0.f ? h : 1.f;
2853 const float phi = atan2f(h, vec[2]) / M_PI;
2855 float uf = vec[0] / lh * phi * s->input_mirror_modifier[0] / s->iflat_range[0];
2856 float vf = vec[1] / lh * phi * s->input_mirror_modifier[1] / s->iflat_range[1];
2858 const int visible = hypotf(uf, vf) <= 0.5f;
2861 uf = (uf + 0.5f) * width;
2862 vf = (vf + 0.5f) * height;
2867 *du = visible ? uf - ui : 0.f;
2868 *dv = visible ? vf - vi : 0.f;
2870 for (int i = 0; i < 4; i++) {
2871 for (int j = 0; j < 4; j++) {
2872 us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
2873 vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
2881 * Calculate 3D coordinates on sphere for corresponding frame position in pannini format.
2883 * @param s filter private context
2884 * @param i horizontal position on frame [0, width)
2885 * @param j vertical position on frame [0, height)
2886 * @param width frame width
2887 * @param height frame height
2888 * @param vec coordinates on sphere
2890 static int pannini_to_xyz(const V360Context *s,
2891 int i, int j, int width, int height,
2894 const float uf = ((2.f * i + 1.f) / width - 1.f);
2895 const float vf = ((2.f * j + 1.f) / height - 1.f);
2897 const float d = s->h_fov;
2898 const float k = uf * uf / ((d + 1.f) * (d + 1.f));
2899 const float dscr = k * k * d * d - (k + 1.f) * (k * d * d - 1.f);
2900 const float clon = (-k * d + sqrtf(dscr)) / (k + 1.f);
2901 const float S = (d + 1.f) / (d + clon);
2902 const float lon = atan2f(uf, S * clon);
2903 const float lat = atan2f(vf, S);
2905 vec[0] = sinf(lon) * cosf(lat);
2907 vec[2] = cosf(lon) * cosf(lat);
2909 normalize_vector(vec);
2915 * Calculate frame position in pannini format for corresponding 3D coordinates on sphere.
2917 * @param s filter private context
2918 * @param vec coordinates on sphere
2919 * @param width frame width
2920 * @param height frame height
2921 * @param us horizontal coordinates for interpolation window
2922 * @param vs vertical coordinates for interpolation window
2923 * @param du horizontal relative coordinate
2924 * @param dv vertical relative coordinate
2926 static int xyz_to_pannini(const V360Context *s,
2927 const float *vec, int width, int height,
2928 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2930 const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0];
2931 const float theta = asinf(vec[1]) * s->input_mirror_modifier[1];
2933 const float d = s->ih_fov;
2934 const float S = (d + 1.f) / (d + cosf(phi));
2936 const float x = S * sinf(phi);
2937 const float y = S * tanf(theta);
2939 const float uf = (x + 1.f) * width / 2.f;
2940 const float vf = (y + 1.f) * height / 2.f;
2942 const int ui = floorf(uf);
2943 const int vi = floorf(vf);
2945 const int visible = vi >= 0 && vi < height && ui >= 0 && ui < width && vec[2] >= 0.f;
2950 for (int i = 0; i < 4; i++) {
2951 for (int j = 0; j < 4; j++) {
2952 us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
2953 vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
2961 * Prepare data for processing cylindrical output format.
2963 * @param ctx filter context
2965 * @return error code
2967 static int prepare_cylindrical_out(AVFilterContext *ctx)
2969 V360Context *s = ctx->priv;
2971 s->flat_range[0] = M_PI * s->h_fov / 360.f;
2972 s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2978 * Calculate 3D coordinates on sphere for corresponding frame position in cylindrical format.
2980 * @param s filter private context
2981 * @param i horizontal position on frame [0, width)
2982 * @param j vertical position on frame [0, height)
2983 * @param width frame width
2984 * @param height frame height
2985 * @param vec coordinates on sphere
2987 static int cylindrical_to_xyz(const V360Context *s,
2988 int i, int j, int width, int height,
2991 const float uf = s->flat_range[0] * ((2.f * i + 1.f) / width - 1.f);
2992 const float vf = s->flat_range[1] * ((2.f * j + 1.f) / height - 1.f);
2994 const float phi = uf;
2995 const float theta = atanf(vf);
2997 const float sin_phi = sinf(phi);
2998 const float cos_phi = cosf(phi);
2999 const float sin_theta = sinf(theta);
3000 const float cos_theta = cosf(theta);
3002 vec[0] = cos_theta * sin_phi;
3004 vec[2] = cos_theta * cos_phi;
3006 normalize_vector(vec);
3012 * Prepare data for processing cylindrical input format.
3014 * @param ctx filter context
3016 * @return error code
3018 static int prepare_cylindrical_in(AVFilterContext *ctx)
3020 V360Context *s = ctx->priv;
3022 s->iflat_range[0] = M_PI * s->ih_fov / 360.f;
3023 s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
3029 * Calculate frame position in cylindrical format for corresponding 3D coordinates on sphere.
3031 * @param s filter private context
3032 * @param vec coordinates on sphere
3033 * @param width frame width
3034 * @param height frame height
3035 * @param us horizontal coordinates for interpolation window
3036 * @param vs vertical coordinates for interpolation window
3037 * @param du horizontal relative coordinate
3038 * @param dv vertical relative coordinate
3040 static int xyz_to_cylindrical(const V360Context *s,
3041 const float *vec, int width, int height,
3042 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3044 const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0] / s->iflat_range[0];
3045 const float theta = asinf(vec[1]) * s->input_mirror_modifier[1];
3047 const float uf = (phi + 1.f) * (width - 1) / 2.f;
3048 const float vf = (tanf(theta) / s->iflat_range[1] + 1.f) * height / 2.f;
3050 const int ui = floorf(uf);
3051 const int vi = floorf(vf);
3053 const int visible = vi >= 0 && vi < height && ui >= 0 && ui < width &&
3054 theta <= M_PI * s->iv_fov / 180.f &&
3055 theta >= -M_PI * s->iv_fov / 180.f;
3060 for (int i = 0; i < 4; i++) {
3061 for (int j = 0; j < 4; j++) {
3062 us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
3063 vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
3071 * Calculate 3D coordinates on sphere for corresponding frame position in perspective format.
3073 * @param s filter private context
3074 * @param i horizontal position on frame [0, width)
3075 * @param j vertical position on frame [0, height)
3076 * @param width frame width
3077 * @param height frame height
3078 * @param vec coordinates on sphere
3080 static int perspective_to_xyz(const V360Context *s,
3081 int i, int j, int width, int height,
3084 const float uf = ((2.f * i + 1.f) / width - 1.f);
3085 const float vf = ((2.f * j + 1.f) / height - 1.f);
3086 const float rh = hypotf(uf, vf);
3087 const float sinzz = 1.f - rh * rh;
3088 const float h = 1.f + s->v_fov;
3089 const float sinz = (h - sqrtf(sinzz)) / (h / rh + rh / h);
3090 const float sinz2 = sinz * sinz;
3093 const float cosz = sqrtf(1.f - sinz2);
3095 const float theta = asinf(cosz);
3096 const float phi = atan2f(uf, vf);
3098 const float sin_phi = sinf(phi);
3099 const float cos_phi = cosf(phi);
3100 const float sin_theta = sinf(theta);
3101 const float cos_theta = cosf(theta);
3103 vec[0] = cos_theta * sin_phi;
3105 vec[2] = cos_theta * cos_phi;
3113 normalize_vector(vec);
3118 * Calculate 3D coordinates on sphere for corresponding frame position in tetrahedron format.
3120 * @param s filter private context
3121 * @param i horizontal position on frame [0, width)
3122 * @param j vertical position on frame [0, height)
3123 * @param width frame width
3124 * @param height frame height
3125 * @param vec coordinates on sphere
3127 static int tetrahedron_to_xyz(const V360Context *s,
3128 int i, int j, int width, int height,
3131 const float uf = (float)i / width;
3132 const float vf = (float)j / height;
3134 vec[0] = uf < 0.5f ? uf * 4.f - 1.f : 3.f - uf * 4.f;
3135 vec[1] = 1.f - vf * 2.f;
3136 vec[2] = 2.f * fabsf(1.f - fabsf(1.f - uf * 2.f + vf)) - 1.f;
3138 normalize_vector(vec);
3144 * Calculate frame position in tetrahedron format for corresponding 3D coordinates on sphere.
3146 * @param s filter private context
3147 * @param vec coordinates on sphere
3148 * @param width frame width
3149 * @param height frame height
3150 * @param us horizontal coordinates for interpolation window
3151 * @param vs vertical coordinates for interpolation window
3152 * @param du horizontal relative coordinate
3153 * @param dv vertical relative coordinate
3155 static int xyz_to_tetrahedron(const V360Context *s,
3156 const float *vec, int width, int height,
3157 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3159 const float d0 = vec[0] * 1.f + vec[1] * 1.f + vec[2] *-1.f;
3160 const float d1 = vec[0] *-1.f + vec[1] *-1.f + vec[2] *-1.f;
3161 const float d2 = vec[0] * 1.f + vec[1] *-1.f + vec[2] * 1.f;
3162 const float d3 = vec[0] *-1.f + vec[1] * 1.f + vec[2] * 1.f;
3163 const float d = FFMAX(d0, FFMAX3(d1, d2, d3));
3165 float uf, vf, x, y, z;
3172 vf = 0.5f - y * 0.5f * s->input_mirror_modifier[1];
3174 if ((x + y >= 0.f && y + z >= 0.f && -z - x <= 0.f) ||
3175 (x + y <= 0.f && -y + z >= 0.f && z - x >= 0.f)) {
3176 uf = 0.25f * x * s->input_mirror_modifier[0] + 0.25f;
3178 uf = 0.75f - 0.25f * x * s->input_mirror_modifier[0];
3190 for (int i = 0; i < 4; i++) {
3191 for (int j = 0; j < 4; j++) {
3192 us[i][j] = reflectx(ui + j - 1, vi + i - 1, width, height);
3193 vs[i][j] = reflecty(vi + i - 1, height);
3201 * Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format.
3203 * @param s filter private context
3204 * @param i horizontal position on frame [0, width)
3205 * @param j vertical position on frame [0, height)
3206 * @param width frame width
3207 * @param height frame height
3208 * @param vec coordinates on sphere
3210 static int dfisheye_to_xyz(const V360Context *s,
3211 int i, int j, int width, int height,
3214 const float ew = width / 2.f;
3215 const float eh = height;
3217 const int ei = i >= ew ? i - ew : i;
3218 const float m = i >= ew ? 1.f : -1.f;
3220 const float uf = s->flat_range[0] * ((2.f * ei) / ew - 1.f);
3221 const float vf = s->flat_range[1] * ((2.f * j + 1.f) / eh - 1.f);
3223 const float h = hypotf(uf, vf);
3224 const float lh = h > 0.f ? h : 1.f;
3225 const float theta = m * M_PI_2 * (1.f - h);
3227 const float sin_theta = sinf(theta);
3228 const float cos_theta = cosf(theta);
3230 vec[0] = cos_theta * m * uf / lh;
3231 vec[1] = cos_theta * vf / lh;
3234 normalize_vector(vec);
3240 * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
3242 * @param s filter private context
3243 * @param vec coordinates on sphere
3244 * @param width frame width
3245 * @param height frame height
3246 * @param us horizontal coordinates for interpolation window
3247 * @param vs vertical coordinates for interpolation window
3248 * @param du horizontal relative coordinate
3249 * @param dv vertical relative coordinate
3251 static int xyz_to_dfisheye(const V360Context *s,
3252 const float *vec, int width, int height,
3253 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3255 const float ew = width / 2.f;
3256 const float eh = height;
3258 const float h = hypotf(vec[0], vec[1]);
3259 const float lh = h > 0.f ? h : 1.f;
3260 const float theta = acosf(fabsf(vec[2])) / M_PI;
3262 float uf = (theta * (vec[0] / lh) * s->input_mirror_modifier[0] / s->iflat_range[0] + 0.5f) * ew;
3263 float vf = (theta * (vec[1] / lh) * s->input_mirror_modifier[1] / s->iflat_range[1] + 0.5f) * eh;
3268 if (vec[2] >= 0.f) {
3269 u_shift = ceilf(ew);
3281 for (int i = 0; i < 4; i++) {
3282 for (int j = 0; j < 4; j++) {
3283 us[i][j] = av_clip(u_shift + ui + j - 1, 0, width - 1);
3284 vs[i][j] = av_clip( vi + i - 1, 0, height - 1);
3292 * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
3294 * @param s filter private context
3295 * @param i horizontal position on frame [0, width)
3296 * @param j vertical position on frame [0, height)
3297 * @param width frame width
3298 * @param height frame height
3299 * @param vec coordinates on sphere
3301 static int barrel_to_xyz(const V360Context *s,
3302 int i, int j, int width, int height,
3305 const float scale = 0.99f;
3306 float l_x, l_y, l_z;
3308 if (i < 4 * width / 5) {
3309 const float theta_range = M_PI_4;
3311 const int ew = 4 * width / 5;
3312 const int eh = height;
3314 const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
3315 const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
3317 const float sin_phi = sinf(phi);
3318 const float cos_phi = cosf(phi);
3319 const float sin_theta = sinf(theta);
3320 const float cos_theta = cosf(theta);
3322 l_x = cos_theta * sin_phi;
3324 l_z = cos_theta * cos_phi;
3326 const int ew = width / 5;
3327 const int eh = height / 2;
3332 uf = 2.f * (i - 4 * ew) / ew - 1.f;
3333 vf = 2.f * (j ) / eh - 1.f;
3342 uf = 2.f * (i - 4 * ew) / ew - 1.f;
3343 vf = 2.f * (j - eh) / eh - 1.f;
3358 normalize_vector(vec);
3364 * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
3366 * @param s filter private context
3367 * @param vec coordinates on sphere
3368 * @param width frame width
3369 * @param height frame height
3370 * @param us horizontal coordinates for interpolation window
3371 * @param vs vertical coordinates for interpolation window
3372 * @param du horizontal relative coordinate
3373 * @param dv vertical relative coordinate
3375 static int xyz_to_barrel(const V360Context *s,
3376 const float *vec, int width, int height,
3377 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3379 const float scale = 0.99f;
3381 const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0];
3382 const float theta = asinf(vec[1]) * s->input_mirror_modifier[1];
3383 const float theta_range = M_PI_4;
3386 int u_shift, v_shift;
3390 if (theta > -theta_range && theta < theta_range) {
3394 u_shift = s->ih_flip ? width / 5 : 0;
3397 uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
3398 vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
3403 u_shift = s->ih_flip ? 0 : 4 * ew;
3405 if (theta < 0.f) { // UP
3406 uf = -vec[0] / vec[1];
3407 vf = -vec[2] / vec[1];
3410 uf = vec[0] / vec[1];
3411 vf = -vec[2] / vec[1];
3415 uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
3416 vf *= s->input_mirror_modifier[1];
3418 uf = 0.5f * ew * (uf * scale + 1.f);
3419 vf = 0.5f * eh * (vf * scale + 1.f);
3428 for (int i = 0; i < 4; i++) {
3429 for (int j = 0; j < 4; j++) {
3430 us[i][j] = u_shift + av_clip(ui + j - 1, 0, ew - 1);
3431 vs[i][j] = v_shift + av_clip(vi + i - 1, 0, eh - 1);
3439 * Calculate frame position in barrel split facebook's format for corresponding 3D coordinates on sphere.
3441 * @param s filter private context
3442 * @param vec coordinates on sphere
3443 * @param width frame width
3444 * @param height frame height
3445 * @param us horizontal coordinates for interpolation window
3446 * @param vs vertical coordinates for interpolation window
3447 * @param du horizontal relative coordinate
3448 * @param dv vertical relative coordinate
3450 static int xyz_to_barrelsplit(const V360Context *s,
3451 const float *vec, int width, int height,
3452 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3454 const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0];
3455 const float theta = asinf(vec[1]) * s->input_mirror_modifier[1];
3457 const float theta_range = M_PI_4;
3460 int u_shift, v_shift;
3464 if (theta >= -theta_range && theta <= theta_range) {
3465 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width * 2.f / 3.f) : 1.f - s->in_pad;
3466 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 2.f) : 1.f - s->in_pad;
3471 u_shift = s->ih_flip ? width / 3 : 0;
3472 v_shift = phi >= M_PI_2 || phi < -M_PI_2 ? eh : 0;
3474 uf = fmodf(phi, M_PI_2) / M_PI_2;
3475 vf = theta / M_PI_4;
3478 uf = uf >= 0.f ? fmodf(uf - 1.f, 1.f) : fmodf(uf + 1.f, 1.f);
3480 uf = (uf * scalew + 1.f) * width / 3.f;
3481 vf = (vf * scaleh + 1.f) * height / 4.f;
3483 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width / 3.f) : 1.f - s->in_pad;
3484 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 4.f) : 1.f - s->in_pad;
3490 u_shift = s->ih_flip ? 0 : 2 * ew;
3492 if (theta <= 0.f && theta >= -M_PI_2 &&
3493 phi <= M_PI_2 && phi >= -M_PI_2) {
3494 uf = -vec[0] / vec[1];
3495 vf = -vec[2] / vec[1];
3498 } else if (theta >= 0.f && theta <= M_PI_2 &&
3499 phi <= M_PI_2 && phi >= -M_PI_2) {
3500 uf = vec[0] / vec[1];
3501 vf = -vec[2] / vec[1];
3502 v_shift = height * 0.25f;
3503 } else if (theta <= 0.f && theta >= -M_PI_2) {
3504 uf = vec[0] / vec[1];
3505 vf = vec[2] / vec[1];
3506 v_shift = height * 0.5f;
3509 uf = -vec[0] / vec[1];
3510 vf = vec[2] / vec[1];
3511 v_shift = height * 0.75f;
3514 uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
3515 vf *= s->input_mirror_modifier[1];
3517 uf = 0.5f * width / 3.f * (uf * scalew + 1.f);
3518 vf = height * 0.25f * (vf * scaleh + 1.f) + v_offset;
3527 for (int i = 0; i < 4; i++) {
3528 for (int j = 0; j < 4; j++) {
3529 us[i][j] = u_shift + av_clip(ui + j - 1, 0, ew - 1);
3530 vs[i][j] = v_shift + av_clip(vi + i - 1, 0, eh - 1);
3538 * Calculate 3D coordinates on sphere for corresponding frame position in barrel split facebook's format.
3540 * @param s filter private context
3541 * @param i horizontal position on frame [0, width)
3542 * @param j vertical position on frame [0, height)
3543 * @param width frame width
3544 * @param height frame height
3545 * @param vec coordinates on sphere
3547 static int barrelsplit_to_xyz(const V360Context *s,
3548 int i, int j, int width, int height,
3551 const float x = (i + 0.5f) / width;
3552 const float y = (j + 0.5f) / height;
3553 float l_x, l_y, l_z;
3555 if (x < 2.f / 3.f) {
3556 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width * 2.f / 3.f) : 1.f - s->out_pad;
3557 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 2.f) : 1.f - s->out_pad;
3559 const float back = floorf(y * 2.f);
3561 const float phi = ((3.f / 2.f * x - 0.5f) / scalew - back) * M_PI;
3562 const float theta = (y - 0.25f - 0.5f * back) / scaleh * M_PI;
3564 const float sin_phi = sinf(phi);
3565 const float cos_phi = cosf(phi);
3566 const float sin_theta = sinf(theta);
3567 const float cos_theta = cosf(theta);
3569 l_x = cos_theta * sin_phi;
3571 l_z = cos_theta * cos_phi;
3573 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width / 3.f) : 1.f - s->out_pad;
3574 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 4.f) : 1.f - s->out_pad;
3576 const int face = floorf(y * 4.f);
3587 l_x = (0.5f - uf) / scalew;
3589 l_z = (0.5f - vf) / scaleh;
3594 vf = 1.f - (vf - 0.5f);
3596 l_x = (0.5f - uf) / scalew;
3598 l_z = (-0.5f + vf) / scaleh;
3601 vf = y * 2.f - 0.5f;
3602 vf = 1.f - (1.f - vf);
3604 l_x = (0.5f - uf) / scalew;
3606 l_z = (0.5f - vf) / scaleh;
3609 vf = y * 2.f - 1.5f;
3611 l_x = (0.5f - uf) / scalew;
3613 l_z = (-0.5f + vf) / scaleh;
3622 normalize_vector(vec);
3628 * Calculate 3D coordinates on sphere for corresponding frame position in tspyramid format.
3630 * @param s filter private context
3631 * @param i horizontal position on frame [0, width)
3632 * @param j vertical position on frame [0, height)
3633 * @param width frame width
3634 * @param height frame height
3635 * @param vec coordinates on sphere
3637 static int tspyramid_to_xyz(const V360Context *s,
3638 int i, int j, int width, int height,
3641 const float x = (i + 0.5f) / width;
3642 const float y = (j + 0.5f) / height;
3645 vec[0] = x * 4.f - 1.f;
3646 vec[1] = (y * 2.f - 1.f);
3648 } else if (x >= 0.6875f && x < 0.8125f &&
3649 y >= 0.375f && y < 0.625f) {
3650 vec[0] = -(x - 0.6875f) * 16.f + 1.f;
3651 vec[1] = (y - 0.375f) * 8.f - 1.f;
3653 } else if (0.5f <= x && x < 0.6875f &&
3654 ((0.f <= y && y < 0.375f && y >= 2.f * (x - 0.5f)) ||
3655 (0.375f <= y && y < 0.625f) ||
3656 (0.625f <= y && y < 1.f && y <= 2.f * (1.f - x)))) {
3658 vec[1] = 2.f * (y - 2.f * x + 1.f) / (3.f - 4.f * x) - 1.f;
3659 vec[2] = -2.f * (x - 0.5f) / 0.1875f + 1.f;
3660 } else if (0.8125f <= x && x < 1.f &&
3661 ((0.f <= y && y < 0.375f && x >= (1.f - y / 2.f)) ||
3662 (0.375f <= y && y < 0.625f) ||
3663 (0.625f <= y && y < 1.f && y <= (2.f * x - 1.f)))) {
3665 vec[1] = 2.f * (y + 2.f * x - 2.f) / (4.f * x - 3.f) - 1.f;
3666 vec[2] = 2.f * (x - 0.8125f) / 0.1875f - 1.f;
3667 } else if (0.f <= y && y < 0.375f &&
3668 ((0.5f <= x && x < 0.8125f && y < 2.f * (x - 0.5f)) ||
3669 (0.6875f <= x && x < 0.8125f) ||
3670 (0.8125f <= x && x < 1.f && x < (1.f - y / 2.f)))) {
3671 vec[0] = 2.f * (1.f - x - 0.5f * y) / (0.5f - y) - 1.f;
3673 vec[2] = 2.f * (0.375f - y) / 0.375f - 1.f;
3675 vec[0] = 2.f * (0.5f - x + 0.5f * y) / (y - 0.5f) - 1.f;
3677 vec[2] = -2.f * (1.f - y) / 0.375f + 1.f;
3680 normalize_vector(vec);
3686 * Calculate frame position in tspyramid format for corresponding 3D coordinates on sphere.
3688 * @param s filter private context
3689 * @param vec coordinates on sphere
3690 * @param width frame width
3691 * @param height frame height
3692 * @param us horizontal coordinates for interpolation window
3693 * @param vs vertical coordinates for interpolation window
3694 * @param du horizontal relative coordinate
3695 * @param dv vertical relative coordinate
3697 static int xyz_to_tspyramid(const V360Context *s,
3698 const float *vec, int width, int height,
3699 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3705 xyz_to_cube(s, vec, &uf, &vf, &face);
3707 uf = (uf + 1.f) * 0.5f;
3708 vf = (vf + 1.f) * 0.5f;
3712 uf = 0.1875f * vf - 0.375f * uf * vf - 0.125f * uf + 0.8125f;
3713 vf = 0.375f - 0.375f * vf;
3719 uf = 1.f - 0.1875f * vf - 0.5f * uf + 0.375f * uf * vf;
3720 vf = 1.f - 0.375f * vf;
3723 vf = 0.25f * vf + 0.75f * uf * vf - 0.375f * uf + 0.375f;
3724 uf = 0.1875f * uf + 0.8125f;
3727 vf = 0.375f * uf - 0.75f * uf * vf + vf;
3728 uf = 0.1875f * uf + 0.5f;
3731 uf = 0.125f * uf + 0.6875f;
3732 vf = 0.25f * vf + 0.375f;
3745 for (int i = 0; i < 4; i++) {
3746 for (int j = 0; j < 4; j++) {
3747 us[i][j] = reflectx(ui + j - 1, vi + i - 1, width, height);
3748 vs[i][j] = reflecty(vi + i - 1, height);
3756 * Calculate 3D coordinates on sphere for corresponding frame position in octahedron format.
3758 * @param s filter private context
3759 * @param i horizontal position on frame [0, width)
3760 * @param j vertical position on frame [0, height)
3761 * @param width frame width
3762 * @param height frame height
3763 * @param vec coordinates on sphere
3765 static int octahedron_to_xyz(const V360Context *s,
3766 int i, int j, int width, int height,
3769 float x = ((i + 0.5f) / width) * 2.f - 1.f;
3770 float y = ((j + 0.5f) / height) * 2.f - 1.f;
3771 float ax = fabsf(x);
3772 float ay = fabsf(y);
3774 vec[2] = 1.f - (ax + ay);
3775 if (ax + ay > 1.f) {
3776 vec[0] = (1.f - ay) * FFSIGN(x);
3777 vec[1] = (1.f - ax) * FFSIGN(y);
3783 normalize_vector(vec);
3789 * Calculate frame position in octahedron format for corresponding 3D coordinates on sphere.
3791 * @param s filter private context
3792 * @param vec coordinates on sphere
3793 * @param width frame width
3794 * @param height frame height
3795 * @param us horizontal coordinates for interpolation window
3796 * @param vs vertical coordinates for interpolation window
3797 * @param du horizontal relative coordinate
3798 * @param dv vertical relative coordinate
3800 static int xyz_to_octahedron(const V360Context *s,
3801 const float *vec, int width, int height,
3802 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3806 float div = fabsf(vec[0]) + fabsf(vec[1]) + fabsf(vec[2]);
3814 vf = (1.f - fabsf(uf)) * FFSIGN(zf);
3815 uf = (1.f - fabsf(zf)) * FFSIGN(uf);
3818 uf = uf * 0.5f + 0.5f;
3819 vf = vf * 0.5f + 0.5f;
3830 for (int i = 0; i < 4; i++) {
3831 for (int j = 0; j < 4; j++) {
3832 us[i][j] = av_clip(uf + j - 1, 0, width - 1);
3833 vs[i][j] = av_clip(vf + i - 1, 0, height - 1);
3840 static void multiply_matrix(float c[3][3], const float a[3][3], const float b[3][3])
3842 for (int i = 0; i < 3; i++) {
3843 for (int j = 0; j < 3; j++) {
3846 for (int k = 0; k < 3; k++)
3847 sum += a[i][k] * b[k][j];
3855 * Calculate rotation matrix for yaw/pitch/roll angles.
3857 static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
3858 float rot_mat[3][3],
3859 const int rotation_order[3])
3861 const float yaw_rad = yaw * M_PI / 180.f;
3862 const float pitch_rad = pitch * M_PI / 180.f;
3863 const float roll_rad = roll * M_PI / 180.f;
3865 const float sin_yaw = sinf(yaw_rad);
3866 const float cos_yaw = cosf(yaw_rad);
3867 const float sin_pitch = sinf(pitch_rad);
3868 const float cos_pitch = cosf(pitch_rad);
3869 const float sin_roll = sinf(roll_rad);
3870 const float cos_roll = cosf(roll_rad);
3875 m[0][0][0] = cos_yaw; m[0][0][1] = 0; m[0][0][2] = sin_yaw;
3876 m[0][1][0] = 0; m[0][1][1] = 1; m[0][1][2] = 0;
3877 m[0][2][0] = -sin_yaw; m[0][2][1] = 0; m[0][2][2] = cos_yaw;
3879 m[1][0][0] = 1; m[1][0][1] = 0; m[1][0][2] = 0;
3880 m[1][1][0] = 0; m[1][1][1] = cos_pitch; m[1][1][2] = -sin_pitch;
3881 m[1][2][0] = 0; m[1][2][1] = sin_pitch; m[1][2][2] = cos_pitch;
3883 m[2][0][0] = cos_roll; m[2][0][1] = -sin_roll; m[2][0][2] = 0;
3884 m[2][1][0] = sin_roll; m[2][1][1] = cos_roll; m[2][1][2] = 0;
3885 m[2][2][0] = 0; m[2][2][1] = 0; m[2][2][2] = 1;
3887 multiply_matrix(temp, m[rotation_order[0]], m[rotation_order[1]]);
3888 multiply_matrix(rot_mat, temp, m[rotation_order[2]]);
3892 * Rotate vector with given rotation matrix.
3894 * @param rot_mat rotation matrix
3897 static inline void rotate(const float rot_mat[3][3],
3900 const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2];
3901 const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2];
3902 const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2];
3909 static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
3912 modifier[0] = h_flip ? -1.f : 1.f;
3913 modifier[1] = v_flip ? -1.f : 1.f;
3914 modifier[2] = d_flip ? -1.f : 1.f;
3917 static inline void mirror(const float *modifier, float *vec)
3919 vec[0] *= modifier[0];
3920 vec[1] *= modifier[1];
3921 vec[2] *= modifier[2];
3924 static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int sizeof_mask, int p)
3926 const int pr_height = s->pr_height[p];
3928 for (int n = 0; n < s->nb_threads; n++) {
3929 SliceXYRemap *r = &s->slice_remap[n];
3930 const int slice_start = (pr_height * n ) / s->nb_threads;
3931 const int slice_end = (pr_height * (n + 1)) / s->nb_threads;
3932 const int height = slice_end - slice_start;
3935 r->u[p] = av_calloc(s->uv_linesize[p] * height, sizeof_uv);
3937 r->v[p] = av_calloc(s->uv_linesize[p] * height, sizeof_uv);
3938 if (!r->u[p] || !r->v[p])
3939 return AVERROR(ENOMEM);
3942 r->ker[p] = av_calloc(s->uv_linesize[p] * height, sizeof_ker);
3944 return AVERROR(ENOMEM);
3947 if (sizeof_mask && !p) {
3949 r->mask = av_calloc(s->pr_width[p] * height, sizeof_mask);
3951 return AVERROR(ENOMEM);
3958 static void fov_from_dfov(int format, float d_fov, float w, float h, float *h_fov, float *v_fov)
3963 const float d = 0.5f * hypotf(w, h);
3964 const float l = sinf(d_fov * M_PI / 360.f) / d;
3966 *h_fov = asinf(w * 0.5 * l) * 360.f / M_PI;
3967 *v_fov = asinf(h * 0.5 * l) * 360.f / M_PI;
3969 if (d_fov > 180.f) {
3970 *h_fov = 180.f - *h_fov;
3971 *v_fov = 180.f - *v_fov;
3977 const float d = 0.5f * hypotf(w, h);
3978 const float l = d / (sinf(d_fov * M_PI / 720.f));
3980 *h_fov = 2.f * asinf(w * 0.5f / l) * 360.f / M_PI;
3981 *v_fov = 2.f * asinf(h * 0.5f / l) * 360.f / M_PI;
3986 const float d = 0.5f * hypotf(w, h);
3987 const float l = d / (tanf(d_fov * M_PI / 720.f));
3989 *h_fov = 2.f * atan2f(w * 0.5f, l) * 360.f / M_PI;
3990 *v_fov = 2.f * atan2f(h * 0.5f, l) * 360.f / M_PI;
3995 const float d = 0.5f * hypotf(w * 0.5f, h);
3997 *h_fov = d / w * 2.f * d_fov;
3998 *v_fov = d / h * d_fov;
4003 const float d = 0.5f * hypotf(w, h);
4005 *h_fov = d / w * d_fov;
4006 *v_fov = d / h * d_fov;
4012 const float da = tanf(0.5f * FFMIN(d_fov, 359.f) * M_PI / 180.f);
4013 const float d = hypotf(w, h);
4015 *h_fov = atan2f(da * w, d) * 360.f / M_PI;
4016 *v_fov = atan2f(da * h, d) * 360.f / M_PI;
4027 static void set_dimensions(int *outw, int *outh, int w, int h, const AVPixFmtDescriptor *desc)
4029 outw[1] = outw[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
4030 outw[0] = outw[3] = w;
4031 outh[1] = outh[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
4032 outh[0] = outh[3] = h;
4035 // Calculate remap data
4036 static av_always_inline int v360_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
4038 V360Context *s = ctx->priv;
4039 SliceXYRemap *r = &s->slice_remap[jobnr];
4041 for (int p = 0; p < s->nb_allocated; p++) {
4042 const int max_value = s->max_value;
4043 const int width = s->pr_width[p];
4044 const int uv_linesize = s->uv_linesize[p];
4045 const int height = s->pr_height[p];
4046 const int in_width = s->inplanewidth[p];
4047 const int in_height = s->inplaneheight[p];
4048 const int slice_start = (height * jobnr ) / nb_jobs;
4049 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
4050 const int elements = s->elements;
4055 for (int j = slice_start; j < slice_end; j++) {
4056 for (int i = 0; i < width; i++) {
4057 int16_t *u = r->u[p] + ((j - slice_start) * uv_linesize + i) * elements;
4058 int16_t *v = r->v[p] + ((j - slice_start) * uv_linesize + i) * elements;
4059 int16_t *ker = r->ker[p] + ((j - slice_start) * uv_linesize + i) * elements;
4060 uint8_t *mask8 = p ? NULL : r->mask + ((j - slice_start) * s->pr_width[0] + i);
4061 uint16_t *mask16 = p ? NULL : (uint16_t *)r->mask + ((j - slice_start) * s->pr_width[0] + i);
4062 int in_mask, out_mask;
4064 if (s->out_transpose)
4065 out_mask = s->out_transform(s, j, i, height, width, vec);
4067 out_mask = s->out_transform(s, i, j, width, height, vec);
4068 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
4069 rotate(s->rot_mat, vec);
4070 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
4071 normalize_vector(vec);
4072 mirror(s->output_mirror_modifier, vec);
4073 if (s->in_transpose)
4074 in_mask = s->in_transform(s, vec, in_height, in_width, rmap.v, rmap.u, &du, &dv);
4076 in_mask = s->in_transform(s, vec, in_width, in_height, rmap.u, rmap.v, &du, &dv);
4077 av_assert1(!isnan(du) && !isnan(dv));
4078 s->calculate_kernel(du, dv, &rmap, u, v, ker);
4080 if (!p && r->mask) {
4081 if (s->mask_size == 1) {
4082 mask8[0] = 255 * (out_mask & in_mask);
4084 mask16[0] = max_value * (out_mask & in_mask);
4094 static int config_output(AVFilterLink *outlink)
4096 AVFilterContext *ctx = outlink->src;
4097 AVFilterLink *inlink = ctx->inputs[0];
4098 V360Context *s = ctx->priv;
4099 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
4100 const int depth = desc->comp[0].depth;
4101 const int sizeof_mask = s->mask_size = (depth + 7) >> 3;
4106 int in_offset_h, in_offset_w;
4107 int out_offset_h, out_offset_w;
4109 int (*prepare_out)(AVFilterContext *ctx);
4112 s->max_value = (1 << depth) - 1;
4113 s->input_mirror_modifier[0] = s->ih_flip ? -1.f : 1.f;
4114 s->input_mirror_modifier[1] = s->iv_flip ? -1.f : 1.f;
4116 switch (s->interp) {
4118 s->calculate_kernel = nearest_kernel;
4119 s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
4121 sizeof_uv = sizeof(int16_t) * s->elements;
4125 s->calculate_kernel = bilinear_kernel;
4126 s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
4127 s->elements = 2 * 2;
4128 sizeof_uv = sizeof(int16_t) * s->elements;
4129 sizeof_ker = sizeof(int16_t) * s->elements;
4132 s->calculate_kernel = lagrange_kernel;
4133 s->remap_slice = depth <= 8 ? remap3_8bit_slice : remap3_16bit_slice;
4134 s->elements = 3 * 3;
4135 sizeof_uv = sizeof(int16_t) * s->elements;
4136 sizeof_ker = sizeof(int16_t) * s->elements;
4139 s->calculate_kernel = bicubic_kernel;
4140 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
4141 s->elements = 4 * 4;
4142 sizeof_uv = sizeof(int16_t) * s->elements;
4143 sizeof_ker = sizeof(int16_t) * s->elements;
4146 s->calculate_kernel = lanczos_kernel;
4147 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
4148 s->elements = 4 * 4;
4149 sizeof_uv = sizeof(int16_t) * s->elements;
4150 sizeof_ker = sizeof(int16_t) * s->elements;
4153 s->calculate_kernel = spline16_kernel;
4154 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
4155 s->elements = 4 * 4;
4156 sizeof_uv = sizeof(int16_t) * s->elements;
4157 sizeof_ker = sizeof(int16_t) * s->elements;
4160 s->calculate_kernel = gaussian_kernel;
4161 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
4162 s->elements = 4 * 4;
4163 sizeof_uv = sizeof(int16_t) * s->elements;
4164 sizeof_ker = sizeof(int16_t) * s->elements;
4170 ff_v360_init(s, depth);
4172 for (int order = 0; order < NB_RORDERS; order++) {
4173 const char c = s->rorder[order];
4177 av_log(ctx, AV_LOG_WARNING,
4178 "Incomplete rorder option. Direction for all 3 rotation orders should be specified. Switching to default rorder.\n");
4179 s->rotation_order[0] = YAW;
4180 s->rotation_order[1] = PITCH;
4181 s->rotation_order[2] = ROLL;
4185 rorder = get_rorder(c);
4187 av_log(ctx, AV_LOG_WARNING,
4188 "Incorrect rotation order symbol '%c' in rorder option. Switching to default rorder.\n", c);
4189 s->rotation_order[0] = YAW;
4190 s->rotation_order[1] = PITCH;
4191 s->rotation_order[2] = ROLL;
4195 s->rotation_order[order] = rorder;
4198 switch (s->in_stereo) {
4202 in_offset_w = in_offset_h = 0;
4220 set_dimensions(s->inplanewidth, s->inplaneheight, w, h, desc);
4221 set_dimensions(s->in_offset_w, s->in_offset_h, in_offset_w, in_offset_h, desc);
4223 s->in_width = s->inplanewidth[0];
4224 s->in_height = s->inplaneheight[0];
4226 if (s->id_fov > 0.f)
4227 fov_from_dfov(s->in, s->id_fov, w, h, &s->ih_fov, &s->iv_fov);
4229 if (s->in_transpose)
4230 FFSWAP(int, s->in_width, s->in_height);
4233 case EQUIRECTANGULAR:
4234 s->in_transform = xyz_to_equirect;
4240 s->in_transform = xyz_to_cube3x2;
4241 err = prepare_cube_in(ctx);
4246 s->in_transform = xyz_to_cube1x6;
4247 err = prepare_cube_in(ctx);
4252 s->in_transform = xyz_to_cube6x1;
4253 err = prepare_cube_in(ctx);
4258 s->in_transform = xyz_to_eac;
4259 err = prepare_eac_in(ctx);
4264 s->in_transform = xyz_to_flat;
4265 err = prepare_flat_in(ctx);
4270 av_log(ctx, AV_LOG_ERROR, "Supplied format is not accepted as input.\n");
4271 return AVERROR(EINVAL);
4273 s->in_transform = xyz_to_dfisheye;
4274 err = prepare_fisheye_in(ctx);
4279 s->in_transform = xyz_to_barrel;
4285 s->in_transform = xyz_to_stereographic;
4286 err = prepare_stereographic_in(ctx);
4291 s->in_transform = xyz_to_mercator;
4297 s->in_transform = xyz_to_ball;
4303 s->in_transform = xyz_to_hammer;
4309 s->in_transform = xyz_to_sinusoidal;
4315 s->in_transform = xyz_to_fisheye;
4316 err = prepare_fisheye_in(ctx);
4321 s->in_transform = xyz_to_pannini;
4327 s->in_transform = xyz_to_cylindrical;
4328 err = prepare_cylindrical_in(ctx);
4333 s->in_transform = xyz_to_tetrahedron;
4339 s->in_transform = xyz_to_barrelsplit;
4345 s->in_transform = xyz_to_tspyramid;
4350 case HEQUIRECTANGULAR:
4351 s->in_transform = xyz_to_hequirect;
4357 s->in_transform = xyz_to_equisolid;
4358 err = prepare_equisolid_in(ctx);
4363 s->in_transform = xyz_to_orthographic;
4364 err = prepare_orthographic_in(ctx);
4369 s->in_transform = xyz_to_octahedron;
4375 av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
4384 case EQUIRECTANGULAR:
4385 s->out_transform = equirect_to_xyz;
4391 s->out_transform = cube3x2_to_xyz;
4392 prepare_out = prepare_cube_out;
4393 w = lrintf(wf / 4.f * 3.f);
4397 s->out_transform = cube1x6_to_xyz;
4398 prepare_out = prepare_cube_out;
4399 w = lrintf(wf / 4.f);
4400 h = lrintf(hf * 3.f);
4403 s->out_transform = cube6x1_to_xyz;
4404 prepare_out = prepare_cube_out;
4405 w = lrintf(wf / 2.f * 3.f);
4406 h = lrintf(hf / 2.f);
4409 s->out_transform = eac_to_xyz;
4410 prepare_out = prepare_eac_out;
4412 h = lrintf(hf / 8.f * 9.f);
4415 s->out_transform = flat_to_xyz;
4416 prepare_out = prepare_flat_out;
4421 s->out_transform = dfisheye_to_xyz;
4422 prepare_out = prepare_fisheye_out;
4427 s->out_transform = barrel_to_xyz;
4429 w = lrintf(wf / 4.f * 5.f);
4433 s->out_transform = stereographic_to_xyz;
4434 prepare_out = prepare_stereographic_out;
4436 h = lrintf(hf * 2.f);
4439 s->out_transform = mercator_to_xyz;
4442 h = lrintf(hf * 2.f);
4445 s->out_transform = ball_to_xyz;
4448 h = lrintf(hf * 2.f);
4451 s->out_transform = hammer_to_xyz;
4457 s->out_transform = sinusoidal_to_xyz;
4463 s->out_transform = fisheye_to_xyz;
4464 prepare_out = prepare_fisheye_out;
4465 w = lrintf(wf * 0.5f);
4469 s->out_transform = pannini_to_xyz;
4475 s->out_transform = cylindrical_to_xyz;
4476 prepare_out = prepare_cylindrical_out;
4478 h = lrintf(hf * 0.5f);
4481 s->out_transform = perspective_to_xyz;
4483 w = lrintf(wf / 2.f);
4487 s->out_transform = tetrahedron_to_xyz;
4493 s->out_transform = barrelsplit_to_xyz;
4495 w = lrintf(wf / 4.f * 3.f);
4499 s->out_transform = tspyramid_to_xyz;
4504 case HEQUIRECTANGULAR:
4505 s->out_transform = hequirect_to_xyz;
4507 w = lrintf(wf / 2.f);
4511 s->out_transform = equisolid_to_xyz;
4512 prepare_out = prepare_equisolid_out;
4514 h = lrintf(hf * 2.f);
4517 s->out_transform = orthographic_to_xyz;
4518 prepare_out = prepare_orthographic_out;
4520 h = lrintf(hf * 2.f);
4523 s->out_transform = octahedron_to_xyz;
4526 h = lrintf(hf * 2.f);
4529 av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
4533 // Override resolution with user values if specified
4534 if (s->width > 0 && s->height <= 0 && s->h_fov > 0.f && s->v_fov > 0.f &&
4535 s->out == FLAT && s->d_fov == 0.f) {
4537 h = w / tanf(s->h_fov * M_PI / 360.f) * tanf(s->v_fov * M_PI / 360.f);
4538 } else if (s->width <= 0 && s->height > 0 && s->h_fov > 0.f && s->v_fov > 0.f &&
4539 s->out == FLAT && s->d_fov == 0.f) {
4541 w = h / tanf(s->v_fov * M_PI / 360.f) * tanf(s->h_fov * M_PI / 360.f);
4542 } else if (s->width > 0 && s->height > 0) {
4545 } else if (s->width > 0 || s->height > 0) {
4546 av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
4547 return AVERROR(EINVAL);
4549 if (s->out_transpose)
4552 if (s->in_transpose)
4560 fov_from_dfov(s->out, s->d_fov, w, h, &s->h_fov, &s->v_fov);
4563 err = prepare_out(ctx);
4568 set_dimensions(s->pr_width, s->pr_height, w, h, desc);
4570 switch (s->out_stereo) {
4572 out_offset_w = out_offset_h = 0;
4588 set_dimensions(s->out_offset_w, s->out_offset_h, out_offset_w, out_offset_h, desc);
4589 set_dimensions(s->planewidth, s->planeheight, w, h, desc);
4591 for (int i = 0; i < 4; i++)
4592 s->uv_linesize[i] = FFALIGN(s->pr_width[i], 8);
4597 s->nb_threads = FFMIN(outlink->h, ff_filter_get_nb_threads(ctx));
4598 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
4599 have_alpha = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
4601 if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
4602 s->nb_allocated = 1;
4603 s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
4605 s->nb_allocated = 2;
4606 s->map[0] = s->map[3] = 0;
4607 s->map[1] = s->map[2] = 1;
4610 if (!s->slice_remap)
4611 s->slice_remap = av_calloc(s->nb_threads, sizeof(*s->slice_remap));
4612 if (!s->slice_remap)
4613 return AVERROR(ENOMEM);
4615 for (int i = 0; i < s->nb_allocated; i++) {
4616 err = allocate_plane(s, sizeof_uv, sizeof_ker, sizeof_mask * have_alpha * s->alpha, i);
4621 calculate_rotation_matrix(s->yaw, s->pitch, s->roll, s->rot_mat, s->rotation_order);
4622 set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, s->output_mirror_modifier);
4624 ctx->internal->execute(ctx, v360_slice, NULL, NULL, s->nb_threads);
4629 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
4631 AVFilterContext *ctx = inlink->dst;
4632 AVFilterLink *outlink = ctx->outputs[0];
4633 V360Context *s = ctx->priv;
4637 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
4640 return AVERROR(ENOMEM);
4642 av_frame_copy_props(out, in);
4647 ctx->internal->execute(ctx, s->remap_slice, &td, NULL, s->nb_threads);
4650 return ff_filter_frame(outlink, out);
4653 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
4654 char *res, int res_len, int flags)
4658 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
4662 return config_output(ctx->outputs[0]);
4665 static av_cold void uninit(AVFilterContext *ctx)
4667 V360Context *s = ctx->priv;
4669 for (int n = 0; n < s->nb_threads && s->slice_remap; n++) {
4670 SliceXYRemap *r = &s->slice_remap[n];
4672 for (int p = 0; p < s->nb_allocated; p++) {
4675 av_freep(&r->ker[p]);
4681 av_freep(&s->slice_remap);
4684 static const AVFilterPad inputs[] = {
4687 .type = AVMEDIA_TYPE_VIDEO,
4688 .filter_frame = filter_frame,
4693 static const AVFilterPad outputs[] = {
4696 .type = AVMEDIA_TYPE_VIDEO,
4697 .config_props = config_output,
4702 AVFilter ff_vf_v360 = {
4704 .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
4705 .priv_size = sizeof(V360Context),
4707 .query_formats = query_formats,
4710 .priv_class = &v360_class,
4711 .flags = AVFILTER_FLAG_SLICE_THREADS,
4712 .process_command = process_command,