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 { "output", "set output projection", OFFSET(out), AV_OPT_TYPE_INT, {.i64=CUBEMAP_3_2}, 0, NB_PROJECTIONS-1, FLAGS, "out" },
87 { "e", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
88 { "equirect", "equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=EQUIRECTANGULAR}, 0, 0, FLAGS, "out" },
89 { "c3x2", "cubemap 3x2", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_3_2}, 0, 0, FLAGS, "out" },
90 { "c6x1", "cubemap 6x1", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_6_1}, 0, 0, FLAGS, "out" },
91 { "eac", "equi-angular cubemap", 0, AV_OPT_TYPE_CONST, {.i64=EQUIANGULAR}, 0, 0, FLAGS, "out" },
92 { "dfisheye", "dual fisheye", 0, AV_OPT_TYPE_CONST, {.i64=DUAL_FISHEYE}, 0, 0, FLAGS, "out" },
93 { "flat", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
94 {"rectilinear", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
95 { "gnomonic", "regular video", 0, AV_OPT_TYPE_CONST, {.i64=FLAT}, 0, 0, FLAGS, "out" },
96 { "barrel", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
97 { "fb", "barrel facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL}, 0, 0, FLAGS, "out" },
98 { "c1x6", "cubemap 1x6", 0, AV_OPT_TYPE_CONST, {.i64=CUBEMAP_1_6}, 0, 0, FLAGS, "out" },
99 { "sg", "stereographic", 0, AV_OPT_TYPE_CONST, {.i64=STEREOGRAPHIC}, 0, 0, FLAGS, "out" },
100 { "mercator", "mercator", 0, AV_OPT_TYPE_CONST, {.i64=MERCATOR}, 0, 0, FLAGS, "out" },
101 { "ball", "ball", 0, AV_OPT_TYPE_CONST, {.i64=BALL}, 0, 0, FLAGS, "out" },
102 { "hammer", "hammer", 0, AV_OPT_TYPE_CONST, {.i64=HAMMER}, 0, 0, FLAGS, "out" },
103 {"sinusoidal", "sinusoidal", 0, AV_OPT_TYPE_CONST, {.i64=SINUSOIDAL}, 0, 0, FLAGS, "out" },
104 { "fisheye", "fisheye", 0, AV_OPT_TYPE_CONST, {.i64=FISHEYE}, 0, 0, FLAGS, "out" },
105 { "pannini", "pannini", 0, AV_OPT_TYPE_CONST, {.i64=PANNINI}, 0, 0, FLAGS, "out" },
106 {"cylindrical", "cylindrical", 0, AV_OPT_TYPE_CONST, {.i64=CYLINDRICAL}, 0, 0, FLAGS, "out" },
107 {"perspective", "perspective", 0, AV_OPT_TYPE_CONST, {.i64=PERSPECTIVE}, 0, 0, FLAGS, "out" },
108 {"tetrahedron", "tetrahedron", 0, AV_OPT_TYPE_CONST, {.i64=TETRAHEDRON}, 0, 0, FLAGS, "out" },
109 {"barrelsplit", "barrel split facebook's 360 format", 0, AV_OPT_TYPE_CONST, {.i64=BARREL_SPLIT}, 0, 0, FLAGS, "out" },
110 { "tsp", "truncated square pyramid", 0, AV_OPT_TYPE_CONST, {.i64=TSPYRAMID}, 0, 0, FLAGS, "out" },
111 { "hequirect", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "out" },
112 { "he", "half equirectangular", 0, AV_OPT_TYPE_CONST, {.i64=HEQUIRECTANGULAR},0, 0, FLAGS, "out" },
113 { "equisolid", "equisolid", 0, AV_OPT_TYPE_CONST, {.i64=EQUISOLID}, 0, 0, FLAGS, "out" },
114 { "og", "orthographic", 0, AV_OPT_TYPE_CONST, {.i64=ORTHOGRAPHIC}, 0, 0, FLAGS, "out" },
115 { "interp", "set interpolation method", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=BILINEAR}, 0, NB_INTERP_METHODS-1, FLAGS, "interp" },
116 { "near", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
117 { "nearest", "nearest neighbour", 0, AV_OPT_TYPE_CONST, {.i64=NEAREST}, 0, 0, FLAGS, "interp" },
118 { "line", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
119 { "linear", "bilinear interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BILINEAR}, 0, 0, FLAGS, "interp" },
120 { "lagrange9", "lagrange9 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LAGRANGE9}, 0, 0, FLAGS, "interp" },
121 { "cube", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
122 { "cubic", "bicubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=BICUBIC}, 0, 0, FLAGS, "interp" },
123 { "lanc", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
124 { "lanczos", "lanczos interpolation", 0, AV_OPT_TYPE_CONST, {.i64=LANCZOS}, 0, 0, FLAGS, "interp" },
125 { "sp16", "spline16 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=SPLINE16}, 0, 0, FLAGS, "interp" },
126 { "spline16", "spline16 interpolation", 0, AV_OPT_TYPE_CONST, {.i64=SPLINE16}, 0, 0, FLAGS, "interp" },
127 { "gauss", "gaussian interpolation", 0, AV_OPT_TYPE_CONST, {.i64=GAUSSIAN}, 0, 0, FLAGS, "interp" },
128 { "gaussian", "gaussian interpolation", 0, AV_OPT_TYPE_CONST, {.i64=GAUSSIAN}, 0, 0, FLAGS, "interp" },
129 { "w", "output width", OFFSET(width), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "w"},
130 { "h", "output height", OFFSET(height), AV_OPT_TYPE_INT, {.i64=0}, 0, INT16_MAX, FLAGS, "h"},
131 { "in_stereo", "input stereo format", OFFSET(in_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
132 {"out_stereo", "output stereo format", OFFSET(out_stereo), AV_OPT_TYPE_INT, {.i64=STEREO_2D}, 0, NB_STEREO_FMTS-1, FLAGS, "stereo" },
133 { "2d", "2d mono", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_2D}, 0, 0, FLAGS, "stereo" },
134 { "sbs", "side by side", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_SBS}, 0, 0, FLAGS, "stereo" },
135 { "tb", "top bottom", 0, AV_OPT_TYPE_CONST, {.i64=STEREO_TB}, 0, 0, FLAGS, "stereo" },
136 { "in_forder", "input cubemap face order", OFFSET(in_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "in_forder"},
137 {"out_forder", "output cubemap face order", OFFSET(out_forder), AV_OPT_TYPE_STRING, {.str="rludfb"}, 0, NB_DIRECTIONS-1, FLAGS, "out_forder"},
138 { "in_frot", "input cubemap face rotation", OFFSET(in_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "in_frot"},
139 { "out_frot", "output cubemap face rotation",OFFSET(out_frot), AV_OPT_TYPE_STRING, {.str="000000"}, 0, NB_DIRECTIONS-1, FLAGS, "out_frot"},
140 { "in_pad", "percent input cubemap pads", OFFSET(in_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 0.1,TFLAGS, "in_pad"},
141 { "out_pad", "percent output cubemap pads", OFFSET(out_pad), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 0.1,TFLAGS, "out_pad"},
142 { "fin_pad", "fixed input cubemap pads", OFFSET(fin_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100,TFLAGS, "fin_pad"},
143 { "fout_pad", "fixed output cubemap pads", OFFSET(fout_pad), AV_OPT_TYPE_INT, {.i64=0}, 0, 100,TFLAGS, "fout_pad"},
144 { "yaw", "yaw rotation", OFFSET(yaw), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "yaw"},
145 { "pitch", "pitch rotation", OFFSET(pitch), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "pitch"},
146 { "roll", "roll rotation", OFFSET(roll), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, -180.f, 180.f,TFLAGS, "roll"},
147 { "rorder", "rotation order", OFFSET(rorder), AV_OPT_TYPE_STRING, {.str="ypr"}, 0, 0,TFLAGS, "rorder"},
148 { "h_fov", "output horizontal field of view",OFFSET(h_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f,TFLAGS, "h_fov"},
149 { "v_fov", "output vertical field of view", OFFSET(v_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f,TFLAGS, "v_fov"},
150 { "d_fov", "output diagonal field of view", OFFSET(d_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "d_fov"},
151 { "h_flip", "flip out video horizontally", OFFSET(h_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "h_flip"},
152 { "v_flip", "flip out video vertically", OFFSET(v_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "v_flip"},
153 { "d_flip", "flip out video indepth", OFFSET(d_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "d_flip"},
154 { "ih_flip", "flip in video horizontally", OFFSET(ih_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "ih_flip"},
155 { "iv_flip", "flip in video vertically", OFFSET(iv_flip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,TFLAGS, "iv_flip"},
156 { "in_trans", "transpose video input", OFFSET(in_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "in_transpose"},
157 { "out_trans", "transpose video output", OFFSET(out_transpose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "out_transpose"},
158 { "ih_fov", "input horizontal field of view",OFFSET(ih_fov), AV_OPT_TYPE_FLOAT, {.dbl=90.f}, 0.00001f, 360.f,TFLAGS, "ih_fov"},
159 { "iv_fov", "input vertical field of view", OFFSET(iv_fov), AV_OPT_TYPE_FLOAT, {.dbl=45.f}, 0.00001f, 360.f,TFLAGS, "iv_fov"},
160 { "id_fov", "input diagonal field of view", OFFSET(id_fov), AV_OPT_TYPE_FLOAT, {.dbl=0.f}, 0.f, 360.f,TFLAGS, "id_fov"},
161 {"alpha_mask", "build mask in alpha plane", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, "alpha"},
165 AVFILTER_DEFINE_CLASS(v360);
167 static int query_formats(AVFilterContext *ctx)
169 V360Context *s = ctx->priv;
170 static const enum AVPixelFormat pix_fmts[] = {
172 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
173 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
174 AV_PIX_FMT_YUVA444P16,
177 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
178 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
179 AV_PIX_FMT_YUVA422P16,
182 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
183 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
186 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
187 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
191 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P9,
192 AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12,
193 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
196 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV440P10,
197 AV_PIX_FMT_YUV440P12,
200 AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P9,
201 AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12,
202 AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
205 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P9,
206 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12,
207 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
216 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9,
217 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
218 AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
221 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
222 AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
225 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
226 AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12,
227 AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
231 static const enum AVPixelFormat alpha_pix_fmts[] = {
232 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9,
233 AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12,
234 AV_PIX_FMT_YUVA444P16,
235 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA422P9,
236 AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12,
237 AV_PIX_FMT_YUVA422P16,
238 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA420P9,
239 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
240 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10,
241 AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
245 AVFilterFormats *fmts_list = ff_make_format_list(s->alpha ? alpha_pix_fmts : pix_fmts);
247 return AVERROR(ENOMEM);
248 return ff_set_common_formats(ctx, fmts_list);
251 #define DEFINE_REMAP1_LINE(bits, div) \
252 static void remap1_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *const src, \
253 ptrdiff_t in_linesize, \
254 const int16_t *const u, const int16_t *const v, \
255 const int16_t *const ker) \
257 const uint##bits##_t *const s = (const uint##bits##_t *const)src; \
258 uint##bits##_t *d = (uint##bits##_t *)dst; \
260 in_linesize /= div; \
262 for (int x = 0; x < width; x++) \
263 d[x] = s[v[x] * in_linesize + u[x]]; \
266 DEFINE_REMAP1_LINE( 8, 1)
267 DEFINE_REMAP1_LINE(16, 2)
270 * Generate remapping function with a given window size and pixel depth.
272 * @param ws size of interpolation window
273 * @param bits number of bits per pixel
275 #define DEFINE_REMAP(ws, bits) \
276 static int remap##ws##_##bits##bit_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
278 ThreadData *td = arg; \
279 const V360Context *s = ctx->priv; \
280 const AVFrame *in = td->in; \
281 AVFrame *out = td->out; \
283 for (int stereo = 0; stereo < 1 + s->out_stereo > STEREO_2D; stereo++) { \
284 for (int plane = 0; plane < s->nb_planes; plane++) { \
285 const unsigned map = s->map[plane]; \
286 const int in_linesize = in->linesize[plane]; \
287 const int out_linesize = out->linesize[plane]; \
288 const int uv_linesize = s->uv_linesize[plane]; \
289 const int in_offset_w = stereo ? s->in_offset_w[plane] : 0; \
290 const int in_offset_h = stereo ? s->in_offset_h[plane] : 0; \
291 const int out_offset_w = stereo ? s->out_offset_w[plane] : 0; \
292 const int out_offset_h = stereo ? s->out_offset_h[plane] : 0; \
293 const uint8_t *const src = in->data[plane] + \
294 in_offset_h * in_linesize + in_offset_w * (bits >> 3); \
295 uint8_t *dst = out->data[plane] + out_offset_h * out_linesize + out_offset_w * (bits >> 3); \
296 const uint8_t *mask = plane == 3 ? s->mask : NULL; \
297 const int width = s->pr_width[plane]; \
298 const int height = s->pr_height[plane]; \
300 const int slice_start = (height * jobnr ) / nb_jobs; \
301 const int slice_end = (height * (jobnr + 1)) / nb_jobs; \
303 for (int y = slice_start; y < slice_end && !mask; y++) { \
304 const int16_t *const u = s->u[map] + y * uv_linesize * ws * ws; \
305 const int16_t *const v = s->v[map] + y * uv_linesize * ws * ws; \
306 const int16_t *const ker = s->ker[map] + y * uv_linesize * ws * ws; \
308 s->remap_line(dst + y * out_linesize, width, src, in_linesize, u, v, ker); \
311 for (int y = slice_start; y < slice_end && mask; y++) { \
312 memcpy(dst + y * out_linesize, mask + y * width * (bits >> 3), width * (bits >> 3)); \
329 #define DEFINE_REMAP_LINE(ws, bits, div) \
330 static void remap##ws##_##bits##bit_line_c(uint8_t *dst, int width, const uint8_t *const src, \
331 ptrdiff_t in_linesize, \
332 const int16_t *const u, const int16_t *const v, \
333 const int16_t *const ker) \
335 const uint##bits##_t *const s = (const uint##bits##_t *const)src; \
336 uint##bits##_t *d = (uint##bits##_t *)dst; \
338 in_linesize /= div; \
340 for (int x = 0; x < width; x++) { \
341 const int16_t *const uu = u + x * ws * ws; \
342 const int16_t *const vv = v + x * ws * ws; \
343 const int16_t *const kker = ker + x * ws * ws; \
346 for (int i = 0; i < ws; i++) { \
347 for (int j = 0; j < ws; j++) { \
348 tmp += kker[i * ws + j] * s[vv[i * ws + j] * in_linesize + uu[i * ws + j]]; \
352 d[x] = av_clip_uint##bits(tmp >> 14); \
356 DEFINE_REMAP_LINE(2, 8, 1)
357 DEFINE_REMAP_LINE(3, 8, 1)
358 DEFINE_REMAP_LINE(4, 8, 1)
359 DEFINE_REMAP_LINE(2, 16, 2)
360 DEFINE_REMAP_LINE(3, 16, 2)
361 DEFINE_REMAP_LINE(4, 16, 2)
363 void ff_v360_init(V360Context *s, int depth)
367 s->remap_line = depth <= 8 ? remap1_8bit_line_c : remap1_16bit_line_c;
370 s->remap_line = depth <= 8 ? remap2_8bit_line_c : remap2_16bit_line_c;
373 s->remap_line = depth <= 8 ? remap3_8bit_line_c : remap3_16bit_line_c;
379 s->remap_line = depth <= 8 ? remap4_8bit_line_c : remap4_16bit_line_c;
384 ff_v360_init_x86(s, depth);
388 * Save nearest pixel coordinates for remapping.
390 * @param du horizontal relative coordinate
391 * @param dv vertical relative coordinate
392 * @param rmap calculated 4x4 window
393 * @param u u remap data
394 * @param v v remap data
395 * @param ker ker remap data
397 static void nearest_kernel(float du, float dv, const XYRemap *rmap,
398 int16_t *u, int16_t *v, int16_t *ker)
400 const int i = lrintf(dv) + 1;
401 const int j = lrintf(du) + 1;
403 u[0] = rmap->u[i][j];
404 v[0] = rmap->v[i][j];
408 * Calculate kernel for bilinear interpolation.
410 * @param du horizontal relative coordinate
411 * @param dv vertical relative coordinate
412 * @param rmap calculated 4x4 window
413 * @param u u remap data
414 * @param v v remap data
415 * @param ker ker remap data
417 static void bilinear_kernel(float du, float dv, const XYRemap *rmap,
418 int16_t *u, int16_t *v, int16_t *ker)
420 for (int i = 0; i < 2; i++) {
421 for (int j = 0; j < 2; j++) {
422 u[i * 2 + j] = rmap->u[i + 1][j + 1];
423 v[i * 2 + j] = rmap->v[i + 1][j + 1];
427 ker[0] = lrintf((1.f - du) * (1.f - dv) * 16385.f);
428 ker[1] = lrintf( du * (1.f - dv) * 16385.f);
429 ker[2] = lrintf((1.f - du) * dv * 16385.f);
430 ker[3] = lrintf( du * dv * 16385.f);
434 * Calculate 1-dimensional lagrange coefficients.
436 * @param t relative coordinate
437 * @param coeffs coefficients
439 static inline void calculate_lagrange_coeffs(float t, float *coeffs)
441 coeffs[0] = (t - 1.f) * (t - 2.f) * 0.5f;
442 coeffs[1] = -t * (t - 2.f);
443 coeffs[2] = t * (t - 1.f) * 0.5f;
447 * Calculate kernel for lagrange interpolation.
449 * @param du horizontal relative coordinate
450 * @param dv vertical relative coordinate
451 * @param rmap calculated 4x4 window
452 * @param u u remap data
453 * @param v v remap data
454 * @param ker ker remap data
456 static void lagrange_kernel(float du, float dv, const XYRemap *rmap,
457 int16_t *u, int16_t *v, int16_t *ker)
462 calculate_lagrange_coeffs(du, du_coeffs);
463 calculate_lagrange_coeffs(dv, dv_coeffs);
465 for (int i = 0; i < 3; i++) {
466 for (int j = 0; j < 3; j++) {
467 u[i * 3 + j] = rmap->u[i + 1][j + 1];
468 v[i * 3 + j] = rmap->v[i + 1][j + 1];
469 ker[i * 3 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
475 * Calculate 1-dimensional cubic coefficients.
477 * @param t relative coordinate
478 * @param coeffs coefficients
480 static inline void calculate_bicubic_coeffs(float t, float *coeffs)
482 const float tt = t * t;
483 const float ttt = t * t * t;
485 coeffs[0] = - t / 3.f + tt / 2.f - ttt / 6.f;
486 coeffs[1] = 1.f - t / 2.f - tt + ttt / 2.f;
487 coeffs[2] = t + tt / 2.f - ttt / 2.f;
488 coeffs[3] = - t / 6.f + ttt / 6.f;
492 * Calculate kernel for bicubic interpolation.
494 * @param du horizontal relative coordinate
495 * @param dv vertical relative coordinate
496 * @param rmap calculated 4x4 window
497 * @param u u remap data
498 * @param v v remap data
499 * @param ker ker remap data
501 static void bicubic_kernel(float du, float dv, const XYRemap *rmap,
502 int16_t *u, int16_t *v, int16_t *ker)
507 calculate_bicubic_coeffs(du, du_coeffs);
508 calculate_bicubic_coeffs(dv, dv_coeffs);
510 for (int i = 0; i < 4; i++) {
511 for (int j = 0; j < 4; j++) {
512 u[i * 4 + j] = rmap->u[i][j];
513 v[i * 4 + j] = rmap->v[i][j];
514 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
520 * Calculate 1-dimensional lanczos coefficients.
522 * @param t relative coordinate
523 * @param coeffs coefficients
525 static inline void calculate_lanczos_coeffs(float t, float *coeffs)
529 for (int i = 0; i < 4; i++) {
530 const float x = M_PI * (t - i + 1);
534 coeffs[i] = sinf(x) * sinf(x / 2.f) / (x * x / 2.f);
539 for (int i = 0; i < 4; i++) {
545 * Calculate kernel for lanczos interpolation.
547 * @param du horizontal relative coordinate
548 * @param dv vertical relative coordinate
549 * @param rmap calculated 4x4 window
550 * @param u u remap data
551 * @param v v remap data
552 * @param ker ker remap data
554 static void lanczos_kernel(float du, float dv, const XYRemap *rmap,
555 int16_t *u, int16_t *v, int16_t *ker)
560 calculate_lanczos_coeffs(du, du_coeffs);
561 calculate_lanczos_coeffs(dv, dv_coeffs);
563 for (int i = 0; i < 4; i++) {
564 for (int j = 0; j < 4; j++) {
565 u[i * 4 + j] = rmap->u[i][j];
566 v[i * 4 + j] = rmap->v[i][j];
567 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
573 * Calculate 1-dimensional spline16 coefficients.
575 * @param t relative coordinate
576 * @param coeffs coefficients
578 static void calculate_spline16_coeffs(float t, float *coeffs)
580 coeffs[0] = ((-1.f / 3.f * t + 0.8f) * t - 7.f / 15.f) * t;
581 coeffs[1] = ((t - 9.f / 5.f) * t - 0.2f) * t + 1.f;
582 coeffs[2] = ((6.f / 5.f - t) * t + 0.8f) * t;
583 coeffs[3] = ((1.f / 3.f * t - 0.2f) * t - 2.f / 15.f) * t;
587 * Calculate kernel for spline16 interpolation.
589 * @param du horizontal relative coordinate
590 * @param dv vertical relative coordinate
591 * @param rmap calculated 4x4 window
592 * @param u u remap data
593 * @param v v remap data
594 * @param ker ker remap data
596 static void spline16_kernel(float du, float dv, const XYRemap *rmap,
597 int16_t *u, int16_t *v, int16_t *ker)
602 calculate_spline16_coeffs(du, du_coeffs);
603 calculate_spline16_coeffs(dv, dv_coeffs);
605 for (int i = 0; i < 4; i++) {
606 for (int j = 0; j < 4; j++) {
607 u[i * 4 + j] = rmap->u[i][j];
608 v[i * 4 + j] = rmap->v[i][j];
609 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
615 * Calculate 1-dimensional gaussian coefficients.
617 * @param t relative coordinate
618 * @param coeffs coefficients
620 static void calculate_gaussian_coeffs(float t, float *coeffs)
624 for (int i = 0; i < 4; i++) {
625 const float x = t - (i - 1);
629 coeffs[i] = expf(-2.f * x * x) * expf(-x * x / 2.f);
634 for (int i = 0; i < 4; i++) {
640 * Calculate kernel for gaussian interpolation.
642 * @param du horizontal relative coordinate
643 * @param dv vertical relative coordinate
644 * @param rmap calculated 4x4 window
645 * @param u u remap data
646 * @param v v remap data
647 * @param ker ker remap data
649 static void gaussian_kernel(float du, float dv, const XYRemap *rmap,
650 int16_t *u, int16_t *v, int16_t *ker)
655 calculate_gaussian_coeffs(du, du_coeffs);
656 calculate_gaussian_coeffs(dv, dv_coeffs);
658 for (int i = 0; i < 4; i++) {
659 for (int j = 0; j < 4; j++) {
660 u[i * 4 + j] = rmap->u[i][j];
661 v[i * 4 + j] = rmap->v[i][j];
662 ker[i * 4 + j] = lrintf(du_coeffs[j] * dv_coeffs[i] * 16385.f);
668 * Modulo operation with only positive remainders.
673 * @return positive remainder of (a / b)
675 static inline int mod(int a, int b)
677 const int res = a % b;
686 * Reflect y operation.
688 * @param y input vertical position
689 * @param h input height
691 static inline int reflecty(int y, int h)
696 return 2 * h - 1 - y;
703 * Reflect x operation for equirect.
705 * @param x input horizontal position
706 * @param y input vertical position
707 * @param w input width
708 * @param h input height
710 static inline int ereflectx(int x, int y, int w, int h)
719 * Reflect x operation.
721 * @param x input horizontal position
722 * @param y input vertical position
723 * @param w input width
724 * @param h input height
726 static inline int reflectx(int x, int y, int w, int h)
735 * Convert char to corresponding direction.
736 * Used for cubemap options.
738 static int get_direction(char c)
759 * Convert char to corresponding rotation angle.
760 * Used for cubemap options.
762 static int get_rotation(char c)
779 * Convert char to corresponding rotation order.
781 static int get_rorder(char c)
799 * Prepare data for processing cubemap input format.
801 * @param ctx filter context
805 static int prepare_cube_in(AVFilterContext *ctx)
807 V360Context *s = ctx->priv;
809 for (int face = 0; face < NB_FACES; face++) {
810 const char c = s->in_forder[face];
814 av_log(ctx, AV_LOG_ERROR,
815 "Incomplete in_forder option. Direction for all 6 faces should be specified.\n");
816 return AVERROR(EINVAL);
819 direction = get_direction(c);
820 if (direction == -1) {
821 av_log(ctx, AV_LOG_ERROR,
822 "Incorrect direction symbol '%c' in in_forder option.\n", c);
823 return AVERROR(EINVAL);
826 s->in_cubemap_face_order[direction] = face;
829 for (int face = 0; face < NB_FACES; face++) {
830 const char c = s->in_frot[face];
834 av_log(ctx, AV_LOG_ERROR,
835 "Incomplete in_frot option. Rotation for all 6 faces should be specified.\n");
836 return AVERROR(EINVAL);
839 rotation = get_rotation(c);
840 if (rotation == -1) {
841 av_log(ctx, AV_LOG_ERROR,
842 "Incorrect rotation symbol '%c' in in_frot option.\n", c);
843 return AVERROR(EINVAL);
846 s->in_cubemap_face_rotation[face] = rotation;
853 * Prepare data for processing cubemap output format.
855 * @param ctx filter context
859 static int prepare_cube_out(AVFilterContext *ctx)
861 V360Context *s = ctx->priv;
863 for (int face = 0; face < NB_FACES; face++) {
864 const char c = s->out_forder[face];
868 av_log(ctx, AV_LOG_ERROR,
869 "Incomplete out_forder option. Direction for all 6 faces should be specified.\n");
870 return AVERROR(EINVAL);
873 direction = get_direction(c);
874 if (direction == -1) {
875 av_log(ctx, AV_LOG_ERROR,
876 "Incorrect direction symbol '%c' in out_forder option.\n", c);
877 return AVERROR(EINVAL);
880 s->out_cubemap_direction_order[face] = direction;
883 for (int face = 0; face < NB_FACES; face++) {
884 const char c = s->out_frot[face];
888 av_log(ctx, AV_LOG_ERROR,
889 "Incomplete out_frot option. Rotation for all 6 faces should be specified.\n");
890 return AVERROR(EINVAL);
893 rotation = get_rotation(c);
894 if (rotation == -1) {
895 av_log(ctx, AV_LOG_ERROR,
896 "Incorrect rotation symbol '%c' in out_frot option.\n", c);
897 return AVERROR(EINVAL);
900 s->out_cubemap_face_rotation[face] = rotation;
906 static inline void rotate_cube_face(float *uf, float *vf, int rotation)
932 static inline void rotate_cube_face_inverse(float *uf, float *vf, int rotation)
963 static void normalize_vector(float *vec)
965 const float norm = sqrtf(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
973 * Calculate 3D coordinates on sphere for corresponding cubemap position.
974 * Common operation for every cubemap.
976 * @param s filter private context
977 * @param uf horizontal cubemap coordinate [0, 1)
978 * @param vf vertical cubemap coordinate [0, 1)
979 * @param face face of cubemap
980 * @param vec coordinates on sphere
981 * @param scalew scale for uf
982 * @param scaleh scale for vf
984 static void cube_to_xyz(const V360Context *s,
985 float uf, float vf, int face,
986 float *vec, float scalew, float scaleh)
988 const int direction = s->out_cubemap_direction_order[face];
994 rotate_cube_face_inverse(&uf, &vf, s->out_cubemap_face_rotation[face]);
1035 normalize_vector(vec);
1039 * Calculate cubemap position for corresponding 3D coordinates on sphere.
1040 * Common operation for every cubemap.
1042 * @param s filter private context
1043 * @param vec coordinated on sphere
1044 * @param uf horizontal cubemap coordinate [0, 1)
1045 * @param vf vertical cubemap coordinate [0, 1)
1046 * @param direction direction of view
1048 static void xyz_to_cube(const V360Context *s,
1050 float *uf, float *vf, int *direction)
1052 const float phi = atan2f(vec[0], vec[2]);
1053 const float theta = asinf(vec[1]);
1054 float phi_norm, theta_threshold;
1057 if (phi >= -M_PI_4 && phi < M_PI_4) {
1060 } else if (phi >= -(M_PI_2 + M_PI_4) && phi < -M_PI_4) {
1062 phi_norm = phi + M_PI_2;
1063 } else if (phi >= M_PI_4 && phi < M_PI_2 + M_PI_4) {
1065 phi_norm = phi - M_PI_2;
1068 phi_norm = phi + ((phi > 0.f) ? -M_PI : M_PI);
1071 theta_threshold = atanf(cosf(phi_norm));
1072 if (theta > theta_threshold) {
1074 } else if (theta < -theta_threshold) {
1078 switch (*direction) {
1080 *uf = -vec[2] / vec[0];
1081 *vf = vec[1] / vec[0];
1084 *uf = -vec[2] / vec[0];
1085 *vf = -vec[1] / vec[0];
1088 *uf = -vec[0] / vec[1];
1089 *vf = -vec[2] / vec[1];
1092 *uf = vec[0] / vec[1];
1093 *vf = -vec[2] / vec[1];
1096 *uf = vec[0] / vec[2];
1097 *vf = vec[1] / vec[2];
1100 *uf = vec[0] / vec[2];
1101 *vf = -vec[1] / vec[2];
1107 face = s->in_cubemap_face_order[*direction];
1108 rotate_cube_face(uf, vf, s->in_cubemap_face_rotation[face]);
1110 (*uf) *= s->input_mirror_modifier[0];
1111 (*vf) *= s->input_mirror_modifier[1];
1115 * Find position on another cube face in case of overflow/underflow.
1116 * Used for calculation of interpolation window.
1118 * @param s filter private context
1119 * @param uf horizontal cubemap coordinate
1120 * @param vf vertical cubemap coordinate
1121 * @param direction direction of view
1122 * @param new_uf new horizontal cubemap coordinate
1123 * @param new_vf new vertical cubemap coordinate
1124 * @param face face position on cubemap
1126 static void process_cube_coordinates(const V360Context *s,
1127 float uf, float vf, int direction,
1128 float *new_uf, float *new_vf, int *face)
1131 * Cubemap orientation
1138 * +-------+-------+-------+-------+ ^ e |
1140 * | left | front | right | back | | g |
1141 * +-------+-------+-------+-------+ v h v
1147 *face = s->in_cubemap_face_order[direction];
1148 rotate_cube_face_inverse(&uf, &vf, s->in_cubemap_face_rotation[*face]);
1150 if ((uf < -1.f || uf >= 1.f) && (vf < -1.f || vf >= 1.f)) {
1151 // There are no pixels to use in this case
1154 } else if (uf < -1.f) {
1156 switch (direction) {
1190 } else if (uf >= 1.f) {
1192 switch (direction) {
1226 } else if (vf < -1.f) {
1228 switch (direction) {
1262 } else if (vf >= 1.f) {
1264 switch (direction) {
1304 *face = s->in_cubemap_face_order[direction];
1305 rotate_cube_face(new_uf, new_vf, s->in_cubemap_face_rotation[*face]);
1309 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap3x2 format.
1311 * @param s filter private context
1312 * @param i horizontal position on frame [0, width)
1313 * @param j vertical position on frame [0, height)
1314 * @param width frame width
1315 * @param height frame height
1316 * @param vec coordinates on sphere
1318 static int cube3x2_to_xyz(const V360Context *s,
1319 int i, int j, int width, int height,
1322 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width / 3.f) : 1.f - s->out_pad;
1323 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 2.f) : 1.f - s->out_pad;
1325 const float ew = width / 3.f;
1326 const float eh = height / 2.f;
1328 const int u_face = floorf(i / ew);
1329 const int v_face = floorf(j / eh);
1330 const int face = u_face + 3 * v_face;
1332 const int u_shift = ceilf(ew * u_face);
1333 const int v_shift = ceilf(eh * v_face);
1334 const int ewi = ceilf(ew * (u_face + 1)) - u_shift;
1335 const int ehi = ceilf(eh * (v_face + 1)) - v_shift;
1337 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1338 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1340 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1346 * Calculate frame position in cubemap3x2 format for corresponding 3D coordinates on sphere.
1348 * @param s filter private context
1349 * @param vec coordinates on sphere
1350 * @param width frame width
1351 * @param height frame height
1352 * @param us horizontal coordinates for interpolation window
1353 * @param vs vertical coordinates for interpolation window
1354 * @param du horizontal relative coordinate
1355 * @param dv vertical relative coordinate
1357 static int xyz_to_cube3x2(const V360Context *s,
1358 const float *vec, int width, int height,
1359 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1361 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width / 3.f) : 1.f - s->in_pad;
1362 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 2.f) : 1.f - s->in_pad;
1363 const float ew = width / 3.f;
1364 const float eh = height / 2.f;
1368 int direction, face;
1371 xyz_to_cube(s, vec, &uf, &vf, &direction);
1376 face = s->in_cubemap_face_order[direction];
1379 ewi = ceilf(ew * (u_face + 1)) - ceilf(ew * u_face);
1380 ehi = ceilf(eh * (v_face + 1)) - ceilf(eh * v_face);
1382 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1383 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1391 for (int i = 0; i < 4; i++) {
1392 for (int j = 0; j < 4; j++) {
1393 int new_ui = ui + j - 1;
1394 int new_vi = vi + i - 1;
1395 int u_shift, v_shift;
1396 int new_ewi, new_ehi;
1398 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1399 face = s->in_cubemap_face_order[direction];
1403 u_shift = ceilf(ew * u_face);
1404 v_shift = ceilf(eh * v_face);
1406 uf = 2.f * new_ui / ewi - 1.f;
1407 vf = 2.f * new_vi / ehi - 1.f;
1412 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1419 u_shift = ceilf(ew * u_face);
1420 v_shift = ceilf(eh * v_face);
1421 new_ewi = ceilf(ew * (u_face + 1)) - u_shift;
1422 new_ehi = ceilf(eh * (v_face + 1)) - v_shift;
1424 new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1425 new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1428 us[i][j] = u_shift + new_ui;
1429 vs[i][j] = v_shift + new_vi;
1437 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap1x6 format.
1439 * @param s filter private context
1440 * @param i horizontal position on frame [0, width)
1441 * @param j vertical position on frame [0, height)
1442 * @param width frame width
1443 * @param height frame height
1444 * @param vec coordinates on sphere
1446 static int cube1x6_to_xyz(const V360Context *s,
1447 int i, int j, int width, int height,
1450 const float scalew = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / width : 1.f - s->out_pad;
1451 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 6.f) : 1.f - s->out_pad;
1453 const float ew = width;
1454 const float eh = height / 6.f;
1456 const int face = floorf(j / eh);
1458 const int v_shift = ceilf(eh * face);
1459 const int ehi = ceilf(eh * (face + 1)) - v_shift;
1461 const float uf = 2.f * (i + 0.5f) / ew - 1.f;
1462 const float vf = 2.f * (j - v_shift + 0.5f) / ehi - 1.f;
1464 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1470 * Calculate 3D coordinates on sphere for corresponding frame position in cubemap6x1 format.
1472 * @param s filter private context
1473 * @param i horizontal position on frame [0, width)
1474 * @param j vertical position on frame [0, height)
1475 * @param width frame width
1476 * @param height frame height
1477 * @param vec coordinates on sphere
1479 static int cube6x1_to_xyz(const V360Context *s,
1480 int i, int j, int width, int height,
1483 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width / 6.f) : 1.f - s->out_pad;
1484 const float scaleh = s->fout_pad > 0 ? 1.f - (float)(s->fout_pad) / height : 1.f - s->out_pad;
1486 const float ew = width / 6.f;
1487 const float eh = height;
1489 const int face = floorf(i / ew);
1491 const int u_shift = ceilf(ew * face);
1492 const int ewi = ceilf(ew * (face + 1)) - u_shift;
1494 const float uf = 2.f * (i - u_shift + 0.5f) / ewi - 1.f;
1495 const float vf = 2.f * (j + 0.5f) / eh - 1.f;
1497 cube_to_xyz(s, uf, vf, face, vec, scalew, scaleh);
1503 * Calculate frame position in cubemap1x6 format for corresponding 3D coordinates on sphere.
1505 * @param s filter private context
1506 * @param vec coordinates on sphere
1507 * @param width frame width
1508 * @param height frame height
1509 * @param us horizontal coordinates for interpolation window
1510 * @param vs vertical coordinates for interpolation window
1511 * @param du horizontal relative coordinate
1512 * @param dv vertical relative coordinate
1514 static int xyz_to_cube1x6(const V360Context *s,
1515 const float *vec, int width, int height,
1516 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1518 const float scalew = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / width : 1.f - s->in_pad;
1519 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 6.f) : 1.f - s->in_pad;
1520 const float eh = height / 6.f;
1521 const int ewi = width;
1525 int direction, face;
1527 xyz_to_cube(s, vec, &uf, &vf, &direction);
1532 face = s->in_cubemap_face_order[direction];
1533 ehi = ceilf(eh * (face + 1)) - ceilf(eh * face);
1535 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1536 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1544 for (int i = 0; i < 4; i++) {
1545 for (int j = 0; j < 4; j++) {
1546 int new_ui = ui + j - 1;
1547 int new_vi = vi + i - 1;
1551 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1552 face = s->in_cubemap_face_order[direction];
1554 v_shift = ceilf(eh * face);
1556 uf = 2.f * new_ui / ewi - 1.f;
1557 vf = 2.f * new_vi / ehi - 1.f;
1562 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1567 v_shift = ceilf(eh * face);
1568 new_ehi = ceilf(eh * (face + 1)) - v_shift;
1570 new_ui = av_clip(lrintf(0.5f * ewi * (uf + 1.f)), 0, ewi - 1);
1571 new_vi = av_clip(lrintf(0.5f * new_ehi * (vf + 1.f)), 0, new_ehi - 1);
1575 vs[i][j] = v_shift + new_vi;
1583 * Calculate frame position in cubemap6x1 format for corresponding 3D coordinates on sphere.
1585 * @param s filter private context
1586 * @param vec coordinates on sphere
1587 * @param width frame width
1588 * @param height frame height
1589 * @param us horizontal coordinates for interpolation window
1590 * @param vs vertical coordinates for interpolation window
1591 * @param du horizontal relative coordinate
1592 * @param dv vertical relative coordinate
1594 static int xyz_to_cube6x1(const V360Context *s,
1595 const float *vec, int width, int height,
1596 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1598 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width / 6.f) : 1.f - s->in_pad;
1599 const float scaleh = s->fin_pad > 0 ? 1.f - (float)(s->fin_pad) / height : 1.f - s->in_pad;
1600 const float ew = width / 6.f;
1601 const int ehi = height;
1605 int direction, face;
1607 xyz_to_cube(s, vec, &uf, &vf, &direction);
1612 face = s->in_cubemap_face_order[direction];
1613 ewi = ceilf(ew * (face + 1)) - ceilf(ew * face);
1615 uf = 0.5f * ewi * (uf + 1.f) - 0.5f;
1616 vf = 0.5f * ehi * (vf + 1.f) - 0.5f;
1624 for (int i = 0; i < 4; i++) {
1625 for (int j = 0; j < 4; j++) {
1626 int new_ui = ui + j - 1;
1627 int new_vi = vi + i - 1;
1631 if (new_ui >= 0 && new_ui < ewi && new_vi >= 0 && new_vi < ehi) {
1632 face = s->in_cubemap_face_order[direction];
1634 u_shift = ceilf(ew * face);
1636 uf = 2.f * new_ui / ewi - 1.f;
1637 vf = 2.f * new_vi / ehi - 1.f;
1642 process_cube_coordinates(s, uf, vf, direction, &uf, &vf, &face);
1647 u_shift = ceilf(ew * face);
1648 new_ewi = ceilf(ew * (face + 1)) - u_shift;
1650 new_ui = av_clip(lrintf(0.5f * new_ewi * (uf + 1.f)), 0, new_ewi - 1);
1651 new_vi = av_clip(lrintf(0.5f * ehi * (vf + 1.f)), 0, ehi - 1);
1654 us[i][j] = u_shift + new_ui;
1663 * Calculate 3D coordinates on sphere for corresponding frame position in equirectangular format.
1665 * @param s filter private context
1666 * @param i horizontal position on frame [0, width)
1667 * @param j vertical position on frame [0, height)
1668 * @param width frame width
1669 * @param height frame height
1670 * @param vec coordinates on sphere
1672 static int equirect_to_xyz(const V360Context *s,
1673 int i, int j, int width, int height,
1676 const float phi = ((2.f * i + 0.5f) / width - 1.f) * M_PI;
1677 const float theta = ((2.f * j + 0.5f) / height - 1.f) * M_PI_2;
1679 const float sin_phi = sinf(phi);
1680 const float cos_phi = cosf(phi);
1681 const float sin_theta = sinf(theta);
1682 const float cos_theta = cosf(theta);
1684 vec[0] = cos_theta * sin_phi;
1686 vec[2] = cos_theta * cos_phi;
1692 * Calculate 3D coordinates on sphere for corresponding frame position in half equirectangular format.
1694 * @param s filter private context
1695 * @param i horizontal position on frame [0, width)
1696 * @param j vertical position on frame [0, height)
1697 * @param width frame width
1698 * @param height frame height
1699 * @param vec coordinates on sphere
1701 static int hequirect_to_xyz(const V360Context *s,
1702 int i, int j, int width, int height,
1705 const float phi = ((2.f * i + 0.5f) / width - 1.f) * M_PI_2;
1706 const float theta = ((2.f * j + 0.5f) / height - 1.f) * M_PI_2;
1708 const float sin_phi = sinf(phi);
1709 const float cos_phi = cosf(phi);
1710 const float sin_theta = sinf(theta);
1711 const float cos_theta = cosf(theta);
1713 vec[0] = cos_theta * sin_phi;
1715 vec[2] = cos_theta * cos_phi;
1721 * Prepare data for processing stereographic output format.
1723 * @param ctx filter context
1725 * @return error code
1727 static int prepare_stereographic_out(AVFilterContext *ctx)
1729 V360Context *s = ctx->priv;
1731 s->flat_range[0] = tanf(FFMIN(s->h_fov, 359.f) * M_PI / 720.f);
1732 s->flat_range[1] = tanf(FFMIN(s->v_fov, 359.f) * M_PI / 720.f);
1738 * Calculate 3D coordinates on sphere for corresponding frame position in stereographic format.
1740 * @param s filter private context
1741 * @param i horizontal position on frame [0, width)
1742 * @param j vertical position on frame [0, height)
1743 * @param width frame width
1744 * @param height frame height
1745 * @param vec coordinates on sphere
1747 static int stereographic_to_xyz(const V360Context *s,
1748 int i, int j, int width, int height,
1751 const float x = ((2.f * i + 1.f) / width - 1.f) * s->flat_range[0];
1752 const float y = ((2.f * j + 1.f) / height - 1.f) * s->flat_range[1];
1753 const float r = hypotf(x, y);
1754 const float theta = atanf(r) * 2.f;
1755 const float sin_theta = sinf(theta);
1757 vec[0] = x / r * sin_theta;
1758 vec[1] = y / r * sin_theta;
1759 vec[2] = cosf(theta);
1761 normalize_vector(vec);
1767 * Prepare data for processing stereographic input format.
1769 * @param ctx filter context
1771 * @return error code
1773 static int prepare_stereographic_in(AVFilterContext *ctx)
1775 V360Context *s = ctx->priv;
1777 s->iflat_range[0] = tanf(FFMIN(s->ih_fov, 359.f) * M_PI / 720.f);
1778 s->iflat_range[1] = tanf(FFMIN(s->iv_fov, 359.f) * M_PI / 720.f);
1784 * Calculate frame position in stereographic format for corresponding 3D coordinates on sphere.
1786 * @param s filter private context
1787 * @param vec coordinates on sphere
1788 * @param width frame width
1789 * @param height frame height
1790 * @param us horizontal coordinates for interpolation window
1791 * @param vs vertical coordinates for interpolation window
1792 * @param du horizontal relative coordinate
1793 * @param dv vertical relative coordinate
1795 static int xyz_to_stereographic(const V360Context *s,
1796 const float *vec, int width, int height,
1797 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1799 const float theta = acosf(vec[2]);
1800 const float r = tanf(theta * 0.5f);
1801 const float c = r / hypotf(vec[0], vec[1]);
1802 const float x = vec[0] * c / s->iflat_range[0] * s->input_mirror_modifier[0];
1803 const float y = vec[1] * c / s->iflat_range[1] * s->input_mirror_modifier[1];
1805 const float uf = (x + 1.f) * width / 2.f;
1806 const float vf = (y + 1.f) * height / 2.f;
1808 const int ui = floorf(uf);
1809 const int vi = floorf(vf);
1811 const int visible = isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
1813 *du = visible ? uf - ui : 0.f;
1814 *dv = visible ? vf - vi : 0.f;
1816 for (int i = 0; i < 4; i++) {
1817 for (int j = 0; j < 4; j++) {
1818 us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
1819 vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
1827 * Prepare data for processing equisolid output format.
1829 * @param ctx filter context
1831 * @return error code
1833 static int prepare_equisolid_out(AVFilterContext *ctx)
1835 V360Context *s = ctx->priv;
1837 s->flat_range[0] = sinf(s->h_fov * M_PI / 720.f);
1838 s->flat_range[1] = sinf(s->v_fov * M_PI / 720.f);
1844 * Calculate 3D coordinates on sphere for corresponding frame position in equisolid format.
1846 * @param s filter private context
1847 * @param i horizontal position on frame [0, width)
1848 * @param j vertical position on frame [0, height)
1849 * @param width frame width
1850 * @param height frame height
1851 * @param vec coordinates on sphere
1853 static int equisolid_to_xyz(const V360Context *s,
1854 int i, int j, int width, int height,
1857 const float x = ((2.f * i + 1.f) / width - 1.f) * s->flat_range[0];
1858 const float y = ((2.f * j + 1.f) / height - 1.f) * s->flat_range[1];
1859 const float r = hypotf(x, y);
1860 const float theta = asinf(r) * 2.f;
1861 const float sin_theta = sinf(theta);
1863 vec[0] = x / r * sin_theta;
1864 vec[1] = y / r * sin_theta;
1865 vec[2] = cosf(theta);
1867 normalize_vector(vec);
1873 * Prepare data for processing equisolid input format.
1875 * @param ctx filter context
1877 * @return error code
1879 static int prepare_equisolid_in(AVFilterContext *ctx)
1881 V360Context *s = ctx->priv;
1883 s->iflat_range[0] = sinf(FFMIN(s->ih_fov, 359.f) * M_PI / 720.f);
1884 s->iflat_range[1] = sinf(FFMIN(s->iv_fov, 359.f) * M_PI / 720.f);
1890 * Calculate frame position in equisolid format for corresponding 3D coordinates on sphere.
1892 * @param s filter private context
1893 * @param vec coordinates on sphere
1894 * @param width frame width
1895 * @param height frame height
1896 * @param us horizontal coordinates for interpolation window
1897 * @param vs vertical coordinates for interpolation window
1898 * @param du horizontal relative coordinate
1899 * @param dv vertical relative coordinate
1901 static int xyz_to_equisolid(const V360Context *s,
1902 const float *vec, int width, int height,
1903 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
1905 const float theta = acosf(vec[2]);
1906 const float r = sinf(theta * 0.5f);
1907 const float c = r / hypotf(vec[0], vec[1]);
1908 const float x = vec[0] * c / s->iflat_range[0] * s->input_mirror_modifier[0];
1909 const float y = vec[1] * c / s->iflat_range[1] * s->input_mirror_modifier[1];
1911 const float uf = (x + 1.f) * width / 2.f;
1912 const float vf = (y + 1.f) * height / 2.f;
1914 const int ui = floorf(uf);
1915 const int vi = floorf(vf);
1917 const int visible = isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
1919 *du = visible ? uf - ui : 0.f;
1920 *dv = visible ? vf - vi : 0.f;
1922 for (int i = 0; i < 4; i++) {
1923 for (int j = 0; j < 4; j++) {
1924 us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
1925 vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
1933 * Prepare data for processing orthographic output format.
1935 * @param ctx filter context
1937 * @return error code
1939 static int prepare_orthographic_out(AVFilterContext *ctx)
1941 V360Context *s = ctx->priv;
1943 s->flat_range[0] = sinf(FFMIN(s->h_fov, 180.f) * M_PI / 360.f);
1944 s->flat_range[1] = sinf(FFMIN(s->v_fov, 180.f) * M_PI / 360.f);
1950 * Calculate 3D coordinates on sphere for corresponding frame position in orthographic format.
1952 * @param s filter private context
1953 * @param i horizontal position on frame [0, width)
1954 * @param j vertical position on frame [0, height)
1955 * @param width frame width
1956 * @param height frame height
1957 * @param vec coordinates on sphere
1959 static int orthographic_to_xyz(const V360Context *s,
1960 int i, int j, int width, int height,
1963 const float x = ((2.f * i + 1.f) / width - 1.f) * s->flat_range[0];
1964 const float y = ((2.f * j + 1.f) / height - 1.f) * s->flat_range[1];
1965 const float r = hypotf(x, y);
1966 const float theta = asinf(r);
1970 vec[2] = cosf(theta);
1972 normalize_vector(vec);
1978 * Prepare data for processing orthographic input format.
1980 * @param ctx filter context
1982 * @return error code
1984 static int prepare_orthographic_in(AVFilterContext *ctx)
1986 V360Context *s = ctx->priv;
1988 s->iflat_range[0] = sinf(FFMIN(s->ih_fov, 180.f) * M_PI / 360.f);
1989 s->iflat_range[1] = sinf(FFMIN(s->iv_fov, 180.f) * M_PI / 360.f);
1995 * Calculate frame position in orthographic format for corresponding 3D coordinates on sphere.
1997 * @param s filter private context
1998 * @param vec coordinates on sphere
1999 * @param width frame width
2000 * @param height frame height
2001 * @param us horizontal coordinates for interpolation window
2002 * @param vs vertical coordinates for interpolation window
2003 * @param du horizontal relative coordinate
2004 * @param dv vertical relative coordinate
2006 static int xyz_to_orthographic(const V360Context *s,
2007 const float *vec, int width, int height,
2008 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2010 const float theta = acosf(vec[2]);
2011 const float r = sinf(theta);
2012 const float c = r / hypotf(vec[0], vec[1]);
2013 const float x = vec[0] * c / s->iflat_range[0] * s->input_mirror_modifier[0];
2014 const float y = vec[1] * c / s->iflat_range[1] * s->input_mirror_modifier[1];
2016 const float uf = (x + 1.f) * width / 2.f;
2017 const float vf = (y + 1.f) * height / 2.f;
2019 const int ui = floorf(uf);
2020 const int vi = floorf(vf);
2022 const int visible = vec[2] >= 0.f && isfinite(x) && isfinite(y) && vi >= 0 && vi < height && ui >= 0 && ui < width;
2024 *du = visible ? uf - ui : 0.f;
2025 *dv = visible ? vf - vi : 0.f;
2027 for (int i = 0; i < 4; i++) {
2028 for (int j = 0; j < 4; j++) {
2029 us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
2030 vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
2038 * Calculate frame position in equirectangular format for corresponding 3D coordinates on sphere.
2040 * @param s filter private context
2041 * @param vec coordinates on sphere
2042 * @param width frame width
2043 * @param height frame height
2044 * @param us horizontal coordinates for interpolation window
2045 * @param vs vertical coordinates for interpolation window
2046 * @param du horizontal relative coordinate
2047 * @param dv vertical relative coordinate
2049 static int xyz_to_equirect(const V360Context *s,
2050 const float *vec, int width, int height,
2051 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2053 const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0];
2054 const float theta = asinf(vec[1]) * s->input_mirror_modifier[1];
2056 const float uf = (phi / M_PI + 1.f) * width / 2.f;
2057 const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
2059 const int ui = floorf(uf);
2060 const int vi = floorf(vf);
2065 for (int i = 0; i < 4; i++) {
2066 for (int j = 0; j < 4; j++) {
2067 us[i][j] = ereflectx(ui + j - 1, vi + i - 1, width, height);
2068 vs[i][j] = reflecty(vi + i - 1, height);
2076 * Calculate frame position in half equirectangular format for corresponding 3D coordinates on sphere.
2078 * @param s filter private context
2079 * @param vec coordinates on sphere
2080 * @param width frame width
2081 * @param height frame height
2082 * @param us horizontal coordinates for interpolation window
2083 * @param vs vertical coordinates for interpolation window
2084 * @param du horizontal relative coordinate
2085 * @param dv vertical relative coordinate
2087 static int xyz_to_hequirect(const V360Context *s,
2088 const float *vec, int width, int height,
2089 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2091 const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0];
2092 const float theta = asinf(vec[1]) * s->input_mirror_modifier[1];
2094 const float uf = (phi / M_PI_2 + 1.f) * width / 2.f;
2095 const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
2097 const int ui = floorf(uf);
2098 const int vi = floorf(vf);
2100 const int visible = phi >= -M_PI_2 && phi <= M_PI_2;
2105 for (int i = 0; i < 4; i++) {
2106 for (int j = 0; j < 4; j++) {
2107 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2108 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2116 * Prepare data for processing flat input format.
2118 * @param ctx filter context
2120 * @return error code
2122 static int prepare_flat_in(AVFilterContext *ctx)
2124 V360Context *s = ctx->priv;
2126 s->iflat_range[0] = tanf(0.5f * s->ih_fov * M_PI / 180.f);
2127 s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
2133 * Calculate frame position in flat format for corresponding 3D coordinates on sphere.
2135 * @param s filter private context
2136 * @param vec coordinates on sphere
2137 * @param width frame width
2138 * @param height frame height
2139 * @param us horizontal coordinates for interpolation window
2140 * @param vs vertical coordinates for interpolation window
2141 * @param du horizontal relative coordinate
2142 * @param dv vertical relative coordinate
2144 static int xyz_to_flat(const V360Context *s,
2145 const float *vec, int width, int height,
2146 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2148 const float theta = acosf(vec[2]);
2149 const float r = tanf(theta);
2150 const float rr = fabsf(r) < 1e+6f ? r : hypotf(width, height);
2151 const float zf = vec[2];
2152 const float h = hypotf(vec[0], vec[1]);
2153 const float c = h <= 1e-6f ? 1.f : rr / h;
2154 float uf = vec[0] * c / s->iflat_range[0] * s->input_mirror_modifier[0];
2155 float vf = vec[1] * c / s->iflat_range[1] * s->input_mirror_modifier[1];
2156 int visible, ui, vi;
2158 uf = zf >= 0.f ? (uf + 1.f) * width / 2.f : 0.f;
2159 vf = zf >= 0.f ? (vf + 1.f) * height / 2.f : 0.f;
2164 visible = vi >= 0 && vi < height && ui >= 0 && ui < width && zf >= 0.f;
2169 for (int i = 0; i < 4; i++) {
2170 for (int j = 0; j < 4; j++) {
2171 us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
2172 vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
2180 * Calculate frame position in mercator format for corresponding 3D coordinates on sphere.
2182 * @param s filter private context
2183 * @param vec coordinates on sphere
2184 * @param width frame width
2185 * @param height frame height
2186 * @param us horizontal coordinates for interpolation window
2187 * @param vs vertical coordinates for interpolation window
2188 * @param du horizontal relative coordinate
2189 * @param dv vertical relative coordinate
2191 static int xyz_to_mercator(const V360Context *s,
2192 const float *vec, int width, int height,
2193 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2195 const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0];
2196 const float theta = vec[1] * s->input_mirror_modifier[1];
2198 const float uf = (phi / M_PI + 1.f) * width / 2.f;
2199 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;
2201 const int ui = floorf(uf);
2202 const int vi = floorf(vf);
2207 for (int i = 0; i < 4; i++) {
2208 for (int j = 0; j < 4; j++) {
2209 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2210 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2218 * Calculate 3D coordinates on sphere for corresponding frame position in mercator format.
2220 * @param s filter private context
2221 * @param i horizontal position on frame [0, width)
2222 * @param j vertical position on frame [0, height)
2223 * @param width frame width
2224 * @param height frame height
2225 * @param vec coordinates on sphere
2227 static int mercator_to_xyz(const V360Context *s,
2228 int i, int j, int width, int height,
2231 const float phi = ((2.f * i + 1.f) / width - 1.f) * M_PI + M_PI_2;
2232 const float y = ((2.f * j + 1.f) / height - 1.f) * M_PI;
2233 const float div = expf(2.f * y) + 1.f;
2235 const float sin_phi = sinf(phi);
2236 const float cos_phi = cosf(phi);
2237 const float sin_theta = 2.f * expf(y) / div;
2238 const float cos_theta = (expf(2.f * y) - 1.f) / div;
2240 vec[0] = -sin_theta * cos_phi;
2242 vec[2] = sin_theta * sin_phi;
2248 * Calculate frame position in ball format for corresponding 3D coordinates on sphere.
2250 * @param s filter private context
2251 * @param vec coordinates on sphere
2252 * @param width frame width
2253 * @param height frame height
2254 * @param us horizontal coordinates for interpolation window
2255 * @param vs vertical coordinates for interpolation window
2256 * @param du horizontal relative coordinate
2257 * @param dv vertical relative coordinate
2259 static int xyz_to_ball(const V360Context *s,
2260 const float *vec, int width, int height,
2261 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2263 const float l = hypotf(vec[0], vec[1]);
2264 const float r = sqrtf(1.f - vec[2]) / M_SQRT2;
2266 const float uf = (1.f + r * vec[0] * s->input_mirror_modifier[0] / (l > 0.f ? l : 1.f)) * width * 0.5f;
2267 const float vf = (1.f + r * vec[1] * s->input_mirror_modifier[1] / (l > 0.f ? l : 1.f)) * height * 0.5f;
2269 const int ui = floorf(uf);
2270 const int vi = floorf(vf);
2275 for (int i = 0; i < 4; i++) {
2276 for (int j = 0; j < 4; j++) {
2277 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2278 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2286 * Calculate 3D coordinates on sphere for corresponding frame position in ball format.
2288 * @param s filter private context
2289 * @param i horizontal position on frame [0, width)
2290 * @param j vertical position on frame [0, height)
2291 * @param width frame width
2292 * @param height frame height
2293 * @param vec coordinates on sphere
2295 static int ball_to_xyz(const V360Context *s,
2296 int i, int j, int width, int height,
2299 const float x = (2.f * i + 1.f) / width - 1.f;
2300 const float y = (2.f * j + 1.f) / height - 1.f;
2301 const float l = hypotf(x, y);
2304 const float z = 2.f * l * sqrtf(1.f - l * l);
2306 vec[0] = z * x / (l > 0.f ? l : 1.f);
2307 vec[1] = z * y / (l > 0.f ? l : 1.f);
2308 vec[2] = 1.f - 2.f * l * l;
2320 * Calculate 3D coordinates on sphere for corresponding frame position in hammer format.
2322 * @param s filter private context
2323 * @param i horizontal position on frame [0, width)
2324 * @param j vertical position on frame [0, height)
2325 * @param width frame width
2326 * @param height frame height
2327 * @param vec coordinates on sphere
2329 static int hammer_to_xyz(const V360Context *s,
2330 int i, int j, int width, int height,
2333 const float x = ((2.f * i + 1.f) / width - 1.f);
2334 const float y = ((2.f * j + 1.f) / height - 1.f);
2336 const float xx = x * x;
2337 const float yy = y * y;
2339 const float z = sqrtf(1.f - xx * 0.5f - yy * 0.5f);
2341 const float a = M_SQRT2 * x * z;
2342 const float b = 2.f * z * z - 1.f;
2344 const float aa = a * a;
2345 const float bb = b * b;
2347 const float w = sqrtf(1.f - 2.f * yy * z * z);
2349 vec[0] = w * 2.f * a * b / (aa + bb);
2350 vec[1] = M_SQRT2 * y * z;
2351 vec[2] = w * (bb - aa) / (aa + bb);
2353 normalize_vector(vec);
2359 * Calculate frame position in hammer format for corresponding 3D coordinates on sphere.
2361 * @param s filter private context
2362 * @param vec coordinates on sphere
2363 * @param width frame width
2364 * @param height frame height
2365 * @param us horizontal coordinates for interpolation window
2366 * @param vs vertical coordinates for interpolation window
2367 * @param du horizontal relative coordinate
2368 * @param dv vertical relative coordinate
2370 static int xyz_to_hammer(const V360Context *s,
2371 const float *vec, int width, int height,
2372 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2374 const float theta = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0];
2376 const float z = sqrtf(1.f + sqrtf(1.f - vec[1] * vec[1]) * cosf(theta * 0.5f));
2377 const float x = sqrtf(1.f - vec[1] * vec[1]) * sinf(theta * 0.5f) / z;
2378 const float y = vec[1] / z * s->input_mirror_modifier[1];
2380 const float uf = (x + 1.f) * width / 2.f;
2381 const float vf = (y + 1.f) * height / 2.f;
2383 const int ui = floorf(uf);
2384 const int vi = floorf(vf);
2389 for (int i = 0; i < 4; i++) {
2390 for (int j = 0; j < 4; j++) {
2391 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2392 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2400 * Calculate 3D coordinates on sphere for corresponding frame position in sinusoidal format.
2402 * @param s filter private context
2403 * @param i horizontal position on frame [0, width)
2404 * @param j vertical position on frame [0, height)
2405 * @param width frame width
2406 * @param height frame height
2407 * @param vec coordinates on sphere
2409 static int sinusoidal_to_xyz(const V360Context *s,
2410 int i, int j, int width, int height,
2413 const float theta = ((2.f * j + 1.f) / height - 1.f) * M_PI_2;
2414 const float phi = ((2.f * i + 1.f) / width - 1.f) * M_PI / cosf(theta);
2416 const float sin_phi = sinf(phi);
2417 const float cos_phi = cosf(phi);
2418 const float sin_theta = sinf(theta);
2419 const float cos_theta = cosf(theta);
2421 vec[0] = cos_theta * sin_phi;
2423 vec[2] = cos_theta * cos_phi;
2425 normalize_vector(vec);
2431 * Calculate frame position in sinusoidal format for corresponding 3D coordinates on sphere.
2433 * @param s filter private context
2434 * @param vec coordinates on sphere
2435 * @param width frame width
2436 * @param height frame height
2437 * @param us horizontal coordinates for interpolation window
2438 * @param vs vertical coordinates for interpolation window
2439 * @param du horizontal relative coordinate
2440 * @param dv vertical relative coordinate
2442 static int xyz_to_sinusoidal(const V360Context *s,
2443 const float *vec, int width, int height,
2444 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2446 const float theta = asinf(vec[1]) * s->input_mirror_modifier[1];
2447 const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0] * cosf(theta);
2449 const float uf = (phi / M_PI + 1.f) * width / 2.f;
2450 const float vf = (theta / M_PI_2 + 1.f) * height / 2.f;
2452 const int ui = floorf(uf);
2453 const int vi = floorf(vf);
2458 for (int i = 0; i < 4; i++) {
2459 for (int j = 0; j < 4; j++) {
2460 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2461 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2469 * Prepare data for processing equi-angular cubemap input format.
2471 * @param ctx filter context
2473 * @return error code
2475 static int prepare_eac_in(AVFilterContext *ctx)
2477 V360Context *s = ctx->priv;
2479 if (s->ih_flip && s->iv_flip) {
2480 s->in_cubemap_face_order[RIGHT] = BOTTOM_LEFT;
2481 s->in_cubemap_face_order[LEFT] = BOTTOM_RIGHT;
2482 s->in_cubemap_face_order[UP] = TOP_LEFT;
2483 s->in_cubemap_face_order[DOWN] = TOP_RIGHT;
2484 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
2485 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
2486 } else if (s->ih_flip) {
2487 s->in_cubemap_face_order[RIGHT] = TOP_LEFT;
2488 s->in_cubemap_face_order[LEFT] = TOP_RIGHT;
2489 s->in_cubemap_face_order[UP] = BOTTOM_LEFT;
2490 s->in_cubemap_face_order[DOWN] = BOTTOM_RIGHT;
2491 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
2492 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
2493 } else if (s->iv_flip) {
2494 s->in_cubemap_face_order[RIGHT] = BOTTOM_RIGHT;
2495 s->in_cubemap_face_order[LEFT] = BOTTOM_LEFT;
2496 s->in_cubemap_face_order[UP] = TOP_RIGHT;
2497 s->in_cubemap_face_order[DOWN] = TOP_LEFT;
2498 s->in_cubemap_face_order[FRONT] = BOTTOM_MIDDLE;
2499 s->in_cubemap_face_order[BACK] = TOP_MIDDLE;
2501 s->in_cubemap_face_order[RIGHT] = TOP_RIGHT;
2502 s->in_cubemap_face_order[LEFT] = TOP_LEFT;
2503 s->in_cubemap_face_order[UP] = BOTTOM_RIGHT;
2504 s->in_cubemap_face_order[DOWN] = BOTTOM_LEFT;
2505 s->in_cubemap_face_order[FRONT] = TOP_MIDDLE;
2506 s->in_cubemap_face_order[BACK] = BOTTOM_MIDDLE;
2510 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_270;
2511 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_90;
2512 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_270;
2513 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_0;
2514 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_0;
2515 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_0;
2517 s->in_cubemap_face_rotation[TOP_LEFT] = ROT_0;
2518 s->in_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
2519 s->in_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
2520 s->in_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
2521 s->in_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
2522 s->in_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
2529 * Prepare data for processing equi-angular cubemap output format.
2531 * @param ctx filter context
2533 * @return error code
2535 static int prepare_eac_out(AVFilterContext *ctx)
2537 V360Context *s = ctx->priv;
2539 s->out_cubemap_direction_order[TOP_LEFT] = LEFT;
2540 s->out_cubemap_direction_order[TOP_MIDDLE] = FRONT;
2541 s->out_cubemap_direction_order[TOP_RIGHT] = RIGHT;
2542 s->out_cubemap_direction_order[BOTTOM_LEFT] = DOWN;
2543 s->out_cubemap_direction_order[BOTTOM_MIDDLE] = BACK;
2544 s->out_cubemap_direction_order[BOTTOM_RIGHT] = UP;
2546 s->out_cubemap_face_rotation[TOP_LEFT] = ROT_0;
2547 s->out_cubemap_face_rotation[TOP_MIDDLE] = ROT_0;
2548 s->out_cubemap_face_rotation[TOP_RIGHT] = ROT_0;
2549 s->out_cubemap_face_rotation[BOTTOM_LEFT] = ROT_270;
2550 s->out_cubemap_face_rotation[BOTTOM_MIDDLE] = ROT_90;
2551 s->out_cubemap_face_rotation[BOTTOM_RIGHT] = ROT_270;
2557 * Calculate 3D coordinates on sphere for corresponding frame position in equi-angular cubemap format.
2559 * @param s filter private context
2560 * @param i horizontal position on frame [0, width)
2561 * @param j vertical position on frame [0, height)
2562 * @param width frame width
2563 * @param height frame height
2564 * @param vec coordinates on sphere
2566 static int eac_to_xyz(const V360Context *s,
2567 int i, int j, int width, int height,
2570 const float pixel_pad = 2;
2571 const float u_pad = pixel_pad / width;
2572 const float v_pad = pixel_pad / height;
2574 int u_face, v_face, face;
2576 float l_x, l_y, l_z;
2578 float uf = (i + 0.5f) / width;
2579 float vf = (j + 0.5f) / height;
2581 // EAC has 2-pixel padding on faces except between faces on the same row
2582 // Padding pixels seems not to be stretched with tangent as regular pixels
2583 // Formulas below approximate original padding as close as I could get experimentally
2585 // Horizontal padding
2586 uf = 3.f * (uf - u_pad) / (1.f - 2.f * u_pad);
2590 } else if (uf >= 3.f) {
2594 u_face = floorf(uf);
2595 uf = fmodf(uf, 1.f) - 0.5f;
2599 v_face = floorf(vf * 2.f);
2600 vf = (vf - v_pad - 0.5f * v_face) / (0.5f - 2.f * v_pad) - 0.5f;
2602 if (uf >= -0.5f && uf < 0.5f) {
2603 uf = tanf(M_PI_2 * uf);
2607 if (vf >= -0.5f && vf < 0.5f) {
2608 vf = tanf(M_PI_2 * vf);
2613 face = u_face + 3 * v_face;
2654 normalize_vector(vec);
2660 * Calculate frame position in equi-angular cubemap format for corresponding 3D coordinates on sphere.
2662 * @param s filter private context
2663 * @param vec coordinates on sphere
2664 * @param width frame width
2665 * @param height frame height
2666 * @param us horizontal coordinates for interpolation window
2667 * @param vs vertical coordinates for interpolation window
2668 * @param du horizontal relative coordinate
2669 * @param dv vertical relative coordinate
2671 static int xyz_to_eac(const V360Context *s,
2672 const float *vec, int width, int height,
2673 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2675 const float pixel_pad = 2;
2676 const float u_pad = pixel_pad / width;
2677 const float v_pad = pixel_pad / height;
2681 int direction, face;
2684 xyz_to_cube(s, vec, &uf, &vf, &direction);
2686 face = s->in_cubemap_face_order[direction];
2690 uf = M_2_PI * atanf(uf) + 0.5f;
2691 vf = M_2_PI * atanf(vf) + 0.5f;
2693 // These formulas are inversed from eac_to_xyz ones
2694 uf = (uf + u_face) * (1.f - 2.f * u_pad) / 3.f + u_pad;
2695 vf = vf * (0.5f - 2.f * v_pad) + v_pad + 0.5f * v_face;
2709 for (int i = 0; i < 4; i++) {
2710 for (int j = 0; j < 4; j++) {
2711 us[i][j] = av_clip(ui + j - 1, 0, width - 1);
2712 vs[i][j] = av_clip(vi + i - 1, 0, height - 1);
2720 * Prepare data for processing flat output format.
2722 * @param ctx filter context
2724 * @return error code
2726 static int prepare_flat_out(AVFilterContext *ctx)
2728 V360Context *s = ctx->priv;
2730 s->flat_range[0] = tanf(0.5f * s->h_fov * M_PI / 180.f);
2731 s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2737 * Calculate 3D coordinates on sphere for corresponding frame position in flat format.
2739 * @param s filter private context
2740 * @param i horizontal position on frame [0, width)
2741 * @param j vertical position on frame [0, height)
2742 * @param width frame width
2743 * @param height frame height
2744 * @param vec coordinates on sphere
2746 static int flat_to_xyz(const V360Context *s,
2747 int i, int j, int width, int height,
2750 const float l_x = s->flat_range[0] * ((2.f * i + 0.5f) / width - 1.f);
2751 const float l_y = s->flat_range[1] * ((2.f * j + 0.5f) / height - 1.f);
2757 normalize_vector(vec);
2763 * Prepare data for processing fisheye output format.
2765 * @param ctx filter context
2767 * @return error code
2769 static int prepare_fisheye_out(AVFilterContext *ctx)
2771 V360Context *s = ctx->priv;
2773 s->flat_range[0] = s->h_fov / 180.f;
2774 s->flat_range[1] = s->v_fov / 180.f;
2780 * Calculate 3D coordinates on sphere for corresponding frame position in fisheye format.
2782 * @param s filter private context
2783 * @param i horizontal position on frame [0, width)
2784 * @param j vertical position on frame [0, height)
2785 * @param width frame width
2786 * @param height frame height
2787 * @param vec coordinates on sphere
2789 static int fisheye_to_xyz(const V360Context *s,
2790 int i, int j, int width, int height,
2793 const float uf = s->flat_range[0] * ((2.f * i) / width - 1.f);
2794 const float vf = s->flat_range[1] * ((2.f * j + 1.f) / height - 1.f);
2796 const float phi = atan2f(vf, uf);
2797 const float theta = M_PI_2 * (1.f - hypotf(uf, vf));
2799 const float sin_phi = sinf(phi);
2800 const float cos_phi = cosf(phi);
2801 const float sin_theta = sinf(theta);
2802 const float cos_theta = cosf(theta);
2804 vec[0] = cos_theta * cos_phi;
2805 vec[1] = cos_theta * sin_phi;
2808 normalize_vector(vec);
2814 * Prepare data for processing fisheye input format.
2816 * @param ctx filter context
2818 * @return error code
2820 static int prepare_fisheye_in(AVFilterContext *ctx)
2822 V360Context *s = ctx->priv;
2824 s->iflat_range[0] = s->ih_fov / 180.f;
2825 s->iflat_range[1] = s->iv_fov / 180.f;
2831 * Calculate frame position in fisheye format for corresponding 3D coordinates on sphere.
2833 * @param s filter private context
2834 * @param vec coordinates on sphere
2835 * @param width frame width
2836 * @param height frame height
2837 * @param us horizontal coordinates for interpolation window
2838 * @param vs vertical coordinates for interpolation window
2839 * @param du horizontal relative coordinate
2840 * @param dv vertical relative coordinate
2842 static int xyz_to_fisheye(const V360Context *s,
2843 const float *vec, int width, int height,
2844 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2846 const float h = hypotf(vec[0], vec[1]);
2847 const float lh = h > 0.f ? h : 1.f;
2848 const float phi = atan2f(h, vec[2]) / M_PI;
2850 float uf = vec[0] / lh * phi * s->input_mirror_modifier[0] / s->iflat_range[0];
2851 float vf = vec[1] / lh * phi * s->input_mirror_modifier[1] / s->iflat_range[1];
2853 const int visible = hypotf(uf, vf) <= 0.5f;
2856 uf = (uf + 0.5f) * width;
2857 vf = (vf + 0.5f) * height;
2862 *du = visible ? uf - ui : 0.f;
2863 *dv = visible ? vf - vi : 0.f;
2865 for (int i = 0; i < 4; i++) {
2866 for (int j = 0; j < 4; j++) {
2867 us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
2868 vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
2876 * Calculate 3D coordinates on sphere for corresponding frame position in pannini format.
2878 * @param s filter private context
2879 * @param i horizontal position on frame [0, width)
2880 * @param j vertical position on frame [0, height)
2881 * @param width frame width
2882 * @param height frame height
2883 * @param vec coordinates on sphere
2885 static int pannini_to_xyz(const V360Context *s,
2886 int i, int j, int width, int height,
2889 const float uf = ((2.f * i + 1.f) / width - 1.f);
2890 const float vf = ((2.f * j + 1.f) / height - 1.f);
2892 const float d = s->h_fov;
2893 const float k = uf * uf / ((d + 1.f) * (d + 1.f));
2894 const float dscr = k * k * d * d - (k + 1.f) * (k * d * d - 1.f);
2895 const float clon = (-k * d + sqrtf(dscr)) / (k + 1.f);
2896 const float S = (d + 1.f) / (d + clon);
2897 const float lon = atan2f(uf, S * clon);
2898 const float lat = atan2f(vf, S);
2900 vec[0] = sinf(lon) * cosf(lat);
2902 vec[2] = cosf(lon) * cosf(lat);
2904 normalize_vector(vec);
2910 * Calculate frame position in pannini format for corresponding 3D coordinates on sphere.
2912 * @param s filter private context
2913 * @param vec coordinates on sphere
2914 * @param width frame width
2915 * @param height frame height
2916 * @param us horizontal coordinates for interpolation window
2917 * @param vs vertical coordinates for interpolation window
2918 * @param du horizontal relative coordinate
2919 * @param dv vertical relative coordinate
2921 static int xyz_to_pannini(const V360Context *s,
2922 const float *vec, int width, int height,
2923 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
2925 const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0];
2926 const float theta = asinf(vec[1]) * s->input_mirror_modifier[1];
2928 const float d = s->ih_fov;
2929 const float S = (d + 1.f) / (d + cosf(phi));
2931 const float x = S * sinf(phi);
2932 const float y = S * tanf(theta);
2934 const float uf = (x + 1.f) * width / 2.f;
2935 const float vf = (y + 1.f) * height / 2.f;
2937 const int ui = floorf(uf);
2938 const int vi = floorf(vf);
2940 const int visible = vi >= 0 && vi < height && ui >= 0 && ui < width && vec[2] >= 0.f;
2945 for (int i = 0; i < 4; i++) {
2946 for (int j = 0; j < 4; j++) {
2947 us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
2948 vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
2956 * Prepare data for processing cylindrical output format.
2958 * @param ctx filter context
2960 * @return error code
2962 static int prepare_cylindrical_out(AVFilterContext *ctx)
2964 V360Context *s = ctx->priv;
2966 s->flat_range[0] = M_PI * s->h_fov / 360.f;
2967 s->flat_range[1] = tanf(0.5f * s->v_fov * M_PI / 180.f);
2973 * Calculate 3D coordinates on sphere for corresponding frame position in cylindrical format.
2975 * @param s filter private context
2976 * @param i horizontal position on frame [0, width)
2977 * @param j vertical position on frame [0, height)
2978 * @param width frame width
2979 * @param height frame height
2980 * @param vec coordinates on sphere
2982 static int cylindrical_to_xyz(const V360Context *s,
2983 int i, int j, int width, int height,
2986 const float uf = s->flat_range[0] * ((2.f * i + 1.f) / width - 1.f);
2987 const float vf = s->flat_range[1] * ((2.f * j + 1.f) / height - 1.f);
2989 const float phi = uf;
2990 const float theta = atanf(vf);
2992 const float sin_phi = sinf(phi);
2993 const float cos_phi = cosf(phi);
2994 const float sin_theta = sinf(theta);
2995 const float cos_theta = cosf(theta);
2997 vec[0] = cos_theta * sin_phi;
2999 vec[2] = cos_theta * cos_phi;
3001 normalize_vector(vec);
3007 * Prepare data for processing cylindrical input format.
3009 * @param ctx filter context
3011 * @return error code
3013 static int prepare_cylindrical_in(AVFilterContext *ctx)
3015 V360Context *s = ctx->priv;
3017 s->iflat_range[0] = M_PI * s->ih_fov / 360.f;
3018 s->iflat_range[1] = tanf(0.5f * s->iv_fov * M_PI / 180.f);
3024 * Calculate frame position in cylindrical format for corresponding 3D coordinates on sphere.
3026 * @param s filter private context
3027 * @param vec coordinates on sphere
3028 * @param width frame width
3029 * @param height frame height
3030 * @param us horizontal coordinates for interpolation window
3031 * @param vs vertical coordinates for interpolation window
3032 * @param du horizontal relative coordinate
3033 * @param dv vertical relative coordinate
3035 static int xyz_to_cylindrical(const V360Context *s,
3036 const float *vec, int width, int height,
3037 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3039 const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0] / s->iflat_range[0];
3040 const float theta = asinf(vec[1]) * s->input_mirror_modifier[1];
3042 const float uf = (phi + 1.f) * (width - 1) / 2.f;
3043 const float vf = (tanf(theta) / s->iflat_range[1] + 1.f) * height / 2.f;
3045 const int ui = floorf(uf);
3046 const int vi = floorf(vf);
3048 const int visible = vi >= 0 && vi < height && ui >= 0 && ui < width &&
3049 theta <= M_PI * s->iv_fov / 180.f &&
3050 theta >= -M_PI * s->iv_fov / 180.f;
3055 for (int i = 0; i < 4; i++) {
3056 for (int j = 0; j < 4; j++) {
3057 us[i][j] = visible ? av_clip(ui + j - 1, 0, width - 1) : 0;
3058 vs[i][j] = visible ? av_clip(vi + i - 1, 0, height - 1) : 0;
3066 * Calculate 3D coordinates on sphere for corresponding frame position in perspective format.
3068 * @param s filter private context
3069 * @param i horizontal position on frame [0, width)
3070 * @param j vertical position on frame [0, height)
3071 * @param width frame width
3072 * @param height frame height
3073 * @param vec coordinates on sphere
3075 static int perspective_to_xyz(const V360Context *s,
3076 int i, int j, int width, int height,
3079 const float uf = ((2.f * i + 1.f) / width - 1.f);
3080 const float vf = ((2.f * j + 1.f) / height - 1.f);
3081 const float rh = hypotf(uf, vf);
3082 const float sinzz = 1.f - rh * rh;
3083 const float h = 1.f + s->v_fov;
3084 const float sinz = (h - sqrtf(sinzz)) / (h / rh + rh / h);
3085 const float sinz2 = sinz * sinz;
3088 const float cosz = sqrtf(1.f - sinz2);
3090 const float theta = asinf(cosz);
3091 const float phi = atan2f(uf, vf);
3093 const float sin_phi = sinf(phi);
3094 const float cos_phi = cosf(phi);
3095 const float sin_theta = sinf(theta);
3096 const float cos_theta = cosf(theta);
3098 vec[0] = cos_theta * sin_phi;
3100 vec[2] = cos_theta * cos_phi;
3108 normalize_vector(vec);
3113 * Calculate 3D coordinates on sphere for corresponding frame position in tetrahedron format.
3115 * @param s filter private context
3116 * @param i horizontal position on frame [0, width)
3117 * @param j vertical position on frame [0, height)
3118 * @param width frame width
3119 * @param height frame height
3120 * @param vec coordinates on sphere
3122 static int tetrahedron_to_xyz(const V360Context *s,
3123 int i, int j, int width, int height,
3126 const float uf = (float)i / width;
3127 const float vf = (float)j / height;
3129 vec[0] = uf < 0.5f ? uf * 4.f - 1.f : 3.f - uf * 4.f;
3130 vec[1] = 1.f - vf * 2.f;
3131 vec[2] = 2.f * fabsf(1.f - fabsf(1.f - uf * 2.f + vf)) - 1.f;
3133 normalize_vector(vec);
3139 * Calculate frame position in tetrahedron format for corresponding 3D coordinates on sphere.
3141 * @param s filter private context
3142 * @param vec coordinates on sphere
3143 * @param width frame width
3144 * @param height frame height
3145 * @param us horizontal coordinates for interpolation window
3146 * @param vs vertical coordinates for interpolation window
3147 * @param du horizontal relative coordinate
3148 * @param dv vertical relative coordinate
3150 static int xyz_to_tetrahedron(const V360Context *s,
3151 const float *vec, int width, int height,
3152 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3154 const float d0 = vec[0] * 1.f + vec[1] * 1.f + vec[2] *-1.f;
3155 const float d1 = vec[0] *-1.f + vec[1] *-1.f + vec[2] *-1.f;
3156 const float d2 = vec[0] * 1.f + vec[1] *-1.f + vec[2] * 1.f;
3157 const float d3 = vec[0] *-1.f + vec[1] * 1.f + vec[2] * 1.f;
3158 const float d = FFMAX(d0, FFMAX3(d1, d2, d3));
3160 float uf, vf, x, y, z;
3167 vf = 0.5f - y * 0.5f * s->input_mirror_modifier[1];
3169 if ((x + y >= 0.f && y + z >= 0.f && -z - x <= 0.f) ||
3170 (x + y <= 0.f && -y + z >= 0.f && z - x >= 0.f)) {
3171 uf = 0.25f * x * s->input_mirror_modifier[0] + 0.25f;
3173 uf = 0.75f - 0.25f * x * s->input_mirror_modifier[0];
3185 for (int i = 0; i < 4; i++) {
3186 for (int j = 0; j < 4; j++) {
3187 us[i][j] = reflectx(ui + j - 1, vi + i - 1, width, height);
3188 vs[i][j] = reflecty(vi + i - 1, height);
3196 * Calculate 3D coordinates on sphere for corresponding frame position in dual fisheye format.
3198 * @param s filter private context
3199 * @param i horizontal position on frame [0, width)
3200 * @param j vertical position on frame [0, height)
3201 * @param width frame width
3202 * @param height frame height
3203 * @param vec coordinates on sphere
3205 static int dfisheye_to_xyz(const V360Context *s,
3206 int i, int j, int width, int height,
3209 const float ew = width / 2.f;
3210 const float eh = height;
3212 const int ei = i >= ew ? i - ew : i;
3213 const float m = i >= ew ? 1.f : -1.f;
3215 const float uf = s->flat_range[0] * ((2.f * ei) / ew - 1.f);
3216 const float vf = s->flat_range[1] * ((2.f * j + 1.f) / eh - 1.f);
3218 const float h = hypotf(uf, vf);
3219 const float lh = h > 0.f ? h : 1.f;
3220 const float theta = m * M_PI_2 * (1.f - h);
3222 const float sin_theta = sinf(theta);
3223 const float cos_theta = cosf(theta);
3225 vec[0] = cos_theta * m * uf / lh;
3226 vec[1] = cos_theta * vf / lh;
3229 normalize_vector(vec);
3235 * Calculate frame position in dual fisheye format for corresponding 3D coordinates on sphere.
3237 * @param s filter private context
3238 * @param vec coordinates on sphere
3239 * @param width frame width
3240 * @param height frame height
3241 * @param us horizontal coordinates for interpolation window
3242 * @param vs vertical coordinates for interpolation window
3243 * @param du horizontal relative coordinate
3244 * @param dv vertical relative coordinate
3246 static int xyz_to_dfisheye(const V360Context *s,
3247 const float *vec, int width, int height,
3248 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3250 const float ew = width / 2.f;
3251 const float eh = height;
3253 const float h = hypotf(vec[0], vec[1]);
3254 const float lh = h > 0.f ? h : 1.f;
3255 const float theta = acosf(fabsf(vec[2])) / M_PI;
3257 float uf = (theta * (vec[0] / lh) * s->input_mirror_modifier[0] / s->iflat_range[0] + 0.5f) * ew;
3258 float vf = (theta * (vec[1] / lh) * s->input_mirror_modifier[1] / s->iflat_range[1] + 0.5f) * eh;
3263 if (vec[2] >= 0.f) {
3264 u_shift = ceilf(ew);
3276 for (int i = 0; i < 4; i++) {
3277 for (int j = 0; j < 4; j++) {
3278 us[i][j] = av_clip(u_shift + ui + j - 1, 0, width - 1);
3279 vs[i][j] = av_clip( vi + i - 1, 0, height - 1);
3287 * Calculate 3D coordinates on sphere for corresponding frame position in barrel facebook's format.
3289 * @param s filter private context
3290 * @param i horizontal position on frame [0, width)
3291 * @param j vertical position on frame [0, height)
3292 * @param width frame width
3293 * @param height frame height
3294 * @param vec coordinates on sphere
3296 static int barrel_to_xyz(const V360Context *s,
3297 int i, int j, int width, int height,
3300 const float scale = 0.99f;
3301 float l_x, l_y, l_z;
3303 if (i < 4 * width / 5) {
3304 const float theta_range = M_PI_4;
3306 const int ew = 4 * width / 5;
3307 const int eh = height;
3309 const float phi = ((2.f * i) / ew - 1.f) * M_PI / scale;
3310 const float theta = ((2.f * j) / eh - 1.f) * theta_range / scale;
3312 const float sin_phi = sinf(phi);
3313 const float cos_phi = cosf(phi);
3314 const float sin_theta = sinf(theta);
3315 const float cos_theta = cosf(theta);
3317 l_x = cos_theta * sin_phi;
3319 l_z = cos_theta * cos_phi;
3321 const int ew = width / 5;
3322 const int eh = height / 2;
3327 uf = 2.f * (i - 4 * ew) / ew - 1.f;
3328 vf = 2.f * (j ) / eh - 1.f;
3337 uf = 2.f * (i - 4 * ew) / ew - 1.f;
3338 vf = 2.f * (j - eh) / eh - 1.f;
3353 normalize_vector(vec);
3359 * Calculate frame position in barrel facebook's format for corresponding 3D coordinates on sphere.
3361 * @param s filter private context
3362 * @param vec coordinates on sphere
3363 * @param width frame width
3364 * @param height frame height
3365 * @param us horizontal coordinates for interpolation window
3366 * @param vs vertical coordinates for interpolation window
3367 * @param du horizontal relative coordinate
3368 * @param dv vertical relative coordinate
3370 static int xyz_to_barrel(const V360Context *s,
3371 const float *vec, int width, int height,
3372 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3374 const float scale = 0.99f;
3376 const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0];
3377 const float theta = asinf(vec[1]) * s->input_mirror_modifier[1];
3378 const float theta_range = M_PI_4;
3381 int u_shift, v_shift;
3385 if (theta > -theta_range && theta < theta_range) {
3389 u_shift = s->ih_flip ? width / 5 : 0;
3392 uf = (phi / M_PI * scale + 1.f) * ew / 2.f;
3393 vf = (theta / theta_range * scale + 1.f) * eh / 2.f;
3398 u_shift = s->ih_flip ? 0 : 4 * ew;
3400 if (theta < 0.f) { // UP
3401 uf = -vec[0] / vec[1];
3402 vf = -vec[2] / vec[1];
3405 uf = vec[0] / vec[1];
3406 vf = -vec[2] / vec[1];
3410 uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
3411 vf *= s->input_mirror_modifier[1];
3413 uf = 0.5f * ew * (uf * scale + 1.f);
3414 vf = 0.5f * eh * (vf * scale + 1.f);
3423 for (int i = 0; i < 4; i++) {
3424 for (int j = 0; j < 4; j++) {
3425 us[i][j] = u_shift + av_clip(ui + j - 1, 0, ew - 1);
3426 vs[i][j] = v_shift + av_clip(vi + i - 1, 0, eh - 1);
3434 * Calculate frame position in barrel split facebook's format for corresponding 3D coordinates on sphere.
3436 * @param s filter private context
3437 * @param vec coordinates on sphere
3438 * @param width frame width
3439 * @param height frame height
3440 * @param us horizontal coordinates for interpolation window
3441 * @param vs vertical coordinates for interpolation window
3442 * @param du horizontal relative coordinate
3443 * @param dv vertical relative coordinate
3445 static int xyz_to_barrelsplit(const V360Context *s,
3446 const float *vec, int width, int height,
3447 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3449 const float phi = atan2f(vec[0], vec[2]) * s->input_mirror_modifier[0];
3450 const float theta = asinf(vec[1]) * s->input_mirror_modifier[1];
3452 const float theta_range = M_PI_4;
3455 int u_shift, v_shift;
3459 if (theta >= -theta_range && theta <= theta_range) {
3460 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width * 2.f / 3.f) : 1.f - s->in_pad;
3461 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 2.f) : 1.f - s->in_pad;
3466 u_shift = s->ih_flip ? width / 3 : 0;
3467 v_shift = phi >= M_PI_2 || phi < -M_PI_2 ? eh : 0;
3469 uf = fmodf(phi, M_PI_2) / M_PI_2;
3470 vf = theta / M_PI_4;
3473 uf = uf >= 0.f ? fmodf(uf - 1.f, 1.f) : fmodf(uf + 1.f, 1.f);
3475 uf = (uf * scalew + 1.f) * width / 3.f;
3476 vf = (vf * scaleh + 1.f) * height / 4.f;
3478 const float scalew = s->fin_pad > 0 ? 1.f - s->fin_pad / (width / 3.f) : 1.f - s->in_pad;
3479 const float scaleh = s->fin_pad > 0 ? 1.f - s->fin_pad / (height / 4.f) : 1.f - s->in_pad;
3485 u_shift = s->ih_flip ? 0 : 2 * ew;
3487 if (theta <= 0.f && theta >= -M_PI_2 &&
3488 phi <= M_PI_2 && phi >= -M_PI_2) {
3489 uf = -vec[0] / vec[1];
3490 vf = -vec[2] / vec[1];
3493 } else if (theta >= 0.f && theta <= M_PI_2 &&
3494 phi <= M_PI_2 && phi >= -M_PI_2) {
3495 uf = vec[0] / vec[1];
3496 vf = -vec[2] / vec[1];
3497 v_shift = height * 0.25f;
3498 } else if (theta <= 0.f && theta >= -M_PI_2) {
3499 uf = vec[0] / vec[1];
3500 vf = vec[2] / vec[1];
3501 v_shift = height * 0.5f;
3504 uf = -vec[0] / vec[1];
3505 vf = vec[2] / vec[1];
3506 v_shift = height * 0.75f;
3509 uf *= s->input_mirror_modifier[0] * s->input_mirror_modifier[1];
3510 vf *= s->input_mirror_modifier[1];
3512 uf = 0.5f * width / 3.f * (uf * scalew + 1.f);
3513 vf = height * 0.25f * (vf * scaleh + 1.f) + v_offset;
3522 for (int i = 0; i < 4; i++) {
3523 for (int j = 0; j < 4; j++) {
3524 us[i][j] = u_shift + av_clip(ui + j - 1, 0, ew - 1);
3525 vs[i][j] = v_shift + av_clip(vi + i - 1, 0, eh - 1);
3533 * Calculate 3D coordinates on sphere for corresponding frame position in barrel split facebook's format.
3535 * @param s filter private context
3536 * @param i horizontal position on frame [0, width)
3537 * @param j vertical position on frame [0, height)
3538 * @param width frame width
3539 * @param height frame height
3540 * @param vec coordinates on sphere
3542 static int barrelsplit_to_xyz(const V360Context *s,
3543 int i, int j, int width, int height,
3546 const float x = (i + 0.5f) / width;
3547 const float y = (j + 0.5f) / height;
3548 float l_x, l_y, l_z;
3550 if (x < 2.f / 3.f) {
3551 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width * 2.f / 3.f) : 1.f - s->out_pad;
3552 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 2.f) : 1.f - s->out_pad;
3554 const float back = floorf(y * 2.f);
3556 const float phi = ((3.f / 2.f * x - 0.5f) / scalew - back) * M_PI;
3557 const float theta = (y - 0.25f - 0.5f * back) / scaleh * M_PI;
3559 const float sin_phi = sinf(phi);
3560 const float cos_phi = cosf(phi);
3561 const float sin_theta = sinf(theta);
3562 const float cos_theta = cosf(theta);
3564 l_x = cos_theta * sin_phi;
3566 l_z = cos_theta * cos_phi;
3568 const float scalew = s->fout_pad > 0 ? 1.f - s->fout_pad / (width / 3.f) : 1.f - s->out_pad;
3569 const float scaleh = s->fout_pad > 0 ? 1.f - s->fout_pad / (height / 4.f) : 1.f - s->out_pad;
3571 const int face = floorf(y * 4.f);
3582 l_x = (0.5f - uf) / scalew;
3584 l_z = (0.5f - vf) / scaleh;
3589 vf = 1.f - (vf - 0.5f);
3591 l_x = (0.5f - uf) / scalew;
3593 l_z = (-0.5f + vf) / scaleh;
3596 vf = y * 2.f - 0.5f;
3597 vf = 1.f - (1.f - vf);
3599 l_x = (0.5f - uf) / scalew;
3601 l_z = (0.5f - vf) / scaleh;
3604 vf = y * 2.f - 1.5f;
3606 l_x = (0.5f - uf) / scalew;
3608 l_z = (-0.5f + vf) / scaleh;
3617 normalize_vector(vec);
3623 * Calculate 3D coordinates on sphere for corresponding frame position in tspyramid format.
3625 * @param s filter private context
3626 * @param i horizontal position on frame [0, width)
3627 * @param j vertical position on frame [0, height)
3628 * @param width frame width
3629 * @param height frame height
3630 * @param vec coordinates on sphere
3632 static int tspyramid_to_xyz(const V360Context *s,
3633 int i, int j, int width, int height,
3636 const float x = (i + 0.5f) / width;
3637 const float y = (j + 0.5f) / height;
3640 vec[0] = x * 4.f - 1.f;
3641 vec[1] = (y * 2.f - 1.f);
3643 } else if (x >= 0.6875f && x < 0.8125f &&
3644 y >= 0.375f && y < 0.625f) {
3645 vec[0] = -(x - 0.6875f) * 16.f + 1.f;
3646 vec[1] = (y - 0.375f) * 8.f - 1.f;
3648 } else if (0.5f <= x && x < 0.6875f &&
3649 ((0.f <= y && y < 0.375f && y >= 2.f * (x - 0.5f)) ||
3650 (0.375f <= y && y < 0.625f) ||
3651 (0.625f <= y && y < 1.f && y <= 2.f * (1.f - x)))) {
3653 vec[1] = 2.f * (y - 2.f * x + 1.f) / (3.f - 4.f * x) - 1.f;
3654 vec[2] = -2.f * (x - 0.5f) / 0.1875f + 1.f;
3655 } else if (0.8125f <= x && x < 1.f &&
3656 ((0.f <= y && y < 0.375f && x >= (1.f - y / 2.f)) ||
3657 (0.375f <= y && y < 0.625f) ||
3658 (0.625f <= y && y < 1.f && y <= (2.f * x - 1.f)))) {
3660 vec[1] = 2.f * (y + 2.f * x - 2.f) / (4.f * x - 3.f) - 1.f;
3661 vec[2] = 2.f * (x - 0.8125f) / 0.1875f - 1.f;
3662 } else if (0.f <= y && y < 0.375f &&
3663 ((0.5f <= x && x < 0.8125f && y < 2.f * (x - 0.5f)) ||
3664 (0.6875f <= x && x < 0.8125f) ||
3665 (0.8125f <= x && x < 1.f && x < (1.f - y / 2.f)))) {
3666 vec[0] = 2.f * (1.f - x - 0.5f * y) / (0.5f - y) - 1.f;
3668 vec[2] = 2.f * (0.375f - y) / 0.375f - 1.f;
3670 vec[0] = 2.f * (0.5f - x + 0.5f * y) / (y - 0.5f) - 1.f;
3672 vec[2] = -2.f * (1.f - y) / 0.375f + 1.f;
3675 normalize_vector(vec);
3681 * Calculate frame position in tspyramid format for corresponding 3D coordinates on sphere.
3683 * @param s filter private context
3684 * @param vec coordinates on sphere
3685 * @param width frame width
3686 * @param height frame height
3687 * @param us horizontal coordinates for interpolation window
3688 * @param vs vertical coordinates for interpolation window
3689 * @param du horizontal relative coordinate
3690 * @param dv vertical relative coordinate
3692 static int xyz_to_tspyramid(const V360Context *s,
3693 const float *vec, int width, int height,
3694 int16_t us[4][4], int16_t vs[4][4], float *du, float *dv)
3700 xyz_to_cube(s, vec, &uf, &vf, &face);
3702 uf = (uf + 1.f) * 0.5f;
3703 vf = (vf + 1.f) * 0.5f;
3707 uf = 0.1875f * vf - 0.375f * uf * vf - 0.125f * uf + 0.8125f;
3708 vf = 0.375f - 0.375f * vf;
3714 uf = 1.f - 0.1875f * vf - 0.5f * uf + 0.375f * uf * vf;
3715 vf = 1.f - 0.375f * vf;
3718 vf = 0.25f * vf + 0.75f * uf * vf - 0.375f * uf + 0.375f;
3719 uf = 0.1875f * uf + 0.8125f;
3722 vf = 0.375f * uf - 0.75f * uf * vf + vf;
3723 uf = 0.1875f * uf + 0.5f;
3726 uf = 0.125f * uf + 0.6875f;
3727 vf = 0.25f * vf + 0.375f;
3740 for (int i = 0; i < 4; i++) {
3741 for (int j = 0; j < 4; j++) {
3742 us[i][j] = reflectx(ui + j - 1, vi + i - 1, width, height);
3743 vs[i][j] = reflecty(vi + i - 1, height);
3750 static void multiply_matrix(float c[3][3], const float a[3][3], const float b[3][3])
3752 for (int i = 0; i < 3; i++) {
3753 for (int j = 0; j < 3; j++) {
3756 for (int k = 0; k < 3; k++)
3757 sum += a[i][k] * b[k][j];
3765 * Calculate rotation matrix for yaw/pitch/roll angles.
3767 static inline void calculate_rotation_matrix(float yaw, float pitch, float roll,
3768 float rot_mat[3][3],
3769 const int rotation_order[3])
3771 const float yaw_rad = yaw * M_PI / 180.f;
3772 const float pitch_rad = pitch * M_PI / 180.f;
3773 const float roll_rad = roll * M_PI / 180.f;
3775 const float sin_yaw = sinf(yaw_rad);
3776 const float cos_yaw = cosf(yaw_rad);
3777 const float sin_pitch = sinf(pitch_rad);
3778 const float cos_pitch = cosf(pitch_rad);
3779 const float sin_roll = sinf(roll_rad);
3780 const float cos_roll = cosf(roll_rad);
3785 m[0][0][0] = cos_yaw; m[0][0][1] = 0; m[0][0][2] = sin_yaw;
3786 m[0][1][0] = 0; m[0][1][1] = 1; m[0][1][2] = 0;
3787 m[0][2][0] = -sin_yaw; m[0][2][1] = 0; m[0][2][2] = cos_yaw;
3789 m[1][0][0] = 1; m[1][0][1] = 0; m[1][0][2] = 0;
3790 m[1][1][0] = 0; m[1][1][1] = cos_pitch; m[1][1][2] = -sin_pitch;
3791 m[1][2][0] = 0; m[1][2][1] = sin_pitch; m[1][2][2] = cos_pitch;
3793 m[2][0][0] = cos_roll; m[2][0][1] = -sin_roll; m[2][0][2] = 0;
3794 m[2][1][0] = sin_roll; m[2][1][1] = cos_roll; m[2][1][2] = 0;
3795 m[2][2][0] = 0; m[2][2][1] = 0; m[2][2][2] = 1;
3797 multiply_matrix(temp, m[rotation_order[0]], m[rotation_order[1]]);
3798 multiply_matrix(rot_mat, temp, m[rotation_order[2]]);
3802 * Rotate vector with given rotation matrix.
3804 * @param rot_mat rotation matrix
3807 static inline void rotate(const float rot_mat[3][3],
3810 const float x_tmp = vec[0] * rot_mat[0][0] + vec[1] * rot_mat[0][1] + vec[2] * rot_mat[0][2];
3811 const float y_tmp = vec[0] * rot_mat[1][0] + vec[1] * rot_mat[1][1] + vec[2] * rot_mat[1][2];
3812 const float z_tmp = vec[0] * rot_mat[2][0] + vec[1] * rot_mat[2][1] + vec[2] * rot_mat[2][2];
3819 static inline void set_mirror_modifier(int h_flip, int v_flip, int d_flip,
3822 modifier[0] = h_flip ? -1.f : 1.f;
3823 modifier[1] = v_flip ? -1.f : 1.f;
3824 modifier[2] = d_flip ? -1.f : 1.f;
3827 static inline void mirror(const float *modifier, float *vec)
3829 vec[0] *= modifier[0];
3830 vec[1] *= modifier[1];
3831 vec[2] *= modifier[2];
3834 static int allocate_plane(V360Context *s, int sizeof_uv, int sizeof_ker, int sizeof_mask, int p)
3837 s->u[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
3839 s->v[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_uv);
3840 if (!s->u[p] || !s->v[p])
3841 return AVERROR(ENOMEM);
3844 s->ker[p] = av_calloc(s->uv_linesize[p] * s->pr_height[p], sizeof_ker);
3846 return AVERROR(ENOMEM);
3849 if (sizeof_mask && !p) {
3851 s->mask = av_calloc(s->pr_width[p] * s->pr_height[p], sizeof_mask);
3853 return AVERROR(ENOMEM);
3859 static void fov_from_dfov(int format, float d_fov, float w, float h, float *h_fov, float *v_fov)
3864 const float d = 0.5f * hypotf(w, h);
3865 const float l = sinf(d_fov * M_PI / 360.f) / d;
3867 *h_fov = asinf(w * 0.5 * l) * 360.f / M_PI;
3868 *v_fov = asinf(h * 0.5 * l) * 360.f / M_PI;
3870 if (d_fov > 180.f) {
3871 *h_fov = 180.f - *h_fov;
3872 *v_fov = 180.f - *v_fov;
3878 const float d = 0.5f * hypotf(w, h);
3879 const float l = d / (sinf(d_fov * M_PI / 720.f));
3881 *h_fov = 2.f * asinf(w * 0.5f / l) * 360.f / M_PI;
3882 *v_fov = 2.f * asinf(h * 0.5f / l) * 360.f / M_PI;
3887 const float d = 0.5f * hypotf(w, h);
3888 const float l = d / (tanf(d_fov * M_PI / 720.f));
3890 *h_fov = 2.f * atan2f(w * 0.5f, l) * 360.f / M_PI;
3891 *v_fov = 2.f * atan2f(h * 0.5f, l) * 360.f / M_PI;
3896 const float d = 0.5f * hypotf(w * 0.5f, h);
3898 *h_fov = d / w * 2.f * d_fov;
3899 *v_fov = d / h * d_fov;
3904 const float d = 0.5f * hypotf(w, h);
3906 *h_fov = d / w * d_fov;
3907 *v_fov = d / h * d_fov;
3913 const float da = tanf(0.5f * FFMIN(d_fov, 359.f) * M_PI / 180.f);
3914 const float d = hypotf(w, h);
3916 *h_fov = atan2f(da * w, d) * 360.f / M_PI;
3917 *v_fov = atan2f(da * h, d) * 360.f / M_PI;
3928 static void set_dimensions(int *outw, int *outh, int w, int h, const AVPixFmtDescriptor *desc)
3930 outw[1] = outw[2] = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
3931 outw[0] = outw[3] = w;
3932 outh[1] = outh[2] = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
3933 outh[0] = outh[3] = h;
3936 // Calculate remap data
3937 static av_always_inline int v360_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
3939 V360Context *s = ctx->priv;
3941 for (int p = 0; p < s->nb_allocated; p++) {
3942 const int max_value = s->max_value;
3943 const int width = s->pr_width[p];
3944 const int uv_linesize = s->uv_linesize[p];
3945 const int height = s->pr_height[p];
3946 const int in_width = s->inplanewidth[p];
3947 const int in_height = s->inplaneheight[p];
3948 const int slice_start = (height * jobnr ) / nb_jobs;
3949 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
3954 for (int j = slice_start; j < slice_end; j++) {
3955 for (int i = 0; i < width; i++) {
3956 int16_t *u = s->u[p] + (j * uv_linesize + i) * s->elements;
3957 int16_t *v = s->v[p] + (j * uv_linesize + i) * s->elements;
3958 int16_t *ker = s->ker[p] + (j * uv_linesize + i) * s->elements;
3959 uint8_t *mask8 = p ? NULL : s->mask + (j * s->pr_width[0] + i);
3960 uint16_t *mask16 = p ? NULL : (uint16_t *)s->mask + (j * s->pr_width[0] + i);
3961 int in_mask, out_mask;
3963 if (s->out_transpose)
3964 out_mask = s->out_transform(s, j, i, height, width, vec);
3966 out_mask = s->out_transform(s, i, j, width, height, vec);
3967 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
3968 rotate(s->rot_mat, vec);
3969 av_assert1(!isnan(vec[0]) && !isnan(vec[1]) && !isnan(vec[2]));
3970 normalize_vector(vec);
3971 mirror(s->output_mirror_modifier, vec);
3972 if (s->in_transpose)
3973 in_mask = s->in_transform(s, vec, in_height, in_width, rmap.v, rmap.u, &du, &dv);
3975 in_mask = s->in_transform(s, vec, in_width, in_height, rmap.u, rmap.v, &du, &dv);
3976 av_assert1(!isnan(du) && !isnan(dv));
3977 s->calculate_kernel(du, dv, &rmap, u, v, ker);
3979 if (!p && s->mask) {
3980 if (s->mask_size == 1) {
3981 mask8[0] = 255 * (out_mask & in_mask);
3983 mask16[0] = max_value * (out_mask & in_mask);
3993 static int config_output(AVFilterLink *outlink)
3995 AVFilterContext *ctx = outlink->src;
3996 AVFilterLink *inlink = ctx->inputs[0];
3997 V360Context *s = ctx->priv;
3998 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
3999 const int depth = desc->comp[0].depth;
4000 const int sizeof_mask = s->mask_size = (depth + 7) >> 3;
4005 int in_offset_h, in_offset_w;
4006 int out_offset_h, out_offset_w;
4008 int (*prepare_out)(AVFilterContext *ctx);
4011 s->max_value = (1 << depth) - 1;
4012 s->input_mirror_modifier[0] = s->ih_flip ? -1.f : 1.f;
4013 s->input_mirror_modifier[1] = s->iv_flip ? -1.f : 1.f;
4015 switch (s->interp) {
4017 s->calculate_kernel = nearest_kernel;
4018 s->remap_slice = depth <= 8 ? remap1_8bit_slice : remap1_16bit_slice;
4020 sizeof_uv = sizeof(int16_t) * s->elements;
4024 s->calculate_kernel = bilinear_kernel;
4025 s->remap_slice = depth <= 8 ? remap2_8bit_slice : remap2_16bit_slice;
4026 s->elements = 2 * 2;
4027 sizeof_uv = sizeof(int16_t) * s->elements;
4028 sizeof_ker = sizeof(int16_t) * s->elements;
4031 s->calculate_kernel = lagrange_kernel;
4032 s->remap_slice = depth <= 8 ? remap3_8bit_slice : remap3_16bit_slice;
4033 s->elements = 3 * 3;
4034 sizeof_uv = sizeof(int16_t) * s->elements;
4035 sizeof_ker = sizeof(int16_t) * s->elements;
4038 s->calculate_kernel = bicubic_kernel;
4039 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
4040 s->elements = 4 * 4;
4041 sizeof_uv = sizeof(int16_t) * s->elements;
4042 sizeof_ker = sizeof(int16_t) * s->elements;
4045 s->calculate_kernel = lanczos_kernel;
4046 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
4047 s->elements = 4 * 4;
4048 sizeof_uv = sizeof(int16_t) * s->elements;
4049 sizeof_ker = sizeof(int16_t) * s->elements;
4052 s->calculate_kernel = spline16_kernel;
4053 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
4054 s->elements = 4 * 4;
4055 sizeof_uv = sizeof(int16_t) * s->elements;
4056 sizeof_ker = sizeof(int16_t) * s->elements;
4059 s->calculate_kernel = gaussian_kernel;
4060 s->remap_slice = depth <= 8 ? remap4_8bit_slice : remap4_16bit_slice;
4061 s->elements = 4 * 4;
4062 sizeof_uv = sizeof(int16_t) * s->elements;
4063 sizeof_ker = sizeof(int16_t) * s->elements;
4069 ff_v360_init(s, depth);
4071 for (int order = 0; order < NB_RORDERS; order++) {
4072 const char c = s->rorder[order];
4076 av_log(ctx, AV_LOG_WARNING,
4077 "Incomplete rorder option. Direction for all 3 rotation orders should be specified. Switching to default rorder.\n");
4078 s->rotation_order[0] = YAW;
4079 s->rotation_order[1] = PITCH;
4080 s->rotation_order[2] = ROLL;
4084 rorder = get_rorder(c);
4086 av_log(ctx, AV_LOG_WARNING,
4087 "Incorrect rotation order symbol '%c' in rorder option. Switching to default rorder.\n", c);
4088 s->rotation_order[0] = YAW;
4089 s->rotation_order[1] = PITCH;
4090 s->rotation_order[2] = ROLL;
4094 s->rotation_order[order] = rorder;
4097 switch (s->in_stereo) {
4101 in_offset_w = in_offset_h = 0;
4119 set_dimensions(s->inplanewidth, s->inplaneheight, w, h, desc);
4120 set_dimensions(s->in_offset_w, s->in_offset_h, in_offset_w, in_offset_h, desc);
4122 s->in_width = s->inplanewidth[0];
4123 s->in_height = s->inplaneheight[0];
4125 if (s->id_fov > 0.f)
4126 fov_from_dfov(s->in, s->id_fov, w, h, &s->ih_fov, &s->iv_fov);
4128 if (s->in_transpose)
4129 FFSWAP(int, s->in_width, s->in_height);
4132 case EQUIRECTANGULAR:
4133 s->in_transform = xyz_to_equirect;
4139 s->in_transform = xyz_to_cube3x2;
4140 err = prepare_cube_in(ctx);
4145 s->in_transform = xyz_to_cube1x6;
4146 err = prepare_cube_in(ctx);
4151 s->in_transform = xyz_to_cube6x1;
4152 err = prepare_cube_in(ctx);
4157 s->in_transform = xyz_to_eac;
4158 err = prepare_eac_in(ctx);
4163 s->in_transform = xyz_to_flat;
4164 err = prepare_flat_in(ctx);
4169 av_log(ctx, AV_LOG_ERROR, "Supplied format is not accepted as input.\n");
4170 return AVERROR(EINVAL);
4172 s->in_transform = xyz_to_dfisheye;
4173 err = prepare_fisheye_in(ctx);
4178 s->in_transform = xyz_to_barrel;
4184 s->in_transform = xyz_to_stereographic;
4185 err = prepare_stereographic_in(ctx);
4190 s->in_transform = xyz_to_mercator;
4196 s->in_transform = xyz_to_ball;
4202 s->in_transform = xyz_to_hammer;
4208 s->in_transform = xyz_to_sinusoidal;
4214 s->in_transform = xyz_to_fisheye;
4215 err = prepare_fisheye_in(ctx);
4220 s->in_transform = xyz_to_pannini;
4226 s->in_transform = xyz_to_cylindrical;
4227 err = prepare_cylindrical_in(ctx);
4232 s->in_transform = xyz_to_tetrahedron;
4238 s->in_transform = xyz_to_barrelsplit;
4244 s->in_transform = xyz_to_tspyramid;
4249 case HEQUIRECTANGULAR:
4250 s->in_transform = xyz_to_hequirect;
4256 s->in_transform = xyz_to_equisolid;
4257 err = prepare_equisolid_in(ctx);
4262 s->in_transform = xyz_to_orthographic;
4263 err = prepare_orthographic_in(ctx);
4268 av_log(ctx, AV_LOG_ERROR, "Specified input format is not handled.\n");
4277 case EQUIRECTANGULAR:
4278 s->out_transform = equirect_to_xyz;
4284 s->out_transform = cube3x2_to_xyz;
4285 prepare_out = prepare_cube_out;
4286 w = lrintf(wf / 4.f * 3.f);
4290 s->out_transform = cube1x6_to_xyz;
4291 prepare_out = prepare_cube_out;
4292 w = lrintf(wf / 4.f);
4293 h = lrintf(hf * 3.f);
4296 s->out_transform = cube6x1_to_xyz;
4297 prepare_out = prepare_cube_out;
4298 w = lrintf(wf / 2.f * 3.f);
4299 h = lrintf(hf / 2.f);
4302 s->out_transform = eac_to_xyz;
4303 prepare_out = prepare_eac_out;
4305 h = lrintf(hf / 8.f * 9.f);
4308 s->out_transform = flat_to_xyz;
4309 prepare_out = prepare_flat_out;
4314 s->out_transform = dfisheye_to_xyz;
4315 prepare_out = prepare_fisheye_out;
4320 s->out_transform = barrel_to_xyz;
4322 w = lrintf(wf / 4.f * 5.f);
4326 s->out_transform = stereographic_to_xyz;
4327 prepare_out = prepare_stereographic_out;
4329 h = lrintf(hf * 2.f);
4332 s->out_transform = mercator_to_xyz;
4335 h = lrintf(hf * 2.f);
4338 s->out_transform = ball_to_xyz;
4341 h = lrintf(hf * 2.f);
4344 s->out_transform = hammer_to_xyz;
4350 s->out_transform = sinusoidal_to_xyz;
4356 s->out_transform = fisheye_to_xyz;
4357 prepare_out = prepare_fisheye_out;
4358 w = lrintf(wf * 0.5f);
4362 s->out_transform = pannini_to_xyz;
4368 s->out_transform = cylindrical_to_xyz;
4369 prepare_out = prepare_cylindrical_out;
4371 h = lrintf(hf * 0.5f);
4374 s->out_transform = perspective_to_xyz;
4376 w = lrintf(wf / 2.f);
4380 s->out_transform = tetrahedron_to_xyz;
4386 s->out_transform = barrelsplit_to_xyz;
4388 w = lrintf(wf / 4.f * 3.f);
4392 s->out_transform = tspyramid_to_xyz;
4397 case HEQUIRECTANGULAR:
4398 s->out_transform = hequirect_to_xyz;
4400 w = lrintf(wf / 2.f);
4404 s->out_transform = equisolid_to_xyz;
4405 prepare_out = prepare_equisolid_out;
4407 h = lrintf(hf * 2.f);
4410 s->out_transform = orthographic_to_xyz;
4411 prepare_out = prepare_orthographic_out;
4413 h = lrintf(hf * 2.f);
4416 av_log(ctx, AV_LOG_ERROR, "Specified output format is not handled.\n");
4420 // Override resolution with user values if specified
4421 if (s->width > 0 && s->height <= 0 && s->h_fov > 0.f && s->v_fov > 0.f &&
4422 s->out == FLAT && s->d_fov == 0.f) {
4424 h = w / tanf(s->h_fov * M_PI / 360.f) * tanf(s->v_fov * M_PI / 360.f);
4425 } else if (s->width <= 0 && s->height > 0 && s->h_fov > 0.f && s->v_fov > 0.f &&
4426 s->out == FLAT && s->d_fov == 0.f) {
4428 w = h / tanf(s->v_fov * M_PI / 360.f) * tanf(s->h_fov * M_PI / 360.f);
4429 } else if (s->width > 0 && s->height > 0) {
4432 } else if (s->width > 0 || s->height > 0) {
4433 av_log(ctx, AV_LOG_ERROR, "Both width and height values should be specified.\n");
4434 return AVERROR(EINVAL);
4436 if (s->out_transpose)
4439 if (s->in_transpose)
4447 fov_from_dfov(s->out, s->d_fov, w, h, &s->h_fov, &s->v_fov);
4450 err = prepare_out(ctx);
4455 set_dimensions(s->pr_width, s->pr_height, w, h, desc);
4457 switch (s->out_stereo) {
4459 out_offset_w = out_offset_h = 0;
4475 set_dimensions(s->out_offset_w, s->out_offset_h, out_offset_w, out_offset_h, desc);
4476 set_dimensions(s->planewidth, s->planeheight, w, h, desc);
4478 for (int i = 0; i < 4; i++)
4479 s->uv_linesize[i] = FFALIGN(s->pr_width[i], 8);
4484 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
4485 have_alpha = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
4487 if (desc->log2_chroma_h == desc->log2_chroma_w && desc->log2_chroma_h == 0) {
4488 s->nb_allocated = 1;
4489 s->map[0] = s->map[1] = s->map[2] = s->map[3] = 0;
4491 s->nb_allocated = 2;
4492 s->map[0] = s->map[3] = 0;
4493 s->map[1] = s->map[2] = 1;
4496 for (int i = 0; i < s->nb_allocated; i++) {
4497 err = allocate_plane(s, sizeof_uv, sizeof_ker, sizeof_mask * have_alpha * s->alpha, i);
4502 calculate_rotation_matrix(s->yaw, s->pitch, s->roll, s->rot_mat, s->rotation_order);
4503 set_mirror_modifier(s->h_flip, s->v_flip, s->d_flip, s->output_mirror_modifier);
4505 ctx->internal->execute(ctx, v360_slice, NULL, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
4510 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
4512 AVFilterContext *ctx = inlink->dst;
4513 AVFilterLink *outlink = ctx->outputs[0];
4514 V360Context *s = ctx->priv;
4518 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
4521 return AVERROR(ENOMEM);
4523 av_frame_copy_props(out, in);
4528 ctx->internal->execute(ctx, s->remap_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
4531 return ff_filter_frame(outlink, out);
4534 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
4535 char *res, int res_len, int flags)
4539 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
4543 return config_output(ctx->outputs[0]);
4546 static av_cold void uninit(AVFilterContext *ctx)
4548 V360Context *s = ctx->priv;
4550 for (int p = 0; p < s->nb_allocated; p++) {
4553 av_freep(&s->ker[p]);
4558 static const AVFilterPad inputs[] = {
4561 .type = AVMEDIA_TYPE_VIDEO,
4562 .filter_frame = filter_frame,
4567 static const AVFilterPad outputs[] = {
4570 .type = AVMEDIA_TYPE_VIDEO,
4571 .config_props = config_output,
4576 AVFilter ff_vf_v360 = {
4578 .description = NULL_IF_CONFIG_SMALL("Convert 360 projection of video."),
4579 .priv_size = sizeof(V360Context),
4581 .query_formats = query_formats,
4584 .priv_class = &v360_class,
4585 .flags = AVFILTER_FLAG_SLICE_THREADS,
4586 .process_command = process_command,